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

plugins/sudoers/iolog.c

index 5f00a98f1f49bc03e8a3f5f5f2484400346b4373..f08b7a5b07a30ddd2943d90b403dab651281a625 100644 (file)
@@ -98,20 +98,29 @@ io_mkdirs(char *path, mode_t mode, bool is_temp)
 
     while ((slash = strchr(slash + 1, '/')) != NULL) {
        *slash = '\0';
-       if (stat(path, &sb) != 0) {
-           if (mkdir(path, mode) != 0) {
+       sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
+           "mkdir %s, mode 0%o", path, (unsigned int) mode);
+       if (mkdir(path, mode) == 0) {
+           ignore_result(chown(path, (uid_t)-1, parent_gid));
+       } else {
+           if (errno != EEXIST) {
                log_warning(SLOG_SEND_MAIL, N_("unable to mkdir %s"), path);
                ok = false;
                break;
            }
-           ignore_result(chown(path, (uid_t)-1, parent_gid));
-       } else if (!S_ISDIR(sb.st_mode)) {
-           log_warningx(SLOG_SEND_MAIL,
-               N_("%s exists but is not a directory (0%o)"),
-               path, (unsigned int) sb.st_mode);
-           ok = false;
-           break;
-       } else {
+           /* Already exists, make sure it is a directory. */
+           if (stat(path, &sb) != 0) {
+               log_warning(SLOG_SEND_MAIL, N_("unable to mkdir %s"), path);
+               ok = false;
+               break;
+           }
+           if (!S_ISDIR(sb.st_mode)) {
+               log_warningx(SLOG_SEND_MAIL,
+                   N_("%s exists but is not a directory (0%o)"),
+                   path, (unsigned int) sb.st_mode);
+               ok = false;
+               break;
+           }
            /* Inherit gid of parent dir for ownership. */
            parent_gid = sb.st_gid;
        }
@@ -120,6 +129,8 @@ io_mkdirs(char *path, mode_t mode, bool is_temp)
     if (ok) {
        /* Create final path component. */
        if (is_temp) {
+           sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
+               "mkdtemp %s", path);
            if (mkdtemp(path) == NULL) {
                log_warning(SLOG_SEND_MAIL, N_("unable to mkdir %s"), path);
                ok = false;
@@ -127,6 +138,8 @@ io_mkdirs(char *path, mode_t mode, bool is_temp)
                ignore_result(chown(path, (uid_t)-1, parent_gid));
            }
        } else {
+           sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
+               "mkdir %s, mode 0%o", path, (unsigned int) mode);
            if (mkdir(path, mode) != 0 && errno != EEXIST) {
                log_warning(SLOG_SEND_MAIL, N_("unable to mkdir %s"), path);
                ok = false;