From 9c1983a9e4212e9089390640e862dfeeda91ae27 Mon Sep 17 00:00:00 2001 From: voidderef-agent <84345133+voidderef@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:49:35 +0200 Subject: [PATCH] fix: harden shared IO library path lookup PR #86 derives the mapping-file directory from dladdr's dli_fname. The initial implementation assumes that the resolved name contains a directory separator. If the loader returns a bare soname, the truncation loop reduces it to its first character and reports success, causing a bogus mapping-file lookup instead of using the executable-directory fallback. Locate the final separator before copying, preserve the filesystem root, reject names that do not identify a directory, and validate the length of the returned directory rather than requiring room for the complete library filename. Link the four PIUIO and PIUBTN keyboard and joystick targets directly with libdl so their dladdr dependency remains explicit on the supported older glibc environment. --- .../main/ptapi/piubtn/joystick/CMakeLists.txt | 2 +- .../main/ptapi/piubtn/keyboard/CMakeLists.txt | 2 +- .../main/ptapi/piuio/joystick/CMakeLists.txt | 2 +- .../main/ptapi/piuio/keyboard/CMakeLists.txt | 2 +- src/main/util/proc.c | 23 +++++++++---------- src/main/util/proc.h | 3 ++- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cmake/src/main/ptapi/piubtn/joystick/CMakeLists.txt b/cmake/src/main/ptapi/piubtn/joystick/CMakeLists.txt index 1ed0445..ae1753a 100644 --- a/cmake/src/main/ptapi/piubtn/joystick/CMakeLists.txt +++ b/cmake/src/main/ptapi/piubtn/joystick/CMakeLists.txt @@ -12,4 +12,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-fPIC") # Remove library name "lib" prefix set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") -target_link_libraries(${PROJECT_NAME} ptapi-io-piubtn-joystick-util io-util util) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ptapi-io-piubtn-joystick-util io-util util dl) diff --git a/cmake/src/main/ptapi/piubtn/keyboard/CMakeLists.txt b/cmake/src/main/ptapi/piubtn/keyboard/CMakeLists.txt index 33e50de..c612159 100644 --- a/cmake/src/main/ptapi/piubtn/keyboard/CMakeLists.txt +++ b/cmake/src/main/ptapi/piubtn/keyboard/CMakeLists.txt @@ -12,4 +12,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-fPIC") # Remove library name "lib" prefix set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") -target_link_libraries(${PROJECT_NAME} ptapi-io-piubtn-keyboard-util io-util util) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ptapi-io-piubtn-keyboard-util io-util util dl) diff --git a/cmake/src/main/ptapi/piuio/joystick/CMakeLists.txt b/cmake/src/main/ptapi/piuio/joystick/CMakeLists.txt index 9dc3c1b..7782117 100644 --- a/cmake/src/main/ptapi/piuio/joystick/CMakeLists.txt +++ b/cmake/src/main/ptapi/piuio/joystick/CMakeLists.txt @@ -12,4 +12,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-fPIC") # Remove library name "lib" prefix set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") -target_link_libraries(${PROJECT_NAME} ptapi-io-piuio-joystick-util io-util util) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ptapi-io-piuio-joystick-util io-util util dl) diff --git a/cmake/src/main/ptapi/piuio/keyboard/CMakeLists.txt b/cmake/src/main/ptapi/piuio/keyboard/CMakeLists.txt index b8a40c3..bc1d62c 100644 --- a/cmake/src/main/ptapi/piuio/keyboard/CMakeLists.txt +++ b/cmake/src/main/ptapi/piuio/keyboard/CMakeLists.txt @@ -12,4 +12,4 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-fPIC") # Remove library name "lib" prefix set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") -target_link_libraries(${PROJECT_NAME} ptapi-io-piuio-keyboard-util io-util util) \ No newline at end of file +target_link_libraries(${PROJECT_NAME} ptapi-io-piuio-keyboard-util io-util util dl) diff --git a/src/main/util/proc.c b/src/main/util/proc.c index 31bd913..5d43445 100644 --- a/src/main/util/proc.c +++ b/src/main/util/proc.c @@ -210,6 +210,8 @@ bool util_proc_get_folder_path_shared_object( void *symbol, char *buffer, size_t size) { Dl_info info; + const char *separator; + size_t folder_len; if (!symbol || !buffer || size == 0) { return false; @@ -219,23 +221,20 @@ bool util_proc_get_folder_path_shared_object( return false; } - if (strlen(info.dli_fname) >= size) { + separator = strrchr(info.dli_fname, '/'); + + if (!separator) { return false; } - strcpy(buffer, info.dli_fname); + folder_len = separator == info.dli_fname ? 1 : separator - info.dli_fname; - // If shared object in the root folder, keep the single / - size_t pos = strlen(buffer) - 1; - while (pos > 0 && buffer[pos] != '/') { - buffer[pos] = '\0'; - pos--; + if (folder_len >= size) { + return false; } - // delete / - if (pos > 0) { - buffer[pos] = '\0'; - } + memcpy(buffer, info.dli_fname, folder_len); + buffer[folder_len] = '\0'; return true; } @@ -257,4 +256,4 @@ void util_proc_log_info() log_info( "========================================================================" "========================"); -} \ No newline at end of file +} diff --git a/src/main/util/proc.h b/src/main/util/proc.h index 297253a..91d2d64 100644 --- a/src/main/util/proc.h +++ b/src/main/util/proc.h @@ -50,7 +50,8 @@ bool util_proc_get_folder_path_executable_no_ld_linux( * @param symbol Pointer to any symbol in the target shared object. * @param buffer Buffer to read the path into. * @param size Size of the buffer. - * @return True on success, false on failure. + * @return True on success, false when the symbol cannot be resolved, the + * resolved name has no folder, or the buffer is too small. */ bool util_proc_get_folder_path_shared_object( void *symbol, char *buffer, size_t size);