git: a58eb4d233bf - stable/14 - lib: Fix calls that naively set F_SETFD.

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 25 Aug 2025 15:22:49 UTC
The branch stable/14 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=a58eb4d233bfdddff243faec071f8dc18d3e5492

commit a58eb4d233bfdddff243faec071f8dc18d3e5492
Author:     Ricardo Branco <rbranco@suse.de>
AuthorDate: 2025-07-14 20:10:38 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-08-25 13:57:56 +0000

    lib: Fix calls that naively set F_SETFD.
    
    With the recent inclusion of the FD_CLOFORK and FD_RESOLVE_BENEATH flags,
    we must avoid clearing them when setting only FD_CLOEXEC.
    
    Signed-off-by: Ricardo Branco <rbranco@suse.de>
    
    Reviewed by:    kib, markj
    MFC after:      1 month
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1766
    
    (cherry picked from commit 8768b60de16a3d72a8783ec1241a711a782a36a9)
---
 lib/libc/gen/fdopendir.c | 10 +++++++++-
 lib/libc/stdio/fdopen.c  | 18 ++++++++++++++----
 lib/libc/stdio/freopen.c | 10 +++++++---
 lib/libfetch/common.c    |  7 +++++--
 4 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/lib/libc/gen/fdopendir.c b/lib/libc/gen/fdopendir.c
index 67c0766b6d83..85c4451f9b48 100644
--- a/lib/libc/gen/fdopendir.c
+++ b/lib/libc/gen/fdopendir.c
@@ -49,8 +49,16 @@
 DIR *
 fdopendir(int fd)
 {
+	int flags, rc;
 
-	if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
+	flags = _fcntl(fd, F_GETFD, 0);
+	if (flags == -1)
 		return (NULL);
+
+	if ((flags & FD_CLOEXEC) == 0) {
+		rc = _fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+		if (rc == -1)
+			return (NULL);
+	}
 	return (__opendir_common(fd, DTF_HIDEW | DTF_NODUP, true));
 }
diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c
index a4f2ac5b699b..59e16c74d51d 100644
--- a/lib/libc/stdio/fdopen.c
+++ b/lib/libc/stdio/fdopen.c
@@ -49,7 +49,7 @@ FILE *
 fdopen(int fd, const char *mode)
 {
 	FILE *fp;
-	int flags, oflags, fdflags, tmp;
+	int flags, oflags, fdflags, rc, tmp;
 
 	/*
 	 * File descriptors are a full int, but _file is only a short.
@@ -79,9 +79,19 @@ fdopen(int fd, const char *mode)
 	if ((fp = __sfp()) == NULL)
 		return (NULL);
 
-	if ((oflags & O_CLOEXEC) && _fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
-		fp->_flags = 0;
-		return (NULL);
+	if ((oflags & O_CLOEXEC) != 0) {
+		tmp = _fcntl(fd, F_GETFD, 0);
+		if (tmp == -1) {
+			fp->_flags = 0;
+			return (NULL);
+		}
+		if ((tmp & FD_CLOEXEC) == 0) {
+			rc = _fcntl(fd, F_SETFD, tmp | FD_CLOEXEC);
+			if (rc == -1) {
+				fp->_flags = 0;
+				return (NULL);
+			}
+		}
 	}
 
 	fp->_flags = flags;
diff --git a/lib/libc/stdio/freopen.c b/lib/libc/stdio/freopen.c
index f987d273d116..4d9bd3bd785d 100644
--- a/lib/libc/stdio/freopen.c
+++ b/lib/libc/stdio/freopen.c
@@ -58,7 +58,7 @@ freopen(const char * __restrict file, const char * __restrict mode,
     FILE * __restrict fp)
 {
 	int f;
-	int dflags, flags, isopen, oflags, sverrno, wantfd;
+	int dflags, fdflags, flags, isopen, oflags, sverrno, wantfd;
 
 	if ((flags = __sflags(mode, &oflags)) == 0) {
 		sverrno = errno;
@@ -116,8 +116,12 @@ freopen(const char * __restrict file, const char * __restrict mode,
 			(void) ftruncate(fp->_file, (off_t)0);
 		if (!(oflags & O_APPEND))
 			(void) _sseek(fp, (fpos_t)0, SEEK_SET);
-		if (oflags & O_CLOEXEC)
-			(void) _fcntl(fp->_file, F_SETFD, FD_CLOEXEC);
+		if ((oflags & O_CLOEXEC) != 0) {
+			fdflags = _fcntl(fp->_file, F_GETFD, 0);
+			if (fdflags != -1 && (fdflags & FD_CLOEXEC) == 0)
+				(void) _fcntl(fp->_file, F_SETFD,
+				    fdflags | FD_CLOEXEC);
+		}
 		f = fp->_file;
 		isopen = 0;
 		wantfd = -1;
diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c
index dfa742577585..22779876ded4 100644
--- a/lib/libfetch/common.c
+++ b/lib/libfetch/common.c
@@ -278,13 +278,16 @@ conn_t *
 fetch_reopen(int sd)
 {
 	conn_t *conn;
+	int flags;
 	int opt = 1;
 
 	/* allocate and fill connection structure */
 	if ((conn = calloc(1, sizeof(*conn))) == NULL)
 		return (NULL);
-	fcntl(sd, F_SETFD, FD_CLOEXEC);
-	setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof opt);
+	flags = fcntl(sd, F_GETFD);
+	if (flags != -1 && (flags & FD_CLOEXEC) == 0)
+		(void)fcntl(sd, F_SETFD, flags | FD_CLOEXEC);
+	(void)setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
 	conn->sd = sd;
 	++conn->ref;
 	return (conn);