Blame SOURCES/0019-curl-7.76.1-CVE-2022-32207.patch

372e18
From 36b47377c2d1a8d141d1ef810102748f27384f5c Mon Sep 17 00:00:00 2001
372e18
From: Daniel Stenberg <daniel@haxx.se>
372e18
Date: Wed, 25 May 2022 10:09:53 +0200
372e18
Subject: [PATCH 1/3] fopen: add Curl_fopen() for better overwriting of files
372e18
372e18
Bug: https://curl.se/docs/CVE-2022-32207.html
372e18
CVE-2022-32207
372e18
Reported-by: Harry Sintonen
372e18
Closes #9050
372e18
372e18
Upstream-commit: 20f9dd6bae50b7223171b17ba7798946e74f877f
372e18
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
372e18
---
372e18
 CMakeLists.txt          |   1 +
372e18
 configure.ac            |   1 +
372e18
 lib/Makefile.inc        |   2 +
372e18
 lib/cookie.c            |  17 ++----
372e18
 lib/curl_config.h.cmake |   3 ++
372e18
 lib/fopen.c             | 113 ++++++++++++++++++++++++++++++++++++++++
372e18
 lib/fopen.h             |  30 +++++++++++
372e18
 7 files changed, 154 insertions(+), 13 deletions(-)
372e18
 create mode 100644 lib/fopen.c
372e18
 create mode 100644 lib/fopen.h
372e18
372e18
diff --git a/CMakeLists.txt b/CMakeLists.txt
372e18
index b77de6d..a0bfaad 100644
372e18
--- a/CMakeLists.txt
372e18
+++ b/CMakeLists.txt
372e18
@@ -982,6 +982,7 @@ elseif(HAVE_LIBSOCKET)
372e18
   set(CMAKE_REQUIRED_LIBRARIES socket)
372e18
 endif()
372e18
 
372e18
+check_symbol_exists(fchmod        "${CURL_INCLUDES}" HAVE_FCHMOD)
372e18
 check_symbol_exists(basename      "${CURL_INCLUDES}" HAVE_BASENAME)
372e18
 check_symbol_exists(socket        "${CURL_INCLUDES}" HAVE_SOCKET)
372e18
 check_symbol_exists(select        "${CURL_INCLUDES}" HAVE_SELECT)
