Blame SOURCES/0113-upload-validate-and-sanitize-uploaded-dump-directori.patch

06486d
From a4e47c753e9d7988f4d938ed2e0fd690a909ce68 Mon Sep 17 00:00:00 2001
06486d
From: Jakub Filak <jfilak@redhat.com>
06486d
Date: Mon, 20 Apr 2015 15:15:40 +0200
06486d
Subject: [ABRT PATCH] upload: validate and sanitize uploaded dump directories
06486d
06486d
It was discovered that, when moving problem reports from
06486d
/var/spool/abrt-upload to /var/spool/abrt or /var/tmp/abrt,
06486d
abrt-handle-upload does not verify that the new problem directory
06486d
has appropriate permissions and does not contain symbolic links.  A
06486d
crafted problem report exposes other parts of abrt to attack, and
06486d
the abrt-handle-upload script allows to overwrite arbitrary files.
06486d
06486d
Acknowledgement:
06486d
06486d
This issue was discovered by Florian Weimer of Red Hat Product Security.
06486d
06486d
Related: #1212953
06486d
06486d
Signed-off-by: Jakub Filak <jfilak@redhat.com>
06486d
---
06486d
 src/daemon/abrt-handle-upload.in | 78 +++++++++++++++++++++++++++++++++++-----
06486d
 1 file changed, 70 insertions(+), 8 deletions(-)
06486d
06486d
diff --git a/src/daemon/abrt-handle-upload.in b/src/daemon/abrt-handle-upload.in
06486d
index dbc4534..7720da4 100755
06486d
--- a/src/daemon/abrt-handle-upload.in
06486d
+++ b/src/daemon/abrt-handle-upload.in
06486d
@@ -10,6 +10,7 @@ import getopt
06486d
 import tempfile
06486d
 import shutil
06486d
 import datetime
06486d
+import grp
06486d
 
06486d
 from reportclient import set_verbosity, error_msg_and_die, error_msg, log
06486d
 
06486d
@@ -36,12 +37,77 @@ def init_gettext():
06486d
 
06486d
 import problem
06486d
 
06486d
-def write_str_to(filename, s):
06486d
-    fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, @DEFAULT_DUMP_DIR_MODE@ | stat.S_IROTH)
06486d
+def write_str_to(filename, s, uid, gid, mode):
06486d
+    fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, mode)
06486d
     if fd >= 0:
06486d
+        os.fchown(fd, uid, gid)
06486d
         os.write(fd, s)
06486d
         os.close(fd)
06486d
 
06486d
+
06486d
+def validate_transform_move_and_notify(uploaded_dir_path, problem_dir_path, dest=None):
06486d
+    fsuid = 0
06486d
+    fsgid = 0
06486d
+
06486d
+    try:
06486d
+        gabrt = grp.getgrnam("abrt")
06486d
+        fsgid = gabrt.gr_gid
06486d
+    except KeyError as ex:
06486d
+        error_msg("Failed to get GID of 'abrt' (using 0 instead): {0}'".format(str(ex)))
06486d
+
06486d
+    try:
06486d
+        # give the uploaded directory to 'root:abrt' or 'root:root'
06486d
+        os.chown(uploaded_dir_path, fsuid, fsgid)
06486d
+        # set the right permissions for this machine
06486d
+        # (allow the owner and the group to access problem elements,
06486d
+        #  the default dump dir mode lacks x bit for both)
06486d
+        os.chmod(uploaded_dir_path, @DEFAULT_DUMP_DIR_MODE@ | stat.S_IXUSR | stat.S_IXGRP)
06486d
+
06486d
+        # sanitize problem elements
06486d
+        for item in os.listdir(uploaded_dir_path):
06486d
+            apath = os.path.join(uploaded_dir_path, item)
06486d
+            if os.path.islink(apath):
06486d
+                # remove symbolic links
06486d
+                os.remove(apath)
06486d
+            elif os.path.isdir(apath):
06486d
+                # remove directories
06486d
+                shutil.rmtree(apath)
06486d
+            elif os.path.isfile(apath):
06486d
+                # set file ownership to 'root:abrt' or 'root:root'
06486d
+                os.chown(apath, fsuid, fsgid)
06486d
+                # set the right file permissions for this machine
06486d
+                os.chmod(apath, @DEFAULT_DUMP_DIR_MODE@)
06486d
+            else:
06486d
+                # remove things that are neither files, symlinks nor directories
06486d
+                os.remove(apath)
06486d
+    except OSError as ex:
06486d
+        error_msg("Removing uploaded dir '{0}': '{1}'".format(uploaded_dir_path, str(ex)))
06486d
+        try:
06486d
+            shutil.rmtree(uploaded_dir_path)
06486d
+        except OSError as ex2:
06486d
+            error_msg_and_die("Failed to clean up dir '{0}': '{1}'".format(uploaded_dir_path, str(ex2)))
06486d
+        return
06486d
+
06486d
+    # overwrite remote if it exists
06486d
+    remote_path = os.path.join(uploaded_dir_path, "remote")
06486d
+    write_str_to(remote_path, "1", fsuid, fsgid, @DEFAULT_DUMP_DIR_MODE@)
06486d
+
06486d
+    # abrtd would increment count value and abrt-server refuses to process
06486d
+    # problem directories containing 'count' element when PrivateReports is on.
06486d
+    count_path = os.path.join(uploaded_dir_path, "count")
06486d
+    if os.path.exists(count_path):
06486d
+        # overwrite remote_count if it exists
06486d
+        remote_count_path = os.path.join(uploaded_dir_path, "remote_count")
06486d
+        os.rename(count_path, remote_count_path)
06486d
+
06486d
+    if not dest:
06486d
+        dest = problem_dir_path
06486d
+
06486d
+    shutil.move(uploaded_dir_path, dest)
06486d
+
06486d
+    problem.notify_new_path(problem_dir_path)
06486d
+
06486d
+
06486d
 if __name__ == "__main__":
06486d
 
06486d
     # Helper: exit with cleanup
06486d
@@ -177,21 +243,17 @@ if __name__ == "__main__":
06486d
         # or one or more complete problem data directories.
06486d
         # Checking second possibility first.
06486d
         if os.path.exists(tempdir+"/analyzer") and os.path.exists(tempdir+"/time"):
06486d
-            write_str_to(tempdir+"/remote", "1")
06486d
-            shutil.move(tempdir, abrt_dir)
06486d
-            problem.notify_new_path(abrt_dir+"/"+os.path.basename(tempdir))
06486d
+            validate_transform_move_and_notify(tempdir, abrt_dir+"/"+os.path.basename(tempdir), dest=abrt_dir)
06486d
         else:
06486d
             for d in os.listdir(tempdir):
06486d
                 if not os.path.isdir(tempdir+"/"+d):
06486d
                     continue
06486d
-                write_str_to(tempdir+"/"+d+"/remote", "1")
06486d
                 dst = abrt_dir+"/"+d
06486d
                 if os.path.exists(dst):
06486d
                     dst += "."+str(os.getpid())
06486d
                 if os.path.exists(dst):
06486d
                     continue
06486d
-                shutil.move(tempdir+"/"+d, dst)
06486d
-                problem.notify_new_path(dst)
06486d
+                validate_transform_move_and_notify(tempdir+"/"+d, dst)
06486d
 
06486d
         die_exitcode = 0
06486d
         # This deletes working_dir (== delete_on_exit)
06486d
-- 
06486d
1.8.3.1
06486d