Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 crates/fspy_elf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ path = "tests/support/launcher.rs"
test = false
bench = false

[lib]
doctest = false

[lints]
workspace = true
6 changes: 5 additions & 1 deletion crates/fspy_elf_loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ This crate handles loading an ELF program and transferring execution to it. The

The current implementation is a proof of concept for evaluating compatibility. It does not yet provide a complete replacement for Linux exec.

It loads native ELF64 dynamic executables on AArch64 and x86-64, including PIE (`ET_DYN`) and fixed-address (`ET_EXEC`) images. Fixed-address images are rejected if their address range overlaps any existing mapping. Static executables remain unsupported.
It loads native ELF64 executables on AArch64 and x86-64, including PIE
(`ET_DYN`) and fixed-address (`ET_EXEC`) images, with or without `PT_INTERP`.
Fixed-address images are rejected if their address range overlaps any existing
mapping. Images with `PT_INTERP` enter the dynamic linker; images without it
enter the executable and receive `AT_BASE` 0.

The freestanding launcher is a static fixed-address executable with no startup relocation:

Expand Down
101 changes: 62 additions & 39 deletions crates/fspy_elf_loader/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,49 +111,69 @@ impl File {
// SAFETY: the file remains unchanged and mapped read-only.
let bytes =
unsafe { self.view.bytes() }.get(start..end).expect("truncated PT_INTERP");
let path = CStr::from_units_with_nul(bytes).expect("invalid PT_INTERP path");
// Linux requires only that the last byte is NUL, then treats
// the slice as a C string. Linkers may pad with extra NULs.
assert!(bytes.last() == Some(&0), "unterminated PT_INTERP");
let len = bytes.iter().position(|byte| *byte == 0).unwrap() + 1;
let path =
CStr::from_units_with_nul(&bytes[..len]).expect("invalid PT_INTERP path");
assert!(!path.as_units().is_empty(), "empty PT_INTERP path");
result = Some(path);
}
}
result
}

fn assert_disjoint_load(&self, index: usize, start: usize, end: usize) {
for (other_index, other) in self.segments().iter().enumerate() {
if other_index > index && other.p_type == abi::PT_LOAD && other.p_memsz != 0 {
let other_start = word(other.p_vaddr);
let other_end = add(other_start, word(other.p_memsz));
assert!(end <= other_start || other_end <= start, "overlapping ELF segments");
}
}
}

