Skip to content

Commit f72105b

Browse files
committed
ext/posix: value ranges check for posix_setrlimit and posix_setpgid
setpgid accepts values from 0 to "PID_MAX". for setrlimit the culprit is using zend_long to represent rlim_t but at least we accept -1 for RLIM_INFINITY, however rl_cur should not be greater than rl_max value. close GH-19281
1 parent c561f7d commit f72105b

File tree

6 files changed

+67
-11
lines changed

6 files changed

+67
-11
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ PHP NEWS
3737
- POSIX:
3838
. posix_kill and posix_setpgid throws a ValueError on invalid process_id.
3939
(David Carlier)
40+
. posix_setpgid throws a ValueError on invalid process_group_id,
41+
posix_setrlimit throws a ValueError on invalid soft_limit and hard_limit
42+
arguments. (David Carlier)
4043

4144
- Reflection:
4245
. Fixed bug GH-19187 (ReflectionNamedType::getName() prints nullable type when

UPGRADING

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,11 @@ PHP 8.5 UPGRADE NOTES
384384
last_error to EBADF and raises an E_WARNING message.
385385
. posix_kill throws a ValueError when the process_id argument is lower
386386
or greater than what supports the platform (signed integer or long
387-
range), posix_setpgid throws a ValueError when the process_id is
388-
lower than zero or greater than what supports the platform.
387+
range), posix_setpgid throws a ValueError when the process_id or
388+
the process_group_id is lower than zero or greater than
389+
what supports the platform.
390+
. posix_setrlimit throws a ValueError when the hard_limit of soft_limit
391+
argument are lower than -1 or if soft_limit is greater than hard_limit.
389392

390393
- Reflection:
391394
. The output of ReflectionClass::toString() for enums has changed to

ext/posix/posix.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ ZEND_GET_MODULE(posix)
126126
} \
127127
RETURN_TRUE;
128128

129-
#define PHP_POSIX_CHECK_PID(pid, lower, upper) \
129+
#define PHP_POSIX_CHECK_PID(pid, arg, lower, upper) \
130130
if (pid < lower || pid > upper) { \
131-
zend_argument_value_error(1, "must be between " ZEND_LONG_FMT " and " ZEND_LONG_FMT, lower, upper); \
131+
zend_argument_value_error(arg, "must be between " ZEND_LONG_FMT " and " ZEND_LONG_FMT, lower, upper); \
132132
RETURN_THROWS(); \
133133
}
134134

@@ -143,7 +143,7 @@ PHP_FUNCTION(posix_kill)
143143
Z_PARAM_LONG(sig)
144144
ZEND_PARSE_PARAMETERS_END();
145145

146-
PHP_POSIX_CHECK_PID(pid, POSIX_PID_MIN, POSIX_PID_MAX)
146+
PHP_POSIX_CHECK_PID(pid, 1, POSIX_PID_MIN, POSIX_PID_MAX)
147147

