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

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