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): raise the bash sandbox NOFILE and FSIZE limits (#8978)

Akarshan Biswas0939e0c488f7
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs
index aa6a36cbb..589ecb743 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs
@@ -200,33 +200,63 @@ const SANDBOX_ENV_ALLOW: &[&str] = &[
/// All are pointed at the scratch together so no tool falls back to the host.
const TEMP_ENV_KEYS: &[&str] = &["TMPDIR", "TMP", "TEMP"];
+/// Soft limits the sandboxed child runs under, each capped by the host's hard
+/// limit. `NOFILE` and `FSIZE` are deliberately generous: toolchains (linkers,
+/// node, cargo) routinely want tens of thousands of descriptors, and a debug
+/// `cargo test` binary or incremental artifact can pass a gigabyte on its own.
+/// Caps that low break ordinary work long before they stop abuse. `FSIZE` is a
+/// per-file cap enforced with `SIGXFSZ`, so exceeding it kills the writer rather
+/// than returning an error most tools report clearly.
+#[cfg(unix)]
+const CHILD_LIMITS: &[(RlimitResource, u64)] = &[
+ (nix::libc::RLIMIT_NPROC, 4096),
+ (nix::libc::RLIMIT_NOFILE, 65536),
+ (nix::libc::RLIMIT_FSIZE, 16 * 1024 * 1024 * 1024),
+];
+
+/// libc types the `RLIMIT_*` ids per platform: `__rlimit_resource_t` (`u32`) on
+/// linux-gnu, plain `c_int` everywhere else Unix (macOS, musl). Naming the type
+/// once keeps [`CHILD_LIMITS`] and the `getrlimit`/`setrlimit` calls cast-free.
+#[cfg(all(target_os = "linux", target_env = "gnu"))]
+type RlimitResource = nix::libc::__rlimit_resource_t;
+#[cfg(all(unix, not(all(target_os = "linux", target_env = "gnu"))))]
+type RlimitResource = nix::libc::c_int;
+
/// Bound the resource exhaustion a sandboxed command could otherwise trigger on
/// the host. `bwrap` 0.6.1 (and older) has no `--rlimit`, so instead we clamp the
/// child's soft limits here, before exec, from the one choke point every backend
/// funnels through. A fork-bomb is capped by `NPROC`, descriptor exhaustion by
/// `NOFILE`, and disk fill through the unbounded workspace bind by `FSIZE`. The
-/// bwrap wrapper execs `bwrap` itself, which sets up the namespace and then
-/// execs the real shell, so the limits carry over to every descendant. Linux
-/// only; the Windows AppContainer child is limited by its token.
+/// hard limit is left at the host's value so a command that genuinely needs more
+/// can raise its own soft limit back up. The bwrap wrapper execs `bwrap` itself,
+/// which sets up the namespace and then execs the real shell, so the limits carry
+/// over to every descendant. Linux only; the Windows AppContainer child is
+/// limited by its token.
#[cfg(unix)]
fn confine_limits(cmd: &mut Command) {
// `tokio::process::Command::pre_exec` (unix) is the std `pre_exec`; the call
// below is what mounts the limits.
// # Safety: `pre_exec` runs in the forked child before exec. Only async-signal-
- // safe calls are allowed; `setrlimit` is one. Errors fall back to the parent's
- // values and are ignored (best effort), so a kernel that refuses a limit
- // cannot wedge a launch.
+ // safe calls are allowed; `getrlimit`/`setrlimit` are. Errors fall back to the
+ // parent's values and are ignored (best effort), so a kernel that refuses a
+ // limit cannot wedge a launch.
unsafe {
cmd.pre_exec(|| {
- for (resource, limit) in [
- (nix::libc::RLIMIT_NPROC, 4096_u64),
- (nix::libc::RLIMIT_NOFILE, 1024_u64),
- (nix::libc::RLIMIT_FSIZE, 1024_u64 * 1024_u64 * 1024_u64),
- ] {
- let r = nix::libc::rlimit {
- rlim_cur: limit,
- rlim_max: limit,
+ for &(resource, limit) in CHILD_LIMITS {
+ let mut r = nix::libc::rlimit {
+ rlim_cur: 0,
+ rlim_max: 0,
};
+ if nix::libc::getrlimit(resource, &mut r) != 0 {
+ continue;
+ }
+ // Never exceed the host's hard limit; setrlimit would refuse.
+ let ceiling = if r.rlim_max == nix::libc::RLIM_INFINITY {
+ limit
+ } else {
+ limit.min(r.rlim_max)
+ };
+ r.rlim_cur = ceiling;
// Best effort: a setrlimit failure is intentionally ignored so a
// kernel that refuses a limit cannot wedge the launch.
let _ = nix::libc::setrlimit(resource, &r);
@@ -744,14 +774,43 @@ mod tests {
child.wait_with_output().await.unwrap();
unregister(None, pid);
- // Spawn a shell that reports its own soft NOFILE limit; confine_limits
- // sets it to 1024, which should be visible inside the sandbox.
- let child = spawn(shell(), "ulimit -n", &tmp(), None, ShellEnv::default(), None).await.unwrap();
- let pid = child.id().unwrap();
- let out = child.wait_with_output().await.unwrap();
- unregister(None, pid);
- let val = String::from_utf8_lossy(&out.stdout).trim().to_string();
- assert_eq!(val, "1024", "NOFILE soft limit should be capped, got: {val}");
+ // Spawn a shell that reports its own soft limits; confine_limits sets
+ // each to the target, bounded by whatever hard limit the host allows.
+ // `ulimit -f` reports FSIZE in 1024-byte blocks, `-n` a raw count.
+ for (name, flag, resource, target, unit) in [
+ ("NOFILE", "-n", nix::libc::RLIMIT_NOFILE, 65536_u64, 1_u64),
+ (
+ "FSIZE",
+ "-f",
+ nix::libc::RLIMIT_FSIZE,
+ 16 * 1024 * 1024 * 1024,
+ 1024,
+ ),
+ ] {
+ let cmd = format!("ulimit {flag}");
+ let child = spawn(shell(), &cmd, &tmp(), None, ShellEnv::default(), None).await.unwrap();
+ let pid = child.id().unwrap();
+ let out = child.wait_with_output().await.unwrap();
+ unregister(None, pid);
+ let val = String::from_utf8_lossy(&out.stdout).trim().to_string();
+
+ let mut host = nix::libc::rlimit {
+ rlim_cur: 0,
+ rlim_max: 0,
+ };
+ // # Safety: reads the calling process's own limit into a local.
+ unsafe { nix::libc::getrlimit(resource, &mut host) };
+ let want = if host.rlim_max == nix::libc::RLIM_INFINITY {
+ target
+ } else {
+ target.min(host.rlim_max)
+ };
+ assert_eq!(
+ val,
+ (want / unit).to_string(),
+ "{name} soft limit should be raised to the target, got: {val}"
+ );
+ }
}
}