dlopen: take log_level argument and log in fallback stubs

Every dlopen_xxx() helper now takes an int log_level argument. It is
passed through to dlopen_many_sym_or_warn() (which in turn propagates it
to dlopen_verbose() for the library-not-installed case), and is used by
the fallback stub when support for the library is not compiled in to
emit a "<lib> support is not compiled in." message at the caller's level.
Callers pass LOG_DEBUG when gracefully degrading, or a higher level when
the failure should surface, and no longer need to log redundantly at the
call site.

As part of this, dlopen_bpf_full() (which already took a log_level) is
merged into dlopen_bpf() rather than keeping both.

The static inline fallbacks used to live in the headers, which required
pulling log.h in from every header that declared a dlopen_xxx(). Move
them into the .c files instead: the declaration is always outside the
#if HAVE_XXX block, the impl sits outside the outer #if HAVE_XXX wrap
with its own internal #if HAVE_XXX/#else/#endif, and apparmor-util.c,
idn-util.c, libmount-util.c and pam-util.c are now always compiled so
they can host their stubs.
This commit is contained in:
Daan De Meyer
2026-04-22 17:41:42 +02:00
committed by Zbigniew Jędrzejewski-Szmek
parent 2d28d3cb91
commit d49f3f287a
112 changed files with 796 additions and 755 deletions
+59 -54
View File
@@ -21,58 +21,6 @@ static DLSYM_PROTOTYPE(crypt_gensalt_ra) = NULL;
static DLSYM_PROTOTYPE(crypt_preferred_method) = NULL;
static DLSYM_PROTOTYPE(crypt_ra) = NULL;
int dlopen_libcrypt(void) {
#ifdef __GLIBC__
static int cached = 0;
int r;
if (libcrypt_dl)
return 0; /* Already loaded */
if (cached < 0)
return cached; /* Already tried, and failed. */
/* Several distributions like Debian/Ubuntu and OpenSUSE provide libxcrypt as libcrypt.so.1
* (libcrypt.so.1.1 on some architectures), while others like Fedora/CentOS and Arch provide it as
* libcrypt.so.2. */
SD_ELF_NOTE_DLOPEN(
"crypt",
"Support for hashing passwords",
SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED,
"libcrypt.so.2", "libcrypt.so.1", "libcrypt.so.1.1");
_cleanup_(dlclosep) void *dl = NULL;
const char *dle = NULL;
FOREACH_STRING(soname, "libcrypt.so.2", "libcrypt.so.1", "libcrypt.so.1.1") {
r = dlopen_safe(soname, &dl, &dle);
if (r >= 0) {
log_debug("Loaded '%s' via dlopen().", soname);
break;
}
}
if (r < 0) {
log_debug_errno(r, "Failed to load libcrypt: %s", dle ?: STRERROR(r));
return (cached = -EOPNOTSUPP); /* turn into recognizable error */
}
r = dlsym_many_or_warn(
dl, LOG_DEBUG,
DLSYM_ARG(crypt_gensalt_ra),
DLSYM_ARG(crypt_preferred_method),
DLSYM_ARG(crypt_ra));
if (r < 0)
return (cached = r);
libcrypt_dl = TAKE_PTR(dl);
#else
libcrypt_dl = NULL;
sym_crypt_gensalt_ra = missing_crypt_gensalt_ra;
sym_crypt_preferred_method = missing_crypt_preferred_method;
sym_crypt_ra = missing_crypt_ra;
#endif
return 0;
}
int make_salt(char **ret) {
const char *e;
char *salt;
@@ -80,7 +28,7 @@ int make_salt(char **ret) {
assert(ret);
r = dlopen_libcrypt();
r = dlopen_libcrypt(LOG_DEBUG);
if (r < 0)
return r;
@@ -127,7 +75,7 @@ int test_password_one(const char *hashed_password, const char *password) {
assert(hashed_password);
assert(password);
r = dlopen_libcrypt();
r = dlopen_libcrypt(LOG_DEBUG);
if (r < 0)
return r;
@@ -175,3 +123,60 @@ bool looks_like_hashed_password(const char *s) {
return !STR_IN_SET(s, "x", "*");
}
int dlopen_libcrypt(int log_level) {
#if HAVE_LIBCRYPT
#ifdef __GLIBC__
static int cached = 0;
int r;
if (libcrypt_dl)
return 0; /* Already loaded */
if (cached < 0)
return cached; /* Already tried, and failed. */
/* Several distributions like Debian/Ubuntu and OpenSUSE provide libxcrypt as libcrypt.so.1
* (libcrypt.so.1.1 on some architectures), while others like Fedora/CentOS and Arch provide it as
* libcrypt.so.2. */
SD_ELF_NOTE_DLOPEN(
"crypt",
"Support for hashing passwords",
SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED,
"libcrypt.so.2", "libcrypt.so.1", "libcrypt.so.1.1");
_cleanup_(dlclosep) void *dl = NULL;
const char *dle = NULL;
FOREACH_STRING(soname, "libcrypt.so.2", "libcrypt.so.1", "libcrypt.so.1.1") {
r = dlopen_safe(soname, &dl, &dle);
if (r >= 0) {
log_debug("Loaded '%s' via dlopen().", soname);
break;
}
}
if (r < 0) {
log_full_errno(log_level, r, "Failed to load libcrypt: %s", dle ?: STRERROR(r));
return (cached = -EOPNOTSUPP); /* turn into recognizable error */
}
r = dlsym_many_or_warn(
dl, log_level,
DLSYM_ARG(crypt_gensalt_ra),
DLSYM_ARG(crypt_preferred_method),
DLSYM_ARG(crypt_ra));
if (r < 0)
return (cached = r);
libcrypt_dl = TAKE_PTR(dl);
#else
libcrypt_dl = NULL;
sym_crypt_gensalt_ra = missing_crypt_gensalt_ra;
sym_crypt_preferred_method = missing_crypt_preferred_method;
sym_crypt_ra = missing_crypt_ra;
#endif
return 0;
#else
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
"libcrypt support is not compiled in.");
#endif
}