mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
## What changed - Centralize `windows-sys` at version `0.61.2` in the workspace and migrate dependent crates to it. - Adapt Windows API calls to the updated handle, boolean, and type definitions. Replace custom handle owners with `OwnedHandle` and use `BorrowedHandle` where ownership stays with the caller. - Add explicit wrappers for non-owning Windows handles passed between threads. ## Testing Enable the MCP refresh cleanup test on Windows. Retain process handles to verify that a superseded MCP server stays alive during an in-flight call, exits after cancellation, and leaves the replacement running until shutdown. GitOrigin-RevId: 86f53d9b300d13edff7154f4a7c32e34dacd2919
84 lines
3.0 KiB
Rust
84 lines
3.0 KiB
Rust
//! Serializes sandbox account and network changes across setup and uninstall.
|
|
//! The acquiring thread must hold the lock until those changes finish.
|
|
|
|
use std::io;
|
|
use std::marker::PhantomData;
|
|
use std::os::windows::io::AsRawHandle;
|
|
use std::os::windows::io::FromRawHandle;
|
|
use std::os::windows::io::OwnedHandle;
|
|
use std::ptr::null_mut;
|
|
|
|
use anyhow::Context;
|
|
use anyhow::Result;
|
|
use anyhow::bail;
|
|
use windows_sys::Win32::Foundation::HANDLE;
|
|
use windows_sys::Win32::Foundation::HLOCAL;
|
|
use windows_sys::Win32::Foundation::LocalFree;
|
|
use windows_sys::Win32::Foundation::WAIT_ABANDONED;
|
|
use windows_sys::Win32::Foundation::WAIT_OBJECT_0;
|
|
use windows_sys::Win32::Foundation::WAIT_TIMEOUT;
|
|
use windows_sys::Win32::Security::Authorization::ConvertStringSecurityDescriptorToSecurityDescriptorW;
|
|
use windows_sys::Win32::Security::SECURITY_ATTRIBUTES;
|
|
use windows_sys::Win32::System::Threading::CreateMutexW;
|
|
use windows_sys::Win32::System::Threading::ReleaseMutex;
|
|
use windows_sys::Win32::System::Threading::WaitForSingleObject;
|
|
|
|
use crate::winutil::to_wide;
|
|
|
|
/// Holds the machine-wide setup lock on the thread that acquired it.
|
|
#[must_use]
|
|
pub struct SandboxSetupLock {
|
|
handle: OwnedHandle,
|
|
_thread_bound: PhantomData<*mut ()>,
|
|
}
|
|
|
|
impl Drop for SandboxSetupLock {
|
|
fn drop(&mut self) {
|
|
unsafe {
|
|
ReleaseMutex(self.handle.as_raw_handle() as HANDLE);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Waits up to `timeout_ms` for exclusive access by LocalSystem or an elevated administrator.
|
|
pub fn acquire_sandbox_setup_lock(timeout_ms: u32) -> Result<SandboxSetupLock> {
|
|
let sddl = to_wide("D:P(A;;GA;;;SY)(A;;GA;;;BA)");
|
|
let mut descriptor = null_mut();
|
|
if unsafe {
|
|
ConvertStringSecurityDescriptorToSecurityDescriptorW(
|
|
sddl.as_ptr(),
|
|
/*stringsdrevision*/ 1,
|
|
&mut descriptor,
|
|
null_mut(),
|
|
)
|
|
} == 0
|
|
{
|
|
return Err(io::Error::last_os_error()).context("create sandbox setup mutex security");
|
|
}
|
|
let attributes = SECURITY_ATTRIBUTES {
|
|
nLength: size_of::<SECURITY_ATTRIBUTES>() as u32,
|
|
lpSecurityDescriptor: descriptor,
|
|
bInheritHandle: 0,
|
|
};
|
|
let name = to_wide(r"Global\CodexSandboxSetup");
|
|
let handle = unsafe {
|
|
CreateMutexW(&attributes, /*binitialowner*/ 0, name.as_ptr())
|
|
};
|
|
let result = if handle.is_null() {
|
|
Err(io::Error::last_os_error())
|
|
} else {
|
|
Ok(unsafe { OwnedHandle::from_raw_handle(handle as _) })
|
|
};
|
|
unsafe { LocalFree(descriptor as HLOCAL) };
|
|
let handle = result.context("open sandbox setup mutex")?;
|
|
match unsafe { WaitForSingleObject(handle.as_raw_handle() as HANDLE, timeout_ms) } {
|
|
// A crashed owner releases the lock; setup still repairs any incomplete account changes.
|
|
WAIT_OBJECT_0 | WAIT_ABANDONED => Ok(SandboxSetupLock {
|
|
handle,
|
|
_thread_bound: PhantomData,
|
|
}),
|
|
WAIT_TIMEOUT => bail!("timed out waiting for sandbox setup mutex after {timeout_ms} ms"),
|
|
_ => Err(io::Error::last_os_error()).context("wait for sandbox setup mutex"),
|
|
}
|
|
}
|