#![allow(unsafe_code)]
use super::{
CapturedPeer, CodeRequirementOutcome, CodeVerificationError, ForcedFailures, ObservedPeer,
PeerObservation, PeerUser, VerifiedCode,
};
use std::{
ffi::{c_char, c_void, CStr, OsString},
mem::size_of,
os::{
fd::{AsRawFd, BorrowedFd},
unix::ffi::OsStringExt,
},
path::PathBuf,
ptr::null,
sync::OnceLock,
};
type CFTypeRef = *const c_void;
type CFIndex = isize;
type OSStatus = i32;
const ERR_SEC_SUCCESS: OSStatus = 0;
const SEC_CS_DEFAULT_FLAGS: u32 = 0;
const CF_STRING_ENCODING_UTF8: u32 = 0x0800_0100;
#[repr(C)]
#[derive(Clone, Copy, Default)]
pub(super) struct AuditToken {
val: [u32; 8],
}
#[link(name = "bsm")]
extern "C" {
fn audit_token_to_euid(token: AuditToken) -> libc::uid_t;
fn audit_token_to_pid(token: AuditToken) -> libc::pid_t;
}
struct Api {
cf_data_create: unsafe extern "C" fn(CFTypeRef, *const u8, CFIndex) -> CFTypeRef,
cf_dictionary_create: unsafe extern "C" fn(
CFTypeRef,
*const CFTypeRef,
*const CFTypeRef,
CFIndex,
*const c_void,
*const c_void,
) -> CFTypeRef,
cf_dictionary_get_value: unsafe extern "C" fn(CFTypeRef, CFTypeRef) -> CFTypeRef,
cf_get_type_id: unsafe extern "C" fn(CFTypeRef) -> usize,
cf_string_get_type_id: unsafe extern "C" fn() -> usize,
cf_dictionary_get_type_id: unsafe extern "C" fn() -> usize,
cf_number_get_type_id: unsafe extern "C" fn() -> usize,
cf_number_get_value: unsafe extern "C" fn(CFTypeRef, CFIndex, *mut c_void) -> u8,
cf_string_create_with_bytes:
unsafe extern "C" fn(CFTypeRef, *const u8, CFIndex, u32, u8) -> CFTypeRef,
cf_string_get_length: unsafe extern "C" fn(CFTypeRef) -> CFIndex,
cf_string_get_maximum_size_for_encoding: unsafe extern "C" fn(CFIndex, u32) -> CFIndex,
cf_string_get_c_string: unsafe extern "C" fn(CFTypeRef, *mut c_char, CFIndex, u32) -> u8,
cf_url_get_file_system_representation:
unsafe extern "C" fn(CFTypeRef, u8, *mut u8, CFIndex) -> u8,
cf_release: unsafe extern "C" fn(CFTypeRef),
dictionary_key_callbacks: *const c_void,
dictionary_value_callbacks: *const c_void,
sec_code_copy_guest_with_attributes:
unsafe extern "C" fn(CFTypeRef, CFTypeRef, u32, *mut CFTypeRef) -> OSStatus,
sec_code_copy_path: unsafe extern "C" fn(CFTypeRef, u32, *mut CFTypeRef) -> OSStatus,
sec_code_check_validity: unsafe extern "C" fn(CFTypeRef, u32, CFTypeRef) -> OSStatus,
sec_requirement_create_with_string:
unsafe extern "C" fn(CFTypeRef, u32, *mut CFTypeRef, *mut CFTypeRef) -> OSStatus,
sec_code_copy_signing_information:
unsafe extern "C" fn(CFTypeRef, u32, *mut CFTypeRef) -> OSStatus,
guest_attribute_audit: CFTypeRef,
code_info_identifier: CFTypeRef,
code_info_plist: CFTypeRef,
code_info_flags: CFTypeRef,
}
unsafe impl Send for Api {}
unsafe impl Sync for Api {}
fn api() -> Option<&'static Api> {
static API: OnceLock<Option<Api>> = OnceLock::new();
API.get_or_init(load).as_ref()
}
fn load() -> Option<Api> {
let cf = open(c"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")?;
let security = open(c"/System/Library/Frameworks/Security.framework/Security")?;
unsafe {
Some(Api {
cf_data_create: function(cf, c"CFDataCreate")?,
cf_dictionary_create: function(cf, c"CFDictionaryCreate")?,
cf_dictionary_get_value: function(cf, c"CFDictionaryGetValue")?,
cf_get_type_id: function(cf, c"CFGetTypeID")?,
cf_string_get_type_id: function(cf, c"CFStringGetTypeID")?,
cf_dictionary_get_type_id: function(cf, c"CFDictionaryGetTypeID")?,
cf_number_get_type_id: function(cf, c"CFNumberGetTypeID")?,
cf_number_get_value: function(cf, c"CFNumberGetValue")?,
cf_string_create_with_bytes: function(cf, c"CFStringCreateWithBytes")?,
cf_string_get_length: function(cf, c"CFStringGetLength")?,
cf_string_get_maximum_size_for_encoding: function(
cf,
c"CFStringGetMaximumSizeForEncoding",
)?,
cf_string_get_c_string: function(cf, c"CFStringGetCString")?,
cf_url_get_file_system_representation: function(
cf,
c"CFURLGetFileSystemRepresentation",
)?,
cf_release: function(cf, c"CFRelease")?,
dictionary_key_callbacks: symbol(cf, c"kCFTypeDictionaryKeyCallBacks")?,
dictionary_value_callbacks: symbol(cf, c"kCFTypeDictionaryValueCallBacks")?,
sec_code_copy_guest_with_attributes: function(
security,
c"SecCodeCopyGuestWithAttributes",
)?,
sec_code_copy_path: function(security, c"SecCodeCopyPath")?,
sec_code_check_validity: function(security, c"SecCodeCheckValidity")?,
sec_requirement_create_with_string: function(
security,
c"SecRequirementCreateWithStringAndErrors",
)?,
sec_code_copy_signing_information: function(
security,
c"SecCodeCopySigningInformation",
)?,
guest_attribute_audit: constant(security, c"kSecGuestAttributeAudit")?,
code_info_identifier: constant(security, c"kSecCodeInfoIdentifier")?,
code_info_plist: constant(security, c"kSecCodeInfoPList")?,
code_info_flags: constant(security, c"kSecCodeInfoFlags")?,
})
}
}
fn open(path: &CStr) -> Option<*mut c_void> {
let library = unsafe { libc::dlopen(path.as_ptr(), libc::RTLD_LAZY | libc::RTLD_LOCAL) };
(!library.is_null()).then_some(library)
}
unsafe fn symbol(library: *mut c_void, name: &CStr) -> Option<*const c_void> {
let address = libc::dlsym(library, name.as_ptr());
(!address.is_null()).then_some(address.cast_const())
}
unsafe fn function<F: Copy>(library: *mut c_void, name: &CStr) -> Option<F> {
const { assert!(size_of::<F>() == size_of::<*const c_void>()) };
symbol(library, name).map(|address| std::mem::transmute_copy(&address))
}
unsafe fn constant(library: *mut c_void, name: &CStr) -> Option<CFTypeRef> {
let value = *symbol(library, name)?.cast::<CFTypeRef>();
(!value.is_null()).then_some(value)
}
struct Owned {
value: CFTypeRef,
api: &'static Api,
}
impl Owned {
fn new(api: &'static Api, value: CFTypeRef) -> Option<Self> {
(!value.is_null()).then(|| Self { value, api })
}
}
impl Drop for Owned {
fn drop(&mut self) {
unsafe { (self.api.cf_release)(self.value) };
}
}
pub(super) fn observe(socket: BorrowedFd<'_>, forced: ForcedFailures) -> ObservedPeer {
capture(socket, forced).observation
}
pub(super) fn capture(socket: BorrowedFd<'_>, forced: ForcedFailures) -> CapturedPeer {
let mut peer = capture_credentials(socket, forced);
peer.observation = observe_code(&peer, forced);
peer
}
pub(super) fn capture_credentials(socket: BorrowedFd<'_>, forced: ForcedFailures) -> CapturedPeer {
let Some(token) = audit_token(socket).filter(|_| !forced.user) else {
return ObservedPeer::Unobserved.into();
};
let (uid, pid) = unsafe { (audit_token_to_euid(token), audit_token_to_pid(token)) };
let Ok(pid) = u32::try_from(pid) else {
return ObservedPeer::Unobserved.into();
};
CapturedPeer {
observation: ObservedPeer::Observed(PeerObservation {
user: PeerUser::Uid(uid),
pid,
exe: None,
signing_id: None,
}),
token: Some(token),
}
}
pub(super) fn peer_pid(socket: std::os::fd::RawFd) -> Option<u32> {
let mut pid: libc::pid_t = 0;
let mut len = size_of::<libc::pid_t>() as libc::socklen_t;
let ok = unsafe {
libc::getsockopt(
socket,
libc::SOL_LOCAL,
libc::LOCAL_PEERPID,
(&mut pid as *mut libc::pid_t).cast(),
&mut len,
)
} == 0;
(ok && len as usize == size_of::<libc::pid_t>() && pid > 0).then_some(pid as u32)
}
pub(super) fn observe_code(peer: &CapturedPeer, forced: ForcedFailures) -> ObservedPeer {
let (Some(token), ObservedPeer::Observed(mut observation)) =
(peer.token, peer.observation.clone())
else {
return peer.observation.clone();
};
let guest = guest_code(token);
observation.exe = guest.as_ref().filter(|_| !forced.exe).and_then(code_path);
observation.signing_id = guest
.as_ref()
.filter(|code| !forced.signing_id && valid(code))
.and_then(signing_identifier);
ObservedPeer::Observed(observation)
}
fn audit_token(socket: BorrowedFd<'_>) -> Option<AuditToken> {
let mut token = AuditToken::default();
let mut length = size_of::<AuditToken>() as libc::socklen_t;
let status = unsafe {
libc::getsockopt(
socket.as_raw_fd(),
libc::SOL_LOCAL,
libc::LOCAL_PEERTOKEN,
(&mut token as *mut AuditToken).cast(),
&mut length,
)
};
(status == 0 && length as usize == size_of::<AuditToken>()).then_some(token)
}
fn guest_code(token: AuditToken) -> Option<Owned> {
let api = api()?;
let data = Owned::new(api, unsafe {
(api.cf_data_create)(
null(),
(&token as *const AuditToken).cast(),
size_of::<AuditToken>() as CFIndex,
)
})?;
let keys = [api.guest_attribute_audit];
let values = [data.value];
let attributes = Owned::new(api, unsafe {
(api.cf_dictionary_create)(
null(),
keys.as_ptr(),
values.as_ptr(),
1,
api.dictionary_key_callbacks,
api.dictionary_value_callbacks,
)
})?;
let mut guest = null();
let status = unsafe {
(api.sec_code_copy_guest_with_attributes)(
null(),
attributes.value,
SEC_CS_DEFAULT_FLAGS,
&mut guest,
)
};
let guest = Owned::new(api, guest);
(status == ERR_SEC_SUCCESS).then_some(guest).flatten()
}
fn code_path(code: &Owned) -> Option<PathBuf> {
let api = code.api;
let mut url = null();
let status = unsafe { (api.sec_code_copy_path)(code.value, SEC_CS_DEFAULT_FLAGS, &mut url) };
let url = Owned::new(api, url);
let url = (status == ERR_SEC_SUCCESS).then_some(url).flatten()?;
let mut buffer = vec![0u8; libc::PATH_MAX as usize + 1];
let converted = unsafe {
(api.cf_url_get_file_system_representation)(
url.value,
1,
buffer.as_mut_ptr(),
buffer.len() as CFIndex,
)
};
if converted == 0 {
return None;
}
let path = CStr::from_bytes_until_nul(&buffer).ok()?.to_bytes();
(!path.is_empty()).then(|| PathBuf::from(OsString::from_vec(path.to_vec())))
}
fn valid(code: &Owned) -> bool {
unsafe {
(code.api.sec_code_check_validity)(code.value, SEC_CS_DEFAULT_FLAGS, null())
== ERR_SEC_SUCCESS
}
}
pub(super) fn check_requirement(
token: Option<AuditToken>,
requirement: &str,
) -> CodeRequirementOutcome {
use CodeRequirementOutcome::{NotSatisfied, Satisfied, Unverifiable};
use CodeVerificationError::*;
let Some(token) = token else {
return Unverifiable(MissingAuditToken);
};
let Some(api) = api() else {
return Unverifiable(FrameworkUnavailable);
};
let Some(text) = make_string(api, requirement) else {
return Unverifiable(InvalidRequirement);
};
let mut requirement = null();
let mut errors = null();
let status = unsafe {
(api.sec_requirement_create_with_string)(text.value, 0, &mut errors, &mut requirement)
};
let _errors = Owned::new(api, errors);
let requirement = Owned::new(api, requirement);
let Some(requirement) = requirement.filter(|_| status == ERR_SEC_SUCCESS) else {
return Unverifiable(InvalidRequirement);
};
let Some(code) = guest_code(token) else {
return Unverifiable(GuestUnavailable);
};
let status = unsafe { (api.sec_code_check_validity)(code.value, 0, requirement.value) };
if status != ERR_SEC_SUCCESS {
return validity_failure(status);
}
let mut information = null();
let status =
unsafe { (api.sec_code_copy_signing_information)(code.value, 1 << 1, &mut information) };
let information = Owned::new(api, information);
let Some(information) = information.filter(|_| status == ERR_SEC_SUCCESS) else {
return Unverifiable(SigningInformationUnavailable);
};
let flags = dictionary_value(api, information.value, api.code_info_flags);
if flags.is_null() || unsafe { (api.cf_get_type_id)(flags) != (api.cf_number_get_type_id)() } {
return Unverifiable(SigningInformationUnavailable);
}
let mut flags_value: i64 = 0;
if unsafe { (api.cf_number_get_value)(flags, 4, (&mut flags_value as *mut i64).cast()) } == 0 {
return Unverifiable(SigningInformationUnavailable);
}
if flags_value & 0x1_0000 == 0 {
return NotSatisfied;
}
let plist = dictionary_value(api, information.value, api.code_info_plist);
let short_version = make_string(api, "CFBundleShortVersionString").and_then(|key| {
let version = dictionary_value(api, plist, key.value);
if version.is_null()
|| unsafe { (api.cf_get_type_id)(version) != (api.cf_string_get_type_id)() }
{
None
} else {
cf_string(api, version)
}
});
let status = unsafe { (api.sec_code_check_validity)(code.value, 0, requirement.value) };
if status != ERR_SEC_SUCCESS {
return validity_failure(status);
}
Satisfied(VerifiedCode { short_version })
}
fn validity_failure(status: OSStatus) -> CodeRequirementOutcome {
if status == -67050 {
CodeRequirementOutcome::NotSatisfied
} else {
CodeRequirementOutcome::Unverifiable(CodeVerificationError::ValidityCheckFailed(status))
}
}
fn make_string(api: &'static Api, text: &str) -> Option<Owned> {
Owned::new(api, unsafe {
(api.cf_string_create_with_bytes)(
null(),
text.as_ptr(),
text.len().try_into().ok()?,
CF_STRING_ENCODING_UTF8,
0,
)
})
}
fn dictionary_value(api: &Api, dictionary: CFTypeRef, key: CFTypeRef) -> CFTypeRef {
unsafe {
if dictionary.is_null()
|| (api.cf_get_type_id)(dictionary) != (api.cf_dictionary_get_type_id)()
{
null()
} else {
(api.cf_dictionary_get_value)(dictionary, key)
}
}
}
fn signing_identifier(code: &Owned) -> Option<String> {
let api = code.api;
let mut information = null();
let status = unsafe {
(api.sec_code_copy_signing_information)(code.value, SEC_CS_DEFAULT_FLAGS, &mut information)
};
let information = Owned::new(api, information);
let information = (status == ERR_SEC_SUCCESS)
.then_some(information)
.flatten()?;
let value =
unsafe { (api.cf_dictionary_get_value)(information.value, api.code_info_identifier) };
if value.is_null() || unsafe { (api.cf_get_type_id)(value) != (api.cf_string_get_type_id)() } {
return None;
}
cf_string(api, value)
}
fn cf_string(api: &Api, string: CFTypeRef) -> Option<String> {
let length = unsafe { (api.cf_string_get_length)(string) };
let capacity =
unsafe { (api.cf_string_get_maximum_size_for_encoding)(length, CF_STRING_ENCODING_UTF8) };
let capacity = usize::try_from(capacity).ok()?.checked_add(1)?;
let mut buffer = vec![0u8; capacity];
let copied = unsafe {
(api.cf_string_get_c_string)(
string,
buffer.as_mut_ptr().cast(),
buffer.len() as CFIndex,
CF_STRING_ENCODING_UTF8,
)
};
if copied == 0 {
return None;
}
let text = CStr::from_bytes_until_nul(&buffer).ok()?.to_str().ok()?;
(!text.is_empty()).then(|| text.to_owned())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn audit_token_layout_is_eight_words() {
assert_eq!(size_of::<AuditToken>(), 32);
}
#[test]
fn null_create_result_is_not_released() {
assert!(Owned::new(api().expect("macOS frameworks load"), null()).is_none());
}
}