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_LOG 0x7ffc0000U /* allow after logging */
52 #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
53
54 #define SECCOMP_RET_DATA 0x0000ffffU /* mask for return value */
55
56 struct seccomp_data {
57 int nr;
58 __u32 arch;
59 __u64 instruction_pointer;
60 __u64 args[6];
61 };
62
63 #define syscall_nr (offsetof(struct seccomp_data, nr))
64 #define arch_nr (offsetof(struct seccomp_data, arch))
65
66 /* Size-dependent defines. */
67 #if defined(BITS32)
68 /*
69 * On 32 bits, comparisons take 2 instructions: 1 for loading the argument,
70 * 1 for the actual comparison.
71 */
72 #define BPF_LOAD_ARG_LEN 1U
73 #define BPF_COMP_LEN 1U
74 #define BPF_SHORT_GT_GE_COMP_LEN 1U
75 #define BPF_GT_GE_COMP_LEN 1U
76 #define BPF_ARG_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_COMP_LEN)
77 #define BPF_ARG_SHORT_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_SHORT_GT_GE_COMP_LEN)
78 #define BPF_ARG_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_GT_GE_COMP_LEN)
79
80 #define bpf_comp_jeq bpf_comp_jeq32
81 #define bpf_comp_jgt bpf_comp_jgt32
82 #define bpf_comp_jge bpf_comp_jge32
83 #define bpf_comp_jset bpf_comp_jset32
84
85 #define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
86
87 #elif defined(BITS64)
88 /*
89 * On 64 bits, comparisons take 7-8 instructions: 4 for loading the argument,
90 * and 3-4 for the actual comparison.
91 */
92 #define BPF_LOAD_ARG_LEN 4U
93 #define BPF_COMP_LEN 3U
94 #define BPF_SHORT_GT_GE_COMP_LEN 3U
95 #define BPF_GT_GE_COMP_LEN 4U
96 #define BPF_ARG_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_COMP_LEN)
97 #define BPF_ARG_SHORT_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_SHORT_GT_GE_COMP_LEN)
98 #define BPF_ARG_GT_GE_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_GT_GE_COMP_LEN)
99
100 #define bpf_comp_jeq bpf_comp_jeq64
101 #define bpf_comp_jgt bpf_comp_jgt64
102 #define bpf_comp_jge bpf_comp_jge64
103 #define bpf_comp_jset bpf_comp_jset64
104
105 /* Ensure that we load the logically correct offset. */
106 #if defined(__LITTLE_ENDIAN__) || __BYTE_ORDER == __LITTLE_ENDIAN
107 #define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
108 #define HI_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) + sizeof(__u32)
109 #else
110 #error "Unsupported endianness"
111 #endif
112
113 #else
114 #error "Unknown bit width"
115
116 #endif
117
118 /* Common jump targets. */
119 #define NEXT 0
120 #define SKIP 1
121 #define SKIPN(_n) (_n)
122
123 /* Support for labels in BPF programs. */
124 #define JUMP_JT 0xff
125 #define JUMP_JF 0xff
126 #define LABEL_JT 0xfe
127 #define LABEL_JF 0xfe
128
129 #define MAX_BPF_LABEL_LEN 32
130
131 #define BPF_LABELS_MAX 512U /* Each syscall could have an argument block. */
132 struct bpf_labels {
133 size_t count;
134 struct __bpf_label {
135 const char *label;
136 unsigned int location;
137 } labels[BPF_LABELS_MAX];
138 };
139
140 /* 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)141 static inline size_t set_bpf_instr(struct sock_filter *instr,
142 unsigned short code, unsigned int k,
143 unsigned char jt, unsigned char jf)
144 {
145 instr->code = code;
146 instr->k = k;
147 instr->jt = jt;
148 instr->jf = jf;
149 return 1U;
150 }
151
152 #define set_bpf_stmt(_block, _code, _k) \
153 set_bpf_instr((_block), (_code), (_k), 0, 0)
154
155 #define set_bpf_jump(_block, _code, _k, _jt, _jf) \
156 set_bpf_instr((_block), (_code), (_k), (_jt), (_jf))
157
158 #define set_bpf_lbl(_block, _lbl_id) \
159 set_bpf_jump((_block), BPF_JMP+BPF_JA, (_lbl_id), \
160 LABEL_JT, LABEL_JF)
161
162 #define set_bpf_jump_lbl(_block, _lbl_id) \
163 set_bpf_jump((_block), BPF_JMP+BPF_JA, (_lbl_id), \
164 JUMP_JT, JUMP_JF)
165
166 #define set_bpf_ret_kill(_block) \
167 set_bpf_stmt((_block), BPF_RET+BPF_K, SECCOMP_RET_KILL)
168
169 #define set_bpf_ret_trap(_block) \
170 set_bpf_stmt((_block), BPF_RET+BPF_K, SECCOMP_RET_TRAP)
171
172 #define set_bpf_ret_errno(_block, _errno) \
173 set_bpf_stmt((_block), BPF_RET+BPF_K, \
174 SECCOMP_RET_ERRNO | ((_errno) & SECCOMP_RET_DATA))
175
176 #define set_bpf_ret_log(_block) \
177 set_bpf_stmt((_block), BPF_RET+BPF_K, SECCOMP_RET_LOG)
178
179 #define set_bpf_ret_allow(_block) \
180 set_bpf_stmt((_block), BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
181
182 #define bpf_load_syscall_nr(_filter) \
183 set_bpf_stmt((_filter), BPF_LD+BPF_W+BPF_ABS, syscall_nr)
184
185 /* BPF label functions. */
186 int bpf_resolve_jumps(struct bpf_labels *labels,
187 struct sock_filter *filter, size_t count);
188 int bpf_label_id(struct bpf_labels *labels, const char *label);
189 void free_label_strings(struct bpf_labels *labels);
190
191 /* BPF helper functions. */
192 size_t bpf_load_arg(struct sock_filter *filter, int argidx);
193 size_t bpf_comp_jeq(struct sock_filter *filter, unsigned long c,
194 unsigned char jt, unsigned char jf);
195 size_t bpf_comp_jgt(struct sock_filter *filter, unsigned long c,
196 unsigned char jt, unsigned char jf);
197 size_t bpf_comp_jge(struct sock_filter *filter, unsigned long c,
198 unsigned char jt, unsigned char jf);
199 size_t bpf_comp_jset(struct sock_filter *filter, unsigned long mask,
200 unsigned char jt, unsigned char jf);
201 size_t bpf_comp_jin(struct sock_filter *filter, unsigned long mask,
202 unsigned char jt, unsigned char jf);
203
204 /* Functions called by syscall_filter.c */
205 #define ARCH_VALIDATION_LEN 3U
206 #define ALLOW_SYSCALL_LEN 2U
207
208 size_t bpf_arg_comp(struct sock_filter **pfilter,
209 int op, int argidx, unsigned long c, unsigned int label_id);
210 size_t bpf_validate_arch(struct sock_filter *filter);
211 size_t bpf_allow_syscall(struct sock_filter *filter, int nr);
212 size_t bpf_allow_syscall_args(struct sock_filter *filter,
213 int nr, unsigned int id);
214
215 #ifdef __cplusplus
216 }; /* extern "C" */
217 #endif
218
219 #endif /* BPF_H */
220