Skip to content

Commit ebc815c

Browse files
Dan Streetmankeszybz
authored andcommitted
test: use cap_last_cap() for max supported cap number, not capability_list_length()
This test assumes capability_list_length() is an invalid cap number, but that isn't true if the running kernel supports more caps than we were compiled with, which results in the test failing. Instead use cap_last_cap() + 1. If cap_last_cap() is 63, there are no more 'invalid' cap numbers to test with, so the invalid cap number test part is skipped.
1 parent 9684a01 commit ebc815c

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

src/basic/cap-list.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ int capability_from_name(const char *name) {
5050
return sc->id;
5151
}
5252

53+
/* This is the number of capability names we are *compiled* with.
54+
* For the max capability number of the currently-running kernel,
55+
* use cap_last_cap(). */
5356
int capability_list_length(void) {
5457
return (int) ELEMENTSOF(capability_names);
5558
}

src/test/test-cap-list.c

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void test_cap_list(void) {
5555

5656
static void test_capability_set_one(uint64_t c, const char *t) {
5757
_cleanup_free_ char *t1 = NULL;
58-
uint64_t c1, c_masked = c & ((UINT64_C(1) << capability_list_length()) - 1);
58+
uint64_t c1, c_masked = c & all_capabilities();
5959

6060
assert_se(capability_set_to_string_alloc(c, &t1) == 0);
6161
assert_se(streq(t1, t));
@@ -70,7 +70,7 @@ static void test_capability_set_one(uint64_t c, const char *t) {
7070
assert_se(c1 == c_masked);
7171
}
7272

73-
static void test_capability_set(void) {
73+
static void test_capability_set_from_string(void) {
7474
uint64_t c;
7575

7676
assert_se(capability_set_from_string(NULL, &c) == 0);
@@ -87,38 +87,42 @@ static void test_capability_set(void) {
8787

8888
assert_se(capability_set_from_string("0 1 2 3", &c) == 0);
8989
assert_se(c == (UINT64_C(1) << 4) - 1);
90+
}
91+
92+
static void test_capability_set_to_string(uint64_t invalid_cap_set) {
93+
uint64_t c;
9094

91-
test_capability_set_one(0, "");
92-
test_capability_set_one(
93-
UINT64_C(1) << CAP_DAC_OVERRIDE,
94-
"cap_dac_override");
95-
test_capability_set_one(
96-
UINT64_C(1) << CAP_DAC_OVERRIDE |
97-
UINT64_C(1) << capability_list_length(),
98-
"cap_dac_override");
99-
test_capability_set_one(
100-
UINT64_C(1) << capability_list_length(), "");
101-
test_capability_set_one(
102-
UINT64_C(1) << CAP_CHOWN |
103-
UINT64_C(1) << CAP_DAC_OVERRIDE |
104-
UINT64_C(1) << CAP_DAC_READ_SEARCH |
105-
UINT64_C(1) << CAP_FOWNER |
106-
UINT64_C(1) << CAP_SETGID |
107-
UINT64_C(1) << CAP_SETUID |
108-
UINT64_C(1) << CAP_SYS_PTRACE |
109-
UINT64_C(1) << CAP_SYS_ADMIN |
110-
UINT64_C(1) << CAP_AUDIT_CONTROL |
111-
UINT64_C(1) << CAP_MAC_OVERRIDE |
112-
UINT64_C(1) << CAP_SYSLOG |
113-
UINT64_C(1) << (capability_list_length() + 1),
114-
"cap_chown cap_dac_override cap_dac_read_search cap_fowner "
115-
"cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin "
116-
"cap_audit_control cap_mac_override cap_syslog");
95+
test_capability_set_one(invalid_cap_set, "");
96+
97+
c = (UINT64_C(1) << CAP_DAC_OVERRIDE | invalid_cap_set);
98+
test_capability_set_one(c, "cap_dac_override");
99+
100+
c = (UINT64_C(1) << CAP_CHOWN |
101+
UINT64_C(1) << CAP_DAC_OVERRIDE |
102+
UINT64_C(1) << CAP_DAC_READ_SEARCH |
103+
UINT64_C(1) << CAP_FOWNER |
104+
UINT64_C(1) << CAP_SETGID |
105+
UINT64_C(1) << CAP_SETUID |
106+
UINT64_C(1) << CAP_SYS_PTRACE |
107+
UINT64_C(1) << CAP_SYS_ADMIN |
108+
UINT64_C(1) << CAP_AUDIT_CONTROL |
109+
UINT64_C(1) << CAP_MAC_OVERRIDE |
110+
UINT64_C(1) << CAP_SYSLOG |
111+
invalid_cap_set);
112+
test_capability_set_one(c, ("cap_chown cap_dac_override cap_dac_read_search cap_fowner "
113+
"cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin "
114+
"cap_audit_control cap_mac_override cap_syslog"));
117115
}
118116

119117
int main(int argc, char *argv[]) {
120118
test_cap_list();
121-
test_capability_set();
119+
test_capability_set_from_string();
120+
test_capability_set_to_string(0);
121+
122+
/* once the kernel supports 63 caps, there are no 'invalid' numbers
123+
* for us to test with */
124+
if (cap_last_cap() < 63)
125+
test_capability_set_to_string(all_capabilities() + 1);
122126

123127
return 0;
124128
}

0 commit comments

Comments
 (0)