Debian Codegraph @

src/basic/capability-list.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "bitfield.h"
7 #include "capability-list.h"
8 #include "capability-util.h"
9 #include "extract-word.h"
10 #include "log.h"
11 #include "parse-util.h"
12 #include "string-util.h"
13 #include "strv.h"
14
15 static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
16
17 #include "capability-from-name.inc"
18 #include "capability-to-name.inc"
19
20 const char* capability_to_name(int id) {
21 if (id < 0)
22 return NULL;
23 if ((unsigned) id >= capability_list_length())
24 return NULL;
25
26 return capability_names[id];
27 }
28
29 const char* capability_to_string(int id, char buf[static CAPABILITY_TO_STRING_MAX]) {
30 const char *p;
31
32 if (id < 0)
33 return NULL;
34 if (id > CAP_LIMIT) /* refuse caps > 62 since we can't store them in a uint64_t mask anymore, and still retain UINT64_MAX as marker for "unset" */
35 return NULL;
36
37 p = capability_to_name(id);
38 if (p)
39 return p;
40
41 sprintf(buf, "0x%x", (unsigned) id); /* numerical fallback */
42 return buf;
43 }
44
45 int capability_from_name(const char *name) {
46 const struct capability_name *sc;
47 int r, i;
48
49 assert(name);
50
51 /* Try to parse numeric capability */
52 r = safe_atoi(name, &i);
53 if (r >= 0) {
54 if (i < 0 || i > CAP_LIMIT)
55 return -EINVAL;
56
57 return i;
58 }
59
60 /* Try to parse string capability */
61 sc = lookup_capability(name, strlen(name));
62 if (!sc)
63 return -EINVAL;
64
65 return sc->id;
66 }
67
68 /* This is the number of capability names we are *compiled* with. For the max capability number of the
69 * currently-running kernel, use cap_last_cap(). Note that this one returns the size of the array, i.e. one
70 * value larger than the last known capability. This is different from cap_last_cap() which returns the
71 * highest supported capability. Hence with everyone agreeing on the same capabilities list, this function
72 * will return one higher than cap_last_cap(). */
73 unsigned capability_list_length(void) {
74 return MIN((unsigned) ELEMENTSOF(capability_names), (unsigned) (CAP_LIMIT + 1));
75 }
76
77 int capability_set_to_string(uint64_t set, char **ret) {
78 _cleanup_free_ char *str = NULL;
79
80 assert(ret);
81
82 for (unsigned i = 0; i <= cap_last_cap(); i++) {
83 const char *p;
84
85 if (!BIT_SET(set, i))
86 continue;
87
88 p = CAPABILITY_TO_STRING(i);
89 assert(p);
90
91 if (!strextend_with_separator(&str, " ", p))
92 return -ENOMEM;
93 }
94
95 if (!str) {
96 str = new0(char, 1);
97 if (!str)
98 return -ENOMEM;
99 }
100
101 *ret = TAKE_PTR(str);
102 return 0;
103 }
104
105 int capability_set_to_string_negative(uint64_t set, char **ret) {
106 _cleanup_free_ char *a = NULL, *b = NULL;
107 int r;
108
109 assert(ret);
110
111 /* Format the specified capability mask both in positive way (i.e. just listing caps) and in negative
112 * way (i.e. listing only caps that are missing from the full set) and return the shorter version of
113 * the two. */
114
115 r = capability_set_to_string(set, &a);
116 if (r < 0)
117 return r;
118
119 r = capability_set_to_string(~set & all_capabilities(), &b);
120 if (r < 0)
121 return r;
122
123 if (strlen(a) <= 1 + strlen(b))
124 *ret = TAKE_PTR(a);
125 else {
126 char *c = strjoin("~", b);
127 if (!c)
128 return -ENOMEM;
129
130 *ret = c;
131 }
132
133 return 0;
134 }
135
136 int capability_set_to_strv(uint64_t set, char ***ret) {
137 _cleanup_strv_free_ char **l = NULL;
138 int r;
139
140 assert(ret);
141
142 for (unsigned i = 0; i <= cap_last_cap(); i++) {
143 const char *p;
144
145 if (!BIT_SET(set, i))
146 continue;
147
148 p = CAPABILITY_TO_STRING(i);
149 assert(p);
150
151 r = strv_extend(&l, p);
152 if (r < 0)
153 return r;
154 }
155
156 *ret = TAKE_PTR(l);
157 return 0;
158 }
159
160 int capability_set_from_string(const char *s, uint64_t *ret) {
161 uint64_t val = 0;
162 bool good = true;
163
164 for (const char *p = s;;) {
165 _cleanup_free_ char *word = NULL;
166 int r;
167
168 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
169 if (r < 0)
170 return r;
171 if (r == 0)
172 break;
173
174 r = capability_from_name(word);
175 if (r < 0) {
176 log_debug_errno(r, "Failed to parse capability '%s', ignoring: %m", word);
177 good = false;
178 } else
179 val |= UINT64_C(1) << r;
180 }
181
182 if (ret)
183 *ret = val;
184
185 return good;
186 }