Blame SOURCES/0119-ccpp-do-not-unlink-failed-and-big-user-cores.patch

06486d
From 7bd77a63f226a572946f30db3e76f23f971f46d5 Mon Sep 17 00:00:00 2001
06486d
From: Jakub Filak <jfilak@redhat.com>
06486d
Date: Wed, 20 May 2015 06:07:15 +0200
06486d
Subject: [ABRT PATCH] ccpp: do not unlink failed and big user cores
06486d
06486d
* We might end up deleting an already existing file.
06486d
* Kernel does not delete nor truncate core files. Admittedly, kernel
06486d
  knows how process's memory is structured, dumps it per logical
06486d
  segments and checks whether a next segment can be written.
06486d
* 'ulimit -c' does not seem to be a hard limit. Kernel wrote 8192 bytes
06486d
  despite $(ulimit -c) == 6.
06486d
06486d
Related: #1212818
06486d
06486d
Signed-off-by: Jakub Filak <jfilak@redhat.com>
06486d
---
06486d
 src/hooks/abrt-hook-ccpp.c | 70 +++++++++++++++++++---------------------------
06486d
 1 file changed, 29 insertions(+), 41 deletions(-)
06486d
06486d
diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
06486d
index fdd9b06..9b38ed7 100644
06486d
--- a/src/hooks/abrt-hook-ccpp.c
06486d
+++ b/src/hooks/abrt-hook-ccpp.c
06486d
@@ -129,8 +129,8 @@ static off_t copyfd_sparse(int src_fd, int dst_fd1, int dst_fd2, off_t size2)
06486d
 		size2 -= rd;
06486d
 		if (size2 < 0)
06486d
 			dst_fd2 = -1;
06486d
-//TODO: truncate to 0 or even delete the second file
06486d
-//(currently we delete the file later)
06486d
+// truncate to 0 or even delete the second file?
06486d
+// No, kernel does not delete nor truncate core files.
06486d
 	}
06486d
  out:
06486d
 
06486d
@@ -502,13 +502,20 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
06486d
 
06486d
 user_core_fail:
06486d
     if (user_core_fd >= 0)
06486d
-    {
06486d
         close(user_core_fd);
06486d
-        unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
06486d
-    }
06486d
     return -1;
06486d
 }
06486d
 
06486d
+static int close_user_core(int user_core_fd, off_t core_size)
06486d
+{
06486d
+    if (user_core_fd >= 0 && (fsync(user_core_fd) != 0 || close(user_core_fd) != 0 || core_size < 0))
06486d
+    {
06486d
+        perror_msg("Error writing '%s' at '%s'", core_basename, user_pwd);
06486d
+        return -1;
06486d
+    }
06486d
+    return 0;
06486d
+}
06486d
+
06486d
 static bool dump_fd_info(const char *dest_filename, char *source_filename, int source_base_ofs, uid_t uid, gid_t gid)
