Skip to contentSkip to conversation

Fix an unbounded recursion in pseudo's own init path

PATCH New Javier Tia 1 participant 3 messages Sep 29

lore
Share & tools
lore

Javier Tia

author
#0 lore raw

cover Fix an unbounded recursion in pseudo's own init path

I hit this building with mold 2.42.1 under bitbake's fakeroot (pseudo)
task: mold links segfault, and pseudo's diagnostic log fills with millions of "failed to save new value" lines before the stack overflows.

mold statically links mimalloc and overrides malloc/strdup process-wide. Under pseudo's LD_PRELOAD interception, mimalloc's own lazy process init calls syscall() to probe /proc/sys/vm/overcommit_memory. pseudo's syscall() wrapper reenters _libpseudo_init(), which allocates via strdup() - landing back in mimalloc, still mid-init, which can legitimately fail that allocation this early. pseudo_set_value() handles a failed strdup() by logging a warning and moving on, which is fine on its own. The problem is that pseudo_get_value()'s recovery path unconditionally calls pseudo_init_util() again whenever a lookup comes back empty but the environment variable is actually set - and pseudo_init_util() itself performs exactly such a lookup at its own tail, while its init-guard has already been cleared. If the value could not be cached, that recovery path re-enters init, which fails the same allocation again, and recurses without bound.

Patch 1 closes the recursion in pseudo_util.c: it moves the init-guard reset to the end of pseudo_init_util() and gates the recovery re-init on it, so a value that still cannot be cached surfaces through the existing diagnostic instead of recursing.

Patch 2 removes the trigger for this specific caller: pseudo's syscall()/prctl() wrappers reach _libpseudo_init() before real_syscall/ real_prctl are resolved, in two overlapping windows - before the constructor runs, and from inside it, before pseudo_init_wrappers() has run. Forwarding a pre-resolution call straight to the real libc function (for every number/option this port does not itself rewrite) means an allocator's own process init no longer re-enters pseudo's init through these two wrappers. It does not cover every other generated wrapper, which still calls pseudo_check_wrappers() on first use; an allocator whose first touch is a different wrapped libc call would still hit the same recursion through that path.

Verified against pseudo's own test suite: no change to the 18/45 tests that pass in this sandbox (which lacks chroot/openat2/renameat2 support - confirmed identical to pristine master). Verified end to end: a real gcc -fuse-ld=mold build, run under pseudo with PSEUDO_DEBUG set (matching what bitbake exports for every fakeroot task), links and runs, and a subsequent chown 0:0 on the output is correctly tracked by pseudo.

---
Javier Tia (2):
pseudo_util: bound pseudo_get_value()'s recovery re-init ports/linux/pseudo_wrappers: forward pre-init syscall/prctl to the real function

 ports/linux/pseudo_wrappers.c | 48 +++++++++++++++++++++++++++++++++++++++++++
 pseudo_util.c                 | 11 +++++++---
 2 files changed, 56 insertions(+), 3 deletions(-)

---
base-commit: ba8887e5f1e922f866681ec7dec1a00b602a9328
change-id: 20260929-pseudo-init-recursion-fix-0009ce928d8e

Best regards,

Signature · 1 line
Javier Tia <[email protected]>
author
#1 lore raw

PATCH 1/2 pseudo_util: bound pseudo_get_value()'s recovery re-init

pseudo_get_value() falls back to calling pseudo_init_util() again when a cached value is empty but the environment variable is actually set - a recovery path for a value that failed to cache the first time. But pseudo_init_util()'s own tail calls pseudo_get_value("PSEUDO_DEBUG") and pseudo_get_value("PSEUDO_EVLOG") while it is still running, and it clears its own initted guard (pseudo_util_initted = 0) before those calls rather than after. If storing a value failed (pseudo_set_value()'s strdup() returned NULL, which happens when init runs re-entrantly on the same thread, inside another allocator's own not-yet-finished process init), the recovery branch re-enters pseudo_init_util() unconditionally, which fails the same strdup() again, calls pseudo_get_value() again, and recurses until the stack overflows.

