Skip to content

ext/session: validate full length of session ID - #24997

Open
Sjord wants to merge 4 commits into
php:PHP-8.4from
Sjord:session-fully-validate
Open

Sjord wants to merge 4 commits into
php:PHP-8.4from
Sjord:session-fully-validate

Conversation

@Sjord

@Sjord Sjord commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@Sjord

Sjord commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

The LiveOverflow video of php2shell shows a leak via the PHP session cookie:

000259

This seems like it shouldn't be possible. PHP does validate the session ID, but apparently then it has already been used in a cookie. While looking into this I found this bug where the session id is only validated up unto the first null byte.

After this is fixed, perhaps it is possible to move the validation up so that an invalid identifier is not used in the cookie value.

@arnaud-lb arnaud-lb left a comment

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.

This is a good idea.

This seems like it shouldn't be possible

It's normally not possible, but in the pre-patched PHP version PS(id) can be uninitialized (therefore not validated) under some conditions.

@iliaal iliaal left a comment

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.

Good idea, in my tests setcookie was main leak vector that makes php2shell work

@jorgsowa jorgsowa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for fix

Comment thread ext/session/session.c Outdated
* ps_modules appropriately */
PHPAPI zend_result php_session_valid_key(const char *key) /* {{{ */
* ps_modules appropriately. */
static zend_result php_session_valid_key_len(const char *key, size_t key_len) /* {{{ */

@jorgsowa jorgsowa Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
static zend_result php_session_valid_key_len(const char *key, size_t key_len) /* {{{ */
static zend_result php_session_valid_key_len(const char *key, size_t key_len)

nit: Please remove those markers in functions you touch.

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. If this is a policy it would be nice if it is documented somewhere.

Comment thread ext/session/session.c Outdated
return FAILURE;
}
/* Validate the full zend_string, including embedded NUL bytes. */
PHPAPI zend_result php_session_valid_key_ex(const zend_string *key) /* {{{ */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think PHPAPI should be added in older versions. Would be nice to have follow up with cleanup on master.

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.

Normally I'd agree, but the original php2shell fix already introduced some new "APIs", and this seems harmless (imho)

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.

My understanding as an RM is adding functions doesn't break ABI, but when it does get merged, you can't change or remove it, so it's less needing approval to break ABI and more scrutiny to make sure we don't need to break ABI again. But this matches other patterns I see (zend_string variant on _ex), so it makes sense to me at least.

@jorgsowa jorgsowa Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That's why I think it's not necessary to be included in older versions, and in master old one should be deprecated in favor of new one. It's not so important though, but I also don't think that php2shell should be treated as excuse to not care about API hygiene.

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.

5 participants