Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
fad03b5
fix(store,doctor): hold a Store's folder to the daemon's 0700 rule (#…
jiangkoumo Nov 12, 2026
a80592c
fix(sandbox): pass the caller's Rust and Git locations to native macO…
jiangkoumo Nov 12, 2026
64149ba
docs(backup,sdk): follow the Store folder's 0700 rule in restore and …
jiangkoumo Nov 12, 2026
eb7a34f
fix(store): claim a Store directory only when this call's mkdir made …
jiangkoumo Nov 12, 2026
16a0ba3
fix(store): drop the install test seam from the shipped API (#570 P1 …
jiangkoumo Nov 12, 2026
e45898a
test(store): cover the mkdir failure that claims no Store directory (…
jiangkoumo Nov 12, 2026
2647fc8
Merge upstream layerfs_v2_0_0 (be317c045) into fix/570-store-perm-env
jiangkoumo Nov 12, 2026
7c54540
fix(store): refuse a restore into a Store folder that is not private …
jiangkoumo Nov 12, 2026
82eb09f
fix(store): check a restore's target folder again before the install …
jiangkoumo Nov 13, 2026
3f039a7
Merge remote-tracking branch 'upstream/layerfs_v2_0_0' into pr663-r1
jiangkoumo Nov 13, 2026
affb55b
fix(store): share the directory claim and report a linked Store folde…
jiangkoumo Nov 13, 2026
7bbe681
fix(doctor): stop descending into a symlinked Store entry (#570 P1 re…
jiangkoumo Nov 13, 2026
f1cdbb4
Merge remote-tracking branch 'upstream/layerfs_v2_0_0' into pr663-r1
jiangkoumo Nov 13, 2026
aef3281
docs(store,doctor): port Store 0700, doctor symlink and macOS exec en…
jiangkoumo Nov 13, 2026
76c8854
Merge layerfs_v2_0_0 (e8fa0a7e00, #678) into fix/570-store-perm-env
yifanxuaaa Nov 14, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions api/crates/layerfs-api/tests/backup_family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,38 @@ fn backups_against_minio() {
);
assert_eq!(states(&home).len(), 2, "objects stayed");

// a Store folder already there at the daemon's own mode is restored into
// as before (#570 P1): its mode stays, and the Store it takes opens
let private = paths::store_dir(&home, "kept");
std::fs::create_dir_all(&private).unwrap();
std::fs::set_permissions(&private, std::fs::Permissions::from_mode(0o700)).unwrap();
let kept = ok(
&home,
"backup.restore",
&[
("store", t("kept")),
("bucket", t("b1")),
("file", t("shop")),
],
);
assert_eq!(kept.get("projects"), Some(&SdkValue::List(vec![t("api")])));
assert!(paths::store_file(&home, "kept").is_file());
assert_eq!(
std::fs::metadata(&private).unwrap().permissions().mode() & 0o7777,
0o700,
"a Store's own folder stays 0700"
);
let read = ok(
&home,
"file.read",
&[
("project", t("kept/api")),
("branch", t("dev")),
("path", t("/NEW")),
],
);
assert_eq!(read.get_text("text"), Some("x"));

for dir in [home, folder, root] {
let _ = std::fs::remove_dir_all(dir);
}
Expand Down
115 changes: 115 additions & 0 deletions api/crates/layerfs-api/tests/backup_refusals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,121 @@ fn restore_over_a_store_with_a_running_sandbox_is_in_use() {
}
}

/// `store/<name>` already there but not the daemon's private directory: the
/// restore refuses it with `store.create`'s own code and remedy before it
/// stages anything, writes nothing and leaves the user's directory as it was
/// (#570 P1). The same folder held `0700`, and an absent one, pass the check
/// and go on into the download.
#[test]
fn restore_refuses_a_store_folder_that_is_not_private() {
let home = tempdir("folder-mode");
home::open(&home).unwrap();
dead_bucket(&home);
let directory = paths::store_dir(&home, "open-dir");
let store = home.join("store");
let restore = || {
call(
&home,
"backup.restore",
&[("store", t("open-dir")), ("bucket", t("dead"))],
)
.unwrap_err()
};
let mode = |dir: &Path| std::fs::metadata(dir).unwrap().permissions().mode() & 0o7777;

std::fs::create_dir_all(&directory).unwrap();
std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o755)).unwrap();
let (code, message) = restore();
assert_eq!(code, "Unsupported", "{message}");
assert!(
message.contains(&directory.display().to_string()),
"{message}"
);
assert!(
message.contains("0755") && message.contains("0700"),
"{message}"
);
assert!(
message.contains(&format!("chmod 700 {}", directory.display())),
"{message}"
);
// nothing was written: no staging folder, no Store file or record, and
// the user's own directory keeps its mode
let names: Vec<_> = std::fs::read_dir(&store)
.unwrap()
.filter_map(|entry| entry.ok())
.map(|entry| entry.file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(names, vec!["open-dir"], "a refused restore stages nothing");
assert_eq!(mode(&directory), 0o755, "the folder's mode is not changed");
assert!(!paths::store_file(&home, "open-dir").exists());
assert!(!paths::store_record(&home, "open-dir").exists());

// the daemon's own private directory passes: the restore goes on into
// the download (`remote::load`), which fails there as it would without
// this check
std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o700)).unwrap();
let failed = restore();
failed_in_the_download(&failed.0, &failed.1);
assert!(!paths::store_file(&home, "open-dir").exists());
assert!(!paths::store_record(&home, "open-dir").exists());

// and an absent folder passes the check the same way; the install that
// would make it `0700` never runs without install permission, and is
// covered by `layerfs-store`'s `restore_install`
std::fs::remove_dir_all(&directory).unwrap();
let failed = restore();
failed_in_the_download(&failed.0, &failed.1);
assert!(!directory.exists(), "no install ran for this call");
// both calls reached the download: each made its staging folder under
// the home lock and removed it when it failed
let staged: Vec<_> = std::fs::read_dir(&store)
.unwrap()
.filter_map(|entry| entry.ok())
.map(|entry| entry.file_name().to_string_lossy().into_owned())
.filter(|name| name.starts_with(".restore-"))
.collect();
assert!(
staged.is_empty(),
"a failed download left its staging folder: {staged:?}"
);
let _ = std::fs::remove_dir_all(home);
}

/// A restore that passed the directory check failed inside the download:
/// `remote::load`'s own first checks run there, and which one stops it says
/// how far it went — the installer user in a Linux process that cannot
/// install (before any request is sent), else the dead endpoint's own
/// request. Neither is this directory guard.
fn failed_in_the_download(code: &str, message: &str) {
assert_eq!(code, "Failed", "{message}");
if installs_as_the_daemons_user() {
assert!(
!message.contains("chmod 700"),
"the download failed, not the directory guard: {message}"
);
} else {
assert!(
message.contains("must run as user 0"),
"the download's own installer-user check: {message}"
);
}
}

/// Whether this process is the user a Store install requires: on Linux only
/// uid 0 is (`layerfs-store-service` `install`), so every other run stops at
/// that check instead of reaching the bucket.
fn installs_as_the_daemons_user() -> bool {
#[cfg(target_os = "linux")]
{
nix::unistd::geteuid().is_root()
}
#[cfg(not(target_os = "linux"))]
{
true
}
}

/// A restore killed mid-download leaves its folder; the next restore removes
/// it, and keeps a folder whose restore still holds its lock and one with
/// no lock file at all.
Expand Down
2 changes: 1 addition & 1 deletion api/crates/layerfs-api/tests/project_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ fn branch_selection_and_pinned_snapshot_under_concurrent_writes() {
fn move_store_file(from: &Path, to: &Path, store: &str) {
let source = paths::store_dir(from, store);
let target = paths::store_dir(to, store);
std::fs::create_dir_all(&target).unwrap();
home::private_dir(&target).unwrap();
for entry in std::fs::read_dir(&source).unwrap() {
let entry = entry.unwrap();
let name = entry.file_name().to_string_lossy().into_owned();
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 @@ -42,7 +42,7 @@ fn init_args(project: &str, folder: &Path) -> SdkValue {
fn move_store_file(from: &Path, to: &Path, store: &str) {
let source = paths::store_dir(from, store);
let target = paths::store_dir(to, store);
std::fs::create_dir_all(&target).unwrap();
home::private_dir(&target).unwrap();
for entry in std::fs::read_dir(&source).unwrap() {
let entry = entry.unwrap();
let name = entry.file_name().to_string_lossy().into_owned();
Expand Down
38 changes: 38 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,46 @@ fn home_with_box() -> (PathBuf, PathBuf) {

#[test]
fn create_list_get_and_status() {
use std::os::unix::fs::PermissionsExt;
let (home, root) = home_with_box();
// a pre-existing store directory with group/other access is refused up
// front without changing the user's directory mode
let bad_dir = paths::store_dir(&home, "open-dir");
home::private_dir(bad_dir.parent().unwrap()).unwrap();
std::fs::create_dir_all(&bad_dir).unwrap();
std::fs::set_permissions(&bad_dir, std::fs::Permissions::from_mode(0o755)).unwrap();
let (code, message) = call(&home, "store.create", &[("name", t("open-dir"))]).unwrap_err();
assert_eq!(code, "Unsupported", "{message}");
assert!(
message.contains(&bad_dir.display().to_string()),
"{message}"
);
assert!(
message.contains("0755") && message.contains("0700"),
"{message}"
);
assert!(
message.contains(&format!("chmod 700 {}", bad_dir.display())),
"{message}"
);
assert!(bad_dir.exists(), "user's existing directory is not deleted");
assert_eq!(
std::fs::metadata(&bad_dir).unwrap().permissions().mode() & 0o7777,
0o755,
"user's existing directory permissions are not modified"
);
std::fs::remove_dir_all(&bad_dir).unwrap();

let made = call(&home, "store.create", &[("name", t("shop"))]).unwrap();
assert_eq!(
std::fs::metadata(paths::store_dir(&home, "shop"))
.unwrap()
.permissions()
.mode()
& 0o7777,
0o700,
"a Store's own folder is 0700"
);
assert_eq!(made.get_text("machine"), Some("native"));
assert!(
made.get("cursor").is_none(),
Expand Down
20 changes: 5 additions & 15 deletions control/crates/layerfs-bucket/src/backup/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ use crate::bucket::{progress, Driver};
use layerfs_persistence::{PersistenceConfig, SqlitePersistenceProfile};
use layerfs_storage::ReservationBlocks;
use layerfs_store_service::bootstrap::open_store_observed;
use layerfs_store_service::install::{installer_user, paths};
use layerfs_store_service::kernel_guard;
use layerfs_store_service::install::{paths, prepare};
use std::fs::{self, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -187,19 +186,10 @@ fn fetch(
let result = (|| {
let text = |error: io::Error| error.to_string();
let object = store_object(name)?;
let directory = destination
.parent()
.ok_or("the Store destination has no directory")?;
// as install_local: the volume holding the Store's directory
kernel_guard::check(directory).map_err(|reason| reason.0)?;
installer_user()?;
if !directory.exists() {
use std::os::unix::fs::DirBuilderExt;
fs::DirBuilder::new()
.mode(0o700)
.create(directory)
.map_err(text)?;
}
// as install_local: the Store's own directory (`0700` when absent),
// the Store file guarded; a refusal reclaims only an empty directory
// this call made
prepare(destination)?;
let (target, _) = paths(destination).map_err(text)?;
let cloning = binding.is_none();
if cloning && exists(&target).map_err(text)? {
Expand Down
Loading