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