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
Xwayland security fixes: no X display for restricted apps, selection offers bound to their generation and focus, X paste only for the focused client, secrets never exported to X; shell overlays take the keyboard from Linux apps; Java non-reparenting; xwm-test and selections test
Federico Pereira077051e8d2e8
diff --git a/runtime/index.js b/runtime/index.jsindex c28687f..68c183a 100644--- a/runtime/index.js+++ b/runtime/index.js@@ -114,11 +114,11 @@ const host = {},appendInitialChild(parent, child) { lord.appendChild(parent.id, child.id); attach(parent, child); },appendChild(parent, child) { lord.appendChild(parent.id, child.id); attach(parent, child); },- appendChildToContainer(container, child) { lord.appendChild(container.id, child.id); attach(container, child); },+ appendChildToContainer(container, child) { lord.appendChild(container.id, child.id); attach(container, child); layerCount(container, 1); },insertBefore(parent, child, before) { lord.insertBefore(parent.id, child.id, before.id); attach(parent, child); },- insertInContainerBefore(container, child, before) { lord.insertBefore(container.id, child.id, before.id); attach(container, child); },+ insertInContainerBefore(container, child, before) { lord.insertBefore(container.id, child.id, before.id); attach(container, child); layerCount(container, 1); },removeChild(parent, child) { lord.removeChild(parent.id, child.id); },- removeChildFromContainer(container, child) { lord.removeChild(container.id, child.id); },+ removeChildFromContainer(container, child) { lord.removeChild(container.id, child.id); layerCount(container, -1); },detachDeletedInstance(inst) { unregister(inst); lord.destroy(inst.id); },clearContainer() {},@@ -137,7 +137,7 @@ const host = {getChildHostContext: (ctx) => ctx,getPublicInstance: (inst) => inst,prepareForCommit: () => null,- resetAfterCommit() {},+ resetAfterCommit() { layersCommitted(); },preparePortalMount() {},setCurrentUpdatePriority(p) { updatePriority = p; },getCurrentUpdatePriority: () => updatePriority,@@ -175,6 +175,28 @@ const host = {const reconciler = Reconciler(host);+// ---- what is open in the layers that take the keyboard ----+// The search, the app launcher, shell sheets and system prompts (keyring, permissions, the file+// chooser) live in the overlay, modal and system layers (sdk/layers.js). While any of them shows+// something, the keyboard is LordOS's: no Linux app (Wayland or X11) gets the keys typed into it+// (sdk/wayland.js asks the compositor; docs/PRIVACY.md).+const KEYBOARD_LAYERS = new Set([40, 60, 100]); // overlay, modal, system+let keyboardBusy = false;+const keyboardWatchers = new Set();+function layerCount(container, d) {+ if (!container.layer) return;+ container.count = Math.max(0, (container.count || 0) + d);+}+function layersCommitted() {+ let busy = false;+ for (const [z, c] of layerNodes) if (KEYBOARD_LAYERS.has(z) && c.count > 0) { busy = true; break; }+ if (busy === keyboardBusy) return;+ keyboardBusy = busy;+ for (const fn of keyboardWatchers) { try { fn(busy); } catch (e) { console.error(e); } }+}+export const shellHasKeyboard = () => keyboardBusy;+export function onShellKeyboard(fn) { keyboardWatchers.add(fn); return () => keyboardWatchers.delete(fn); }+// ---- layers: one stacking order for the whole screen (sdk/layers.js, docs/LAYERS.md) ----// Each layer is a root node of the screen's size, above the React tree (zIndex > 0) and// pointerEvents 'box-none' (lord-server never hits the layer itself, only what is in it), so paint@@ -188,7 +210,7 @@ export function layerContainer(z) {for (const [k, v] of [['position', 'absolute'], ['left', 0], ['top', 0], ['width', screen.width], ['height', screen.height],['pointerEvents', 'box-none'], ['zIndex', z]]) lord.setStyle(id, k, v);lord.appendChild(rootNode, id);- c = { id, isContainer: true };+ c = { id, isContainer: true, layer: z, count: 0 };layerNodes.set(z, c);}return c;diff --git a/sdk/wayland.js b/sdk/wayland.jsindex c11a7bd..2f9a854 100644--- a/sdk/wayland.js+++ b/sdk/wayland.js@@ -6,7 +6,7 @@// el suyo (GTK) van "sin marco": args.csd = true, y el tema dibuja solo el contenido. Mover y// cambiar el tamaño desde la barra de una app GTK llega como pedido del compositor.import { createElement as h } from 'react';-import { View, Text, Surface, addListener } from '@lordos/runtime';+import { View, Text, Surface, addListener, onShellKeyboard, shellHasKeyboard } from '@lordos/runtime';import { windows, wm, registerBuiltinApp, windowChrome, pointerWorkArea, getApp } from './windows.js';import { useAppWindow } from './components.jsx';import { locale, appName } from './i18n.js';@@ -107,7 +107,8 @@ function sync() {const list = windows.get();const vis = list.filter((w) => !w.minimized);const top = vis.length ? vis[vis.length - 1] : null;- const f = top ? surfaceOf(top.id) : 0;+ // search, the launcher, a sheet or a system prompt is open: the keys are LordOS's, not the app's+ const f = top && !shellHasKeyboard() ? surfaceOf(top.id) : 0;if (f !== lastFocus) { lastFocus = f; lord.wayland.focus(f); }for (const w of list) {const s = surfaceOf(w.id);@@ -117,6 +118,7 @@ function sync() {}}windows.subscribe(sync);+if (typeof onShellKeyboard === 'function') onShellKeyboard(() => sync());export const wayland = {info: () => (typeof lord.wayland !== 'undefined' ? lord.wayland.info() : { socket: '', windows: 0 }),diff --git a/server/Makefile b/server/Makefileindex 900ac30..8691de6 100644--- a/server/Makefile+++ b/server/Makefile@@ -222,6 +222,12 @@ build/codesig-test: tests/codesig_test.c src/sec/codesig.c src/sec/json.c src/pk$(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/codesig_test.c src/sec/codesig.c src/sec/json.c src/pkg/crypto.c./$@ ../tools/release/lordos-release.pub.pem || { rm -f $@; exit 1; }# Clock time zones: TZif and POSIX rules against glibc (docs/SYSTEM.md)+# tests/xwm_test.c: the X11 wire helpers of the window manager (src/wl/x11wire.c, docs/WAYLAND.md)+test: build/xwm-test+build/xwm-test: tests/xwm_test.c src/wl/x11wire.c src/wl/x11wire.h+ @mkdir -p build+ $(CC) $(CPPFLAGS) $(CFLAGS) $(WARN) -std=c11 -o $@ tests/xwm_test.c src/wl/x11wire.c+ ./$@ || { rm -f $@; exit 1; }test: build/tzif-testbuild/tzif-test: tests/tzif_test.c src/ui/tzif.c src/ui/tzif.h@mkdir -p build@@ -364,7 +370,7 @@ build/net-test: tests/net_test.c $(NET_SRC) $(PKG_SRC) build/obj/ui/stb.o src/ne# Test Wayland app with dmabuf (udmabuf): see docs/GPU.md. Needs libwayland-client.CLIENT_PROTO := stable/xdg-shell/xdg-shell unstable/xdg-decoration/xdg-decoration-unstable-v1 stable/linux-dmabuf/linux-dmabuf-v1 stable/presentation-time/presentation-time \unstable/relative-pointer/relative-pointer-unstable-v1 unstable/pointer-constraints/pointer-constraints-unstable-v1 \- unstable/pointer-gestures/pointer-gestures-unstable-v1+ unstable/pointer-gestures/pointer-gestures-unstable-v1 unstable/primary-selection/primary-selection-unstable-v1CLIENT_GEN := $(foreach p,$(CLIENT_PROTO),build/gen/client/$(notdir $(p))-client-protocol.h build/gen/client/$(notdir $(p))-protocol.c)define wl_client_protobuild/gen/client/$(notdir $(1))-client-protocol.h: $(WLPROTO)/$(1).xml@@ -377,6 +383,9 @@ endef$(foreach p,$(CLIENT_PROTO),$(eval $(call wl_client_proto,$(p))))build/dmabuf-client: tests/dmabuf_client.c $(CLIENT_GEN)$(CC) $(CFLAGS) -D_GNU_SOURCE -Ibuild/gen $(WARN) -std=c11 -o $@ tests/dmabuf_client.c $(filter %.c,$(CLIENT_GEN)) -lwayland-client+# Clipboard and primary-selection offers bound to their selection (tests/xwayland/clip_offers.sh, docs/WAYLAND.md)+build/clip-offers: tests/clip_offers.c $(CLIENT_GEN)+ $(CC) $(CFLAGS) -D_GNU_SOURCE -Ibuild/gen $(WARN) -std=c11 -o $@ tests/clip_offers.c $(filter %.c,$(CLIENT_GEN)) -lwayland-client# off/on-monitor cycles against one session (tests/gpu_cycle.sh, docs/GPU.md); a test tool, never installedbuild/seat-cycle: tests/seat_cycle.c@mkdir -p builddiff --git a/server/src/app/appspawn.c b/server/src/app/appspawn.cindex a91e62b..e6b4887 100644--- a/server/src/app/appspawn.c+++ b/server/src/app/appspawn.c@@ -97,6 +97,7 @@ int app_spawn(const char *id) {if (null >= 0) { dup2(null, 0); if (null > 2) close(null); }/* lord-server passes the id, already validated; there are no caller arguments */char *const argv[] = { self, (char *)"--app", (char *)id, NULL };+ unsetenv("DISPLAY"); /* no X11 for isolated apps (their seccomp has no unix socket() either) */execve("/proc/self/exe", argv, environ); /* not `self`: after a package upgrade it is "<path> (deleted)" */_exit(127);}diff --git a/server/src/apps.c b/server/src/apps.cindex bbe7452..b987c78 100644--- a/server/src/apps.c+++ b/server/src/apps.c@@ -555,17 +555,6 @@ static int grant_ok(const char *path) {return 1;}-/* The session's X socket, from DISPLAY=":<n>" (set by wl/xwayland.c): kept in a restricted app's- * private /tmp so an X11 app still opens there. NULL without one. */-static const char *x11_socket(void) {- static char path[40];- const char *d = getenv("DISPLAY");- if (!d || d[0] != ':' || !d[1] || strlen(d) > 4) return NULL;- for (const char *q = d + 1; *q; q++) if (*q < '0' || *q > '9') return NULL;- snprintf(path, sizeof path, "/tmp/.X11-unix/X%s", d + 1);- return path;-}-static int is_dir(const char *p) { struct stat st; return !lstat(p, &st) && S_ISDIR(st.st_mode); }/* What a restricted app may touch (see sandbox.h). file: the realpath it was opened with, or NULL. */@@ -593,7 +582,6 @@ static FilesPlan *files_plan(const DesktopApp *a, const char *file, const char *p->cfg.sockets[ns++] = "pipewire-0";p->cfg.sockets[ns++] = "pulse/native";p->cfg.sockets[ns] = NULL;- p->cfg.x11_socket = x11_socket(); /* X11 apps (Xwayland) */char path[PATH_MAX];/* its own folders, named exactly after its canonical id, created if missing */if (own_dir_ok(a->id)) {@@ -669,7 +657,6 @@ static FilesPlan *view_plan(const char *cwd) {p->cfg.sockets[ns++] = "pipewire-0"; /* playback; capture goes through the PipeWire guard */p->cfg.sockets[ns++] = "pulse/native";p->cfg.sockets[ns] = NULL;- p->cfg.x11_socket = x11_socket(); /* X11 apps (Xwayland) */if (cwd && cwd[0] == '/') { snprintf(p->cwd, sizeof p->cwd, "%s", cwd); p->cfg.cwd = p->cwd; }return p;}@@ -827,6 +814,10 @@ static int launch(const char *id, const char *file, const char *action) {_exit(126);}/* its own view (namespaces: no session sockets, only its folders), then Landlock */+ /* a restricted app gets no X display: X11 apps share one X server and can watch each+ * other's windows, input and selections (docs/PRIVACY.md, "X11 apps"); its private /tmp has no+ * X socket either */+ if (plan) unsetenv("DISPLAY");if (plan && (sandbox_files_isolate(&plan->cfg) ||(plan->cfg.keep_home ? sandbox_apply_scope() : sandbox_apply_files(&plan->cfg)))) {static const char msg[] = "apps: could not apply the no-files restriction: not launched\n";diff --git a/server/src/sec/sandbox.c b/server/src/sec/sandbox.cindex 676cf5f..3338d0a 100644--- a/server/src/sec/sandbox.c+++ b/server/src/sec/sandbox.c@@ -388,12 +388,6 @@ int sandbox_files_isolate(const SandboxFilesCfg *cfg) {keep[nk].fd = t; keep[nk].dir = 0; keep[nk].path = sockpath[nsock]; keep[nk].ro = 0; nk++;nsock++;}- /* X11 apps: the session's X socket (Xwayland), the only thing kept in the private /tmp */- if (cfg->x11_socket && !strncmp(cfg->x11_socket, "/tmp/.X11-unix/X", 16)) {- struct stat st;- int t = !lstat(cfg->x11_socket, &st) && S_ISSOCK(st.st_mode) ? (int)syscall(SYS_open_tree, AT_FDCWD, cfg->x11_socket, OPEN_TREE_CLONE | O_CLOEXEC) : -1;- if (t >= 0) { keep[nk].fd = t; keep[nk].dir = 0; keep[nk].path = cfg->x11_socket; keep[nk].ro = 0; nk++; }- }int bad = 0;const char *opts = "mode=0700,size=64m";if (!cfg->keep_home) bad |= mount("tmpfs", cfg->home, "tmpfs", MS_NOSUID | MS_NODEV, opts);diff --git a/server/src/sec/sandbox.h b/server/src/sec/sandbox.hindex 4ece0be..d55aac6 100644--- a/server/src/sec/sandbox.h+++ b/server/src/sec/sandbox.h@@ -89,7 +89,6 @@ typedef struct {const char *rw[SANDBOX_FILES_MAX + 1]; /* NULL-terminated */const char *ro[SANDBOX_FILES_MAX + 1];const char *sockets[8]; /* names inside runtime_dir kept visible (wayland-0...), NULL-terminated */- const char *x11_socket; /* the session's X socket (/tmp/.X11-unix/X<n>), kept in the private /tmp; or NULL */const char *cwd; /* the .desktop's Path= (or NULL: the home folder), entered inside the view *//* the file or folder the app was opened with ("Open with"; also in rw[] or ro[]): opened* again without following any symlink and only if it is still the same inode the launcher checked */diff --git a/server/src/wl/wl.c b/server/src/wl/wl.cindex dce827f..8e2cdef 100644--- a/server/src/wl/wl.c+++ b/server/src/wl/wl.c@@ -1083,6 +1083,10 @@ void wl_keymap_changed(void) {#define CLIP_MAX (16u << 20)typedef struct { struct wl_resource *res; char mimes[MAX_MIMES][64]; int nmimes; } Source;static Source *selection; /* source of the app owning the selection, or NULL (LordOS) */+/* Bumped whenever the selection changes: an offer is only good for the selection it was made for,+ * and only while its client has the keyboard (an app can't keep an old offer to read later copies). */+static unsigned clip_offer_gen = 1;+typedef struct { Source *src; unsigned gen; } OfferRef;static unsigned clip_seen; /* LordOS clipboard generation already offered */static unsigned char *clip_png; /* last copied image */static size_t clip_png_len;@@ -1110,6 +1114,7 @@ static void src_gone(struct wl_resource *r) {Source *s = wl_resource_get_user_data(r);if (selection == s) {selection = NULL;+ clip_offer_gen++;if (clip_is_image) offer_to_focus(); /* the image stays, now LordOS offers it */}free(s);@@ -1246,8 +1251,9 @@ static void ofr_accept(struct wl_client *c, struct wl_resource *r, uint32_t seri(void)c; (void)r; (void)serial; (void)mime;}static void ofr_receive(struct wl_client *c, struct wl_resource *r, const char *mime, int32_t fd) {- (void)c;- Source *src = wl_resource_get_user_data(r);+ OfferRef *ref = wl_resource_get_user_data(r);+ if (!ref || ref->gen != clip_offer_gen || !key_focus || key_focus->client != c) { close(fd); return; } /* stale, or not focused */+ Source *src = ref->src;if (src && src == selection) { wl_data_source_send_send(src->res, mime, fd); close(fd); return; }if (!src && clip_is_image && clip_png && !strcmp(mime, "image/png")) { send_bytes(fd, clip_png, clip_png_len); return; }if (!src && !clip_is_image && text_mime(mime)) { const char *t = term_clipboard(); send_bytes(fd, t, strlen(t)); return; }@@ -1262,6 +1268,8 @@ static const struct wl_data_offer_interface offer_impl = {.accept = ofr_accept, .receive = ofr_receive, .destroy = ofr_destroy, .finish = ofr_finish, .set_actions = ofr_actions,};+static void offer_gone(struct wl_resource *r) { free(wl_resource_get_user_data(r)); }+static void offer_selection(struct wl_resource *dev) {struct wl_client *c = wl_resource_get_client(dev);if (selection && wl_resource_get_client(selection->res) == c) return; /* it is its own */@@ -1270,7 +1278,11 @@ static void offer_selection(struct wl_resource *dev) {if (!selection && !image && !t[0]) { wl_data_device_send_selection(dev, NULL); return; }struct wl_resource *o = wl_resource_create(c, &wl_data_offer_interface, wl_resource_get_version(dev), 0);if (!o) return;- wl_resource_set_implementation(o, &offer_impl, selection, NULL);+ OfferRef *ref = malloc(sizeof *ref);+ if (!ref) { wl_resource_destroy(o); return; }+ ref->src = selection;+ ref->gen = clip_offer_gen;+ wl_resource_set_implementation(o, &offer_impl, ref, offer_gone);wl_data_device_send_data_offer(dev, o);if (selection) for (int i = 0; i < selection->nmimes; i++) wl_data_offer_send_offer(o, selection->mimes[i]);else if (image) wl_data_offer_send_offer(o, "image/png");@@ -1323,6 +1335,7 @@ int wl_clip_use_image(const char *path) {close(fd);if (!buf || got != (size_t)st.st_size) { free(buf); return -1; }if (selection) { wl_data_source_send_cancelled(selection->res); selection = NULL; }+ clip_offer_gen++;store_png(buf, got);snprintf(clip_img_origin, sizeof clip_img_origin, "lordos");clip_img_sensitive = 0;@@ -1347,6 +1360,7 @@ static void dev_set_selection(struct wl_client *c, struct wl_resource *r, structSource *s = src ? wl_resource_get_user_data(src) : NULL;if (selection && selection != s) wl_data_source_send_cancelled(selection->res);selection = s;+ clip_offer_gen++;if (s) read_selection(s);}static const struct wl_data_device_interface device_impl = { .start_drag = dev_drag, .set_selection = dev_set_selection, .release = res_release };@@ -1421,12 +1435,15 @@ static void psrc_gone(struct wl_resource *r) {free(s);}+/* An offer carries the generation it was made for: it reads that selection only, and only while+ * its client has the keyboard. */static void poffer_receive(struct wl_client *c, struct wl_resource *r, const char *mime, int32_t fd) {- (void)c; (void)r;- if (prim_text && prim_rank(mime)) { send_bytes(fd, prim_text, prim_len); return; }+ unsigned *gen = wl_resource_get_user_data(r);+ if (gen && *gen == prim_gen && key_focus && key_focus->client == c && prim_text && prim_rank(mime)) { send_bytes(fd, prim_text, prim_len); return; }close(fd);}static const struct zwp_primary_selection_offer_v1_interface poffer_impl = { .receive = poffer_receive, .destroy = res_release };+static void poffer_gone(struct wl_resource *r) { free(wl_resource_get_user_data(r)); }static void prim_offer_to(struct wl_resource *dev) {struct wl_client *c = wl_resource_get_client(dev);@@ -1434,7 +1451,10 @@ static void prim_offer_to(struct wl_resource *dev) {if (!prim_text || !prim_len) { zwp_primary_selection_device_v1_send_selection(dev, NULL); return; }struct wl_resource *o = wl_resource_create(c, &zwp_primary_selection_offer_v1_interface, wl_resource_get_version(dev), 0);if (!o) return;- wl_resource_set_implementation(o, &poffer_impl, NULL, NULL);+ unsigned *gen = malloc(sizeof *gen);+ if (!gen) { wl_resource_destroy(o); return; }+ *gen = prim_gen;+ wl_resource_set_implementation(o, &poffer_impl, gen, poffer_gone);zwp_primary_selection_device_v1_send_data_offer(dev, o);zwp_primary_selection_offer_v1_send_offer(o, "text/plain;charset=utf-8");zwp_primary_selection_offer_v1_send_offer(o, "text/plain");@@ -3079,6 +3099,7 @@ void wl_flush(void) {clip_seen = g;clip_is_image = 0;if (selection) { wl_data_source_send_cancelled(selection->res); selection = NULL; }+ clip_offer_gen++;offer_to_focus();}xwm_tick(); /* X11: clipboard changes, pending requests */@@ -3199,6 +3220,7 @@ void wlx_clip_text(const char *t, size_t n, const char *origin) {void wlx_clip_png(const unsigned char *png, size_t n, const char *origin) {if (n < 8 || memcmp(png, "\x89PNG\r\n\x1a\n", 8)) return;if (selection) { wl_data_source_send_cancelled(selection->res); selection = NULL; }+ clip_offer_gen++;store_png(png, n);snprintf(clip_img_origin, sizeof clip_img_origin, "%s", origin && origin[0] ? origin : "x11");clip_img_sensitive = 0;diff --git a/server/src/wl/x11wire.h b/server/src/wl/x11wire.hindex 36d5df9..58947b0 100644--- a/server/src/wl/x11wire.h+++ b/server/src/wl/x11wire.h@@ -113,6 +113,10 @@ int x11_text_target(const char *name);/* Preference among the text targets an owner offers (higher is better, 0 = not text). */int x11_text_rank(const char *name);+/* Do two X resource ids belong to the same client? (The bits above the server's resource mask+ * are the client.) The window manager answers a paste request only from the focused window's client. */+static inline int x11_same_client(uint32_t a, uint32_t b, uint32_t id_mask) { return (a & ~id_mask) == (b & ~id_mask); }+/* First free display number (0..63), given a test for whether :n is in use (a live lock file or* a socket). -1 if they are all taken. */int x11_pick_display(int (*taken)(int n, void *ctx), void *ctx);diff --git a/server/src/wl/xwayland.c b/server/src/wl/xwayland.cindex a835468..c3132b6 100644--- a/server/src/wl/xwayland.c+++ b/server/src/wl/xwayland.c@@ -132,6 +132,9 @@ int xwl_init(void) {}if (XW.lfd < 0) { fprintf(stderr, "xwayland: no free X display\n"); return -1; }setenv("DISPLAY", XW.display, 1); /* inherited by the apps LordOS starts */+ /* Java (AWT/Swing) guesses frame insets for a reparenting window manager unless told: ours+ * doesn't reparent (the theme draws the frame), so without this its content is shifted and blank */+ setenv("_JAVA_AWT_WM_NONREPARENTING", "1", 1);watch_socket(1);fprintf(stderr, "xwayland: DISPLAY=%s (%s), started on the first X11 connection\n", XW.display, XW.sock);return 0;diff --git a/server/src/wl/xwm.c b/server/src/wl/xwm.cindex 2a5bf5e..94ddab9 100644--- a/server/src/wl/xwm.c+++ b/server/src/wl/xwm.c@@ -61,7 +61,7 @@ enum { XA_PRIMARY = 1, XA_ATOM = 4, XA_CARDINAL = 6, XA_STRING = 31, XA_WINDOW =X(NET_CLIENT_LIST, "_NET_CLIENT_LIST") X(NET_FRAME_EXTENTS, "_NET_FRAME_EXTENTS") X(MOTIF_WM_HINTS, "_MOTIF_WM_HINTS") \X(WL_SURFACE_ID, "WL_SURFACE_ID") X(CLIPBOARD, "CLIPBOARD") X(TARGETS, "TARGETS") X(TIMESTAMP, "TIMESTAMP") X(INCR, "INCR") \X(TEXT, "TEXT") X(MIME_UTF8, "text/plain;charset=utf-8") X(MIME_TEXT, "text/plain") X(MIME_PNG, "image/png") \- X(PW_HINT, "x-kde-passwordManagerHint") X(LORD_SEL_CLIPBOARD, "_LORD_SEL_CLIPBOARD") X(LORD_SEL_PRIMARY, "_LORD_SEL_PRIMARY")+ X(LORD_SEL_CLIPBOARD, "_LORD_SEL_CLIPBOARD") X(LORD_SEL_PRIMARY, "_LORD_SEL_PRIMARY")#define AE(id, name) A_##id,enum { ATOM_LIST(AE) A_COUNT };@@ -251,7 +251,7 @@ static XWin *add_win(uint32_t id) {return w;}-static int same_client(uint32_t a, uint32_t b) { return (a & ~X.su.id_mask) == (b & ~X.su.id_mask); }+static int same_client(uint32_t a, uint32_t b) { return x11_same_client(a, b, X.su.id_mask); }static int is_popup_type(uint32_t t) {return t && (t == AT(NET_WM_WINDOW_TYPE_MENU) || t == AT(NET_WM_WINDOW_TYPE_DROPDOWN_MENU) ||@@ -395,7 +395,10 @@ static unsigned lord_gen(int k) {}static int lord_has(int k) {if (k == SEL_PRIM) { size_t n; unsigned g; return wlx_primary(&n, &g) && n > 0; }- return wl_clip_is_image() || term_clipboard()[0];+ if (wl_clip_is_image()) return 1;+ /* a secret (Passwords, password fields) never goes to X: any X client can read the property the+ * text is written to while it's pasted */+ return term_clipboard()[0] && !term_clipboard_sensitive();}/* LordOS's selection is offered to X only while an X window has the keyboard, and only if it is@@ -410,7 +413,7 @@ static void clip_sync(void) {s->owned = 1;s->owned_gen = g;}- } else if (!X.focus && s->owned) {+ } else if (s->owned && (!X.focus || !lord_has(k))) {set_owner(0, sel_atom(k));s->owned = 0;}@@ -577,14 +580,17 @@ static void sel_request(const uint8_t *p) {target = x11_get32(p + 20), prop = x11_get32(p + 24);int k = sel_index(selection);if (!prop) prop = target; /* obsolete clients */- if (k < 0 || owner != X.wmwin || !X.sel[k].owned || !X.focus) { notify_requestor(time_, requestor, selection, target, 0); return; }+ /* only the X app the user is in (the requestor window is that client's) may read it */+ if (k < 0 || owner != X.wmwin || !X.sel[k].owned || !X.focus || !same_client(requestor, X.focus->id) || !lord_has(k)) {+ notify_requestor(time_, requestor, selection, target, 0);+ return;+ }int image = k == SEL_CLIP && wl_clip_is_image();const char *text = "";size_t tlen = 0;unsigned g;if (k == SEL_PRIM) text = wlx_primary(&tlen, &g);else if (!image) { text = term_clipboard(); tlen = strlen(text); }- int sensitive = k == SEL_CLIP && !image && term_clipboard_sensitive();if (target == AT(TARGETS)) {uint32_t list[10];uint32_t nl = 0;@@ -592,7 +598,6 @@ static void sel_request(const uint8_t *p) {list[nl++] = AT(TIMESTAMP);if (image) list[nl++] = AT(MIME_PNG);else { list[nl++] = AT(UTF8_STRING); list[nl++] = AT(MIME_UTF8); list[nl++] = AT(MIME_TEXT); list[nl++] = XA_STRING; list[nl++] = AT(TEXT); }- if (sensitive) list[nl++] = AT(PW_HINT);change_prop(requestor, prop, XA_ATOM, 32, list, nl);} else if (target == AT(TIMESTAMP)) {uint32_t t = 0;@@ -618,8 +623,6 @@ static void sel_request(const uint8_t *p) {free(l);} else if (!image && (target == AT(UTF8_STRING) || target == AT(MIME_UTF8) || target == AT(MIME_TEXT) || target == AT(TEXT))) {write_data(requestor, prop, target == AT(TEXT) ? AT(UTF8_STRING) : target, (const uint8_t *)text, tlen);- } else if (sensitive && target == AT(PW_HINT)) { /* clipboard managers skip it */- change_prop(requestor, prop, AT(PW_HINT), 8, "secret", 6);} else {notify_requestor(time_, requestor, selection, target, 0);return;diff --git a/server/tests/clip_offers.c b/server/tests/clip_offers.cnew file mode 100644index 0000000..bd5130f--- /dev/null+++ b/server/tests/clip_offers.c@@ -0,0 +1,207 @@+/* clip_offers.c — a Wayland client that checks that clipboard and primary-selection offers are+ * bound to the selection they were made for (docs/PRIVACY.md, "X11 apps"; wl.c clip_offer_gen and+ * prim_gen): after the selection changes, reading through an OLD offer must give nothing, and the+ * new offer must give the new text. Run by tests/xwayland/clip_offers.sh inside a LordOS session,+ * which changes both selections from an X11 app (xclip) while this window keeps the keyboard.+ *+ * make -C server build/clip-offers+ * build/clip-offers NEW_CLIPBOARD NEW_PRIMARY (prints READY, then the results)+ *+ * Exit status 0 when every check passed. */+#include <errno.h>+#include <fcntl.h>+#include <poll.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/mman.h>+#include <time.h>+#include <unistd.h>+#include <wayland-client.h>+#include "client/xdg-shell-client-protocol.h"+#include "client/primary-selection-unstable-v1-client-protocol.h"++static struct wl_compositor *comp;+static struct wl_shm *shm;+static struct xdg_wm_base *wm;+static struct wl_seat *seat;+static struct wl_data_device_manager *ddm;+static struct zwp_primary_selection_device_manager_v1 *pdm;+static int focused, configured;+static struct wl_data_offer *clip_cur;+static struct zwp_primary_selection_offer_v1 *prim_cur;+static int clip_changes, prim_changes;++static void reg_global(void *d, struct wl_registry *r, uint32_t name, const char *iface, uint32_t ver) {+ (void)d; (void)ver;+ if (!strcmp(iface, wl_compositor_interface.name)) comp = wl_registry_bind(r, name, &wl_compositor_interface, 4);+ else if (!strcmp(iface, wl_shm_interface.name)) shm = wl_registry_bind(r, name, &wl_shm_interface, 1);+ else if (!strcmp(iface, xdg_wm_base_interface.name)) wm = wl_registry_bind(r, name, &xdg_wm_base_interface, 1);+ else if (!strcmp(iface, wl_seat_interface.name)) seat = wl_registry_bind(r, name, &wl_seat_interface, 1);+ else if (!strcmp(iface, wl_data_device_manager_interface.name)) ddm = wl_registry_bind(r, name, &wl_data_device_manager_interface, 3);+ else if (!strcmp(iface, zwp_primary_selection_device_manager_v1_interface.name))+ pdm = wl_registry_bind(r, name, &zwp_primary_selection_device_manager_v1_interface, 1);+}+static void reg_remove(void *d, struct wl_registry *r, uint32_t name) { (void)d; (void)r; (void)name; }+static const struct wl_registry_listener reg = { reg_global, reg_remove };++static void wm_ping(void *d, struct xdg_wm_base *b, uint32_t serial) { (void)d; xdg_wm_base_pong(b, serial); }+static const struct xdg_wm_base_listener wm_l = { wm_ping };+static void xs_configure(void *d, struct xdg_surface *s, uint32_t serial) { (void)d; xdg_surface_ack_configure(s, serial); configured = 1; }+static const struct xdg_surface_listener xs_l = { xs_configure };+static void top_configure(void *d, struct xdg_toplevel *t, int32_t w, int32_t h, struct wl_array *st) { (void)d; (void)t; (void)w; (void)h; (void)st; }+static void top_close(void *d, struct xdg_toplevel *t) { (void)d; (void)t; }+static const struct xdg_toplevel_listener top_l = { .configure = top_configure, .close = top_close };++static void kb_keymap(void *d, struct wl_keyboard *k, uint32_t f, int32_t fd, uint32_t n) { (void)d; (void)k; (void)f; (void)n; close(fd); }+static void kb_enter(void *d, struct wl_keyboard *k, uint32_t s, struct wl_surface *sf, struct wl_array *keys) { (void)d; (void)k; (void)s; (void)sf; (void)keys; focused = 1; }+static void kb_leave(void *d, struct wl_keyboard *k, uint32_t s, struct wl_surface *sf) { (void)d; (void)k; (void)s; (void)sf; focused = 0; }+static void kb_key(void *d, struct wl_keyboard *k, uint32_t s, uint32_t t, uint32_t key, uint32_t st) { (void)d; (void)k; (void)s; (void)t; (void)key; (void)st; }+static void kb_mods(void *d, struct wl_keyboard *k, uint32_t s, uint32_t a, uint32_t b, uint32_t c, uint32_t g) { (void)d; (void)k; (void)s; (void)a; (void)b; (void)c; (void)g; }+static const struct wl_keyboard_listener kb_l = { kb_keymap, kb_enter, kb_leave, kb_key, kb_mods, NULL };++static void off_offer(void *d, struct wl_data_offer *o, const char *m) { (void)d; (void)o; (void)m; }+static const struct wl_data_offer_listener off_l = { .offer = off_offer };+static void dev_offer(void *d, struct wl_data_device *dv, struct wl_data_offer *o) { (void)d; (void)dv; wl_data_offer_add_listener(o, &off_l, NULL); }+static void dev_enter(void *d, struct wl_data_device *dv, uint32_t s, struct wl_surface *sf, wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *o) { (void)d; (void)dv; (void)s; (void)sf; (void)x; (void)y; (void)o; }+static void dev_leave(void *d, struct wl_data_device *dv) { (void)d; (void)dv; }+static void dev_motion(void *d, struct wl_data_device *dv, uint32_t t, wl_fixed_t x, wl_fixed_t y) { (void)d; (void)dv; (void)t; (void)x; (void)y; }+static void dev_drop(void *d, struct wl_data_device *dv) { (void)d; (void)dv; }+/* old offers are kept (not destroyed) on purpose: the test reads through them later */+static struct wl_data_offer *clip_old;+static void dev_selection(void *d, struct wl_data_device *dv, struct wl_data_offer *o) {+ (void)d; (void)dv;+ if (!o) return;+ clip_cur = o;+ clip_changes++;+}+static const struct wl_data_device_listener dev_l = { dev_offer, dev_enter, dev_leave, dev_motion, dev_drop, dev_selection };++static void poff_offer(void *d, struct zwp_primary_selection_offer_v1 *o, const char *m) { (void)d; (void)o; (void)m; }+static const struct zwp_primary_selection_offer_v1_listener poff_l = { poff_offer };+static void pdev_offer(void *d, struct zwp_primary_selection_device_v1 *dv, struct zwp_primary_selection_offer_v1 *o) { (void)d; (void)dv; zwp_primary_selection_offer_v1_add_listener(o, &poff_l, NULL); }+static struct zwp_primary_selection_offer_v1 *prim_old;+static void pdev_selection(void *d, struct zwp_primary_selection_device_v1 *dv, struct zwp_primary_selection_offer_v1 *o) {+ (void)d; (void)dv;+ if (!o) return;+ prim_cur = o;+ prim_changes++;+}+static const struct zwp_primary_selection_device_v1_listener pdev_l = { pdev_offer, pdev_selection };++static struct wl_display *dpy;++static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (double)t.tv_sec + t.tv_nsec / 1e9; }++/* Dispatches until cond() or the timeout. */+static int wait_for(int (*cond)(void), double secs) {+ double end = now() + secs;+ while (!cond() && now() < end) {+ wl_display_flush(dpy);+ struct pollfd p = { wl_display_get_fd(dpy), POLLIN, 0 };+ if (poll(&p, 1, 100) > 0) { if (wl_display_dispatch(dpy) < 0) return 0; }+ else wl_display_dispatch_pending(dpy);+ }+ return cond();+}++static int ready_cond(void) { return focused && clip_cur && prim_cur; }+static int base_clip, base_prim;+static int changed_cond(void) { return clip_changes > base_clip && prim_changes > base_prim; }++/* Reads what the offer gives (at most size-1 bytes; EOF or 2 s). */+static size_t read_pipe(int fd, char *buf, size_t size) {+ size_t n = 0;+ double end = now() + 2;+ while (n + 1 < size && now() < end) {+ wl_display_flush(dpy);+ wl_display_dispatch_pending(dpy);+ struct pollfd p = { fd, POLLIN, 0 };+ if (poll(&p, 1, 50) <= 0) continue;+ ssize_t r = read(fd, buf + n, size - 1 - n);+ if (r <= 0) break;+ n += (size_t)r;+ }+ buf[n] = 0;+ close(fd);+ return n;+}+static size_t read_clip(struct wl_data_offer *o, char *buf, size_t size) {+ int p[2];+ if (pipe(p)) return 0;+ wl_data_offer_receive(o, "text/plain;charset=utf-8", p[1]);+ close(p[1]);+ wl_display_flush(dpy);+ return read_pipe(p[0], buf, size);+}+static size_t read_prim(struct zwp_primary_selection_offer_v1 *o, char *buf, size_t size) {+ int p[2];+ if (pipe(p)) return 0;+ zwp_primary_selection_offer_v1_receive(o, "text/plain;charset=utf-8", p[1]);+ close(p[1]);+ wl_display_flush(dpy);+ return read_pipe(p[0], buf, size);+}++int main(int argc, char **argv) {+ if (argc < 3) { fprintf(stderr, "usage: clip-offers NEW_CLIPBOARD NEW_PRIMARY\n"); return 2; }+ setvbuf(stdout, NULL, _IOLBF, 0);+ dpy = wl_display_connect(NULL);+ if (!dpy) { fprintf(stderr, "no Wayland\n"); return 2; }+ struct wl_registry *r = wl_display_get_registry(dpy);+ wl_registry_add_listener(r, ®, NULL);+ wl_display_roundtrip(dpy);+ if (!comp || !shm || !wm || !seat || !ddm || !pdm) { fprintf(stderr, "a global is missing\n"); return 2; }+ xdg_wm_base_add_listener(wm, &wm_l, NULL);+ struct wl_keyboard *kb = wl_seat_get_keyboard(seat);+ wl_keyboard_add_listener(kb, &kb_l, NULL);+ struct wl_data_device *dev = wl_data_device_manager_get_data_device(ddm, seat);+ wl_data_device_add_listener(dev, &dev_l, NULL);+ struct zwp_primary_selection_device_v1 *pdev = zwp_primary_selection_device_manager_v1_get_device(pdm, seat);+ zwp_primary_selection_device_v1_add_listener(pdev, &pdev_l, NULL);+ /* a small window, so the compositor gives it the keyboard */+ struct wl_surface *s = wl_compositor_create_surface(comp);+ struct xdg_surface *xs = xdg_wm_base_get_xdg_surface(wm, s);+ xdg_surface_add_listener(xs, &xs_l, NULL);+ struct xdg_toplevel *top = xdg_surface_get_toplevel(xs);+ xdg_toplevel_add_listener(top, &top_l, NULL);+ xdg_toplevel_set_title(top, "Clipboard offers test");+ xdg_toplevel_set_app_id(top, "clip-offers");+ wl_surface_commit(s);+ while (!configured && wl_display_dispatch(dpy) >= 0) {}+ int w = 240, h = 120, stride = w * 4;+ int fd = memfd_create("clip-offers", MFD_CLOEXEC);+ if (fd < 0 || ftruncate(fd, stride * h)) return 2;+ uint32_t *px = mmap(NULL, (size_t)stride * h, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);+ if (px == MAP_FAILED) return 2;+ for (int i = 0; i < w * h; i++) px[i] = 0xff3060a0;+ struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, stride * h);+ struct wl_buffer *buf = wl_shm_pool_create_buffer(pool, 0, w, h, stride, WL_SHM_FORMAT_XRGB8888);+ wl_surface_attach(s, buf, 0, 0);+ wl_surface_commit(s);+ if (!wait_for(ready_cond, 10)) { printf("FAIL: no keyboard focus or no initial offers (focused %d)\n", focused); return 1; }+ clip_old = clip_cur;+ prim_old = prim_cur;+ base_clip = clip_changes;+ base_prim = prim_changes;+ printf("READY\n");+ if (!wait_for(changed_cond, 15)) { printf("FAIL: the selections didn't change\n"); return 1; }+ int fails = 0;+ char got[4096];+ struct wl_data_offer *oldc = clip_old;+ struct zwp_primary_selection_offer_v1 *oldp = prim_old;+ size_t n = oldc ? read_clip(oldc, got, sizeof got) : 0;+ printf("%s old clipboard offer gives nothing (%zu bytes)\n", n ? "FAIL:" : "ok:", n);+ fails += n != 0 || !oldc;+ n = read_clip(clip_cur, got, sizeof got);+ printf("%s new clipboard offer gives \"%s\"\n", strcmp(got, argv[1]) ? "FAIL:" : "ok:", got);+ fails += strcmp(got, argv[1]) != 0;+ n = oldp ? read_prim(oldp, got, sizeof got) : 0;+ printf("%s old primary offer gives nothing (%zu bytes)\n", n ? "FAIL:" : "ok:", n);+ fails += n != 0 || !oldp;+ n = read_prim(prim_cur, got, sizeof got);+ printf("%s new primary offer gives \"%s\"\n", strcmp(got, argv[2]) ? "FAIL:" : "ok:", got);+ fails += strcmp(got, argv[2]) != 0;+ printf("%s\n", fails ? "FAILED" : "PASSED");+ return fails ? 1 : 0;+}diff --git a/server/tests/xwayland/selections.sh b/server/tests/xwayland/selections.shnew file mode 100755index 0000000..eb3b328--- /dev/null+++ b/server/tests/xwayland/selections.sh@@ -0,0 +1,46 @@+#!/bin/sh+# selections.sh — clipboard/primary checks for X11 apps, run inside a LordOS session on a test VM+# (DISPLAY and WAYLAND_DISPLAY of the session; needs xclip, wl-clipboard, xterm and+# build/clip-offers). See docs/WAYLAND.md, "X11 apps (Xwayland)", and docs/PRIVACY.md.+#+# sh tests/xwayland/selections.sh (from server/)+#+# 1. An X11 app's copy (CLIPBOARD and PRIMARY) reaches Wayland apps.+# 2. Offers are bound to their selection: a Wayland app can't read later copies through an old offer.+# 3. A Wayland copy is not readable by a background X11 app (no X window has the keyboard), nor by+# another X11 client while an X window has it (only the focused window's client may paste).+cd "$(dirname "$0")/../.." || exit 1+fails=0+ok() { printf ' ok %s\n' "$1"; }+bad() { printf ' FAILED %s\n' "$1"; fails=$((fails + 1)); }+xset_sel() { setsid -f sh -c "printf %s '$2' | xclip -selection $1" >/dev/null 2>&1 </dev/null; sleep 1; }++# 1+xset_sel clipboard "x11 clipboard 1"+xset_sel primary "x11 primary 1"+[ "$(timeout 3 wl-paste -n 2>/dev/null)" = "x11 clipboard 1" ] && ok "X11 CLIPBOARD → Wayland" || bad "X11 CLIPBOARD → Wayland"+[ "$(timeout 3 wl-paste -n --primary 2>/dev/null)" = "x11 primary 1" ] && ok "X11 PRIMARY → Wayland" || bad "X11 PRIMARY → Wayland"++# 2+out=$(mktemp)+build/clip-offers "x11 clipboard 2" "x11 primary 2" >"$out" 2>&1 &+pid=$!+i=0; while ! grep -q READY "$out" && [ $i -lt 100 ]; do sleep 0.1; i=$((i + 1)); done+xset_sel clipboard "x11 clipboard 2"+xset_sel primary "x11 primary 2"+wait $pid && ok "offers bound to their selection" || { bad "offers bound to their selection"; cat "$out"; }+rm -f "$out"++# 3+setsid -f sh -c "printf %s 'wayland secret-ish' | wl-copy" >/dev/null 2>&1 </dev/null+sleep 1.5+got=$(timeout 3 xclip -o -selection clipboard 2>/dev/null)+[ "$got" != "wayland secret-ish" ] && ok "background X11 app can't read a Wayland copy" || bad "background X11 app read a Wayland copy"+setsid -f xterm -title selections-test >/dev/null 2>&1 </dev/null+sleep 3+got=$(timeout 3 xclip -o -selection clipboard 2>/dev/null)+[ "$got" != "wayland secret-ish" ] && ok "another X11 client can't read it while xterm has the keyboard" || bad "another X11 client read it"+pkill -f "xterm -title selections-test"++[ $fails -eq 0 ] && echo "selections: PASSED" || echo "selections: $fails FAILED"+exit $failsdiff --git a/server/tests/xwm_test.c b/server/tests/xwm_test.cnew file mode 100644index 0000000..a04f42a--- /dev/null+++ b/server/tests/xwm_test.c@@ -0,0 +1,170 @@+/* xwm_test.c — the X11 wire helpers of the window manager (src/wl/x11wire.c): request encoding,+ * the setup reply, packet framing, sequence numbers and the ICCCM/EWMH/Motif properties. No X+ * server needed; the window manager itself is tested on a VM (docs/WAYLAND.md, "X11 apps"). */+#include "../src/wl/x11wire.h"++#include <stdio.h>+#include <string.h>++static int fails, checks;+#define CHECK(c) do { checks++; if (!(c)) { fails++; printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); } } while (0)++static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); }+static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); }++static void test_requests(void) {+ XOut o = { 0 };+ xo_begin(&o, X_InternAtom, 0);+ xo_u16(&o, 5); xo_u16(&o, 0);+ xo_bytes(&o, "WM_S0", 5);+ xo_end(&o);+ CHECK(o.n == 16); /* 4 header + 4 + "WM_S0" padded to 8 */+ CHECK(o.b[0] == X_InternAtom && x11_get16(o.b + 2) == 4);+ CHECK(!memcmp(o.b + 8, "WM_S0\0\0\0", 8));+ size_t first = o.n;+ xo_begin(&o, X_MapWindow, 0);+ xo_u32(&o, 0x400001);+ xo_end(&o);+ CHECK(o.n == first + 8 && x11_get16(o.b + first + 2) == 2 && x11_get32(o.b + first + 4) == 0x400001);+ xo_drop(&o, first);+ CHECK(o.n == 8 && o.b[0] == X_MapWindow);+ xo_free(&o);+ uint8_t setup[12];+ x11_setup_request(setup);+ CHECK(setup[0] == 'l' && x11_get16(setup + 2) == 11 && x11_get16(setup + 4) == 0 && x11_get16(setup + 6) == 0);+}++static void test_setup(void) {+ uint8_t r[200] = { 0 };+ const char vendor[] = "The X.Org Foundation"; /* 20 bytes */+ size_t vlen = 20, nformats = 2;+ size_t data = 32 + vlen + nformats * 8 + 40 + 8 + 24; /* one screen with one depth and one visual */+ r[0] = 1;+ put16(r + 2, 11);+ put16(r + 6, (uint16_t)(data / 4));+ uint8_t *d = r + 8;+ put32(d + 4, 0x200000);+ put32(d + 8, 0x1fffff);+ put16(d + 16, (uint16_t)vlen);+ put16(d + 18, 65535);+ d[20] = 1; d[21] = (uint8_t)nformats; d[26] = 8; d[27] = 255;+ memcpy(d + 32, vendor, vlen);+ uint8_t *scr = d + 32 + vlen + nformats * 8;+ put32(scr, 0x221);+ put16(scr + 20, 2560); put16(scr + 22, 1440);+ put32(scr + 32, 0x21);+ scr[38] = 24;+ XSetup s;+ size_t used = 0;+ CHECK(x11_parse_setup(r, 8, &s, &used) == 0); /* not complete */+ CHECK(x11_parse_setup(r, 8 + data, &s, &used) == 1);+ CHECK(used == 8 + data);+ CHECK(s.root == 0x221 && s.root_visual == 0x21 && s.root_depth == 24);+ CHECK(s.width == 2560 && s.height == 1440);+ CHECK(s.id_base == 0x200000 && s.id_mask == 0x1fffff);+ CHECK(s.max_req_bytes == 65535u * 4 && s.min_keycode == 8 && s.max_keycode == 255);+ r[0] = 0; /* refused */+ CHECK(x11_parse_setup(r, 8 + data, &s, &used) == -1);+}++static void test_framing(void) {+ uint8_t p[64] = { 0 };+ p[0] = X_MapRequest;+ CHECK(x11_packet_len(p, 31) == 0);+ CHECK(x11_packet_len(p, 32) == 32);+ p[0] = X_Reply;+ put32(p + 4, 2); /* 8 more bytes */+ CHECK(x11_packet_len(p, 32) == 0);+ CHECK(x11_packet_len(p, 40) == 40);+ p[0] = X_GenericEvent | 0x80;+ CHECK(x11_packet_len(p, 40) == 40);+ p[0] = X_Error;+ CHECK(x11_packet_len(p, 64) == 32);+ CHECK(x11_seq_widen(5, 5) == 5);+ CHECK(x11_seq_widen(0x10003, 0xfffe) == 0xfffe); /* sent before the wrap */+ CHECK(x11_seq_widen(0x20010, 0x0005) == 0x20005);+}++static void test_props(void) {+ char inst[32], cls[32];+ const uint8_t wc[] = "xterm\0XTerm";+ x11_wm_class(wc, sizeof wc, inst, sizeof inst, cls, sizeof cls);+ CHECK(!strcmp(inst, "xterm") && !strcmp(cls, "XTerm"));+ const uint8_t only[] = "neverball";+ x11_wm_class(only, 9, inst, sizeof inst, cls, sizeof cls);+ CHECK(!strcmp(inst, "neverball") && !cls[0]);+ const uint8_t evil[] = "a\x1b[2Jb\0C\x7fz"; /* control bytes are dropped */+ x11_wm_class(evil, sizeof evil, inst, sizeof inst, cls, sizeof cls);+ CHECK(!strcmp(inst, "a[2Jb") && !strcmp(cls, "Cz"));++ char t[64];+ x11_title((const uint8_t *)"Caf\xc3\xa9 \xe2\x80\x94 ok", 13, 0, t, sizeof t);+ CHECK(!strcmp(t, "Caf\xc3\xa9 \xe2\x80\x94 ok"));+ x11_title((const uint8_t *)"Caf\xe9", 4, 1, t, sizeof t); /* Latin-1 STRING */+ CHECK(!strcmp(t, "Caf\xc3\xa9"));+ x11_title((const uint8_t *)"a\xff" "b\xc0\xaf" "c", 6, 0, t, sizeof t); /* invalid and overlong */+ CHECK(!strcmp(t, "a\xef\xbf\xbd" "b\xef\xbf\xbd\xef\xbf\xbd" "c"));+ x11_title((const uint8_t *)"line\nbreak\ttab", 14, 0, t, sizeof t);+ CHECK(!strcmp(t, "linebreaktab"));+ x11_title((const uint8_t *)"\xe2\x82\xac\xe2\x82\xac\xe2\x82\xac", 9, 0, t, 7); /* never a cut character */+ CHECK(!strcmp(t, "\xe2\x82\xac\xe2\x82\xac"));++ uint32_t motif_off[5] = { 2, 0, 0, 0, 0 }, motif_on[5] = { 2, 0, 1, 0, 0 }, motif_none[5] = { 1, 0, 0, 0, 0 };+ CHECK(x11_motif_decorated(motif_off, 5) == 0);+ CHECK(x11_motif_decorated(motif_on, 5) == 1);+ CHECK(x11_motif_decorated(motif_none, 5) == -1);+ CHECK(x11_motif_decorated(NULL, 0) == -1);++ uint32_t hints[18] = { 16 | 32, 0, 0, 0, 0, 200, 100, 800, 600 };+ XSizeHints h;+ x11_size_hints(hints, 18, &h);+ CHECK(h.min_w == 200 && h.min_h == 100 && h.max_w == 800 && h.max_h == 600);+ hints[5] = 0xffffffffu; /* nonsense is ignored */+ x11_size_hints(hints, 18, &h);+ CHECK(h.min_w == 0);+ x11_size_hints(hints, 3, &h);+ CHECK(h.min_w == 0 && h.max_w == 0);++ uint32_t wmh_no[2] = { 1, 0 }, wmh_yes[2] = { 1, 1 }, wmh_unset[2] = { 0, 0 };+ CHECK(x11_wm_hints_input(wmh_no, 2) == 0);+ CHECK(x11_wm_hints_input(wmh_yes, 2) == 1);+ CHECK(x11_wm_hints_input(wmh_unset, 2) == 1);+ CHECK(x11_wm_hints_input(NULL, 0) == 1);+}++static void test_policy(void) {+ CHECK(x11_moveresize_edges(0) == (1 | 4)); /* top-left */+ CHECK(x11_moveresize_edges(3) == 8); /* right */+ CHECK(x11_moveresize_edges(4) == (2 | 8)); /* bottom-right */+ CHECK(x11_moveresize_edges(8) == -1); /* move */+ CHECK(x11_moveresize_edges(9) == 0 && x11_moveresize_edges(11) == 0 && x11_moveresize_edges(99) == 0);+ CHECK(x11_text_rank("UTF8_STRING") > x11_text_rank("text/plain;charset=utf-8"));+ CHECK(x11_text_rank("STRING") > x11_text_rank("text/plain"));+ CHECK(x11_text_target("TEXT") && !x11_text_target("image/png") && !x11_text_target(NULL));+ /* a paste request is answered only for the focused window's client */+ uint32_t mask = 0x1fffff;+ CHECK(x11_same_client(0x400001, 0x40000c, mask));+ CHECK(!x11_same_client(0x400001, 0x600001, mask));+ CHECK(!x11_same_client(0x200001, 0x400001, mask));+}++static int taken_set[64];+static int taken(int n, void *ctx) { (void)ctx; return taken_set[n]; }+static void test_display(void) {+ CHECK(x11_pick_display(taken, NULL) == 0);+ taken_set[0] = taken_set[1] = 1;+ CHECK(x11_pick_display(taken, NULL) == 2);+ for (int i = 0; i < 64; i++) taken_set[i] = 1;+ CHECK(x11_pick_display(taken, NULL) == -1);+}++int main(void) {+ test_requests();+ test_setup();+ test_framing();+ test_props();+ test_policy();+ test_display();+ printf("xwm-test: %d/%d\n", checks - fails, checks);+ return fails ? 1 : 0;+}