• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* bpf.h
2  * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  *
6  * Berkeley Packet Filter functions.
7  */
8 
9 #ifndef BPF_H
10 #define BPF_H
11 
12 #include <asm/bitsperlong.h>   /* for __BITS_PER_LONG */
13 #include <endian.h>
14 #include <linux/audit.h>
15 #include <linux/filter.h>
16 #include <stddef.h>
17 #include <sys/user.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #include "arch.h"
24 
25 #if __BITS_PER_LONG == 32 || defined(__ILP32__)
26 #define BITS32
27 #elif __BITS_PER_LONG == 64
28 #define BITS64
29 #endif
30 
31 /* Constants for comparison operators. */
32 #define MIN_OPERATOR 128
33 enum {
34 	EQ = MIN_OPERATOR,
35 	NE,
36 	LT,
37 	LE,
38 	GT,
39 	GE,
40 	SET,
41 	IN
42 };
43 
44 /*
45  * BPF return values and data structures,
46  * since they're not yet in the kernel.
47  */
48 #define SECCOMP_RET_KILL	0x00000000U /* kill the task immediately */
49 #define SECCOMP_RET_TRAP	0x00030000U /* return SIGSYS */
50 #define SECCOMP_RET_ERRNO	0x00050000U /* return -1 and set errno */
51 #define SECCOMP_RET_ALLOW	0x7fff0000U /* allow */
52 
53 #define SECCOMP_RET_DATA	0x0000ffffU /* mask for return value */
54 
55 struct seccomp_data {
56 	int nr;
57 	__u32 arch;
58 	__u64 instruction_pointer;
59 	__u64 args[6];
60 };
61 
62 #define syscall_nr (offsetof(struct seccomp_data, nr))
63 #define arch_nr (offsetof(struct seccomp_data, arch))
64 
65 /* Size-dependent defines. */
66 #if defined(BITS32)
67 /*
68  * On 32 bits, comparisons take 2 instructions: 1 for loading the argument,
69  * 1 for the actual comparison.
70  */
71 #define BPF_LOAD_ARG_LEN		1U
72 #define BPF_COMP_LEN			1U
73 #define BPF_SHORT_GT_GE_COMP_LEN	1U
74 #define BPF_GT_GE_COMP_LEN		1U
75 #define BPF_ARG_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_COMP_LEN)
76 #define BPF_ARG_SHORT_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_SHORT_GT_GE_COMP_LEN)
77 #define BPF_ARG_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_GT_GE_COMP_LEN)
78 
79 #define bpf_comp_jeq bpf_comp_jeq32
80 #define bpf_comp_jgt bpf_comp_jgt32
81 #define bpf_comp_jge bpf_comp_jge32
82 #define bpf_comp_jset bpf_comp_jset32
83 
84 #define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
85 
86 #elif defined(BITS64)
87 /*
88  * On 64 bits, comparisons take 7-8 instructions: 4 for loading the argument,
89  * and 3-4 for the actual comparison.
90  */
91 #define BPF_LOAD_ARG_LEN		4U
92 #define BPF_COMP_LEN			3U
93 #define BPF_SHORT_GT_GE_COMP_LEN	3U
94 #define BPF_GT_GE_COMP_LEN		4U
95 #define BPF_ARG_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_COMP_LEN)
96 #define BPF_ARG_SHORT_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_SHORT_GT_GE_COMP_LEN)
97 #define BPF_ARG_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_GT_GE_COMP_LEN)
98 
99 #define bpf_comp_jeq bpf_comp_jeq64
100 #define bpf_comp_jgt bpf_comp_jgt64
101 #define bpf_comp_jge bpf_comp_jge64
102 #define bpf_comp_jset bpf_comp_jset64
103 
104 /* Ensure that we load the logically correct offset. */
105 #if defined(__LITTLE_ENDIAN__) || __BYTE_ORDER == __LITTLE_ENDIAN
106 #define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
107 #define HI_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) + sizeof(__u32)
108 #else
109 #error "Unsupported endianness"
110 #endif
111 
112 #else
113 #error "Unknown bit width"
114 
115 #endif
116 
117 /* Common jump targets. */
118 #define NEXT 0
119 #define SKIP 1
120 #define SKIPN(_n) (_n)
121 
122 /* Support for labels in BPF programs. */
123 #define JUMP_JT 0xff
124 #define JUMP_JF 0xff
125 #define LABEL_JT 0xfe
126 #define LABEL_JF 0xfe
127 
128 #define MAX_BPF_LABEL_LEN 32
129 
130 #define BPF_LABELS_MAX 512U	/* Each syscall could have an argument block. */
131 struct bpf_labels {
132 	size_t count;
133 	struct __bpf_label {
134 		const char *label;
135 		unsigned int location;
136 	} labels[BPF_LABELS_MAX];
137 };
138 
139 /* BPF instruction manipulation functions and macros. */
set_bpf_instr(struct sock_filter * instr,unsigned short code,unsigned int k,unsigned char jt,unsigned char jf)140 static inline size_t set_bpf_instr(struct sock_filter *instr,
141 				   unsigned short code, unsigned int k,
142 				   unsigned char jt, unsigned char jf)
143 {
144 	instr->code = code;
145 	instr->k = k;
146 	instr->jt = jt;
147 	instr->jf = jf;
148 	return 1U;
149 }
150 
151 #define set_bpf_stmt(_block, _code, _k) \
152 	set_bpf_instr((_block), (_code), (_k), 0, 0)
153 
154 #define set_bpf_jump(_block, _code, _k, _jt, _jf) \
155 	set_bpf_instr((_block), (_code), (_k), (_jt), (_jf))
156 
157 #define set_bpf_lbl(_block, _lbl_id) \
158 	set_bpf_jump((_block), BPF_JMP+BPF_JA, (_lbl_id), \
159 			LABEL_JT, LABEL_JF)
160 
161 #define set_bpf_jump_lbl(_block, _lbl_id) \
162 	set_bpf_jump((_block), BPF_JMP+BPF_JA, (_lbl_id), \
163 			JUMP_JT, JUMP_JF)
164 
165 #define set_bpf_ret_kill(_block) \
166 	set_bpf_stmt((_block), BPF_RET+BPF_K, SECCOMP_RET_KILL)
167 
168 #define set_bpf_ret_trap(_block) \
169 	set_bpf_stmt((_block), BPF_RET+BPF_K, SECCOMP_RET_TRAP)
170 
171 #define set_bpf_ret_errno(_block, _errno) \
172 	set_bpf_stmt((_block), BPF_RET+BPF_K, \
173 		SECCOMP_RET_ERRNO | ((_errno) & SECCOMP_RET_DATA))
174 
175 #define set_bpf_ret_allow(_block) \
176 	set_bpf_stmt((_block), BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
177 
178 #define bpf_load_syscall_nr(_filter) \
179 	set_bpf_stmt((_filter), BPF_LD+BPF_W+BPF_ABS, syscall_nr)
180 
181 /* BPF label functions. */
182 int bpf_resolve_jumps(struct bpf_labels *labels,
183 		struct sock_filter *filter, size_t count);
184 int bpf_label_id(struct bpf_labels *labels, const char *label);
185 void free_label_strings(struct bpf_labels *labels);
186 
187 /* BPF helper functions. */
188 size_t bpf_load_arg(struct sock_filter *filter, int argidx);
189 size_t bpf_comp_jeq(struct sock_filter *filter, unsigned long c,
190 		    unsigned char jt, unsigned char jf);
191 size_t bpf_comp_jgt(struct sock_filter *filter, unsigned long c,
192 		    unsigned char jt, unsigned char jf);
193 size_t bpf_comp_jge(struct sock_filter *filter, unsigned long c,
194 		    unsigned char jt, unsigned char jf);
195 size_t bpf_comp_jset(struct sock_filter *filter, unsigned long mask,
196 		     unsigned char jt, unsigned char jf);
197 size_t bpf_comp_jin(struct sock_filter *filter, unsigned long mask,
198 		    unsigned char jt, unsigned char jf);
199 
200 /* Functions called by syscall_filter.c */
201 #define ARCH_VALIDATION_LEN 3U
202 #define ALLOW_SYSCALL_LEN 2U
203 
204 size_t bpf_arg_comp(struct sock_filter **pfilter,
205 		int op, int argidx, unsigned long c, unsigned int label_id);
206 size_t bpf_validate_arch(struct sock_filter *filter);
207 size_t bpf_allow_syscall(struct sock_filter *filter, int nr);
208 size_t bpf_allow_syscall_args(struct sock_filter *filter,
209 		int nr, unsigned int id);
210 
211 #ifdef __cplusplus
212 }; /* extern "C" */
213 #endif
214 
215 #endif /* BPF_H */
216