148148
if (kill(pid, sig) < 0) {
149149
POSIX_G(last_error) = errno;
@@ -307,7 +307,8 @@ PHP_FUNCTION(posix_setpgid)
307307
Z_PARAM_LONG(pgid)
308308
ZEND_PARSE_PARAMETERS_END();
309309

310-
PHP_POSIX_CHECK_PID(pid, 0, POSIX_PID_MAX)
310+
PHP_POSIX_CHECK_PID(pid, 1, 0, POSIX_PID_MAX)
311+
PHP_POSIX_CHECK_PID(pgid, 2, 0, POSIX_PID_MAX)
311312

312313
if (setpgid(pid, pgid) < 0) {
313314
POSIX_G(last_error) = errno;
@@ -347,6 +348,8 @@ PHP_FUNCTION(posix_getsid)
347348
Z_PARAM_LONG(val)
348349
ZEND_PARSE_PARAMETERS_END();
349350

351+
PHP_POSIX_CHECK_PID(val, 1, 0, POSIX_PID_MAX)
352+
350353
if ((val = getsid(val)) < 0) {
351354
POSIX_G(last_error) = errno;
352355
RETURN_FALSE;
@@ -1201,6 +1204,21 @@ PHP_FUNCTION(posix_setrlimit)
12011204
Z_PARAM_LONG(max)
12021205
ZEND_PARSE_PARAMETERS_END();
12031206

1207+
if (cur < -1) {
1208+
zend_argument_value_error(2, "must be greater or equal to -1");
1209+
RETURN_THROWS();
1210+
}
1211+
1212+
if (max < -1) {
1213+
zend_argument_value_error(3, "must be greater or equal to -1");
1214+
RETURN_THROWS();
1215+
}
1216+
1217+
if (max > -1 && cur > max) {
1218+
zend_argument_value_error(2, "must be lower or equal to " ZEND_LONG_FMT, max);
1219+
RETURN_THROWS();
1220+
}
1221+
12041222
rl.rlim_cur = cur;
12051223
rl.rlim_max = max;
12061224

ext/posix/tests/posix_getsid_error.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ PHP Testfest Berlin 2009-05-10
99
posix
1010
--FILE--
1111
<?php
12-
var_dump( posix_getsid(-1) );
12+
try {
13+
posix_getsid(-1);
14+
} catch (\ValueError $e) {
15+
echo $e->getMessage(), PHP_EOL;
16+
}
1317
?>
14-
--EXPECT--
15-
bool(false)
18+
--EXPECTF--
19+
posix_getsid(): Argument #1 ($process_id) must be between 0 and %d

ext/posix/tests/posix_setpgid_error.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ try {
1616
} catch (\ValueError $e) {
1717
echo $e->getMessage(), PHP_EOL;
1818
}
19+
try {
20+
posix_setpgid(1, PHP_INT_MAX);
21+
} catch (\ValueError $e) {
22+
echo $e->getMessage(), PHP_EOL;
23+
}
24+
try {
25+
posix_setpgid(1, -2);
26+
} catch (\ValueError $e) {
27+
echo $e->getMessage(), PHP_EOL;
28+
}
1929
?>
2030
--EXPECTF--
2131
posix_setpgid(): Argument #1 ($process_id) must be between 0 and %d
2232
posix_setpgid(): Argument #1 ($process_id) must be between 0 and %d
33+
posix_setpgid(): Argument #2 ($process_group_id) must be between 0 and %d
34+
posix_setpgid(): Argument #2 ($process_group_id) must be between 0 and %d

ext/posix/tests/posix_setrlimit.phpt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,25 @@ if (str_contains(PHP_OS, 'FreeBSD')) die('skip different behavior on FreeBSD');
1212
<?php
1313

1414
var_dump(posix_setrlimit(POSIX_RLIMIT_NOFILE, 128, 128));
15-
var_dump(posix_setrlimit(POSIX_RLIMIT_NOFILE, 129, 128));
15+
try {
16+
posix_setrlimit(POSIX_RLIMIT_NOFILE, 129, 128);
17+
} catch (\ValueError $e) {
18+
echo $e->getMessage(), PHP_EOL;
19+
}
20+
try {
21+
posix_setrlimit(POSIX_RLIMIT_NOFILE, -2, -1);
22+
} catch (\ValueError $e) {
23+
echo $e->getMessage(), PHP_EOL;
24+
}
25+
try {
26+
posix_setrlimit(POSIX_RLIMIT_NOFILE, -1, -2);
27+
} catch (\ValueError $e) {
28+
echo $e->getMessage(), PHP_EOL;
29+
}
1630

1731
?>
1832
--EXPECT--
1933
bool(true)
20-
bool(false)
34+
posix_setrlimit(): Argument #2 ($soft_limit) must be lower or equal to 128
35+
posix_setrlimit(): Argument #2 ($soft_limit) must be greater or equal to -1
36+
posix_setrlimit(): Argument #3 ($hard_limit) must be greater or equal to -1

0 commit comments

Comments
 (0)