Blame SOURCES/0099-ccpp-emulate-selinux-for-creation-of-compat-cores.patch

06486d
From 2f0a18b499b9b0e1afbdab8a8bb31d38f2acc6d8 Mon Sep 17 00:00:00 2001
06486d
From: Jakub Filak <jfilak@redhat.com>
06486d
Date: Fri, 17 Apr 2015 16:06:33 +0200
06486d
Subject: [ABRT PATCH] ccpp: emulate selinux for creation of compat cores
06486d
06486d
This issue was discovered by Florian Weimer of Red Hat Product Security.
06486d
06486d
http://article.gmane.org/gmane.comp.security.selinux/21842
06486d
06486d
v2: use the _raw interface and do the preparation steps as root
06486d
v3: don't fail if SELinux is disabled
06486d
    https://github.com/abrt/abrt/commit/c4f06d4198658c82550e93bb2617b96022c06cf4#commitcomment-11021276
06486d
06486d
Signed-off-by: Jakub Filak <jfilak@redhat.com>
06486d
---
06486d
 configure.ac               |  1 +
06486d
 src/hooks/Makefile.am      |  4 ++-
06486d
 src/hooks/abrt-hook-ccpp.c | 85 ++++++++++++++++++++++++++++++++++++++++++++--
06486d
 3 files changed, 86 insertions(+), 4 deletions(-)
06486d
06486d
diff --git a/configure.ac b/configure.ac
06486d
index 9ff616d..6c6d2e8 100644
06486d
--- a/configure.ac
06486d
+++ b/configure.ac
06486d
@@ -106,6 +106,7 @@ PKG_CHECK_MODULES([LIBREPORT_GTK], [libreport-gtk])
06486d
 PKG_CHECK_MODULES([POLKIT], [polkit-gobject-1])
06486d
 PKG_CHECK_MODULES([GIO], [gio-2.0])
06486d
 PKG_CHECK_MODULES([SATYR], [satyr])
06486d
+PKG_CHECK_MODULES([LIBSELINUX], [libselinux])
06486d
 
06486d
 PKG_PROG_PKG_CONFIG
06486d
 AC_ARG_WITH([systemdsystemunitdir],
06486d
diff --git a/src/hooks/Makefile.am b/src/hooks/Makefile.am
06486d
index e536089..9a527f4 100644
06486d
--- a/src/hooks/Makefile.am
06486d
+++ b/src/hooks/Makefile.am
06486d
@@ -33,10 +33,12 @@ abrt_hook_ccpp_CPPFLAGS = \
06486d
     -DDEFAULT_DUMP_DIR_MODE=$(DEFAULT_DUMP_DIR_MODE) \
06486d
     $(GLIB_CFLAGS) \
06486d
     $(LIBREPORT_CFLAGS) \
06486d
+    $(LIBSELINUX_CFLAGS) \
06486d
     -D_GNU_SOURCE
06486d
 abrt_hook_ccpp_LDADD = \
06486d
     ../lib/libabrt.la \
06486d
-    $(LIBREPORT_LIBS)
06486d
+    $(LIBREPORT_LIBS) \
06486d
+    $(LIBSELINUX_LIBS)
06486d
 
06486d
 # abrt-merge-pstoreoops
06486d
 abrt_merge_pstoreoops_SOURCES = \
06486d
diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
06486d
index 81f9349..00ae621 100644
06486d
--- a/src/hooks/abrt-hook-ccpp.c
06486d
+++ b/src/hooks/abrt-hook-ccpp.c
06486d
@@ -20,6 +20,7 @@
06486d
 */
06486d
 #include <sys/utsname.h>
06486d
 #include "libabrt.h"
06486d
+#include <selinux/selinux.h>
06486d
 
06486d
 #define  DUMP_SUID_UNSAFE 1
06486d
 #define  DUMP_SUID_SAFE 2
06486d
@@ -286,6 +287,54 @@ static int dump_suid_policy()
06486d
     return suid_dump_policy;
06486d
 }
06486d
 
