Blame SOURCES/0133-lib-add-alternative-dd-functions-accepting-fds.patch

28bab8
From 16b2ebdb678b7475bacb80dd59e949055d3f856c Mon Sep 17 00:00:00 2001
28bab8
From: Jakub Filak <jfilak@redhat.com>
28bab8
Date: Fri, 24 Apr 2015 11:42:18 +0200
28bab8
Subject: [LIBREPORT PATCH] lib: add alternative dd functions accepting fds
28bab8
28bab8
Florian Weimer <fweimer@redhat.com>:
28bab8
28bab8
    dump_dir_accessible_by_uid() is fundamentally insecure because it
28bab8
    opens up a classic time-of-check-time-of-use race between this
28bab8
    function and and dd_opendir().  At least re-checking after
28bab8
    dd_opendir() with the stored file descriptor is needed.
28bab8
28bab8
Related: #1214745
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
---
28bab8
 src/include/dump_dir.h |  8 +++++
28bab8
 src/lib/dump_dir.c     | 82 +++++++++++++++++++++++++++++++++++++++++---------
28bab8
 2 files changed, 75 insertions(+), 15 deletions(-)
28bab8
28bab8
diff --git a/src/include/dump_dir.h b/src/include/dump_dir.h
28bab8
index d675362..07b119a 100644
28bab8
--- a/src/include/dump_dir.h
28bab8
+++ b/src/include/dump_dir.h
28bab8
@@ -68,7 +68,13 @@ struct dump_dir {
28bab8
 
28bab8
 void dd_close(struct dump_dir *dd);
28bab8
 
28bab8
+/* Opens the given path and returns the resulting file descriptor.
28bab8
+ */
28bab8
+int dd_openfd(const char *dir);
28bab8
 struct dump_dir *dd_opendir(const char *dir, int flags);
28bab8
+/* Skips dd_openfd(dir) and uses the given file descriptor instead
28bab8
+ */
28bab8
+struct dump_dir *dd_fdopendir(int dir_fd, const char *dir, int flags);
28bab8
 struct dump_dir *dd_create_skeleton(const char *dir, uid_t uid, mode_t mode, int flags);
28bab8
 int dd_reset_ownership(struct dump_dir *dd);
28bab8
 /* Pass uid = (uid_t)-1L to disable chown'ing of newly created files
28bab8
@@ -147,6 +153,7 @@ void delete_dump_dir(const char *dirname);
28bab8
  * Returns non zero if dump dir is accessible otherwise return 0 value.
28bab8
  */
28bab8
 int dump_dir_accessible_by_uid(const char *dirname, uid_t uid);
28bab8
+int fdump_dir_accessible_by_uid(int dir_fd, uid_t uid);
28bab8
 
28bab8
 enum {
28bab8
     DD_STAT_ACCESSIBLE_BY_UID = 1,
28bab8
@@ -161,6 +168,7 @@ enum {
28bab8
  * Returns negative number if error occurred otherwise returns 0 or positive number.
28bab8
  */
28bab8
 int dump_dir_stat_for_uid(const char *dirname, uid_t uid);
28bab8
+int fdump_dir_stat_for_uid(int dir_fd, uid_t uid);
28bab8
 
28bab8
 /* creates not_reportable file in the problem directory and saves the
28bab8
    reason to it, which prevents libreport from reporting the problem
28bab8
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
28bab8
index 16cd987..a0e96e4 100644
28bab8
--- a/src/lib/dump_dir.c
28bab8
+++ b/src/lib/dump_dir.c
28bab8
@@ -417,12 +417,8 @@ static char* rm_trailing_slashes(const char *dir)
28bab8
     return xstrndup(dir, len);
28bab8
 }
28bab8
 
28bab8
-struct dump_dir *dd_opendir(const char *dir, int flags)
28bab8
+static struct dump_dir *dd_do_open(struct dump_dir *dd, int flags)
28bab8
 {
28bab8
-    struct dump_dir *dd = dd_init();
28bab8
-
28bab8
-    dir = dd->dd_dirname = rm_trailing_slashes(dir);
28bab8
-    dd->dd_fd = open(dir, O_DIRECTORY | O_NOFOLLOW);
28bab8
     struct stat stat_buf;
28bab8
     if (dd->dd_fd < 0)
28bab8
         goto cant_access;
28bab8
@@ -440,6 +436,7 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
28bab8
             /* Directory is not writable. If it seems to be readable,
28bab8
              * return "read only" dd, not NULL
28bab8
              *
28bab8
+             *
28bab8
              * Does the directory have 'x' flag?
28bab8
              */
28bab8
             if (faccessat(dd->dd_fd, ".", R_OK, AT_SYMLINK_NOFOLLOW) == 0)
28bab8
@@ -461,7 +458,7 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
28bab8
              * directory when run without arguments, because its option -d DIR
28bab8
              * defaults to "."!
28bab8
              */
28bab8
-            error_msg("'%s' is not a problem directory", dir);
28bab8
+            error_msg("'%s' is not a problem directory", dd->dd_dirname);
28bab8
         }
28bab8
         else
28bab8
         {
28bab8
@@ -469,12 +466,12 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
28bab8
             if (errno == ENOENT || errno == ENOTDIR)
28bab8
             {
28bab8
                 if (!(flags & DD_FAIL_QUIETLY_ENOENT))
28bab8
-                    error_msg("'%s' does not exist", dir);
28bab8
+                    error_msg("'%s' does not exist", dd->dd_dirname);
28bab8
             }
28bab8
             else
28bab8
             {
28bab8
                 if (!(flags & DD_FAIL_QUIETLY_EACCES))
28bab8
-                    perror_msg("Can't access '%s'", dir);
28bab8
+                    perror_msg("Can't access '%s'", dd->dd_dirname);
28bab8
             }
28bab8
         }
28bab8
         dd_close(dd);
28bab8
@@ -488,7 +485,7 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
28bab8
         /* In case caller would want to create more files, he'll need uid:gid */
28bab8
         if (fstat(dd->dd_fd, &stat_buf) != 0)
28bab8
         {
28bab8
-            error_msg("Can't stat '%s'", dir);
28bab8
+            error_msg("Can't stat '%s'", dd->dd_dirname);
28bab8
             dd_close(dd);
28bab8
             return NULL;
28bab8
         }
28bab8
@@ -499,6 +496,34 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
28bab8
     return dd;
28bab8
 }
28bab8
 
28bab8
+int dd_openfd(const char *dir)
28bab8
+{
28bab8
+    return open(dir, O_DIRECTORY | O_NOFOLLOW);
28bab8
+}
28bab8
+
28bab8
+struct dump_dir *dd_fdopendir(int dir_fd, const char *dir, int flags)
28bab8
+{
28bab8
+    struct dump_dir *dd = dd_init();
28bab8
+
28bab8
+    dd->dd_dirname = rm_trailing_slashes(dir);
28bab8
+    dd->dd_fd = dir_fd;
28bab8
+
28bab8
+    /* Do not interpret errno in dd_do_open() if dd_fd < 0 */
28bab8
+    errno = 0;
28bab8
+    return dd_do_open(dd, flags);
28bab8
+}
28bab8
+
28bab8
+struct dump_dir *dd_opendir(const char *dir, int flags)
28bab8
+{
28bab8
+    struct dump_dir *dd = dd_init();
28bab8
+
28bab8
+    dd->dd_dirname = rm_trailing_slashes(dir);
28bab8
+    /* dd_do_open validates dd_fd */
28bab8
+    dd->dd_fd = dd_openfd(dir);
28bab8
+
28bab8
+    return dd_do_open(dd, flags);
28bab8
+}
28bab8
+
28bab8
 /* Create a fresh empty debug dump dir which is owned bu the calling user. If
28bab8
  * you want to create the directory with meaningful ownership you should
28bab8
  * consider using dd_create() function or you can modify the ownership
28bab8
@@ -936,7 +961,7 @@ int dd_chown(struct dump_dir *dd, uid_t new_uid)
28bab8
         error_msg_and_die("dump_dir is not opened"); /* bug */
28bab8
 
28bab8
     struct stat statbuf;
28bab8
-    if (!(stat(dd->dd_dirname, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)))
28bab8
+    if (!fstat(dd->dd_fd, &statbuf) == 0)
28bab8
     {
28bab8
         perror_msg("stat('%s')", dd->dd_dirname);
28bab8
         return 1;
28bab8
@@ -1352,12 +1377,12 @@ static bool uid_in_group(uid_t uid, gid_t gid)
28bab8
 }
28bab8
 #endif
28bab8
 
28bab8
-int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
28bab8
+int fdump_dir_stat_for_uid(int dir_fd, uid_t uid)
28bab8
 {
28bab8
     struct stat statbuf;
28bab8
-    if (stat(dirname, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
28bab8
+    if (fstat(dir_fd, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
28bab8
     {
28bab8
-        log_debug("can't get stat of '%s': not a problem directory", dirname);
28bab8
+        log_debug("can't get stat: not a problem directory");
28bab8
         errno = ENOTDIR;
28bab8
         return -1;
28bab8
     }
28bab8
@@ -1367,7 +1392,7 @@ int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
28bab8
     int ddstat = 0;
28bab8
     if (uid == 0 || (statbuf.st_mode & S_IROTH))
28bab8
     {
28bab8
-        log_debug("directory '%s' is accessible by %ld uid", dirname, (long)uid);
28bab8
+        log_debug("directory is accessible by %ld uid", (long)uid);
28bab8
         ddstat |= DD_STAT_ACCESSIBLE_BY_UID;
28bab8
     }
28bab8
 
28bab8
@@ -1377,7 +1402,7 @@ int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
28bab8
     if (uid_in_group(uid, statbuf.st_gid))
28bab8
 #endif
28bab8
     {
28bab8
-        log_debug("%ld uid owns directory '%s'", (long)uid, dirname);
28bab8
+        log_debug("%ld uid owns directory", (long)uid);
28bab8
         ddstat |= DD_STAT_ACCESSIBLE_BY_UID;
28bab8
         ddstat |= DD_STAT_OWNED_BY_UID;
28bab8
     }
28bab8
@@ -1385,6 +1410,33 @@ int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
28bab8
     return ddstat;
28bab8
 }
28bab8
 
28bab8
+int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
28bab8
+{
28bab8
+    int dir_fd = open(dirname, O_DIRECTORY | O_NOFOLLOW);
28bab8
+    if (dir_fd < 0)
28bab8
+    {
28bab8
+        log_debug("can't open '%s': not a problem directory", dirname);
28bab8
+        errno = ENOTDIR;
28bab8
+        return -1;
28bab8
+    }
28bab8
+
28bab8
+    int r = fdump_dir_stat_for_uid(dir_fd, uid);
28bab8
+    close(dir_fd);
28bab8
+    return r;
28bab8
+}
28bab8
+
28bab8
+int fdump_dir_accessible_by_uid(int dir_fd, uid_t uid)
28bab8
+{
28bab8
+    int ddstat = fdump_dir_stat_for_uid(dir_fd, uid);
28bab8
+
28bab8
+    if (ddstat >= 0)
28bab8
+        return ddstat & DD_STAT_ACCESSIBLE_BY_UID;
28bab8
+
28bab8
+    VERB3 pwarn_msg("can't determine accessibility for %ld uid", (long)uid);
28bab8
+
28bab8
+    return 0;
28bab8
+}
28bab8
+
28bab8
 int dump_dir_accessible_by_uid(const char *dirname, uid_t uid)
28bab8
 {
28bab8
     int ddstat = dump_dir_stat_for_uid(dirname, uid);
28bab8
-- 
28bab8
1.8.3.1
28bab8