doczkal / rpms / abrt

Forked from rpms/abrt 4 years ago
Clone

Blame SOURCES/0106-lib-add-functions-validating-dump-dir.patch

baab13
From b7f8bd20b7fb5b72f003ae3fa647c1d75f4218b7 Mon Sep 17 00:00:00 2001
baab13
From: Jakub Filak <jfilak@redhat.com>
baab13
Date: Thu, 23 Apr 2015 14:40:18 +0200
baab13
Subject: [ABRT PATCH] lib: add functions validating dump dir
baab13
baab13
Move the code from abrt-server to shared library and fix the condition
baab13
validating dump dir's path.
baab13
baab13
As of now, abrt is allowed to process only direct sub-directories of the
baab13
dump locations.
baab13
baab13
Signed-off-by: Jakub Filak <jfilak@redhat.com>
baab13
---
baab13
 src/daemon/abrt-server.c | 42 ++++++------------------
baab13
 src/include/libabrt.h    |  4 +++
baab13
 src/lib/hooklib.c        | 56 +++++++++++++++++++++++++++++++
baab13
 tests/Makefile.am        |  3 +-
baab13
 tests/hooklib.at         | 85 ++++++++++++++++++++++++++++++++++++++++++++++++
baab13
 tests/testsuite.at       |  1 +
baab13
 6 files changed, 158 insertions(+), 33 deletions(-)
baab13
 create mode 100644 tests/hooklib.at
baab13
baab13
diff --git a/src/daemon/abrt-server.c b/src/daemon/abrt-server.c
baab13
index 4d486d4..1030461 100644
baab13
--- a/src/daemon/abrt-server.c
baab13
+++ b/src/daemon/abrt-server.c
baab13
@@ -76,20 +76,6 @@ static unsigned total_bytes_read = 0;
baab13
 static uid_t client_uid = (uid_t)-1L;
baab13
 
baab13
 
baab13
-static bool dir_is_in_dump_location(const char *dump_dir_name)
baab13
-{
baab13
-    unsigned len = strlen(g_settings_dump_location);
baab13
-
baab13
-    if (strncmp(dump_dir_name, g_settings_dump_location, len) == 0
baab13
-     && dump_dir_name[len] == '/'
baab13
-    /* must not contain "/." anywhere (IOW: disallow ".." component) */
baab13
-     && !strstr(dump_dir_name + len, "/.")
baab13
-    ) {
baab13
-        return 1;
baab13
-    }
baab13
-    return 0;
baab13
-}
baab13
-
baab13
 /* Remove dump dir */
baab13
 static int delete_path(const char *dump_dir_name)
baab13
 {
baab13
@@ -100,6 +86,11 @@ static int delete_path(const char *dump_dir_name)
baab13
         error_msg("Bad problem directory name '%s', should start with: '%s'", dump_dir_name, g_settings_dump_location);
baab13
         return 400; /* Bad Request */
baab13
     }
baab13
+    if (!dir_has_correct_permissions(dump_dir_name))
baab13
+    {
baab13
+        error_msg("Problem directory '%s' isn't owned by root:abrt or others are not restricted from access", dump_dir_name);
baab13
+        return 400; /*  */
baab13
+    }
baab13
     if (!dump_dir_accessible_by_uid(dump_dir_name, client_uid))
baab13
     {
baab13
         if (errno == ENOTDIR)
baab13
@@ -154,26 +145,13 @@ static int run_post_create(const char *dirname)
baab13
         error_msg("Bad problem directory name '%s', should start with: '%s'", dirname, g_settings_dump_location);
baab13
         return 400; /* Bad Request */
baab13
     }
baab13
+    if (!dir_has_correct_permissions(dirname))
baab13
+    {
baab13
+        error_msg("Problem directory '%s' isn't owned by root:abrt or others are not restricted from access", dirname);
baab13
+        return 400; /*  */
baab13
+    }
baab13
     if (g_settings_privatereports)
baab13
     {
baab13
-        struct stat statbuf;
baab13
-        if (lstat(dirname, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
baab13
-        {
baab13
-            error_msg("Path '%s' isn't directory", dirname);
baab13
-            return 404; /* Not Found */
baab13
-        }
baab13
-        /* Get ABRT's group gid */
baab13
-        struct group *gr = getgrnam("abrt");
baab13
-        if (!gr)
baab13
-        {
baab13
-            error_msg("Group 'abrt' does not exist");
baab13
-            return 500;
baab13
-        }
baab13
-        if (statbuf.st_uid != 0 || !(statbuf.st_gid == 0 || statbuf.st_gid == gr->gr_gid) || statbuf.st_mode & 07)
baab13
-        {
baab13
-            error_msg("Problem directory '%s' isn't owned by root:abrt or others are not restricted from access", dirname);
baab13
-            return 403;
baab13
-        }
baab13
         struct dump_dir *dd = dd_opendir(dirname, DD_OPEN_READONLY);
baab13
         const bool complete = dd && problem_dump_dir_is_complete(dd);
baab13
         dd_close(dd);
baab13
diff --git a/src/include/libabrt.h b/src/include/libabrt.h
baab13
index 0320c5b..5bf2397 100644
baab13
--- a/src/include/libabrt.h
baab13
+++ b/src/include/libabrt.h
baab13
@@ -47,6 +47,10 @@ char *run_unstrip_n(const char *dump_dir_name, unsigned timeout_sec);
baab13
 #define get_backtrace abrt_get_backtrace
baab13
 char *get_backtrace(const char *dump_dir_name, unsigned timeout_sec, const char *debuginfo_dirs);
baab13
 
baab13
+#define dir_is_in_dump_location abrt_dir_is_in_dump_location
baab13
+bool dir_is_in_dump_location(const char *dir_name);
baab13
+#define dir_has_correct_permissions abrt_dir_has_correct_permissions
baab13
+bool dir_has_correct_permissions(const char *dir_name);
baab13
 
baab13
 #define g_settings_nMaxCrashReportsSize abrt_g_settings_nMaxCrashReportsSize
baab13
 extern unsigned int  g_settings_nMaxCrashReportsSize;
baab13
diff --git a/src/lib/hooklib.c b/src/lib/hooklib.c
baab13
index fb7750d..4b20025 100644
baab13
--- a/src/lib/hooklib.c
baab13
+++ b/src/lib/hooklib.c
baab13
@@ -427,3 +427,59 @@ char* problem_data_save(problem_data_t *pd)
baab13
     log_info("problem id: '%s'", problem_id);
baab13
     return problem_id;
baab13
 }
baab13
+
baab13
+bool dir_is_in_dump_location(const char *dir_name)
baab13
+{
baab13
+    unsigned len = strlen(g_settings_dump_location);
baab13
+
baab13
+    /* The path must start with "g_settings_dump_location" */
baab13
+    if (strncmp(dir_name, g_settings_dump_location, len) != 0)
baab13
+    {
baab13
+        log_debug("Bad parent directory: '%s' not in '%s'", g_settings_dump_location, dir_name);
baab13
+        return false;
baab13
+    }
baab13
+
baab13
+    /* and must be a sub-directory of the g_settings_dump_location dir */
baab13
+    const char *base_name = dir_name + len;
baab13
+    while (*base_name && *base_name == '/')
baab13
+        ++base_name;
baab13
+
baab13
+    if (*(base_name - 1) != '/' || !str_is_correct_filename(base_name))
baab13
+    {
baab13
+        log_debug("Invalid dump directory name: '%s'", base_name);
baab13
+        return false;
baab13
+    }
baab13
+
baab13
+    /* and we are sure it is a directory */
baab13
+    struct stat sb;
baab13
+    if (lstat(dir_name, &sb) < 0)
baab13
+    {
baab13
+        VERB2 perror_msg("stat('%s')", dir_name);
baab13
+        return errno== ENOENT;
baab13
+    }
baab13
+
baab13
+    return S_ISDIR(sb.st_mode);
baab13
+}
baab13
+
baab13
+bool dir_has_correct_permissions(const char *dir_name)
baab13
+{
baab13
+    if (g_settings_privatereports)
baab13
+    {
baab13
+        struct stat statbuf;
baab13
+        if (lstat(dir_name, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
baab13
+        {
baab13
+            error_msg("Path '%s' isn't directory", dir_name);
baab13
+            return false;
baab13
+        }
baab13
+        /* Get ABRT's group gid */
baab13
+        struct group *gr = getgrnam("abrt");
baab13
+        if (!gr)
baab13
+        {
baab13
+            error_msg("Group 'abrt' does not exist");
baab13
+            return false;
baab13
+        }
baab13
+        if (statbuf.st_uid != 0 || !(statbuf.st_gid == 0 || statbuf.st_gid == gr->gr_gid) || statbuf.st_mode & 07)
baab13
+            return false;
baab13
+    }
baab13
+    return true;
baab13
+}
baab13
diff --git a/tests/Makefile.am b/tests/Makefile.am
baab13
index 5ef08a0..416f579 100644
baab13
--- a/tests/Makefile.am
baab13
+++ b/tests/Makefile.am
baab13
@@ -29,7 +29,8 @@ TESTSUITE_AT = \
baab13
   testsuite.at \
baab13
   pyhook.at \
baab13
   koops-parser.at \
baab13
-  ignored_problems.at
baab13
+  ignored_problems.at \
baab13
+  hooklib.at
baab13
 
baab13
 EXTRA_DIST += $(TESTSUITE_AT)
baab13
 TESTSUITE = $(srcdir)/testsuite
baab13
diff --git a/tests/hooklib.at b/tests/hooklib.at
baab13
new file mode 100644
baab13
index 0000000..70631c6
baab13
--- /dev/null
baab13
+++ b/tests/hooklib.at
baab13
@@ -0,0 +1,85 @@
baab13
+# -*- Autotest -*-
baab13
+
baab13
+AT_BANNER([hooklib])
baab13
+
baab13
+AT_TESTFUN([dir_is_in_dump_location],
baab13
+[[
baab13
+#include "libabrt.h"
baab13
+#include <assert.h>
baab13
+
baab13
+void test(char *name, bool expected)
baab13
+{
baab13
+    if (dir_is_in_dump_location(name) != expected)
baab13
+    {
baab13
+        fprintf(stderr, "Bad: %s", name);
baab13
+        abort();
baab13
+    }
baab13
+
baab13
+    free(name);
baab13
+}
baab13
+
baab13
+int main(void)
baab13
+{
baab13
+    g_verbose = 3;
baab13
+    load_abrt_conf();
baab13
+
baab13
+    g_verbose = 3;
baab13
+
baab13
+    char *name;
baab13
+
baab13
+    assert(dir_is_in_dump_location("/") == false);
baab13
+
baab13
+    asprintf(&name, "%s", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s..evil", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s/", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s///", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s/.", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s///.", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s/./", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s/.///", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s/..", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s///..", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s/../", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s/..///", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s/good/../../../evil", g_settings_dump_location);
baab13
+    test(name, false);
baab13
+
baab13
+    asprintf(&name, "%s/good..still", g_settings_dump_location);
baab13
+    test(name, true);
baab13
+
baab13
+    asprintf(&name, "%s/good.new", g_settings_dump_location);
baab13
+    test(name, true);
baab13
+
baab13
+    asprintf(&name, "%s/.meta", g_settings_dump_location);
baab13
+    test(name, true);
baab13
+
baab13
+    asprintf(&name, "%s/..data", g_settings_dump_location);
baab13
+    test(name, true);
baab13
+
baab13
+    return 0;
baab13
+}
baab13
+]])
baab13
diff --git a/tests/testsuite.at b/tests/testsuite.at
baab13
index b8f363d..765de2a 100644
baab13
--- a/tests/testsuite.at
baab13
+++ b/tests/testsuite.at
baab13
@@ -4,3 +4,4 @@
baab13
 m4_include([koops-parser.at])
baab13
 m4_include([pyhook.at])
baab13
 m4_include([ignored_problems.at])
baab13
+m4_include([hooklib.at])
baab13
-- 
baab13
1.8.3.1
baab13