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
Notifications: drop a restricted Linux app's notifications
Federico Pereira6a5201d7bd3e
diff --git a/server/Makefile b/server/Makefileindex 7f3a491..2694a55 100644--- a/server/Makefile+++ b/server/Makefile@@ -245,6 +245,12 @@ test: build/launch-clibuild/launch-cli: tests/launch_cli.c $(LAUNCH_CLI_SRC) src/apps.h $(wildcard src/sec/*.h src/fw/*.h src/hw/*.h)@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+test: build/notifyd-cli+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)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.cdiff --git a/server/src/hw/notifyd.c b/server/src/hw/notifyd.cindex 757d68e..4781320 100644--- a/server/src/hw/notifyd.c+++ b/server/src/hw/notifyd.c@@ -2,6 +2,8 @@#include "notifyd.h"#include "jbuf.h"#include "../vault/dbus.h"+#include "../fw/rules.h"+#include "../sec/restrict.h"#include <stdio.h>#include <stdlib.h>@@ -15,6 +17,11 @@ static int up;static uint32_t next_id = 1;static NotifydEvent emit;+/* Notify calls waiting for the bus to say who sent them (GetConnectionUnixProcessID). */+#define MAX_PENDING 32+static struct { uint32_t serial; DMsg call; } pending[MAX_PENDING];+static int npending;+int notifyd_fd(void) { return up ? conn.fd : -1; }int notifyd_wants_write(void) { return up && dbus_pending_out(&conn); }@@ -128,8 +135,87 @@ static void on_notify(const DMsg *m) {free(js);}-static void handle(const DMsg *m) {- if (m->type != DBUS_CALL || !m->member) return;+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);+}++/* Is the process behind this PID in the scope of an app restricted to "no notifications"? */+static int pid_blocked(uint32_t pid) {+ char f[64], line[1024], app[FW_APP_MAX];+ snprintf(f, sizeof f, "/proc/%u/cgroup", pid);+ FILE *fp = fopen(f, "re");+ if (!fp) return 0;+ int blocked = 0;+ while (fgets(line, sizeof line, fp))+ if (!strncmp(line, "0::", 3)) { blocked = !notifyd_app_of_cgroup(line + 3, app, sizeof app) && restrict_blocks(app, FW_R_NO_NOTIFICATIONS); break; }+ fclose(fp);+ return blocked;+}++/* A restricted app's notification: not shown, but answered with a real id (and closed at once),+ * so the app doesn't treat it as an error. */+static void drop_notify(const DMsg *m) {+ DRd r;+ dr_init(&r, m);+ dr_str(&r, NULL);+ uint32_t replaces = dr_u32(&r);+ uint32_t id = replaces ? replaces : next_id++;+ if (!next_id) next_id = 1;+ DBuf b;+ dw_init(&b);+ dw_u32(&b, id);+ dbus_reply(&conn, m, "u", &b);+ dw_free(&b);+ notifyd_closed(id, 3);+}++/* Asks the bus who sent the Notify; the call waits in `pending`. 1 kept, 0 handled now. */+static int ask_sender(DMsg *m) {+ if (!m->sender || npending == MAX_PENDING) {+ dbus_error(&conn, m, "org.freedesktop.DBus.Error.LimitsExceeded", "too many notifications at once");+ return 0;+ }+ DBuf b;+ dw_init(&b);+ dw_str(&b, m->sender);+ DHdr h = { .type = DBUS_CALL, .path = "/org/freedesktop/DBus", .iface = "org.freedesktop.DBus",+ .member = "GetConnectionUnixProcessID", .dest = "org.freedesktop.DBus", .sig = "s" };+ uint32_t s = dbus_send(&conn, &h, &b);+ dw_free(&b);+ if (!s) { on_notify(m); return 0; }+ pending[npending].serial = s;+ pending[npending].call = *m;+ npending++;+ return 1;+}++/* The bus's answer about a pending Notify. */+static void on_sender(const DMsg *m) {+ for (int i = 0; i < npending; i++) {+ if (pending[i].serial != m->reply_serial) continue;+ DMsg call = pending[i].call;+ pending[i] = pending[--npending];+ uint32_t pid = 0;+ if (m->type == DBUS_RETURN && m->sig && !strcmp(m->sig, "u")) { DRd r; dr_init(&r, m); pid = dr_u32(&r); if (r.err) pid = 0; }+ if (pid && pid_blocked(pid)) drop_notify(&call);+ else on_notify(&call);+ dbus_msg_free(&call);+ return;+ }+}++/* 1 if the message was kept (a Notify waiting for its sender), 0 if the caller frees it. */+static int handle(DMsg *m) {+ if ((m->type == DBUS_RETURN || m->type == DBUS_ERROR) && m->reply_serial) { on_sender(m); return 0; }+ if (m->type != DBUS_CALL || !m->member) return 0;if (m->iface && !strcmp(m->iface, "org.freedesktop.DBus.Introspectable")) {DBuf b;dw_init(&b);@@ -139,16 +225,16 @@ static void handle(const DMsg *m) {"</interface></node>");dbus_reply(&conn, m, "s", &b);dw_free(&b);- return;+ return 0;}if (!m->path || strcmp(m->path, OBJ) || (m->iface && strcmp(m->iface, BUS_NAME))) {dbus_error(&conn, m, "org.freedesktop.DBus.Error.UnknownObject", "no such object");- return;+ return 0;}const char *sig = m->sig ? m->sig : "";if (!strcmp(m->member, "GetCapabilities")) reply_caps(m);else if (!strcmp(m->member, "GetServerInformation")) reply_info(m);- else if (!strcmp(m->member, "Notify") && !strcmp(sig, "susssasa{sv}i")) on_notify(m);+ else if (!strcmp(m->member, "Notify") && !strcmp(sig, "susssasa{sv}i")) return ask_sender(m);else if (!strcmp(m->member, "CloseNotification") && !strcmp(sig, "u")) {DRd r;dr_init(&r, m);@@ -159,6 +245,7 @@ static void handle(const DMsg *m) {if (emit) emit("notify-close", j);notifyd_closed(id, 3);} else dbus_error(&conn, m, "org.freedesktop.DBus.Error.UnknownMethod", "unknown method");+ return 0;}int notifyd_start(NotifydEvent fn) {@@ -191,8 +278,14 @@ void notifyd_dispatch(void) {if (!up) return;DMsg m;int r;- while ((r = dbus_next(&conn, &m)) == 1) { handle(&m); dbus_msg_free(&m); }- if (r < 0 || dbus_flush(&conn) < 0) { fprintf(stderr, "notifyd: lost the session bus\n"); dbus_close(&conn); up = 0; }+ while ((r = dbus_next(&conn, &m)) == 1) { if (!handle(&m)) dbus_msg_free(&m); }+ if (r < 0 || dbus_flush(&conn) < 0) {+ fprintf(stderr, "notifyd: lost the session bus\n");+ for (int i = 0; i < npending; i++) dbus_msg_free(&pending[i].call);+ npending = 0;+ dbus_close(&conn);+ up = 0;+ }}void notifyd_closed(uint32_t id, uint32_t reason) {diff --git a/server/src/hw/notifyd.h b/server/src/hw/notifyd.hindex de7b119..663bb4b 100644--- a/server/src/hw/notifyd.h+++ b/server/src/hw/notifyd.h@@ -3,7 +3,13 @@* (per-app style, sound, Do Not Disturb). The banner is drawn by the theme (sdk/notify.js); this* only translates the protocol (spec 1.2): Notify, CloseNotification, GetCapabilities,* GetServerInformation, and the NotificationClosed / ActionInvoked signals. Body markup is reduced- * to plain text. Uses our own D-Bus client (vault/dbus.c). */+ * to plain text. Uses our own D-Bus client (vault/dbus.c).+ *+ * The firewall's "no notifications" (docs/FIREWALL.md, "Linux apps"): before showing a Notify,+ * the bus is asked for the caller's PID (GetConnectionUnixProcessID, answered by the bus from the+ * socket's credentials, not by the app), the PID's cgroup gives its app scope and so its firewall+ * id; if that app is restricted the notification is dropped and the call still gets a valid id,+ * so the app carries on as if it had been shown. Applies to the next notification. */#ifndef LORD_HW_NOTIFYD_H#define LORD_HW_NOTIFYD_H@@ -20,6 +26,10 @@ void notifyd_dispatch(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);/* Strips markup (<b>, <i>, <a href>, <img>...) and decodes & < > " '. */void notifyd_plain(const char *in, char *out, size_t n);diff --git a/server/tests/fwlinux/notify.sh b/server/tests/fwlinux/notify.shnew file mode 100755index 0000000..5488bd1--- /dev/null+++ b/server/tests/fwlinux/notify.sh@@ -0,0 +1,55 @@+#!/bin/bash+# notify.sh — "no notifications" for Linux apps: the notification server (src/hw/notifyd.c) asks+# the bus for the caller's PID, maps it to its app scope and drops the restricted app's+# notifications, still answering with a valid id. Runs the real server (build/notifyd-cli) on a+# private session bus and notify-send inside an app scope. Applies to the next notification.+#+# bash server/tests/fwlinux/notify.sh (needs dbus-daemon and notify-send: libnotify-bin)+. "$(dirname "$0")/lib.sh"+D=$(cd "$(dirname "$0")" && pwd)+CLI=$D/../../build/notifyd-cli+[ -x "$CLI" ] || { echo "missing build/notifyd-cli (make -C server test)"; exit 2; }+command -v notify-send >/dev/null || { echo "skipped: needs notify-send (libnotify-bin)"; exit 0; }+APP=desktop:fwnotify+SC=$(scope fwnotify)+BUS=$(mktemp -d)+dbus-daemon --session --address="unix:path=$BUS/bus" --fork --print-pid > "$BUS/pid"+export DBUS_SESSION_BUS_ADDRESS="unix:path=$BUS/bus"+OUT=$BUS/out+server() { "$CLI" "$1" > "$OUT" 2>&1 & for i in $(seq 1 30); do grep -q ready "$OUT" 2>/dev/null && break; sleep 0.1; done; }+send() { # send <title>: notify-send in the app's scope; prints its exit status and the id it got+ sudo env DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" HOME="$HOME" bash -c \+ 'echo $$ > "$0/cgroup.procs"; exec setpriv --reuid='"$U"' --regid='"$G"' --init-groups notify-send -p -a fwnotify "$1" body' "$SC" "$1" 2>&1+ echo "status=$?"+}+shown() { grep -c "\"title\":\"$1\"" "$OUT"; }++echo "1. not restricted: shown"+restrict $APP ""+server 6+r=$(send "visible-1")+[ "$(shown visible-1)" = 1 ] && ok "the notification was shown" || bad "not shown ($(cat "$OUT"))"+echo "$r" | grep -q "status=0" && ok "notify-send succeeded ($(echo $r | tr '\n' ' '))" || bad "notify-send failed: $r"+wait++echo "2. no-notifications: dropped, but the app gets a valid id"+restrict $APP no-notifications+server 6+r=$(send "hidden-1")+[ "$(shown hidden-1)" = 0 ] && ok "the notification was not shown" || bad "it was shown"+echo "$r" | grep -q "status=0" && ok "notify-send still succeeded" || bad "notify-send failed: $r"+id=$(echo "$r" | head -1)+[ "${id:-0}" -gt 0 ] 2>/dev/null && ok "it got a notification id ($id)" || bad "no id: $r"+other=$(notify-send -p -a outside "outside-1" body; echo "status=$?")+[ "$(shown outside-1)" = 1 ] && ok "a process outside the app's scope still notifies" || bad "outside process not shown"+wait++echo "3. lifted"+restrict $APP ""+server 5+send "visible-2" >/dev/null+[ "$(shown visible-2)" = 1 ] && ok "shown again" || bad "not shown"+wait+kill "$(cat "$BUS/pid")" 2>/dev/null+rm -rf "$BUS"+fwlinux_donediff --git a/server/tests/notifyd_cli.c b/server/tests/notifyd_cli.cnew file mode 100644index 0000000..62e95ab--- /dev/null+++ b/server/tests/notifyd_cli.c@@ -0,0 +1,43 @@+/* notifyd_cli.c — the real org.freedesktop.Notifications server (src/hw/notifyd.c) outside the+ * desktop, for tests/fwlinux/notify.sh.+ *+ * DBUS_SESSION_BUS_ADDRESS=<a private test bus> build/notifyd-cli <seconds>+ *+ * Loads ~/.config/lordos/firewall.json into the store lord.fw uses (sec/restrict.h), owns the+ * name on that bus and prints one line per notification it would show ("notify {...}") or close.+ * A test tool: never installed. */+#include "hw/notifyd.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; }++static void on_event(const char *kind, const char *json) { printf("%s %s\n", kind, json); fflush(stdout); }++int main(int argc, char **argv) {+ int secs = argc > 1 ? atoi(argv[1]) : 10;+ const char *home = getenv("HOME");+ char path[4096], err[200] = "";+ snprintf(path, sizeof path, "%s/.config/lordos/firewall.json", home ? home : "/nonexistent");+ FwStore *s = fw_store_new();+ if (!s || fw_load(s, path, err, sizeof err) < 0) fprintf(stderr, "notifyd-cli: %s: %s (no restrictions)\n", path, err);+ restrict_set_store(s);+ if (notifyd_start(on_event)) { fprintf(stderr, "notifyd-cli: could not own the name\n"); return 1; }+ printf("ready\n");+ fflush(stdout);+ time_t end = time(NULL) + secs;+ while (time(NULL) < end) {+ struct pollfd p = { .fd = notifyd_fd(), .events = POLLIN | (notifyd_wants_write() ? POLLOUT : 0) };+ if (p.fd < 0) return 1;+ if (poll(&p, 1, 200) > 0) notifyd_dispatch();+ }+ return 0;+}