Blame SOURCES/0147-abrt-hook-ccpp-minor-refactoring.patch

a60cd7
From 09dcb3af839ee80b3e7faf35a621d0ff0dcc9ebf Mon Sep 17 00:00:00 2001
a60cd7
From: Martin Milata <mmilata@redhat.com>
a60cd7
Date: Mon, 1 Dec 2014 11:47:55 +0100
a60cd7
Subject: [PATCH] abrt-hook-ccpp: minor refactoring
a60cd7
a60cd7
Related to #829.
a60cd7
a60cd7
Signed-off-by: Martin Milata <mmilata@redhat.com>
a60cd7
a60cd7
Conflicts:
a60cd7
	src/hooks/abrt-hook-ccpp.c
a60cd7
---
a60cd7
 src/hooks/abrt-hook-ccpp.c | 83 ++++++++++++++++++++++++++++------------------
a60cd7
 1 file changed, 50 insertions(+), 33 deletions(-)
a60cd7
a60cd7
diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
a60cd7
index 9b38ed7..2dd9ac6 100644
a60cd7
--- a/src/hooks/abrt-hook-ccpp.c
a60cd7
+++ b/src/hooks/abrt-hook-ccpp.c
a60cd7
@@ -143,12 +143,10 @@ static off_t copyfd_sparse(int src_fd, int dst_fd1, int dst_fd2, off_t size2)
a60cd7
 
a60cd7
 
a60cd7
 /* Global data */
a60cd7
-
a60cd7
 static char *user_pwd;
a60cd7
 static DIR *proc_cwd;
a60cd7
-static char *proc_pid_status;
a60cd7
 static struct dump_dir *dd;
a60cd7
-static int user_core_fd = -1;
a60cd7
+
a60cd7
 /*
a60cd7
  * %s - signal number
a60cd7
  * %c - ulimit -c value
a60cd7
@@ -219,7 +217,7 @@ static char* get_rootdir(pid_t pid)
a60cd7
     return malloc_readlink(buf);
a60cd7
 }
a60cd7
 
a60cd7
-static int get_proc_fs_id(char type)
a60cd7
+static int get_proc_fs_id(char type, char *proc_pid_status)
a60cd7
 {
a60cd7
     const char *scanf_format = "%*cid:\t%d\t%d\t%d\t%d\n";
a60cd7
     char id_type[] = "_id";
a60cd7
@@ -250,14 +248,14 @@ static int get_proc_fs_id(char type)
a60cd7
     perror_msg_and_die("Failed to get file system %cID of the crashed process", type);
a60cd7
 }
a60cd7
 
a60cd7
-static int get_fsuid(void)
a60cd7
+static int get_fsuid(char *proc_pid_status)
a60cd7
 {
a60cd7
-    return get_proc_fs_id(/*UID*/'U');
a60cd7
+    return get_proc_fs_id(/*UID*/'U', proc_pid_status);
a60cd7
 }
a60cd7
 
a60cd7
-static int get_fsgid(void)
a60cd7
+static int get_fsgid(char *proc_pid_status)
a60cd7
 {
a60cd7
-    return get_proc_fs_id(/*GID*/'G');
a60cd7
+    return get_proc_fs_id(/*GID*/'G', proc_pid_status);
a60cd7
 }
a60cd7
 
a60cd7
 static int dump_suid_policy()
a60cd7
@@ -335,7 +333,7 @@ static int compute_selinux_con_for_new_file(pid_t pid, int dir_fd, security_cont
a60cd7
     return 0;
a60cd7
 }
a60cd7
 
a60cd7
-static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_values)
a60cd7
+static int open_user_core(uid_t uid, uid_t fsuid, gid_t fsgid, pid_t pid, char **percent_values)
a60cd7
 {
a60cd7
     proc_cwd = open_cwd(pid);
a60cd7
     if (proc_cwd == NULL)
a60cd7
@@ -449,7 +447,7 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
a60cd7
     /*
a60cd7
      * These calls must be reverted as soon as possible.
a60cd7
      */
a60cd7
-    xsetegid(get_fsgid());
a60cd7
+    xsetegid(fsgid);
a60cd7
     xseteuid(fsuid);
a60cd7
 
a60cd7
     /* Set SELinux context like kernel when creating core dump file.
a60cd7
@@ -563,7 +561,7 @@ static bool dump_fd_info(const char *dest_filename, char *source_filename, int s
a60cd7
 }
a60cd7
 
a60cd7
 /* Like xopen, but on error, unlocks and deletes dd and user core */
a60cd7
-static int create_or_die(const char *filename)
a60cd7
+static int create_or_die(const char *filename, int user_core_fd)
a60cd7
 {
a60cd7
     int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, DEFAULT_DUMP_DIR_MODE);
a60cd7
     if (fd >= 0)
a60cd7
@@ -582,6 +580,29 @@ static int create_or_die(const char *filename)
a60cd7
     perror_msg_and_die("Can't open '%s'", filename);
a60cd7
 }
