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): B80 - do not publish a number that two separately linked binaries cannot agree on

CarterPerez-devda2ae7ea3730
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/coverage.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/coverage.rs
index 075ab9f8..5368d086 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/coverage.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-harness/src/coverage.rs
@@ -2,8 +2,52 @@
// coverage.rs
use std::collections::BTreeSet;
+use std::path::Path;
pub const EDGE_FIELD_SEPARATOR: char = ':';
+pub const AR_MAGIC: &[u8] = b"!<arch>\n";
+pub const AR_HEADER_BYTES: usize = 60;
+pub const AR_SIZE_RANGE: std::ops::Range<usize> = 48..58;
+pub const BITCODE_MAGIC: &[u8] = b"BC\xc0\xde";
+
+// Whether two SEPARATELY LINKED binaries built against this archive can have their edge IDs
+// compared at all, which is the precondition under every cross-harness coverage number.
+//
+// Measured on three real emitted parent/child pairs over eight seeds each. An LTO-bitcode
+// archive is re-instrumented at every link and the parent's map was contained in the child's
+// in 0 of 24 cases. An object archive carries PCGUARD guards whose IDs are assigned at STARTUP
+// over the guard array, so a harness compiled to bitcode still shifts the library's base: 16 of
+// 24. Matching both sides on PCGUARD gives 24 of 24. So a bitcode archive is the one state that
+// can be read off the artifact and is always wrong, and that is what this answers.
+#[must_use]
+pub fn is_bitcode_archive(path: &Path) -> bool {
+ let Ok(bytes) = std::fs::read(path) else {
+ return false;
+ };
+ if !bytes.starts_with(AR_MAGIC) {
+ // Not an archive at all - a bare object or a shared library. Its own magic decides.
+ return bytes.starts_with(BITCODE_MAGIC);
+ }
+ let mut at = AR_MAGIC.len();
+ while at + AR_HEADER_BYTES <= bytes.len() {
+ let header = &bytes[at..at + AR_HEADER_BYTES];
+ let size: usize = std::str::from_utf8(&header[AR_SIZE_RANGE])
+ .ok()
+ .and_then(|field| field.trim().parse().ok())
+ .unwrap_or(0);
+ let body = at + AR_HEADER_BYTES;
+ if size == 0 || body + size > bytes.len() {
+ return false;
+ }
+ if bytes[body..].starts_with(BITCODE_MAGIC) {
+ return true;
+ }
+ // Members are padded to an even offset, and the symbol table is a member like any
+ // other - it simply never carries the bitcode magic, so it needs no special case.
+ at = body + size + (size & 1);
+ }
+ false
+}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct EdgeSet {
@@ -77,6 +121,90 @@ pub fn parse_showmap(text: &str) -> EdgeSet {
mod tests {
use super::*;
+ // A real `ar`, built here rather than shelled out for, so the parser is tested against the
+ // format rather than against whatever `llvm-ar` happened to emit on this machine.
+ fn archive(members: &[(&str, &[u8])]) -> Vec<u8> {
+ let mut bytes = AR_MAGIC.to_vec();
+ for (name, body) in members {
+ bytes.extend_from_slice(format!("{name:<16}").as_bytes());
+ bytes.extend_from_slice(format!("{:<12}{:<6}{:<6}{:<8}", 0, 0, 0, 644).as_bytes());
+ bytes.extend_from_slice(format!("{:<10}", body.len()).as_bytes());
+ bytes.extend_from_slice(b"\x60\n");
+ bytes.extend_from_slice(body);
+ if body.len() % 2 == 1 {
+ bytes.push(b'\n');
+ }
+ }
+ bytes
+ }
+
+ fn written(name: &str, bytes: &[u8]) -> std::path::PathBuf {
+ let path = std::env::temp_dir().join(format!("lisdex-ar-{}-{name}", std::process::id()));
+ std::fs::write(&path, bytes).expect("fixture");
+ path
+ }
+
+ // B80. This is the fact under every cross-harness number: a bitcode archive is
+ // re-instrumented at every link, so two harnesses do not number the same edge the same way.
+ #[test]
+ fn an_archive_holding_llvm_bitcode_is_recognised_as_relinked_every_time() {
+ let path = written(
+ "lto",
+ &archive(&[("mylib.o/", b"BC\xc0\xde\x00\x01\x02\x03")]),
+ );
+ assert!(is_bitcode_archive(&path));
+ let _ = std::fs::remove_file(path);
+ }
+
+ // The isolating control: same container, same parser, an ELF member instead. Without this
+ // a function that simply returned true would pass the test above.
+ #[test]
+ fn an_archive_holding_elf_objects_is_not() {
+ let path = written("elf", &archive(&[("mylib.o/", b"\x7fELF\x02\x01\x01\x00")]));
+ assert!(!is_bitcode_archive(&path));
+ let _ = std::fs::remove_file(path);
+ }
+
+ // The symbol table is member ZERO of almost every real archive and never carries the
+ // magic, so a parser that only ever looked at the first member would call every archive
+ // an object archive - including libpng16.a, which is bitcode.
+ #[test]
+ fn bitcode_behind_a_symbol_table_member_is_still_found() {
+ let path = written(
+ "sym",
+ &archive(&[
+ ("/", b"\x00\x00\x00\x01symbols"),
+ ("mylib.o/", b"BC\xc0\xde\x00\x01\x02\x03"),
+ ]),
+ );
+ assert!(is_bitcode_archive(&path));
+ let _ = std::fs::remove_file(path);
+ }
+
+ // `--archive` is documented as taking an archive, but nothing stops a bare object or a
+ // shared library, and the renumbering does not care which container the bitcode arrived in.
+ // Gutting this arm reddened NOTHING until this pair existed.
+ #[test]
+ fn a_bare_bitcode_object_outside_any_archive_is_still_recognised() {
+ let path = written("bare-bc", b"BC\xc0\xde\x00\x01\x02\x03");
+ assert!(is_bitcode_archive(&path));
+ let _ = std::fs::remove_file(path);
+ }
+
+ #[test]
+ fn a_bare_elf_object_outside_any_archive_is_not() {
+ let path = written("bare-elf", b"\x7fELF\x02\x01\x01\x00");
+ assert!(!is_bitcode_archive(&path));
+ let _ = std::fs::remove_file(path);
+ }
+
+ #[test]
+ fn a_missing_archive_is_not_claimed_to_be_bitcode() {
+ assert!(!is_bitcode_archive(std::path::Path::new(
+ "/nonexistent/libnope.a"
+ )));
+ }
+
#[test]
fn parses_edge_ids_and_discards_hit_counts() {
let map = parse_showmap("000123:1\n004567:3\n065535:255\n");
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/src/main.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/src/main.rs
index 89495c7b..c1d04b80 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/src/main.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex/src/main.rs
@@ -421,10 +421,27 @@ fn harness(args: &HarnessArgs) -> Result<(), Box<dyn Error>> {
println!("no candidate in the top {} survived the oracles:", args.top);
return Err("no harness synthesized".into());
}
- report_survivors(&outcome.survivors, outcome.accepted.len());
+ report_survivors(
+ &outcome.survivors,
+ outcome.accepted.len(),
+ comparable_numbering(&args.oracle.archives),
+ );
Ok(())
}
+// B80. Every number in the survivor summary that spans MORE THAN ONE harness - the novelty
+// figure and the union total - rests on two separately linked binaries numbering the same edge
+// the same way. Against an LTO-bitcode archive they do not: measured over three real emitted
+// parent/child pairs and eight seeds, the shorter sequence's map was contained in the longer
+// one's in 0 of 24 cases, and renumbering can only INFLATE apparent novelty. A rejection
+// therefore still stands - a harness that looks redundant even with scrambled IDs really is -
+// but no cross-harness TOTAL may be printed as if it were measured.
+fn comparable_numbering(archives: &[std::path::PathBuf]) -> bool {
+ !archives
+ .iter()
+ .any(|archive| lisdex_harness::coverage::is_bitcode_archive(archive))
+}
+
// `--no-verify` promotes the first candidate that plans and stops. It cannot climb: an
// extension only earns its place against a parent that PASSED, and with no bench nothing has
// passed. Saying so is the whole content of the banner above.
@@ -443,19 +460,32 @@ fn unverified(attempts: &[search::Attempt], out: &std::path::Path) -> Result<(),
Err("no harness synthesized".into())
}
-fn report_survivors(survivors: &[search::Survivor], total: usize) {
+fn report_survivors(survivors: &[search::Survivor], total: usize, comparable: bool) {
+ let reach = if comparable {
+ format!(", covering {total} edges in total")
+ } else {
+ String::new()
+ };
println!(
- "\n{} harness(es) survived the oracles, covering {total} edges in total:",
+ "\n{} harness(es) survived the oracles{reach}:",
survivors.len()
);
for survivor in survivors {
let novelty = if survivor.judged_against == 0 {
"first accepted, nothing to be novel against".to_owned()
- } else {
+ } else if comparable {
format!(
"{} edge(s) novel against the {} already accepted",
survivor.novel, survivor.judged_against
)
+ } else {
+ format!(
+ "novelty against the {} already accepted NOT MEASURED - the target archive is \
+ LTO bitcode, so it is re-instrumented at every link and two harnesses do not \
+ number the same edge the same way; build the oracle bench against an archive \
+ whose instrumentation was fixed when the LIBRARY was built",
+ survivor.judged_against
+ )
};
let climbed = if survivor.steps == 0 {
String::new()
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 40936812..62af8e84 100755
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m4-gate.sh
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m4-gate.sh
@@ -135,6 +135,16 @@ if [ -n "$SUMMARY" ] && [ "$SUMMARY" -eq "$WRITTEN" ] && [ "$WRITTEN" -ge 1 ]; t
else
fail "survivor summary '$SUMMARY' does not match $WRITTEN harness(es) on disk"
fi
+# B80. magma's libpng16.a is an LTO-BITCODE archive, so it is re-instrumented at every link and
+# two separately linked harnesses do not number the same edge the same way - measured at 0 of 24
+# real (pair, seed) cases. Every number in this summary that spans MORE THAN ONE harness is
+# therefore not a measurement, and the union total must not be printed as though it were. The
+# opposite direction is asserted in stage 13 against an object archive, where it MUST appear.
+if grep -q 'harness(es) survived the oracles, covering' "$GATE/synth.log"; then
+ fail "a cross-harness edge total was published against an LTO-bitcode archive (B80)"
+else
+ pass "no cross-harness total is claimed against an archive that is relinked every time"
+fi
# The honesty line: with one survivor, oracle 4 could NOT discriminate and must say so
# rather than claim it ran.
if grep -q '4 global novelty could only reject a harness reaching nothing' "$GATE/synth.log"; then
@@ -649,6 +659,15 @@ else
fail "the emitted extension does not own its slot: \
$(grep -n 'lisdex_filled' "${ENTRY_C:-/dev/null}" | head -3)"
fi
+# B80, the ACCEPTING direction of the same rule, and the reason stage 4's assertion is a control
+# rather than a tautology. This lab's libmylib.a is an OBJECT archive, whose instrumentation was
+# fixed when the library was built, so the total IS a measurement and must be published. Gut the
+# archive check either way and exactly one of these two stages goes red.
+if grep -q 'harness(es) survived the oracles, covering' "$LAB/entry.log"; then
+ pass "and the same total IS published against an archive whose instrumentation is fixed"
+else
+ fail "a measurable cross-harness total was suppressed against an object archive"
+fi
printf '\n'
if [ "$FAILURES" -eq 0 ]; then