Add fresh device trust, installer recovery and scoped patch runtime

This commit is contained in:
2026-11-17 16:08:53 +01:00
parent 60f8f21f54
commit 81fcd9c8f8
24 changed files with 543 additions and 45 deletions
+68 -1
View File
@@ -54,6 +54,36 @@ pub enum PrivilegedScriptProfile {
#[serde(rename = "full_maintenance_v1")]
FullMaintenanceV1,
}
/// Bytes of the Windows Update Agent PowerShell template seeded by migration 0023.
pub const WINDOWS_UPDATE_SCRIPT_UTF8: &str = include_str!("windows_update.ps1");
pub const WINDOWS_UPDATE_SCRIPT_SHA256: &str =
"0fa5f513a06b6cf902a3f9ac1989987642c181eedd6c305b3912771bd10f2ebf";
pub const WINDOWS_UPDATE_MAX_SECONDS: u32 = 3600;
pub const WINDOWS_UPDATE_LONG_CAPABILITY: &str = "script.windows_update.long/1";
pub fn valid_windows_update_limit(
limit: Option<u32>,
profiles: Option<&[PrivilegedScriptProfile]>,
) -> bool {
limit.is_none_or(|seconds| {
(121..=WINDOWS_UPDATE_MAX_SECONDS).contains(&seconds)
&& profiles == Some(&[PrivilegedScriptProfile::FullMaintenanceV1][..])
})
}
/// Only the built-in template's final ASCII JSON object describes the host's
/// restart state. Earlier lines can include per-update results and native titles.
pub fn windows_update_reboot_report(stdout: &[u8]) -> Option<bool> {
let summary = stdout
.rsplit(|byte| *byte == b'\n')
.map(<[u8]>::trim_ascii)
.find(|line| !line.is_empty())?;
let json: serde_json::Value = serde_json::from_slice(summary).ok()?;
let obj = json.as_object()?;
let reboot = obj.get("reboot_required")?.as_bool()?;
(obj.len() == 2
&& (obj.get("found").is_some_and(|v| v.is_u64())
|| (reboot && obj.get("status").and_then(|v| v.as_str()) == Some("restart_pending"))))
.then_some(reboot)
}
pub const FULL_MAINTENANCE_CAPABILITY: &str = "script.full_maintenance/1";
pub fn deserialize_privileged_script_profile<'de, D>(
d: D,
@@ -387,6 +417,20 @@ impl Action {
}
)
}
pub fn is_windows_update_template(&self) -> bool {
matches!(self, Self::RunScript {
interpreter, script_utf8, arguments, expected_stdout: None,
windows_execution_profile: Some(WindowsExecutionProfile::PowerShell51RemoteSigned),
privileged_script_profile: Some(PrivilegedScriptProfile::FullMaintenanceV1),
} if interpreter == "powershell" && arguments.is_empty() && script_utf8 == WINDOWS_UPDATE_SCRIPT_UTF8)
}
pub fn windows_system_runtime_limit(&self, normal: u32, windows_update: Option<u32>) -> u32 {
if self.is_windows_update_template() {
windows_update.unwrap_or(normal)
} else {
normal
}
}
pub fn valid_execution_identity(&self, identity: &str) -> bool {
if let Self::UpdateAgent { update } = self {
return update.execution_identity() == Some(identity);
@@ -410,7 +454,7 @@ impl Action {
|| (output_bytes <= 65536
&& runtime_seconds
<= if self.script_platform() == Some("windows") {
120
self.windows_system_runtime_limit(120, Some(WINDOWS_UPDATE_MAX_SECONDS))
} else {
300
}))
@@ -779,6 +823,29 @@ impl JobState {
mod tests {
use super::*;
#[test]
fn windows_update_final_report_and_policy_are_exact() {
assert_eq!(
windows_update_reboot_report(
b"ignored \xff\r\n{\"found\":1,\"reboot_required\":true}\r\n"
),
Some(true)
);
assert_eq!(
windows_update_reboot_report(br#"{"reboot_required":false,"found":0}"#),
Some(false)
);
assert_eq!(
windows_update_reboot_report(br#"{"reboot_required":true,"status":"restart_pending"}"#),
Some(true)
);
assert_eq!(
windows_update_reboot_report(br#"{"id":"an update","reboot_required":true}"#),
None
);
assert_eq!(windows_update_reboot_report(b"partial"), None);
assert!(!valid_windows_update_limit(Some(3600), None));
}
#[test]
fn windows_system_scm_is_explicit_and_preserves_linux_service_bytes() {
let caps: Vec<String> = [
"windows.base/1",