| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! Post-link mixed-ELF64 (`EM_X86_64` and `EM_AARCH64`) auditor. | |
| //! | |
| //! Compares verified pre-link `.o` symbol `.text` and referenced `.rodata` | |
| //! bytes against a final linked `ET_EXEC`/`ET_DYN` binary, permitting only | |
| //! exact architecture-allowlisted relocation patches while enforcing image | |
| //! hardening invariants (`W^X`, non-overlapping `PT_LOAD`, non-executable | |
| //! `PT_GNU_STACK`, page-rounded `PT_GNU_RELRO`, dynamic-relocation isolation, | |
| //! optional `PT_GNU_PROPERTY` CET/BTI/PAC, `.stack_sizes`, and forbidden | |
| //! syscall-instruction absence). | |
| #![forbid(unsafe_code)] | |
| pub mod audit; | |
| pub mod elf; | |
| pub mod x86_len; | |
| use std::{fmt::Write as _, path::Path}; | |
| pub use crate::{ | |
| audit::{ | |
| audit_elf, AuditConfig, AuditReport, Finding, Manifest, ManifestSymbol, ObjectInput, | |
| RelroReport, RodataCheck, StackSizesReport, SymbolAudit, SCHEMA_VERSION, | |
| }, | |
| elf::{Arch, Elf64, ElfType, GnuProperties, StackSizeEntry}, | |
| }; | |
| /// Convenience entry point reading the linked ELF and pre-link `.o` files from | |
| /// disk and running [`audit_elf`]. | |
| pub fn audit_files( | |
| linked_path: &Path, | |
| object_paths: &[&Path], | |
| manifest: &Manifest, | |
| config: &AuditConfig, | |
| ) -> Result<AuditReport, String> { | |
| let linked_bytes = std::fs::read(linked_path) | |
| .map_err(|e| format!("cannot read linked ELF `{}`: {e}", linked_path.display()))?; | |
| let mut loaded_objects = Vec::with_capacity(object_paths.len()); | |
| for path in object_paths { | |
| let bytes = std::fs::read(path) | |
| .map_err(|e| format!("cannot read object `{}`: {e}", path.display()))?; | |
| loaded_objects.push((path.display().to_string(), bytes)); | |
| } | |
| let inputs: Vec<ObjectInput<'_>> = loaded_objects | |
| .iter() | |
| .map(|(label, bytes)| ObjectInput { label: label.clone(), bytes: bytes.as_slice() }) | |
| .collect(); | |
| audit_elf(&linked_bytes, &inputs, manifest, config) | |
| } | |
| /// Format a concise human-readable summary of an [`AuditReport`]. | |
| pub fn format_human_report(report: &AuditReport) -> String { | |
| let mut out = String::new(); | |
| let _ = writeln!( | |
| out, | |
| "anneal-postlink: {} ({}, {:?})", | |
| report.verdict, report.arch, report.elf_type | |
| ); | |
| let _ = writeln!( | |
| out, | |
| " hardening: W^X={} PT_GNU_STACK={} PT_GNU_RELRO={}", | |
| yes_no(report.wx_ok), | |
| yes_no(report.gnu_stack_ok), | |
| if report.relro.present { "present" } else { "absent" } | |
| ); | |
| if report.gnu_properties.present { | |
| let _ = writeln!( | |
| out, | |
| " gnu.property (phdr={}): x86_ibt={} x86_shstk={} aarch64_bti={} aarch64_pac={}", | |
| report.gnu_properties.pt_gnu_property_present, | |
| report.gnu_properties.x86_ibt, | |
| report.gnu_properties.x86_shstk, | |
| report.gnu_properties.aarch64_bti, | |
| report.gnu_properties.aarch64_pac | |
| ); | |
| } | |
| if report.stack_sizes.present { | |
| let _ = writeln!( | |
| out, | |
| " stack_sizes: entries={} max_verified={:?} max_image={:?}", | |
| report.stack_sizes.entry_count, | |
| report.stack_sizes.max_verified_stack_bytes, | |
| report.stack_sizes.max_image_stack_bytes | |
| ); | |
| } | |
| for sym in &report.symbols { | |
| let va = sym.linked_va.map_or_else(|| "none".to_owned(), |v| format!("0x{v:x}")); | |
| let _ = writeln!( | |
| out, | |
| " symbol `{}` [{}]: va={va} size={} relocs={} status={}", | |
| sym.name, | |
| sym.object, | |
| sym.size, | |
| sym.relocations_checked, | |
| if sym.ok { "OK" } else { "FAIL" } | |
| ); | |
| } | |
| for ro in &report.rodata_checks { | |
| let _ = writeln!( | |
| out, | |
| " rodata `{}` -> `{}`: va=0x{:x} len={} sha256={} status={}", | |
| ro.symbol, | |
| ro.target, | |
| ro.linked_va, | |
| ro.len, | |
| &ro.sha256[..16], | |
| if ro.ok { "OK" } else { "FAIL" } | |
| ); | |
| } | |
| for finding in &report.findings { | |
| if let Some(sym) = &finding.symbol { | |
| let _ = writeln!(out, " FINDING [{}] ({sym}): {}", finding.code, finding.message); | |
| } else { | |
| let _ = writeln!(out, " FINDING [{}]: {}", finding.code, finding.message); | |
| } | |
| } | |
| out | |
| } | |
| fn yes_no(b: bool) -> &'static str { | |
| if b { | |
| "OK" | |
| } else { | |
| "FAIL" | |
| } | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| use crate::elf::{ | |
| Elf64, EM_AARCH64, EM_X86_64, ET_DYN, ET_EXEC, ET_REL, GNU_PROPERTY_AARCH64_FEATURE_1_AND, | |
| GNU_PROPERTY_AARCH64_FEATURE_1_BTI, GNU_PROPERTY_AARCH64_FEATURE_1_PAC, | |
| GNU_PROPERTY_X86_FEATURE_1_AND, GNU_PROPERTY_X86_FEATURE_1_IBT, NT_GNU_PROPERTY_TYPE_0, | |
| PF_R, PF_W, PF_X, PT_DYNAMIC, PT_GNU_PROPERTY, PT_GNU_RELRO, PT_GNU_STACK, PT_LOAD, | |
| R_AARCH64_ADD_ABS_LO12_NC, R_AARCH64_ADR_PREL_PG_HI21, R_AARCH64_CALL26, R_AARCH64_JUMP26, | |
| R_X86_64_COPY, R_X86_64_GLOB_DAT, R_X86_64_GOTTPOFF, R_X86_64_JUMP_SLOT, R_X86_64_PC32, | |
| R_X86_64_PLT32, R_X86_64_RELATIVE, R_X86_64_REX_GOTPCRELX, R_X86_64_TLSDESC, SHF_ALLOC, | |
| SHF_COMPRESSED, SHF_EXECINSTR, SHF_MERGE, SHF_STRINGS, SHF_WRITE, SHN_XINDEX, SHT_CREL, | |
| SHT_DYNSYM, SHT_LLVM_LTO, SHT_NOTE, SHT_PROGBITS, SHT_REL, SHT_RELA, SHT_RELR, SHT_STRTAB, | |
| SHT_SYMTAB, SHT_SYMTAB_SHNDX, STB_LOCAL, STT_FUNC, STT_GNU_IFUNC, STT_NOTYPE, STT_SECTION, | |
| STV_HIDDEN, STV_INTERNAL, | |
| }; | |
| const STB_GLOBAL: u8 = 1; | |
| const STT_OBJECT: u8 = 1; | |
| struct StrTab { | |
| buf: Vec<u8>, | |
| } | |
| impl StrTab { | |
| fn new() -> Self { | |
| Self { buf: vec![0] } | |
| } | |
| fn add(&mut self, s: &str) -> u32 { | |
| let off = self.buf.len() as u32; | |
| self.buf.extend_from_slice(s.as_bytes()); | |
| self.buf.push(0); | |
| off | |
| } | |
| } | |
| struct SymSpec { | |
| name: String, | |
| bind: u8, | |
| sym_type: u8, | |
| shndx: u16, | |
| value: u64, | |
| size: u64, | |
| } | |
| struct SecSpec { | |
| name: String, | |
| sh_type: u32, | |
| sh_flags: u64, | |
| sh_addr: u64, | |
| sh_link: u32, | |
| sh_info: u32, | |
| sh_entsize: u64, | |
| data: Vec<u8>, | |
| } | |
| struct PhdrSpec { | |
| p_type: u32, | |
| p_flags: u32, | |
| p_offset: u64, | |
| p_vaddr: u64, | |
| p_filesz: u64, | |
| p_memsz: u64, | |
| p_align: u64, | |
| } | |
| fn encode_symtab(syms: &[SymSpec]) -> (Vec<u8>, Vec<u8>) { | |
| let mut strtab = StrTab::new(); | |
| let mut sym_bytes = vec![0u8; 24]; // STN_UNDEF | |
| for s in syms { | |
| let name_off = if s.name.is_empty() { 0 } else { strtab.add(&s.name) }; | |
| sym_bytes.extend_from_slice(&name_off.to_le_bytes()); | |
| sym_bytes.push((s.bind << 4) | (s.sym_type & 0x0f)); | |
| sym_bytes.push(0); | |
| sym_bytes.extend_from_slice(&s.shndx.to_le_bytes()); | |
| sym_bytes.extend_from_slice(&s.value.to_le_bytes()); | |
| sym_bytes.extend_from_slice(&s.size.to_le_bytes()); | |
| } | |
| (sym_bytes, strtab.buf) | |
| } | |
| fn encode_rela(entries: &[(u64, u32, u32, i64)]) -> Vec<u8> { | |
| let mut out = Vec::with_capacity(entries.len() * 24); | |
| for &(r_offset, r_sym, r_type, r_addend) in entries { | |
| let r_info = (u64::from(r_sym) << 32) | u64::from(r_type); | |
| out.extend_from_slice(&r_offset.to_le_bytes()); | |
| out.extend_from_slice(&r_info.to_le_bytes()); | |
| out.extend_from_slice(&(r_addend as u64).to_le_bytes()); | |
| } | |
| out | |
| } | |
| fn encode_dyn(entries: &[(i64, u64)]) -> Vec<u8> { | |
| let mut out = Vec::with_capacity((entries.len() + 1) * 16); | |
| for &(tag, val) in entries { | |
| out.extend_from_slice(&tag.to_le_bytes()); | |
| out.extend_from_slice(&val.to_le_bytes()); | |
| } | |
| out.extend_from_slice(&0i64.to_le_bytes()); | |
| out.extend_from_slice(&0u64.to_le_bytes()); | |
| out | |
| } | |
| fn encode_gnu_property_note(pr_type: u32, bits: u32) -> Vec<u8> { | |
| let mut out = Vec::new(); | |
| out.extend_from_slice(&4u32.to_le_bytes()); | |
| out.extend_from_slice(&16u32.to_le_bytes()); | |
| out.extend_from_slice(&NT_GNU_PROPERTY_TYPE_0.to_le_bytes()); | |
| out.extend_from_slice(b"GNU\0"); | |
| out.extend_from_slice(&pr_type.to_le_bytes()); | |
| out.extend_from_slice(&4u32.to_le_bytes()); | |
| out.extend_from_slice(&bits.to_le_bytes()); | |
| out.extend_from_slice(&0u32.to_le_bytes()); | |
| out | |
| } | |
| fn encode_stack_sizes(entries: &[(u64, u64)]) -> Vec<u8> { | |
| let mut out = Vec::new(); | |
| for &(addr, mut sz) in entries { | |
| out.extend_from_slice(&addr.to_le_bytes()); | |
| loop { | |
| let mut byte = (sz & 0x7f) as u8; | |
| sz >>= 7; | |
| if sz != 0 { | |
| byte |= 0x80; | |
| } | |
| out.push(byte); | |
| if sz == 0 { | |
| break; | |
| } | |
| } | |
| } | |
| out | |
| } | |
| fn build_elf( | |
| e_type: u16, | |
| e_machine: u16, | |
| phdrs: &[PhdrSpec], | |
| mut sections: Vec<SecSpec>, | |
| fixed_offsets: &[(usize, usize)], | |
| ) -> Vec<u8> { | |
| let mut shstrtab = StrTab::new(); | |
| let mut name_offs = vec![0u32]; | |
| for s in §ions { | |
| name_offs.push(shstrtab.add(&s.name)); | |
| } | |
| let shstrtab_name_off = shstrtab.add(".shstrtab"); | |
| sections.push(SecSpec { | |
| name: ".shstrtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: shstrtab.buf, | |
| }); | |
| name_offs.push(shstrtab_name_off); | |
| let phoff = if phdrs.is_empty() { 0u64 } else { 64u64 }; | |
| let after_phdrs = 64 + phdrs.len() * 56; | |
| let mut buf = vec![0u8; after_phdrs]; | |
| let mut sec_offsets = vec![0u64; sections.len()]; | |
| for &(sec_idx_1based, off) in fixed_offsets { | |
| let s = §ions[sec_idx_1based - 1]; | |
| let end = off + s.data.len(); | |
| if buf.len() < end { | |
| buf.resize(end, 0); | |
| } | |
| buf[off..end].copy_from_slice(&s.data); | |
| sec_offsets[sec_idx_1based - 1] = off as u64; | |
| } | |
| for (i, s) in sections.iter().enumerate() { | |
| if fixed_offsets.iter().any(|&(idx, _)| idx == i + 1) { | |
| continue; | |
| } | |
| while !buf.len().is_multiple_of(8) { | |
| buf.push(0); | |
| } | |
| sec_offsets[i] = buf.len() as u64; | |
| buf.extend_from_slice(&s.data); | |
| } | |
| while !buf.len().is_multiple_of(8) { | |
| buf.push(0); | |
| } | |
| let shoff = buf.len() as u64; | |
| let shnum = (sections.len() + 1) as u16; | |
| let shstrndx = sections.len() as u16; | |
| buf.extend_from_slice(&[0u8; 64]); | |
| for (i, s) in sections.iter().enumerate() { | |
| buf.extend_from_slice(&name_offs[i + 1].to_le_bytes()); | |
| buf.extend_from_slice(&s.sh_type.to_le_bytes()); | |
| buf.extend_from_slice(&s.sh_flags.to_le_bytes()); | |
| buf.extend_from_slice(&s.sh_addr.to_le_bytes()); | |
| buf.extend_from_slice(&sec_offsets[i].to_le_bytes()); | |
| buf.extend_from_slice(&(s.data.len() as u64).to_le_bytes()); | |
| buf.extend_from_slice(&s.sh_link.to_le_bytes()); | |
| buf.extend_from_slice(&s.sh_info.to_le_bytes()); | |
| buf.extend_from_slice(&8u64.to_le_bytes()); | |
| buf.extend_from_slice(&s.sh_entsize.to_le_bytes()); | |
| } | |
| for (i, p) in phdrs.iter().enumerate() { | |
| let off = 64 + i * 56; | |
| buf[off..off + 4].copy_from_slice(&p.p_type.to_le_bytes()); | |
| buf[off + 4..off + 8].copy_from_slice(&p.p_flags.to_le_bytes()); | |
| buf[off + 8..off + 16].copy_from_slice(&p.p_offset.to_le_bytes()); | |
| buf[off + 16..off + 24].copy_from_slice(&p.p_vaddr.to_le_bytes()); | |
| buf[off + 24..off + 32].copy_from_slice(&p.p_vaddr.to_le_bytes()); | |
| buf[off + 32..off + 40].copy_from_slice(&p.p_filesz.to_le_bytes()); | |
| buf[off + 40..off + 48].copy_from_slice(&p.p_memsz.to_le_bytes()); | |
| buf[off + 48..off + 56].copy_from_slice(&p.p_align.to_le_bytes()); | |
| } | |
| buf[0..4].copy_from_slice(&[0x7f, b'E', b'L', b'F']); | |
| buf[4] = 2; | |
| buf[5] = 1; | |
| buf[6] = 1; | |
| buf[16..18].copy_from_slice(&e_type.to_le_bytes()); | |
| buf[18..20].copy_from_slice(&e_machine.to_le_bytes()); | |
| buf[20..24].copy_from_slice(&1u32.to_le_bytes()); | |
| buf[24..32].copy_from_slice(&0x1000u64.to_le_bytes()); | |
| buf[32..40].copy_from_slice(&phoff.to_le_bytes()); | |
| buf[40..48].copy_from_slice(&shoff.to_le_bytes()); | |
| buf[52..54].copy_from_slice(&64u16.to_le_bytes()); | |
| buf[54..56].copy_from_slice(&(if phdrs.is_empty() { 0u16 } else { 56u16 }).to_le_bytes()); | |
| buf[56..58].copy_from_slice(&(phdrs.len() as u16).to_le_bytes()); | |
| buf[58..60].copy_from_slice(&64u16.to_le_bytes()); | |
| buf[60..62].copy_from_slice(&shnum.to_le_bytes()); | |
| buf[62..64].copy_from_slice(&shstrndx.to_le_bytes()); | |
| buf | |
| } | |
| struct X86FixtureOpts { | |
| wx_violation: bool, | |
| gnu_stack_flags: Option<u32>, | |
| relro_memsz: u64, | |
| relro_vaddr: u64, | |
| duplicate_relro: bool, | |
| include_cet_ibt: bool, | |
| emit_pt_gnu_property: bool, | |
| conflict_pt_gnu_property: bool, | |
| stack_size_foo: Option<u64>, | |
| second_stack_sizes_bar: Option<u64>, | |
| tamper_rodata: bool, | |
| tamper_opcode: bool, | |
| use_gottpoff: bool, | |
| icf_alias_bar: bool, | |
| tail_merge_unverified: bool, | |
| linked_ifunc_foo: bool, | |
| inject_syscall: Option<[u8; 2]>, | |
| fat_lto_kind: Option<&'static str>, | |
| got_glob_dat_name: Option<&'static str>, | |
| got_target_va: u64, | |
| add_textrel: bool, | |
| dyn_reloc_va: Option<u64>, | |
| add_relr_va: Option<u64>, | |
| add_second_shinfo0_rela: bool, | |
| overlap_pt_load: bool, | |
| } | |
| impl Default for X86FixtureOpts { | |
| fn default() -> Self { | |
| Self { | |
| wx_violation: false, | |
| gnu_stack_flags: Some(PF_R | PF_W), | |
| relro_memsz: 0x1000, | |
| relro_vaddr: 0x3000, | |
| duplicate_relro: false, | |
| include_cet_ibt: true, | |
| emit_pt_gnu_property: true, | |
| conflict_pt_gnu_property: false, | |
| stack_size_foo: Some(32), | |
| second_stack_sizes_bar: None, | |
| tamper_rodata: false, | |
| tamper_opcode: false, | |
| use_gottpoff: false, | |
| icf_alias_bar: false, | |
| tail_merge_unverified: false, | |
| linked_ifunc_foo: false, | |
| inject_syscall: None, | |
| fat_lto_kind: None, | |
| got_glob_dat_name: Some("g_sym"), | |
| got_target_va: 0x3000, | |
| add_textrel: false, | |
| dyn_reloc_va: None, | |
| add_relr_va: None, | |
| add_second_shinfo0_rela: false, | |
| overlap_pt_load: false, | |
| } | |
| } | |
| } | |
| fn build_x86_fixture(opts: &X86FixtureOpts) -> (Vec<u8>, Vec<u8>, Manifest) { | |
| // Layout in linked ELF (strictly 4 KiB page-disjoint PT_LOADs): | |
| // .text @ 0x1000 (RX) | |
| // .rodata @ 0x2000 (R) | |
| // .note.gnu.property @ 0x2100 (R, same PT_LOAD + PT_GNU_PROPERTY) | |
| // .got @ 0x3000 (RW, in PT_GNU_RELRO 0x3000..0x4000) | |
| // .dynamic @ 0x3100 (RW, in PT_GNU_RELRO 0x3000..0x4000) | |
| let mut obj_text = vec![ | |
| 0x48, 0x8d, 0x05, 0x00, 0x00, 0x00, 0x00, // lea k_table(%rip), %rax | |
| 0xe8, 0x00, 0x00, 0x00, 0x00, // call bar | |
| 0x48, 0x8b, 0x0d, 0x00, 0x00, 0x00, 0x00, // mov g_sym@GOTPCREL(%rip) | |
| 0x90, 0x90, 0x90, 0x90, 0xc3, // nops + ret | |
| ]; | |
| if let Some(pair) = opts.inject_syscall { | |
| obj_text[19] = pair[0]; | |
| obj_text[20] = pair[1]; | |
| } | |
| obj_text.resize(0x20, 0x90); | |
| obj_text.extend_from_slice(&[0x31, 0xc0, 0x90, 0xc3]); | |
| let rodata = b"ANNEAL_RODATA_V1".to_vec(); | |
| let mut linked_text = obj_text.clone(); | |
| let pc32_disp = (0x2000i64 - 4 - 0x1003) as i32; | |
| linked_text[3..7].copy_from_slice(&pc32_disp.to_le_bytes()); | |
| let plt32_disp = (0x1020i64 - 4 - 0x1008) as i32; | |
| linked_text[8..12].copy_from_slice(&plt32_disp.to_le_bytes()); | |
| let got_disp = (opts.got_target_va as i64 - 4 - 0x100f) as i32; | |
| linked_text[15..19].copy_from_slice(&got_disp.to_le_bytes()); | |
| if opts.tamper_opcode { | |
| linked_text[13] = if opts.use_gottpoff { 0x81 } else { 0x8d }; | |
| } | |
| let mut linked_rodata = rodata.clone(); | |
| if opts.tamper_rodata { | |
| linked_rodata[0] ^= 0x01; | |
| } | |
| // Pre-link object (.o). | |
| let obj_syms = vec![ | |
| SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 24, | |
| }, | |
| SymSpec { | |
| name: "bar".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x20, | |
| size: 4, | |
| }, | |
| SymSpec { | |
| name: "k_table".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: rodata.len() as u64, | |
| }, | |
| SymSpec { | |
| name: "g_sym".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]; | |
| let (obj_sym_bytes, obj_str_bytes) = encode_symtab(&obj_syms); | |
| let third_reloc = | |
| if opts.use_gottpoff { R_X86_64_GOTTPOFF } else { R_X86_64_REX_GOTPCRELX }; | |
| let obj_relas = encode_rela(&[ | |
| (3, 3, R_X86_64_PC32, -4), | |
| (8, 2, R_X86_64_PLT32, -4), | |
| (15, 4, third_reloc, -4), | |
| ]); | |
| let mut obj_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: rodata, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: obj_sym_bytes, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_str_bytes, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: obj_relas, | |
| }, | |
| ]; | |
| if let Some(kind) = opts.fat_lto_kind { | |
| let (name, sh_type) = match kind { | |
| "llvm" => (".llvm.lto", SHT_LLVM_LTO), | |
| "debuglto" => (".gnu.debuglto_.inline", SHT_PROGBITS), | |
| _ => (".gnu.lto_.inline", SHT_PROGBITS), | |
| }; | |
| obj_secs.push(SecSpec { | |
| name: name.to_owned(), | |
| sh_type, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![1, 2, 3, 4], | |
| }); | |
| } | |
| let obj_bytes = build_elf(ET_REL, EM_X86_64, &[], obj_secs, &[]); | |
| // Linked ELF sections: | |
| // 1: .text @ 0x1000 | |
| // 2: .rodata @ 0x2000 | |
| // 3: .got @ 0x3000 | |
| // 4: .dynamic @ 0x3100 | |
| // 5: .dynsym @ 0x2200 | |
| // 6: .dynstr @ 0x2260 | |
| // 7: .rela.dyn @ 0x2300 | |
| // 8: .symtab, 9: .strtab | |
| let bar_va = if opts.icf_alias_bar { 0x1000 } else { 0x1020 }; | |
| let bar_sz = if opts.icf_alias_bar { 24 } else { 4 }; | |
| let mut linked_syms = vec![ | |
| SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: if opts.linked_ifunc_foo { STT_GNU_IFUNC } else { STT_FUNC }, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 24, | |
| }, | |
| SymSpec { | |
| name: "bar".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: bar_va, | |
| size: bar_sz, | |
| }, | |
| SymSpec { | |
| name: "k_table".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: linked_rodata.len() as u64, | |
| }, | |
| ]; | |
| if opts.tail_merge_unverified { | |
| linked_syms.push(SymSpec { | |
| name: "tail_merged_suffix".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1013, // strictly inside foo [0x1000..0x1018) | |
| size: 5, | |
| }); | |
| } | |
| let (l_sym_bytes, l_str_bytes) = encode_symtab(&linked_syms); | |
| let dynsym_name = opts.got_glob_dat_name.unwrap_or("other_sym"); | |
| let (dynsym_bytes, dynstr_bytes) = encode_symtab(&[SymSpec { | |
| name: dynsym_name.to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let mut rela_dyn_entries = Vec::new(); | |
| if opts.got_glob_dat_name.is_some() { | |
| rela_dyn_entries.push((0x3000, 1, R_X86_64_GLOB_DAT, 0)); | |
| } | |
| if let Some(va) = opts.dyn_reloc_va { | |
| rela_dyn_entries.push((va, 0, R_X86_64_RELATIVE, 0x1234)); | |
| } | |
| let rela_dyn_bytes = encode_rela(&rela_dyn_entries); | |
| let mut dyn_tags = vec![(5, 0x2260), (6, 0x2200), (10, dynstr_bytes.len() as u64)]; | |
| if !rela_dyn_bytes.is_empty() { | |
| dyn_tags.push((7, 0x2300)); | |
| dyn_tags.push((8, rela_dyn_bytes.len() as u64)); | |
| dyn_tags.push((9, 24)); | |
| } | |
| if opts.add_textrel { | |
| dyn_tags.push((22, 0)); // DT_TEXTREL | |
| } | |
| let dyn_bytes = encode_dyn(&dyn_tags); | |
| let mut linked_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: linked_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: linked_rodata.clone(), | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0u8; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 6, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 6, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: dynsym_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2260, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: dynstr_bytes, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2300, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym_bytes, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str_bytes, | |
| }, | |
| ]; | |
| let mut fixed = vec![ | |
| (1, 0x1000), | |
| (2, 0x2000), | |
| (3, 0x3000), | |
| (4, 0x3100), | |
| (5, 0x2200), | |
| (6, 0x2260), | |
| (7, 0x2300), | |
| ]; | |
| let prop_note = encode_gnu_property_note( | |
| GNU_PROPERTY_X86_FEATURE_1_AND, | |
| if opts.include_cet_ibt { GNU_PROPERTY_X86_FEATURE_1_IBT } else { 0 }, | |
| ); | |
| if opts.include_cet_ibt || opts.conflict_pt_gnu_property { | |
| let sec_idx = linked_secs.len() + 1; | |
| linked_secs.push(SecSpec { | |
| name: ".note.gnu.property".to_owned(), | |
| sh_type: SHT_NOTE, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: if opts.conflict_pt_gnu_property { | |
| encode_gnu_property_note(GNU_PROPERTY_X86_FEATURE_1_AND, 0) | |
| } else { | |
| prop_note.clone() | |
| }, | |
| }); | |
| fixed.push((sec_idx, 0x2100)); | |
| } | |
| if opts.conflict_pt_gnu_property { | |
| // Put conflicting PT_GNU_PROPERTY bytes at 0x2180. | |
| let sec_idx = linked_secs.len() + 1; | |
| linked_secs.push(SecSpec { | |
| name: ".rodata.prop_phdr".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2180, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: prop_note.clone(), | |
| }); | |
| fixed.push((sec_idx, 0x2180)); | |
| } | |
| if let Some(sz) = opts.stack_size_foo { | |
| let entries = if opts.second_stack_sizes_bar.is_some() { | |
| vec![(0x1000, sz)] | |
| } else { | |
| vec![(0x1000, sz), (0x1020, 0)] | |
| }; | |
| linked_secs.push(SecSpec { | |
| name: ".stack_sizes".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: encode_stack_sizes(&entries), | |
| }); | |
| } | |
| if let Some(bar_sz) = opts.second_stack_sizes_bar { | |
| linked_secs.push(SecSpec { | |
| name: ".stack_sizes".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: encode_stack_sizes(&[(0x1020, bar_sz)]), | |
| }); | |
| } | |
| if let Some(relr_va) = opts.add_relr_va { | |
| linked_secs.push(SecSpec { | |
| name: ".relr.dyn".to_owned(), | |
| sh_type: SHT_RELR, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2380, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 8, | |
| data: relr_va.to_le_bytes().to_vec(), | |
| }); | |
| } | |
| if opts.add_second_shinfo0_rela { | |
| linked_secs.push(SecSpec { | |
| name: ".rela.iplt".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x23c0, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: Vec::new(), | |
| }); | |
| } | |
| let text_flags = if opts.wx_violation { PF_R | PF_W | PF_X } else { PF_R | PF_X }; | |
| let mut phdrs = vec![ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: text_flags, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: linked_text.len() as u64, | |
| p_memsz: linked_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x400, | |
| p_memsz: 0x400, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_RELRO, | |
| p_flags: PF_R, | |
| p_offset: opts.relro_vaddr, | |
| p_vaddr: opts.relro_vaddr, | |
| p_filesz: opts.relro_memsz.min(0x200), | |
| p_memsz: opts.relro_memsz, | |
| p_align: 1, | |
| }, | |
| ]; | |
| if opts.duplicate_relro { | |
| phdrs.push(PhdrSpec { | |
| p_type: PT_GNU_RELRO, | |
| p_flags: PF_R, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 1, | |
| }); | |
| } | |
| if opts.overlap_pt_load { | |
| // Second PT_LOAD sharing 4 KiB page 0x1000..0x2000. | |
| phdrs.push(PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x1800, | |
| p_vaddr: 0x1800, | |
| p_filesz: 0x10, | |
| p_memsz: 0x10, | |
| p_align: 0x1000, | |
| }); | |
| } | |
| if opts.emit_pt_gnu_property && (opts.include_cet_ibt || opts.conflict_pt_gnu_property) { | |
| let prop_off = if opts.conflict_pt_gnu_property { 0x2180 } else { 0x2100 }; | |
| phdrs.push(PhdrSpec { | |
| p_type: PT_GNU_PROPERTY, | |
| p_flags: PF_R, | |
| p_offset: prop_off, | |
| p_vaddr: prop_off, | |
| p_filesz: prop_note.len() as u64, | |
| p_memsz: prop_note.len() as u64, | |
| p_align: 8, | |
| }); | |
| } | |
| if let Some(flags) = opts.gnu_stack_flags { | |
| phdrs.push(PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: flags, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }); | |
| } | |
| let linked_bytes = build_elf(ET_DYN, EM_X86_64, &phdrs, linked_secs, &fixed); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ | |
| ManifestSymbol { | |
| name: "foo".to_owned(), | |
| object: Some("unit.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ManifestSymbol { | |
| name: "bar".to_owned(), | |
| object: Some("unit.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ], | |
| }; | |
| (linked_bytes, obj_bytes, manifest) | |
| } | |
| fn encode_adrp(rd: u32, page_delta: i64) -> u32 { | |
| let imm21 = ((page_delta >> 12) as u32) & 0x1f_ffff; | |
| let immlo = imm21 & 0x3; | |
| let immhi = (imm21 >> 2) & 0x7ffff; | |
| 0x9000_0000 | (immlo << 29) | (immhi << 5) | (rd & 0x1f) | |
| } | |
| fn encode_add_imm12(rd: u32, rn: u32, imm12: u32) -> u32 { | |
| 0x9100_0000 | ((imm12 & 0xfff) << 10) | ((rn & 0x1f) << 5) | (rd & 0x1f) | |
| } | |
| fn encode_branch26(is_call: bool, byte_delta: i64) -> u32 { | |
| let op = if is_call { 0x9400_0000 } else { 0x1400_0000 }; | |
| op | (((byte_delta >> 2) as u32) & 0x03ff_ffff) | |
| } | |
| struct AArch64FixtureOpts { | |
| thunk_redirect: bool, | |
| tamper_rodata: bool, | |
| inject_svc: bool, | |
| include_bti_pac: bool, | |
| emit_pt_gnu_property: bool, | |
| modify_adrp_to_adr: bool, | |
| zero_size_named_rodata: bool, | |
| unresolvable_bss_adrp: bool, | |
| conflicting_duplicate_adrp: bool, | |
| } | |
| impl Default for AArch64FixtureOpts { | |
| fn default() -> Self { | |
| Self { | |
| thunk_redirect: false, | |
| tamper_rodata: false, | |
| inject_svc: false, | |
| include_bti_pac: true, | |
| emit_pt_gnu_property: true, | |
| modify_adrp_to_adr: false, | |
| zero_size_named_rodata: false, | |
| unresolvable_bss_adrp: false, | |
| conflicting_duplicate_adrp: false, | |
| } | |
| } | |
| } | |
| fn build_aarch64_fixture(opts: &AArch64FixtureOpts) -> (Vec<u8>, Vec<u8>, Manifest) { | |
| let ret_insn = if opts.inject_svc { 0xd400_0001u32 } else { 0xd65f_03c0u32 }; | |
| let mut obj_insns = vec![ | |
| encode_adrp(0, 0), | |
| encode_add_imm12(0, 0, 0), | |
| encode_branch26(true, 0), | |
| encode_branch26(false, 0), | |
| ret_insn, | |
| ]; | |
| if opts.conflicting_duplicate_adrp { | |
| obj_insns.insert(0, encode_adrp(1, 0)); | |
| } | |
| let entry_sz = (obj_insns.len() * 4) as u64; | |
| let mut obj_text = Vec::new(); | |
| for w in &obj_insns { | |
| obj_text.extend_from_slice(&w.to_le_bytes()); | |
| } | |
| while obj_text.len() < 0x20 { | |
| obj_text.extend_from_slice(&0xd503_201fu32.to_le_bytes()); | |
| } | |
| obj_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| obj_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let shift: u64 = if opts.conflicting_duplicate_adrp { 4 } else { 0 }; | |
| let call_target = if opts.thunk_redirect { 0x1_0030 } else { 0x1_0020 }; | |
| let mut adrp_linked = encode_adrp(0, 0x2_0000 - 0x1_0000); | |
| if opts.modify_adrp_to_adr { | |
| adrp_linked &= 0x7fff_ffff; | |
| } | |
| let mut linked_insns = vec![ | |
| adrp_linked, | |
| encode_add_imm12(0, 0, 0x10), | |
| encode_branch26(true, call_target - (0x1_0008 + shift as i64)), | |
| encode_branch26(false, 0x1_0024 - (0x1_000c + shift as i64)), | |
| ret_insn, | |
| ]; | |
| if opts.conflicting_duplicate_adrp { | |
| // Bogus page 0x90000 vs 0x20000 for the same (r_sym, addend). | |
| linked_insns.insert(0, encode_adrp(1, 0x9_0000 - 0x1_0000)); | |
| } | |
| let mut linked_text = Vec::new(); | |
| for w in &linked_insns { | |
| linked_text.extend_from_slice(&w.to_le_bytes()); | |
| } | |
| while linked_text.len() < 0x20 { | |
| linked_text.extend_from_slice(&0xd503_201fu32.to_le_bytes()); | |
| } | |
| linked_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| linked_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| while linked_text.len() < 0x30 { | |
| linked_text.extend_from_slice(&0xd503_201fu32.to_le_bytes()); | |
| } | |
| linked_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let mut rodata = vec![0xaa; 0x10]; | |
| rodata.extend_from_slice(b"A64_CONST_DATA!!"); | |
| let mut linked_rodata = rodata.clone(); | |
| if opts.tamper_rodata { | |
| linked_rodata[0x12] ^= 0xff; | |
| } | |
| let target_sym_spec = if opts.zero_size_named_rodata { | |
| SymSpec { | |
| name: ".L.str0".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x10, | |
| size: 0, | |
| } | |
| } else if opts.unresolvable_bss_adrp { | |
| SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 6, // writable .data/.bss section | |
| value: 0, | |
| size: 0, | |
| } | |
| } else { | |
| SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 2, | |
| value: 0, | |
| size: 0, | |
| } | |
| }; | |
| let obj_syms = vec![ | |
| SymSpec { | |
| name: "entry_a64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: entry_sz, | |
| }, | |
| SymSpec { | |
| name: "callee_a64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x20, | |
| size: 4, | |
| }, | |
| SymSpec { | |
| name: "tail_a64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x24, | |
| size: 4, | |
| }, | |
| target_sym_spec, | |
| ]; | |
| let (o_sym, o_str) = encode_symtab(&obj_syms); | |
| let ro_addend = if opts.zero_size_named_rodata { 0 } else { 0x10 }; | |
| let mut rela_list = vec![ | |
| (shift, 4, R_AARCH64_ADR_PREL_PG_HI21, ro_addend), | |
| (4 + shift, 4, R_AARCH64_ADD_ABS_LO12_NC, ro_addend), | |
| (8 + shift, 2, R_AARCH64_CALL26, 0), | |
| (12 + shift, 3, R_AARCH64_JUMP26, 0), | |
| ]; | |
| if opts.conflicting_duplicate_adrp { | |
| rela_list.insert(0, (0, 4, R_AARCH64_ADR_PREL_PG_HI21, ro_addend)); | |
| } | |
| let o_relas = encode_rela(&rela_list); | |
| let mut obj_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: rodata, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_relas, | |
| }, | |
| ]; | |
| if opts.unresolvable_bss_adrp { | |
| obj_secs.push(SecSpec { | |
| name: ".data".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0u8; 32], | |
| }); | |
| } | |
| let obj_bytes = build_elf(ET_REL, EM_AARCH64, &[], obj_secs, &[]); | |
| let linked_syms = vec![ | |
| SymSpec { | |
| name: "entry_a64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0000, | |
| size: entry_sz, | |
| }, | |
| SymSpec { | |
| name: "callee_a64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0020, | |
| size: 4, | |
| }, | |
| SymSpec { | |
| name: "tail_a64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0024, | |
| size: 4, | |
| }, | |
| SymSpec { | |
| name: "__AArch64AbsLongThunk_callee_a64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0030, | |
| size: 4, | |
| }, | |
| ]; | |
| let (l_sym, l_str) = encode_symtab(&linked_syms); | |
| let prop_note = encode_gnu_property_note( | |
| GNU_PROPERTY_AARCH64_FEATURE_1_AND, | |
| GNU_PROPERTY_AARCH64_FEATURE_1_BTI | GNU_PROPERTY_AARCH64_FEATURE_1_PAC, | |
| ); | |
| let mut l_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: linked_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: linked_rodata.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ]; | |
| let mut fixed = vec![(1, 0x1_0000), (2, 0x2_0000)]; | |
| if opts.include_bti_pac { | |
| let idx = l_secs.len() + 1; | |
| l_secs.push(SecSpec { | |
| name: ".note.gnu.property".to_owned(), | |
| sh_type: SHT_NOTE, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2_0100, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: prop_note.clone(), | |
| }); | |
| fixed.push((idx, 0x2_0100)); | |
| } | |
| let mut phdrs = vec![ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1_0000, | |
| p_vaddr: 0x1_0000, | |
| p_filesz: linked_text.len() as u64, | |
| p_memsz: linked_text.len() as u64, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2_0000, | |
| p_vaddr: 0x2_0000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x200, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ]; | |
| if opts.include_bti_pac && opts.emit_pt_gnu_property { | |
| phdrs.push(PhdrSpec { | |
| p_type: PT_GNU_PROPERTY, | |
| p_flags: PF_R, | |
| p_offset: 0x2_0100, | |
| p_vaddr: 0x2_0100, | |
| p_filesz: prop_note.len() as u64, | |
| p_memsz: prop_note.len() as u64, | |
| p_align: 8, | |
| }); | |
| } | |
| let linked_bytes = build_elf(ET_DYN, EM_AARCH64, &phdrs, l_secs, &fixed); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "entry_a64".to_owned(), | |
| object: Some("a64.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| (linked_bytes, obj_bytes, manifest) | |
| } | |
| fn run_single( | |
| linked: &[u8], | |
| obj_name: &str, | |
| obj: &[u8], | |
| manifest: &Manifest, | |
| cfg: &AuditConfig, | |
| ) -> AuditReport { | |
| audit_elf(linked, &[ObjectInput { label: obj_name.to_owned(), bytes: obj }], manifest, cfg) | |
| .expect("audit_elf should parse fixture") | |
| } | |
| #[test] | |
| fn x86_64_clean_image_passes_all_gates() { | |
| let (linked, obj, manifest) = build_x86_fixture(&X86FixtureOpts::default()); | |
| let cfg = AuditConfig { | |
| require_relro: true, | |
| require_cet_ibt: true, | |
| max_stack_bytes: Some(64), | |
| ..AuditConfig::default() | |
| }; | |
| let report = run_single(&linked, "unit.o", &obj, &manifest, &cfg); | |
| assert!(report.passed(), "findings: {:?}", report.findings); | |
| assert_eq!(report.verdict, "PASS"); | |
| assert!(report.wx_ok); | |
| assert!(report.gnu_stack_ok); | |
| assert_eq!(report.relro.covers_got, Some(true)); | |
| assert_eq!(report.relro.covers_dynamic, Some(true)); | |
| assert_eq!(report.relro.effective_end, Some(0x4000)); | |
| assert!(report.gnu_properties.pt_gnu_property_present); | |
| assert!(report.gnu_properties.x86_ibt); | |
| assert_eq!(report.stack_sizes.max_verified_stack_bytes, Some(32)); | |
| assert_eq!(report.rodata_checks.len(), 1); | |
| assert!(report.rodata_checks[0].ok); | |
| } | |
| #[test] | |
| fn aarch64_clean_image_passes_all_gates() { | |
| let (linked, obj, manifest) = build_aarch64_fixture(&AArch64FixtureOpts::default()); | |
| let cfg = AuditConfig { require_bti: true, require_pac: true, ..AuditConfig::default() }; | |
| let report = run_single(&linked, "a64.o", &obj, &manifest, &cfg); | |
| assert!(report.passed(), "findings: {:?}", report.findings); | |
| assert_eq!(report.arch, Arch::AArch64); | |
| assert!(report.gnu_properties.pt_gnu_property_present); | |
| assert!(report.gnu_properties.aarch64_bti); | |
| assert!(report.gnu_properties.aarch64_pac); | |
| assert_eq!(report.rodata_checks.len(), 1); | |
| assert!(report.rodata_checks[0].ok); | |
| } | |
| #[test] | |
| fn rejects_wx_violation() { | |
| let (linked, obj, manifest) = | |
| build_x86_fixture(&X86FixtureOpts { wx_violation: true, ..X86FixtureOpts::default() }); | |
| let report = run_single(&linked, "unit.o", &obj, &manifest, &AuditConfig::default()); | |
| assert!(!report.passed()); | |
| assert!(report.findings.iter().any(|f| f.code == "WX_VIOLATION")); | |
| } | |
| #[test] | |
| fn rejects_executable_and_missing_gnu_stack() { | |
| let (linked, obj, manifest) = build_x86_fixture(&X86FixtureOpts { | |
| gnu_stack_flags: Some(PF_R | PF_W | PF_X), | |
| ..X86FixtureOpts::default() | |
| }); | |
| let report = run_single(&linked, "unit.o", &obj, &manifest, &AuditConfig::default()); | |
| assert!(report.findings.iter().any(|f| f.code == "GNU_STACK_EXECUTABLE")); | |
| let (linked2, obj2, manifest2) = build_x86_fixture(&X86FixtureOpts { | |
| gnu_stack_flags: None, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let report2 = run_single(&linked2, "unit.o", &obj2, &manifest2, &AuditConfig::default()); | |
| assert!(report2.findings.iter().any(|f| f.code == "GNU_STACK_MISSING")); | |
| let opt_out = AuditConfig { allow_missing_gnu_stack: true, ..AuditConfig::default() }; | |
| let report3 = run_single(&linked2, "unit.o", &obj2, &manifest2, &opt_out); | |
| assert!(report3.passed(), "findings: {:?}", report3.findings); | |
| } | |
| #[test] | |
| fn rejects_gottpoff_and_gotpcrelx_opcode_rewrites() { | |
| let (linked, obj, manifest) = build_x86_fixture(&X86FixtureOpts { | |
| use_gottpoff: true, | |
| tamper_opcode: true, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let report = run_single(&linked, "unit.o", &obj, &manifest, &AuditConfig::default()); | |
| assert!(report.findings.iter().any(|f| f.code == "GOTTPOFF_FORBIDDEN")); | |
| assert!(report.findings.iter().any(|f| f.code == "TEXT_BYTE_MISMATCH")); | |
| let (linked2, obj2, manifest2) = | |
| build_x86_fixture(&X86FixtureOpts { tamper_opcode: true, ..X86FixtureOpts::default() }); | |
| let report2 = run_single(&linked2, "unit.o", &obj2, &manifest2, &AuditConfig::default()); | |
| assert!(report2.findings.iter().any(|f| f.code == "TEXT_BYTE_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_aarch64_thunk_indirection_and_adrp_rewrite() { | |
| let (linked, obj, manifest) = build_aarch64_fixture(&AArch64FixtureOpts { | |
| thunk_redirect: true, | |
| ..AArch64FixtureOpts::default() | |
| }); | |
| let report = run_single(&linked, "a64.o", &obj, &manifest, &AuditConfig::default()); | |
| assert!(report.findings.iter().any(|f| f.code == "AARCH64_THUNK_INDIRECTION")); | |
| let (linked2, obj2, manifest2) = build_aarch64_fixture(&AArch64FixtureOpts { | |
| modify_adrp_to_adr: true, | |
| ..AArch64FixtureOpts::default() | |
| }); | |
| let report2 = run_single(&linked2, "a64.o", &obj2, &manifest2, &AuditConfig::default()); | |
| assert!(report2.findings.iter().any(|f| f.code == "AARCH64_OPCODE_MODIFIED")); | |
| } | |
| #[test] | |
| fn rejects_icf_aliasing_tail_merge_and_linked_ifunc() { | |
| let (linked, obj, manifest) = | |
| build_x86_fixture(&X86FixtureOpts { icf_alias_bar: true, ..X86FixtureOpts::default() }); | |
| let report = run_single(&linked, "unit.o", &obj, &manifest, &AuditConfig::default()); | |
| assert!(report.findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| let (linked2, obj2, manifest2) = build_x86_fixture(&X86FixtureOpts { | |
| tail_merge_unverified: true, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let report2 = run_single(&linked2, "unit.o", &obj2, &manifest2, &AuditConfig::default()); | |
| assert!(report2.findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| let (linked3, obj3, manifest3) = build_x86_fixture(&X86FixtureOpts { | |
| linked_ifunc_foo: true, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let report3 = run_single(&linked3, "unit.o", &obj3, &manifest3, &AuditConfig::default()); | |
| assert!(report3.findings.iter().any(|f| f.code == "SYMBOL_IFUNC_FORBIDDEN")); | |
| } | |
| #[test] | |
| fn rejects_x86_syscalls_and_aarch64_svc() { | |
| for bytes in [[0x0f, 0x05], [0x0f, 0x34], [0xcd, 0x80]] { | |
| let (linked, obj, manifest) = build_x86_fixture(&X86FixtureOpts { | |
| inject_syscall: Some(bytes), | |
| ..X86FixtureOpts::default() | |
| }); | |
| let report = run_single(&linked, "unit.o", &obj, &manifest, &AuditConfig::default()); | |
| assert!( | |
| report.findings.iter().any(|f| f.code == "FORBIDDEN_SYSCALL_INSN"), | |
| "expected syscall finding for {bytes:02x?}" | |
| ); | |
| let mut allow_manifest = manifest.clone(); | |
| allow_manifest.symbols[0].allow_syscalls = true; | |
| let report_ok = | |
| run_single(&linked, "unit.o", &obj, &allow_manifest, &AuditConfig::default()); | |
| assert!(report_ok.passed(), "findings: {:?}", report_ok.findings); | |
| } | |
| let (linked_a64, obj_a64, manifest_a64) = build_aarch64_fixture(&AArch64FixtureOpts { | |
| inject_svc: true, | |
| ..AArch64FixtureOpts::default() | |
| }); | |
| let report_a64 = | |
| run_single(&linked_a64, "a64.o", &obj_a64, &manifest_a64, &AuditConfig::default()); | |
| assert!(report_a64.findings.iter().any(|f| f.code == "FORBIDDEN_SYSCALL_INSN")); | |
| } | |
| #[test] | |
| fn rejects_rodata_tamper_on_both_arches() { | |
| let (linked, obj, manifest) = | |
| build_x86_fixture(&X86FixtureOpts { tamper_rodata: true, ..X86FixtureOpts::default() }); | |
| let report = run_single(&linked, "unit.o", &obj, &manifest, &AuditConfig::default()); | |
| assert!(report.findings.iter().any(|f| f.code == "RODATA_MISMATCH")); | |
| let (linked_a64, obj_a64, manifest_a64) = build_aarch64_fixture(&AArch64FixtureOpts { | |
| tamper_rodata: true, | |
| ..AArch64FixtureOpts::default() | |
| }); | |
| let report_a64 = | |
| run_single(&linked_a64, "a64.o", &obj_a64, &manifest_a64, &AuditConfig::default()); | |
| assert!(report_a64.findings.iter().any(|f| f.code == "RODATA_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_got_slot_redirection_and_unbound_external_plt32() { | |
| // 1. GOT slot pointing outside .got (into .rodata @ 0x2000). | |
| let (l1, o1, m1) = build_x86_fixture(&X86FixtureOpts { | |
| got_target_va: 0x2000, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r1 = run_single(&l1, "unit.o", &o1, &m1, &AuditConfig::default()); | |
| assert!(r1.findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| // 2. GOT slot in .got, but GLOB_DAT binds to a different symbol. | |
| let (l2, o2, m2) = build_x86_fixture(&X86FixtureOpts { | |
| got_glob_dat_name: Some("wrong_sym"), | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r2 = run_single(&l2, "unit.o", &o2, &m2, &AuditConfig::default()); | |
| assert!(r2.findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| // 3. External PLT32 call: eager PT_DYNAMIC BIND_NOW -> PASS; lazy, | |
| // section-only, wrong symbol, or arbitrary RX -> FAIL. | |
| let build_plt_pair = |call_to_plt: bool, | |
| wrong_jump_slot: bool, | |
| with_bind_now: bool, | |
| with_pt_dynamic: bool| | |
| -> AuditReport { | |
| // `caller` @ 0x1000: `e8 <disp32>; c3` (6 bytes). | |
| let obj_text = vec![0xe8, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(1, 2, R_X86_64_PLT32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| // Linked: .text @ 0x1000, .plt @ 0x1040 (jmp *0x3000(%rip)). | |
| let target_va: i64 = if call_to_plt { 0x1040 } else { 0x1020 }; | |
| let mut l_text = obj_text; | |
| let disp = (target_va - 4 - 0x1001) as i32; | |
| l_text[1..5].copy_from_slice(&disp.to_le_bytes()); | |
| l_text.resize(0x24, 0x90); | |
| l_text[0x20] = 0xc3; // arbitrary RX ret @ 0x1020 | |
| let mut plt_bytes = vec![0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90]; | |
| let plt_disp = (0x3000i64 - 0x1046) as i32; | |
| plt_bytes[2..6].copy_from_slice(&plt_disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 6, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: if wrong_jump_slot { "other_fn" } else { "ext_fn" }.to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let rela_plt = encode_rela(&[(0x3000, 1, R_X86_64_JUMP_SLOT, 0)]); | |
| let mut dyn_tags = vec![ | |
| (5, 0x2060), // DT_STRTAB | |
| (6, 0x2000), // DT_SYMTAB | |
| (10, d_str.len() as u64), // DT_STRSZ | |
| (11, 24), // DT_SYMENT | |
| (23, 0x2100), // DT_JMPREL | |
| (2, rela_plt.len() as u64), // DT_PLTRELSZ | |
| (20, 7), // DT_PLTREL = DT_RELA | |
| ]; | |
| if with_bind_now { | |
| dyn_tags.push((30, 0x8)); // DT_FLAGS = DF_BIND_NOW | |
| } | |
| let dyn_bytes = encode_dyn(&dyn_tags); | |
| let mut phdrs = vec![ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x100, | |
| p_memsz: 0x100, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x200, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x200, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ]; | |
| if with_pt_dynamic { | |
| phdrs.push(PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &phdrs, | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1040, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: plt_bytes, | |
| }, | |
| SecSpec { | |
| name: ".got.plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0u8; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2060, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.plt".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_plt, | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[ | |
| (1, 0x1000), | |
| (2, 0x1040), | |
| (3, 0x3000), | |
| (4, 0x2000), | |
| (5, 0x2060), | |
| (6, 0x2100), | |
| (7, 0x3100), | |
| ], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "caller".to_owned(), | |
| object: Some("plt.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "plt.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| assert!(build_plt_pair(true, false, true, true).passed()); | |
| // Lazy JUMP_SLOT without BIND_NOW rejected. | |
| assert!(build_plt_pair(true, false, false, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // Phantom section-only .rela.plt without PT_DYNAMIC rejected. | |
| assert!(build_plt_pair(true, false, true, false) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(build_plt_pair(false, false, true, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(build_plt_pair(true, true, true, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_aarch64_unresolvable_bss_conflicting_adrp_and_zero_size_rodata() { | |
| let (l1, o1, m1) = build_aarch64_fixture(&AArch64FixtureOpts { | |
| unresolvable_bss_adrp: true, | |
| ..AArch64FixtureOpts::default() | |
| }); | |
| let r1 = run_single(&l1, "a64.o", &o1, &m1, &AuditConfig::default()); | |
| assert!(r1.findings.iter().any(|f| f.code == "AARCH64_RELOC_TARGET_MISMATCH")); | |
| let (l2, o2, m2) = build_aarch64_fixture(&AArch64FixtureOpts { | |
| conflicting_duplicate_adrp: true, | |
| ..AArch64FixtureOpts::default() | |
| }); | |
| let r2 = run_single(&l2, "a64.o", &o2, &m2, &AuditConfig::default()); | |
| assert!(r2.findings.iter().any(|f| f.code == "AARCH64_RELOC_TARGET_MISMATCH")); | |
| let (l3, o3, m3) = build_aarch64_fixture(&AArch64FixtureOpts { | |
| zero_size_named_rodata: true, | |
| ..AArch64FixtureOpts::default() | |
| }); | |
| let r3 = run_single(&l3, "a64.o", &o3, &m3, &AuditConfig::default()); | |
| assert!(r3.findings.iter().any(|f| f.code == "RODATA_UNRESOLVABLE")); | |
| } | |
| #[test] | |
| fn x86_merged_rodata_handles_cmpb_pc_bias_and_wide_strings() { | |
| // Test 1: `cmpb $0x41, .rodata.str1.1+8(%rip)` (`80 3d disp32 41 c3`): | |
| // pc_bias = -5, so r_addend = 8 - 5 = 3. | |
| let build_merged = |wide: bool, tamper_tail: bool| -> AuditReport { | |
| let (obj_text, rel_off, addend, rodata, sec_name, entsize, target_va) = if !wide { | |
| // "1234567\0SECOND\0": second string starts at +8. | |
| let code = vec![0x80, 0x3d, 0, 0, 0, 0, 0x41, 0xc3]; | |
| let mut ro = b"1234567\0SECOND\0".to_vec(); | |
| ro.resize(16, 0); | |
| (code, 2u64, 3i64, ro, ".rodata.str1.1", 1u64, 0x2008i64) | |
| } else { | |
| // UTF-16 "AB\0" at offset 0: [b'A', 0, b'B', 0, 0, 0]. | |
| let code = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let ro = vec![b'A', 0, b'B', 0, 0, 0, 0, 0]; | |
| (code, 3u64, -4i64, ro, ".rodata.str2.2", 2u64, 0x2000i64) | |
| }; | |
| let pc_bias = if !wide { -5i64 } else { -4i64 }; | |
| let mut l_text = obj_text.clone(); | |
| let disp = (target_va + pc_bias - (0x1000 + rel_off as i64)) as i32; | |
| l_text[rel_off as usize..rel_off as usize + 4].copy_from_slice(&disp.to_le_bytes()); | |
| let mut l_ro = rodata.clone(); | |
| if tamper_tail { | |
| if !wide { | |
| l_ro[10] ^= 0xff; // flip byte inside "SECOND" | |
| } else { | |
| l_ro[2] ^= 0xff; // flip 2nd UTF-16 char 'B' | |
| } | |
| } | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: obj_text.len() as u64, | |
| }, | |
| SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 2, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text, | |
| }, | |
| SecSpec { | |
| name: sec_name.to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_MERGE | SHF_STRINGS, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: entsize, | |
| data: rodata, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(rel_off, 2, R_X86_64_PC32, addend)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: l_text.len() as u64, | |
| }]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("m.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "m.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| // cmpb pc_bias = -5: clean passes (7 bytes = "SECOND\0"), tamper fails. | |
| let r_ok = build_merged(false, false); | |
| assert!(r_ok.passed(), "findings: {:?}", r_ok.findings); | |
| assert_eq!(r_ok.rodata_checks[0].len, 7); | |
| let r_bad = build_merged(false, true); | |
| assert!(r_bad.findings.iter().any(|f| f.code == "RODATA_MISMATCH")); | |
| // UTF-16 wide string: clean passes (6 bytes = "AB\0"); tamper fails. | |
| let w_ok = build_merged(true, false); | |
| assert!(w_ok.passed(), "findings: {:?}", w_ok.findings); | |
| assert_eq!(w_ok.rodata_checks[0].len, 6); | |
| let w_bad = build_merged(true, true); | |
| assert!(w_bad.findings.iter().any(|f| f.code == "RODATA_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_textrel_dyn_reloc_mutation_and_accepts_multi_shinfo0() { | |
| // DT_TEXTREL rejected. | |
| let (l1, o1, m1) = | |
| build_x86_fixture(&X86FixtureOpts { add_textrel: true, ..X86FixtureOpts::default() }); | |
| let r1 = run_single(&l1, "unit.o", &o1, &m1, &AuditConfig::default()); | |
| assert!(r1.findings.iter().any(|f| f.code == "DYNAMIC_TEXTREL_FORBIDDEN")); | |
| // Dynamic relocation overlapping verified `.text` rejected. | |
| let (l2, o2, m2) = build_x86_fixture(&X86FixtureOpts { | |
| dyn_reloc_va: Some(0x1004), | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r2 = run_single(&l2, "unit.o", &o2, &m2, &AuditConfig::default()); | |
| assert!(r2.findings.iter().any(|f| f.code == "DYNAMIC_RELOC_MUTATES_VERIFIED")); | |
| // `.relr.dyn` targeting verified `.rodata` rejected. | |
| let (l3, o3, m3) = build_x86_fixture(&X86FixtureOpts { | |
| add_relr_va: Some(0x2000), | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r3 = run_single(&l3, "unit.o", &o3, &m3, &AuditConfig::default()); | |
| assert!(r3.findings.iter().any(|f| f.code == "DYNAMIC_RELOC_MUTATES_VERIFIED")); | |
| // Multiple `sh_info == 0` SHT_RELA sections in linked ELF accepted. | |
| let (l4, o4, m4) = build_x86_fixture(&X86FixtureOpts { | |
| add_second_shinfo0_rela: true, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r4 = run_single(&l4, "unit.o", &o4, &m4, &AuditConfig::default()); | |
| assert!(r4.passed(), "findings: {:?}", r4.findings); | |
| } | |
| #[test] | |
| fn rejects_unaligned_relro_duplicate_relro_pt_load_overlap_and_missing_phdr() { | |
| // Unaligned PT_GNU_RELRO 0x3000..0x3010 rounds down to 0x3000 (0 B). | |
| let (l1, o1, m1) = | |
| build_x86_fixture(&X86FixtureOpts { relro_memsz: 0x10, ..X86FixtureOpts::default() }); | |
| let r1 = run_single( | |
| &l1, | |
| "unit.o", | |
| &o1, | |
| &m1, | |
| &AuditConfig { require_relro: true, ..AuditConfig::default() }, | |
| ); | |
| assert!(r1.findings.iter().any(|f| f.code == "RELRO_INVALID")); | |
| assert!(r1.findings.iter().any(|f| f.code == "RELRO_GOT_UNCOVERED")); | |
| // Duplicate PT_GNU_RELRO rejected. | |
| let (l2, o2, m2) = build_x86_fixture(&X86FixtureOpts { | |
| duplicate_relro: true, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r2 = run_single(&l2, "unit.o", &o2, &m2, &AuditConfig::default()); | |
| assert!(r2.findings.iter().any(|f| f.code == "RELRO_DUPLICATE")); | |
| // Page-overlapping PT_LOAD segments rejected. | |
| let (l3, o3, m3) = build_x86_fixture(&X86FixtureOpts { | |
| overlap_pt_load: true, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r3 = run_single(&l3, "unit.o", &o3, &m3, &AuditConfig::default()); | |
| assert!(r3.findings.iter().any(|f| f.code == "LOAD_SEGMENT_OVERLAP")); | |
| // `.note.gnu.property` WITHOUT `PT_GNU_PROPERTY` phdr fails CET/BTI. | |
| let (l4, o4, m4) = build_x86_fixture(&X86FixtureOpts { | |
| emit_pt_gnu_property: false, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r4 = run_single( | |
| &l4, | |
| "unit.o", | |
| &o4, | |
| &m4, | |
| &AuditConfig { require_cet_ibt: true, ..AuditConfig::default() }, | |
| ); | |
| assert!(r4.findings.iter().any(|f| f.code == "CET_IBT_MISSING")); | |
| // Conflicting `PT_GNU_PROPERTY` vs `.note.gnu.property` fails. | |
| let (l5, o5, m5) = build_x86_fixture(&X86FixtureOpts { | |
| conflict_pt_gnu_property: true, | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r5 = run_single(&l5, "unit.o", &o5, &m5, &AuditConfig::default()); | |
| assert!(r5.findings.iter().any(|f| f.code == "GNU_PROPERTY_PARSE_ERROR")); | |
| // AArch64 section-only `.note.gnu.property` without phdr fails BTI/PAC. | |
| let (la, oa, ma) = build_aarch64_fixture(&AArch64FixtureOpts { | |
| emit_pt_gnu_property: false, | |
| ..AArch64FixtureOpts::default() | |
| }); | |
| let ra = run_single( | |
| &la, | |
| "a64.o", | |
| &oa, | |
| &ma, | |
| &AuditConfig { require_bti: true, require_pac: true, ..AuditConfig::default() }, | |
| ); | |
| assert!(ra.findings.iter().any(|f| f.code == "AARCH64_BTI_MISSING")); | |
| assert!(ra.findings.iter().any(|f| f.code == "AARCH64_PAC_MISSING")); | |
| } | |
| #[test] | |
| fn rejects_fat_lto_variants_binding_mismatch_and_aggregates_stack_sizes() { | |
| for kind in ["gnu", "llvm", "debuglto"] { | |
| let (l, o, m) = build_x86_fixture(&X86FixtureOpts { | |
| fat_lto_kind: Some(kind), | |
| ..X86FixtureOpts::default() | |
| }); | |
| let r = run_single(&l, "unit.o", &o, &m, &AuditConfig::default()); | |
| assert!( | |
| r.findings.iter().any(|f| f.code == "FAT_LTO_OBJECT"), | |
| "expected FAT_LTO_OBJECT for {kind}" | |
| ); | |
| } | |
| // Multiple `.stack_sizes` sections aggregated; second exceeds limit. | |
| let (ls, os, ms) = build_x86_fixture(&X86FixtureOpts { | |
| stack_size_foo: Some(32), | |
| second_stack_sizes_bar: Some(128), | |
| ..X86FixtureOpts::default() | |
| }); | |
| let rs = run_single( | |
| &ls, | |
| "unit.o", | |
| &os, | |
| &ms, | |
| &AuditConfig { max_stack_bytes: Some(64), ..AuditConfig::default() }, | |
| ); | |
| assert_eq!(rs.stack_sizes.entry_count, 2); | |
| assert_eq!(rs.stack_sizes.max_verified_stack_bytes, Some(128)); | |
| assert!(rs.findings.iter().any(|f| f.code == "STACK_SIZE_EXCEEDED")); | |
| // Local-vs-global binding isolation: STB_LOCAL target in another | |
| // section must NOT resolve to a same-named STB_GLOBAL linked symbol. | |
| let obj_text = vec![0xe8, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "helper".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 2, // separate section (.text.helper) | |
| value: 0, | |
| size: 1, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".text.helper".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0xc3], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(1, 2, R_X86_64_PLT32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x1020i64 - 4 - 0x1001) as i32; | |
| l_text[1..5].copy_from_slice(&disp.to_le_bytes()); | |
| l_text.resize(0x21, 0x90); | |
| l_text[0x20] = 0xc3; | |
| let (l_sym, l_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "helper".to_owned(), | |
| bind: STB_GLOBAL, // unrelated global! | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1020, | |
| size: 1, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000)], | |
| ); | |
| let m_bind = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "entry".to_owned(), | |
| object: Some("b.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| let r_bind = run_single(&linked, "b.o", &obj, &m_bind, &AuditConfig::default()); | |
| assert!(r_bind.findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| } | |
| #[test] | |
| fn shn_xindex_defined_and_crafted_pn_xnum_no_oom() { | |
| // 1. Symbol with SHN_XINDEX resolving to section >= 0xff00 is defined. | |
| let (sym_bytes, str_bytes) = encode_symtab(&[SymSpec { | |
| name: "big_sec_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: SHN_XINDEX, | |
| value: 0, | |
| size: 4, | |
| }]); | |
| let mut shndx_table = Vec::new(); | |
| shndx_table.extend_from_slice(&0u32.to_le_bytes()); // STN_UNDEF | |
| shndx_table.extend_from_slice(&0xff05u32.to_le_bytes()); // >= SHN_LORESERVE | |
| let elf_x = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: sym_bytes, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: str_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab_shndx".to_owned(), | |
| sh_type: SHT_SYMTAB_SHNDX, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 1, | |
| sh_info: 0, | |
| sh_entsize: 4, | |
| data: shndx_table, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let parsed = Elf64::parse(&elf_x).expect("SHN_XINDEX ELF parses"); | |
| assert!(parsed.symbols[1].is_defined()); | |
| assert_eq!(parsed.symbols[1].st_shndx, 0xff05); | |
| // 2. Crafted 128-byte ELF with PN_XNUM + sh0.sh_info == u32::MAX must | |
| // fail cleanly with Err without attempting a 274 GiB allocation. | |
| let mut crafted = vec![0u8; 128]; | |
| crafted[0..4].copy_from_slice(&[0x7f, b'E', b'L', b'F']); | |
| crafted[4] = 2; | |
| crafted[5] = 1; | |
| crafted[6] = 1; | |
| crafted[16..18].copy_from_slice(&ET_DYN.to_le_bytes()); | |
| crafted[18..20].copy_from_slice(&EM_X86_64.to_le_bytes()); | |
| crafted[20..24].copy_from_slice(&1u32.to_le_bytes()); | |
| crafted[32..40].copy_from_slice(&64u64.to_le_bytes()); // e_phoff | |
| crafted[40..48].copy_from_slice(&64u64.to_le_bytes()); // e_shoff | |
| crafted[52..54].copy_from_slice(&64u16.to_le_bytes()); | |
| crafted[54..56].copy_from_slice(&56u16.to_le_bytes()); | |
| crafted[56..58].copy_from_slice(&0xffffu16.to_le_bytes()); // PN_XNUM | |
| crafted[58..60].copy_from_slice(&64u16.to_le_bytes()); | |
| crafted[60..62].copy_from_slice(&1u16.to_le_bytes()); | |
| crafted[64 + 44..64 + 48].copy_from_slice(&u32::MAX.to_le_bytes()); // sh0.sh_info | |
| assert!(Elf64::parse(&crafted).is_err()); | |
| } | |
| #[test] | |
| fn gotpcrel_verifies_rodata_rejects_local_glob_dat_and_supports_dt_relr() { | |
| // Minimal x86-64 fixture where `f` accesses `c_ro` via REX_GOTPCRELX. | |
| let run_case = |use_relr: bool, | |
| local_glob_dat: bool, | |
| tamper_rodata: bool, | |
| bad_relr_val: bool| | |
| -> AuditReport { | |
| // `f`: `48 8b 05 <disp32>; c3` (8 bytes). | |
| let obj_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let rodata = b"GOT_RODATA_BYTES".to_vec(); | |
| let sym_bind = if local_glob_dat { STB_LOCAL } else { STB_GLOBAL }; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "c_ro".to_owned(), | |
| bind: sym_bind, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: rodata.len() as u64, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: rodata.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x3000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let mut l_ro = rodata; | |
| if tamper_rodata { | |
| l_ro[0] ^= 0xff; | |
| } | |
| let got_val: u64 = if bad_relr_val { 0x2008 } else { 0x2000 }; | |
| let mut got_bytes = got_val.to_le_bytes().to_vec(); | |
| got_bytes.resize(16, 0); | |
| let (l_sym, l_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "c_ro".to_owned(), | |
| bind: sym_bind, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: l_ro.len() as u64, | |
| }, | |
| ]); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "c_ro".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: l_ro.len() as u64, | |
| }]); | |
| let rela_dyn = if use_relr { | |
| Vec::new() | |
| } else { | |
| encode_rela(&[(0x3000, 1, R_X86_64_GLOB_DAT, 0)]) | |
| }; | |
| let relr_dyn = 0x3000u64.to_le_bytes().to_vec(); | |
| let mut dyn_tags = vec![(5, 0x2160), (6, 0x2100), (10, d_str.len() as u64), (11, 24)]; | |
| if use_relr { | |
| dyn_tags.push((36, 0x2200)); // DT_RELR | |
| dyn_tags.push((35, 8)); // DT_RELRSZ | |
| dyn_tags.push((37, 8)); // DT_RELRENT | |
| } else { | |
| dyn_tags.push((7, 0x2200)); // DT_RELA | |
| dyn_tags.push((8, rela_dyn.len() as u64)); // DT_RELASZ | |
| dyn_tags.push((9, 24)); // DT_RELAENT | |
| } | |
| let dyn_bytes = encode_dyn(&dyn_tags); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: got_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 6, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 6, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: if use_relr { ".relr.dyn" } else { ".rela.dyn" }.to_owned(), | |
| sh_type: if use_relr { SHT_RELR } else { SHT_RELA }, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: if use_relr { 0 } else { 5 }, | |
| sh_info: 0, | |
| sh_entsize: if use_relr { 8 } else { 24 }, | |
| data: if use_relr { relr_dyn } else { rela_dyn }, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[ | |
| (1, 0x1000), | |
| (2, 0x2000), | |
| (3, 0x3000), | |
| (4, 0x3100), | |
| (5, 0x2100), | |
| (6, 0x2160), | |
| (7, 0x2200), | |
| ], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("g.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "g.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| // Interposable R_X86_64_GLOB_DAT on .rodata in ET_DYN is rejected | |
| // because ld.so may redirect or copy-relocate overridable .rodata. | |
| let r_glob_ro = run_case(false, false, false, false); | |
| assert!(r_glob_ro.findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| // STB_LOCAL symbol cannot bind via global R_X86_64_GLOB_DAT. | |
| let r_local = run_case(false, true, false, false); | |
| assert!(r_local.findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| // DT_RELR in-place addend: matching passes + checks .rodata SHA-256, | |
| // tampered .rodata fails with RODATA_MISMATCH, mismatched slot fails. | |
| let r_relr_ok = run_case(true, false, false, false); | |
| assert!(r_relr_ok.passed(), "findings: {:?}", r_relr_ok.findings); | |
| assert_eq!(r_relr_ok.rodata_checks.len(), 1); | |
| let r_relr_tamper = run_case(true, false, true, false); | |
| assert!(r_relr_tamper.findings.iter().any(|f| f.code == "RODATA_MISMATCH")); | |
| let r_relr_bad = run_case(true, false, false, true); | |
| assert!(r_relr_bad.findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| } | |
| #[test] | |
| fn rejects_reordered_same_section_callee_and_ifunc_callee() { | |
| let run_case = |reorder_bar: bool, ifunc_bar: bool| -> AuditReport { | |
| // `foo` @ 0x0 (6 B) calls sibling `bar` @ 0x20 (1 B) in same .text. | |
| let mut obj_text = vec![0xe8, 0, 0, 0, 0, 0xc3]; | |
| obj_text.resize(0x21, 0x90); | |
| obj_text[0x20] = 0xc3; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "bar".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x20, | |
| size: 1, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(1, 2, R_X86_64_PLT32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| // Linked binary patches call to 0x1020, but moves `bar` to 0x1050. | |
| let mut l_text = obj_text; | |
| let disp = (0x1020i64 - 4 - 0x1001) as i32; | |
| l_text[1..5].copy_from_slice(&disp.to_le_bytes()); | |
| l_text.resize(0x51, 0x90); | |
| l_text[0x50] = 0xc3; | |
| let bar_va = if reorder_bar { 0x1050 } else { 0x1020 }; | |
| let (l_sym, l_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "bar".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: if ifunc_bar { STT_GNU_IFUNC } else { STT_FUNC }, | |
| shndx: 1, | |
| value: bar_va, | |
| size: 1, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "foo".to_owned(), | |
| object: Some("sib.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "sib.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| assert!(run_case(false, false).passed()); | |
| assert!(run_case(true, false).findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_case(false, true).findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_tlsdesc_16b_and_copy_overlap_and_spoofed_pt_dynamic() { | |
| // Place GOT slot at 0x3008 and a 16-byte TLSDESC or 16-byte COPY at | |
| // 0x3000 so its second 8 bytes overwrite [0x3008..0x3010). | |
| let run_overlap = |use_copy: bool, spoof_dyn_filesz_zero: bool| -> AuditReport { | |
| let obj_text = vec![0x48, 0x8b, 0x0d, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "g_sym".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x3008i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "g_sym".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| SymSpec { | |
| name: "big_copy".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 16, | |
| }, | |
| ]); | |
| let overlap_type = if use_copy { R_X86_64_COPY } else { R_X86_64_TLSDESC }; | |
| let rela_dyn = | |
| encode_rela(&[(0x3000, 2, overlap_type, 0), (0x3008, 1, R_X86_64_GLOB_DAT, 0)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| ]); | |
| let dyn_filesz = if spoof_dyn_filesz_zero { 0 } else { dyn_bytes.len() as u64 }; | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_filesz, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0u8; 32], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 8, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x3000), (3, 0x3100), (4, 0x2100), (5, 0x2160), (6, 0x2200)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("t.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "t.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| assert!(run_overlap(false, false) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| assert!(run_overlap(true, false).findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| assert!(run_overlap(false, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID")); | |
| } | |
| #[test] | |
| fn aarch64_64k_relro_rounding_and_pt_dynamic_relro_coverage() { | |
| // AArch64 RELRO ending at 0x4000 (4 KiB, not 64 KiB) rounds to 0. | |
| let obj_text = 0xd65f_03c0u32.to_le_bytes().to_vec(); | |
| let (o_sym, o_str) = encode_symtab(&[SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 4, | |
| }]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0000, | |
| size: 4, | |
| }]); | |
| let dyn_bytes = encode_dyn(&[]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1_0000, | |
| p_vaddr: 0x1_0000, | |
| p_filesz: 4, | |
| p_memsz: 4, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x2_0000, | |
| p_vaddr: 0x2_0000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x4000, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x2_0100, | |
| p_vaddr: 0x2_0100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_RELRO, | |
| p_flags: PF_R, | |
| p_offset: 0x2_0000, | |
| p_vaddr: 0x2_0000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x4000, // 0x24000 is not 64 KiB aligned! | |
| p_align: 1, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x2_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0u8; 16], | |
| }, | |
| SecSpec { | |
| name: ".dyn_renamed".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x2_0100, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1_0000), (2, 0x2_0000), (3, 0x2_0100)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f64".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| let r = run_single( | |
| &linked, | |
| "a.o", | |
| &obj, | |
| &manifest, | |
| &AuditConfig { require_relro: true, ..AuditConfig::default() }, | |
| ); | |
| assert!(r.findings.iter().any(|f| f.code == "RELRO_INVALID")); | |
| assert!(r.findings.iter().any(|f| f.code == "RELRO_GOT_UNCOVERED")); | |
| assert!(r.findings.iter().any(|f| f.code == "RELRO_DYNAMIC_UNCOVERED")); | |
| } | |
| #[test] | |
| fn rejects_notype_zero_size_dynsym_aliases_and_cross_object_rodata_tamper() { | |
| // Cross-object .rodata: caller.o references `ext_ro` in rodata.o. | |
| let run_cross = |tamper_ro: bool, add_notype_alias: bool, add_dynsym_alias: bool| { | |
| let obj_text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (o1_sym, o1_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "cross_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "ext_ro".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj1 = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o1_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o1_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let rodata = b"CROSS_OBJ_CONST!".to_vec(); | |
| let (o2_sym, o2_str) = encode_symtab(&[SymSpec { | |
| name: "ext_ro".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 1, | |
| value: 0, | |
| size: rodata.len() as u64, | |
| }]); | |
| let obj2 = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: rodata.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o2_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o2_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x2000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let mut l_ro = rodata; | |
| if tamper_ro { | |
| l_ro[0] ^= 0xff; | |
| } | |
| let mut l_syms = vec![ | |
| SymSpec { | |
| name: "cross_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "ext_ro".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: l_ro.len() as u64, | |
| }, | |
| ]; | |
| if add_notype_alias { | |
| l_syms.push(SymSpec { | |
| name: "asm_alias".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_NOTYPE, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 0, | |
| }); | |
| } | |
| let (l_sym, l_str) = encode_symtab(&l_syms); | |
| let mut l_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ]; | |
| if add_dynsym_alias { | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "dyn_alias".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }]); | |
| l_secs.push(SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 6, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }); | |
| l_secs.push(SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| l_secs, | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "cross_fn".to_owned(), | |
| object: Some("caller.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ | |
| ObjectInput { label: "caller.o".to_owned(), bytes: &obj1 }, | |
| ObjectInput { label: "rodata.o".to_owned(), bytes: &obj2 }, | |
| ], | |
| &manifest, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf parses cross-object fixture") | |
| }; | |
| let r_ok = run_cross(false, false, false); | |
| assert!(r_ok.passed(), "findings: {:?}", r_ok.findings); | |
| assert_eq!(r_ok.rodata_checks.len(), 1); | |
| assert!(run_cross(true, false, false).findings.iter().any(|f| f.code == "RODATA_MISMATCH")); | |
| assert!(run_cross(false, true, false).findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| assert!(run_cross(false, false, true).findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| } | |
| #[test] | |
| fn rejects_dt_symtab_ifunc_plt_addend_static_et_dyn_got_and_spoofed_dynsym_offset() { | |
| // 1. Non-zero r_addend or DT_SYMTAB STT_GNU_IFUNC on external PLT32 | |
| // fails. | |
| let run_plt_case = |plt_addend: i64, ifunc_dynsym: bool, spoof_dynsym_off: bool| { | |
| let obj_text = vec![0xe8, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(1, 2, R_X86_64_PLT32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let call_disp = (0x1020i64 - 4 - 0x1001) as i32; | |
| l_text[1..5].copy_from_slice(&call_disp.to_le_bytes()); | |
| let mut plt = vec![0xff, 0x25, 0, 0, 0, 0]; | |
| let plt_disp = (0x3000i64 - (0x1020 + 6)) as i32; | |
| plt[2..6].copy_from_slice(&plt_disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 6, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: if ifunc_dynsym { STT_GNU_IFUNC } else { STT_FUNC }, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let rela_plt = encode_rela(&[(0x3000, 1, R_X86_64_JUMP_SLOT, plt_addend)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (23, 0x2200), | |
| (2, rela_plt.len() as u64), | |
| (20, 7), | |
| (30, 8), // DT_FLAGS = DF_BIND_NOW | |
| ]); | |
| let dynsym_file_off = if spoof_dynsym_off { 0x2280 } else { 0x2100 }; | |
| let mut linked_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1020, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: plt, | |
| }, | |
| SecSpec { | |
| name: ".got.plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0u8; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 6, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 6, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.plt".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_plt, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ]; | |
| let mut fixed = vec![ | |
| (1, 0x1000), | |
| (2, 0x1020), | |
| (3, 0x3000), | |
| (4, 0x3100), | |
| (5, dynsym_file_off), | |
| (6, 0x2160), | |
| (7, 0x2200), | |
| ]; | |
| if spoof_dynsym_off { | |
| // Keep real DT_SYMTAB bytes mapped at 0x2100 while .dynsym | |
| // sh_offset is spoofed. | |
| let idx = linked_secs.len() + 1; | |
| linked_secs.push(SecSpec { | |
| name: ".real_dynsym".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_sym, | |
| }); | |
| fixed.push((idx, 0x2100)); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| linked_secs, | |
| &fixed, | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "caller".to_owned(), | |
| object: Some("p.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "p.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| assert!(run_plt_case(0, false, false).passed()); | |
| assert!(run_plt_case(16, false, false) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_plt_case(0, true, false) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_plt_case(0, false, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID")); | |
| // 2. Unrelocated static GOT slot passes only in ET_EXEC, never ET_DYN. | |
| let run_static_got = |e_type: u16| -> AuditReport { | |
| let obj_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let rodata = b"STATIC_GOT_RO!".to_vec(); | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "ro_s".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: rodata.len() as u64, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: rodata.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x3000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "ro_s".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: rodata.len() as u64, | |
| }, | |
| ]); | |
| let mut got = 0x2000u64.to_le_bytes().to_vec(); | |
| got.resize(16, 0); | |
| let linked = build_elf( | |
| e_type, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: rodata.len() as u64, | |
| p_memsz: rodata.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 16, | |
| p_memsz: 16, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: rodata, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: got, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000), (3, 0x3000)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("s.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "s.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| assert!(run_static_got(ET_EXEC).passed()); | |
| assert!(run_static_got(ET_DYN).findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| } | |
| #[test] | |
| fn rejects_stt_section_text_reloc_and_out_of_range_self_addend() { | |
| let run_case = |use_stt_section: bool| -> AuditReport { | |
| let mut obj_text = vec![0xe8, 0, 0, 0, 0, 0xc3]; | |
| obj_text.resize(0x41, 0x90); | |
| obj_text[0x40] = 0xc3; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 1, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let r_sym = if use_stt_section { 2 } else { 1 }; | |
| // Both target offset +0x40 (outside foo's [0..6) byte range). | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(1, r_sym, R_X86_64_PLT32, 0x3c)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x1040i64 - 4 - 0x1001) as i32; | |
| l_text[1..5].copy_from_slice(&disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 6, | |
| }]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "foo".to_owned(), | |
| object: Some("sec.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "sec.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| assert!(run_case(true).findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_case(false).findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_duplicate_dt_flags_and_malformed_or_duplicate_gnu_property() { | |
| let obj_text = vec![0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 1, | |
| }]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }]); | |
| let run_case = | |
| |dyn_tags: &[(i64, u64)], prop_bytes: Vec<u8>, prop_align: u64| -> AuditReport { | |
| let dyn_bytes = encode_dyn(dyn_tags); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 1, | |
| p_memsz: 1, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x200, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x200, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_PROPERTY, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: prop_bytes.len() as u64, | |
| p_memsz: prop_bytes.len() as u64, | |
| p_align: prop_align, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".note.gnu.property".to_owned(), | |
| sh_type: SHT_NOTE, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: prop_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str.clone(), | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000), (3, 0x3000)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("d.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single( | |
| &linked, | |
| "d.o", | |
| &obj, | |
| &manifest, | |
| &AuditConfig { require_cet_ibt: true, ..AuditConfig::default() }, | |
| ) | |
| }; | |
| let clean_prop = encode_gnu_property_note( | |
| GNU_PROPERTY_X86_FEATURE_1_AND, | |
| GNU_PROPERTY_X86_FEATURE_1_IBT, | |
| ); | |
| // 0. Baseline clean note with p_align == 8 passes. | |
| assert!(run_case(&[], clean_prop.clone(), 8).passed()); | |
| // 1. Duplicate DT_FLAGS, DT_REL, and DT_ANDROID_RELA fail closed. | |
| let r_dup_flags = run_case(&[(30, 8), (30, 0)], clean_prop.clone(), 8); | |
| assert!(r_dup_flags | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID" && f.message.contains("duplicate"))); | |
| let r_dt_rel = run_case(&[(17, 0x2100), (18, 16), (19, 16)], clean_prop.clone(), 8); | |
| assert!(r_dt_rel | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID" && f.message.contains("DT_REL"))); | |
| let r_android = run_case(&[(0x6000_0011, 0x2100)], clean_prop.clone(), 8); | |
| assert!(r_android | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID" && f.message.contains("packed relocation"))); | |
| // 2. Duplicate NT_GNU_PROPERTY_TYPE_0 notes -> specific parse error. | |
| let mut dup_notes = encode_gnu_property_note(GNU_PROPERTY_X86_FEATURE_1_AND, 0); | |
| dup_notes.extend_from_slice(&clean_prop); | |
| let r_dup_note = run_case(&[], dup_notes, 8); | |
| assert!(r_dup_note.findings.iter().any(|f| { | |
| f.code == "GNU_PROPERTY_PARSE_ERROR" | |
| && f.message.contains("duplicate NT_GNU_PROPERTY_TYPE_0") | |
| })); | |
| // 3. Duplicate pr_type inside one note (12B hdr + 4B name = 16B). | |
| let mut dup_pr = Vec::new(); | |
| dup_pr.extend_from_slice(&4u32.to_le_bytes()); | |
| dup_pr.extend_from_slice(&32u32.to_le_bytes()); | |
| dup_pr.extend_from_slice(&NT_GNU_PROPERTY_TYPE_0.to_le_bytes()); | |
| dup_pr.extend_from_slice(b"GNU\0"); | |
| for bits in [0u32, GNU_PROPERTY_X86_FEATURE_1_IBT] { | |
| dup_pr.extend_from_slice(&GNU_PROPERTY_X86_FEATURE_1_AND.to_le_bytes()); | |
| dup_pr.extend_from_slice(&4u32.to_le_bytes()); | |
| dup_pr.extend_from_slice(&bits.to_le_bytes()); | |
| dup_pr.extend_from_slice(&0u32.to_le_bytes()); | |
| } | |
| let r_dup_pr = run_case(&[], dup_pr, 8); | |
| assert!(r_dup_pr.findings.iter().any(|f| { | |
| f.code == "GNU_PROPERTY_PARSE_ERROR" && f.message.contains("unsorted or duplicate") | |
| })); | |
| // 4. Malformed pr_datasz == 8 -> specific pr_datasz error. | |
| let mut bad_sz_note = Vec::new(); | |
| bad_sz_note.extend_from_slice(&4u32.to_le_bytes()); | |
| bad_sz_note.extend_from_slice(&16u32.to_le_bytes()); | |
| bad_sz_note.extend_from_slice(&NT_GNU_PROPERTY_TYPE_0.to_le_bytes()); | |
| bad_sz_note.extend_from_slice(b"GNU\0"); | |
| bad_sz_note.extend_from_slice(&GNU_PROPERTY_X86_FEATURE_1_AND.to_le_bytes()); | |
| bad_sz_note.extend_from_slice(&8u32.to_le_bytes()); | |
| bad_sz_note.extend_from_slice(&1u64.to_le_bytes()); | |
| let r_bad_sz = run_case(&[], bad_sz_note, 8); | |
| assert!(r_bad_sz | |
| .findings | |
| .iter() | |
| .any(|f| { f.code == "GNU_PROPERTY_PARSE_ERROR" && f.message.contains("pr_datasz") })); | |
| // 5. PT_GNU_PROPERTY with p_align != 8 -> specific alignment error. | |
| let r_bad_align = run_case(&[], clean_prop, 4); | |
| assert!(r_bad_align.findings.iter().any(|f| { | |
| f.code == "GNU_PROPERTY_PARSE_ERROR" && f.message.contains("p_align == 8") | |
| })); | |
| } | |
| #[test] | |
| fn rejects_aarch64_64k_load_overlap_and_incongruent_vaddr_offset() { | |
| let obj_text = 0xd65f_03c0u32.to_le_bytes().to_vec(); | |
| let (o_sym, o_str) = encode_symtab(&[SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 4, | |
| }]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0000, | |
| size: 4, | |
| }]); | |
| let run_case = |rw_vaddr: u64, rw_offset: u64| -> AuditReport { | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1_0000, | |
| p_vaddr: 0x1_0000, | |
| p_filesz: 4, | |
| p_memsz: 4, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: rw_offset, | |
| p_vaddr: rw_vaddr, | |
| p_filesz: 0x10, | |
| p_memsz: 0x10, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".data".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: rw_vaddr, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0u8; 0x10], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str.clone(), | |
| }, | |
| ], | |
| &[(1, 0x1_0000), (2, rw_offset as usize)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f64".to_owned(), | |
| object: Some("wx.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "wx.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| // 1. 0x10000 (RX) and 0x12000 (RW) collide in one 64 KiB AArch64 page. | |
| let r_overlap = run_case(0x1_2000, 0x1_2000); | |
| assert!(r_overlap.findings.iter().any(|f| f.code == "LOAD_SEGMENT_OVERLAP")); | |
| // 2. Disjoint 64 KiB page (0x20000), but incongruent p_vaddr/p_offset. | |
| let r_incong = run_case(0x2_0000, 0x2_0100); | |
| assert!(r_incong.findings.iter().any(|f| f.code == "LOAD_SEGMENT_INVALID")); | |
| } | |
| #[test] | |
| fn rejects_verified_dynsym_drift_interposable_got_and_truncated_dynsym() { | |
| // `f` @ 0x1000 loads defined in-image `callee` @ 0x1020 via GOTPCREL. | |
| let mut obj_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| obj_text.resize(0x21, 0x90); | |
| obj_text[0x20] = 0xc3; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "callee".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x20, | |
| size: 1, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x3000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "callee".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1020, | |
| size: 1, | |
| }, | |
| ]); | |
| let run_case = |mark_pie: bool, | |
| use_relative: bool, | |
| f_dyn_va: u64, | |
| f_dyn_ifunc: bool, | |
| truncate_dynsym: bool, | |
| add_dt_hash: bool| | |
| -> AuditReport { | |
| let (d_sym_full, d_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: if f_dyn_ifunc { STT_GNU_IFUNC } else { STT_FUNC }, | |
| shndx: 1, | |
| value: f_dyn_va, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "callee".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1020, | |
| size: 1, | |
| }, | |
| ]); | |
| let d_sym_sec = | |
| if truncate_dynsym { d_sym_full[..24].to_vec() } else { d_sym_full.clone() }; | |
| // DT_HASH with nbucket=1, nchain=3 (STN_UNDEF + f + callee). | |
| let mut hash_bytes = Vec::new(); | |
| for w in [1u32, 3, 1, 0, 2, 0] { | |
| hash_bytes.extend_from_slice(&w.to_le_bytes()); | |
| } | |
| let rela_dyn = if use_relative { | |
| encode_rela(&[(0x3000, 0, R_X86_64_RELATIVE, 0x1020)]) | |
| } else { | |
| encode_rela(&[(0x3000, 2, R_X86_64_GLOB_DAT, 0)]) | |
| }; | |
| let mut dyn_tags = vec![ | |
| (5, 0x2180), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| ]; | |
| if mark_pie { | |
| dyn_tags.push((0x6fff_fffb, 0x0800_0000)); // DT_FLAGS_1 = DF_1_PIE | |
| } | |
| if add_dt_hash { | |
| dyn_tags.push((4, 0x2280)); // DT_HASH | |
| } | |
| let dyn_bytes = encode_dyn(&dyn_tags); | |
| let mut l_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0u8; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym_sec, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2180, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".hash".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2280, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 4, | |
| data: hash_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str.clone(), | |
| }, | |
| ]; | |
| let mut fixed = vec![ | |
| (1, 0x1000), | |
| (2, 0x3000), | |
| (3, 0x3100), | |
| (4, 0x2100), | |
| (5, 0x2180), | |
| (6, 0x2200), | |
| (7, 0x2280), | |
| ]; | |
| if truncate_dynsym { | |
| // Keep full DT_SYMTAB bytes mapped at 0x2118..0x2148. | |
| let idx = l_secs.len() + 1; | |
| l_secs.push(SecSpec { | |
| name: ".hidden_dynsym_tail".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2118, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_sym_full[24..].to_vec(), | |
| }); | |
| fixed.push((idx, 0x2118)); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| l_secs, | |
| &fixed, | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("d.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "d.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| // 1. PIE with local R_X86_64_RELATIVE binding, matching .dynsym, and | |
| // DT_HASH passes. | |
| assert!(run_case(true, true, 0x1000, false, false, true).passed()); | |
| // 2. Defined in-image R_X86_64_GLOB_DAT is rejected fail-closed in | |
| // both PIE (DF_1_PIE) and shared ET_DYN images. | |
| assert!(run_case(true, false, 0x1000, false, false, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| assert!(run_case(false, false, 0x1000, false, false, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| // 3. Verified symbol `.dynsym` VA drift and STT_GNU_IFUNC fail closed. | |
| assert!(run_case(true, true, 0x1020, false, false, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNSYM_MISMATCH")); | |
| assert!(run_case(true, true, 0x1000, true, false, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "SYMBOL_IFUNC_FORBIDDEN")); | |
| // 4. Truncated SHT_DYNSYM caught both by DT_HASH nchain and r_sym OOB. | |
| assert!(run_case(true, false, 0x1000, false, true, true) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID" && f.message.contains("DT_HASH"))); | |
| assert!( | |
| run_case(true, false, 0x1000, false, true, false) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID" | |
| && f.message.contains("exceeds SHT_DYNSYM")) | |
| ); | |
| } | |
| #[test] | |
| fn rejects_plt_defined_dynsym_ifunc_fallthrough_nonzero_addend_and_dt_hash_oob() { | |
| // 1. PLT32 guard checks: defined/ABS dynsym, .symtab IFUNC fallthrough, | |
| // and non-zero semantic addend (r_addend != pc_bias) all fail closed. | |
| let run_plt_guard = | |
| |dynsym_shndx: u16, add_symtab_ifunc: bool, rela_addend: i64| -> AuditReport { | |
| let obj_text = vec![0xe8, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(1, 2, R_X86_64_PLT32, rela_addend)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let call_disp = (0x1020i64 + rela_addend - 0x1001) as i32; | |
| l_text[1..5].copy_from_slice(&call_disp.to_le_bytes()); | |
| let mut plt = vec![0xff, 0x25, 0, 0, 0, 0]; | |
| let plt_disp = (0x3000i64 - (0x1020 + 6)) as i32; | |
| plt[2..6].copy_from_slice(&plt_disp.to_le_bytes()); | |
| let mut l_syms = vec![SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 6, | |
| }]; | |
| if add_symtab_ifunc { | |
| l_syms.push(SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_GNU_IFUNC, | |
| shndx: 1, | |
| value: 0x1010, | |
| size: 4, | |
| }); | |
| } | |
| let (l_sym, l_str) = encode_symtab(&l_syms); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: dynsym_shndx, | |
| value: if dynsym_shndx == 0 { 0 } else { 0x1010 }, | |
| size: 0, | |
| }]); | |
| let rela_plt = encode_rela(&[(0x3000, 1, R_X86_64_JUMP_SLOT, 0)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (23, 0x2200), | |
| (2, rela_plt.len() as u64), | |
| (20, 7), | |
| (30, 8), | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1020, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: plt, | |
| }, | |
| SecSpec { | |
| name: ".got.plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0u8; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 6, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 6, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.plt".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_plt, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[ | |
| (1, 0x1000), | |
| (2, 0x1020), | |
| (3, 0x3000), | |
| (4, 0x3100), | |
| (5, 0x2100), | |
| (6, 0x2160), | |
| (7, 0x2200), | |
| ], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "caller".to_owned(), | |
| object: Some("p.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "p.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| assert!(run_plt_guard(0, false, -4).passed()); | |
| // Defined section shndx (1) and SHN_ABS (0xfff1) fail closed. | |
| assert!(run_plt_guard(1, false, -4) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_plt_guard(0xfff1, false, -4) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // .symtab STT_GNU_IFUNC cannot fall through to external PLT. | |
| assert!(run_plt_guard(0, true, -4) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // Non-zero semantic addend (r_addend = 4 != -4) fails closed. | |
| assert!(run_plt_guard(0, false, 4) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // 2. DT_HASH with nchain=1, dynsym_count=1, bucket[0]=1 fails closed. | |
| let obj_text = vec![0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[SymSpec { | |
| name: "h".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 1, | |
| }]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "h".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }]); | |
| let (d_sym_full, d_str) = encode_symtab(&[SymSpec { | |
| name: "hidden_alias".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }]); | |
| // Truncate SHT_DYNSYM to 1 entry, set DT_HASH bucket[0] = 1 (OOB!). | |
| let mut bad_hash = Vec::new(); | |
| for w in [1u32, 1, 1, 0] { | |
| bad_hash.extend_from_slice(&w.to_le_bytes()); | |
| } | |
| let dyn_bytes = encode_dyn(&[ | |
| (4, 0x2200), | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 1, | |
| p_memsz: 1, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x100, | |
| p_memsz: 0x100, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym_full[..24].to_vec(), | |
| }, | |
| SecSpec { | |
| name: ".hidden_tail".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2118, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_sym_full[24..].to_vec(), | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".hash".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 2, | |
| sh_info: 0, | |
| sh_entsize: 4, | |
| data: bad_hash, | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 8, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2100), (3, 0x2118), (4, 0x2160), (5, 0x2200), (6, 0x3000)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "h".to_owned(), | |
| object: Some("h.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| let r = run_single(&linked, "h.o", &obj, &manifest, &AuditConfig::default()); | |
| assert!(r | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID" && f.message.contains("DT_HASH entry 1"))); | |
| } | |
| #[test] | |
| fn negative_controls_cover_remaining_finding_codes_and_x86_mapping_alias() { | |
| let (clean_x86, obj_x86, manifest_x86) = build_x86_fixture(&X86FixtureOpts::default()); | |
| let (_clean_a64, obj_a64, _manifest_a64) = | |
| build_aarch64_fixture(&AArch64FixtureOpts::default()); | |
| // 1. MANIFEST_INVALID (bad schema_version and empty symbols). | |
| let bad_ver = Manifest { schema_version: 99, symbols: manifest_x86.symbols.clone() }; | |
| assert!(run_single(&clean_x86, "unit.o", &obj_x86, &bad_ver, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "MANIFEST_INVALID")); | |
| let empty_m = Manifest { schema_version: 1, symbols: vec![] }; | |
| assert!(run_single(&clean_x86, "unit.o", &obj_x86, &empty_m, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "MANIFEST_INVALID")); | |
| // 2. OBJECT_ARCH_MISMATCH (AArch64 .o against x86-64 linked binary). | |
| assert!(run_single(&clean_x86, "a64.o", &obj_a64, &manifest_x86, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "OBJECT_ARCH_MISMATCH")); | |
| // 3. SYMBOL_MISSING_IN_OBJECT and SYMBOL_AMBIGUOUS_IN_OBJECT. | |
| let missing_m = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "no_such_fn".to_owned(), | |
| object: Some("unit.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| assert!(run_single(&clean_x86, "unit.o", &obj_x86, &missing_m, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "SYMBOL_MISSING_IN_OBJECT")); | |
| let ambig_m = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "foo".to_owned(), | |
| object: None, | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| let r_ambig = audit_elf( | |
| &clean_x86, | |
| &[ | |
| ObjectInput { label: "a.o".to_owned(), bytes: &obj_x86 }, | |
| ObjectInput { label: "b.o".to_owned(), bytes: &obj_x86 }, | |
| ], | |
| &ambig_m, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("parses ambiguous inputs"); | |
| assert!(r_ambig.findings.iter().any(|f| f.code == "SYMBOL_AMBIGUOUS_IN_OBJECT")); | |
| // 4. Minimal x86-64 negative scenarios for remaining finding codes. | |
| let make_min_obj = |obj_sym_size: u64, | |
| relas: &[(u64, u32, u32, i64)], | |
| extra_rel_sec: Option<u32>| | |
| -> Vec<u8> { | |
| let text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let ro = b"RO_CHECK".to_vec(); | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: obj_sym_size, | |
| }, | |
| SymSpec { | |
| name: "k".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: ro.len() as u64, | |
| }, | |
| ]); | |
| let mut secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(relas), | |
| }, | |
| ]; | |
| if let Some(sh_type) = extra_rel_sec { | |
| secs.push(SecSpec { | |
| name: ".rel.text".to_owned(), | |
| sh_type, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: if sh_type == SHT_REL { 16 } else { 0 }, | |
| data: vec![0u8; 16], | |
| }); | |
| } | |
| build_elf(ET_REL, EM_X86_64, &[], secs, &[]) | |
| }; | |
| let make_min_linked = |text_flags: u32, | |
| ro_flags: u32, | |
| linked_sym_size: Option<u64>, | |
| dup_gnu_stack: bool, | |
| bad_stack_sizes: bool, | |
| add_x86_dollar_x: bool| | |
| -> Vec<u8> { | |
| let mut text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let disp = (0x2000i64 - 4 - 0x1003) as i32; | |
| text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let ro = b"RO_CHECK".to_vec(); | |
| let mut lsyms = vec![SymSpec { | |
| name: "k".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: ro.len() as u64, | |
| }]; | |
| if let Some(sz) = linked_sym_size { | |
| lsyms.push(SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: sz, | |
| }); | |
| } | |
| if add_x86_dollar_x { | |
| lsyms.push(SymSpec { | |
| name: "$x".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_NOTYPE, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 0, | |
| }); | |
| } | |
| let (ls, lstr) = encode_symtab(&lsyms); | |
| let mut phdrs = vec![ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: text_flags, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: ro_flags, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ]; | |
| if dup_gnu_stack { | |
| phdrs.push(PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }); | |
| } | |
| let mut secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ]; | |
| if bad_stack_sizes { | |
| secs.push(SecSpec { | |
| name: ".stack_sizes".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0x01, 0x02, 0x03], | |
| }); | |
| } | |
| build_elf(ET_DYN, EM_X86_64, &phdrs, secs, &[(1, 0x1000), (2, 0x2000)]) | |
| }; | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("m.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| let ok_obj = make_min_obj(8, &[(3, 2, R_X86_64_PC32, -4)], None); | |
| let ok_linked = make_min_linked(PF_R | PF_X, PF_R, Some(8), false, false, false); | |
| assert!(run_single(&ok_linked, "m.o", &ok_obj, &mf, &AuditConfig::default()).passed()); | |
| // RELOC_UNALLOWLISTED (x86-64 R_X86_64_32S = 11). | |
| let unallow_obj = make_min_obj(8, &[(3, 2, 11, -4)], None); | |
| assert!(run_single(&ok_linked, "m.o", &unallow_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "RELOC_UNALLOWLISTED")); | |
| // RELOC_OUT_OF_BOUNDS (patch window crosses 8-byte end). | |
| let oob_obj = make_min_obj(8, &[(6, 2, R_X86_64_PC32, -4)], None); | |
| assert!(run_single(&ok_linked, "m.o", &oob_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "RELOC_OUT_OF_BOUNDS")); | |
| // RELOC_OVERLAP (two overlapping 4B patch windows). | |
| let ov_obj = make_min_obj(8, &[(2, 2, R_X86_64_PC32, -4), (3, 2, R_X86_64_PC32, -4)], None); | |
| assert!(run_single(&ok_linked, "m.o", &ov_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "RELOC_OVERLAP")); | |
| // UNSUPPORTED_RELOC_SECTION (both SHT_REL and SHT_CREL). | |
| let rel_obj = make_min_obj(8, &[(3, 2, R_X86_64_PC32, -4)], Some(SHT_REL)); | |
| assert!(run_single(&ok_linked, "m.o", &rel_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "UNSUPPORTED_RELOC_SECTION")); | |
| let crel_obj = make_min_obj(8, &[(3, 2, R_X86_64_PC32, -4)], Some(SHT_CREL)); | |
| assert!(run_single(&ok_linked, "m.o", &crel_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "UNSUPPORTED_RELOC_SECTION")); | |
| // SYMBOL_INVALID_IN_OBJECT (zero-size symbol in .o). | |
| let zero_obj = make_min_obj(0, &[], None); | |
| assert!(run_single(&ok_linked, "m.o", &zero_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "SYMBOL_INVALID_IN_OBJECT")); | |
| // SYMBOL_MISSING_IN_LINKED & SYMBOL_SIZE_MISMATCH. | |
| let no_sym_linked = make_min_linked(PF_R | PF_X, PF_R, None, false, false, false); | |
| assert!(run_single(&no_sym_linked, "m.o", &ok_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "SYMBOL_MISSING_IN_LINKED")); | |
| let bad_sz_linked = make_min_linked(PF_R | PF_X, PF_R, Some(16), false, false, false); | |
| assert!(run_single(&bad_sz_linked, "m.o", &ok_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "SYMBOL_SIZE_MISMATCH")); | |
| // SYMBOL_NOT_RX (non-exec text segment) and RODATA_NOT_READONLY. | |
| let non_rx = make_min_linked(PF_R, PF_R, Some(8), false, false, false); | |
| assert!(run_single(&non_rx, "m.o", &ok_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "SYMBOL_NOT_RX")); | |
| let rw_ro = make_min_linked(PF_R | PF_X, PF_R | PF_W, Some(8), false, false, false); | |
| assert!(run_single(&rw_ro, "m.o", &ok_obj, &mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "RODATA_NOT_READONLY")); | |
| // RELRO_MISSING, STACK_SIZES_MISSING, STACK_SIZES_PARSE_ERROR, | |
| // GNU_STACK_DUPLICATE, and x86 `$x` ICF_ALIASING. | |
| let req_cfg = AuditConfig { | |
| require_relro: true, | |
| max_stack_bytes: Some(64), | |
| ..AuditConfig::default() | |
| }; | |
| let r_req = run_single(&ok_linked, "m.o", &ok_obj, &mf, &req_cfg); | |
| assert!(r_req.findings.iter().any(|f| f.code == "RELRO_MISSING")); | |
| assert!(r_req.findings.iter().any(|f| f.code == "STACK_SIZES_MISSING")); | |
| let bad_ss_linked = make_min_linked(PF_R | PF_X, PF_R, Some(8), true, true, true); | |
| let r_ss = run_single(&bad_ss_linked, "m.o", &ok_obj, &mf, &AuditConfig::default()); | |
| assert!(r_ss.findings.iter().any(|f| f.code == "GNU_STACK_DUPLICATE")); | |
| assert!(r_ss.findings.iter().any(|f| f.code == "STACK_SIZES_PARSE_ERROR")); | |
| assert!(r_ss.findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| // 5. AArch64 RELOC_UNALLOWLISTED (R_AARCH64_ABS64 = 257). | |
| let a64_code = 0xd65f_03c0u32.to_le_bytes().to_vec(); | |
| let (a_os, a_ostr) = encode_symtab(&[SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 4, | |
| }]); | |
| let a64_obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_code.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: a_os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a_ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(0, 1, 257, 0)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (a_ls, a_lstr) = encode_symtab(&[SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0000, | |
| size: 4, | |
| }]); | |
| let a64_linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1_0000, | |
| p_vaddr: 0x1_0000, | |
| p_filesz: 4, | |
| p_memsz: 4, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_code, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: a_ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a_lstr, | |
| }, | |
| ], | |
| &[(1, 0x1_0000)], | |
| ); | |
| let a64_mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f64".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| assert!(run_single(&a64_linked, "a.o", &a64_obj, &a64_mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "RELOC_UNALLOWLISTED")); | |
| } | |
| #[test] | |
| fn rejects_glob_dat_shn_abs_xindex_and_ifunc_fallthrough() { | |
| #[derive(Clone, Copy)] | |
| enum Case { | |
| ValidExternal, | |
| DynsymShnAbs, | |
| DynsymShnXindex, | |
| SymtabIfuncFallthrough, | |
| } | |
| let run_case = |case: Case| -> AuditReport { | |
| let obj_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "ext_sym".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x3000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let mut l_syms = vec![SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }]; | |
| if matches!(case, Case::SymtabIfuncFallthrough) { | |
| l_syms.push(SymSpec { | |
| name: "ext_sym".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_GNU_IFUNC, | |
| shndx: 1, | |
| value: 0x1020, | |
| size: 4, | |
| }); | |
| } | |
| let (l_sym, l_str) = encode_symtab(&l_syms); | |
| let dsym_shndx = match case { | |
| Case::ValidExternal | Case::SymtabIfuncFallthrough => 0, | |
| Case::DynsymShnAbs => 0xfff1, | |
| Case::DynsymShnXindex => SHN_XINDEX, | |
| }; | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "ext_sym".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: dsym_shndx, | |
| value: if dsym_shndx == 0 { 0 } else { 0x1020 }, | |
| size: 0, | |
| }]); | |
| let rela_dyn = encode_rela(&[(0x3000, 1, R_X86_64_GLOB_DAT, 0)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| ]); | |
| let mut secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 8, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ]; | |
| if matches!(case, Case::DynsymShnXindex) { | |
| let mut shndx_tab = Vec::new(); | |
| shndx_tab.extend_from_slice(&0u32.to_le_bytes()); | |
| shndx_tab.extend_from_slice(&1u32.to_le_bytes()); | |
| secs.push(SecSpec { | |
| name: ".dynsym_shndx".to_owned(), | |
| sh_type: SHT_SYMTAB_SHNDX, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 4, | |
| data: shndx_tab, | |
| }); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: 128, | |
| p_memsz: 128, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| secs, | |
| &[(1, 0x1000), (2, 0x3000), (3, 0x3100), (4, 0x2100), (5, 0x2160), (6, 0x2200)], | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("g.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "g.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| let r_ok = run_case(Case::ValidExternal); | |
| assert!(r_ok.passed(), "valid external GLOB_DAT failed: {:?}", r_ok.findings); | |
| assert!(run_case(Case::DynsymShnAbs) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| assert!(run_case(Case::DynsymShnXindex) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| assert!(run_case(Case::SymtabIfuncFallthrough) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| } | |
| #[test] | |
| fn rejects_writable_dynamic_tables_and_reloc_overlapping_pt_dynamic() { | |
| #[derive(Clone, Copy)] | |
| enum Case { | |
| BaselineRo, | |
| WritableLoaderTables, | |
| WritableJmprel, | |
| WritableRelr, | |
| RelocMutatesPtDynamic, | |
| } | |
| let run_case = |case: Case| -> AuditReport { | |
| let text = vec![0x90, 0x90, 0x90, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 4, | |
| }]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 4, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let rela_target = | |
| if matches!(case, Case::RelocMutatesPtDynamic) { 0x3100 } else { 0x3000 }; | |
| let rela_dyn = encode_rela(&[(rela_target, 0, R_X86_64_RELATIVE, 0x1000)]); | |
| let jmprel_bytes = encode_rela(&[(0x3010, 1, R_X86_64_JUMP_SLOT, 0)]); | |
| let relr_bytes = 0x3000u64.to_le_bytes().to_vec(); | |
| let meta_flags = | |
| if matches!(case, Case::WritableLoaderTables) { PF_R | PF_W } else { PF_R }; | |
| let mut dyn_tags = vec![ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| ]; | |
| let extra_va = match case { | |
| Case::WritableJmprel => { | |
| dyn_tags.push((23, 0x3200)); // DT_JMPREL in RW segment | |
| dyn_tags.push((2, jmprel_bytes.len() as u64)); // DT_PLTRELSZ | |
| dyn_tags.push((20, 7)); // DT_PLTREL = DT_RELA | |
| Some((0x3200u64, SHT_RELA, 24u64, jmprel_bytes)) | |
| } | |
| Case::WritableRelr => { | |
| dyn_tags.push((36, 0x3200)); // DT_RELR in RW segment | |
| dyn_tags.push((35, 8)); // DT_RELRSZ | |
| dyn_tags.push((37, 8)); // DT_RELRENT | |
| Some((0x3200u64, SHT_RELR, 8u64, relr_bytes)) | |
| } | |
| _ => None, | |
| }; | |
| let dyn_bytes = encode_dyn(&dyn_tags); | |
| let mut secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text, | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 3, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 7, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ]; | |
| let mut placements: Vec<(usize, usize)> = | |
| vec![(1, 0x1000), (2, 0x3100), (3, 0x2100), (4, 0x2160), (5, 0x2200)]; | |
| if let Some((va, ty, ent, data)) = extra_va { | |
| secs.push(SecSpec { | |
| name: ".extra_rel".to_owned(), | |
| sh_type: ty, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: va, | |
| sh_link: if ty == SHT_RELA { 3 } else { 0 }, | |
| sh_info: 0, | |
| sh_entsize: ent, | |
| data, | |
| }); | |
| placements.push((8, va as usize)); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: meta_flags, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| secs, | |
| &placements, | |
| ); | |
| let manifest = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("d.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "d.o", &obj, &manifest, &AuditConfig::default()) | |
| }; | |
| let r_ok = run_case(Case::BaselineRo); | |
| assert!(r_ok.passed(), "RO loader tables baseline failed: {:?}", r_ok.findings); | |
| for bad in [Case::WritableLoaderTables, Case::WritableJmprel, Case::WritableRelr] { | |
| let r = run_case(bad); | |
| assert!( | |
| r.findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID" && f.message.contains("non-RO")), | |
| "expected non-RO DYNAMIC_TABLE_INVALID, got: {:?}", | |
| r.findings | |
| ); | |
| } | |
| let r_dyn = run_case(Case::RelocMutatesPtDynamic); | |
| assert!( | |
| r_dyn.findings.iter().any(|f| f.code == "DYNAMIC_RELOC_MUTATES_VERIFIED"), | |
| "expected DYNAMIC_RELOC_MUTATES_VERIFIED on PT_DYNAMIC overlap, got: {:?}", | |
| r_dyn.findings | |
| ); | |
| } | |
| #[test] | |
| fn rejects_duplicate_and_empty_manifest_symbols() { | |
| let text = vec![0x90, 0x90, 0x90, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 4, | |
| }]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 4, | |
| }]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 4, | |
| p_memsz: 4, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000)], | |
| ); | |
| let dup_mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ | |
| ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("m.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("m.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ], | |
| }; | |
| assert!(run_single(&linked, "m.o", &obj, &dup_mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "MANIFEST_INVALID" && f.message.contains("duplicate"))); | |
| let empty_mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: String::new(), | |
| object: Some("m.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| assert!(run_single(&linked, "m.o", &obj, &empty_mf, &AuditConfig::default()) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "MANIFEST_INVALID" && f.message.contains("empty"))); | |
| } | |
| #[test] | |
| fn enforces_strict_aaelf64_mapping_symbol_rules() { | |
| let a64_code = 0xd65f_03c0u32.to_le_bytes().to_vec(); | |
| let (a_os, a_ostr) = encode_symtab(&[SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 4, | |
| }]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_code.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: a_os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a_ostr, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let run_case = | |
| |alias_name: &str, bind: u8, sym_type: u8, size: u64, in_dynsym: bool| -> AuditReport { | |
| let mut lsyms = vec![SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0000, | |
| size: 4, | |
| }]; | |
| let alias = SymSpec { | |
| name: alias_name.to_owned(), | |
| bind, | |
| sym_type, | |
| shndx: 1, | |
| value: 0x1_0000, | |
| size, | |
| }; | |
| let dynsym_alias = if in_dynsym { | |
| Some(alias) | |
| } else { | |
| lsyms.push(alias); | |
| None | |
| }; | |
| let (ls, lstr) = encode_symtab(&lsyms); | |
| let mut secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_code.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ]; | |
| if let Some(d_alias) = dynsym_alias { | |
| let (ds, dstr) = encode_symtab(&[d_alias]); | |
| secs.push(SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2_0000, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ds, | |
| }); | |
| secs.push(SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2_0100, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: dstr, | |
| }); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1_0000, | |
| p_vaddr: 0x1_0000, | |
| p_filesz: 4, | |
| p_memsz: 4, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| secs, | |
| &[(1, 0x1_0000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f64".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "a.o", &obj, &mf, &AuditConfig::default()) | |
| }; | |
| // Legitimate AAELF64 local zero-size STT_NOTYPE `$x` / `$d.0` pass. | |
| assert!(run_case("$x", STB_LOCAL, STT_NOTYPE, 0, false).passed()); | |
| assert!(run_case("$d.0", STB_LOCAL, STT_NOTYPE, 0, false).passed()); | |
| // Non-local, typed, non-zero-size, ARM32 `$a`, or `.dynsym` `$x` fail. | |
| for (name, bind, ty, sz, dynsym) in [ | |
| ("$x", STB_GLOBAL, STT_NOTYPE, 0, false), | |
| ("$x", STB_LOCAL, STT_FUNC, 0, false), | |
| ("$x", STB_LOCAL, STT_NOTYPE, 4, false), | |
| ("$a", STB_LOCAL, STT_NOTYPE, 0, false), | |
| ("$x", STB_LOCAL, STT_NOTYPE, 0, true), | |
| ] { | |
| let r = run_case(name, bind, ty, sz, dynsym); | |
| assert!( | |
| r.findings.iter().any(|f| f.code == "ICF_ALIASING"), | |
| "expected ICF_ALIASING for ({name}, bind={bind}, ty={ty}, sz={sz}, \ | |
| dynsym={dynsym}), got: {:?}", | |
| r.findings | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn rejects_android_relr_dynamic_tags() { | |
| let text = vec![0x90, 0x90, 0x90, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 4, | |
| }]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 4, | |
| }]); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("ar.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| // DT_ANDROID_RELR*, including DT_ANDROID_RELRCOUNT | |
| // (0x6fffe000, 0x6fffe001, 0x6fffe003, 0x6fffe005). | |
| for tag in [0x6fff_e000i64, 0x6fff_e001, 0x6fff_e003, 0x6fff_e005] { | |
| let dyn_bytes = encode_dyn(&[(tag, 0x2000)]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 4, | |
| p_memsz: 4, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x200, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str.clone(), | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x3100)], | |
| ); | |
| let r = run_single(&linked, "ar.o", &obj, &mf, &AuditConfig::default()); | |
| assert!( | |
| r.findings.iter().any(|f| { | |
| f.code == "DYNAMIC_TABLE_INVALID" && f.message.contains("packed relocation") | |
| }), | |
| "expected packed-reloc DYNAMIC_TABLE_INVALID for tag 0x{tag:x}, got: {:?}", | |
| r.findings | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn enforces_relro_on_plt_dispatch_slots_and_got_plt() { | |
| #[derive(Clone, Copy)] | |
| enum Case { | |
| FullRelroOk, | |
| GotPltOutsideRelro, | |
| DataSlotOutsideGot, | |
| } | |
| let run_case = |case: Case| -> AuditReport { | |
| let obj_text = vec![0xe8, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(1, 2, R_X86_64_PLT32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let call_disp = (0x1020i64 - 4 - 0x1001) as i32; | |
| l_text[1..5].copy_from_slice(&call_disp.to_le_bytes()); | |
| let slot_va: u64 = match case { | |
| Case::FullRelroOk => 0x3010, | |
| Case::GotPltOutsideRelro | Case::DataSlotOutsideGot => 0x4000, | |
| }; | |
| let mut plt = vec![0xff, 0x25, 0, 0, 0, 0]; | |
| let plt_disp = (slot_va as i64 - 0x1026) as i32; | |
| plt[2..6].copy_from_slice(&plt_disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 6, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let reloc_type = if matches!(case, Case::DataSlotOutsideGot) { | |
| R_X86_64_GLOB_DAT | |
| } else { | |
| R_X86_64_JUMP_SLOT | |
| }; | |
| let rela_dyn = encode_rela(&[(slot_va, 1, reloc_type, 0)]); | |
| let mut dyn_tags = vec![ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| ]; | |
| if !matches!(case, Case::DataSlotOutsideGot) { | |
| dyn_tags.push((24, 0)); // DT_BIND_NOW | |
| } | |
| let dyn_bytes = encode_dyn(&dyn_tags); | |
| let slot_sec_name = | |
| if matches!(case, Case::DataSlotOutsideGot) { ".data" } else { ".got.plt" }; | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: if matches!(case, Case::FullRelroOk) { 0x200 } else { 0x1010 }, | |
| p_memsz: 0x1100, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_RELRO, | |
| p_flags: PF_R, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x1000, | |
| p_memsz: 0x1000, | |
| p_align: 1, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1020, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: plt, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0; 16], | |
| }, | |
| SecSpec { | |
| name: slot_sec_name.to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: slot_va, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 7, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 7, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 6, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 10, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[ | |
| (1, 0x1000), | |
| (2, 0x1020), | |
| (3, 0x3000), | |
| (4, slot_va as usize), | |
| (5, 0x3100), | |
| (6, 0x2100), | |
| (7, 0x2160), | |
| (8, 0x2200), | |
| ], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "caller".to_owned(), | |
| object: Some("p.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single( | |
| &linked, | |
| "p.o", | |
| &obj, | |
| &mf, | |
| &AuditConfig { require_relro: true, ..AuditConfig::default() }, | |
| ) | |
| }; | |
| let r_ok = run_case(Case::FullRelroOk); | |
| assert!(r_ok.passed(), "full-RELRO PLT failed: {:?}", r_ok.findings); | |
| let r_uncov = run_case(Case::GotPltOutsideRelro); | |
| assert!(r_uncov.findings.iter().any(|f| f.code == "RELRO_GOT_UNCOVERED")); | |
| assert!(r_uncov.findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| let r_data = run_case(Case::DataSlotOutsideGot); | |
| assert!(r_data.findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_x86_got_addend_mismatch_and_non_rip_rel_instruction() { | |
| let run_case = |obj_text: Vec<u8>, rel_off: u64, addend: i64| -> AuditReport { | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: obj_text.len() as u64, | |
| }, | |
| SymSpec { | |
| name: "ext_sym".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(rel_off, 2, R_X86_64_REX_GOTPCRELX, addend)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x3000i64 + addend - (0x1000i64 + rel_off as i64)) as i32; | |
| let ro = rel_off as usize; | |
| l_text[ro..ro + 4].copy_from_slice(&disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: l_text.len() as u64, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "ext_sym".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let rela_dyn = encode_rela(&[(0x3000, 1, R_X86_64_GLOB_DAT, 0)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 8, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x3000), (3, 0x3100), (4, 0x2100), (5, 0x2160), (6, 0x2200)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("ga.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "ga.o", &obj, &mf, &AuditConfig::default()) | |
| }; | |
| // Addend +4 disagrees with RIP-relative pc_bias -4. | |
| let r_addend = run_case(vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3], 3, 4); | |
| assert!(r_addend | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID" && f.message.contains("pc_bias"))); | |
| // Non-RIP-relative `mov eax, imm32; ret` fails closed. | |
| let r_non_rip = run_case(vec![0xb8, 0, 0, 0, 0, 0xc3], 1, -4); | |
| assert!(r_non_rip | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID" && f.message.contains("RIP-relative"))); | |
| } | |
| #[test] | |
| fn rejects_aarch64_add_abs_lo12_non_unshifted_add64_opcode() { | |
| let ro = b"A64_RO!".to_vec(); | |
| let adrp_obj = 0x9000_0000u32; | |
| let ret_insn = 0xd65f_03c0u32; | |
| // Linked ADRP from 0x10000 to page 0x20000 => imm21 = 16 (0x10). | |
| let adrp_linked = 0x9000_0080u32; | |
| // Shifted ADD (`lsl #12`, sh=1), 32-bit ADD W0, and SUB X0 must fail. | |
| for bad_add in [0x9140_0000u32, 0x1100_0000u32, 0xd100_0000u32] { | |
| let mut obj_code = Vec::new(); | |
| obj_code.extend_from_slice(&adrp_obj.to_le_bytes()); | |
| obj_code.extend_from_slice(&bad_add.to_le_bytes()); | |
| obj_code.extend_from_slice(&ret_insn.to_le_bytes()); | |
| let mut linked_code = Vec::new(); | |
| linked_code.extend_from_slice(&adrp_linked.to_le_bytes()); | |
| linked_code.extend_from_slice(&bad_add.to_le_bytes()); | |
| linked_code.extend_from_slice(&ret_insn.to_le_bytes()); | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 12, | |
| }, | |
| SymSpec { | |
| name: "k64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: ro.len() as u64, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_code, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[ | |
| (0, 2, R_AARCH64_ADR_PREL_PG_HI21, 0), | |
| (4, 2, R_AARCH64_ADD_ABS_LO12_NC, 0), | |
| ]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0000, | |
| size: 12, | |
| }, | |
| SymSpec { | |
| name: "k64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2_0000, | |
| size: ro.len() as u64, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1_0000, | |
| p_vaddr: 0x1_0000, | |
| p_filesz: 12, | |
| p_memsz: 12, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2_0000, | |
| p_vaddr: 0x2_0000, | |
| p_filesz: ro.len() as u64, | |
| p_memsz: ro.len() as u64, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: linked_code, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1_0000), (2, 0x2_0000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f64".to_owned(), | |
| object: Some("a64.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| let r = run_single(&linked, "a64.o", &obj, &mf, &AuditConfig::default()); | |
| assert!( | |
| r.findings.iter().any(|f| f.code == "AARCH64_OPCODE_MODIFIED"), | |
| "expected AARCH64_OPCODE_MODIFIED for 0x{bad_add:08x}, got: {:?}", | |
| r.findings | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn accepts_cet_and_bnd_plt_stubs_at_end_of_rx_segment() { | |
| // Test 10-byte `endbr64; jmpq` and 7-byte `bnd jmpq` stubs terminating | |
| // at the exact end of the RX PT_LOAD (`p_filesz`). | |
| for prefix in [&[0xf3u8, 0x0f, 0x1e, 0xfa][..], &[0xf2u8][..]] { | |
| let obj_text = vec![0xe8, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(1, 2, R_X86_64_PLT32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let call_disp = (0x1020i64 - 4 - 0x1001) as i32; | |
| l_text[1..5].copy_from_slice(&call_disp.to_le_bytes()); | |
| let mut plt = prefix.to_vec(); | |
| let stub_len = (prefix.len() + 6) as u64; | |
| let plt_disp = (0x3000i64 - (0x1020i64 + stub_len as i64)) as i32; | |
| plt.extend_from_slice(&[0xff, 0x25]); | |
| plt.extend_from_slice(&plt_disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 6, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let rela_dyn = encode_rela(&[(0x3000, 1, R_X86_64_JUMP_SLOT, 0)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| (24, 0), | |
| ]); | |
| let rx_filesz = 0x20 + stub_len; | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: rx_filesz, | |
| p_memsz: rx_filesz, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".plt.sec".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1020, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: plt, | |
| }, | |
| SecSpec { | |
| name: ".got.plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 6, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 6, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[ | |
| (1, 0x1000), | |
| (2, 0x1020), | |
| (3, 0x3000), | |
| (4, 0x3100), | |
| (5, 0x2100), | |
| (6, 0x2160), | |
| (7, 0x2200), | |
| ], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "caller".to_owned(), | |
| object: Some("c.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| let r = run_single(&linked, "c.o", &obj, &mf, &AuditConfig::default()); | |
| assert!(r.passed(), "stub len {stub_len} failed: {:?}", r.findings); | |
| } | |
| } | |
| #[test] | |
| fn rejects_gnu_hash_zero_nbuckets_and_bucket_below_symoffset() { | |
| let text = vec![0x90, 0x90, 0x90, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 4, | |
| }]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 4, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "ext_a".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 4, | |
| }, | |
| ]); | |
| let run_ghash = |ghash: Vec<u8>| -> AuditReport { | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (0x6fff_fef5, 0x2200), // DT_GNU_HASH | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 4, | |
| p_memsz: 4, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x200, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str.clone(), | |
| }, | |
| SecSpec { | |
| name: ".gnu.hash".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 3, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ghash, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 7, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str.clone(), | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x3100), (3, 0x2100), (4, 0x2160), (5, 0x2200)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("gh.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "gh.o", &obj, &mf, &AuditConfig::default()) | |
| }; | |
| // nbuckets = 0 must fail closed. | |
| let mut zero_nb = Vec::new(); | |
| for w in [0u32, 2, 1, 0] { | |
| zero_nb.extend_from_slice(&w.to_le_bytes()); | |
| } | |
| zero_nb.extend_from_slice(&0u64.to_le_bytes()); | |
| let r_zero = run_ghash(zero_nb); | |
| assert!(r_zero | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID" && f.message.contains("nbuckets"))); | |
| // symoffset = 2 with buckets = [1, 2]: bucket[0] = 1 < symoffset fails. | |
| let mut bad_bucket = Vec::new(); | |
| for w in [2u32, 2, 1, 0] { | |
| bad_bucket.extend_from_slice(&w.to_le_bytes()); | |
| } | |
| bad_bucket.extend_from_slice(&0u64.to_le_bytes()); // bloom[0] | |
| bad_bucket.extend_from_slice(&1u32.to_le_bytes()); // bucket[0] < symoffset | |
| bad_bucket.extend_from_slice(&2u32.to_le_bytes()); // bucket[1] == symoffset | |
| bad_bucket.extend_from_slice(&1u32.to_le_bytes()); // chain terminator | |
| let r_bucket = run_ghash(bad_bucket); | |
| assert!(r_bucket | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "DYNAMIC_TABLE_INVALID" && f.message.contains("bucket symbol"))); | |
| } | |
| #[test] | |
| fn rejects_x86_plt32_and_pc32_when_pc_bias_undecodable() { | |
| // 1. R_X86_64_PLT32 on non-PC-relative `mov eax, imm32` targeting a | |
| // valid eagerly-bound PLT stub must fail closed. | |
| let run_plt_case = |obj_text: Vec<u8>, rel_off: u64| -> AuditReport { | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: obj_text.len() as u64, | |
| }, | |
| SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(rel_off, 2, R_X86_64_PLT32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x1020i64 - 4 - (0x1000i64 + rel_off as i64)) as i32; | |
| let ro = rel_off as usize; | |
| l_text[ro..ro + 4].copy_from_slice(&disp.to_le_bytes()); | |
| let mut plt = vec![0xff, 0x25, 0, 0, 0, 0]; | |
| let plt_disp = (0x3000i64 - 0x1026) as i32; | |
| plt[2..6].copy_from_slice(&plt_disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: l_text.len() as u64, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let rela_dyn = encode_rela(&[(0x3000, 1, R_X86_64_JUMP_SLOT, 0)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| (24, 0), | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1020, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: plt, | |
| }, | |
| SecSpec { | |
| name: ".got.plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 6, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 6, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[ | |
| (1, 0x1000), | |
| (2, 0x1020), | |
| (3, 0x3000), | |
| (4, 0x3100), | |
| (5, 0x2100), | |
| (6, 0x2160), | |
| (7, 0x2200), | |
| ], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "caller".to_owned(), | |
| object: Some("pb.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "pb.o", &obj, &mf, &AuditConfig::default()) | |
| }; | |
| // Valid `call rel32` and `je rel32` pass; `mov eax, imm32` fails. | |
| assert!(run_plt_case(vec![0xe8, 0, 0, 0, 0, 0xc3], 1).passed()); | |
| assert!(run_plt_case(vec![0x0f, 0x84, 0, 0, 0, 0, 0xc3], 2).passed()); | |
| let r_mov = run_plt_case(vec![0xb8, 0, 0, 0, 0, 0xc3], 1); | |
| assert!(r_mov | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH" && f.message.contains("pc_bias"))); | |
| // 2. Undecoded `cpuid` (0f a2) before `cmpb $1, data_sym(%rip)` must | |
| // fail closed instead of falling back to pc_bias = -4. | |
| let cpuid_cmpb = vec![0x0f, 0xa2, 0x80, 0x3d, 0, 0, 0, 0, 0x01, 0xc3]; | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: cpuid_cmpb.len() as u64, | |
| }, | |
| SymSpec { | |
| name: "rw_var".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: cpuid_cmpb.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(4, 2, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = cpuid_cmpb; | |
| let disp = (0x3000i64 - 4 - 0x1004) as i32; | |
| l_text[4..8].copy_from_slice(&disp.to_le_bytes()); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: l_text.len() as u64, | |
| }, | |
| SymSpec { | |
| name: "rw_var".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x3000, | |
| size: 4, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".data".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0; 8], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x3000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("cpuid.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| let r_cpuid = run_single(&linked, "cpuid.o", &obj, &mf, &AuditConfig::default()); | |
| assert!(r_cpuid | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH" && f.message.contains("pc_bias"))); | |
| } | |
| #[test] | |
| fn rejects_rodata_icf_aliasing_on_non_merge_named_constants() { | |
| let ro_bytes = b"SAME_TAG".to_vec(); | |
| // 1. x86-64: CONST_A and CONST_B folded to 0x2000 by `lld --icf=all`. | |
| let run_x86 = |const_b_va: u64| -> AuditReport { | |
| let obj_text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "CONST_A".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata.CONST_A".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x2000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let mut l_ro = ro_bytes.clone(); | |
| l_ro.resize(0x18, 0); | |
| l_ro[0x10..0x18].copy_from_slice(&ro_bytes); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "CONST_A".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "CONST_B".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: const_b_va, | |
| size: 8, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("ricf.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "ricf.o", &obj, &mf, &AuditConfig::default()) | |
| }; | |
| assert!(run_x86(0x2010).passed()); | |
| let r_x86_icf = run_x86(0x2000); | |
| assert!(r_x86_icf.findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| // 2. AArch64: ADRP + ADD referencing CONST_A folded onto CONST_B. | |
| let run_a64 = |const_b_va: u64| -> AuditReport { | |
| let mut obj_code = Vec::new(); | |
| obj_code.extend_from_slice(&0x9000_0000u32.to_le_bytes()); // adrp x0, 0 | |
| obj_code.extend_from_slice(&0x9100_0000u32.to_le_bytes()); // add x0, x0, #0 | |
| obj_code.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); // ret | |
| let mut linked_code = Vec::new(); | |
| linked_code.extend_from_slice(&0x9000_0080u32.to_le_bytes()); // page +0x10000 | |
| linked_code.extend_from_slice(&0x9100_0000u32.to_le_bytes()); // +0 | |
| linked_code.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); // ret | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 12, | |
| }, | |
| SymSpec { | |
| name: "CONST_A".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_code, | |
| }, | |
| SecSpec { | |
| name: ".rodata.CONST_A".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[ | |
| (0, 2, R_AARCH64_ADR_PREL_PG_HI21, 0), | |
| (4, 2, R_AARCH64_ADD_ABS_LO12_NC, 0), | |
| ]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_ro = ro_bytes.clone(); | |
| l_ro.resize(0x18, 0); | |
| l_ro[0x10..0x18].copy_from_slice(&ro_bytes); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f64".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1_0000, | |
| size: 12, | |
| }, | |
| SymSpec { | |
| name: "CONST_A".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2_0000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "CONST_B".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: const_b_va, | |
| size: 8, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1_0000, | |
| p_vaddr: 0x1_0000, | |
| p_filesz: 12, | |
| p_memsz: 12, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2_0000, | |
| p_vaddr: 0x2_0000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x1_0000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: linked_code, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2_0000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1_0000), (2, 0x2_0000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f64".to_owned(), | |
| object: Some("ricf64.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "ricf64.o", &obj, &mf, &AuditConfig::default()) | |
| }; | |
| assert!(run_a64(0x2_0010).passed()); | |
| let r_a64_icf = run_a64(0x2_0000); | |
| assert!(r_a64_icf.findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| } | |
| #[test] | |
| fn rejects_x86_named_rodata_redirection_and_ifunc() { | |
| let ro_bytes = b"CONST_VA".to_vec(); | |
| let run_case = | |
| |obj_text: Vec<u8>, rel_off: u64, patch_va_target: u64, linked_ty: u8| -> AuditReport { | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: obj_text.len() as u64, | |
| }, | |
| SymSpec { | |
| name: "CONST_A".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(rel_off, 2, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (patch_va_target as i64 - 4 - (0x1000i64 + rel_off as i64)) as i32; | |
| let ro = rel_off as usize; | |
| l_text[ro..ro + 4].copy_from_slice(&disp.to_le_bytes()); | |
| // Put identical bytes at 0x2000 (CONST_A) and unnamed 0x2010. | |
| let mut l_ro = ro_bytes.clone(); | |
| l_ro.resize(0x18, 0); | |
| l_ro[0x10..0x18].copy_from_slice(&ro_bytes); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: l_text.len() as u64, | |
| }, | |
| SymSpec { | |
| name: "CONST_A".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: linked_ty, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("rv.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "rv.o", &obj, &mf, &AuditConfig::default()) | |
| }; | |
| let lea = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| assert!(run_case(lea.clone(), 3, 0x2000, STT_OBJECT).passed()); | |
| // Redirecting disp32 to unnamed byte-identical copy at 0x2010 fails. | |
| assert!(run_case(lea.clone(), 3, 0x2010, STT_OBJECT) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // STT_GNU_IFUNC on linked rodata symbol fails. | |
| assert!(run_case(lea, 3, 0x2000, STT_GNU_IFUNC) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // Non-RIP-relative instruction (`mov eax, imm32`) fails. | |
| assert!(run_case(vec![0xb8, 0, 0, 0, 0, 0xc3], 1, 0x2000, STT_OBJECT) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_weak_and_hidden_internal_glob_dat_bindings() { | |
| const STB_WEAK: u8 = 2; | |
| let run_case = |dsym_bind: u8, dsym_vis: u8| -> AuditReport { | |
| let obj_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "g_var".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x3000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "g_var".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x3040, | |
| size: 8, | |
| }, | |
| ]); | |
| let (mut ds, dstr) = encode_symtab(&[SymSpec { | |
| name: "g_var".to_owned(), | |
| bind: dsym_bind, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x3040, | |
| size: 8, | |
| }]); | |
| // Patch st_other visibility byte of symbol 1 (entry offset 24 + 5). | |
| ds[24 + 5] = dsym_vis; | |
| let rela_dyn = encode_rela(&[(0x3000, 1, R_X86_64_GLOB_DAT, 0)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, dstr.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0; 0x50], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ds, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: dstr, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 8, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x3000), (3, 0x3100), (4, 0x2100), (5, 0x2160), (6, 0x2200)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("gd.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "gd.o", &obj, &mf, &AuditConfig::default()) | |
| }; | |
| // All defined in-image R_X86_64_GLOB_DAT bindings fail closed, | |
| // including STB_GLOBAL + STV_PROTECTED (3), STB_GLOBAL + STV_DEFAULT | |
| // (0), STB_WEAK + STV_PROTECTED, STV_HIDDEN (2), and STV_INTERNAL (1). | |
| for (bind, vis) in | |
| [(STB_GLOBAL, 3), (STB_GLOBAL, 0), (STB_WEAK, 3), (STB_GLOBAL, 2), (STB_GLOBAL, 1)] | |
| { | |
| let r = run_case(bind, vis); | |
| assert!( | |
| r.findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID"), | |
| "expected X86_GOT_SLOT_INVALID for bind={bind} vis={vis}, got: {:?}", | |
| r.findings | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn rejects_dt_pltgot_and_dt_tlsdesc_got_overlap_with_got_slots() { | |
| let run_case = |e_type: u16, extra_dyn_tag: Option<(i64, u64)>| -> AuditReport { | |
| let obj_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "g_var".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x3010i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let mut got_bytes = vec![0u8; 0x50]; | |
| got_bytes[0x10..0x18].copy_from_slice(&0x3040u64.to_le_bytes()); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "g_var".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x3040, | |
| size: 8, | |
| }, | |
| ]); | |
| let (ds, dstr) = encode_symtab(&[SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let relr_bytes = | |
| if e_type == ET_DYN { 0x3010u64.to_le_bytes().to_vec() } else { Vec::new() }; | |
| let mut dyn_tags = vec![(5, 0x2160), (6, 0x2100), (10, dstr.len() as u64), (11, 24)]; | |
| if e_type == ET_DYN { | |
| dyn_tags.push((36, 0x2200)); // DT_RELR | |
| dyn_tags.push((35, 8)); // DT_RELRSZ | |
| dyn_tags.push((37, 8)); // DT_RELRENT | |
| } | |
| if let Some(tag) = extra_dyn_tag { | |
| dyn_tags.push(tag); | |
| } | |
| let dyn_bytes = encode_dyn(&dyn_tags); | |
| let linked = build_elf( | |
| e_type, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x1000, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: got_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 5, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ds, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: dstr, | |
| }, | |
| SecSpec { | |
| name: ".relr.dyn".to_owned(), | |
| sh_type: SHT_RELR, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 8, | |
| data: relr_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 8, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x3000), (3, 0x3100), (4, 0x2100), (5, 0x2160), (6, 0x2200)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("pg.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| run_single(&linked, "pg.o", &obj, &mf, &AuditConfig::default()) | |
| }; | |
| // Disjoint DT_PLTGOT (0x3020) passes on both ET_EXEC and DT_RELR. | |
| assert!(run_case(ET_EXEC, Some((3, 0x3020))).passed()); | |
| assert!(run_case(ET_DYN, Some((3, 0x3020))).passed()); | |
| // DT_PLTGOT = 0x3000 clobbers slot 0x3010 (DT_PLTGOT[2]). | |
| assert!(run_case(ET_EXEC, Some((3, 0x3000))) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| assert!(run_case(ET_DYN, Some((3, 0x3000))) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| // DT_TLSDESC_GOT = 0x3010 clobbers slot 0x3010 directly. | |
| assert!(run_case(ET_EXEC, Some((0x6fff_fef7, 0x3010))) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| } | |
| #[test] | |
| fn rejects_same_named_and_stripped_local_rodata_icf_folding() { | |
| let ro_bytes = b"LOC_DATA".to_vec(); | |
| let make_obj = |fn_name: &str| -> Vec<u8> { | |
| let text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: fn_name.to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: text, | |
| }, | |
| SecSpec { | |
| name: ".rodata.TABLE".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 2, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 1, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ) | |
| }; | |
| let obj_a = make_obj("f_a"); | |
| let obj_b = make_obj("f_b"); | |
| let run_case = |table_b_va: u64, strip_locals: bool| -> AuditReport { | |
| let mut l_text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let disp_a = (0x2000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp_a.to_le_bytes()); | |
| let mut fb_text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let disp_b = (table_b_va as i64 - 4 - 0x100b) as i32; | |
| fb_text[3..7].copy_from_slice(&disp_b.to_le_bytes()); | |
| l_text.extend_from_slice(&fb_text); | |
| let mut l_ro = ro_bytes.clone(); | |
| l_ro.resize(0x18, 0); | |
| l_ro[0x10..0x18].copy_from_slice(&ro_bytes); | |
| let mut lsyms = Vec::new(); | |
| if !strip_locals { | |
| lsyms.push(SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }); | |
| lsyms.push(SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: table_b_va, | |
| size: 8, | |
| }); | |
| } | |
| lsyms.push(SymSpec { | |
| name: "f_a".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }); | |
| lsyms.push(SymSpec { | |
| name: "f_b".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1008, | |
| size: 8, | |
| }); | |
| let (ls, lstr) = encode_symtab(&lsyms); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 16, | |
| p_memsz: 16, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: if strip_locals { 1 } else { 3 }, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ | |
| ManifestSymbol { | |
| name: "f_a".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ManifestSymbol { | |
| name: "f_b".to_owned(), | |
| object: Some("b.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ | |
| ObjectInput { label: "a.o".to_owned(), bytes: &obj_a }, | |
| ObjectInput { label: "b.o".to_owned(), bytes: &obj_b }, | |
| ], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| // Disjoint VAs pass (both with and without stripped local symbols). | |
| assert!(run_case(0x2010, false).passed()); | |
| assert!(run_case(0x2010, true).passed()); | |
| // Folded to same VA 0x2000: rejected with duplicate TABLE entries AND | |
| // rejected even when `ld -x` strips local symbols from .symtab! | |
| assert!(run_case(0x2000, false).findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| assert!(run_case(0x2000, true).findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| } | |
| #[test] | |
| fn rejects_out_of_bounds_rodata_addends_across_x86_and_aarch64() { | |
| // 1a. x86-64 non-SHF_MERGE STT_SECTION .rodata: enforce RIP-relative | |
| // opcode decoding and 0 <= r_addend - pc_bias < obj_sec_bytes.len(). | |
| let run_x86_sec = |non_rip_op: bool, addend: i64| -> AuditReport { | |
| let ro_bytes = vec![0xaa; 16]; | |
| let obj_text = if non_rip_op { | |
| vec![0xb8, 0, 0, 0, 0, 0xc3] | |
| } else { | |
| vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3] | |
| }; | |
| let rel_off: usize = if non_rip_op { 1 } else { 3 }; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 2, | |
| value: 0, | |
| size: 0, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: obj_text.len() as u64, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 2, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(rel_off as u64, 1, R_X86_64_PC32, addend)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x2000i64 + addend - (0x1000 + rel_off as i64)) as i32; | |
| l_text[rel_off..rel_off + 4].copy_from_slice(&disp.to_le_bytes()); | |
| let mut l_ro = ro_bytes; | |
| l_ro.resize(0x1100, 0x55); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: l_text.len() as u64, | |
| }]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_x86_sec(false, 8 - 4).passed()); | |
| assert!(run_x86_sec(false, 0x1000 - 4) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_x86_sec(false, -8).findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_x86_sec(true, -4).findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // 1b. x86-64 zero-size SHF_MERGE named .rodata symbol bounds check. | |
| let run_x86_merge_named = |semantic_off: i64| -> AuditReport { | |
| let ro_bytes = b"hello\0".to_vec(); | |
| let obj_text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "MSG".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 0, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| let r_addend = semantic_off - 4; | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata.str1.1".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_MERGE | SHF_STRINGS, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 1, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 2, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 1, R_X86_64_PC32, r_addend)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x2000i64 + r_addend - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let mut l_ro = ro_bytes; | |
| l_ro.resize(0x200, 0x41); | |
| let (l_sym, l_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "MSG".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 0, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 2, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_x86_merge_named(2).passed()); | |
| assert!(run_x86_merge_named(6).findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_x86_merge_named(0x100) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // 1c & 1d. AArch64 STT_SECTION and named .rodata symbol addend bounds. | |
| let run_aarch64 = |use_named: bool, addend: i64| -> AuditReport { | |
| let ro_bytes = vec![0x33; 16]; | |
| let mut obj_text = Vec::new(); | |
| obj_text.extend_from_slice(&encode_adrp(0, 0).to_le_bytes()); | |
| obj_text.extend_from_slice(&encode_add_imm12(0, 0, 0).to_le_bytes()); | |
| obj_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let target_sym_spec = if use_named { | |
| SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| } | |
| } else { | |
| SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 2, | |
| value: 0, | |
| size: 0, | |
| } | |
| }; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| target_sym_spec, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 12, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: if use_named { 1 } else { 2 }, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[ | |
| (0, 1, R_AARCH64_ADR_PREL_PG_HI21, addend), | |
| (4, 1, R_AARCH64_ADD_ABS_LO12_NC, addend), | |
| ]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let target_va = (0x20010i64 + addend) as u64; | |
| let page_delta = (target_va & !0xfff) as i64 - 0x10000; | |
| let mut l_text = Vec::new(); | |
| l_text.extend_from_slice(&encode_adrp(0, page_delta).to_le_bytes()); | |
| l_text.extend_from_slice( | |
| &encode_add_imm12(0, 0, (target_va & 0xfff) as u32).to_le_bytes(), | |
| ); | |
| l_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let mut l_ro = vec![0u8; 0x100]; | |
| l_ro[0x10..0x20].copy_from_slice(&ro_bytes); | |
| let mut l_syms = vec![SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x10000, | |
| size: 12, | |
| }]; | |
| if use_named { | |
| l_syms.push(SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x20010, | |
| size: 8, | |
| }); | |
| } | |
| let (l_sym, l_str) = encode_symtab(&l_syms); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x10000, | |
| p_vaddr: 0x10000, | |
| p_filesz: 12, | |
| p_memsz: 12, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x20000, | |
| p_vaddr: 0x20000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x10000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x20000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x10000), (2, 0x20000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_aarch64(false, 4).passed()); | |
| assert!(run_aarch64(false, -4) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "AARCH64_RELOC_TARGET_MISMATCH")); | |
| assert!(run_aarch64(false, 16) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "AARCH64_RELOC_TARGET_MISMATCH")); | |
| assert!(run_aarch64(true, 4).passed()); | |
| assert!(run_aarch64(true, -4) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "AARCH64_RELOC_TARGET_MISMATCH")); | |
| assert!(run_aarch64(true, 8) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "AARCH64_RELOC_TARGET_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_compressed_rodata_fail_closed_across_reloc_paths() { | |
| let ro_bytes = vec![0x77; 16]; | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| // 2a. x86-64 PC32 targeting a compressed .rodata section fails closed. | |
| let obj_text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| let obj_pc32 = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_COMPRESSED, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 1, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let disp = (0x2000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| ]); | |
| let linked_pc32 = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 16, | |
| p_memsz: 16, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str.clone(), | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let rep_pc32 = audit_elf( | |
| &linked_pc32, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj_pc32 }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse"); | |
| assert!(rep_pc32.findings.iter().any(|f| f.code == "RODATA_UNRESOLVABLE")); | |
| // 2b. x86-64 GOTPCREL* targeting compressed .rodata rejects GLOB_DAT | |
| // and fails closed on RELATIVE. | |
| let run_got_compressed = |use_glob_dat: bool| -> AuditReport { | |
| let obj_got_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let obj_got = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_got_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_COMPRESSED, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 1, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_got_text = obj_got_text; | |
| let got_disp = (0x3000i64 - 4 - 0x1003) as i32; | |
| l_got_text[3..7].copy_from_slice(&got_disp.to_le_bytes()); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }]); | |
| let rela_dyn = if use_glob_dat { | |
| encode_rela(&[(0x3000, 1, R_X86_64_GLOB_DAT, 0)]) | |
| } else { | |
| encode_rela(&[(0x3000, 0, R_X86_64_RELATIVE, 0x2000)]) | |
| }; | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| (0x6fff_fffb, 0x0800_0000), // DT_FLAGS_1 = DF_1_PIE | |
| ]); | |
| let linked_got = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x100, | |
| p_memsz: 0x100, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3020, | |
| p_vaddr: 0x3020, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_got_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 3, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 8, | |
| data: 0x2000u64.to_le_bytes().to_vec(), | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: 6, // SHT_DYNAMIC | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3020, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str.clone(), | |
| }, | |
| ], | |
| &[ | |
| (1, 0x1000), | |
| (2, 0x2000), | |
| (3, 0x2100), | |
| (4, 0x2160), | |
| (5, 0x2200), | |
| (6, 0x3000), | |
| (7, 0x3020), | |
| ], | |
| ); | |
| audit_elf( | |
| &linked_got, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj_got }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_got_compressed(true).findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| assert!(run_got_compressed(false).findings.iter().any(|f| f.code == "RODATA_UNRESOLVABLE")); | |
| // 2c. AArch64 ADRP + ADD targeting compressed .rodata fails closed. | |
| let mut a64_obj_text = Vec::new(); | |
| a64_obj_text.extend_from_slice(&encode_adrp(0, 0).to_le_bytes()); | |
| a64_obj_text.extend_from_slice(&encode_add_imm12(0, 0, 0).to_le_bytes()); | |
| a64_obj_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let (a64_osym, a64_ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 12, | |
| }, | |
| ]); | |
| let a64_obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_obj_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_COMPRESSED, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: a64_osym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[ | |
| (0, 1, R_AARCH64_ADR_PREL_PG_HI21, 0), | |
| (4, 1, R_AARCH64_ADD_ABS_LO12_NC, 0), | |
| ]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut a64_ltext = Vec::new(); | |
| a64_ltext.extend_from_slice(&encode_adrp(0, 0x10000).to_le_bytes()); | |
| a64_ltext.extend_from_slice(&encode_add_imm12(0, 0, 0).to_le_bytes()); | |
| a64_ltext.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let (a64_lsym, a64_lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x20000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x10000, | |
| size: 12, | |
| }, | |
| ]); | |
| let a64_linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x10000, | |
| p_vaddr: 0x10000, | |
| p_filesz: 12, | |
| p_memsz: 12, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x20000, | |
| p_vaddr: 0x20000, | |
| p_filesz: 16, | |
| p_memsz: 16, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x10000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_ltext, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x20000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: a64_lsym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_lstr, | |
| }, | |
| ], | |
| &[(1, 0x10000), (2, 0x20000)], | |
| ); | |
| let rep_a64 = audit_elf( | |
| &a64_linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &a64_obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse"); | |
| assert!(rep_a64.findings.iter().any(|f| f.code == "RODATA_UNRESOLVABLE")); | |
| } | |
| #[test] | |
| fn rejects_section_rodata_icf_and_local_global_alias_confusion() { | |
| let ro_bytes = vec![0xde, 0xad, 0xbe, 0xef, 1, 2, 3, 4]; | |
| // 3a. x86-64 STT_SECTION non-SHF_MERGE .rodata ICF across two objects | |
| // and against an unowned linked symbol (while allowing owned symbols). | |
| let make_x86_sec_obj = |fn_name: &str, owned_local: Option<&str>| -> Vec<u8> { | |
| let mut syms = vec![SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 2, | |
| value: 0, | |
| size: 0, | |
| }]; | |
| let mut sh_info = 2u32; | |
| if let Some(lbl) = owned_local { | |
| syms.push(SymSpec { | |
| name: lbl.to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }); | |
| sh_info = 3; | |
| } | |
| syms.push(SymSpec { | |
| name: fn_name.to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }); | |
| let (os, ostr) = encode_symtab(&syms); | |
| build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3], | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 1, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ) | |
| }; | |
| let x86_a = make_x86_sec_obj("f_a", Some("OWNED_A")); | |
| let x86_b = make_x86_sec_obj("f_b", None); | |
| let run_x86_icf = |ro_b_va: u64, strip_locals: bool, add_foreign: bool| -> AuditReport { | |
| let mut l_text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let disp_a = (0x2000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp_a.to_le_bytes()); | |
| let mut fb = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let disp_b = (ro_b_va as i64 - 4 - 0x100b) as i32; | |
| fb[3..7].copy_from_slice(&disp_b.to_le_bytes()); | |
| l_text.extend_from_slice(&fb); | |
| let mut l_ro = ro_bytes.clone(); | |
| l_ro.resize(0x18, 0); | |
| l_ro[0x10..0x18].copy_from_slice(&ro_bytes); | |
| let mut lsyms = Vec::new(); | |
| let mut sh_info = 1u32; | |
| if !strip_locals { | |
| lsyms.push(SymSpec { | |
| name: "OWNED_A".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }); | |
| sh_info += 1; | |
| } | |
| if add_foreign { | |
| lsyms.push(SymSpec { | |
| name: "FOREIGN".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }); | |
| } | |
| lsyms.push(SymSpec { | |
| name: "f_a".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }); | |
| lsyms.push(SymSpec { | |
| name: "f_b".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1008, | |
| size: 8, | |
| }); | |
| let (ls, lstr) = encode_symtab(&lsyms); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 16, | |
| p_memsz: 16, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ | |
| ManifestSymbol { | |
| name: "f_a".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ManifestSymbol { | |
| name: "f_b".to_owned(), | |
| object: Some("b.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ | |
| ObjectInput { label: "a.o".to_owned(), bytes: &x86_a }, | |
| ObjectInput { label: "b.o".to_owned(), bytes: &x86_b }, | |
| ], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_x86_icf(0x2010, false, false).passed()); | |
| assert!(run_x86_icf(0x2010, true, false).passed()); | |
| assert!(run_x86_icf(0x2000, false, false) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "ICF_ALIASING")); | |
| assert!(run_x86_icf(0x2000, true, false).findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| assert!(run_x86_icf(0x2010, false, true).findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| // 3b. AArch64 STT_SECTION non-SHF_MERGE .rodata ICF across two objects. | |
| let make_a64_sec_obj = |fn_name: &str| -> Vec<u8> { | |
| let mut txt = Vec::new(); | |
| txt.extend_from_slice(&encode_adrp(0, 0).to_le_bytes()); | |
| txt.extend_from_slice(&encode_add_imm12(0, 0, 0).to_le_bytes()); | |
| txt.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let (os, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 2, | |
| value: 0, | |
| size: 0, | |
| }, | |
| SymSpec { | |
| name: fn_name.to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 12, | |
| }, | |
| ]); | |
| build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: txt, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 2, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[ | |
| (0, 1, R_AARCH64_ADR_PREL_PG_HI21, 0), | |
| (4, 1, R_AARCH64_ADD_ABS_LO12_NC, 0), | |
| ]), | |
| }, | |
| ], | |
| &[], | |
| ) | |
| }; | |
| let a64_a = make_a64_sec_obj("f_a"); | |
| let a64_b = make_a64_sec_obj("f_b"); | |
| let run_a64_icf = |ro_b_va: u64| -> AuditReport { | |
| let mut l_text = Vec::new(); | |
| l_text.extend_from_slice(&encode_adrp(0, 0x10000).to_le_bytes()); | |
| l_text.extend_from_slice(&encode_add_imm12(0, 0, 0).to_le_bytes()); | |
| l_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| l_text.extend_from_slice(&encode_adrp(0, 0x10000).to_le_bytes()); | |
| l_text | |
| .extend_from_slice(&encode_add_imm12(0, 0, (ro_b_va & 0xfff) as u32).to_le_bytes()); | |
| l_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let mut l_ro = ro_bytes.clone(); | |
| l_ro.resize(0x18, 0); | |
| l_ro[0x10..0x18].copy_from_slice(&ro_bytes); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f_a".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x10000, | |
| size: 12, | |
| }, | |
| SymSpec { | |
| name: "f_b".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000c, | |
| size: 12, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x10000, | |
| p_vaddr: 0x10000, | |
| p_filesz: 24, | |
| p_memsz: 24, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x20000, | |
| p_vaddr: 0x20000, | |
| p_filesz: l_ro.len() as u64, | |
| p_memsz: l_ro.len() as u64, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x10000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x20000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_ro, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x10000), (2, 0x20000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ | |
| ManifestSymbol { | |
| name: "f_a".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ManifestSymbol { | |
| name: "f_b".to_owned(), | |
| object: Some("b.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ | |
| ObjectInput { label: "a.o".to_owned(), bytes: &a64_a }, | |
| ObjectInput { label: "b.o".to_owned(), bytes: &a64_b }, | |
| ], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_a64_icf(0x20010).passed()); | |
| assert!(run_a64_icf(0x20000).findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| // 3c. Verified STB_LOCAL named .rodata symbol stripped from .symtab | |
| // must not treat an exported STB_GLOBAL symbol of the same name/size | |
| // in .symtab or .dynsym as self. | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| let obj_local = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3], | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 2, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 1, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let run_local_vs_global = |in_dynsym: bool| -> AuditReport { | |
| let mut l_text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let disp = (0x2000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let mut lsyms = vec![SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }]; | |
| if !in_dynsym { | |
| lsyms.push(SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }); | |
| } | |
| let (ls, lstr) = encode_symtab(&lsyms); | |
| let mut secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ]; | |
| if in_dynsym { | |
| let (ds, dstr) = encode_symtab(&[SymSpec { | |
| name: "TABLE".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }]); | |
| secs.push(SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 6, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ds, | |
| }); | |
| secs.push(SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: dstr, | |
| }); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| secs, | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj_local }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_local_vs_global(false).findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| assert!(run_local_vs_global(true).findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| } | |
| #[test] | |
| fn rejects_overflowing_or_non_alloc_got_and_plt_section_headers() { | |
| let patch_sh_size = |elf: &mut [u8], shndx_1based: usize, new_size: u64| { | |
| let shoff = u64::from_le_bytes(elf[40..48].try_into().unwrap()) as usize; | |
| let off = shoff + shndx_1based * 64 + 32; | |
| elf[off..off + 8].copy_from_slice(&new_size.to_le_bytes()); | |
| }; | |
| // 4a. GOT containment check requires SHF_ALLOC and checked_add. | |
| let run_got = |alloc_flag: bool, overflow_size: bool| -> AuditReport { | |
| let obj_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "gvar".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 1, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let slot_va: u64 = if overflow_size { 0x3010 } else { 0x3000 }; | |
| let mut l_text = obj_text; | |
| let disp = (slot_va as i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let mut rw = vec![0u8; 0x30]; | |
| let slot_off = (slot_va - 0x3000) as usize; | |
| rw[slot_off..slot_off + 8].copy_from_slice(&0x3020u64.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "gvar".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x3020, | |
| size: 8, | |
| }, | |
| ]); | |
| let mut linked = build_elf( | |
| ET_EXEC, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x30, | |
| p_memsz: 0x30, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".data".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: rw, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: if alloc_flag { SHF_ALLOC | SHF_WRITE } else { SHF_WRITE }, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 8, | |
| data: 0x3020u64.to_le_bytes().to_vec(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 5, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x3000)], | |
| ); | |
| if overflow_size { | |
| patch_sh_size(&mut linked, 3, u64::MAX); | |
| } | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_got(true, false).passed()); | |
| assert!(run_got(false, false).findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| assert!(run_got(true, true).findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID")); | |
| // 4b. PLT and .got.plt containment checks require SHF_ALLOC and | |
| // checked_add when --require-relro is off. | |
| #[derive(Clone, Copy)] | |
| enum PltCase { | |
| Ok, | |
| PltNonAlloc, | |
| PltOverflow, | |
| GotPltNonAlloc, | |
| GotPltOverflow, | |
| } | |
| let run_plt = |case: PltCase| -> AuditReport { | |
| let obj_text = vec![0xe8, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 6, | |
| }, | |
| SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(1, 2, R_X86_64_PLT32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = obj_text; | |
| let call_disp = (0x1020i64 - 4 - 0x1001) as i32; | |
| l_text[1..5].copy_from_slice(&call_disp.to_le_bytes()); | |
| let mut plt = vec![0xff, 0x25, 0, 0, 0, 0]; | |
| let plt_disp = (0x3010i64 - 0x1026) as i32; | |
| plt[2..6].copy_from_slice(&plt_disp.to_le_bytes()); | |
| let (l_sym, l_str) = encode_symtab(&[SymSpec { | |
| name: "caller".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 6, | |
| }]); | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "ext_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }]); | |
| let rela_dyn = encode_rela(&[(0x3010, 1, R_X86_64_JUMP_SLOT, 0)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (5, 0x2160), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| (24, 0), // DT_BIND_NOW | |
| ]); | |
| let plt_flags = if matches!(case, PltCase::PltNonAlloc) { | |
| SHF_EXECINSTR | |
| } else { | |
| SHF_ALLOC | SHF_EXECINSTR | |
| }; | |
| let got_plt_flags = if matches!(case, PltCase::GotPltNonAlloc) { | |
| SHF_WRITE | |
| } else { | |
| SHF_ALLOC | SHF_WRITE | |
| }; | |
| let mut linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x100, | |
| p_memsz: 0x100, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3040, | |
| p_vaddr: 0x3040, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: plt_flags, | |
| sh_addr: 0x1020, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: plt, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 3, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".got.plt".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: got_plt_flags, | |
| sh_addr: 0x3010, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 8, | |
| data: vec![0u8; 8], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: 6, // SHT_DYNAMIC | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3040, | |
| sh_link: 4, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 9, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[ | |
| (1, 0x1000), | |
| (2, 0x1020), | |
| (3, 0x2100), | |
| (4, 0x2160), | |
| (5, 0x2200), | |
| (6, 0x3010), | |
| (7, 0x3040), | |
| ], | |
| ); | |
| if matches!(case, PltCase::PltOverflow) { | |
| patch_sh_size(&mut linked, 2, u64::MAX); | |
| } | |
| if matches!(case, PltCase::GotPltOverflow) { | |
| patch_sh_size(&mut linked, 6, u64::MAX); | |
| } | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "caller".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_plt(PltCase::Ok).passed()); | |
| assert!(run_plt(PltCase::PltNonAlloc) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_plt(PltCase::PltOverflow) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_plt(PltCase::GotPltNonAlloc) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| assert!(run_plt(PltCase::GotPltOverflow) | |
| .findings | |
| .iter() | |
| .any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| } | |
| #[test] | |
| fn rejects_shf_compressed_symtab_strtab_shndx_and_rela_sections() { | |
| let (o_sym, o_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: SHN_XINDEX, | |
| value: 0, | |
| size: 1, | |
| }]); | |
| let mut shndx_data = Vec::new(); | |
| shndx_data.extend_from_slice(&0u32.to_le_bytes()); | |
| shndx_data.extend_from_slice(&1u32.to_le_bytes()); | |
| let build_obj_with_compressed = |target: Option<&str>| -> Vec<u8> { | |
| let flag = |name: &str| if target == Some(name) { SHF_COMPRESSED } else { 0 }; | |
| build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0xc3], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: flag(".symtab"), | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: flag(".strtab"), | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab_shndx".to_owned(), | |
| sh_type: SHT_SYMTAB_SHNDX, | |
| sh_flags: flag(".symtab_shndx"), | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 0, | |
| sh_entsize: 4, | |
| data: shndx_data.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: flag(".rela.text"), | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(0, 1, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ) | |
| }; | |
| assert!(Elf64::parse(&build_obj_with_compressed(None)).is_ok()); | |
| for sec in [".symtab", ".strtab", ".symtab_shndx", ".rela.text"] { | |
| let err = Elf64::parse(&build_obj_with_compressed(Some(sec))) | |
| .err() | |
| .unwrap_or_else(|| panic!("expected SHF_COMPRESSED rejection for {sec}")); | |
| assert!(err.contains("SHF_COMPRESSED"), "unexpected error for {sec}: {err}"); | |
| } | |
| // Also reject SHF_COMPRESSED on .dynsym and .dynstr in ET_DYN. | |
| let (d_sym, d_str) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }]); | |
| let build_dyn_with_compressed = |target: Option<&str>| -> Vec<u8> { | |
| let flag = | |
| |name: &str| SHF_ALLOC | if target == Some(name) { SHF_COMPRESSED } else { 0 }; | |
| build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 1, | |
| p_memsz: 1, | |
| p_align: 0x1000, | |
| }], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0xc3], | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: flag(".dynsym"), | |
| sh_addr: 0x1100, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym.clone(), | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: flag(".dynstr"), | |
| sh_addr: 0x1160, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str.clone(), | |
| }, | |
| ], | |
| &[(1, 0x1000)], | |
| ) | |
| }; | |
| assert!(Elf64::parse(&build_dyn_with_compressed(None)).is_ok()); | |
| for sec in [".dynsym", ".dynstr"] { | |
| let err = Elf64::parse(&build_dyn_with_compressed(Some(sec))) | |
| .err() | |
| .unwrap_or_else(|| panic!("expected SHF_COMPRESSED rejection for {sec}")); | |
| assert!(err.contains("SHF_COMPRESSED"), "unexpected error for {sec}: {err}"); | |
| } | |
| } | |
| #[test] | |
| fn rejects_verified_local_symbol_binding_mismatch_and_dynsym_icf() { | |
| let make_obj = |bind: u8, name: &str| -> Vec<u8> { | |
| let (os, ostr) = encode_symtab(&[SymSpec { | |
| name: name.to_owned(), | |
| bind, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 1, | |
| }]); | |
| build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0xc3], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: if bind == STB_LOCAL { 2 } else { 1 }, | |
| sh_entsize: 24, | |
| data: os, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| ], | |
| &[], | |
| ) | |
| }; | |
| let obj_local = make_obj(STB_LOCAL, "foo"); | |
| let obj_global = make_obj(STB_GLOBAL, "bar"); | |
| let run_case = |obj: &[u8], | |
| sym_name: &str, | |
| lsyms: Vec<SymSpec>, | |
| sym_sh_info: u32, | |
| dsyms: Vec<SymSpec>, | |
| dyn_sh_info: u32| | |
| -> AuditReport { | |
| let (ls, lstr) = encode_symtab(&lsyms); | |
| let mut l_text = vec![0xc3; 0x20]; | |
| l_text[0] = 0xc3; | |
| let mut secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: sym_sh_info, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ]; | |
| if !dsyms.is_empty() { | |
| let (ds, dstr) = encode_symtab(&dsyms); | |
| secs.push(SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 5, | |
| sh_info: dyn_sh_info, | |
| sh_entsize: 24, | |
| data: ds, | |
| }); | |
| secs.push(SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: dstr, | |
| }); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x20, | |
| p_memsz: 0x20, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| secs, | |
| &[(1, 0x1000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: sym_name.to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| // 1a. Honest STB_LOCAL foo with unrelated global foo at disjoint VA. | |
| let ok = run_case( | |
| &obj_local, | |
| "foo", | |
| vec![SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }], | |
| 2, | |
| vec![SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1010, | |
| size: 1, | |
| }], | |
| 1, | |
| ); | |
| assert!(ok.passed(), "unexpected findings: {:?}", ok.findings); | |
| // 1b. Stripped STB_LOCAL foo must not match an unverified STB_GLOBAL | |
| // foo at 0x1000. | |
| let wrong_bind = run_case( | |
| &obj_local, | |
| "foo", | |
| vec![SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }], | |
| 1, | |
| vec![], | |
| 1, | |
| ); | |
| assert!(wrong_bind.findings.iter().any(|f| f.code == "SYMBOL_MISSING_IN_LINKED")); | |
| // 1c. STB_LOCAL foo in .symtab aliased by STB_GLOBAL foo in .dynsym or | |
| // .symtab at the same VA must trigger ICF_ALIASING. | |
| let icf_dynsym = run_case( | |
| &obj_local, | |
| "foo", | |
| vec![SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }], | |
| 2, | |
| vec![SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }], | |
| 1, | |
| ); | |
| assert!(icf_dynsym.findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| let icf_symtab = run_case( | |
| &obj_local, | |
| "foo", | |
| vec![ | |
| SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }, | |
| SymSpec { | |
| name: "foo".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }, | |
| ], | |
| 2, | |
| vec![], | |
| 1, | |
| ); | |
| assert!(icf_symtab.findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| // 1d. Verified STB_GLOBAL bar rejects STB_LOCAL bar in .dynsym and | |
| // flags STB_LOCAL bar aliased at the same VA in .symtab. | |
| let local_in_dynsym = run_case( | |
| &obj_global, | |
| "bar", | |
| vec![SymSpec { | |
| name: "bar".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }], | |
| 1, | |
| vec![SymSpec { | |
| name: "bar".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }], | |
| 2, | |
| ); | |
| assert!(local_in_dynsym.findings.iter().any(|f| f.code == "DYNSYM_MISMATCH")); | |
| let global_aliased_by_local = run_case( | |
| &obj_global, | |
| "bar", | |
| vec![ | |
| SymSpec { | |
| name: "bar".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }, | |
| SymSpec { | |
| name: "bar".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 1, | |
| }, | |
| ], | |
| 2, | |
| vec![], | |
| 1, | |
| ); | |
| assert!(global_aliased_by_local.findings.iter().any(|f| f.code == "ICF_ALIASING")); | |
| } | |
| #[test] | |
| fn rejects_unpaired_aarch64_adrp_to_ambiguous_cross_object_rodata() { | |
| let mut caller_text = Vec::new(); | |
| caller_text.extend_from_slice(&encode_adrp(0, 0).to_le_bytes()); | |
| caller_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let (c_sym, c_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "ro_k".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let caller_obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: caller_text, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: c_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: c_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(0, 2, R_AARCH64_ADR_PREL_PG_HI21, 0)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let make_ro_obj = |byte: u8| -> Vec<u8> { | |
| let (rs, rstr) = encode_symtab(&[SymSpec { | |
| name: "ro_k".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }]); | |
| build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![byte; 8], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: rs, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: rstr, | |
| }, | |
| ], | |
| &[], | |
| ) | |
| }; | |
| let ro1 = make_ro_obj(0x11); | |
| let ro2 = make_ro_obj(0x22); | |
| let mut l_text = Vec::new(); | |
| l_text.extend_from_slice(&encode_adrp(0, 0x10000).to_le_bytes()); | |
| l_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x10000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "ro_k".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x20000, | |
| size: 8, | |
| }, | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x10000, | |
| p_vaddr: 0x10000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x20000, | |
| p_vaddr: 0x20000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x10000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x20000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0xff; 8], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x10000), (2, 0x20000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("caller.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| let report = audit_elf( | |
| &linked, | |
| &[ | |
| ObjectInput { label: "caller.o".to_owned(), bytes: &caller_obj }, | |
| ObjectInput { label: "ro1.o".to_owned(), bytes: &ro1 }, | |
| ObjectInput { label: "ro2.o".to_owned(), bytes: &ro2 }, | |
| ], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse"); | |
| assert!( | |
| report.findings.iter().any(|f| f.code == "RODATA_UNRESOLVABLE"), | |
| "expected RODATA_UNRESOLVABLE, got: {:?}", | |
| report.findings | |
| ); | |
| } | |
| #[test] | |
| fn rejects_underflowing_same_section_rodata_base_alias() { | |
| // Two overlapping STB_LOCAL .rodata symbols in the same section: | |
| // ro_a at obj_off=0x100 (len=0x100) and ro_b at obj_off=0x180 | |
| // (len=0x80). Folding both onto low VA 0x40 makes both checked_sub | |
| // calls return None; the auditor must still fire ICF_ALIASING. | |
| let obj_text = vec![ | |
| 0x48, 0x8d, 0x05, 0, 0, 0, 0, // lea rax, [rip + disp_a] | |
| 0x48, 0x8d, 0x0d, 0, 0, 0, 0, // lea rcx, [rip + disp_b] | |
| 0xc3, | |
| ]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "ro_a".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x100, | |
| size: 0x100, | |
| }, | |
| SymSpec { | |
| name: "ro_b".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x180, | |
| size: 0x80, | |
| }, | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: obj_text.len() as u64, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0x42; 0x200], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 3, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 1, R_X86_64_PC32, -4), (10, 2, R_X86_64_PC32, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let run_case = |ro_seg_off: usize, ro_seg_va: u64, va_a: u64, va_b: u64| -> AuditReport { | |
| let mut l_text = obj_text.clone(); | |
| let disp_a = (va_a as i64 - 4 - 0x1003) as i32; | |
| let disp_b = (va_b as i64 - 4 - 0x100a) as i32; | |
| l_text[3..7].copy_from_slice(&disp_a.to_le_bytes()); | |
| l_text[10..14].copy_from_slice(&disp_b.to_le_bytes()); | |
| let (ls, lstr) = encode_symtab(&[SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: l_text.len() as u64, | |
| }]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: l_text.len() as u64, | |
| p_memsz: l_text.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: ro_seg_off as u64, | |
| p_vaddr: ro_seg_va, | |
| p_filesz: 0x200, | |
| p_memsz: 0x200, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: ro_seg_va, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0x42; 0x200], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, ro_seg_off)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| // Rigid same-section placement at 0x2000 (0x2100 and 0x2180) passes. | |
| let ok = run_case(0x2000, 0x2000, 0x2100, 0x2180); | |
| assert!(ok.passed(), "unexpected findings: {:?}", ok.findings); | |
| // Both folded onto low VA 0x40 (both checked_sub underflow to None): | |
| // must fire ICF_ALIASING rather than matching None == None. | |
| let folded = run_case(0x2040, 0x40, 0x40, 0x40); | |
| assert!( | |
| folded.findings.iter().any(|f| f.code == "ICF_ALIASING"), | |
| "expected ICF_ALIASING, got: {:?}", | |
| folded.findings | |
| ); | |
| } | |
| #[test] | |
| fn rejects_in_image_glob_dat_across_pie_symbolic_protected_and_versioned() { | |
| #[derive(Clone, Copy)] | |
| enum Case { | |
| RelativeWithVersionTagsOk, | |
| ExternalUndefGlobDatOk, | |
| InImageGlobDatExec, | |
| InImageGlobDatPie, | |
| InImageGlobDatSymbolic, | |
| InImageGlobDatProtected, | |
| InImageGlobDatVersioned, | |
| } | |
| let obj_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (o_sym, o_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "target_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| let obj = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: o_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: o_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 2, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let run_case = |case: Case| -> AuditReport { | |
| let mut l_text = obj_text.clone(); | |
| let disp = (0x3000i64 - 4 - 0x1003) as i32; | |
| l_text[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| l_text.resize(0x40, 0xc3); | |
| let external_undef = matches!(case, Case::ExternalUndefGlobDatOk); | |
| let mut lsyms = vec![SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }]; | |
| if !external_undef { | |
| lsyms.push(SymSpec { | |
| name: "target_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1020, | |
| size: 1, | |
| }); | |
| } | |
| let (l_sym, l_str) = encode_symtab(&lsyms); | |
| let (mut d_sym, d_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "f".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "target_fn".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: if external_undef { 0 } else { 1 }, | |
| value: if external_undef { 0 } else { 0x1020 }, | |
| size: if external_undef { 0 } else { 1 }, | |
| }, | |
| ]); | |
| if matches!(case, Case::InImageGlobDatProtected) { | |
| d_sym[2 * 24 + 5] = 3; // STV_PROTECTED | |
| } | |
| let rela_dyn = if matches!(case, Case::RelativeWithVersionTagsOk) { | |
| encode_rela(&[(0x3000, 0, R_X86_64_RELATIVE, 0x1020)]) | |
| } else { | |
| encode_rela(&[(0x3000, 2, R_X86_64_GLOB_DAT, 0)]) | |
| }; | |
| let mut dyn_tags = vec![ | |
| (5, 0x2180), | |
| (6, 0x2100), | |
| (10, d_str.len() as u64), | |
| (11, 24), | |
| (7, 0x2200), | |
| (8, rela_dyn.len() as u64), | |
| (9, 24), | |
| ]; | |
| match case { | |
| Case::RelativeWithVersionTagsOk | Case::InImageGlobDatVersioned => { | |
| dyn_tags.push((0x6fff_fffb, 0x0800_0000)); // DF_1_PIE | |
| dyn_tags.push((0x6fff_fff0, 0x2280)); // DT_VERSYM | |
| dyn_tags.push((0x6fff_fffc, 0x2290)); // DT_VERDEF | |
| dyn_tags.push((0x6fff_fffe, 0x22a0)); // DT_VERNEED | |
| } | |
| Case::InImageGlobDatPie => { | |
| dyn_tags.push((0x6fff_fffb, 0x0800_0000)); // DF_1_PIE | |
| } | |
| Case::InImageGlobDatSymbolic => { | |
| dyn_tags.push((16, 0)); // DT_SYMBOLIC | |
| dyn_tags.push((30, 0x2)); // DT_FLAGS = DF_SYMBOLIC | |
| } | |
| Case::ExternalUndefGlobDatOk | |
| | Case::InImageGlobDatExec | |
| | Case::InImageGlobDatProtected => {} | |
| } | |
| let dyn_bytes = encode_dyn(&dyn_tags); | |
| let dyn_len = dyn_bytes.len() as u64; | |
| let e_type = if matches!(case, Case::InImageGlobDatExec) { ET_EXEC } else { ET_DYN }; | |
| let linked = build_elf( | |
| e_type, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x300, | |
| p_memsz: 0x300, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 0x200, | |
| p_memsz: 0x200, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3100, | |
| p_vaddr: 0x3100, | |
| p_filesz: dyn_len, | |
| p_memsz: dyn_len, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: d_sym, | |
| }, | |
| SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2180, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: d_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2200, | |
| sh_link: 2, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 8, | |
| data: vec![0u8; 16], | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: 6, // SHT_DYNAMIC | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3100, | |
| sh_link: 3, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 8, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: l_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_str, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2100), (3, 0x2180), (4, 0x2200), (5, 0x3000), (6, 0x3100)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "f".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| let ok_rel = run_case(Case::RelativeWithVersionTagsOk); | |
| assert!(ok_rel.passed(), "RelativeWithVersionTagsOk findings: {:?}", ok_rel.findings); | |
| let ok_ext = run_case(Case::ExternalUndefGlobDatOk); | |
| assert!(ok_ext.passed(), "ExternalUndefGlobDatOk findings: {:?}", ok_ext.findings); | |
| for bad in [ | |
| Case::InImageGlobDatExec, | |
| Case::InImageGlobDatPie, | |
| Case::InImageGlobDatSymbolic, | |
| Case::InImageGlobDatProtected, | |
| Case::InImageGlobDatVersioned, | |
| ] { | |
| let rep = run_case(bad); | |
| assert!( | |
| rep.findings.iter().any(|f| f.code == "X86_GOT_SLOT_INVALID"), | |
| "expected X86_GOT_SLOT_INVALID, got: {:?}", | |
| rep.findings | |
| ); | |
| } | |
| } | |
| #[test] | |
| fn accepts_hidden_and_internal_visibility_local_demotion_and_rejects_ambiguity() { | |
| let patch_sym_vis = |symtab: &mut [u8], sym_idx_1based: usize, vis: u8| { | |
| symtab[sym_idx_1based * 24 + 5] = vis; | |
| }; | |
| let ro_bytes = vec![0xca, 0xfe, 0xba, 0xbe, 1, 2, 3, 4]; | |
| // 1. x86-64: entry calls same-object STV_HIDDEN helper, reads | |
| // STV_HIDDEN HCONST in .rodata, and calls cross-object STV_INTERNAL | |
| // xhelper defined in b.o (all demoted to STB_LOCAL in linked .symtab). | |
| let run_x86 = |helper_vis: u8, | |
| dup_linked_helper: bool, | |
| extra_dynsym: Option<SymSpec>| | |
| -> AuditReport { | |
| let entry_code = vec![ | |
| 0xe8, 0, 0, 0, 0, // call helper | |
| 0xe8, 0, 0, 0, 0, // call xhelper | |
| 0x48, 0x8d, 0x05, 0, 0, 0, 0, // lea rax, [rip + HCONST] | |
| 0xc3, | |
| ]; | |
| let mut obj_a_text = entry_code.clone(); | |
| let helper_off = obj_a_text.len() as u64; | |
| obj_a_text.push(0xc3); // helper | |
| let (mut a_sym, a_str) = encode_symtab(&[ | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: entry_code.len() as u64, | |
| }, | |
| SymSpec { | |
| name: "helper".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: helper_off, | |
| size: 1, | |
| }, | |
| SymSpec { | |
| name: "HCONST".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "xhelper".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 0, | |
| value: 0, | |
| size: 0, | |
| }, | |
| ]); | |
| patch_sym_vis(&mut a_sym, 2, helper_vis); | |
| patch_sym_vis(&mut a_sym, 3, STV_HIDDEN); | |
| let obj_a = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_a_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: a_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a_str, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[ | |
| (1, 2, R_X86_64_PLT32, -4), | |
| (6, 4, R_X86_64_PLT32, -4), | |
| (13, 3, R_X86_64_PC32, -4), | |
| ]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let (mut b_sym, b_str) = encode_symtab(&[SymSpec { | |
| name: "xhelper".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 1, | |
| }]); | |
| patch_sym_vis(&mut b_sym, 1, STV_INTERNAL); | |
| let obj_b = build_elf( | |
| ET_REL, | |
| EM_X86_64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: vec![0xc3], | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: b_sym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: b_str, | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut l_text = entry_code.clone(); | |
| let disp_h = (0x1020i64 - 4 - 0x1001) as i32; | |
| let disp_x = (0x1030i64 - 4 - 0x1006) as i32; | |
| let disp_ro = (0x2000i64 - 4 - 0x100d) as i32; | |
| l_text[1..5].copy_from_slice(&disp_h.to_le_bytes()); | |
| l_text[6..10].copy_from_slice(&disp_x.to_le_bytes()); | |
| l_text[13..17].copy_from_slice(&disp_ro.to_le_bytes()); | |
| l_text.resize(0x40, 0xc3); | |
| // Linked .symtab: helper, HCONST, xhelper demoted to STB_LOCAL. | |
| let mut lsyms = vec![ | |
| SymSpec { | |
| name: "helper".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1020, | |
| size: 1, | |
| }, | |
| SymSpec { | |
| name: "HCONST".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "xhelper".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1030, | |
| size: 1, | |
| }, | |
| ]; | |
| let mut sh_info = 4u32; | |
| if dup_linked_helper { | |
| lsyms.push(SymSpec { | |
| name: "helper".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1038, | |
| size: 1, | |
| }); | |
| sh_info += 1; | |
| } | |
| lsyms.push(SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: entry_code.len() as u64, | |
| }); | |
| let (ls, lstr) = encode_symtab(&lsyms); | |
| let mut secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: l_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ]; | |
| if let Some(dsym_spec) = extra_dynsym { | |
| let (ds, dstr) = encode_symtab(&[dsym_spec]); | |
| secs.push(SecSpec { | |
| name: ".dynsym".to_owned(), | |
| sh_type: SHT_DYNSYM, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2100, | |
| sh_link: 6, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ds, | |
| }); | |
| secs.push(SecSpec { | |
| name: ".dynstr".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2180, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: dstr, | |
| }); | |
| } | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 0x40, | |
| p_memsz: 0x40, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| secs, | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ | |
| ManifestSymbol { | |
| name: "entry".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ManifestSymbol { | |
| name: "helper".to_owned(), | |
| object: Some("a.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ManifestSymbol { | |
| name: "xhelper".to_owned(), | |
| object: Some("b.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ | |
| ObjectInput { label: "a.o".to_owned(), bytes: &obj_a }, | |
| ObjectInput { label: "b.o".to_owned(), bytes: &obj_b }, | |
| ], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| let ok = run_x86(STV_HIDDEN, false, None); | |
| assert!(ok.passed(), "hidden demotion should pass, got: {:?}", ok.findings); | |
| // Ambiguous duplicate linked STB_LOCAL helper fails closed. | |
| let ambig = run_x86(STV_HIDDEN, true, None); | |
| assert!(ambig.findings.iter().any(|f| f.code == "SYMBOL_MISSING_IN_LINKED")); | |
| assert!(ambig.findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // Non-hidden STV_DEFAULT helper must NOT match demoted STB_LOCAL. | |
| let non_hidden = run_x86(0, false, None); | |
| assert!(non_hidden.findings.iter().any(|f| f.code == "SYMBOL_MISSING_IN_LINKED")); | |
| assert!(non_hidden.findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // Hidden helper exported in .dynsym at the same VA fails closed. | |
| let dynsym_same_va = run_x86( | |
| STV_HIDDEN, | |
| false, | |
| Some(SymSpec { | |
| name: "helper".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1020, | |
| size: 1, | |
| }), | |
| ); | |
| assert!(dynsym_same_va.findings.iter().any(|f| f.code == "DYNSYM_MISMATCH")); | |
| assert!(dynsym_same_va.findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // Hidden helper exported in .dynsym at a non-overlapping VA fails | |
| // closed (both in audit_one_symbol and in resolve_direct_symbol_va). | |
| let dynsym_other_va = run_x86( | |
| STV_HIDDEN, | |
| false, | |
| Some(SymSpec { | |
| name: "helper".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1038, | |
| size: 1, | |
| }), | |
| ); | |
| assert!(dynsym_other_va.findings.iter().any(|f| f.code == "DYNSYM_MISMATCH")); | |
| assert!(dynsym_other_va.findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // STV_INTERNAL symbol exported in .dynsym as non-overlapping | |
| // STT_GNU_IFUNC fails closed with SYMBOL_IFUNC_FORBIDDEN. | |
| let dynsym_ifunc = run_x86( | |
| STV_HIDDEN, | |
| false, | |
| Some(SymSpec { | |
| name: "xhelper".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_GNU_IFUNC, | |
| shndx: 1, | |
| value: 0x1038, | |
| size: 1, | |
| }), | |
| ); | |
| assert!(dynsym_ifunc.findings.iter().any(|f| f.code == "SYMBOL_IFUNC_FORBIDDEN")); | |
| assert!(dynsym_ifunc.findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // STV_HIDDEN .rodata symbol exported in .dynsym at a non-overlapping | |
| // VA fails closed in verify_named_rodata_symbol. | |
| let ro_dynsym_other_va = run_x86( | |
| STV_HIDDEN, | |
| false, | |
| Some(SymSpec { | |
| name: "HCONST".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2010, | |
| size: 8, | |
| }), | |
| ); | |
| assert!(ro_dynsym_other_va.findings.iter().any(|f| f.code == "DYNSYM_MISMATCH")); | |
| // STV_HIDDEN .rodata symbol exported as non-overlapping STT_GNU_IFUNC | |
| // in .dynsym also fails closed. | |
| let ro_dynsym_ifunc = run_x86( | |
| STV_HIDDEN, | |
| false, | |
| Some(SymSpec { | |
| name: "HCONST".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_GNU_IFUNC, | |
| shndx: 2, | |
| value: 0x2010, | |
| size: 8, | |
| }), | |
| ); | |
| assert!(!ro_dynsym_ifunc.passed()); | |
| assert!(ro_dynsym_ifunc.findings.iter().any(|f| f.code == "X86_DISP32_MISMATCH")); | |
| // 2. AArch64: STV_HIDDEN callee (CALL26) and STV_HIDDEN .rodata | |
| // (ADRP+ADD) demoted to STB_LOCAL pass cleanly. | |
| let mut a64_obj_text = Vec::new(); | |
| a64_obj_text.extend_from_slice(&encode_branch26(true, 0).to_le_bytes()); | |
| a64_obj_text.extend_from_slice(&encode_adrp(0, 0).to_le_bytes()); | |
| a64_obj_text.extend_from_slice(&encode_add_imm12(0, 0, 0).to_le_bytes()); | |
| a64_obj_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| a64_obj_text.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); // helper at +16 | |
| let (mut a64_osym, a64_ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 16, | |
| }, | |
| SymSpec { | |
| name: "helper".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 16, | |
| size: 4, | |
| }, | |
| SymSpec { | |
| name: "HCONST".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| patch_sym_vis(&mut a64_osym, 2, STV_HIDDEN); | |
| patch_sym_vis(&mut a64_osym, 3, STV_HIDDEN); | |
| let a64_obj = build_elf( | |
| ET_REL, | |
| EM_AARCH64, | |
| &[], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_obj_text, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: a64_osym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[ | |
| (0, 2, R_AARCH64_CALL26, 0), | |
| (4, 3, R_AARCH64_ADR_PREL_PG_HI21, 0), | |
| (8, 3, R_AARCH64_ADD_ABS_LO12_NC, 0), | |
| ]), | |
| }, | |
| ], | |
| &[], | |
| ); | |
| let mut a64_ltext = Vec::new(); | |
| a64_ltext.extend_from_slice(&encode_branch26(true, 16).to_le_bytes()); | |
| a64_ltext.extend_from_slice(&encode_adrp(0, 0x10000).to_le_bytes()); | |
| a64_ltext.extend_from_slice(&encode_add_imm12(0, 0, 0).to_le_bytes()); | |
| a64_ltext.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| a64_ltext.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let (a64_lsym, a64_lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "helper".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x10010, | |
| size: 4, | |
| }, | |
| SymSpec { | |
| name: "HCONST".to_owned(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x20000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x10000, | |
| size: 16, | |
| }, | |
| ]); | |
| let a64_linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x10000, | |
| p_vaddr: 0x10000, | |
| p_filesz: 20, | |
| p_memsz: 20, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x20000, | |
| p_vaddr: 0x20000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x10000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_ltext, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x20000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 3, | |
| sh_entsize: 24, | |
| data: a64_lsym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: a64_lstr, | |
| }, | |
| ], | |
| &[(1, 0x10000), (2, 0x20000)], | |
| ); | |
| let a64_mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ | |
| ManifestSymbol { | |
| name: "entry".to_owned(), | |
| object: Some("a64.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ManifestSymbol { | |
| name: "helper".to_owned(), | |
| object: Some("a64.o".to_owned()), | |
| allow_syscalls: false, | |
| }, | |
| ], | |
| }; | |
| let a64_rep = audit_elf( | |
| &a64_linked, | |
| &[ObjectInput { label: "a64.o".to_owned(), bytes: &a64_obj }], | |
| &a64_mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse"); | |
| assert!(a64_rep.passed(), "AArch64 hidden demotion should pass: {:?}", a64_rep.findings); | |
| } | |
| #[test] | |
| fn rejects_prelink_relocs_targeting_verified_rodata() { | |
| const R_X86_64_64: u32 = 1; | |
| const R_AARCH64_PREL32: u32 = 261; | |
| const SHT_DYNAMIC: u32 = 6; | |
| const DT_RELA: i64 = 7; | |
| const DT_RELASZ: i64 = 8; | |
| const DT_RELAENT: i64 = 9; | |
| let make_reloc_sec = |name: &str, sh_type: u32, data: Vec<u8>| -> SecSpec { | |
| SecSpec { | |
| name: name.to_owned(), | |
| sh_type, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 2, | |
| sh_entsize: if sh_type == SHT_RELA { 24 } else { 16 }, | |
| data, | |
| } | |
| }; | |
| // 1. x86-64 direct PC32 targeting named .rodata, non-merge STT_SECTION | |
| // .rodata, and SHF_MERGE|SHF_STRINGS STT_SECTION .rodata. | |
| let run_x86_ro = |use_section_sym: bool, | |
| merge_strings: bool, | |
| ro_reloc: Option<SecSpec>| | |
| -> AuditReport { | |
| let ro_bytes = if merge_strings { b"hello\0\0\0".to_vec() } else { vec![0u8; 8] }; | |
| let obj_text = vec![0x48, 0x8d, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (osym, ostr, sh_info, rel_sym) = if use_section_sym { | |
| let (s, st) = encode_symtab(&[ | |
| SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 2, | |
| value: 0, | |
| size: 0, | |
| }, | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| (s, st, 2u32, 1u32) | |
| } else { | |
| let (s, st) = encode_symtab(&[ | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "KDATA".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| (s, st, 1u32, 2u32) | |
| }; | |
| let mut obj_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: if merge_strings { ".rodata.str1.1" } else { ".rodata" }.to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | if merge_strings { SHF_MERGE | SHF_STRINGS } else { 0 }, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: u64::from(merge_strings), | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info, | |
| sh_entsize: 24, | |
| data: osym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, rel_sym, R_X86_64_PC32, -4)]), | |
| }, | |
| ]; | |
| if let Some(rs) = ro_reloc { | |
| obj_secs.push(rs); | |
| } | |
| let obj = build_elf(ET_REL, EM_X86_64, &[], obj_secs, &[]); | |
| let mut ltext = obj_text; | |
| let disp = (0x2000i64 - 4 - 0x1003) as i32; | |
| ltext[3..7].copy_from_slice(&disp.to_le_bytes()); | |
| let mut lsyms = vec![SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }]; | |
| if !use_section_sym { | |
| lsyms.push(SymSpec { | |
| name: "KDATA".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }); | |
| } | |
| let (ls, lstr) = encode_symtab(&lsyms); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ltext, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "entry".to_owned(), | |
| object: Some("ro.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "ro.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| // Positive controls: clean .rodata (and empty 0-entry SHT_RELA) pass. | |
| assert!(run_x86_ro(false, false, None).passed()); | |
| assert!(run_x86_ro(false, false, Some(make_reloc_sec(".rela.rodata", SHT_RELA, vec![]))) | |
| .passed()); | |
| assert!(run_x86_ro(true, false, None).passed()); | |
| assert!(run_x86_ro(true, true, None).passed()); | |
| // Named .rodata with SHT_RELA, SHT_REL, or SHT_CREL fails closed. | |
| let named_rela = run_x86_ro( | |
| false, | |
| false, | |
| Some(make_reloc_sec(".rela.rodata", SHT_RELA, encode_rela(&[(0, 1, R_X86_64_64, 0)]))), | |
| ); | |
| assert!(named_rela.findings.iter().any(|f| f.code == "RODATA_RELOC_UNSUPPORTED")); | |
| let named_rel = | |
| run_x86_ro(false, false, Some(make_reloc_sec(".rel.rodata", SHT_REL, vec![0u8; 16]))); | |
| assert!(named_rel.findings.iter().any(|f| f.code == "UNSUPPORTED_RELOC_SECTION")); | |
| let named_crel = | |
| run_x86_ro(false, false, Some(make_reloc_sec(".crel.rodata", SHT_CREL, vec![0u8; 8]))); | |
| assert!(named_crel.findings.iter().any(|f| f.code == "UNSUPPORTED_RELOC_SECTION")); | |
| // Non-merge and SHF_MERGE STT_SECTION .rodata with relocs fail closed. | |
| let sec_rela = run_x86_ro( | |
| true, | |
| false, | |
| Some(make_reloc_sec( | |
| ".rela.rodata", | |
| SHT_RELA, | |
| encode_rela(&[(0, 2, R_X86_64_PC32, 0)]), | |
| )), | |
| ); | |
| assert!(sec_rela.findings.iter().any(|f| f.code == "RODATA_RELOC_UNSUPPORTED")); | |
| let sec_relr = | |
| run_x86_ro(true, false, Some(make_reloc_sec(".relr.rodata", SHT_RELR, vec![0u8; 8]))); | |
| assert!(sec_relr.findings.iter().any(|f| f.code == "UNSUPPORTED_RELOC_SECTION")); | |
| let merge_rela = run_x86_ro( | |
| true, | |
| true, | |
| Some(make_reloc_sec( | |
| ".rela.rodata.str1.1", | |
| SHT_RELA, | |
| encode_rela(&[(0, 2, R_X86_64_PC32, 0)]), | |
| )), | |
| ); | |
| assert!(merge_rela.findings.iter().any(|f| f.code == "RODATA_RELOC_UNSUPPORTED")); | |
| // 2. x86-64 GOT-referenced .rodata (verify_x86_got_slot -> | |
| // verify_named_rodata_symbol) rejects .rela.rodata fail-closed. | |
| let run_x86_got_ro = |with_ro_rela: bool| -> AuditReport { | |
| let ro_bytes = vec![0u8; 8]; | |
| let obj_text = vec![0x48, 0x8b, 0x05, 0, 0, 0, 0, 0xc3]; | |
| let (osym, ostr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "KDATA".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| let mut obj_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: obj_text.clone(), | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: osym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[(3, 2, R_X86_64_REX_GOTPCRELX, -4)]), | |
| }, | |
| ]; | |
| if with_ro_rela { | |
| obj_secs.push(make_reloc_sec( | |
| ".rela.rodata", | |
| SHT_RELA, | |
| encode_rela(&[(0, 1, R_X86_64_64, 0)]), | |
| )); | |
| } | |
| let obj = build_elf(ET_REL, EM_X86_64, &[], obj_secs, &[]); | |
| let mut ltext = obj_text; | |
| let got_disp = (0x3000i64 - 4 - 0x1003) as i32; | |
| ltext[3..7].copy_from_slice(&got_disp.to_le_bytes()); | |
| let (ls, lstr) = encode_symtab(&[ | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x1000, | |
| size: 8, | |
| }, | |
| SymSpec { | |
| name: "KDATA".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x2000, | |
| size: 8, | |
| }, | |
| ]); | |
| let rela_dyn = encode_rela(&[(0x3000, 0, R_X86_64_RELATIVE, 0x2000)]); | |
| let dyn_bytes = encode_dyn(&[ | |
| (DT_RELA, 0x2040), | |
| (DT_RELASZ, rela_dyn.len() as u64), | |
| (DT_RELAENT, 24), | |
| ]); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_X86_64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x1000, | |
| p_vaddr: 0x1000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x2000, | |
| p_vaddr: 0x2000, | |
| p_filesz: 0x80 + dyn_bytes.len() as u64, | |
| p_memsz: 0x80 + dyn_bytes.len() as u64, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0x3000, | |
| p_vaddr: 0x3000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x1000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_DYNAMIC, | |
| p_flags: PF_R, | |
| p_offset: 0x2080, | |
| p_vaddr: 0x2080, | |
| p_filesz: dyn_bytes.len() as u64, | |
| p_memsz: dyn_bytes.len() as u64, | |
| p_align: 8, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x1000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ltext, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes, | |
| }, | |
| SecSpec { | |
| name: ".rela.dyn".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2040, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 24, | |
| data: rela_dyn, | |
| }, | |
| SecSpec { | |
| name: ".dynamic".to_owned(), | |
| sh_type: SHT_DYNAMIC, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x2080, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 16, | |
| data: dyn_bytes, | |
| }, | |
| SecSpec { | |
| name: ".got".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_WRITE, | |
| sh_addr: 0x3000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 8, | |
| data: 0x2000u64.to_le_bytes().to_vec(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 7, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x1000), (2, 0x2000), (3, 0x2040), (4, 0x2080), (5, 0x3000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "entry".to_owned(), | |
| object: Some("got_ro.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "got_ro.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_x86_got_ro(false).passed()); | |
| let got_ro_rela = run_x86_got_ro(true); | |
| assert!(got_ro_rela.findings.iter().any(|f| f.code == "RODATA_RELOC_UNSUPPORTED")); | |
| // 3. AArch64 ADRP+ADD targeting named and STT_SECTION .rodata rejects | |
| // SHT_RELA (R_AARCH64_PREL32) and SHT_REL fail-closed. | |
| let run_a64_ro = |use_section_sym: bool, ro_reloc: Option<SecSpec>| -> AuditReport { | |
| let ro_bytes = vec![0u8; 8]; | |
| let mut otext = Vec::new(); | |
| otext.extend_from_slice(&encode_adrp(0, 0).to_le_bytes()); | |
| otext.extend_from_slice(&encode_add_imm12(0, 0, 0).to_le_bytes()); | |
| otext.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let (osym, ostr, sh_info, rel_sym) = if use_section_sym { | |
| let (s, st) = encode_symtab(&[ | |
| SymSpec { | |
| name: String::new(), | |
| bind: STB_LOCAL, | |
| sym_type: STT_SECTION, | |
| shndx: 2, | |
| value: 0, | |
| size: 0, | |
| }, | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 12, | |
| }, | |
| ]); | |
| (s, st, 2u32, 1u32) | |
| } else { | |
| let (s, st) = encode_symtab(&[ | |
| SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0, | |
| size: 12, | |
| }, | |
| SymSpec { | |
| name: "KDATA".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0, | |
| size: 8, | |
| }, | |
| ]); | |
| (s, st, 1u32, 2u32) | |
| }; | |
| let mut obj_secs = vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: otext, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes.clone(), | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info, | |
| sh_entsize: 24, | |
| data: osym, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ostr, | |
| }, | |
| SecSpec { | |
| name: ".rela.text".to_owned(), | |
| sh_type: SHT_RELA, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 3, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: encode_rela(&[ | |
| (0, rel_sym, R_AARCH64_ADR_PREL_PG_HI21, 0), | |
| (4, rel_sym, R_AARCH64_ADD_ABS_LO12_NC, 0), | |
| ]), | |
| }, | |
| ]; | |
| if let Some(rs) = ro_reloc { | |
| obj_secs.push(rs); | |
| } | |
| let obj = build_elf(ET_REL, EM_AARCH64, &[], obj_secs, &[]); | |
| let mut ltext = Vec::new(); | |
| ltext.extend_from_slice(&encode_adrp(0, 0x10000).to_le_bytes()); | |
| ltext.extend_from_slice(&encode_add_imm12(0, 0, 0).to_le_bytes()); | |
| ltext.extend_from_slice(&0xd65f_03c0u32.to_le_bytes()); | |
| let mut lsyms = vec![SymSpec { | |
| name: "entry".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_FUNC, | |
| shndx: 1, | |
| value: 0x10000, | |
| size: 12, | |
| }]; | |
| if !use_section_sym { | |
| lsyms.push(SymSpec { | |
| name: "KDATA".to_owned(), | |
| bind: STB_GLOBAL, | |
| sym_type: STT_OBJECT, | |
| shndx: 2, | |
| value: 0x20000, | |
| size: 8, | |
| }); | |
| } | |
| let (ls, lstr) = encode_symtab(&lsyms); | |
| let linked = build_elf( | |
| ET_DYN, | |
| EM_AARCH64, | |
| &[ | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R | PF_X, | |
| p_offset: 0x10000, | |
| p_vaddr: 0x10000, | |
| p_filesz: 12, | |
| p_memsz: 12, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_LOAD, | |
| p_flags: PF_R, | |
| p_offset: 0x20000, | |
| p_vaddr: 0x20000, | |
| p_filesz: 8, | |
| p_memsz: 8, | |
| p_align: 0x10000, | |
| }, | |
| PhdrSpec { | |
| p_type: PT_GNU_STACK, | |
| p_flags: PF_R | PF_W, | |
| p_offset: 0, | |
| p_vaddr: 0, | |
| p_filesz: 0, | |
| p_memsz: 0, | |
| p_align: 16, | |
| }, | |
| ], | |
| vec![ | |
| SecSpec { | |
| name: ".text".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC | SHF_EXECINSTR, | |
| sh_addr: 0x10000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ltext, | |
| }, | |
| SecSpec { | |
| name: ".rodata".to_owned(), | |
| sh_type: SHT_PROGBITS, | |
| sh_flags: SHF_ALLOC, | |
| sh_addr: 0x20000, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: ro_bytes, | |
| }, | |
| SecSpec { | |
| name: ".symtab".to_owned(), | |
| sh_type: SHT_SYMTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 4, | |
| sh_info: 1, | |
| sh_entsize: 24, | |
| data: ls, | |
| }, | |
| SecSpec { | |
| name: ".strtab".to_owned(), | |
| sh_type: SHT_STRTAB, | |
| sh_flags: 0, | |
| sh_addr: 0, | |
| sh_link: 0, | |
| sh_info: 0, | |
| sh_entsize: 0, | |
| data: lstr, | |
| }, | |
| ], | |
| &[(1, 0x10000), (2, 0x20000)], | |
| ); | |
| let mf = Manifest { | |
| schema_version: 1, | |
| symbols: vec![ManifestSymbol { | |
| name: "entry".to_owned(), | |
| object: Some("a64_ro.o".to_owned()), | |
| allow_syscalls: false, | |
| }], | |
| }; | |
| audit_elf( | |
| &linked, | |
| &[ObjectInput { label: "a64_ro.o".to_owned(), bytes: &obj }], | |
| &mf, | |
| &AuditConfig::default(), | |
| ) | |
| .expect("audit_elf should parse") | |
| }; | |
| assert!(run_a64_ro(false, None).passed()); | |
| assert!(run_a64_ro(true, None).passed()); | |
| let a64_named_rela = run_a64_ro( | |
| false, | |
| Some(make_reloc_sec( | |
| ".rela.rodata", | |
| SHT_RELA, | |
| encode_rela(&[(0, 1, R_AARCH64_PREL32, 0)]), | |
| )), | |
| ); | |
| assert!(a64_named_rela.findings.iter().any(|f| f.code == "RODATA_RELOC_UNSUPPORTED")); | |
| let a64_sec_rela = run_a64_ro( | |
| true, | |
| Some(make_reloc_sec( | |
| ".rela.rodata", | |
| SHT_RELA, | |
| encode_rela(&[(0, 2, R_AARCH64_PREL32, 0)]), | |
| )), | |
| ); | |
| assert!(a64_sec_rela.findings.iter().any(|f| f.code == "RODATA_RELOC_UNSUPPORTED")); | |
| let a64_sec_rel = | |
| run_a64_ro(true, Some(make_reloc_sec(".rel.rodata", SHT_REL, vec![0u8; 16]))); | |
| assert!(a64_sec_rel.findings.iter().any(|f| f.code == "UNSUPPORTED_RELOC_SECTION")); | |
| } | |
| } |