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