jstedfast / gmime

A C/C++ MIME creation and parser library with support for S/MIME, PGP, and Unix mbox spools.
GNU Lesser General Public License v2.1
113 stars 36 forks source link

g_mime_init() / g_mime_shutdown() cannot be run twice - segfaults triggered #98

Closed minfrin closed 3 years ago

minfrin commented 4 years ago

When wiring gmime into an Apache httpd module, the module is configured twice, once as a test to return error messages, and a second time for the real setup after forking.

If the standard setup pattern is used, this results in the following calls:

g_mime_init();
g_mime_shutdown();
g_mime_init();
g_mime_stream_cat_new();

The above sequence triggers a segfault at the g_mime_stream_cat_new() step.

Adding a hack to ignore initialisation in the first pass works round this problem, but it does mean that if two different modules try use gmime, the second module will fail and cause httpd to crash.

The fix is to make g_mime_shutdown() leave the system in a state where a subsequent g_mime_init() will work.

jstedfast commented 3 years ago

Could you submit a patch? I don't have time to work on this. Thanks.

jstedfast commented 3 years ago

I should recommend not using g_mime_shutdown()

jstedfast commented 3 years ago

This can't be fixed.

minfrin commented 3 years ago

Both NSS and OpenSSL suffered from this issue and subsequently fixed their libraries.

In the case of NSS they supported separate concurrent sessions, in the case of OpenSSL their shutdown became a noop.

I am up to my eyeballs in fixing open source software right now, it would be significantly more efficient for a gmime expert to fix this than have me rip apart the code from scratch.

jstedfast commented 3 years ago

in the case of OpenSSL their shutdown became a noop.

That's how it would have to be done in gmime. Which begs the question: why bother? Why not just simply not call g_mime_shutdown() which is exactly what I suggested.

Let g_mime_shutdown() be useful to people not wanting to do these kinds of hacks.