From 0dddc354d8a84d50104f2470f414130f9679630a Mon Sep 17 00:00:00 2001 From: Eva Wong Date: Tue, 5 May 2026 03:38:13 -0700 Subject: [PATCH] Wire Windows metadata monitor through sandbox exits --- .../windows-sandbox-rs/src/elevated_impl.rs | 4 +-- codex-rs/windows-sandbox-rs/src/lib.rs | 4 +-- .../src/protected_metadata.rs | 2 -- .../src/unified_exec/backends/elevated.rs | 3 +- .../src/unified_exec/backends/legacy.rs | 32 +++++++++---------- .../unified_exec/backends/windows_common.rs | 8 ++--- 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/codex-rs/windows-sandbox-rs/src/elevated_impl.rs b/codex-rs/windows-sandbox-rs/src/elevated_impl.rs index df8ce48d15..05f8bd8058 100644 --- a/codex-rs/windows-sandbox-rs/src/elevated_impl.rs +++ b/codex-rs/windows-sandbox-rs/src/elevated_impl.rs @@ -131,6 +131,7 @@ mod windows_impl { allow_null_device(psid_to_use); allow_named_pipe_device(psid_to_use); } + let protected_metadata_runtime = protected_metadata_guard.into_runtime()?; (|| -> Result { let spawn_request = SpawnRequest { @@ -183,8 +184,7 @@ mod windows_impl { } }; - let protected_metadata_violations = - protected_metadata_guard.cleanup_created_monitored_paths()?; + let protected_metadata_violations = protected_metadata_runtime.finish()?; if !protected_metadata_violations.is_empty() && exit_code == 0 { exit_code = 1; } diff --git a/codex-rs/windows-sandbox-rs/src/lib.rs b/codex-rs/windows-sandbox-rs/src/lib.rs index 70e39cacb4..692a9d0475 100644 --- a/codex-rs/windows-sandbox-rs/src/lib.rs +++ b/codex-rs/windows-sandbox-rs/src/lib.rs @@ -522,6 +522,7 @@ mod windows_impl { allow_named_pipe_device(psid); } } + let protected_metadata_runtime = protected_metadata_guard.into_runtime()?; let (stdin_pair, stdout_pair, stderr_pair) = unsafe { setup_stdio_pipes()? }; let ((in_r, in_w), (out_r, out_w), (err_r, err_w)) = (stdin_pair, stdout_pair, stderr_pair); let spawn_res = unsafe { @@ -638,8 +639,7 @@ mod windows_impl { } else { exit_code_u32 as i32 }; - let protected_metadata_violations = - protected_metadata_guard.cleanup_created_monitored_paths()?; + let protected_metadata_violations = protected_metadata_runtime.finish()?; if !protected_metadata_violations.is_empty() && exit_code == 0 { exit_code = 1; } diff --git a/codex-rs/windows-sandbox-rs/src/protected_metadata.rs b/codex-rs/windows-sandbox-rs/src/protected_metadata.rs index 8ea780fc79..c1c1b2d887 100644 --- a/codex-rs/windows-sandbox-rs/src/protected_metadata.rs +++ b/codex-rs/windows-sandbox-rs/src/protected_metadata.rs @@ -1,5 +1,3 @@ -#![allow(dead_code)] - use crate::setup::ProtectedMetadataMode; use crate::setup::ProtectedMetadataTarget; use crate::winutil::to_wide; diff --git a/codex-rs/windows-sandbox-rs/src/unified_exec/backends/elevated.rs b/codex-rs/windows-sandbox-rs/src/unified_exec/backends/elevated.rs index 1e30b76b7b..f1efcb9d42 100644 --- a/codex-rs/windows-sandbox-rs/src/unified_exec/backends/elevated.rs +++ b/codex-rs/windows-sandbox-rs/src/unified_exec/backends/elevated.rs @@ -45,6 +45,7 @@ pub(crate) async fn spawn_windows_sandbox_session_elevated( )?; let protected_metadata_guard = prepare_protected_metadata_targets(protected_metadata_targets); + let protected_metadata_runtime = protected_metadata_guard.into_runtime()?; let spawn_request = SpawnRequest { command: command.clone(), cwd: cwd.to_path_buf(), @@ -104,7 +105,7 @@ pub(crate) async fn spawn_windows_sandbox_session_elevated( stdout_tx, stderr_rx.as_ref().map(|(tx, _rx)| tx.clone()), exit_tx, - Some(protected_metadata_guard), + Some(protected_metadata_runtime), ); Ok(finish_driver_spawn( diff --git a/codex-rs/windows-sandbox-rs/src/unified_exec/backends/legacy.rs b/codex-rs/windows-sandbox-rs/src/unified_exec/backends/legacy.rs index 8e42c7554b..f9cba58ad8 100644 --- a/codex-rs/windows-sandbox-rs/src/unified_exec/backends/legacy.rs +++ b/codex-rs/windows-sandbox-rs/src/unified_exec/backends/legacy.rs @@ -10,7 +10,7 @@ use crate::process::StderrMode; use crate::process::StdinMode; use crate::process::read_handle_loop; use crate::process::spawn_process_with_pipes; -use crate::protected_metadata::ProtectedMetadataGuard; +use crate::protected_metadata::ProtectedMetadataRuntime; use crate::protected_metadata::prepare_protected_metadata_targets; use crate::setup::ProtectedMetadataTarget; use crate::spawn_prep::LocalSid; @@ -208,7 +208,7 @@ fn finalize_exit( output_join: std::thread::JoinHandle<()>, guards: Vec, cap_sid: Option, - protected_metadata_guard: ProtectedMetadataGuard, + protected_metadata_runtime: ProtectedMetadataRuntime, logs_base_dir: Option<&Path>, command: Vec, ) { @@ -226,21 +226,20 @@ fn finalize_exit( }; let _ = output_join.join(); - let protected_metadata_failure = - match protected_metadata_guard.cleanup_created_monitored_paths() { - Ok(paths) => { - if !paths.is_empty() && exit_code == 0 { - exit_code = 1; - } - None + let protected_metadata_failure = match protected_metadata_runtime.finish() { + Ok(paths) => { + if !paths.is_empty() && exit_code == 0 { + exit_code = 1; } - Err(err) => { - if exit_code == 0 { - exit_code = 1; - } - Some(format!("protected metadata cleanup failed: {err:#}")) + None + } + Err(err) => { + if exit_code == 0 { + exit_code = 1; } - }; + Some(format!("protected metadata cleanup failed: {err:#}")) + } + }; let _ = exit_tx.send(exit_code); unsafe { @@ -343,6 +342,7 @@ pub(crate) async fn spawn_windows_sandbox_session_legacy( persist_aces, &additional_deny_write_paths, ); + let protected_metadata_runtime = protected_metadata_guard.into_runtime()?; let (writer_tx, writer_rx) = mpsc::channel::>(128); let (stdout_tx, stdout_rx) = broadcast::channel::>(256); @@ -433,7 +433,7 @@ pub(crate) async fn spawn_windows_sandbox_session_legacy( output_join, guards_for_wait, cap_sid_for_wait, - protected_metadata_guard, + protected_metadata_runtime, common.logs_base_dir.as_deref(), command_for_wait, ); diff --git a/codex-rs/windows-sandbox-rs/src/unified_exec/backends/windows_common.rs b/codex-rs/windows-sandbox-rs/src/unified_exec/backends/windows_common.rs index f46a01ec8a..1e1190cdb1 100644 --- a/codex-rs/windows-sandbox-rs/src/unified_exec/backends/windows_common.rs +++ b/codex-rs/windows-sandbox-rs/src/unified_exec/backends/windows_common.rs @@ -6,7 +6,7 @@ use crate::ipc_framed::ResizePayload; use crate::ipc_framed::StdinPayload; use crate::ipc_framed::decode_bytes; use crate::ipc_framed::encode_bytes; -use crate::protected_metadata::ProtectedMetadataGuard; +use crate::protected_metadata::ProtectedMetadataRuntime; use anyhow::Result; use codex_utils_pty::ProcessDriver; use codex_utils_pty::SpawnedProcess; @@ -98,7 +98,7 @@ pub(crate) fn start_runner_stdout_reader( stdout_tx: broadcast::Sender>, stderr_tx: Option>>, exit_tx: oneshot::Sender, - protected_metadata_guard: Option, + protected_metadata_runtime: Option, ) { std::thread::spawn(move || { loop { @@ -143,8 +143,8 @@ pub(crate) fn start_runner_stdout_reader( } Message::Exit { payload } => { let mut exit_code = payload.exit_code; - if let Some(protected_metadata_guard) = protected_metadata_guard { - match protected_metadata_guard.cleanup_created_monitored_paths() { + if let Some(protected_metadata_runtime) = protected_metadata_runtime { + match protected_metadata_runtime.finish() { Ok(paths) => { if !paths.is_empty() && exit_code == 0 { exit_code = 1;