[3] `RenderMenuList::updateOptionsWidth()` measures text through a borrowed pointer into a freed temporary
A styled multi-select makes layout measure text that no longer exists.
Medium, and closer to the floor than the bug class suggests. The freed read covers a handful of characters sized by a count label, its only output is a float width, and no script runs inside the window. A real heap UAF, but a read-only one.
Use-after-free bugs in C++ string handling often come down to a view, meaning a non-owning pointer-and-length pair, outliving the string object that owns the characters. WebCore measures text for layout by building a TextRun with the RenderBlock::constructTextRun() family of helpers. A TextRun is a run of characters plus layout attributes; font code walks it to sum glyph widths. Callers expect that a TextRun built from a String keeps its characters alive for as long as the run is in use.
The angle: a page with a styled multi-select can make layout read a few bytes of freed heap memory. The realistic ceiling is a crash or a low-bandwidth width-based leak, never a write.
The commit message is titled "REGRESSION(323720@main)". RenderMenuList::updateOptionsWidth() built a TextRun from the temporary String returned by applyTextTransform(). RenderBlock::constructTextRun(const String&) went through StringView and toStringWithoutCopying(), so the TextRun did not own its characters, and the temporary was destroyed before font->width(run) read them. The fix keeps the transformed string in a local. It also makes the String and AtomString overloads of constructTextRun() retain the string instead of creating a non-owning StringImpl, which makes passing a temporary safe and avoids allocating a StringImpl per call.
Source/WebCore/rendering/RenderMenuList.cpp
Source/WebCore/rendering/RenderBlock.cpp
LayoutTests/fast/forms/select/select-multiple-text-transform-crash.html
Patch Details
In the countTextWidth lambda inside RenderMenuList::updateOptionsWidth(), the inline applyTextTransform(style, text) argument is replaced by a named local, transformed, which lives until font->width(run) returns. This matches the measure lambda in the same function, which already used a local.
In RenderBlock.cpp, the body of the StringView overload moves into a file-static helper, constructTextRunFromString(String&&, ...). The helper moves the string through updateSecurityDiscCharacters() into the TextRun. The StringView overload still calls toStringWithoutCopying(), now with a comment that the caller must keep the characters alive. The const String& overload no longer routes through StringView. It copies the String, which retains its StringImpl, and maps a null string to emptyString(). The const AtomString& overload forwards to the String overload. The old comment claiming "TextRun owns its text string" is deleted.
The new layout test has two <select multiple size=1> elements, one with text-transform: uppercase and one with full-width. It forces layout and runs with HTMLEnhancedSelectMultipleDropdownBoxEnabled=true.
A non-owning view over a temporary's storage is laundered into an apparently owning object that outlives the temporary's full-expression, leaving the object reading freed backing memory.
Background
String and StringImpl.
WTF's String is a reference-counted handle to a StringImpl. For ordinary strings, the StringImpl allocation also holds the character data inline, so freeing the impl frees the characters.
StringView and borrowing.
A StringView is a non-owning (pointer, length) view over 8-bit or 16-bit characters and does not retain the buffer it points into. StringView::toStringWithoutCopying() returns a String backed by a StringImpl that points at the view's existing characters instead of copying them; the original owner must outlive that String. WebKit offers this to avoid a copy on hot measurement paths, at the cost of an unstated lifetime contract.
C++ temporary lifetime. A temporary created as a function argument, such as the return value of a call, lives until the end of the enclosing full-expression, typically the end of the statement. A named local lives until the end of its scope.
TextRun and constructTextRun.
TextRun stores a String plus layout attributes (expansion, direction). FontCascade::width(const TextRun&) walks the run's characters to sum glyph advances. RenderBlock::constructTextRun is a set of static overloads (taking StringView, String, AtomString or RenderText) that build a TextRun and apply bidi direction and -webkit-text-security disc substitution via updateSecurityDiscCharacters.
text-transform.
This CSS property (uppercase, lowercase, capitalize, full-width, ...) is applied by applyTextTransform. When the property changes the text, the function returns a new string.
RenderMenuList::updateOptionsWidth().
This function computes the intrinsic width of a <select> control by measuring each option label. For multiple selects it also measures the localized count labels from htmlSelectMultipleItems(count) for every possible count. It runs from styleDidChange when the font changes and from updateFromElement. Results land in m_optionsWidth and m_widthWithoutOptions, which feed computeIntrinsicLogicalWidths().
Analysis
This is a heap use-after-free read, caused by a borrowed string buffer outliving its owner by one statement.
auto run = constructTextRun(applyTextTransform(style, text), style);
| 1. applyTextTransform -> temporary String T (fresh buffer, sole owner)
| 2. constructTextRun(const String&) -> StringView(T)
| -> toStringWithoutCopying(): run's String borrows T's characters
| 3. end of full-expression: ~T -> StringImpl and inline chars freed
v
return font->width(run);
| 4. FontCascade::width walks run's characters <- read of freed heap
Step 2 is the trap. TextRun holds a String member, so it looked owning, but toStringWithoutCopying() produced a String whose characters still belonged to T. At step 3, C++ destroys the prvalue at the end of the declaration statement. Step 4 then reads through the non-owning StringImpl, using the length captured at step 2.
The bug fires only when the temporary is the sole owner of its buffer. With uppercase or full-width, applyTextTransform returns a newly allocated string, and step 3 frees it. Without a transform, the result can share its StringImpl with the still-live local text, so the buffer survives. That is why the regression test needs text-transform. The commit message attributes the regression to 323720@main; the measure lambda in the same function already bound its result to a local, and only countTextWidth lacked one.
The ASan-style title, plus a trigger that combines a form control with a CSS property and a forced layout, points to DOM/CSS grammar fuzzing under ASan. Because the bug is a fresh regression in a feature-flagged path, auditing that change would also have found it.
Reachability takes plain HTML and CSS: a <select multiple> styled with a text-transform that produces a fresh string, plus anything that forces layout. Whether this works in shipping configurations depends on the HTMLEnhancedSelectMultipleDropdownBoxEnabled setting that the test enables. The read length is the transformed count label's length. That label passes through select->buttonLabelText() when countReplacesSelectedContent is set, so it is not purely browser-generated.
The TextRun is left with a dangling character pointer into a freed StringImpl allocation. The observable effect is an ASan-detectable heap read. To go further, an attacker would have to influence what lands in the freed slot between the end of the constructTextRun statement and font->width(run), a window with no script execution. If the slot were reclaimed with attacker-influenced data, those bytes would be interpreted as characters for glyph-advance lookup. The computed width, surfaced through m_optionsWidth and the select's layout box, might then encode them: a low-bandwidth information leak. Even then the bug would stay a read-only primitive with no write on this path, and it could not stand alone as a step toward code execution. Under hardened or debug allocators the realistic primary effect is a crash.
This vulnerability weakens memory safety in the WebContent renderer. It breaks the invariant that a TextRun's characters stay alive for the run's whole lifetime; constructTextRun(const String&) had silently turned that guarantee into an unstated requirement on every caller. The consequence is a read of freed heap memory reachable from ordinary HTML and CSS. The best an attacker could gain is a crash or a weak width-based leak of freed bytes. The bug does not cross the sandbox.
Insight: The deleted comment, "TextRun owns its text string. Its member is a String, not a StringView", is exactly the misunderstanding that created the bug: the type said "owning" while toStringWithoutCopying() made it a borrow. The fix closes the trap at the API level, not just at the call site. The String and AtomString overloads now retain the StringImpl, so passing a temporary is safe by construction. Only the StringView overload keeps borrowing semantics, now documented. In the common case without -webkit-text-security, it also replaces a per-call StringImpl allocation with a refcount bump. The two nearly identical lambdas in updateOptionsWidth() show how a prvalue argument hides a lifetime boundary: only the one that used a named local was safe.
Audit directions
- Borrowing string handle wrapped in an object that outlives the source's full-expression. Borrowing handles include
toStringWithoutCopying(),StringImpl::createWithoutCopying, and aStringViewstored in a member. The invariant: a borrowed buffer may not escape the statement that produced its owner unless the owner is a named local or member. - Narrow: grep WebCore for
toStringWithoutCopying()andcreateWithoutCopyingcall sites, especially inrendering/,platform/graphics/andlayout/. For each, check whether the resultingStringis stored in a struct (TextRun,TextBreakIteratorinputs, cache keys) that is used after the source temporary dies. - Wider: the same shape appears with
std::spanorstd::string_viewover a returnedVectororstd::string, and withCString::data()on a temporary. - Widest: any codebase with view types and implicit owner-to-view conversion has this class: C++
std::string_viewfromstd::stringprvalues, Chromium'sbase::StringPiece, LLVM'sStringRef. - Code-review tell: a view-producing conversion applied to a prvalue, or a call returning
Stringby value used directly as an argument, whose result is bound to a variable that outlives the statement. - Remaining callers of
RenderBlock::constructTextRun(StringView, ...). The fix made only theStringandAtomStringoverloads owning, so the borrowing contract remains forStringViewcallers. Check each call site inRenderMenuList,RenderListBox,RenderFileUploadControl,RenderTextControl*, and the SVG and MathML text measurement code. Confirm that the view's backing string is a named local or a member that outlives the returnedTextRun. Code-review tell: aStringViewbuilt from aStringtemporary, fromsubstring(), or from a by-value accessor, inside the same statement that constructs the run. - Regressions introduced by duplicating a code path, where one copy is correct and the other is not. Here,
measureheldtransformedin a local butcountTextWidthdid not. Review recently added feature-flagged form-control rendering paths, such as the enhanced<select multiple>drop-down andbuttonLabelText/selectedcontenthandling, for sibling lambdas or helpers that differ only in whether an intermediate string or object is bound to a named local. Clang's-Wdanglingand[[clang::lifetimebound]]annotations exist to catch exactly this asymmetry in any C++ codebase. Code-review tell: two near-identical blocks in one function, where one bindsauto x = f(...)and the other inlinesf(...)into a borrowing call.