Repository: openai/codex
File: codex-rs/linux-sandbox/src/linux_run_main.rs (Rust)

Code before:
    }
    exec_or_panic(command);
}

fn run_bwrap_with_proc_fallback(
    sandbox_policy_cwd: &Path,
    sandbox_policy: &codex_core::protocol::SandboxPolicy,
    inner: Vec<String>,
    mount_proc: bool,
    allow_network_for_proxy: bool,
) -> ! {
    let mut mount_proc = mount_proc;
    let mut network_mode = bwrap_network_mode(sandbox_policy, allow_network_for_proxy);

    if mount_proc && !preflight_proc_mount_support(sandbox_policy_cwd, sandbox_policy) {
        eprintln!("codex-linux-sandbox: bwrap could not mount /proc; retrying with --no-proc");
        mount_proc = false;
    }

    if network_mode != BwrapNetworkMode::FullAccess
        && !preflight_unshare_net_support(sandbox_policy_cwd, sandbox_policy, mount_proc)
    {
        eprintln!(
            "codex-linux-sandbox: bwrap could not configure loopback in a network namespace; retrying without --unshare-net"
        );
        network_mode = BwrapNetworkMode::FullAccess;
    }

    let options = BwrapOptions {
        mount_proc,
        network_mode,
    };
    let argv = build_bwrap_argv(inner, sandbox_policy, sandbox_policy_cwd, options);
    exec_vendored_bwrap(argv);
}

fn bwrap_network_mode(
    sandbox_policy: &codex_core::protocol::SandboxPolicy,
    allow_network_for_proxy: bool,
) -> BwrapNetworkMode {
    if allow_network_for_proxy {
        BwrapNetworkMode::ProxyOnly
    } else if sandbox_policy.has_full_network_access() {
        BwrapNetworkMode::FullAccess
    } else {
        BwrapNetworkMode::Isolated
    }
}

fn build_bwrap_argv(
    inner: Vec<String>,

Review comment (bug):
Is this meant to be part of this PR? I don't think we should be silently upgrading to `FullAccess` for `network_mode`: we should just fail?

Code after:
    }
    exec_or_panic(command);
}

fn run_bwrap_with_proc_fallback(
    sandbox_policy_cwd: &Path,
    sandbox_policy: &codex_core::protocol::SandboxPolicy,
    inner: Vec<String>,
    mount_proc: bool,
    allow_network_for_proxy: bool,
) -> ! {
    let mut mount_proc = mount_proc;

    if mount_proc && !preflight_proc_mount_support(sandbox_policy_cwd, sandbox_policy) {
        eprintln!("codex-linux-sandbox: bwrap could not mount /proc; retrying with --no-proc");
        mount_proc = false;
    }

    let network_mode = bwrap_network_mode(sandbox_policy, allow_network_for_proxy);
    let options = BwrapOptions {
        mount_proc,
        network_mode,
    };
    let argv = build_bwrap_argv(inner, sandbox_policy, sandbox_policy_cwd, options);
    exec_vendored_bwrap(argv);
}

fn bwrap_network_mode(
    sandbox_policy: &codex_core::protocol::SandboxPolicy,
    allow_network_for_proxy: bool,
) -> BwrapNetworkMode {
    if allow_network_for_proxy {
        BwrapNetworkMode::ProxyOnly
    } else if sandbox_policy.has_full_network_access() {
        BwrapNetworkMode::FullAccess
    } else {
        BwrapNetworkMode::Isolated
    }
}

fn build_bwrap_argv(
    inner: Vec<String>,
    sandbox_policy: &codex_core::protocol::SandboxPolicy,
    sandbox_policy_cwd: &Path,
    options: BwrapOptions,
) -> Vec<String> {
    let mut args = create_bwrap_command_args(inner, sandbox_policy, sandbox_policy_cwd, options)
        .unwrap_or_else(|err| panic!("error building bubblewrap command: {err:?}"));

    let command_separator_index = args
        .iter()