Add isolated time broker and raw Windows capture with Linux 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,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))
|
||||
|
||||
Reference in New Issue
Block a user