pub fn map(&self, page: usize) -> Image {
// First validate the segments and find their page-rounded span.
// mmap operates on whole pages, even when ELF segments do not.
let mut low = usize::MAX;
let mut high = 0;
let mut alignment = page;
let mut previous_end = 0;
for segment in self.segments().iter() {
for (index, segment) in self.segments().iter().enumerate() {
if segment.p_type == abi::PT_GNU_STACK {
assert!(segment.p_flags & abi::PF_X == 0, "executable stacks are unsupported");
}
if segment.p_type != abi::PT_LOAD {
continue;
}
let start = word(segment.p_vaddr);
let end = add(start, word(segment.p_memsz));
let file_end = add(word(segment.p_offset), word(segment.p_filesz));
assert!(segment.p_filesz <= segment.p_memsz, "ELF filesz exceeds memsz");
assert!(file_end <= self.view.len, "truncated ELF segment");
// Our MVP rejects overlapping byte ranges. Page-rounded ranges
// can still overlap, so mapping below must preserve header order.
assert!(start >= previous_end, "overlapping or unordered ELF segments");
previous_end = end;
// ELF treats alignment 0 and 1 as no extra constraint. File and
// memory offsets must agree within a page for file-backed mmap.
let align = word(segment.p_align).max(1);
assert!(align.is_power_of_two(), "invalid segment alignment");
assert_eq!(start % align, word(segment.p_offset) % align, "incongruent ELF alignment");
assert_eq!(start % page, word(segment.p_offset) % page, "incongruent ELF page offsets");
alignment = alignment.max(align);
if segment.p_memsz == 0 {
continue;
if segment.p_type == abi::PT_LOAD {
let start = word(segment.p_vaddr);
let end = add(start, word(segment.p_memsz));
let file_end = add(word(segment.p_offset), word(segment.p_filesz));
assert!(segment.p_filesz <= segment.p_memsz, "ELF filesz exceeds memsz");
assert!(file_end <= self.view.len, "truncated ELF segment");
// Byte ranges must not overlap. Header order need not match
// vaddr order; mapping below still walks headers so a later
// MAP_FIXED can share a page with an earlier load.
if segment.p_memsz != 0 {
self.assert_disjoint_load(index, start, end);
low = low.min(start & !sub(page, 1));
high = high.max(align_up(end, page));
}
// ELF treats alignment 0 and 1 as no extra constraint. File
// and memory offsets must agree within a page for mmap.
let align = word(segment.p_align).max(1);
assert!(align.is_power_of_two(), "invalid segment alignment");
assert_eq!(
start % align,
word(segment.p_offset) % align,
"incongruent ELF alignment"
);
assert_eq!(
start % page,
word(segment.p_offset) % page,
"incongruent ELF page offsets"
);
alignment = alignment.max(align);
}
low = low.min(start & !sub(page, 1));
high = high.max(align_up(end, page));
}
let span = high.checked_sub(low).expect("ELF has no loadable memory");
// Reserve the whole image, including holes, before replacing pages
Expand Down Expand Up @@ -228,36 +248,40 @@ impl File {
protection |= ProtFlags::EXEC;
}
let fixed = MapFlags::PRIVATE | MapFlags::FIXED;
if segment.p_filesz != 0 {
// Linux elf_map skips a zero-length mapping, which happens only when
// filesz is 0 and vaddr is page-aligned. A zero-filesz load that
// starts mid-page still maps that shared page from the file so a
// later MAP_FIXED does not anonymous-wipe the previous load's bytes.
let file_len = sub(align_up(file_end, page), page_start);
if file_len != 0 {
// Round both the file offset and address down by the same amount.
// For example, an offset ending in 0x340 needs those first 0x340
// bytes of its page mapped too. Round the end up to cover the tail.
// PRIVATE keeps BSS writes and later relocations out of the file.
let len = sub(align_up(file_end, page), page_start);
// SAFETY: caller owns the entire reservation being replaced.
unsafe {
mm::mmap(
ptr::with_exposed_provenance_mut(page_start),
len,
file_len,
protection,
fixed,
self.fd.as_fd(),
segment.p_offset & !u64::try_from(sub(page, 1)).unwrap(),
)
}
.expect("cannot map ELF segment");
if file_end < end && !file_end.is_multiple_of(page) {
// Zero through the page boundary, not just p_memsz: leftover file
// bytes after filesz==memsz, and the BSS tail of filesz<memsz,
// must be zero. glibc's early allocator reads spare bytes after
// the interpreter's _end (Linux padzero). Stopping at p_memsz
// crashed /bin/echo's interpreter in the ARM64 smoke test.
if !file_end.is_multiple_of(page) {
let tail_page = file_end & !sub(page, 1);
// Writable, non-executable pages can be zeroed directly.
// Otherwise temporarily make the last file page writable,
// without X, and restore its original protection afterwards.
let change_protection =
!protection.contains(ProtFlags::WRITE) || protection.contains(ProtFlags::EXEC);
// Clear through the page boundary, not just p_memsz: glibc's
// early allocator uses the spare bytes after the interpreter's
// _end and expects them to be zero (Linux padzero behavior).
// Stopping at p_memsz crashed /bin/echo's interpreter in the
// ARM64 smoke test. image/tests.rs checks the entire page tail.
// SAFETY: this private page was mapped above; it has no live references.
unsafe {
if change_protection {
Expand All @@ -284,12 +308,11 @@ impl File {
}
}
}
let anonymous_start =
if segment.p_filesz == 0 { page_start } else { align_up(file_end, page) };
let anonymous_start = align_up(file_end, page);
let anonymous_end = align_up(end, page);
if anonymous_start < anonymous_end {
// The remaining BSS has no file bytes. Anonymous pages start at
// zero; mapping the file past its end could instead cause SIGBUS.
// Remaining BSS pages have no file bytes. Anonymous pages start
// at zero; mapping the file past its end could instead SIGBUS.
// SAFETY: these pages lie inside the reservation and have no live references.
unsafe {
mm::mmap_anonymous(
Expand Down
164 changes: 146 additions & 18 deletions crates/fspy_elf_loader/src/image/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ fn zeroes_the_entire_last_file_page_and_anonymous_bss() {
fn check_bss(page: usize, flags: u32, expected_protection: &str) {
for memory_size in [0x180, page + 0x180] {
let bytes = fixture(page, memory_size, flags);
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(file.path(), bytes).unwrap();
let path = std::ffi::CString::new(file.path().as_os_str().as_encoded_bytes()).unwrap();
let path = CStr::from_units_with_nul(path.as_bytes_with_nul()).unwrap();
let image = File::new(
fs::openat(fspy_nostd::CWD, path, fs::OFlags::RDONLY, fs::Mode::empty()).unwrap(),
);
let image = image.map(page);
let image = map_bytes(&bytes);
assert_eq!(image.bias % (2 * page), 0);
assert_eq!(image.phdr, Some(image.bias + 64));
let maps = std::fs::read_to_string("/proc/self/maps").unwrap();
Expand Down Expand Up @@ -61,24 +54,148 @@ fn check_bss(page: usize, flags: u32, expected_protection: &str) {
}
}

#[test]
fn zeroes_spare_file_bytes_when_filesz_equals_memsz() {
let page = fspy_nostd::param::page_size();
for flags in [abi::PF_R | abi::PF_W, abi::PF_R, abi::PF_R | abi::PF_X] {
let mut bytes = std::vec![0xa5; 2 * page];
write_header(&mut bytes, 2, 0x200);
load(&mut bytes, 0, 0, page, page, abi::PF_R | abi::PF_X, page);
load(&mut bytes, 1, page + 0x100, 0x200, 0x200, flags, page);
let image = map_bytes(&bytes);
// SAFETY: the second LOAD and its padzero tail are mapped readable.
unsafe {
assert_eq!(ptr::with_exposed_provenance::<u8>(image.bias + page + 0x100).read(), 0xa5);
assert_eq!(ptr::with_exposed_provenance::<u8>(image.bias + page + 0x2ff).read(), 0xa5);
let tail = core::slice::from_raw_parts(
ptr::with_exposed_provenance::<u8>(image.bias + page + 0x300),
page - 0x300,
);
assert!(tail.iter().all(|byte| *byte == 0), "filesz==memsz must still padzero");
}
}
}

#[test]
fn preserves_previous_load_when_bss_only_starts_mid_page() {
let page = fspy_nostd::param::page_size();
for memory_size in [0x180, page + 0x180] {
let mut bytes = std::vec![0xa5; 3 * page];
write_header(&mut bytes, 2, 0x200);
load(&mut bytes, 0, 0, page + 0x200, page + 0x200, abi::PF_R | abi::PF_X, page);
load(&mut bytes, 1, page + 0x200, 0, memory_size, abi::PF_R | abi::PF_W, page);
let image = map_bytes(&bytes);
// SAFETY: both LOADs are mapped readable; the first occupies the
// shared page below the BSS-only vaddr.
unsafe {
assert_eq!(
ptr::with_exposed_provenance::<u8>(image.bias + page + 0x80).read(),
0xa5,
"anonymous MAP_FIXED of a filesz==0 LOAD must not wipe the previous segment"
);
assert_eq!(ptr::with_exposed_provenance::<u8>(image.bias + page + 0x1ff).read(), 0xa5);
let zero_start = image.bias + page + 0x200;
let zero_end = align_up(zero_start + memory_size, page);
let zeroed = core::slice::from_raw_parts(
ptr::with_exposed_provenance::<u8>(zero_start),
zero_end - zero_start,
);
assert!(zeroed.iter().all(|byte| *byte == 0));
}
}
}

#[test]
fn maps_unordered_non_overlapping_loads() {
let page = fspy_nostd::param::page_size();
let mut bytes = std::vec![0; 3 * page];
write_header(&mut bytes, 2, 0x200);
// Higher vaddr first: Linux still loads in header order.
load(&mut bytes, 0, 2 * page, 0x40, 0x40, abi::PF_R | abi::PF_W, page);
load(&mut bytes, 1, 0, page, page, abi::PF_R | abi::PF_X, page);
bytes[2 * page] = 0x11;
bytes[0x200] = 0x22;
let image = map_bytes(&bytes);
assert_eq!(image.entry, image.bias + 0x200);
// SAFETY: both disjoint LOADs are mapped readable.
unsafe {
assert_eq!(ptr::with_exposed_provenance::<u8>(image.bias + 2 * page).read(), 0x11);
assert_eq!(ptr::with_exposed_provenance::<u8>(image.entry).read(), 0x22);
}
}

#[test]
#[should_panic(expected = "overlapping ELF segments")]
fn rejects_overlapping_load_segments() {
let page = fspy_nostd::param::page_size();
let mut bytes = std::vec![0xa5; 2 * page];
write_header(&mut bytes, 2, 0x80);
load(&mut bytes, 0, 0, 0x200, 0x200, abi::PF_R | abi::PF_X, page);
load(&mut bytes, 1, 0x100, 0x200, 0x200, abi::PF_R | abi::PF_W, page);
map_bytes(&bytes);
}

#[test]
fn reports_no_interpreter_when_pt_interp_is_absent() {
let page = fspy_nostd::param::page_size();
let file = File::new(open_bytes(&fixture(page, 0x180, abi::PF_R | abi::PF_W)));
assert!(file.interpreter().is_none());
}

#[test]
fn accepts_pt_interp_padded_with_extra_nuls() {
let file = File::new(open_bytes(&interp_image(b"ld.so\0\0\0")));
let path = file.interpreter().unwrap();
assert_eq!(path.as_units(), b"ld.so");
}

#[test]
#[should_panic(expected = "unterminated PT_INTERP")]
fn rejects_unterminated_pt_interp() {
let _ = File::new(open_bytes(&interp_image(b"ld.so"))).interpreter();
}

#[test]
#[should_panic(expected = "empty PT_INTERP path")]
fn rejects_empty_pt_interp() {
let _ = File::new(open_bytes(&interp_image(b"\0\0\0"))).interpreter();
}

fn fixture(page: usize, memory_size: usize, flags: u32) -> std::vec::Vec<u8> {
let mut bytes = std::vec![0xa5; 3 * page];
write_header(&mut bytes, 2, 0x200);
load(&mut bytes, 0, 0, page + 0x100, page + 0x100, abi::PF_R | abi::PF_X, page);
load(&mut bytes, 1, page + 0x200, 0x100, memory_size, flags, page);
bytes
}

fn interp_image(path: &[u8]) -> std::vec::Vec<u8> {
let start = 64 + 56;
let mut bytes = std::vec![0; start + path.len()];
write_header(&mut bytes, 1, 0);
let header = &mut bytes[64..120];
header[..4].copy_from_slice(&abi::PT_INTERP.to_le_bytes());
for (field, value) in [(8, start), (32, path.len()), (40, path.len()), (48, 1)] {
header[field..field + 8].copy_from_slice(&u64::try_from(value).unwrap().to_le_bytes());
}
bytes[start..start + path.len()].copy_from_slice(path);
bytes
}

fn write_header(bytes: &mut [u8], phnum: u16, entry: u64) {
bytes[..64].fill(0);
bytes[..7].copy_from_slice(b"\x7fELF\x02\x01\x01");
bytes[16..18].copy_from_slice(&abi::ET_DYN.to_le_bytes());
bytes[18..20].copy_from_slice(&MACHINE.to_le_bytes());
bytes[20..24].copy_from_slice(&1_u32.to_le_bytes());
bytes[24..32].copy_from_slice(&0x200_u64.to_le_bytes());
bytes[24..32].copy_from_slice(&entry.to_le_bytes());
bytes[32..40].copy_from_slice(&64_u64.to_le_bytes());
bytes[52..54].copy_from_slice(&64_u16.to_le_bytes());
bytes[54..56].copy_from_slice(&56_u16.to_le_bytes());
bytes[56..58].copy_from_slice(&2_u16.to_le_bytes());
segment(&mut bytes, 0, 0, page + 0x100, page + 0x100, abi::PF_R | abi::PF_X, page);
segment(&mut bytes, 1, page + 0x200, 0x100, memory_size, flags, page);
bytes
bytes[56..58].copy_from_slice(&phnum.to_le_bytes());
}

fn segment(
fn load(
bytes: &mut [u8],
index: usize,
start: usize,
Expand All @@ -87,14 +204,25 @@ fn segment(
flags: u32,
page: usize,
) {
let offset = 64 + index * 56;
let header = &mut bytes[offset..offset + 56];
let header = &mut bytes[64 + index * 56..64 + index * 56 + 56];
header.fill(0);
header[..4].copy_from_slice(&abi::PT_LOAD.to_le_bytes());
header[4..8].copy_from_slice(&flags.to_le_bytes());
for (offset, value) in
for (field, value) in
[(8, start), (16, start), (32, file_size), (40, memory_size), (48, 2 * page)]
{
header[offset..offset + 8].copy_from_slice(&u64::try_from(value).unwrap().to_le_bytes());
header[field..field + 8].copy_from_slice(&u64::try_from(value).unwrap().to_le_bytes());
}
}

fn map_bytes(bytes: &[u8]) -> Image {
File::new(open_bytes(bytes)).map(fspy_nostd::param::page_size())
}

fn open_bytes(bytes: &[u8]) -> OwnedFd {
let file = tempfile::NamedTempFile::new().unwrap();
std::fs::write(file.path(), bytes).unwrap();
let path = std::ffi::CString::new(file.path().as_os_str().as_encoded_bytes()).unwrap();
let path = CStr::from_units_with_nul(path.as_bytes_with_nul()).unwrap();
fs::openat(fspy_nostd::CWD, path, fs::OFlags::RDONLY, fs::Mode::empty()).unwrap()
}
Loading
Loading