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
Screen capture for Linux apps: verify the design, and deny the framebuffer
Federico Pereira0e27a5955d03
diff --git a/server/Makefile b/server/Makefileindex eb66fc6..eb2a214 100644--- a/server/Makefile+++ b/server/Makefile@@ -266,6 +266,10 @@ build/pwguard-test: tests/pwguard_test.c $(PWGUARD_SRC) $(wildcard src/sec/*.h s@mkdir -p build$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/pwguard_test.c $(PWGUARD_SRC)./$@ || { rm -f $@; exit 1; }+# tests/wl_globals.c: the compositor's globals as a client sees them (tests/fwlinux/screen.sh)+build/wl-globals: tests/wl_globals.c+ @mkdir -p build+ $(CC) $(CFLAGS) $(WARN) -std=c11 -o $@ $< -lwayland-clientbuild/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.cdiff --git a/server/src/netperm/np.bpf.c b/server/src/netperm/np.bpf.cindex 55fb8d8..f5cf4fa 100644--- a/server/src/netperm/np.bpf.c+++ b/server/src/netperm/np.bpf.c@@ -9,9 +9,9 @@* 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" and "no USB": opening video devices, raw USB devices- * (/dev/bus/usb), hidraw, ttyACM and ttyUSB (by major); every other- * device stays allowed+ * 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/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)*diff --git a/server/src/netperm/np_shared.h b/server/src/netperm/np_shared.hindex f247792..27bdeb9 100644--- a/server/src/netperm/np_shared.h+++ b/server/src/netperm/np_shared.h@@ -16,8 +16,9 @@* np_counts HASH cgroup id → NpCount allowed / denied* np_events RINGBUF NpEvent towards lord-netperm*- * 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"+ * 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+ * major) and "no Bluetooth"* (socket(AF_BLUETOOTH), cgroup/lsm_socket_create).*/#ifndef LORD_NP_SHARED_H@@ -53,13 +54,15 @@ 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_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_USB | NP_R_NO_BLUETOOTH)+#define NP_R_DEV_MASK (NP_R_NO_CAMERA | 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_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*) */@@ -134,11 +137,13 @@ typedef struct {} NpRes;/* Which restriction covers opening a character device (cgroup/dev): NP_R_NO_CAMERA for video- * devices, NP_R_NO_USB for raw USB, hidraw and USB serial; 0 for everything else (always+ * devices, NP_R_NO_SCREEN for the framebuffer device, NP_R_NO_USB for raw USB, hidraw and USB+ * serial; 0 for everything else (always* allowed: the program only takes away what the user restricted). */NP_INLINE np_u32 np_dev_restriction(np_u32 type, np_u32 major, np_u32 hidraw_major) {if (type != NP_DEV_CHAR) return 0;if (major == NP_MAJOR_V4L) return NP_R_NO_CAMERA;+ if (major == NP_MAJOR_FB) return NP_R_NO_SCREEN;if (major == NP_MAJOR_USBDEV || major == NP_MAJOR_TTYACM || major == NP_MAJOR_TTYUSB) return NP_R_NO_USB;if (hidraw_major && major == hidraw_major) return NP_R_NO_USB;return 0;diff --git a/server/tests/fwlinux/screen.sh b/server/tests/fwlinux/screen.shnew file mode 100755index 0000000..d46def2--- /dev/null+++ b/server/tests/fwlinux/screen.sh@@ -0,0 +1,38 @@+#!/bin/bash+# screen.sh — "no screen capture" for Linux apps is enforced by design: no app can copy the+# screen, restricted or not (docs/FIREWALL.md, "Linux apps"). This checks the design holds on the+# running system:+# - the compositor offers no capture protocol (screencopy, image capture, dmabuf export) to a+# client (build/wl-globals, connected like any app);+# - there is no X server (Xwayland isn't supported), so no X11 screen grab;+# - no screen-cast portal is installed that could hand a PipeWire screen stream to an app+# (xdg-desktop-portal with a ScreenCast backend);+# - no PipeWire node is a screen source;+# - a restricted app can't read the framebuffer device (/dev/fb*, lord-netperm's cgroup/dev).+#+# bash server/tests/fwlinux/screen.sh (after make -C server build/wl-globals)+. "$(dirname "$0")/lib.sh"+D=$(cd "$(dirname "$0")" && pwd)+WG=$D/../../build/wl-globals+export XDG_RUNTIME_DIR=/run/user/$U+[ -x "$WG" ] || { echo "missing build/wl-globals (make -C server build/wl-globals)"; exit 2; }+GLOBALS=$(WAYLAND_DISPLAY=${WAYLAND_DISPLAY:-wayland-0} "$WG")+[ -n "$GLOBALS" ] && ok "connected to the compositor: $(echo "$GLOBALS" | wc -l) globals" || { bad "no globals"; fwlinux_done; exit 1; }+cap=$(echo "$GLOBALS" | grep -E -i "screencopy|image_copy_capture|image_capture_source|export_dmabuf|screencast|output_image|capture" || true)+[ -z "$cap" ] && ok "no capture protocol among them" || bad "capture protocols offered: $cap"+ls /tmp/.X11-unix/X* >/dev/null 2>&1 && bad "an X server is running" || ok "no X server socket"+portal=$(grep -l -s "ScreenCast" /usr/share/xdg-desktop-portal/portals/*.portal 2>/dev/null)+command -v /usr/libexec/xdg-desktop-portal >/dev/null 2>&1 || [ -x /usr/libexec/xdg-desktop-portal ] && pp=1 || pp=0+[ -z "$portal" ] && ok "no screen-cast portal backend installed (xdg-desktop-portal: $([ $pp = 1 ] && echo present || echo absent))" || bad "screen-cast portal backends: $portal"+n=$(pw-dump 2>/dev/null | grep -c -E '"media.class": "Video/Source"|"media.role": "Screen"' || true)+[ "${n:-0}" = 0 ] && ok "no PipeWire video or screen source node" || echo " note: $n PipeWire video nodes (cameras; screens only come from a portal)"+if [ -S /run/lordos/netperm.sock ] && ls /dev/fb* >/dev/null 2>&1; then+ FB=$(ls /dev/fb* | head -1)+ SC=$(scope fwscreen)+ restrict desktop:fwscreen no-screen+ got=$(in_scope "$SC" python3 -c 'import os,sys,errno+try: os.close(os.open(sys.argv[1], os.O_RDONLY)); print("ok")+except OSError as e: print(errno.errorcode.get(e.errno))' "$FB" 2>&1 | tail -1)+ [ "$got" = EPERM ] && ok "a no-screen app can't open $FB ($got)" || bad "$FB → $got"+fi+fwlinux_donediff --git a/server/tests/npcompile_test.c b/server/tests/npcompile_test.cindex 61f2bdc..1634640 100644--- a/server/tests/npcompile_test.c+++ b/server/tests/npcompile_test.c@@ -422,6 +422,7 @@ static void bench(void) {static void device_cases(void) {const uint32_t HID = 242;CHECK(np_dev_restriction(NP_DEV_CHAR, 81, HID) == NP_R_NO_CAMERA); /* /dev/video* */+ CHECK(np_dev_restriction(NP_DEV_CHAR, 29, HID) == NP_R_NO_SCREEN); /* /dev/fb* */CHECK(np_dev_restriction(NP_DEV_CHAR, 189, HID) == NP_R_NO_USB); /* /dev/bus/usb */CHECK(np_dev_restriction(NP_DEV_CHAR, 166, HID) == NP_R_NO_USB); /* ttyACM */CHECK(np_dev_restriction(NP_DEV_CHAR, 188, HID) == NP_R_NO_USB); /* ttyUSB */@@ -431,7 +432,7 @@ 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_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_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);diff --git a/server/tests/wl_globals.c b/server/tests/wl_globals.cnew file mode 100644index 0000000..de5201f--- /dev/null+++ b/server/tests/wl_globals.c@@ -0,0 +1,23 @@+/* wl_globals.c — prints every global the Wayland compositor offers to a client, one interface+ * name per line (tests/fwlinux/screen.sh checks that none of them lets an app copy the screen).+ *+ * WAYLAND_DISPLAY=wayland-0 build/wl-globals */+#include <stdio.h>+#include <wayland-client.h>++static void global(void *d, struct wl_registry *r, uint32_t name, const char *iface, uint32_t version) {+ (void)d; (void)r; (void)name;+ printf("%s %u\n", iface, version);+}+static void global_remove(void *d, struct wl_registry *r, uint32_t name) { (void)d; (void)r; (void)name; }+static const struct wl_registry_listener listener = { global, global_remove };++int main(void) {+ struct wl_display *dpy = wl_display_connect(NULL);+ if (!dpy) { fprintf(stderr, "wl-globals: no Wayland display\n"); return 2; }+ struct wl_registry *reg = wl_display_get_registry(dpy);+ wl_registry_add_listener(reg, &listener, NULL);+ wl_display_roundtrip(dpy);+ wl_display_disconnect(dpy);+ return 0;+}