06486d
 {
06486d
     FILE *fp = fopen(dest_filename, "wx");
06486d
@@ -569,7 +576,7 @@ static int create_or_die(const char *filename)
06486d
     if (dd)
06486d
         dd_delete(dd);
06486d
     if (user_core_fd >= 0)
06486d
-        unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
06486d
+        close(user_core_fd);
06486d
 
06486d
     errno = sv_errno;
06486d
     perror_msg_and_die("Can't open '%s'", filename);
06486d
@@ -577,6 +584,7 @@ static int create_or_die(const char *filename)
06486d
 
06486d
 int main(int argc, char** argv)
06486d
 {
06486d
+    int err = 1;
06486d
     /* Kernel starts us with all fd's closed.
06486d
      * But it's dangerous:
06486d
      * fprintf(stderr) can dump messages into random fds, etc.
06486d
@@ -778,9 +786,8 @@ int main(int argc, char** argv)
06486d
             error_msg_and_die("Error saving '%s'", path);
06486d
         }
06486d
         log("Saved core dump of pid %lu (%s) to %s (%llu bytes)", (long)pid, executable, path, (long long)core_size);
06486d
-        if (proc_cwd != NULL)
06486d
-            closedir(proc_cwd);
06486d
-        return 0;
06486d
+        err = 0;
06486d
+        goto finito;
06486d
     }
06486d
 
06486d
     unsigned path_len = snprintf(path, sizeof(path), "%s/ccpp-%s-%lu.new",
06486d
@@ -895,26 +902,17 @@ int main(int argc, char** argv)
06486d
          * ls: cannot access core*: No such file or directory <=== BAD
06486d
          */
06486d
         off_t core_size = copyfd_sparse(STDIN_FILENO, abrt_core_fd, user_core_fd, ulimit_c);
06486d
+
06486d
+        close_user_core(user_core_fd, core_size);
06486d
+
06486d
         if (fsync(abrt_core_fd) != 0 || close(abrt_core_fd) != 0 || core_size < 0)
06486d
         {
06486d
             unlink(path);
06486d
             dd_delete(dd);
06486d
-            if (user_core_fd >= 0)
06486d
-                unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
06486d
             /* copyfd_sparse logs the error including errno string,
06486d
              * but it does not log file name */
06486d
             error_msg_and_die("Error writing '%s'", path);
06486d
         }
06486d
-        if (user_core_fd >= 0
06486d
-            /* error writing user coredump? */
06486d
-         && (fsync(user_core_fd) != 0 || close(user_core_fd) != 0
06486d
-            /* user coredump is too big? */
06486d
-            || (ulimit_c == 0 /* paranoia */ || core_size > ulimit_c)
06486d
-            )
06486d
-        ) {
06486d
-            /* nuke it (silently) */
06486d
-            unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
06486d
-        }
06486d
 
06486d
 /* Because of #1211835 and #1126850 */
06486d
 #if 0
06486d
@@ -984,9 +982,9 @@ int main(int argc, char** argv)
06486d
         }
06486d
 
06486d
         free(rootdir);
06486d
-        if (proc_cwd != NULL)
06486d
-            closedir(proc_cwd);
06486d
-        return 0;
06486d
+
06486d
+        err = 0;
06486d
+        goto finito;
06486d
     }
06486d
 
06486d
     /* We didn't create abrt dump, but may need to create compat coredump */
06486d
@@ -994,26 +992,16 @@ int main(int argc, char** argv)
06486d
     if (user_core_fd >= 0)
06486d
     {
06486d
         off_t core_size = copyfd_size(STDIN_FILENO, user_core_fd, ulimit_c, COPYFD_SPARSE);
06486d
-        if (fsync(user_core_fd) != 0 || close(user_core_fd) != 0 || core_size < 0)
06486d
-        {
06486d
-            /* perror first, otherwise unlink may trash errno */
06486d
-            perror_msg("Error writing '%s' at '%s'", core_basename, user_pwd);
06486d
-            unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
06486d
-            if (proc_cwd != NULL)
06486d
-                closedir(proc_cwd);
06486d
-            return 1;
06486d
-        }
06486d
-        if (ulimit_c == 0 || core_size > ulimit_c)
06486d
-        {
06486d
-            unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
06486d
-            if (proc_cwd != NULL)
06486d
-                closedir(proc_cwd);
06486d
-            return 1;
06486d
-        }
06486d
+        if (close_user_core(user_core_fd, core_size) != 0)
06486d
+            goto finito;
06486d
+
06486d
+        err = 0;
06486d
         log("Saved core dump of pid %lu to %s at %s (%llu bytes)", (long)pid, core_basename, user_pwd, (long long)core_size);
06486d
     }
06486d
 
06486d
+ finito:
06486d
     if (proc_cwd != NULL)
06486d
         closedir(proc_cwd);
06486d
-    return 0;
06486d
+
06486d
+    return err;
06486d
 }
06486d
-- 
06486d
1.8.3.1
06486d