mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-18 20:10:29 +01:00
WUtil: Be more strict about base directory for wmkdirhier()
The original code refused to create anything not in $WMAKER_USER_ROOT, now we go one step further and limit creation inside its 'Library' or 'Defaults' sub-directories.
This commit is contained in:
committed by
Carlos R. Mafra
parent
671db45007
commit
a0b283a60f
@@ -1732,23 +1732,36 @@ Bool WMWritePropListToFile(WMPropList * plist, const char *path)
|
|||||||
* file, and the last component is stripped off. the rest is the
|
* file, and the last component is stripped off. the rest is the
|
||||||
* the hierarchy to be created.
|
* the hierarchy to be created.
|
||||||
*
|
*
|
||||||
* refuses to create anything outside $WMAKER_USER_ROOT
|
* refuses to create anything outside $WMAKER_USER_ROOT/Defaults or $WMAKER_USER_ROOT/Library
|
||||||
*
|
*
|
||||||
* returns 1 on success, 0 on failure
|
* returns 1 on success, 0 on failure
|
||||||
*/
|
*/
|
||||||
int wmkdirhier(const char *path)
|
int wmkdirhier(const char *path)
|
||||||
{
|
{
|
||||||
const char *t;
|
const char *libpath;
|
||||||
|
char *udefpath;
|
||||||
|
int cmp;
|
||||||
char *thePath = NULL, buf[1024];
|
char *thePath = NULL, buf[1024];
|
||||||
size_t p, plen;
|
size_t p, plen;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
/* Only create directories under $WMAKER_USER_ROOT */
|
/* Only create directories under $WMAKER_USER_ROOT/Defaults or $WMAKER_USER_ROOT/Library */
|
||||||
if ((t = wusergnusteppath()) == NULL)
|
libpath = wuserdatapath();
|
||||||
return 0;
|
if (strncmp(path, libpath, strlen(libpath)) == 0)
|
||||||
if (strncmp(path, t, strlen(t)) != 0)
|
if (path[strlen(libpath)] == '/')
|
||||||
return 0;
|
goto path_in_valid_tree;
|
||||||
|
|
||||||
|
udefpath = wdefaultspathfordomain("");
|
||||||
|
cmp = strncmp(path, udefpath, strlen(udefpath));
|
||||||
|
wfree(udefpath);
|
||||||
|
if (cmp == 0)
|
||||||
|
/* Note: by side effect, 'udefpath' already contains a final '/' */
|
||||||
|
goto path_in_valid_tree;
|
||||||
|
|
||||||
|
/* If we reach this point, the path is outside the allowed tree */
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
path_in_valid_tree:
|
||||||
thePath = wstrdup(path);
|
thePath = wstrdup(path);
|
||||||
/* Strip the trailing component if it is a file */
|
/* Strip the trailing component if it is a file */
|
||||||
p = strlen(thePath);
|
p = strlen(thePath);
|
||||||
@@ -1771,7 +1784,6 @@ int wmkdirhier(const char *path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
||||||
strncpy(buf, t, sizeof(buf) - 1);
|
|
||||||
p = strlen(buf);
|
p = strlen(buf);
|
||||||
plen = strlen(thePath);
|
plen = strlen(thePath);
|
||||||
|
|
||||||
@@ -1782,7 +1794,7 @@ int wmkdirhier(const char *path)
|
|||||||
strncpy(buf, thePath, p);
|
strncpy(buf, thePath, p);
|
||||||
if (mkdir(buf, 0777) == -1 && errno == EEXIST &&
|
if (mkdir(buf, 0777) == -1 && errno == EEXIST &&
|
||||||
stat(buf, &st) == 0 && !S_ISDIR(st.st_mode)) {
|
stat(buf, &st) == 0 && !S_ISDIR(st.st_mode)) {
|
||||||
werror(_("Could not create component %s"), buf);
|
werror(_("Could not create path component %s"), buf);
|
||||||
wfree(thePath);
|
wfree(thePath);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user