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.

PipeWire guard: no microphone and no camera for Linux apps, live

Federico Pereiracf88ec2151e5
diff --git a/server/Makefile b/server/Makefile
index f82389a..eb66fc6 100644
--- a/server/Makefile
+++ b/server/Makefile
@@ -51,7 +51,7 @@ QJS_OBJ := $(patsubst %,build/qjs/%.o,quickjs libregexp libunicode cutils dtoa)
MON_SRC := src/monitor/mon.c src/monitor/conn.c src/monitor/mmdb.c src/monitor/map.c src/ui/js_monitor.c
# Media (src/media, docs/MEDIA.md): only the lord-server side. Only lord-media links FFmpeg.
MEDIA_SRC := src/media/media.c src/media/mview.c src/media/capture.c src/media/annot.c src/media/maudio.c src/media/mpulse.c src/media/proto.c src/ui/js_media.c
-SEC_SRC := src/sec/json.c src/sec/sign.c src/sec/pkgstate.c src/sec/perms.c src/sec/cgroup.c src/sec/sandbox.c src/sec/brokersrv.c src/sec/brokercam.c src/sec/brokerkr.c src/sec/restrict.c
+SEC_SRC := src/sec/json.c src/sec/sign.c src/sec/pkgstate.c src/sec/perms.c src/sec/cgroup.c src/sec/sandbox.c src/sec/brokersrv.c src/sec/brokercam.c src/sec/brokerkr.c src/sec/restrict.c src/sec/appid.c src/sec/pwguard.c
# App mode: lord-server --app <id> runs an .lpk app as an isolated Wayland client (src/app)
APP_SRC := src/app/appclient.c src/app/appspawn.c src/ui/js_broker.c src/ui/js_brokercam.c
SRC := src/main.c src/kms.c src/gpu/gpu.c src/input.c src/canvas.c src/admin.c src/install/install.c src/apps.c src/lpk.c src/fsops.c src/remote/mirror.c src/remote/remote_admin.c src/users/users_admin.c src/users/homedirs.c src/keyboard.c src/ime.c src/wl/wl.c \
@@ -246,7 +246,7 @@ build/launch-cli: tests/launch_cli.c $(LAUNCH_CLI_SRC) src/apps.h $(wildcard src
@mkdir -p build
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/launch_cli.c $(LAUNCH_CLI_SRC)
# tests/notifyd_cli.c: the real notification server on a private bus, for tests/fwlinux/notify.sh
-NOTIFYD_CLI_SRC := src/hw/notifyd.c src/hw/jbuf.c src/vault/dbus.c src/sec/restrict.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c
+NOTIFYD_CLI_SRC := src/hw/notifyd.c src/sec/appid.c src/hw/jbuf.c src/vault/dbus.c src/sec/restrict.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c
test: build/notifyd-cli build/notifyd-test
build/notifyd-test: tests/notifyd_test.c $(NOTIFYD_CLI_SRC) $(wildcard src/hw/*.h src/sec/*.h src/fw/*.h src/vault/*.h)
@mkdir -p build
@@ -255,6 +255,17 @@ build/notifyd-test: tests/notifyd_test.c $(NOTIFYD_CLI_SRC) $(wildcard src/hw/*.
build/notifyd-cli: tests/notifyd_cli.c $(NOTIFYD_CLI_SRC) $(wildcard src/hw/*.h src/sec/*.h src/fw/*.h src/vault/*.h)
@mkdir -p build
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/notifyd_cli.c $(NOTIFYD_CLI_SRC)
+# tests/pwguard_cli.c: the PipeWire guard against the session's PipeWire, for tests/fwlinux/mic.sh;
+# tests/pwguard_test.c: its POD reader and node classes
+PWGUARD_SRC := src/sec/pwguard.c src/sec/appid.c src/sec/restrict.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c
+test: build/pwguard-cli build/pwguard-test
+build/pwguard-cli: tests/pwguard_cli.c $(PWGUARD_SRC) $(wildcard src/sec/*.h src/fw/*.h)
+ @mkdir -p build
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/pwguard_cli.c $(PWGUARD_SRC)
+build/pwguard-test: tests/pwguard_test.c $(PWGUARD_SRC) $(wildcard src/sec/*.h src/fw/*.h)
+ @mkdir -p build
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/pwguard_test.c $(PWGUARD_SRC)
+ ./$@ || { rm -f $@; exit 1; }
build/fw-rules-test: tests/fw_rules_test.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c $(wildcard src/fw/*.h) src/sec/json.h
@mkdir -p build
$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/fw_rules_test.c src/fw/rules.c src/fw/fwmap.c src/sec/json.c
diff --git a/server/src/hw/hw.c b/server/src/hw/hw.c
index e7ba7fc..a5f3104 100644
--- a/server/src/hw/hw.c
+++ b/server/src/hw/hw.c
@@ -3,6 +3,7 @@
#include "bt.h"
#include "jbuf.h"
#include "notifyd.h"
+#include "../sec/pwguard.h"
#include "ppd.h"
#include "sysbus.h"
#include "uevent.h"
@@ -34,6 +35,7 @@ void hw_start(const DisplayHost *h, int with_bus) {
power_read("/sys/class/power_supply", &power);
if (uevent_open()) fprintf(stderr, "hw: no kernel uevents (hotplugged displays and batteries are not followed)\n");
if (with_bus && notifyd_start(hw_emit)) fprintf(stderr, "hw: no notification server for Linux apps\n");
+ if (with_bus && pwguard_start()) fprintf(stderr, "hw: PipeWire not reachable yet (the microphone and camera restrictions of Linux apps apply once it is)\n");
if (with_bus && sysbus_start() == 0) {
bt_start(hw_emit);
ppd_start(hw_emit);
@@ -47,6 +49,7 @@ int hw_pollfds(struct pollfd *p, int max) {
if (n < max && uevent_fd() >= 0) p[n++] = (struct pollfd){ uevent_fd(), POLLIN, 0 };
if (n < max && notifyd_fd() >= 0) p[n++] = (struct pollfd){ notifyd_fd(), (short)(POLLIN | (notifyd_wants_write() ? POLLOUT : 0)), 0 };
if (n < max && sysbus_fd() >= 0) p[n++] = (struct pollfd){ sysbus_fd(), (short)(POLLIN | (sysbus_wants_write() ? POLLOUT : 0)), 0 };
+ if (n < max && pwguard_fd() >= 0) p[n++] = (struct pollfd){ pwguard_fd(), (short)(POLLIN | (pwguard_wants_write() ? POLLOUT : 0)), 0 };
return n;
}
@@ -60,12 +63,14 @@ void hw_dispatch(const struct pollfd *p, int n) {
if (m & UEV_BACKLIGHT) display_hotplug(); /* the list carries the backlight */
} else if (p[i].fd == sysbus_fd()) sysbus_dispatch();
else if (p[i].fd == notifyd_fd()) notifyd_dispatch();
+ else if (p[i].fd == pwguard_fd()) pwguard_dispatch();
}
}
void hw_events(void) {
bt_flush_events();
notifyd_tick();
+ pwguard_tick();
if (sysbus_up() && sysbus_wants_write()) dbus_flush(sysbus_conn());
}
diff --git a/server/src/hw/notifyd.c b/server/src/hw/notifyd.c
index e0a5798..813c6fc 100644
--- a/server/src/hw/notifyd.c
+++ b/server/src/hw/notifyd.c
@@ -3,6 +3,7 @@
#include "jbuf.h"
#include "../vault/dbus.h"
#include "../fw/rules.h"
+#include "../sec/appid.h"
#include "../sec/restrict.h"
#include <stdio.h>
@@ -140,31 +141,7 @@ static void on_notify(const DMsg *m) {
free(js);
}
-int notifyd_app_of_cgroup(const char *path, char *out, size_t n) {
- const char *p = strstr(path, "/lordos-session-");
- if (!p || !(p = strchr(p + 1, '/'))) return -1;
- p++;
- char scope[FW_APP_MAX + 32];
- size_t l = strcspn(p, "/\n");
- if (l < 18 || l >= sizeof scope || strncmp(p, "app-lordos-", 11) || strncmp(p + l - 6, ".scope", 6)) return -1;
- memcpy(scope, p, l);
- scope[l] = 0;
- return fw_app_canon(scope, out, n);
-}
-
-/* Firewall id of a PID's app scope, from /proc/<pid>/cgroup. 0 ok, 1 not in an app's scope, -1
- * the process is gone (or unreadable). */
-static int app_of_pid_proc(uint32_t pid, char *out, size_t n) {
- char f[64], line[1024];
- snprintf(f, sizeof f, "/proc/%u/cgroup", pid);
- FILE *fp = fopen(f, "re");
- if (!fp) return -1;
- int r = -1;
- while (fgets(line, sizeof line, fp))
- if (!strncmp(line, "0::", 3)) { r = notifyd_app_of_cgroup(line + 3, out, n) ? 1 : 0; break; }
- fclose(fp);
- return r;
-}
+static int app_of_pid_proc(uint32_t pid, char *out, size_t n) { return appid_of_pid(pid, out, n); }
void notifyd_set_app_of_pid(int (*fn)(uint32_t pid, char *out, size_t n)) { app_of_pid = fn; }
diff --git a/server/src/hw/notifyd.h b/server/src/hw/notifyd.h
index b0d9535..c9a367e 100644
--- a/server/src/hw/notifyd.h
+++ b/server/src/hw/notifyd.h
@@ -30,10 +30,6 @@ void notifyd_tick(void);
* an action was clicked. */
void notifyd_closed(uint32_t id, uint32_t reason);
void notifyd_action(uint32_t id, const char *key);
-/* Firewall id of the app whose cgroup path (the "0::" line of /proc/<pid>/cgroup, without the
- * "0::") is given: a launcher scope ".../lordos-session-<uid>/app-lordos-<id>.scope[/...]" gives
- * "desktop:<id>" (or "lpk:<id>"). 0 ok, -1 not in an app's scope. */
-int notifyd_app_of_cgroup(const char *path, char *out, size_t n);
/* Tests only: replaces the PID → firewall id lookup (/proc/<pid>/cgroup). fn returns 0 with the
* id, 1 if the PID is in no app's scope, -1 if it can't tell. */
void notifyd_set_app_of_pid(int (*fn)(uint32_t pid, char *out, size_t n));
diff --git a/server/src/sec/appid.c b/server/src/sec/appid.c
new file mode 100644
index 0000000..a1e8c5c
--- /dev/null
+++ b/server/src/sec/appid.c
@@ -0,0 +1,30 @@
+/* appid.c — app of a process by its cgroup (see appid.h). */
+#include "appid.h"
+#include "../fw/rules.h"
+
+#include <stdio.h>
+#include <string.h>
+
+int appid_of_cgroup(const char *path, char *out, size_t n) {
+ const char *p = strstr(path, "/lordos-session-");
+ if (!p || !(p = strchr(p + 1, '/'))) return -1;
+ p++;
+ char scope[FW_APP_MAX + 32];
+ size_t l = strcspn(p, "/\n");
+ if (l < 18 || l >= sizeof scope || strncmp(p, "app-lordos-", 11) || strncmp(p + l - 6, ".scope", 6)) return -1;
+ memcpy(scope, p, l);
+ scope[l] = 0;
+ return fw_app_canon(scope, out, n);
+}
+
+int appid_of_pid(unsigned pid, char *out, size_t n) {
+ char f[64], line[1024];
+ snprintf(f, sizeof f, "/proc/%u/cgroup", pid);
+ FILE *fp = fopen(f, "re");
+ if (!fp) return -1;
+ int r = -1;
+ while (fgets(line, sizeof line, fp))
+ if (!strncmp(line, "0::", 3)) { r = appid_of_cgroup(line + 3, out, n) ? 1 : 0; break; }
+ fclose(fp);
+ return r;
+}
diff --git a/server/src/sec/appid.h b/server/src/sec/appid.h
new file mode 100644
index 0000000..99d80b8
--- /dev/null
+++ b/server/src/sec/appid.h
@@ -0,0 +1,15 @@
+/* appid.h — which app a process belongs to, by its cgroup (the launcher's root-owned scopes,
+ * sec/cgroup.h): ".../lordos-session-<uid>/app-lordos-<id>.scope[/...]" → "desktop:<id>",
+ * "app-lordos-lpk-<id>.scope" → "lpk:<id>" (fw_app_canon). Used by the notification server and the
+ * PipeWire guard to apply the firewall's restrictions to Linux apps. */
+#ifndef LORD_SEC_APPID_H
+#define LORD_SEC_APPID_H
+
+#include <stddef.h>
+
+/* From a cgroup path (the "0::" line of /proc/<pid>/cgroup, without "0::"). 0 ok, -1 not an app's scope. */
+int appid_of_cgroup(const char *path, char *out, size_t n);
+/* From a PID. 0 ok, 1 the process is in no app's scope, -1 it can't be read (gone). */
+int appid_of_pid(unsigned pid, char *out, size_t n);
+
+#endif
diff --git a/server/src/sec/pwguard.c b/server/src/sec/pwguard.c
new file mode 100644
index 0000000..c90f8f5
--- /dev/null
+++ b/server/src/sec/pwguard.c
@@ -0,0 +1,467 @@
+/* pwguard.c — "no microphone" / "no camera" for Linux apps inside PipeWire (see pwguard.h).
+ *
+ * A minimal client of PipeWire's native protocol (version 3): each message is a 16-byte header
+ * (object id; opcode << 24 | payload size; sequence; number of fds) and a SPA POD Struct.
+ * We only use: core.hello, client.update_properties, core.get_registry, core.pong,
+ * registry.bind (clients only), registry.destroy (a restricted app's capture streams) and
+ * client.update_permissions; and we read core.ping/error/remove_id, registry.global /
+ * global_remove and client.info. Everything is non-blocking: lord-server never waits for it. */
+#include "pwguard.h"
+#include "appid.h"
+#include "restrict.h"
+#include "../fw/rules.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <time.h>
+#include <unistd.h>
+
+#define MAX_CLIENTS 256
+#define MAX_NODES 1024
+#define MAX_PORTS 4096
+#define MAX_PROXY (MAX_CLIENTS + 8)
+#define IN_MAX (1u << 20)
+#define OUT_MAX (1u << 20)
+
+/* PipeWire's permission bits (pw_permission): read, write, execute, link, metadata */
+#define PW_R 0400
+#define PW_W 0200
+#define PW_X 0100
+#define PW_L 0020
+#define PW_M 0010
+#define PW_ALL (PW_R | PW_W | PW_X | PW_L | PW_M)
+
+#define SPA_INT 4
+#define SPA_LONG 5
+#define SPA_STRING 8
+#define SPA_STRUCT 14
+
+typedef struct {
+ uint32_t id, proxy; /* global id; our bound proxy (0 = not bound yet) */
+ int pid_sec, pid_app, pulse;/* pipewire.sec.pid, application.process.id, a pipewire-pulse client */
+ int resolved; /* app worked out (needs the client's info) */
+ char app[FW_APP_MAX];
+ uint32_t want, applied; /* restrictions (FW_R_NO_MIC | FW_R_NO_CAMERA) to apply / applied */
+ int live;
+} PgClient;
+
+typedef struct { uint32_t id, client, flag, stream; int live; } PgNode; /* flag: capture node; stream: capture stream */
+typedef struct { uint32_t id, node; int live; } PgPort;
+
+static struct {
+ int fd;
+ uint8_t in[IN_MAX];
+ size_t inlen;
+ uint8_t *out;
+ size_t outlen;
+ uint32_t seq;
+ PgClient cl[MAX_CLIENTS];
+ PgNode node[MAX_NODES];
+ PgPort port[MAX_PORTS];
+ uint8_t proxy_used[MAX_PROXY];
+ double next_try, next_tick;
+ int up;
+} P = { .fd = -1 };
+
+static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
+
+/* ---- SPA POD: reading ---------------------------------------------------------------------- */
+
+static uint32_t rd32(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v; }
+
+/* The pod at p (size + type + body), within n bytes. */
+static int pod_at(const uint8_t *p, size_t n, PwPod *out, uint32_t *type) {
+ if (n < 8) return -1;
+ uint32_t size = rd32(p), t = rd32(p + 4);
+ if (size > n - 8) return -1;
+ out->p = p;
+ out->n = 8 + size;
+ if (type) *type = t;
+ return 0;
+}
+
+int pwpod_struct_field(PwPod s, int index, PwPod *out) {
+ uint32_t t;
+ PwPod whole;
+ if (pod_at(s.p, s.n, &whole, &t) || t != SPA_STRUCT) return -1;
+ size_t off = 8, end = whole.n;
+ for (int i = 0; off < end; i++) {
+ PwPod f;
+ if (pod_at(s.p + off, end - off, &f, NULL)) return -1;
+ if (i == index) { *out = f; return 0; }
+ size_t adv = (f.n + 7) & ~(size_t)7;
+ if (adv > end - off) return -1;
+ off += adv;
+ }
+ return -1;
+}
+
+int pwpod_int(PwPod p, int32_t *v) {
+ if (p.n < 12 || rd32(p.p + 4) != SPA_INT || rd32(p.p) != 4) return -1;
+ memcpy(v, p.p + 8, 4);
+ return 0;
+}
+
+int pwpod_string(PwPod p, const char **s) {
+ uint32_t size = p.n >= 8 ? rd32(p.p) : 0;
+ if (p.n < 9 || rd32(p.p + 4) != SPA_STRING || !size || p.p[8 + size - 1]) return -1;
+ *s = (const char *)p.p + 8;
+ return 0;
+}
+
+/* A dict (Struct: Int n, then String key, String value...) → the value of key, or NULL. */
+static const char *dict_get(PwPod d, const char *key) {
+ PwPod f;
+ int32_t n;
+ if (pwpod_struct_field(d, 0, &f) || pwpod_int(f, &n) || n < 0 || n > 4096) return NULL;
+ for (int i = 0; i < n; i++) {
+ PwPod k, v;
+ const char *ks, *vs;
+ if (pwpod_struct_field(d, 1 + 2 * i, &k) || pwpod_string(k, &ks)) return NULL;
+ if (strcmp(ks, key)) continue;
+ if (pwpod_struct_field(d, 2 + 2 * i, &v) || pwpod_string(v, &vs)) return NULL;
+ return vs;
+ }
+ return NULL;
+}
+
+/* ---- SPA POD: writing ---------------------------------------------------------------------- */
+
+typedef struct { uint8_t b[16384]; size_t n; int err; } Pb;
+
+static void pb_raw(Pb *b, const void *p, size_t n) {
+ if (b->err || n > sizeof b->b - b->n) { b->err = 1; return; }
+ memcpy(b->b + b->n, p, n);
+ b->n += n;
+}
+static void pb_pad(Pb *b) { static const uint8_t z[8] = { 0 }; size_t r = b->n % 8; if (r) pb_raw(b, z, 8 - r); }
+static void pb_hdr(Pb *b, uint32_t size, uint32_t type) { pb_raw(b, &size, 4); pb_raw(b, &type, 4); }
+static void pb_int(Pb *b, int32_t v) { pb_hdr(b, 4, SPA_INT); pb_raw(b, &v, 4); pb_pad(b); }
+static void pb_str(Pb *b, const char *s) { size_t l = strlen(s) + 1; pb_hdr(b, (uint32_t)l, SPA_STRING); pb_raw(b, s, l); pb_pad(b); }
+static size_t pb_begin(Pb *b) { size_t at = b->n; pb_hdr(b, 0, SPA_STRUCT); return at; }
+static void pb_end(Pb *b, size_t at) { if (b->err) return; uint32_t size = (uint32_t)(b->n - at - 8); memcpy(b->b + at, &size, 4); }
+
+/* ---- connection ---------------------------------------------------------------------------- */
+
+static void drop(void) {
+ if (P.fd >= 0) close(P.fd);
+ P.fd = -1;
+ P.up = 0;
+ P.inlen = P.outlen = 0;
+ memset(P.cl, 0, sizeof P.cl);
+ memset(P.node, 0, sizeof P.node);
+ memset(P.port, 0, sizeof P.port);
+ memset(P.proxy_used, 0, sizeof P.proxy_used);
+ P.next_try = now_ms() + 2000;
+}
+
+static void flush_out(void) {
+ while (P.fd >= 0 && P.outlen) {
+ ssize_t w = send(P.fd, P.out, P.outlen, MSG_NOSIGNAL | MSG_DONTWAIT);
+ if (w < 0 && errno == EINTR) continue;
+ if (w < 0 && errno == EAGAIN) return;
+ if (w <= 0) { drop(); return; }
+ memmove(P.out, P.out + w, P.outlen - (size_t)w);
+ P.outlen -= (size_t)w;
+ }
+}
+
+static void send_msg(uint32_t id, uint32_t opcode, const Pb *b) {
+ if (P.fd < 0 || b->err) return;
+ if (!P.out && !(P.out = malloc(OUT_MAX))) return;
+ if (P.outlen + 16 + b->n > OUT_MAX) { drop(); return; } /* PipeWire isn't reading: start over */
+ uint32_t h[4] = { id, opcode << 24 | (uint32_t)(b->n & 0xffffff), P.seq++, 0 };
+ memcpy(P.out + P.outlen, h, 16);
+ memcpy(P.out + P.outlen + 16, b->b, b->n);
+ P.outlen += 16 + b->n;
+}
+
+static uint32_t proxy_new(void) {
+ for (uint32_t i = 3; i < MAX_PROXY; i++) if (!P.proxy_used[i]) { P.proxy_used[i] = 1; return i; }
+ return 0;
+}
+
+int pwguard_start(void) {
+ if (P.fd >= 0) return 0;
+ const char *rt = getenv("XDG_RUNTIME_DIR");
+ if (!rt || rt[0] != '/') return -1;
+ struct sockaddr_un sa = { .sun_family = AF_UNIX };
+ if ((size_t)snprintf(sa.sun_path, sizeof sa.sun_path, "%s/pipewire-0-manager", rt) >= sizeof sa.sun_path) return -1;
+ int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ if (fd < 0) return -1;
+ if (connect(fd, (struct sockaddr *)&sa, sizeof sa) && errno != EINPROGRESS) { close(fd); P.next_try = now_ms() + 2000; return -1; }
+ P.fd = fd;
+ P.seq = 0;
+ P.proxy_used[0] = P.proxy_used[1] = P.proxy_used[2] = 1;
+ Pb b = { .n = 0 };
+ size_t s = pb_begin(&b); pb_int(&b, 3); pb_end(&b, s);
+ send_msg(0, 1, &b); /* core.hello(3) */
+ b.n = 0;
+ s = pb_begin(&b);
+ size_t d = pb_begin(&b);
+ pb_int(&b, 2);
+ pb_str(&b, "application.name"); pb_str(&b, "LordOS privacy guard");
+ pb_str(&b, "media.category"); pb_str(&b, "Manager");
+ pb_end(&b, d);
+ pb_end(&b, s);
+ send_msg(1, 2, &b); /* client.update_properties */
+ b.n = 0;
+ s = pb_begin(&b); pb_int(&b, 3); pb_int(&b, 2); pb_end(&b, s);
+ send_msg(0, 5, &b); /* core.get_registry(3, new id 2) */
+ flush_out();
+ P.up = 1;
+ return 0;
+}
+
+int pwguard_fd(void) { return P.fd; }
+int pwguard_wants_write(void) { return P.fd >= 0 && P.outlen > 0; }
+
+/* ---- the policy ---------------------------------------------------------------------------- */
+
+uint32_t pwguard_capture_flag(const char *mc) {
+ if (!mc) return 0;
+ if (!strncmp(mc, "Audio/Source", 12) || !strcmp(mc, "Audio/Duplex")) return FW_R_NO_MIC;
+ if (!strncmp(mc, "Video/Source", 12)) return FW_R_NO_CAMERA;
+ return 0;
+}
+
+uint32_t pwguard_stream_flag(const char *mc) {
+ if (!mc) return 0;
+ if (!strcmp(mc, "Stream/Input/Audio")) return FW_R_NO_MIC;
+ if (!strcmp(mc, "Stream/Input/Video")) return FW_R_NO_CAMERA;
+ return 0;
+}
+
+static PgClient *client_by_id(uint32_t id) {
+ for (int i = 0; i < MAX_CLIENTS; i++) if (P.cl[i].live && P.cl[i].id == id) return &P.cl[i];
+ return NULL;
+}
+
+static PgNode *node_by_id(uint32_t id) {
+ for (int i = 0; i < MAX_NODES; i++) if (P.node[i].live && P.node[i].id == id) return &P.node[i];
+ return NULL;
+}
+
+/* What the client's app is restricted to, among what PipeWire hands out. */
+static uint32_t client_restrictions(PgClient *c) {
+ if (!c->resolved) return 0;
+ if (!c->app[0]) return 0;
+ uint32_t f = 0;
+ if (restrict_blocks(c->app, FW_R_NO_MIC)) f |= FW_R_NO_MIC;
+ if (restrict_blocks(c->app, FW_R_NO_CAMERA)) f |= FW_R_NO_CAMERA;
+ return f;
+}
+
+/* Sends the whole permission list of a restricted client (or the restore list when its
+ * restriction is lifted): hidden = the capture nodes and ports of what it is restricted to, and
+ * every other client. */
+static void apply_permissions(PgClient *c, uint32_t flags) {
+ if (!c->proxy) return;
+ Pb b = { .n = 0 };
+ size_t s = pb_begin(&b);
+ size_t count_at = b.n;
+ pb_int(&b, 0); /* patched below */
+ int32_t n = 0;
+ uint32_t perm_clients = flags ? 0 : PW_ALL;
+ for (int i = 0; i < MAX_NODES && !b.err; i++) {
+ PgNode *nd = &P.node[i];
+ if (!nd->live || !nd->flag) continue;
+ uint32_t want = (flags & nd->flag) ? 0 : PW_ALL; /* restored when lifted */
+ pb_int(&b, (int32_t)nd->id); pb_int(&b, (int32_t)want); n++;
+ for (int k = 0; k < MAX_PORTS && !b.err; k++)
+ if (P.port[k].live && P.port[k].node == nd->id) { pb_int(&b, (int32_t)P.port[k].id); pb_int(&b, (int32_t)want); n++; }
+ }
+ for (int i = 0; i < MAX_CLIENTS && !b.err; i++) {
+ PgClient *o = &P.cl[i];
+ if (!o->live || o == c) continue;
+ pb_int(&b, (int32_t)o->id); pb_int(&b, (int32_t)perm_clients); n++;
+ }
+ pb_end(&b, s);
+ if (b.err) { fprintf(stderr, "pwguard: permission list too long for %s\n", c->app); return; }
+ memcpy(b.b + count_at + 8, &n, 4);
+ send_msg(c->proxy, 4, &b); /* client.update_permissions */
+ c->applied = flags;
+}
+
+/* Destroys the capture streams of a restricted client (the app gets its stream closed). */
+static void kill_streams(void) {
+ for (int i = 0; i < MAX_NODES; i++) {
+ PgNode *nd = &P.node[i];
+ if (!nd->live || !nd->stream) continue;
+ PgClient *c = client_by_id(nd->client);
+ if (!c || !(c->want & nd->stream)) continue;
+ Pb b = { .n = 0 };
+ size_t s = pb_begin(&b); pb_int(&b, (int32_t)nd->id); pb_end(&b, s);
+ send_msg(2, 2, &b); /* registry.destroy */
+ fprintf(stderr, "pwguard: %s is restricted (%s): its capture stream %u was closed\n", c->app,
+ nd->stream == FW_R_NO_MIC ? "no-mic" : "no-camera", nd->id);
+ nd->stream = 0; /* once; the global_remove follows */
+ }
+}
+
+static void evaluate(PgClient *c) {
+ c->want = client_restrictions(c);
+ if (c->want != c->applied) apply_permissions(c, c->want);
+}
+
+static void resolve(PgClient *c) {
+ int pid = c->pulse ? c->pid_app : c->pid_sec;
+ c->app[0] = 0;
+ if (pid <= 0 || appid_of_pid((unsigned)pid, c->app, sizeof c->app)) c->app[0] = 0;
+ c->resolved = 1;
+}
+
+/* ---- events -------------------------------------------------------------------------------- */
+
+static void on_global(PwPod body) {
+ PwPod f;
+ int32_t id;
+ const char *type;
+ if (pwpod_struct_field(body, 0, &f) || pwpod_int(f, &id) || pwpod_struct_field(body, 2, &f) || pwpod_string(f, &type)) return;
+ PwPod props;
+ int has_props = !pwpod_struct_field(body, 4, &props);
+ if (!strcmp(type, "PipeWire:Interface:Client")) {
+ PgClient *c = NULL;
+ for (int i = 0; i < MAX_CLIENTS && !c; i++) if (!P.cl[i].live) c = &P.cl[i];
+ if (!c) return;
+ memset(c, 0, sizeof *c);
+ c->live = 1;
+ c->id = (uint32_t)id;
+ uint32_t px = proxy_new();
+ if (px) {
+ Pb b = { .n = 0 };
+ size_t s = pb_begin(&b); pb_int(&b, id); pb_str(&b, "PipeWire:Interface:Client"); pb_int(&b, 3); pb_int(&b, (int32_t)px); pb_end(&b, s);
+ send_msg(2, 1, &b); /* registry.bind → client.info follows */
+ c->proxy = px;
+ }
+ /* the others already restricted must not see this one either */
+ for (int i = 0; i < MAX_CLIENTS; i++)
+ if (P.cl[i].live && &P.cl[i] != c && P.cl[i].applied) apply_permissions(&P.cl[i], P.cl[i].applied);
+ } else if (!strcmp(type, "PipeWire:Interface:Node") && has_props) {
+ const char *mc = dict_get(props, "media.class"), *cid = dict_get(props, "client.id");
+ uint32_t flag = pwguard_capture_flag(mc), stream = pwguard_stream_flag(mc);
+ if (!flag && !stream) return;
+ PgNode *nd = NULL;
+ for (int i = 0; i < MAX_NODES && !nd; i++) if (!P.node[i].live) nd = &P.node[i];
+ if (!nd) return;
+ nd->live = 1;
+ nd->id = (uint32_t)id;
+ nd->flag = flag;
+ nd->stream = stream;
+ nd->client = cid ? (uint32_t)strtoul(cid, NULL, 10) : 0;
+ if (flag) /* a new microphone or camera: hidden from the restricted at once */
+ for (int i = 0; i < MAX_CLIENTS; i++) if (P.cl[i].live && (P.cl[i].applied & flag)) apply_permissions(&P.cl[i], P.cl[i].applied);
+ if (stream) kill_streams();
+ } else if (!strcmp(type, "PipeWire:Interface:Port") && has_props) {
+ const char *nid = dict_get(props, "node.id");
+ if (!nid) return;
+ uint32_t node = (uint32_t)strtoul(nid, NULL, 10);
+ PgNode *nd = node_by_id(node);
+ if (!nd || !nd->flag) return;
+ PgPort *pt = NULL;
+ for (int i = 0; i < MAX_PORTS && !pt; i++) if (!P.port[i].live) pt = &P.port[i];
+ if (!pt) return;
+ pt->live = 1;
+ pt->id = (uint32_t)id;
+ pt->node = node;
+ for (int i = 0; i < MAX_CLIENTS; i++) if (P.cl[i].live && (P.cl[i].applied & nd->flag)) apply_permissions(&P.cl[i], P.cl[i].applied);
+ }
+}
+
+static void on_global_remove(uint32_t id) {
+ for (int i = 0; i < MAX_CLIENTS; i++) if (P.cl[i].live && P.cl[i].id == id) P.cl[i].live = 0; /* its proxy: remove_id */
+ for (int i = 0; i < MAX_NODES; i++) if (P.node[i].live && P.node[i].id == id) P.node[i].live = 0;
+ for (int i = 0; i < MAX_PORTS; i++) if (P.port[i].live && P.port[i].id == id) P.port[i].live = 0;
+}
+
+/* client.info(id, change_mask, props): who it is */
+static void on_client_info(uint32_t proxy, PwPod body) {
+ PgClient *c = NULL;
+ for (int i = 0; i < MAX_CLIENTS && !c; i++) if (P.cl[i].live && P.cl[i].proxy == proxy) c = &P.cl[i];
+ PwPod props;
+ if (!c || pwpod_struct_field(body, 2, &props)) return;
+ const char *sp = dict_get(props, "pipewire.sec.pid"), *ap = dict_get(props, "application.process.id");
+ c->pid_sec = sp ? atoi(sp) : 0;
+ c->pid_app = ap ? atoi(ap) : 0;
+ c->pulse = dict_get(props, "pulse.server.type") != NULL;
+ resolve(c);
+ evaluate(c);
+ kill_streams();
+}
+
+static void on_message(uint32_t id, uint32_t opcode, PwPod body) {
+ PwPod f;
+ int32_t a, b;
+ if (id == 0 && opcode == 2) { /* core.ping(id, seq) → pong */
+ if (pwpod_struct_field(body, 0, &f) || pwpod_int(f, &a) || pwpod_struct_field(body, 1, &f) || pwpod_int(f, &b)) return;
+ Pb p = { .n = 0 };
+ size_t s = pb_begin(&p); pb_int(&p, a); pb_int(&p, b); pb_end(&p, s);
+ send_msg(0, 3, &p);
+ } else if (id == 0 && opcode == 3) { /* core.error */
+ const char *msg = "";
+ if (!pwpod_struct_field(body, 3, &f)) pwpod_string(f, &msg);
+ fprintf(stderr, "pwguard: PipeWire error: %s\n", msg);
+ } else if (id == 0 && opcode == 4) { /* core.remove_id */
+ if (!pwpod_struct_field(body, 0, &f) && !pwpod_int(f, &a) && a > 2 && a < MAX_PROXY) P.proxy_used[a] = 0;
+ } else if (id == 2 && opcode == 0) on_global(body);
+ else if (id == 2 && opcode == 1) {
+ if (!pwpod_struct_field(body, 0, &f) && !pwpod_int(f, &a)) on_global_remove((uint32_t)a);
+ } else if (id > 2 && opcode == 0) on_client_info(id, body); /* our bound clients: info */
+}
+
+void pwguard_dispatch(void) {
+ if (P.fd < 0) return;
+ flush_out();
+ for (;;) {
+ if (P.fd < 0) return;
+ char cbuf[CMSG_SPACE(28 * sizeof(int))];
+ struct iovec io = { P.in + P.inlen, sizeof P.in - P.inlen };
+ struct msghdr mh = { .msg_iov = &io, .msg_iovlen = 1, .msg_control = cbuf, .msg_controllen = sizeof cbuf };
+ ssize_t n = recvmsg(P.fd, &mh, MSG_DONTWAIT | MSG_CMSG_CLOEXEC);
+ if (n < 0 && errno == EINTR) continue;
+ if (n < 0 && errno == EAGAIN) break;
+ if (n <= 0) { drop(); return; }
+ for (struct cmsghdr *cm = CMSG_FIRSTHDR(&mh); cm; cm = CMSG_NXTHDR(&mh, cm)) /* we never use fds: close them */
+ if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SCM_RIGHTS) {
+ int *fds = (int *)CMSG_DATA(cm);
+ size_t k = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+ for (size_t i = 0; i < k; i++) close(fds[i]);
+ }
+ P.inlen += (size_t)n;
+ size_t off = 0;
+ while (P.inlen - off >= 16) {
+ uint32_t id = rd32(P.in + off), op = rd32(P.in + off + 4);
+ uint32_t size = op & 0xffffff, opcode = op >> 24;
+ if (size > IN_MAX - 16) { drop(); return; }
+ if (P.inlen - off < 16 + (size_t)size) break;
+ PwPod body = { P.in + off + 16, size };
+ on_message(id, opcode, body);
+ if (P.fd < 0) return;
+ off += 16 + size;
+ }
+ memmove(P.in, P.in + off, P.inlen - off);
+ P.inlen -= off;
+ if (P.inlen == sizeof P.in) { drop(); return; }
+ }
+ flush_out();
+}
+
+void pwguard_tick(void) {
+ double t = now_ms();
+ if (P.fd < 0) {
+ if (t >= P.next_try) pwguard_start();
+ return;
+ }
+ if (t < P.next_tick) return;
+ P.next_tick = t + 1000;
+ /* the rules may have changed (Settings): apply or lift */
+ for (int i = 0; i < MAX_CLIENTS; i++) if (P.cl[i].live) evaluate(&P.cl[i]);
+ kill_streams();
+ flush_out();
+}
diff --git a/server/src/sec/pwguard.h b/server/src/sec/pwguard.h
new file mode 100644
index 0000000..11f50ca
--- /dev/null
+++ b/server/src/sec/pwguard.h
@@ -0,0 +1,47 @@
+/* pwguard.h — the firewall's "no microphone" and "no camera" for Linux apps inside PipeWire
+ * (docs/FIREWALL.md, "Linux apps").
+ *
+ * Linux apps talk to PipeWire directly (natively, or through pipewire-pulse), so the kernel's
+ * device rules don't cover the microphone (PipeWire opens the sound card, not the app) nor a
+ * camera that PipeWire serves. lord-server keeps one connection of its own to the session's
+ * PipeWire, on the manager socket ($XDG_RUNTIME_DIR/pipewire-0-manager), with our own
+ * implementation of the native protocol (no libpipewire). For every PipeWire client it works out
+ * the app (the PID's cgroup → the launcher's scope → desktop:<id>) and, if that app is
+ * restricted (sec/restrict.h):
+ * - hides from that client every capture node: Audio/Source* and Audio/Duplex for "no
+ * microphone", Video/Source for "no camera", and their ports (permission 0, so it can neither
+ * see nor link them), and every other client (so it can't change anyone's permissions);
+ * - destroys every capture stream that client creates (Stream/Input/Audio, Stream/Input/Video),
+ * as soon as it appears.
+ * The rules are lord.fw's store (the one Settings edits), read every second: a restriction set or
+ * lifted on a running app applies within a second, without restarting it. Nothing is written to a
+ * file the apps could change.
+ *
+ * Who a client is: PipeWire's own pipewire.sec.pid (from the socket's credentials) for native
+ * clients. Apps using the PulseAudio API all reach PipeWire through pipewire-pulse's process, so
+ * for those the PID is the application.process.id the app itself reported to pipewire-pulse (see
+ * the documentation for what that means). */
+#ifndef LORD_SEC_PWGUARD_H
+#define LORD_SEC_PWGUARD_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+int pwguard_start(void); /* 0 connected; retried by pwguard_tick if PipeWire isn't up */
+int pwguard_fd(void); /* -1 if not connected */
+int pwguard_wants_write(void);
+void pwguard_dispatch(void);
+void pwguard_tick(void); /* every pass of the main loop; does work once a second */
+
+/* ---- for tests (tests/pwguard_test.c) ---- */
+/* A SPA POD reader over buf: the n-th field of a Struct and its kind. */
+typedef struct { const uint8_t *p; size_t n; } PwPod;
+int pwpod_struct_field(PwPod s, int index, PwPod *out); /* 0 ok */
+int pwpod_int(PwPod p, int32_t *v);
+int pwpod_string(PwPod p, const char **s);
+/* Which restriction makes a node a capture node to hide ("Audio/Source" → FW_R_NO_MIC...), 0 none. */
+uint32_t pwguard_capture_flag(const char *media_class);
+/* The same for a stream an app creates to capture ("Stream/Input/Audio" → FW_R_NO_MIC). */
+uint32_t pwguard_stream_flag(const char *media_class);
+
+#endif
diff --git a/server/tests/fwlinux/mic.sh b/server/tests/fwlinux/mic.sh
new file mode 100755
index 0000000..b7782ee
--- /dev/null
+++ b/server/tests/fwlinux/mic.sh
@@ -0,0 +1,81 @@
+#!/bin/bash
+# mic.sh — "no microphone" for Linux apps inside PipeWire (src/sec/pwguard.c): the restricted
+# app's capture streams are closed and the capture nodes hidden from it, live. Runs the real guard
+# (build/pwguard-cli) against the session's PipeWire and records from inside an app scope with
+# pw-record (native PipeWire) and parecord (PulseAudio API), and arecord (ALSA → PipeWire) if
+# present. Needs an Audio/Source node (any, even a virtual sound card's).
+#
+# bash server/tests/fwlinux/mic.sh
+. "$(dirname "$0")/lib.sh"
+D=$(cd "$(dirname "$0")" && pwd)
+CLI=$D/../../build/pwguard-cli
+[ -x "$CLI" ] || { echo "missing build/pwguard-cli (make -C server test)"; exit 2; }
+export XDG_RUNTIME_DIR=/run/user/$U
+[ -S "$XDG_RUNTIME_DIR/pipewire-0-manager" ] || { echo "skipped: needs the session's PipeWire"; exit 0; }
+APP=desktop:fwmic
+SC=$(scope fwmic)
+OUTD=$(mktemp -d); chmod 755 "$OUTD"
+GLOG=$OUTD/guard.log
+restrict $APP ""
+"$CLI" 60 > "$GLOG" 2>&1 &
+GPID=$!
+sleep 1
+# rec <tool> <seconds>: records inside the scope; prints the bytes of audio it got
+rec() {
+ local f=$OUTD/$1.raw
+ rm -f "$f"
+ case $1 in
+ pw-record) in_scope "$SC" timeout "$2" pw-record --rate 48000 --channels 1 --format s16 "$f" >/dev/null 2>&1 ;;
+ parecord) in_scope "$SC" timeout "$2" parecord --raw --rate=48000 --channels=1 --format=s16le "$f" >/dev/null 2>&1 ;;
+ arecord) in_scope "$SC" timeout "$2" arecord -q -D pipewire -f S16_LE -r 48000 -c 1 -t raw "$f" >/dev/null 2>&1 ;;
+ esac
+ stat -c %s "$f" 2>/dev/null || echo 0
+}
+TOOLS="pw-record parecord"
+command -v arecord >/dev/null && [ -e /usr/share/alsa/alsa.conf.d/50-pipewire.conf ] && TOOLS="$TOOLS arecord"
+
+echo "1. not restricted: each tool records"
+for t in $TOOLS; do n=$(rec $t 3); [ "$n" -gt 20000 ] && ok "$t recorded $n bytes" || bad "$t recorded only $n bytes"; done
+
+echo "2. no-mic: nothing is recorded"
+restrict $APP no-mic
+sleep 1.5 # the guard rereads the rules every second
+for t in $TOOLS; do n=$(rec $t 3); [ "$n" -lt 4096 ] && ok "$t got $n bytes" || bad "$t still recorded $n bytes"; done
+grep -q "fwmic is restricted (no-mic)" "$GLOG" && ok "the guard closed the capture stream(s)" || bad "no closing in the guard's log"
+seen=$(in_scope "$SC" pw-dump 2>/dev/null | grep -c '"media.class": "Audio/Source"')
+all=$(pw-dump 2>/dev/null | grep -c '"media.class": "Audio/Source"')
+[ "$seen" = 0 ] && [ "$all" -gt 0 ] && ok "the app sees no microphone node ($all exist)" || bad "the app sees $seen of $all microphone nodes"
+python3 -c "import wave,sys; w=wave.open(sys.argv[1],'wb'); w.setnchannels(1); w.setsampwidth(2); w.setframerate(48000); w.writeframes(bytes(96000)); w.close()" "$OUTD/silence.wav"; chmod 644 "$OUTD/silence.wav"
+in_scope "$SC" timeout 5 pw-play "$OUTD/silence.wav" >/dev/null 2>&1 && ok "playback still works for the app" || bad "playback failed"
+
+echo "3. live: a recording that is running is cut when the restriction is set"
+restrict $APP ""
+sleep 1.5
+f=$OUTD/live.raw
+in_scope "$SC" timeout 8 pw-record --rate 48000 --channels 1 --format s16 "$f" >/dev/null 2>&1 &
+RP=$!
+sleep 2.5
+before=$(stat -c %s "$f" 2>/dev/null || echo 0)
+restrict $APP no-mic
+sleep 3
+mid=$(stat -c %s "$f" 2>/dev/null || echo 0)
+sleep 2
+after=$(stat -c %s "$f" 2>/dev/null || echo 0)
+wait $RP 2>/dev/null
+[ "$before" -gt 20000 ] && ok "it was recording ($before bytes)" || bad "it wasn't recording ($before)"
+[ "$after" = "$mid" ] && ok "it stopped growing once restricted ($mid → $after)" || bad "it kept recording ($mid → $after)"
+
+echo "4. lifted: records again"
+restrict $APP ""
+sleep 1.5
+n=$(rec pw-record 3); [ "$n" -gt 20000 ] && ok "pw-record recorded $n bytes" || bad "pw-record recorded only $n bytes"
+
+echo "5. outside the app's scope while it is restricted"
+restrict $APP no-mic
+sleep 1.5
+f=$OUTD/out.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)
+[ "$n" -gt 20000 ] && ok "another program records ($n bytes)" || bad "another program got $n bytes"
+kill $GPID 2>/dev/null; wait $GPID 2>/dev/null
+rm -rf "$OUTD"
+fwlinux_done
diff --git a/server/tests/pwguard_cli.c b/server/tests/pwguard_cli.c
new file mode 100644
index 0000000..15dc22c
--- /dev/null
+++ b/server/tests/pwguard_cli.c
@@ -0,0 +1,47 @@
+/* pwguard_cli.c — the PipeWire guard (src/sec/pwguard.c) outside the desktop, for
+ * tests/fwlinux/mic.sh.
+ *
+ * XDG_RUNTIME_DIR=/run/user/<uid> build/pwguard-cli <seconds>
+ *
+ * Rereads ~/.config/lordos/firewall.json every second into the store lord.fw uses
+ * (sec/restrict.h), the way Settings changes it in a session, and runs the guard against the
+ * session's PipeWire. A test tool: never installed. */
+#include "sec/pwguard.h"
+#include "fw/rules.h"
+#include "sec/broker.h"
+#include "sec/perms.h"
+#include "sec/restrict.h"
+
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+void brokersrv_revoke(const char *app, const char *perm) { (void)app; (void)perm; }
+PermState perm_get(const char *app_id, Perm p) { (void)app_id; (void)p; return PERM_ASK; }
+
+int main(int argc, char **argv) {
+ int secs = argc > 1 ? atoi(argv[1]) : 10;
+ const char *home = getenv("HOME");
+ char path[4096];
+ snprintf(path, sizeof path, "%s/.config/lordos/firewall.json", home ? home : "/nonexistent");
+ FwStore *cur = NULL;
+ if (pwguard_start()) { fprintf(stderr, "pwguard-cli: no PipeWire manager socket\n"); return 1; }
+ printf("ready\n");
+ fflush(stdout);
+ time_t end = time(NULL) + secs, last = 0;
+ while (time(NULL) < end) {
+ if (time(NULL) != last) {
+ last = time(NULL);
+ FwStore *s = fw_store_new();
+ char err[200];
+ if (s && fw_load(s, path, err, sizeof err) >= 0) { restrict_set_store(s); fw_store_free(cur); cur = s; }
+ else fw_store_free(s);
+ }
+ struct pollfd p = { .fd = pwguard_fd(), .events = POLLIN | (pwguard_wants_write() ? POLLOUT : 0) };
+ if (p.fd >= 0 && poll(&p, 1, 100) > 0) pwguard_dispatch();
+ else if (p.fd < 0) { struct timespec t = { 0, 100000000 }; nanosleep(&t, NULL); }
+ pwguard_tick();
+ }
+ return 0;
+}
diff --git a/server/tests/pwguard_test.c b/server/tests/pwguard_test.c
new file mode 100644
index 0000000..a50fff8
--- /dev/null
+++ b/server/tests/pwguard_test.c
@@ -0,0 +1,59 @@
+/* pwguard_test.c — the PipeWire guard's pieces that don't need PipeWire (src/sec/pwguard.c): the
+ * SPA POD reader on well-formed, truncated and lying input, and which nodes and streams each
+ * restriction covers. The guard against a real PipeWire: tests/fwlinux/mic.sh. */
+#include "sec/pwguard.h"
+#include "fw/rules.h"
+#include "sec/broker.h"
+#include "sec/perms.h"
+
+#include <stdio.h>
+#include <string.h>
+
+void brokersrv_revoke(const char *app, const char *perm) { (void)app; (void)perm; }
+PermState perm_get(const char *app_id, Perm p) { (void)app_id; (void)p; return PERM_ASK; }
+
+static int fails, total;
+#define CHECK(c) do { total++; if (!(c)) { fails++; fprintf(stderr, "%s:%d: failed: %s\n", __FILE__, __LINE__, #c); } } while (0)
+
+static size_t put(uint8_t *b, size_t o, uint32_t v) { memcpy(b + o, &v, 4); return o + 4; }
+
+int main(void) {
+ /* Struct { Int 7, String "Audio/Source", Int -1 } */
+ uint8_t b[128] = { 0 };
+ size_t o = 8;
+ o = put(b, o, 4); o = put(b, o, 4); o = put(b, o, 7); o += 4;
+ o = put(b, o, 13); o = put(b, o, 8); memcpy(b + o, "Audio/Source", 13); o += 16;
+ o = put(b, o, 4); o = put(b, o, 4); o = put(b, o, 0xffffffffu); o += 4;
+ put(b, 0, (uint32_t)(o - 8)); put(b, 4, 14);
+ PwPod s = { b, o }, f;
+ int32_t v;
+ const char *str;
+ CHECK(!pwpod_struct_field(s, 0, &f) && !pwpod_int(f, &v) && v == 7);
+ CHECK(!pwpod_struct_field(s, 1, &f) && !pwpod_string(f, &str) && !strcmp(str, "Audio/Source"));
+ CHECK(!pwpod_struct_field(s, 2, &f) && !pwpod_int(f, &v) && v == -1);
+ CHECK(pwpod_struct_field(s, 3, &f) != 0); /* past the end */
+ CHECK(!pwpod_struct_field(s, 1, &f) && pwpod_int(f, &v) != 0); /* a string is not an int */
+ PwPod cut = { b, o - 10 };
+ CHECK(pwpod_struct_field(cut, 0, &f) != 0); /* the struct says more than there is */
+ uint8_t lie[64];
+ memcpy(lie, b, sizeof lie);
+ put(lie, 8, 400); /* a field longer than the struct */
+ PwPod l = { lie, o };
+ CHECK(pwpod_struct_field(l, 0, &f) != 0);
+ uint8_t nonul[40] = { 0 };
+ put(nonul, 0, 24); put(nonul, 4, 14); put(nonul, 8, 4); put(nonul, 12, 8); memcpy(nonul + 16, "abcd", 4);
+ PwPod nn = { nonul, 32 };
+ CHECK(!pwpod_struct_field(nn, 0, &f) && pwpod_string(f, &str) != 0); /* string without its 0 */
+
+ CHECK(pwguard_capture_flag("Audio/Source") == FW_R_NO_MIC);
+ CHECK(pwguard_capture_flag("Audio/Source/Virtual") == FW_R_NO_MIC);
+ CHECK(pwguard_capture_flag("Audio/Duplex") == FW_R_NO_MIC);
+ CHECK(pwguard_capture_flag("Video/Source") == FW_R_NO_CAMERA);
+ CHECK(pwguard_capture_flag("Audio/Sink") == 0); /* playback stays */
+ CHECK(pwguard_capture_flag(NULL) == 0);
+ CHECK(pwguard_stream_flag("Stream/Input/Audio") == FW_R_NO_MIC);
+ CHECK(pwguard_stream_flag("Stream/Input/Video") == FW_R_NO_CAMERA);
+ CHECK(pwguard_stream_flag("Stream/Output/Audio") == 0);
+ printf("pwguard-test: %d/%d passed\n", total - fails, total);
+ return fails ? 1 : 0;
+}