Sandbox härten (#1043): cap-drop=ALL, no-new-privileges, Nur-Lesen-Wurzel, tmpfs mit Größenlimit für /work, /tmp, /checks; Tests gegen echtes Podman
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -12,6 +12,10 @@ use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub const WORK_DIR: &str = "/work";
|
||||
/// Größe der schreibbaren Ordner im Container in MB. Sie zählen zum Speicherlimit des Containers.
|
||||
pub const WORK_SIZE_MB: u32 = 256;
|
||||
pub const TMP_SIZE_MB: u32 = 128;
|
||||
pub const CHECKS_SIZE_MB: u32 = 64;
|
||||
/// Obergrenze je Ausgabestrom eines Befehls. Mehr wird gelesen und verworfen.
|
||||
pub const MAX_CAPTURE_BYTES: u64 = 1 << 20;
|
||||
|
||||
@ -57,6 +61,18 @@ pub fn run_args(name: &str, image: &str, limits: &Limits) -> Vec<String> {
|
||||
limits.cpus.clone(),
|
||||
"--pids-limit".into(),
|
||||
"512".into(),
|
||||
// Härtung (#1043): keine Linux-Capabilities, keine neuen Rechte, Wurzeldateisystem nur lesbar.
|
||||
"--cap-drop=ALL".into(),
|
||||
"--security-opt=no-new-privileges".into(),
|
||||
"--read-only".into(),
|
||||
// Schreibbar sind nur diese Ordner, im Arbeitsspeicher und mit Größenlimit. So kann ein Lauf
|
||||
// weder die Platte füllen noch das System im Container verändern.
|
||||
"--tmpfs".into(),
|
||||
format!("{WORK_DIR}:rw,exec,nosuid,nodev,size={WORK_SIZE_MB}m"),
|
||||
"--tmpfs".into(),
|
||||
format!("/tmp:rw,exec,nosuid,nodev,size={TMP_SIZE_MB}m"),
|
||||
"--tmpfs".into(),
|
||||
format!("/checks:rw,exec,nosuid,nodev,size={CHECKS_SIZE_MB}m"),
|
||||
"--label".into(),
|
||||
"coding-bench=lauf".into(),
|
||||
// `--` beendet die Optionen: ein Image-Name wird so nie als Option gelesen.
|
||||
@ -300,6 +316,18 @@ mod tests {
|
||||
assert!(s.ends_with("img:1 sleep infinity"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_args_haerten_den_container() {
|
||||
let s = run_args("cb-x", "img:1", &Limits::default()).join(" ");
|
||||
assert!(s.contains("--cap-drop=ALL"));
|
||||
assert!(s.contains("--security-opt=no-new-privileges"));
|
||||
assert!(s.contains("--read-only"));
|
||||
assert!(s.contains("--tmpfs /work:rw,exec,nosuid,nodev,size=256m"));
|
||||
assert!(s.contains("--tmpfs /checks:"));
|
||||
assert!(s.contains("--pids-limit 512"));
|
||||
assert!(!s.contains("--privileged"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_steht_nach_doppeltem_strich() {
|
||||
let args = run_args("cb-x", "img:1", &Limits::default());
|
||||
@ -374,4 +402,46 @@ mod tests {
|
||||
assert!(String::from_utf8_lossy(&ps.stdout).trim().is_empty(), "Container nicht entfernt");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// Echter Test der Härtung (#1043) gegen Podman. `CB_TEST_IMAGE=cb-python:latest cargo test sandbox::tests::gehaertet -- --ignored`
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn gehaertet_keine_rechte_nur_work_schreibbar_mit_limit() {
|
||||
let image = std::env::var("CB_TEST_IMAGE").unwrap_or_else(|_| "cb-python:latest".into());
|
||||
let name = container_name(&format!("hart-{}", std::process::id()));
|
||||
remove_by_name("podman", &name);
|
||||
let dir = std::env::temp_dir().join(format!("cb-hart-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(dir.join("start.txt"), "hallo").unwrap();
|
||||
std::fs::write(dir.join("lauf.sh"), "#!/bin/sh\necho ausgefuehrt\n").unwrap();
|
||||
let _ = Command::new("chmod").args(["+x", &dir.join("lauf.sh").display().to_string()]).output();
|
||||
|
||||
let sb = Sandbox::start("podman", &name, &image, &Limits::default(), Some(&dir)).unwrap();
|
||||
let ex = |cmd: &str| sb.exec(cmd, Duration::from_secs(30)).unwrap();
|
||||
|
||||
// Keine Capabilities, keine neuen Rechte.
|
||||
let st = ex("grep -E 'CapEff|NoNewPrivs' /proc/self/status");
|
||||
assert!(st.stdout.contains("CapEff:\t0000000000000000"), "{}", st.stdout);
|
||||
assert!(st.stdout.contains("NoNewPrivs:\t1"), "{}", st.stdout);
|
||||
|
||||
// Wurzeldateisystem nur lesbar, /work, /tmp und /checks schreibbar.
|
||||
assert_ne!(ex("touch /usr/neu 2>/dev/null").exit_code, Some(0), "/usr darf nicht schreibbar sein");
|
||||
assert_eq!(ex("touch /work/a /tmp/a /checks/a").exit_code, Some(0));
|
||||
|
||||
// Startdateien sind da, ein ausführbares Skript in /work läuft.
|
||||
assert_eq!(ex("cat /work/start.txt").stdout.trim(), "hallo");
|
||||
assert!(ex("/work/lauf.sh").stdout.contains("ausgefuehrt"));
|
||||
|
||||
// Größenlimit: 300 MB passen nicht in /work (Limit 256 MB).
|
||||
let big = ex("dd if=/dev/zero of=/work/gross bs=1M count=300 2>&1; echo exit=$?");
|
||||
assert!(!big.stdout.contains("exit=0"), "Limit muss greifen: {}", big.stdout);
|
||||
|
||||
// Ergebnis lässt sich trotzdem herausholen.
|
||||
let out = dir.join("export");
|
||||
sb.export_work(&out).unwrap();
|
||||
assert!(out.join("start.txt").is_file());
|
||||
sb.remove().unwrap();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user