Use-after-free in RasterImage::OnSurfaceDiscarded due to check-then-AddRef race on a raw SurfaceCache ImageKey
A race condition in Firefox's image SurfaceCache lets a decoder thread AddRef a RasterImage after its final release has already committed destruction, causing a heap use-after-free. An attacker could trigger this by creating SurfaceCache pressure across cross-site frames and releasing the owning frame, potentially leading to memory corruption in the content process.
Originally disclosed on Mozilla
- Fixed in 5 days
- Paid in 55 days
- Disclosed after 5 days
Created attachment 9615287
poc.rar
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36
Firefox for Android
Steps to reproduce:
A web page creates SurfaceCache pressure (many discardable image surfaces across
cross-site frames) while releasing the frame that owns the image requests. No
click, permission, extension, or pref change is required. An automated PoC is
attached (poc/run_fission_fanout_trial.py; see poc/README.md and SUBMISSION.md
for the exact command). The race is intermittent.
Confirmed on:
- Official ASAN Nightly 154.0a1 (Build ID 20260713202634)
- A from-source opt build of current tip (155.0a1, rev 9f4db3078551)
Actual results:
Heap use-after-free in the content process. SurfaceCacheImpl::Remove() calls
OnSurfaceDiscarded() through a raw, non-owning ImageKey (ImageResource*).
RasterImage::OnSurfaceDiscarded() checks the Atomic<bool> mIsBeingDestroyed flag
and only afterwards constructs RefPtr<RasterImage> image = this — an AddRef after
the final Release() has already reached zero and destruction has committed. The
captured runnable then reads freed RasterImage storage (mProgressTracker at
offset 8) on the main thread.
ASAN: heap-use-after-free, READ of size 8, 8 bytes into a freed 248-byte
RasterImage; use in OnSurfaceDiscardedInternal; free via RasterImage::Release()
through imgRequest teardown; alloc via ImageFactory::CreateRasterImage() on the
ImageIO thread. Full ASAN block in poc/asan-report.txt.
Root cause (both revisions, byte-identical affected files):
OnSurfaceDiscarded runs on a decoder/ImageIO thread holding the SurfaceCache
static mutex; ~RasterImage() sets the flag then blocks in RemoveImage() on that
mutex. Interleaving: (1) decoder reads mIsBeingDestroyed==false; (2) main thread
Release()->0, ~RasterImage sets the flag and blocks on the mutex; (3) decoder
does the post-zero AddRef and queues the runnable; (4) main thread frees the
object; (5) the runnable reads freed memory.
Note: this check-then-AddRef dispatch pattern originates from bug 1351434
(avoid working under the SurfaceCache mutex). Related: bug 1382664.
Expected results:
The discard notification must not acquire or queue a reference to an image after
its final release has committed destruction. Rechecking mIsBeingDestroyed still
leaves a check-then-AddRef race. Correct fix: give the SurfaceCache key promotable
lifetime semantics (e.g. surfaces hold a promotable weak ref to their image and
Remove() promotes it with an atomic AddRef-if-live, skipping notification once
destruction has begun), or otherwise remove the image's discard callbacks before
the final owning release can commit.
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 check-then-AddRef race: OnSurfaceDiscarded reads the mIsBeingDestroyed flag and only afterwards constructs a RefPtr, so a concurrent final Release can free the object before the AddRef happens.
Impact
The race is intermittent but can cause a heap use-after-free read in the content process, which may be exploitable for memory corruption; no click, permission, extension or pref change is required.
What the attacker needed
Victim loads a crafted web page; no click or permission needed
The fix
The discard notification should not acquire or queue a reference after final release; the report suggests giving SurfaceCache keys promotable lifetime semantics or removing discard callbacks before the final owning release.
How it was found
- Create SurfaceCache pressure with many discardable image surfaces across cross-site frames.
- Release the frame that owns the image requests.
- Let the decoder thread and main thread interleave so the final release commits destruction between the flag check and the AddRef.
- Observe the queued runnable reading freed RasterImage memory on the main thread.
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.
When a web page shows images, Firefox keeps them in a shared cache and can throw them away to save memory. A background thread tells the image object it has been discarded, but it checks whether the object is still alive and then grabs a reference in two separate steps. If the object is freed in between, the thread ends up touching memory that no longer belongs to it, which is a use-after-free.
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.