Files
codex/codex-rs/windows-sandbox-rs/src/token_tests.rs
T
Tamir Duberstein b06b7d2f77 Upgrade Windows bindings to windows-sys 0.61.2 (#50058)
## What changed

- Centralize `windows-sys` at version `0.61.2` in the workspace and migrate dependent crates to it.
- Adapt Windows API calls to the updated handle, boolean, and type definitions. Replace custom handle owners with `OwnedHandle` and use `BorrowedHandle` where ownership stays with the caller.
- Add explicit wrappers for non-owning Windows handles passed between threads.

## Testing

Enable the MCP refresh cleanup test on Windows. Retain process handles to verify that a superseded MCP server stays alive during an in-flight call, exits after cancellation, and leaves the replacement running until shutdown.

GitOrigin-RevId: 86f53d9b300d13edff7154f4a7c32e34dacd2919
2026-10-01 18:05:05 +00:00

232 lines
8.9 KiB
Rust

use super::*;
use pretty_assertions::assert_eq;
use windows_sys::Win32::Security::EqualSid;
use windows_sys::Win32::Security::TokenRestrictedSids;
unsafe fn token_has_restricting_sid(token: HANDLE, expected_sid: *mut c_void) -> Result<bool> {
let mut needed = 0;
GetTokenInformation(
token,
TokenRestrictedSids,
std::ptr::null_mut(),
0,
&mut needed,
);
if needed == 0 {
return Err(anyhow!(
"GetTokenInformation(TokenRestrictedSids) size query failed: {}",
GetLastError()
));
}
let mut buffer = vec![0_u8; needed as usize];
if GetTokenInformation(
token,
TokenRestrictedSids,
buffer.as_mut_ptr().cast(),
needed,
&mut needed,
) == 0
{
return Err(anyhow!(
"GetTokenInformation(TokenRestrictedSids) failed: {}",
GetLastError()
));
}
let group_count = std::ptr::read_unaligned(buffer.as_ptr().cast::<u32>()) as usize;
let after_count = buffer.as_ptr().add(std::mem::size_of::<u32>()) as usize;
let align = std::mem::align_of::<SID_AND_ATTRIBUTES>();
let entries_addr = (after_count + (align - 1)) & !(align - 1);
let restricting_sids =
std::slice::from_raw_parts(entries_addr as *const SID_AND_ATTRIBUTES, group_count);
Ok(restricting_sids
.iter()
.any(|entry| EqualSid(entry.Sid, expected_sid) != 0))
}
#[test]
fn elevated_token_includes_network_proxy_restricting_sid() -> Result<()> {
let capability_sid = LocalSid::from_string("S-1-5-21-10-20-30-40")?;
let network_proxy_sid = LocalSid::from_string("S-1-5-21-50-60-70-80")?;
let base_token = unsafe { get_current_token_for_restriction()? };
let restricted_token = unsafe {
create_readonly_token_with_caps_and_user_from(
base_token,
&[capability_sid.as_ptr()],
&[network_proxy_sid.as_ptr()],
)?
};
let has_network_proxy_sid =
unsafe { token_has_restricting_sid(restricted_token, network_proxy_sid.as_ptr()) };
unsafe {
CloseHandle(restricted_token);
CloseHandle(base_token);
}
assert!(has_network_proxy_sid?);
Ok(())
}
#[test]
fn default_objects_deny_other_logons_even_with_the_same_owner() -> Result<()> {
use std::os::windows::io::AsRawHandle;
use std::os::windows::io::FromRawHandle;
use std::os::windows::io::OwnedHandle;
use windows_sys::Win32::Security::ACCESS_ALLOWED_ACE;
use windows_sys::Win32::Security::AccessCheck;
use windows_sys::Win32::Security::DuplicateToken;
use windows_sys::Win32::Security::GENERIC_MAPPING;
use windows_sys::Win32::Security::GetAce;
use windows_sys::Win32::Security::InitializeSecurityDescriptor;
use windows_sys::Win32::Security::MapGenericMask;
use windows_sys::Win32::Security::SECURITY_DESCRIPTOR;
use windows_sys::Win32::Security::SecurityImpersonation;
use windows_sys::Win32::Security::SetSecurityDescriptorDacl;
use windows_sys::Win32::Security::SetSecurityDescriptorGroup;
use windows_sys::Win32::Security::SetSecurityDescriptorOwner;
use windows_sys::Win32::Storage::FileSystem::READ_CONTROL;
use windows_sys::Win32::Storage::FileSystem::WRITE_DAC;
use windows_sys::Win32::System::Threading::PROCESS_ALL_ACCESS;
use windows_sys::Win32::System::Threading::PROCESS_CREATE_THREAD;
use windows_sys::Win32::System::Threading::PROCESS_QUERY_INFORMATION;
use windows_sys::Win32::System::Threading::PROCESS_TERMINATE;
use windows_sys::Win32::System::Threading::PROCESS_VM_OPERATION;
use windows_sys::Win32::System::Threading::PROCESS_VM_READ;
use windows_sys::Win32::System::Threading::PROCESS_VM_WRITE;
unsafe {
let base = OwnedHandle::from_raw_handle(get_current_token_for_restriction()? as _);
let base_raw = base.as_raw_handle() as HANDLE;
let capability = LocalSid::from_string("S-1-5-21-10-20-30-40")?;
let victim =
OwnedHandle::from_raw_handle(create_workspace_write_token_with_caps_and_user_from(
base_raw,
&[capability.as_ptr()],
&[],
)? as _);
let mut needed = 0;
GetTokenInformation(
victim.as_raw_handle() as _,
TokenDefaultDacl,
std::ptr::null_mut(),
0,
&mut needed,
);
let mut buffer = vec![0u8; needed as usize];
ensure!(
GetTokenInformation(
victim.as_raw_handle() as _,
TokenDefaultDacl,
buffer.as_mut_ptr().cast(),
needed,
&mut needed
) != 0,
"GetTokenInformation(TokenDefaultDacl) failed: {}",
GetLastError()
);
let default_dacl = std::ptr::read_unaligned(buffer.as_ptr().cast::<TokenDefaultDaclInfo>());
let mapping = GENERIC_MAPPING {
GenericRead: READ_CONTROL | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
GenericWrite: READ_CONTROL | PROCESS_VM_WRITE | PROCESS_VM_OPERATION,
GenericExecute: READ_CONTROL,
GenericAll: PROCESS_ALL_ACCESS,
};
// Object creation maps the token default DACL's generic ACE masks to
// object-specific rights. AccessCheck expects that mapping already done.
for index in 0..u32::from((*default_dacl.default_dacl).AceCount) {
let mut ace = std::ptr::null_mut();
ensure!(GetAce(default_dacl.default_dacl, index, &mut ace) != 0);
MapGenericMask(&mut (*ace.cast::<ACCESS_ALLOWED_ACE>()).Mask, &mapping);
}
let mut owner = get_user_sid_bytes(base_raw)?;
let mut descriptor: SECURITY_DESCRIPTOR = std::mem::zeroed();
let sd = std::ptr::addr_of_mut!(descriptor).cast();
ensure!(InitializeSecurityDescriptor(sd, 1) != 0);
ensure!(SetSecurityDescriptorOwner(sd, owner.as_mut_ptr().cast(), 0) != 0);
ensure!(SetSecurityDescriptorGroup(sd, owner.as_mut_ptr().cast(), 0) != 0);
ensure!(SetSecurityDescriptorDacl(sd, 1, default_dacl.default_dacl, 0) != 0);
// Model another logon of the same account without the victim's logon
// SID. Build from the base token, retaining the shared user, Everyone,
// and filesystem capability in the write-restricting SID set.
let mut logon = get_logon_sid_bytes(base_raw)?;
let mut everyone = world_sid()?;
let disabled = SID_AND_ATTRIBUTES {
Sid: logon.as_mut_ptr().cast(),
Attributes: 0,
};
let restricting = [
capability.as_ptr(),
owner.as_mut_ptr().cast(),
everyone.as_mut_ptr().cast(),
]
.map(|sid| SID_AND_ATTRIBUTES {
Sid: sid,
Attributes: 0,
});
let mut other = std::ptr::null_mut();
ensure!(
CreateRestrictedToken(
base_raw,
DISABLE_MAX_PRIVILEGE | LUA_TOKEN | WRITE_RESTRICTED,
1,
&disabled,
0,
std::ptr::null(),
restricting.len() as u32,
restricting.as_ptr(),
&mut other
) != 0,
"CreateRestrictedToken(other logon) failed: {}",
GetLastError()
);
let other = OwnedHandle::from_raw_handle(other as _);
for (token, same_logon) in [(&victim, true), (&other, false)] {
let mut impersonation = std::ptr::null_mut();
ensure!(
DuplicateToken(
token.as_raw_handle() as _,
SecurityImpersonation,
&mut impersonation
) != 0,
"DuplicateToken failed: {}",
GetLastError()
);
let impersonation = OwnedHandle::from_raw_handle(impersonation as _);
for access in [
PROCESS_VM_READ,
PROCESS_VM_WRITE,
PROCESS_VM_OPERATION,
PROCESS_CREATE_THREAD,
PROCESS_TERMINATE,
PROCESS_QUERY_INFORMATION,
WRITE_DAC,
0x43a,
] {
let mut privileges = [0u64; 128];
let mut privilege_bytes = std::mem::size_of_val(&privileges) as u32;
let mut granted = 0;
let mut allowed = 0;
ensure!(
AccessCheck(
sd,
impersonation.as_raw_handle() as _,
access,
&mapping,
privileges.as_mut_ptr().cast(),
&mut privilege_bytes,
&mut granted,
&mut allowed
) != 0,
"AccessCheck failed: {}",
GetLastError()
);
assert_eq!(allowed != 0, same_logon, "access {access:#x}");
}
}
}
Ok(())
}