Race condition in firefox!sandbox::SharedMemIPCServer::Init leading to relative out-of-bounds read/write in the broker process (Sandbox escape / LPE)
A race condition in Firefox's sandbox shared memory initialisation on Windows allows a renderer process to corrupt the broker's IPC channel buffer pointer, leading to out-of-bounds read/write in the broker process. An attacker with code execution in a renderer could potentially escape the sandbox and achieve local privilege escalation.
Originally disclosed on Mozilla
- Fixed in 35 days
- Paid in 42 days
- Disclosed after 35 days
Created attachment 9111144
race.zip
Hi,
These issues only affects Windows client, I found two race condition in the shared memory of Firefox sandbox implementation.
* Information Disclosure of broker heap address in CopyPolicyToTarget (race described in the next issue)
* Memory corruption (Sandbox escape) in sandbox::SharedMemIPCServer::ThreadPingEventReady (described in this issue)
Vulnerability:
Since Firefox (current version tested 70.0.1) configures the renderer sandbox with USER_LIMITED and Initial Integrity Level == Delayed Integrity Level (INTEGRITY_LEVEL_LOW), any renderer process can interact with other renderers process (read/write/createthread/duplicatehandles) even while new renderer process bootstrapping (when a new tab process is spawned).
Google Chrome is not affected because the renderer processes runs with a USER_LOCKDOWN token (also the initial integrity level = LOW and delayed IL is UNTRUSTED) which prevent access to other renderer processes.
The race condition vulnerability happens during new process bootstrapping, when a new tab process is created, the broker does:
1/ CreateProcess SUSPENDED with the lockdown token (USER_LIMITED for Firefox renderer) and low IL in TargetProcess::Create
2/ Assign target process to JOB object
3/ Change target thread token to initial token (USER_RESTRICTED_SAME_ACCESS) for initialisation later
4/ Create IPCServer shared memory in TargetProcess::Init
5/ DuplicateHandle the shared memory with the target in TargetProcess::Init
6/ Initialise the shared memory in SharedMemIPCServer::Init
So, the target process is suspended but other tabs are not and they can access the target process (thanks to USER_LIMITED token and same IL) and duplicate the IPC shared memory handle.
The shared memory initialisation is vulnerable to race condition when initialising service_context->channel_buffer in SharedMemIPCServer::Init (6):
bool SharedMemIPCServer::Init(void* shared_mem,
uint32_t shared_size,
uint32_t channel_size) {
..
client_control_ = reinterpret_cast<IPCControl*>(shared_mem); // client_control_ points to shared memory
..
for (size_t ix = 0; ix != channel_count; ++ix) {
ChannelControl* client_context = &client_control_->channels[ix]; // client_context points to shared memory
..
client_context->channel_base = base_start; // (A) Write to shared memory (channel_base)
client_context->state = kFreeChannel;
..
service_context->shared_base = reinterpret_cast<char*>(shared_mem);
service_context->channel_size = channel_size;
service_context->channel = client_context;
service_context->channel_buffer =
service_context->shared_base + client_context->channel_base; // (B) Race condition between (A) and (B), read from shared memory (the value client_context->channel_base can be different)
Source code in:
https://github.com/mozilla/gecko-dev/blob/master/security/sandbox/chromium/sandbox/win/src/sharedmem_ipc_server.cc#L122
This vulnerability can lead to relative out-of-bounds read and write in the broker process in sandbox::SharedMemIPCServer::ThreadPingEventReady when the ping event is signaled.
The pointer service_context->channel_buffer is fully controlled (64 bits added offset) due to the race condition.
Crash:
firefox!sandbox::CrossCallParams::GetParamsCount:
00007ff7`c6ae4d60 8b4160 mov eax,dword ptr [rcx+60h] ds:424243c6`124242a2=????????
0:018> kc
# Call Site
00 firefox!sandbox::CrossCallParams::GetParamsCount
01 firefox!sandbox::CrossCallParamsEx::CreateFromBuffer
02 firefox!sandbox::SharedMemIPCServer::InvokeCallback
03 firefox!sandbox::SharedMemIPCServer::ThreadPingEventReady
This exception is a first chance and handled by SEH, the second chance exception is:
firefox!sandbox::SharedMemIPCServer::ThreadPingEventReady+0x8d:
00007ff7`c6aff1bd 48894758 mov qword ptr [rdi+58h],rax ds:424243c6`1242429a=????????????????
during memcpy(call_params->GetCallReturn(), &call_result, sizeof(call_result));
Repro:
I didn't work a lot to make the race reliable so the provided POC (attachment payload_corrupt) rarely triggers the vulnerability.
To reproduce, please debug and break in the broker process between (A) and (B) to help the race on the first subchannel channel_base.
Install Python 2.7 64 bits and PythonForWindows (https://github.com/hakril/PythonForWindows) then run inject.py with the payload_corrupt.dll path.
Exploitability:
The exploitation assumes having code execution in sandboxed renderers processes (RCE -> LPE step targeted).
Since the race condition allow to corrupt the offset of the IPC channel buffer, you can overwrite data near the shared memory base in the broker without requiring an information disclosure vulnerability.
Notice that the shared_base is close to the service_context address which is stored in the broker heap.
dt firefox!sandbox::SharedMemIPCServer::ServerControl 0x00000183d630eec0
+0x018 channel_buffer : 0x424243c6`12424242 "--- memory read error at address 0x424243c6`12424242 ---"
+0x020 shared_base : 0x00000183`d0000000 "???"
+0x028 channel : 0x00000183`d0000010 sandbox::ChannelControl
+0x030 dispatcher : 0x00000183`d7f36a00 sandbox::Dispatcher
So, by spraying heap using an exploit convenient object (not researched), you can use this vulnerability to corrupt other heap allocation (craft fake IPC crosscall in the heap and corrupt adjacent allocations) and with some work, achieve code execution in Firefox main process (Sandbox escape).
Thank you
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 a race condition between writing the channel_base value and reading it to compute channel_buffer in SharedMemIPCServer::Init, allowing another renderer to modify the shared memory in between.
Impact
An attacker with code execution in a renderer could potentially corrupt broker heap memory and achieve sandbox escape, though the proof-of-concept rarely triggers the race without debugging.
What the attacker needed
Code execution in a renderer; Windows client; race condition triggered
How it was found
- Create a new tab process, which triggers the broker to set up shared memory while the process is suspended.
- From another renderer process, access the suspended process and duplicate the IPC shared memory handle.
- Modify the shared memory contents to change the channel_base value before the broker reads it.
- Cause the broker to use the corrupted channel_buffer pointer, leading to out-of-bounds access.
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.
Firefox uses a sandbox to keep web pages from harming your computer. A race condition is when two things happen at the same time and the program gets confused. Here, a malicious web page could run code inside the sandbox and then trick the browser's main process into reading or writing memory it shouldn't, potentially breaking out of the sandbox.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.