| 1 | diff -ruN a/request-server_fuzz_test.go b/request-server_fuzz_test.go |
| 2 | |
| 3 | |
| 4 | @@ -0,0 +1,77 @@ |
| 5 | +package sftp |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/binary" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +type fuzzConn struct { |
| 17 | + io.Reader |
| 18 | +} |
| 19 | + |
| 20 | +func (fuzzConn) Write(p []byte) (int, error) { return len(p), nil } |
| 21 | +func (fuzzConn) Close() error { return nil } |
| 22 | + |
| 23 | +func fuzzFrame(typ byte, payload ...[]byte) []byte { |
| 24 | + body := append([]byte{typ}, bytes.Join(payload, nil)...) |
| 25 | + return append(binary.BigEndian.AppendUint32(nil, uint32(len(body))), body...) |
| 26 | +} |
| 27 | + |
| 28 | +func fuzzU32(v uint32) []byte { return binary.BigEndian.AppendUint32(nil, v) } |
| 29 | + |
| 30 | +// fuzzHandler keeps nothing: what is fuzzed is the protocol, not a handler |
| 31 | +// (InMemHandler grows a buffer to whatever offset a WRITE names). |
| 32 | +type fuzzHandler struct{} |
| 33 | + |
| 34 | +type discardAt struct{} |
| 35 | + |
| 36 | +func (discardAt) WriteAt(p []byte, _ int64) (int, error) { return len(p), nil } |
| 37 | + |
| 38 | +type fuzzLister struct{} |
| 39 | + |
| 40 | +func (fuzzLister) ListAt([]os.FileInfo, int64) (int, error) { return 0, io.EOF } |
| 41 | + |
| 42 | +func (fuzzHandler) Fileread(*Request) (io.ReaderAt, error) { return bytes.NewReader([]byte("content")), nil } |
| 43 | +func (fuzzHandler) Filewrite(*Request) (io.WriterAt, error) { return discardAt{}, nil } |
| 44 | +func (fuzzHandler) Filecmd(*Request) error { return nil } |
| 45 | +func (fuzzHandler) Filelist(*Request) (ListerAt, error) { return fuzzLister{}, nil } |
| 46 | +func fuzzStr(s string) []byte { return append(fuzzU32(uint32(len(s))), s...) } |
| 47 | + |
| 48 | +// FuzzRequestServer sends what a hostile client could, after INIT: any |
| 49 | +// bytes, framed or not. The server, as the hostagent runs it (sshocker's |
| 50 | +// options), never panics and never hangs: in the hostagent, either takes the |
| 51 | +// VM down. |
| 52 | +func FuzzRequestServer(f *testing.F) { |
| 53 | + open := fuzzFrame(sshFxpOpen, fuzzU32(1), fuzzStr("/f"), fuzzU32(sshFxfWrite|sshFxfCreat|sshFxfTrunc), fuzzU32(0)) |
| 54 | + write := fuzzFrame(sshFxpWrite, fuzzU32(2), fuzzStr("1"), make([]byte, 8), fuzzStr("data")) |
| 55 | + read := fuzzFrame(sshFxpRead, fuzzU32(3), fuzzStr("1"), make([]byte, 8), fuzzU32(4)) |
| 56 | + closeH := fuzzFrame(sshFxpClose, fuzzU32(4), fuzzStr("1")) |
| 57 | + mkdir := fuzzFrame(sshFxpMkdir, fuzzU32(5), fuzzStr("/d"), fuzzU32(0)) |
| 58 | + f.Add(bytes.Join([][]byte{open, write, read, closeH}, nil)) |
| 59 | + f.Add(bytes.Join([][]byte{mkdir, fuzzFrame(sshFxpOpendir, fuzzU32(6), fuzzStr("/d")), fuzzFrame(sshFxpReaddir, fuzzU32(7), fuzzStr("1"))}, nil)) |
| 60 | + f.Add(fuzzFrame(sshFxpSetstat, fuzzU32(8), fuzzStr("/f"), fuzzU32(0xffffffff))) |
| 61 | + f.Add(fuzzFrame(sshFxpExtended, fuzzU32(9), fuzzStr("[email protected]"), fuzzStr("/a"), fuzzStr("/b"))) |
| 62 | + f.Add([]byte{0xff, 0xff, 0xff, 0xff, 1}) |
| 63 | + init := fuzzFrame(sshFxpInit, fuzzU32(3)) |
| 64 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 65 | + conn := fuzzConn{bytes.NewReader(append(append([]byte(nil), init...), data...))} |
| 66 | + h := fuzzHandler{} |
| 67 | + srv := NewRequestServer(conn, Handlers{FileGet: h, FilePut: h, FileCmd: h, FileList: h}, |
| 68 | + WithRSSequential(), WithRSMaxHandles(16), WithRSMaxTxPacket(64*1024)) |
| 69 | + done := make(chan struct{}) |
| 70 | + go func() { |
| 71 | + defer close(done) |
| 72 | + _ = srv.Serve() |
| 73 | + }() |
| 74 | + select { |
| 75 | + case <-done: |
| 76 | + case <-time.After(10 * time.Second): |
| 77 | + t.Fatalf("the server hangs on %q", data) |
| 78 | + } |
| 79 | + srv.Close() |
| 80 | + }) |
| 81 | +} |
| 82 | diff -ruN a/testdata/fuzz/FuzzRequestServer/f7a4063272af0e29 b/testdata/fuzz/FuzzRequestServer/f7a4063272af0e29 |
| 83 | |
| 84 | |
| 85 | @@ -0,0 +1,2 @@ |
| 86 | +go test fuzz v1 |
| 87 | +[]byte("\x00\x00\x00\x13\x03\x00\x00\x00\x01\x00\x00\x00\x02/f\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x1a\x06\x00\x00\x00\x02\x00\x00\x00\x011\x00\x00\x04\x00\x00\x00\n\x04\x00\x00\x00\x04\x00\x00\x00\x01\v") |