Skip to content

ext/session: Fix cache_expire ini overflow/underflow. #16445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: PHP-8.2
Choose a base branch
from

Conversation

devnexen
Copy link
Member

Setting with PHP_INT_MIN/PHP_INT_MAX lead to these.

ext/session/session.c:1181:37: runtime error: signed integer overflow: -9223372036854775808 * 60 cannot be represented in type 'long int'
ext/session/session.c:1181:37: runtime error: signed integer overflow: 9223372036854775807 * 60 cannot be represented in type 'long int'

Setting with PHP_INT_MIN/PHP_INT_MAX lead to these.

```
ext/session/session.c:1181:37: runtime error: signed integer overflow: -9223372036854775808 * 60 cannot be represented in type 'long int'
ext/session/session.c:1181:37: runtime error: signed integer overflow: 9223372036854775807 * 60 cannot be represented in type 'long int'
```
@devnexen devnexen force-pushed the cache_expire_oflow branch 5 times, most recently from 0c404c0 to 8200b40 Compare October 15, 2024 07:02
@devnexen devnexen marked this pull request as ready for review October 15, 2024 08:12
@devnexen devnexen requested a review from Girgias as a code owner October 15, 2024 08:12
Comment on lines 724 to 726
if (v < 0 || v > maxexpire) {
return SUCCESS;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely this should be return FAILURE ?

Copy link
Member Author

@devnexen devnexen Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I applied the same "policy" as for cookie_lifetime as to not disturb things for stable branches but I can return FAILURE sure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this strategy makes sense.

However, the approach appears to be insufficient. For 32bit architectures we have ((ZEND_LONG_MAX / 2) / 60) - 1;. Asssuming that time_t is a signed 32bit, that can still overflow: let's say the current timestamp is 1728992731; the max value would be 7456539. Then we do tv.tv_sec + PS(cache_expire) * 60, which results in signed overflow. For our Windows 32bit builds, time_t is long which is a signed 32bit value on LLP64.

It seems to me that we need to do the overflow check when we're actually calculating the time (maybe instead of failing, just clamping the value). Rejecting very large values in the INI modifcation handler makes sense, but doesn't solve all issues; not even for 64bit architectures when running this code in say 15 years.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright thx, I ll get back to it in couple of hours.

Comment on lines 718 to 722
#ifdef ZEND_ENABLE_ZVAL_LONG64
const zend_long maxexpire = ((ZEND_LONG_MAX - INT_MAX) / 60) - 1;
#else
const zend_long maxexpire = ((ZEND_LONG_MAX / 2) / 60) - 1;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that this is highly platform dependent, since the C standard makes no claim about time_t other than that it is an arithmetic type (could even be float/double). Current POSIX states "time_t shall be an integer type with a width of at least 64 bits". Now, that it not true for our x86 Windows builds (it's 32bit wide there); I don't know about other systems.

To handle this cleanly, we should probably determine something like TIME_MIN and TIME_MAX during configuration (and deal one way or another with non integral time_t implementations).

Copy link
Member Author

@devnexen devnexen Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concretally, even QNX does not implement time_t as float/double.

@devnexen devnexen marked this pull request as draft October 15, 2024 18:32
for now checking time_t size at runtime just for POC purpose..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants