1From dce4683cbbe107a95f1f0d45fabc304acfb5d71a Mon Sep 17 00:00:00 2001 2From: Andreas Gruenbacher <agruen@gnu.org> 3Date: Mon, 15 Jul 2019 16:21:48 +0200 4Subject: Don't follow symlinks unless --follow-symlinks is given 5 6* src/inp.c (plan_a, plan_b), src/util.c (copy_to_fd, copy_file, 7append_to_file): Unless the --follow-symlinks option is given, open files with 8the O_NOFOLLOW flag to avoid following symlinks. So far, we were only doing 9that consistently for input files. 10* src/util.c (create_backup): When creating empty backup files, (re)create them 11with O_CREAT | O_EXCL to avoid following symlinks in that case as well. 12[Retrieved from: 13https://git.savannah.gnu.org/cgit/patch.git/commit/?id=dce4683cbbe107a95f1f0d45fabc304acfb5d71a] 14Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com> 15--- 16 src/inp.c | 12 ++++++++++-- 17 src/util.c | 14 +++++++++++--- 18 2 files changed, 21 insertions(+), 5 deletions(-) 19 20diff --git a/src/inp.c b/src/inp.c 21index 32d0919..22d7473 100644 22--- a/src/inp.c 23+++ b/src/inp.c 24@@ -238,8 +238,13 @@ plan_a (char const *filename) 25 { 26 if (S_ISREG (instat.st_mode)) 27 { 28- int ifd = safe_open (filename, O_RDONLY|binary_transput, 0); 29+ int flags = O_RDONLY | binary_transput; 30 size_t buffered = 0, n; 31+ int ifd; 32+ 33+ if (! follow_symlinks) 34+ flags |= O_NOFOLLOW; 35+ ifd = safe_open (filename, flags, 0); 36 if (ifd < 0) 37 pfatal ("can't open file %s", quotearg (filename)); 38 39@@ -340,6 +345,7 @@ plan_a (char const *filename) 40 static void 41 plan_b (char const *filename) 42 { 43+ int flags = O_RDONLY | binary_transput; 44 int ifd; 45 FILE *ifp; 46 int c; 47@@ -353,7 +359,9 @@ plan_b (char const *filename) 48 49 if (instat.st_size == 0) 50 filename = NULL_DEVICE; 51- if ((ifd = safe_open (filename, O_RDONLY | binary_transput, 0)) < 0 52+ if (! follow_symlinks) 53+ flags |= O_NOFOLLOW; 54+ if ((ifd = safe_open (filename, flags, 0)) < 0 55 || ! (ifp = fdopen (ifd, binary_transput ? "rb" : "r"))) 56 pfatal ("Can't open file %s", quotearg (filename)); 57 if (TMPINNAME_needs_removal) 58diff --git a/src/util.c b/src/util.c 59index 1cc08ba..fb38307 100644 60--- a/src/util.c 61+++ b/src/util.c 62@@ -388,7 +388,7 @@ create_backup (char const *to, const struct stat *to_st, bool leave_original) 63 64 try_makedirs_errno = ENOENT; 65 safe_unlink (bakname); 66- while ((fd = safe_open (bakname, O_CREAT | O_WRONLY | O_TRUNC, 0666)) < 0) 67+ while ((fd = safe_open (bakname, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC, 0666)) < 0) 68 { 69 if (errno != try_makedirs_errno) 70 pfatal ("Can't create file %s", quotearg (bakname)); 71@@ -579,10 +579,13 @@ create_file (char const *file, int open_flags, mode_t mode, 72 static void 73 copy_to_fd (const char *from, int tofd) 74 { 75+ int from_flags = O_RDONLY | O_BINARY; 76 int fromfd; 77 ssize_t i; 78 79- if ((fromfd = safe_open (from, O_RDONLY | O_BINARY, 0)) < 0) 80+ if (! follow_symlinks) 81+ from_flags |= O_NOFOLLOW; 82+ if ((fromfd = safe_open (from, from_flags, 0)) < 0) 83 pfatal ("Can't reopen file %s", quotearg (from)); 84 while ((i = read (fromfd, buf, bufsize)) != 0) 85 { 86@@ -625,6 +628,8 @@ copy_file (char const *from, char const *to, struct stat *tost, 87 else 88 { 89 assert (S_ISREG (mode)); 90+ if (! follow_symlinks) 91+ to_flags |= O_NOFOLLOW; 92 tofd = create_file (to, O_WRONLY | O_BINARY | to_flags, mode, 93 to_dir_known_to_exist); 94 copy_to_fd (from, tofd); 95@@ -640,9 +645,12 @@ copy_file (char const *from, char const *to, struct stat *tost, 96 void 97 append_to_file (char const *from, char const *to) 98 { 99+ int to_flags = O_WRONLY | O_APPEND | O_BINARY; 100 int tofd; 101 102- if ((tofd = safe_open (to, O_WRONLY | O_BINARY | O_APPEND, 0)) < 0) 103+ if (! follow_symlinks) 104+ to_flags |= O_NOFOLLOW; 105+ if ((tofd = safe_open (to, to_flags, 0)) < 0) 106 pfatal ("Can't reopen file %s", quotearg (to)); 107 copy_to_fd (from, tofd); 108 if (close (tofd) != 0) 109-- 110cgit v1.2.1 111 112