Observed with mold 2.42.1: its bundled mimalloc statically overrides malloc/strdup process-wide, and under pseudo's LD_PRELOAD interception, mimalloc's own lazy process init can call a libc function pseudo wraps before mimalloc has anywhere safe to return an allocation from. See the companion patch to ports/linux/pseudo_wrappers.c for the syscall()/ prctl() half of that same interaction.

Move the initted-guard reset to the end of pseudo_init_util(), after its own lookups, and gate the recovery re-init in pseudo_get_value() on that guard. A value that still could not be cached now surfaces through the existing "failed to save new value" diagnostic and this function returns NULL, instead of recursing.

Verified against pseudo's own test suite (no change to the 18/45 tests that pass in this sandbox, which lacks chroot/openat2/renameat2 support - confirmed identical to pristine master) and against a real gcc -fuse-ld=mold build run under pseudo with PSEUDO_DEBUG set, matching what bitbake exports for every fakeroot task.

---
 pseudo_util.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
pseudo_util.c +8 -3
index afd0266..b8afe34 100644
@@ -159,8 +159,13 @@ pseudo_get_value(const char *key) {
 
 	/* Check if the environment has it and we don't ...
 	 * if so, something went wrong... so we'll attempt to recover
+	 * -- but not while pseudo_init_util() is running.  Its own lookups
+	 * below reach this point, and when a value could not be stored
+	 * (strdup() failed) re-running it fails the same way and recurses
+	 * until the stack overflows.
 	 */
-	if (pseudo_env[i].key && !pseudo_env[i].value && GETENV(pseudo_env[i].key))
+	if (pseudo_util_initted == 0 && pseudo_env[i].key &&
+	    !pseudo_env[i].value && GETENV(pseudo_env[i].key))
 		pseudo_init_util();
 
 	if (pseudo_env[i].value)
@@ -218,8 +223,6 @@ pseudo_init_util(void) {
 			pseudo_set_value(pseudo_env[i].key, GETENV(pseudo_env[i].key));
 	}
 
-	pseudo_util_initted = 0;
-
 	/* Somewhere we have to set the debug level.. */
 	env = pseudo_get_value("PSEUDO_DEBUG");
 	if (env) {
@@ -241,6 +244,8 @@ pseudo_init_util(void) {
 		pseudo_evlog_flags_finalize();
 	}
 	free(env);
+
+	pseudo_util_initted = 0;
 }
 
 unsigned long pseudo_util_debug_flags = 0;
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH 2/2 ports/linux/pseudo_wrappers: forward pre-init syscall/prctl to the real function

In reply to Javier Tia

pseudo's syscall() and prctl() wrappers both call pseudo_check_wrappers(), which runs _libpseudo_init() when _libpseudo_initted is still 0. That is correct for a wrapped call pseudo actually needs to look at, but init itself allocates (pseudo_init_util()'s pseudo_set_value() calls strdup()), and an allocator that statically overrides the process's global malloc/strdup and uses syscall()/prctl() during its own not-yet-complete process init re-enters pseudo's own init on the same thread, before that allocator has anywhere safe to return an allocation from. Observed with mold 2.42.1's bundled mimalloc, which does exactly this under pseudo's LD_PRELOAD interception: mi_process_init() calls syscall(SYS_open, ...) to probe /proc/sys/vm/overcommit_memory.

