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

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