Merge reviewed signed clock sources and Linux acceptance evidence
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,343 @@
|
||||
//! Fixed, temporary SCM capture probe. It never installs a service or opens an
|
||||
//! RMM store, key/pin, network endpoint, W32Time, job or clock setter.
|
||||
#[cfg(all(windows, feature = "system-helper"))]
|
||||
mod probe {
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use rmm_agent_windows::{helper_ipc as ipc, signed_clock};
|
||||
use rmm_domain::jobs::hash;
|
||||
use serde::Serialize;
|
||||
use std::{
|
||||
fs::OpenOptions,
|
||||
io::Write,
|
||||
mem::{size_of, zeroed},
|
||||
os::windows::{
|
||||
fs::OpenOptionsExt,
|
||||
io::{AsRawHandle, FromRawHandle, OwnedHandle},
|
||||
},
|
||||
path::{Path, PathBuf},
|
||||
ptr,
|
||||
sync::{
|
||||
OnceLock,
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
},
|
||||
};
|
||||
use windows_sys::Win32::{
|
||||
Foundation::*,
|
||||
Security::*,
|
||||
Storage::FileSystem::*,
|
||||
System::{Services::*, Threading::*},
|
||||
};
|
||||
const BASE: &str = r"C:\ProgramData\RustDiscoverySignedClockCapture";
|
||||
const LS: &str = "RmmSignedClockCaptureLocalService";
|
||||
const SYSTEM: &str = "RmmSignedClockCaptureSystem";
|
||||
static CASE: OnceLock<uuid::Uuid> = OnceLock::new();
|
||||
static ROLE: OnceLock<Role> = OnceLock::new();
|
||||
static STATUS: AtomicUsize = AtomicUsize::new(0);
|
||||
static STAGE: AtomicUsize = AtomicUsize::new(0);
|
||||
#[derive(Clone, Copy)]
|
||||
enum Role {
|
||||
LocalService,
|
||||
System,
|
||||
}
|
||||
impl Role {
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::LocalService => LS,
|
||||
Self::System => SYSTEM,
|
||||
}
|
||||
}
|
||||
fn tag(self) -> &'static str {
|
||||
match self {
|
||||
Self::LocalService => "local-service",
|
||||
Self::System => "system",
|
||||
}
|
||||
}
|
||||
fn user(self) -> &'static str {
|
||||
match self {
|
||||
Self::LocalService => "S-1-5-19",
|
||||
Self::System => "S-1-5-18",
|
||||
}
|
||||
}
|
||||
}
|
||||
fn wide(value: &str) -> Vec<u16> {
|
||||
value.encode_utf16().chain(Some(0)).collect()
|
||||
}
|
||||
fn owned(value: HANDLE) -> Result<OwnedHandle> {
|
||||
ensure!(
|
||||
!value.is_null() && value != INVALID_HANDLE_VALUE,
|
||||
"CAPTURE_NATIVE_HANDLE"
|
||||
);
|
||||
// SAFETY: only uniquely owned handles returned by successful APIs enter.
|
||||
Ok(unsafe { OwnedHandle::from_raw_handle(value) })
|
||||
}
|
||||
#[derive(Serialize)]
|
||||
struct TokenReport {
|
||||
user_sid: &'static str,
|
||||
service_sid_sha256: String,
|
||||
elevation_flag: u32,
|
||||
elevation_type: i32,
|
||||
}
|
||||
fn token(role: Role) -> Result<TokenReport> {
|
||||
let mut thread = ptr::null_mut();
|
||||
// SAFETY: valid output pointer, query only, no impersonation is created.
|
||||
if unsafe { OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, 1, &mut thread) } != 0 {
|
||||
let _thread = owned(thread)?;
|
||||
anyhow::bail!("CAPTURE_IMPERSONATION_FORBIDDEN");
|
||||
}
|
||||
ensure!(
|
||||
unsafe { GetLastError() } == ERROR_NO_TOKEN,
|
||||
"CAPTURE_THREAD_TOKEN"
|
||||
);
|
||||
let mut raw = ptr::null_mut();
|
||||
ensure!(
|
||||
unsafe { OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut raw) } != 0,
|
||||
"CAPTURE_PROCESS_TOKEN"
|
||||
);
|
||||
let handle = owned(raw)?;
|
||||
let sid = ipc::service_sid(role.name())?;
|
||||
ipc::verify_token(handle.as_raw_handle(), role.user(), &sid)?;
|
||||
let mut elevation: TOKEN_ELEVATION = unsafe { zeroed() };
|
||||
let mut count = 0;
|
||||
ensure!(
|
||||
unsafe {
|
||||
GetTokenInformation(
|
||||
handle.as_raw_handle(),
|
||||
TokenElevation,
|
||||
ptr::addr_of_mut!(elevation).cast(),
|
||||
size_of::<TOKEN_ELEVATION>() as u32,
|
||||
&mut count,
|
||||
)
|
||||
} != 0
|
||||
&& count as usize == size_of::<TOKEN_ELEVATION>(),
|
||||
"CAPTURE_ELEVATION"
|
||||
);
|
||||
let mut kind: TOKEN_ELEVATION_TYPE = 0;
|
||||
ensure!(
|
||||
unsafe {
|
||||
GetTokenInformation(
|
||||
handle.as_raw_handle(),
|
||||
TokenElevationType,
|
||||
ptr::addr_of_mut!(kind).cast(),
|
||||
size_of::<TOKEN_ELEVATION_TYPE>() as u32,
|
||||
&mut count,
|
||||
)
|
||||
} != 0
|
||||
&& count as usize == size_of::<TOKEN_ELEVATION_TYPE>()
|
||||
&& (1..=3).contains(&kind),
|
||||
"CAPTURE_ELEVATION_TYPE"
|
||||
);
|
||||
Ok(TokenReport {
|
||||
user_sid: role.user(),
|
||||
service_sid_sha256: hash(sid.as_bytes()),
|
||||
elevation_flag: elevation.TokenIsElevated,
|
||||
elevation_type: kind,
|
||||
})
|
||||
}
|
||||
fn create(path: &Path, bytes: &[u8]) -> Result<()> {
|
||||
ensure!(bytes.len() <= 16384, "CAPTURE_REPORT_LIMIT");
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.share_mode(FILE_SHARE_READ)
|
||||
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
|
||||
.open(path)?;
|
||||
let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { zeroed() };
|
||||
ensure!(
|
||||
unsafe { GetFileInformationByHandle(file.as_raw_handle(), &mut info) } != 0
|
||||
&& info.dwFileAttributes
|
||||
& (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)
|
||||
== 0
|
||||
&& info.nNumberOfLinks == 1,
|
||||
"CAPTURE_NEW_FILE_IDENTITY"
|
||||
);
|
||||
file.write_all(bytes)?;
|
||||
file.sync_all()?;
|
||||
Ok(())
|
||||
}
|
||||
fn execute() -> Result<()> {
|
||||
let role = *ROLE.get().context("CAPTURE_ROLE")?;
|
||||
let case = *CASE.get().context("CAPTURE_CASE")?;
|
||||
STAGE.store(10, Ordering::SeqCst);
|
||||
let token = token(role)?;
|
||||
let role_sid = ipc::service_sid(role.name())?;
|
||||
// Both fixed fixture SIDs may read the common case directory. Only the
|
||||
// selected fixture SID may write its own role subdirectory.
|
||||
let readers = vec![ipc::service_sid(LS)?, ipc::service_sid(SYSTEM)?];
|
||||
let root = PathBuf::from(BASE);
|
||||
let case_root = root.join(case.to_string());
|
||||
let output = case_root.join(role.tag());
|
||||
let image_path = case_root.join("SignedClockCapture.exe");
|
||||
ensure!(
|
||||
std::env::current_exe()? == image_path,
|
||||
"CAPTURE_FIXED_IMAGE_PATH"
|
||||
);
|
||||
let mut held = vec![
|
||||
ipc::PinnedFile::open(&root, true, &readers, &[], false)?,
|
||||
ipc::PinnedFile::open(&case_root, true, &readers, &[], false)?,
|
||||
ipc::PinnedFile::open(&output, true, &readers, &[role_sid], false)?,
|
||||
];
|
||||
let mut image = ipc::PinnedFile::open(&image_path, false, &readers, &[], false)?;
|
||||
let image_sha256 = image.sha256()?;
|
||||
let image_file = image.identity;
|
||||
held.push(image);
|
||||
for name in ["intent.json", "report.json", "end.json"] {
|
||||
ensure!(!output.join(name).exists(), "CAPTURE_CASE_ALREADY_ENTERED");
|
||||
}
|
||||
let pid = unsafe { GetCurrentProcessId() };
|
||||
let (mut born, mut ended, mut kernel, mut user) = (
|
||||
unsafe { zeroed() },
|
||||
unsafe { zeroed() },
|
||||
unsafe { zeroed() },
|
||||
unsafe { zeroed() },
|
||||
);
|
||||
ensure!(
|
||||
unsafe {
|
||||
GetProcessTimes(
|
||||
GetCurrentProcess(),
|
||||
&mut born,
|
||||
&mut ended,
|
||||
&mut kernel,
|
||||
&mut user,
|
||||
)
|
||||
} != 0,
|
||||
"CAPTURE_PROCESS_BIRTH"
|
||||
);
|
||||
let born = (u64::from(born.dwHighDateTime) << 32) | u64::from(born.dwLowDateTime);
|
||||
STAGE.store(20, Ordering::SeqCst);
|
||||
// Fixed existing read-only CIM boot getter, BEFORE the 250ms sampling
|
||||
// budget; never a caller script or a substitute process-start identity.
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?;
|
||||
let boot = uuid::Uuid::parse_str(&runtime.block_on(rmm_agent_windows::native::boot_id())?)?;
|
||||
ensure!(
|
||||
!boot.is_nil() && born > 0 && pid > 0,
|
||||
"CAPTURE_NATIVE_IDENTITY"
|
||||
);
|
||||
create(
|
||||
&output.join("intent.json"),
|
||||
&serde_json::to_vec(
|
||||
&serde_json::json!({"schema_version":1,"case_id":case,"role":role.tag(),"pid":pid,"process_birth_filetime":born,"image_sha256":image_sha256,"boot_id":boot,"calibrated":false,"clock_authority":false,"no_repeat":true,"maximum_samples":128,"maximum_capture_ns":250000000}),
|
||||
)?,
|
||||
)?;
|
||||
STAGE.store(30, Ordering::SeqCst);
|
||||
let result = (|| {
|
||||
let captures = signed_clock::diagnostic_capture_series()?;
|
||||
STAGE.store(40, Ordering::SeqCst);
|
||||
let current_token = token_role_recheck(role, &token)?;
|
||||
for file in &mut held {
|
||||
file.check()?;
|
||||
}
|
||||
let report = serde_json::to_vec(
|
||||
&serde_json::json!({"schema_version":1,"case_id":case,"role":role.tag(),"calibrated":false,"clock_authority":false,"read_only_clock":true,"token":current_token,"pid":pid,"process_birth_filetime":born,"image_sha256":image_sha256,"image_file":image_file,"boot_id":boot,"capture":captures}),
|
||||
)?;
|
||||
ensure!(report.len() <= 16384, "CAPTURE_REPORT_LIMIT");
|
||||
create(&output.join("report.json"), &report)?;
|
||||
Ok::<_, anyhow::Error>(hash(&report))
|
||||
})();
|
||||
let end = serde_json::to_vec(
|
||||
&serde_json::json!({"schema_version":1,"case_id":case,"role":role.tag(),"success":result.is_ok(),"stage":STAGE.load(Ordering::SeqCst),"report_sha256":result.as_ref().ok(),"calibrated":false,"clock_authority":false,"no_repeat":true}),
|
||||
)?;
|
||||
create(&output.join("end.json"), &end)?;
|
||||
result?;
|
||||
STAGE.store(50, Ordering::SeqCst);
|
||||
Ok(())
|
||||
}
|
||||
fn token_role_recheck(role: Role, original: &TokenReport) -> Result<TokenReport> {
|
||||
let current = token(role)?;
|
||||
ensure!(
|
||||
serde_json::to_vec(¤t)? == serde_json::to_vec(original)?,
|
||||
"CAPTURE_TOKEN_CHANGED"
|
||||
);
|
||||
Ok(current)
|
||||
}
|
||||
fn status(state: u32, error: u32) {
|
||||
let handle = STATUS.load(Ordering::SeqCst) as SERVICE_STATUS_HANDLE;
|
||||
if handle.is_null() {
|
||||
return;
|
||||
}
|
||||
let value = SERVICE_STATUS {
|
||||
dwServiceType: SERVICE_WIN32_OWN_PROCESS,
|
||||
dwCurrentState: state,
|
||||
dwControlsAccepted: 0,
|
||||
dwWin32ExitCode: error,
|
||||
dwServiceSpecificExitCode: if error == 1066 {
|
||||
STAGE.load(Ordering::SeqCst) as u32
|
||||
} else {
|
||||
0
|
||||
},
|
||||
dwCheckPoint: 0,
|
||||
dwWaitHint: 10000,
|
||||
};
|
||||
// SAFETY: this handle is returned by SCM for this own temporary service.
|
||||
unsafe { SetServiceStatus(handle, &value) };
|
||||
}
|
||||
unsafe extern "system" fn control(
|
||||
_: u32,
|
||||
_: u32,
|
||||
_: *mut std::ffi::c_void,
|
||||
_: *mut std::ffi::c_void,
|
||||
) -> u32 {
|
||||
0
|
||||
}
|
||||
unsafe extern "system" fn service_main(_: u32, _: *mut *mut u16) {
|
||||
let Some(role) = ROLE.get().copied() else {
|
||||
return;
|
||||
};
|
||||
let name = wide(role.name());
|
||||
let handle =
|
||||
unsafe { RegisterServiceCtrlHandlerExW(name.as_ptr(), Some(control), ptr::null_mut()) };
|
||||
if handle.is_null() {
|
||||
return;
|
||||
}
|
||||
STATUS.store(handle as usize, Ordering::SeqCst);
|
||||
status(SERVICE_RUNNING, 0);
|
||||
let result = execute();
|
||||
status(SERVICE_STOPPED, if result.is_ok() { 0 } else { 1066 });
|
||||
}
|
||||
pub fn dispatch() -> Result<()> {
|
||||
let args: Vec<_> = std::env::args().collect();
|
||||
ensure!(
|
||||
args.len() == 4 && args[1] == "--service",
|
||||
"CAPTURE_FIXED_ARGUMENTS"
|
||||
);
|
||||
let case = uuid::Uuid::parse_str(&args[2])?;
|
||||
ensure!(
|
||||
case.get_version_num() == 4 && case.to_string() == args[2],
|
||||
"CAPTURE_CANONICAL_UUID"
|
||||
);
|
||||
let role = match args[3].as_str() {
|
||||
"local-service" => Role::LocalService,
|
||||
"system" => Role::System,
|
||||
_ => anyhow::bail!("CAPTURE_FIXED_ROLE"),
|
||||
};
|
||||
CASE.set(case)
|
||||
.map_err(|_| anyhow::anyhow!("CAPTURE_ALREADY_DISPATCHED"))?;
|
||||
ROLE.set(role)
|
||||
.map_err(|_| anyhow::anyhow!("CAPTURE_ALREADY_DISPATCHED"))?;
|
||||
let mut name = wide(role.name());
|
||||
let table = [
|
||||
SERVICE_TABLE_ENTRYW {
|
||||
lpServiceName: name.as_mut_ptr(),
|
||||
lpServiceProc: Some(service_main),
|
||||
},
|
||||
SERVICE_TABLE_ENTRYW {
|
||||
lpServiceName: ptr::null_mut(),
|
||||
lpServiceProc: None,
|
||||
},
|
||||
];
|
||||
ensure!(
|
||||
unsafe { StartServiceCtrlDispatcherW(table.as_ptr()) } != 0,
|
||||
"CAPTURE_SCM_ONLY"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
#[cfg(all(windows, feature = "system-helper"))]
|
||||
fn main() -> anyhow::Result<()> {
|
||||
probe::dispatch()
|
||||
}
|
||||
#[cfg(not(all(windows, feature = "system-helper")))]
|
||||
fn main() -> anyhow::Result<()> {
|
||||
anyhow::bail!("Windows system-helper build required; no diagnostic clock fallback")
|
||||
}
|
||||
@@ -148,18 +148,13 @@ fn bracket_sample(
|
||||
base_error_ns > 0 && base_error_ns <= MAX_BASE_ERROR_NS,
|
||||
"TIME_NATIVE_CALIBRATION_REQUIRED"
|
||||
);
|
||||
let width = interrupt_after
|
||||
.checked_sub(interrupt_before)
|
||||
.and_then(|value| value.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_COUNTER"))?;
|
||||
let unbiased_width = unbiased_after
|
||||
.checked_sub(unbiased_before)
|
||||
.and_then(|value| value.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_COUNTER"))?;
|
||||
ensure!(
|
||||
width.abs_diff(unbiased_width) <= SECOND_NS,
|
||||
"TIME_NATIVE_CAPTURE_SUSPEND"
|
||||
);
|
||||
let width = raw_widths(
|
||||
interrupt_before,
|
||||
interrupt_after,
|
||||
unbiased_before,
|
||||
unbiased_after,
|
||||
)?
|
||||
.0;
|
||||
let capture_error_ns = width
|
||||
.checked_add(base_error_ns)
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_OVERFLOW"))?;
|
||||
@@ -184,6 +179,215 @@ fn bracket_sample(
|
||||
})
|
||||
}
|
||||
|
||||
fn raw_widths(ib: u64, ia: u64, ub: u64, ua: u64) -> Result<(u64, u64)> {
|
||||
let width = ia
|
||||
.checked_sub(ib)
|
||||
.and_then(|value| value.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_COUNTER"))?;
|
||||
let unbiased_width = ua
|
||||
.checked_sub(ub)
|
||||
.and_then(|value| value.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_NATIVE_CAPTURE_COUNTER"))?;
|
||||
ensure!(
|
||||
width.abs_diff(unbiased_width) <= SECOND_NS,
|
||||
"TIME_NATIVE_CAPTURE_SUSPEND"
|
||||
);
|
||||
Ok((width, unbiased_width))
|
||||
}
|
||||
/// Private raw capture, not a Sample and never clock authority.
|
||||
#[derive(Clone, Copy)]
|
||||
struct RawCapture {
|
||||
wall: u64,
|
||||
ib: u64,
|
||||
ub: u64,
|
||||
ua: u64,
|
||||
ia: u64,
|
||||
}
|
||||
impl RawCapture {
|
||||
fn widths(self) -> Result<(u64, u64)> {
|
||||
ensure!(
|
||||
self.wall > 116_444_736_000_000_000 && self.ib > 0 && self.ub > 0,
|
||||
"TIME_NATIVE_RAW_RANGE"
|
||||
);
|
||||
raw_widths(self.ib, self.ia, self.ub, self.ua)
|
||||
}
|
||||
fn row(self) -> [u64; 5] {
|
||||
[self.wall, self.ib, self.ub, self.ua, self.ia]
|
||||
}
|
||||
}
|
||||
/// Fixed read-only getter sequence shared by installed and diagnostic paths.
|
||||
#[cfg(windows)]
|
||||
fn raw_capture() -> Result<RawCapture> {
|
||||
#[link(name = "kernel32")]
|
||||
unsafe extern "system" {
|
||||
fn GetSystemTimePreciseAsFileTime(value: *mut u64);
|
||||
fn QueryInterruptTimePrecise(value: *mut u64);
|
||||
fn QueryUnbiasedInterruptTimePrecise(value: *mut u64);
|
||||
}
|
||||
let mut value = RawCapture {
|
||||
wall: 0,
|
||||
ib: 0,
|
||||
ub: 0,
|
||||
ua: 0,
|
||||
ia: 0,
|
||||
};
|
||||
// SAFETY: documented VOID getters write valid aligned u64 outputs. These
|
||||
// Windows10+ imports have no optional fallback or caller-selected target.
|
||||
unsafe {
|
||||
QueryInterruptTimePrecise(&mut value.ib);
|
||||
QueryUnbiasedInterruptTimePrecise(&mut value.ub);
|
||||
GetSystemTimePreciseAsFileTime(&mut value.wall);
|
||||
QueryUnbiasedInterruptTimePrecise(&mut value.ua);
|
||||
QueryInterruptTimePrecise(&mut value.ia);
|
||||
}
|
||||
value.widths()?;
|
||||
Ok(value)
|
||||
}
|
||||
const RAW_SAMPLES: usize = 128;
|
||||
const RAW_BUDGET_NS: u64 = 250_000_000;
|
||||
/// Numeric observations only. No Deserialize, calibrated Sample conversion or
|
||||
/// readiness flag; a report cannot open SignedClock or bypass its pin loader.
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct CaptureDiagnostics {
|
||||
schema_version: u32,
|
||||
calibrated: bool,
|
||||
clock_authority: bool,
|
||||
columns_100ns: [&'static str; 5],
|
||||
samples_100ns: Vec<[u64; 5]>,
|
||||
sample_count: u32,
|
||||
elapsed_monotonic_ns: u64,
|
||||
elapsed_interrupt_ns: u64,
|
||||
max_bracket_ns: u64,
|
||||
max_internal_interrupt_unbiased_difference_ns: u64,
|
||||
max_wall_interrupt_difference_ns: u64,
|
||||
max_interrupt_unbiased_difference_ns: u64,
|
||||
}
|
||||
struct RawSeries {
|
||||
first: Option<RawCapture>,
|
||||
previous: Option<RawCapture>,
|
||||
rows: Vec<[u64; 5]>,
|
||||
max_bracket_ns: u64,
|
||||
max_internal_delta_ns: u64,
|
||||
max_wall_delta_ns: u64,
|
||||
max_unbiased_delta_ns: u64,
|
||||
}
|
||||
impl RawSeries {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
first: None,
|
||||
previous: None,
|
||||
rows: Vec::with_capacity(RAW_SAMPLES),
|
||||
max_bracket_ns: 0,
|
||||
max_internal_delta_ns: 0,
|
||||
max_wall_delta_ns: 0,
|
||||
max_unbiased_delta_ns: 0,
|
||||
}
|
||||
}
|
||||
fn push(&mut self, value: RawCapture) -> Result<()> {
|
||||
ensure!(self.rows.len() < RAW_SAMPLES, "TIME_RAW_SAMPLE_LIMIT");
|
||||
let (width, unbiased_width) = value.widths()?;
|
||||
ensure!(width <= RAW_BUDGET_NS, "TIME_RAW_CAPTURE_BUDGET");
|
||||
if let Some(previous) = self.previous {
|
||||
let wall = value
|
||||
.wall
|
||||
.checked_sub(previous.wall)
|
||||
.and_then(|v| v.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_WALL_BACKWARD"))?;
|
||||
let interrupt = value
|
||||
.ia
|
||||
.checked_sub(previous.ia)
|
||||
.and_then(|v| v.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_INTERRUPT_BACKWARD"))?;
|
||||
let unbiased = value
|
||||
.ua
|
||||
.checked_sub(previous.ua)
|
||||
.and_then(|v| v.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_UNBIASED_BACKWARD"))?;
|
||||
ensure!(
|
||||
value.ib >= previous.ia && value.ub >= previous.ua,
|
||||
"TIME_RAW_COUNTER_ORDER"
|
||||
);
|
||||
self.max_wall_delta_ns = self.max_wall_delta_ns.max(wall.abs_diff(interrupt));
|
||||
self.max_unbiased_delta_ns =
|
||||
self.max_unbiased_delta_ns.max(interrupt.abs_diff(unbiased));
|
||||
ensure!(
|
||||
wall.abs_diff(interrupt) <= SECOND_NS && interrupt.abs_diff(unbiased) <= SECOND_NS,
|
||||
"TIME_RAW_DISCONTINUITY"
|
||||
);
|
||||
}
|
||||
self.first.get_or_insert(value);
|
||||
self.previous = Some(value);
|
||||
self.max_bracket_ns = self.max_bracket_ns.max(width);
|
||||
self.max_internal_delta_ns = self
|
||||
.max_internal_delta_ns
|
||||
.max(width.abs_diff(unbiased_width));
|
||||
self.rows.push(value.row());
|
||||
Ok(())
|
||||
}
|
||||
fn finish(self, elapsed_monotonic_ns: u64) -> Result<CaptureDiagnostics> {
|
||||
ensure!(
|
||||
self.rows.len() == RAW_SAMPLES && elapsed_monotonic_ns <= RAW_BUDGET_NS,
|
||||
"TIME_RAW_INCOMPLETE_OR_EXPIRED"
|
||||
);
|
||||
let first = self
|
||||
.first
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_EMPTY"))?;
|
||||
let last = self
|
||||
.previous
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_EMPTY"))?;
|
||||
let elapsed_interrupt_ns = last
|
||||
.ia
|
||||
.checked_sub(first.ib)
|
||||
.and_then(|v| v.checked_mul(100))
|
||||
.ok_or_else(|| anyhow::anyhow!("TIME_RAW_COUNTER_RANGE"))?;
|
||||
ensure!(
|
||||
elapsed_interrupt_ns <= RAW_BUDGET_NS,
|
||||
"TIME_RAW_INTERRUPT_BUDGET"
|
||||
);
|
||||
Ok(CaptureDiagnostics {
|
||||
schema_version: 1,
|
||||
calibrated: false,
|
||||
clock_authority: false,
|
||||
columns_100ns: [
|
||||
"wall_filetime",
|
||||
"interrupt_before",
|
||||
"unbiased_before",
|
||||
"unbiased_after",
|
||||
"interrupt_after",
|
||||
],
|
||||
samples_100ns: self.rows,
|
||||
sample_count: RAW_SAMPLES as u32,
|
||||
elapsed_monotonic_ns,
|
||||
elapsed_interrupt_ns,
|
||||
max_bracket_ns: self.max_bracket_ns,
|
||||
max_internal_interrupt_unbiased_difference_ns: self.max_internal_delta_ns,
|
||||
max_wall_interrupt_difference_ns: self.max_wall_delta_ns,
|
||||
max_interrupt_unbiased_difference_ns: self.max_unbiased_delta_ns,
|
||||
})
|
||||
}
|
||||
}
|
||||
/// Exactly128 bounded captures, spaced at fixed1ms target offsets. A missed
|
||||
/// 250ms own-monotonic budget aborts; no sleep/time-setting API is used.
|
||||
#[cfg(windows)]
|
||||
pub fn diagnostic_capture_series() -> Result<CaptureDiagnostics> {
|
||||
let started = std::time::Instant::now();
|
||||
let budget = std::time::Duration::from_nanos(RAW_BUDGET_NS);
|
||||
let mut series = RawSeries::new();
|
||||
for index in 0..RAW_SAMPLES {
|
||||
let due = std::time::Duration::from_millis(index as u64);
|
||||
loop {
|
||||
let elapsed = started.elapsed();
|
||||
ensure!(elapsed <= budget, "TIME_RAW_MONOTONIC_BUDGET");
|
||||
if elapsed >= due {
|
||||
break;
|
||||
}
|
||||
std::hint::spin_loop();
|
||||
}
|
||||
series.push(raw_capture()?)?;
|
||||
}
|
||||
series.finish(started.elapsed().as_nanos().try_into()?)
|
||||
}
|
||||
|
||||
#[cfg(all(windows, feature = "system-helper"))]
|
||||
mod installed {
|
||||
use super::*;
|
||||
@@ -239,24 +443,10 @@ mod installed {
|
||||
PROCESS.get_or_init(|| Mutex::new(Continuity::default()))
|
||||
}
|
||||
fn capture(base_error_ns: u64) -> Result<Sample> {
|
||||
#[link(name = "kernel32")]
|
||||
unsafe extern "system" {
|
||||
fn GetSystemTimePreciseAsFileTime(value: *mut u64);
|
||||
fn QueryInterruptTimePrecise(value: *mut u64);
|
||||
fn QueryUnbiasedInterruptTimePrecise(value: *mut u64);
|
||||
}
|
||||
let (mut ib, mut ia, mut ub, mut ua, mut wall) = (0, 0, 0, 0, 0);
|
||||
// SAFETY: documented VOID kernel getters with valid aligned u64
|
||||
// outputs. Windows 10+ is required; no optional API fallback exists.
|
||||
unsafe {
|
||||
QueryInterruptTimePrecise(&mut ib);
|
||||
QueryUnbiasedInterruptTimePrecise(&mut ub);
|
||||
GetSystemTimePreciseAsFileTime(&mut wall);
|
||||
QueryUnbiasedInterruptTimePrecise(&mut ua);
|
||||
QueryInterruptTimePrecise(&mut ia);
|
||||
}
|
||||
bracket_sample(wall, ib, ia, ub, ua, base_error_ns)
|
||||
let raw = raw_capture()?;
|
||||
bracket_sample(raw.wall, raw.ib, raw.ia, raw.ub, raw.ua, base_error_ns)
|
||||
}
|
||||
|
||||
fn observed(boot: Uuid, base_error_ns: u64) -> Result<(Sample, u64)> {
|
||||
let mut state = process()
|
||||
.lock()
|
||||
@@ -560,6 +750,63 @@ mod tests {
|
||||
capture_error_ns: 1_000_000,
|
||||
}
|
||||
}
|
||||
fn raw(index: u64) -> RawCapture {
|
||||
RawCapture {
|
||||
wall: 134_444_736_000_000_000 + index * 10_000,
|
||||
ib: 10_000_000 + index * 10_000,
|
||||
ia: 10_000_010 + index * 10_000,
|
||||
ub: 9_000_000 + index * 10_000,
|
||||
ua: 9_000_008 + index * 10_000,
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn raw_capture_denies_backward_wall_counters_and_incomplete_or_expired_series() {
|
||||
let mut series = RawSeries::new();
|
||||
series.push(raw(0)).unwrap();
|
||||
assert!(
|
||||
series
|
||||
.push(RawCapture {
|
||||
wall: raw(0).wall - 1,
|
||||
..raw(1)
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
let mut series = RawSeries::new();
|
||||
series.push(raw(0)).unwrap();
|
||||
assert!(
|
||||
series
|
||||
.push(RawCapture {
|
||||
ia: raw(0).ia - 1,
|
||||
ib: raw(0).ib - 1,
|
||||
..raw(1)
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
assert!(RawSeries::new().finish(0).is_err());
|
||||
let mut series = RawSeries::new();
|
||||
for index in 0..RAW_SAMPLES as u64 {
|
||||
series.push(raw(index)).unwrap();
|
||||
}
|
||||
assert!(series.finish(RAW_BUDGET_NS + 1).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn complete_raw_report_is_bounded_non_authoritative_and_keeps_actual_brackets() {
|
||||
let mut series = RawSeries::new();
|
||||
for index in 0..RAW_SAMPLES as u64 {
|
||||
series.push(raw(index)).unwrap();
|
||||
}
|
||||
assert!(series.push(raw(RAW_SAMPLES as u64)).is_err());
|
||||
let report = series.finish(128_000_000).unwrap();
|
||||
assert_eq!(report.sample_count, 128);
|
||||
assert_eq!(report.max_bracket_ns, 1000);
|
||||
assert_eq!(report.elapsed_interrupt_ns, 127_001_000);
|
||||
assert_eq!(report.max_internal_interrupt_unbiased_difference_ns, 200);
|
||||
let value = serde_json::to_value(&report).unwrap();
|
||||
assert_eq!(value["calibrated"], false);
|
||||
assert_eq!(value["clock_authority"], false);
|
||||
assert!(value.get("capture_error_ns").is_none());
|
||||
assert!(serde_json::to_vec(&report).unwrap().len() < 16384);
|
||||
}
|
||||
#[test]
|
||||
fn job_key_cannot_be_reintroduced_as_a_time_key_with_different_hex_case() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -37,3 +37,5 @@ futures-util.workspace = true
|
||||
libc = "0.2"
|
||||
|
||||
rmm-updater-handoff = { path = "../updater-handoff" }
|
||||
|
||||
rmm-time-attestation = { path = "../time-attestation" }
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
//! Passive protocol clock material only: never a sync decision or job grant.
|
||||
//! Context comes from current mTLS identity + locked tenant installation row.
|
||||
use axum::{
|
||||
Extension, Json, Router,
|
||||
body::Bytes,
|
||||
extract::{DefaultBodyLimit, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
routing::post,
|
||||
};
|
||||
use rmm_time_attestation::{Request, SignedAttestation};
|
||||
use sqlx::Row;
|
||||
use std::{os::unix::fs::MetadataExt, sync::OnceLock, time::Duration};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
net::UnixStream,
|
||||
sync::Semaphore,
|
||||
};
|
||||
|
||||
const SOCKET: &str = "/run/rmm-time-source/broker.sock";
|
||||
const MAX_FRAME: usize = 8192;
|
||||
static QUERIES: OnceLock<Semaphore> = OnceLock::new();
|
||||
|
||||
pub(crate) fn gateway_routes(routes: Router<crate::State>) -> Router<crate::State> {
|
||||
routes.route(
|
||||
"/agent/v1/time-attestation",
|
||||
post(attest).layer(DefaultBodyLimit::max(MAX_FRAME)),
|
||||
)
|
||||
}
|
||||
fn protected_socket() -> anyhow::Result<(u64, u64)> {
|
||||
use anyhow::ensure;
|
||||
for path in ["/run", "/run/rmm-time-source"] {
|
||||
let m = std::fs::symlink_metadata(path)?;
|
||||
ensure!(
|
||||
m.is_dir() && !m.file_type().is_symlink() && m.uid() == 0 && m.mode() & 0o022 == 0,
|
||||
"TIME_BROKER_DIRECTORY"
|
||||
);
|
||||
}
|
||||
let m = std::fs::symlink_metadata(SOCKET)?;
|
||||
use std::os::unix::fs::FileTypeExt;
|
||||
ensure!(
|
||||
m.file_type().is_socket()
|
||||
&& m.uid() == 0
|
||||
&& m.gid() == 10001
|
||||
&& m.nlink() == 1
|
||||
&& m.mode() & 0o7777 == 0o660,
|
||||
"TIME_BROKER_SOCKET"
|
||||
);
|
||||
Ok((m.dev(), m.ino()))
|
||||
}
|
||||
async fn broker(request: &Request) -> anyhow::Result<SignedAttestation> {
|
||||
use anyhow::ensure;
|
||||
let owned = protected_socket()?;
|
||||
let mut connection = UnixStream::connect(SOCKET).await?;
|
||||
let peer = connection.peer_cred()?;
|
||||
// A host broker is in an ancestor PID namespace: Linux may report PID0
|
||||
// (or the wrapper may omit it) to this container. Authorization instead
|
||||
// requires the kernel-mapped root UID, protected root-owned socket inode,
|
||||
// and the verifier's independently pinned end-to-end time signature.
|
||||
// Userns remapping is not compensated: unmapped UID/socket ownership denies.
|
||||
ensure!(peer.uid() == 0, "TIME_BROKER_PEER");
|
||||
ensure!(protected_socket()? == owned, "TIME_BROKER_REPLACED");
|
||||
let raw = serde_json::to_vec(request)?;
|
||||
ensure!(
|
||||
!raw.is_empty() && raw.len() <= MAX_FRAME,
|
||||
"TIME_REQUEST_SIZE"
|
||||
);
|
||||
connection
|
||||
.write_all(&(u32::try_from(raw.len())?).to_be_bytes())
|
||||
.await?;
|
||||
connection.write_all(&raw).await?;
|
||||
connection.shutdown().await?;
|
||||
let length = connection.read_u32().await? as usize;
|
||||
ensure!(length > 0 && length <= MAX_FRAME, "TIME_RESPONSE_SIZE");
|
||||
let mut raw = vec![0; length];
|
||||
connection.read_exact(&mut raw).await?;
|
||||
let mut extra = [0; 1];
|
||||
ensure!(
|
||||
connection.read(&mut extra).await? == 0,
|
||||
"TIME_RESPONSE_TRAILING"
|
||||
);
|
||||
ensure!(protected_socket()? == owned, "TIME_BROKER_REPLACED");
|
||||
let response: SignedAttestation = serde_json::from_slice(&raw)?;
|
||||
ensure!(
|
||||
serde_json::to_vec(&response)? == raw,
|
||||
"TIME_RESPONSE_NONCANONICAL"
|
||||
);
|
||||
// The independent verifier checks the signature, protected key and whole
|
||||
// challenge. This root-peer transport is not an OS/helper attestation.
|
||||
let payload: rmm_time_attestation::Payload = serde_json::from_str(&response.payload)?;
|
||||
ensure!(
|
||||
payload.request_sha256 == request.sha256()?,
|
||||
"TIME_RESPONSE_REQUEST"
|
||||
);
|
||||
Ok(response)
|
||||
}
|
||||
async fn attest(
|
||||
State(s): State<crate::State>,
|
||||
Extension(peer): Extension<crate::Peer>,
|
||||
headers: HeaderMap,
|
||||
bytes: Bytes,
|
||||
) -> Result<Json<SignedAttestation>, StatusCode> {
|
||||
tokio::time::timeout(Duration::from_millis(500), issue(&s, peer, headers, bytes))
|
||||
.await
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?
|
||||
}
|
||||
async fn issue(
|
||||
s: &crate::State,
|
||||
peer: crate::Peer,
|
||||
headers: HeaderMap,
|
||||
bytes: Bytes,
|
||||
) -> Result<Json<SignedAttestation>, StatusCode> {
|
||||
let (tenant, agent) = crate::agent_http::identity(s, peer).await?;
|
||||
if headers.get("content-type").and_then(|v| v.to_str().ok()) != Some("application/json") {
|
||||
return Err(StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
let request: Request = serde_json::from_slice(&bytes).map_err(|_| StatusCode::BAD_REQUEST)?;
|
||||
request.validate().map_err(|_| StatusCode::BAD_REQUEST)?;
|
||||
let c = &request.context;
|
||||
if c.tenant_id != tenant || c.agent_id != agent {
|
||||
return Err(StatusCode::FORBIDDEN);
|
||||
}
|
||||
let _slot = QUERIES
|
||||
.get_or_init(|| Semaphore::new(4))
|
||||
.try_acquire()
|
||||
.map_err(|_| StatusCode::TOO_MANY_REQUESTS)?;
|
||||
let mut tx = rmm_persistence::tenant_tx(&s.pool, tenant)
|
||||
.await
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
|
||||
// No sync_ready precondition: the independent time check is needed before
|
||||
// the first signed reconciliation, including its strictly negative paths.
|
||||
let row = sqlx::query("SELECT asset_id,enrollment_generation,assignment_version,connection_generation,connection_boot_id,capabilities FROM agent_installations WHERE tenant_id=$1 AND agent_id=$2 AND revoked_at IS NULL FOR SHARE")
|
||||
.bind(tenant).bind(agent).fetch_optional(&mut *tx).await
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?.ok_or(StatusCode::FORBIDDEN)?;
|
||||
let same = row.get::<uuid::Uuid, _>("asset_id") == c.asset_id
|
||||
&& u64::try_from(row.get::<i64, _>("enrollment_generation")).ok()
|
||||
== Some(c.enrollment_generation)
|
||||
&& u64::try_from(row.get::<i64, _>("assignment_version")).ok()
|
||||
== Some(c.assignment_generation)
|
||||
&& u64::try_from(row.get::<i64, _>("connection_generation")).ok()
|
||||
== Some(c.connection_generation)
|
||||
&& row.get::<Option<uuid::Uuid>, _>("connection_boot_id") == Some(c.windows_boot_id)
|
||||
&& row
|
||||
.get::<serde_json::Value, _>("capabilities")
|
||||
.as_array()
|
||||
.is_some_and(|v| v.iter().any(|v| v.as_str() == Some("windows.base/1")));
|
||||
if !same {
|
||||
return Err(StatusCode::CONFLICT);
|
||||
}
|
||||
// Hold the shared installation lock through this bounded local query:
|
||||
// revoke/reassignment/connection changes cannot race the signing moment.
|
||||
let response = tokio::time::timeout(Duration::from_millis(300), broker(&request))
|
||||
.await
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
|
||||
tx.commit()
|
||||
.await
|
||||
.map_err(|_| StatusCode::SERVICE_UNAVAILABLE)?;
|
||||
Ok(Json(response))
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod agent_artifacts;
|
||||
mod agent_http;
|
||||
mod agent_service;
|
||||
mod agent_time;
|
||||
mod agent_update_health;
|
||||
mod agent_update_policy;
|
||||
mod agent_updates;
|
||||
@@ -163,6 +164,7 @@ async fn main() -> Result<()> {
|
||||
if role.gateway() {
|
||||
routes = agent_updates::gateway_routes(routes);
|
||||
routes = agent_update_health::gateway_routes(routes);
|
||||
routes = agent_time::gateway_routes(routes);
|
||||
routes = routes
|
||||
.route("/agent/v1/enroll", post(gateway::enroll))
|
||||
.route("/agent/v1/messages", post(agent_http::messages))
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "rmm-time-broker-linux"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
libc = "0.2"
|
||||
ed25519-dalek = "=2.2.0"
|
||||
hex.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
uuid.workspace = true
|
||||
rmm-time-source-linux = { path = "../time-source-linux" }
|
||||
rmm-time-attestation = { path = "../time-attestation" }
|
||||
@@ -0,0 +1,10 @@
|
||||
//! Fixed local, root-only signing broker. HTTP/mTLS/SQL context authorization is
|
||||
//! the trusted unprivileged server's responsibility, never inferred here.
|
||||
#[cfg(target_os = "linux")]
|
||||
mod native;
|
||||
#[cfg(target_os = "linux")]
|
||||
pub use native::run;
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
pub fn run() -> anyhow::Result<()> {
|
||||
anyhow::bail!("TIME_BROKER_LINUX_REQUIRED")
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fn main() {
|
||||
// This fixed executable has no configuration/path/command arguments.
|
||||
if std::env::args_os().len() != 1 {
|
||||
eprintln!("TIME_BROKER_NO_ARGUMENTS");
|
||||
std::process::exit(2);
|
||||
}
|
||||
if rmm_time_broker_linux::run().is_err() {
|
||||
// Never format configuration, request, key material or nested errors.
|
||||
eprintln!("TIME_BROKER_STOPPED");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,752 @@
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use ed25519_dalek::SigningKey;
|
||||
use rmm_time_attestation::{Payload, Request, SignedAttestation};
|
||||
use rmm_time_source_linux::{NativeTimeSource, QualifiedSample};
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
collections::BTreeSet,
|
||||
ffi::CString,
|
||||
fs::File,
|
||||
io::{Read, Write},
|
||||
mem,
|
||||
os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd},
|
||||
os::unix::{fs::MetadataExt, net::UnixStream},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
const ROOT: &str = "/run/rmm-time-source";
|
||||
const SOCKET: &str = "/run/rmm-time-source/broker.sock";
|
||||
const MAX_BYTES: usize = 8192;
|
||||
const DEADLINE_NS: u64 = 250_000_000;
|
||||
const CAPTURE_NS: u64 = 10_000_000;
|
||||
const MAX_ERROR_NS: u64 = 500_000_000;
|
||||
const MAX_AGE_NS: u64 = 3_600_000_000_000;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Configuration {
|
||||
schema_version: u32,
|
||||
server_instance: Uuid,
|
||||
key_id: String,
|
||||
key_generation: u64,
|
||||
signing_key_hex: String,
|
||||
forbidden_public_keys: Vec<String>,
|
||||
expected_server_uid: u32,
|
||||
server_gid: u32,
|
||||
}
|
||||
struct Signer {
|
||||
key: SigningKey,
|
||||
server_instance: Uuid,
|
||||
key_id: String,
|
||||
generation: u64,
|
||||
server_gid: u32,
|
||||
_root: OwnedFd,
|
||||
_file: OwnedFd,
|
||||
file_state: FileState,
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
struct FileState {
|
||||
identity: Identity,
|
||||
length: u64,
|
||||
mtime: i64,
|
||||
mtime_ns: i64,
|
||||
ctime: i64,
|
||||
ctime_ns: i64,
|
||||
}
|
||||
fn file_state(file: &OwnedFd) -> Result<FileState> {
|
||||
let metadata = File::from(file.try_clone()?).metadata()?;
|
||||
Ok(FileState {
|
||||
identity: identity(file.as_raw_fd())?,
|
||||
length: metadata.len(),
|
||||
mtime: metadata.mtime(),
|
||||
mtime_ns: metadata.mtime_nsec(),
|
||||
ctime: metadata.ctime(),
|
||||
ctime_ns: metadata.ctime_nsec(),
|
||||
})
|
||||
}
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
struct Identity {
|
||||
dev: u64,
|
||||
ino: u64,
|
||||
uid: u32,
|
||||
gid: u32,
|
||||
mode: u32,
|
||||
links: u64,
|
||||
}
|
||||
fn fd(value: i32) -> Result<OwnedFd> {
|
||||
if value < 0 {
|
||||
return Err(std::io::Error::last_os_error().into());
|
||||
}
|
||||
// SAFETY: each caller supplies a new descriptor.
|
||||
Ok(unsafe { OwnedFd::from_raw_fd(value) })
|
||||
}
|
||||
fn openat(dir: RawFd, name: &str, flags: i32) -> Result<OwnedFd> {
|
||||
let name = CString::new(name)?;
|
||||
fd(unsafe {
|
||||
libc::openat(
|
||||
dir,
|
||||
name.as_ptr(),
|
||||
flags | libc::O_CLOEXEC | libc::O_NOFOLLOW,
|
||||
)
|
||||
})
|
||||
}
|
||||
fn identity(descriptor: RawFd) -> Result<Identity> {
|
||||
let mut st = mem::MaybeUninit::<libc::stat>::uninit();
|
||||
ensure!(
|
||||
unsafe { libc::fstat(descriptor, st.as_mut_ptr()) } == 0,
|
||||
"BROKER_FSTAT"
|
||||
);
|
||||
let st = unsafe { st.assume_init() };
|
||||
Ok(Identity {
|
||||
dev: st.st_dev,
|
||||
ino: st.st_ino,
|
||||
uid: st.st_uid,
|
||||
gid: st.st_gid,
|
||||
mode: st.st_mode,
|
||||
links: st.st_nlink,
|
||||
})
|
||||
}
|
||||
fn directory(path: &str) -> Result<OwnedFd> {
|
||||
ensure!(
|
||||
path.starts_with('/') && !path.contains(".."),
|
||||
"BROKER_FIXED_DIRECTORY"
|
||||
);
|
||||
let mut current = openat(libc::AT_FDCWD, "/", libc::O_RDONLY | libc::O_DIRECTORY)?;
|
||||
let valid =
|
||||
|m: Identity| m.uid == 0 && m.mode & libc::S_IFMT == libc::S_IFDIR && m.mode & 0o022 == 0;
|
||||
ensure!(
|
||||
valid(identity(current.as_raw_fd())?),
|
||||
"BROKER_ROOT_PROTECTION"
|
||||
);
|
||||
for part in path.split('/').filter(|v| !v.is_empty()) {
|
||||
current = openat(
|
||||
current.as_raw_fd(),
|
||||
part,
|
||||
libc::O_RDONLY | libc::O_DIRECTORY,
|
||||
)?;
|
||||
ensure!(
|
||||
valid(identity(current.as_raw_fd())?),
|
||||
"BROKER_DIRECTORY_PROTECTION"
|
||||
);
|
||||
}
|
||||
Ok(current)
|
||||
}
|
||||
fn bound_directory(path: &str, held: &OwnedFd) -> Result<()> {
|
||||
ensure!(
|
||||
identity(directory(path)?.as_raw_fd())? == identity(held.as_raw_fd())?,
|
||||
"BROKER_DIRECTORY_CHANGED"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
fn exact_hex(value: &str) -> Result<[u8; 32]> {
|
||||
ensure!(
|
||||
value.len() == 64
|
||||
&& value
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)),
|
||||
"BROKER_KEY_FORMAT"
|
||||
);
|
||||
let bytes = hex::decode(value)?;
|
||||
bytes
|
||||
.try_into()
|
||||
.map_err(|_| anyhow::anyhow!("BROKER_KEY_LENGTH"))
|
||||
}
|
||||
fn key(configuration: &Configuration) -> Result<SigningKey> {
|
||||
ensure!(
|
||||
configuration.schema_version == 1
|
||||
&& !configuration.server_instance.is_nil()
|
||||
&& configuration.key_generation > 0
|
||||
&& configuration.expected_server_uid == 10001
|
||||
&& configuration.server_gid == 10001
|
||||
&& !configuration.key_id.is_empty()
|
||||
&& configuration.key_id.len() <= 64
|
||||
&& configuration
|
||||
.key_id
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
|
||||
&& (1..=32).contains(&configuration.forbidden_public_keys.len()),
|
||||
"BROKER_CONFIGURATION"
|
||||
);
|
||||
let signer = SigningKey::from_bytes(&exact_hex(&configuration.signing_key_hex)?);
|
||||
let own = signer.verifying_key().to_bytes();
|
||||
let mut seen = BTreeSet::new();
|
||||
for forbidden in &configuration.forbidden_public_keys {
|
||||
let public = exact_hex(forbidden)?;
|
||||
ensure!(
|
||||
seen.insert(public) && public != own,
|
||||
"BROKER_KEY_ROLE_REUSE_OR_DUPLICATE"
|
||||
);
|
||||
}
|
||||
Ok(signer)
|
||||
}
|
||||
impl Signer {
|
||||
fn open() -> Result<Self> {
|
||||
let root = directory("/etc/rmm-time-source")?;
|
||||
let file = openat(root.as_raw_fd(), "signer.json", libc::O_RDONLY)?;
|
||||
let info = identity(file.as_raw_fd())?;
|
||||
ensure!(
|
||||
info.uid == 0
|
||||
&& info.mode & libc::S_IFMT == libc::S_IFREG
|
||||
&& info.mode & 0o7777 == 0o600
|
||||
&& info.links == 1,
|
||||
"BROKER_SIGNER_PROTECTION"
|
||||
);
|
||||
let initial_state = file_state(&file)?;
|
||||
let before = File::from(file.try_clone()?).metadata()?;
|
||||
ensure!(before.len() <= MAX_BYTES as u64, "BROKER_SIGNER_SIZE");
|
||||
let mut raw = Vec::new();
|
||||
File::from(file.try_clone()?)
|
||||
.take(MAX_BYTES as u64 + 1)
|
||||
.read_to_end(&mut raw)?;
|
||||
ensure!(raw.len() <= MAX_BYTES, "BROKER_SIGNER_SIZE");
|
||||
let after = File::from(file.try_clone()?).metadata()?;
|
||||
ensure!(
|
||||
before.len() == after.len()
|
||||
&& before.mtime() == after.mtime()
|
||||
&& before.mtime_nsec() == after.mtime_nsec()
|
||||
&& before.ctime() == after.ctime()
|
||||
&& before.ctime_nsec() == after.ctime_nsec(),
|
||||
"BROKER_SIGNER_READ_CHANGED"
|
||||
);
|
||||
ensure!(
|
||||
raw.len() as u64 == before.len(),
|
||||
"BROKER_SIGNER_READ_LENGTH"
|
||||
);
|
||||
ensure!(
|
||||
file_state(&file)? == initial_state,
|
||||
"BROKER_SIGNER_READ_IDENTITY_CHANGED"
|
||||
);
|
||||
let state = initial_state;
|
||||
let configuration: Configuration = serde_json::from_slice(&raw)?;
|
||||
let key = key(&configuration)?;
|
||||
let result = Self {
|
||||
key,
|
||||
server_instance: configuration.server_instance,
|
||||
key_id: configuration.key_id,
|
||||
generation: configuration.key_generation,
|
||||
server_gid: configuration.server_gid,
|
||||
_root: root,
|
||||
_file: file,
|
||||
file_state: state,
|
||||
};
|
||||
result.recheck()?;
|
||||
Ok(result)
|
||||
}
|
||||
fn recheck(&self) -> Result<()> {
|
||||
bound_directory("/etc/rmm-time-source", &self._root)?;
|
||||
let current = openat(self._root.as_raw_fd(), "signer.json", libc::O_RDONLY)?;
|
||||
let held = identity(self._file.as_raw_fd())?;
|
||||
ensure!(
|
||||
file_state(&self._file)? == self.file_state && file_state(¤t)? == self.file_state,
|
||||
"BROKER_SIGNER_CONTENT_IDENTITY_CHANGED"
|
||||
);
|
||||
ensure!(
|
||||
held == identity(current.as_raw_fd())?
|
||||
&& held.uid == 0
|
||||
&& held.mode & 0o7777 == 0o600
|
||||
&& held.links == 1,
|
||||
"BROKER_SIGNER_CHANGED"
|
||||
);
|
||||
// Contents are immutable for this process. Root rotation requires stop,
|
||||
// independently provisioned new configuration and a fresh instance.
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Clock {
|
||||
mono: u64,
|
||||
end: u64,
|
||||
boot: u64,
|
||||
utc: i64,
|
||||
}
|
||||
fn clock_ns(which: libc::clockid_t) -> Result<i128> {
|
||||
let mut value = mem::MaybeUninit::<libc::timespec>::uninit();
|
||||
ensure!(
|
||||
unsafe { libc::clock_gettime(which, value.as_mut_ptr()) } == 0,
|
||||
"BROKER_CLOCK"
|
||||
);
|
||||
let value = unsafe { value.assume_init() };
|
||||
ensure!(
|
||||
value.tv_sec >= 0 && (0..1_000_000_000).contains(&value.tv_nsec),
|
||||
"BROKER_CLOCK_RANGE"
|
||||
);
|
||||
Ok(i128::from(value.tv_sec) * 1_000_000_000 + i128::from(value.tv_nsec))
|
||||
}
|
||||
fn capture() -> Result<Clock> {
|
||||
let mono = u64::try_from(clock_ns(libc::CLOCK_MONOTONIC)?)?;
|
||||
let boot = u64::try_from(clock_ns(libc::CLOCK_BOOTTIME)?)?;
|
||||
let utc = i64::try_from(clock_ns(libc::CLOCK_REALTIME)?)?;
|
||||
let end = u64::try_from(clock_ns(libc::CLOCK_MONOTONIC)?)?;
|
||||
ensure!(
|
||||
end >= mono && end - mono <= CAPTURE_NS && utc > 0,
|
||||
"BROKER_CAPTURE_WINDOW"
|
||||
);
|
||||
Ok(Clock {
|
||||
mono,
|
||||
end,
|
||||
boot,
|
||||
utc,
|
||||
})
|
||||
}
|
||||
fn boot_id() -> Result<Uuid> {
|
||||
let raw = std::fs::read_to_string("/proc/sys/kernel/random/boot_id")?;
|
||||
let id = Uuid::parse_str(raw.trim())?;
|
||||
ensure!(!id.is_nil(), "BROKER_BOOT_ID");
|
||||
Ok(id)
|
||||
}
|
||||
fn continuity(sample: &QualifiedSample, now: Clock) -> Result<()> {
|
||||
let mono = now
|
||||
.mono
|
||||
.checked_sub(sample.captured_monotonic_ns())
|
||||
.context("BROKER_MONOTONIC_BACKWARD")?;
|
||||
let boot = now
|
||||
.boot
|
||||
.checked_sub(sample.sampled_boottime_ns())
|
||||
.context("BROKER_BOOTTIME_BACKWARD")?;
|
||||
let wall = now
|
||||
.utc
|
||||
.checked_sub(sample.captured_utc_ns())
|
||||
.context("BROKER_UTC_OVERFLOW")?;
|
||||
let tolerance = CAPTURE_NS
|
||||
.checked_add(now.end - now.mono)
|
||||
.context("BROKER_WINDOW_OVERFLOW")?;
|
||||
ensure!(
|
||||
wall >= 0
|
||||
&& mono.abs_diff(boot) <= tolerance
|
||||
&& mono.abs_diff(wall as u64) <= tolerance
|
||||
&& boot_id()? == sample.server_boot_id(),
|
||||
"BROKER_CLOCK_DISCONTINUITY"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
#[derive(Clone, Copy)]
|
||||
struct Deadline {
|
||||
mono: u64,
|
||||
boot: u64,
|
||||
}
|
||||
fn deadline(start: Clock) -> Result<Deadline> {
|
||||
Ok(Deadline {
|
||||
mono: start
|
||||
.mono
|
||||
.checked_add(DEADLINE_NS)
|
||||
.context("BROKER_DEADLINE_OVERFLOW")?,
|
||||
boot: start
|
||||
.boot
|
||||
.checked_add(DEADLINE_NS)
|
||||
.context("BROKER_DEADLINE_OVERFLOW")?,
|
||||
})
|
||||
}
|
||||
fn budget_at(end: Deadline, mono: u64, boot: u64) -> Result<u64> {
|
||||
Ok(remaining(end.mono, mono)?.min(remaining(end.boot, boot)?))
|
||||
}
|
||||
fn budget(end: Deadline) -> Result<u64> {
|
||||
budget_at(
|
||||
end,
|
||||
u64::try_from(clock_ns(libc::CLOCK_MONOTONIC)?)?,
|
||||
u64::try_from(clock_ns(libc::CLOCK_BOOTTIME)?)?,
|
||||
)
|
||||
}
|
||||
fn remaining(end: u64, now: u64) -> Result<u64> {
|
||||
let left = end.checked_sub(now).context("BROKER_DEADLINE")?;
|
||||
ensure!(left > 0, "BROKER_DEADLINE");
|
||||
Ok(left)
|
||||
}
|
||||
fn padded_quality(error: u64, age: u64, capture_mono: u64, end: u64) -> Result<(u64, u64)> {
|
||||
let padding = end
|
||||
.checked_sub(capture_mono)
|
||||
.and_then(|v| v.checked_add(2 * CAPTURE_NS))
|
||||
.context("BROKER_QUALITY_OVERFLOW")?;
|
||||
let error = error
|
||||
.checked_add(padding)
|
||||
.context("BROKER_ERROR_OVERFLOW")?;
|
||||
let age = age.checked_add(padding).context("BROKER_AGE_OVERFLOW")?;
|
||||
ensure!(
|
||||
error <= MAX_ERROR_NS && age <= MAX_AGE_NS,
|
||||
"BROKER_QUALITY_LIMIT"
|
||||
);
|
||||
Ok((error, age))
|
||||
}
|
||||
fn wait(fd: RawFd, event: i16, end: Deadline) -> Result<()> {
|
||||
let left = budget(end)?;
|
||||
let timeout = libc::timespec {
|
||||
tv_sec: (left / 1_000_000_000) as _,
|
||||
tv_nsec: (left % 1_000_000_000) as _,
|
||||
};
|
||||
let mut p = libc::pollfd {
|
||||
fd,
|
||||
events: event,
|
||||
revents: 0,
|
||||
};
|
||||
ensure!(
|
||||
unsafe { libc::ppoll(&mut p, 1, &timeout, std::ptr::null()) } == 1
|
||||
&& p.revents & event != 0
|
||||
&& p.revents & (libc::POLLERR | libc::POLLNVAL) == 0,
|
||||
"BROKER_IO_TIMEOUT"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
fn exact_read(stream: &mut UnixStream, mut raw: &mut [u8], end: Deadline) -> Result<()> {
|
||||
while !raw.is_empty() {
|
||||
wait(stream.as_raw_fd(), libc::POLLIN, end)?;
|
||||
let got = stream.read(raw)?;
|
||||
ensure!(got > 0, "BROKER_FRAME_EOF");
|
||||
raw = &mut raw[got..];
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn parse_request(raw: &[u8]) -> Result<Request> {
|
||||
ensure!(
|
||||
!raw.is_empty() && raw.len() <= MAX_BYTES,
|
||||
"BROKER_FRAME_SIZE"
|
||||
);
|
||||
let request: Request = serde_json::from_slice(raw)?;
|
||||
request.validate()?;
|
||||
ensure!(
|
||||
serde_json::to_vec(&request)? == raw,
|
||||
"BROKER_NONCANONICAL_REQUEST"
|
||||
);
|
||||
Ok(request)
|
||||
}
|
||||
fn frame_length(header: [u8; 4]) -> Result<usize> {
|
||||
let len = u32::from_be_bytes(header) as usize;
|
||||
ensure!((1..=MAX_BYTES).contains(&len), "BROKER_FRAME_SIZE");
|
||||
Ok(len)
|
||||
}
|
||||
fn read_request(stream: &mut UnixStream, end: Deadline) -> Result<Request> {
|
||||
let mut header = [0; 4];
|
||||
exact_read(stream, &mut header, end)?;
|
||||
let length = frame_length(header)?;
|
||||
let mut raw = vec![0; length];
|
||||
exact_read(stream, &mut raw, end)?;
|
||||
// A pipelined/extra byte already supplied with this request is malformed.
|
||||
// The protocol grants exactly one request; the stream is always closed.
|
||||
let mut byte = [0u8; 1];
|
||||
let peek = unsafe {
|
||||
libc::recv(
|
||||
stream.as_raw_fd(),
|
||||
byte.as_mut_ptr().cast(),
|
||||
1,
|
||||
libc::MSG_PEEK | libc::MSG_DONTWAIT,
|
||||
)
|
||||
};
|
||||
ensure!(peek <= 0, "BROKER_EXTRA_FRAME");
|
||||
if peek < 0 {
|
||||
ensure!(
|
||||
std::io::Error::last_os_error().kind() == std::io::ErrorKind::WouldBlock,
|
||||
"BROKER_FRAME_PEEK"
|
||||
);
|
||||
}
|
||||
parse_request(&raw)
|
||||
}
|
||||
fn write_reply(stream: &mut UnixStream, signed: &SignedAttestation, end: Deadline) -> Result<()> {
|
||||
let raw = serde_json::to_vec(signed)?;
|
||||
ensure!(raw.len() <= MAX_BYTES, "BROKER_REPLY_BOUND");
|
||||
let mut frame = Vec::with_capacity(raw.len() + 4);
|
||||
frame.extend_from_slice(&(raw.len() as u32).to_be_bytes());
|
||||
frame.extend_from_slice(&raw);
|
||||
let mut rest = frame.as_slice();
|
||||
while !rest.is_empty() {
|
||||
wait(stream.as_raw_fd(), libc::POLLOUT, end)?;
|
||||
let got = stream.write(rest)?;
|
||||
ensure!(got > 0, "BROKER_REPLY_EOF");
|
||||
rest = &rest[got..];
|
||||
}
|
||||
budget(end)?;
|
||||
Ok(())
|
||||
}
|
||||
fn peer(stream: &UnixStream, gid: u32) -> Result<()> {
|
||||
let mut credential = mem::MaybeUninit::<libc::ucred>::uninit();
|
||||
let mut length = mem::size_of::<libc::ucred>() as libc::socklen_t;
|
||||
ensure!(
|
||||
unsafe {
|
||||
libc::getsockopt(
|
||||
stream.as_raw_fd(),
|
||||
libc::SOL_SOCKET,
|
||||
libc::SO_PEERCRED,
|
||||
credential.as_mut_ptr().cast(),
|
||||
&mut length,
|
||||
)
|
||||
} == 0
|
||||
&& length as usize == mem::size_of::<libc::ucred>(),
|
||||
"BROKER_PEERCRED"
|
||||
);
|
||||
let credential = unsafe { credential.assume_init() };
|
||||
ensure!(
|
||||
credential.pid > 1 && credential.uid == 10001 && credential.gid == gid,
|
||||
"BROKER_SERVER_IDENTITY"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct Listener {
|
||||
fd: OwnedFd,
|
||||
root: OwnedFd,
|
||||
own: Option<Identity>,
|
||||
}
|
||||
impl Listener {
|
||||
fn current(&self) -> Result<Identity> {
|
||||
let name = CString::new("broker.sock")?;
|
||||
let mut st = mem::MaybeUninit::<libc::stat>::uninit();
|
||||
ensure!(
|
||||
unsafe {
|
||||
libc::fstatat(
|
||||
self.root.as_raw_fd(),
|
||||
name.as_ptr(),
|
||||
st.as_mut_ptr(),
|
||||
libc::AT_SYMLINK_NOFOLLOW,
|
||||
)
|
||||
} == 0,
|
||||
"BROKER_SOCKET_STAT"
|
||||
);
|
||||
let st = unsafe { st.assume_init() };
|
||||
Ok(Identity {
|
||||
dev: st.st_dev,
|
||||
ino: st.st_ino,
|
||||
uid: st.st_uid,
|
||||
gid: st.st_gid,
|
||||
mode: st.st_mode,
|
||||
links: st.st_nlink,
|
||||
})
|
||||
}
|
||||
fn open(gid: u32) -> Result<Self> {
|
||||
let root = directory(ROOT)?;
|
||||
let fd =
|
||||
fd(unsafe { libc::socket(libc::AF_UNIX, libc::SOCK_STREAM | libc::SOCK_CLOEXEC, 0) })?;
|
||||
let mut result = Self {
|
||||
fd,
|
||||
root,
|
||||
own: None,
|
||||
};
|
||||
let mut addr: libc::sockaddr_un = unsafe { mem::zeroed() };
|
||||
addr.sun_family = libc::AF_UNIX as _;
|
||||
for (to, from) in addr.sun_path.iter_mut().zip(SOCKET.bytes()) {
|
||||
*to = from as _;
|
||||
}
|
||||
let length =
|
||||
(mem::offset_of!(libc::sockaddr_un, sun_path) + SOCKET.len() + 1) as libc::socklen_t;
|
||||
bound_directory(ROOT, &result.root)?;
|
||||
ensure!(
|
||||
unsafe {
|
||||
libc::bind(
|
||||
result.fd.as_raw_fd(),
|
||||
(&addr as *const libc::sockaddr_un).cast(),
|
||||
length,
|
||||
)
|
||||
} == 0,
|
||||
"BROKER_BIND_ONCE"
|
||||
);
|
||||
let own = result.current()?;
|
||||
ensure!(
|
||||
own.uid == 0 && own.mode & libc::S_IFMT == libc::S_IFSOCK && own.links == 1,
|
||||
"BROKER_SOCKET_IDENTITY"
|
||||
);
|
||||
result.own = Some(own);
|
||||
let name = CString::new("broker.sock")?;
|
||||
ensure!(
|
||||
unsafe {
|
||||
libc::fchownat(
|
||||
result.root.as_raw_fd(),
|
||||
name.as_ptr(),
|
||||
0,
|
||||
gid,
|
||||
libc::AT_SYMLINK_NOFOLLOW,
|
||||
)
|
||||
} == 0,
|
||||
"BROKER_SOCKET_GROUP"
|
||||
);
|
||||
ensure!(
|
||||
unsafe { libc::fchmodat(result.root.as_raw_fd(), name.as_ptr(), 0o660, 0) } == 0,
|
||||
"BROKER_SOCKET_MODE"
|
||||
);
|
||||
let updated = result.current()?;
|
||||
ensure!(
|
||||
updated.dev == own.dev
|
||||
&& updated.ino == own.ino
|
||||
&& updated.uid == 0
|
||||
&& updated.gid == gid
|
||||
&& updated.mode & 0o7777 == 0o660
|
||||
&& updated.links == 1,
|
||||
"BROKER_SOCKET_READY"
|
||||
);
|
||||
result.own = Some(updated);
|
||||
result.recheck()?;
|
||||
ensure!(
|
||||
unsafe { libc::listen(result.fd.as_raw_fd(), 4) } == 0,
|
||||
"BROKER_LISTEN"
|
||||
);
|
||||
Ok(result)
|
||||
}
|
||||
fn recheck(&self) -> Result<()> {
|
||||
bound_directory(ROOT, &self.root)?;
|
||||
ensure!(Some(self.current()?) == self.own, "BROKER_SOCKET_CHANGED");
|
||||
Ok(())
|
||||
}
|
||||
fn accept(&self) -> Result<UnixStream> {
|
||||
self.recheck()?;
|
||||
let accepted = fd(unsafe {
|
||||
libc::accept4(
|
||||
self.fd.as_raw_fd(),
|
||||
std::ptr::null_mut(),
|
||||
std::ptr::null_mut(),
|
||||
libc::SOCK_CLOEXEC | libc::SOCK_NONBLOCK,
|
||||
)
|
||||
})?;
|
||||
Ok(UnixStream::from(accepted))
|
||||
}
|
||||
}
|
||||
impl Drop for Listener {
|
||||
fn drop(&mut self) {
|
||||
// No adoption, restart cleanup or retry: only this exact own inode.
|
||||
if let Some(own) = self.own.take()
|
||||
&& bound_directory(ROOT, &self.root).is_ok()
|
||||
&& self.current().ok() == Some(own)
|
||||
&& let Ok(name) = CString::new("broker.sock")
|
||||
{
|
||||
unsafe { libc::unlinkat(self.root.as_raw_fd(), name.as_ptr(), 0) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
ensure!(unsafe { libc::geteuid() } == 0, "BROKER_ROOT_REQUIRED");
|
||||
let signer = Signer::open()?;
|
||||
let mut source = NativeTimeSource::open()?;
|
||||
let listener = Listener::open(signer.server_gid)?;
|
||||
loop {
|
||||
let mut stream = listener.accept()?;
|
||||
let start = capture()?;
|
||||
let end = deadline(start)?;
|
||||
let _ = handle(&signer, &mut source, &mut stream, end);
|
||||
// Drop closes every connection after exactly one attempt, including
|
||||
// malformed/unauthenticated input. Errors never disclose internals.
|
||||
}
|
||||
}
|
||||
fn handle(
|
||||
signer: &Signer,
|
||||
source: &mut NativeTimeSource,
|
||||
stream: &mut UnixStream,
|
||||
end: Deadline,
|
||||
) -> Result<()> {
|
||||
peer(stream, signer.server_gid)?;
|
||||
let request = read_request(stream, end)?;
|
||||
signer.recheck()?;
|
||||
budget(end)?;
|
||||
let sample = source.sample()?;
|
||||
let before = capture()?;
|
||||
continuity(&sample, before)?;
|
||||
budget_at(end, before.end, before.boot)?;
|
||||
let (error, age) = padded_quality(
|
||||
sample.server_error_ns(),
|
||||
sample.source_sync_age_ns(),
|
||||
sample.captured_monotonic_ns(),
|
||||
end.mono,
|
||||
)?;
|
||||
let payload = Payload {
|
||||
version: 1,
|
||||
request_sha256: request.sha256()?,
|
||||
server_instance: signer.server_instance,
|
||||
server_boot_id: sample.server_boot_id(),
|
||||
key_id: signer.key_id.clone(),
|
||||
key_generation: signer.generation,
|
||||
captured_utc_ns: sample.captured_utc_ns(),
|
||||
server_error_ns: error,
|
||||
source_sync_age_ns: age,
|
||||
};
|
||||
let signed = rmm_time_attestation::sign(&payload, &signer.key)?;
|
||||
let after = capture()?;
|
||||
continuity(&sample, after)?;
|
||||
budget_at(end, after.end, after.boot)?;
|
||||
signer.recheck()?;
|
||||
write_reply(stream, &signed, end)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
fn configuration() -> Configuration {
|
||||
Configuration {
|
||||
schema_version: 1,
|
||||
server_instance: Uuid::from_bytes([1; 16]),
|
||||
key_id: "time-v1".into(),
|
||||
key_generation: 1,
|
||||
signing_key_hex: hex::encode([42u8; 32]),
|
||||
forbidden_public_keys: vec![hex::encode(
|
||||
SigningKey::from_bytes(&[43; 32]).verifying_key().to_bytes(),
|
||||
)],
|
||||
expected_server_uid: 10001,
|
||||
server_gid: 10001,
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn signer_cannot_reuse_protected_forbidden_key_or_change_server_identity() {
|
||||
let mut config = configuration();
|
||||
let signer = key(&config).unwrap();
|
||||
config.forbidden_public_keys = vec![hex::encode(signer.verifying_key().to_bytes())];
|
||||
assert!(key(&config).is_err());
|
||||
config.forbidden_public_keys.clear();
|
||||
assert!(key(&config).is_err());
|
||||
let mut config = configuration();
|
||||
config.server_gid = 0;
|
||||
assert!(key(&config).is_err());
|
||||
let mut config = configuration();
|
||||
config.expected_server_uid = 0;
|
||||
assert!(key(&config).is_err());
|
||||
}
|
||||
fn request() -> Request {
|
||||
use rmm_time_attestation::{Context, ENDPOINT, Role};
|
||||
Request {
|
||||
version: 1,
|
||||
endpoint: ENDPOINT.into(),
|
||||
nonce: "ab".repeat(32),
|
||||
context: Context {
|
||||
tenant_id: Uuid::from_bytes([1; 16]),
|
||||
asset_id: Uuid::from_bytes([2; 16]),
|
||||
agent_id: Uuid::from_bytes([3; 16]),
|
||||
enrollment_generation: 1,
|
||||
assignment_generation: 1,
|
||||
connection_generation: 1,
|
||||
windows_boot_id: Uuid::from_bytes([4; 16]),
|
||||
verifier_incarnation: Uuid::from_bytes([5; 16]),
|
||||
role: Role::Agent,
|
||||
helper_image_sha256: None,
|
||||
binding_schema: 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn frame_requires_exact_small_canonical_request_before_native_work() {
|
||||
let request = request();
|
||||
let raw = serde_json::to_vec(&request).unwrap();
|
||||
assert!(parse_request(&raw).is_ok());
|
||||
let mut spaced = vec![b' '];
|
||||
spaced.extend_from_slice(&raw);
|
||||
assert!(parse_request(&spaced).is_err());
|
||||
assert!(frame_length(0u32.to_be_bytes()).is_err());
|
||||
assert!(frame_length(8193u32.to_be_bytes()).is_err());
|
||||
assert_eq!(frame_length(8192u32.to_be_bytes()).unwrap(), 8192);
|
||||
let mut unknown = serde_json::to_value(&request).unwrap();
|
||||
unknown["extra"] = true.into();
|
||||
assert!(parse_request(&serde_json::to_vec(&unknown).unwrap()).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn whole_deadline_and_pre_sign_reservation_never_extend_quality_bounds() {
|
||||
let end = Deadline {
|
||||
mono: 100,
|
||||
boot: 200,
|
||||
};
|
||||
assert!(budget_at(end, 99, 201).is_err());
|
||||
assert!(budget_at(end, 101, 199).is_err());
|
||||
assert_eq!(budget_at(end, 99, 198).unwrap(), 1);
|
||||
assert!(remaining(100, 100).is_err());
|
||||
assert!(remaining(100, 101).is_err());
|
||||
assert_eq!(remaining(100, 99).unwrap(), 1);
|
||||
let (error, age) = padded_quality(10_000_000, 1_000_000_000, 100, 200).unwrap();
|
||||
assert_eq!(error, 30_000_100);
|
||||
assert_eq!(age, 1_020_000_100);
|
||||
assert!(padded_quality(MAX_ERROR_NS, 0, 100, 200).is_err());
|
||||
assert!(padded_quality(0, MAX_AGE_NS, 100, 200).is_err());
|
||||
assert!(padded_quality(0, 0, 200, 100).is_err());
|
||||
assert!(padded_quality(u64::MAX, 0, 0, 1).is_err());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user