arrfab / rpms / abrt

Forked from rpms/abrt 5 years ago
Clone

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

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