Skip to content

Commit 360b165

Browse files
author
Mikhail Galanin
committed
Set CLOEXEC on listened sockets when forking FPM children
1 parent 72e2e25 commit 360b165

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

sapi/fpm/fpm/fpm_children.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ 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+
/* }}} */
189+
170190
static void fpm_child_init(struct fpm_worker_pool_s *wp) /* {{{ */
171191
{
172192
fpm_globals.max_requests = wp->config->pm_max_requests;
@@ -178,7 +198,8 @@ static void fpm_child_init(struct fpm_worker_pool_s *wp) /* {{{ */
178198
0 > fpm_unix_init_child(wp) ||
179199
0 > fpm_signals_init_child() ||
180200
0 > fpm_env_init_child(wp) ||
181-
0 > fpm_php_init_child(wp)) {
201+
0 > fpm_php_init_child(wp) ||
202+
0 > fpm_child_cloexec()) {
182203

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

0 commit comments

Comments
 (0)