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
feat(lisdex): B87 - a scalar the library writes is a slot, and the perfect harness still leaked
CarterPerez-deva167f17ea50b
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 b91a6071..930f075e 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@@ -45,6 +45,7 @@ pub enum Refusal {UnsupportedShape { param: String },NoProducer { handle: String },ProducerNeedsConstant { producer: String, param: String },+ UnreleasedReturn { ty: String },}impl std::fmt::Display for Refusal {@@ -94,6 +95,10 @@ impl std::fmt::Display for Refusal {formatter,"producer {producer} needs an integer {param} that the headers cannot supply; feeding it one hands the library a mode the caller picks in production"),+ Refusal::UnreleasedReturn { ty } => write!(+ formatter,+ "the entry point returns {ty}, memory the library allocated and nothing here can release; leaving it unfreed makes LeakSanitizer report a leak inside the library on every seed, which is a crash this harness manufactured"+ ),}}}@@ -386,11 +391,58 @@ fn is_double_pointer(param: &Param) -> bool {// harness cannot spell is one it cannot emit; `png_sPLT_tpp` hides both stars in a typedef and// is refused rather than guessed at, exactly as `indirect_extent_local` refuses.fn filled_by_library(param: &Param) -> Option<String> {- signature::record_behind_two_pointers(¶m.shape)?;- let inner = param.spelling.trim().strip_suffix('*')?.trim();+ if signature::record_behind_two_pointers(¶m.shape).is_none() && !is_scalar_slot(param) {+ return None;+ }+ declared_inner(¶m.spelling)+}++// `int *const width` is a const POINTER to a mutable int, so the qualifier trails the star and+// `strip_suffix('*')` alone misses it - which would have dropped libwebp's `VP8GetInfo` and+// `VP8LGetInfo` from the population while keeping their `WebPDecode*` siblings.+fn declared_inner(spelling: &str) -> Option<String> {+ let trimmed = spelling.trim();+ let without_qualifier = trimmed.strip_suffix("const").unwrap_or(trimmed).trim();+ let inner = without_qualifier.strip_suffix('*')?.trim();(!inner.is_empty()).then(|| inner.to_owned())}+// B87. A pointer to ONE scalar the library writes is a slot the harness owns, exactly as+// `struct archive_entry **` is: declare it, pass its address, let the library fill it. The+// number does not have to be guessed, so B67's objection does not apply - a pointer to a single+// element has no extent because it points at exactly one.+//+// What it is NOT is a buffer, and the census is why both guards below are the rule rather than+// decoration. Over 10 trees there are 86 non-const `Integral *` parameters on 29 functions, and+// `tj3Decompress12`'s `short *dstBuf` sits in that population beside `WebPDecodeRGBA`'s+// `int *width`. Handing a destination image buffer one stack `short` is a heap overflow this+// harness manufactured, on a rank-1 candidate the authors themselves call - the D12 class.+// A name carrying an OUTPUT word is memory; a PLURAL name is one element per plane or per image+// (`strides`, `dstSizes`). Both directions are refused, because guessing wrong forges a crash+// and refusing only costs reach.+fn is_scalar_slot(param: &Param) -> bool {+ if !matches!(+ param.shape,+ Shape::Pointer {+ pointee: Pointee::Integral,+ is_const: false,+ }+ ) {+ return false;+ }+ let Some(name) = param.name.as_deref() else {+ return false;+ };+ !signature::matches_vocabulary(name, signature::OUTPUT_NAMES) && !names_many(name)+}++// A trailing `s` that is not `ss`. Deliberately crude and deliberately biased: a singular name it+// misreads as plural is only refused, while a plural it misses is an out-of-bounds write.+fn names_many(name: &str) -> bool {+ let lowered = name.to_lowercase();+ lowered.ends_with('s') && !lowered.ends_with("ss")+}+// Which parameters the library fills in, and what the harness must declare for each. Shared by// the entry point and by an upward step so both spell the same local for the same signature.//@@ -586,13 +638,21 @@ fn bind_all(.collect()}+// The declared type decides the initialiser, and it decides it structurally: a slot for a record+// the library allocates is a POINTER and starts NULL, while a scalar slot is a value and starts+// zeroed. `int lisdex_filled = NULL;` is not the same statement - it is a constraint violation+// that a target built with `-Werror` refuses to compile.fn filled_declarations(filled: &BTreeMap<usize, (String, String)>) -> Vec<Stmt> {filled.values().map(|(ty, local)| Stmt::Declare {ty: ty.clone(),name: local.clone(),- init: Init::Null,+ init: if ty.trim_end().ends_with('*') {+ Init::Null+ } else {+ Init::ZeroBytes+ },}).collect()}@@ -841,11 +901,14 @@ fn bind_context(analysis: &Analysis,scope: Option<&mut Scope>,) -> Result<Value, Refusal> {+ // The map IS the answer to "does the library fill this": `filled_locals` decided it and+ // declared the local, so consulting it for every index rather than only for a double pointer+ // is what lets B87's scalar slot reach the same binding B81's record slot already had.+ if let Some((_, local)) = filled.get(&index) {+ return Ok(Value::AddressOf(local.clone()));+ }if is_double_pointer(param) {- return filled- .get(&index)- .map(|(_, local)| Value::AddressOf(local.clone()))- .ok_or(Refusal::UnsizedOutParam { param: label });+ return Err(Refusal::UnsizedOutParam { param: label });}// Binding every context param to one shared local passed a small struct's address where a// large one was expected and produced a real ASAN stack-buffer-overflow attributed to the@@ -899,6 +962,46 @@ pub fn variant_masks(fields: usize) -> Vec<u32> {masks}+// B87, and it was found by RUNNING the harnesses rather than by reading them. The emitted C for+// `WebPDecodeRGBA(data, size, &w, &h)` is exactly what a human writes and it still failed every+// valid seed: the function RETURNS a freshly allocated RGBA buffer, nothing released it, and+// LeakSanitizer reported `Direct leak of 4096 byte(s)` with the frames inside+// `WebPAllocateDecBuffer` - a crash attributed to the library that the harness manufactured.+// That is D15's forged crash and the same line B84 already draws for a record with a destructor:+// where the plan cannot schedule the teardown, it must refuse rather than leak.+//+// Scoped to a byte-typed return because that is what the census reaches: an `Into`-style decoder+// hands back the caller's own buffer, so the leak only exists when the library allocated it, and+// a plain `int` return (`WebPGetInfo`, `VP8GetInfo`, `VP8LGetInfo`) owns nothing at all.+fn returns_releasable_memory(+ candidate: &Candidate,+ vocabulary: &Vocabulary,+) -> Result<(), Refusal> {+ let result = &candidate.result;+ let Shape::Pointer {+ pointee,+ is_const: false,+ } = &result.shape+ else {+ return Ok(());+ };+ if !pointee.is_byte_sized() {+ return Ok(());+ }+ // An argument the harness passed in and got back is memory it already owns.+ if candidate+ .params+ .iter()+ .any(|param| param.shape == result.shape)+ {+ return Ok(());+ }+ let _ = vocabulary;+ Err(Refusal::UnreleasedReturn {+ ty: result.spelling.clone(),+ })+}+fn admissible(candidate: &Candidate) -> Result<(), Refusal> {if candidate.fuzzability == Fuzzability::NoInput {return Err(Refusal::NoDataInput);@@ -1441,6 +1544,7 @@ pub fn plan_candidate_with(setup: &[String],) -> Result<Vec<HarnessPlan>, Refusal> {admissible(candidate)?;+ returns_releasable_memory(candidate, vocabulary)?;let Contexts {locals,@@ -1572,6 +1676,218 @@ mod tests {.expect("the plan calls the entry point")}+ // B87. Each condition of the scalar-slot rule gets its own test, because the two guards have+ // NO isolating control on any real tree: `strides` and `dstSizes` sit in signatures whose+ // EARLIER parameter (`dstPlanes`, or a leftover extent `n`) refuses first, so gutting the+ // plural guard reddens nothing end to end. That is B73's situation exactly - the gate tests+ // the rule whole and the halves are isolated here.+ fn scalar_ptr(name: &str, spelling: &str) -> Param {+ param(+ name,+ spelling,+ Shape::Pointer {+ pointee: Pointee::Integral,+ is_const: false,+ },+ )+ }++ #[test]+ fn a_scalar_the_library_writes_is_a_slot_the_harness_declares() {+ assert!(is_scalar_slot(&scalar_ptr("width", "int *")));+ assert_eq!(+ filled_by_library(&scalar_ptr("width", "int *")),+ Some("int".to_owned())+ );+ }++ // `int *const width` is a const POINTER to a mutable int - libwebp's VP8GetInfo and+ // VP8LGetInfo spell it that way and their WebPDecode* siblings do not.+ #[test]+ fn a_const_pointer_to_a_mutable_scalar_is_still_a_slot() {+ assert_eq!(+ filled_by_library(&scalar_ptr("width", "int *const")),+ Some("int".to_owned())+ );+ }++ // The guard that keeps `tj3Decompress12`'s `short *dstBuf` out. Handing a destination image+ // buffer one stack `short` is an out-of-bounds write this harness manufactured.+ #[test]+ fn a_scalar_pointer_named_for_an_output_is_memory_and_is_refused() {+ assert!(!is_scalar_slot(&scalar_ptr("dstBuf", "short *")));+ assert!(filled_by_library(&scalar_ptr("dstBuf", "short *")).is_none());+ }++ // The guard that keeps `strides` and `dstSizes` out: one element per plane, per image.+ #[test]+ fn a_plural_scalar_pointer_is_an_array_and_is_refused() {+ assert!(!is_scalar_slot(&scalar_ptr("strides", "int *")));+ assert!(!is_scalar_slot(&scalar_ptr("dstSizes", "unsigned long *")));+ assert!(names_many("strides"));+ assert!(!names_many("stride"));+ }++ // Biased on purpose: a singular name misread as plural is only refused, which costs reach;+ // a plural one missed is an out-of-bounds write.+ #[test]+ fn the_plural_test_errs_toward_refusing() {+ assert!(!names_many("has_alpha"));+ assert!(!names_many("width"));+ assert!(names_many("axis"));+ }++ #[test]+ fn a_byte_pointer_is_never_a_scalar_slot() {+ for pointee in [Pointee::UnsignedChar, Pointee::Char, Pointee::Void] {+ let shape = Shape::Pointer {+ pointee,+ is_const: false,+ };+ assert!(!is_scalar_slot(¶m("out", "char *", shape)));+ }+ }++ #[test]+ fn a_const_scalar_pointer_is_an_input_not_a_slot() {+ let shape = Shape::Pointer {+ pointee: Pointee::Integral,+ is_const: true,+ };+ assert!(!is_scalar_slot(¶m("width", "const int *", shape)));+ }++ // A record slot is a POINTER and starts NULL; a scalar slot is a value and starts zeroed.+ // `int lisdex_filled = NULL;` is a constraint violation, not a slower way of saying zero.+ #[test]+ fn a_scalar_slot_is_zeroed_while_a_record_slot_is_nulled() {+ let mut filled = BTreeMap::new();+ filled.insert(0, ("int".to_owned(), "a".to_owned()));+ filled.insert(1, ("struct archive_entry *".to_owned(), "b".to_owned()));+ let declared = filled_declarations(&filled);+ assert_eq!(+ declared,+ vec![+ Stmt::Declare {+ ty: "int".to_owned(),+ name: "a".to_owned(),+ init: Init::ZeroBytes,+ },+ Stmt::Declare {+ ty: "struct archive_entry *".to_owned(),+ name: "b".to_owned(),+ init: Init::Null,+ },+ ]+ );+ }++ fn byte_ptr_return(spelling: &str) -> Returns {+ Returns {+ spelling: spelling.to_owned(),+ shape: Shape::Pointer {+ pointee: Pointee::UnsignedChar,+ is_const: false,+ },+ }+ }++ // Reproduces the measurement, not a guess about it: the emitted `WebPDecodeRGBA(data, size,+ // &w, &h)` was structurally perfect and LeakSanitizer still reported `Direct leak of 4096+ // byte(s)` inside `WebPAllocateDecBuffer` on every valid seed.+ #[test]+ fn an_entry_point_that_hands_back_library_memory_is_refused() {+ let got = returns_releasable_memory(+ &candidate(+ "WebPDecodeRGBA",+ vec![+ param(+ "data",+ "const uint8_t *",+ Shape::Pointer {+ pointee: Pointee::UnsignedChar,+ is_const: true,+ },+ ),+ param("size", "size_t", Shape::Integral),+ ],+ vec![ParamRole::Buffer, ParamRole::Length],+ Fuzzability::DirectBuffer,+ byte_ptr_return("uint8_t *"),+ ),+ &Vocabulary::default(),+ );+ assert!(+ matches!(got, Err(Refusal::UnreleasedReturn { .. })),+ "a leaking return was allowed through: {got:?}"+ );+ }++ // The paired positive control, and it is the design rather than decoration: an `Into`-style+ // decoder returns the caller's OWN buffer, so refusing every byte-pointer return would take+ // `WebPDecodeRGBAInto` and its four siblings with it - all of which survive the oracles today.+ #[test]+ fn an_entry_point_that_hands_back_the_callers_own_buffer_still_plans() {+ let got = returns_releasable_memory(+ &candidate(+ "WebPDecodeRGBAInto",+ vec![+ param(+ "data",+ "const uint8_t *",+ Shape::Pointer {+ pointee: Pointee::UnsignedChar,+ is_const: true,+ },+ ),+ param("size", "size_t", Shape::Integral),+ param(+ "output_buffer",+ "uint8_t *",+ Shape::Pointer {+ pointee: Pointee::UnsignedChar,+ is_const: false,+ },+ ),+ ],+ vec![ParamRole::Buffer, ParamRole::Length, ParamRole::Output],+ Fuzzability::DirectBuffer,+ byte_ptr_return("uint8_t *"),+ ),+ &Vocabulary::default(),+ );+ assert!(got.is_ok(), "the caller's own buffer was read as a leak");+ }++ // `WebPGetInfo` returns an int and owns nothing, which is what leaves B87 any reach at all.+ #[test]+ fn a_scalar_return_owns_nothing_and_is_not_refused() {+ let got = returns_releasable_memory(+ &candidate(+ "WebPGetInfo",+ vec![+ param(+ "data",+ "const uint8_t *",+ Shape::Pointer {+ pointee: Pointee::UnsignedChar,+ is_const: true,+ },+ ),+ param("size", "size_t", Shape::Integral),+ ],+ vec![ParamRole::Buffer, ParamRole::Length],+ Fuzzability::DirectBuffer,+ Returns {+ spelling: "int".to_owned(),+ shape: Shape::Integral,+ },+ ),+ &Vocabulary::default(),+ );+ assert!(got.is_ok(), "an int return was read as owned memory");+ }+fn param(name: &str, spelling: &str, shape: Shape) -> Param {Param {name: Some(name.to_owned()),