Skip to content

Fix heap-buffer-overflow from non-instruction-aligned FUNC symbols#1106

Merged
elazarg merged 2 commits intovbpf:mainfrom
mikeagun:fix-unaligned-func-symbol-overflow
Apr 28, 2026
Merged

Fix heap-buffer-overflow from non-instruction-aligned FUNC symbols#1106
elazarg merged 2 commits intovbpf:mainfrom
mikeagun:fix-unaligned-func-symbol-overflow

Conversation

@mikeagun
Copy link
Copy Markdown
Contributor

Summary

Reject STT_FUNC symbols in executable sections whose st_value is not a multiple of sizeof(EbpfInst) (8 bytes). Without this validation, a malformed ELF can cause a heap-buffer-overflow in vector_of during program loading.

Fixes #1105

Changes

Root-cause fix — get_program_name_and_size() (elf_reader.cpp):

  • Validate that each FUNC symbol's st_value is instruction-aligned before using it as a program boundary. Throws UnmarshalError for non-aligned values. This protects both callers: ProgramReader::read_programs() and ElfObjectState::discover_programs().

Defense-in-depth — ProgramReader::read_programs() (elf_reader.cpp):

  • Bounds-check offset + extracted_size <= sec->get_size() before the vector_of call to guard against future regressions in span computation.

Test — test_elf_loader.cpp:

  • Add [elf][hardening] test that constructs a minimal ELF (via ELFIO) with a FUNC symbol at an unaligned offset and verifies it is cleanly rejected with UnmarshalError.

get_program_name_and_size() Root cause analysis

The overflow originates from compute_reachable_program_span() performing truncating integer division (program_offset / sizeof(EbpfInst)) on a non-aligned byte offset. But the non-aligned offset enters the system through get_program_name_and_size(), which uses FUNC symbol values as program boundaries without alignment validation.

Fixing at this shared helper protects all downstream consumers, including the discover_programs() path in elf_loader.cpp which has the same offset += size loop.

Reject ELF FUNC symbols in executable sections whose st_value is not a
multiple of sizeof(EbpfInst) (8 bytes). A malformed ELF with such a
symbol causes get_program_name_and_size() to produce non-aligned program
boundaries. When read_programs() advances offset by a non-aligned
symbol_size, compute_reachable_program_span() uses truncating integer
division (offset / sizeof(EbpfInst)), inflating the computed span and
causing vector_of<EbpfInst> to memcpy past the section data buffer.

The root-cause fix validates FUNC symbol alignment in
get_program_name_and_size(), which is shared by both read_programs() and
ElfObjectState::discover_programs(). A defense-in-depth bounds check
before the vector_of call in read_programs() guards against future
regressions in span computation.

Add a test that constructs a minimal ELF with a FUNC symbol at an
unaligned offset and verifies it is cleanly rejected.

Signed-off-by: Michael Agun <danielagun@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Michael Agun <danielagun@microsoft.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 28, 2026

Warning

Rate limit exceeded

@mikeagun has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 25 minutes and 8 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: b2e74e29-07e8-4451-abda-454adac27b37

📥 Commits

Reviewing files that changed from the base of the PR and between 8089a5a and eaf6b21.

📒 Files selected for processing (1)
  • src/test/test_elf_loader.cpp
📝 Walkthrough

Walkthrough

The PR adds input validation to the ELF reader to reject non-instruction-aligned STT_FUNC symbol offsets and ensure program spans remain within section boundaries. A corresponding test validates this validation behavior.

Changes

Cohort / File(s) Summary
ELF reader validation
src/io/elf_reader.cpp
Adds instruction-alignment checks for FUNC symbol offsets (modulo sizeof(EbpfInst) == 0) and bounds verification to ensure derived program spans don't exceed ELF section limits; both conditions now throw UnmarshalError on failure.
Test coverage
src/test/test_elf_loader.cpp
Introduces test that constructs an in-memory ELF with aligned and unaligned FUNC symbols, verifying that read_elf rejects unaligned symbols with an appropriate error message.

Suggested reviewers

  • elazarg
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly summarizes the main fix: rejecting non-instruction-aligned FUNC symbols to prevent heap-buffer-overflow.
Description check ✅ Passed The description is thorough and clearly related to the changeset, detailing the root cause, the fix approach, and test coverage.
Linked Issues check ✅ Passed The PR fully addresses #1105: validates FUNC symbol alignment in get_program_name_and_size(), adds bounds-checking in read_programs(), and includes a hardening test for unaligned FUNC symbols.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the heap-buffer-overflow vulnerability and hardening the ELF loader; no unrelated modifications present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/test/test_elf_loader.cpp`:
- Around line 584-638: Add a companion test mirroring TEST_CASE("ELF loader
rejects non-instruction-aligned FUNC symbol", "[elf][hardening]") that proves
the aligned case is accepted: create the same ELF (.text, 32 exit instructions,
strtab/symtab) but when adding the second STT_FUNC symbol (currently added via
sym_writer.add_symbol for "prog_b") use an 8-byte aligned offset (e.g., 8)
instead of 7 and an appropriate size, then call read_elf(in_stream, "memory",
".text", "", options, &g_ebpf_platform_linux) and assert it succeeds (use
REQUIRE_NOTHROW or equivalent) so the test verifies the aligned-FUNC path is
accepted; reuse the same helpers/variables (text_sec, sym_writer, str_writer,
read_elf) and give the test a clear name like "ELF loader accepts
instruction-aligned FUNC symbol".
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5f9e7f9f-9d5d-456e-88c0-e01e2a0dfabe

📥 Commits

Reviewing files that changed from the base of the PR and between 5d5cbe1 and 8089a5a.

📒 Files selected for processing (2)
  • src/io/elf_reader.cpp
  • src/test/test_elf_loader.cpp

Comment thread src/test/test_elf_loader.cpp
Companion to the rejection test — verifies that FUNC symbols at
8-byte-aligned offsets are accepted without error, ensuring the
alignment validation does not reject well-formed ELF files.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Michael Agun <danielagun@microsoft.com>
@elazarg elazarg merged commit 1cc4ec5 into vbpf:main Apr 28, 2026
16 checks passed
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.

Heap-buffer-overflow in read_programs() when ELF contains non-instruction-aligned FUNC symbol

2 participants