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

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