← All reports

[4] ANGLE Metal backend out-of-bounds vertex fetch for converted attributes

MediumANGLE Metal backendOOB

ANGLE's front-end approved the WebGL2 draw. Metal then read past the buffer.

a3cf2c8

Rated Medium: WebGL2 content can make the GPU fetch tens of bytes past the end of a vertex buffer, but the read is bounded and read-only. Whether it becomes a leak depends on what is allocated next to the buffer and how the GPU treats out-of-range fetches.

WebGL on Apple platforms is implemented by translating OpenGL ES into Metal, and every draw is validated once by a front-end before a backend turns it into native GPU bindings. In ANGLE, the GL-to-native translation library WebKit embeds, VertexArrayMtl (the Metal backend's vertex-array state) may store a vertex attribute in a wider actual format than the GL type the page declared, converting the data into a new buffer when Metal cannot fetch the original directly. The backend is expected to fetch only within the bytes the front-end measured when it admitted the draw.

The angle: a WebGL2 page with a packed vertex attribute in a buffer sized to the last byte can get a front-end-approved draw executed as a GPU read past the end of that buffer, with the in-bounds bytes misread as floats.

VertexArrayMtl::syncDirtyAttrib sizes its buffer-capacity check with the actual Metal format rather than the GL attribute's type size. The two agree for natively supported formats, but the actual format is widened whenever the attribute needs conversion: R10G10B10A2_SSCALED is emulated as R32G32B32A32_FLOAT, 4 bytes becoming 16. GetVertexCount then subtracts 16 from a buffer that only has to hold 4 bytes per vertex, the subtraction underflows, and a buffer holding a legal number of vertices reports zero. Zero takes the numVertices == 0 branch, which is tested before needConversion, so the attribute skips convertVertexBuffer and the raw GL buffer is bound with the widened descriptor at a stride of 16. That fallback relied on KHR_robust_buffer_access_behavior clamping the fetch, which Metal does not expose, while the front-end admitted the draw because it measures the same attribute at 4 bytes. The GPU reads past the end of the buffer, and misreads the packed bits as floats even for the in-bounds vertex. The size basis changed in 314003@main, which hoisted this check out of convertVertexBuffer so it would cover the non-conversion path too.

Count with gl::ComputeVertexAttributeTypeSize(attrib) instead, the call the front-end uses to compute VertexAttribute::mCachedElementLimit. Every other backend already passes this value to its equivalent check. Also stop the numVertices == 0 fallback from binding the source buffer with a fabricated stride; leave the attribute unbound so setupDraw supplies the default attribute value, and clear the inline data pointer so a stale client array cannot be picked up instead.

Source/ThirdParty/ANGLE/src/libANGLE/renderer/metal/VertexArrayMtl.mm

- unsigned srcFormatSize = format.actualAngleFormat().pixelBytes;
+ // Count source vertices with the GL attribute's type size, which is
+ // the basis the front-end uses for
+ // VertexAttribute::mCachedElementLimit. The actual Metal format is
+ // wider whenever the format needs conversion, eg.
+ // R10G10B10A2_SSCALED (4 bytes) is emulated as R32G32B32A32_FLOAT
+ // (16 bytes), so using it under-counts the vertices the source
+ // buffer holds.
+ unsigned srcFormatSize =
+ static_cast<unsigned>(gl::ComputeVertexAttributeTypeSize(attrib));
size_t numVertices = GetVertexCount(bufferMtl, binding, srcFormatSize);
if (numVertices == 0)
{
// Out of bound buffer access, can return any values.
// See KHR_robust_buffer_access_behavior.
- mCurrentArrayBuffers[attribIndex] = bufferMtl;
- mCurrentArrayBufferFormats[attribIndex] = &format;
+ mCurrentArrayBuffers[attribIndex] = nullptr;
+ mCurrentArrayInlineDataPointers[attribIndex] = nullptr;
+ mCurrentArrayInlineDataSizes[attribIndex] = 0;
+ mCurrentArrayBufferFormats[attribIndex] =
+ &contextMtl->getVertexFormat(angle::FormatID::NONE, false);
mCurrentArrayBufferOffsets[attribIndex] = 0;
- mCurrentArrayBufferStrides[attribIndex] = 16;
+ mCurrentArrayBufferStrides[attribIndex] = 0;
}
else if (needConversion)
...

Source/ThirdParty/ANGLE/src/tests/gl_tests/VertexAttributeTest.cpp

+TEST_P(VertexAttributeTestES3, PackedIntRevBufferWithNoSlack)
+{
+ constexpr size_t kNoSlackVertexCount = 3;
...
+ // Sized to the last byte: exactly kNoSlackVertexCount packed vertices, no slack.
+ glBufferData(GL_ARRAY_BUFFER, sizeof(packedInput), packedInput.data(), GL_STATIC_DRAW);
+ glVertexAttribPointer(testLoc, 4, GL_INT_2_10_10_10_REV, GL_FALSE, 0, nullptr);
...
+ glDrawArrays(GL_TRIANGLES, 0, kNoSlackVertexCount);

rx::VertexArrayMtl::syncDirtyAttrib changes in two places. The size passed to GetVertexCount(bufferMtl, binding, srcFormatSize) is now gl::ComputeVertexAttributeTypeSize(attrib), the GL attribute's own type size, instead of format.actualAngleFormat().pixelBytes, the size of the emulated Metal format. The numVertices == 0 branch no longer binds the source buffer: it sets mCurrentArrayBuffers[attribIndex] to nullptr, clears mCurrentArrayInlineDataPointers and mCurrentArrayInlineDataSizes, points mCurrentArrayBufferFormats at the FormatID::NONE vertex format, and sets offset and stride to 0. In the test file, a Pack2101010Rev helper and GL_INT_2_10_10_10_REV stride entry are added, along with signed normalized/unnormalized INT_2_10_10_10_REV tests and the regression test PackedIntRevBufferWithNoSlack, which draws three packed vertices from a 12-byte buffer with no slack.

Validator and backend measuring one element with different size bases, so a widened format underflows the capacity check and routes the draw to an unconverted fallback.

Vertex attribute fetch. A WebGL draw reads per-vertex attributes from a bound buffer. Each attribute is described by a component type (such as GL_FLOAT or GL_INT_2_10_10_10_REV), a component count, a stride (bytes between vertices, 0 meaning tightly packed), and an offset into the buffer. GL_INT_2_10_10_10_REV packs three signed 10-bit channels and one signed 2-bit channel into one 4-byte word and is available in OpenGL ES 3.0 and WebGL2.

Front-end and backend. ANGLE's front-end (libANGLE) validates every draw; one check compares the requested vertex range against VertexAttribute::mCachedElementLimit, a per-attribute bound derived from buffer size, offset, stride and gl::ComputeVertexAttributeTypeSize(attrib) (per the added comment). The backend (renderer/metal) then translates validated state into native bindings.

Intended vs. actual format. Each ANGLE format has an intended format (what GL specified) and an actual format (what the native API uses). When Metal cannot fetch a format directly, the backend picks a wider actual format and converts the data into a new buffer. actualAngleFormat().pixelBytes is the per-element size of the actual format.

GetVertexCount. A helper in VertexArrayMtl.mm that computes how many whole vertices fit in a binding, using angle::CheckedNumeric<size_t> and returning 0 if any subtraction underflows.

KHR_robust_buffer_access_behavior and default attributes. The extension guarantees that out-of-bounds buffer reads return zero or values from inside the buffer, never outside it, so a backend on a robustness-enforcing API can treat an out-of-range fetch as safe. When an attribute array is disabled or unbound, the shader receives the current generic attribute value (set by glVertexAttrib4f and similar); per the patch's added comment, in the Metal backend setupDraw() supplies it when the attribute is left unbound.

The front-end and the Metal backend measured the same attribute in two different units, and the mismatch routed a legal draw into a fallback that bound real memory.

  Before:                                       After:
  GetVertexCount(size=16)  (actual format)      GetVertexCount(size=4)  (GL type size)
    12 - 16 underflows -> 0                       12 bytes -> 3 vertices
  numVertices == 0  (tested before conversion)  needConversion
    bind raw 12-byte buffer                       convertVertexBuffer -> R32G32B32A32
    format R32G32B32A32_FLOAT, stride 16          fetch from converted buffer
    GPU fetches 16 B/vertex  -> past end        numVertices == 0 (truly too small)
                                                  unbind; setupDraw default value

On the left, GetVertexCount computes bytes = size - bindingOffset - srcFormatSize with checked arithmetic and returns 0 on underflow. With srcFormatSize at 16 (the widened R32G32B32A32_FLOAT), any binding with fewer than 16 usable bytes reports zero vertices, even though the front-end, measuring at 4 bytes, has admitted up to three. Because numVertices == 0 is tested before needConversion, conversion is skipped and the raw buffer is bound with the widened descriptor at a fabricated stride of 16. That branch's comment (can return any values. See KHR_robust_buffer_access_behavior) treated the fetch as benign; the commit message states Metal does not expose that extension, and the new test's comment describes the binding as "misreading the packed bits as floats and fetching past the end of the buffer." Two failures follow: an out-of-bounds read past the source buffer, and misinterpretation of in-bounds packed bits as IEEE floats.

The commit message attributes the size-basis change to 314003@main, which hoisted the check out of convertVertexBuffer to cover the non-conversion path. convertVertexBuffer measured the intended format; the hoisted copy picked up the format local, which in syncDirtyAttrib refers to the actual format.

The regression test shows exactly the shape needed: a 12-byte buffer holding three GL_INT_2_10_10_10_REV vertices with no slack, an unnormalized attribute (which needs conversion), and glDrawArrays(GL_TRIANGLES, 0, 3). Before the fix it failed at every pixel, including the in-bounds ones.

Exploitability: reachable from web content through WebGL2 with a buffer-backed, unnormalized GL_INT_2_10_10_10_REV attribute whose usable bytes after the offset are fewer than 16, followed by a draw the front-end admits. The GPU then fetched up to about 36 bytes past the end of the binding. The confirmed effect is wrong rendering plus an out-of-bounds GPU read. The read is read-only and bounded to tens of bytes, and the values reach only the vertex shader as float inputs. Escalation to a meaningful leak hinges on the BufferMtl allocation model. If WebGL buffers are sub-allocated from a shared pool, the bytes past the end could belong to another WebGL buffer, possibly from another context or origin, and could be routed through the vertex shader into rendered pixels or transform feedback and read back with readPixels. If they are standalone page-backed allocations, the over-read would land in padding or fault on the GPU. ANGLE typically runs in the GPU process on current Apple platforms, so any leaked bytes would come from GPU-process Metal allocations.

Before the fix, this vulnerability weakened memory safety at the WebGL/GPU boundary. WebGL relies on front-end validation meaning the backend only fetches within the bound buffer, and the Metal backend broke that for converted packed attributes. Whether Apple GPU hardware clamps or zero-fills such fetches is open. If the over-read returns real contents of adjacent GPU allocations and those can be made visible through rendering and readback, the vulnerability could expose a small, bounded cross-allocation information leak in the process hosting ANGLE.

Takeaway: a backend's capacity check should be the same measurement as the front-end bound that admitted the draw; when a translation layer widens a format, any bound computed in native units downstream of validation is a mismatch waiting to happen.