Skip to content

Conversation

@lucylq
Copy link
Contributor

@lucylq lucylq commented Jan 22, 2026

Summary

Add size checks for int, double, bool before memcpying. Otherwise the input buffer may be larger than the intended size and overwrite adjacent memory.

Test Plan

(executorch) [[email protected] /data/users/lfq/executorch/build (lfq.check-size-before-memcpy)]$ ctest -R extension_runner_util_test -V
...
54: [ RUN      ] InputsTest.DoubleInputWrongSizeFails
54: E 00:00:00.001251 executorch:inputs.cpp:101] Double input at index 2 has size 16, expected sizeof(double) 8
54: [       OK ] InputsTest.DoubleInputWrongSizeFails (0 ms)
...

1/1 Test #54: extension_runner_util_test .......   Passed    0.01 sec

The following tests passed:
        extension_runner_util_test

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.01 sec

@pytorch-bot
Copy link

pytorch-bot bot commented Jan 22, 2026

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit 9c75403 with merge base 86b4bea (image):

NEW FAILURE - The following job has 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 Jan 22, 2026
@github-actions
Copy link

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 January 22, 2026 21:22
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

Mitigates a stack buffer overflow risk in prepare_input_tensors() by validating scalar input buffer sizes before copying into stack-allocated variables.

Changes:

  • Add strict size checks for Tag::Int (int64_t), Tag::Double (double), and Tag::Bool (bool) inputs prior to memcpy.
  • Return InvalidArgument with a descriptive error when the provided scalar buffer size is unexpected.

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

@mergennachin
Copy link
Contributor

Can you add a regression test?

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


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

@lucylq lucylq force-pushed the lfq.check-size-before-memcpy branch from 8c8d7dc to c621f13 Compare January 23, 2026 01:13
Copilot AI review requested due to automatic review settings January 23, 2026 01:14
@lucylq lucylq force-pushed the lfq.check-size-before-memcpy branch from c621f13 to 33aa861 Compare January 23, 2026 01:14
@lucylq lucylq force-pushed the lfq.check-size-before-memcpy branch from 33aa861 to 8090b55 Compare January 23, 2026 01:14
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.

@lucylq
Copy link
Contributor Author

lucylq commented Jan 23, 2026

Can you add a regression test?

@mergennachin added one for double. Feel like that should be OK as it's the same logic, and adding tests for bool/int would need new models to export_program.py that take in bool/int as input. But let me know what you think

Copy link
Contributor

@larryliu0820 larryliu0820 left a comment

Choose a reason for hiding this comment

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

Left some non-blocking comments

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 6 out of 6 changed files in this pull request and generated 1 comment.


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

@lucylq lucylq force-pushed the lfq.check-size-before-memcpy branch from 801f5a3 to e95bc65 Compare January 27, 2026 23:23
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 6 out of 6 changed files in this pull request and generated 3 comments.


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

Comment on lines +44 to +66
const char* add_path = std::getenv("ET_MODULE_ADD_PATH");
Result<FileDataLoader> add_loader = FileDataLoader::from(add_path);
ASSERT_EQ(add_loader.error(), Error::Ok);
add_loader_ = std::make_unique<FileDataLoader>(std::move(add_loader.get()));

Result<Program> add_program = Program::load(
add_loader_.get(), Program::Verification::InternalConsistency);
ASSERT_EQ(add_program.error(), Error::Ok);
add_program_ = std::make_unique<Program>(std::move(add_program.get()));

add_mmm_ = std::make_unique<ManagedMemoryManager>(
/*planned_memory_bytes=*/32 * 1024U,
/*method_allocator_bytes=*/32 * 1024U);

Result<Method> add_method =
add_program_->load_method("forward", &add_mmm_->get());
ASSERT_EQ(add_method.error(), Error::Ok);
add_method_ = std::make_unique<Method>(std::move(add_method.get()));

// Load ModuleIntBool
const char* intbool_path = std::getenv("ET_MODULE_INTBOOL_PATH");
Result<FileDataLoader> intbool_loader = FileDataLoader::from(intbool_path);
ASSERT_EQ(intbool_loader.error(), Error::Ok);
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

SetUp() assumes the environment variables are present. If ET_MODULE_ADD_PATH/ET_MODULE_INTBOOL_PATH are missing, the failure will show up later as a generic FileDataLoader::from() error. Adding ASSERT_NE(getenv(...), nullptr) for each path would make failures much easier to diagnose.

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

Choose a reason for hiding this comment

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

think it's probably fine to have the filedataloader error show up.

@lucylq lucylq force-pushed the lfq.check-size-before-memcpy branch from e95bc65 to 1f63405 Compare January 28, 2026 01:26
Copilot AI review requested due to automatic review settings January 28, 2026 01:31
@lucylq lucylq force-pushed the lfq.check-size-before-memcpy branch from 1f63405 to 9c75403 Compare January 28, 2026 01:31
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 6 out of 6 changed files in this pull request and generated 4 comments.


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

Comment on lines +241 to +244
// Double is size 8; use a larger buffer to invoke overflow.
char large_buffer[16];
memcpy(large_buffer, &alpha, sizeof(double));

Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

This file now calls memcpy() directly; consider adding an explicit #include <cstring> (and #include <cstdlib> for std::getenv) to avoid relying on transitive includes for C library declarations.

Copilot uses AI. Check for mistakes.
{
std::vector<std::pair<char*, size_t>> input_buffers;

char int_buffer[8];
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

Avoid the magic constant 8 for the int buffer size; use sizeof(int64_t) (or sizeof(y)) so the test stays consistent with the production check (buffer_size != sizeof(int64_t)).

Suggested change
char int_buffer[8];
char int_buffer[sizeof(int64_t)];

Copilot uses AI. Check for mistakes.
Comment on lines +44 to +46
const char* add_path = std::getenv("ET_MODULE_ADD_PATH");
Result<FileDataLoader> add_loader = FileDataLoader::from(add_path);
ASSERT_EQ(add_loader.error(), Error::Ok);
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

std::getenv("ET_MODULE_ADD_PATH") can return nullptr; passing that into FileDataLoader::from() just yields a generic "File name cannot be empty" failure. Add an ASSERT_NE(add_path, nullptr) (ideally mentioning the env var name) before calling FileDataLoader::from() to make misconfigured test environments easier to debug.

Copilot uses AI. Check for mistakes.
add_method_ = std::make_unique<Method>(std::move(add_method.get()));

// Load ModuleIntBool
const char* intbool_path = std::getenv("ET_MODULE_INTBOOL_PATH");
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

Same as above for ET_MODULE_INTBOOL_PATH: add an ASSERT_NE(intbool_path, nullptr) (with a helpful message) before calling FileDataLoader::from() so failures clearly indicate a missing env var.

Suggested change
const char* intbool_path = std::getenv("ET_MODULE_INTBOOL_PATH");
const char* intbool_path = std::getenv("ET_MODULE_INTBOOL_PATH");
ASSERT_NE(intbool_path, nullptr)
<< "ET_MODULE_INTBOOL_PATH environment variable must be set";

Copilot uses AI. Check for mistakes.
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.

4 participants