[php-src] PR #18612: Do not use RTLD_DEEPBIND if dlmopen is available
| From: | danog | Date: | Wed, 21 May 2025 13:10:00 +0000 |
| Subject: | [php-src] PR #18612: Do not use RTLD_DEEPBIND if dlmopen is available | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-31741@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/18612
Author: danog
This pull request disables usage of RTLD_DEEPBIND if dlmopen is available.
Context:
* https://github.com/php/php-src/issues/10670 -
The issue this PR fixes, using RTLD_DEEPBIND with custom allocators preloaded via LD_PRELOAD causes
segfaults as the main binary is using the custom malloc, while shared libs use the global malloc due
to RTLD_DEEPBIND
* https://github.com/php/php-src/pull/16779
- My previous PR which disables RTLD_DEEPBIND only on non-apache SAPIs via autoconf at buildtime,
which raised some questions when building multiple SAPIs
* https://github.com/php/php-src/issues/10670#issuecomment-1503458109
- Context as to why RTLD_DEEPBIND was initially added, to avoid symbol conflicts with already loaded
libraries when using the embed SAPI for example via apache.
After careful consideration, I believe this is the best approach, after considering the following
alternatives:
* Making a PR based on https://github.com/php/php-src/pull/11094,
disabling deepbind by default via an ini default except when using the embed SAPI, changing the ini
value in
php_embed_init; this is still SAPI-dependent behavior, which will still cause
the same segfaults if jemalloc is used with the embed SAPI.
* Writing a wrapper for the embed SAPI which dlmopens the real libphp.so within a new namespace
(essentially what @dstogov intended with his
[suggestion](https://github.com/php/php-src/pull/16779#issuecomment-2497784069), except with
RTLD_DEEPBIND the isolation is not propagated, with dlmopen namespaces it is)
I opted for the much cleaner approach of completely disabling RTLD_DEEPBIND if [dlmopen with
LM_ID_NEWLM](https://www.man7.org/linux/man-pages/man3/dlopen.3.html) is available, leaving to users
the resposibility of isolating libphp.so when including it by using dlmopen(LM_ID_NEWLM,
"libphp.so", RTLD_LAZY); instead of dlopen("libphp.so",
RTLD_LAZY|RTLD_DEEPBIND);.
dlmopen provides full recursive isolation for all symbols both in the opened library,
and in libraries opened by that library, avoiding symbol conflict issues even more effectively than
RTLD_DEEPBIND, which is **not** recursive.
Per https://github.com/php/php-src/issues/10670#issuecomment-1503458109,
to archieve full real isolation, real-world apache usecases still need to patch apache (libapr) to
enable RTLD_DEEPBIND when dlopening libphp.so, as dso_load [does
not](https://github.com/apache/apr/blob/84850094fe9b26263af561a1c39fb7287642c0b7/dso/unix/dso.c#L139)
allow passing the RTLD_DEEPBIND flag, so it would not change much for usecases which
currently use isolation, just a tweak to the patch that currently adds the
RTLD_DEEPBIND flag to libapr to use dlmopen(LM_ID_NEWLM, ...);, instead.
On platforms where GNU extensions aren't available (and dlmopen thus isn't available),
RTLD_DEEPBIND is left enabled; if equivalent namespace isolation methods are available
on other platforms, they can be added with later pull requests if needed.