2018-11-15 19:48:09 +00:00
|
|
|
// Copyright 2019 syzkaller project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/google/syzkaller/pkg/osutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) != 2 {
|
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
|
|
|
syslog, err := ioutil.ReadFile(args[0])
|
|
|
|
if err != nil {
|
|
|
|
failf("failed to read file %v: %v", args[0], err)
|
|
|
|
}
|
|
|
|
|
2019-07-22 17:25:54 +00:00
|
|
|
usbIds := extractIds(syslog, "USBID", 34)
|
|
|
|
hidIds := extractIds(syslog, "HIDID", 24)
|
|
|
|
|
2020-07-29 11:35:50 +00:00
|
|
|
output := []byte(`// Code generated by tools/syz-usbgen. DO NOT EDIT.
|
|
|
|
// See docs/linux/external_fuzzing_usb.md
|
|
|
|
|
|
|
|
package linux
|
|
|
|
|
|
|
|
`)
|
2019-07-22 17:25:54 +00:00
|
|
|
output = append(output, generateIdsVar(usbIds, "usbIds")...)
|
|
|
|
output = append(output, []byte("\n")...)
|
|
|
|
output = append(output, generateIdsVar(hidIds, "hidIds")...)
|
|
|
|
|
|
|
|
if err := osutil.WriteFile(args[1], output); err != nil {
|
|
|
|
failf("failed to output file %v: %v", args[1], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractIds(syslog []byte, prefix string, size int) []string {
|
|
|
|
re := fmt.Sprintf("%s: [0-9a-f]{%d}", prefix, size)
|
|
|
|
r := regexp.MustCompile(re)
|
2018-11-15 19:48:09 +00:00
|
|
|
matches := r.FindAll(syslog, -1)
|
|
|
|
uniqueMatches := make(map[string]bool)
|
|
|
|
for _, match := range matches {
|
|
|
|
uniqueMatches[string(match)] = true
|
|
|
|
}
|
|
|
|
sortedMatches := make([]string, 0)
|
|
|
|
for match := range uniqueMatches {
|
2019-07-22 17:25:54 +00:00
|
|
|
match = match[len(prefix+": "):]
|
|
|
|
match = match[:size]
|
2018-11-15 19:48:09 +00:00
|
|
|
sortedMatches = append(sortedMatches, match)
|
|
|
|
}
|
|
|
|
sort.Strings(sortedMatches)
|
2019-07-22 17:25:54 +00:00
|
|
|
return sortedMatches
|
|
|
|
}
|
2018-11-15 19:48:09 +00:00
|
|
|
|
2019-07-22 17:25:54 +00:00
|
|
|
func generateIdsVar(ids []string, name string) []byte {
|
|
|
|
output := []byte(fmt.Sprintf("var %s = ", name))
|
|
|
|
for i, id := range ids {
|
|
|
|
decodedID, err := hex.DecodeString(id)
|
2018-11-15 19:48:09 +00:00
|
|
|
if err != nil {
|
2019-07-22 17:25:54 +00:00
|
|
|
failf("failed to decode hex string %v: %v", id, err)
|
2018-11-15 19:48:09 +00:00
|
|
|
}
|
|
|
|
prefix := "\t"
|
|
|
|
suffix := " +"
|
|
|
|
if i == 0 {
|
|
|
|
prefix = ""
|
|
|
|
}
|
2019-07-22 17:25:54 +00:00
|
|
|
if i == len(ids)-1 {
|
2018-11-15 19:48:09 +00:00
|
|
|
suffix = ""
|
|
|
|
}
|
2019-07-22 17:25:54 +00:00
|
|
|
outputID := fmt.Sprintf("%v%#v%v\n", prefix, string(decodedID), suffix)
|
|
|
|
output = append(output, []byte(outputID)...)
|
2018-11-15 19:48:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 17:25:54 +00:00
|
|
|
fmt.Printf("%v %s ids written\n", len(ids), name)
|
2018-11-15 19:48:09 +00:00
|
|
|
|
2019-07-22 17:25:54 +00:00
|
|
|
return output
|
2018-11-15 19:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func usage() {
|
|
|
|
fmt.Fprintf(os.Stderr, "usage:\n")
|
|
|
|
fmt.Fprintf(os.Stderr, " syz-usbgen syslog.txt sys/linux/init_vusb_ids.go\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func failf(msg string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, msg+"\n", args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|