Open Bug 2029466 Opened 7 months ago Updated 15 days ago

Heap-buffer-overflow write in [@ mozilla::ipc::MiniTransceiver::RecvData] due to peer-controlled message size

Categories

(Core :: IPC, defect, P3)

defect

Tracking

()

ASSIGNED

People

(Reporter: bugmon, Assigned: jld)

References

Details

(Keywords: ai-involved, csectype-bounds, testcase)

Attachments

(7 files, 1 external file)

MiniTransceiver is the blocking request/response transport used between the Firefox parent process and the Linux fork-server process (and between the parent and a just-forked child via the exec pipe). MiniTransceiver::Recv() allocates a fixed 8 KiB heap buffer (kMaxDataSize = 8192) and calls RecvData() to fill it from the socket. After the first recvmsg(), RecvData() parses the total message length msgsz directly out of the peer-supplied IPC::Message header and then loops, setting iov_base = aDataBuf + total_readed and iov_len = msgsz - total_readed. Neither value is bounded against aBufSize; the only check is MOZ_ASSERT(total_readed <= aBufSize), which is compiled out of release builds. The corresponding MOZ_ASSERT(bytes_to_send <= kMaxDataSize) on the Send side is likewise debug-only, so a peer that emits a >8 KiB IPC::Message causes the kernel, via recvmsg(), to write peer-controlled bytes past the end of the 8 KiB heap allocation in the receiving process.

In this PoC the fork-server process (simulating a compromised peer) sends an oversized Reply_ForkNewSubprocess. The unmodified parent-process code in ForkServiceChild::SendForkNewSubprocess() -> MiniTransceiver::Recv() -> RecvData() then performs an out-of-bounds heap write of ~8 KiB of attacker-controlled data immediately after the 8192-byte buffer. The same file also contains a second, related defect: Recv() passes kMaxDataSize (8192) instead of kMaxNumFds (16) as aMaxFds, so a peer that ships >16 SCM_RIGHTS file descriptors can overflow the 16-int stack array all_fds (not exercised by this PoC).

The fork server is enabled by default in Linux Nightly and in Mozilla's official Linux .deb/.rpm packages (dom.ipc.forkserver.enable). Exploitation against the parent requires first controlling the bytes written to the fork-server socket — i.e. a compromised fork-server process — so this is primarily a defence-in-depth / process-boundary hardening bug rather than a directly web-reachable vulnerability, but it is an unbounded, attacker-controlled-length, attacker-controlled-content linear heap write in the parent process and should be fixed with a release-mode bounds check.

Build Info

Affected Code

File: ipc/glue/MiniTransceiver.cpp, line 181-204

  size_t total_readed = 0;
  uint32_t msgsz = 0;
  while (msgsz == 0 || total_readed < msgsz) {
    // Set IO vector with the begin of the unused buffer.
    hdr.msg_iov->iov_base = aDataBuf + total_readed;
    hdr.msg_iov->iov_len = (msgsz == 0 ? aBufSize : msgsz) - total_readed;

    // Read the socket
    ssize_t bytes_readed = HANDLE_EINTR(recvmsg(mFd, &hdr, 0));
    if (bytes_readed <= 0) {
      // Closed or error!
      return false;
    }
    total_readed += bytes_readed;
    MOZ_ASSERT(total_readed <= aBufSize);   // debug-only, no release check

    if (msgsz == 0) {
      // Parse the size of the message.
      // Get 0 if data in the buffer is no enough to get message size.
      msgsz = IPC::Message::MessageSize(aDataBuf, aDataBuf + total_readed); // peer-controlled
    }

    num_all_fds += RecvFDs(&hdr, all_fds + num_all_fds, aMaxFds - num_all_fds);
  }

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

  UniquePtr<char[]> databuf = MakeUnique<char[]>(kMaxDataSize);   // 8192-byte heap buffer
  uint32_t msgsz = 0;
  int all_fds[kMaxNumFds];                                       // 16-int stack array
  unsigned num_all_fds = 0;

  if (!RecvData(databuf.get(), kMaxDataSize, &msgsz, all_fds, kMaxDataSize,  // BUG: should be kMaxNumFds
                &num_all_fds)) {

File: ipc/glue/MiniTransceiver.cpp, line 99-105

    MOZ_ASSERT(iovlen <= kMaxIOVecSize);
    bytes_to_send += size;
  }
  MOZ_ASSERT(bytes_to_send <= kMaxDataSize);   // sender-side limit is also debug-only
  aHdr->msg_iovlen = iovlen;

  return bytes_to_send;

