From: Oliver Kurth Date: Tue, 21 Apr 2020 21:43:46 +0000 (-0700) Subject: Changes to common source files not directly applicable to open-vm-tools. X-Git-Tag: stable-11.2.0~243 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1852a9e98e3b1c31542986b2d332c707e4a0b540;p=thirdparty%2Fopen-vm-tools.git Changes to common source files not directly applicable to open-vm-tools. --- diff --git a/open-vm-tools/lib/file/file.c b/open-vm-tools/lib/file/file.c index 0b99e159e..30d491959 100644 --- a/open-vm-tools/lib/file/file.c +++ b/open-vm-tools/lib/file/file.c @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 1998-2019 VMware, Inc. All rights reserved. + * Copyright (C) 1998-2020 VMware, Inc. All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published @@ -2684,3 +2684,48 @@ File_ContainSymLink(const char *pathName) // IN: return retValue; } + + +/* + *---------------------------------------------------------------------------- + * + * File_IsSubPathOf -- + * + * Check if the argument path is a sub path for argument base. + * The argument path will be converted to canonical path which doesn't + * contain ".." and then check if this canonical path is a sub path for + * argument base. + * So, this function can correctly recognize that a path like + * "/tmp/dir1/dir2/../../../bin/" is not a sub path for "/tmp/". + * + * Results: + * True if the argument path is a sub path for argument base. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------------- + */ + +Bool +File_IsSubPathOf(const char *base, // IN: the base path to test against. + const char *path) // IN: the possible subpath to test. +{ + char *fullBase = File_FullPath(base); + char *fullPath = File_FullPath(path); + Bool isSubPath = TRUE; + + ASSERT(fullBase); + ASSERT(fullPath); + if ( fullPath == NULL + || fullBase == NULL + || strncmp(fullPath, fullBase, strlen(fullBase)) != 0) { + isSubPath = FALSE; + } + + free(fullBase); + free(fullPath); + + return isSubPath; +} + diff --git a/open-vm-tools/lib/include/file.h b/open-vm-tools/lib/include/file.h index 181308ea0..d2bb8d824 100644 --- a/open-vm-tools/lib/include/file.h +++ b/open-vm-tools/lib/include/file.h @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 1998-2019 VMware, Inc. All rights reserved. + * Copyright (C) 1998-2020 VMware, Inc. All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published @@ -372,6 +372,8 @@ int File_MakeSafeTemp(const char *tag, Bool File_DoesVolumeSupportAcls(const char *pathName); +Bool File_IsSubPathOf(const char *base, const char *path); + /* *--------------------------------------------------------------------------- *