diff --git a/api/crates/layerfs-api/tests/backup_family.rs b/api/crates/layerfs-api/tests/backup_family.rs index 273f94eeaf..f334b686e3 100644 --- a/api/crates/layerfs-api/tests/backup_family.rs +++ b/api/crates/layerfs-api/tests/backup_family.rs @@ -261,6 +261,38 @@ fn backups_against_minio() { ); assert_eq!(states(&home).len(), 2, "objects stayed"); + // a Store folder already there at the daemon's own mode is restored into + // as before (#570 P1): its mode stays, and the Store it takes opens + let private = paths::store_dir(&home, "kept"); + std::fs::create_dir_all(&private).unwrap(); + std::fs::set_permissions(&private, std::fs::Permissions::from_mode(0o700)).unwrap(); + let kept = ok( + &home, + "backup.restore", + &[ + ("store", t("kept")), + ("bucket", t("b1")), + ("file", t("shop")), + ], + ); + assert_eq!(kept.get("projects"), Some(&SdkValue::List(vec![t("api")]))); + assert!(paths::store_file(&home, "kept").is_file()); + assert_eq!( + std::fs::metadata(&private).unwrap().permissions().mode() & 0o7777, + 0o700, + "a Store's own folder stays 0700" + ); + let read = ok( + &home, + "file.read", + &[ + ("project", t("kept/api")), + ("branch", t("dev")), + ("path", t("/NEW")), + ], + ); + assert_eq!(read.get_text("text"), Some("x")); + for dir in [home, folder, root] { let _ = std::fs::remove_dir_all(dir); } diff --git a/api/crates/layerfs-api/tests/backup_refusals.rs b/api/crates/layerfs-api/tests/backup_refusals.rs index ebf7cc008e..25dc198a3d 100644 --- a/api/crates/layerfs-api/tests/backup_refusals.rs +++ b/api/crates/layerfs-api/tests/backup_refusals.rs @@ -128,6 +128,121 @@ fn restore_over_a_store_with_a_running_sandbox_is_in_use() { } } +/// `store/` already there but not the daemon's private directory: the +/// restore refuses it with `store.create`'s own code and remedy before it +/// stages anything, writes nothing and leaves the user's directory as it was +/// (#570 P1). The same folder held `0700`, and an absent one, pass the check +/// and go on into the download. +#[test] +fn restore_refuses_a_store_folder_that_is_not_private() { + let home = tempdir("folder-mode"); + home::open(&home).unwrap(); + dead_bucket(&home); + let directory = paths::store_dir(&home, "open-dir"); + let store = home.join("store"); + let restore = || { + call( + &home, + "backup.restore", + &[("store", t("open-dir")), ("bucket", t("dead"))], + ) + .unwrap_err() + }; + let mode = |dir: &Path| std::fs::metadata(dir).unwrap().permissions().mode() & 0o7777; + + std::fs::create_dir_all(&directory).unwrap(); + std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o755)).unwrap(); + let (code, message) = restore(); + assert_eq!(code, "Unsupported", "{message}"); + assert!( + message.contains(&directory.display().to_string()), + "{message}" + ); + assert!( + message.contains("0755") && message.contains("0700"), + "{message}" + ); + assert!( + message.contains(&format!("chmod 700 {}", directory.display())), + "{message}" + ); + // nothing was written: no staging folder, no Store file or record, and + // the user's own directory keeps its mode + let names: Vec<_> = std::fs::read_dir(&store) + .unwrap() + .filter_map(|entry| entry.ok()) + .map(|entry| entry.file_name().to_string_lossy().into_owned()) + .collect(); + assert_eq!(names, vec!["open-dir"], "a refused restore stages nothing"); + assert_eq!(mode(&directory), 0o755, "the folder's mode is not changed"); + assert!(!paths::store_file(&home, "open-dir").exists()); + assert!(!paths::store_record(&home, "open-dir").exists()); + + // the daemon's own private directory passes: the restore goes on into + // the download (`remote::load`), which fails there as it would without + // this check + std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o700)).unwrap(); + let failed = restore(); + failed_in_the_download(&failed.0, &failed.1); + assert!(!paths::store_file(&home, "open-dir").exists()); + assert!(!paths::store_record(&home, "open-dir").exists()); + + // and an absent folder passes the check the same way; the install that + // would make it `0700` never runs without install permission, and is + // covered by `layerfs-store`'s `restore_install` + std::fs::remove_dir_all(&directory).unwrap(); + let failed = restore(); + failed_in_the_download(&failed.0, &failed.1); + assert!(!directory.exists(), "no install ran for this call"); + // both calls reached the download: each made its staging folder under + // the home lock and removed it when it failed + let staged: Vec<_> = std::fs::read_dir(&store) + .unwrap() + .filter_map(|entry| entry.ok()) + .map(|entry| entry.file_name().to_string_lossy().into_owned()) + .filter(|name| name.starts_with(".restore-")) + .collect(); + assert!( + staged.is_empty(), + "a failed download left its staging folder: {staged:?}" + ); + let _ = std::fs::remove_dir_all(home); +} + +/// A restore that passed the directory check failed inside the download: +/// `remote::load`'s own first checks run there, and which one stops it says +/// how far it went — the installer user in a Linux process that cannot +/// install (before any request is sent), else the dead endpoint's own +/// request. Neither is this directory guard. +fn failed_in_the_download(code: &str, message: &str) { + assert_eq!(code, "Failed", "{message}"); + if installs_as_the_daemons_user() { + assert!( + !message.contains("chmod 700"), + "the download failed, not the directory guard: {message}" + ); + } else { + assert!( + message.contains("must run as user 0"), + "the download's own installer-user check: {message}" + ); + } +} + +/// Whether this process is the user a Store install requires: on Linux only +/// uid 0 is (`layerfs-store-service` `install`), so every other run stops at +/// that check instead of reaching the bucket. +fn installs_as_the_daemons_user() -> bool { + #[cfg(target_os = "linux")] + { + nix::unistd::geteuid().is_root() + } + #[cfg(not(target_os = "linux"))] + { + true + } +} + /// A restore killed mid-download leaves its folder; the next restore removes /// it, and keeps a folder whose restore still holds its lock and one with /// no lock file at all. diff --git a/api/crates/layerfs-api/tests/project_export.rs b/api/crates/layerfs-api/tests/project_export.rs index 703e989e47..85ef103334 100644 --- a/api/crates/layerfs-api/tests/project_export.rs +++ b/api/crates/layerfs-api/tests/project_export.rs @@ -875,7 +875,7 @@ fn branch_selection_and_pinned_snapshot_under_concurrent_writes() { fn move_store_file(from: &Path, to: &Path, store: &str) { let source = paths::store_dir(from, store); let target = paths::store_dir(to, store); - std::fs::create_dir_all(&target).unwrap(); + home::private_dir(&target).unwrap(); for entry in std::fs::read_dir(&source).unwrap() { let entry = entry.unwrap(); let name = entry.file_name().to_string_lossy().into_owned(); diff --git a/api/crates/layerfs-api/tests/project_family.rs b/api/crates/layerfs-api/tests/project_family.rs index 6785b14abc..fc4ba41bc0 100644 --- a/api/crates/layerfs-api/tests/project_family.rs +++ b/api/crates/layerfs-api/tests/project_family.rs @@ -86,6 +86,36 @@ fn init_files_branches_move_and_remove() { ); assert_eq!(refused.unwrap_err().0, "NotFound"); ok(&home, "store.create", &[("name", t("shop"))]); + // project.init on an existing Store refuses up front if its directory was + // made non-private (e.g. chmod 0755), reporting path, 0755, 0700 and chmod 700 + { + use std::os::unix::fs::PermissionsExt; + let shop_dir = paths::store_dir(&home, "shop"); + std::fs::set_permissions(&shop_dir, std::fs::Permissions::from_mode(0o755)).unwrap(); + let (code, message) = call( + &home, + "project.init", + &[ + ("project", t("shop/api")), + ("folder", t(&folder.display().to_string())), + ], + ) + .unwrap_err(); + assert_eq!(code, "Unsupported", "{message}"); + assert!( + message.contains(&shop_dir.display().to_string()), + "{message}" + ); + assert!( + message.contains("0755") && message.contains("0700"), + "{message}" + ); + assert!( + message.contains(&format!("chmod 700 {}", shop_dir.display())), + "{message}" + ); + std::fs::set_permissions(&shop_dir, std::fs::Permissions::from_mode(0o700)).unwrap(); + } let mut events = Vec::new(); let args = SdkValue::map([ ("project", t("shop/api")), diff --git a/api/crates/layerfs-api/tests/project_init_worker.rs b/api/crates/layerfs-api/tests/project_init_worker.rs index 553f602218..d0f3900c4b 100644 --- a/api/crates/layerfs-api/tests/project_init_worker.rs +++ b/api/crates/layerfs-api/tests/project_init_worker.rs @@ -42,7 +42,7 @@ fn init_args(project: &str, folder: &Path) -> SdkValue { fn move_store_file(from: &Path, to: &Path, store: &str) { let source = paths::store_dir(from, store); let target = paths::store_dir(to, store); - std::fs::create_dir_all(&target).unwrap(); + home::private_dir(&target).unwrap(); for entry in std::fs::read_dir(&source).unwrap() { let entry = entry.unwrap(); let name = entry.file_name().to_string_lossy().into_owned(); diff --git a/api/crates/layerfs-api/tests/store_family.rs b/api/crates/layerfs-api/tests/store_family.rs index 8fa06e6312..36e5100aae 100644 --- a/api/crates/layerfs-api/tests/store_family.rs +++ b/api/crates/layerfs-api/tests/store_family.rs @@ -63,8 +63,46 @@ fn home_with_box() -> (PathBuf, PathBuf) { #[test] fn create_list_get_and_status() { + use std::os::unix::fs::PermissionsExt; let (home, root) = home_with_box(); + // a pre-existing store directory with group/other access is refused up + // front without changing the user's directory mode + let bad_dir = paths::store_dir(&home, "open-dir"); + home::private_dir(bad_dir.parent().unwrap()).unwrap(); + std::fs::create_dir_all(&bad_dir).unwrap(); + std::fs::set_permissions(&bad_dir, std::fs::Permissions::from_mode(0o755)).unwrap(); + let (code, message) = call(&home, "store.create", &[("name", t("open-dir"))]).unwrap_err(); + assert_eq!(code, "Unsupported", "{message}"); + assert!( + message.contains(&bad_dir.display().to_string()), + "{message}" + ); + assert!( + message.contains("0755") && message.contains("0700"), + "{message}" + ); + assert!( + message.contains(&format!("chmod 700 {}", bad_dir.display())), + "{message}" + ); + assert!(bad_dir.exists(), "user's existing directory is not deleted"); + assert_eq!( + std::fs::metadata(&bad_dir).unwrap().permissions().mode() & 0o7777, + 0o755, + "user's existing directory permissions are not modified" + ); + std::fs::remove_dir_all(&bad_dir).unwrap(); + let made = call(&home, "store.create", &[("name", t("shop"))]).unwrap(); + assert_eq!( + std::fs::metadata(paths::store_dir(&home, "shop")) + .unwrap() + .permissions() + .mode() + & 0o7777, + 0o700, + "a Store's own folder is 0700" + ); assert_eq!(made.get_text("machine"), Some("native")); assert!( made.get("cursor").is_none(), diff --git a/control/crates/layerfs-bucket/src/backup/restore.rs b/control/crates/layerfs-bucket/src/backup/restore.rs index 86d57bfe97..f2a87bcf3a 100644 --- a/control/crates/layerfs-bucket/src/backup/restore.rs +++ b/control/crates/layerfs-bucket/src/backup/restore.rs @@ -25,8 +25,7 @@ use crate::bucket::{progress, Driver}; use layerfs_persistence::{PersistenceConfig, SqlitePersistenceProfile}; use layerfs_storage::ReservationBlocks; use layerfs_store_service::bootstrap::open_store_observed; -use layerfs_store_service::install::{installer_user, paths}; -use layerfs_store_service::kernel_guard; +use layerfs_store_service::install::{paths, prepare}; use std::fs::{self, OpenOptions}; use std::io::{self, Write}; use std::path::{Path, PathBuf}; @@ -187,19 +186,10 @@ fn fetch( let result = (|| { let text = |error: io::Error| error.to_string(); let object = store_object(name)?; - let directory = destination - .parent() - .ok_or("the Store destination has no directory")?; - // as install_local: the volume holding the Store's directory - kernel_guard::check(directory).map_err(|reason| reason.0)?; - installer_user()?; - if !directory.exists() { - use std::os::unix::fs::DirBuilderExt; - fs::DirBuilder::new() - .mode(0o700) - .create(directory) - .map_err(text)?; - } + // as install_local: the Store's own directory (`0700` when absent), + // the Store file guarded; a refusal reclaims only an empty directory + // this call made + prepare(destination)?; let (target, _) = paths(destination).map_err(text)?; let cloning = binding.is_none(); if cloning && exists(&target).map_err(text)? { diff --git a/control/crates/layerfs-control/src/doctor/scan.rs b/control/crates/layerfs-control/src/doctor/scan.rs index cc8cd0567f..bb933ed7f6 100644 --- a/control/crates/layerfs-control/src/doctor/scan.rs +++ b/control/crates/layerfs-control/src/doctor/scan.rs @@ -1,8 +1,8 @@ //! Every record of a layout 3 home, read once: whether it parses, whether //! an entity record has `"v": 1`, and the modes of the home's folders -//! (0700, or 0701 where a namespaces Sandbox passes through) and record -//! files (0600). A Store folder with projects and no Store record is -//! reported here too. +//! (0700, or 0701 where a namespaces Sandbox passes through; a Store's own +//! folder is 0700 like the daemon's rule) and record files (0600). A Store +//! folder with projects and no Store record is reported here too. use super::report::Findings; use layerfs_bridge::control::{SdkRefusal, SdkValue}; use layerfs_home::home::{paths, read_record, RECORD_VERSION}; @@ -37,6 +37,31 @@ fn names(dir: &Path) -> Result> { paths::names(dir).map_err(failed) } +/// The Store names under `home/store`: a real folder, and a link to one, which +/// the scan reports (the Store guard refuses a link where it wants the Store's +/// own directory) rather than skipping it as [`paths::names`] does. +fn store_names(dir: &Path) -> Result> { + let mut names = Vec::new(); + let entries = match std::fs::read_dir(dir) { + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(names), + other => other.map_err(failed)?, + }; + for entry in entries { + let entry = entry.map_err(failed)?; + let kind = entry.file_type().map_err(failed)?; + if !kind.is_dir() && !kind.is_symlink() { + continue; + } + if let Some(name) = entry.file_name().to_str() { + if !name.starts_with('.') { + names.push(name.to_owned()); + } + } + } + names.sort(); + Ok(names) +} + /// Reads the home's records and modes. pub(super) fn scan(home: &Path, findings: &mut Findings) -> Result { let mut scanned = Scanned::default(); @@ -60,7 +85,7 @@ pub(super) fn scan(home: &Path, findings: &mut Findings) -> Result { } record(&paths::worker_record(home, &name), entity, false, findings); } - for name in names(&home.join("store"))? { + for name in store_names(&home.join("store"))? { store(home, &name, &mut scanned, findings)?; } for name in names(&home.join("sandbox"))? { @@ -78,7 +103,9 @@ pub(super) fn scan(home: &Path, findings: &mut Findings) -> Result { /// One Store's folder: its record and its projects'. fn store(home: &Path, name: &str, scanned: &mut Scanned, findings: &mut Findings) -> Result<()> { let entity = ("store", name); - folder(&paths::store_dir(home, name), entity, findings); + if store_folder(&paths::store_dir(home, name), entity, findings) { + return Ok(()); + } let path = paths::store_record(home, name); let has_record = path.exists(); if has_record { @@ -260,9 +287,37 @@ fn mode(path: &Path) -> Option { Some(metadata.permissions().mode() & 0o7777) } +/// Reports `path` when it is a symbolic link and says whether it did: a +/// link's own mode (0777) is not the folder's, and a `chmod` through the link +/// changes what it points at, so neither describes the folder. The Store +/// guard likewise refuses a link where it wants the Store's own directory. +fn link(path: &Path, entity: (&str, &str), findings: &mut Findings) -> bool { + let Ok(metadata) = std::fs::symlink_metadata(path) else { + return false; + }; + if !metadata.file_type().is_symlink() { + return false; + } + findings.error( + entity, + format!( + "{} is a symlink, not a folder; a `chmod` through it would change what it points at", + path.display() + ), + format!( + "make it a real folder: remove the link and create {} as 0700", + path.display() + ), + ); + true +} + /// A folder others may list or change, or its group may use, is an error; /// others may pass through (0701). fn folder(path: &Path, entity: (&str, &str), findings: &mut Findings) { + if link(path, entity, findings) { + return; + } if let Some(mode) = mode(path).filter(|mode| mode & 0o076 != 0) { findings.error( entity, @@ -275,6 +330,27 @@ fn folder(path: &Path, entity: (&str, &str), findings: &mut Findings) { } } +/// A Store's own folder is held to the daemon's rule (kernel_guard): no +/// group or other bits at all, not even pass-through, so a folder doctor +/// calls fine is one the daemon opens. Returns whether `path` is a symlink +/// (already reported, so the caller stops descending into this entry). +fn store_folder(path: &Path, entity: (&str, &str), findings: &mut Findings) -> bool { + if link(path, entity, findings) { + return true; + } + if let Some(mode) = mode(path).filter(|mode| mode & 0o077 != 0) { + findings.error( + entity, + format!( + "{} has mode {mode:04o}; a Store's folder is 0700", + path.display() + ), + format!("`chmod 700 {}`", path.display()), + ); + } + false +} + /// A record file others or its group may read is an error. fn file_mode(path: &Path, entity: (&str, &str), findings: &mut Findings) { if let Some(mode) = mode(path).filter(|mode| mode & 0o077 != 0) { diff --git a/control/crates/layerfs-control/tests/doctor.rs b/control/crates/layerfs-control/tests/doctor.rs index ab3a1bf223..f7de118a84 100644 --- a/control/crates/layerfs-control/tests/doctor.rs +++ b/control/crates/layerfs-control/tests/doctor.rs @@ -146,6 +146,198 @@ fn a_clean_home_is_ok_with_no_findings() { std::fs::remove_dir_all(home).unwrap(); } +#[test] +fn a_store_folder_others_may_pass_through_is_an_error_naming_chmod_700() { + // #534: the daemon refuses a Store folder with any group/other bit, so + // doctor must not call 0701 fine there; other home folders keep 0701. + let home = new_home("store-0701"); + layerfs_control::migrate::open(&home).unwrap(); + store(&home, "shop", sqlite("shop", "native")); + let native = SdkValue::map([("name", t("native")), ("platform", t("native"))]); + write_entity(&paths::machine_record(&home, "native"), &native).unwrap(); + let dir = paths::store_dir(&home, "shop"); + std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o701)).unwrap(); + std::fs::set_permissions(home.join("store"), std::fs::Permissions::from_mode(0o701)).unwrap(); + let report = doctor(&home).unwrap(); + assert_eq!(report.get("ok"), Some(&SdkValue::Bool(false)), "{report:?}"); + let found = findings(&report); + assert_eq!(found.len(), 1, "{found:?}"); + let (severity, kind, name, message, fix) = &found[0]; + assert_eq!( + (severity.as_str(), kind.as_str(), name.as_str()), + ("error", "store", "shop") + ); + assert!(message.contains("0701"), "{message}"); + assert!(fix.contains("chmod 700"), "{fix}"); + std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap(); + let report = doctor(&home).unwrap(); + assert_eq!(report.get("ok"), Some(&SdkValue::Bool(true)), "{report:?}"); + std::fs::remove_dir_all(home).unwrap(); +} + +#[test] +fn a_symlinked_store_folder_is_an_error_that_does_not_change_the_target() { + // the home's `store/` as a link: its own mode is 0777, not the folder's, + // and the remedy must not chmod through the link into its target + let home = new_home("store-link"); + layerfs_control::migrate::open(&home).unwrap(); + let elsewhere = + std::env::temp_dir().join(format!("layerfs-doctor-link-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&elsewhere); + std::fs::create_dir(&elsewhere).unwrap(); + std::fs::set_permissions(&elsewhere, std::fs::Permissions::from_mode(0o700)).unwrap(); + let store = home.join("store"); + let _ = std::fs::remove_dir_all(&store); + std::os::unix::fs::symlink(&elsewhere, &store).unwrap(); + let report = doctor(&home).unwrap(); + assert_eq!(report.get("ok"), Some(&SdkValue::Bool(false)), "{report:?}"); + let found = findings(&report); + assert_eq!(found.len(), 1, "{found:?}"); + let (severity, kind, _name, message, fix) = &found[0]; + assert_eq!((severity.as_str(), kind.as_str()), ("error", "home")); + assert!(message.contains("symlink"), "{message}"); + assert!(!fix.contains("chmod"), "{fix}"); + std::fs::remove_file(&store).unwrap(); + std::fs::remove_dir_all(&elsewhere).unwrap(); + std::fs::remove_dir_all(home).unwrap(); +} + +#[test] +fn a_symlinked_store_entry_is_an_error_that_does_not_change_the_target() { + // `store/` as a link: the daemon refuses it, so doctor reports it; + // the remedy is not a chmod, which would change the link's target + let home = new_home("store-entry-link"); + layerfs_control::migrate::open(&home).unwrap(); + store(&home, "shop", sqlite("shop", "native")); + let elsewhere = + std::env::temp_dir().join(format!("layerfs-doctor-entry-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&elsewhere); + std::fs::rename(paths::store_dir(&home, "shop"), &elsewhere).unwrap(); + std::os::unix::fs::symlink(&elsewhere, paths::store_dir(&home, "shop")).unwrap(); + let report = doctor(&home).unwrap(); + assert_eq!(report.get("ok"), Some(&SdkValue::Bool(false)), "{report:?}"); + let found = findings(&report); + assert_eq!(found.len(), 1, "{found:?}"); + let (severity, kind, name, message, fix) = &found[0]; + assert_eq!( + (severity.as_str(), kind.as_str(), name.as_str()), + ("error", "store", "shop") + ); + assert!(message.contains("symlink"), "{message}"); + assert!(!fix.contains("chmod"), "{fix}"); + std::fs::remove_file(paths::store_dir(&home, "shop")).unwrap(); + std::fs::remove_dir_all(&elsewhere).unwrap(); + std::fs::remove_dir_all(home).unwrap(); +} + +#[test] +fn a_symlink_to_a_file_under_store_does_not_abort_other_findings() { + // `store/` linked to a regular file: must not fail `read_dir(.../project)` + // with ENOTDIR and discard findings for the rest of the home + let home = new_home("store-entry-file-link"); + layerfs_control::migrate::open(&home).unwrap(); + let native = SdkValue::map([("name", t("native")), ("platform", t("native"))]); + write_entity(&paths::machine_record(&home, "native"), &native).unwrap(); + store(&home, "other", sqlite("other", "native")); + std::fs::set_permissions( + paths::store_dir(&home, "other"), + std::fs::Permissions::from_mode(0o701), + ) + .unwrap(); + let target = std::env::temp_dir().join(format!("layerfs-doctor-file-{}", std::process::id())); + let _ = std::fs::remove_file(&target); + std::fs::write(&target, b"keep").unwrap(); + std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o644)).unwrap(); + let link = paths::store_dir(&home, "shop"); + std::os::unix::fs::symlink(&target, &link).unwrap(); + + let report = doctor(&home).unwrap(); + assert_eq!(report.get("ok"), Some(&SdkValue::Bool(false)), "{report:?}"); + let found = findings(&report); + assert_eq!(found.len(), 2, "{found:#?}"); + assert_eq!( + ( + found[0].0.as_str(), + found[0].1.as_str(), + found[0].2.as_str() + ), + ("error", "store", "other") + ); + assert!(found[0].3.contains("0701"), "{found:#?}"); + assert_eq!( + ( + found[1].0.as_str(), + found[1].1.as_str(), + found[1].2.as_str() + ), + ("error", "store", "shop") + ); + assert!(found[1].3.contains("symlink"), "{found:#?}"); + assert!(!found[1].4.contains("chmod"), "{found:#?}"); + + assert!(std::fs::symlink_metadata(&link) + .unwrap() + .file_type() + .is_symlink()); + assert_eq!(std::fs::read_link(&link).unwrap(), target); + let target_meta = std::fs::symlink_metadata(&target).unwrap(); + assert!(target_meta.file_type().is_file()); + assert_eq!(target_meta.permissions().mode() & 0o7777, 0o644); + assert_eq!(std::fs::read(&target).unwrap(), b"keep"); + + std::fs::remove_file(&link).unwrap(); + std::fs::remove_file(&target).unwrap(); + std::fs::remove_dir_all(home).unwrap(); +} + +#[test] +fn a_dangling_symlink_under_store_is_an_error_that_keeps_scanning() { + let home = new_home("store-entry-dangling-link"); + layerfs_control::migrate::open(&home).unwrap(); + let native = SdkValue::map([("name", t("native")), ("platform", t("native"))]); + write_entity(&paths::machine_record(&home, "native"), &native).unwrap(); + store(&home, "other", sqlite("other", "native")); + std::fs::set_permissions( + paths::store_dir(&home, "other"), + std::fs::Permissions::from_mode(0o701), + ) + .unwrap(); + let missing = + std::env::temp_dir().join(format!("layerfs-doctor-missing-{}", std::process::id())); + let _ = std::fs::remove_file(&missing); + let link = paths::store_dir(&home, "dangling"); + std::os::unix::fs::symlink(&missing, &link).unwrap(); + + let report = doctor(&home).unwrap(); + assert_eq!(report.get("ok"), Some(&SdkValue::Bool(false)), "{report:?}"); + let found = findings(&report); + assert_eq!(found.len(), 2, "{found:#?}"); + assert_eq!( + ( + found[0].0.as_str(), + found[0].1.as_str(), + found[0].2.as_str() + ), + ("error", "store", "dangling") + ); + assert!(found[0].3.contains("symlink"), "{found:#?}"); + assert!(!found[0].4.contains("chmod"), "{found:#?}"); + assert_eq!( + ( + found[1].0.as_str(), + found[1].1.as_str(), + found[1].2.as_str() + ), + ("error", "store", "other") + ); + assert!(found[1].3.contains("0701"), "{found:#?}"); + assert_eq!(std::fs::read_link(&link).unwrap(), missing); + assert!(!missing.exists()); + + std::fs::remove_file(&link).unwrap(); + std::fs::remove_dir_all(home).unwrap(); +} + #[test] fn a_home_that_does_not_exist_is_ok_and_stays_absent() { let home = new_home("absent"); diff --git a/control/crates/layerfs-control/tests/store_doctor.rs b/control/crates/layerfs-control/tests/store_doctor.rs index c603653d33..fc4a68ee02 100644 --- a/control/crates/layerfs-control/tests/store_doctor.rs +++ b/control/crates/layerfs-control/tests/store_doctor.rs @@ -105,7 +105,7 @@ fn worker_child() { fn actual_worker_audit_streams_and_cancelled_peer_does_not_poison_next_call() { let f = Fixture::new(); let path = paths::store_file(&f.root, "one"); - std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + layerfs_home::home::private_dir(path.parent().unwrap()).unwrap(); let authority = HistoryCatalogConfig { binding_key: b"doctor-worker".to_vec(), cursor_key: [9; 32], diff --git a/control/crates/layerfs-sandbox-backend/src/backend/host_process/exec.rs b/control/crates/layerfs-sandbox-backend/src/backend/host_process/exec.rs index 32c7cc807f..2dc0c87fe0 100644 --- a/control/crates/layerfs-sandbox-backend/src/backend/host_process/exec.rs +++ b/control/crates/layerfs-sandbox-backend/src/backend/host_process/exec.rs @@ -1,9 +1,10 @@ //! One ordinary command to its end in a local Sandbox: the command identity, -//! the Workspace directory and a search path (on macOS this process's own); -//! nothing else of this process's environment, no shell of its own and no -//! implicit timeout. Each command leads a process group of its own, named in -//! the Sandbox until it is empty, so what it leaves running ends with the -//! Sandbox. +//! the Workspace directory, a search path (on macOS this process's own), and +//! (on macOS) the caller's Rust and Git locations (`RUSTUP_HOME`, +//! `CARGO_HOME`, `GIT_CONFIG_GLOBAL`) when present; nothing else of this +//! process's environment, no shell of its own and no implicit timeout. Each +//! command leads a process group of its own, named in the Sandbox until it is +//! empty, so what it leaves running ends with the Sandbox. use super::sandbox::Sandbox; use crate::local::streams; use crate::local::{groups, search::search_path}; @@ -12,6 +13,7 @@ use crate::{ExecExit, ExecRequest, RuntimeError, WireFailure}; use layerfs_bridge::process::{self, LocalProcess}; use nix::sys::signal::{killpg, Signal}; use std::{ + ffi::OsString, io::{self, Write}, os::unix::process::{CommandExt, ExitStatusExt}, path::Path, @@ -54,6 +56,7 @@ impl Sandbox { .args(&request.arguments[1..]) .env_clear() .env("PATH", search_path()) + .envs(user_environment()) .current_dir(Path::new(&request.directory)) // std drops supplementary groups (setgroups 0) when it sets a uid as root .uid(request.identity.uid) @@ -155,3 +158,43 @@ fn check(request: &ExecRequest) -> Result<(), RuntimeError> { } Ok(()) } + +/// The caller's Rust and Git locations on macOS, when present, so a command +/// whose `HOME` is the Sandbox's scratch directory still sees them unless the +/// caller's environment or the request sets its own. +#[cfg(target_os = "macos")] +fn user_environment() -> Vec<(&'static str, OsString)> { + use std::path::PathBuf; + let home = non_empty_env("HOME").map(PathBuf::from); + let existing = |path: PathBuf| path.exists().then(|| path.into_os_string()); + let mut vars = Vec::new(); + for (name, subdir) in [("RUSTUP_HOME", ".rustup"), ("CARGO_HOME", ".cargo")] { + let value = + non_empty_env(name).or_else(|| home.as_ref().and_then(|h| existing(h.join(subdir)))); + if let Some(value) = value { + vars.push((name, value)); + } + } + let git_config = non_empty_env("GIT_CONFIG_GLOBAL") + .or_else(|| home.as_ref().and_then(|h| existing(h.join(".gitconfig")))) + .or_else(|| { + let xdg = non_empty_env("XDG_CONFIG_HOME") + .map(PathBuf::from) + .or_else(|| home.as_ref().map(|h| h.join(".config")))?; + existing(xdg.join("git").join("config")) + }); + if let Some(value) = git_config { + vars.push(("GIT_CONFIG_GLOBAL", value)); + } + vars +} + +#[cfg(not(target_os = "macos"))] +fn user_environment() -> Vec<(&'static str, OsString)> { + Vec::new() +} + +#[cfg(target_os = "macos")] +fn non_empty_env(name: &str) -> Option { + std::env::var_os(name).filter(|value| !value.is_empty()) +} diff --git a/control/crates/layerfs-sandbox-backend/tests/local_search_path.rs b/control/crates/layerfs-sandbox-backend/tests/local_search_path.rs index 2296effb2c..a287f973cf 100644 --- a/control/crates/layerfs-sandbox-backend/tests/local_search_path.rs +++ b/control/crates/layerfs-sandbox-backend/tests/local_search_path.rs @@ -2,17 +2,26 @@ //! process's own PATH, so this user's tools (`node` in `~/.local/bin`, //! Homebrew's `/opt/homebrew/bin`) are found as in the caller's shell; with //! no PATH, or an empty one, a container's search path and Homebrew's. -//! A small shell script stands in for the daemon. One test: it changes this -//! process's PATH, which no other test in this binary reads. +//! Commands also receive `RUSTUP_HOME`, `CARGO_HOME` and `GIT_CONFIG_GLOBAL` +//! from the caller's environment or real `HOME` when present, while `HOME` +//! stays the Sandbox's scratch directory. A small shell script stands in for +//! the daemon. #![cfg(target_os = "macos")] use layerfs_sandbox_backend::{host_process::Sandbox, local::Local, CommandIdentity, ExecRequest}; use std::{ + ffi::OsString, fs, os::unix::fs::PermissionsExt, path::{Path, PathBuf}, + sync::{ + atomic::{AtomicUsize, Ordering}, + Mutex, + }, time::Duration, }; +static ENV_LOCK: Mutex<()> = Mutex::new(()); +static NEXT: AtomicUsize = AtomicUsize::new(0); const WAIT: Duration = Duration::from_secs(10); const FALLBACK: &str = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/homebrew/bin"; @@ -20,10 +29,11 @@ const FALLBACK: &str = /// A fresh 0755 runtime root and a stand-in daemon that prints its /// environment, then its listener line, then waits. fn fixture() -> (PathBuf, PathBuf) { - let base = std::env::temp_dir() - .canonicalize() - .unwrap() - .join(format!("layerfs-search-path-{}", std::process::id())); + let base = std::env::temp_dir().canonicalize().unwrap().join(format!( + "layerfs-search-path-{}-{}", + std::process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) + )); let _ = fs::remove_dir_all(&base); fs::create_dir_all(base.join("root")).unwrap(); fs::set_permissions(&base, fs::Permissions::from_mode(0o755)).unwrap(); @@ -76,6 +86,7 @@ fn command_path(sandbox: &Sandbox, environment: &[&str]) -> String { #[test] fn a_native_daemon_and_its_commands_start_with_this_processs_path() { + let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); let (root, daemon) = fixture(); let own = "/Users/someone/.local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin"; let saved = std::env::var_os("PATH"); @@ -105,3 +116,163 @@ fn a_native_daemon_and_its_commands_start_with_this_processs_path() { } let _ = fs::remove_dir_all(root.parent().unwrap()); } + +/// A command's output for `script`, `printf` by its absolute path, so a +/// lookup is not needed. +fn command_script(sandbox: &Sandbox, script: &str, environment: &[&str]) -> String { + let request = ExecRequest { + identity: CommandIdentity { + uid: nix::unistd::Uid::effective().as_raw(), + gid: nix::unistd::Gid::effective().as_raw(), + }, + arguments: vec!["/bin/sh".into(), "-c".into(), script.into()], + environment: environment.iter().map(|e| e.to_string()).collect(), + directory: sandbox.directory().display().to_string(), + stdin: false, + }; + let (mut out, mut err) = (Vec::new(), Vec::new()); + let exit = sandbox.run(&request, &mut out, &mut err).unwrap(); + assert_eq!(exit.code, 0, "{}", String::from_utf8_lossy(&err)); + String::from_utf8(out).unwrap() +} + +struct SavedEnv(Vec<(&'static str, Option)>); + +impl SavedEnv { + fn capture(names: &[&'static str]) -> Self { + Self( + names + .iter() + .map(|&name| (name, std::env::var_os(name))) + .collect(), + ) + } +} + +impl Drop for SavedEnv { + fn drop(&mut self) { + for (name, value) in &self.0 { + match value { + Some(v) => std::env::set_var(name, v), + None => std::env::remove_var(name), + } + } + } +} + +#[test] +fn a_native_command_receives_existing_user_toolchain_and_git_locations() { + let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let _saved = SavedEnv::capture(&[ + "HOME", + "RUSTUP_HOME", + "CARGO_HOME", + "GIT_CONFIG_GLOBAL", + "XDG_CONFIG_HOME", + ]); + let (root, daemon) = fixture(); + let base = root.parent().unwrap(); + let user_home = base.join("user-home"); + fs::create_dir_all(&user_home).unwrap(); + for name in [ + "RUSTUP_HOME", + "CARGO_HOME", + "GIT_CONFIG_GLOBAL", + "XDG_CONFIG_HOME", + ] { + std::env::remove_var(name); + } + std::env::set_var("HOME", &user_home); + + let mut sandbox = started(&root, &daemon, "layerfs-user-env-1"); + let scratch_home = format!("HOME={}", sandbox.tmp().display()); + let probe = + "printf '%s|%s|%s|%s' \"$HOME\" \"${RUSTUP_HOME:-unset}\" \"${CARGO_HOME:-unset}\" \"${GIT_CONFIG_GLOBAL:-unset}\""; + + // absent under HOME: none of the three is set, and HOME stays the Sandbox's tmp + assert_eq!( + command_script(&sandbox, probe, &[&scratch_home]), + format!("{}|unset|unset|unset", sandbox.tmp().display()) + ); + + // XDG git config under ~/.config/git/config when ~/.gitconfig is absent + let xdg_default = user_home.join(".config/git/config"); + fs::create_dir_all(xdg_default.parent().unwrap()).unwrap(); + fs::write(&xdg_default, "[user]\n\tname = XdgUser\n").unwrap(); + assert_eq!( + command_script(&sandbox, probe, &[&scratch_home]), + format!( + "{}|unset|unset|{}", + sandbox.tmp().display(), + xdg_default.display() + ) + ); + + // XDG_CONFIG_HOME/git/config when XDG_CONFIG_HOME is set + let custom_xdg = base.join("xdg-config"); + let custom_xdg_git = custom_xdg.join("git/config"); + fs::create_dir_all(custom_xdg_git.parent().unwrap()).unwrap(); + fs::write(&custom_xdg_git, "[user]\n\tname = CustomXdg\n").unwrap(); + std::env::set_var("XDG_CONFIG_HOME", &custom_xdg); + assert_eq!( + command_script(&sandbox, probe, &[&scratch_home]), + format!( + "{}|unset|unset|{}", + sandbox.tmp().display(), + custom_xdg_git.display() + ) + ); + + // ~/.rustup, ~/.cargo and ~/.gitconfig present: all three passed through, + // with ~/.gitconfig chosen ahead of XDG, while HOME remains the Sandbox's tmp + let rustup = user_home.join(".rustup"); + let cargo = user_home.join(".cargo"); + let gitconfig = user_home.join(".gitconfig"); + fs::create_dir(&rustup).unwrap(); + fs::create_dir(&cargo).unwrap(); + fs::write(&gitconfig, "[user]\n\tname = HomeUser\n").unwrap(); + assert_eq!( + command_script(&sandbox, probe, &[&scratch_home]), + format!( + "{}|{}|{}|{}", + sandbox.tmp().display(), + rustup.display(), + cargo.display(), + gitconfig.display() + ) + ); + + // caller environment variables take precedence over HOME defaults + std::env::set_var("RUSTUP_HOME", "/custom/env/rustup"); + std::env::set_var("CARGO_HOME", "/custom/env/cargo"); + std::env::set_var("GIT_CONFIG_GLOBAL", "/custom/env/gitconfig"); + assert_eq!( + command_script(&sandbox, probe, &[&scratch_home]), + format!( + "{}|/custom/env/rustup|/custom/env/cargo|/custom/env/gitconfig", + sandbox.tmp().display() + ) + ); + + // explicit request environment entries take precedence over both + assert_eq!( + command_script( + &sandbox, + probe, + &[ + &scratch_home, + "RUSTUP_HOME=/req/rustup", + "CARGO_HOME=/req/cargo", + "GIT_CONFIG_GLOBAL=/req/gitconfig", + ] + ), + format!( + "{}|/req/rustup|/req/cargo|/req/gitconfig", + sandbox.tmp().display() + ) + ); + + sandbox.stop(1).unwrap(); + sandbox.delete().unwrap(); + let _ = fs::remove_dir_all(base); +} diff --git a/control/crates/layerfs-store/src/backup/restore.rs b/control/crates/layerfs-store/src/backup/restore.rs index 79ace2aa98..988e30d2ac 100644 --- a/control/crates/layerfs-store/src/backup/restore.rs +++ b/control/crates/layerfs-store/src/backup/restore.rs @@ -41,6 +41,15 @@ pub fn restore( let home = home.to_path_buf(); let store = request.store.as_str(); layerfs_home::names::StoreName::parse(store)?; + // the Store's own directory, before this call writes anything (its + // staging folder included): a restore installs into the folder a daemon + // opens, so an existing one must be the same private directory + // `store.create` and every opener require, refused with its own code and + // remedy (#570 P1). Absent: the install makes it `0700`, as before. + if paths::store_dir(&home, store).exists() { + layerfs_store_service::kernel_guard::check(&paths::store_file(&home, store)) + .map_err(|reason| refused("Unsupported", reason.0))?; + } let bucket = request.bucket.as_str(); let driver = BucketRecord::of(&home, bucket)?.driver()?; let existing = StoreRecord::read(&home, store)?; @@ -128,20 +137,75 @@ pub fn restore( shown.phase(events, "install"); let _lock = home::lock::take(&home)?; let projects = records(&home, store, &restored, &found.projects)?; + install( + &home, + store, + bucket, + existing.as_ref(), + &record, + &staged, + related, + )?; + write_link( + &home, + store, + bucket, + StoreLink::new( + &file, + "restore", + loaded.bytes, + &loaded.sha256, + loaded.heads.as_ref(), + ), + )?; + let projects = projects.write(&home)?; + Ok(SdkValue::map([ + ("store", SdkValue::text(store)), + ("bucket", SdkValue::text(bucket)), + ("file", SdkValue::text(&file)), + ("bytes", SdkValue::Int(loaded.bytes as i64)), + ("sha256", hex(&loaded.sha256)), + ("heads", loaded.heads.as_ref().map(hex).unwrap_or_default()), + ("projects", projects), + ])) +} + +/// Installs the downloaded Store `staged` as `store` (under the home lock): +/// a new Store where `existing` is `None`, else it replaces the Store that +/// record names. +/// +/// The target's folder is checked again first: the download is a long +/// window, in which `store/` can appear or lose its privacy after the +/// restore's own first check, and `home::private_dir` keeps an existing +/// folder's mode. This is `store.create`'s guard, its code and its remedy +/// (#570 P1). +pub fn install( + home: &Path, + store: &str, + bucket: &str, + existing: Option<&StoreRecord>, + record: &StoreRecord, + staged: &Path, + related: Related, +) -> Result<()> { + if paths::store_dir(home, store).exists() { + layerfs_store_service::kernel_guard::check(&paths::store_file(home, store)) + .map_err(|reason| refused("Unsupported", reason.0))?; + } // a Store replaced here is checked and replaced under its lock, which // the link below takes again let store_lock = match &existing { - Some(_) => Some(crate::record::lock(&home, store)?), + Some(_) => Some(crate::record::lock(home, store)?), None => None, }; - let destination = paths::store_file(&home, store); - let now = StoreRecord::read(&home, store)?; + let destination = paths::store_file(home, store); + let now = StoreRecord::read(home, store)?; match (&existing, now) { (None, None) if !destination.exists() => { - home::private_dir(&paths::store_dir(&home, store))?; + home::private_dir(&paths::store_dir(home, store))?; // a link never replaces a Store that appeared meanwhile - std::fs::hard_link(&staged, &destination).map_err(failed)?; - record.write(&home)?; + std::fs::hard_link(staged, &destination).map_err(failed)?; + record.write(home)?; } (None, _) => { return Err(refused( @@ -162,33 +226,12 @@ pub fn restore( format!("Store {store} was replaced while this restore downloaded"), )); } - replaceable(&home, store, bucket, &now, related)?; - std::fs::rename(&staged, &destination).map_err(failed)?; + replaceable(home, store, bucket, &now, related)?; + std::fs::rename(staged, &destination).map_err(failed)?; } } drop(store_lock); - write_link( - &home, - store, - bucket, - StoreLink::new( - &file, - "restore", - loaded.bytes, - &loaded.sha256, - loaded.heads.as_ref(), - ), - )?; - let projects = projects.write(&home)?; - Ok(SdkValue::map([ - ("store", SdkValue::text(store)), - ("bucket", SdkValue::text(bucket)), - ("file", SdkValue::text(&file)), - ("bytes", SdkValue::Int(loaded.bytes as i64)), - ("sha256", hex(&loaded.sha256)), - ("heads", loaded.heads.as_ref().map(hex).unwrap_or_default()), - ("projects", projects), - ])) + Ok(()) } /// A restore's own download folder, `store/.restore-/` (hidden from diff --git a/control/crates/layerfs-store/src/identity.rs b/control/crates/layerfs-store/src/identity.rs index fc972569a9..a6d233fa3a 100644 --- a/control/crates/layerfs-store/src/identity.rs +++ b/control/crates/layerfs-store/src/identity.rs @@ -200,21 +200,50 @@ pub fn sqlite_only(identity: &Identity, what: &str) -> Result<()> { } } +/// Whether `make_empty`'s own `mkdir` made the Store's directory: the only +/// ownership that lets its caller reclaim the directory when nothing was +/// published. A directory another caller (or the user) made is never this +/// call's to remove. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum MadeDirectory { + /// This call's `mkdir` created it. + ThisCall, + /// It was already there. + AlreadyThere, +} + /// Creates and seals the empty Store at `.creating`, then -/// renames it into place; a failure removes this call's own file. +/// renames it into place; a failure removes this call's own temporary file. +/// Also says whether this call's own `mkdir` made `global`, which is the only +/// ownership that may reclaim the directory. pub fn make_empty( global: &Path, destination: &Path, identity: &Identity, policy: layerfs_storage::StoragePolicy, +) -> (Result, MadeDirectory) { + let mut made = MadeDirectory::AlreadyThere; + let result = create_empty(global, destination, identity, policy, &mut made); + (result, made) +} + +/// [`make_empty`]'s steps; `made` records whether this call's own `mkdir` +/// created `global`. +fn create_empty( + global: &Path, + destination: &Path, + identity: &Identity, + policy: layerfs_storage::StoragePolicy, + made: &mut MadeDirectory, ) -> Result { - use std::os::unix::fs::{DirBuilderExt, PermissionsExt}; - if !global.exists() { - std::fs::DirBuilder::new() - .mode(0o700) - .create(global) - .map_err(failed)?; - } + use std::os::unix::fs::PermissionsExt; + // the one ownership rule an install uses too: this call's own `mkdir` + *made = match layerfs_store_service::install::make_private_directory(global).map_err(failed)? { + layerfs_store_service::install::Made::ThisCall => MadeDirectory::ThisCall, + layerfs_store_service::install::Made::AlreadyThere => MadeDirectory::AlreadyThere, + }; + layerfs_store_service::kernel_guard::check(destination) + .map_err(|reason| refused("Unsupported", reason.0))?; let (target, _) = layerfs_store_service::install::paths(destination).map_err(failed)?; layerfs_store_service::install::absent(&target) .map_err(|error| refused("Exists", format!("{}: {error}", target.display())))?; diff --git a/control/crates/layerfs-store/src/project/add.rs b/control/crates/layerfs-store/src/project/add.rs index 4bf3122d22..af7e24516f 100644 --- a/control/crates/layerfs-store/src/project/add.rs +++ b/control/crates/layerfs-store/src/project/add.rs @@ -131,8 +131,7 @@ pub fn add_project(request: &ProjectAdd) -> Outcome { let deadline = crate::project::init::deadline(request.timeout_ms)?; let open = request.open(); let store_file = request.store_file(); - let global = store_file.parent().unwrap_or(&store_file); - layerfs_store_service::kernel_guard::check(global) + layerfs_store_service::kernel_guard::check(&store_file) .map_err(|reason| refused("Unsupported", reason.0))?; let identity = identity(&open)?; sqlite_only(&identity, "adding a project")?; diff --git a/control/crates/layerfs-store/src/sqlite/create.rs b/control/crates/layerfs-store/src/sqlite/create.rs index 4928f17e40..1f83304f7a 100644 --- a/control/crates/layerfs-store/src/sqlite/create.rs +++ b/control/crates/layerfs-store/src/sqlite/create.rs @@ -14,7 +14,9 @@ pub fn create(home: &Path, name: &StoreName) -> Outcome { return Err(refused("Exists", format!("a Store named {name} exists"))); } let directory = paths::store_dir(home, name); - home::private_dir(&directory)?; + if let Some(parent) = directory.parent() { + home::private_dir(parent)?; + } let identity = crate::identity::Identity { kind: crate::identity::Kind::Sqlite, binding: layerfs_home::helpers::random()?.to_vec(), @@ -22,11 +24,15 @@ pub fn create(home: &Path, name: &StoreName) -> Outcome { }; let file = paths::store_file(home, name); let policy = layerfs_storage::StoragePolicy::frozen_default(); - let sealed = match crate::identity::make_empty(&directory, &file, &identity, policy) { + let (created, made) = crate::identity::make_empty(&directory, &file, &identity, policy); + let sealed = match created { Ok(sealed) => sealed, Err(refusal) => { - // nothing was published: this call's own directory goes - let _ = std::fs::remove_dir_all(&directory); + // nothing was published: the directory goes only when this call's + // own mkdir made it, never one another caller made meanwhile + if made == crate::identity::MadeDirectory::ThisCall { + let _ = std::fs::remove_dir_all(&directory); + } return Err(refusal); } }; diff --git a/control/crates/layerfs-store/tests/create_directory.rs b/control/crates/layerfs-store/tests/create_directory.rs new file mode 100644 index 0000000000..06db7ed906 --- /dev/null +++ b/control/crates/layerfs-store/tests/create_directory.rs @@ -0,0 +1,92 @@ +//! `store.create`'s directory (#570 P1): `make_empty` hands back whether this +//! call's own `mkdir` made `store/`, and only that ownership may reclaim +//! the directory when nothing was published. The guard refuses tmpfs by +//! filesystem, after the `mkdir` succeeded, so it shows both sides; a `mkdir` +//! that fails on a taken name claims nothing and removes nothing. +use layerfs_home::home::paths; +use layerfs_home::names::StoreName; +use std::path::{Path, PathBuf}; + +fn scratch(parent: &Path, label: &str) -> PathBuf { + use std::os::unix::fs::DirBuilderExt; + let directory = parent.join(format!("layerfs-create-{label}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&directory); + std::fs::DirBuilder::new() + .recursive(true) + .mode(0o700) + .create(&directory) + .unwrap(); + directory +} + +/// A `mkdir` that fails removes nothing: the Store's directory name is taken +/// already, by a dangling symbolic link here — `Path::exists` looks through +/// the link and sees nothing, so `make_empty` reaches its own `mkdir`, which +/// returns `AlreadyExists` and so claims no directory. The link, its target +/// and its surroundings stay as they were, and no Store is created. +#[cfg(unix)] +#[test] +fn a_mkdir_failure_removes_nothing() { + let home = scratch(&std::env::temp_dir(), "taken-name"); + layerfs_home::home::layout::create(&home).unwrap(); + let name = StoreName::parse("taken").unwrap(); + let directory = paths::store_dir(&home, &name); + let absent_target = home.join("link-target"); + std::fs::create_dir(home.join("store")).unwrap(); + std::os::unix::fs::symlink(&absent_target, &directory).unwrap(); + let refusal = layerfs_store::sqlite::create::create(&home, &name).unwrap_err(); + assert_eq!(refusal.code, "Failed", "{}", refusal.message); + assert!( + refusal.message.contains("File exists"), + "{}", + refusal.message + ); + let link = std::fs::symlink_metadata(&directory).unwrap(); + assert!(link.file_type().is_symlink(), "the taken name stays"); + assert_eq!(std::fs::read_link(&directory).unwrap(), absent_target); + assert!( + std::fs::symlink_metadata(&absent_target).is_err(), + "the call creates no directory of its own" + ); + std::fs::remove_dir_all(&home).unwrap(); +} + +#[cfg(target_os = "linux")] +#[test] +fn a_refused_create_removes_only_the_directory_this_call_made() { + use std::os::unix::fs::DirBuilderExt; + // a home on tmpfs: `make_empty`'s own `0700` mkdir succeeds and the kernel + // guard then refuses the filesystem + let home = scratch(Path::new("/dev/shm"), "tmpfs-home"); + layerfs_home::home::layout::create(&home).unwrap(); + let name = StoreName::parse("tmp").unwrap(); + let refusal = layerfs_store::sqlite::create::create(&home, &name).unwrap_err(); + assert_eq!(refusal.code, "Unsupported", "{}", refusal.message); + assert!( + refusal.message.contains("ext4, xfs or btrfs"), + "{}", + refusal.message + ); + let directory = paths::store_dir(&home, &name); + assert!( + !directory.exists(), + "the empty directory this call made is reclaimed" + ); + assert!( + paths::store_dir(&home, &name).parent().unwrap().is_dir(), + "store/ itself stays" + ); + + // a directory this call did not make, and what it holds, stay + std::fs::DirBuilder::new() + .mode(0o700) + .create(&directory) + .unwrap(); + let marker = directory.join("another callers file"); + std::fs::write(&marker, b"another caller's file").unwrap(); + let refusal = layerfs_store::sqlite::create::create(&home, &name).unwrap_err(); + assert_eq!(refusal.code, "Unsupported", "{}", refusal.message); + assert!(directory.is_dir(), "the other caller's directory stays"); + assert_eq!(std::fs::read(&marker).unwrap(), b"another caller's file"); + std::fs::remove_dir_all(home).unwrap(); +} diff --git a/control/crates/layerfs-store/tests/diagnostic_structure.rs b/control/crates/layerfs-store/tests/diagnostic_structure.rs index 3d2caa060d..385882fb9d 100644 --- a/control/crates/layerfs-store/tests/diagnostic_structure.rs +++ b/control/crates/layerfs-store/tests/diagnostic_structure.rs @@ -3,9 +3,24 @@ use layerfs_store::{ diagnostic::{self, Options, Outcome}, identity::{Identity, Kind}, }; + +/// A private (`0700`) scratch directory for one Store file: the kernel guard +/// admits a Store's own directory only, as in a home. +fn scratch(label: &str) -> std::path::PathBuf { + use std::os::unix::fs::DirBuilderExt; + let directory = + std::env::temp_dir().join(format!("layerfs-doctor-{label}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&directory); + std::fs::DirBuilder::new() + .mode(0o700) + .create(&directory) + .unwrap(); + directory +} + #[test] fn selected_malformed_store_has_structural_failure_and_incomplete_access() { - let path = std::env::temp_dir().join(format!("layerfs-doctor-header-{}", std::process::id())); + let path = scratch("header").join("store.sqlite"); std::fs::write(&path, vec![0xA5; 8192]).unwrap(); let before = std::fs::read(&path).unwrap(); let mut observations = Vec::new(); @@ -27,14 +42,14 @@ fn selected_malformed_store_has_structural_failure_and_incomplete_access() { "{observations:?}" ); assert_eq!(std::fs::read(&path).unwrap(), before); - std::fs::remove_file(path).unwrap(); + std::fs::remove_dir_all(path.parent().unwrap()).unwrap(); } #[test] fn binding_mismatch_keeps_logical_access_unavailable_without_corruption() { use layerfs_persistence::{PersistenceConfig, SqliteHandles, SqlitePersistenceProfile}; use layerfs_storage::StoragePolicy; - let path = std::env::temp_dir().join(format!("layerfs-doctor-binding-{}", std::process::id())); + let path = scratch("binding").join("store.sqlite"); let handles = SqliteHandles::create( PersistenceConfig::sqlite(&path).with_sqlite_profile(SqlitePersistenceProfile::Disposable), StoragePolicy::frozen_default(), @@ -72,5 +87,5 @@ fn binding_mismatch_keeps_logical_access_unavailable_without_corruption() { "{observations:?}" ); assert_eq!(std::fs::read(&path).unwrap(), before); - std::fs::remove_file(path).unwrap(); + std::fs::remove_dir_all(path.parent().unwrap()).unwrap(); } diff --git a/control/crates/layerfs-store/tests/restore_install.rs b/control/crates/layerfs-store/tests/restore_install.rs new file mode 100644 index 0000000000..2364f2e8c3 --- /dev/null +++ b/control/crates/layerfs-store/tests/restore_install.rs @@ -0,0 +1,194 @@ +//! The install step of `backup.restore` (#570 P1, review P2-1). The restore +//! checks `store/` when it starts, but the download between that check +//! and the install is a long window: the folder can appear, or lose its +//! privacy, while it runs, and `home::private_dir` keeps an existing folder's +//! mode. The install therefore checks the target's folder again, under the +//! home lock, before it links the Store into it: a folder that is not the +//! daemon's private one is refused with `store.create`'s code and remedy and +//! nothing is installed; a private one, and an absent one (made `0700`), are +//! installed. These tests run in an ordinary user's process: no bucket, no +//! install permission, no timing. +use layerfs_bridge::control::SdkRefusal; +use layerfs_home::home::{self, paths}; +use layerfs_store::backup::restore::install; +use layerfs_store::record::StoreRecord; +use layerfs_store::Related; +use std::os::unix::fs::{DirBuilderExt, PermissionsExt}; +use std::path::{Path, PathBuf}; + +/// A home whose own folder is private, as `backup.restore` requires. +fn scratch(label: &str) -> PathBuf { + let home = std::env::temp_dir().join(format!( + "layerfs-restore-install-{label}-{}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&home); + std::fs::DirBuilder::new() + .recursive(true) + .mode(0o700) + .create(&home) + .unwrap(); + home::layout::create(&home).unwrap(); + home::private_dir(&home.join("store")).unwrap(); + home +} + +/// The `InUse` checks a new Store's install never asks. +fn unused(home: &Path, name: &str) -> Result<(), SdkRefusal> { + panic!( + "a new Store's install asks no user: {} in {}", + name, + home.display() + ); +} + +fn related() -> Related { + Related { + store_idle: unused, + can_remove_project: unused, + } +} + +/// The downloaded Store, staged as `backup.restore` leaves it before the +/// install. +fn staged(home: &Path) -> PathBuf { + let folder = home.join("store/.restore-fixture"); + home::private_dir(&folder).unwrap(); + let file = folder.join("store.sqlite"); + std::fs::write(&file, b"the downloaded Store\n").unwrap(); + file +} + +fn mode(path: &Path) -> u32 { + std::fs::metadata(path).unwrap().permissions().mode() & 0o7777 +} + +fn set_mode(path: &Path, mode: u32) { + std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)).unwrap(); +} + +/// `store/` made `0755` while the restore downloads: the install +/// refuses it, with `store.create`'s `Unsupported` and `chmod 700` remedy, +/// before it links the Store or writes the record. The user's folder keeps +/// its mode, nothing of the restore is installed and the staged file stays +/// for the restore to remove with its staging folder; once the folder is +/// private again the same install goes through. +#[test] +fn a_folder_made_unprivate_during_the_download_is_refused_before_the_install() { + let home = scratch("window"); + let store = "shop"; + let directory = paths::store_dir(&home, store); + let staged = staged(&home); + let record = StoreRecord::restored(store, b"binding", &[7; 32]); + let _lock = home::lock::take(&home).unwrap(); + + // the restore's own first check saw no folder; the download ran + assert!(!directory.exists()); + std::fs::create_dir(&directory).unwrap(); + set_mode(&directory, 0o755); + + let refusal = install(&home, store, "bucket", None, &record, &staged, related()) + .expect_err("a 0755 folder is not the daemon's private directory"); + assert_eq!(refusal.code, "Unsupported", "{}", refusal.message); + assert!( + refusal.message.contains(&directory.display().to_string()), + "{}", + refusal.message + ); + assert!( + refusal.message.contains("0755") && refusal.message.contains("0700"), + "{}", + refusal.message + ); + assert!( + refusal + .message + .contains(&format!("chmod 700 {}", directory.display())), + "{}", + refusal.message + ); + assert_eq!(mode(&directory), 0o755, "the folder's mode is not changed"); + assert!(!paths::store_file(&home, store).exists()); + assert!(!paths::store_record(&home, store).exists()); + assert_eq!( + std::fs::read(&staged).unwrap(), + b"the downloaded Store\n", + "nothing was linked out of the staging folder" + ); + + // the folder made private again: this install is the one that goes in + set_mode(&directory, 0o700); + install(&home, store, "bucket", None, &record, &staged, related()).unwrap(); + assert_eq!(mode(&directory), 0o700); + assert_eq!( + std::fs::read(paths::store_file(&home, store)).unwrap(), + b"the downloaded Store\n" + ); + let written = StoreRecord::read(&home, store).unwrap().unwrap(); + assert_eq!(written.binding(), Some(&b"binding"[..])); + let _ = std::fs::remove_dir_all(&home); +} + +/// A Store this restore replaces lost its privacy during the download: its +/// own folder is refused before the replacement's checks run, so the download +/// is not installed over the Store either. +#[test] +fn a_replaced_stores_folder_is_checked_again_before_the_install() { + let home = scratch("replacing"); + let store = "shop"; + let directory = paths::store_dir(&home, store); + let staged = staged(&home); + let existing = StoreRecord::restored(store, b"binding", &[9; 32]); + existing.write(&home).unwrap(); + set_mode(&directory, 0o755); + let _lock = home::lock::take(&home).unwrap(); + + let refusal = install( + &home, + store, + "bucket", + Some(&existing), + &existing, + &staged, + related(), + ) + .expect_err("a 0755 folder is not the daemon's private directory"); + assert_eq!(refusal.code, "Unsupported", "{}", refusal.message); + assert!(refusal.message.contains("chmod 700"), "{}", refusal.message); + // the guard's refusal, not the replacement's own: the Store was not + // reached, so it still holds what it had and its folder its mode + assert!( + !refusal.message.contains("changed since"), + "{}", + refusal.message + ); + assert!(!paths::store_file(&home, store).exists()); + assert_eq!(mode(&directory), 0o755); + assert_eq!( + StoreRecord::read(&home, store).unwrap().unwrap().binding(), + Some(&b"binding"[..]) + ); + let _ = std::fs::remove_dir_all(&home); +} + +/// A folder the download left absent is made `0700` by the install, which is +/// the restore's own new-Store path: the Store file and its record are +/// written, and the folder is one every opener accepts. +#[test] +fn an_absent_folder_is_made_private_by_the_install() { + let home = scratch("absent"); + let store = "shop"; + let staged = staged(&home); + let record = StoreRecord::restored(store, b"binding", &[3; 32]); + let _lock = home::lock::take(&home).unwrap(); + assert!(!paths::store_dir(&home, store).exists()); + + install(&home, store, "bucket", None, &record, &staged, related()).unwrap(); + assert_eq!(mode(&paths::store_dir(&home, store)), 0o700); + assert_eq!( + std::fs::read(paths::store_file(&home, store)).unwrap(), + b"the downloaded Store\n" + ); + assert!(paths::store_record(&home, store).is_file()); + let _ = std::fs::remove_dir_all(&home); +} diff --git a/docs/architecture/api/sdk/backup.md b/docs/architecture/api/sdk/backup.md index 28125ffd18..a20a1ddc96 100644 --- a/docs/architecture/api/sdk/backup.md +++ b/docs/architecture/api/sdk/backup.md @@ -150,6 +150,11 @@ CLI layerfs backup restore [] [--progress] ``` `file` defaults to the file the Store's link names, else the Store's name. +If `store/` already exists, it runs `store.create`'s kernel guard +before staging anything and again under the home lock before the install +(`backup::restore::install`), refusing `Unsupported` (with `chmod 700`) if +the folder is not a real `0700` directory of this user on a local +filesystem. Phases: `check`, `download` (bytes, pieces), `verify` (the bytes open as the Store), `install`. `projects` lists the projects the restored file holds; each project's record is rebuilt as `project.init` writes it (carrying its @@ -172,7 +177,7 @@ second restore of the same backup completes them: read `store.get` first | Error | When | | --- | --- | -| `Unsupported` | the Store to replace is not on `native` | +| `Unsupported` | `store/` already exists and is not the daemon's private `0700` directory (checked before staging and again under the home lock before the install); or the Store to replace is not on `native` | | `InUse` | a process holds the Store, a Sandbox of it is recorded, or its `-wal`/`-shm` exists | | `Conflict` | the Store changed since its last transfer through this bucket, has no link, or was replaced during the download | | `Exists` | a Store of that name was made during the download | diff --git a/docs/architecture/api/sdk/project.md b/docs/architecture/api/sdk/project.md index 71519f1b80..e300021835 100644 --- a/docs/architecture/api/sdk/project.md +++ b/docs/architecture/api/sdk/project.md @@ -74,7 +74,7 @@ Branch's base) and `root` (that Branch's effective root) | Store | Runs in | | --- | --- | | PostgreSQL | caller's process (a build with the `postgresql` feature) | -| sqlite on a locally executing machine (`native` on Linux or macOS) | caller's process; the kernel guard admits only Linux ext4, xfs or btrfs, or macOS apfs or hfs | +| sqlite on a locally executing machine (`native` on Linux or macOS) | caller's process; the kernel guard admits the Store's directory only when it is owned by this user and private (`0700`) on Linux ext4, xfs or btrfs, or macOS apfs or hfs | | sqlite on any other machine, and `native` on Windows | that machine's worker, on a staged copy of the folder sent from the caller | | Error | When | Source | @@ -84,7 +84,7 @@ Branch's base) and `root` (that Branch's effective root) | `Exists` | the project's record, entry or LayerStack name exists | `project/family.rs`, [`identity.rs`](../../../../control/crates/layerfs-store/src/identity.rs) | | `Busy` | also when another add's working rows remain in the Store | `identity.rs` `add_failure` | | `Capacity` | worker route: the machine's free disk is below the copy plus headroom | `project/send.rs` `room` | -| `Unsupported` | the kernel guard refuses the Store's filesystem; PostgreSQL in a build without `postgresql` | [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | +| `Unsupported` | the kernel guard refuses the Store's directory (non-private mode/owner/type or unsupported filesystem); PostgreSQL in a build without `postgresql` | [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | | `NoStore` | no Store file at the Store's path | `identity.rs` | | `Failed` | deadline passed, read error or any other engine failure, **including an Init whose outcome is unknown**; the entry this call made is kept | `identity.rs` `add_failure` | | `Unknown` | worker route: a stage or the `project.init` answer was lost; the home gains no record, and the worker removes its staged copy when its Init ends | [`worker/client.rs`](../../../../control/crates/layerfs-machine/src/worker/client.rs) | @@ -254,7 +254,7 @@ under the Store's lease, locally or as the worker operation `project.remove`. | --- | --- | --- | | `NoSuchProject` | also when the Store holds no LayerStack of that name | [`identity.rs`](../../../../control/crates/layerfs-store/src/identity.rs) | | `InUse` | a Sandbox of the project is recorded; `detail.related` lists `{ kind: "sandbox", name }` | [`inventory.rs`](../../../../control/crates/layerfs-control/src/inventory.rs) | -| `Unsupported` | a PostgreSQL Store ("removing a project from a PostgreSQL Store is not built"); the kernel guard refuses the Store's filesystem | [`project/family.rs`](../../../../control/crates/layerfs-store/src/project/family.rs) | +| `Unsupported` | a PostgreSQL Store ("removing a project from a PostgreSQL Store is not built"); the kernel guard refuses the Store's directory when opened (non-private mode/owner/type — another user's directory or group/other access instead of `0700` — or unsupported filesystem) | [`project/family.rs`](../../../../control/crates/layerfs-store/src/project/family.rs) | | `Failed` | the Store write failed, its outcome included | `identity.rs` `remove_project` | | `Unknown` | worker route: the answer was lost | [`worker/client.rs`](../../../../control/crates/layerfs-machine/src/worker/client.rs) | diff --git a/docs/architecture/api/sdk/store.md b/docs/architecture/api/sdk/store.md index efca03c22a..9ecdbe155b 100644 --- a/docs/architecture/api/sdk/store.md +++ b/docs/architecture/api/sdk/store.md @@ -172,14 +172,21 @@ CLI layerfs store create Returns `{ name, kind: "sqlite", machine: "native", profile: "disposable", created, sqlite, projects: [] }`. Under the home lock (waits up to 120 s) -the call makes `store//` (0700), builds the empty file as -`store.sqlite.creating` (0600) and renames it to `store.sqlite`; a failure -before the record is written removes the call's folder +the call makes `store/` (`0700`), creates `store//` (`0700`) when +absent and checks `store//store.sqlite` with `kernel_guard::check` +before building the empty file as `store.sqlite.creating` (`0600`) and +renaming it to `store.sqlite`. A failure of `make_empty` removes +`store//` only when this call's own `mkdir` made it (a pre-existing +folder or one made by another caller is never removed); once `make_empty` +succeeds, a `save_record` failure keeps the Store file and folder ([`sqlite/create.rs`](../../../../control/crates/layerfs-store/src/sqlite/create.rs), [`identity.rs`](../../../../control/crates/layerfs-store/src/identity.rs)). To place it elsewhere, create it and then push it. Errors: `Exists` (a -record of that name, of either kind, or a file at the target); `Unavailable` -(a lock held 120 s); `Failed`. +record of that name, of either kind, or a file at the target); +`Unsupported` (the kernel guard refuses `store//`: not on a local +lock-carrying filesystem, or an existing directory is not owned by this +user, is a symlink, or has group/other access instead of `0700`); +`Unavailable` (a lock held 120 s); `Failed`. ### store.connect diff --git a/docs/architecture/api/sdk/workspace.md b/docs/architecture/api/sdk/workspace.md index 4fac960411..d7352dead2 100644 --- a/docs/architecture/api/sdk/workspace.md +++ b/docs/architecture/api/sdk/workspace.md @@ -90,7 +90,7 @@ CLI layerfs workspace exec [--message ] [--stream] [ | Argument | Kind | Rule | | --- | --- | --- | -| `command` | Text | `bash -c ` as the Sandbox's command identity, `HOME` its scratch folder ([`exec/run.rs`](../../../../control/crates/layerfs-sandbox/src/exec/run.rs)) | +| `command` | Text | `bash -c ` as the Sandbox's command identity, `HOME` its scratch folder (on macOS `native`, `RUSTUP_HOME`, `CARGO_HOME` and `GIT_CONFIG_GLOBAL` default to the caller's when present; override in the caller's environment or the command) ([`exec/run.rs`](../../../../control/crates/layerfs-sandbox/src/exec/run.rs)) | | `message` option | Text | echoed in the answer; no Commit is made | | `stream` option | Bool | pass output to this process's stdout and stderr when no functions are given (CLI `--stream`) | | `stdout`, `stderr` options | Function | receive each output event's text, or bytes when not UTF-8; never reach the library | diff --git a/docs/architecture/control/backups/README.md b/docs/architecture/control/backups/README.md index ad8d7161f0..d0fa38fb94 100644 --- a/docs/architecture/control/backups/README.md +++ b/docs/architecture/control/backups/README.md @@ -76,12 +76,12 @@ A PostgreSQL Store is not backed up here. | # | Step | Source | | --- | --- | --- | -| 1 | Existing Store: refuse PostgreSQL; under the home lock and the Store's lock check `replaceable`: on `native`, no unfinished operation, this process's open copy closed, no other lease holder or running Sandbox (`store_idle`), heads equal to the link's, no `-wal` or `-shm`. Then hold its lease. A new Store gets a fresh cursor key. | [`restore.rs`](../../../../control/crates/layerfs-store/src/backup/restore.rs), [`inventory.rs`](../../../../control/crates/layerfs-control/src/inventory.rs) | +| 1 | Existing `store/` folder (if present): `store.create`'s kernel guard requires a real `0700` directory of this user (`Unsupported`, `chmod 700` before any staging). Existing Store: refuse PostgreSQL; under the home lock and the Store's lock check `replaceable`: on `native`, no unfinished operation, this process's open copy closed, no other lease holder or running Sandbox (`store_idle`), heads equal to the link's, no `-wal` or `-shm`. Then hold its lease. A new Store gets a fresh cursor key. | [`restore.rs`](../../../../control/crates/layerfs-store/src/backup/restore.rs), [`inventory.rs`](../../../../control/crates/layerfs-control/src/inventory.rs) | | 2 | Make `store/.restore-/` with a held `lock` file; first remove older restore folders whose lock can be taken (killed restores). | same, `Staging`, `sweep` | -| 3 | `load` into it: kernel guard on the folder, installer is the daemons' user, no Store at the target, no sidecars; stat the object and take `layerfs-binding` and `layerfs-sqlite`; download to `.pulling` (0600, create-new) through `pieces::download`. | [`restore.rs` (bucket)](../../../../control/crates/layerfs-bucket/src/backup/restore.rs) | +| 3 | `load` into it: `installer_user` (daemons' user), make the destination directory `0700` when absent (`install::make_private_directory`), `kernel_guard` on the Store file (a guard refusal removes only the still-empty directory this call's `mkdir` made; installer-user or `mkdir` failure removes nothing), no Store at the target, no sidecars; stat the object and take `layerfs-binding` and `layerfs-sqlite`; download to `.pulling` (0600, create-new) through `pieces::download`. | [`restore.rs` (bucket)](../../../../control/crates/layerfs-bucket/src/backup/restore.rs) | | 4 | `verify`: open the bytes as a Disposable Store with that binding and cursor key, close, read heads, require no `-wal`/`-shm` left; `install`: hard link into place. | same | | 5 | The binding must be the existing Store's. Read the Store's projects and each one's `main` Branch on a connection of the call's own. | [`restore.rs`](../../../../control/crates/layerfs-store/src/backup/restore.rs), `projects_in` | -| 6 | Under the home lock: plan each project's record before the install (rebuilt as `project.init` writes it with `manifest` and `installed` from `main`, binding, cursor key, file length and `layerfs-sqlite` version; complete records kept, incomplete ones completed, no `main` keeps the listing record; an unencodable manifest refuses here). A new Store is hard-linked and recorded; replacing an existing Store takes the Store lock to re-check binding and `replaceable` and rename over, then drops it. `write_link` takes the Store lock on its own; `Records::write` writes the planned project records and removes records of absent projects under the home lock alone. The staging folder goes when the call ends. | [`restore.rs`](../../../../control/crates/layerfs-store/src/backup/restore.rs), [`restored.rs`](../../../../control/crates/layerfs-store/src/backup/restored.rs), [`family.rs`](../../../../control/crates/layerfs-store/src/backup/family.rs) | +| 6 | Under the home lock: plan each project's record before the install (rebuilt as `project.init` writes it with `manifest` and `installed` from `main`, binding, cursor key, file length and `layerfs-sqlite` version; complete records kept, incomplete ones completed, no `main` keeps the listing record; an unencodable manifest refuses here). Then `backup::restore::install` re-checks the target's folder with the kernel guard, so a folder that appeared or lost its privacy during the download is refused `Unsupported` with nothing installed. A new Store is hard-linked (never over one that appeared) and recorded; replacing an existing Store takes the Store lock to re-check binding and `replaceable` and rename over, then drops it. `write_link` takes the Store lock on its own; `Records::write` writes the planned project records and removes records of absent projects under the home lock alone. The staging folder goes when the call ends. | [`restore.rs`](../../../../control/crates/layerfs-store/src/backup/restore.rs), [`restored.rs`](../../../../control/crates/layerfs-store/src/backup/restored.rs), [`family.rs`](../../../../control/crates/layerfs-store/src/backup/family.rs) | The download, its checks and reading the Store's projects run before the home lock; the home lock covers planning each project's record (without the Store @@ -124,7 +124,7 @@ Bucket errors map to call refusals: `Uncertain` is `Unknown`, `Invalid` is | Create: snapshot fails | `Failed` "snapshot: …" | [`steps.rs`](../../../../control/crates/layerfs-store/src/sqlite/transfer/steps.rs) | | Create: fetched worker copy differs | `Failed` "the staged copy is not the snapshot the worker made" | [`create.rs`](../../../../control/crates/layerfs-store/src/backup/create.rs) | | Create: a request's answer is lost | `Unknown`; the object may or may not be replaced; not resent | [`record.rs`](../../../../control/crates/layerfs-store/src/backup/record.rs) | -| Restore over a Store not on `native` | `Unsupported`; move it there first | `replaceable` in [`restore.rs`](../../../../control/crates/layerfs-store/src/backup/restore.rs) | +| Restore over a Store not on `native`, or into a non-private `store/` folder | `Unsupported`; move to `native` or `chmod 700` the Store's folder (checked before staging and again under the home lock before install) | `replaceable`, `install` in [`restore.rs`](../../../../control/crates/layerfs-store/src/backup/restore.rs) | | Restore over a changed Store | `Conflict`; back it up first or restore under a new name | same | | Restore over an open Store (lease holder, Sandbox, `-wal`/`-shm`) | `InUse` | same; [`inventory.rs`](../../../../control/crates/layerfs-control/src/inventory.rs) | | Restore: any load check (missing object, no binding, sha256, open) | `Failed` naming the check, e.g. "no Store `` on the remote: remote file missing"; the staging folder is removed | [`restore.rs` (bucket)](../../../../control/crates/layerfs-bucket/src/backup/restore.rs) | diff --git a/docs/architecture/control/doctor/checks.md b/docs/architecture/control/doctor/checks.md index 725594ff9d..b5b6ac634c 100644 --- a/docs/architecture/control/doctor/checks.md +++ b/docs/architecture/control/doctor/checks.md @@ -58,7 +58,8 @@ without options. | --- | --- | --- | | each record | `error` when it does not parse ("repair or remove it by hand"), or when an entity record's `v` is not 1 ("use the LayerFS build that wrote it") | [`scan.rs`](../../../../control/crates/layerfs-control/src/doctor/scan.rs) `record` | | record file mode | `error` when group or others may read it; fix `chmod 600 ` | [`scan.rs`](../../../../control/crates/layerfs-control/src/doctor/scan.rs) `file_mode` | -| folder mode | `error` when others may list or change it or its group may use it (0701 is allowed, for a namespaces Sandbox); fix `chmod g-rwx,o-rw ` | [`scan.rs`](../../../../control/crates/layerfs-control/src/doctor/scan.rs) `folder` | +| folder mode | `error` when others may list or change it or its group may use it (0701 is allowed, for a namespaces Sandbox, except on a Store's own folder; a symlink is reported as a link with `make it a real folder`, never `chmod`, which would change the target); fix `chmod g-rwx,o-rw ` | [`scan.rs`](../../../../control/crates/layerfs-control/src/doctor/scan.rs) `folder`, `link` | +| Store folder mode | `error` when a Store's own folder has any group or other bit (`0701` is not allowed; fix `chmod 700 `) or is a symlink (scanned under `store/`, reported without descending into `record.json` or `project/`, and without suggesting `chmod`) | [`scan.rs`](../../../../control/crates/layerfs-control/src/doctor/scan.rs) `store_folder`, `store_names` | | Store folder | `error` on each project in a Store folder with no Store record; fix: restore the record or delete the folder by hand | [`scan.rs`](../../../../control/crates/layerfs-control/src/doctor/scan.rs) `store` | | Workspace folder | `warning` for `workspace//` with no `.json` beside it; fix: its Workspace was unmounted, remove the folder by hand | [`scan.rs`](../../../../control/crates/layerfs-control/src/doctor/scan.rs) `execs` | | command record | `warning` when its owner process is gone; fix `layerfs workspace exec-cancel : `, or the Workspace's unmount or the Sandbox's close; `error` when a file under `exec/` does not read as one | [`scan.rs`](../../../../control/crates/layerfs-control/src/doctor/scan.rs) `execs` | diff --git a/docs/architecture/control/sandboxes/daemon-channel-and-exec.md b/docs/architecture/control/sandboxes/daemon-channel-and-exec.md index cf0308755a..0fe217ae48 100644 --- a/docs/architecture/control/sandboxes/daemon-channel-and-exec.md +++ b/docs/architecture/control/sandboxes/daemon-channel-and-exec.md @@ -95,6 +95,7 @@ No step is retried, rebound or re-attached on the caller's behalf. | Item | Rule | Source | | --- | --- | --- | | Exec record | `/workspace//exec/.json`: `{ v, id, workspace, path, name, owner, route, stop, started, cancel? }`. `owner` is pid, start time and boot; `route` is `local` or `machine:`; `stop` is null until spawn. Kept on the side that keeps the Workspace's record. | [`exec/record.rs`](../../../../control/crates/layerfs-sandbox/src/exec/record.rs) | +| Environment | Caller variables are cleared except `PATH`; on macOS `HostProcess` (`native`), `RUSTUP_HOME`, `CARGO_HOME` and `GIT_CONFIG_GLOBAL` are passed from the caller's environment or `HOME` (`~/.rustup`, `~/.cargo`, `~/.gitconfig` or XDG `git/config`) when present and not overridden by `request.environment`. Those are the caller's own writable directories, not copies, so `cargo install`, `rustup update` or git writes change the host toolchain. | [`host_process/exec.rs`](../../../../control/crates/layerfs-sandbox-backend/src/backend/host_process/exec.rs) | | Duplicate id | A live record of the id is `Exists`, found by path, with no scan. | `Running::start` | | Stop handle | `{ cgroup: { path } }` (namespaces: `/ws-/exec-`) or `{ group: { pgid, pid, pid_start, pid_boot } }` (native); written only once the command runs in it. | `stop_value` in `exec/record.rs`, [Backends](backends.md#stoprs-stop-handles) | | Output | Each stream kept in memory up to 1 MiB (`INLINE`), then spilled to an unlinked file; read back in pages of at most 1 MiB until released. The backend reads the pipe only as fast as the spool takes it (8 KiB windows). `truncated` is always false. | [`output.rs`](../../../../control/crates/layerfs-sandbox/src/output.rs), [`exec/run.rs`](../../../../control/crates/layerfs-sandbox/src/exec/run.rs) | diff --git a/docs/architecture/control/stores-and-projects/README.md b/docs/architecture/control/stores-and-projects/README.md index f2f102c052..b14bdba469 100644 --- a/docs/architecture/control/stores-and-projects/README.md +++ b/docs/architecture/control/stores-and-projects/README.md @@ -100,7 +100,7 @@ refusal names the clearing call: a new `store push ` or `store pull | The Store lock is taken after the home lock, never before, and never held across a worker call or byte transfer | [`record.rs`](../../../../control/crates/layerfs-store/src/record.rs) `lock` | | No call starts a machine: a worker not recorded or not answering is `Unavailable`, naming `layerfs machine start` | [`place.rs`](../../../../control/crates/layerfs-store/src/place.rs) | | A guest never reads the home: the caller sends the Store's record with each call; the worker finds the file under its own root by name | [`call.rs`](../../../../control/crates/layerfs-store/src/call.rs), [`worker_server.rs`](../../../../control/crates/layerfs-control/src/worker_server.rs) | -| Direct Store access only where every opener shares one kernel and a local filesystem: Linux ext4, xfs, btrfs; macOS apfs, hfs; else every Store call is `Unsupported` with its reason, no fallback | [`service.rs`](../../../../control/crates/layerfs-store/src/service.rs), [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | +| Direct Store access only where the Store's own directory is a real `0700` directory owned by this user and every opener shares one kernel and a local filesystem: Linux ext4, xfs, btrfs; macOS apfs, hfs; else every Store call is `Unsupported` with its reason, no fallback | [`service.rs`](../../../../control/crates/layerfs-store/src/service.rs), [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | | One attempt per call; nothing is resent | [`service.rs`](../../../../control/crates/layerfs-store/src/service.rs), [`place.rs`](../../../../control/crates/layerfs-store/src/place.rs) | | Store records are created with `profile: disposable` (Disposable/WAL/synchronous=OFF, the only permitted profile); Durable execution is `NOT_RUN — disabled by owner until explicit reauthorization` | [`record.rs`](../../../../control/crates/layerfs-store/src/record.rs) `created_on_native`, `restored` | @@ -113,7 +113,7 @@ refusal names the clearing call: a new `store push ` or `store pull | Store lock held | `Unavailable` after 120 s | [Home](../home/README.md#locks-and-leases) | | Leased or used by a Sandbox (push, pull, remove, disconnect, restore) | `InUse`, `detail.related` lists `{kind: "lease", name: }` and `{kind: "sandbox", name}` | [`inventory.rs`](../../../../control/crates/layerfs-control/src/inventory.rs) `store_idle` | | Open-Store idle close | 60 s (`IDLE`); a Store with a call running is never closed (`close` refuses `InUse`) | [`open.rs`](../../../../control/crates/layerfs-store/src/open.rs) | -| Store kernel guard | `Unsupported`, naming the filesystem | [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | +| Store kernel guard | `Unsupported`, naming the directory requirement (`0700` owned by this user, not missing/symlink) or filesystem | [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | | Store file missing | `NoStore` | [`service.rs`](../../../../control/crates/layerfs-store/src/service.rs) | ## Demonstration diff --git a/docs/architecture/engine/daemon/README.md b/docs/architecture/engine/daemon/README.md index 5b133f5b72..4697766de6 100644 --- a/docs/architecture/engine/daemon/README.md +++ b/docs/architecture/engine/daemon/README.md @@ -56,7 +56,7 @@ layerfs-daemon --config | --- | --- | --- | | Takes one argument | `--config `; argv and env carry no key and no command ("no launcher or shell command mode") | [`application/cli.rs`](../../../../engine/crates/layerfs-daemon/src/application/cli.rs) | | Checks the config | Opened `O_NOFOLLOW`; regular file owned by the daemon user, no group/other bits, private directory; at most 8 192 bytes; one `DaemonSetup` record | [`application/config.rs`](../../../../engine/crates/layerfs-daemon/src/application/config.rs) | -| Separates identities and paths | `command_uid` differs from the daemon uid (except on NFS); Store, overlay and config sit outside the mount home in private `0700` directories; the Store is not the overlay file; `serial_low_water` below `SERIAL_REFILL` | [`application/config.rs`](../../../../engine/crates/layerfs-daemon/src/application/config.rs) | +| Separates identities and paths | `command_uid` differs from the daemon uid (except on NFS); Store, overlay and config sit outside the mount home in private `0700` directories (failing with `private directory ownership/mode/type at : …` and a `chmod 700` hint on mode errors); the Store is not the overlay file; `serial_low_water` below `SERIAL_REFILL` | [`application/config.rs`](../../../../engine/crates/layerfs-daemon/src/application/config.rs) | | Chooses the route | `fuse` or `nfs`; macOS accepts only `nfs` | [`application/filesystem.rs`](../../../../engine/crates/layerfs-daemon/src/application/filesystem.rs) | | Starts the overlay owner | Creates and closes the overlay template (`pager_kib`, `namespaces`); every later start failure is `AfterOverlay`, keeping the owner | [`application/owner.rs`](../../../../engine/crates/layerfs-daemon/src/application/owner.rs), [Overlay](../overlay/README.md) | | Takes a fresh incarnation | `generate_keypair()`; the public half is `DaemonStatus.instance` | [`application/owner.rs`](../../../../engine/crates/layerfs-daemon/src/application/owner.rs) | diff --git a/docs/architecture/engine/store-calls/README.md b/docs/architecture/engine/store-calls/README.md index 0a80d2974b..71bfec018f 100644 --- a/docs/architecture/engine/store-calls/README.md +++ b/docs/architecture/engine/store-calls/README.md @@ -50,7 +50,7 @@ Commits use; those are described under [Commit](../commit/README.md) and | --- | --- | --- | | One route, in the caller's process | `fs.*`, `branch.*`, `project.*` and `store.stats` run on this engine in the process that opened the Store. There is no daemon route for them and no fallback. | [`lib.rs`](../../../../engine/crates/layerfs-store-service/src/lib.rs), [`service.rs`](../../../../control/crates/layerfs-store/src/service.rs) | | Daemon serves only Workspace calls | Mount, exec, Workspace Commit, status and unmount go to a Sandbox daemon; the daemon reuses this crate's `store` and `overlay` modules, not `StoreService`. | [`store_service.rs`](../../../../engine/crates/layerfs-store-service/src/store_service.rs), [Daemon](../daemon/README.md) | -| Kernel guard first | Before any file opens, `kernel_guard::check` reads the platform and the filesystem type of the Store's directory. Linux: ext4, xfs, btrfs. macOS: apfs or hfs (native Sandbox, one shared macOS kernel). Anything else: `OpenRefusal::Unsupported` with its reason. | [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | +| Kernel guard first | Before any file opens or is created, `kernel_guard::check` verifies without modifying permissions that the Store's own directory (`store.parent()`, or `.` when empty) exists as a real `0700` directory owned by the current effective user (refusing a missing path, a symlink, wrong owner or `mode & 0o077 != 0` with a `chmod 700` hint) and checks the filesystem type. Linux: ext4, xfs, btrfs. macOS: apfs or hfs (native Sandbox, one shared macOS kernel). Anything else: `OpenRefusal::Unsupported` with its reason. | [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | | macOS and Windows hosts | A process that cannot open the Store under the guard sends its Store calls to a loopback worker: a Linux process (a container on macOS, a WSL2 distribution on Windows) that runs this same engine and opens the Store directly. The engine knows no worker. | [Control: Workers and reach](../../control/machines/workers-and-reach.md) | | PostgreSQL | `open_postgres` (feature `postgresql`) runs no kernel guard: the Store is a server reached over a connection. Without the feature a PostgreSQL selection fails `BackendUnavailable`. | [`store_service.rs`](../../../../engine/crates/layerfs-store-service/src/store_service.rs), [`bootstrap.rs`](../../../../engine/crates/layerfs-store-service/src/bootstrap.rs) | @@ -195,7 +195,7 @@ not in `methods::call`, and in this repository only tests call it. | Limit or failure | Value or error | Source | | --- | --- | --- | -| Unsupported platform or filesystem | `OpenRefusal::Unsupported`, nothing opened | [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | +| Unsupported directory, platform or filesystem | `OpenRefusal::Unsupported`, nothing opened or created | [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | | Store file absent | `OpenRefusal::Missing` | [`store_service.rs`](../../../../engine/crates/layerfs-store-service/src/store_service.rs) | | Private Workspaces per process | 64; then `Capacity` | same | | Overlay maintenance stopped | `bind` refuses | same | diff --git a/docs/versioned/2.0.0/calls.md b/docs/versioned/2.0.0/calls.md index 43e9627e6d..851a952652 100644 --- a/docs/versioned/2.0.0/calls.md +++ b/docs/versioned/2.0.0/calls.md @@ -488,7 +488,8 @@ layerfs doctor --text # a count line, then each finding and its fix out of date and refuses nothing. Each finding's `fix` names the call (or the by-hand step, where no call can) that fixes it. - It checks: records that do not parse, or whose `"v"` is not 1; folder - modes other than 0700 (0701 where a namespaces Sandbox passes through) and + modes other than 0700 (0701 where a namespaces Sandbox passes through, but + never a Store's own folder, which stays 0700) and record modes other than 0600; broken references (a Sandbox whose Store, project or machine is gone, a Store on a machine with no record, a project in a Store folder with no record, a built-in machine whose recorded daemon is not there, an error diff --git a/docs/versioned/2.0.0/machines-and-stores.md b/docs/versioned/2.0.0/machines-and-stores.md index 17d03040ac..f617a6b3b4 100644 --- a/docs/versioned/2.0.0/machines-and-stores.md +++ b/docs/versioned/2.0.0/machines-and-stores.md @@ -20,7 +20,7 @@ the [contract](../../architecture/control/machines/platforms.md#removed-platform | Kind | How many | Made by | Sandboxes run as | Tool | | --- | --- | --- | --- | --- | -| `native` | one, built in | — | macOS: host processes with a loopback NFS mount. Linux: namespaces (needs root). Windows: none; `native` holds only records (O5) | — | +| `native` | one, built in | — | macOS: host processes with a loopback NFS mount (each sqlite Store's directory must be owned by this user and private, mode `0700`). Linux: namespaces (needs root). Windows: none; `native` holds only records (O5) | — | | `docker` | one, built in | — | namespaces inside its worker container (D-8), rooted in the bundle's Sandbox image. The image `machine update docker --image` records, else the bundle's Sandbox image loaded into Docker once (#535 P5), is the worker container's; see Routes | `docker` | | `apple-container` | many | `machine create` | namespaces inside the VM | `container` | | `lima` | many | `machine create` | namespaces inside the VM | `limactl` | diff --git a/engine/crates/layerfs-daemon/src/application/config.rs b/engine/crates/layerfs-daemon/src/application/config.rs index 1e556f81b4..95caad8bf8 100644 --- a/engine/crates/layerfs-daemon/src/application/config.rs +++ b/engine/crates/layerfs-daemon/src/application/config.rs @@ -106,11 +106,36 @@ fn private_directory(path: &Path, create: bool) -> io::Result<()> { .custom_flags((OFlag::O_NOFOLLOW | OFlag::O_DIRECTORY | OFlag::O_CLOEXEC).bits()) .open(path)?; let metadata = directory.metadata()?; - if !metadata.is_dir() - || metadata.uid() != Uid::effective().as_raw() - || metadata.mode() & 0o077 != 0 - { - return Err(invalid("private directory ownership/mode/type")); + let uid = Uid::effective().as_raw(); + let mode = metadata.mode() & 0o7777; + if !metadata.is_dir() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "private directory ownership/mode/type at {}: not a directory (mode {mode:04o})", + path.display() + ), + )); + } + if metadata.uid() != uid { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "private directory ownership/mode/type at {}: owned by uid {}, expected uid {uid}", + path.display(), + metadata.uid() + ), + )); + } + if mode & 0o077 != 0 { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!( + "private directory ownership/mode/type at {}: mode is {mode:04o}, expected 0700 (fix with: chmod 700 {})", + path.display(), + path.display() + ), + )); } Ok(()) } diff --git a/engine/crates/layerfs-store-service/Cargo.toml b/engine/crates/layerfs-store-service/Cargo.toml index c35603f928..0647dc6f0b 100644 --- a/engine/crates/layerfs-store-service/Cargo.toml +++ b/engine/crates/layerfs-store-service/Cargo.toml @@ -20,7 +20,7 @@ layerfs-history = { path = "../../../core/crates/layerfs-history" } layerfs-telemetry = { path = "../../../core/crates/layerfs-telemetry" } # Concrete Store opening (bootstrap) and the kernel guard's open. layerfs-persistence = { path = "../layerfs-persistence" } -nix = { version = "=0.31.3", default-features = false, features = ["fs"] } +nix = { version = "=0.31.3", default-features = false, features = ["fs", "user"] } # Only the `postgresql` feature's tests use it directly (schema cleanup). postgres = { version = "=0.19.14", optional = true } diff --git a/engine/crates/layerfs-store-service/src/install.rs b/engine/crates/layerfs-store-service/src/install.rs index 2b7692f459..5c27660c7e 100644 --- a/engine/crates/layerfs-store-service/src/install.rs +++ b/engine/crates/layerfs-store-service/src/install.rs @@ -5,7 +5,9 @@ //! initial Branch. The daemon does this for a Store streamed to it; Import //! does it in the SDK's process ([`install_local`]), so no Sandbox is started //! to install a Store. One attempt: a failure keeps whatever it left, named -//! in its report, and nothing is deleted or tried again. +//! in its report, and nothing is deleted or tried again, except that a guard +//! refusal before any Store file exists reclaims only the empty directory +//! that call's own `mkdir` made. use crate::bootstrap::{open_store_observed, OpenedStore}; use crate::store::{checked_base, PortError}; use layerfs_bridge::provision::{StoreManifest, StoreProfile}; @@ -136,12 +138,66 @@ impl std::fmt::Display for LocalInstallFailure { } } +/// Whether this call's own `mkdir` made the Store's directory: the only +/// ownership that lets a caller reclaim that directory when nothing was +/// published. A directory another caller, or the user, made is never this +/// call's to remove. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum Made { + /// This call's `mkdir` created it. + ThisCall, + /// It was already there; it is not this call's to remove. + AlreadyThere, +} + +/// Makes the Store's directory owner-only (`0700`) when it is absent and says +/// whether this call's own `mkdir` made it. One ownership rule for the Store's +/// directory, shared by an install and `store.create` (`make_empty`): a +/// `mkdir` that fails — `AlreadyExists`, when another caller made the +/// directory first, included — claims nothing. +pub fn make_private_directory(directory: &Path) -> io::Result { + use std::os::unix::fs::DirBuilderExt; + if directory.exists() { + return Ok(Made::AlreadyThere); + } + fs::DirBuilder::new().mode(0o700).create(directory)?; + Ok(Made::ThisCall) +} + +/// Makes the Store's directory owner-only (`0700`, created as such when it is +/// absent), then checks the kernel guard for the Store file. A directory is +/// this call's own only when this call's `mkdir` returned success: an +/// installer-user or `mkdir` failure (`AlreadyExists`, when another caller +/// made the directory first, included) removes nothing, and a guard refusal — +/// which happens before any Store file exists — removes only that own +/// directory, non-recursively, so it cannot take another caller's Store. +pub fn prepare(destination: &Path) -> Result<(), String> { + installer_user()?; + let directory = destination + .parent() + .ok_or("the Store destination has no directory")?; + let made = + make_private_directory(directory).map_err(|error| error.to_string())? == Made::ThisCall; + match crate::kernel_guard::check(destination) { + Ok(_) => Ok(()), + Err(reason) => { + if made { + // no Store file exists yet: this call's own directory, and + // only while it still holds nothing + let _ = fs::remove_dir(directory); + } + Err(reason.0) + } + } +} + /// Installs the sealed Store `source` (described by `manifest`) at /// `destination` in this process: the project volume's -/// `global/store.sqlite`, its `global/` directory made owner-only. The -/// destination's filesystem must pass the kernel guard, as every Store opener -/// must, and this process must run as the daemons' user. Returns the -/// installed manifest, which records this process's SQLite version. +/// `global/store.sqlite`, its `global/` directory made owner-only (`0700`, +/// created as such when missing). The destination's directory and filesystem +/// must pass the kernel guard, as every Store opener must, and this process +/// must run as the daemons' user. Returns the installed manifest, which +/// records this process's SQLite version. pub fn install_local( source: &Path, manifest: &StoreManifest, @@ -156,19 +212,8 @@ pub fn install_local( if manifest.profile != StoreProfile::Disposable || manifest.daemon_sqlite.is_some() { return Err("a sealed, not yet installed, Disposable Store is required".to_owned()); } - let directory = destination - .parent() - .ok_or("the Store destination has no directory")?; - crate::kernel_guard::check(directory).map_err(|reason| reason.0)?; - installer_user()?; let text = |error: io::Error| error.to_string(); - if !directory.exists() { - use std::os::unix::fs::DirBuilderExt; - fs::DirBuilder::new() - .mode(0o700) - .create(directory) - .map_err(text)?; - } + prepare(destination)?; let (target, temporary) = paths(destination).map_err(text)?; let mut file = claim(&target, &temporary).map_err(text)?; left.temporary = Some(temporary.clone()); @@ -207,12 +252,12 @@ pub fn install_local( /// installs its Store (there is no root daemon to match); the daemon's own /// check that it owns the Store file still holds. #[cfg(target_os = "macos")] -pub fn installer_user() -> Result<(), String> { +fn installer_user() -> Result<(), String> { Ok(()) } /// Refuses an installer that is not the daemons' user. #[cfg(not(target_os = "macos"))] -pub fn installer_user() -> Result<(), String> { +fn installer_user() -> Result<(), String> { use std::os::unix::fs::MetadataExt; // the owner of /proc/self is this process's effective user let uid = fs::metadata("/proc/self") diff --git a/engine/crates/layerfs-store-service/src/kernel_guard.rs b/engine/crates/layerfs-store-service/src/kernel_guard.rs index e0e3945972..1be6339d48 100644 --- a/engine/crates/layerfs-store-service/src/kernel_guard.rs +++ b/engine/crates/layerfs-store-service/src/kernel_guard.rs @@ -4,9 +4,10 @@ //! filesystem that holds it: SQLite's WAL locks and the writers' admission //! lane (a `flock` on the Store's directory) coordinate only within one //! kernel. The guard checks what this process can verify before it opens any -//! file: the platform, and the type of the filesystem holding the Store's -//! directory, from an allowlist of local filesystems. Anything else is -//! `Unsupported` with its reason; there is no other route to fall back to. +//! file: the platform, the Store's own directory (a real `0700` directory +//! owned by this user) and the type of the filesystem holding it, from an +//! allowlist of local filesystems. Anything else is `Unsupported` with its +//! reason; there is no other route to fall back to. use std::path::Path; /// Filesystems that carry the Store's locks: ext4, xfs and btrfs. @@ -27,8 +28,54 @@ impl std::fmt::Display for Unsupported { } } -/// Checks the platform and the filesystem holding `store`'s directory, -/// before anything is opened. Returns the filesystem's name. +/// Checks that `directory` satisfies the daemon's private-directory +/// requirements: an existing real directory (not a symlink) owned by the +/// current effective user with no group/other access (`mode & 0o077 == 0`). +/// Side-effect-free: never creates a directory or modifies permissions. +#[cfg(any(target_os = "linux", target_os = "macos"))] +fn check_private_directory(directory: &Path) -> Result<(), Unsupported> { + use std::fs; + use std::os::unix::fs::MetadataExt; + let euid = nix::unistd::geteuid().as_raw(); + let meta = fs::symlink_metadata(directory).map_err(|error| { + Unsupported(format!( + "the Store's directory {} cannot be examined: {error}", + directory.display() + )) + })?; + let path = directory.display(); + if meta.file_type().is_symlink() { + return Err(Unsupported(format!( + "the Store's directory {path} is a symlink; a real private directory (mode 0700) is required" + ))); + } + if !meta.is_dir() { + return Err(Unsupported(format!( + "the Store's directory {path} is not a directory (mode {:04o}); a private directory (mode 0700) is required", + meta.mode() & 0o7777 + ))); + } + if meta.uid() != euid { + return Err(Unsupported(format!( + "the Store's directory {path} is owned by uid {}, not the current user (uid {euid}); \ + the daemon requires a private directory owned by uid {euid} (mode 0700)", + meta.uid() + ))); + } + let mode = meta.mode() & 0o7777; + if mode & 0o077 != 0 { + return Err(Unsupported(format!( + "the Store's directory {path} has mode {mode:04o} (group/other bits {:04o} are set); \ + the daemon requires a private directory (mode 0700, no group/other access); \ + fix with: chmod 700 {path}", + mode & 0o077 + ))); + } + Ok(()) +} + +/// Checks the platform, `store`'s own private directory and the filesystem +/// holding it, before anything is opened. Returns the filesystem's name. pub fn check(store: &Path) -> Result<&'static str, Unsupported> { #[cfg(target_os = "linux")] { @@ -36,6 +83,7 @@ pub fn check(store: &Path) -> Result<&'static str, Unsupported> { .parent() .filter(|parent| !parent.as_os_str().is_empty()) .unwrap_or_else(|| Path::new(".")); + check_private_directory(directory)?; let stat = nix::sys::statfs::statfs(directory).map_err(|error| { Unsupported(format!( "the Store's directory {} cannot be examined: {error}", @@ -65,6 +113,7 @@ pub fn check(store: &Path) -> Result<&'static str, Unsupported> { .parent() .filter(|parent| !parent.as_os_str().is_empty()) .unwrap_or_else(|| Path::new(".")); + check_private_directory(directory)?; let stat = nix::sys::statfs::statfs(directory).map_err(|error| { Unsupported(format!( "the Store's directory {} cannot be examined: {error}", diff --git a/engine/crates/layerfs-store-service/tests/install_directory.rs b/engine/crates/layerfs-store-service/tests/install_directory.rs new file mode 100644 index 0000000000..14f8024f1b --- /dev/null +++ b/engine/crates/layerfs-store-service/tests/install_directory.rs @@ -0,0 +1,180 @@ +//! The Store directory an install (`install_local`) or a backup restore +//! (`backup::fetch`) prepares before it writes anything (#570 P1). Both call +//! [`layerfs_store_service::install::prepare`]: the directory belongs to the +//! call only when that call's own `mkdir` returned success. A `mkdir` failure +//! claims nothing, and a guard refusal reclaims only that own, still-empty +//! directory, non-recursively. +//! +//! `prepare` starts with the installer-user check, so its own two guard +//! branches need the daemons' user (uid 0), as an install runs in production. +//! Run elsewhere, each case asserts what the same call does at this privilege +//! level: the check refuses before the directory or the guard is touched, so +//! the call creates nothing and removes nothing. The claim step itself is +//! [`install::make_private_directory`], one function shared with +//! `store.create`'s `make_empty`; it has no installer-user check, so the test +//! below drives both of its branches and its failed `mkdir` unprivileged, +//! and `layerfs-store`'s `create_directory` test drives the same step and the +//! guard through `store.create`. +#![cfg(target_os = "linux")] +use layerfs_store_service::install; +use std::path::{Path, PathBuf}; + +/// Whether this process is the daemons' user, which installs a Store. +fn daemons_user() -> bool { + use std::os::unix::fs::MetadataExt; + std::fs::metadata("/proc/self").is_ok_and(|meta| meta.uid() == 0) +} + +fn scratch(parent: &Path, label: &str) -> PathBuf { + use std::os::unix::fs::DirBuilderExt; + let directory = parent.join(format!("layerfs-install-{label}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&directory); + std::fs::DirBuilder::new() + .recursive(true) + .mode(0o700) + .create(&directory) + .unwrap(); + directory +} + +/// The claim step on its own, as any user: an absent directory is made +/// `0700` and belongs to this call, one that is already there keeps its mode +/// and belongs to no one, and a `mkdir` that fails on a taken name claims +/// nothing. `prepare` acts on exactly this answer, and `store.create`'s +/// `make_empty` uses the same step. +#[test] +fn the_directory_claim_creates_0700_and_marks_only_its_own_mkdir() { + use std::os::unix::fs::PermissionsExt; + let volume = scratch(&std::env::temp_dir(), "claim"); + let made = volume.join("made"); + assert_eq!( + install::make_private_directory(&made).unwrap(), + install::Made::ThisCall + ); + let mode = std::fs::symlink_metadata(&made) + .unwrap() + .permissions() + .mode() + & 0o7777; + assert_eq!(mode, 0o700, "this call's own directory is private"); + std::fs::set_permissions(&made, std::fs::Permissions::from_mode(0o755)).unwrap(); + assert_eq!( + install::make_private_directory(&made).unwrap(), + install::Made::AlreadyThere + ); + let mode = std::fs::symlink_metadata(&made) + .unwrap() + .permissions() + .mode() + & 0o7777; + assert_eq!( + mode, 0o755, + "a directory this call did not make keeps its mode" + ); + // a taken name: `Path::exists` looks through the dangling link and sees + // nothing, so the step reaches `mkdir`, which claims no directory + let taken = volume.join("taken"); + std::os::unix::fs::symlink(volume.join("absent-target"), &taken).unwrap(); + let refused = install::make_private_directory(&taken).unwrap_err(); + assert_eq!( + refused.kind(), + std::io::ErrorKind::AlreadyExists, + "{refused}" + ); + assert!( + std::fs::symlink_metadata(&taken) + .unwrap() + .file_type() + .is_symlink(), + "the taken name stays" + ); + std::fs::remove_dir_all(&volume).unwrap(); +} + +/// A guard refusal happens before any Store file exists: it reclaims the +/// empty directory this call's own `mkdir` made, and leaves the volume. +/// tmpfs refuses the guard by filesystem, after the `mkdir` succeeded. +#[test] +fn a_refused_guard_removes_only_the_empty_directory_this_call_made() { + let volume = scratch(Path::new("/dev/shm"), "tmpfs-volume"); + let directory = volume.join("global"); + let destination = directory.join("store.sqlite"); + let refused = install::prepare(&destination).unwrap_err(); + if !daemons_user() { + // the installer-user check comes first: this call made no directory, + // so it has none of its own to reclaim + assert!(refused.contains("must run as user 0"), "{refused}"); + assert!(!directory.exists(), "a refused call made no directory"); + std::fs::remove_dir_all(&volume).unwrap(); + return; + } + assert!(refused.contains("ext4, xfs or btrfs"), "{refused}"); + assert!( + !directory.exists(), + "the empty directory this call made is reclaimed" + ); + assert!(volume.is_dir(), "the volume itself is untouched"); + std::fs::remove_dir_all(&volume).unwrap(); +} + +/// A `mkdir` that fails claims nothing: the destination's directory name is +/// already taken, by a dangling symbolic link here — `Path::exists` looks +/// through the link and sees nothing, so the call reaches `mkdir`, which +/// returns `AlreadyExists`. The call removes neither that name nor anything +/// behind it, and creates no target of its own. +#[test] +fn a_mkdir_failure_claims_nothing() { + let volume = scratch(Path::new("/dev/shm"), "taken-name"); + let directory = volume.join("global"); + let absent_target = volume.join("link-target"); + std::os::unix::fs::symlink(&absent_target, &directory).unwrap(); + let destination = directory.join("store.sqlite"); + let refused = install::prepare(&destination).unwrap_err(); + if !daemons_user() { + // the installer-user check comes first: this call never reached its + // `mkdir`, and changes nothing + assert!(refused.contains("must run as user 0"), "{refused}"); + } else { + assert!(refused.contains("File exists"), "{refused}"); + } + let link = std::fs::symlink_metadata(&directory).unwrap(); + assert!(link.file_type().is_symlink(), "the taken name stays"); + assert_eq!(std::fs::read_link(&directory).unwrap(), absent_target); + assert!( + std::fs::symlink_metadata(&absent_target).is_err(), + "the call creates no directory of its own" + ); + std::fs::remove_dir_all(&volume).unwrap(); +} + +/// A guard refusal never removes a directory this call did not make, nor the +/// Store another caller put in it. +#[test] +fn a_refused_guard_keeps_a_directory_this_call_did_not_make() { + use std::os::unix::fs::{DirBuilderExt, PermissionsExt}; + let volume = scratch(Path::new("/dev/shm"), "tmpfs-foreign"); + let directory = volume.join("global"); + std::fs::DirBuilder::new() + .mode(0o700) + .create(&directory) + .unwrap(); + let destination = directory.join("store.sqlite"); + std::fs::write(&destination, b"another caller's Store").unwrap(); + std::fs::set_permissions(&destination, std::fs::Permissions::from_mode(0o600)).unwrap(); + let refused = install::prepare(&destination).unwrap_err(); + let guard = daemons_user(); + assert!( + refused.contains(if guard { + "ext4, xfs or btrfs" + } else { + "must run as user 0" + }), + "{refused}" + ); + assert!(directory.is_dir(), "the other caller's directory stays"); + assert_eq!( + std::fs::read(&destination).unwrap(), + b"another caller's Store" + ); + std::fs::remove_dir_all(&volume).unwrap(); +} diff --git a/engine/crates/layerfs-store-service/tests/kernel_guard.rs b/engine/crates/layerfs-store-service/tests/kernel_guard.rs index e99d46ea85..ab17acbba4 100644 --- a/engine/crates/layerfs-store-service/tests/kernel_guard.rs +++ b/engine/crates/layerfs-store-service/tests/kernel_guard.rs @@ -3,7 +3,6 @@ //! a filesystem outside the allowlist (here tmpfs). The refusal comes before //! any file is opened: the Store file's bytes are unchanged and no overlay //! template is created. On macOS a local apfs or hfs directory is allowed. -#[cfg(not(target_os = "macos"))] use layerfs_store_service::{OpenRefusal, StoreService}; use std::path::{Path, PathBuf}; @@ -27,11 +26,82 @@ fn refused(directory: &Path) -> (String, Vec, Vec) { } fn scratch(parent: &Path, label: &str) -> PathBuf { + use std::os::unix::fs::DirBuilderExt; let directory = parent.join(format!("layerfs-guard-{label}-{}", std::process::id())); - std::fs::create_dir_all(&directory).unwrap(); + let _ = std::fs::remove_dir_all(&directory); + std::fs::DirBuilder::new() + .recursive(true) + .mode(0o700) + .create(&directory) + .unwrap(); directory } +#[cfg(any(target_os = "linux", target_os = "macos"))] +#[test] +fn missing_store_directory_is_refused_without_creating_it_and_non_private_directory_is_refused() { + use std::os::unix::fs::{DirBuilderExt, PermissionsExt}; + let parent = scratch(&std::env::temp_dir(), "private-mode"); + let missing = parent.join("global"); + assert!(!missing.exists()); + let refused_missing = layerfs_store_service::kernel_guard::check(&missing.join("store.sqlite")) + .expect_err("missing directory must be refused without creating it"); + assert!( + refused_missing.0.contains(&missing.display().to_string()), + "{refused_missing}" + ); + assert!( + !missing.exists(), + "kernel_guard::check must not create a missing directory" + ); + let template = parent.join("overlay.sqlite"); + let open_missing = StoreService::open_direct( + &missing.join("store.sqlite"), + b"binding", + [7; 32], + &template, + ); + match open_missing { + Err(OpenRefusal::Unsupported(_)) => {} + Err(other) => panic!("expected OpenRefusal::Unsupported, got {other:?}"), + Ok(_) => panic!("expected OpenRefusal::Unsupported, Store opened"), + } + assert!( + !missing.exists(), + "open_direct on a missing Store directory must not leave an empty directory" + ); + + // An existing 0700 directory passes, even for a non-.sqlite filename. + std::fs::DirBuilder::new() + .mode(0o700) + .create(&missing) + .unwrap(); + let checked = layerfs_store_service::kernel_guard::check(&missing.join("db")); + assert!(checked.is_ok(), "{checked:?}"); + + // An existing directory with group/other access (e.g. 0755 from default + // umask) is refused up front with its path, current mode, expected 0700 + // and chmod command, without modifying the user's directory permissions. + std::fs::set_permissions(&missing, std::fs::Permissions::from_mode(0o755)).unwrap(); + let refused = layerfs_store_service::kernel_guard::check(&missing.join("store.sqlite")) + .expect_err("0755 directory must be refused"); + assert!( + refused.0.contains(&missing.display().to_string()), + "{refused}" + ); + assert!(refused.0.contains("0755"), "{refused}"); + assert!(refused.0.contains("0700"), "{refused}"); + assert!( + refused + .0 + .contains(&format!("chmod 700 {}", missing.display())), + "{refused}" + ); + let unchanged = std::fs::metadata(&missing).unwrap().permissions().mode() & 0o7777; + assert_eq!(unchanged, 0o755); + std::fs::remove_dir_all(parent).unwrap(); +} + #[cfg(target_os = "macos")] #[test] fn direct_access_is_allowed_on_a_local_macos_filesystem() { diff --git a/engine/crates/layerfs-store-service/tests/support/installed_store.rs b/engine/crates/layerfs-store-service/tests/support/installed_store.rs index e609c8258e..8187f5c8a6 100644 --- a/engine/crates/layerfs-store-service/tests/support/installed_store.rs +++ b/engine/crates/layerfs-store-service/tests/support/installed_store.rs @@ -45,7 +45,7 @@ impl Fixture { ) -> Self { let directory = std::env::temp_dir().join(format!("layerfs-installed-{}-{label}", std::process::id())); - std::fs::create_dir(&directory).unwrap(); + private_directory(&directory); let source = directory.join("source"); std::fs::create_dir(&source).unwrap(); let count = build(&source); @@ -116,7 +116,7 @@ impl Fixture { pub fn cloned(label: &str, store: &std::path::Path, count: usize) -> Self { let directory = std::env::temp_dir().join(format!("layerfs-installed-{}-{label}", std::process::id())); - std::fs::create_dir(&directory).unwrap(); + private_directory(&directory); let installed = directory.join("store.sqlite"); std::fs::copy(store, &installed).unwrap(); Self { @@ -132,6 +132,14 @@ impl Fixture { std::fs::remove_dir_all(self.directory).unwrap(); } } +/// The fixture's own Store folder: private as every folder a daemon opens, +/// because `kernel_guard` refuses group or other access and an open through +/// `StoreService::open_direct` reaches the Store itself. +fn private_directory(path: &std::path::Path) { + use std::os::unix::fs::DirBuilderExt; + std::fs::DirBuilder::new().mode(0o700).create(path).unwrap(); +} + pub fn bytes(n: usize) -> Vec { format!("complete installed file {n:06}\n").into_bytes() }