Closed Bug 2029902 Opened 7 months ago Closed 7 months ago

Stack-buffer-overflow write in [@ MiniTransceiver::RecvFDs] due to wrong constant kMaxDataSize passed as aMaxFds

Categories

(Core :: IPC, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2029466

People

(Reporter: bugmon, Unassigned)

Details

(5 keywords)

Attachments

(7 files)

MiniTransceiver::Recv() in the Firefox parent process declares a fixed-size stack array int all_fds[kMaxNumFds] (16 ints, 64 bytes) to receive SCM_RIGHTS file descriptors from the fork-server process over a Unix socket. However, when calling RecvData() at line 226, it incorrectly passes kMaxDataSize (8192) as the aMaxFds argument instead of kMaxNumFds (16). This wrong value flows through to InitMsgHdr(), which sizes the cmsg control buffer for 8192 file descriptors (~32 KiB), allowing the kernel to deliver up to SCM_MAX_FD (≈253 on Linux) descriptors per recvmsg call. The MOZ_ASSERT at line 161 that should catch the overflow is debug-only AND compares against the wrong limit (8192). When a malicious or compromised fork-server attaches more than 16 file descriptors to its reply, the memcpy at line 163 writes past the 64-byte stack array, corrupting adjacent stack frames including local variables, saved RBP, and the return address.

This is a privilege escalation across an IPC trust boundary: the fork-server is a separate, less-privileged process that the parent spawns to fork content processes. A compromised fork-server can use this bug to corrupt the stack of the IPC launcher thread in the chrome (parent) process. The attacker controls the overflow length precisely (4 bytes per attached FD, up to ~1 KB on Linux). The overflowed values are kernel-assigned FD numbers in the receiving process — not fully arbitrary, but predictable and influenceable by manipulating the parent's open-FD count beforehand. Stack canaries are the only mitigation; ASLR does not help against same-frame variable corruption.

This vulnerability is distinct from a separately-reported heap-buffer-overflow at line 189 in the same file: that bug attacks the data path (recvmsg writing past the heap databuf due to unchecked peer-controlled msgsz at line 200), whereas this bug attacks the FD path (memcpy writing past the stack all_fds due to the wrong constant at line 226). The two have independent root causes, independent fixes, and independent ASan signatures (heap-buffer-overflow vs stack-buffer-overflow).

Build Info

Affected Code

File: ipc/glue/MiniTransceiver.cpp, line 21-23

static const size_t kMaxIOVecSize = 64;
static const size_t kMaxDataSize = 8 * 1024;
static const size_t kMaxNumFds = 16;

File: ipc/glue/MiniTransceiver.cpp, line 221-228

  UniquePtr<char[]> databuf = MakeUnique<char[]>(kMaxDataSize);
  uint32_t msgsz = 0;
  int all_fds[kMaxNumFds];           // 16 ints = 64 bytes on stack
  unsigned num_all_fds = 0;

  if (!RecvData(databuf.get(), kMaxDataSize, &msgsz, all_fds, kMaxDataSize,
  //                                                          ^^^^^^^^^^^^
  // BUG: 5th argument should be kMaxNumFds (16), not kMaxDataSize (8192)
                &num_all_fds)) {
    return false;
  }

File: ipc/glue/MiniTransceiver.cpp, line 169-173

bool MiniTransceiver::RecvData(char* aDataBuf, size_t aBufSize,
                               uint32_t* aMsgSize, int* aFdsBuf,
                               unsigned aMaxFds, unsigned* aNumFds) {
  msghdr hdr;
  InitMsgHdr(&hdr, 1, aMaxFds);  // sizes cmsg control buffer for 8192 fds (~32 KiB)

File: ipc/glue/MiniTransceiver.cpp, line 143-167

unsigned MiniTransceiver::RecvFDs(msghdr* aHdr, int* aAllFds,
                                  unsigned aMaxFds) {
  if (aHdr->msg_controllen == 0) {
    return 0;
  }

  unsigned num_all_fds = 0;
  for (cmsghdr* cmsg = CMSG_FIRSTHDR(aHdr); cmsg;
       cmsg = CMSG_NXTHDR(aHdr, cmsg)) {
    MOZ_ASSERT(cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS,
               "Accept only SCM_RIGHTS to receive file descriptors");

    unsigned payload_sz = cmsg->cmsg_len - CMSG_LEN(0);
    MOZ_ASSERT(payload_sz % sizeof(int) == 0);

    // Add fds to |aAllFds|
    unsigned num_part_fds = payload_sz / sizeof(int);
    int* part_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
    MOZ_ASSERT(num_all_fds + num_part_fds <= aMaxFds);
    // ^^^ Debug-only AND checks against wrong limit (8192). No release-mode check.

    memcpy(aAllFds + num_all_fds, part_fds, num_part_fds * sizeof(int));
    // ^^^ STACK-BUFFER-OVERFLOW: writes past 64-byte all_fds array
    num_all_fds += num_part_fds;
  }
  return num_all_fds;
}

File: ipc/glue/ForkServiceChild.cpp, line 174-180

  UniquePtr<IPC::Message> reply;
  if (!mTcver->Recv(reply)) {  // <-- Parent process receives from fork-server here
    MOZ_LOG(gForkServiceLog, LogLevel::Verbose,
            ("the pipe to the fork server is closed or having errors"));
    OnError();
    return Err(LaunchError("FSC::SFNS::Recv"));
  }

The root cause is a single-token copy-paste typo at line 226: the 5th argument to RecvData() is kMaxDataSize (8192) but should be kMaxNumFds (16). This wrong value propagates through aMaxFds → InitMsgHdr (sizes the control buffer to accept up to 8192 FDs) → RecvFDs (where the only bounds check is a debug-only MOZ_ASSERT comparing against the wrong value). When the kernel delivers more than 16 FDs in a single recvmsg, the memcpy at line 163 overruns the 64-byte stack array. The fix is a one-token change: kMaxDataSize → kMaxNumFds at line 226 (and ideally promote the MOZ_ASSERT at line 161 to a release-mode check that closes excess FDs and returns failure).

Exploit Chain

  1. Attacker compromises the fork-server process (separate, less-privileged process spawned by the parent to fork content processes).
  2. Compromised fork-server waits for the parent to send a Msg_ForkNewSubprocess request over the MiniTransceiver Unix socket.
  3. Fork-server crafts a Reply_ForkNewSubprocess message and attaches >16 SCM_RIGHTS file descriptors via SetAttachedFileHandles() (e.g. 64 dups of stdin). Linux permits up to SCM_MAX_FD ≈ 253 per sendmsg.
  4. Parent process IPC launcher thread calls ForkServiceChild::SendForkNewSubprocess → MiniTransceiver::Recv → RecvData → recvmsg. Because InitMsgHdr was called with aMaxFds=8192, the cmsg control buffer is ~32 KiB and the kernel delivers all attached FDs.
  5. RecvFDs computes num_part_fds from cmsg_len (e.g. 64) and memcpys 64×4=256 bytes into the 64-byte stack array all_fds[16], overwriting 192 bytes of adjacent stack: local 'handles' nsTArray, temporaries, saved RBP, and the return address of MiniTransceiver::Recv.
  6. Attacker controls the overflow length precisely (4 bytes per FD, up to ~1 KB). The overflowed values are kernel-assigned FD numbers in the parent process — predictable small integers, influenceable by first causing the parent to open/close many FDs to push the numbering into a chosen range.
  7. On return from MiniTransceiver::Recv (or via corrupted nsTArray vtable/pointer), control flow is hijacked in the chrome process. Stack canaries are the primary mitigation; if bypassed, this yields code execution in the fully-privileged parent process.

IPC Path

  • MiniTransceiver fork-server channel
    • Protocol: MiniTransceiver (raw Unix socket, not IPDL)
    • Parent process: Parent (chrome)
    • Child process: ForkServer
    • Message flow: ForkServer -> Parent: Reply_ForkNewSubprocess with >16 SCM_RIGHTS file descriptors

Steps to Reproduce

  1. Apply source_patch.diff to ipc/glue/ForkServer.cpp to simulate a compromised fork-server attaching 64 extra FDs after the 8th fork reply (counter-gated so the harness can attach to the parent before the crash). The patch only modifies fork-server-process code (entire file gated by XRE_IsForkServerProcess() at line 334).
  2. Rebuild Firefox (./mach build).
  3. Set prefs: dom.ipc.forkserver.enable=true, dom.ipc.processPrelaunch.enabled=true, dom.ipc.processPrelaunch.fission.number=12
  4. Load test.html which creates 24 cross-origin iframes (16 immediately + 8 after 2 s) to drive >8 fork requests through the fork-server.
  5. Timeout: 90 seconds.
  6. Observe ASan stack-buffer-overflow WRITE of size 256 in the parent process at MiniTransceiver.cpp:163, victim object 'all_fds' (line 223) at frame offset [32,96).

Security Impact

  • Severity: High
  • Attacker capability: Stack-buffer-overflow WRITE in the chrome (parent) process IPC launcher thread. Attacker controls overflow length precisely (4 bytes × number of attached FDs, up to ~1 KB on Linux). Overflowed values are kernel-assigned FD numbers in the parent — predictable small integers, influenceable by manipulating the parent's open-FD table. Overflow corrupts adjacent locals (nsTArray header, temporaries) followed by saved RBP and return address. With stack-canary bypass or via the corrupted nsTArray, this yields control-flow hijack and code execution in the fully-privileged parent process — a sandbox escape from fork-server to chrome.
  • Preconditions: Attacker must first compromise the fork-server process. The fork-server is a separate process spawned by the parent on Linux when dom.ipc.forkserver.enable is true; it forks content processes on demand. It runs with fewer privileges than the parent and is treated as a trust boundary by the MiniTransceiver protocol. Compromise could be achieved via a separate vulnerability in fork-server message parsing, or via a content process exploiting the fork-server's pre-fork shared address space.

ASAN Report

=================================================================
==4374==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7843d0ab9ba0 at pc 0x5bb1f29547ee bp 0x7843d0ab99a0 sp 0x7843d0ab9160
WRITE of size 256 at 0x7843d0ab9ba0 thread T32
    #0 0x5bb1f29547ed in __asan_memcpy _asan_rtl_:3
    #1 0x784410f48073 in mozilla::ipc::MiniTransceiver::RecvFDs(msghdr*, int*, unsigned int) /firefox/ipc/glue/MiniTransceiver.cpp:163:5
    #2 0x784410f48648 in mozilla::ipc::MiniTransceiver::RecvData(char*, unsigned long, unsigned int*, int*, unsigned int, unsigned int*) /firefox/ipc/glue/MiniTransceiver.cpp:203:20
    #3 0x784410f48966 in mozilla::ipc::MiniTransceiver::Recv(std::unique_ptr<IPC::Message, std::default_delete<IPC::Message>>&) /firefox/ipc/glue/MiniTransceiver.cpp:226:8
    #4 0x784410edf790 in mozilla::ipc::ForkServiceChild::SendForkNewSubprocess(mozilla::geckoargs::ChildProcessArgs&&, base::LaunchOptions&&, int*) /firefox/ipc/glue/ForkServiceChild.cpp:175:16
    #5 0x784410eeafbc in mozilla::ipc::PosixProcessLauncher::DoLaunch() /firefox/ipc/glue/GeckoChildProcessHost.cpp:1351:39
    #6 0x784410ee77bc in mozilla::ipc::BaseProcessLauncher::PerformAsyncLaunch() /firefox/ipc/glue/GeckoChildProcessHost.cpp:1085:10
    #7 0x784410f18c25 in mozilla::detail::ProxyRunnable<...>::Run() /firefox/obj-firefox-asan/dist/include/mozilla/MozPromise.h:1729:42
    #8 0x78440f4b5fd7 in mozilla::TaskQueue::Runner::Run() /firefox/xpcom/threads/TaskQueue.cpp:309:20
    #9 0x78440f4d0a9a in nsThread::ProcessNextEvent(bool, bool*) /firefox/xpcom/threads/nsThread.cpp:1173:16
    #10 0x78440f4d9469 in NS_ProcessNextEvent(nsIThread*, bool) /firefox/xpcom/threads/nsThreadUtils.cpp:465:10
    #11 0x784410f46b2a in mozilla::ipc::MessagePumpForNonMainThreads::Run(base::MessagePump::Delegate*) /firefox/ipc/glue/MessagePump.cpp:297:20
    #12 0x784410d87de4 in MessageLoop::Run() /firefox/ipc/chromium/src/base/message_loop.cc:346:3
    #13 0x78440f4c8f60 in nsThread::ThreadFunc(void*) /firefox/xpcom/threads/nsThread.cpp:374:10
    #14 0x7c443b2eb88f in _pt_root /firefox/nsprpub/pr/src/pthreads/ptthread.c:191:3
    #15 0x5bb1f2952bf6 in asan_thread_start(void*) _asan_rtl_:28
    #16 0x7c443bad2aa3 in pthread_condattr_setpshared
    #17 0x7c443bb5fa63 in clone

Address 0x7843d0ab9ba0 is located in stack of thread T32 at offset 96 in frame
    #0 0x784410f4886f in mozilla::ipc::MiniTransceiver::Recv(std::unique_ptr<IPC::Message, std::default_delete<IPC::Message>>&) /firefox/ipc/glue/MiniTransceiver.cpp:211:0

  This frame has 4 object(s):
    [32, 96) 'all_fds' (line 223)
    [128, 136) 'handles' (line 233) <== Memory access at offset 96 partially underflows this variable
    [160, 164) 'ref.tmp18' (line 235) <== Memory access at offset 96 partially underflows this variable
    [176, 184) 'agg.tmp36' <== Memory access at offset 96 partially underflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)

SUMMARY: AddressSanitizer: stack-buffer-overflow (/firefox/obj-firefox-asan/dist/bin/firefox+0x1757ed) (BuildId: 7a20d764ce230858726c646dc3259cdf)
Shadow bytes around the buggy address:
  0x7843d0ab9b00: 00 00 00 00 00 00 00 00 f1 f1 f1 f1 00 00 00 00
=>0x7843d0ab9b80: 00 00 00 00[f2]f2 f2 f2 f8 f2 f2 f2 f8 f2 00 f3
  0x7843d0ab9c00: f3 f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00
==4374==ABORTING
Attached file test.html —
Attached file prefs.json —
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Attached file README.txt —
Attached file prefs.js —
Group: core-security → dom-core-security
Status: UNCONFIRMED → RESOLVED
Closed: 7 months ago
Duplicate of bug: 2029466
Resolution: --- → DUPLICATE
Group: dom-core-security → general-core-security
Group: general-core-security
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: