mirror of
https://github.com/openai/codex.git
synced 2026-11-20 23:08:21 +00:00
Drop capabilities from Linux sandbox processes (#39103)
## What changed - Pass `--cap-drop ALL` in both bubblewrap launch modes. - Verify in the inner sandbox stage that the effective and permitted Linux capability sets are empty, and abort before executing the command otherwise. - Cover the generated bubblewrap arguments, capability state of sandboxed commands, and rejection of a capability-bearing inner-stage process. GitOrigin-RevId: 9dbd651d306d92327e36c9b3f308619db7d80a67
This commit is contained in:
1 parent
2eee483e49
commit
632420e67a
4 files changed
+107
No files matched your search
@@ -290,6 +290,8 @@ fn create_bwrap_flags_full_filesystem(command: Vec<String>, options: BwrapOption
|
|||||||
args.push("--proc".to_string());
|
args.push("--proc".to_string());
|
||||||
args.push("/proc".to_string());
|
args.push("/proc".to_string());
|
||||||
}
|
}
|
||||||
|
args.push("--cap-drop".to_string());
|
||||||
|
args.push("ALL".to_string());
|
||||||
args.push("--".to_string());
|
args.push("--".to_string());
|
||||||
args.extend(command);
|
args.extend(command);
|
||||||
BwrapArgs {
|
BwrapArgs {
|
||||||
@@ -345,6 +347,8 @@ fn create_bwrap_flags(
|
|||||||
args.push("--chdir".to_string());
|
args.push("--chdir".to_string());
|
||||||
args.push(path_to_string(normalized_command_cwd.as_path()));
|
args.push(path_to_string(normalized_command_cwd.as_path()));
|
||||||
}
|
}
|
||||||
|
args.push("--cap-drop".to_string());
|
||||||
|
args.push("ALL".to_string());
|
||||||
args.push("--".to_string());
|
args.push("--".to_string());
|
||||||
args.extend(command);
|
args.extend(command);
|
||||||
Ok(BwrapArgs {
|
Ok(BwrapArgs {
|
||||||
@@ -1421,6 +1425,8 @@ mod tests {
|
|||||||
"--unshare-net".to_string(),
|
"--unshare-net".to_string(),
|
||||||
"--proc".to_string(),
|
"--proc".to_string(),
|
||||||
"/proc".to_string(),
|
"/proc".to_string(),
|
||||||
|
"--cap-drop".to_string(),
|
||||||
|
"ALL".to_string(),
|
||||||
"--".to_string(),
|
"--".to_string(),
|
||||||
"/bin/true".to_string(),
|
"/bin/true".to_string(),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ static PENDING_FORWARDED_SIGNAL: AtomicI32 = AtomicI32::new(0);
|
|||||||
|
|
||||||
const FORWARDED_SIGNALS: &[libc::c_int] =
|
const FORWARDED_SIGNALS: &[libc::c_int] =
|
||||||
&[libc::SIGHUP, libc::SIGINT, libc::SIGQUIT, libc::SIGTERM];
|
&[libc::SIGHUP, libc::SIGINT, libc::SIGQUIT, libc::SIGTERM];
|
||||||
|
const LINUX_CAPABILITY_VERSION_3: u32 = 0x2008_0522;
|
||||||
const SYNTHETIC_MOUNT_MARKER_SYNTHETIC: &[u8] = b"synthetic\n";
|
const SYNTHETIC_MOUNT_MARKER_SYNTHETIC: &[u8] = b"synthetic\n";
|
||||||
const SYNTHETIC_MOUNT_MARKER_EXISTING: &[u8] = b"existing\n";
|
const SYNTHETIC_MOUNT_MARKER_EXISTING: &[u8] = b"existing\n";
|
||||||
const PROTECTED_CREATE_MARKER: &[u8] = b"protected-create\n";
|
const PROTECTED_CREATE_MARKER: &[u8] = b"protected-create\n";
|
||||||
@@ -180,6 +181,30 @@ pub fn run_main() -> ! {
|
|||||||
// Inner stage: apply seccomp/no_new_privs after bubblewrap has already
|
// Inner stage: apply seccomp/no_new_privs after bubblewrap has already
|
||||||
// established the filesystem view.
|
// established the filesystem view.
|
||||||
if apply_seccomp_then_exec {
|
if apply_seccomp_then_exec {
|
||||||
|
let mut capability_header = [LINUX_CAPABILITY_VERSION_3, 0];
|
||||||
|
let mut capability_sets = [[0_u32; 3]; 2];
|
||||||
|
// SAFETY: capability ABI version 3 uses a [version, pid] header and
|
||||||
|
// two [effective, permitted, inheritable] capability-set entries.
|
||||||
|
let result = unsafe {
|
||||||
|
libc::syscall(
|
||||||
|
libc::SYS_capget,
|
||||||
|
capability_header.as_mut_ptr(),
|
||||||
|
capability_sets.as_mut_ptr(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if result < 0 {
|
||||||
|
panic!(
|
||||||
|
"failed to verify Linux sandbox capabilities: {}",
|
||||||
|
std::io::Error::last_os_error()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if capability_sets
|
||||||
|
.into_iter()
|
||||||
|
.any(|[effective, permitted, _]| effective != 0 || permitted != 0)
|
||||||
|
{
|
||||||
|
panic!("Linux sandbox retained effective or permitted capabilities");
|
||||||
|
}
|
||||||
|
|
||||||
if allow_network_for_proxy {
|
if allow_network_for_proxy {
|
||||||
let spec = proxy_route_spec
|
let spec = proxy_route_spec
|
||||||
.as_deref()
|
.as_deref()
|
||||||
|
|||||||
@@ -83,6 +83,8 @@ fn inserts_bwrap_argv0_before_command_separator() {
|
|||||||
"--unshare-pid".to_string(),
|
"--unshare-pid".to_string(),
|
||||||
"--proc".to_string(),
|
"--proc".to_string(),
|
||||||
"/proc".to_string(),
|
"/proc".to_string(),
|
||||||
|
"--cap-drop".to_string(),
|
||||||
|
"ALL".to_string(),
|
||||||
"--argv0".to_string(),
|
"--argv0".to_string(),
|
||||||
"codex-linux-sandbox".to_string(),
|
"codex-linux-sandbox".to_string(),
|
||||||
"--".to_string(),
|
"--".to_string(),
|
||||||
|
|||||||
@@ -417,6 +417,80 @@ async fn test_no_new_privs_is_enabled() {
|
|||||||
assert_eq!(line.trim(), "NoNewPrivs:\t1");
|
assert_eq!(line.trim(), "NoNewPrivs:\t1");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn sandboxed_command_has_no_effective_or_permitted_capabilities() {
|
||||||
|
if should_skip_bwrap_tests().await {
|
||||||
|
eprintln!("skipping bwrap test: bwrap sandbox prerequisites are unavailable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = run_cmd_output(
|
||||||
|
&[
|
||||||
|
"bash",
|
||||||
|
"-lc",
|
||||||
|
"awk '$1 == \"CapPrm:\" || $1 == \"CapEff:\" { print $1, $2 }' /proc/self/status",
|
||||||
|
],
|
||||||
|
&[],
|
||||||
|
LONG_TIMEOUT_MS,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
output.stdout.text,
|
||||||
|
"CapPrm: 0000000000000000\nCapEff: 0000000000000000\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn sandbox_inner_stage_rejects_retained_capabilities() {
|
||||||
|
if should_skip_bwrap_tests().await {
|
||||||
|
eprintln!("skipping bwrap test: bwrap sandbox prerequisites are unavailable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let user_namespace_probe = match std::process::Command::new("unshare")
|
||||||
|
.args(["--user", "--map-root-user", "--", "/bin/true"])
|
||||||
|
.output()
|
||||||
|
{
|
||||||
|
Ok(output) => output,
|
||||||
|
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
|
||||||
|
eprintln!("skipping capability test: unshare is unavailable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Err(err) => panic!("failed to probe unprivileged user namespaces: {err}"),
|
||||||
|
};
|
||||||
|
if !user_namespace_probe.status.success() {
|
||||||
|
eprintln!("skipping capability test: unprivileged user namespaces are unavailable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let permission_profile = serde_json::to_string(&PermissionProfile::read_only())
|
||||||
|
.expect("read-only permission profile should serialize");
|
||||||
|
let output = std::process::Command::new("unshare")
|
||||||
|
.args(["--user", "--map-root-user", "--"])
|
||||||
|
.arg(codex_linux_sandbox_exe())
|
||||||
|
.args(["--sandbox-policy-cwd", "/", "--permission-profile"])
|
||||||
|
.arg(permission_profile)
|
||||||
|
.args([
|
||||||
|
"--apply-seccomp-then-exec",
|
||||||
|
"--",
|
||||||
|
"/bin/sh",
|
||||||
|
"-c",
|
||||||
|
"printf command-ran",
|
||||||
|
])
|
||||||
|
.output()
|
||||||
|
.expect("capability-bearing sandbox helper should execute");
|
||||||
|
|
||||||
|
assert!(!output.status.success());
|
||||||
|
assert_eq!(output.stdout, Vec::<u8>::new());
|
||||||
|
assert!(
|
||||||
|
String::from_utf8_lossy(&output.stderr)
|
||||||
|
.contains("Linux sandbox retained effective or permitted capabilities"),
|
||||||
|
"unexpected stderr: {}",
|
||||||
|
String::from_utf8_lossy(&output.stderr)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
#[should_panic(expected = "Sandbox(Timeout")]
|
#[should_panic(expected = "Sandbox(Timeout")]
|
||||||
async fn test_timeout() {
|
async fn test_timeout() {
|
||||||
|
|||||||
Reference in new issue
Block a user