fix(data): reject raw tensor whose data length != declared shape (#2390)#2413
Open
czoli1976 wants to merge 1 commit into
Open
fix(data): reject raw tensor whose data length != declared shape (#2390)#2413czoli1976 wants to merge 1 commit into
czoli1976 wants to merge 1 commit into
Conversation
`Tensor::from_raw_dt_align` allocated `∏(shape) × sizeof(dt)` bytes and then `copy_from_slice`-d the caller-supplied content into it with no length check. A mismatched length (e.g. an ONNX initializer whose `raw_data` byte count does not match its `dims`) panicked in `copy_from_slice`, aborting model load in both debug and release builds (DoS on any process loading untrusted models). Validate the content length against the allocated tensor before copying and return a `TractResult` error instead of panicking. This is the single choke point for all `from_raw` callers (ONNX, NNEF, ...). Fixes sonos#2390. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2390.
Tensor::from_raw_dt_alignallocated∏(shape) × sizeof(dt)bytes and thencopy_from_slice-d the caller-supplied content into it with no length check. When the two lengths differ — e.g. a crafted or malformed.onnxwhose initializerraw_databyte count does not match its declareddims—copy_from_slicepanics, aborting model load in both debug and release builds. Since tract loads externally-supplied ONNX, this is a reachable DoS.This validates the content length against the freshly-allocated tensor and returns a
TractResulterror instead of panicking. The check sits atfrom_raw_dt_align, the single choke point for allfrom_rawcallers (ONNX, NNEF, …), so every raw-load path is covered.Reproduction (before this PR)
The 94-byte PoC from #2390 (
raw_dataof 12 bytes against a shape needing 440):After this PR the same file loads to a graceful
Err(no panic), verified in release.Change
Tests
from_raw_rejects_length_mismatchindata(short, long, and exact-match cases).cargo test -p tract-data --lib(140) andcargo test -p tract-onnx --lib(10) pass; the end-to-end PoC load now returnsErrinstead of panicking.🤖 Generated with Claude Code