Files
codex/codex-rs/windows-sandbox-rs/src/deny_read_state.rs
T
chess 36f0dbe796 Register Windows desktop uninstall ownership before sandbox setup (#45224)
## Why

Desktop uninstall cleanup needs an installation owner even when the user has not signed in or configured the Windows sandbox. Recording ownership only during provisioning leaves those installations unregistered.

## What changed

- Add an authenticated installation registration request and attempt it during Windows desktop stdio initialization, with a five-second timeout before the initialization response.
- Persist ownership independently of provisioning, preserve existing desktop ownership, and prevent another user or home from replacing the registered owner.
- Validate write authority and retain directory handles and guards to protect the registered home against junction conversion through privileged cleanup.
- Preserve existing CLI homes during desktop uninstall while removing their `CodexSandboxUsers` ACL entries. Report ACL revocation errors and avoid propagating unchanged ACLs.
- Grant the owner `WRITE_DAC` on `.sandbox-bin` and allow elevated-helper fallback when older permissions need repair or the service cannot establish an uninstall watcher.

## Testing

Add tests for installation registration without sandbox settings and for preserving a child's null DACL when revoking an absent SID from its parent.

GitOrigin-RevId: fb48923e9d76758d1bf5b50c7305aa60f91629db
2026-09-13 13:06:40 +00:00

88 lines
2.9 KiB
Rust

use crate::acl::revoke_ace;
use crate::deny_read_acl::apply_deny_read_acls;
use crate::deny_read_acl::lexical_path_key;
use crate::setup::sandbox_dir;
use anyhow::Context;
use anyhow::Result;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::ffi::c_void;
use std::path::Path;
use std::path::PathBuf;
const DENY_READ_ACL_STATE_FILE: &str = "deny_read_acl_state.json";
#[derive(Default, Deserialize, Serialize)]
struct PersistentDenyReadAclState {
principals: BTreeMap<String, Vec<PathBuf>>,
}
/// Reconciles the persistent deny-read ACEs owned by one sandbox principal.
///
/// Workspace-write and elevated sandbox sessions intentionally leave ACLs in
/// place after a command exits, because descendants may outlive the launcher.
/// That makes the ACL set stateful across runs. Persist the paths applied for
/// 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.
///
/// # Safety
/// Caller must pass a valid SID pointer matching `principal_sid`.
pub unsafe fn sync_persistent_deny_read_acls(
codex_home: &Path,
principal_sid: &str,
desired_paths: &[PathBuf],
psid: *mut c_void,
) -> Result<Vec<PathBuf>> {
let state_path = sandbox_dir(codex_home).join(DENY_READ_ACL_STATE_FILE);
let mut state = load_state(&state_path)?;
let previous_paths = state
.principals
.get(principal_sid)
.cloned()
.unwrap_or_default();
let applied_paths = unsafe { apply_deny_read_acls(desired_paths, psid) }?;
let desired_keys = applied_paths
.iter()
.map(|path| lexical_path_key(path))
.collect::<HashSet<_>>();
for path in previous_paths {
if !desired_keys.contains(&lexical_path_key(&path)) {
let _ = revoke_ace(&path, psid);
}
}
if applied_paths.is_empty() {
state.principals.remove(principal_sid);
} else {
state
.principals
.insert(principal_sid.to_string(), applied_paths.clone());
}
store_state(&state_path, &state)?;
Ok(applied_paths)
}
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()))
}
}
}
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()))
}