From e24aba6e046fc184ff6925e92a62dc2fe691dc15 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 6 Apr 2024 15:49:36 +0200 Subject: Resolve paths with readlink and realpath for path comparison --- main.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index e59691f..ba046f5 100644 --- a/main.c +++ b/main.c @@ -9,15 +9,14 @@ #include #include -static int get_self_filepath(char *buffer) { - char self_path[PATH_MAX]; - - ssize_t bytes_written = readlink("/proc/self/exe", self_path, sizeof(self_path) - 1); +static int readlink_realpath(const char *filepath, char *buffer) { + char symlinked_path[PATH_MAX]; + ssize_t bytes_written = readlink(filepath, symlinked_path, sizeof(symlinked_path) - 1); if(bytes_written == -1) return 0; - self_path[bytes_written] = '\0'; - if(!realpath(self_path, buffer)) + symlinked_path[bytes_written] = '\0'; + if(!realpath(symlinked_path, buffer)) return 0; return 1; @@ -120,7 +119,7 @@ int main(int argc, char **argv) { const char *user_homepath = argv[3]; char self_path[PATH_MAX]; - if(!get_self_filepath(self_path)) + if(!readlink_realpath("/proc/self/exe", self_path)) return 1; char kms_server_proxy_local_filepath[PATH_MAX]; @@ -130,8 +129,17 @@ int main(int argc, char **argv) { const char *args[] = { gsr_kms_server_filepath, initial_socket_path, card_path, NULL }; return execv(args[0], (char *const*)args); } else if(file_has_sys_admin_capability(kms_server_proxy_local_filepath)) { - if(strcmp(self_path, kms_server_proxy_local_filepath) != 0) { - const char *args[] = { kms_server_proxy_local_filepath, initial_socket_path, card_path, user_homepath, NULL }; + /* Need to resolve kms_server_proxy_local_filepath because /home can be a symlink to another location */ + char kms_server_proxy_local_filepath_full[PATH_MAX]; + if(!readlink_realpath(kms_server_proxy_local_filepath, kms_server_proxy_local_filepath_full)) + return 1; + + /* + Run cached ~/.local/share/gpu-screen-recorder/kms-server-proxy which has sys admin capability. + The one in flatpak location gets capabilities overwritten on flatpak update. + */ + if(strcmp(self_path, kms_server_proxy_local_filepath_full) != 0) { + const char *args[] = { kms_server_proxy_local_filepath_full, initial_socket_path, card_path, user_homepath, NULL }; return execv(args[0], (char *const*)args); } -- cgit v1.2.3