RecvData() reads msgsz from the peer-supplied IPC::Message header and uses it directly to size subsequent recvmsg() iovecs without clamping against aBufSize. The only bounds check, MOZ_ASSERT(total_readed <= aBufSize), is compiled out of release builds. Once msgsz > aBufSize, the next loop iteration sets iov_base past the end of the 8 KiB databuf and the kernel writes peer-controlled bytes there. Recv() additionally passes kMaxDataSize (8192) instead of kMaxNumFds (16) for aMaxFds, allowing a related stack overflow of all_fds via SCM_RIGHTS.

Exploit Chain

  1. Attacker obtains code execution in (or control over the bytes written by) the Linux fork-server process, which holds the other end of the parent's MiniTransceiver Unix socket.
  2. When the parent sends Msg_ForkNewSubprocess, the compromised fork-server replies with a well-formed IPC::Message whose header payload_size advertises a total size well above 8192 bytes, followed by the corresponding payload bytes.
  3. In the parent process, ForkServiceChild::SendForkNewSubprocess() calls MiniTransceiver::Recv(), which heap-allocates an 8192-byte databuf and calls RecvData().
  4. The first recvmsg() fills up to 8192 bytes; RecvData() then parses msgsz (>8192) from the peer header and loops again with iov_base = databuf + total_readed (already at or past the end of the buffer) and iov_len = msgsz - total_readed.
  5. recvmsg() copies the remaining attacker-controlled bytes directly past the end of the 8 KiB heap allocation — an attacker-controlled-length, attacker-controlled-content linear heap overflow on the parent's IPC-launcher thread.
  6. With heap grooming this can corrupt adjacent heap metadata or objects in the parent process, leading to potential code execution in the parent (sandbox escape from the fork-server boundary).

IPC Path

  • ForkServer reply over MiniTransceiver
    • Protocol: MiniTransceiver (ForkServer Unix socketpair)
    • Parent process: Parent (chrome)
    • Child process: ForkServer
    • Message flow: ForkServer -> Parent: Reply_ForkNewSubprocess (oversized IPC::Message)

Steps to Reproduce

  1. Apply the 4-line patch to ipc/glue/ForkServer.cpp that, after the 8th fork, appends 4096 extra int32 values to the Reply_ForkNewSubprocess message before SendInfallible(). Rebuild Firefox.
  2. Launch the ASan build with prefs: dom.ipc.forkserver.enable=true, dom.ipc.processPrelaunch.enabled=true, dom.ipc.processPrelaunch.fission.number=12.
  3. Load test.html (creates 12 data: iframes and 12 window.open calls, twice) to force >8 content-process launches via the fork server.
  4. Observe AddressSanitizer heap-buffer-overflow WRITE in the parent process at MiniTransceiver::RecvData (MiniTransceiver.cpp:189), 0 bytes after the 8192-byte region allocated at MiniTransceiver.cpp:221.

