Skip to content

Fix GH-17122: memory leak in regex #17132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ext/pcre/php_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,21 @@ static int pcre_clean_cache(zval *data, void *arg)
/* }}} */

static void free_subpats_table(zend_string **subpat_names, uint32_t num_subpats) {
bool destroy = EG(flags) & EG_FLAGS_IN_SHUTDOWN;
Copy link
Member Author

@nielsdos nielsdos Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bah I realised that this might not be safe.
Consider the following:
We are in a function called during shutdown (i.e. due to register_shutdown_function). Prior to the engine calling php_call_shutdown_functions(), the EG_FLAGS_IN_SHUTDOWN will already be set.
As a consequence, when during shutdown we prune an entry from the pcre cache table we will destroy the subpattern name permanently.
So if we have an array with the subpattern name in it, and then we make sure to prune the cache table, then we end up with a dangling subpattern name.

A solution to this would be to set a flag in ext/pcre's module shutdown that indicates we may destroy. This would be fairly simple and would avoid the above issue. WDYT

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonus: and even that is not enough.
We still may leak memory if:

  1. We create an array in global scope by matching some regex and using subpattern names
  2. Prune the cache, we decrement the refcount of the subpattern name string to 1
  3. The global scope gets cleaned up, except the symbol table doesn't and we end up with a leak

Furthermore, if the final string release happens on hash table cleanup, we can break ZendMM because it gets cleaned up in zend_array_destroy with zend_string_release_ex.

The proper solution to all of this is change how the subpattern names are cached: make the subpattern name cache per-request cache, and make it lazily populated.


uint32_t i;
for (i = 0; i < num_subpats; i++) {
if (subpat_names[i]) {
zend_string_release_ex(subpat_names[i], true);
if (destroy) {
/* Because the subpattern names are persistent, and the fact that the
* symbol table destruction is skipped when using fast_shutdown,
* this means the refcounts will not be updated for the destruction of
* the arrays that hold the subpattern name keys. */
ZEND_ASSERT(!ZSTR_IS_INTERNED(subpat_names[i]));
pefree(subpat_names[i], true);
} else {
zend_string_release_ex(subpat_names[i], true);
}
}
}
pefree(subpat_names, true);
Expand Down Expand Up @@ -971,6 +982,8 @@ static zend_always_inline void populate_match_value(

static inline void add_named(
HashTable *const subpats, zend_string *name, zval *val, bool unmatched) {
ZEND_ASSERT(GC_FLAGS(name) & IS_STR_PERSISTENT);

/* If the DUPNAMES option is used, multiple subpatterns might have the same name.
* In this case we want to preserve the one that actually has a value. */
if (!unmatched) {
Expand Down
30 changes: 30 additions & 0 deletions ext/pcre/tests/gh17122.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
GH-17122 (memory leak in regex)
--FILE--
<?php
preg_match('|(?P<name>)(\d+)|', 0xffffffff, $m1);
var_dump($m1);
\preg_match('|(?P<name2>)(\d+)|', 0, $m2);
var_dump($m2);
?>
--EXPECT--
array(4) {
[0]=>
string(10) "4294967295"
["name"]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(10) "4294967295"
}
array(4) {
[0]=>
string(1) "0"
["name2"]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(1) "0"
}
Loading