Skip to content

Commit d082b9e

Browse files
authored
fix(dot/network): Check for size when decoding leb128. (#1634)
1 parent 957302f commit d082b9e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

dot/network/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ func readLEB128ToUint64(r io.Reader, buf []byte) (uint64, error) {
157157

158158
var out uint64
159159
var shift uint
160+
161+
maxSize := 10 // Max bytes in LEB128 encoding of uint64 is 10.
160162
for {
161163
_, err := r.Read(buf)
162164
if err != nil {
@@ -168,6 +170,12 @@ func readLEB128ToUint64(r io.Reader, buf []byte) (uint64, error) {
168170
if b&0x80 == 0 {
169171
break
170172
}
173+
174+
maxSize--
175+
if maxSize == 0 {
176+
return 0, fmt.Errorf("invalid LEB128 encoded data")
177+
}
178+
171179
shift += 7
172180
}
173181
return out, nil

dot/network/utils_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ func TestReadLEB128ToUint64(t *testing.T) {
102102
input: []byte("\xB9\x64"),
103103
output: 12857,
104104
},
105+
{
106+
input: []byte{'\xFF', '\xFF', '\xFF', '\xFF', '\xFF',
107+
'\xFF', '\xFF', '\xFF', '\xFF', '\x01'},
108+
output: 18446744073709551615,
109+
},
105110
}
106111

107112
for _, tc := range tests {
@@ -115,3 +120,15 @@ func TestReadLEB128ToUint64(t *testing.T) {
115120
require.Equal(t, tc.output, ret)
116121
}
117122
}
123+
124+
func TestInvalidLeb128(t *testing.T) {
125+
input := []byte{'\xFF', '\xFF', '\xFF', '\xFF', '\xFF',
126+
'\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\x01'}
127+
b := make([]byte, 2)
128+
buf := new(bytes.Buffer)
129+
_, err := buf.Write(input)
130+
require.NoError(t, err)
131+
132+
_, err = readLEB128ToUint64(buf, b[:1])
133+
require.Error(t, err)
134+
}

0 commit comments

Comments
 (0)