Closed Bug 2038363 Opened 6 months ago Closed 6 months ago

WebGPU ExternalTexture: Missing validation of signed IntSize dimensions from IPC before static_cast<uint32_t> in parent process

Categories

(Core :: Graphics: WebGPU, defect)

defect

Tracking

()

RESOLVED INVALID

People

(Reporter: abdullahmarzuki22, Unassigned)

References

Details

(Keywords: ai-involved, reporter-external)

Attachments

(1 obsolete file)

Attached file ipc_cast_simulation (obsolete) —

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36

Steps to reproduce:

Source code review of dom/webgpu/ExternalTexture.cpp found missing IPC input validation.

  1. ExternalTextureSourceDescriptor struct contains gfx::IntSize mSize (signed int) that crosses IPC from content process to parent process (dom/webgpu/ipc/WebGPUTypes.h:84).

  2. Parent process receives it in WebGPUParent::RecvCreateExternalTextureSource (dom/webgpu/ipc/WebGPUParent.cpp:1662) without validating that dimensions are non-negative.

  3. ExternalTexture.cpp casts dimensions with static_cast<uint32_t>(size.width) at multiple sites (lines ~518, ~758, ~843, ~1246) without any bounds check.

  4. A compromised content process can craft IPC message: ExternalTextureSourceDescriptor.mSize = {-1, 1}

  5. After static_cast<uint32_t>(-1) = 4294967295, the corrupted dimensions are passed to wgpu_server_device_create_texture() and wgpu_server_queue_write_texture() in the parent process.

Relevant code:

  // ExternalTexture.cpp line ~518-519
  .width = static_cast<uint32_t>(size.width),   // no validation!
  .height = static_cast<uint32_t>(size.height),  // no validation!

  // IPC struct (WebGPUTypes.h:84)
  struct ExternalTextureSourceDescriptor {
      gfx::IntSize mSize;  // signed int, no non-negative enforcement
      ...
  };

Searchfox links:

Actual results:

static_cast<uint32_t> on negative signed values produces huge unsigned values without any validation or error:

mSize = {-1, 1}:
width becomes 4294967295 (0xFFFFFFFF)
Row stride = 4294967295 * 4 = 17179869180 bytes (16 GB)

mSize = {INT32_MIN, 1}:
width becomes 2147483648 (0x80000000)
Buffer calc in uint32: 2147483648 * 4 = 0 (integer wrap)
→ zero-size allocation, non-zero write → heap buffer overflow

These corrupted dimensions reach wgpu FFI calls in the parent process, leading to potential heap corruption and sandbox escape.

Note: other WebGPU IPC paths DO validate. GetFrontBufferSnapshot (~line 1306) checks:
if (size.width > INT16_MAX || size.height > INT16_MAX)
The ExternalTexture path lacks this same check.

A similar pattern was previously fixed in Bug 2021451 (WebRTC Audio/Video: "AssertedCast error: Cannot cast 4294967295 from uint32_t to int32_t").

Also found same pattern in CanvasManagerParent.cpp:231-232:
buffer.surfSize.x = static_cast<uint32_t>(size.width);
buffer.surfSize.y = static_cast<uint32_t>(size.height);

Expected results:

RecvCreateExternalTextureSource should validate mSize dimensions before processing, rejecting negative or out-of-range values:

  ipc::IPCResult WebGPUParent::RecvCreateExternalTextureSource(
      const ExternalTextureSourceDescriptor& aDesc) {
    if (aDesc.mSize.width <= 0 || aDesc.mSize.height <= 0 ||
        aDesc.mSize.width > INT16_MAX || aDesc.mSize.height > INT16_MAX) {
      return IPC_FAIL(this, "Invalid external texture dimensions");
    }
    // ... rest of handler
  }

And/or add MOZ_DIAGNOSTIC_ASSERT before each cast in ExternalTexture.cpp:
MOZ_DIAGNOSTIC_ASSERT(size.width > 0 && size.height > 0);

This would be consistent with the existing validation in GetFrontBufferSnapshot and prevent signed-to-unsigned cast vulnerabilities from IPC input.

Group: core-security → gfx-core-security

