| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "apparmor-util.h" |
| 4 |
#include "log.h" |
| 5 |
|
| 6 |
#if HAVE_APPARMOR |
| 7 |
|
| 8 |
#include <syslog.h> |
| 9 |
|
| 10 |
#include "sd-dlopen.h" |
| 11 |
|
| 12 |
#include "fileio.h" |
| 13 |
|
| 14 |
DLSYM_PROTOTYPE(aa_change_onexec) = NULL; |
| 15 |
DLSYM_PROTOTYPE(aa_change_profile) = NULL; |
| 16 |
DLSYM_PROTOTYPE(aa_features_new_from_kernel) = NULL; |
| 17 |
DLSYM_PROTOTYPE(aa_features_unref) = NULL; |
| 18 |
DLSYM_PROTOTYPE(aa_policy_cache_dir_path_preview) = NULL; |
| 19 |
DLSYM_PROTOTYPE(aa_policy_cache_new) = NULL; |
| 20 |
DLSYM_PROTOTYPE(aa_policy_cache_replace_all) = NULL; |
| 21 |
DLSYM_PROTOTYPE(aa_policy_cache_unref) = NULL; |
| 22 |
|
| 23 |
bool mac_apparmor_use(void) { |
| 24 |
static int cached_use = -1; |
| 25 |
int r; |
| 26 |
|
| 27 |
if (cached_use >= 0) |
| 28 |
return cached_use; |
| 29 |
|
| 30 |
r = read_boolean_file("/sys/module/apparmor/parameters/enabled"); |
| 31 |
if (r < 0) { |
| 32 |
if (r != -ENOENT) |
| 33 |
log_debug_errno(r, "Failed to read and parse /sys/module/apparmor/parameters/enabled, assuming AppArmor is not available: %m"); |
| 34 |
return (cached_use = false); |
| 35 |
} |
| 36 |
if (r == 0) |
| 37 |
return (cached_use = false); |
| 38 |
|
| 39 |
if (dlopen_libapparmor(LOG_DEBUG) < 0) |
| 40 |
return (cached_use = false); |
| 41 |
|
| 42 |
return (cached_use = true); |
| 43 |
} |
| 44 |
|
| 45 |
#endif |
| 46 |
|
| 47 |
int dlopen_libapparmor(int log_level) { |
| 48 |
#if HAVE_APPARMOR |
| 49 |
static void *libapparmor_dl = NULL; |
| 50 |
|
| 51 |
SD_ELF_NOTE_DLOPEN( |
| 52 |
"apparmor", |
| 53 |
"Support for AppArmor policies", |
| 54 |
SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED, |
| 55 |
"libapparmor.so.1"); |
| 56 |
|
| 57 |
return dlopen_many_sym_or_warn( |
| 58 |
&libapparmor_dl, |
| 59 |
"libapparmor.so.1", |
| 60 |
log_level, |
| 61 |
DLSYM_ARG(aa_change_onexec), |
| 62 |
DLSYM_ARG(aa_change_profile), |
| 63 |
DLSYM_ARG(aa_features_new_from_kernel), |
| 64 |
DLSYM_ARG(aa_features_unref), |
| 65 |
DLSYM_ARG(aa_policy_cache_dir_path_preview), |
| 66 |
DLSYM_ARG(aa_policy_cache_new), |
| 67 |
DLSYM_ARG(aa_policy_cache_replace_all), |
| 68 |
DLSYM_ARG(aa_policy_cache_unref)); |
| 69 |
#else |
| 70 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 71 |
"libapparmor support is not compiled in."); |
| 72 |
#endif |
| 73 |
} |