1
0
mirror of https://github.com/samba-team/samba.git synced 2025-08-30 17:49:30 +03:00

lib: Fix lstat check in directory_create_or_exist

The lstat check in directory_create_or_exist did not verify whether an
existing object is actually a directory. Also move the check to only
apply when mkdir returns EEXIST; this fixes CID 241930 Time of check
time of use.

Signed-off-by: Christof Schmitt <cs@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
This commit is contained in:
Christof Schmitt
2018-08-29 12:04:29 -07:00
parent 58b8f2a31e
commit 9f60a77e0b

View File

@ -194,20 +194,8 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname,
mode_t dir_perms) mode_t dir_perms)
{ {
int ret; int ret;
struct stat st;
mode_t old_umask; mode_t old_umask;
ret = lstat(dname, &st);
if (ret == 0) {
return true;
}
if (errno != ENOENT) {
DBG_WARNING("lstat failed on directory %s: %s\n",
dname, strerror(errno));
return false;
}
/* Create directory */ /* Create directory */
old_umask = umask(0); old_umask = umask(0);
ret = mkdir(dname, dir_perms); ret = mkdir(dname, dir_perms);
@ -220,11 +208,17 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname,
} }
umask(old_umask); umask(old_umask);
ret = lstat(dname, &st); if (ret != 0 && errno == EEXIST) {
if (ret == -1) { struct stat sbuf;
DEBUG(0, ("lstat failed on created directory %s: %s\n",
dname, strerror(errno))); ret = lstat(dname, &sbuf);
return false; if (ret != 0) {
return false;
}
if (!S_ISDIR(sbuf.st_mode)) {
return false;
}
} }
return true; return true;