WebGL getUniform returns a non-canonicalized float NaN as a forged JS::Value (type confusion)
WebGL's getUniform returned an attacker-supplied 32-bit float NaN to JavaScript without canonicalising it, so a crafted NaN bit pattern was reinterpreted as a forged JS::Value with an attacker-chosen garbage-collector tag and pointer. A web page could therefore obtain a fake-object type-confusion primitive, and the reporter demonstrated a chain to code execution in the content process with the sandbox disabled.
- Fixed in 28 hours
- Paid in 51 days
- Disclosed after 28 hours
Created attachment 9600131 asan.log
Description
WebGLRenderingContext.getUniform() returns an attacker-controlled GLfloat to JavaScript without canonicalizing NaN. Because the 64-bit JS::Value is NaN-boxed (punbox64), a crafted f32 NaN whose widened f64 bit pattern carries a non-Double tag is delivered to web script as a forged JS::Value of an attacker-chosen GC tag (Object/String/Symbol/BigInt) whose pointer field is attacker-influenced — a fakeobj / type-confusion primitive reachable from ordinary web content, no special API, no user interaction, default configuration.
Affected
- Firefox 153.0a1 (Nightly, mozilla-central rev
f19dd05bb08e, from-source) — crash + code-exec chain confirmed. Release 150.0.3 also confirmed. Vulnerable code is present in currentmozilla-centraland the current release (151.0.4). In scope; not EOL. - All releases Firefox 15.0 → current (see Regression).
- Reachable in the default configuration; no special settings or user interaction required.
Steps to reproduce
Crash PoC (attached poc.html) — upload a negative-NaN f32 as a float uniform, then read it back:
gl.uniform1fv(loc, new Float32Array(Uint32Array.of(0xfff00001).buffer));
var v = gl.getUniform(prog, loc); // delivers a forged ObjectValue(pointer = 0x20000000)
getUniform's own GC read-barrier dereferences the forged pointer as it exposes the return value to JS → content-process crash. Run headless:
xvfb-run -a firefox -no-remote -profile /tmp/pocprof 'file:///path/to/poc.html'
# parent: "process <pid> exited on signal 11"
Minimal non-crashing testcase (suitable for the test suite): upload an f32 NaN, getUniform, store the result into a Float64Array, read the u64. On affected builds it is a non-canonical NaN (!= 0x7FF8000000000000) or a non-number typeof; on a fixed build it is the canonical NaN.
ASAN stacktrace (clean from-source build, rev f19dd05)
==ERROR: AddressSanitizer: SEGV on unknown address 0x000020000000 (READ)
#0 js::gc::detail::ChunkPtrHasStoreBuffer(void const*) js/HeapAPI.h:720
#1 js::gc::IsInsideNursery(js::gc::Cell const*) js/HeapAPI.h:727
#2 js::gc::ExposeGCThingToActiveJS(JS::GCCellPtr) js/HeapAPI.h:895
#3 JS::ExposeValueToActiveJS(JS::Value const&) js/Value.h:1074
#4 mozilla::dom::WebGLRenderingContext_Binding::getUniform(...) dom/bindings/WebGLRenderingContextBinding.cpp:18607
Root cause
ClientWebGLContext::GetUniform (dom/canvas/ClientWebGLContext.cpp, LOCAL_GL_FLOAT case) boxes the scalar float via dom::ToJSValue(JSContext*, float, …) (dom/bindings/ToJSValue.h), which stores it through the raw JS::Value::setNumber(float) → setDouble(double(f)) — no CanonicalizeNaN. f32→f64 widening preserves the NaN sign and payload, so a crafted f32 NaN yields an f64 whose top 17 bits form a non-Double punbox64 tag (JSVAL_TAG_OBJECT = 0x1FFFC, etc.). The sibling ToJSValue(double) overload does canonicalize (routes through JS_NumberValue); the float overload is the only floating-point ingress that does not.
Attacker control of the forged value
Decoding the widened (sign-set) f64 as punbox64:
- pointer =
f32_mantissa[17:0] << 29→ 18 attacker-controlled bits; low 29 forced to 0 (512 MB-aligned, attacker-chosen across the 47-bit space). - tag =
f32_mantissa[22:18](0x1C→OBJECT; others forge STRING/SYMBOL/BIGINT).
Empirically the forged pointer tracks the upload (0x7ff00001→0x20000000, 0x7ff00002→0x40000000, …). A negative-NaN upload forges the object directly, with no JS arithmetic.
Impact (factual; committee to rate)
A web-reachable fakeobj/type-confusion primitive: forge a JS::Value of a chosen GC tag pointing at a chosen 512 MB-aligned address. We demonstrated the full chain end-to-end to code execution — heap-spray → forged-string arbitrary read → self-derived libxul base (Mach-O/ELF header scan via the bug's own read) → fake typed-array arbitrary R/W → forged-callable single-gadget call → process spawn — i.e. demonstrated attacker control of PC and memory (attached). The code-exec demonstration ran with the content sandbox disabled to permit the final spawn; this establishes content-process code execution, not a sandbox escape (a real chain would pair a separate sandbox-escape bug).
Regression
Introduced in Firefox 15.0 (2012-08-28) by Bug 745897 ("Add the new DOM bindings APIs to WebGLContext", commit 83a6633bd95c, landed mozilla-central 2012-05-04): the WebIDL-bindings migration changed getUniform's scalar-float return from nsIWritableVariant::SetAsFloat(fv[0]) (which produced a canonical JS number) to the raw JS::DoubleValue(fv[0]). Runtime-bisected: 14.0b12 not triggerable (canonical number returned); 15.0b1 triggerable (SIGSEGV). Firefox 4–14 canonicalize.
Prior art / reachability
The same NaN-boxing type-confusion class was addressed in Bug 1776658 (2022; fixed in Firefox 104), which routed a set of setNumber call sites — including the sibling ToJSValue(double) overload — through JS_NumberValue. The ToJSValue(float) overload was not among them and still boxes via the raw JS::Value::setNumber(float). WebGLRenderingContext.getUniform reaches that overload with an attacker-controlled binary float — uploaded via uniform1fv(loc, Float32Array) (ArrayBuffer-backed) and read back — which this report demonstrates to content-process code execution (with the content sandbox disabled; see Impact). The proposed fix applies the same change to the float overload.
Proposed fix
Align ToJSValue(float) with ToJSValue(double) (canonicalize via JS_NumberValue) — see attached …fix.patch:
- aValue.setNumber(aArgument);
+ aValue.set(JS_NumberValue(aArgument));
Keeps the int32 fast path, canonicalizes NaN, leaves non-NaN floats unchanged (a float NaN is spec-equivalent to a JS NaN). Closes getUniform and the other scalar-float getters that flow through ToJSValue — texParameterf/getTexParameter, samplerParameterf/getSamplerParameter, polygonOffset/getParameter(POLYGON_OFFSET_*). Defense-in-depth alternative: canonicalize in JS::Value::setNumber(float) itself.
Credit
Discovered by XBOW
Triaged by Brendan Dolan-Gavitt
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 float overload of ToJSValue boxes the value with the raw JS::Value::setNumber(float) instead of canonicalising NaN through JS_NumberValue, so a crafted f32 NaN widens to an f64 whose tag bits forge a non-Double JS::Value.
Impact
A web-reachable fake-object primitive giving attacker control of a forged value's tag and pointer, which the reporter chained to content-process code execution with the content sandbox disabled; this is not a sandbox escape.
What the attacker needed
Victim visits a malicious web page; default browser configuration; no user interaction
The fix
The float overload of ToJSValue should canonicalise NaN via JS_NumberValue, matching the double overload; the text proposes this patch but does not say it was applied.
How it was found
- Upload a negative-NaN 32-bit float as a float uniform through uniform1fv with an ArrayBuffer-backed Float32Array.
- Read the uniform back with getUniform, which returns the forged value to script.
- The GC read-barrier dereferences the forged pointer, crashing the content process.
- For a non-crashing check, store the result in a Float64Array and read the 64-bit value; affected builds give a non-canonical NaN or a non-number type.
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.
JavaScript engines store every value in a fixed-size box and use spare bits in the pattern to mark what kind of value it is. A special 'not a number' float (NaN) has many valid bit patterns, and the browser passed one of them straight through, so the engine read the float's bits as if they described an object at a memory address the page chose. That lets a malicious page trick the browser into treating arbitrary memory as a JavaScript object.
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.