Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -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/<version>/<os>-<arch>/layerfs-daemon` (`Unavailable` when missing) |
| `native` on macOS | host processes, Workspaces over loopback NFS. The sqlite Store's directory (`<home>/store/<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/<version>/<os>-<arch>/layerfs-daemon` (`Unavailable` when missing). Commands get `HOME=<sandbox>/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) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ tests, `f67d5a9f6f`, `771823d728`, `94671c974c`, `37f1bbd2b9`):

**Limits**

- `HOME` is the Sandbox's scratch directory (`/tmp` in a container, `<sandbox>/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
Expand Down
30 changes: 30 additions & 0 deletions api/crates/layerfs-api/tests/project_family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
2 changes: 1 addition & 1 deletion api/crates/layerfs-api/tests/project_init_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
36 changes: 36 additions & 0 deletions api/crates/layerfs-api/tests/store_family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions api/crates/layerfs-api/tests/store_open_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
18 changes: 17 additions & 1 deletion api/crates/layerfs-control/src/doctor/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(super) fn scan(home: &Path, findings: &mut Findings) -> Result<Scanned> {
/// 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 {
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions api/crates/layerfs-control/src/store/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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);
}
};
Expand Down
5 changes: 3 additions & 2 deletions api/crates/layerfs-control/src/store/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ pub(crate) fn make_empty(
.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)
.map_err(|error| refused("Exists", format!("{}: {error}", target.display())))?;
Expand Down Expand Up @@ -374,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")?;
Expand Down
5 changes: 4 additions & 1 deletion api/crates/layerfs-control/src/store/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
{
Expand Down
1 change: 1 addition & 0 deletions api/crates/layerfs-control/src/store/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn open(store: &Path, args: &SdkValue) -> Result<Opened, SdkRefusal> {
.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),
})
}
Expand Down
29 changes: 29 additions & 0 deletions api/crates/layerfs-control/tests/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
53 changes: 48 additions & 5 deletions connect/crates/layerfs-sandbox/src/backend/host_process/exec.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! One ordinary command to its end in a local Sandbox: the command identity,
//! the Workspace directory and a search path (on macOS this process's own);
//! nothing else of this process's environment, no shell of its own and no
//! implicit timeout. Each command leads a process group of its own, named in
//! the Sandbox until it is empty, so what it leaves running ends with the
//! Sandbox.
//! the Workspace directory, a search path (on macOS this process's own), and
//! (on macOS) the caller's Rust and Git locations (`RUSTUP_HOME`,
//! `CARGO_HOME`, `GIT_CONFIG_GLOBAL`) when present; nothing else of this
//! process's environment, no shell of its own and no implicit timeout. Each
//! command leads a process group of its own, named in the Sandbox until it is
//! empty, so what it leaves running ends with the Sandbox.
use super::sandbox::Sandbox;
use crate::local::streams;
use crate::local::{groups, 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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -123,3 +126,43 @@ fn check(request: &ExecRequest) -> Result<(), RuntimeError> {
}
Ok(())
}

/// The caller's Rust and Git locations on macOS, when present, so a command
/// whose `HOME` is the Sandbox's scratch directory still sees them unless the
/// caller's environment or the request sets its own.
#[cfg(target_os = "macos")]
fn user_environment() -> Vec<(&'static str, OsString)> {
use std::path::PathBuf;
let home = non_empty_env("HOME").map(PathBuf::from);
let existing = |path: PathBuf| path.exists().then(|| path.into_os_string());
let mut vars = Vec::new();
for (name, subdir) in [("RUSTUP_HOME", ".rustup"), ("CARGO_HOME", ".cargo")] {
let value =
non_empty_env(name).or_else(|| home.as_ref().and_then(|h| existing(h.join(subdir))));
if let Some(value) = value {
vars.push((name, value));
}
}
let git_config = non_empty_env("GIT_CONFIG_GLOBAL")
.or_else(|| home.as_ref().and_then(|h| existing(h.join(".gitconfig"))))
.or_else(|| {
let xdg = non_empty_env("XDG_CONFIG_HOME")
.map(PathBuf::from)
.or_else(|| home.as_ref().map(|h| h.join(".config")))?;
existing(xdg.join("git").join("config"))
});
if let Some(value) = git_config {
vars.push(("GIT_CONFIG_GLOBAL", value));
}
vars
}

#[cfg(not(target_os = "macos"))]
fn user_environment() -> Vec<(&'static str, OsString)> {
Vec::new()
}

#[cfg(target_os = "macos")]
fn non_empty_env(name: &str) -> Option<OsString> {
std::env::var_os(name).filter(|value| !value.is_empty())
}
Loading
Loading