Skip to content

Bounds-check scatter indices in the WASM backend - #8743

Open
shaggyinsomniac wants to merge 1 commit into
tensorflow:masterfrom
shaggyinsomniac:fix/wasm-scatter-bounds
Open

Bounds-check scatter indices in the WASM backend#8743
shaggyinsomniac wants to merge 1 commit into
tensorflow:masterfrom
shaggyinsomniac:fix/wasm-scatter-bounds

Conversation

@shaggyinsomniac

Copy link
Copy Markdown

The WASM scatter kernels compute an output pointer directly from the caller's index values with no range validation, so an out-of-range index writes outside the output allocation in wasm linear memory. A negative index wraps around and writes before the allocation. The CPU backend throws an error for the same inputs, so the backends are also inconsistent:

// wasm: tf.tensorScatterUpdate(tf.zeros([100]), [[-1]], [42.0]) — writes silently
// cpu: throws "Invalid indices: ... does not index into 100"

This change validates indices in the ScatterNd and TensorScatterUpdate wrappers before entering wasm, throwing the same error as the CPU backend, and adds bounds guards in the C++ kernels as defense in depth. Same validation-gap class as #8730 (WASM CropAndResize), but on the write side.

Added a test in index_test.ts mirroring the placement of the CropAndResize test.

The wasm scatter kernels computed an output pointer directly from
attacker-controlled index values with no range validation, so an
out-of-range index (including a negative one, which sign-extends)
wrote outside the output allocation in wasm linear memory. The CPU
backend throws 'Invalid indices' for the same inputs.

Validate indices in the ScatterNd/TensorScatterUpdate wrappers before
entering wasm, matching the CPU backend's error, and guard the C++
kernels as defense in depth. Mirrors the CropAndResize bounds fix.
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.

1 participant