From cfce1bd2291503233564a5299172cb04d5d652e4 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Thu, 26 Mar 2020 15:48:56 +0100 Subject: [PATCH 1/2] Fix #79413: session_create_id() fails for active sessions The comment on `PS_VALIDATE_SID_FUNC(files)` is very clear that the function is supposed to return `SUCCESS` if the session already exists. So to detect a collision, we have to check for `SUCCESS`, not `FAILURE`. --- ext/session/session.c | 2 +- ext/session/tests/bug79091.phpt | 2 +- ext/session/tests/bug79413.phpt | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 ext/session/tests/bug79413.phpt diff --git a/ext/session/session.c b/ext/session/session.c index 078b3f0b3ce85..ab114c0c1a0d8 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -2285,7 +2285,7 @@ static PHP_FUNCTION(session_create_id) break; } else { /* Detect collision and retry */ - if (PS(mod)->s_validate_sid(&PS(mod_data), new_id) == FAILURE) { + if (PS(mod)->s_validate_sid(&PS(mod_data), new_id) == SUCCESS) { zend_string_release_ex(new_id, 0); new_id = NULL; continue; diff --git a/ext/session/tests/bug79091.phpt b/ext/session/tests/bug79091.phpt index 1d14427159aca..4d60e698729ec 100644 --- a/ext/session/tests/bug79091.phpt +++ b/ext/session/tests/bug79091.phpt @@ -50,7 +50,7 @@ class MySessionHandler implements SessionHandlerInterface, SessionIdInterface, S public function validateId($key) { - return false; + return true; } } diff --git a/ext/session/tests/bug79413.phpt b/ext/session/tests/bug79413.phpt new file mode 100644 index 0000000000000..756b29f6ea6a3 --- /dev/null +++ b/ext/session/tests/bug79413.phpt @@ -0,0 +1,15 @@ +--TEST-- +Bug #79413 (session_create_id() fails for active sessions) +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) From 148d7a33ab3357ea0c1d198bba2418670f09a2b1 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Thu, 26 Mar 2020 19:01:33 +0100 Subject: [PATCH 2/2] Fix wrong condition in session_regenerate_id() as well If the new SID is invalid, i.e. no such session exists yet, there is no need to create a new SID. On the other hand, if such session already exists, we have to create a new SID to satisfy strict mode (actually, we would have to repeat this until we find an invalid SID). --- ext/session/session.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/session/session.c b/ext/session/session.c index ab114c0c1a0d8..52b9da31808de 100644 --- a/ext/session/session.c +++ b/ext/session/session.c @@ -2223,7 +2223,7 @@ static PHP_FUNCTION(session_regenerate_id) RETURN_FALSE; } if (PS(use_strict_mode) && PS(mod)->s_validate_sid && - PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == FAILURE) { + PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == SUCCESS) { zend_string_release_ex(PS(id), 0); PS(id) = PS(mod)->s_create_sid(&PS(mod_data)); if (!PS(id)) {