diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2022-11-23 15:13:59 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2022-11-23 16:43:13 +0100 |
| commit | 0f8654c4164c291313fae13e8e15adea1a9630bd (patch) | |
| tree | dbb64895e61e054ed3f9b258e16e4267b83d43a5 /tools/syz-imagegen | |
| parent | 3c2d90bcdbe790a5e28bd49152c75c611664cc28 (diff) | |
tools/syz-imagegen: generate syz_read_part_table seeds
Fixes #3529
Diffstat (limited to 'tools/syz-imagegen')
| -rw-r--r-- | tools/syz-imagegen/imagegen.go | 66 |
1 files changed, 51 insertions, 15 deletions
diff --git a/tools/syz-imagegen/imagegen.go b/tools/syz-imagegen/imagegen.go index 4cd9806be..fdf68b721 100644 --- a/tools/syz-imagegen/imagegen.go +++ b/tools/syz-imagegen/imagegen.go @@ -461,6 +461,48 @@ var fileSystems = []FileSystem{ return err }, }, + { + Name: parttable, + MinSize: 1 << 20, + ReadOnly: true, + MkfsFlagCombinations: [][]string{{ + // You can test/explore these commands with: + // $ rm -f disk && touch disk && fallocate -l 16M disk && fdisk disk + "g n 1 34 47 n 2 48 96 n 3 97 2013 t 1 uefi t 2 linux t 3 swap w", + "g n 1 100 200 n 2 50 80 n 3 1000 1900 M a c r x n 1 syzkaller A 1 r w", + "g n 3 200 300 n 7 400 500 n 2 600 700 x l 700 r w", + "G n 1 16065 16265 w", + "G n 16 20000 30000 i w", + "o n p 1 2048 4096 n p 3 4097 4100 n e 2 4110 4200 a 1 t 2 a5 t 3 uefi b y x 1 e r w", + "o n p 1 2048 3071 n e 3 3072 32767 n 6200 7999 n 10240 20000 w", + "s c 1 a 2 w", + }}, + Mkfs: func(image *Image) error { + cmd := exec.Command("fdisk", "--noauto-pt", "--color=always", image.disk) + cmd.Stdin = strings.NewReader(strings.Join(image.flags, "\n")) + output, err := osutil.Run(10*time.Minute, cmd) + if err != nil { + return err + } + // It's hard to understand if any of the commands fail, + // fdisk does not exit with an error and print assorted error messages. + // So instead we run it with --color=always and grep for red color marker + // in the output (ESC[31m). + if bytes.Contains(output, []byte{0x1b, 0x5b, 0x33, 0x31, 0x6d}) { + return errors.New(string(output)) + } + return err + }, + }, +} + +const parttable = "parttable" + +func (fs FileSystem) filePrefix() string { + if fs.Name == parttable { + return "syz_read_part_table" + } + return "syz_mount_image_" + fs.Name } // Image represents one image we generate for a file system. @@ -611,7 +653,7 @@ func generateImages(target *prog.Target, flagFS string, list bool) ([]*Image, er fmt.Printf("%v [%v images]\n", fs.Name, index) continue } - files, err := filepath.Glob(filepath.Join("sys", targets.Linux, "test", "syz_mount_image_"+fs.Name+"_*")) + files, err := filepath.Glob(filepath.Join("sys", targets.Linux, "test", fs.filePrefix()+"_*")) if err != nil { return nil, fmt.Errorf("error reading output dir: %v", err) } @@ -659,7 +701,7 @@ func (image *Image) generate() error { func (image *Image) generateSize() error { outFile := filepath.Join("sys", targets.Linux, "test", - fmt.Sprintf("syz_mount_image_%v_%v", image.fs.Name, image.index)) + fmt.Sprintf("%v_%v", image.fs.filePrefix(), image.index)) image.disk = outFile + ".img" f, err := os.Create(image.disk) if err != nil { @@ -692,7 +734,7 @@ func (image *Image) generateSize() error { image.hash = crc32.ChecksumIEEE(data) // Write out image *with* change of directory. - out, err := writeImage(image, data, true) + out, err := writeImage(image, data) if err != nil { return fmt.Errorf("failed to write image: %v", err) } @@ -767,27 +809,21 @@ func runCmd(cmd string, args ...string) ([]byte, error) { return osutil.RunCmd(10*time.Minute, "", cmd, args...) } -func writeImage(image *Image, data []byte, chdir bool) ([]byte, error) { +func writeImage(image *Image, data []byte) ([]byte, error) { buf := new(bytes.Buffer) fmt.Fprintf(buf, "# Code generated by tools/syz-imagegen. DO NOT EDIT.\n") fmt.Fprintf(buf, "# requires: manual\n\n") fmt.Fprintf(buf, "# %v\n\n", image) compressedData := prog.Compress(data) b64Data := prog.EncodeB64(compressedData) - chdirAsInt := 0 - if chdir { - chdirAsInt = 1 + if image.fs.Name == parttable { + fmt.Fprintf(buf, `syz_read_part_table(AUTO, &AUTO="$`) + } else { + fmt.Fprintf(buf, `syz_mount_image$%v(&AUTO='%v\x00', &AUTO='./file0\x00', 0x0, &AUTO, 0x1, AUTO, &AUTO="$`, + image.fs.Name, image.fs.Name) } - fmt.Fprintf(buf, `syz_mount_image$%v(&AUTO='%v\x00', &AUTO='./file0\x00', 0x0, &AUTO, 0x%x, AUTO, &AUTO="$`, - image.fs.Name, image.fs.Name, chdirAsInt) buf.Write(b64Data) fmt.Fprintf(buf, "\")\n") return buf.Bytes(), nil } - -// TODO: also generate syz_read_part_table tests: -// fmt.Fprintf(buf, `syz_read_part_table(0x%x, 0x%x, &AUTO="$`, -// len(data), len(compressedData)) -// buf.Write(b64Data) -// fmt.Fprintf(buf, "\")\n") |
