2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions
5 * 1. Redistributions of source code must retain the above copyright
6 * notice immediately at the beginning of the file, without modification,
7 * this list of conditions, and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
16 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 FILE_RCSID("@(#)$File: seccomp.c,v 1.2 2017/11/04 01:14:25 christos Exp $")
34 #include <seccomp.h> /* libseccomp */
35 #include <sys/prctl.h> /* prctl */
36 #include <sys/socket.h>
41 #define DENY_RULE(call) \
43 if (seccomp_rule_add (ctx, SCMP_ACT_KILL, SCMP_SYS(call), 0) == -1) \
45 while (/*CONSTCOND*/0)
46 #define ALLOW_RULE(call) \
48 if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, SCMP_SYS(call), 0) == -1) \
50 while (/*CONSTCOND*/0)
52 static scmp_filter_ctx ctx;
56 enable_sandbox_basic(void)
59 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
63 // prevent escape via ptrace
64 prctl(PR_SET_DUMPABLE, 0);
67 if (prctl (PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
70 // initialize the filter
71 ctx = seccomp_init(SCMP_ACT_ALLOW);
80 DENY_RULE(clock_adjtime);
81 DENY_RULE(create_module);
82 DENY_RULE(delete_module);
83 DENY_RULE(fanotify_init);
84 DENY_RULE(finit_module);
85 DENY_RULE(get_kernel_syms);
86 DENY_RULE(get_mempolicy);
87 DENY_RULE(init_module);
89 DENY_RULE(io_destroy);
90 DENY_RULE(io_getevents);
95 DENY_RULE(ioprio_set);
97 #ifdef __NR_kexec_file_load
98 DENY_RULE(kexec_file_load);
100 DENY_RULE(kexec_load);
102 DENY_RULE(lookup_dcookie);
104 DENY_RULE(nfsservctl);
105 DENY_RULE(migrate_pages);
106 DENY_RULE(modify_ldt);
108 DENY_RULE(move_pages);
109 DENY_RULE(name_to_handle_at);
110 DENY_RULE(open_by_handle_at);
111 DENY_RULE(perf_event_open);
112 DENY_RULE(pivot_root);
113 DENY_RULE(process_vm_readv);
114 DENY_RULE(process_vm_writev);
117 DENY_RULE(remap_file_pages);
118 DENY_RULE(request_key);
119 DENY_RULE(set_mempolicy);
129 // blocking dangerous syscalls that file should not need
135 // applying filter...
136 if (seccomp_load (ctx) == -1)
138 // free ctx after the filter has been loaded into the kernel
139 seccomp_release(ctx);
143 seccomp_release(ctx);
149 enable_sandbox_full(void)
152 // prevent child processes from getting more priv e.g. via setuid,
154 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
157 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
160 // initialize the filter
161 ctx = seccomp_init(SCMP_ACT_KILL);
170 ALLOW_RULE(exit_group);
173 ALLOW_RULE(getdents);
178 ALLOW_RULE(mprotect);
185 ALLOW_RULE(readlink);
186 ALLOW_RULE(rt_sigaction);
187 ALLOW_RULE(rt_sigprocmask);
188 ALLOW_RULE(rt_sigreturn);
197 // needed by valgrind
200 ALLOW_RULE(rt_sigtimedwait);
204 /* special restrictions for socket, only allow AF_UNIX/AF_LOCAL */
205 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
206 SCMP_CMP(0, SCMP_CMP_EQ, AF_UNIX)) == -1)
209 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
210 SCMP_CMP(0, SCMP_CMP_EQ, AF_LOCAL)) == -1)
214 /* special restrictions for open, prevent opening files for writing */
215 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
216 SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) == -1)
219 if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
220 SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) == -1)
223 if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
224 SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) == -1)
229 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
230 SCMP_CMP(0, SCMP_CMP_EQ, 2)) == -1)
234 // applying filter...
235 if (seccomp_load(ctx) == -1)
237 // free ctx after the filter has been loaded into the kernel
238 seccomp_release(ctx);
242 // something went wrong
243 seccomp_release(ctx);