diff --git a/src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs b/src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs index 3948b4e4c..ffac498c5 100644 --- a/src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs +++ b/src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs @@ -118,7 +118,10 @@ public BZip2InputStream(Stream stream) bsBuff = 0; Initialize(); InitBlock(); - SetupBlock(); + if (!streamEnd) + { + SetupBlock(); + } } /// @@ -351,7 +354,7 @@ private void Initialize() if (magic1 != 'B' || magic2 != 'Z' || magic3 != 'h' || magic4 < '1' || magic4 > '9') { streamEnd = true; - return; + throw new BZip2Exception("BZip2 input stream bad header"); } SetDecompressStructureSizes(magic4 - '0'); @@ -469,7 +472,7 @@ private int BsGetInt32() return result; } - private void RecvDecodingTables() + private int RecvDecodingTables() { char[][] len = new char[BZip2Constants.GroupCount][]; for (int i = 0; i < BZip2Constants.GroupCount; ++i) @@ -504,11 +507,24 @@ private void RecvDecodingTables() } MakeMaps(); + if (nInUse == 0) + { + throw new BZip2Exception("Bzip data error"); + } int alphaSize = nInUse + 2; //--- Now the selectors --- int nGroups = BsR(3); + if (nGroups < 2 || nGroups > BZip2Constants.GroupCount) + { + throw new BZip2Exception("Bzip data error"); + } + int nSelectors = BsR(15); + if (nSelectors < 1 || nSelectors > BZip2Constants.MaximumSelectors) + { + throw new BZip2Exception("Bzip data error"); + } for (int i = 0; i < nSelectors; i++) { @@ -516,6 +532,10 @@ private void RecvDecodingTables() while (BsR(1) == 1) { j++; + if (j >= nGroups) + { + throw new BZip2Exception("Bzip data error"); + } } selectorMtf[i] = (byte)j; } @@ -546,8 +566,16 @@ private void RecvDecodingTables() int curr = BsR(5); for (int i = 0; i < alphaSize; i++) { - while (BsR(1) == 1) + while (true) { + if (curr < 1 || curr > 20) + { + throw new BZip2Exception("Bzip data error"); + } + if (BsR(1) == 0) + { + break; + } if (BsR(1) == 0) { curr++; @@ -574,6 +602,8 @@ private void RecvDecodingTables() HbCreateDecodeTables(limit[t], baseArray[t], perm[t], len[t], minLen, maxLen, alphaSize); minLens[t] = minLen; } + + return nSelectors; } private void GetAndMoveToFrontDecode() @@ -584,11 +614,52 @@ private void GetAndMoveToFrontDecode() int limitLast = BZip2Constants.BaseBlockSize * blockSize100k; origPtr = BsGetIntVS(24); - RecvDecodingTables(); + int nSelectors = RecvDecodingTables(); + int alphaSize = nInUse + 2; int EOB = nInUse + 1; int groupNo = -1; int groupPos = 0; + int DecodeNextSym() + { + if (groupPos == 0) + { + groupNo++; + if (groupNo >= nSelectors) + { + throw new BZip2Exception("Bzip data error"); + } + groupPos = BZip2Constants.GroupSize; + } + + groupPos--; + int zt = selector[groupNo]; + int zn = minLens[zt]; + int zvec = BsR(zn); + + while (zvec > limit[zt][zn]) + { + if (zn >= 20) + { // the longest code + throw new BZip2Exception("Bzip data error"); + } + zn++; + while (bsLive < 1) + { + FillBuffer(); + } + int zj = (bsBuff >> (bsLive - 1)) & 1; + bsLive--; + zvec = (zvec << 1) | zj; + } + int idx = zvec - baseArray[zt][zn]; + if (idx < 0 || idx >= alphaSize) + { + throw new BZip2Exception("Bzip data error"); + } + return perm[zt][idx]; + } + /*-- Setting up the unzftab entries here is not strictly necessary, but it does save having to do it later @@ -607,38 +678,7 @@ cache misses. last = -1; - if (groupPos == 0) - { - groupNo++; - groupPos = BZip2Constants.GroupSize; - } - - groupPos--; - int zt = selector[groupNo]; - int zn = minLens[zt]; - int zvec = BsR(zn); - int zj; - - while (zvec > limit[zt][zn]) - { - if (zn > 20) - { // the longest code - throw new BZip2Exception("Bzip data error"); - } - zn++; - while (bsLive < 1) - { - FillBuffer(); - } - zj = (bsBuff >> (bsLive - 1)) & 1; - bsLive--; - zvec = (zvec << 1) | zj; - } - if (zvec - baseArray[zt][zn] < 0 || zvec - baseArray[zt][zn] >= BZip2Constants.MaximumAlphaSize) - { - throw new BZip2Exception("Bzip data error"); - } - nextSym = perm[zt][zvec - baseArray[zt][zn]]; + nextSym = DecodeNextSym(); while (true) { @@ -662,35 +702,21 @@ cache misses. s += (1 + 1) * n; } - n <<= 1; - - if (groupPos == 0) + if (s >= limitLast) { - groupNo++; - groupPos = BZip2Constants.GroupSize; + BlockOverrun(); } - groupPos--; - - zt = selector[groupNo]; - zn = minLens[zt]; - zvec = BsR(zn); - - while (zvec > limit[zt][zn]) - { - zn++; - while (bsLive < 1) - { - FillBuffer(); - } - zj = (bsBuff >> (bsLive - 1)) & 1; - bsLive--; - zvec = (zvec << 1) | zj; - } - nextSym = perm[zt][zvec - baseArray[zt][zn]]; + n <<= 1; + nextSym = DecodeNextSym(); } while (nextSym == BZip2Constants.RunA || nextSym == BZip2Constants.RunB); s++; + if (last + s >= limitLast) + { + BlockOverrun(); + } + byte ch = seqToUnseq[yy[0]]; unzftab[ch] += s; @@ -701,10 +727,6 @@ cache misses. s--; } - if (last >= limitLast) - { - BlockOverrun(); - } continue; } else @@ -742,28 +764,7 @@ cache misses. yy[0] = tmp; - if (groupPos == 0) - { - groupNo++; - groupPos = BZip2Constants.GroupSize; - } - - groupPos--; - zt = selector[groupNo]; - zn = minLens[zt]; - zvec = BsR(zn); - while (zvec > limit[zt][zn]) - { - zn++; - while (bsLive < 1) - { - FillBuffer(); - } - zj = (bsBuff >> (bsLive - 1)) & 1; - bsLive--; - zvec = (zvec << 1) | zj; - } - nextSym = perm[zt][zvec - baseArray[zt][zn]]; + nextSym = DecodeNextSym(); continue; } } @@ -771,6 +772,16 @@ cache misses. private void SetupBlock() { + if (streamEnd || tt == null || ll8 == null) + { + return; + } + + if (origPtr < 0 || origPtr > last || origPtr >= tt.Length) + { + throw new BZip2Exception("Bzip data error"); + } + int[] cftab = new int[257]; cftab[0] = 0; @@ -1001,6 +1012,11 @@ private static void CrcError() private static void HbCreateDecodeTables(int[] limit, int[] baseArray, int[] perm, char[] length, int minLen, int maxLen, int alphaSize) { + if (minLen < 1 || maxLen >= BZip2Constants.MaximumCodeLength || minLen > maxLen || alphaSize > BZip2Constants.MaximumAlphaSize) + { + throw new BZip2Exception("Bzip data error"); + } + int pp = 0; for (int i = minLen; i <= maxLen; ++i) diff --git a/test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs b/test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs index 62d5a7874..32bd602e9 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs @@ -82,6 +82,30 @@ public void CreateEmptyArchive() } } + [Test] + [Category("BZip2")] + [TestCase("425a6839314159265359c1c08044a00030cd00c346299717724538e2000001410000100244a00030cd00c3462997177245385090c1c086e2", TestName = "MalformedInput_GetAndMoveToFrontDecode")] + [TestCase("425a6839314159265359c1c080e20003e8410000100244a000303200c3462997177245385090c1c080e2", TestName = "MalformedInput_HbCreateDecodeTables")] + [TestCase("425a6839314159265359c1c080e2a90001410000100244a00030cd00c3462997177245384290c1c080e2", TestName = "MalformedInput_SetupBlock_OrigPtr")] + [TestCase("425a6839314159265359c1c080e2000001410000100244a000bfcd00c346299717724538500010c080e2", TestName = "MalformedInput_RecvDecodingTables")] + [TestCase("425a68c6314159265359c1c080e2000001410000100244a00030cd00c3672997177245385090c1c080e2", TestName = "MalformedInput_InvalidHeaderBlockSize")] + public void MalformedInputThrowsBZip2Exception(string hexData) + { + var data = new byte[hexData.Length / 2]; + for (int i = 0; i < data.Length; i++) + { + data[i] = System.Convert.ToByte(hexData.Substring(i * 2, 2), 16); + } + + Assert.Throws(() => + { + using var stream = new MemoryStream(data); + using var bz2 = new BZip2InputStream(stream); + var buffer = new byte[4096]; + while (bz2.Read(buffer, 0, buffer.Length) > 0) { } + }); + } + [Test] [Category("BZip2")] [Category("Performance")]