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

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