Closed Bug 2000823 Opened 1 year ago Closed 10 months ago

Heap-use-after-free with WebGPU copyTextureToTexture and Mesa drivers

Categories

(Core :: Graphics: WebGPU, defect, P1)

Unspecified
Linux
defect

Tracking

()

RESOLVED FIXED
148 Branch
Tracking Status
firefox-esr115 --- unaffected
firefox-esr140 --- unaffected
firefox145 --- disabled
firefox146 --- disabled
firefox147 --- disabled
firefox148 --- fixed

People

(Reporter: attekett, Assigned: aleiserson)

References

Details

(4 keywords, Whiteboard: [fixed by 2005294])

Attachments

(1 file)

Attached file minimal-repro.html —

Tested on:
-OS: Ubuntu 24.04
-Mesa: ASAN build Mesa 25.0.7 (git-742a20f48c). Also tested to reproduce on current newest for Ubuntu 24.04, Mesa 25.0.7.
-Firefox: fuzzfetch -a --fuzzing

  Version: 147.0a1
  BuildID=20251114094637
  SourceStamp=4e885473cb08ec97b42aa99ea83cc2485a463d31

Reproducing file as an attachment.

I suspect this issue to be a race condition where texture.destroy frees the texture before commands from queue.submit have finished. If you put sourceTexture.destroy() in a setTimeout, it will crash with timeout 0, but anything >0 and it doesn't crash anymore.

ASAN-trace with ASAN-built Mesa:

=================================================================
==541676==ERROR: AddressSanitizer: heap-use-after-free on address 0x7d1ff73685c8 at pc 0x7bfeade4fdc6 bp 0x7bfe80bcc0d0 sp 0x7bfe80bcc0c8
READ of size 4 at 0x7d1ff73685c8 thread T141
    #0 0x7bfeade4fdc5 in vk_object_base_assert_valid /tmp/vulkan-build/mesa/build/../src/vulkan/runtime/vk_object.h:120:4
    #1 0x7bfeade4fdc5 in lvp_image_from_handle /tmp/vulkan-build/mesa/build/../src/gallium/frontends/lavapipe/lvp_private.h:704:1
    #2 0x7bfeade4fdc5 in handle_copy_buffer_to_image /tmp/vulkan-build/mesa/build/../src/gallium/frontends/lavapipe/lvp_execute.c:2363:4
    #3 0x7bfeade4fdc5 in lvp_execute_cmd_buffer /tmp/vulkan-build/mesa/build/../src/gallium/frontends/lavapipe/lvp_execute.c:5057:10
    #4 0x7bfeade28430 in lvp_execute_cmds /tmp/vulkan-build/mesa/build/../src/gallium/frontends/lavapipe/lvp_execute.c:5395:4
...

0x7d1ff73685c8 is located 8 bytes inside of 288-byte region [0x7d1ff73685c0,0x7d1ff73686e0)
freed by thread T73 here:
    #0 0x5555556c37c6 in __interceptor_free _asan_rtl_:3
    #1 0x7bffe3f9a6b0 in ash::device::Device::destroy_image::h4fe97e07efd3f066 /builds/worker/checkouts/gecko/third_party/rust/ash/src/device.rs:1153:9
    #2 0x7bffe3f9a6b0 in wgpu_hal::vulkan::device::_$LT$impl$u20$wgpu_hal..Device$u20$for$u20$wgpu_hal..vulkan..Device$GT$::destroy_texture::h0c7c441f067e1be5 /builds/worker/checkouts/gecko/third_party/rust/wgpu-hal/src/vulkan/device.rs:1208:38
...
previously allocated by thread T73 here:
    #0 0x5555556c3a64 in __interceptor_malloc _asan_rtl_:3
    #1 0x7bfeadcc5735 in vk_alloc /tmp/vulkan-build/mesa/build/../src/vulkan/util/vk_alloc.h:48:11
    #2 0x7bfeadcc5735 in vk_alloc2 /tmp/vulkan-build/mesa/build/../src/vulkan/util/vk_alloc.h:0:0
    #3 0x7bfeadcc5735 in vk_zalloc2 /tmp/vulkan-build/mesa/build/../src/vulkan/util/vk_alloc.h:143:16
    #4 0x7bfeadcc5735 in vk_image_create /tmp/vulkan-build/mesa/build/../src/vulkan/runtime/vk_image.c:141:7
    #5 0x7bfeade5eb6a in lvp_image_create /tmp/vulkan-build/mesa/build/../src/gallium/frontends/lavapipe/lvp_image.c:69:12
    #6 0x7bfeade5e74f in lvp_CreateImage /tmp/vulkan-build/mesa/build/../src/gallium/frontends/lavapipe/lvp_image.c:259:11
    #7 0x7bffe3f98780 in ash::device::Device::create_image::hcef909c6d63cbbe9 /builds/worker/checkouts/gecko/third_party/rust/ash/src/device.rs:2596:9
