Blame 0108-dbus-avoid-race-conditions-in-tests-for-dum-dir-avai.patch

69165ba
From 7814554e0827ece778ca88fd90832bd4d05520b1 Mon Sep 17 00:00:00 2001
69165ba
From: Jakub Filak <jfilak@redhat.com>
69165ba
Date: Fri, 24 Apr 2015 13:48:32 +0200
69165ba
Subject: [ABRT PATCH] dbus: avoid race-conditions in tests for dum dir
69165ba
 availability
69165ba
69165ba
Florian Weimer <fweimer@redhat.com>
69165ba
69165ba
    dump_dir_accessible_by_uid() is fundamentally insecure because it
69165ba
    opens up a classic time-of-check-time-of-use race between this
69165ba
    function and and dd_opendir().
69165ba
69165ba
Related: #1214745
69165ba
69165ba
Signed-off-by: Jakub Filak <jfilak@redhat.com>
69165ba
---
69165ba
 src/dbus/abrt-dbus.c  | 66 ++++++++++++++++++++++++++++++++++++++++++++-------
69165ba
 src/lib/problem_api.c | 15 ++++++++++--
69165ba
 2 files changed, 71 insertions(+), 10 deletions(-)
69165ba
69165ba
diff --git a/src/dbus/abrt-dbus.c b/src/dbus/abrt-dbus.c
69165ba
index 7400dff..9e1844a 100644
69165ba
--- a/src/dbus/abrt-dbus.c
69165ba
+++ b/src/dbus/abrt-dbus.c
69165ba
@@ -245,7 +245,15 @@ static struct dump_dir *open_directory_for_modification_of_element(
69165ba
         }
69165ba
     }
69165ba
 
69165ba
-    if (!dump_dir_accessible_by_uid(problem_id, caller_uid))
69165ba
+    int dir_fd = dd_openfd(problem_id);
69165ba
+    if (dir_fd < 0)
69165ba
+    {
69165ba
+        perror_msg("can't open problem directory '%s'", problem_id);
69165ba
+        return_InvalidProblemDir_error(invocation, problem_id);
69165ba
+        return NULL;
69165ba
+    }
69165ba
+
69165ba
+    if (!fdump_dir_accessible_by_uid(dir_fd, caller_uid))
69165ba
     {
69165ba
         if (errno == ENOTDIR)
69165ba
         {
69165ba
@@ -260,10 +268,11 @@ static struct dump_dir *open_directory_for_modification_of_element(
69165ba
                                 _("Not Authorized"));
69165ba
         }
69165ba
 
69165ba
+        close(dir_fd);
69165ba
         return NULL;
69165ba
     }
69165ba
 
69165ba
-    struct dump_dir *dd = dd_opendir(problem_id, /* flags : */ 0);
69165ba
+    struct dump_dir *dd = dd_fdopendir(dir_fd, problem_id, /* flags : */ 0);
69165ba
     if (!dd)
