Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3a8de45
feat(fspy_elf_loader): add SIGSYS syscall interception, signal virtua…
wan9chi Nov 27, 2026
26dfa29
docs(fspy_elf_loader): fix broken intra-doc link in stack_pool
wan9chi Nov 27, 2026
17f3396
fix(fspy_elf_loader): reject io_uring_setup with ENOSYS in seccomp fi…
wan9chi Nov 27, 2026
dde2d05
fix(fspy_elf_loader): copy trapped syscall arguments directly instead…
wan9chi Nov 27, 2026
ae2a3d4
refactor(fspy_elf_loader): borrow user C strings directly and drop ca…
wan9chi Nov 27, 2026
e07a511
Merge branch 'fspy-linux/2-elf-loader-validation-stack' into fspy-lin…
wan9chi Nov 27, 2026
8871c06
refactor(fspy_elf_loader): drop CET, GCS, and EXTRA_MAGIC handling fr…
wan9chi Nov 27, 2026
798d44c
docs(fspy_elf_loader): spell out SME in the vfork frame comment
wan9chi Nov 27, 2026
6958484
fix(fspy_elf_loader): stop trapping lstat and correct the relative /p…
wan9chi Nov 27, 2026
d21f57b
docs(fspy_elf_loader): fix AT_FDCWD comment in proc_exe::is_proc_self…
wan9chi Nov 27, 2026
6097e51
fix(fspy_elf_loader): harden SIGSYS runtime, expose try_exec_elf, and…
wan9chi Nov 27, 2026
6682071
Merge branch 'fspy-linux/2-elf-loader-validation-stack' into fspy-lin…
wan9chi Nov 27, 2026
fd98cce
Merge branch 'fspy-linux/2-elf-loader-validation-stack' into fspy-lin…
wan9chi Nov 27, 2026
ebf764f
Merge branch 'fspy-linux/2-elf-loader-validation-stack' into fspy-lin…
wan9chi Nov 27, 2026
c60fe12
fix(fspy_elf_loader): prevent null-mmap UB, torn fork seqlock, and de…
wan9chi Nov 27, 2026
75fd976
fix(fspy_elf_loader): expose Syscall::sigsys_ignored and serialize TI…
wan9chi Nov 27, 2026
c2fda38
test(fspy_elf_loader): allow static glibc CRT SIGSYS on standalone /p…
wan9chi Nov 27, 2026
5e6aa3e
Merge branch 'fspy-linux/2-elf-loader-validation-stack' into fspy-lin…
wan9chi Nov 27, 2026
4637846
Merge branch 'fspy-linux/2-elf-loader-validation-stack' into fspy-lin…
wan9chi Nov 27, 2026
da112b1
fix(fspy_elf_loader): bound TID_SLOTS scans, eagerly reap dead TIDs, …
wan9chi Nov 27, 2026
7ecf96f
Merge branch 'fspy-linux/2-elf-loader-validation-stack' into fspy-lin…
wan9chi Nov 27, 2026
ffc0b8a
fix(fspy_elf_loader): reject foreign-arch and x32 syscalls with ENOSYS
wan9chi Nov 27, 2026
e3e7500
Merge branch 'fspy-linux/2-elf-loader-validation-stack' into fspy-lin…
wan9chi Nov 28, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion crates/fspy_elf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ publish.workspace = true
rust-version.workspace = true

[target.'cfg(any(target_os = "linux", target_os = "none"))'.dependencies]
allocator-api2 = { workspace = true }
elf = { workspace = true }
fspy_nostd = { workspace = true }
linux-raw-sys = { workspace = true, features = ["auxvec", "errno", "general", "no_std", "prctl"] }
fspy_nostd_alloc = { workspace = true }
linux-raw-sys = { workspace = true, features = [
"auxvec",
"errno",
"general",
"no_std",
"prctl",
"ptrace",
] }
syscalls = { workspace = true }

