Skip to content

LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.

lord-netperm: no-mic back in the user@ union; only the user's real PipeWire and WirePlumber (exact cgroup, sole process, matched by tgid) keep the capture nodes; tests for the escape and for the desktop keeping its microphone

Federico Pereirab7299561d6e4
diff --git a/docs/FIREWALL.md b/docs/FIREWALL.md
index d2b4acf..f3c41b4 100644
--- a/docs/FIREWALL.md
+++ b/docs/FIREWALL.md
@@ -190,10 +190,13 @@ on `open()` and `mknod()` of a device node, and returns 0 only for the device cl
restricted from (`np_dev_restriction()` in `np_shared.h`, plus, for "no microphone", the ALSA
capture PCMs in `np_snd`). In the app's own scope "no microphone" fails closed: an ALSA node the
last scan of `/sys/class/sound` hasn't classified yet (a USB microphone plugged in, at most 5 s)
-is denied too. "No microphone" is never part of the `user@` union: there it would deny the ALSA
-capture nodes to the user's own PipeWire, which drops the microphone for every app and doesn't
-get it back. A process that escaped to `user@` is held by the PipeWire guard instead; opening the
-card directly from there is a residual risk. Everything else returns 1. Programs systemd attaches higher up (`DeviceAllow=`) still
+is denied too. Under the `user@` union "no microphone" denies the known capture nodes to every
+process except the user's real PipeWire and WirePlumber (otherwise the microphone is gone for
+every app and doesn't come back): exactly their process id in exactly the cgroups
+`user.slice/user-<uid>.slice/user@<uid>.service/session.slice/{pipewire,wireplumber}.service`, and
+only while each holds that one process (`np_sndsvc`, refreshed every second). A process the user moves
+into that cgroup has another process id and is still denied; a nested cgroup that merely ends in
+the same names is another cgroup. Everything else returns 1. Programs systemd attaches higher up (`DeviceAllow=`) still
apply: the kernel allows only what all of them allow. A denial is logged by `lord-netperm` once per
5 s, not counted with the connections. `lord-server`'s own scope (`lordos`) never gets device
restrictions in the kernel: it opens the camera, the FIDO key and the sound for the apps after
@@ -239,12 +242,26 @@ the apps could change. Who a client is:
- for a native client, `pipewire.sec.pid`, which PipeWire takes from the socket's credentials
(a client can't change `pipewire.sec.*`), and that PID's cgroup;
- `application.process.id` (what the app reported) is used only when `pipewire.sec.pid` is the
- session's real `pipewire-pulse`: the root-owned `/usr/bin/pipewire` executable, our uid, in the
- user manager's `pipewire-pulse.service`. `pulse.server.type` alone is the client's word and is
- ignored;
+ session's real `pipewire-pulse`: the root-owned `/usr/bin/pipewire` executable, our uid, at the
+ exact cgroup path of the user manager's `pipewire-pulse.service` and the only process in it (the
+ user owns the `user@` subtree: a nested cgroup that merely ends in the same names, or a second
+ process moved into the unit, never counts). `pulse.server.type` alone is the client's word and
+ is ignored;
- even then the claimed process must really hold as many connections to `pipewire-pulse` as there
are clients claiming it (the kernel's unix socket peers, `sock_diag`, against the process's
- fds);
+ fds), and must live in our PID namespace (a process in another one, such as a `no-files` app,
+ reports its inner PID, so a claim of its outer PID is someone else's);
+- `pipewire-pulse`'s own connection carries the streams of modules a pulse client loaded; while a
+ restricted app runs its capture streams are closed. Module loading itself is off in LordOS
+ (`/etc/pipewire/pipewire-pulse.conf.d/lordos-no-modules.conf`: a loopback from the
+ microphone, a tunnel to any host or a pipe at any path would run outside the app's scope);
+- a client of a process under `user@<uid>` (escaped with `systemd-run --user`, or D-Bus
+ activated) is treated as restricted while a restricted app runs, except PipeWire, WirePlumber and
+ `pipewire-pulse` themselves, checked by exact unit path, being alone in it and their root-owned
+ executable;
+- when the guard can't follow a client or a capture node (its fixed tables are full), it
+ disconnects or destroys it; a permission list longer than one message is sent in parts. It never
+ leaves a client unrestricted because it ran out of room;
- while a restricted app of the session is running, a client whose identity can't be established
(a false claim, a PID that doesn't resolve or is gone) is treated as restricted; with no
restricted app running nothing is checked, so normal use pays nothing.
@@ -262,10 +279,17 @@ the apps could change. Who a client is:
- When a restricted app falsely claims another process's PID through `pipewire-pulse`, every
client claiming that PID is treated as restricted while the false claim lasts, the honest one
included.
-- A process that escaped to `user@<uid>` (`systemd-run --user`) is in no app scope: its PipeWire
- clients aren't restricted (PipeWire, WirePlumber and `pipewire-pulse` themselves live there).
- With `no-files` the app can't reach the session bus or the user manager, which closes that path;
- without it, it stays open.
+- A process that escaped to `user@<uid>` (`systemd-run --user`) is held as restricted only while
+ a restricted app runs, like `lord-netperm`'s union (which also denies it the ALSA capture
+ nodes). If the restricted app exits first, the escaped process is free. With `no-files` the app
+ can't reach the session bus or the user manager, which closes that path; no-mic and no-camera
+ alone don't cut the session bus (that would break most apps' D-Bus use).
+- If PipeWire restarts while a no-mic app runs, the new daemon is known to `lord-netperm` within a
+ second; an ALSA capture open in that second is refused.
+- A Linux app restricted without `no-files` runs as the user, so it could take over another of the
+ user's processes (lord-server, PipeWire, another app) with `ptrace` and act through it. LordOS
+ sets `kernel.yama.ptrace_scope = 1` (`/usr/lib/sysctl.d/50-lordos-ptrace.conf`): only a
+ process's own descendants can be traced.
- A pulse socket of its own for each restricted app (`pipewire-pulse` listening on a per-app
address bound into the app's view) would remove the need to trust `application.process.id` at
all: also the next step.
diff --git a/server/src/netperm/netperm.c b/server/src/netperm/netperm.c
index aec7363..2f9cef9 100644
--- a/server/src/netperm/netperm.c
+++ b/server/src/netperm/netperm.c
@@ -80,6 +80,7 @@ typedef struct {
int nsc, cap;
int dirty_counts;
uint64_t user_cg; /* id of user@<uid>.service, or 0 if it isn't there (nor attached) */
+ uint32_t snd_tgid[2]; /* its PipeWire and WirePlumber in np_sndsvc (0 = none) */
int ulinks[16], nulinks; /* the programs attached to user@<uid>.service */
} Session;
@@ -101,7 +102,7 @@ typedef struct {
static struct {
int bpf_ok;
char bpf_err[200]; /* a code line (errcode.h): the firewall page shows it translated */
- int m_np_cfg, m_np_apps, m_np_union, m_np_lpm4, m_np_lpm6, m_np_seen, m_np_counts, m_np_events, m_np_snd;
+ int m_np_cfg, m_np_apps, m_np_union, m_np_lpm4, m_np_lpm6, m_np_seen, m_np_counts, m_np_events, m_np_snd, m_np_sndsvc;
uint32_t snd[NP_MAX_SND]; /* the ALSA minors in np_snd */
int nsnd;
int dev_ok, bt_ok; /* the device / Bluetooth programs loaded */
@@ -211,6 +212,7 @@ static int map_fd(const char *name) {
if (!strcmp(name, "np_counts")) return G.m_np_counts;
if (!strcmp(name, "np_events")) return G.m_np_events;
if (!strcmp(name, "np_snd")) return G.m_np_snd;
+ if (!strcmp(name, "np_sndsvc")) return G.m_np_sndsvc;
return -1;
}
#endif
@@ -309,16 +311,13 @@ static void drop_scope(Scope *sc) {
/* Recompiles the user's union from the flags of their scopes and writes it (or deletes it if
* there's no restriction). Only takes effect with user@ attached. */
+static void sync_sndsvc(Session *s);
+
static void sync_union(Session *s) {
if (!G.bpf_ok || !s->user_cg) return;
NpApp u;
memset(&u, 0, sizeof u);
- /* "no microphone" stays out of the union: under user@ it would deny the ALSA capture nodes
- * to the user's own PipeWire, which then drops the microphone for every app (and doesn't get
- * it back). A process escaped there is held by the PipeWire guard instead (its clients count
- * as restricted while restricted apps run); reading the card directly from there is the
- * residual risk (docs/FIREWALL.md). */
- for (int k = 0; k < s->nsc; k++) u.flags |= s->sc[k].app_v.flags & ~NP_R_NO_MIC;
+ for (int k = 0; k < s->nsc; k++) u.flags |= s->sc[k].app_v.flags;
if (u.flags) {
u.trusted = 1; /* whatever the union doesn't restrict is allowed without notice */
u.mode = NP_M_SILENT;
@@ -326,6 +325,38 @@ static void sync_union(Session *s) {
} else {
bpfs_map_delete(G.m_np_union, &s->user_cg);
}
+ sync_sndsvc(s);
+}
+
+/* The user's real sound services, which keep the ALSA capture nodes under the union's no-mic
+ * (PipeWire opens them to record, WirePlumber to probe the card's profiles): the exact cgroups
+ * user.slice/user-<uid>.slice/user@<uid>.service/session.slice/{pipewire,wireplumber}.service
+ * (never a nested one that merely ends in the same names), and only while each holds exactly one
+ * process. Its tgid and cgroup id go into np_sndsvc; the program matches both. Refreshed every
+ * second (a restart is a new process). */
+static void sync_sndsvc(Session *s) {
+ if (!G.bpf_ok) return;
+ static const char *const unit[2] = { "pipewire.service", "wireplumber.service" };
+ char dir[200], path[300], line[32];
+ snprintf(dir, sizeof dir, CG_ROOT "/" CG_USER "/user-%u.slice/user@%u.service/session.slice", s->uid, s->uid);
+ int dfd = s->user_cg ? open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC) : -1;
+ for (int k = 0; k < 2; k++) {
+ uint32_t tgid = 0;
+ uint64_t cg = dfd >= 0 ? bpfs_cgroup_id(dfd, unit[k]) : 0;
+ if (cg) {
+ snprintf(path, sizeof path, "%s/%s/cgroup.procs", dir, unit[k]);
+ FILE *f = fopen(path, "re");
+ int n = 0;
+ unsigned long v = 0;
+ while (f && fgets(line, sizeof line, f)) { if (!n) v = strtoul(line, NULL, 10); n++; }
+ if (f) fclose(f);
+ if (n == 1 && v > 0 && v <= UINT32_MAX) tgid = (uint32_t)v;
+ }
+ if (s->snd_tgid[k] && s->snd_tgid[k] != tgid) bpfs_map_delete(G.m_np_sndsvc, &s->snd_tgid[k]);
+ if (tgid && bpfs_map_update(G.m_np_sndsvc, &tgid, &cg, BPF_ANY)) tgid = 0;
+ s->snd_tgid[k] = tgid;
+ }
+ if (dfd >= 0) close(dfd);
}
static void drop_user_links(Session *s) {
@@ -424,6 +455,7 @@ static void drop_session(int k) {
for (int i = 0; i < s->nsc; i++) drop_scope(&s->sc[i]);
drop_user_links(s);
if (s->user_cg) { bpfs_map_delete(G.m_np_union, &s->user_cg); bpfs_map_delete(G.m_np_counts, &s->user_cg); }
+ for (int k = 0; k < 2; k++) if (s->snd_tgid[k]) bpfs_map_delete(G.m_np_sndsvc, &s->snd_tgid[k]);
if (s->wd >= 0) inotify_rm_watch(G.ifd, s->wd);
free(s->sc);
fw_store_free(s->store);
@@ -761,7 +793,7 @@ static int bpf_setup(void) {
#define NP_MKMAP(n, t, k, v, m, f) G.m_##n = bpfs_map_create(t, k, v, m, f, #n);
NP_MAPS(NP_MKMAP)
#undef NP_MKMAP
- if (G.m_np_snd < 0 || G.m_np_cfg < 0 || G.m_np_apps < 0 || G.m_np_union < 0 || G.m_np_lpm4 < 0 || G.m_np_lpm6 < 0 || G.m_np_seen < 0 || G.m_np_counts < 0 || G.m_np_events < 0) {
+ if (G.m_np_snd < 0 || G.m_np_sndsvc < 0 || G.m_np_cfg < 0 || G.m_np_apps < 0 || G.m_np_union < 0 || G.m_np_lpm4 < 0 || G.m_np_lpm6 < 0 || G.m_np_seen < 0 || G.m_np_counts < 0 || G.m_np_events < 0) {
lerr_set(G.bpf_err, sizeof G.bpf_err, "fw.bpfMaps", "errno", lerr_errno(errno), NULL);
return -1;
}
@@ -939,6 +971,7 @@ int main(int argc, char **argv) {
attach_service();
if (G.dns) { npdns_tick(G.dns); watch_dns(); }
if (G.tick % 5 == 0) scan_all(); /* in case an inotify notice was lost */
+ for (int k = 0; k < G.nsess; k++) sync_sndsvc(&G.sess[k]);
if (G.tick % 30 == 0) {
dnsmap_expire(G.names, mono());
for (int s = 0; s < G.nsess; s++)
diff --git a/server/src/netperm/np.bpf.c b/server/src/netperm/np.bpf.c
index 0e63582..feeb958 100644
--- a/server/src/netperm/np.bpf.c
+++ b/server/src/netperm/np.bpf.c
@@ -40,6 +40,8 @@
static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *)1;
static long (*bpf_map_update_elem)(void *map, const void *key, const void *value, unsigned long long flags) = (void *)2;
static unsigned long long (*bpf_ktime_get_ns)(void) = (void *)5;
+static unsigned long long (*bpf_get_current_pid_tgid)(void) = (void *)14;
+static unsigned long long (*bpf_get_current_cgroup_id)(void) = (void *)80;
static unsigned long long (*bpf_get_current_ancestor_cgroup_id)(int level) = (void *)123;
static long (*bpf_ringbuf_output)(void *ringbuf, void *data, unsigned long long size, unsigned long long flags) = (void *)130;
@@ -53,6 +55,7 @@ char np_seen[4] SEC("maps");
char np_counts[4] SEC("maps");
char np_events[4] SEC("maps");
char np_snd[4] SEC("maps");
+char np_sndsvc[4] SEC("maps");
#define AF_INET 2
#define AF_INET6 10
@@ -297,14 +300,21 @@ SEC("cgroup/dev") int np_dev(struct bpf_cgroup_dev_ctx *ctx) {
if (snd) {
/* Recording straight from the card. In the app's own scope this fails closed: an ALSA node
* the last scan didn't classify (a USB microphone plugged in since) counts as capture.
- * Outside it only the known capture nodes count (lord-netperm keeps no-mic out of the
- * user@ union today, so this is for a union that ever carries it). */
+ * Under the user@ union only the known capture nodes count, and the user's real PipeWire
+ * and WirePlumber keep them: exactly their process (tgid) in exactly their unit's cgroup
+ * (np_sndsvc, which lord-netperm fills only while that cgroup holds that one process). A
+ * process the user moves into that cgroup has another tgid, so it never matches. */
np_u32 minor = ctx->minor;
np_u32 *kind = bpf_map_lookup_elem(&np_snd, &minor);
np_u64 sid = bpf_get_current_ancestor_cgroup_id((int)cfg->level);
int own = sid && sid == cgid;
- if (kind ? *kind == NP_SND_CAPTURE : own) need = NP_R_NO_MIC;
- else return 1;
+ if (!(kind ? *kind == NP_SND_CAPTURE : own)) return 1;
+ if (!own) {
+ np_u32 tgid = (np_u32)(bpf_get_current_pid_tgid() >> 32);
+ np_u64 *svc = bpf_map_lookup_elem(&np_sndsvc, &tgid);
+ if (svc && *svc == bpf_get_current_cgroup_id()) return 1;
+ }
+ need = NP_R_NO_MIC;
}
if (!(app->flags & need)) return 1;
report_dev(cgid, NP_D_DEV, ctx->major, ctx->minor, need);
diff --git a/server/src/netperm/np_shared.h b/server/src/netperm/np_shared.h
index 8d26b4b..8f305bf 100644
--- a/server/src/netperm/np_shared.h
+++ b/server/src/netperm/np_shared.h
@@ -17,6 +17,9 @@
* np_events RINGBUF NpEvent towards lord-netperm
* np_snd HASH ALSA minor (u32) → u32 every ALSA node in /sys/class/sound: NP_SND_CAPTURE for the
* capture PCMs (/dev/snd/pcmC*D*c), NP_SND_OTHER for the rest
+ * np_sndsvc HASH tgid (u32) → cgroup id (u64) each session user's real PipeWire and WirePlumber: the one process in
+ * user@<uid>/session.slice/{pipewire,wireplumber}.service, which the user@
+ * union's no-mic leaves the capture nodes to
*
* Besides the network, the same per-app policy carries the device restrictions (docs/FIREWALL.md,
* "Linux apps"): "no camera", "no USB" and "no screen capture" (/dev/fb*) (cgroup/dev, by device
diff --git a/server/src/netperm/npload.h b/server/src/netperm/npload.h
index 86d2285..7ca29a8 100644
--- a/server/src/netperm/npload.h
+++ b/server/src/netperm/npload.h
@@ -22,7 +22,8 @@
X(np_seen, BPF_MAP_TYPE_LRU_HASH, sizeof(NpSeenKey), 8, NP_MAX_SEEN, 0) \
X(np_counts, BPF_MAP_TYPE_HASH, 8, sizeof(NpCount), NP_MAX_APPS, 0) \
X(np_events, BPF_MAP_TYPE_RINGBUF, 0, 0, NP_RINGBUF, 0) \
- X(np_snd, BPF_MAP_TYPE_HASH, 4, 4, NP_MAX_SND, 0)
+ X(np_snd, BPF_MAP_TYPE_HASH, 4, 4, NP_MAX_SND, 0) \
+ X(np_sndsvc, BPF_MAP_TYPE_HASH, 4, 8, 2 * NP_MAX_SESS, 0)
/* Each program: its section, kind and attach point. btf: the kernel function an LSM program
* hooks (its id comes from /sys/kernel/btf/vmlinux). opt: 1 = the device and Bluetooth
diff --git a/server/tests/fwlinux/mic.sh b/server/tests/fwlinux/mic.sh
index a068903..1e1460b 100755
--- a/server/tests/fwlinux/mic.sh
+++ b/server/tests/fwlinux/mic.sh
@@ -157,11 +157,29 @@ if command -v systemd-run >/dev/null && [ -d "$UM" ]; then
done
for d in session.slice/pipewire.service session.slice/wireplumber.service session.slice ""; do rmdir "$UM/fwfake.scope/$d" 2>/dev/null; done
rm -f "$f"
+ CAPN=$(ls /dev/snd/pcmC*D*c 2>/dev/null | head -1)
+ if [ -n "$CAPN" ] && [ -S /run/lordos/netperm.sock ]; then
+ got=$(timeout 10 systemd-run --user --pipe --quiet python3 -c 'import os,sys,errno
+try: os.close(os.open(sys.argv[1], os.O_RDONLY | os.O_NONBLOCK)); print("ok")
+except OSError as e: print(errno.errorcode.get(e.errno))' "$CAPN" 2>&1 | tail -1)
+ [ "$got" = EPERM ] && ok "escaped with systemd-run --user, open $CAPN → EPERM" || bad "escaped open of $CAPN → $got"
+ got=$(timeout 10 systemd-run --user --scope -q bash -c 'P=$1/user@$2.service/session.slice/pipewire.service; echo $$ > $P/cgroup.procs 2>/dev/null; exec python3 -c "import os,sys,errno
+try: os.close(os.open(sys.argv[1], os.O_RDONLY | os.O_NONBLOCK)); print(\"ok\")
+except OSError as e: print(errno.errorcode.get(e.errno))" "$3"' _ /sys/fs/cgroup/user.slice/user-$U.slice "$U" "$CAPN" 2>&1 | tail -1)
+ [ "$got" = EPERM ] && ok "moved into PipeWire's own cgroup, open $CAPN → EPERM" || bad "moved into pipewire.service, open → $got"
+ sleep 1.5 # lord-netperm sees PipeWire alone in its cgroup again
+ fi
f=$OUTD/um.raw
timeout 10 systemd-run --user --scope -q timeout 3 pw-record --rate 48000 --channels 1 --format s16 "$f" >/dev/null 2>&1
n=$(stat -c %s "$f" 2>/dev/null || echo 0)
[ "$n" -lt 4096 ] && ok "systemd-run --user pw-record: $n bytes" || bad "systemd-run --user recorded $n bytes"
fi
+restrict $APP ""
+sleep 1.5
+f=$OUTD/after.raw; timeout 3 pw-record --rate 48000 --channels 1 --format s16 "$f" >/dev/null 2>&1
+n=$(stat -c %s "$f" 2>/dev/null || echo 0)
+all=$(pw-dump 2>/dev/null | grep -c '"media.class": "Audio/Source"')
+[ "$n" -gt 20000 ] && [ "$all" -gt 0 ] && ok "once lifted, the desktop still records ($n bytes, $all microphone nodes)" || bad "the desktop lost the microphone ($n bytes, $all nodes)"
kill $GPID 2>/dev/null; wait $GPID 2>/dev/null
rm -rf "$OUTD"
fwlinux_done