diff --git a/codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs b/codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs index f80efe4d12..95ca3fe473 100644 --- a/codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs +++ b/codex-rs/windows-sandbox-rs/src/bin/setup_main/win.rs @@ -344,28 +344,42 @@ fn lock_sandbox_dir( let system_sid = resolve_sid("SYSTEM")?; let admins_sid = resolve_sid("Administrators")?; let real_sid = resolve_sid(real_user)?; - let entries = [ + let inherited = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE; + let mut entries = vec![ ( sandbox_group_sid.to_vec(), sandbox_group_mask, sandbox_group_access_mode, + inherited, ), ( system_sid, FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE, GRANT_ACCESS, + inherited, ), ( admins_sid, FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE, GRANT_ACCESS, + inherited, + ), + ( + real_sid.clone(), + real_user_mask & !WRITE_DAC, + GRANT_ACCESS, + inherited, ), - (real_sid, real_user_mask, GRANT_ACCESS), ]; + // Permission maintenance belongs to the trusted caller on this directory, + // not to the helper files and directories that inherit its content access. + if real_user_mask & WRITE_DAC != 0 { + entries.push((real_sid, WRITE_DAC, GRANT_ACCESS, 0)); + } unsafe { let mut eas: Vec = Vec::new(); let mut sids: Vec<*mut c_void> = Vec::new(); - for (sid_bytes, mask, access_mode) in entries.iter().map(|(s, m, a)| (s, *m, *a)) { + for (sid_bytes, mask, access_mode, inheritance) in &entries { let sid_str = string_from_sid_bytes(sid_bytes).map_err(anyhow::Error::msg)?; let sid_w = to_wide(OsStr::new(&sid_str)); let mut psid: *mut c_void = std::ptr::null_mut(); @@ -377,9 +391,9 @@ fn lock_sandbox_dir( } sids.push(psid); eas.push(EXPLICIT_ACCESS_W { - grfAccessPermissions: mask, - grfAccessMode: access_mode, - grfInheritance: OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE, + grfAccessPermissions: *mask, + grfAccessMode: *access_mode, + grfInheritance: *inheritance, Trustee: TRUSTEE_W { pMultipleTrustee: std::ptr::null_mut(), MultipleTrusteeOperation: 0, @@ -816,7 +830,9 @@ fn lock_sandbox_bin_dir(payload: &Payload, sandbox_group_sid: &[u8]) -> Result<( sandbox_group_sid, GRANT_ACCESS, FILE_GENERIC_READ | FILE_GENERIC_EXECUTE, - FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE, + // Elevated provisioning may own this directory as Administrators. + // The following ordinary refresh must be able to retain its protected ACL. + FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE | WRITE_DAC, DaclInheritance::Protected, payload.mode, ) diff --git a/codex-rs/windows-sandbox-rs/src/bin/setup_main/win_acl_tests.rs b/codex-rs/windows-sandbox-rs/src/bin/setup_main/win_acl_tests.rs index df39cfdd18..0edce4aab6 100644 --- a/codex-rs/windows-sandbox-rs/src/bin/setup_main/win_acl_tests.rs +++ b/codex-rs/windows-sandbox-rs/src/bin/setup_main/win_acl_tests.rs @@ -7,6 +7,7 @@ use super::GRANT_ACCESS; use super::Payload; use super::SETUP_VERSION; use super::SetupMode; +use super::WRITE_DAC; use super::convert_string_sid_to_sid; use super::lock_sandbox_bin_dir; use super::lock_sandbox_dir; @@ -43,7 +44,11 @@ fn provision_only_locks_plain_directory_via_handle() { #[test] fn lock_sandbox_dir_blocks_inherited_write_for_runner_files() { - for setup_mode in [SetupMode::Full, SetupMode::ProvisionOnly] { + for setup_mode in [ + SetupMode::Full, + SetupMode::InteractiveProvision, + SetupMode::ProvisionOnly, + ] { let temp = tempfile::tempdir().expect("tempdir"); let workspace = temp.path().join("workspace"); let sandbox_bin = workspace.join(".sandbox-bin"); @@ -74,6 +79,8 @@ fn lock_sandbox_dir_blocks_inherited_write_for_runner_files() { ); let real_user = std::env::var("USERNAME").unwrap_or_else(|_| "Administrators".to_string()); + let real_sid = resolve_sid(&real_user).expect("resolve real user SID"); + let real_psid = sid_bytes_to_psid(&real_sid).expect("convert real user SID"); let payload = Payload { version: SETUP_VERSION, offline_username: String::new(), @@ -95,16 +102,52 @@ fn lock_sandbox_dir_blocks_inherited_write_for_runner_files() { let new_runner = sandbox_bin.join("new-runner.exe"); fs::write(&new_runner, b"new").expect("create new runner"); - for path in [&sandbox_bin, &existing_runner, &new_runner] { + let child_dir = sandbox_bin.join("child"); + fs::create_dir(&child_dir).expect("create child directory"); + + // Repeated refresh must retain the right seeded by privileged setup. + lock_sandbox_bin_dir(&payload, &sandbox_group_sid).expect("refresh sandbox bin"); + lock_sandbox_bin_dir(&payload, &sandbox_group_sid).expect("repeat refresh sandbox bin"); + assert!( + path_mask_allows( + &sandbox_bin, + &[real_psid], + WRITE_DAC, + /*require_all_bits*/ true + ) + .expect("check caller can maintain directory permissions") + ); + for path in [&existing_runner, &new_runner, &child_dir] { + assert!( + !path_mask_allows( + path, + &[real_psid], + WRITE_DAC, + /*require_all_bits*/ false + ) + .expect("check permission maintenance does not inherit to helpers") + ); + } + + for path in [&sandbox_bin, &existing_runner, &new_runner, &child_dir] { assert!( !path_mask_allows( path, &[workspace_psid], - FILE_GENERIC_WRITE | DELETE, + FILE_GENERIC_WRITE | DELETE | WRITE_DAC, /*require_all_bits*/ false, ) .expect("check protected path write and delete access") ); + assert!( + !path_mask_allows( + path, + &[sandbox_group_psid], + WRITE_DAC, + /*require_all_bits*/ false + ) + .expect("check sandbox cannot maintain permissions") + ); assert!( path_mask_allows( path, @@ -117,6 +160,7 @@ fn lock_sandbox_dir_blocks_inherited_write_for_runner_files() { } unsafe { + LocalFree(real_psid as HLOCAL); LocalFree(workspace_psid as HLOCAL); LocalFree(sandbox_group_psid as HLOCAL); }