Skip to content

feat(fspy_elf_loader): add ELF validation, PF_X metadata, and dynamic stack sizing - #1027

Open
wan9chi wants to merge 11 commits into
fspy-linux/1-nostd-primitivesfrom
fspy-linux/2-elf-loader-validation-stack
Open

wan9chi wants to merge 11 commits into
fspy-linux/1-nostd-primitivesfrom
fspy-linux/2-elf-loader-validation-stack

Conversation

@wan9chi

@wan9chi wan9chi commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Motivation

Before fspy_elf_loader can host a SIGSYS syscall-virtualization runtime for arbitrary Linux executables, it needs fallible pre-exec ELF validation (so invalid executables return ENOEXEC instead of panicking inside the launcher), fixed high-address link-arg propagation for IP allowlisting, RLIMIT_STACK-aware stack sizing, and /proc/self/comm initialization matching kernel execve.

Changes

  • Export fixed-address --no-pie --image-base=... link args from fspy_elf_loader/build.rs via links metadata and consume them in fspy_elf_loader_test_launcher/build.rs.
  • Add validate_exec, fallible ELF metadata mapping (Mapping::try_file), ET_DYN high-p_vaddr load-bias fixes, and segment validation that accepts what Linux binfmt_elf accepts (an executable PT_GNU_STACK maps the stack executable; overlapping loads, non-power-of-two p_align, and ET_EXEC interpreters are allowed; AT_PHDR is computed as the kernel does), with unit tests in image/tests.rs.
  • Size the target process stack dynamically from RLIMIT_STACK via prlimit64 (clamped to [8 MiB, 64 MiB]) with unit tests in stack.rs, and update /proc/self/comm via PR_SET_NAME.

Stack created with GitHub Stacks CLI • Give Feedback 💬


Note

Medium Risk
Touches in-process ELF mapping, fixed-address reservations, and raw syscalls (prlimit64, prctl) on the critical path to running untrusted executables; regressions would break launcher startup or memory layout rather than typical app logic.

Overview
Adds validate_exec so callers can reject bad ELF/interpreter images (returning NOEXEC / EEXIST for launcher-region overlap) without mapping segments or closing the fd — a prerequisite for pre-exec checks before the freestanding launcher jumps in.

ELF handling is reworked toward Linux binfmt_elf behavior: fallible parsing/open via BorrowedFd, separate validate_segments then map, acceptance of executable PT_GNU_STACK, overlapping loads, and high p_vaddr PIE bias without underflow; AT_PHDR and padzero clamping are tightened.

exec_elf now sets PR_SET_NAME from execfn, sizes the target stack from RLIMIT_STACK (clamped 8–64 MiB) with optional rwx stack, and centralizes freestanding launcher link placement via build.rs links metadata consumed by fspy_elf_loader_test_launcher. Integration/runtime tests cover malformed binaries, executable stacks, and auxv/stack layout expectations.

Reviewed by Cursor Bugbot for commit d6d08a6. Bugbot is set up for automated code reviews on this repo. Configure here.

@wan9chi
wan9chi added this pull request to stack #1030 November 27, 2026 00:33
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

fspy benchmark

linux

dynamic/launch             change  +0.78%  [ -3.27% ..  +5.17%]  overhead   +55.95%
dynamic/access             change  +0.00%  [ -4.47% ..  +4.75%]  overhead   +24.23%
dynamic/access-relative    change  +0.88%  [-16.28% .. +20.29%]  overhead   +68.42%
dynamic/access-contended   change  -0.75%  [-11.88% ..  +9.57%]  overhead   +11.75%
static/launch              change  +0.11%  [ -5.47% ..  +5.24%]  overhead  +196.81%
static/access              change  +0.57%  [ -2.38% .. +11.01%]  overhead +1235.71%
static/access-relative     change  -0.74%  [ -3.22% ..  +1.93%]  overhead +1940.07%
static/access-contended    change  -0.19%  [-11.59% ..  +9.80%]  overhead +1415.72%

macos