Security Impact

  • Severity: Moderate
  • Attacker capability: An attacker who controls the fork-server end of the MiniTransceiver socket can perform an unbounded, controlled-length, controlled-content linear heap-buffer-overflow write in the Firefox parent (chrome) process, potentially leading to parent-process code execution. A separate variant in the same function allows overflowing a 16-int stack array via excess SCM_RIGHTS file descriptors.
  • Preconditions: Linux with the fork server enabled (default in Nightly and in Mozilla's Linux .deb/.rpm packages). The attacker must already control the bytes written by the fork-server process to the parent's MiniTransceiver socket — i.e. a compromised fork-server process. The fork server is not directly reachable from web content, so this is a process-boundary hardening / defence-in-depth issue rather than a directly web-triggerable bug.

ASAN Report

==13358==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x786a07ef7900 at pc 0x55bb9fb8e6d5 bp 0x7619a53b2180 sp 0x7619a53b1928
WRITE of size 8244 at 0x786a07ef7900 thread T24
    #0 0x55bb9fb8e6d4 in write_iovec(void*, __sanitizer::__sanitizer_iovec*, unsigned long, unsigned long) /builds/worker/fetches/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:987:5
    #1 0x55bb9fba81c8 in write_msghdr(void*, __sanitizer::__sanitizer_msghdr*, long) /builds/worker/fetches/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:3245:3
    #2 0x55bb9fba8065 in ___interceptor_recvmsg /builds/worker/fetches/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:3263:7
    #3 0x7619dde4dd18 in mozilla::ipc::MiniTransceiver::RecvData(char*, unsigned long, unsigned int*, int*, unsigned int, unsigned int*) /firefox/ipc/glue/MiniTransceiver.cpp:189:28
    #4 0x7619dde4e176 in mozilla::ipc::MiniTransceiver::Recv(std::unique_ptr<IPC::Message, std::default_delete<IPC::Message>>&) /firefox/ipc/glue/MiniTransceiver.cpp:226:8
    #5 0x7619ddde4fa0 in mozilla::ipc::ForkServiceChild::SendForkNewSubprocess(mozilla::geckoargs::ChildProcessArgs&&, base::LaunchOptions&&, int*) /firefox/ipc/glue/ForkServiceChild.cpp:175:16
    #6 0x7619dddf07cc in mozilla::ipc::PosixProcessLauncher::DoLaunch() /firefox/ipc/glue/GeckoChildProcessHost.cpp:1351:39
    #7 0x7619dddecfcc in mozilla::ipc::BaseProcessLauncher::PerformAsyncLaunch() /firefox/ipc/glue/GeckoChildProcessHost.cpp:1085:10

0x786a07ef7900 is located 0 bytes after 8192-byte region [0x786a07ef5900,0x786a07ef7900)
allocated by thread T24 here:
    #0 0x55bb9fc0fa04 in __interceptor_malloc _asan_rtl_:3
    #1 0x55bb9fc65495 in moz_xmalloc /firefox/memory/mozalloc/mozalloc.cpp:48:15
    #2 0x7619dde4e11d in operator new[](unsigned long) /firefox/obj-firefox-asan/dist/include/mozilla/cxxalloc.h:42:10
    #3 0x7619dde4e11d in mozilla::MakeUnique<char [], unsigned long const&>(unsigned long const&) /firefox/obj-firefox-asan/dist/include/mozilla/UniquePtr.h:102:10
    #4 0x7619dde4e11d in mozilla::ipc::MiniTransceiver::Recv(std::unique_ptr<IPC::Message, std::default_delete<IPC::Message>>&) /firefox/ipc/glue/MiniTransceiver.cpp:221:31
    #5 0x7619ddde4fa0 in mozilla::ipc::ForkServiceChild::SendForkNewSubprocess(mozilla::geckoargs::ChildProcessArgs&&, base::LaunchOptions&&, int*) /firefox/ipc/glue/ForkServiceChild.cpp:175:16
    #6 0x7619dddf07cc in mozilla::ipc::PosixProcessLauncher::DoLaunch() /firefox/ipc/glue/GeckoChildProcessHost.cpp:1351:39

SUMMARY: AddressSanitizer: heap-buffer-overflow (/firefox/obj-firefox-asan/dist/bin/firefox+0xf66d4)
==13358==ABORTING
Group: dom-core-security
Group: core-security
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 —

MiniTransceiver is used exclusively between the parent and fork server processes, neither of which is sandboxed. This is a memory safety error but it wouldn't be useful for an exploit chain.

Assignee: nobody → jld
Duplicate of this bug: 2029902

As :jld noted, the fork server process being communicated to here is actually trusted, so if it requires a misbehaving fork server, we don't need to worry about it as a security bug.

That being said, we should probably fix it so that if someone tries to use MiniTransceiver for some other reason we could potentially run into issues in the future. (Though I admittedly have a hard time imagining when MiniTransceiver would be used elsewhere).

Priority: -- → P3

The severity field is not set for this bug.
:jld, could you have a look please?

For more information, please visit BugBot documentation.

Flags: needinfo?(jld)
Severity: -- → S4
Flags: needinfo?(jld)
Duplicate of this bug: 2056816
Duplicate of this bug: 2065832

Could we throw a bounds check in here or something? We've gotten a few dupes filed against this. Thanks.

Flags: needinfo?(jld)
Group: dom-core-security → general-core-security

I don't think this bug needs to be hidden. MiniTransceiver isn't used across a security boundary and there are currently no plans to do so. It's possible in theory that the parent process might accidentally crash the fork server this way, but I don't think there's any way to turn that into any kind of exploit.

Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Flags: needinfo?(jld)
Keywords: crash, sec-other
Group: general-core-security

I have a patch for this. I started out just trying to add bounds checks, but ended up doing a few other related cleanups while I was in there, to (hopefully) make things easier to follow.

Duplicate of this bug: 2066170

There is one intended functional change in here: avoid reading past the
end of the message, by reading only to the end of the header until the
message size is known. I have no evidence that this is breaking in
actual use, but it is possible and I don't like the implications.

Otherwise, this fixes the errant bounds checks mentioned in the bug, but
I also cleaned up a few other things related to bounds and resources:

  • Span is used for byte ranges instead of separate pointer/length

  • Received fds, once read out of the cmsg buffer, are always stored as
    RAII objects in an nsTArray rather than adding another static buffer
    and needing special-case code to close them on error.

  • MakeScopeExit instead of UniquePtr.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: