summaryrefslogtreecommitdiff
path: root/lib/sandbox.c
blob: 2ad47e95e01f14158cdc72c108588528c99e5b2f (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
/*
 * sandbox.c: Process sandboxing
 *  
 * Copyright (C) 2017 Colin Watson.
 *
 * This file is part of man-db.
 *
 * man-db is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * man-db is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with man-db; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Some of the syscall lists in this file come from systemd, whose
 * copyright/licensing statement is as follows.  Per LGPLv2.1 s. 3, I have
 * altered the original references to LGPLv2.1 to refer to GPLv2 instead.
 *
 * Copyright 2014 Lennart Poettering
 *
 * systemd is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * systemd is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with systemd; If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif /* HAVE_CONFIG_H */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifdef HAVE_LIBSECCOMP
#  include <sys/prctl.h>
#  include <seccomp.h>
#endif /* HAVE_LIBSECCOMP */

#include "pipeline.h"

#include "manconfig.h"

#include "error.h"

#include "sandbox.h"

struct man_sandbox {
#ifdef HAVE_LIBSECCOMP
	scmp_filter_ctx ctx;
	scmp_filter_ctx permissive_ctx;
#else /* !HAVE_LIBSECCOMP */
	char dummy;
#endif /* HAVE_LIBSECCOMP */
};

#ifdef HAVE_LIBSECCOMP
/* Can we load a seccomp filter into this process?
 *
 * This guard allows us to call sandbox_load in code paths that may
 * conditionally do so again.
 */
static int can_load_seccomp (void)
{
	const char *man_disable_seccomp, *ld_preload;
	int seccomp_status;

	man_disable_seccomp = getenv ("MAN_DISABLE_SECCOMP");
	if (man_disable_seccomp && *man_disable_seccomp) {
		debug ("seccomp filter disabled by user request\n");
		return 0;
	}

	/* Valgrind causes the child process to make some system calls we
	 * don't want to allow in general, so disable seccomp when running
	 * on Valgrind.
	 *
	 * The correct approach seems to be to either require valgrind.h at
	 * build-time or copy valgrind.h into this project and then use the
	 * RUNNING_ON_VALGRIND macro, but I'd really rather not add a
	 * build-dependency for this or take a copy of a >6000-line header
	 * file.  Since the goal of this is only to disable the seccomp
	 * filter under Valgrind, this will do for now.
	 */
	ld_preload = getenv ("LD_PRELOAD");
	if (ld_preload && strstr (ld_preload, "/vgpreload") != NULL) {
		debug ("seccomp filter disabled while running under "
		       "Valgrind\n");
		return 0;
	}

	seccomp_status = prctl (PR_GET_SECCOMP);

	if (seccomp_status == 0)
		return 1;

	if (seccomp_status == -1) {
		if (errno == EINVAL)
			debug ("running kernel does not support seccomp\n");
		else
			debug ("unknown error getting seccomp status: %s\n",
			       strerror (errno));
	} else if (seccomp_status == 2)
		debug ("seccomp already enabled\n");
	else
		debug ("unknown return value from PR_GET_SECCOMP: %d\n",
		       seccomp_status);
	return 0;
}
#endif /* HAVE_LIBSECCOMP */

#ifdef HAVE_LIBSECCOMP
/* Create a seccomp filter.
 *
 * If permissive is true, then the returned filter will allow limited file
 * creation (although not making executable files).  This obviously
 * constitutes less effective confinement, but it's necessary for some
 * subprocesses (such as groff) that need the ability to write to temporary
 * files.  Confining these further requires additional tools that can do
 * path-based filtering or similar, such as AppArmor.
 */
scmp_filter_ctx make_seccomp_filter (int permissive)
{
	scmp_filter_ctx ctx;
	mode_t mode_mask = S_ISUID | S_ISGID | S_IXUSR | S_IXGRP | S_IXOTH;
	int create_mask = O_CREAT
#ifdef O_TMPFILE
		| O_TMPFILE
#endif /* O_TMPFILE */
		;

	debug ("initialising seccomp filter (permissive: %d)\n", permissive);
	ctx = seccomp_init (SCMP_ACT_TRAP);
	if (!ctx)
		error (FATAL, errno, "can't initialise seccomp filter");

#define SC_ALLOW(name) \
	do { \
		int nr = seccomp_syscall_resolve_name (name); \
		if (nr == __NR_SCMP_ERROR) \
			break; \
		if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, nr, 0) < 0) \
			error (FATAL, errno, "can't add seccomp rule"); \
	} while (0)

#define SC_ALLOW_ARG_1(name, cmp1) \
	do { \
		int nr = seccomp_syscall_resolve_name (name); \
		if (nr == __NR_SCMP_ERROR) \
			break; \
		if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, nr, 1, cmp1) < 0) \
			error (FATAL, errno, "can't add seccomp rule"); \
	} while (0)

