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

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