fix(relinker): overflow-safe RELA entry bounds check #917

Merged
TidalImpact merged 3 commits from upstream-pr/917 into main 2026-10-08 06:03:28 +00:00
TidalImpact commented 2026-10-06 16:33:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TidalImpact on 2026-10-06T16:33:22Z.


What

Make the RELA relocation entry bounds check overflow-safe.

The previous check used pos + relaEntSize, which could overflow before the bounds check. The new check validates the offset and remaining buffer size separately.

Tested

Windows, x64 Release build.

  • relinker.exe builds successfully and starts.
  • Full ctest run attempted; many tests were reported as Not Run in this Windows environment.
  • No ELF fixture was available in the repository for a manual relinker run.

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
  • Unimplemented paths throw; silent stubs are documented
  • No notes, investigation .md files or images added
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: no
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4761750557 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917). Originally posted by [TidalImpact](https://github.com/TidalImpact) on 2026-10-06T16:33:22Z. --- ### What Make the RELA relocation entry bounds check overflow-safe. The previous check used `pos + relaEntSize`, which could overflow before the bounds check. The new check validates the offset and remaining buffer size separately. ### Tested Windows, x64 Release build. * `relinker.exe` builds successfully and starts. * Full `ctest` run attempted; many tests were reported as `Not Run` in this Windows environment. * No ELF fixture was available in the repository for a manual relinker run. ### Checklist * [x] Based on current `main`; no other open PR implements the same functions * [x] One topic per PR; follow-ups go in a new PR * [x] No comments in code except technical debt * [x] Unimplemented paths throw; silent stubs are documented * [x] No notes, investigation `.md` files or images added * [x] New third-party code is a submodule built from source * [x] Generic behaviour, not specific to one title * [x] Depends on: none * [x] AI-assisted: no
TidalImpact commented 2026-10-07 05:27:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TidalImpact on 2026-10-07T05:27:14Z.


Hi, I’m not sure I fully understand the mention of the old PR.

As I understand it, #917 already fixes the overflow-safe RELA entry bounds check that was part of #298, but #917 is still open, while #930 has already been merged and handles a different part of the issue.

I just wanted to ask whether there is still anything you would like me to change or add to #917, or if I should just wait for further instructions.

I just want to make sure I’m following the intended approach.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6031599045 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917#issuecomment-6031599045). Originally posted by [TidalImpact](https://github.com/TidalImpact) on 2026-10-07T05:27:14Z. --- Hi, I’m not sure I fully understand the mention of the old PR. As I understand it, #917 already fixes the overflow-safe RELA entry bounds check that was part of #298, but #917 is still open, while #930 has already been merged and handles a different part of the issue. I just wanted to ask whether there is still anything you would like me to change or add to #917, or if I should just wait for further instructions. I just want to make sure I’m following the intended approach.
muhzain05 commented 2026-10-07 11:03:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by muhzain05 on 2026-10-07T11:03:53Z.


I noticed a related bounds issue nearby that #917 doesn’t seem to cover.

dynSymTabOffset can come directly from DT_OS_SYMTAB, and the later symOff + 4 > raw.size() check can overflow for an offset near UINT64_MAX. DT_OS_SYMTABSZ also appears to be defined but unused for bounding symbol indices.

extractRelative nearby also has no independent bounds check and currently seems to rely on extractRela running first over the same ranges.

I’d be happy to send a separate PR with focused malformed-ELF regression tests, depending on #917, unless you’d prefer this folded into the same work.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6036578932 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917#issuecomment-6036578932). Originally posted by [muhzain05](https://github.com/muhzain05) on 2026-10-07T11:03:53Z. --- I noticed a related bounds issue nearby that #917 doesn’t seem to cover. `dynSymTabOffset` can come directly from `DT_OS_SYMTAB`, and the later `symOff + 4 > raw.size()` check can overflow for an offset near `UINT64_MAX`. `DT_OS_SYMTABSZ` also appears to be defined but unused for bounding symbol indices. `extractRelative` nearby also has no independent bounds check and currently seems to rely on `extractRela` running first over the same ranges. I’d be happy to send a separate PR with focused malformed-ELF regression tests, depending on #917, unless you’d prefer this folded into the same work.
adriadam10 commented 2026-10-07 20:22:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T20:22:05Z.

Review: CHANGES_REQUESTED


Thanks, the overflow-safe check in extractRela looks right to me: with a huge relaOff, pos = relaOff + off wrapped and passed the old check. Two things before this can merge:

  • No regression test (RelinkerPipeline.cpp:189-191). The PR needs a test that fails on main and passes here, e.g. a synthetic ELF whose DT_RELA/DT_OS_RELA offset is near UINT64_MAX (and one with size not fitting in the file), expecting "Relocation entry out of bounds". Relinker tests live next to the code (see core/relinker/relinker/tests/, and the ELF fixture helpers there); tests register explicitly in core/relinker/CMakeLists.txt.
  • extractRelative has the same pattern (RelinkerPipeline.cpp:294-295): relaOff + off with no bounds check of its own, relying on extractRela having run first over the same ranges. Either give it the same check (preferably through one shared helper instead of two copies of the expression) or say why it can't be reached with different ranges.

Also note #1194 touches the same function for a different defect (partial trailing entries); no conflict expected, but whichever lands second will need a rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5447880087 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917#pullrequestreview-5447880087). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-07T20:22:05Z. **Review:** CHANGES_REQUESTED --- Thanks, the overflow-safe check in `extractRela` looks right to me: with a huge `relaOff`, `pos = relaOff + off` wrapped and passed the old check. Two things before this can merge: - **No regression test** (`RelinkerPipeline.cpp:189-191`). The PR needs a test that fails on `main` and passes here, e.g. a synthetic ELF whose `DT_RELA`/`DT_OS_RELA` offset is near `UINT64_MAX` (and one with size not fitting in the file), expecting "Relocation entry out of bounds". Relinker tests live next to the code (see `core/relinker/relinker/tests/`, and the ELF fixture helpers there); tests register explicitly in `core/relinker/CMakeLists.txt`. - **`extractRelative` has the same pattern** (`RelinkerPipeline.cpp:294-295`): `relaOff + off` with no bounds check of its own, relying on `extractRela` having run first over the same ranges. Either give it the same check (preferably through one shared helper instead of two copies of the expression) or say why it can't be reached with different ranges. Also note #1194 touches the same function for a different defect (partial trailing entries); no conflict expected, but whichever lands second will need a rebase.
Dextroid17 commented 2026-10-07 20:29:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T20:29:16Z.


@muhzain05's finding is real, and it is one instance of a pattern in this file. Mapping it here in case it saves whoever fixes it the sweep — I have not started a branch, and I am happy to stay off it.

The dynamic string table is bounds-checked (RelinkerPipeline.cpp:160). The other three offsets read from the same dynamic section are not:

offset read from bounded?
dynStrTabOffset / dynStrTabSize DT_STRTAB / DT_STRSZ yes, line 160
dynSymTabOffset DT_OS_SYMTAB / DT_SYMTAB, line 124 no
dynRelaOffset / dynRelaSize DT_RELA / DT_RELASZ, lines 143-144 no
dynJmpRelOffset / dynJmpRelSize DT_JMPREL, line 137 no — DT_PLTRELSZ is only checked to be a multiple of 24

Three read sites then rely on a per-entry check that can wrap, or on nothing:

1. The symbol table, line 210. symOff + 4 > raw.size() wraps, while the memcpy uses symOff itself:

dynSymTabOffset = 0xfffffffffffffffe     (straight from DT_OS_SYMTAB, never bounded)
symOff          = 0xfffffffffffffffe
check:  symOff + 4   wraps to 2   <= raw.size()   -> passes
then:   memcpy(raw.data() + 0xfffffffffffffffe, 4)

2. extractRelative, line 293. pos = relaOff + off, then memcpy(raw.data() + pos, 8) with no bounds check at all. extractRela at least had one — the one this PR repairs — while its sibling never did, and relaOff is unchecked.

3. The shape of the fix. This PR's check is right for its own site, but bounding these offsets once, where they are read, covers all three and duplicates the idiom the string table already uses at line 160 (offset > size || size > size - offset). That would leave per-entry arithmetic with nothing to defend.

What I did not do: I have not built a crafted ELF to drive any of this end to end. The evidence above is the code path and the arithmetic, not a run — saying so rather than implying a reproducer I do not have.

@muhzain05 are you taking this? If you are I will stay out of it. If not, I will pick it up and bound the offsets at the read rather than at each use.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6046252240 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917#issuecomment-6046252240). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-07T20:29:16Z. --- @muhzain05's finding is real, and it is one instance of a pattern in this file. Mapping it here in case it saves whoever fixes it the sweep — I have not started a branch, and I am happy to stay off it. The dynamic string table *is* bounds-checked (`RelinkerPipeline.cpp:160`). The other three offsets read from the same dynamic section are not: | offset | read from | bounded? | | --- | --- | --- | | `dynStrTabOffset` / `dynStrTabSize` | `DT_STRTAB` / `DT_STRSZ` | yes, line 160 | | `dynSymTabOffset` | `DT_OS_SYMTAB` / `DT_SYMTAB`, line 124 | **no** | | `dynRelaOffset` / `dynRelaSize` | `DT_RELA` / `DT_RELASZ`, lines 143-144 | **no** | | `dynJmpRelOffset` / `dynJmpRelSize` | `DT_JMPREL`, line 137 | **no** — `DT_PLTRELSZ` is only checked to be a multiple of 24 | Three read sites then rely on a per-entry check that can wrap, or on nothing: **1. The symbol table, line 210.** `symOff + 4 > raw.size()` wraps, while the `memcpy` uses `symOff` itself: ``` dynSymTabOffset = 0xfffffffffffffffe (straight from DT_OS_SYMTAB, never bounded) symOff = 0xfffffffffffffffe check: symOff + 4 wraps to 2 <= raw.size() -> passes then: memcpy(raw.data() + 0xfffffffffffffffe, 4) ``` **2. `extractRelative`, line 293.** `pos = relaOff + off`, then `memcpy(raw.data() + pos, 8)` with **no bounds check at all**. `extractRela` at least had one — the one this PR repairs — while its sibling never did, and `relaOff` is unchecked. **3. The shape of the fix.** This PR's check is right for its own site, but bounding these offsets once, where they are read, covers all three and duplicates the idiom the string table already uses at line 160 (`offset > size || size > size - offset`). That would leave per-entry arithmetic with nothing to defend. What I did **not** do: I have not built a crafted ELF to drive any of this end to end. The evidence above is the code path and the arithmetic, not a run — saying so rather than implying a reproducer I do not have. @muhzain05 are you taking this? If you are I will stay out of it. If not, I will pick it up and bound the offsets at the read rather than at each use.
Dextroid17 commented 2026-10-07 21:19:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T21:19:06Z.


Update, so nobody duplicates it: I picked this up as #1272. It bounds the read rather than bounding the offsets up front, deliberately — an unused-but-bogus table must not start failing titles that work today.

Two things it does not touch, so they are still open if either of you wants one:

  • extractRela (line 191) — this PR (#917) already repairs it.
  • No bound on DT_RELA / DT_RELASZ or DT_JMPREL themselves. #1272 makes each read safe; a table that lies outside the file but is never read is still accepted, and when the per-entry arithmetic does not wrap, nothing names the table as the problem.

If you want either, say so and I will stay out of it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6047063289 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917#issuecomment-6047063289). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-07T21:19:06Z. --- Update, so nobody duplicates it: I picked this up as **#1272**. It bounds the *read* rather than bounding the offsets up front, deliberately — an unused-but-bogus table must not start failing titles that work today. Two things it does **not** touch, so they are still open if either of you wants one: - **`extractRela`** (line 191) — this PR (#917) already repairs it. - **No bound on `DT_RELA` / `DT_RELASZ` or `DT_JMPREL` themselves.** #1272 makes each read safe; a table that lies outside the file but is never read is still accepted, and when the per-entry arithmetic does not wrap, nothing names the table as the problem. If you want either, say so and I will stay out of it.
Dextroid17 commented 2026-10-07 21:41:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T21:41:47Z.


@adriadam10 asked for a test that fails on main and passes here. I verified it, built it, and it is below ready to paste — plus one thing that will block it.

First, the blocker: this branch predates the fixture. core/relinker/relinker/tests/ElfFixture.hpp exists on main (added after your merge-base 5895ea721a43) but not on fix/rela-bounds-check:

main:   HAS ElfFixture.hpp
pr-917: does NOT have it (stale base)

So the advice to "see the ELF fixture helpers there" only lands after a rebase, and the test cannot compile as the branch stands. I applied your diff to current main (it applies cleanly) and worked from there.

The test, both cases you listed. Two files: core/relinker/relinker/tests/RelaBoundsTests.cpp (new, verbatim below) and five lines in core/relinker/CMakeLists.txt, in the same if(UNIX AND ... x86_64) block as to_intel_linux_stubs_tests:

        add_test_executable(rela_bounds_tests
            ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/RelaBoundsTests.cpp
        )
        add_test(NAME rela_bounds COMMAND rela_bounds_tests $<TARGET_FILE:relinker>)
#include "ElfFixture.hpp"
#include "RelinkerProcess.hpp"
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>

namespace {

using namespace RelinkerTests;

void require(const bool condition, const std::string& message) {
    if (!condition) throw std::runtime_error(message);
}

const Bytes kCode = {0xC3};

constexpr std::size_t kTagStride = 16;
constexpr std::size_t kTagBase = kCodeOffset + kDynamicVaddr;
constexpr std::size_t kRelaTag = 4;
constexpr std::size_t kRelaSzTag = 5;
constexpr std::int64_t kDtOsRela = 0x6100002f;
constexpr std::int64_t kDtRelaSz = 8;
constexpr std::size_t kImageSize = 0x8000;

void WriteTag(Bytes& image, const std::size_t index, const std::int64_t tag, const std::uint64_t value) {
    Write<std::int64_t>(image, kTagBase + index * kTagStride, tag);
    Write<std::uint64_t>(image, kTagBase + index * kTagStride + 8, value);
}

void RunAndRequireRejected(const std::string& binary, const Bytes& image, const std::string& what) {
    const TempDirectory directory;
    const auto input = directory.Path() / "input.elf";
    const auto output = directory.Path() / "output.elf";
    WriteFile(input, image);
    const auto run = RunRelinker(binary, {"--skip-sce-module", "--to-intel", input.string(), output.string()}, directory.Path() / "relinker.log");
    require(run.ExitCode != 0, "Relinker accepted a relocation table " + what + " and exited 0:\n" + run.Output);
    require(run.Output.find("Relocation entry out of bounds") != std::string::npos,
            "Relinker did not report the entry as out of bounds for a table " + what + ":\n" + run.Output);
}

}

int main(const int argc, char** argv) {
    try {
        require(argc == 2, "usage: rela_bounds_tests <relinker>");
        const std::string binary = argv[1];

        {
            Bytes image = MakeExecutable(kCode);
            WriteTag(image, kRelaTag, kDtOsRela, 0xFFFFFFFFFFFFFFF8ull);
            WriteTag(image, kRelaSzTag, kDtRelaSz, 24);
            RunAndRequireRejected(binary, image, "whose offset is near UINT64_MAX");
        }

        {
            Bytes image = MakeExecutable(kCode);
            WriteTag(image, kRelaTag, kDtOsRela, kImageSize - 16);
            WriteTag(image, kRelaSzTag, kDtRelaSz, 48);
            RunAndRequireRejected(binary, image, "that does not fit in the file");
        }

        return 0;
    } catch (const std::exception& error) {
        std::cerr << "rela_bounds_tests: " << error.what() << '\n';
        return 1;
    }
}```

**Result — reverted the implementation only, keeping the test:**

| build | outcome |
| --- | --- |
| with the fix | `exit 0` — both cases pass |
| `RelinkerPipeline.cpp` stashed back to `main` | `exit 1` — `rela_bounds_tests: Relinker did not report the entry as out of bounds for a table whose offset is near UINT64_MAX` … `FAIL: Symbol table entry out of bounds (offset 0x185e50)` |

That failure line is the point: on `main` the wrapped `pos` lands 8 bytes before the buffer, the garbage entry's symbol index sends it to the symbol table, and the relinker reports a problem in a **different function**. It never says "Relocation entry out of bounds".

**Two honest notes about what each case proves.**

- The **`UINT64_MAX` case is the discriminator** — it is the only one that changes behaviour.
- The **"size does not fit in the file" case is coverage, not regression coverage**: with `relaOff` in-bounds and `relaSize` over-running, `main`'s `pos + relaEntSize > raw.size()` throws the *same* message, so it passes on both. Worth keeping, but it should not be cited as failing on `main`.
- My first attempt at that second case set `DT_RELASZ` past the file while leaving the entries zeroed. It did **not** reach the guard at all — the loop read the zeros as ordinary entries and died later in `readCStr` (`Dynamic string offset is outside DT_STRSZ`). The version below puts the table 16 bytes from the end so the **first** entry over-runs, which is what actually exercises it.

**On your second point:** `extractRelative` has the same missing check, and it is already handled — it is the second half of #1272, which bounds the same reads at the use site rather than up front (so an unused-but-bogus table cannot start failing titles that work today). Nothing to do here for it.

Happy to open a PR against `TidalImpact:fix/rela-bounds-check` with exactly this if that is easier than pasting — say the word. Either way I am not claiming any of the implementation, only the test.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6047417766 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917#issuecomment-6047417766). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-07T21:41:47Z. --- @adriadam10 asked for a test that fails on `main` and passes here. I verified it, built it, and it is below ready to paste — plus one thing that will block it. **First, the blocker:** this branch predates the fixture. `core/relinker/relinker/tests/ElfFixture.hpp` exists on `main` (added after your merge-base `5895ea721a43`) but not on `fix/rela-bounds-check`: ``` main: HAS ElfFixture.hpp pr-917: does NOT have it (stale base) ``` So the advice to "see the ELF fixture helpers there" only lands after a rebase, and the test cannot compile as the branch stands. I applied your diff to current `main` (it applies cleanly) and worked from there. **The test, both cases you listed.** Two files: `core/relinker/relinker/tests/RelaBoundsTests.cpp` (new, verbatim below) and five lines in `core/relinker/CMakeLists.txt`, in the same `if(UNIX AND ... x86_64)` block as `to_intel_linux_stubs_tests`: ```cmake add_test_executable(rela_bounds_tests ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/RelaBoundsTests.cpp ) add_test(NAME rela_bounds COMMAND rela_bounds_tests $<TARGET_FILE:relinker>) ``` ```cpp #include "ElfFixture.hpp" #include "RelinkerProcess.hpp" #include <cstdint> #include <iostream> #include <stdexcept> #include <string> namespace { using namespace RelinkerTests; void require(const bool condition, const std::string& message) { if (!condition) throw std::runtime_error(message); } const Bytes kCode = {0xC3}; constexpr std::size_t kTagStride = 16; constexpr std::size_t kTagBase = kCodeOffset + kDynamicVaddr; constexpr std::size_t kRelaTag = 4; constexpr std::size_t kRelaSzTag = 5; constexpr std::int64_t kDtOsRela = 0x6100002f; constexpr std::int64_t kDtRelaSz = 8; constexpr std::size_t kImageSize = 0x8000; void WriteTag(Bytes& image, const std::size_t index, const std::int64_t tag, const std::uint64_t value) { Write<std::int64_t>(image, kTagBase + index * kTagStride, tag); Write<std::uint64_t>(image, kTagBase + index * kTagStride + 8, value); } void RunAndRequireRejected(const std::string& binary, const Bytes& image, const std::string& what) { const TempDirectory directory; const auto input = directory.Path() / "input.elf"; const auto output = directory.Path() / "output.elf"; WriteFile(input, image); const auto run = RunRelinker(binary, {"--skip-sce-module", "--to-intel", input.string(), output.string()}, directory.Path() / "relinker.log"); require(run.ExitCode != 0, "Relinker accepted a relocation table " + what + " and exited 0:\n" + run.Output); require(run.Output.find("Relocation entry out of bounds") != std::string::npos, "Relinker did not report the entry as out of bounds for a table " + what + ":\n" + run.Output); } } int main(const int argc, char** argv) { try { require(argc == 2, "usage: rela_bounds_tests <relinker>"); const std::string binary = argv[1]; { Bytes image = MakeExecutable(kCode); WriteTag(image, kRelaTag, kDtOsRela, 0xFFFFFFFFFFFFFFF8ull); WriteTag(image, kRelaSzTag, kDtRelaSz, 24); RunAndRequireRejected(binary, image, "whose offset is near UINT64_MAX"); } { Bytes image = MakeExecutable(kCode); WriteTag(image, kRelaTag, kDtOsRela, kImageSize - 16); WriteTag(image, kRelaSzTag, kDtRelaSz, 48); RunAndRequireRejected(binary, image, "that does not fit in the file"); } return 0; } catch (const std::exception& error) { std::cerr << "rela_bounds_tests: " << error.what() << '\n'; return 1; } }``` **Result — reverted the implementation only, keeping the test:** | build | outcome | | --- | --- | | with the fix | `exit 0` — both cases pass | | `RelinkerPipeline.cpp` stashed back to `main` | `exit 1` — `rela_bounds_tests: Relinker did not report the entry as out of bounds for a table whose offset is near UINT64_MAX` … `FAIL: Symbol table entry out of bounds (offset 0x185e50)` | That failure line is the point: on `main` the wrapped `pos` lands 8 bytes before the buffer, the garbage entry's symbol index sends it to the symbol table, and the relinker reports a problem in a **different function**. It never says "Relocation entry out of bounds". **Two honest notes about what each case proves.** - The **`UINT64_MAX` case is the discriminator** — it is the only one that changes behaviour. - The **"size does not fit in the file" case is coverage, not regression coverage**: with `relaOff` in-bounds and `relaSize` over-running, `main`'s `pos + relaEntSize > raw.size()` throws the *same* message, so it passes on both. Worth keeping, but it should not be cited as failing on `main`. - My first attempt at that second case set `DT_RELASZ` past the file while leaving the entries zeroed. It did **not** reach the guard at all — the loop read the zeros as ordinary entries and died later in `readCStr` (`Dynamic string offset is outside DT_STRSZ`). The version below puts the table 16 bytes from the end so the **first** entry over-runs, which is what actually exercises it. **On your second point:** `extractRelative` has the same missing check, and it is already handled — it is the second half of #1272, which bounds the same reads at the use site rather than up front (so an unused-but-bogus table cannot start failing titles that work today). Nothing to do here for it. Happy to open a PR against `TidalImpact:fix/rela-bounds-check` with exactly this if that is easier than pasting — say the word. Either way I am not claiming any of the implementation, only the test.
muhzain05 commented 2026-10-08 02:49:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by muhzain05 on 2026-10-08T02:49:01Z.


@muhzain05's finding is real, and it is one instance of a pattern in this file. Mapping it here in case it saves whoever fixes it the sweep — I have not started a branch, and I am happy to stay off it.

The dynamic string table is bounds-checked (RelinkerPipeline.cpp:160). The other three offsets read from the same dynamic section are not:

offset read from bounded?
dynStrTabOffset / dynStrTabSize DT_STRTAB / DT_STRSZ yes, line 160
dynSymTabOffset DT_OS_SYMTAB / DT_SYMTAB, line 124 no
dynRelaOffset / dynRelaSize DT_RELA / DT_RELASZ, lines 143-144 no
dynJmpRelOffset / dynJmpRelSize DT_JMPREL, line 137 no — DT_PLTRELSZ is only checked to be a multiple of 24
Three read sites then rely on a per-entry check that can wrap, or on nothing:

1. The symbol table, line 210. symOff + 4 > raw.size() wraps, while the memcpy uses symOff itself:

dynSymTabOffset = 0xfffffffffffffffe     (straight from DT_OS_SYMTAB, never bounded)
symOff          = 0xfffffffffffffffe
check:  symOff + 4   wraps to 2   <= raw.size()   -> passes
then:   memcpy(raw.data() + 0xfffffffffffffffe, 4)

2. extractRelative, line 293. pos = relaOff + off, then memcpy(raw.data() + pos, 8) with no bounds check at all. extractRela at least had one — the one this PR repairs — while its sibling never did, and relaOff is unchecked.

3. The shape of the fix. This PR's check is right for its own site, but bounding these offsets once, where they are read, covers all three and duplicates the idiom the string table already uses at line 160 (offset > size || size > size - offset). That would leave per-entry arithmetic with nothing to defend.

What I did not do: I have not built a crafted ELF to drive any of this end to end. The evidence above is the code path and the arithmetic, not a run — saying so rather than implying a reproducer I do not have.

@muhzain05 are you taking this? If you are I will stay out of it. If not, I will pick it up and bound the offsets at the read rather than at each use.

Thanks, I checked the current open PRs and I don’t see anyone taking the DT_JMPREL bounds side, so I’ll take that one.

I’ll keep it separate from #1272’s per-read bounds work and focus specifically on validating the DT_JMPREL / DT_PLTRELSZ table range with a malformed-ELF regression that fails on current main.

I’ll leave extractRela, extractRelative, and the symbol-table read work to #917/#1272.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051153826 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917#issuecomment-6051153826). Originally posted by [muhzain05](https://github.com/muhzain05) on 2026-10-08T02:49:01Z. --- > @muhzain05's finding is real, and it is one instance of a pattern in this file. Mapping it here in case it saves whoever fixes it the sweep — I have not started a branch, and I am happy to stay off it. > > The dynamic string table _is_ bounds-checked (`RelinkerPipeline.cpp:160`). The other three offsets read from the same dynamic section are not: > > offset read from bounded? > `dynStrTabOffset` / `dynStrTabSize` `DT_STRTAB` / `DT_STRSZ` yes, line 160 > `dynSymTabOffset` `DT_OS_SYMTAB` / `DT_SYMTAB`, line 124 **no** > `dynRelaOffset` / `dynRelaSize` `DT_RELA` / `DT_RELASZ`, lines 143-144 **no** > `dynJmpRelOffset` / `dynJmpRelSize` `DT_JMPREL`, line 137 **no** — `DT_PLTRELSZ` is only checked to be a multiple of 24 > Three read sites then rely on a per-entry check that can wrap, or on nothing: > > **1. The symbol table, line 210.** `symOff + 4 > raw.size()` wraps, while the `memcpy` uses `symOff` itself: > > ``` > dynSymTabOffset = 0xfffffffffffffffe (straight from DT_OS_SYMTAB, never bounded) > symOff = 0xfffffffffffffffe > check: symOff + 4 wraps to 2 <= raw.size() -> passes > then: memcpy(raw.data() + 0xfffffffffffffffe, 4) > ``` > > **2. `extractRelative`, line 293.** `pos = relaOff + off`, then `memcpy(raw.data() + pos, 8)` with **no bounds check at all**. `extractRela` at least had one — the one this PR repairs — while its sibling never did, and `relaOff` is unchecked. > > **3. The shape of the fix.** This PR's check is right for its own site, but bounding these offsets once, where they are read, covers all three and duplicates the idiom the string table already uses at line 160 (`offset > size || size > size - offset`). That would leave per-entry arithmetic with nothing to defend. > > What I did **not** do: I have not built a crafted ELF to drive any of this end to end. The evidence above is the code path and the arithmetic, not a run — saying so rather than implying a reproducer I do not have. > > @muhzain05 are you taking this? If you are I will stay out of it. If not, I will pick it up and bound the offsets at the read rather than at each use. Thanks, I checked the current open PRs and I don’t see anyone taking the `DT_JMPREL` bounds side, so I’ll take that one. I’ll keep it separate from #1272’s per-read bounds work and focus specifically on validating the `DT_JMPREL` / `DT_PLTRELSZ` table range with a malformed-ELF regression that fails on current `main`. I’ll leave `extractRela`, `extractRelative`, and the symbol-table read work to #917/#1272.
muhzain05 commented 2026-10-08 04:23:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by muhzain05 on 2026-10-08T04:23:59Z.


@Dextroid17 Took the DT_JMPREL / DT_PLTRELSZ bounds side as discussed, PR is up now as #1424.

I kept it separate from #917/#1272 and focused only on the table-range validation path.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6052181214 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917#issuecomment-6052181214). Originally posted by [muhzain05](https://github.com/muhzain05) on 2026-10-08T04:23:59Z. --- @Dextroid17 Took the DT_JMPREL / DT_PLTRELSZ bounds side as discussed, PR is up now as #1424. I kept it separate from #917/#1272 and focused only on the table-range validation path.
adriadam10 commented 2026-10-08 05:58:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T05:58:32Z.

Review: APPROVED


Thanks. The overflow-safe check in extractRela/extractRelative is correct: with a huge relaOff, relaOff + off used to wrap and pass the old check.

What I added on your branch (fix-merge):

  • Rebased on main (the test needs ElfFixture.hpp).
  • RelaBoundsTests.cpp + CMake entry, from @Dextroid17's test in this thread (co-authored): a DT_OS_RELA offset near UINT64_MAX, and a table that doesn't fit in the file. The first case fails on main without the fix and passes with it; the second passes on both (coverage only).
  • Reworded the two commits and the PR title to Conventional Commits (the conventions job was failing on them).

Full build + ctest green locally (416/416). Merging once CI is green. #1272 will need a normal rebase afterwards.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5452197627 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/917#pullrequestreview-5452197627). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T05:58:32Z. **Review:** APPROVED --- Thanks. The overflow-safe check in `extractRela`/`extractRelative` is correct: with a huge `relaOff`, `relaOff + off` used to wrap and pass the old check. What I added on your branch (fix-merge): - Rebased on `main` (the test needs `ElfFixture.hpp`). - `RelaBoundsTests.cpp` + CMake entry, from @Dextroid17's test in this thread (co-authored): a `DT_OS_RELA` offset near `UINT64_MAX`, and a table that doesn't fit in the file. The first case fails on `main` without the fix and passes with it; the second passes on both (coverage only). - Reworded the two commits and the PR title to Conventional Commits (the `conventions` job was failing on them). Full build + ctest green locally (416/416). Merging once CI is green. #1272 will need a normal rebase afterwards.
Sign in to join this conversation.
No reviewers
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!917
No description provided.