From b4b972aeb56d04fb100661a29223f3df9b27fda0 Mon Sep 17 00:00:00 2001 From: Glenn Washburn Date: Mon, 1 Oct 2018 16:10:12 -0500 Subject: Fix command name parsing so that program paths with spaces do not cause the wrong or no profile to be detected. --- src/firejail/util.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/firejail/util.c b/src/firejail/util.c index 4a164901d..ae07a42b0 100644 --- a/src/firejail/util.c +++ b/src/firejail/util.c @@ -636,33 +636,33 @@ void extract_command_name(int index, char **argv) { if (!cfg.command_name) errExit("strdup"); - // restrict the command name to the first word - char *ptr = cfg.command_name; - while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0') - ptr++; - *ptr = '\0'; - // remove the path: /usr/bin/firefox becomes firefox - ptr = strrchr(cfg.command_name, '/'); + char *basename = cfg.command_name; + char *ptr = strrchr(cfg.command_name, '/'); if (ptr) { - ptr++; + basename = ++ptr; if (*ptr == '\0') { fprintf(stderr, "Error: invalid command name\n"); exit(1); } + } + else + ptr = basename; - char *tmp = strdup(ptr); - if (!tmp) - errExit("strdup"); + // restrict the command name to the first word + while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0') + ptr++; - // limit the command to the first ' ' - char *ptr2 = tmp; - while (*ptr2 != ' ' && *ptr2 != '\0') - ptr2++; - *ptr2 = '\0'; + // command name is a substring of cfg.command_name + if (basename != cfg.command_name || *ptr != '\0') { + *ptr = '\0'; + + basename = strdup(basename); + if (!basename) + errExit("strdup"); free(cfg.command_name); - cfg.command_name = tmp; + cfg.command_name = basename; } } -- cgit v1.2.3-54-g00ecf