Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,28 @@ The `ctor` crate runs the function at load time, before `main` or the
test harness starts. This keeps your test functions clean and avoids
repeating the namespace setup in every binary.

### Keeping your own uid

`init_userns()` maps your user to root (uid 0) inside the namespace. A
process that also talks to the session D-Bus, for example to use a desktop
portal, then fails: D-Bus clients such as `zbus` claim uid 0, and the bus
rejects them.

In that case call `init_userns_keep_uid()` (or
`init_userns_keep_uid_for_ctor()` from a `#[ctor]`). Your user keeps its
uid. Spawned commands keep `CAP_NET_ADMIN`, `CAP_NET_RAW` and
`CAP_NET_BIND_SERVICE`, so `nft`, `tc`, `ping` and low ports work, but
commands such as `mount` do not. That is why it is not the default.
### Your uid and running as root

`init_userns()` keeps your uid inside the namespace. Services that check the
caller's uid, such as the session D-Bus or PipeWire portals, keep working.
The patchbay process has all capabilities in the namespace. Spawned commands
keep `CAP_NET_ADMIN`, `CAP_NET_RAW` and `CAP_NET_BIND_SERVICE`, so `nft`,
`tc`, `ping` and low ports work.

Some commands need root, for example `mount`. Spawn them through `root()`:

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

Root needs a subordinate id range for your user, the same setup rootless
podman uses: `newuidmap` and `newgidmap` on `PATH` and an entry in
`/etc/subuid` and `/etc/subgid`. patchbay maps uid 0 to the first id of
that range. Without it, `root()` returns an error and everything else
works. Root inside the namespace is that subordinate id on the host, so
files a root command creates outside the lab belong to it, and you need
`podman unshare rm` to remove them. Prefer running commands as yourself
where they work.

## Creating a lab

Expand Down
4 changes: 2 additions & 2 deletions patchbay-cli/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use serde::{Deserialize, Serialize};
/// where main() is not our code (nextest spawns each test as a process).
#[ctor::ctor(unsafe)]
fn _init_userns() {
// Safety: called from .init_array before main() and before any threads.
unsafe { patchbay::init_userns_for_ctor() };
// Runs before main() and before any threads. `init()` reports the result.
let _ = patchbay::init_userns();
}

/// Initialize user namespaces (called from main() as well for the CLI binary).
Expand Down
2 changes: 1 addition & 1 deletion patchbay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ libc = "0.2"
netlink-packet-core = "0.9"
netlink-packet-route = "0.33"
netlink-proto = "0.13"
rustix = { version = "1.1", default-features = false, features = ["std", "thread", "process"] }
rustix = { version = "1.1", default-features = false, features = ["std", "thread", "process", "pipe"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
strum = { version = "0.28", features = ["derive"] }
Expand Down
8 changes: 8 additions & 0 deletions patchbay/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ impl Device {
self.lab.netns.spawn_thread_in(&self.ns, f)
}

/// Returns a handle that spawns commands as root in this device's namespace.
///
/// See [`AsRoot`](crate::AsRoot) for when root is available and what it
/// means for files on the host.
pub fn root(&self) -> crate::AsRoot<'_> {
crate::AsRoot::new(&self.lab, &self.ns)
}

/// Spawns a [`tokio::process::Command`] in this device's network namespace.
///
/// The child is registered with the namespace's tokio reactor so that
Expand Down
10 changes: 4 additions & 6 deletions patchbay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@
//!
//! Linux with unprivileged user namespaces enabled, plus `nft` and `tc` in
//! PATH. No root needed. Call [`init_userns`] before spawning any threads
//! to enter the user namespace. Use [`init_userns_keep_uid`] instead if the
//! process also talks to services that check its uid, such as the session
//! D-Bus.
//! to enter the user namespace. The process keeps its uid. Commands can run
//! as root on request with [`Device::root`] if the host has a subordinate id
//! range for the user.

use anyhow::{anyhow, bail, Context, Result};

Expand Down Expand Up @@ -256,9 +256,7 @@ pub use crate::dns_server::DnsServer;
pub use crate::{
core::{NodeId, ReflectorGuard},
event::{IfaceCounters, IfaceSnapshot, LabEvent, LabEventKind, LabState},
userns::{
init_userns, init_userns_for_ctor, init_userns_keep_uid, init_userns_keep_uid_for_ctor,
},
userns::{init_userns, AsRoot},
};

/// Verifies the process has enough privileges to manage namespaces, routes, and raw sockets.
Expand Down
8 changes: 8 additions & 0 deletions patchbay/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,14 @@ impl Router {
self.lab.netns.spawn_thread_in(&self.ns, f)
}

/// Returns a handle that spawns commands as root in this router's namespace.
///
/// See [`AsRoot`](crate::AsRoot) for when root is available and what it
/// means for files on the host.
pub fn root(&self) -> crate::AsRoot<'_> {
crate::AsRoot::new(&self.lab, &self.ns)
}

/// Spawns a [`tokio::process::Command`] in this router's network namespace.
///
/// The child is registered with the namespace's tokio reactor.
Expand Down
Loading
Loading