From 9c758355299e2a4d3f56c8a854f317cf9fc043ab Mon Sep 17 00:00:00 2001 From: Ralph Ronnquist Date: Thu, 28 Aug 2025 16:44:31 +1000 Subject: [PATCH] Map pathnames containing ".." component to prefix path. --- libpathmap-0.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/libpathmap-0.c b/libpathmap-0.c index f7dea49..e1d9a94 100644 --- a/libpathmap-0.c +++ b/libpathmap-0.c @@ -134,25 +134,38 @@ static void load_prefixes() { prefix.root_index = binsearch( prefix.root ); } +static char *add_prefix(const char *path) { + char *p = (char*)malloc( strlen( path ) + prefix.root_length + 1 ); + memcpy( p, prefix.root, prefix.root_length ); + strcpy( p + prefix.root_length, path ); + //fprintf( stderr, "libpathmap: [%s] %s\n", prefix.root, path ); + return p; +} + // Utility function to lookup a matching prefix for the given path. If // none is found, then 0 is returned. Otherwise memory is allocated // (malloc) for a new string that consists of the root path followed // by the given path. Note that prefixing only applies to absolute // paths. static char *maybe_add_prefix(const char *path) { - if ( prefix.count > 0 && *path == '/' ) { - //fprintf( stderr, "libpathmap: check %s\n", path ); + if ( prefix.count == 0 ) { + return 0; + } + //fprintf( stderr, "libpathmap: check %s\n", path ); + int n = strlen( path ); + // Return only prefix for any pathname that includes a ".." + // component. + if ( ( strcmp( path, ".." ) == 0 ) || + ( strstr( path, "/../" ) != 0 ) || + ( n > 2 && ( ( strncmp( path, "../", 3 ) == 0 ) || + ( strcmp( path + n -3, "/.." ) == 0 ) ) ) ) { + return add_prefix( "" ); + } + // Add prefix to absolut paths, except where it should not + if ( *path == '/' ) { int x = prefixsearch( path ); //fprintf( stderr, "libpathmap: %d %s\n", x, path ); - if ( x < 0 ) { - char *p = (char*)malloc( strlen( path ) + prefix.root_length + 1 ); - memcpy( p, prefix.root, prefix.root_length ); - strcpy( p + prefix.root_length, path ); - //fprintf( stderr, "libpathmap: [%s] %s\n", prefix.root, path ); - return p; - } else { - //fprintf( stderr, "libpathmap: [] %s\n", path ); - } + return ( x < 0 )? add_prefix( path ) : 0; } return 0; } -- 2.47.2