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"
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
),
52 SCMP_SYS(fremovexattr
),
65 SCMP_SYS(gettimeofday
),
81 SCMP_SYS(prctl
), /* TODO restrict to just PR_SET_NAME? */
91 SCMP_SYS(removexattr
),
92 SCMP_SYS(rt_sigaction
),
93 SCMP_SYS(rt_sigprocmask
),
94 SCMP_SYS(rt_sigreturn
),
98 #ifdef __NR_setresgid32
99 SCMP_SYS(setresgid32
),
101 #ifdef __NR_setresuid32
102 SCMP_SYS(setresuid32
),
104 SCMP_SYS(set_robust_list
),
107 SCMP_SYS(time
), /* Rarely needed, except on static builds */
116 /* Syscalls used when --syslog is enabled */
117 static const int syscall_whitelist_syslog
[] = {
121 static void add_whitelist(scmp_filter_ctx ctx
, const int syscalls
[], size_t len
)
125 for (i
= 0; i
< len
; i
++) {
126 if (seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, syscalls
[i
], 0) != 0) {
127 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add syscall %d failed\n",
134 void setup_seccomp(bool enable_syslog
)
138 #ifdef SCMP_ACT_KILL_PROCESS
139 ctx
= seccomp_init(SCMP_ACT_KILL_PROCESS
);
140 /* Handle a newer libseccomp but an older kernel */
141 if (!ctx
&& errno
== EOPNOTSUPP
) {
142 ctx
= seccomp_init(SCMP_ACT_TRAP
);
145 ctx
= seccomp_init(SCMP_ACT_TRAP
);
148 fuse_log(FUSE_LOG_ERR
, "seccomp_init() failed\n");
152 add_whitelist(ctx
, syscall_whitelist
, G_N_ELEMENTS(syscall_whitelist
));
154 add_whitelist(ctx
, syscall_whitelist_syslog
,
155 G_N_ELEMENTS(syscall_whitelist_syslog
));
158 /* libvhost-user calls this for post-copy migration, we don't need it */
159 if (seccomp_rule_add(ctx
, SCMP_ACT_ERRNO(ENOSYS
),
160 SCMP_SYS(userfaultfd
), 0) != 0) {
161 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add userfaultfd failed\n");
165 if (seccomp_load(ctx
) < 0) {
166 fuse_log(FUSE_LOG_ERR
, "seccomp_load() failed\n");
170 seccomp_release(ctx
);