Fix: Verify directory's existence before calling mkdir
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 2 Nov 2015 16:39:59 +0000 (11:39 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 2 Nov 2015 22:37:22 +0000 (17:37 -0500)
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/utils.c

index ee53477de559fedaf43eabfa194ee3255e58b293..6e772671451aec4e90e9943dd4c78eddd087c083 100644 (file)
@@ -30,6 +30,7 @@
 #include <grp.h>
 #include <pwd.h>
 #include <sys/file.h>
 #include <grp.h>
 #include <pwd.h>
 #include <sys/file.h>
+#include <unistd.h>
 
 #include <common/common.h>
 #include <common/runas.h>
 
 #include <common/common.h>
 #include <common/runas.h>
@@ -551,6 +552,44 @@ error:
        return fd;
 }
 
        return fd;
 }
 
+/*
+ * On some filesystems (e.g. nfs), mkdir will validate access rights before
+ * checking for the existence of the path element. This means that on a setup
+ * where "/home/" is a mounted NFS share, and running as an unpriviledged user,
+ * recursively creating a path of the form "/home/my_user/trace/" will fail with
+ * EACCES on mkdir("/home", ...).
+ *
+ * Performing a stat(...) on the path to check for existence allows us to
+ * work around this behaviour.
+ */
+static
+int mkdir_check_exists(const char *path, mode_t mode)
+{
+       int ret = 0;
+       struct stat st;
+
+       ret = stat(path, &st);
+       if (ret == 0) {
+               if (S_ISDIR(st.st_mode)) {
+                       /* Directory exists, skip. */
+                       goto end;
+               } else {
+                       /* Exists, but is not a directory. */
+                       errno = ENOTDIR;
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       /*
+        * Let mkdir handle other errors as the caller expects mkdir
+        * semantics.
+        */
+       ret = mkdir(path, mode);
+end:
+       return ret;
+}
+
 /*
  * Create directory using the given path and mode.
  *
 /*
  * Create directory using the given path and mode.
  *
@@ -562,7 +601,7 @@ int utils_mkdir(const char *path, mode_t mode, int uid, int gid)
        int ret;
 
        if (uid < 0 || gid < 0) {
        int ret;
 
        if (uid < 0 || gid < 0) {
-               ret = mkdir(path, mode);
+               ret = mkdir_check_exists(path, mode);
        } else {
                ret = run_as_mkdir(path, mode, uid, gid);
        }
        } else {
                ret = run_as_mkdir(path, mode, uid, gid);
        }
@@ -617,9 +656,9 @@ int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode)
                                ret = -1;
                                goto error;
                        }
                                ret = -1;
                                goto error;
                        }
-                       ret = mkdir(tmp, mode);
+                       ret = mkdir_check_exists(tmp, mode);
                        if (ret < 0) {
                        if (ret < 0) {
-                               if (errno != EEXIST) {
+                               if (errno != EACCES) {
                                        PERROR("mkdir recursive");
                                        ret = -errno;
                                        goto error;
                                        PERROR("mkdir recursive");
                                        ret = -errno;
                                        goto error;
@@ -629,14 +668,10 @@ int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode)
                }
        }
 
                }
        }
 
-       ret = mkdir(tmp, mode);
+       ret = mkdir_check_exists(tmp, mode);
        if (ret < 0) {
        if (ret < 0) {
-               if (errno != EEXIST) {
-                       PERROR("mkdir recursive last element");
-                       ret = -errno;
-               } else {
-                       ret = 0;
-               }
+               PERROR("mkdir recursive last element");
+               ret = -errno;
        }
 
 error:
        }
 
 error:
This page took 0.026594 seconds and 4 git commands to generate.