MiniTransceiver::Recv passes kMaxDataSize instead of kMaxNumFds — stack buffer overflow in ForkServer IPC
Categories
(Core :: IPC, defect)
Tracking
()
People
(Reporter: ahmedalbalawij, Unassigned)
Details
Attachments
(1 file)
|
9.23 KB,
application/octet-stream
|
Details |
Bug Summary
MiniTransceiver::Recv() at ipc/glue/MiniTransceiver.cpp:226 passes the wrong constant as the max file descriptor count parameter. It passes kMaxDataSize (8192) instead of kMaxNumFds (16). The stack-allocated array all_fds[kMaxNumFds] holds only 16 ints (64 bytes), but RecvData and RecvFDs believe they can store up to 8192 FDs. This causes a stack buffer overflow when more than 16 file descriptors are received.
The MOZ_ASSERT at line 161 checks against the wrong limit (8192), so it never fires even in debug builds.
ForkServer is enabled by default on all Linux release/beta/nightly Firefox builds (dom.ipc.forkserver.enable = true, StaticPrefList.yaml:3644).
Root Cause
Two constants are defined at lines 22-23:
static const size_t kMaxDataSize = 8 * 1024; // 8192 — data buffer size
static const size_t kMaxNumFds = 16; // max file descriptors
At line 221-226, Recv() allocates the stack array with kMaxNumFds but passes kMaxDataSize to RecvData:
int all_fds[kMaxNumFds]; // 16 ints = 64 bytes on stack
if (!RecvData(databuf.get(), kMaxDataSize, &msgsz, all_fds, kMaxDataSize, &num_all_fds)) {
// data buffer OK ^^^^^^^^^^^^ FDs ^^^ ^^^^^^^^^^^^
// BUG: should be kMaxNumFds
RecvData passes aMaxFds=8192 to InitMsgHdr (line 173), which allocates a control buffer sized for 8192 FDs. The kernel can then deliver up to 253 FDs per recvmsg call (Linux SCM_MAX_FD limit). RecvFDs (line 163) copies all received FDs into the 16-element stack array via memcpy:
memcpy(aAllFds + num_all_fds, part_fds, num_part_fds * sizeof(int));
With 253 FDs received in a single recvmsg, this writes 253 × 4 = 1012 bytes into a 64-byte stack buffer — a 948-byte stack overflow.
The MOZ_ASSERT at line 161 is:
MOZ_ASSERT(num_all_fds + num_part_fds <= aMaxFds); // aMaxFds=8192
Since 253 <= 8192 is always true, this assertion never catches the overflow, even in debug builds.
Second Bug: Heap Buffer Overflow (line 186)
In the same RecvData function, if the declared message size (msgsz) parsed from the IPC header exceeds the buffer size (8192), the loop continues reading past the heap buffer:
hdr.msg_iov->iov_len = (msgsz == 0 ? aBufSize : msgsz) - total_readed;
The only guard is MOZ_ASSERT(total_readed <= aBufSize) at line 195 — stripped in release builds.
- Build Firefox on Linux with ASAN (--enable-address-sanitizer)
- Note: ForkServer is disabled in ASAN builds by default. To test, set dom.ipc.forkserver.enable = true or write a standalone test:
- Create a Unix domain socket pair
- Create a MiniTransceiver on the receiving end
- On the sending end, use sendmsg with SCM_RIGHTS to send >16 file descriptors
- Call MiniTransceiver::Recv on the receiving end
- ASAN reports stack-buffer-overflow in RecvFDs at the memcpy call
Expected Result
RecvFDs should reject or truncate file descriptors beyond kMaxNumFds (16).
Actual Result
RecvFDs writes up to 253 FDs (1012 bytes) into a 64-byte stack buffer. No release-build or debug-build guard prevents this.
Attack Surface
The ForkServer IPC socket is between privileged processes (parent ↔ ForkServer). The FD is properly closed in forked children (ForkServer destructor at RunForkServer line 351), so a compromised content process cannot directly reach this code path. The attack surface is between privileged processes during process spawning.
This is a defense-in-depth failure: the wrong constant makes the bug invisible to all testing, and any future code that uses MiniTransceiver for less-trusted IPC would inherit this vulnerability.
Affected Versions
- All Firefox versions on Linux with ForkServer enabled
- dom.ipc.forkserver.enable defaults to true on release/beta/nightly (StaticPrefList.yaml:3639-3644)
- Disabled only on ASAN/TSAN/UBSAN/code-coverage builds
Suggested Fix
-
Line 226: Change kMaxDataSize to kMaxNumFds:
if (!RecvData(databuf.get(), kMaxDataSize, &msgsz, all_fds, kMaxNumFds, &num_all_fds)) { -
Line 161: Promote to MOZ_RELEASE_ASSERT:
MOZ_RELEASE_ASSERT(num_all_fds + num_part_fds <= aMaxFds); -
Line 197: Add release-build check for message size vs buffer:
if (msgsz > aBufSize) {
return false;
}
How to use The POC:
- Copy to a Linux machine (this is Linux-only — Unix domain sockets + SCM_RIGHTS)
- Compile with ASAN:
gcc -o poc poc_minitransceiver_stack_overflow.c -fsanitize=address -g - Run:
./poc - ASAN will report: ERROR: AddressSanitizer: stack-buffer-overflow in RecvFDs_buggy at the memcpy line
What the PoC does:
- Creates a Unix socket pair (exactly like ForkServer IPC)
- Child sends 64 FDs via sendmsg + SCM_RIGHTS (64 > 16 = overflow)
- Parent receives with control buffer sized for 8192 FDs (the bug — should be 16)
- RecvFDs_buggy does memcpy of all 64 FDs into a 16-int stack array (64 bytes)
- Result: 64 × 4 = 256 bytes written into 64-byte buffer = 192 bytes of stack overflow
The code maps 1:1 to Firefox's MiniTransceiver.cpp — same constants, same RecvFDs loop, same memcpy. Each line references the exact Firefox source line number. You can also pass a custom FD count: ./poc 253 sends the max (253 FDs = 948 bytes past buffer).
Comment 1•2 months ago
|
||
The severity field is not set for this bug.
:jld, could you have a look please?
For more information, please visit BugBot documentation.
Updated•2 months ago
|
Description
•