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