2 * Seccomp sandboxing for virtiofsd
4 * Copyright (C) 2019 Red Hat, Inc.
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #include "qemu/osdep.h"
10 #include "passthrough_seccomp.h"
18 /* Bodge for libseccomp 2.4.2 which broke ppoll */
19 #if !defined(__SNR_ppoll) && defined(__SNR_brk)
21 #define __SNR_ppoll __NR_ppoll
23 #define __SNR_ppoll __PNR_ppoll
27 static const int syscall_whitelist
[] = {
28 /* TODO ireg sem*() syscalls */
30 SCMP_SYS(capget
), /* For CAP_FSETID */
32 SCMP_SYS(clock_gettime
),
38 SCMP_SYS(copy_file_range
),
53 SCMP_SYS(fremovexattr
),
66 SCMP_SYS(gettimeofday
),
83 SCMP_SYS(prctl
), /* TODO restrict to just PR_SET_NAME? */
93 SCMP_SYS(removexattr
),
94 SCMP_SYS(rt_sigaction
),
95 SCMP_SYS(rt_sigprocmask
),
96 SCMP_SYS(rt_sigreturn
),
97 SCMP_SYS(sched_getattr
),
98 SCMP_SYS(sched_setattr
),
102 #ifdef __NR_setresgid32
103 SCMP_SYS(setresgid32
),
105 #ifdef __NR_setresuid32
106 SCMP_SYS(setresuid32
),
108 SCMP_SYS(set_robust_list
),
111 SCMP_SYS(time
), /* Rarely needed, except on static builds */
120 /* Syscalls used when --syslog is enabled */
121 static const int syscall_whitelist_syslog
[] = {
126 static void add_whitelist(scmp_filter_ctx ctx
, const int syscalls
[], size_t len
)
130 for (i
= 0; i
< len
; i
++) {
131 if (seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, syscalls
[i
], 0) != 0) {
132 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add syscall %d failed\n",
139 void setup_seccomp(bool enable_syslog
)
143 #ifdef SCMP_ACT_KILL_PROCESS
144 ctx
= seccomp_init(SCMP_ACT_KILL_PROCESS
);
145 /* Handle a newer libseccomp but an older kernel */
146 if (!ctx
&& errno
== EOPNOTSUPP
) {
147 ctx
= seccomp_init(SCMP_ACT_TRAP
);
150 ctx
= seccomp_init(SCMP_ACT_TRAP
);
153 fuse_log(FUSE_LOG_ERR
, "seccomp_init() failed\n");
157 add_whitelist(ctx
, syscall_whitelist
, G_N_ELEMENTS(syscall_whitelist
));
159 add_whitelist(ctx
, syscall_whitelist_syslog
,
160 G_N_ELEMENTS(syscall_whitelist_syslog
));
163 /* libvhost-user calls this for post-copy migration, we don't need it */
164 if (seccomp_rule_add(ctx
, SCMP_ACT_ERRNO(ENOSYS
),
165 SCMP_SYS(userfaultfd
), 0) != 0) {
166 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add userfaultfd failed\n");
170 if (seccomp_load(ctx
) < 0) {
171 fuse_log(FUSE_LOG_ERR
, "seccomp_load() failed\n");
175 seccomp_release(ctx
);