123 lines
4.6 KiB
Rust
123 lines
4.6 KiB
Rust
//! Shared protected public signing binding; never learns trust from a worker ticket.
|
|
use crate::native;
|
|
use anyhow::{Context, Result, ensure};
|
|
use rmm_agent_core::auth::Binding;
|
|
use std::path::Path;
|
|
fn read_private(path: &Path, limit: u64) -> Result<Vec<u8>> {
|
|
use std::{fs::OpenOptions, io::Read, os::windows::fs::OpenOptionsExt};
|
|
use windows_sys::Win32::Storage::FileSystem::{FILE_FLAG_OPEN_REPARSE_POINT, FILE_SHARE_READ};
|
|
native::private_path(path, false)?;
|
|
let mut file = OpenOptions::new()
|
|
.read(true)
|
|
.share_mode(FILE_SHARE_READ)
|
|
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
|
|
.open(path)?;
|
|
ensure!(file.metadata()?.len() <= limit, "BINDING_SIZE");
|
|
let mut bytes = Vec::new();
|
|
(&mut file).take(limit + 1).read_to_end(&mut bytes)?;
|
|
ensure!(bytes.len() as u64 <= limit, "BINDING_SIZE");
|
|
Ok(bytes)
|
|
}
|
|
pub fn check_admin_readonly(path: &Path) -> Result<()> {
|
|
use std::ptr;
|
|
use windows_sys::Win32::{
|
|
Foundation::LocalFree,
|
|
Security::{
|
|
self, ACCESS_ALLOWED_ACE, ACL,
|
|
Authorization::{ConvertSidToStringSidW, GetNamedSecurityInfoW, SE_FILE_OBJECT},
|
|
DACL_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION,
|
|
},
|
|
};
|
|
unsafe fn sid(sid: Security::PSID) -> Result<String> {
|
|
let mut raw = ptr::null_mut();
|
|
ensure!(
|
|
unsafe { ConvertSidToStringSidW(sid, &mut raw) } != 0,
|
|
"BINDING_SID"
|
|
);
|
|
let mut len = 0;
|
|
while unsafe { *raw.add(len) } != 0 {
|
|
len += 1;
|
|
ensure!(len < 256, "BINDING_SID_LIMIT");
|
|
}
|
|
let value = String::from_utf16(unsafe { std::slice::from_raw_parts(raw, len) })?;
|
|
unsafe {
|
|
LocalFree(raw.cast());
|
|
}
|
|
Ok(value)
|
|
}
|
|
ensure!(path.is_absolute(), "ABSOLUTE_JOB_BINDING_REQUIRED");
|
|
// The immediate configuration directory must also deny agent replacement.
|
|
for (item, directory) in [
|
|
(path, false),
|
|
(path.parent().context("BINDING_PARENT")?, true),
|
|
] {
|
|
native::private_path(item, directory)?;
|
|
let mut owner = ptr::null_mut();
|
|
let mut dacl: *mut ACL = ptr::null_mut();
|
|
let mut descriptor = ptr::null_mut();
|
|
let status = unsafe {
|
|
GetNamedSecurityInfoW(
|
|
native::wide(item).as_ptr(),
|
|
SE_FILE_OBJECT,
|
|
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
|
|
&mut owner,
|
|
ptr::null_mut(),
|
|
&mut dacl,
|
|
ptr::null_mut(),
|
|
&mut descriptor,
|
|
)
|
|
};
|
|
ensure!(status == 0, "BINDING_SECURITY");
|
|
let check = (|| -> Result<()> {
|
|
ensure!(!owner.is_null() && !dacl.is_null(), "BINDING_NULL_SECURITY");
|
|
ensure!(
|
|
matches!(unsafe { sid(owner) }?.as_str(), "S-1-5-18" | "S-1-5-32-544"),
|
|
"BINDING_ADMIN_OWNER_REQUIRED"
|
|
);
|
|
for index in 0..unsafe { (*dacl).AceCount } {
|
|
let mut raw = ptr::null_mut();
|
|
ensure!(
|
|
unsafe { Security::GetAce(dacl, u32::from(index), &mut raw) } != 0,
|
|
"BINDING_ACE"
|
|
);
|
|
let ace = unsafe { &*raw.cast::<ACCESS_ALLOWED_ACE>() };
|
|
ensure!(
|
|
ace.Header.AceType == 0
|
|
&& usize::from(ace.Header.AceSize)
|
|
>= std::mem::size_of::<ACCESS_ALLOWED_ACE>(),
|
|
"BINDING_ACE_TYPE"
|
|
);
|
|
let who = unsafe { sid(ptr::addr_of!(ace.SidStart).cast_mut().cast()) }?;
|
|
if !matches!(who.as_str(), "S-1-5-18" | "S-1-5-32-544") {
|
|
// Generic write/all, owner/DACL/delete and all file/directory mutation bits.
|
|
ensure!(ace.Mask & 0x500d_0156 == 0, "BINDING_AGENT_WRITE_FORBIDDEN");
|
|
}
|
|
}
|
|
Ok(())
|
|
})();
|
|
unsafe {
|
|
LocalFree(descriptor.cast());
|
|
}
|
|
check?;
|
|
}
|
|
Ok(())
|
|
}
|
|
pub fn readonly_binding(path: &Path) -> Result<Binding> {
|
|
check_admin_readonly(path)?;
|
|
let binding: Binding = serde_json::from_slice(&read_private(path, 8192)?)?;
|
|
ensure!(
|
|
!binding.tenant_id.is_nil()
|
|
&& !binding.agent_id.is_nil()
|
|
&& !binding.asset_id.is_nil()
|
|
&& binding.enrollment_generation > 0
|
|
&& binding.assignment_version > 0
|
|
&& binding.public_key_hex.len() == 64
|
|
&& binding
|
|
.public_key_hex
|
|
.bytes()
|
|
.all(|b| b.is_ascii_hexdigit()),
|
|
"JOB_BINDING_PROFILE"
|
|
);
|
|
Ok(binding)
|
|
}
|