a60cd7
 
a60cd7
+static int create_user_core(int user_core_fd, pid_t pid, off_t ulimit_c)
a60cd7
+{
a60cd7
+    int err = 1;
a60cd7
+    if (user_core_fd >= 0)
a60cd7
+    {
a60cd7
+        off_t core_size = copyfd_size(STDIN_FILENO, user_core_fd, ulimit_c, COPYFD_SPARSE);
a60cd7
+        if (close_user_core(user_core_fd, core_size) != 0)
a60cd7
+            goto finito;
a60cd7
+
a60cd7
+        err = 0;
a60cd7
+        log("Saved core dump of pid %lu to %s at %s (%llu bytes)", (long)pid, core_basename, user_pwd, (long long)core_size);
a60cd7
+    }
a60cd7
+
a60cd7
+finito:
a60cd7
+    if (proc_cwd != NULL)
a60cd7
+    {
a60cd7
+        closedir(proc_cwd);
a60cd7
+        proc_cwd = NULL;
a60cd7
+    }
a60cd7
+
a60cd7
+    return err;
a60cd7
+}
a60cd7
+
a60cd7
 int main(int argc, char** argv)
a60cd7
 {
a60cd7
     int err = 1;
a60cd7
@@ -686,10 +707,12 @@ int main(int argc, char** argv)
a60cd7
     log_notice("user_pwd:'%s'", user_pwd);
a60cd7
 
a60cd7
     sprintf(path, "/proc/%lu/status", (long)pid);
a60cd7
-    proc_pid_status = xmalloc_xopen_read_close(path, /*maxsz:*/ NULL);
a60cd7
+    char *proc_pid_status = xmalloc_xopen_read_close(path, /*maxsz:*/ NULL);
a60cd7
 
a60cd7
     uid_t fsuid = uid;
a60cd7
-    uid_t tmp_fsuid = get_fsuid();
a60cd7
+    uid_t tmp_fsuid = get_fsuid(proc_pid_status);
a60cd7
+    const int fsgid = get_fsgid(proc_pid_status);
a60cd7
+
a60cd7
     int suid_policy = dump_suid_policy();
a60cd7
     if (tmp_fsuid != uid)
a60cd7
     {
a60cd7
@@ -708,15 +731,16 @@ int main(int argc, char** argv)
a60cd7
     const uid_t dduid = g_settings_privatereports ? 0 : fsuid;
a60cd7
 
a60cd7
     /* Open a fd to compat coredump, if requested and is possible */
a60cd7
+    int user_core_fd = -1;
a60cd7
     if (setting_MakeCompatCore && ulimit_c != 0)
a60cd7
         /* note: checks "user_pwd == NULL" inside; updates core_basename */
a60cd7
-        user_core_fd = open_user_core(uid, fsuid, pid, &argv[1]);
a60cd7
+        user_core_fd = open_user_core(uid, fsuid, fsgid, pid, &argv[1]);
a60cd7
 
a60cd7
     if (executable == NULL)
a60cd7
     {
a60cd7
         /* readlink on /proc/$PID/exe failed, don't create abrt dump dir */
a60cd7
         error_msg("Can't read /proc/%lu/exe link", (long)pid);
a60cd7
-        goto create_user_core;
a60cd7
+        return create_user_core(user_core_fd, pid, ulimit_c);
a60cd7
     }
a60cd7
 
a60cd7
     const char *signame = NULL;
a60cd7
@@ -735,7 +759,7 @@ int main(int argc, char** argv)
a60cd7
       //case SIGSYS : signame = "SYS" ; break; //Bad argument to routine (SVr4)
a60cd7
       //case SIGXCPU: signame = "XCPU"; break; //CPU time limit exceeded (4.2BSD)
a60cd7
       //case SIGXFSZ: signame = "XFSZ"; break; //File size limit exceeded (4.2BSD)
a60cd7
-        default: goto create_user_core; // not a signal we care about
a60cd7
+        default: return create_user_core(user_core_fd, pid, ulimit_c); // not a signal we care about
a60cd7
     }
a60cd7
 
a60cd7
     if (!daemon_is_ok())
a60cd7
@@ -745,14 +769,14 @@ int main(int argc, char** argv)
a60cd7
             "/proc/sys/kernel/core_pattern contains a stale value, "
a60cd7
             "consider resetting it to 'core'"
a60cd7
         );
