From 602ed0c74794a3c05bbcbd90cd4fc49194842e54 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Fri, 6 May 2016 16:37:20 -0600 Subject: [PATCH] In ts_mkdirs(), change the order from stat then mkdir, to mkdir then stat. This more closely matches what "mkdir -p" does. Coverity CID 104119. --- plugins/sudoers/timestamp.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/plugins/sudoers/timestamp.c b/plugins/sudoers/timestamp.c index f7a9ba090..05a67aae5 100644 --- a/plugins/sudoers/timestamp.c +++ b/plugins/sudoers/timestamp.c @@ -160,22 +160,29 @@ ts_mkdirs(char *path, uid_t owner, mode_t mode, mode_t parent_mode, bool quiet) while ((slash = strchr(slash + 1, '/')) != NULL) { *slash = '\0'; - if (stat(path, &sb) != 0) { - sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO, - "mkdir %s, mode 0%o", path, (unsigned int) parent_mode); - if (mkdir(path, parent_mode) != 0) { + sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO, + "mkdir %s, mode 0%o", path, (unsigned int) parent_mode); + if (mkdir(path, parent_mode) == 0) { + ignore_result(chown(path, (uid_t)-1, parent_gid)); + } else { + if (errno != EEXIST) { if (!quiet) sudo_warn(U_("unable to mkdir %s"), path); goto done; } - ignore_result(chown(path, (uid_t)-1, parent_gid)); - } else if (!S_ISDIR(sb.st_mode)) { - if (!quiet) { - sudo_warnx(U_("%s exists but is not a directory (0%o)"), - path, (unsigned int) sb.st_mode); + /* Already exists, make sure it is a directory. */ + if (stat(path, &sb) != 0) { + if (!quiet) + sudo_warn(U_("unable to stat %s"), path); + goto done; + } + if (!S_ISDIR(sb.st_mode)) { + if (!quiet) { + sudo_warnx(U_("%s exists but is not a directory (0%o)"), + path, (unsigned int) sb.st_mode); + } + goto done; } - goto done; - } else { /* Inherit gid of parent dir for ownership. */ parent_gid = sb.st_gid; } -- 2.40.0