Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Analysing code at the top of the address space finishes instead of running forever (#78)

Agapenor348949ca7687
diff --git a/crates/disasm/src/program.rs b/crates/disasm/src/program.rs
index f41e4621..66a7831c 100644
--- a/crates/disasm/src/program.rs
+++ b/crates/disasm/src/program.rs
@@ -731,7 +731,9 @@ fn scan_plt(img: &Image, mode: Mode, slots: &BTreeMap<u64, SlotInfo>, stubs: &mu
let mut stub_start = a;
while a < end {
let Some(mut i) = img.decode(a, false) else {
- a += mode.align().max(1);
+ // At the top of the address space the step would wrap to 0 and walk it all again.
+ let Some(next) = a.checked_add(mode.align().max(1)) else { break };
+ a = next;
stub_start = a;
continue;
};
@@ -1030,11 +1032,13 @@ fn build_function(entry: u64, src: &'static str, sc: Scan, all_xrefs: &mut Vec<X
}
cur = Some(Block { start: a, end: a, insns: 0, succs: Vec::new(), term: Term::Fall });
}
+ // Wrapping, as `Insn::end` is: an instruction at the top of the address space ends at 0.
+ let end = a.wrapping_add(u64::from(len));
if let Some(b) = cur.as_mut() {
- b.end = a + u64::from(len);
+ b.end = end;
b.insns += 1;
}
- prev_end = Some(a + u64::from(len));
+ prev_end = Some(end);
prev_flow = flow;
prev_addr = a;
}
@@ -1076,9 +1080,9 @@ fn prologue_scan(img: &Image, mode: Mode, covered: &BTreeMap<u64, u64>) -> Vec<u
if s.name.starts_with(".plt") || s.name == "__stubs" || s.name == ".init" || s.name == ".fini" {
continue;
}
- let mut a = (s.addr + step - 1) & !(step - 1);
+ let Some(mut a) = s.addr.checked_add(step - 1).map(|a| a & !(step - 1)) else { continue };
let end = s.addr.saturating_add(s.file_size);
- while a + 4 <= end && out.len() < 200_000 {
+ while a.checked_add(4).is_some_and(|e| e <= end) && out.len() < 200_000 {
if !is_covered(a)
&& let Some(b) = img.read(a, 8)
{
@@ -1121,7 +1125,9 @@ fn prologue_scan(img: &Image, mode: Mode, covered: &BTreeMap<u64, u64>) -> Vec<u
out.push(a);
}
}
- a += step;
+ // At the top of the address space the step would wrap to 0 and walk it all again.
+ let Some(next) = a.checked_add(step) else { break };
+ a = next;
}
}
out
@@ -1150,7 +1156,7 @@ fn pointer_scan(img: &Image, limits: &Limits, functions: &BTreeMap<u64, Function
if let Some(v) = v
&& v != 0
{
- let from = s.addr + i as u64;
+ let Some(from) = s.addr.checked_add(i as u64) else { break };
if functions.contains_key(&v) {
xrefs.push(Xref { from, to: v, kind: XrefKind::Pointer });
} else if strings.contains_key(&v) {
@@ -1424,6 +1430,28 @@ mod tests {
assert!(matches!(Program::analyze(&img, &Limits::default(), &Stop), Err(DisasmError::Cancelled)));
}
+ #[test]
+ fn code_at_the_top_of_the_address_space_ends_without_wrapping() {
+ // Each scan used to step past u64::MAX: a panic in debug builds, and in release a wrap to
+ // 0 that walked the whole address space (never ending). Run on a thread with a deadline so
+ // a regression fails instead of hanging the test run.
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ use craft_anything_formats::Arch;
+ let base = u64::MAX - 63;
+ for arch in [Arch::X86, Arch::X86_64, Arch::Arm, Arch::Aarch64, Arch::X86_16] {
+ for seed in 0..4 {
+ let mut img = Image::raw(Arc::from(tk::noise(seed, 64)), arch, base);
+ let _ = Program::analyze(&img, &Limits::default(), &NoControl);
+ img.bin.sections[0].name = ".plt".into();
+ let _ = Program::analyze(&img, &Limits::default(), &NoControl);
+ }
+ }
+ let _ = tx.send(());
+ });
+ assert!(rx.recv_timeout(std::time::Duration::from_secs(60)).is_ok(), "analysis at the top of the address space never ended");
+ }
+
#[test]
fn damaged_binaries_never_panic() {
let (elf, _) = tk::elf64(tk::ElfArch::X86_64, true);
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index 8be6e1ff..f04aa926 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -147,6 +147,16 @@ Owner: open · Status: done (branch chore/r0.5-cleanups)
- [x] Capped allocation in `unreal_iostore.rs`: a `.utoc` header naming 2^31 compression methods of
length 0 collected empty names until memory ran out (6 GiB from a 559-byte file); only the
255 a block can point at are read, the rest skipped. Found by mutation fuzzing of `crates/formats`
+- [x] No endless loop on code at the top of the address space in `disasm/src/program.rs`: the
+ prologue and PLT scans stepped past `u64::MAX`, which in release wrapped to 0 and walked the
+ whole 64-bit space (`native_functions_list` on a 4 KB ELF whose `.text` is at
+ `0xffffffffffffffc0` never returned, and Stop couldn't end it). Found by mutation fuzzing of `crates/disasm`
+- [ ] `yaxpeax-arm` 0.5.0 panics ("register number out of range") on an A32 `STRD`/`LDRD` with
+ Rt = 15 (e.g. the word `0xc0c7fef3`), so one such word fails the whole analysis of a 32-bit
+ ARM program; decode it as invalid instead
+- [ ] Unchecked `o + 4`/`o + 8` on section and program header offsets in `formats/src/dart.rs`
+ (`Elf::parse`): a header table near `usize::MAX` panics in debug builds and reads the wrong
+ bytes in release
- [x] `cargo xtask layers` also scans `apps/`
- [x] Engines in `KNOWN` without a `ToolPaths` override (tshark, objdump, rizin, java)
- [x] Missing `database` icon for SQLite in the UI (`kind_icon`)