Forward a pre-init syscall()/prctl() straight to the real libc function via dlsym(RTLD_NEXT, ...) instead, for every number/option this port does not itself rewrite (openat2, renameat2, seccomp stay on the existing path so their behavior is unchanged; PR_SET_SECCOMP is guarded with #ifdef the same way the syscall() side already guards SYS_renameat2 and SYS_seccomp, since this is now the first unconditional reference to it - previously it only appeared inside the existing
#ifdef SECCOMP_SET_MODE_FILTER block below). The guard is !_libpseudo_initted || !real_syscall rather than just the first half:
_libpseudo_init() sets _libpseudo_initted before it resolves real_syscall via pseudo_init_wrappers(), so an allocator whose first touch happens from inside the constructor itself, rather than before it runs, would otherwise still reach pseudo_enosys() and get ENOSYS instead of a real result. The resolved pointer is stored directly into real_syscall/ real_prctl, the same globals pseudo_init_wrappers() would otherwise populate, and pseudo_init_one_wrapper() already skips re-resolving a wrapper whose real pointer is non-NULL, so this path and the normal init path cannot race to different results.

This removes the re-entrant call into _libpseudo_init() for the caller that triggers it before pseudo has any state of its own to protect, without touching behavior once pseudo is initialized. It covers only syscall()/prctl(); every other generated wrapper still calls pseudo_check_wrappers() on first use (templates/wrapfuncs.c), so an allocator whose first touch is a different wrapped libc call (open(), readlink(), etc.) would still re-enter init through that path. On glibc before 2.34, dlsym() itself can allocate on a thread's first dynamic-linker call (the lazy dlerror() result struct), which would re-enter the same allocator this patch is trying to protect; this is not fixed here.

Verified against pseudo's own test suite (no change to the 18/45 tests that pass in this sandbox, which lacks chroot/openat2/renameat2 support - confirmed identical to pristine master) and against a real gcc -fuse-ld=mold build, plus a subsequent chown 0:0 on the output, run under pseudo with PSEUDO_DEBUG set, matching what bitbake exports for every fakeroot task.

---
 ports/linux/pseudo_wrappers.c | 48 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
ports/linux/pseudo_wrappers.c +48 -0
index b920cb2..63912c6 100644
@@ -59,6 +59,31 @@ syscall(long number, ...) {
 	long rc = -1;
 	va_list ap;
 
+	/* Reached before real_syscall is resolved: the caller may be a malloc
+	 * implementation initializing itself, and pseudo_check_wrappers()
+	 * would run _libpseudo_init() -> pseudo_init_util(), which allocates.
+	 * Forward the numbers pseudo never rewrites without running that path.
+	 *
+	 * The !real_syscall half of the guard covers a narrower window than
+	 * !_libpseudo_initted alone would: _libpseudo_init() (below) sets
+	 * _libpseudo_initted before it resolves real_syscall via
+	 * pseudo_init_wrappers(), so an allocator whose first touch happens
+	 * from inside the constructor itself - rather than before it runs -
+	 * would otherwise still fall through to pseudo_enosys() below. */
+	if ((!_libpseudo_initted || !real_syscall) && number != SYS_openat2
+#ifdef SYS_renameat2
+	    && number != SYS_renameat2
+#endif
+#ifdef SYS_seccomp
+	    && number != SYS_seccomp
+#endif
+	    ) {
+		if (!real_syscall)
+			real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
+		if (real_syscall)
+			goto call_syscall;
+	}
+
 	if (!pseudo_check_wrappers() || !real_syscall) {
 		/* rc was initialized to the "failure" value */
 		pseudo_enosys("syscall");
@@ -147,6 +172,20 @@ prctl(int option, ...) {
 	int rc = -1;
 	va_list ap;
 
+	/* See syscall() above: covers both the pre-constructor window and the
+	 * mid-constructor one, for every option except the one prctl() itself
+	 * still special-cases below. */
+	if ((!_libpseudo_initted || !real_prctl)
+#ifdef PR_SET_SECCOMP
+	    && option != PR_SET_SECCOMP
+#endif
+	    ) {
+		if (!real_prctl)
+			real_prctl = (int (*)(int, ...)) dlsym(RTLD_NEXT, "prctl");
+		if (real_prctl)
+			goto call_prctl;
+	}
+
 	if (!pseudo_check_wrappers() || !real_prctl) {
 		/* rc was initialized to the "failure" value */
 		pseudo_enosys("prctl");
@@ -167,6 +206,15 @@ prctl(int option, ...) {
 	}
 #endif
 
+call_prctl:
+	/* On Debian 11 - gcc (Debian 10.2.1-6) this results in:
+	 *   error: a label can only be part of a statement and a declaration
+	 *   is not a statement
+	 *
+	 * adding a ; here resolves this
+	 */
+	;
+
 	/* gcc magic to attempt to just pass these args to prctl. we have to
 	 * guess about the number of args; the docs discuss calling conventions
 	 * up to 5, so let's try that?
Signature · 1 line
2.55.0