From 2ccc8204ad97c9615ac64563dd4c9d66a2fe8a09 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Mon, 5 Oct 2026 12:44:16 -0400 Subject: [PATCH] Fix Xwayland dma-buf leak: close Zink's emulated GEM handles on KGSL (#275) Close Zink's emulated GEM handles on KGSL (Xwayland dma-buf leak) The session preload hands out GEM handles on the KGSL render node by duplicating the dma-buf descriptor, and released them only through drmCloseBufferHandle. Zink gives handles back with drmIoctl(DRM_IOCTL_GEM_CLOSE) instead, which KGSL refuses, so every duplicated descriptor stayed open and kept its buffer alive. Xwayland asks for a handle for each buffer it imports through gbm. With "Force game windows fullscreen" fighting a game's own resolution (DiRT 3 at 1280x720), the game recreates its swapchain many times a second and Xwayland piled up ~2,000 old swapchain images (6+ GB) in seconds until Android killed the session. A preload trace on the device put 2,090 of 2,118 open dma-buf descriptors in Xwayland on drmPrimeFDToHandle called from Zink. Handle GEM_CLOSE on the KGSL node in drmIoctl, and check the three DRM symbols are exported in CI. Co-authored-by: The412Banner --- .github/workflows/build.yml | 2 +- tools/linuxfs/preload/drm.c | 51 +++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a9f186c82e..ca91945da5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -343,7 +343,7 @@ jobs: echo "::error::libfakeinput.so exports the C++ runtime"; exit 1 fi SESSION=$(syms "$D/libblsession.so") - for sym in socket bind getsockname setsockopt statfs statvfs syscall shm_open shm_unlink mmap mmap64 munmap; do + for sym in socket bind getsockname setsockopt statfs statvfs syscall shm_open shm_unlink mmap mmap64 munmap drmPrimeFDToHandle drmCloseBufferHandle drmIoctl; do grep -qx "$sym" <<<"$SESSION" || { echo "::error::libblsession.so does not export $sym"; exit 1; } done test -f "$D/usr/local/bin/droiddeck-session" diff --git a/tools/linuxfs/preload/drm.c b/tools/linuxfs/preload/drm.c index f2d3ae1926..82b072b1b1 100644 --- a/tools/linuxfs/preload/drm.c +++ b/tools/linuxfs/preload/drm.c @@ -12,11 +12,19 @@ #include #include #include +#include #include #include #define MAX_HANDLES 4096 +/* DRM_IOCTL_GEM_CLOSE from drm.h: _IOW('d', 0x09, struct drm_gem_close { u32 handle, pad; }). */ +struct gem_close { + uint32_t handle; + uint32_t pad; +}; +#define GEM_CLOSE _IOW('d', 0x09, struct gem_close) + static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static int handle_fds[MAX_HANDLES]; static dev_t kgsl_dev; @@ -82,20 +90,47 @@ int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd) { return ret; } +/* Drops a handle's descriptor; false when the handle was never given out. */ +static int release_handle(uint32_t handle) { + int found = 0; + pthread_mutex_lock(&lock); + if (handle > 0 && handle < MAX_HANDLES && handle_fds[handle] > 0) { + close(handle_fds[handle]); + handle_fds[handle] = 0; + found = 1; + } + pthread_mutex_unlock(&lock); + return found; +} + int drmCloseBufferHandle(int fd, uint32_t handle) { if (!is_kgsl(fd)) { int (*fn)(int, uint32_t) = (int (*)(int, uint32_t)) real("drmCloseBufferHandle"); return fn ? fn(fd, handle) : -ENOSYS; } - int ret = -EINVAL; - pthread_mutex_lock(&lock); - if (handle > 0 && handle < MAX_HANDLES && handle_fds[handle] > 0) { - close(handle_fds[handle]); - handle_fds[handle] = 0; - ret = 0; + return release_handle(handle) ? 0 : -EINVAL; +} + +/* + * Zink gives its handles back with the GEM_CLOSE ioctl itself, not drmCloseBufferHandle + * (zink_bo.c, bo_destroy). KGSL refuses the ioctl, so the duplicated descriptor stayed open and + * kept the whole buffer alive: Xwayland asks for a handle for every buffer it imports, and a game + * whose swapchain was recreated on each resize piled up gigabytes of old swapchain images until + * Android killed the session. + */ +int drmIoctl(int fd, unsigned long request, void *arg) { + if (request == GEM_CLOSE && arg && is_kgsl(fd)) { + if (release_handle(((struct gem_close *) arg)->handle)) { + return 0; + } + errno = EINVAL; + return -1; } - pthread_mutex_unlock(&lock); - return ret; + int (*fn)(int, unsigned long, void *) = (int (*)(int, unsigned long, void *)) real("drmIoctl"); + if (!fn) { + return -1; + } + return fn(fd, request, arg); } /*