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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user