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