Skip to content

Commit 0f1f47a

Browse files
author
Github Executorch
committed
Fix integer overflow in PlatformMemoryAllocator::allocate() (TOB-EXECUTORCH-26)
Add overflow checking before computing the total allocation size (sizeof(AllocationNode) + size + alignment) in PlatformMemoryAllocator::allocate(). Previously, when this sum exceeded SIZE_MAX, it would wrap around to a small value, causing pal_allocate to allocate an undersized buffer. This could lead to subsequent out-of-bounds writes. The fix validates each addition step against SIZE_MAX and returns nullptr on overflow. This PR was authored with the assistance of Claude.
1 parent ee92757 commit 0f1f47a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

runtime/executor/platform_memory_allocator.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <cinttypes>
1313
#include <cstdint>
1414

15+
#include <c10/util/safe_numerics.h>
1516
#include <executorch/runtime/core/memory_allocator.h>
1617
#include <executorch/runtime/platform/log.h>
1718
#include <executorch/runtime/platform/platform.h>
@@ -46,8 +47,19 @@ class PlatformMemoryAllocator final : public MemoryAllocator {
4647
return nullptr;
4748
}
4849

49-
// Allocate enough memory for the node, the data and the alignment bump.
50-
size_t alloc_size = sizeof(AllocationNode) + size + alignment;
50+
// Check for overflow before computing total allocation size.
51+
// Then allocate enough memory for node, data and the alignment bump.
52+
size_t alloc_size = 0;
53+
if (c10::add_overflows(sizeof(AllocationNode), size, &alloc_size) ||
54+
c10::add_overflows(alloc_size, alignment, &alloc_size)) {
55+
ET_LOG(
56+
Error,
57+
"Allocation size overflow: size %zu, alignment %zu",
58+
size,
59+
alignment);
60+
return nullptr;
61+
}
62+
5163
void* node_memory = runtime::pal_allocate(alloc_size);
5264

5365
// If allocation failed, log message and return nullptr.

0 commit comments

Comments
 (0)