fix(linux-sandbox): simplify dns setup edges

Co-authored-by: Codex noreply@openai.com
This commit is contained in:
viyatb-oai
2026-07-09 12:38:29 -07:00
co-authored by Codex noreply@openai.com
parent f28265ca42
commit ae481c4a89
4 changed files with 30 additions and 23 deletions
+5 -15
View File
@@ -9,6 +9,7 @@ use rustix::thread::capability_is_in_bounding_set;
use rustix::thread::clear_ambient_capability_set;
use rustix::thread::remove_capability_from_bounding_set;
use rustix::thread::set_capabilities;
use std::ffi::OsString;
use std::fs::File;
use std::io;
use std::io::Seek;
@@ -16,13 +17,14 @@ use std::io::SeekFrom;
use std::io::Write;
use std::os::fd::AsRawFd;
use std::os::fd::FromRawFd;
use std::os::unix::ffi::OsStringExt;
use std::path::Path;
use std::path::PathBuf;
const RESOLV_CONF_PATH: &str = "/etc/resolv.conf";
const LOADER_ENV_KEYS: &[&str] = &["LD_AUDIT", "LD_LIBRARY_PATH", "LD_PRELOAD"];
pub(crate) type LoaderEnvironment = Vec<(String, String)>;
pub(crate) type LoaderEnvironment = Vec<(Vec<u8>, Vec<u8>)>;
#[derive(Debug)]
pub(crate) struct ResolvConfMount {
@@ -73,14 +75,7 @@ pub(crate) fn capture_loader_environment() -> LoaderEnvironment {
LOADER_ENV_KEYS
.iter()
.filter_map(|&key| {
std::env::var_os(key).map(|value| {
(
key.to_string(),
value
.into_string()
.unwrap_or_else(|_| panic!("{key} must contain valid UTF-8")),
)
})
std::env::var_os(key).map(|value| (key.as_bytes().to_vec(), value.into_vec()))
})
.collect()
}
@@ -88,7 +83,7 @@ pub(crate) fn capture_loader_environment() -> LoaderEnvironment {
pub(crate) fn restore_loader_environment(environment: LoaderEnvironment) {
for (key, value) in environment {
// SAFETY: the setup process is single-threaded and has dropped all capabilities.
unsafe { std::env::set_var(key, value) };
unsafe { std::env::set_var(OsString::from_vec(key), OsString::from_vec(value)) };
}
}
@@ -98,9 +93,6 @@ pub(crate) fn append_bwrap_args(bwrap_args: &mut BwrapArgs, mount: ResolvConfMou
.iter()
.position(|arg| arg == "--")
.unwrap_or_else(|| panic!("bubblewrap argv is missing command separator '--'"));
let Some(parent) = mount.path.parent() else {
panic!("resolver configuration path has no parent");
};
let fd = mount.file.as_raw_fd().to_string();
bwrap_args.args.splice(
separator..separator,
@@ -114,8 +106,6 @@ pub(crate) fn append_bwrap_args(bwrap_args: &mut BwrapArgs, mount: ResolvConfMou
"CAP_NET_BIND_SERVICE".to_string(),
"--cap-add".to_string(),
"CAP_SETPCAP".to_string(),
"--dir".to_string(),
parent.to_string_lossy().into_owned(),
"--perms".to_string(),
"444".to_string(),
"--ro-bind-data".to_string(),
@@ -41,6 +41,10 @@ fn unsets_loader_environment_before_inner_command() {
"{key} must be unset before the privileged DNS setup command"
);
}
assert!(
!setup_args.windows(2).any(|args| args == ["--dir", "/etc"]),
"DNS setup must not shadow an existing resolver parent directory"
);
}
#[test]
@@ -476,6 +476,17 @@ fn managed_proxy_inner_command_includes_route_spec() {
assert!(args.iter().any(|arg| arg == "{\"routes\":[]}"));
}
#[test]
fn loader_environment_round_trips_non_utf8_values() {
let environment = vec![(b"LD_PRELOAD".to_vec(), b"\xffloader.so".to_vec())];
assert_eq!(
parse_loader_environment(Some(&serialize_loader_environment(&environment)))
.expect("loader environment should deserialize"),
Some(environment)
);
}
#[test]
fn inner_command_includes_permission_profile_flag() {
let permission_profile = read_only_permission_profile();
@@ -120,6 +120,15 @@ async fn managed_proxy_skip_reason() -> Option<String> {
None
}
async fn command_available(command: &str) -> bool {
Command::new("bash")
.args(["-c", "command -v \"$1\" >/dev/null", "--", command])
.status()
.await
.expect("command probe should execute")
.success()
}
async fn run_linux_sandbox_direct(
command: &[&str],
permission_profile: &PermissionProfile,
@@ -271,14 +280,7 @@ async fn managed_proxy_mode_denies_af_unix_socket_but_allows_socketpair() {
return;
}
let python_available = Command::new("bash")
.arg("-c")
.arg("command -v python3 >/dev/null")
.status()
.await
.expect("python3 probe should execute")
.success();
if !python_available {
if !command_available("python3").await {
eprintln!("skipping managed proxy AF_UNIX test: python3 is unavailable");
return;
}