Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 5 additions & 3 deletions architecture/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
159 changes: 143 additions & 16 deletions crates/openshell-supervisor-process/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uid>, gid: Option<Gid>) -> Result<()> {
use nix::unistd::chown;

let meta = std::fs::symlink_metadata(root).into_diagnostic()?;
if meta.file_type().is_symlink() {
return Err(miette::miette!(
Expand All @@ -1180,23 +1184,63 @@ fn chown_sandbox_home(root: &Path, uid: Option<Uid>, gid: Option<Gid>) -> 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<Uid>,
gid: Option<Gid>,
do_chown: &impl Fn(&Path, Option<Uid>, Option<Gid>) -> 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<Uid>,
gid: Option<Gid>,
do_chown: &impl Fn(&Path, Option<Uid>, Option<Gid>) -> 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(())
Expand Down Expand Up @@ -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<Mutex<Vec<PathBuf>>> = 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<Uid>, _gid: Option<Gid>| -> 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<Uid>, _gid: Option<Gid>| -> 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<Uid>,
_gid: Option<Gid>|
-> 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() {
Expand Down
5 changes: 2 additions & 3 deletions tasks/python.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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

Expand Down
Loading