[target.'cfg(target_os = "linux")'.dev-dependencies]
Expand Down
41 changes: 20 additions & 21 deletions crates/fspy_elf_loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ reserved ranges.
- Startup stack: contiguous argv/envp strings, rebuilt auxv, copied
`AT_RANDOM` / platform strings, inherited scalars and resident vDSO.
- Naked enter stubs: install SP, clear dirty GPRs, jump (no return address).
- `SIGSYS` signal, `/proc/*/exe`, and custom `SyscallInterceptor` virtualization
with a private pooled `SIGSYS` stack, virtualized `rt_sigaction` /
`rt_sigprocmask` / `rt_sigpending` / masked waits / `fork` / `vfork`, and
direct user memory copies that assume valid pointers.
- Wired into `fspy` via `fspy_preload_linux` as the sole Linux tracking backend.
- [`fspy_elf_loader_tests`](../fspy_elf_loader_tests/README.md): dynamic and
static PIE/`ET_EXEC` runtimes, overlap rejection, malformed images. An
ignored Node test exists (`--include-ignored` on Linux with Node on `PATH`).
static PIE/`ET_EXEC` runtimes, overlap rejection, malformed images, and
`SIGSYS` + `/proc/*/exe` virtualization coverage.

## Not a complete exec

Expand All @@ -61,25 +66,19 @@ Do not try to close these in this crate unless a real target requires it:
- **`brk` / `start_brk`.** The kernel's brk belongs to the high launcher
mapping. It cannot be moved to a Linux-faithful image end. glibc malloc
still works via mmap; leave brk alone.
- **`/proc` identity.** `/proc/self/exe` and parent-visible `/proc/<pid>/exe`
still name the launcher. Parent `/proc/<pid>/exe` is not a JS/Chromium
identity gate. `/proc/self/exe` is, and needs virtualization later.
- GNU properties, executable stacks, ELF32, other arches, growable stack
(fixed 8 MiB + guard), fd/signal/personality cleanup.
- **External `/proc` views.** `/proc/self/exe` and `/proc/<pid>/exe` are
virtualized inside the tracked process, while external `/proc/<pid>/exe`
observers still see the launcher binary.
- **Privileged / unsupported binaries.** `PR_SET_NO_NEW_PRIVS` is enabled for
seccomp (so setuid/setgid and file-capability escalations do not apply), and
ELF32 and `binfmt_misc` binaries are rejected with `ENOEXEC`.
- **Launcher path visibility across mount changes.** Every tracked `execve`
re-enters the materialized freestanding launcher binary on disk. If a tracked
process tree changes its mount namespace or root (`chroot`, `pivot_root`, or
a private `/tmp` mount hiding the materialized launcher path), subsequent
child `execve` calls fail with `ENOENT`.
- GNU properties, other arches, growable stack (fixed 8–64 MiB from
`RLIMIT_STACK` + guard), and personality cleanup.

Load failures panic and terminate. `exec_elf` is for a fresh single-threaded
process with no libc or TLS init. Trusted images only; this is not a sandbox.

## Product work (elsewhere)

fspy still spawns with `LD_PRELOAD` or `seccomp_unotify`. The loader is not
wired in. Remaining work is to make launcher + userland shebang + `exec_elf`
the only Linux spawn:

1. Shebang and PATH above `exec_elf`, then load the resulting ELF. Scripts
never hit kernel `binfmt_script`. Nested `execve` must re-enter this path
instead of kernel-execing the child image.
2. Virtualize `/proc/self/exe` so the target sees its own path (`process.execPath`
/ `fs.realpath`). The ignored Node test does not do this.
3. Drop the Linux preload/seccomp spawn split. One intercept setup in the
launcher covers dynamic glibc, musl, and static images.
37 changes: 16 additions & 21 deletions crates/fspy_elf_loader/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,33 +267,31 @@ impl<'fd> File<'fd> {
Ok(())
}

