| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "extract-word.h" |
| 5 |
#include "securebits-util.h" |
| 6 |
#include "string-util.h" |
| 7 |
#include "strv.h" |
| 8 |
|
| 9 |
static inline const char* secure_bit_to_string(int i) { |
| 10 |
/* match a single bit */ |
| 11 |
|
| 12 |
switch (i) { |
| 13 |
case SECURE_KEEP_CAPS: |
| 14 |
return "keep-caps"; |
| 15 |
case SECURE_KEEP_CAPS_LOCKED: |
| 16 |
return "keep-caps-locked"; |
| 17 |
case SECURE_NO_SETUID_FIXUP: |
| 18 |
return "no-setuid-fixup"; |
| 19 |
case SECURE_NO_SETUID_FIXUP_LOCKED: |
| 20 |
return "no-setuid-fixup-locked"; |
| 21 |
case SECURE_NOROOT: |
| 22 |
return "noroot"; |
| 23 |
case SECURE_NOROOT_LOCKED: |
| 24 |
return "noroot-locked"; |
| 25 |
default: |
| 26 |
assert_not_reached(); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
int secure_bits_to_string_alloc(int i, char **ret) { |
| 31 |
_cleanup_strv_free_ char **sv = NULL; |
| 32 |
_cleanup_free_ char *joined = NULL; |
| 33 |
int r; |
| 34 |
|
| 35 |
assert(ret); |
| 36 |
|
| 37 |
r = secure_bits_to_strv(i, &sv); |
| 38 |
if (r < 0) |
| 39 |
return r; |
| 40 |
|
| 41 |
joined = strv_join(sv, " "); |
| 42 |
if (!joined) |
| 43 |
return -ENOMEM; |
| 44 |
|
| 45 |
*ret = TAKE_PTR(joined); |
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
int secure_bits_to_strv(int i, char ***ret) { |
| 50 |
_cleanup_strv_free_ char **sv = NULL; |
| 51 |
static const int bits[] = { |
| 52 |
SECURE_KEEP_CAPS, |
| 53 |
SECURE_KEEP_CAPS_LOCKED, |
| 54 |
SECURE_NO_SETUID_FIXUP, |
| 55 |
SECURE_NO_SETUID_FIXUP_LOCKED, |
| 56 |
SECURE_NOROOT, |
| 57 |
SECURE_NOROOT_LOCKED, |
| 58 |
}; |
| 59 |
int r; |
| 60 |
|
| 61 |
assert(ret); |
| 62 |
|
| 63 |
FOREACH_ELEMENT(bit, bits) { |
| 64 |
if (i & (1 << *bit)) { |
| 65 |
r = strv_extend(&sv, secure_bit_to_string(*bit)); |
| 66 |
if (r < 0) |
| 67 |
return r; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
*ret = TAKE_PTR(sv); |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
int secure_bits_from_string(const char *s) { |
| 76 |
int secure_bits = 0; |
| 77 |
const char *p; |
| 78 |
int r; |
| 79 |
|
| 80 |
for (p = s;;) { |
| 81 |
_cleanup_free_ char *word = NULL; |
| 82 |
|
| 83 |
r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE); |
| 84 |
if (r == -ENOMEM) |
| 85 |
return r; |
| 86 |
if (r <= 0) |
| 87 |
break; |
| 88 |
|
| 89 |
if (streq(word, "keep-caps")) |
| 90 |
secure_bits |= 1 << SECURE_KEEP_CAPS; |
| 91 |
else if (streq(word, "keep-caps-locked")) |
| 92 |
secure_bits |= 1 << SECURE_KEEP_CAPS_LOCKED; |
| 93 |
else if (streq(word, "no-setuid-fixup")) |
| 94 |
secure_bits |= 1 << SECURE_NO_SETUID_FIXUP; |
| 95 |
else if (streq(word, "no-setuid-fixup-locked")) |
| 96 |
secure_bits |= 1 << SECURE_NO_SETUID_FIXUP_LOCKED; |
| 97 |
else if (streq(word, "noroot")) |
| 98 |
secure_bits |= 1 << SECURE_NOROOT; |
| 99 |
else if (streq(word, "noroot-locked")) |
| 100 |
secure_bits |= 1 << SECURE_NOROOT_LOCKED; |
| 101 |
} |
| 102 |
|
| 103 |
return secure_bits; |
| 104 |
} |