]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cf-socket: set FD_CLOEXEC on all sockets opened
authorStefan Eissing <stefan@eissing.org>
Thu, 9 Oct 2025 10:19:49 +0000 (12:19 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Fri, 10 Oct 2025 21:44:43 +0000 (23:44 +0200)
Removed TODO item

Reported-by: Joshua Rogers
Closes #18968

docs/TODO
lib/cf-socket.c

index 65d735d1be8c8463843ada651af3b66ae6d1c1a5..6c3ba904e721fdbf2607f5b6ed2d906ccc3c6f2e 100644 (file)
--- a/docs/TODO
+++ b/docs/TODO
@@ -34,7 +34,6 @@
  1.20 SRV and URI DNS records
  1.22 CURLINFO_PAUSE_STATE
  1.25 Expose tried IP addresses that failed
- 1.28 FD_CLOEXEC
  1.30 config file parsing
  1.31 erase secrets from heap/stack after use
  1.32 add asynch getaddrinfo support
 
  https://github.com/curl/curl/issues/2126
 
-1.28 FD_CLOEXEC
-
- It sets the close-on-exec flag for the file descriptor, which causes the file
- descriptor to be automatically (and atomically) closed when any of the
- exec-family functions succeed. Should probably be set by default?
-
- https://github.com/curl/curl/issues/2252
-
 1.30 config file parsing
 
  Consider providing an API, possibly in a separate companion library, for
index 0182de67e80a36cb972dac95314931d79767bd85..758641e40dbe3c889bdaab9bdec3c9cb8405ac10 100644 (file)
@@ -371,6 +371,17 @@ static CURLcode socket_open(struct Curl_easy *data,
     /* no socket, no connection */
     return CURLE_COULDNT_CONNECT;
 
+#ifdef HAVE_FCNTL
+  if(fcntl(*sockfd, F_SETFD, FD_CLOEXEC) < 0) {
+    char errbuf[STRERROR_LEN];
+    failf(data, "fcntl set CLOEXEC: %s",
+          curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf)));
+    close(*sockfd);
+    *sockfd = CURL_SOCKET_BAD;
+    return CURLE_COULDNT_CONNECT;
+  }
+#endif
+
 #if defined(USE_IPV6) && defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
   if(data->conn->scope_id && (addr->family == AF_INET6)) {
     struct sockaddr_in6 * const sa6 = (void *)&addr->curl_sa_addr;
@@ -2122,11 +2133,16 @@ static CURLcode cf_tcp_accept_connect(struct Curl_cfilter *cf,
           curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf)));
     return CURLE_FTP_ACCEPT_FAILED;
   }
-
-  infof(data, "Connection accepted from server");
-#ifndef HAVE_ACCEPT4
-  (void)curlx_nonblock(s_accepted, TRUE); /* enable non-blocking */
+#if !defined(HAVE_ACCEPT4) && defined(HAVE_FCNTL)
+  if((fcntl(s_accepted, F_SETFD, FD_CLOEXEC) < 0) ||
+     (curlx_nonblock(s_accepted, TRUE) < 0)) {
+    failf(data, "fcntl set CLOEXEC/NONBLOCK: %s",
+          curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf)));
+    return CURLE_FTP_ACCEPT_FAILED;
+  }
 #endif
+  infof(data, "Connection accepted from server");
+
   /* Replace any filter on SECONDARY with one listening on this socket */
   ctx->listening = FALSE;
   ctx->accepted = TRUE;