From 3879b4cc4df4ff8645c9cd556ab803f3018c18e6 Mon Sep 17 00:00:00 2001 From: Niklas Haiden Date: Thu, 22 Oct 2026 19:51:01 +0200 Subject: [PATCH] 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. --- src/vm.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/vm.rs b/src/vm.rs index cf29b3c..9567c49 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -119,8 +119,9 @@ pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec, extra: &[String], g } } for e in env { - if let Some((k, v)) = e.split_once('=') { - s += &format!("export {k}={}\n", sq(v)); + match export(&e) { + Some(line) => s += &line, + None => eprintln!("wsbx: skipping env entry with invalid name: {e:?}"), } } s += &format!("export WSBX_USER={}\n", sq(&user)); @@ -241,6 +242,13 @@ pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec, 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 { + 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. fn guest_output(share: &Path, name: &str) -> Option { let p = share.join(name); @@ -282,6 +290,14 @@ pub fn term_size() -> Option<(u16, u16)> { mod tests { 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] fn vfkit_injection_rejected() { assert!(super::vf(Path::new("/Users/me/repo")).is_ok());