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