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 π
Go aicertificationcloud-engineeringcybercybersecuritycybersecurity-analystcybersecurity-certificationcybersecurity-certificationscybersecurity-educationcybersecurity-jobscybersecurity-portfoliocybersecurity-projectscybersecurity-toolsgrchackinglearningpentestingproject-repositoryprojetcspython
fix(lisdex): B74 + B75 - the harness that resolved every argument and still ignored its input
CarterPerez-devd2dda6f5d5cd
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/plan.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/plan.rsindex b994a599..49f9ae5d 100644--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/plan.rs+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/plan.rs@@ -35,6 +35,19 @@ pub enum Value {OutCapacity,}+impl Value {+ // The corpus is the BYTES, so a length alone does not count. `OutBuffer`/`OutCapacity`+ // describe memory the harness allocated and are the two values that made the defect+ // below look like a bound argument.+ #[must_use]+ pub fn carries_corpus(&self) -> bool {+ matches!(+ self,+ Value::Data { .. } | Value::Cursor | Value::NulTerminated+ )+ }+}+#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Init {Uninitialised,@@ -155,12 +168,93 @@ impl HarnessPlan {self.body.push(statement);self}++ // A harness whose finished body never mentions the fuzzer's input is not a harness, it+ // is a constant program: every seed drives identical coverage and any crash it reports+ // is one we manufactured. libarchive's `archive_read_data(struct archive *, void *,+ // size_t)` reached rank 1 and EMITTED, because `struct archive` is a stream record so+ // the signature grades StreamDriven, and StreamDriven means the corpus arrives through+ // some other call - which the replay then failed to bring. The six calls it did replay+ // are all handle plumbing and the emitted C referenced neither `data` nor `size`.+ //+ // The test belongs on the finished body rather than on the signature, because whether+ // the corpus arrives at all is decided by what the replay brought with it, and that is+ // not knowable until the plan is assembled. A cursor-reading callback counts: it is how+ // a genuinely stream-driven harness feeds the library.+ #[must_use]+ pub fn binds_corpus(&self) -> bool {+ if self.needs_cursor || self.needs_nul_copy {+ return true;+ }+ self.body.iter().any(|statement| match statement {+ Stmt::Call { args, .. } => args.iter().any(Value::carries_corpus),+ Stmt::SetField { value, .. } => value.carries_corpus(),+ _ => false,+ })+ }}#[cfg(test)]mod tests {use super::*;+ // The buffer and the capacity both belong to the HARNESS, so a body built only from+ // them is a constant program however many arguments it resolved. This is the exact+ // argument list `archive_read_data` emitted.+ #[test]+ fn a_body_holding_only_the_harnesss_own_buffer_does_not_bind_the_corpus() {+ let plan = HarnessPlan::new("archive_read_data").with(Stmt::Call {+ function: "archive_read_data".to_owned(),+ args: vec![+ Value::Local("ctx".to_owned()),+ Value::OutBuffer { cast: None },+ Value::OutCapacity,+ ],+ capture: None,+ });+ assert!(!plan.binds_corpus());+ }++ // Isolates the value test from the statement walk: same shape, one argument swapped.+ #[test]+ fn the_same_body_binds_the_corpus_once_one_argument_is_the_data() {+ let plan = HarnessPlan::new("archive_read_open_memory").with(Stmt::Call {+ function: "archive_read_open_memory".to_owned(),+ args: vec![+ Value::Local("ctx".to_owned()),+ Value::Data { cast: None },+ Value::Size,+ ],+ capture: None,+ });+ assert!(plan.binds_corpus());+ }++ // Isolates the statement walk from the value test: zlib's `z_stream` shape hands the+ // corpus over by assignment, never as an argument, so a walk that only read Call args+ // would refuse every stream decoder in the set.+ #[test]+ fn a_corpus_delivered_by_field_assignment_still_counts_as_bound() {+ let plan = HarnessPlan::new("inflate").with(Stmt::SetField {+ local: "ctx".to_owned(),+ field: "next_in".to_owned(),+ value: Value::Data { cast: None },+ });+ assert!(plan.binds_corpus());+ }++ // A length carries no bytes. Counting it would have let `archive_read_data` through on+ // its capacity argument the moment that argument was spelled `Size` instead.+ #[test]+ fn the_corpus_length_on_its_own_is_not_the_corpus() {+ let plan = HarnessPlan::new("configure").with(Stmt::Call {+ function: "configure".to_owned(),+ args: vec![Value::Local("ctx".to_owned()), Value::Size],+ capture: None,+ });+ assert!(!plan.binds_corpus());+ }+#[test]fn a_read_callback_forces_the_cursor_in() {let plan = HarnessPlan::new("png_read_info").with_callback(CallbackStub {diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/synth.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/synth.rsindex 3da6de97..c1a0f4d5 100644--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/synth.rs+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/synth.rs@@ -28,6 +28,7 @@ pub const FILESYSTEM_VERBS: [&str; 12] = [#[derive(Debug, Clone, PartialEq, Eq)]pub enum Refusal {NoDataInput,+ CorpusNeverBound,UnsizedOutParam { param: String },IndirectExtent { param: String },OpaqueContext { record: String },@@ -45,6 +46,10 @@ impl std::fmt::Display for Refusal {fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {match self {Refusal::NoDataInput => write!(formatter, "consumes no fuzzable input"),+ Refusal::CorpusNeverBound => write!(+ formatter,+ "the assembled harness never passes the corpus to the library, so every seed drives the same code and any crash it reports is one the harness manufactured"+ ),Refusal::UnsizedOutParam { param } => write!(formatter,"out-param {param} needs a caller-supplied extent that headers cannot give; guessing it fabricates a crash inside the library"@@ -861,7 +866,7 @@ pub fn plan_candidate_with(plan};- let plans = variant_masks(bindings.len())+ let plans: Vec<HarnessPlan> = variant_masks(bindings.len()).into_iter().map(|mask| {let mut plan = base.clone();@@ -878,6 +883,14 @@ pub fn plan_candidate_with(call(plan)}).collect();++ // Every variant shares the same arguments and the same replayed setup, so they either+ // all reach the library with the corpus or none of them does. Asserted over all of+ // them anyway, because a variant differing here would mean the mask changed an+ // argument, and that is a defect worth failing on rather than shipping.+ if !plans.iter().all(HarnessPlan::binds_corpus) {+ return Err(Refusal::CorpusNeverBound);+ }Ok(plans)}@@ -1290,6 +1303,74 @@ mod tests {);}+ // `archive_read_data(struct archive *, void *, size_t)` is the shape that shipped. Its+ // handle is a stream record so the signature grades StreamDriven, which says the corpus+ // arrives through some OTHER call - and nothing made the replay bring one. Every+ // argument then resolved: a produced handle, an out buffer the harness allocated, and+ // the capacity it allocated. The emitted C referenced neither `data` nor `size`.+ fn archive_read_data() -> Candidate {+ candidate(+ "archive_read_data",+ vec![+ param(+ "a",+ "struct archive *",+ Shape::Pointer {+ pointee: Pointee::Record("archive".to_owned()),+ is_const: false,+ },+ ),+ param(+ "buff",+ "void *",+ Shape::Pointer {+ pointee: Pointee::Void,+ is_const: false,+ },+ ),+ param("len", "size_t", Shape::Integral),+ ],+ vec![ParamRole::Context, ParamRole::Output, ParamRole::Length],+ Fuzzability::StreamDriven,+ Returns::default(),+ )+ }++ #[test]+ fn a_plan_whose_every_argument_resolves_is_still_refused_when_none_of_them_is_the_corpus() {+ assert_eq!(+ plan_candidate(&archive_read_data(), "archive.h", &archive_vocabulary()),+ Err(Refusal::CorpusNeverBound),+ "this planned and EMITTED at 7f1bb283: six format registrations, an out buffer, \+ and an entry call that never saw the fuzzer's bytes"+ );+ }++ // The isolating control for the rule above. Refusing on "the signature has an Output"+ // or on "the candidate is StreamDriven" would reject this too, and this is the entry+ // point the M3 gate asserts ranks first - so the two tests only pass together if the+ // rule is keyed on the assembled body rather than on the shape.+ #[test]+ fn the_same_handle_and_the_same_stream_still_plan_when_one_argument_is_the_corpus() {+ let plans = plan_candidate(+ &archive_open(param(+ "buff",+ "const void *",+ Shape::Pointer {+ pointee: Pointee::Void,+ is_const: true,+ },+ )),+ "archive.h",+ &archive_vocabulary(),+ )+ .unwrap();+ assert!(+ plans[0].binds_corpus(),+ "archive_read_open_memory hands the library `data` and must not be refused"+ );+ }+#[test]fn a_typedef_hiding_a_void_pointer_is_keyed_on_the_typedef_not_the_void() {let plans = plan_candidate(diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/harness.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/harness.rsindex 66b0e7cf..f2562933 100644--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/harness.rs+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/harness.rs@@ -70,7 +70,15 @@ fn is_source(path: &Path) -> bool {// libjpeg-turbo target carried `Copyright`, `DAMAGES` and `TORT` in its call list. That is// not merely noise in the length and frequency keys: a comment naming a real function would// have made it "authored", and authorship is what decides the floor.-fn without_comments(body: &str) -> String {+//+// Stripping only comments left the identical defect alive in its other form. zlib's+// `examples/gun.c` writes `fprintf(stderr, "Copyright (C) 2003-2010 Mark Adler\n")`, and the+// text INSIDE that string literal mines as a call to `Copyright` exactly as the licence+// header did - it is the fourth entry in the sequence the ladder chose for zlib. A usage or+// error string naming a real library function is the case that matters, because it would be+// replayed as setup. A literal cannot span a raw newline, so an unterminated quote stops at+// the end of its line rather than eating the rest of the file.+fn without_comments_or_literals(body: &str) -> String {let chars: Vec<char> = body.chars().collect();let mut kept = String::with_capacity(body.len());let mut index = 0;@@ -92,6 +100,17 @@ fn without_comments(body: &str) -> String {index += 1;}}+ ('"' | '\'', _) => {+ let quote = chars[index];+ index += 1;+ while index < chars.len() && chars[index] != quote && chars[index] != '\n' {+ index += if chars[index] == '\\' { 2 } else { 1 };+ }+ if chars.get(index) == Some("e) {+ index += 1;+ }+ kept.push(' ');+ }_ => {kept.push(chars[index]);index += 1;@@ -183,7 +202,7 @@ fn find_identifier(chars: &[char], name: &[char], from: usize) -> Option<usize>// first. Dedup on FIRST occurrence, because that is where the author established the order.#[must_use]pub fn extract_calls(body: &str) -> Vec<String> {- let body = without_comments(body);+ let body = without_comments_or_literals(body);let chars: Vec<char> = body.chars().collect();let mut seen = BTreeSet::new();let mut found = Vec::new();@@ -348,6 +367,37 @@ mod order_tests {);}+ // zlib's `examples/gun.c` is the tree this was measured on: `Copyright` was mined as the+ // fourth call in the sequence the ladder chose, out of the text of an fprintf argument.+ // The fixture names a REAL library function inside the literal, because that is the case+ // that costs something - an unreal name is only noise in the length key, but a real one+ // is replayed as setup and votes on the direction verdict.+ #[test]+ fn a_function_name_inside_a_string_literal_is_not_a_call() {+ let calls = extract_calls(+ r#"+ int main(void) {+ fprintf(stderr, "Copyright (C) 2003-2010 Mark Adler\n");+ fputs("usage: inflateInit(strm) then inflate(strm)\n", stderr);+ inflateEnd(&strm);+ }+ "#,+ );+ assert_eq!(+ calls,+ vec!["main", "fprintf", "fputs", "inflateEnd"],+ "Copyright, inflateInit and inflate are all text, not calls"+ );+ }++ // Isolates the literal strip from the comment strip: an escaped quote must not end the+ // literal early, or the tail of the string leaks back in as callable text.+ #[test]+ fn an_escaped_quote_does_not_end_the_literal_early() {+ let calls = extract_calls(r#"void f(void) { puts("a \" inflate(x) b"); }"#);+ assert_eq!(calls, vec!["f", "puts"]);+ }+#[test]fn a_call_repeated_later_keeps_only_its_first_position() {let calls = extract_calls("zulu(); alpha(); zulu();");diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.shindex 707e08a9..1f94a703 100755--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh@@ -322,6 +322,39 @@ elseok "only handle-taking registrations are replayed, no teardown and no string-taking call"fi+# A harness that never passes the corpus to the library is a constant program: every seed+# drives the same code and any crash it reports is one we manufactured.+# `archive_read_data(struct archive *, void *, size_t)` graded StreamDriven (its handle is a+# stream record), so admissibility passed it; the out-param promotion then gave it an+# allocated buffer and a capacity, and it PLANNED and EMITTED at rank 1 with the six format+# registrations in front of it and no `data` anywhere in the file. It has to REFUSE so the+# ranking falls through to the entry point that does take the bytes.+if echo "$LADDER_ARCHIVE" | grep -q "refused archive_read_data"; then+ ok "libarchive refuses archive_read_data, which resolves every argument but binds no corpus"+else+ bad "archive_read_data was not refused: \+$(echo "$LADDER_ARCHIVE" | grep 'archive_read_data' | head -1)"+fi+# Isolates the corpus test from a blanket StreamDriven or out-param ban: the SAME handle and+# the SAME stream record must still plan when one argument really is the fuzzer's input.+if echo "$LADDER_ARCHIVE" | grep -q "PLANS archive_read_open_memory"; then+ ok "the same handle still plans archive_read_open_memory, which does take the corpus"+else+ bad "the corpus test over-refused and took libarchive's ground-truth entry with it"+fi+# The end-to-end control: whatever libarchive ships must reference the corpus in its BODY.+# Grepping the whole file matches the LLVMFuzzerTestOneInput signature and passes vacuously.+ARCH_OUT="$OUT/corpus-libarchive"+rm -rf "$ARCH_OUT"+if "$LISDEX" harness "$SRC/libarchive" --out "$ARCH_OUT" --no-verify >/dev/null 2>&1 \+ && EMITTED="$(find "$ARCH_OUT" -maxdepth 1 -name '*.c' | head -1)" \+ && [ -n "$EMITTED" ] \+ && grep -v "LLVMFuzzerTestOneInput" "$EMITTED" | grep -qE "\bdata\b|lisdex_cursor|lisdex_text"; then+ ok "the harness libarchive ships passes the corpus to the library ($(basename "$EMITTED"))"+else+ bad "libarchive shipped a harness that ignores its input: $(basename "${EMITTED:-none}")"+fi+# The fall-through B46 named, still live at 4e23a80a: with every real decoder refused for an# unrelated reason, the loop kept walking down the ranking and emitted a configurator.LADDER_LZ4="$(plan_of lz4)"