69165ba
     {   /* This should not happen because of the access check above */
69165ba
         log_notice("Can't access the problem '%s' for modification", problem_id);
69165ba
@@ -429,7 +438,15 @@ static void handle_method_call(GDBusConnection *connection,
69165ba
             return;
69165ba
         }
69165ba
 
69165ba
-        int ddstat = dump_dir_stat_for_uid(problem_dir, caller_uid);
69165ba
+        int dir_fd = dd_openfd(problem_dir);
69165ba
+        if (dir_fd < 0)
69165ba
+        {
69165ba
+            perror_msg("can't open problem directory '%s'", problem_dir);
69165ba
+            return_InvalidProblemDir_error(invocation, problem_dir);
69165ba
+            return;
69165ba
+        }
69165ba
+
69165ba
+        int ddstat = fdump_dir_stat_for_uid(dir_fd, caller_uid);
69165ba
         if (ddstat < 0)
69165ba
         {
69165ba
             if (errno == ENOTDIR)
69165ba
@@ -443,6 +460,7 @@ static void handle_method_call(GDBusConnection *connection,
69165ba
 
69165ba
             return_InvalidProblemDir_error(invocation, problem_dir);
69165ba
 
69165ba
+            close(dir_fd);
69165ba
             return;
69165ba
         }
69165ba
 
69165ba
@@ -450,6 +468,7 @@ static void handle_method_call(GDBusConnection *connection,
69165ba
         {   //caller seems to be in group with access to this dir, so no action needed
69165ba
             log_notice("caller has access to the requested directory %s", problem_dir);
69165ba
             g_dbus_method_invocation_return_value(invocation, NULL);
69165ba
+            close(dir_fd);
69165ba
             return;
69165ba
         }
69165ba
 
69165ba
@@ -460,10 +479,11 @@ static void handle_method_call(GDBusConnection *connection,
69165ba
             g_dbus_method_invocation_return_dbus_error(invocation,
69165ba
                                               "org.freedesktop.problems.AuthFailure",
69165ba
                                               _("Not Authorized"));
69165ba
+            close(dir_fd);
69165ba
             return;
69165ba
         }
69165ba
 
69165ba
-        struct dump_dir *dd = dd_opendir(problem_dir, DD_OPEN_READONLY | DD_FAIL_QUIETLY_EACCES);
69165ba
+        struct dump_dir *dd = dd_fdopendir(dir_fd, problem_dir, DD_OPEN_READONLY | DD_FAIL_QUIETLY_EACCES);
69165ba
         if (!dd)
69165ba
         {
69165ba
             return_InvalidProblemDir_error(invocation, problem_dir);
69165ba
@@ -497,12 +517,21 @@ static void handle_method_call(GDBusConnection *connection,
69165ba
             return;
69165ba
         }
69165ba
 
69165ba
-        if (!dump_dir_accessible_by_uid(problem_dir, caller_uid))
69165ba
+        int dir_fd = dd_openfd(problem_dir);
69165ba
+        if (dir_fd < 0)
69165ba
+        {
69165ba
+            perror_msg("can't open problem directory '%s'", problem_dir);
69165ba
+            return_InvalidProblemDir_error(invocation, problem_dir);
69165ba
+            return;
69165ba
+        }
69165ba
+
69165ba
+        if (!fdump_dir_accessible_by_uid(dir_fd, caller_uid))
69165ba
         {
69165ba
             if (errno == ENOTDIR)
69165ba
             {
69165ba
                 log_notice("Requested directory does not exist '%s'", problem_dir);
69165ba
                 return_InvalidProblemDir_error(invocation, problem_dir);
69165ba
+                close(dir_fd);
69165ba
                 return;
69165ba
             }
69165ba
 
69165ba
@@ -512,11 +541,12 @@ static void handle_method_call(GDBusConnection *connection,
69165ba
                 g_dbus_method_invocation_return_dbus_error(invocation,
69165ba
                                                   "org.freedesktop.problems.AuthFailure",
69165ba
                                                   _("Not Authorized"));
69165ba
+                close(dir_fd);
69165ba
                 return;
69165ba
             }
69165ba
         }
69165ba
 
69165ba
-        struct dump_dir *dd = dd_opendir(problem_dir, DD_OPEN_READONLY | DD_FAIL_QUIETLY_EACCES);
69165ba
+        struct dump_dir *dd = dd_fdopendir(dir_fd, problem_dir, DD_OPEN_READONLY | DD_FAIL_QUIETLY_EACCES);
69165ba
         if (!dd)
69165ba
         {
69165ba
             return_InvalidProblemDir_error(invocation, problem_dir);
69165ba
@@ -677,20 +707,40 @@ static void handle_method_call(GDBusConnection *connection,
69165ba
         for (GList *l = problem_dirs; l; l = l->next)
69165ba
         {
69165ba
             const char *dir_name = (const char*)l->data;
69165ba
-            if (!dump_dir_accessible_by_uid(dir_name, caller_uid))
69165ba
+
69165ba
+            int dir_fd = dd_openfd(dir_name);
69165ba
+            if (dir_fd < 0)
69165ba
+            {
69165ba
+                perror_msg("can't open problem directory '%s'", dir_name);
69165ba
+                return_InvalidProblemDir_error(invocation, dir_name);
69165ba
+                return;
69165ba
+            }
69165ba
+
69165ba
+            if (!fdump_dir_accessible_by_uid(dir_fd, caller_uid))
69165ba
             {
69165ba
                 if (errno == ENOTDIR)
69165ba
                 {
69165ba
                     log_notice("Requested directory does not exist '%s'", dir_name);
69165ba
+                    close(dir_fd);
69165ba
                     continue;
69165ba
                 }
69165ba
 
69165ba
                 if (polkit_check_authorization_dname(caller, "org.freedesktop.problems.getall") != PolkitYes)
69165ba
                 { // if user didn't provide correct credentials, just move to the next dir
69165ba
+                    close(dir_fd);
69165ba
                     continue;
69165ba
                 }
69165ba
             }
69165ba
-            delete_dump_dir(dir_name);
69165ba
+
69165ba
+            struct dump_dir *dd = dd_fdopendir(dir_fd, dir_name, /*flags:*/ 0);
69165ba
+            if (dd)
69165ba
+            {
69165ba
+                if (dd_delete(dd) != 0)
69165ba
+                {
69165ba
+                    error_msg("Failed to delete problem directory '%s'", dir_name);
69165ba
+                    dd_close(dd);
69165ba
+                }
69165ba
+            }
69165ba
         }
69165ba
 
69165ba
         g_dbus_method_invocation_return_value(invocation, NULL);
69165ba
diff --git a/src/lib/problem_api.c b/src/lib/problem_api.c
69165ba
index c2b4b1c..b343882 100644
69165ba
--- a/src/lib/problem_api.c
69165ba
+++ b/src/lib/problem_api.c
69165ba
@@ -46,7 +46,15 @@ int for_each_problem_in_dir(const char *path,
69165ba
             continue; /* skip "." and ".." */
69165ba
 
69165ba
         char *full_name = concat_path_file(path, dent->d_name);
69165ba
-        if (caller_uid == -1 || dump_dir_accessible_by_uid(full_name, caller_uid))
69165ba
+
69165ba
+        int dir_fd = dd_openfd(full_name);
69165ba
+        if (dir_fd < 0)
69165ba
+        {
69165ba
+            VERB2 perror_msg("can't open problem directory '%s'", full_name);
69165ba
+            continue;
69165ba
+        }
69165ba
+
69165ba
+        if (caller_uid == -1 || fdump_dir_accessible_by_uid(dir_fd, caller_uid))
69165ba
         {
69165ba
             /* Silently ignore *any* errors, not only EACCES.
69165ba
              * We saw "lock file is locked by process PID" error
69165ba
@@ -54,7 +62,7 @@ int for_each_problem_in_dir(const char *path,
69165ba
              */
69165ba
             int sv_logmode = logmode;
69165ba
             logmode = 0;
69165ba
-            struct dump_dir *dd = dd_opendir(full_name, DD_OPEN_READONLY | DD_FAIL_QUIETLY_EACCES | DD_DONT_WAIT_FOR_LOCK);
69165ba
+            struct dump_dir *dd = dd_fdopendir(dir_fd, full_name, DD_OPEN_READONLY | DD_FAIL_QUIETLY_EACCES | DD_DONT_WAIT_FOR_LOCK);
69165ba
             logmode = sv_logmode;
69165ba
             if (dd)
69165ba
             {
69165ba
@@ -62,6 +70,9 @@ int for_each_problem_in_dir(const char *path,
69165ba
                 dd_close(dd);
69165ba
             }
69165ba
         }
69165ba
+        else
69165ba
+            close(dir_fd);
69165ba
+
69165ba
         free(full_name);
69165ba
         if (brk)
69165ba
             break;
69165ba
-- 
69165ba
1.8.3.1
69165ba