aboutsummaryrefslogtreecommitdiff
path: root/sandbox.c
blob: 782892216a8019df9b5051dd2507577d02af73fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "gmid.h"

#if DISABLE_SANDBOX

#warning "Sandbox disabled! Please report issues upstream instead of disabling the sandbox."

void
sandbox_server_process(void)
{
	return;
}

void
sandbox_executor_process(void)
{
        log_notice(NULL, "Sandbox disabled!  "
	    "Please report issues upstream instead of disabling the sandbox.");
}

void
sandbox_logger_process(void)
{
	return;
}

#elif defined(__FreeBSD__)

#include <sys/capsicum.h>

void
sandbox_server_process(void)
{
	if (cap_enter() == -1)
		fatal("cap_enter");
}

void
sandbox_executor_process(void)
{
	/*
	 * We cannot capsicum the executor process because it needs to
	 * fork(2)+execve(2) cgi scripts
	 */
	return;
}

void
sandbox_logger_process(void)
{
	if (cap_enter() == -1)
		fatal("cap_enter");
}

#elif defined(__linux__)

#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/syscall.h>
#include <sys/types.h>

#include <linux/audit.h>
#include <linux/filter.h>
#include <linux/seccomp.h>

#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

/* uncomment to enable debugging.  ONLY FOR DEVELOPMENT */
/* #define SC_DEBUG */

#ifdef SC_DEBUG
# define SC_FAIL SECCOMP_RET_TRAP
#else
# define SC_FAIL SECCOMP_RET_KILL
#endif

#if (BYTE_ORDER == LITTLE_ENDIAN)
# define SC_ARG_LO	0
# define SC_ARG_HI	sizeof(uint32_t)
#elif (BYTE_ORDER == BIG_ENDIAN)
# define SC_ARG_LO	sizeof(uint32_t)
# define SC_ARG_HI	0
#else
# error "Uknown endian"
#endif

/* make the filter more readable */
#define SC_ALLOW(nr)						\
	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_##nr, 0, 1),	\
	BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)

/*
 * SC_ALLOW_ARG and the SECCOMP_AUDIT_ARCH below are courtesy of
 * https://roy.marples.name/git/dhcpcd/blob/HEAD:/src/privsep-linux.c
 */
#define SC_ALLOW_ARG(_nr, _arg, _val)						\
	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (_nr), 0, 6),			\
	BPF_STMT(BPF_LD + BPF_W + BPF_ABS,					\
	    offsetof(struct seccomp_data, args[(_arg)]) + SC_ARG_LO),		\
	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K,					\
	    ((_val) & 0xffffffff), 0, 3),					\
	BPF_STMT(BPF_LD + BPF_W + BPF_ABS,					\
	    offsetof(struct seccomp_data, args[(_arg)]) + SC_ARG_HI),		\
	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K,					\
	    (((uint32_t)((uint64_t)(_val) >> 32)) & 0xffffffff), 0, 1),		\
	BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW),				\
	BPF_STMT(BPF_LD + BPF_W + BPF_ABS,					\
	    offsetof(struct seccomp_data, nr))

/*
 * I personally find this quite nutty.  Why can a system header not
 * define a default for this?
 */
#if defined(__i386__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386
#elif defined(__x86_64__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64
#elif defined(__arc__)
#  if defined(__A7__)
#    if (BYTE_ORDER == LITTLE_ENDIAN)
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACT
#    else
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACTBE
#    endif
#  elif defined(__HS__)
#    if (BYTE_ORDER == LITTLE_ENDIAN)
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2
#    else
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2BE
#    endif
#  else
#    error "Platform does not support seccomp filter yet"
#  endif
#elif defined(__arm__)
#  ifndef EM_ARM
#    define EM_ARM 40
#  endif
#  if (BYTE_ORDER == LITTLE_ENDIAN)
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM
#  else
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARMEB
#  endif
#elif defined(__aarch64__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64
#elif defined(__alpha__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ALPHA
#elif defined(__hppa__)
#  if defined(__LP64__)
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC64
#  else
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC
#  endif
#elif defined(__ia64__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_IA64
#elif defined(__microblaze__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MICROBLAZE
#elif defined(__m68k__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_M68K
#elif defined(__mips__)
#  if defined(__MIPSEL__)
#    if defined(__LP64__)
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL64
#    else
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL
#    endif
#  elif defined(__LP64__)
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS64
#  else
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS
#  endif
#elif defined(__nds32__)
#  if (BYTE_ORDER == LITTLE_ENDIAN)
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32
#else
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32BE
#endif
#elif defined(__nios2__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NIOS2
#elif defined(__or1k__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_OPENRISC
#elif defined(__powerpc64__)
#  if (BYTE_ORDER == LITTLE_ENDIAN)
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64LE
#  else
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64
#  endif
#elif defined(__powerpc__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC
#elif defined(__riscv)
#  if defined(__LP64__)
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV64
#  else
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV32
#  endif
#elif defined(__s390x__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390X
#elif defined(__s390__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390
#elif defined(__sh__)
#  if defined(__LP64__)
#    if (BYTE_ORDER == LITTLE_ENDIAN)
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL64
#    else
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH64
#    endif
#  else
#    if (BYTE_ORDER == LITTLE_ENDIAN)
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL
#    else
#      define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH
#    endif
#  endif
#elif defined(__sparc__)
#  if defined(__arch64__)
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC64
#  else
#    define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC
#  endif
#elif defined(__xtensa__)
#  define SECCOMP_AUDIT_ARCH AUDIT_ARCH_XTENSA
#else
#  error "Platform does not support seccomp filter yet"
#endif