...

With system Mesa:

==55338==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x7bff32eb7cf4 bp 0x7bff2fa64910 sp 0x7bff2fa648b8 T122)
==55338==The signal is caused by a READ memory access.
==55338==Hint: this fault was caused by a dereference of a high value address (see register values below).  Disassemble the provided pc to learn which register was used.
    #0 0x7bff32eb7cf4 in util_run_tests ??:?
    #1 0x7bff32eb9a8d in util_run_tests ??:?
    #2 0x7bff32eb7982 in util_run_tests ??:?
    #3 0x7bff32eb7ce8 in util_run_tests ??:?
    #4 0x7bff32ccfffa in vk_icdGetInstanceProcAddr ??:?
    #5 0x7bff32cd81f8 in vk_icdGetInstanceProcAddr ??:?
...
==55338==Register values:
rax = 0x00007f0ff7accce8  rbx = 0x0000000000000000  rcx = 0x0000000000000000  rdx = 0x0000000000000000
rdi = 0x00007f0ff7acc800  rsi = 0xe5e5e5e5e5e5e5e5  rbp = 0x00007bff2fa64910  rsp = 0x00007bff2fa648b8
 r8 = 0x0000000000000001   r9 = 0x0000000000000000  r10 = 0xe5e5e5e5e5e5e5e5  r11 = 0x00007c6ff8238e50
r12 = 0x00007daff6f4e008  r13 = 0xe5e5e5e5e5e5e5e5  r14 = 0x0000000000000000  r15 = 0x00007f0ff7acc800
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/usr/lib/x86_64-linux-gnu/libvulkan_lvp.so+0x2b7cf4) (BuildId: 303ca66f5bb9d4347b8481428adecbb49e9ce623)
Thread T122 created by T74 here:
    #0 0x5555556a7f01 in ___interceptor_pthread_create _asan_rtl_:3
    #1 0x7bff32d763b4 in std::vector<unsigned char, std::allocator<unsigned char> >::_M_default_append(unsigned long) ??:?
    #2 0x7bff32c8184a in ?? ??:0
    #3 0x7bff32c83bcc in ?? ??:0
    #4 0x7bff32cc8364 in vk_icdGetInstanceProcAddr ??:?
    #5 0x7bff5ce306fd in ?? ??:0
    #6 0x7bff5ce2d600 in ?? ??:0
    #7 0x7bff5ce28af4 in ?? ??:0
    #8 0x7bff5ce390f2 in vkCreateDevice ??:0:0
    #9 0x7bffe3f78d40 in ash::instance::Instance::create_device::h459f9d1f4e71d6ca /builds/worker/checkouts/gecko/third_party/rust/ash/src/instance.rs:369:22
...

WebGPU Info with ASAN-built Mesa:

