From 289d648004c78b19cd953b36db69df6958dfb0aa Mon Sep 17 00:00:00 2001 From: smitsohu Date: Mon, 3 May 2021 00:52:08 +0200 Subject: enhance clean_pathname function --- src/firejail/util.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/firejail/util.c b/src/firejail/util.c index 2ad85acd6..8966521cb 100644 --- a/src/firejail/util.c +++ b/src/firejail/util.c @@ -544,11 +544,13 @@ char *split_comma(char *str) { } -// remove consecutive and trailing slashes -// and return allocated memory -// e.g. /home//user/ -> /home/user +// simplify absolute path by removing +// 1) consecutive and trailing slashes, and +// 2) segments with a single dot +// for example /foo//./bar/ -> /foo/bar char *clean_pathname(const char *path) { - assert(path); + assert(path && path[0] == '/'); + size_t len = strlen(path); char *rv = malloc(len + 1); if (!rv) @@ -557,15 +559,23 @@ char *clean_pathname(const char *path) { size_t i = 0; size_t j = 0; while (path[i]) { - while (path[i] == '/' && path[i+1] == '/') - i++; + if (path[i] == '/') { + while (path[i+1] == '/' || + (path[i+1] == '.' && path[i+2] == '/')) + i++; + } + rv[j++] = path[i++]; } rv[j] = '\0'; + // remove a trailing dot + if (j > 1 && rv[j - 1] == '.' && rv[j - 2] == '/') + rv[--j] = '\0'; + // remove a trailing slash if (j > 1 && rv[j - 1] == '/') - rv[j - 1] = '\0'; + rv[--j] = '\0'; return rv; } -- cgit v1.2.3-54-g00ecf