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
+22 -17
View File
@@ -55,9 +55,15 @@ DLSYM_PROTOTYPE(crypt_resume_by_volume_key) = NULL;
DLSYM_PROTOTYPE(crypt_set_data_device) = NULL;
DLSYM_PROTOTYPE(crypt_set_data_offset) = NULL;
DLSYM_PROTOTYPE(crypt_set_debug_level) = NULL;
#if HAVE_CRYPT_SET_KEYRING_TO_LINK
DLSYM_PROTOTYPE(crypt_set_keyring_to_link) = NULL;
#endif
static int missing_crypt_set_keyring_to_link(
struct crypt_device *cd,
const char *key_description,
const char *old_key_description,
const char *key_type_desc,
const char *keyring_to_link_vk) {
return -ENOSYS;
}
DLSYM_PROTOTYPE(crypt_set_keyring_to_link) = missing_crypt_set_keyring_to_link;
DLSYM_PROTOTYPE(crypt_set_log_callback) = NULL;
DLSYM_PROTOTYPE(crypt_set_metadata_size) = NULL;
DLSYM_PROTOTYPE(crypt_set_pbkdf_type) = NULL;
@@ -67,9 +73,10 @@ DLSYM_PROTOTYPE(crypt_token_external_path) = NULL;
DLSYM_PROTOTYPE(crypt_token_json_get) = NULL;
DLSYM_PROTOTYPE(crypt_token_json_set) = NULL;
DLSYM_PROTOTYPE(crypt_token_max) = NULL;
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
DLSYM_PROTOTYPE(crypt_token_set_external_path) = NULL;
#endif
static int missing_crypt_token_set_external_path(const char *path) {
return -ENOSYS;
}
DLSYM_PROTOTYPE(crypt_token_set_external_path) = missing_crypt_token_set_external_path;
DLSYM_PROTOTYPE(crypt_token_status) = NULL;
DLSYM_PROTOTYPE(crypt_volume_key_get) = NULL;
DLSYM_PROTOTYPE(crypt_volume_key_keyring) = NULL;
@@ -318,9 +325,6 @@ int dlopen_cryptsetup(int log_level) {
DLSYM_ARG(crypt_set_data_device),
DLSYM_ARG(crypt_set_data_offset),
DLSYM_ARG(crypt_set_debug_level),
#if HAVE_CRYPT_SET_KEYRING_TO_LINK
DLSYM_ARG(crypt_set_keyring_to_link),
#endif
DLSYM_ARG(crypt_set_log_callback),
DLSYM_ARG(crypt_set_metadata_size),
DLSYM_ARG(crypt_set_pbkdf_type),
@@ -330,9 +334,6 @@ int dlopen_cryptsetup(int log_level) {
DLSYM_ARG(crypt_token_json_get),
DLSYM_ARG(crypt_token_json_set),
DLSYM_ARG(crypt_token_max),
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
DLSYM_ARG(crypt_token_set_external_path),
#endif
DLSYM_ARG(crypt_token_status),
DLSYM_ARG(crypt_volume_key_get),
DLSYM_ARG(crypt_volume_key_keyring),
@@ -341,6 +342,12 @@ int dlopen_cryptsetup(int log_level) {
if (r <= 0)
return r;
/* Optional symbols: present in libcryptsetup 2.7+ only. If unresolved, the prototype keeps its
* static initializer pointing at a fallback that returns -ENOSYS, so call sites can invoke the
* symbol unconditionally. */
DLSYM_OPTIONAL(cryptsetup_dl, crypt_set_keyring_to_link);
DLSYM_OPTIONAL(cryptsetup_dl, crypt_token_set_external_path);
/* Redirect the default logging calls of libcryptsetup to our own logging infra. (Note that
* libcryptsetup also maintains per-"struct crypt_device" log functions, which we'll also set
* whenever allocating a "struct crypt_device" context. Why set both? To be defensive: maybe some
@@ -350,13 +357,11 @@ int dlopen_cryptsetup(int log_level) {
const char *e = secure_getenv("SYSTEMD_CRYPTSETUP_TOKEN_PATH");
if (e) {
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
r = sym_crypt_token_set_external_path(e);
if (r < 0)
if (r == -ENOSYS)
log_debug("Loaded libcryptsetup does not support setting the external token path, not setting it to '%s'.", e);
else if (r < 0)
log_debug_errno(r, "Failed to set the libcryptsetup external token path to '%s', ignoring: %m", e);
#else
log_debug("libcryptsetup version does not support setting the external token path, not setting it to '%s'.", e);
#endif
}
return 1;