Skip to content

Commit 6088cef

Browse files
committed
basic/cap-list: report empty capability set as ""
$ systemctl show systemd-journald -p CapabilityBoundingSet,AmbientCapabilities CapabilityBoundingSet=cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_setgid ... AmbientCapabilities=(null) ↓ $ systemctl show systemd-journald -p CapabilityBoundingSet,AmbientCapabilities CapabilityBoundingSet=cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_setgid ... AmbientCapabilities= Partially fixes systemd#6511. Add some basic tests for the printing function.
1 parent efaa317 commit 6088cef

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/basic/cap-list.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ int capability_set_to_string_alloc(uint64_t set, char **s) {
8686

8787
add = strlen(p);
8888

89-
if (!GREEDY_REALLOC0(str, allocated, n + add + 2))
89+
if (!GREEDY_REALLOC(str, allocated, n + add + 2))
9090
return -ENOMEM;
9191

9292
strcpy(mempcpy(str + n, p, add), " ");
9393
n += add + 1;
9494
}
9595

96-
if (n != 0)
97-
str[n - 1] = '\0';
96+
if (!GREEDY_REALLOC(str, allocated, n + 1))
97+
return -ENOMEM;
98+
99+
str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
98100

99101
*s = str;
100102
str = NULL;

src/test/test-cap-list.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "capability-util.h"
2525
#include "fileio.h"
2626
#include "parse-util.h"
27+
#include "string-util.h"
2728
#include "util.h"
2829

2930
/* verify the capability parser */
@@ -102,10 +103,24 @@ static void test_last_cap_probe(void) {
102103
assert_se(p == cap_last_cap());
103104
}
104105

106+
static void test_capability_set_to_string_alloc(void) {
107+
_cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL;
108+
109+
assert_se(capability_set_to_string_alloc(0u, &t1) == 0);
110+
assert_se(streq(t1, ""));
111+
112+
assert_se(capability_set_to_string_alloc(1u<<CAP_DAC_OVERRIDE, &t2) == 0);
113+
assert_se(streq(t2, "cap_dac_override"));
114+
115+
assert_se(capability_set_to_string_alloc(UINT64_C(1)<<CAP_CHOWN | UINT64_C(1)<<CAP_DAC_OVERRIDE | UINT64_C(1)<<CAP_DAC_READ_SEARCH | UINT64_C(1)<<CAP_FOWNER | UINT64_C(1)<<CAP_SETGID | UINT64_C(1)<<CAP_SETUID | UINT64_C(1)<<CAP_SYS_PTRACE | UINT64_C(1)<<CAP_SYS_ADMIN | UINT64_C(1)<<CAP_AUDIT_CONTROL | UINT64_C(1)<<CAP_MAC_OVERRIDE | UINT64_C(1)<<CAP_SYSLOG, &t3) == 0);
116+
assert_se(streq(t3, "cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin cap_audit_control cap_mac_override cap_syslog"));
117+
}
118+
105119
int main(int argc, char *argv[]) {
106120
test_cap_list();
107121
test_last_cap_file();
108122
test_last_cap_probe();
123+
test_capability_set_to_string_alloc();
109124

110125
return 0;
111126
}

0 commit comments

Comments
 (0)