Skip to content

Out-of-bounds read in validateTensorLayout#17131

Merged
lucylq merged 1 commit intomainfrom
lfq.security-16
Apr 1, 2026
Merged

Out-of-bounds read in validateTensorLayout#17131
lucylq merged 1 commit intomainfrom
lfq.security-16

Conversation

@lucylq
Copy link
Copy Markdown
Contributor

@lucylq lucylq commented Feb 3, 2026

Summary

check sizes and dim are same length

Test plan

cmake --build . --target tensor_parser_test
ctest -R tensor_parser_test -V

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Feb 3, 2026

🔗 Helpful Links

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

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

❌ 1 New Failure, 2 Unrelated Failures

As of commit 024b63e with merge base 4c56d9b (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 Feb 3, 2026
@github-actions
Copy link
Copy Markdown

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 marked this pull request as ready for review February 3, 2026 22:48
@lucylq lucylq requested a review from JacobSzwejbka as a code owner February 3, 2026 22:48
Copilot AI review requested due to automatic review settings February 3, 2026 22: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 tensor layout validation against malformed serialized tensors by ensuring dim_order and sizes have consistent lengths, preventing an out-of-bounds read in validateTensorLayout.

Changes:

  • Added a dimensionality check in validateTensorLayout to require s_tensor->dim_order()->size() to match the tensor rank before iterating over dim_order.
  • Introduced a focused unit test that builds a tensor with sizes longer than dim_order and asserts that validateTensorLayout returns Error::InvalidExternalData.
  • Wired up the test harness with the tensor_layout API by adding the appropriate includes and using declarations.

Reviewed changes

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

File Description
runtime/executor/tensor_parser_exec_aten.cpp Tightens validateTensorLayout by checking that dim_order length equals the tensor dimension before accessing dim_order()[i], eliminating the out-of-bounds read for short dim_order vectors.
runtime/executor/test/tensor_parser_test.cpp Adds a regression test for the dim-order/size mismatch case and includes TensorLayout/validateTensorLayout so that the new behavior is exercised.

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

Comment on lines +132 to +137
ET_CHECK_OR_RETURN_ERROR(
s_tensor->dim_order()->size() == static_cast<size_t>(dim),
InvalidExternalData,
"Dim order size mismatch. Expected %d, got %zu.",
dim,
s_tensor->dim_order()->size());
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

TensorLayout::create returns a Result<const TensorLayout>, but this function calls s_tensor->dim_order()->size() and later s_tensor->dim_order()->Get(i) without first checking that s_tensor->dim_order() is non-null. If a malformed or tampered flatbuffer omits the dim_order field, these dereferences will crash instead of returning Error::InvalidExternalData; consider adding an ET_CHECK_OR_RETURN_ERROR that s_tensor->sizes() != nullptr and s_tensor->dim_order() != nullptr (similar to the checks in tensor_parser_aten.cpp) before using them.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

@lucylq lucylq Feb 3, 2026

Choose a reason for hiding this comment

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

This is validated before going into validateTensorLayout, see: https://github.com/pytorch/executorch/blob/main/runtime/executor/tensor_parser_aten.cpp#L69
https://github.com/pytorch/executorch/blob/main/runtime/executor/tensor_parser_portable.cpp#L73

^ before calling getTensorDataPtr, which calls validateTensorLayout

Comment thread runtime/executor/test/tensor_parser_test.cpp
Copilot AI review requested due to automatic review settings February 6, 2026 23: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 no new comments.


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

@lucylq lucylq mentioned this pull request Mar 30, 2026
@lucylq lucylq merged commit 8b30cfe into main Apr 1, 2026
162 of 165 checks passed
@lucylq lucylq deleted the lfq.security-16 branch April 1, 2026 17:47
Jiseong-oh pushed a commit to Jiseong-oh/executorch that referenced this pull request Apr 2, 2026
### Summary
check sizes and dim are same length

### Test plan
```
cmake --build . --target tensor_parser_test
ctest -R tensor_parser_test -V
```
Jiseong-oh pushed a commit that referenced this pull request Apr 2, 2026
### Summary
check sizes and dim are same length

### Test plan
```
cmake --build . --target tensor_parser_test
ctest -R tensor_parser_test -V
```
Jiseong-oh pushed a commit that referenced this pull request Apr 7, 2026
### Summary
check sizes and dim are same length

### Test plan
```
cmake --build . --target tensor_parser_test
ctest -R tensor_parser_test -V
```
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. security-fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants