Skip to content

feat(fspy_elf_loader): add SIGSYS syscall interception, signal virtualization, and /proc/exe rewriting - #1028

Open
wan9chi wants to merge 49 commits into
fspy-linux/2-elf-loader-validation-stackfrom
fspy-linux/3-elf-loader-sigsys
Open

wan9chi wants to merge 49 commits into
fspy-linux/2-elf-loader-validation-stackfrom
fspy-linux/3-elf-loader-sigsys

Conversation

@wan9chi

@wan9chi wan9chi commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Motivation

To track filesystem accesses uniformly across glibc, musl, static binaries, and direct-syscall runtimes on Linux without LD_PRELOAD or seccomp_unotify, the userland ELF loader needs an in-process seccomp-BPF SECCOMP_RET_TRAP (SIGSYS) virtualization runtime that transparently preserves target signal semantics, vfork/posix_spawn, multithreading, and /proc/*/exe identity while exposing a clean syscall-interception hook.

Changes

  • sigsys/filter.rs: seccomp-BPF filter allowlisting the launcher's fixed PF_X range, rejecting io_uring_setup and clone3 with ENOSYS, and trapping signal, exec, /proc/*/exe, and caller-supplied syscalls (with unit tests).
  • sigsys/signal.rs, sigsys/arch.rs, and sigsys/stack_pool.rs: per-thread virtual SIGSYS disposition/mask virtualization, pooled private SIGSYS stacks, vfork/posix_spawn altstack handoff returning via rt_sigreturn (relocating x86-64 fpregs; CET shadow stacks, Arm GCS, and SME EXTRA_MAGIC frames are unsupported), and masked-wait deferred-SIGSYS handling (with unit tests).
  • sigsys/proc_exe.rs and sigsys/usermem.rs: /proc/*/exe path rewriting (with unit tests) and user memory copies that, like the macOS and Windows preloads, assume the pointers passed to syscalls are valid.
  • exec_elf / try_exec_elf: take argv_in_place, so the target can keep the launcher's own kernel argument strings as its argv and writes to them (such as process titles) show in /proc/<pid>/cmdline.
  • fspy_elf_loader_test_launcher and fspy_elf_loader_tests: wire --test-interceptor and add the 39-test Linux virtualization integration suite in fixtures/virtualization.c and tests/exec_elf.rs.

Stack created with GitHub Stacks CLI • Give Feedback 💬


Note

High Risk
Large, security-sensitive runtime changes around seccomp, signals, vfork/clone, and exec/path rewriting that must stay correct across glibc/musl and both x86-64 and AArch64.

Overview
Adds a full Linux SIGSYS + seccomp-BPF runtime to the freestanding ELF loader: trapped syscalls run from a private pooled handler stack, with virtualized rt_sigaction / rt_sigprocmask / fork / vfork behavior and /proc/*/exe rewriting so the tracked process sees its real executable path.

Exports a SyscallInterceptor hook (Replay vs replacement execve) and wires installation through new try_exec_elf (fallible load/map/stack/sigsys::install) while exec_elf now takes argv_in_place and an optional interceptor. ELF mapping and reservations are fallible (try_map, try_reserve*) instead of panicking on mmap failures.

New modules include cBPF filter construction, arch-specific naked asm dispatch, proc_exe path matching, and integration coverage via virtualization.c fixtures and updated loader tests/docs.

Reviewed by Cursor Bugbot for commit ba828b8. 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.11%  [ -5.27% ..  +7.89%]  overhead   +64.23%
dynamic/access             change  +1.47%  [-11.79% .. +13.40%]  overhead   +19.49%
dynamic/access-relative    change  +0.57%  [ -5.60% ..  +7.55%]  overhead   +65.98%
dynamic/access-contended   change  +5.23%  [ -7.92% .. +15.84%]  overhead   +23.58%
static/launch              change  -0.58%  [ -7.41% ..  +5.57%]  overhead  +202.06%
static/access              change  +1.05%  [ -8.00% .. +11.55%]  overhead +1332.43%
static/access-relative     change  +0.46%  [ -3.02% .. +12.00%]  overhead +1880.28%
static/access-contended    change  +0.69%  [-11.03% ..  +6.98%]  overhead  +976.83%