a60cd7
-        goto create_user_core;
a60cd7
+        return create_user_core(user_core_fd, pid, ulimit_c);
a60cd7
     }
a60cd7
 
a60cd7
     if (g_settings_nMaxCrashReportsSize > 0)
a60cd7
     {
a60cd7
         /* If free space is less than 1/4 of MaxCrashReportsSize... */
a60cd7
         if (low_free_space(g_settings_nMaxCrashReportsSize, g_settings_dump_location))
a60cd7
-            goto create_user_core;
a60cd7
+            return create_user_core(user_core_fd, pid, ulimit_c);
a60cd7
     }
a60cd7
 
a60cd7
     /* Check /var/tmp/abrt/last-ccpp marker, do not dump repeated crashes
a60cd7
@@ -762,7 +786,7 @@ int main(int argc, char** argv)
a60cd7
     if (check_recent_crash_file(path, executable))
a60cd7
     {
a60cd7
         /* It is a repeating crash */
a60cd7
-        goto create_user_core;
a60cd7
+        return create_user_core(user_core_fd, pid, ulimit_c);
a60cd7
     }
a60cd7
 
a60cd7
     const char *last_slash = strrchr(executable, '/');
a60cd7
@@ -794,7 +818,7 @@ int main(int argc, char** argv)
a60cd7
             g_settings_dump_location, iso_date_string(NULL), (long)pid);
a60cd7
     if (path_len >= (sizeof(path) - sizeof("/"FILENAME_COREDUMP)))
a60cd7
     {
a60cd7
-        goto create_user_core;
a60cd7
+        return create_user_core(user_core_fd, pid, ulimit_c);
a60cd7
     }
a60cd7
 
a60cd7
     /* use dduid (either fsuid or 0) instead of uid, so we don't expose any
a60cd7
@@ -878,7 +902,7 @@ int main(int argc, char** argv)
a60cd7
         if (src_fd_binary > 0)
a60cd7
         {
a60cd7
             strcpy(path + path_len, "/"FILENAME_BINARY);
a60cd7
-            int dst_fd = create_or_die(path);
a60cd7
+            int dst_fd = create_or_die(path, user_core_fd);
a60cd7
             off_t sz = copyfd_eof(src_fd_binary, dst_fd, COPYFD_SPARSE);
a60cd7
             if (fsync(dst_fd) != 0 || close(dst_fd) != 0 || sz < 0)
a60cd7
             {
a60cd7
@@ -889,7 +913,7 @@ int main(int argc, char** argv)
a60cd7
         }
a60cd7
 
a60cd7
         strcpy(path + path_len, "/"FILENAME_COREDUMP);
a60cd7
-        int abrt_core_fd = create_or_die(path);
a60cd7
+        int abrt_core_fd = create_or_die(path, user_core_fd);
a60cd7
 
a60cd7
         /* We write both coredumps at once.
a60cd7
          * We can't write user coredump first, since it might be truncated
a60cd7
@@ -938,7 +962,7 @@ int main(int argc, char** argv)
a60cd7
             if (src_fd >= 0)
a60cd7
             {
a60cd7
                 strcpy(path + path_len, "/hs_err.log");
a60cd7
-                int dst_fd = create_or_die(path);
a60cd7
+                int dst_fd = create_or_die(path, user_core_fd);
a60cd7
                 off_t sz = copyfd_eof(src_fd, dst_fd, COPYFD_SPARSE);
a60cd7
                 if (close(dst_fd) != 0 || sz < 0)
a60cd7
                 {
a60cd7
@@ -986,17 +1010,10 @@ int main(int argc, char** argv)
a60cd7
         err = 0;
a60cd7
         goto finito;
a60cd7
     }
a60cd7
-
a60cd7
-    /* We didn't create abrt dump, but may need to create compat coredump */
a60cd7
- create_user_core:
a60cd7
-    if (user_core_fd >= 0)
a60cd7
+    else
a60cd7
     {
a60cd7
-        off_t core_size = copyfd_size(STDIN_FILENO, user_core_fd, ulimit_c, COPYFD_SPARSE);
a60cd7
-        if (close_user_core(user_core_fd, core_size) != 0)
a60cd7
-            goto finito;
a60cd7
-
a60cd7
-        err = 0;
a60cd7
-        log("Saved core dump of pid %lu to %s at %s (%llu bytes)", (long)pid, core_basename, user_pwd, (long long)core_size);
a60cd7
+        /* We didn't create abrt dump, but may need to create compat coredump */
a60cd7
+        return create_user_core(user_core_fd, pid, ulimit_c);
a60cd7
     }
a60cd7
 
a60cd7
  finito:
a60cd7
-- 
a60cd7
2.4.3
a60cd7