Skip to content

Commit aa0f8bd

Browse files
author
Mikhail Galanin
committed
Set CLOEXEC on listened/accepted sockets in the FPM children
1 parent 4077dad commit aa0f8bd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

main/fastcgi.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,10 @@ int fcgi_accept_request(fcgi_request *req)
14231423
return -1;
14241424
}
14251425

1426+
if (0 > fcntl(req->fd, F_SETFD, fcntl(req->fd, F_GETFD) | FD_CLOEXEC)) {
1427+
fcgi_log(FCGI_WARNING, "failed to change attribute of error_log");
1428+
}
1429+
14261430
#ifdef _WIN32
14271431
break;
14281432
#else

sapi/fpm/fpm/fpm_children.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ struct fpm_child_s *fpm_child_find(pid_t pid) /* {{{ */
167167
}
168168
/* }}} */
169169

170+
static int fpm_child_cloexec(void)
171+
{
172+
/* If PHP code invokes pcntl_fork()/exec(), we don't want the external programm to inherit the descriptor.
173+
If the external process accidentally uses the socket it will likely break the communication */
174+
int attrs = fcntl(fpm_globals.listening_socket, F_GETFD);
175+
if (0 > attrs) {
176+
zlog(ZLOG_WARNING, "failed to get attributes of listening socket, errno: %d", errno);
177+
return -1;
178+
}
179+
180+
/* set CLOEXEC to prevent the descriptor leaking to child processes */
181+
if (0 > fcntl(fpm_globals.listening_socket, F_SETFD, attrs | FD_CLOEXEC)) {
182+
zlog(ZLOG_WARNING, "failed to change attribute of listening socket");
183+
return -1;
184+
}
185+
186+
return 0;
187+
}
188+
170189
static void fpm_child_init(struct fpm_worker_pool_s *wp) /* {{{ */
171190
{
172191
fpm_globals.max_requests = wp->config->pm_max_requests;
@@ -178,7 +197,8 @@ static void fpm_child_init(struct fpm_worker_pool_s *wp) /* {{{ */
178197
0 > fpm_unix_init_child(wp) ||
179198
0 > fpm_signals_init_child() ||
180199
0 > fpm_env_init_child(wp) ||
181-
0 > fpm_php_init_child(wp)) {
200+
0 > fpm_php_init_child(wp) ||
201+
0 > fpm_child_cloexec()) {
182202

183203
zlog(ZLOG_ERROR, "[pool %s] child failed to initialize", wp->config->name);
184204
exit(FPM_EXIT_SOFTWARE);

0 commit comments

Comments
 (0)