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.
C cdebiandesktop-environmentebpffirewalllandlocklinuxlinux-distributionopen-sourceprivacyquickjsraspberry-pi-5reactsecuritywaylandwayland-compositor
PipeWire guard: the sound services count only at their exact unit path, alone in it, running the root-owned binary; test a nested fake unit
Federico Pereira21ac89cfe87e
diff --git a/server/src/sec/pwguard.c b/server/src/sec/pwguard.cindex 5b99dc7..33f9284 100644--- a/server/src/sec/pwguard.c+++ b/server/src/sec/pwguard.c@@ -346,28 +346,45 @@ static void evaluate(PgClient *c) {/* ---- who a client really is ---------------------------------------------------------------- */-/* Is pid the session's real pipewire-pulse? Never from a property: the executable is the root-owned- * /usr/bin/pipewire (pipewire-pulse is a link to it), the process is ours, and it runs in the user- * manager's pipewire-pulse.service. */-static int is_pulse_daemon(int pid) {- char p[64], line[512];- snprintf(p, sizeof p, "/proc/%d/exe", pid);+/* Is pid running the root-owned executable at path (same inode, not a copy)? */+static int runs_exe(int pid, const char *path) {+ char p[64];struct stat st, want;- if (stat(p, &st) || stat("/usr/bin/pipewire", &want) || st.st_dev != want.st_dev || st.st_ino != want.st_ino || want.st_uid != 0) return 0;+ snprintf(p, sizeof p, "/proc/%d/exe", pid);+ if (stat(p, &st) || stat(path, &want) || want.st_uid != 0 || (want.st_mode & 022)) return 0;+ if (st.st_dev != want.st_dev || st.st_ino != want.st_ino) return 0;snprintf(p, sizeof p, "/proc/%d", pid);- if (stat(p, &st) || st.st_uid != getuid()) return 0;+ return !stat(p, &st) && st.st_uid == getuid();+}++/* Is pid exactly in the user manager's session.slice/<unit>, alone? The whole path is compared:+ * the user owns the user@ subtree and can make a nested cgroup that merely ends in the same names. */+static int in_user_unit(int pid, const char *unit) {+ char p[64], line[1024], want[256];+ unsigned u = (unsigned)getuid();+ snprintf(want, sizeof want, "0::/user.slice/user-%u.slice/user@%u.service/session.slice/%s\n", u, u, unit);snprintf(p, sizeof p, "/proc/%d/cgroup", pid);FILE *f = fopen(p, "re");int ok = 0;- char unit[96];- snprintf(unit, sizeof unit, "/user@%u.service/session.slice/pipewire-pulse.service\n", (unsigned)getuid());- while (f && fgets(line, sizeof line, f)) {- if (strncmp(line, "0::", 3)) continue;- size_t l = strlen(line), ul = strlen(unit);- ok = l > ul && !strcmp(line + l - ul, unit);- }+ while (f && fgets(line, sizeof line, f))+ if (!strncmp(line, "0::", 3)) { ok = !strcmp(line, want); break; }if (f) fclose(f);- return ok;+ if (!ok) return 0;+ /* and the only process there: the user can also write that unit's own cgroup.procs (the+ * subtree is theirs), so a second process moved in makes neither count (fail closed) */+ snprintf(want, sizeof want, "/sys/fs/cgroup/user.slice/user-%u.slice/user@%u.service/session.slice/%s/cgroup.procs", u, u, unit);+ f = fopen(want, "re");+ int n = 0, first = -1;+ while (f && fgets(line, sizeof line, f)) { if (!n) first = atoi(line); n++; }+ if (f) fclose(f);+ return n == 1 && first == pid;+}++/* Is pid the session's real pipewire-pulse? Never from a property: the executable is the root-owned+ * /usr/bin/pipewire (pipewire-pulse is a link to it), the process is ours, and it runs in the user+ * manager's pipewire-pulse.service. */+static int is_pulse_daemon(int pid) {+ return runs_exe(pid, "/usr/bin/pipewire") && in_user_unit(pid, "pipewire-pulse.service");}/* The unix sockets of the system and their peers (sock_diag), to see who is really connected to@@ -464,19 +481,14 @@ static int in_user_manager(int pid) {FILE *f = fopen(p, "re");int r = 0;snprintf(unit, sizeof unit, "/user@%u.service/", (unsigned)getuid());- while (f && fgets(line, sizeof line, f)) {- if (strncmp(line, "0::", 3)) continue;- line[strcspn(line, "\n")] = 0;- if (!strstr(line, unit)) break;- static const char *const sound[] = { "/session.slice/pipewire.service", "/session.slice/wireplumber.service",- "/session.slice/pipewire-pulse.service", NULL };- r = 1;- size_t l = strlen(line);- for (int i = 0; sound[i]; i++) { size_t k = strlen(sound[i]); if (l > k && !strcmp(line + l - k, sound[i])) r = 0; }- break;- }+ while (f && fgets(line, sizeof line, f))+ if (!strncmp(line, "0::", 3)) { r = strstr(line, unit) != NULL; break; }if (f) fclose(f);- return r;+ if (!r) return 0;+ /* the sound services: their exact unit AND their root-owned executable */+ if (runs_exe(pid, "/usr/bin/pipewire") && (in_user_unit(pid, "pipewire.service") || in_user_unit(pid, "pipewire-pulse.service"))) return 0;+ if (runs_exe(pid, "/usr/bin/wireplumber") && in_user_unit(pid, "wireplumber.service")) return 0;+ return 1;}static void resolve(PgClient *c) {diff --git a/server/tests/fwlinux/mic.sh b/server/tests/fwlinux/mic.shindex 6095b27..1a236d1 100755--- a/server/tests/fwlinux/mic.sh+++ b/server/tests/fwlinux/mic.sh@@ -130,6 +130,23 @@ else bad "a module's capture stream stayed open"; fi[ -n "$LOOPMOD" ] && pactl unload-module "$LOOPMOD" 2>/dev/null[ -n "$NULLMOD" ] && pactl unload-module "$NULLMOD" 2>/dev/null+echo "9. escaped to the user manager while an app is restricted, posing as the sound server's unit"+UM=/sys/fs/cgroup/user.slice/user-$U.slice/user@$U.service/app.slice+if command -v systemd-run >/dev/null && [ -d "$UM" ]; then+ f=$OUTD/fakeunit.raw+ for leaf in "fwfake.scope/session.slice/pipewire.service" "fwfake.scope/session.slice/wireplumber.service"; do+ rm -f "$f"+ timeout 10 systemd-run --user --scope -q bash -c 'mkdir -p "$1/$2" && echo $$ > "$1/$2/cgroup.procs" && exec timeout 3 pw-record --rate 48000 --channels 1 --format s16 "$3"' _ "$UM" "$leaf" "$f" >/dev/null 2>&1+ n=$(stat -c %s "$f" 2>/dev/null || echo 0)+ [ "$n" -lt 4096 ] && ok "a recorder in user@…/$leaf: $n bytes" || bad "a nested $leaf recorded $n bytes"+ 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"+ 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"+fikill $GPID 2>/dev/null; wait $GPID 2>/dev/nullrm -rf "$OUTD"fwlinux_done