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.

Notifications: only the bus answers who sent a Notify, and failures drop it

Federico Pereirabff68638feae
diff --git a/server/Makefile b/server/Makefile
index 2694a55..f82389a 100644
--- a/server/Makefile
+++ b/server/Makefile
@@ -247,7 +247,11 @@ build/launch-cli: tests/launch_cli.c $(LAUNCH_CLI_SRC) src/apps.h $(wildcard src
$(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
+test: build/notifyd-cli build/notifyd-test
+build/notifyd-test: tests/notifyd_test.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_test.c $(NOTIFYD_CLI_SRC)
+ ./$@ || { rm -f $@; exit 1; }
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)
diff --git a/server/src/hw/hw.c b/server/src/hw/hw.c
index 4ca7fea..e7ba7fc 100644
--- a/server/src/hw/hw.c
+++ b/server/src/hw/hw.c
@@ -65,6 +65,7 @@ void hw_dispatch(const struct pollfd *p, int n) {
void hw_events(void) {
bt_flush_events();
+ notifyd_tick();
if (sysbus_up() && sysbus_wants_write()) dbus_flush(sysbus_conn());
}
diff --git a/server/src/hw/notifyd.c b/server/src/hw/notifyd.c
index 4781320..e0a5798 100644
--- a/server/src/hw/notifyd.c
+++ b/server/src/hw/notifyd.c
@@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#define BUS_NAME "org.freedesktop.Notifications"
#define OBJ "/org/freedesktop/Notifications"
@@ -19,8 +20,12 @@ 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];
+#define PENDING_MS 3000
+static struct { uint32_t serial; DMsg call; double since; } pending[MAX_PENDING];
static int npending;
+static int (*app_of_pid)(uint32_t pid, char *out, size_t n);
+
+static double mono_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
int notifyd_fd(void) { return up ? conn.fd : -1; }
int notifyd_wants_write(void) { return up && dbus_pending_out(&conn); }
@@ -147,17 +152,30 @@ int notifyd_app_of_cgroup(const char *path, char *out, size_t n) {
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];
+/* Firewall id of a PID's app scope, from /proc/<pid>/cgroup. 0 ok, 1 not in an app's scope, -1
+ * the process is gone (or unreadable). */
+static int app_of_pid_proc(uint32_t pid, char *out, size_t n) {
+ char f[64], line[1024];
snprintf(f, sizeof f, "/proc/%u/cgroup", pid);
FILE *fp = fopen(f, "re");
- if (!fp) return 0;
- int blocked = 0;
+ if (!fp) return -1;
+ int r = -1;
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; }
+ if (!strncmp(line, "0::", 3)) { r = notifyd_app_of_cgroup(line + 3, out, n) ? 1 : 0; break; }
fclose(fp);
- return blocked;
+ return r;
+}
+
+void notifyd_set_app_of_pid(int (*fn)(uint32_t pid, char *out, size_t n)) { app_of_pid = fn; }
+
+/* Shown or not: only a PID the bus gave us whose process is outside any restricted app's scope.
+ * Fails closed: no PID, or a process we can't read (it already exited), is not shown. */
+static int pid_blocked(uint32_t pid) {
+ char app[FW_APP_MAX];
+ if (!pid) return 1;
+ int r = (app_of_pid ? app_of_pid : app_of_pid_proc)(pid, app, sizeof app);
+ if (r < 0) return 1;
+ return r == 0 && restrict_blocks(app, FW_R_NO_NOTIFICATIONS);
}
/* A restricted app's notification: not shown, but answered with a real id (and closed at once),
@@ -193,19 +211,23 @@ static int ask_sender(DMsg *m) {
if (!s) { on_notify(m); return 0; }
pending[npending].serial = s;
pending[npending].call = *m;
+ pending[npending].since = mono_ms();
npending++;
return 1;
}
-/* The bus's answer about a pending Notify. */
+/* The bus's answer about a pending Notify. Only the bus itself may answer (its sender can't be
+ * forged: the bus daemon sets it); a reply from anyone else is ignored. An error, an odd answer
+ * or no answer within PENDING_MS: the notification is dropped (with a valid id). */
static void on_sender(const DMsg *m) {
+ if (!m->sender || strcmp(m->sender, "org.freedesktop.DBus")) return;
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);
+ if (pid_blocked(pid)) drop_notify(&call);
else on_notify(&call);
dbus_msg_free(&call);
return;
@@ -274,11 +296,27 @@ int notifyd_start(NotifydEvent fn) {
return 0;
}
+void notifyd_tick(void) {
+ if (!up || !npending) return;
+ double now = mono_ms();
+ int dropped = 0;
+ for (int i = 0; i < npending;) { /* the bus never answered: fail closed */
+ if (now - pending[i].since < PENDING_MS) { i++; continue; }
+ DMsg call = pending[i].call;
+ pending[i] = pending[--npending];
+ drop_notify(&call);
+ dbus_msg_free(&call);
+ dropped = 1;
+ }
+ if (dropped) dbus_flush(&conn);
+}
+
void notifyd_dispatch(void) {
if (!up) return;
DMsg m;
int r;
while ((r = dbus_next(&conn, &m)) == 1) { if (!handle(&m)) dbus_msg_free(&m); }
+ notifyd_tick();
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);
diff --git a/server/src/hw/notifyd.h b/server/src/hw/notifyd.h
index 663bb4b..b0d9535 100644
--- a/server/src/hw/notifyd.h
+++ b/server/src/hw/notifyd.h
@@ -9,7 +9,9 @@
* 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. */
+ * so the app carries on as if it had been shown. Only the bus's own answer counts; an error, a
+ * process that can't be read or no answer within 3 s also drop it (fail closed). Applies to the
+ * next notification. */
#ifndef LORD_HW_NOTIFYD_H
#define LORD_HW_NOTIFYD_H
@@ -22,6 +24,8 @@ int notifyd_start(NotifydEvent fn); /* 0 if we own the name */
int notifyd_fd(void);
int notifyd_wants_write(void);
void notifyd_dispatch(void);
+/* Called on every pass of the main loop: drops Notify calls the bus didn't answer in time. */
+void notifyd_tick(void);
/* From the theme: the banner went away (reason: 1 expired, 2 dismissed, 3 closed by a call) or
* an action was clicked. */
void notifyd_closed(uint32_t id, uint32_t reason);
@@ -30,6 +34,9 @@ void notifyd_action(uint32_t id, const char *key);
* "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);
+/* Tests only: replaces the PID → firewall id lookup (/proc/<pid>/cgroup). fn returns 0 with the
+ * id, 1 if the PID is in no app's scope, -1 if it can't tell. */
+void notifyd_set_app_of_pid(int (*fn)(uint32_t pid, char *out, size_t n));
/* Strips markup (<b>, <i>, <a href>, <img>...) and decodes &amp; &lt; &gt; &quot; &apos;. */
void notifyd_plain(const char *in, char *out, size_t n);
diff --git a/server/tests/notifyd_test.c b/server/tests/notifyd_test.c
new file mode 100644
index 0000000..c635dac
--- /dev/null
+++ b/server/tests/notifyd_test.c
@@ -0,0 +1,229 @@
+/* notifyd_test.c — the notification server's "no notifications" check (src/hw/notifyd.c) against a
+ * fake session bus, so the cases a real bus never produces can be forced:
+ * - an unrestricted app's notification is shown, a restricted one's is dropped with a valid id;
+ * - a reply to the PID question forged by another client (sender not org.freedesktop.DBus) is
+ * ignored; the bus's own answer decides;
+ * - an error reply, a PID that can't be resolved, and no reply at all (3 s) drop it.
+ * The test is the bus (a unix socket in /tmp); notifyd runs in a child with the PID → app lookup
+ * replaced (100 = desktop:bad, restricted; 200 = desktop:good; anything else unknown). */
+#include "hw/notifyd.h"
+#include "fw/rules.h"
+#include "sec/broker.h"
+#include "sec/perms.h"
+#include "sec/restrict.h"
+#include "vault/dbus.h"
+
+#include <poll.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.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 int fails, total;
+static void check(int ok, const char *what) { total++; if (!ok) { fails++; printf("FAIL: %s\n", what); } }
+
+/* ---- the notifyd side (child) ---- */
+static int shown_fd;
+static void on_event(const char *kind, const char *json) {
+ if (strcmp(kind, "notify")) return;
+ const char *t = strstr(json, "\"title\":\"");
+ if (!t) return;
+ t += 9;
+ const char *e = strchr(t, '"');
+ if (e && write(shown_fd, t, (size_t)(e - t)) < 0) {}
+ if (write(shown_fd, "\n", 1) < 0) {}
+}
+static int fake_app_of_pid(uint32_t pid, char *out, size_t n) {
+ if (pid == 100) { snprintf(out, n, "desktop:bad"); return 0; }
+ if (pid == 200) { snprintf(out, n, "desktop:good"); return 0; }
+ return -1;
+}
+static void run_notifyd(int secs) {
+ FwStore *s = fw_store_new();
+ fw_set_restricted(s, "desktop:bad", FW_R_NO_NOTIFICATIONS);
+ restrict_set_store(s);
+ notifyd_set_app_of_pid(fake_app_of_pid);
+ if (notifyd_start(on_event)) _exit(3);
+ 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) _exit(4);
+ if (poll(&p, 1, 100) > 0) notifyd_dispatch();
+ notifyd_tick();
+ }
+ _exit(0);
+}
+
+/* ---- the bus side (this process) ---- */
+static DConn bus;
+static uint32_t bserial = 100;
+static void bus_send(DHdr *h, DBuf *b) { h->serial = ++bserial; dbus_send(&bus, h, b); dbus_flush(&bus); }
+
+static int read_line(int fd, char *out, size_t cap) {
+ size_t n = 0;
+ while (n + 1 < cap) {
+ struct pollfd p = { fd, POLLIN, 0 };
+ if (poll(&p, 1, 5000) <= 0 || read(fd, out + n, 1) != 1) return -1;
+ n++;
+ if (n >= 2 && out[n - 2] == '\r' && out[n - 1] == '\n') { out[n - 2] = 0; return 0; }
+ }
+ return -1;
+}
+
+/* next message from notifyd within ms (1), or 0 */
+static int next(DMsg *m, int ms) {
+ for (int i = 0; i < ms / 20; i++) {
+ dbus_flush(&bus);
+ int r = dbus_next(&bus, m);
+ if (r < 0) return 0;
+ if (r == 1) return 1;
+ struct pollfd p = { bus.fd, POLLIN, 0 };
+ poll(&p, 1, 20);
+ }
+ return 0;
+}
+
+static void reply_to(const DMsg *call, const char *sig, DBuf *b, const char *sender) {
+ DHdr h = { .type = DBUS_RETURN, .reply_serial = call->serial, .dest = ":1.1", .sig = sig, .sender = sender };
+ bus_send(&h, b);
+}
+
+/* sends Notify(title) from :1.9 and returns its serial */
+static uint32_t notify(const char *title) {
+ DBuf b;
+ dw_init(&b);
+ dw_str(&b, "test"); dw_u32(&b, 0); dw_str(&b, ""); dw_str(&b, title); dw_str(&b, "body");
+ size_t a = dw_arr_begin(&b, 4); dw_arr_end(&b, a);
+ a = dw_arr_begin(&b, 8); dw_arr_end(&b, a);
+ dw_i32(&b, -1);
+ DHdr h = { .type = DBUS_CALL, .path = "/org/freedesktop/Notifications", .iface = "org.freedesktop.Notifications",
+ .member = "Notify", .dest = ":1.1", .sig = "susssasa{sv}i", .sender = ":1.9" };
+ bus_send(&h, &b);
+ dw_free(&b);
+ return bserial;
+}
+
+/* waits for notifyd's GetConnectionUnixProcessID; returns its serial (0 if none) */
+static uint32_t pid_question(void) {
+ DMsg m;
+ while (next(&m, 2000)) {
+ uint32_t s = m.type == DBUS_CALL && m.member && !strcmp(m.member, "GetConnectionUnixProcessID") ? m.serial : 0;
+ dbus_msg_free(&m);
+ if (s) return s;
+ }
+ return 0;
+}
+
+static void pid_answer(uint32_t q, uint32_t pid, const char *sender) {
+ DMsg fake = { 0 };
+ fake.serial = q;
+ DBuf b;
+ dw_init(&b);
+ dw_u32(&b, pid);
+ reply_to(&fake, "u", &b, sender);
+ dw_free(&b);
+}
+
+/* waits for the answer to Notify serial: the id (0 = none, or an error) */
+static uint32_t notify_answer(uint32_t serial, int ms) {
+ DMsg m;
+ while (next(&m, ms)) {
+ uint32_t id = 0;
+ int mine = (m.type == DBUS_RETURN || m.type == DBUS_ERROR) && m.reply_serial == serial;
+ if (mine && m.type == DBUS_RETURN && m.sig && !strcmp(m.sig, "u")) { DRd r; dr_init(&r, &m); id = dr_u32(&r); }
+ dbus_msg_free(&m);
+ if (mine) return id;
+ }
+ return 0;
+}
+
+int main(void) {
+ setvbuf(stdout, NULL, _IONBF, 0);
+ char path[] = "/tmp/notifyd-test-XXXXXX";
+ if (!mkdtemp(path)) return 2;
+ char sock[100], addr[120];
+ snprintf(sock, sizeof sock, "%s/bus", path);
+ snprintf(addr, sizeof addr, "unix:path=%s", sock);
+ int l = socket(AF_UNIX, SOCK_STREAM, 0);
+ struct sockaddr_un sa = { .sun_family = AF_UNIX };
+ snprintf(sa.sun_path, sizeof sa.sun_path, "%s", sock);
+ if (l < 0 || bind(l, (struct sockaddr *)&sa, sizeof sa) || listen(l, 1)) return 2;
+ int pipefd[2];
+ if (pipe(pipefd)) return 2;
+ setenv("DBUS_SESSION_BUS_ADDRESS", addr, 1);
+ pid_t child = fork();
+ if (child == 0) { close(pipefd[0]); shown_fd = pipefd[1]; close(l); run_notifyd(15); }
+ close(pipefd[1]);
+ int fd = accept(l, NULL, NULL);
+ char line[256], nul;
+ check(fd >= 0 && read(fd, &nul, 1) == 1 && !read_line(fd, line, sizeof line), "auth line");
+ check(write(fd, "OK 0123456789abcdef0123456789abcdef\r\n", 37) == 37, "auth ok");
+ check(!read_line(fd, line, sizeof line) && !strcmp(line, "BEGIN"), "BEGIN");
+ memset(&bus, 0, sizeof bus);
+ bus.fd = fd;
+ for (int step = 0; step < 2; step++) { /* Hello, RequestName */
+ DMsg m;
+ if (!next(&m, 3000)) { check(0, "Hello/RequestName"); break; }
+ DBuf b;
+ dw_init(&b);
+ if (!strcmp(m.member, "Hello")) { dw_str(&b, ":1.1"); reply_to(&m, "s", &b, "org.freedesktop.DBus"); }
+ else { dw_u32(&b, 1); reply_to(&m, "u", &b, "org.freedesktop.DBus"); }
+ dw_free(&b);
+ dbus_msg_free(&m);
+ }
+
+ usleep(300 * 1000); /* notifyd_start's wait for RequestName is over */
+
+ /* 1. unrestricted: shown */
+ uint32_t n = notify("good"), q = pid_question();
+ check(q != 0, "1: notifyd asks the bus for the sender's PID");
+ pid_answer(q, 200, "org.freedesktop.DBus");
+ check(notify_answer(n, 2000) > 0, "1: Notify answered with an id");
+
+ /* 2. restricted: answered, not shown */
+ n = notify("bad"); q = pid_question();
+ pid_answer(q, 100, "org.freedesktop.DBus");
+ check(notify_answer(n, 2000) > 0, "2: restricted Notify still gets an id");
+
+ /* 3. a forged reply from another client with an unrestricted PID is ignored */
+ n = notify("spoof"); q = pid_question();
+ pid_answer(q, 200, ":1.9");
+ pid_answer(q, 100, "org.freedesktop.DBus");
+ check(notify_answer(n, 2000) > 0, "3: answered after the bus's own reply");
+
+ /* 4. an error reply from the bus: dropped, still an id */
+ n = notify("error"); q = pid_question();
+ { DHdr h = { .type = DBUS_ERROR, .reply_serial = q, .dest = ":1.1", .error = "org.freedesktop.DBus.Error.NameHasNoOwner", .sender = "org.freedesktop.DBus" };
+ bus_send(&h, NULL); }
+ check(notify_answer(n, 2000) > 0, "4: error reply: answered with an id");
+
+ /* 5. a PID that can't be resolved (process gone): dropped */
+ n = notify("gone"); q = pid_question();
+ pid_answer(q, 999, "org.freedesktop.DBus");
+ check(notify_answer(n, 2000) > 0, "5: unknown PID: answered with an id");
+
+ /* 6. no reply at all: after 3 s, dropped with an id */
+ n = notify("silent"); q = pid_question();
+ check(q != 0, "6: asked");
+ check(notify_answer(n, 5000) > 0, "6: no reply: answered with an id after the timeout");
+
+ kill(child, SIGTERM);
+ waitpid(child, NULL, 0);
+ char shown[512] = "";
+ ssize_t r = read(pipefd[0], shown, sizeof shown - 1);
+ shown[r > 0 ? r : 0] = 0;
+ check(!strcmp(shown, "good\n"), "only the unrestricted notification was shown");
+ if (strcmp(shown, "good\n")) printf(" shown: %s\n", shown);
+ unlink(sock);
+ rmdir(path);
+ printf("notifyd-test: %d/%d passed\n", total - fails, total);
+ return fails ? 1 : 0;
+}