Skip to content

ext/session: Fix cache_expire ini overflow/underflow. #16445

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

Draft
wants to merge 4 commits into
base: PHP-8.2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/php.m4
Original file line number Diff line number Diff line change
Expand Up @@ -2430,6 +2430,7 @@ AC_DEFUN([PHP_CHECK_STDINT_TYPES], [
AC_CHECK_SIZEOF([long long])
AC_CHECK_SIZEOF([size_t])
AC_CHECK_SIZEOF([off_t])
AC_CHECK_SIZEOF([time_t])
AC_CHECK_TYPES([int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t], [], [
AC_MSG_ERROR([One of the intN_t or uintN_t types is not available])
], [
Expand Down
16 changes: 15 additions & 1 deletion ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,21 @@ CACHE_LIMITER_FUNC(public) /* {{{ */
time_t now;

gettimeofday(&tv, NULL);
now = tv.tv_sec + PS(cache_expire) * 60;
zend_long cache_expire = PS(cache_expire);

#if SIZEOF_TIME_T == 4 || SIZEOF_TIME_T == 8
const time_t max = (time_t)((zend_ulong)~0 >> 1);
#else
const time_t max = (time_t)((~(time_t)0) >> 1);
#endif

if (cache_expire < 0) {
now = tv.tv_sec;
} else if ((cache_expire / 60) > (max - (zend_long)max)) {
now = max;
} else {
now = tv.tv_sec + cache_expire * 60;
}
memcpy(buf, EXPIRES, sizeof(EXPIRES) - 1);
strcpy_gmt(buf + sizeof(EXPIRES) - 1, &now);
ADD_HEADER(buf);
Expand Down
4 changes: 2 additions & 2 deletions ext/session/tests/session_cache_expire_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ var_dump(session_cache_expire());
echo "Done";
ob_end_flush();
?>
--EXPECT--
--EXPECTF--
*** Testing session_cache_expire() : basic functionality ***
int(180)
int(180)
int(1234567890)
int(%d)
bool(true)
int(180)
bool(true)
Expand Down
27 changes: 27 additions & 0 deletions ext/session/tests/session_cache_expire_oflow.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
session_cache_expire() overflow
--EXTENSIONS--
session
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php

ob_start();

echo "*** Testing session_cache_expire() : overflow test ***\n";

session_cache_limiter("public");
var_dump(session_cache_expire((int)(PHP_INT_MAX/60)));
session_start();
var_dump(session_cache_expire() * 60);

echo "Done";
ob_end_flush();
?>
--EXPECTF--
*** Testing session_cache_expire() : overflow test ***
int(180)
int(%s)
Done

4 changes: 2 additions & 2 deletions ext/session/tests/session_cache_expire_variation1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var_dump(session_cache_expire());
echo "Done";
ob_end_flush();
?>
--EXPECT--
--EXPECTF--
*** Testing session_cache_expire() : variation ***
int(360)
int(360)
int(1234567890)
int(%d)
bool(true)
int(180)
bool(true)
Expand Down
4 changes: 2 additions & 2 deletions ext/session/tests/session_cache_expire_variation2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ var_dump(session_cache_expire());
echo "Done";
ob_end_flush();
?>
--EXPECT--
--EXPECTF--
*** Testing session_cache_expire() : variation ***
int(360)
int(360)
int(1234567890)
int(%d)
bool(true)
int(180)
bool(true)
Expand Down
6 changes: 3 additions & 3 deletions ext/session/tests/session_cache_expire_variation3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ var_dump(ini_get("session.cache_expire"));
echo "Done";
ob_end_flush();
?>
--EXPECT--
--EXPECTF--
*** Testing session_cache_expire() : variation ***
string(3) "180"
int(180)
string(3) "180"
int(180)
string(10) "1234567890"
bool(true)
int(1234567890)
int(%d)
string(10) "1234567890"
bool(true)
int(1234567890)
int(%d)
string(10) "1234567890"
Done
Loading