|
| 1 | +//! Scratch probe: dump GitHub runner unshare semantics (temporary, PR will be closed). |
| 2 | +#![cfg(target_os = "linux")] |
| 3 | + |
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +fn run(args: &[&str]) -> (i32, String, String) { |
| 7 | + let out = Command::new("unshare").args(args).output(); |
| 8 | + match out { |
| 9 | + Ok(o) => ( |
| 10 | + o.status.code().unwrap_or(-1), |
| 11 | + String::from_utf8_lossy(&o.stdout).trim().to_string(), |
| 12 | + String::from_utf8_lossy(&o.stderr).trim().to_string(), |
| 13 | + ), |
| 14 | + Err(e) => (-1, String::new(), format!("spawn error: {e}")), |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +#[test] |
| 19 | +fn dump_unshare_semantics() { |
| 20 | + let uid = unsafe { libc::getuid() }; |
| 21 | + let mut report = String::new(); |
| 22 | + report.push_str(&format!("uid={uid} euid={}\n", unsafe { libc::geteuid() })); |
| 23 | + for f in ["/etc/subuid", "/etc/subgid"] { |
| 24 | + report.push_str(&format!("--- {f} ---\n")); |
| 25 | + if let Ok(s) = std::fs::read_to_string(f) { |
| 26 | + report.push_str(&s); |
| 27 | + } else { |
| 28 | + report.push_str("(unreadable)\n"); |
| 29 | + } |
| 30 | + } |
| 31 | + for k in ["/proc/sys/kernel/unprivileged_userns_clone", "/proc/sys/kernel/apparmor_restrict_unprivileged_userns"] { |
| 32 | + report.push_str(&format!("{k} = {}\n", std::fs::read_to_string(k).unwrap_or_else(|_| "(n/a)".into()))); |
| 33 | + } |
| 34 | + for (name, args) in [ |
| 35 | + ("plain", &["--user", "--map-root-user", "true"][..]), |
| 36 | + ("auto", &["--user", "--map-root-user", "--map-auto", "true"][..]), |
| 37 | + ( |
| 38 | + "plain-full", |
| 39 | + &["--user", "--map-root-user", "--mount", "--ipc", "--pid", "--uts", "--fork", "sh", "-lc", "echo alpha"][..], |
| 40 | + ), |
| 41 | + ( |
| 42 | + "auto-full", |
| 43 | + &["--user", "--map-root-user", "--map-auto", "--mount", "--ipc", "--pid", "--uts", "--fork", "sh", "-lc", "echo alpha"][..], |
| 44 | + ), |
| 45 | + ] { |
| 46 | + let (rc, so, se) = run(args); |
| 47 | + report.push_str(&format!("[{name}] rc={rc} stdout={so:?} stderr={se:?}\n")); |
| 48 | + } |
| 49 | + panic!("PROBE REPORT:\n{report}"); |
| 50 | +} |
0 commit comments