aboutsummaryrefslogtreecommitdiffstats
path: root/lib/chaseccomp/chaseccomp.h
blob: 3d123062058a8b40e4548cb7cce9249222adc904 (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
#include <stdint.h>

/*
 * seccomp
 */

#define SECCOMP_SET_MODE_FILTER	1

#define SECCOMP_RET_KILL_PROCESS	0x80000000u
#define SECCOMP_RET_ALLOW		0x7FFF0000u
#define SECCOMP_RET_TRAP		0x00030000u
#define SECCOMP_RET_ERRNO		0x00050000u
#define SECCOMP_RET_DATA		0x0000FFFFu

struct seccomp_data {
	int nr;
	uint32_t arch;
	uint64_t instruction_pointer;
	uint64_t args[6];
};

/*
 * BPF
 */

/* instruction classes */
#define BPF_LD			0x00
#define BPF_JMP			0x05
#define BPF_RET			0x06

/* ld/ldx fields */
#define BPF_ABS			0x20
#define BPF_W			0x00

/* alu/jmp fields */
#define BPF_JEQ			0x10
#define BPF_JGT			0x20

#define BPF_K			0x00

struct sock_filter {
	uint16_t code;
	uint8_t jt;
	uint8_t jf;
	uint32_t k;
};

struct sock_fprog {
	unsigned short len;
	struct sock_filter *filter;
};

#define BPF_STMT(code, k) { (unsigned short)(code), 0, 0, k }
#define BPF_JUMP(code, k, jt, jf) { (unsigned short)(code), jt, jf, k }

/*
 * chaseccomp stuff
 */

#define COUNTOF(x) (sizeof(x) / sizeof(*(x)))

#define CHA_BPF_LOAD(field) \
	BPF_STMT(BPF_LD | BPF_W | BPF_ABS, \
	    (offsetof(struct seccomp_data, field)))

#define CHA_BPF_RET(val)	BPF_STMT(BPF_RET | BPF_K, val)
#define CHA_BPF_JE(data, n)	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, data, n, 0)
#define CHA_BPF_JNE(data, m)	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, data, 0, m)
#define CHA_BPF_JLE(data, m)	BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, data, 0, m)