{
  "navigator.gpu.requestAdapter({\"forceFallbackAdapter\":true})": {
    "info": {
      "architecture": "",
      "description": "",
      "device": "",
      "isFallbackAdapter": true,
      "subgroupMaxSize": 128,
      "subgroupMinSize": 4,
      "vendor": "",
      "wgpuBackend": "Vulkan",
      "wgpuDevice": 0,
      "wgpuDeviceType": "Cpu",
      "wgpuDriver": "llvmpipe",
      "wgpuDriverInfo": "Mesa 25.0.7 (git-742a20f48c) (LLVM 15.0.7)",
      "wgpuName": "llvmpipe (LLVM 15.0.7, 256 bits)",
      "wgpuVendor": 65541
    },
    "features": [
      "bgra8unorm-storage",
      "core-features-and-limits",
      "depth-clip-control",
      "depth32float-stencil8",
      "float32-filterable",
      "indirect-first-instance",
      "rg11b10ufloat-renderable",
      "shader-f16",
      "texture-compression-bc",
      "texture-compression-bc-sliced-3d",
      "timestamp-query"
    ],
    "limits": {
      "maxBindGroups": 8,
      "maxBindGroupsPlusVertexBuffers": 24,
      "maxBindingsPerBindGroup": 1000,
      "maxBufferSize": 1073741824,
      "maxColorAttachmentBytesPerSample": 32,
      "maxColorAttachments": 8,
      "maxComputeInvocationsPerWorkgroup": 1024,
      "maxComputeWorkgroupSizeX": 1024,
      "maxComputeWorkgroupSizeY": 1024,
      "maxComputeWorkgroupSizeZ": 1024,
      "maxComputeWorkgroupStorageSize": 32768,
      "maxComputeWorkgroupsPerDimension": 65535,
      "maxDynamicStorageBuffersPerPipelineLayout": 1000000,
      "maxDynamicUniformBuffersPerPipelineLayout": 1000000,
      "maxInterStageShaderVariables": 16,
      "maxSampledTexturesPerShaderStage": 64,
      "maxSamplersPerShaderStage": 64,
      "maxStorageBufferBindingSize": 134217728,
      "maxStorageBuffersPerShaderStage": 64,
      "maxStorageTexturesPerShaderStage": 64,
      "maxTextureArrayLayers": 2048,
      "maxTextureDimension1D": 16384,
      "maxTextureDimension2D": 16384,
      "maxTextureDimension3D": 4096,
      "maxUniformBufferBindingSize": 65536,
      "maxUniformBuffersPerShaderStage": 64,
      "maxVertexAttributes": 32,
      "maxVertexBufferArrayStride": 2048,
      "maxVertexBuffers": 16,
      "minStorageBufferOffsetAlignment": 32,
      "minUniformBufferOffsetAlignment": 32
    }
  }
}
Group: core-security → gfx-core-security
Priority: -- → P1
Severity: -- → S2
Assignee: nobody → aleiserson

I can reproduce this. I think it will be easy to fix.

Note that the attached repro has the non-zero setTimeout mentioned in the description, this needs to be removed in order to reproduce.

Status: NEW → ASSIGNED

Sorry. Must have accidentally attached the version I used to test the timing.

Fix is in https://github.com/gfx-rs/wgpu/pull/8567. The problem is that internal bookkeeping structures were in an inconsistent state at the point an early return is taken for zero-size transfers, so the texture was scheduled to be initialized, but not recorded as needing to be kept alive until that initialization happens. The fix is to relocate that early out before any side effects of the transfer (but after parameter validation, so that all errors required by the spec are correctly reported).

This is similar to the problem in bug 1974924, comment 22, but in a different context.

Like bug 1974924, this only affects Linux. The other platforms have reference counting in the drivers that is sufficient to avoid the issue. Marking Firefox 147 as affected while it is nightly.

Depends on: webgpu-update-wgpu
See Also: → 1974924
Depends on: 2005294
No longer depends on: webgpu-update-wgpu

Atte, could you verify that this is fixed in Nightly?

Flags: needinfo?(attekett)

Doesn't reproduce anymore on central:

fuzzfetch -a -n firefox
Identified task: https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/gecko.v2.mozilla-central.latest.firefox.linux64-asan-opt
> Task ID: Tpa7suhsRaKFiofOjbxzPw
> Rank: 1767518779
> Changeset: 39305265f6c10fc31953222dad4d9319ac35bb28
> Build ID: 20260104092619
> Downloading: https://firefox-ci-tc.services.mozilla.com/api/queue/v1/task/Tpa7suhsRaKFiofOjbxzPw/artifacts/public/build/target.tar.xz (364.52MiB total)

FWIW, tested against central, because fuzzfetch doesn't support Nightly as a branch keyword: --branch {central,release,beta,esr-stable,esr-next,try,autoland}

Flags: needinfo?(attekett)

Adding bounty flag requested by the reporter

Flags: sec-bounty?
Whiteboard: [fixed by 2005294]

Confirmed fixed by reporter.

Status: ASSIGNED → RESOLVED
Closed: 10 months ago
Resolution: --- → FIXED
Group: gfx-core-security → core-security-release
Target Milestone: --- → 148 Branch
QA Whiteboard: [sec] [qa-triage-done-c149/b148]
Flags: sec-bounty? → sec-bounty+
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: