Skip to content

Commit febae1c

Browse files
committed
Fix use-after-free when a string offset warning releases the string (GH-25329)
The error handler may release the container string while the "Illegal string offset" warning (VM read) or the undefined offset warning (JIT FETCH_DIM_IS) is raised. Hold a reference across the warning, like the other string offset warnings already do. Closes GH-25329
1 parent e8714b0 commit febae1c

5 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ PHP NEWS
1818
(Lazizbek Ergashev, David Carlier)
1919
. Fixed bug GH-25302 (Use-after-free in zend_compile_const_decl() with
2020
OPcache during concurrent compilation). (Nicolas Grekas)
21+
. Fixed use-after-free when the error handler releases a string while an
22+
"Illegal string offset" warning is raised for its offset in a read, ?? or
23+
nested isset()/empty(). (Jorg Sowa)
2124

2225
- Curl:
2326
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
@@ -44,6 +47,9 @@ PHP NEWS
4447
(Ilia Alshanetsky)
4548
. Fixed JIT ignoring leading-numeric string offsets in isset(), empty() and
4649
??. (Jorg Sowa)
50+
. Fixed use-after-free in JIT when the error handler releases a string while
51+
an "Undefined variable" warning is raised for its offset in ?? or nested
52+
isset()/empty(). (Jorg Sowa)
4753

4854
- PDO_Firebird:
4955
. Fixed stack buffer overflow with long connection user names, passwords,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Reading a leading-numeric string offset when the error handler releases the string
3+
--FILE--
4+
<?php
5+
set_error_handler(function ($errno, $errstr) {
6+
$GLOBALS['s'] = null;
7+
echo $errstr, "\n";
8+
});
9+
10+
function make(int $n) {
11+
return str_repeat("abc", $n);
12+
}
13+
14+
function test() {
15+
global $s;
16+
return $s["1x"];
17+
}
18+
19+
$s = make(2);
20+
test();
21+
var_dump($s);
22+
?>
23+
--EXPECT--
24+
Illegal string offset "1x"
25+
NULL

‎Zend/zend_execute.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2956,7 +2956,17 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
29562956
if (IS_LONG == is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset,
29572957
NULL, /* allow errors */ true, NULL, &trailing_data)) {
29582958
if (UNEXPECTED(trailing_data)) {
2959+
/* The string may be destroyed while throwing the warning.
2960+
* Temporarily increase the refcount to detect this situation. */
2961+
if (!(GC_FLAGS(str) & IS_STR_INTERNED)) {
2962+
GC_ADDREF(str);
2963+
}
29592964
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
2965+
if (!(GC_FLAGS(str) & IS_STR_INTERNED) && UNEXPECTED(GC_DELREF(str) == 0)) {
2966+
zend_string_efree(str);
2967+
ZVAL_NULL(result);
2968+
return;
2969+
}
29602970
}
29612971
goto out;
29622972
}

‎ext/opcache/jit/zend_jit_helpers.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,16 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_str_is_helper(zend_string *str, zva
11341134
break;
11351135
}
11361136
case IS_UNDEF:
1137+
/* The string may be destroyed while throwing the notice */
1138+
if (!(GC_FLAGS(str) & IS_STR_INTERNED)) {
1139+
GC_ADDREF(str);
1140+
}
11371141
zend_jit_undefined_op_helper(EG(current_execute_data)->opline->op2.var);
1142+
if (!(GC_FLAGS(str) & IS_STR_INTERNED) && UNEXPECTED(GC_DELREF(str) == 0)) {
1143+
zend_string_efree(str);
1144+
ZVAL_NULL(result);
1145+
return;
1146+
}
11381147
ZEND_FALLTHROUGH;
11391148
case IS_DOUBLE:
11401149
case IS_NULL:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
JIT FETCH_DIM_IS: undefined offset variable when the error handler releases the string
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.jit=function
7+
opcache.file_update_protection=0
8+
--EXTENSIONS--
9+
opcache
10+
--FILE--
11+
<?php
12+
set_error_handler(function ($errno, $errstr) {
13+
$GLOBALS['s'] = null;
14+
echo $errstr, "\n";
15+
});
16+
17+
function make(int $n) {
18+
return str_repeat("abc", $n);
19+
}
20+
21+
function test() {
22+
global $s;
23+
return $s[$undef] ?? "default";
24+
}
25+
26+
$s = make(2);
27+
var_dump(test());
28+
?>
29+
--EXPECT--
30+
Undefined variable $undef
31+
string(7) "default"

0 commit comments

Comments
 (0)