#define SC_ALLOW_ARG_2(name, cmp1, cmp2) \
	do { \
		int nr = seccomp_syscall_resolve_name (name); \
		if (nr == __NR_SCMP_ERROR) \
			break; \
		if (seccomp_rule_add (ctx, SCMP_ACT_ALLOW, nr, \
				      2, cmp1, cmp2) < 0) \
			error (FATAL, errno, "can't add seccomp rule"); \
	} while (0)

	/* This sandbox is intended to allow operations that might
	 * reasonably be needed in simple data-transforming pipes: it should
	 * allow the process to do most reasonable things to itself, to read
	 * and write data from and to already-open file descriptors, to open
	 * files in read-only mode, and to fork new processes with the same
	 * restrictions.  (If permissive is true, then it should also allow
	 * limited file creation; see the header comment above.)
	 *
	 * Since I currently know of no library with suitable syscall lists,
	 * the syscall lists here are taken from
	 * systemd:src/shared/seccomp-util.c, last updated from commit
	 * 67eb5b380a7b7eed82f658190bff4ca2d83e9abe (2017-11-30).
	 */

	/* systemd: SystemCallFilter=@default */
	SC_ALLOW ("clock_getres");
	SC_ALLOW ("clock_gettime");
	SC_ALLOW ("clock_nanosleep");
	SC_ALLOW ("execve");
	SC_ALLOW ("exit");
	SC_ALLOW ("exit_group");
	SC_ALLOW ("futex");
	SC_ALLOW ("get_robust_list");
	SC_ALLOW ("get_thread_area");
	SC_ALLOW ("getegid");
	SC_ALLOW ("getegid32");
	SC_ALLOW ("geteuid");
	SC_ALLOW ("geteuid32");
	SC_ALLOW ("getgid");
	SC_ALLOW ("getgid32");
	SC_ALLOW ("getgroups");
	SC_ALLOW ("getgroups32");
	SC_ALLOW ("getpgid");
	SC_ALLOW ("getpgrp");
	SC_ALLOW ("getpid");
	SC_ALLOW ("getppid");
	SC_ALLOW ("getresgid");
	SC_ALLOW ("getresgid32");
	SC_ALLOW ("getresuid");
	SC_ALLOW ("getresuid32");
	SC_ALLOW ("getrlimit");
	SC_ALLOW ("getsid");
	SC_ALLOW ("gettid");
	SC_ALLOW ("gettimeofday");
	SC_ALLOW ("getuid");
	SC_ALLOW ("getuid32");
	SC_ALLOW ("membarrier");
	SC_ALLOW ("nanosleep");
	SC_ALLOW ("pause");
	SC_ALLOW ("prlimit64");
	SC_ALLOW ("restart_syscall");
	SC_ALLOW ("rt_sigreturn");
	SC_ALLOW ("sched_yield");
	SC_ALLOW ("set_robust_list");
	SC_ALLOW ("set_thread_area");
	SC_ALLOW ("set_tid_address");
	SC_ALLOW ("set_tls");
	SC_ALLOW ("sigreturn");
	SC_ALLOW ("time");
	SC_ALLOW ("ugetrlimit");

	/* systemd: SystemCallFilter=@basic-io */
	SC_ALLOW ("_llseek");
	SC_ALLOW ("close");
	SC_ALLOW ("dup");
	SC_ALLOW ("dup2");
	SC_ALLOW ("dup3");
	SC_ALLOW ("lseek");
	SC_ALLOW ("pread64");
	SC_ALLOW ("preadv");
	SC_ALLOW ("preadv2");
	SC_ALLOW ("pwrite64");
	SC_ALLOW ("pwritev");
	SC_ALLOW ("pwritev2");
	SC_ALLOW ("read");
	SC_ALLOW ("readv");
	SC_ALLOW ("write");
	SC_ALLOW ("writev");

	/* systemd: SystemCallFilter=@file-system (subset) */
	SC_ALLOW ("access");
	SC_ALLOW ("chdir");
	if (permissive) {
		SC_ALLOW_ARG_1 ("chmod",
				SCMP_A1 (SCMP_CMP_MASKED_EQ, mode_mask, 0));
		SC_ALLOW_ARG_1 ("creat",
				SCMP_A1 (SCMP_CMP_MASKED_EQ, mode_mask, 0));
	}
	SC_ALLOW ("faccessat");
	SC_ALLOW ("fallocate");
	SC_ALLOW ("fchdir");
	if (permissive) {
		SC_ALLOW_ARG_1 ("fchmod",
				SCMP_A1 (SCMP_CMP_MASKED_EQ, mode_mask, 0));
		SC_ALLOW_ARG_1 ("fchmodat",
				SCMP_A2 (SCMP_CMP_MASKED_EQ, mode_mask, 0));
	}
	SC_ALLOW ("fcntl");
	SC_ALLOW ("fcntl64");
	SC_ALLOW ("fstat");
	SC_ALLOW ("fstat64");
	SC_ALLOW ("fstatat64");
	SC_ALLOW ("fstatfs");
	SC_ALLOW ("fstatfs64");
	SC_ALLOW ("ftruncate");
	SC_ALLOW ("ftruncate64");
	if (permissive) SC_ALLOW ("futimesat");
	SC_ALLOW ("getcwd");
	SC_ALLOW ("getdents");
	SC_ALLOW ("getdents64");
	if (permissive) SC_ALLOW ("link");
	if (permissive) SC_ALLOW ("linkat");
	SC_ALLOW ("lstat");
	SC_ALLOW ("lstat64");
	if (permissive) SC_ALLOW ("mkdir");
	if (permissive) SC_ALLOW ("mkdirat");
	SC_ALLOW ("mmap");
	SC_ALLOW ("mmap2");
	SC_ALLOW ("munmap");
	SC_ALLOW ("newfstatat");
	SC_ALLOW ("oldfstat");
	SC_ALLOW ("oldlstat");
	SC_ALLOW ("oldstat");
	if (permissive) {
		SC_ALLOW_ARG_2 ("open",
				SCMP_A1 (SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT),
				SCMP_A2 (SCMP_CMP_MASKED_EQ, mode_mask, 0));
		SC_ALLOW_ARG_2 ("openat",
				SCMP_A2 (SCMP_CMP_MASKED_EQ, O_CREAT, O_CREAT),
				SCMP_A3 (SCMP_CMP_MASKED_EQ, mode_mask, 0));
#ifdef O_TMPFILE
		SC_ALLOW_ARG_2 ("open",
				SCMP_A1 (SCMP_CMP_MASKED_EQ,
					 O_TMPFILE, O_TMPFILE),
				SCMP_A2 (SCMP_CMP_MASKED_EQ, mode_mask, 0));
		SC_ALLOW_ARG_2 ("openat",
				SCMP_A2 (SCMP_CMP_MASKED_EQ,
					 O_TMPFILE, O_TMPFILE),
				SCMP_A3 (SCMP_CMP_MASKED_EQ, mode_mask, 0));
#endif /* O_TMPFILE */
		SC_ALLOW_ARG_1 ("open",
				SCMP_A1 (SCMP_CMP_MASKED_EQ, create_mask, 0));
		SC_ALLOW_ARG_1 ("openat",
				SCMP_A2 (SCMP_CMP_MASKED_EQ, create_mask, 0));
	} else {
		SC_ALLOW_ARG_1 ("open",
				SCMP_A1 (SCMP_CMP_MASKED_EQ, O_ACCMODE,
					 O_RDONLY));
		SC_ALLOW_ARG_1 ("openat",
				SCMP_A2 (SCMP_CMP_MASKED_EQ, O_ACCMODE,
					 O_RDONLY));
	}
	SC_ALLOW ("readlink");
	SC_ALLOW ("readlinkat");
	if (permissive) SC_ALLOW ("rename");
	if (permissive) SC_ALLOW ("renameat");
	if (permissive) SC_ALLOW ("renameat2");
	if (permissive) SC_ALLOW ("rmdir");
	SC_ALLOW ("stat");
	SC_ALLOW ("stat64");
	SC_ALLOW ("statfs");
	SC_ALLOW ("statfs64");
	SC_ALLOW ("statx");
	if (permissive) SC_ALLOW ("symlink");
	if (permissive) SC_ALLOW ("symlinkat");
	if (permissive) SC_ALLOW ("truncate");
	if (permissive) SC_ALLOW ("truncateat");
	if (permissive) SC_ALLOW ("unlink");
	if (permissive) SC_ALLOW ("unlinkat");
	if (permissive) SC_ALLOW ("utime");
	if (permissive) SC_ALLOW ("utimensat");
	if (permissive) SC_ALLOW ("utimes");

	/* systemd: SystemCallFilter=@io-event */
	SC_ALLOW ("_newselect");
	SC_ALLOW ("epoll_create");
	SC_ALLOW ("epoll_create1");
	SC_ALLOW ("epoll_ctl");
	SC_ALLOW ("epoll_ctl_old");
	SC_ALLOW ("epoll_pwait");
	SC_ALLOW ("epoll_wait");
	SC_ALLOW ("epoll_wait_old");
	SC_ALLOW ("eventfd");
	SC_ALLOW ("eventfd2");
	SC_ALLOW ("poll");
	SC_ALLOW ("ppoll");
	SC_ALLOW ("pselect6");
	SC_ALLOW ("select");

	/* systemd: SystemCallFilter=@ipc (subset) */
	SC_ALLOW ("pipe");
	SC_ALLOW ("pipe2");

	/* systemd: SystemCallFilter=@process (subset) */
	SC_ALLOW ("arch_prctl");
	SC_ALLOW ("capget");
	SC_ALLOW ("clone");
	SC_ALLOW ("execveat");
	SC_ALLOW ("fork");
	SC_ALLOW ("getrusage");
	SC_ALLOW ("prctl");
	SC_ALLOW ("vfork");
	SC_ALLOW ("wait4");
	SC_ALLOW ("waitid");
	SC_ALLOW ("waitpid");

	/* systemd: SystemCallFilter=@signal */
	SC_ALLOW ("rt_sigaction");
	SC_ALLOW ("rt_sigpending");
	SC_ALLOW ("rt_sigprocmask");
	SC_ALLOW ("rt_sigsuspend");
	SC_ALLOW ("rt_sigtimedwait");
	SC_ALLOW ("sigaction");
	SC_ALLOW ("sigaltstack");
	SC_ALLOW ("signal");
	SC_ALLOW ("signalfd");
	SC_ALLOW ("signalfd4");
	SC_ALLOW ("sigpending");
	SC_ALLOW ("sigprocmask");
	SC_ALLOW ("sigsuspend");

	/* systemd: SystemCallFilter=@sync */
	SC_ALLOW ("fdatasync");
	SC_ALLOW ("fsync");
	SC_ALLOW ("msync");
	SC_ALLOW ("sync");
	SC_ALLOW ("sync_file_range");
	SC_ALLOW ("syncfs");

	/* Extra syscalls not in any of systemd's sets. */
	SC_ALLOW ("brk");
	SC_ALLOW ("fadvise64");
	SC_ALLOW ("fadvise64_64");
	if (permissive) SC_ALLOW ("ioctl");
	SC_ALLOW ("mprotect");
	SC_ALLOW ("sysinfo");
	SC_ALLOW ("uname");

