| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "apparmor-setup.h" |
| 4 |
|
| 5 |
#if HAVE_APPARMOR |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "apparmor-util.h" |
| 10 |
#include "errno-util.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "log.h" |
| 14 |
#include "string-util.h" |
| 15 |
#include "strv.h" |
| 16 |
#endif |
| 17 |
|
| 18 |
int mac_apparmor_setup(void) { |
| 19 |
#if HAVE_APPARMOR |
| 20 |
_cleanup_(aa_policy_cache_unrefp) aa_policy_cache *policy_cache = NULL; |
| 21 |
_cleanup_(aa_features_unrefp) aa_features *features = NULL; |
| 22 |
_cleanup_free_ char *current_profile = NULL, *cache_dir_path = NULL; |
| 23 |
int r; |
| 24 |
|
| 25 |
if (!mac_apparmor_use()) { |
| 26 |
log_debug("Skipping AppArmor initialization: not supported by the kernel, disabled, or libapparmor not installed."); |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
/* To honor LSM stacking, check per-LSM subdirectory first, and then the generic one as fallback. */ |
| 31 |
FOREACH_STRING(current_file, "/proc/self/attr/apparmor/current", "/proc/self/attr/current") { |
| 32 |
r = read_one_line_file(current_file, ¤t_profile); |
| 33 |
if (r >= 0) |
| 34 |
break; |
| 35 |
if (r != -ENOENT) |
| 36 |
log_warning_errno(r, "Failed to read current AppArmor profile from '%s', ignoring: %m", |
| 37 |
current_file); |
| 38 |
} |
| 39 |
if (!current_profile) { |
| 40 |
log_warning("Failed to get the current AppArmor profile of our own process, ignoring."); |
| 41 |
return 0; |
| 42 |
} |
| 43 |
if (!streq(current_profile, "unconfined")) { |
| 44 |
log_debug("We are already confined in an AppArmor profile."); |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
r = sym_aa_features_new_from_kernel(&features); |
| 49 |
if (r < 0) { |
| 50 |
log_warning_errno(errno, "Failed to get the AppArmor feature set from the kernel, ignoring: %m"); |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
cache_dir_path = sym_aa_policy_cache_dir_path_preview(features, AT_FDCWD, "/etc/apparmor/earlypolicy"); |
| 55 |
if (!cache_dir_path) { |
| 56 |
log_debug_errno(errno, "Failed to get the path of the early AppArmor policy cache directory, ignoring: %m"); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
/* aa_policy_cache_new will internally use the same path as aa_policy_cache_dir_path_preview has returned. */ |
| 61 |
r = sym_aa_policy_cache_new(&policy_cache, features, AT_FDCWD, "/etc/apparmor/earlypolicy", 0); |
| 62 |
if (r < 0) { |
| 63 |
if (errno == ENOENT) |
| 64 |
log_debug_errno(errno, |
| 65 |
"The early AppArmor policy cache directory '%s' does not exist.", |
| 66 |
cache_dir_path); |
| 67 |
else |
| 68 |
log_warning_errno(errno, "Failed to create a new AppArmor policy cache, ignoring: %m"); |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
r = sym_aa_policy_cache_replace_all(policy_cache, NULL); |
| 73 |
if (r < 0) { |
| 74 |
log_warning_errno(errno, |
| 75 |
"Failed to load the profiles from the early AppArmor policy cache directory '%s', ignoring: %m", |
| 76 |
cache_dir_path); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
log_info("Successfully loaded all binary profiles from AppArmor early policy cache (%s).", cache_dir_path); |
| 81 |
|
| 82 |
r = sym_aa_change_profile("systemd"); |
| 83 |
if (r < 0) { |
| 84 |
if (errno == ENOENT) |
| 85 |
log_debug_errno(errno, |
| 86 |
"Failed to change to AppArmor profile 'systemd'.\n" |
| 87 |
"Please ensure that one of the binary profile files in policy cache directory '%s' contains a profile with that name.", |
| 88 |
cache_dir_path); |
| 89 |
else |
| 90 |
log_error_errno(errno, "Failed to change to AppArmor profile 'systemd': %m"); |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
log_info("Changed to AppArmor profile systemd."); |
| 95 |
#endif |
| 96 |
return 0; |
| 97 |
} |