Skip to content

fix(relinker): reject Linux RELATIVE addends outside the ELF image - #27980

Open
JustDownTV wants to merge 1 commit into
boykopovar:mainfrom
JustDownTV:fix/relinker-linux-relative-addend-bounds
Open

JustDownTV wants to merge 1 commit into
boykopovar:mainfrom
JustDownTV:fix/relinker-linux-relative-addend-bounds

Conversation

@JustDownTV

@JustDownTV JustDownTV commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

What

A R_X86_64_RELATIVE relocation of the executable whose addend lies outside its PT_LOAD span was converted for Linux outputs (default, fixed base and --lazy-binding): with one PT_LOAD at 0x0-0x1000 and an addend of 0x1008, the output slot points at 0x401008, inside the PT_LOAD the relinker appends for its dynamic strings, symbols and stubs. Windows and macOS outputs (WindowsLoadImage::GetRelocatedAddress) and guest modules on every target (GuestImageReader) already reject it with Relative relocation addend is outside the ELF image.

LinuxElfPatcher::Patch now checks each RELATIVE entry of the data relocation table against the bound GuestImageReader uses: from the lowest PT_LOAD address to the end of the highest one, the end included. RELR entries reach the same table and are covered too.

test_jmprel_table_bounds.py adds RELATIVE cases for the Linux, fixed-base, lazy, Windows and macOS outputs: addends 0, 0x200 and the image end 0x1000 convert, 0x1008, 0x5000 and -8 are rejected. On main the Linux cases linux-relative-addend-outside-1008, -5000 and the lazy -8 convert successfully. In test_linux_fixed_base.py, negative-target (RELATIVE, addend -16) now stops at this check with Relative relocation addend is outside the ELF image instead of Relocation target -16 lies below the image at a fixed Linux base, which still guards IRELATIVE.

Tested

Windows 11, WinLibs GCC 15.2.0 (15.2.0posix-14.0.0-ucrt-r7), relinker-only Release build.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Fixed-base relinking now rejects relative relocation addends that fall outside the ELF image’s address range, with a clear error. Addends at the range boundary remain accepted.
  • Tests
    • Expanded coverage of relative relocation validation across supported output configurations, including cases that should succeed and fail.

A RELATIVE relocation of the executable whose addend lies outside its
PT_LOAD span was converted for Linux outputs and pointed into the
metadata segment the relinker appends, while Windows and macOS outputs
and guest modules on every target reject it with "Relative relocation
addend is outside the ELF image". The Linux patcher now applies the
guest module bound: from the lowest PT_LOAD address to the end of the
highest one, the end included. RELR entries reach the same table.
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Relative relocation bounds

Layer / File(s) Summary
Relative relocation addend validation
core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp, core/relinker/relinker/tests/test_jmprel_table_bounds.py, core/relinker/relinker/tests/test_linux_fixed_base.py
The patcher checks RelaData addends against the image range from nonempty PT_LOAD segments. Tests expect addends 0, 0x200, and 0x1000 to succeed, and 0x1008, 0x5000, and -8 to fail with Relative relocation addend is outside the ELF image. The fixed-base negative-target test now expects that same error.

Suggested reviewers: roboterspinne, recapi

Change: Bug fix

Merge Risk: Minimal · up to 6ff7a

This change makes the Linux relinker reject relative relocation addends that fall outside the ELF image, matching the existing behavior for other output targets. Tests cover the accepted and rejected boundary values. No merge-blocking risk was found.

Security architecture risk: Minimal · up to 6ff7a

Linux conversion now rejects relative relocation addends outside the original executable’s load span. The change narrows accepted input and does not add a new execution path or grant additional access.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The direct effect is confined to Linux conversion of the executable being processed: the gate either rejects that conversion or lets it continue through the existing output path. The change introduces no new caller, privilege transition, or downstream destination.

Trust Boundaries and Controls

  • observed — The control compares executable-supplied addends with a span also derived from the executable. It bounds relative references to that span, but does not prove that an accepted addend identifies mapped bytes: the upper endpoint and gaps within the span are accepted.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8fc14a63-4de5-45ef-b5ac-63490b5e2890
Commits

Reviewing files that changed from the base of the PR and between 1ab34ac and 6ff7a5e.

Files selected for processing: 3

  • core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp
  • core/relinker/relinker/tests/test_jmprel_table_bounds.py
  • core/relinker/relinker/tests/test_linux_fixed_base.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@arcwiser arcwiser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed exact head 6ff7a5e1f7b014b194c5af9937e45146ffa9f872.

The check applies the same inclusive image-span rule already used by GuestImageReader and WindowsLoadImage, handles virtual-range overflow conservatively, and runs before fixed-base rebasing can move an invalid addend into the relinker's appended segment. RELR entries are folded into RelaData, so they reach the check. I also verified that a RELATIVE entry cannot bypass it through RelaPltData: RelinkerPipeline rejects every PLT relocation type other than R_X86_64_JUMP_SLOT before patching.

The regression matrix covers the lower bound, an interior address, the inclusive image end, positive out-of-range values, negative addends, all Linux modes, and parity with Windows/macOS.

Local source-level validation:

  • python tools/check_conventions.py --base upstream/main — passed
  • git diff --check upstream/main...upstream/pr-27980 — passed
  • merge-tree against current upstream/main — clean

This workspace has no C/C++ compiler, so I could not independently execute the relinker cases; the PR reports the focused tests, RELR variant, and broader relinker suite from Windows. No blocking issue found.

@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 24915 core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp
PR 27825 core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp
PR 28808 core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp

@Plyxen Plyxen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checked head 6ff7a5e1f7 on Windows 11 with WinLibs GCC 15.2 (Release), applied to main c6331cc61b together with 14 other open PRs.

  • tools/check_conventions.py --head and tools/debt_fold.py --check pass, and the full build succeeds.
  • jmprel_table_bounds and linux_fixed_base pass in the full suite, with the relinker producing every output target on this Windows host.
  • Full CPU suite (5,146 tests): the only failures are windows_read_trap_unknown, which fails on main too (fixed in #28028), and the frame_capture_minimize load timeout.

From reading the code: addend > end keeps an addend equal to the end of the last PT_LOAD, a one-past-the-end address, which the test pins with 0x1000.

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — looks good

Nothing blocks this change; the review found no verified defect in the changed code.

What I ran

Reviewed head 6ff7a5e1f7b014b194c5af9937e45146ffa9f872. I read the prepared description, commits, manifest and diff, plus all three changed files at the relevant hunks. I used repo_read.py to inspect targeted repository context. No build or test commands were run.

Results

Check Result Note
Diff and target code review passed Changed behavior and callers inspected
Tests not run Author-reported results were not independently reproduced

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests or build were run. I did not read portions of LinuxElfPatcher.cpp and the two test files outside the cited ranges. review/files.json was unavailable; the manifest lists all three changed files as included and reviewed.

To get this to a good state

Nothing is required.

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ran jmprel_table_bounds, linux_fixed_base, and linux_load_pages on Linux with the PR applied. All passed, covering the relocation addend bounds checks and the valid fixed-base layout cases.

AI-assisted: yes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants