VideoFrame.copyTo() leaks uninitialized heap bytes due to stride mismatch in copyPlane
VideoFrame.copyTo() used a hardcoded packed stride to read pixel rows from a surface that actually had a larger aligned stride, so rows after the first were read from the wrong offsets. An attacker could obtain uninitialized heap bytes in the returned ArrayBuffer, leaking memory contents.
- Fixed in 31 days
- Paid in 35 days
- Disclosed after 31 days
Created attachment 9551938
poc_videoframe_copyto_padding.html
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:148.0) Gecko/20100101 Firefox/148.0
Steps to reproduce:
I was looking at how VideoFrame handles RGB surfaces internally and found
that copyTo() uses a hardcoded packed stride (width * 4) to walk the source
data, but the actual backing surface has a larger aligned stride. The gap
bytes are uninitialized heap, and they end up in the caller's ArrayBuffer.
The bug is in the copyPlane lambda inside Resource::CopyTo()
(VideoFrame.cpp:2825-2850). It advances the source pointer by
Stride(aPlane) on each row (line 2844). For RGB formats, Stride() returns
width * SampleBytes (line 2778) = width * 4 — the packed row size.
But copyPlane is called on a temp DataSourceSurface that CopyTo creates at
line 2925-2927:
Factory::CreateDataSourceSurfaceWithStride(size, f, map.GetStride())
This temp surface inherits the stride from the stored source surface, which
comes from AllocateBGRASurface (VideoFrame.cpp:211):
CreateDataSourceSurfaceWithStride(..., surfaceMap.GetStride())
The stored surface stride ultimately comes from wherever the snapshot was
created. For any DataSourceSurface, Init (SourceSurfaceRawData.cpp:74-103)
uses GetAlignedStride<16> by default, rounding up to the next 16-byte
boundary. For WebGPU canvases, the stride comes from the GPU readback
buffer alignment (CanvasManagerChild.cpp:257-272) and can be much larger
(e.g. 256 bytes).
So for a canvas with width=5 (packed row = 20 bytes):
- GetAlignedStride<16>(20) = 32 for any 2D/WebGL canvas
- GPU readback stride can be 256 for a WebGPU canvas
- copyPlane advances by Stride(RGBA) = 20
copyPlane reads:
row 0 from offset 0 -- correct
row 1 from offset 20 -- wrong, actual row 1 is at offset 32 (or 256)
row 2 from offset 40 -- wrong, actual row 2 is at offset 64 (or 512)
Everything after row 0 drifts into the padding of earlier rows.
Both the AllocateBGRASurface and CopyTo temp surfaces are allocated with
aZero=false (default for B8G8R8A8), so the padding is whatever malloc
returned. SwizzleData writes only width*4 bytes per row — padding is never
touched.
The output goes directly into the caller's ArrayBuffer — no image encoder
in the path, no lossy step. Raw heap bytes.
Steps to reproduce:
1. Firefox with WebCodecs enabled (dom.media.webcodecs.enabled = true)
2. Serve the attached PoC over localhost or https (needs secure context)
3. Click "Run"
4. It creates a 5x16 canvas, draws solid green, constructs a VideoFrame
from it, calls copyTo() into a Uint8Array, and checks whether all
pixels match the expected green
5. On a vulnerable build, rows after row 0 will contain corrupted data —
a mix of real pixels and uninitialized padding bytes
The PoC uses a plain 2D canvas (stride gap = 12 bytes/row). WebGPU
canvases produce much larger gaps (up to 236 bytes/row for width=5) but
the root cause is the same: copyPlane uses Stride(aPlane) instead of the
temp surface's actual mapped stride.
Source analysis against mozilla-central at 150.0a1.
Tested on Firefox 148.0.2 (aarch64), macOS.
Actual results:
VideoFrame.copyTo() returns corrupted pixel data. Rows after the first
contain bytes from the surface padding instead of actual pixel data.
The chain:
new VideoFrame(canvas, {timestamp: 0})
-> SurfaceFromElement -> HTMLCanvasElement::GetSurfaceSnapshot()
-> returns DataSourceSurface with GetAlignedStride<16> stride
-> AllocateBGRASurface(surface)
-> CreateDataSourceSurfaceWithStride(size, B8G8R8A8, surfaceMap.GetStride())
-> SwizzleData, padding untouched
frame.copyTo(buffer)
-> Resource::CopyTo()
-> maps stored surface, gets map.GetStride() = 32 (for width=5)
-> creates temp surface with stride 32, aZero=false
-> SwizzleData into temp (pixel data correct, padding stale)
-> copyPlane(tempMap.GetData())
-> advances source by Stride(aPlane) = width * 4 = 20
-> reads padding bytes as pixel data
-> writes them into caller's ArrayBuffer
For width=5 with aligned stride 32:
- packed row = 20 bytes, actual stride = 32
- 12 bytes of uninitialized padding per row
- copyPlane reads 20 bytes starting at offsets 0, 20, 40, 60, ...
- but real rows start at offsets 0, 32, 64, 96, ...
- row 1 output contains 12 bytes of row 0 padding + 8 bytes of row 1
- each subsequent row drifts 12 bytes further
The caller gets a mix of real pixels and stale heap content in a plain
Uint8Array with no encoding or filtering step.
Expected results:
copyPlane should advance the source pointer by the actual mapped stride of
the surface it's reading from, not by the hardcoded Stride(aPlane).
The cleanest fix in Resource::CopyTo(): pass the temp surface's real stride
into copyPlane, or better yet skip the temp surface and swizzle directly
into the destination buffer using the caller's requested stride.
At minimum:
- in copyPlane, use tempMap.GetStride() instead of Stride(aPlane) to
advance aPlaneData
- or allocate the temp surface with aZero=true so padding is at least
zeroed (but the stride mismatch still corrupts the image)
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
Likely the copy routine advanced the source pointer by a hardcoded packed row size instead of the temporary surface's actual mapped stride, causing it to read padding bytes as pixel data.
Impact
The caller could receive uninitialized heap bytes in a plain Uint8Array, potentially disclosing memory contents; the text does not claim a full exploit beyond this leak.
What the attacker needed
WebCodecs enabled in Firefox; Secure context (localhost or HTTPS); Victim runs the crafted page
The fix
The copy routine should advance by the temporary surface's actual mapped stride, or swizzle directly into the destination using the caller's requested stride; the text does not say it was fixed.
How it was found
- Enable WebCodecs in Firefox.
- Serve the proof-of-concept page over localhost or HTTPS.
- Click Run to create a small canvas, draw solid green, build a VideoFrame and call copyTo().
- Observe that rows after the first contain corrupted data mixing pixels and uninitialized padding.
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.
When an image is stored in memory, each row may be padded to a round size for speed. The code that copied the image out assumed rows were packed tightly, so it read the padding as if it were pixels. That padding was never cleared, so the caller received leftover memory contents.
What is Info disclosure?
Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.