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