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
fix(lisdex): B30 - refuse a quoted include that resolves to another copy of the library
CarterPerez-devd26f73dd544d
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.rsindex 8e07597b..12fb4583 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@@ -142,6 +142,14 @@ pub enum ExtractError {count: usize,first: String,},+ #[error(+ "{path} resolved its own quoted include {spelling:?} to {resolved}, which lies outside the source tree and every include path lisdex was given - that is a different copy of this library, so the declarations parsed here need not match the sources being built"+ )]+ ForeignInclude {+ path: PathBuf,+ spelling: String,+ resolved: PathBuf,+ },#[error("a libclang parse was started from inside another one; PARSE_LOCK is not reentrant and would deadlock forever")]@@ -477,6 +485,83 @@ fn collect_vocabulary(entity: &Entity, target: &Path, into: &mut Vocabulary) {}}+// B30: a system-installed copy of the target satisfies the target's own #includes and the+// parse SUCCEEDS, so no diagnostic can catch it. The trusted set is every path lisdex or the+// user pointed at plus the includer's own directory; the compiler's default search path is+// exactly what is missing from it, which is where the foreign copy lives.+fn trusted_roots(target: &Path, options: &ParseOptions) -> Vec<PathBuf> {+ let mut roots = Vec::new();+ if let Some(parent) = target.parent() {+ roots.push(parent.to_path_buf());+ }+ roots.extend(options.includes.iter().cloned());+ roots+ .iter()+ .filter_map(|root| std::fs::canonicalize(root).ok())+ .collect()+}++// A quoted include means "my own file"; an angled one means "somebody else's". Only the+// quoted form is evidence of the wrong library, and libclang exposes the difference nowhere+// except the raw tokens - an angled path is punctuation, a quoted one is a single literal.+fn is_quoted_include(entity: &Entity) -> bool {+ entity.get_range().is_some_and(|range| {+ range+ .tokenize()+ .iter()+ .any(|token| token.get_spelling().starts_with('"'))+ })+}++fn located_in(path: &Path, roots: &[PathBuf]) -> bool {+ roots.iter().any(|root| path.starts_with(root))+}++fn foreign_include(entity: &Entity, roots: &[PathBuf]) -> Option<(String, PathBuf)> {+ for child in entity.get_children() {+ if child.get_kind() != EntityKind::InclusionDirective {+ continue;+ }+ let Some(includer) = child+ .get_location()+ .and_then(|location| location.get_file_location().file)+ .and_then(|file| std::fs::canonicalize(file.get_path()).ok())+ else {+ continue;+ };+ if !located_in(&includer, roots) {+ continue;+ }+ let Some(resolved) = child+ .get_file()+ .and_then(|file| std::fs::canonicalize(file.get_path()).ok())+ else {+ continue;+ };+ if located_in(&resolved, roots) || !is_quoted_include(&child) {+ continue;+ }+ return Some((child.get_name().unwrap_or_default(), resolved));+ }+ None+}++fn refuse_foreign_includes(+ entity: &Entity,+ target: &Path,+ options: &ParseOptions,+) -> Result<(), ExtractError> {+ let roots = trusted_roots(target, options);+ match foreign_include(entity, &roots) {+ Some((spelling, resolved)) => Err(ExtractError::ForeignInclude {+ path: target.to_path_buf(),+ spelling,+ resolved,+ }),+ None => Ok(()),+ }+}+pub fn parse_vocabulary(path: &Path, options: &ParseOptions) -> Result<Vocabulary, ExtractError> {if !path.is_file() {return Err(ExtractError::NotAFile(path.to_path_buf()));@@ -514,6 +599,8 @@ pub fn parse_vocabulary(path: &Path, options: &ParseOptions) -> Result<Vocabular});}+ refuse_foreign_includes(&unit.get_entity(), &target, options)?;+let mut vocabulary = Vocabulary::default();collect_vocabulary(&unit.get_entity(), &target, &mut vocabulary);Ok(vocabulary)@@ -534,6 +621,7 @@ pub fn parse_header(path: &Path, options: &ParseOptions) -> Result<Vec<Declared>.parser(&target).arguments(&options.argv()).skip_function_bodies(true)+ .detailed_preprocessing_record(true).parse().map_err(|source| ExtractError::Parse {path: target.clone(),@@ -556,6 +644,8 @@ pub fn parse_header(path: &Path, options: &ParseOptions) -> Result<Vec<Declared>});}+ refuse_foreign_includes(&unit.get_entity(), &target, options)?;+let mut found = Vec::new();let mut seen = BTreeSet::new();collect(&unit.get_entity(), &target, &mut found, &mut seen);@@ -1056,6 +1146,100 @@ void WebPFreeDecBuffer(WebPDecBuffer *buffer);assert_eq!(*pointee, Pointee::UnsignedChar);}+ fn outside_tree(label: &str, name: &str, body: &str) -> PathBuf {+ let dir =+ std::env::temp_dir().join(format!("lisdex-outside-{label}-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&dir);+ std::fs::create_dir_all(&dir).unwrap();+ std::fs::write(dir.join(name), body).unwrap();+ dir+ }++ const GENERATED_CONFIG: &str = "typedef unsigned char CONFIGURED;\n";++ #[test]+ fn a_quoted_include_resolved_outside_every_trusted_root_is_refused() {+ let path = header(+ "foreignquoted",+ "#include \"lisdexconf.h\"\nint parse_frame(CONFIGURED *d, unsigned long n);\n",+ );+ let elsewhere = outside_tree("foreignquoted", "lisdexconf.h", GENERATED_CONFIG);+ let options = ParseOptions {+ extra_args: vec![format!("-I{}", elsewhere.display())],+ ..ParseOptions::default()+ };+ let error = parse_header(&path, &options).unwrap_err();+ let ExtractError::ForeignInclude {+ spelling, resolved, ..+ } = &error+ else {+ panic!("a foreign copy of a generated header must be refused, not parsed: {error:?}");+ };+ assert_eq!(spelling, "lisdexconf.h");+ assert!(resolved.starts_with(&elsewhere), "{resolved:?}");+ }++ // The isolating control for the rule above: identical fixture, identical resolution, the+ // ONLY difference is the bracket. If quoted-ness stops being detected this one starts+ // failing too, so neither test can alibi the other.+ #[test]+ fn an_angled_include_resolved_outside_every_trusted_root_is_accepted() {+ let path = header(+ "foreignangled",+ "#include <lisdexconf.h>\nint parse_frame(CONFIGURED *d, unsigned long n);\n",+ );+ let elsewhere = outside_tree("foreignangled", "lisdexconf.h", GENERATED_CONFIG);+ let options = ParseOptions {+ extra_args: vec![format!("-I{}", elsewhere.display())],+ ..ParseOptions::default()+ };+ let found = parse_header(&path, &options).unwrap();+ assert!(+ found.iter().any(|item| item.name == "parse_frame"),+ "an angled include is somebody else's header by construction and must still \+ resolve from the system: {:?}",+ names(&found)+ );+ }++ #[test]+ fn a_quoted_include_beside_the_header_is_not_foreign() {+ let path = header(+ "quotedsibling",+ "#include \"lisdexconf.h\"\nint parse_frame(CONFIGURED *d, unsigned long n);\n",+ );+ std::fs::write(+ path.parent().unwrap().join("lisdexconf.h"),+ GENERATED_CONFIG,+ )+ .unwrap();+ let found = parse_header(&path, &ParseOptions::default()).unwrap();+ assert!(+ found.iter().any(|item| item.name == "parse_frame"),+ "{:?}",+ names(&found)+ );+ }++ #[test]+ fn a_foreign_quoted_include_is_refused_when_mining_the_vocabulary_too() {+ let path = header(+ "foreignvocab",+ "#include \"lisdexconf.h\"\ntypedef struct ctx { int v; } ctx;\nint parse(ctx *c, CONFIGURED *d);\n",+ );+ let elsewhere = outside_tree("foreignvocab", "lisdexconf.h", GENERATED_CONFIG);+ let options = ParseOptions {+ extra_args: vec![format!("-I{}", elsewhere.display())],+ ..ParseOptions::default()+ };+ let error = parse_vocabulary(&path, &options).unwrap_err();+ assert!(+ matches!(error, ExtractError::ForeignInclude { .. }),+ "the harness compiles against these records, so the vocabulary path needs the \+ same refusal as the declaration path: {error:?}"+ );+ }+#[test]fn functions_inside_an_extern_c_block_are_not_missed() {let path = header(diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.shindex 716f3801..5b2661fa 100755--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/scripts/m3-gate.sh@@ -7,7 +7,10 @@ set -uo pipefailWORK="${LISDEX_M3_WORK:-/opt/lisdex-m3}"SRC="$WORK/src"OUT="$WORK/out"-LISDEX="${LISDEX_BIN:-./target/debug/lisdex}"+# The container exports CARGO_TARGET_DIR, so ./target/debug/lisdex is a path cargo+# never writes. This gate read a binary from it that was two days stale, which is+# how a green M3 gate came to say nothing about the code under test.+LISDEX="${LISDEX_BIN:-${CARGO_TARGET_DIR:-/cargo-target}/debug/lisdex}"PASS=0FAIL=0@@ -35,6 +38,15 @@ if [ "${1:-}" = "reset" ]; thenexit 0fi+cargo build -p lisdex || {+ echo " FAIL lisdex does not build"+ exit 1+}+[ -x "$LISDEX" ] || {+ echo " FAIL lisdex binary not found at $LISDEX"+ exit 1+}+mkdir -p "$SRC" "$OUT"clone() {@@ -197,6 +209,35 @@ elsebad "harness awareness had no effect (cold=$cold_png warm=$warm_png)"fi+# B30. CPATH puts a directory on the compiler's default search path without lisdex+# ever being told about it, which is exactly the shape of a system-installed copy of+# the target. The two halves differ in one thing only - whether the tree still owns+# its own pnglibconf.h - so a check that refused merely because a foreign copy is+# reachable fails the second half instead of passing both.+banner "B30 FOREIGN INCLUDE"+FOREIGN="$WORK/foreign"+B30SRC="$SRC/libpng-b30"+rm -rf "$FOREIGN" "$B30SRC"+mkdir -p "$FOREIGN"+cp -r "$SRC/libpng" "$B30SRC"+mv "$B30SRC/pnglibconf.h" "$FOREIGN/pnglibconf.h"++if CPATH="$FOREIGN" "$LISDEX" surface "$B30SRC" >"$OUT/b30-foreign.txt" 2>&1; then+ bad "libpng was analysed against a pnglibconf.h outside its own source tree"+elif grep -q "quoted include" "$OUT/b30-foreign.txt"; then+ ok "a quoted include resolving outside the source tree is refused and named"+else+ bad "libpng was refused, but not by the foreign-include check"+fi++cp "$FOREIGN/pnglibconf.h" "$B30SRC/pnglibconf.h"+if CPATH="$FOREIGN" "$LISDEX" surface "$B30SRC" >"$OUT/b30-intree.txt" 2>&1; then+ ok "the same tree owning its own pnglibconf.h still analyses with the foreign copy reachable"+else+ bad "the foreign-include check rejects a tree that resolves its own config: \+$(head -1 "$OUT/b30-intree.txt")"+fi+banner "RESULT"echo " passed: $PASS"echo " failed: $FAIL"