[php-src] PR #15205: Fix UAF issues with PCRE after request shutdown
| From: | nielsdos | Date: | Fri, 02 Aug 2024 22:14:48 +0000 |
| Subject: | [php-src] PR #15205: Fix UAF issues with PCRE after request shutdown | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-27617@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/15205
Author: nielsdos
There are two related issues, each tested.
First problem:
What happens is that on the CLI SAPI we have a per-request pcre cache, and on there the request
shutdown for the pcre module happens prior to the remaining live object destruction. So when the SPL
object wants to clean up the regular expression object it gets a use-after-free.
Second problem:
Very similarly, the non-persistent resources are destroyed after request shutdown, so on the CLI
SAPI the pcre request cache is already gone, but if a userspace stream references a regex in the
pcre cache, this breaks.
Two things that come immediately to mind:
- We could fix it by no longer treating the CLI SAPI special and just use the same lifecycle as
the module. This simplifies the pcre module code a bit too. I wonder why we even have the separation
in the first place. The downside here is that we're using more the system allocator than
Zend's allocator for cache entries.
- We could modify the shutdown code to not remove regular expressions with a refcount>0 and
modify php_pcre_pce_decref code such that it becomes php_pcre_pce_decref's job to clean up when
the refcount becomes 0 during shutdown. However, this gets nasty quickly.
I chose the first solution here as it should be reliable and simple.
Closes GH-15064.