Skip to content

Commit

Permalink
zstd: Detect extra block data and report as corrupted (#520)
Browse files Browse the repository at this point in the history
* zstd: Detect extra block data and report as corrupted
* Remove confirmed bad files from decoder corpus.

When no sequences, there should not be any more data on the block.
  • Loading branch information
klauspost authored Mar 9, 2022
1 parent 0ff8ec1 commit 531d692
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions zstd/blockdec.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ func (b *blockDec) prepareSequences(in []byte, hist *history) (err error) {
nSeqs = 0x7f00 + int(in[1]) + (int(in[2]) << 8)
in = in[3:]
}
if nSeqs == 0 && len(in) != 0 {
// When no sequences, there should not be any more data...
return ErrUnexpectedBlockSize
}

var seqs = &hist.decoders
seqs.nSeqs = nSeqs
Expand Down
1 change: 1 addition & 0 deletions zstd/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,7 @@ func testDecoderDecodeAllError(t *testing.T, fn string, dec *Decoder, errMap map
t.Error("Did not get expected error, got", len(got), "bytes")
return
}
t.Log(err)
if errMap[tt.Name] == "" {
t.Error("cannot check error")
} else {
Expand Down
Binary file modified zstd/testdata/bad.zip
Binary file not shown.
Binary file modified zstd/testdata/decoder.zip
Binary file not shown.
4 changes: 4 additions & 0 deletions zstd/zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ var (
// Typically returned on invalid input.
ErrBlockTooSmall = errors.New("block too small")

// ErrUnexpectedBlockSize is returned when a block has unexpected size.
// Typically returned on invalid input.
ErrUnexpectedBlockSize = errors.New("unexpected block size")

// ErrMagicMismatch is returned when a "magic" number isn't what is expected.
// Typically this indicates wrong or corrupted input.
ErrMagicMismatch = errors.New("invalid input: magic number mismatch")
Expand Down

0 comments on commit 531d692

Please sign in to comment.