diff --git a/changelog.d/+cargo-prelude-quoting.bugfix.md b/changelog.d/+cargo-prelude-quoting.bugfix.md new file mode 100644 index 00000000..17dfd768 --- /dev/null +++ b/changelog.d/+cargo-prelude-quoting.bugfix.md @@ -0,0 +1 @@ +The cargo-on-EESSI prelude quotes `CC` and `LIBRARY_PATH`. `CC='gcc; echo pwned'` is stored as the linker, and it is not run. diff --git a/docs/orgmode/howto/pypi-extend.org b/docs/orgmode/howto/pypi-extend.org index 331971e4..bde3943b 100644 --- a/docs/orgmode/howto/pypi-extend.org +++ b/docs/orgmode/howto/pypi-extend.org @@ -124,7 +124,8 @@ a residual, not a silent SAT edge. A PEP 518 ~build_system~ block with ~build-backend = mesonpy~ adds the listed build requirements and emits ~wrap_mode=default~ plus the same cargo-on-EESSI isolation prelude used by Cargo leftovers: drop host rustc wrappers, set -~LINKER=${CC:-gcc}~, put the EESSI compat ~ld~ on ~PATH~, and point +~LINKER="$CC"~ (an empty ~CC~ becomes ~gcc~, and the value is not +evaluated), put the EESSI compat ~ld~ on ~PATH~, and point ~RUSTFLAGS -B~ at that ~ld~. The cargo target triple and compat arch come from the build host (~rustc -vV~, ~uname -m~), not from a plan-time x86_64 literal. Hatchling and ~meson-python~ already on diff --git a/docs/orgmode/reference/cli.org b/docs/orgmode/reference/cli.org index b43536a5..82d5f591 100644 --- a/docs/orgmode/reference/cli.org +++ b/docs/orgmode/reference/cli.org @@ -181,7 +181,9 @@ it. Do not hand-edit the emitted recipe for those names. A PEP 518 ~build_system~ object with ~build-backend = mesonpy~ is the mesonpy hint: listed ~requires~ become build deps in the Boolean satisfiability (SAT) solve, and the emitter writes ~wrap_mode=default~ plus the shared cargo-on-EESSI isolation prelude -(host wrappers off, ~LINKER=${CC:-gcc}~, compat ~ld~ from ~uname -m~). +(host wrappers off, ~LINKER="$CC"~ after an empty ~CC~ becomes ~gcc~, +compat ~ld~ from ~uname -m~). ~CC~ and ~LIBRARY_PATH~ stay quoted, so a +value in either is not run as shell. Hatchling stays a ~Python-bundle-PyPI~ module when the robot provides it. See [[file:../howto/pypi-extend.org][site Python extras]]. diff --git a/llms-full.txt b/llms-full.txt index 7cb9a62a..0f60d338 100644 --- a/llms-full.txt +++ b/llms-full.txt @@ -1288,7 +1288,8 @@ a residual, not a silent SAT edge. A PEP 518 `build_system` block with `build-backend = mesonpy` adds the listed build requirements and emits `wrap_mode=default` plus the same cargo-on-EESSI isolation prelude used by Cargo leftovers: drop host rustc wrappers, set -`LINKER=${CC:-gcc}`, put the EESSI compat `ld` on `PATH`, and point +`LINKER="$CC"` (an empty `CC` becomes `gcc`, and the value is not +evaluated), put the EESSI compat `ld` on `PATH`, and point `RUSTFLAGS -B` at that `ld`. The cargo target triple and compat arch come from the build host (`rustc -vV`, `uname -m`), not from a plan-time x86_64 literal. Hatchling and `meson-python` already on @@ -6885,7 +6886,9 @@ it. Do not hand-edit the emitted recipe for those names. A PEP 518 `build_system` object with `build-backend = mesonpy` is the mesonpy hint: listed `requires` become build deps in the Boolean satisfiability (SAT) solve, and the emitter writes `wrap_mode=default` plus the shared cargo-on-EESSI isolation prelude -(host wrappers off, `LINKER=${CC:-gcc}`, compat `ld` from `uname -m`). +(host wrappers off, `LINKER="$CC"` after an empty `CC` becomes `gcc`, +compat `ld` from `uname -m`). `CC` and `LIBRARY_PATH` stay quoted, so a +value in either is not run as shell. Hatchling stays a `Python-bundle-PyPI` module when the robot provides it. See [site Python extras](https://eb-stack.rgoswami.me/../howto/pypi-extend.html.md). diff --git a/src/cargo.rs b/src/cargo.rs index d7179070..75e21101 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -27,17 +27,22 @@ pub fn eessi_cargo_host_isolation() -> &'static str { "unset RUSTC_WRAPPER CARGO_BUILD_RUSTC_WRAPPER RUSTC_WORKSPACE_WRAPPER RUSTFLAGS CARGO_ENCODED_RUSTFLAGS && ", r#"eval $(env | awk -F= '/^CARGO_TARGET_[A-Z0-9_]*_(LINKER|RUSTFLAGS)=/{print "unset "$1}') && "#, "export CARGO_HOME=%(builddir)s/.cargohome && ", - "export LINKER=${CC:-gcc} && ", + // CC and LIBRARY_PATH are data. An unquoted expansion is reparsed by + // `eval`, so `CC='gcc; echo pwned'` would run. Default an empty CC, + // keep the value quoted, and only allow a triple that is a variable name. + r#"if [ -z "${CC:-}" ]; then CC=gcc; fi && "#, + r#"export LINKER="$CC" && "#, // An ordinary recipe has no rustc. The host triple is best-effort, // the same way scripts/eessi-extend-eb.sh discovers it, so a missing // compiler does not drop the compat-layer linker from PATH. r#"_triple=""; if _host=$(rustc -vV 2>/dev/null); then _triple=$(printf '%s\n' "$_host" | awk '/^host:/{print $2}'); fi && "#, - r#"_triple_env=$(printf %s "$_triple" | tr "[:lower:]-" "[:upper:]_") && "#, - r#"if [ -n "$_triple_env" ]; then eval export CARGO_TARGET_${_triple_env}_LINKER=${CC:-gcc}; fi && "#, + r#"_triple_env=$(printf %s "$_triple" | tr "[:lower:]-" "[:upper:]_" | tr -cd 'A-Z0-9_') && "#, + r#"if [ -n "$_triple_env" ]; then eval "export CARGO_TARGET_${_triple_env}_LINKER=\"\$CC\""; fi && "#, r#"_arch=$(uname -m) && "#, r#"_ebld=${EESSI_EPREFIX:-${EESSI_CVMFS_REPO:-/cvmfs/software.eessi.io}/versions/${EESSI_VERSION:-2026.06}/compat/linux/${_arch}}/usr/bin && "#, r#"export PATH="$_ebld:$PATH" && "#, - r#"export RUSTFLAGS="-C link-arg=-B$_ebld $(printf -- '-L %s ' $(echo ${LIBRARY_PATH:-} | tr ':' ' '))" && "#, + r#"_rlibs= && _ifs="$IFS" && IFS=: && set -f && for _dir in ${LIBRARY_PATH:-}; do [ -n "$_dir" ] || continue; _rlibs="${_rlibs} -L ${_dir}"; done && IFS="$_ifs" && set +f && "#, + r#"export RUSTFLAGS="-C link-arg=-B$_ebld${_rlibs}" && "#, ) } @@ -518,10 +523,38 @@ pyo3 = "0.22" "a missing rustc must not abort the prelude" ); assert!(prelude.contains("link-arg=-B")); - assert!(prelude.contains("LINKER=${CC:-gcc}")); + assert!(prelude.contains("export LINKER=\"$CC\"")); + assert!(prelude.contains("tr -cd 'A-Z0-9_'")); + assert!(!prelude.contains("LINKER=${CC:-gcc}")); assert!( !prelude.contains("X86_64"), "triple and compat arch are runtime, not a plan-time x86_64 literal:\n{prelude}" ); } + + #[test] + fn eessi_cargo_isolation_does_not_run_a_command_hidden_in_cc() { + let prelude = eessi_cargo_host_isolation(); + // EasyBuild substitutes %(builddir)s before the shell sees the line. + // A raw parenthesis is a syntax error, so the substitution has to + // happen here too or the injection never runs. + let script = format!("{} true\n", prelude.replace("%(builddir)s", "/tmp")); + let out = std::process::Command::new("bash") + .arg("-c") + .arg(&script) + .env("CC", "gcc; echo CC_PWNED") + .env("LIBRARY_PATH", "/usr/lib") + .output() + .expect("bash"); + let text = format!( + "{}{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); + assert!( + out.status.success() && !text.contains("CC_PWNED"), + "status {:?} prelude injected or failed:\nPRELUDE {prelude}\nOUT {text}", + out.status + ); + } } diff --git a/tests/pypi_cran_extend.rs b/tests/pypi_cran_extend.rs index 2c55eb54..5896e16b 100644 --- a/tests/pypi_cran_extend.rs +++ b/tests/pypi_cran_extend.rs @@ -239,7 +239,10 @@ fn assert_eessi_cargo_isolation(text: &str) { && text.contains("${EESSI_EPREFIX:-") && text.contains("compat/linux/${_arch}}/usr/bin") && text.contains("link-arg=-B") - && text.contains("LINKER=${CC:-gcc}") + && text.contains("export LINKER=\"$CC\"") + && text.contains("tr -cd") + && !text.contains("LINKER=${CC:-gcc}") + && !text.contains("echo ${LIBRARY_PATH") && !text.contains("X86_64"), "cargo on EESSI must share one host-derived isolation prelude:\n{text}" );