arrfab / rpms / abrt

Forked from rpms/abrt 5 years ago
Clone

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

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