libc,shared: detect newer library symbols at runtime

For libc syscall wrappers (pidfd_open, fsopen, openat2, etc.) we previously
gated the calls behind build-time HAVE_* checks. Replace these with shim
functions in src/libc/ that fall back to the raw syscall at runtime when the
loaded glibc lacks the symbol. The infrastructure lives in src/libc/libc-shim.h:
DEFINE_SYSCALL_SHIM falls back to a direct syscall, DEFINE_LIBC_SHIM returns
ENOSYS (for posix_spawn-family helpers that have no corresponding syscall), and
DEFINE_LIBC_ERRNO_SHIM sets errno=ENOSYS and returns -1 (for read/write-style
helpers). The weak reference to the libc symbol is bound via __asm__(\"name\")
rename so the bare libc identifier never appears as a C token — this avoids
both #undef boilerplate against override-header redirects and the resulting
-Wredundant-decls warning. Drop the corresponding cc.has_function() loop from
meson.build.

For optional libraries (libcryptsetup, libdw, libarchive), drop the per-symbol
HAVE_* checks. Always declare the prototypes, suppressing the redundant-decl
warnings via DISABLE_WARNING_REDUNDANT_DECLS and NOLINT, and resolve the symbols
after the main dlopen via a new DLSYM_OPTIONAL() helper that only assigns on
success. libcryptsetup's crypt_set_keyring_to_link / crypt_token_set_external_path
and libarchive's *_is_set wrappers use fallback functions as their pointer
initializers (returning -ENOSYS and 0 respectively), so call sites can invoke
the symbol unconditionally and just check for -ENOSYS where the \"not supported\"
distinction matters.

The same shim treatment applies to pidfd_spawn / posix_spawnattr_setcgroup_np
(src/libc/spawn.c) and epoll_pwait2 (src/libc/epoll.c), with corresponding
override headers in src/include/override/spawn.h and
src/include/override/sys/epoll.h. posix_spawn_wrapper() in process-util.c and
epoll_wait_usec() in sd-event.c now detect ENOSYS in the return value instead
of checking the function pointer, falling back to plain posix_spawn() and
epoll_wait() respectively. coredump-config and coredump-submit get a
dlopen_dw_has_dwfl_set_sysroot() helper. The kexec arch gate now uses
defined(__NR_kexec_file_load) directly; pidfd.h uses __has_include_next() to
decide whether to pull in glibc's header.

This lets binaries built against newer glibc / libcryptsetup / libdw /
libarchive headers still load and run on older targets where these symbols are
absent.
This commit is contained in:
Daan De Meyer
2026-05-13 10:29:50 +02:00
parent 5d60cd8539
commit f795d54591
51 changed files with 504 additions and 475 deletions
+13
View File
@@ -33,6 +33,19 @@ int dlopen_many_sym_or_warn_sentinel(void **dlp, const char *filename, int log_l
#define DLSYM_ARG_FORCE(arg) \
&sym_##arg, STRINGIFY(arg)
/* Resolve a single optional symbol from an already-opened library handle. The pointer variable is expected
* to be named sym_<name> (same convention as DLSYM_ARG). Only assigns on success, so the pointer keeps its
* pre-existing value if the symbol is not present — useful for fallback initialization. dlerror() is
* cleared first so callers can distinguish "symbol not found" from "symbol's value is NULL" by checking
* dlerror() after; for function symbols (which can never be NULL on success) the _v check below is
* sufficient. */
#define DLSYM_OPTIONAL(dl, name) \
({ \
(void) dlerror(); \
typeof(sym_##name) _v = (typeof(sym_##name)) dlsym((dl), #name); \
if (_v) sym_##name = _v; \
})
/* If called dlopen_many_sym_or_warn() will fail with EPERM. This can be used to block lazy loading of shared
* libs, if we transfer a process into a different namespace. Note that this does not work for all calls of
* dlopen(), just those through our dlopen_safe() wrapper (which we use comprehensively in our
+14 -13
View File
@@ -2047,7 +2047,6 @@ int posix_spawn_wrapper(
/* Initialization needs to succeed before we can set up a destructor. */
_unused_ _cleanup_(posix_spawnattr_destroyp) posix_spawnattr_t *attr_destructor = &attr;
#if HAVE_PIDFD_SPAWN
static bool have_clone_into_cgroup = true; /* kernel 5.7+ */
_cleanup_close_ int cgroup_fd = -EBADF;
@@ -2063,12 +2062,13 @@ int posix_spawn_wrapper(
return -errno;
r = posix_spawnattr_setcgroup_np(&attr, cgroup_fd);
if (r != 0)
if (r == 0)
flags |= POSIX_SPAWN_SETCGROUP;
else if (r != ENOSYS)
return -r;
flags |= POSIX_SPAWN_SETCGROUP;
/* If libc lacks posix_spawnattr_setcgroup_np we silently skip POSIX_SPAWN_SETCGROUP — the
* caller will then need to attach the child to the cgroup themselves. */
}
#endif
r = posix_spawnattr_setflags(&attr, flags);
if (r != 0)
@@ -2077,7 +2077,6 @@ int posix_spawn_wrapper(
if (r != 0)
return -r;
#if HAVE_PIDFD_SPAWN
_cleanup_close_ int pidfd = -EBADF;
r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
@@ -2101,15 +2100,18 @@ int posix_spawn_wrapper(
r = pidfd_spawn(&pidfd, path, NULL, &attr, argv, envp);
}
if (r != 0)
if (r == 0) {
r = pidref_set_pidfd_consume(ret_pidref, TAKE_FD(pidfd));
if (r < 0)
return r;
return FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP);
}
if (!ERRNO_IS_NOT_SUPPORTED(r))
return -r;
r = pidref_set_pidfd_consume(ret_pidref, TAKE_FD(pidfd));
if (r < 0)
return r;
/* pidfd_spawn unavailable (libc or kernel missing) — fall back to plain posix_spawn. */
return FLAGS_SET(flags, POSIX_SPAWN_SETCGROUP);
#else
pid_t pid;
r = posix_spawn(&pid, path, NULL, &attr, argv, envp);
@@ -2121,7 +2123,6 @@ int posix_spawn_wrapper(
return r;
return 0; /* We did not use CLONE_INTO_CGROUP so return 0, the caller will have to move the child */
#endif
}
int proc_dir_open(DIR **ret) {