pub fn map(&self, page: usize, layout: &ValidatedLayout) -> Image {
pub fn try_map(&self, page: usize, layout: &ValidatedLayout) -> Result<Image> {
// Reserve the whole image, including holes, before replacing pages
// with MAP_FIXED. That flag discards existing mappings at its address;
// restricting it to our reservation protects the loader and old stack.
let (mapping, bias) = if self.header.e_type == abi::ET_EXEC {
// Fixed-address executables cannot move. Refuse collisions rather
// than replacing resident loader code or other process state.
self.reject_launcher_region_overlap(layout)
.expect("cannot reserve fixed ELF address range");
(Mapping::reserve_at(layout.low, layout.span), 0)
self.reject_launcher_region_overlap(layout)?;
(Mapping::try_reserve_at(layout.low, layout.span)?, 0)
} else {
// Extra space aligns the load bias even when `p_align` exceeds a
// page or `layout.low > base`. Compute `mapped_low` inside the
// reservation with `mapped_low % alignment == layout.low % alignment`
// so `bias = mapped_low.wrapping_sub(layout.low)` is aligned to
// `layout.alignment` and never underflows.
let mapping = Mapping::reserve(add(layout.span, layout.alignment));
let mapping = Mapping::try_reserve(add(layout.span, layout.alignment))?;
let base = mapping.ptr.as_ptr().expose_provenance();
let desired_mod = layout.low % layout.alignment;
let cur_mod = base % layout.alignment;
let delta = (desired_mod + layout.alignment - cur_mod) % layout.alignment;
let mapped_low = add(base, delta);
let bias = mapped_low.wrapping_sub(layout.low);
assert!(
add(mapped_low, layout.span) <= add(base, mapping.len),
"ELF exceeds reservation"
);
if add(mapped_low, layout.span) > add(base, mapping.len) {
return Err(Error::NOEXEC);
}
(mapping, bias)
};
// Like Linux, AT_PHDR is the table's address in the LOAD whose file
Expand All @@ -317,19 +315,19 @@ impl<'fd> File<'fd> {
}
// SAFETY: validation and reservation cover every page this
// segment replaces. No references to those pages exist yet.
unsafe { self.map_segment(segment, bias, page) };
unsafe { self.map_segment(segment, bias, page) }?;
}
Image {
Ok(Image {
mapping,
bias,
entry: bias.wrapping_add(word(self.header.e_entry)),
phdr,
phnum: usize::from(self.header.e_phnum),
exec_stack: layout.exec_stack,
}
})
}

unsafe fn map_segment(&self, segment: ProgramHeader, bias: usize, page: usize) {
unsafe fn map_segment(&self, segment: ProgramHeader, bias: usize, page: usize) -> Result<()> {
let start = bias.wrapping_add(word(segment.p_vaddr));
let end = add(start, word(segment.p_memsz));
let file_end = add(start, word(segment.p_filesz));
Expand Down Expand Up @@ -365,8 +363,7 @@ impl<'fd> File<'fd> {
self.fd,
segment.p_offset & !u64::try_from(sub(page, 1)).unwrap(),
)
}
.expect("cannot map ELF segment");
}?;
// Zero through the page boundary (Linux padzero) when the segment
// has BSS (memsz > filesz) or is writable (glibc's early allocator
// reads spare bytes after the interpreter's _end even when
Expand Down Expand Up @@ -402,8 +399,7 @@ impl<'fd> File<'fd> {
ptr::with_exposed_provenance_mut(tail_page),
page,
MprotectFlags::READ | MprotectFlags::WRITE,
)
.expect("cannot zero ELF BSS");
)?;
}
ptr::write_bytes(
ptr::with_exposed_provenance_mut::<u8>(file_end),
Expand All @@ -415,8 +411,7 @@ impl<'fd> File<'fd> {
ptr::with_exposed_provenance_mut(tail_page),
page,
MprotectFlags::from_bits_retain(protection.bits()),
)
.expect("cannot restore ELF protection");
)?;
}
}
}
Expand All @@ -435,9 +430,9 @@ impl<'fd> File<'fd> {
protection,
fixed,
)
}
.expect("cannot map ELF BSS");
}?;
}
Ok(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/fspy_elf_loader/src/image/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ fn maps_et_dyn_with_high_nonzero_p_vaddr_without_underflow() {
let file = open_bytes(&bytes);
let parsed = File::open(file.as_fd()).unwrap();
let layout = parsed.validate_segments(page).unwrap();
let image = parsed.map(page, &layout);
let image = parsed.try_map(page, &layout).unwrap();
let mapped_base = image.bias.wrapping_add(layout.low);
assert_eq!(image.entry, mapped_base + 0x200);
assert_eq!(image.phdr, mapped_base + EHDR_SIZE);
Expand Down Expand Up @@ -618,7 +618,7 @@ fn map_bytes(bytes: &[u8]) -> Image {
let fd = open_bytes(bytes);
let file = File::open(fd.as_fd()).unwrap();
let layout = file.validate_segments(page).unwrap();
file.map(page, &layout)
file.try_map(page, &layout).unwrap()
}

fn open_bytes(bytes: &[u8]) -> OwnedFd {
Expand Down
Loading
Loading