← All reports

[1] WGSL `__ArrayLength` slots assigned in declaration order swap buffer bounds

HighWebGPU WGSL compilerOOB

Two WebGPU layouts looked identical. They disagreed on which buffer was which size.

a2572ee

High. The shader's bound for one storage buffer came from a sibling buffer's length word, so script picks both the index and the value of a write past a 16-byte allocation. The reach stays in GPU-visible memory; turning it into CPU-side compromise needs an isolation failure this change does not establish.

A shader compiler and the host-side resource layout code have to agree on an implicit binary layout, and a disagreement over which slot holds a buffer's size turns a bounds check into a check against the wrong buffer. WebKit's WGSL compiler, the translator from WebGPU's shading language to Metal, packs each bind group into an argument buffer (a struct of pointers and metadata at numeric indices) and adds a hidden __<name>_ArrayLength word, the per-buffer size that arrayLength() and runtime-array bounds handling rely on, for every runtime-sized array. The expectation is that two bind-group layouts that look identical to script also place those hidden words identically.

The angle: a page using WebGPU can get a small storage buffer bounded by a larger sibling's length and perform script-indexed, script-valued reads and writes past it in the GPU process's GPU-visible memory.

The commit message is terse: ensure buffer lengths are sorted by id to avoid clashes (rdar://185201177, webkit.org/b/323234). The regression test runs with Metal shader validation enabled.

Source/WebGPU/WGSL/GlobalVariableRewriter.cpp

std::optional<Error> RewriteGlobalVariables::collectGlobals()
{
- Vector<std::tuple<AST::Variable*, unsigned>> bufferLengths;
+ Vector<std::tuple<AST::Variable*, unsigned, unsigned>> bufferLengths;
...
packResource(*globalVar);
 
- bufferLengths.append({ globalVar, resource->group });
+ bufferLengths.append({ globalVar, resource->group, resource->binding });
}
}
 
for (auto& [_, vector] : m_groupBindingMap)
std::ranges::sort(vector, { }, &std::pair<unsigned, String>::first);
 
