Skip to content

Commit 4aba136

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

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-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);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
--TEST--
2+
FPM: Set CLOEXEC on the listen socket
3+
--SKIPIF--
4+
<?php include "skipif.inc"; ?>
5+
<?php
6+
if (!in_array(PHP_OS_FAMILY, ['Linux', 'Darwin'], true)) {
7+
die("skip: only can work on Linux or macOS\n");
8+
}
9+
?>
10+
--FILE--
11+
<?php
12+
13+
require_once "tester.inc";
14+
15+
$cfg = <<<'EOT'
16+
[global]
17+
error_log = {{FILE:LOG}}
18+
[unconfined]
19+
listen = {{ADDR}}
20+
pm = static
21+
pm.max_children = 1
22+
pm.status_listen = {{ADDR[status]}}
23+
pm.status_path = /status
24+
env[PATH] = $PATH
25+
EOT;
26+
27+
28+
$code = <<<'EOT'
29+
<?php
30+
31+
echo "My sockets (expect to see 2 of them):\n";
32+
33+
$mypid = getmypid();
34+
$ph = popen("lsof -Pn -p$mypid 2>&1 | grep TCP", 'r');
35+
echo stream_get_contents($ph);
36+
pclose($ph);
37+
38+
echo "\n\n";
39+
40+
echo "Sockets after exec(), expected to be empty:\n";
41+
$ph = popen("lsof -Pn -p\$\$ 2>&1 | grep TCP", 'r');
42+
var_dump(stream_get_contents($ph));
43+
pclose($ph);
44+
45+
EOT;
46+
47+
$tester = new FPM\Tester($cfg, $code);
48+
$tester->start();
49+
$tester->expectLogStartNotices();
50+
$tester->request()->printBody();
51+
usleep(100000);
52+
//$tester->status($expectedStatusData, '{{ADDR[status]}}');
53+
$tester->terminate();
54+
$tester->expectLogTerminatingNotices();
55+
$tester->close();
56+
57+
?>
58+
Done
59+
--EXPECTF--
60+
My sockets (expect to see 2 of them):
61+
%S
62+
%S
63+
64+
65+
Sockets after exec(), expected to be empty:
66+
string(0) ""
67+
Done
68+
--CLEAN--
69+
<?php
70+
require_once "tester.inc";
71+
FPM\Tester::clean();
72+
?>

0 commit comments

Comments
 (0)