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 cgroup exemption from the device and Bluetooth union

Federico Pereira9057c11f2059
diff --git a/server/src/netperm/netperm.c b/server/src/netperm/netperm.c
index 19e80e1..c90993c 100644
--- a/server/src/netperm/netperm.c
+++ b/server/src/netperm/netperm.c
@@ -80,8 +80,6 @@ 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) */
- uint64_t exempt[8]; /* the user's audio services in np_exempt (see sync_exempt) */
- int nexempt;
int ulinks[16], nulinks; /* the programs attached to user@<uid>.service */
} Session;
@@ -103,7 +101,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_exempt;
+ 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 dev_ok, bt_ok; /* the device / Bluetooth programs loaded */
int links[16], nlinks;
int progs[16], nprogs;
@@ -210,7 +208,6 @@ 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_exempt")) return G.m_np_exempt;
return -1;
}
#endif
@@ -323,35 +320,6 @@ static void sync_union(Session *s) {
}
}
-/* The user's audio services stay out of the device and Bluetooth union (np.bpf.c, find_dev_app):
- * PipeWire opens the cameras for its video nodes and WirePlumber the Bluetooth audio sockets, for
- * the whole session. Their cgroups (user@<uid>.service/session.slice/<unit>) change id when they
- * restart, so this runs on every pass. Fixed unit names, nothing from the user. */
-static void sync_exempt(Session *s) {
- static const char *const units[] = { "pipewire.service", "pipewire-pulse.service", "wireplumber.service", "filter-chain.service" };
- uint64_t now[8];
- int n = 0;
- if (s->user_cg) {
- char path[200];
- snprintf(path, sizeof path, CG_ROOT "/" CG_USER "/user-%u.slice/user@%u.service/session.slice", s->uid, s->uid);
- int dfd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
- for (size_t i = 0; dfd >= 0 && i < sizeof units / sizeof units[0] && n < 8; i++) {
- uint64_t id = bpfs_cgroup_id(dfd, units[i]);
- if (id) now[n++] = id;
- }
- if (dfd >= 0) close(dfd);
- }
- for (int i = 0; i < s->nexempt; i++) {
- int keep = 0;
- for (int k = 0; k < n; k++) keep |= now[k] == s->exempt[i];
- if (!keep) bpfs_map_delete(G.m_np_exempt, &s->exempt[i]);
- }
- uint32_t one = 1;
- for (int k = 0; k < n; k++) bpfs_map_update(G.m_np_exempt, &now[k], &one, BPF_ANY);
- memcpy(s->exempt, now, sizeof now);
- s->nexempt = n;
-}
-
static void drop_user_links(Session *s) {
for (int i = 0; i < s->nulinks; i++) close(s->ulinks[i]);
s->nulinks = 0;
@@ -441,7 +409,6 @@ static void scan_session(Session *s) {
}
sync_user_scope(s); /* attaches/re-attaches user@ and recompiles the union */
sync_union(s);
- if (G.bpf_ok) sync_exempt(s);
}
static void drop_session(int k) {
@@ -449,7 +416,6 @@ 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); }
- if (G.bpf_ok) for (int i = 0; i < s->nexempt; i++) bpfs_map_delete(G.m_np_exempt, &s->exempt[i]);
if (s->wd >= 0) inotify_rm_watch(G.ifd, s->wd);
free(s->sc);
fw_store_free(s->store);
@@ -730,7 +696,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_exempt < 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_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 6c9ab09..55fb8d8 100644
--- a/server/src/netperm/np.bpf.c
+++ b/server/src/netperm/np.bpf.c
@@ -50,7 +50,6 @@ 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_exempt[4] SEC("maps");
#define AF_INET 2
#define AF_INET6 10
@@ -82,28 +81,6 @@ NP_INLINE NpApp *find_app(NpCfg *cfg, np_u64 *cgid) {
return 0;
}
-/* The same for the device and Bluetooth restrictions, with one difference: under user@<uid> the
- * user's own audio services (PipeWire, WirePlumber: np_exempt, a service at level
- * cfg->user_level + 2) are left out of the union, so a restricted app running doesn't take the
- * camera or Bluetooth audio away from the whole session. */
-NP_INLINE NpApp *find_dev_app(NpCfg *cfg, np_u64 *cgid) {
- np_u64 id = bpf_get_current_ancestor_cgroup_id((int)cfg->level);
- if (id) {
- NpApp *app = bpf_map_lookup_elem(&np_apps, &id);
- if (app) { *cgid = id; return app; }
- }
- if (cfg->user_level) {
- np_u64 svc = bpf_get_current_ancestor_cgroup_id((int)cfg->user_level + 2);
- if (svc && bpf_map_lookup_elem(&np_exempt, &svc)) return 0;
- np_u64 uid = bpf_get_current_ancestor_cgroup_id((int)cfg->user_level);
- if (uid) {
- NpApp *app = bpf_map_lookup_elem(&np_union, &uid);
- if (app) { *cgid = uid; return app; }
- }
- }
- return 0;
-}
-
NP_INLINE void count(np_u64 cgid, int deny) {
NpCount *c = bpf_map_lookup_elem(&np_counts, &cgid);
if (!c) {
@@ -299,6 +276,11 @@ NP_INLINE void report_dev(np_u64 cgid, int dir, np_u32 major, np_u32 minor, np_u
* open() and mknod() of a device node; 1 = allow, 0 = EPERM. Programs attached higher up (systemd's
* DeviceAllow) still apply: the kernel allows only what all of them allow. Only the devices of
* np_dev_restriction are ever denied, and only to a restricted app. */
+/* Under user@<uid> the union applies to every process, PipeWire and WirePlumber included: the
+ * user owns that subtree and can move any of their processes into any of its cgroups, so an
+ * exemption by cgroup would be an open door. The cost (docs/FIREWALL.md): while a camera- or
+ * Bluetooth-restricted app runs, the user's own services can't open a new camera or create a new
+ * Bluetooth socket either; what they already have open keeps working. */
SEC("cgroup/dev") int np_dev(struct bpf_cgroup_dev_ctx *ctx) {
np_u32 zero = 0;
NpCfg *cfg = bpf_map_lookup_elem(&np_cfg, &zero);
@@ -306,7 +288,7 @@ SEC("cgroup/dev") int np_dev(struct bpf_cgroup_dev_ctx *ctx) {
np_u32 need = np_dev_restriction(ctx->access_type & 0xffff, ctx->major, cfg->hidraw_major);
if (!need) return 1;
np_u64 cgid = 0;
- NpApp *app = find_dev_app(cfg, &cgid);
+ NpApp *app = find_app(cfg, &cgid);
if (!app || !(app->flags & need)) return 1;
report_dev(cgid, NP_D_DEV, ctx->major, ctx->minor, need);
return 0;
@@ -323,7 +305,7 @@ SEC("cgroup/lsm_socket_create") int np_bt_socket(unsigned long long *ctx) {
NpCfg *cfg = bpf_map_lookup_elem(&np_cfg, &zero);
if (!cfg || !cfg->on) return 1;
np_u64 cgid = 0;
- NpApp *app = find_dev_app(cfg, &cgid);
+ NpApp *app = find_app(cfg, &cgid);
if (!app || !(app->flags & NP_R_NO_BLUETOOTH)) return 1;
report_dev(cgid, NP_D_BT, 0, 0, NP_R_NO_BLUETOOTH);
return 0;
diff --git a/server/src/netperm/np_shared.h b/server/src/netperm/np_shared.h
index 9997cf6..f247792 100644
--- a/server/src/netperm/np_shared.h
+++ b/server/src/netperm/np_shared.h
@@ -15,9 +15,6 @@
* 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_exempt HASH cgroup id (u64) → u32 the user's audio services (PipeWire,
- * WirePlumber) under user@<uid>: the device and
- * Bluetooth union doesn't apply to them
*
* Besides the network, the same per-app policy carries three device restrictions (docs/FIREWALL.md,
* "Linux apps"): "no camera" and "no USB" (cgroup/dev, by device major) and "no Bluetooth"
diff --git a/server/src/netperm/npload.h b/server/src/netperm/npload.h
index 3a358ee..f5ff9da 100644
--- a/server/src/netperm/npload.h
+++ b/server/src/netperm/npload.h
@@ -20,8 +20,7 @@
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_exempt, BPF_MAP_TYPE_HASH, 8, 4, NP_MAX_SESS * 8, 0)
+ X(np_events, BPF_MAP_TYPE_RINGBUF, 0, 0, NP_RINGBUF, 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/bluetooth.sh b/server/tests/fwlinux/bluetooth.sh
index be2ec57..0b76474 100755
--- a/server/tests/fwlinux/bluetooth.sh
+++ b/server/tests/fwlinux/bluetooth.sh
@@ -30,7 +30,14 @@ restrict $APP no-bluetooth
expect EPERM "$(bt)" "socket(AF_BLUETOOTH)"
expect ok "$(in_scope "$SC" python3 -c "$INET" 2>&1 | tail -1)" "inet and unix sockets still work"
expect "$base" "$(python3 -c "$SOCK" | tail -1)" "outside the app's scope"
-echo "3. lifted"
+echo "3. escape with systemd-run --user, also into the user's pipewire.service"
+in_scope "$SC" sleep 5 &
+sleep 1.5
+expect EPERM "$(systemd-run --user --scope --quiet python3 -c "$SOCK" 2>&1 | tail -1)" "systemd-run --user --scope (user@ union)"
+PWCG=/sys/fs/cgroup/user.slice/user-$U.slice/user@$U.service/session.slice/pipewire.service
+[ -d "$PWCG" ] && expect EPERM "$(systemd-run --user --scope --quiet bash -c 'echo $$ > "$0/cgroup.procs" && exec python3 -c "$1"' "$PWCG" "$SOCK" 2>&1 | tail -1)" "an escapee that moved into pipewire.service"
+wait
+echo "4. lifted"
restrict $APP ""
expect "$base" "$(bt)" "socket(AF_BLUETOOTH)"
fwlinux_done
diff --git a/server/tests/fwlinux/devices.sh b/server/tests/fwlinux/devices.sh
index fc7b0e2..053f15a 100755
--- a/server/tests/fwlinux/devices.sh
+++ b/server/tests/fwlinux/devices.sh
@@ -55,9 +55,10 @@ sleep 1.5 # lord-netperm sees the scope and builds
got=$(systemd-run --user --quiet --wait --pipe python3 -c "$OPEN" "$VID" 2>&1 | tail -1)
expect EPERM "$got" "systemd-run --user (user@ union)"
PWCG=/sys/fs/cgroup/user.slice/user-$U.slice/user@$U.service/session.slice/pipewire.service
-if [ -d "$PWCG" ]; then # the user's PipeWire keeps the camera for the whole session
- got=$(sudo bash -c 'echo $$ > "$0/cgroup.procs"; exec setpriv --reuid='"$U"' --regid='"$G"' --init-groups python3 -c "$1" "$2"' "$PWCG" "$OPEN" "$VID" 2>&1 | tail -1)
- expect ok "$got" "a process in pipewire.service (exempt from the union)"
+if [ -d "$PWCG" ]; then # no exemption a process could join: an escapee that moves itself
+ # into the user's pipewire.service (the user owns user@'s subtree, so it can) is still refused
+ got=$(systemd-run --user --scope --quiet bash -c 'echo $$ > "$0/cgroup.procs" && exec python3 -c "$1" "$2"' "$PWCG" "$OPEN" "$VID" 2>&1 | tail -1)
+ expect EPERM "$got" "an escapee that moved itself into pipewire.service"
fi
wait
fwlinux_done