tmfile: bounds-check offsets in the tm2 tensor loader (#1449)#1450
Open
professor-moody wants to merge 1 commit into
Open
tmfile: bounds-check offsets in the tm2 tensor loader (#1449)#1450professor-moody wants to merge 1 commit into
professor-moody wants to merge 1 commit into
Conversation
The tm2 loader dereferenced file offsets and counts without checking them against the mapped size, so a crafted model caused out-of-bounds reads on load (OAID#1449). Add tm2_off_ok() and route the load_graph_tensors reads through it: the tensor/buffer offset tables, the per-tensor offset, the name string (offset and length), the dims vector, and the buffer_id index.
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.
Follows up #1449.
As reported there, the tmfile (tm2) loader dereferences offsets and counts straight from the file without checking them against the mapped size, so a crafted model triggers out-of-bounds reads while loading.
This adds a small helper,
tm2_off_ok(priv, off, sz), that returns true only when[off, off+sz)is fully inside the mapped file (priv->mem_lenis already stored by both the file and memory load paths), and routes the reads inload_graph_tensorsthrough it: the tensor and buffer offset tables, the per-tensor offset, the tensor name string (offset and length), the dims vector, and thebuffer_idindex (checked againstv_numbefore indexing the offsets array).I started with
load_graph_tensorssince that is where most of the crashes landed. The same helper should be applied to the base-pointer resolution (get_tm_file_header/model/subgraph) and the node and IO loaders (load_graph_nodes,set_graph_io_nodes,load_graph_sub_info) to close the remaining sites. Happy to extend this PR to cover those, or split it, whatever is easiest to review.Verified on
tengine-liteHEAD that a build with this patch rejects the crafted models that previously crashed in the tensor loader, and still loads the shipped benchmark models normally.