| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "capability-list.h" |
| 7 |
#include "capability-util.h" |
| 8 |
#include "random-util.h" |
| 9 |
#include "string-util.h" |
| 10 |
#include "strv.h" |
| 11 |
#include "tests.h" |
| 12 |
|
| 13 |
/* verify the capability parser */ |
| 14 |
TEST(cap_list) { |
| 15 |
assert_se(!capability_to_name(-1)); |
| 16 |
assert_se(!capability_to_name(capability_list_length())); |
| 17 |
assert_se(!capability_to_name(63)); |
| 18 |
assert_se(!capability_to_name(64)); |
| 19 |
|
| 20 |
assert_se(!CAPABILITY_TO_STRING(-1)); |
| 21 |
if (capability_list_length() <= 62) |
| 22 |
ASSERT_STREQ(CAPABILITY_TO_STRING(62), "0x3e"); |
| 23 |
assert_se(!CAPABILITY_TO_STRING(64)); |
| 24 |
|
| 25 |
for (unsigned i = 0; i < capability_list_length(); i++) { |
| 26 |
const char *n; |
| 27 |
|
| 28 |
ASSERT_NOT_NULL(n = capability_to_name(i)); |
| 29 |
ASSERT_OK_EQ(capability_from_name(n), (int) i); |
| 30 |
printf("%s = %u\n", n, i); |
| 31 |
|
| 32 |
ASSERT_STREQ(CAPABILITY_TO_STRING(i), n); |
| 33 |
} |
| 34 |
|
| 35 |
assert_se(capability_from_name("asdfbsd") == -EINVAL); |
| 36 |
assert_se(capability_from_name("CAP_AUDIT_READ") == CAP_AUDIT_READ); |
| 37 |
assert_se(capability_from_name("cap_audit_read") == CAP_AUDIT_READ); |
| 38 |
assert_se(capability_from_name("cAp_aUdIt_rEAd") == CAP_AUDIT_READ); |
| 39 |
assert_se(capability_from_name("0") == 0); |
| 40 |
assert_se(capability_from_name("15") == 15); |
| 41 |
assert_se(capability_from_name("62") == 62); |
| 42 |
assert_se(capability_from_name("63") == -EINVAL); |
| 43 |
assert_se(capability_from_name("64") == -EINVAL); |
| 44 |
assert_se(capability_from_name("-1") == -EINVAL); |
| 45 |
} |
| 46 |
|
| 47 |
static void test_capability_set_one(uint64_t c, const char *t) { |
| 48 |
_cleanup_free_ char *t1 = NULL; |
| 49 |
uint64_t c1, c_masked = c & all_capabilities(); |
| 50 |
|
| 51 |
assert_se(capability_set_to_string(c, &t1) == 0); |
| 52 |
ASSERT_STREQ(t1, t); |
| 53 |
|
| 54 |
assert_se(capability_set_from_string(t1, &c1) > 0); |
| 55 |
assert_se(c1 == c_masked); |
| 56 |
|
| 57 |
free(t1); |
| 58 |
assert_se(t1 = strjoin("'cap_chown cap_dac_override' \"cap_setgid cap_setuid\"", t, |
| 59 |
" hogehoge foobar 18446744073709551616 3.14 -3 ", t)); |
| 60 |
assert_se(capability_set_from_string(t1, &c1) == 0); |
| 61 |
assert_se(c1 == c_masked); |
| 62 |
} |
| 63 |
|
| 64 |
TEST(capability_set_from_string) { |
| 65 |
uint64_t c; |
| 66 |
|
| 67 |
assert_se(capability_set_from_string(NULL, &c) > 0); |
| 68 |
assert_se(c == 0); |
| 69 |
|
| 70 |
assert_se(capability_set_from_string("", &c) > 0); |
| 71 |
assert_se(c == 0); |
| 72 |
|
| 73 |
assert_se(capability_set_from_string("0", &c) > 0); |
| 74 |
assert_se(c == UINT64_C(1)); |
| 75 |
|
| 76 |
assert_se(capability_set_from_string("1", &c) > 0); |
| 77 |
assert_se(c == UINT64_C(1) << 1); |
| 78 |
|
| 79 |
assert_se(capability_set_from_string("0 1 2 3", &c) > 0); |
| 80 |
assert_se(c == (UINT64_C(1) << 4) - 1); |
| 81 |
} |
| 82 |
|
| 83 |
static void test_capability_set_to_strv_one(uint64_t m, char **l) { |
| 84 |
_cleanup_strv_free_ char **b = NULL; |
| 85 |
|
| 86 |
assert_se(capability_set_to_strv(m, &b) >= 0); |
| 87 |
assert_se(strv_equal(l, b)); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(capability_set_to_strv) { |
| 91 |
test_capability_set_to_strv_one(0, STRV_MAKE(NULL)); |
| 92 |
test_capability_set_to_strv_one(UINT64_C(1) << CAP_MKNOD, STRV_MAKE("cap_mknod")); |
| 93 |
test_capability_set_to_strv_one((UINT64_C(1) << CAP_MKNOD) | |
| 94 |
(UINT64_C(1) << CAP_NET_BIND_SERVICE), STRV_MAKE("cap_net_bind_service", "cap_mknod")); |
| 95 |
test_capability_set_to_strv_one((UINT64_C(1) << CAP_MKNOD) | |
| 96 |
(UINT64_C(1) << CAP_NET_BIND_SERVICE) | |
| 97 |
(UINT64_C(1) << CAP_IPC_OWNER), STRV_MAKE("cap_net_bind_service", "cap_ipc_owner", "cap_mknod")); |
| 98 |
} |
| 99 |
|
| 100 |
static void test_capability_set_to_string_invalid(uint64_t invalid_cap_set) { |
| 101 |
uint64_t c; |
| 102 |
|
| 103 |
test_capability_set_one(invalid_cap_set, ""); |
| 104 |
|
| 105 |
c = (UINT64_C(1) << CAP_DAC_OVERRIDE | invalid_cap_set); |
| 106 |
test_capability_set_one(c, "cap_dac_override"); |
| 107 |
|
| 108 |
c = (UINT64_C(1) << CAP_CHOWN | |
| 109 |
UINT64_C(1) << CAP_DAC_OVERRIDE | |
| 110 |
UINT64_C(1) << CAP_DAC_READ_SEARCH | |
| 111 |
UINT64_C(1) << CAP_FOWNER | |
| 112 |
UINT64_C(1) << CAP_SETGID | |
| 113 |
UINT64_C(1) << CAP_SETUID | |
| 114 |
UINT64_C(1) << CAP_SYS_PTRACE | |
| 115 |
UINT64_C(1) << CAP_SYS_ADMIN | |
| 116 |
UINT64_C(1) << CAP_AUDIT_CONTROL | |
| 117 |
UINT64_C(1) << CAP_MAC_OVERRIDE | |
| 118 |
UINT64_C(1) << CAP_SYSLOG | |
| 119 |
invalid_cap_set); |
| 120 |
test_capability_set_one(c, ("cap_chown cap_dac_override cap_dac_read_search cap_fowner " |
| 121 |
"cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin " |
| 122 |
"cap_audit_control cap_mac_override cap_syslog")); |
| 123 |
} |
| 124 |
|
| 125 |
TEST(capability_set_to_string) { |
| 126 |
test_capability_set_to_string_invalid(0); |
| 127 |
|
| 128 |
/* once the kernel supports 62 (CAP_LIMIT) caps, there are no 'invalid' numbers |
| 129 |
* for us to test with */ |
| 130 |
if (cap_last_cap() < CAP_LIMIT) |
| 131 |
test_capability_set_to_string_invalid(all_capabilities() + 1); |
| 132 |
} |
| 133 |
|
| 134 |
TEST(capability_set_to_string_negative) { |
| 135 |
|
| 136 |
for (unsigned i = 0; i < 150; i++) { |
| 137 |
_cleanup_free_ char *a = NULL, *b = NULL; |
| 138 |
|
| 139 |
uint64_t m = |
| 140 |
random_u64() % (UINT64_C(1) << (cap_last_cap() + 1)); |
| 141 |
|
| 142 |
ASSERT_OK(capability_set_to_string(m, &a)); |
| 143 |
ASSERT_OK(capability_set_to_string_negative(m, &b)); |
| 144 |
|
| 145 |
printf("%s (%zu) → ", a, strlen(a)); |
| 146 |
|
| 147 |
if (streq(a, b)) |
| 148 |
printf("same\n"); |
| 149 |
else |
| 150 |
printf("%s (%zu)\n", b, strlen(b)); |
| 151 |
|
| 152 |
ASSERT_LE(strlen(b), strlen(a)); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
DEFINE_TEST_MAIN(LOG_INFO); |