use std::{
ffi::c_void,
io,
mem::{size_of, zeroed},
ptr::null_mut,
};
use windows_sys::Win32::{
Foundation::{CloseHandle, ERROR_PIPE_LOCAL, HANDLE},
Security::{
AddAccessAllowedAceEx, CreateWellKnownSid, GetLengthSid, GetTokenInformation,
InitializeAcl, InitializeSecurityDescriptor, SetSecurityDescriptorControl,
SetSecurityDescriptorDacl, SetSecurityDescriptorOwner, TokenUser,
WinBuiltinAdministratorsSid, WinLocalSystemSid, ACCESS_ALLOWED_ACE, ACL, ACL_REVISION,
PSECURITY_DESCRIPTOR, PSID, SECURITY_ATTRIBUTES, SECURITY_DESCRIPTOR,
SECURITY_MAX_SID_SIZE, SE_DACL_PROTECTED, TOKEN_QUERY, TOKEN_USER,
},
System::Pipes::{GetNamedPipeClientComputerNameW, GetNamedPipeClientProcessId},
System::Threading::{GetCurrentProcess, OpenProcessToken},
};
const FILE_ALL_ACCESS: u32 = 0x001F_01FF;
const SECURITY_DESCRIPTOR_REVISION: u32 = 1;
struct Handle(HANDLE);
impl Drop for Handle {
fn drop(&mut self) {
unsafe { CloseHandle(self.0) };
}
}
fn last_error() -> io::Error {
io::Error::last_os_error()
}
fn current_user_sid() -> io::Result<Vec<u8>> {
let process = unsafe { GetCurrentProcess() };
let mut token: HANDLE = null_mut();
if unsafe { OpenProcessToken(process, TOKEN_QUERY, &mut token) } == 0 {
return Err(last_error());
}
let token = Handle(token);
let mut size = 0u32;
unsafe { GetTokenInformation(token.0, TokenUser, null_mut(), 0, &mut size) };
if size == 0 {
return Err(last_error());
}
let mut buffer = vec![0u8; size as usize];
if unsafe {
GetTokenInformation(
token.0,
TokenUser,
buffer.as_mut_ptr().cast(),
size,
&mut size,
)
} == 0
{
return Err(last_error());
}
let user: TOKEN_USER = unsafe { std::ptr::read_unaligned(buffer.as_ptr().cast()) };
let sid_len = unsafe { GetLengthSid(user.User.Sid) };
if sid_len == 0 {
return Err(last_error());
}
Ok(
unsafe { std::slice::from_raw_parts(user.User.Sid as *const u8, sid_len as usize) }
.to_vec(),
)
}
fn well_known_sid(kind: i32) -> io::Result<Vec<u8>> {
let mut sid = vec![0u8; SECURITY_MAX_SID_SIZE as usize];
let mut len = sid.len() as u32;
if unsafe { CreateWellKnownSid(kind, null_mut(), sid.as_mut_ptr().cast(), &mut len) } == 0 {
return Err(last_error());
}
sid.truncate(len as usize);
Ok(sid)
}
pub struct OwnerOnlySecurity {
#[cfg_attr(not(feature = "tokio"), allow(dead_code))]
attributes: SECURITY_ATTRIBUTES,
_descriptor: Box<SECURITY_DESCRIPTOR>,
_acl: Box<[u8]>,
_sids: Vec<Vec<u8>>,
}
impl std::fmt::Debug for OwnerOnlySecurity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OwnerOnlySecurity").finish_non_exhaustive()
}
}
impl OwnerOnlySecurity {
pub(super) fn new() -> io::Result<Self> {
let sids = vec![
current_user_sid()?,
well_known_sid(WinLocalSystemSid)?,
well_known_sid(WinBuiltinAdministratorsSid)?,
];
let acl_capacity = sids
.iter()
.map(|sid| size_of::<ACCESS_ALLOWED_ACE>() + sid.len() + size_of::<u32>())
.sum::<usize>()
+ size_of::<ACL>()
+ 16;
let mut acl = vec![0u8; acl_capacity];
let acl_ptr = acl.as_mut_ptr().cast::<ACL>();
if unsafe { InitializeAcl(acl_ptr, acl_capacity as u32, ACL_REVISION) } == 0 {
return Err(last_error());
}
for sid in &sids {
if unsafe {
AddAccessAllowedAceEx(
acl_ptr,
ACL_REVISION,
0,
FILE_ALL_ACCESS,
sid.as_ptr().cast_mut().cast::<c_void>(),
)
} == 0
{
return Err(last_error());
}
}
let mut descriptor = Box::new(unsafe { zeroed::<SECURITY_DESCRIPTOR>() });
let descriptor_ptr: PSECURITY_DESCRIPTOR = Box::as_mut_ptr(&mut descriptor).cast();
if unsafe { InitializeSecurityDescriptor(descriptor_ptr, SECURITY_DESCRIPTOR_REVISION) }
== 0
{
return Err(last_error());
}
if unsafe { SetSecurityDescriptorDacl(descriptor_ptr, 1, acl_ptr, 0) } == 0 {
return Err(last_error());
}
if unsafe {
SetSecurityDescriptorOwner(descriptor_ptr, sids[0].as_ptr().cast_mut() as PSID, 0)
} == 0
{
return Err(last_error());
}
if unsafe {
SetSecurityDescriptorControl(descriptor_ptr, SE_DACL_PROTECTED, SE_DACL_PROTECTED)
} == 0
{
return Err(last_error());
}
Ok(Self {
attributes: SECURITY_ATTRIBUTES {
nLength: size_of::<SECURITY_ATTRIBUTES>() as u32,
lpSecurityDescriptor: descriptor_ptr,
bInheritHandle: 0,
},
_descriptor: descriptor,
_acl: acl.into_boxed_slice(),
_sids: sids,
})
}
#[cfg(feature = "tokio")]
pub fn create_end(
&mut self,
pipe_name: &std::ffi::OsStr,
first: bool,
) -> io::Result<tokio::net::windows::named_pipe::NamedPipeServer> {
let mut options = tokio::net::windows::named_pipe::ServerOptions::new();
options
.first_pipe_instance(first)
.reject_remote_clients(true);
let attributes = self.attributes_ptr();
unsafe { options.create_with_security_attributes_raw(pipe_name, attributes) }
}
#[cfg(feature = "tokio")]
fn attributes_ptr(&mut self) -> *mut c_void {
(&mut self.attributes as *mut SECURITY_ATTRIBUTES).cast()
}
}
pub(super) fn current_user_sid_string() -> io::Result<String> {
render_sid(¤t_user_sid()?)
}
pub(super) fn reject_remote_client(raw: std::os::windows::raw::HANDLE) -> io::Result<()> {
let mut computer = [0u16; 256];
let wrote = unsafe {
GetNamedPipeClientComputerNameW(
raw,
computer.as_mut_ptr(),
computer.len() as u32 * size_of::<u16>() as u32,
)
};
if wrote != 0 {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"named-pipe client is on another machine: the local endpoint refuses remote clients",
));
}
let error = last_error();
if error.raw_os_error() != Some(ERROR_PIPE_LOCAL as i32) {
return Err(error);
}
let mut pid = 0u32;
if unsafe { GetNamedPipeClientProcessId(raw, &mut pid) } == 0 {
return Err(last_error());
}
if pid == 0 {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"named-pipe client process is unknown, so it cannot be a local process",
));
}
Ok(())
}
fn render_sid(bytes: &[u8]) -> io::Result<String> {
if bytes.len() < 8 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"a SID has a 8-byte header",
));
}
let revision = bytes[0];
let count = bytes[1] as usize;
if bytes.len() < 8 + count * 4 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"a SID is shorter than its sub-authority count",
));
}
let mut authority = 0u64;
for byte in &bytes[2..8] {
authority = (authority << 8) | u64::from(*byte);
}
let mut rendered = format!("S-{revision}-{authority}");
for index in 0..count {
let start = 8 + index * 4;
let value = u32::from_le_bytes([
bytes[start],
bytes[start + 1],
bytes[start + 2],
bytes[start + 3],
]);
rendered.push_str(&format!("-{value}"));
}
Ok(rendered)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sid_render_matches_the_whoami_form() {
let expected = String::from_utf8(
std::process::Command::new("whoami")
.arg("/user")
.output()
.expect("run whoami")
.stdout,
)
.expect("whoami output is UTF-8");
let expected = expected
.split_whitespace()
.find(|token| token.starts_with("S-1-"))
.expect("whoami /user names the SID");
assert_eq!(current_user_sid_string().unwrap(), expected);
}
#[cfg(feature = "tokio")]
#[test]
fn owner_only_security_hands_out_a_usable_pointer() {
let mut security = OwnerOnlySecurity::new().expect("build the owner-only DACL");
assert!(!security.attributes_ptr().is_null());
}
}