Bind approved Windows PowerShell process policy and prove Linux-first contracts
Rust development slice / linux (push) Failing after 6s

This commit is contained in:
2026-11-14 10:06:10 +01:00
parent 8a315706ea
commit abea4eb4d9
28 changed files with 1275 additions and 32 deletions
+143 -13
View File
@@ -2,7 +2,7 @@
//! consumption are supplied by the shared core and the runtime, never bypassed.
use anyhow::{Result, ensure};
use rmm_agent_core::auth::Outcome;
use rmm_domain::jobs::{Action, JobPayload, hash};
use rmm_domain::jobs::{Action, JobPayload, WindowsExecutionProfile, hash};
use std::{
ffi::OsString,
fs::{File, OpenOptions},
@@ -11,6 +11,41 @@ use std::{
path::{Path, PathBuf},
sync::atomic::AtomicBool,
};
/// Implemented contract name only. Runtime publication still requires its own
/// native clock/boot/job qualification; this constant grants no capability.
pub const REMOTE_SIGNED_PROFILE_CAPABILITY: &str = "script.powershell.process_remote_signed/1";
fn script_arguments(job: &JobPayload, path: &Path) -> Result<Vec<OsString>> {
let Action::RunScript {
interpreter,
arguments,
windows_execution_profile,
..
} = &job.action
else {
anyhow::bail!("SCRIPT_REQUIRED")
};
ensure!(
job.execution_identity == "agent_service"
&& interpreter == "powershell"
&& job.action.validate(),
"WINDOWS_SCRIPT_PROFILE_BINDING"
);
let mut args: Vec<OsString> = ["-NoLogo", "-NoProfile", "-NonInteractive"]
.into_iter()
.map(OsString::from)
.collect();
if let Some(WindowsExecutionProfile::PowerShell51RemoteSigned) = windows_execution_profile {
args.extend([
OsString::from("-ExecutionPolicy"),
OsString::from("RemoteSigned"),
]);
}
args.push(OsString::from("-File"));
args.push(path.as_os_str().into());
args.extend(arguments.iter().map(OsString::from));
Ok(args)
}
use windows_sys::Win32::{
Storage::FileSystem::{
FILE_ATTRIBUTE_REPARSE_POINT, FILE_FLAG_OPEN_REPARSE_POINT, FILE_SHARE_READ,
@@ -47,6 +82,7 @@ pub struct Script {
_file: File,
cwd: PathBuf,
arguments: Vec<OsString>,
profile: Option<WindowsExecutionProfile>,
}
impl Script {
/// Preparation creates only a protected copy of approved bytes, no child.
@@ -56,7 +92,7 @@ impl Script {
let Action::RunScript {
interpreter,
script_utf8,
arguments,
windows_execution_profile,
..
} = &job.action
else {
@@ -95,18 +131,14 @@ impl Script {
hash(&std::fs::read(&path)?) == hash(script_utf8.as_bytes()),
"SCRIPT_BYTES_CHANGED"
);
let mut args: Vec<OsString> = ["-NoLogo", "-NoProfile", "-NonInteractive", "-File"]
.into_iter()
.map(OsString::from)
.collect();
args.push(path.clone().into_os_string());
args.extend(arguments.iter().map(OsString::from));
let args = script_arguments(job, &path)?;
Ok(Self {
executable: powershell()?,
path,
_file: file,
cwd: directory.to_path_buf(),
arguments: args,
profile: *windows_execution_profile,
})
}
pub fn run(self, job: &JobPayload, cancel: &AtomicBool) -> Result<Outcome> {
@@ -115,6 +147,7 @@ impl Script {
let Action::RunScript {
script_utf8,
expected_stdout,
windows_execution_profile,
..
} = &job.action
else {
@@ -124,6 +157,11 @@ impl Script {
std::fs::read(&self.path)? == script_utf8.as_bytes(),
"SCRIPT_BYTES_CHANGED"
);
ensure!(
self.profile == *windows_execution_profile
&& self.arguments == script_arguments(job, &self.path)?,
"WINDOWS_SCRIPT_PROFILE_CHANGED"
);
let output = crate::process::run(
&self.executable,
&self.arguments,
@@ -159,6 +197,17 @@ impl Script {
} else {
""
};
let mut evidence = serde_json::json!({"postconditions_passed":success,"interpreter":"powershell",
"script_sha256":hash(script_utf8.as_bytes()),"execution_sid":"S-1-5-19",
"output_encoding":"raw_native_bytes","comparison":"exact_utf8_bytes",
"execution_policy":"inherited","process_tree_bounded":true,
"cancel_requested":output.cancelled,"termination_does_not_prove_absence_of_effects":output.interrupted});
if self.profile.is_some() {
evidence["windows_execution_profile"] = serde_json::to_value(self.profile)?;
evidence["execution_policy"] = serde_json::json!("process_remote_signed_requested");
evidence["execution_policy_requested"] = serde_json::json!("RemoteSigned");
evidence["execution_policy_scope"] = serde_json::json!("process");
}
Ok(Outcome {
state: state.into(),
stdout: output.stdout,
@@ -167,11 +216,7 @@ impl Script {
output_truncated: output.truncated,
reboot_required: false,
error_code: code.into(),
evidence: serde_json::json!({"postconditions_passed":success,"interpreter":"powershell",
"script_sha256":hash(script_utf8.as_bytes()),"execution_sid":"S-1-5-19",
"output_encoding":"raw_native_bytes","comparison":"exact_utf8_bytes",
"execution_policy":"inherited","process_tree_bounded":true,
"cancel_requested":output.cancelled,"termination_does_not_prove_absence_of_effects":output.interrupted}),
evidence,
})
}
}
@@ -200,3 +245,88 @@ pub fn inventory(job: &JobPayload) -> Result<Outcome> {
"observed_at":chrono::Utc::now(),"completeness":"partial","execution_sid":"S-1-5-19"}),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn powershell_policy_is_an_exact_job_profile_before_file_not_a_mutable_default() {
let id = uuid::Uuid::new_v4();
let now = chrono::Utc::now();
let mut job = JobPayload {
schema_version: 1,
tenant_id: id,
asset_id: id,
agent_id: id,
enrollment_generation: 1,
assignment_version: 1,
job_id: id,
attempt_id: id,
action: Action::RunScript {
interpreter: "powershell".into(),
script_utf8: "Write-Output 'fixed'\r\n".into(),
arguments: vec!["-ExecutionPolicy".into(), "Bypass".into()],
expected_stdout: None,
windows_execution_profile: None,
},
execution_identity: "agent_service".into(),
not_before: now,
start_before: now + chrono::Duration::seconds(60),
max_runtime_seconds: 30,
max_output_bytes: 4096,
};
let path = Path::new(r"C:\fixed state\approved.ps1");
let inherited = script_arguments(&job, path).unwrap();
assert_eq!(
inherited,
vec![
"-NoLogo",
"-NoProfile",
"-NonInteractive",
"-File",
r"C:\fixed state\approved.ps1",
"-ExecutionPolicy",
"Bypass"
]
.into_iter()
.map(OsString::from)
.collect::<Vec<_>>()
);
if let Action::RunScript {
windows_execution_profile,
..
} = &mut job.action
{
*windows_execution_profile = Some(WindowsExecutionProfile::PowerShell51RemoteSigned);
}
let args = script_arguments(&job, path).unwrap();
assert_eq!(
args[..6],
[
"-NoLogo",
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy",
"RemoteSigned",
"-File"
]
.map(OsString::from)
);
assert_eq!(args[6], path.as_os_str()); // approved arguments remain AFTER -File
assert_ne!(inherited, args);
let mut unknown = serde_json::to_value(&job.action).unwrap();
unknown["windows_execution_profile"] = serde_json::json!("Bypass");
assert!(serde_json::from_value::<Action>(unknown).is_err());
for identity in ["root", "system"] {
job.execution_identity = identity.into();
assert!(script_arguments(&job, path).is_err());
}
job.execution_identity = "agent_service".into();
for other in ["posix_sh", "bash", "pwsh"] {
if let Action::RunScript { interpreter, .. } = &mut job.action {
*interpreter = other.into();
}
assert!(script_arguments(&job, path).is_err());
}
}
}
+2
View File
@@ -684,6 +684,8 @@ impl Agent {
{
caps.extend(["rmm.sync.v1", "refresh_inventory"].map(str::to_string));
if rmm_agent_windows::actions::powershell().is_ok() {
// The process_remote_signed profile is implemented but remains
// unpublished until its separate native qualification passes.
caps.extend(["run_script", "script.powershell/1"].map(str::to_string));
}
}