macos

dynamic/launch             change  -0.04%  [ -2.69% ..  +2.81%]  overhead  +227.81%
dynamic/access             change  +0.38%  [ -2.00% ..  +3.06%]  overhead    +6.10%
dynamic/access-relative    change  +0.12%  [ -3.62% ..  +3.35%]  overhead  +230.18%
dynamic/access-contended   change  -0.20%  [ -2.59% ..  +1.85%]  overhead    +2.67%

windows

dynamic/launch             change  -0.12%  [ -6.84% ..  +4.27%]  overhead   +25.81%
dynamic/access             change  -0.68%  [-16.53% ..  +9.68%]  overhead    +2.03%
dynamic/access-relative    change  -0.17%  [-13.31% .. +18.61%]  overhead    +1.23%
dynamic/access-contended   change  -1.16%  [ -5.48% ..  +2.63%]  overhead    +1.82%

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

Stale Bugbot comment from a previous run.

wan9chi and others added 3 commits November 27, 2026 08:45
…lization, and /proc/exe rewriting

Add the in-process SIGSYS syscall virtualization engine to
`fspy_elf_loader`, wire it into the freestanding test launcher, and add
unit and Linux virtualization integration tests:

- `sigsys/filter.rs`: seccomp-BPF filter construction allowlisting the
  launcher's `PF_X` range, rejecting `io_uring_setup` with `ENOSYS`,
  and trapping signal/exec/`/proc/exe` and caller-supplied syscalls.
- `sigsys/signal.rs` & `sigsys/arch.rs`: per-thread virtual `SIGSYS`
  disposition/mask virtualization, pooled SIGSYS stacks, `vfork` /
  `posix_spawn` altstack handoff with `rt_sigreturn` (including AArch64
  `EXTRA_MAGIC`/GCS and x86-64 `fpregs`/CET handling), and masked-wait
  deferred-SIGSYS handling.
- `sigsys/proc_exe.rs` & `sigsys/usermem.rs`: `/proc/*/exe` path
  rewriting and fault-safe user memory copy/probe helpers.
- `fspy_elf_loader_tests`: 39-test Linux integration suite in
  `fixtures/virtualization.c` and `tests/exec_elf.rs`.

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

Co-authored-by: Gemini 4 Neon <noreply@google.com>
@wan9chi
wan9chi force-pushed the fspy-linux/3-elf-loader-sigsys branch from c34be75 to 17f3396 Compare November 27, 2026 00:46

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

Stale Bugbot comment from a previous run.

wan9chi and others added 4 commits November 27, 2026 10:45
… of via process_vm_readv

The SIGSYS runtime copied every user pointer it read or wrote (paths,
`struct sigaction`, signal sets, `open_how`, timeouts, the
`/proc/self/exe` readlink buffer) through `process_vm_readv` /
`process_vm_writev` on its own pid, so a bad pointer returned `EFAULT`
instead of crashing the handler. That costs a `getpid` plus a copy
syscall per pointer, and it breaks under filters the target installs
itself: Chromium's sandboxed renderer traps `process_vm_readv`, and the
trap reached Chromium's handler, which crashed the renderer.

Assume the pointers are valid, as the macOS and Windows preloads already
do for the calls they interpose, and access them directly. Addresses in
the first page (`NULL`, small integers) still fail with `EFAULT` without
being touched. The copy helpers become `unsafe fn` with that contract.
Abseil's `rt_sigprocmask(~0, addr)` address probe already goes straight
to the kernel, so it keeps returning `EFAULT`/`EINVAL` as before.