+ // Ensure buffer lengths are sorted by id to avoid clashes
+ std::ranges::sort(bufferLengths, { }, [](const auto& bufferLength) {
+ return std::pair { std::get<1>(bufferLength), std::get<2>(bufferLength) };
+ });
+
if (!bufferLengths.isEmpty()) {
- for (const auto& [variable, group] : bufferLengths) {
+ for (const auto& [variable, group, webBinding] : bufferLengths) {
auto name = AST::Identifier::make(makeString("__"_s, variable->name(), "_ArrayLength"_s));
...

LayoutTests/fast/webgpu/regression/repro_185201177.html

+@group(0) @binding(0) var<storage, read_write> a0 : array<u32>;
+@group(0) @binding(1) var<storage, read_write> a1 : array<u32>;
+@group(1) @binding(1) var<storage, read_write> b1 : array<u32>;
+@group(1) @binding(0) var<storage, read_write> b0 : array<u32>;
...
+ b1[a1[0]] = a1[1];
...
+ // A bind group encoded against group 0's layout, set at group index 1. Both layouts were
+ // auto-generated by the same pipeline and have identical entries ...
+ const crossBindGroup = device.createBindGroup({
+ layout: pipeline.getBindGroupLayout(0),
+ entries: [
+ { binding: 0, resource: { buffer: large } },
+ { binding: 1, resource: { buffer: small } },

RewriteGlobalVariables::collectGlobals() makes three changes. The bufferLengths tuple widens from (variable, group) to (variable, group, binding) by also recording resource->binding. A new std::ranges::sort orders bufferLengths by std::pair { group, binding }, placed right after the existing per-group sort of m_groupBindingMap. The emission loop that synthesises one __<name>_ArrayLength variable per runtime-sized buffer updates its structured binding to [variable, group, webBinding]. The net effect: hidden length variables are now generated in (group, @binding) order rather than module-scope source order. The regression test declares group 1's bindings in reversed order and binds a group-0 bind group at index 1.

Hidden bounds metadata assigned slots in source declaration order, so layouts equal on their visible entries can disagree on which slot holds which buffer's length.

WGSL resources. WGSL resources are module-scope variables annotated @group(N) @binding(M). A runtime-sized array (array<T> with no length) has a size known only once a buffer is bound, and arrayLength(&x) returns it at run time. WebKit's compiler synthesises a hidden per-buffer word, __<var>_ArrayLength, which generated shader code reads whenever it needs that size.

Argument buffers on Metal. Each WebGPU bind group becomes a Metal argument buffer: a struct of pointers, textures, samplers and the synthetic length words, each at a numeric index. The host-side BindGroup encodes values into those indices according to its BindGroupLayout; the BufferBindingType::ArrayLength entries in BindGroupLayout.mm are the host-side counterparts of the length words.

Auto layouts and layout reuse. With layout: 'auto', a pipeline derives each group's BindGroupLayout from the shader via compiler reflection. WebGPU allows a bind group created from one layout to be used with another layout whose entries are equivalent, so the same layout description can serve multiple group slots.

collectGlobals. RewriteGlobalVariables::collectGlobals is the pass entry point. It walks the module's declarations, records each resource's group and binding in m_groupBindingMap, packs resource types, and creates the synthetic length variables. It runs in the GPU process at pipeline creation.

This is an OOB read/write on GPU device memory caused by a slot-assignment mismatch, not a missing check.

  Group 0 source order           Group 1 source order
  @binding(0) a0                 @binding(1) b1
  @binding(1) a1                 @binding(0) b0
  len slots: [a0, a1]            len slots: [b1, b0]

  crossBindGroup (group 0 layout) set at index 1:
    slot 0 = len(large, 1 MiB)  ──►  shader reads as len(b1)
    slot 1 = len(small, 16 B)   ──►  shader reads as len(b0)
  b1 is the 16-byte buffer, bounded by 1 MiB
    └─► b1[a1[0] = 0x800] = a1[1]   write lands past the allocation

Before the fix, collectGlobals appended to bufferLengths in the order declarations appeared and never sorted it, while user-visible entries in m_groupBindingMap were sorted by @binding. The order dependence follows from the fix reordering only this vector, and from the test's description of slots "chosen by @binding, not by declaration order"; the exact emission step that turns vector order into argument-buffer member indices is the one link that is inferred rather than demonstrated.

Each auto-generated layout should be internally consistent with its own group's declaration order, which would explain why the test's positive control (group 1 bound with getBindGroupLayout(1)) behaves. The broken invariant is narrower: two auto layouts with identical visible {binding, visibility, type} entries could place their length words at different indices. In the diagram, group 0 puts binding 0's length first and group 1 puts binding 1's first, so a group-0 bind group accepted at index 1 hands b1 the 1 MiB buffer's element count.

The trigger is fully web-reachable: a WGSL module with reversed declaration order in one group, a layout: 'auto' pipeline, and a bind group from one group's layout reused at the other index. The test places a 1 MiB buffer at binding 0 and a 16-byte buffer at binding 1; index 0x800 from a1[0] is then within the believed bound, and the store writes about 8192 bytes past the small allocation per the test comment. The same swap lets reads through, and arrayLength() itself reports the other binding's size. Whether the host-side compatibility check accepted the cross-group bind group depends on whether it compares hidden argument-buffer indices; the test treats rejection as a safe outcome, so acceptance is the conditional precondition for the write.

The primitive operates on GPU device memory relative to an attacker-chosen buffer's base, with attacker-supplied index and value, bounded by the swapped-in sibling's length. The open escalation question is what sits beyond a small storage buffer: buffers or textures from other WebGPU devices, or internal staging and indirect-argument buffers. If those could be placed reliably near an attacker-sized buffer, this would offer a data-corruption or disclosure primitive against them; reaching CPU-side GPU-process state would depend on Metal's GPU address-space isolation and the unified-memory mapping, which the supplied context does not establish. The shader runs on behalf of the GPU process, and this bug alone shows no path to CPU code execution or out of that sandbox.

This vulnerability weakens the WebGPU memory-safety boundary, which WebKit enforces in the shader compiler. WebGPU requires every shader access to a storage buffer to stay inside that buffer's bound range, and the per-binding __ArrayLength words are part of how that confinement is realised for runtime-sized arrays. Before the fix, web content could arrange for one buffer's accesses to be bounded by another buffer's length and perform attacker-indexed, attacker-valued reads and writes past a small storage buffer, which could corrupt or disclose other GPU-resident resources owned by the GPU process.

Insight: WebGPU safety on Metal depends on the compiler and the host-side layout code agreeing exactly on an implicit ABI — which index holds which pointer and which length. Each auto layout was self-consistent; the failure only appeared when one layout's encoding was used where a visibly identical one was expected. The fix removes the dependency by giving hidden slots a canonical (group, binding) order, and the general rule is that hidden per-binding metadata must either be a pure function of (group, binding) or be part of the layout-compatibility key.