transferred OffscreenCanvas toDataURL() serializes uninitialized surface padding as pixel data (stride mismatch in ExtractDataInternal)
When a transferred OffscreenCanvas with a WebGL context is exported via toDataURL(), the image encoder is told the surface rows are tightly packed, but the real surface has 16-byte aligned stride with uninitialised padding. The encoder therefore reads padding bytes as pixel data, producing corrupted output that can include uninitialised heap memory.
- Fixed in 21 days
- Paid in 35 days
- Disclosed after 21 days
Created attachment 9551948
poc_transferred_offscreencanvas_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 the transferred-OffscreenCanvas toDataURL() path — the
aOffscreenDisplay branch in ImageEncoder::ExtractDataInternal
(dom/base/ImageEncoder.cpp, line 373-404) — and noticed it tells the image
encoder the stride is width * 4, as if the surface buffer were tightly packed.
But the surface it's mapping isn't packed.
When a transferred WebGL canvas calls toDataURL(), GetSurfaceSnapshot()
routes through TransformSurface() (OffscreenCanvasDisplayHelper.cpp:405),
which allocates a fresh DataSourceSurface via
Factory::CreateDataSourceSurface(size, format, false). That surface gets a
16-byte aligned stride from SourceSurfaceAlignedRawData::Init
(GetAlignedStride<16>). The transform helpers (SwizzleYFlipData etc.) only
write width*4 bytes per row and leave the per-row padding alone.
Then ExtractDataInternal maps that surface and passes it to the encoder:
rv = aEncoder->InitFromData(map.mData,
size.width * size.height * 4,
size.width,
size.height,
size.width * 4, // <-- wrong, assumes packed
...);
The encoder steps through rows at width*4 intervals, but the actual row
spacing is larger. So it reads across row boundaries into the padding region.
For alpha-bearing formats (B8G8R8A8), that padding is uninitialized heap
memory. CreateDataSourceSurface with aZero=false only clears for B8G8R8X8
(Factory.cpp:921: clearSurface = aZero || aFormat == B8G8R8X8). The backing
comes straight from malloc via AlignedArray::Realloc(bufLen, false).
toBlob() on the same canvas is NOT affected — it goes through
OffscreenCanvasDisplayHelper::GetImageBuffer() which uses
SurfaceToPackedBGRA() to copy row-by-row with the real stride.
Steps to reproduce:
1. Serve the attached PoC over localhost or https
2. Click "Run"
3. It creates a 5x8 canvas, transfers to OffscreenCanvas, gets a WebGL
context with alpha:true, draws solid green, then exports via toDataURL
and toBlob on the original HTMLCanvasElement
4. toBlob returns correct pixels (all green)
5. toDataURL returns corrupted pixels — padding bytes mixed into the output
Width=5 is chosen because 5*4=20 aligns up to stride 32 under
GetAlignedStride<16>, giving 12 bytes of uninitialized padding per row. Any
width not divisible by 4 triggers this.
WebGL with alpha:true gives B8G8R8A8 format (not cleared) and BottomLeft
origin (forces TransformSurface into the copy path). This is the strongest
deterministic trigger I found.
Source analysis against mozilla-central at 150.0a1.
Tested on Firefox 148.0.2 (aarch64), macOS.
Actual results:
toDataURL() on the transferred canvas returns corrupted image data. Decoded
pixels don't match what was drawn because uninitialized surface padding is
read as pixel data.
The chain:
OffscreenCanvasDisplayHelper::GetSurfaceSnapshot()
-> TransformSurface(surface, hasAlpha=true, isAlphaPremult=true, BottomLeft)
-> Factory::CreateDataSourceSurface(size, B8G8R8A8, false)
-> clearSurface = false (not B8G8R8X8, aZero=false)
-> stride = GetAlignedStride<16>(5, 4) = 32
-> AlignedArray::Realloc(bufLen, false) -> malloc()
-> SwizzleYFlipData writes 20 bytes/row, padding untouched
ImageEncoder::ExtractDataInternal (line 396-399)
-> InitFromData(map.mData, 160, 5, 8, 20, ...)
-> encoder walks rows at offset 0, 20, 40, ...
-> actual rows are at 0, 32, 64, ...
-> row 1 reads: 12 bytes padding + 8 bytes of real row 1
For a 5x8 canvas, the first row is correct but every subsequent row is
shifted and contains padding bytes from the previous row.
toBlob() on the same canvas works correctly because it uses
SurfaceToPackedBGRA() which copies row-by-row with the real stride
(OffscreenCanvasDisplayHelper.cpp:591, DataSurfaceHelpers.cpp:137-165).
Expected results:
The aOffscreenDisplay branch in ExtractDataInternal should use the real
surface stride, or better yet, pack the surface first.
The cleanest fix: don't use the raw mapped surface for the encoder. Pack it
through SurfaceToPackedBGRA() or CopySurfaceDataToPackedArray() first, then
hand the packed buffer to ImageEncoder::GetInputStream(). This is basically
what the toBlob path already does.
At minimum:
- pass map.mStride instead of size.width * 4 to InitFromData
- or zero the surface (aZero=true in TransformSurface) to prevent heap
content from leaking through the padding
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
The aOffscreenDisplay branch of ImageEncoder::ExtractDataInternal passes width*4 as the stride to the encoder, but the mapped surface actually has a 16-byte aligned stride with uninitialised padding, so the encoder walks rows at the wrong offsets.
Impact
toDataURL() on a transferred WebGL canvas returns corrupted image data and may expose uninitialised heap memory through the padding bytes; toBlob() on the same canvas is not affected.
What the attacker needed
Victim loads attacker page; browser runs the script
The fix
The aOffscreenDisplay branch should pack the surface first (for example via SurfaceToPackedBGRA or CopySurfaceDataToPackedArray) before handing it to the encoder, or at minimum pass the real stride or zero the surface.
How it was found
- Serve the proof-of-concept page over localhost or https
- Click the Run button
- The page creates a small canvas, transfers it to an OffscreenCanvas, gets a WebGL context with alpha enabled, fills it with solid green, then exports via toDataURL and toBlob
- toBlob returns correct pixels while toDataURL returns corrupted pixels with padding bytes mixed in
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.
A canvas is a drawing area in the browser; toDataURL() turns it into an image. Internally the browser stores each row of pixels with some extra unused bytes at the end (padding) to keep rows aligned in memory. The code told the image encoder to skip that padding, so it read the unused bytes as if they were real pixels, and those bytes were never cleared, so they could contain 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.