diff --git a/codex-rs/windows-sandbox-rs/src/acl.rs b/codex-rs/windows-sandbox-rs/src/acl.rs index a10e8cdd57..c91fb91a79 100644 --- a/codex-rs/windows-sandbox-rs/src/acl.rs +++ b/codex-rs/windows-sandbox-rs/src/acl.rs @@ -99,23 +99,18 @@ fn acl_api_result(path: &Path, operation: &str, code: u32) -> Result<()> { /// # Safety /// Caller must free the returned security descriptor with `LocalFree` and pass an existing path. pub unsafe fn fetch_dacl_handle(path: &Path) -> Result<(*mut ACL, *mut c_void)> { - let wpath = to_wide(path); - let h = CreateFileW( - wpath.as_ptr(), - READ_CONTROL, - FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, - std::ptr::null_mut(), - OPEN_EXISTING, - FILE_FLAG_BACKUP_SEMANTICS, - 0, - ); - if h == INVALID_HANDLE_VALUE { - return Err(anyhow!("CreateFileW failed for {}", path.display())); - } + // Rust's file opening supports extended-length paths without changing the + // caller's spelling or resolving links before the existing reparse checks. + let handle = OpenOptions::new() + .access_mode(READ_CONTROL) + .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE) + .custom_flags(FILE_FLAG_BACKUP_SEMANTICS) + .open(path) + .with_context(|| format!("open ACL target {}", path.display()))?; let mut p_sd: *mut c_void = std::ptr::null_mut(); let mut p_dacl: *mut ACL = std::ptr::null_mut(); let code = GetSecurityInfo( - h, + handle.as_raw_handle() as _, 1, // SE_FILE_OBJECT DACL_SECURITY_INFORMATION, std::ptr::null_mut(), @@ -124,7 +119,6 @@ pub unsafe fn fetch_dacl_handle(path: &Path) -> Result<(*mut ACL, *mut c_void)> std::ptr::null_mut(), &mut p_sd, ); - CloseHandle(h); if code != ERROR_SUCCESS { return Err(anyhow!( "GetSecurityInfo failed for {}: {}", @@ -576,50 +570,51 @@ unsafe fn ensure_allow_mask_aces_with_inheritance_impl( }, }); } - let mut added = false; - if !entries.is_empty() { - let mut p_new_dacl: *mut ACL = std::ptr::null_mut(); + let mut p_new_dacl: *mut ACL = std::ptr::null_mut(); + // Collect errors before releasing either allocation so every update path + // shares the same cleanup, including failure to open the write handle. + let result = (|| -> Result { + if entries.is_empty() { + return Ok(false); + } let code2 = SetEntriesInAclW( entries.len() as u32, entries.as_ptr(), p_dacl, &mut p_new_dacl, ); - if code2 == ERROR_SUCCESS { - let code3 = SetNamedSecurityInfoW( - to_wide(path).as_ptr() as *mut u16, - 1, - DACL_SECURITY_INFORMATION, - std::ptr::null_mut(), - std::ptr::null_mut(), - p_new_dacl, - std::ptr::null_mut(), - ); - if code3 == ERROR_SUCCESS { - added = true; - if !p_new_dacl.is_null() { - LocalFree(p_new_dacl as HLOCAL); - } - } else { - if !p_new_dacl.is_null() { - LocalFree(p_new_dacl as HLOCAL); - } - if !p_sd.is_null() { - LocalFree(p_sd as HLOCAL); - } - return Err(anyhow!("SetNamedSecurityInfoW failed: {code3}")); - } - } else { - if !p_sd.is_null() { - LocalFree(p_sd as HLOCAL); - } + if code2 != ERROR_SUCCESS { return Err(anyhow!("SetEntriesInAclW failed: {code2}")); } + // Keep no-op checks read-only; request ACL-write access only when + // a grant actually needs updating. Use the same long-path support. + let handle = OpenOptions::new() + .access_mode(READ_CONTROL | WRITE_DAC) + .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE) + .custom_flags(FILE_FLAG_BACKUP_SEMANTICS) + .open(path) + .context("open ACL target for update")?; + let code3 = SetSecurityInfo( + handle.as_raw_handle() as _, + 1, + DACL_SECURITY_INFORMATION, + std::ptr::null_mut(), + std::ptr::null_mut(), + p_new_dacl, + std::ptr::null_mut(), + ); + if code3 != ERROR_SUCCESS { + return Err(anyhow!("SetSecurityInfo failed: {code3}")); + } + Ok(true) + })(); + if !p_new_dacl.is_null() { + LocalFree(p_new_dacl as HLOCAL); } if !p_sd.is_null() { LocalFree(p_sd as HLOCAL); } - Ok(added) + result } /// Ensure all provided SIDs have an allow ACE with the requested mask on the path. diff --git a/codex-rs/windows-sandbox-rs/src/setup_provisioning/setup_runtime_bin.rs b/codex-rs/windows-sandbox-rs/src/setup_provisioning/setup_runtime_bin.rs index 24965a7b56..c4173b8a86 100644 --- a/codex-rs/windows-sandbox-rs/src/setup_provisioning/setup_runtime_bin.rs +++ b/codex-rs/windows-sandbox-rs/src/setup_provisioning/setup_runtime_bin.rs @@ -19,6 +19,10 @@ use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_READ; #[path = "setup_runtime_bin_tests.rs"] mod tests; +#[cfg(test)] +#[path = "setup_runtime_long_paths_tests.rs"] +mod long_paths_tests; + pub(super) fn ensure_codex_app_runtime_paths_readable( sandbox_group_psid: *mut c_void, refresh_errors: &mut Vec, diff --git a/codex-rs/windows-sandbox-rs/src/setup_provisioning/setup_runtime_long_paths_tests.rs b/codex-rs/windows-sandbox-rs/src/setup_provisioning/setup_runtime_long_paths_tests.rs new file mode 100644 index 0000000000..4e5df34252 --- /dev/null +++ b/codex-rs/windows-sandbox-rs/src/setup_provisioning/setup_runtime_long_paths_tests.rs @@ -0,0 +1,80 @@ +//! Exercises runtime ACL repair beyond the legacy Windows path limit while preserving read-only grants. + +use super::ensure_runtime_tree_readable; +use crate::LocalSid; +use crate::acl::grant_read_execute_aces; +use crate::path_mask_allows; +use std::fs; +use std::os::windows::ffi::OsStrExt; +use windows_sys::Win32::Storage::FileSystem::DELETE; +use windows_sys::Win32::Storage::FileSystem::FILE_APPEND_DATA; +use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_EXECUTE; +use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_READ; +use windows_sys::Win32::Storage::FileSystem::FILE_WRITE_DATA; +use windows_sys::Win32::Storage::FileSystem::WRITE_DAC; + +#[test] +fn runtime_repair_handles_long_directory_and_file_paths() { + let runtime = tempfile::tempdir().expect("runtime directory"); + let mut directory = runtime.path().join("dependency-cache"); + while directory.as_os_str().encode_wide().count() <= 280 { + directory.push("a".repeat(60)); + } + fs::create_dir_all(&directory).expect("long runtime directory"); + let module = directory.join("module.js"); + fs::write(&module, b"runtime content").expect("long runtime file"); + let short = runtime.path().join("short.js"); + fs::write(&short, b"short-path control").expect("short runtime file"); + let sandbox_sid = LocalSid::from_string("S-1-5-21-10-20-30-40").expect("test SID"); + + for path in [&directory, &module, &short] { + assert!( + !path_mask_allows( + path, + &[sandbox_sid.as_ptr()], + FILE_GENERIC_READ | FILE_GENERIC_EXECUTE, + /*require_all_bits*/ true, + ) + .expect("read ACL before repair") + ); + // Exercise the write even for the nested file: a parent grant during + // traversal could otherwise make its inherited ACL a read-only no-op. + assert!( + unsafe { + grant_read_execute_aces(path, &[sandbox_sid.as_ptr()], /*inheritance*/ 0) + } + .expect("grant read/execute on the existing path") + ); + } + + for _ in 0..2 { + ensure_runtime_tree_readable(runtime.path(), sandbox_sid.as_ptr()) + .expect("repair the full runtime tree"); + for path in [&directory, &module, &short] { + assert!( + path_mask_allows( + path, + &[sandbox_sid.as_ptr()], + FILE_GENERIC_READ | FILE_GENERIC_EXECUTE, + /*require_all_bits*/ true, + ) + .expect("read repaired ACL") + ); + assert!( + !path_mask_allows( + path, + &[sandbox_sid.as_ptr()], + FILE_WRITE_DATA | FILE_APPEND_DATA | DELETE | WRITE_DAC, + /*require_all_bits*/ false, + ) + .expect("repair must not grant write or ACL management") + ); + assert!( + !unsafe { + grant_read_execute_aces(path, &[sandbox_sid.as_ptr()], /*inheritance*/ 0) + } + .expect("an already-correct ACL is a no-op") + ); + } + } +}