mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
add deny ACEs for world writable dirs (#7022)
Our Restricted Token contains 3 SIDs (Logon, Everyone, {WorkspaceWrite
Capability || ReadOnly Capability})
because it must include Everyone, that left us vulnerable to directories
that allow writes to Everyone. Even though those directories do not have
ACEs that enable our capability SIDs to write to them, they could still
be written to even in ReadOnly mode, or even in WorkspaceWrite mode if
they are outside of a writable root.
A solution to this is to explicitly add *Deny* ACEs to these
directories, always for the ReadOnly Capability SID, and for the
WorkspaceWrite SID if the directory is outside of a workspace root.
Under a restricted token, Windows always checks Deny ACEs before Allow
ACEs so even though our restricted token would allow a write to these
directories due to the Everyone SID, it fails first because of the Deny
ACE on the capability SID
This commit is contained in:
1 parent
54ee302a06
commit
3f92ad4190
4 files changed
+224
-3
No files matched your search
@@ -1,4 +1,11 @@
|
||||
use crate::acl::add_deny_write_ace;
|
||||
use crate::cap::cap_sid_file;
|
||||
use crate::cap::load_or_create_cap_sids;
|
||||
use crate::logging::log_note;
|
||||
use crate::policy::SandboxPolicy;
|
||||
use crate::token::convert_string_sid_to_sid;
|
||||
use crate::token::world_sid;
|
||||
use anyhow::anyhow;
|
||||
use crate::winutil::to_wide;
|
||||
use anyhow::Result;
|
||||
use std::collections::HashMap;
|
||||
@@ -271,6 +278,13 @@ pub fn audit_everyone_writable(
|
||||
for p in &flagged {
|
||||
list.push_str(&format!("\n - {}", p.display()));
|
||||
}
|
||||
crate::logging::log_note(
|
||||
&format!(
|
||||
"AUDIT: world-writable scan FAILED; cwd={cwd:?}; checked={checked}; duration_ms={elapsed_ms}; flagged:{}",
|
||||
list
|
||||
),
|
||||
logs_base_dir,
|
||||
);
|
||||
|
||||
return Ok(flagged);
|
||||
}
|
||||
@@ -282,6 +296,70 @@ pub fn audit_everyone_writable(
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
pub fn apply_capability_denies_for_world_writable(
|
||||
codex_home: &Path,
|
||||
flagged: &[PathBuf],
|
||||
sandbox_policy: &SandboxPolicy,
|
||||
cwd: &Path,
|
||||
logs_base_dir: Option<&Path>,
|
||||
) -> Result<()> {
|
||||
if flagged.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
std::fs::create_dir_all(codex_home)?;
|
||||
let cap_path = cap_sid_file(codex_home);
|
||||
let caps = load_or_create_cap_sids(codex_home);
|
||||
std::fs::write(&cap_path, serde_json::to_string(&caps)?)?;
|
||||
let (active_sid, workspace_roots): (*mut c_void, Vec<PathBuf>) = match sandbox_policy {
|
||||
SandboxPolicy::WorkspaceWrite { writable_roots, .. } => {
|
||||
let sid = unsafe { convert_string_sid_to_sid(&caps.workspace) }
|
||||
.ok_or_else(|| anyhow!("ConvertStringSidToSidW failed for workspace capability"))?;
|
||||
let mut roots: Vec<PathBuf> =
|
||||
vec![dunce::canonicalize(cwd).unwrap_or_else(|_| cwd.to_path_buf())];
|
||||
for root in writable_roots {
|
||||
let candidate = if root.is_absolute() {
|
||||
root.clone()
|
||||
} else {
|
||||
cwd.join(root)
|
||||
};
|
||||
roots.push(dunce::canonicalize(&candidate).unwrap_or(candidate));
|
||||
}
|
||||
(sid, roots)
|
||||
}
|
||||
SandboxPolicy::ReadOnly => (
|
||||
unsafe { convert_string_sid_to_sid(&caps.readonly) }.ok_or_else(|| {
|
||||
anyhow!("ConvertStringSidToSidW failed for readonly capability")
|
||||
})?,
|
||||
Vec::new(),
|
||||
),
|
||||
SandboxPolicy::DangerFullAccess => {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
for path in flagged {
|
||||
if workspace_roots.iter().any(|root| path.starts_with(root)) {
|
||||
continue;
|
||||
}
|
||||
let res = unsafe { add_deny_write_ace(path, active_sid) };
|
||||
match res {
|
||||
Ok(true) => log_note(
|
||||
&format!("AUDIT: applied capability deny ACE to {}", path.display()),
|
||||
logs_base_dir,
|
||||
),
|
||||
Ok(false) => {}
|
||||
Err(err) => log_note(
|
||||
&format!(
|
||||
"AUDIT: failed to apply capability deny ACE to {}: {}",
|
||||
path.display(),
|
||||
err
|
||||
),
|
||||
logs_base_dir,
|
||||
),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn normalize_windows_path_for_display(p: impl AsRef<Path>) -> String {
|
||||
let canon = dunce::canonicalize(p.as_ref()).unwrap_or_else(|_| p.as_ref().to_path_buf());
|
||||
canon.display().to_string().replace('/', "\\")
|
||||
|
||||
Reference in new issue
Block a user