Skip to content

Commit 625ab10

Browse files
committed
Fix bug #68942 (Use after free vulnerability in unserialize() with DateTimeZone)
1 parent 85aab94 commit 625ab10

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

ext/date/php_date.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,15 +2720,11 @@ static int php_date_initialize_from_hash(php_date_obj **dateobj, HashTable *myht
27202720
php_timezone_obj *tzobj;
27212721

27222722
z_date = zend_hash_str_find(myht, "date", sizeof("data")-1);
2723-
if (z_date) {
2724-
convert_to_string(z_date);
2723+
if (z_date && Z_TYPE_P(z_date) == IS_STRING) {
27252724
z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type")-1);
2726-
if (z_timezone_type) {
2727-
convert_to_long(z_timezone_type);
2725+
if (z_timezone_type && Z_TYPE_P(z_timezone_type) == IS_LONG) {
27282726
z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone")-1);
2729-
if (z_timezone) {
2730-
convert_to_string(z_timezone);
2731-
2727+
if (z_timezone && Z_TYPE_P(z_timezone) == IS_STRING) {
27322728
switch (Z_LVAL_P(z_timezone_type)) {
27332729
case TIMELIB_ZONETYPE_OFFSET:
27342730
case TIMELIB_ZONETYPE_ABBR: {
@@ -2742,7 +2738,6 @@ static int php_date_initialize_from_hash(php_date_obj **dateobj, HashTable *myht
27422738

27432739
case TIMELIB_ZONETYPE_ID: {
27442740
int ret;
2745-
convert_to_string(z_timezone);
27462741

27472742
tzi = php_date_parse_tzfile(Z_STRVAL_P(z_timezone), DATE_TIMEZONEDB);
27482743

@@ -3657,7 +3652,9 @@ static int php_date_timezone_initialize_from_hash(zval **return_value, php_timez
36573652

36583653
if ((z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type")-1)) != NULL) {
36593654
if ((z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone")-1)) != NULL) {
3660-
convert_to_long(z_timezone_type);
3655+
if(Z_TYPE_P(z_timezone_type) != IS_LONG) {
3656+
return FAILURE;
3657+
}
36613658
if (SUCCESS == timezone_initialize(*tzobj, Z_STRVAL_P(z_timezone))) {
36623659
return SUCCESS;
36633660
}
@@ -3682,7 +3679,9 @@ PHP_METHOD(DateTimeZone, __set_state)
36823679

36833680
php_date_instantiate(date_ce_timezone, return_value);
36843681
tzobj = Z_PHPTIMEZONE_P(return_value);
3685-
php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht);
3682+
if(php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht) != SUCCESS) {
3683+
php_error_docref(NULL, E_ERROR, "Timezone initialization failed");
3684+
}
36863685
}
36873686
/* }}} */
36883687

@@ -3698,7 +3697,9 @@ PHP_METHOD(DateTimeZone, __wakeup)
36983697

36993698
myht = Z_OBJPROP_P(object);
37003699

3701-
php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht);
3700+
if(php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht) != SUCCESS) {
3701+
php_error_docref(NULL, E_ERROR, "Timezone initialization failed");
3702+
}
37023703
}
37033704
/* }}} */
37043705

ext/date/tests/bug68942.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Bug #68942 (Use after free vulnerability in unserialize() with DateTimeZone).
3+
--FILE--
4+
<?php
5+
$data = unserialize('a:2:{i:0;O:12:"DateTimeZone":2:{s:13:"timezone_type";a:2:{i:0;i:1;i:1;i:2;}s:8:"timezone";s:1:"A";}i:1;R:4;}');
6+
var_dump($data);
7+
?>
8+
--EXPECTF--
9+
Fatal error: DateTimeZone::__wakeup(): Timezone initialization failed in %s/bug68942.php on line %d

ext/date/tests/bug68942_2.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Bug #68942 (Use after free vulnerability in unserialize() with DateTime).
3+
--FILE--
4+
<?php
5+
$data = unserialize('a:2:{i:0;O:8:"DateTime":3:{s:4:"date";s:26:"2000-01-01 00:00:00.000000";s:13:"timezone_type";a:2:{i:0;i:1;i:1;i:2;}s:8:"timezone";s:1:"A";}i:1;R:5;}');
6+
var_dump($data);
7+
?>
8+
--EXPECTF--
9+
Fatal error: Invalid serialization data for DateTime object in %s/bug68942_2.php on line %d

0 commit comments

Comments
 (0)