Tests that checked fault-safe copies of bad pointers, including the
`process_vm_*`-denied fallback, are removed; the Abseil probe cases stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ched_cstr

Now that `usermem` reads trapped syscall arguments directly instead of
copying through `process_vm_readv`, copying path arguments into a 4 KiB
stack buffer in `dispatch_phase1` / `proc_exe::rewrite_path_arg` and
caching that slice on `Syscall` is unnecessary:

- Replace `usermem::read_user_cstr` / `copy_user_cstr` with zero-copy
  `user_cstr(addr) -> Result<CStr<'_, Thin>>` (still rejecting page 0
  with `Error::FAULT`).
- Remove `Prefetched::path`, `Syscall::cached_cstr`, and the 4 KiB
  `path_buf` from `dispatch_phase1` and `proc_exe::rewrite_path_arg`.
- Update `fspy_elf_loader_test_launcher` to use `user_cstr` directly.

Co-authored-by: Gemini 4 Neon <noreply@google.com>
…ux/3-elf-loader-sigsys

# Conflicts:
#	crates/fspy_elf_loader/src/lib.rs
…om the vfork path

The vfork/posix_spawn child returns through a copied signal frame. The
copy also disabled an x86 CET shadow stack or an Arm GCS in the child
(with a hand-written register restore when either was locked), cleared
the GCS record, and relocated SVE/SME `EXTRA_MAGIC` payloads.

None of that runs for real programs: userspace CET shadow stacks are
opt-in, GCS needs hardware that isn't deployed, and the kernel discards
SVE state on syscall entry, so a trapped frame only outgrows `__reserved`
with live SME `ZA` state. Copy the fixed-size frame and return via
`rt_sigreturn` instead; such processes are documented as unsupported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.

Stale Bugbot comment from a previous run.

