doczkal / rpms / abrt

Forked from rpms/abrt 4 years ago
Clone

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

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