From ec1af7c92bd5377e2f27241ffba8464aff7be3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 22 Feb 2026 17:31:41 +0800 Subject: [PATCH] Platform (OpenBSD): improves executable path retrieval method Fixes #2195 --- src/common/impl/FFPlatform_unix.c | 81 ++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/src/common/impl/FFPlatform_unix.c b/src/common/impl/FFPlatform_unix.c index c26a625e5..8e590dd34 100644 --- a/src/common/impl/FFPlatform_unix.c +++ b/src/common/impl/FFPlatform_unix.c @@ -1,5 +1,6 @@ #include "FFPlatform_private.h" #include "common/FFstrbuf.h" +#include "common/arrayUtils.h" #include "common/stringUtils.h" #include "common/io.h" #include "fastfetch_config.h" @@ -13,8 +14,12 @@ #ifdef __APPLE__ #include #include -#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) +#elif defined(__FreeBSD__) || defined(__NetBSD__) #include +#elif defined(__OpenBSD__) + #include + #include + #include "common/path.h" #elif defined(__HAIKU__) #include #include @@ -46,7 +51,81 @@ static void getExePath(FFPlatform* platform) else exePathLen--; // remove terminating NUL #elif defined(__OpenBSD__) + // OpenBSD doesn't have a reliable way to get the executable path. + // Current implementation uses argv[0], which can be easily spoofed. + // See #2195 size_t exePathLen = 0; + kvm_t* kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL); + if (kd) + { + int kpCount; + struct kinfo_proc* kp = kvm_getprocs(kd, KERN_PROC_PID, (pid_t) platform->pid, sizeof(*kp), &kpCount); + if (kp && kpCount == 1) + { + char** argv = kvm_getargv(kd, kp, 0); + if (argv && argv[0]) + { + char* arg0 = argv[0]; + if (arg0[0]) + { + if (strchr(arg0, '/') != NULL) // likely a path (absolute or relative) + { + exePathLen = strlen(arg0); + if (exePathLen < ARRAY_SIZE(exePath)) + { + memcpy(exePath, arg0, exePathLen); + exePath[exePathLen] = '\0'; + } + else + exePathLen = 0; + } + else + { + FF_STRBUF_AUTO_DESTROY tmpPath = ffStrbufCreate(); + if (ffFindExecutableInPath(arg0, &tmpPath) == NULL && tmpPath.length < ARRAY_SIZE(exePath)) + { + memcpy(exePath, tmpPath.chars, tmpPath.length + 1); + exePathLen = tmpPath.length; + } + } + + if (exePathLen > 0) + { + struct stat st; + if (stat(exePath, &st) == 0 && S_ISREG(st.st_mode)) + { + int cntp; + struct kinfo_file* kf = kvm_getfiles(kd, KERN_FILE_BYPID, platform->pid, sizeof(*kf), &cntp); + if (kf) + { + int i; + for (i = 0; i < cntp; i++) + { + if (kf[i].fd_fd == KERN_FILE_TEXT) + { + // KERN_FILE_TEXT is the executable file, not a shared library, and should be unique in the list. + if (st.st_dev != (dev_t)kf[i].va_fsid || st.st_ino != (ino_t)kf[i].va_fileid) + i = -1; + break; + } + } + if (i < 0) + exePathLen = 0; + } + else + { + // If we can't get the list of open files, we can't verify that the file is actually the executable + // Assume it is + } + } + else + exePathLen = 0; + } + } + } + } + kvm_close(kd); + } #elif defined(__sun) ssize_t exePathLen = readlink("/proc/self/path/a.out", exePath, sizeof(exePath) - 1); if (exePathLen >= 0)