Skip to content

Commit 50dea59

Browse files
committed
Merge branch 'PHP-7.1'
2 parents 3139172 + 29433f9 commit 50dea59

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
Bug #69425: Use After Free in unserialize()
3+
--FILE--
4+
<?php
5+
6+
// POC 1
7+
class test
8+
{
9+
var $ryat;
10+
11+
function __wakeup()
12+
{
13+
$this->ryat = 1;
14+
}
15+
}
16+
17+
$data = unserialize('a:2:{i:0;O:4:"test":1:{s:4:"ryat";R:1;}i:1;i:2;}');
18+
var_dump($data);
19+
20+
// POC 2
21+
$data = unserialize('a:2:{i:0;O:12:"DateInterval":1:{s:1:"y";R:1;}i:1;i:2;}');
22+
var_dump($data);
23+
24+
?>
25+
--EXPECT--
26+
int(1)
27+
array(2) {
28+
[0]=>
29+
object(DateInterval)#1 (16) {
30+
["y"]=>
31+
int(-1)
32+
["m"]=>
33+
int(-1)
34+
["d"]=>
35+
int(-1)
36+
["h"]=>
37+
int(-1)
38+
["i"]=>
39+
int(-1)
40+
["s"]=>
41+
int(-1)
42+
["f"]=>
43+
float(-1)
44+
["weekday"]=>
45+
int(-1)
46+
["weekday_behavior"]=>
47+
int(-1)
48+
["first_last_day_of"]=>
49+
int(-1)
50+
["invert"]=>
51+
int(0)
52+
["days"]=>
53+
int(-1)
54+
["special_type"]=>
55+
int(0)
56+
["special_amount"]=>
57+
int(-1)
58+
["have_weekday_relative"]=>
59+
int(0)
60+
["have_special_relative"]=>
61+
int(0)
62+
}
63+
[1]=>
64+
int(2)
65+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Bug #70513: GMP Deserialization Type Confusion Vulnerability
3+
--SKIPIF--
4+
<?php if (!extension_loaded('gmp')) die('skip requires gmp');
5+
--FILE--
6+
<?php
7+
8+
class obj
9+
{
10+
var $ryat;
11+
12+
function __wakeup()
13+
{
14+
$this->ryat = 1;
15+
}
16+
}
17+
18+
$obj = new stdClass;
19+
$obj->aa = 1;
20+
$obj->bb = 2;
21+
22+
$inner = 's:1:"1";a:3:{s:2:"aa";s:2:"hi";s:2:"bb";s:2:"hi";i:0;O:3:"obj":1:{s:4:"ryat";R:2;}}';
23+
$exploit = 'a:1:{i:0;C:3:"GMP":'.strlen($inner).':{'.$inner.'}}';
24+
$x = unserialize($exploit);
25+
var_dump($x);
26+
var_dump($obj);
27+
28+
?>
29+
--EXPECT--
30+
array(1) {
31+
[0]=>
32+
int(1)
33+
}
34+
object(stdClass)#1 (2) {
35+
["aa"]=>
36+
int(1)
37+
["bb"]=>
38+
int(2)
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Bug #72731: Type Confusion in Object Deserialization
3+
--FILE--
4+
<?php
5+
6+
class obj {
7+
var $ryat;
8+
function __wakeup() {
9+
$this->ryat = 0x1122334455;
10+
}
11+
}
12+
13+
$poc = 'O:8:"stdClass":1:{i:0;O:3:"obj":1:{s:4:"ryat";R:1;}}';
14+
var_dump(unserialize($poc));
15+
16+
?>
17+
--EXPECT--
18+
int(73588229205)

ext/standard/var.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,6 @@ PHP_FUNCTION(unserialize)
11221122
}
11231123
RETVAL_FALSE;
11241124
} else {
1125-
ZVAL_DEREF(retval);
11261125
ZVAL_COPY(return_value, retval);
11271126
}
11281127

@@ -1134,6 +1133,13 @@ PHP_FUNCTION(unserialize)
11341133
/* Reset to previous allowed_classes in case this is a nested call */
11351134
php_var_unserialize_set_allowed_classes(var_hash, prev_class_hash);
11361135
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1136+
1137+
/* Per calling convention we must not return a reference here, so unwrap. We're doing this at
1138+
* the very end, because __wakeup() calls performed during UNSERIALIZE_DESTROY might affect
1139+
* the value we unwrap here. This is compatible with behavior in PHP <=7.0. */
1140+
if (Z_ISREF_P(return_value)) {
1141+
zend_unwrap_reference(return_value);
1142+
}
11371143
}
11381144
/* }}} */
11391145

0 commit comments

Comments
 (0)