← All reports

[3] drawIndexedIndirect clamp verdict survives an index-buffer rewrite

HighWebGPU Metal backendOOB

WebGPU proved a draw safe once, then kept trusting the proof after the indices changed.

411a4c9

High, and deterministic. A cached "safe to skip the clamp" verdict ignored the one buffer its result was computed from, so script-chosen indices drive vertex fetches past the bound buffer on the GPU. The ceiling is a read-only GPU-memory disclosure, gated on driver robustness behaviour and what neighbours the vertex buffer.

Indexed draws on Metal fetch per-vertex data at index * stride without bounds checking, so a WebGPU implementation has to validate index values itself before the GPU runs. For drawIndexedIndirect — a draw whose count and offsets live in a GPU-side args buffer instead of the CPU call — WebKit runs a clamp pass that scans the index buffer and zeroes the draw count if any index is out of range. Because that pass costs GPU time, its verdict is cached, and the expectation is that any change to an input the verdict read invalidates the cache.

The angle: a page can validate a draw once with in-range indices, rewrite only the index buffer, and have the next identical draw skip the clamp, so script-chosen indices drive GPU vertex fetches out of bounds.

The commit message (rdar://182324564): a stale cache leads to drawIndexedIndirect performing an OOB read on the GPU cores.

LayoutTests/fast/webgpu/regression/draw-indexed-indirect-stale-clamp-after-index-write.html

+ // 24 bytes / arrayStride 8 => minVertexCount 3, so index values >= 3 are out of bounds.
+ let vertexData = new Float32Array([-1, -3, -1, 1, 3, 1]);
+ let vertexBuffer = device.createBuffer({ size: vertexData.byteLength, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST });
...
+ let indexBuffer = device.createBuffer({ size: 8, usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST });
+ device.queue.writeBuffer(indexBuffer, 0, new Uint16Array([0, 1, 2, 0])); // 8 bytes: uint16 index buffer must be > 4 bytes
...
+ // Written once and never again: rewriting it would invalidate the args buffer's own cache and defeat the setup.
+ let argsBuffer = device.createBuffer({ size: 20, usage: GPUBufferUsage.INDIRECT | GPUBufferUsage.COPY_DST });
+ device.queue.writeBuffer(argsBuffer, 0, new Uint32Array([3, 1, 0, 0, 0]));
...
+ pass.setIndexBuffer(indexBuffer, 'uint16');
+ pass.drawIndexedIndirect(argsBuffer, 0);
...
+ // Frame 0: indices 0,1,2 are all below minVertexCount, so the clamp pass runs, passes, and
+ // indirectIndexedBufferRecomputed() caches the verdict on argsBuffer.
+ let frame0 = await drawAndReadback();
+
+ // Rewrite only the index buffer, with values at or above minVertexCount. This bumps the index buffer's
+ // generation but touches nothing on argsBuffer, where the verdict lives.
+ device.queue.writeBuffer(indexBuffer, 0, new Uint16Array([3, 4, 5, 0]));
+
+ // Frame 1: same pipeline, same buffers, same offsets - every term the old predicate compared is unchanged.
+ // The clamp pass must still re-run, find indices >= minVertexCount, and zero indexCount, leaving the clear.
+ let frame1 = await drawAndReadback();
...
+ shouldBeEqualToString('frame0.join()', '0,255,0,255');
+ shouldBeEqualToString('frame1.join()', '255,0,0,255');

The production hunks are not part of this write-up; the regression test above shows the defect's shape. The test's description names the functions involved, indirectIndexedBufferRequiresRecomputation() and indirectIndexedBufferRecomputed(). The test sets up a 3-vertex buffer (minVertexCount 3), an index buffer holding 0,1,2,0, and an args buffer written once with indexCount 3. Frame 0 draws and caches a passing verdict on the args buffer. The test then rewrites only the index buffer with 3,4,5,0 and draws again with every other parameter unchanged, expecting frame 1 to show the red clear (clamp re-ran and zeroed indexCount) instead of green (draw executed). The fix makes the cached verdict sensitive to index-buffer content changes. Buffer.h declares a per-buffer m_indexContentsGeneration bumped by indexBufferContentsModified(...), which is a plausible new invalidation key, though the exact mechanism is not established here.

Memoized validation verdict cached on one object but derived from another object's mutable contents, which are absent from the invalidation key.

Indexed draws. drawIndexed/drawIndexedIndirect read a list of uint16 or uint32 indices from an index buffer. Each index selects which vertex's attributes to fetch, at index * arrayStride within each vertex-step-mode vertex buffer.

Indirect draws. drawIndexedIndirect(argsBuffer, offset) reads indexCount, instanceCount, firstIndex, baseVertex and firstInstance from a GPU buffer in the MTLDrawIndexedPrimitivesIndirectArguments layout, so the CPU does not know them at encode time.

minVertexCount and robust access. minVertexCount is the smallest number of vertices any bound vertex-step-mode buffer can supply (size divided by arrayStride); indices at or above it name vertices that don't exist. WebGPU requires that out-of-range accesses never read outside the bound resource. Metal's indexed fetch doesn't provide that, so WebKit's backend validates itself — for indirect indexed draws, with the clamp pass.

Validation caching. Buffer keeps caches recording that a draw configuration has already been proven safe, so identical later draws skip validation. The indirect-indexed verdict is managed by indirectIndexedBufferRecomputed / indirectIndexedBufferRequiresRecomputation. Buffer.h also tracks a per-write index content generation (m_indexContentsGeneration) and monotone upper bounds on index values ever written (m_indexValueUpperBoundUint / m_indexValueUpperBoundUshort).

This is a stale validation cache — an incomplete cache key — with an out-of-bounds GPU read as the consequence.

  Before:                                After:
  frame 0: clamp pass, 0,1,2 < 3         frame 0: clamp pass, verdict cached
    verdict cached on argsBuffer
  writeBuffer(indexBuffer, 3,4,5)        writeBuffer(indexBuffer, 3,4,5)
    argsBuffer untouched                   index-content change observed
  frame 1: predicate compares            frame 1: verdict invalid
    offset, minVertexCount, ... ==          └─► clamp pass re-runs
    └─► clamp skipped                            └─► indexCount = 0
         └─► fetch vertex[3..5]  OOB                  └─► red clear, no draw

The verdict is a function of two buffers, but the left column's predicate compared only args-buffer-side terms: indirectOffset, minVertexCount, minInstanceCount, indexType, indexBufferOffsetInBytes and drawType, per the test description. The writeBuffer arrow bumps the index buffer's generation and leaves the args buffer untouched, so frame 1's comparison still matches and new index values are never bounds-checked. The test description stresses that "no high-water-mark trick is needed": the args-buffer verdict didn't observe index-buffer writes at all, independently of the monotone upper-bound tracking.

The test makes the result observable without depending on fetched attribute values (it uses @builtin(vertex_index)), reads back one pixel per frame, and shows that with a stale verdict the draw would "skip the clamp, draw green, and fetch vertex attributes past the end of the 3-vertex buffer". Reachability is two ordinary drawIndexedIndirect calls sharing an unchanged args buffer with a writeBuffer on the index buffer between them, and the skip is deterministic.

The primitive is a GPU-side out-of-bounds read relative to the vertex buffer's base, at an offset of script-chosen index times arrayStride — up to 65535 * arrayStride for uint16 and 2^32 * arrayStride for uint32. It is read-only: the skipped pass guards only index-driven vertex fetches. Whether it produces a real out-of-bounds memory read assumes the Metal driver or hardware applies no robustness of its own that would zero or clamp the fetch. If fetched values were routed into a readable render target — which the WebGPU API makes straightforward — and neighbouring memory held another context's data, this would amount to a GPU-memory disclosure bounded by attribute format and draw count. Encoding happens in the GPU process, but the access runs on shader cores, so this bug alone gives no code execution or sandbox escape.

This vulnerability weakens the WebGPU robustness boundary. The security model assumes no draw issued by web content can make shader cores read outside the buffers bound to it, and WebKit enforces that with the clamp pass. Before the fix, script-chosen indices drove vertex fetches past the end of the vertex buffer; if those reads landed in other GPU allocations and flowed into output readable via copyTextureToBuffer or mapAsync, this would be a GPU-memory information disclosure reachable from web content, with cross-origin reach depending on GPU memory layout.

Insight: Buffer.h already carries two kinds of index-content tracking — a per-write generation and monotone high-water marks. The generation changes on every write, so it's the sound invalidation key for any cached proof that depends on exact contents; the upper bound is only sound for proofs that depend solely on the max. Any cache in this file that keys on the monotone bounds, or is stored on a different buffer than the one whose contents it read, deserves the same scrutiny — and this week's appendix carries three siblings (cb21dfb, fb81a00, 45270f2) in the same cache family.