Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent-tools): refuse writes beside an attached folder on Linux instead of discarding them (#9680)

Akarshan Biswasb9c1f0dc29c9
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/jail.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/jail.rs
index b0a547aab..72f295880 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/jail.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/jail.rs
@@ -25,7 +25,7 @@
//! Windows has no argv to wrap -- see [`super::appcontainer`], which re-execs this
//! binary because the confinement is a `CreateProcessW` token attribute.
-use std::path::{Path, PathBuf};
+use std::path::{Component, Path, PathBuf};
use std::sync::OnceLock;
use crate::workspace::{CargoConfig, PkgCache};
@@ -569,6 +569,64 @@ fn push(args: &mut Vec<String>, parts: &[&str]) {
args.extend(parts.iter().map(|s| s.to_string()));
}
+/// The folders bubblewrap binds onto their own paths for this policy.
+fn bound_roots(policy: &Policy) -> impl Iterator<Item = &Path> {
+ std::iter::once(policy.workspace.as_path())
+ .chain(policy.read_roots.iter().map(PathBuf::as_path))
+ .chain(policy.write_roots.iter().map(PathBuf::as_path))
+}
+
+/// Top ancestors of bound folders under a writable sandbox tmpfs, remounted read-only so
+/// `../x` is refused rather than discarded (#9678). Skips ones covering writable or masked dirs.
+fn escape_anchors(policy: &Policy, home: Option<&Path>) -> Vec<PathBuf> {
+ let bases: Vec<&Path> = std::iter::once(Path::new("/tmp"))
+ .chain(home.filter(|_| !policy.home_readonly))
+ .collect();
+ let kept: Vec<PathBuf> = bases
+ .iter()
+ .map(|base| base.to_path_buf())
+ .chain(policy.mask_root.clone())
+ .chain(policy.hide_root.clone())
+ .chain(policy.cache_mask())
+ .chain(policy.own_path_dirs().map(Path::to_path_buf))
+ .collect();
+ let mut anchors: Vec<PathBuf> = bound_roots(policy)
+ .flat_map(|root| {
+ bases.iter().filter_map(move |base| {
+ let first = root.strip_prefix(base).ok()?.components().next()?;
+ let anchor = match first {
+ Component::Normal(name) => base.join(name),
+ _ => return None,
+ };
+ (anchor != root).then_some(anchor)
+ })
+ })
+ .filter(|anchor| !kept.iter().any(|k| k.starts_with(anchor)))
+ .filter(|anchor| !bound_roots(policy).any(|root| anchor.starts_with(root)))
+ .collect();
+ anchors.sort();
+ anchors.dedup();
+ anchors
+}
+
+/// Masks remounted read-only like [`escape_anchors`]; one under or replaced by a bind is
+/// no longer the top mount at its path, so it is skipped.
+fn read_only_masks(policy: &Policy) -> Vec<PathBuf> {
+ policy
+ .mask_root
+ .iter()
+ .chain(&policy.hide_root)
+ .cloned()
+ .chain(policy.cache_mask())
+ .filter(|mask| !policy.hide_under_a_bind(mask))
+ .filter(|mask| {
+ !bound_roots(policy)
+ .chain(policy.own_path_dirs())
+ .any(|dir| dir == mask.as_path())
+ })
+ .collect()
+}
+
/// Build bubblewrap's argv. Operations apply in order, which the layering below
/// depends on: the read-only root comes first, then the tmpfs that hides `$HOME`,
/// then the workspace bind that punches back through it.
@@ -625,6 +683,12 @@ pub fn bwrap_args(policy: &Policy, cfg: &ShellConfig) -> Vec<String> {
if let Some(cache) = policy.cache_mask() {
push(&mut args, &["--tmpfs", &cache.to_string_lossy()]);
}
+ // Before the binds, which create their mount points inside these; made
+ // read-only after them.
+ let anchors = escape_anchors(policy, home_dir().as_deref());
+ for anchor in &anchors {
+ push(&mut args, &["--tmpfs", &anchor.to_string_lossy()]);
+ }
// Between the masks above and the workspace below, and that position is the
// enforcement: after the `$HOME`/data-folder tmpfs so a project living under
@@ -701,6 +765,11 @@ pub fn bwrap_args(policy: &Policy, cfg: &ShellConfig) -> Vec<String> {
if let Some(hide) = policy.hide_root.as_deref().filter(|h| policy.hide_under_a_bind(h)) {
push(&mut args, &["--tmpfs", &hide.to_string_lossy()]);
}
+ // Last, after every bind: a remount covers only its own mount, so the
+ // binds beneath stay writable while a write beside them is refused.
+ for dir in anchors.iter().chain(&read_only_masks(policy)) {
+ push(&mut args, &["--remount-ro", &dir.to_string_lossy()]);
+ }
push(&mut args, &["--chdir", &ws]);
// Drops the network, pid, ipc, uts and cgroup namespaces along with the
@@ -1763,6 +1832,123 @@ mod tests {
assert!(home_tmpfs < rw && rw < ws_bind, "{text}");
}
+ fn paths(items: &[&str]) -> Vec<PathBuf> {
+ items.iter().map(PathBuf::from).collect()
+ }
+
+ #[test]
+ fn escape_anchors_are_the_top_ancestor_under_tmp_and_home() {
+ let home = Path::new("/home/u");
+ let p = Policy::new(Path::new("/srv/ws"), false)
+ .with_write_roots(paths(&["/home/u/Projects/app", "/tmp/qa/folders/f"]));
+ assert_eq!(
+ escape_anchors(&p, Some(home)),
+ paths(&["/home/u/Projects", "/tmp/qa"])
+ );
+ }
+
+ #[test]
+ fn escape_anchors_skip_what_has_no_ancestor_to_protect() {
+ let home = Path::new("/home/u");
+ // Directly in the tmpfs: its parent is the tmpfs itself (the known gap).
+ let direct = Policy::new(Path::new("/srv/ws"), false)
+ .with_write_roots(paths(&["/home/u/app", "/tmp/f"]));
+ assert!(escape_anchors(&direct, Some(home)).is_empty());
+ // A read-only `$HOME` is the host's own directories, already refusing.
+ let ro = Policy::new(Path::new("/srv/ws"), false)
+ .with_home_readonly(true)
+ .with_write_roots(paths(&["/home/u/Projects/app"]));
+ assert!(escape_anchors(&ro, Some(home)).is_empty());
+ // Outside both tmpfs mounts the read-only root already refuses.
+ let outside = Policy::new(Path::new("/srv/a/ws"), false);
+ assert!(escape_anchors(&outside, Some(home)).is_empty());
+ }
+
+ /// An anchor never covers what must stay writable or is masked on its own,
+ /// and never sits on a bound root, where the remount would hit the bind.
+ #[test]
+ fn escape_anchors_never_cover_writable_or_bound_paths() {
+ let home = Path::new("/home/u");
+ let ws = Path::new("/home/u/.local/share/Jan/data/ws/t1");
+ let desktop =
+ Policy::new(ws, false).with_mask_root(Path::new("/home/u/.local/share/Jan/data"));
+ assert!(
+ escape_anchors(&desktop, Some(home)).is_empty(),
+ "the mask covers the workspace"
+ );
+ let scratch = Policy::new(Path::new("/home/u/.cache/proj"), false)
+ .with_scratch_root(Path::new("/home/u/.cache/jan/s1"));
+ assert!(
+ escape_anchors(&scratch, Some(home)).is_empty(),
+ "~/.cache holds the scratch"
+ );
+ let nested = Policy::new(Path::new("/srv/ws"), false)
+ .with_write_roots(paths(&["/home/u/Projects", "/home/u/Projects/app"]));
+ assert!(
+ escape_anchors(&nested, Some(home)).is_empty(),
+ "~/Projects is itself bound"
+ );
+ // A `$HOME` inside `/tmp` must not be buried under a `/tmp` anchor.
+ let tmp_home = Policy::new(Path::new("/srv/ws"), false)
+ .with_write_roots(paths(&["/tmp/qa/folders/f"]));
+ assert!(escape_anchors(&tmp_home, Some(Path::new("/tmp/qa/home"))).is_empty());
+ }
+
+ /// The anchor is mounted before the root it protects and remounted
+ /// read-only after every bind, so the binds stay writable beneath it.
+ #[test]
+ fn bwrap_mounts_an_anchor_before_the_root_and_remounts_it_read_only_after() {
+ let home = home_dir().expect("a home dir");
+ let repo = home.join("jan-anchor-test/app");
+ let anchor = home.join("jan-anchor-test");
+ let (repo, anchor) = (repo.to_string_lossy(), anchor.to_string_lossy());
+ let text = joined(&bwrap_args(
+ &policy().with_write_roots(vec![PathBuf::from(&*repo)]),
+ &cfg(),
+ ));
+ let tmpfs = text
+ .find(&format!("--tmpfs {anchor} "))
+ .expect("anchor tmpfs");
+ let bind = text
+ .find(&format!("--bind {repo} {repo}"))
+ .expect("root bind");
+ let ws_bind = text
+ .find("--bind /data/agent-workspace/threads/t1")
+ .expect("ws bind");
+ let remount = text
+ .find(&format!("--remount-ro {anchor} "))
+ .expect("anchor remount");
+ let chdir = text.find("--chdir").expect("chdir");
+ assert!(
+ tmpfs < bind && bind < ws_bind && ws_bind < remount && remount < chdir,
+ "{text}"
+ );
+ }
+
+ /// Writes into the masks are refused rather than discarded; a mask inside a
+ /// bind is no longer the top mount there and is left alone.
+ #[test]
+ fn bwrap_remounts_the_masks_read_only_unless_a_bind_contains_them() {
+ let mask = "/data";
+ let hide = "/srv/u/.jan";
+ let p = policy()
+ .with_mask_root(Path::new(mask))
+ .with_hide_root(Path::new(hide));
+ let text = joined(&bwrap_args(&p, &cfg()));
+ let ws_bind = text
+ .find("--bind /data/agent-workspace/threads/t1")
+ .expect("ws bind");
+ let remount = text
+ .find(&format!("--remount-ro {mask} "))
+ .expect("mask remount");
+ assert!(ws_bind < remount, "{text}");
+ assert!(text.contains(&format!("--remount-ro {hide} ")), "{text}");
+ let around =
+ Policy::new(Path::new("/srv/u"), false).with_mask_root(Path::new("/srv/u/data"));
+ let text = joined(&bwrap_args(&around, &cfg()));
+ assert!(!text.contains("--remount-ro /srv/u/data"), "{text}");
+ }
+
#[test]
fn seatbelt_allows_a_write_root_to_write() {
let repo = "/Users/u/repo";
@@ -2884,4 +3070,53 @@ mod enforcement_tests {
"a relocated store root must be unreadable, got: {out}"
);
}
+
+ /// #9678: a write beside an attached folder is refused, not discarded, while the
+ /// folder, a bare `/tmp` path and `$HOME` (where caches go) stay writable.
+ #[tokio::test]
+ #[cfg(target_os = "linux")]
+ async fn a_write_beside_an_attached_folder_is_refused_not_discarded() {
+ require_backend!();
+ let ws = workspace();
+ let base = workspace();
+ let folder = base.join("folders/f");
+ std::fs::create_dir_all(&folder).unwrap();
+ let policy = Policy::new(&ws, false).with_write_roots(vec![folder.clone()]);
+ let in_folder = format!("cd {} && echo in > in.txt", folder.to_string_lossy());
+ let (ok, out) = run_policy(policy.clone(), &ws, &in_folder).await;
+ assert!(ok, "the folder stays writable: {out}");
+ assert_eq!(
+ std::fs::read_to_string(folder.join("in.txt"))
+ .unwrap_or_default()
+ .trim(),
+ "in"
+ );
+ for target in ["../x", "../../x"] {
+ let cmd = format!("cd {} && echo x > {target}", folder.to_string_lossy());
+ let (ok, out) = run_policy(policy.clone(), &ws, &cmd).await;
+ assert!(!ok, "{target} must be refused, not discarded: {out}");
+ assert!(looks_denied(&out), "{out}");
+ }
+ let (ok, out) = run_policy(policy, &ws, "echo t > /tmp/t && mkdir -p \"$HOME/.npm\"").await;
+ let _ = std::fs::remove_dir_all(&ws);
+ let _ = std::fs::remove_dir_all(&base);
+ assert!(ok, "/tmp and $HOME stay writable for tools: {out}");
+ }
+
+ /// The same for the thread workspace, which sits inside the masked data
+ /// folder on the desktop: `../x` would land in the mask's tmpfs.
+ #[tokio::test]
+ #[cfg(target_os = "linux")]
+ async fn a_write_beside_the_workspace_in_a_masked_store_is_refused() {
+ require_backend!();
+ let store = workspace();
+ let ws = store.join("threads/t1");
+ std::fs::create_dir_all(&ws).unwrap();
+ let policy = Policy::new(&ws, false).with_mask_root(&store);
+ let (ok, out) = run_policy(policy.clone(), &ws, "echo in > in.txt").await;
+ assert!(ok, "{out}");
+ let (ok, out) = run_policy(policy, &ws, "echo x > ../x").await;
+ let _ = std::fs::remove_dir_all(&store);
+ assert!(!ok && looks_denied(&out), "../x must be refused: {out}");
+ }
}