Blame SOURCES/0112-ccpp-revert-the-UID-GID-changes-if-user-core-fails.patch

8ec399
From 7269a2cc88735aee0d1fa62491b9efe73ab5c6e8 Mon Sep 17 00:00:00 2001
8ec399
From: Jakub Filak <jfilak@redhat.com>
8ec399
Date: Mon, 4 May 2015 13:23:43 +0200
8ec399
Subject: [ABRT PATCH] ccpp: revert the UID/GID changes if user core fails
8ec399
8ec399
Thanks Florian Weimer <fweimer@redhat.com>
8ec399
8ec399
Signed-off-by: Jakub Filak <jfilak@redhat.com>
8ec399
---
8ec399
 src/hooks/abrt-hook-ccpp.c | 58 ++++++++++++++++++++++++++++------------------
8ec399
 1 file changed, 36 insertions(+), 22 deletions(-)
8ec399
8ec399
diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
8ec399
index 02f15d5..fdd9b06 100644
8ec399
--- a/src/hooks/abrt-hook-ccpp.c
8ec399
+++ b/src/hooks/abrt-hook-ccpp.c
8ec399
@@ -351,9 +351,6 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
8ec399
         return -1;
8ec399
     }
8ec399
 
8ec399
-    xsetegid(get_fsgid());
8ec399
-    xseteuid(fsuid);
8ec399
-
8ec399
     if (strcmp(core_basename, "core") == 0)
8ec399
     {
8ec399
         /* Mimic "core.PID" if requested */
8ec399
@@ -446,36 +443,53 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
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
+    int user_core_fd = -1;
8ec399
+    int selinux_fail = 1;
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
+     * These calls must be reverted as soon as possible.
8ec399
+     */
8ec399
+    xsetegid(get_fsgid());
8ec399
+    xseteuid(fsuid);
8ec399
 
8ec399
-    if (newcon != NULL && setfscreatecon_raw(NULL) < 0)
8ec399
+    /* Set SELinux context like kernel when creating core dump file.
8ec399
+     * This condition is TRUE if */
8ec399
+    if (/* SELinux is disabled  */ newcon == NULL
8ec399
+     || /* or the call succeeds */ setfscreatecon_raw(newcon) >= 0)
8ec399
     {
8ec399
-        error_msg("setfscreatecon_raw(NULL)");
8ec399
-        goto user_core_fail;
8ec399
+        /* Do not O_TRUNC: if later checks fail, we do not want to have file already modified here */
8ec399
+        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
+        /* Do the error check here and print the error message in order to
8ec399
+         * avoid interference in 'errno' usage caused by SELinux functions */
8ec399
+        if (user_core_fd < 0)
8ec399
+            perror_msg("Can't open '%s' at '%s'", core_basename, user_pwd);
8ec399
+
8ec399
+        /* Fail if SELinux is enabled and the call fails */
8ec399
+        if (newcon != NULL && setfscreatecon_raw(NULL) < 0)
8ec399
+            perror_msg("setfscreatecon_raw(NULL)");
8ec399
+        else
8ec399
+            selinux_fail = 0;
8ec399
     }
8ec399
+    else
8ec399
+        perror_msg("setfscreatecon_raw(%s)", newcon);
8ec399
 
8ec399
+    /*
8ec399
+     * DON'T JUMP OVER THIS REVERT OF THE UID/GID CHANGES
8ec399
+     */
8ec399
     xsetegid(0);
8ec399
     xseteuid(0);
8ec399
-    if (user_core_fd < 0
8ec399
-     || fstat(user_core_fd, &sb) != 0
8ec399
+
8ec399
+    if (user_core_fd < 0 || selinux_fail)
8ec399
+        goto user_core_fail;
8ec399
+
8ec399
+    struct stat sb;
8ec399
+    if (fstat(user_core_fd, &sb) != 0
8ec399
      || !S_ISREG(sb.st_mode)
8ec399
      || sb.st_nlink != 1
8ec399
      || sb.st_uid != fsuid
8ec399
     ) {
8ec399
-        if (user_core_fd < 0)
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
+        perror_msg("'%s' at '%s' is not a regular file with link count 1 owned by UID(%d)", core_basename, user_pwd, fsuid);
8ec399
         goto user_core_fail;
8ec399
     }
8ec399
     if (ftruncate(user_core_fd, 0) != 0) {
8ec399
-- 
8ec399
1.8.3.1
8ec399