aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-04-06 15:49:36 +0200
committerdec05eba <dec05eba@protonmail.com>2024-04-06 15:49:36 +0200
commite24aba6e046fc184ff6925e92a62dc2fe691dc15 (patch)
tree1c68e8f68bb7b4da512d60036509de84a79e4531
parent9776cc4e7e9037bde5d5ece69f7d1f959da4bfd5 (diff)
Resolve paths with readlink and realpath for path comparison
-rw-r--r--main.c26
1 files changed, 17 insertions, 9 deletions
diff --git a/main.c b/main.c
index e59691f..ba046f5 100644
--- a/main.c
+++ b/main.c
@@ -9,15 +9,14 @@
#include <sys/sendfile.h>
#include <sys/capability.h>
-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);
}