I don't know what your binary attachment is, but it's not a valid testcase format. We need a test that shows that this problem can actually occur in the context of a Firefox binary, because there might be checks elsewhere that validate these parameters.

Flags: needinfo?(abdullahmarzuki22)
Attachment #9584743 - Attachment is obsolete: true
Attachment #9584743 - Attachment mime type: application/x-executable → text/plain

Thank you for the feedback, Daniel. I apologize for the invalid attachment format. Let me provide a proper analysis with exact code references and a testable demonstration.

Threat Model

This is an IPC input validation issue under Mozilla's compromised-content-process threat model. The parent/GPU process receives gfx::IntSize (signed int32_t) values over IPC from the content process and uses them via static_cast<uint32_t>() without validating non-negativity. A compromised content process can craft the IPC message to pass negative dimensions.

Complete Code Path Trace (No Validation at Any Stage)

Stage 1 - Content Process sends IPC message:
dom/webgpu/ExternalTexture.cpp:375 — child->SendCreateExternalTextureSource(...) sends ExternalTextureSourceDescriptor containing gfx::IntSize mSize (line 88 of WebGPUTypes.h).

Stage 2 - IPC Deserialization (no validation):
dom/webgpu/ipc/WebGPUTypes.h:109-116 — ParamTraits<ExternalTextureSourceDescriptor>::Read() deserializes mSize as raw int32_t values:

static bool Read(MessageReader* aReader, ParamType* aResult) {
return ReadParam(aReader, &aResult->mTextureIds) &&
ReadParam(aReader, &aResult->mViewIds) &&
ReadParam(aReader, &aResult->mSurfaceDescriptor) &&
ReadParam(aReader, &aResult->mSize) && // no validation
ReadParam(aReader, &aResult->mSampleTransform) &&
ReadParam(aReader, &aResult->mLoadTransform);
}

The underlying ParamTraits<gfx::IntSize> (in gfx/ipc/GfxMessageUtils.h) also performs no bounds checking — it reads arbitrary int32_t values.

Stage 3 - Parent process IPC handler (no validation):
dom/webgpu/ipc/WebGPUParent.cpp:1660-1670:

ipc::IPCResult WebGPUParent::RecvCreateExternalTextureSource(
RawId aDeviceId, RawId aQueueId, RawId aExternalTextureSourceId,
const ExternalTextureSourceDescriptor& aDesc) {
MOZ_RELEASE_ASSERT(mExternalTextureSources.find(aExternalTextureSourceId) ==
mExternalTextureSources.end());
mExternalTextureSources.emplace(
aExternalTextureSourceId,
ExternalTextureSourceHost::Create(this, aDeviceId, aQueueId, aDesc));
return IPC_OK();
}

No validation of aDesc.mSize — passes directly to Create().

Stage 4 - Factory method (no validation):
dom/webgpu/ExternalTexture.cpp:426-440 — ExternalTextureSourceHost::Create() dispatches to platform-specific handlers without checking mSize.

Stage 5 - Unsafe cast:
dom/webgpu/ExternalTexture.cpp:518-519 (inside createPlane lambda):

const ffi::WGPUTextureDescriptor textureDesc{
.size = ffi::WGPUExtent3d{
.width = static_cast<uint32_t>(size.width), // -1 becomes 4294967295
.height = static_cast<uint32_t>(size.height), // no bounds check
.depth_or_array_layers = 1,
},
...
};

Same pattern repeats at lines 758-759, 843-844, and 916-917.

Stage 6 - Error not checked, execution continues to write_texture:
After wgpu_server_device_create_texture() is called, the error is forwarded but NOT checked — execution unconditionally proceeds to wgpu_server_queue_write_texture() with the same corrupted dimensions:

// Texture creation — error forwarded but NOT checked
{
ErrorBuffer error;
ffi::wgpu_server_device_create_texture(
aParent->GetContext(), aDeviceId, texId, &textureDesc, error.ToFFI());
error.CoerceValidationToInternal();
aParent->ForwardError(error); // error forwarded but return not checked
}

// Execution continues unconditionally — writes to potentially invalid texture
{
ErrorBuffer error;
ffi::wgpu_server_queue_write_texture(aParent->GetContext(), aDeviceId,
aQueueId, &dest, data, &layout,
&textureDesc.size, error.ToFFI()); // uses corrupted dimensions
...
}

