diff --git a/src/arch/mod.rs b/src/arch/mod.rs
@@ -363,4 +363,71 @@ mod tests {
other => panic!("got {other:?}"),
}
}
+
+ #[test]
+ fn aarch64_movk_merges_into_dst() {
+ let k = decode_one(Arch::Aarch64, &[0x81, 0x00, 0xb0, 0xf2], 0, "movk");
+ match k {
+ Kind::AddImm { dst, base, imm } => {
+ assert_eq!(dst, aarch64::ARG_REGS[1]);
+ assert_eq!(base, aarch64::ARG_REGS[1]);
+ assert_eq!(imm, 0x8004 << 16);
+ }
+ other => panic!("got {other:?}"),
+ }
+ }
+
+ #[test]
+ fn aarch64_sxtw_is_mov() {
+ let k = decode_one(Arch::Aarch64, &[0x02, 0x7c, 0x40, 0x93], 0, "sxtw");
+ match k {
+ Kind::Mov { dst, src } => {
+ assert_eq!(dst, aarch64::ARG_REGS[2]);
+ assert_eq!(src, aarch64::ARG_REGS[0]);
+ }
+ other => panic!("got {other:?}"),
+ }
+ }
+
+ #[test]
+ fn mips_ret_reg_is_v0_others_are_arg0() {
+ assert_eq!(ret_reg(Arch::Mips), Some(mips::V0));
+ assert_eq!(ret_reg(Arch::Arm), arg_reg(Arch::Arm, 0));
+ assert_eq!(ret_reg(Arch::Aarch64), arg_reg(Arch::Aarch64, 0));
+ }
+
+ #[test]
+ fn wide_aliases_endpoints_and_fp_lr() {
+ use capstone::arch::arm64::Arm64Reg;
+ let w = |r| aarch64::wide(r as u16);
+ assert_eq!(w(Arm64Reg::ARM64_REG_W0), Arm64Reg::ARM64_REG_X0 as u16);
+ assert_eq!(w(Arm64Reg::ARM64_REG_W28), Arm64Reg::ARM64_REG_X28 as u16);
+ assert_eq!(w(Arm64Reg::ARM64_REG_W29), Arm64Reg::ARM64_REG_X29 as u16);
+ assert_eq!(w(Arm64Reg::ARM64_REG_W30), Arm64Reg::ARM64_REG_X30 as u16);
+ }
+
+ #[test]
+ fn sweep_stitches_across_chunk_boundary() {
+ let n = 9000usize;
+ let mut bin = dummy(Arch::Aarch64, true);
+ bin.data = std::iter::repeat_n([0x1f, 0x20, 0x03, 0xd5], n)
+ .flatten()
+ .collect();
+ bin.segs = vec![crate::load::Seg {
+ vaddr: 0x1000,
+ off: 0,
+ filesz: (n * 4) as u64,
+ exec: true,
+ }];
+ let cs = build(Arch::Aarch64, true).unwrap();
+ let out = sweep(&cs, &bin);
+ assert_eq!(
+ out.len(),
+ n,
+ "chunk stitching dropped or duplicated instructions"
+ );
+ for (i, ins) in out.iter().enumerate() {
+ assert_eq!(ins.addr, 0x1000 + (i as u64) * 4);
+ }
+ }
}
diff --git a/src/graph.rs b/src/graph.rs
@@ -246,9 +246,7 @@ impl Graph {
regs.clobber_args();
regs.returned(pushed);
}
- // A return ends the frame. Reset here so a callee-saved value
- // (notably Val::Ret) cannot bleed into a following function that
- // was not detected as a start and so never triggers enter().
+ // enter() only resets at detected function starts; a return is the other frame boundary.
Kind::Ret => regs.enter(),
_ => {}
}
@@ -339,9 +337,6 @@ mod tests {
}
}
- // Func A (start 0) calls recv, returns, then an UNDETECTED func at 0x8 calls
- // memcpy with the value in x0. Without the ret reset, x0=Ret(recv) bleeds
- // across and memcpy's arg is tagged as recv's return.
fn stream() -> (Vec<Ins>, HashMap<u64, String>) {
let x0 = arg_reg(Arch::Aarch64, 0).unwrap();
let x2 = arg_reg(Arch::Aarch64, 2).unwrap();
@@ -349,7 +344,7 @@ mod tests {
Ins {
addr: 0x0,
kind: Kind::Call(0x100),
- }, // recv -> x0 = Ret
+ },
Ins {
addr: 0x4,
kind: Kind::Ret,
@@ -361,7 +356,7 @@ mod tests {
Ins {
addr: 0xc,
kind: Kind::Call(0x200),
- }, // memcpy(_, _, x2)
+ },
];
let stubs = HashMap::from([(0x100, "recv".to_string()), (0x200, "memcpy".to_string())]);
(insns, stubs)
@@ -382,4 +377,32 @@ mod tests {
"recv return leaked across ret into memcpy"
);
}
+
+ #[test]
+ fn call_return_flows_to_next_sink() {
+ let x0 = arg_reg(Arch::Aarch64, 0).unwrap();
+ let x2 = arg_reg(Arch::Aarch64, 2).unwrap();
+ let insns = vec![
+ Ins {
+ addr: 0x0,
+ kind: Kind::Call(0x100),
+ },
+ Ins {
+ addr: 0x4,
+ kind: Kind::Mov { dst: x2, src: x0 },
+ },
+ Ins {
+ addr: 0x8,
+ kind: Kind::Call(0x200),
+ },
+ ];
+ let stubs = HashMap::from([(0x100, "recv".to_string()), (0x200, "memcpy".to_string())]);
+ let g = Graph::build(&empty_bin(), &insns, stubs);
+ let memcpy = g
+ .sites
+ .iter()
+ .find(|s| s.import.as_deref() == Some("memcpy"))
+ .unwrap();
+ assert_eq!(memcpy.args.get(2), Some(&Some(Val::Ret(0))));
+ }
}
diff --git a/src/main.rs b/src/main.rs
@@ -312,3 +312,45 @@ fn main() -> Result<()> {
Cmd::Imports { path } => run_imports(&path),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use rules::{Class, Evidence, Finding};
+
+ fn templated(call: &str, template: &str) -> Finding {
+ Finding {
+ bin: "b".into(),
+ class: Class::Exec,
+ risky_call: call.into(),
+ site: 0x10,
+ func: "f".into(),
+ reach: None,
+ evidence: vec![Evidence {
+ points: 0,
+ text: format!("command template {template:?}"),
+ }],
+ score: 100,
+ }
+ }
+
+ #[test]
+ fn dedup_collapses_identical_templates_keeps_distinct() {
+ let mut v = vec![
+ templated("system", "%s"),
+ templated("system", "%s"),
+ templated("system", "%d"),
+ ];
+ dedup_templates(&mut v);
+ assert_eq!(v.len(), 2);
+ }
+
+ #[test]
+ fn dedup_keeps_findings_without_a_template() {
+ let mut v = vec![templated("system", "%s"), templated("system", "%s")];
+ v[0].evidence.clear();
+ v[1].evidence.clear();
+ dedup_templates(&mut v);
+ assert_eq!(v.len(), 2);
+ }
+}
diff --git a/src/rules.rs b/src/rules.rs
@@ -1880,6 +1880,10 @@ mod tests {
.filter(|f| f.risky_call == "memcpy")
.collect();
assert!(says(&f[0], "bounded by len_clamp()"));
+ assert!(f[0]
+ .evidence
+ .iter()
+ .any(|e| e.text.contains("bounded by len_clamp") && e.points == -20));
}
#[test]
@@ -1916,4 +1920,17 @@ mod tests {
assert_eq!(scan(&b, write(0x2000), "write")[0].class, Class::Wdt);
assert!(scan(&b, write(0x2002), "write").is_empty());
}
+
+ #[test]
+ fn watchdog_settimeout_flagged_on_all_arches_and_note_only_sums() {
+ let ioctl = |req| vec![at("ioctl", 0x1000, vec![None, Some(Val::Num(req)), None])];
+ for arch in [crate::arch::Arch::Arm, crate::arch::Arch::Mips] {
+ let b = bin(arch, &[]);
+ let f = &scan(&b, ioctl(0xc004_5706), "ioctl")[0];
+ assert_eq!(f.class, Class::Wdt);
+ assert!(says(f, "SETTIMEOUT"));
+ let sum: i32 = f.evidence.iter().map(|e| e.points).sum();
+ assert_eq!(sum, f.score);
+ }
+ }
}
| | | |