-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix GH-23061: SessionHandler::create_sid() failure leaks memory in debug build #23064
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
base: PHP-8.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,6 +145,14 @@ static void php_rshutdown_session_globals(void) /* {{{ */ | |
| PS(mod)->s_close(&PS(mod_data)); | ||
| } zend_end_try(); | ||
| } | ||
| /* The user handler may not have closed the default handler it opened, e.g. because a pending | ||
| * exception prevented its close callback from running at all */ | ||
| if (PS(mod_user_is_open)) { | ||
| zend_try { | ||
| PS(default_mod)->s_close(&PS(mod_data)); | ||
| } zend_end_try(); | ||
| PS(mod_user_is_open) = false; | ||
| } | ||
|
Comment on lines
+148
to
+155
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still needed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, different path. This one guards normal shutdown when a user |
||
| if (PS(id)) { | ||
| zend_string_release_ex(PS(id), 0); | ||
| PS(id) = NULL; | ||
|
|
@@ -1760,6 +1768,12 @@ static zend_result php_session_abort(void) /* {{{ */ | |
| if (PS(mod_data) || PS(mod_user_implemented)) { | ||
| PS(mod)->s_close(&PS(mod_data)); | ||
| } | ||
| /* The user handler may not have closed the default handler it opened, e.g. because a pending | ||
| * exception prevented its close callback from running at all */ | ||
| if (PS(mod_user_is_open)) { | ||
| PS(default_mod)->s_close(&PS(mod_data)); | ||
| PS(mod_user_is_open) = false; | ||
| } | ||
| PS(session_status) = php_session_none; | ||
| return SUCCESS; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| --TEST-- | ||
| GH-23061 (SessionHandler::create_sid() callback failure leaves the default handler open) | ||
| --EXTENSIONS-- | ||
| session | ||
| --FILE-- | ||
| <?php | ||
| class BrokenSidHandler extends SessionHandler { | ||
| public function create_sid(): string { | ||
| return [3, 6]; | ||
| } | ||
| } | ||
|
|
||
| class OwnStorageHandler extends SessionHandler { | ||
| public function open(string $path, string $name): bool { | ||
| return true; | ||
| } | ||
| public function read(string $id): string|false { | ||
| return parent::read($id); | ||
| } | ||
| public function close(): bool { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| session_set_save_handler(new BrokenSidHandler()); | ||
| $message = ''; | ||
| try { | ||
| session_start(); | ||
| } catch (Error $e) { | ||
| $message = get_class($e) . ': ' . $e->getMessage(); | ||
| } | ||
|
|
||
| /* The aborted session must not leave the default handler open for the next one */ | ||
| session_set_save_handler(new OwnStorageHandler()); | ||
| $started = session_start(); | ||
| session_write_close(); | ||
|
|
||
| echo $message, "\n"; | ||
| var_dump($started); | ||
| ?> | ||
| --EXPECTF-- | ||
| Warning: SessionHandler::read(): Parent session handler is not open in %s on line %d | ||
|
|
||
| Warning: session_start(): Failed to read session data: user (path: ) in %s on line %d | ||
| Error: Session id must be a string | ||
| bool(false) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --TEST-- | ||
| GH-23061 (SessionHandler::create_sid() callback failure leaks memory in debug build) | ||
| --EXTENSIONS-- | ||
| session | ||
| --FILE-- | ||
| <?php | ||
| class a extends SessionHandler { | ||
| function create_sid(): string { | ||
| return [3, 6]; | ||
| } | ||
| } | ||
| $c = new a; | ||
| session_set_save_handler($c); | ||
| try { | ||
| session_start(); | ||
| } catch (Error $e) { | ||
| echo get_class($e), ": ", $e->getMessage(), "\n"; | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| Error: Session id must be a string |
Uh oh!
There was an error while loading. Please reload this page.