Repository navigation
feat!: init_userns keeps the caller's uid, run commands as root via as_root() - #94
Merged
Merged
Conversation
Frando
force-pushed
the
feat/userns-root
branch
from
November 24, 2026 16:33
592868b to
11b4686
Compare
…oot() There is now one bootstrap, `init_userns()`, and the process always keeps its uid and gid. Services that check the caller's uid, such as the session D-Bus and PipeWire portals, work by default. Spawned commands keep CAP_NET_ADMIN, CAP_NET_RAW and CAP_NET_BIND_SERVICE as before in keep-uid mode. If the host has a subordinate id range for the user (newuidmap and newgidmap on PATH, /etc/subuid and /etc/subgid entries), uid and gid 0 map to the first subordinate id and the rest of the range to itself. `Device::root()` and `Router::root()` return an `AsRoot` handle whose `spawn_command` and `spawn_command_sync` run the child as uid 0. The patchbay process has CAP_SETUID in the namespace, so this needs no setuid binary. Without the range, root() spawns fail with an error that names the requirement. If newuidmap or newgidmap fails after the unshare, write the single-id maps instead of leaving the process without a mapping. The forked helper is reaped on every path. ## Breaking changes - `init_userns()` keeps the caller's uid instead of mapping it to 0. Commands that need uid 0 must be spawned through `root()`. - `init_userns_keep_uid()`, `init_userns_for_ctor()` and `init_userns_keep_uid_for_ctor()` are removed. Call `init_userns()`, also from a `#[ctor]`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ters only for labs Address review findings on the uid-keeping bootstrap: - Map the subordinate range onto inner ids 0, 1, ... and skip only the user's own id (0..=999 and 1001..=65536 for uid 1000). Root, system users and nobody now exist, so a daemon that drops from root works. - Skip the range in a nested user namespace, where newuidmap cannot work, and for real root. Look up /etc/subgid by uid, as shadow does. Treat an empty or overflowing range as none. Find only executable helpers. - Keep the reason when root is not mapped (nested, no subuid entry, or the helpers' stderr) and put it in the root spawn error. Helper stderr is no longer printed by the child. - Create the bootstrap pipes with CLOEXEC, and fall back to the single-id map after any failure past the unshare. - AsRoot gets Debug and #[must_use]. - patchbay-cli enters the user namespace only for run, inspect and run-in. test and compare start cargo, whose test binaries set up their own namespace, and vm runs qemu as the host user. Before, test binaries under `patchbay test` ran nested and lost root. - Sim run and spawn steps take `root = true`. Tests require root where the host is set up for it, unless nested or PATCHBAY_EXPECT_ROOT says otherwise. They check that root can switch to nobody, compare capabilities against CapBnd, and cover a newgidmap that fails after newuidmap worked. A CLI test runs a sim with a root step. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t in CI `root()` read like the IX root namespace (`lab.ix()`, `root_ns`). Name the methods after the `AsRoot` handle they return: `Device::as_root()` and `Router::as_root()`. `Ix::as_root()` spawns as root in the IX root namespace, next to `Ix::spawn_command`. AsRoot owns its namespace name. CI sets PATCHBAY_EXPECT_ROOT, so a broken subordinate range on the runner fails the root test instead of skipping it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
run-in joins an inspect session with `nsenter -U -t <keeper> -n`. From its own user namespace, a sibling of the session's, it has no rights there, so nsenter failed. Without a namespace, the user owns the session's namespace and nsenter may join it. Drop the capability check, which assumed the namespace. Checked by hand: run-in in an inspect session runs as root in the node, sees the node's address and pings another node. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Frando
force-pushed
the
feat/userns-root
branch
from
November 25, 2026 14:11
700fe51 to
2e69b1b
Compare
Without --preserve-credentials, nsenter switches to uid 0 after it joins the user namespace. Now that init_userns keeps the caller's uid, that failed with "setgroups failed" on hosts without a subordinate id range, and it ran the command as root on hosts with one. run-in now passes --preserve-credentials, and --keep-caps where nsenter supports it (util-linux 2.39 or later), so the command has the capabilities of the namespace. With an older nsenter, run-in warns and runs the command without capabilities. A new test starts a real inspect session and checks the uid, the network namespace and CAP_NET_ADMIN of a run-in command. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The hosts and resolv.conf files of the DNS overlay got their mode from the umask. Under umask 077 they were 0600, and a root command that switched to another user, such as nobody, could not read them. They are now 0644, whatever the umask. The root test runs under umask 077 and reads both files as nobody. Taken from the container work in #45. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Moving ctor to the Linux-only dev-dependencies also moved serde_json and tempfile there. tests/compare_integration.rs uses tempfile on every platform, so its build failed on macOS. Only ctor is Linux-only now. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The bootstrap warns when newuidmap or newgidmap fail and it falls back to the single-id map. The CLI started tracing after the bootstrap, so the warning never showed. init_tracing starts no threads, so it can run first. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The reason for a missing root said that newuidmap and newgidmap have no
effect in a nested user namespace. That is true for the host's helpers,
but not for helpers inside a container. The reason now says that
patchbay does not try them there.
Ix::as_root uses expect("poisoned") on the lab lock, as the project
convention asks.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
init_userns_for_ctor is part of 0.7.1, and downstream test crates call it from a #[ctor]. It now calls init_userns and ignores the error, so these crates still compile, with a deprecation warning. init_userns works from a #[ctor] too. The libc-only variant existed because of a Rust 1.81 bug: std output before main made the runtime abort at start (rust-lang/rust#130210). Rust 1.83 fixed it, and the dependencies of patchbay need Rust 1.88. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Problem
patchbay had two bootstraps.
init_userns()mapped the user to uid 0, which breaks services that check the caller's uid, such as the session D-Bus and PipeWire portals.init_userns_keep_uid()kept the uid, but then no command could run as root. A process had to pick one, and each had a libc-only_for_ctortwin.Approach
One bootstrap,
init_userns(). The process always keeps its uid and gid.If the host has a subordinate id range for the user (
newuidmap/newgidmaponPATH,/etc/subuidand/etc/subgidentries), the ids around the user's map onto that range. For uid 1000 and100000:65536:Root, system users and
nobodyexist inside, so a daemon that drops from root to another user works.Commands run as root through
Device::as_root(),Router::as_root()andIx::as_root()(the IX root namespace):as_root()returns anAsRoothandle withspawn_commandandspawn_command_sync. They set uid and gid 0 on the command. The patchbay process hasCAP_SETUIDin the namespace, so this needs no setuid binary. In a sim,runandspawnsteps takeroot = true.Root is only for spawned commands. The patchbay process already has all capabilities in the namespace, and glibc applies
setuidto every thread, so an in-process "become root" would not be clean.Without root, the spawn error says why: no subordinate ids, a nested user namespace (where patchbay does not try the helpers), or the helpers' own error output. If
newuidmapornewgidmapfails after the unshare, the bootstrap falls back to the single-id map instead of leaving the process without a mapping.patchbay-clinow enters the user namespace only forrunandinspect.testandcomparestart cargo, whose test binaries set up their own namespace.vmruns qemu and virtiofsd as the host user. Before, test binaries underpatchbay testran nested, and with this PR they would have lost root.run-inalso stays outside: from there the user owns the inspect session's namespace, sonsentermay join it. Before, it entered its own sibling namespace andnsenterfailed (also on main).run-inpasses--preserve-credentialstonsenter, so the command runs as the user and not as root. Wherensentersupports--keep-caps(util-linux 2.39 or later), the command also gets the capabilities of the namespace. With an oldernsenter,run-inwarns and runs the command without capabilities.The DNS overlay files (
/etc/hostsand/etc/resolv.confof a device) now have mode 0644 under any umask. A root command that switches to another user, such asnobody, can then read them. This fix comes from #45.Breaking changes
init_userns()keeps the caller's uid instead of mapping it to 0. Commands that need uid 0 must be spawned throughas_root().mount,chown, or tools that check for root) needroot = true.nft,tc,ip,pingand low ports work without it.init_userns_for_ctor()is deprecated. It callsinit_userns(), ignores the error, and keeps the uid likeinit_userns(). Replaceunsafe { patchbay::init_userns_for_ctor() }withlet _ = patchbay::init_userns();, which also works from a#[ctor].init_userns_keep_uid()andinit_userns_keep_uid_for_ctor()(from feat(userns): add init_userns_keep_uid to keep the caller's uid #91) are removed. No release contains them.patchbay testandpatchbay compareno longer run cargo inside a user namespace. Each test binary must callinit_userns()itself, as the testing guide shows. Before, a test binary without this call worked underpatchbay test.Impact
podman unshare rmto remove them. The docs say to prefer running as the user./etc/subuid, there is no root. Labs work as before.Testing
tests/userns.rs: the uid maps to itself and 0, 1 and 65534 exist;as_root()spawns run as uid 0 with the full bounding set and can switch tonobody; the async variant works; plain spawns run as the user. Root is required where the host is set up for it, unless the test runs nested (or always withPATCHBAY_EXPECT_ROOT, which CI sets). The root test runs under umask 077 and reads the DNS files asnobody. The deprecatedinit_userns_for_ctor()does nothing after the bootstrap.tests/userns_fallback.rs: runs its own binary with failing helpers first onPATH: both failing, and onlynewgidmapfailing. Checks the fallback, a working lab, and that the root error carries the helper's message.root = truestep and a plain step checks both uids.patchbay-cli/tests/run_in.rs: starts a realinspectsession and runsrun-infrom the initial user namespace. Checks the uid, the network namespace andCAP_NET_ADMINof the command.patchbay test -p patchbay --test usernsruns the root test for real, with no helper noise.unshare --userskips root with the nesting reason.patchbay vm --backend qemu upworks. Files the guest writes to/work, also as guest root, are owned by the user on the host.busctl --user statusandpw-cli info 0work from inside the namespace.patchbay runwith failing helpers prints the fallback warning.run-inwith annsenterthat has no--keep-capswarns, and runs the command as the user without capabilities.🤖 Generated with Claude Code