summaryrefslogtreecommitdiff
path: root/src/gen_pfc.c
blob: 62dd9842dd50d29b47947f7010ba58461a891ef2 (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
/**
 * Seccomp Pseudo Filter Code (PFC) Generator
 *
 * Copyright (c) 2012 Red Hat <pmoore@redhat.com>
 * Author: Paul Moore <paul@paul-moore.com>
 */

/*
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of version 2.1 of the GNU Lesser General Public License as
 * published by the Free Software Foundation.
 *
 * This library 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses>.
 */

#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

/* NOTE: needed for the arch->token decoding in _pfc_arch() */
#include <linux/audit.h>

#include <seccomp.h>

#include "arch.h"
#include "db.h"
#include "gen_pfc.h"
#include "helper.h"
#include "system.h"

struct pfc_sys_list {
	struct db_sys_list *sys;
	struct pfc_sys_list *next;
};

/* XXX - we should check the fprintf() return values */

/**
 * Display a string representation of the architecture
 * @param arch the architecture definition
 */
static const char *_pfc_arch(const struct arch_def *arch)
{
	switch (arch->token) {
	case SCMP_ARCH_X86:
		return "x86";
	case SCMP_ARCH_X86_64:
		return "x86_64";
	case SCMP_ARCH_X32:
		return "x32";
	case SCMP_ARCH_ARM:
		return "arm";
	case SCMP_ARCH_AARCH64:
		return "aarch64";
	case SCMP_ARCH_LOONGARCH64:
		return "loongarch64";
	case SCMP_ARCH_M68K:
		return "m68k";
	case SCMP_ARCH_MIPS:
		return "mips";
	case SCMP_ARCH_MIPSEL:
		return "mipsel";
	case SCMP_ARCH_MIPS64:
		return "mips64";
	case SCMP_ARCH_MIPSEL64:
		return "mipsel64";
	case SCMP_ARCH_MIPS64N32:
		return "mips64n32";
	case SCMP_ARCH_MIPSEL64N32:
		return "mipsel64n32";
	case SCMP_ARCH_PARISC:
		return "parisc";
	case SCMP_ARCH_PARISC64:
		return "parisc64";
	case SCMP_ARCH_PPC64:
		return "ppc64";
	case SCMP_ARCH_PPC64LE:
		return "ppc64le";
	case SCMP_ARCH_PPC:
		return "ppc";
	case SCMP_ARCH_S390X:
		return "s390x";
	case SCMP_ARCH_S390:
		return "s390";
	case SCMP_ARCH_RISCV64:
		return "riscv64";
	case SCMP_ARCH_SHEB:
		return "sheb";
	case SCMP_ARCH_SH:
		return "sh";
	default:
		return "UNKNOWN";
	}
}

/**
 * Display a string representation of the node argument
 * @param fds the file stream to send the output
 * @param arch the architecture definition
 * @param node the node
 */
static void _pfc_arg(FILE *fds,
		     const struct arch_def *arch,
		     const struct db_arg_chain_tree *node)
{
	if (arch->size == ARCH_SIZE_64) {
		if (arch_arg_offset_hi(arch, node->arg) == node->arg_offset)
			fprintf(fds, "$a%d.hi32", node->arg);
		else
			fprintf(fds, "$a%d.lo32", node->arg);
	} else
		fprintf(fds, "$a%d", node->arg);
}

/**
 * Display a string representation of the filter action
 * @param fds the file stream to send the output
 * @param action the action
 */
static void _pfc_action(FILE *fds, uint32_t action)
{
	switch (action & SECCOMP_RET_ACTION_FULL) {
	case SCMP_ACT_KILL_PROCESS:
		fprintf(fds, "action KILL_PROCESS;\n");
		break;
	case SCMP_ACT_KILL_THREAD:
		fprintf(fds, "action KILL;\n");
		break;
	case SCMP_ACT_TRAP:
		fprintf(fds, "action TRAP;\n");
		break;
	case SCMP_ACT_ERRNO(0):
		fprintf(fds, "action ERRNO(%u);\n", (action & 0x0000ffff));
		break;
	case SCMP_ACT_TRACE(0):
		fprintf(fds, "action TRACE(%u);\n", (action & 0x0000ffff));
		break;
	case SCMP_ACT_LOG:
		fprintf(fds, "action LOG;\n");
		break;
	case SCMP_ACT_ALLOW:
		fprintf(fds, "action ALLOW;\n");
		break;
	default:
		fprintf(fds, "action 0x%x;\n", action);
	}
}

/**
 * Indent the output stream
 * @param fds the file stream to send the output
 * @param lvl the indentation level
 *
 * This function indents the output stream with whitespace based on the
 * requested indentation level.
 */
static void _indent(FILE *fds, unsigned int lvl)
{
	while (lvl-- > 0)
		fprintf(fds, "  ");
}

/**
 * Generate the pseudo filter code for an argument chain
 * @param arch the architecture definition
 * @param node the head of the argument chain
 * @param lvl the indentation level
 * @param fds the file stream to send the output
 *
 * This function generates the pseudo filter code representation of the given
 * argument chain and writes it to the given output stream.
 *
 */
static void _gen_pfc_chain(const struct arch_def *arch,
			   const struct db_arg_chain_tree *node,
			   unsigned int lvl, FILE *fds)
{
	const struct db_arg_chain_tree *c_iter;

	/* get to the start */
	c_iter = node;
	while (c_iter->lvl_prv != NULL)
		c_iter = c_iter->lvl_prv;

	while (c_iter != NULL) {
		/* comparison operation */
		_indent(fds, lvl);
		fprintf(fds, "if (");
		_pfc_arg(fds, arch, c_iter);
		switch (c_iter->op) {
		case SCMP_CMP_EQ:
			fprintf(fds, " == ");
			break;
		case SCMP_CMP_GE:
			fprintf(fds, " >= ");
			break;
		case SCMP_CMP_GT:
			fprintf(fds, " > ");
			break;
		case SCMP_CMP_MASKED_EQ:
			fprintf(fds, " & 0x%.8x == ", c_iter->mask);
			break;
		case SCMP_CMP_NE:
		case SCMP_CMP_LT:
		case SCMP_CMP_LE:
		default:
			fprintf(fds, " ??? ");
		}
		fprintf(fds, "%u)\n", c_iter->datum);

		/* true result */
		if (c_iter->act_t_flg) {
			_indent(fds, lvl + 1);
			_pfc_action(fds, c_iter->act_t);
		} else if (c_iter->nxt_t != NULL)
			_gen_pfc_chain(arch, c_iter->nxt_t, lvl + 1, fds);

		/* false result */
		if (c_iter->act_f_flg) {
			_indent(fds, lvl);
			fprintf(fds, "else\n");
			_indent(fds, lvl + 1);
			_pfc_action(fds, c_iter->act_f);
		} else if (c_iter->nxt_f != NULL) {
			_indent(fds, lvl);
			fprintf(fds, "else\n");
			_gen_pfc_chain(arch, c_iter->nxt_f, lvl + 1, fds);
		}

		c_iter = c_iter->lvl_nxt;
	}
}

/**
 * Generate pseudo filter code for a syscall
 * @param arch the architecture definition
 * @param sys the syscall filter
 * @param fds the file stream to send the output
 *
 * This function generates a pseudo filter code representation of the given
 * syscall filter and writes it to the given output stream.
 *
 */
static void _gen_pfc_syscall(const struct arch_def *arch,
			     const struct db_sys_list *sys, FILE *fds,
			     int lvl)
{
	unsigned int sys_num = sys->num;
	const char *sys_name = arch_syscall_resolve_num(arch, sys_num);

	_indent(fds, lvl);
	fprintf(fds, "# filter for syscall \"%s\" (%u) [priority: %d]\n",
		(sys_name ? sys_name : "UNKNOWN"), sys_num, sys->priority);
	_indent(fds, lvl);
	fprintf(fds, "if ($syscall == %u)\n", sys_num);
	if (sys->chains == NULL) {
		_indent(fds, lvl + 1);
		_pfc_action(fds, sys->action);
	} else
		_gen_pfc_chain(arch, sys->chains, lvl + 1, fds);
}

#define SYSCALLS_PER_NODE		(4)
static int _get_bintree_levels(unsigned int syscall_cnt,
			       uint32_t optimize)
{
	unsigned int i = 0, max_level;

	if (optimize != 2)
		/* Only use a binary tree if requested */
		return 0;

	if (syscall_cnt == 0)
		return 0;

	do {
		max_level = SYSCALLS_PER_NODE << i;
		i++;
	} while(max_level < syscall_cnt);

	return i;
}

static int _get_bintree_syscall_num(const struct pfc_sys_list *cur,
				    int lookahead_cnt,
				    int *const num)
{
	while (lookahead_cnt > 0 && cur != NULL) {
		cur = cur->next;
		lookahead_cnt--;
	}

	if (cur == NULL)
		return -EFAULT;

	*num = cur->sys->num;
	return 0;
}

static int _sys_num_sort(struct db_sys_list *syscalls,
			 struct pfc_sys_list **p_head)
{
	struct pfc_sys_list *p_iter = NULL, *p_new, *p_prev;
	struct db_sys_list *s_iter;

	db_list_foreach(s_iter, syscalls) {
		p_new = zmalloc(sizeof(*p_new));
		if (p_new == NULL) {
			return -ENOMEM;
		}
		p_new->sys = s_iter;

		p_prev = NULL;
		p_iter = *p_head;
		while (p_iter != NULL &&
		       s_iter->num < p_iter->sys->num) {
			p_prev = p_iter;
			p_iter = p_iter->next;
		}
		if (*p_head == NULL)
			*p_head = p_new;
		else if (p_prev == NULL) {
			p_new->next = *p_head;
			*p_head = p_new;
		} else {
			p_new->next = p_iter;
			p_prev->next = p_new;
		}
	}

	return 0;
}

static int _sys_priority_sort(struct db_sys_list *syscalls,
			      struct pfc_sys_list **p_head)
{
	struct pfc_sys_list *p_iter = NULL, *p_new, *p_prev;
	struct db_sys_list *s_iter;

	db_list_foreach(s_iter, syscalls) {
		p_new = zmalloc(sizeof(*p_new));
		if (p_new == NULL) {
			return -ENOMEM;
		}
		p_new->sys = s_iter;

		p_prev = NULL;
		p_iter = *p_head;
		while (p_iter != NULL &&
		       s_iter->priority < p_iter->sys->priority) {
			p_prev = p_iter;
			p_iter = p_iter->next;
		}
		if (*p_head == NULL)
			*p_head = p_new;
		else if (p_prev == NULL) {
			p_new->next = *p_head;
			*p_head = p_new;
		} else {
			p_new->next = p_iter;
			p_prev->next = p_new;
		}
	}

	return 0;
}

static int _sys_sort(struct db_sys_list *syscalls,
		     struct pfc_sys_list **p_head,
		     uint32_t optimize)
{
	if (optimize != 2)
		return _sys_priority_sort(syscalls, p_head);
	else
		/* sort by number for the binary tree */
		return _sys_num_sort(syscalls, p_head);
}

/**
 * Generate pseudo filter code for an architecture
 * @param col the seccomp filter collection
 * @param db the single seccomp filter
 * @param fds the file stream to send the output
 *
 * This function generates a pseudo filter code representation of the given
 * filter DB and writes it to the given output stream.  Returns zero on
 * success, negative values on failure.
 *
 */
static int _gen_pfc_arch(const struct db_filter_col *col,
			 const struct db_filter *db, FILE *fds,
			 uint32_t optimize)
{
	int rc = 0, i = 0, lookahead_num;
	unsigned int syscall_cnt = 0, bintree_levels, level, indent = 1;
	struct pfc_sys_list *p_iter = NULL, *p_head = NULL;

	/* sort the syscall list */
	rc = _sys_sort(db->syscalls, &p_head, optimize);
	if (rc < 0)
		goto arch_return;

	bintree_levels = _get_bintree_levels(db->syscall_cnt, optimize);

	fprintf(fds, "# filter for arch %s (%u)\n",
		_pfc_arch(db->arch), db->arch->token_bpf);
	fprintf(fds, "if ($arch == %u)\n", db->arch->token_bpf);
	p_iter = p_head;
	while (p_iter != NULL) {
		if (!p_iter->sys->valid) {
			p_iter = p_iter->next;
			continue;
		}

		for (i = bintree_levels - 1; i > 0; i--) {
			level = SYSCALLS_PER_NODE << i;

			if (syscall_cnt == 0 || (syscall_cnt % level) == 0) {
				rc = _get_bintree_syscall_num(p_iter, level / 2,
							      &lookahead_num);
				if (rc < 0)
					/* We have reached the end of the bintree.
					 * There aren't enough syscalls to construct
					 * any more if-elses.
					 */
					continue;
				_indent(fds, indent);
				fprintf(fds, "if ($syscall > %u)\n", lookahead_num);
				indent++;
			} else if ((syscall_cnt % (level / 2)) == 0) {
				lookahead_num = p_iter->sys->num;
				_indent(fds, indent - 1);
				fprintf(fds, "else # ($syscall <= %u)\n",
					p_iter->sys->num);
			}

		}

		_gen_pfc_syscall(db->arch, p_iter->sys, fds, indent);
		syscall_cnt++;
		p_iter = p_iter->next;

		/* undo the indentations as the else statements complete */
		for (i = 0; i < bintree_levels; i++) {
			if (syscall_cnt % ((SYSCALLS_PER_NODE * 2) << i) == 0)
				indent--;
		}
	}
	_indent(fds, 1);
	fprintf(fds, "# default action\n");
	_indent(fds, 1);
	_pfc_action(fds, col->attr.act_default);

arch_return:
	while (p_head != NULL) {
		p_iter = p_head;
		p_head = p_head->next;
		free(p_iter);
	}
	return rc;
}
/**
 * Generate a pseudo filter code string representation
 * @param col the seccomp filter collection
 * @param fd the fd to send the output
 *
 * This function generates a pseudo filter code representation of the given
 * filter collection and writes it to the given fd.  Returns zero on success,
 * negative errno values on failure.
 *
 */
int gen_pfc_generate(const struct db_filter_col *col, int fd)
{
	int newfd;
	unsigned int iter;
	FILE *fds;

	newfd = dup(fd);
	if (newfd < 0)
		return -errno;
	fds = fdopen(newfd, "a");
	if (fds == NULL) {
		close(newfd);
		return -errno;
	}

	/* generate the pfc */
	fprintf(fds, "#\n");
	fprintf(fds, "# pseudo filter code start\n");
	fprintf(fds, "#\n");

	for (iter = 0; iter < col->filter_cnt; iter++)
		_gen_pfc_arch(col, col->filters[iter], fds,
			      col->attr.optimize);

	fprintf(fds, "# invalid architecture action\n");
	_pfc_action(fds, col->attr.act_badarch);
	fprintf(fds, "#\n");
	fprintf(fds, "# pseudo filter code end\n");
	fprintf(fds, "#\n");

	fflush(fds);
	fclose(fds);

	return 0;
}