Skip to content

Commit 3588812

Browse files
authored
zstd: Fix ineffective block size check (#771)
When falling back to Go decoding block sizes were not checked correctly. Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=56755
1 parent 0f734cf commit 3588812

File tree

4 files changed

+10
-21
lines changed

4 files changed

+10
-21
lines changed

zstd/fuzz_test.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,6 @@ func FuzzDecAllNoBMI2(f *testing.F) {
6262
func FuzzDecoder(f *testing.F) {
6363
fuzz.AddFromZip(f, "testdata/fuzz/decode-corpus-raw.zip", true, testing.Short())
6464
fuzz.AddFromZip(f, "testdata/fuzz/decode-corpus-encoded.zip", false, testing.Short())
65-
decLow, err := NewReader(nil, WithDecoderLowmem(true), WithDecoderConcurrency(2), WithDecoderMaxMemory(20<<20), WithDecoderMaxWindow(1<<20), IgnoreChecksum(true), WithDecodeBuffersBelow(8<<10))
66-
if err != nil {
67-
f.Fatal(err)
68-
}
69-
defer decLow.Close()
70-
// Test with high memory, but sync decoding
71-
decHi, err := NewReader(nil, WithDecoderLowmem(false), WithDecoderConcurrency(1), WithDecoderMaxMemory(20<<20), WithDecoderMaxWindow(1<<20), IgnoreChecksum(true), WithDecodeBuffersBelow(8<<10))
72-
if err != nil {
73-
f.Fatal(err)
74-
}
75-
defer decHi.Close()
7665

7766
brLow := newBytesReader(nil)
7867
brHi := newBytesReader(nil)
@@ -86,14 +75,19 @@ func FuzzDecoder(f *testing.F) {
8675
}()
8776
brLow.Reset(b)
8877
brHi.Reset(b)
89-
err := decLow.Reset(brLow)
78+
decLow, err := NewReader(brLow, WithDecoderLowmem(true), WithDecoderConcurrency(2), WithDecoderMaxMemory(20<<20), WithDecoderMaxWindow(1<<20), IgnoreChecksum(true), WithDecodeBuffersBelow(8<<10))
9079
if err != nil {
91-
t.Fatal(err)
80+
f.Fatal(err)
9281
}
93-
err = decHi.Reset(brHi)
82+
defer decLow.Close()
83+
84+
// Test with high memory, but sync decoding
85+
decHi, err := NewReader(brHi, WithDecoderLowmem(false), WithDecoderConcurrency(1), WithDecoderMaxMemory(20<<20), WithDecoderMaxWindow(1<<20), IgnoreChecksum(true), WithDecodeBuffersBelow(8<<10))
9486
if err != nil {
95-
t.Fatal(err)
87+
f.Fatal(err)
9688
}
89+
defer decHi.Close()
90+
9791
b1, err1 := io.ReadAll(decLow)
9892
b2, err2 := io.ReadAll(decHi)
9993
if err1 != err2 {

zstd/seqdec.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,6 @@ func (s *sequenceDecs) decodeSync(hist []byte) error {
314314
}
315315
size := ll + ml + len(out)
316316
if size-startSize > maxBlockSize {
317-
if size-startSize == 424242 {
318-
panic("here")
319-
}
320317
return fmt.Errorf("output bigger than max block size (%d)", maxBlockSize)
321318
}
322319
if size > cap(out) {
@@ -427,8 +424,7 @@ func (s *sequenceDecs) decodeSync(hist []byte) error {
427424
}
428425
}
429426

430-
// Check if space for literals
431-
if size := len(s.literals) + len(s.out) - startSize; size > maxBlockSize {
427+
if size := len(s.literals) + len(out) - startSize; size > maxBlockSize {
432428
return fmt.Errorf("output bigger than max block size (%d)", maxBlockSize)
433429
}
434430

zstd/seqdec_amd64.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ func (s *sequenceDecs) decodeSyncSimple(hist []byte) (bool, error) {
148148
s.seqSize += ctx.litRemain
149149
if s.seqSize > maxBlockSize {
150150
return true, fmt.Errorf("output bigger than max block size (%d)", maxBlockSize)
151-
152151
}
153152
err := br.close()
154153
if err != nil {
197 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)