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
),
63 SCMP_SYS(gettimeofday
),
68 SCMP_SYS(_llseek
), /* For POWER */
81 SCMP_SYS(prctl
), /* TODO restrict to just PR_SET_NAME? */
91 SCMP_SYS(removexattr
),
92 SCMP_SYS(restart_syscall
),
93 SCMP_SYS(rt_sigaction
),
94 SCMP_SYS(rt_sigprocmask
),
95 SCMP_SYS(rt_sigreturn
),
96 SCMP_SYS(sched_getattr
),
97 SCMP_SYS(sched_setattr
),
101 #ifdef __NR_setresgid32
102 SCMP_SYS(setresgid32
),
104 #ifdef __NR_setresuid32
105 SCMP_SYS(setresuid32
),
107 SCMP_SYS(set_robust_list
),
110 SCMP_SYS(time
), /* Rarely needed, except on static builds */
119 /* Syscalls used when --syslog is enabled */
120 static const int syscall_allowlist_syslog
[] = {
125 static void add_allowlist(scmp_filter_ctx ctx
, const int syscalls
[], size_t len
)
129 for (i
= 0; i
< len
; i
++) {
130 if (seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, syscalls
[i
], 0) != 0) {
131 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add syscall %d failed\n",
138 void setup_seccomp(bool enable_syslog
)
142 #ifdef SCMP_ACT_KILL_PROCESS
143 ctx
= seccomp_init(SCMP_ACT_KILL_PROCESS
);
144 /* Handle a newer libseccomp but an older kernel */
145 if (!ctx
&& errno
== EOPNOTSUPP
) {
146 ctx
= seccomp_init(SCMP_ACT_TRAP
);
149 ctx
= seccomp_init(SCMP_ACT_TRAP
);
152 fuse_log(FUSE_LOG_ERR
, "seccomp_init() failed\n");
156 add_allowlist(ctx
, syscall_allowlist
, G_N_ELEMENTS(syscall_allowlist
));
158 add_allowlist(ctx
, syscall_allowlist_syslog
,
159 G_N_ELEMENTS(syscall_allowlist_syslog
));
162 /* libvhost-user calls this for post-copy migration, we don't need it */
163 if (seccomp_rule_add(ctx
, SCMP_ACT_ERRNO(ENOSYS
),
164 SCMP_SYS(userfaultfd
), 0) != 0) {
165 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add userfaultfd failed\n");
169 if (seccomp_load(ctx
) < 0) {
170 fuse_log(FUSE_LOG_ERR
, "seccomp_load() failed\n");
174 seccomp_release(ctx
);