mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Preserve Windows managed deny reads in the sandbox CLI (#42835)
## Why Windows sandbox setup reconciles persistent deny ACLs against the paths supplied for a session. The `codex sandbox --permission-profile` path supplied an empty list, which could discard the profile's deny-read restrictions. ## What changed Resolve deny-read paths from the selected permission profile and workspace roots before starting a Windows sandbox session, and pass them through to setup. ## Testing Add a Windows CLI integration test that launches the same managed-deny profile twice and verifies that denied file reads and DLL loads remain blocked while allowed access still works. GitOrigin-RevId: 6ec8b6a13a33b862d7b152eadbbdde3146de4b7d
This commit is contained in:
1 parent
f1aac1e885
commit
a482e65b86
6 files changed
+272
-1
No files matched your search
@@ -0,0 +1,13 @@
|
||||
// Keep the ACL probe independent of test-harness startup, which writes CODEX_HOME.
|
||||
#[cfg(target_os = "windows")]
|
||||
mod win;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
fn main() {
|
||||
win::main();
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn main() {
|
||||
panic!("codex-windows-managed-deny-probe is Windows-only");
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
use std::ffi::c_void;
|
||||
use std::io::Write as _;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[link(name = "kernel32")]
|
||||
unsafe extern "system" {
|
||||
#[link_name = "LoadLibraryW"]
|
||||
fn load_library_w(file_name: *const u16) -> *mut c_void;
|
||||
#[link_name = "FreeLibrary"]
|
||||
fn free_library(module: *mut c_void) -> i32;
|
||||
}
|
||||
|
||||
fn load_library(path: &Path) -> std::io::Result<()> {
|
||||
let wide_path: Vec<u16> = path
|
||||
.as_os_str()
|
||||
.encode_wide()
|
||||
.chain(std::iter::once(0))
|
||||
.collect();
|
||||
// SAFETY: `wide_path` is NUL-terminated and remains alive for the call.
|
||||
let module = unsafe { load_library_w(wide_path.as_ptr()) };
|
||||
if module.is_null() {
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
// SAFETY: `module` is a non-null handle returned by `LoadLibraryW` above.
|
||||
if unsafe { free_library(module) } == 0 {
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn access_was_denied(err: &std::io::Error) -> bool {
|
||||
err.kind() == std::io::ErrorKind::PermissionDenied || err.raw_os_error() == Some(5)
|
||||
}
|
||||
|
||||
fn probe_path(key: &str) -> PathBuf {
|
||||
let Some(value) = std::env::var_os(key) else {
|
||||
eprintln!("managed deny probe is missing {key}");
|
||||
probe_exit(/*code*/ 21);
|
||||
};
|
||||
PathBuf::from(value)
|
||||
}
|
||||
|
||||
fn probe_exit(code: i32) -> ! {
|
||||
let _ = std::io::stdout().flush();
|
||||
let _ = std::io::stderr().flush();
|
||||
std::process::exit(code);
|
||||
}
|
||||
|
||||
pub(super) fn main() {
|
||||
let allowed_text = probe_path("CODEX_WINDOWS_ALLOWED_TEXT");
|
||||
let denied_text = probe_path("CODEX_WINDOWS_DENIED_TEXT");
|
||||
let allowed_module = probe_path("CODEX_WINDOWS_ALLOWED_MODULE");
|
||||
let denied_module = probe_path("CODEX_WINDOWS_DENIED_MODULE");
|
||||
|
||||
match std::fs::read_to_string(&allowed_text) {
|
||||
Ok(contents) if contents.trim() == "ALLOW-CONTROL" => println!("allowed-read:OK"),
|
||||
Ok(_) => {
|
||||
eprintln!("allowed read returned unexpected contents");
|
||||
probe_exit(/*code*/ 21);
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("allowed read failed: {err}");
|
||||
probe_exit(/*code*/ 21);
|
||||
}
|
||||
}
|
||||
if let Err(err) = load_library(&allowed_module) {
|
||||
eprintln!("allowed import failed: {err}");
|
||||
probe_exit(/*code*/ 21);
|
||||
}
|
||||
println!("allowed-import:OK");
|
||||
|
||||
let mut failures = 0;
|
||||
match std::fs::read(&denied_text) {
|
||||
Ok(_) => {
|
||||
println!("denied-read:UNEXPECTED_SUCCESS");
|
||||
failures += 1;
|
||||
}
|
||||
Err(err) if access_was_denied(&err) => println!("denied-read:DENIED"),
|
||||
Err(err) => {
|
||||
eprintln!("denied read failed for a non-access reason: {err}");
|
||||
probe_exit(/*code*/ 21);
|
||||
}
|
||||
}
|
||||
match load_library(&denied_module) {
|
||||
Ok(()) => {
|
||||
println!("denied-import:UNEXPECTED_SUCCESS");
|
||||
failures += 1;
|
||||
}
|
||||
Err(err) if access_was_denied(&err) => println!("denied-import:DENIED"),
|
||||
Err(err) => {
|
||||
eprintln!("denied import failed for a non-access reason: {err}");
|
||||
probe_exit(/*code*/ 21);
|
||||
}
|
||||
}
|
||||
|
||||
if failures != 0 {
|
||||
probe_exit(/*code*/ 20);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user