dynamic/launch             change  +0.39%  [ -2.20% ..  +2.38%]  overhead  +227.48%
dynamic/access             change  -0.37%  [ -2.42% ..  +1.59%]  overhead    +6.02%
dynamic/access-relative    change  +0.00%  [ -3.35% ..  +2.96%]  overhead  +268.51%
dynamic/access-contended   change  +0.19%  [ -2.08% ..  +2.11%]  overhead    +2.89%

windows

dynamic/launch             change  -0.26%  [ -1.28% ..  +1.26%]  overhead   +27.03%
dynamic/access             change  +0.00%  [ -0.77% ..  +0.58%]  overhead    +1.16%
dynamic/access-relative    change  +0.19%  [ -0.55% ..  +0.94%]  overhead    +1.31%
dynamic/access-contended   change  +0.18%  [ -0.54% ..  +0.54%]  overhead    +1.46%

… stack sizing

Harden ELF image validation and stack setup ahead of SIGSYS syscall
virtualization:

- Export fixed-address `--no-pie --image-base=...` link args from
  `fspy_elf_loader/build.rs` and consume them in the test launcher.
- Add `validate_exec`, fallible ELF metadata mapping (`Mapping::try_file`),
  `PT_GNU_STACK` `PF_X` rejection, and stricter segment validation with
  unit tests in `image/tests.rs`.
- Size the target process stack dynamically from `RLIMIT_STACK` (clamped
  to `[8 MiB, 64 MiB]`) with unit tests in `stack.rs`, and update
  `/proc/self/comm` via `PR_SET_NAME`.

Co-authored-by: Gemini 4 Neon <noreply@google.com>
@wan9chi
wan9chi force-pushed the fspy-linux/2-elf-loader-validation-stack branch from 3a359d1 to 28f84bd Compare November 27, 2026 00:46
wan9chi and others added 6 commits November 27, 2026 11:13
…ng them

`validate_exec` returned `ENOEXEC` for images the kernel loads, and shells
turn that into a confusing "cannot execute binary file" under `vp run`
only. Match `binfmt_elf` instead:

- An executable `PT_GNU_STACK` (e.g. objects without `.note.GNU-stack`,
  gcc nested-function trampolines) maps the target stack executable.
  The interpreter's `PT_GNU_STACK` is ignored.
- Overlapping `PT_LOAD`s are accepted; a later `MAP_FIXED` wins.
- A non-power-of-two `p_align` is ignored.
- The first `PT_INTERP` is used; an `ET_EXEC` interpreter or one with its
  own `PT_INTERP` is accepted.
- `AT_PHDR` comes from the `PT_LOAD` containing `e_phoff`, or the load bias
  when none does, instead of requiring the whole table to be mapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ons, and expand tests

- Clamp padzero using other_start > start && other_start >= file_end so BSS
  extending past a higher-vaddr LOAD on the same page cannot wipe it without
  self-clamping zero-filesz segments.
- Require the full program-header table (headers.end <= file_start + p_filesz)
  before computing AT_PHDR from a PT_LOAD segment, with unit tests for both.
- Share LAUNCHER_REGION_X86_64 and LAUNCHER_REGION_AARCH64 from
  src/launcher_region.rs across build.rs, lib.rs, and launcher_image tests;
  export LAUNCHER_REGION and add rerun-if-changed directives in build.rs.
- Replace RLIM_INFINITY signed cast with u64::MAX in stack.rs and simplify
  unreachable null-mmap handling in mapping.rs.
- Expand exec_elf and runtime.c tests for rwxp stack verification, malformed
  ELF/PT_INTERP/entry/alignment rejections, AT_PLATFORM/AT_BASE_PLATFORM,
  16-byte AT_RANDOM alignment, and argv/envp string contiguity.

Co-authored-by: Gemini 4 Neon <noreply@google.com>
Co-authored-by: Gemini 4 Neon <noreply@google.com>
Co-authored-by: Gemini 4 Neon <noreply@google.com>

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0452b6e. Configure here.

Comment thread crates/fspy_elf_loader/src/image.rs Resolved
wan9chi and others added 4 commits November 27, 2026 18:28
…segments

Co-authored-by: Gemini 4 Neon <noreply@google.com>
…ions

Co-authored-by: Gemini 4 Neon <noreply@google.com>
…ON with EEXIST

Co-authored-by: Gemini 4 Neon <noreply@google.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant