Repository navigation
Bounds-check the FlatTensor root table offset before reading it - #22118
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22118
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 Cancelled JobAs of commit df10f77 with merge base 1afd07f ( CANCELLED JOB - The following job was cancelled. Please retry:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
aed2145 to
7d4b64e
Compare
7d4b64e to
df10f77
Compare
|
@claude review the PR |
|
Claude finished @shoumikhin's task in 4m 10s —— View job PR Review: Bounds-check the FlatTensor root table offset
Verdict: LGTM. This is a correct, well-scoped hardening that faithfully mirrors the tested guard in What I verified
Non-blocking notes
None of these block merge. The core change is sound and the new |
df10f77 to
d2740fd
Compare
d2740fd to
a2e2ad3
Compare
|
Thanks @claude. Changes made:
On the two nits, keeping as-is on purpose:
The partial-hardening framing (note 1) is exactly right: this closes the root-offset OOB specifically, mirroring |
|
Claude finished @shoumikhin's task in 42s —— View job Re: review follow-upThanks for the thorough responses — all three resolutions look right to me.
And yes — the partial-hardening framing (note 1) is the key point: this closes the specific root-offset OOB and mirrors the |
FlatTensorDataMap::load() calls GetFlatTensor() and then reads named_data(), segments() (and, with the version gate, version()) straight away. GetFlatTensor() takes the uoffset_t at the start of the buffer as the root-table offset and returns buf + offset; the first field access then walks that table's vtable. Nothing on this path runs full flatbuffer verification, so a corrupt or truncated file with a bad root offset makes those accessors dereference memory outside the buffer. The program loader already guards against this (runtime/executor/program.cpp). Mirror that here: after the identifier and alignment checks, confirm the buffer is at least a flatbuffer header long and that the root offset stays within the buffer with room for a vtable soffset, returning InvalidExternalData otherwise. FlatTensor is not size-prefixed (both writers finish the buffer plain and the reader uses GetFlatTensor, not the size-prefixed variant), so the offset is at byte 0, exactly as in the program case. Add a regression test that copies a valid file into a max-aligned buffer, points the root offset past the end, and checks load() returns InvalidExternalData. Without the check that read is an out-of-bounds vtable access under ASan.
a2e2ad3 to
ff51978
Compare
|
@claude review the PR |
|
Claude finished @shoumikhin's task in 1m 26s —— View job PR Review: Bounds-check the FlatTensor root table offset
Verdict: LGTM. Re-reviewed against the current What I re-verified
Note on the earlier follow-up (
|
Summary
FlatTensorDataMap::load()reads the flatbuffer root table without first bounds-checking the root offset:GetFlatTensor()interprets theuoffset_tat the start of the buffer as the root-table offset and returnsbuf + offset. The first field access then walks that table's vtable. Nothing on this path runs full flatbuffer verification, so a corrupt or truncated PTD file with a bad root offset makes these accessors dereference memory outside the buffer (an OOB read, flagged by ASan).The program loader already guards against exactly this in
runtime/executor/program.cpp. This change mirrors that guard for PTD files.What changed
After the existing identifier and alignment checks, before
GetFlatTensor():uoffset_t+ file identifier).>= kMinBufferSizeand leaves room for a vtablesoffset_twithin the buffer.InvalidExternalDataotherwise (the same error the surrounding checks use).FlatTensor is not size-prefixed — both writers finish the buffer plain (
builder.Finishin C++, defaultflatcin Python) and the reader usesGetFlatTensor(not the size-prefixed variant) — so the root offset is at byte 0, exactly as in the program case.Scope
This is a pre-existing hardening, independent of the schema-version work in #22114 (the version gate this protects is additive; the OOB path exists today via
named_data()/segments()).Test plan
New
FlatTensorDataMapTest.RejectsOutOfBoundsRootOffset: copies a valid PTD into a max-aligned buffer, overwrites only the root offset with a value past the end (leaving identifier + alignment intact), and assertsload()returnsInvalidExternalData. Without the check, that same input is an out-of-bounds vtable read under ASan.