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

zstd: Detect extra block data and report as corrupted #520

Merged
merged 2 commits into from
Mar 9, 2022
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 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