scratch: fix probe to avoid unsafe

This commit is contained in:
code-yeongyu 2026-08-06 19:03:27 +09:00
parent 5bcc43b081
commit 39b3821cd5
1 changed files with 14 additions and 3 deletions

View File

@ -1,4 +1,4 @@
//! Scratch probe: dump GitHub runner unshare semantics (temporary, PR will be closed).
//! Scratch probe: dump GitHub runner unshare semantics (temporary, PRs will be closed).
#![cfg(target_os = "linux")]
use std::process::Command;
@ -15,11 +15,18 @@ fn run(args: &[&str]) -> (i32, String, String) {
}
}
fn sh(cmd: &str) -> String {
Command::new("sh")
.args(["-lc", cmd])
.output()
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.unwrap_or_default()
}
#[test]
fn dump_unshare_semantics() {
let uid = unsafe { libc::getuid() };
let mut report = String::new();
report.push_str(&format!("uid={uid} euid={}\n", unsafe { libc::geteuid() }));
report.push_str(&format!("uid line: {}\n", sh("id")));
for f in ["/etc/subuid", "/etc/subgid"] {
report.push_str(&format!("--- {f} ---\n"));
if let Ok(s) = std::fs::read_to_string(f) {
@ -42,6 +49,10 @@ fn dump_unshare_semantics() {
"auto-full",
&["--user", "--map-root-user", "--map-auto", "--mount", "--ipc", "--pid", "--uts", "--fork", "sh", "-lc", "echo alpha"][..],
),
(
"auto-full-echo-multi",
&["--user", "--map-root-user", "--map-auto", "--mount", "--ipc", "--pid", "--uts", "--fork", "sh", "-lc", "echo alpha from bash"][..],
),
] {
let (rc, so, se) = run(args);
report.push_str(&format!("[{name}] rc={rc} stdout={so:?} stderr={se:?}\n"));