0005-Don-t-follow-symlinks-unless--follow-symlinks-is-given.patch 3.8 KB

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