Skip to content

Commit e9402c6

Browse files
committed
ext/posix: posix_kill() process_id range check.
pid_t is, for the most part, represented by a signed int, by overflowing it, we end up being in the -1 case which affect all accessible processes.
1 parent 8b61c49 commit e9402c6

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

ext/posix/posix.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
# include <sys/sysmacros.h>
4646
#endif
4747

48+
#if (defined(__sun) && !defined(_LP64)) || defined(_AIX)
49+
#define POSIX_PID_MAX LONG_MAX
50+
#else
51+
#define POSIX_PID_MAX INT_MAX
52+
#endif
53+
4854
#include "posix_arginfo.h"
4955

5056
ZEND_DECLARE_MODULE_GLOBALS(posix)
@@ -129,6 +135,11 @@ PHP_FUNCTION(posix_kill)
129135
Z_PARAM_LONG(sig)
130136
ZEND_PARSE_PARAMETERS_END();
131137

138+
if (pid < -1 || pid > POSIX_PID_MAX) {
139+
zend_argument_value_error(1, "must be between -1 and " ZEND_LONG_FMT, POSIX_PID_MAX);
140+
RETURN_THROWS();
141+
}
142+
132143
if (kill(pid, sig) < 0) {
133144
POSIX_G(last_error) = errno;
134145
RETURN_FALSE;

ext/posix/tests/posix_kill_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $sig = 999;
1313
var_dump( posix_kill($pid, 999) );
1414

1515
echo "\n-- Testing posix_kill() function with negative pid --\n";
16-
$pid = -999;
16+
$pid = -1;
1717
$sig = 9;
1818
var_dump( posix_kill($pid, 999) );
1919

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
posix_kill() with large pid
3+
--EXTENSIONS--
4+
posix
5+
--SKIPIF--
6+
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
7+
--FILE--
8+
<?php
9+
// with pid overflow, it ends up being -1 which means all permissible processes are affected
10+
try {
11+
posix_kill(PHP_INT_MAX, SIGTERM);
12+
} catch (\ValueError $e) {
13+
echo $e->getMessage(), PHP_EOL;
14+
}
15+
16+
try {
17+
posix_kill(PHP_INT_MIN, SIGTERM);
18+
} catch (\ValueError $e) {
19+
echo $e->getMessage(), PHP_EOL;
20+
}
21+
?>
22+
--EXPECTF--
23+
posix_kill(): Argument #1 ($process_id) must be between -1 and %d
24+
posix_kill(): Argument #1 ($process_id) must be between -1 and %d

0 commit comments

Comments
 (0)