Fix denial-of-service crashes in 8 decompressors on malformed input#1260
Merged
adamhathcock merged 5 commits intoreleasefrom Mar 23, 2026
Merged
Fix denial-of-service crashes in 8 decompressors on malformed input#1260adamhathcock merged 5 commits intoreleasefrom
adamhathcock merged 5 commits intoreleasefrom
Conversation
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com> Agent-Logs-Url: https://github.com/adamhathcock/sharpcompress/sessions/3037a2f7-f243-4261-802f-e8c83b4d6722
…, NullRef) Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com> Agent-Logs-Url: https://github.com/adamhathcock/sharpcompress/sessions/3037a2f7-f243-4261-802f-e8c83b4d6722
… helper Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com> Agent-Logs-Url: https://github.com/adamhathcock/sharpcompress/sessions/3037a2f7-f243-4261-802f-e8c83b4d6722
Copilot
AI
changed the title
[WIP] Fix crashes in decompressors from malformed input
Fix denial-of-service crashes in 8 decompressors on malformed input
Mar 23, 2026
adamhathcock
approved these changes
Mar 23, 2026
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (17 files)
Review NotesThis is a well-implemented security fix that addresses denial-of-service vulnerabilities in 8 decompressors. The changes add proper input validation to convert raw system exceptions ( Key observations:
|
This was referenced Mar 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Coverage-guided fuzzing found 14 crash sites across 8 decompressors where stream-derived values (table sizes, code lengths, bit widths) were used as array indices or divisors without validation, causing unhandled
IndexOutOfRangeException,DivideByZeroException, andNullReferenceExceptionthat terminate the process on any malformed archive input.Fixes by decompressor
LzwStream: Validate
maxBits >= INIT_BITS(9) inParseHeader; without this, code tables are undersized for the minimum 9-bit codes causing IOOB. Seteof = truebefore re-throwingParseHeaderexceptions to preventDivideByZeroonSkipEntry's subsequent read whennBitswas never initialized.CBZip2InputStream:
Create()/CreateAsync(): CheckInitialize()return value — previously ignored, leavingbsStream = nulland causing NullRef inBsRRecvDecodingTables: ValidatenGroups∈ [2,6]; validate each selector MTF value is< nGroupsbefore using as array index intopos[]HbCreateDecodeTables: Validatelength[i] < MAX_CODE_LENbeforebasev[length[i]+1]++GetAndMoveToFrontDecode: Validatezn < MAX_CODE_LENin the Huffman bit-reading loop; validatepermIdxin bounds beforeperm[zt][permIdx]SetupBlock: Validatecftab[ch] < tt.LengthandorigPtr < tt.LengthSqueezeStream: Validate non-leaf node index
< numnodesinBuildDecodedStreamtraversal loop.ArcLzwStream: Guard empty compressed data before
input[0]access; validatecode < suffix.Lengthin the decompression loop.ExplodeStream:
Create()/CreateAsync()now check the return value ofexplode_SetTables()and throwInvalidFormatExceptionon failure rather than proceeding with uninitialized empty Huffman tables.Deflate64
HuffmanTree: Addindex < array.Lengthguard beforearray[index]inCreateTable's binary tree traversal.ReduceStream: Validate
nextByteIndex < nextByteTable[outByte].LengthinGetNextByte— the bit-count table can yield an index equal to the table length.PPMd
Model: Guard solid-mode suffix traversal inStartModelwhen_maximumContext == PpmContext.ZERO; add post-StartModelZERO check inDecodeStart/DecodeStartAsync.LZMA
OutWindow: ValidatewindowSize > 0inCreate()— a zero dictionary size skips buffer allocation leaving_buffer = null, then_buffer[windowSize - 1]faults.All fixes are applied to both sync and async code paths. A new
MalformedInputTestsclass covers all 10 reproduced crash inputs, asserting each throws aSharpCompressExceptionsubtype rather than a raw CLR exception.Original prompt
This section details on the original issue you should resolve
<issue_title>Multiple decompressors crash on malformed input (IOOB, DivideByZero, NullRef)</issue_title>
<issue_description>Tags: BZip2, ZIP
Description
Fuzzing SharpCompress 0.47.0 with AFL++ and SharpFuzz found 9 unique crash sites across 4 decompressors, all triggered by small malformed archive inputs (38–98 bytes). The decompressors do not validate stream-derived fields before using them as array indices or divisors, leading to
IndexOutOfRangeException,DivideByZeroException, andNullReferenceException.These are exploitable for denial of service - any application that decompresses user-supplied archives using SharpCompress will crash on these inputs.
LzwStream (2 crash sites)
IOOB in
LzwStream.Read:DivideByZero in
LzwStream.Read:CBZip2InputStream (5 crash sites)
Note: #917 previously fixed an IOOB in
RecvDecodingTablesvianSelectorssanitization, but additional crash sites remain.NullRef in
BsR:IOOB in
GetAndMoveToFrontDecode:IOOB in
SetupBlock:IOOB in
RecvDecodingTables:IOOB in
HbCreateDecodeTables:SqueezeStream (1 crash site)
IOOB in
BuildDecodedStream:ArcLzwStream (1 crash site)
IOOB in
Decompress:Reproduction Code