From 238ef76c5c83c189291b44cb4f8382b1f29c30da Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Tue, 24 Nov 2026 20:19:53 -0500 Subject: [PATCH] fix(preload): apps that reload libudev no longer crash in the input wrappers (#3180) fix(preload): keep libudev loaded while the input wrappers use it Programs that close libudev and open it again crashed in the next wrapped udev call: the wrappers kept function pointers into the unmapped copy. Pin libudev with RTLD_NODELETE when it is opened so those pointers and the stored handle stay valid. --- tools/linuxfs/preload/inputudev.c | 8 +++--- tools/linuxfs/preload/robust.c | 5 ++-- tools/tests/test_inputudev_preload.py | 37 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/tools/linuxfs/preload/inputudev.c b/tools/linuxfs/preload/inputudev.c index 54aac381fa..346541c347 100644 --- a/tools/linuxfs/preload/inputudev.c +++ b/tools/linuxfs/preload/inputudev.c @@ -419,9 +419,11 @@ static void *real_dlsym(void); * searches, so the handle the program opened is the only way back to the real implementation. */ static _Atomic(void *) udev_handle; -__attribute__((visibility("hidden"))) void bl_inputudev_note_dlopen(const char *path, void *handle) { - if (handle != NULL && path != NULL && strstr(path, "libudev") != NULL) - atomic_store_explicit(&udev_handle, handle, memory_order_release); +__attribute__((visibility("hidden"))) void bl_inputudev_note_dlopen(const char *path, void *handle, + void *(*open)(const char *, int)) { + if (handle == NULL || path == NULL || strstr(path, "libudev") == NULL) return; + if (open(path, RTLD_LAZY | RTLD_NOLOAD | RTLD_NODELETE) == NULL) return; + atomic_store_explicit(&udev_handle, handle, memory_order_release); } static void *next_symbol(const char *name) { diff --git a/tools/linuxfs/preload/robust.c b/tools/linuxfs/preload/robust.c index 9d5707e352..12df5d662b 100644 --- a/tools/linuxfs/preload/robust.c +++ b/tools/linuxfs/preload/robust.c @@ -100,7 +100,8 @@ static long path_statx(int dirfd, const char *path, int flags, unsigned int mask return next_statx(dirfd, path, flags, mask, out); } -void bl_inputudev_note_dlopen(const char *path, void *handle) __attribute__((weak, visibility("hidden"))); +void bl_inputudev_note_dlopen(const char *path, void *handle, void *(*open)(const char *, int)) + __attribute__((weak, visibility("hidden"))); void *dlopen(const char *filename, int flags) { void *handle; @@ -114,7 +115,7 @@ void *dlopen(const char *filename, int flags) { flags &= ~RTLD_DEEPBIND; } handle = real_dlopen != NULL ? real_dlopen(filename, flags) : NULL; - if (handle != NULL && bl_inputudev_note_dlopen != NULL) bl_inputudev_note_dlopen(filename, handle); + if (handle != NULL && bl_inputudev_note_dlopen != NULL) bl_inputudev_note_dlopen(filename, handle, real_dlopen); return handle; } diff --git a/tools/tests/test_inputudev_preload.py b/tools/tests/test_inputudev_preload.py index 68d55403fe..06f8a8f95f 100644 --- a/tools/tests/test_inputudev_preload.py +++ b/tools/tests/test_inputudev_preload.py @@ -223,6 +223,9 @@ PROGRAM = r""" #include #include #include +#include +#include +#include #include #include @@ -466,6 +469,36 @@ static void run_probe(void) { printf("pad %d\n", new_device(&udev, PAD) != NULL); } +static int loaded_span(struct dl_phdr_info *info, size_t size, void *data) { + uintptr_t *span = data; + int i; + + (void)size; + if (info->dlpi_addr != span[0]) return 0; + for (i = 0; i < info->dlpi_phnum; i++) + if (info->dlpi_phdr[i].p_type == PT_LOAD && info->dlpi_phdr[i].p_vaddr + info->dlpi_phdr[i].p_memsz > span[1]) + span[1] = info->dlpi_phdr[i].p_vaddr + info->dlpi_phdr[i].p_memsz; + return 1; +} + +static void run_reload(const char *path) { + struct link_map *map = NULL; + uintptr_t span[2] = {0, 0}; + void *first = enumerate_new(NULL); + void *second; + + dlinfo(lib, RTLD_DI_LINKMAP, &map); + span[0] = map->l_addr; + dl_iterate_phdr(loaded_span, span); + dlclose(lib); + mmap((void *)span[0], span[1], PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0); + lib = dlopen(path, RTLD_NOW | RTLD_LOCAL); + enumerate_new = sym("udev_enumerate_new"); + list = sym("udev_enumerate_get_list_entry"); + second = enumerate_new(NULL); + printf("reload %s %s\n", entry_name(list(first)), entry_name(list(second))); +} + int main(int argc, char **argv) { char link[4096]; @@ -510,6 +543,7 @@ int main(int argc, char **argv) { else if (strcmp(argv[1], "every") == 0) run_every(argc - 3, argv + 3); else if (strcmp(argv[1], "stable") == 0) run_stable(); else if (strcmp(argv[1], "enumerate") == 0) run_enumerate(); + else if (strcmp(argv[1], "reload") == 0) run_reload(argv[2]); else run_udev(); fflush(stdout); return 0; @@ -650,6 +684,9 @@ class InputUdevPreloadTest(unittest.TestCase): "nothing added scan -1", ]) + def test_a_program_that_closes_and_reopens_libudev_keeps_working(self): + self.assertEqual(self.run_program("reload"), ["reload real0 real1"]) + def test_only_the_steam_client_and_its_remote_play_client_get_the_real_libudev(self): for path, pad in (("steamrt64/steam", 0), ("steamrtarm64/steam", 0), ("steamrt64/streaming_client", 0), ("steamrtarm64/streaming_client", 0), ("steamrt64/steam-probe", 1),