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, 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 { 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: libc::nlink_t, } fn fd(value: i32) -> Result { 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 { 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 { let mut st = mem::MaybeUninit::::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 { 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 { 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 { 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 { let mut value = mem::MaybeUninit::::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 { 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 { 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 { 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 { Ok(remaining(end.mono, mono)?.min(remaining(end.boot, boot)?)) } fn budget(end: Deadline) -> Result { 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 { 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 { 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 { 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 { 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::::uninit(); let mut length = mem::size_of::() 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::(), "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, } impl Listener { fn current(&self) -> Result { let name = CString::new("broker.sock")?; let mut st = mem::MaybeUninit::::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 { 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 { 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, updater_image_sha256: None, }, } } #[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()); } }