Blame 0073-dd-harden-functions-against-directory-traversal-issu.patch

3a204fb
From 14872ca4ac094205519dcbadde9b7f1ff28eda9a Mon Sep 17 00:00:00 2001
3a204fb
From: Jakub Filak <jfilak@redhat.com>
3a204fb
Date: Thu, 23 Apr 2015 13:30:15 +0200
3a204fb
Subject: [PATCH] dd: harden functions against directory traversal issues
3a204fb
3a204fb
Test correctness of all accessed dump dir files in all dd* functions.
3a204fb
Before this commit, the callers were allowed to pass strings like
3a204fb
"../../etc/shadow" in the filename argument of all dd* functions.
3a204fb
3a204fb
Related: #1214457
3a204fb
3a204fb
Signed-off-by: Jakub Filak <jfilak@redhat.com>
3a204fb
---
3a204fb
 src/lib/create_dump_dir.c | 15 ++++++++++-----
3a204fb
 src/lib/dump_dir.c        | 30 ++++++++++++++++++++++++++++++
3a204fb
 2 files changed, 40 insertions(+), 5 deletions(-)
3a204fb
3a204fb
diff --git a/src/lib/create_dump_dir.c b/src/lib/create_dump_dir.c
3a204fb
index 6aee370..d2cdd29 100644
3a204fb
--- a/src/lib/create_dump_dir.c
3a204fb
+++ b/src/lib/create_dump_dir.c
3a204fb
@@ -107,16 +107,15 @@ int save_problem_data_in_dump_dir(struct dump_dir *dd, problem_data_t *problem_d
3a204fb
     g_hash_table_iter_init(&iter, problem_data);
3a204fb
     while (g_hash_table_iter_next(&iter, (void**)&name, (void**)&value))
3a204fb
     {
3a204fb
-        if (value->flags & CD_FLAG_BIN)
3a204fb
+        if (!str_is_correct_filename(name))
3a204fb
         {
3a204fb
-            dd_copy_file(dd, name, value->content);
3a204fb
+            error_msg("Problem data field name contains disallowed chars: '%s'", name);
3a204fb
             continue;
3a204fb
         }
3a204fb
 
3a204fb
-        /* only files should contain '/' and those are handled earlier */
3a204fb
-        if (name[0] == '.' || strchr(name, '/'))
3a204fb
+        if (value->flags & CD_FLAG_BIN)
3a204fb
         {
3a204fb
-            error_msg("Problem data field name contains disallowed chars: '%s'", name);
3a204fb
+            dd_copy_file(dd, name, value->content);
3a204fb
             continue;
3a204fb
         }
3a204fb
 
3a204fb
@@ -138,6 +137,12 @@ struct dump_dir *create_dump_dir_from_problem_data(problem_data_t *problem_data,
3a204fb
         return NULL;
3a204fb
     }
3a204fb
 
3a204fb
+    if (!str_is_correct_filename(type))
3a204fb
+    {
3a204fb
+        error_msg(_("'%s' is not correct file name"), FILENAME_TYPE);
3a204fb
+        return NULL;
3a204fb
+    }
3a204fb
+
3a204fb
     uid_t uid = (uid_t)-1L;
3a204fb
     char *uid_str = problem_data_get_content_or_NULL(problem_data, FILENAME_UID);
3a204fb
 
3a204fb
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
3a204fb
index 25a8aeb..017a9c1 100644
3a204fb
--- a/src/lib/dump_dir.c
3a204fb
+++ b/src/lib/dump_dir.c
3a204fb
@@ -357,6 +357,9 @@ static inline struct dump_dir *dd_init(void)
3a204fb
 
3a204fb
 int dd_exist(const struct dump_dir *dd, const char *path)
3a204fb
 {
3a204fb
+    if (!str_is_correct_filename(path))
3a204fb
+        error_msg_and_die("Cannot test existence. '%s' is not a valid file name", path);
3a204fb
+
3a204fb
     char *full_path = concat_path_file(dd->dd_dirname, path);
3a204fb
     int ret = exist_file_dir(full_path);
3a204fb
     free(full_path);
3a204fb
@@ -1059,6 +1062,15 @@ char* dd_load_text_ext(const struct dump_dir *dd, const char *name, unsigned fla
3a204fb
 //    if (!dd->locked)
3a204fb
 //        error_msg_and_die("dump_dir is not opened"); /* bug */
3a204fb
 
3a204fb
+    if (!str_is_correct_filename(name))
3a204fb
+    {
3a204fb
+        error_msg("Cannot load text. '%s' is not a valid file name", name);
3a204fb
+        if ((flags & DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE))
3a204fb
+            return NULL;
3a204fb
+
3a204fb
+        xfunc_die();
3a204fb
+    }
3a204fb
+
3a204fb
     /* Compat with old abrt dumps. Remove in abrt-2.1 */
3a204fb
     if (strcmp(name, "release") == 0)
3a204fb
         name = FILENAME_OS_RELEASE;
3a204fb
@@ -1080,6 +1092,9 @@ void dd_save_text(struct dump_dir *dd, const char *name, const char *data)
3a204fb
     if (!dd->locked)
3a204fb
         error_msg_and_die("dump_dir is not opened"); /* bug */
3a204fb
 
3a204fb
+    if (!str_is_correct_filename(name))
3a204fb
+        error_msg_and_die("Cannot save text. '%s' is not a valid file name", name);
3a204fb
+
3a204fb
     char *full_path = concat_path_file(dd->dd_dirname, name);
3a204fb
     save_binary_file(full_path, data, strlen(data), dd->dd_uid, dd->dd_gid, dd->mode);
3a204fb
     free(full_path);
3a204fb
@@ -1090,6 +1105,9 @@ void dd_save_binary(struct dump_dir* dd, const char* name, const char* data, uns
3a204fb
     if (!dd->locked)
3a204fb
         error_msg_and_die("dump_dir is not opened"); /* bug */
3a204fb
 
3a204fb
+    if (!str_is_correct_filename(name))
3a204fb
+        error_msg_and_die("Cannot save binary. '%s' is not a valid file name", name);
3a204fb
+
3a204fb
     char *full_path = concat_path_file(dd->dd_dirname, name);
3a204fb
     save_binary_file(full_path, data, size, dd->dd_uid, dd->dd_gid, dd->mode);
3a204fb
     free(full_path);
3a204fb
@@ -1097,6 +1115,9 @@ void dd_save_binary(struct dump_dir* dd, const char* name, const char* data, uns
3a204fb
 
3a204fb
 long dd_get_item_size(struct dump_dir *dd, const char *name)
3a204fb
 {
3a204fb
+    if (!str_is_correct_filename(name))
3a204fb
+        error_msg_and_die("Cannot get item size. '%s' is not a valid file name", name);
3a204fb
+
3a204fb
     long size = -1;
3a204fb
     char *iname = concat_path_file(dd->dd_dirname, name);
3a204fb
     struct stat statbuf;
3a204fb
@@ -1121,6 +1142,9 @@ int dd_delete_item(struct dump_dir *dd, const char *name)
3a204fb
     if (!dd->locked)
3a204fb
         error_msg_and_die("dump_dir is not opened"); /* bug */
3a204fb
 
3a204fb
+    if (!str_is_correct_filename(name))
3a204fb
+        error_msg_and_die("Cannot delete item. '%s' is not a valid file name", name);
3a204fb
+
3a204fb
     char *path = concat_path_file(dd->dd_dirname, name);
3a204fb
     int res = unlink(path);
3a204fb
 
3a204fb
@@ -1335,6 +1359,9 @@ int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason)
3a204fb
 
3a204fb
 int dd_copy_file(struct dump_dir *dd, const char *name, const char *source_path)
3a204fb
 {
3a204fb
+    if (!str_is_correct_filename(name))
3a204fb
+        error_msg_and_die("Cannot test existence. '%s' is not a valid file name", name);
3a204fb
+
3a204fb
     char *dest = concat_path_file(dd->dd_dirname, name);
3a204fb
 
3a204fb
     log_debug("copying '%s' to '%s'", source_path, dest);
3a204fb
@@ -1351,6 +1378,9 @@ int dd_copy_file(struct dump_dir *dd, const char *name, const char *source_path)
3a204fb
 
3a204fb
 int dd_copy_file_unpack(struct dump_dir *dd, const char *name, const char *source_path)
3a204fb
 {
3a204fb
+    if (!str_is_correct_filename(name))
3a204fb
+        error_msg_and_die("Cannot test existence. '%s' is not a valid file name", name);
3a204fb
+
3a204fb
     char *dest = concat_path_file(dd->dd_dirname, name);
3a204fb
 
3a204fb
     log_debug("unpacking '%s' to '%s'", source_path, dest);
3a204fb
-- 
3a204fb
2.1.0
3a204fb