#undef SC_ALLOW_ARG_2
#undef SC_ALLOW_ARG_1
#undef SC_ALLOW

	return ctx;
}
#endif /* HAVE_LIBSECCOMP */

/* Create a sandbox for processing untrusted data.
 *
 * This only sets up data structures; the caller must call sandbox_load to
 * actually enter the sandbox.
 */
man_sandbox *sandbox_init (void)
{
	man_sandbox *sandbox = XZALLOC (man_sandbox);

#ifdef HAVE_LIBSECCOMP
	sandbox->ctx = make_seccomp_filter (0);
	sandbox->permissive_ctx = make_seccomp_filter (1);
#else /* !HAVE_LIBSECCOMP */
	sandbox->dummy = 0;
#endif /* HAVE_LIBSECCOMP */

	return sandbox;
}

typedef struct man_sandbox_op {
	man_sandbox *sandbox;
	int permissive;
} man_sandbox_op;

/* Attach a sandbox to a pipeline command. */
void sandbox_attach (man_sandbox *sandbox, pipecmd *cmd) {
	man_sandbox_op *sandbox_op = XZALLOC (man_sandbox_op);
	sandbox_op->sandbox = sandbox;
	sandbox_op->permissive = 0;
	pipecmd_pre_exec (cmd, sandbox_load, sandbox_free, sandbox_op);
}

