Blame SOURCES/0096-ccpp-do-not-use-value-of-proc-PID-cwd-for-chdir.patch

baab13
From 806bb07571b698d90169c3b73cb65cd09c900284 Mon Sep 17 00:00:00 2001
baab13
From: Jakub Filak <jfilak@redhat.com>
baab13
Date: Fri, 17 Apr 2015 14:40:20 +0200
baab13
Subject: [ABRT PATCH] ccpp: do not use value of /proc/PID/cwd for chdir
baab13
baab13
Avoid symlink resolutions.
baab13
baab13
This issue was discovered by Florian Weimer of Red Hat Product Security.
baab13
baab13
Signed-off-by: Jakub Filak <jfilak@redhat.com>
baab13
---
baab13
 src/hooks/abrt-hook-ccpp.c | 85 +++++++++++++++++++++++-----------------------
baab13
 1 file changed, 42 insertions(+), 43 deletions(-)
baab13
baab13
diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
baab13
index 82ff555..d600bb7 100644
baab13
--- a/src/hooks/abrt-hook-ccpp.c
baab13
+++ b/src/hooks/abrt-hook-ccpp.c
baab13
@@ -144,6 +144,7 @@ static off_t copyfd_sparse(int src_fd, int dst_fd1, int dst_fd2, off_t size2)
baab13
 /* Global data */
baab13
 
baab13
 static char *user_pwd;
baab13
+static DIR *proc_cwd;
baab13
 static char *proc_pid_status;
baab13
 static struct dump_dir *dd;
baab13
 static int user_core_fd = -1;
baab13
@@ -163,13 +164,6 @@ static int user_core_fd = -1;
baab13
  */
baab13
 static const char percent_specifiers[] = "%scpugteh";
baab13
 static char *core_basename = (char*) "core";
baab13
-/*
baab13
- * Used for error messages only.
baab13
- * It is either the same as core_basename if it is absolute,
baab13
- * or $PWD/core_basename.
baab13
- */
baab13
-static char *full_core_basename;
baab13
-
baab13
 
baab13
 static char* get_executable(pid_t pid, int *fd_p)
baab13
 {
baab13
@@ -198,6 +192,18 @@ static char* get_executable(pid_t pid, int *fd_p)
baab13
     return executable;
baab13
 }
baab13
 
baab13
+static DIR *open_cwd(pid_t pid)
baab13
+{
baab13
+    char buf[sizeof("/proc/%lu/cwd") + sizeof(long)*3];
baab13
+    sprintf(buf, "/proc/%lu/cwd", (long)pid);
baab13
+
baab13
+    DIR *cwd = opendir(buf);
baab13
+    if (cwd == NULL)
baab13
+        perror_msg("Can't open process's CWD for CompatCore");
baab13
+
baab13
+    return cwd;
baab13
+}
baab13
+
baab13
 static char* get_cwd(pid_t pid)
baab13
 {
baab13
     char buf[sizeof("/proc/%lu/cwd") + sizeof(long)*3];
baab13
@@ -268,13 +274,9 @@ static int dump_suid_policy()
baab13
 
baab13
 static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_values)
baab13
 {
baab13
-    errno = 0;
baab13
-    if (user_pwd == NULL
baab13
-     || chdir(user_pwd) != 0
baab13
-    ) {
baab13
-        perror_msg("Can't cd to '%s'", user_pwd);
baab13
+    proc_cwd = open_cwd(pid);
baab13
+    if (proc_cwd == NULL)
baab13
         return -1;
baab13
-    }
baab13
 
baab13
     struct passwd* pw = getpwuid(uid);
baab13
     gid_t gid = pw ? pw->pw_gid : uid;
baab13
@@ -337,15 +339,10 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
baab13
         }
baab13
     }
baab13
 
baab13
-    full_core_basename = core_basename;
baab13
-    if (core_basename[0] != '/')
baab13
+    if (g_need_nonrelative && core_basename[0] != '/')
baab13
     {
baab13
-        if (g_need_nonrelative)
baab13
-        {
baab13
-            error_msg("Current suid_dumpable policy prevents from saving core dumps according to relative core_pattern");
baab13
-            return -1;
baab13
-        }
baab13
-        core_basename = concat_path_file(user_pwd, core_basename);
baab13
+        error_msg("Current suid_dumpable policy prevents from saving core dumps according to relative core_pattern");
baab13
+        return -1;
baab13
     }
baab13
 
baab13
     /* Open (create) compat core file.
baab13
@@ -381,7 +378,7 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
baab13
     struct stat sb;
baab13
     errno = 0;
baab13
     /* Do not O_TRUNC: if later checks fail, we do not want to have file already modified here */
baab13
-    int user_core_fd = open(core_basename, O_WRONLY | O_CREAT | O_NOFOLLOW | g_user_core_flags, 0600); /* kernel makes 0600 too */
baab13
+    int user_core_fd = openat(dirfd(proc_cwd), core_basename, O_WRONLY | O_CREAT | O_NOFOLLOW | g_user_core_flags, 0600); /* kernel makes 0600 too */
baab13
     xsetegid(0);
baab13
     xseteuid(0);
