1
0

050-CVE-2019-13636.patch 3.7 KB

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