From 75ac65364d7d23a4d93dd44566768351845f7465 Mon Sep 17 00:00:00 2001 From: mahach666 <79989386+mahach666@users.noreply.github.com> Date: Sat, 14 Nov 2026 05:59:36 +0300 Subject: [PATCH 1/2] Use gzip trailer unpacked size as initial buffer capacity (fixes #7) --- src/TdbIO/Internal/Gzip.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/TdbIO/Internal/Gzip.cs b/src/TdbIO/Internal/Gzip.cs index 69537bd..eb9e64d 100644 --- a/src/TdbIO/Internal/Gzip.cs +++ b/src/TdbIO/Internal/Gzip.cs @@ -50,22 +50,17 @@ public static int Peek(ReadOnlySpan head, Span into) } } - // Reads into a buffer that grows up to the declared size, so a forged size cannot allocate memory the data does not fill. + // Reads into a buffer sized to the trailer's unpacked size (capped by MaxUnpackedLength). private static byte[] Inflate(byte[] file, int trailer, uint size) { var input = new LastByteAloneStream(file, HeaderLength, trailer); using var deflate = new DeflateStream(input, CompressionMode.Decompress); - var buffer = new byte[(int)Math.Min(size, Math.Max(65536L, file.Length * 4L))]; + var buffer = new byte[size]; int total = 0; try { - while (true) + while (total < buffer.Length) { - if (total == buffer.Length) - { - if (total == size) break; - Array.Resize(ref buffer, (int)Math.Min(size, buffer.Length * 2L)); - } int n = deflate.Read(buffer, total, buffer.Length - total); if (n == 0) break; total += n; From 3c0bdadc68b05b1a2a598a65c21d0bdb3469bd05 Mon Sep 17 00:00:00 2001 From: mahach666 <79989386+mahach666@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:12:36 +0300 Subject: [PATCH 2/2] Keep initial gzip buffer allocation bounded for untrusted ISIZE Bound initial buffer size and grow as bytes are inflated to prevent forged ISIZE from allocating large buffers on invalid data. Add regression test. --- src/TdbIO/Internal/Gzip.cs | 11 ++++++++--- tests/TdbIO.Tests/GzipTests.cs | 10 ++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/TdbIO/Internal/Gzip.cs b/src/TdbIO/Internal/Gzip.cs index eb9e64d..69537bd 100644 --- a/src/TdbIO/Internal/Gzip.cs +++ b/src/TdbIO/Internal/Gzip.cs @@ -50,17 +50,22 @@ public static int Peek(ReadOnlySpan head, Span into) } } - // Reads into a buffer sized to the trailer's unpacked size (capped by MaxUnpackedLength). + // Reads into a buffer that grows up to the declared size, so a forged size cannot allocate memory the data does not fill. private static byte[] Inflate(byte[] file, int trailer, uint size) { var input = new LastByteAloneStream(file, HeaderLength, trailer); using var deflate = new DeflateStream(input, CompressionMode.Decompress); - var buffer = new byte[size]; + var buffer = new byte[(int)Math.Min(size, Math.Max(65536L, file.Length * 4L))]; int total = 0; try { - while (total < buffer.Length) + while (true) { + if (total == buffer.Length) + { + if (total == size) break; + Array.Resize(ref buffer, (int)Math.Min(size, buffer.Length * 2L)); + } int n = deflate.Read(buffer, total, buffer.Length - total); if (n == 0) break; total += n; diff --git a/tests/TdbIO.Tests/GzipTests.cs b/tests/TdbIO.Tests/GzipTests.cs index 54d8437..a0b650a 100644 --- a/tests/TdbIO.Tests/GzipTests.cs +++ b/tests/TdbIO.Tests/GzipTests.cs @@ -148,6 +148,16 @@ public void CorruptDeflateDataIsRejected() Rejects(file); } + [Fact] + public void LargeForgedIsizeWithCorruptDataDoesNotAllocateLargeBuffer() + { + byte[] file = Synthetic.Concat([0x1F, 0x8B, 0x08, 0x00, 0, 0, 0, 0, 0, 0], [0xFF, 0xFF, 0xFF], Synthetic.Le32(0u), Synthetic.Le32(64u * 1024 * 1024)); + long before = GC.GetTotalAllocatedBytes(precise: true); + Rejects(file); + long allocated = GC.GetTotalAllocatedBytes(precise: true) - before; + Assert.True(allocated < 4 * 1024 * 1024, $"Allocated {allocated} bytes, expected < 4 MiB"); + } + [Fact] public void PeekInflatesTheHeadOfCutData() {