[php-src] master: ext/session: Open session files with O_CLOEXEC (#24376)
| From: | Jorg Adam Sowa via GitHub | Date: | Fri, 16 Oct 2026 19:55:40 +0000 |
| Subject: | [php-src] master: ext/session: Open session files with O_CLOEXEC (#24376) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-142365@lists.php.net to get a copy of this message | ||
Author: Jorg Adam Sowa (jorgsowa)
Committer: GitHub (web-flow)
Pusher: jorgsowa
Date: 2026-10-16T21:55:36+02:00
Commit: https://github.com/php/php-src/commit/6e4082b2eba9d35073062dec96052272b46763ed
Raw diff: https://github.com/php/php-src/commit/6e4082b2eba9d35073062dec96052272b46763ed.diff
ext/session: Open session files with O_CLOEXEC (#24376)
Set close-on-exec atomically when opening the session file instead of
issuing a separate fcntl(F_SETFD) call afterwards. This saves one
syscall per request and closes the window in which the descriptor could
leak into a child process forked by another thread. The fcntl() fallback
is kept for platforms without O_CLOEXEC.
Changed paths:
M ext/session/mod_files.c
Diff:
diff --git a/ext/session/mod_files.c b/ext/session/mod_files.c
index 194e9433af24..2e2d798431d4 100644
--- a/ext/session/mod_files.c
+++ b/ext/session/mod_files.c
@@ -175,7 +175,11 @@ static void ps_files_open(ps_files *data, /* const */ zend_string *key)
data->last_key = zend_string_copy(key);
/* O_NOFOLLOW to prevent us from following evil symlinks */
- data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY | O_NOFOLLOW, data->filemode);
+ int flags = O_CREAT | O_RDWR | O_BINARY | O_NOFOLLOW;
+#ifdef O_CLOEXEC
+ flags |= O_CLOEXEC;
+#endif
+ data->fd = VCWD_OPEN_MODE(buf, flags, data->filemode);
if (data->fd != -1) {
#ifndef PHP_WIN32
@@ -198,7 +202,7 @@ static void ps_files_open(ps_files *data, /* const */ zend_string *key)
ret = flock(data->fd, LOCK_EX);
} while (ret == -1 && errno == EINTR);
-#ifdef F_SETFD
+#if defined(F_SETFD) && !defined(O_CLOEXEC)
# ifndef FD_CLOEXEC
# define FD_CLOEXEC 1
# endif