]> granicus.if.org Git - sudo/commitdiff
In ts_mkdirs(), change the order from stat then mkdir, to mkdir then stat.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 6 May 2016 22:37:20 +0000 (16:37 -0600)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 6 May 2016 22:37:20 +0000 (16:37 -0600)
This more closely matches what "mkdir -p" does.
Coverity CID 104119.

plugins/sudoers/timestamp.c

index f7a9ba09079574f4d2ca704b6ce6601f1894e4e6..05a67aae5841b0fe006a0f83ecf85a40ef14eba7 100644 (file)
@@ -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;
        }