From 922c4a10abe52d9f2c1e1b1c932a43076822920f Mon Sep 17 00:00:00 2001 From: smitsohu Date: Fri, 26 Oct 2018 22:47:48 +0200 Subject: little tweaks, cosmetic changes --- src/firejail/firejail.h | 2 +- src/firejail/fs.c | 17 +++++++---------- src/firejail/mountinfo.c | 40 ++++++++++++++++++++-------------------- 3 files changed, 28 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/firejail/firejail.h b/src/firejail/firejail.h index d5733e678..2d96863c5 100644 --- a/src/firejail/firejail.h +++ b/src/firejail/firejail.h @@ -561,7 +561,7 @@ typedef struct { // mountinfo.c MountData *get_last_mount(void); int get_mount_id(const char *path); -char **get_all_mounts(const int mountid, const char *path); +char **build_mount_array(const int mountid, const char *path); // fs_var.c void fs_var_log(void); // mounting /var/log diff --git a/src/firejail/fs.c b/src/firejail/fs.c index 6fe9d56aa..eda46d127 100644 --- a/src/firejail/fs.c +++ b/src/firejail/fs.c @@ -485,12 +485,12 @@ void fs_rdonly_rec(const char *dir) { EUID_USER(); // get mount point of the directory int mountid = get_mount_id(dir); - if (mountid == 0) { + if (mountid == -1) { EUID_ROOT(); return; } // build array with all mount points that need to get remounted - char **arr = get_all_mounts(mountid, dir); + char **arr = build_mount_array(mountid, dir); assert(arr); // remount EUID_ROOT(); @@ -543,12 +543,12 @@ static void fs_rdwr_rec(const char *dir) { EUID_USER(); // get mount point of the directory int mountid = get_mount_id(dir); - if (mountid == 0) { + if (mountid == -1) { EUID_ROOT(); return; } // build array with all mount points that need to get remounted - char **arr = get_all_mounts(mountid, dir); + char **arr = build_mount_array(mountid, dir); assert(arr); // remount EUID_ROOT(); @@ -589,12 +589,12 @@ void fs_noexec_rec(const char *dir) { EUID_USER(); // get mount point of the directory int mountid = get_mount_id(dir); - if (mountid == 0) { + if (mountid == -1) { EUID_ROOT(); return; } // build array with all mount points that need to get remounted - char **arr = get_all_mounts(mountid, dir); + char **arr = build_mount_array(mountid, dir); assert(arr); // remount EUID_ROOT(); @@ -809,20 +809,17 @@ void fs_basic_fs(void) { uid_t uid = getuid(); if (arg_debug) - printf("Mounting read-only /bin, /sbin, /lib, /lib32, /lib64, /usr"); + printf("Basic read-only filesystem:\n"); if (!arg_writable_etc) { fs_rdonly("/etc"); if (uid) fs_noexec("/etc"); - if (arg_debug) printf(", /etc"); } if (!arg_writable_var) { fs_rdonly("/var"); if (uid) fs_noexec("/var"); - if (arg_debug) printf(", /var"); } - if (arg_debug) printf("\n"); fs_rdonly("/bin"); fs_rdonly("/sbin"); fs_rdonly("/lib"); diff --git a/src/firejail/mountinfo.c b/src/firejail/mountinfo.c index b7760ba67..b7e6c6fdd 100644 --- a/src/firejail/mountinfo.c +++ b/src/firejail/mountinfo.c @@ -67,7 +67,7 @@ static void unmangle_path(char *path) { // Parse a line from /proc/self/mountinfo, // the function does an exit(1) if anything goes wrong. static void parse_line(char *line, MountData *output) { - assert(line && *line); + assert(line && output); memset(output, 0, sizeof(*output)); // extract filesystem name, directory and filesystem types // examples: @@ -156,7 +156,7 @@ int get_mount_id(const char *path) { EUID_ASSERT(); int fd = open(path, O_PATH|O_CLOEXEC); if (fd == -1) - return 0; + return -1; char *fdinfo; if (asprintf(&fdinfo, "/proc/self/fdinfo/%d", fd) == -1) @@ -166,31 +166,31 @@ int get_mount_id(const char *path) { EUID_USER(); if (!fp) goto errexit; - // go to the last line + + // read the file char buf[MAX_BUF]; - while (fgets(buf, MAX_BUF, fp)); - fclose(fp); - close(fd); - // go to the mount id - if (strncmp(buf, "mnt_id:", 7) != 0) - goto errexit; - char *ptr = buf + 7; - while (*ptr != '\0' && (*ptr == ' ' || *ptr == '\t')) { - ptr++; + while (fgets(buf, MAX_BUF, fp)) { + if (strncmp(buf, "mnt_id:", 7) == 0) { + char *ptr = buf + 7; + while (*ptr != '\0' && (*ptr == ' ' || *ptr == '\t')) { + ptr++; + } + if (*ptr == '\0') + goto errexit; + fclose(fp); + close(fd); + free(fdinfo); + return atoi(ptr); + } } - if (*ptr == '\0') - goto errexit; - free(fdinfo); - - return atoi(ptr); errexit: - fprintf(stderr, "Error: cannot read file in /proc/self/fdinfo\n"); + fprintf(stderr, "Error: cannot read %s\n", fdinfo); exit(1); } // Return array with all paths that might need a remount. -char **get_all_mounts(const int mountid, const char *path) { +char **build_mount_array(const int mountid, const char *path) { // open /proc/self/mountinfo FILE *fp = fopen("/proc/self/mountinfo", "re"); if (!fp) { @@ -244,7 +244,7 @@ char **get_all_mounts(const int mountid, const char *path) { errExit("realloc"); } rv[cnt] = strdup(mdata.dir); - if (!rv[cnt]) + if (rv[cnt] == NULL) errExit("strdup"); cnt++; } -- cgit v1.2.3-54-g00ecf