Skip to content

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

Open
Frando wants to merge 1 commit into
mainfrom
feat/userns-root
Open

Frando wants to merge 1 commit 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 map is:

1000   1000   1       the user, to itself
0      100000 1       root, to the first subordinate id
100001 100001 65535   the rest of the range, to itself

Commands run as root through Device::root() and Router::root():

let child = device.root().spawn_command_sync(Command::new("mount"))?;

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. Without a subordinate range, root is not mapped, and these spawns fail with an error that names the requirement.

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.

If newuidmap or newgidmap fails after the unshare, for example without its setuid bit, the bootstrap writes the single-id maps. Before, such a host was left in a namespace without a mapping, and every lab failed.

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]. patchbay-cli and the lib tests already do.

Impact

  • Root inside the namespace is the first subordinate id on the host. Files that a root command creates outside the lab belong to that id, 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 (nft, tc and ping use the ambient capabilities).
  • Nothing in patchbay, the runner or the CLI needs uid 0. The only sudo calls are inside the VM guest.

Testing

  • tests/userns.rs: the uid maps to itself, 0 maps to the first subordinate id when a range exists, root() spawns run as uid 0 with all capabilities, plain spawns run as the user, and root() fails without a range.
  • tests/userns_fallback.rs: runs its own binary with failing newuidmap/newgidmap first on PATH, and checks that the bootstrap falls back and a lab works. Fails without the fallback.
  • The lib suite runs with the new bootstrap. Across four full runs, each run had at most one failure among the NAT and hairpin tests that also flake on main, and each passes alone.
  • Checked by hand: busctl --user status and pw-cli info 0 work from inside the namespace.

The containers PR (#45) builds on this.

🤖 Generated with Claude Code

…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 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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