static struct sock_filter filter[] = {
	/* load the *current* architecture */
	BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
	    (offsetof(struct seccomp_data, arch))),
	/* ensure it's the same that we've been compiled on */
	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K,
	    SECCOMP_AUDIT_ARCH, 1, 0),
	/* if not, kill the program */
	BPF_STMT(BPF_RET | BPF_K, SC_FAIL),

	/* load the syscall number */
	BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
	    (offsetof(struct seccomp_data, nr))),

#ifdef __NR_accept
	SC_ALLOW(accept),
#endif
#ifdef __NR_accept4
	SC_ALLOW(accept4),
#endif
#ifdef __NR_brk
	SC_ALLOW(brk),
#endif
#ifdef __NR_clock_gettime
	SC_ALLOW(clock_gettime),
#endif
#if defined(__x86_64__) && defined(__ILP32__) && defined(__X32_SYSCALL_BIT)
	SECCOMP_ALLOW(__NR_clock_gettime & ~__X32_SYSCALL_BIT),
#endif
#ifdef __NR_clock_gettime64
	SC_ALLOW(clock_gettime64),
#endif
#ifdef __NR_close
	SC_ALLOW(close),
#endif
#ifdef __NR_epoll_ctl
	SC_ALLOW(epoll_ctl),
#endif
#ifdef __NR_epoll_pwait
	SC_ALLOW(epoll_pwait),
#endif
#ifdef __NR_epoll_wait
	SC_ALLOW(epoll_wait),
#endif
#ifdef __NR_exit
	SC_ALLOW(exit),
#endif
#ifdef __NR_exit_group
	SC_ALLOW(exit_group),
#endif
#ifdef __NR_fcntl
	SC_ALLOW(fcntl),
#endif
#ifdef __NR_fcntl64
	SC_ALLOW(fcntl64),
#endif
#ifdef __NR_fstat
	SC_ALLOW(fstat),
#endif
#ifdef __NR_getdents64
	SC_ALLOW(getdents64),
#endif
#ifdef __NR_getpid
	SC_ALLOW(getpid),
#endif
#ifdef __NR_getrandom
	SC_ALLOW(getrandom),
#endif
#ifdef __NR_gettimeofday
	SC_ALLOW(gettimeofday),
#endif
#ifdef __NR_ioctl
	/* allow ioctl only on fd 1, glibc doing stuff? */
        SC_ALLOW_ARG(__NR_ioctl, 0, 1),
#endif
#ifdef __NR_lseek
	SC_ALLOW(lseek),
#endif
#ifdef __NR_madvise
	SC_ALLOW(madvise),
#endif
#ifdef __NR_mmap
	SC_ALLOW(mmap),
#endif
#ifdef __NR_mmap2
	SC_ALLOW(mmap2),
#endif
#ifdef __NR_munmap
	SC_ALLOW(munmap),
#endif
#ifdef __NR_newfstatat
	SC_ALLOW(newfstatat),
#endif
#ifdef __NR_oldfstat
	SC_ALLOW(oldfstat),
#endif
#ifdef __NR_openat
	SC_ALLOW(openat),
#endif
#ifdef __NR_prlimit64
	SC_ALLOW(prlimit64),
#endif
#ifdef __NR_read
	SC_ALLOW(read),
#endif
#ifdef __NR_recvmsg
	SC_ALLOW(recvmsg),
#endif
#ifdef __NR_redav
	SC_ALLOW(redav),
#endif
#ifdef __NR_rt_sigaction
	SC_ALLOW(rt_sigaction),