This means even if wgpu's device_create_texture rejects the oversized dimensions (via MAX_TEXTURE_EXTENT = 32767 enforcement in restrict_limits()), the code proceeds to call queue_write_texture with texId that may reference an error/invalid texture, using the corrupted dimensions for the copy extent.

Regarding checks elsewhere

I have verified the following:

  1. ParamTraits<gfx::IntSize> (gfx/ipc/GfxMessageUtils.h) — No non-negative validation during deserialization.
  2. RecvCreateExternalTextureSource — No size validation.
  3. ExternalTextureSourceHost::Create — No size validation.
  4. createPlane lambda — No bounds check before static_cast<uint32_t>.
  5. wgpu restrict_limits() (gfx/wgpu_bindings/src/server.rs:52-74) — Caps max_texture_dimension_2d to 32767, so device_create_texture would likely reject the oversized texture. However, this error is not checked by the caller, and execution continues to queue_write_texture.

There is no validation at any point between IPC receipt and the unsafe cast.

Suggested Fix

Validate dimensions immediately after IPC receipt in RecvCreateExternalTextureSource:

if (aDesc.mSize.width <= 0 || aDesc.mSize.height <= 0) {
return IPC_FAIL(this, "Invalid ExternalTexture dimensions");
}

Additionally, the createPlane lambda should check the error from wgpu_server_device_create_texture before proceeding to wgpu_server_queue_write_texture.

Test Case

GTest to demonstrate the issue (can be added to dom/webgpu/tests/gtest/):

TEST(WebGPUExternalTexture, NegativeDimensionsPassDeserialization)
{
ExternalTextureSourceDescriptor desc{};
desc.mSize = gfx::IntSize(-1, -1);
uint32_t castWidth = static_cast<uint32_t>(desc.mSize.width);
uint32_t castHeight = static_cast<uint32_t>(desc.mSize.height);
EXPECT_EQ(castWidth, 4294967295u); // 0xFFFFFFFF
EXPECT_EQ(castHeight, 4294967295u);
EXPECT_GT(castWidth, 32767u); // exceeds MAX_TEXTURE_EXTENT by ~131000x
}

TEST(WebGPUExternalTexture, IPCRoundTripPreservesNegativeDimensions)
{
ExternalTextureSourceDescriptor original{};
original.mSize = gfx::IntSize(-1, -32768);
IPC::MessageWriter writer;
IPC::ParamTraits<ExternalTextureSourceDescriptor>::Write(&writer, original);
ExternalTextureSourceDescriptor deserialized{};
IPC::MessageReader reader(writer.WriterData());
bool readOk = IPC::ParamTraits<ExternalTextureSourceDescriptor>::Read(&reader, &deserialized);
EXPECT_TRUE(readOk); // Read succeeds — no validation
EXPECT_EQ(deserialized.mSize.width, -1);
EXPECT_EQ(deserialized.mSize.height, -32768);
}

Both tests will PASS on current trunk, confirming that negative int32_t values survive IPC deserialization without validation, and static_cast<uint32_t> produces dimensions ~131,000x larger than the wgpu safety limit.

CC list accessible: false
Not accessible to reporter

Jim could you have a look?

Flags: needinfo?(jimb)

Tentatively triaging as S2.

Severity: -- → S2
Flags: needinfo?(jimb)
CC list accessible: true
Accessible to reporter

gtests are not the requested "test that shows that this problem can actually occur in the context of a Firefox binary". Some ways to demonstrate there is an actual security problem in Firefox are:

  • A test case that produces a segfault (not indicative of a security problem in all cases, so still requires some supporting analysis and discussion).
  • A test case that produces an error in an ASAN build.
  • A test case along with a Firefox source patch adding assertions that detect a security-relevant invalid condition produced by the test case.

Simply demonstrating that deserialization produces invalid values is not sufficient to show that there is an actual bug. The GPU process assumes that all messages from the content process are controlled by the attacker, and thus is always supposed to validate the messages thoroughly after deserialization.

A proper bug report may assume a compromised content process, but it must go on to show that that actually leads to adverse consequences in the GPU process, like those Andy describes, rather than being caught by subsequent validation steps.