372e18
diff --git a/configure.ac b/configure.ac
372e18
index d431870..7433bb9 100644
372e18
--- a/configure.ac
372e18
+++ b/configure.ac
372e18
@@ -4516,6 +4516,7 @@ AC_CHECK_DECLS([getpwuid_r], [], [AC_DEFINE(HAVE_DECL_GETPWUID_R_MISSING, 1, "Se
372e18
 
372e18
 
372e18
 AC_CHECK_FUNCS([fnmatch \
372e18
+  fchmod \
372e18
   geteuid \
372e18
   getpass_r \
372e18
   getppid \
372e18
diff --git a/lib/Makefile.inc b/lib/Makefile.inc
372e18
index e8f110f..5139b03 100644
372e18
--- a/lib/Makefile.inc
372e18
+++ b/lib/Makefile.inc
372e18
@@ -130,6 +130,7 @@ LIB_CFILES =         \
372e18
   escape.c           \
372e18
   file.c             \
372e18
   fileinfo.c         \
372e18
+  fopen.c            \
372e18
   formdata.c         \
372e18
   ftp.c              \
372e18
   ftplistparser.c    \
372e18
@@ -261,6 +262,7 @@ LIB_HFILES =         \
372e18
   escape.h           \
372e18
   file.h             \
372e18
   fileinfo.h         \
372e18
+  fopen.h            \
372e18
   formdata.h         \
372e18
   ftp.h              \
372e18
   ftplistparser.h    \
372e18
diff --git a/lib/cookie.c b/lib/cookie.c
372e18
index 8a6aa1a..cb0c03b 100644
372e18
--- a/lib/cookie.c
372e18
+++ b/lib/cookie.c
372e18
@@ -97,8 +97,8 @@ Example set of cookies:
372e18
 #include "curl_memrchr.h"
372e18
 #include "inet_pton.h"
372e18
 #include "parsedate.h"
372e18
-#include "rand.h"
372e18
 #include "rename.h"
372e18
+#include "fopen.h"
372e18
 
372e18
 /* The last 3 #include files should be in this order */
372e18
 #include "curl_printf.h"
372e18
@@ -1537,17 +1537,8 @@ static int cookie_output(struct Curl_easy *data,
372e18
     use_stdout = TRUE;
372e18
   }
372e18
   else {
372e18
-    unsigned char randsuffix[9];
372e18
-
372e18
-    if(Curl_rand_hex(data, randsuffix, sizeof(randsuffix)))
372e18
-      return 2;
372e18
-
372e18
-    tempstore = aprintf("%s.%s.tmp", filename, randsuffix);
372e18
-    if(!tempstore)
372e18
-      return 1;
372e18
-
372e18
-    out = fopen(tempstore, FOPEN_WRITETEXT);
372e18
-    if(!out)
372e18
+    error = Curl_fopen(data, filename, &out, &tempstore);
372e18
+    if(error)
372e18
       goto error;
372e18
   }
372e18
 
372e18
@@ -1594,7 +1585,7 @@ static int cookie_output(struct Curl_easy *data,
372e18
   if(!use_stdout) {
372e18
     fclose(out);
372e18
     out = NULL;
372e18
-    if(Curl_rename(tempstore, filename)) {
372e18
+    if(tempstore && Curl_rename(tempstore, filename)) {
372e18
       unlink(tempstore);
372e18
       goto error;
372e18
     }
372e18
diff --git a/lib/curl_config.h.cmake b/lib/curl_config.h.cmake
372e18
index d2a0f43..c254359 100644
372e18
--- a/lib/curl_config.h.cmake
372e18
+++ b/lib/curl_config.h.cmake
372e18
@@ -148,6 +148,9 @@
372e18
 /* Define to 1 if you have the <assert.h> header file. */
372e18
 #cmakedefine HAVE_ASSERT_H 1
372e18
 
372e18
+/* Define to 1 if you have the `fchmod' function. */
372e18
+#cmakedefine HAVE_FCHMOD 1
372e18
+
372e18
 /* Define to 1 if you have the `basename' function. */
372e18
 #cmakedefine HAVE_BASENAME 1
372e18
 
372e18
diff --git a/lib/fopen.c b/lib/fopen.c
372e18
new file mode 100644
372e18
index 0000000..ad3691b
372e18
--- /dev/null
372e18
+++ b/lib/fopen.c
372e18
@@ -0,0 +1,113 @@
372e18
+/***************************************************************************
372e18
+ *                                  _   _ ____  _
372e18
+ *  Project                     ___| | | |  _ \| |
372e18
+ *                             / __| | | | |_) | |
372e18
+ *                            | (__| |_| |  _ <| |___
372e18
+ *                             \___|\___/|_| \_\_____|
372e18
+ *
372e18
+ * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
372e18
+ *
372e18
+ * This software is licensed as described in the file COPYING, which
372e18
+ * you should have received as part of this distribution. The terms
372e18
+ * are also available at https://curl.se/docs/copyright.html.
372e18
+ *
372e18
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
372e18
+ * copies of the Software, and permit persons to whom the Software is
372e18
+ * furnished to do so, under the terms of the COPYING file.
372e18
+ *
372e18
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
372e18
+ * KIND, either express or implied.
372e18
+ *
372e18
+ * SPDX-License-Identifier: curl
372e18
+ *
372e18
+ ***************************************************************************/
372e18
+
372e18
+#include "curl_setup.h"
372e18
+
372e18
+#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) ||  \
372e18
+  defined(USE_HSTS)
372e18
+
372e18
+#ifdef HAVE_FCNTL_H
372e18
+#include <fcntl.h>
372e18
+#endif
372e18
+
372e18
+#include "urldata.h"
372e18
+#include "rand.h"
372e18
+#include "fopen.h"
372e18
+/* The last 3 #include files should be in this order */
372e18
+#include "curl_printf.h"
372e18
+#include "curl_memory.h"
372e18
+#include "memdebug.h"
372e18
+
372e18
+/*
372e18
+ * Curl_fopen() opens a file for writing with a temp name, to be renamed
372e18
+ * to the final name when completed. If there is an existing file using this
372e18
+ * name at the time of the open, this function will clone the mode from that
372e18
+ * file.  if 'tempname' is non-NULL, it needs a rename after the file is
372e18
+ * written.
372e18
+ */
372e18
+CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
372e18
+                    FILE **fh, char **tempname)
372e18
+{
372e18
+  CURLcode result = CURLE_WRITE_ERROR;
372e18
+  unsigned char randsuffix[9];
372e18
+  char *tempstore = NULL;
372e18
+  struct_stat sb;
372e18
+  int fd = -1;
372e18
+  *tempname = NULL;
372e18
+
372e18
+  if(stat(filename, &sb) == -1 || !S_ISREG(sb.st_mode)) {
372e18
+    /* a non-regular file, fallback to direct fopen() */
372e18
+    *fh = fopen(filename, FOPEN_WRITETEXT);
372e18
+    if(*fh)
372e18
+      return CURLE_OK;
372e18
+    goto fail;
372e18
+  }
372e18
+
372e18
+  result = Curl_rand_hex(data, randsuffix, sizeof(randsuffix));
372e18
+  if(result)
372e18
+    goto fail;
372e18
+
372e18
+  tempstore = aprintf("%s.%s.tmp", filename, randsuffix);
372e18
+  if(!tempstore) {
372e18
+    result = CURLE_OUT_OF_MEMORY;
372e18
+    goto fail;
372e18
+  }
372e18
+
372e18
+  result = CURLE_WRITE_ERROR;
372e18
+  fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, 0600);
372e18
+  if(fd == -1)
372e18
+    goto fail;
372e18
+
372e18
+#ifdef HAVE_FCHMOD
372e18
+  {
372e18
+    struct_stat nsb;
372e18
+    if((fstat(fd, &nsb) != -1) &&
372e18
+       (nsb.st_uid == sb.st_uid) && (nsb.st_gid == sb.st_gid)) {
372e18
+      /* if the user and group are the same, clone the original mode */
372e18
+      if(fchmod(fd, sb.st_mode) == -1)
372e18
+        goto fail;
372e18
+    }
372e18
+  }
372e18
+#endif
372e18
+
372e18
+  *fh = fdopen(fd, FOPEN_WRITETEXT);
372e18
+  if(!*fh)
372e18
+    goto fail;
372e18
+
372e18
+  *tempname = tempstore;
372e18
+  return CURLE_OK;
372e18
+
372e18
+fail:
372e18
+  if(fd != -1) {
372e18
+    close(fd);
372e18
+    unlink(tempstore);
372e18
+  }
372e18
+
372e18
+  free(tempstore);
372e18
+
372e18
+  *tempname = NULL;
372e18
+  return result;
372e18
+}
372e18
+
372e18
+#endif /* ! disabled */
372e18
diff --git a/lib/fopen.h b/lib/fopen.h
372e18
new file mode 100644
372e18
index 0000000..289e55f
372e18
--- /dev/null
372e18
+++ b/lib/fopen.h
372e18
@@ -0,0 +1,30 @@
372e18
+#ifndef HEADER_CURL_FOPEN_H
372e18
+#define HEADER_CURL_FOPEN_H
372e18
+/***************************************************************************
372e18
+ *                                  _   _ ____  _
372e18
+ *  Project                     ___| | | |  _ \| |
372e18
+ *                             / __| | | | |_) | |
372e18
+ *                            | (__| |_| |  _ <| |___
372e18
+ *                             \___|\___/|_| \_\_____|
372e18
+ *
372e18
+ * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
372e18
+ *
372e18
+ * This software is licensed as described in the file COPYING, which
372e18
+ * you should have received as part of this distribution. The terms
372e18
+ * are also available at https://curl.se/docs/copyright.html.
372e18
+ *
372e18
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
372e18
+ * copies of the Software, and permit persons to whom the Software is
372e18
+ * furnished to do so, under the terms of the COPYING file.
372e18
+ *
372e18
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
372e18
+ * KIND, either express or implied.
372e18
+ *
372e18
+ * SPDX-License-Identifier: curl
372e18
+ *
372e18
+ ***************************************************************************/
372e18
+
372e18
+CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
372e18
+                    FILE **fh, char **tempname);
372e18
+
372e18
+#endif
372e18
-- 
372e18
2.35.3
372e18
372e18
372e18
From bd7af48238b058e9b46fdf2e1333b355920c341c Mon Sep 17 00:00:00 2001
372e18
From: Daniel Stenberg <daniel@haxx.se>
372e18
Date: Wed, 25 May 2022 10:09:53 +0200
372e18
Subject: [PATCH 2/3] altsvc: use Curl_fopen()
372e18
372e18
Upstream-commit: fab970a5d19c1faa2052239ec1e2602b892cbeb2
372e18
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
372e18
---
372e18
 lib/altsvc.c | 22 ++++++----------------
372e18
 1 file changed, 6 insertions(+), 16 deletions(-)
372e18
372e18
diff --git a/lib/altsvc.c b/lib/altsvc.c
372e18
index 242733b..4dc4078 100644
372e18
--- a/lib/altsvc.c
372e18
+++ b/lib/altsvc.c
372e18
@@ -34,7 +34,7 @@
372e18
 #include "parsedate.h"
372e18
 #include "sendf.h"
372e18
 #include "warnless.h"
372e18
-#include "rand.h"
372e18
+#include "fopen.h"
372e18
 #include "rename.h"
372e18
 
372e18
 /* The last 3 #include files should be in this order */
372e18
@@ -329,8 +329,7 @@ CURLcode Curl_altsvc_save(struct Curl_easy *data,
372e18
   struct Curl_llist_element *n;
372e18
   CURLcode result = CURLE_OK;
372e18
   FILE *out;
372e18
-  char *tempstore;
372e18
-  unsigned char randsuffix[9];
372e18
+  char *tempstore = NULL;
372e18
 
372e18
   if(!altsvc)
372e18
     /* no cache activated */
372e18
@@ -344,17 +343,8 @@ CURLcode Curl_altsvc_save(struct Curl_easy *data,
372e18
     /* marked as read-only, no file or zero length file name */
372e18
     return CURLE_OK;
372e18
 
372e18
-  if(Curl_rand_hex(data, randsuffix, sizeof(randsuffix)))
372e18
-    return CURLE_FAILED_INIT;
372e18
-
372e18
-  tempstore = aprintf("%s.%s.tmp", file, randsuffix);
372e18
-  if(!tempstore)
372e18
-    return CURLE_OUT_OF_MEMORY;
372e18
-
372e18
-  out = fopen(tempstore, FOPEN_WRITETEXT);
372e18
-  if(!out)
372e18
-    result = CURLE_WRITE_ERROR;
372e18
-  else {
372e18
+  result = Curl_fopen(data, file, &out, &tempstore);
372e18
+  if(!result) {
372e18
     fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n"
372e18
           "# This file was generated by libcurl! Edit at your own risk.\n",
372e18
           out);
372e18
@@ -366,10 +356,10 @@ CURLcode Curl_altsvc_save(struct Curl_easy *data,
372e18
         break;
372e18
     }
372e18
     fclose(out);
372e18
-    if(!result && Curl_rename(tempstore, file))
372e18
+    if(!result && tempstore && Curl_rename(tempstore, file))
372e18
       result = CURLE_WRITE_ERROR;
372e18
 
372e18
-    if(result)
372e18
+    if(result && tempstore)
372e18
       unlink(tempstore);
372e18
   }
372e18
   free(tempstore);
372e18
-- 
372e18
2.35.3
372e18
372e18
372e18
From 2011622a36fa715f38277422241e77e25dfdf0d0 Mon Sep 17 00:00:00 2001
372e18
From: Daniel Stenberg <daniel@haxx.se>
372e18
Date: Wed, 25 May 2022 10:09:54 +0200
372e18
Subject: [PATCH 3/3] hsts: use Curl_fopen()
372e18
372e18
Upstream-commit: d64115d7bb8ae4c136b620912da523c063f1d2ee
372e18
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
372e18
---
372e18
 lib/hsts.c | 22 ++++++----------------
372e18
 1 file changed, 6 insertions(+), 16 deletions(-)
372e18
372e18
diff --git a/lib/hsts.c b/lib/hsts.c
372e18
index b9fa6f7..9d54c82 100644
372e18
--- a/lib/hsts.c
372e18
+++ b/lib/hsts.c
372e18
@@ -35,7 +35,7 @@
372e18
 #include "sendf.h"
372e18
 #include "strtoofft.h"
372e18
 #include "parsedate.h"
372e18
-#include "rand.h"
372e18
+#include "fopen.h"
372e18
 #include "rename.h"
372e18
 
372e18
 /* The last 3 #include files should be in this order */
372e18
@@ -316,8 +316,7 @@ CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h,
372e18
   struct Curl_llist_element *n;
372e18
   CURLcode result = CURLE_OK;
372e18
   FILE *out;
372e18
-  char *tempstore;
372e18
-  unsigned char randsuffix[9];
372e18
+  char *tempstore = NULL;
372e18
 
372e18
   if(!h)
372e18
     /* no cache activated */
372e18
@@ -331,17 +330,8 @@ CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h,
372e18
     /* marked as read-only, no file or zero length file name */
372e18
     goto skipsave;
372e18
 
372e18
-  if(Curl_rand_hex(data, randsuffix, sizeof(randsuffix)))
372e18
-    return CURLE_FAILED_INIT;
372e18
-
372e18
-  tempstore = aprintf("%s.%s.tmp", file, randsuffix);
372e18
-  if(!tempstore)
372e18
-    return CURLE_OUT_OF_MEMORY;
372e18
-
372e18
-  out = fopen(tempstore, FOPEN_WRITETEXT);
372e18
-  if(!out)
372e18
-    result = CURLE_WRITE_ERROR;
372e18
-  else {
372e18
+  result = Curl_fopen(data, file, &out, &tempstore);
372e18
+  if(!result) {
372e18
     fputs("# Your HSTS cache. https://curl.se/docs/hsts.html\n"
372e18
           "# This file was generated by libcurl! Edit at your own risk.\n",
372e18
           out);
372e18
@@ -353,10 +343,10 @@ CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h,
372e18
         break;
372e18
     }
372e18
     fclose(out);
372e18
-    if(!result && Curl_rename(tempstore, file))
372e18
+    if(!result && tempstore && Curl_rename(tempstore, file))
372e18
       result = CURLE_WRITE_ERROR;
372e18
 
372e18
-    if(result)
372e18
+    if(result && tempstore)
372e18
       unlink(tempstore);
372e18
   }
372e18
   free(tempstore);
372e18
-- 
372e18
2.35.3
372e18