]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
fopen: create new file using old file's mode
authorDaniel Stenberg <daniel@haxx.se>
Thu, 23 Nov 2023 14:52:57 +0000 (15:52 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 23 Nov 2023 21:30:42 +0000 (22:30 +0100)
Because the function renames the temp file to the target name as a last
step, if the file was previously owned by a different user, not ORing
the old mode could otherwise end up creating a file that was no longer
readable by the original owner after save.

Reported-by: Loïc Yhuel
Fixes #12299
Closes #12395

CMakeLists.txt
configure.ac
lib/curl_config.h.cmake
lib/fopen.c

index a71b5b0b5d886d56885f96216cdd3e2f62b7b061..a54c2fff921e67183e9a70dd54e3a5ba3ba89d7a 100644 (file)
@@ -1185,7 +1185,6 @@ elseif(HAVE_LIBSOCKET)
   set(CMAKE_REQUIRED_LIBRARIES socket)
 endif()
 
-check_symbol_exists(fchmod        "${CURL_INCLUDES}" HAVE_FCHMOD)
 check_symbol_exists(fnmatch       "${CURL_INCLUDES};fnmatch.h" HAVE_FNMATCH)
 check_symbol_exists(basename      "${CURL_INCLUDES};string.h" HAVE_BASENAME)
 check_symbol_exists(socket        "${CURL_INCLUDES}" HAVE_SOCKET)
index a2c5165034e99804a3f6c58d2d0198e68531d345..d9b396376d20c09b9ed59952b91db893e09ab234 100644 (file)
@@ -3581,7 +3581,6 @@ AC_CHECK_DECLS([getpwuid_r], [], [AC_DEFINE(HAVE_DECL_GETPWUID_R_MISSING, 1, "Se
 AC_CHECK_FUNCS([\
   _fseeki64 \
   arc4random \
-  fchmod \
   fnmatch \
   fseeko \
   geteuid \
index 274c2c168215badf0e10e34413c12fcf99122ccb..339358ea3621d622558b386d757a58f73b089ae9 100644 (file)
 /* Define to 1 if you have _Atomic support. */
 #cmakedefine HAVE_ATOMIC 1
 
-/* Define to 1 if you have the `fchmod' function. */
-#cmakedefine HAVE_FCHMOD 1
-
 /* Define to 1 if you have the `fnmatch' function. */
 #cmakedefine HAVE_FNMATCH 1
 
index a73ac068ea3016f97576e9e527e07b0490a7b0e0..2e726cc952b6e69ef92b9e4f48d5fc1828a9e6eb 100644 (file)
@@ -129,22 +129,10 @@ CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
   }
 
   result = CURLE_WRITE_ERROR;
-  fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, 0600);
+  fd = open(tempstore, O_WRONLY | O_CREAT | O_EXCL, 0600|sb.st_mode);
   if(fd == -1)
     goto fail;
 
-#ifdef HAVE_FCHMOD
-  {
-    struct_stat nsb;
-    if((fstat(fd, &nsb) != -1) &&
-       (nsb.st_uid == sb.st_uid) && (nsb.st_gid == sb.st_gid)) {
-      /* if the user and group are the same, clone the original mode */
-      if(fchmod(fd, (mode_t)sb.st_mode) == -1)
-        goto fail;
-    }
-  }
-#endif
-
   *fh = fdopen(fd, FOPEN_WRITETEXT);
   if(!*fh)
     goto fail;