| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "dlfcn-util.h" |
| 4 |
#include "errno-util.h" |
| 5 |
#include "log.h" |
| 6 |
|
| 7 |
void* safe_dlclose(void *dl) { |
| 8 |
if (!dl) |
| 9 |
return NULL; |
| 10 |
|
| 11 |
assert_se(dlclose(dl) == 0); |
| 12 |
return NULL; |
| 13 |
} |
| 14 |
|
| 15 |
static int dlsym_many_or_warnv(void *dl, int log_level, va_list ap) { |
| 16 |
void (**fn)(void); |
| 17 |
|
| 18 |
/* Tries to resolve a bunch of function symbols, and logs an error about if it cannot resolve one of |
| 19 |
* them. Note that this function possibly modifies the supplied function pointers if the whole |
| 20 |
* operation fails. */ |
| 21 |
|
| 22 |
while ((fn = va_arg(ap, typeof(fn)))) { |
| 23 |
void (*tfn)(void); |
| 24 |
const char *symbol; |
| 25 |
|
| 26 |
symbol = va_arg(ap, typeof(symbol)); |
| 27 |
|
| 28 |
tfn = (typeof(tfn)) dlsym(dl, symbol); |
| 29 |
if (!tfn) |
| 30 |
return log_full_errno(log_level, |
| 31 |
SYNTHETIC_ERRNO(ELIBBAD), |
| 32 |
"Can't find symbol %s: %s", symbol, dlerror()); |
| 33 |
*fn = tfn; |
| 34 |
} |
| 35 |
|
| 36 |
return 0; |
| 37 |
} |
| 38 |
|
| 39 |
int dlsym_many_or_warn_sentinel(void *dl, int log_level, ...) { |
| 40 |
va_list ap; |
| 41 |
int r; |
| 42 |
|
| 43 |
va_start(ap, log_level); |
| 44 |
r = dlsym_many_or_warnv(dl, log_level, ap); |
| 45 |
va_end(ap); |
| 46 |
|
| 47 |
return r; |
| 48 |
} |
| 49 |
|
| 50 |
int dlopen_verbose(void **dlp, const char *filename, int log_level) { |
| 51 |
int r; |
| 52 |
|
| 53 |
assert(dlp); |
| 54 |
|
| 55 |
if (*dlp) |
| 56 |
return 0; /* Already loaded */ |
| 57 |
|
| 58 |
_cleanup_(dlclosep) void *dl = NULL; |
| 59 |
const char *dle = NULL; |
| 60 |
r = dlopen_safe(filename, &dl, &dle); |
| 61 |
if (r < 0) |
| 62 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 63 |
"Shared library '%s' is not available: %s", filename, dle ?: STRERROR(r)); |
| 64 |
|
| 65 |
log_debug("Loaded shared library '%s' via dlopen().", filename); |
| 66 |
*dlp = TAKE_PTR(dl); |
| 67 |
return 1; |
| 68 |
} |
| 69 |
|
| 70 |
int dlopen_many_sym_or_warn_sentinel(void **dlp, const char *filename, int log_level, ...) { |
| 71 |
int r; |
| 72 |
|
| 73 |
assert(dlp); |
| 74 |
|
| 75 |
if (*dlp) |
| 76 |
return 0; /* Already loaded */ |
| 77 |
|
| 78 |
_cleanup_(dlclosep) void *dl = NULL; |
| 79 |
r = dlopen_verbose(&dl, filename, log_level); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
|
| 83 |
va_list ap; |
| 84 |
va_start(ap, log_level); |
| 85 |
r = dlsym_many_or_warnv(dl, log_level, ap); |
| 86 |
va_end(ap); |
| 87 |
|
| 88 |
if (r < 0) |
| 89 |
return r; |
| 90 |
|
| 91 |
/* Note that we never release the reference here, because there's no real reason to. After all this |
| 92 |
* was traditionally a regular shared library dependency which lives forever too. */ |
| 93 |
*dlp = TAKE_PTR(dl); |
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
static bool dlopen_blocked = false; |
| 98 |
|
| 99 |
void block_dlopen(void) { |
| 100 |
dlopen_blocked = true; |
| 101 |
} |
| 102 |
|
| 103 |
int dlopen_safe(const char *filename, void **ret, const char **reterr_dlerror) { |
| 104 |
_cleanup_(dlclosep) void *dl = NULL; |
| 105 |
int r; |
| 106 |
|
| 107 |
assert(filename); |
| 108 |
|
| 109 |
/* A wrapper around dlopen(), that takes dlopen_blocked into account, and tries to normalize the |
| 110 |
* error reporting a bit. */ |
| 111 |
|
| 112 |
int flags = RTLD_NOW|RTLD_NODELETE; /* Always set RTLD_NOW + RTLD_NODELETE, for security reasons */ |
| 113 |
|
| 114 |
/* If dlopen() is blocked we'll still try it, but set RTLD_NOLOAD, so that it will still work if |
| 115 |
* already loaded (for example because the binary linked to things regularly), but fail if not. */ |
| 116 |
if (dlopen_blocked) |
| 117 |
flags |= RTLD_NOLOAD; |
| 118 |
|
| 119 |
errno = 0; |
| 120 |
dl = dlopen(filename, flags); |
| 121 |
if (!dl) { |
| 122 |
if (dlopen_blocked) { |
| 123 |
(void) dlerror(); /* consume error, so that no later call will return it */ |
| 124 |
|
| 125 |
if (reterr_dlerror) |
| 126 |
*reterr_dlerror = NULL; |
| 127 |
|
| 128 |
return log_debug_errno(SYNTHETIC_ERRNO(EPERM), "Refusing loading of '%s', as loading further dlopen() modules has been blocked.", filename); |
| 129 |
} |
| 130 |
|
| 131 |
r = errno_or_else(ENOPKG); |
| 132 |
|
| 133 |
if (reterr_dlerror) |
| 134 |
*reterr_dlerror = dlerror(); |
| 135 |
else |
| 136 |
(void) dlerror(); /* consume error, so that no later call will return it */ |
| 137 |
|
| 138 |
return r; |
| 139 |
} |
| 140 |
|
| 141 |
if (ret) |
| 142 |
*ret = TAKE_PTR(dl); |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |