Added database protection with setuid.
Added the initial support for the database protection with the setuid mechanism. In the beginning the program creates(or opens) the database as a special user, and then switches to the real uid and functions normally.
This commit is contained in:
		
							
								
								
									
										14
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/main.c
									
									
									
									
									
								
							@@ -10,6 +10,8 @@
 | 
				
			|||||||
  See the file LICENSE.
 | 
					  See the file LICENSE.
 | 
				
			||||||
*/
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <sys/types.h>
 | 
				
			||||||
 | 
					#include <unistd.h>
 | 
				
			||||||
#define FUSE_USE_VERSION 31
 | 
					#define FUSE_USE_VERSION 31
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define _GNU_SOURCE
 | 
					#define _GNU_SOURCE
 | 
				
			||||||
@@ -28,17 +30,17 @@ const char *mountpoint = NULL;
 | 
				
			|||||||
int main(int argc, char *argv[]) {
 | 
					int main(int argc, char *argv[]) {
 | 
				
			||||||
  umask(0);
 | 
					  umask(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  mountpoint = realpath(argv[argc - 1], NULL);
 | 
					  int ret = init_ui_socket();
 | 
				
			||||||
 | 
					 | 
				
			||||||
  int ret = source_init(mountpoint);
 | 
					 | 
				
			||||||
  if (ret != 0) {
 | 
					  if (ret != 0) {
 | 
				
			||||||
    perror("source_init");
 | 
					    fprintf(stderr, "Could not initalize ui-socket.\n");
 | 
				
			||||||
    exit(EXIT_FAILURE);
 | 
					    exit(EXIT_FAILURE);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ret = init_ui_socket();
 | 
					  mountpoint = realpath(argv[argc - 1], NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  ret = source_init(mountpoint);
 | 
				
			||||||
  if (ret != 0) {
 | 
					  if (ret != 0) {
 | 
				
			||||||
    fprintf(stderr, "Could not initalize ui-socket.\n");
 | 
					    perror("source_init");
 | 
				
			||||||
    exit(EXIT_FAILURE);
 | 
					    exit(EXIT_FAILURE);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,10 +1,16 @@
 | 
				
			|||||||
#include "perm_permissions_table.h"
 | 
					#include "perm_permissions_table.h"
 | 
				
			||||||
#include "process_info.h"
 | 
					#include "process_info.h"
 | 
				
			||||||
 | 
					#include <fcntl.h>
 | 
				
			||||||
 | 
					#include <pthread.h>
 | 
				
			||||||
#include <sqlite3.h>
 | 
					#include <sqlite3.h>
 | 
				
			||||||
#include <stddef.h>
 | 
					#include <stddef.h>
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <sys/fsuid.h>
 | 
				
			||||||
 | 
					#include <sys/stat.h>
 | 
				
			||||||
 | 
					#include <sys/types.h>
 | 
				
			||||||
 | 
					#include <unistd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sqlite3 *perm_database = NULL;
 | 
					sqlite3 *perm_database = NULL;
 | 
				
			||||||
const char *const table_name = "permissions";
 | 
					const char *const table_name = "permissions";
 | 
				
			||||||
@@ -12,6 +18,38 @@ const char *const table_name = "permissions";
 | 
				
			|||||||
const int column_count = 2;
 | 
					const int column_count = 2;
 | 
				
			||||||
const char *const schema[] = {"executable", "filename"};
 | 
					const char *const schema[] = {"executable", "filename"};
 | 
				
			||||||
const char *const types[] = {"TEXT", "TEXT"};
 | 
					const char *const types[] = {"TEXT", "TEXT"};
 | 
				
			||||||
 | 
					uid_t ruid, euid, current_pid;
 | 
				
			||||||
 | 
					pthread_mutex_t uid_switch = PTHREAD_MUTEX_INITIALIZER;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void set_db_fsuid() {
 | 
				
			||||||
 | 
					  pthread_mutex_lock(&uid_switch);
 | 
				
			||||||
 | 
					  if (current_pid == ruid)
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  int status = -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  status = setfsuid(ruid);
 | 
				
			||||||
 | 
					  if (status < 0) {
 | 
				
			||||||
 | 
					    fprintf(stderr, "Couldn't set uid to %d.\n", ruid);
 | 
				
			||||||
 | 
					    exit(status);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  pthread_mutex_unlock(&uid_switch);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void set_real_fsuid() {
 | 
				
			||||||
 | 
					  pthread_mutex_lock(&uid_switch);
 | 
				
			||||||
 | 
					  if (current_pid == ruid)
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  int status = -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  status = setfsuid(ruid);
 | 
				
			||||||
 | 
					  if (status < 0) {
 | 
				
			||||||
 | 
					    fprintf(stderr, "Couldn't set uid to %d.\n", euid);
 | 
				
			||||||
 | 
					    exit(status);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  pthread_mutex_unlock(&uid_switch);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int check_table_col_schema(void *notused, int argc, char **argv,
 | 
					static int check_table_col_schema(void *notused, int argc, char **argv,
 | 
				
			||||||
                                  char **colname) {
 | 
					                                  char **colname) {
 | 
				
			||||||
@@ -21,15 +59,16 @@ static int check_table_col_schema(void *notused, int argc, char **argv,
 | 
				
			|||||||
    fprintf(stderr, "Unexpected amount of arguments given to the callback.\n");
 | 
					    fprintf(stderr, "Unexpected amount of arguments given to the callback.\n");
 | 
				
			||||||
    return 1;
 | 
					    return 1;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  int i = atoi(argv[0]);
 | 
					  int column_num = atoi(argv[0]);
 | 
				
			||||||
  if (i >= column_count) {
 | 
					  if (column_num >= column_count) {
 | 
				
			||||||
    fprintf(stderr, "Table contains more columns than expected.\n");
 | 
					    fprintf(stderr, "Table contains more columns than expected.\n");
 | 
				
			||||||
    return 1;
 | 
					    return 1;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if (strcmp(schema[i], argv[1]) == 0 && strcmp(types[i], argv[2]) == 0) {
 | 
					  if (strcmp(schema[column_num], argv[1]) == 0 &&
 | 
				
			||||||
 | 
					      strcmp(types[column_num], argv[2]) == 0) {
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  fprintf(stderr, "Column %d does not conform to the schema.\n", i);
 | 
					  fprintf(stderr, "Column %d does not conform to the schema.\n", column_num);
 | 
				
			||||||
  return 1;
 | 
					  return 1;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -101,14 +140,28 @@ int ensure_database_schema() {
 | 
				
			|||||||
 * @return: 0 on success, -1 on failure
 | 
					 * @return: 0 on success, -1 on failure
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
int init_perm_permissions_table(const char *db_filename) {
 | 
					int init_perm_permissions_table(const char *db_filename) {
 | 
				
			||||||
 | 
					  // we don't want the group and others to access the db
 | 
				
			||||||
 | 
					  umask(0077);
 | 
				
			||||||
 | 
					  ruid = getuid();
 | 
				
			||||||
 | 
					  euid = geteuid();
 | 
				
			||||||
 | 
					  fprintf(stderr, "Running with uid: %d, gid: %d\n", euid, getegid());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (sqlite3_open(db_filename, &perm_database)) {
 | 
					  if (sqlite3_open(db_filename, &perm_database)) {
 | 
				
			||||||
    perror("Can't open permanent permissions database:");
 | 
					    perror("Can't open permanent permissions database:");
 | 
				
			||||||
    return -1;
 | 
					    return -1;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  umask(0);
 | 
				
			||||||
  if (ensure_database_schema()) {
 | 
					  if (ensure_database_schema()) {
 | 
				
			||||||
    fprintf(stderr, "Database schema is not correct.\n");
 | 
					    fprintf(stderr, "Database schema is not correct.\n");
 | 
				
			||||||
    return -1;
 | 
					    return -1;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  int status = seteuid(ruid);
 | 
				
			||||||
 | 
					  if (status < 0) {
 | 
				
			||||||
 | 
					    fprintf(stderr, "Couldn't set euid to ruid during database setup.\n");
 | 
				
			||||||
 | 
					    exit(status);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return 0;
 | 
					  return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,6 +33,8 @@ int source_init(const char *root_path) {
 | 
				
			|||||||
  int root_fd = open(root_path, O_PATH);
 | 
					  int root_fd = open(root_path, O_PATH);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (root_fd == -1) {
 | 
					  if (root_fd == -1) {
 | 
				
			||||||
 | 
					    fprintf(stderr, "Could not initialize source file system at %s", root_path);
 | 
				
			||||||
 | 
					    perror("");
 | 
				
			||||||
    return -1;
 | 
					    return -1;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,9 +16,23 @@ PATH="$(realpath ./mock/):$PATH"
 | 
				
			|||||||
# mount the filesystem
 | 
					# mount the filesystem
 | 
				
			||||||
 | 
					
 | 
				
			||||||
echo "Run $(date -u +%Y-%m-%dT%H:%M:%S) "
 | 
					echo "Run $(date -u +%Y-%m-%dT%H:%M:%S) "
 | 
				
			||||||
 | 
					if [[ $1 == "--setuid" ]]; then
 | 
				
			||||||
 | 
					  echo "Setting the setuid bit..."
 | 
				
			||||||
 | 
					  echo "root privilieges are required to create a special user and set correct ownership of the executable."
 | 
				
			||||||
 | 
					  id -u icfs &>/dev/null || sudo useradd --system --user-group icfs
 | 
				
			||||||
 | 
					  sudo chown icfs: ../build/icfs && sudo chmod 4777 ../build/icfs
 | 
				
			||||||
 | 
					  chmod g+w . # needed for icfs to be able to create the database
 | 
				
			||||||
 | 
					  echo "Valgrind will not be used due to setuid compatibility issues."
 | 
				
			||||||
 | 
					  ../build/icfs -o default_permissions ./protected &
 | 
				
			||||||
 | 
					  sleep 1
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
					  echo "Database protection will not be tested due to the lack of setuid capabilites."
 | 
				
			||||||
 | 
					  echo "To test it, run this script with '--setuid'."
 | 
				
			||||||
  valgrind -s ../build/icfs -o default_permissions ./protected &
 | 
					  valgrind -s ../build/icfs -o default_permissions ./protected &
 | 
				
			||||||
 | 
					 | 
				
			||||||
  sleep 5
 | 
					  sleep 5
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#valgrind -s ../build/icfs -o default_permissions ./protected &
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# WARN: please don't use `>` or `>>` operators. They force **this script** to open the file, **not the program you are trying to run**. This is probably not what you mean when you want to test a specific program's access.
 | 
					# WARN: please don't use `>` or `>>` operators. They force **this script** to open the file, **not the program you are trying to run**. This is probably not what you mean when you want to test a specific program's access.
 | 
				
			||||||
# WARN: avoid using touch, since it generates errors because setting times is not implemented in icfs **yet**.
 | 
					# WARN: avoid using touch, since it generates errors because setting times is not implemented in icfs **yet**.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user