mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
Restrict Windows sandbox default object access to the logon session (#47361)
## Why Shared filesystem capabilities must not grant access to another logon session's processes, threads, or IPC objects. A shared account's implicit owner rights can also allow another logon to rewrite an object's DACL. ## What changed - Grant full access in the token's default DACL only to the runner's logon SID, removing grants to Everyone and capability SIDs. - Add an `OWNER RIGHTS` entry granting only `READ_CONTROL` to suppress the owner's implicit `WRITE_DAC` access. - Close the new token handle if default DACL or privilege setup fails. ## Testing Add a Windows `AccessCheck` regression test verifying that process memory, thread creation, termination, query, and DACL modification rights remain available to the creating logon but are denied to a simulated other logon sharing the same owner and capability. GitOrigin-RevId: 94608466ee1b461fe10db1866e80b3988119bdfc
This commit is contained in:
@@ -51,27 +51,32 @@ struct TokenDefaultDaclInfo {
|
||||
default_dacl: *mut ACL,
|
||||
}
|
||||
|
||||
/// Sets a permissive default DACL so sandboxed processes can create pipes/IPC objects
|
||||
/// without hitting ACCESS_DENIED when PowerShell builds pipelines.
|
||||
unsafe fn set_default_dacl(h_token: HANDLE, sids: &[*mut c_void]) -> Result<()> {
|
||||
if sids.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let entries: Vec<EXPLICIT_ACCESS_W> = sids
|
||||
.iter()
|
||||
.map(|sid| EXPLICIT_ACCESS_W {
|
||||
grfAccessPermissions: GENERIC_ALL,
|
||||
grfAccessMode: GRANT_ACCESS,
|
||||
grfInheritance: 0,
|
||||
Trustee: TRUSTEE_W {
|
||||
pMultipleTrustee: std::ptr::null_mut(),
|
||||
MultipleTrusteeOperation: 0,
|
||||
TrusteeForm: TRUSTEE_IS_SID,
|
||||
TrusteeType: TRUSTEE_IS_UNKNOWN,
|
||||
ptstrName: *sid as *mut u16,
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
/// Keep child-process and IPC access within the runner's logon session.
|
||||
/// Elevated runners have distinct logon SIDs even when they use the same account.
|
||||
unsafe fn set_default_dacl(h_token: HANDLE, logon_sid: *mut c_void) -> Result<()> {
|
||||
let owner_rights = LocalSid::from_string("S-1-3-4")?;
|
||||
// The shared account also owns these objects. An OWNER RIGHTS ACE suppresses
|
||||
// its implicit WRITE_DAC grant, which would otherwise let a different logon
|
||||
// rewrite this DACL. The creating logon retains full access explicitly.
|
||||
let entries = [
|
||||
(logon_sid, GENERIC_ALL),
|
||||
(
|
||||
owner_rights.as_ptr(),
|
||||
windows_sys::Win32::Storage::FileSystem::READ_CONTROL,
|
||||
),
|
||||
]
|
||||
.map(|(sid, access)| EXPLICIT_ACCESS_W {
|
||||
grfAccessPermissions: access,
|
||||
grfAccessMode: GRANT_ACCESS,
|
||||
grfInheritance: 0,
|
||||
Trustee: TRUSTEE_W {
|
||||
pMultipleTrustee: std::ptr::null_mut(),
|
||||
MultipleTrusteeOperation: 0,
|
||||
TrusteeForm: TRUSTEE_IS_SID,
|
||||
TrusteeType: TRUSTEE_IS_UNKNOWN,
|
||||
ptstrName: sid as *mut u16,
|
||||
},
|
||||
});
|
||||
let mut p_new_dacl: *mut ACL = std::ptr::null_mut();
|
||||
let res = SetEntriesInAclW(
|
||||
entries.len() as u32,
|
||||
@@ -508,15 +513,14 @@ unsafe fn create_token_with_caps_from(
|
||||
return Err(anyhow!("CreateRestrictedToken failed: {}", GetLastError()));
|
||||
}
|
||||
|
||||
// Additional restricting SIDs are identity markers, not capabilities. Deliberately exclude
|
||||
// them from the default DACL so possessing a route identity cannot grant object access.
|
||||
let mut dacl_sids: Vec<*mut c_void> = Vec::with_capacity(psid_capabilities.len() + 2);
|
||||
dacl_sids.push(psid_logon);
|
||||
dacl_sids.push(psid_everyone);
|
||||
dacl_sids.extend_from_slice(psid_capabilities);
|
||||
set_default_dacl(new_token, &dacl_sids)?;
|
||||
|
||||
enable_single_privilege(new_token, "SeChangeNotifyPrivilege")?;
|
||||
// Filesystem and route capabilities may be shared between launches. They
|
||||
// must not grant access to another launch's processes, threads, or IPC.
|
||||
if let Err(error) = set_default_dacl(new_token, psid_logon)
|
||||
.and_then(|()| enable_single_privilege(new_token, "SeChangeNotifyPrivilege"))
|
||||
{
|
||||
CloseHandle(new_token);
|
||||
return Err(error);
|
||||
}
|
||||
Ok(new_token)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
use windows_sys::Win32::Security::EqualSid;
|
||||
use windows_sys::Win32::Security::TokenRestrictedSids;
|
||||
|
||||
@@ -67,3 +68,164 @@ fn elevated_token_includes_network_proxy_restricting_sid() -> Result<()> {
|
||||
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 = 0;
|
||||
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 = 0;
|
||||
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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user