Lightweight coding agent that runs in your terminal
Rust
Recover malformed Windows deny-read ACL state safely (#50940)
zm-oaide3721a7be07
diff --git a/codex-rs/core/tests/suite/windows_sandbox.rs b/codex-rs/core/tests/suite/windows_sandbox.rsindex 28703bbe45..8afde91cda 100644--- a/codex-rs/core/tests/suite/windows_sandbox.rs+++ b/codex-rs/core/tests/suite/windows_sandbox.rs@@ -849,10 +849,15 @@ async fn windows_elevated_unified_exec_enforces_large_recursive_deny_reads() ->]);config.permissions- .set_permission_profile(PermissionProfile::from_runtime_permissions(- &file_system_sandbox_policy,- NetworkSandboxPolicy::Restricted,- ))+ .set_permission_profile(+ PermissionProfile::from_runtime_permissions(+ &file_system_sandbox_policy,+ NetworkSandboxPolicy::Restricted,+ )+ .materialize_project_roots_with_workspace_roots(+ std::slice::from_ref(&config.cwd),+ ),+ ).expect("set managed deny-read permission profile");}).with_workspace_setup(|cwd, _fs| async move {@@ -968,6 +973,95 @@ async fn windows_elevated_unified_exec_enforces_large_recursive_deny_reads() ->"exec_command leaked exact-path-denied file contents: {output:?}");+ // Re-enter real setup after corrupting this test's bookkeeping, then exercise+ // recovery through the same agent tool path rather than relying on a cached launch.+ let state_path = codex_home.path().join(".sandbox/deny_read_acl_state.json");+ std::fs::write(&state_path, b"malformed bookkeeping")?;+ let permission_profile = harness+ .test()+ .config+ .permissions+ .effective_permission_profile();+ run_windows_sandbox_setup(WindowsSandboxSetupRequest {+ mode: WindowsSandboxSetupMode::Elevated,+ permission_profile: permission_profile.clone(),+ workspace_roots: vec![harness.test().config.cwd.clone()],+ command_cwd: harness.test().cwd_path().to_path_buf(),+ env_map: std::env::vars().collect(),+ codex_home: codex_home.path().to_path_buf(),+ })+ .await?;+ let rebuilt: serde_json::Value = serde_json::from_slice(&std::fs::read(&state_path)?)?;+ let principals = rebuilt["principals"]+ .as_object()+ .expect("rebuilt principals");+ assert!(+ principals+ .values()+ .any(|paths| paths.as_array().is_some_and(|paths| !paths.is_empty()))+ );++ std::fs::write(+ harness.test().workspace_path("remove-after-recovery.txt"),+ b"must be removed by the sandboxed command",+ )?;+ let recovery_call_id = "windows-recovered-deny-read-exec-command";+ let recovery_args = json!({+ "cmd": concat!(+ "(echo edited-after-recovery)>public.txt & ",+ "del remove-after-recovery.txt & ",+ "(type secret.env 1>NUL 2>NUL && echo GLOB-READ || echo GLOB-DENIED) & ",+ "(type exact-secret.txt 1>NUL 2>NUL && echo EXACT-READ || echo EXACT-DENIED)"+ ),+ "yield_time_ms": 30_000,+ "tty": false,+ "login": false,+ });+ mount_sse_sequence(+ harness.server(),+ vec![+ sse(vec![+ ev_response_created("resp-windows-recovered"),+ ev_function_call(+ recovery_call_id,+ "exec_command",+ &serde_json::to_string(&recovery_args)?,+ ),+ ev_completed("resp-windows-recovered"),+ ]),+ sse(vec![+ ev_assistant_message("msg-windows-recovered", "done"),+ ev_completed("resp-windows-recovered-complete"),+ ]),+ ],+ )+ .await;+ harness+ .submit_with_permission_profile(+ "edit and delete the allowed fixtures and try the denied reads",+ permission_profile,+ )+ .await?;+ let output = harness.function_call_stdout(recovery_call_id).await;+ assert!(+ output.contains("GLOB-DENIED") && output.contains("EXACT-DENIED"),+ "denies must survive recovery: {output:?}"+ );+ assert!(+ !output.contains("GLOB-READ") && !output.contains("EXACT-READ"),+ "recovery must not allow denied reads: {output:?}"+ );+ assert_eq!(+ std::fs::read(harness.test().workspace_path("public.txt"))?,+ b"edited-after-recovery\r\n"+ );+ assert!(+ !harness+ .test()+ .workspace_path("remove-after-recovery.txt")+ .exists()+ );+Ok(())}diff --git a/codex-rs/windows-sandbox-rs/src/deny_read_state.rs b/codex-rs/windows-sandbox-rs/src/deny_read_state.rsindex 274ff004e9..7b12f50bfa 100644--- a/codex-rs/windows-sandbox-rs/src/deny_read_state.rs+++ b/codex-rs/windows-sandbox-rs/src/deny_read_state.rs@@ -4,13 +4,24 @@ use crate::deny_read_acl::lexical_path_key;use crate::setup::sandbox_dir;use anyhow::Context;use anyhow::Result;+use anyhow::ensure;use serde::Deserialize;use serde::Serialize;use std::collections::BTreeMap;use std::collections::HashSet;use std::ffi::c_void;+use std::fs::File;+use std::fs::OpenOptions;+use std::io::Read;+use std::io::Write;+use std::os::windows::fs::OpenOptionsExt;+use std::os::windows::io::AsRawHandle;use std::path::Path;use std::path::PathBuf;+use std::time::Duration;+use std::time::Instant;+use windows_sys::Win32::Foundation as win;+use windows_sys::Win32::Storage::FileSystem as winfs;const DENY_READ_ACL_STATE_FILE: &str = "deny_read_acl_state.json";@@ -27,6 +38,9 @@ struct PersistentDenyReadAclState {/// each SID, apply the new desired set first, and only then revoke stale paths/// from the same SID so profile changes do not leave old deny-read ACEs behind.///+/// Malformed bookkeeping is rebuilt after applying current denies. Unknown+/// historical restrictions cannot safely be revoked and remain in place.+////// # Safety/// Caller must pass a valid SID pointer matching `principal_sid`.pub unsafe fn sync_persistent_deny_read_acls(@@ -68,20 +82,97 @@ pub unsafe fn sync_persistent_deny_read_acls(}fn load_state(path: &Path) -> Result<PersistentDenyReadAclState> {- match std::fs::read(path) {- Ok(bytes) => serde_json::from_slice(&bytes)- .with_context(|| format!("parse deny-read ACL state {}", path.display())),- Err(err) if err.kind() == std::io::ErrorKind::NotFound => {- Ok(PersistentDenyReadAclState::default())- }- Err(err) => {- Err(err).with_context(|| format!("read deny-read ACL state {}", path.display()))+ let deadline = Instant::now() + Duration::from_secs(2);+ loop {+ let stable = Instant::now() >= deadline;+ let bytes = (|| -> Result<Vec<u8>> {+ // Ordinary reads allow legacy writers; malformed recovery excludes live writers.+ let share_mode = if stable {+ winfs::FILE_SHARE_READ | winfs::FILE_SHARE_DELETE+ } else {+ winfs::FILE_SHARE_READ | winfs::FILE_SHARE_WRITE | winfs::FILE_SHARE_DELETE+ };+ let mut file = OpenOptions::new()+ .read(true)+ .share_mode(share_mode)+ .custom_flags(winfs::FILE_FLAG_OPEN_REPARSE_POINT)+ .open(path)?;+ // Validate the same leaf before either accepting state or recovering it.+ validate_state_file(&file)?;+ let mut bytes = Vec::new();+ file.read_to_end(&mut bytes)?;+ Ok(bytes)+ })();+ match bytes {+ Ok(bytes) => match serde_json::from_slice(&bytes) {+ Ok(state) => return Ok(state),+ Err(_) if !stable => std::thread::sleep(Duration::from_millis(25)),+ Err(_) => return Ok(PersistentDenyReadAclState::default()),+ },+ Err(err)+ if err+ .downcast_ref::<std::io::Error>()+ .is_some_and(|error| error.kind() == std::io::ErrorKind::NotFound) =>+ {+ return Ok(PersistentDenyReadAclState::default());+ }+ Err(err) => {+ return Err(err)+ .with_context(|| format!("read deny-read ACL state {}", path.display()));+ }}}}fn store_state(path: &Path, state: &PersistentDenyReadAclState) -> Result<()> {let bytes = serde_json::to_vec_pretty(state).context("serialize deny-read ACL state")?;- std::fs::write(path, bytes)- .with_context(|| format!("write deny-read ACL state {}", path.display()))+ (|| -> Result<()> {+ // Recovery makes malformed files writable. Never follow the state leaf+ // or truncate it before checking the actual object. Resolve configured+ // parent paths normally, including junctions and volume GUIDs.+ let deadline = Instant::now() + Duration::from_secs(2);+ let mut file = loop {+ let opened = OpenOptions::new()+ .write(true)+ .create(true)+ .truncate(false)+ .share_mode(winfs::FILE_SHARE_READ | winfs::FILE_SHARE_WRITE)+ .custom_flags(winfs::FILE_FLAG_OPEN_REPARSE_POINT)+ .open(path);+ match opened {+ Ok(file) => break file,+ Err(error)+ if error.raw_os_error() == Some(win::ERROR_SHARING_VIOLATION as i32)+ && Instant::now() < deadline =>+ {+ std::thread::sleep(Duration::from_millis(25));+ }+ Err(error) => return Err(error.into()),+ }+ };+ validate_state_file(&file)?;+ file.write_all(&bytes)?;+ file.set_len(bytes.len() as u64)?;+ Ok(())+ })()+ .with_context(|| format!("write deny-read ACL state {}", path.display()))+}++fn validate_state_file(file: &File) -> Result<()> {+ let mut info = unsafe { std::mem::zeroed::<winfs::BY_HANDLE_FILE_INFORMATION>() };+ if unsafe { winfs::GetFileInformationByHandle(file.as_raw_handle() as win::HANDLE, &mut info) }+ == 0+ {+ return Err(std::io::Error::last_os_error().into());+ }+ ensure!(+ info.dwFileAttributes & winfs::FILE_ATTRIBUTE_REPARSE_POINT == 0,+ "state file is a reparse point"+ );+ ensure!(info.nNumberOfLinks == 1, "state file has multiple links");+ Ok(())}++#[cfg(test)]+#[path = "deny_read_state_tests.rs"]+mod tests;diff --git a/codex-rs/windows-sandbox-rs/src/deny_read_state_tests.rs b/codex-rs/windows-sandbox-rs/src/deny_read_state_tests.rsnew file mode 100644index 0000000000..7a651ad7af--- /dev/null+++ b/codex-rs/windows-sandbox-rs/src/deny_read_state_tests.rs@@ -0,0 +1,351 @@+//! Exercises recovery and its file-safety boundaries on real Windows files.++use super::*;+use crate::token::LocalSid;+use pretty_assertions::assert_eq;+use std::ffi::OsString;+use std::fs;+use std::fs::File;+use std::os::windows::ffi::OsStringExt;+use std::process::Command;+use tempfile::TempDir;++#[test]+fn load_propagates_io_errors_and_preserves_missing_file_behavior() -> Result<()> {+ let temp = TempDir::new()?;+ let state_path = temp.path().join(DENY_READ_ACL_STATE_FILE);+ assert!(load_state(&state_path)?.principals.is_empty());++ fs::create_dir(&state_path)?;+ let error = load_state(&state_path).err().expect("read error");+ assert!(error.downcast_ref::<std::io::Error>().is_some());+ Ok(())+}++#[test]+fn load_waits_for_a_legacy_writer_without_recovering_over_it() -> Result<()> {+ let temp = TempDir::new()?;+ let path = temp.path().join(DENY_READ_ACL_STATE_FILE);+ let valid = br#"{"principals":{"legacy":["existing"]}}"#;+ fs::write(&path, valid)?;+ let mut writer = OpenOptions::new().write(true).truncate(true).open(&path)?;+ std::thread::scope(|scope| -> Result<()> {+ let write = scope.spawn(move || {+ std::thread::sleep(Duration::from_millis(75));+ writer.write_all(valid)+ });+ let state = load_state(&path)?;+ write.join().expect("legacy writer")?;+ assert_eq!(state.principals["legacy"], vec![PathBuf::from("existing")]);+ Ok(())+ })?;++ let _writer = OpenOptions::new().write(true).truncate(true).open(&path)?;+ let error = load_state(&path)+ .err()+ .expect("active writer must prevent recovery");+ let error = error+ .downcast_ref::<std::io::Error>()+ .expect("sharing error");+ assert_eq!(+ error.raw_os_error(),+ Some(win::ERROR_SHARING_VIOLATION as i32)+ );+ Ok(())+}++#[test]+fn sync_rebuilds_corrupt_bookkeeping_only_after_successful_apply() -> Result<()> {+ let temp = TempDir::new()?;+ let state_dir = sandbox_dir(temp.path());+ fs::create_dir(&state_dir)?;+ let state_path = state_dir.join(DENY_READ_ACL_STATE_FILE);+ let malformed = b" ";+ fs::write(&state_path, malformed)?;+ let blocker = temp.path().join("ordinary-file");+ fs::write(&blocker, b"not a directory")?;+ let sid_string = "S-1-5-21-10-20-30-40";+ let sid = LocalSid::from_string(sid_string)?;++ // Creating a child beneath a file fails before any ACL can be changed.+ let error = unsafe {+ sync_persistent_deny_read_acls(+ temp.path(),+ sid_string,+ &[blocker.join("child")],+ sid.as_ptr(),+ )+ }+ .expect_err("apply error must propagate");+ assert!(error.downcast_ref::<std::io::Error>().is_some());+ assert_eq!(fs::read(&state_path)?.as_slice(), malformed);++ let historical = temp.path().join("historical");+ let current = temp.path().join("current");+ fs::create_dir(&historical)?;+ unsafe { crate::acl::add_deny_read_ace(&historical, sid.as_ptr()) }?;+ let applied = unsafe {+ sync_persistent_deny_read_acls(+ temp.path(),+ sid_string,+ std::slice::from_ref(¤t),+ sid.as_ptr(),+ )+ }?;+ assert_eq!(applied, vec![current.clone()]);+ let rebuilt: PersistentDenyReadAclState = serde_json::from_slice(&fs::read(&state_path)?)?;+ assert_eq!(+ rebuilt.principals,+ BTreeMap::from([(sid_string.to_owned(), vec![current])])+ );+ unsafe {+ let (dacl, descriptor) = crate::acl::fetch_dacl_handle(&historical)?;+ let preserved = crate::acl::dacl_has_read_deny_for_sid(dacl, sid.as_ptr());+ win::LocalFree(descriptor as win::HLOCAL);+ assert!(preserved, "unknown historical restrictions remain in place");+ }++ let error = unsafe {+ sync_persistent_deny_read_acls(+ &temp.path().join("missing-home"),+ sid_string,+ &[],+ sid.as_ptr(),+ )+ }+ .expect_err("save error must propagate");+ assert!(error.downcast_ref::<std::io::Error>().is_some());+ Ok(())+}++/// Linked bookkeeping, valid or malformed, must preserve its victim and existing denies without adding new ones.+#[test]+fn sync_rejects_linked_state_before_changing_acls() -> Result<()> {+ let temp = TempDir::new()?;+ let state_dir = sandbox_dir(temp.path());+ fs::create_dir(&state_dir)?;+ let path = state_dir.join(DENY_READ_ACL_STATE_FILE);+ let victim = temp.path().join("victim");+ let sid_string = "S-1-5-21-10-20-30-40";+ let sid = LocalSid::from_string(sid_string)?;+ let previous = temp.path().join("previous");+ let desired = temp.path().join("desired");+ fs::create_dir(&previous)?;+ fs::create_dir(&desired)?;+ unsafe { crate::acl::add_deny_read_ace(&previous, sid.as_ptr()) }?;+ let valid = serde_json::to_vec(&PersistentDenyReadAclState {+ principals: BTreeMap::from([(sid_string.to_owned(), vec![previous.clone()])]),+ })?;+ for contents in [b"not json: keep me".as_slice(), valid.as_slice()] {+ fs::write(&victim, contents)?;+ for link_kind in ["hardlink", "symlink"] {+ if link_kind == "hardlink" {+ fs::hard_link(&victim, &path)?;+ } else {+ std::os::windows::fs::symlink_file(&victim, &path)?;+ }+ let error = unsafe {+ sync_persistent_deny_read_acls(+ temp.path(),+ sid_string,+ std::slice::from_ref(&desired),+ sid.as_ptr(),+ )+ }+ .expect_err("linked state must be rejected");+ assert!(format!("{error:#}").contains(if link_kind == "hardlink" {+ "multiple links"+ } else {+ "reparse point"+ }));+ assert_eq!(fs::read(&victim)?, contents);+ for (target, expected_deny) in [(&previous, true), (&desired, false)] {+ unsafe {+ let (dacl, descriptor) = crate::acl::fetch_dacl_handle(target)?;+ let denied = crate::acl::dacl_has_read_deny_for_sid(dacl, sid.as_ptr());+ win::LocalFree(descriptor as win::HLOCAL);+ assert_eq!(denied, expected_deny, "rejected state must not change ACLs");+ }+ }+ fs::remove_file(&path)?;+ }+ }+ Ok(())+}++#[test]+fn store_keeps_file_identity_permissions_and_legacy_writer_compatibility() -> Result<()> {+ let temp = TempDir::new()?;+ let path = temp.path().join(DENY_READ_ACL_STATE_FILE);+ let state = PersistentDenyReadAclState {+ principals: BTreeMap::from([("kept".to_owned(), vec![PathBuf::from("secret")])]),+ };+ fs::write(+ &path,+ b"malformed longer than the new state ",+ )?;+ let sid = LocalSid::from_string("S-1-5-21-10-20-30-40")?;+ unsafe { crate::acl::add_deny_read_ace(&path, sid.as_ptr()) }?;+ let file = File::open(&path)?;+ let mut before = unsafe { std::mem::zeroed::<winfs::BY_HANDLE_FILE_INFORMATION>() };+ assert_ne!(+ unsafe {+ winfs::GetFileInformationByHandle(file.as_raw_handle() as win::HANDLE, &mut before)+ },+ 0+ );+ // A legacy writer can remain open after finishing valid bytes; normal store+ // must not newly fail just because that writer has not closed its handle.+ let _writer = OpenOptions::new().write(true).open(&path)?;+ // A delete-access holder may coexist with readers and legacy writers. Wait+ // for it to close without allowing replacement during validation or writing.+ let holder = OpenOptions::new()+ .access_mode(winfs::DELETE)+ .share_mode(winfs::FILE_SHARE_READ | winfs::FILE_SHARE_WRITE | winfs::FILE_SHARE_DELETE)+ .open(&path)?;+ std::thread::scope(|scope| -> Result<()> {+ let release = scope.spawn(move || {+ std::thread::sleep(Duration::from_millis(75));+ drop(holder);+ });+ store_state(&path, &state)?;+ release.join().expect("delete-access holder");+ Ok(())+ })?;+ let root = path.ancestors().last().expect("drive root");+ let mut volume = [0u16; 50];+ assert_ne!(+ unsafe {+ winfs::GetVolumeNameForVolumeMountPointW(+ crate::winutil::to_wide(root).as_ptr(),+ volume.as_mut_ptr(),+ volume.len() as u32,+ )+ },+ 0+ );+ let end = volume+ .iter()+ .position(|unit| *unit == 0)+ .expect("volume name");+ let volume_path =+ PathBuf::from(OsString::from_wide(&volume[..end])).join(path.strip_prefix(root)?);+ store_state(&volume_path, &state)?;++ let alias_dir = TempDir::new()?;+ let junction = alias_dir.path().join("sandbox-junction");+ assert!(+ Command::new("cmd.exe")+ .args(["/c", "mklink", "/J"])+ .arg(&junction)+ .arg(temp.path())+ .output()?+ .status+ .success()+ );+ store_state(&junction.join(DENY_READ_ACL_STATE_FILE), &state)?;+ let reopened = File::open(&path)?;+ let mut after = unsafe { std::mem::zeroed::<winfs::BY_HANDLE_FILE_INFORMATION>() };+ assert_ne!(+ unsafe {+ winfs::GetFileInformationByHandle(reopened.as_raw_handle() as win::HANDLE, &mut after)+ },+ 0+ );+ assert_eq!(+ (+ after.dwVolumeSerialNumber,+ after.nFileIndexHigh,+ after.nFileIndexLow+ ),+ (+ before.dwVolumeSerialNumber,+ before.nFileIndexHigh,+ before.nFileIndexLow+ )+ );+ assert_eq!(load_state(&path)?.principals, state.principals);+ unsafe {+ let (dacl, descriptor) = crate::acl::fetch_dacl_handle(&path)?;+ let preserved = crate::acl::dacl_has_read_deny_for_sid(dacl, sid.as_ptr());+ win::LocalFree(descriptor as win::HLOCAL);+ assert!(preserved);+ }+ Ok(())+}++// An unreleased delete-access handle must produce a bounded sharing error,+// leaving the existing bytes intact instead of truncating or replacing them.+#[test]+fn store_times_out_without_modifying_state_held_for_delete() -> Result<()> {+ let temp = TempDir::new()?;+ let path = temp.path().join(DENY_READ_ACL_STATE_FILE);+ let original = br#"{"principals":{"kept":["secret"]}}"#;+ fs::write(&path, original)?;+ let holder = OpenOptions::new()+ .access_mode(winfs::DELETE)+ .share_mode(winfs::FILE_SHARE_READ | winfs::FILE_SHARE_WRITE | winfs::FILE_SHARE_DELETE)+ .open(&path)?;+ let started = Instant::now();+ let error = store_state(&path, &PersistentDenyReadAclState::default())+ .expect_err("delete-access holder must block storage");+ assert!(started.elapsed() >= Duration::from_secs(2));+ assert_eq!(+ error+ .downcast_ref::<std::io::Error>()+ .expect("sharing error")+ .raw_os_error(),+ Some(win::ERROR_SHARING_VIOLATION as i32)+ );+ assert_eq!(fs::read(&path)?, original);+ drop(holder);+ assert_eq!(+ load_state(&path)?.principals["kept"],+ vec![PathBuf::from("secret")]+ );+ Ok(())+}++#[test]+fn recovered_bookkeeping_tracks_repeated_calls_and_a_later_profile_change() -> Result<()> {+ let temp = TempDir::new()?;+ let state_dir = sandbox_dir(temp.path());+ fs::create_dir(&state_dir)?;+ let path = state_dir.join(DENY_READ_ACL_STATE_FILE);+ // Simulate an interrupted in-place save, after an older deny was applied.+ fs::write(&path, br#"{"principals":"#)?;+ let sid_string = "S-1-5-21-10-20-30-40";+ let sid = LocalSid::from_string(sid_string)?;+ let historical = temp.path().join("historical");+ let first = temp.path().join("first");+ let second = temp.path().join("second");+ fs::create_dir(&historical)?;+ unsafe { crate::acl::add_deny_read_ace(&historical, sid.as_ptr()) }?;++ for desired in [&first, &first, &second, &second] {+ let applied = unsafe {+ sync_persistent_deny_read_acls(+ temp.path(),+ sid_string,+ std::slice::from_ref(desired),+ sid.as_ptr(),+ )+ }?;+ assert_eq!(applied, vec![desired.clone()]);+ let state = load_state(&path)?;+ assert_eq!(+ state.principals,+ BTreeMap::from([(sid_string.to_owned(), vec![desired.clone()])])+ );+ }+ // Recovery must preserve both unknown historical denies and the current profile.+ for target in [&historical, &second] {+ unsafe {+ let (dacl, descriptor) = crate::acl::fetch_dacl_handle(target)?;+ let denied = crate::acl::dacl_has_read_deny_for_sid(dacl, sid.as_ptr());+ win::LocalFree(descriptor as win::HLOCAL);+ assert!(denied, "deny must remain at {}", target.display());+ }+ }+ Ok(())+}