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): M5 - a parameter list is a set of ownership claims, not one buffer and its length
CarterPerez-dev7f1bb283f65a
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.rsindex e8fbd9d6..a10db46f 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@@ -5,7 +5,8 @@ use std::fmt::Write;use crate::plan::{CALLBACK_BUDGET, CURSOR_LOCAL, CURSOR_STRUCT, CallbackKind, CallbackStub, FUZZ_DATA, FUZZ_SIZE,- HarnessPlan, Init, MAX_CALLBACK_CALLS, NUL_COPY_LOCAL, Stmt, Value,+ HarnessPlan, Init, MAX_CALLBACK_CALLS, MAX_OUT_BYTES, NUL_COPY_LOCAL, OUT_CAPACITY, OUT_LOCAL,+ Stmt, Value,};pub const CALLBACK_COUNTER: &str = "lisdex_callback_calls";@@ -43,6 +44,10 @@ fn render_includes(out: &mut String, plan: &HarnessPlan) {}fn render_support(out: &mut String, plan: &HarnessPlan) {+ if plan.needs_out_buffer {+ let _ = writeln!(out, "#define {OUT_CAPACITY} {MAX_OUT_BYTES}");+ let _ = writeln!(out);+ }if !plan.needs_cursor && plan.callbacks.is_empty() {return;}@@ -149,11 +154,14 @@ fn render_entry(out: &mut String, plan: &HarnessPlan) {let _ = writeln!(out, "{{");render_prologue(out, plan);for statement in &plan.body {- render_stmt(out, statement, plan.needs_nul_copy);+ render_stmt(out, statement, plan);}if plan.needs_nul_copy {let _ = writeln!(out, " free({NUL_COPY_LOCAL});");}+ if plan.needs_out_buffer {+ let _ = writeln!(out, " free({OUT_LOCAL});");+ }let _ = writeln!(out, " return 0;");let _ = writeln!(out, "}}");}@@ -178,6 +186,23 @@ fn render_prologue(out: &mut String, plan: &HarnessPlan) {let _ = writeln!(out, " {NUL_COPY_LOCAL}[{FUZZ_SIZE}] = '\\0';");wrote = true;}+ // Heap rather than a stack array so ASAN's redzones bound it exactly: an overrun is+ // reported as a heap-buffer-overflow with the library's own frames on top, instead of+ // smashing the harness frame and being read as a stack exhaustion in a deep parser.+ if plan.needs_out_buffer {+ let _ = writeln!(+ out,+ " unsigned char *{OUT_LOCAL} = (unsigned char *)malloc({OUT_CAPACITY});"+ );+ let _ = writeln!(out, " if ({OUT_LOCAL} == NULL) {{");+ if plan.needs_nul_copy {+ let _ = writeln!(out, " free({NUL_COPY_LOCAL});");+ }+ let _ = writeln!(out, " return 0;");+ let _ = writeln!(out, " }}");+ let _ = writeln!(out, " memset({OUT_LOCAL}, 0, {OUT_CAPACITY});");+ wrote = true;+ }if plan.needs_cursor {let _ = writeln!(out, " {CURSOR_LOCAL}.data = {FUZZ_DATA};");let _ = writeln!(out, " {CURSOR_LOCAL}.size = {FUZZ_SIZE};");@@ -193,7 +218,7 @@ fn render_prologue(out: &mut String, plan: &HarnessPlan) {}}-fn render_stmt(out: &mut String, statement: &Stmt, holds_nul_copy: bool) {+fn render_stmt(out: &mut String, statement: &Stmt, plan: &HarnessPlan) {match statement {Stmt::Declare { ty, name, init } => match init {Init::Null => {@@ -206,6 +231,9 @@ fn render_stmt(out: &mut String, statement: &Stmt, holds_nul_copy: bool) {Init::Uninitialised => {let _ = writeln!(out, " {ty} {name};");}+ Init::Capacity => {+ let _ = writeln!(out, " {ty} {name} = {OUT_CAPACITY};");+ }},Stmt::SetField {local,@@ -237,10 +265,10 @@ fn render_stmt(out: &mut String, statement: &Stmt, holds_nul_copy: bool) {let _ = writeln!(out, " (void){local};");}Stmt::BailIfNull { local } => {- render_bail(out, &format!("{local} == NULL"), holds_nul_copy);+ render_bail(out, &format!("{local} == NULL"), plan);}Stmt::BailIfZero { local } => {- render_bail(out, &format!("{local} == 0"), holds_nul_copy);+ render_bail(out, &format!("{local} == 0"), plan);}}}@@ -248,11 +276,14 @@ fn render_stmt(out: &mut String, statement: &Stmt, holds_nul_copy: bool) {// 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) {+fn render_bail(out: &mut String, condition: &str, plan: &HarnessPlan) {let _ = writeln!(out, " if ({condition}) {{");- if holds_nul_copy {+ if plan.needs_nul_copy {let _ = writeln!(out, " free({NUL_COPY_LOCAL});");}+ if plan.needs_out_buffer {+ let _ = writeln!(out, " free({OUT_LOCAL});");+ }let _ = writeln!(out, " return 0;");let _ = writeln!(out, " }}");}@@ -271,6 +302,11 @@ fn render_value(value: &Value) -> String {Value::AddressOfField { local, field } => format!("&{local}.{field}"),Value::Cursor => format!("&{CURSOR_LOCAL}"),Value::NulTerminated => NUL_COPY_LOCAL.to_owned(),+ Value::OutBuffer { cast } => match cast {+ Some(ty) => format!("({ty}){OUT_LOCAL}"),+ None => OUT_LOCAL.to_owned(),+ },+ Value::OutCapacity => OUT_CAPACITY.to_owned(),}}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 b44633b2..b994a599 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@@ -8,6 +8,10 @@ pub const CURSOR_LOCAL: &str = "lisdex_input";pub const CALLBACK_BUDGET: &str = "LISDEX_CALLBACK_BUDGET";pub const MAX_CALLBACK_CALLS: u32 = 4096;pub const NUL_COPY_LOCAL: &str = "lisdex_text";+pub const OUT_LOCAL: &str = "lisdex_out";+pub const OUT_LEN_LOCAL: &str = "lisdex_out_len";+pub const OUT_CAPACITY: &str = "LISDEX_OUT_CAPACITY";+pub const MAX_OUT_BYTES: usize = 65536;#[derive(Debug, Clone, PartialEq, Eq)]pub enum Value {@@ -22,6 +26,13 @@ pub enum Value {Callback(String),Cursor,NulTerminated,+ // A caller-allocated output buffer belongs to the HARNESS, so its extent is not+ // something to infer about the library - it is something the harness declares and the+ // library is obliged to honour. That is what separates it from the out-params that+ // must stay refused: png_read_image and tj3Decompress8 take no capacity at all, so+ // the size is knowledge the caller is expected to already hold.+ OutBuffer { cast: Option<String> },+ OutCapacity,}#[derive(Debug, Clone, Copy, PartialEq, Eq)]@@ -29,6 +40,7 @@ pub enum Init {Uninitialised,Null,ZeroBytes,+ Capacity,}#[derive(Debug, Clone, PartialEq, Eq)]@@ -85,6 +97,7 @@ pub struct HarnessPlan {pub includes: Vec<String>,pub needs_cursor: bool,pub needs_nul_copy: bool,+ pub needs_out_buffer: bool,pub callbacks: Vec<CallbackStub>,pub body: Vec<Stmt>,// Named separately from `body` because "which calls came from the author's own sequence@@ -101,6 +114,7 @@ impl HarnessPlan {includes: Vec::new(),needs_cursor: false,needs_nul_copy: false,+ needs_out_buffer: false,callbacks: Vec::new(),body: Vec::new(),replayed: Vec::new(),@@ -131,6 +145,12 @@ impl HarnessPlan {if args.contains(&Value::NulTerminated) {self.needs_nul_copy = true;}+ if args+ .iter()+ .any(|value| matches!(value, Value::OutBuffer { .. }))+ {+ self.needs_out_buffer = true;+ }}self.body.push(statement);selfdiff --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 f6d563ce..3da6de97 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@@ -6,9 +6,11 @@ use std::collections::{BTreeMap, BTreeSet};use lisdex_surface::extract::{Vocabulary, carries_word, identifier_words};use lisdex_surface::naming::polarity_of;use lisdex_surface::rank::Candidate;-use lisdex_surface::signature::{Fuzzability, Param, ParamRole, Pointee, Shape, handle_key};+use lisdex_surface::signature::{+ Analysis, Fuzzability, Param, ParamRole, Pointee, Shape, handle_key,+};-use crate::plan::{Binding, HarnessPlan, Init, Stmt, Value};+use crate::plan::{Binding, HarnessPlan, Init, OUT_LEN_LOCAL, Stmt, Value};pub const CONTEXT_LOCAL: &str = "ctx";pub const RESULT_LOCAL: &str = "rc";@@ -27,6 +29,7 @@ pub const FILESYSTEM_VERBS: [&str; 12] = [pub enum Refusal {NoDataInput,UnsizedOutParam { param: String },+ IndirectExtent { param: String },OpaqueContext { record: String },UnboundExtent { param: String },NeedsCallback { param: String },@@ -46,6 +49,10 @@ impl std::fmt::Display for Refusal {formatter,"out-param {param} needs a caller-supplied extent that headers cannot give; guessing it fabricates a crash inside the library"),+ Refusal::IndirectExtent { param } => write!(+ formatter,+ "extent {param} is passed by address, so the library may rewrite the length of a buffer it does not own"+ ),Refusal::OpaqueContext { record } => write!(formatter,"context {record} is opaque and needs a producer chain"@@ -372,11 +379,57 @@ impl Held {}}+// zlib declares uncompress(Bytef *dest, uLongf *destLen, ...): the caller writes the+// capacity in and the library writes the decoded length back out. The local has to carry+// the library's OWN spelling, because a size_t where a uLongf is wanted is a type error on+// any target where they differ.+fn indirect_extent_local(candidate: &Candidate) -> Option<Stmt> {+ let extent = candidate+ .analysis+ .extents+ .iter()+ .find(|(pointer, extent)| {+ candidate.analysis.roles[**pointer] == ParamRole::Output+ && is_indirect(&candidate.params[**extent])+ })+ .map(|(_, extent)| *extent)?;+ let pointee = candidate.params[extent]+ .spelling+ .trim()+ .strip_suffix('*')?+ .trim()+ .to_owned();+ Some(Stmt::Declare {+ ty: declarable(&pointee),+ name: OUT_LEN_LOCAL.to_owned(),+ init: Init::Capacity,+ })+}++fn owns_this_extent(analysis: &Analysis, extent: usize) -> Option<usize> {+ analysis+ .extents+ .iter()+ .find(|(_, owned)| **owned == extent)+ .map(|(pointer, _)| *pointer)+}++fn is_indirect(param: &Param) -> bool {+ matches!(+ ¶m.shape,+ Shape::Pointer {+ pointee: Pointee::Integral,+ ..+ }+ )+}+fn bind(+ index: usize,param: &Param,role: ParamRole,locals: &BTreeMap<String, Held>,- sized: bool,+ analysis: &Analysis,) -> Result<Value, Refusal> {let label = param.name.clone().unwrap_or_else(|| param.spelling.clone());match role {@@ -385,11 +438,13 @@ fn bind(pointee: Pointee::Char | Pointee::SignedChar,..} => {- // A char pointer with no length beside it is a STRING, not a byte buffer.+ // A char pointer with no length OF ITS OWN is a STRING, not a byte buffer.// zlib's real gzopen prototype is unnamed, so the path detector could not// fire and both arguments took the fuzz copy: the harness then created and- // TRUNCATED files in the working directory named by corpus bytes.- if sized {+ // TRUNCATED files in the working directory named by corpus bytes. Asking+ // whether the SIGNATURE had any length at all was the weaker question -+ // LZ4_decompress_safe_partial_usingDict has three and none of them is src's.+ if analysis.extents.contains_key(&index) {Ok(Value::NulTerminated)} else {Err(Refusal::UnsizedTextParam { param: label })@@ -399,7 +454,27 @@ fn bind(cast: cast_for(param)?,}),},- ParamRole::Length => Ok(Value::Size),+ ParamRole::Length => {+ let owner = owns_this_extent(analysis, index);+ let owned_by_output =+ owner.is_some_and(|pointer| analysis.roles[pointer] == ParamRole::Output);+ if owned_by_output {+ // The capacity of a buffer the HARNESS allocated, so it is a fact, not an+ // inference. An in/out extent carries it in and the decoded length back out.+ return Ok(if is_indirect(param) {+ Value::AddressOf(OUT_LEN_LOCAL.to_owned())+ } else {+ Value::OutCapacity+ });+ }+ // The fuzz data's own length cannot be handed over by address without a local+ // the library is then free to overwrite; emitting `size` where `size_t *` is+ // wanted does not compile, so say so here rather than spend a compile on it.+ if is_indirect(param) {+ return Err(Refusal::IndirectExtent { param: label });+ }+ Ok(Value::Size)+ }ParamRole::Config => {if is_extent_name(&label) {return Err(Refusal::UnboundExtent { param: label });@@ -409,6 +484,22 @@ fn bind(ParamRole::Callback => Err(Refusal::NeedsCallback { param: label }),ParamRole::Path => Err(Refusal::PathEntry),ParamRole::Stream => Err(Refusal::UnsupportedShape { param: label }),+ ParamRole::Output+ if matches!(¶m.shape, Shape::Pointer { pointee, .. }+ if pointee.is_byte_sized()) =>+ {+ // The class B67 was written for. WITH a capacity parameter the library is under+ // contract not to pass it, so an overrun is the library's bug and ASAN says so.+ // WITHOUT one - png_read_image, tj3Decompress8 - the size is knowledge the+ // caller is expected to already hold, and guessing it manufactures the crash.+ if analysis.extents.contains_key(&index) {+ Ok(Value::OutBuffer {+ cast: cast_for(param)?,+ })+ } else {+ Err(Refusal::UnsizedOutParam { param: label })+ }+ }ParamRole::Output | ParamRole::Context => {if is_double_pointer(param) {return Err(Refusal::UnsizedOutParam { param: label });@@ -682,7 +773,6 @@ pub fn plan_candidate_with(setup: &[String],) -> Result<Vec<HarnessPlan>, Refusal> {admissible(candidate)?;- let sized = candidate.analysis.roles.contains(&ParamRole::Length);let Contexts {locals,@@ -691,12 +781,20 @@ pub fn plan_candidate_with(} = resolve_contexts(candidate, vocabulary)?;let mut args = Vec::with_capacity(candidate.params.len());- for (param, role) in candidate.params.iter().zip(&candidate.analysis.roles) {- args.push(bind(param, *role, &locals, sized)?);+ for (index, (param, role)) in candidate+ .params+ .iter()+ .zip(&candidate.analysis.roles)+ .enumerate()+ {+ args.push(bind(index, param, *role, &locals, &candidate.analysis)?);}let mut emitted = BTreeSet::new();let mut base = HarnessPlan::new(candidate.name.clone()).with_include(include);+ if let Some(statement) = indirect_extent_local(candidate) {+ base = base.with(statement);+ }for statement in opening {if let Stmt::Call { function, .. } = &statement {emitted.insert(function.clone());@@ -810,6 +908,29 @@ mod tests {}}+ // These fixtures hand-write their role vector to isolate BINDING, so they need an+ // extent map to match. Nearest-Length is enough for shapes this simple; the assignment+ // rule itself is measured in lisdex-surface, not here.+ fn nearest_extents(roles: &[ParamRole]) -> BTreeMap<usize, usize> {+ let mut extents = BTreeMap::new();+ let mut claimed: BTreeSet<usize> = BTreeSet::new();+ for (index, role) in roles.iter().enumerate() {+ if !matches!(role, ParamRole::Buffer | ParamRole::Output) {+ continue;+ }+ let found = [index.saturating_add(1), index.wrapping_sub(1)]+ .into_iter()+ .find(|near| {+ roles.get(*near) == Some(&ParamRole::Length) && !claimed.contains(near)+ });+ if let Some(extent) = found {+ extents.insert(index, extent);+ claimed.insert(extent);+ }+ }+ extents+ }+fn candidate(name: &str,params: Vec<Param>,@@ -817,6 +938,7 @@ mod tests {fuzzability: Fuzzability,result: Returns,) -> Candidate {+ let extents = nearest_extents(&roles);Candidate {name: name.to_owned(),params,@@ -828,6 +950,7 @@ mod tests {fuzzability,roles,context_args: 1,+ extents,},complexity: Complexity::default(),exported: true,@@ -2127,4 +2250,190 @@ mod tests {};assert_eq!(args[0], Value::Data { cast: None });}++ fn byte_pointer(name: &str, spelling: &str, is_const: bool) -> Param {+ param(+ name,+ spelling,+ Shape::Pointer {+ pointee: Pointee::UnsignedChar,+ is_const,+ },+ )+ }++ fn owning(mut entry: Candidate, extents: &[(usize, usize)]) -> Candidate {+ entry.analysis.extents = extents.iter().copied().collect();+ entry+ }++ // B67. The out-param is caller-allocated AND the signature says how much the library+ // may write, so the harness declares the extent rather than inferring it.+ #[test]+ fn an_out_param_with_a_capacity_gets_a_buffer_the_harness_owns() {+ let decode = owning(+ candidate(+ "lz4_decode",+ vec![+ byte_pointer("src", "const unsigned char *", true),+ byte_pointer("dst", "unsigned char *", false),+ param("srcSize", "size_t", Shape::Integral),+ param("dstCapacity", "size_t", Shape::Integral),+ ],+ vec![+ ParamRole::Buffer,+ ParamRole::Output,+ ParamRole::Length,+ ParamRole::Length,+ ],+ Fuzzability::DirectBuffer,+ Returns::default(),+ ),+ &[(0, 2), (1, 3)],+ );+ let plans = plan_candidate(&decode, "x.h", &Vocabulary::default()).unwrap();+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {+ unreachable!()+ };+ assert_eq!(args[0], Value::Data { cast: None });+ assert_eq!(args[1], Value::OutBuffer { cast: None });+ assert_eq!(args[2], Value::Size, "the fuzz data's own length");+ assert_eq!(+ args[3],+ Value::OutCapacity,+ "the capacity the harness actually allocated, not the fuzz size"+ );+ assert!(plans[0].needs_out_buffer);+ }++ // The control for the clause above, and the class the refusal was written for.+ // tj3Decompress8 takes a dstBuf and no capacity at all: its size is width * height *+ // pixel size, which the caller is expected to already know.+ #[test]+ fn an_out_param_with_no_capacity_anywhere_is_refused_not_guessed() {+ let decode = owning(+ candidate(+ "tj3_decompress",+ vec![+ byte_pointer("jpegBuf", "const unsigned char *", true),+ param("jpegSize", "size_t", Shape::Integral),+ byte_pointer("dstBuf", "unsigned char *", false),+ ],+ vec![ParamRole::Buffer, ParamRole::Length, ParamRole::Output],+ Fuzzability::DirectBuffer,+ Returns::default(),+ ),+ &[(0, 1)],+ );+ assert!(matches!(+ plan_candidate(&decode, "x.h", &Vocabulary::default()),+ Err(Refusal::UnsizedOutParam { param }) if param == "dstBuf"+ ));+ }++ // Isolates per-buffer sizing from the old whole-signature question. There IS a Length+ // here, it just is not src's - and asking only whether the signature had one let+ // lz4's src take the fuzz copy on the strength of the OUTPUT's capacity.+ #[test]+ fn a_char_buffer_is_a_string_when_the_only_extent_belongs_to_another_buffer() {+ let decode = owning(+ candidate(+ "lz4_decode_partial",+ vec![+ param(+ "src",+ "const char *",+ Shape::Pointer {+ pointee: Pointee::Char,+ is_const: true,+ },+ ),+ byte_pointer("dst", "unsigned char *", false),+ param("dstCapacity", "int", Shape::Integral),+ ],+ vec![ParamRole::Buffer, ParamRole::Output, ParamRole::Length],+ Fuzzability::CastBuffer,+ Returns::default(),+ ),+ &[(1, 2)],+ );+ assert!(matches!(+ plan_candidate(&decode, "x.h", &Vocabulary::default()),+ Err(Refusal::UnsizedTextParam { param }) if param == "src"+ ));+ }++ // zlib's uncompress writes the decoded length back through destLen, so the local has+ // to carry the library's own spelling - uLongf is not size_t everywhere.+ #[test]+ fn an_in_out_capacity_is_declared_with_the_librarys_own_spelling() {+ let decode = owning(+ candidate(+ "uncompress",+ vec![+ byte_pointer("dest", "Bytef *", false),+ param(+ "destLen",+ "uLongf *",+ Shape::Pointer {+ pointee: Pointee::Integral,+ is_const: false,+ },+ ),+ byte_pointer("source", "const Bytef *", true),+ param("sourceLen", "uLong", Shape::Integral),+ ],+ vec![+ ParamRole::Output,+ ParamRole::Length,+ ParamRole::Buffer,+ ParamRole::Length,+ ],+ Fuzzability::DirectBuffer,+ Returns::default(),+ ),+ &[(0, 1), (2, 3)],+ );+ let plans = plan_candidate(&decode, "zlib.h", &Vocabulary::default()).unwrap();+ assert!(plans[0].body.contains(&Stmt::Declare {+ ty: "uLongf".to_owned(),+ name: OUT_LEN_LOCAL.to_owned(),+ init: Init::Capacity,+ }));+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {+ unreachable!()+ };+ assert_eq!(args[1], Value::AddressOf(OUT_LEN_LOCAL.to_owned()));+ assert_eq!(args[3], Value::Size);+ }++ // The fuzz data's length cannot be handed over by address: the local would be the+ // library's to rewrite, and it describes memory the harness does not own.+ #[test]+ fn an_input_extent_passed_by_address_is_refused() {+ let decode = owning(+ candidate(+ "uncompress2",+ vec![+ byte_pointer("source", "const Bytef *", true),+ param(+ "sourceLen",+ "uLong *",+ Shape::Pointer {+ pointee: Pointee::Integral,+ is_const: false,+ },+ ),+ ],+ vec![ParamRole::Buffer, ParamRole::Length],+ Fuzzability::DirectBuffer,+ Returns::default(),+ ),+ &[(0, 1)],+ );+ assert!(matches!(+ plan_candidate(&decode, "zlib.h", &Vocabulary::default()),+ Err(Refusal::IndirectExtent { param }) if param == "sourceLen"+ ));+ }}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.rsindex d054d29f..ece0a071 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@@ -1,7 +1,7 @@// ©AngelaMos | 2026// signature.rs-use std::collections::BTreeSet;+use std::collections::{BTreeMap, BTreeSet};use crate::naming::tokenize;@@ -15,6 +15,8 @@ pub const SCORE_NO_INPUT: f64 = 0.0;pub const MIN_SUBSTRING_MATCH: usize = 3;+pub const AFFINITY_SHARED_TOKENS: usize = 100;+pub const STREAM_RECORDS: &[&str] = &["FILE", "_IO_FILE", "__sFILE"];pub const LENGTH_NAMES: &[&str] = &[@@ -201,6 +203,12 @@ pub struct Analysis {pub fuzzability: Fuzzability,pub roles: Vec<ParamRole>,pub context_args: usize,+ // Which parameter is the extent OF which pointer, rather than the single global pair+ // the signature used to carry. A decoder declares several at once -+ // LZ4_decompress_safe(src, dst, compressedSize, dstCapacity) has two - and binding one+ // buffer's length to another buffer is how a harness hands a library a lie about+ // memory it does not own.+ pub extents: BTreeMap<usize, usize>,}fn name_matches(name: Option<&String>, candidates: &[&str]) -> bool {@@ -270,6 +278,148 @@ fn carries_extent(role: ParamRole, param: &Param) -> bool {matches!(role, ParamRole::Config | ParamRole::Context) && is_length_like(param)}+// How strongly `extent` reads as the length OF `pointer`. Measured over 81 real signatures+// on seven upstream trees, this is the only tier that survives a count mismatch:+// LZ4_decompress_safe_partial(src, dst, srcSize, targetOutputSize, dstCapacity) has three+// extents for two buffers, and only the names say that targetOutputSize belongs to neither.+//+// A prefix arm and a substring arm were built beside this one and BOTH WERE DELETED: gutting+// them changed the extent map of exactly ZERO candidates across all seven trees. The+// concatenated-lowercase shapes they were written for - blake2xb's `out`/`outlen`,+// `in`/`inlen` - are already placed by the position tier, which needs no name at all. Two+// scoring arms that never decide anything can still decide something WRONG on a tree nobody+// has measured, so they earn their place by measurement or they do not ship.+fn extent_affinity(pointer: Option<&String>, extent: Option<&String>) -> usize {+ let run = shared_leading_run(pointer, extent);+ if run > 0 {+ AFFINITY_SHARED_TOKENS + run+ } else {+ 0+ }+}++fn extent_candidates(params: &[Param], roles: &[ParamRole]) -> Vec<usize> {+ (0..params.len())+ .filter(|index| carries_extent(roles[*index], ¶ms[*index]))+ .collect()+}++// Context is in this set because a byte pointer is never a real context - `handle_key`+// returns None for one, so it can only ever refuse. brotli declares+// `uint8_t decoded_buffer[]`, which is not spelled out/dst/dest/output/result and so fell+// to Context and refused the library's canonical decode entry. What makes a parameter an+// output buffer is being writable and carrying a declared capacity, not being named for it.+fn extentable_pointers(params: &[Param], roles: &[ParamRole]) -> Vec<usize> {+ (0..params.len())+ .filter(|index| {+ matches!(+ roles[*index],+ ParamRole::Buffer | ParamRole::Output | ParamRole::Context+ )+ })+ .filter(|index| {+ matches!(¶ms[*index].shape, Shape::Pointer { pointee, .. }+ if pointee.is_byte_sized())+ })+ // `typedef void *tjhandle` is byte-sized and non-const, so it grades Context beside+ // the real output buffers. Promoting one to an output would hand libjpeg-turbo a+ // zeroed malloc where it expects an initialised handle and SEGV inside the library+ // on the first seed - the D12 class, manufactured by us. An opaque handle is+ // whatever it was SPELLED as, which is the question handle_key already answers.+ .filter(|index| {+ roles[*index] != ParamRole::Context+ || handle_key(¶ms[*index].spelling, ¶ms[*index].shape).is_none()+ })+ .collect()+}++// Every buffer's own extent, not the one strongest pair in the signature. Adjacency alone+// could not see past an intervening pointer, so LZ4_decompress_safe's `compressedSize`+// graded as a Config and its `src` was refused as an unsized string - which is why lz4+// emitted nothing at all.+fn assign_extents(params: &[Param], roles: &[ParamRole]) -> BTreeMap<usize, usize> {+ let pointers = extentable_pointers(params, roles);+ let extents = extent_candidates(params, roles);+ let mut owned: BTreeMap<usize, usize> = BTreeMap::new();+ let mut claimed: BTreeSet<usize> = BTreeSet::new();++ let mut scored: Vec<(usize, usize, usize)> = Vec::new();+ for pointer in &pointers {+ for extent in &extents {+ let score = extent_affinity(+ params[*pointer].name.as_ref(),+ params[*extent].name.as_ref(),+ );+ if score > 0 {+ scored.push((score, *pointer, *extent));+ }+ }+ }+ scored.sort_by(|one, other| {+ other+ .0+ .cmp(&one.0)+ .then_with(|| one.1.cmp(&other.1))+ .then_with(|| one.2.cmp(&other.2))+ });+ for (_, pointer, extent) in scored {+ if owned.contains_key(&pointer) || claimed.contains(&extent) {+ continue;+ }+ owned.insert(pointer, extent);+ claimed.insert(extent);+ }++ // Position is a statement about the whole signature and adjacency is a statement about+ // one gap, so position speaks first. Running adjacency ahead of it gave+ // LZ4_uncompress_unknownOutputSize(source, dest, isize, maxOutputSize) the pair+ // dest=isize, and the leftover then handed `source` the output capacity - both lies.+ let unowned: Vec<usize> = pointers+ .iter()+ .copied()+ .filter(|pointer| !owned.contains_key(pointer))+ .collect();+ let unclaimed: Vec<usize> = extents+ .iter()+ .copied()+ .filter(|extent| !claimed.contains(extent))+ .collect();+ if !unowned.is_empty() && unowned.len() == unclaimed.len() {+ for (pointer, extent) in unowned.iter().zip(&unclaimed) {+ owned.insert(*pointer, *extent);+ claimed.insert(*extent);+ }+ }++ // Adjacency is what is left when there is no name to reason with - libtiff and+ // libarchive declare their parameters unnamed - or when only one pointer is still+ // asking, so there is nothing for it to be confused with. Letting it decide a NAMED+ // signature with two claimants gave lz4's dst the extent of its src.+ let unowned: Vec<usize> = pointers+ .iter()+ .copied()+ .filter(|pointer| !owned.contains_key(pointer))+ .collect();+ let unambiguous = unowned.len() == 1;+ for pointer in unowned {+ if params[pointer].name.is_some() && !unambiguous {+ continue;+ }+ let follower = pointer+ .checked_add(1)+ .filter(|next| extents.contains(next) && !claimed.contains(next));+ let leader = pointer+ .checked_sub(1)+ .filter(|prior| extents.contains(prior) && !claimed.contains(prior));+ if let Some(extent) = follower.or(leader) {+ owned.insert(pointer, extent);+ claimed.insert(extent);+ }+ }++ owned+}+fn buffer_quality(param: &Param) -> u8 {match ¶m.shape {Shape::Pointer { pointee, .. } if pointee.is_exact_byte() => 3,@@ -346,6 +496,7 @@ pub fn analyze_named(name: &str, params: &[Param], streams: &BTreeSet<String>) -*role = ParamRole::Config;}}+ analysis.extents.clear();analysis.fuzzability = Fuzzability::Path;analysis.context_args = analysis.roles@@ -385,58 +536,30 @@ pub fn analyze_with_streams(params: &[Param], streams: &BTreeSet<String>) -> Anafn grade_signature(params: &[Param]) -> Analysis {let mut roles: Vec<ParamRole> = params.iter().map(provisional_role).collect();- let mut paired: Option<usize> = None;- let mut best = 0;-- // Score every buffer+length pair and keep the strongest, rather than breaking on- // the first. png_set_iCCP(const char *name, int type, const png_byte *profile,- // png_uint_32 proflen) otherwise pairs `name` with the enum `type` and grades on- // that, leaving the real byte buffer unpaired.- for index in 0..params.len() {- if roles[index] != ParamRole::Buffer {- continue;- }- let quality = buffer_quality(¶ms[index]);- let follower = index- .checked_add(1)- .filter(|next| *next < params.len())- .filter(|next| carries_extent(roles[*next], ¶ms[*next]));- // A length may also precede its buffer: BrotliDecoderDecompress declares- // (size_t encoded_size, const uint8_t encoded_buffer[], ...).- let leader = index- .checked_sub(1)- .filter(|prior| carries_extent(roles[*prior], ¶ms[*prior]));- // When a buffer is flanked on both sides, the follower used to win unconditionally.- // BrotliDecoderDecompress(size_t encoded_size, const uint8_t encoded_buffer[],- // size_t *decoded_size, uint8_t decoded_buffer[]) gave `encoded_buffer` the extent of- // the OUTPUT buffer and left its own beside it as a config, so the planner refused- // the canonical decode entry of the library. The name says which one owns it.- let flanked = match (follower, leader) {- (Some(after), Some(before)) => {- let buffer = params[index].name.as_ref();- let leader_owns_it = shared_leading_run(buffer, params[before].name.as_ref())- > shared_leading_run(buffer, params[after].name.as_ref());- Some(if leader_owns_it { before } else { after })- }- (after, before) => after.or(before),- };- if let Some(extent) = flanked- && quality >= best- {- if let Some(previous) = paired- && let Some(stale) = roles.iter_mut().find(|role| **role == ParamRole::Length)- && previous != index- {- *stale = ParamRole::Config;- }- roles[extent] = ParamRole::Length;- paired = Some(index);- best = quality;+ let extents = assign_extents(params, &roles);+ for extent in extents.values() {+ roles[*extent] = ParamRole::Length;+ }+ // A writable byte pointer that ended up owning an extent IS the output buffer, whatever+ // it was called. Leaving it graded Context sent it to the handle rule, which has no+ // binding for a byte pointer and could only refuse.+ for pointer in extents.keys() {+ if roles[*pointer] == ParamRole::Context {+ roles[*pointer] = ParamRole::Output;}}+ // Which buffer the signature is GRADED on is still a single choice, and it is the+ // strongest fuzzable one that knows its own length. png_set_iCCP(const char *name,+ // int type, const png_byte *profile, png_uint_32 proflen) must grade on `profile`.+ let paired = extents+ .keys()+ .copied()+ .filter(|index| roles[*index] == ParamRole::Buffer)+ .max_by_key(|index| buffer_quality(¶ms[*index]));+for index in 0..params.len() {- if roles[index] != ParamRole::Buffer || paired == Some(index) {+ if roles[index] != ParamRole::Buffer || extents.contains_key(&index) {continue;}if lone_pointer_is_path(¶ms[index]) {@@ -454,6 +577,7 @@ fn grade_signature(params: &[Param]) -> Analysis {fuzzability,roles,context_args,+ extents,}}@@ -766,11 +890,20 @@ mod tests {];let analysis = analyze(¶ms);assert_eq!(- analysis.roles[0],- ParamRole::Length,+ analysis.extents.get(&1),+ Some(&0),"encoded_buffer's extent is encoded_size, not the output's decoded_size");- assert_ne!(analysis.roles[2], ParamRole::Length);+ assert_eq!(+ analysis.extents.get(&3),+ Some(&2),+ "and decoded_size is the OUTPUT's, which is why taking it was wrong"+ );+ assert_eq!(+ analysis.roles[3],+ ParamRole::Output,+ "a writable byte pointer owning an extent is an output however it is spelled"+ );}// The control: with no name affinity to go on, the follower still wins. zlib's@@ -1035,4 +1168,190 @@ mod tests {assert_eq!(analysis.roles[1], ParamRole::Buffer);assert_eq!(analysis.fuzzability, Fuzzability::CastBuffer);}++ // Isolates the NAME tier. Both other tiers answer this signature and both answer it+ // wrongly: position pairs data with out_size, and adjacency pairs data with the+ // integral that follows it, which is also out_size.+ #[test]+ fn an_extent_belongs_to_the_buffer_it_is_named_after_not_the_one_beside_it() {+ let params = [+ pointer("data", Pointee::UnsignedChar, true),+ integral("out_size"),+ pointer("out", Pointee::UnsignedChar, false),+ integral("data_size"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(analysis.extents.get(&0), Some(&3), "data owns data_size");+ assert_eq!(analysis.extents.get(&2), Some(&1), "out owns out_size");+ }++ // Isolates the SHARED-TOKEN arm of the name tier from the prefix arm beside it, which+ // alibied it on the fixture above: neither `dictStart` nor `srcBuffer` is a prefix or a+ // substring of its own extent, so only a shared leading TOKEN can place them. Without+ // it, position zips the two pairs crosswise and both buffers get the other's length.+ // Real lz4: LZ4_decompress_safe_usingDict declares dictStart beside dictSize.+ #[test]+ fn a_shared_leading_token_places_an_extent_that_is_no_prefix_of_its_buffer() {+ let params = [+ pointer("dictStart", Pointee::UnsignedChar, true),+ pointer("srcBuffer", Pointee::UnsignedChar, true),+ integral("srcSize"),+ integral("dictSize"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(+ analysis.extents.get(&0),+ Some(&3),+ "dictStart owns dictSize"+ );+ assert_eq!(analysis.extents.get(&1), Some(&2), "srcBuffer owns srcSize");+ }++ // Isolates the POSITION tier: no name here shares anything with any other, so the name+ // tier is silent, and adjacency would hand `dest` the extent of `source`. Real lz4.+ #[test]+ fn two_unrelated_names_pair_by_position_rather_than_by_the_nearest_integral() {+ let params = [+ pointer("source", Pointee::Char, true),+ pointer("dest", Pointee::Char, false),+ integral("isize"),+ integral("maxOutputSize"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(analysis.extents.get(&0), Some(&2), "source owns isize");+ assert_eq!(+ analysis.extents.get(&1),+ Some(&3),+ "dest owns maxOutputSize, and taking isize would be the input's extent"+ );+ }++ // Isolates the ADJACENCY tier: libarchive declares these unnamed, so the name tier+ // cannot speak, and two pointers against one extent leaves position nothing to zip.+ #[test]+ fn an_unnamed_buffer_still_takes_the_unnamed_extent_beside_it() {+ let params = [+ pointer("a", Pointee::Record("archive".to_owned()), false),+ Param {+ name: None,+ spelling: "const char *".to_owned(),+ shape: Shape::Pointer {+ pointee: Pointee::Char,+ is_const: true,+ },+ },+ Param {+ name: None,+ spelling: "const void *".to_owned(),+ shape: Shape::Pointer {+ pointee: Pointee::Void,+ is_const: true,+ },+ },+ typed_integral(None, "size_t"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(analysis.extents.get(&2), Some(&3));+ assert_eq!(+ analysis.extents.get(&1),+ None,+ "the command string has none"+ );+ }++ // Isolates the "last one asking" clause of the adjacency tier. `buff` is NAMED, so the+ // unnamed-parameter clause cannot carry it, and two extents against one pointer leaves+ // position nothing to zip. Real libarchive: archive_read_open_memory2.+ #[test]+ fn the_only_buffer_left_asking_takes_the_extent_beside_it_even_when_named() {+ let params = [+ pointer("a", Pointee::Record("archive".to_owned()), false),+ pointer("buff", Pointee::Void, true),+ integral("size"),+ integral("read_size"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(analysis.extents.get(&1), Some(&2));+ }++ // The control for that clause. Two NAMED buffers are both still asking and only one+ // integral is on offer, so there is no evidence which of them owns it. lz4's+ // LZ4_decompress_fast is the real case, and `originalSize` is the OUTPUT's extent -+ // handing it to `src` would tell the library the fuzz data is as long as the data it+ // is about to produce.+ #[test]+ fn two_named_buffers_and_one_extent_is_ambiguous_so_neither_takes_it() {+ let params = [+ pointer("src", Pointee::Char, true),+ pointer("dst", Pointee::Char, false),+ integral("originalSize"),+ ];+ let analysis = analyze(¶ms);+ assert!(+ analysis.extents.is_empty(),+ "guessing here is the fabricated-crash class, got {:?}",+ analysis.extents+ );+ }++ // The whole point of B69: the extent is not adjacent to the buffer it belongs to,+ // because the output buffer sits between them. Real lz4, and every decoder in the+ // library carries this shape.+ #[test]+ fn a_decoder_whose_output_sits_between_its_input_and_its_length_still_pairs() {+ let params = [+ pointer("src", Pointee::Char, true),+ pointer("dst", Pointee::Char, false),+ integral("compressedSize"),+ integral("dstCapacity"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(+ analysis.extents.get(&0),+ Some(&2),+ "src owns compressedSize"+ );+ assert_eq!(analysis.extents.get(&1), Some(&3), "dst owns dstCapacity");+ assert_eq!(analysis.roles[2], ParamRole::Length);+ assert_eq!(analysis.roles[3], ParamRole::Length);+ }++ // An opaque handle is byte-sized and writable, so it sits in the same set as the real+ // output buffers. It must never win an extent: promoting it would hand libjpeg-turbo a+ // zeroed allocation where it expects an initialised handle, and the SEGV would be ours.+ // Only the handle rule can decide this fixture - `jpegSize` shares no name with+ // `handle`, and the name tier is what would otherwise place it.+ #[test]+ fn an_opaque_handle_typedef_never_takes_an_extent_and_never_becomes_an_output() {+ let params = [+ Param {+ name: Some("handle".to_owned()),+ spelling: "tjhandle".to_owned(),+ shape: Shape::Pointer {+ pointee: Pointee::Void,+ is_const: false,+ },+ },+ integral("jpegSize"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(analysis.extents.get(&0), None);+ assert_ne!(analysis.roles[0], ParamRole::Output);+ }++ // A signature may now carry more than one Length, but only the fuzzable one decides+ // how the entry point is graded.+ #[test]+ fn a_second_extent_on_the_output_does_not_regrade_the_entry_point() {+ let params = [+ pointer("src", Pointee::Char, true),+ pointer("dst", Pointee::Char, false),+ integral("compressedSize"),+ integral("dstCapacity"),+ ];+ let analysis = analyze(¶ms);+ assert_eq!(analysis.fuzzability, Fuzzability::CastBuffer);+ assert_eq!(analysis.roles[0], ParamRole::Buffer);+ assert_eq!(analysis.roles[1], ParamRole::Output);+ }}