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