Debian Codegraph @

src/core/bpf-devices.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fnmatch.h>
4 #include <linux/bpf.h>
5 #include <linux/bpf_insn.h>
6 #include <sys/stat.h>
7
8 #include "alloc-util.h"
9 #include "bpf-devices.h"
10 #include "bpf-program.h"
11 #include "cgroup.h"
12 #include "devnum-util.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "log.h"
16 #include "nulstr-util.h"
17 #include "parse-util.h"
18 #include "path-util.h"
19 #include "stat-util.h"
20 #include "string-util.h"
21
22 #define PASS_JUMP_OFF 4096
23
24 /* Ensure the high level flags we use and the low-level BPF flags exposed on the kernel are defined the same way */
25 assert_cc((unsigned) BPF_DEVCG_ACC_MKNOD == (unsigned) CGROUP_DEVICE_MKNOD);
26 assert_cc((unsigned) BPF_DEVCG_ACC_READ == (unsigned) CGROUP_DEVICE_READ);
27 assert_cc((unsigned) BPF_DEVCG_ACC_WRITE == (unsigned) CGROUP_DEVICE_WRITE);
28
29 static int bpf_prog_allow_list_device(
30 BPFProgram *prog,
31 char type,
32 unsigned major,
33 unsigned minor,
34 CGroupDevicePermissions p) {
35
36 int r;
37
38 assert(prog);
39
40 log_trace("%s: %c %u:%u %s", __func__, type, major, minor, cgroup_device_permissions_to_string(p));
41
42 if (p <= 0 || p >= _CGROUP_DEVICE_PERMISSIONS_MAX)
43 return -EINVAL;
44
45 assert(IN_SET(type, 'b', 'c'));
46 const int bpf_type = type == 'c' ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK;
47
48 const struct bpf_insn insn[] = {
49 BPF_MOV32_REG(BPF_REG_1, BPF_REG_3),
50 BPF_ALU32_IMM(BPF_AND, BPF_REG_1, p),
51 BPF_JMP_REG(BPF_JNE, BPF_REG_1, BPF_REG_3, 4), /* compare access type */
52
53 BPF_JMP_IMM(BPF_JNE, BPF_REG_2, bpf_type, 3), /* compare device type */
54 BPF_JMP_IMM(BPF_JNE, BPF_REG_4, major, 2), /* compare major */
55 BPF_JMP_IMM(BPF_JNE, BPF_REG_5, minor, 1), /* compare minor */
56 BPF_JMP_A(PASS_JUMP_OFF), /* jump to PASS */
57 };
58
59 if (p == _CGROUP_DEVICE_PERMISSIONS_ALL)
60 r = bpf_program_add_instructions(prog, insn + 3, ELEMENTSOF(insn) - 3);
61 else
62 r = bpf_program_add_instructions(prog, insn, ELEMENTSOF(insn));
63 if (r < 0)
64 return log_error_errno(r, "Extending device control BPF program failed: %m");
65
66 return 1; /* return 1 → we did something */
67 }
68
69 static int bpf_prog_allow_list_major(
70 BPFProgram *prog,
71 char type,
72 unsigned major,
73 CGroupDevicePermissions p) {
74
75 int r;
76
77 assert(prog);
78
79 log_trace("%s: %c %u:* %s", __func__, type, major, cgroup_device_permissions_to_string(p));
80
81 if (p <= 0 || p >= _CGROUP_DEVICE_PERMISSIONS_MAX)
82 return -EINVAL;
83
84 assert(IN_SET(type, 'b', 'c'));
85 const int bpf_type = type == 'c' ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK;
86
87 const struct bpf_insn insn[] = {
88 BPF_MOV32_REG(BPF_REG_1, BPF_REG_3),
89 BPF_ALU32_IMM(BPF_AND, BPF_REG_1, p),
90 BPF_JMP_REG(BPF_JNE, BPF_REG_1, BPF_REG_3, 3), /* compare access type */
91
92 BPF_JMP_IMM(BPF_JNE, BPF_REG_2, bpf_type, 2), /* compare device type */
93 BPF_JMP_IMM(BPF_JNE, BPF_REG_4, major, 1), /* compare major */
94 BPF_JMP_A(PASS_JUMP_OFF), /* jump to PASS */
95 };
96
97 if (p == _CGROUP_DEVICE_PERMISSIONS_ALL)
98 r = bpf_program_add_instructions(prog, insn + 3, ELEMENTSOF(insn) - 3);
99 else
100 r = bpf_program_add_instructions(prog, insn, ELEMENTSOF(insn));
101 if (r < 0)
102 return log_error_errno(r, "Extending device control BPF program failed: %m");
103
104 return 1; /* return 1 → we did something */
105 }
106
107 static int bpf_prog_allow_list_class(
108 BPFProgram *prog,
109 char type,
110 CGroupDevicePermissions p) {
111
112 int r;
113
114 assert(prog);
115
116 log_trace("%s: %c *:* %s", __func__, type, cgroup_device_permissions_to_string(p));
117
118 if (p <= 0 || p >= _CGROUP_DEVICE_PERMISSIONS_MAX)
119 return -EINVAL;
120
121 assert(IN_SET(type, 'b', 'c'));
122 const int bpf_type = type == 'c' ? BPF_DEVCG_DEV_CHAR : BPF_DEVCG_DEV_BLOCK;
123
124 const struct bpf_insn insn[] = {
125 BPF_MOV32_REG(BPF_REG_1, BPF_REG_3),
126 BPF_ALU32_IMM(BPF_AND, BPF_REG_1, p),
127 BPF_JMP_REG(BPF_JNE, BPF_REG_1, BPF_REG_3, 2), /* compare access type */
128
129 BPF_JMP_IMM(BPF_JNE, BPF_REG_2, bpf_type, 1), /* compare device type */
130 BPF_JMP_A(PASS_JUMP_OFF), /* jump to PASS */
131 };
132
133 if (p == _CGROUP_DEVICE_PERMISSIONS_ALL)
134 r = bpf_program_add_instructions(prog, insn + 3, ELEMENTSOF(insn) - 3);
135 else
136 r = bpf_program_add_instructions(prog, insn, ELEMENTSOF(insn));
137 if (r < 0)
138 return log_error_errno(r, "Extending device control BPF program failed: %m");
139
140 return 1; /* return 1 → we did something */
141 }
142
143 int bpf_devices_cgroup_init(
144 BPFProgram **ret,
145 CGroupDevicePolicy policy,
146 bool allow_list) {
147
148 const struct bpf_insn pre_insn[] = {
149 /* load device type to r2 */
150 BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1,
151 offsetof(struct bpf_cgroup_dev_ctx, access_type)),
152 BPF_ALU32_IMM(BPF_AND, BPF_REG_2, 0xFFFF),
153
154 /* load access type to r3 */
155 BPF_LDX_MEM(BPF_W, BPF_REG_3, BPF_REG_1,
156 offsetof(struct bpf_cgroup_dev_ctx, access_type)),
157 BPF_ALU32_IMM(BPF_RSH, BPF_REG_3, 16),
158
159 /* load major number to r4 */
160 BPF_LDX_MEM(BPF_W, BPF_REG_4, BPF_REG_1,
161 offsetof(struct bpf_cgroup_dev_ctx, major)),
162
163 /* load minor number to r5 */
164 BPF_LDX_MEM(BPF_W, BPF_REG_5, BPF_REG_1,
165 offsetof(struct bpf_cgroup_dev_ctx, minor)),
166 };
167
168 _cleanup_(bpf_program_freep) BPFProgram *prog = NULL;
169 int r;
170
171 assert(ret);
172
173 if (policy == CGROUP_DEVICE_POLICY_AUTO && !allow_list) {
174 *ret = NULL;
175 return 0;
176 }
177
178 r = bpf_program_new(BPF_PROG_TYPE_CGROUP_DEVICE, "sd_devices", &prog);
179 if (r < 0)
180 return log_error_errno(r, "Loading device control BPF program failed: %m");
181
182 if (policy == CGROUP_DEVICE_POLICY_CLOSED || allow_list) {
183 r = bpf_program_add_instructions(prog, pre_insn, ELEMENTSOF(pre_insn));
184 if (r < 0)
185 return log_error_errno(r, "Extending device control BPF program failed: %m");
186 }
187
188 *ret = TAKE_PTR(prog);
189 return 1;
190 }
191
192 int bpf_devices_apply_policy(
193 BPFProgram **prog,
194 CGroupDevicePolicy policy,
195 bool allow_list,
196 const char *cgroup_path,
197 BPFProgram **prog_installed) {
198
199 _cleanup_free_ char *controller_path = NULL;
200 int r;
201
202 /* This will assign *prog_installed if everything goes well. */
203
204 assert(prog);
205 if (!*prog)
206 goto finish;
207
208 const bool deny_everything = policy == CGROUP_DEVICE_POLICY_STRICT && !allow_list;
209
210 const struct bpf_insn post_insn[] = {
211 /* return DENY */
212 BPF_MOV64_IMM(BPF_REG_0, 0),
213 BPF_JMP_A(1),
214 };
215
216 const struct bpf_insn exit_insn[] = {
217 /* finally return DENY if deny_everything else ALLOW */
218 BPF_MOV64_IMM(BPF_REG_0, deny_everything ? 0 : 1),
219 BPF_EXIT_INSN()
220 };
221
222 if (!deny_everything) {
223 r = bpf_program_add_instructions(*prog, post_insn, ELEMENTSOF(post_insn));
224 if (r < 0)
225 return log_error_errno(r, "Extending device control BPF program failed: %m");
226
227 /* Fixup PASS_JUMP_OFF jump offsets. */
228 for (size_t off = 0; off < (*prog)->n_instructions; off++) {
229 struct bpf_insn *ins = &((*prog)->instructions[off]);
230
231 if (ins->code == (BPF_JMP | BPF_JA) && ins->off == PASS_JUMP_OFF)
232 ins->off = (*prog)->n_instructions - off - 1;
233 }
234 }
235
236 r = bpf_program_add_instructions(*prog, exit_insn, ELEMENTSOF(exit_insn));
237 if (r < 0)
238 return log_error_errno(r, "Extending device control BPF program failed: %m");
239
240 r = cg_get_path(cgroup_path, /* suffix= */ NULL, &controller_path);
241 if (r < 0)
242 return log_error_errno(r, "Failed to determine cgroup path: %m");
243
244 r = bpf_program_cgroup_attach(*prog, BPF_CGROUP_DEVICE, controller_path, BPF_F_ALLOW_MULTI);
245 if (r < 0)
246 return log_error_errno(r, "Attaching device control BPF program to cgroup %s failed: %m",
247 empty_to_root(cgroup_path));
248
249 finish:
250 /* Unref the old BPF program (which will implicitly detach it) right before attaching the new program. */
251 if (prog_installed) {
252 bpf_program_free(*prog_installed);
253 *prog_installed = TAKE_PTR(*prog);
254 }
255 return 0;
256 }
257
258 static int allow_list_device_pattern(
259 BPFProgram *prog,
260 const char *path,
261 char type,
262 unsigned major,
263 unsigned minor,
264 CGroupDevicePermissions p) {
265
266 assert(IN_SET(type, 'b', 'c'));
267
268 if (!prog)
269 return 0;
270
271 if (major != UINT_MAX && minor != UINT_MAX)
272 return bpf_prog_allow_list_device(prog, type, major, minor, p);
273 if (major != UINT_MAX)
274 return bpf_prog_allow_list_major(prog, type, major, p);
275
276 return bpf_prog_allow_list_class(prog, type, p);
277 }
278
279 int bpf_devices_allow_list_device(
280 BPFProgram *prog,
281 const char *path,
282 const char *node,
283 CGroupDevicePermissions p) {
284
285 mode_t mode;
286 dev_t rdev;
287 int r;
288
289 assert(path);
290 assert(p >= 0 && p < _CGROUP_DEVICE_PERMISSIONS_MAX);
291
292 log_trace("%s: %s %s", __func__, node, cgroup_device_permissions_to_string(p));
293
294 /* Some special handling for /dev/block/%u:%u, /dev/char/%u:%u, /run/systemd/inaccessible/chr and
295 * /run/systemd/inaccessible/blk paths. Instead of stat()ing these we parse out the major/minor directly. This
296 * means clients can use these path without the device node actually around */
297 r = device_path_parse_major_minor(node, &mode, &rdev);
298 if (r < 0) {
299 if (r != -ENODEV)
300 return log_warning_errno(r, "Couldn't parse major/minor from device path '%s': %m", node);
301
302 struct stat st;
303 if (stat(node, &st) < 0) {
304 if (errno == ENOENT) {
305 log_debug_errno(errno, "Device '%s' does not exist, skipping.", node);
306 return 0; /* returning 0 means → skipped */
307 }
308
309 return log_warning_errno(errno, "Couldn't stat device %s: %m", node);
310 }
311
312 r = stat_verify_device_node(&st);
313 if (r < 0)
314 return log_warning_errno(r, "'%s' is not a device node.", node);
315
316 mode = st.st_mode;
317 rdev = (dev_t) st.st_rdev;
318 }
319
320 return allow_list_device_pattern(prog, path, S_ISCHR(mode) ? 'c' : 'b', major(rdev), minor(rdev), p);
321 }
322
323 int bpf_devices_allow_list_major(
324 BPFProgram *prog,
325 const char *path,
326 const char *name,
327 char type,
328 CGroupDevicePermissions permissions) {
329
330 unsigned major;
331 int r;
332
333 assert(path);
334 assert(IN_SET(type, 'b', 'c'));
335 assert(permissions >= 0 && permissions < _CGROUP_DEVICE_PERMISSIONS_MAX);
336
337 if (streq(name, "*"))
338 /* If the name is a wildcard, then apply this list to all devices of this type */
339 return allow_list_device_pattern(prog, path, type, /* major= */ UINT_MAX, /* minor= */ UINT_MAX, permissions);
340
341 if (safe_atou(name, &major) >= 0 && DEVICE_MAJOR_VALID(major))
342 /* The name is numeric and suitable as major. In that case, let's take its major, and create
343 * the entry directly. */
344 return allow_list_device_pattern(prog, path, type, major, /* minor= */ UINT_MAX, permissions);
345
346 _cleanup_fclose_ FILE *f = NULL;
347 bool good = false, any = false;
348
349 f = fopen("/proc/devices", "re");
350 if (!f)
351 return log_warning_errno(errno, "Cannot open /proc/devices to resolve %s: %m", name);
352
353 for (;;) {
354 _cleanup_free_ char *line = NULL;
355 char *w, *p;
356
357 r = read_line(f, LONG_LINE_MAX, &line);
358 if (r < 0)
359 return log_warning_errno(r, "Failed to read /proc/devices: %m");
360 if (r == 0)
361 break;
362
363 if (type == 'c' && streq(line, "Character devices:")) {
364 good = true;
365 continue;
366 }
367
368 if (type == 'b' && streq(line, "Block devices:")) {
369 good = true;
370 continue;
371 }
372
373 if (isempty(line)) {
374 good = false;
375 continue;
376 }
377
378 if (!good)
379 continue;
380
381 p = strstrip(line);
382
383 w = strpbrk(p, WHITESPACE);
384 if (!w)
385 continue;
386 *w = 0;
387
388 r = safe_atou(p, &major);
389 if (r < 0)
390 continue;
391 if (major <= 0)
392 continue;
393
394 w++;
395 w += strspn(w, WHITESPACE);
396
397 if (fnmatch(name, w, 0) != 0)
398 continue;
399
400 if (allow_list_device_pattern(prog, path, type, major, /* minor= */ UINT_MAX, permissions) > 0)
401 any = true;
402 }
403
404 if (!any)
405 return log_debug_errno(SYNTHETIC_ERRNO(ENOENT),
406 "Device allow list pattern \"%s\" did not match anything.", name);
407
408 return any;
409 }
410
411 int bpf_devices_allow_list_static(
412 BPFProgram *prog,
413 const char *path) {
414
415 static const char auto_devices[] =
416 "/dev/null\0" "rwm\0"
417 "/dev/zero\0" "rwm\0"
418 "/dev/full\0" "rwm\0"
419 "/dev/random\0" "rwm\0"
420 "/dev/urandom\0" "rwm\0"
421 "/dev/tty\0" "rwm\0"
422 "/dev/ptmx\0" "rwm\0"
423 /* Allow /run/systemd/inaccessible/{chr,blk} devices for mapping InaccessiblePaths */
424 "/run/systemd/inaccessible/chr\0" "rwm\0"
425 "/run/systemd/inaccessible/blk\0" "rwm\0";
426 int r = 0, k;
427
428 NULSTR_FOREACH_PAIR(node, acc, auto_devices) {
429 k = bpf_devices_allow_list_device(prog, path, node, cgroup_device_permissions_from_string(acc));
430 if ((r >= 0 && k < 0) || (r >= 0 && k > 0))
431 r = k;
432 }
433
434 /* PTS (/dev/pts) devices may not be duplicated, but accessed */
435 k = bpf_devices_allow_list_major(prog, path, "pts", 'c', CGROUP_DEVICE_READ|CGROUP_DEVICE_WRITE);
436 if ((r >= 0 && k < 0) || (r >= 0 && k > 0))
437 r = k;
438
439 return r;
440 }