Blame SOURCES/0089-ccpp-fix-symlink-race-conditions.patch

baab13
From 80408e9e24a1c10f85fd969e1853e0f192157f92 Mon Sep 17 00:00:00 2001
baab13
From: Jakub Filak <jfilak@redhat.com>
baab13
Date: Wed, 15 Apr 2015 12:14:22 +0200
baab13
Subject: [ABRT PATCH] ccpp: fix symlink race conditions
baab13
baab13
Fix copy & chown race conditions
baab13
baab13
Related: #1211835
baab13
baab13
Signed-off-by: Jakub Filak <jfilak@redhat.com>
baab13
---
baab13
 src/hooks/abrt-hook-ccpp.c | 27 ++++++++++++++++-----------
baab13
 1 file changed, 16 insertions(+), 11 deletions(-)
baab13
baab13
diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
baab13
index 8e141d4..be16fab 100644
baab13
--- a/src/hooks/abrt-hook-ccpp.c
baab13
+++ b/src/hooks/abrt-hook-ccpp.c
baab13
@@ -397,7 +397,7 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
baab13
     return user_core_fd;
baab13
 }
baab13
 
baab13
-static bool dump_fd_info(const char *dest_filename, char *source_filename, int source_base_ofs)
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, "w");
baab13
     if (!fp)
baab13
@@ -429,6 +429,16 @@ static bool dump_fd_info(const char *dest_filename, char *source_filename, int s
baab13
         }
baab13
         fclose(in);
baab13
     }
baab13
+
baab13
+    const int dest_fd = fileno(fp);
baab13
+    if (fchown(dest_fd, uid, gid) < 0)
baab13
+    {
baab13
+        perror_msg("Can't change '%s' ownership to %lu:%lu", dest_filename, (long)uid, (long)gid);
baab13
+        fclose(fp);
baab13
+        unlink(dest_filename);
baab13
+        return false;
baab13
+    }
baab13
+
baab13
     fclose(fp);
baab13
     return true;
baab13
 }
baab13
@@ -678,27 +688,22 @@ int main(int argc, char** argv)
baab13
 
baab13
         // Disabled for now: /proc/PID/smaps tends to be BIG,
baab13
         // and not much more informative than /proc/PID/maps:
baab13
-        //copy_file(source_filename, dest_filename, 0640);
baab13
-        //chown(dest_filename, dd->dd_uid, dd->dd_gid);
baab13
+        //copy_file_ext(source_filename, dest_filename, 0640, dd->dd_uid, dd->dd_gid, O_RDONLY, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
baab13
 
baab13
         strcpy(source_filename + source_base_ofs, "maps");
baab13
         strcpy(dest_base, FILENAME_MAPS);
baab13
-        copy_file(source_filename, dest_filename, DEFAULT_DUMP_DIR_MODE);
baab13
-        IGNORE_RESULT(chown(dest_filename, dd->dd_uid, dd->dd_gid));
baab13
+        copy_file_ext(source_filename, dest_filename, 0640, dd->dd_uid, dd->dd_gid, O_RDONLY, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
baab13
 
baab13
         strcpy(source_filename + source_base_ofs, "limits");
baab13
         strcpy(dest_base, FILENAME_LIMITS);
baab13
-        copy_file(source_filename, dest_filename, DEFAULT_DUMP_DIR_MODE);
baab13
-        IGNORE_RESULT(chown(dest_filename, dd->dd_uid, dd->dd_gid));
baab13
+        copy_file_ext(source_filename, dest_filename, 0640, dd->dd_uid, dd->dd_gid, O_RDONLY, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
baab13
 
baab13
         strcpy(source_filename + source_base_ofs, "cgroup");
baab13
         strcpy(dest_base, FILENAME_CGROUP);
baab13
-        copy_file(source_filename, dest_filename, DEFAULT_DUMP_DIR_MODE);
baab13
-        IGNORE_RESULT(chown(dest_filename, dd->dd_uid, dd->dd_gid));
baab13
+        copy_file_ext(source_filename, dest_filename, 0640, dd->dd_uid, dd->dd_gid, O_RDONLY, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
baab13
 
baab13
         strcpy(dest_base, FILENAME_OPEN_FDS);
baab13
-        if (dump_fd_info(dest_filename, source_filename, source_base_ofs))
baab13
-            IGNORE_RESULT(chown(dest_filename, dd->dd_uid, dd->dd_gid));
baab13
+        dump_fd_info(dest_filename, source_filename, source_base_ofs, dd->dd_uid, dd->dd_gid);
baab13
 
baab13
         free(dest_filename);
baab13
 
baab13
-- 
baab13
1.8.3.1
baab13