Skip to content

Apply CPU, memory and pid limits to bubblewrap jails through a systemd scope - #173

Merged
M3gA-Mind merged 3 commits into
tinyhumansai:mainfrom
M3gA-Mind:feat/104-bwrap-resource-limits
Nov 12, 2026
Merged

M3gA-Mind merged 3 commits into
tinyhumansai:mainfrom
M3gA-Mind:feat/104-bwrap-resource-limits

Conversation

@M3gA-Mind

Copy link
Copy Markdown
Collaborator

Summary

The bubblewrap jail backend claimed no resource limits. A hard CPU, memory or pid requirement was refused, and a soft resources budget was silently not applied.

Now the launcher runs under a rootless systemd-run --user --scope, so the whole jail, bwrap included, lives in one transient cgroup holding the limits. Only the controllers the user manager was delegated are applied and claimed in constraint_support; Disk stays unsupported.

A jail that could reach the user manager could start a process outside its scope. So a hard limit is refused for a jail that shares the host network (its abstract sockets reach the manager) or that is granted /run or /sys. A soft limit is still applied there, as a best effort.

tinybox-linux already builds this scope prefix for native jails. It now exposes it as resource_scope_argv with ScopeLimits, so both paths share one implementation.

Related issue

Closes #104. Its last open item was the known gap "resource limits" for the bwrap backend. The other items are already on main:

Part of #27: this is the last tinybox-side item of its narrowed scope (bwrap limits).

API or behavior changes

  • BubblewrapBackend::constraint_support now reports Cpu, Memory and Pids as Enforced for each controller the user manager was delegated, and Unsupported otherwise. Before, all three were always unsupported.
  • A bwrap jail with resources now runs inside a tinybox-bwrap-<pid>-<n>.scope unit. The scope's task count also includes the two or three host-side processes bwrap runs as.
  • A hard Cpu/Memory/Pids requirement is refused with Unsupported for a jail that shares the host network or is granted /run or /sys.
    • A soft budget whose scope command cannot be built runs unlimited, as before, and logs at debug level.
  • The module's Linux native jail path uses this backend. A resource budget that names no disk, on a host whose manager was delegated the controllers, can now be admitted for a jail with a private network: one that denies network, or one with a pasta network. Before, it was always refused.
  • New public API in tinybox-linux: resource_scope_argv and ScopeLimits. It's additive.
  • No wire type changes, and no contract version change.

Validation

Run on our Linux CI runner at cd3ff44, against main b0a91eb:

  • cargo fmt --all -- --check: clean.
  • cargo clippy --all-targets --all-features -- -D warnings: 0 errors, the same as main.
  • cargo build --all-targets --all-features --locked, and RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features: ok.
  • cargo test --all-features --workspace --locked: 1684 passed and 0 failed, with no failures unique to this branch (main: 1670 passed).
  • TINYBOX_REQUIRE_BWRAP=1 cargo test -p tinybox-jail --test bwrap_isolation --test bwrap_fd_leak: 26 passed. cargo test -p tinybox-jail --lib bwrap: 81 passed. Nothing skipped.
  • TINYBOX_REQUIRE_BWRAP=1 TINYBOX_REQUIRE_BWRAP_LIMITS=1 cargo test -p tinybox-jail --test bwrap_resource_limits, under a user systemd manager delegated cpu memory pids: 2 passed. The shared-network test was run again with TINYBOX_REQUIRE_SHARED_NETWORK=1 on a host without pasta, and passed.
  • cargo test -p tinybox-module native_jail: 35 passed, nothing skipped. No transient scope units were left behind.

