Closed Bug 2065832 Opened 3 months ago Closed 3 months ago

MiniTransceiver::Recv passes kMaxDataSize instead of kMaxNumFds as the fd bound, allowing a stack buffer overflow of all_fds[] when >16 fds are received

Categories

(Core :: IPC, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2029466

People

(Reporter: ahmedalbalawij, Unassigned)

Details

(Keywords: reporter-external)

Attachments

(1 file)

Attached file minitransceiver_demo.c —

Stack buffer overflow in MiniTransceiver::Recv() — incorrect fd-count bound lets received file descriptors overflow a fixed 16-element stack array

Overview

ipc/glue/MiniTransceiver.cpp contains a stack buffer overflow. MiniTransceiver::Recv() receives incoming file descriptors into a fixed-size stack array of kMaxNumFds (16) elements, but it passes the wrong constant — kMaxDataSize (8192) — as the descriptor-count limit. Because the only bounds check on the write is a debug-only assertion, an incoming message that carries more than 16 file descriptors writes past the end of the stack array in release/opt builds.

Affected code

MiniTransceiver::Recv() (around line 223):

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

if (!RecvData(databuf.get(), kMaxDataSize, &msgsz, all_fds, kMaxDataSize,
&num_all_fds)) { // ^^^^^^^^^^^^
return false; // 5th arg is aMaxFds (capacity of all_fds),
} // but kMaxDataSize (8192) is passed instead

The relevant constants (top of the file):

static const size_t kMaxDataSize = 8 * 1024; // 8192 — size of the DATA buffer
static const size_t kMaxNumFds = 16; // capacity of the FD array

The 5th argument to RecvData is aMaxFds, i.e. the number of descriptor slots available in all_fds. It should be kMaxNumFds (16). Instead the code passes kMaxDataSize (8192), which is the size of the unrelated databuf data buffer.

Root cause / how the overflow happens

RecvData forwards that bound to RecvFDs() (around line 180), which copies the descriptors from the socket's SCM_RIGHTS control message into all_fds:

unsigned MiniTransceiver::RecvFDs(msghdr* aHdr, int* aAllFds, unsigned aMaxFds) {
...
for (cmsghdr* cmsg = CMSG_FIRSTHDR(aHdr); cmsg; cmsg = CMSG_NXTHDR(aHdr, cmsg)) {
...
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
memcpy(aAllFds + num_all_fds, part_fds, num_part_fds * sizeof(int)); // OOB write
num_all_fds += num_part_fds;
}
return num_all_fds;
}

The only thing preventing a write past all_fds is the MOZ_ASSERT, which is compiled out in release/opt builds. Since aMaxFds is 8192 (not 16), RecvFDs will happily memcpy more than 16 descriptors into the 16-slot stack array. A single message can carry up to SCM_MAX_FD (253) descriptors per recvmsg, and RecvData accumulates descriptors across its receive loop, so the received descriptor count can exceed 16 and overflow the array.

This overflow occurs on the fork-server IPC path: MiniTransceiver::Recv() is used to receive the "fork new subprocess" message (the fds_to_remap descriptor list) sent via ForkServiceChild::SendForkNewSubprocess() in ipc/glue/ForkServiceChild.cpp. If that descriptor list contains more than 16 entries, RecvFDs overflows all_fds[16] on the fork server's stack, corrupting adjacent stack memory with attacker-influenceable descriptor integer values.

Reproduction

The vulnerable logic — a 16-slot stack array, the incorrect kMaxDataSize bound, and the debug-only assertion — reproduced standalone and built with -DNDEBUG (to match an opt build) triggers the overflow under AddressSanitizer:

static const unsigned kMaxNumFds = 16;
static const unsigned kMaxDataSize = 8*1024;

attribute((noinline))
static unsigned RecvFDs(int* aAllFds, unsigned aMaxFds,
const int* part_fds, unsigned num_part_fds) {
unsigned num_all_fds = 0;
assert(num_all_fds + num_part_fds <= aMaxFds); // stripped under NDEBUG
memcpy(aAllFds + num_all_fds, part_fds, num_part_fds * sizeof(int));
return num_all_fds + num_part_fds;
}

int main(void) {
int all_fds[kMaxNumFds]; // 16-slot / 64-byte array
int incoming[32]; for (int i=0;i<32;i++) incoming[i]=100+i;
RecvFDs(all_fds, kMaxDataSize /* bug: should be kMaxNumFds */, incoming, 32);
}

Build and run:

clang -fsanitize=address -O0 -DNDEBUG demo.c -o demo && ./demo

Output:

==...==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x...
WRITE of size 128 at 0x... thread T0
#0 __asan_memcpy
#1 RecvFDs
#2 main
SUMMARY: AddressSanitizer: stack-buffer-overflow in __asan_memcpy

32 descriptors × 4 bytes = a 128-byte write into the 64-byte array — a 64-byte stack overflow. (The standalone .c file and its ASan output are attached.)

Suggested fix

Pass the fd-array capacity, not the data-buffer size, as aMaxFds:

--- a/ipc/glue/MiniTransceiver.cpp
+++ b/ipc/glue/MiniTransceiver.cpp
@@ -223,7 +223,7 @@ bool MiniTransceiver::Recv(UniquePtr<IPC::Message>& aMsg) {
int all_fds[kMaxNumFds];
unsigned num_all_fds = 0;

  • if (!RecvData(databuf.get(), kMaxDataSize, &msgsz, all_fds, kMaxDataSize,
  • if (!RecvData(databuf.get(), kMaxDataSize, &msgsz, all_fds, kMaxNumFds,
    &num_all_fds)) {
    return false;
    }

As additional hardening, the MOZ_ASSERT in RecvFDs should be promoted to a release-mode check so that the copy stops at aMaxFds rather than overflowing even if a caller passes a wrong bound in the future:

if (num_all_fds + num_part_fds > aMaxFds) {
return num_all_fds; // refuse to write past the buffer
}

Group: core-security, dom-core-security
Group: core-security
Status: UNCONFIRMED → RESOLVED
Closed: 3 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

Creator:
Created:
Updated:
Size: