From 8a14b3a47709f5216303d6fd2b7f086d9a1f4ba3 Mon Sep 17 00:00:00 2001 From: Varsha Date: Wed, 22 Jul 2026 14:37:07 -0700 Subject: [PATCH 1/2] fix(sandbox): skip read-only mounts during recursive chown of /sandbox (#2341) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(sandbox): skip read-only mounts during recursive chown of /sandbox The sandbox supervisor crashed with EROFS when the recursive chown of /sandbox encountered read-only submounts. This is common in gVisor-based Kubernetes deployments where read-only volume mounts are the only way to enforce per-directory immutability (Landlock is unavailable under gVisor). The fix adds two guards to the ownership walk: 1. Mount-boundary detection via st_dev comparison — paths on a different filesystem than /sandbox are skipped entirely, avoiding the chown call on nested read-only mounts. 2. EROFS tolerance — if chown still returns EROFS (e.g. the root mount itself is read-only), the error is logged at debug level and startup continues. Symlink skipping (already present) is preserved and extracted into the recursive walker for consistency. Manually verified on a kind cluster with a read-only PVC mounted at /sandbox/readonly-data: the pod starts successfully, writable paths are owned by the sandbox user, and the read-only mount retains root ownership. Kubernetes e2e test coverage is a separate follow-up. Closes #2294 Signed-off-by: Varsha Prasad Narsing * refactor(sandbox): consolidate symlink check into chown_recursive Move the per-child symlink guard from the directory iteration loop into the top of chown_recursive, reusing the symlink_metadata call that is already performed there. This centralizes all three skip guards (symlink, mount boundary, EROFS) in one place and eliminates a redundant symlink_metadata call per child entry. Suggested-by: elezar Signed-off-by: Varsha Prasad Narsing * fix(sandbox): recurse into children after EROFS and fix doc comment When chown returns EROFS on a directory, the code previously returned early without recursing into children. This skipped writable submounts nested under a read-only directory on the same device (e.g., a writable emptyDir inside a read-only ConfigMap mount sharing the same st_dev). Now the EROFS case falls through to the directory recursion so that writable children are still chowned. Also fixes the doc comment which said EROFS errors are "logged as warnings" when they are actually logged at debug level. Signed-off-by: Varsha Prasad Narsing * fix(sandbox): allow similar_names clippy lint in chown test Signed-off-by: Varsha Prasad Narsing * fix(sandbox): remove cross-device skip and inject chown for EROFS tests Remove the unconditional st_dev boundary check that prevented chown on writable PVC mounts with a different device ID. Rely solely on EROFS errors to skip read-only paths while continuing traversal into children and siblings. Inject the chown operation into chown_recursive to enable unit testing the EROFS recovery path without requiring root or mount privileges. Signed-off-by: Varsha Prasad Narsing Signed-off-by: Varsha Prasad Narsing * style(sandbox): apply rustfmt to chown test closures Signed-off-by: Varsha Prasad Narsing Signed-off-by: Varsha Prasad Narsing * fix(sandbox): stop descending into EROFS subtrees to prevent TOCTOU race After chown returns EROFS, return immediately instead of recursing into the read-only directory's children. Descending into a mount we cannot own creates a TOCTOU window where an attacker could swap a child for a symlink between symlink_metadata and chown (CWE-367/CWE-59). Siblings of the EROFS path are still visited by the caller's loop. Signed-off-by: Varsha Prasad Narsing Signed-off-by: Varsha Prasad Narsing * fix(sandbox): hard-error on root EROFS, log read_dir failures, eliminate double stat Chown root directly in chown_sandbox_home so EROFS on /sandbox itself is a hard startup error rather than being silently swallowed. Extract chown_children to log read_dir failures instead of silently skipping them. This also eliminates the redundant symlink_metadata syscall on the root path. Signed-off-by: Varsha Prasad Narsing Signed-off-by: Varsha Prasad Narsing --------- Signed-off-by: Varsha Prasad Narsing Signed-off-by: Varsha Prasad Narsing --- .../src/process.rs | 159 ++++++++++++++++-- 1 file changed, 143 insertions(+), 16 deletions(-) diff --git a/crates/openshell-supervisor-process/src/process.rs b/crates/openshell-supervisor-process/src/process.rs index 72effa98f8..322dcadd86 100644 --- a/crates/openshell-supervisor-process/src/process.rs +++ b/crates/openshell-supervisor-process/src/process.rs @@ -1168,10 +1168,14 @@ fn rewrite_group_at(path: &Path, gid: &str) -> Result<()> { /// Symlinks are skipped (not followed) to prevent privilege escalation via /// malicious container images. The TOCTOU window is not exploitable because /// no untrusted process is running yet. +/// +/// The root path is chowned unconditionally — EROFS there is a hard error +/// (a read-only `/sandbox` is a misconfiguration). For children, `EROFS` +/// causes the walker to skip that path and its entire subtree — descending +/// into a read-only mount we do not control would be a TOCTOU risk +/// (CWE-367/CWE-59). Siblings of the read-only path are still visited. #[cfg(unix)] fn chown_sandbox_home(root: &Path, uid: Option, gid: Option) -> Result<()> { - use nix::unistd::chown; - let meta = std::fs::symlink_metadata(root).into_diagnostic()?; if meta.file_type().is_symlink() { return Err(miette::miette!( @@ -1180,23 +1184,63 @@ fn chown_sandbox_home(root: &Path, uid: Option, gid: Option) -> Result )); } - chown(root, uid, gid).into_diagnostic()?; + nix::unistd::chown(root, uid, gid).into_diagnostic()?; - if meta.is_dir() - && let Ok(entries) = std::fs::read_dir(root) - { - for entry in entries { - let entry = entry.into_diagnostic()?; - let path = entry.path(); - if path - .symlink_metadata() - .is_ok_and(|m| m.file_type().is_symlink()) - { - debug!(path = %path.display(), "Skipping symlink during sandbox home chown"); - continue; + if meta.is_dir() { + chown_children(root, uid, gid, &nix::unistd::chown)?; + } + + Ok(()) +} + +/// Walk directory children and chown each entry, skipping symlinks and +/// EROFS subtrees. Called after the parent has already been chowned. +#[cfg(unix)] +fn chown_children( + dir: &Path, + uid: Option, + gid: Option, + do_chown: &impl Fn(&Path, Option, Option) -> nix::Result<()>, +) -> Result<()> { + match std::fs::read_dir(dir) { + Ok(entries) => { + for entry in entries { + let entry = entry.into_diagnostic()?; + let child = entry.path(); + chown_recursive(&child, uid, gid, do_chown)?; } - chown_sandbox_home(&path, uid, gid)?; } + Err(e) => { + debug!(path = %dir.display(), error = %e, "Cannot list directory during sandbox home chown"); + } + } + Ok(()) +} + +#[cfg(unix)] +fn chown_recursive( + path: &Path, + uid: Option, + gid: Option, + do_chown: &impl Fn(&Path, Option, Option) -> nix::Result<()>, +) -> Result<()> { + let meta = std::fs::symlink_metadata(path).into_diagnostic()?; + + if meta.file_type().is_symlink() { + debug!(path = %path.display(), "Skipping symlink during sandbox home chown"); + return Ok(()); + } + + if let Err(e) = do_chown(path, uid, gid) { + if e == nix::errno::Errno::EROFS { + debug!(path = %path.display(), "Skipping read-only path during sandbox home chown"); + return Ok(()); + } + return Err(e).into_diagnostic(); + } + + if meta.is_dir() { + chown_children(path, uid, gid, do_chown)?; } Ok(()) @@ -2115,6 +2159,89 @@ mod tests { .expect("should skip symlink children without error"); } + #[cfg(unix)] + #[test] + fn chown_recursive_skips_erofs_subtree_but_continues_siblings() { + use std::sync::{Arc, Mutex}; + + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().join("sandbox"); + std::fs::create_dir(&root).unwrap(); + + let readonly_dir = root.join("ro-mount"); + std::fs::create_dir(&readonly_dir).unwrap(); + std::fs::write(readonly_dir.join("child-under-ro.txt"), "data").unwrap(); + + std::fs::write(root.join("writable-sibling.txt"), "data").unwrap(); + + let uid = Some(nix::unistd::geteuid()); + let gid = Some(nix::unistd::getegid()); + + let chowned: Arc>> = Arc::new(Mutex::new(Vec::new())); + let chowned_ref = Arc::clone(&chowned); + + let readonly_dir_clone = readonly_dir.clone(); + let fake_chown = + move |path: &Path, _uid: Option, _gid: Option| -> nix::Result<()> { + if path == readonly_dir_clone { + return Err(nix::errno::Errno::EROFS); + } + chowned_ref.lock().unwrap().push(path.to_path_buf()); + Ok(()) + }; + + chown_children(&root, uid, gid, &fake_chown).expect("EROFS should be handled gracefully"); + + let chowned = chowned.lock().unwrap(); + assert!( + !chowned.contains(&readonly_dir.join("child-under-ro.txt")), + "children under EROFS directory must NOT be descended into" + ); + assert!( + chowned.contains(&root.join("writable-sibling.txt")), + "writable sibling should still be chowned" + ); + } + + #[cfg(unix)] + #[test] + fn chown_recursive_propagates_non_erofs_errors() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().join("sandbox"); + std::fs::create_dir(&root).unwrap(); + + let uid = Some(nix::unistd::geteuid()); + let gid = Some(nix::unistd::getegid()); + + let fake_chown = |_path: &Path, _uid: Option, _gid: Option| -> nix::Result<()> { + Err(nix::errno::Errno::EPERM) + }; + + let result = chown_recursive(&root, uid, gid, &fake_chown); + assert!(result.is_err(), "non-EROFS errors should propagate"); + } + + #[cfg(unix)] + #[test] + fn chown_children_skips_all_erofs_children_gracefully() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().join("sandbox"); + std::fs::create_dir(&root).unwrap(); + std::fs::create_dir(root.join("a")).unwrap(); + std::fs::write(root.join("b.txt"), "data").unwrap(); + + let uid = Some(nix::unistd::geteuid()); + let gid = Some(nix::unistd::getegid()); + + let always_erofs = |_path: &Path, + _uid: Option, + _gid: Option| + -> nix::Result<()> { Err(nix::errno::Errno::EROFS) }; + + chown_children(&root, uid, gid, &always_erofs) + .expect("EROFS on all children should be skipped gracefully"); + } + #[cfg(unix)] #[test] fn rewrite_passwd_modifies_existing_sandbox_entry() { From 541b97f0b157757862d44ba9a1aaf4e4e6039a22 Mon Sep 17 00:00:00 2001 From: Matthew Grossman Date: Wed, 22 Jul 2026 16:08:23 -0700 Subject: [PATCH 2/2] fix(mise): initialize Python dependencies in fresh worktrees (#2429) * fix(mise): Properly initialize venv for fresh worktrees Signed-off-by: Matthew Grossman * fix(mise): use uv sync for protobuf generation Signed-off-by: Matthew Grossman * docs: remove redundant worktree setup note Signed-off-by: Matthew Grossman --------- Signed-off-by: Matthew Grossman --- architecture/build.md | 8 +++++--- tasks/python.toml | 5 ++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/architecture/build.md b/architecture/build.md index 70b6bf5dc6..eb8672f15a 100644 --- a/architecture/build.md +++ b/architecture/build.md @@ -138,9 +138,11 @@ do not infer from kube context. ## Python Wheel Packaging The generated protobuf/gRPC stubs under `python/openshell/_proto/` are gitignored -build outputs of `mise run python:proto`. maturin honors `.gitignore` when -collecting `python-source` files, so native builds (Linux CI, local -`pip install .`) would drop them and ship an unimportable wheel. `pyproject.toml` +build outputs of `mise run python:proto`. The task uses `uv run --frozen` to +synchronize the current worktree's `.venv` from `uv.lock` before generation. +maturin honors `.gitignore` when collecting `python-source` files, so native +builds (Linux CI, local `pip install .`) would drop them and ship an unimportable +wheel. `pyproject.toml` pins them back in with `[tool.maturin].include` globs. The release workflows install each Linux wheel in a clean image and import `openshell.sandbox` as a smoke check. diff --git a/tasks/python.toml b/tasks/python.toml index 3aa9727ebf..e04bf27bb4 100644 --- a/tasks/python.toml +++ b/tasks/python.toml @@ -218,11 +218,10 @@ hide = true ["python:proto"] description = "Generate Python protobuf stubs from .proto files" -env = { UV_NO_SYNC = "1" } run = """ #!/usr/bin/env bash set -euo pipefail -uv run python -m grpc_tools.protoc \ +uv run --frozen python -m grpc_tools.protoc \ -Iproto \ --python_out=python/openshell/_proto \ --pyi_out=python/openshell/_proto \ @@ -233,7 +232,7 @@ uv run python -m grpc_tools.protoc \ proto/options.proto \ proto/sandbox.proto # Fix absolute imports in generated stubs to use package-relative imports -uv run python - <<'PY' +uv run --frozen python - <<'PY' from pathlib import Path import re