/* Attach a sandbox to a pipeline command, allowing limited file creation. */
void sandbox_attach_permissive (man_sandbox *sandbox, pipecmd *cmd) {
	man_sandbox_op *sandbox_op = XZALLOC (man_sandbox_op);
	sandbox_op->sandbox = sandbox;
	sandbox_op->permissive = 1;
	pipecmd_pre_exec (cmd, sandbox_load, sandbox_free, sandbox_op);
}

/* Enter a sandbox for processing untrusted data. */
void sandbox_load (void *data) {
	man_sandbox_op *sandbox_op = data;

#ifdef HAVE_LIBSECCOMP
	if (can_load_seccomp ()) {
		scmp_filter_ctx ctx;

		debug ("loading seccomp filter (permissive: %d)\n",
		       sandbox_op->permissive);
		if (sandbox_op->permissive)
			ctx = sandbox_op->sandbox->permissive_ctx;
		else
			ctx = sandbox_op->sandbox->ctx;
		if (seccomp_load (ctx) < 0)
			error (FATAL, errno, "can't load seccomp filter");
	}
#endif /* HAVE_LIBSECCOMP */
}

/* Free a sandbox for processing untrusted data. */
void sandbox_free (void *data) {
	man_sandbox_op *sandbox_op = data;

#ifdef HAVE_LIBSECCOMP
	seccomp_release (sandbox_op->sandbox->ctx);
#endif /* HAVE_LIBSECCOMP */

	free (sandbox_op->sandbox);
	free (sandbox_op);
}