mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Block non-loopback inbound traffic for the Windows offline sandbox (#44639)
## Why The offline sandbox's non-loopback firewall block covered only outbound traffic. Add a matching inbound block for the offline sandbox user. ## What changed - Configure an inbound firewall rule scoped to the offline user's SID, blocking all protocols and ports for non-loopback remote addresses. - Make firewall rule direction explicit while preserving the existing outbound rules. - Remove the new inbound rule during uninstall. ## Testing Extend the elevated Windows sandbox integration test to inspect effective inbound and outbound rules, asserting their uniqueness, direction, block action, enabled state, profiles, address and port filters, protocol, and user scope. GitOrigin-RevId: cbc1ca0d8d08a31fc53da5ad5be6ee5d51afa63d
This commit is contained in:
1 parent
8e2afc0912
commit
e25bedc166
4 files changed
+104
-4
No files matched your search
@@ -438,6 +438,78 @@ async fn windows_elevated_does_not_create_missing_workspace_metadata() -> anyhow
|
|||||||
path.display()
|
path.display()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let firewall_output = std::process::Command::new("powershell.exe")
|
||||||
|
.args([
|
||||||
|
"-NoProfile",
|
||||||
|
"-NonInteractive",
|
||||||
|
"-Command",
|
||||||
|
r#"
|
||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
$sid = (Get-LocalUser -Name 'CodexSandboxOffline').SID.Value
|
||||||
|
$rules = foreach ($name in @('codex_sandbox_offline_block_inbound', 'codex_sandbox_offline_block_outbound')) {
|
||||||
|
$rule = @(Get-NetFirewallRule -PolicyStore ActiveStore -DisplayName $name)
|
||||||
|
if ($rule.Count -ne 1) { throw "Expected exactly one effective rule for $name" }
|
||||||
|
$address = $rule | Get-NetFirewallAddressFilter
|
||||||
|
$security = $rule | Get-NetFirewallSecurityFilter
|
||||||
|
$port = $rule | Get-NetFirewallPortFilter
|
||||||
|
[pscustomobject]@{
|
||||||
|
name = $name
|
||||||
|
direction = [string]$rule[0].Direction
|
||||||
|
action = [string]$rule[0].Action
|
||||||
|
enabled = [string]$rule[0].Enabled
|
||||||
|
profile = [string]$rule[0].Profile
|
||||||
|
protocol = [string]$port.Protocol
|
||||||
|
localAddresses = @($address.LocalAddress)
|
||||||
|
remoteAddresses = @($address.RemoteAddress | Sort-Object)
|
||||||
|
localPorts = @($port.LocalPort)
|
||||||
|
remotePorts = @($port.RemotePort)
|
||||||
|
localUser = [string]$security.LocalUser
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[pscustomobject]@{ offlineSid = $sid; rules = @($rules) } | ConvertTo-Json -Depth 4 -Compress
|
||||||
|
"#,
|
||||||
|
])
|
||||||
|
.output()
|
||||||
|
.context("read offline sandbox firewall rules")?;
|
||||||
|
assert!(
|
||||||
|
firewall_output.status.success(),
|
||||||
|
"read offline sandbox firewall rules: {}",
|
||||||
|
String::from_utf8_lossy(&firewall_output.stderr)
|
||||||
|
);
|
||||||
|
let firewall_rules: serde_json::Value = serde_json::from_slice(&firewall_output.stdout)
|
||||||
|
.context("parse offline sandbox firewall rules")?;
|
||||||
|
let offline_sid = firewall_rules["offlineSid"]
|
||||||
|
.as_str()
|
||||||
|
.context("offline sandbox firewall rules should include their user SID")?;
|
||||||
|
let local_user = format!("O:LSD:(A;;CC;;;{offline_sid})");
|
||||||
|
let expected_rules: Vec<_> = [
|
||||||
|
("codex_sandbox_offline_block_inbound", "Inbound"),
|
||||||
|
("codex_sandbox_offline_block_outbound", "Outbound"),
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.map(|(name, direction)| {
|
||||||
|
serde_json::json!({
|
||||||
|
"name": name,
|
||||||
|
"direction": direction,
|
||||||
|
"action": "Block",
|
||||||
|
"enabled": "True",
|
||||||
|
"profile": "Any",
|
||||||
|
"protocol": "Any",
|
||||||
|
"localAddresses": ["Any"],
|
||||||
|
"remoteAddresses": [
|
||||||
|
"::",
|
||||||
|
"::2-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
|
||||||
|
"0.0.0.0-126.255.255.255",
|
||||||
|
"128.0.0.0-255.255.255.255",
|
||||||
|
],
|
||||||
|
"localPorts": ["Any"],
|
||||||
|
"remotePorts": ["Any"],
|
||||||
|
"localUser": local_user,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
assert_eq!(firewall_rules["rules"], serde_json::json!(expected_rules));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -750,14 +750,14 @@ fn configure_offline_sandbox_network(
|
|||||||
format!("ensure offline proxy allowlist failed: {err}"),
|
format!("ensure offline proxy allowlist failed: {err}"),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
let firewall_result = firewall::ensure_offline_outbound_block(offline_sid_str, log);
|
let firewall_result = firewall::ensure_offline_network_blocks(offline_sid_str, log);
|
||||||
if let Err(err) = firewall_result {
|
if let Err(err) = firewall_result {
|
||||||
if extract_setup_failure(&err).is_some() {
|
if extract_setup_failure(&err).is_some() {
|
||||||
return Err(err);
|
return Err(err);
|
||||||
}
|
}
|
||||||
return Err(anyhow::Error::new(SetupFailure::new(
|
return Err(anyhow::Error::new(SetupFailure::new(
|
||||||
SetupErrorCode::HelperFirewallRuleCreateOrAddFailed,
|
SetupErrorCode::HelperFirewallRuleCreateOrAddFailed,
|
||||||
format!("ensure offline outbound block failed: {err}"),
|
format!("ensure offline network blocks failed: {err}"),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_IP_PROTOCOL_UDP;
|
|||||||
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_MODIFY_STATE;
|
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_MODIFY_STATE;
|
||||||
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_MODIFY_STATE_OK;
|
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_MODIFY_STATE_OK;
|
||||||
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_PROFILE2_ALL;
|
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_PROFILE2_ALL;
|
||||||
|
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_RULE_DIR_IN;
|
||||||
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_RULE_DIR_OUT;
|
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_RULE_DIR_OUT;
|
||||||
|
use windows::Win32::NetworkManagement::WindowsFirewall::NET_FW_RULE_DIRECTION;
|
||||||
use windows::Win32::NetworkManagement::WindowsFirewall::NetFwPolicy2;
|
use windows::Win32::NetworkManagement::WindowsFirewall::NetFwPolicy2;
|
||||||
use windows::Win32::NetworkManagement::WindowsFirewall::NetFwRule;
|
use windows::Win32::NetworkManagement::WindowsFirewall::NetFwRule;
|
||||||
use windows::Win32::System::Com::CLSCTX_INPROC_SERVER;
|
use windows::Win32::System::Com::CLSCTX_INPROC_SERVER;
|
||||||
@@ -30,11 +32,14 @@ use codex_windows_sandbox::SetupFailure;
|
|||||||
// This is the stable identifier we use to find/update the rule idempotently.
|
// This is the stable identifier we use to find/update the rule idempotently.
|
||||||
// It intentionally does not change between installs.
|
// It intentionally does not change between installs.
|
||||||
const OFFLINE_BLOCK_RULE_NAME: &str = "codex_sandbox_offline_block_outbound";
|
const OFFLINE_BLOCK_RULE_NAME: &str = "codex_sandbox_offline_block_outbound";
|
||||||
|
const OFFLINE_BLOCK_INBOUND_RULE_NAME: &str = "codex_sandbox_offline_block_inbound";
|
||||||
const OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME: &str = "codex_sandbox_offline_block_loopback_tcp";
|
const OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME: &str = "codex_sandbox_offline_block_loopback_tcp";
|
||||||
const OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME: &str = "codex_sandbox_offline_block_loopback_udp";
|
const OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME: &str = "codex_sandbox_offline_block_loopback_udp";
|
||||||
|
|
||||||
// Friendly text shown in the firewall UI.
|
// Friendly text shown in the firewall UI.
|
||||||
const OFFLINE_BLOCK_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Non-Loopback Outbound";
|
const OFFLINE_BLOCK_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Non-Loopback Outbound";
|
||||||
|
const OFFLINE_BLOCK_INBOUND_RULE_FRIENDLY: &str =
|
||||||
|
"Codex Sandbox Offline - Block Non-Loopback Inbound";
|
||||||
const OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY: &str =
|
const OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY: &str =
|
||||||
"Codex Sandbox Offline - Block Loopback TCP (Except Proxy)";
|
"Codex Sandbox Offline - Block Loopback TCP (Except Proxy)";
|
||||||
const OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Loopback UDP";
|
const OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY: &str = "Codex Sandbox Offline - Block Loopback UDP";
|
||||||
@@ -45,6 +50,7 @@ const NON_LOOPBACK_REMOTE_ADDRESSES: &str = "0.0.0.0-126.255.255.255,128.0.0.0-2
|
|||||||
struct BlockRuleSpec<'a> {
|
struct BlockRuleSpec<'a> {
|
||||||
internal_name: &'a str,
|
internal_name: &'a str,
|
||||||
friendly_desc: &'a str,
|
friendly_desc: &'a str,
|
||||||
|
direction: NET_FW_RULE_DIRECTION,
|
||||||
protocol: i32,
|
protocol: i32,
|
||||||
local_user_spec: &'a str,
|
local_user_spec: &'a str,
|
||||||
offline_sid: &'a str,
|
offline_sid: &'a str,
|
||||||
@@ -99,6 +105,7 @@ pub fn ensure_offline_proxy_allowlist(
|
|||||||
&BlockRuleSpec {
|
&BlockRuleSpec {
|
||||||
internal_name: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME,
|
internal_name: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME,
|
||||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY,
|
friendly_desc: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY,
|
||||||
|
direction: NET_FW_RULE_DIR_OUT,
|
||||||
protocol: NET_FW_IP_PROTOCOL_UDP.0,
|
protocol: NET_FW_IP_PROTOCOL_UDP.0,
|
||||||
local_user_spec: &local_user_spec,
|
local_user_spec: &local_user_spec,
|
||||||
offline_sid,
|
offline_sid,
|
||||||
@@ -115,6 +122,7 @@ pub fn ensure_offline_proxy_allowlist(
|
|||||||
&BlockRuleSpec {
|
&BlockRuleSpec {
|
||||||
internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME,
|
internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME,
|
||||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY,
|
friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY,
|
||||||
|
direction: NET_FW_RULE_DIR_OUT,
|
||||||
protocol: NET_FW_IP_PROTOCOL_TCP.0,
|
protocol: NET_FW_IP_PROTOCOL_TCP.0,
|
||||||
local_user_spec: &local_user_spec,
|
local_user_spec: &local_user_spec,
|
||||||
offline_sid,
|
offline_sid,
|
||||||
@@ -134,6 +142,7 @@ pub fn ensure_offline_proxy_allowlist(
|
|||||||
&BlockRuleSpec {
|
&BlockRuleSpec {
|
||||||
internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME,
|
internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME,
|
||||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY,
|
friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY,
|
||||||
|
direction: NET_FW_RULE_DIR_OUT,
|
||||||
protocol: NET_FW_IP_PROTOCOL_TCP.0,
|
protocol: NET_FW_IP_PROTOCOL_TCP.0,
|
||||||
local_user_spec: &local_user_spec,
|
local_user_spec: &local_user_spec,
|
||||||
offline_sid,
|
offline_sid,
|
||||||
@@ -153,7 +162,7 @@ pub fn ensure_offline_proxy_allowlist(
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ensure_offline_outbound_block(offline_sid: &str, log: &mut dyn Write) -> Result<()> {
|
pub fn ensure_offline_network_blocks(offline_sid: &str, log: &mut dyn Write) -> Result<()> {
|
||||||
let local_user_spec = format!("O:LSD:(A;;CC;;;{offline_sid})");
|
let local_user_spec = format!("O:LSD:(A;;CC;;;{offline_sid})");
|
||||||
|
|
||||||
let hr = unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) };
|
let hr = unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) };
|
||||||
@@ -187,6 +196,21 @@ pub fn ensure_offline_outbound_block(offline_sid: &str, log: &mut dyn Write) ->
|
|||||||
&BlockRuleSpec {
|
&BlockRuleSpec {
|
||||||
internal_name: OFFLINE_BLOCK_RULE_NAME,
|
internal_name: OFFLINE_BLOCK_RULE_NAME,
|
||||||
friendly_desc: OFFLINE_BLOCK_RULE_FRIENDLY,
|
friendly_desc: OFFLINE_BLOCK_RULE_FRIENDLY,
|
||||||
|
direction: NET_FW_RULE_DIR_OUT,
|
||||||
|
protocol: NET_FW_IP_PROTOCOL_ANY.0,
|
||||||
|
local_user_spec: &local_user_spec,
|
||||||
|
offline_sid,
|
||||||
|
remote_addresses: Some(NON_LOOPBACK_REMOTE_ADDRESSES),
|
||||||
|
remote_ports: None,
|
||||||
|
},
|
||||||
|
log,
|
||||||
|
)?;
|
||||||
|
ensure_block_rule(
|
||||||
|
&rules,
|
||||||
|
&BlockRuleSpec {
|
||||||
|
internal_name: OFFLINE_BLOCK_INBOUND_RULE_NAME,
|
||||||
|
friendly_desc: OFFLINE_BLOCK_INBOUND_RULE_FRIENDLY,
|
||||||
|
direction: NET_FW_RULE_DIR_IN,
|
||||||
protocol: NET_FW_IP_PROTOCOL_ANY.0,
|
protocol: NET_FW_IP_PROTOCOL_ANY.0,
|
||||||
local_user_spec: &local_user_spec,
|
local_user_spec: &local_user_spec,
|
||||||
offline_sid,
|
offline_sid,
|
||||||
@@ -335,7 +359,7 @@ fn configure_rule(rule: &INetFwRule3, spec: &BlockRuleSpec<'_>) -> Result<()> {
|
|||||||
format!("SetDescription failed: {err:?}"),
|
format!("SetDescription failed: {err:?}"),
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
rule.SetDirection(NET_FW_RULE_DIR_OUT).map_err(|err| {
|
rule.SetDirection(spec.direction).map_err(|err| {
|
||||||
anyhow::Error::new(SetupFailure::new(
|
anyhow::Error::new(SetupFailure::new(
|
||||||
SetupErrorCode::HelperFirewallRuleCreateOrAddFailed,
|
SetupErrorCode::HelperFirewallRuleCreateOrAddFailed,
|
||||||
format!("SetDirection failed: {err:?}"),
|
format!("SetDirection failed: {err:?}"),
|
||||||
@@ -519,6 +543,7 @@ mod tests {
|
|||||||
BlockRuleSpec {
|
BlockRuleSpec {
|
||||||
internal_name: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME,
|
internal_name: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME,
|
||||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY,
|
friendly_desc: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY,
|
||||||
|
direction: NET_FW_RULE_DIR_OUT,
|
||||||
protocol: NET_FW_IP_PROTOCOL_UDP.0,
|
protocol: NET_FW_IP_PROTOCOL_UDP.0,
|
||||||
local_user_spec,
|
local_user_spec,
|
||||||
offline_sid,
|
offline_sid,
|
||||||
@@ -528,6 +553,7 @@ mod tests {
|
|||||||
BlockRuleSpec {
|
BlockRuleSpec {
|
||||||
internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME,
|
internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME,
|
||||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY,
|
friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY,
|
||||||
|
direction: NET_FW_RULE_DIR_OUT,
|
||||||
protocol: NET_FW_IP_PROTOCOL_TCP.0,
|
protocol: NET_FW_IP_PROTOCOL_TCP.0,
|
||||||
local_user_spec,
|
local_user_spec,
|
||||||
offline_sid,
|
offline_sid,
|
||||||
@@ -537,6 +563,7 @@ mod tests {
|
|||||||
BlockRuleSpec {
|
BlockRuleSpec {
|
||||||
internal_name: OFFLINE_BLOCK_RULE_NAME,
|
internal_name: OFFLINE_BLOCK_RULE_NAME,
|
||||||
friendly_desc: OFFLINE_BLOCK_RULE_FRIENDLY,
|
friendly_desc: OFFLINE_BLOCK_RULE_FRIENDLY,
|
||||||
|
direction: NET_FW_RULE_DIR_OUT,
|
||||||
protocol: NET_FW_IP_PROTOCOL_ANY.0,
|
protocol: NET_FW_IP_PROTOCOL_ANY.0,
|
||||||
local_user_spec,
|
local_user_spec,
|
||||||
offline_sid,
|
offline_sid,
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ pub(super) fn cleanup_firewall_rules() -> Result<()> {
|
|||||||
let mut errors = Vec::new();
|
let mut errors = Vec::new();
|
||||||
for name in [
|
for name in [
|
||||||
"codex_sandbox_offline_block_outbound",
|
"codex_sandbox_offline_block_outbound",
|
||||||
|
"codex_sandbox_offline_block_inbound",
|
||||||
"codex_sandbox_offline_block_loopback_tcp",
|
"codex_sandbox_offline_block_loopback_tcp",
|
||||||
"codex_sandbox_offline_block_loopback_udp",
|
"codex_sandbox_offline_block_loopback_udp",
|
||||||
"codex_sandbox_offline_allow_loopback_proxy",
|
"codex_sandbox_offline_allow_loopback_proxy",
|
||||||
|
|||||||
Reference in new issue
Block a user