Tests

  • bwrap::scope::tests (7 tests):
    • only delegated controllers are claimed;
    • no budget or no scope means no scope command;
    • a soft budget runs unlimited when the scope command cannot be built, and a hard one fails closed;
    • a hard limit is refused for a shared host network and for grants at or under /run or /sys;
    • an unrelated grant with a private network has no escape route.
  • bwrap::tests:
    • constraint_support follows the delegated controllers;
    • a scoped launch starts systemd-run with bwrap after its separator;
    • a hard limit the scope cannot hold refuses the spawn.
  • bwrap::launcher::tests: the scoped launcher keeps only XDG_RUNTIME_DIR and DBUS_SESSION_BUS_ADDRESS, and its argv is the prefix followed by the unchanged launcher.
  • tinybox-linux sandbox::test: the shared prefix sets only the limits asked for, ends at --, and gives each scope its own unit name. The existing native-jail argv tests cover the refactor.
  • New live suite tests/bwrap_resource_limits.rs:
    • a 32 MiB hard memory limit kills a jail holding 128 MiB, and a 1 GiB limit lets the same work finish;
    • a hard limit is refused for a jail sharing the host network.
  • CI: the native-resource-live job already starts a user manager with delegated controllers, and now runs this suite with TINYBOX_REQUIRE_BWRAP=1 TINYBOX_REQUIRE_BWRAP_LIMITS=1, so a skip there fails. It then removes passt and runs the shared-network test again with TINYBOX_REQUIRE_SHARED_NETWORK=1.
  • Not tested live: the CPU and pid limits. They go through the same scope prefix that the native jail's live CPU-throttle and fork-limit tests already exercise.

Documentation

  • crates/tinybox-jail/src/bwrap/mod.rs: a new "Resource limits" section, with the entry removed from "Known gaps".
  • crates/tinybox-jail/src/bwrap/scope.rs: module docs on the scope, the task count and the escape routes.
  • crates/tinybox-jail/README.md: the bwrap row is updated.

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

…d scope

The bubblewrap backend claimed no resource limits: a hard request was
refused and a soft one silently not applied. Run the launcher under a
rootless systemd-run scope for the controllers the user manager was
delegated, claim exactly those in constraint_support, and refuse a hard
limit for a jail that could reach the manager and leave its scope (a
shared host network, or a grant of /run or /sys).

tinybox-linux exposes the scope prefix it already builds for native
jails (resource_scope_argv, ScopeLimits), so both paths share it.
@coderabbitai

coderabbitai Bot commented Nov 12, 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: 760df075-b2a7-4e51-9ba6-95b3b9df276b
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@tinysweeper

tinysweeper Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Tiny Sweeper review

Reviewed head: cd3ff44d5c4311e84fef319c051a378eafc0cb8e

Verdict: Ready for maintainer review · Priority: none
Coverage: Reviewed 13 of 13 files.
Pull request purpose (title): Apply CPU, memory an…

Diagnostics and supporting details

Full summary

Pull request purpose (title): Apply CPU, memory and pid limits to bubblewrap jails through a systemd scope. Tiny Sweeper completed its review; deterministic results follow.

Review snapshot

Change surface Files Review signal Count
Production 6 Active findings 0
Tests 5 Noted findings 0
Documentation 1 Resolved findings 0
Configuration 1 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.

