Skip to content

Add assertion that php_pcre_replace_impl() returns a valid pointer in SPL RegexIterator() #10520

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 1 commit into from

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Feb 6, 2023

The regex is precompiled during the constructions of the object at line 1424 in spl_iterators.c Moreover, the warnings emitted by pcre_get_compiled_regex_cache() get promoted to InvalidArgumentExceptions.

php_pcre_replace_impl() will only return NULL in case of an internal PCRE error, something which should not happen.

Closes GH-8271

I tried coming up with a test case to trigger this, but it seems impossible? @nielsdos could you have a look at this? I imagine static analysis is going to complain but wouldn't be able to come up with a test case for it... maybe creating a fuzzer for ext-pcre and the RegexIterator might be a good idea.

@nielsdos
Copy link
Member

nielsdos commented Feb 6, 2023

I can run my analysers tonight and see what they report. However, doesn't cmb's testcase (#8271 (comment)) trigger this :)? If so, why does that one not suffice? It at least triggers this on my system's PHP install.

@Girgias
Copy link
Member Author

Girgias commented Feb 6, 2023

I can run my analysers tonight and see what they report. However, doesn't cmb's testcase (#8271 (comment)) trigger this :)? If so, why does that one not suffice? It at least triggers this on my system's PHP install.

how did I miss that, somehow I'm blind... I'll go an amend this then.

@nielsdos
Copy link
Member

nielsdos commented Feb 7, 2023

Ran analysis tools, they said nothing interesting about this function (or its callees) other than what we already know: the return value of php_pcre_replace_impl can be NULL...

@Girgias Girgias changed the base branch from master to PHP-8.1 February 9, 2023 14:50
@Girgias Girgias force-pushed the pr-8271-follow-up branch 3 times, most recently from 7f6a1e4 to 6ad4881 Compare February 13, 2023 02:34
@Girgias
Copy link
Member Author

Girgias commented Feb 13, 2023

So I can reproduce the leak, Opcache needs to be disabled, but I can't figure out the issue. There seems to be a bug with generators and using foreach on a constant array where the value of yield is discarded.

@bwoebi do you maybe have an idea?

zend_string_release(replacement_str);

if (!result) {
RETURN_FALSE;
Copy link
Member

Choose a reason for hiding this comment

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

You probably have to release subject as well.

@Girgias Girgias force-pushed the pr-8271-follow-up branch from 6ad4881 to 9f7a4bc Compare March 10, 2023 14:36
php_pcre_replace_impl() will return NULL on failure.
However, in the implementation of RegexIterator::accept() the return value of
php_pcre_replace_impl() is directly used without any check,
which leads to a NULL pointer dereference.

Fix this by adding a NULL check, and returning false in that case.

Closes phpGH-8271

Signed-off-by: George Peter Banyard <[email protected]>
@Girgias Girgias force-pushed the pr-8271-follow-up branch from 9f7a4bc to 8e258a1 Compare March 10, 2023 15:36
@@ -1866,6 +1866,12 @@ PHP_METHOD(RegexIterator, accept)
}

result = php_pcre_replace_impl(intern->u.regex.pce, subject, ZSTR_VAL(subject), ZSTR_LEN(subject), replacement_str, -1, &count);
zend_string_release(replacement_str);
zend_string_release(subject);
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this PR was forgotten, but you cannot release subject here as it would cause a double free at the end of the function. This should only be released in the !result block.
Please test this with a non-interned subject string.

@nielsdos
Copy link
Member

nielsdos commented Nov 8, 2024

Actually nevermind, looks like this was already fixed by b3a56bd and I forgot about it.

@nielsdos nielsdos closed this Nov 8, 2024
@Girgias Girgias deleted the pr-8271-follow-up branch November 10, 2024 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants