Skip to content

Commit

Permalink
internal/zstd: avoid panic when windowSize is negative
Browse files Browse the repository at this point in the history
Consistency of Window_Size and Frame_Content_Size value ranges as
per RFC 8878 3.1.1.1.2 to resolve panic issues.

Fixes golang#63979
  • Loading branch information
aimuz committed Nov 18, 2023
1 parent 631a6c2 commit ab0be65
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/internal/zstd/fuzz_test.go
Expand Up @@ -25,6 +25,7 @@ var badStrings = []string{
"(\xb5/\xfd00\xec\x00\x00&@\x05\x05A7002\x02\x00\x02\x00\x02\x0000000000000000",
"(\xb5/\xfd00\xec\x00\x00V@\x05\x0517002\x02\x00\x02\x00\x02\x0000000000000000",
"\x50\x2a\x4d\x18\x02\x00\x00\x00",
"(\xb5/\xfd\xe40000000\xfa20\x000",
}

// This is a simple fuzzer to see if the decompressor panics.
Expand Down
13 changes: 7 additions & 6 deletions src/internal/zstd/zstd.go
Expand Up @@ -237,7 +237,7 @@ retry:

// Figure out the maximum amount of data we need to retain
// for backreferences.
var windowSize int
var windowSize uint64
if !singleSegment {
// Window descriptor. RFC 3.1.1.1.2.
windowDescriptor := r.scratch[0]
Expand All @@ -246,7 +246,7 @@ retry:
windowLog := exponent + 10
windowBase := uint64(1) << windowLog
windowAdd := (windowBase / 8) * mantissa
windowSize = int(windowBase + windowAdd)
windowSize = windowBase + windowAdd

// Default zstd sets limits on the window size.
if fuzzing && (windowLog > 31 || windowSize > 1<<27) {
Expand Down Expand Up @@ -288,12 +288,13 @@ retry:
// When Single_Segment_Flag is set, Window_Descriptor is not present.
// In this case, Window_Size is Frame_Content_Size.
if singleSegment {
windowSize = int(r.remainingFrameSize)
windowSize = r.remainingFrameSize
}

// RFC 8878 3.1.1.1.1.2. permits us to set an 8M max on window size.
if windowSize > 8<<20 {
windowSize = 8 << 20
const maxWindowSize = 8 << 20
if windowSize > maxWindowSize {
windowSize = maxWindowSize
}

relativeOffset += headerSize
Expand All @@ -307,7 +308,7 @@ retry:
r.repeatedOffset2 = 4
r.repeatedOffset3 = 8
r.huffmanTableBits = 0
r.window.reset(windowSize)
r.window.reset(int(windowSize))
r.seqTables[0] = nil
r.seqTables[1] = nil
r.seqTables[2] = nil
Expand Down

0 comments on commit ab0be65

Please sign in to comment.