commit 0cb5198c0ab76933abf67167753562d4d8ca4050
parent 920e683212aae3d4996e8e7ad38801df8cc67141
Author: Christian Grothoff <christian@grothoff.org>
Date: Wed, 11 Nov 2026 22:48:54 +0100
ipc: run plugins in a posix_spawn()ed helper instead of a fork()ed child
After fork() in a multi-threaded application the child may only call
async-signal-safe functions. Previously the child loaded and ran the
plugin, which could deadlock on a lock held by another thread. On
macOS the Objective-C runtime even killed it whenever the plugin's
libraries pulled in Apple frameworks. So this is required for macOS
and generally safer for multi-threaded applications using LE.
Diffstat:
11 files changed, 348 insertions(+), 121 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -64,6 +64,7 @@ src/main/.libs/
src/main/Makefile
src/main/Makefile.in
src/main/extract
+src/main/libextractor-helper
src/main/extract.o
src/main/getopt.o
src/main/getopt1.o
diff --git a/ChangeLog b/ChangeLog
@@ -1,3 +1,12 @@
+Wed Nov 11 10:00:00 PM CET 2026
+ Run out-of-process plugins in the new libextractor-helper program,
+ started with posix_spawn(), instead of in a fork()ed copy of the
+ application. The forked child could deadlock on a lock held by
+ another thread of a multi-threaded application, and on macOS the
+ Objective-C runtime killed it as soon as a plugin's libraries
+ loaded Apple frameworks (FFmpeg, GStreamer). The helper is
+ installed next to the plugins. -CG
+
Fri Aug 7 09:30:00 AM CEST 2026
Add sixteen plugins for a fast forensic/file-manager pass over large
volumes, all of which parse headers directly and add no new
diff --git a/contrib/apparmor/usr.bin.extract b/contrib/apparmor/usr.bin.extract
@@ -24,6 +24,8 @@
/usr/lib/libextractor/ r,
/usr/lib/libextractor/libextractor_*.so mr,
+ # runs the plugins, switching itself to the libextractor profile
+ /usr/lib/libextractor/libextractor-helper ix,
/usr/lib/libextractor_common.so.* mr,
diff --git a/doc/libextractor.texi b/doc/libextractor.texi
@@ -539,25 +539,23 @@ and finally the extraction API itself.
@cindex thread-safety
@tindex enum EXTRACTOR_Options
-Using GNU libextractor from a multi-threaded parent process requires some
-care. The problem is that on most platforms GNU libextractor starts
-sub-processes for the actual extraction work. This is useful to
-isolate the parent process from potential bugs; however, it can cause
-problems if the parent process is multi-threaded. The issue is that
-at the time of the fork, another thread of the application may hold a
-lock (i.e. in gettext or libc). That lock would then never be
-released in the child process (as the other thread is not present in
-the child process). As a result, the child process would then
-deadlock on trying to acquire the lock and never terminate. This has
-actually been observed with a lock in GNU gettext that is triggered by
-the plugin startup code when it interacts with libltdl.
-
-The problem can be solved by loading the plugins using the
-@code{EXTRACTOR_OPTION_IN_PROCESS} option, which will run GNU libextractor
-in-process and thus avoid the locking issue. In this case, all of the
-functions for loading and unloading plugins, including
-@verb{|EXTRACTOR_plugin_add_defaults|} and
-@verb{|EXTRACTOR_plugin_remove_all|}, are thread-safe and reentrant.
+By default, GNU libextractor runs each plugin in a sub-process, which
+isolates the parent process from bugs in the plugins and in the
+libraries they use. On POSIX systems, these sub-processes run the
+small program @file{libextractor-helper}, which is installed next to
+the plugins and started with @code{posix_spawn()}. As the helper is
+a new program and not a @code{fork()}ed copy of the parent process,
+this is safe even if the parent process is multi-threaded. (Versions
+before 1.21 forked the parent. If another thread of the application
+held a lock at that moment, for example in GNU gettext, the child
+could deadlock, and on macOS the Objective-C runtime terminated such
+children right away.)
+
+With the @code{EXTRACTOR_OPTION_IN_PROCESS} option, no sub-processes
+are used. In this case, all of the functions for loading and
+unloading plugins, including @verb{|EXTRACTOR_plugin_add_defaults|}
+and @verb{|EXTRACTOR_plugin_remove_all|}, are thread-safe and
+reentrant.
However, using the same plugin list from multiple threads at the same
time is not safe.
@@ -1003,14 +1001,10 @@ For example, @code{"thumbnailgst(size=256,format=png)"} asks for a
256x256 PNG, and @code{"thumbnailffmpeg(gallery:12,srt:en)"} for a
contact sheet of twelve frames plus the English subtitles.
-Two caveats apply to the GStreamer variants. Their decoders and
+One caveat applies to the GStreamer variants. Their decoders and
encoders are themselves GStreamer plugins, resolved at run time, so a
system that lacks the @code{jpeg} or @code{opus} GStreamer plugin
-builds them happily and then gets no results; and because GStreamer
-does not survive a @code{fork()} of a process that has already
-initialized it, an application that runs these plugins
-@code{EXTRACTOR_OPTION_IN_PROCESS} should not also use out-of-process
-plugins in the same process afterwards.
+builds them happily and then gets no results.
@section Full text
diff --git a/src/main/Makefile.am b/src/main/Makefile.am
@@ -34,6 +34,16 @@ if WINDOWS
EXTRACTOR_IPC=extractor_ipc_w32.c
else
EXTRACTOR_IPC=extractor_ipc_gnu.c
+# Runs each plugin out-of-process, see extractor_ipc_gnu.c. The library
+# looks for it under this name, so it is installed by hand: automake
+# would apply --program-transform-name to it.
+noinst_PROGRAMS = libextractor-helper
+install-exec-local:
+ $(MKDIR_P) '$(DESTDIR)$(plugindir)'
+ $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install \
+ $(INSTALL_PROGRAM) libextractor-helper$(EXEEXT) '$(DESTDIR)$(plugindir)'
+uninstall-local:
+ rm -f '$(DESTDIR)$(plugindir)/libextractor-helper$(EXEEXT)'
endif
if HAVE_GNU_LD
@@ -52,7 +62,8 @@ EXTRA_DIST = \
test_file.dat.bz2
libextractor_la_CPPFLAGS = \
- -DPLUGINDIR=\"@RPLUGINDIR@\" -DPLUGININSTDIR=\"${plugindir}\" $(AM_CPPFLAGS)
+ -DPLUGINDIR=\"@RPLUGINDIR@\" -DPLUGININSTDIR=\"${plugindir}\" \
+ -DPLUGINHELPER=\"libextractor-helper$(EXEEXT)\" $(AM_CPPFLAGS)
libextractor_la_SOURCES = \
extractor_common.c extractor_common.h \
extractor_datasource.c extractor_datasource.h \
@@ -67,6 +78,15 @@ libextractor_la_SOURCES = \
libextractor_la_LDFLAGS = \
$(LE_LIB_LDFLAGS) -version-info @LIB_VERSION_CURRENT@:@LIB_VERSION_REVISION@:@LIB_VERSION_AGE@
libextractor_la_LIBADD = \
+ -lltdl $(zlib) $(bz2lib) $(LTLIBICONV) $(XLIB) $(LE_LIBINTL)
+
+# The helper calls functions that libextractor.so does not export, so
+# it is built from the library's sources instead of linking against it.
+libextractor_helper_SOURCES = \
+ extractor_helper.c $(libextractor_la_SOURCES)
+libextractor_helper_CPPFLAGS = \
+ $(libextractor_la_CPPFLAGS)
+libextractor_helper_LDADD = \
-lltdl $(zlib) $(bz2lib) $(LTLIBICONV) $(XLIB) $(LE_LIBINTL) $(apparmor)
extract_SOURCES = \
diff --git a/src/main/extractor_helper.c b/src/main/extractor_helper.c
@@ -0,0 +1,97 @@
+/*
+ This file is part of libextractor.
+ Copyright (C) 2026 Vidyut Samanta and Christian Grothoff
+
+ libextractor is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ libextractor is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with libextractor; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+ */
+/**
+ * @file main/extractor_helper.c
+ * @brief program that runs one plugin out-of-process; started by
+ * extractor_ipc_gnu.c, never by hand
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "extractor.h"
+#include "extractor_plugins.h"
+#include "extractor_plugin_main.h"
+#if HAVE_SYS_APPARMOR_H
+#include <sys/apparmor.h>
+#endif
+
+
+/**
+ * Run a plugin for the process that started us.
+ *
+ * @param argc number of arguments, 5 or 6
+ * @param argv file descriptor to read requests from, file descriptor
+ * to write replies to, full path of the plugin, its short name
+ * and, optionally, its options
+ * @return 1 on usage errors, otherwise does not return
+ */
+int
+main (int argc,
+ char *const *argv)
+{
+ struct EXTRACTOR_PluginList plugin;
+ int in;
+ int out;
+
+ if ( (argc < 5) ||
+ (argc > 6) )
+ {
+ fprintf (stderr,
+ "%s runs plugins for GNU libextractor and is not meant to be started directly\n",
+ argv[0]);
+ return 1;
+ }
+ in = atoi (argv[1]);
+ out = atoi (argv[2]);
+#if HAVE_SYS_APPARMOR_H
+#if HAVE_APPARMOR
+ if (0 > aa_change_profile ("libextractor"))
+ {
+ int eno = errno;
+
+ if ( (EINVAL != eno) &&
+ (ENOENT != eno) )
+ {
+ fprintf (stderr,
+ "Failure changing AppArmor profile: %s\n",
+ strerror (eno));
+ _exit (1);
+ }
+ }
+#endif
+#endif
+ memset (&plugin,
+ 0,
+ sizeof (plugin));
+ /* EXTRACTOR_plugin_load_() frees the name if loading fails */
+ if (NULL == (plugin.libname = strdup (argv[3])))
+ _exit (1);
+ plugin.short_libname = argv[4];
+ plugin.plugin_options = (6 == argc) ? argv[5] : NULL;
+ plugin.flags = EXTRACTOR_OPTION_DEFAULT_POLICY;
+ EXTRACTOR_plugin_main_ (&plugin,
+ in,
+ out);
+ /* like the forked child we replace: skip destructors of the
+ plugin's libraries, some of which do not survive them */
+ _exit (0);
+}
+
+
+/* end of extractor_helper.c */
diff --git a/src/main/extractor_ipc_gnu.c b/src/main/extractor_ipc_gnu.c
@@ -34,9 +34,13 @@
#include <sys/wait.h>
#include <sys/shm.h>
#include <signal.h>
-#if HAVE_SYS_APPARMOR_H
-#include <sys/apparmor.h>
-#endif
+#include <spawn.h>
+#include "extractor_plugpath.h"
+
+/**
+ * The environment, passed on to the plugin helper.
+ */
+extern char **environ;
/**
* A shared memory resource (often shared with several
@@ -278,6 +282,69 @@ EXTRACTOR_datasource_get_pos_ (struct EXTRACTOR_Datasource *ds)
/**
+ * Start the process that runs @a plugin: the helper program, which
+ * then reads requests from @a in and writes replies to @a out.
+ *
+ * The helper is exec()ed rather than running the plugin in a forked
+ * copy of ourselves: if the application is multi-threaded, a fork()ed
+ * child may only call async-signal-safe functions, and loading a
+ * plugin is far from that. It could deadlock on a lock that another
+ * thread held at the time of the fork(), and on Darwin the
+ * Objective-C runtime kills such a child outright.
+ *
+ * @param plugin the plugin to run
+ * @param in pipe end the helper reads from
+ * @param out pipe end the helper writes to
+ * @param[out] pid set to the process ID of the helper
+ * @return 0 on success, otherwise an error number
+ */
+static int
+spawn_helper (const struct EXTRACTOR_PluginList *plugin,
+ int in,
+ int out,
+ pid_t *pid)
+{
+ char *helper;
+ char in_s[16];
+ char out_s[16];
+ char *argv[7];
+ int ret;
+
+ if (NULL == plugin->libname)
+ return ENOENT;
+ if (NULL == (helper = EXTRACTOR_find_helper_ ()))
+ {
+ LOG ("Could not find `%s'\n",
+ PLUGINHELPER);
+ return ENOENT;
+ }
+ snprintf (in_s,
+ sizeof (in_s),
+ "%d",
+ in);
+ snprintf (out_s,
+ sizeof (out_s),
+ "%d",
+ out);
+ argv[0] = helper;
+ argv[1] = in_s;
+ argv[2] = out_s;
+ argv[3] = plugin->libname;
+ argv[4] = plugin->short_libname;
+ argv[5] = plugin->plugin_options; /* may be NULL, ending the list */
+ argv[6] = NULL;
+ ret = posix_spawn (pid,
+ helper,
+ NULL,
+ NULL,
+ argv,
+ environ);
+ free (helper);
+ return ret;
+}
+
+
+/**
* Create a channel to communicate with a process wrapping
* the plugin of the given name. Starts the process as well.
*
@@ -295,6 +362,7 @@ EXTRACTOR_IPC_channel_create_ (struct EXTRACTOR_PluginList *plugin,
pid_t pid;
struct InitMessage *init;
size_t slen;
+ int err;
static int sigpipe_ignored;
if (! sigpipe_ignored)
@@ -337,10 +405,17 @@ EXTRACTOR_IPC_channel_create_ (struct EXTRACTOR_PluginList *plugin,
free (channel);
return NULL;
}
- pid = fork ();
- if (pid == -1)
+ /* our ends of the pipes must not be inherited by this helper, nor by
+ those of the other plugins */
+ (void) fcntl (p1[1], F_SETFD, FD_CLOEXEC);
+ (void) fcntl (p2[0], F_SETFD, FD_CLOEXEC);
+ if (0 != (err = spawn_helper (plugin,
+ p1[0],
+ p2[1],
+ &pid)))
{
- LOG_STRERROR ("fork");
+ errno = err;
+ LOG_STRERROR ("posix_spawn");
(void) close (p1[0]);
(void) close (p1[1]);
(void) close (p2[0]);
@@ -349,32 +424,6 @@ EXTRACTOR_IPC_channel_create_ (struct EXTRACTOR_PluginList *plugin,
free (channel);
return NULL;
}
- if (0 == pid)
- {
- (void) close (p1[1]);
- (void) close (p2[0]);
- free (channel->mdata);
- free (channel);
-#if HAVE_SYS_APPARMOR_H
-#if HAVE_APPARMOR
- if (0 > aa_change_profile ("libextractor"))
- {
- int eno = errno;
-
- if ( (EINVAL != eno) &&
- (ENOENT != eno) )
- {
- fprintf (stderr,
- "Failure changing AppArmor profile: %s\n",
- strerror (errno));
- _exit (1);
- }
- }
-#endif
-#endif
- EXTRACTOR_plugin_main_ (plugin, p1[0], p2[1]);
- _exit (0);
- }
(void) close (p1[0]);
(void) close (p2[1]);
channel->cpipe_in = p1[1];
diff --git a/src/main/extractor_plugpath.c b/src/main/extractor_plugpath.c
@@ -282,40 +282,36 @@ get_path_from_NSGetExecutablePath ()
/**
- * Try to obtain the path of our executable using '_dyld_image' API.
+ * Try to obtain the directory of the image that contains this code,
+ * normally libextractor itself. (We do not compare against
+ * _mh_dylib_header, which only exists in a dylib: the plugin helper
+ * is an executable built from the same sources.)
*
* @return NULL on error
*/
static char *
get_path_from_dyld_image ()
{
- const char *path;
+ Dl_info info;
char *s;
char *p;
- unsigned int i;
- int c;
- c = _dyld_image_count ();
- for (i = 0; i < c; i++)
+ if ( (0 == dladdr ((void *) &get_path_from_dyld_image,
+ &info)) ||
+ (NULL == info.dli_fname) ||
+ ('\0' == info.dli_fname[0]) )
+ return NULL;
+ if (NULL == (p = strdup (info.dli_fname)))
{
- if (((void *) _dyld_get_image_header (i)) != (void *) &_mh_dylib_header)
- continue;
- path = _dyld_get_image_name (i);
- if ( (NULL == path) || (0 == strlen (path)) )
- continue;
- if (NULL == (p = strdup (path)))
- {
- LOG_STRERROR ("strdup");
- return NULL;
- }
- s = p + strlen (p);
- while ( (s > p) && ('/' != *s) )
- s--;
- s++;
- *s = '\0';
- return p;
+ LOG_STRERROR ("strdup");
+ return NULL;
}
- return NULL;
+ s = p + strlen (p);
+ while ( (s > p) && ('/' != *s) )
+ s--;
+ s++;
+ *s = '\0';
+ return p;
}
@@ -626,6 +622,56 @@ EXTRACTOR_find_plugin_ (const char *short_name)
/**
+ * Look for the plugin helper program in the given directory.
+ *
+ * @param cls the `struct SearchContext`, whose short_name is the
+ * file name of the helper
+ * @param path directory to look in
+ */
+static void
+find_helper_in_path (void *cls,
+ const char *path)
+{
+ struct SearchContext *sc = cls;
+ char *fn;
+
+ if (NULL != sc->path)
+ return;
+ if (NULL == (fn = append_to_dir (path,
+ sc->short_name)))
+ return;
+ if (0 == access (fn,
+ X_OK))
+ sc->path = fn;
+ else
+ free (fn);
+}
+
+
+/**
+ * Find the program that runs plugins out-of-process. It is installed
+ * next to the plugins, so we look where we look for them.
+ *
+ * @return full path of the helper, NULL if it was not found
+ */
+char *
+EXTRACTOR_find_helper_ (void)
+{
+ struct SearchContext sc;
+
+ sc.path = NULL;
+ sc.short_name = PLUGINHELPER;
+ get_installation_paths (&find_helper_in_path,
+ &sc);
+ /* LIBEXTRACTOR_PREFIX may only name directories with additional
+ plugins, but the installed helper can run those as well */
+ find_helper_in_path (&sc,
+ PLUGININSTDIR);
+ return sc.path;
+}
+
+
+/**
* Closure for #load_plugins_from_dir().
*/
struct DefaultLoaderContext
diff --git a/src/main/extractor_plugpath.h b/src/main/extractor_plugpath.h
@@ -1,37 +1,46 @@
-/*
- This file is part of libextractor.
- Copyright (C) 2002, 2003, 2004, 2005, 2006, 2009, 2012 Vidyut Samanta and Christian Grothoff
-
- libextractor is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 3, or (at your
- option) any later version.
-
- libextractor is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with libextractor; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
- */
-/**
- * @file main/extractor_plugpath.h
- * @brief determine path where plugins are installed
- * @author Christian Grothoff
- */
-#ifndef EXTRACTOR_PLUGPATH_H
-#define EXTRACTOR_PLUGPATH_H
-
-/**
- * Given a short name of a library (i.e. "mime"), find
- * the full path of the respective plugin.
- */
-char *
-EXTRACTOR_find_plugin_ (const char *short_name);
-
-
-#endif
-/* EXTRACTOR_PLUGPATH_H */
+/*
+ This file is part of libextractor.
+ Copyright (C) 2002, 2003, 2004, 2005, 2006, 2009, 2012 Vidyut Samanta and Christian Grothoff
+
+ libextractor is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ libextractor is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with libextractor; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+ */
+/**
+ * @file main/extractor_plugpath.h
+ * @brief determine path where plugins are installed
+ * @author Christian Grothoff
+ */
+#ifndef EXTRACTOR_PLUGPATH_H
+#define EXTRACTOR_PLUGPATH_H
+
+/**
+ * Given a short name of a library (i.e. "mime"), find
+ * the full path of the respective plugin.
+ */
+char *
+EXTRACTOR_find_plugin_ (const char *short_name);
+
+
+/**
+ * Find the program that runs plugins out-of-process.
+ *
+ * @return full path of the helper, NULL if it was not found
+ */
+char *
+EXTRACTOR_find_helper_ (void);
+
+
+#endif
+/* EXTRACTOR_PLUGPATH_H */
diff --git a/src/plugins/test_lib.c b/src/plugins/test_lib.c
@@ -264,12 +264,12 @@ int
ET_main (const char *plugin_name,
struct ProblemSet *ps)
{
- static char env_val[] = "LIBEXTRACTOR_PREFIX=./.libs/";
+ static char env_val[] = "LIBEXTRACTOR_PREFIX=./.libs/:../main/";
int ret;
/* change environment to find plugins which may not yet be
not installed but should be in the current directory (or .libs)
- on 'make check' */
+ on 'make check', and the helper that runs them in ../main */
#ifndef WINDOWS
ignore_sigpipe ();
#endif
diff --git a/src/plugins/test_thumbnailgst.c b/src/plugins/test_thumbnailgst.c
@@ -553,7 +553,7 @@ main (int argc, char *argv[])
(void) argc;
(void) argv;
/* collect() does not go through ET_main(), which would set this */
- if (0 != putenv ("LIBEXTRACTOR_PREFIX=./.libs/"))
+ if (0 != putenv ("LIBEXTRACTOR_PREFIX=./.libs/:../main/"))
return 1;
/* None of the rounds may initialize GStreamer in this process; see
run_round(). */