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): B73 - a leftover extent is a knob unless it bounds memory we allocated

CarterPerez-dev88ecbb7bfddf
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 78a6efa1..a1121fb7 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,6 +6,7 @@ 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;
use lisdex_surface::signature::{
Analysis, Fuzzability, Param, ParamRole, Pointee, Shape, handle_key,
};
@@ -411,6 +412,34 @@ fn indirect_extent_local(candidate: &Candidate) -> Option<Stmt> {
})
}
+// A leftover extent is refused because its MEANING is not header-visible, and the census
+// says so: across 306 candidates on seven trees, 23 integrals reach this arm and they are
+// four unrelated quantities - libarchive's `read_size` is a block size, libjpeg-turbo's `n`
+// is an array element count, libwebp's `chunk_size` is a sub-extent of the input, libpng's
+// `num_to_check` IS the buffer's missing length. No single invented value is right for all
+// of them, and 0 is not the safe universal it looks like: it makes the library decode
+// nothing, which is B74's constant program wearing different clothes.
+//
+// The one case where the number is a FACT the harness owns rather than a guess is an extent
+// that names an OUTPUT quantity in a signature whose output buffer already declares a
+// capacity. lz4's own ossfuzz/decompress_fuzzer.c writes exactly that call:
+// `LZ4_decompress_safe_partial(data, dst, size, dstCapacity, dstCapacity)`. The harness
+// allocated that buffer, so asking the library for at most as much as it holds cannot
+// overrun it, and an overrun anyway is the library breaking its contract - the same line
+// B67 draws for the capacity itself.
+//
+// Both halves are load-bearing and the census sizes each: 3 of the 23 name an output, and
+// only 1 of those three sits in a signature with a sized output buffer. Dropping the second
+// half would hand `LZ4_decompress_safe_partial_usingDict` a capacity for a buffer nothing
+// allocated.
+fn bounds_the_output(label: &str, analysis: &Analysis) -> bool {
+ signature::matches_vocabulary(label, signature::OUTPUT_NAMES)
+ && analysis
+ .extents
+ .keys()
+ .any(|pointer| analysis.roles[*pointer] == ParamRole::Output)
+}
+
fn owns_this_extent(analysis: &Analysis, extent: usize) -> Option<usize> {
analysis
.extents
@@ -525,6 +554,9 @@ fn bind(
}
ParamRole::Config => {
if is_extent_name(&label) {
+ if bounds_the_output(&label, analysis) {
+ return Ok(Value::OutCapacity);
+ }
return Err(Refusal::UnboundExtent { param: label });
}
Ok(Value::Zero)
@@ -2048,6 +2080,102 @@ mod tests {
);
}
+ // `LZ4_decompress_safe_partial(src, dst, srcSize, targetOutputSize, dstCapacity)` -
+ // real, and the only one of the 23 leftover extents on seven trees that binds.
+ fn lz4_partial() -> Candidate {
+ let mut entry = candidate(
+ "LZ4_decompress_safe_partial",
+ vec![
+ param(
+ "src",
+ "const char *",
+ Shape::Pointer {
+ pointee: Pointee::Char,
+ is_const: true,
+ },
+ ),
+ param(
+ "dst",
+ "char *",
+ Shape::Pointer {
+ pointee: Pointee::Char,
+ is_const: false,
+ },
+ ),
+ param("srcSize", "int", Shape::Integral),
+ param("targetOutputSize", "int", Shape::Integral),
+ param("dstCapacity", "int", Shape::Integral),
+ ],
+ vec![
+ ParamRole::Buffer,
+ ParamRole::Output,
+ ParamRole::Length,
+ ParamRole::Config,
+ ParamRole::Length,
+ ],
+ Fuzzability::DirectBuffer,
+ Returns::default(),
+ );
+ entry.analysis.extents = BTreeMap::from([(0, 2), (1, 4)]);
+ entry
+ }
+
+ // lz4's own ossfuzz/decompress_fuzzer.c writes this call as
+ // `LZ4_decompress_safe_partial(data, dst, size, dstCapacity, dstCapacity)`, so the
+ // number is the capacity of a buffer the harness allocated - a fact it owns, not a
+ // guess about memory it does not.
+ #[test]
+ fn an_extent_bounding_the_output_takes_the_capacity_the_harness_allocated() {
+ let plans = plan_candidate(&lz4_partial(), "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[3],
+ Value::OutCapacity,
+ "targetOutputSize is what the authors pass: the output capacity"
+ );
+ assert_eq!(args[4], Value::OutCapacity, "and so is dstCapacity");
+ }
+
+ // Isolating control for the NAME half. `read_size` is libarchive's block size and `n`
+ // is libjpeg-turbo's array element count; both sit beside a sized buffer, and inventing
+ // 65536 for either is a lie the library acts on. Only a name that says OUTPUT may bind.
+ #[test]
+ fn an_extent_that_does_not_name_an_output_quantity_is_still_refused() {
+ let mut knob = lz4_partial();
+ knob.params[3].name = Some("read_size".to_owned());
+ assert_eq!(
+ plan_candidate(&knob, "lz4.h", &Vocabulary::default()),
+ Err(Refusal::UnboundExtent {
+ param: "read_size".to_owned()
+ })
+ );
+ }
+
+ // Isolating control for the SIZED-OUTPUT half. The output buffer is REMOVED rather than
+ // downgraded, because a downgraded one refuses at its own parameter first and alibis
+ // this rule: flipping `dst` to a Buffer makes it an unsized second input, and flipping
+ // it to an unsized Output trips B67 - either way index 3 is never reached. With no
+ // output buffer at all, `maxOutputSize` still names an output quantity and there is
+ // simply no allocation whose size it could honestly be.
+ #[test]
+ fn an_output_extent_without_an_output_buffer_that_declares_a_capacity_is_refused() {
+ let mut no_output = lz4_partial();
+ no_output.params.remove(1);
+ no_output.params[2].name = Some("maxOutputSize".to_owned());
+ no_output.analysis.roles = vec![ParamRole::Buffer, ParamRole::Length, ParamRole::Config];
+ no_output.analysis.extents = BTreeMap::from([(0, 1)]);
+ assert_eq!(
+ plan_candidate(&no_output, "lz4.h", &Vocabulary::default()),
+ Err(Refusal::UnboundExtent {
+ param: "maxOutputSize".to_owned()
+ })
+ );
+ }
+
// `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
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-surface/src/signature.rs
index ece0a071..64ee705a 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
@@ -212,9 +212,16 @@ pub struct Analysis {
}
fn name_matches(name: Option<&String>, candidates: &[&str]) -> bool {
- let Some(name) = name else {
- return false;
- };
+ name.is_some_and(|name| matches_vocabulary(name, candidates))
+}
+
+// Exposed so the synthesizer asks the SAME question of a name that the grader does. A
+// leftover extent is allowed to bind the output capacity only when it names an output
+// quantity, and "names an output quantity" has to mean exactly what it means when it
+// decides that a pointer is an output, or the two halves of the tool disagree about
+// `targetOutputSize`.
+#[must_use]
+pub fn matches_vocabulary(name: &str, candidates: &[&str]) -> bool {
let lowered = name.to_lowercase();
let tokens = tokenize(name);
candidates.iter().any(|candidate| {
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 0ac8a750..4f85f817 100755
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh
@@ -373,6 +373,30 @@ else
$(echo "$LADDER_LZ4" | grep '^call order')"
fi
+# B73. A leftover extent is refused because its meaning is not header-visible, but
+# `targetOutputSize` bounds a buffer the HARNESS allocated, and lz4's own decompress_fuzzer.c
+# passes exactly the capacity for it. Censused before the fix: 23 integrals reach that arm
+# across 306 candidates on seven trees, and this is the ONLY one that binds.
+if echo "$LADDER_LZ4" | grep -q "PLANS LZ4_decompress_safe_partial "; then
+ ok "lz4's partial decoder plans, taking the output capacity the authors pass"
+else
+ bad "LZ4_decompress_safe_partial is still refused: \
+$(echo "$LADDER_LZ4" | grep 'LZ4_decompress_safe_partial ')"
+fi
+# The paired negative, on two trees, because widening the rule to "bind every leftover" is
+# the failure that matters and it would look like reach. libarchive's `read_size` is a block
+# size, and libpng's `num_to_check` IS the buffer's own missing length - `png_sig_cmp(sig,
+# start, num_to_check)` leaves `sig` unclaimed, so a number invented there is read straight
+# off the end of the corpus. The two HALVES of the rule have no isolating control on any real
+# tree - no candidate in the set has both a leftover extent and a sized output buffer except
+# lz4's - so they are isolated by unit tests, which m4-gate runs as its stage 2.
+if echo "$LADDER_ARCHIVE" | grep -q "refused archive_read_open_memory2 - config read_size" \
+ && plan_of libpng | grep -q "refused png_sig_cmp - config num_to_check"; then
+ ok "a leftover extent that names no output quantity is still refused on two trees"
+else
+ bad "the output-capacity allow widened into block sizes and element counts"
+fi
+
LADDER_BROTLI="$(plan_of brotli)"
if echo "$LADDER_BROTLI" | grep "PLANS" | head -1 | grep -q "BrotliDecoderAttachDictionary"; then
bad "brotli still synthesizes a dictionary configurator"