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.

@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 on lines +712 to +718
if (type == 0) {
php_error_docref(NULL, E_WARNING, "Invalid value for CookieLifetime");
return FAILURE;
} else if (type == IS_DOUBLE && oflow == 0) {
php_error_docref(NULL, E_WARNING, "CookieLifetime must be an integer");
return FAILURE;
}
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.

Maybe move this into an if (UNEXPECTED(type != IS_LONG)) {} block?

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.

Done, also bit simplified the flow.

}
zend_long v = lval;
if (oflow < 0 || v < 0) {
php_error_docref(NULL, E_WARNING, "CookieLifetime cannot be negative");
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.

Maybe this error could be updated to the more standard wording of must be between 0 and maxcookie?

Comment on lines 723 to 728
} else if (oflow > 0 || v > maxcookie) {
php_error_docref(NULL, E_WARNING, "CookieLifetime value too large, value was set to the maximum of " ZEND_LONG_FMT, 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.

Did this use to actually update the INI setting or was it just returning SUCCESS even if it was failing? :|

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.

Yes, currently it doesn't update the value when overflows, but returns success.

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.

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
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