| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "extract-word.h" |
| 5 |
#include "fileio.h" |
| 6 |
#include "log.h" |
| 7 |
#include "lsm-util.h" |
| 8 |
#include "mountpoint-util.h" |
| 9 |
#include "string-util.h" |
| 10 |
|
| 11 |
int lsm_supported(const char *name) { |
| 12 |
_cleanup_free_ char *lsm_list = NULL; |
| 13 |
int r; |
| 14 |
|
| 15 |
assert(name); |
| 16 |
|
| 17 |
r = read_one_line_file("/sys/kernel/security/lsm", &lsm_list); |
| 18 |
if (r == -ENOENT) { /* LSM support not available at all? */ |
| 19 |
/* If securityfs is not mounted, then propagate this as ENOPKG, to indicate that LSM might be |
| 20 |
* available we just can't determine it */ |
| 21 |
|
| 22 |
r = path_is_mount_point("/sys/kernel/security"); |
| 23 |
if (r < 0 && r != -ENOENT) |
| 24 |
return log_debug_errno(r, "Failed to check if /sys/kernel/security is a mount point: %m"); |
| 25 |
if (r == 0) |
| 26 |
return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "/sys/kernel/security is not mounted, can't determine LSM status."); |
| 27 |
|
| 28 |
return false; |
| 29 |
} |
| 30 |
if (r < 0) |
| 31 |
return log_debug_errno(r, "Failed to read /sys/kernel/security/lsm: %m"); |
| 32 |
|
| 33 |
for (const char *p = lsm_list;;) { |
| 34 |
_cleanup_free_ char *word = NULL; |
| 35 |
|
| 36 |
r = extract_first_word(&p, &word, ",", 0); |
| 37 |
if (r == 0) |
| 38 |
return false; |
| 39 |
if (r < 0) |
| 40 |
return log_debug_errno(r, "Failed to parse /sys/kernel/security/lsm: %m"); |
| 41 |
|
| 42 |
if (streq(word, name)) |
| 43 |
return true; |
| 44 |
} |
| 45 |
} |