feat: add approval bound full maintenance script console and executors
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
@@ -482,6 +482,7 @@ fn signed_request() -> (
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
},
|
||||
execution_identity: "root".into(),
|
||||
not_before: now - chrono::Duration::seconds(1),
|
||||
|
||||
@@ -69,6 +69,18 @@ pub struct Policy {
|
||||
pub allowed_services: Vec<String>,
|
||||
pub allow_root_scripts: bool,
|
||||
pub allow_reboot: bool,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
deserialize_with = "rmm_domain::jobs::deserialize_script_profiles"
|
||||
)]
|
||||
pub allowed_script_profiles: Option<Vec<rmm_domain::jobs::PrivilegedScriptProfile>>,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
deserialize_with = "rmm_domain::jobs::deserialize_optional_sha256"
|
||||
)]
|
||||
pub helper_binary_sha256: Option<String>,
|
||||
}
|
||||
impl Policy {
|
||||
pub fn sha256(&self) -> Result<String> {
|
||||
@@ -90,6 +102,12 @@ pub struct Response {
|
||||
pub ledger_inode: u64,
|
||||
pub lock_device: u64,
|
||||
pub lock_inode: u64,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
deserialize_with = "rmm_domain::jobs::deserialize_optional_sha256"
|
||||
)]
|
||||
pub helper_binary_sha256: Option<String>,
|
||||
}
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
@@ -98,6 +116,12 @@ pub struct GeneralPin {
|
||||
pub profile: String,
|
||||
pub identity: Identity,
|
||||
pub policy_sha256: String,
|
||||
#[serde(
|
||||
default,
|
||||
skip_serializing_if = "Option::is_none",
|
||||
deserialize_with = "rmm_domain::jobs::deserialize_optional_sha256"
|
||||
)]
|
||||
pub helper_binary_sha256: Option<String>,
|
||||
}
|
||||
impl GeneralPin {
|
||||
pub fn read(path: &Path, binding: &Binding) -> Result<Self> {
|
||||
@@ -105,8 +129,12 @@ impl GeneralPin {
|
||||
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.schema_version == 1
|
||||
&& pin.profile == "general"
|
||||
&& pin.helper_binary_sha256.is_none())
|
||||
|| (pin.schema_version == 2
|
||||
&& pin.profile == "full_maintenance_v1"
|
||||
&& pin.helper_binary_sha256.as_ref().is_some_and(|h| digest(h))))
|
||||
&& pin.identity == Identity::from_binding(binding)?
|
||||
&& digest(&pin.policy_sha256),
|
||||
"POLICY_PIN_BINDING"
|
||||
@@ -199,9 +227,20 @@ pub fn validate_response(request: &Request, response: &Response, binding: &Bindi
|
||||
&& response.challenge == challenge
|
||||
&& &response.identity == identity
|
||||
&& response.identity == Identity::from_binding(binding)?
|
||||
&& matches!(response.profile.as_str(), "general" | "apt_only")
|
||||
&& matches!(
|
||||
response.profile.as_str(),
|
||||
"general" | "apt_only" | "full_maintenance_v1"
|
||||
)
|
||||
&& response.ledger_inode != 0
|
||||
&& response.lock_inode != 0,
|
||||
&& response.lock_inode != 0
|
||||
&& if response.profile == "full_maintenance_v1" {
|
||||
response
|
||||
.helper_binary_sha256
|
||||
.as_ref()
|
||||
.is_some_and(|h| digest(h))
|
||||
} else {
|
||||
response.helper_binary_sha256.is_none()
|
||||
},
|
||||
"HELPER_CONTROL_RESPONSE_BINDING"
|
||||
);
|
||||
Ok(())
|
||||
@@ -214,8 +253,23 @@ pub fn general_probe(socket: &Path, binding: &Binding, pin: &GeneralPin) -> Resu
|
||||
let response = call(socket, binding, &request)?;
|
||||
let policy = response.policy.as_ref().context("GENERAL_POLICY_MISSING")?;
|
||||
ensure!(
|
||||
response.profile == "general"
|
||||
&& policy.profile == "general"
|
||||
response.profile == pin.profile
|
||||
&& policy.profile == pin.profile
|
||||
&& ((policy.schema_version == 1
|
||||
&& policy.profile == "general"
|
||||
&& policy.allowed_script_profiles.is_none()
|
||||
&& policy.helper_binary_sha256.is_none()
|
||||
&& pin.helper_binary_sha256.is_none())
|
||||
|| (policy.schema_version == 2
|
||||
&& policy.profile == "full_maintenance_v1"
|
||||
&& policy.allowed_script_profiles.as_deref()
|
||||
== Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1])
|
||||
&& policy.helper_binary_sha256 == pin.helper_binary_sha256
|
||||
&& response.helper_binary_sha256 == pin.helper_binary_sha256
|
||||
&& pin.helper_binary_sha256.is_some()
|
||||
&& policy.allow_root_scripts
|
||||
&& !policy.allow_reboot
|
||||
&& policy.allowed_services.is_empty()))
|
||||
&& response.policy_sha256 == pin.policy_sha256
|
||||
&& policy.sha256()? == pin.policy_sha256
|
||||
&& policy.identity == pin.identity,
|
||||
@@ -264,8 +318,21 @@ mod tests {
|
||||
ledger_inode: 2,
|
||||
lock_device: 1,
|
||||
lock_inode: 3,
|
||||
helper_binary_sha256: None,
|
||||
};
|
||||
validate_response(&q, &r, &b).unwrap();
|
||||
let legacy = serde_json::to_value(&r).unwrap();
|
||||
assert!(legacy.get("helper_binary_sha256").is_none());
|
||||
let mut null = legacy.clone();
|
||||
null["helper_binary_sha256"] = serde_json::Value::Null;
|
||||
assert!(serde_json::from_value::<Response>(null).is_err());
|
||||
r.profile = "full_maintenance_v1".into();
|
||||
assert!(validate_response(&q, &r, &b).is_err());
|
||||
r.helper_binary_sha256 = Some("ab".repeat(32));
|
||||
validate_response(&q, &r, &b).unwrap();
|
||||
r.profile = "general".into();
|
||||
assert!(validate_response(&q, &r, &b).is_err());
|
||||
r.helper_binary_sha256 = None;
|
||||
r.challenge = Uuid::new_v4();
|
||||
assert!(validate_response(&q, &r, &b).is_err());
|
||||
r.challenge = challenge;
|
||||
@@ -313,6 +380,8 @@ mod tests {
|
||||
allowed_services: vec!["rmm-acceptance.service".into()],
|
||||
allow_root_scripts: false,
|
||||
allow_reboot: false,
|
||||
allowed_script_profiles: None,
|
||||
helper_binary_sha256: None,
|
||||
};
|
||||
let old = p.sha256().unwrap();
|
||||
let mut changed = p.clone();
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
//! Root observes its own *running* executable. An agent-readable configured
|
||||
//! pathname or a build claim is never substituted for this descriptor.
|
||||
use anyhow::{Result, ensure};
|
||||
use openssl::sha::Sha256;
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{Read, Seek, SeekFrom},
|
||||
os::unix::fs::MetadataExt,
|
||||
};
|
||||
|
||||
pub struct RunningImage {
|
||||
file: File,
|
||||
expected: String,
|
||||
}
|
||||
impl RunningImage {
|
||||
pub fn current(expected: &str) -> Result<Self> {
|
||||
let mut value = Self {
|
||||
file: File::open("/proc/self/exe")?,
|
||||
expected: expected.into(),
|
||||
};
|
||||
value.verify()?;
|
||||
Ok(value)
|
||||
}
|
||||
pub fn verify(&mut self) -> Result<String> {
|
||||
let before = self.file.metadata()?;
|
||||
ensure!(
|
||||
before.is_file()
|
||||
&& before.uid() == 0
|
||||
&& before.mode() & 0o022 == 0
|
||||
&& before.len() > 0
|
||||
&& before.len() <= 128 * 1024 * 1024,
|
||||
"RUNNING_HELPER_IMAGE_METADATA"
|
||||
);
|
||||
let identity = |m: &std::fs::Metadata| {
|
||||
(
|
||||
m.dev(),
|
||||
m.ino(),
|
||||
m.len(),
|
||||
m.uid(),
|
||||
m.mode(),
|
||||
m.mtime(),
|
||||
m.mtime_nsec(),
|
||||
m.ctime(),
|
||||
m.ctime_nsec(),
|
||||
)
|
||||
};
|
||||
self.file.seek(SeekFrom::Start(0))?;
|
||||
let mut hash = Sha256::new();
|
||||
let mut buffer = [0u8; 65536];
|
||||
let mut length = 0u64;
|
||||
loop {
|
||||
let n = self.file.read(&mut buffer)?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
length += n as u64;
|
||||
ensure!(
|
||||
length <= before.len(),
|
||||
"RUNNING_HELPER_IMAGE_LENGTH_CHANGED"
|
||||
);
|
||||
hash.update(&buffer[..n]);
|
||||
}
|
||||
let actual = hex::encode(hash.finish());
|
||||
ensure!(
|
||||
length == before.len()
|
||||
&& identity(&before) == identity(&self.file.metadata()?)
|
||||
&& actual == self.expected,
|
||||
"RUNNING_HELPER_IMAGE_PIN_MISMATCH"
|
||||
);
|
||||
Ok(actual)
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn actual_running_image_is_independent_of_a_claimed_path_or_digest() {
|
||||
let metadata = std::fs::metadata("/proc/self/exe").unwrap();
|
||||
let bytes = std::fs::read("/proc/self/exe").unwrap();
|
||||
let actual = hex::encode(openssl::sha::sha256(&bytes));
|
||||
if metadata.uid() == 0 && metadata.mode() & 0o022 == 0 {
|
||||
let mut image = RunningImage::current(&actual).unwrap();
|
||||
assert_eq!(image.verify().unwrap(), actual);
|
||||
} else {
|
||||
// An untrusted-owned/writable executable must fail even if its
|
||||
// content hash is correct. This is a tested denial, never a skip.
|
||||
assert!(RunningImage::current(&actual).is_err());
|
||||
}
|
||||
assert!(RunningImage::current(&"00".repeat(32)).is_err());
|
||||
}
|
||||
}
|
||||
@@ -124,13 +124,17 @@ pub fn save_start(
|
||||
}
|
||||
|
||||
pub fn script(job: &JobPayload) -> Result<Outcome> {
|
||||
use rmm_agent_linux::actions::{Interpreter, ScriptRequest, run_script};
|
||||
script_controlled(job, || Ok(()))
|
||||
}
|
||||
pub fn script_controlled(job: &JobPayload, observe: impl FnMut() -> Result<()>) -> Result<Outcome> {
|
||||
use rmm_agent_linux::actions::{Interpreter, ScriptRequest, run_script_controlled};
|
||||
let rmm_domain::jobs::Action::RunScript {
|
||||
interpreter,
|
||||
script_utf8,
|
||||
arguments,
|
||||
expected_stdout,
|
||||
windows_execution_profile: None,
|
||||
..
|
||||
} = &job.action
|
||||
else {
|
||||
anyhow::bail!("NOT_SCRIPT")
|
||||
@@ -147,14 +151,15 @@ pub fn script(job: &JobPayload) -> Result<Outcome> {
|
||||
std::time::Duration::from_secs(job.max_runtime_seconds.into()),
|
||||
job.max_output_bytes as usize,
|
||||
)?;
|
||||
let output = run_script(&request)?;
|
||||
let output = run_script_controlled(&request, observe)?;
|
||||
let passed = output.code == Some(0)
|
||||
&& !output.timed_out
|
||||
&& !output.interrupted
|
||||
&& expected_stdout
|
||||
.as_ref()
|
||||
.is_none_or(|s| !output.truncated && output.stdout == s.as_bytes());
|
||||
Ok(Outcome {
|
||||
state: if output.timed_out || output.code.is_none() {
|
||||
state: if output.timed_out || output.interrupted || output.code.is_none() {
|
||||
"unknown"
|
||||
} else if passed {
|
||||
"succeeded"
|
||||
@@ -167,9 +172,11 @@ pub fn script(job: &JobPayload) -> Result<Outcome> {
|
||||
exit_code: output.code,
|
||||
output_truncated: output.truncated,
|
||||
reboot_required: false,
|
||||
evidence: serde_json::json!({"postconditions_passed":passed}),
|
||||
evidence: serde_json::json!({"postconditions_passed":passed,"script_sha256":rmm_domain::jobs::hash(script_utf8.as_bytes()),"privileged_script_profile":if job.action.full_maintenance(){Some("full_maintenance_v1")}else{None},"execution_uid":unsafe{libc::geteuid()},"output_encoding":"raw_native_bytes","cancel_or_fence_observed":output.interrupted,"termination_does_not_prove_absence_of_effects":output.interrupted||output.timed_out}),
|
||||
error_code: if passed {
|
||||
""
|
||||
} else if output.interrupted {
|
||||
"CANCEL_OR_FENCE_EFFECT_UNKNOWN"
|
||||
} else if output.timed_out {
|
||||
"TIMEOUT_EFFECT_UNKNOWN"
|
||||
} else {
|
||||
@@ -210,6 +217,14 @@ pub struct TrustedClock {
|
||||
/// Local client authenticates the root process as well as the root-owned socket.
|
||||
/// A malicious same-user Unix server must not be able to forge helper success.
|
||||
pub fn invoke(socket: &Path, request: &Request, max_runtime_seconds: u32) -> Result<Outcome> {
|
||||
invoke_controlled(socket, request, max_runtime_seconds, || Ok(()))
|
||||
}
|
||||
pub fn invoke_controlled(
|
||||
socket: &Path,
|
||||
request: &Request,
|
||||
max_runtime_seconds: u32,
|
||||
mut observe: impl FnMut() -> Result<()>,
|
||||
) -> Result<Outcome> {
|
||||
use std::{
|
||||
io::{Read, Write},
|
||||
os::{
|
||||
@@ -256,16 +271,50 @@ pub fn invoke(socket: &Path, request: &Request, max_runtime_seconds: u32) -> Res
|
||||
stream.set_read_timeout(Some(Duration::from_secs(
|
||||
u64::from(max_runtime_seconds) + 10,
|
||||
)))?;
|
||||
observe()?;
|
||||
let bytes = serde_json::to_vec(request)?;
|
||||
ensure!(bytes.len() <= 512 * 1024, "HELPER_REQUEST_SIZE");
|
||||
stream.write_all(&(u32::try_from(bytes.len())?).to_be_bytes())?;
|
||||
stream.write_all(&bytes)?;
|
||||
stream.set_read_timeout(Some(Duration::from_millis(50)))?;
|
||||
let started = std::time::Instant::now();
|
||||
let mut closed = false;
|
||||
let mut read = |stream: &mut UnixStream, mut target: &mut [u8]| -> Result<()> {
|
||||
while !target.is_empty() {
|
||||
if !closed && observe().is_err() {
|
||||
// Half-close only this exact authenticated request stream. The
|
||||
// helper treats its EOF as cancellation; result reads continue.
|
||||
stream.shutdown(std::net::Shutdown::Write)?;
|
||||
closed = true;
|
||||
}
|
||||
ensure!(
|
||||
started.elapsed() < Duration::from_secs(u64::from(max_runtime_seconds) + 10),
|
||||
"HELPER_RESPONSE_TIMEOUT_EFFECT_UNKNOWN"
|
||||
);
|
||||
match stream.read(target) {
|
||||
Ok(0) => anyhow::bail!("HELPER_RESPONSE_CLOSED_EFFECT_UNKNOWN"),
|
||||
Ok(n) => target = &mut target[n..],
|
||||
Err(e)
|
||||
if matches!(
|
||||
e.kind(),
|
||||
std::io::ErrorKind::WouldBlock
|
||||
| std::io::ErrorKind::TimedOut
|
||||
| std::io::ErrorKind::Interrupted
|
||||
) =>
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
let mut length = [0; 4];
|
||||
stream.read_exact(&mut length)?;
|
||||
read(&mut stream, &mut length)?;
|
||||
let length = u32::from_be_bytes(length) as usize;
|
||||
ensure!(length <= 2 * 1024 * 1024, "HELPER_RESPONSE_SIZE");
|
||||
let mut bytes = vec![0; length];
|
||||
stream.read_exact(&mut bytes)?;
|
||||
read(&mut stream, &mut bytes)?;
|
||||
let outcome: Outcome = serde_json::from_slice(&bytes)?;
|
||||
ensure!(
|
||||
outcome.stdout.len() + outcome.stderr.len() <= 262144,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
mod image;
|
||||
use anyhow::{Result, ensure};
|
||||
use rmm_domain::jobs::Action;
|
||||
use rmm_privileged_helper::{
|
||||
@@ -33,6 +34,16 @@ struct Config {
|
||||
allowed_services: Vec<String>,
|
||||
allow_reboot: bool,
|
||||
allow_root_scripts: bool,
|
||||
#[serde(
|
||||
default,
|
||||
deserialize_with = "rmm_domain::jobs::deserialize_script_profiles"
|
||||
)]
|
||||
allowed_script_profiles: Option<Vec<rmm_domain::jobs::PrivilegedScriptProfile>>,
|
||||
#[serde(
|
||||
default,
|
||||
deserialize_with = "rmm_domain::jobs::deserialize_optional_sha256"
|
||||
)]
|
||||
helper_binary_sha256: Option<String>,
|
||||
#[serde(default)]
|
||||
apt_provider: Option<rmm_privileged_helper::apt::ProviderConfig>,
|
||||
}
|
||||
@@ -42,16 +53,28 @@ enum HelperProfile {
|
||||
#[default]
|
||||
General,
|
||||
AptOnly,
|
||||
FullMaintenanceV1,
|
||||
}
|
||||
impl Config {
|
||||
fn general_policy(&self) -> Result<control::Policy> {
|
||||
Ok(control::Policy {
|
||||
schema_version: 1,
|
||||
profile: "general".into(),
|
||||
schema_version: if self.profile == HelperProfile::FullMaintenanceV1 {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
},
|
||||
profile: if self.profile == HelperProfile::FullMaintenanceV1 {
|
||||
"full_maintenance_v1"
|
||||
} else {
|
||||
"general"
|
||||
}
|
||||
.into(),
|
||||
identity: rmm_privileged_helper::readiness::Identity::from_binding(&self.binding)?,
|
||||
allowed_services: self.allowed_services.clone(),
|
||||
allow_root_scripts: self.allow_root_scripts,
|
||||
allow_reboot: self.allow_reboot,
|
||||
allowed_script_profiles: self.allowed_script_profiles.clone(),
|
||||
helper_binary_sha256: self.helper_binary_sha256.clone(),
|
||||
})
|
||||
}
|
||||
fn validate_profile(&self) -> Result<()> {
|
||||
@@ -60,7 +83,19 @@ impl Config {
|
||||
== std::path::Path::new(rmm_privileged_helper::readiness::SHARED_STATE),
|
||||
"SHARED_HELPER_LEDGER_REQUIRED"
|
||||
);
|
||||
if self.profile == HelperProfile::AptOnly {
|
||||
if self.profile == HelperProfile::FullMaintenanceV1 {
|
||||
ensure!(
|
||||
self.apt_provider.is_none()
|
||||
&& self.allowed_services.is_empty()
|
||||
&& !self.allow_reboot
|
||||
&& self.allow_root_scripts
|
||||
&& self.allowed_script_profiles.as_deref()
|
||||
== Some(&[rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1])
|
||||
&& self.socket.as_path()
|
||||
!= std::path::Path::new(rmm_privileged_helper::readiness::SOCKET),
|
||||
"FULL_MAINTENANCE_PROFILE"
|
||||
);
|
||||
} else if self.profile == HelperProfile::AptOnly {
|
||||
ensure!(
|
||||
self.agent_uid == 993
|
||||
&& self.socket.as_path()
|
||||
@@ -79,6 +114,19 @@ impl Config {
|
||||
"GENERAL_HELPER_CANNOT_RUN_APT"
|
||||
);
|
||||
}
|
||||
ensure!(
|
||||
self.profile == HelperProfile::FullMaintenanceV1
|
||||
|| self.allowed_script_profiles.is_none(),
|
||||
"FULL_MAINTENANCE_EXPLICIT_PROFILE_REQUIRED"
|
||||
);
|
||||
ensure!(
|
||||
if self.profile == HelperProfile::FullMaintenanceV1 {
|
||||
self.helper_binary_sha256.is_some()
|
||||
} else {
|
||||
self.helper_binary_sha256.is_none()
|
||||
},
|
||||
"FULL_MAINTENANCE_BINARY_PIN_REQUIRED"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -138,9 +186,21 @@ fn policy(config: &Config, job: &rmm_domain::jobs::JobPayload) -> Result<()> {
|
||||
config.profile != HelperProfile::AptOnly,
|
||||
"APT_ONLY_CROSS_ACTION_DENIED"
|
||||
);
|
||||
ensure!(
|
||||
config.profile != HelperProfile::FullMaintenanceV1 || job.action.full_maintenance(),
|
||||
"FULL_MAINTENANCE_ONLY_SCRIPT"
|
||||
);
|
||||
match &job.action {
|
||||
Action::RunScript { interpreter, .. } => {
|
||||
ensure!(config.allow_root_scripts, "ROOT_SCRIPT_DENIED");
|
||||
ensure!(
|
||||
job.action.full_maintenance()
|
||||
== (config.profile == HelperProfile::FullMaintenanceV1)
|
||||
&& job.action.valid_execution_identity(&job.execution_identity)
|
||||
&& (!job.action.full_maintenance()
|
||||
|| (job.max_runtime_seconds <= 300 && job.max_output_bytes <= 65536)),
|
||||
"ROOT_SCRIPT_PROFILE_DENIED"
|
||||
);
|
||||
let available = match interpreter.as_str() {
|
||||
"posix_sh" => rmm_agent_linux::actions::Interpreter::PosixSh.available(),
|
||||
"bash" => rmm_agent_linux::actions::Interpreter::Bash.available(),
|
||||
@@ -267,6 +327,7 @@ fn handle(
|
||||
clock: &rmm_privileged_helper::TrustedClock,
|
||||
sync: &mut reconcile::HelperSync,
|
||||
sync_clock: &reconcile::NativeClock,
|
||||
mut image: Option<&mut image::RunningImage>,
|
||||
) -> Result<()> {
|
||||
use rusqlite::OptionalExtension;
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
@@ -278,6 +339,11 @@ fn handle(
|
||||
ensure!(size <= 8 * 1024 * 1024, "REQUEST_SIZE");
|
||||
let mut bytes = vec![0; size];
|
||||
stream.read_exact(&mut bytes)?;
|
||||
let binary_sha256 = image.as_mut().map(|i| i.verify()).transpose()?;
|
||||
ensure!(
|
||||
binary_sha256 == config.helper_binary_sha256,
|
||||
"RUNNING_HELPER_BINARY_CHANGED"
|
||||
);
|
||||
let request: Request = match serde_json::from_slice::<Incoming>(&bytes)? {
|
||||
Incoming::Job(request) => {
|
||||
ensure!(size <= 512 * 1024, "JOB_REQUEST_SIZE");
|
||||
@@ -328,11 +394,11 @@ fn handle(
|
||||
unlocked = applied.unlocked_grants;
|
||||
}
|
||||
control::Request::Policy { .. } => ensure!(
|
||||
config.profile == HelperProfile::General,
|
||||
config.profile != HelperProfile::AptOnly,
|
||||
"GENERAL_POLICY_PROFILE"
|
||||
),
|
||||
}
|
||||
let general = config.profile == HelperProfile::General;
|
||||
let general = config.profile != HelperProfile::AptOnly;
|
||||
let policy = if general {
|
||||
Some(config.general_policy()?)
|
||||
} else {
|
||||
@@ -355,7 +421,14 @@ fn handle(
|
||||
schema_version: 1,
|
||||
challenge,
|
||||
identity: identity.clone(),
|
||||
profile: if general { "general" } else { "apt_only" }.into(),
|
||||
profile: if config.profile == HelperProfile::FullMaintenanceV1 {
|
||||
"full_maintenance_v1"
|
||||
} else if general {
|
||||
"general"
|
||||
} else {
|
||||
"apt_only"
|
||||
}
|
||||
.into(),
|
||||
policy_sha256,
|
||||
policy,
|
||||
snapshot,
|
||||
@@ -364,6 +437,7 @@ fn handle(
|
||||
ledger_inode: ledger.ino(),
|
||||
lock_device: lock.dev(),
|
||||
lock_inode: lock.ino(),
|
||||
helper_binary_sha256: binary_sha256.clone(),
|
||||
};
|
||||
let bytes = serde_json::to_vec(&response)?;
|
||||
ensure!(bytes.len() <= 8 * 1024 * 1024, "CONTROL_RESPONSE_LIMIT");
|
||||
@@ -441,6 +515,10 @@ fn handle(
|
||||
if let Some(apt) = &apt {
|
||||
apt.verify_retry_history(db)?;
|
||||
}
|
||||
ensure!(
|
||||
image.as_mut().map(|i| i.verify()).transpose()? == config.helper_binary_sha256,
|
||||
"RUNNING_HELPER_BINARY_CHANGED"
|
||||
);
|
||||
rmm_privileged_helper::begin_execution(db, &permit, &request)?;
|
||||
let mut outcome =
|
||||
if !clock.trusted() || config.binding.verify(&request, chrono::Utc::now()).is_err() {
|
||||
@@ -454,7 +532,31 @@ fn handle(
|
||||
apt.execute(db)
|
||||
.unwrap_or_else(|_| Outcome::unknown("APT_HELPER_EFFECT_UNKNOWN"))
|
||||
} else {
|
||||
execute(&job).unwrap_or_else(|_| Outcome::unknown("HELPER_EFFECT_UNKNOWN"))
|
||||
if job.action.full_maintenance() {
|
||||
rmm_privileged_helper::script_controlled(&job, || {
|
||||
ensure!(clock.trusted(), "CLOCK_UNTRUSTED");
|
||||
peer_allowed(stream, config.agent_uid)?;
|
||||
let mut byte = 0u8;
|
||||
let read = unsafe {
|
||||
libc::recv(
|
||||
stream.as_raw_fd(),
|
||||
(&mut byte as *mut u8).cast(),
|
||||
1,
|
||||
libc::MSG_PEEK | libc::MSG_DONTWAIT,
|
||||
)
|
||||
};
|
||||
ensure!(
|
||||
read < 0
|
||||
&& std::io::Error::last_os_error().kind()
|
||||
== std::io::ErrorKind::WouldBlock,
|
||||
"SCRIPT_CALLER_CANCELLED_OR_DISCONNECTED"
|
||||
);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap_or_else(|_| Outcome::unknown("HELPER_SCRIPT_EFFECT_UNKNOWN"))
|
||||
} else {
|
||||
execute(&job).unwrap_or_else(|_| Outcome::unknown("HELPER_EFFECT_UNKNOWN"))
|
||||
}
|
||||
}
|
||||
};
|
||||
if rmm_privileged_helper::apt::is_apt_action(&job) {
|
||||
@@ -497,7 +599,7 @@ fn run() -> Result<()> {
|
||||
let args: Vec<_> = std::env::args_os().skip(1).collect();
|
||||
if args == [std::ffi::OsString::from("--help")] {
|
||||
println!(
|
||||
"rmm-privileged-helper [--help|--inspect-general-profile|--inspect-apt-profile|--probe-native|--install-apt-provider]\nRMM_HELPER_CONFIG=/etc/rmm/helper.json (root-owned 0600).\nDaemon mode accepts only authenticated typed IPC; all effects need independent signed Sync and a fresh consumed grant. Administrative install/probe modes are explicit bootstrap actions, never network commands."
|
||||
"rmm-privileged-helper [--help|--inspect-general-profile|--inspect-full-maintenance-profile|--inspect-apt-profile|--probe-native|--install-apt-provider]\nRMM_HELPER_CONFIG=/etc/rmm/helper.json (root-owned 0600).\nDaemon mode accepts only authenticated typed IPC; all effects need independent signed Sync and a fresh consumed grant. Administrative install/probe modes are explicit bootstrap actions, never network commands."
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
@@ -510,7 +612,11 @@ fn run() -> Result<()> {
|
||||
args.is_empty()
|
||||
|| args == [std::ffi::OsString::from("--probe-native")]
|
||||
|| args == [std::ffi::OsString::from("--inspect-apt-profile")]
|
||||
|| args == [std::ffi::OsString::from("--inspect-general-profile")],
|
||||
|| args == [std::ffi::OsString::from("--inspect-general-profile")]
|
||||
|| args
|
||||
== [std::ffi::OsString::from(
|
||||
"--inspect-full-maintenance-profile"
|
||||
)],
|
||||
"UNKNOWN_HELPER_MODE"
|
||||
);
|
||||
let path = PathBuf::from(
|
||||
@@ -533,17 +639,38 @@ fn run() -> Result<()> {
|
||||
config.binding.public_key_hex = key.public_key_hex;
|
||||
}
|
||||
config.validate_profile()?;
|
||||
if args == [std::ffi::OsString::from("--inspect-general-profile")] {
|
||||
let mut running_image = config
|
||||
.helper_binary_sha256
|
||||
.as_deref()
|
||||
.map(image::RunningImage::current)
|
||||
.transpose()?;
|
||||
if args == [std::ffi::OsString::from("--inspect-general-profile")]
|
||||
|| args
|
||||
== [std::ffi::OsString::from(
|
||||
"--inspect-full-maintenance-profile",
|
||||
)]
|
||||
{
|
||||
ensure!(
|
||||
config.profile == HelperProfile::General,
|
||||
(config.profile == HelperProfile::General
|
||||
&& args == [std::ffi::OsString::from("--inspect-general-profile")])
|
||||
|| (config.profile == HelperProfile::FullMaintenanceV1
|
||||
&& args
|
||||
== [std::ffi::OsString::from(
|
||||
"--inspect-full-maintenance-profile"
|
||||
)]),
|
||||
"GENERAL_POLICY_PROFILE"
|
||||
);
|
||||
let policy = config.general_policy()?;
|
||||
let pin = control::GeneralPin {
|
||||
schema_version: 1,
|
||||
profile: "general".into(),
|
||||
schema_version: if config.profile == HelperProfile::FullMaintenanceV1 {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
},
|
||||
profile: policy.profile.clone(),
|
||||
identity: policy.identity.clone(),
|
||||
policy_sha256: policy.sha256()?,
|
||||
helper_binary_sha256: running_image.as_mut().map(|i| i.verify()).transpose()?,
|
||||
};
|
||||
println!("{}", serde_json::to_string(&pin)?);
|
||||
return Ok(());
|
||||
@@ -627,6 +754,7 @@ fn run() -> Result<()> {
|
||||
&clock,
|
||||
&mut sync,
|
||||
&sync_clock,
|
||||
running_image.as_mut(),
|
||||
) {
|
||||
let code = safe_error_code(&error);
|
||||
eprintln!("helper request rejected: {code}");
|
||||
@@ -704,6 +832,34 @@ mod tests {
|
||||
assert!(config.validate_profile().is_err());
|
||||
}
|
||||
#[test]
|
||||
fn full_maintenance_is_a_separate_explicit_profile_and_shared_ledger() {
|
||||
let id = uuid::Uuid::new_v4();
|
||||
let now = chrono::Utc::now();
|
||||
let mut config:Config=serde_json::from_value(serde_json::json!({"profile":"general","binding":{"tenant_id":id,"asset_id":id,"agent_id":id,"enrollment_generation":1,"assignment_version":1,"public_key_hex":"00".repeat(32)},"agent_uid":993,"socket_gid":993,"socket":"/run/rmm-helper-maintenance/control.sock","state":rmm_privileged_helper::readiness::SHARED_STATE,"allowed_services":[],"allow_reboot":false,"allow_root_scripts":true})).unwrap();
|
||||
config.validate_profile().unwrap();
|
||||
let mut job:rmm_domain::jobs::JobPayload=serde_json::from_value(serde_json::json!({"schema_version":1,"tenant_id":id,"asset_id":id,"agent_id":id,"enrollment_generation":1,"assignment_version":1,"job_id":id,"attempt_id":id,"execution_identity":"root","not_before":now,"start_before":now+chrono::Duration::minutes(1),"max_runtime_seconds":30,"max_output_bytes":4096,"action":{"action":"run_script","interpreter":"posix_sh","script_utf8":"id\n","arguments":[],"expected_stdout":null,"privileged_script_profile":"full_maintenance_v1"}})).unwrap();
|
||||
assert!(policy(&config, &job).is_err());
|
||||
config.allowed_script_profiles = Some(vec![
|
||||
rmm_domain::jobs::PrivilegedScriptProfile::FullMaintenanceV1,
|
||||
]);
|
||||
assert!(config.validate_profile().is_err());
|
||||
config.profile = HelperProfile::FullMaintenanceV1;
|
||||
assert!(config.validate_profile().is_err());
|
||||
config.helper_binary_sha256 = Some("ab".repeat(32));
|
||||
config.validate_profile().unwrap();
|
||||
assert!(policy(&config, &job).is_ok());
|
||||
let proof = config.general_policy().unwrap();
|
||||
assert_eq!(proof.schema_version, 2);
|
||||
assert_eq!(proof.profile, "full_maintenance_v1");
|
||||
job.action = Action::SetServiceState {
|
||||
unit: "rmm-mvp-test.service".into(),
|
||||
state: "running".into(),
|
||||
};
|
||||
assert!(policy(&config, &job).is_err());
|
||||
config.state = "/var/lib/another-ledger".into();
|
||||
assert!(config.validate_profile().is_err());
|
||||
}
|
||||
#[test]
|
||||
fn peer_acl_does_not_trust_request_identity() {
|
||||
let (a, _b) = UnixStream::pair().unwrap();
|
||||
// SAFETY: geteuid has no preconditions.
|
||||
@@ -733,6 +889,8 @@ mod tests {
|
||||
allowed_services: vec!["rmm-acceptance.service".into()],
|
||||
allow_reboot: false,
|
||||
allow_root_scripts: false,
|
||||
allowed_script_profiles: None,
|
||||
helper_binary_sha256: None,
|
||||
apt_provider: None,
|
||||
};
|
||||
let now = chrono::Utc::now();
|
||||
@@ -751,6 +909,7 @@ mod tests {
|
||||
arguments: vec![],
|
||||
expected_stdout: None,
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
},
|
||||
execution_identity: "agent_service".into(),
|
||||
not_before: now,
|
||||
@@ -763,6 +922,13 @@ mod tests {
|
||||
assert!(policy(&config, &job).is_err());
|
||||
config.allow_root_scripts = true;
|
||||
assert!(policy(&config, &job).is_ok());
|
||||
// The additive full profile must not alter historical local General
|
||||
// ceilings; the independent central legacy policy remains unchanged.
|
||||
job.max_runtime_seconds = 301;
|
||||
job.max_output_bytes = 65537;
|
||||
assert!(policy(&config, &job).is_ok());
|
||||
job.max_runtime_seconds = 180;
|
||||
job.max_output_bytes = 4096;
|
||||
job.action = Action::SetServiceState {
|
||||
unit: "not-approved.service".into(),
|
||||
state: "running".into(),
|
||||
|
||||
@@ -34,6 +34,7 @@ fn fixture() -> (
|
||||
arguments: vec![],
|
||||
expected_stdout: Some("Hello World\n".into()),
|
||||
windows_execution_profile: None,
|
||||
privileged_script_profile: None,
|
||||
},
|
||||
execution_identity: "agent_service".into(),
|
||||
not_before: now - chrono::Duration::seconds(60),
|
||||
|
||||
Reference in New Issue
Block a user