Repository navigation
Fallback to bwrap in linux-sandbox when AppArmor denies mount in userns - #32315
Open
tripleyoung wants to merge 1 commit into
Open
tripleyoung wants to merge 1 commit into
tripleyoung wants to merge 1 commit into
Conversation
tripleyoung
force-pushed
the
fix/linux-sandbox-ubuntu24-bwrap-fallback
branch
from
November 1, 2026 09:22
d4e3c18 to
86a365c
Compare
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
On Ubuntu 24.04+, kernel.apparmor_restrict_unprivileged_userns=1 is enabled by default. Non-allowlisted binaries that create an unprivileged user namespace transition into the 'unprivileged_userns' AppArmor profile, which drops capabilities and denies mount(2) with EACCES, causing Bazel to silently fall back from linux-sandbox to processwrapper-sandbox. Unlike busybox (whose unconfined AppArmor profile was removed in recent Ubuntu security updates and which left child processes unconfined inside the user namespace), /usr/bin/bwrap ships with an official AppArmor profile (/etc/apparmor.d/bwrap-userns-restrict) designed specifically for unprivileged sandboxes: bwrap is permitted to set up user, mount, PID, IPC, UTS, and network namespaces and bind/tmpfs mounts, and then transitions its child process into the 'unpriv_bwrap' stacked profile which strips capabilities inside the user namespace. When SetupMountNamespace() in Pid1Main fails with EACCES or EPERM and /usr/bin/bwrap is available, exit with a dedicated status code before signaling readiness to the parent process and transparently re-spawn the sandbox namespace and mount hierarchy via /usr/bin/bwrap, re-entering linux-sandbox inside the mounted namespace to preserve child process management, shebang error reporting, polite SIGTERM forwarding, and rusage/cgroup statistics collection. Fixes bazelbuild#24081
tripleyoung
force-pushed
the
fix/linux-sandbox-ubuntu24-bwrap-fallback
branch
from
November 1, 2026 10:30
86a365c to
99b6793
Compare
Collaborator
|
Nice find, this can really help improve the situation on Ubuntu by. Rather than falling back to bwrap from within the sandbox binary, would it be possible to detect the availability directly from the Java sandbox runner and run it without the indirection? |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
On Ubuntu 24.04+,
kernel.apparmor_restrict_unprivileged_userns=1is enabled by default (#24081). Whenlinux-sandboxcreates an unprivileged user namespace viaclone(CLONE_NEWUSER), AppArmor transitions it into theunprivileged_usernsprofile, which deniesCAP_SYS_ADMINandmount(2)withEACCES(src/main/tools/linux-sandbox-pid1.cc: "mount": Permission denied). As a result,LinuxSandboxedSpawnRunner.isSupported()fails and Bazel silently falls back toprocesswrapper-sandbox.Previously, #26434 explored wrapping
linux-sandboxwithbusybox, but:busyboxAppArmor profile in Ubuntu 24.04 security updates (sobusybox sh -c '"$@"' -- linux-sandboxnow also fails with"mount": Permission denied).linux-sandboxin anunconfinedprofile left the sandboxed payload itself unconfined with full capabilities inside the user namespace.By contrast,
/usr/bin/bwrap(bubblewrap, pre-installed on Ubuntu 24.04 Desktop/Server) has an official Canonical-maintained AppArmor profile (/etc/apparmor.d/bwrap-userns-restrict) designed specifically for unprivileged sandboxing:profile bwrapis granteduserns,mount,umount, andpivot_rootto construct the sandbox mount table.execveof the child process, AppArmor transitions the child into the stacked&bwrap//&unpriv_bwrapprofile (audit deny capability), stripping capabilities inside the user namespace and preserving Ubuntu's security model.Changes
src/main/tools/linux-sandbox-pid1.cc:SetupMountNamespace(), if the initialmount(nullptr, "/", nullptr, MS_REC | MS_PRIVATE, nullptr)fails withEACCESorEPERM(and!opt.hermeticand/usr/bin/bwrapis executable), close the readiness pipe and exit withkAppArmorBwrapFallbackExitCodebefore signaling the parent.SpawnBwrap()andRunInBwrapPid1(): translate the parsedOptions(-W,-w,-e,-M/-m,-N/-n,-H,-R,-U,-P) into/usr/bin/bwraparguments, and re-enterlinux-sandboxinside the mounted namespace (BAZEL_LINUX_SANDBOX_BWRAP_PID1=1) soSpawnChild(), shebang interpreter diagnostics,ForwardSignal(SIGTERM), andWaitForChild()behave identically to nativePid1Main.src/main/tools/linux-sandbox.cc:kAppArmorBwrapFallbackExitCodeinSpawnPid1()and transparently fall back toSpawnBwrap(), while keeping outer-Ttimeout,-tpoliteSIGTERMgrace period,-l/-Loutput redirection, and-Swait4rusage/cgroup statistics collection in the outer process.Verification on Ubuntu 24.04 (
kernel.apparmor_restrict_unprivileged_userns = 1)linux-sandboxbasic execution, read-only root filesystem, writable-W/-wpaths,-M/-mbind mounts,-etmpfs mounts,-R(root) /-U(nobody) user mapping, missing shebang interpreter diagnostics, politeSIGTERMforwarding (-t),SIGKILLtimeout (-T), and zero FD leaks.LinuxSandboxedSpawnRunner.isSupported()succeeds out-of-the-box and actions execute with# Runner: linux-sandboxinstead of falling back toprocesswrapper-sandbox.Fixes #24081