Wire preserved update control, health evidence and record publication
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -9,6 +9,8 @@ pub mod readiness;
|
||||
pub mod reconcile;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub mod update_control;
|
||||
pub mod update_guard;
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
use openssl::pkey::{Id, PKey};
|
||||
|
||||
@@ -133,11 +133,12 @@ impl Config {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum Incoming {
|
||||
NativeUpdate(rmm_agent_core::update_handoff::NativeControlRequest),
|
||||
Control(control::Request),
|
||||
Probe(rmm_privileged_helper::readiness::Request),
|
||||
Job(Request),
|
||||
}
|
||||
fn peer_allowed(stream: &UnixStream, uid: u32) -> Result<()> {
|
||||
fn peer_uid(stream: &UnixStream) -> Result<u32> {
|
||||
let mut credentials = std::mem::MaybeUninit::<libc::ucred>::uninit();
|
||||
let mut size = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
|
||||
// SAFETY: getsockopt receives correctly aligned writable storage and length.
|
||||
@@ -158,10 +159,10 @@ fn peer_allowed(stream: &UnixStream, uid: u32) -> Result<()> {
|
||||
"PEER_SIZE"
|
||||
);
|
||||
// SAFETY: successful SO_PEERCRED initialized the complete ucred.
|
||||
ensure!(
|
||||
unsafe { credentials.assume_init() }.uid == uid && uid != 0,
|
||||
"PEER_DENIED"
|
||||
);
|
||||
Ok(unsafe { credentials.assume_init() }.uid)
|
||||
}
|
||||
fn peer_allowed(stream: &UnixStream, uid: u32) -> Result<()> {
|
||||
ensure!(uid != 0 && peer_uid(stream)? == uid, "PEER_DENIED");
|
||||
Ok(())
|
||||
}
|
||||
fn policy(config: &Config, job: &rmm_domain::jobs::JobPayload) -> Result<()> {
|
||||
@@ -320,17 +321,29 @@ fn execute(job: &rmm_domain::jobs::JobPayload) -> Result<Outcome> {
|
||||
.into(),
|
||||
})
|
||||
}
|
||||
struct RuntimeClocks {
|
||||
action: rmm_privileged_helper::TrustedClock,
|
||||
sync: reconcile::NativeClock,
|
||||
update: Option<rmm_updater_worker::clock::NativeClock>,
|
||||
}
|
||||
fn handle(
|
||||
config: &Config,
|
||||
db: &mut rusqlite::Connection,
|
||||
stream: &mut UnixStream,
|
||||
clock: &rmm_privileged_helper::TrustedClock,
|
||||
clocks: &RuntimeClocks,
|
||||
sync: &mut reconcile::HelperSync,
|
||||
sync_clock: &reconcile::NativeClock,
|
||||
mut image: Option<&mut image::RunningImage>,
|
||||
) -> Result<()> {
|
||||
use rusqlite::OptionalExtension;
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
let clock = &clocks.action;
|
||||
let sync_clock = &clocks.sync;
|
||||
let update_clock = clocks.update.as_ref();
|
||||
let caller_uid = peer_uid(stream)?;
|
||||
ensure!(
|
||||
caller_uid == config.agent_uid
|
||||
|| (caller_uid == 0 && config.profile == HelperProfile::FullMaintenanceV1),
|
||||
"PEER_DENIED"
|
||||
);
|
||||
stream.set_read_timeout(Some(Duration::from_secs(5)))?;
|
||||
stream.set_write_timeout(Some(Duration::from_secs(5)))?;
|
||||
let mut size = [0; 4];
|
||||
@@ -345,11 +358,36 @@ fn handle(
|
||||
"RUNNING_HELPER_BINARY_CHANGED"
|
||||
);
|
||||
let request: Request = match serde_json::from_slice::<Incoming>(&bytes)? {
|
||||
Incoming::NativeUpdate(request) => {
|
||||
ensure!(
|
||||
config.profile == HelperProfile::FullMaintenanceV1
|
||||
&& config.socket
|
||||
== std::path::Path::new("/run/rmm-helper-maintenance/control.sock")
|
||||
&& size <= 32768,
|
||||
"UPDATE_FULL_CONTROL_REQUIRED"
|
||||
);
|
||||
let resource = reconcile::ExecutorGuard::acquire(&config.state, 0)?;
|
||||
let response = rmm_privileged_helper::update_control::handle(
|
||||
db,
|
||||
&resource,
|
||||
&config.binding,
|
||||
update_clock.ok_or_else(|| anyhow::anyhow!("UPDATE_NOT_CONFIGURED"))?,
|
||||
stream,
|
||||
&request,
|
||||
)?;
|
||||
let raw = serde_json::to_vec(&response)?;
|
||||
ensure!(raw.len() <= 1536 * 1024, "UPDATE_CONTROL_RESPONSE_LIMIT");
|
||||
stream.write_all(&u32::try_from(raw.len())?.to_be_bytes())?;
|
||||
stream.write_all(&raw)?;
|
||||
return Ok(());
|
||||
}
|
||||
Incoming::Job(request) => {
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
ensure!(size <= 512 * 1024, "JOB_REQUEST_SIZE");
|
||||
request
|
||||
}
|
||||
Incoming::Control(request) => {
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
let (challenge, identity) = request.binding();
|
||||
ensure!(
|
||||
!challenge.is_nil()
|
||||
@@ -360,6 +398,21 @@ fn handle(
|
||||
"CONTROL_IDENTITY"
|
||||
);
|
||||
let guard = reconcile::ExecutorGuard::acquire(&config.state, 0)?;
|
||||
let update_guard = if config.profile == HelperProfile::FullMaintenanceV1 {
|
||||
rmm_privileged_helper::update_control::active_grant(db)?
|
||||
.map(|grant| {
|
||||
rmm_privileged_helper::update_guard::FullUpdateGuard::open(
|
||||
db,
|
||||
&guard,
|
||||
&config.binding,
|
||||
update_clock.ok_or_else(|| anyhow::anyhow!("UPDATE_NOT_CONFIGURED"))?,
|
||||
grant,
|
||||
)
|
||||
})
|
||||
.transpose()?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let (mut snapshot, mut unlocked) = (None, vec![]);
|
||||
match &request {
|
||||
control::Request::Snapshot { generation, .. } => {
|
||||
@@ -369,27 +422,36 @@ fn handle(
|
||||
&guard,
|
||||
reconcile::Clock::now(sync_clock)?,
|
||||
)?;
|
||||
snapshot = Some(sync.snapshot(
|
||||
db,
|
||||
&guard,
|
||||
*generation,
|
||||
sync_clock,
|
||||
&reconcile::NativeBoot,
|
||||
)?)
|
||||
snapshot = Some(if let Some(owner) = &update_guard {
|
||||
sync.snapshot(db, owner, *generation, sync_clock, &reconcile::NativeBoot)?
|
||||
} else {
|
||||
sync.snapshot(db, &guard, *generation, sync_clock, &reconcile::NativeBoot)?
|
||||
})
|
||||
}
|
||||
control::Request::Apply {
|
||||
response,
|
||||
request_bytes,
|
||||
..
|
||||
} => {
|
||||
let applied = sync.apply(
|
||||
db,
|
||||
&guard,
|
||||
response,
|
||||
request_bytes,
|
||||
sync_clock,
|
||||
&reconcile::NativeBoot,
|
||||
)?;
|
||||
let applied = if let Some(owner) = &update_guard {
|
||||
sync.apply(
|
||||
db,
|
||||
owner,
|
||||
response,
|
||||
request_bytes,
|
||||
sync_clock,
|
||||
&reconcile::NativeBoot,
|
||||
)?
|
||||
} else {
|
||||
sync.apply(
|
||||
db,
|
||||
&guard,
|
||||
response,
|
||||
request_bytes,
|
||||
sync_clock,
|
||||
&reconcile::NativeBoot,
|
||||
)?
|
||||
};
|
||||
snapshot = Some(applied.snapshot);
|
||||
unlocked = applied.unlocked_grants;
|
||||
}
|
||||
@@ -446,6 +508,7 @@ fn handle(
|
||||
return Ok(());
|
||||
}
|
||||
Incoming::Probe(request) => {
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
ensure!(
|
||||
config.profile == HelperProfile::AptOnly,
|
||||
"PROBE_REQUIRES_APT_HELPER"
|
||||
@@ -722,9 +785,23 @@ fn run() -> Result<()> {
|
||||
let _initialization_lock = lock_state(&config.state, 0)?;
|
||||
open_ledger(&config.state, 0)?
|
||||
};
|
||||
let clock = rmm_privileged_helper::TrustedClock::new();
|
||||
let sync_clock = reconcile::NativeClock::default();
|
||||
let mut sync = reconcile::HelperSync::new(config.binding.clone(), &sync_clock)?;
|
||||
let update_clock = if config.profile == HelperProfile::FullMaintenanceV1
|
||||
&& std::path::Path::new("/etc/rmm-agent-updater/policy.json").try_exists()?
|
||||
{
|
||||
Some(rmm_updater_worker::clock::NativeClock::new()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if update_clock.is_some() {
|
||||
let guard = reconcile::ExecutorGuard::acquire(&config.state, 0)?;
|
||||
rmm_agent_core::update_handoff::initialize(&db, &guard)?;
|
||||
}
|
||||
let clocks = RuntimeClocks {
|
||||
action: rmm_privileged_helper::TrustedClock::new(),
|
||||
sync: reconcile::NativeClock::default(),
|
||||
update: update_clock,
|
||||
};
|
||||
let mut sync = reconcile::HelperSync::new(config.binding.clone(), &clocks.sync)?;
|
||||
let parent = config
|
||||
.socket
|
||||
.parent()
|
||||
@@ -751,9 +828,8 @@ fn run() -> Result<()> {
|
||||
&config,
|
||||
&mut db,
|
||||
&mut stream,
|
||||
&clock,
|
||||
&clocks,
|
||||
&mut sync,
|
||||
&sync_clock,
|
||||
running_image.as_mut(),
|
||||
) {
|
||||
let code = safe_error_code(&error);
|
||||
|
||||
@@ -27,7 +27,7 @@ impl BootSource for NativeBoot {
|
||||
/// Holding this guard is mandatory through snapshot/apply/can_start/consume and
|
||||
/// actual effect. Production uses owner_uid=0; tests use their own private UID.
|
||||
pub struct ExecutorGuard {
|
||||
_file: File,
|
||||
pub(crate) _file: File,
|
||||
directory: PathBuf,
|
||||
_not_sync: std::marker::PhantomData<std::cell::Cell<()>>,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
//! Closed control of the one ORIGINAL Full-authorized update. Caller data never
|
||||
//! authorizes an event: actual process/file/ledger observations are independent.
|
||||
use crate::{Binding, reconcile::ExecutorGuard, update_guard::FullUpdateGuard};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use rmm_agent_core::{
|
||||
reconcile::SyncClock,
|
||||
update_handoff::{
|
||||
self as core, NativeControlOperation, NativeControlReply, NativeControlRequest,
|
||||
NativeEffect, NativeService,
|
||||
},
|
||||
};
|
||||
use rmm_updater_handoff::{Evidence, Phase};
|
||||
use rmm_updater_worker::clock::NativeClock;
|
||||
use rusqlite::Connection;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn active_grant(db: &Connection) -> Result<Option<Uuid>> {
|
||||
let exists:bool=db.query_row("SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='agent_update_handoffs')",[],|r|r.get(0))?;
|
||||
if !exists {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut query=db.prepare("SELECT grant_id FROM agent_update_handoffs WHERE phase NOT IN ('unknown','outcome_persisted') ORDER BY grant_id LIMIT 2")?;
|
||||
let rows = query
|
||||
.query_map([], |r| r.get::<_, String>(0))?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
ensure!(rows.len() <= 1, "UPDATE_MULTIPLE_ORIGINAL_HANDOFFS");
|
||||
rows.first()
|
||||
.map(|id| Uuid::parse_str(id).map_err(Into::into))
|
||||
.transpose()
|
||||
}
|
||||
pub fn handle(
|
||||
db: &mut Connection,
|
||||
executor: &ExecutorGuard,
|
||||
binding: &Binding,
|
||||
clock: &NativeClock,
|
||||
stream: &UnixStream,
|
||||
request: &NativeControlRequest,
|
||||
) -> Result<NativeControlReply> {
|
||||
ensure!(
|
||||
request.operation == "agent_update_control_v1"
|
||||
&& request.schema_version == 1
|
||||
&& !request.challenge.is_nil()
|
||||
&& request.tenant_id == binding.tenant_id
|
||||
&& request.asset_id == binding.asset_id
|
||||
&& request.agent_id == binding.agent_id
|
||||
&& request.enrollment_generation == binding.enrollment_generation
|
||||
&& request.assignment_version == binding.assignment_version,
|
||||
"UPDATE_CONTROL_BINDING"
|
||||
);
|
||||
let mut guard = FullUpdateGuard::open(db, executor, binding, clock, request.grant_id)?;
|
||||
let header = guard.original().handoff().clone();
|
||||
ensure!(
|
||||
header.owner_nonce == request.owner_nonce
|
||||
&& rmm_domain::jobs::hash(&header.bytes()?) == request.handoff_sha256,
|
||||
"UPDATE_CONTROL_ORIGINAL_REQUIRED"
|
||||
);
|
||||
if !matches!(request.method, NativeControlOperation::ReadOriginal) {
|
||||
guard.qualify_effects(db)?;
|
||||
}
|
||||
if matches!(request.method, NativeControlOperation::Health { .. }) {
|
||||
guard.verify_health_peer(stream)?;
|
||||
} else {
|
||||
guard.verify_updater_peer(stream)?;
|
||||
}
|
||||
let mut original_start_base64url = None;
|
||||
let mut claim = None;
|
||||
let mut original_outcome_base64url = None;
|
||||
match &request.method {
|
||||
NativeControlOperation::ReadOriginal => {
|
||||
original_start_base64url = Some(URL_SAFE_NO_PAD.encode(guard.original().start_bytes()))
|
||||
}
|
||||
NativeControlOperation::ClaimStop {
|
||||
expected_head_sha256,
|
||||
service,
|
||||
} => {
|
||||
claim = Some(core::claim_native_effect(
|
||||
db,
|
||||
&guard,
|
||||
request.grant_id,
|
||||
expected_head_sha256,
|
||||
&NativeEffect::StopService { service: *service },
|
||||
clock.now()?,
|
||||
)?);
|
||||
}
|
||||
NativeControlOperation::ObserveStarts {
|
||||
services,
|
||||
observation_sha256,
|
||||
} => {
|
||||
let journal = core::read(db, &guard, request.grant_id)?;
|
||||
ensure!(
|
||||
journal.phase() == Phase::LedgerReturnIntent,
|
||||
"UPDATE_START_ALREADY_OBSERVED_OR_PHASE"
|
||||
);
|
||||
let (actual, actual_hash) = guard.started()?;
|
||||
ensure!(
|
||||
services == &actual && observation_sha256 == &actual_hash,
|
||||
"UPDATE_START_WORKER_WITNESS_CHANGED"
|
||||
);
|
||||
for service in [
|
||||
NativeService::General,
|
||||
NativeService::Full,
|
||||
NativeService::Agent,
|
||||
] {
|
||||
let effect = NativeEffect::StartService { service };
|
||||
let reserved = core::read_native_claim(db, &guard, request.grant_id, &effect)?;
|
||||
let already:bool=db.query_row("SELECT EXISTS(SELECT 1 FROM agent_update_native_observations WHERE grant_id=?1 AND claim_sha256=?2)",rusqlite::params![request.grant_id.to_string(),rmm_domain::jobs::hash(&serde_json::to_vec(&reserved)?)],|r|r.get(0))?;
|
||||
// One control request performs one sequence, never retries an
|
||||
// uncertain partially committed start-observation transaction.
|
||||
ensure!(!already, "UPDATE_START_OBSERVATION_ALREADY_COMMITTED");
|
||||
core::observe_native_effect(
|
||||
db,
|
||||
&guard,
|
||||
&reserved,
|
||||
&guard.service_observation_sha256(service)?,
|
||||
clock.now()?,
|
||||
)?;
|
||||
}
|
||||
let event = journal.prepare(
|
||||
u64::try_from(clock.now()?.timestamp())?,
|
||||
Evidence::ServicesStarted {
|
||||
services: actual,
|
||||
ledger_identity_sha256: header.ledger_identity_sha256.clone(),
|
||||
observation_sha256: actual_hash,
|
||||
},
|
||||
)?;
|
||||
core::append(db, &guard, request.grant_id, journal.head_sha256(), &event)?;
|
||||
}
|
||||
NativeControlOperation::Health { receipt } => {
|
||||
guard.qualify_health(db, receipt)?;
|
||||
let journal = core::read(db, &guard, request.grant_id)?;
|
||||
let event = journal.prepare(
|
||||
u64::try_from(clock.now()?.timestamp())?,
|
||||
Evidence::HealthVerified {
|
||||
receipt: receipt.clone(),
|
||||
},
|
||||
)?;
|
||||
core::append(db, &guard, request.grant_id, journal.head_sha256(), &event)?;
|
||||
}
|
||||
NativeControlOperation::Finalize => {
|
||||
original_outcome_base64url =
|
||||
Some(URL_SAFE_NO_PAD.encode(guard.publish_record_and_finalize(db)?));
|
||||
}
|
||||
}
|
||||
let proof = if original_outcome_base64url.is_some() {
|
||||
None
|
||||
} else {
|
||||
Some(
|
||||
core::current_proof(db, &guard, request.grant_id)?
|
||||
.context("UPDATE_ACTIVE_PROOF_REQUIRED")?,
|
||||
)
|
||||
};
|
||||
Ok(NativeControlReply {
|
||||
schema_version: 1,
|
||||
challenge: request.challenge,
|
||||
tenant_id: binding.tenant_id,
|
||||
asset_id: binding.asset_id,
|
||||
agent_id: binding.agent_id,
|
||||
enrollment_generation: binding.enrollment_generation,
|
||||
assignment_version: binding.assignment_version,
|
||||
grant_id: request.grant_id,
|
||||
owner_nonce: request.owner_nonce,
|
||||
handoff_sha256: request.handoff_sha256.clone(),
|
||||
ready: false,
|
||||
proof,
|
||||
claim,
|
||||
original_start_base64url,
|
||||
original_outcome_base64url,
|
||||
})
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user