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
This commit is contained in:
David Carlier 2025-07-28 19:57:21 +01:00
parent c561f7da85
commit f72105be81
No known key found for this signature in database
GPG key ID: 8486F847B4B94EF1
6 changed files with 67 additions and 11 deletions

3
NEWS
View file

@ -37,6 +37,9 @@ PHP NEWS
- POSIX:
. posix_kill and posix_setpgid throws a ValueError on invalid process_id.
(David Carlier)
. posix_setpgid throws a ValueError on invalid process_group_id,
posix_setrlimit throws a ValueError on invalid soft_limit and hard_limit
arguments. (David Carlier)
- Reflection:
. Fixed bug GH-19187 (ReflectionNamedType::getName() prints nullable type when

View file

@ -384,8 +384,11 @@ PHP 8.5 UPGRADE NOTES
last_error to EBADF and raises an E_WARNING message.
. posix_kill throws a ValueError when the process_id argument is lower
or greater than what supports the platform (signed integer or long
range), posix_setpgid throws a ValueError when the process_id is
lower than zero or greater than what supports the platform.
range), posix_setpgid throws a ValueError when the process_id or
the process_group_id is lower than zero or greater than
what supports the platform.
. posix_setrlimit throws a ValueError when the hard_limit of soft_limit
argument are lower than -1 or if soft_limit is greater than hard_limit.
- Reflection:
. The output of ReflectionClass::toString() for enums has changed to

View file

@ -126,9 +126,9 @@ ZEND_GET_MODULE(posix)
} \
RETURN_TRUE;
#define PHP_POSIX_CHECK_PID(pid, lower, upper) \
#define PHP_POSIX_CHECK_PID(pid, arg, lower, upper) \
if (pid < lower || pid > upper) { \
zend_argument_value_error(1, "must be between " ZEND_LONG_FMT " and " ZEND_LONG_FMT, lower, upper); \
zend_argument_value_error(arg, "must be between " ZEND_LONG_FMT " and " ZEND_LONG_FMT, lower, upper); \
RETURN_THROWS(); \
}
@ -143,7 +143,7 @@ PHP_FUNCTION(posix_kill)
Z_PARAM_LONG(sig)
ZEND_PARSE_PARAMETERS_END();
PHP_POSIX_CHECK_PID(pid, POSIX_PID_MIN, POSIX_PID_MAX)
PHP_POSIX_CHECK_PID(pid, 1, POSIX_PID_MIN, POSIX_PID_MAX)
if (kill(pid, sig) < 0) {
POSIX_G(last_error) = errno;
@ -307,7 +307,8 @@ PHP_FUNCTION(posix_setpgid)
Z_PARAM_LONG(pgid)
ZEND_PARSE_PARAMETERS_END();
PHP_POSIX_CHECK_PID(pid, 0, POSIX_PID_MAX)
PHP_POSIX_CHECK_PID(pid, 1, 0, POSIX_PID_MAX)
PHP_POSIX_CHECK_PID(pgid, 2, 0, POSIX_PID_MAX)
if (setpgid(pid, pgid) < 0) {
POSIX_G(last_error) = errno;
@ -347,6 +348,8 @@ PHP_FUNCTION(posix_getsid)
Z_PARAM_LONG(val)
ZEND_PARSE_PARAMETERS_END();
PHP_POSIX_CHECK_PID(val, 1, 0, POSIX_PID_MAX)
if ((val = getsid(val)) < 0) {
POSIX_G(last_error) = errno;
RETURN_FALSE;
@ -1201,6 +1204,21 @@ PHP_FUNCTION(posix_setrlimit)
Z_PARAM_LONG(max)
ZEND_PARSE_PARAMETERS_END();
if (cur < -1) {
zend_argument_value_error(2, "must be greater or equal to -1");
RETURN_THROWS();
}
if (max < -1) {
zend_argument_value_error(3, "must be greater or equal to -1");
RETURN_THROWS();
}
if (max > -1 && cur > max) {
zend_argument_value_error(2, "must be lower or equal to " ZEND_LONG_FMT, max);
RETURN_THROWS();
}
rl.rlim_cur = cur;
rl.rlim_max = max;

View file

@ -9,7 +9,11 @@ PHP Testfest Berlin 2009-05-10
posix
--FILE--
<?php
var_dump( posix_getsid(-1) );
try {
posix_getsid(-1);
} catch (\ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
bool(false)
--EXPECTF--
posix_getsid(): Argument #1 ($process_id) must be between 0 and %d

View file

@ -16,7 +16,19 @@ try {
} catch (\ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
try {
posix_setpgid(1, PHP_INT_MAX);
} catch (\ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
try {
posix_setpgid(1, -2);
} catch (\ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECTF--
posix_setpgid(): Argument #1 ($process_id) must be between 0 and %d
posix_setpgid(): Argument #1 ($process_id) must be between 0 and %d
posix_setpgid(): Argument #2 ($process_group_id) must be between 0 and %d
posix_setpgid(): Argument #2 ($process_group_id) must be between 0 and %d

View file

@ -12,9 +12,25 @@ if (str_contains(PHP_OS, 'FreeBSD')) die('skip different behavior on FreeBSD');
<?php
var_dump(posix_setrlimit(POSIX_RLIMIT_NOFILE, 128, 128));
var_dump(posix_setrlimit(POSIX_RLIMIT_NOFILE, 129, 128));
try {
posix_setrlimit(POSIX_RLIMIT_NOFILE, 129, 128);
} catch (\ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
try {
posix_setrlimit(POSIX_RLIMIT_NOFILE, -2, -1);
} catch (\ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
try {
posix_setrlimit(POSIX_RLIMIT_NOFILE, -1, -2);
} catch (\ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
bool(true)
bool(false)
posix_setrlimit(): Argument #2 ($soft_limit) must be lower or equal to 128
posix_setrlimit(): Argument #2 ($soft_limit) must be greater or equal to -1
posix_setrlimit(): Argument #3 ($hard_limit) must be greater or equal to -1