Skip to content

fix: prevent integer overflow in kernel heap allocations - #39

Merged
srpatcha merged 1 commit into
embeddedos-org:masterfrom
AlbiKallaba:fix/heap-allocation-overflow
Aug 25, 2026
Merged

fix: prevent integer overflow in kernel heap allocations#39
srpatcha merged 1 commit into
embeddedos-org:masterfrom
AlbiKallaba:fix/heap-allocation-overflow

Conversation

@AlbiKallaba

Copy link
Copy Markdown
Contributor

Problem

eos_malloc() aligns the requested size and then adds the internal block-header size without first checking whether either calculation can overflow size_t.

For requests close to SIZE_MAX, the arithmetic can wrap to a small value. The allocator may then return a valid-looking pointer to a block that is much smaller than requested, potentially leading to out-of-bounds writes.

The heap implementation was also not included in the eos_kernel CMake target, despite the allocator API being publicly exposed.

Solution

  • Reject allocation sizes that would overflow during 8-byte alignment.
  • Reject sizes that would overflow when adding HEADER_SIZE.
  • Add kernel/src/mem/heap.c to the eos_kernel target.
  • Add a dedicated CTest target for the heap allocator.
  • Add regression coverage for SIZE_MAX, alignment overflow, and a valid allocation after rejected requests.

Testing

Built and executed the dedicated test on a Windows host using CMake, Ninja, and Zig/Clang 21:

cmake --build build/codex --target test_heap

ctest --test-dir build/codex -R "^test_heap$" --output-on-failure

Result:

100% tests passed, 1/1

The regression test was run against the previous implementation and failed with:

SIZE_MAX allocation must be rejected

Additional considerations

This change only rejects unrepresentable allocation sizes. It does not change the allocator's best-fit, splitting, coalescing, or statistics behavior.

@srpatcha
srpatcha merged commit ac169e5 into embeddedos-org:master Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants