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

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