Skip to content
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

imagick.so extension enabled in PHP kills PHP with segfault even with 'php -v' #624

Closed
jpbion opened this issue Aug 7, 2023 · 15 comments
Closed

Comments

@jpbion
Copy link

jpbion commented Aug 7, 2023

Kernel 6.4.8
GLibC 2.38
PHP 8.2.8
ImageMagick 7.1.1-15
GCC 13.2

.. I believe this is trivial to reproduce if you have the above installed on your system. Then, just enable the imagick PHP extension, and even just do 'php --version'

@Danack
Copy link
Collaborator

Danack commented Aug 7, 2023

I believe this is trivial to reproduce if you have the above installed on your system.

No.

The problem will be unique to your system, and probably depend on how you installed Imagick.

Please can you try to generate a back trace using the instructions on this page: https://bugs.php.net/bugs-generating-backtrace.php .

Or with just strace if you're on Linux, by installing strace with apt e.g. apt install strace and then running PHP through it e.g. strace php --version

@Danack
Copy link
Collaborator

Danack commented Aug 7, 2023

It might also be useful if you say how you installed Imagick.

@Danack
Copy link
Collaborator

Danack commented Aug 10, 2023

@jpbion any info? I can't investigate problems that are only happening on your system without any info.

@Danack
Copy link
Collaborator

Danack commented Aug 13, 2023

I can't do anything without info, so I'm going to close this issue.

Feel free to update it with info

@jay92837483
Copy link

Hello,
I have the same issue.

php loads imagick.so using dlopen
Imagick/imagemagick loads a bunch of libraries, including libgomp
libgomp have a function initialize_env() which checks the environ variable, but this variable seems to be set to NULL by something in php.

@jay92837483
Copy link

Removing RTLD_DEEPBIND from dl_open define in php-8.2 Zend/zend_portability.h solves the problem

@Danack
Copy link
Collaborator

Danack commented Dec 14, 2023

Removing RTLD_DEEPBIND from dl_open define in php-8.2 Zend/zend_portability.h solves the problem

Oh, that's very interesting.

Please can you write some words as to what you think is going on, as I have no idea what RTLD_DEEPBIND is.

@jay92837483
Copy link

Removing RTLD_DEEPBIND from dl_open define in php-8.2 Zend/zend_portability.h solves the problem

Oh, that's very interesting.

Please can you write some words as to what you think is going on, as I have no idea what RTLD_DEEPBIND is.

If a function name in a loaded library already exists in the runtime (coming from another library etc), the flag causes imagick to always use the function from it self instead of the function already loaded in the runtime.

I think gcc 13 did some breaking optimisation changes to stdio/lib initialisation, so this might be related if libraries used by a binary was compiled with different gcc versions.
Both php, imagemagick and imagick is compiled with gcc13 on my machine, but not all of the libraries used by imagemagick (pango, cairo etc).

So when imagick is loaded using its own gcc-provided function, it is incompatible with the already loaded function in php and segfault occurrs.

Unless all software/libraries used by a process is always compiled with the exact same compiler, using RTLD_DEEPBIND can crash it.
This is just my guess..

@jpbion
Copy link
Author

jpbion commented Dec 14, 2023 via email

@jpbion
Copy link
Author

jpbion commented Dec 14, 2023 via email

@jay92837483
Copy link

my entire system is compiled with the same compiler
ok so then I guess its not that

I'm thinking it might be a bug in libgomp.
Libgomp in gcc 13 have a for-loop of environ, not checking if it is NULL in the first place.
Libgomp in gcc 12 calls getenv() instead of iterating. Getenv() probably checks if environ is NULL.

When DEEPBIND is used to load a library, environ is not taken from caller/php and could be NULL during dlopen (might be a bug in itself)
When DEEPBIND is not used, environ variable gets overloaded by caller/php and set to non-NULL.

@jay92837483
Copy link

Ill report it at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111556 and see how it goes

@jay92837483
Copy link

Aight. This is already fixed in gcc for next release :)

@jpbion
Copy link
Author

jpbion commented Dec 14, 2023 via email

@Danack
Copy link
Collaborator

Danack commented Feb 9, 2024

Well, I didn't do anything, and yet it looks like the issue is handled. Probably.

Just relinking the actual issue here - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111413

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants