Blob Blame Raw
From d8a3bd0d464f5b75ac360f4ee4e3cc6927a09199 Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Wed, 30 Sep 2015 14:13:35 +0200
Subject: [PATCH] lib: add convenient wrappers for ensuring writable dir

Replace lchown with fchown and chmod with fchmod.

Related CVE-2015-5287

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 src/include/libabrt.h |  4 ++++
 src/lib/hooklib.c     | 41 ++++++++++++++++++++++++++++++++++-------
 2 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/src/include/libabrt.h b/src/include/libabrt.h
index 6f89959..b26dcc6 100644
--- a/src/include/libabrt.h
+++ b/src/include/libabrt.h
@@ -42,8 +42,12 @@ int low_free_space(unsigned setting_MaxCrashReportsSize, const char *dump_locati
 
 #define trim_problem_dirs abrt_trim_problem_dirs
 void trim_problem_dirs(const char *dirname, double cap_size, const char *exclude_path);
+#define ensure_writable_dir_id abrt_ensure_writable_dir_uid_git
+void ensure_writable_dir_uid_gid(const char *dir, mode_t mode, uid_t uid, gid_t gid);
 #define ensure_writable_dir abrt_ensure_writable_dir
 void ensure_writable_dir(const char *dir, mode_t mode, const char *user);
+#define ensure_writable_dir_group abrt_ensure_writable_dir_group
+void ensure_writable_dir_group(const char *dir, mode_t mode, const char *user, const char *group);
 #define run_unstrip_n abrt_run_unstrip_n
 char *run_unstrip_n(const char *dump_dir_name, unsigned timeout_sec);
 #define get_backtrace abrt_get_backtrace
diff --git a/src/lib/hooklib.c b/src/lib/hooklib.c
index 2b76eea..0daa144 100644
--- a/src/lib/hooklib.c
+++ b/src/lib/hooklib.c
@@ -476,23 +476,50 @@ int signal_is_fatal(int signal_no, const char **name)
     return signame != NULL;
 }
 
-void ensure_writable_dir(const char *dir, mode_t mode, const char *user)
+void ensure_writable_dir_uid_gid(const char *dir, mode_t mode, uid_t uid, gid_t gid)
 {
     struct stat sb;
+    int dir_fd;
 
     if (mkdir(dir, mode) != 0 && errno != EEXIST)
         perror_msg_and_die("Can't create '%s'", dir);
-    if (stat(dir, &sb) != 0 || !S_ISDIR(sb.st_mode))
-        error_msg_and_die("'%s' is not a directory", dir);
 
+    dir_fd = open(dir, O_DIRECTORY | O_NOFOLLOW);
+    if (dir_fd < 0)
+        perror_msg_and_die("Can't open directory '%s'", dir);
+
+    if (fstat(dir_fd, &sb) != 0)
+        perror_msg_and_die("Can't stat directory '%s'", dir);
+
+    if ((sb.st_uid != uid || sb.st_gid != gid) && fchown(dir_fd, uid, gid) != 0)
+        perror_msg_and_die("Can't set owner %u:%u on '%s'", (unsigned int)uid, (unsigned int)gid, dir);
+
+    if ((sb.st_mode & 07777) != mode && fchmod(dir_fd, mode) != 0)
+        perror_msg_and_die("Can't set mode %o on '%s'", mode, dir);
+
+    close(dir_fd);
+}
+
+void ensure_writable_dir(const char *dir, mode_t mode, const char *user)
+{
+    struct passwd *pw = getpwnam(user);
+    if (!pw)
+        perror_msg_and_die("Can't find user '%s'", user);
+
+    ensure_writable_dir_uid_gid(dir, mode, pw->pw_uid, pw->pw_gid);
+}
+
+void ensure_writable_dir_group(const char *dir, mode_t mode, const char *user, const char *group)
+{
     struct passwd *pw = getpwnam(user);
     if (!pw)
         perror_msg_and_die("Can't find user '%s'", user);
 
-    if ((sb.st_uid != pw->pw_uid || sb.st_gid != pw->pw_gid) && lchown(dir, pw->pw_uid, pw->pw_gid) != 0)
-        perror_msg_and_die("Can't set owner %u:%u on '%s'", (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid, dir);
-    if ((sb.st_mode & 07777) != mode && chmod(dir, mode) != 0)
-        perror_msg_and_die("Can't set mode %o on '%s'", mode, dir);
+    struct group *gr = getgrnam(group);
+    if (!gr)
+        perror_msg_and_die("Can't find group '%s'", group);
+
+    ensure_writable_dir_uid_gid(dir, mode, pw->pw_uid, gr->gr_gid);
 }
 
 bool dir_is_in_dump_location(const char *dir_name)
-- 
2.6.3