← All reports

[1] CSSSegmentedFontFace::fontRanges() UAF via reentrant FontFace promise rehash

HighWebCore CSS font matchingUAF

A font promise lets any page pick the exact moment a renderer cache dies

425cd4e

High. The dangling reference points into a freed hash-table bucket array, and author script chooses exactly when that array is freed. Only the reclaim step keeps this from going higher: the outer frame's writes become controlled only if a same-size allocation lands in the freed slot first.

Font matching in WebKit's CSS engine memoizes its answers: once it has worked out which fonts cover which code points for a given family, size and style, it caches that list so text measurement does not redo the work. The cache lives on a CSSSegmentedFontFace (the object that represents one font family as an ordered list of @font-face sources) as m_cache, a WTF HashMap that stores its entries inline in a single bucket array. Filling an entry walks the family's sources and may load each one, and nothing else is expected to insert into m_cache during that walk.

The angle: any page script can free the CSSSegmentedFontFace cache's bucket array while a native frame is still writing through a reference into it. The result is a renderer use-after-free whose free point the attacker times.

fontRanges() inserted an empty FontRanges into m_cache and kept a reference into the bucket array while looping over m_fontFaces. The loop calls CSSFontAccessor::font(), which for a local() source completes the load synchronously and resolves the FontFace loaded promise, running author script. That script re-enters fontRanges() on the same object, and because FontDescriptionKey includes computedSize each call inserts a new key, so enough of them rehash the table and free the array the outer call still points into. Build the FontRanges in a local and insert it once, after the loop. This is the same pattern used by ManualSlotAssignment::slotManualAssignmentDidChange and AXIsolatedTree::updateChildren, which both take a local copy instead of a reference into a HashMap that code called below can insert into.

Source/WebCore/css/CSSSegmentedFontFace.cpp

FontRanges CSSSegmentedFontFace::fontRanges(const FontDescription& fontDescription, ...)
{
- auto addResult = m_cache.add(std::make_tuple(FontDescriptionKey(fontDescription), fontPaletteValues), FontRanges());
- auto& ranges = addResult.iterator->value;
+ auto key = std::make_tuple(FontDescriptionKey(fontDescription), fontPaletteValues);
 
- if (!addResult.isNewEntry)
- return ranges;
+ if (auto entry = m_cache.find(key); entry != m_cache.end())
+ return entry->value;
 
+ // ... A reference into m_cache's
+ // bucket array would dangle the moment such a reentrant insertion rehashed the table.
+ FontRanges ranges;
auto desiredRequest = fontDescription.fontSelectionRequest();
 
for (auto& face : m_fontFaces) {
...
if (ranges.isNull() && !fontAccessor->font(ExternalResourceDownloadPolicy::Forbid))
continue;
appendFont(ranges, WTF::move(fontAccessor), face->ranges());
}
ranges.shrinkToFit();
+ m_cache.add(WTF::move(key), ranges);
return ranges;
}

LayoutTests/sar3/fast/text/font-face/css-segmented-font-face-reentrancy-crash.html

