Skip to content

feat(userns): add init_userns_keep_uid to keep the caller's uid - #91

Merged
Frando merged 1 commit into
mainfrom
Frando/userns-keep-uid
Nov 20, 2026
Merged

Frando merged 1 commit into
mainfrom
Frando/userns-keep-uid

Conversation

@Frando

@Frando Frando commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Problem

init_userns maps the calling user to root (uid 0) inside the user namespace. A process that runs a lab and also talks to the session D-Bus (for example a desktop portal) then fails the handshake: clients such as zbus claim uid 0, and the bus rejects them (EXTERNAL rejected by the server). Such a process has to carry its own copy of the bootstrap.

Approach

Add init_userns_keep_uid() and init_userns_keep_uid_for_ctor(). They map the user and group to themselves and raise CAP_NET_ADMIN, CAP_NET_RAW and CAP_NET_BIND_SERVICE into the ambient set. The process keeps all capabilities in the namespace, so everything patchbay does in-process works unchanged. Spawned nft, tc, conntrack and ping keep working.

Both Rust entry points share one OnceLock: calling both variants in one process returns an error. The ctor path stays libc-only.

Impact

  • Additive. init_userns keeps its behavior.
  • The patchbay lib tests pass with the default, and also with the ctor switched to the new variant (local experiment). New tests/userns_keep_uid.rs checks the uid mapping, NAT, tc, a bind on port 80 and ping as a child.
  • Checked by hand: an in-process zbus session connection fails after init_userns and works after init_userns_keep_uid.

Alternatives

  • Change init_userns itself. Not done: spawned commands would no longer be root, which can break user commands and runner steps (mount, file ownership, root checks).
  • An options struct. With one knob, two functions are simpler and mirror the existing pair.

`init_userns` maps the calling user to root inside the user namespace.
A process that runs a lab in-process and also talks to the user's
session D-Bus (for example a desktop portal) then fails the D-Bus
handshake: clients like zbus send uid 0 in the EXTERNAL mechanism, and
the bus rejects it because the kernel reports the real uid.

Add `init_userns_keep_uid` and `init_userns_keep_uid_for_ctor`, which
map the user and group to themselves and raise CAP_NET_ADMIN,
CAP_NET_RAW and CAP_NET_BIND_SERVICE into the ambient set, so spawned
`nft`, `tc`, `conntrack` and `ping` keep working. The default stays the
root mapping, because spawned commands only keep those capabilities.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Frando
Frando force-pushed the Frando/userns-keep-uid branch from f1b371c to 8379a6b Compare November 20, 2026 12:43
@Frando
Frando marked this pull request as ready for review November 20, 2026 12:46
@Frando
Frando merged commit c084f10 into main Nov 20, 2026
4 checks passed
Frando added a commit that referenced this pull request Nov 26, 2026
…s_root() (#94)

## 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):

```rust
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
#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. #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 #95 applied on top, 14 of 14 runs passed. Merged with current main,
the workspace suite passes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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