Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate texture copy ranges earlier to prevent integer overflow #3090

Merged
merged 6 commits into from Dec 1, 2022

Conversation

nical
Copy link
Contributor

@nical nical commented Oct 10, 2022

Checklist

  • Run cargo clippy.
  • Add change to CHANGELOG.md. See simple instructions inside file.

Connections

https://bugzilla.mozilla.org/show_bug.cgi?id=1791806

Description

validate_texture_copy_range has a number of important checks on sizes that we want to make as early as possible to prevent integer overflows in other places such as transfer::extract_texture_selector which computes copy_texture.origin.z + copy_size.depth_or_array_layers (overflow found by a fuzzer, as always).

The fix is to call validate_texture_copy_range earlier, in particular before extract_texture_selector.

Unfortunately it led to more code churn than I would have liked. Thankfully it is mechanical and simple: Rather than getting the texture reference from Tracker::set_single, we grab it earlier (for validation) and pass it to set_single instead of the texture guard.
As a bonus we are looking texture texture up less often than we used to, and we do all of the validation before starting to transition some states which sounds safer to me.

Testing

I did actually add a test this time around!

@nical nical changed the title Transfer valid early Validate texture copy ranges earlier to prevent integer overflow Oct 10, 2022
Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor nit that would have required time travel - lgtm

wgpu/tests/transfer.rs Outdated Show resolved Hide resolved
validate_texture_copy_range has a number of important checks on sizes that we want to make as early as possible to prevent integer overflows in other places such as transfer::extract_texture_selector which computes copy_texture.origin.z + copy_size.depth_or_array_layers (overflow found by fuzzers as always).

The fix is to call validate_texture_copy_range earlier, in particular before extract_texture_selector.
Unfortunately it led to more code churn than I would have liked. Thankfully it is mechanical and simple: Rather than getting the texture reference from Tracker::set_single, we grab it earlier (for validation) and pass it to set_single instead of the texture guard.
As a bonus we are looking texture texture up less often than we used to.
@codecov-commenter
Copy link

Codecov Report

Merging #3090 (d3091f9) into master (46b1216) will decrease coverage by 18.26%.
The diff coverage is 69.11%.

❗ Current head d3091f9 differs from pull request most recent head 3c8ed51. Consider uploading reports for the commit 3c8ed51 to get more accurate results

@@             Coverage Diff             @@
##           master    #3090       +/-   ##
===========================================
- Coverage   65.41%   47.14%   -18.27%     
===========================================
  Files          82       82               
  Lines       39455    39436       -19     
===========================================
- Hits        25809    18592     -7217     
- Misses      13646    20844     +7198     
Impacted Files Coverage Δ
wgpu-core/src/command/transfer.rs 75.49% <64.00%> (+5.46%) ⬆️
wgpu-core/src/device/queue.rs 70.43% <75.00%> (-0.27%) ⬇️
wgpu-core/src/command/clear.rs 89.58% <100.00%> (-0.04%) ⬇️
wgpu-core/src/track/texture.rs 59.46% <100.00%> (-21.54%) ⬇️
wgpu-hal/src/gles/device.rs 0.00% <0.00%> (-80.19%) ⬇️
wgpu-hal/src/vulkan/adapter.rs 0.00% <0.00%> (-77.52%) ⬇️
wgpu-hal/src/vulkan/device.rs 0.00% <0.00%> (-75.42%) ⬇️
wgpu-hal/src/gles/conv.rs 0.00% <0.00%> (-74.36%) ⬇️
wgpu-hal/src/gles/adapter.rs 9.70% <0.00%> (-73.90%) ⬇️
wgpu-hal/src/vulkan/conv.rs 1.11% <0.00%> (-70.66%) ⬇️
... and 21 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@cwfitzgerald cwfitzgerald enabled auto-merge (squash) December 1, 2022 01:10
@cwfitzgerald cwfitzgerald merged commit e90aace into gfx-rs:master Dec 1, 2022
@nical nical deleted the transfer-valid-early branch December 1, 2022 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants