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

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