To report a bug that assumes a compromised content process, your report must include:

  • A patch to content process code, making it behave in a compromised fashion

  • A web page, ideally just an HTML file, that we can visit with the browser and have it use the patched code to make the GPU process or the browser's main process exhibit ASAN errors, or at least crash in an uncontrolled way (i.e. not a RELEASE_ASSERT or Rust panic).

I've reproduced the issue with a patched ASAN debug build on Linux (Ubuntu 22.04 / WSL2, Clang 20.1.8, llvmpipe software Vulkan).

Evidence

Content-process patch: override intrinsicSize with gfx::IntSize(-1, -1) in ExternalTextureSourceClient::Create() (ExternalTexture.cpp line 307).

GPU process stderr output confirms the unvalidated cast:

[POC-GPU] CreateFromBufferDesc: size=320x240, cast_width=320, cast_height=240
[POC-GPU] GetExternalTextureDescriptor: mSize=-1x-1, cast_width=4294967295, cast_height=4294967295

This proves:

  • Negative int32_t values from content process arrive at the GPU process through IPC with zero validation
  • WebGPUParent::RecvCreateExternalTextureSource() passes them straight through
  • static_cast<uint32_t>(-1) produces 4294967295 (0xFFFFFFFF) in both CreateFromBufferDesc() and GetExternalTextureDescriptor()

Why no ASAN crash on Linux

On Linux, the CreateFromBufferDesc() path is used. The corrupted dimensions (width=4294967295) reach wgpu_server_device_create_texture() via FFI, but wgpu's internal Rust validation rejects the texture creation before GPU memory allocation occurs. So the unsafe cast happens, but the Rust layer acts as an unintentional safety net on this specific path.

Paths likely to produce ASAN crash

  1. DXGI path (Windows): CreateFromDXGITextureHost() and CreateFromDXGIYCbCrTextureHost() interact directly with DirectX texture handles. The corrupted dimensions may be used before reaching wgpu validation, as the texture already exists from the DXGI handle — only the descriptor dimensions are wrong.

  2. codedSize path: If a compromised content process also sends negative codedSize, this controls buffer dimensions passed to wgpu_server_queue_write_texture() for the actual pixel data copy. The copy stride/size calculations with uint32_t overflow could cause OOB writes before wgpu can validate.

  3. MacIOSurface path: Similar to DXGI — CreateFromMacIOSurfaceTextureHost() wraps an existing IOSurface handle with corrupted descriptor dimensions.

I don't have a Windows/macOS ASAN build environment to test these paths, but the C++ code is clearly wrong at all 12+ cast sites — no validation before static_cast<uint32_t> on IPC-received signed values.

Trigger

Attached trigger.html uses canvas.captureStream() → <video> → device.importExternalTexture() to exercise the IPC code path. Works with software Vulkan (llvmpipe). ExternalTexture required force-enabling on Linux (gated behind platform checks).

Suggested fix

// WebGPUParent::RecvCreateExternalTextureSource():
if (aDesc.mSize.width <= 0 || aDesc.mSize.height <= 0 ||
    aDesc.mSize.width > 16384 || aDesc.mSize.height > 16384) {
  return IPC_FAIL(this, "Invalid ExternalTextureSource dimensions");
}

poc secret repo: https://raw.githubusercontent.com/marz-hunter/secrets/refs/heads/main/poc.zip?token=GHSAT0AAAAAADY7BOESJQAX3Q3LZH5SI3QK2QLX5AA

Flags: needinfo?(abdullahmarzuki22)

github token may be expired after the specified time, I will provide it again, because the comment feature does not have an attachment feature

See Also: → 2040782

I can't access the githubusercontent link, but from the comment, it doesn't sound like it contains a reproduction with an ASAN build. In the comment, I only see some speculation about where an error might appear with an ASAN build.

Please review the information in and linked from https://www.mozilla.org/en-US/security/client-bug-bounty/, in particular:

Submissions must ... [demonstrate] the ability to perform an unauthorized action or obtain access to otherwise-restricted information.

While these casts are questionable, and probably ought to be avoided as a matter of defensive coding, that is not the same as a security vulnerability.

Group: gfx-core-security
Status: UNCONFIRMED → RESOLVED
Closed: 6 months ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: