Skip to content

Commit e59c0bd

Browse files
lucylqGithub Executorch
andauthored
Fix integer overflow in program.cpp bounds checks (#18662)
17: In get_constant_buffer_data(), the bounds check `offset + nbytes <= size` can overflow when offset and nbytes are large. Replace with the overflow-safe pattern `offset <= size && nbytes <= size - offset`. 24: In Program::load(), the computation `segment_base_offset + segment_data_size` for the expected file size can overflow. Add an explicit overflow check before the addition to ensure the sum does not exceed SIZE_MAX. This PR was authored with the assistance of Claude. Co-authored-by: Github Executorch <github_executorch@arm.com>
1 parent 8919ef8 commit e59c0bd

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

runtime/executor/program.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
9292
// is positive (0-value may indicate no segments)
9393
if ((segment_data_size == 0 && segment_base_offset == 0) ||
9494
segment_data_size > 0) {
95+
ET_CHECK_OR_RETURN_ERROR(
96+
segment_base_offset <= SIZE_MAX - segment_data_size,
97+
InvalidProgram,
98+
"segment_base_offset %zu + segment_data_size %zu overflows",
99+
segment_base_offset,
100+
segment_data_size);
95101
size_t expected = segment_base_offset == 0
96102
? program_size
97103
: segment_base_offset + segment_data_size;
@@ -429,7 +435,7 @@ Result<const void*> Program::get_constant_buffer_data(
429435

430436
size_t size = constant_segment_data_.size();
431437
ET_CHECK_OR_RETURN_ERROR(
432-
offset + nbytes <= size,
438+
offset <= size && nbytes <= size - offset,
433439
InvalidArgument,
434440
"Constant segment offset %" PRIu64
435441
" + size_bytes %zu invalid for program constant segment size %zu",

0 commit comments

Comments
 (0)