[5] Editor/WebPageMac UAF: key-command and IME text borrowed by `const&` across event dispatch
High on bug class and reach: a renderer heap UAF on a variable-size allocation, steered by event handlers on any editable element. It most likely needs real keyboard or IME input to start, and the escalation question (which script-reachable operation actually frees the storage) is still open.
Native editing code in a browser often hands borrowed views of data to routines that then fire DOM events, and every event is a point where page JavaScript runs synchronously before control returns. On macOS, AppKit turns each keystroke into a list of KeypressCommands (one editing selector such as insertText: plus its text), stored on the refcounted DOM KeyboardEvent in a by-value Vector member, m_keypressCommands. According to the commit message, WebPage::executeKeypressCommandsInternal, the WebContent-side loop that runs those commands, receives that vector by const&, while Editor::setComposition, which confirms or cancels an IME composition, receives its text by const& from caller-owned heap storage (likely a KeypressCommand's text), and both assume the borrowed data stays alive while they run.
The angle: if a page's event handlers can release the m_keypressCommands storage while executeKeypressCommandsInternal is still iterating it, ordinary typing into an editable element becomes a renderer use-after-free on a heap buffer whose size tracks the command list.
use-after-free of m_keypressCommands in Editor::setComposition. Copy both
textinsetCompositionandcommandsinexecuteKeypressCommandsInternalonto the stack; these point to heap memory when invoked from their respective call sites, but the logic in both of these methods can fire events and trigger JavaScript (and thus, have the potential to corrupt the heap).
Source/WebKit/WebProcess/WebPage/mac/WebPageMac.mm
Source/WebCore/editing/Editor.cpp
Patch Details
There are two parallel changes, one per file. Editor::setComposition(const String&, SetCompositionMode), the overload behind confirmComposition/cancelComposition, renames its parameter to passedText and immediately copies it into auto text before creating SetCompositionScope or running any composition logic. WebPage::executeKeypressCommandsInternal renames its parameter to passedCommands and copies it into a local auto commands after the frame null-check. The existing Borrow borrowedCommands = commands; and the execution loop now operate on that local. No guards or control flow are added. Two borrowed views of caller-owned heap storage become owned stack values that hold their own references to the underlying StringImpl and Vector buffer. There are no test changes.
Holding a borrowed reference into mutable container storage across a call that can re-enter script and mutate or free that container.
Background
Where this lives.
WebKit's UI process receives key input and interprets it with AppKit, then forwards the resulting commands with the key event to the sandboxed WebContent process that hosts the page. There, WebEditorClient::handleKeyboardEvent and didDispatchInputMethodKeydown forward the KeyboardEvent to WebPage::handleEditingKeyboardEvent, which, per the commit message, feeds the event's command vector into executeKeypressCommandsInternal against the focused frame's Editor.
KeypressCommand and KeyboardEvent.
A KeypressCommand records one AppKit editing selector, such as insertText: or a movement or deletion command, plus its associated text. On Cocoa, KeyboardEvent stores these commands in a by-value member m_keypressCommands, which is exposed through a mutable Vector<KeypressCommand>& keypressCommands() accessor.
Editor::setComposition.
With ConfirmComposition or CancelComposition, it finalizes or cancels an active IME composition. Doing so dispatches composition, beforeinput/input, and text-insertion events to the focused editable element.
WTF::String and WTF::Vector.
String is a smart-pointer wrapper around a refcounted StringImpl. Copying a String takes a new reference, while binding a const String& takes none and aliases the caller's object. Vector<T> owns a heap element buffer that clear(), assignment, or growth past capacity frees or reallocates. A const Vector& parameter aliases the caller's vector object.
Re-entrancy. A re-entrancy point is where native code dispatches a DOM event, so page JavaScript runs synchronously and can mutate C++-visible state before control returns.
Analysis
This is a use-after-free through a dangling const& across JavaScript re-entrancy.
executeKeypressCommandsInternal(const Vector& commands) page JS
─────────────────────────────────────────────────────── ───────
commands ── alias ──► event->m_keypressCommands (heap buf)
for each cmd in commands:
editor runs cmd ── beforeinput/input/composition ──────► handler
m_keypressCommands
cleared / grown?
buffer freed
◄────────────────────────────────────── returns
next cmd = commands[i+1] <- read from freed buffer
Before the fix, both functions took const& parameters that aliased storage owned by someone else, and both then ran logic that dispatches DOM events. The commit message identifies the aliased storage as m_keypressCommands, and states that handleEditingKeyboardEvent is the hop that feeds the event's command vector into executeKeypressCommandsInternal. In the diagram, the loop executes editing commands that fire beforeinput/input/composition/textInput events. If script-reachable work in that window clears, reassigns, or grows m_keypressCommands, the buffer that commands points into is freed or reallocated, and the loop's next element access reads freed memory.
Editor::setComposition has the same shape one level deeper. Per the commit message, its text argument points to heap memory at the call site, likely the text field of a KeypressCommand in the same vector. The function fires composition and input events before it finishes consuming text. If that backing storage is freed in the window, text becomes a dangling reference to a String slot in a freed buffer, and even if only the String is reassigned, its StringImpl may lose its last reference. The copies close both cases: Vector's copy constructor copies each KeypressCommand, and String's copy refs its StringImpl, so the locals survive whatever script does to the originals.
The path is reachable from web content through event handlers on an editable element, but it likely needs real user keyboard or IME input to start: script-created KeyboardEvents do not appear to carry AppKit KeypressCommands, since those are populated from the UI process's AppKit interpretation of a real keystroke. The mutable accessor on KeyboardEvent establishes that the storage can be changed; which script-reachable operation performs that mutation, and whether it frees rather than merely reassigns, is the open question that sets the primitive's strength. Depending on the mutation, the result would be either a stale read of a freed Vector<KeypressCommand> backing buffer, sized by command count times sizeof(KeypressCommand), or a dangling String whose StringImpl lost its last reference, sized by string length. If the freed Vector buffer were reclaimed, its String slots would be reinterpreted as StringImpl* pointers. That could turn the stale read into a fake-StringImpl access, a candidate for an info-leak or refcount-decrement-write primitive bounded by how the editing path consumes the text and command name. Without reclamation, the observed effect would be a crash. The executeKeypressCommandsInternal path is macOS-only, while setComposition is shared WebCore code, and neither crosses the WebContent sandbox on its own.
This vulnerability weakens memory safety in the WebContent process. Native editing code assumed the command list and composition text it was handed would stay valid for the whole call, and nothing enforced that across the event handlers the same code dispatches. If a page can steer script in those handlers into releasing the backing storage, the renderer would consume freed Vector/StringImpl memory. That is renderer-plane memory corruption whose ceiling depends on whether the freed slot can be reclaimed with controlled data.
Insight. The fix is a defensive copy, not a lifetime guard, which is WebKit's standard remedy when a function both borrows caller-owned data and dispatches events. The telling detail is that executeKeypressCommandsInternal already wrapped the vector in Borrow, yet the borrow was taken from a reference the function did not own. If Borrow tracks mutation only through its own handle, as a borrow-tracking helper typically would, it cannot see the event's owner mutating the storage through another alias. The hazard also propagates: every callee that receives a const& derived from m_keypressCommands, like setComposition, inherits it, and nothing in such signatures signals that event dispatch happens inside.
Audit directions
const¶meters aliasing a script-reachable object's member container, held across event dispatch. A borrowed view into a refcounted DOM object's storage must not outlive a point where script runs. Narrow: grepWebCore/editingandWebKit/WebProcess/WebPagefor functions takingconst String&orconst Vector<...>&that calldispatchEvent,dispatchBeforeInputEvent,TypingCommand::insertText, orCompositeEditCommand::apply; start with the iOS/GTK/WPE siblings ofexecuteKeypressCommandsInternaland the otherEditor::setCompositionoverload. Match tell: the parameter is used after the dispatch call, and callers pass a field of anEvent,Element, orDocument. Wider: the same shape withstd::span,StringView, and range-for over a memberVectorwhose loop body can run script; match tell: the iteration source is a member of an object JS can reach. Widest: any engine whose native code borrows embedder-owned collections while invoking user callbacks, including Blink editing code withbase::span/StringViewand Rust&[T]borrows across FFI callbacks. Match tell: a non-owning view outlives a callback boundary.- Mutable-reference accessors on refcounted DOM events or nodes. Audit every accessor returning
T&to a member of such an object, starting withKeyboardEvent::keypressCommands(). Enumerate its writers and check whether any can run while a borrowed view of the same member is live higher on the stack. Wider: apply the same check toPlatformKeyboardEvent,InputEventdata transfers, andCompositionEventdata, which travel the same editing pipeline. Widest: a mutable-reference getter on a shared object turns everyconst&reader into a potential dangling-reference site; ask whether the class has a mutation path reachable from a callback. Match tell: both aconst T&and aT&getter exist, and the non-const one has callers outside construction. Borrowapplied to references the enclosing function does not own. Grep forBorrowdeclarations whose initializer is a function parameter or the result of a member accessor rather than a local value. Wider: review other lifetime annotations, such asLIFETIME_BOUND,CheckedRef, andCanMakeCheckedPtr, for places where the annotation sits on a view while the owner stays mutable through another alias. Widest: ifBorrowtracks mutation only through its own handle, it shares the general limit of borrow-tracking helpers in non-Rust codebases, which cannot see mutation through other aliases; confirmBorrow's actual semantics before relying on it. In code review, aBorrow x = param;whereparamis aconst&argument is the tell.