+ let face = new FontFace('MyTestFam', 'local(Helvetica)');
+ face.loaded;
+ document.fonts.add(face);
+ Object.defineProperty(FontFace.prototype, 'then', {
+ get() {
+ ...
+ for (let sizeIndex = 0; sizeIndex < 40; sizeIndex++) {
+ let context = new OffscreenCanvas(10, 10).getContext('2d');
+ context.font = (11 + sizeIndex) + 'px MyTestFam';
+ context.measureText('a');
+ }
+ ...
+ let context = new OffscreenCanvas(10, 10).getContext('2d');
+ context.font = '10px MyTestFam';
+ context.measureText('a');

fontRanges() no longer inserts a placeholder up front. It builds the key once and probes with m_cache.find(key), returning a copy of the cached value on a hit. On a miss it builds FontRanges ranges as a stack local. The loop over m_fontFaces is unchanged: it creates a CSSFontAccessor per face, may call font(ExternalResourceDownloadPolicy::Forbid), and calls appendFont. After shrinkToFit() the finished value is inserted exactly once with m_cache.add(WTF::move(key), ranges). A comment documents the re-entrancy hazard. The new LayoutTest installs a then getter on FontFace.prototype and re-enters font resolution at 40 distinct pixel sizes on the same family.

Holding a reference into a growable hash table's storage across a call that can re-enter and insert into the same table, so a reentrant rehash frees the storage under the outstanding reference.

WTF HashMap. An open-addressing hash table that stores keys and values inline in one bucket array. When an insertion pushes the load factor past its threshold, the table allocates a new array and frees the old one. HashMap::add() returns an AddResult whose iterator->value is a reference into that array.

Font lookup structure. CSSSegmentedFontFace represents one family as a sequence of CSSFontFace entries, coming from @font-face rules or the FontFace API. fontRanges() turns a FontDescription into a FontRanges, an ordered list of (code-point range, FontAccessor) entries that shaping and measurement use to pick a font per code point. Results are memoized in m_cache, keyed on FontDescriptionKey (size, weight, style and similar attributes) plus FontPaletteValues.

CSSFontAccessor and local() sources. CSSFontAccessor is a refcounted lazy handle that asks its CSSFontFace for a concrete Font on the first font() call and caches the result. A local() source names an installed system font instead of a URL, so no network fetch is involved and the load can complete synchronously.

FontFace.loaded and thenable lookup. FontFace.loaded is a promise that resolves with the FontFace object itself. Under ECMAScript promise semantics, resolving a promise with an object synchronously reads that object's then property. A then getter therefore runs at the resolution point, not on a later microtask.

This is a use-after-free of WTF HashMap bucket storage, reached through JavaScript re-entrancy.

  outer fontRanges()                     author script (then getter)
  ──────────────────                     ───────────────────────────
  m_cache.add(key, {})
  ranges = &bucket[k] ────┐
  loop: accessor->font()  │
    local() load completes│
    resolve(face.loaded) ─┼──► Get(face, "then")
                          │     measureText at 11px..50px
                          │       fontRanges() x40 -> m_cache.add(newKey)
                          │         rehash: old bucket array freed
                          │ ◄───┘
  appendFont(ranges, ...) ┘   <- write into freed array
  shrinkToFit / return copy   <- read from freed array

In the left column, ranges is bound to a slot inside the bucket array before the loop starts. The patch's own comment states that CSSFontAccessor::font() on a local() source finishes the load synchronously, notifies clients, and can resolve FontFace.loaded, and the regression test exercises exactly that path. Resolution performs Get(face, "then"), which hands control to the right column while the outer frame is still mid-loop. FontDescriptionKey includes the computed size, so each of the 40 measureText calls at a different pixel size misses the cache and inserts a new key. Enough insertions cross the load-factor threshold and the table rehashes, freeing the array that ranges points into. When control returns, appendFont(ranges, ...) calls ranges.appendRange(...) on the stale slot, shrinkToFit() mutates it again, and return ranges copy-constructs the result from freed memory.

A second invalidation route exists in the surrounding source: CSSSegmentedFontFace::fontLoaded() calls m_cache.clear(). If fontLoaded() could fire synchronously inside the loop, it would also free the table under ranges. Client notification in CSSFontFace.cpp goes through iterateClients, and whether that reaches fontLoaded() inside this loop is not established.

The regression test drives the bug in four steps:

  1. Create new FontFace('MyTestFam', 'local(Helvetica)'), touch face.loaded, and add the face to document.fonts.
  2. Define a then getter on FontFace.prototype. Any resolution of a promise with a FontFace now runs this getter synchronously.
  3. Call measureText('a') on an OffscreenCanvas at 10px MyTestFam. This enters fontRanges(), which loads the local() source and resolves loaded.
  4. Inside the getter, loop over 40 sizes from 11px to 50px. Each call re-enters fontRanges() on the same family object, inserts a new key, and eventually forces a rehash before the outer frame resumes.

Timing is deterministic. The then getter runs exactly inside the window where the dangling reference is live, and distinct sizes force the rehash on demand. The immediate effect is a UAF read and write on one FontRanges slot inside a freed bucket array. The writes come from appendRange and shrinkToFit mutating the stale Vector's buffer pointer and size fields, and from storing Ref<FontAccessor> entries. The reads come from the copy-out at return ranges, which copies the Vector and refs every FontAccessor pointer it finds.

Escalation hinges on what reclaims the freed array between the rehash and the outer frame's resumption. The getter's script window sits in that gap, so an attacker can in principle allocate there. If a same-size allocation with attacker-influenced contents occupied the slot, the outer frame's operations would act on attacker-shaped Vector metadata, which could turn the stale write into a controlled-pointer write or a fake refcounted-object access. Whether allocator segregation for HashMap tables permits that reclaim is the invariant an exploit would need. Without a reclaim, the observed effect would be a crash or silent corruption of freed allocator memory. The path runs in the WebContent process, both for document layout and for OffscreenCanvas measurement, and does not cross the sandbox on its own.

This vulnerability weakens renderer-process memory safety. WebCore assumes that a native frame holding interior references into a container is never interleaved with author script that can mutate that container, and promise resolution inside font loading broke that assumption for m_cache. A page could drive the renderer into reading and writing a freed heap allocation at a script-chosen moment.

Insight. The less obvious lesson is the re-entry vector. iterateClients in CSSFontFace.cpp wraps client notification in a ScriptDisallowedScope, which asserts in debug builds if script runs inside it. The promise resolution that reaches the then getter therefore most likely sits on a separate step of the same load-completion flow, outside that scope. Script-disallowed guards protect only the region they wrap. An adjacent unguarded step, such as resolving a DOMPromise with a wrapper object, reintroduces synchronous script through the thenable lookup. A correctness side effect also remains: fontLoaded() clears m_cache, but the post-fix fontRanges() still inserts its locally built result after the loop, so a reentrant fontLoaded() could leave a stale but memory-safe entry.