From cc1edadad0a325756603dbc1e0acc7e710f5ca31 Mon Sep 17 00:00:00 2001 From: liuyuchen <152110621+jiangkoumo@users.noreply.github.com> Date: Thu, 5 Nov 2026 18:46:51 +0800 Subject: [PATCH 1/6] sandbox(534): pass user rustup, cargo and git config locations to native commands On native macOS (host_process), workspace.exec sets HOME to the Sandbox's scratch directory (/tmp), so commands do not write into the user's home directory. Pass RUSTUP_HOME (~/.rustup), CARGO_HOME (~/.cargo) and GIT_CONFIG_GLOBAL (~/.gitconfig, else $XDG_CONFIG_HOME/git/config or ~/.config/git/config) from the caller's environment or real HOME when present, before applying request.environment so explicit values win. Docker and namespaces backends are unchanged. Tests: local_search_path::a_native_command_receives_existing_user_toolchain_and_git_locations covers absent paths, XDG and ~/.gitconfig resolution, ~/.rustup and ~/.cargo passthrough, and caller environment / request environment overrides while HOME remains /tmp. Production LOC: 137010 -> 137039 (delta +29) Scope: tools/production_loc.py --root on parent d430474135 and committed tree (core 38400 -> 38400, engine 62799 -> 62799, connect 15947 -> 15976 (+29), api 19864 -> 19864, dsh 0 -> 0; reference absent (retired R9c; last counted 65,417 at tree 498dd1917812)). --- .../references/methods/sandbox.md | 2 +- .../references/methods/workspace.md | 1 + .../src/backend/host_process/exec.rs | 47 ++++- .../tests/local_search_path.rs | 181 +++++++++++++++++- docs/architecture/api/sdk/workspace/exec.md | 2 +- .../connect/sandboxes/commands-and-streams.md | 2 +- 6 files changed, 220 insertions(+), 15 deletions(-) diff --git a/.agents/skills/layerfs-js-sdk/references/methods/sandbox.md b/.agents/skills/layerfs-js-sdk/references/methods/sandbox.md index 0ebc7b7211..2bea7025a5 100644 --- a/.agents/skills/layerfs-js-sdk/references/methods/sandbox.md +++ b/.agents/skills/layerfs-js-sdk/references/methods/sandbox.md @@ -61,7 +61,7 @@ ready. | Machine | Sandbox | | --- | --- | -| `native` on macOS | host processes, Workspaces over loopback NFS. Needs the daemon: the native machine record's `daemon`, else `~/.cache/layerfs/bundle//-/layerfs-daemon` (`Unavailable` when missing) | +| `native` on macOS | host processes, Workspaces over loopback NFS. Needs the daemon: the native machine record's `daemon`, else `~/.cache/layerfs/bundle//-/layerfs-daemon` (`Unavailable` when missing). Commands get `HOME=/tmp` and inherit `PATH` plus `RUSTUP_HOME` (`~/.rustup`), `CARGO_HOME` (`~/.cargo`) and `GIT_CONFIG_GLOBAL` (`~/.gitconfig` or XDG `git/config`) when present; set them in the caller's environment or the command to override | | `native` on Linux | Linux namespaces | | a VM or ssh machine | sent to that machine's worker, which runs it on its own `native`. On `ssh` and `lima` this process then forwards the daemon's port to its loopback and keeps its keys in the home (#535 G4): the Workspace calls but `exec` go to the daemon directly. A forward that fails leaves the Sandbox on the worker route and the answer says `forward_failed` | | `docker`, sqlite Store on `docker` | a container on `image`, else the docker machine's image (`machine update docker --image`), else the bundle's Sandbox image, loaded into Docker once (#535 P5); on macOS and Linux the docker worker starts it beside itself, mounting only the Store's folder (G8). Its daemon's port is published on this host's loopback: the Workspace calls but `exec` go to it directly (#535 G4) | diff --git a/.agents/skills/layerfs-js-sdk/references/methods/workspace.md b/.agents/skills/layerfs-js-sdk/references/methods/workspace.md index bafb3fd72f..af6b3c6eaf 100644 --- a/.agents/skills/layerfs-js-sdk/references/methods/workspace.md +++ b/.agents/skills/layerfs-js-sdk/references/methods/workspace.md @@ -130,6 +130,7 @@ tests, `f67d5a9f6f`, `771823d728`, `94671c974c`, `37f1bbd2b9`): **Limits** +- `HOME` is the Sandbox's scratch directory (`/tmp` in a container, `/tmp` on `native`). On macOS `native`, `RUSTUP_HOME` (`~/.rustup`), `CARGO_HOME` (`~/.cargo`) and `GIT_CONFIG_GLOBAL` (`~/.gitconfig`, else `$XDG_CONFIG_HOME/git/config` or `~/.config/git/config`) are passed through from the caller's environment or real `HOME` when present; set them in the caller's environment or inline in the command (`CARGO_HOME=/path …`) to override. - No timeout: put `timeout 110` inside the command. - It waits until everything holding its output has ended, background jobs included. Start a server, probe it and stop it in **one** call, with its diff --git a/connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs b/connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs index fc9640e9a0..0616e7900b 100644 --- a/connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs +++ b/connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs @@ -1,18 +1,20 @@ //! 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 +//! 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, process, process::LocalProcess, search::search_path}; use crate::{ExecExit, ExecRequest, RuntimeError, WireFailure}; use nix::sys::signal::{killpg, Signal}; use std::{ + ffi::OsString, io::{self, Write}, os::unix::process::{CommandExt, ExitStatusExt}, - path::Path, + path::{Path, PathBuf}, process::{Command, Stdio}, }; @@ -37,6 +39,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) @@ -123,3 +126,35 @@ fn check(request: &ExecRequest) -> Result<(), RuntimeError> { } Ok(()) } + +/// The caller's Rust and Git locations, 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. +fn user_environment() -> Vec<(&'static str, OsString)> { + 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 +} + +fn non_empty_env(name: &str) -> Option { + std::env::var_os(name).filter(|value| !value.is_empty()) +} diff --git a/connect/crates/layerfs-sandbox/tests/local_search_path.rs b/connect/crates/layerfs-sandbox/tests/local_search_path.rs index cbc7d35e65..5ceff4f076 100644 --- a/connect/crates/layerfs-sandbox/tests/local_search_path.rs +++ b/connect/crates/layerfs-sandbox/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::{docker::ExecRequest, host_process::Sandbox, local::Local, CommandIdentity}; 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,161 @@ fn a_native_daemon_and_its_commands_start_with_this_processs_path() { } let _ = fs::remove_dir_all(root.parent().unwrap()); } + +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/docs/architecture/api/sdk/workspace/exec.md b/docs/architecture/api/sdk/workspace/exec.md index abea42de59..5a547d6e18 100644 --- a/docs/architecture/api/sdk/workspace/exec.md +++ b/docs/architecture/api/sdk/workspace/exec.md @@ -37,7 +37,7 @@ layerfs workspace exec [--stream] [--message TEXT] | Name | Kind | Rule | Source | | --- | --- | --- | --- | | `ws` | Text | `:` or `:` from [workspace.mount](mount.md) | [`common/names.rs`](../../../../../api/crates/layerfs-control/src/common/names.rs) `WorkspaceRef` | -| `command` | Text | run as `bash -c ` as the Sandbox's command identity, `HOME` its scratch folder, in the Workspace directory | [`sandbox/exec.rs`](../../../../../api/crates/layerfs-control/src/sandbox/exec.rs) | +| `command` | Text | run as `bash -c ` as the Sandbox's command identity, `HOME` its scratch folder (on `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), in the Workspace directory | [`sandbox/exec.rs`](../../../../../api/crates/layerfs-control/src/sandbox/exec.rs), [`host_process/exec.rs`](../../../../../connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs) | ## Options diff --git a/docs/architecture/connect/sandboxes/commands-and-streams.md b/docs/architecture/connect/sandboxes/commands-and-streams.md index 4d686507db..2219695f44 100644 --- a/docs/architecture/connect/sandboxes/commands-and-streams.md +++ b/docs/architecture/connect/sandboxes/commands-and-streams.md @@ -38,7 +38,7 @@ the Workspace through ordinary file calls on the mount. | --- | --- | --- | | Identity | uid and gid non-zero, else `Protocol("nonroot command uid/gid")`. | [`types/execution.rs`](../../../../connect/crates/layerfs-sandbox/src/types/execution.rs) | | Arguments | At least one, first non-empty, no NUL. No shell is added by the runtime; `bash -c` comes from the API. | [`types/request.rs`](../../../../connect/crates/layerfs-sandbox/src/types/request.rs) | -| Environment | `NAME=value`, not starting with `=`, no NUL. Nothing else of the runtime's environment reaches the command but `PATH` (local backends). | same; [`host_process/exec.rs`](../../../../connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs) | +| Environment | `NAME=value`, not starting with `=`, no NUL. Nothing else of the runtime's environment reaches the command but `PATH` (local backends) and, on `HostProcess`, `RUSTUP_HOME`, `CARGO_HOME` and `GIT_CONFIG_GLOBAL` from the caller's environment or `HOME` (`~/.rustup`, `~/.cargo`, `~/.gitconfig` or XDG `git/config`) when present and not overridden by `request.environment`. | same; [`host_process/exec.rs`](../../../../connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs) | | Directory | Absolute, no NUL. | same | | Stdin | Local backends refuse it (`Unsupported`); Docker `run` refuses it, while the Docker Exec API itself supports it (see the example below). | [`host_process/exec.rs`](../../../../connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs), [`docker/exec.rs`](../../../../connect/crates/layerfs-sandbox/src/backend/docker/exec.rs) | | Timeout | None. A command runs until it ends. | each `exec.rs` | From 1d24657d737092d1fbbd1c269a7410b08316c1b4 Mon Sep 17 00:00:00 2001 From: liuyuchen <152110621+jiangkoumo@users.noreply.github.com> Date: Thu, 5 Nov 2026 18:54:42 +0800 Subject: [PATCH 2/6] store, daemon: ensure Store directory is private 0700 and report clear permission errors When the Store directory does not exist, kernel_guard::check creates it as 0700 (without changing permissions on existing user directories). When it already exists with non-private mode (mode & 0o077 != 0), wrong owner or non-directory type, kernel_guard::check refuses up front during store.create, project.init and StoreService::open_direct with the directory path, current mode, expected mode 0700 and chmod command. In layerfs-daemon's config::private_directory, include the directory path and which check failed (type, uid or mode with chmod hint). Document the private 0700 Store directory requirement on native macOS. Production LOC: 137010 -> 137111 (delta +101) Scope: tools/production_loc.py, combined core+engine+connect+api (reference absent (retired R9c; last counted 65,417 at tree 498dd1917812)). core 38400 -> 38400 (0); engine 62799 -> 62899 (+100); connect 15947 -> 15947 (0); api 19864 -> 19865 (+1). --- .../references/methods/sandbox.md | 2 +- .../layerfs-api/tests/project_init_worker.rs | 2 +- api/crates/layerfs-api/tests/store_family.rs | 36 ++++++ .../layerfs-control/src/store/create.rs | 9 +- .../layerfs-control/src/store/identity.rs | 10 +- docs/architecture/api/sdk/project/init.md | 4 +- docs/architecture/api/sdk/store/create.md | 1 + docs/architecture/engine/daemon/startup.md | 2 +- .../engine/store-calls/service.md | 9 +- docs/versioned/2.0.0/machines-and-stores.md | 2 +- .../layerfs-daemon/src/application/config.rs | 35 +++++- .../crates/layerfs-store-service/Cargo.toml | 2 +- .../layerfs-store-service/src/kernel_guard.rs | 107 ++++++++++++++++-- .../tests/kernel_guard.rs | 43 ++++++- 14 files changed, 228 insertions(+), 36 deletions(-) diff --git a/.agents/skills/layerfs-js-sdk/references/methods/sandbox.md b/.agents/skills/layerfs-js-sdk/references/methods/sandbox.md index 0ebc7b7211..d08d529a12 100644 --- a/.agents/skills/layerfs-js-sdk/references/methods/sandbox.md +++ b/.agents/skills/layerfs-js-sdk/references/methods/sandbox.md @@ -61,7 +61,7 @@ ready. | Machine | Sandbox | | --- | --- | -| `native` on macOS | host processes, Workspaces over loopback NFS. Needs the daemon: the native machine record's `daemon`, else `~/.cache/layerfs/bundle//-/layerfs-daemon` (`Unavailable` when missing) | +| `native` on macOS | host processes, Workspaces over loopback NFS. The sqlite Store's directory (`/store/`) must be owned by this user and private (mode `0700`, no group/other access; created `0700` when missing). Needs the daemon: the native machine record's `daemon`, else `~/.cache/layerfs/bundle//-/layerfs-daemon` (`Unavailable` when missing) | | `native` on Linux | Linux namespaces | | a VM or ssh machine | sent to that machine's worker, which runs it on its own `native`. On `ssh` and `lima` this process then forwards the daemon's port to its loopback and keeps its keys in the home (#535 G4): the Workspace calls but `exec` go to the daemon directly. A forward that fails leaves the Sandbox on the worker route and the answer says `forward_failed` | | `docker`, sqlite Store on `docker` | a container on `image`, else the docker machine's image (`machine update docker --image`), else the bundle's Sandbox image, loaded into Docker once (#535 P5); on macOS and Linux the docker worker starts it beside itself, mounting only the Store's folder (G8). Its daemon's port is published on this host's loopback: the Workspace calls but `exec` go to it directly (#535 G4) | diff --git a/api/crates/layerfs-api/tests/project_init_worker.rs b/api/crates/layerfs-api/tests/project_init_worker.rs index a8adc85a41..2c7acd0801 100644 --- a/api/crates/layerfs-api/tests/project_init_worker.rs +++ b/api/crates/layerfs-api/tests/project_init_worker.rs @@ -41,7 +41,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 1f3b90a991..2187565168 100644 --- a/api/crates/layerfs-api/tests/store_family.rs +++ b/api/crates/layerfs-api/tests/store_family.rs @@ -63,8 +63,44 @@ 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"); + 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 + ); assert_eq!(made.get_text("machine"), Some("native")); assert!( made.get("cursor").is_none(), diff --git a/api/crates/layerfs-control/src/store/create.rs b/api/crates/layerfs-control/src/store/create.rs index e92170b2fd..52e5be5d9f 100644 --- a/api/crates/layerfs-control/src/store/create.rs +++ b/api/crates/layerfs-control/src/store/create.rs @@ -14,7 +14,10 @@ pub(super) fn create(home: &Path, name: &str) -> Outcome { return Err(refused("Exists", format!("a Store named {name} exists"))); } let directory = paths::store_dir(home, name); - home::private_dir(&directory)?; + let existed = directory.exists(); + if let Some(parent) = directory.parent() { + home::private_dir(parent)?; + } let identity = crate::store::identity::Identity { kind: crate::store::identity::Kind::Sqlite, binding: crate::sandbox::registry::random()?.to_vec(), @@ -26,7 +29,9 @@ pub(super) fn create(home: &Path, name: &str) -> Outcome { Ok(sealed) => sealed, Err(refusal) => { // nothing was published: this call's own directory goes - let _ = std::fs::remove_dir_all(&directory); + if !existed { + let _ = std::fs::remove_dir_all(&directory); + } return Err(refusal); } }; diff --git a/api/crates/layerfs-control/src/store/identity.rs b/api/crates/layerfs-control/src/store/identity.rs index db4c0f0089..2557259361 100644 --- a/api/crates/layerfs-control/src/store/identity.rs +++ b/api/crates/layerfs-control/src/store/identity.rs @@ -227,13 +227,9 @@ pub(crate) fn make_empty( identity: &Identity, policy: layerfs_storage::StoragePolicy, ) -> 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; + layerfs_store_service::kernel_guard::check(global) + .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/docs/architecture/api/sdk/project/init.md b/docs/architecture/api/sdk/project/init.md index 9b1428c1a8..760abc11fe 100644 --- a/docs/architecture/api/sdk/project/init.md +++ b/docs/architecture/api/sdk/project/init.md @@ -102,7 +102,7 @@ The summary is built in [`project/record.rs`](../../../../../api/crates/layerfs- | Store kind | Platform of the Store's machine | Runs in | | --- | --- | --- | | PostgreSQL | any | caller's process (needs a build with the `postgresql` feature) | -| sqlite | executes locally (`native` on Linux or macOS) | caller's process; the kernel guard admits the Store's directory only on Linux ext4, xfs or btrfs, or macOS apfs or hfs | +| sqlite | executes locally (`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 | any other machine, and `native` on Windows | that machine's worker, on a copy of the folder | Init is a live add, not a seal: `ProjectApi::add_project` opens the @@ -125,7 +125,7 @@ Store calls hide that Branch: they treat `main` as the line of Layers (see | `Exists` | the project's record, entry or LayerStack name exists | no | `project/family.rs`, `store/identity.rs` | | `Capacity` | worker route: the machine's free disk is below the copy plus headroom | no | `project/send.rs` `room` | | `Unavailable` | worker route: no worker recorded, or not reached; nothing sent | no | [`common/place.rs`](../../../../../api/crates/layerfs-control/src/common/place.rs) | -| `Unsupported` | the kernel guard refuses the Store's filesystem; the Store has no acquisition tables; PostgreSQL in a build without `postgresql` | no | `store/identity.rs`, [`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); the Store has no acquisition tables; PostgreSQL in a build without `postgresql` | no | `store/identity.rs`, [`kernel_guard.rs`](../../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | | `NoStore` | no Store file at the Store's path | no | `store/identity.rs` | | `Failed` | deadline passed, read error, or any engine failure, **including an Init whose outcome is unknown** | **may be** | `store/identity.rs` `add_failure` | | `Unknown` | **worker route: a stage or the `project.init` answer was lost** | **yes** | [`machine/worker/client.rs`](../../../../../api/crates/layerfs-control/src/machine/worker/client.rs) | diff --git a/docs/architecture/api/sdk/store/create.md b/docs/architecture/api/sdk/store/create.md index 2b9c136d25..73c86a5ec9 100644 --- a/docs/architecture/api/sdk/store/create.md +++ b/docs/architecture/api/sdk/store/create.md @@ -103,6 +103,7 @@ create it and then [push](push.md) it. See the | `InvalidInput` | a bad Store name; a folder-scoped client | no | [`common/names.rs`](../../../../../api/crates/layerfs-control/src/common/names.rs), [`calls/scope.rs`](../../../../../api/crates/layerfs-control/src/calls/scope.rs) | | `Exists` | a Store record of that name exists, or a file is already at the target | no | [`create.rs`](../../../../../api/crates/layerfs-control/src/store/create.rs), [`identity.rs`](../../../../../api/crates/layerfs-control/src/store/identity.rs) `make_empty` | | `Unavailable` | another process held the home lock or the Store's lock for 120 s | no | [`common/home/lock.rs`](../../../../../api/crates/layerfs-control/src/common/home/lock.rs) | +| `Unsupported` | the kernel guard refuses `store//` (not on a local lock-carrying filesystem, or an existing directory is not owned by this user or has group/other access instead of mode `0700`) | no | [`identity.rs`](../../../../../api/crates/layerfs-control/src/store/identity.rs), [`kernel_guard.rs`](../../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | | `Failed` | the empty Store could not be made or written (`Store create: …`), or the record write failed | no | [`identity.rs`](../../../../../api/crates/layerfs-control/src/store/identity.rs) | A failure before the record is written removes the call's own diff --git a/docs/architecture/engine/daemon/startup.md b/docs/architecture/engine/daemon/startup.md index 295732b6c6..31f0221939 100644 --- a/docs/architecture/engine/daemon/startup.md +++ b/docs/architecture/engine/daemon/startup.md @@ -39,7 +39,7 @@ carry no key and no command. | `command_uid` differs from the daemon uid, except on the NFS route | `command and daemon identities must differ` | [`config.rs`](../../../../engine/crates/layerfs-daemon/src/application/config.rs) | | `serial_low_water` below `layerfs_workspace::SERIAL_REFILL` | `serial low-water must be below the refill window` | [`config.rs`](../../../../engine/crates/layerfs-daemon/src/application/config.rs) | | Store, overlay and config paths outside the mount home; Store is not the overlay file | `separate protected backing and mount paths` | [`config.rs`](../../../../engine/crates/layerfs-daemon/src/application/config.rs) | -| Overlay and Store parents are private `0700` directories (created once if absent); an installed Store file is protected | `private directory ownership/mode/type` | [`config.rs`](../../../../engine/crates/layerfs-daemon/src/application/config.rs) | +| Overlay and Store parents are private `0700` directories (created once if absent); an installed Store file is protected | `private directory ownership/mode/type at : …` | [`config.rs`](../../../../engine/crates/layerfs-daemon/src/application/config.rs) | The `DaemonSetup` fields are listed on [Bridge: Daemon setup](bridge.md#daemon-setup). diff --git a/docs/architecture/engine/store-calls/service.md b/docs/architecture/engine/store-calls/service.md index 3735f832fc..35aabb3756 100644 --- a/docs/architecture/engine/store-calls/service.md +++ b/docs/architecture/engine/store-calls/service.md @@ -77,9 +77,12 @@ Backend details are in [Store: Lifecycle](../store/lifecycle.md) and [Store: Backends](../store/backends.md). The kernel guard ([`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs)) -allows ext4, xfs and btrfs on Linux and apfs or hfs on macOS (for the native -Sandbox, where the SDK and daemon share one macOS kernel). Every other platform -is `Unsupported`. +ensures the Store's directory exists as a private `0700` directory owned by the +current effective user (creating it `0700` when missing, refusing a symlink, +wrong owner or `mode & 0o077 != 0` without modifying an existing directory's +permissions), and allows ext4, xfs and btrfs on Linux and apfs or hfs on macOS +(for the `native` machine, where the SDK and daemon share one macOS kernel). +Every other platform or filesystem is `Unsupported`. ```text StoreService::bind(workspace, branch) Store::bind_private diff --git a/docs/versioned/2.0.0/machines-and-stores.md b/docs/versioned/2.0.0/machines-and-stores.md index 263a7add03..5863ace0cc 100644 --- a/docs/versioned/2.0.0/machines-and-stores.md +++ b/docs/versioned/2.0.0/machines-and-stores.md @@ -16,7 +16,7 @@ a copy of a sqlite Store in a bucket. | 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 | — | Docker containers on the image `machine update docker --image` records, else the bundle's Sandbox image, loaded into Docker once (#535 P5); see Routes | `docker` | | `apple-container` | many | `machine create` | namespaces inside the VM | `container` | | `microsandbox` | many | `machine create` | namespaces inside the VM | `msb` | 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/kernel_guard.rs b/engine/crates/layerfs-store-service/src/kernel_guard.rs index e0e3945972..5a88e26844 100644 --- a/engine/crates/layerfs-store-service/src/kernel_guard.rs +++ b/engine/crates/layerfs-store-service/src/kernel_guard.rs @@ -27,15 +27,102 @@ 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. +/// Ensures `directory` exists (creating it `0700` when missing) and satisfies +/// the daemon's private-directory requirements: a real directory (not a +/// symlink) owned by the current effective user with no group/other access +/// (`mode & 0o077 == 0`). Existing directories' permissions are never modified. +#[cfg(any(target_os = "linux", target_os = "macos"))] +pub fn ensure_private_directory(directory: &Path) -> Result<(), Unsupported> { + use std::fs; + use std::os::unix::fs::{DirBuilderExt, MetadataExt}; + let euid = nix::unistd::geteuid().as_raw(); + let meta = match fs::symlink_metadata(directory) { + Ok(meta) => meta, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + fs::DirBuilder::new() + .mode(0o700) + .create(directory) + .map_err(|create_err| { + Unsupported(format!( + "the Store's directory {} cannot be examined: {error} (create 0700 failed: {create_err})", + directory.display() + )) + })?; + fs::symlink_metadata(directory).map_err(|stat_err| { + Unsupported(format!( + "the Store's directory {} cannot be examined: {stat_err}", + directory.display() + )) + })? + } + Err(error) => { + return Err(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(()) +} + +/// The directory Checked by the guard: `store` directly when it is already an +/// existing directory (or has no file component), otherwise `store.parent()` +/// when `store` names the `store.sqlite` file inside its directory. +#[cfg(any(target_os = "linux", target_os = "macos"))] +fn store_directory(store: &Path) -> &Path { + if store.is_dir() { + return store; + } + let is_store_file = store + .file_name() + .and_then(|n| n.to_str()) + .is_some_and(|name| name.starts_with("store.sqlite") || name.ends_with(".sqlite")); + if is_store_file { + store + .parent() + .filter(|parent| !parent.as_os_str().is_empty()) + .unwrap_or_else(|| Path::new(".")) + } else { + store + } +} + +/// Checks the platform, private-directory ownership/mode (creating `store`'s +/// directory `0700` when missing) and the filesystem holding `store`'s +/// directory, before anything is opened. Returns the filesystem's name. pub fn check(store: &Path) -> Result<&'static str, Unsupported> { #[cfg(target_os = "linux")] { - let directory = store - .parent() - .filter(|parent| !parent.as_os_str().is_empty()) - .unwrap_or_else(|| Path::new(".")); + let directory = store_directory(store); + ensure_private_directory(directory)?; let stat = nix::sys::statfs::statfs(directory).map_err(|error| { Unsupported(format!( "the Store's directory {} cannot be examined: {error}", @@ -56,15 +143,13 @@ pub fn check(store: &Path) -> Result<&'static str, Unsupported> { )) }) } - // macOS native (LAYERFS_SANDBOX=native): the daemon, the SDK and every + // macOS native (the `native` machine): the daemon, the SDK and every // command share this one macOS kernel, so the Store's locks coordinate // exactly as on Linux; only a local filesystem may hold the Store. #[cfg(target_os = "macos")] { - let directory = store - .parent() - .filter(|parent| !parent.as_os_str().is_empty()) - .unwrap_or_else(|| Path::new(".")); + let directory = store_directory(store); + ensure_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/kernel_guard.rs b/engine/crates/layerfs-store-service/tests/kernel_guard.rs index e99d46ea85..9bbaa99fd7 100644 --- a/engine/crates/layerfs-store-service/tests/kernel_guard.rs +++ b/engine/crates/layerfs-store-service/tests/kernel_guard.rs @@ -27,11 +27,52 @@ 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_created_0700_and_non_private_directory_is_refused() { + use std::os::unix::fs::PermissionsExt; + let parent = scratch(&std::env::temp_dir(), "private-mode"); + let missing = parent.join("global"); + assert!(!missing.exists()); + let checked = layerfs_store_service::kernel_guard::check(&missing.join("store.sqlite")); + assert!(checked.is_ok(), "{checked:?}"); + let mode = std::fs::metadata(&missing).unwrap().permissions().mode() & 0o7777; + assert_eq!(mode, 0o700); + + // 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() { From bb0dd0b18799614140428a277a1039a2966f43c5 Mon Sep 17 00:00:00 2001 From: liuyuchen <152110621+jiangkoumo@users.noreply.github.com> Date: Thu, 5 Nov 2026 19:01:00 +0800 Subject: [PATCH 3/6] fix(persistence,store-service): enable macOS writer admission flock and Busy open refusal On native macOS, multiple processes open the same SQLite Store directly on APFS/HFS+. Previously, Writer::acquire was a no-op on macOS (retaining an outdated comment from when it locked the main database file instead of the parent directory), and Session::connect ran initial WAL header recovery without holding the cooperative directory lock. Concurrent Store opens collided on SQLite WAL_WRITE_LOCK with busy_timeout=0 and failed with Store open: Store busy before effect wrapped as Failed instead of Busy. - writer_admission.rs: enable blocking exclusive flock on the Store's parent directory vnode on macOS (matching Linux). The parent directory is a distinct vnode from SQLite's main/WAL/SHM files, so Darwin flock never conflicts with SQLite fcntl locks and creates no extra files in the Store directory. - connection.rs: hold Writer::acquire across writable Session::connect so concurrent first openers serialize initial WAL-index recovery (-shm initialization) with open-time write transactions. - store_service.rs, layerfs-control store/{service,postgres}.rs: add OpenRefusal::Busy and map StorageError::Busy during Store open to Busy refusal code instead of Failed, keeping other open errors as Failed. - Tests: enable cross-process/multi-handle writer_admission tests on macOS and add 3-process concurrent open + reservation/commit tests in writer_admission.rs and kernel_guard.rs. Deleted tests: none. Production LOC: 137010 -> 137025 (delta +15) Core 38400 -> 38400 (+0); engine 62799 -> 62810 (+11: layerfs-persistence 12314 -> 12317, layerfs-store-service 10771 -> 10779); connect 15947 -> 15947 (+0); api 19864 -> 19868 (+4: layerfs-control 17528 -> 17532); dsh 0 -> 0 (+0). reference absent (retired R9c; last counted 65,417 at tree 498dd1917812). Method: python3 -B tools/production_loc.py --detail on exact parent d43047413 and the committed tree. --- .../layerfs-control/src/store/postgres.rs | 5 +- .../layerfs-control/src/store/service.rs | 1 + engine/crates/layerfs-persistence/Cargo.toml | 2 +- .../backend/sqlite/lane/writer_admission.rs | 29 ++- .../src/backend/sqlite/session/connection.rs | 8 + .../tests/writer_admission.rs | 97 ++++++++- .../src/store_service.rs | 14 +- .../tests/kernel_guard.rs | 201 +++++++++++++++++- 8 files changed, 324 insertions(+), 33 deletions(-) diff --git a/api/crates/layerfs-control/src/store/postgres.rs b/api/crates/layerfs-control/src/store/postgres.rs index 557ac8f16a..b1aeab72b2 100644 --- a/api/crates/layerfs-control/src/store/postgres.rs +++ b/api/crates/layerfs-control/src/store/postgres.rs @@ -103,7 +103,10 @@ pub(crate) fn open( identity.cursor_key, template, ) - .map_err(|refusal| refused("Failed", format!("{refusal:?}"))) + .map_err(|refusal| match refusal { + layerfs_store_service::OpenRefusal::Busy(reason) => refused("Busy", reason), + other => refused("Failed", format!("{other:?}")), + }) } #[cfg(not(feature = "postgresql"))] { diff --git a/api/crates/layerfs-control/src/store/service.rs b/api/crates/layerfs-control/src/store/service.rs index f420217e3f..a59fa716b0 100644 --- a/api/crates/layerfs-control/src/store/service.rs +++ b/api/crates/layerfs-control/src/store/service.rs @@ -60,6 +60,7 @@ fn open(store: &Path, args: &SdkValue) -> Result { .map_err(|refusal| match refusal { OpenRefusal::Unsupported(reason) => refused("Unsupported", reason.0), OpenRefusal::Missing(reason) => refused("NoStore", reason), + OpenRefusal::Busy(reason) => refused("Busy", reason), OpenRefusal::Failed(reason) => refused("Failed", reason), }) } diff --git a/engine/crates/layerfs-persistence/Cargo.toml b/engine/crates/layerfs-persistence/Cargo.toml index 5cab686620..7e8a0fc3c2 100644 --- a/engine/crates/layerfs-persistence/Cargo.toml +++ b/engine/crates/layerfs-persistence/Cargo.toml @@ -30,7 +30,7 @@ blake3 = { version = "=1.8.5", default-features = false } [target.'cfg(any(target_os = "macos", target_os = "linux"))'.dependencies] nix = { version = "=0.31.3", default-features = false, features = ["fs"] } -[target.'cfg(target_os = "linux")'.dev-dependencies] +[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dev-dependencies] nix = { version = "=0.31.3", default-features = false, features = ["fs", "pthread", "signal"] } [target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies] diff --git a/engine/crates/layerfs-persistence/src/backend/sqlite/lane/writer_admission.rs b/engine/crates/layerfs-persistence/src/backend/sqlite/lane/writer_admission.rs index 6ced5c703e..98ab4d9ce2 100644 --- a/engine/crates/layerfs-persistence/src/backend/sqlite/lane/writer_admission.rs +++ b/engine/crates/layerfs-persistence/src/backend/sqlite/lane/writer_admission.rs @@ -1,7 +1,7 @@ -//! Linux local-volume cooperative ownership before one SQLite write attempt. +//! Cooperative local-volume ownership before one SQLite write attempt. use crate::backend::records::BackendError; use std::path::Path; -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] use { nix::{ errno::Errno, @@ -11,20 +11,22 @@ use { }; pub(crate) struct Writer { - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "linux", target_os = "macos"))] lock: Flock, } impl Writer { pub(crate) fn acquire(path: &Path) -> Result { - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "linux", target_os = "macos"))] { // Never open another FD to SQLite's main/WAL/SHM files here: // closing any main-file FD cancels every process POSIX lock on - // that inode, including SQLite's live SHARED locks. An independent - // description of the existing parent directory coordinates short - // writer jobs without interfering with SQLite's descriptors. - // Multiple Store files in one directory share this coarse lane. + // that inode, including SQLite's live SHARED locks, and Darwin + // flock on the main file conflicts with SQLite's fcntl locks. An + // independent description of the existing parent directory + // coordinates short writer jobs without interfering with SQLite's + // descriptors. Multiple Store files in one directory share this + // coarse lane. let directory = path .parent() .filter(|parent| !parent.as_os_str().is_empty()) @@ -47,13 +49,6 @@ impl Writer { Err(_) => Err(BackendError::Integrity), } } - #[cfg(target_os = "macos")] - { - // Darwin's flock conflicts with SQLite's open-file locks. Host Init - // is sole-owner; cross-session contention retains exact SQLite Busy. - let _ = path; - Ok(Self {}) - } #[cfg(not(any(target_os = "linux", target_os = "macos")))] { let _ = path; @@ -62,7 +57,7 @@ impl Writer { } pub(crate) fn release(self) -> Result<(), Self> { - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "linux", target_os = "macos"))] { match self.lock.unlock() { Ok(file) => { @@ -72,7 +67,7 @@ impl Writer { Err((lock, _)) => Err(Self { lock }), } } - #[cfg(not(target_os = "linux"))] + #[cfg(not(any(target_os = "linux", target_os = "macos")))] { Ok(()) } diff --git a/engine/crates/layerfs-persistence/src/backend/sqlite/session/connection.rs b/engine/crates/layerfs-persistence/src/backend/sqlite/session/connection.rs index 8c5d1cc8de..cbc868e4d8 100644 --- a/engine/crates/layerfs-persistence/src/backend/sqlite/session/connection.rs +++ b/engine/crates/layerfs-persistence/src/backend/sqlite/session/connection.rs @@ -159,6 +159,11 @@ impl Session { if !cfg!(any(target_os = "macos", target_os = "linux")) { return Err(BackendError::Integrity); } + let writer = if writable { + Some(crate::backend::sqlite::lane::writer_admission::Writer::acquire(path)?) + } else { + None + }; let flags = if writable { OpenFlags::SQLITE_OPEN_READ_WRITE } else { @@ -246,6 +251,9 @@ impl Session { .map_err(rows::error)? as usize, }; profile::check(&profile)?; + if let Some(writer) = writer { + writer.release().map_err(|_| BackendError::Unknown)?; + } super::thread_work::add(&work.borrow()); Ok(Self { admission: crate::backend::admission::Admission::default(), diff --git a/engine/crates/layerfs-persistence/tests/writer_admission.rs b/engine/crates/layerfs-persistence/tests/writer_admission.rs index eb9fce9fa5..af29e1649a 100644 --- a/engine/crates/layerfs-persistence/tests/writer_admission.rs +++ b/engine/crates/layerfs-persistence/tests/writer_admission.rs @@ -4,7 +4,7 @@ use layerfs_content::{FinalizedObject, ObjectRole}; use layerfs_history::HistoryCatalogConfig; use layerfs_persistence::{PersistenceConfig, SqliteHandles, SqlitePersistenceProfile}; use layerfs_storage::{location::PackInfo, port::*, Storage, StoragePolicy}; -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] use std::{ io::{Read, Write}, os::unix::net::{UnixListener, UnixStream}, @@ -26,7 +26,7 @@ fn config(path: &Path) -> PersistenceConfig { fn create(path: &Path) -> SqliteHandles { SqliteHandles::create(config(path), StoragePolicy::frozen_default(), &authority()).unwrap() } -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] fn open(path: &Path) -> SqliteHandles { SqliteHandles::open_writable( config(path), @@ -130,12 +130,12 @@ fn shared_session_reservation_waits_for_an_ordinary_public_read_owner() { assert_eq!(after.rollbacks, before.rollbacks); } -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] struct Held { child: Child, socket: UnixStream, } -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] impl Held { fn start(path: &Path, socket_path: &Path) -> Self { let listener = UnixListener::bind(socket_path).unwrap(); @@ -169,6 +169,7 @@ impl Held { } }; let mut held = Self { child, socket }; + held.socket.set_nonblocking(false).unwrap(); held.socket .set_read_timeout(Some(Duration::from_secs(3))) .unwrap(); @@ -189,11 +190,11 @@ impl Held { break; } assert!(Instant::now() < deadline, "child exit deadline"); - std::thread::yield_now(); + std::thread::yield_now() } } } -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] impl Drop for Held { fn drop(&mut self) { let _ = self.child.kill(); @@ -202,7 +203,7 @@ impl Drop for Held { } #[test] -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] fn another_process_admission_precedes_the_single_sqlite_begin() { // Keep the owned AF_UNIX path below Darwin's sockaddr_un capacity. let temp = support::Temp::new("wa"); @@ -243,7 +244,7 @@ fn another_process_admission_precedes_the_single_sqlite_begin() { } #[test] -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] fn two_opened_handles_admit_eight_distinct_reservations_without_conflict() { let temp = support::Temp::new("writer-admission-two-handles"); let path = temp.join("store.sqlite"); @@ -373,7 +374,7 @@ fn queued_writers_outlive_the_old_window_and_all_succeed() { } #[test] -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] fn a_cross_process_waiter_outlives_the_old_window_and_succeeds() { let temp = support::Temp::new("wl"); let path = temp.join("store.sqlite"); @@ -497,7 +498,7 @@ fn writers_queue_behind_a_held_publication_lane_without_deadlock() { } #[test] -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] fn a_holder_process_that_exits_hands_the_lock_to_the_waiter() { let temp = support::Temp::new("wx"); let path = temp.join("store.sqlite"); @@ -518,7 +519,7 @@ fn a_holder_process_that_exits_hands_the_lock_to_the_waiter() { } #[test] -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] fn a_signalled_kernel_waiter_is_cancelled_and_leaves_the_queue_cleanly() { use nix::sys::{ pthread, @@ -606,3 +607,77 @@ fn a_thread_reentering_its_own_turn_is_refused_instead_of_deadlocking() { // The turn was released: the next call is admitted at once. assert!(handles.storage.reserve(reserve()).is_ok()); } + +#[test] +#[cfg(any(target_os = "linux", target_os = "macos"))] +fn concurrent_open_child() { + let Some(path) = std::env::var_os("LAYERFS_ADMISSION_OPEN_STORE") else { + return; + }; + let mut sock = + UnixStream::connect(std::env::var_os("LAYERFS_ADMISSION_OPEN_SOCK").unwrap()).unwrap(); + sock.write_all(b"R").unwrap(); + let mut go = [0u8; 1]; + sock.read_exact(&mut go).unwrap(); + let handles = open(Path::new(&path)); + for _ in 0..20 { + handles.storage.reserve(reserve()).unwrap(); + } +} + +#[test] +#[cfg(any(target_os = "linux", target_os = "macos"))] +fn three_processes_open_and_reserve_concurrently() { + let temp = support::Temp::new("wo"); + let path = temp.join("store.sqlite"); + create(&path).seal().unwrap(); + let sock_path = temp.join("s"); + let listener = UnixListener::bind(&sock_path).unwrap(); + listener.set_nonblocking(true).unwrap(); + let exe = std::env::current_exe().unwrap(); + let children: Vec<_> = (0..3) + .map(|_| { + Command::new(&exe) + .args(["--exact", "concurrent_open_child", "--nocapture"]) + .env("LAYERFS_ADMISSION_OPEN_STORE", &path) + .env("LAYERFS_ADMISSION_OPEN_SOCK", &sock_path) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap() + }) + .collect(); + let mut sockets = Vec::new(); + let deadline = Instant::now() + Duration::from_secs(5); + while sockets.len() < 3 { + match listener.accept() { + Ok((mut stream, _)) => { + stream.set_nonblocking(false).unwrap(); + let mut ready = [0u8; 1]; + stream.read_exact(&mut ready).unwrap(); + assert_eq!(&ready, b"R"); + sockets.push(stream); + } + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock && Instant::now() < deadline => { + std::thread::yield_now(); + } + Err(e) => panic!("barrier accept: {e}"), + } + } + for mut stream in sockets { + stream.write_all(b"G").unwrap(); + } + for child in children { + let output = child.wait_with_output().unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + } + let handles = open(&path); + let next = handles.storage.reserve(reserve()).unwrap(); + assert_eq!(next.first_pack_id, 61); + assert_eq!(next.first_ordinal, 61); +} diff --git a/engine/crates/layerfs-store-service/src/store_service.rs b/engine/crates/layerfs-store-service/src/store_service.rs index 1c9d59c3a4..7752d16845 100644 --- a/engine/crates/layerfs-store-service/src/store_service.rs +++ b/engine/crates/layerfs-store-service/src/store_service.rs @@ -42,10 +42,20 @@ pub enum OpenRefusal { Unsupported(Unsupported), /// The Store file is absent: no Sandbox installed it into the volume yet. Missing(String), + /// Another writer held the Store at open before any effect. + Busy(String), /// The Store or the overlay template failed to open. Failed(String), } +fn open_refusal(error: layerfs_storage::StorageError) -> OpenRefusal { + let reason = format!("Store open: {error}"); + match error { + layerfs_storage::StorageError::Busy => OpenRefusal::Busy(reason), + _ => OpenRefusal::Failed(reason), + } +} + /// The Store and overlay owner of this process's Store calls. pub struct StoreService { pub opened: OpenedStore, @@ -99,7 +109,7 @@ impl StoreService { CACHE_BYTES, ReservationBlocks::default(), ) - .map_err(|error| OpenRefusal::Failed(format!("Store open: {error}")))?; + .map_err(open_refusal)?; Self::owning(opened, template) } @@ -122,7 +132,7 @@ impl StoreService { CACHE_BYTES, ReservationBlocks::default(), ) - .map_err(|error| OpenRefusal::Failed(format!("Store open: {error}")))?; + .map_err(open_refusal)?; Self::owning(opened, template) } diff --git a/engine/crates/layerfs-store-service/tests/kernel_guard.rs b/engine/crates/layerfs-store-service/tests/kernel_guard.rs index e99d46ea85..ddbb8df703 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}; @@ -41,6 +40,206 @@ fn direct_access_is_allowed_on_a_local_macos_filesystem() { std::fs::remove_dir_all(directory).unwrap(); } +#[allow(dead_code)] +#[path = "support/installed_store.rs"] +mod installed; + +#[cfg(any(target_os = "linux", target_os = "macos"))] +#[test] +fn open_direct_reports_busy_when_locked_and_failed_when_corrupt() { + let fixture = installed::Fixture::new(1, "open-refusal"); + let sql = rusqlite::Connection::open(&fixture.config.path).unwrap(); + sql.busy_timeout(std::time::Duration::ZERO).unwrap(); + sql.execute_batch("BEGIN IMMEDIATE").unwrap(); + let template = fixture.directory.join("overlay.sqlite"); + let busy = StoreService::open_direct( + &fixture.config.path, + installed::BINDING, + installed::CURSOR, + &template, + ); + match busy { + Err(OpenRefusal::Busy(reason)) => { + assert_eq!(reason, "Store open: Store busy before effect"); + } + Err(other) => panic!("expected OpenRefusal::Busy, got {other:?}"), + Ok(_) => panic!("expected OpenRefusal::Busy, Store opened"), + } + sql.execute_batch("ROLLBACK").unwrap(); + drop(sql); + + let corrupt = fixture.directory.join("corrupt.sqlite"); + std::fs::write(&corrupt, b"not a valid sqlite store").unwrap(); + let failed = + StoreService::open_direct(&corrupt, installed::BINDING, installed::CURSOR, &template); + match failed { + Err(OpenRefusal::Failed(reason)) => assert!(reason.starts_with("Store open:"), "{reason}"), + Err(other) => panic!("expected OpenRefusal::Failed, got {other:?}"), + Ok(_) => panic!("expected OpenRefusal::Failed, Store opened"), + } + fixture.cleanup(); +} + +#[test] +fn open_concurrent_child() { + let Some(store) = std::env::var_os("LAYERFS_OPEN_CONCURRENT_STORE") else { + return; + }; + use layerfs_bridge::control::{SdkCall, SdkValue}; + use std::io::{Read, Write}; + use std::os::unix::net::UnixStream; + let id = std::env::var("LAYERFS_OPEN_CONCURRENT_ID").unwrap(); + let template = + std::path::PathBuf::from(std::env::var_os("LAYERFS_OPEN_CONCURRENT_TPL").unwrap()); + let mut sock = + UnixStream::connect(std::env::var_os("LAYERFS_OPEN_CONCURRENT_SOCK").unwrap()).unwrap(); + sock.write_all(b"R").unwrap(); + let mut go = [0u8; 1]; + sock.read_exact(&mut go).unwrap(); + let service = StoreService::open_direct( + Path::new(&store), + installed::BINDING, + installed::CURSOR, + &template, + ) + .unwrap_or_else(|e| panic!("agent-{id} open_direct: {e:?}")); + let call = |method: &str, fields: Vec<(&str, SdkValue)>| { + let mut map: Vec<(String, SdkValue)> = + fields.into_iter().map(|(k, v)| (k.to_owned(), v)).collect(); + map.push(("project".to_owned(), SdkValue::text("installed"))); + layerfs_store_service::methods::call( + &service, + &SdkCall { + method: method.to_owned(), + args: SdkValue::Map(map), + }, + ) + .unwrap_or_else(|e| panic!("agent-{id} {method}: {e:?}")) + }; + let branch = format!("agent-{id}"); + call("branch.fork", vec![("name", SdkValue::text(&branch))]); + for step in 0..20 { + call( + "fs.write", + vec![ + ("branch", SdkValue::text(&branch)), + ("path", SdkValue::text(format!("/agent-{id}-{step}.txt"))), + ( + "bytes", + SdkValue::Bytes(format!("step {step}\n").into_bytes()), + ), + ( + "message", + SdkValue::text(format!("agent-{id} commit {step}")), + ), + ], + ); + } + service.close().unwrap(); +} + +#[cfg(any(target_os = "linux", target_os = "macos"))] +#[test] +fn three_processes_open_direct_and_commit_concurrently() { + use std::io::{Read, Write}; + use std::os::unix::net::UnixListener; + use std::process::{Command, Stdio}; + use std::time::{Duration, Instant}; + + let fixture = installed::Fixture::new(2, "concurrent-open"); + let sock_path = fixture.directory.join("barrier.sock"); + let listener = UnixListener::bind(&sock_path).unwrap(); + listener.set_nonblocking(true).unwrap(); + + let exe = std::env::current_exe().unwrap(); + let mut children = Vec::new(); + for id in 0..3 { + let overlay_dir = fixture.directory.join(format!("overlay-{id}")); + std::fs::create_dir(&overlay_dir).unwrap(); + let tpl = overlay_dir.join("overlay.sqlite"); + let child = Command::new(&exe) + .args(["--exact", "open_concurrent_child", "--nocapture"]) + .env("LAYERFS_OPEN_CONCURRENT_STORE", &fixture.config.path) + .env("LAYERFS_OPEN_CONCURRENT_ID", id.to_string()) + .env("LAYERFS_OPEN_CONCURRENT_TPL", &tpl) + .env("LAYERFS_OPEN_CONCURRENT_SOCK", &sock_path) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + children.push(child); + } + let mut sockets = Vec::new(); + let deadline = Instant::now() + Duration::from_secs(10); + while sockets.len() < 3 { + match listener.accept() { + Ok((mut stream, _)) => { + stream.set_nonblocking(false).unwrap(); + let mut ready = [0u8; 1]; + stream.read_exact(&mut ready).unwrap(); + assert_eq!(&ready, b"R"); + sockets.push(stream); + } + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock && Instant::now() < deadline => { + std::thread::yield_now(); + } + Err(e) => panic!("barrier accept: {e}"), + } + } + for mut stream in sockets { + stream.write_all(b"G").unwrap(); + } + let mut failures = Vec::new(); + for (id, child) in children.into_iter().enumerate() { + let output = child.wait_with_output().unwrap(); + if !output.status.success() { + failures.push(format!( + "agent-{id}: {}", + String::from_utf8_lossy(&output.stderr) + )); + } + } + assert!( + failures.is_empty(), + "concurrent open_direct + 20 commits failed:\n{}", + failures.join("\n") + ); + let verify_dir = fixture.directory.join("overlay-verify"); + std::fs::create_dir(&verify_dir).unwrap(); + let service = StoreService::open_direct( + &fixture.config.path, + installed::BINDING, + installed::CURSOR, + &verify_dir.join("overlay.sqlite"), + ) + .unwrap(); + for id in 0..3 { + let branch = format!("agent-{id}"); + let list = layerfs_store_service::methods::call( + &service, + &layerfs_bridge::control::SdkCall { + method: "branch.list_commits".to_owned(), + args: layerfs_bridge::control::SdkValue::map([ + ( + "project", + layerfs_bridge::control::SdkValue::text("installed"), + ), + ("branch", layerfs_bridge::control::SdkValue::text(&branch)), + ("pageSize", layerfs_bridge::control::SdkValue::Int(50)), + ]), + }, + ) + .unwrap(); + let Some(layerfs_bridge::control::SdkValue::List(items)) = list.get("items") else { + panic!("missing items: {list:?}"); + }; + assert_eq!(items.len(), 20, "branch {branch} commit count"); + } + service.close().unwrap(); + fixture.cleanup(); +} + #[cfg(not(any(target_os = "linux", target_os = "macos")))] #[test] fn direct_access_is_unsupported_off_linux() { From 7beb47c527e7ef4b1410842d0d1bef8de39136f0 Mon Sep 17 00:00:00 2001 From: liuyuchen <152110621+jiangkoumo@users.noreply.github.com> Date: Thu, 5 Nov 2026 19:05:21 +0800 Subject: [PATCH 4/6] docs(architecture): update Store and Store-calls pages for macOS writer flock Updates the 2.0 architecture guide pages to reflect macOS parent-directory writer admission flock, writable Session::connect lock acquisition, and OpenRefusal::Busy on Store open. Deleted tests: none. Production LOC: 137025 -> 137025 (delta +0) Core 38400 -> 38400 (+0); engine 62810 -> 62810 (+0); connect 15947 -> 15947 (+0); api 19868 -> 19868 (+0); dsh 0 -> 0 (+0). reference absent (retired R9c; last counted 65,417 at tree 498dd1917812). Method: python3 -B tools/production_loc.py --detail on exact parent bb0dd0b18 and the committed tree. --- docs/architecture/engine/store-calls/service.md | 3 ++- docs/architecture/engine/store/README.md | 9 +++++---- docs/architecture/engine/store/backends.md | 13 +++++++------ docs/architecture/engine/store/sqlite.md | 5 +++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/docs/architecture/engine/store-calls/service.md b/docs/architecture/engine/store-calls/service.md index 3735f832fc..d365e2cd59 100644 --- a/docs/architecture/engine/store-calls/service.md +++ b/docs/architecture/engine/store-calls/service.md @@ -57,7 +57,8 @@ open_direct(store, binding, cursor_key, template) SqliteHandles::open_writable (the writer session) factory(0) -> control reader (read-only session) Store::new(..).with_merge_scratch(..) - --> Failed("Store open: ..") + --> Busy("Store open: ..") / + Failed("Store open: ..") 4 Owner::start(template, pager 1024 KiB, namespaces 64) --> Failed("overlay template: ..") ``` diff --git a/docs/architecture/engine/store/README.md b/docs/architecture/engine/store/README.md index bfb6b94094..0b99138da2 100644 --- a/docs/architecture/engine/store/README.md +++ b/docs/architecture/engine/store/README.md @@ -81,7 +81,7 @@ Nothing converts a Store from one kind to the other. | --- | --- | --- | | Platform for SQLite | macOS or Linux; elsewhere `BackendUnavailable` | [`open.rs`](../../../../engine/crates/layerfs-persistence/src/store/open.rs) | | Direct opening for Store calls | Linux local ext4/xfs/btrfs (macOS apfs/hfs); else `Unsupported`, no fallback | [`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs) | -| macOS cross-process writers | No directory lock; a second writer gets SQLite `Busy` | [`writer_admission.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sqlite/lane/writer_admission.rs) | +| Cross-process writers (Linux and macOS) | Directory `flock` on the Store's parent directory; an uncoordinated raw SQLite writer still gets SQLite `Busy` | [`writer_admission.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sqlite/lane/writer_admission.rs) | | PostgreSQL without the feature | `BackendUnavailable` | [`selection.rs`](../../../../engine/crates/layerfs-persistence/src/store/selection.rs) | | PostgreSQL lane wait | `lock_timeout_ms`, default 30 s; then `Busy` | [`config.rs`](../../../../engine/crates/layerfs-persistence/src/store/config.rs) | | Rows per transaction | `TRANSACTION_ROW_LIMIT` 8 191 | [`publication.rs`](../../../../engine/crates/layerfs-persistence/src/storage/publication.rs) | @@ -110,9 +110,10 @@ Commands are written from source and were not run while writing this page. `cargo +1.85.1 test --locked -p layerfs-persistence --test heads` - **`writer_admission.rs`** checks first-come order, `Closed` for queued writers on close, writers queued behind `publish_lane`, refusal of a - thread re-entering its own turn, and (Linux only, with child processes) the - directory lock across processes, its hand-over when a holder exits and - `Cancelled` on a signal. + thread re-entering its own turn, and (with child processes on Linux and + macOS) the directory lock across processes, concurrent opens and + reservations, its hand-over when a holder exits and `Cancelled` on a + signal. `cargo +1.85.1 test --locked -p layerfs-persistence --test writer_admission` - **`diagnostics_during_lane.rs`** holds the lane for 3 s on one thread and requires `storage.diagnostics()` to return within 750 ms. Any platform the diff --git a/docs/architecture/engine/store/backends.md b/docs/architecture/engine/store/backends.md index 6c636096e6..063b5719cb 100644 --- a/docs/architecture/engine/store/backends.md +++ b/docs/architecture/engine/store/backends.md @@ -97,8 +97,8 @@ it never queues behind a held lane. SQLite write: Session::run(true, body) any process, one host 1 admission.enter in-process FIFO turn on this session 2 state mutex the connection - 3 Writer::acquire Linux: flock(LOCK_EX) on the Store's parent - directory; macOS: none (skipped if nested) + 3 Writer::acquire flock(LOCK_EX) on the Store's parent directory + (Linux and macOS) (skipped if nested) 4 BEGIN IMMEDIATE SQLite's write lock; busy_timeout 0 -> Busy 5 body; COMMIT or ROLLBACK; Unknown -> quarantine 6 finish_writer release the flock @@ -116,16 +116,17 @@ it never queues behind a held lane. | Rule | Enforced by | | --- | --- | | One write transaction at a time per session | `Admission` turn: [`admission.rs`](../../../../engine/crates/layerfs-persistence/src/backend/admission.rs) | -| One writer across processes on one Linux host | Directory `flock`: [`writer_admission.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sqlite/lane/writer_admission.rs) | +| One writer across processes on one Linux or macOS host | Directory `flock`: [`writer_admission.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sqlite/lane/writer_admission.rs) | | One writer in the SQLite file | `BEGIN IMMEDIATE` with `busy_timeout=0`: [`transaction.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sqlite/session/transaction.rs) | | One writer across PostgreSQL clients | `store_lane` row `FOR UPDATE`: [`profile.rs`](../../../../engine/crates/layerfs-persistence/src/backend/postgresql/session/profile.rs) | | No busy handler, no retry | `busy_timeout` reads back 0; `Busy` is returned as is | The lock is taken on a separate descriptor of the Store's directory, never on the main, WAL or SHM file: closing a main-file descriptor would drop SQLite's -own POSIX locks. Several Store files in one directory share this lock. On -macOS `flock` conflicts with SQLite's locks, so there is no cross-process -writer wait: a second writer gets SQLite's `Busy`. Reads (`BEGIN`, or one +own POSIX locks, and on macOS `flock` on the main file would conflict with +SQLite's `fcntl` locks. Several Store files in one directory share this lock. +A writable `Session::connect` also takes it so concurrent first openers +serialize initial WAL-index recovery. Reads (`BEGIN`, or one implicit snapshot) take the turn but never the directory lock or the lane. The publication lane holds steps 1 and 3 across several transactions; see [History publication](history-publication.md#the-publish-lane). diff --git a/docs/architecture/engine/store/sqlite.md b/docs/architecture/engine/store/sqlite.md index a636545707..03a4d866f0 100644 --- a/docs/architecture/engine/store/sqlite.md +++ b/docs/architecture/engine/store/sqlite.md @@ -73,6 +73,7 @@ text ([`schema.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sq | Step in `Session::connect` | Detail | Source | | --- | --- | --- | +| Writer lock | When `writable`, takes `Writer::acquire(path)` across connect and releases it after `profile::check` | [`connection.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sqlite/session/connection.rs) | | Open | `READ_WRITE` or `READ_ONLY`, `NO_MUTEX`; statement cache 64 | [`connection.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sqlite/session/connection.rs) | | Guard | `busy_timeout=0`; `SQLITE_DBCONFIG_DEFENSIVE` on | same | | Settings | `profile::apply`, then read back and `profile::check` | [`profile.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sqlite/session/profile.rs) | @@ -106,8 +107,8 @@ also adds its SQL work to the calling thread's counters v inside: run(true, ..) enters nested: no flock, BEGIN IMMEDIATE ``` -The lane is one held admission turn plus, on Linux, the directory lock, kept -by one thread across several short transactions. No publication of any +The lane is one held admission turn plus the directory lock (Linux and +macOS), kept by one thread across several short transactions. No publication of any process interleaves. `LaneHold` is `!Send`: it belongs to the thread that took it. Source: [`lane.rs`](../../../../engine/crates/layerfs-persistence/src/backend/sqlite/lane/lane.rs). From 6be2f6c318f5943af2a57641699415733a3bef6d Mon Sep 17 00:00:00 2001 From: liuyuchen <152110621+jiangkoumo@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:42:13 +0800 Subject: [PATCH 5/6] fix(store,sandbox): keep kernel_guard::check side-effect-free and gate user_environment on macOS Addresses the #534 review findings after merging fix/534a-native-macos, fix/534b-native-macos and fix/534c-native-macos: - engine/crates/layerfs-store-service/tests/support/installed_store.rs: create Fixture::built_with and Fixture::cloned directories with mode 0700 so kernel_guard tests added in 534A pass with 534B's private-directory enforcement. - engine/crates/layerfs-store-service/src/kernel_guard.rs: make kernel_guard::check purely side-effect-free (never creating directories) and always take store.parent() as the Store directory without a .sqlite filename heuristic. - api/crates/layerfs-control/src/store/identity.rs, engine/crates/layerfs-store-service/src/install.rs, connect/crates/layerfs-store-backup/src/backup/restore.rs: create missing Store directories as 0700 in the actual creation/installation entrypoints (make_empty, install_local after installer_user(), and backup::restore::fetch after installer_user()), then run kernel_guard::check(destination) on the Store file path and clean up newly created directories if the guard fails. - connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs: gate user_environment() with #[cfg(target_os = "macos")] (returning empty off macOS) so Linux host_process command environments remain unchanged. - api/crates/layerfs-api/tests/{project_family,store_open_cache}.rs, engine/crates/layerfs-store-service/tests/kernel_guard.rs: verify side-effect-free missing-directory refusal, project.init 0755 refusal and macOS running-call writer lock protection. Deleted tests: none. Production LOC: 137155 -> 137153 (delta -2) Core 38400 -> 38400 (+0); engine 62910 -> 62887 (-23: layerfs-store-service 10854 -> 10831); connect 15976 -> 15992 (+16: layerfs-sandbox 8628 -> 8635, layerfs-store-backup 3227 -> 3236); api 19869 -> 19874 (+5: layerfs-control 17533 -> 17538); dsh 0 -> 0 (+0). reference absent (retired R9c; last counted 65,417 at tree 498dd1917812). Method: python3 -B tools/production_loc.py --detail on exact parent 61b644fcd and the committed tree. --- .../layerfs-api/tests/project_family.rs | 30 +++++++ .../layerfs-api/tests/store_open_cache.rs | 6 +- .../layerfs-control/src/store/identity.rs | 13 ++- .../src/backend/host_process/exec.rs | 24 ++++-- .../src/backup/restore.rs | 25 ++++-- docs/architecture/api/sdk/workspace/exec.md | 2 +- .../connect/sandboxes/commands-and-streams.md | 2 +- .../engine/store-calls/service.md | 12 +-- .../layerfs-store-service/src/install.rs | 25 ++++-- .../layerfs-store-service/src/kernel_guard.rs | 86 ++++++------------- .../tests/kernel_guard.rs | 40 +++++++-- .../tests/support/installed_store.rs | 18 ++++ 12 files changed, 178 insertions(+), 105 deletions(-) diff --git a/api/crates/layerfs-api/tests/project_family.rs b/api/crates/layerfs-api/tests/project_family.rs index 7fa844fcad..59a835a62b 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/store_open_cache.rs b/api/crates/layerfs-api/tests/store_open_cache.rs index 06b913bb3b..e9af86ceed 100644 --- a/api/crates/layerfs-api/tests/store_open_cache.rs +++ b/api/crates/layerfs-api/tests/store_open_cache.rs @@ -156,9 +156,9 @@ fn a_worker_closes_its_open_copy_so_a_store_is_pulled_at_once_and_returns_fresh( let _ = std::fs::remove_dir_all(folder); } -/// A write waits for the Store directory's writer lock (Linux): holding it -/// here keeps one call running for as long as this test wants. -#[cfg(target_os = "linux")] +/// A write waits for the Store directory's writer lock (Linux and macOS): +/// holding it here keeps one call running for as long as this test wants. +#[cfg(any(target_os = "linux", target_os = "macos"))] #[test] fn a_running_call_keeps_its_store_open_against_a_push_or_remove() { use nix::fcntl::{Flock, FlockArg}; diff --git a/api/crates/layerfs-control/src/store/identity.rs b/api/crates/layerfs-control/src/store/identity.rs index 2557259361..29a9d88bd9 100644 --- a/api/crates/layerfs-control/src/store/identity.rs +++ b/api/crates/layerfs-control/src/store/identity.rs @@ -227,8 +227,14 @@ pub(crate) fn make_empty( identity: &Identity, policy: layerfs_storage::StoragePolicy, ) -> Result { - use std::os::unix::fs::PermissionsExt; - layerfs_store_service::kernel_guard::check(global) + use std::os::unix::fs::{DirBuilderExt, PermissionsExt}; + if !global.exists() { + std::fs::DirBuilder::new() + .mode(0o700) + .create(global) + .map_err(failed)?; + } + 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) @@ -370,8 +376,7 @@ pub(crate) fn add_project(args: &SdkValue) -> Outcome { } let deadline = crate::project::init::deadline(args)?; let store_file = store_path(args)?; - 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(args)?; sqlite_only(&identity, "adding a project")?; diff --git a/connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs b/connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs index 0616e7900b..c000b13f2f 100644 --- a/connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs +++ b/connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs @@ -1,10 +1,10 @@ //! One ordinary command to its end in a local Sandbox: the command identity, //! the Workspace directory, a search path (on macOS this process's own), and -//! 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. +//! (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, process, process::LocalProcess, search::search_path}; @@ -14,7 +14,7 @@ use std::{ ffi::OsString, io::{self, Write}, os::unix::process::{CommandExt, ExitStatusExt}, - path::{Path, PathBuf}, + path::Path, process::{Command, Stdio}, }; @@ -127,10 +127,12 @@ fn check(request: &ExecRequest) -> Result<(), RuntimeError> { Ok(()) } -/// The caller's Rust and Git locations, when present, so a command whose -/// `HOME` is the Sandbox's scratch directory still sees them unless the +/// 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(); @@ -155,6 +157,12 @@ fn user_environment() -> Vec<(&'static str, OsString)> { 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/connect/crates/layerfs-store-backup/src/backup/restore.rs b/connect/crates/layerfs-store-backup/src/backup/restore.rs index a4aa64e622..8dec25bbb6 100644 --- a/connect/crates/layerfs-store-backup/src/backup/restore.rs +++ b/connect/crates/layerfs-store-backup/src/backup/restore.rs @@ -180,14 +180,23 @@ fn fetch( .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)?; + let existed = directory.exists(); + let outcome = (|| { + installer_user()?; + if !existed { + use std::os::unix::fs::DirBuilderExt; + fs::DirBuilder::new() + .mode(0o700) + .create(directory) + .map_err(text)?; + } + kernel_guard::check(destination).map_err(|reason| reason.0) + })(); + if let Err(error) = outcome { + if !existed { + let _ = fs::remove_dir_all(directory); + } + return Err(error); } let (target, _) = paths(destination).map_err(text)?; let cloning = binding.is_none(); diff --git a/docs/architecture/api/sdk/workspace/exec.md b/docs/architecture/api/sdk/workspace/exec.md index 5a547d6e18..9aa77d26e6 100644 --- a/docs/architecture/api/sdk/workspace/exec.md +++ b/docs/architecture/api/sdk/workspace/exec.md @@ -37,7 +37,7 @@ layerfs workspace exec [--stream] [--message TEXT] | Name | Kind | Rule | Source | | --- | --- | --- | --- | | `ws` | Text | `:` or `:` from [workspace.mount](mount.md) | [`common/names.rs`](../../../../../api/crates/layerfs-control/src/common/names.rs) `WorkspaceRef` | -| `command` | Text | run as `bash -c ` as the Sandbox's command identity, `HOME` its scratch folder (on `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), in the Workspace directory | [`sandbox/exec.rs`](../../../../../api/crates/layerfs-control/src/sandbox/exec.rs), [`host_process/exec.rs`](../../../../../connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs) | +| `command` | Text | run as `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), in the Workspace directory | [`sandbox/exec.rs`](../../../../../api/crates/layerfs-control/src/sandbox/exec.rs), [`host_process/exec.rs`](../../../../../connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs) | ## Options diff --git a/docs/architecture/connect/sandboxes/commands-and-streams.md b/docs/architecture/connect/sandboxes/commands-and-streams.md index 2219695f44..c4da4aa3b8 100644 --- a/docs/architecture/connect/sandboxes/commands-and-streams.md +++ b/docs/architecture/connect/sandboxes/commands-and-streams.md @@ -38,7 +38,7 @@ the Workspace through ordinary file calls on the mount. | --- | --- | --- | | Identity | uid and gid non-zero, else `Protocol("nonroot command uid/gid")`. | [`types/execution.rs`](../../../../connect/crates/layerfs-sandbox/src/types/execution.rs) | | Arguments | At least one, first non-empty, no NUL. No shell is added by the runtime; `bash -c` comes from the API. | [`types/request.rs`](../../../../connect/crates/layerfs-sandbox/src/types/request.rs) | -| Environment | `NAME=value`, not starting with `=`, no NUL. Nothing else of the runtime's environment reaches the command but `PATH` (local backends) and, on `HostProcess`, `RUSTUP_HOME`, `CARGO_HOME` and `GIT_CONFIG_GLOBAL` from the caller's environment or `HOME` (`~/.rustup`, `~/.cargo`, `~/.gitconfig` or XDG `git/config`) when present and not overridden by `request.environment`. | same; [`host_process/exec.rs`](../../../../connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs) | +| Environment | `NAME=value`, not starting with `=`, no NUL. Nothing else of the runtime's environment reaches the command but `PATH` (local backends) and, on `HostProcess` on macOS, `RUSTUP_HOME`, `CARGO_HOME` and `GIT_CONFIG_GLOBAL` from the caller's environment or `HOME` (`~/.rustup`, `~/.cargo`, `~/.gitconfig` or XDG `git/config`) when present and not overridden by `request.environment`. | same; [`host_process/exec.rs`](../../../../connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs) | | Directory | Absolute, no NUL. | same | | Stdin | Local backends refuse it (`Unsupported`); Docker `run` refuses it, while the Docker Exec API itself supports it (see the example below). | [`host_process/exec.rs`](../../../../connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs), [`docker/exec.rs`](../../../../connect/crates/layerfs-sandbox/src/backend/docker/exec.rs) | | Timeout | None. A command runs until it ends. | each `exec.rs` | diff --git a/docs/architecture/engine/store-calls/service.md b/docs/architecture/engine/store-calls/service.md index 738aba322e..f953e8a11a 100644 --- a/docs/architecture/engine/store-calls/service.md +++ b/docs/architecture/engine/store-calls/service.md @@ -78,12 +78,12 @@ Backend details are in [Store: Lifecycle](../store/lifecycle.md) and [Store: Backends](../store/backends.md). The kernel guard ([`kernel_guard.rs`](../../../../engine/crates/layerfs-store-service/src/kernel_guard.rs)) -ensures the Store's directory exists as a private `0700` directory owned by the -current effective user (creating it `0700` when missing, refusing a symlink, -wrong owner or `mode & 0o077 != 0` without modifying an existing directory's -permissions), and allows ext4, xfs and btrfs on Linux and apfs or hfs on macOS -(for the `native` machine, where the SDK and daemon share one macOS kernel). -Every other platform or filesystem is `Unsupported`. +checks, without opening or creating anything, that the Store's parent directory +exists as a real `0700` directory owned by the current effective user (refusing +a missing path, a symlink, wrong owner or `mode & 0o077 != 0` without modifying +directory permissions), and allows ext4, xfs and btrfs on Linux and apfs or hfs +on macOS (for the `native` machine, where the SDK and daemon share one macOS +kernel). Every other platform or filesystem is `Unsupported`. ```text StoreService::bind(workspace, branch) Store::bind_private diff --git a/engine/crates/layerfs-store-service/src/install.rs b/engine/crates/layerfs-store-service/src/install.rs index 2b7692f459..3ea22479c7 100644 --- a/engine/crates/layerfs-store-service/src/install.rs +++ b/engine/crates/layerfs-store-service/src/install.rs @@ -159,15 +159,24 @@ pub fn install_local( 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)?; + let existed = directory.exists(); + let outcome = (|| { + installer_user()?; + if !existed { + use std::os::unix::fs::DirBuilderExt; + fs::DirBuilder::new() + .mode(0o700) + .create(directory) + .map_err(text)?; + } + crate::kernel_guard::check(destination).map_err(|reason| reason.0) + })(); + if let Err(error) = outcome { + if !existed { + let _ = fs::remove_dir_all(directory); + } + return Err(error); } let (target, temporary) = paths(destination).map_err(text)?; let mut file = claim(&target, &temporary).map_err(text)?; diff --git a/engine/crates/layerfs-store-service/src/kernel_guard.rs b/engine/crates/layerfs-store-service/src/kernel_guard.rs index 5a88e26844..f6eabd1c21 100644 --- a/engine/crates/layerfs-store-service/src/kernel_guard.rs +++ b/engine/crates/layerfs-store-service/src/kernel_guard.rs @@ -27,41 +27,21 @@ impl std::fmt::Display for Unsupported { } } -/// Ensures `directory` exists (creating it `0700` when missing) and satisfies -/// the daemon's private-directory requirements: a real directory (not a -/// symlink) owned by the current effective user with no group/other access -/// (`mode & 0o077 == 0`). Existing directories' permissions are never modified. +/// 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"))] -pub fn ensure_private_directory(directory: &Path) -> Result<(), Unsupported> { +fn check_private_directory(directory: &Path) -> Result<(), Unsupported> { use std::fs; - use std::os::unix::fs::{DirBuilderExt, MetadataExt}; + use std::os::unix::fs::MetadataExt; let euid = nix::unistd::geteuid().as_raw(); - let meta = match fs::symlink_metadata(directory) { - Ok(meta) => meta, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => { - fs::DirBuilder::new() - .mode(0o700) - .create(directory) - .map_err(|create_err| { - Unsupported(format!( - "the Store's directory {} cannot be examined: {error} (create 0700 failed: {create_err})", - directory.display() - )) - })?; - fs::symlink_metadata(directory).map_err(|stat_err| { - Unsupported(format!( - "the Store's directory {} cannot be examined: {stat_err}", - directory.display() - )) - })? - } - Err(error) => { - return Err(Unsupported(format!( - "the Store's directory {} cannot be examined: {error}", - directory.display() - ))); - } - }; + 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!( @@ -93,36 +73,17 @@ pub fn ensure_private_directory(directory: &Path) -> Result<(), Unsupported> { Ok(()) } -/// The directory Checked by the guard: `store` directly when it is already an -/// existing directory (or has no file component), otherwise `store.parent()` -/// when `store` names the `store.sqlite` file inside its directory. -#[cfg(any(target_os = "linux", target_os = "macos"))] -fn store_directory(store: &Path) -> &Path { - if store.is_dir() { - return store; - } - let is_store_file = store - .file_name() - .and_then(|n| n.to_str()) - .is_some_and(|name| name.starts_with("store.sqlite") || name.ends_with(".sqlite")); - if is_store_file { - store - .parent() - .filter(|parent| !parent.as_os_str().is_empty()) - .unwrap_or_else(|| Path::new(".")) - } else { - store - } -} - -/// Checks the platform, private-directory ownership/mode (creating `store`'s -/// directory `0700` when missing) and the filesystem holding `store`'s -/// directory, before anything is opened. Returns the filesystem's name. +/// Checks the platform, private-directory ownership/mode and the filesystem +/// holding `store`'s parent directory, before anything is opened or created. +/// Returns the filesystem's name. pub fn check(store: &Path) -> Result<&'static str, Unsupported> { #[cfg(target_os = "linux")] { - let directory = store_directory(store); - ensure_private_directory(directory)?; + let directory = store + .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}", @@ -148,8 +109,11 @@ pub fn check(store: &Path) -> Result<&'static str, Unsupported> { // exactly as on Linux; only a local filesystem may hold the Store. #[cfg(target_os = "macos")] { - let directory = store_directory(store); - ensure_private_directory(directory)?; + let directory = store + .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/kernel_guard.rs b/engine/crates/layerfs-store-service/tests/kernel_guard.rs index 95744c2652..1ce1f31819 100644 --- a/engine/crates/layerfs-store-service/tests/kernel_guard.rs +++ b/engine/crates/layerfs-store-service/tests/kernel_guard.rs @@ -39,15 +39,45 @@ fn scratch(parent: &Path, label: &str) -> PathBuf { #[cfg(any(target_os = "linux", target_os = "macos"))] #[test] -fn missing_store_directory_is_created_0700_and_non_private_directory_is_refused() { - use std::os::unix::fs::PermissionsExt; +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 checked = layerfs_store_service::kernel_guard::check(&missing.join("store.sqlite")); + 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:?}"); - let mode = std::fs::metadata(&missing).unwrap().permissions().mode() & 0o7777; - assert_eq!(mode, 0o700); // 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, 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..e0bcf2e551 100644 --- a/engine/crates/layerfs-store-service/tests/support/installed_store.rs +++ b/engine/crates/layerfs-store-service/tests/support/installed_store.rs @@ -45,6 +45,15 @@ impl Fixture { ) -> Self { let directory = std::env::temp_dir().join(format!("layerfs-installed-{}-{label}", std::process::id())); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt; + std::fs::DirBuilder::new() + .mode(0o700) + .create(&directory) + .unwrap(); + } + #[cfg(not(unix))] std::fs::create_dir(&directory).unwrap(); let source = directory.join("source"); std::fs::create_dir(&source).unwrap(); @@ -116,6 +125,15 @@ 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())); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt; + std::fs::DirBuilder::new() + .mode(0o700) + .create(&directory) + .unwrap(); + } + #[cfg(not(unix))] std::fs::create_dir(&directory).unwrap(); let installed = directory.join("store.sqlite"); std::fs::copy(store, &installed).unwrap(); From 8a85fa4b971bbf35973ee2ce78df333ac511e9ee Mon Sep 17 00:00:00 2001 From: liuyuchen <152110621+jiangkoumo@users.noreply.github.com> Date: Thu, 5 Nov 2026 16:48:14 +0000 Subject: [PATCH 6/6] fix(doctor): hold a Store's folder to kernel_guard's 0700 rule doctor accepted 0701 on store/ and suggested chmod g-rwx,o-rw, but the daemon refuses any group/other bit there. Check the Store folder with 0o077 and suggest chmod 700; other home folders keep 0701. --- api/crates/layerfs-control/src/doctor/scan.rs | 18 +++++++++++- api/crates/layerfs-control/tests/doctor.rs | 29 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/api/crates/layerfs-control/src/doctor/scan.rs b/api/crates/layerfs-control/src/doctor/scan.rs index edb28cd504..d1dbd9a7d6 100644 --- a/api/crates/layerfs-control/src/doctor/scan.rs +++ b/api/crates/layerfs-control/src/doctor/scan.rs @@ -78,7 +78,7 @@ 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); + store_folder(&paths::store_dir(home, name), entity, findings); let path = paths::store_record(home, name); let has_record = path.exists(); if has_record { @@ -210,6 +210,22 @@ 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. +fn store_folder(path: &Path, entity: (&str, &str), findings: &mut Findings) { + 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()), + ); + } +} + /// 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/api/crates/layerfs-control/tests/doctor.rs b/api/crates/layerfs-control/tests/doctor.rs index 29174b704b..479297e67e 100644 --- a/api/crates/layerfs-control/tests/doctor.rs +++ b/api/crates/layerfs-control/tests/doctor.rs @@ -140,6 +140,35 @@ 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"); + home::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_home_that_does_not_exist_is_ok_and_stays_absent() { let home = new_home("absent");