Skip to content

Commit a9aac7d

Browse files
committed
core: also split out helper to handle static device nodes
1 parent 124e05b commit a9aac7d

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

src/core/bpf-devices.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "bpf-program.h"
88
#include "fd-util.h"
99
#include "fileio.h"
10+
#include "nulstr-util.h"
1011
#include "parse-util.h"
1112
#include "stat-util.h"
1213
#include "stdio-util.h"
@@ -417,3 +418,32 @@ int bpf_devices_whitelist_major(BPFProgram *prog, const char *path, const char *
417418

418419
return 0;
419420
}
421+
422+
int bpf_devices_whitelist_static(BPFProgram *prog, const char *path) {
423+
static const char auto_devices[] =
424+
"/dev/null\0" "rwm\0"
425+
"/dev/zero\0" "rwm\0"
426+
"/dev/full\0" "rwm\0"
427+
"/dev/random\0" "rwm\0"
428+
"/dev/urandom\0" "rwm\0"
429+
"/dev/tty\0" "rwm\0"
430+
"/dev/ptmx\0" "rwm\0"
431+
/* Allow /run/systemd/inaccessible/{chr,blk} devices for mapping InaccessiblePaths */
432+
"/run/systemd/inaccessible/chr\0" "rwm\0"
433+
"/run/systemd/inaccessible/blk\0" "rwm\0";
434+
int r = 0, k;
435+
436+
const char *node, *acc;
437+
NULSTR_FOREACH_PAIR(node, acc, auto_devices) {
438+
k = bpf_devices_whitelist_device(prog, path, node, acc);
439+
if (r >= 0 && k < 0)
440+
r = k;
441+
}
442+
443+
/* PTS (/dev/pts) devices may not be duplicated, but accessed */
444+
k = bpf_devices_whitelist_major(prog, path, "pts", 'c', "rw");
445+
if (r >= 0 && k < 0)
446+
r = k;
447+
448+
return r;
449+
}

src/core/bpf-devices.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ int bpf_devices_apply_policy(Unit *u, BPFProgram *prog, CGroupDevicePolicy polic
1313
int bpf_devices_supported(void);
1414
int bpf_devices_whitelist_device(BPFProgram *prog, const char *path, const char *node, const char *acc);
1515
int bpf_devices_whitelist_major(BPFProgram *prog, const char *path, const char *name, char type, const char *acc);
16+
int bpf_devices_whitelist_static(BPFProgram *prog, const char *path);

src/core/cgroup.c

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "fd-util.h"
1717
#include "fileio.h"
1818
#include "fs-util.h"
19-
#include "nulstr-util.h"
2019
#include "parse-util.h"
2120
#include "path-util.h"
2221
#include "process-util.h"
@@ -1254,26 +1253,8 @@ static void cgroup_context_apply(
12541253
}
12551254

12561255
if (c->device_policy == CGROUP_DEVICE_POLICY_CLOSED ||
1257-
(c->device_policy == CGROUP_DEVICE_POLICY_AUTO && c->device_allow)) {
1258-
static const char auto_devices[] =
1259-
"/dev/null\0" "rwm\0"
1260-
"/dev/zero\0" "rwm\0"
1261-
"/dev/full\0" "rwm\0"
1262-
"/dev/random\0" "rwm\0"
1263-
"/dev/urandom\0" "rwm\0"
1264-
"/dev/tty\0" "rwm\0"
1265-
"/dev/ptmx\0" "rwm\0"
1266-
/* Allow /run/systemd/inaccessible/{chr,blk} devices for mapping InaccessiblePaths */
1267-
"/run/systemd/inaccessible/chr\0" "rwm\0"
1268-
"/run/systemd/inaccessible/blk\0" "rwm\0";
1269-
1270-
const char *node, *acc;
1271-
NULSTR_FOREACH_PAIR(node, acc, auto_devices)
1272-
(void) bpf_devices_whitelist_device(prog, path, node, acc);
1273-
1274-
/* PTS (/dev/pts) devices may not be duplicated, but accessed */
1275-
(void) bpf_devices_whitelist_major(prog, path, "pts", 'c', "rw");
1276-
}
1256+
(c->device_policy == CGROUP_DEVICE_POLICY_AUTO && c->device_allow))
1257+
(void) bpf_devices_whitelist_static(prog, path);
12771258

12781259
LIST_FOREACH(device_allow, a, c->device_allow) {
12791260
char acc[4], *val;

0 commit comments

Comments
 (0)