Skip to content

Fix crashes in ParseHeaders #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ gor
*.pcap

.DS_Store

corpus
crashers
suppressions
12 changes: 12 additions & 0 deletions proto/fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build gofuzz

package proto

func Fuzz(data []byte) int {

ParseHeaders([][]byte{data}, func(header []byte, value []byte) bool {
return true
})

return 1
}
11 changes: 6 additions & 5 deletions proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,10 @@ func HeadersEqual(h1 []byte, h2 []byte) bool {

// Parsing headers from multiple payloads
func ParseHeaders(payloads [][]byte, cb func(header []byte, value []byte) bool) {

hS := [2]int{0, 0}
hE := [2]int{-1, -1}
vS := [2]int{-1, -1}
vE := [2]int{-1, -1}
hS := [2]int{0, 0} // header start
hE := [2]int{-1, -1} // header end
vS := [2]int{-1, -1} // value start
vE := [2]int{-1, -1} // value end

i := 0
pIdx := 0
Expand Down Expand Up @@ -260,11 +259,13 @@ func ParseHeaders(payloads [][]byte, cb func(header []byte, value []byte) bool)
hE = [2]int{pIdx, i}
newLineBreak = false
}
lineBreaks = 0
default:
lineBreaks = 0

if hS[1] == -1 {
hS = [2]int{pIdx, i}
hE = [2]int{-1, -1}
} else {
if hE[1] == -1 {
break
Expand Down
13 changes: 13 additions & 0 deletions proto/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ func TestParseHeaders(t *testing.T) {
}
}

// See https://github.com/dvyukov/go-fuzz and fuzz.go
func TestFuzzCrashers(t *testing.T) {
var crashers = []string{
"\n:00\n",
}

for _, f := range crashers {
ParseHeaders([][]byte{[]byte(f)}, func(header []byte, value []byte) bool {
return true
})
}
}

func TestParseHeadersWithComplexUserAgent(t *testing.T) {
// User-Agent could contain inside ':'
// Parser should wait for \r\n
Expand Down