loader: validate ELF and SELF file bounds #348

Closed
github-bot wants to merge 1 commit from gh-pr/348/head into gh-pr/348/base
Owner

@⁠Techx3 on GitHub · 2026-08-28 19:47 UTC · original
Techx3:techx3/elf-self-bounds → main · +568 −15 · 6 files · 1 commits · closed without merging

Summary

  • validate ELF/SELF file-backed ranges before Elf64 allocates or reads loader tables
  • use overflow-safe arithmetic for program tables, section tables, segment payloads, and SELF payloads
  • validate SELF program-header references before LoadSegment indexes them
  • share the exact SELF header classification between validation and loading
  • report unsupported compression and inconsistent SELF sizes explicitly

Safety and compatibility details

The validator runs before the existing loader reads the image. It checks only invariants that the current Elf64 implementation already relies on:

  • supported ELF/SELF header variants
  • exact table entry sizes consumed by the loader
  • complete file-backed tables and payloads
  • at most one process-parameter segment
  • valid SELF-to-program-header indices
  • uncompressed SELF payloads whose sizes match the referenced ELF program headers

Zero-length segments and empty tables may retain otherwise unused offsets, avoiding over-validation. The validator does not authenticate, decrypt, or claim general SELF format support.

The local SDK 12 corpus has no ELF/SELF container documentation matching these fields, so this PR deliberately makes no SDK-derived metadata or format claims. The SDK-version extraction present in the experimental KytyTouche implementation was omitted.

Relationship to #302

PR #302 improves named boot-header failures. This PR covers deeper table, payload, arithmetic-overflow, and SELF-reference bounds that #302 does not validate. Both touch elf.cpp, so a small rebase conflict is expected if #302 merges first; the functionality is complementary.

Validation

  • dedicated elf_validator_tests Release build: passed
  • direct elf_validator_tests.exe: passed
  • ctest --test-dir _Build/launcher-usability -C Release -R "^elf_validator$" --output-on-failure: 1/1 passed
  • full kyty_emulator Release build with clang-cl: passed

Tests cover raw ELF, real file I/O, truncated and overflowing tables, out-of-range and zero-length segments, section payloads, SHT_NOBITS, empty tables, supported and unsupported SELF headers, declared size, invalid program-header references, compression, size mismatches, and overflowing ranges.

Provenance

This isolates and substantially hardens the ELF validator introduced in KytyTouche commit 9b5f2cd. Original authorship is retained through the commit co-author trailer.

**@⁠Techx3** on GitHub · 2026-08-28 19:47 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/348) `Techx3:techx3/elf-self-bounds` → `main` · +568 −15 · 6 files · 1 commits · closed without merging ## Summary - validate ELF/SELF file-backed ranges before `Elf64` allocates or reads loader tables - use overflow-safe arithmetic for program tables, section tables, segment payloads, and SELF payloads - validate SELF program-header references before `LoadSegment` indexes them - share the exact SELF header classification between validation and loading - report unsupported compression and inconsistent SELF sizes explicitly ## Safety and compatibility details The validator runs before the existing loader reads the image. It checks only invariants that the current `Elf64` implementation already relies on: - supported ELF/SELF header variants - exact table entry sizes consumed by the loader - complete file-backed tables and payloads - at most one process-parameter segment - valid SELF-to-program-header indices - uncompressed SELF payloads whose sizes match the referenced ELF program headers Zero-length segments and empty tables may retain otherwise unused offsets, avoiding over-validation. The validator does not authenticate, decrypt, or claim general SELF format support. The local SDK 12 corpus has no ELF/SELF container documentation matching these fields, so this PR deliberately makes no SDK-derived metadata or format claims. The SDK-version extraction present in the experimental KytyTouche implementation was omitted. ## Relationship to #302 PR #302 improves named boot-header failures. This PR covers deeper table, payload, arithmetic-overflow, and SELF-reference bounds that #302 does not validate. Both touch `elf.cpp`, so a small rebase conflict is expected if #302 merges first; the functionality is complementary. ## Validation - dedicated `elf_validator_tests` Release build: passed - direct `elf_validator_tests.exe`: passed - `ctest --test-dir _Build/launcher-usability -C Release -R "^elf_validator$" --output-on-failure`: 1/1 passed - full `kyty_emulator` Release build with clang-cl: passed Tests cover raw ELF, real file I/O, truncated and overflowing tables, out-of-range and zero-length segments, section payloads, `SHT_NOBITS`, empty tables, supported and unsupported SELF headers, declared size, invalid program-header references, compression, size mismatches, and overflowing ranges. ## Provenance This isolates and substantially hardens the ELF validator introduced in KytyTouche commit `9b5f2cd`. Original authorship is retained through the commit co-author trailer.
Validate file-backed headers, tables, segment payloads, section payloads, and SELF program-header references before Elf64 reads them. Use checked range arithmetic and preserve legal unused offsets on zero-length entries.

Share SELF classification with the loader, reject unsupported compression and inconsistent sizes explicitly, and cover raw ELF, SELF, overflow, truncation, section, file-I/O, and empty-table cases.

Co-authored-by: SueryRD <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:17:08 +00:00
github-bot closed this pull request 2026-11-26 09:17:08 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.