Hang while opening BZip2 #641

Closed
opened 2026-01-29 22:15:10 +00:00 by claunia · 4 comments
Owner

Originally created by @fuzzah on GitHub (May 15, 2024).

In continuation of #841.
The following code results in an infinite loop in SharpCompress:

using System.IO;
using SharpCompress.Readers;
Stream stream = new MemoryStream(new byte[] {0x42, 0x5a, 0x68, 0x34, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x35});
ReaderFactory.Open(stream);

The stack trace (created with the dotnet stack tool):

System.Private.CoreLib!System.IO.Stream.ReadByte()
SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream.BsR(int32)
SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream.RecvDecodingTables()
SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream.GetAndMoveToFrontDecode()
SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream.InitBlock()
SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream..ctor(class System.IO.Stream,bool)
SharpCompress!SharpCompress.Compressors.BZip2.BZip2Stream..ctor(class System.IO.Stream,value class SharpCompress.Compressors.CompressionMode,bool)
SharpCompress!SharpCompress.Factories.TarFactory.TryOpenReader(class SharpCompress.IO.RewindableStream,class SharpCompress.Readers.ReaderOptions,class SharpCompress.Readers.IReader&)
SharpCompress!SharpCompress.Readers.ReaderFactory.Open(class System.IO.Stream,class SharpCompress.Readers.ReaderOptions)
app!Program.<Main>$(class System.String[])

This byte sequence written to a file gets detected as "bzip2 compressed data" by the file tool, but bzip2 fails to read it with the error message "bzip2: Compressed file ends unexpectedly".

Found by Linux Verification Center (linuxtesting.org) with AFL++ and SharpFuzz.
Reporter: Valery Korolyov (v.korolyov@gardatech.ru)
Organization: Garda Technologies (info@gardatech.ru)

Originally created by @fuzzah on GitHub (May 15, 2024). In continuation of #841. The following code results in an infinite loop in SharpCompress: ```csharp using System.IO; using SharpCompress.Readers; Stream stream = new MemoryStream(new byte[] {0x42, 0x5a, 0x68, 0x34, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x35}); ReaderFactory.Open(stream); ``` The stack trace (created with the [`dotnet stack`](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-stack) tool): ``` System.Private.CoreLib!System.IO.Stream.ReadByte() SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream.BsR(int32) SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream.RecvDecodingTables() SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream.GetAndMoveToFrontDecode() SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream.InitBlock() SharpCompress!SharpCompress.Compressors.BZip2.CBZip2InputStream..ctor(class System.IO.Stream,bool) SharpCompress!SharpCompress.Compressors.BZip2.BZip2Stream..ctor(class System.IO.Stream,value class SharpCompress.Compressors.CompressionMode,bool) SharpCompress!SharpCompress.Factories.TarFactory.TryOpenReader(class SharpCompress.IO.RewindableStream,class SharpCompress.Readers.ReaderOptions,class SharpCompress.Readers.IReader&) SharpCompress!SharpCompress.Readers.ReaderFactory.Open(class System.IO.Stream,class SharpCompress.Readers.ReaderOptions) app!Program.<Main>$(class System.String[]) ``` This byte sequence written to a file gets detected as "bzip2 compressed data" by the `file` tool, but `bzip2` fails to read it with the error message "bzip2: Compressed file ends unexpectedly". _Found by Linux Verification Center (linuxtesting.org) with AFL++ and SharpFuzz. Reporter: Valery Korolyov ([v.korolyov@gardatech.ru](mailto:v.korolyov@gardatech.ru)) Organization: Garda Technologies ([info@gardatech.ru](mailto:info@gardatech.ru))_
claunia added the bugup for grabs labels 2026-01-29 22:15:10 +00:00
Author
Owner

@Morilli commented on GitHub (May 30, 2024):

Stepping through, the code correctly identifies the EOF here:
6fc4b045fd/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs (L394-L397)
and then proceeds to... do nothing?
6fc4b045fd/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs (L42-L52)

Perhaps the fix is just to make this function actually throw an exception instead of silently passing?

@Morilli commented on GitHub (May 30, 2024): Stepping through, the code correctly identifies the EOF here: https://github.com/adamhathcock/sharpcompress/blob/6fc4b045fde70c42df738d203c453082354e9558/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs#L394-L397 and then proceeds to... do nothing? https://github.com/adamhathcock/sharpcompress/blob/6fc4b045fde70c42df738d203c453082354e9558/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs#L42-L52 Perhaps the fix is just to make this function actually throw an exception instead of silently passing?
Author
Owner

@adamhathcock commented on GitHub (Jun 4, 2024):

Seems like I should have ported the exception.

@adamhathcock commented on GitHub (Jun 4, 2024): Seems like I should have ported the exception.
Author
Owner

@DineshSolanki commented on GitHub (Jul 30, 2024):

So as #850 is merged, this should be closed?

@DineshSolanki commented on GitHub (Jul 30, 2024): So as #850 is merged, this should be closed?
Author
Owner

@adamhathcock commented on GitHub (Aug 2, 2024):

Thanks!

@adamhathcock commented on GitHub (Aug 2, 2024): Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#641