//! Machine-wide install state in `HKLM\SOFTWARE\sandbox-runtime`.
//!
//! The registry is the natural home for small machine-wide
//! key/value state written by an elevated installer and read
//! unprivileged, and its DEFAULT security model is exactly the one
//! the install state needs — properties a `%ProgramData%` file
//! store had to hand-build (and defend in review, repeatedly):
//!
//! - Standard users cannot create keys under `HKLM\SOFTWARE`, so
//! there is nothing to squat: no take-ownership, no no-follow
//! open, no reparse rejection for the store itself.
//! - Admin-write / Users-read is the inherited default, so the
//! identity, marker, and ambient-deny values are trustworthy on
//! read — no SAM cross-validation, no corrupt-file degrade
//! paths, no schema-version machinery.
//!
//! Layout (all opens `KEY_WOW64_64KEY` so a 32-bit build would not
//! land in WOW6432Node):
//!
//! - `HKLM\SOFTWARE\sandbox-runtime`: `MarkerVersion` (DWORD,
//! written LAST — registry writes are not transactional across
//! values, so the marker is the commit point), `SandboxUser`,
//! `SandboxUserSid`, `SandboxGroupSid` (SZ), `CreatedAt`
//! (QWORD), `AmbientDenies` (MULTI_SZ).
//! - `…\Cred`: one `Blob` (BINARY, machine-scope DPAPI). Created
//! with an explicit SA — DACL atomic with creation — SYSTEM/
//! Administrators full, Users READ (machine DPAPI means readable
//! ⇒ decryptable: the shared-credential trade-off), sandbox
//! group DENY (the child must never learn its own password: with
//! it, it could CreateProcessWithLogonW itself a fresh logon
//! session outside the job/desktop confinement).
//! - `…\Ca`: one `Der` (BINARY). Users KEY_READ|KEY_SET_VALUE so
//! the unelevated `trust-ca` can record the CA; sandbox group
//! DENY so the CHILD cannot touch the record (the accepted
//! replace-the-CA trade-off is scoped to REAL local users).
use anyhow::{Context, Result, anyhow};
use windows::Win32::Foundation::ERROR_FILE_NOT_FOUND;
use windows::Win32::Security::Authorization::{SE_REGISTRY_KEY, SetNamedSecurityInfoW};
use windows::Win32::Security::{
DACL_SECURITY_INFORMATION, GetSecurityDescriptorDacl, PROTECTED_DACL_SECURITY_INFORMATION,
};
use windows::Win32::System::Registry::{
HKEY, HKEY_LOCAL_MACHINE, KEY_READ, KEY_WOW64_64KEY, KEY_WRITE, REG_BINARY, REG_DWORD,
REG_MULTI_SZ, REG_OPENED_EXISTING_KEY, REG_OPTION_NON_VOLATILE, REG_QWORD, REG_SZ, RegCloseKey,
RegCreateKeyExW, RegDeleteTreeW, RegOpenKeyExW, RegQueryValueExW, RegSetValueExW,
};
use crate::util::{OwnedSd, pcwstr, wstr};
pub const BASE: &str = r"SOFTWARE\sandbox-runtime";
pub const CRED_SUBKEY: &str = r"SOFTWARE\sandbox-runtime\Cred";
pub const CA_SUBKEY: &str = r"SOFTWARE\sandbox-runtime\Ca";
/// RAII HKEY.
pub struct Key(HKEY);
impl Drop for Key {
fn drop(&mut self) {
unsafe {
let _ = RegCloseKey(self.0);
}
}
}
impl Key {
/// Open an existing key. `Ok(None)` when absent.
pub fn open(path: &str, write: bool) -> Result