diff --git a/src/arch/mips.rs b/src/arch/mips.rs
@@ -19,6 +19,7 @@ pub const ARG_REGS: [u16; 4] = [
pub const SP: u16 = MipsReg::MIPS_REG_SP as u16;
pub const GP: u16 = MipsReg::MIPS_REG_GP as u16;
+pub const V0: u16 = MipsReg::MIPS_REG_V0 as u16;
pub fn decode(mnemonic: &str, o: &[Op; 3]) -> Option<Kind> {
match mnemonic {
diff --git a/src/arch/mod.rs b/src/arch/mod.rs
@@ -115,7 +115,8 @@ pub fn sweep(cs: &Capstone, bin: &Binary) -> Vec<Ins> {
let mut pos: u64 = 0;
let len = (end - start) as u64;
while pos < len {
- let Ok(insns) = cs.disasm_all(&code[pos as usize..], seg.vaddr + pos) else {
+ let Ok(insns) = cs.disasm_count(&code[pos as usize..], seg.vaddr + pos, SWEEP_CHUNK)
+ else {
pos += align;
continue;
};
@@ -221,6 +222,13 @@ pub fn arg_reg(arch: Arch, n: usize) -> Option<u16> {
arg_regs(arch).get(n).copied()
}
+pub fn ret_reg(arch: Arch) -> Option<u16> {
+ match arch {
+ Arch::Mips => Some(mips::V0),
+ _ => arg_reg(arch, 0),
+ }
+}
+
pub fn arg_count(arch: Arch) -> usize {
arg_regs(arch).len()
}
diff --git a/src/graph.rs b/src/graph.rs
@@ -8,6 +8,7 @@ pub enum Val {
Num(u64),
Stack(i64),
Import(u64),
+ Ret(usize),
}
pub struct CallSite {
@@ -156,6 +157,12 @@ impl<'a> Regs<'a> {
}
}
+ fn returned(&mut self, site: Option<usize>) {
+ if let Some(r) = arch::ret_reg(self.bin.arch) {
+ self.set(r, site.map(Val::Ret));
+ }
+ }
+
fn clobber_args(&mut self) {
for n in 0..self.nargs {
if let Some(r) = arch::arg_reg(self.bin.arch, n) {
@@ -222,8 +229,10 @@ impl Graph {
args: regs.args(),
});
regs.clobber_args();
+ regs.returned(Some(g.sites.len() - 1));
}
Kind::CallInd { reg } => {
+ let mut pushed = None;
if let Some(name) = regs.import_at(reg).cloned() {
g.sites.push(CallSite {
site: ins.addr,
@@ -232,9 +241,15 @@ impl Graph {
target: 0,
args: regs.args(),
});
+ pushed = Some(g.sites.len() - 1);
}
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().
+ Kind::Ret => regs.enter(),
_ => {}
}
if let Some(Val::Num(value)) = wrote {
@@ -301,3 +316,70 @@ impl Graph {
tainted
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::arch::{arg_reg, Arch, Ins, Kind};
+
+ fn empty_bin() -> Binary {
+ Binary {
+ path: "/t".into(),
+ data: vec![],
+ arch: Arch::Aarch64,
+ little: true,
+ entry: 0,
+ segs: vec![],
+ got2sym: HashMap::new(),
+ undef_stub: vec![],
+ imports: HashSet::new(),
+ exports: vec![],
+ locals: vec![],
+ gp: None,
+ }
+ }
+
+ // 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();
+ let insns = vec![
+ Ins {
+ addr: 0x0,
+ kind: Kind::Call(0x100),
+ }, // recv -> x0 = Ret
+ Ins {
+ addr: 0x4,
+ kind: Kind::Ret,
+ },
+ Ins {
+ addr: 0x8,
+ kind: Kind::Mov { dst: x2, src: x0 },
+ },
+ Ins {
+ addr: 0xc,
+ kind: Kind::Call(0x200),
+ }, // memcpy(_, _, x2)
+ ];
+ let stubs = HashMap::from([(0x100, "recv".to_string()), (0x200, "memcpy".to_string())]);
+ (insns, stubs)
+ }
+
+ #[test]
+ fn ret_stops_cross_function_ret_leak() {
+ let (insns, stubs) = stream();
+ 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(&None),
+ "recv return leaked across ret into memcpy"
+ );
+ }
+}
diff --git a/src/rules.rs b/src/rules.rs
@@ -14,6 +14,7 @@ pub enum Class {
Auth,
Priv,
Path,
+ Wdt,
}
pub const ORDER: &[Class] = &[
@@ -25,6 +26,7 @@ pub const ORDER: &[Class] = &[
Class::Auth,
Class::Priv,
Class::Path,
+ Class::Wdt,
];
impl Class {
@@ -38,6 +40,7 @@ impl Class {
Class::Auth => 30,
Class::Priv => 30,
Class::Path => 20,
+ Class::Wdt => 25,
}
}
pub fn tag(self) -> &'static str {
@@ -50,6 +53,7 @@ impl Class {
Class::Auth => "auth",
Class::Priv => "priv",
Class::Path => "path",
+ Class::Wdt => "wdt",
}
}
}
@@ -413,6 +417,12 @@ const CURL_VERIFY_OPTS: &[(u64, &str)] = &[
(249, "CURLOPT_PROXY_SSL_VERIFYHOST"),
];
const WRDE_NOCMD: u64 = 4;
+const WDIOC_SETOPTIONS: u64 = 0x8004_5704;
+const WDIOC_SETOPTIONS_MIPS: u64 = 0x4004_5704;
+const WDIOC_SETTIMEOUT: u64 = 0xc004_5706;
+const LENGTH_NOT_WRITTEN: &[&str] = &["snprintf", "vsnprintf", "strlcpy", "strlcat"];
+const BOUNDS_LENGTH: &[&str] = &["strnlen", "wcsnlen"];
+const BOUND_NAMES: &[&str] = &["clamp", "bound", "limit", "min"];
const MBEDTLS_VERIFY_NONE: u64 = 0;
const MBEDTLS_VERIFY_OPTIONAL: u64 = 1;
const PROT_WRITE_EXEC: u64 = 0o6;
@@ -748,7 +758,24 @@ impl Site<'_> {
}
}
fn unknown(&self, n: usize) -> bool {
- self.call.args.get(n).map(Option::is_none).unwrap_or(false)
+ matches!(self.call.args.get(n), Some(None | Some(Val::Ret(_))))
+ }
+ fn source(&self, n: usize) -> Option<&str> {
+ let Some(Some(Val::Ret(i))) = self.call.args.get(n) else {
+ return None;
+ };
+ let producer = self.scan.graph.sites.get(*i)?;
+ producer.import.as_deref().or_else(|| {
+ self.scan
+ .graph
+ .names
+ .get(&producer.target)
+ .map(String::as_str)
+ })
+ }
+ fn tainted(&self, n: usize) -> bool {
+ self.source(n)
+ .is_some_and(|f| UNTRUSTED_INPUTS.contains(&f))
}
fn visible(&self, n: usize) -> bool {
n < self.call.args.len()
@@ -962,7 +989,8 @@ fn sized_write(s: &Site, len: usize) -> Option<Hit> {
let dest = if s.imp == "bcopy" { 1 } else { 0 };
let stack_dest = s.stack(dest);
let measures_reachable_input = measures_input(s) && s.reach == Some(0);
- let suspicious = stack_dest.is_some() || fortify_disabled || measures_reachable_input;
+ let suspicious =
+ stack_dest.is_some() || fortify_disabled || measures_reachable_input || s.tainted(len);
if !s.unknown(len) || !suspicious {
return None;
}
@@ -1223,11 +1251,65 @@ fn classify(s: &Site) -> Option<Hit> {
temp_file(s)
} else if imp == "bind" {
unix_listener(s)
+ } else if imp == "ioctl" {
+ watchdog_ioctl(s)
+ } else if imp == "write" {
+ watchdog_magic_close(s)
} else {
None
}
}
+fn watchdog_ioctl(s: &Site) -> Option<Hit> {
+ let setoptions = match s.scan.bin.arch {
+ crate::arch::Arch::Mips => WDIOC_SETOPTIONS_MIPS,
+ _ => WDIOC_SETOPTIONS,
+ };
+ let (adjust, why) = match s.num(1)? {
+ n if n == setoptions => (
+ 15,
+ "WDIOC_SETOPTIONS: WDIOS_DISABLECARD disarms the watchdog",
+ ),
+ WDIOC_SETTIMEOUT => (5, "WDIOC_SETTIMEOUT: a huge timeout is a disarmed watchdog"),
+ _ => return None,
+ };
+ let mut h = Hit::new(Class::Wdt, adjust);
+ h.note(why);
+ Some(h)
+}
+
+fn watchdog_magic_close(s: &Site) -> Option<Hit> {
+ if s.text(1).as_deref() != Some("V") {
+ return None;
+ }
+ let mut h = Hit::new(Class::Wdt, 10);
+ h.note("magic close: writing 'V' disarms the watchdog when this fd closes (unless nowayout)");
+ Some(h)
+}
+
+fn is_bound_name(name: &str) -> bool {
+ let name = name.to_ascii_lowercase();
+ BOUND_NAMES.iter().any(|k| name.contains(k))
+}
+
+fn note_provenance(s: &Site, h: &mut Hit) {
+ for n in 0..s.call.args.len() {
+ let Some(src) = s.source(n) else { continue };
+ if UNTRUSTED_INPUTS.contains(&src) {
+ h.bump(15, format!("arg {n} is the raw return of {src}()"));
+ } else if LENGTH_NOT_WRITTEN.contains(&src) {
+ h.bump(
+ 15,
+ format!(
+ "arg {n} is the return of {src}(): the untruncated length, not bytes written"
+ ),
+ );
+ } else if BOUNDS_LENGTH.contains(&src) || is_bound_name(src) {
+ h.bump(-20, format!("arg {n} is bounded by {src}()"));
+ }
+ }
+}
+
fn note_sanitizers(s: &Site, h: &mut Hit) {
if matches!(h.class, Class::Ipc | Class::Tls) {
return;
@@ -1313,6 +1395,7 @@ fn adjust(s: &Site, h: &mut Hit) {
note_sanitizers(s, h);
note_function_strings(s, h);
note_cgi_variable(s, h);
+ note_provenance(s, h);
if s.scan.wrappers.contains(&s.call.func) && h.class != Class::Exec {
h.bump(10, "inside a shell-equivalent wrapper");
}
@@ -1599,7 +1682,13 @@ mod tests {
}
fn site(import: &str, args: Vec<Option<Val>>) -> CallSite {
- CallSite { site: 0x1000, func: 0, import: Some(import.into()), target: 0, args }
+ CallSite {
+ site: 0x1000,
+ func: 0,
+ import: Some(import.into()),
+ target: 0,
+ args,
+ }
}
#[test]
@@ -1627,7 +1716,7 @@ mod tests {
let findings = analyze(&b, &g);
assert!(!findings
.iter()
- .any(|f| f.evidence.iter().any(|e| e.contains("no SO_PEERCRED"))));
+ .any(|f| f.evidence.iter().any(|e| e.text.contains("no SO_PEERCRED"))));
}
#[test]
@@ -1639,7 +1728,7 @@ mod tests {
let findings = analyze(&b, &g);
assert!(findings
.iter()
- .any(|f| f.evidence.iter().any(|e| e.contains("no SO_PEERCRED"))));
+ .any(|f| f.evidence.iter().any(|e| e.text.contains("no SO_PEERCRED"))));
}
#[test]
| | | |