mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Clean up Windows sandbox resources on app uninstall (#42375)
## What changed - Record the authenticated sandbox owner and observe package uninstall events across service restarts and session changes. - During uninstall, serialize setup and cleanup, disable sandbox accounts, stop their processes, and remove sandbox directories, firewall and WFP rules, hidden-user entries, accounts, and the sandbox group. - Restrict desktop-owned directory cleanup to pinned paths and perform it while impersonating the authenticated owner. - Detect missing or disabled sandbox accounts after interrupted cleanup and reprovision them before restoring network access. GitOrigin-RevId: 7d63fff7ddcff3eb01d018653146df12044bd277
This commit is contained in:
1 parent
a14ef02e1c
commit
665e5f45ab
25 files changed
+1349
-97
No files matched your search
@@ -0,0 +1,83 @@
|
||||
//! 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 == 0 {
|
||||
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"),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user