Fix GH-16630: UAF in lexer with encoding translation and heredocs

zend_save_lexical_state() can be nested multiple times, for example for
the parser initialization and then in the heredoc lexing. The input
should not be freed if we restore to the same filtered string.

Closes GH-16716.
This commit is contained in:
Niels Dossche 2024-11-06 20:12:10 +01:00
parent cae2582416
commit fc1db70f10
No known key found for this signature in database
GPG key ID: B8A8AD166DF0E2E5
3 changed files with 22 additions and 1 deletions

2
NEWS
View file

@ -12,6 +12,8 @@ PHP NEWS
(frankenphp)). (nielsdos)
. Fixed bug GH-16799 (Assertion failure at Zend/zend_vm_execute.h:7469).
(nielsdos)
. Fixed bug GH-16630 (UAF in lexer with encoding translation and heredocs).
(nielsdos)
- FPM:
. Fixed GH-16432 (PHP-FPM 8.2 SIGSEGV in fpm_get_status). (Jakub Zelenka)

19
Zend/tests/gh16630.phpt Normal file
View file

@ -0,0 +1,19 @@
--TEST--
GH-16630 (UAF in lexer with encoding translation and heredocs)
--EXTENSIONS--
mbstring
--INI--
zend.multibyte=On
zend.script_encoding=ISO-8859-1
internal_encoding=EUC-JP
--FILE--
<?php
$data3 = <<<CODE
heredoc
text
CODE;
echo $data3;
?>
--EXPECT--
heredoc
text

View file

@ -275,7 +275,7 @@ ZEND_API void zend_restore_lexical_state(zend_lex_state *lex_state)
CG(zend_lineno) = lex_state->lineno;
zend_restore_compiled_filename(lex_state->filename);
if (SCNG(script_filtered)) {
if (SCNG(script_filtered) && SCNG(script_filtered) != lex_state->script_filtered) {
efree(SCNG(script_filtered));
SCNG(script_filtered) = NULL;
}