Skip to content

tmfile: bounds-check offsets in the tm2 tensor loader (#1449)#1450

Open
professor-moody wants to merge 1 commit into
OAID:tengine-litefrom
professor-moody:fix-tmfile-oob-reads
Open

tmfile: bounds-check offsets in the tm2 tensor loader (#1449)#1450
professor-moody wants to merge 1 commit into
OAID:tengine-litefrom
professor-moody:fix-tmfile-oob-reads

Conversation

@professor-moody

Copy link
Copy Markdown

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_len is already stored by both the file and memory load paths), and routes the reads in load_graph_tensors through it: the tensor and buffer offset tables, the per-tensor offset, the tensor name string (offset and length), the dims vector, and the buffer_id index (checked against v_num before indexing the offsets array).

I started with load_graph_tensors since 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-lite HEAD 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.

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.
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