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

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