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 πŸ‘‡

fix(lisdex): B72 - an auxiliary input is defined by its declared size, not its contents

CarterPerez-dev9caea0d5eed8
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 a10db46f..b22a5181 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
@@ -4,9 +4,9 @@
use std::fmt::Write;
use crate::plan::{
- CALLBACK_BUDGET, CURSOR_LOCAL, CURSOR_STRUCT, CallbackKind, CallbackStub, FUZZ_DATA, FUZZ_SIZE,
- HarnessPlan, Init, MAX_CALLBACK_CALLS, MAX_OUT_BYTES, NUL_COPY_LOCAL, OUT_CAPACITY, OUT_LOCAL,
- Stmt, Value,
+ AUX_CAPACITY, AUX_LOCAL, CALLBACK_BUDGET, CURSOR_LOCAL, CURSOR_STRUCT, CallbackKind,
+ CallbackStub, FUZZ_DATA, FUZZ_SIZE, HarnessPlan, Init, MAX_AUX_BYTES, MAX_CALLBACK_CALLS,
+ MAX_OUT_BYTES, NUL_COPY_LOCAL, OUT_CAPACITY, OUT_LOCAL, Stmt, Value,
};
pub const CALLBACK_COUNTER: &str = "lisdex_callback_calls";
@@ -44,8 +44,13 @@ fn render_includes(out: &mut String, plan: &HarnessPlan) {
}
fn render_support(out: &mut String, plan: &HarnessPlan) {
+ if plan.needs_aux_buffer {
+ let _ = writeln!(out, "#define {AUX_CAPACITY} {MAX_AUX_BYTES}");
+ }
if plan.needs_out_buffer {
let _ = writeln!(out, "#define {OUT_CAPACITY} {MAX_OUT_BYTES}");
+ }
+ if plan.needs_aux_buffer || plan.needs_out_buffer {
let _ = writeln!(out);
}
if !plan.needs_cursor && plan.callbacks.is_empty() {
@@ -162,6 +167,9 @@ fn render_entry(out: &mut String, plan: &HarnessPlan) {
if plan.needs_out_buffer {
let _ = writeln!(out, " free({OUT_LOCAL});");
}
+ if plan.needs_aux_buffer {
+ let _ = writeln!(out, " free({AUX_LOCAL});");
+ }
let _ = writeln!(out, " return 0;");
let _ = writeln!(out, "}}");
}
@@ -203,6 +211,26 @@ fn render_prologue(out: &mut String, plan: &HarnessPlan) {
let _ = writeln!(out, " memset({OUT_LOCAL}, 0, {OUT_CAPACITY});");
wrote = true;
}
+ // Zeroed on purpose, not filled from the corpus: lz4's own fuzzer memsets its dictionary
+ // and says in a comment that the contents do not matter. Spending corpus bytes here would
+ // take them away from the buffer that IS being parsed.
+ if plan.needs_aux_buffer {
+ let _ = writeln!(
+ out,
+ " unsigned char *{AUX_LOCAL} = (unsigned char *)malloc({AUX_CAPACITY});"
+ );
+ let _ = writeln!(out, " if ({AUX_LOCAL} == NULL) {{");
+ 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, " }}");
+ let _ = writeln!(out, " memset({AUX_LOCAL}, 0, {AUX_CAPACITY});");
+ wrote = true;
+ }
if plan.needs_cursor {
let _ = writeln!(out, " {CURSOR_LOCAL}.data = {FUZZ_DATA};");
let _ = writeln!(out, " {CURSOR_LOCAL}.size = {FUZZ_SIZE};");
@@ -284,6 +312,9 @@ fn render_bail(out: &mut String, condition: &str, plan: &HarnessPlan) {
if plan.needs_out_buffer {
let _ = writeln!(out, " free({OUT_LOCAL});");
}
+ if plan.needs_aux_buffer {
+ let _ = writeln!(out, " free({AUX_LOCAL});");
+ }
let _ = writeln!(out, " return 0;");
let _ = writeln!(out, " }}");
}
@@ -307,6 +338,11 @@ fn render_value(value: &Value) -> String {
None => OUT_LOCAL.to_owned(),
},
Value::OutCapacity => OUT_CAPACITY.to_owned(),
+ Value::AuxBuffer { cast } => match cast {
+ Some(ty) => format!("({ty}){AUX_LOCAL}"),
+ None => AUX_LOCAL.to_owned(),
+ },
+ Value::AuxCapacity => AUX_CAPACITY.to_owned(),
}
}
@@ -606,6 +642,69 @@ mod tests {
assert!(!source.contains("free("));
}
+ fn using_dict() -> HarnessPlan {
+ HarnessPlan::new("LZ4_decompress_safe_usingDict").with(Stmt::Call {
+ function: "LZ4_decompress_safe_usingDict".to_owned(),
+ args: vec![
+ Value::NulTerminated,
+ Value::OutBuffer { cast: None },
+ Value::Size,
+ Value::OutCapacity,
+ Value::AuxBuffer {
+ cast: Some("const char *".to_owned()),
+ },
+ Value::AuxCapacity,
+ ],
+ capture: None,
+ })
+ }
+
+ // Zeroed rather than filled from the corpus, which is what lz4's own fuzzer does to its
+ // dictionary, and sized at the authors' own `64 * 1024 - 1` boundary.
+ #[test]
+ fn the_auxiliary_buffer_is_allocated_zeroed_and_distinct_from_the_corpus() {
+ let source = render(&using_dict(), "x.c");
+ assert!(
+ source.contains("#define LISDEX_AUX_CAPACITY 65535"),
+ "{source}"
+ );
+ assert!(
+ source.contains(
+ "unsigned char *lisdex_aux = (unsigned char *)malloc(LISDEX_AUX_CAPACITY);"
+ ),
+ "{source}"
+ );
+ assert!(
+ source.contains("memset(lisdex_aux, 0, LISDEX_AUX_CAPACITY);"),
+ "{source}"
+ );
+ assert!(
+ source.contains("(const char *)lisdex_aux, LISDEX_AUX_CAPACITY)"),
+ "the dictionary must be its own memory, not a second copy of lisdex_text: {source}"
+ );
+ }
+
+ // LSAN runs on the direct oracle pass, so an auxiliary allocation that outlives any exit
+ // reports a leak attributed to the library - the forged crash D15 warns about.
+ #[test]
+ fn every_exit_frees_the_auxiliary_buffer() {
+ let plan = using_dict().with(Stmt::BailIfNull {
+ local: "ctx".to_owned(),
+ });
+ let source = render(&plan, "x.c");
+ assert_eq!(
+ source.matches("free(lisdex_aux);").count(),
+ 2,
+ "once on the bail path and once at the end: {source}"
+ );
+ assert!(
+ source.contains(
+ " if (lisdex_aux == NULL) {\n free(lisdex_text);\n free(lisdex_out);\n return 0;\n }"
+ ),
+ "its own failed allocation must free the two before it: {source}"
+ );
+ }
+
#[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/plan.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/plan.rs
index 49f9ae5d..b298cd4f 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
@@ -12,6 +12,12 @@ 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;
+pub const AUX_LOCAL: &str = "lisdex_aux";
+pub const AUX_CAPACITY: &str = "LISDEX_AUX_CAPACITY";
+// lz4's own decompress_fuzzer.c uses `64 * 1024 - 1` for its large external dictionary, so
+// this is that size rather than a round number: a dictionary at or above 64 KiB takes a
+// different path inside LZ4 than one below it, and the authors picked the boundary.
+pub const MAX_AUX_BYTES: usize = 64 * 1024 - 1;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Value {
@@ -33,6 +39,14 @@ pub enum Value {
// the size is knowledge the caller is expected to already hold.
OutBuffer { cast: Option<String> },
OutCapacity,
+ // A SECOND readable byte buffer is a second input, and the fuzzer supplies one. Handing
+ // it the same pointer as the first is not a memory-safety lie - both reads are in bounds
+ // - but it is not what the authors do, and lz4's own fuzzer says why in a comment:
+ // `/* Prepare the dictionary. The data doesn't matter for decompression. */` above a
+ // `memset(dict, 0, dictSize)`. What matters about an auxiliary buffer is its SIZE and
+ // that it is distinct memory, not its contents.
+ AuxBuffer { cast: Option<String> },
+ AuxCapacity,
}
impl Value {
@@ -104,13 +118,19 @@ pub enum CallbackKind {
ReadFromCursor { buffer: usize, count: usize },
}
+// Four independent scaffolding needs, each derived from the arguments the plan actually
+// binds and each read by name in the emitter. Not the boolean-blindness the lint is aimed at
+// - there is no call site that passes them positionally - and `SurfaceArgs` carries the same
+// allow for the same reason.
#[derive(Debug, Clone, PartialEq, Eq)]
+#[allow(clippy::struct_excessive_bools)]
pub struct HarnessPlan {
pub entry: String,
pub includes: Vec<String>,
pub needs_cursor: bool,
pub needs_nul_copy: bool,
pub needs_out_buffer: bool,
+ pub needs_aux_buffer: bool,
pub callbacks: Vec<CallbackStub>,
pub body: Vec<Stmt>,
// Named separately from `body` because "which calls came from the author's own sequence
@@ -128,6 +148,7 @@ impl HarnessPlan {
needs_cursor: false,
needs_nul_copy: false,
needs_out_buffer: false,
+ needs_aux_buffer: false,
callbacks: Vec::new(),
body: Vec::new(),
replayed: Vec::new(),
@@ -164,6 +185,12 @@ impl HarnessPlan {
{
self.needs_out_buffer = true;
}
+ if args
+ .iter()
+ .any(|value| matches!(value, Value::AuxBuffer { .. }))
+ {
+ self.needs_aux_buffer = true;
+ }
}
self.body.push(statement);
self
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 c1a0f4d5..78a6efa1 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
@@ -429,6 +429,23 @@ fn is_indirect(param: &Param) -> bool {
)
}
+// The corpus is ONE object, so exactly one readable buffer can be it. Position decides,
+// because C APIs put the primary input first and every real case in the set agrees:
+// `LZ4_decompress_safe_usingDict(src, dst, srcSize, dstCapacity, dictStart, dictSize)`,
+// `blake2xb(out, outlen, in, inlen, key, keylen)`, `LZ4F_decompress_usingDict(dctx, dst,
+// dstSize, srcBuffer, srcSize, dict, dictSize)`.
+//
+// The signature's own grade cannot be reused for this. `grade_signature` picks its buffer
+// with `max_by_key(buffer_quality)`, and lz4's `src` and `dictStart` are the same type, so
+// they score equal - and `max_by_key` returns the LAST maximum, which is `dictStart`. Keying
+// on that would have handed the dictionary the corpus and the input a zeroed buffer.
+fn primary_buffer(analysis: &Analysis) -> Option<usize> {
+ analysis
+ .roles
+ .iter()
+ .position(|role| *role == ParamRole::Buffer)
+}
+
fn bind(
index: usize,
param: &Param,
@@ -438,6 +455,20 @@ fn bind(
) -> Result<Value, Refusal> {
let label = param.name.clone().unwrap_or_else(|| param.spelling.clone());
match role {
+ // A readable buffer that is not the FIRST one is a second input the fuzzer cannot
+ // supply. It gets its own zeroed allocation and its own declared size, which is what
+ // lz4's authors do for the dictionary and what keeps the two pointers distinct.
+ ParamRole::Buffer if primary_buffer(analysis) != Some(index) => {
+ if analysis.extents.contains_key(&index) {
+ Ok(Value::AuxBuffer {
+ cast: cast_for(param)?,
+ })
+ } else {
+ // Without an extent there is no size to declare, so the library is told
+ // nothing about how far it may read - the same line B67 draws for outputs.
+ Err(Refusal::UnsizedTextParam { param: label })
+ }
+ }
ParamRole::Buffer => match &param.shape {
Shape::Pointer {
pointee: Pointee::Char | Pointee::SignedChar,
@@ -461,6 +492,18 @@ fn bind(
},
ParamRole::Length => {
let owner = owns_this_extent(analysis, index);
+ // An auxiliary buffer's extent describes memory the HARNESS allocated, exactly
+ // as an output's does, so it is a fact rather than an inference about the corpus.
+ if owner.is_some_and(|pointer| {
+ analysis.roles[pointer] == ParamRole::Buffer
+ && primary_buffer(analysis) != Some(pointer)
+ }) {
+ return if is_indirect(param) {
+ Err(Refusal::IndirectExtent { param: label })
+ } else {
+ Ok(Value::AuxCapacity)
+ };
+ }
let owned_by_output =
owner.is_some_and(|pointer| analysis.roles[pointer] == ParamRole::Output);
if owned_by_output {
@@ -2005,6 +2048,110 @@ mod tests {
);
}
+ // `LZ4_decompress_safe_usingDict(src, dst, srcSize, dstCapacity, dictStart, dictSize)`.
+ // The extents are written out rather than derived, because this is the buffers-first
+ // shape where no length is adjacent to the buffer it measures - which is the whole
+ // reason the ownership map exists.
+ fn lz4_using_dict() -> Candidate {
+ let readable = |name: &str| {
+ param(
+ name,
+ "const char *",
+ Shape::Pointer {
+ pointee: Pointee::Char,
+ is_const: true,
+ },
+ )
+ };
+ let mut entry = candidate(
+ "LZ4_decompress_safe_usingDict",
+ vec![
+ readable("src"),
+ param(
+ "dst",
+ "char *",
+ Shape::Pointer {
+ pointee: Pointee::Char,
+ is_const: false,
+ },
+ ),
+ param("srcSize", "int", Shape::Integral),
+ param("dstCapacity", "int", Shape::Integral),
+ readable("dictStart"),
+ param("dictSize", "int", Shape::Integral),
+ ],
+ vec![
+ ParamRole::Buffer,
+ ParamRole::Output,
+ ParamRole::Length,
+ ParamRole::Length,
+ ParamRole::Buffer,
+ ParamRole::Length,
+ ],
+ Fuzzability::DirectBuffer,
+ Returns::default(),
+ );
+ entry.analysis.extents = BTreeMap::from([(0, 2), (1, 3), (4, 5)]);
+ entry
+ }
+
+ // Handing the same pointer to both readable buffers is not a memory-safety lie - both
+ // reads are in bounds - but it is not what the authors do, and lz4's own fuzzer says
+ // why: `/* Prepare the dictionary. The data doesn't matter for decompression. */` over
+ // a `memset(dict, 0, dictSize)`.
+ #[test]
+ fn a_second_readable_buffer_gets_its_own_allocation_rather_than_a_second_copy_of_the_corpus() {
+ let plans = plan_candidate(&lz4_using_dict(), "lz4.h", &Vocabulary::default()).unwrap();
+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {
+ unreachable!()
+ };
+ assert_eq!(args[0], Value::NulTerminated, "src is the corpus");
+ assert_eq!(args[2], Value::Size, "srcSize is the corpus length");
+ assert_eq!(
+ args[4],
+ Value::AuxBuffer {
+ cast: Some("const char *".to_owned())
+ },
+ "dictStart is a second input the fuzzer cannot supply"
+ );
+ assert_eq!(args[5], Value::AuxCapacity, "and it declares its own size");
+ assert!(plans[0].needs_aux_buffer);
+ }
+
+ // The isolating control for POSITION deciding which buffer is primary. `src` and
+ // `dictStart` are the same type, so `buffer_quality` scores them equal and the
+ // signature's own `max_by_key` returns the LAST maximum - reusing that would have given
+ // the dictionary the corpus and handed the real input a zeroed buffer.
+ #[test]
+ fn the_first_readable_buffer_keeps_the_corpus_and_never_swaps_with_the_second() {
+ let plans = plan_candidate(&lz4_using_dict(), "lz4.h", &Vocabulary::default()).unwrap();
+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {
+ unreachable!()
+ };
+ assert!(
+ !matches!(args[0], Value::AuxBuffer { .. }),
+ "src must never become the auxiliary buffer"
+ );
+ assert_ne!(args[2], Value::AuxCapacity, "nor may srcSize");
+ }
+
+ // The other side of the control: one readable buffer means no auxiliary allocation at
+ // all, so the rule cannot be a blanket "every char buffer gets its own memory".
+ #[test]
+ fn a_signature_with_one_readable_buffer_allocates_no_auxiliary() {
+ let mut single = lz4_using_dict();
+ single.name = "LZ4_decompress_safe".to_owned();
+ single.params.truncate(4);
+ single.analysis.roles.truncate(4);
+ single.analysis.extents = BTreeMap::from([(0, 2), (1, 3)]);
+ let plans = plan_candidate(&single, "lz4.h", &Vocabulary::default()).unwrap();
+ assert!(!plans[0].needs_aux_buffer);
+ let Stmt::Call { args, .. } = call_in(&plans[0]) else {
+ unreachable!()
+ };
+ assert_eq!(args[0], Value::NulTerminated);
+ }
+
#[test]
fn a_glued_lowercase_extent_name_is_still_an_extent() {
assert!(is_extent_name("maxlen"));
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 1f94a703..0ac8a750 100755
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh
@@ -389,6 +389,43 @@ else
$(echo "$LADDER_BROTLI" | grep '^call order')"
fi
+# B72. Two readable buffers in one signature are two inputs and the fuzzer supplies one.
+# `LZ4_decompress_safe_usingDict(src, dst, srcSize, dstCapacity, dictStart, dictSize)` used to
+# take `lisdex_text` for BOTH `src` and `dictStart` with `size` as both extents. Not a
+# memory-safety lie - both reads are in bounds - but not what the authors do: their own
+# decompress_fuzzer.c memsets the dictionary and says in a comment that its contents do not
+# matter, so what the harness must reproduce is a DISTINCT buffer of a declared size.
+LZ4_EMIT="$OUT/aux-lz4"
+rm -rf "$LZ4_EMIT"
+if "$LISDEX" harness "$SRC/lz4" --out "$LZ4_EMIT" --no-verify >/dev/null 2>&1 \
+ && LZ4_C="$(find "$LZ4_EMIT" -maxdepth 1 -name '*.c' | head -1)" && [ -n "$LZ4_C" ]; then
+ ENTRY_CALL="$(grep 'LZ4_decompress' "$LZ4_C" | grep -v '^//' | tail -1)"
+ # `grep -c` counts LINES, so it returns 1 for a call that passes the same pointer twice
+ # on one line - which is the whole defect. Count OCCURRENCES.
+ CORPUS_ARGS="$(grep -o 'lisdex_text' <<<"$ENTRY_CALL" | wc -l)"
+ if [ "$CORPUS_ARGS" -eq 1 ]; then
+ ok "lz4's dictionary is its own buffer, not a second copy of the corpus pointer"
+ else
+ bad "the same corpus pointer is passed twice (B72): $ENTRY_CALL"
+ fi
+ # Isolates DISTINCTNESS from the corpus test: the primary buffer must still be the
+ # corpus. `src` and `dictStart` are the same type, so a rule keyed on the signature's
+ # own strongest-buffer pick would have swapped them and fuzzed the dictionary slot.
+ if grep -q 'LZ4_decompress[A-Za-z_0-9]*(lisdex_text' "$LZ4_C"; then
+ ok "and the FIRST readable buffer is still the one holding the corpus"
+ else
+ bad "the corpus and the dictionary were swapped: $ENTRY_CALL"
+ fi
+ if grep -q "memset(lisdex_aux, 0, LISDEX_AUX_CAPACITY);" "$LZ4_C" \
+ && [ "$(grep -c 'free(lisdex_aux);' "$LZ4_C")" -ge 1 ]; then
+ ok "the auxiliary buffer is zeroed like the authors' dictionary and freed"
+ else
+ bad "the auxiliary buffer is not zeroed or not freed, so LSAN blames the library"
+ fi
+else
+ bad "lz4 emitted nothing to check for B72"
+fi
+
# 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"