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 fails closed for ALSA nodes not yet classified in the app's own scope

Federico Pereira7054e2aba73f
diff --git a/docs/FIREWALL.md b/docs/FIREWALL.md
index 1df4154..3fcbe82 100644
--- a/docs/FIREWALL.md
+++ b/docs/FIREWALL.md
@@ -187,8 +187,11 @@ program; with none attached, everything is allowed. `lord-netperm` attaches one
`bpf_link`, multi-attach) to `lordos.service` and to each session's `user@<uid>.service`. It looks
the app up exactly like the network programs (its scope's ancestor id, or the `user@` union), runs
on `open()` and `mknod()` of a device node, and returns 0 only for the device classes the app is
-restricted from (`np_dev_restriction()` in `np_shared.h`, plus the ALSA capture minors in
-`np_capdev`); everything else returns 1. Programs systemd attaches higher up (`DeviceAllow=`) still
+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. In the `user@` union only the known capture nodes are, so the user's own PipeWire
+can still open a headset plugged in meanwhile. 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
@@ -639,7 +642,7 @@ is denied by the union all the same. The decision helper (`np_decide`) is unchan
| `np_seen` | LRU | (cgroup, address, port, protocol, connection direction, verdict) → last time reported |
| `np_counts` | hash | cgroup id → allowed, denied |
| `np_events` | ring buffer | new connections (and device or Bluetooth denials) to the service |
-| `np_capdev` | hash | ALSA capture PCM minors (from `/sys/class/sound`) → 1 |
+| `np_snd` | hash | every ALSA minor in `/sys/class/sound` → 2 for a capture PCM, 1 for the rest |
**What is counted and reported.** Every TCP `connect()` and every `bind()` is counted. A UDP
`sendto` and a UDP `connect()` count only the first time in 5 s, so the numbers don't get
diff --git a/server/src/netperm/netperm.c b/server/src/netperm/netperm.c
index 11de6e6..b13ced9 100644
--- a/server/src/netperm/netperm.c
+++ b/server/src/netperm/netperm.c
@@ -101,9 +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, m_np_capdev;
- uint32_t capdev[256]; /* the ALSA capture minors in np_capdev */
- int ncapdev;
+ 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;
+ uint32_t snd[NP_MAX_SND]; /* the ALSA minors in np_snd */
+ int nsnd;
int dev_ok, bt_ok; /* the device / Bluetooth programs loaded */
int links[16], nlinks;
int progs[16], nprogs;
@@ -210,7 +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;
+ if (!strcmp(name, "np_snd")) return G.m_np_snd;
return -1;
}
#endif
@@ -425,41 +425,49 @@ 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) {
+/* Every ALSA node (/sys/class/sound/<name>/dev, "116:<minor>"), capture PCMs (pcmC*D*c) marked,
+ * for "no microphone" against apps that read the sound card directly instead of through PipeWire.
+ * The program denies a no-mic app's own scope every ALSA minor not marked NP_SND_OTHER, so a node
+ * that appears between two scans (a USB microphone plugged in) is denied until it is known: fail
+ * closed. Rescanned with the sessions. Only sysfs: no device node is opened. */
+static int capture_pcm(const char *name) {
+ size_t l = strlen(name);
+ return !strncmp(name, "pcmC", 4) && l >= 6 && name[l - 1] == 'c';
+}
+
+static void scan_snd(void) {
if (!G.bpf_ok) return;
- uint32_t now[256];
+ static uint32_t now[NP_MAX_SND], kind[NP_MAX_SND];
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;
+ while (d && (e = readdir(d)) && n < NP_MAX_SND) {
+ if (e->d_name[0] == '.' || strlen(e->d_name) > 64) 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);
+ int fd = open(p, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
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 (k > 0 && (buf[k] = 0, sscanf(buf, "%u:%u", &ma, &mi) == 2) && ma == NP_MAJOR_SND) {
+ now[n] = mi;
+ kind[n++] = capture_pcm(e->d_name) ? NP_SND_CAPTURE : NP_SND_OTHER;
+ }
}
if (d) closedir(d);
- for (int i = 0; i < G.ncapdev; i++) {
+ for (int k = 0; k < n; k++) bpfs_map_update(G.m_np_snd, &now[k], &kind[k], BPF_ANY);
+ for (int i = 0; i < G.nsnd; 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]);
+ for (int k = 0; k < n && !keep; k++) keep = now[k] == G.snd[i];
+ if (!keep) bpfs_map_delete(G.m_np_snd, &G.snd[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;
+ memcpy(G.snd, now, sizeof(uint32_t) * (size_t)n);
+ G.nsnd = n;
}
static void scan_all(void) {
- scan_capdev();
+ scan_snd();
DIR *d = opendir(CG_ROOT "/" CG_SERVICE);
for (int i = 0; i < G.nsess; i++) G.sess[i].seen = 0;
struct dirent *e;
@@ -748,7 +756,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_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) {
+ 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) {
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.c
index e2e2009..18a2afd 100644
--- a/server/src/netperm/np.bpf.c
+++ b/server/src/netperm/np.bpf.c
@@ -11,8 +11,9 @@
* cgroup/sock_create "no network": not even creating an AF_INET/AF_INET6 socket
* 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
+ * ttyACM, ttyUSB, the framebuffer (by major) and ALSA capture
+ * nodes (np_snd: for an app's own scope every ALSA node not known
+ * to be a non-capture one); 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)
*
@@ -51,7 +52,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");
+char np_snd[4] SEC("maps");
#define AF_INET 2
#define AF_INET6 10
@@ -288,14 +289,24 @@ 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;
+ int snd = !need && (ctx->access_type & 0xffff) == NP_DEV_CHAR && ctx->major == NP_MAJOR_SND;
+ if (!need && !snd) return 1;
np_u64 cgid = 0;
NpApp *app = find_app(cfg, &cgid);
- if (!app || !(app->flags & need)) return 1;
+ if (!app) return 1;
+ 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. In
+ * the user@ union (the user's own PipeWire and WirePlumber too) only the known capture
+ * nodes are denied, so plugging in a headset still works for the desktop. */
+ 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 (!(app->flags & need)) return 1;
report_dev(cgid, NP_D_DEV, ctx->major, ctx->minor, need);
return 0;
}
diff --git a/server/src/netperm/np_shared.h b/server/src/netperm/np_shared.h
index 1984181..8d26b4b 100644
--- a/server/src/netperm/np_shared.h
+++ b/server/src/netperm/np_shared.h
@@ -15,7 +15,8 @@
* 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"
+ * 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
*
* 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
@@ -65,7 +66,9 @@ typedef uint64_t np_u64;
/* 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_SND 116 /* ALSA: its capture nodes (np_snd) */
+#define NP_SND_OTHER 1u
+#define NP_SND_CAPTURE 2u
#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.h
index 9858de5..86d2285 100644
--- a/server/src/netperm/npload.h
+++ b/server/src/netperm/npload.h
@@ -10,6 +10,7 @@
#include "netperm/np_shared.h"
#define NP_MAX_SESS 64
+#define NP_MAX_SND 1024 /* ALSA nodes in np_snd */
/* name, type, key size, value size, max entries, flags */
#define NP_MAPS(X) \
@@ -21,7 +22,7 @@
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_capdev, BPF_MAP_TYPE_HASH, 4, 4, 256, 0)
+ X(np_snd, BPF_MAP_TYPE_HASH, 4, 4, NP_MAX_SND, 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