aboutsummaryrefslogtreecommitdiffstats
path: root/src/fseccomp/seccomp_file.c
blob: 872b412618ca3bb8435ea130e45418cbdffef23c (plain) (blame)
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
/*
 * Copyright (C) 2014-2020 Firejail Authors
 *
 * This file is part of firejail project
 *
 * This program 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.
 *
 * This program 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "fseccomp.h"
#include "../include/seccomp.h"
#include <sys/syscall.h>

void write_to_file(int fd, const void *data, size_t size) {
	assert(data);
	assert(size);

	size_t written = 0;
	while (written < size) {
		int rv = write(fd, (unsigned char *) data + written, size - written);
		if (rv == -1) {
			fprintf(stderr, "Error fseccomp: cannot write seccomp file\n");
			exit(1);
		}
		written += rv;
	}
}

void filter_init(int fd, bool native) {
	struct sock_filter filter_native[] = {
		VALIDATE_ARCHITECTURE,
#if defined(__x86_64__)
		EXAMINE_SYSCALL,
		HANDLE_X32
#else
		EXAMINE_SYSCALL
#endif
	};
	struct sock_filter filter_32[] = {
		VALIDATE_ARCHITECTURE_32,
		EXAMINE_SYSCALL
	};

#if 0
{
	int i;
	unsigned char *ptr = (unsigned char *) &filter[0];
	for (i = 0; i < sizeof(filter); i++, ptr++)
		printf("%x, ", (*ptr) & 0xff);
	printf("\n");
}
#endif

	if (native)
		write_to_file(fd, filter_native, sizeof(filter_native));
	else
		write_to_file(fd, filter_32, sizeof(filter_32));
}

static void write_whitelist(int fd, int syscall) {
	struct sock_filter filter[] = {
		WHITELIST(syscall)
	};
	write_to_file(fd, filter, sizeof(filter));
}

static void write_blacklist(int fd, int syscall) {
	struct sock_filter filter[] = {
		BLACKLIST(syscall)
	};
	write_to_file(fd, filter, sizeof(filter));
}

void filter_add_whitelist(int fd, int syscall, int arg, void *ptrarg, bool native) {
	(void) arg;
	(void) ptrarg;
	(void) native;

	if (syscall >= 0) {
		write_whitelist(fd, syscall);
	}
}

// handle seccomp list exceptions (seccomp x,y,!z)
void filter_add_whitelist_for_excluded(int fd, int syscall, int arg, void *ptrarg, bool native) {
	(void) arg;
	(void) ptrarg;
	(void) native;

	if (syscall < 0) {
		write_whitelist(fd, -syscall);
	}
}

void filter_add_blacklist(int fd, int syscall, int arg, void *ptrarg, bool native) {
	(void) arg;
	(void) ptrarg;
	(void) native;

	if (syscall >= 0) {
		write_blacklist(fd, syscall);
	}
}

// handle seccomp list exceptions (seccomp x,y,!z)
void filter_add_blacklist_for_excluded(int fd, int syscall, int arg, void *ptrarg, bool native) {
	(void) arg;
	(void) ptrarg;
	(void) native;

	if (syscall < 0) {
		write_blacklist(fd, -syscall);
	}
}

void filter_add_errno(int fd, int syscall, int arg, void *ptrarg, bool native) {
	(void) ptrarg;
	(void) native;

	struct sock_filter filter[] = {
		BLACKLIST_ERRNO(syscall, arg)
	};
	write_to_file(fd, filter, sizeof(filter));
}

void filter_end_blacklist(int fd) {
	struct sock_filter filter[] = {
		RETURN_ALLOW
	};
	write_to_file(fd, filter, sizeof(filter));
}

void filter_end_whitelist(int fd) {
	struct sock_filter filter[] = {
		KILL_PROCESS
	};
	write_to_file(fd, filter, sizeof(filter));
}