baab13
     if (user_core_fd < 0
baab13
@@ -391,15 +388,15 @@ static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_valu
baab13
      || sb.st_uid != fsuid
baab13
     ) {
baab13
         if (user_core_fd < 0)
baab13
-            perror_msg("Can't open '%s'", full_core_basename);
baab13
+            perror_msg("Can't open '%s' at '%s'", core_basename, user_pwd);
baab13
         else
baab13
-            perror_msg("'%s' is not a regular file with link count 1 owned by UID(%d)", full_core_basename, fsuid);
baab13
+            perror_msg("'%s' at '%s' is not a regular file with link count 1 owned by UID(%d)", core_basename, user_pwd, fsuid);
baab13
         return -1;
baab13
     }
baab13
     if (ftruncate(user_core_fd, 0) != 0) {
baab13
         /* perror first, otherwise unlink may trash errno */
baab13
-        perror_msg("Can't truncate '%s' to size 0", full_core_basename);
baab13
-        unlink(core_basename);
baab13
+        perror_msg("Can't truncate '%s' at '%s' to size 0", core_basename, user_pwd);
baab13
+        unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
baab13
         return -1;
baab13
     }
baab13
 
baab13
@@ -466,10 +463,8 @@ static int create_or_die(const char *filename)
baab13
     if (dd)
baab13
         dd_delete(dd);
baab13
     if (user_core_fd >= 0)
baab13
-    {
baab13
-        xchdir(user_pwd);
baab13
-        unlink(core_basename);
baab13
-    }
baab13
+        unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
baab13
+
baab13
     errno = sv_errno;
baab13
     perror_msg_and_die("Can't open '%s'", filename);
baab13
 }
baab13
@@ -573,7 +568,7 @@ int main(int argc, char** argv)
baab13
                         (long)pid, executable);
baab13
     }
baab13
 
baab13
-    user_pwd = get_cwd(pid); /* may be NULL on error */
baab13
+    user_pwd = get_cwd(pid);
baab13
     log_notice("user_pwd:'%s'", user_pwd);
baab13
 
baab13
     sprintf(path, "/proc/%lu/status", (long)pid);
baab13
@@ -672,6 +667,8 @@ int main(int argc, char** argv)
baab13
             error_msg_and_die("Error saving '%s'", path);
baab13
         }
baab13
         log("Saved core dump of pid %lu (%s) to %s (%llu bytes)", (long)pid, executable, path, (long long)core_size);
baab13
+        if (proc_cwd != NULL)
baab13
+            closedir(proc_cwd);
baab13
         return 0;
baab13
     }
baab13
 
baab13
@@ -791,10 +788,7 @@ int main(int argc, char** argv)
baab13
             unlink(path);
baab13
             dd_delete(dd);
baab13
             if (user_core_fd >= 0)
baab13
-            {
baab13
-                xchdir(user_pwd);
baab13
-                unlink(core_basename);
baab13
-            }
baab13
+                unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
baab13
             /* copyfd_sparse logs the error including errno string,
baab13
              * but it does not log file name */
baab13
             error_msg_and_die("Error writing '%s'", path);
baab13
@@ -807,8 +801,7 @@ int main(int argc, char** argv)
baab13
             )
baab13
         ) {
baab13
             /* nuke it (silently) */
baab13
-            xchdir(user_pwd);
baab13
-            unlink(core_basename);
baab13
+            unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
baab13
         }
baab13
 
baab13
 /* Because of #1211835 and #1126850 */
baab13
@@ -879,6 +872,8 @@ int main(int argc, char** argv)
baab13
         }
baab13
 
baab13
         free(rootdir);
baab13
+        if (proc_cwd != NULL)
baab13
+            closedir(proc_cwd);
baab13
         return 0;
baab13
     }
baab13
 
baab13
@@ -890,19 +885,23 @@ int main(int argc, char** argv)
baab13
         if (fsync(user_core_fd) != 0 || close(user_core_fd) != 0 || core_size < 0)
baab13
         {
baab13
             /* perror first, otherwise unlink may trash errno */
baab13
-            perror_msg("Error writing '%s'", full_core_basename);
baab13
-            xchdir(user_pwd);
baab13
-            unlink(core_basename);
baab13
+            perror_msg("Error writing '%s' at '%s'", core_basename, user_pwd);
baab13
+            unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
baab13
+            if (proc_cwd != NULL)
baab13
+                closedir(proc_cwd);
baab13
             return 1;
baab13
         }
baab13
         if (ulimit_c == 0 || core_size > ulimit_c)
baab13
         {
baab13
-            xchdir(user_pwd);
baab13
-            unlink(core_basename);
baab13
+            unlinkat(dirfd(proc_cwd), core_basename, /*unlink file*/0);
baab13
+            if (proc_cwd != NULL)
baab13
+                closedir(proc_cwd);
baab13
             return 1;
baab13
         }
baab13
-        log("Saved core dump of pid %lu to %s (%llu bytes)", (long)pid, full_core_basename, (long long)core_size);
baab13
+        log("Saved core dump of pid %lu to %s at %s (%llu bytes)", (long)pid, core_basename, user_pwd, (long long)core_size);
baab13
     }
baab13
 
baab13
+    if (proc_cwd != NULL)
baab13
+        closedir(proc_cwd);
baab13
     return 0;
baab13
 }
baab13
-- 
baab13
1.8.3.1
baab13