Skip to content

Commit 6e0505b

Browse files
committed
Fix GH-9589: dl() segfaults when module is already loaded
As of PHP 8.2.0, `zend_module_entry` structures are no longer copied, so when a module is permanently loaded, and users try to dynamically load that module again, the structure is corrupted[1], causing a segfault on shutdown. We catch that by checking whether any dynamically loaded module is already loaded, and bailing out in that case without modifying the `zend_module_entry` structure. [1] <#9589 (comment)> Closes GH-9689.
1 parent 8e146c8 commit 6e0505b

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ PHP NEWS
55
- Core:
66
. Fixed bug GH-9655 (Pure intersection types cannot be implicitly nullable)
77
(Girgias)
8+
. Fixed bug GH-9589 (dl() segfaults when module is already loaded). (cmb,
9+
Arnaud)
810

911
- Streams:
1012
. Fixed bug GH-9590 (stream_select does not abort upon exception or empty

ext/standard/dl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ PHPAPI int php_load_extension(const char *filename, int type, int start_now)
205205
return FAILURE;
206206
}
207207
module_entry = get_module();
208+
if (zend_hash_str_exists(&module_registry, module_entry->name, strlen(module_entry->name))) {
209+
DL_UNLOAD(handle);
210+
zend_error(E_CORE_WARNING, "Module \"%s\" is already loaded", module_entry->name);
211+
return FAILURE;
212+
}
208213
if (module_entry->zend_api != ZEND_MODULE_API_NO) {
209214
php_error_docref(NULL, error_type,
210215
"%s: Unable to initialize module\n"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
dl() segfaults when module is already loaded
3+
--EXTENSIONS--
4+
dl_test
5+
--FILE--
6+
<?php
7+
dl("dl_test");
8+
?>
9+
--EXPECT--
10+
Warning: Module "dl_test" is already loaded in Unknown on line 0

0 commit comments

Comments
 (0)