Skip to content
Race condition Disclosed report Advanced CVE-2022-22753

Arbitrary permissions overwrite due to folder locking TOCTOU in Maintenance Service

A time-of-check to time-of-use flaw in the Mozilla Maintenance Service's folder permission update allows an attacker to swap a subfolder with a junction, causing the service to grant 'All Permissions' to the Users group on files in the junction target. This enables privilege escalation to SYSTEM, for example by replacing the service binary with a malicious one.

Seb Patane Follow

Originally disclosed on Mozilla

  • Fixed in 2 months
  • Paid in 3 months
  • Disclosed after 2 months
Text
Created attachment 9242817
poc.zip

There is a time of check to time of use bug in the way the fix-update-directory-perms functionality of the maintenance service locks folders that allows an attacker to swap out a subfolder with a junction. The files in the junction target will then have 'All Permissions' for the 'Users' group added to them. This effectively allows privilege escalation to SYSTEM, eg. by targeting the Mozilla Maintenance Service installation directory and replacing the maintenanceservice.exe binary with a malicious one.

The TOCTOU is in the commonupdatedir.cpp:Lock() function [0]. There is a brief window between the initial call to GetFileAttributesW() on line 413 to when the directory is locked with the mozlock file on line 435 and then again to when the attributes are updated on line 448.

During this time, the directory referenced in path can be replaced with a directory junction to a temporary directory. When this happens, the mozlock file is created in the temporary directory target, which doesn't lock access to original path. The junction can then be removed and replaced with a normal folder before the second call to GetFileAttributesW() so the class records attributes for a standard folder.

It needs to be left as a normal folder throughout the filesystem link checks in the Reset() function (line 508) and up until after the call to file.PermsOK() in EnsureCorrectPermissions() (line 1486) to avoid the directory being moved out of the way. After this point, it can be replaced with a junction to the actual target directory which the maintenance service will recurse into (line 1528) and update the permissions of all files contained.

The fact the mAttributes property is only updated/checked at lock time means once it's poisoned we don't have to worry about it.

The timing is very tight, but I've confirmed it's possible with a proof of concept (attached). I did have to run Prime95 to generate some load at the same time and I manually changed it's priority to Above Normal - the idea being that the maintenance service runs at Normal priority and the POC runs at High priority so the load should only slow down the maintenance service.

I had success with 0ms delay for stage 1, 181ms delay for stage 2 and 11ms delay for stage 3. The only mandatory parameter the POC takes is the target directory to overwrite the permissions on, but will also take lower/upper bounds for the delays on the three race "stages". The following command line worked on my system after around 20 minutes:

.\FirefoxPoc.exe 'C:\\Program Files (x86)\\Mozilla Maintenance Service\\' 0 100 150 250 0 50

Where 0 100 means stage 1 will start at a delay of 0ms and keep trying up until 100ms, 150 250 is stage 2 starting at 150ms up to 250ms and stage 3 starting at 0ms up to 50ms. If that yields no results after running a few times, you can just run the command line without the timing hints and it will try to work it out itself - that took me a good five or so hours though. Eg.

.\FirefoxPoc.exe 'C:\\Program Files (x86)\\Mozilla Maintenance Service\\'

Please let me know if you need more information

[0] - https://hg.mozilla.org/mozilla-central/file/00858d05a9e2685dac987d90866f201989def64d/toolkit/mozapps/update/common/commonupdatedir.cpp#l412

The original report as published on Mozilla's Bugzilla, by Seb Patane. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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 checking folder attributes and locking/updating permissions, allowing a junction to be swapped in.

What the attacker needed

Local access to the system; ability to create junctions; timing the race precisely

How it was found

  1. Run the proof-of-concept executable with the target directory as an argument.
  2. Optionally provide timing bounds for the three race stages to improve success.
  3. The proof-of-concept repeatedly attempts to swap the folder with a junction during the race window.
  4. If successful, the maintenance service grants 'All Permissions' to the Users group on files in the junction target.

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.

The maintenance service updates folder permissions but checks a folder and then uses it later, leaving a gap. An attacker can quickly replace the folder with a shortcut (junction) to another location, so the service accidentally gives everyone full control over files there. This lets the attacker replace a system program with their own, gaining higher privileges.

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.

More by Seb Patane

View all

10 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.