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 đŸ‘‡

feat(lisdex): B8 - the crash report was cut by a pipe, not by the cap it checked

CarterPerez-dev14a0ec6b672d
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/process.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/process.rs
index edc9c557..033a3fec 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/process.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-core/src/process.rs
@@ -13,7 +13,7 @@ const POLL_MS: u64 = 25;
const ENV_PATH: &str = "PATH";
const EXECUTABLE_BITS: u32 = 0o111;
const DRAIN_CHUNK_BYTES: usize = 16 * 1024;
-const DRAIN_GRACE_MS: u64 = 2000;
+pub const DRAIN_GRACE_MS: u64 = 2000;
const KILL_BIN: &str = "kill";
const SIGNAL_KILL: &str = "-KILL";
const SCRUBBED_PREFIXES: [&str; 4] = ["AFL_", "ASAN_", "UBSAN_", "MSAN_"];
@@ -188,9 +188,12 @@ pub fn resolve_on_path(program: &str) -> Option<PathBuf> {
})
}
-type Captured = std::sync::Arc<std::sync::Mutex<Vec<u8>>>;
+pub type Captured = std::sync::Arc<std::sync::Mutex<Vec<u8>>>;
-fn drain(mut stream: impl Read + Send + 'static) -> (Captured, std::thread::JoinHandle<()>) {
+pub fn drain(
+ mut stream: impl Read + Send + 'static,
+ limit: usize,
+) -> (Captured, std::thread::JoinHandle<()>) {
let captured: Captured = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let sink = std::sync::Arc::clone(&captured);
let handle = std::thread::spawn(move || {
@@ -200,7 +203,7 @@ fn drain(mut stream: impl Read + Send + 'static) -> (Captured, std::thread::Join
Ok(0) | Err(_) => break,
Ok(read) => {
let Ok(mut buffer) = sink.lock() else { break };
- let room = MAX_CAPTURED_BYTES.saturating_sub(buffer.len());
+ let room = limit.saturating_sub(buffer.len());
if room > 0 {
buffer.extend_from_slice(&chunk[..read.min(room)]);
}
@@ -211,7 +214,8 @@ fn drain(mut stream: impl Read + Send + 'static) -> (Captured, std::thread::Join
(captured, handle)
}
-fn take_captured(captured: &Captured) -> Vec<u8> {
+#[must_use]
+pub fn take_captured(captured: &Captured) -> Vec<u8> {
captured
.lock()
.map(|buffer| buffer.clone())
@@ -272,8 +276,14 @@ pub fn run(spec: &CommandSpec, timeout: Duration) -> Result<Execution, ProcessEr
source,
})?;
- let stdout = child.stdout.take().map(drain);
- let stderr = child.stderr.take().map(drain);
+ let stdout = child
+ .stdout
+ .take()
+ .map(|stream| drain(stream, MAX_CAPTURED_BYTES));
+ let stderr = child
+ .stderr
+ .take()
+ .map(|stream| drain(stream, MAX_CAPTURED_BYTES));
let mut timed_out = false;
let status = loop {
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/src/runner.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/src/runner.rs
index 40a09b2c..f3d13f10 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/src/runner.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/src/runner.rs
@@ -1,7 +1,7 @@
// ©AngelaMos | 2026
// runner.rs
-use std::io::Read;
+use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};
@@ -12,6 +12,7 @@ use lisdex_core::config::{
REPRODUCE_SANITIZER_OPTIONS as SANITIZER_OPTIONS,
};
use lisdex_core::crash::Crash;
+use lisdex_core::process;
use lisdex_core::report::SanitizerReport;
use crate::dictionary;
@@ -132,6 +133,7 @@ pub struct Reproduction {
pub output: String,
pub timed_out: bool,
pub truncated: bool,
+ pub lossy: bool,
}
#[derive(Debug, Clone, PartialEq)]
@@ -372,6 +374,11 @@ pub fn reproduce(
.stderr(Stdio::piped())
.spawn()?;
+ let captured = child
+ .stderr
+ .take()
+ .map(|stream| process::drain(stream, MAX_REPORT_BYTES));
+
let started = Instant::now();
let mut timed_out = false;
loop {
@@ -388,19 +395,23 @@ pub fn reproduce(
}
let mut raw = Vec::new();
- if let Some(mut stderr) = child.stderr.take() {
- stderr
- .by_ref()
- .take(MAX_REPORT_BYTES as u64)
- .read_to_end(&mut raw)?;
+ if let Some((buffer, handle)) = captured {
+ let deadline = Instant::now() + Duration::from_millis(process::DRAIN_GRACE_MS);
+ while !handle.is_finished() && Instant::now() < deadline {
+ std::thread::sleep(Duration::from_millis(REPRODUCE_POLL_MS));
+ }
+ raw = process::take_captured(&buffer);
}
let truncated = raw.len() >= MAX_REPORT_BYTES;
- let raw_text = String::from_utf8_lossy(&raw).into_owned();
+ let decoded = String::from_utf8_lossy(&raw);
+ let lossy = matches!(decoded, Cow::Owned(_));
+ let raw_text = decoded.into_owned();
Ok(Reproduction {
output: crate::symbolize::symbolize_report(&raw_text),
timed_out,
truncated,
+ lossy,
})
}
@@ -435,6 +446,12 @@ pub fn harvest(
input_path.display()
));
}
+ if reproduction.lossy {
+ problems.push(format!(
+ "{}: report held bytes that are not UTF-8 and they were replaced",
+ input_path.display()
+ ));
+ }
if reproduction.truncated {
problems.push(format!("{}: report truncated", input_path.display()));
}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/tests/reproduce_capture.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/tests/reproduce_capture.rs
new file mode 100644
index 00000000..deb79820
--- /dev/null
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-fuzz/tests/reproduce_capture.rs
@@ -0,0 +1,166 @@
+// ©AngelaMos | 2026
+// reproduce_capture.rs
+
+use std::path::PathBuf;
+use std::process::Command;
+use std::time::{Duration, Instant};
+
+use lisdex_core::scratch;
+use lisdex_fuzz::runner::{self, MAX_REPORT_BYTES};
+
+const CHATTY_SOURCE: &str = r#"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char **argv) {
+ if (argc < 2) { return 1; }
+ FILE *f = fopen(argv[1], "rb");
+ if (!f) { return 1; }
+ char buf[64];
+ memset(buf, 0, sizeof buf);
+ if (!fgets(buf, sizeof buf, f)) { fclose(f); return 1; }
+ fclose(f);
+ long n = atol(buf);
+ int raw = (strchr(buf, 'r') != NULL);
+ for (long i = 0; i < n; i++) {
+ fputc(raw ? 0xC3 : ('A' + (int)(i % 26)), stderr);
+ }
+ fflush(stderr);
+ abort();
+}
+"#;
+
+const REPRODUCE_TIMEOUT: Duration = Duration::from_secs(10);
+const SMALL_REPORT_BYTES: usize = 512;
+
+struct Lab {
+ root: PathBuf,
+ target: PathBuf,
+}
+
+impl Lab {
+ fn new(label: &str) -> Self {
+ let root = scratch::path(label);
+ let _ = std::fs::remove_dir_all(&root);
+ std::fs::create_dir_all(&root).unwrap();
+
+ let source = root.join("chatty.c");
+ std::fs::write(&source, CHATTY_SOURCE).unwrap();
+
+ let target = root.join("chatty");
+ let status = Command::new("cc")
+ .arg("-O0")
+ .arg("-o")
+ .arg(&target)
+ .arg(&source)
+ .status()
+ .unwrap();
+ assert!(status.success(), "cc failed to build the chatty target");
+
+ Self { root, target }
+ }
+
+ fn run(&self, name: &str, bytes: usize, raw: bool) -> (runner::Reproduction, Duration) {
+ let input = self.root.join(name);
+ let marker = if raw { "r" } else { "" };
+ std::fs::write(&input, format!("{bytes}{marker}\n")).unwrap();
+ let started = Instant::now();
+ let reproduction = runner::reproduce(&self.target, &input, REPRODUCE_TIMEOUT).unwrap();
+ (reproduction, started.elapsed())
+ }
+}
+
+const LARGEST_LIKELY_PIPE_BYTES: usize = 64 * 1024;
+
+#[test]
+fn a_report_larger_than_the_stderr_pipe_is_captured_whole_and_is_not_a_hang() {
+ let lab = Lab::new("reproduce-capture-overflow");
+ let bytes = 200_000;
+ assert!(
+ bytes > LARGEST_LIKELY_PIPE_BYTES && bytes < MAX_REPORT_BYTES,
+ "the probe must clear the pipe and stay under the cap"
+ );
+
+ let (reproduction, elapsed) = lab.run("big", bytes, false);
+
+ assert!(
+ !reproduction.timed_out,
+ "a target that emitted {bytes} bytes and exited was reported as a hang: stderr is not \
+ drained while the child runs, so the child blocked on a full pipe"
+ );
+ assert_eq!(
+ reproduction.output.len(),
+ bytes,
+ "captured {} of {bytes} emitted bytes",
+ reproduction.output.len()
+ );
+ assert!(
+ !reproduction.truncated,
+ "a report under the cap must not be flagged truncated"
+ );
+ assert!(
+ elapsed < REPRODUCE_TIMEOUT,
+ "reproduction burned the whole timeout: {elapsed:?}"
+ );
+}
+
+#[test]
+fn a_report_over_the_cap_is_flagged_truncated_and_still_lets_the_target_exit() {
+ let lab = Lab::new("reproduce-capture-capped");
+ let bytes = MAX_REPORT_BYTES + 100_000;
+
+ let (reproduction, elapsed) = lab.run("huge", bytes, false);
+
+ assert!(
+ reproduction.truncated,
+ "a {bytes}-byte report exceeds the {MAX_REPORT_BYTES}-byte cap and must say so"
+ );
+ assert!(
+ !reproduction.timed_out,
+ "an over-cap report is not a hang: without a concurrent drain the child blocks on a full \
+ pipe long before it reaches the cap"
+ );
+ assert_eq!(
+ reproduction.output.len(),
+ MAX_REPORT_BYTES,
+ "an over-cap report must be stored at exactly the cap"
+ );
+ assert!(
+ elapsed < REPRODUCE_TIMEOUT,
+ "reproduction burned the whole timeout: {elapsed:?}"
+ );
+}
+
+#[test]
+fn a_small_clean_report_is_captured_verbatim() {
+ let lab = Lab::new("reproduce-capture-small");
+
+ let (reproduction, _) = lab.run("small", SMALL_REPORT_BYTES, false);
+
+ assert!(!reproduction.timed_out);
+ assert!(!reproduction.truncated);
+ assert!(!reproduction.lossy);
+ assert_eq!(reproduction.output.len(), SMALL_REPORT_BYTES);
+}
+
+#[test]
+fn a_report_that_is_not_utf8_says_so_instead_of_substituting_in_silence() {
+ let lab = Lab::new("reproduce-capture-lossy");
+
+ let (reproduction, _) = lab.run("raw", SMALL_REPORT_BYTES, true);
+
+ assert!(
+ reproduction.lossy,
+ "the target emitted {SMALL_REPORT_BYTES} bytes that are not UTF-8 and every one was \
+ replaced, so the stored report is not what the target wrote"
+ );
+ assert!(
+ !reproduction.timed_out,
+ "a small report must not hang regardless of its bytes"
+ );
+ assert!(
+ !reproduction.truncated,
+ "the lossy flag must not be conflated with the truncation flag"
+ );
+}