Double-delete UAF in BrokerServicesBase::SpawnTarget()
A double-delete use-after-free occurs in BrokerServicesBase::SpawnTarget() when AddTargetPeerInternal() fails, because SpawnCleanup() deletes the target while it is still owned by policy_base->targets_. An attacker could potentially trigger this by causing many concurrent fetches, leading to memory corruption.
Originally disclosed on Mozilla
- Fixed in 7 days
- Paid in 18 days
- Disclosed after 7 days
`BrokerServicesBase::SpawnTarget()` (`security/sandbox/chromium/sandbox/win/src/broker_services.cc`) can cause a double-delete UAF if Windows cannot duplicate a handle [1]. The bug is in the `!job.IsValid()` case (see lines 662 et seq below). If `AddTargetPeerInternal()` fails (which can occur if the Windows API `DuplicateHandle()` returns error), then `SpawnTarget()` calls `SpawnCleanup(target)`, which does `delete target`. But line 638 in `SpawnTarget()` already called `policy_base->AddTarget(target)`, which adds `target` to `policy_base->targets_`, which is a list of raw pointers. When `policy_base`'s reference count goes to 0, its destructor `PolicyBase::~PolicyBase()` (`security/sandbox/chromium/sandbox/win/src/sandbox_policy_base.cc`) does:
'''
122: TargetSet::iterator it;
123: for (it = targets_.begin(); it != targets_.end(); ++it) {
124: TargetProcess* target = (*it);
125: delete target; // BOOM!
126: }
'''
thus causing a double-delete UAF.
'''
423: ResultCode BrokerServicesBase::SpawnTarget(...
...
426: scoped_refptr<TargetPolicy> policy...) {
...
457: scoped_refptr<PolicyBase> policy_base(static_cast<PolicyBase*>(policy.get()));
...
637: // Now the policy is the owner of the target.
638: result = policy_base->AddTarget(target);
...
646: if (job.IsValid()) {
...
662: } else {
663: result = AddTargetPeerInternal(process_info.process_handle(),
664: process_info.process_id(),
665: policy_base, last_error);
666: if (result != SBOX_ALL_OK) {
667: // This may fail in the same way as Job associated processes.
668: // crbug.com/480639.
669: SpawnCleanup(target);
670: return result;
671: }
672: }
...
675: return result;
676: }
'''
'''
688: ResultCode BrokerServicesBase::AddTargetPeerInternal(
...
695: HANDLE tmp_process_handle = INVALID_HANDLE_VALUE;
696: if (!::DuplicateHandle(::GetCurrentProcess(), peer_process_handle,
697: ::GetCurrentProcess(), &tmp_process_handle,
698: SYNCHRONIZE, false, 0 /*no options*/)) {
699: *last_error = ::GetLastError();
700: return SBOX_ERROR_CANNOT_DUPLICATE_PROCESS_HANDLE;
701: }
...
714: }
'''
You can demonstrate the UAF using the debugger. To do so:
1. Start FF and attach a debugger to it.
2. Set a BP on `broker_services.cc` line 663. Also set a BP on `PolicyBase()::~PolicyBase()`
3. Open any webpage in FF.
4. When the line 663 BP fires, step into `AddTargetPeerInternal()`. Simulate an error from `DuplicateHandle()` by skipping lines 696-98, setting control to line 700, setting `*lastError` to `5` (or anything else except `0`, really), and stepping out of the function.
5. Step into `SpawnCleanup()` and notice that it does `delete target`. Note the value of `target` and open a memory window there.
6. Proceed. Eventually the reference count of the object that was pointed to by `policy_base` will reach 0, and something will call `PolicyBase()::~PolicyBase()` in the chrome process.
7. Step the loop and notice that `~PolicyBase()` calls `delete` on the address you noted in step 5, which, of course, the code no longer owns.
8. Step further and watch the mayhem.
Interestingly, `BrokerServicesBase::SpawnTarget()`'s author noticed that this UAF was possible (lines 667-68), but never fixed it.
I will report this bug upstream.
[1] An attacker might be able to cause this situation by, e.g., using a script to do fetches from many URLs concurrently.
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 target object is added to policy_base->targets_ (a list of raw pointers) but not removed when SpawnCleanup() deletes it, leading to a double-delete when the policy is destroyed.
Impact
An attacker might be able to cause this situation by using a script to do fetches from many URLs concurrently, potentially leading to memory corruption.
What the attacker needed
Ability to trigger DuplicateHandle failure; concurrent fetches from many URLs
How it was found
- Start Firefox and attach a debugger.
- Set breakpoints on broker_services.cc line 663 and PolicyBase::~PolicyBase().
- Open any webpage in Firefox.
- When the line 663 breakpoint fires, step into AddTargetPeerInternal(), simulate an error from DuplicateHandle() by skipping lines 696-98, setting control to line 700, setting *lastError to 5, and stepping out.
- Step into SpawnCleanup() and note that it deletes target; record the address.
- Proceed until PolicyBase::~PolicyBase() is called, then step the loop and observe it deletes the same address again.
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.
A use-after-free (UAF) is a bug where a program uses memory after it has been freed, which can lead to crashes or code execution. Here, the code deletes an object twice: once directly and once later when the policy object is destroyed, because the object was added to a list of raw pointers without removing it.
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.