xref: /netbsd-src/external/bsd/file/dist/src/seccomp.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: seccomp.c,v 1.1.1.6 2021/04/09 18:58:01 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.18 2021/03/14 17:01:58 christos Exp $")
34 #else
35 __RCSID("$NetBSD: seccomp.c,v 1.1.1.6 2021/04/09 18:58:01 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/ioctl.h>
43 #include <sys/socket.h>
44 #include <termios.h>
45 #include <fcntl.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 
49 #define DENY_RULE(call) \
50     do \
51 	if (seccomp_rule_add (ctx, SCMP_ACT_KILL, SCMP_SYS(call), 0) == -1) \
52 	    goto out; \
53     while (/*CONSTCOND*/0)
54 #define ALLOW_RULE(call) \
55     do \
56 	if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, SCMP_SYS(call), 0) == -1) \
57 	    goto out; \
58     while (/*CONSTCOND*/0)
59 
60 #define ALLOW_IOCTL_RULE(param) \
61     do \
62 	if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(ioctl), 1, \
63 	    SCMP_CMP(1, SCMP_CMP_EQ, param)) == -1) \
64 		goto out; \
65     while (/*CONSTCOND*/0)
66 
67 static scmp_filter_ctx ctx;
68 
69 int
70 enable_sandbox_basic(void)
71 {
72 
73 	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
74 		return -1;
75 
76 	if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
77 		return -1;
78 
79 	// initialize the filter
80 	ctx = seccomp_init(SCMP_ACT_ALLOW);
81 	if (ctx == NULL)
82 	    return 1;
83 
84 	DENY_RULE(_sysctl);
85 	DENY_RULE(acct);
86 	DENY_RULE(add_key);
87 	DENY_RULE(adjtimex);
88 	DENY_RULE(chroot);
89 	DENY_RULE(clock_adjtime);
90 	DENY_RULE(create_module);
91 	DENY_RULE(delete_module);
92 	DENY_RULE(fanotify_init);
93 	DENY_RULE(finit_module);
94 	DENY_RULE(get_kernel_syms);
95 	DENY_RULE(get_mempolicy);
96 	DENY_RULE(init_module);
97 	DENY_RULE(io_cancel);
98 	DENY_RULE(io_destroy);
99 	DENY_RULE(io_getevents);
100 	DENY_RULE(io_setup);
101 	DENY_RULE(io_submit);
102 	DENY_RULE(ioperm);
103 	DENY_RULE(iopl);
104 	DENY_RULE(ioprio_set);
105 	DENY_RULE(kcmp);
106 #ifdef __NR_kexec_file_load
107 	DENY_RULE(kexec_file_load);
108 #endif
109 	DENY_RULE(kexec_load);
110 	DENY_RULE(keyctl);
111 	DENY_RULE(lookup_dcookie);
112 	DENY_RULE(mbind);
113 	DENY_RULE(nfsservctl);
114 	DENY_RULE(migrate_pages);
115 	DENY_RULE(modify_ldt);
116 	DENY_RULE(mount);
117 	DENY_RULE(move_pages);
118 	DENY_RULE(name_to_handle_at);
119 	DENY_RULE(open_by_handle_at);
120 	DENY_RULE(perf_event_open);
121 	DENY_RULE(pivot_root);
122 	DENY_RULE(process_vm_readv);
123 	DENY_RULE(process_vm_writev);
124 	DENY_RULE(ptrace);
125 	DENY_RULE(reboot);
126 	DENY_RULE(remap_file_pages);
127 	DENY_RULE(request_key);
128 	DENY_RULE(set_mempolicy);
129 	DENY_RULE(swapoff);
130 	DENY_RULE(swapon);
131 	DENY_RULE(sysfs);
132 	DENY_RULE(syslog);
133 	DENY_RULE(tuxcall);
134 	DENY_RULE(umount2);
135 	DENY_RULE(uselib);
136 	DENY_RULE(vmsplice);
137 
138 	// blocking dangerous syscalls that file should not need
139 	DENY_RULE (execve);
140 	DENY_RULE (socket);
141 	// ...
142 
143 
144 	// applying filter...
145 	if (seccomp_load (ctx) == -1)
146 		goto out;
147 	// free ctx after the filter has been loaded into the kernel
148 	seccomp_release(ctx);
149 	return 0;
150 
151 out:
152 	seccomp_release(ctx);
153 	return -1;
154 }
155 
156 
157 int
158 enable_sandbox_full(void)
159 {
160 
161 	// prevent child processes from getting more priv e.g. via setuid,
162 	// capabilities, ...
163 	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
164 		return -1;
165 
166 	if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1)
167 		return -1;
168 
169 	// initialize the filter
170 	ctx = seccomp_init(SCMP_ACT_KILL);
171 	if (ctx == NULL)
172 		return -1;
173 
174 	ALLOW_RULE(access);
175 	ALLOW_RULE(brk);
176 	ALLOW_RULE(close);
177 	ALLOW_RULE(dup2);
178 	ALLOW_RULE(exit);
179 	ALLOW_RULE(exit_group);
180 	ALLOW_RULE(fcntl);
181  	ALLOW_RULE(fcntl64);
182 	ALLOW_RULE(fstat);
183  	ALLOW_RULE(fstat64);
184 	ALLOW_RULE(futex);
185 	ALLOW_RULE(getdents);
186 #ifdef __NR_getdents64
187 	ALLOW_RULE(getdents64);
188 #endif
189 #ifdef FIONREAD
190 	// called in src/compress.c under sread
191 	ALLOW_IOCTL_RULE(FIONREAD);
192 #endif
193 #ifdef TIOCGWINSZ
194 	// musl libc may call ioctl TIOCGWINSZ on stdout
195 	ALLOW_IOCTL_RULE(TIOCGWINSZ);
196 #endif
197 #ifdef TCGETS
198 	// glibc may call ioctl TCGETS on stdout on physical terminal
199 	ALLOW_IOCTL_RULE(TCGETS);
200 #endif
201 	ALLOW_RULE(lseek);
202  	ALLOW_RULE(_llseek);
203 	ALLOW_RULE(lstat);
204  	ALLOW_RULE(lstat64);
205 	ALLOW_RULE(madvise);
206 	ALLOW_RULE(mmap);
207  	ALLOW_RULE(mmap2);
208 	ALLOW_RULE(mprotect);
209 	ALLOW_RULE(mremap);
210 	ALLOW_RULE(munmap);
211 #ifdef __NR_newfstatat
212 	ALLOW_RULE(newfstatat);
213 #endif
214 	ALLOW_RULE(open);
215 	ALLOW_RULE(openat);
216 	ALLOW_RULE(pread64);
217 	ALLOW_RULE(read);
218 	ALLOW_RULE(readlink);
219 #ifdef __NR_readlinkat
220 	ALLOW_RULE(readlinkat);
221 #endif
222 	ALLOW_RULE(rt_sigaction);
223 	ALLOW_RULE(rt_sigprocmask);
224 	ALLOW_RULE(rt_sigreturn);
225 	ALLOW_RULE(select);
226 	ALLOW_RULE(stat);
227 	ALLOW_RULE(statx);
228 	ALLOW_RULE(stat64);
229 	ALLOW_RULE(sysinfo);
230 	ALLOW_RULE(umask);	// Used in file_pipe2file()
231 	ALLOW_RULE(getpid);	// Used by glibc in file_pipe2file()
232 	ALLOW_RULE(unlink);
233 	ALLOW_RULE(write);
234 	ALLOW_RULE(writev);
235 
236 
237 #if 0
238 	// needed by valgrind
239 	ALLOW_RULE(gettid);
240 	ALLOW_RULE(rt_sigtimedwait);
241 #endif
242 
243 #if 0
244 	 /* special restrictions for socket, only allow AF_UNIX/AF_LOCAL */
245 	 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
246 	     SCMP_CMP(0, SCMP_CMP_EQ, AF_UNIX)) == -1)
247 	 	goto out;
248 
249 	 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(socket), 1,
250 	     SCMP_CMP(0, SCMP_CMP_EQ, AF_LOCAL)) == -1)
251 	 	goto out;
252 
253 
254 	 /* special restrictions for open, prevent opening files for writing */
255 	 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 1,
256 	     SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY | O_RDWR, 0)) == -1)
257 	 	goto out;
258 
259 	 if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
260 	     SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_WRONLY, O_WRONLY)) == -1)
261 	 	goto out;
262 
263 	 if (seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EACCES), SCMP_SYS(open), 1,
264 	     SCMP_CMP(1, SCMP_CMP_MASKED_EQ, O_RDWR, O_RDWR)) == -1)
265 	 	goto out;
266 
267 
268 	 /* allow stderr */
269 	 if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1,
270 	     SCMP_CMP(0, SCMP_CMP_EQ, 2)) == -1)
271 		 goto out;
272 #endif
273 
274 	// applying filter...
275 	if (seccomp_load(ctx) == -1)
276 		goto out;
277 	// free ctx after the filter has been loaded into the kernel
278 	seccomp_release(ctx);
279 	return 0;
280 
281 out:
282 	// something went wrong
283 	seccomp_release(ctx);
284 	return -1;
285 }
286 #endif
287