| 1 | package sftp |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "io" |
| 7 | "os" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | type fuzzConn struct { |
| 13 | io.Reader |
| 14 | } |
| 15 | |
| 16 | func (fuzzConn) Write(p []byte) (int, error) { return len(p), nil } |
| 17 | func (fuzzConn) Close() error { return nil } |
| 18 | |
| 19 | func fuzzFrame(typ byte, payload ...[]byte) []byte { |
| 20 | body := append([]byte{typ}, bytes.Join(payload, nil)...) |
| 21 | return append(binary.BigEndian.AppendUint32(nil, uint32(len(body))), body...) |
| 22 | } |
| 23 | |
| 24 | func fuzzU32(v uint32) []byte { return binary.BigEndian.AppendUint32(nil, v) } |
| 25 | |
| 26 | |
| 27 | |
| 28 | type fuzzHandler struct{} |
| 29 | |
| 30 | type discardAt struct{} |
| 31 | |
| 32 | func (discardAt) WriteAt(p []byte, _ int64) (int, error) { return len(p), nil } |
| 33 | |
| 34 | type fuzzLister struct{} |
| 35 | |
| 36 | func (fuzzLister) ListAt([]os.FileInfo, int64) (int, error) { return 0, io.EOF } |
| 37 | |
| 38 | func (fuzzHandler) Fileread(*Request) (io.ReaderAt, error) { return bytes.NewReader([]byte("content")), nil } |
| 39 | func (fuzzHandler) Filewrite(*Request) (io.WriterAt, error) { return discardAt{}, nil } |
| 40 | func (fuzzHandler) Filecmd(*Request) error { return nil } |
| 41 | func (fuzzHandler) Filelist(*Request) (ListerAt, error) { return fuzzLister{}, nil } |
| 42 | func fuzzStr(s string) []byte { return append(fuzzU32(uint32(len(s))), s...) } |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | func FuzzRequestServer(f *testing.F) { |
| 49 | open := fuzzFrame(sshFxpOpen, fuzzU32(1), fuzzStr("/f"), fuzzU32(sshFxfWrite|sshFxfCreat|sshFxfTrunc), fuzzU32(0)) |
| 50 | write := fuzzFrame(sshFxpWrite, fuzzU32(2), fuzzStr("1"), make([]byte, 8), fuzzStr("data")) |
| 51 | read := fuzzFrame(sshFxpRead, fuzzU32(3), fuzzStr("1"), make([]byte, 8), fuzzU32(4)) |
| 52 | closeH := fuzzFrame(sshFxpClose, fuzzU32(4), fuzzStr("1")) |
| 53 | mkdir := fuzzFrame(sshFxpMkdir, fuzzU32(5), fuzzStr("/d"), fuzzU32(0)) |
| 54 | f.Add(bytes.Join([][]byte{open, write, read, closeH}, nil)) |
| 55 | f.Add(bytes.Join([][]byte{mkdir, fuzzFrame(sshFxpOpendir, fuzzU32(6), fuzzStr("/d")), fuzzFrame(sshFxpReaddir, fuzzU32(7), fuzzStr("1"))}, nil)) |
| 56 | f.Add(fuzzFrame(sshFxpSetstat, fuzzU32(8), fuzzStr("/f"), fuzzU32(0xffffffff))) |
| 57 | f.Add(fuzzFrame(sshFxpExtended, fuzzU32(9), fuzzStr("[email protected]"), fuzzStr("/a"), fuzzStr("/b"))) |
| 58 | f.Add([]byte{0xff, 0xff, 0xff, 0xff, 1}) |
| 59 | init := fuzzFrame(sshFxpInit, fuzzU32(3)) |
| 60 | f.Fuzz(func(t *testing.T, data []byte) { |
| 61 | conn := fuzzConn{bytes.NewReader(append(append([]byte(nil), init...), data...))} |
| 62 | h := fuzzHandler{} |
| 63 | srv := NewRequestServer(conn, Handlers{FileGet: h, FilePut: h, FileCmd: h, FileList: h}, |
| 64 | WithRSSequential(), WithRSMaxHandles(16), WithRSMaxTxPacket(64*1024)) |
| 65 | done := make(chan struct{}) |
| 66 | go func() { |
| 67 | defer close(done) |
| 68 | _ = srv.Serve() |
| 69 | }() |
| 70 | select { |
| 71 | case <-done: |
| 72 | case <-time.After(10 * time.Second): |
| 73 | t.Fatalf("the server hangs on %q", data) |
| 74 | } |
| 75 | srv.Close() |
| 76 | }) |
| 77 | } |