relinker --to-intel: signed integer overflow in Amd64OnlyConverter::_collectBranchTargets for a segment mapped across 2^63 #10653

Closed
opened 2026-10-24 15:20:27 +00:00 by TechLevelZero · 0 comments
TechLevelZero commented 2026-10-24 15:20:27 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by TechLevelZero on 2026-10-24T15:20:27Z.


What happens
With --to-intel (or --to-rosetta), _collectBranchTargets adds the branch displacement to the next instruction's address as std::int64_t. For an executable segment mapped at or above 2^63 the sum overflows: Amd64OnlyConverter.cpp:282: runtime error: signed integer overflow: -9223372036854774438 + -2105376126 cannot be represented in type 'std::int64_t'.

Title, test or build step
relinker --to-intel on a malformed ELF. Found by fuzzing Relinker::Conversion::Convert with libFuzzer, ASan and UBSan.

AnyPS5 commit
main 6cd36e651 (also b84322da6 and 924b5628b). No PRs on top.

System
Ubuntu 26.04 (clang 21.1.8, -fsanitize=address,undefined) for the fuzzer. Ubuntu 26.04 container (GCC 15, -fsanitize=undefined, relinker-only build) for the command-line reproduction.

Steps to reproduce

  1. Build the relinker with UBSan:
    cmake -S . -B build-ubsan -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DANYPS5_RELINKER_ONLY=ON \
      "-DCMAKE_CXX_FLAGS=-fsanitize=undefined -fno-sanitize-recover=undefined" "-DCMAKE_EXE_LINKER_FLAGS=-fsanitize=undefined"
    cmake --build build-ubsan --target relinker
    
  2. Run it on the attached overflow-repro.elf. It's an 8,107-byte fuzzer-generated file, SHA-256 3636a37f7422a0eaf3b247a22a65fdb730101666cdea934bda996daeda1cc2c4. Its first PT_LOAD is executable at p_vaddr 0x7fffffffffffffff, p_offset 0x1000, p_filesz 0x800.
    build-ubsan/core/relinker/relinker --skip-sce-module --to-intel overflow-repro.elf out
    

Log

core/relinker/codegen/src/Amd64OnlyConverter.cpp:282:24: runtime error: signed integer overflow: -2105376126 + -9223372036854774438 cannot be represented in type 'long int'

Stack from the fuzzer (clang):

#0 Codegen::(anonymous namespace)::Amd64OnlyConverter::_collectBranchTargets(...) core/relinker/codegen/src/Amd64OnlyConverter.cpp:282:38
#1 Codegen::(anonymous namespace)::Amd64OnlyConverter::_convertSegment(...) core/relinker/codegen/src/Amd64OnlyConverter.cpp:347:53
#2 Codegen::(anonymous namespace)::Amd64OnlyConverter::Convert(...) core/relinker/codegen/src/Amd64OnlyConverter.cpp:479:9
#3 Relinker::Conversion::Convert(...) core/relinker/relinker/src/pipeline/Conversion.cpp:67:264

