[php-src] PR #15284: fix: Allows negative values for session GC probability
| From: | jorgsowa | Date: | Wed, 07 Aug 2024 22:34:42 +0000 |
| Subject: | [php-src] PR #15284: fix: Allows negative values for session GC probability | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-27735@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/15284
Author: jorgsowa
Initially, I reported this on security advisory so I just copied all the content. I would like to
ask for the deprecation of invalid values later on.
---
### Summary
According to the documentation, session configuration including settings gc_divisor and
gc_probability determines the probability that the gc (garbage collection) process is started on
every session initialization.
[Snippet from PHP
Documentation](https://www.php.net/manual/en/session.configuration.php#ini.session.gc-probability):
>
session.gc_divisor coupled with
> session.gc_probability defines the probability that the GC (garbage
> collection) process is started on every session initialization. The probability is calculated by
> using gc_probability/gc_divisor, e.g. 1/100 means there is a 1% chance that the GC process starts on
> each request. session.gc_divisor defaults to
> 100.
It doesn’t explain what happens if those values have negative or 0 values, so it may assume that
it works the same. However, the behavior for such settings differs in the code, which is not
reflected in the documentation and if incorrectly configured it exposes the application for broken
session expiration vulnerability.
### Details
Setting up specific settings for the session’s garbage collection causes it not to behave as
expected. It either doesn’t work at all, or works **unpredictably**.
Probability calculated for the session GC is stored in session.c lines 397-399:
```
if (!collect && PS(gc_probability) > 0) {
collect = php_random_range(PS(random), 0, PS(gc_divisor) - 1) < PS(gc_probability);
}
```
As seen it’s not gc_probability/gc_divisor as stated in the documentation. It accepts
only positive gc_probability values and the function php_random_range is
not prepared for handling negative values as it casts them to zend_ulong returning
values outside of the defined range of passed min (0) and max
(PS(gc_divisor)-1).
### PoC
For the negative values and 0 for gc_probability, session GC doesn’t work during
session initialization.
The value 0 for gc_divisor should lead to division by zero error however, the GC runs
unpredictably depending on the system. Internally the number -1 is passed to the function
php_random_range casting values to zend_ulong which distorts expected
probability.
For the negative values for gc_divisor probability works similarly when the value is 0,
however, the expected result should be well-defined.
It doesn’t matter if the values are set through the INI file, or passed to the
session_start() function.
I.e. values -1 for gc_divisor and -1 for gc_probability disable session
GC, however, it should be run with probability 100%.
### Impact
Configuration seen as valid turns out to break the session expiration settings and may lead to
attacks using [CWE-613: Insufficient Session
Expiration](https://cwe.mitre.org/data/definitions/613.html)
All the servers using invalid values (negative numbers) of session.gc_probability and
session.gc_divisor are impacted.