xref: /netbsd-src/external/bsd/file/dist/src/seccomp.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: seccomp.c,v 1.1.1.1 2018/04/15 19:32:48 christos Exp $	*/
2 
3 /*
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice immediately at the beginning of the file, without modification,
9  *    this list of conditions, and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 /*
27  * libseccomp hooks.
28  */
29 #include "file.h"
30 
31 #ifndef	lint
32 #if 0
33 FILE_RCSID("@(#)$File: seccomp.c,v 1.2 2017/11/04 01:14:25 christos Exp $")
34 #else
35 __RCSID("$NetBSD: seccomp.c,v 1.1.1.1 2018/04/15 19:32:48 christos Exp $");
36 #endif
37 #endif	/* lint */
38 
39 #if HAVE_LIBSECCOMP
40 #include <seccomp.h> /* libseccomp */
41 #include <sys/prctl.h> /* prctl */
42 #include <sys/socket.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <errno.h>
46 
47 #define DENY_RULE(call) \
48     do \
49 	if (seccomp_rule_add (ctx, SCMP_ACT_KILL, SCMP_SYS(call), 0) == -1) \
50 	    goto out; \
51     while (/*CONSTCOND*/0)
52 #define ALLOW_RULE(call) \
53     do \
54 	if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, SCMP_SYS(call), 0) == -1) \
55 	    goto out; \
56     while (/*CONSTCOND*/0)
57 
58 static scmp_filter_ctx ctx;
59 
60 
61 int
62 enable_sandbox_basic(void)
63 {
64 
65 	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
66 		return -1;
67 
68 #if 0
69 	// prevent escape via ptrace
70 	prctl(PR_SET_DUMPABLE, 0);
71 #endif
72 
73 	if (prctl (PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
74 		return -1;
75 
76 	// initialize the filter
77 	ctx = seccomp_init(SCMP_ACT_ALLOW);
78 	if (ctx == NULL)
79 	    return 1;
80 
81 	DENY_RULE(_sysctl);
82 	DENY_RULE(acct);
83 	DENY_RULE(add_key);
84 	DENY_RULE(adjtimex);
85 	DENY_RULE(chroot);
86 	DENY_RULE(clock_adjtime);
87 	DENY_RULE(create_module);
88 	DENY_RULE(delete_module);
89 	DENY_RULE(fanotify_init);
90 	DENY_RULE(finit_module);
91 	DENY_RULE(get_kernel_syms);
92 	DENY_RULE(get_mempolicy);
93 	DENY_RULE(init_module);
94 	DENY_RULE(io_cancel);
95 	DENY_RULE(io_destroy);
96 	DENY_RULE(io_getevents);
97 	DENY_RULE(io_setup);
98 	DENY_RULE(io_submit);
99 	DENY_RULE(ioperm);
100 	DENY_RULE(iopl);
101 	DENY_RULE(ioprio_set);
102 	DENY_RULE(kcmp);
103 #ifdef __NR_kexec_file_load
104 	DENY_RULE(kexec_file_load);
105 #endif
106 	DENY_RULE(kexec_load);
107 	DENY_RULE(keyctl);
108 	DENY_RULE(lookup_dcookie);
109 	DENY_RULE(mbind);
110 	DENY_RULE(nfsservctl);
111 	DENY_RULE(migrate_pages);
112 	DENY_RULE(modify_ldt);
113 	DENY_RULE(mount);
114 	DENY_RULE(move_pages);
115 	DENY_RULE(name_to_handle_at);
116 	DENY_RULE(open_by_handle_at);
117 	DENY_RULE(perf_event_open);
118 	DENY_RULE(pivot_root);
119 	DENY_RULE(process_vm_readv);
120 	DENY_RULE(process_vm_writev);
121 	DENY_RULE(ptrace);
122 	DENY_RULE(reboot);
123 	DENY_RULE(remap_file_pages);
124 	DENY_RULE(request_key);
125 	DENY_RULE(set_mempolicy);
126 	DENY_RULE(swapoff);
127 	DENY_RULE(swapon);
128 	DENY_RULE(sysfs);
129 	DENY_RULE(syslog);
130 	DENY_RULE(tuxcall);
131 	DENY_RULE(umount2);
132 	DENY_RULE(uselib);
133 	DENY_RULE(vmsplice);
134 
135 	// blocking dangerous syscalls that file should not need
136 	DENY_RULE (execve);
137 	DENY_RULE (socket);
138 	// ...
139 
140 
141 	// applying filter...
142 	if (seccomp_load (ctx) == -1)
143 		goto out;
144 	// free ctx after the filter has been loaded into the kernel
145 	seccomp_release(ctx);
146 	return 0;
147 
148 out:
149 	seccomp_release(ctx);
150 	return -1;
151 }
152 
153 
154 int
155 enable_sandbox_full(void)
156 {
157 
158 	// prevent child processes from getting more priv e.g. via setuid,
159 	// capabilities, ...
160 	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
161 		return -1;
162 
163 	if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
164 		return -1;
165 
166 	// initialize the filter
167 	ctx = seccomp_init(SCMP_ACT_KILL);
168 	if (ctx == NULL)
169 		return -1;
170 
171 	ALLOW_RULE(access);
172 	ALLOW_RULE(brk);
173 	ALLOW_RULE(close);
174 	ALLOW_RULE(dup2);
175 	ALLOW_RULE(exit);
176 	ALLOW_RULE(exit_group);
177 	ALLOW_RULE(fcntl);
178 	ALLOW_RULE(fstat);
179 	ALLOW_RULE(getdents);
180 	ALLOW_RULE(ioctl);
181 	ALLOW_RULE(lseek);
182 	ALLOW_RULE(lstat);
183 	ALLOW_RULE(mmap);
184 	ALLOW_RULE(mprotect);
185 	ALLOW_RULE(mremap);
186 	ALLOW_RULE(munmap);
187 	ALLOW_RULE(open);
188 	ALLOW_RULE(openat);
189 	ALLOW_RULE(pread64);
190 	ALLOW_RULE(read);
191 	ALLOW_RULE(readlink);
192 	ALLOW_RULE(rt_sigaction);
193 	ALLOW_RULE(rt_sigprocmask);
194 	ALLOW_RULE(rt_sigreturn);
195 	ALLOW_RULE(select);
196 	ALLOW_RULE(stat);
197 	ALLOW_RULE(sysinfo);
198 	ALLOW_RULE(unlink);
199 	ALLOW_RULE(write);
200 
201 
202 #if 0
203 	// needed by valgrind
204 	ALLOW_RULE(gettid);
205 	ALLOW_RULE(getpid);
206 	ALLOW_RULE(rt_sigtimedwait);
207 #endif
208 
209 #if 0
210 	 /* special restrictions for socket, only allow AF_UNIX/AF_LOCAL */
211 	 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
212 	     SCMP_CMP(0, SCMP_CMP_EQ, AF_UNIX)) == -1)
213 	 	goto out;
214 
215 	 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
216 	     SCMP_CMP(0, SCMP_CMP_EQ, AF_LOCAL)) == -1)
217 	 	goto out;
218 
219 
220 	 /* special restrictions for open, prevent opening files for writing */
221 	 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
222 	     SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) == -1)
223 	 	goto out;
224 
225 	 if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
226 	     SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) == -1)
227 	 	goto out;
228 
229 	 if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
230 	     SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) == -1)
231 	 	goto out;
232 
233 
234 	 /* allow stderr */
235 	 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
236 	     SCMP_CMP(0, SCMP_CMP_EQ, 2)) == -1)
237 		 goto out;
238 #endif
239 
240 	// applying filter...
241 	if (seccomp_load(ctx) == -1)
242 		goto out;
243 	// free ctx after the filter has been loaded into the kernel
244 	seccomp_release(ctx);
245 	return 0;
246 
247 out:
248 	// something went wrong
249 	seccomp_release(ctx);
250 	return -1;
251 }
252 #endif
253