ambient.rs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //! Install-time ambient write-deny targets.
  2. //!
  3. //! The sandbox-user FS model's confinement premise — "a fresh local
  4. //! account has no inherent rights on real-user files" — holds under
  5. //! inheritance-protected profiles but NOT in Windows' stock
  6. //! world-writable corners: directories whose DACLs grant
  7. //! Users/Authenticated Users write access to every local account,
  8. //! the sandbox user included. `srt-win install` stamps each of these
  9. //! with `(D;OICI;WriteDeny;;;<sb-SID>)` (scoped to the sandbox user
  10. //! only; reads unaffected), records them in the state DB
  11. //! (`ambient_denies`), and `uninstall` removes them. Recording them
  12. //! makes [`crate::state_db`]'s recompose chokepoint fold the deny
  13. //! into every converge, so a session grant/stamp/release or crash
  14. //! recovery on the same path can never strip an install-time deny.
  15. //!
  16. //! Deliberately NOT in this list:
  17. //! - Drive roots (`C:\`, `D:\`): semantically fine — inherited denies
  18. //! compose correctly with grants (nearer-ancestor allow wins) — but
  19. //! `SetNamedSecurityInfoW` materializes inheritable ACEs onto every
  20. //! unprotected descendant, so stamping a volume root rewrites DACLs
  21. //! volume-wide at stamp and unstamp time. Tracked separately.
  22. //! - Third-party subtrees that sever inheritance and grant Users
  23. //! (`C:\ProgramData\<app>` with its own permissive protected DACL):
  24. //! unreachable by any ancestor stamp; the WRITE_RESTRICTED-token
  25. //! mechanism that would close them wholesale breaks Schannel by
  26. //! LSASS policy (see RESTRICTINGSIDS-RESULTS.md).
  27. use std::path::Path;
  28. /// The fixed world-writable system list, resolved against this
  29. /// machine's environment. Entries that do not exist on this SKU are
  30. /// omitted (e.g. `FxsTmp` is absent on some arm64 builds; `SysWOW64`
  31. /// twins exist only where WOW64 is installed).
  32. ///
  33. /// Sources: stock Windows 11 DACLs, verified by probing sandboxed
  34. /// writes (each of these accepted a write from the sandbox account
  35. /// with zero grants): `%ProgramData%` (`Users:(WD,AD)` + CREATOR
  36. /// OWNER), `%PUBLIC%`, and the classic `Users`-writable spool/temp
  37. /// dirs under `%SystemRoot%`.
  38. pub fn ambient_deny_targets() -> Vec<String> {
  39. let program_data = std::env::var("ProgramData").unwrap_or_else(|_| r"C:\ProgramData".into());
  40. let public = std::env::var("PUBLIC").unwrap_or_else(|_| r"C:\Users\Public".into());
  41. let system_root = std::env::var("SystemRoot").unwrap_or_else(|_| r"C:\Windows".into());
  42. let sr = |tail: &str| -> String { format!("{system_root}\\{tail}") };
  43. let candidates: Vec<String> = vec![
  44. program_data,
  45. public,
  46. sr("Temp"),
  47. sr("Tasks"),
  48. sr("tracing"),
  49. sr(r"Registration\CRMLog"),
  50. sr(r"System32\FxsTmp"),
  51. sr(r"System32\com\dmp"),
  52. sr(r"System32\spool\PRINTERS"),
  53. sr(r"System32\spool\drivers\color"),
  54. sr(r"SysWOW64\FxsTmp"),
  55. sr(r"SysWOW64\com\dmp"),
  56. sr(r"SysWOW64\Tasks"),
  57. ];
  58. candidates
  59. .into_iter()
  60. .filter(|p| Path::new(p).is_dir())
  61. .collect()
  62. }
  63. #[cfg(test)]
  64. mod tests {
  65. use super::*;
  66. #[test]
  67. fn targets_exist_and_are_absolute() {
  68. for t in ambient_deny_targets() {
  69. assert!(Path::new(&t).is_dir(), "{t} should exist");
  70. assert!(t.chars().nth(1) == Some(':'), "{t} should be absolute");
  71. }
  72. }
  73. /// On any Windows box the big three resolve and exist.
  74. #[test]
  75. fn core_targets_present() {
  76. let ts = ambient_deny_targets();
  77. let has = |frag: &str| ts.iter().any(|t| t.to_ascii_lowercase().contains(frag));
  78. assert!(has("programdata"), "ProgramData missing from {ts:?}");
  79. assert!(has("public"), "Public missing from {ts:?}");
  80. assert!(has(r"\temp"), "Windows\\Temp missing from {ts:?}");
  81. }
  82. }