fix(#1): skip env entries whose key is not a shell identifier

Image Env (and -e) keys were interpolated unquoted into run.sh, which runs as
root: a hostile image could execute commands as guest root via the key.
This commit is contained in:
2026-10-22 19:51:01 +02:00
parent b816e435df
commit 3879b4cc4d
+18 -2
View File
@@ -119,8 +119,9 @@ pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g
} }
} }
for e in env { for e in env {
if let Some((k, v)) = e.split_once('=') { match export(&e) {
s += &format!("export {k}={}\n", sq(v)); Some(line) => s += &line,
None => eprintln!("wsbx: skipping env entry with invalid name: {e:?}"),
} }
} }
s += &format!("export WSBX_USER={}\n", sq(&user)); s += &format!("export WSBX_USER={}\n", sq(&user));
@@ -241,6 +242,13 @@ pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g
} }
} }
/// `export K='v'` line; None unless K is a shell identifier (image Env is hostile and run.sh runs as root).
fn export(e: &str) -> Option<String> {
let (k, v) = e.split_once('=')?;
let ok = k.bytes().next().is_some_and(|b| b.is_ascii_alphabetic() || b == b'_') && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_');
ok.then(|| format!("export {k}={}\n", sq(v)))
}
/// Small result file written by the guest; regular files only (no symlinks), size-capped. /// Small result file written by the guest; regular files only (no symlinks), size-capped.
fn guest_output(share: &Path, name: &str) -> Option<String> { fn guest_output(share: &Path, name: &str) -> Option<String> {
let p = share.join(name); let p = share.join(name);
@@ -282,6 +290,14 @@ pub fn term_size() -> Option<(u16, u16)> {
mod tests { mod tests {
use std::path::Path; use std::path::Path;
#[test]
fn env_keys() {
assert_eq!(super::export("_A1=x y").as_deref(), Some("export _A1='x y'\n"));
for bad in ["PWN$(id > /ws/pwn)=x", "=x", "1A=x", "A B=x", "A;id=x", "noequals"] {
assert!(super::export(bad).is_none(), "{bad}");
}
}
#[test] #[test]
fn vfkit_injection_rejected() { fn vfkit_injection_rejected() {
assert!(super::vf(Path::new("/Users/me/repo")).is_ok()); assert!(super::vf(Path::new("/Users/me/repo")).is_ok());