134 lines
4.8 KiB
Rust
134 lines
4.8 KiB
Rust
//! Exact approved SYSTEM script bytes, created only after helper consumption.
|
|
use crate::helper_ipc::PinnedFile;
|
|
use anyhow::{Result, ensure};
|
|
use rmm_agent_core::auth::Outcome;
|
|
use rmm_domain::jobs::{Action, JobPayload, WindowsExecutionProfile, hash};
|
|
use std::{
|
|
fs::OpenOptions,
|
|
io::Write,
|
|
os::windows::fs::OpenOptionsExt,
|
|
path::Path,
|
|
sync::atomic::AtomicBool,
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
pub(crate) fn execute(
|
|
job: &JobPayload,
|
|
grant: uuid::Uuid,
|
|
helper_sid: &str,
|
|
began: Instant,
|
|
mut observe: impl FnMut(bool) -> Result<()>,
|
|
) -> Result<Outcome> {
|
|
ensure!(
|
|
job.execution_identity == "system"
|
|
&& job.action.full_maintenance()
|
|
&& job.action.valid_execution_identity("system")
|
|
&& job.action.validate()
|
|
&& job
|
|
.action
|
|
.valid_job_limits(job.max_runtime_seconds, job.max_output_bytes),
|
|
"SYSTEM_SCRIPT_PROFILE_REQUIRED"
|
|
);
|
|
let Action::RunScript {
|
|
script_utf8,
|
|
expected_stdout,
|
|
windows_execution_profile,
|
|
..
|
|
} = &job.action
|
|
else {
|
|
anyhow::bail!("SYSTEM_SCRIPT_REQUIRED")
|
|
};
|
|
let directory = Path::new(crate::helper_system::STATE_ROOT);
|
|
let writers = [helper_sid.to_owned()];
|
|
let mut root = PinnedFile::open(directory, true, &[], &writers, false)?;
|
|
let path = directory.join(format!("script-{}-{}.ps1", job.attempt_id, grant));
|
|
let mut file = OpenOptions::new()
|
|
.create_new(true)
|
|
.read(true)
|
|
.write(true)
|
|
.share_mode(windows_sys::Win32::Storage::FileSystem::FILE_SHARE_READ)
|
|
.open(&path)?;
|
|
file.write_all(script_utf8.as_bytes())?;
|
|
file.sync_all()?;
|
|
drop(file);
|
|
struct Cleanup(std::path::PathBuf);
|
|
impl Drop for Cleanup {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_file(&self.0);
|
|
}
|
|
}
|
|
let _cleanup = Cleanup(path.clone());
|
|
let mut pinned = PinnedFile::open(&path, false, &[], &writers, false)?;
|
|
ensure!(
|
|
std::fs::read(&path)? == script_utf8.as_bytes(),
|
|
"SYSTEM_SCRIPT_BYTES_CHANGED"
|
|
);
|
|
let executable = crate::actions::powershell()?;
|
|
let arguments = crate::actions::script_arguments(job, &path)?;
|
|
let timeout = Duration::from_secs(job.max_runtime_seconds.into());
|
|
let remaining = timeout
|
|
.checked_sub(began.elapsed())
|
|
.filter(|d| !d.is_zero())
|
|
.ok_or_else(|| anyhow::anyhow!("SYSTEM_SCRIPT_PREPARATION_TIMEOUT"))?;
|
|
let mut first = true;
|
|
let output = crate::process::run_observed(
|
|
&executable,
|
|
&arguments,
|
|
directory,
|
|
remaining,
|
|
job.max_output_bytes as usize,
|
|
&AtomicBool::new(false),
|
|
|| {
|
|
root.check()?;
|
|
pinned.check()?;
|
|
observe(first)?;
|
|
first = false;
|
|
Ok(())
|
|
},
|
|
)?;
|
|
let expected = expected_stdout
|
|
.as_ref()
|
|
.is_none_or(|s| output.stdout == s.as_bytes());
|
|
let interrupted = output.interrupted || began.elapsed() >= timeout || observe(false).is_err();
|
|
let success = !interrupted && output.exit_code == Some(0) && expected && !output.truncated;
|
|
let mut evidence = serde_json::json!({"postconditions_passed":success,"interpreter":"powershell",
|
|
"script_sha256":hash(script_utf8.as_bytes()),"privileged_script_profile":"full_maintenance_v1",
|
|
"execution_sid":"S-1-5-18","output_encoding":"raw_native_bytes","comparison":"exact_utf8_bytes",
|
|
"execution_policy":"inherited","own_job_object_tree_bounded":true,"full_host_effects_not_contained":true,"full_host_authority":true,
|
|
"cancel_or_fence_observed":output.cancelled,"termination_does_not_prove_absence_of_effects":interrupted});
|
|
if windows_execution_profile == &Some(WindowsExecutionProfile::PowerShell51RemoteSigned) {
|
|
evidence["windows_execution_profile"] = "powershell_51_remote_signed".into();
|
|
evidence["execution_policy"] = "process_remote_signed_requested".into();
|
|
evidence["execution_policy_requested"] = "RemoteSigned".into();
|
|
evidence["execution_policy_scope"] = "process".into();
|
|
}
|
|
Ok(Outcome {
|
|
state: if interrupted {
|
|
"unknown"
|
|
} else if success {
|
|
"succeeded"
|
|
} else {
|
|
"failed"
|
|
}
|
|
.into(),
|
|
stdout: output.stdout,
|
|
stderr: output.stderr,
|
|
exit_code: output.exit_code,
|
|
output_truncated: output.truncated,
|
|
reboot_required: false,
|
|
error_code: if success {
|
|
""
|
|
} else if interrupted {
|
|
"SYSTEM_SCRIPT_INTERRUPTED_EFFECT_UNKNOWN"
|
|
} else if output.truncated {
|
|
"OUTPUT_LIMIT_REACHED"
|
|
} else if !expected {
|
|
"STDOUT_POSTCONDITION_FAILED"
|
|
} else {
|
|
"POWERSHELL_PROCESS_FAILED"
|
|
}
|
|
.into(),
|
|
evidence,
|
|
})
|
|
}
|