doczkal / rpms / abrt

Forked from rpms/abrt 4 years ago
Clone
Blob Blame History Raw
From 09dcb3af839ee80b3e7faf35a621d0ff0dcc9ebf Mon Sep 17 00:00:00 2001
From: Martin Milata <mmilata@redhat.com>
Date: Mon, 1 Dec 2014 11:47:55 +0100
Subject: [PATCH] abrt-hook-ccpp: minor refactoring

Related to #829.

Signed-off-by: Martin Milata <mmilata@redhat.com>

Conflicts:
	src/hooks/abrt-hook-ccpp.c
---
 src/hooks/abrt-hook-ccpp.c | 83 ++++++++++++++++++++++++++++------------------
 1 file changed, 50 insertions(+), 33 deletions(-)

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