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
lord-netperm: no-mic also denies the ALSA capture nodes
Federico Pereira3fad8b2806f2
diff --git a/server/src/netperm/netperm.c b/server/src/netperm/netperm.cindex a96f4a4..11de6e6 100644--- a/server/src/netperm/netperm.c+++ b/server/src/netperm/netperm.c@@ -101,7 +101,9 @@ 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;+ 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_capdev;+ uint32_t capdev[256]; /* the ALSA capture minors in np_capdev */+ int ncapdev;int dev_ok, bt_ok; /* the device / Bluetooth programs loaded */int links[16], nlinks;int progs[16], nprogs;@@ -208,6 +210,7 @@ static int map_fd(const char *name) {if (!strcmp(name, "np_seen")) return G.m_np_seen;if (!strcmp(name, "np_counts")) return G.m_np_counts;if (!strcmp(name, "np_events")) return G.m_np_events;+ if (!strcmp(name, "np_capdev")) return G.m_np_capdev;return -1;}#endif@@ -422,7 +425,41 @@ static void drop_session(int k) {G.sess[k] = G.sess[--G.nsess];}+/* The ALSA capture PCM nodes (/sys/class/sound/pcmC*D*c, "116:<minor>"), for "no microphone"+ * against apps that read the sound card directly instead of through PipeWire. Rescanned with the+ * sessions (a USB microphone plugged in later). Only sysfs: no device node is opened. */+static void scan_capdev(void) {+ if (!G.bpf_ok) return;+ uint32_t now[256];+ int n = 0;+ DIR *d = opendir("/sys/class/sound");+ struct dirent *e;+ while (d && (e = readdir(d)) && n < 256) {+ size_t l = strlen(e->d_name);+ if (strncmp(e->d_name, "pcmC", 4) || l < 6 || e->d_name[l - 1] != 'c') continue;+ char p[300], buf[32];+ snprintf(p, sizeof p, "/sys/class/sound/%s/dev", e->d_name);+ int fd = open(p, O_RDONLY | O_CLOEXEC);+ if (fd < 0) continue;+ ssize_t k = read(fd, buf, sizeof buf - 1);+ close(fd);+ unsigned ma, mi;+ if (k > 0 && (buf[k] = 0, sscanf(buf, "%u:%u", &ma, &mi) == 2) && ma == NP_MAJOR_SND) now[n++] = mi;+ }+ if (d) closedir(d);+ for (int i = 0; i < G.ncapdev; i++) {+ int keep = 0;+ for (int k = 0; k < n; k++) keep |= now[k] == G.capdev[i];+ if (!keep) bpfs_map_delete(G.m_np_capdev, &G.capdev[i]);+ }+ uint32_t one = 1;+ for (int k = 0; k < n; k++) bpfs_map_update(G.m_np_capdev, &now[k], &one, BPF_ANY);+ memcpy(G.capdev, now, sizeof now);+ G.ncapdev = n;+}+static void scan_all(void) {+ scan_capdev();DIR *d = opendir(CG_ROOT "/" CG_SERVICE);for (int i = 0; i < G.nsess; i++) G.sess[i].seen = 0;struct dirent *e;@@ -711,7 +748,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_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_capdev < 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;}diff --git a/server/src/netperm/np.bpf.c b/server/src/netperm/np.bpf.cindex f5cf4fa..e2e2009 100644--- a/server/src/netperm/np.bpf.c+++ b/server/src/netperm/np.bpf.c@@ -9,9 +9,10 @@* cgroup/sendmsg4, sendmsg6 unconnected UDP (sendto with an address)* cgroup/bind4, bind6 "no incoming connections" and inbound rules (local port != 0)* cgroup/sock_create "no network": not even creating an AF_INET/AF_INET6 socket- * cgroup/dev "no camera", "no USB" and "no screen capture": opening video- * devices, raw USB devices (/dev/bus/usb), hidraw, ttyACM, ttyUSB- * and the framebuffer (by major); every other device stays allowed+ * cgroup/dev "no camera", "no USB", "no screen capture" and "no microphone":+ * opening video devices, raw USB devices (/dev/bus/usb), hidraw,+ * ttyACM, ttyUSB, the framebuffer (by major) and ALSA capture PCM+ * nodes (np_capdev); every other device stays allowed* cgroup/lsm_socket_create "no Bluetooth": socket(AF_BLUETOOTH) (a per-cgroup LSM program,* BPF_LSM_CGROUP: the inet sock_create hook never sees that family)*@@ -50,6 +51,7 @@ char np_lpm6[4] SEC("maps");char np_seen[4] SEC("maps");char np_counts[4] SEC("maps");char np_events[4] SEC("maps");+char np_capdev[4] SEC("maps");#define AF_INET 2#define AF_INET6 10@@ -286,6 +288,10 @@ SEC("cgroup/dev") int np_dev(struct bpf_cgroup_dev_ctx *ctx) {NpCfg *cfg = bpf_map_lookup_elem(&np_cfg, &zero);if (!cfg || !cfg->on) return 1;np_u32 need = np_dev_restriction(ctx->access_type & 0xffff, ctx->major, cfg->hidraw_major);+ if (!need && (ctx->access_type & 0xffff) == NP_DEV_CHAR && ctx->major == NP_MAJOR_SND) {+ np_u32 minor = ctx->minor; /* a capture PCM node: recording straight from ALSA */+ if (bpf_map_lookup_elem(&np_capdev, &minor)) need = NP_R_NO_MIC;+ }if (!need) return 1;np_u64 cgid = 0;NpApp *app = find_app(cfg, &cgid);diff --git a/server/src/netperm/np_shared.h b/server/src/netperm/np_shared.hindex 27bdeb9..1984181 100644--- a/server/src/netperm/np_shared.h+++ b/server/src/netperm/np_shared.h@@ -15,6 +15,7 @@* np_seen LRU NpSeenKey → u64 ns to report only new connections* np_counts HASH cgroup id → NpCount allowed / denied* np_events RINGBUF NpEvent towards lord-netperm+ * np_capdev HASH ALSA minor (u32) → u32 the capture PCM nodes (/dev/snd/pcmC*D*c), for "no microphone"** 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@@ -54,15 +55,17 @@ typedef uint64_t np_u64;#define NP_R_NO_NETWORK (1u << 2)#define NP_R_NO_INCOMING (1u << 3)#define NP_R_NO_CAMERA (1u << 4)+#define NP_R_NO_MIC (1u << 5)#define NP_R_NO_SCREEN (1u << 6)#define NP_R_NO_USB (1u << 7)#define NP_R_NO_BLUETOOTH (1u << 8)#define NP_R_NET_MASK (NP_R_NO_INTERNET | NP_R_NO_LAN | NP_R_NO_NETWORK | NP_R_NO_INCOMING)-#define NP_R_DEV_MASK (NP_R_NO_CAMERA | NP_R_NO_SCREEN | NP_R_NO_USB | NP_R_NO_BLUETOOTH)+#define NP_R_DEV_MASK (NP_R_NO_CAMERA | NP_R_NO_MIC | NP_R_NO_SCREEN | NP_R_NO_USB | NP_R_NO_BLUETOOTH)/* Character device majors (Documentation/admin-guide/devices.txt; hidraw is dynamic and comes* from /proc/devices, in NpCfg). */#define NP_MAJOR_FB 29 /* /dev/fb* (the framebuffer device) */+#define NP_MAJOR_SND 116 /* ALSA: only its capture PCM nodes (np_capdev) */#define NP_MAJOR_V4L 81 /* /dev/video*, /dev/v4l-subdev*, /dev/radio*, /dev/vbi* */#define NP_MAJOR_TTYACM 166 /* USB modems and boards (/dev/ttyACM*) */#define NP_MAJOR_TTYUSB 188 /* USB serial adapters (/dev/ttyUSB*) */diff --git a/server/src/netperm/npload.h b/server/src/netperm/npload.hindex f5ff9da..9858de5 100644--- a/server/src/netperm/npload.h+++ b/server/src/netperm/npload.h@@ -20,7 +20,8 @@X(np_lpm6, BPF_MAP_TYPE_LPM_TRIE, sizeof(NpLpm6Key), sizeof(NpList), NP_MAX_LPM, BPF_F_NO_PREALLOC) \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_events, BPF_MAP_TYPE_RINGBUF, 0, 0, NP_RINGBUF, 0) \+ X(np_capdev, BPF_MAP_TYPE_HASH, 4, 4, 256, 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 Bluetoothdiff --git a/server/tests/fwlinux/mic.sh b/server/tests/fwlinux/mic.shindex 4255bdb..9e796b4 100755--- a/server/tests/fwlinux/mic.sh+++ b/server/tests/fwlinux/mic.sh@@ -76,7 +76,21 @@ sleep 1.5f=$OUTD/out.raw; timeout 3 pw-record --rate 48000 --channels 1 --format s16 "$f" >/dev/null 2>&1n=$(stat -c %s "$f" 2>/dev/null || echo 0)[ "$n" -gt 20000 ] && ok "another program records ($n bytes)" || bad "another program got $n bytes"-echo "6. lying about who it is: still nothing recorded"+echo "6. straight from the sound card (ALSA hw device, not PipeWire): refused by lord-netperm"+CAP=$(ls /dev/snd/pcmC*D*c 2>/dev/null | head -1)+if [ -n "$CAP" ] && [ -S /run/lordos/netperm.sock ]; then+ got=$(in_scope "$SC" 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))' "$CAP" 2>&1 | tail -1)+ [ "$got" = EPERM ] && ok "open $CAP → EPERM" || bad "open $CAP → $got"+ PB=$(ls /dev/snd/pcmC*D*p 2>/dev/null | head -1)+ got=$(in_scope "$SC" 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))' "$PB" 2>&1 | tail -1)+ [ "$got" != EPERM ] && ok "the playback node $PB is not refused ($got)" || bad "playback node refused"+fi++echo "7. lying about who it is: still nothing recorded"# an unrestricted recorder outside the scope, whose pid the restricted app claimstimeout 12 parecord --raw "$OUTD/decoy.raw" >/dev/null 2>&1 &DECOY=$!diff --git a/server/tests/netperm_integration.sh b/server/tests/netperm_integration.shindex 2d85152..a7f4884 100644--- a/server/tests/netperm_integration.sh+++ b/server/tests/netperm_integration.sh@@ -115,26 +115,31 @@ got=$(python3 -c 'import socket;socket.create_connection(("'$GW'",53),timeout=4)echo "9. connection notices carry the names the resolver gave (hosts)"rules '{"v":1,"mode":"silent","trusted":[],"restricted":{},"rules":[{"app":"'$APP'","dir":"out","dst":"example.com","action":"deny","when":0}]}'-python3 - "$APP" > /tmp/np-hosts-$U.txt <<'PY' &+HOSTPY='import socket, struct, sys, time, json-s = socket.socket(socket.AF_UNIX); s.connect('/run/lordos/netperm.sock'); s.settimeout(8)-buf = b''; end = time.time() + 8+s = socket.socket(socket.AF_UNIX); s.connect("/run/lordos/netperm.sock"); s.settimeout(8)+buf = b""; end = time.time() + 8while time.time() < end:try: d = s.recv(65536)except socket.timeout: breakif not d: breakbuf += dwhile len(buf) >= 5:- l, t = struct.unpack('<IB', buf[:5])+ l, t = struct.unpack("<IB", buf[:5])if len(buf) < 4 + l: breakbody = buf[5:4 + l]; buf = buf[4 + l:]if t == 11:ev = json.loads(body)- if ev.get('app') == sys.argv[1] and ev.get('hosts'): print(' '.join(ev['hosts'])); sys.exit(0)-PY-sleep 1-try https example.com 443 >/dev/null-wait+ if ev.get("app") == sys.argv[1] and ev.get("hosts"): print(" ".join(ev["hosts"])); sys.exit(0)+'+for attempt in 1 2 3; do # the resolver may answer from its cache: try a few times+ python3 -c "$HOSTPY" "$APP" > /tmp/np-hosts-$U.txt &+ sleep 1+ try https example.com 443 >/dev/null+ wait+ grep -q "example.com" /tmp/np-hosts-$U.txt && break+ sleep 5.5 # a repeated notice comes again only after 5 s+donegrep -q "example.com" /tmp/np-hosts-$U.txt && ok "the notice names example.com ($(cat /tmp/np-hosts-$U.txt))" || bad "no host names in the notice"rm -f /tmp/np-hosts-$U.txtdiff --git a/server/tests/npcompile_test.c b/server/tests/npcompile_test.cindex 1634640..d5cd951 100644--- a/server/tests/npcompile_test.c+++ b/server/tests/npcompile_test.c@@ -432,14 +432,14 @@ static void device_cases(void) {static const uint32_t free_majors[] = { 1, 4, 5, 10, 13, 116, 226, 180, 246, 0 }; /* mem, tty, misc, input, sound, dri... */for (int i = 0; free_majors[i]; i++) CHECK(np_dev_restriction(NP_DEV_CHAR, free_majors[i], HID) == 0);/* the FW_R_* bits are the same */- CHECK(NP_R_NO_CAMERA == FW_R_NO_CAMERA && NP_R_NO_SCREEN == FW_R_NO_SCREEN && NP_R_NO_USB == FW_R_NO_USB && NP_R_NO_BLUETOOTH == FW_R_NO_BLUETOOTH);+ CHECK(NP_R_NO_CAMERA == FW_R_NO_CAMERA && NP_R_NO_MIC == FW_R_NO_MIC && NP_R_NO_SCREEN == FW_R_NO_SCREEN && NP_R_NO_USB == FW_R_NO_USB && NP_R_NO_BLUETOOTH == FW_R_NO_BLUETOOTH);FwStore *s = fw_store_new();DnsMap *names = dnsmap_new(0);fw_set_restricted(s, "desktop:cam", FW_R_NO_CAMERA | FW_R_NO_USB | FW_R_NO_BLUETOOTH | FW_R_NO_FILES | FW_R_NO_MIC);fw_set_restricted(s, "lordos", FW_R_NO_CAMERA | FW_R_NO_USB | FW_R_NO_BLUETOOTH | FW_R_NO_INTERNET);NpCompiled c = compile(s, "desktop:cam", names, 1000);- CHECK(c.app.flags == (NP_R_NO_CAMERA | NP_R_NO_USB | NP_R_NO_BLUETOOTH)); /* files/mic aren't the kernel's */+ CHECK(c.app.flags == (NP_R_NO_CAMERA | NP_R_NO_USB | NP_R_NO_BLUETOOTH | NP_R_NO_MIC)); /* files aren't the kernel's */uint8_t a[16];FwDecision ref;NpRes got;