Skip to content

Fix XNNPACK FlatBuffer verification and header bounds checking#18784

Open
lucylq wants to merge 1 commit intomainfrom
security33-34
Open

Fix XNNPACK FlatBuffer verification and header bounds checking#18784
lucylq wants to merge 1 commit intomainfrom
security33-34

Conversation

@lucylq
Copy link
Copy Markdown
Contributor

@lucylq lucylq commented Apr 8, 2026

  1. Add flatbuffer verification to xnnpack graph
  2. Check the flatbuffer and constant data region are valid (within flatbuffer size, and do not overlap with each other)

This PR was authored with the assistance of Claude.

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot bot commented Apr 8, 2026

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 New Failure, 2 Unrelated Failures

As of commit 40c83d4 with merge base 21d9c64 (image):

NEW FAILURE - The following job has failed:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Apr 8, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 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 marked this pull request as ready for review April 8, 2026 23:48
@lucylq lucylq requested a review from digantdesai as a code owner April 8, 2026 23:48
@lucylq lucylq requested review from GregoryComer and Copilot April 8, 2026 23:48
Copy link
Copy Markdown
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 PR hardens XNNPACK delegate deserialization by validating wrapper-header regions and verifying the embedded FlatBuffer before traversing it, aiming to prevent OOB reads on malformed/corrupt inputs.

Changes:

  • Add bounds/overlap checks for the flatbuffer and constant-data regions in XNNHeader::Parse.
  • Track the effective flatbuffer byte length and run FlatBuffers verifier before GetXNNGraph access in XNNCompiler::compileModel.

Reviewed changes

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

File Description
backends/xnnpack/runtime/XNNHeader.cpp Adds header-based range checks for flatbuffer/constant-data offsets & sizes, including non-overlap enforcement.
backends/xnnpack/runtime/XNNCompiler.cpp Introduces FlatBuffer verification using the computed flatbuffer size before parsing the graph.

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

Comment on lines +1849 to +1856
// Verify the FlatBuffer data integrity before accessing it. Without this,
// malformed data could cause out-of-bounds reads when traversing the
// FlatBuffer's internal offset tables.
flatbuffers::Verifier verifier(flatbuffer_data, flatbuffer_size);
ET_CHECK_OR_RETURN_ERROR(
fb_xnnpack::VerifyXNNGraphBuffer(verifier),
DelegateInvalidCompatibility,
"FlatBuffer verification failed; data may be truncated or corrupt");
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

flatbuffers::Verifier is run via fb_xnnpack::VerifyXNNGraphBuffer(verifier), but the generated verifier typically enforces the schema’s file_identifier. In this repo, the runtime schema uses file_identifier "XN00" while the Python serializer produces buffers with identifier XN01 (and this function already intends to support both). As written, verification will likely reject valid XN01 payloads and break backward compatibility.

Consider verifying the buffer without enforcing the identifier (e.g., verifier.VerifyBuffer<fb_xnnpack::XNNGraph>(nullptr)), or selecting the expected identifier string ("XN00"/"XN01") based on the earlier identifier check and passing it to VerifyBuffer<...>(expected_id) instead of calling the generated VerifyXNNGraphBuffer.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you double check BC with this change? We don't have good CI coverage for this currently (I need to fix this).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah this is updated to use verifier.VerifyBuffer.. which doesn't specify the magic number

Comment on lines 1846 to 1847
"XNNPACK Delegate Serialization Format version identifier '%.4s' != expected XN00 or XN01'",
flatbuffers::GetBufferIdentifier(flatbuffer_data));
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

flatbuffers::GetBufferIdentifier(flatbuffer_data) is used before any minimum-length/verification check. If flatbuffer_size is < 8 (e.g., truncated/corrupt input), this can read past the provided buffer. Add an explicit minimum-size check before calling it (at least sizeof(flatbuffers::uoffset_t) + flatbuffers::kFileIdentifierLength), or perform verification/bounds checking prior to identifier access.

Copilot uses AI. Check for mistakes.
Comment on lines +69 to +75
ET_CHECK_OR_RETURN_ERROR(
flatbuffer_offset <= size && flatbuffer_size <= size - flatbuffer_offset,
InvalidArgument,
"flatbuffer_offset: %u and flatbuffer_size: %u are invalid for buffer of size: %zu",
flatbuffer_offset,
flatbuffer_size,
size);
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

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

The new error messages print uint32_t fields using %u. In this codebase there are already places using PRIu32 for uint32_t formatting; using the PRIu32 macros here as well avoids type/format mismatches on platforms where uint32_t isn’t unsigned int and keeps formatting consistent.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Valid nit.

Copy link
Copy Markdown
Contributor Author

@lucylq lucylq Apr 10, 2026

Choose a reason for hiding this comment

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

thanks, updated!

…XECUTORCH-33, TOB-EXECUTORCH-34)

TOB-EXECUTORCH-33: XNNCompiler::compileModel() processed FlatBuffer data
via fb_xnnpack::GetXNNGraph() without first running the FlatBuffer verifier.
A malformed or truncated payload could cause out-of-bounds reads when the
FlatBuffer library follows internal offset tables. This adds a
flatbuffers::Verifier pass (matching the pattern used in program.cpp) before
any FlatBuffer accessors are called, and tracks the flatbuffer_size so the
verifier knows the exact bounds of the serialized data.

TOB-EXECUTORCH-34: XNNHeader::Parse() read flatbuffer_offset,
flatbuffer_size, constant_data_offset, and constant_data_size from untrusted
header bytes but never validated that the resulting regions actually fit within
the provided buffer. Crafted offset/size values could point past the end of
the buffer, leading to out-of-bounds reads in compileModel(). This adds
overflow-safe bounds checks that ensure both the flatbuffer and constant data
regions fall within [0, size).

This PR was authored with the assistance of Claude.
Copilot AI review requested due to automatic review settings April 10, 2026 21:54
Copy link
Copy Markdown
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.

Comment on lines +68 to +76
// Validate that flatbuffer region does not overflow or exceed the buffer.
ET_CHECK_OR_RETURN_ERROR(
flatbuffer_offset <= size && flatbuffer_size <= size - flatbuffer_offset,
InvalidArgument,
"flatbuffer_offset: %" PRIu32 " and flatbuffer_size: %" PRIu32
" are invalid for buffer of size: %zu",
flatbuffer_offset,
flatbuffer_size,
size);
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.

3 participants