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_whitelist
[] = {
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
),
80 SCMP_SYS(prctl
), /* TODO restrict to just PR_SET_NAME? */
90 SCMP_SYS(removexattr
),
91 SCMP_SYS(rt_sigaction
),
92 SCMP_SYS(rt_sigprocmask
),
93 SCMP_SYS(rt_sigreturn
),
94 SCMP_SYS(sched_getattr
),
95 SCMP_SYS(sched_setattr
),
99 #ifdef __NR_setresgid32
100 SCMP_SYS(setresgid32
),
102 #ifdef __NR_setresuid32
103 SCMP_SYS(setresuid32
),
105 SCMP_SYS(set_robust_list
),
108 SCMP_SYS(time
), /* Rarely needed, except on static builds */
117 /* Syscalls used when --syslog is enabled */
118 static const int syscall_whitelist_syslog
[] = {
123 static void add_whitelist(scmp_filter_ctx ctx
, const int syscalls
[], size_t len
)
127 for (i
= 0; i
< len
; i
++) {
128 if (seccomp_rule_add(ctx
, SCMP_ACT_ALLOW
, syscalls
[i
], 0) != 0) {
129 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add syscall %d failed\n",
136 void setup_seccomp(bool enable_syslog
)
140 #ifdef SCMP_ACT_KILL_PROCESS
141 ctx
= seccomp_init(SCMP_ACT_KILL_PROCESS
);
142 /* Handle a newer libseccomp but an older kernel */
143 if (!ctx
&& errno
== EOPNOTSUPP
) {
144 ctx
= seccomp_init(SCMP_ACT_TRAP
);
147 ctx
= seccomp_init(SCMP_ACT_TRAP
);
150 fuse_log(FUSE_LOG_ERR
, "seccomp_init() failed\n");
154 add_whitelist(ctx
, syscall_whitelist
, G_N_ELEMENTS(syscall_whitelist
));
156 add_whitelist(ctx
, syscall_whitelist_syslog
,
157 G_N_ELEMENTS(syscall_whitelist_syslog
));
160 /* libvhost-user calls this for post-copy migration, we don't need it */
161 if (seccomp_rule_add(ctx
, SCMP_ACT_ERRNO(ENOSYS
),
162 SCMP_SYS(userfaultfd
), 0) != 0) {
163 fuse_log(FUSE_LOG_ERR
, "seccomp_rule_add userfaultfd failed\n");
167 if (seccomp_load(ctx
) < 0) {
168 fuse_log(FUSE_LOG_ERR
, "seccomp_load() failed\n");
172 seccomp_release(ctx
);