What was checked

  • The code:
    const auto next = static_cast<std::int64_t>(ph.MappedAddress + match.Offset + info.Length);
    if (info.HasBranchTarget && !info.HasRipRelativeDisp) {
        const auto target = next + info.BranchDisp;
        if (target >= 0 && !pastPrefixes(static_cast<std::uint64_t>(target)))
    
    next + displacement for LEA (line 288) has the same pattern.
  • Without sanitizers it doesn't crash: the sum wraps in practice, and every target at or above 2^63 comes out negative and is dropped by target >= 0. A consequence even without the UB is that a branch into an AMD-only instruction in such a segment is not refused. The existing converterSegment check ("Branch into an AMD-only instruction was accepted") passes at 0x1000 but accepts the same bytes mapped at 0x7FFFFFFFFFFFFFFC.
  • Without --to-intel the same file is refused before conversion (FAIL: ET_NONE inputs do not specify an ELF object type). With it, Amd64OnlyConverter::Convert runs on the raw program headers first.
  • The later check in ProgramHeaderLayoutBuilder (Load segment virtual range overflowed) doesn't cover this. 0x7fffffffffffffff plus 0x800 doesn't overflow std::uint64_t, only std::int64_t.
  • Same result every run. StubBodyBuilder.cpp:228 (end + info.BranchDisp) uses an unsigned end, so it wraps without UB.
  • A fix (add the addresses as unsigned values and drop only targets that wrap) with a regression test is ready. I'll open it as a PR once my first open PR is merged.

overflow-repro.zip

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6014468545 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/10653). Originally posted by [TechLevelZero](https://github.com/TechLevelZero) on 2026-10-24T15:20:27Z. --- **What happens** With `--to-intel` (or `--to-rosetta`), `_collectBranchTargets` adds the branch displacement to the next instruction's address as `std::int64_t`. For an executable segment mapped at or above 2^63 the sum overflows: `Amd64OnlyConverter.cpp:282: runtime error: signed integer overflow: -9223372036854774438 + -2105376126 cannot be represented in type 'std::int64_t'`. **Title, test or build step** `relinker --to-intel` on a malformed ELF. Found by fuzzing `Relinker::Conversion::Convert` with libFuzzer, ASan and UBSan. **AnyPS5 commit** `main` 6cd36e651 (also b84322da6 and 924b5628b). No PRs on top. **System** Ubuntu 26.04 (clang 21.1.8, `-fsanitize=address,undefined`) for the fuzzer. Ubuntu 26.04 container (GCC 15, `-fsanitize=undefined`, relinker-only build) for the command-line reproduction. **Steps to reproduce** 1. Build the relinker with UBSan: ``` cmake -S . -B build-ubsan -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DANYPS5_RELINKER_ONLY=ON \ "-DCMAKE_CXX_FLAGS=-fsanitize=undefined -fno-sanitize-recover=undefined" "-DCMAKE_EXE_LINKER_FLAGS=-fsanitize=undefined" cmake --build build-ubsan --target relinker ``` 2. Run it on the attached `overflow-repro.elf`. It's an 8,107-byte fuzzer-generated file, SHA-256 `3636a37f7422a0eaf3b247a22a65fdb730101666cdea934bda996daeda1cc2c4`. Its first `PT_LOAD` is executable at `p_vaddr` 0x7fffffffffffffff, `p_offset` 0x1000, `p_filesz` 0x800. ``` build-ubsan/core/relinker/relinker --skip-sce-module --to-intel overflow-repro.elf out ``` **Log** ``` core/relinker/codegen/src/Amd64OnlyConverter.cpp:282:24: runtime error: signed integer overflow: -2105376126 + -9223372036854774438 cannot be represented in type 'long int' ``` Stack from the fuzzer (clang): ``` #0 Codegen::(anonymous namespace)::Amd64OnlyConverter::_collectBranchTargets(...) core/relinker/codegen/src/Amd64OnlyConverter.cpp:282:38 #1 Codegen::(anonymous namespace)::Amd64OnlyConverter::_convertSegment(...) core/relinker/codegen/src/Amd64OnlyConverter.cpp:347:53 #2 Codegen::(anonymous namespace)::Amd64OnlyConverter::Convert(...) core/relinker/codegen/src/Amd64OnlyConverter.cpp:479:9 #3 Relinker::Conversion::Convert(...) core/relinker/relinker/src/pipeline/Conversion.cpp:67:264 ``` **What was checked** - The code: ```cpp const auto next = static_cast<std::int64_t>(ph.MappedAddress + match.Offset + info.Length); if (info.HasBranchTarget && !info.HasRipRelativeDisp) { const auto target = next + info.BranchDisp; if (target >= 0 && !pastPrefixes(static_cast<std::uint64_t>(target))) ``` `next + displacement` for LEA (line 288) has the same pattern. - Without sanitizers it doesn't crash: the sum wraps in practice, and every target at or above 2^63 comes out negative and is dropped by `target >= 0`. A consequence even without the UB is that a branch into an AMD-only instruction in such a segment is not refused. The existing `converterSegment` check ("Branch into an AMD-only instruction was accepted") passes at 0x1000 but accepts the same bytes mapped at 0x7FFFFFFFFFFFFFFC. - Without `--to-intel` the same file is refused before conversion (`FAIL: ET_NONE inputs do not specify an ELF object type`). With it, `Amd64OnlyConverter::Convert` runs on the raw program headers first. - The later check in `ProgramHeaderLayoutBuilder` (`Load segment virtual range overflowed`) doesn't cover this. 0x7fffffffffffffff plus 0x800 doesn't overflow `std::uint64_t`, only `std::int64_t`. - Same result every run. `StubBodyBuilder.cpp:228` (`end + info.BranchDisp`) uses an unsigned `end`, so it wraps without UB. - A fix (add the addresses as unsigned values and drop only targets that wrap) with a regression test is ready. I'll open it as a PR once my first open PR is merged. [overflow-repro.zip](https://github.com/user-attachments/files/33907568/overflow-repro.zip)
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#10653
No description provided.