summaryrefslogtreecommitdiff
path: root/tests/seccomp.c
blob: 8427fd9f7b6596ced852a4a82bcb2670f7a663ea (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
/*
 * Copyright (C) 2013 Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS test suite.
 *
 * ocserv 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.
 *
 * ocserv 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include "utils.h"

#ifdef HAVE_LIBSECCOMP

#include <seccomp.h>
#include <errno.h>
#include <string.h>

int disable_system_calls(void)
{
	int ret;
	scmp_filter_ctx ctx;

	/*ctx = seccomp_init(SCMP_ACT_ERRNO(EPERM));*/
	ctx = seccomp_init(SCMP_ACT_TRAP);
	if (ctx == NULL) {
		fprintf(stderr, "could not initialize seccomp");
		return -1;
	}

#define ADD_SYSCALL(name, ...) \
	ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(name), __VA_ARGS__); \
	/* libseccomp returns EDOM for pseudo-syscalls due to a bug */ \
	if (ret < 0 && ret != -EDOM) { \
		fprintf(stderr, "could not add " #name " to seccomp filter: %s", strerror(-ret)); \
		ret = -1; \
		goto fail; \
	}

	ADD_SYSCALL(nanosleep, 0);
	ADD_SYSCALL(time, 0);
	ADD_SYSCALL(gettimeofday, 0);
#if defined(HAVE_CLOCK_GETTIME)
	ADD_SYSCALL(clock_gettime, 0);
#endif

	ADD_SYSCALL(getrusage, 0);

	/* recv/send for the default pull/push functions. It is unknown
	 * which syscall is used by libc and varies from system to system
	 * so we enable all */
	ADD_SYSCALL(recvmsg, 0);
	ADD_SYSCALL(sendmsg, 0);
	ADD_SYSCALL(send, 0);
	ADD_SYSCALL(recv, 0);
	ADD_SYSCALL(sendto, 0);
	ADD_SYSCALL(recvfrom, 0);

	/* to read from /dev/urandom */
	ADD_SYSCALL(read, 0);
	ADD_SYSCALL(getrandom, 0);

	/* we use it in select */
	ADD_SYSCALL(sigprocmask, 0);
	ADD_SYSCALL(rt_sigprocmask, 0);

	/* used in to detect reading timeouts */
	ADD_SYSCALL(select, 0);
	/* in x86, glibc uses _newselect() */
	ADD_SYSCALL(_newselect, 0);

	/* for memory allocation */
	ADD_SYSCALL(brk, 0);

	/* the following are for generic operations, not specific to
	 * gnutls. */
	ADD_SYSCALL(close, 0);
	ADD_SYSCALL(exit, 0);
	ADD_SYSCALL(exit_group, 0);

	/* allow returning from signal handlers */
	ADD_SYSCALL(sigreturn, 0);
	ADD_SYSCALL(rt_sigreturn, 0);

	ret = seccomp_load(ctx);
	if (ret < 0) {
		fprintf(stderr, "could not load seccomp filter");
		ret = -1;
		goto fail;
	}
	
	ret = 0;

fail:
	seccomp_release(ctx);
	return ret;
}
#else
int disable_system_calls(void)
{
	return 0;
}
#endif