Skip to content

Keep a resource jail's environment off every command line - #155

Merged
M3gA-Mind merged 5 commits into
tinyhumansai:mainfrom
M3gA-Mind:fix/sr024-native-jail-env-out-of-argv
Nov 12, 2026
Merged

M3gA-Mind merged 5 commits into
tinyhumansai:mainfrom
M3gA-Mind:fix/sr024-native-jail-env-out-of-argv

Conversation

@M3gA-Mind

Copy link
Copy Markdown
Collaborator

Summary

The 2.1 native resource jail (StartJailedExecWithResources) passed the jailed command's environment to bubblewrap as --setenv KEY VALUE arguments. /proc/<pid>/cmdline is readable by every process of the same user, so a token in that environment was readable by every such process for the life of the launcher, systemd-run and bwrap processes.

The --setenv triples now go, NUL-separated, into a private file: mode 0600, in a fresh 0700 directory under $XDG_RUNTIME_DIR (else the temp directory), never inside one of the jail's grants. The launcher opens that file as descriptor 9 and deletes it before anything else runs. bwrap --args 9 then reads the triples. No value appears on any command line or in any host-side process environment. The file is also removed when the launch handle drops, in case the launcher never ran.

Setting the variables on the launcher's own environment was rejected: that would expose them to the host-side programs on the way, and a value such as LD_PRELOAD would run code there.

Related issue

None filed. Found reading the 2.1 launcher. The legacy bwrap backend already avoids this with its own --args file (tinybox-jail/src/bwrap/launcher.rs).

API or behavior changes

  • tinybox_linux::native_jail_argv and native_jail_argv_with_programs return NativeJailLaunch { argv, .. } instead of Vec<String>. The handle owns the environment file and must outlive the spawn. Nothing outside this repo calls them.
  • The module's per-execution resource backend keeps that handle until the child is spawned.
  • No bus contract change.

Validation

On our Linux CI runner (bubblewrap 0.9.0, user systemd session running), at b4910d6:

  • cargo fmt --all -- --check: clean.
  • cargo clippy --all-targets --all-features -- -D warnings: 0 errors, same as main.
  • cargo build --all-targets --all-features and RUSTDOCFLAGS="-D warnings" cargo doc: ok.
  • cargo test -p tinybox-linux -p tinybox-module: no failures, the same as main. The live test a_resource_jail_keeps_the_target_environment_off_every_command_line ran, not skipped.
  • Red controls, each restored afterwards:
    • putting --setenv back in the argv: both the argv test and the live test fail ("the launcher's own argv carries the value");
    • a scan that never matches: the scan test fails.

Tests

  • env_file_tests.rs:
    • the file holds the NUL-separated triples;
    • the file is 0600 and its directory 0700, and both are removed on drop;
    • a NUL in a key or value is refused;
    • a directory inside a grant is never used.
  • The native_jail_command_… test asserts that no target value appears anywhere in the argv.
  • a_resource_jail_keeps_the_target_environment_off_every_command_line (live, Linux with a user systemd session and bubblewrap): it runs a jailed command with a marker value in its environment, and checks that the command sees the value and that no /proc/*/cmdline holds it.
  • the_command_line_scan_finds_a_running_process_argument (Linux): the /proc/*/cmdline scan the live test relies on does find a marker on a running process's command line, so an empty result means no leak, not a broken scan.
  • Red controls: putting --setenv back in the argv fails both tests, and a scan that never matches fails the scan test.

Documentation

Module docs in env_file.rs and rustdoc on NativeJailLaunch.

Checklist

  • The change is focused on one logical change
  • No new #[allow(...)], #[ignore], or relaxed lints
  • No secrets, tokens, or .env contents in the diff or the description

The 2.1 native jail (StartJailedExecWithResources) launched
bash -> systemd-run --scope -> bwrap with the target's environment as
`--setenv KEY VALUE` arguments. /proc/<pid>/cmdline is readable by every
process of the same user, so a token a host passed in the environment
was visible on the launcher's, systemd-run's and bwrap's command lines
for as long as the command ran. Setting the variables on the launcher's
environment instead would expose them to those host-side programs and
let a value such as LD_PRELOAD or BASH_ENV run code there.

The --setenv triples are now written NUL-separated to a private file
(0600 in a fresh 0700 directory under $XDG_RUNTIME_DIR or the temp dir,
never under a grant). The launcher shim opens it as descriptor 9 and
deletes it before closing every other inherited descriptor, and bwrap
reads it with `--args 9`. native_jail_argv(_with_programs) now returns a
NativeJailLaunch that owns the file and removes it on drop, in case the
launcher never runs; the module holds it for the execution.

Not in this change: the namespace box backend's exec also passes
--setenv on bwrap's command line, and its host may be remote (SSH),
which needs its own transport.
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 17ebe6d6-79a2-4d31-b8bc-137ec7a4ff57
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@tinysweeper

tinysweeper Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Tiny Sweeper review

Reviewed head: 42cf394866b4a87525ceeb589d8ec714658a441e

Verdict: Ready for maintainer review · Priority: none
Coverage: Reviewed 8 of 8 files.
Pull request purpose (title): Keep a resource jail…

Diagnostics and supporting details

Full summary

Pull request purpose (title): Keep a resource jail's environment off every command line. Tiny Sweeper completed its review; deterministic results follow.

Review snapshot

Change surface Files Review signal Count
Production 5 Active findings 0
Tests 3 Noted findings 0
Documentation 0 Resolved findings 0
Configuration 0 Pending checks/questions 0

Completeness: Complete
Test assessment: Test coverage is assessed from changed tests and lane evidence; execution is not claimed without trusted check data.

What changed

`EnvArgsFile::create` in `crates/tinybox-linux/src/sandbox/env_file.rs` serializes the environment map into NUL-separated `--setenv`/key/value runs, refusing NULs, into a 0600 file created with `create_new` inside a fresh 0700 directory whose base (`$XDG_RUNTIME_DIR`, else the temp directory) is canonicalized and rejected if under any jail grant, named with random hex from `/dev/urandom`, and removed on `Drop`. `native_bwrap_argv` in `crates/tinybox-linux/src/sandbox/args.rs` now emits `--args 9` when the environment is non-empty instead of per-variable `--setenv` triples, and the launcher shim takes the file path (or `-`) as its first argument, opens it on descriptor 9, unlinks it and its parent directory, and keeps descriptor 9 while closing every other non-stdio descriptor. `native_jail_argv_with_programs` returns a new `NativeJailLaunch` pairing the argv with the owned file, re-exported from `crates/tinybox-linux/src/lib.rs` and `crates/tinybox-linux/src/sandbox/mod.rs`. In `crates/tinybox-module/src/tinybus_module/native_jail.rs`, `NativeResourceBackend` gains a `launch` slot with `keep`, and `configure_resource_launcher` clones the new argv rather than assigning a `Vec<String>`, holding the handle until the execution's backend drops. Tests pin the file contents, permissions, cleanup, NUL refusal, grant avoidance, the argv assertion that no target value or `--setenv` appears, and a live `/proc` scan with a self-check. Latest-push delta: unavailable (no trusted base-to-head diff comparison; the whole-pull-request behavior above is not a push delta).