#endif
#ifdef __NR_rt_sigreturn
	SC_ALLOW(rt_sigreturn),
#endif
#ifdef __NR_sendmsg
	SC_ALLOW(sendmsg),
#endif
#ifdef __NR_statx
	SC_ALLOW(statx),
#endif
#ifdef __NR_write
	SC_ALLOW(write),
#endif
#ifdef __NR_writev
	SC_ALLOW(writev),
#endif

	/* disallow everything else */
	BPF_STMT(BPF_RET | BPF_K, SC_FAIL),
};

#ifdef SC_DEBUG

#include <signal.h>
#include <unistd.h>

static void
sandbox_seccomp_violation(int signum, siginfo_t *info, void *ctx)
{
	(void)signum;
	(void)ctx;

	fprintf(stderr, "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)\n",
	    __func__, info->si_arch, info->si_syscall, info->si_call_addr);
	_exit(1);
}

static void
sandbox_seccomp_catch_sigsys(void)
{
	struct sigaction act;
	sigset_t mask;

	memset(&act, 0, sizeof(act));
	sigemptyset(&mask);
	sigaddset(&mask, SIGSYS);

	act.sa_sigaction = &sandbox_seccomp_violation;
	act.sa_flags = SA_SIGINFO;
	if (sigaction(SIGSYS, &act, NULL) == -1)
		fatal("%s: sigaction(SIGSYS): %s",
		    __func__, strerror(errno));

	if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
		fatal("%s: sigprocmask(SIGSYS): %s\n",
		    __func__, strerror(errno));
}
#endif	/* SC_DEBUG */

void
sandbox_server_process(void)
{
	struct sock_fprog prog = {
		.len = (unsigned short) (sizeof(filter) / sizeof(filter[0])),
		.filter = filter,
	};

#ifdef SC_DEBUG
	sandbox_seccomp_catch_sigsys();
#endif

	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1)
		fatal("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
		    __func__, strerror(errno));

	if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) == -1)
		fatal("%s: prctl(PR_SET_SECCOMP): %s\n",
		    __func__, strerror(errno));
}

void
sandbox_executor_process(void)
{
	/*
	 * We cannot use seccomp for the executor process because we
	 * don't know what the child will do.  Also, our filter will
	 * be inherited so the child cannot set its own seccomp
	 * policy.
	 */
	return;
}

void
sandbox_logger_process(void)
{
	/*
	 * To be honest, here we could use a seccomp policy to only
	 * allow writev(2) and memory allocations.
	 */
	return;
}

#elif defined(__OpenBSD__)

#include <unistd.h>

void
sandbox_server_process(void)
{
	struct vhost	*h;
	struct location	*l;

	TAILQ_FOREACH(h, &hosts, vhosts) {
		TAILQ_FOREACH(l, &h->locations, locations) {
			if (l->dir == NULL)
				continue;

			if (unveil(l->dir, "r") == -1)
				fatal("unveil %s for domain %s",
				    l->dir,
				    h->domain);
		}
	}

	if (pledge("stdio recvfd rpath inet", NULL) == -1)
		fatal("pledge");
}

void
sandbox_executor_process(void)
{
	struct vhost	*h;
	struct location	*l;
	struct fcgi	*f;
	size_t		 i;

	TAILQ_FOREACH(h, &hosts, vhosts) {
		TAILQ_FOREACH(l, &h->locations, locations) {
			if (l->dir == NULL)
				continue;

			/* r so we can chdir into the directory */
			if (unveil(l->dir, "rx") == -1)
				fatal("unveil %s for domain %s",
				    l->dir, h->domain);
		}
	}

	for (i = 0; i < FCGI_MAX; i++) {
                f = &fcgi[i];
		if (f->path != NULL) {
			if (unveil(f->path, "rw") == -1)
				fatal("unveil %s", f->path);
		}

		if (f->prog != NULL) {
			if (unveil(f->prog, "rx") == -1)
				fatal("unveil %s", f->prog);
		}
	}

	/*
	 * rpath: to chdir into the correct directory
	 * proc exec: CGI
	 * dns inet unix: FastCGI
	 */
	if (pledge("stdio rpath sendfd proc exec dns inet unix", NULL))
		err(1, "pledge");
}

void
sandbox_logger_process(void)
{
	if (pledge("stdio recvfd", NULL) == -1)
		err(1, "pledge");
}

#else

#warning "No sandbox method known for this OS"

void
sandbox_server_process(void)
{
	return;
}

void
sandbox_executor_process(void)
{
	log_notice(NULL, "no sandbox method known for this OS");
}

void
sandbox_logger_process(void)
{
	return;
}

#endif