Prepare fixed MSI 1.1 major upgrade with honest timeout and noop outcomes
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
//! attempt before start(). Cache population is a separate verified RMM artifact
|
||||
//! operation: this module has no network, URL, UNC, transform or free arguments.
|
||||
//!
|
||||
//! Only first install/exact noop of v1.0.0 is implemented. Upgrade and EXE are
|
||||
//! separate deliveries, not guessed modes of this MSI executor.
|
||||
//! Only fixed1.0 first install/noop and fixed1.0→1.1 major-upgrade/noop.
|
||||
//! No fresh1.1, downgrade, repair, arbitrary predecessor, or EXE mode.
|
||||
//! MSI metadata APIs are synchronous. The caller owns the outer observation
|
||||
//! deadline; a poll timeout is not a promise that Windows Installer stopped.
|
||||
//! Do not put this controller in a kill-on-close process-tree executor.
|
||||
@@ -15,33 +15,80 @@
|
||||
//! https://learn.microsoft.com/en-us/windows/win32/msi/error-codes
|
||||
#[cfg(windows)]
|
||||
use anyhow::{Result, ensure};
|
||||
#[cfg(windows)]
|
||||
use rmm_domain::windows_execution::ResolvedWindowsMsi;
|
||||
#[cfg(any(windows, test))]
|
||||
use rmm_domain::windows_execution::{ResolvedWindowsMsi, valid_package_code};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
|
||||
pub struct Snapshot {
|
||||
pub product_code: String,
|
||||
pub package_code: Option<String>,
|
||||
pub related_products: Vec<String>,
|
||||
pub predecessor_state: Option<String>,
|
||||
pub predecessor_version: Option<String>,
|
||||
pub predecessor_package_code: Option<String>,
|
||||
pub product_state: Option<String>,
|
||||
pub product_version: Option<String>,
|
||||
pub payload_sha256: Option<String>,
|
||||
pub payload_bytes: Option<u64>,
|
||||
}
|
||||
impl Snapshot {
|
||||
#[cfg(windows)]
|
||||
#[cfg(any(windows, test))]
|
||||
fn desired(&self, r: &ResolvedWindowsMsi) -> bool {
|
||||
let p = &r.recipe.postcondition;
|
||||
self.product_state.as_deref() == Some("5")
|
||||
self.product_code == p.product_code
|
||||
&& self.related_products == [p.product_code.clone()]
|
||||
&& self.predecessor_state.is_none()
|
||||
&& self.predecessor_version.is_none()
|
||||
&& self.predecessor_package_code.is_none()
|
||||
&& r.recipe
|
||||
.package_code
|
||||
.as_ref()
|
||||
.is_none_or(|code| self.package_code.as_ref() == Some(code))
|
||||
&& self.product_state.as_deref() == Some("5")
|
||||
&& self.product_version.as_deref() == Some(&p.product_version)
|
||||
&& self.payload_sha256.as_deref() == Some(&p.payload_sha256)
|
||||
&& self.payload_bytes == Some(p.payload_bytes)
|
||||
}
|
||||
#[cfg(any(windows, test))]
|
||||
fn absent(&self) -> bool {
|
||||
self.product_state.is_none()
|
||||
self.related_products.is_empty()
|
||||
&& self.package_code.is_none()
|
||||
&& self.predecessor_state.is_none()
|
||||
&& self.predecessor_version.is_none()
|
||||
&& self.predecessor_package_code.is_none()
|
||||
&& self.product_state.is_none()
|
||||
&& self.product_version.is_none()
|
||||
&& self.payload_sha256.is_none()
|
||||
&& self.payload_bytes.is_none()
|
||||
}
|
||||
#[cfg(any(windows, test))]
|
||||
fn predecessor(&self, r: &ResolvedWindowsMsi) -> bool {
|
||||
let Some(old) = &r.recipe.predecessor else {
|
||||
return false;
|
||||
};
|
||||
self.product_code == r.recipe.postcondition.product_code
|
||||
&& self.related_products == [old.product_code.clone()]
|
||||
&& self.product_state.is_none()
|
||||
&& self.product_version.is_none()
|
||||
&& self.package_code.is_none()
|
||||
&& self.predecessor_state.as_deref() == Some("5")
|
||||
&& self.predecessor_version.as_ref() == Some(&old.product_version)
|
||||
&& self.predecessor_package_code.as_ref().is_some_and(|code| {
|
||||
valid_package_code(code) && Some(code) != r.recipe.package_code.as_ref()
|
||||
})
|
||||
&& self.payload_sha256.as_ref() == Some(&old.payload_sha256)
|
||||
&& self.payload_bytes == Some(old.payload_bytes)
|
||||
}
|
||||
#[cfg(any(windows, test))]
|
||||
fn baseline_allowed(&self, r: &ResolvedWindowsMsi) -> bool {
|
||||
self.desired(r)
|
||||
|| if r.recipe.predecessor.is_some() {
|
||||
self.predecessor(r)
|
||||
} else {
|
||||
self.absent()
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Observation {
|
||||
@@ -93,6 +140,94 @@ fn classification(
|
||||
_ => ("unknown", "MSI_FAILURE_EFFECT_UNRESOLVED", None, false),
|
||||
}
|
||||
}
|
||||
#[cfg(any(windows, test))]
|
||||
fn within_budget(began: std::time::Instant, timeout: std::time::Duration) -> bool {
|
||||
began.elapsed() < timeout
|
||||
}
|
||||
/// Classify only evidence actually observed inside the original budget. An
|
||||
/// installer may already have changed the machine when a late poll sees exit0;
|
||||
/// late evidence cannot retroactively turn that timed-out attempt into success.
|
||||
#[cfg(any(windows, test))]
|
||||
fn observed_classification(
|
||||
exit: u32,
|
||||
postconditions: bool,
|
||||
within_deadline: bool,
|
||||
) -> (&'static str, &'static str, Option<bool>, bool) {
|
||||
let result = classification(exit, postconditions);
|
||||
if within_deadline {
|
||||
result
|
||||
} else {
|
||||
(
|
||||
"unknown",
|
||||
"MSI_TIMEOUT_EFFECT_UNRESOLVED",
|
||||
result.2,
|
||||
result.3,
|
||||
)
|
||||
}
|
||||
}
|
||||
#[cfg(any(windows, test))]
|
||||
fn expected_upgrade_rows() -> Vec<Vec<Option<String>>> {
|
||||
use rmm_domain::windows_execution::*;
|
||||
let row = |min: &str, max: Option<&str>, attributes: &str, property: &str| {
|
||||
vec![
|
||||
Some(MSI_UPGRADE_CODE.into()),
|
||||
Some(min.into()),
|
||||
max.map(str::to_owned),
|
||||
None,
|
||||
Some(attributes.into()),
|
||||
Some("ALL".into()),
|
||||
Some(property.into()),
|
||||
]
|
||||
};
|
||||
// Both lists must contain exactly the one approved ProductCode in the
|
||||
// LaunchCondition. OnlyDetect never removes an unexpected family member.
|
||||
vec![
|
||||
row("0.0.0", None, "258", MSI_ALL_PRODUCTS),
|
||||
row(MSI_VERSION, Some(MSI_VERSION), "768", MSI_OLD_PRODUCTS),
|
||||
]
|
||||
}
|
||||
#[cfg(any(windows, test))]
|
||||
fn expected_sequence_rows(upgrade: bool) -> Vec<Vec<Option<String>>> {
|
||||
let mut rows = vec![
|
||||
("LaunchConditions", 100),
|
||||
("CostInitialize", 800),
|
||||
("FileCost", 900),
|
||||
("CostFinalize", 1000),
|
||||
("InstallValidate", 1400),
|
||||
("InstallInitialize", 1500),
|
||||
("ProcessComponents", 1600),
|
||||
("UnpublishFeatures", 1800),
|
||||
("RemoveFiles", 3500),
|
||||
("RemoveFolders", 3600),
|
||||
("CreateFolders", 3700),
|
||||
("InstallFiles", 4000),
|
||||
("RegisterUser", 6000),
|
||||
("RegisterProduct", 6100),
|
||||
("PublishFeatures", 6300),
|
||||
("PublishProduct", 6400),
|
||||
("InstallFinalize", 6600),
|
||||
];
|
||||
if upgrade {
|
||||
rows.extend([
|
||||
("FindRelatedProducts", 25),
|
||||
("RemoveExistingProducts", 1550),
|
||||
]);
|
||||
}
|
||||
rows.sort_by_key(|r| r.1);
|
||||
rows.into_iter()
|
||||
.map(|(action, seq)| {
|
||||
vec![
|
||||
Some(action.into()),
|
||||
if action == "RemoveExistingProducts" {
|
||||
Some("NOT Installed".into())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
Some(seq.to_string()),
|
||||
]
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
/// Contract and locally protected policy must agree, independently of the job.
|
||||
/// The policy hash must come from the helper's protected settings, never a field
|
||||
/// copied back out of the job being checked.
|
||||
@@ -108,7 +243,7 @@ fn validate(r: &ResolvedWindowsMsi, allowed_recipe_sha256: &str) -> Result<()> {
|
||||
#[cfg(windows)]
|
||||
mod windows {
|
||||
use super::*;
|
||||
use rmm_domain::windows_execution::{MSI_PAYLOAD, MSI_PRODUCT_CODE};
|
||||
use rmm_domain::windows_execution::*;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{
|
||||
fs::{File, OpenOptions},
|
||||
@@ -352,6 +487,106 @@ mod windows {
|
||||
ensure!(code == ERROR_NO_MORE_ITEMS, "MSI_DATABASE_NOT_UNIQUE");
|
||||
Ok(String::from_utf16(&text[..len as usize])?)
|
||||
}
|
||||
fn msi_rows(db: u32, query: &str, fields: u32) -> Result<Vec<Vec<Option<String>>>> {
|
||||
let mut view = 0;
|
||||
ensure!(
|
||||
unsafe { MsiDatabaseOpenViewW(db, wide(query).as_ptr(), &mut view) } == 0,
|
||||
"MSI_ROWS_VIEW"
|
||||
);
|
||||
let view = Msi(view);
|
||||
ensure!(
|
||||
unsafe { MsiViewExecute(view.0, 0) } == 0,
|
||||
"MSI_ROWS_EXECUTE"
|
||||
);
|
||||
let mut rows = Vec::new();
|
||||
loop {
|
||||
let mut record = 0;
|
||||
let code = unsafe { MsiViewFetch(view.0, &mut record) };
|
||||
let record = Msi(record);
|
||||
if code == ERROR_NO_MORE_ITEMS {
|
||||
return Ok(rows);
|
||||
}
|
||||
ensure!(
|
||||
code == 0
|
||||
&& rows.len() < 64
|
||||
&& unsafe { MsiRecordGetFieldCount(record.0) } == fields,
|
||||
"MSI_ROWS_BOUND"
|
||||
);
|
||||
let mut row = Vec::new();
|
||||
for field in 1..=fields {
|
||||
if unsafe { MsiRecordIsNull(record.0, field) } != 0 {
|
||||
row.push(None);
|
||||
continue;
|
||||
}
|
||||
let mut text = [0u16; 256];
|
||||
let mut count = 255;
|
||||
ensure!(
|
||||
unsafe { MsiRecordGetStringW(record.0, field, text.as_mut_ptr(), &mut count) }
|
||||
== 0
|
||||
&& count < 256,
|
||||
"MSI_ROW_STRING"
|
||||
);
|
||||
row.push(Some(String::from_utf16(&text[..count as usize])?));
|
||||
}
|
||||
rows.push(row);
|
||||
}
|
||||
}
|
||||
fn inspect_upgrade(db: u32, r: &ResolvedWindowsMsi) -> Result<()> {
|
||||
let upgrade = r.recipe.predecessor.is_some();
|
||||
let tables = msi_rows(db, "SELECT `Name` FROM `_Tables` WHERE `Name`='Upgrade'", 1)?;
|
||||
if upgrade {
|
||||
ensure!(
|
||||
tables == vec![vec![Some("Upgrade".into())]],
|
||||
"MSI_UPGRADE_TABLE_REQUIRED"
|
||||
);
|
||||
ensure!(
|
||||
msi_rows(
|
||||
db,
|
||||
"SELECT `UpgradeCode`,`VersionMin`,`VersionMax`,`Language`,`Attributes`,`Remove`,`ActionProperty` FROM `Upgrade` ORDER BY `ActionProperty`",
|
||||
7
|
||||
)? == expected_upgrade_rows(),
|
||||
"MSI_EXACT_UPGRADE_TABLE_REQUIRED"
|
||||
);
|
||||
ensure!(
|
||||
msi_text(
|
||||
db,
|
||||
"SELECT `Value` FROM `Property` WHERE `Property`='SecureCustomProperties'"
|
||||
)? == format!("{MSI_ALL_PRODUCTS};{MSI_OLD_PRODUCTS}"),
|
||||
"MSI_SECURE_UPGRADE_PROPERTIES"
|
||||
);
|
||||
ensure!(
|
||||
msi_rows(
|
||||
db,
|
||||
"SELECT `Condition`,`Description` FROM `LaunchCondition` ORDER BY `Condition`",
|
||||
2
|
||||
)? == vec![
|
||||
vec![
|
||||
Some(MSI_UPGRADE_CONDITION.into()),
|
||||
Some(
|
||||
"Only the exact approved fixture1.0 predecessor may be upgraded."
|
||||
.into()
|
||||
)
|
||||
],
|
||||
vec![
|
||||
Some("VersionNT64".into()),
|
||||
Some("This fixture requires 64-bit Windows.".into())
|
||||
]
|
||||
],
|
||||
"MSI_EXACT_UPGRADE_LAUNCH_CONDITIONS"
|
||||
);
|
||||
} else {
|
||||
ensure!(tables.is_empty(), "MSI_UNEXPECTED_UPGRADE_TABLE");
|
||||
}
|
||||
ensure!(
|
||||
msi_rows(
|
||||
db,
|
||||
"SELECT `Action`,`Condition`,`Sequence` FROM `InstallExecuteSequence` ORDER BY `Sequence`",
|
||||
3
|
||||
)? == expected_sequence_rows(upgrade),
|
||||
"MSI_EXACT_EXECUTE_SEQUENCE_REQUIRED"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
fn no_custom_actions(db: u32) -> Result<()> {
|
||||
let mut view = 0;
|
||||
ensure!(
|
||||
@@ -409,6 +644,7 @@ mod windows {
|
||||
);
|
||||
let db = Msi(db);
|
||||
no_custom_actions(db.0)?;
|
||||
inspect_upgrade(db.0, r)?;
|
||||
let cabinet = msi_text(db.0, "SELECT `Cabinet` FROM `Media`")?;
|
||||
ensure!(
|
||||
cabinet.starts_with('#')
|
||||
@@ -420,7 +656,7 @@ mod windows {
|
||||
"MSI_EMBEDDED_CABINET_REQUIRED"
|
||||
);
|
||||
for (field, value) in [
|
||||
("ProductCode", MSI_PRODUCT_CODE),
|
||||
("ProductCode", r.recipe.postcondition.product_code.as_str()),
|
||||
(
|
||||
"ProductVersion",
|
||||
r.recipe.postcondition.product_version.as_str(),
|
||||
@@ -467,15 +703,41 @@ mod windows {
|
||||
String::from_utf16(&text[..len as usize])?.starts_with("x64;"),
|
||||
"MSI_NOT_X64"
|
||||
);
|
||||
let mut len = 255;
|
||||
ensure!(
|
||||
unsafe {
|
||||
MsiSummaryInfoGetPropertyW(
|
||||
summary.0,
|
||||
9,
|
||||
&mut kind,
|
||||
&mut value,
|
||||
&mut time,
|
||||
text.as_mut_ptr(),
|
||||
&mut len,
|
||||
)
|
||||
} == 0
|
||||
&& kind == 30
|
||||
&& len < 256,
|
||||
"MSI_PACKAGE_SUMMARY"
|
||||
);
|
||||
let package_code = String::from_utf16(&text[..len as usize])?;
|
||||
ensure!(
|
||||
valid_package_code(&package_code)
|
||||
&& r.recipe
|
||||
.package_code
|
||||
.as_ref()
|
||||
.is_none_or(|code| code == &package_code),
|
||||
"MSI_PACKAGE_CODE_BINDING"
|
||||
);
|
||||
file.check()?;
|
||||
Ok(())
|
||||
}
|
||||
fn property(name: &str) -> Result<Option<String>> {
|
||||
fn property(product: &str, name: &str) -> Result<Option<String>> {
|
||||
let mut value = [0u16; 256];
|
||||
let mut len = 255;
|
||||
let code = unsafe {
|
||||
MsiGetProductInfoExW(
|
||||
wide(MSI_PRODUCT_CODE).as_ptr(),
|
||||
wide(product).as_ptr(),
|
||||
ptr::null(),
|
||||
MSIINSTALLCONTEXT_MACHINE,
|
||||
wide(name).as_ptr(),
|
||||
@@ -489,13 +751,58 @@ mod windows {
|
||||
ensure!(code == 0 && len < 256, "MSI_MACHINE_PRODUCT_QUERY");
|
||||
Ok(Some(String::from_utf16(&value[..len as usize])?))
|
||||
}
|
||||
pub fn snapshot() -> Result<Snapshot> {
|
||||
let product_state = property("State")?;
|
||||
fn snapshot_product(product: &str, predecessor: Option<&str>) -> Result<Snapshot> {
|
||||
let product_state = property(product, "State")?;
|
||||
let product_version = if product_state.as_deref() == Some("5") {
|
||||
property("VersionString")?
|
||||
property(product, "VersionString")?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let package_code = if product_state.as_deref() == Some("5") {
|
||||
property(product, "PackageCode")?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let predecessor_state = predecessor
|
||||
.map(|p| property(p, "State"))
|
||||
.transpose()?
|
||||
.flatten();
|
||||
let predecessor_version = if predecessor_state.as_deref() == Some("5") {
|
||||
property(predecessor.unwrap(), "VersionString")?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let predecessor_package_code = if predecessor_state.as_deref() == Some("5") {
|
||||
property(predecessor.unwrap(), "PackageCode")?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut related_products = Vec::new();
|
||||
for index in 0..=16 {
|
||||
let mut code = [0u16; 39];
|
||||
let status = unsafe {
|
||||
MsiEnumRelatedProductsW(
|
||||
wide(MSI_UPGRADE_CODE).as_ptr(),
|
||||
0,
|
||||
index,
|
||||
code.as_mut_ptr(),
|
||||
)
|
||||
};
|
||||
if status == ERROR_NO_MORE_ITEMS {
|
||||
break;
|
||||
}
|
||||
ensure!(
|
||||
status == ERROR_SUCCESS && index < 16 && code[38] == 0,
|
||||
"MSI_RELATED_PRODUCT_QUERY"
|
||||
);
|
||||
let code = String::from_utf16(&code[..38])?;
|
||||
ensure!(
|
||||
!related_products.contains(&code),
|
||||
"MSI_DUPLICATE_RELATED_PRODUCT"
|
||||
);
|
||||
related_products.push(code);
|
||||
}
|
||||
related_products.sort();
|
||||
let (payload_sha256, payload_bytes) = match std::fs::symlink_metadata(MSI_PAYLOAD) {
|
||||
Ok(_) => {
|
||||
let mut file = Held::open(Path::new(MSI_PAYLOAD), false)?;
|
||||
@@ -506,12 +813,32 @@ mod windows {
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
Ok(Snapshot {
|
||||
product_code: product.into(),
|
||||
package_code,
|
||||
related_products,
|
||||
predecessor_state,
|
||||
predecessor_version,
|
||||
predecessor_package_code,
|
||||
product_state,
|
||||
product_version,
|
||||
payload_sha256,
|
||||
payload_bytes,
|
||||
})
|
||||
}
|
||||
/// Retained1.0 read-only witness. No implicit choice of upgrade recipe.
|
||||
pub fn snapshot() -> Result<Snapshot> {
|
||||
snapshot_product(MSI_PRODUCT_CODE, None)
|
||||
}
|
||||
pub fn snapshot_for(r: &ResolvedWindowsMsi) -> Result<Snapshot> {
|
||||
ensure!(r.validate(), "MSI_RECIPE_INVALID");
|
||||
snapshot_product(
|
||||
&r.recipe.postcondition.product_code,
|
||||
r.recipe
|
||||
.predecessor
|
||||
.as_ref()
|
||||
.map(|old| old.product_code.as_str()),
|
||||
)
|
||||
}
|
||||
fn system_root() -> Result<PathBuf> {
|
||||
let mut value = [0u16; 32768];
|
||||
let len = unsafe { GetSystemWindowsDirectoryW(value.as_mut_ptr(), value.len() as u32) };
|
||||
@@ -547,9 +874,9 @@ mod windows {
|
||||
inspect_archive(&mut archive, r)?;
|
||||
let system_root = system_root()?;
|
||||
let msiexec = Held::open(&system_root.join("System32").join("msiexec.exe"), true)?;
|
||||
let before = snapshot()?;
|
||||
let before = snapshot_for(r)?;
|
||||
ensure!(
|
||||
before.absent() || before.desired(r),
|
||||
before.baseline_allowed(r),
|
||||
"MSI_EXISTING_PRODUCT_OR_PAYLOAD_CONFLICT"
|
||||
);
|
||||
Ok(Self {
|
||||
@@ -561,6 +888,25 @@ mod windows {
|
||||
system_root,
|
||||
})
|
||||
}
|
||||
fn verification_timeout(&self, after: Option<Snapshot>) -> Run {
|
||||
Run::Noop(Box::new(Observation {
|
||||
recipe_sha256: self.recipe.recipe_sha256.clone(),
|
||||
artifact_sha256: self.recipe.recipe.artifact.sha256.clone(),
|
||||
process_id: None,
|
||||
state: "unknown",
|
||||
code: "MSI_VERIFICATION_TIMEOUT_NO_INSTALLER_START",
|
||||
exit_code: None,
|
||||
postconditions_passed: false,
|
||||
already_desired: false,
|
||||
installer_process_started: false,
|
||||
transaction_started: Some(false),
|
||||
installer_reboot_required: None,
|
||||
reboot_initiated: false,
|
||||
before: self.before.clone(),
|
||||
after,
|
||||
automatic_retry: false,
|
||||
}))
|
||||
}
|
||||
/// Called only after durable original-grant consumption. No change of
|
||||
/// config, privilege, policy, clock, reboot or foreign process occurs.
|
||||
pub fn start(
|
||||
@@ -575,10 +921,35 @@ mod windows {
|
||||
&& !cancelled.load(Ordering::SeqCst),
|
||||
"MSI_START_LIMIT_OR_CANCELLED"
|
||||
);
|
||||
inspect_archive(&mut self.archive, &self.recipe)?;
|
||||
self.msiexec.check()?;
|
||||
let before = snapshot()?;
|
||||
// One original budget includes all synchronous verification and
|
||||
// the eventual installer. A late Noop is not retroactive success.
|
||||
let began = Instant::now();
|
||||
let archive = inspect_archive(&mut self.archive, &self.recipe);
|
||||
if !within_budget(began, timeout) {
|
||||
return Ok(self.verification_timeout(None));
|
||||
}
|
||||
archive?;
|
||||
let executable = self.msiexec.check();
|
||||
if !within_budget(began, timeout) {
|
||||
return Ok(self.verification_timeout(None));
|
||||
}
|
||||
executable?;
|
||||
let observation = snapshot_for(&self.recipe);
|
||||
if !within_budget(began, timeout) {
|
||||
return Ok(self.verification_timeout(observation.ok()));
|
||||
}
|
||||
let before = observation?;
|
||||
ensure!(before == self.before, "MSI_BASELINE_CHANGED");
|
||||
// Both a Noop and an installer decision require a fresh caller
|
||||
// authorization fence; no bypass for the already-desired branch.
|
||||
check_effect_fence()?;
|
||||
if !within_budget(began, timeout) {
|
||||
return Ok(self.verification_timeout(Some(before)));
|
||||
}
|
||||
ensure!(
|
||||
!cancelled.load(Ordering::SeqCst),
|
||||
"MSI_CANCELLED_BEFORE_START"
|
||||
);
|
||||
if before.desired(&self.recipe) {
|
||||
return Ok(Run::Noop(Box::new(Observation {
|
||||
recipe_sha256: self.recipe.recipe_sha256.clone(),
|
||||
@@ -598,14 +969,11 @@ mod windows {
|
||||
automatic_retry: false,
|
||||
})));
|
||||
}
|
||||
ensure!(before.absent(), "MSI_INSTALL_BASELINE_CONFLICT");
|
||||
use std::os::windows::process::CommandExt;
|
||||
check_effect_fence()?;
|
||||
ensure!(
|
||||
!cancelled.load(Ordering::SeqCst),
|
||||
"MSI_CANCELLED_BEFORE_START"
|
||||
before.baseline_allowed(&self.recipe),
|
||||
"MSI_INSTALL_BASELINE_CONFLICT"
|
||||
);
|
||||
let began = Instant::now();
|
||||
use std::os::windows::process::CommandExt;
|
||||
let child = Command::new(&self.msiexec.path)
|
||||
.args([
|
||||
std::ffi::OsStr::new("/i"),
|
||||
@@ -657,14 +1025,21 @@ mod windows {
|
||||
};
|
||||
if exit.is_none()
|
||||
&& !cancelled.load(Ordering::SeqCst)
|
||||
&& self.began.elapsed() < self.timeout
|
||||
&& within_budget(self.began, self.timeout)
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
let mut after = None;
|
||||
let (state, error, reboot, initiated) = if let Some(code) = code {
|
||||
after = snapshot().ok();
|
||||
if after.is_none() && matches!(code, 0 | 3010) {
|
||||
// No extra synchronous MSI query once the original budget has
|
||||
// expired. try_wait and snapshot can themselves return late.
|
||||
if within_budget(self.began, self.timeout) {
|
||||
after = snapshot_for(&self.prepared.recipe).ok();
|
||||
}
|
||||
let within_deadline = within_budget(self.began, self.timeout);
|
||||
if !within_deadline {
|
||||
observed_classification(code, false, false)
|
||||
} else if after.is_none() && matches!(code, 0 | 3010) {
|
||||
(
|
||||
"unknown",
|
||||
"MSI_POSTCONDITION_QUERY_FAILED",
|
||||
@@ -672,11 +1047,12 @@ mod windows {
|
||||
false,
|
||||
)
|
||||
} else {
|
||||
classification(
|
||||
observed_classification(
|
||||
code,
|
||||
after
|
||||
.as_ref()
|
||||
.is_some_and(|s| s.desired(&self.prepared.recipe)),
|
||||
within_deadline,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@@ -716,11 +1092,124 @@ mod windows {
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
pub use windows::{Installer, Prepared, Run, snapshot};
|
||||
pub use windows::{Installer, Prepared, Run, snapshot, snapshot_for};
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn upgrade_and_noop_need_independent_exact_products_family_and_payload() {
|
||||
use rmm_domain::windows_execution::*;
|
||||
let recipe = WindowsMsiRecipe {
|
||||
schema_version: 1,
|
||||
recipe_id: MSI_UPGRADE_RECIPE_ID.into(),
|
||||
recipe_version: "1".into(),
|
||||
platform: "windows".into(),
|
||||
upgrade_code: MSI_UPGRADE_CODE.into(),
|
||||
architecture: Architecture::X64,
|
||||
context: InstallContext::Machine,
|
||||
command_profile: MsiCommandProfile::FixtureQuietNoRebootV1,
|
||||
artifact: MsiArtifact {
|
||||
artifact_id: uuid::Uuid::new_v4(),
|
||||
sha256: "ab".repeat(32),
|
||||
bytes: 20480,
|
||||
},
|
||||
postcondition: MsiPostcondition {
|
||||
product_code: MSI_UPGRADE_PRODUCT_CODE.into(),
|
||||
product_version: MSI_UPGRADE_VERSION.into(),
|
||||
payload_path: MSI_PAYLOAD.into(),
|
||||
payload_sha256: MSI_PAYLOAD_11_SHA256.into(),
|
||||
payload_bytes: MSI_PAYLOAD_BYTES,
|
||||
},
|
||||
no_reboot: true,
|
||||
package_code: Some("{8475C4D1-6EE2-4C3A-B41D-D834A20F4135}".into()),
|
||||
predecessor: Some(MsiPostcondition {
|
||||
product_code: MSI_PRODUCT_CODE.into(),
|
||||
product_version: MSI_VERSION.into(),
|
||||
payload_path: MSI_PAYLOAD.into(),
|
||||
payload_sha256: MSI_PAYLOAD_10_SHA256.into(),
|
||||
payload_bytes: MSI_PAYLOAD_BYTES,
|
||||
}),
|
||||
};
|
||||
let r = ResolvedWindowsMsi {
|
||||
recipe_sha256: recipe.sha256().unwrap(),
|
||||
recipe,
|
||||
};
|
||||
assert!(r.validate());
|
||||
let mut before = Snapshot {
|
||||
product_code: MSI_UPGRADE_PRODUCT_CODE.into(),
|
||||
related_products: vec![MSI_PRODUCT_CODE.into()],
|
||||
predecessor_state: Some("5".into()),
|
||||
predecessor_version: Some(MSI_VERSION.into()),
|
||||
predecessor_package_code: Some("{D1C4ECE2-7025-4E16-A842-A20F320CE783}".into()),
|
||||
payload_sha256: Some(MSI_PAYLOAD_10_SHA256.into()),
|
||||
payload_bytes: Some(MSI_PAYLOAD_BYTES),
|
||||
..Snapshot::default()
|
||||
};
|
||||
assert!(before.predecessor(&r));
|
||||
assert!(before.baseline_allowed(&r));
|
||||
assert!(!before.desired(&r));
|
||||
assert!(!Snapshot::default().baseline_allowed(&r)); // no fresh1.1 fallback
|
||||
before
|
||||
.related_products
|
||||
.push("{11111111-1111-4111-8111-111111111111}".into());
|
||||
assert!(!before.baseline_allowed(&r));
|
||||
let mut after = Snapshot {
|
||||
product_code: MSI_UPGRADE_PRODUCT_CODE.into(),
|
||||
package_code: r.recipe.package_code.clone(),
|
||||
related_products: vec![MSI_UPGRADE_PRODUCT_CODE.into()],
|
||||
product_state: Some("5".into()),
|
||||
product_version: Some(MSI_UPGRADE_VERSION.into()),
|
||||
payload_sha256: Some(MSI_PAYLOAD_11_SHA256.into()),
|
||||
payload_bytes: Some(MSI_PAYLOAD_BYTES),
|
||||
..Snapshot::default()
|
||||
};
|
||||
assert!(after.desired(&r));
|
||||
after.predecessor_state = Some("5".into());
|
||||
assert!(!after.desired(&r));
|
||||
after.predecessor_state = None;
|
||||
after.payload_sha256 = Some(MSI_PAYLOAD_10_SHA256.into());
|
||||
assert!(!after.desired(&r));
|
||||
let table = expected_upgrade_rows();
|
||||
assert_eq!(table.len(), 2);
|
||||
assert_eq!(table[0][4].as_deref(), Some("258"));
|
||||
assert_eq!(table[1][4].as_deref(), Some("768"));
|
||||
let sequence = expected_sequence_rows(true);
|
||||
let position = |name: &str| {
|
||||
sequence
|
||||
.iter()
|
||||
.position(|r| r[0].as_deref() == Some(name))
|
||||
.unwrap()
|
||||
};
|
||||
assert!(position("FindRelatedProducts") < position("LaunchConditions"));
|
||||
assert!(position("InstallInitialize") < position("RemoveExistingProducts"));
|
||||
assert!(position("RemoveExistingProducts") < position("ProcessComponents"));
|
||||
}
|
||||
#[test]
|
||||
fn late_exit_or_postcondition_cannot_retroactively_succeed_or_retry() {
|
||||
use std::time::{Duration, Instant};
|
||||
let original = Instant::now().checked_sub(Duration::from_secs(2)).unwrap();
|
||||
let timely = within_budget(Instant::now(), Duration::from_secs(60));
|
||||
let late = within_budget(original, Duration::from_secs(1));
|
||||
assert!(timely);
|
||||
assert!(!late);
|
||||
// This same budget gates the Noop decision and the installer poll;
|
||||
// metadata returning late does not receive a fresh timer.
|
||||
assert_eq!(observed_classification(0, true, late).0, "unknown");
|
||||
for exit in [0, 3010, 1618, 1603, 1641] {
|
||||
let late = observed_classification(exit, true, false);
|
||||
assert_eq!(
|
||||
(late.0, late.1),
|
||||
("unknown", "MSI_TIMEOUT_EFFECT_UNRESOLVED")
|
||||
);
|
||||
assert_eq!(
|
||||
(late.2, late.3),
|
||||
(classification(exit, true).2, classification(exit, true).3)
|
||||
);
|
||||
}
|
||||
assert_eq!(observed_classification(0, true, true).0, "succeeded");
|
||||
assert_eq!(observed_classification(0, false, true).0, "failed");
|
||||
}
|
||||
#[test]
|
||||
fn exit_code_never_substitutes_for_independent_postconditions_or_reboot_authority() {
|
||||
assert_eq!(
|
||||
classification(0, true),
|
||||
@@ -752,6 +1241,7 @@ mod tests {
|
||||
product_version: None,
|
||||
payload_sha256: None,
|
||||
payload_bytes: None,
|
||||
..Snapshot::default()
|
||||
};
|
||||
assert!(!advertised.absent());
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
//! Unexported Windows MSI fixture contract. No legacy Linux codec is changed.
|
||||
//! This version supports first installation or independently proven exact noop;
|
||||
//! an upgrade requires a separate reviewed recipe with explicit predecessor
|
||||
//! products/UpgradeCode semantics. No implicit REINSTALL flags are inferred.
|
||||
//! Fixed1.0 first installation/noop and separate1.0→1.1 major-upgrade/noop
|
||||
//! recipes only. No implicit REINSTALL, repair, downgrade or fresh1.1 mode.
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use uuid::Uuid;
|
||||
@@ -11,6 +10,17 @@ pub const MSI_PRODUCT_CODE: &str = "{3F070B4B-9C08-4D65-BD41-13D81B884AEC}";
|
||||
pub const MSI_UPGRADE_CODE: &str = "{C9BF1FB8-E31D-4BD7-BA0D-AB46539BBBAA}";
|
||||
pub const MSI_VERSION: &str = "1.0.0";
|
||||
pub const MSI_PAYLOAD: &str = r"C:\Program Files\RustDiscoveryFixtures\msi-payload.txt";
|
||||
pub const MSI_UPGRADE_RECIPE_ID: &str = "rmm-windows-msi-fixture-upgrade-1.1";
|
||||
pub const MSI_UPGRADE_PRODUCT_CODE: &str = "{D42BD387-079B-4BBF-A134-0DA7B7A97011}";
|
||||
pub const MSI_UPGRADE_VERSION: &str = "1.1.0";
|
||||
pub const MSI_PAYLOAD_BYTES: u64 = 33;
|
||||
pub const MSI_PAYLOAD_10_SHA256: &str =
|
||||
"0c90c4fc7233017cc88c0e925095d6662ac57abdb303c7527ef59d02b454fca0";
|
||||
pub const MSI_PAYLOAD_11_SHA256: &str =
|
||||
"ba0c83deff9eed014009bcd45369b2a356ae909ebf0c1434dc836b0e6d759c49";
|
||||
pub const MSI_OLD_PRODUCTS: &str = "RMM_OLD_PRODUCTS";
|
||||
pub const MSI_ALL_PRODUCTS: &str = "RMM_ALL_PRODUCTS";
|
||||
pub const MSI_UPGRADE_CONDITION: &str = "Installed OR (RMM_OLD_PRODUCTS = \"{3F070B4B-9C08-4D65-BD41-13D81B884AEC}\" AND RMM_ALL_PRODUCTS = \"{3F070B4B-9C08-4D65-BD41-13D81B884AEC}\")";
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Architecture {
|
||||
@@ -56,6 +66,11 @@ pub struct WindowsMsiRecipe {
|
||||
pub artifact: MsiArtifact,
|
||||
pub postcondition: MsiPostcondition,
|
||||
pub no_reboot: bool,
|
||||
// Appended opt-in metadata: old1.0 recipes retain exactly their old bytes.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub package_code: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub predecessor: Option<MsiPostcondition>,
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
@@ -70,10 +85,16 @@ pub fn digest(value: &str) -> bool {
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
|
||||
}
|
||||
pub fn valid_package_code(value: &str) -> bool {
|
||||
Uuid::parse_str(value).is_ok_and(|id| {
|
||||
!id.is_nil()
|
||||
&& value == format!("{{{}}}", id.to_string().to_uppercase())
|
||||
&& ![MSI_PRODUCT_CODE, MSI_UPGRADE_PRODUCT_CODE, MSI_UPGRADE_CODE].contains(&value)
|
||||
})
|
||||
}
|
||||
impl WindowsMsiRecipe {
|
||||
pub fn validate(&self) -> bool {
|
||||
self.schema_version == 1
|
||||
&& self.recipe_id == MSI_RECIPE_ID
|
||||
&& self.recipe_version == "1"
|
||||
&& self.platform == "windows"
|
||||
&& self.upgrade_code == MSI_UPGRADE_CODE
|
||||
@@ -81,11 +102,32 @@ impl WindowsMsiRecipe {
|
||||
&& !self.artifact.artifact_id.is_nil()
|
||||
&& digest(&self.artifact.sha256)
|
||||
&& (1..=262144).contains(&self.artifact.bytes)
|
||||
&& self.postcondition.product_code == MSI_PRODUCT_CODE
|
||||
&& self.postcondition.product_version == MSI_VERSION
|
||||
&& self.postcondition.payload_path == MSI_PAYLOAD
|
||||
&& digest(&self.postcondition.payload_sha256)
|
||||
&& (1..=65536).contains(&self.postcondition.payload_bytes)
|
||||
&& match self.recipe_id.as_str() {
|
||||
MSI_RECIPE_ID => {
|
||||
self.postcondition.product_code == MSI_PRODUCT_CODE
|
||||
&& self.postcondition.product_version == MSI_VERSION
|
||||
&& self.package_code.is_none()
|
||||
&& self.predecessor.is_none()
|
||||
}
|
||||
MSI_UPGRADE_RECIPE_ID => {
|
||||
self.postcondition.product_code == MSI_UPGRADE_PRODUCT_CODE
|
||||
&& self.postcondition.product_version == MSI_UPGRADE_VERSION
|
||||
&& self.postcondition.payload_sha256 == MSI_PAYLOAD_11_SHA256
|
||||
&& self.postcondition.payload_bytes == MSI_PAYLOAD_BYTES
|
||||
&& self.package_code.as_deref().is_some_and(valid_package_code)
|
||||
&& self.predecessor.as_ref().is_some_and(|old| {
|
||||
old.product_code == MSI_PRODUCT_CODE
|
||||
&& old.product_version == MSI_VERSION
|
||||
&& old.payload_path == MSI_PAYLOAD
|
||||
&& old.payload_sha256 == MSI_PAYLOAD_10_SHA256
|
||||
&& old.payload_bytes == MSI_PAYLOAD_BYTES
|
||||
})
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
pub fn sha256(&self) -> Result<String, serde_json::Error> {
|
||||
Ok(format!("{:x}", Sha256::digest(serde_json::to_vec(self)?)))
|
||||
@@ -127,6 +169,55 @@ mod tests {
|
||||
payload_bytes: 20,
|
||||
},
|
||||
no_reboot: true,
|
||||
package_code: None,
|
||||
predecessor: None,
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn major_upgrade_requires_explicit_exact_predecessor_new_package_and_same_family() {
|
||||
let old = recipe();
|
||||
let old_bytes = serde_json::to_vec(&old).unwrap();
|
||||
assert!(
|
||||
!String::from_utf8(old_bytes.clone())
|
||||
.unwrap()
|
||||
.contains("predecessor")
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::to_vec(&serde_json::from_slice::<WindowsMsiRecipe>(&old_bytes).unwrap())
|
||||
.unwrap(),
|
||||
old_bytes
|
||||
);
|
||||
let mut r = old.clone();
|
||||
r.recipe_id = MSI_UPGRADE_RECIPE_ID.into();
|
||||
r.package_code = Some("{8475C4D1-6EE2-4C3A-B41D-D834A20F4135}".into());
|
||||
r.postcondition.product_code = MSI_UPGRADE_PRODUCT_CODE.into();
|
||||
r.postcondition.product_version = MSI_UPGRADE_VERSION.into();
|
||||
r.postcondition.payload_sha256 = MSI_PAYLOAD_11_SHA256.into();
|
||||
r.postcondition.payload_bytes = MSI_PAYLOAD_BYTES;
|
||||
r.predecessor = Some(MsiPostcondition {
|
||||
product_code: MSI_PRODUCT_CODE.into(),
|
||||
product_version: MSI_VERSION.into(),
|
||||
payload_path: MSI_PAYLOAD.into(),
|
||||
payload_sha256: MSI_PAYLOAD_10_SHA256.into(),
|
||||
payload_bytes: MSI_PAYLOAD_BYTES,
|
||||
});
|
||||
assert!(r.validate());
|
||||
assert_ne!(r.sha256().unwrap(), old.sha256().unwrap());
|
||||
for case in 0..8 {
|
||||
let mut bad = r.clone();
|
||||
match case {
|
||||
0 => bad.predecessor = None,
|
||||
1 => bad.package_code = None,
|
||||
2 => bad.package_code = Some(MSI_PRODUCT_CODE.into()),
|
||||
3 => bad.upgrade_code = Uuid::new_v4().to_string(),
|
||||
4 => {
|
||||
bad.predecessor.as_mut().unwrap().product_code = MSI_UPGRADE_PRODUCT_CODE.into()
|
||||
}
|
||||
5 => bad.predecessor.as_mut().unwrap().payload_sha256 = "aa".repeat(32),
|
||||
6 => bad.postcondition.product_version = "1.2.0".into(),
|
||||
_ => bad.postcondition.payload_sha256 = MSI_PAYLOAD_10_SHA256.into(),
|
||||
};
|
||||
assert!(!bad.validate());
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user