Skip to content
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

proto: fix uint64->int overflow in table unmarshal #461

Merged
merged 3 commits into from
Nov 29, 2017
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
10 changes: 9 additions & 1 deletion proto/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ func (*NNIMessage) Reset() {}
func (*NNIMessage) String() string { return "" }
func (*NNIMessage) ProtoMessage() {}

type NMMessage struct {}
type NMMessage struct{}

func (*NMMessage) Reset() {}
func (*NMMessage) String() string { return "" }
Expand Down Expand Up @@ -1505,6 +1505,14 @@ func TestVarintOverflow(t *testing.T) {
}
}

func TestBytesWithInvalidLengthInGroup(t *testing.T) {
// Overflowing a 64-bit length should not be allowed.
b := []byte{0xbb, 0x30, 0xb2, 0x30, 0xb0, 0xb2, 0x83, 0xf1, 0xb0, 0xb2, 0xef, 0xbf, 0xbd, 0x01}
if err := Unmarshal(b, new(MyMessage)); err == nil {
t.Fatalf("Overflowed uint64 length without error")
}
}

func TestUnmarshalFuzz(t *testing.T) {
const N = 1000
seed := time.Now().UnixNano()
Expand Down
8 changes: 4 additions & 4 deletions proto/table_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error {
b = b[8:]
case WireBytes:
m, k := decodeVarint(b)
if k == 0 || uint64(len(b)) < uint64(k)+m {
if k == 0 || uint64(len(b)-k) < m {
return io.ErrUnexpectedEOF
}
b = b[uint64(k)+m:]
Expand Down Expand Up @@ -1834,12 +1834,12 @@ func findEndGroup(b []byte) (int, int) {
}
i += k
case WireFixed32:
if i+4 > len(b) {
if len(b)-4 < i {
return -1, -1
}
i += 4
case WireFixed64:
if i+8 > len(b) {
if len(b)-8 < i {
return -1, -1
}
i += 8
Expand All @@ -1849,7 +1849,7 @@ func findEndGroup(b []byte) (int, int) {
return -1, -1
}
i += k
if i+int(m) > len(b) {
if uint64(len(b)-i) < m {
return -1, -1
}
i += int(m)
Expand Down