Skip to content

Check flatbuffer offset when verification is minimal#17129

Open
lucylq wants to merge 3 commits intomainfrom
lfq.check-offset-minimal-verificaiton
Open

Check flatbuffer offset when verification is minimal#17129
lucylq wants to merge 3 commits intomainfrom
lfq.check-offset-minimal-verificaiton

Conversation

@lucylq
Copy link
Contributor

@lucylq lucylq commented Feb 3, 2026

Summary

Unconditionally verify that the offset is within the flatbuffer bounds. Setting InternalConsistency is a binary size burden (20-30kb), so check this separately when we have minimal verification.

Error out if InternalConsistency is requested, but verification is disabled.

Test plan

cd build
cmake .. -DEXECUTORCH_BUILD_TESTS=ON
cmake --build . --target program_test
ctest -R program_test -V

@pytorch-bot
Copy link

pytorch-bot bot commented Feb 3, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/17129

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Awaiting Approval, 9 New Failures, 21 Pending

As of commit 13ba046 with merge base 6c02866 (image):

AWAITING APPROVAL - The following workflow needs approval before CI can run:

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 3, 2026
@github-actions
Copy link

github-actions bot commented Feb 3, 2026

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@lucylq lucylq force-pushed the lfq.check-offset-minimal-verificaiton branch from 8fc7c5f to 3b4ce0c Compare February 3, 2026 01:05
@lucylq lucylq marked this pull request as ready for review February 3, 2026 01:06
@lucylq lucylq requested a review from JacobSzwejbka as a code owner February 3, 2026 01:06
Copilot AI review requested due to automatic review settings February 3, 2026 01:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds minimal verification for flatbuffer root offsets to prevent crashes when InternalConsistency verification is disabled (to save binary size). The PR also changes the behavior when InternalConsistency verification is requested but not available - it now returns an error instead of just logging a warning.

Changes:

  • Added bounds checking for the flatbuffer root offset in minimal verification mode to catch corruption early
  • Changed behavior when InternalConsistency verification is unavailable from logging at Info level to logging at Error level and returning Error::NotSupported
  • Added a test to verify that invalid root offsets are caught by minimal verification

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
runtime/executor/program.cpp Implements minimal verification by checking root offset bounds and makes InternalConsistency unavailability an error instead of a warning
runtime/executor/test/program_test.cpp Adds test case to verify that obviously invalid root offsets are rejected during minimal verification

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +161 to +177
// Minimal verification: check that the root table offset is within bounds.
// The first 4 bytes of the flatbuffer contain an offset to the root table.
ET_CHECK_OR_RETURN_ERROR(
program_data->size() >= sizeof(flatbuffers::uoffset_t),
InvalidProgram,
"Program data size %zu is too small for flatbuffer header",
program_data->size());
uint32_t root_offset = flatbuffers::ReadScalar<flatbuffers::uoffset_t>(
program_data->data());
// The root table is at buf + root_offset, and must have at least a
// vtable offset (soffset_t) at that position.
ET_CHECK_OR_RETURN_ERROR(
root_offset <= program_data->size() - sizeof(flatbuffers::uoffset_t),
InvalidProgram,
"Root table offset %u exceeds program size %zu",
root_offset,
program_data->size());
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new Minimal verification check only enforces an upper bound on the flatbuffer root table offset. Since this program format uses a FlatBuffers file_identifier (checked via ProgramBufferHasIdentifier), Minimal verification should also ensure the root offset cannot point into the header/identifier region (e.g., require root_offset >= sizeof(uoffset_t) + file identifier length, and that the buffer is at least that large). Without a lower-bound check, a corrupted root_offset like 4 can still pass this check and cause GetProgram() to interpret the identifier bytes as a table, risking out-of-bounds reads later during parsing.

Copilot uses AI. Check for mistakes.
lucylq and others added 3 commits March 13, 2026 12:00
Add a preliminary check that the buffer is large enough to contain the
flatbuffer header before reading the root offset, and ensure the root
table position has room for at least a vtable offset.
The FlatBuffer root table offset was only validated when
InternalConsistency verification was both requested and compiled in.
With Minimal verification (the default), a malicious PTE file could set
the root offset to point outside the buffer, causing OOB heap reads.

Add a Minimal verification path that validates:
- Lower bound: root_offset >= header size (offset + file identifier),
  preventing the root table from overlapping the header region.
- Upper bound: root_offset <= size - sizeof(uoffset_t), ensuring room
  for at least the vtable offset at the root table position.

Also return Error::NotSupported when InternalConsistency verification
is requested but compiled out (ET_ENABLE_PROGRAM_VERIFICATION=0),
instead of silently continuing with no verification.

Addresses TOB-EXECUTORCH-12.

This PR was authored with the assistance of Claude.
Copilot AI review requested due to automatic review settings March 13, 2026 19:07
@lucylq lucylq force-pushed the lfq.check-offset-minimal-verificaiton branch from be5ce97 to 13ba046 Compare March 13, 2026 19:07
@lucylq lucylq mentioned this pull request Mar 13, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +169 to +176
// Minimal verification: check that the root table offset is within bounds.
// The first 4 bytes of the flatbuffer contain an offset to the root table.
ET_CHECK_OR_RETURN_ERROR(
program_data->size() >= sizeof(flatbuffers::uoffset_t),
InvalidProgram,
"Program data size %zu is too small for flatbuffer header",
program_data->size());
uint32_t root_offset = flatbuffers::ReadScalar<flatbuffers::uoffset_t>(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants