mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
feat(linux-sandbox): activate managed DNS resolution
Co-authored-by: Codex noreply@openai.com
This commit is contained in:
co-authored by
Codex noreply@openai.com
parent
a44b6fea62
commit
7e6915f1db
@@ -6,6 +6,7 @@ use codex_protocol::config_types::ShellEnvironmentPolicy;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
use std::io::Write;
|
||||
use std::net::Ipv4Addr;
|
||||
@@ -59,6 +60,18 @@ fn is_bwrap_unavailable_output(output: &Output) -> bool {
|
||||
String::from_utf8_lossy(&output.stderr).contains(BWRAP_UNAVAILABLE_ERR)
|
||||
}
|
||||
|
||||
async fn command_available(program: &str) -> bool {
|
||||
matches!(
|
||||
Command::new(program)
|
||||
.arg("--version")
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
.await,
|
||||
Ok(status) if status.success()
|
||||
)
|
||||
}
|
||||
|
||||
async fn should_skip_bwrap_tests() -> bool {
|
||||
let mut env = create_env_from_core_vars();
|
||||
strip_proxy_env(&mut env);
|
||||
@@ -67,6 +80,7 @@ async fn should_skip_bwrap_tests() -> bool {
|
||||
&["bash", "-c", "true"],
|
||||
&PermissionProfile::read_only(),
|
||||
/*allow_network_for_proxy*/ false,
|
||||
/*dns_domain_policy*/ None,
|
||||
env,
|
||||
NETWORK_TIMEOUT_MS,
|
||||
)
|
||||
@@ -93,6 +107,7 @@ async fn managed_proxy_skip_reason() -> Option<String> {
|
||||
&["bash", "-c", "true"],
|
||||
&PermissionProfile::Disabled,
|
||||
/*allow_network_for_proxy*/ true,
|
||||
/*dns_domain_policy*/ None,
|
||||
env,
|
||||
NETWORK_TIMEOUT_MS,
|
||||
)
|
||||
@@ -116,6 +131,7 @@ async fn run_linux_sandbox_direct(
|
||||
command: &[&str],
|
||||
permission_profile: &PermissionProfile,
|
||||
allow_network_for_proxy: bool,
|
||||
dns_domain_policy: Option<&str>,
|
||||
env: HashMap<String, String>,
|
||||
timeout_ms: u64,
|
||||
) -> Output {
|
||||
@@ -132,6 +148,9 @@ async fn run_linux_sandbox_direct(
|
||||
if allow_network_for_proxy {
|
||||
args.push("--allow-network-for-proxy".to_string());
|
||||
}
|
||||
if let Some(policy) = dns_domain_policy {
|
||||
args.extend(["--dns-domain-policy".to_string(), policy.to_string()]);
|
||||
}
|
||||
args.push("--".to_string());
|
||||
args.extend(command.iter().map(|entry| (*entry).to_string()));
|
||||
|
||||
@@ -163,6 +182,7 @@ async fn managed_proxy_mode_fails_closed_without_proxy_env() {
|
||||
&["bash", "-c", "true"],
|
||||
&PermissionProfile::Disabled,
|
||||
/*allow_network_for_proxy*/ true,
|
||||
/*dns_domain_policy*/ None,
|
||||
env,
|
||||
NETWORK_TIMEOUT_MS,
|
||||
)
|
||||
@@ -218,6 +238,7 @@ async fn managed_proxy_mode_routes_through_bridge_and_blocks_direct_egress() {
|
||||
],
|
||||
&PermissionProfile::Disabled,
|
||||
/*allow_network_for_proxy*/ true,
|
||||
/*dns_domain_policy*/ None,
|
||||
env.clone(),
|
||||
NETWORK_TIMEOUT_MS,
|
||||
)
|
||||
@@ -249,6 +270,7 @@ async fn managed_proxy_mode_routes_through_bridge_and_blocks_direct_egress() {
|
||||
&["bash", "-c", "echo hi > /dev/tcp/192.0.2.1/80"],
|
||||
&PermissionProfile::Disabled,
|
||||
/*allow_network_for_proxy*/ true,
|
||||
/*dns_domain_policy*/ None,
|
||||
env,
|
||||
NETWORK_TIMEOUT_MS,
|
||||
)
|
||||
@@ -263,14 +285,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;
|
||||
}
|
||||
@@ -287,6 +302,7 @@ async fn managed_proxy_mode_denies_af_unix_socket_but_allows_socketpair() {
|
||||
],
|
||||
&PermissionProfile::Disabled,
|
||||
/*allow_network_for_proxy*/ true,
|
||||
/*dns_domain_policy*/ None,
|
||||
env,
|
||||
NETWORK_TIMEOUT_MS,
|
||||
)
|
||||
@@ -301,3 +317,198 @@ async fn managed_proxy_mode_denies_af_unix_socket_but_allows_socketpair() {
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn managed_dns_applies_domain_policy_and_drops_setup_capabilities() {
|
||||
if managed_proxy_skip_reason().await.is_some()
|
||||
|| !command_available("python3").await
|
||||
|| !command_available("cc").await
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let temp = tempfile::tempdir().expect("temporary loader-hook directory");
|
||||
let source = temp.path().join("cap_hook.c");
|
||||
let library = temp.path().join("cap_hook.so");
|
||||
let hook_log = temp.path().join("cap_hook.log");
|
||||
fs::write(
|
||||
&source,
|
||||
r#"#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
typedef int (*getaddrinfo_fn)(const char *, const char *, const struct addrinfo *, struct addrinfo **);
|
||||
static int dns_name_equal(const char *node, const char *fixture) {
|
||||
size_t fixture_len;
|
||||
if (!node || !fixture) return 0;
|
||||
fixture_len = strlen(fixture);
|
||||
return !strcmp(node, fixture) ||
|
||||
(!strncmp(node, fixture, fixture_len) && node[fixture_len] == '.' && !node[fixture_len + 1]);
|
||||
}
|
||||
static const char *resolver_fixture_address(const char *node) {
|
||||
const char *fixture4 = getenv("DNS_RESOLVER_FIXTURE4");
|
||||
const char *fixture6 = getenv("DNS_RESOLVER_FIXTURE6");
|
||||
const char *denied_canonical = getenv("DNS_RESOLVER_FIXTURE_DENIED_CANONICAL");
|
||||
char exe[4096];
|
||||
ssize_t len;
|
||||
if (!dns_name_equal(node, fixture4) && !dns_name_equal(node, fixture6) &&
|
||||
!dns_name_equal(node, denied_canonical)) return NULL;
|
||||
len = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
|
||||
if (len < 0) return NULL;
|
||||
exe[len] = 0;
|
||||
if (!strstr(exe, "codex-linux-sandbox")) return NULL;
|
||||
if (dns_name_equal(node, fixture4)) return "192.0.2.53";
|
||||
if (dns_name_equal(node, fixture6)) return "2001:db8::53";
|
||||
return "192.0.2.54";
|
||||
}
|
||||
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
|
||||
getaddrinfo_fn real = (getaddrinfo_fn)dlsym(RTLD_NEXT, "getaddrinfo");
|
||||
const char *address = resolver_fixture_address(node);
|
||||
struct addrinfo numeric = {0};
|
||||
if (!real) return EAI_SYSTEM;
|
||||
if (!address) return real(node, service, hints, res);
|
||||
if (hints) {
|
||||
numeric.ai_socktype = hints->ai_socktype;
|
||||
numeric.ai_protocol = hints->ai_protocol;
|
||||
}
|
||||
numeric.ai_flags = AI_NUMERICHOST;
|
||||
numeric.ai_family = strchr(address, ':') ? AF_INET6 : AF_INET;
|
||||
int status = real(address, service, &numeric, res);
|
||||
if (!status && hints && (hints->ai_flags & AI_CANONNAME) && *res) {
|
||||
const char *canonical = dns_name_equal(node, getenv("DNS_RESOLVER_FIXTURE_DENIED_CANONICAL"))
|
||||
? "blocked.fixture.test"
|
||||
: (strchr(address, ':') ? "canonical6.fixture.test" : "canonical.fixture.test");
|
||||
char *copy = strdup(canonical);
|
||||
if (!copy) { freeaddrinfo(*res); *res = NULL; return EAI_MEMORY; }
|
||||
(*res)->ai_canonname = copy;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
__attribute__((constructor)) static void record_caps(void) {
|
||||
const char *path = getenv("DNS_CAP_LOG");
|
||||
if (!path) return;
|
||||
FILE *status = fopen("/proc/self/status", "r");
|
||||
char line[256], exe[4096];
|
||||
unsigned long long caps = 0;
|
||||
while (status && fgets(line, sizeof(line), status))
|
||||
if (!strncmp(line, "Cap", 3)) caps |= strtoull(strchr(line, '\t') + 1, NULL, 16);
|
||||
if (status) fclose(status);
|
||||
ssize_t len = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
|
||||
if (len < 0) return;
|
||||
exe[len] = 0;
|
||||
FILE *out = fopen(path, "a");
|
||||
if (out) { fprintf(out, "%s %llx\n", exe, caps); fclose(out); }
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.expect("write loader hook");
|
||||
let build = Command::new("cc")
|
||||
.args(["-shared", "-fPIC", "-o"])
|
||||
.arg(&library)
|
||||
.arg(&source)
|
||||
.arg("-ldl")
|
||||
.output()
|
||||
.await
|
||||
.expect("compile loader hook");
|
||||
assert!(
|
||||
build.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&build.stderr)
|
||||
);
|
||||
|
||||
let mut env = create_env_from_core_vars();
|
||||
strip_proxy_env(&mut env);
|
||||
env.insert("HTTP_PROXY".to_string(), "http://127.0.0.1:9".to_string());
|
||||
env.insert("LD_PRELOAD".to_string(), library.display().to_string());
|
||||
env.insert("DNS_CAP_LOG".to_string(), hook_log.display().to_string());
|
||||
env.insert(
|
||||
"DNS_RESOLVER_FIXTURE4".to_string(),
|
||||
"resolver.fixture.test".to_string(),
|
||||
);
|
||||
env.insert(
|
||||
"DNS_RESOLVER_FIXTURE6".to_string(),
|
||||
"resolver6.fixture.test".to_string(),
|
||||
);
|
||||
env.insert(
|
||||
"DNS_RESOLVER_FIXTURE_DENIED_CANONICAL".to_string(),
|
||||
"denied-canonical.fixture.test".to_string(),
|
||||
);
|
||||
let policy = r#"{"allowedDomains":["localhost","**.fixture.test"],"deniedDomains":["blocked.fixture.test"]}"#;
|
||||
let script = r#"
|
||||
import socket, struct
|
||||
assert open('/etc/resolv.conf').read() == 'nameserver 127.0.0.1\n'
|
||||
def skip_name(wire, offset):
|
||||
while True:
|
||||
size = wire[offset]
|
||||
offset += 1
|
||||
if size == 0: return offset
|
||||
if size & 0xc0 == 0xc0: return offset + 1
|
||||
offset += size
|
||||
def query(name, qtype=1, tcp=False):
|
||||
labels = b''.join(bytes([len(part)]) + part.encode() for part in name.split('.')) + b'\0'
|
||||
wire = struct.pack('!HHHHHH', 1, 0x100, 1, 0, 0, 0) + labels + struct.pack('!HH', qtype, 1)
|
||||
if tcp:
|
||||
with socket.create_connection(('127.0.0.1', 53)) as sock:
|
||||
stream = sock.makefile('rwb')
|
||||
stream.write(struct.pack('!H', len(wire)) + wire); stream.flush()
|
||||
size = struct.unpack('!H', stream.read(2))[0]
|
||||
reply = stream.read(size)
|
||||
assert len(reply) == size
|
||||
else:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
||||
sock.sendto(wire, ('127.0.0.1', 53)); reply = sock.recv(65535)
|
||||
question_count, answer_count = struct.unpack('!HH', reply[4:8])
|
||||
offset = 12
|
||||
for _ in range(question_count): offset = skip_name(reply, offset) + 4
|
||||
answer_types = []
|
||||
for _ in range(answer_count):
|
||||
offset = skip_name(reply, offset)
|
||||
answer_types.append(struct.unpack('!H', reply[offset:offset + 2])[0])
|
||||
data_length = struct.unpack('!H', reply[offset + 8:offset + 10])[0]
|
||||
offset += 10 + data_length
|
||||
return reply[3] & 15, answer_types
|
||||
assert query('resolver.fixture.test') == (0, [5, 1])
|
||||
assert query('resolver6.fixture.test', qtype=28) == (0, [5, 28])
|
||||
assert query('resolver.fixture.test', qtype=5) == (0, [5])
|
||||
assert query('resolver.fixture.test', tcp=True) == (0, [5, 1])
|
||||
assert query('denied-canonical.fixture.test') == (0, [1])
|
||||
assert query('denied-canonical.fixture.test', qtype=5) == (5, [])
|
||||
assert query('blocked.fixture.test') == (5, [])
|
||||
resolved = socket.getaddrinfo('resolver.fixture.test', 0, socket.AF_INET, 0, 0, socket.AI_CANONNAME)
|
||||
assert {entry[4][0] for entry in resolved} == {'192.0.2.53'}
|
||||
assert resolved[0][3] == 'canonical.fixture.test'
|
||||
try: socket.getaddrinfo('blocked.fixture.test', 0)
|
||||
except socket.gaierror: pass
|
||||
else: raise AssertionError('denied name resolved')
|
||||
with open('/proc/self/status') as status:
|
||||
caps = [int(line.split()[1], 16) for line in status if line.startswith(('CapInh:', 'CapPrm:', 'CapEff:', 'CapBnd:', 'CapAmb:'))]
|
||||
assert caps == [0] * 5, caps
|
||||
"#;
|
||||
let output = run_linux_sandbox_direct(
|
||||
&["python3", "-c", script],
|
||||
&PermissionProfile::Disabled,
|
||||
/*allow_network_for_proxy*/ true,
|
||||
/*dns_domain_policy*/ Some(policy),
|
||||
env,
|
||||
NETWORK_TIMEOUT_MS,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"expected policy-checked DNS with no residual capabilities: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let hook_log = fs::read_to_string(&hook_log).expect("read loader-hook log");
|
||||
assert!(
|
||||
hook_log.lines().any(|line| line.contains("python")),
|
||||
"{hook_log}"
|
||||
);
|
||||
assert!(
|
||||
hook_log.lines().all(|line| line.ends_with(" 0")),
|
||||
"loader hook ran with capabilities: {hook_log}"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user