06486d
+/* Computes a security context of new file created by the given process with
06486d
+ * pid in the given directory represented by file descriptor.
06486d
+ *
06486d
+ * On errors returns negative number. Returns 0 if the function succeeds and
06486d
+ * computes the context and returns positive number and assigns NULL to newcon
06486d
+ * if the security context is not needed (SELinux disabled).
06486d
+ */
06486d
+static int compute_selinux_con_for_new_file(pid_t pid, int dir_fd, security_context_t *newcon)
06486d
+{
06486d
+    security_context_t srccon;
06486d
+    security_context_t dstcon;
06486d
+
06486d
+    const int r = is_selinux_enabled();
06486d
+    if (r == 0)
06486d
+    {
06486d
+        *newcon = NULL;
06486d
+        return 1;
06486d
+    }
06486d
+    else if (r == -1)
06486d
+    {
06486d
+        perror_msg("Couldn't get state of SELinux");
06486d
+        return -1;
06486d
+    }
06486d
+    else if (r != 1)
06486d
+        error_msg_and_die("Unexpected SELinux return value: %d", r);
06486d
+
06486d
+
06486d
+    if (getpidcon_raw(pid, &srccon) < 0)
06486d
+    {
06486d
+        perror_msg("getpidcon_raw(%d)", pid);
06486d
+        return -1;
06486d
+    }
06486d
+
06486d
+    if (fgetfilecon_raw(dir_fd, &dstcon) < 0)
06486d
+    {
06486d
+        perror_msg("getfilecon_raw(%s)", user_pwd);
06486d
+        return -1;
06486d
+    }
06486d
+
06486d
+    if (security_compute_create_raw(srccon, dstcon, string_to_security_class("file"), newcon) < 0)
06486d
+    {
06486d
+        perror_msg("security_compute_create_raw(%s, %s, 'file')", srccon, dstcon);
06486d
+        return -1;
06486d
+    }
06486d
+
06486d
+    return 0;
06486d
+}
06486d
+
06486d
 static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_values)
06486d
 {
06486d
     proc_cwd = open_cwd(pid);
06486d
@@ -294,6 +343,14 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
06486d
 
06486d
     errno = 0;
06486d
 
06486d
+    /* http://article.gmane.org/gmane.comp.security.selinux/21842 */
06486d
+    security_context_t newcon;
06486d
+    if (compute_selinux_con_for_new_file(pid, dirfd(proc_cwd), &newcon) < 0)
06486d
+    {
06486d
+        log_notice("Not going to create a user core due to SELinux errors");
06486d
+        return -1;
06486d
+    }
06486d
+
06486d
     xsetegid(get_fsgid());
06486d
     xseteuid(fsuid);
06486d
 
06486d
@@ -388,10 +445,25 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
06486d
      * (However, see the description of the prctl(2) PR_SET_DUMPABLE operation,
06486d
      * and the description of the /proc/sys/fs/suid_dumpable file in proc(5).)
06486d
      */
06486d
+
06486d
+    /* Set SELinux context like kernel when creating core dump file */
06486d
+    if (newcon != NULL && setfscreatecon_raw(newcon) < 0)
06486d
+    {
06486d
+        perror_msg("setfscreatecon_raw(%s)", newcon);
06486d
+        return -1;
06486d
+    }
06486d
+
06486d
     struct stat sb;
06486d
     errno = 0;
06486d
     /* Do not O_TRUNC: if later checks fail, we do not want to have file already modified here */
06486d
     int user_core_fd = openat(dirfd(proc_cwd), core_basename, O_WRONLY | O_CREAT | O_NOFOLLOW | g_user_core_flags, 0600); /* kernel makes 0600 too */
06486d
+
06486d
+    if (newcon != NULL && setfscreatecon_raw(NULL) < 0)
06486d
+    {
06486d
+        error_msg("setfscreatecon_raw(NULL)");
06486d
+        goto user_core_fail;
06486d
+    }
06486d
+
06486d
     xsetegid(0);
06486d
     xseteuid(0);
06486d
     if (user_core_fd < 0
06486d
@@ -404,16 +476,23 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
06486d
             perror_msg("Can't open '%s' at '%s'", core_basename, user_pwd);
06486d
         else
06486d
             perror_msg("'%s' at '%s' is not a regular file with link count 1 owned by UID(%d)", core_basename, user_pwd, fsuid);
06486d
-        return -1;
06486d
+        goto user_core_fail;
06486d
     }
06486d
     if (ftruncate(user_core_fd, 0) != 0) {
06486d
         /* perror first, otherwise unlink may trash errno */
06486d
         perror_msg("Can't truncate '%s' at '%s' to size 0", core_basename, user_pwd);
06486d
-        unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
06486d
-        return -1;
06486d
+        goto user_core_fail;
06486d
     }
06486d
 
06486d
     return user_core_fd;
06486d
+
06486d
+user_core_fail:
06486d
+    if (user_core_fd >= 0)
06486d
+    {
06486d
+        close(user_core_fd);
06486d
+        unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
06486d
+    }
06486d
+    return -1;
06486d
 }
06486d
 
06486d
 static bool dump_fd_info(const char *dest_filename, char *source_filename, int source_base_ofs, uid_t uid, gid_t gid)
06486d
-- 
06486d
1.8.3.1
06486d