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"
15 /* Bodge for libseccomp 2.4.2 which broke ppoll */
16 #if !defined(__SNR_ppoll) && defined(__SNR_brk)
18 #define __SNR_ppoll __NR_ppoll
20 #define __SNR_ppoll __PNR_ppoll
24 static const int syscall_allowlist
[] = {
25 /* TODO ireg sem*() syscalls */
27 SCMP_SYS(capget
), /* For CAP_FSETID */
29 SCMP_SYS(clock_gettime
),
35 SCMP_SYS(copy_file_range
),
50 SCMP_SYS(fremovexattr
),
64 SCMP_SYS(gettimeofday
),
69 SCMP_SYS(_llseek
), /* For POWER */
82 SCMP_SYS(prctl
), /* TODO restrict to just PR_SET_NAME? */
92 SCMP_SYS(removexattr
),
93 SCMP_SYS(restart_syscall
),
95 SCMP_SYS(rseq
), /* required since glibc 2.35 */
97 SCMP_SYS(rt_sigaction
),
98 SCMP_SYS(rt_sigprocmask
),
99 SCMP_SYS(rt_sigreturn
),
100 SCMP_SYS(sched_getattr
),
101 SCMP_SYS(sched_setattr
),
105 #ifdef __NR_setresgid32
106 SCMP_SYS(setresgid32
),
108 #ifdef __NR_setresuid32
109 SCMP_SYS(setresuid32
),
111 SCMP_SYS(set_robust_list
),
116 SCMP_SYS(time
), /* Rarely needed, except on static builds */
126 /* Syscalls used when --syslog is enabled */
127 static const int syscall_allowlist_syslog
[] = {
132 static void add_allowlist(scmp_filter_ctx ctx
, const int syscalls
[], size_t len
)
136 for (i
= 0; i
< len
; i
++) {
137 if (seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, syscalls
[i
], 0) != 0) {
138 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add syscall %d failed\n",
145 void setup_seccomp(bool enable_syslog
)
149 #ifdef SCMP_ACT_KILL_PROCESS
150 ctx
= seccomp_init(SCMP_ACT_KILL_PROCESS
);
151 /* Handle a newer libseccomp but an older kernel */
152 if (!ctx
&& errno
== EOPNOTSUPP
) {
153 ctx
= seccomp_init(SCMP_ACT_TRAP
);
156 ctx
= seccomp_init(SCMP_ACT_TRAP
);
159 fuse_log(FUSE_LOG_ERR
, "seccomp_init() failed\n");
163 add_allowlist(ctx
, syscall_allowlist
, G_N_ELEMENTS(syscall_allowlist
));
165 add_allowlist(ctx
, syscall_allowlist_syslog
,
166 G_N_ELEMENTS(syscall_allowlist_syslog
));
169 /* libvhost-user calls this for post-copy migration, we don't need it */
170 if (seccomp_rule_add(ctx
, SCMP_ACT_ERRNO(ENOSYS
),
171 SCMP_SYS(userfaultfd
), 0) != 0) {
172 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add userfaultfd failed\n");
176 if (seccomp_load(ctx
) < 0) {
177 fuse_log(FUSE_LOG_ERR
, "seccomp_load() failed\n");
181 seccomp_release(ctx
);