Skip to content

ext/session: fix cookie_lifetime overflow#21704

Open
jorgsowa wants to merge 5 commits intophp:masterfrom
jorgsowa:fix/session-cookie-lifetime-overflow
Open

ext/session: fix cookie_lifetime overflow#21704
jorgsowa wants to merge 5 commits intophp:masterfrom
jorgsowa:fix/session-cookie-lifetime-overflow

Conversation

@jorgsowa
Copy link
Copy Markdown
Contributor

@jorgsowa jorgsowa commented Apr 10, 2026

When session.cookie_lifetime was set to a value larger than maxcookie, OnUpdateCookieLifetime returned SUCCESS without updating the internal long value, causing ini_get() string and PS(cookie_lifetime) to go out of sync.

Now the value is properly clamped to maxcookie with both the string and internal long updated consistently, and a warning is emitted.

Edit:
Added validation of value of maxcookie, only numeric strings are allowed.

When session.cookie_lifetime was set to a value larger than maxcookie,
OnUpdateCookieLifetime returned SUCCESS without updating the internal
long value, causing ini_get() string and PS(cookie_lifetime) to go
out of sync.

Now the value is properly clamped to maxcookie with both the string
and internal long updated consistently, and a warning is emitted.
Copy link
Copy Markdown
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

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

While at it could you fix the way we parse the string as well? As this probably allows non numeric strings and float strings.

Comment thread ext/session/session.c Outdated
@jorgsowa jorgsowa marked this pull request as ready for review April 11, 2026 09:43
@jorgsowa jorgsowa requested a review from Girgias April 11, 2026 09:46
Copy link
Copy Markdown
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

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

Getting there, minor comments. Thanks for tackling this :)

Comment thread ext/session/session.c Outdated
Comment thread ext/session/session.c Outdated
Comment thread ext/session/session.c Outdated
Comment thread ext/session/session.c Outdated
Comment on lines 723 to 729
} else if (lval > maxcookie) {
php_error_docref(NULL, E_WARNING, "session.cookie_lifetime must be between 0 and " ZEND_LONG_FMT ", value clamped to maximum", maxcookie);
zend_long *p = ZEND_INI_GET_ADDR();
*p = maxcookie;
entry->value = zend_long_to_str(maxcookie);
return SUCCESS;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it was a bug before for it to return SUCCESS, so I would rather have it return FAILURE and warn then silently change behaviour.

Effectively just change the prior if condition to if (lval < 0 || lval > maxcookie) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure. It changes logic and may break some implementations, but it's fine to me.

Comment thread ext/session/session.c Outdated
if (oflow != 0) {
php_error_docref(NULL, E_WARNING, "session.cookie_lifetime must be between 0 and " ZEND_LONG_FMT, maxcookie);
} else {
php_error_docref(NULL, E_WARNING, "session.cookie_lifetime must be an integer");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The usual error message is something along the line of must be of type int.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied + created an addition to CODING_CONVENTIONS.md in #21761

@jorgsowa jorgsowa force-pushed the fix/session-cookie-lifetime-overflow branch from b99050e to 83991b6 Compare April 14, 2026 21:36
@jorgsowa jorgsowa requested a review from Girgias April 18, 2026 18:22
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.

2 participants