Features

  • Added — Rootless systemd scope for bubblewrap resource limits: A jail carrying `resources` now runs under a `tinybox-bwrap-<pid>-<n>.scope` transient unit, so CPU, memory and pid limits can be enforced for the whole jail instead of being silently unapplied. (crates/tinybox-jail/src/bwrap/scope.rs, crates/tinybox-jail/src/bwrap/mod.rs fn prepare_command(, crates/tinybox-linux/src/sandbox/args.rs fn systemd_run_supports_safe_arguments(systemd_run: &Path) -> std::io::Result<bo)
  • Added — Minimal environment for the scoped launcher: When wrapped, the launcher is re-parented under `systemd-run` with an otherwise empty environment restricted to `XDG_RUNTIME_DIR` and `DBUS_SESSION_BUS_ADDRESS`, and the launcher argv is preserved after the scope separator. (crates/tinybox-jail/src/bwrap/launcher.rs, crates/tinybox-jail/src/bwrap/launcher_tests.rs fn entering_namespaces_puts_nsenter_in_front_by_descriptor() -> io::Result<()> {)
  • Internal refactor — Shared scope-prefix API in tinybox-linux: The native jail scope prefix builder is generalized into a public `resource_scope_argv`/`ScopeLimits` API and re-exported, so native jails and the bubblewrap backend share one implementation without a change to native behavior. (crates/tinybox-linux/src/sandbox/args.rs fn systemd_run_supports_safe_arguments(systemd_run: &Path) -> std::io::Result<bo, crates/tinybox-linux/src/lib.rs mod sandbox;, crates/tinybox-linux/src/sandbox/mod.rs mod args;)
  • Modified — Updated documentation for resource limits: The bwrap README row and module docs now describe the scope-based limits, the reflection in `constraint_support`, and the escape-route refusal, and the resource-limits entry is removed from known gaps. (crates/tinybox-jail/README.md spawns, never the core process itself., crates/tinybox-jail/src/bwrap/mod.rs struct Verified {)
  • Added — CI job proving bubblewrap limits hold: The CI workflow runs the live bwrap resource-limit suite with skips treated as failures, then removes passt and re-runs the shared-network refusal test with `TINYBOX_REQUIRE_SHARED_NETWORK=1`, so the live paths are exercised rather than silently skipped. (.github/workflows/ci.yml jobs:, crates/tinybox-jail/tests/bwrap_resource_limits.rs)

Tests

  • unit — Checks that only delegated controllers are claimed and that no resources or no scope yields no scope command.: Covers the claiming logic in `with_limits` and `prefix_for`; the tests lane reports these are covered. (crates/tinybox-jail/src/bwrap/scope_tests.rs)

Findings

No active actionable findings.

Before merge

None.

How this fits together

flowchart LR
  n0["...paces_puts_nsenter_in_front_by_descriptor<br/>changed"]:::changed
  n1["BubblewrapBackend<br/>changed"]:::changed
  n2["Verified<br/>changed"]:::changed
  n3["prepare_command<br/>changed"]:::changed
  n4["verify<br/>changed"]:::changed
  n5["discard"]:::impacted
  n6["env"]:::impacted
  n7["...riptors_reach_bwrap_and_nothing_else_does"]:::impacted
  n8["tempdir"]:::impacted
  n9["the_launcher_runs_with_an_empty_environment"]:::impacted
  n0 -->|calls| n5
  n0 -->|tests| n5
  n1 -->|uses| n2
  n3 -->|uses| n2
  n3 -->|uses| n6
  n4 -->|uses| n2
  n7 -->|calls| n5
  n7 -->|tests| n5
  n7 -->|calls| n8
  n7 -->|tests| n8
  n9 -->|calls| n5
  n9 -->|tests| n5
  n9 -->|calls| n8
  n9 -->|tests| n8
  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
  • Lane summary: Reviewed 13 files; 0 findings.

security

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The security lane answered with 0 findings across 12 files, noting only that `crates/tinybox-jail/README.md` was prose and not security-reviewed.
  • Lane summary: Reviewed 12 files; 0 findings. 1 file was not security-reviewed: crates/tinybox-jail/README.md (prose or tabular data).

tests

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Lane summary: The change wires bubblewrap jails into a rootless systemd scope and adds unit, integration and CI coverage for the new limit-claiming, refusal and scope-prefix paths; the one gap I found is a doc-claim in `prefix_for` that nothing pins — the argument invariant the code actually relies on is untested.

commits

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The commits lane answered with no findings, reporting no sensitive content in committed files.
  • Lane summary: No sensitive content in committed files.

description

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The description lane answered with no findings; it judged the description consistent with the diff, noting only that it doesn't mention the seccomp sentence added to the same README edit.
  • Lane summary: The description matches the diff closely: it announces the new `resource_scope_argv`/`ScopeLimits` API, the `constraint_support` change, the escape-route refusals, the scope environment allow-list, the new tests and the CI job, and all of those are present. Two things it does not mention: the pre-existing 'no resource limits yet' sentence it removes from the README was joined by a seccomp sentence that is also part of this edit, and the type is named `ScopeLimits` (self-consistent) — neither is a substantive mismatch. I found no reportable description defect.
Evidence and run details
  • Models: ladder/vectors-oai3, deepseek-v4.1-flash
  • Spend: $0.007747
  • Tokens: 889077 input · 11506 output · 315392 cached · 785 embedding
  • Updated: 2026-11-12T17:05:38Z
Head State Pass summary
cd3ff44d5c43 ready for maintainer review 0 active finding(s), 0 resolved finding(s) (at 2026-11-12T17:05:38Z)

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.0077

@tinysweeper tinysweeper Bot added the priority: p3 Whenever. Cosmetic, a nicety, or a cleanup with no user visible effect. label Nov 12, 2026
@M3gA-Mind
M3gA-Mind merged commit 5eadf43 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.

bwrap jail backend: follow-ups from #99 review

1 participant