diff --git a/Cargo.lock b/Cargo.lock index 71b34407c6..7ea938b507 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4156,6 +4156,7 @@ dependencies = [ name = "craft-suite-account" version = "0.1.0" dependencies = [ + "craft-platform", "dirs", "keyring", "keyring-core", diff --git a/crates/suite-account/Cargo.toml b/crates/suite-account/Cargo.toml index e1527bfd8c..75edc2a751 100644 --- a/crates/suite-account/Cargo.toml +++ b/crates/suite-account/Cargo.toml @@ -31,5 +31,10 @@ keyring = "4.2" # machine-local persistence. `keyring` installs the platform store; this reads it. keyring-core = "1" +# The account list is created with an access list of its user and SYSTEM only +# (`owner_only::create_private_file`). +[target.'cfg(windows)'.dependencies] +craft-platform = { workspace = true } + [dev-dependencies] tempfile = "3" diff --git a/crates/suite-account/src/accounts.rs b/crates/suite-account/src/accounts.rs index fb8ce5d7de..b11cb27156 100644 --- a/crates/suite-account/src/accounts.rs +++ b/crates/suite-account/src/accounts.rs @@ -263,7 +263,10 @@ pub fn read(path: &Path) -> Result { /// [`Error::Malformed`] forever. Every mutation here reads the file first. There would be /// nothing left to repair it with. /// -/// Readable by this user alone where the platform has file modes. The file holds no token. It does +/// Readable by this user alone: mode `0600` on Unix, and on Windows an access list of this user and +/// SYSTEM only from the moment the file exists, whatever folder a portable install or +/// [`crate::paths::ENV_VAR`] chose. On Windows the access list is protected, so a change to the +/// folder's list does not reach the file. The file holds no token. It does /// say who is signed in. That is nobody else's business. /// /// Refuses a list that would serialize past `MAX_BYTES`, before any file is touched. [`read`] @@ -282,14 +285,17 @@ pub fn write(path: &Path, accounts: &Accounts) -> Result<(), Error> { ))); } std::fs::create_dir_all(dir).map_err(|e| Error::Io(format!("couldn't create {} ({e})", dir.display())))?; - use std::io::Write; let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0); let pid = std::process::id(); - let tmp = dir.join(format!(".{}.{}.{}.tmp", path.file_name().and_then(|n| n.to_str()).unwrap_or("accounts"), stamp, pid)); + let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("accounts"); + let tmp_name = format!(".{file_name}.{stamp}.{pid}.tmp"); + #[cfg(not(windows))] + let tmp = dir.join(&tmp_name); #[cfg(unix)] { use std::fs::OpenOptions; + use std::io::Write; use std::os::unix::fs::OpenOptionsExt; let mut file = OpenOptions::new() .write(true) @@ -300,14 +306,16 @@ pub fn write(path: &Path, accounts: &Accounts) -> Result<(), Error> { file.write_all(&json).map_err(|e| Error::Io(format!("couldn't write {} ({e})", tmp.display())))?; file.sync_all().map_err(|e| Error::Io(format!("couldn't flush {} ({e})", tmp.display())))?; } - #[cfg(not(unix))] + #[cfg(windows)] + let tmp = private_copy(path, dir, &tmp_name, &json)?; + #[cfg(not(any(unix, windows)))] { use std::fs::OpenOptions; + use std::io::Write; let mut file = OpenOptions::new().write(true).create_new(true).open(&tmp).map_err(|e| Error::Io(format!("couldn't create {} ({e})", tmp.display())))?; file.write_all(&json).map_err(|e| Error::Io(format!("couldn't write {} ({e})", tmp.display())))?; file.sync_all().map_err(|e| Error::Io(format!("couldn't flush {} ({e})", tmp.display())))?; - restrict(&tmp)?; } std::fs::rename(&tmp, path).map_err(|e| Error::Io(format!("couldn't replace {} ({e})", path.display())))?; // The flush above persisted the file's bytes. The rename changed a directory entry, and that @@ -324,19 +332,147 @@ pub fn write(path: &Path, accounts: &Accounts) -> Result<(), Error> { Ok(()) } -#[cfg(not(unix))] -fn restrict(_path: &Path) -> Result<(), Error> { - // A no-op today. That is a real gap this comment used to paper over. A new file in the - // platform configuration folder (the default `paths::Rule::Platform` home) inherits that - // folder's own, profile-scoped access rules. The account list is this user's alone there. - // `paths::Rule::Portable` and `paths::Rule::EnvVar` point `write` at whatever folder the - // caller named. A new file there inherits that folder's own Windows access control list, - // whatever it is. A shared or loosely permissioned folder in either of those two cases - // leaves the account list (subjects, display names, emails, never a token) readable by - // anyone else with access to it. Narrowing that needs a per-user Windows DACL on the file, - // set through the Win32 authorization API. Every binding for that API is an `unsafe fn`. - // This workspace forbids `unsafe_code` outright. Closing this for real needs either a - // maintainer-approved exception for one call site or a vetted dependency that wraps that API - // safely. Both are decisions for a dedicated PR. See `paths::Rule`. - Ok(()) +/// Write `json` to a new file beside the list at `path`, in its folder `dir`, that only this user +/// and SYSTEM can open, and return its path for [`write`] to rename into place. +/// +/// A new file on Windows takes its folder's access list, and a portable install or +/// [`crate::paths::ENV_VAR`] can name a folder that lets every local user read what is created in +/// it. Every copy is created by `owner_only::create_private_file`, which gives the file a protected +/// access list of this user and SYSTEM only in the call that creates it. The rename keeps that +/// list, and a later change to the folder's list does not reach it. That call runs PowerShell, so +/// [`prepare`] creates an empty copy before the accounts lock is taken, and this function writes +/// the list into it. Without a copy this thread prepared (a direct call, or a failed preparation), +/// the copy is created here, named `tmp_name`. +#[cfg(windows)] +fn private_copy(path: &Path, dir: &Path, tmp_name: &str, json: &[u8]) -> Result { + use std::io::Write; + if let Some((copy, mut file)) = take_prepared(path) { + match file.write_all(json).and_then(|()| file.sync_all()) { + Ok(()) => return Ok(copy), + Err(e) => { + drop(file); + let _ = std::fs::remove_file(©); + log::warn!("couldn't write a prepared copy of the account list ({e}); a new one is created"); + } + } + } + let tmp = dir.join(tmp_name); + user_sid() + .and_then(|me| craft_platform::owner_only::create_private_file(&tmp, &me, json)) + .map_err(|e| Error::Io(format!("couldn't create {} for its owner only ({e})", tmp.display())))?; + Ok(tmp) +} + +/// Copies made by [`prepare`] that wait for [`private_copy`]: the thread that made the copy, the +/// list it is for, the copy, and the copy held open without delete sharing. +#[cfg(windows)] +type Prepared = Vec<(std::thread::ThreadId, std::path::PathBuf, std::path::PathBuf, std::fs::File)>; + +/// The copy one [`prepare`] call made. Dropping it closes and removes the copy if no write took it. +#[cfg(windows)] +pub(crate) struct PreparedCopy(Option); + +#[cfg(windows)] +impl Drop for PreparedCopy { + fn drop(&mut self) { + let Some(copy) = self.0.take() else { return }; + let mut waiting = prepared(); + let Some(at) = waiting.iter().position(|(_, _, waiting, _)| *waiting == copy) else { return }; + let (_, _, copy, file) = waiting.swap_remove(at); + drop(waiting); + drop(file); + let _ = std::fs::remove_file(©); + } +} + +/// The copies waiting for [`private_copy`]. +#[cfg(windows)] +fn prepared() -> std::sync::MutexGuard<'static, Prepared> { + static PREPARED: std::sync::Mutex = std::sync::Mutex::new(Vec::new()); + PREPARED.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + +/// The copy of the list at `path` that this thread prepared, with its open handle, removed from the +/// waiting list. A copy another thread prepared is left for that thread's write. +#[cfg(windows)] +fn take_prepared(path: &Path) -> Option<(std::path::PathBuf, std::fs::File)> { + let me = std::thread::current().id(); + let mut prepared = prepared(); + let at = prepared.iter().position(|(thread, list, ..)| *thread == me && list == path)?; + let (_, _, copy, file) = prepared.swap_remove(at); + Some((copy, file)) +} + +/// This user's SID, read once per process (`whoami` is a process start). +#[cfg(windows)] +fn user_sid() -> std::io::Result { + static SID: std::sync::OnceLock = std::sync::OnceLock::new(); + if let Some(sid) = SID.get() { + return Ok(sid.clone()); + } + let sid = craft_platform::owner_only::current_user_sid()?; + Ok(SID.get_or_init(|| sid).clone()) +} + +/// Before the accounts lock is taken, create an empty copy of the list at `path` for its owner only +/// and keep it open for [`private_copy`], so that the PowerShell run it needs doesn't hold the lock +/// other processes wait on. The caller keeps the returned [`PreparedCopy`] until its change under +/// the lock is done; a change that writes nothing then has its copy removed. A failure here is left +/// for [`write`] to handle. +/// +/// The copy is opened without delete sharing and then checked, so no other account can remove, +/// rename or replace it between the check and the write. A copy that fails the check is removed. +/// Empty copies beside the list that are at least a minute old, which ended processes left, are +/// removed first; a copy that a running process holds open can't be, since removing it needs +/// delete sharing. +#[cfg(windows)] +pub(crate) fn prepare(path: &Path) -> PreparedCopy { + use std::os::windows::fs::OpenOptionsExt as _; + use std::sync::atomic::{AtomicU64, Ordering}; + + use craft_platform::owner_only; + /// `FILE_SHARE_READ | FILE_SHARE_WRITE`: no `FILE_SHARE_DELETE`. + const SHARE_NO_DELETE: u32 = 0x1 | 0x2; + /// How old an empty copy that no process holds must be before it is removed. Another process + /// that is preparing its copy has it closed for a moment, between its creation and the open. + const LEFT_AFTER: std::time::Duration = std::time::Duration::from_secs(60); + static COUNT: AtomicU64 = AtomicU64::new(0); + + let (Some(dir), Some(name)) = (path.parent(), path.file_name().and_then(|n| n.to_str())) else { return PreparedCopy(None) }; + let Ok(me) = user_sid() else { return PreparedCopy(None) }; + let _ = std::fs::create_dir_all(dir); + for entry in std::fs::read_dir(dir).into_iter().flatten().flatten() { + let left = entry.file_name(); + let left = left.to_string_lossy(); + if left.starts_with(&format!(".{name}.")) + && left.ends_with(".prepared.tmp") + && entry.metadata().is_ok_and(|meta| { + meta.is_file() && meta.len() == 0 && meta.modified().ok().and_then(|at| at.elapsed().ok()).is_some_and(|age| age >= LEFT_AFTER) + }) + { + let _ = std::fs::remove_file(entry.path()); + } + } + let copy = dir.join(format!(".{name}.{}.{}.prepared.tmp", std::process::id(), COUNT.fetch_add(1, Ordering::Relaxed))); + if let Err(e) = owner_only::create_private_file(©, &me, b"") { + log::warn!("couldn't prepare a copy of the account list for its owner only ({e})"); + return PreparedCopy(None); + } + let checked = std::fs::OpenOptions::new().write(true).share_mode(SHARE_NO_DELETE).open(©).and_then(|file| { + match owner_only::foreign_access(&owner_only::sddl_of(©)?, &me) { + None => Ok(file), + Some(problem) => Err(std::io::Error::other(problem)), + } + }); + match checked { + Ok(file) => { + prepared().push((std::thread::current().id(), path.to_path_buf(), copy.clone(), file)); + PreparedCopy(Some(copy)) + } + Err(e) => { + let _ = std::fs::remove_file(©); + log::warn!("a prepared copy of the account list is not this user's alone ({e}); it was removed"); + PreparedCopy(None) + } + } } diff --git a/crates/suite-account/src/lock.rs b/crates/suite-account/src/lock.rs index 160ff3e3c8..6a95e6bd2a 100644 --- a/crates/suite-account/src/lock.rs +++ b/crates/suite-account/src/lock.rs @@ -49,6 +49,8 @@ fn open_lock_file(path: &Path) -> io::Result { /// [`Error::Io`]; `f` may return any [`Error`]. pub(crate) fn with_lock(path: &Path, f: impl FnOnce() -> Result) -> Result { let lock = open_lock_file(path).map_err(|e| Error::Io(format!("couldn't open the lock beside {} ({e})", path.display())))?; + #[cfg(windows)] + let _prepared = crate::accounts::prepare(path); let start = Instant::now(); loop { match lock.try_lock() { diff --git a/crates/suite-account/src/tests.rs b/crates/suite-account/src/tests.rs index c1a614aab4..045a2065ee 100644 --- a/crates/suite-account/src/tests.rs +++ b/crates/suite-account/src/tests.rs @@ -521,6 +521,159 @@ fn write_leaves_a_previously_good_file_untouched_when_the_new_one_is_too_large() assert_eq!(accounts::read(&path).unwrap(), good, "the earlier, readable file must still be exactly what was there before"); } +/// A portable install or `ARTCRAFT_SUITE_DIR` can put the list in a folder that lets every local +/// user read the files created in it, as a new folder at a drive root does. The list written there +/// has an access list of its user and SYSTEM only. +#[cfg(windows)] +#[test] +fn the_list_in_a_folder_users_can_read_is_still_this_users_alone() { + use craft_platform::owner_only; + let dir = tempfile::tempdir().unwrap(); + let granted = std::process::Command::new("icacls").arg(dir.path()).arg("/grant").arg("*S-1-5-32-545:(OI)(CI)R").output().unwrap(); + assert!(granted.status.success(), "{}", String::from_utf8_lossy(&granted.stderr)); + let path = dir.path().join(paths::FILE); + let list = + accounts::Accounts { format: accounts::FORMAT, accounts: vec![account_at("a", 0)], active: Some("a".to_owned()), tombstones: Vec::new() }; + + accounts::write(&path, &list).unwrap(); + + let me = owner_only::current_user_sid().unwrap(); + let sddl = owner_only::sddl_of(&path).unwrap(); + assert!(!sddl.contains(";BU)") && !sddl.contains(";S-1-5-32-545)"), "Users must not inherit read access: {sddl}"); + assert_eq!(owner_only::foreign_access(&sddl, &me), None, "{sddl}"); + assert_eq!(accounts::read(&path).unwrap(), list); + // A second write, through a copy prepared before the lock as a `Store` does, leaves no copy behind. + let _copy = accounts::prepare(&path); + accounts::write(&path, &list).unwrap(); + assert_eq!(owner_only::foreign_access(&owner_only::sddl_of(&path).unwrap(), &me), None); + let names: Vec<_> = std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().file_name()).collect(); + assert_eq!(names, [std::ffi::OsString::from(paths::FILE)], "no temporary file is left behind"); +} + +/// The list's access list is protected: a change to its folder's access list, which Windows hands +/// down to the files in the folder that inherit from it, does not reach the list, whether the list +/// was written directly or through a `Store`. +#[cfg(windows)] +#[test] +fn a_change_to_the_folders_access_list_does_not_reach_the_list() { + use craft_platform::owner_only; + let me = owner_only::current_user_sid().unwrap(); + for through_store in [false, true] { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(paths::FILE); + if through_store { + let store = Store::at(Some(dir.path().to_path_buf()), Memory::new()); + store.sign_in(&new_account(), &Secret::new(TOKEN), NOW).unwrap(); + } else { + let list = accounts::Accounts { + format: accounts::FORMAT, + accounts: vec![account_at("a", 0)], + active: Some("a".to_owned()), + tombstones: Vec::new(), + }; + accounts::write(&path, &list).unwrap(); + } + + let granted = std::process::Command::new("icacls").arg(dir.path()).arg("/grant").arg("*S-1-5-32-545:(OI)(CI)R").output().unwrap(); + assert!(granted.status.success(), "{}", String::from_utf8_lossy(&granted.stdout)); + + let sddl = owner_only::sddl_of(&path).unwrap(); + assert_eq!(owner_only::foreign_access(&sddl, &me), None, "through a Store: {through_store}; {sddl}"); + } +} + +/// A prepared copy that no write took is closed and removed when its change under the lock is +/// done, as for a discard of an account that is not in the list. +#[cfg(windows)] +#[test] +fn a_prepared_copy_no_write_took_is_removed() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(paths::FILE); + let copy = accounts::prepare(&path); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1, "the copy is made"); + drop(copy); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0, "the copy is removed"); + + let store = Store::at(Some(dir.path().to_path_buf()), Memory::new()); + store.sign_in(&new_account(), &Secret::new(TOKEN), NOW).unwrap(); + let names = || std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect::>(); + let before = names(); + let _ = store.discard("no such account"); + assert_eq!(names(), before, "a change that writes nothing leaves no copy behind"); +} + +/// A write uses the copy its own thread prepared and leaves another thread's copy for that +/// thread, which would otherwise have to create one while it holds the accounts lock. +#[cfg(windows)] +#[test] +fn a_write_takes_only_the_copy_its_thread_prepared() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(paths::FILE); + let list = + accounts::Accounts { format: accounts::FORMAT, accounts: vec![account_at("a", 0)], active: Some("a".to_owned()), tombstones: Vec::new() }; + let copies = || std::fs::read_dir(dir.path()).unwrap().map(|e| e.unwrap().path()).filter(|p| p != &path).collect::>(); + let prepared = std::sync::Barrier::new(2); + // The other thread keeps its copy until `done` is dropped, which a failed assertion does too. + let (done, wait) = std::sync::mpsc::channel::<()>(); + std::thread::scope(|scope| { + let (list_path, prepared) = (&path, &prepared); + scope.spawn(move || { + let _other = accounts::prepare(list_path); + prepared.wait(); + let _ = wait.recv(); + }); + prepared.wait(); + let other = copies(); + assert_eq!(other.len(), 1, "{other:?}"); + let _mine = accounts::prepare(&path); + accounts::write(&path, &list).unwrap(); + assert_eq!(copies(), other, "the other thread's copy is still waiting"); + drop(done); + }); + assert_eq!(copies(), Vec::::new()); +} + +/// A prepared copy is created with an access list of its user and SYSTEM only, and can't be +/// removed, renamed or replaced, nor its folder renamed, between its check and the write. An empty +/// copy that an ended process left a minute or more ago is removed by the next preparation; a +/// newer one, which another process may be about to open, is kept. +#[cfg(windows)] +#[test] +fn a_prepared_copy_is_private_and_cannot_be_swapped_before_the_write() { + use craft_platform::owner_only; + let me = owner_only::current_user_sid().unwrap(); + let root = tempfile::tempdir().unwrap(); + let dir = root.path().join("suite"); + std::fs::create_dir(&dir).unwrap(); + let granted = std::process::Command::new("icacls").arg(&dir).arg("/grant").arg("*S-1-5-32-545:(OI)(CI)R").output().unwrap(); + assert!(granted.status.success(), "{}", String::from_utf8_lossy(&granted.stdout)); + let path = dir.join(paths::FILE); + let left = dir.join(format!(".{}.1.0.prepared.tmp", paths::FILE)); + std::fs::File::create(&left).unwrap().set_modified(std::time::SystemTime::now() - std::time::Duration::from_secs(120)).unwrap(); + let fresh = dir.join(format!(".{}.2.0.prepared.tmp", paths::FILE)); + std::fs::write(&fresh, b"").unwrap(); + + let _copy = accounts::prepare(&path); + + assert!(!left.exists(), "an old empty copy that no process holds is removed"); + assert!(fresh.exists(), "a new empty copy, which another process may be about to open, is kept"); + let copies: Vec<_> = std::fs::read_dir(&dir).unwrap().map(|e| e.unwrap().path()).filter(|p| *p != fresh).collect(); + assert_eq!(copies.len(), 1, "{copies:?}"); + let copy = &copies[0]; + let sddl = owner_only::sddl_of(copy).unwrap(); + assert_eq!(owner_only::foreign_access(&sddl, &me), None, "{sddl}"); + assert!(std::fs::remove_file(copy).is_err(), "the checked copy must stay in place"); + assert!(std::fs::rename(copy, dir.join("moved")).is_err(), "the checked copy must not be renamed away"); + assert!(std::fs::rename(&dir, root.path().join("moved")).is_err(), "its folder must not be renamed away"); + + let list = + accounts::Accounts { format: accounts::FORMAT, accounts: vec![account_at("a", 0)], active: Some("a".to_owned()), tombstones: Vec::new() }; + accounts::write(&path, &list).unwrap(); + assert!(!copy.exists(), "the write used the prepared copy"); + assert_eq!(accounts::read(&path).unwrap(), list); + assert_eq!(owner_only::foreign_access(&owner_only::sddl_of(&path).unwrap(), &me), None); +} + #[test] fn a_sign_in_with_an_oversized_display_name_fails_without_bricking_the_list() { let dir = tempfile::tempdir().unwrap();