Features

  • Added — Private NUL-separated environment file for the jail: Target environment values no longer appear in any `--setenv` argv; they live in a 0600 file in a 0700 directory chosen to lie outside the jail's grants, so the jailed program cannot read the file it is fed. (crates/tinybox-linux/src/sandbox/env_file.rs, crates/tinybox-linux/src/sandbox/args.rs pub fn native_jail_argv_with_programs()
  • Modified — bubblewrap reads the environment from descriptor 9: `bwrap --args 9` replaces per-variable `--setenv` arguments, so a token in the target environment is no longer visible in the bwrap process's `/proc/<pid>/cmdline`. (crates/tinybox-linux/src/sandbox/args.rs fn native_bwrap_argv()
  • Modified — Launcher shim opens, unlinks and preserves descriptor 9: The environment file is opened and removed before the scope command runs, and the launcher preserves descriptor 9 while closing every other non-stdio descriptor, so the value reaches only the intended target. (crates/tinybox-linux/src/sandbox/args.rs fn native_bwrap_argv(, crates/tinybox-linux/src/sandbox/env_file.rs)
  • Added — `NativeJailLaunch` launch handle: The public API returns a handle owning the environment file instead of a bare argv, so callers must keep it alive across the spawn; the handle removes the file on drop if the launcher never ran. (crates/tinybox-linux/src/sandbox/args.rs pub fn native_jail_argv_with_programs(, crates/tinybox-linux/src/lib.rs, crates/tinybox-linux/src/sandbox/mod.rs)
  • Modified — Resource backend retains the launch for the execution: The module backend stores the launch handle until the execution's backend is dropped, keeping the file alive long enough for the launcher to open it while still cleaning it up on a launch that never happens. (crates/tinybox-module/src/tinybus_module/native_jail.rs fn configure_resource_launcher(, crates/tinybox-module/src/tinybus_module/native_jail.rs fn unavailable() -> Error {)

Tests

  • unit — Checks that the private file holds the NUL-separated `--setenv`/key/value triples for a two-entry environment including a value with a space.: Added by this change and cited in the description; no execution or pass result is claimed here. (crates/tinybox-linux/src/sandbox/env_file_tests.rs)
  • unit — Checks the file is mode 0600 and its containing directory 0700, and that both are removed when the handle is dropped.: Added by this change and cited in the description; no execution or pass result is claimed here. (crates/tinybox-linux/src/sandbox/env_file_tests.rs)
  • unit — Checks that a NUL in a key or a value is refused with `InvalidInput`.: Added by this change and cited in the description; no execution or pass result is claimed here. (crates/tinybox-linux/src/sandbox/env_file_tests.rs)
  • unit — Asserts the launcher argv contains no `BASH_ENV` or value, no `--setenv`, that `--args 9` is present, that the returned env-file path appears in the argv, and that the file holds the expected NUL-separated triple.: Updated by this change to match the new return type and file mechanism; no execution or pass result is claimed here. (crates/tinybox-linux/src/sandbox/mod_tests.rs fn native_jail_command_keeps_limits_and_target_environment_after_namespace_setup, crates/tinybox-linux/src/sandbox/mod_tests.rs)
  • live_integration — Runs a jailed command carrying a marker value in its environment, confirms the launcher argv holds no value, scans every host process command line while the jail runs for the marker, and confirms the jailed command still receives the value.: Added by this change and skip-guarded when the rootless systemd scope and bubblewrap namespaces are unavailable; no execution or pass result is claimed here. (crates/tinybox-module/src/tinybus_module/native_jail_tests.rs, crates/tinybox-module/src/tinybus_module/native_jail_tests.rs fn a_name_that_is_not_one_plain_component_is_an_invalid_path())

Findings

No active actionable findings.

Before merge

None.

How this fits together

flowchart LR
  n0["NativeJailCommand<br/>changed"]:::changed
  n1["native_bwrap_argv<br/>changed"]:::changed
  n2["native_jail_argv_with_programs<br/>changed"]:::changed
  n3["..._target_environment_after_namespace_setup<br/>changed"]:::changed
  n4["configure_resource_launcher<br/>changed"]:::changed
  n5["unavailable<br/>changed"]:::changed
  n6["...ot_one_plain_component_is_an_invalid_path<br/>changed"]:::changed
  n7["...esolve_trusted_launchers_from_target_path"]:::impacted
  n8["...d_descendants_and_preserves_stream_events"]:::impacted
  n9["tempdir"]:::impacted
  n10["prepare_with_resources"]:::impacted
  n11["io"]:::impacted
  n1 -->|uses| n0
  n2 -->|uses| n0
  n2 -->|calls| n1
  n2 -->|uses| n11
  n3 -->|uses| n0
  n3 -->|calls| n2
  n3 -->|tests| n2
  n3 -->|calls| n9
  n3 -->|tests| n9
  n4 -->|uses| n0
  n4 -->|calls| n2
  n4 -->|calls| n5
  n6 -->|calls| n9
  n6 -->|tests| n9
  n7 -->|uses| n0
  n7 -->|calls| n9
  n7 -->|tests| n9
  n7 -->|uses| n11
  n8 -->|calls| n9
  n8 -->|tests| n9
  n8 -->|calls| n10
  n8 -->|tests| n10
  n10 -->|uses| n0
  n10 -->|calls| n4
  classDef changed fill:#0d4429,stroke:#238636,color:#e6edf3
  classDef impacted fill:#161b22,stroke:#6e7681,color:#c9d1d9
  classDef flagged fill:#5a1e02,stroke:#d93f0b,color:#ffffff
  classDef blocking fill:#67060c,stroke:#f85149,color:#ffffff
Loading
Agent review details

critique

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: Critique lane answered with no findings: it traced argv construction, the shim's `envfile=$1; shift` handling, the `keep()` lifetime, and the updated tests, and found no defect, noting that an empty environment creates no file, the `-` placeholder keeps the shim's `$1` well-defined, and existing `native_jail_argv` callers only inspect `is_err()`.
  • Lane summary: The change moves the target environment from `--setenv` pairs on the launcher's command line into a private `EnvArgsFile` passed to the launcher and read by `bwrap --args 9`, plumbing a `NativeJailLaunch` through `native_jail_argv_with_programs` so the file survives until the backend is dropped. I traced the argv construction, the launcher shim's `envfile=$1; shift` handling, the `keep()` lifetime, and the updated tests, and found no defect: with an empty environment no file is created, the `-` placeholder keeps the shim's `$1` well-defined, and the existing `native_jail_argv` callers only inspect `is_err()`.

security

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Lane summary: Moving the target environment off every command line into a private 0600 file on descriptor 9 is a security improvement, and the launch handle is kept alive until the backend drops. I found no reachable defect introduced by this change; the only remaining question is whether the new live test's `command_lines_containing` helper (which does not compare `windows` lengths correctly against `needle.len()`) can panic or false-negative, which I could not settle within the lookups allowed.

tests

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: Tests lane answered with no reportable defects, mapping the claimed invariants to the file-contents, permissions, cleanup, NUL-refusal and grant-avoidance tests, the argv assertions, and the live scan with its self-check; it named the untested `backend.keep` rejection branch as the only gap.
  • Lane summary: The change moves the target environment off every command line into a private 0600 file read on fd 9, and the tests pin the claimed invariants: `the_file_holds_nul_separated_setenv_triples`, `the_file_and_its_directory_are_private_and_removed_on_drop`, `a_nul_in_an_entry_is_refused`, `a_directory_inside_a_grant_is_never_used`, the updated argv test asserting no `--setenv`/value on the launcher line plus the file's contents, and the live `a_resource_jail_keeps_the_target_environment_off_every_command_line`, backed by a self-check (`the_command_line_scan_finds_a_running_process_argument`) so the scan cannot pass vacuously. The `EnvArgsFile` is held by `NativeResourceBackend` until the backend is dropped, matching the lifetime the launcher needs. I found no defect that meets the reporting bar.

commits

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: Commits lane answered and reported no sensitive content in the committed files.
  • Lane summary: No sensitive content in committed files.

description

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: Description lane answered and found the body consistent with the diff across the file-based environment, the `NativeJailLaunch` return type, the backend handle retention, the new `env_file` module, and the listed tests.
  • Positive: Description lane noted that the Validation and Tests sections cite live tests and red controls not part of this diff, so a reader cannot verify those claims from the request.
  • Lane summary: The description accurately covers this revision: it states the signature change to NativeJailLaunch, the private-file mechanism with descriptor 9, the module backend holding the handle, and the tests it adds, all of which match the diff. The change looks sound; nothing reportable was found beyond the description.
Evidence and run details
  • Models: ladder/vectors-oai3, deepseek-v4.1-flash
  • Spend: $0.003552
  • Tokens: 399237 input · 6728 output · 153344 cached · 772 embedding
  • Updated: 2026-11-12T15:21:03Z
  • Continuity: summary cache chain restarted at the storage ceiling.
Head State Pass summary
b4910d6993bb ready for maintainer review 0 active finding(s), 0 resolved finding(s) (at 2026-11-11T11:51:30Z)
7f2b0f1f2d0b ready for maintainer review 0 active finding(s), 0 resolved finding(s) (at 2026-11-11T14:19:09Z)
b23e4ec813aa ready for maintainer review 0 active finding(s), 0 resolved finding(s) (at 2026-11-12T07:30:20Z)
42cf394866b4 ready for maintainer review 0 active finding(s), 0 resolved finding(s) (at 2026-11-12T15:21:03Z)

tinysweeper 0.1.2

@tinysweeper tinysweeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tinysweeper found nothing blocking. Approving.

Diagnostics

Total spend: $0.0576

@tinysweeper tinysweeper Bot added the priority: p3 Whenever. Cosmetic, a nicety, or a cleanup with no user visible effect. label Nov 11, 2026
M3gA-Mind and others added 2 commits November 11, 2026 19:43

@tinysweeper tinysweeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tinysweeper found nothing blocking. Approving.

Diagnostics

Total spend: $0.0049

# Conflicts:
#	crates/tinybox-linux/src/sandbox/mod_tests.rs
#	crates/tinybox-module/src/tinybus_module/native_jail_tests.rs
@M3gA-Mind
M3gA-Mind merged commit cf573e7 into tinyhumansai:main Nov 12, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority: p3 Whenever. Cosmetic, a nicety, or a cleanup with no user visible effect.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant