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): M5 producer chain - a handle is what it was spelled, not what it collapses to

CarterPerez-dev152d81262466
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/emit.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/emit.rs
index 4f307202..e8fbd9d6 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/emit.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/emit.rs
@@ -149,7 +149,7 @@ fn render_entry(out: &mut String, plan: &HarnessPlan) {
let _ = writeln!(out, "{{");
render_prologue(out, plan);
for statement in &plan.body {
- render_stmt(out, statement);
+ render_stmt(out, statement, plan.needs_nul_copy);
}
if plan.needs_nul_copy {
let _ = writeln!(out, " free({NUL_COPY_LOCAL});");
@@ -193,7 +193,7 @@ fn render_prologue(out: &mut String, plan: &HarnessPlan) {
}
}
-fn render_stmt(out: &mut String, statement: &Stmt) {
+fn render_stmt(out: &mut String, statement: &Stmt, holds_nul_copy: bool) {
match statement {
Stmt::Declare { ty, name, init } => match init {
Init::Null => {
@@ -237,18 +237,26 @@ fn render_stmt(out: &mut String, statement: &Stmt) {
let _ = writeln!(out, " (void){local};");
}
Stmt::BailIfNull { local } => {
- let _ = writeln!(out, " if ({local} == NULL) {{");
- let _ = writeln!(out, " return 0;");
- let _ = writeln!(out, " }}");
+ render_bail(out, &format!("{local} == NULL"), holds_nul_copy);
}
Stmt::BailIfZero { local } => {
- let _ = writeln!(out, " if ({local} == 0) {{");
- let _ = writeln!(out, " return 0;");
- let _ = writeln!(out, " }}");
+ render_bail(out, &format!("{local} == 0"), holds_nul_copy);
}
}
}
+// A producer that failed bails out of a body the NUL copy was already allocated for, and
+// the direct oracle run enforces LeakSanitizer - so a bail that skips the free reports a
+// leak in the target on every replay, which is exactly the forged crash D15 warns about.
+fn render_bail(out: &mut String, condition: &str, holds_nul_copy: bool) {
+ let _ = writeln!(out, " if ({condition}) {{");
+ if holds_nul_copy {
+ let _ = writeln!(out, " free({NUL_COPY_LOCAL});");
+ }
+ let _ = writeln!(out, " return 0;");
+ let _ = writeln!(out, " }}");
+}
+
fn render_value(value: &Value) -> String {
match value {
Value::Data { cast } => match cast {
@@ -530,6 +538,38 @@ mod tests {
assert!(source.contains(" if (png == NULL) {\n return 0;\n }"));
}
+ #[test]
+ fn a_bail_frees_the_nul_copy_it_was_allocated_after() {
+ let plan = HarnessPlan::new("f")
+ .with(Stmt::Call {
+ function: "archive_entry_new".to_owned(),
+ args: vec![Value::NulTerminated],
+ capture: Some(Binding {
+ ty: "struct archive_entry *".to_owned(),
+ name: "ctx".to_owned(),
+ }),
+ })
+ .with(Stmt::BailIfNull {
+ local: "ctx".to_owned(),
+ });
+ let source = render(&plan, "x.c");
+ assert!(
+ source.contains(
+ " if (ctx == NULL) {\n free(lisdex_text);\n return 0;\n }"
+ ),
+ "the direct oracle run enforces LeakSanitizer, so a bail that skips the free forges a leak inside the target: {source}"
+ );
+ }
+
+ #[test]
+ fn a_bail_with_no_nul_copy_frees_nothing() {
+ let plan = HarnessPlan::new("f").with(Stmt::BailIfNull {
+ local: "ctx".to_owned(),
+ });
+ let source = render(&plan, "x.c");
+ assert!(!source.contains("free("));
+ }
+
#[test]
fn the_body_ends_by_returning_zero() {
let source = render(&simplified_libpng(), "x.c");
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.rs
index 7e7a06fc..5196e55c 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
@@ -5,7 +5,7 @@ use std::collections::BTreeMap;
use lisdex_surface::extract::{Vocabulary, carries_word, identifier_words};
use lisdex_surface::rank::Candidate;
-use lisdex_surface::signature::{Fuzzability, Param, ParamRole, Pointee, Shape};
+use lisdex_surface::signature::{Fuzzability, Param, ParamRole, Pointee, Shape, handle_key};
use crate::plan::{Binding, HarnessPlan, Init, Stmt, Value};
@@ -21,19 +21,34 @@ pub const FILESYSTEM_VERBS: [&str; 12] = [
"open", "fopen", "unlink", "remove", "rename", "mkdir", "rmdir", "chdir", "chmod", "symlink",
"truncate", "creat",
];
+pub const READ_WORDS: [&str; 9] = [
+ "read",
+ "decode",
+ "decoder",
+ "decompress",
+ "uncompress",
+ "parse",
+ "load",
+ "inflate",
+ "extract",
+];
+pub const WRITE_WORDS: [&str; 7] = [
+ "write", "encode", "encoder", "compress", "save", "deflate", "flush",
+];
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Refusal {
NoDataInput,
UnsizedOutParam { param: String },
OpaqueContext { record: String },
- HeapProducedContext { record: String },
UnboundExtent { param: String },
NeedsCallback { param: String },
PathEntry,
UnsizedTextParam { param: String },
FilesystemVerb { verb: String },
UnsupportedShape { param: String },
+ NoProducer { handle: String },
+ ProducerNeedsConstant { producer: String, param: String },
}
impl std::fmt::Display for Refusal {
@@ -48,10 +63,6 @@ impl std::fmt::Display for Refusal {
formatter,
"context {record} is opaque and needs a producer chain"
),
- Refusal::HeapProducedContext { record } => write!(
- formatter,
- "the library allocates {record} itself, so the fields the header exposes are only a prefix of the real object; a stack local reads past its own extent"
- ),
Refusal::UnboundExtent { param } => write!(
formatter,
"config {param} is an extent that no buffer in this signature claims; guessing it fabricates a crash inside the library"
@@ -71,6 +82,14 @@ impl std::fmt::Display for Refusal {
Refusal::UnsupportedShape { param } => {
write!(formatter, "parameter {param} has no binding rule")
}
+ Refusal::NoProducer { handle } => write!(
+ formatter,
+ "handle {handle} has no producer this harness can call: nothing in the public headers returns one without opening a file or being handed one first"
+ ),
+ Refusal::ProducerNeedsConstant { producer, param } => write!(
+ 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"
+ ),
}
}
}
@@ -95,6 +114,134 @@ pub fn is_extent_name(name: &str) -> bool {
carries_word(name, &EXTENT_WORDS)
}
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum Polarity {
+ Read,
+ Write,
+}
+
+// Read is tested first and wins outright, because the ambiguity runs one way only: every
+// decode word contains an encode word as a suffix (`decompress`, `uncompress`) and no
+// encode word contains a decode one. carries_word suffix-matches a lone lowercase
+// identifier, so without this zlib's `uncompress` - the DECOMPRESSOR - reads as a writer.
+fn polarity_of(name: &str) -> Option<Polarity> {
+ if carries_word(name, &READ_WORDS) {
+ return Some(Polarity::Read);
+ }
+ carries_word(name, &WRITE_WORDS).then_some(Polarity::Write)
+}
+
+fn producer_arguments(producer: &str, params: &[Param]) -> Result<Vec<Value>, Refusal> {
+ params
+ .iter()
+ .map(|param| {
+ let label = param.name.clone().unwrap_or_else(|| param.spelling.clone());
+ match &param.shape {
+ Shape::Pointer { .. } => Ok(Value::Null),
+ // An integer a constructor takes is a MODE, not data: tj3Init's initType
+ // selects compress versus decompress and inflateInit_'s stream_size is a
+ // sizeof. Guessing one hands the library a value the caller owns in
+ // production, which is the fabricated-crash class, so it is refused here
+ // and satisfied by a macro variant only where affinity names the macro.
+ _ => Err(Refusal::ProducerNeedsConstant {
+ producer: producer.to_owned(),
+ param: label,
+ }),
+ }
+ })
+ .collect()
+}
+
+type Rank<'a> = ((bool, usize), std::cmp::Reverse<(usize, usize, &'a str)>);
+
+struct Chosen<'a> {
+ affinity: (bool, usize),
+ name: &'a String,
+ arguments: Vec<Value>,
+ arity: usize,
+}
+
+impl<'a> Chosen<'a> {
+ fn rank(&self) -> Rank<'a> {
+ (
+ self.affinity,
+ std::cmp::Reverse((self.arity, self.name.len(), self.name.as_str())),
+ )
+ }
+}
+
+// D14: for `struct archive *` all five producers take zero arguments and contain `new`,
+// so a verb rule and a fewest-parameters rule both tie and alphabetical order picks
+// `archive_match_new` - which SIGSEGVs on every input because the read-side vtable is
+// never installed. Polarity is what separates them; affinity is what ranks what survives.
+fn choose_producer<'a>(
+ handle: &str,
+ entry: &str,
+ vocabulary: &'a Vocabulary,
+) -> Result<(&'a str, Vec<Value>), Refusal> {
+ let missing = || Refusal::NoProducer {
+ handle: handle.to_owned(),
+ };
+ let candidates = vocabulary.producers.get(handle).ok_or_else(missing)?;
+ let wanted = polarity_of(entry);
+ let mut best: Option<Chosen<'a>> = None;
+ let mut refusal: Option<Refusal> = None;
+ for (name, params) in candidates {
+ // A producer that consumes the handle it returns is a rebinder, not a
+ // constructor: XML_ExternalEntityParserCreate takes an XML_Parser and hands one
+ // back, and sorts first among its four producers.
+ if name == entry
+ || params
+ .iter()
+ .any(|param| handle_key(&param.spelling, &param.shape).as_deref() == Some(handle))
+ {
+ continue;
+ }
+ // A constructor that opens a file cannot be driven from memory, and zlib's gz*
+ // family already proved what happens when corpus bytes reach one.
+ if FILESYSTEM_VERBS
+ .iter()
+ .any(|verb| carries_word(name, &[verb]))
+ {
+ continue;
+ }
+ let found = polarity_of(name);
+ if let (Some(wanted), Some(found)) = (wanted, found)
+ && wanted != found
+ {
+ continue;
+ }
+ let arguments = match producer_arguments(name, params) {
+ Ok(arguments) => arguments,
+ Err(reason) => {
+ refusal.get_or_insert(reason);
+ continue;
+ }
+ };
+ // Polarity RANKS, it does not merely veto. tj3DecompressHeader's producers are
+ // tjInitDecompress and the polarity-neutral tjInitTransform; both share exactly
+ // one leading word with it ("tj", because the 3 splits off), so affinity ties and
+ // the shorter name wins - handing a decompress entry point a transform handle.
+ let matched = wanted.is_some() && wanted == found;
+ let contender = Chosen {
+ affinity: (matched, shared_leading_words(name, entry)),
+ name,
+ arguments,
+ arity: params.len(),
+ };
+ if best
+ .as_ref()
+ .is_none_or(|top| contender.rank() > top.rank())
+ {
+ best = Some(contender);
+ }
+ }
+ match best {
+ Some(chosen) => Ok((chosen.name.as_str(), chosen.arguments)),
+ None => Err(refusal.unwrap_or_else(missing)),
+ }
+}
+
#[must_use]
pub fn teardown_for(record: &str, entry: &str, vocabulary: &Vocabulary) -> Option<String> {
let candidates = vocabulary.destructors.get(record)?;
@@ -125,14 +272,19 @@ pub fn teardown_for(record: &str, entry: &str, vocabulary: &Vocabulary) -> Optio
fn teardown_calls(
record: &str,
- local: &str,
+ held: &Held,
entry: &str,
vocabulary: &Vocabulary,
) -> Vec<(String, Value)> {
// Calling a field's destructor after the record's own would double free, so a record
// that owns a destructor tears itself down and nothing else runs.
if let Some(function) = teardown_for(record, entry, vocabulary) {
- return vec![(function, Value::AddressOf(local.to_owned()))];
+ return vec![(function, held.reference())];
+ }
+ // A produced handle is opaque by construction: there are no fields to walk, and the
+ // library owns whatever hangs off it.
+ if held.by_value {
+ return Vec::new();
}
let Some(entity) = vocabulary.records.get(record) else {
return Vec::new();
@@ -145,7 +297,7 @@ fn teardown_calls(
(
function,
Value::AddressOfField {
- local: local.to_owned(),
+ local: held.local.clone(),
field: field.name.clone(),
},
)
@@ -221,20 +373,39 @@ fn cast_for(param: &Param) -> Result<Option<String>, Refusal> {
}
}
-fn context_record(param: &Param) -> Option<&str> {
- match &param.shape {
- Shape::Pointer {
- pointee: Pointee::Record(name),
- ..
- } => Some(name.as_str()),
- _ => None,
+// The entry point's own spelling is what the local must be declared with, but a context
+// declared `const` cannot then be handed to a teardown that takes it mutable.
+fn declarable(spelling: &str) -> String {
+ spelling
+ .trim()
+ .strip_prefix("const ")
+ .unwrap_or(spelling.trim())
+ .to_owned()
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+struct Held {
+ local: String,
+ // A stack context is passed by address; a handle the library allocated IS the address
+ // already. Getting this backwards is how `LZ4_freeStream(&ctx)` reported "attempting
+ // free on address which was not malloc()-ed" with the frames inside lz4.
+ by_value: bool,
+}
+
+impl Held {
+ fn reference(&self) -> Value {
+ if self.by_value {
+ Value::Local(self.local.clone())
+ } else {
+ Value::AddressOf(self.local.clone())
+ }
}
}
fn bind(
param: &Param,
role: ParamRole,
- locals: &BTreeMap<String, String>,
+ locals: &BTreeMap<String, Held>,
sized: bool,
) -> Result<Value, Refusal> {
let label = param.name.clone().unwrap_or_else(|| param.spelling.clone());
@@ -275,15 +446,15 @@ fn bind(
// 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 library, not the harness.
- let record = context_record(param).ok_or_else(|| Refusal::UnsupportedShape {
- param: label.clone(),
+ let handle = handle_key(&param.spelling, &param.shape).ok_or_else(|| {
+ Refusal::UnsupportedShape {
+ param: label.clone(),
+ }
})?;
locals
- .get(record)
- .map(|local| Value::AddressOf(local.clone()))
- .ok_or_else(|| Refusal::OpaqueContext {
- record: record.to_owned(),
- })
+ .get(&handle)
+ .map(Held::reference)
+ .ok_or(Refusal::OpaqueContext { record: handle })
}
}
}
@@ -325,9 +496,19 @@ fn admissible(candidate: &Candidate) -> Result<(), Refusal> {
}
// zlib's gzopen takes two unnamed char pointers, so no parameter-name rule could see
// the path. The function's own name is the signal that an argument becomes a path.
+ //
+ // The name alone is not enough, though: keyed on the verb only, this rule refused
+ // libarchive's `archive_read_open_memory(struct archive *, const void *, size_t)` -
+ // the ground-truth #1 entry point the M3 gate asserts ranks first - along with four
+ // more, because the word "open" appears in them. There is no path in that signature
+ // and a `void *` cannot be handed to the filesystem without a cast. So the verb only
+ // condemns a signature that also carries somewhere for a path to land, and `char` is
+ // that place: `unsigned char`/`signed char` are byte buffers, which is the same line
+ // the unsized-text rule below already draws.
if let Some(verb) = FILESYSTEM_VERBS
.iter()
.find(|verb| carries_word(&candidate.name, &[verb]))
+ && candidate.params.iter().any(is_path_capable)
{
return Err(Refusal::FilesystemVerb {
verb: (*verb).to_owned(),
@@ -336,46 +517,97 @@ fn admissible(candidate: &Candidate) -> Result<(), Refusal> {
Ok(())
}
-pub fn plan_candidate(
- candidate: &Candidate,
- include: &str,
- vocabulary: &Vocabulary,
-) -> Result<Vec<HarnessPlan>, Refusal> {
- admissible(candidate)?;
- let sized = candidate.analysis.roles.contains(&ParamRole::Length);
+fn is_path_capable(param: &Param) -> bool {
+ matches!(
+ &param.shape,
+ Shape::Pointer {
+ pointee: Pointee::Char | Pointee::SignedChar,
+ ..
+ }
+ )
+}
+
+struct Contexts {
+ locals: BTreeMap<String, Held>,
+ ordered: Vec<(String, Held)>,
+ opening: Vec<Stmt>,
+}
- let mut locals: BTreeMap<String, String> = BTreeMap::new();
- let mut ordered: Vec<(String, String)> = Vec::new();
+fn resolve_contexts(candidate: &Candidate, vocabulary: &Vocabulary) -> Result<Contexts, Refusal> {
+ let mut found = Contexts {
+ locals: BTreeMap::new(),
+ ordered: Vec::new(),
+ opening: Vec::new(),
+ };
for (param, role) in candidate.params.iter().zip(&candidate.analysis.roles) {
if !matches!(role, ParamRole::Context | ParamRole::Output) || is_double_pointer(param) {
continue;
}
- let Some(record) = context_record(param) else {
+ let Some(handle) = handle_key(&param.spelling, &param.shape) else {
continue;
};
- if !vocabulary.records.contains_key(record) {
- return Err(Refusal::OpaqueContext {
- record: record.to_owned(),
- });
+ if found.locals.contains_key(&handle) {
+ continue;
}
- // zlib exposes a 24-byte prefix of `struct gzFile_s` only so the gzgetc macro can
- // inline; the real object is heap allocated by gzopen. Declared on the stack it
- // produced a stack-buffer-overflow inside gzputs on every input.
- if vocabulary.produced.contains(record) {
- return Err(Refusal::HeapProducedContext {
- record: record.to_owned(),
+ let local = if found.locals.is_empty() {
+ CONTEXT_LOCAL.to_owned()
+ } else {
+ format!("{CONTEXT_LOCAL}{}", found.locals.len())
+ };
+ // A record the header defines completely and the library never hands back is the
+ // caller's to declare. Everything else - an opaque handle, or a record the library
+ // allocates and only exposes a prefix of - has to come from a producer.
+ let owned =
+ vocabulary.records.contains_key(&handle) && !vocabulary.produced.contains(&handle);
+ let held = if owned {
+ found.opening.push(Stmt::Declare {
+ ty: vocabulary.records[&handle].spelling.clone(),
+ name: local.clone(),
+ init: Init::ZeroBytes,
});
- }
- if !locals.contains_key(record) {
- let local = if locals.is_empty() {
- CONTEXT_LOCAL.to_owned()
- } else {
- format!("{CONTEXT_LOCAL}{}", locals.len())
- };
- locals.insert(record.to_owned(), local.clone());
- ordered.push((record.to_owned(), local));
- }
+ Held {
+ local,
+ by_value: false,
+ }
+ } else {
+ let (producer, args) = choose_producer(&handle, &candidate.name, vocabulary)?;
+ found.opening.push(Stmt::Call {
+ function: producer.to_owned(),
+ args,
+ capture: Some(Binding {
+ ty: declarable(&param.spelling),
+ name: local.clone(),
+ }),
+ });
+ // A producer that failed leaves a NULL the entry point will dereference, and
+ // that crash belongs to the harness, not to the library.
+ found.opening.push(Stmt::BailIfNull {
+ local: local.clone(),
+ });
+ Held {
+ local,
+ by_value: true,
+ }
+ };
+ found.locals.insert(handle.clone(), held.clone());
+ found.ordered.push((handle, held));
}
+ Ok(found)
+}
+
+pub fn plan_candidate(
+ candidate: &Candidate,
+ include: &str,
+ vocabulary: &Vocabulary,
+) -> Result<Vec<HarnessPlan>, Refusal> {
+ admissible(candidate)?;
+ let sized = candidate.analysis.roles.contains(&ParamRole::Length);
+
+ let Contexts {
+ locals,
+ ordered,
+ opening,
+ } = resolve_contexts(candidate, vocabulary)?;
let mut args = Vec::with_capacity(candidate.params.len());
for (param, role) in candidate.params.iter().zip(&candidate.analysis.roles) {
@@ -383,21 +615,20 @@ pub fn plan_candidate(
}
let mut base = HarnessPlan::new(candidate.name.clone()).with_include(include);
- for (record, local) in &ordered {
- base = base.with(Stmt::Declare {
- ty: vocabulary.records[record].spelling.clone(),
- name: local.clone(),
- init: Init::ZeroBytes,
- });
+ for statement in opening {
+ base = base.with(statement);
}
+ // Populating a field only means anything on a record the harness itself declared. A
+ // produced handle is opaque by construction and its interior belongs to the library.
let bindings = ordered
.first()
- .map(|(record, _)| field_bindings(record, vocabulary))
+ .filter(|(_, held)| !held.by_value)
+ .map(|(handle, _)| field_bindings(handle, vocabulary))
.unwrap_or_default();
let teardowns: Vec<(String, Value)> = ordered
.iter()
- .flat_map(|(record, local)| teardown_calls(record, local, &candidate.name, vocabulary))
+ .flat_map(|(handle, held)| teardown_calls(handle, held, &candidate.name, vocabulary))
.collect();
let call = |plan: HarnessPlan| -> HarnessPlan {
let capture = match candidate.result.shape {
@@ -463,7 +694,9 @@ mod tests {
fn call_in(plan: &HarnessPlan) -> &Stmt {
plan.body
.iter()
- .find(|statement| matches!(statement, Stmt::Call { .. }))
+ .find(|statement| {
+ matches!(statement, Stmt::Call { function, .. } if function == &plan.entry)
+ })
.expect("the plan calls the entry point")
}
@@ -502,6 +735,240 @@ mod tests {
}
}
+ fn opaque_handle_entry(name: &str) -> Candidate {
+ candidate(
+ name,
+ vec![
+ param(
+ "handle",
+ "tjhandle",
+ Shape::Pointer {
+ pointee: Pointee::Void,
+ is_const: false,
+ },
+ ),
+ param(
+ "jpegBuf",
+ "const unsigned char *",
+ Shape::Pointer {
+ pointee: Pointee::UnsignedChar,
+ is_const: true,
+ },
+ ),
+ param("jpegSize", "size_t", Shape::Integral),
+ ],
+ vec![ParamRole::Context, ParamRole::Buffer, ParamRole::Length],
+ Fuzzability::DirectBuffer,
+ Returns::default(),
+ )
+ }
+
+ fn handle_vocabulary(producers: &[(&str, Vec<Param>)]) -> Vocabulary {
+ Vocabulary {
+ producers: BTreeMap::from([(
+ "tjhandle".to_owned(),
+ producers
+ .iter()
+ .map(|(name, params)| ((*name).to_owned(), params.clone()))
+ .collect(),
+ )]),
+ ..Vocabulary::default()
+ }
+ }
+
+ fn producer_in(plan: &HarnessPlan) -> String {
+ let Some(Stmt::Call { function, .. }) = plan.body.first() else {
+ panic!("the plan opens with the producer call")
+ };
+ function.clone()
+ }
+
+ fn archive_open(buffer: Param) -> Candidate {
+ candidate(
+ "archive_read_open_memory",
+ vec![
+ param(
+ "a",
+ "struct archive *",
+ Shape::Pointer {
+ pointee: Pointee::Record("archive".to_owned()),
+ is_const: false,
+ },
+ ),
+ buffer,
+ param("size", "size_t", Shape::Integral),
+ ],
+ vec![ParamRole::Context, ParamRole::Buffer, ParamRole::Length],
+ Fuzzability::DirectBuffer,
+ Returns::default(),
+ )
+ }
+
+ fn archive_vocabulary() -> Vocabulary {
+ Vocabulary {
+ producers: BTreeMap::from([(
+ "archive".to_owned(),
+ BTreeMap::from([("archive_read_new".to_owned(), Vec::new())]),
+ )]),
+ ..Vocabulary::default()
+ }
+ }
+
+ #[test]
+ fn a_filesystem_verb_over_a_void_buffer_is_not_a_path() {
+ // This is libarchive's ground-truth #1 and the M3 gate asserts it ranks first.
+ // Refusing it on the word "open" cost five entry points and the whole library.
+ let plans = plan_candidate(
+ &archive_open(param(
+ "buff",
+ "const void *",
+ Shape::Pointer {
+ pointee: Pointee::Void,
+ is_const: true,
+ },
+ )),
+ "archive.h",
+ &archive_vocabulary(),
+ )
+ .unwrap();
+ assert_eq!(producer_in(&plans[0]), "archive_read_new");
+ }
+
+ #[test]
+ fn a_filesystem_verb_over_a_char_buffer_is_still_refused() {
+ assert_eq!(
+ plan_candidate(
+ &archive_open(param(
+ "path",
+ "const char *",
+ Shape::Pointer {
+ pointee: Pointee::Char,
+ is_const: true,
+ },
+ )),
+ "archive.h",
+ &archive_vocabulary(),
+ ),
+ Err(Refusal::FilesystemVerb {
+ verb: "open".to_owned()
+ }),
+ "zlib's gzopen created and TRUNCATED files named by corpus bytes"
+ );
+ }
+
+ #[test]
+ fn a_typedef_hiding_a_void_pointer_is_keyed_on_the_typedef_not_the_void() {
+ let plans = plan_candidate(
+ &opaque_handle_entry("tj3DecompressHeader"),
+ "turbojpeg.h",
+ &handle_vocabulary(&[("tjInitDecompress", Vec::new())]),
+ )
+ .unwrap();
+ assert_eq!(producer_in(&plans[0]), "tjInitDecompress");
+ }
+
+ #[test]
+ fn a_matching_polarity_outranks_a_neutral_producer_with_a_shorter_name() {
+ // Both share exactly one leading word with tj3DecompressHeader, because the 3
+ // splits off, so affinity ties and the shorter tjInitTransform used to win.
+ let plans = plan_candidate(
+ &opaque_handle_entry("tj3DecompressHeader"),
+ "turbojpeg.h",
+ &handle_vocabulary(&[
+ ("tjInitTransform", Vec::new()),
+ ("tjInitDecompress", Vec::new()),
+ ]),
+ )
+ .unwrap();
+ assert_eq!(
+ producer_in(&plans[0]),
+ "tjInitDecompress",
+ "a decompress entry point handed a transform handle decodes nothing"
+ );
+ }
+
+ #[test]
+ fn the_opposite_polarity_is_never_chosen_even_when_it_is_the_only_producer() {
+ assert_eq!(
+ plan_candidate(
+ &opaque_handle_entry("tj3DecompressHeader"),
+ "turbojpeg.h",
+ &handle_vocabulary(&[("tjInitCompress", Vec::new())]),
+ ),
+ Err(Refusal::NoProducer {
+ handle: "tjhandle".to_owned()
+ }),
+ "archive_match_new SIGSEGVd on every input because the read-side vtable was never installed"
+ );
+ }
+
+ #[test]
+ fn a_producer_that_consumes_the_handle_it_returns_is_not_a_constructor() {
+ // XML_ExternalEntityParserCreate returns an XML_Parser and takes one, and sorts
+ // first among its four producers, so without the guard this recurses forever.
+ let rebinder = vec![param(
+ "parent",
+ "tjhandle",
+ Shape::Pointer {
+ pointee: Pointee::Void,
+ is_const: false,
+ },
+ )];
+ assert_eq!(
+ plan_candidate(
+ &opaque_handle_entry("tj3DecompressHeader"),
+ "turbojpeg.h",
+ &handle_vocabulary(&[("tjCloneDecompress", rebinder)]),
+ ),
+ Err(Refusal::NoProducer {
+ handle: "tjhandle".to_owned()
+ })
+ );
+ }
+
+ #[test]
+ fn a_producer_that_opens_a_file_is_not_driveable_from_memory() {
+ assert_eq!(
+ plan_candidate(
+ &opaque_handle_entry("tj3DecompressHeader"),
+ "turbojpeg.h",
+ &handle_vocabulary(&[(
+ "tjOpenDecompress",
+ vec![param(
+ "path",
+ "const char *",
+ Shape::Pointer {
+ pointee: Pointee::Char,
+ is_const: true,
+ },
+ )],
+ )]),
+ ),
+ Err(Refusal::NoProducer {
+ handle: "tjhandle".to_owned()
+ }),
+ "zlib's gz* family already proved what corpus bytes reaching an opener do"
+ );
+ }
+
+ #[test]
+ fn a_producer_needing_an_integer_mode_is_refused_rather_than_guessed() {
+ assert_eq!(
+ plan_candidate(
+ &opaque_handle_entry("tj3DecompressHeader"),
+ "turbojpeg.h",
+ &handle_vocabulary(&[
+ ("tj3Init", vec![param("initType", "int", Shape::Integral)],)
+ ]),
+ ),
+ Err(Refusal::ProducerNeedsConstant {
+ producer: "tj3Init".to_owned(),
+ param: "initType".to_owned(),
+ }),
+ "tj3Init's initType selects compress versus decompress and the headers cannot say which"
+ );
+ }
+
fn png_image_vocabulary() -> Vocabulary {
Vocabulary {
macros: BTreeSet::from(["PNG_IMAGE_VERSION".to_owned(), "PNG_FORMAT_RGBA".to_owned()]),
@@ -926,18 +1393,75 @@ mod tests {
}
#[test]
- fn a_record_the_library_allocates_itself_is_refused_as_a_stack_context() {
+ fn a_record_the_library_allocates_itself_is_never_declared_on_the_stack() {
let mut vocab = png_image_vocabulary();
vocab.produced.insert("png_image".to_owned());
assert_eq!(
plan_candidate(&begin_read(), "png.h", &vocab),
- Err(Refusal::HeapProducedContext {
- record: "png_image".to_owned()
+ Err(Refusal::NoProducer {
+ handle: "png_image".to_owned()
}),
"zlib's exposed gzFile_s prefix declared on the stack overflowed it on every input"
);
}
+ #[test]
+ fn a_record_the_library_allocates_itself_comes_back_from_its_producer() {
+ let mut vocab = png_image_vocabulary();
+ vocab.produced.insert("png_image".to_owned());
+ vocab.producers.insert(
+ "png_image".to_owned(),
+ BTreeMap::from([("png_image_new".to_owned(), Vec::new())]),
+ );
+ let plans = plan_candidate(&begin_read(), "png.h", &vocab).unwrap();
+ let opening = &plans[0].body[0];
+ assert_eq!(
+ opening,
+ &Stmt::Call {
+ function: "png_image_new".to_owned(),
+ args: Vec::new(),
+ capture: Some(Binding {
+ ty: "png_imagep".to_owned(),
+ name: CONTEXT_LOCAL.to_owned(),
+ }),
+ },
+ "a produced record must come from the producer, never from a stack declaration"
+ );
+ assert_eq!(
+ plans[0].body[1],
+ Stmt::BailIfNull {
+ local: CONTEXT_LOCAL.to_owned()
+ },
+ "a producer that returned NULL must not reach the entry point"
+ );
+ assert!(
+ !plans[0]
+ .body
+ .iter()
+ .any(|statement| matches!(statement, Stmt::Declare { .. })),
+ "nothing about a library-allocated record may be declared on the stack"
+ );
+ }
+
+ #[test]
+ fn a_produced_handle_is_passed_by_value_not_by_address() {
+ let mut vocab = png_image_vocabulary();
+ vocab.produced.insert("png_image".to_owned());
+ vocab.producers.insert(
+ "png_image".to_owned(),
+ BTreeMap::from([("png_image_new".to_owned(), Vec::new())]),
+ );
+ let plans = plan_candidate(&begin_read(), "png.h", &vocab).unwrap();
+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {
+ unreachable!()
+ };
+ assert_eq!(
+ args[0],
+ Value::Local(CONTEXT_LOCAL.to_owned()),
+ "taking the address of a pointer the library already returned is how LZ4_freeStream(&ctx) reported a free of a non-malloc'd address"
+ );
+ }
+
#[test]
fn a_size_like_config_is_refused_rather_than_pinned_or_guessed() {
let lz4 = candidate(
@@ -1178,8 +1702,8 @@ mod tests {
};
assert_eq!(
plan_candidate(&opaque, "png.h", &png_image_vocabulary()),
- Err(Refusal::OpaqueContext {
- record: "png_struct_def".to_owned()
+ Err(Refusal::NoProducer {
+ handle: "png_struct_def".to_owned()
})
);
}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/extract.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/extract.rs
index d15712f6..2171c291 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/extract.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/extract.rs
@@ -8,7 +8,7 @@ use std::sync::{Mutex, PoisonError};
use clang::diagnostic::{Diagnostic, Severity};
use clang::{Clang, Entity, EntityKind, Index, Type, TypeKind};
-use crate::signature::{Param, Pointee, Returns, Shape};
+use crate::signature::{Param, Pointee, Returns, Shape, handle_key};
static PARSE_LOCK: Mutex<()> = Mutex::new(());
thread_local! {
@@ -97,6 +97,7 @@ pub struct Vocabulary {
pub records: BTreeMap<String, Record>,
pub produced: BTreeSet<String>,
pub destructors: BTreeMap<String, BTreeSet<String>>,
+ pub producers: BTreeMap<String, BTreeMap<String, Vec<Param>>>,
}
impl Vocabulary {
@@ -112,6 +113,9 @@ impl Vocabulary {
.or_default()
.extend(functions);
}
+ for (handle, functions) in other.producers {
+ self.producers.entry(handle).or_default().extend(functions);
+ }
}
}
@@ -400,18 +404,13 @@ fn record_behind_two_pointers(ty: &Type) -> Option<String> {
(target.get_kind() == TypeKind::Record).then(|| record_name(&target))
}
-fn sole_record_parameter(entity: &Entity) -> Option<String> {
+fn sole_handle_parameter(entity: &Entity) -> Option<String> {
let arguments = entity.get_arguments()?;
let [only] = arguments.as_slice() else {
return None;
};
- match shape_of(&only.get_type()?) {
- Shape::Pointer {
- pointee: Pointee::Record(name),
- ..
- } => Some(name),
- _ => None,
- }
+ let ty = only.get_type()?;
+ handle_key(&ty.get_display_name(), &shape_of(&ty))
}
fn collect_relations(entity: &Entity, target: &Path, into: &mut Vocabulary) {
@@ -424,12 +423,26 @@ fn collect_relations(entity: &Entity, target: &Path, into: &mut Vocabulary) {
// A const return is a borrow, not an allocation: libwebp's WebPIDecodedArea hands back
// a `const WebPDecBuffer *` while WebPInitDecBuffer exists precisely so the caller can
// own one, and archive_entry_stat hands back a `const struct stat *`.
+ let returns = result_of(entity);
if let Shape::Pointer {
pointee: Pointee::Record(record),
is_const: false,
- } = result_of(entity).shape
+ } = &returns.shape
+ {
+ into.produced.insert(record.clone());
+ }
+ // The same borrow-versus-allocation rule decides what may open a producer chain: a
+ // function handing back a const pointer did not make the object and calling it will
+ // not give the harness one it is allowed to tear down.
+ if let Shape::Pointer {
+ is_const: false, ..
+ } = &returns.shape
+ && let Some(handle) = handle_key(&returns.spelling, &returns.shape)
{
- into.produced.insert(record);
+ into.producers
+ .entry(handle)
+ .or_default()
+ .insert(name.clone(), parameters(entity));
}
// An allocator does not have to RETURN the record: `int X_create(X **out)` hands it
// back through a double pointer, and a record whose allocation we never see reads as a
@@ -443,9 +456,9 @@ fn collect_relations(entity: &Entity, target: &Path, into: &mut Vocabulary) {
}
}
if is_teardown_name(&name)
- && let Some(record) = sole_record_parameter(entity)
+ && let Some(handle) = sole_handle_parameter(entity)
{
- into.destructors.entry(record).or_default().insert(name);
+ into.destructors.entry(handle).or_default().insert(name);
}
}
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rs
index 7d10648b..7cff6565 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rs
@@ -145,6 +145,35 @@ pub struct Returns {
pub shape: Shape,
}
+fn opaque_typedef(spelling: &str) -> Option<String> {
+ let name = spelling.trim();
+ let mut characters = name.chars();
+ let heads = characters
+ .next()
+ .is_some_and(|first| first.is_ascii_alphabetic() || first == '_');
+ let tails = characters.all(|rest| rest.is_ascii_alphanumeric() || rest == '_');
+ (heads && tails).then(|| name.to_owned())
+}
+
+// `typedef void *tjhandle` defeats canonical-type matching on 17 of libjpeg-turbo's 21
+// entry points: every one of them refused with "parameter handle has no binding rule"
+// because the canonical type is a bare `void *` that names nothing. The identity of an
+// opaque handle is therefore the typedef it was SPELLED with, never what it collapses to.
+#[must_use]
+pub fn handle_key(spelling: &str, shape: &Shape) -> Option<String> {
+ match shape {
+ Shape::Pointer {
+ pointee: Pointee::Record(name),
+ ..
+ } => Some(name.clone()),
+ Shape::Pointer {
+ pointee: Pointee::Void,
+ ..
+ } => opaque_typedef(spelling),
+ _ => None,
+ }
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub enum ParamRole {
Buffer,