Blame SOURCES/0182-lib-add-convenient-wrappers-for-ensuring-writable-di.patch

baab13
From 40826f4ed0b325961d23f1e5dda45215bdb120c0 Mon Sep 17 00:00:00 2001
baab13
From: Jakub Filak <jfilak@redhat.com>
baab13
Date: Wed, 30 Sep 2015 14:13:35 +0200
baab13
Subject: [PATCH] lib: add convenient wrappers for ensuring writable dir
baab13
baab13
Replace lchown with fchown and chmod with fchmod.
baab13
baab13
Related to CVE-2015-5287
baab13
Related: #1262252
baab13
baab13
Signed-off-by: Jakub Filak <jfilak@redhat.com>
baab13
---
baab13
 src/daemon/abrtd.c    | 19 -------------------
baab13
 src/include/libabrt.h |  6 ++++++
baab13
 src/lib/hooklib.c     | 46 ++++++++++++++++++++++++++++++++++++++++++++++
baab13
 3 files changed, 52 insertions(+), 19 deletions(-)
baab13
baab13
diff --git a/src/daemon/abrtd.c b/src/daemon/abrtd.c
baab13
index b8426dd..b1252d2 100644
baab13
--- a/src/daemon/abrtd.c
baab13
+++ b/src/daemon/abrtd.c
baab13
@@ -158,25 +158,6 @@ static gboolean handle_signal_cb(GIOChannel *gio, GIOCondition condition, gpoint
baab13
     return TRUE; /* "please don't remove this event" */
baab13
 }
baab13
 
baab13
-static void ensure_writable_dir(const char *dir, mode_t mode, const char *user)
baab13
-{
baab13
-    struct stat sb;
baab13
-
baab13
-    if (mkdir(dir, mode) != 0 && errno != EEXIST)
baab13
-        perror_msg_and_die("Can't create '%s'", dir);
baab13
-    if (stat(dir, &sb) != 0 || !S_ISDIR(sb.st_mode))
baab13
-        error_msg_and_die("'%s' is not a directory", dir);
baab13
-
baab13
-    struct passwd *pw = getpwnam(user);
baab13
-    if (!pw)
baab13
-        perror_msg_and_die("Can't find user '%s'", user);
baab13
-
baab13
-    if ((sb.st_uid != pw->pw_uid || sb.st_gid != pw->pw_gid) && lchown(dir, pw->pw_uid, pw->pw_gid) != 0)
baab13
-        perror_msg_and_die("Can't set owner %u:%u on '%s'", (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid, dir);
baab13
-    if ((sb.st_mode & 07777) != mode && chmod(dir, mode) != 0)
baab13
-        perror_msg_and_die("Can't set mode %o on '%s'", mode, dir);
baab13
-}
baab13
-
baab13
 static void sanitize_dump_dir_rights(void)
baab13
 {
baab13
     /* We can't allow everyone to create dumps: otherwise users can flood
baab13
diff --git a/src/include/libabrt.h b/src/include/libabrt.h
baab13
index 21ce440..2510a77 100644
baab13
--- a/src/include/libabrt.h
baab13
+++ b/src/include/libabrt.h
baab13
@@ -42,6 +42,12 @@ int low_free_space(unsigned setting_MaxCrashReportsSize, const char *dump_locati
baab13
 
baab13
 #define trim_problem_dirs abrt_trim_problem_dirs
baab13
 void trim_problem_dirs(const char *dirname, double cap_size, const char *exclude_path);
baab13
+#define ensure_writable_dir_id abrt_ensure_writable_dir_uid_git
baab13
+void ensure_writable_dir_uid_gid(const char *dir, mode_t mode, uid_t uid, gid_t gid);
baab13
+#define ensure_writable_dir abrt_ensure_writable_dir
baab13
+void ensure_writable_dir(const char *dir, mode_t mode, const char *user);
baab13
+#define ensure_writable_dir_group abrt_ensure_writable_dir_group
baab13
+void ensure_writable_dir_group(const char *dir, mode_t mode, const char *user, const char *group);
baab13
 #define run_unstrip_n abrt_run_unstrip_n
baab13
 char *run_unstrip_n(const char *dump_dir_name, unsigned timeout_sec);
baab13
 #define get_backtrace abrt_get_backtrace
baab13
diff --git a/src/lib/hooklib.c b/src/lib/hooklib.c
baab13
index 8e93663..160a011 100644
baab13
--- a/src/lib/hooklib.c
baab13
+++ b/src/lib/hooklib.c
baab13
@@ -428,6 +428,52 @@ char* problem_data_save(problem_data_t *pd)
baab13
     return problem_id;
baab13
 }
baab13
 
baab13
+void ensure_writable_dir_uid_gid(const char *dir, mode_t mode, uid_t uid, gid_t gid)
baab13
+{
baab13
+    struct stat sb;
baab13
+    int dir_fd;
baab13
+
baab13
+    if (mkdir(dir, mode) != 0 && errno != EEXIST)
baab13
+        perror_msg_and_die("Can't create '%s'", dir);
baab13
+
baab13
+    dir_fd = open(dir, O_DIRECTORY | O_NOFOLLOW);
baab13
+    if (dir_fd < 0)
baab13
+        perror_msg_and_die("Can't open directory '%s'", dir);
baab13
+
baab13
+    if (fstat(dir_fd, &sb) != 0)
baab13
+        perror_msg_and_die("Can't stat directory '%s'", dir);
baab13
+
baab13
+    if ((sb.st_uid != uid || sb.st_gid != gid) && fchown(dir_fd, uid, gid) != 0)
baab13
+        perror_msg_and_die("Can't set owner %u:%u on '%s'", (unsigned int)uid, (unsigned int)gid, dir);
baab13
+
baab13
+    if ((sb.st_mode & 07777) != mode && fchmod(dir_fd, mode) != 0)
baab13
+        perror_msg_and_die("Can't set mode %o on '%s'", mode, dir);
baab13
+
baab13
+    close(dir_fd);
baab13
+}
baab13
+
baab13
+void ensure_writable_dir(const char *dir, mode_t mode, const char *user)
baab13
+{
baab13
+    struct passwd *pw = getpwnam(user);
baab13
+    if (!pw)
baab13
+        perror_msg_and_die("Can't find user '%s'", user);
baab13
+
baab13
+    ensure_writable_dir_uid_gid(dir, mode, pw->pw_uid, pw->pw_gid);
baab13
+}
baab13
+
baab13
+void ensure_writable_dir_group(const char *dir, mode_t mode, const char *user, const char *group)
baab13
+{
baab13
+    struct passwd *pw = getpwnam(user);
baab13
+    if (!pw)
baab13
+        perror_msg_and_die("Can't find user '%s'", user);
baab13
+
baab13
+    struct group *gr = getgrnam(group);
baab13
+    if (!gr)
baab13
+        perror_msg_and_die("Can't find group '%s'", group);
baab13
+
baab13
+    ensure_writable_dir_uid_gid(dir, mode, pw->pw_uid, gr->gr_gid);
baab13
+}
baab13
+
baab13
 bool dir_is_in_dump_location(const char *dir_name)
baab13
 {
baab13
     unsigned len = strlen(g_settings_dump_location);
baab13
-- 
baab13
1.8.3.1
baab13