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): B84 - a slot is not an allocation, and the climb finally has a real tree

CarterPerez-dev9a7459c071db
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 d900693f..ca4d99c1 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
@@ -358,7 +358,9 @@ fn render_value(value: &Value) -> String {
Value::Size => FUZZ_SIZE.to_owned(),
Value::Null => "NULL".to_owned(),
Value::Zero => "0".to_owned(),
- Value::Literal(text) | Value::Local(text) | Value::Callback(text) => text.clone(),
+ Value::Literal(text) | Value::Local(text) | Value::Callback(text) | Value::Stated(text) => {
+ text.clone()
+ }
Value::AddressOf(name) => format!("&{name}"),
Value::AddressOfField { local, field } => format!("&{local}.{field}"),
Value::Cursor => format!("&{CURSOR_LOCAL}"),
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.rs
index 694bee1f..53a684c5 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
@@ -56,6 +56,11 @@ pub enum Value {
// the library has already filled in. `PNG_IMAGE_SIZE(image)` is libpng saying, in its own
// header, exactly how many bytes `png_image_finish_read` is going to write.
SizedBuffer { local: String, cast: Option<String> },
+ // A NUMBER the header states, which is a different claim from `Zero`. `Zero` is a Config
+ // pinned because nothing could say what it should be - the invented constant an upward step
+ // may not copy. This one is `PNG_IMAGE_ROW_STRIDE(image)`: libpng computing the stride from
+ // the record it just filled, which is the same knowledge `SizedBuffer` gets its length from.
+ Stated(String),
}
impl Value {
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 590bb500..c9d74647 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
@@ -309,15 +309,22 @@ pub fn macro_for_field(record: &str, field: &str, vocabulary: &Vocabulary) -> Op
// is the size of the record's whole payload. Censused over ten tree-runs, it fires on exactly
// one parameter in the entire validation set, which is where that claim is worth what it costs.
fn sizing_macro(record: &str, param: Option<&String>, vocabulary: &Vocabulary) -> Option<String> {
- let unary: Vec<&str> = vocabulary
+ param
+ .and_then(|name| {
+ macro_by_affinity(record, &format!("{name}_size"), unary_sizers(vocabulary))
+ })
+ .or_else(|| macro_by_affinity(record, "size", unary_sizers(vocabulary)))
+}
+
+// A macro taking a second argument is one the caller has to supply a number to, and inventing
+// that number is the guess B67 exists to refuse. One filter, read by both the buffer rule and
+// the Config rule, so the two cannot drift apart about what a usable macro is.
+fn unary_sizers(vocabulary: &Vocabulary) -> impl Iterator<Item = &str> + Clone {
+ vocabulary
.sizers
.iter()
.filter(|(_, arity)| **arity == 1)
.map(|(name, _)| name.as_str())
- .collect();
- param
- .and_then(|name| macro_by_affinity(record, &format!("{name}_size"), unary.iter().copied()))
- .or_else(|| macro_by_affinity(record, "size", unary.iter().copied()))
}
fn macro_by_affinity<'a>(
@@ -606,21 +613,70 @@ fn filled_declarations(filled: &BTreeMap<usize, (String, String)>) -> Vec<Stmt>
// fields the library has not written yet. Before `png_image_begin_read_from_memory` the
// record is zeroed and `PNG_IMAGE_SIZE` is 0, so the entry point would allocate nothing and
// hand the library a pointer to it, which is the manufactured overflow this is avoiding.
-struct Sizing<'a> {
+//
+// B84 gave the same struct a second job. A step binds against the PARENT's local map and has no
+// context resolution of its own, so a call needing a record the entry point never took refuses
+// however declarable that record is - which is what left `png_image_finish_read` stopped at
+// `background` with every other parameter bound.
+struct Scope<'a> {
vocabulary: &'a Vocabulary,
- record: String,
- local: String,
+ // `None` when the parent's contexts are all produced handles. A macro is written about the
+ // record and needs the object, so there is nothing to apply it to.
+ sizable: Option<(String, String)>,
// Names are unique across the whole climb because every buffer is a distinct C declaration
// in one block, but `buffers` holds only the ones THIS step allocated - the earlier steps
// freed theirs beside their own call, so listing them in a later bail is a double free.
first: usize,
- allocations: Vec<Stmt>,
+ contexts: usize,
+ statements: Vec<Stmt>,
buffers: Vec<String>,
+ declared: BTreeMap<String, String>,
}
-impl Sizing<'_> {
+impl Scope<'_> {
+ // The entry point's own ownership rule, applied one level out: a record the header defines
+ // completely and the library never hands back is the caller's to declare. The third
+ // condition is this side's alone - a step has nowhere to schedule a teardown, so a record
+ // with a destructor is refused rather than declared and left unfreed, which would be a
+ // LeakSanitizer report inside the target on every replay.
+ fn declare(&mut self, handle: &str) -> Option<Value> {
+ if let Some(local) = self.declared.get(handle) {
+ return Some(Value::AddressOf(local.clone()));
+ }
+ let record = self.vocabulary.records.get(handle)?;
+ if self.vocabulary.produced.contains(handle)
+ || self.vocabulary.destructors.contains_key(handle)
+ {
+ return None;
+ }
+ let local = format!("{CONTEXT_LOCAL}{}", self.contexts);
+ self.contexts += 1;
+ self.statements.push(Stmt::Declare {
+ ty: record.spelling.clone(),
+ name: local.clone(),
+ init: Init::ZeroBytes,
+ });
+ self.declared.insert(handle.to_owned(), local.clone());
+ Some(Value::AddressOf(local))
+ }
+
+ // The same fact as `offer`, for a NUMBER rather than a buffer. libpng defines
+ // `PNG_IMAGE_SIZE(image)` as `PNG_IMAGE_BUFFER_SIZE(image, PNG_IMAGE_ROW_STRIDE(image))`, so
+ // taking the stride from the header rather than pinning it to zero makes the buffer and the
+ // stride agree BY CONSTRUCTION instead of by trusting the library to substitute a default.
+ //
+ // Asked before the extent test, because a number the header states beats a number the tool
+ // refuses to guess by the same argument B82 makes. The corpus has no extent-named Config
+ // with a stating macro, so that ordering is asserted by a unit test rather than a tree.
+ fn stated(&mut self, field: &str) -> Option<Value> {
+ let (record, held) = self.sizable.clone()?;
+ let named = macro_by_affinity(&record, field, unary_sizers(self.vocabulary))?;
+ Some(Value::Stated(format!("{named}({held})")))
+ }
+
fn offer(&mut self, param: &Param) -> Option<Value> {
- let stated = sizing_macro(&self.record, param.name.as_ref(), self.vocabulary)?;
+ let (record, held) = self.sizable.clone()?;
+ let stated = sizing_macro(&record, param.name.as_ref(), self.vocabulary)?;
let cast = cast_for(param).ok()?;
let position = self.first + self.buffers.len();
let local = if position == 0 {
@@ -628,9 +684,9 @@ impl Sizing<'_> {
} else {
format!("{SIZED_LOCAL}{position}")
};
- self.allocations.push(Stmt::AllocSized {
+ self.statements.push(Stmt::AllocSized {
length: format!("{local}{SIZED_LEN_SUFFIX}"),
- size: format!("{stated}({})", self.local),
+ size: format!("{stated}({held})"),
frees: self.buffers.clone(),
local: local.clone(),
});
@@ -643,9 +699,9 @@ fn sized_by_macro(
index: usize,
param: &Param,
analysis: &Analysis,
- sizing: Option<&mut Sizing>,
+ scope: Option<&mut Scope>,
) -> Option<Value> {
- let sizing = sizing?;
+ let sizing = scope?;
// No const test, deliberately: `provisional_role` grades every const byte pointer as a
// Buffer, so one cannot reach either arm this is called from and a check for it would be an
// inert rule with no control able to redden it.
@@ -665,7 +721,7 @@ fn bind(
locals: &BTreeMap<String, Held>,
filled: &BTreeMap<usize, (String, String)>,
analysis: &Analysis,
- sizing: Option<&mut Sizing>,
+ scope: Option<&mut Scope>,
) -> Result<Value, Refusal> {
let label = param.name.clone().unwrap_or_else(|| param.spelling.clone());
match role {
@@ -738,6 +794,9 @@ fn bind(
Ok(Value::Size)
}
ParamRole::Config => {
+ if let Some(value) = scope.and_then(|scope| scope.stated(&label)) {
+ return Ok(value);
+ }
if is_extent_name(&label) {
if bounds_the_output(&label, analysis) {
return Ok(Value::OutCapacity);
@@ -761,40 +820,51 @@ fn bind(
Ok(Value::OutBuffer {
cast: cast_for(param)?,
})
- } else if let Some(value) = sized_by_macro(index, param, analysis, sizing) {
+ } else if let Some(value) = sized_by_macro(index, param, analysis, scope) {
Ok(value)
} else {
Err(Refusal::UnsizedOutParam { param: label })
}
}
ParamRole::Output | ParamRole::Context => {
- if is_double_pointer(param) {
- return filled
- .get(&index)
- .map(|(_, local)| Value::AddressOf(local.clone()))
- .ok_or(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 library, not the harness.
- let handle = handle_key(&param.spelling, &param.shape);
- if let Some(handle) = &handle
- && let Some(held) = locals.get(handle)
- {
+ bind_context(index, param, label, locals, filled, analysis, scope)
+ }
+ }
+}
+
+fn bind_context(
+ index: usize,
+ param: &Param,
+ label: String,
+ locals: &BTreeMap<String, Held>,
+ filled: &BTreeMap<usize, (String, String)>,
+ analysis: &Analysis,
+ scope: Option<&mut Scope>,
+) -> Result<Value, Refusal> {
+ if is_double_pointer(param) {
+ return filled
+ .get(&index)
+ .map(|(_, local)| Value::AddressOf(local.clone()))
+ .ok_or(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
+ // library, not the harness.
+ match handle_key(&param.spelling, &param.shape) {
+ Some(handle) => {
+ if let Some(held) = locals.get(&handle) {
return Ok(held.reference());
}
- // Asked only AFTER the handle map has had its answer, so a byte pointer the plan
- // is already holding under an opaque typedef keeps the local it was bound to.
- // `void *buffer` is what falls through to here: it names no handle at all, which
- // is why the B67 arm above never sees the parameter B82 was written for.
- if let Some(value) = sized_by_macro(index, param, analysis, sizing) {
- return Ok(value);
- }
- match handle {
- Some(record) => Err(Refusal::OpaqueContext { record }),
- None => Err(Refusal::UnsupportedShape { param: label }),
- }
+ // B84. Asked only after the parent's map has had its answer, so a handle the plan
+ // already holds keeps the local it was bound to.
+ scope
+ .and_then(|scope| scope.declare(&handle))
+ .ok_or(Refusal::OpaqueContext { record: handle })
}
+ // A byte pointer names no handle at all, and `void *buffer` is what falls through to
+ // here - which is why the B67 arm above never sees the parameter B82 was written for.
+ None => sized_by_macro(index, param, analysis, scope)
+ .ok_or(Refusal::UnsupportedShape { param: label }),
}
}
@@ -1114,6 +1184,9 @@ pub fn upward_steps(
let mut steps: Vec<Extension> = Vec::new();
let mut declared = filled_locals(&candidate.params, &candidate.analysis.roles, 0).len();
let mut allocated = 0usize;
+ // `resolve_contexts` names locals `ctx`, `ctx1`, `ctx2` off its own count, so a step's own
+ // declaration has to start past every one of them AND past every earlier step's.
+ let mut opened = 0usize;
for name in tail_run(sequence, &candidate.name) {
if steps.len() == MAX_UPWARD_STEPS {
break;
@@ -1158,29 +1231,34 @@ pub fn upward_steps(
// The record a macro-stated size can be read off is the FIRST context this plan
// declared for itself. A produced handle is excluded by construction: the macro spells
// the record by value and the harness only has the object for one it declared.
- let mut sizing = ordered
- .iter()
- .find(|(_, held)| !held.by_value)
- .map(|(handle, held)| Sizing {
- vocabulary,
- record: handle.clone(),
- local: held.local.clone(),
- first: allocated,
- allocations: Vec::new(),
- buffers: Vec::new(),
- });
- let Some(args) = bind_step(params, &analysis, &locals, &filled, sizing.as_mut()) else {
+ let mut scope = Scope {
+ vocabulary,
+ sizable: ordered
+ .iter()
+ .find(|(_, held)| !held.by_value)
+ .map(|(handle, held)| (handle.clone(), held.local.clone())),
+ first: allocated,
+ contexts: locals.len() + opened,
+ statements: Vec::new(),
+ buffers: Vec::new(),
+ declared: BTreeMap::new(),
+ };
+ let Some(args) = bind_step(params, &analysis, &locals, &filled, Some(&mut scope)) else {
break;
};
if !args.iter().all(is_replayable_value) {
break;
}
- let (allocations, buffers) = sizing
- .map(|sizing| (sizing.allocations, sizing.buffers))
- .unwrap_or_default();
+ let Scope {
+ statements,
+ buffers,
+ declared: opened_here,
+ ..
+ } = scope;
+ opened += opened_here.len();
declared += filled.len();
let mut preamble = filled_declarations(&filled);
- preamble.extend(allocations);
+ preamble.extend(statements);
// Freed on the step that allocated it rather than at the end of the harness, because a
// step is dropped whole when a later one is rejected and the free has to go with it.
let releases: Vec<Stmt> = buffers
@@ -1205,16 +1283,16 @@ pub fn upward_steps(
steps
}
-// Split out of `upward_steps` because a step's binding needs a `&mut Sizing` threaded through
+// Split out of `upward_steps` because a step's binding needs a `&mut Scope` threaded through
// every parameter in turn, and a `map` closure cannot hold that borrow across iterations.
fn bind_step(
params: &[Param],
analysis: &Analysis,
locals: &BTreeMap<String, Held>,
filled: &BTreeMap<usize, (String, String)>,
- sizing: Option<&mut Sizing>,
+ scope: Option<&mut Scope>,
) -> Option<Vec<Value>> {
- let mut sizing = sizing;
+ let mut sizing = scope;
let mut args = Vec::with_capacity(params.len());
for (index, (param, role)) in params.iter().zip(&analysis.roles).enumerate() {
let bound = bind(
@@ -1255,6 +1333,7 @@ fn is_replayable_value(value: &Value) -> bool {
| Value::AuxBuffer { .. }
| Value::AuxCapacity
| Value::SizedBuffer { .. }
+ | Value::Stated(_)
| Value::Local(_)
| Value::AddressOf(_)
| Value::AddressOfField { .. } => true,
@@ -3195,6 +3274,176 @@ mod tests {
assert!(source.contains(" free(lisdex_sized);"), "{source}");
}
+ // B84. `png_const_colorp background` - a record png.h defines completely, that the library
+ // also hands back through `png_get_PLTE`'s slot, on a call the entry point never took.
+ fn colour_record() -> Record {
+ Record {
+ spelling: "struct png_color_struct".to_owned(),
+ fields: vec![Field {
+ name: "red".to_owned(),
+ spelling: "png_byte".to_owned(),
+ shape: Shape::Integral,
+ }],
+ }
+ }
+
+ fn background() -> Param {
+ param(
+ "background",
+ "png_const_colorp",
+ Shape::Pointer {
+ pointee: Pointee::Record("png_color_struct".to_owned()),
+ is_const: true,
+ },
+ )
+ }
+
+ fn context_vocabulary() -> Vocabulary {
+ let mut vocabulary = sizing_vocabulary(&[("PNG_IMAGE_SIZE", 1)]);
+ vocabulary
+ .records
+ .insert("png_color_struct".to_owned(), colour_record());
+ with_tail(
+ &mut vocabulary,
+ vec![image_handle(), background(), void_out("buffer")],
+ );
+ vocabulary
+ }
+
+ #[test]
+ fn a_step_declares_the_context_the_entry_point_never_took() {
+ let vocabulary = context_vocabulary();
+ let steps = upward_steps(&begin_read(), &vocabulary, &simplified_read());
+ assert_eq!(steps.len(), 1, "{steps:?}");
+ assert_eq!(
+ steps[0].declares[0],
+ Stmt::Declare {
+ ty: "struct png_color_struct".to_owned(),
+ name: "ctx1".to_owned(),
+ init: Init::ZeroBytes,
+ }
+ );
+ let Stmt::Call { args, .. } = &steps[0].call else {
+ panic!("the step is a call");
+ };
+ assert_eq!(args[1], Value::AddressOf("ctx1".to_owned()));
+ }
+
+ // The isolating control for the RETURNED rule. `gzFile_s` and `WebPMux` are this shape: the
+ // header shows a struct and the library hands back one it allocated, whose real layout the
+ // header only prefixes. Stack-declaring one is a fabricated crash inside the target.
+ #[test]
+ fn a_record_the_library_hands_back_from_a_constructor_is_not_declared_by_a_step() {
+ let mut vocabulary = context_vocabulary();
+ vocabulary.produced.insert("png_color_struct".to_owned());
+ let steps = upward_steps(&begin_read(), &vocabulary, &simplified_read());
+ assert!(steps.is_empty(), "{steps:?}");
+ }
+
+ // The isolating control for the DESTRUCTOR rule, and it is this side's alone: the entry
+ // point schedules teardowns for the contexts it declares, and a step has nowhere to put one.
+ #[test]
+ fn a_record_something_must_free_is_not_declared_where_no_teardown_can_be_scheduled() {
+ let mut vocabulary = context_vocabulary();
+ vocabulary.destructors.insert(
+ "png_color_struct".to_owned(),
+ BTreeSet::from(["png_color_free".to_owned()]),
+ );
+ let steps = upward_steps(&begin_read(), &vocabulary, &simplified_read());
+ assert!(steps.is_empty(), "{steps:?}");
+ }
+
+ // Two parameters of one record share one declaration. Declaring twice in a block either
+ // shadows the slot the first argument refers to or does not compile at all.
+ #[test]
+ fn two_parameters_of_the_same_record_share_the_local_the_step_declared() {
+ let mut vocabulary = context_vocabulary();
+ with_tail(
+ &mut vocabulary,
+ vec![
+ image_handle(),
+ background(),
+ param(
+ "foreground",
+ "png_const_colorp",
+ Shape::Pointer {
+ pointee: Pointee::Record("png_color_struct".to_owned()),
+ is_const: true,
+ },
+ ),
+ ],
+ );
+ let steps = upward_steps(&begin_read(), &vocabulary, &simplified_read());
+ assert_eq!(steps.len(), 1, "{steps:?}");
+ assert_eq!(steps[0].declares.len(), 1, "{:?}", steps[0].declares);
+ let Stmt::Call { args, .. } = &steps[0].call else {
+ panic!("the step is a call");
+ };
+ assert_eq!(args[1], args[2]);
+ }
+
+ // The stated number. A Config pinned to zero is the invented constant an upward step may
+ // not copy - `tj3Set(ctx, 0, 0)` where the author wrote `TJPARAM_SCANLIMIT, 500`. A macro
+ // naming the record and the parameter is the header stating it instead.
+ #[test]
+ fn a_config_a_macro_states_is_read_from_the_header_rather_than_pinned() {
+ let mut vocabulary = sizing_vocabulary(&[("PNG_IMAGE_ROW_STRIDE", 1)]);
+ with_tail(
+ &mut vocabulary,
+ vec![
+ image_handle(),
+ param("row_stride", "png_int_32", Shape::Integral),
+ ],
+ );
+ let steps = upward_steps(&begin_read(), &vocabulary, &simplified_read());
+ assert_eq!(steps.len(), 1, "{steps:?}");
+ assert_eq!(
+ steps[0].call,
+ Stmt::Call {
+ function: "png_image_finish_read".to_owned(),
+ args: vec![
+ Value::AddressOf(CONTEXT_LOCAL.to_owned()),
+ Value::Stated("PNG_IMAGE_ROW_STRIDE(ctx)".to_owned()),
+ ],
+ capture: None,
+ }
+ );
+ }
+
+ // The isolating control for it: the same call with nothing naming the number stays a pinned
+ // zero, which `is_replayable_value` refuses, so the climb stops exactly where D32 left it.
+ #[test]
+ fn a_config_no_macro_states_is_still_a_pinned_zero_the_climb_will_not_copy() {
+ let mut vocabulary = sizing_vocabulary(&[]);
+ with_tail(
+ &mut vocabulary,
+ vec![
+ image_handle(),
+ param("row_stride", "png_int_32", Shape::Integral),
+ ],
+ );
+ let steps = upward_steps(&begin_read(), &vocabulary, &simplified_read());
+ assert!(steps.is_empty(), "{steps:?}");
+ }
+
+ // The ordering claim the corpus cannot make: a Config that is ALSO an extent name reaches
+ // the stated-number offer BEFORE the refusal, because a number the header computes beats a
+ // number the tool declines to guess. No tree in the validation set has both.
+ #[test]
+ fn a_macro_answers_for_an_extent_named_config_the_tool_would_otherwise_refuse() {
+ let mut vocabulary = sizing_vocabulary(&[("PNG_IMAGE_SIZE", 1)]);
+ with_tail(
+ &mut vocabulary,
+ vec![image_handle(), param("size", "size_t", Shape::Integral)],
+ );
+ let steps = upward_steps(&begin_read(), &vocabulary, &simplified_read());
+ assert_eq!(steps.len(), 1, "{steps:?}");
+ let Stmt::Call { args, .. } = &steps[0].call else {
+ panic!("the step is a call");
+ };
+ assert_eq!(args[1], Value::Stated("PNG_IMAGE_SIZE(ctx)".to_owned()));
+ }
+
#[test]
fn a_captured_result_is_discarded_so_wall_werror_targets_still_build() {
let plans = plan_candidate(&begin_read(), "png.h", &png_image_vocabulary()).unwrap();
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 eb61724c..7646700f 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,6 @@ use std::sync::{Mutex, PoisonError};
use clang::diagnostic::{Diagnostic, Severity};
use clang::{Clang, Entity, EntityKind, Index, Type, TypeKind};
-use crate::signature;
use crate::signature::{Param, Pointee, Returns, Shape, handle_key};
static PARSE_LOCK: Mutex<()> = Mutex::new(());
@@ -469,17 +468,14 @@ fn collect_relations(entity: &Entity, target: &Path, into: &mut Vocabulary) {
.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
- // stack context. Freeing that stack address then reports "attempting free on address
- // which was not malloc()-ed" with the frames inside the target library.
- for argument in entity.get_arguments().unwrap_or_default() {
- if let Some(ty) = argument.get_type()
- && let Some(record) = signature::record_behind_two_pointers(&shape_of(&ty))
- {
- into.produced.insert(record.to_owned());
- }
- }
+ // A double-pointer out-param used to count as an allocation too, on the argument that
+ // `int X_create(X **out)` hands the record back through a slot. B84 measured that arm over
+ // ten tree-runs and DELETED it: a slot says the library HAS an instance, never that the
+ // caller may not declare one. Every real handle it was written for is opaque - `LZ4F_dctx_s`
+ // and `LZ4F_cctx_s` from `LZ4F_create*Context(X **)`, `LZ4_readFile_s` from `LZ4F_readOpen` -
+ // so the completeness test blocks all four without it, while it blocked seven libpng records
+ // that are plain value types a caller declares: `png_color`, `png_time`, `png_text` and four
+ // more, every one slotted by a `png_get_*` getter handing back a view into its own state.
if is_teardown_name(&name)
&& let Some(handle) = sole_handle_parameter(entity)
{
@@ -1022,6 +1018,36 @@ void WebPFreeDecBuffer(WebPDecBuffer *buffer);
assert!(vocab.produced.contains("buf"));
}
+ // B84. The pair that decides whether a slot is an allocation. `LZ4F_dctx_s`'s shape - opaque,
+ // handed back through `int X_create(X **)`, freed by a destructor - must stay unavailable to
+ // a caller, and it is the COMPLETENESS test that keeps it so: the header names the struct and
+ // never defines it, so there is nothing to declare. Nothing needs `produced` to say it.
+ #[test]
+ fn a_record_handed_back_through_a_slot_it_never_defines_has_nothing_to_declare() {
+ let vocab = vocabulary(
+ "slotted_opaque",
+ "struct dctx;\nint dctx_create(struct dctx **out);\nvoid dctx_free(struct dctx *d);\n",
+ );
+ assert!(!vocab.records.contains_key("dctx"));
+ }
+
+ // The other half, and the seven libpng records the deleted arm was costing. A getter handing
+ // back a pointer INTO its own state says the library has an instance; it says nothing about
+ // whether the caller may declare one, and `png_color` is three bytes the caller owns.
+ #[test]
+ fn a_record_a_getter_hands_back_is_still_the_callers_to_declare() {
+ let vocab = vocabulary(
+ "slotted_complete",
+ "struct png_color_struct { unsigned char red; };\nstruct png_info;\n\
+ unsigned png_get_PLTE(struct png_info *i, struct png_color_struct **palette);\n",
+ );
+ assert!(vocab.records.contains_key("png_color_struct"));
+ assert!(
+ !vocab.produced.contains("png_color_struct"),
+ "a slot is not an allocation; only a returned non-const pointer is"
+ );
+ }
+
#[test]
fn a_multi_parameter_teardown_is_not_paired() {
let vocab = vocabulary(
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh
index 89e0a5de..8439d054 100755
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh
@@ -731,6 +731,125 @@ else
fi
rm -rf "$SIZEROOT"
+# B84. A step binds against the PARENT's local map and had no context resolution of its own, so
+# a call needing a record the entry point never took refused however declarable that record was.
+# That is `png_image_finish_read`'s `background`, and it is what left libpng's climb stopped with
+# every other parameter bound.
+#
+# Four trees, each differing from the first by exactly ONE declaration, because three rules are
+# new: a record the library RETURNS is a handle and stays refused; a record with a DESTRUCTOR is
+# refused because a step has nowhere to schedule the teardown; and a Config pinned to zero is
+# still the invented constant an upward step may not copy unless a macro states it.
+banner "M5 A STEP'S OWN CONTEXT"
+
+ctxlib_tree() {
+ local root="$1"
+ local extra="$2"
+ local stride="$3"
+ rm -rf "$root"
+ mkdir -p "$root/include" "$root/fuzz"
+ cat >"$root/include/ctxlib.h" <<CTX_H
+#include <stddef.h>
+
+typedef struct ctxlib_image {
+ void *opaque;
+ unsigned int version;
+ unsigned int width;
+} ctxlib_image;
+
+typedef struct ctxlib_color {
+ unsigned char red;
+} ctxlib_color;
+
+#define CTXLIB_IMAGE_VERSION 1
+#define CTXLIB_IMAGE_SIZE(image) ((image).width)
+$stride
+$extra
+
+int ctxlib_begin_read(ctxlib_image *image, const void *memory, size_t size);
+int ctxlib_finish_read(ctxlib_image *image, const ctxlib_color *background, void *buffer,
+ int row_stride);
+CTX_H
+ cat >"$root/fuzz/ctxlib_fuzzer.c" <<'CTX_FUZZ'
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "ctxlib.h"
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+ ctxlib_image image;
+ ctxlib_color background;
+ void *buffer;
+
+ memset(&image, 0, sizeof image);
+ memset(&background, 0, sizeof background);
+ image.version = CTXLIB_IMAGE_VERSION;
+ ctxlib_begin_read(&image, data, size);
+ buffer = malloc(CTXLIB_IMAGE_SIZE(image));
+ ctxlib_finish_read(&image, &background, buffer, 0);
+ free(buffer);
+ return 0;
+}
+CTX_FUZZ
+}
+
+CTXROOT="$WORK/stepctx"
+STRIDE='#define CTXLIB_IMAGE_ROW_STRIDE(image) ((image).width)'
+SLOT='unsigned ctxlib_get_palette(ctxlib_image *i, ctxlib_color **palette);'
+rm -rf "$CTXROOT"
+ctxlib_tree "$CTXROOT/slotted" "$SLOT" "$STRIDE"
+CTXPLAN="$OUT/stepctx-plan.txt"
+"$LISDEX" harness "$CTXROOT/slotted" --out "$OUT/stepctx-out" --plan-only --top 10 \
+ >"$CTXPLAN" 2>&1
+if grep -q 'extending \["ctxlib_finish_read"\]' "$CTXPLAN"; then
+ ok "a step declares the context the entry point never took, and the climb copies the call"
+else
+ bad "the step's own context did not reach the climb: $(cat "$CTXPLAN")"
+fi
+# The isolating control for the RETURNED rule, and the one that keeps every real handle out.
+# Same tree, same signature - one declaration added, and now the library hands the record back.
+# This is `gzFile_s` from `gzopen` and `WebPMux` from `WebPMuxNew`, whose header struct is a
+# prefix of what the library actually allocates.
+ctxlib_tree "$CTXROOT/returned" \
+ "$SLOT
+ctxlib_color *ctxlib_color_new(void);" "$STRIDE"
+"$LISDEX" harness "$CTXROOT/returned" --out "$OUT/stepctx-ret-out" --plan-only --top 10 \
+ >"$OUT/stepctx-ret.txt" 2>&1
+if grep 'extending' "$OUT/stepctx-ret.txt" | grep -q 'ctxlib_finish_read'; then
+ bad "a record the library returns was declared on the stack anyway: \
+$(grep extending "$OUT/stepctx-ret.txt")"
+else
+ ok "and a record the library hands back from a constructor is still refused"
+fi
+# The isolating control for the DESTRUCTOR rule. A step has nowhere to schedule a teardown, so
+# declaring a record something must free would be a LeakSanitizer report inside the target on
+# every replay - D15's forged crash arriving through the mechanism meant to add depth.
+ctxlib_tree "$CTXROOT/owned" \
+ "$SLOT
+void ctxlib_color_free(ctxlib_color *c);" "$STRIDE"
+"$LISDEX" harness "$CTXROOT/owned" --out "$OUT/stepctx-own-out" --plan-only --top 10 \
+ >"$OUT/stepctx-own.txt" 2>&1
+if grep 'extending' "$OUT/stepctx-own.txt" | grep -q 'ctxlib_finish_read'; then
+ bad "a record with a destructor was declared where nothing can free it: \
+$(grep extending "$OUT/stepctx-own.txt")"
+else
+ ok "and a record something must free is refused, because a step cannot schedule a teardown"
+fi
+# The isolating control for the STATED NUMBER. Same tree, the stride macro removed: `row_stride`
+# falls back to a pinned zero, which is the invented constant D32 measured four bad steps from
+# on libjpeg-turbo - `tj3Set(ctx, 0, 0)` where the author wrote `TJPARAM_SCANLIMIT, 500`.
+ctxlib_tree "$CTXROOT/unstated" "$SLOT" '/* no macro states the stride */'
+"$LISDEX" harness "$CTXROOT/unstated" --out "$OUT/stepctx-zero-out" --plan-only --top 10 \
+ >"$OUT/stepctx-zero.txt" 2>&1
+if grep 'extending' "$OUT/stepctx-zero.txt" | grep -q 'ctxlib_finish_read'; then
+ bad "a Config nothing states was pinned to zero and copied into the climb: \
+$(grep extending "$OUT/stepctx-zero.txt")"
+else
+ ok "and a Config no macro states is still a pinned zero the climb refuses to copy"
+fi
+rm -rf "$CTXROOT"
+
# The negative control the ladder must REFUSE to help: a tree with nothing to mine has no
# authored floor, so the ranking alone decides and no setup is invented.
BARE="$WORK/ladder-bare"
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m4-gate.sh b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m4-gate.sh
index dbe14155..a058b245 100755
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m4-gate.sh
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m4-gate.sh
@@ -582,13 +582,25 @@ typedef struct mylib_image {
unsigned int acc;
} mylib_image;
+/* B84. A record the header defines completely that the library also hands back through a
+ GETTER's slot. Before B84 the slot alone marked it produced, so the harness could not declare
+ one and every call taking it refused - which is what `png_const_colorp background` is. */
+typedef struct mylib_color {
+ unsigned char red;
+ unsigned char green;
+ unsigned char blue;
+} mylib_color;
+
#define MYLIB_IMAGE_VERSION 1
#define MYLIB_IMAGE_SIZE(image) ((size_t)(image).width * (size_t)(image).height)
+#define MYLIB_IMAGE_ROW_STRIDE(image) ((size_t)(image).width)
#define MYLIB_IMAGE_BUFFER_SIZE(image, row_stride) \
((size_t)(image).height * (size_t)(row_stride))
int mylib_begin_read(mylib_image *image, const void *buff, size_t size);
-int mylib_finish_read(mylib_image *image, void *buffer);
+unsigned mylib_get_palette(mylib_image *image, mylib_color **palette);
+int mylib_finish_read(mylib_image *image, const mylib_color *background, void *buffer,
+ int row_stride);
IMAGE_H
cat >"$LAB/lib/mylib_image.c" <<'IMAGE_C'
#include "mylib_image.h"
@@ -630,15 +642,31 @@ int mylib_begin_read(mylib_image *image, const void *buff, size_t size)
compile check: a harness that allocated anything less than MYLIB_IMAGE_SIZE bytes is a
heap-buffer-overflow with the library's frames on top, which is the D12 crash B82 is built to
avoid, and ASAN reports it here instead of in a real target. */
-int mylib_finish_read(mylib_image *image, void *buffer)
+/* The palette belongs to the library and the caller only borrows a pointer to it, which is
+ exactly `png_get_PLTE`. It is here so `mylib_color` is a record the library HANDS BACK. */
+static mylib_color mylib_palette[4];
+
+unsigned mylib_get_palette(mylib_image *image, mylib_color **palette)
+{
+ if (image == NULL || palette == NULL) { return 0; }
+ *palette = mylib_palette;
+ return 4u;
+}
+
+int mylib_finish_read(mylib_image *image, const mylib_color *background, void *buffer,
+ int row_stride)
{
unsigned char *out = (unsigned char *)buffer;
size_t total;
size_t at;
if (image == NULL || out == NULL) { return 0; }
- total = MYLIB_IMAGE_SIZE(*image);
- if (total == 0) { return 0; }
+ /* Read, so a harness passing a pointer to memory it never initialised is an ASAN report
+ here rather than a quiet pass. NULL stays legal, exactly as libpng documents it. */
+ if (background != NULL) { image->acc += background->red; }
+ if (row_stride == 0) { row_stride = (int)MYLIB_IMAGE_ROW_STRIDE(*image); }
+ total = (size_t)image->height * (size_t)row_stride;
+ if (total == 0 || total > MYLIB_IMAGE_SIZE(*image)) { return 0; }
for (at = 0; at < total; at++) {
out[at] = (unsigned char)(at ^ (size_t)image->width);
}
@@ -667,14 +695,16 @@ cat >"$LAB/sized/fuzz/mylib_image_fuzzer.c" <<'IMAGE_FUZZ'
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
mylib_image image;
+ mylib_color background;
void *buffer;
memset(&image, 0, sizeof image);
+ memset(&background, 0, sizeof background);
image.version = MYLIB_IMAGE_VERSION;
if (mylib_begin_read(&image, data, size)) {
buffer = malloc(MYLIB_IMAGE_SIZE(image));
if (buffer != NULL) {
- mylib_finish_read(&image, buffer);
+ mylib_finish_read(&image, &background, buffer, 0);
free(buffer);
}
}
@@ -836,6 +866,29 @@ if [ -n "$SIZED_C" ] && ! grep -q 'MYLIB_IMAGE_BUFFER_SIZE' "$SIZED_C"; then
else
fail "a macro needing a number the caller owns reached the emitted harness"
fi
+# B84, the step's own context. `mylib_color` is complete in the header AND handed back by
+# `mylib_get_palette`'s slot, which is `png_get_PLTE`'s shape - before B84 that slot alone marked
+# it produced and this parameter refused. The library READS the background, so a harness passing
+# a pointer to memory it never initialised is an ASAN report rather than a quiet pass.
+if [ -n "$SIZED_C" ] && grep -qE 'struct mylib_color ctx1;' "$SIZED_C" \
+ && grep -q 'memset(&ctx1, 0, sizeof ctx1);' "$SIZED_C" \
+ && grep -q 'mylib_finish_read(&ctx, &ctx1,' "$SIZED_C"; then
+ pass "a step declares the context the entry point never took, and zeroes it before use"
+else
+ fail "the step did not declare its own context: \
+$(grep -n 'ctx1\|mylib_finish_read' "${SIZED_C:-/dev/null}" | head -3)"
+fi
+# B84, the stated number. `row_stride` is a Config, and a Config pinned to zero is the invented
+# constant an upward step may not copy - which is what stopped this climb after the buffer and
+# the background both bound. `MYLIB_IMAGE_ROW_STRIDE(image)` is the header stating it, and it is
+# the same stride `MYLIB_IMAGE_SIZE` is defined in terms of, so the two agree by construction.
+if [ -n "$SIZED_C" ] && grep -q 'MYLIB_IMAGE_ROW_STRIDE(ctx));' "$SIZED_C" \
+ && ! grep -qE 'mylib_finish_read\(.*, 0\);' "$SIZED_C"; then
+ pass "and the stride comes from the header that states it, not from a pinned zero"
+else
+ fail "the step invented its Config rather than reading the macro: \
+$(grep -n 'mylib_finish_read' "${SIZED_C:-/dev/null}" | head -2)"
+fi
printf '\n'
if [ "$FAILURES" -eq 0 ]; then