Skip to content

Commit 4831e15

Browse files
committed
Fixed bug #77843
1 parent ce73841 commit 4831e15

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ PHP NEWS
55
- FPM:
66
. Fixed bug #77921 (static.php.net doesn't work anymore). (Peter Kokot)
77

8+
- JSON:
9+
. Fixed bug #77843 (Use after free with json serializer). (Nikita)
10+
811
- Session:
912
. Fixed bug #77911 (Wrong warning for session.sid_bits_per_character). (cmb)
1013

ext/json/json_encoder.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,16 @@ int php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encode
542542
return php_json_encode_serializable_object(buf, val, options, encoder);
543543
}
544544
/* fallthrough -- Non-serializable object */
545-
case IS_ARRAY:
546-
return php_json_encode_array(buf, val, options, encoder);
545+
case IS_ARRAY: {
546+
/* Avoid modifications (and potential freeing) of the array through a reference when a
547+
* jsonSerialize() method is invoked. */
548+
zval zv;
549+
int res;
550+
ZVAL_COPY(&zv, val);
551+
res = php_json_encode_array(buf, &zv, options, encoder);
552+
zval_ptr_dtor_nogc(&zv);
553+
return res;
554+
}
547555

548556
case IS_REFERENCE:
549557
val = Z_REFVAL_P(val);

ext/json/tests/bug77843.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Bug #77843: Use after free with json serializer
3+
--FILE--
4+
<?php
5+
6+
class X implements JsonSerializable {
7+
public $prop = "value";
8+
public function jsonSerialize() {
9+
global $arr;
10+
unset($arr[0]);
11+
var_dump($this);
12+
return $this;
13+
}
14+
}
15+
16+
$arr = [new X()];
17+
var_dump(json_encode([&$arr]));
18+
19+
?>
20+
--EXPECT--
21+
object(X)#1 (1) {
22+
["prop"]=>
23+
string(5) "value"
24+
}
25+
string(20) "[[{"prop":"value"}]]"

0 commit comments

Comments
 (0)