[php-src] PHP-8.4: Fix opcache.enable_file_override lookups racing with OPcache restarts (#25483)
| From: | pavimus via GitHub | Date: | Mon, 16 Nov 2026 14:36:11 +0000 |
| Subject: | [php-src] PHP-8.4: Fix opcache.enable_file_override lookups racing with OPcache restarts (#25483) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143736@lists.php.net to get a copy of this message | ||
Author: pavimus (pavimus)
Committer: GitHub (web-flow)
Pusher: arnaud-lb
Date: 2026-11-16T15:36:06+01:00
Commit: https://github.com/php/php-src/commit/482c6a2644b5278fef23b26bd841461511c4b8c4
Raw diff: https://github.com/php/php-src/commit/482c6a2644b5278fef23b26bd841461511c4b8c4.diff
Fix opcache.enable_file_override lookups racing with OPcache restarts (#25483)
With opcache.enable_file_override=1, file_exists(), is_file() and
is_readable() look the file up in the shared hash table first. The
lookup neither checked whether the cache is enabled for the request nor
took the SHM read lock, so it could run while a restart (scheduled by
OOM, hash overflow or opcache_reset(), then executed by another process
or thread) cleaned the hash and the scripts, and dereference freed
entries.
Look the hash up only when the cache is enabled for the request and the
read lock is held, like accelerator_get_scripts() does; otherwise fall
back to the original function.
Changed paths:
M NEWS
M ext/opcache/zend_accelerator_module.c
Diff:
diff --git a/NEWS b/NEWS
index 95e47d2b34ad..a6555ac871c9 100644
--- a/NEWS
+++ b/NEWS
@@ -300,6 +300,8 @@ PHP NEWS
. Fix opcache phar check. (ndossche)
. Fixed bug GH-8739 (OPcache restart corrupts SHM in threaded SAPIs).
(pavimus)
+ . Fixed opcache.enable_file_override lookups racing with OPcache restarts
+ (pavimus)
- OpenSSL:
. Fixed build and test compatibility with OpenSSL 4.0 and 4.1.
diff --git a/ext/opcache/zend_accelerator_module.c b/ext/opcache/zend_accelerator_module.c
index 902d69374bb7..821dfbe39121 100644
--- a/ext/opcache/zend_accelerator_module.c
+++ b/ext/opcache/zend_accelerator_module.c
@@ -367,6 +367,12 @@ ZEND_INI_END()
static int filename_is_in_cache(zend_string *filename)
{
zend_string *key;
+ int ret = 0;
+
+ /* The hash may be cleaned by a restart: only look it up while holding the read lock */
+ if (!ZCG(accelerator_enabled) || accelerator_shm_read_lock() != SUCCESS) {
+ return 0;
+ }
key = accel_make_persistent_key(filename);
if (key != NULL) {
@@ -374,20 +380,20 @@ static int filename_is_in_cache(zend_string *filename)
if (persistent_script && !persistent_script->corrupted) {
if (ZCG(accel_directives).validate_timestamps) {
zend_file_handle handle;
- int ret;
zend_stream_init_filename_ex(&handle, filename);
ret = validate_timestamp_and_record_ex(persistent_script, &handle) == SUCCESS
? 1 : 0;
zend_destroy_file_handle(&handle);
- return ret;
+ } else {
+ ret = 1;
}
-
- return 1;
}
}
- return 0;
+ accelerator_shm_read_unlock();
+
+ return ret;
}
static int accel_file_in_cache(INTERNAL_FUNCTION_PARAMETERS)