Sandbox escape via process type confusion in mozwer
A compromised content process could modify its own memory to pretend to be a browser process, causing the mozwer module to launch the crash reporter with inherited environment variables. By controlling these variables, the attacker could execute arbitrary code outside the sandbox.
- Fixed in 49 days
- Paid in 2 months
- Disclosed after 49 days
Created attachment 9370940 vn0027.patch
Sandbox escape via process type confusion in mozwer
TL;DR: A compromised content process on Windows 10 & Windows 11 can pretend to be a browser process, which will lead the mozwer module to spawn a crashreporter.exe process that inherits environment variables from the content process. By controlling these environment variables, the content process can execute arbitrary code from within the crash reporter and escape the sandbox.
Details
When one of the firefox processes throws an exception that must be handled out-of-process, a WER (Windows Error Reporting) process is spawned, which loads mozwer.dll and executes OutOfProcessExceptionEventCallback. At the end of this function, there's an attempt to determine whether the exception-throwing process is a browser process or a content process:
let wer_data = read_from_process::<InProcessWindowsErrorReportingData>(
process,
context as *mut InProcessWindowsErrorReportingData,
)?;
...
if wer_data.process_type == MAIN_PROCESS_TYPE {
handle_main_process_crash(crash_report, process, &application_info)
} else {
handle_child_process_crash(crash_report, process)
}
Where the context variable contains the address of the gInProcessWerData structure residing in the memory of the crashing process:
void RegisterRuntimeExceptionModule() {
...
gInProcessWerData.mProcessType = mozilla::GetGeckoProcessType();
gInProcessWerData.mOOMAllocationSizePtr = &gOOMAllocationSize;
if (FAILED(::WerRegisterRuntimeExceptionModule(sModulePath,
&gInProcessWerData))) {
// The registration failed null out sModulePath to record this.
*sModulePath = L'\0';
return;
}
...
}
Since wer_data is read from the memory of the crashing process, a content process can pose as a browser process by simply modifying gInProcessWerData.mProcessType within its own memory. Once that happens, handle_main_process_crash will be executed instead of handle_child_process_crash. This execution path launches the crash reporter:
...
let mut environment = read_environment_block(process)?;
launch_crash_reporter_client(
&application_information.install_path,
&mut environment,
&crash_report,
);
...
fn launch_crash_reporter_client(
install_path: &Path,
environment: &mut Vec<u16>,
crash_report: &CrashReport,
) {
...
unsafe {
if CreateProcessW(
null_mut(),
cmd_line.as_mut_ptr(),
null_mut(),
null_mut(),
FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW | CREATE_UNICODE_ENVIRONMENT,
environment.as_mut_ptr() as *mut _,
null_mut(),
&mut si,
&mut pi,
) != 0
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
}
When launching the crash reporter, the environment variables are inherited from the crashing process. As such, a content process can set its own environment variables and have them also be set in the launched crash reporter process. By controlling the environment variables inside the crash reporter process, there are multiple paths that a content process could use to achieve arbitrary code exec from the crash reporter (thus, outside the sandbox):
- By abusing the
MOZ_CRASHREPORTER_RESTART_ARG_*environment variables. These environment variables are used to determine the command line that should be executed in order to restart firefox. For example, by settingMOZ_CRASHREPORTER_RESTART_ARG_0=path//to//some//executable,MOZ_CRASHREPORTER_RESTART_ARG_1=some_arg_1etc., the crash reporter can be made to execute an arbitrary file present on the system with arbitrary command line arguments. However, user interaction is required, as this code path is only triggered once "Restart" is pressed by the user. Note that the attacker can confuse/convince the user to press the "Restart" button by abusing theMOZ_CRASHREPORTER_STRINGS_OVERRIDEenvironment variable, which allows the attacker to control the text (and thus the layout) of the crash reporter window. - By abusing the
MOZ_REPLACE_MALLOC_LIBenvironment variable. SettingMOZ_REPLACE_MALLOC_LIB=path//to//some//librarywill make the crash reporter process load the specified library on boot. This could be used in conjunction with other vulnerabilities (e.g. allowing the attacker to plant a malicious library file somewhere on the system), but it could also be used stand-alone (e.g. specifying a UNC share path on an SMB-enabled machine, or using a vulnerable library to trigger a vulnerability and continue from there). No interaction is required here. - By abusing the
SystemRootenvironment variable. This is similar to the case above, in thatSystemRootcan be hijacked to side-load DLLs whose full path is resolved using this variable. Certain dependencies of the crash reporter load their DLLs by using this environment variable (e.g.%SystemRoot%\system32\rpcrt4.dll), making it possible to abuse this behaviour.
Fix
To fix the issue, you need to ensure that mozwer doesn't trust the content process to be truthful about its process type, i.e. this information should come from somewhere unreachable to the content process. I'm not familiar with all the intrinsics of the underlying implementation, but querying the process' command line parameters (as is already being done in the child process crash code path) to determine its type would be a more suitable check, as the process cannot directly affect this.
Proof of Concept & Repro Steps
We assume that a content process has been fully compromised and can execute arbitrary code. The proof of concept consist of:
- The "payload" function that's executed from the content process and performs the steps necessary to trigger the sandbox escape:
- Set
gInProcessWerData.mProcessTypetoGeckoProcessType_Defaultto pose as a browser process tomozwer. - Setup the appropriate environment variables for code execution. Depending on the choice of option, one of the aforementioned methods is used. The first method is chosen by default, with
calc.exebeing the command line to execute. - Trigger a crash that needs to be handled OOP by WER. For example, we use a classic stack-based buffer overflow, which will be detected by the stack protector and throw out a stack corruption exception.
- Set
- A hook inside of the
console.time()method implementation that's used to trigger the payload function via a crafted HTML file. This part is irrelevant to the vulnerability and the exploit and is used solely for demonstration purposes.
To reproduce the issue:
- Apply the attached
vn0027.patchas a git patch in the root directory of firefox source.git apply /path/to/vn0027.patch
- Build firefox.
- Ensure that local
mozwer.dllhas been registered as a WER exception handler module:HKLM\SOFTWARE\Microsoft\Windows\Windows Error Reporting\RuntimeExceptionHelperModulescontains apath/to/mozwer.dllDWORD entry with value 0.
- Ensure that crash reporter files/folders have been properly setup in
%APPDATA%\Roaming\Mozilla\Firefox\Crash Reports:- There is an InstallTime file corresponding to the firefox build number/timestamp that was built earlier.
- The
pendingfolder exists.
- Grab the attached
vn0027.html. Start a local http server in the directory wherevn0027.htmlresides:python3 -m http.server
- Open previously-built firefox and navigate to
localhost:8000/vn0027.html. - Observe that firefox shows that the tab crashed.
- Observe that
crashreporter.exeis executed and the crash reporter window is shown. Observe that the process inherited environment variables from the content process corresponding to the html file that was opened earlier. (You can observe this via e.g. process explorer) - Observe that pressing "Restart" spawns
calc.exe(in case the first method is used for code exec), or a specified library has been loaded intocrashreporter.exe(if using the second or third method for code exec).
NOTE: I couldn't get mozwer to work on Windows 10 Pro 22H2 (19045.3803), though it could be a local issue. During DLL loading, the process attempts to load user32.dll as one of the dependencies of mozwer.dll and this throws out a 0xC0000135 error. Everything works as expected on a clean latest Windows 11 insider build, so I recommend using that to test, should you run into any issues on your regular setup.
IMPORTANT
It is very important to mention that the impact of the vulnerability extends beyond firefox. Since mozwer.dll is registered as a WER exception handler module in the registry, any (non-firefox) sandboxed process on the system can use it via WerRegisterRuntimeExceptionModule("path/to/mozwer.dll", ...) and escape its sandbox using this vulnerability. This virtually means that having firefox installed on the system makes it possible to escape from any sandboxed process running on the system.
Let me know if any further information is required. I won't be dedicating more time to this issue, unless requested.
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
The mozwer module trusted a process type value stored in the crashing process's own memory, which the content process could modify.
Impact
A compromised content process could escape the sandbox and execute arbitrary code, and any sandboxed process on the system could also exploit the registered mozwer.dll to escape its sandbox.
What the attacker needed
Compromised content process; Windows 11 (or Windows 10 with caveats); Firefox installed with mozwer.dll registered
The fix
The fix should ensure that mozwer does not trust the content process's self-reported process type, for example by querying the process command line instead.
How it was found
- Apply the provided patch to the Firefox source and build Firefox.
- Register mozwer.dll as a Windows Error Reporting exception handler module in the registry.
- Ensure crash reporter files and folders are set up in the user's AppData directory.
- Serve the provided HTML file from a local HTTP server.
- Open the HTML file in the built Firefox and observe the tab crash and crash reporter launch.
- Press Restart in the crash reporter to trigger execution of the specified command or library load.
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 content isolated from the rest of the system. This report shows that a malicious page that already broke out of the content process could trick a crash-handling component into starting a new program with attacker-controlled settings, letting it run code outside the sandbox.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.