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

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