]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-seccomp.c
nspawn: replace homegrown seccomp filter table largely with references to the existin...
[thirdparty/systemd.git] / src / nspawn / nspawn-seccomp.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2016 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <linux/netlink.h>
22 #include <sys/capability.h>
23 #include <sys/types.h>
24
25 #ifdef HAVE_SECCOMP
26 #include <seccomp.h>
27 #endif
28
29 #include "alloc-util.h"
30 #include "log.h"
31 #include "nspawn-seccomp.h"
32 #ifdef HAVE_SECCOMP
33 #include "seccomp-util.h"
34 #endif
35 #include "string-util.h"
36
37 #ifdef HAVE_SECCOMP
38
39 static int seccomp_add_default_syscall_filter(
40 scmp_filter_ctx ctx,
41 uint32_t arch,
42 uint64_t cap_list_retain) {
43
44 static const struct {
45 uint64_t capability;
46 const char* name;
47 } blacklist[] = {
48 { 0, "@obsolete" },
49 { 0, "@keyring" }, /* keyring is not namespaced */
50 { 0, "bpf" },
51 { 0, "kexec_file_load" },
52 { 0, "kexec_load" },
53 { 0, "lookup_dcookie" },
54 { 0, "open_by_handle_at" },
55 { 0, "perf_event_open" },
56 { 0, "quotactl" },
57 { 0, "@swap" },
58 { CAP_SYSLOG, "syslog" },
59 { CAP_SYS_MODULE, "@module" },
60 { CAP_SYS_PACCT, "acct" },
61 { CAP_SYS_PTRACE, "process_vm_readv" },
62 { CAP_SYS_PTRACE, "process_vm_writev" },
63 { CAP_SYS_PTRACE, "ptrace" },
64 { CAP_SYS_RAWIO, "@raw-io" },
65 { CAP_SYS_TIME, "@clock" },
66 };
67
68 int r, c = 0;
69 size_t i;
70
71 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
72 if (blacklist[i].capability != 0 && (cap_list_retain & (1ULL << blacklist[i].capability)))
73 continue;
74
75 r = seccomp_add_syscall_filter_item(ctx, blacklist[i].name, SCMP_ACT_ERRNO(EPERM));
76 if (r < 0)
77 /* If the system call is not known on this architecture, then that's fine, let's ignore it */
78 log_debug_errno(r, "Failed to add rule for system call %s, ignoring: %m", blacklist[i].name);
79 else
80 c++;
81 }
82
83 return c;
84 }
85
86 int setup_seccomp(uint64_t cap_list_retain) {
87 uint32_t arch;
88 int r;
89
90 if (!is_seccomp_available()) {
91 log_debug("SECCOMP features not detected in the kernel, disabling SECCOMP audit filter");
92 return 0;
93 }
94
95 SECCOMP_FOREACH_LOCAL_ARCH(arch) {
96 _cleanup_(seccomp_releasep) scmp_filter_ctx seccomp = NULL;
97 int n;
98
99 log_debug("Operating on architecture: %s", seccomp_arch_to_string(arch));
100
101 r = seccomp_init_for_arch(&seccomp, arch, SCMP_ACT_ALLOW);
102 if (r < 0)
103 return log_error_errno(r, "Failed to allocate seccomp object: %m");
104
105 n = seccomp_add_default_syscall_filter(seccomp, arch, cap_list_retain);
106 if (n < 0)
107 return n;
108
109 /*
110 Audit is broken in containers, much of the userspace audit hookup will fail if running inside a
111 container. We don't care and just turn off creation of audit sockets.
112
113 This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail with EAFNOSUPPORT which audit userspace uses
114 as indication that audit is disabled in the kernel.
115 */
116
117 r = seccomp_rule_add_exact(
118 seccomp,
119 SCMP_ACT_ERRNO(EAFNOSUPPORT),
120 SCMP_SYS(socket),
121 2,
122 SCMP_A0(SCMP_CMP_EQ, AF_NETLINK),
123 SCMP_A2(SCMP_CMP_EQ, NETLINK_AUDIT));
124 if (r < 0)
125 log_debug_errno(r, "Failed to add audit seccomp rule, ignoring: %m");
126 else
127 n++;
128
129 if (n <= 0) /* no rule added? then skip this architecture */
130 continue;
131
132 r = seccomp_load(seccomp);
133 if (IN_SET(r, -EPERM, -EACCES))
134 return log_error_errno(r, "Failed to install seccomp audit filter: %m");
135 if (r < 0)
136 log_debug_errno(r, "Failed to install filter set for architecture %s, skipping: %m", seccomp_arch_to_string(arch));
137 }
138
139 return 0;
140 }
141
142 #else
143
144 int setup_seccomp(uint64_t cap_list_retain) {
145 return 0;
146 }
147
148 #endif