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