Add service-managed package registration for Windows sandbox accounts (#45542)

## What changed

- Add an explicit `registered_core` provisioning mode that registers the app package for both managed sandbox accounts and records their runner aliases. Authenticate callers against the service's package family and installed version.
- Persist runtime ownership and publish readiness only after both registrations and AppData metadata grants succeed. Prevent legacy setup or another owner from replacing registered sandbox accounts, and restrict `refresh_only` requests to an existing ready setup.
- Scope service and pipe names by package identity.
- Preserve registrations across package updates and clean them up when the owner uninstalls the app. Fence provisioning during teardown and defer package removal until native cleanup succeeds and the service exits.

## Testing

Add tests for registration opt-in, ownership and readiness checks, legacy setup isolation, metadata ACL preservation, listener shutdown handling, and the cleanup finalizer's commit barrier and UTF-8 input.

GitOrigin-RevId: 69e52f7463285b51f3bef831f071245f0eca0838
This commit is contained in:
zm-oai authored and copyberry committed 2026-09-14 23:24:10 +00:00
1 parent 18d7ace221
commit 6ae5e71458
50 files changed
+3515 -169

No files matched your search

@@ -3,5 +3,6 @@ load("//:defs.bzl", "codex_rust_crate")
codex_rust_crate(
name = "windows-sandbox-service",
binary_test_target_compatible_with = ["@platforms//os:windows"],
compile_data = ["src/registered_runtime/removal.ps1"],
crate_name = "codex_windows_sandbox_service",
)
+4 -4
View File
@@ -16,10 +16,6 @@ path = "src/main.rs"
[lints]
workspace = true
# Temporary until #8415 adds the cleanup implementation that uses these dependencies.
[package.metadata.cargo-shear]
ignored = ["base64", "serde_json"]
[dependencies]
anyhow = { workspace = true }
base64 = { workspace = true }
@@ -38,6 +34,8 @@ pretty_assertions = { workspace = true }
version = "0.58"
features = [
"ApplicationModel",
"Foundation_Collections",
"Management_Deployment",
"Win32_System_WinRT",
]
@@ -54,12 +52,14 @@ features = [
"Win32_System_Environment",
"Win32_System_EventLog",
"Win32_System_IO",
"Win32_System_JobObjects",
"Win32_System_Memory",
"Win32_System_Pipes",
"Win32_System_Registry",
"Win32_System_RemoteDesktop",
"Win32_System_Services",
"Win32_System_SystemServices",
"Win32_System_SystemInformation",
"Win32_System_Threading",
"Win32_UI_Shell",
]
@@ -9,11 +9,16 @@ use std::ptr;
use anyhow::Context;
use anyhow::Result;
pub(crate) use codex_windows_sandbox::CORE_INSTALLATION_KEY;
pub(crate) use codex_windows_sandbox::DesktopInstallation;
pub(crate) use codex_windows_sandbox::INSTALLATION_KEY;
pub(crate) use codex_windows_sandbox::INSTALLATION_VALUE;
pub(crate) use codex_windows_sandbox::InstallationRecord;
pub(crate) use codex_windows_sandbox::load_sandbox_installation as load;
pub(crate) use codex_windows_sandbox::remove_sandbox_installation as remove;
pub(crate) use codex_windows_sandbox::save_sandbox_installation as save;
pub(crate) use codex_windows_sandbox::RuntimeAccountRegistration;
pub(crate) use codex_windows_sandbox::RuntimeRegistration;
pub(crate) use codex_windows_sandbox::load_installation as load;
pub(crate) use codex_windows_sandbox::remove_installation as remove;
pub(crate) use codex_windows_sandbox::save_installation as save_runtime;
use codex_windows_sandbox::validate_local_directory_path;
use windows_sys::Win32::Foundation as foundation;
use windows_sys::Win32::UI::Shell::GetUserProfileDirectoryW;
@@ -40,3 +45,33 @@ pub(crate) fn read_desktop_installation(
cache_home,
})
}
pub(crate) fn load_runtime() -> Result<Option<InstallationRecord>> {
Ok(load()?.filter(|record| record.runtime.is_some()))
}
pub(crate) fn is_current_package_family(record: &InstallationRecord) -> Result<bool> {
Ok(record.runtime()?.package_family.eq_ignore_ascii_case(
&windows::ApplicationModel::Package::Current()?
.Id()?
.FamilyName()?
.to_string(),
))
}
pub(crate) fn save(mut record: InstallationRecord) -> Result<InstallationRecord> {
let _setup_lock = codex_windows_sandbox::acquire_sandbox_setup_lock(/*timeout_ms*/ 5_000)?;
record.runtime = None;
if let Some(current) = load_runtime()? {
let family = windows::ApplicationModel::Package::Current()?
.Id()?
.FamilyName()?;
current.admit_owner(&record, &family.to_string())?;
// The helper wait may have admitted another account. Preserve fresh Core
// state instead of overwriting it with the caller's owner-only snapshot.
record.runtime = current.runtime;
}
save_runtime(&record)?;
Ok(record)
}
+49 -15
View File
@@ -22,8 +22,7 @@ use codex_windows_sandbox::write_provisioning_frame;
pub(crate) use home::OwnedHandle;
pub(crate) use home::pin_directory;
pub(crate) use home::pin_existing_ancestors;
#[cfg(test)]
use request::ProvisioningRequest;
pub(crate) use request::ProvisioningRequest;
pub(crate) use request::ServiceRequest;
use request::validate_request;
use std::mem::size_of;
@@ -40,8 +39,6 @@ use windows_sys::Win32::System::Pipes as pipes;
use crate::installation_record::InstallationRecord;
pub(crate) const PIPE_NAME: &str = codex_windows_sandbox::SANDBOX_PROVISIONING_PIPE_NAME;
const MAX_REQUEST_BYTES: usize = 4096;
const MAX_RESPONSE_MESSAGE_BYTES: usize = 512;
const REQUEST_IDLE_TIMEOUT: Duration = Duration::from_secs(5);
@@ -68,19 +65,31 @@ enum PipeConnection {
pub(crate) fn run(
shutdown: Arc<AtomicBool>,
on_ready: impl FnOnce() -> Result<()>,
register_installation: impl Fn(InstallationRecord, OwnedHandle) -> Result<InstallationRecord>,
on_authenticated_user: impl Fn(
InstallationRecord,
OwnedHandle,
codex_windows_sandbox::SetupRuntime,
) -> Result<InstallationRecord>,
on_session_change: impl Fn() -> Result<()>,
) -> Result<()> {
let listener = listener::ProvisioningListener::open()?;
if shutdown.load(Ordering::Acquire) {
return Ok(());
}
let mut listener = listener::ProvisioningListener::open()?;
on_ready().context("publish provisioning listener readiness")?;
while !shutdown.load(Ordering::Acquire) {
// Refresh before blocking even if a direct setup claim's zero-byte wake was lost.
while refresh_session(&shutdown, &on_session_change)
.context("refresh the recorded sandbox owner")?
{
let connection = accept_pipe_connection(listener.pipe.0)?;
if shutdown.load(Ordering::Acquire) {
// A session refresh can finish cleanup. Never dispatch an already accepted
// connection after it stops the listener, including a disconnected wakeup.
if !refresh_session(&shutdown, &on_session_change)
.context("restore the signed-in user's uninstall listener")?
{
break;
}
// Session-change wakeups close the pipe immediately and can arrive disconnected.
on_session_change().context("restore the signed-in user's uninstall listener")?;
if connection == PipeConnection::Disconnected {
continue;
}
@@ -98,12 +107,14 @@ pub(crate) fn run(
&authorized_process,
&listener.sandbox_sid,
&shutdown,
&register_installation,
&on_authenticated_user,
);
let response = match result {
Ok(response) => response,
Err(error) if error.is::<crate::registered_runtime::RegistrationInterrupted>() => {
return Err(error);
}
Err(error) if error.is::<ServiceUnavailable>() => {
eprintln!("sandbox provisioning service unavailable: {error:#}");
SandboxProvisioningResponse::Unavailable
}
Err(error) => {
@@ -118,10 +129,23 @@ pub(crate) fn run(
message: ProvisioningMessage::ProvisionSandboxResponse { payload: response },
};
write_response(&listener.pipe, &response, &shutdown)?;
unsafe { pipes::DisconnectNamedPipe(listener.pipe.0) };
if shutdown.load(Ordering::Acquire) {
break;
}
listener = listener.refresh()?;
}
Ok(())
}
fn refresh_session(
shutdown: &AtomicBool,
on_session_change: impl FnOnce() -> Result<()>,
) -> Result<bool> {
if shutdown.load(Ordering::Acquire) {
return Ok(false);
}
on_session_change()?;
Ok(!shutdown.load(Ordering::Acquire))
}
/// Sends one frame, then waits briefly for the client to close before disconnecting.
fn write_response(
@@ -234,7 +258,11 @@ fn handle_request(
authorized_process: &crate::package_identity::AuthorizedClientProcess,
sandbox_sid: &[u8],
shutdown: &AtomicBool,
register_installation: &dyn Fn(InstallationRecord, OwnedHandle) -> Result<InstallationRecord>,
on_authenticated_user: &dyn Fn(
InstallationRecord,
OwnedHandle,
codex_windows_sandbox::SetupRuntime,
) -> Result<InstallationRecord>,
) -> Result<SandboxProvisioningResponse> {
let deadline = Instant::now() + REQUEST_IDLE_TIMEOUT;
let mut request = [0_u8; MAX_REQUEST_BYTES];
@@ -316,7 +344,13 @@ fn handle_request(
return Err(error)
.context("requested sandbox settings violate administrator-controlled machine policy");
}
crate::provisioning::run(identity, request, register_installation)
crate::provisioning::run(
identity,
request,
sandbox_sid,
shutdown,
on_authenticated_user,
)
}
fn is_config_parse_error(error: &anyhow::Error) -> bool {
@@ -4,6 +4,7 @@ use anyhow::Context;
use anyhow::Result;
use anyhow::bail;
use codex_windows_sandbox::DirectoryOpenDisposition;
use codex_windows_sandbox::SetupRuntime;
use codex_windows_sandbox::create_directory_guard;
use codex_windows_sandbox::string_from_sid_bytes;
use std::ffi::OsString;
@@ -28,6 +29,8 @@ pub(crate) struct ClientIdentity {
pub(crate) codex_home: PathBuf,
pub(crate) user_sid: String,
pub(crate) session_id: u32,
// The requested route, authenticated against the held client's installed image.
pub(crate) runtime: SetupRuntime,
pub(crate) token: OwnedHandle,
pub(crate) desktop_installation: Option<crate::installation_record::DesktopInstallation>,
// Pins and guards remain live through registration and the provisioning helper.
@@ -74,6 +77,14 @@ fn authenticate_impersonated_client(
sandbox_sid: &[u8],
request: &ServiceRequest,
) -> Result<ClientIdentity> {
// Pin the group generation through identity capture.
// Release this lock before the provisioning worker checks machine policy.
let _setup_lock = codex_windows_sandbox::acquire_sandbox_setup_lock(/*timeout_ms*/ 5_000)?;
anyhow::ensure!(
codex_windows_sandbox::resolve_sid(codex_windows_sandbox::SANDBOX_USERS_GROUP)
.is_ok_and(|current| current == sandbox_sid),
codex_windows_sandbox::SANDBOX_GROUP_CHANGED
);
if unsafe { pipes::ImpersonateNamedPipeClient(pipe) } == 0 {
return Err(std::io::Error::last_os_error()).context("impersonate provisioning client");
}
@@ -130,12 +141,21 @@ fn authenticate_impersonated_client(
let user_sid = string_from_sid_bytes(&user).map_err(anyhow::Error::msg)?;
let account = unsafe { codex_windows_sandbox::account_name_from_sid(user.as_ptr() as _) }
.context("resolve provisioning client account")?;
let runtime = crate::package_identity::authorize_setup_runtime(authorized_process, request)?;
let requested_home = match request {
ServiceRequest::RegisterInstallation { codex_home } => codex_home,
ServiceRequest::ProvisionSandbox(request) => &request.codex_home,
};
let (codex_home, handles) = match request {
ServiceRequest::ProvisionSandbox(_) => prepare_codex_home(requested_home)?,
ServiceRequest::ProvisionSandbox(request) => prepare_codex_home(
requested_home,
runtime,
if request.refresh_only {
DirectoryOpenDisposition::OpenExisting
} else {
DirectoryOpenDisposition::OpenOrCreate
},
)?,
ServiceRequest::RegisterInstallation { .. } => {
// Registration must not create the sandbox directories or change their ACLs.
codex_windows_sandbox::validate_local_directory_path(requested_home)?;
@@ -195,6 +215,7 @@ fn authenticate_impersonated_client(
codex_home,
user_sid,
session_id: session,
runtime,
token,
desktop_installation,
directory_handles: handles,
@@ -5,6 +5,7 @@ use anyhow::Context;
use anyhow::Result;
use anyhow::bail;
use codex_windows_sandbox::DirectoryOpenDisposition;
use codex_windows_sandbox::SetupRuntime;
use codex_windows_sandbox::create_directory_guard;
use codex_windows_sandbox::open_directory_no_reparse;
use codex_windows_sandbox::to_wide;
@@ -33,7 +34,15 @@ impl Drop for OwnedHandle {
}
}
pub(super) fn prepare_codex_home(requested: &Path) -> Result<(PathBuf, Vec<OwnedHandle>)> {
pub(super) fn prepare_codex_home(
requested: &Path,
runtime: SetupRuntime,
disposition: DirectoryOpenDisposition,
) -> Result<(PathBuf, Vec<OwnedHandle>)> {
let directory_count = match runtime {
SetupRuntime::Registered => 3,
SetupRuntime::Legacy => 4,
};
let mut handles = Vec::new();
validate_local_directory_path(requested)?;
let requested_root = requested
@@ -52,7 +61,7 @@ pub(super) fn prepare_codex_home(requested: &Path) -> Result<(PathBuf, Vec<Owned
handles.push(pin_directory(
requested,
filesystem::FILE_READ_ATTRIBUTES,
DirectoryOpenDisposition::OpenOrCreate,
disposition,
)?);
let home = requested
.canonicalize()
@@ -76,6 +85,7 @@ pub(super) fn prepare_codex_home(requested: &Path) -> Result<(PathBuf, Vec<Owned
&sandbox_bin,
]
.into_iter()
.take(directory_count)
.enumerate()
{
let mut access = filesystem::FILE_READ_ATTRIBUTES
@@ -84,7 +94,7 @@ pub(super) fn prepare_codex_home(requested: &Path) -> Result<(PathBuf, Vec<Owned
if index != 0 {
access |= filesystem::WRITE_DAC;
}
let handle = pin_directory(directory, access, DirectoryOpenDisposition::OpenOrCreate)
let handle = pin_directory(directory, access, disposition)
.map_err(|error| {
// Older elevated installs omitted WRITE_DAC; let the interactive helper repair it.
if directory == sandbox_bin.as_path()
@@ -1,7 +1,9 @@
//! Owns the provisioning pipe and its security descriptor for the listener lifetime.
//! Owns a provisioning pipe and the sandbox group generation in its deny rule.
//! Replaces stale listeners only after a response, closing the old pipe before creating another.
use anyhow::Context;
use anyhow::Result;
use anyhow::bail;
use codex_windows_sandbox::ensure_sandbox_users_group;
use codex_windows_sandbox::string_from_sid_bytes;
use codex_windows_sandbox::to_wide;
@@ -25,14 +27,39 @@ pub(super) struct ProvisioningListener {
impl ProvisioningListener {
pub(super) fn open() -> Result<Self> {
let pipe_name = codex_windows_sandbox::windows_sandbox_service_pipe_name()?;
let _setup_lock =
codex_windows_sandbox::acquire_sandbox_setup_lock(/*timeout_ms*/ 5_000)?;
if crate::installation_record::load()?
.and_then(|record| record.runtime)
.is_some_and(|runtime| runtime.retiring.is_some())
{
bail!("interrupted sandbox cleanup requires repair");
}
let sandbox_sid = ensure_sandbox_users_group()?;
let (descriptor, pipe) = create_provisioning_pipe(super::PIPE_NAME, &sandbox_sid)?;
let (descriptor, pipe) = create_provisioning_pipe(&pipe_name, &sandbox_sid)?;
Ok(Self {
pipe,
sandbox_sid,
_descriptor: descriptor,
})
}
pub(super) fn refresh(self) -> Result<Self> {
let current_group = {
let _setup_lock =
codex_windows_sandbox::acquire_sandbox_setup_lock(/*timeout_ms*/ 5_000)?;
codex_windows_sandbox::resolve_sid(codex_windows_sandbox::SANDBOX_USERS_GROUP)
};
if current_group.is_ok_and(|current| current == self.sandbox_sid) {
unsafe { pipes::DisconnectNamedPipe(self.pipe.0) };
return Ok(self);
}
// A stale deny SID was refused before dispatch. Close this first-instance
// pipe before rebuilding its descriptor for the current group generation.
drop(self);
Self::open()
}
}
struct SecurityDescriptor(security::PSECURITY_DESCRIPTOR);
@@ -20,8 +20,10 @@ pub(crate) enum ServiceRequest {
#[derive(Debug, Eq, PartialEq)]
pub(crate) struct ProvisioningRequest {
pub(super) codex_home: PathBuf,
pub(super) listeners: WindowsSandboxProxyListeners,
pub(crate) codex_home: PathBuf,
pub(crate) registered_core: bool,
pub(crate) refresh_only: bool,
pub(crate) listeners: WindowsSandboxProxyListeners,
pub(crate) settings: WindowsSandboxProvisioningSettings,
}
@@ -36,6 +38,7 @@ pub(super) fn validate_request(request: &[u8]) -> Result<ServiceRequest> {
if !reader.is_empty() {
bail!("provisioning requests must contain exactly one IPC frame");
}
if frame.version != PROVISIONING_PROTOCOL_VERSION {
bail!(
"unsupported provisioning request version: {}",
@@ -53,6 +56,9 @@ pub(super) fn validate_request(request: &[u8]) -> Result<ServiceRequest> {
ProvisioningMessage::ProvisionSandboxResponse { .. } => bail!("expected a service request"),
};
validate_home(&request.codex_home)?;
if request.refresh_only && !request.registered_core {
bail!("registration refresh requires registered Core");
}
let mut settings = request.settings;
let mut listeners = request.listeners;
for ports in [
@@ -76,6 +82,8 @@ pub(super) fn validate_request(request: &[u8]) -> Result<ServiceRequest> {
}
Ok(ServiceRequest::ProvisionSandbox(ProvisioningRequest {
codex_home: PathBuf::from(request.codex_home),
registered_core: request.registered_core,
refresh_only: request.refresh_only,
listeners,
settings,
}))
@@ -1,18 +1,22 @@
use super::OwnedHandle;
use super::PipeConnection;
use super::ProvisioningRequest;
use super::ServiceRequest;
use super::accept_pipe_connection;
use super::home::prepare_codex_home;
use super::is_config_parse_error;
use super::pin_existing_ancestors;
use super::pipe_security_descriptor;
use super::refresh_session;
use super::request::ProvisioningRequest;
use super::validate_request;
use super::wake;
use codex_windows_sandbox::DirectoryOpenDisposition;
use codex_windows_sandbox::FramedProvisioningMessage;
use codex_windows_sandbox::PROVISIONING_PROTOCOL_VERSION;
use codex_windows_sandbox::ProvisioningMessage;
use codex_windows_sandbox::SandboxProvisioningRequest;
use codex_windows_sandbox::SandboxProvisioningResponse;
use codex_windows_sandbox::SetupRuntime;
use codex_windows_sandbox::WindowsSandboxProvisioningSettings;
use codex_windows_sandbox::WindowsSandboxProxyListeners;
use codex_windows_sandbox::read_provisioning_frame;
@@ -22,6 +26,7 @@ use pretty_assertions::assert_eq;
use std::path::Path;
use std::path::PathBuf;
use std::ptr;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::sync::mpsc;
@@ -32,6 +37,141 @@ use windows_sys::Win32::Storage::FileSystem as filesystem;
use windows_sys::Win32::Storage::Packaging::Appx;
use windows_sys::Win32::System::Pipes as pipes;
#[test]
fn session_refresh_observes_cleanup_completion_before_dispatch() {
let shutdown = AtomicBool::new(false);
assert!(
!refresh_session(&shutdown, || {
shutdown.store(true, Ordering::Release);
Ok(())
})
.unwrap()
);
}
#[test]
fn session_refresh_preserves_authoritative_stop() {
let shutdown = AtomicBool::new(true);
assert!(!refresh_session(&shutdown, || panic!("stopped listener must not refresh")).unwrap());
assert!(shutdown.load(Ordering::Acquire));
}
#[test]
fn session_refresh_keeps_admission_open_without_cleanup() {
let shutdown = AtomicBool::new(false);
assert!(refresh_session(&shutdown, || Ok(())).unwrap());
assert!(!shutdown.load(Ordering::Acquire));
}
#[test]
fn session_refresh_propagates_cleanup_failure() {
let shutdown = AtomicBool::new(false);
let error = refresh_session(&shutdown, || anyhow::bail!("cleanup failed")).unwrap_err();
assert!(error.to_string().contains("cleanup failed"));
}
#[test]
fn provisioning_request_rejects_malformed_registered_flag() {
for registered_core in [
serde_json::json!("true"),
serde_json::json!(1),
serde_json::json!(null),
] {
let bytes = serde_json::to_vec(&serde_json::json!({
"version": PROVISIONING_PROTOCOL_VERSION,
"type": "provision_sandbox_request",
"payload": {
"codex_home": "C:\\Users\\owner\\.codex",
"registered_core": registered_core,
"settings": WindowsSandboxProvisioningSettings::default(),
"listeners": WindowsSandboxProxyListeners::default(),
},
}))
.unwrap();
let mut frame = (bytes.len() as u32).to_le_bytes().to_vec();
frame.extend(bytes);
assert!(validate_request(&frame).is_err());
}
}
#[test]
fn runtime_registration_is_an_explicit_wire_opt_in() {
let request = SandboxProvisioningRequest {
codex_home: r"C:\Users\owner\.codex".to_string(),
registered_core: false,
refresh_only: false,
settings: WindowsSandboxProvisioningSettings::default(),
listeners: WindowsSandboxProxyListeners::default(),
};
let serialized = serde_json::to_value(&request).unwrap();
assert_eq!(
serialized,
serde_json::json!({
"codex_home": request.codex_home,
"settings": request.settings,
"listeners": request.listeners,
}),
);
let legacy: SandboxProvisioningRequest = serde_json::from_value(serialized).unwrap();
assert!(matches!(
validate_request(&framed_request(legacy)).unwrap(),
ServiceRequest::ProvisionSandbox(ProvisioningRequest {
registered_core: false,
refresh_only: false,
..
})
));
let registered = SandboxProvisioningRequest {
registered_core: true,
refresh_only: false,
..request
};
assert!(matches!(
validate_request(&framed_request(registered)).unwrap(),
ServiceRequest::ProvisionSandbox(ProvisioningRequest {
registered_core: true,
refresh_only: false,
..
})
));
}
#[test]
fn registration_refresh_is_explicit_and_requires_registered_core() {
let mut request = SandboxProvisioningRequest {
codex_home: r"C:\Users\owner\.codex".to_string(),
registered_core: false,
refresh_only: true,
settings: WindowsSandboxProvisioningSettings::default(),
listeners: WindowsSandboxProxyListeners::default(),
};
assert!(validate_request(&framed_request(request.clone())).is_err());
request.registered_core = true;
assert!(matches!(
validate_request(&framed_request(request)).unwrap(),
ServiceRequest::ProvisionSandbox(ProvisioningRequest {
registered_core: true,
refresh_only: true,
..
})
));
}
#[test]
fn runtime_registration_has_no_unpackaged_or_foreground_identity_exemption() {
let service = Some("OpenAI.Codex_testpublisher");
assert!(crate::package_identity::require_runtime_package_family(service, service).is_ok());
for (client, service) in [
(None, service),
(service, None),
(None, None),
(Some("OpenAI.CodexBeta_testpublisher"), service),
(Some("OpenAI.Codex_otherpublisher"), service),
] {
assert!(crate::package_identity::require_runtime_package_family(client, service).is_err());
}
}
fn framed_request(request: SandboxProvisioningRequest) -> Vec<u8> {
let mut frame = Vec::new();
write_provisioning_frame(
@@ -110,6 +250,8 @@ fn config_parse_errors_are_distinguished_from_policy_and_io_failures() {
fn provisioning_request_preserves_home_spaces_and_unicode() {
let request = framed_request(SandboxProvisioningRequest {
codex_home: "D:\\Codex Homes\\Jos\u{00e9}\\.codex".to_string(),
registered_core: false,
refresh_only: false,
settings: WindowsSandboxProvisioningSettings::default(),
listeners: WindowsSandboxProxyListeners::default(),
});
@@ -117,6 +259,8 @@ fn provisioning_request_preserves_home_spaces_and_unicode() {
validate_request(&request).unwrap(),
ServiceRequest::ProvisionSandbox(ProvisioningRequest {
codex_home: PathBuf::from("D:\\Codex Homes\\Jos\u{00e9}\\.codex"),
registered_core: false,
refresh_only: false,
listeners: WindowsSandboxProxyListeners::default(),
settings: WindowsSandboxProvisioningSettings::default(),
})
@@ -130,6 +274,8 @@ fn structured_provisioning_request_carries_normalized_proxy_settings() {
{
let request = framed_request(SandboxProvisioningRequest {
codex_home: "D:\\Codex Homes\\Jos\u{00e9}\\.codex".to_string(),
registered_core: false,
refresh_only: false,
settings: WindowsSandboxProvisioningSettings {
proxy_ports: vec![http_port, socks_port, http_port],
allow_local_binding: true,
@@ -143,6 +289,8 @@ fn structured_provisioning_request_carries_normalized_proxy_settings() {
validate_request(&request).unwrap(),
ServiceRequest::ProvisionSandbox(ProvisioningRequest {
codex_home: PathBuf::from("D:\\Codex Homes\\Jos\u{00e9}\\.codex"),
registered_core: false,
refresh_only: false,
listeners: WindowsSandboxProxyListeners {
http_ports: vec![http_port],
socks_ports: vec![socks_port],
@@ -173,6 +321,8 @@ fn structured_provisioning_request_accepts_independent_and_additional_proxy_port
};
let request = framed_request(SandboxProvisioningRequest {
codex_home: r"C:\Users\alice\.codex".to_string(),
registered_core: false,
refresh_only: false,
settings: settings.clone(),
listeners: listeners.clone(),
});
@@ -180,6 +330,8 @@ fn structured_provisioning_request_accepts_independent_and_additional_proxy_port
validate_request(&request).unwrap(),
ServiceRequest::ProvisionSandbox(ProvisioningRequest {
codex_home: PathBuf::from(r"C:\Users\alice\.codex"),
registered_core: false,
refresh_only: false,
settings,
listeners,
})
@@ -191,6 +343,8 @@ fn structured_provisioning_request_accepts_independent_and_additional_proxy_port
fn structured_provisioning_request_accepts_disabled_listeners() {
let request = framed_request(SandboxProvisioningRequest {
codex_home: r"C:\Users\alice\.codex".to_string(),
registered_core: false,
refresh_only: false,
settings: WindowsSandboxProvisioningSettings::default(),
listeners: WindowsSandboxProxyListeners::default(),
});
@@ -198,6 +352,8 @@ fn structured_provisioning_request_accepts_disabled_listeners() {
validate_request(&request).unwrap(),
ServiceRequest::ProvisionSandbox(ProvisioningRequest {
codex_home: PathBuf::from(r"C:\Users\alice\.codex"),
registered_core: false,
refresh_only: false,
listeners: WindowsSandboxProxyListeners::default(),
settings: WindowsSandboxProvisioningSettings::default(),
})
@@ -208,6 +364,8 @@ fn structured_provisioning_request_accepts_disabled_listeners() {
fn structured_provisioning_request_requires_exact_version_fields_and_framing() {
let valid = SandboxProvisioningRequest {
codex_home: r"C:\Users\alice\.codex".to_string(),
registered_core: false,
refresh_only: false,
settings: WindowsSandboxProvisioningSettings::default(),
listeners: WindowsSandboxProxyListeners::default(),
};
@@ -254,6 +412,8 @@ fn structured_provisioning_request_rejects_invalid_or_inconsistent_ports() {
] {
let request = framed_request(SandboxProvisioningRequest {
codex_home: r"C:\Users\alice\.codex".to_string(),
registered_core: false,
refresh_only: false,
settings: WindowsSandboxProvisioningSettings {
proxy_ports,
allow_local_binding: false,
@@ -272,6 +432,8 @@ fn provisioning_request_rejects_empty_control_characters_and_invalid_utf8() {
for home in ["", "C:\\safe\0evil", "C:\\safe\rmore", "C:\\safe\nmore"] {
let request = framed_request(SandboxProvisioningRequest {
codex_home: home.to_string(),
registered_core: false,
refresh_only: false,
settings: WindowsSandboxProvisioningSettings::default(),
listeners: WindowsSandboxProxyListeners::default(),
});
@@ -280,6 +442,8 @@ fn provisioning_request_rejects_empty_control_characters_and_invalid_utf8() {
let mut invalid_utf8 = framed_request(SandboxProvisioningRequest {
codex_home: r"C:\Users\alice\.codex".to_string(),
registered_core: false,
refresh_only: false,
settings: WindowsSandboxProvisioningSettings::default(),
listeners: WindowsSandboxProxyListeners::default(),
});
@@ -499,6 +663,71 @@ fn pipe_descriptor_denies_sandbox_group_before_interactive_users() {
assert!(deny < interactive);
}
#[test]
fn registered_home_preparation_does_not_touch_the_legacy_bin() -> anyhow::Result<()> {
let root = std::env::temp_dir().join(format!(
"codex-service-home-{:?}",
windows::core::GUID::new()?
));
std::fs::create_dir(&root)?;
let result = (|| -> anyhow::Result<()> {
let home = root.join(".codex");
std::fs::create_dir(&home)?;
let bin = home.join(".sandbox-bin");
let marker = b"legacy bin is deliberately not a directory";
std::fs::write(&bin, marker)?;
assert!(
prepare_codex_home(
&home,
SetupRuntime::Registered,
DirectoryOpenDisposition::OpenExisting,
)
.is_err()
);
assert!(!home.join(".sandbox").exists());
assert!(!home.join(".sandbox-secrets").exists());
let (_, handles) = prepare_codex_home(
&home,
SetupRuntime::Registered,
DirectoryOpenDisposition::OpenOrCreate,
)?;
assert!(home.join(".sandbox").is_dir());
assert!(home.join(".sandbox-secrets").is_dir());
assert_eq!(std::fs::read(&bin)?.as_slice(), marker.as_slice());
drop(handles);
// Legacy still validates and pins the copied-bin path.
let (_, handles) = prepare_codex_home(
&home,
SetupRuntime::Registered,
DirectoryOpenDisposition::OpenExisting,
)?;
drop(handles);
assert!(
prepare_codex_home(
&home,
SetupRuntime::Legacy,
DirectoryOpenDisposition::OpenOrCreate,
)
.is_err()
);
assert_eq!(std::fs::read(&bin)?.as_slice(), marker.as_slice());
std::fs::remove_file(&bin)?;
let (_, handles) = prepare_codex_home(
&home,
SetupRuntime::Legacy,
DirectoryOpenDisposition::OpenOrCreate,
)?;
assert!(bin.is_dir());
drop(handles);
Ok(())
})();
std::fs::remove_dir_all(&root)?;
result
}
#[test]
fn installation_registration_requires_no_sandbox_settings() {
let mut frame = Vec::new();
@@ -4,6 +4,7 @@ use anyhow::Result;
mod installation_record;
#[cfg(windows)]
mod ipc;
#[cfg(windows)]
mod machine_policy;
#[cfg(windows)]
@@ -13,6 +14,8 @@ mod package_lifecycle;
#[cfg(windows)]
mod provisioning;
#[cfg(windows)]
mod registered_runtime;
#[cfg(windows)]
mod service;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -15,6 +15,11 @@ use windows_sys::Win32::Security as security;
use windows_sys::Win32::System::Pipes;
use windows_sys::Win32::System::Threading;
mod registered;
pub(crate) use registered::authorize_setup_runtime;
#[cfg(test)]
pub(crate) use registered::require_runtime_package_family;
#[cfg(debug_assertions)]
static FOREGROUND_MODE: AtomicBool = AtomicBool::new(false);
@@ -0,0 +1,75 @@
//! Admits registered Core only from the service's installed package version and
//! prevents legacy setup from replacing accounts owned by a registered runtime.
use anyhow::Context;
use anyhow::Result;
use anyhow::bail;
use codex_windows_sandbox::SetupRuntime;
use windows_sys::Win32::System::Threading;
use super::AuthorizedClientProcess;
use crate::ipc::ServiceRequest;
pub(crate) fn authorize_setup_runtime(
process: &AuthorizedClientProcess,
request: &ServiceRequest,
) -> Result<SetupRuntime> {
match request {
ServiceRequest::ProvisionSandbox(request) if request.registered_core => {
authorize_runtime_client_process(process)?;
Ok(SetupRuntime::Registered)
}
ServiceRequest::ProvisionSandbox(_) => {
anyhow::ensure!(
crate::installation_record::load_runtime()?.is_none(),
"registered Core owns these sandbox accounts; legacy provisioning is not permitted"
);
Ok(SetupRuntime::Legacy)
}
ServiceRequest::RegisterInstallation { .. } => Ok(SetupRuntime::Legacy),
}
}
fn authorize_runtime_client_process(process: &AuthorizedClientProcess) -> Result<()> {
let client_family = unsafe { codex_windows_sandbox::process_package_family(process.handle.0) }
.context("read runtime registration client package family")?;
let service_family =
unsafe { codex_windows_sandbox::process_package_family(Threading::GetCurrentProcess()) }
.context("read runtime registration service package family")?;
require_runtime_package_family(client_family.as_deref(), service_family.as_deref())?;
let mut image = [0u16; 32768];
let mut length = image.len() as u32;
if unsafe {
Threading::QueryFullProcessImageNameW(process.handle.0, 0, image.as_mut_ptr(), &mut length)
} == 0
{
return Err(std::io::Error::last_os_error()).context("read provisioning client image");
}
let image = std::path::PathBuf::from(String::from_utf16(&image[..length as usize])?);
let image = image.canonicalize()?;
let service = std::env::current_exe()?.canonicalize()?;
let directory = service.parent().context("service image has no directory")?;
anyhow::ensure!(
["codex.exe", "codex-code-mode-host.exe"]
.iter()
.any(|name| {
image
.as_os_str()
.eq_ignore_ascii_case(directory.join(name).as_os_str())
}),
"registered Core caller is not from this installed service version"
);
Ok(())
}
pub(crate) fn require_runtime_package_family(
client: Option<&str>,
service: Option<&str>,
) -> Result<()> {
match (client, service) {
(Some(client), Some(service)) if client == service => Ok(()),
_ => bail!(
"runtime registration requires a client in the service's installed package family"
),
}
}
@@ -1,4 +1,5 @@
//! Restores owner-scoped uninstall notifications and ties file cleanup to pinned directories.
//! Registered runtime policy stays in its private module; owner tokens and pins stay here.
use std::cell::RefCell;
use std::io;
@@ -7,13 +8,14 @@ use std::os::windows::io::IntoRawHandle;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use anyhow::Context;
use anyhow::Result;
use anyhow::ensure;
use codex_windows_sandbox::DirectoryOpenDisposition;
use codex_windows_sandbox::PreparedWindowsSandboxCleanup;
use codex_windows_sandbox::create_directory_guard;
use codex_windows_sandbox::prepare_packaged_windows_sandbox_cleanup;
use codex_windows_sandbox::string_from_sid_bytes;
use windows::ApplicationModel::Package;
use windows::ApplicationModel::PackageCatalog;
@@ -36,6 +38,9 @@ use crate::installation_record::InstallationRecord;
use crate::ipc::OwnedHandle;
mod cleanup;
mod registered;
pub(crate) use registered::runtime_owner_removed;
struct UserInstallation {
codex_home: Option<PathBuf>,
@@ -87,7 +92,7 @@ impl PackageLifecycle {
.desktop_installation
.or(record.desktop_installation);
}
crate::installation_record::save(&record)?;
record = crate::installation_record::save(record)?;
if let Some(installation) = active.as_mut()
&& installation.codex_home.is_some()
{
@@ -143,6 +148,7 @@ impl PackageLifecycle {
)?;
let package_name = self.package_name.clone();
let uninstalling = Arc::clone(&self.uninstalling);
let owner_sid = record.user_sid.clone();
let token = catalog
.PackageUninstalling(&TypedEventHandler::<
PackageCatalog,
@@ -151,7 +157,7 @@ impl PackageLifecycle {
if let Some(event) = event
&& event.Package()?.Id()?.FullName()? == package_name
{
uninstalling.store(!event.IsComplete()?, Ordering::Release);
registered::on_package_uninstalling(event, &owner_sid, &uninstalling)?;
}
Ok(())
}))
@@ -208,6 +214,11 @@ impl PackageLifecycle {
return Ok(());
};
if record.runtime.is_some()
&& !crate::installation_record::is_current_package_family(&record)?
{
return Ok(());
}
let mut raw_token = 0;
if unsafe { WTSQueryUserToken(session_id, &mut raw_token) } == 0 {
return Err(io::Error::last_os_error()).context("open the logged-in user's token");
@@ -231,11 +242,29 @@ impl PackageLifecycle {
}
pub(crate) fn clean_up(&self) -> Result<()> {
let _setup_lock =
codex_windows_sandbox::acquire_sandbox_setup_lock(/*timeout_ms*/ 5_000)?;
if let Some(record) = crate::installation_record::load_runtime()? {
return registered::clean_up(self, record);
}
// Preserve the legacy path's ownership retirement before native preparation.
crate::installation_record::remove()?;
self.clean_up_resources(
&prepare_packaged_windows_sandbox_cleanup()?,
/*runtime*/ None,
)
}
fn clean_up_resources(
&self,
prepared: &PreparedWindowsSandboxCleanup,
runtime: Option<&InstallationRecord>,
) -> Result<()> {
let mut installation = self.installation.borrow_mut();
let installation = installation
.as_mut()
.context("the authenticated package installation was not recorded")?;
cleanup::clean_up(installation)
cleanup::clean_up(installation, prepared, runtime)
}
}
@@ -1,29 +1,43 @@
//! Removes one authenticated owner's sandbox resources during package uninstall.
//! Desktop paths are deleted only while impersonating the owner; native cleanup keeps its order.
//! Removes one authenticated owner's sandbox resources using prepared native cleanup.
//! Owner impersonation, directory pins, and registration-aware cleanup order are preserved.
use std::io;
use anyhow::Context;
use anyhow::Result;
use anyhow::ensure;
use codex_windows_sandbox::clean_up_packaged_windows_sandbox;
use codex_windows_sandbox::PreparedWindowsSandboxCleanup;
use codex_windows_sandbox::resolve_sid;
use codex_windows_sandbox::revoke_ace;
use super::UserInstallation;
use super::with_owner_impersonation;
use crate::installation_record::InstallationRecord;
pub(super) fn clean_up(installation: &mut UserInstallation) -> Result<()> {
pub(super) fn clean_up(
installation: &mut UserInstallation,
prepared: &PreparedWindowsSandboxCleanup,
runtime: Option<&InstallationRecord>,
) -> Result<()> {
crate::service::log_information(
crate::service::EVENT_CLEANUP_STARTED,
"sandbox uninstall cleanup started",
);
// A partial uninstall must not let a later install inherit stale directory ownership.
crate::installation_record::remove()?;
let codex_home = installation.codex_home.clone();
clean_up_packaged_windows_sandbox(codex_home.as_deref(), || {
// Privileged file cleanup is finished; release the guard before owner-scoped removal.
// Remove exact grants from the locked cleanup record before native account deletion.
if let Some(record) = runtime {
crate::registered_runtime::remove_metadata(installation.user_token.0, record)?;
}
let result = prepared.finish(codex_home.as_deref(), || {
// Release once, even when registered cleanup retries its remaining steps.
installation.directory_guard.take();
if let Some(record) = runtime
&& super::registered::runtime_owner_has_package(record)?
{
// Only the old sandbox resources are repaired on reinstall. Never remove
// the reinstalled app's desktop-created home or runtime cache.
return Ok(());
}
let Some(desktop) = &installation.record.desktop_installation else {
return Ok(());
};
@@ -38,7 +52,7 @@ pub(super) fn clean_up(installation: &mut UserInstallation) -> Result<()> {
}
};
if let Some(home) = &codex_home {
if desktop.created_codex_home {
if desktop.created_codex_home && runtime.is_none() {
// Release the home itself so it can be deleted; keep its ancestors pinned.
installation.directory_handles.pop();
record_error(std::fs::remove_dir_all(home));
@@ -73,8 +87,8 @@ pub(super) fn clean_up(installation: &mut UserInstallation) -> Result<()> {
);
Ok(())
})
})
.context("remove packaged Windows sandbox resources")?;
});
result.context("remove packaged Windows sandbox resources")?;
crate::service::log_information(
crate::service::EVENT_CLEANUP_FINISHED,
"sandbox uninstall cleanup finished",
@@ -0,0 +1,122 @@
//! Registered runtime ownership and uninstall policy for the shared package watcher.
//! Native cleanup stays inside the caller's setup lock and precedes package removal.
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use anyhow::Context;
use anyhow::Result;
use anyhow::ensure;
use codex_windows_sandbox::prepare_packaged_windows_sandbox_cleanup_with_retained_tokens;
use windows::ApplicationModel::PackageUninstallingEventArgs;
use windows::Management::Deployment::PackageManager;
use windows::core::HSTRING;
use super::PackageLifecycle;
use crate::installation_record::InstallationRecord;
pub(super) fn on_package_uninstalling(
event: &PackageUninstallingEventArgs,
owner_sid: &str,
uninstalling: &AtomicBool,
) -> windows::core::Result<()> {
let retention = crate::installation_record::load_runtime().and_then(|record| match record {
Some(record) if record.user_sid == owner_sid => {
crate::registered_runtime::has_retaining_registration(&record).map(Some)
}
Some(_) => Ok(None),
None => Ok(Some(false)),
});
match retention {
Ok(Some(true)) => {
// Removing an old version during update must not remove the
// managed user's registration. Check the whole family only
// after the owner's uninstall has completed.
if event.IsComplete()? {
crate::service::wake_listener();
}
}
Ok(None) => {}
Ok(Some(false)) => {
// No actual managed registration keeps this service installed,
// even if a failed attempt left intent. Retain SCM-stop cleanup.
uninstalling.store(!event.IsComplete()?, Ordering::Release);
}
Err(error) => crate::service::log_error(
crate::service::EVENT_SERVICE_FAILED,
&format!("unable to read registered runtime ownership: {error:#}"),
),
}
Ok(())
}
/// The shared cleanup entry holds the setup mutex until this function returns.
pub(super) fn clean_up(lifecycle: &PackageLifecycle, record: InstallationRecord) -> Result<()> {
if !crate::installation_record::is_current_package_family(&record)? {
return Ok(());
}
ensure!(
record.runtime()?.retiring.is_none(),
"interrupted runtime cleanup requires repair"
);
ensure!(
!runtime_owner_has_package(&record)?
|| (lifecycle.uninstalling.load(Ordering::Acquire)
&& !crate::registered_runtime::has_retaining_registration(&record)?),
"registered runtime owner still has the app installed"
);
ensure!(
lifecycle
.installation
.borrow()
.as_ref()
.is_some_and(|installation| {
installation.record.user_sid == record.user_sid
&& installation.codex_home.as_ref() == Some(&record.codex_home)
}),
"waiting for the authenticated runtime owner and home before sandbox file cleanup"
);
let record = crate::registered_runtime::prepare_cleanup(record)?;
let removal = {
let installation = lifecycle.installation.borrow();
let installation = installation
.as_ref()
.context("authenticated installation is missing")?;
crate::registered_runtime::prepare_removal(installation.user_token.0, &record)?
};
let prepared =
prepare_packaged_windows_sandbox_cleanup_with_retained_tokens(&removal.tokens())?;
// A reinstall before this boundary cancels without retiring resources.
ensure!(
(lifecycle.uninstalling.load(Ordering::Acquire)
&& !crate::registered_runtime::has_retaining_registration(&record)?)
|| !runtime_owner_has_package(&record)?,
"owner reinstalled before native cleanup started"
);
crate::installation_record::save_runtime(&record)?;
// Retry only in this live invocation, retaining the outer setup lock.
// A stored retirement fence is never proof that native cleanup finished.
crate::service::retry_cleanup(|| lifecycle.clean_up_resources(&prepared, Some(&record)))?;
removal.commit()
}
/// The managed registration must not keep the package alive after its owner
/// uninstalls it, but an update to another version of the family is not uninstall.
pub(crate) fn runtime_owner_removed() -> Result<bool> {
let Some(record) = crate::installation_record::load_runtime()? else {
return Ok(false);
};
Ok(
crate::installation_record::is_current_package_family(&record)?
&& !runtime_owner_has_package(&record)?,
)
}
// Callers have verified that the record belongs to this service's package family.
pub(super) fn runtime_owner_has_package(record: &InstallationRecord) -> Result<bool> {
let packages = PackageManager::new()?.FindPackagesByUserSecurityIdPackageFamilyName(
&HSTRING::from(&record.user_sid),
&HSTRING::from(&record.runtime()?.package_family),
)?;
Ok(packages.First()?.HasCurrent()?)
}
@@ -1,15 +1,18 @@
//! Runs provisioning after IPC has authenticated the owner and checked machine policy.
//! The authenticated token and directory pins retain their existing helper lifetimes.
//! Provisions an authenticated client's sandbox through its selected setup path.
mod registered;
use std::os::windows::fs::MetadataExt;
use std::os::windows::io::BorrowedHandle;
use std::os::windows::io::IntoRawHandle;
use std::sync::atomic::AtomicBool;
use anyhow::Context;
use anyhow::Result;
use anyhow::bail;
use codex_windows_sandbox::SandboxProvisioningResponse;
use codex_windows_sandbox::SetupRuntime;
use codex_windows_sandbox::run_elevated_provisioning_setup_with_retained_handles;
use codex_windows_sandbox::sandbox_setup_is_complete_with_settings;
use windows_sys::Win32::Storage::FileSystem as filesystem;
use crate::installation_record::InstallationRecord;
@@ -17,29 +20,82 @@ use crate::ipc::ClientIdentity;
use crate::ipc::OwnedHandle;
use crate::ipc::ServiceRequest;
// The lifecycle callback validates ownership and returns the canonical saved record.
fn register_owner(
identity: &ClientIdentity,
on_authenticated_user: &dyn Fn(
InstallationRecord,
OwnedHandle,
codex_windows_sandbox::SetupRuntime,
) -> Result<InstallationRecord>,
) -> Result<InstallationRecord> {
let token = unsafe { BorrowedHandle::borrow_raw(identity.token.0 as _) }
.try_clone_to_owned()
.context("retain authenticated uninstall owner")?;
on_authenticated_user(
InstallationRecord {
codex_home: identity.codex_home.clone(),
user_sid: identity.user_sid.clone(),
session_id: identity.session_id,
desktop_installation: identity.desktop_installation.clone(),
runtime: None,
},
OwnedHandle(token.into_raw_handle() as _),
identity.runtime,
)
}
fn setup_is_complete(
identity: &ClientIdentity,
settings: &codex_windows_sandbox::WindowsSandboxProvisioningSettings,
) -> Result<bool> {
crate::package_lifecycle::with_owner_impersonation(identity.token.0, || {
Ok(
codex_windows_sandbox::sandbox_setup_is_complete_with_settings(
&identity.codex_home,
settings,
),
)
})
}
pub(crate) fn run(
identity: ClientIdentity,
request: ServiceRequest,
register_installation: &dyn Fn(InstallationRecord, OwnedHandle) -> Result<InstallationRecord>,
sandbox_sid: &[u8],
shutdown: &AtomicBool,
on_authenticated_user: &dyn Fn(
InstallationRecord,
OwnedHandle,
codex_windows_sandbox::SetupRuntime,
) -> Result<InstallationRecord>,
) -> Result<SandboxProvisioningResponse> {
// A policy-rejected request must not choose the uninstall owner. Use the
// token already authenticated above instead of impersonating the pipe again.
register_installation(
InstallationRecord {
codex_home: identity.codex_home.clone(),
user_sid: identity.user_sid,
session_id: identity.session_id,
desktop_installation: identity.desktop_installation,
},
identity.token,
)?;
let request = match request {
ServiceRequest::RegisterInstallation { .. } => {
let installation = InstallationRecord {
codex_home: identity.codex_home,
user_sid: identity.user_sid,
session_id: identity.session_id,
desktop_installation: identity.desktop_installation,
runtime: None,
};
on_authenticated_user(installation, identity.token, identity.runtime)?;
return Ok(SandboxProvisioningResponse::Ok);
}
ServiceRequest::ProvisionSandbox(request) => request,
};
if sandbox_setup_is_complete_with_settings(&identity.codex_home, &request.settings) {
if identity.runtime == SetupRuntime::Registered {
return registered::run(
&identity,
request,
sandbox_sid,
shutdown,
on_authenticated_user,
);
}
let settings = request.settings;
register_owner(&identity, on_authenticated_user)?;
if setup_is_complete(&identity, &settings)? {
return Ok(SandboxProvisioningResponse::Ok);
}
let helper = std::env::current_exe()
@@ -65,7 +121,7 @@ pub(crate) fn run(
match run_elevated_provisioning_setup_with_retained_handles(
&identity.codex_home,
&identity.account,
request.settings,
settings,
&retained_handles,
) {
Ok(()) => {
@@ -0,0 +1,129 @@
//! The registered-Core setup transaction: admit, provision, register, then publish readiness.
use std::sync::atomic::AtomicBool;
use anyhow::Context;
use anyhow::Result;
use anyhow::ensure;
use codex_windows_sandbox::SandboxProvisioningResponse;
use crate::ipc::ProvisioningRequest;
use codex_windows_sandbox::string_from_sid_bytes;
use windows::ApplicationModel::Package;
use windows_sys::Win32::NetworkManagement::NetManagement::UF_ACCOUNTDISABLE;
use crate::installation_record::InstallationRecord;
use crate::installation_record::RuntimeRegistration;
use crate::ipc::ClientIdentity;
use crate::ipc::OwnedHandle;
pub(super) fn run(
identity: &ClientIdentity,
request: ProvisioningRequest,
sandbox_sid: &[u8],
shutdown: &AtomicBool,
on_authenticated_user: &dyn Fn(
InstallationRecord,
OwnedHandle,
codex_windows_sandbox::SetupRuntime,
) -> Result<InstallationRecord>,
) -> Result<SandboxProvisioningResponse> {
// Keep admission, account provisioning and package registration in one transaction.
let _setup_lock = codex_windows_sandbox::acquire_sandbox_setup_lock(/*timeout_ms*/ 5_000)?;
ensure!(
codex_windows_sandbox::resolve_sid(codex_windows_sandbox::SANDBOX_USERS_GROUP)
.is_ok_and(|current| current == sandbox_sid),
codex_windows_sandbox::SANDBOX_GROUP_CHANGED
);
let settings = request.settings;
let mut setup_complete = super::setup_is_complete(identity, &settings)?;
if request.refresh_only {
// Startup may update registrations, but must never create accounts or repair teardown.
// Check under the setup lock, before changing ownership or sandbox accounts.
let previous = crate::installation_record::load_runtime()?
.context("registration refresh requires completed setup")?;
let runtime = previous.runtime()?;
let ready = runtime
.ready_package
.as_deref()
.context("registration refresh requires a ready runtime")?;
ensure!(
previous.user_sid == identity.user_sid
&& previous.codex_home == identity.codex_home
&& crate::installation_record::is_current_package_family(&previous)?
&& runtime.ready_for_package(ready)
&& setup_complete,
"registration refresh requires unchanged, ready sandbox ownership and settings"
);
for entry in &runtime.accounts {
let account = entry.account.username();
ensure!(
codex_windows_sandbox::local_user_flags(account)?
.is_some_and(|flags| flags & UF_ACCOUNTDISABLE == 0)
&& string_from_sid_bytes(&codex_windows_sandbox::resolve_sid(account)?)
.map_err(anyhow::Error::msg)?
== entry.user_sid,
"registration refresh cannot replace a sandbox account"
);
}
}
let mut installation = super::register_owner(identity, on_authenticated_user)?;
let result = (|| -> Result<()> {
let package = Package::Current()?.Id()?;
installation.runtime.get_or_insert(RuntimeRegistration {
package_family: package.FamilyName()?.to_string(),
accounts: Vec::new(),
metadata_roots: Vec::new(),
ready_package: None,
retiring: None,
});
// Persist the authenticated owner before setup can rotate shared credentials.
crate::installation_record::save_runtime(&installation)?;
for account in [
codex_windows_sandbox::OFFLINE_USERNAME,
codex_windows_sandbox::ONLINE_USERNAME,
] {
setup_complete &= codex_windows_sandbox::local_user_flags(account)?
.is_some_and(|flags| flags & UF_ACCOUNTDISABLE == 0);
}
if !setup_complete {
// Account state can change outside our setup lock; refresh must never repair it.
ensure!(
!request.refresh_only,
"registration refresh cannot repair sandbox setup"
);
// Setup resolves this name for ACL trustees; it must still name the held token's user.
let setup_owner_sid = codex_windows_sandbox::resolve_sid(&identity.account)?;
ensure!(
string_from_sid_bytes(&setup_owner_sid).map_err(anyhow::Error::msg)?
== identity.user_sid,
"sandbox setup account no longer matches the authenticated owner"
);
codex_windows_sandbox::provision_sandbox_in_process(
&identity.codex_home,
&identity.account,
settings,
identity.runtime,
)?;
}
crate::registered_runtime::provision(
identity,
&mut installation,
&package.FullName()?,
shutdown,
)
})();
result
.inspect_err(|error| {
crate::service::log_error(
crate::service::EVENT_PROVISIONING_FAILED,
&format!("Codex sandbox provisioning failed: {error}"),
);
})
.context("registered sandbox provisioning failed")?;
crate::service::log_information(
crate::service::EVENT_PROVISIONING_SUCCEEDED,
"Codex sandbox provisioning completed successfully.",
);
Ok(SandboxProvisioningResponse::Ok)
}
@@ -0,0 +1,328 @@
//! Owns managed-account registration, AppData metadata access, and deferred removal.
//! Lifecycle and admission decisions stay with callers. Persist ownership before deployment;
//! retain profiles until Windows completes, or pending resources until service exit on shutdown.
use std::os::windows::io::AsRawHandle;
use std::os::windows::io::OwnedHandle;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Duration;
use anyhow::Context;
use anyhow::Result;
use anyhow::ensure;
use codex_windows_sandbox::APP_CORE_RUNNER_ALIAS;
use codex_windows_sandbox::SandboxRuntimeAccount;
use codex_windows_sandbox::logon_existing_sandbox_account;
use codex_windows_sandbox::resolve_sid as lookup_sid;
use codex_windows_sandbox::sandbox_secrets_dir;
use codex_windows_sandbox::string_from_sid_bytes;
use codex_windows_sandbox::to_wide;
use codex_windows_sandbox::token_groups;
use windows::Foundation::AsyncStatus;
use windows::Management::Deployment::DeploymentOptions;
use windows::Management::Deployment::PackageManager;
use windows::Win32::Foundation::BOOL;
use windows::core::HSTRING;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Security as security;
use windows_sys::Win32::System::SystemServices::SE_GROUP_ENABLED;
use windows_sys::Win32::UI::Shell::FOLDERID_LocalAppData;
use windows_sys::Win32::UI::Shell::LoadUserProfileW;
use windows_sys::Win32::UI::Shell::PROFILEINFOW;
use windows_sys::Win32::UI::Shell::UnloadUserProfile;
use crate::installation_record::InstallationRecord;
use crate::installation_record::RuntimeAccountRegistration;
use crate::ipc::ClientIdentity;
use crate::package_lifecycle::with_owner_impersonation;
mod known_folder;
mod metadata;
mod removal;
pub(crate) use metadata::remove as remove_metadata;
pub(crate) use removal::prepare as prepare_removal;
/// Must escape request handling: pending resources are retained until this service exits.
#[derive(Debug)]
pub(crate) struct RegistrationInterrupted;
impl std::fmt::Display for RegistrationInterrupted {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("runtime setup interrupted; stopping service")
}
}
impl std::error::Error for RegistrationInterrupted {}
struct AccountProfile {
user_sid: String,
token: OwnedHandle,
profile: HANDLE,
}
impl Drop for AccountProfile {
fn drop(&mut self) {
let _ = self.unload_profile();
}
}
impl AccountProfile {
fn unload_profile(&mut self) -> Result<()> {
if self.profile != 0 {
let unloaded =
unsafe { UnloadUserProfile(self.token.as_raw_handle() as _, self.profile) };
BOOL(unloaded)
.ok()
.context("unload managed runtime account profile")?;
self.profile = 0;
}
Ok(())
}
}
/// The caller holds the setup mutex from owner admission through readiness.
/// Provision both users together. No runner launch performs package deployment.
pub(crate) fn provision(
identity: &ClientIdentity,
record: &mut InstallationRecord,
full_name: &HSTRING,
shutdown: &AtomicBool,
) -> Result<()> {
let family = record.runtime()?.package_family.clone();
validate_record(record)?;
let mut profiles = Vec::new();
let mut accounts = Vec::new();
for account in [
SandboxRuntimeAccount::Offline,
SandboxRuntimeAccount::Online,
] {
let token = with_owner_impersonation(identity.token.0, || {
let mut pins = Vec::new();
crate::ipc::pin_existing_ancestors(
&sandbox_secrets_dir(&identity.codex_home),
&mut pins,
)?;
logon_existing_sandbox_account(&identity.codex_home, account)
})?;
let expected = record
.runtime()?
.accounts
.iter()
.find(|entry| entry.account == account);
let profile = load_profile(
token,
account,
expected.map(|entry| entry.user_sid.as_str()),
)?;
let installed = registered_packages(&profile.user_sid, &family)?;
if expected.is_none() {
ensure!(
installed.is_empty(),
"sandbox account has a runtime registration not owned by this service"
);
}
let alias_path = known_folder::path(
profile.token.as_raw_handle() as _,
&FOLDERID_LocalAppData,
/*flags*/ 0,
)?
.join(r"Microsoft\WindowsApps")
.join(&family)
.join(APP_CORE_RUNNER_ALIAS);
accounts.push(RuntimeAccountRegistration {
account,
user_sid: profile.user_sid.clone(),
alias_path: Some(alias_path),
});
profiles.push((profile, installed.contains(full_name)));
}
let receipt_current = record.runtime()?.ready_for_package(&full_name.to_string())
&& record.runtime()?.accounts == accounts
&& profiles.iter().all(|(_, installed)| *installed);
// Both exact SIDs are durable before either package registration or metadata grant.
// An idempotent setup check must not temporarily revoke a valid launch receipt.
record.runtime_mut()?.accounts = accounts;
if !receipt_current {
record.runtime_mut()?.ready_package = None;
crate::installation_record::save_runtime(record)?;
}
for (mut profile, installed) in profiles {
if !installed {
let operation = with_owner_impersonation(profile.token.as_raw_handle() as _, || {
Ok(PackageManager::new()?.RegisterPackageByFullNameAsync(
full_name,
None,
DeploymentOptions::None,
)?)
})?;
// As with provisioning, retain the request's resources until Windows finishes.
// A client timeout or an unreadable status cannot safely release the profile.
let mut status_error_logged = false;
loop {
if shutdown.load(Ordering::Acquire) {
// The typed error bypasses all response conversion and cleanup. Keep the
// in-flight operation and its profile alive until this own-process service exits.
std::mem::forget((operation, profile));
return Err(RegistrationInterrupted.into());
}
match operation.Status() {
Ok(AsyncStatus::Started) => {}
Ok(AsyncStatus::Completed | AsyncStatus::Canceled | AsyncStatus::Error) => {
break;
}
status => {
if !status_error_logged {
crate::service::log_error(
crate::service::EVENT_SERVICE_FAILED,
&format!(
"runtime registration status unavailable; retaining request: {status:?}"
),
);
status_error_logged = true;
}
}
}
std::thread::sleep(Duration::from_millis(50));
}
operation.GetResults()?.ExtendedErrorCode()?.ok()?;
ensure!(
registered_packages(&profile.user_sid, &family)?.contains(full_name),
"runtime registration completed without the expected package"
);
}
metadata::grant(identity.token.0, &profile.user_sid, record)?;
profile.unload_profile()?;
}
if !receipt_current {
record.runtime_mut()?.ready_package = Some(full_name.to_string());
crate::installation_record::save_runtime(record)?;
}
Ok(())
}
/// Prepare one retirement fence under the caller's setup lock. Removal preparation
/// then retains exact account logons before the native guard disables those users.
pub(crate) fn prepare_cleanup(mut record: InstallationRecord) -> Result<InstallationRecord> {
validate_record(&record)?;
for account in &record.runtime()?.accounts {
if codex_windows_sandbox::local_user_flags(account.account.username())?.is_some() {
let sid = lookup_sid(account.account.username())?;
ensure!(
string_from_sid_bytes(&sid).map_err(anyhow::Error::msg)? == account.user_sid,
"managed runtime account was replaced before cleanup"
);
}
}
ensure!(
record.runtime()?.retiring.is_none(),
"interrupted runtime cleanup requires repair"
);
record.runtime_mut()?.ready_package = None;
record.runtime_mut()?.retiring = Some(format!("{:?}", windows::core::GUID::new()?));
Ok(record)
}
fn validate_record(record: &InstallationRecord) -> Result<()> {
let runtime = record.runtime()?;
ensure!(
runtime.accounts.len() <= 2,
"runtime registration record has too many accounts"
);
if runtime.accounts.len() == 2 {
ensure!(
runtime.accounts[0].account != runtime.accounts[1].account,
"runtime registration record contains duplicate accounts"
);
}
Ok(())
}
fn registered_packages(user_sid: &str, family: &str) -> Result<Vec<HSTRING>> {
let packages = PackageManager::new()?.FindPackagesByUserSecurityIdPackageFamilyName(
&HSTRING::from(user_sid),
&HSTRING::from(family),
)?;
// The Rust Iterator adapter suppresses Current/MoveNext errors. Absence must
// be authoritative before releasing cleanup ownership or its package anchor.
let iterator = packages.First()?;
let mut names = Vec::new();
let mut has_current = iterator.HasCurrent()?;
while has_current {
names.push(iterator.Current()?.Id()?.FullName()?);
has_current = iterator.MoveNext()?;
}
Ok(names)
}
/// An intent is not an anchor: failed registration may leave no managed package.
pub(crate) fn has_retaining_registration(record: &InstallationRecord) -> Result<bool> {
let runtime = record.runtime()?;
for account in &runtime.accounts {
if !registered_packages(&account.user_sid, &runtime.package_family)?.is_empty() {
return Ok(true);
}
}
Ok(false)
}
fn load_profile(
token: OwnedHandle,
account: SandboxRuntimeAccount,
expected_sid: Option<&str>,
) -> Result<AccountProfile> {
let user_sid = validate_target(token.as_raw_handle() as _, account, expected_sid)?;
let mut username = to_wide(account.username());
let mut profile: PROFILEINFOW = unsafe { std::mem::zeroed() };
profile.dwSize = std::mem::size_of::<PROFILEINFOW>() as u32;
profile.dwFlags = 1; // PI_NOUI: service operations never prompt interactively.
profile.lpUserName = username.as_mut_ptr();
unsafe { BOOL(LoadUserProfileW(token.as_raw_handle() as _, &mut profile)).ok() }
.context("load managed runtime account profile")?;
Ok(AccountProfile {
user_sid,
token,
profile: profile.hProfile,
})
}
fn validate_target(
token: HANDLE,
account: SandboxRuntimeAccount,
expected_sid: Option<&str>,
) -> Result<String> {
let user = unsafe { codex_windows_sandbox::get_user_sid_bytes(token) }?;
let user_sid = string_from_sid_bytes(&user).map_err(anyhow::Error::msg)?;
ensure!(
expected_sid.is_none_or(|expected| expected == user_sid),
"managed runtime account SID changed"
);
ensure!(
lookup_sid(account.username())? == user,
"managed runtime account was replaced"
);
let groups = unsafe {
token_groups(token, /*max_bytes*/ 65_536)
}
.context("read runtime account groups")?;
ensure!(groups.len() <= 256, "invalid runtime account groups");
let sandbox_group = lookup_sid(codex_windows_sandbox::SANDBOX_USERS_GROUP)?;
let mut sandbox_member = false;
for group in groups {
ensure!(
string_from_sid_bytes(&group.sid).map_err(anyhow::Error::msg)? != "S-1-5-32-544",
"managed runtime account is an administrator"
);
if unsafe { security::EqualSid(group.sid.as_ptr() as _, sandbox_group.as_ptr() as _) } != 0
&& group.attributes & SE_GROUP_ENABLED as u32 != 0
{
sandbox_member = true;
}
}
ensure!(
sandbox_member,
"runtime account is not in CodexSandboxUsers"
);
Ok(user_sid)
}
@@ -0,0 +1,32 @@
//! Resolves Known Folders for an explicit token and caller-selected flags.
//! Preserve Windows UTF-16 paths and free the returned allocation on every result.
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::path::PathBuf;
use anyhow::Result;
use anyhow::ensure;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::System::Com::CoTaskMemFree;
use windows_sys::Win32::UI::Shell::SHGetKnownFolderPath;
use windows_sys::core::GUID;
pub(super) fn path(token: HANDLE, folder: &GUID, flags: u32) -> Result<PathBuf> {
let mut raw = std::ptr::null_mut();
let status = unsafe { SHGetKnownFolderPath(folder, flags, token, &mut raw) };
let result = (|| {
windows::core::HRESULT(status).ok()?;
ensure!(!raw.is_null(), "Known Folder path missing");
let mut length = 0;
while length < 32768 && unsafe { *raw.add(length) } != 0 {
length += 1;
}
ensure!(length < 32768, "Known Folder path exceeds bound");
Ok(PathBuf::from(OsString::from_wide(unsafe {
std::slice::from_raw_parts(raw, length)
})))
})();
unsafe { CoTaskMemFree(raw.cast()) };
result
}
@@ -0,0 +1,326 @@
//! Gives registered commands metadata access to the owner's two AppData roots.
//! No file contents or descendants are granted. Persist the managed account and
//! each owner-authorized root before granting access, so cleanup includes former roots.
use std::io;
use std::os::windows::io::AsRawHandle;
use std::os::windows::io::OwnedHandle;
use std::path::Path;
use std::ptr;
use anyhow::Context;
use anyhow::Result;
use anyhow::ensure;
use codex_windows_sandbox::DirectoryOpenDisposition;
use codex_windows_sandbox::LocalSid;
use codex_windows_sandbox::open_directory_no_reparse;
use windows::Win32::Foundation::BOOL;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Foundation::LocalFree;
use windows_sys::Win32::Foundation::RtlNtStatusToDosError;
use windows_sys::Win32::Security as security;
use windows_sys::Win32::Security::Authorization as authorization;
use windows_sys::Win32::Storage::FileSystem as filesystem;
use windows_sys::Win32::UI::Shell as shell;
use super::known_folder::path as known_folder;
use crate::installation_record::InstallationRecord;
use crate::package_lifecycle::with_owner_impersonation;
const OWNER_FOLDER_FLAGS: u32 =
(shell::KF_FLAG_DONT_VERIFY | shell::KF_FLAG_NO_PACKAGE_REDIRECTION) as u32;
#[link(name = "ntdll")]
unsafe extern "system" {
fn NtSetSecurityObject(
handle: HANDLE,
information: u32,
descriptor: *const security::SECURITY_DESCRIPTOR,
) -> i32;
}
pub(super) fn grant(
owner_token: HANDLE,
user_sid: &str,
record: &mut InstallationRecord,
) -> Result<()> {
ensure!(
record
.runtime()?
.accounts
.iter()
.any(|account| account.user_sid == user_sid),
"AppData metadata access requires a recorded managed account"
);
let sid = LocalSid::from_string(user_sid)?;
for folder in [shell::FOLDERID_LocalAppData, shell::FOLDERID_RoamingAppData] {
let path = known_folder(owner_token, &folder, OWNER_FOLDER_FLAGS)?;
edit_root_acl(owner_token, &path, |acl| {
// The owner-authorized directory and its ancestors are pinned here.
// Commit provenance before any ACE mutation, including a partial grant.
if !record.runtime()?.metadata_roots.contains(&path) {
record.runtime_mut()?.metadata_roots.push(path.clone());
crate::installation_record::save_runtime(record)?;
}
if metadata_ace(acl, &sid)?.is_some() {
return Ok(false);
}
// Packaged Known Folder APIs verify these directory objects even when
// APPDATA is inherited. Do not grant listing, contents, or inheritance.
insert_metadata_ace(acl, &sid)?;
Ok(true)
})?;
}
Ok(())
}
/// Removes exact grants from all recorded roots before account deletion.
pub(crate) fn remove(token: HANDLE, record: &InstallationRecord) -> Result<()> {
let runtime = record.runtime()?;
let accounts = &runtime.accounts;
if accounts.is_empty() {
return Ok(());
}
let mut errors = Vec::new();
// Every grant records its root before changing permissions, including redirects.
for path in &runtime.metadata_roots {
let result = edit_root_acl(token, path, |acl| {
let mut changed = false;
for account in accounts {
let sid = LocalSid::from_string(&account.user_sid)?;
while let Some(index) = metadata_ace(acl, &sid)? {
unsafe { BOOL(security::DeleteAce(acl.as_mut_ptr().cast(), index)).ok() }
.context("remove AppData metadata ACE")?;
changed = true;
}
}
Ok(changed)
});
if let Err(error) = result
&& error
.downcast_ref::<io::Error>()
.is_none_or(|error| error.kind() != io::ErrorKind::NotFound)
{
errors.push(format!("{error:#}"));
}
}
ensure!(
errors.is_empty(),
"AppData metadata cleanup: {}",
errors.join("; ")
);
Ok(())
}
fn edit_root_acl(
token: HANDLE,
path: &Path,
edit: impl FnOnce(&mut Vec<u32>) -> Result<bool>,
) -> Result<()> {
let (_pins, handle) = with_owner_impersonation(token, || {
let mut pins = Vec::new();
crate::ipc::pin_existing_ancestors(path, &mut pins)?;
let handle = open_directory_no_reparse(
path,
filesystem::READ_CONTROL
| filesystem::WRITE_DAC
| filesystem::FILE_READ_ATTRIBUTES
| filesystem::FILE_TRAVERSE,
filesystem::FILE_SHARE_READ | filesystem::FILE_SHARE_WRITE,
DirectoryOpenDisposition::OpenExisting,
)?;
Ok((pins, handle))
})?;
let Some((mut acl, control)) = read_acl(&handle)? else {
return Ok(()); // A null DACL already allows metadata; never replace it.
};
if edit(&mut acl)? {
write_acl(&handle, &mut acl, control)?;
}
Ok(())
}
fn read_acl(handle: &OwnedHandle) -> Result<Option<(Vec<u32>, u16)>> {
let mut acl = ptr::null_mut();
let mut descriptor = ptr::null_mut();
let status = unsafe {
authorization::GetSecurityInfo(
handle.as_raw_handle() as _,
authorization::SE_FILE_OBJECT,
security::DACL_SECURITY_INFORMATION,
ptr::null_mut(),
ptr::null_mut(),
&mut acl,
ptr::null_mut(),
&mut descriptor,
)
};
if status != 0 {
return Err(io::Error::from_raw_os_error(status as i32)).context("read AppData DACL");
}
let result = (|| {
if acl.is_null() {
return Ok(None);
}
ensure!(
unsafe { security::IsValidAcl(acl) } != 0,
"invalid AppData DACL"
);
let length = unsafe { (*acl).AclSize } as usize;
let mut control = 0;
let mut revision = 0;
let queried = unsafe {
security::GetSecurityDescriptorControl(descriptor, &mut control, &mut revision)
};
BOOL(queried).ok().context("read AppData DACL control")?;
let mut buffer = vec![0u32; length.div_ceil(4)];
unsafe {
ptr::copy_nonoverlapping(acl.cast::<u8>(), buffer.as_mut_ptr().cast(), length);
}
Ok(Some((buffer, control)))
})();
unsafe { LocalFree(descriptor as _) };
result
}
fn metadata_ace(acl: &mut [u32], sid: &LocalSid) -> Result<Option<u32>> {
let acl = acl.as_mut_ptr().cast::<security::ACL>();
for index in 0..u32::from(unsafe { (*acl).AceCount }) {
let mut raw = ptr::null_mut();
unsafe { BOOL(security::GetAce(acl, index, &mut raw)).ok() }.context("read AppData ACE")?;
let offset = raw as usize - acl as usize;
ensure!(
offset + std::mem::size_of::<security::ACE_HEADER>()
<= usize::from(unsafe { (*acl).AclSize }),
"AppData ACE header exceeds ACL"
);
let header = unsafe { &*raw.cast::<security::ACE_HEADER>() };
ensure!(
offset + usize::from(header.AceSize) <= usize::from(unsafe { (*acl).AclSize }),
"AppData ACE exceeds ACL"
);
if header.AceType == 0 && header.AceFlags == 0 {
ensure!(header.AceSize >= 16, "AppData allow ACE is truncated");
let ace = unsafe { &*raw.cast::<security::ACCESS_ALLOWED_ACE>() };
let sid_pointer = ptr::addr_of!(ace.SidStart) as *mut std::ffi::c_void;
let subauthorities = unsafe { *sid_pointer.cast::<u8>().add(1) } as usize;
ensure!(
subauthorities <= 15 && 16 + subauthorities * 4 <= usize::from(header.AceSize),
"AppData allow ACE SID is truncated"
);
ensure!(
unsafe { security::IsValidSid(sid_pointer) } != 0,
"AppData allow ACE SID is invalid"
);
if ace.Mask == filesystem::FILE_READ_ATTRIBUTES
&& unsafe { security::EqualSid(sid_pointer, sid.as_ptr()) } != 0
{
return Ok(Some(index));
}
}
}
Ok(None)
}
fn insert_metadata_ace(acl: &mut Vec<u32>, sid: &LocalSid) -> Result<()> {
let capacity = usize::from(unsafe { (*acl.as_ptr().cast::<security::ACL>()).AclSize })
+ 8
+ unsafe { security::GetLengthSid(sid.as_ptr()) } as usize;
ensure!(capacity <= u16::MAX as usize, "AppData DACL is full");
acl.resize(capacity.div_ceil(4), 0);
unsafe { (*acl.as_mut_ptr().cast::<security::ACL>()).AclSize = capacity as u16 };
let mut single = [0u32; 32];
let single_acl = single.as_mut_ptr().cast();
unsafe {
BOOL(security::InitializeAcl(
single_acl,
std::mem::size_of_val(&single) as u32,
security::ACL_REVISION_DS,
))
.ok()
.context("initialize AppData metadata ACE")?;
BOOL(security::AddAccessAllowedAceEx(
single_acl,
security::ACL_REVISION_DS,
0,
filesystem::FILE_READ_ATTRIBUTES,
sid.as_ptr(),
))
.ok()
.context("construct AppData metadata ACE")?;
}
let mut added = ptr::null_mut();
unsafe { BOOL(security::GetAce(single_acl, 0, &mut added)).ok() }
.context("read new AppData metadata ACE")?;
let acl = acl.as_mut_ptr().cast::<security::ACL>();
let mut position = u32::from(unsafe { (*acl).AceCount });
for index in 0..position {
let mut raw = ptr::null_mut();
unsafe { BOOL(security::GetAce(acl, index, &mut raw)).ok() }
.context("read AppData ACE order")?;
if unsafe { (*raw.cast::<security::ACE_HEADER>()).AceFlags } & security::INHERITED_ACE as u8
!= 0
{
position = index;
break;
}
}
unsafe {
BOOL(security::AddAce(
acl,
security::ACL_REVISION_DS,
position,
added,
u32::from((*added.cast::<security::ACE_HEADER>()).AceSize),
))
.ok()
}
.context("insert AppData metadata ACE")?;
Ok(())
}
fn write_acl(handle: &OwnedHandle, acl: &mut [u32], control: u16) -> Result<()> {
let mut descriptor: security::SECURITY_DESCRIPTOR = unsafe { std::mem::zeroed() };
let preserved = security::SE_DACL_PROTECTED | security::SE_DACL_AUTO_INHERITED;
let raw_descriptor = (&mut descriptor as *mut security::SECURITY_DESCRIPTOR).cast();
unsafe {
BOOL(security::InitializeSecurityDescriptor(raw_descriptor, 1))
.ok()
.context("initialize AppData security descriptor")?;
BOOL(security::SetSecurityDescriptorDacl(
raw_descriptor,
1,
acl.as_mut_ptr().cast(),
i32::from(control & security::SE_DACL_DEFAULTED != 0),
))
.ok()
.context("set AppData DACL")?;
BOOL(security::SetSecurityDescriptorControl(
raw_descriptor,
preserved,
control & preserved,
))
.ok()
.context("preserve AppData DACL control")?;
}
// SetSecurityInfo recursively reapplies existing inheritable ACEs. The native
// object setter changes only this retained, owner-authorized directory handle.
let status = unsafe {
NtSetSecurityObject(
handle.as_raw_handle() as _,
security::DACL_SECURITY_INFORMATION,
&descriptor,
)
};
if status < 0 {
return Err(io::Error::from_raw_os_error(
unsafe { RtlNtStatusToDosError(status) } as i32,
))
.context("write AppData metadata DACL");
}
Ok(())
}
#[cfg(test)]
#[path = "metadata_tests.rs"]
mod tests;
@@ -0,0 +1,97 @@
//! Tests that metadata ACE edits preserve unrelated, inherited, and broader permissions.
use super::*;
fn ace_bytes(acl: &mut [u32]) -> Vec<Vec<u8>> {
let acl = acl.as_mut_ptr().cast::<security::ACL>();
(0..u32::from(unsafe { (*acl).AceCount }))
.map(|index| {
let mut raw = ptr::null_mut();
assert_ne!(unsafe { security::GetAce(acl, index, &mut raw) }, 0);
let length = unsafe { (*raw.cast::<security::ACE_HEADER>()).AceSize } as usize;
unsafe { std::slice::from_raw_parts(raw.cast::<u8>(), length) }.to_vec()
})
.collect()
}
fn existing_acl(sid: &LocalSid) -> Vec<u32> {
let mut acl = vec![0u32; 256];
let raw = acl.as_mut_ptr().cast();
assert_ne!(
unsafe { security::InitializeAcl(raw, 1024, security::ACL_REVISION_DS) },
0
);
assert_ne!(
unsafe {
security::AddAccessDeniedAceEx(
raw,
security::ACL_REVISION_DS,
0,
filesystem::FILE_WRITE_DATA,
sid.as_ptr(),
)
},
0
);
assert_ne!(
unsafe {
security::AddAccessAllowedAceEx(
raw,
security::ACL_REVISION_DS,
0,
filesystem::FILE_READ_DATA | filesystem::FILE_READ_ATTRIBUTES,
sid.as_ptr(),
)
},
0
);
assert_ne!(
unsafe {
security::AddAccessAllowedAceEx(
raw,
security::ACL_REVISION_DS,
security::INHERITED_ACE,
filesystem::FILE_READ_ATTRIBUTES,
sid.as_ptr(),
)
},
0
);
acl
}
#[test]
fn metadata_grant_and_cleanup_preserve_broader_and_inherited_aces() -> Result<()> {
let sid = LocalSid::from_string("S-1-5-21-1-2-3-1001")?;
let mut acl = existing_acl(&sid);
let before = ace_bytes(&mut acl);
assert_eq!(metadata_ace(&mut acl, &sid)?, None);
insert_metadata_ace(&mut acl, &sid)?;
assert_eq!(metadata_ace(&mut acl, &sid)?, Some(2));
assert_ne!(unsafe { security::IsValidAcl(acl.as_mut_ptr().cast()) }, 0);
assert_ne!(
unsafe { security::DeleteAce(acl.as_mut_ptr().cast(), 2) },
0
);
assert_eq!(ace_bytes(&mut acl), before);
Ok(())
}
#[test]
fn metadata_cleanup_handles_duplicates_without_touching_another_principal() -> Result<()> {
let sid = LocalSid::from_string("S-1-5-21-1-2-3-1001")?;
let other = LocalSid::from_string("S-1-5-21-1-2-3-1002")?;
let mut acl = existing_acl(&sid);
let before = ace_bytes(&mut acl);
insert_metadata_ace(&mut acl, &sid)?;
assert_eq!(metadata_ace(&mut acl, &other)?, None);
insert_metadata_ace(&mut acl, &sid)?;
while let Some(index) = metadata_ace(&mut acl, &sid)? {
assert_ne!(
unsafe { security::DeleteAce(acl.as_mut_ptr().cast(), index) },
0
);
}
assert_eq!(ace_bytes(&mut acl), before);
Ok(())
}
@@ -0,0 +1,146 @@
$ErrorActionPreference = 'Stop'
$PSModuleAutoLoadingPreference = 'None'
Import-Module -Name ($PSHOME + '\Modules\Microsoft.PowerShell.Utility\Microsoft.PowerShell.Utility.psd1') -ErrorAction Stop
Import-Module -Name ($PSHOME + '\Modules\Appx\Appx.psd1') -ErrorAction Stop
Import-Module -Name ($PSHOME + '\Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1') -ErrorAction Stop
if ([Security.Principal.WindowsIdentity]::GetCurrent().User.Value -ne 'S-1-5-18') { throw 'SYSTEM required' }
# Rust sends UTF-8 regardless of the machine's console code page. Keep one reader
# for the complete protocol so read-ahead cannot consume COMMIT or its EOF fence.
$protocolInput = [IO.StreamReader]::new([Console]::OpenStandardInput(), [Text.UTF8Encoding]::new($false, $true), $false)
$planJson = $protocolInput.ReadLine()
if (!$planJson) { return }
$plan = Microsoft.PowerShell.Utility\ConvertFrom-Json -InputObject $planJson
if ([string]::IsNullOrEmpty($plan.record.runtime.retiring)) { throw 'Retirement generation required' }
if (@($plan.targets).Count -gt 2) { throw 'Too many cleanup tokens' }
Add-Type -TypeDefinition @"
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
public static class CleanupNative {
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct Profile { public int size,flags; public string user,path,def,server,policy; public IntPtr handle; }
[StructLayout(LayoutKind.Sequential)] public struct Luid { public uint lo; public int hi; }
[StructLayout(LayoutKind.Sequential)] public struct Priv { public uint count; public Luid luid; public uint attributes; }
[DllImport("userenv.dll",CharSet=CharSet.Unicode,SetLastError=true)] public static extern bool LoadUserProfile(IntPtr t,ref Profile p);
[DllImport("userenv.dll",SetLastError=true)] public static extern bool UnloadUserProfile(IntPtr t,IntPtr p);
[DllImport("advapi32.dll",SetLastError=true)] public static extern bool ImpersonateLoggedOnUser(IntPtr t);
[DllImport("advapi32.dll",SetLastError=true)] public static extern bool RevertToSelf();
[DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr h);
[DllImport("kernel32.dll")] public static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll",SetLastError=true)] static extern bool OpenProcessToken(IntPtr p,uint a,out IntPtr t);
[DllImport("advapi32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern bool LookupPrivilegeValue(string s,string n,out Luid l);
[DllImport("advapi32.dll",SetLastError=true)] static extern bool AdjustTokenPrivileges(IntPtr t,bool d,ref Priv p,int len,IntPtr old,IntPtr ret);
public static void Enable(string n) { IntPtr t; Luid l; if(!OpenProcessToken(GetCurrentProcess(),0x28,out t))throw new Win32Exception(); try { if(!LookupPrivilegeValue(null,n,out l))throw new Win32Exception(); Priv p=new Priv{count=1,luid=l,attributes=2}; if(!AdjustTokenPrivileges(t,false,ref p,0,IntPtr.Zero,IntPtr.Zero))throw new Win32Exception(); int e=Marshal.GetLastWin32Error(); if(e!=0)throw new Win32Exception(e); } finally { CloseHandle(t); } }
public static void Revert() { if(!RevertToSelf())Environment.FailFast("Cleanup impersonation revert failed"); }
}
"@
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$null = [Windows.Management.Deployment.PackageManager,Windows.Management.Deployment,ContentType=WindowsRuntime]
$null = [Windows.Management.Deployment.DeploymentResult,Windows.Management.Deployment,ContentType=WindowsRuntime]
$null = [Windows.Management.Deployment.DeploymentProgress,Windows.Management.Deployment,ContentType=WindowsRuntime]
$asTask = [System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.IsGenericMethod -and $_.GetGenericArguments().Count -eq 2 -and $_.GetParameters().Count -eq 1 } | Select-Object -First 1
$await = $asTask.MakeGenericMethod([Windows.Management.Deployment.DeploymentResult],[Windows.Management.Deployment.DeploymentProgress])
$profiles = @()
try {
[CleanupNative]::Enable('SeBackupPrivilege')
[CleanupNative]::Enable('SeRestorePrivilege')
foreach ($target in $plan.targets) {
$token = [IntPtr][long]$target.handle
$identity = [Security.Principal.WindowsIdentity]::new($token)
try { if ($identity.User.Value -cne $target.sid) { throw 'Cleanup token SID mismatch' } }
finally { $identity.Dispose() }
$profile = New-Object CleanupNative+Profile
$profile.size = [Runtime.InteropServices.Marshal]::SizeOf($profile)
$profile.flags = 1
$profile.user = $target.username
if (![CleanupNative]::LoadUserProfile($token, [ref]$profile)) { throw [ComponentModel.Win32Exception]::new([Runtime.InteropServices.Marshal]::GetLastWin32Error()) }
$profiles += @{ token = $token; profile = $profile; sid = $target.sid }
}
[Console]::Out.WriteLine('READY')
# EOF without a successful native-cleanup commit only unloads profiles.
if ($protocolInput.ReadLine() -cne 'COMMIT') { return }
# Even after COMMIT, removal must wait until the service releases its own package.
if ($protocolInput.ReadToEnd().Length -ne 0) { return }
$finished = $false
while (!$finished) {
$mutex = $null; $locked = $false; $key = $null; $legacy = $null
try {
$security = [Security.AccessControl.MutexSecurity]::new()
$security.SetSecurityDescriptorSddlForm('D:P(A;;GA;;;SY)(A;;GA;;;BA)')
$created = $false
$mutex = [Threading.Mutex]::new($false, 'Global\CodexSandboxSetup', [ref]$created, $security)
try { $locked = $mutex.WaitOne(30000) }
catch [Threading.AbandonedMutexException] { $locked = $true }
if (!$locked) { throw 'Sandbox setup is still active' }
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($plan.key, $true)
if (!$key) { return }
$record = Microsoft.PowerShell.Utility\ConvertFrom-Json -InputObject ([string]$key.GetValue($plan.value))
if ($record.runtime.retiring -ne $plan.record.runtime.retiring -or
$record.user_sid -ne $plan.record.user_sid -or
$record.codex_home -ne $plan.record.codex_home -or
$record.runtime.package_family -ne $plan.record.runtime.package_family) { return }
foreach ($entry in $profiles) {
foreach ($package in @(Appx\Get-AppxPackage -User $entry.sid -ErrorAction Stop)) {
if ($package.PackageFamilyName -ne $record.runtime.package_family) { continue }
if (![CleanupNative]::ImpersonateLoggedOnUser($entry.token)) { throw 'Cleanup impersonation failed' }
try {
if ([Security.Principal.WindowsIdentity]::GetCurrent().User.Value -cne $entry.sid) { throw 'Cleanup identity changed' }
# Construct and submit synchronously under the retained user, not SYSTEM.
$manager = [Windows.Management.Deployment.PackageManager]::new()
$operation = $manager.RemovePackageAsync($package.PackageFullName)
} finally { [CleanupNative]::Revert() }
# Failure to obtain a waiter is not proof the operation stopped.
$task = $null
while (!$task) {
try { $task = $await.Invoke($null, @($operation)) }
catch { Microsoft.PowerShell.Utility\Start-Sleep -Seconds 1 }
}
while (!$task.IsCompleted) { Microsoft.PowerShell.Utility\Start-Sleep -Milliseconds 50 }
$result = $task.GetAwaiter().GetResult()
if ($result.ExtendedErrorCode -and $result.ExtendedErrorCode.HResult -ne 0) { throw $result.ErrorText }
}
}
foreach ($account in $plan.record.runtime.accounts) {
foreach ($package in @(Appx\Get-AppxPackage -User $account.user_sid -ErrorAction Stop)) {
if ($package.PackageFamilyName -eq $record.runtime.package_family) { throw 'Runtime registration remains' }
}
}
$legacy = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($plan.legacy_key, $true)
if (!$legacy) { throw 'Installation parent is missing' }
$legacyJson = $legacy.GetValue($plan.value)
if ($legacyJson) {
$owner = Microsoft.PowerShell.Utility\ConvertFrom-Json -InputObject ([string]$legacyJson)
if ($owner.user_sid -eq $plan.record.user_sid -and $owner.codex_home -eq $plan.record.codex_home) { $legacy.DeleteValue($plan.value, $false) }
}
$key.Dispose(); $key = $null
$legacy.Flush()
[Microsoft.Win32.Registry]::LocalMachine.DeleteSubKey($plan.key, $false)
$finished = $true
} catch { [Console]::Error.WriteLine("Runtime package removal deferred: $_") }
finally {
if ($key) { $key.Dispose() }
if ($legacy) { $legacy.Dispose() }
if ($locked) { $mutex.ReleaseMutex() }
if ($mutex) { $mutex.Dispose() }
}
if (!$finished) { Microsoft.PowerShell.Utility\Start-Sleep -Seconds 30 }
}
} finally {
foreach ($entry in $profiles) {
while (![CleanupNative]::UnloadUserProfile($entry.token, $entry.profile.handle)) {
Microsoft.PowerShell.Utility\Start-Sleep -Seconds 1
}
}
foreach ($target in $plan.targets) { $null = [CleanupNative]::CloseHandle([IntPtr][long]$target.handle) }
}
# A reinstall may have tried to start the service while the retirement fence blocked it.
while ($true) {
try {
foreach ($package in @(Appx\Get-AppxPackage -User $plan.record.user_sid -ErrorAction Stop)) {
if ($package.PackageFamilyName -eq $plan.record.runtime.package_family) {
Microsoft.PowerShell.Management\Start-Service -Name $plan.service_name -ErrorAction Stop
break
}
}
break
} catch { Microsoft.PowerShell.Utility\Start-Sleep -Seconds 30 }
}
@@ -0,0 +1,215 @@
//! Prepare exact managed logons before native cleanup; authorize removal only after success.
//! The child owns loaded profiles and waits for both COMMIT and service-process EOF.
use std::io::Read;
use std::io::Write;
use std::os::windows::fs::MetadataExt;
use std::os::windows::io::AsHandle;
use std::os::windows::io::AsRawHandle;
use std::os::windows::io::BorrowedHandle;
use std::os::windows::io::OwnedHandle;
use std::os::windows::process::CommandExt;
use std::path::PathBuf;
use std::process::Child;
use std::process::Command;
use std::process::Stdio;
use std::time::Duration;
use std::time::Instant;
use anyhow::Context;
use anyhow::Result;
use anyhow::ensure;
use base64::Engine;
use windows_sys::Win32::Foundation::DUPLICATE_SAME_ACCESS;
use windows_sys::Win32::Foundation::DuplicateHandle;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::System::Pipes::PeekNamedPipe;
use windows_sys::Win32::System::SystemInformation::GetSystemDirectoryW;
use windows_sys::Win32::System::Threading as threading;
use crate::installation_record::InstallationRecord;
use crate::package_lifecycle::with_owner_impersonation;
pub(crate) struct PreparedRemoval {
child: Child,
tokens: Vec<OwnedHandle>,
}
impl PreparedRemoval {
pub(crate) fn tokens(&self) -> Vec<BorrowedHandle<'_>> {
self.tokens.iter().map(AsHandle::as_handle).collect()
}
/// Called only after the native cleanup guard has successfully finished.
pub(crate) fn commit(mut self) -> Result<()> {
self.child
.stdin
.as_mut()
.context("cleanup input closed")?
.write_all(b"COMMIT\n")
.context("commit Windows package cleanup")?;
// The only pipe writer remains open until this own-process service exits.
// Dropping an uncommitted preparation instead sends EOF, which only aborts.
std::mem::forget(self.child);
Ok(())
}
}
// The cleanup entry checked the service family; prepare_cleanup validated this record
// under the same setup lock. Fresh logons below still require exact account-SID checks.
pub(crate) fn prepare(owner_token: HANDLE, record: &InstallationRecord) -> Result<PreparedRemoval> {
ensure!(
record.runtime()?.retiring.is_some(),
"runtime is not retiring"
);
let mut tokens = Vec::new();
let mut targets = Vec::new();
for account in &record.runtime()?.accounts {
if codex_windows_sandbox::local_user_flags(account.account.username())?.is_none() {
ensure!(
super::registered_packages(&account.user_sid, &record.runtime()?.package_family)?
.is_empty(),
"runtime account is missing while its package remains registered"
);
continue;
}
let token = with_owner_impersonation(owner_token, || {
let mut pins = Vec::new();
crate::ipc::pin_existing_ancestors(
&codex_windows_sandbox::sandbox_secrets_dir(&record.codex_home),
&mut pins,
)?;
codex_windows_sandbox::logon_existing_sandbox_account(
&record.codex_home,
account.account,
)
})?;
super::validate_target(
token.as_raw_handle() as _,
account.account,
Some(&account.user_sid),
)?;
targets.push(serde_json::json!({
"username": account.account.username(),
"sid": account.user_sid,
}));
tokens.push(token);
}
let mut system = [0u16; 32768];
let length = unsafe { GetSystemDirectoryW(system.as_mut_ptr(), system.len() as u32) } as usize;
ensure!(
length > 0 && length < system.len(),
"Windows system directory is unavailable"
);
let system = PathBuf::from(String::from_utf16(&system[..length])?);
let executable = system.join(r"WindowsPowerShell\v1.0\powershell.exe");
let metadata = executable
.symlink_metadata()
.context("inspect Windows PowerShell")?;
ensure!(
metadata.is_file()
&& metadata.file_attributes()
& windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_REPARSE_POINT
== 0,
"invalid Windows PowerShell executable"
);
let bytes = include_str!("removal.ps1")
.encode_utf16()
.flat_map(u16::to_le_bytes)
.collect::<Vec<_>>();
let encoded = base64::engine::general_purpose::STANDARD.encode(bytes);
ensure!(
executable.as_os_str().len() + encoded.len() + 64 < 32767,
"cleanup command exceeds the Windows command-line limit"
);
let mut child = Command::new(executable)
.args([
"-NoLogo",
"-NoProfile",
"-NonInteractive",
"-EncodedCommand",
&encoded,
])
.current_dir(system)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.creation_flags(threading::CREATE_NO_WINDOW)
.spawn()
.context("start Windows package cleanup")?;
// Do not relax process handle inheritance. Only these validated token handles
// are duplicated into the exact child process, with inheritance disabled.
for (target, token) in targets.iter_mut().zip(&tokens) {
let mut remote = 0;
let duplicated = unsafe {
DuplicateHandle(
threading::GetCurrentProcess(),
token.as_raw_handle() as _,
child.as_raw_handle() as _,
&mut remote,
/*dwdesiredaccess*/ 0,
/*binherithandle*/ 0,
DUPLICATE_SAME_ACCESS,
)
};
ensure!(
duplicated != 0,
"duplicate managed cleanup token: {}",
std::io::Error::last_os_error()
);
target["handle"] = serde_json::json!(remote as usize);
}
let plan = serde_json::json!({
"key": crate::installation_record::CORE_INSTALLATION_KEY,
"legacy_key": crate::installation_record::INSTALLATION_KEY,
"value": crate::installation_record::INSTALLATION_VALUE,
"record": record,
"targets": targets,
"service_name": codex_windows_sandbox::windows_sandbox_service_name()?,
});
let input = child.stdin.as_mut().context("capture cleanup input")?;
serde_json::to_writer(&mut *input, &plan)?;
input.write_all(b"\n")?;
let output = child.stdout.as_mut().context("capture cleanup readiness")?;
let deadline = Instant::now() + Duration::from_secs(30);
let mut ready = Vec::new();
while !ready.ends_with(b"\n") {
ensure!(
Instant::now() < deadline,
"timed out preparing Windows package cleanup"
);
let mut available = 0;
let result = unsafe {
PeekNamedPipe(
output.as_raw_handle() as _,
std::ptr::null_mut(),
/*nbuffersize*/ 0,
std::ptr::null_mut(),
&mut available,
std::ptr::null_mut(),
)
};
ensure!(
result != 0,
"cleanup exited before readiness: {}",
std::io::Error::last_os_error()
);
if available == 0 {
std::thread::sleep(Duration::from_millis(20));
continue;
}
let mut byte = [0];
output.read_exact(&mut byte)?;
ready.push(byte[0]);
ensure!(ready.len() <= 16, "invalid cleanup readiness response");
}
ensure!(
ready == b"READY\r\n" || ready == b"READY\n",
"cleanup profiles were not prepared"
);
Ok(PreparedRemoval { child, tokens })
}
#[cfg(test)]
#[path = "removal_tests.rs"]
mod tests;
@@ -0,0 +1,106 @@
//! Exercise the actual finalizer authorization barrier without account or package mutations.
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::os::windows::io::AsRawHandle;
use std::os::windows::process::CommandExt;
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;
use anyhow::Context;
use anyhow::Result;
use anyhow::ensure;
use pretty_assertions::assert_eq;
use windows_sys::Win32::Foundation::WAIT_OBJECT_0;
use windows_sys::Win32::Foundation::WAIT_TIMEOUT;
use windows_sys::Win32::System::SystemInformation::GetSystemDirectoryW;
use windows_sys::Win32::System::Threading::CREATE_NO_WINDOW;
use windows_sys::Win32::System::Threading::WaitForSingleObject;
#[test]
fn finalizer_preserves_utf8_plan_and_requires_commit_and_stdin_eof() -> Result<()> {
let mut system = [0u16; 32768];
let length = unsafe { GetSystemDirectoryW(system.as_mut_ptr(), system.len() as u32) } as usize;
ensure!(
length > 0 && length < system.len(),
"Windows system directory is unavailable"
);
let executable = PathBuf::from(String::from_utf16(&system[..length])?)
.join(r"WindowsPowerShell\v1.0\powershell.exe");
// Run the production decoder and protocol in isolation, never the cleanup body.
let script = include_str!("removal.ps1");
let input_anchor = "throw 'SYSTEM required' }";
let input_start = script.find(input_anchor).context("find input setup")? + input_anchor.len();
let input_end = script
.find("if ([string]::IsNullOrEmpty")
.context("find plan validation")?;
let input_setup = &script[input_start..input_end];
let start = script
.find(" # EOF without a successful native-cleanup commit")
.context("find commit barrier")?;
let end = script[start..]
.find(" $finished = $false")
.context("find cleanup boundary")?
+ start;
let home = r"C:\Users\Zoë-東京\.codex";
let script = format!(
"$ErrorActionPreference = 'Stop';
[Console]::InputEncoding = [Text.Encoding]::GetEncoding(437);
{input_setup}
if ($plan.record.codex_home -cne '{home}') {{ throw 'UTF-8 plan was corrupted' }}
[Console]::Out.WriteLine('waiting');\n{}\n[Console]::Out.WriteLine('released')",
&script[start..end]
);
let mut plan = serde_json::to_vec(&serde_json::json!({ "record": { "codex_home": home } }))?;
plan.push(b'\n');
for (input, expected) in [
("", ""),
("ABORT\n", ""),
("COMMIT\n", "released"),
("COMMIT\ntrailing", ""),
] {
let mut child = Command::new(&executable)
.args([
"-NoLogo",
"-NoProfile",
"-NonInteractive",
"-Command",
&script,
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.creation_flags(CREATE_NO_WINDOW)
.spawn()?;
// Batch the plan and command so decoder read-ahead also exercises the shared reader.
let mut request = plan.clone();
request.extend_from_slice(input.as_bytes());
child
.stdin
.as_mut()
.context("capture barrier input")?
.write_all(&request)?;
let mut output = BufReader::new(child.stdout.take().context("capture barrier output")?);
let mut line = String::new();
output.read_line(&mut line)?;
assert_eq!(line.trim_end(), "waiting");
if input.is_empty() || input.starts_with("COMMIT\n") {
let before_eof = unsafe {
WaitForSingleObject(child.as_raw_handle() as _, /*dwmilliseconds*/ 500)
};
assert_eq!(before_eof, WAIT_TIMEOUT);
}
drop(child.stdin.take());
let after_eof = unsafe {
WaitForSingleObject(child.as_raw_handle() as _, /*dwmilliseconds*/ 10_000)
};
assert_eq!(after_eof, WAIT_OBJECT_0);
line.clear();
output.read_line(&mut line)?;
assert_eq!(line.trim_end(), expected);
ensure!(child.wait()?.success(), "barrier subprocess failed");
}
Ok(())
}
+25 -24
View File
@@ -1,8 +1,7 @@
//! Windows service lifecycle and event-log integration for sandbox provisioning.
//! Owns Windows SCM registration, service state, and sandbox provisioning event logs.
use std::ffi::c_void;
use std::io;
use std::mem::size_of;
use std::ptr;
use std::sync::Arc;
use std::sync::OnceLock;
@@ -20,7 +19,6 @@ use windows_sys::Win32::System::EventLog::EVENTLOG_ERROR_TYPE;
use windows_sys::Win32::System::EventLog::EVENTLOG_INFORMATION_TYPE;
use windows_sys::Win32::System::EventLog::RegisterEventSourceW;
use windows_sys::Win32::System::EventLog::ReportEventW;
use windows_sys::Win32::System::RemoteDesktop::WTSSESSION_NOTIFICATION;
use windows_sys::Win32::System::Services::RegisterServiceCtrlHandlerExW;
use windows_sys::Win32::System::Services::SERVICE_ACCEPT_SESSIONCHANGE;
use windows_sys::Win32::System::Services::SERVICE_ACCEPT_SHUTDOWN;
@@ -42,7 +40,8 @@ use windows_sys::Win32::System::Services::StartServiceCtrlDispatcherW;
mod runtime_lifecycle;
pub(crate) const SERVICE_NAME: &str = "CodexSandboxService";
pub(crate) use runtime_lifecycle::retry_cleanup;
const EVENT_SERVICE_STARTED: u32 = 1000;
const EVENT_SERVICE_STOP_REQUESTED: u32 = 1001;
const EVENT_SERVICE_STOPPED: u32 = 1002;
@@ -57,28 +56,31 @@ const MAX_EVENT_MESSAGE_UNITS: usize = 1024;
static SERVICE_STATE: OnceLock<ServiceState> = OnceLock::new();
struct ServiceState {
service_name: String,
pipe_name: String,
shutdown: Arc<AtomicBool>,
uninstalling: Arc<AtomicBool>,
status_handle: OnceLock<SERVICE_STATUS_HANDLE>,
current_status: AtomicU32,
changed_session: AtomicU32,
stop_requested: AtomicBool,
}
pub(crate) fn run() -> Result<()> {
let service_name = codex_windows_sandbox::windows_sandbox_service_name()?;
let state = ServiceState {
service_name: service_name.clone(),
pipe_name: codex_windows_sandbox::windows_sandbox_service_pipe_name()?,
shutdown: Arc::new(AtomicBool::new(false)),
uninstalling: Arc::new(AtomicBool::new(false)),
status_handle: OnceLock::new(),
current_status: AtomicU32::new(SERVICE_START_PENDING),
changed_session: AtomicU32::new(u32::MAX),
stop_requested: AtomicBool::new(false),
};
SERVICE_STATE
.set(state)
.map_err(|_| anyhow::anyhow!("the service dispatcher was already initialized"))?;
let mut service_name = SERVICE_NAME
let mut service_name = service_name
.encode_utf16()
.chain(std::iter::once(0))
.collect::<Vec<_>>();
@@ -108,10 +110,14 @@ pub(crate) fn run_foreground() -> Result<()> {
crate::ipc::run(
Arc::new(AtomicBool::new(false)),
|| {
eprintln!("{SERVICE_NAME} listening on {}", crate::ipc::PIPE_NAME);
eprintln!(
"{} listening on {}",
codex_windows_sandbox::windows_sandbox_service_name()?,
codex_windows_sandbox::windows_sandbox_service_pipe_name()?
);
Ok(())
},
|installation, _| Ok(installation),
runtime_lifecycle::foreground_owner,
|| Ok(()),
)
}
@@ -126,7 +132,7 @@ unsafe extern "system" fn service_main(_argument_count: u32, _arguments: *mut *m
EVENT_SERVICE_FAILED,
&format!("The Codex sandbox service encountered a fatal error: {error:#}"),
);
eprintln!("{SERVICE_NAME} failed: {error:#}");
eprintln!("{} failed: {error:#}", state.service_name);
if state.status_handle.get().is_some() {
let _ = state.report_status(SERVICE_STOPPED, ERROR_SERVICE_SPECIFIC_ERROR);
}
@@ -134,7 +140,8 @@ unsafe extern "system" fn service_main(_argument_count: u32, _arguments: *mut *m
}
fn service_main_inner(state: &ServiceState) -> Result<()> {
let service_name = SERVICE_NAME
let service_name = state
.service_name
.encode_utf16()
.chain(std::iter::once(0))
.collect::<Vec<_>>();
@@ -169,7 +176,7 @@ fn service_main_inner(state: &ServiceState) -> Result<()> {
unsafe extern "system" fn service_control_handler(
control: u32,
_event_type: u32,
event_data: *mut c_void,
_event_data: *mut c_void,
_context: *mut c_void,
) -> u32 {
let Some(state) = SERVICE_STATE.get() else {
@@ -205,25 +212,17 @@ unsafe extern "system" fn service_control_handler(
NO_ERROR
}
SERVICE_CONTROL_SESSIONCHANGE => {
if !event_data.is_null() {
let event = unsafe { &*event_data.cast::<WTSSESSION_NOTIFICATION>() };
if event.cbSize as usize >= size_of::<WTSSESSION_NOTIFICATION>() {
state
.changed_session
.store(event.dwSessionId, Ordering::Release);
wake_listener();
}
}
wake_listener();
NO_ERROR
}
_ => ERROR_CALL_NOT_IMPLEMENTED,
}
}
fn wake_listener() {
pub(crate) fn wake_listener() {
if let Some(state) = SERVICE_STATE.get() {
std::thread::spawn(move || {
crate::ipc::wake(crate::ipc::PIPE_NAME, || {
crate::ipc::wake(&state.pipe_name, || {
state.current_status.load(Ordering::Acquire) == SERVICE_STOPPED
});
});
@@ -239,7 +238,9 @@ pub(crate) fn log_error(event_id: u32, message: &str) {
}
fn log_event(event_type: u16, event_id: u32, message: &str) {
let source = SERVICE_NAME
let source = SERVICE_STATE
.get()
.map_or("CodexSandboxService", |state| state.service_name.as_str())
.encode_utf16()
.chain(std::iter::once(0))
.collect::<Vec<_>>();
@@ -1,5 +1,7 @@
//! Connects the provisioning broker to the existing package owner and uninstall listener.
//! Runs owner-aware cleanup in the live provisioning broker before further admission.
//! Shutdown means SCM stop until cleanup finishes; retirement fences are never replayed.
use std::cell::Cell;
use std::sync::Arc;
use std::sync::atomic::Ordering;
@@ -7,54 +9,141 @@ use anyhow::Context;
use anyhow::Result;
use windows_sys::Win32::Foundation::NO_ERROR;
use windows_sys::Win32::System::Services::SERVICE_RUNNING;
use windows_sys::Win32::System::Services::SERVICE_STOP_PENDING;
use super::EVENT_SERVICE_FAILED;
use super::EVENT_SERVICE_STARTED;
use super::SERVICE_STATE;
use super::ServiceState;
use super::log_error;
use super::log_information;
use crate::package_lifecycle::PackageLifecycle;
/// Foreground debugging has no persistent watcher, but must retain the same admission rules.
#[cfg(debug_assertions)]
pub(super) fn foreground_owner(
mut record: crate::installation_record::InstallationRecord,
_token: crate::ipc::OwnedHandle,
runtime: codex_windows_sandbox::SetupRuntime,
) -> Result<crate::installation_record::InstallationRecord> {
let _lock = codex_windows_sandbox::acquire_sandbox_setup_lock(/*timeout_ms*/ 5_000)?;
if let Some(previous) = crate::installation_record::load()? {
let same_owner =
previous.user_sid == record.user_sid && previous.codex_home == record.codex_home;
if previous.runtime.is_some() {
let family = windows::ApplicationModel::Package::Current()?
.Id()?
.FamilyName()?;
previous.admit_owner(&record, &family.to_string())?;
record.runtime = previous.runtime;
} else if runtime == codex_windows_sandbox::SetupRuntime::Registered {
anyhow::ensure!(
same_owner,
"shared sandbox resources belong to a different owner"
);
}
if same_owner {
record.desktop_installation = previous
.desktop_installation
.or(record.desktop_installation);
}
}
Ok(record)
}
pub(super) fn run(state: &ServiceState, package_lifecycle: &PackageLifecycle) -> Result<()> {
let cleaned = Cell::new(false);
let restore_owner = || {
let restored = crate::installation_record::load().and_then(|record| {
record.map_or(Ok(()), |record| {
package_lifecycle.restore_logged_in_owner(record.session_id)
})
});
if let Err(error) = restored {
log_error(
EVENT_SERVICE_FAILED,
&format!("unable to restore package uninstall listener: {error:#}"),
);
}
};
crate::ipc::run(
Arc::clone(&state.shutdown),
|| {
restore_owner();
state.report_status(SERVICE_RUNNING, NO_ERROR)?;
if let Some(record) = crate::installation_record::load()?
&& let Err(error) = package_lifecycle.restore_logged_in_owner(record.session_id)
{
log_error(
EVENT_SERVICE_FAILED,
&format!("unable to restore package uninstall listener: {error:#}"),
);
}
log_information(
EVENT_SERVICE_STARTED,
"The Codex sandbox service is running.",
);
Ok(())
},
|installation, user_token| {
|installation, user_token, _runtime| {
package_lifecycle.register_authenticated_user(installation, user_token)
},
|| {
let session = state.changed_session.swap(u32::MAX, Ordering::AcqRel);
if session == u32::MAX {
|| loop {
restore_owner();
if !crate::package_lifecycle::runtime_owner_removed()? {
return Ok(());
}
if let Err(error) = package_lifecycle.restore_authenticated_user(session) {
log_error(
EVENT_SERVICE_FAILED,
&format!("unable to restore package uninstall listener: {error:#}"),
);
let Err(error) = package_lifecycle.clean_up() else {
cleaned.set(true);
state.shutdown.store(true, Ordering::Release);
return state.report_status(SERVICE_STOP_PENDING, NO_ERROR);
};
if crate::installation_record::load_runtime()?.is_none_or(|record| {
record
.runtime
.is_some_and(|runtime| runtime.retiring.is_some())
}) {
return Err(error);
}
log_error(
EVENT_SERVICE_FAILED,
&format!("sandbox cleanup deferred: {error:#}"),
);
// Keep the pipe available during pre-fence backoff. A reinstall
// resumes admission on the next owner check; SCM stops are never reset.
if !wait_for_cleanup_retry(state) {
return Err(error);
}
Ok(())
},
)
.context("run the sandbox provisioning broker")?;
if state.stop_requested.load(Ordering::Acquire) && state.uninstalling.load(Ordering::Acquire) {
if !cleaned.get()
&& state.stop_requested.load(Ordering::Acquire)
&& (state.uninstalling.load(Ordering::Acquire)
|| crate::package_lifecycle::runtime_owner_removed()?)
{
package_lifecycle.clean_up()?;
}
Ok(())
}
/// Retries the current teardown step, never a phase recovered from stored intent.
pub(crate) fn retry_cleanup(mut operation: impl FnMut() -> Result<()>) -> Result<()> {
loop {
let Err(error) = operation() else {
return Ok(());
};
let Some(state) = SERVICE_STATE.get() else {
return Err(error);
};
log_error(
EVENT_SERVICE_FAILED,
&format!("sandbox cleanup deferred: {error:#}"),
);
if !wait_for_cleanup_retry(state) {
return Err(error);
}
}
}
fn wait_for_cleanup_retry(state: &ServiceState) -> bool {
for _ in 0..30 {
if state.shutdown.load(Ordering::Acquire) {
return false;
}
std::thread::sleep(std::time::Duration::from_secs(1));
}
!state.shutdown.load(Ordering::Acquire)
}