Blame SOURCES/patch-2.7.6-CVE-2019-13636-symlinks.patch

784d9f
commit dce4683cbbe107a95f1f0d45fabc304acfb5d71a
784d9f
Author: Andreas Gruenbacher <agruen@gnu.org>
784d9f
Date:   Mon Jul 15 16:21:48 2019 +0200
784d9f
784d9f
    Don't follow symlinks unless --follow-symlinks is given
784d9f
    
784d9f
    * src/inp.c (plan_a, plan_b), src/util.c (copy_to_fd, copy_file,
784d9f
    append_to_file): Unless the --follow-symlinks option is given, open files with
784d9f
    the O_NOFOLLOW flag to avoid following symlinks.  So far, we were only doing
784d9f
    that consistently for input files.
784d9f
    * src/util.c (create_backup): When creating empty backup files, (re)create them
784d9f
    with O_CREAT | O_EXCL to avoid following symlinks in that case as well.
784d9f
784d9f
diff --git a/src/inp.c b/src/inp.c
784d9f
index 32d0919..22d7473 100644
784d9f
--- a/src/inp.c
784d9f
+++ b/src/inp.c
784d9f
@@ -238,8 +238,13 @@ plan_a (char const *filename)
784d9f
     {
784d9f
       if (S_ISREG (instat.st_mode))
784d9f
         {
784d9f
-	  int ifd = safe_open (filename, O_RDONLY|binary_transput, 0);
784d9f
+	  int flags = O_RDONLY | binary_transput;
784d9f
 	  size_t buffered = 0, n;
784d9f
+	  int ifd;
784d9f
+
784d9f
+	  if (! follow_symlinks)
784d9f
+	    flags |= O_NOFOLLOW;
784d9f
+	  ifd = safe_open (filename, flags, 0);
784d9f
 	  if (ifd < 0)
784d9f
 	    pfatal ("can't open file %s", quotearg (filename));
784d9f
 
784d9f
@@ -340,6 +345,7 @@ plan_a (char const *filename)
784d9f
 static void
784d9f
 plan_b (char const *filename)
784d9f
 {
784d9f
+  int flags = O_RDONLY | binary_transput;
784d9f
   int ifd;
784d9f
   FILE *ifp;
784d9f
   int c;
784d9f
@@ -353,7 +359,9 @@ plan_b (char const *filename)
784d9f
 
784d9f
   if (instat.st_size == 0)
784d9f
     filename = NULL_DEVICE;
784d9f
-  if ((ifd = safe_open (filename, O_RDONLY | binary_transput, 0)) < 0
784d9f
+  if (! follow_symlinks)
784d9f
+    flags |= O_NOFOLLOW;
784d9f
+  if ((ifd = safe_open (filename, flags, 0)) < 0
784d9f
       || ! (ifp = fdopen (ifd, binary_transput ? "rb" : "r")))
784d9f
     pfatal ("Can't open file %s", quotearg (filename));
784d9f
   if (TMPINNAME_needs_removal)
784d9f
diff --git a/src/util.c b/src/util.c
784d9f
index 1cc08ba..fb38307 100644
784d9f
--- a/src/util.c
784d9f
+++ b/src/util.c
784d9f
@@ -388,7 +388,7 @@ create_backup (char const *to, const struct stat *to_st, bool leave_original)
784d9f
 
784d9f
 	  try_makedirs_errno = ENOENT;
784d9f
 	  safe_unlink (bakname);
784d9f
-	  while ((fd = safe_open (bakname, O_CREAT | O_WRONLY | O_TRUNC, 0666)) < 0)
784d9f
+	  while ((fd = safe_open (bakname, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC, 0666)) < 0)
784d9f
 	    {
784d9f
 	      if (errno != try_makedirs_errno)
784d9f
 		pfatal ("Can't create file %s", quotearg (bakname));
784d9f
@@ -579,10 +579,13 @@ create_file (char const *file, int open_flags, mode_t mode,
784d9f
 static void
784d9f
 copy_to_fd (const char *from, int tofd)
784d9f
 {
784d9f
+  int from_flags = O_RDONLY | O_BINARY;
784d9f
   int fromfd;
784d9f
   ssize_t i;
784d9f
 
784d9f
-  if ((fromfd = safe_open (from, O_RDONLY | O_BINARY, 0)) < 0)
784d9f
+  if (! follow_symlinks)
784d9f
+    from_flags |= O_NOFOLLOW;
784d9f
+  if ((fromfd = safe_open (from, from_flags, 0)) < 0)
784d9f
     pfatal ("Can't reopen file %s", quotearg (from));
784d9f
   while ((i = read (fromfd, buf, bufsize)) != 0)
784d9f
     {
784d9f
@@ -625,6 +628,8 @@ copy_file (char const *from, char const *to, struct stat *tost,
784d9f
   else
784d9f
     {
784d9f
       assert (S_ISREG (mode));
784d9f
+      if (! follow_symlinks)
784d9f
+	to_flags |= O_NOFOLLOW;
784d9f
       tofd = create_file (to, O_WRONLY | O_BINARY | to_flags, mode,
784d9f
 			  to_dir_known_to_exist);
784d9f
       copy_to_fd (from, tofd);
784d9f
@@ -640,9 +645,12 @@ copy_file (char const *from, char const *to, struct stat *tost,
784d9f
 void
784d9f
 append_to_file (char const *from, char const *to)
784d9f
 {
784d9f
+  int to_flags = O_WRONLY | O_APPEND | O_BINARY;
784d9f
   int tofd;
784d9f
 
784d9f
-  if ((tofd = safe_open (to, O_WRONLY | O_BINARY | O_APPEND, 0)) < 0)
784d9f
+  if (! follow_symlinks)
784d9f
+    to_flags |= O_NOFOLLOW;
784d9f
+  if ((tofd = safe_open (to, to_flags, 0)) < 0)
784d9f
     pfatal ("Can't reopen file %s", quotearg (to));
784d9f
   copy_to_fd (from, tofd);
784d9f
   if (close (tofd) != 0)