Fixed wrong pid bug

The issue was that the thread ID wasn't factored in. A presumption was,
that FUSE already returned the PID, not TID. The issue was fixed by
implementing a function that translates the TID to PID.
This commit is contained in:
fedir 2025-05-14 20:37:32 +02:00
parent 33f55384bc
commit e4dbc5becc
Signed by: fedir
GPG Key ID: C959EE85F0C9362C
5 changed files with 56 additions and 3 deletions

View File

@ -272,7 +272,7 @@ access_t check_perm_access(const char *filename, struct process_info pi) {
} }
current_pi.name = NULL; current_pi.name = NULL;
while (current_pi.name == NULL) { while (current_pi.name == NULL) {
current_pi.PID = get_parent_pid(current_pi.PID); current_pi.PID = get_main_thread_pid(get_parent_pid(current_pi.PID));
if (current_pi.PID != 0) { if (current_pi.PID != 0) {
current_pi.name = get_process_name_by_pid(current_pi.PID); current_pi.name = get_process_name_by_pid(current_pi.PID);
} else { } else {

View File

@ -7,9 +7,49 @@
*/ */
#include "proc_operations.h" #include "proc_operations.h"
#include <linux/limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
/**
* @brief Returns the PID of the main thread (i.e., the process ID) of the
* process that the given thread ID (tid) belongs to.
*
* @param tid The thread ID (TID) of any thread in the process.
* @return pid_t The process ID (main thread's PID), or -1 on error.
*/
pid_t get_main_thread_pid(pid_t tid) {
// Validate input
if (tid <= 0) {
// errno = EINVAL;
return 0;
}
char path[PATH_MAX];
snprintf(path, sizeof(path), "/proc/%d/status", tid);
FILE *fp = fopen(path, "r");
if (!fp) {
return 0; // Could not open the file
}
pid_t tgid = 0;
char line[256];
while (fgets(line, sizeof(line), fp)) {
if (sscanf(line, "Tgid: %d", &tgid) == 1) {
break;
}
}
fclose(fp);
if (tgid != tid) {
fprintf(stderr, "The tid and and pid wasn't equal. tid:%d, pid:%d.\n", tid,
tgid);
}
return tgid;
}
char *get_process_name_by_pid(const int pid) { char *get_process_name_by_pid(const int pid) {
char path[1024]; char path[1024];
sprintf(path, "/proc/%d/exe", pid); sprintf(path, "/proc/%d/exe", pid);

View File

@ -22,4 +22,13 @@ char *get_process_name_by_pid(const int pid);
*/ */
pid_t get_parent_pid(pid_t pid); pid_t get_parent_pid(pid_t pid);
/**
* @brief Returns the PID of the main thread (i.e., the process ID) of the
* process that the given thread ID (tid) belongs to.
*
* @param tid The thread ID (TID) of any thread in the process.
* @return pid_t The process ID (main thread's PID), or -1 on error.
*/
pid_t get_main_thread_pid(pid_t tid);
#endif // !PROC_OPERATIONS #endif // !PROC_OPERATIONS

View File

@ -10,7 +10,11 @@
#define PROCESS_INFO_H #define PROCESS_INFO_H
#include "proc_operations.h" #include "proc_operations.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
struct process_info { struct process_info {
pid_t PID; pid_t PID;
char *name; char *name;
@ -18,7 +22,7 @@ struct process_info {
static inline struct process_info get_process_info(pid_t pid) { static inline struct process_info get_process_info(pid_t pid) {
struct process_info pi; struct process_info pi;
pi.PID = pid; pi.PID = get_main_thread_pid(pid);
pi.name = get_process_name_by_pid(pi.PID); pi.name = get_process_name_by_pid(pi.PID);
if (pi.name == NULL) { if (pi.name == NULL) {
pi.PID = 0; pi.PID = 0;

View File

@ -237,7 +237,7 @@ access_t check_temp_access(const char *filename, struct process_info pi) {
if (access != NDEF) { if (access != NDEF) {
return access; return access;
} }
current_pid = get_parent_pid(current_pid); current_pid = get_main_thread_pid(get_parent_pid(current_pid));
} }
return NDEF; return NDEF;