Added folder globbing for permanent permissions
This commit is contained in:
		@@ -39,63 +39,10 @@
 | 
			
		||||
#include <sys/file.h> /* flock(2) */
 | 
			
		||||
 | 
			
		||||
#include "fuse_operations.h"
 | 
			
		||||
#include "proc_operations.h"
 | 
			
		||||
#include "sourcefs.h"
 | 
			
		||||
#include "ui-socket.h"
 | 
			
		||||
 | 
			
		||||
char *get_process_name_by_pid(const int pid) {
 | 
			
		||||
  char path[1024];
 | 
			
		||||
  sprintf(path, "/proc/%d/exe", pid);
 | 
			
		||||
 | 
			
		||||
  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) {
 | 
			
		||||
    size_t size = 0;
 | 
			
		||||
    size = fread(path, sizeof(char), 1024, file);
 | 
			
		||||
    if (size > 0) {
 | 
			
		||||
      if ('\n' == path[size - 1]) {
 | 
			
		||||
        path[size - 1] = '\0';
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    fclose(file);
 | 
			
		||||
  }
 | 
			
		||||
  */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *xmp_init(struct fuse_conn_info *conn, struct fuse_config *cfg) {
 | 
			
		||||
  (void)conn;
 | 
			
		||||
  cfg->use_ino = 1;
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@
 | 
			
		||||
 | 
			
		||||
#include "perm_permissions_table.h"
 | 
			
		||||
#include "access_t.h"
 | 
			
		||||
#include "proc_operations.h"
 | 
			
		||||
#include "process_info.h"
 | 
			
		||||
#include "set_mode_t.h"
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
@@ -204,13 +205,16 @@ void destroy_perm_permissions_table(void) { sqlite3_close(perm_database); }
 | 
			
		||||
 * @return: access status - ALLOW, DENY or NDEF in case if no information was
 | 
			
		||||
 * found
 | 
			
		||||
 */
 | 
			
		||||
access_t check_perm_access(const char *filename, struct process_info pi) {
 | 
			
		||||
access_t check_perm_access_noparent(const char *filename,
 | 
			
		||||
                                    struct process_info pi) {
 | 
			
		||||
 | 
			
		||||
  char *query = NULL;
 | 
			
		||||
  int ret = asprintf(&query,
 | 
			
		||||
                     "SELECT * FROM %s WHERE executable = \'%s\' "
 | 
			
		||||
                     "AND filename = \'%s\';",
 | 
			
		||||
                     table_name, pi.name, filename);
 | 
			
		||||
                     "AND ((\'%s\' LIKE CONCAT(filename, \'%%\') AND filename "
 | 
			
		||||
                     "GLOB \'*/\') OR filename = \'%s\');",
 | 
			
		||||
                     table_name, pi.name, filename, filename);
 | 
			
		||||
  fprintf(stderr, "query: %s\n", query);
 | 
			
		||||
 | 
			
		||||
  if (ret < 0) {
 | 
			
		||||
    // If asprintf fails, the contents of query are undefined (see man
 | 
			
		||||
@@ -241,6 +245,45 @@ access_t check_perm_access(const char *filename, struct process_info pi) {
 | 
			
		||||
  return NDEF;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Checks if the process or any of it's parents have permanent access to the
 | 
			
		||||
 * file.
 | 
			
		||||
 *
 | 
			
		||||
 * @param filename: The file that the process is trying to access
 | 
			
		||||
 * @pram pi: The process information
 | 
			
		||||
 * @return: access status - ALLOW, DENY or NDEF in case if no information was
 | 
			
		||||
 * found. Does not return ALLOW_TEMP or DENY_TEMP.
 | 
			
		||||
 * @note: In case one of the parent processes is killed while this function
 | 
			
		||||
 * execution the result is not guranteed to be correct. It should only lead to
 | 
			
		||||
 * false negatives, though.
 | 
			
		||||
 */
 | 
			
		||||
access_t check_perm_access(const char *filename, struct process_info pi) {
 | 
			
		||||
  if (pi.PID == 0) {
 | 
			
		||||
    return NDEF;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  struct process_info current_pi = pi;
 | 
			
		||||
  current_pi.name = strdup(current_pi.name);
 | 
			
		||||
  while (current_pi.PID != 0) {
 | 
			
		||||
    access_t access = check_perm_access_noparent(filename, current_pi);
 | 
			
		||||
    free(current_pi.name);
 | 
			
		||||
    if (access != NDEF) {
 | 
			
		||||
      return access;
 | 
			
		||||
    }
 | 
			
		||||
    current_pi.name = NULL;
 | 
			
		||||
    while (current_pi.name == NULL) {
 | 
			
		||||
      current_pi.PID = get_parent_pid(current_pi.PID);
 | 
			
		||||
      if (current_pi.PID != 0) {
 | 
			
		||||
        current_pi.name = get_process_name_by_pid(current_pi.PID);
 | 
			
		||||
      } else {
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return NDEF;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Gives permanent access to the process to the file.
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										45
									
								
								src/proc_operations.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/proc_operations.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
#include "proc_operations.h"
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
char *get_process_name_by_pid(const int pid) {
 | 
			
		||||
  char path[1024];
 | 
			
		||||
  sprintf(path, "/proc/%d/exe", pid);
 | 
			
		||||
 | 
			
		||||
  char *name = realpath(path, NULL);
 | 
			
		||||
  if (name == NULL) {
 | 
			
		||||
    fprintf(stderr, "Could not get process name by pid %d", pid);
 | 
			
		||||
    perror("");
 | 
			
		||||
  }
 | 
			
		||||
  return name;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Finds the parent process ID of a given process.
 | 
			
		||||
 *
 | 
			
		||||
 * @param pid: The process ID of the process to find the parent of
 | 
			
		||||
 * @return: The parent process ID, or 0 if the parent process ID could not be
 | 
			
		||||
 * found
 | 
			
		||||
 */
 | 
			
		||||
pid_t get_parent_pid(pid_t pid) {
 | 
			
		||||
  pid_t ppid = 0;
 | 
			
		||||
  char path[256];
 | 
			
		||||
  snprintf(path, sizeof(path), "/proc/%u/status", pid);
 | 
			
		||||
 | 
			
		||||
  FILE *file = fopen(path, "r");
 | 
			
		||||
  if (file == NULL) {
 | 
			
		||||
    perror("Failed to open /proc/<pid>/status");
 | 
			
		||||
    return 0;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  char line[256];
 | 
			
		||||
  while (fgets(line, sizeof(line), file)) {
 | 
			
		||||
    if (sscanf(line, "PPid:\t%d", &ppid) == 1) {
 | 
			
		||||
      fclose(file);
 | 
			
		||||
      return ppid;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fclose(file);
 | 
			
		||||
  return 0; // Parent PID not found
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								src/proc_operations.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/proc_operations.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
#ifndef PROC_OPERATIONS
 | 
			
		||||
#define PROC_OPERATIONS
 | 
			
		||||
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
char *get_process_name_by_pid(const int pid);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Finds the parent process ID of a given process.
 | 
			
		||||
 *
 | 
			
		||||
 * @param pid: The process ID of the process to find the parent of
 | 
			
		||||
 * @return: The parent process ID, or 0 if the parent process ID could not be
 | 
			
		||||
 * found
 | 
			
		||||
 */
 | 
			
		||||
pid_t get_parent_pid(pid_t pid);
 | 
			
		||||
 | 
			
		||||
#endif // !PROC_OPERATIONS
 | 
			
		||||
@@ -9,6 +9,7 @@
 | 
			
		||||
#include "temp_permissions_table.h"
 | 
			
		||||
#include "access_t.h"
 | 
			
		||||
#include "cc.h"
 | 
			
		||||
#include "proc_operations.h"
 | 
			
		||||
#include "process_info.h"
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
@@ -144,36 +145,6 @@ access_t check_temp_access_noparent(const char *filename, pid_t pid) {
 | 
			
		||||
  return NDEF;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Finds the parent process ID of a given process.
 | 
			
		||||
 *
 | 
			
		||||
 * @param pid: The process ID of the process to find the parent of
 | 
			
		||||
 * @return: The parent process ID, or 0 if the parent process ID could not be
 | 
			
		||||
 * found
 | 
			
		||||
 */
 | 
			
		||||
pid_t get_parent_pid(pid_t pid) {
 | 
			
		||||
  pid_t ppid = 0;
 | 
			
		||||
  char path[256];
 | 
			
		||||
  snprintf(path, sizeof(path), "/proc/%u/status", pid);
 | 
			
		||||
 | 
			
		||||
  FILE *file = fopen(path, "r");
 | 
			
		||||
  if (file == NULL) {
 | 
			
		||||
    perror("Failed to open /proc/<pid>/status");
 | 
			
		||||
    return 0;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  char line[256];
 | 
			
		||||
  while (fgets(line, sizeof(line), file)) {
 | 
			
		||||
    if (sscanf(line, "PPid:\t%d", &ppid) == 1) {
 | 
			
		||||
      fclose(file);
 | 
			
		||||
      return ppid;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fclose(file);
 | 
			
		||||
  return 0; // Parent PID not found
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Checks if the process or any of it's parents have temporary access to the
 | 
			
		||||
 * file.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user