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
@@ -750,14 +750,14 @@ fn configure_offline_sandbox_network(
|
||||
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 extract_setup_failure(&err).is_some() {
|
||||
return Err(err);
|
||||
}
|
||||
return Err(anyhow::Error::new(SetupFailure::new(
|
||||
SetupErrorCode::HelperFirewallRuleCreateOrAddFailed,
|
||||
format!("ensure offline outbound block failed: {err}"),
|
||||
format!("ensure offline network blocks failed: {err}"),
|
||||
)));
|
||||
}
|
||||
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_OK;
|
||||
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_DIRECTION;
|
||||
use windows::Win32::NetworkManagement::WindowsFirewall::NetFwPolicy2;
|
||||
use windows::Win32::NetworkManagement::WindowsFirewall::NetFwRule;
|
||||
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.
|
||||
// It intentionally does not change between installs.
|
||||
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_UDP_RULE_NAME: &str = "codex_sandbox_offline_block_loopback_udp";
|
||||
|
||||
// Friendly text shown in the firewall UI.
|
||||
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 =
|
||||
"Codex Sandbox Offline - Block Loopback TCP (Except Proxy)";
|
||||
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> {
|
||||
internal_name: &'a str,
|
||||
friendly_desc: &'a str,
|
||||
direction: NET_FW_RULE_DIRECTION,
|
||||
protocol: i32,
|
||||
local_user_spec: &'a str,
|
||||
offline_sid: &'a str,
|
||||
@@ -99,6 +105,7 @@ pub fn ensure_offline_proxy_allowlist(
|
||||
&BlockRuleSpec {
|
||||
internal_name: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME,
|
||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY,
|
||||
direction: NET_FW_RULE_DIR_OUT,
|
||||
protocol: NET_FW_IP_PROTOCOL_UDP.0,
|
||||
local_user_spec: &local_user_spec,
|
||||
offline_sid,
|
||||
@@ -115,6 +122,7 @@ pub fn ensure_offline_proxy_allowlist(
|
||||
&BlockRuleSpec {
|
||||
internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME,
|
||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY,
|
||||
direction: NET_FW_RULE_DIR_OUT,
|
||||
protocol: NET_FW_IP_PROTOCOL_TCP.0,
|
||||
local_user_spec: &local_user_spec,
|
||||
offline_sid,
|
||||
@@ -134,6 +142,7 @@ pub fn ensure_offline_proxy_allowlist(
|
||||
&BlockRuleSpec {
|
||||
internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME,
|
||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY,
|
||||
direction: NET_FW_RULE_DIR_OUT,
|
||||
protocol: NET_FW_IP_PROTOCOL_TCP.0,
|
||||
local_user_spec: &local_user_spec,
|
||||
offline_sid,
|
||||
@@ -153,7 +162,7 @@ pub fn ensure_offline_proxy_allowlist(
|
||||
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 hr = unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) };
|
||||
@@ -187,6 +196,21 @@ pub fn ensure_offline_outbound_block(offline_sid: &str, log: &mut dyn Write) ->
|
||||
&BlockRuleSpec {
|
||||
internal_name: OFFLINE_BLOCK_RULE_NAME,
|
||||
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,
|
||||
local_user_spec: &local_user_spec,
|
||||
offline_sid,
|
||||
@@ -335,7 +359,7 @@ fn configure_rule(rule: &INetFwRule3, spec: &BlockRuleSpec<'_>) -> Result<()> {
|
||||
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(
|
||||
SetupErrorCode::HelperFirewallRuleCreateOrAddFailed,
|
||||
format!("SetDirection failed: {err:?}"),
|
||||
@@ -519,6 +543,7 @@ mod tests {
|
||||
BlockRuleSpec {
|
||||
internal_name: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_NAME,
|
||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_UDP_RULE_FRIENDLY,
|
||||
direction: NET_FW_RULE_DIR_OUT,
|
||||
protocol: NET_FW_IP_PROTOCOL_UDP.0,
|
||||
local_user_spec,
|
||||
offline_sid,
|
||||
@@ -528,6 +553,7 @@ mod tests {
|
||||
BlockRuleSpec {
|
||||
internal_name: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_NAME,
|
||||
friendly_desc: OFFLINE_BLOCK_LOOPBACK_TCP_RULE_FRIENDLY,
|
||||
direction: NET_FW_RULE_DIR_OUT,
|
||||
protocol: NET_FW_IP_PROTOCOL_TCP.0,
|
||||
local_user_spec,
|
||||
offline_sid,
|
||||
@@ -537,6 +563,7 @@ mod tests {
|
||||
BlockRuleSpec {
|
||||
internal_name: OFFLINE_BLOCK_RULE_NAME,
|
||||
friendly_desc: OFFLINE_BLOCK_RULE_FRIENDLY,
|
||||
direction: NET_FW_RULE_DIR_OUT,
|
||||
protocol: NET_FW_IP_PROTOCOL_ANY.0,
|
||||
local_user_spec,
|
||||
offline_sid,
|
||||
|
||||
@@ -18,6 +18,7 @@ pub(super) fn cleanup_firewall_rules() -> Result<()> {
|
||||
let mut errors = Vec::new();
|
||||
for name in [
|
||||
"codex_sandbox_offline_block_outbound",
|
||||
"codex_sandbox_offline_block_inbound",
|
||||
"codex_sandbox_offline_block_loopback_tcp",
|
||||
"codex_sandbox_offline_block_loopback_udp",
|
||||
"codex_sandbox_offline_allow_loopback_proxy",
|
||||
|
||||
Reference in new issue
Block a user