Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can β€” learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company πŸ‘‡

fix(lisdex): M7.0 - bound the symbolizer wait, and the first control was inert

CarterPerez-dev757573ad22d4
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/src/symbolize.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/src/symbolize.rs
index 2c20c38b..1a671f01 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/src/symbolize.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/src/symbolize.rs
@@ -2,15 +2,20 @@
// symbolize.rs
use std::collections::BTreeMap;
-use std::io::Write;
+use std::io::{Read, Write};
use std::path::Path;
-use std::process::{Command, Stdio};
+use std::process::{Child, Command, Stdio};
+use std::thread;
+use std::time::{Duration, Instant};
pub const LLVM_SYMBOLIZER_BIN: &str = "llvm-symbolizer";
pub const DEBUGINFOD_URLS: &str = "DEBUGINFOD_URLS";
pub const DEBUGINFOD_DISABLED: &str = "";
+pub const SYMBOLIZER_TIMEOUT: Duration = Duration::from_secs(30);
+const SYMBOLIZER_POLL: Duration = Duration::from_millis(10);
+
const UNKNOWN_SYMBOL: &str = "??";
const FRAME_PREFIX: char = '#';
const MODULE_OFFSET_MARKER: &str = "+0x";
@@ -170,26 +175,72 @@ fn symbolizer_command(module: &str) -> Command {
}
fn run_symbolizer(module: &str, offsets: &[u64]) -> Option<BTreeMap<(String, u64), Symbol>> {
- let mut child = symbolizer_command(module).spawn().ok()?;
+ let child = symbolizer_command(module).spawn().ok()?;
+ let text = drive_symbolizer(child, offsets, SYMBOLIZER_TIMEOUT)?;
+ Some(parse_symbolizer_output(&text, offsets, module))
+}
+
+fn reap(child: &mut Child) {
+ let _ = child.kill();
+ let _ = child.wait();
+}
+
+fn drive_symbolizer(mut child: Child, offsets: &[u64], timeout: Duration) -> Option<String> {
+ let Some(mut stdin) = child.stdin.take() else {
+ reap(&mut child);
+ return None;
+ };
+ for offset in offsets {
+ if writeln!(stdin, "0x{offset:x}").is_err() {
+ break;
+ }
+ }
+ drop(stdin);
+
+ let Some(mut stdout) = child.stdout.take() else {
+ reap(&mut child);
+ return None;
+ };
+ let reader = thread::spawn(move || {
+ let mut text = String::new();
+ let _ = stdout.read_to_string(&mut text);
+ text
+ });
- {
- let stdin = child.stdin.as_mut()?;
- for offset in offsets {
- writeln!(stdin, "0x{offset:x}").ok()?;
+ let deadline = Instant::now() + timeout;
+ loop {
+ match child.try_wait() {
+ Ok(Some(_)) => break,
+ Ok(None) => {
+ if Instant::now() >= deadline {
+ reap(&mut child);
+ break;
+ }
+ thread::sleep(SYMBOLIZER_POLL);
+ }
+ Err(_) => {
+ reap(&mut child);
+ break;
+ }
}
}
- let output = child.wait_with_output().ok()?;
- let text = String::from_utf8_lossy(&output.stdout);
- Some(parse_symbolizer_output(&text, offsets, module))
+ reader.join().ok()
}
#[cfg(test)]
mod tests {
use std::ffi::OsStr;
+ use std::sync::mpsc;
use super::*;
+ const SETTLE_TIMEOUT: Duration = Duration::from_secs(5);
+ const SETTLE_BUDGET: Duration = Duration::from_secs(2);
+ const NEVER_EXITS_ARG: &str = "60";
+ const NEVER_EXITS_TIMEOUT: Duration = Duration::from_millis(100);
+ const RETURN_BUDGET: Duration = Duration::from_secs(10);
+
const RAW: &str = "\
==38182==ERROR: AddressSanitizer: SEGV on unknown address 0x9526 (pc 0x7fb bp 0x7ff sp 0x7ff T0)
#0 0x7f66678b253b (/lib/x86_64-linux-gnu/libc.so.6+0x4553b) (BuildId: 8e9f)
@@ -268,6 +319,49 @@ mod tests {
assert_eq!(symbolize_report("nothing here\n"), "nothing here\n");
}
+ fn piped(program: &str, arg: Option<&str>) -> Option<Child> {
+ let mut command = Command::new(program);
+ if let Some(arg) = arg {
+ command.arg(arg);
+ }
+ command
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::null())
+ .spawn()
+ .ok()
+ }
+
+ #[test]
+ fn the_child_sees_end_of_input_and_exits_on_its_own() {
+ let Some(child) = piped("cat", None) else {
+ return;
+ };
+ let started = Instant::now();
+ let text = drive_symbolizer(child, &[0x10, 0x20], SETTLE_TIMEOUT)
+ .expect("the reader thread must return the child's output");
+ let elapsed = started.elapsed();
+ assert_eq!(text, "0x10\n0x20\n", "the child's output must be collected");
+ assert!(
+ elapsed < SETTLE_BUDGET,
+ "the child was still waiting for input and had to be killed at the deadline after {elapsed:?}, so stdin is not being closed; output alone cannot detect this because cat writes with raw syscalls and flushes before it dies"
+ );
+ }
+
+ #[test]
+ fn a_child_that_never_exits_is_killed_instead_of_waited_on() {
+ let (sender, receiver) = mpsc::channel();
+ thread::spawn(move || {
+ let outcome = piped("sleep", Some(NEVER_EXITS_ARG))
+ .map(|child| drive_symbolizer(child, &[0x10], NEVER_EXITS_TIMEOUT));
+ let _ = sender.send(outcome);
+ });
+ assert!(
+ receiver.recv_timeout(RETURN_BUDGET).is_ok(),
+ "drive_symbolizer never returned: the deadline is not enforced and the crash path can hang forever"
+ );
+ }
+
#[test]
fn the_symbolizer_never_inherits_a_debuginfod_server() {
let command = symbolizer_command("/opt/out/harness");