wan9chi and others added 2 commits November 27, 2026 12:24
The typos check rejects the bare abbreviation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…roc/*/exe comment

`lstat` never follows `/proc/self/exe`, so trapping it in the base set
only added a SIGSYS round trip. The `/proc/*/exe` matcher does not
resolve paths relative to the cwd; say so instead of claiming it does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.

Stale comment

Agentic security review found one high-severity issue: the native-architecture gate allowlists all foreign/compat syscalls, which bypasses the SIGSYS interception this PR otherwise enforces.

Open in Web View Automation 

Sent by Cursor Security Agent: Security Reviewer

wan9chi and others added 11 commits November 27, 2026 13:01
…_exe

`AT_FDCWD` is `-100` on Linux, not `-1`, so `BorrowedFd::try_borrow_raw`
returns `Some(CWD)` and `fd_path` resolves `getcwd`. Restore the comment
to match the actual behavior.

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

- Add execve and execveat to BASE_TRAPPED_SYSCALLS for standalone /proc/*/exe virtualization
- Drop SA_NODEFER on the loader SIGSYS handler so async SIGSYS cannot preempt Phase 1 on stack_pool
- Clear pending virtual SIGSYS across all threads in the process on rt_sigaction(SIGSYS, SIG_IGN)
- Write masked-wait syscall results into ucontext before deliver_sigsys_now and deliver deferred SIGSYS on handler return
- Stop lexically collapsing '..' in proc_exe::normalize_abs_path and skip rewriting non-readlink syscalls for ' (deleted)' executables
- Expose fallible try_exec_elf(..., on_ready) and SyscallInterceptor::on_vfork_return validated inside PF_X
- Add usermem::user_cstr_bounded bounded at PATH_MAX for /proc/*/exe matching
- Replace ACTION_LOCK and waitid(WNOWAIT) lock-stealing with a lock-free SeqAction seqlock
- Simplify replay_masked_wait_deferring_sigsys by removing MAX_ZERO_POLL_RETRIES, clear_pselect_fdsets, and masked_wait_return_pc
- Simplify BPF filter with symbolic jump patching and assert!(start_hi == end_hi), add PROT_NONE guard pages and Relaxed load guard to stack_pool, and remove dead stores/fallbacks
- Add integration tests for AArch64 x28, raw current_sp vfork save_top + separate altstack, io_uring_setup ENOSYS, SyscallAction::Execve failure/cleanup, inherited SIGSYS state, sigqueue/SA_NODEFER/SIG_IGN/epoll_pwait edge cases, raw SYS_fork + mismatched-fingerprint re-exec, /proc/*/exe edge cases, stack pool overflow, and blocking openat2 FIFO EINTR

Co-authored-by: Gemini 4 Neon <noreply@google.com>
…ux/3-elf-loader-sigsys

# Conflicts:
#	crates/fspy_elf_loader/src/lib.rs
#	crates/fspy_elf_loader/src/mapping.rs
#	crates/fspy_elf_loader/src/stack.rs
…ad INFO_LOCK_BIT leak

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

Co-authored-by: Gemini 4 Neon <noreply@google.com>
…roc/self/exe re-exec

Co-authored-by: Gemini 4 Neon <noreply@google.com>
…and preserve CLONE_VM SIGSYS overrides

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 da112b1. Configure here.

…ux/3-elf-loader-sigsys

# Conflicts:
#	crates/fspy_elf_loader/Cargo.toml
#	crates/fspy_elf_loader/src/image.rs
wan9chi and others added 27 commits November 28, 2026 01:10
Co-authored-by: Gemini 4 Neon <noreply@google.com>
Open the interpreter with O_NONBLOCK and require a regular file with
execute permission, as the kernel does for interpreters and as the main
executable already does. A 0644 interpreter now fails with EACCES instead
of running, and a FIFO interpreter no longer blocks startup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ux/3-elf-loader-sigsys

# Conflicts:
#	crates/fspy_elf_loader/src/lib.rs
#	crates/fspy_elf_loader_tests/tests/exec_elf.rs
…xe link fd

An O_PATH | O_NOFOLLOW descriptor of /proc/self/exe read back with
readlinkat(fd, "") reached the kernel and returned the launcher path.
Resolve the descriptor through /proc/self/fd/<fd> and answer with the
target path when it names this process's exe link. The matcher shared
by the path-following syscalls is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The PT_INTERP check now requires execute permission, as Linux does, so
the unit tests' temp-file interpreters need mode 0755.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SA_NODEFER only skips the automatic self-block. A SIGSYS handler that
also lists SIGSYS in sa_mask must still defer a nested SIGSYS until it
returns, as Linux does, instead of re-entering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An interpreter whose header or program headers are unusable now fails with
ELIBBAD, and one too short for an ELF header with EIO, as Linux
load_elf_binary reports them. ENOEXEC made execvp run the binary itself
with /bin/sh. Open and resource errors pass through unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, and pending SIGSYS

- try_exec_elf/exec_elf take argv_in_place: the target's argv can keep
  pointing at the launcher's kernel argument strings instead of copies,
  so a program that overwrites argv (a scrubbed password, a process
  title) changes /proc/<pid>/cmdline as it does natively.
- try_exec_elf reports an unusable PT_INTERP image as ELIBBAD or EIO,
  like validate_exec.
- A SIGSYS that arrives while one is virtually pending coalesces into it
  and keeps the first siginfo, as Linux standard signals do.
- A SIGSYS already pending when a masked wait unblocks it runs its
  handler under the wait's mask; rt_sigreturn restores the caller's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
/proc/self/fd stops resolving once the thread-group leader has exited
(main called pthread_exit) while worker threads keep running with the
same descriptors, so their accesses through descriptors went untracked.
/proc/thread-self/fd keeps working for the calling thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
try_exec_elf/exec_elf take envp_in_place, independent of argv_in_place:
the target's envp can keep pointing at the launcher's kernel environment
strings, so a program that overwrites a getenv result (scrubbing a
secret) changes /proc/<pid>/environ as it does natively.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lf/fd

Reverts the /proc/thread-self/fd change and its test: it only matters
after a process's main thread exits while workers keep running, which is
too rare to justify the change and its subprocess-based test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r mapping

Reverts the O_NONBLOCK/regular-file/X_OK checks on PT_INTERP and the
ELIBBAD/EIO mapping for unusable interpreter images. They only change
the outcome when PT_INTERP names something other than a working dynamic
linker, which real binaries don't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…case fixes

Reverts envp_in_place, SA_NODEFER with SIGSYS in sa_mask, and
readlinkat(fd, "") on a /proc/self/exe link descriptor. Each only
changes behavior in contrived cases (scrubbing secrets through environ,
raising SIGSYS inside its own handler with those flags, reading the exe
link through an O_PATH descriptor).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ux/3-elf-loader-sigsys

# Conflicts:
#	crates/fspy_elf_loader/src/lib.rs
#	crates/fspy_elf_loader_tests/tests/exec_elf.rs
…fixes

Both only matter for a program that queues SIGSYS to itself while it has
it blocked (a second sigqueue overwriting the first's siginfo, or the
handler mask seen when ppoll unblocks an already pending SIGSYS).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.

Stale comment

Agentic security review of SIGSYS /proc/*/exe virtualization found one remaining identity leak via empty-path readlinkat / /proc/self/fd after O_PATH|O_NOFOLLOW.

Open in Web View Automation 

Sent by Cursor Security Agent: Security Reviewer

// Fast rejection for 99.99% of paths: any path naming `/proc/.../exe`
// (without a trailing `/`, which is ENOTDIR on a regular-file symlink)
// must end with the component `exe`.
if !ends_with_exe_component(raw) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Agentic Security Review
Severity: MEDIUM

/proc/*/exe virtualization misses documented empty-pathname readlinkat (and readlink of /proc/self/fd/<n>) after an O_PATH|O_NOFOLLOW open of the magic symlink.

rewrite_open leaves O_NOFOLLOW opens unrewritten so the fd is the symlink itself, but is_proc_self_exe only matches paths whose last component is exe. An empty pathname (or /proc/self/fd/<n>) therefore falls through to a kernel replay that returns the real launcher path instead of the virtualized target.

The virtualization fixture opens this fd and checks S_ISLNK, but never readlinkats it.

Impact: A loaded binary can recover the real loader path and then open, exec, or unlink it, breaking the /proc/self/exe identity-hiding contract this change is meant to enforce.

Fix in Cursor Fix in Web

Reviewed by Cursor Security Reviewer for commit 3d0f243. Configure here.

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

Stale comment

Agentic security review found 1 medium finding on /proc/*/exe virtualization (empty-pathname readlinkat after O_PATH|O_NOFOLLOW).

Open in Web View Automation 

Sent by Cursor Security Agent: Security Reviewer

// Fast rejection for 99.99% of paths: any path naming `/proc/.../exe`
// (without a trailing `/`, which is ENOTDIR on a regular-file symlink)
// must end with the component `exe`.
if !ends_with_exe_component(raw) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Agentic Security Review
Severity: MEDIUM

/proc/*/exe virtualization matches only pathnames whose last component is exe. rewrite_open leaves O_NOFOLLOW (including O_PATH|O_NOFOLLOW) unrewritten so the fd is the magic symlink, but empty-pathname readlinkat/openat/execveat on that dirfd and readlink("/proc/self/fd/<n>") never match, so the kernel returns the real launcher path.

The virtualization fixture opens this fd and checks S_ISLNK, but never readlinkats it.

Impact: A loaded binary can recover the materialized launcher path and then open, exec, or unlink it, breaking the /proc/self/exe identity contract this change is meant to enforce.

Fix in Cursor Fix in Web

Reviewed by Cursor Security Reviewer for commit ba828b8. Configure here.

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