ishcherb / rpms / abrt

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