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): B13 - the bootstrap that exits 0 and writes a configure nothing can run

CarterPerez-dev7d44cb6cc921
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/ladder.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/ladder.rs
index 5b0d0463..b5068fe2 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/ladder.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/ladder.rs
@@ -259,10 +259,39 @@ fn execute(
};
return Ok((outcomes, Some(reason)));
}
+ if let Some(reason) = bootstrap_wrote_a_broken_configure(layout, step.name) {
+ return Ok((outcomes, Some(reason)));
+ }
}
Ok((outcomes, None))
}
+// `autoreconf` exits 0 having written a `configure` that cannot run, whenever a macro comes
+// from a namespace autoconf does not forbid. Reproduced: a configure.ac calling an
+// unavailable `AX_` macro bootstraps with exit 0, and the `configure` it wrote then dies with
+// `syntax error near unexpected token` on the line holding the macro. The build fails either
+// way; the difference is that the shell's message names a token and this one names the cause,
+// which is what the M5 repair tier needs to act on.
+fn bootstrap_wrote_a_broken_configure(layout: &BuildLayout, step: &str) -> Option<String> {
+ if step != plan::STEP_BOOTSTRAP {
+ return None;
+ }
+ let script = layout.source.join(plan::CONFIGURE_SCRIPT);
+ let found = plan::unexpanded_macros(&std::fs::read_to_string(&script).ok()?);
+ let first = found.first()?;
+ Some(format!(
+ "step {step} reported success but wrote a configure script with {} unexpanded m4 \
+ macro(s); {} is still a bare token at {}:{}. the macro's m4 file is not installed, \
+ so autoconf copied the call through instead of expanding it and the shell will read \
+ it as a command. install the package that provides it (autoconf-archive for AX_, \
+ pkg-config for PKG_, libtool for LT_) and build again",
+ found.len(),
+ first.token,
+ script.display(),
+ first.line
+ ))
+}
+
fn inspect_artifacts(
roots: &[PathBuf],
requirements: &Requirements,
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/plan.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/plan.rs
index 729770ee..2189b09a 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/plan.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-build/src/plan.rs
@@ -31,7 +31,62 @@ pub const ENV_NOCONFIGURE: &str = "NOCONFIGURE";
pub const ENV_CMAKE_POLICY_MINIMUM: &str = "CMAKE_POLICY_VERSION_MINIMUM";
pub const CMAKE_POLICY_MINIMUM: &str = "3.5";
-const CONFIGURE_SCRIPT: &str = "configure";
+pub const CONFIGURE_SCRIPT: &str = "configure";
+
+// autoconf already refuses an unexpanded macro whose name matches its own forbidden pattern,
+// `^_?A[CHUM]_`, and exits non-zero. Every namespace BELOW is one autoconf does not forbid,
+// which is why `autoreconf` can exit 0 having written a `configure` that dies on line 4216
+// with `syntax error near unexpected token`. These are the m4 namespaces the tools that
+// install macros actually use: autoconf-archive, pkg-config, libtool, gtk-doc, gettext,
+// glib and intltool.
+//
+// The prefix is REQUIRED rather than accepting any uppercase token, because a bare
+// `^[A-Z][A-Z0-9_]*\(` scan has a false positive on a real script: qemuafl's configure
+// embeds `G_DEFINE_AUTOPTR_CLEANUP_FUNC(Foo, foo_free);` inside a C compile test, which is
+// working code and not a leftover. Censused over every configure of 40KB or more in the
+// image: this list flags that file zero times and the reproduction once.
+pub const M4_NAMESPACES: &[&str] = &[
+ "AX_", "PKG_", "LT_", "AS_", "AU_", "GTK_DOC_", "GLIB_", "IT_", "BOOST_",
+];
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct UnexpandedMacro {
+ pub line: usize,
+ pub token: String,
+}
+
+fn macro_token(line: &str) -> Option<&str> {
+ let token = line.split('(').next()?;
+ if token.is_empty() || token.len() == line.len() {
+ return None;
+ }
+ if !token
+ .chars()
+ .all(|letter| letter.is_ascii_uppercase() || letter.is_ascii_digit() || letter == '_')
+ {
+ return None;
+ }
+ M4_NAMESPACES
+ .iter()
+ .any(|namespace| token.starts_with(namespace))
+ .then_some(token)
+}
+
+// A generated `configure` is shell, so a macro nobody expanded survives as a bare word at the
+// start of a line and the shell reads it as a command with a syntax error in its arguments.
+#[must_use]
+pub fn unexpanded_macros(script: &str) -> Vec<UnexpandedMacro> {
+ script
+ .lines()
+ .enumerate()
+ .filter_map(|(offset, line)| {
+ macro_token(line).map(|token| UnexpandedMacro {
+ line: offset + 1,
+ token: token.to_owned(),
+ })
+ })
+ .collect()
+}
const TARGET_CLEAN: &str = "clean";
const RELWITHDEBINFO_FLAGS: &str = "-O2 -g";
const AUTORECONF_ARGS: [&str; 4] = ["--force", "--install", "-I", "m4"];
@@ -961,4 +1016,69 @@ mod tests {
.unwrap();
assert_eq!(built.step(STEP_CONFIGURE).unwrap().spec.program, SH_BIN);
}
+
+ #[test]
+ fn a_macro_no_m4_file_expanded_is_found_where_the_shell_will_choke_on_it() {
+ let found = unexpanded_macros(
+ "#! /bin/sh\n\
+ ac_cv_prog_cc=gcc\n\
+ AX_TOTALLY_MADE_UP_MACRO(one, two)\n\
+ exit 0\n",
+ );
+ assert_eq!(found.len(), 1);
+ assert_eq!(found[0].line, 3);
+ assert_eq!(found[0].token, "AX_TOTALLY_MADE_UP_MACRO");
+ }
+
+ #[test]
+ fn c_code_a_configure_test_compiles_is_not_a_leftover_macro() {
+ let found = unexpanded_macros(
+ "G_DEFINE_AUTOPTR_CLEANUP_FUNC(Foo, foo_free);\n\
+ G_STATIC_ASSERT(sizeof(int) == 4);\n",
+ );
+ assert!(
+ found.is_empty(),
+ "qemuafl's configure embeds this in a compile test, and a scan that accepts any \
+ uppercase token refuses a project that builds perfectly well: {found:?}"
+ );
+ }
+
+ #[test]
+ fn a_macro_autoconf_itself_forbids_is_left_to_autoconf() {
+ let found = unexpanded_macros("AC_INIT(foo, 1.0)\nAM_INIT_AUTOMAKE(bar)\n");
+ assert!(
+ found.is_empty(),
+ "autoconf's own forbidden pattern already covers ^_?A[CHUM]_ and exits non-zero, \
+ so reporting these here would blame the bootstrap for a failure it did report"
+ );
+ }
+
+ #[test]
+ fn ordinary_shell_a_configure_script_is_made_of_is_not_flagged() {
+ // Every line here begins with an uppercase run and contains a parenthesis, so each
+ // one is a shape a looser reading of "bare macro call" would accept. `PKG_` and `LT_`
+ // are deliberately among them: those namespaces ARE on the list, and an assignment or
+ // a substitution using one is still ordinary shell rather than a leftover call.
+ let found = unexpanded_macros(
+ "PKG_CONFIG=$(which pkg-config)\n\
+ LT_SYS_LIBRARY_PATH=$(cat /etc/ld.so.conf)\n\
+ AX_FLAGS=\"$(echo -O2)\"\n\
+ CFLAGS=$(get_flags)\n\
+ LIBS=\"-lz $LIBS\"\n\
+ as_fn_error ()\n",
+ );
+ assert!(found.is_empty(), "{found:?}");
+ }
+
+ #[test]
+ fn every_namespace_the_list_carries_is_recognised() {
+ for namespace in M4_NAMESPACES {
+ let line = format!("{namespace}SOME_CHECK(arg)\n");
+ assert_eq!(
+ unexpanded_macros(&line).len(),
+ 1,
+ "{namespace} is listed but not matched"
+ );
+ }
+ }
}