Pkware encryption for Zip files didn't allow for multiple reads of an entry #147

Closed
opened 2026-01-29 22:07:23 +00:00 by claunia · 5 comments
Owner

Originally created by @alexmgerasimov on GitHub (Nov 18, 2016).

Hello!

I tried to use this library and faced some issues. Basically repeated reading of some zip archive entries leads to crashes. It seems that this is related to encrypted zips somehow. I'm using the following test code to reproduce the crashes:

	class Program
	{
		static void Main(string[] args)
		{
			try
			{
				using (var fileStream = File.Open(@"c:\Archive1.zip", FileMode.Open))
					UnpackMultipleTimes(fileStream, "12345678");
			}
			catch (Exception e)
			{				
			}
		}

		private static void UnpackMultipleTimes(Stream stream, string password)
		{
			using (var archive = ArchiveFactory.Open(stream, new ReaderOptions {Password = password}))
			{
				var entries = archive.Entries.Where(entry => !entry.IsDirectory);

				foreach (var entry in entries)
				{
					for (int i = 0; i < 100; i++)
					{
						using (var memoryStream = new MemoryStream())
						using (var entryStream = entry.OpenEntryStream())
						{
							entryStream.CopyTo(memoryStream);
						}
					}
				}
			}
		}
	}

Exception from Archive1.zip:

SharpCompress.Compressors.Deflate.ZlibException was unhandled
HResult=-2146233088
Message=Bad state (invalid block type)
Source=SharpCompress
StackTrace:
в SharpCompress.Compressors.Deflate.InflateManager.Inflate(FlushType flush)
в SharpCompress.Compressors.Deflate.ZlibBaseStream.Read(Byte[] buffer, Int32 offset, Int32 count)
в System.IO.Stream.InternalCopyTo(Stream destination, Int32 bufferSize)
в SevenZipTest.Program.UnpackMultipleTimes(Stream stream, String password)
в SevenZipTest.Program.Main(String[] args)
в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
в System.Threading.ThreadHelper.ThreadStart()

I have several other archives with confidential data and can't share them. They produce exceptions with another messages. For example:

SharpCompress.Compressors.Deflate.ZlibException was unhandled
HResult=-2146233088
Message=Bad state (invalid literal/length code)
Source=SharpCompress
StackTrace:
в SharpCompress.Compressors.Deflate.InflateManager.Inflate(FlushType flush)
в SharpCompress.Compressors.Deflate.ZlibBaseStream.Read(Byte[] buffer, Int32 offset, Int32 count)
в System.IO.Stream.InternalCopyTo(Stream destination, Int32 bufferSize)
в SevenZipTest.Program.UnpackMultipleTimes(Stream stream, String password)
в SevenZipTest.Program.Main(String[] args)
в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
в System.Threading.ThreadHelper.ThreadStart()

Can you fix this, please?

Originally created by @alexmgerasimov on GitHub (Nov 18, 2016). Hello! I tried to use this library and faced some issues. Basically repeated reading of some zip archive entries leads to crashes. It seems that this is related to encrypted zips somehow. I'm using the following test code to reproduce the crashes: ``` class Program { static void Main(string[] args) { try { using (var fileStream = File.Open(@"c:\Archive1.zip", FileMode.Open)) UnpackMultipleTimes(fileStream, "12345678"); } catch (Exception e) { } } private static void UnpackMultipleTimes(Stream stream, string password) { using (var archive = ArchiveFactory.Open(stream, new ReaderOptions {Password = password})) { var entries = archive.Entries.Where(entry => !entry.IsDirectory); foreach (var entry in entries) { for (int i = 0; i < 100; i++) { using (var memoryStream = new MemoryStream()) using (var entryStream = entry.OpenEntryStream()) { entryStream.CopyTo(memoryStream); } } } } } } ``` Exception from [Archive1.zip](https://github.com/adamhathcock/sharpcompress/files/600724/Archive1.zip): > SharpCompress.Compressors.Deflate.ZlibException was unhandled > HResult=-2146233088 > Message=Bad state (invalid block type) > Source=SharpCompress > StackTrace: > в SharpCompress.Compressors.Deflate.InflateManager.Inflate(FlushType flush) > в SharpCompress.Compressors.Deflate.ZlibBaseStream.Read(Byte[] buffer, Int32 offset, Int32 count) > в System.IO.Stream.InternalCopyTo(Stream destination, Int32 bufferSize) > в SevenZipTest.Program.UnpackMultipleTimes(Stream stream, String password) > в SevenZipTest.Program.Main(String[] args) > в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) > в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) > в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() > в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) > в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) > в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) > в System.Threading.ThreadHelper.ThreadStart() > I have several other archives with confidential data and can't share them. They produce exceptions with another messages. For example: > SharpCompress.Compressors.Deflate.ZlibException was unhandled > HResult=-2146233088 > Message=Bad state (invalid literal/length code) > Source=SharpCompress > StackTrace: > в SharpCompress.Compressors.Deflate.InflateManager.Inflate(FlushType flush) > в SharpCompress.Compressors.Deflate.ZlibBaseStream.Read(Byte[] buffer, Int32 offset, Int32 count) > в System.IO.Stream.InternalCopyTo(Stream destination, Int32 bufferSize) > в SevenZipTest.Program.UnpackMultipleTimes(Stream stream, String password) > в SevenZipTest.Program.Main(String[] args) > в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) > в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) > в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() > в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) > в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) > в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) > в System.Threading.ThreadHelper.ThreadStart() > Can you fix this, please?
claunia added the bug label 2026-01-29 22:07:23 +00:00
Author
Owner

@adamhathcock commented on GitHub (Nov 21, 2016):

I reproduced this.

This is specific to PKWare encryption as the encryptor is stateful and reused on multiple runs. I'll fix that.

I'm still getting an error. Probably something else is stateful and needs to be reset.

@adamhathcock commented on GitHub (Nov 21, 2016): I reproduced this. This is specific to PKWare encryption as the encryptor is stateful and reused on multiple runs. I'll fix that. I'm still getting an error. Probably something else is stateful and needs to be reset.
Author
Owner

@alexmgerasimov commented on GitHub (Nov 21, 2016):

Thank you for the quick response!
Is there any chance you can fix this issues in the near future?

@alexmgerasimov commented on GitHub (Nov 21, 2016): Thank you for the quick response! Is there any chance you can fix this issues in the near future?
Author
Owner

@adamhathcock commented on GitHub (Nov 21, 2016):

I'm doing this in my free time as I take breaks from work. Not sure when I'll fix it. This one I haven't figured out yet.

@adamhathcock commented on GitHub (Nov 21, 2016): I'm doing this in my free time as I take breaks from work. Not sure when I'll fix it. This one I haven't figured out yet.
Author
Owner

@alexmgerasimov commented on GitHub (Nov 25, 2016):

Thank you for merging our pull request to the master!
Can you please advise when the new version of the Nuget package will be released?

@alexmgerasimov commented on GitHub (Nov 25, 2016): Thank you for merging our pull request to the master! Can you please advise when the new version of the Nuget package will be released?
Author
Owner

@adamhathcock commented on GitHub (Nov 26, 2016):

I will try to do a release Monday.

@adamhathcock commented on GitHub (Nov 26, 2016): I will try to do a release Monday.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#147