mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
## Why The split filesystem policy stack already supports exact and glob `access = none` read restrictions on macOS and Linux. Windows still needed subprocess handling for those deny-read policies without claiming enforcement from a backend that cannot provide it. ## Key finding The unelevated restricted-token backend cannot safely enforce deny-read overlays. Its `WRITE_RESTRICTED` token model is authoritative for write checks, not read denials, so this PR intentionally fails that backend closed when deny-read overrides are present instead of claiming unsupported enforcement. ## What changed This PR adds the Windows deny-read enforcement layer and makes the backend split explicit: - Resolves Windows deny-read filesystem policy entries into concrete ACL targets. - Preserves exact missing paths so they can be materialized and denied before an enforceable sandboxed process starts. - Snapshot-expands existing glob matches into ACL targets for Windows subprocess enforcement. - Honors `glob_scan_max_depth` when expanding Windows deny-read globs. - Plans both the configured lexical path and the canonical target for existing paths so reparse-point aliases are covered. - Threads deny-read overrides through the elevated/logon-user Windows sandbox backend and unified exec. - Applies elevated deny-read ACLs synchronously before command launch rather than delegating them to the background read-grant helper. - Reconciles persistent deny-read ACEs per sandbox principal so policy changes do not leave stale deny-read ACLs behind. - Fails closed on the unelevated restricted-token backend when deny-read overrides are present, because its `WRITE_RESTRICTED` token model is not authoritative for read denials. ## Landed prerequisites These prerequisite PRs are already on `main`: 1. #15979 `feat(permissions): add glob deny-read policy support` 2. #18096 `feat(sandbox): add glob deny-read platform enforcement` 3. #17740 `feat(config): support managed deny-read requirements` This PR targets `main` directly and contains only the Windows deny-read enforcement layer. ## Implementation notes - Exact deny-read paths remain enforceable on the elevated path even when they do not exist yet: Windows materializes the missing path before applying the deny ACE, so the sandboxed command cannot create and read it during the same run. - Existing exact deny paths are preserved lexically until the ACL planner, which then adds the canonical target as a second ACL target when needed. That keeps both the configured alias and the resolved object covered. - Windows ACLs do not consume Codex glob syntax directly, so glob deny-read entries are expanded to the concrete matches that exist before process launch. - Glob traversal deduplicates directory visits within each pattern walk to avoid cycles, without collapsing distinct lexical roots that happen to resolve to the same target. - Persistent deny-read ACL state is keyed by sandbox principal SID, so cleanup only removes ACEs owned by the same backend principal. - Deny-read ACEs are fail-closed on the elevated path: setup aborts if mandatory deny-read ACL application fails. - Unelevated restricted-token sessions reject deny-read overrides early instead of running with a silently unenforceable read policy. ## Verification - `cargo test -p codex-core windows_restricted_token_rejects_unreadable_split_carveouts` - `just fmt` - `just fix -p codex-core` - `just fix -p codex-windows-sandbox` - GitHub Actions rerun is in progress on the pushed head. --------- Co-authored-by: Codex <noreply@openai.com>
239 lines
8.1 KiB
Rust
239 lines
8.1 KiB
Rust
use crate::dpapi;
|
|
use crate::logging::debug_log;
|
|
use crate::policy::SandboxPolicy;
|
|
use crate::setup::SandboxNetworkIdentity;
|
|
use crate::setup::SandboxUserRecord;
|
|
use crate::setup::SandboxUsersFile;
|
|
use crate::setup::SetupMarker;
|
|
use crate::setup::gather_read_roots;
|
|
use crate::setup::gather_write_roots;
|
|
use crate::setup::offline_proxy_settings_from_env;
|
|
use crate::setup::run_elevated_setup;
|
|
use crate::setup::run_setup_refresh_with_overrides;
|
|
use crate::setup::sandbox_users_path;
|
|
use crate::setup::setup_marker_path;
|
|
use anyhow::Context;
|
|
use anyhow::Result;
|
|
use anyhow::anyhow;
|
|
use base64::Engine;
|
|
use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
|
|
use std::collections::HashMap;
|
|
use std::fs;
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Debug, Clone)]
|
|
struct SandboxIdentity {
|
|
username: String,
|
|
password: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct SandboxCreds {
|
|
pub username: String,
|
|
pub password: String,
|
|
}
|
|
|
|
/// Returns true when the on-disk setup artifacts exist and match the current
|
|
/// setup version.
|
|
///
|
|
/// This is a coarse readiness check; `require_logon_sandbox_creds` performs the
|
|
/// additional runtime validation for offline firewall settings.
|
|
pub fn sandbox_setup_is_complete(codex_home: &Path) -> bool {
|
|
let marker_ok = matches!(load_marker(codex_home), Ok(Some(marker)) if marker.version_matches());
|
|
if !marker_ok {
|
|
return false;
|
|
}
|
|
matches!(load_users(codex_home), Ok(Some(users)) if users.version_matches())
|
|
}
|
|
|
|
fn load_marker(codex_home: &Path) -> Result<Option<SetupMarker>> {
|
|
let path = setup_marker_path(codex_home);
|
|
let marker = match fs::read_to_string(&path) {
|
|
Ok(contents) => match serde_json::from_str::<SetupMarker>(&contents) {
|
|
Ok(m) => Some(m),
|
|
Err(err) => {
|
|
debug_log(
|
|
&format!("sandbox setup marker parse failed: {err}"),
|
|
Some(codex_home),
|
|
);
|
|
None
|
|
}
|
|
},
|
|
Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
|
|
Err(err) => {
|
|
debug_log(
|
|
&format!("sandbox setup marker read failed: {err}"),
|
|
Some(codex_home),
|
|
);
|
|
None
|
|
}
|
|
};
|
|
Ok(marker)
|
|
}
|
|
|
|
fn load_users(codex_home: &Path) -> Result<Option<SandboxUsersFile>> {
|
|
let path = sandbox_users_path(codex_home);
|
|
let file = match fs::read_to_string(&path) {
|
|
Ok(contents) => contents,
|
|
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
|
Err(err) => {
|
|
debug_log(
|
|
&format!("sandbox users read failed: {err}"),
|
|
Some(codex_home),
|
|
);
|
|
return Ok(None);
|
|
}
|
|
};
|
|
match serde_json::from_str::<SandboxUsersFile>(&file) {
|
|
Ok(users) => Ok(Some(users)),
|
|
Err(err) => {
|
|
debug_log(
|
|
&format!("sandbox users parse failed: {err}"),
|
|
Some(codex_home),
|
|
);
|
|
Ok(None)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn decode_password(record: &SandboxUserRecord) -> Result<String> {
|
|
let blob = BASE64_STANDARD
|
|
.decode(record.password.as_bytes())
|
|
.context("base64 decode password")?;
|
|
let decrypted = dpapi::unprotect(&blob)?;
|
|
let pwd = String::from_utf8(decrypted).context("sandbox password not utf-8")?;
|
|
Ok(pwd)
|
|
}
|
|
|
|
fn select_identity(
|
|
network_identity: SandboxNetworkIdentity,
|
|
codex_home: &Path,
|
|
) -> Result<Option<SandboxIdentity>> {
|
|
let _marker = match load_marker(codex_home)? {
|
|
Some(m) if m.version_matches() => m,
|
|
_ => return Ok(None),
|
|
};
|
|
let users = match load_users(codex_home)? {
|
|
Some(u) if u.version_matches() => u,
|
|
_ => return Ok(None),
|
|
};
|
|
let chosen = match network_identity {
|
|
SandboxNetworkIdentity::Offline => users.offline,
|
|
SandboxNetworkIdentity::Online => users.online,
|
|
};
|
|
let password = decode_password(&chosen)?;
|
|
Ok(Some(SandboxIdentity {
|
|
username: chosen.username,
|
|
password,
|
|
}))
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn require_logon_sandbox_creds(
|
|
policy: &SandboxPolicy,
|
|
policy_cwd: &Path,
|
|
command_cwd: &Path,
|
|
env_map: &HashMap<String, String>,
|
|
codex_home: &Path,
|
|
read_roots_override: Option<&[PathBuf]>,
|
|
read_roots_include_platform_defaults: bool,
|
|
write_roots_override: Option<&[PathBuf]>,
|
|
deny_read_paths_override: &[PathBuf],
|
|
deny_write_paths_override: &[PathBuf],
|
|
proxy_enforced: bool,
|
|
) -> Result<SandboxCreds> {
|
|
let sandbox_dir = crate::setup::sandbox_dir(codex_home);
|
|
let needed_read = read_roots_override
|
|
.map(<[PathBuf]>::to_vec)
|
|
.unwrap_or_else(|| gather_read_roots(command_cwd, policy, codex_home));
|
|
let needed_write = write_roots_override
|
|
.map(<[PathBuf]>::to_vec)
|
|
.unwrap_or_else(|| gather_write_roots(policy, policy_cwd, command_cwd, env_map));
|
|
let network_identity = SandboxNetworkIdentity::from_policy(policy, proxy_enforced);
|
|
let desired_offline_proxy_settings = offline_proxy_settings_from_env(env_map, network_identity);
|
|
// NOTE: Do not add CODEX_HOME/.sandbox to `needed_write`; it must remain non-writable by the
|
|
// restricted capability token. The setup helper's `lock_sandbox_dir` is responsible for
|
|
// granting the sandbox group access to this directory without granting the capability SID.
|
|
let mut setup_reason: Option<String> = None;
|
|
|
|
let mut identity = match load_marker(codex_home)? {
|
|
Some(marker) if marker.version_matches() => {
|
|
if let Some(reason) =
|
|
marker.request_mismatch_reason(network_identity, &desired_offline_proxy_settings)
|
|
{
|
|
setup_reason = Some(reason);
|
|
None
|
|
} else {
|
|
let selected = select_identity(network_identity, codex_home)?;
|
|
if selected.is_none() {
|
|
setup_reason = Some(
|
|
"sandbox users missing or incompatible with marker version".to_string(),
|
|
);
|
|
}
|
|
selected
|
|
}
|
|
}
|
|
_ => {
|
|
setup_reason = Some("sandbox setup marker missing or incompatible".to_string());
|
|
None
|
|
}
|
|
};
|
|
|
|
if identity.is_none() {
|
|
if let Some(reason) = &setup_reason {
|
|
crate::logging::log_note(
|
|
&format!("sandbox setup required: {reason}"),
|
|
Some(&sandbox_dir),
|
|
);
|
|
} else {
|
|
crate::logging::log_note("sandbox setup required", Some(&sandbox_dir));
|
|
}
|
|
run_elevated_setup(
|
|
crate::setup::SandboxSetupRequest {
|
|
policy,
|
|
policy_cwd,
|
|
command_cwd,
|
|
env_map,
|
|
codex_home,
|
|
proxy_enforced,
|
|
},
|
|
crate::setup::SetupRootOverrides {
|
|
read_roots: Some(needed_read.clone()),
|
|
read_roots_include_platform_defaults,
|
|
write_roots: Some(needed_write.clone()),
|
|
deny_read_paths: Some(deny_read_paths_override.to_vec()),
|
|
deny_write_paths: Some(deny_write_paths_override.to_vec()),
|
|
},
|
|
)?;
|
|
identity = select_identity(network_identity, codex_home)?;
|
|
}
|
|
// Always refresh ACLs (non-elevated) for current roots via the setup binary.
|
|
run_setup_refresh_with_overrides(
|
|
crate::setup::SandboxSetupRequest {
|
|
policy,
|
|
policy_cwd,
|
|
command_cwd,
|
|
env_map,
|
|
codex_home,
|
|
proxy_enforced,
|
|
},
|
|
crate::setup::SetupRootOverrides {
|
|
read_roots: Some(needed_read),
|
|
read_roots_include_platform_defaults,
|
|
write_roots: Some(needed_write),
|
|
deny_read_paths: Some(deny_read_paths_override.to_vec()),
|
|
deny_write_paths: Some(deny_write_paths_override.to_vec()),
|
|
},
|
|
)?;
|
|
let identity = identity.ok_or_else(|| {
|
|
anyhow!(
|
|
"Windows sandbox setup is missing or out of date; rerun the sandbox setup with elevation"
|
|
)
|
|
})?;
|
|
Ok(SandboxCreds {
|
|
username: identity.username,
|
|
password: identity.password,
|
|
})
|
|
}
|