git: 8768b60de16a - main - lib: Fix calls that naively set F_SETFD.

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 17 Jul 2025 17:02:34 UTC
The branch main has been updated by markj:

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

commit 8768b60de16a3d72a8783ec1241a711a782a36a9
Author:     Ricardo Branco <rbranco@suse.de>
AuthorDate: 2025-07-14 20:10:38 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-07-17 17:00:32 +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
---
 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 df6709fbcb85..9393cbe28f85 100644
--- a/lib/libc/gen/fdopendir.c
+++ b/lib/libc/gen/fdopendir.c
@@ -48,8 +48,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 a0d7b71df782..49ec97eda39d 100644
--- a/lib/libc/stdio/fdopen.c
+++ b/lib/libc/stdio/fdopen.c
@@ -46,7 +46,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.
@@ -76,9 +76,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 f0732b6d6741..048fd30b3193 100644
--- a/lib/libc/stdio/freopen.c
+++ b/lib/libc/stdio/freopen.c
@@ -55,7 +55,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;
@@ -113,8 +113,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 0d85ed468284..786d5647d993 100644
--- a/lib/libfetch/common.c
+++ b/lib/libfetch/common.c
@@ -277,13 +277,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);