Files
codex/codex-rs/windows-sandbox-rs/src/package_identity.rs
T
zm-oai d39cfa8a2d Harden and share Windows sandbox identity helpers (#45533)
## What changed

- Share process package-family queries, token user SID extraction, and account-name lookup through `codex_windows_sandbox`, and use them in the provisioning service.
- Bound token query sizes and validate SID pointers, revisions, and lengths before copying SIDs into owned storage.
- Use a drop guard to balance firewall COM initialization, and track the package lifecycle directory guard separately so cleanup releases it while keeping ancestor and home handles pinned.

## Testing

Add tests that verify copied SIDs outlive their query buffers and reject truncated token data, malformed SIDs, and invalid SID pointers.

GitOrigin-RevId: 2b893e4524d3e00cb114df05215beef7b9eebbe9
2026-09-14 22:40:01 +00:00

49 lines
1.8 KiB
Rust

//! Queries held process package identities without trusting routing hints or environment state.
use anyhow::Context;
use anyhow::Result;
use anyhow::bail;
use std::io;
use windows_sys::Win32::Foundation as foundation;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Storage::Packaging::Appx::GetPackageFamilyName;
/// Reads a process's OS package family. Absence is not authorization.
///
/// # Safety
/// The caller must keep the process handle valid throughout the query.
pub unsafe fn process_package_family(process: HANDLE) -> Result<Option<String>> {
let mut length = 0;
let status = unsafe { GetPackageFamilyName(process, &mut length, std::ptr::null_mut()) };
if status == foundation::APPMODEL_ERROR_NO_PACKAGE {
return Ok(None);
}
if status != foundation::ERROR_INSUFFICIENT_BUFFER {
return Err(io::Error::from_raw_os_error(status as i32))
.context("query the process package family");
}
if length == 0 || length > 256 {
bail!("the process package family has an invalid length");
}
let mut buffer = vec![0_u16; length as usize];
let status = unsafe { GetPackageFamilyName(process, &mut length, buffer.as_mut_ptr()) };
if status != foundation::ERROR_SUCCESS {
return Err(io::Error::from_raw_os_error(status as i32))
.context("read the process package family");
}
let value = buffer
.get(..length as usize)
.context("the package-family API returned an invalid length")?;
let Some((&0, value)) = value.split_last() else {
bail!("the process package family is not null-terminated");
};
if value.is_empty() || value.contains(&0) {
bail!("the process package family is malformed");
}
Ok(Some(
String::from_utf16(value).context("the package family contains invalid UTF-16")?,
))
}