Blame 0068-lib-make-the-dump-proc-data-functions-more-robust.patch

3a204fb
From 688fb0e85551eb6f05e54c3df20ae325f4b88224 Mon Sep 17 00:00:00 2001
3a204fb
From: Jakub Filak <jfilak@redhat.com>
3a204fb
Date: Thu, 16 Apr 2015 11:17:20 +0200
3a204fb
Subject: [PATCH] lib: make the dump proc data functions more robust
3a204fb
3a204fb
dump_fd_info and dump_proc_diff are being called from processes running
3a204fb
under root permissions, so these functions must allow callers to
3a204fb
atomically created the destination file and update the ownership of that
3a204fb
file.
3a204fb
3a204fb
Related: #1211835
3a204fb
3a204fb
Signed-off-by: Jakub Filak <jfilak@redhat.com>
3a204fb
---
3a204fb
 src/include/internal_libreport.h |  2 ++
3a204fb
 src/lib/get_cmdline.c            | 29 ++++++++++++++++++++++++++---
3a204fb
 2 files changed, 28 insertions(+), 3 deletions(-)
3a204fb
3a204fb
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
3a204fb
index 11c18d9..99f2fe1 100644
3a204fb
--- a/src/include/internal_libreport.h
3a204fb
+++ b/src/include/internal_libreport.h
3a204fb
@@ -641,6 +641,8 @@ char* get_cwd(pid_t pid);
3a204fb
 char* get_rootdir(pid_t pid);
3a204fb
 #define get_fsuid libreport_get_fsuid
3a204fb
 int get_fsuid(const char *proc_pid_status);
3a204fb
+#define dump_fd_info_ext libreport_dump_fd_info_ext
3a204fb
+int dump_fd_info_ext(const char *dest_filename, const char *proc_pid_fd_path, uid_t uid, gid_t gid);
3a204fb
 #define dump_fd_info libreport_dump_fd_info
3a204fb
 int dump_fd_info(const char *dest_filename, const char *proc_pid_fd_path);
3a204fb
 
3a204fb
diff --git a/src/lib/get_cmdline.c b/src/lib/get_cmdline.c
3a204fb
index 2e362c5..c55de30 100644
3a204fb
--- a/src/lib/get_cmdline.c
3a204fb
+++ b/src/lib/get_cmdline.c
3a204fb
@@ -213,7 +213,7 @@ int get_fsuid(const char *proc_pid_status)
3a204fb
     return fs_uid;
3a204fb
 }
3a204fb
 
3a204fb
-int dump_fd_info(const char *dest_filename, const char *proc_pid_fd_path)
3a204fb
+int dump_fd_info_ext(const char *dest_filename, const char *proc_pid_fd_path, uid_t uid, gid_t gid)
3a204fb
 {
3a204fb
     DIR *proc_fd_dir = NULL;
3a204fb
     int proc_fdinfo_fd = -1;
3a204fb
@@ -237,7 +237,7 @@ int dump_fd_info(const char *dest_filename, const char *proc_pid_fd_path)
3a204fb
         goto dumpfd_cleanup;
3a204fb
     }
3a204fb
 
3a204fb
-    stream = fopen(dest_filename, "w");
3a204fb
+    stream = fopen(dest_filename, "wex");
3a204fb
     if (!stream)
3a204fb
     {
3a204fb
         r = -ENOMEM;
3a204fb
@@ -295,7 +295,25 @@ dumpfd_next_fd:
3a204fb
 
3a204fb
 dumpfd_cleanup:
3a204fb
     errno = 0;
3a204fb
-    fclose(stream);
3a204fb
+
3a204fb
+    if (stream != NULL)
3a204fb
+    {
3a204fb
+        if (uid != (uid_t)-1L)
3a204fb
+        {
3a204fb
+            const int stream_fd = fileno(stream);
3a204fb
+            r = fchown(stream_fd, uid, gid);
3a204fb
+            if (r < 0)
3a204fb
+            {
3a204fb
+                perror_msg("Can't change '%s' ownership to %lu:%lu", dest_filename, (long)uid, (long)gid);
3a204fb
+                fclose(stream);
3a204fb
+                unlink(dest_filename);
3a204fb
+                stream = NULL;
3a204fb
+            }
3a204fb
+        }
3a204fb
+
3a204fb
+        if (stream != NULL)
3a204fb
+            fclose(stream);
3a204fb
+    }
3a204fb
 
3a204fb
     if (r == 0 && errno != 0)
3a204fb
         r = -errno;
3a204fb
@@ -306,3 +324,8 @@ dumpfd_cleanup:
3a204fb
 
3a204fb
     return r;
3a204fb
 }
3a204fb
+
3a204fb
+int dump_fd_info(const char *dest_filename, const char *proc_pid_fd_path)
3a204fb
+{
3a204fb
+    return dump_fd_info_ext(dest_filename, proc_pid_fd_path, /*UID*/-1, /*GID*/-1);
3a204fb
+}
3a204fb
-- 
3a204fb
2.1.0
3a204fb