feat: snapshot Linux-first RMM MVP and pause handover
Rust development slice / linux (push) Failing after 1m10s
Rust development slice / linux (push) Failing after 1m10s
This commit is contained in:
@@ -0,0 +1,328 @@
|
||||
//! Typed, root-peer authenticated synchronization/policy IPC. No action or
|
||||
//! readiness flag supplied by the network process grants root authority.
|
||||
use crate::{
|
||||
Binding,
|
||||
readiness::Identity,
|
||||
reconcile::{SignedResponse, Snapshot},
|
||||
};
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use serde::{Deserialize, Serialize, de::DeserializeOwned};
|
||||
use std::{
|
||||
fs,
|
||||
io::{Read, Write},
|
||||
os::{
|
||||
fd::AsRawFd,
|
||||
unix::{
|
||||
fs::{FileTypeExt, MetadataExt},
|
||||
net::UnixStream,
|
||||
},
|
||||
},
|
||||
path::Path,
|
||||
time::Duration,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "operation", deny_unknown_fields)]
|
||||
pub enum Request {
|
||||
#[serde(rename = "sync_snapshot_v1")]
|
||||
Snapshot {
|
||||
challenge: Uuid,
|
||||
identity: Identity,
|
||||
generation: u64,
|
||||
},
|
||||
#[serde(rename = "sync_apply_v1")]
|
||||
Apply {
|
||||
challenge: Uuid,
|
||||
identity: Identity,
|
||||
response: SignedResponse,
|
||||
request_bytes: Vec<u8>,
|
||||
},
|
||||
#[serde(rename = "general_policy_v1")]
|
||||
Policy { challenge: Uuid, identity: Identity },
|
||||
}
|
||||
impl Request {
|
||||
pub fn binding(&self) -> (Uuid, &Identity) {
|
||||
match self {
|
||||
Self::Snapshot {
|
||||
challenge,
|
||||
identity,
|
||||
..
|
||||
}
|
||||
| Self::Apply {
|
||||
challenge,
|
||||
identity,
|
||||
..
|
||||
}
|
||||
| Self::Policy {
|
||||
challenge,
|
||||
identity,
|
||||
} => (*challenge, identity),
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Policy {
|
||||
pub schema_version: u32,
|
||||
pub profile: String,
|
||||
pub identity: Identity,
|
||||
pub allowed_services: Vec<String>,
|
||||
pub allow_root_scripts: bool,
|
||||
pub allow_reboot: bool,
|
||||
}
|
||||
impl Policy {
|
||||
pub fn sha256(&self) -> Result<String> {
|
||||
Ok(rmm_domain::jobs::hash(&serde_json::to_vec(self)?))
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Response {
|
||||
pub schema_version: u32,
|
||||
pub challenge: Uuid,
|
||||
pub identity: Identity,
|
||||
pub profile: String,
|
||||
pub policy_sha256: String,
|
||||
pub policy: Option<Policy>,
|
||||
pub snapshot: Option<Snapshot>,
|
||||
pub unlocked_grants: Vec<Uuid>,
|
||||
pub ledger_device: u64,
|
||||
pub ledger_inode: u64,
|
||||
pub lock_device: u64,
|
||||
pub lock_inode: u64,
|
||||
}
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct GeneralPin {
|
||||
pub schema_version: u32,
|
||||
pub profile: String,
|
||||
pub identity: Identity,
|
||||
pub policy_sha256: String,
|
||||
}
|
||||
impl GeneralPin {
|
||||
pub fn read(path: &Path, binding: &Binding) -> Result<Self> {
|
||||
trusted(path, false)?;
|
||||
ensure!(fs::metadata(path)?.len() <= 8192, "POLICY_PIN_LIMIT");
|
||||
let pin: Self = serde_json::from_slice(&fs::read(path)?)?;
|
||||
ensure!(
|
||||
pin.schema_version == 1
|
||||
&& pin.profile == "general"
|
||||
&& pin.identity == Identity::from_binding(binding)?
|
||||
&& digest(&pin.policy_sha256),
|
||||
"POLICY_PIN_BINDING"
|
||||
);
|
||||
Ok(pin)
|
||||
}
|
||||
}
|
||||
fn digest(s: &str) -> bool {
|
||||
s.len() == 64
|
||||
&& s.bytes()
|
||||
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
||||
}
|
||||
pub fn trusted(path: &Path, socket: bool) -> Result<()> {
|
||||
ensure!(path.is_absolute(), "HELPER_ABSOLUTE_PATH");
|
||||
for (index, parent) in path.ancestors().enumerate() {
|
||||
let m = fs::symlink_metadata(parent)?;
|
||||
ensure!(
|
||||
!m.file_type().is_symlink()
|
||||
&& m.uid() == 0
|
||||
&& (m.mode() & 0o022 == 0 || (socket && index == 0 && m.mode() & 0o007 == 0)),
|
||||
"UNTRUSTED_HELPER_PATH"
|
||||
);
|
||||
}
|
||||
ensure!(
|
||||
if socket {
|
||||
fs::symlink_metadata(path)?.file_type().is_socket()
|
||||
} else {
|
||||
fs::metadata(path)?.is_file()
|
||||
},
|
||||
"HELPER_PATH_TYPE"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
pub fn exchange<Q: Serialize, R: DeserializeOwned>(
|
||||
socket: &Path,
|
||||
request: &Q,
|
||||
timeout: Duration,
|
||||
max_response: usize,
|
||||
) -> Result<R> {
|
||||
trusted(socket, true)?;
|
||||
let mut stream = UnixStream::connect(socket)?;
|
||||
let mut credentials = std::mem::MaybeUninit::<libc::ucred>::uninit();
|
||||
let mut size = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
|
||||
// SAFETY: live socket and correctly aligned writable credentials storage.
|
||||
ensure!(
|
||||
unsafe {
|
||||
libc::getsockopt(
|
||||
stream.as_raw_fd(),
|
||||
libc::SOL_SOCKET,
|
||||
libc::SO_PEERCRED,
|
||||
credentials.as_mut_ptr().cast(),
|
||||
&mut size,
|
||||
)
|
||||
} == 0
|
||||
&& size as usize == std::mem::size_of::<libc::ucred>(),
|
||||
"HELPER_PEER_CREDENTIALS"
|
||||
);
|
||||
// SAFETY: successful getsockopt initialized the full credentials.
|
||||
ensure!(
|
||||
unsafe { credentials.assume_init() }.uid == 0,
|
||||
"HELPER_PEER_NOT_ROOT"
|
||||
);
|
||||
stream.set_read_timeout(Some(timeout))?;
|
||||
stream.set_write_timeout(Some(Duration::from_secs(5)))?;
|
||||
let bytes = serde_json::to_vec(request)?;
|
||||
ensure!(
|
||||
bytes.len() <= 8 * 1024 * 1024,
|
||||
"HELPER_CONTROL_REQUEST_LIMIT"
|
||||
);
|
||||
stream.write_all(&u32::try_from(bytes.len())?.to_be_bytes())?;
|
||||
stream.write_all(&bytes)?;
|
||||
let mut length = [0; 4];
|
||||
stream.read_exact(&mut length)?;
|
||||
let length = u32::from_be_bytes(length) as usize;
|
||||
ensure!(length <= max_response, "HELPER_CONTROL_RESPONSE_LIMIT");
|
||||
let mut bytes = vec![0; length];
|
||||
stream.read_exact(&mut bytes)?;
|
||||
Ok(serde_json::from_slice(&bytes)?)
|
||||
}
|
||||
pub fn call(socket: &Path, binding: &Binding, request: &Request) -> Result<Response> {
|
||||
let response: Response = exchange(socket, request, Duration::from_secs(10), 8 * 1024 * 1024)?;
|
||||
validate_response(request, &response, binding)?;
|
||||
Ok(response)
|
||||
}
|
||||
pub fn validate_response(request: &Request, response: &Response, binding: &Binding) -> Result<()> {
|
||||
let (challenge, identity) = request.binding();
|
||||
ensure!(
|
||||
!challenge.is_nil()
|
||||
&& response.schema_version == 1
|
||||
&& response.challenge == challenge
|
||||
&& &response.identity == identity
|
||||
&& response.identity == Identity::from_binding(binding)?
|
||||
&& matches!(response.profile.as_str(), "general" | "apt_only")
|
||||
&& response.ledger_inode != 0
|
||||
&& response.lock_inode != 0,
|
||||
"HELPER_CONTROL_RESPONSE_BINDING"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
pub fn general_probe(socket: &Path, binding: &Binding, pin: &GeneralPin) -> Result<Response> {
|
||||
let request = Request::Policy {
|
||||
challenge: Uuid::new_v4(),
|
||||
identity: Identity::from_binding(binding)?,
|
||||
};
|
||||
let response = call(socket, binding, &request)?;
|
||||
let policy = response.policy.as_ref().context("GENERAL_POLICY_MISSING")?;
|
||||
ensure!(
|
||||
response.profile == "general"
|
||||
&& policy.profile == "general"
|
||||
&& response.policy_sha256 == pin.policy_sha256
|
||||
&& policy.sha256()? == pin.policy_sha256
|
||||
&& policy.identity == pin.identity,
|
||||
"GENERAL_POLICY_PROOF_MISMATCH"
|
||||
);
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
fn binding() -> Binding {
|
||||
Binding {
|
||||
tenant_id: Uuid::new_v4(),
|
||||
asset_id: Uuid::new_v4(),
|
||||
agent_id: Uuid::new_v4(),
|
||||
enrollment_generation: 1,
|
||||
assignment_version: 1,
|
||||
public_key_hex: hex::encode(
|
||||
openssl::pkey::PKey::generate_ed25519()
|
||||
.unwrap()
|
||||
.raw_public_key()
|
||||
.unwrap(),
|
||||
),
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn control_nonce_identity_and_profile_are_not_unsigned_readiness() {
|
||||
let b = binding();
|
||||
let identity = Identity::from_binding(&b).unwrap();
|
||||
let challenge = Uuid::new_v4();
|
||||
let q = Request::Policy {
|
||||
challenge,
|
||||
identity: identity.clone(),
|
||||
};
|
||||
let mut r = Response {
|
||||
schema_version: 1,
|
||||
challenge,
|
||||
identity,
|
||||
profile: "general".into(),
|
||||
policy_sha256: "ab".repeat(32),
|
||||
policy: None,
|
||||
snapshot: None,
|
||||
unlocked_grants: vec![],
|
||||
ledger_device: 1,
|
||||
ledger_inode: 2,
|
||||
lock_device: 1,
|
||||
lock_inode: 3,
|
||||
};
|
||||
validate_response(&q, &r, &b).unwrap();
|
||||
r.challenge = Uuid::new_v4();
|
||||
assert!(validate_response(&q, &r, &b).is_err());
|
||||
r.challenge = challenge;
|
||||
r.identity.assignment_version = 2;
|
||||
assert!(validate_response(&q, &r, &b).is_err());
|
||||
r.identity = Identity::from_binding(&b).unwrap();
|
||||
r.profile = "fake_root".into();
|
||||
assert!(validate_response(&q, &r, &b).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn apply_preserves_original_signed_bytes_and_rejects_unlock_additions() {
|
||||
let b = binding();
|
||||
let q = Request::Apply {
|
||||
challenge: Uuid::new_v4(),
|
||||
identity: Identity::from_binding(&b).unwrap(),
|
||||
response: SignedResponse {
|
||||
decision_bytes: b" exact signed bytes\n".to_vec(),
|
||||
signature: vec![7; 64],
|
||||
},
|
||||
request_bytes: vec![10, 3, 1, 2, 3],
|
||||
};
|
||||
let bytes = serde_json::to_vec(&q).unwrap();
|
||||
let roundtrip: Request = serde_json::from_slice(&bytes).unwrap();
|
||||
let Request::Apply {
|
||||
response,
|
||||
request_bytes,
|
||||
..
|
||||
} = roundtrip
|
||||
else {
|
||||
panic!("wrong operation")
|
||||
};
|
||||
assert_eq!(response.decision_bytes, b" exact signed bytes\n");
|
||||
assert_eq!(request_bytes, vec![10, 3, 1, 2, 3]);
|
||||
let mut value = serde_json::to_value(q).unwrap();
|
||||
value["unlock"] = serde_json::json!(true);
|
||||
assert!(serde_json::from_value::<Request>(value).is_err());
|
||||
}
|
||||
#[test]
|
||||
fn general_policy_hash_binds_identity_units_and_broad_admin_flags() {
|
||||
let b = binding();
|
||||
let p = Policy {
|
||||
schema_version: 1,
|
||||
profile: "general".into(),
|
||||
identity: Identity::from_binding(&b).unwrap(),
|
||||
allowed_services: vec!["rmm-acceptance.service".into()],
|
||||
allow_root_scripts: false,
|
||||
allow_reboot: false,
|
||||
};
|
||||
let old = p.sha256().unwrap();
|
||||
let mut changed = p.clone();
|
||||
changed.allow_root_scripts = true;
|
||||
assert_ne!(old, changed.sha256().unwrap());
|
||||
changed = p.clone();
|
||||
changed.allowed_services.push("other.service".into());
|
||||
assert_ne!(old, changed.sha256().unwrap());
|
||||
changed = p;
|
||||
changed.identity.assignment_version += 1;
|
||||
assert_ne!(old, changed.sha256().unwrap());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user