use std::ffi::c_void;
use std::io;
use std::mem::size_of;
use std::os::windows::io::AsRawHandle;
use std::os::windows::io::BorrowedHandle;
use std::os::windows::io::FromRawHandle;
use std::os::windows::io::OwnedHandle;
use std::ptr::null;
use std::ptr::null_mut;
use windows_sys::Win32::Foundation::ERROR_INSUFFICIENT_BUFFER;
use windows_sys::Win32::Foundation::ERROR_NO_TOKEN;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Foundation::TRUE;
use windows_sys::Win32::Security::ACE_HEADER;
use windows_sys::Win32::Security::ACL;
use windows_sys::Win32::Security::ACL_SIZE_INFORMATION;
use windows_sys::Win32::Security::AclSizeInformation;
use windows_sys::Win32::Security::CONTAINER_INHERIT_ACE;
use windows_sys::Win32::Security::CreatePrivateObjectSecurityEx;
use windows_sys::Win32::Security::DACL_SECURITY_INFORMATION;
use windows_sys::Win32::Security::DestroyPrivateObjectSecurity;
use windows_sys::Win32::Security::GENERIC_MAPPING;
use windows_sys::Win32::Security::GROUP_SECURITY_INFORMATION;
use windows_sys::Win32::Security::GetAce;
use windows_sys::Win32::Security::GetAclInformation;
use windows_sys::Win32::Security::GetKernelObjectSecurity;
use windows_sys::Win32::Security::GetSecurityDescriptorControl;
use windows_sys::Win32::Security::GetSecurityDescriptorDacl;
use windows_sys::Win32::Security::GetTokenInformation;
use windows_sys::Win32::Security::OBJECT_INHERIT_ACE;
use windows_sys::Win32::Security::OBJECT_SECURITY_INFORMATION;
use windows_sys::Win32::Security::OWNER_SECURITY_INFORMATION;
use windows_sys::Win32::Security::PROTECTED_DACL_SECURITY_INFORMATION;
use windows_sys::Win32::Security::PSECURITY_DESCRIPTOR;
use windows_sys::Win32::Security::PSID;
use windows_sys::Win32::Security::SE_DACL_AUTO_INHERIT_REQ;
use windows_sys::Win32::Security::SE_DACL_AUTO_INHERITED;
use windows_sys::Win32::Security::SE_DACL_PROTECTED;
use windows_sys::Win32::Security::SEF_AVOID_OWNER_CHECK;
use windows_sys::Win32::Security::SEF_AVOID_PRIVILEGE_CHECK;
use windows_sys::Win32::Security::SEF_DACL_AUTO_INHERIT;
use windows_sys::Win32::Security::SetKernelObjectSecurity;
use windows_sys::Win32::Security::SetSecurityDescriptorControl;
use windows_sys::Win32::Security::TOKEN_QUERY;
use windows_sys::Win32::Security::TOKEN_USER;
use windows_sys::Win32::Security::TokenUser as TokenUserClass;
use windows_sys::Win32::Security::UNPROTECTED_DACL_SECURITY_INFORMATION;
use windows_sys::Win32::Storage::FileSystem::FILE_ALL_ACCESS;
use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_EXECUTE;
use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_READ;
use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_WRITE;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
use windows_sys::Win32::System::Threading::GetCurrentThread;
use windows_sys::Win32::System::Threading::OpenProcessToken;
use windows_sys::Win32::System::Threading::OpenThreadToken;
use super::has_code;
use super::length_u32;
use super::win32;
#[cfg(test)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::formats) enum TokenSource {
Thread,
Process,
}
#[derive(Debug)]
pub(in crate::formats) struct EffectiveToken {
handle: OwnedHandle,
#[cfg(test)]
source: TokenSource,
}
impl EffectiveToken {
pub(in crate::formats) fn open() -> io::Result<Self> {
let mut token: HANDLE = null_mut();
let thread =
unsafe { OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &raw mut token) };
if thread != 0 {
let handle = unsafe { OwnedHandle::from_raw_handle(token) };
return Ok(Self {
handle,
#[cfg(test)]
source: TokenSource::Thread,
});
}
let err = io::Error::last_os_error();
if !has_code(&err, ERROR_NO_TOKEN) {
return Err(err);
}
win32(unsafe { OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &raw mut token) })?;
let handle = unsafe { OwnedHandle::from_raw_handle(token) };
Ok(Self {
handle,
#[cfg(test)]
source: TokenSource::Process,
})
}
#[cfg(test)]
pub(in crate::formats) const fn source(&self) -> TokenSource {
self.source
}
pub(in crate::formats) fn user(&self) -> io::Result<TokenUser> {
let mut needed = 0_u32;
unsafe {
GetTokenInformation(
self.handle.as_raw_handle(),
TokenUserClass,
null_mut(),
0,
&raw mut needed,
);
}
if needed == 0 {
return Err(io::Error::last_os_error());
}
let mut buffer = vec![0_u64; (needed as usize).div_ceil(size_of::<u64>())];
let ok = unsafe {
GetTokenInformation(
self.handle.as_raw_handle(),
TokenUserClass,
buffer.as_mut_ptr().cast(),
needed,
&raw mut needed,
)
};
win32(ok)?;
Ok(TokenUser { buffer })
}
fn raw(&self) -> HANDLE {
self.handle.as_raw_handle()
}
}
#[derive(Debug)]
pub(in crate::formats) struct TokenUser {
buffer: Vec<u64>,
}
impl TokenUser {
pub(in crate::formats) fn sid(&self) -> PSID {
unsafe { self.buffer.as_ptr().cast::<TOKEN_USER>().read() }
.User
.Sid
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::formats) enum Part {
OwnerAndGroup,
OwnerGroupAndDacl,
}
impl Part {
const fn information(self) -> OBJECT_SECURITY_INFORMATION {
match self {
Self::OwnerAndGroup => OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION,
Self::OwnerGroupAndDacl => {
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::formats) enum DaclState {
Null,
Present {
entries: u32,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DaclProtection {
Protected,
Inheriting,
}
impl DaclProtection {
const fn of(control: u16) -> Self {
if control & SE_DACL_PROTECTED != 0 {
Self::Protected
} else {
Self::Inheriting
}
}
const fn flag(self) -> OBJECT_SECURITY_INFORMATION {
match self {
Self::Protected => PROTECTED_DACL_SECURITY_INFORMATION,
Self::Inheriting => UNPROTECTED_DACL_SECURITY_INFORMATION,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::formats) enum ObjectKind {
Container,
Leaf,
}
fn control_of(descriptor: PSECURITY_DESCRIPTOR) -> io::Result<u16> {
let mut control = 0_u16;
let mut revision = 0_u32;
let ok =
unsafe { GetSecurityDescriptorControl(descriptor, &raw mut control, &raw mut revision) };
win32(ok)?;
Ok(control)
}
fn request_auto_inherited(descriptor: PSECURITY_DESCRIPTOR) -> io::Result<()> {
if control_of(descriptor)? & SE_DACL_AUTO_INHERITED == 0 {
return Ok(());
}
let ok = unsafe {
SetSecurityDescriptorControl(
descriptor,
SE_DACL_AUTO_INHERIT_REQ,
SE_DACL_AUTO_INHERIT_REQ,
)
};
win32(ok)
}
#[derive(Debug)]
pub(in crate::formats) struct SecurityDescriptor(Box<[u64]>);
impl SecurityDescriptor {
const READ_ATTEMPTS: usize = 4;
pub(in crate::formats) fn read(handle: BorrowedHandle<'_>, part: Part) -> io::Result<Self> {
let mut words = vec![0_u64; 64];
for _ in 0..Self::READ_ATTEMPTS {
let capacity = words.len().saturating_mul(size_of::<u64>());
let mut needed = 0_u32;
let ok = unsafe {
GetKernelObjectSecurity(
handle.as_raw_handle(),
part.information(),
words.as_mut_ptr().cast(),
length_u32(capacity)?,
&raw mut needed,
)
};
if ok != 0 {
let mut read = Self(words.into_boxed_slice());
request_auto_inherited(read.as_mut_ptr())?;
return Ok(read);
}
let err = io::Error::last_os_error();
if !has_code(&err, ERROR_INSUFFICIENT_BUFFER) || needed as usize <= capacity {
return Err(err);
}
words = vec![0_u64; (needed as usize).div_ceil(size_of::<u64>())];
}
Err(io::Error::from(io::ErrorKind::InvalidData))
}
fn as_mut_ptr(&mut self) -> PSECURITY_DESCRIPTOR {
self.0.as_mut_ptr().cast()
}
fn as_ptr(&self) -> PSECURITY_DESCRIPTOR {
self.0.as_ptr().cast_mut().cast()
}
}
fn dacl_of(descriptor: PSECURITY_DESCRIPTOR) -> io::Result<Option<(*mut ACL, u32)>> {
let mut present = 0;
let mut defaulted = 0;
let mut acl: *mut ACL = null_mut();
let ok = unsafe {
GetSecurityDescriptorDacl(
descriptor,
&raw mut present,
&raw mut acl,
&raw mut defaulted,
)
};
win32(ok)?;
if present == 0 || acl.is_null() {
return Ok(None);
}
let mut size = ACL_SIZE_INFORMATION::default();
let ok = unsafe {
GetAclInformation(
acl,
(&raw mut size).cast(),
length_u32(size_of::<ACL_SIZE_INFORMATION>())?,
AclSizeInformation,
)
};
win32(ok)?;
Ok(Some((acl, size.AceCount)))
}
#[derive(Debug)]
pub(in crate::formats) struct Derived(PSECURITY_DESCRIPTOR);
impl Derived {
pub(in crate::formats) fn new(
parent: &Descriptor<'_>,
creator: &SecurityDescriptor,
kind: ObjectKind,
token: &EffectiveToken,
) -> io::Result<Self> {
let mapping = GENERIC_MAPPING {
GenericRead: FILE_GENERIC_READ,
GenericWrite: FILE_GENERIC_WRITE,
GenericExecute: FILE_GENERIC_EXECUTE,
GenericAll: FILE_ALL_ACCESS,
};
let mut flags = SEF_AVOID_OWNER_CHECK | SEF_AVOID_PRIVILEGE_CHECK;
if control_of(parent.as_ptr())? & SE_DACL_AUTO_INHERITED != 0 {
flags |= SEF_DACL_AUTO_INHERIT;
}
let mut derived: PSECURITY_DESCRIPTOR = null_mut();
let ok = unsafe {
CreatePrivateObjectSecurityEx(
parent.as_ptr(),
creator.as_ptr(),
&raw mut derived,
null(),
i32::from(kind == ObjectKind::Container),
flags,
token.raw(),
&raw const mapping,
)
};
win32(ok)?;
let derived = Self(derived);
request_auto_inherited(derived.0)?;
Ok(derived)
}
pub(in crate::formats) fn dacl_state(&self) -> io::Result<DaclState> {
Ok(match dacl_of(self.0)? {
None => DaclState::Null,
Some((_, entries)) => DaclState::Present { entries },
})
}
}
impl Drop for Derived {
fn drop(&mut self) {
unsafe { DestroyPrivateObjectSecurity(&raw const self.0) };
}
}
#[derive(Debug)]
pub(in crate::formats) enum Descriptor<'a> {
Read(&'a SecurityDescriptor),
Derived(Derived),
}
impl Descriptor<'_> {
pub(in crate::formats) fn has_inheritable_entries(&self) -> io::Result<bool> {
let Some((acl, entries)) = dacl_of(self.as_ptr())? else {
return Ok(false);
};
for index in 0..entries {
let mut ace: *mut c_void = null_mut();
win32(unsafe { GetAce(acl, index, &raw mut ace) })?;
let header = unsafe { ace.cast::<ACE_HEADER>().read_unaligned() };
if u32::from(header.AceFlags) & (OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE) != 0 {
return Ok(true);
}
}
Ok(false)
}
fn as_ptr(&self) -> PSECURITY_DESCRIPTOR {
match self {
Self::Read(read) => read.as_ptr(),
Self::Derived(derived) => derived.0,
}
}
}
pub(in crate::formats) fn apply_dacl(
handle: BorrowedHandle<'_>,
descriptor: &Descriptor<'_>,
) -> io::Result<()> {
let ptr = descriptor.as_ptr();
let protection = DaclProtection::of(control_of(ptr)?);
let ok = unsafe {
SetKernelObjectSecurity(
handle.as_raw_handle(),
DACL_SECURITY_INFORMATION | protection.flag(),
ptr,
)
};
win32(ok)
}
#[derive(Debug)]
pub(crate) struct DirSecurity(SecurityDescriptor);
impl DirSecurity {
pub(in crate::formats) const fn new(read: SecurityDescriptor) -> Self {
Self(read)
}
pub(in crate::formats) const fn descriptor(&self) -> Descriptor<'_> {
Descriptor::Read(&self.0)
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::formats::pinned_dir::dacl_probe;
#[test]
fn the_token_is_the_process_token_without_impersonation() {
let token = EffectiveToken::open().unwrap();
assert_eq!(token.source(), TokenSource::Process);
assert_eq!(
dacl_probe::sid_string_of(&token.user().unwrap()),
dacl_probe::current_user()
);
}
#[test]
fn the_token_is_the_thread_token_under_impersonation() {
let _impersonation = dacl_probe::Impersonation::start();
let token = EffectiveToken::open().unwrap();
assert_eq!(token.source(), TokenSource::Thread);
assert_eq!(
dacl_probe::sid_string_of(&token.user().unwrap()),
dacl_probe::current_user()
);
}
}