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