Blame SOURCES/0127-dump_dir-allow-creating-of-a-new-dir-w-o-chowning-it.patch

28bab8
From b81884dcf41a6ee84c9ef5633acd2193bee60005 Mon Sep 17 00:00:00 2001
28bab8
From: Jakub Filak <jfilak@redhat.com>
28bab8
Date: Wed, 15 Apr 2015 15:19:40 +0200
28bab8
Subject: [LIBREPORT PATCH] dump_dir: allow creating of a new dir w/o chowning
28bab8
 it
28bab8
28bab8
Split dd_create() in to dd_create_skeleton() creating the directory and
28bab8
intializing struct dd* and dd_reset_ownership() updating UID and GUI to
28bab8
the deemed values.
28bab8
28bab8
We need this because we have to avoid situations where root is using a
28bab8
directory owned by a regular user.
28bab8
28bab8
Related: #1211835
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
---
28bab8
 src/include/dump_dir.h |  2 ++
28bab8
 src/lib/dump_dir.c     | 39 ++++++++++++++++++++++++++++++++-------
28bab8
 2 files changed, 34 insertions(+), 7 deletions(-)
28bab8
28bab8
diff --git a/src/include/dump_dir.h b/src/include/dump_dir.h
28bab8
index 124511e..71cf66f 100644
28bab8
--- a/src/include/dump_dir.h
28bab8
+++ b/src/include/dump_dir.h
28bab8
@@ -60,6 +60,8 @@ struct dump_dir {
28bab8
 void dd_close(struct dump_dir *dd);
28bab8
 
28bab8
 struct dump_dir *dd_opendir(const char *dir, int flags);
28bab8
+struct dump_dir *dd_create_skeleton(const char *dir, uid_t uid, mode_t mode);
28bab8
+int dd_reset_ownership(struct dump_dir *dd);
28bab8
 /* Pass uid = (uid_t)-1L to disable chown'ing of newly created files
28bab8
  * (IOW: if you aren't running under root):
28bab8
  */
28bab8
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
28bab8
index 28439af..fabad0b 100644
28bab8
--- a/src/lib/dump_dir.c
28bab8
+++ b/src/lib/dump_dir.c
28bab8
@@ -455,7 +455,10 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
28bab8
     return dd;
28bab8
 }
28bab8
 
28bab8
-/* Create a fresh empty debug dump dir.
28bab8
+/* Create a fresh empty debug dump dir which is owned bu the calling user. If
28bab8
+ * you want to create the directory with meaningful ownership you should
28bab8
+ * consider using dd_create() function or you can modify the ownership
28bab8
+ * afterwards by calling dd_reset_ownership() function.
28bab8
  *
28bab8
  * ABRT owns dump dir:
28bab8
  *   We should not allow users to write new files or write into existing ones,
28bab8
@@ -511,7 +514,7 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
28bab8
  *     this runs under 0:0
28bab8
  *     - clients: setroubleshootd, abrt python
28bab8
  */
28bab8
-struct dump_dir *dd_create(const char *dir, uid_t uid, mode_t mode)
28bab8
+struct dump_dir *dd_create_skeleton(const char *dir, uid_t uid, mode_t mode)
28bab8
 {
28bab8
     /* a little trick to copy read bits from file mode to exec bit of dir mode*/
28bab8
     mode_t dir_mode = mode | ((mode & 0444) >> 2);
28bab8
@@ -601,13 +604,35 @@ struct dump_dir *dd_create(const char *dir, uid_t uid, mode_t mode)
28bab8
         else
28bab8
             error_msg("User %lu does not exist, using gid 0", (long)uid);
28bab8
 #endif
28bab8
+    }
28bab8
 
28bab8
-        if (lchown(dir, dd->dd_uid, dd->dd_gid) == -1)
28bab8
-        {
28bab8
-            perror_msg("Can't change '%s' ownership to %lu:%lu", dir,
28bab8
-                       (long)dd->dd_uid, (long)dd->dd_gid);
28bab8
-        }
28bab8
+    return dd;
28bab8
+}
28bab8
+
28bab8
+/* Resets ownership of the given directory to UID and GID according to values
28bab8
+ * in dd_create_skeleton().
28bab8
+ */
28bab8
+int dd_reset_ownership(struct dump_dir *dd)
28bab8
+{
28bab8
+    const int r =lchown(dd->dd_dirname, dd->dd_uid, dd->dd_gid);
28bab8
+    if (r < 0)
28bab8
+    {
28bab8
+        perror_msg("Can't change '%s' ownership to %lu:%lu", dd->dd_dirname,
28bab8
+                   (long)dd->dd_uid, (long)dd->dd_gid);
28bab8
     }
28bab8
+    return r;
28bab8
+}
28bab8
+
28bab8
+/* Calls dd_create_skeleton() and dd_reset_ownership().
28bab8
+ */
28bab8
+struct dump_dir *dd_create(const char *dir, uid_t uid, mode_t mode)
28bab8
+{
28bab8
+    struct dump_dir *dd = dd_create_skeleton(dir, uid, mode);
28bab8
+    if (dd == NULL)
28bab8
+        return NULL;
28bab8
+
28bab8
+    /* ignore results */
28bab8
+    dd_reset_ownership(dd);
28bab8
 
28bab8
     return dd;
28bab8
 }
28bab8
-- 
28bab8
1.8.3.1
28bab8