Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Replace Xcursor library with bounded cursor validation

oxydizerbc300ed888bf
diff --git a/README.md b/README.md
index a72a4c0..54f0de3 100644
--- a/README.md
+++ b/README.md
@@ -56,7 +56,7 @@ Installing never replaces your login manager
Requirements: Zig 0.16, wlroots 0.20, and the development files for Wayland,
wayland-protocols, xkbcommon, pixman, FreeType, HarfBuzz, Fontconfig,
librsvg/GdkPixbuf, PangoCairo, libinput, PAM, libpulse, libpipewire-0.3, Poppler GLib,
-libseccomp, libarchive, libjpeg, libpng, libwebp and libXcursor. Optional:
+libseccomp, libarchive, libjpeg, libpng and libwebp. Optional:
Xwayland, `foot`, `brightnessctl`, `gio`/`xdg-open`, `git` (optional).
Run from a checkout, `scripts/install.sh` builds that checkout (dependencies,
diff --git a/TESTING.md b/TESTING.md
index 6037a5b..ebed858 100644
--- a/TESTING.md
+++ b/TESTING.md
@@ -434,4 +434,4 @@ for the remaining capture acceptance checks.
`python3 tests/taskbar_shape.py` covers the floating taskbar: the Appearance sliders for gap from the edge, length and corner radius, the same geometry from the theme file on both edges, the strip a maximized window leaves, and the pixels of the rounded ends.
-`python3 tests/theme_packs.py` covers theme ZIP installation, refusal of unsafe paths/links/special files/bombs, pack discovery, base plus overrides, live wallpaper and folder icons, reset, cursors and removal in isolated data/config/state directories.
+`python3 tests/theme_packs.py` covers theme ZIP installation, refusal of unsafe paths/links/special files/bombs, Xcursor headers/data bounds and per-size frame limits (including sizes other than the selected one), pack discovery, base plus overrides, live wallpaper and folder icons, reset, cursors and removal in isolated data/config/state directories.
diff --git a/build.zig b/build.zig
index f985614..874c8a7 100644
--- a/build.zig
+++ b/build.zig
@@ -114,7 +114,6 @@ pub fn build(b: *std.Build) void {
inline for (.{ "libarchive", "librsvg-2.0", "gdk-pixbuf-2.0" }) |lib| theme_helper.root_module.linkSystemLibrary(lib, .{});
addThumbCodec(b, theme_helper.root_module);
theme_helper.root_module.linkSystemLibrary("libwebp", .{});
- theme_helper.root_module.linkSystemLibrary("xcursor", .{});
b.installArtifact(theme_helper);
b.getInstallStep().dependOn(&b.addInstallFileWithDir(b.path("data/rediwm-theme.desktop"), .prefix, "share/applications/rediwm-theme.desktop").step);
b.getInstallStep().dependOn(&b.addInstallFileWithDir(b.path("data/rediwm-theme.xml"), .prefix, "share/mime/packages/rediwm-theme.xml").step);
diff --git a/scripts/install.sh b/scripts/install.sh
index 5cbb1df..ca6a194 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -215,7 +215,7 @@ case "$FAMILY" in
arch)
deps="wayland wayland-protocols pixman libxkbcommon \
freetype2 harfbuzz fontconfig librsvg cairo pango libinput poppler-glib libseccomp \
- libarchive libwebp libxcursor libpng libjpeg-turbo libpulse pipewire pam mesa pkgconf git python \
+ libarchive libwebp libpng libjpeg-turbo libpulse pipewire pam mesa pkgconf git python \
xorg-xwayland xdg-desktop-portal xdg-desktop-portal-wlr xdg-desktop-portal-gtk"
if [ "$PRIVATE_WLROOTS" = no ]; then
deps="wlroots0.20 $deps"
@@ -238,13 +238,13 @@ case "$FAMILY" in
libxcb-res0-dev libxcb-xfixes0-dev libxcb-dri3-dev libxcb-present-dev \
libxcb-render-util0-dev libxcb-shm0-dev libxcb-xinput-dev xwayland \
libfreetype-dev libharfbuzz-dev libfontconfig-dev librsvg2-dev libcairo2-dev libpango1.0-dev libgdk-pixbuf-2.0-dev \
- libglib2.0-dev libarchive-dev libwebp-dev libxcursor-dev libpng-dev libjpeg-dev libpulse-dev libpipewire-0.3-dev libpam0g-dev libpoppler-glib-dev libseccomp-dev \
+ libglib2.0-dev libarchive-dev libwebp-dev libpng-dev libjpeg-dev libpulse-dev libpipewire-0.3-dev libpam0g-dev libpoppler-glib-dev libseccomp-dev \
xdg-desktop-portal xdg-desktop-portal-wlr xdg-desktop-portal-gtk"
;;
fedora)
deps="wayland-devel wayland-protocols-devel pixman-devel libxkbcommon-devel \
freetype-devel harfbuzz-devel fontconfig-devel librsvg2-devel cairo-devel pango-devel libinput-devel \
- libarchive-devel libwebp-devel libXcursor-devel libpng-devel libjpeg-turbo-devel pulseaudio-libs-devel pipewire-devel pam-devel poppler-glib-devel libseccomp-devel \
+ libarchive-devel libwebp-devel libpng-devel libjpeg-turbo-devel pulseaudio-libs-devel pipewire-devel pam-devel poppler-glib-devel libseccomp-devel \
mesa-libEGL-devel mesa-libGLES-devel glibc-devel \
pkgconf-pkg-config git python3 \
xorg-x11-server-Xwayland \
@@ -261,7 +261,7 @@ case "$FAMILY" in
opensuse)
deps="wlroots-devel wayland-devel wayland-protocols-devel libpixman-1-0-devel libxkbcommon-devel \
freetype2-devel harfbuzz-devel fontconfig-devel librsvg-devel cairo-devel pango-devel libinput-devel \
- libarchive-devel libwebp-devel libXcursor-devel libpng16-devel libjpeg8-devel libpulse-devel pipewire-devel pam-devel libpoppler-glib-devel libseccomp-devel \
+ libarchive-devel libwebp-devel libpng16-devel libjpeg8-devel libpulse-devel pipewire-devel pam-devel libpoppler-glib-devel libseccomp-devel \
Mesa-libEGL-devel Mesa-libGLESv2-devel glibc-devel \
pkg-config git python3 \
xwayland \
@@ -270,7 +270,7 @@ case "$FAMILY" in
gentoo)
deps="gui-libs/wlroots dev-libs/wayland dev-libs/wayland-protocols x11-libs/pixman x11-libs/libxkbcommon \
media-libs/freetype media-libs/harfbuzz media-libs/fontconfig gnome-base/librsvg x11-libs/cairo x11-libs/pango dev-libs/libinput \
- app-arch/libarchive media-libs/libwebp x11-libs/libXcursor media-libs/libpng media-libs/libjpeg-turbo media-libs/libpulse media-video/pipewire sys-libs/pam \
+ app-arch/libarchive media-libs/libwebp media-libs/libpng media-libs/libjpeg-turbo media-libs/libpulse media-video/pipewire sys-libs/pam \
media-libs/mesa \
virtual/pkgconfig dev-vcs/git dev-lang/python \
x11-base/xwayland \
diff --git a/src/theme_helper/native.c b/src/theme_helper/native.c
index b65fd19..c6dad67 100644
--- a/src/theme_helper/native.c
+++ b/src/theme_helper/native.c
@@ -23,7 +23,6 @@
#include <sys/syscall.h>
#include <sys/wait.h>
#include <sys/resource.h>
-#include <X11/Xcursor/Xcursor.h>
#include <time.h>
#include <unistd.h>
@@ -172,17 +171,75 @@ static int png_stream_validate(const char *file,int *width,int *height) {
for(int pass=0;pass<passes;pass++)for(int y=0;y<*height;y++)png_read_row(png,(png_bytep)row,NULL);
png_read_end(png,NULL);free((void*)row);png_destroy_read_struct(&png,&info,NULL);fclose(input);return 1;
}
+// Xcursor is little-endian: a file header, a TOC, then typed chunks.
+// Only metadata is needed here; validate every size, without loading pixels.
+static int cursor_words(FILE *input, uint64_t offset, uint32_t *words, size_t count) {
+ unsigned char bytes[36]; // Largest read: the nine-word image header.
+ if (count > sizeof(bytes)/4 || fseeko(input, (off_t)offset, SEEK_SET) != 0 ||
+ fread(bytes, 4, count, input) != count) return 0;
+ for (size_t i=0; i<count; i++) {
+ const unsigned char *p=bytes+4*i;
+ words[i]=(uint32_t)p[0] | (uint32_t)p[1]<<8 | (uint32_t)p[2]<<16 | (uint32_t)p[3]<<24;
+ }
+ return 1;
+}
+static int cursor_size_compare(const void *a, const void *b) {
+ uint32_t x=*(const uint32_t *)a, y=*(const uint32_t *)b;
+ return (x>y)-(x<y);
+}
+static int cursor_validate(const char *file) {
+ const uint32_t image_type=0xfffd0002;
+ FILE *input=fopen(file,"rb");
+ if (!input) return 0;
+ int valid=0;
+ uint32_t *sizes=NULL;
+ struct stat st;
+ uint32_t header[4];
+ if (fstat(fileno(input),&st)<0 || !S_ISREG(st.st_mode) || st.st_size<16 ||
+ (uint64_t)st.st_size>MAX_FILE || !cursor_words(input,0,header,4)) goto done;
+ uint64_t length=(uint64_t)st.st_size;
+ // Keep the format's extension headers, but reject incompatible major versions.
+ if (header[0]!=0x72756358 || header[1]<16 || header[1]>length ||
+ header[2]>>16!=1 || header[3]==0 || header[3]>65536 ||
+ header[3]>(length-header[1])/12) goto done;
+ uint64_t toc_end=header[1]+(uint64_t)header[3]*12;
+ sizes=malloc((size_t)header[3]*sizeof(*sizes));
+ if (!sizes) goto done;
+ size_t images=0;
+ for (uint32_t i=0; i<header[3]; i++) {
+ uint32_t toc[3], chunk[9];
+ if (!cursor_words(input,header[1]+(uint64_t)i*12,toc,3)) goto done;
+ uint64_t position=toc[2];
+ if (position<toc_end || position>length || length-position<16 ||
+ !cursor_words(input,position,chunk,4) || chunk[0]<16 ||
+ chunk[0]>length-position || chunk[1]!=toc[0] || chunk[2]!=toc[1]) goto done;
+ // Unknown chunk types (including comments) do not affect cursor images.
+ if (toc[0]!=image_type) continue;
+ if (chunk[0]<36 || chunk[2]==0 || chunk[3]==0 ||
+ !cursor_words(input,position,chunk,9)) goto done;
+ uint32_t width=chunk[4], height=chunk[5];
+ if (width==0 || height==0 || width>512 || height>512 ||
+ chunk[6]>=width || chunk[7]>=height ||
+ (uint64_t)width*height*4>length-position-chunk[0]) goto done;
+ sizes[images++]=toc[1];
+ }
+ if (images==0) goto done;
+ // The 256-frame limit applies independently to each nominal size.
+ qsort(sizes,images,sizeof(*sizes),cursor_size_compare);
+ size_t frames=1;
+ for (size_t i=1; i<images; i++) {
+ frames=sizes[i]==sizes[i-1]?frames+1:1;
+ if (frames>256) goto done;
+ }
+ valid=1;
+done:
+ free(sizes);
+ fclose(input);
+ return valid;
+}
static int validate_file(const char *file,const char *rel,off_t bytes) {
if(!strncmp(rel,"cursors/cursors/",16)) {
- XcursorImages *images=XcursorFilenameLoadImages(file,256);
- if(!images)return fail(rel,"invalid Xcursor file");
- int valid=images->nimage>0&&images->nimage<=256;
- for(int i=0;i<images->nimage&&valid;i++) {
- XcursorImage *image=images->images[i];
- valid=image&&image->width>0&&image->height>0&&image->width<=512&&image->height<=512&&image->xhot<image->width&&image->yhot<image->height;
- }
- XcursorImagesDestroy(images);
- return valid?0:fail(rel,"invalid or oversized cursor images");
+ return cursor_validate(file)?0:fail(rel,"invalid or oversized Xcursor file");
}
if(suffix(rel,".svg")) {
size_t limit=!strncmp(rel,"glyphs/",7)?65536:512*1024;
diff --git a/tests/theme_packs.py b/tests/theme_packs.py
index be80523..9b9611b 100644
--- a/tests/theme_packs.py
+++ b/tests/theme_packs.py
@@ -39,6 +39,82 @@ def reset_theme(ipc):
ipc.action("close_panel", {"panel": "control_center"})
+def cursor_bytes(images, file_extra=b"", image_extra=b""):
+ """Build Xcursor fixtures: nominal size, dimensions, hotspot, raw pixels."""
+ header_size = 16 + len(file_extra)
+ image_header_size = 36 + len(image_extra)
+ data = bytearray(struct.pack("<4I", 0x72756358, header_size, 0x10000, len(images)) + file_extra)
+ position = header_size + 12 * len(images)
+ for size, width, height, xhot, yhot, pixels in images:
+ data.extend(struct.pack("<3I", 0xfffd0002, size, position))
+ position += image_header_size + len(pixels)
+ for size, width, height, xhot, yhot, pixels in images:
+ data.extend(struct.pack("<9I", image_header_size, 0xfffd0002, size, 1,
+ width, height, xhot, yhot, 20) + image_extra + pixels)
+ return bytes(data)
+
+
+CURSOR_FRAME = (256, 1, 1, 0, 0, b"\xff" * 4)
+CURSOR_OVERSIZED = (512, 513, 1, 0, 0, b"\xff" * (513 * 4))
+CURSOR_TRUNCATED = (512, 512, 512, 0, 0, b"")
+
+
+def cursor_validation(tmp, env):
+ pack = tmp / "cursor-validation"
+ shutil.copytree(FIXTURE, pack)
+ path = pack / "cursors/cursors/default"
+ path.parent.mkdir(parents=True)
+ valid = cursor_bytes([CURSOR_FRAME])
+
+ def changed(offset, value):
+ data = bytearray(valid)
+ struct.pack_into("<I", data, offset, value)
+ return bytes(data)
+
+ cases = {
+ "single": (valid, True),
+ "extensions": (cursor_bytes([CURSOR_FRAME], b"file", b"image header"), True),
+ # More than 256 images overall is valid: frames are counted per size.
+ "animations": (cursor_bytes([CURSOR_FRAME] * 256 + [(512, *CURSOR_FRAME[1:])] * 256), True),
+ "empty": (b"", False),
+ "no-images": (cursor_bytes([]), False),
+ "magic": (changed(0, 0), False),
+ "file-version": (changed(8, 0x20000), False),
+ "short-header": (changed(4, 15), False),
+ "long-header": (changed(4, 0xffffffff), False),
+ "toc-overflow": (changed(12, 0xffffffff), False),
+ "truncated-toc": (valid[:27], False),
+ "offset-in-toc": (changed(24, 16), False),
+ "offset-outside": (changed(24, 0xffffffff), False),
+ "short-chunk": (changed(28, 35), False),
+ "long-chunk": (changed(28, 0xffffffff), False),
+ "type-mismatch": (changed(32, 0), False),
+ "size-mismatch": (changed(36, 32), False),
+ "image-version": (changed(40, 0), False),
+ "zero-width": (changed(44, 0), False),
+ "zero-height": (changed(48, 0), False),
+ "huge-width": (changed(44, 0xffffffff), False),
+ "huge-height": (changed(48, 0xffffffff), False),
+ "hotspot-x": (changed(52, 1), False),
+ "hotspot-y": (changed(56, 1), False),
+ "truncated-pixels": (valid[:-1], False),
+ "oversized-unselected": (cursor_bytes([CURSOR_FRAME, CURSOR_OVERSIZED]), False),
+ "truncated-unselected": (cursor_bytes([CURSOR_FRAME, CURSOR_TRUNCATED]), False),
+ "too-many-unselected-frames": (cursor_bytes([CURSOR_FRAME] + [(512, *CURSOR_FRAME[1:])] * 257), False),
+ }
+ # A comment chunk is optional metadata and must not count as an image.
+ comment = struct.pack("<6I", 20, 0xfffe0001, 1, 1, 4, 0x74736574)
+ cases["comment"] = (struct.pack("<4I", 0x72756358, 16, 0x10000, 2)
+ + struct.pack("<6I", 0xfffe0001, 1, 40, 0xfffd0002, 256, 64)
+ + comment + valid[28:], True)
+ for name, (data, accepted) in cases.items():
+ path.write_bytes(data)
+ result = helper(env, "validate", pack, ok=accepted)
+ if not accepted:
+ assert "Xcursor" in result.stderr, (name, result.stderr)
+ shutil.rmtree(pack)
+
+
def hostile_archives(tmp, env):
manifest = (FIXTURE / "theme.toml").read_bytes()
cases = {
@@ -53,6 +129,8 @@ def hostile_archives(tmp, env):
"glyph-size": [("glyphs/wifi.svg", b"x" * (64 * 1024 + 1), None)],
"format": [("theme.toml", b"[meta]\nformat = 2\n", None)],
"cursor-alias": [("cursors/cursors/default", b"../outside", stat.S_IFLNK | 0o777)],
+ "cursor-oversized": [("cursors/cursors/default", cursor_bytes([CURSOR_FRAME, CURSOR_OVERSIZED]), None)],
+ "cursor-truncated": [("cursors/cursors/default", cursor_bytes([CURSOR_FRAME, CURSOR_TRUNCATED]), None)],
}
def chunk(tag, data):
return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data))
@@ -95,6 +173,7 @@ def run():
env.pop("REDIWM_THEME", None)
env.pop("REDIWM_CONFIG", None)
env.pop("REDIWM_SOCKET", None)
+ cursor_validation(tmp, env)
hostile_archives(tmp, env)
pack = tmp / "source"
shutil.copytree(FIXTURE, pack)