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

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