Skip to content

feat!: init_userns keeps the caller's uid, run commands as root via as_root() - #94

Merged
Frando merged 10 commits into
mainfrom
feat/userns-root
Nov 26, 2026
Merged

Frando merged 10 commits into
mainfrom
feat/userns-root

Conversation

@Frando

@Frando Frando commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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_ctor twin.

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/newgidmap on PATH, /etc/subuid and /etc/subgid entries), the ids around the user's map onto that range. For uid 1000 and 100000:65536:

1000  1000    1       the user, to itself
0     100000  1000    inner 0..=999
1001  101000  64536   inner 1001..=65536

Root, system users and nobody exist inside, so a daemon that drops from root to another user works.

Commands run as root through Device::as_root(), Router::as_root() and Ix::as_root() (the IX root namespace):

let child = device.as_root().spawn_command_sync(cmd)?;

as_root() returns an AsRoot handle with spawn_command and spawn_command_sync. They set uid and gid 0 on the command. The patchbay process has CAP_SETUID in the namespace, so this needs no setuid binary. In a sim, run and spawn steps take root = true.

Root is only for spawned commands. The patchbay process already has all capabilities in the namespace, and glibc applies setuid to 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 newuidmap or newgidmap fails after the unshare, the bootstrap falls back to the single-id map instead of leaving the process without a mapping.

patchbay-cli now enters the user namespace only for run and inspect. test and compare start cargo, whose test binaries set up their own namespace. vm runs qemu and virtiofsd as the host user. Before, test binaries under patchbay test ran nested, and with this PR they would have lost root. run-in also stays outside: from there the user owns the inspect session's namespace, so nsenter may join it. Before, it entered its own sibling namespace and nsenter failed (also on main). run-in passes --preserve-credentials to nsenter, so the command runs as the user and not as root. Where nsenter supports --keep-caps (util-linux 2.39 or later), the command also gets the capabilities of the namespace. With an older nsenter, run-in warns and runs the command without capabilities.

The DNS overlay files (/etc/hosts and /etc/resolv.conf of a device) now have mode 0644 under any umask. A root command that switches to another user, such as nobody, 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 through as_root().
  • Sim steps no longer run as root. Steps that need uid 0 (for example mount, chown, or tools that check for root) need root = true. nft, tc, ip, ping and low ports work without it.
  • init_userns_for_ctor() is deprecated. It calls init_userns(), ignores the error, and keeps the uid like init_userns(). Replace unsafe { patchbay::init_userns_for_ctor() } with let _ = patchbay::init_userns();, which also works from a #[ctor].
  • init_userns_keep_uid() and init_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 test and patchbay compare no longer run cargo inside a user namespace. Each test binary must call init_userns() itself, as the testing guide shows. Before, a test binary without this call worked under patchbay test.

Impact

  • Root inside the namespace is a subordinate id on the host. Files that a root command creates outside the lab belong to it, and the user needs podman unshare rm to remove them. The docs say to prefer running as the user.
  • On a host without /etc/subuid, there is no root. Labs work as before.
  • The NAT and hairpin tests that already flake on main flake more often with this change. feat: retry binds on EADDRINUSE, document rebinding fixed ports #95, now on main, fixes the cause (a forked child briefly holding a closed probe socket).

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 to nobody; 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 with PATCHBAY_EXPECT_ROOT, which CI sets). The root test runs under umask 077 and reads the DNS files as nobody. The deprecated init_userns_for_ctor() does nothing after the bootstrap.
  • tests/userns_fallback.rs: runs its own binary with failing helpers first on PATH: both failing, and only newgidmap failing. Checks the fallback, a working lab, and that the root error carries the helper's message.
  • CLI: a sim with a root = true step and a plain step checks both uids.
  • patchbay-cli/tests/run_in.rs: starts a real inspect session and runs run-in from the initial user namespace. Checks the uid, the network namespace and CAP_NET_ADMIN of the command.
  • Checked by hand:
    • patchbay test -p patchbay --test userns runs the root test for real, with no helper noise.
    • The same binary under unshare --user skips root with the nesting reason.
    • patchbay vm --backend qemu up works. Files the guest writes to /work, also as guest root, are owned by the user on the host.
    • busctl --user status and pw-cli info 0 work from inside the namespace.
    • patchbay run with failing helpers prints the fallback warning.
    • run-in with an nsenter that has no --keep-caps warns, and runs the command as the user without capabilities.
  • The lib suite flaked in NAT and hairpin tests more often than on main. With feat: retry binds on EADDRINUSE, document rebinding fixed ports #95 applied on top, 14 of 14 runs passed. Merged with current main, the workspace suite passes.

🤖 Generated with Claude Code

@Frando Frando changed the title feat!: init_userns keeps the caller's uid, run commands as root via root() feat!: init_userns keeps the caller's uid, run commands as root via as_root() Nov 24, 2026
Frando and others added 4 commits November 25, 2026 15:07
…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 and others added 6 commits November 26, 2026 10:18
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]>
@Frando
Frando merged commit f73eea9 into main Nov 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant