Fixed incorrect executable path problem.

Previously, process name was grabbed from `/proc/pid/cmdline`. This was
revealed to be faulty, since the path to the executable might be
relative, and thus would change the result depending on how the program
was called. Also, it made executable renaming a viable bypass of the
entire access control.

I still don't fully undestand how I managed to not think of this before
:)
This commit is contained in:
BritishTeapot 2025-04-12 18:44:20 +02:00
parent beec6f4a4c
commit 402a5d109f
Signed by untrusted user who does not match committer: fedir
GPG Key ID: C959EE85F0C9362C

View File

@ -11,6 +11,7 @@
See the file LICENSE. See the file LICENSE.
*/ */
#include <stddef.h>
#define FUSE_USE_VERSION 31 #define FUSE_USE_VERSION 31
#define _GNU_SOURCE #define _GNU_SOURCE
@ -40,22 +41,57 @@
#include "ui-socket.h" #include "ui-socket.h"
const char *get_process_name_by_pid(const int pid) { const char *get_process_name_by_pid(const int pid) {
char *name = (char *)calloc(1024, sizeof(char)); char path[1024];
if (name) { sprintf(path, "/proc/%d/exe", pid);
sprintf(name, "/proc/%d/cmdline", pid);
FILE *file = fopen(name, "r"); char *name = realpath(path, NULL);
if (name == NULL) {
fprintf(stderr, "Could not get process name by pid %d", pid);
perror("");
}
/*
size_t namelen = 32;
ssize_t readret = 0;
char *name = NULL;
while (namelen >= (size_t)readret && readret > 0) {
namelen *= 2;
name = calloc(namelen, sizeof(char));
if (name == NULL) {
free(path);
fprintf(stderr, "Could not get get process name by pid %d", pid);
perror("");
return NULL;
}
readret = readlink(path, name, namelen);
if (readret < 0) {
free(name);
free(path);
fprintf(stderr, "Couldn't get process name by pid %d", pid);
perror("");
return NULL;
}
if (namelen >= (size_t)readret) {
free(name);
}
}
*/
return name;
/*
FILE *file = fopen(path, "r");
if (file) { if (file) {
size_t size = 0; size_t size = 0;
size = fread(name, sizeof(char), 1024, file); size = fread(path, sizeof(char), 1024, file);
if (size > 0) { if (size > 0) {
if ('\n' == name[size - 1]) { if ('\n' == path[size - 1]) {
name[size - 1] = '\0'; path[size - 1] = '\0';
} }
} }
fclose(file); fclose(file);
} }
} */
return name;
} }
// TODO: move this somewhere else // TODO: move this somewhere else
@ -83,6 +119,7 @@ static void *xmp_init(struct fuse_conn_info *conn, struct fuse_config *cfg) {
cfg->entry_timeout = 0; cfg->entry_timeout = 0;
cfg->attr_timeout = 0; cfg->attr_timeout = 0;
cfg->negative_timeout = 0; cfg->negative_timeout = 0;
fprintf(stderr, "%d\n", getpid());
return NULL; return NULL;
} }