]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Linux Socket Filter - Kernel level socket filtering | |
3 | * | |
4 | * Based on the design of the Berkeley Packet Filter. The new | |
5 | * internal format has been designed by PLUMgrid: | |
6 | * | |
7 | * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com | |
8 | * | |
9 | * Authors: | |
10 | * | |
11 | * Jay Schulist <jschlst@samba.org> | |
12 | * Alexei Starovoitov <ast@plumgrid.com> | |
13 | * Daniel Borkmann <dborkman@redhat.com> | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License | |
17 | * as published by the Free Software Foundation; either version | |
18 | * 2 of the License, or (at your option) any later version. | |
19 | * | |
20 | * Andi Kleen - Fix a few bad bugs and races. | |
21 | * Kris Katterjohn - Added many additional checks in bpf_check_classic() | |
22 | */ | |
23 | ||
24 | #include <linux/module.h> | |
25 | #include <linux/types.h> | |
26 | #include <linux/mm.h> | |
27 | #include <linux/fcntl.h> | |
28 | #include <linux/socket.h> | |
29 | #include <linux/in.h> | |
30 | #include <linux/inet.h> | |
31 | #include <linux/netdevice.h> | |
32 | #include <linux/if_packet.h> | |
33 | #include <linux/gfp.h> | |
34 | #include <net/ip.h> | |
35 | #include <net/protocol.h> | |
36 | #include <net/netlink.h> | |
37 | #include <linux/skbuff.h> | |
38 | #include <net/sock.h> | |
39 | #include <net/flow_dissector.h> | |
40 | #include <linux/errno.h> | |
41 | #include <linux/timer.h> | |
42 | #include <asm/uaccess.h> | |
43 | #include <asm/unaligned.h> | |
44 | #include <linux/filter.h> | |
45 | #include <linux/ratelimit.h> | |
46 | #include <linux/seccomp.h> | |
47 | #include <linux/if_vlan.h> | |
48 | #include <linux/bpf.h> | |
49 | ||
50 | /** | |
51 | * sk_filter - run a packet through a socket filter | |
52 | * @sk: sock associated with &sk_buff | |
53 | * @skb: buffer to filter | |
54 | * | |
55 | * Run the filter code and then cut skb->data to correct size returned by | |
56 | * SK_RUN_FILTER. If pkt_len is 0 we toss packet. If skb->len is smaller | |
57 | * than pkt_len we keep whole skb->data. This is the socket level | |
58 | * wrapper to SK_RUN_FILTER. It returns 0 if the packet should | |
59 | * be accepted or -EPERM if the packet should be tossed. | |
60 | * | |
61 | */ | |
62 | int sk_filter(struct sock *sk, struct sk_buff *skb) | |
63 | { | |
64 | int err; | |
65 | struct sk_filter *filter; | |
66 | ||
67 | /* | |
68 | * If the skb was allocated from pfmemalloc reserves, only | |
69 | * allow SOCK_MEMALLOC sockets to use it as this socket is | |
70 | * helping free memory | |
71 | */ | |
72 | if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) | |
73 | return -ENOMEM; | |
74 | ||
75 | err = security_sock_rcv_skb(sk, skb); | |
76 | if (err) | |
77 | return err; | |
78 | ||
79 | rcu_read_lock(); | |
80 | filter = rcu_dereference(sk->sk_filter); | |
81 | if (filter) { | |
82 | unsigned int pkt_len = SK_RUN_FILTER(filter, skb); | |
83 | ||
84 | err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM; | |
85 | } | |
86 | rcu_read_unlock(); | |
87 | ||
88 | return err; | |
89 | } | |
90 | EXPORT_SYMBOL(sk_filter); | |
91 | ||
92 | static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) | |
93 | { | |
94 | return skb_get_poff((struct sk_buff *)(unsigned long) ctx); | |
95 | } | |
96 | ||
97 | static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) | |
98 | { | |
99 | struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx; | |
100 | struct nlattr *nla; | |
101 | ||
102 | if (skb_is_nonlinear(skb)) | |
103 | return 0; | |
104 | ||
105 | if (skb->len < sizeof(struct nlattr)) | |
106 | return 0; | |
107 | ||
108 | if (a > skb->len - sizeof(struct nlattr)) | |
109 | return 0; | |
110 | ||
111 | nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x); | |
112 | if (nla) | |
113 | return (void *) nla - (void *) skb->data; | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
118 | static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) | |
119 | { | |
120 | struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx; | |
121 | struct nlattr *nla; | |
122 | ||
123 | if (skb_is_nonlinear(skb)) | |
124 | return 0; | |
125 | ||
126 | if (skb->len < sizeof(struct nlattr)) | |
127 | return 0; | |
128 | ||
129 | if (a > skb->len - sizeof(struct nlattr)) | |
130 | return 0; | |
131 | ||
132 | nla = (struct nlattr *) &skb->data[a]; | |
133 | if (nla->nla_len > skb->len - a) | |
134 | return 0; | |
135 | ||
136 | nla = nla_find_nested(nla, x); | |
137 | if (nla) | |
138 | return (void *) nla - (void *) skb->data; | |
139 | ||
140 | return 0; | |
141 | } | |
142 | ||
143 | static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) | |
144 | { | |
145 | return raw_smp_processor_id(); | |
146 | } | |
147 | ||
148 | /* note that this only generates 32-bit random numbers */ | |
149 | static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) | |
150 | { | |
151 | return prandom_u32(); | |
152 | } | |
153 | ||
154 | static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg, | |
155 | struct bpf_insn *insn_buf) | |
156 | { | |
157 | struct bpf_insn *insn = insn_buf; | |
158 | ||
159 | switch (skb_field) { | |
160 | case SKF_AD_MARK: | |
161 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); | |
162 | ||
163 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
164 | offsetof(struct sk_buff, mark)); | |
165 | break; | |
166 | ||
167 | case SKF_AD_PKTTYPE: | |
168 | *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET()); | |
169 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX); | |
170 | #ifdef __BIG_ENDIAN_BITFIELD | |
171 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5); | |
172 | #endif | |
173 | break; | |
174 | ||
175 | case SKF_AD_QUEUE: | |
176 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2); | |
177 | ||
178 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
179 | offsetof(struct sk_buff, queue_mapping)); | |
180 | break; | |
181 | ||
182 | case SKF_AD_VLAN_TAG: | |
183 | case SKF_AD_VLAN_TAG_PRESENT: | |
184 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); | |
185 | BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000); | |
186 | ||
187 | /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */ | |
188 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
189 | offsetof(struct sk_buff, vlan_tci)); | |
190 | if (skb_field == SKF_AD_VLAN_TAG) { | |
191 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, | |
192 | ~VLAN_TAG_PRESENT); | |
193 | } else { | |
194 | /* dst_reg >>= 12 */ | |
195 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12); | |
196 | /* dst_reg &= 1 */ | |
197 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1); | |
198 | } | |
199 | break; | |
200 | } | |
201 | ||
202 | return insn - insn_buf; | |
203 | } | |
204 | ||
205 | static bool convert_bpf_extensions(struct sock_filter *fp, | |
206 | struct bpf_insn **insnp) | |
207 | { | |
208 | struct bpf_insn *insn = *insnp; | |
209 | u32 cnt; | |
210 | ||
211 | switch (fp->k) { | |
212 | case SKF_AD_OFF + SKF_AD_PROTOCOL: | |
213 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); | |
214 | ||
215 | /* A = *(u16 *) (CTX + offsetof(protocol)) */ | |
216 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, | |
217 | offsetof(struct sk_buff, protocol)); | |
218 | /* A = ntohs(A) [emitting a nop or swap16] */ | |
219 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); | |
220 | break; | |
221 | ||
222 | case SKF_AD_OFF + SKF_AD_PKTTYPE: | |
223 | cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn); | |
224 | insn += cnt - 1; | |
225 | break; | |
226 | ||
227 | case SKF_AD_OFF + SKF_AD_IFINDEX: | |
228 | case SKF_AD_OFF + SKF_AD_HATYPE: | |
229 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); | |
230 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2); | |
231 | BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0); | |
232 | ||
233 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)), | |
234 | BPF_REG_TMP, BPF_REG_CTX, | |
235 | offsetof(struct sk_buff, dev)); | |
236 | /* if (tmp != 0) goto pc + 1 */ | |
237 | *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1); | |
238 | *insn++ = BPF_EXIT_INSN(); | |
239 | if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX) | |
240 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP, | |
241 | offsetof(struct net_device, ifindex)); | |
242 | else | |
243 | *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP, | |
244 | offsetof(struct net_device, type)); | |
245 | break; | |
246 | ||
247 | case SKF_AD_OFF + SKF_AD_MARK: | |
248 | cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn); | |
249 | insn += cnt - 1; | |
250 | break; | |
251 | ||
252 | case SKF_AD_OFF + SKF_AD_RXHASH: | |
253 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); | |
254 | ||
255 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, | |
256 | offsetof(struct sk_buff, hash)); | |
257 | break; | |
258 | ||
259 | case SKF_AD_OFF + SKF_AD_QUEUE: | |
260 | cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn); | |
261 | insn += cnt - 1; | |
262 | break; | |
263 | ||
264 | case SKF_AD_OFF + SKF_AD_VLAN_TAG: | |
265 | cnt = convert_skb_access(SKF_AD_VLAN_TAG, | |
266 | BPF_REG_A, BPF_REG_CTX, insn); | |
267 | insn += cnt - 1; | |
268 | break; | |
269 | ||
270 | case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: | |
271 | cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, | |
272 | BPF_REG_A, BPF_REG_CTX, insn); | |
273 | insn += cnt - 1; | |
274 | break; | |
275 | ||
276 | case SKF_AD_OFF + SKF_AD_VLAN_TPID: | |
277 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); | |
278 | ||
279 | /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */ | |
280 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, | |
281 | offsetof(struct sk_buff, vlan_proto)); | |
282 | /* A = ntohs(A) [emitting a nop or swap16] */ | |
283 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); | |
284 | break; | |
285 | ||
286 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: | |
287 | case SKF_AD_OFF + SKF_AD_NLATTR: | |
288 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: | |
289 | case SKF_AD_OFF + SKF_AD_CPU: | |
290 | case SKF_AD_OFF + SKF_AD_RANDOM: | |
291 | /* arg1 = CTX */ | |
292 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); | |
293 | /* arg2 = A */ | |
294 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A); | |
295 | /* arg3 = X */ | |
296 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X); | |
297 | /* Emit call(arg1=CTX, arg2=A, arg3=X) */ | |
298 | switch (fp->k) { | |
299 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: | |
300 | *insn = BPF_EMIT_CALL(__skb_get_pay_offset); | |
301 | break; | |
302 | case SKF_AD_OFF + SKF_AD_NLATTR: | |
303 | *insn = BPF_EMIT_CALL(__skb_get_nlattr); | |
304 | break; | |
305 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: | |
306 | *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest); | |
307 | break; | |
308 | case SKF_AD_OFF + SKF_AD_CPU: | |
309 | *insn = BPF_EMIT_CALL(__get_raw_cpu_id); | |
310 | break; | |
311 | case SKF_AD_OFF + SKF_AD_RANDOM: | |
312 | *insn = BPF_EMIT_CALL(__get_random_u32); | |
313 | break; | |
314 | } | |
315 | break; | |
316 | ||
317 | case SKF_AD_OFF + SKF_AD_ALU_XOR_X: | |
318 | /* A ^= X */ | |
319 | *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X); | |
320 | break; | |
321 | ||
322 | default: | |
323 | /* This is just a dummy call to avoid letting the compiler | |
324 | * evict __bpf_call_base() as an optimization. Placed here | |
325 | * where no-one bothers. | |
326 | */ | |
327 | BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0); | |
328 | return false; | |
329 | } | |
330 | ||
331 | *insnp = insn; | |
332 | return true; | |
333 | } | |
334 | ||
335 | /** | |
336 | * bpf_convert_filter - convert filter program | |
337 | * @prog: the user passed filter program | |
338 | * @len: the length of the user passed filter program | |
339 | * @new_prog: buffer where converted program will be stored | |
340 | * @new_len: pointer to store length of converted program | |
341 | * | |
342 | * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style. | |
343 | * Conversion workflow: | |
344 | * | |
345 | * 1) First pass for calculating the new program length: | |
346 | * bpf_convert_filter(old_prog, old_len, NULL, &new_len) | |
347 | * | |
348 | * 2) 2nd pass to remap in two passes: 1st pass finds new | |
349 | * jump offsets, 2nd pass remapping: | |
350 | * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len); | |
351 | * bpf_convert_filter(old_prog, old_len, new_prog, &new_len); | |
352 | * | |
353 | * User BPF's register A is mapped to our BPF register 6, user BPF | |
354 | * register X is mapped to BPF register 7; frame pointer is always | |
355 | * register 10; Context 'void *ctx' is stored in register 1, that is, | |
356 | * for socket filters: ctx == 'struct sk_buff *', for seccomp: | |
357 | * ctx == 'struct seccomp_data *'. | |
358 | */ | |
359 | static int bpf_convert_filter(struct sock_filter *prog, int len, | |
360 | struct bpf_insn *new_prog, int *new_len) | |
361 | { | |
362 | int new_flen = 0, pass = 0, target, i; | |
363 | struct bpf_insn *new_insn; | |
364 | struct sock_filter *fp; | |
365 | int *addrs = NULL; | |
366 | u8 bpf_src; | |
367 | ||
368 | BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK); | |
369 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
370 | ||
371 | if (len <= 0 || len > BPF_MAXINSNS) | |
372 | return -EINVAL; | |
373 | ||
374 | if (new_prog) { | |
375 | addrs = kcalloc(len, sizeof(*addrs), | |
376 | GFP_KERNEL | __GFP_NOWARN); | |
377 | if (!addrs) | |
378 | return -ENOMEM; | |
379 | } | |
380 | ||
381 | do_pass: | |
382 | new_insn = new_prog; | |
383 | fp = prog; | |
384 | ||
385 | if (new_insn) | |
386 | *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1); | |
387 | new_insn++; | |
388 | ||
389 | for (i = 0; i < len; fp++, i++) { | |
390 | struct bpf_insn tmp_insns[6] = { }; | |
391 | struct bpf_insn *insn = tmp_insns; | |
392 | ||
393 | if (addrs) | |
394 | addrs[i] = new_insn - new_prog; | |
395 | ||
396 | switch (fp->code) { | |
397 | /* All arithmetic insns and skb loads map as-is. */ | |
398 | case BPF_ALU | BPF_ADD | BPF_X: | |
399 | case BPF_ALU | BPF_ADD | BPF_K: | |
400 | case BPF_ALU | BPF_SUB | BPF_X: | |
401 | case BPF_ALU | BPF_SUB | BPF_K: | |
402 | case BPF_ALU | BPF_AND | BPF_X: | |
403 | case BPF_ALU | BPF_AND | BPF_K: | |
404 | case BPF_ALU | BPF_OR | BPF_X: | |
405 | case BPF_ALU | BPF_OR | BPF_K: | |
406 | case BPF_ALU | BPF_LSH | BPF_X: | |
407 | case BPF_ALU | BPF_LSH | BPF_K: | |
408 | case BPF_ALU | BPF_RSH | BPF_X: | |
409 | case BPF_ALU | BPF_RSH | BPF_K: | |
410 | case BPF_ALU | BPF_XOR | BPF_X: | |
411 | case BPF_ALU | BPF_XOR | BPF_K: | |
412 | case BPF_ALU | BPF_MUL | BPF_X: | |
413 | case BPF_ALU | BPF_MUL | BPF_K: | |
414 | case BPF_ALU | BPF_DIV | BPF_X: | |
415 | case BPF_ALU | BPF_DIV | BPF_K: | |
416 | case BPF_ALU | BPF_MOD | BPF_X: | |
417 | case BPF_ALU | BPF_MOD | BPF_K: | |
418 | case BPF_ALU | BPF_NEG: | |
419 | case BPF_LD | BPF_ABS | BPF_W: | |
420 | case BPF_LD | BPF_ABS | BPF_H: | |
421 | case BPF_LD | BPF_ABS | BPF_B: | |
422 | case BPF_LD | BPF_IND | BPF_W: | |
423 | case BPF_LD | BPF_IND | BPF_H: | |
424 | case BPF_LD | BPF_IND | BPF_B: | |
425 | /* Check for overloaded BPF extension and | |
426 | * directly convert it if found, otherwise | |
427 | * just move on with mapping. | |
428 | */ | |
429 | if (BPF_CLASS(fp->code) == BPF_LD && | |
430 | BPF_MODE(fp->code) == BPF_ABS && | |
431 | convert_bpf_extensions(fp, &insn)) | |
432 | break; | |
433 | ||
434 | *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k); | |
435 | break; | |
436 | ||
437 | /* Jump transformation cannot use BPF block macros | |
438 | * everywhere as offset calculation and target updates | |
439 | * require a bit more work than the rest, i.e. jump | |
440 | * opcodes map as-is, but offsets need adjustment. | |
441 | */ | |
442 | ||
443 | #define BPF_EMIT_JMP \ | |
444 | do { \ | |
445 | if (target >= len || target < 0) \ | |
446 | goto err; \ | |
447 | insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ | |
448 | /* Adjust pc relative offset for 2nd or 3rd insn. */ \ | |
449 | insn->off -= insn - tmp_insns; \ | |
450 | } while (0) | |
451 | ||
452 | case BPF_JMP | BPF_JA: | |
453 | target = i + fp->k + 1; | |
454 | insn->code = fp->code; | |
455 | BPF_EMIT_JMP; | |
456 | break; | |
457 | ||
458 | case BPF_JMP | BPF_JEQ | BPF_K: | |
459 | case BPF_JMP | BPF_JEQ | BPF_X: | |
460 | case BPF_JMP | BPF_JSET | BPF_K: | |
461 | case BPF_JMP | BPF_JSET | BPF_X: | |
462 | case BPF_JMP | BPF_JGT | BPF_K: | |
463 | case BPF_JMP | BPF_JGT | BPF_X: | |
464 | case BPF_JMP | BPF_JGE | BPF_K: | |
465 | case BPF_JMP | BPF_JGE | BPF_X: | |
466 | if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) { | |
467 | /* BPF immediates are signed, zero extend | |
468 | * immediate into tmp register and use it | |
469 | * in compare insn. | |
470 | */ | |
471 | *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k); | |
472 | ||
473 | insn->dst_reg = BPF_REG_A; | |
474 | insn->src_reg = BPF_REG_TMP; | |
475 | bpf_src = BPF_X; | |
476 | } else { | |
477 | insn->dst_reg = BPF_REG_A; | |
478 | insn->src_reg = BPF_REG_X; | |
479 | insn->imm = fp->k; | |
480 | bpf_src = BPF_SRC(fp->code); | |
481 | } | |
482 | ||
483 | /* Common case where 'jump_false' is next insn. */ | |
484 | if (fp->jf == 0) { | |
485 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; | |
486 | target = i + fp->jt + 1; | |
487 | BPF_EMIT_JMP; | |
488 | break; | |
489 | } | |
490 | ||
491 | /* Convert JEQ into JNE when 'jump_true' is next insn. */ | |
492 | if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) { | |
493 | insn->code = BPF_JMP | BPF_JNE | bpf_src; | |
494 | target = i + fp->jf + 1; | |
495 | BPF_EMIT_JMP; | |
496 | break; | |
497 | } | |
498 | ||
499 | /* Other jumps are mapped into two insns: Jxx and JA. */ | |
500 | target = i + fp->jt + 1; | |
501 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; | |
502 | BPF_EMIT_JMP; | |
503 | insn++; | |
504 | ||
505 | insn->code = BPF_JMP | BPF_JA; | |
506 | target = i + fp->jf + 1; | |
507 | BPF_EMIT_JMP; | |
508 | break; | |
509 | ||
510 | /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ | |
511 | case BPF_LDX | BPF_MSH | BPF_B: | |
512 | /* tmp = A */ | |
513 | *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A); | |
514 | /* A = BPF_R0 = *(u8 *) (skb->data + K) */ | |
515 | *insn++ = BPF_LD_ABS(BPF_B, fp->k); | |
516 | /* A &= 0xf */ | |
517 | *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf); | |
518 | /* A <<= 2 */ | |
519 | *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2); | |
520 | /* X = A */ | |
521 | *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); | |
522 | /* A = tmp */ | |
523 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP); | |
524 | break; | |
525 | ||
526 | /* RET_K, RET_A are remaped into 2 insns. */ | |
527 | case BPF_RET | BPF_A: | |
528 | case BPF_RET | BPF_K: | |
529 | *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ? | |
530 | BPF_K : BPF_X, BPF_REG_0, | |
531 | BPF_REG_A, fp->k); | |
532 | *insn = BPF_EXIT_INSN(); | |
533 | break; | |
534 | ||
535 | /* Store to stack. */ | |
536 | case BPF_ST: | |
537 | case BPF_STX: | |
538 | *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == | |
539 | BPF_ST ? BPF_REG_A : BPF_REG_X, | |
540 | -(BPF_MEMWORDS - fp->k) * 4); | |
541 | break; | |
542 | ||
543 | /* Load from stack. */ | |
544 | case BPF_LD | BPF_MEM: | |
545 | case BPF_LDX | BPF_MEM: | |
546 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? | |
547 | BPF_REG_A : BPF_REG_X, BPF_REG_FP, | |
548 | -(BPF_MEMWORDS - fp->k) * 4); | |
549 | break; | |
550 | ||
551 | /* A = K or X = K */ | |
552 | case BPF_LD | BPF_IMM: | |
553 | case BPF_LDX | BPF_IMM: | |
554 | *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? | |
555 | BPF_REG_A : BPF_REG_X, fp->k); | |
556 | break; | |
557 | ||
558 | /* X = A */ | |
559 | case BPF_MISC | BPF_TAX: | |
560 | *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); | |
561 | break; | |
562 | ||
563 | /* A = X */ | |
564 | case BPF_MISC | BPF_TXA: | |
565 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); | |
566 | break; | |
567 | ||
568 | /* A = skb->len or X = skb->len */ | |
569 | case BPF_LD | BPF_W | BPF_LEN: | |
570 | case BPF_LDX | BPF_W | BPF_LEN: | |
571 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? | |
572 | BPF_REG_A : BPF_REG_X, BPF_REG_CTX, | |
573 | offsetof(struct sk_buff, len)); | |
574 | break; | |
575 | ||
576 | /* Access seccomp_data fields. */ | |
577 | case BPF_LDX | BPF_ABS | BPF_W: | |
578 | /* A = *(u32 *) (ctx + K) */ | |
579 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k); | |
580 | break; | |
581 | ||
582 | /* Unknown instruction. */ | |
583 | default: | |
584 | goto err; | |
585 | } | |
586 | ||
587 | insn++; | |
588 | if (new_prog) | |
589 | memcpy(new_insn, tmp_insns, | |
590 | sizeof(*insn) * (insn - tmp_insns)); | |
591 | new_insn += insn - tmp_insns; | |
592 | } | |
593 | ||
594 | if (!new_prog) { | |
595 | /* Only calculating new length. */ | |
596 | *new_len = new_insn - new_prog; | |
597 | return 0; | |
598 | } | |
599 | ||
600 | pass++; | |
601 | if (new_flen != new_insn - new_prog) { | |
602 | new_flen = new_insn - new_prog; | |
603 | if (pass > 2) | |
604 | goto err; | |
605 | goto do_pass; | |
606 | } | |
607 | ||
608 | kfree(addrs); | |
609 | BUG_ON(*new_len != new_flen); | |
610 | return 0; | |
611 | err: | |
612 | kfree(addrs); | |
613 | return -EINVAL; | |
614 | } | |
615 | ||
616 | /* Security: | |
617 | * | |
618 | * As we dont want to clear mem[] array for each packet going through | |
619 | * __bpf_prog_run(), we check that filter loaded by user never try to read | |
620 | * a cell if not previously written, and we check all branches to be sure | |
621 | * a malicious user doesn't try to abuse us. | |
622 | */ | |
623 | static int check_load_and_stores(const struct sock_filter *filter, int flen) | |
624 | { | |
625 | u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ | |
626 | int pc, ret = 0; | |
627 | ||
628 | BUILD_BUG_ON(BPF_MEMWORDS > 16); | |
629 | ||
630 | masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); | |
631 | if (!masks) | |
632 | return -ENOMEM; | |
633 | ||
634 | memset(masks, 0xff, flen * sizeof(*masks)); | |
635 | ||
636 | for (pc = 0; pc < flen; pc++) { | |
637 | memvalid &= masks[pc]; | |
638 | ||
639 | switch (filter[pc].code) { | |
640 | case BPF_ST: | |
641 | case BPF_STX: | |
642 | memvalid |= (1 << filter[pc].k); | |
643 | break; | |
644 | case BPF_LD | BPF_MEM: | |
645 | case BPF_LDX | BPF_MEM: | |
646 | if (!(memvalid & (1 << filter[pc].k))) { | |
647 | ret = -EINVAL; | |
648 | goto error; | |
649 | } | |
650 | break; | |
651 | case BPF_JMP | BPF_JA: | |
652 | /* A jump must set masks on target */ | |
653 | masks[pc + 1 + filter[pc].k] &= memvalid; | |
654 | memvalid = ~0; | |
655 | break; | |
656 | case BPF_JMP | BPF_JEQ | BPF_K: | |
657 | case BPF_JMP | BPF_JEQ | BPF_X: | |
658 | case BPF_JMP | BPF_JGE | BPF_K: | |
659 | case BPF_JMP | BPF_JGE | BPF_X: | |
660 | case BPF_JMP | BPF_JGT | BPF_K: | |
661 | case BPF_JMP | BPF_JGT | BPF_X: | |
662 | case BPF_JMP | BPF_JSET | BPF_K: | |
663 | case BPF_JMP | BPF_JSET | BPF_X: | |
664 | /* A jump must set masks on targets */ | |
665 | masks[pc + 1 + filter[pc].jt] &= memvalid; | |
666 | masks[pc + 1 + filter[pc].jf] &= memvalid; | |
667 | memvalid = ~0; | |
668 | break; | |
669 | } | |
670 | } | |
671 | error: | |
672 | kfree(masks); | |
673 | return ret; | |
674 | } | |
675 | ||
676 | static bool chk_code_allowed(u16 code_to_probe) | |
677 | { | |
678 | static const bool codes[] = { | |
679 | /* 32 bit ALU operations */ | |
680 | [BPF_ALU | BPF_ADD | BPF_K] = true, | |
681 | [BPF_ALU | BPF_ADD | BPF_X] = true, | |
682 | [BPF_ALU | BPF_SUB | BPF_K] = true, | |
683 | [BPF_ALU | BPF_SUB | BPF_X] = true, | |
684 | [BPF_ALU | BPF_MUL | BPF_K] = true, | |
685 | [BPF_ALU | BPF_MUL | BPF_X] = true, | |
686 | [BPF_ALU | BPF_DIV | BPF_K] = true, | |
687 | [BPF_ALU | BPF_DIV | BPF_X] = true, | |
688 | [BPF_ALU | BPF_MOD | BPF_K] = true, | |
689 | [BPF_ALU | BPF_MOD | BPF_X] = true, | |
690 | [BPF_ALU | BPF_AND | BPF_K] = true, | |
691 | [BPF_ALU | BPF_AND | BPF_X] = true, | |
692 | [BPF_ALU | BPF_OR | BPF_K] = true, | |
693 | [BPF_ALU | BPF_OR | BPF_X] = true, | |
694 | [BPF_ALU | BPF_XOR | BPF_K] = true, | |
695 | [BPF_ALU | BPF_XOR | BPF_X] = true, | |
696 | [BPF_ALU | BPF_LSH | BPF_K] = true, | |
697 | [BPF_ALU | BPF_LSH | BPF_X] = true, | |
698 | [BPF_ALU | BPF_RSH | BPF_K] = true, | |
699 | [BPF_ALU | BPF_RSH | BPF_X] = true, | |
700 | [BPF_ALU | BPF_NEG] = true, | |
701 | /* Load instructions */ | |
702 | [BPF_LD | BPF_W | BPF_ABS] = true, | |
703 | [BPF_LD | BPF_H | BPF_ABS] = true, | |
704 | [BPF_LD | BPF_B | BPF_ABS] = true, | |
705 | [BPF_LD | BPF_W | BPF_LEN] = true, | |
706 | [BPF_LD | BPF_W | BPF_IND] = true, | |
707 | [BPF_LD | BPF_H | BPF_IND] = true, | |
708 | [BPF_LD | BPF_B | BPF_IND] = true, | |
709 | [BPF_LD | BPF_IMM] = true, | |
710 | [BPF_LD | BPF_MEM] = true, | |
711 | [BPF_LDX | BPF_W | BPF_LEN] = true, | |
712 | [BPF_LDX | BPF_B | BPF_MSH] = true, | |
713 | [BPF_LDX | BPF_IMM] = true, | |
714 | [BPF_LDX | BPF_MEM] = true, | |
715 | /* Store instructions */ | |
716 | [BPF_ST] = true, | |
717 | [BPF_STX] = true, | |
718 | /* Misc instructions */ | |
719 | [BPF_MISC | BPF_TAX] = true, | |
720 | [BPF_MISC | BPF_TXA] = true, | |
721 | /* Return instructions */ | |
722 | [BPF_RET | BPF_K] = true, | |
723 | [BPF_RET | BPF_A] = true, | |
724 | /* Jump instructions */ | |
725 | [BPF_JMP | BPF_JA] = true, | |
726 | [BPF_JMP | BPF_JEQ | BPF_K] = true, | |
727 | [BPF_JMP | BPF_JEQ | BPF_X] = true, | |
728 | [BPF_JMP | BPF_JGE | BPF_K] = true, | |
729 | [BPF_JMP | BPF_JGE | BPF_X] = true, | |
730 | [BPF_JMP | BPF_JGT | BPF_K] = true, | |
731 | [BPF_JMP | BPF_JGT | BPF_X] = true, | |
732 | [BPF_JMP | BPF_JSET | BPF_K] = true, | |
733 | [BPF_JMP | BPF_JSET | BPF_X] = true, | |
734 | }; | |
735 | ||
736 | if (code_to_probe >= ARRAY_SIZE(codes)) | |
737 | return false; | |
738 | ||
739 | return codes[code_to_probe]; | |
740 | } | |
741 | ||
742 | /** | |
743 | * bpf_check_classic - verify socket filter code | |
744 | * @filter: filter to verify | |
745 | * @flen: length of filter | |
746 | * | |
747 | * Check the user's filter code. If we let some ugly | |
748 | * filter code slip through kaboom! The filter must contain | |
749 | * no references or jumps that are out of range, no illegal | |
750 | * instructions, and must end with a RET instruction. | |
751 | * | |
752 | * All jumps are forward as they are not signed. | |
753 | * | |
754 | * Returns 0 if the rule set is legal or -EINVAL if not. | |
755 | */ | |
756 | static int bpf_check_classic(const struct sock_filter *filter, | |
757 | unsigned int flen) | |
758 | { | |
759 | bool anc_found; | |
760 | int pc; | |
761 | ||
762 | if (flen == 0 || flen > BPF_MAXINSNS) | |
763 | return -EINVAL; | |
764 | ||
765 | /* Check the filter code now */ | |
766 | for (pc = 0; pc < flen; pc++) { | |
767 | const struct sock_filter *ftest = &filter[pc]; | |
768 | ||
769 | /* May we actually operate on this code? */ | |
770 | if (!chk_code_allowed(ftest->code)) | |
771 | return -EINVAL; | |
772 | ||
773 | /* Some instructions need special checks */ | |
774 | switch (ftest->code) { | |
775 | case BPF_ALU | BPF_DIV | BPF_K: | |
776 | case BPF_ALU | BPF_MOD | BPF_K: | |
777 | /* Check for division by zero */ | |
778 | if (ftest->k == 0) | |
779 | return -EINVAL; | |
780 | break; | |
781 | case BPF_LD | BPF_MEM: | |
782 | case BPF_LDX | BPF_MEM: | |
783 | case BPF_ST: | |
784 | case BPF_STX: | |
785 | /* Check for invalid memory addresses */ | |
786 | if (ftest->k >= BPF_MEMWORDS) | |
787 | return -EINVAL; | |
788 | break; | |
789 | case BPF_JMP | BPF_JA: | |
790 | /* Note, the large ftest->k might cause loops. | |
791 | * Compare this with conditional jumps below, | |
792 | * where offsets are limited. --ANK (981016) | |
793 | */ | |
794 | if (ftest->k >= (unsigned int)(flen - pc - 1)) | |
795 | return -EINVAL; | |
796 | break; | |
797 | case BPF_JMP | BPF_JEQ | BPF_K: | |
798 | case BPF_JMP | BPF_JEQ | BPF_X: | |
799 | case BPF_JMP | BPF_JGE | BPF_K: | |
800 | case BPF_JMP | BPF_JGE | BPF_X: | |
801 | case BPF_JMP | BPF_JGT | BPF_K: | |
802 | case BPF_JMP | BPF_JGT | BPF_X: | |
803 | case BPF_JMP | BPF_JSET | BPF_K: | |
804 | case BPF_JMP | BPF_JSET | BPF_X: | |
805 | /* Both conditionals must be safe */ | |
806 | if (pc + ftest->jt + 1 >= flen || | |
807 | pc + ftest->jf + 1 >= flen) | |
808 | return -EINVAL; | |
809 | break; | |
810 | case BPF_LD | BPF_W | BPF_ABS: | |
811 | case BPF_LD | BPF_H | BPF_ABS: | |
812 | case BPF_LD | BPF_B | BPF_ABS: | |
813 | anc_found = false; | |
814 | if (bpf_anc_helper(ftest) & BPF_ANC) | |
815 | anc_found = true; | |
816 | /* Ancillary operation unknown or unsupported */ | |
817 | if (anc_found == false && ftest->k >= SKF_AD_OFF) | |
818 | return -EINVAL; | |
819 | } | |
820 | } | |
821 | ||
822 | /* Last instruction must be a RET code */ | |
823 | switch (filter[flen - 1].code) { | |
824 | case BPF_RET | BPF_K: | |
825 | case BPF_RET | BPF_A: | |
826 | return check_load_and_stores(filter, flen); | |
827 | } | |
828 | ||
829 | return -EINVAL; | |
830 | } | |
831 | ||
832 | static int bpf_prog_store_orig_filter(struct bpf_prog *fp, | |
833 | const struct sock_fprog *fprog) | |
834 | { | |
835 | unsigned int fsize = bpf_classic_proglen(fprog); | |
836 | struct sock_fprog_kern *fkprog; | |
837 | ||
838 | fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); | |
839 | if (!fp->orig_prog) | |
840 | return -ENOMEM; | |
841 | ||
842 | fkprog = fp->orig_prog; | |
843 | fkprog->len = fprog->len; | |
844 | ||
845 | fkprog->filter = kmemdup(fp->insns, fsize, | |
846 | GFP_KERNEL | __GFP_NOWARN); | |
847 | if (!fkprog->filter) { | |
848 | kfree(fp->orig_prog); | |
849 | return -ENOMEM; | |
850 | } | |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
855 | static void bpf_release_orig_filter(struct bpf_prog *fp) | |
856 | { | |
857 | struct sock_fprog_kern *fprog = fp->orig_prog; | |
858 | ||
859 | if (fprog) { | |
860 | kfree(fprog->filter); | |
861 | kfree(fprog); | |
862 | } | |
863 | } | |
864 | ||
865 | static void __bpf_prog_release(struct bpf_prog *prog) | |
866 | { | |
867 | if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) { | |
868 | bpf_prog_put(prog); | |
869 | } else { | |
870 | bpf_release_orig_filter(prog); | |
871 | bpf_prog_free(prog); | |
872 | } | |
873 | } | |
874 | ||
875 | static void __sk_filter_release(struct sk_filter *fp) | |
876 | { | |
877 | __bpf_prog_release(fp->prog); | |
878 | kfree(fp); | |
879 | } | |
880 | ||
881 | /** | |
882 | * sk_filter_release_rcu - Release a socket filter by rcu_head | |
883 | * @rcu: rcu_head that contains the sk_filter to free | |
884 | */ | |
885 | static void sk_filter_release_rcu(struct rcu_head *rcu) | |
886 | { | |
887 | struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); | |
888 | ||
889 | __sk_filter_release(fp); | |
890 | } | |
891 | ||
892 | /** | |
893 | * sk_filter_release - release a socket filter | |
894 | * @fp: filter to remove | |
895 | * | |
896 | * Remove a filter from a socket and release its resources. | |
897 | */ | |
898 | static void sk_filter_release(struct sk_filter *fp) | |
899 | { | |
900 | if (atomic_dec_and_test(&fp->refcnt)) | |
901 | call_rcu(&fp->rcu, sk_filter_release_rcu); | |
902 | } | |
903 | ||
904 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) | |
905 | { | |
906 | u32 filter_size = bpf_prog_size(fp->prog->len); | |
907 | ||
908 | atomic_sub(filter_size, &sk->sk_omem_alloc); | |
909 | sk_filter_release(fp); | |
910 | } | |
911 | ||
912 | /* try to charge the socket memory if there is space available | |
913 | * return true on success | |
914 | */ | |
915 | bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) | |
916 | { | |
917 | u32 filter_size = bpf_prog_size(fp->prog->len); | |
918 | ||
919 | /* same check as in sock_kmalloc() */ | |
920 | if (filter_size <= sysctl_optmem_max && | |
921 | atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) { | |
922 | atomic_inc(&fp->refcnt); | |
923 | atomic_add(filter_size, &sk->sk_omem_alloc); | |
924 | return true; | |
925 | } | |
926 | return false; | |
927 | } | |
928 | ||
929 | static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) | |
930 | { | |
931 | struct sock_filter *old_prog; | |
932 | struct bpf_prog *old_fp; | |
933 | int err, new_len, old_len = fp->len; | |
934 | ||
935 | /* We are free to overwrite insns et al right here as it | |
936 | * won't be used at this point in time anymore internally | |
937 | * after the migration to the internal BPF instruction | |
938 | * representation. | |
939 | */ | |
940 | BUILD_BUG_ON(sizeof(struct sock_filter) != | |
941 | sizeof(struct bpf_insn)); | |
942 | ||
943 | /* Conversion cannot happen on overlapping memory areas, | |
944 | * so we need to keep the user BPF around until the 2nd | |
945 | * pass. At this time, the user BPF is stored in fp->insns. | |
946 | */ | |
947 | old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), | |
948 | GFP_KERNEL | __GFP_NOWARN); | |
949 | if (!old_prog) { | |
950 | err = -ENOMEM; | |
951 | goto out_err; | |
952 | } | |
953 | ||
954 | /* 1st pass: calculate the new program length. */ | |
955 | err = bpf_convert_filter(old_prog, old_len, NULL, &new_len); | |
956 | if (err) | |
957 | goto out_err_free; | |
958 | ||
959 | /* Expand fp for appending the new filter representation. */ | |
960 | old_fp = fp; | |
961 | fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0); | |
962 | if (!fp) { | |
963 | /* The old_fp is still around in case we couldn't | |
964 | * allocate new memory, so uncharge on that one. | |
965 | */ | |
966 | fp = old_fp; | |
967 | err = -ENOMEM; | |
968 | goto out_err_free; | |
969 | } | |
970 | ||
971 | fp->len = new_len; | |
972 | ||
973 | /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ | |
974 | err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len); | |
975 | if (err) | |
976 | /* 2nd bpf_convert_filter() can fail only if it fails | |
977 | * to allocate memory, remapping must succeed. Note, | |
978 | * that at this time old_fp has already been released | |
979 | * by krealloc(). | |
980 | */ | |
981 | goto out_err_free; | |
982 | ||
983 | bpf_prog_select_runtime(fp); | |
984 | ||
985 | kfree(old_prog); | |
986 | return fp; | |
987 | ||
988 | out_err_free: | |
989 | kfree(old_prog); | |
990 | out_err: | |
991 | __bpf_prog_release(fp); | |
992 | return ERR_PTR(err); | |
993 | } | |
994 | ||
995 | static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp, | |
996 | bpf_aux_classic_check_t trans) | |
997 | { | |
998 | int err; | |
999 | ||
1000 | fp->bpf_func = NULL; | |
1001 | fp->jited = false; | |
1002 | ||
1003 | err = bpf_check_classic(fp->insns, fp->len); | |
1004 | if (err) { | |
1005 | __bpf_prog_release(fp); | |
1006 | return ERR_PTR(err); | |
1007 | } | |
1008 | ||
1009 | /* There might be additional checks and transformations | |
1010 | * needed on classic filters, f.e. in case of seccomp. | |
1011 | */ | |
1012 | if (trans) { | |
1013 | err = trans(fp->insns, fp->len); | |
1014 | if (err) { | |
1015 | __bpf_prog_release(fp); | |
1016 | return ERR_PTR(err); | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | /* Probe if we can JIT compile the filter and if so, do | |
1021 | * the compilation of the filter. | |
1022 | */ | |
1023 | bpf_jit_compile(fp); | |
1024 | ||
1025 | /* JIT compiler couldn't process this filter, so do the | |
1026 | * internal BPF translation for the optimized interpreter. | |
1027 | */ | |
1028 | if (!fp->jited) | |
1029 | fp = bpf_migrate_filter(fp); | |
1030 | ||
1031 | return fp; | |
1032 | } | |
1033 | ||
1034 | /** | |
1035 | * bpf_prog_create - create an unattached filter | |
1036 | * @pfp: the unattached filter that is created | |
1037 | * @fprog: the filter program | |
1038 | * | |
1039 | * Create a filter independent of any socket. We first run some | |
1040 | * sanity checks on it to make sure it does not explode on us later. | |
1041 | * If an error occurs or there is insufficient memory for the filter | |
1042 | * a negative errno code is returned. On success the return is zero. | |
1043 | */ | |
1044 | int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog) | |
1045 | { | |
1046 | unsigned int fsize = bpf_classic_proglen(fprog); | |
1047 | struct bpf_prog *fp; | |
1048 | ||
1049 | /* Make sure new filter is there and in the right amounts. */ | |
1050 | if (fprog->filter == NULL) | |
1051 | return -EINVAL; | |
1052 | ||
1053 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); | |
1054 | if (!fp) | |
1055 | return -ENOMEM; | |
1056 | ||
1057 | memcpy(fp->insns, fprog->filter, fsize); | |
1058 | ||
1059 | fp->len = fprog->len; | |
1060 | /* Since unattached filters are not copied back to user | |
1061 | * space through sk_get_filter(), we do not need to hold | |
1062 | * a copy here, and can spare us the work. | |
1063 | */ | |
1064 | fp->orig_prog = NULL; | |
1065 | ||
1066 | /* bpf_prepare_filter() already takes care of freeing | |
1067 | * memory in case something goes wrong. | |
1068 | */ | |
1069 | fp = bpf_prepare_filter(fp, NULL); | |
1070 | if (IS_ERR(fp)) | |
1071 | return PTR_ERR(fp); | |
1072 | ||
1073 | *pfp = fp; | |
1074 | return 0; | |
1075 | } | |
1076 | EXPORT_SYMBOL_GPL(bpf_prog_create); | |
1077 | ||
1078 | /** | |
1079 | * bpf_prog_create_from_user - create an unattached filter from user buffer | |
1080 | * @pfp: the unattached filter that is created | |
1081 | * @fprog: the filter program | |
1082 | * @trans: post-classic verifier transformation handler | |
1083 | * | |
1084 | * This function effectively does the same as bpf_prog_create(), only | |
1085 | * that it builds up its insns buffer from user space provided buffer. | |
1086 | * It also allows for passing a bpf_aux_classic_check_t handler. | |
1087 | */ | |
1088 | int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog, | |
1089 | bpf_aux_classic_check_t trans) | |
1090 | { | |
1091 | unsigned int fsize = bpf_classic_proglen(fprog); | |
1092 | struct bpf_prog *fp; | |
1093 | ||
1094 | /* Make sure new filter is there and in the right amounts. */ | |
1095 | if (fprog->filter == NULL) | |
1096 | return -EINVAL; | |
1097 | ||
1098 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); | |
1099 | if (!fp) | |
1100 | return -ENOMEM; | |
1101 | ||
1102 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { | |
1103 | __bpf_prog_free(fp); | |
1104 | return -EFAULT; | |
1105 | } | |
1106 | ||
1107 | fp->len = fprog->len; | |
1108 | /* Since unattached filters are not copied back to user | |
1109 | * space through sk_get_filter(), we do not need to hold | |
1110 | * a copy here, and can spare us the work. | |
1111 | */ | |
1112 | fp->orig_prog = NULL; | |
1113 | ||
1114 | /* bpf_prepare_filter() already takes care of freeing | |
1115 | * memory in case something goes wrong. | |
1116 | */ | |
1117 | fp = bpf_prepare_filter(fp, trans); | |
1118 | if (IS_ERR(fp)) | |
1119 | return PTR_ERR(fp); | |
1120 | ||
1121 | *pfp = fp; | |
1122 | return 0; | |
1123 | } | |
1124 | ||
1125 | void bpf_prog_destroy(struct bpf_prog *fp) | |
1126 | { | |
1127 | __bpf_prog_release(fp); | |
1128 | } | |
1129 | EXPORT_SYMBOL_GPL(bpf_prog_destroy); | |
1130 | ||
1131 | static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk) | |
1132 | { | |
1133 | struct sk_filter *fp, *old_fp; | |
1134 | ||
1135 | fp = kmalloc(sizeof(*fp), GFP_KERNEL); | |
1136 | if (!fp) | |
1137 | return -ENOMEM; | |
1138 | ||
1139 | fp->prog = prog; | |
1140 | atomic_set(&fp->refcnt, 0); | |
1141 | ||
1142 | if (!sk_filter_charge(sk, fp)) { | |
1143 | kfree(fp); | |
1144 | return -ENOMEM; | |
1145 | } | |
1146 | ||
1147 | old_fp = rcu_dereference_protected(sk->sk_filter, | |
1148 | sock_owned_by_user(sk)); | |
1149 | rcu_assign_pointer(sk->sk_filter, fp); | |
1150 | ||
1151 | if (old_fp) | |
1152 | sk_filter_uncharge(sk, old_fp); | |
1153 | ||
1154 | return 0; | |
1155 | } | |
1156 | ||
1157 | /** | |
1158 | * sk_attach_filter - attach a socket filter | |
1159 | * @fprog: the filter program | |
1160 | * @sk: the socket to use | |
1161 | * | |
1162 | * Attach the user's filter code. We first run some sanity checks on | |
1163 | * it to make sure it does not explode on us later. If an error | |
1164 | * occurs or there is insufficient memory for the filter a negative | |
1165 | * errno code is returned. On success the return is zero. | |
1166 | */ | |
1167 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) | |
1168 | { | |
1169 | unsigned int fsize = bpf_classic_proglen(fprog); | |
1170 | unsigned int bpf_fsize = bpf_prog_size(fprog->len); | |
1171 | struct bpf_prog *prog; | |
1172 | int err; | |
1173 | ||
1174 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) | |
1175 | return -EPERM; | |
1176 | ||
1177 | /* Make sure new filter is there and in the right amounts. */ | |
1178 | if (fprog->filter == NULL) | |
1179 | return -EINVAL; | |
1180 | ||
1181 | prog = bpf_prog_alloc(bpf_fsize, 0); | |
1182 | if (!prog) | |
1183 | return -ENOMEM; | |
1184 | ||
1185 | if (copy_from_user(prog->insns, fprog->filter, fsize)) { | |
1186 | __bpf_prog_free(prog); | |
1187 | return -EFAULT; | |
1188 | } | |
1189 | ||
1190 | prog->len = fprog->len; | |
1191 | ||
1192 | err = bpf_prog_store_orig_filter(prog, fprog); | |
1193 | if (err) { | |
1194 | __bpf_prog_free(prog); | |
1195 | return -ENOMEM; | |
1196 | } | |
1197 | ||
1198 | /* bpf_prepare_filter() already takes care of freeing | |
1199 | * memory in case something goes wrong. | |
1200 | */ | |
1201 | prog = bpf_prepare_filter(prog, NULL); | |
1202 | if (IS_ERR(prog)) | |
1203 | return PTR_ERR(prog); | |
1204 | ||
1205 | err = __sk_attach_prog(prog, sk); | |
1206 | if (err < 0) { | |
1207 | __bpf_prog_release(prog); | |
1208 | return err; | |
1209 | } | |
1210 | ||
1211 | return 0; | |
1212 | } | |
1213 | EXPORT_SYMBOL_GPL(sk_attach_filter); | |
1214 | ||
1215 | int sk_attach_bpf(u32 ufd, struct sock *sk) | |
1216 | { | |
1217 | struct bpf_prog *prog; | |
1218 | int err; | |
1219 | ||
1220 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) | |
1221 | return -EPERM; | |
1222 | ||
1223 | prog = bpf_prog_get(ufd); | |
1224 | if (IS_ERR(prog)) | |
1225 | return PTR_ERR(prog); | |
1226 | ||
1227 | if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) { | |
1228 | bpf_prog_put(prog); | |
1229 | return -EINVAL; | |
1230 | } | |
1231 | ||
1232 | err = __sk_attach_prog(prog, sk); | |
1233 | if (err < 0) { | |
1234 | bpf_prog_put(prog); | |
1235 | return err; | |
1236 | } | |
1237 | ||
1238 | return 0; | |
1239 | } | |
1240 | ||
1241 | /** | |
1242 | * bpf_skb_clone_not_writable - is the header of a clone not writable | |
1243 | * @skb: buffer to check | |
1244 | * @len: length up to which to write, can be negative | |
1245 | * | |
1246 | * Returns true if modifying the header part of the cloned buffer | |
1247 | * does require the data to be copied. I.e. this version works with | |
1248 | * negative lengths needed for eBPF case! | |
1249 | */ | |
1250 | static bool bpf_skb_clone_unwritable(const struct sk_buff *skb, int len) | |
1251 | { | |
1252 | return skb_header_cloned(skb) || | |
1253 | (int) skb_headroom(skb) + len > skb->hdr_len; | |
1254 | } | |
1255 | ||
1256 | #define BPF_RECOMPUTE_CSUM(flags) ((flags) & 1) | |
1257 | ||
1258 | static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags) | |
1259 | { | |
1260 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1261 | int offset = (int) r2; | |
1262 | void *from = (void *) (long) r3; | |
1263 | unsigned int len = (unsigned int) r4; | |
1264 | char buf[16]; | |
1265 | void *ptr; | |
1266 | ||
1267 | /* bpf verifier guarantees that: | |
1268 | * 'from' pointer points to bpf program stack | |
1269 | * 'len' bytes of it were initialized | |
1270 | * 'len' > 0 | |
1271 | * 'skb' is a valid pointer to 'struct sk_buff' | |
1272 | * | |
1273 | * so check for invalid 'offset' and too large 'len' | |
1274 | */ | |
1275 | if (unlikely((u32) offset > 0xffff || len > sizeof(buf))) | |
1276 | return -EFAULT; | |
1277 | ||
1278 | offset -= skb->data - skb_mac_header(skb); | |
1279 | if (unlikely(skb_cloned(skb) && | |
1280 | bpf_skb_clone_unwritable(skb, offset + len))) | |
1281 | return -EFAULT; | |
1282 | ||
1283 | ptr = skb_header_pointer(skb, offset, len, buf); | |
1284 | if (unlikely(!ptr)) | |
1285 | return -EFAULT; | |
1286 | ||
1287 | if (BPF_RECOMPUTE_CSUM(flags)) | |
1288 | skb_postpull_rcsum(skb, ptr, len); | |
1289 | ||
1290 | memcpy(ptr, from, len); | |
1291 | ||
1292 | if (ptr == buf) | |
1293 | /* skb_store_bits cannot return -EFAULT here */ | |
1294 | skb_store_bits(skb, offset, ptr, len); | |
1295 | ||
1296 | if (BPF_RECOMPUTE_CSUM(flags) && skb->ip_summed == CHECKSUM_COMPLETE) | |
1297 | skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0)); | |
1298 | return 0; | |
1299 | } | |
1300 | ||
1301 | const struct bpf_func_proto bpf_skb_store_bytes_proto = { | |
1302 | .func = bpf_skb_store_bytes, | |
1303 | .gpl_only = false, | |
1304 | .ret_type = RET_INTEGER, | |
1305 | .arg1_type = ARG_PTR_TO_CTX, | |
1306 | .arg2_type = ARG_ANYTHING, | |
1307 | .arg3_type = ARG_PTR_TO_STACK, | |
1308 | .arg4_type = ARG_CONST_STACK_SIZE, | |
1309 | .arg5_type = ARG_ANYTHING, | |
1310 | }; | |
1311 | ||
1312 | #define BPF_HEADER_FIELD_SIZE(flags) ((flags) & 0x0f) | |
1313 | #define BPF_IS_PSEUDO_HEADER(flags) ((flags) & 0x10) | |
1314 | ||
1315 | static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags) | |
1316 | { | |
1317 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1318 | int offset = (int) r2; | |
1319 | __sum16 sum, *ptr; | |
1320 | ||
1321 | if (unlikely((u32) offset > 0xffff)) | |
1322 | return -EFAULT; | |
1323 | ||
1324 | offset -= skb->data - skb_mac_header(skb); | |
1325 | if (unlikely(skb_cloned(skb) && | |
1326 | bpf_skb_clone_unwritable(skb, offset + sizeof(sum)))) | |
1327 | return -EFAULT; | |
1328 | ||
1329 | ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum); | |
1330 | if (unlikely(!ptr)) | |
1331 | return -EFAULT; | |
1332 | ||
1333 | switch (BPF_HEADER_FIELD_SIZE(flags)) { | |
1334 | case 2: | |
1335 | csum_replace2(ptr, from, to); | |
1336 | break; | |
1337 | case 4: | |
1338 | csum_replace4(ptr, from, to); | |
1339 | break; | |
1340 | default: | |
1341 | return -EINVAL; | |
1342 | } | |
1343 | ||
1344 | if (ptr == &sum) | |
1345 | /* skb_store_bits guaranteed to not return -EFAULT here */ | |
1346 | skb_store_bits(skb, offset, ptr, sizeof(sum)); | |
1347 | ||
1348 | return 0; | |
1349 | } | |
1350 | ||
1351 | const struct bpf_func_proto bpf_l3_csum_replace_proto = { | |
1352 | .func = bpf_l3_csum_replace, | |
1353 | .gpl_only = false, | |
1354 | .ret_type = RET_INTEGER, | |
1355 | .arg1_type = ARG_PTR_TO_CTX, | |
1356 | .arg2_type = ARG_ANYTHING, | |
1357 | .arg3_type = ARG_ANYTHING, | |
1358 | .arg4_type = ARG_ANYTHING, | |
1359 | .arg5_type = ARG_ANYTHING, | |
1360 | }; | |
1361 | ||
1362 | static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags) | |
1363 | { | |
1364 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1365 | u32 is_pseudo = BPF_IS_PSEUDO_HEADER(flags); | |
1366 | int offset = (int) r2; | |
1367 | __sum16 sum, *ptr; | |
1368 | ||
1369 | if (unlikely((u32) offset > 0xffff)) | |
1370 | return -EFAULT; | |
1371 | ||
1372 | offset -= skb->data - skb_mac_header(skb); | |
1373 | if (unlikely(skb_cloned(skb) && | |
1374 | bpf_skb_clone_unwritable(skb, offset + sizeof(sum)))) | |
1375 | return -EFAULT; | |
1376 | ||
1377 | ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum); | |
1378 | if (unlikely(!ptr)) | |
1379 | return -EFAULT; | |
1380 | ||
1381 | switch (BPF_HEADER_FIELD_SIZE(flags)) { | |
1382 | case 2: | |
1383 | inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo); | |
1384 | break; | |
1385 | case 4: | |
1386 | inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo); | |
1387 | break; | |
1388 | default: | |
1389 | return -EINVAL; | |
1390 | } | |
1391 | ||
1392 | if (ptr == &sum) | |
1393 | /* skb_store_bits guaranteed to not return -EFAULT here */ | |
1394 | skb_store_bits(skb, offset, ptr, sizeof(sum)); | |
1395 | ||
1396 | return 0; | |
1397 | } | |
1398 | ||
1399 | const struct bpf_func_proto bpf_l4_csum_replace_proto = { | |
1400 | .func = bpf_l4_csum_replace, | |
1401 | .gpl_only = false, | |
1402 | .ret_type = RET_INTEGER, | |
1403 | .arg1_type = ARG_PTR_TO_CTX, | |
1404 | .arg2_type = ARG_ANYTHING, | |
1405 | .arg3_type = ARG_ANYTHING, | |
1406 | .arg4_type = ARG_ANYTHING, | |
1407 | .arg5_type = ARG_ANYTHING, | |
1408 | }; | |
1409 | ||
1410 | static const struct bpf_func_proto * | |
1411 | sk_filter_func_proto(enum bpf_func_id func_id) | |
1412 | { | |
1413 | switch (func_id) { | |
1414 | case BPF_FUNC_map_lookup_elem: | |
1415 | return &bpf_map_lookup_elem_proto; | |
1416 | case BPF_FUNC_map_update_elem: | |
1417 | return &bpf_map_update_elem_proto; | |
1418 | case BPF_FUNC_map_delete_elem: | |
1419 | return &bpf_map_delete_elem_proto; | |
1420 | case BPF_FUNC_get_prandom_u32: | |
1421 | return &bpf_get_prandom_u32_proto; | |
1422 | case BPF_FUNC_get_smp_processor_id: | |
1423 | return &bpf_get_smp_processor_id_proto; | |
1424 | case BPF_FUNC_tail_call: | |
1425 | return &bpf_tail_call_proto; | |
1426 | default: | |
1427 | return NULL; | |
1428 | } | |
1429 | } | |
1430 | ||
1431 | static const struct bpf_func_proto * | |
1432 | tc_cls_act_func_proto(enum bpf_func_id func_id) | |
1433 | { | |
1434 | switch (func_id) { | |
1435 | case BPF_FUNC_skb_store_bytes: | |
1436 | return &bpf_skb_store_bytes_proto; | |
1437 | case BPF_FUNC_l3_csum_replace: | |
1438 | return &bpf_l3_csum_replace_proto; | |
1439 | case BPF_FUNC_l4_csum_replace: | |
1440 | return &bpf_l4_csum_replace_proto; | |
1441 | default: | |
1442 | return sk_filter_func_proto(func_id); | |
1443 | } | |
1444 | } | |
1445 | ||
1446 | static bool sk_filter_is_valid_access(int off, int size, | |
1447 | enum bpf_access_type type) | |
1448 | { | |
1449 | /* only read is allowed */ | |
1450 | if (type != BPF_READ) | |
1451 | return false; | |
1452 | ||
1453 | /* check bounds */ | |
1454 | if (off < 0 || off >= sizeof(struct __sk_buff)) | |
1455 | return false; | |
1456 | ||
1457 | /* disallow misaligned access */ | |
1458 | if (off % size != 0) | |
1459 | return false; | |
1460 | ||
1461 | /* all __sk_buff fields are __u32 */ | |
1462 | if (size != 4) | |
1463 | return false; | |
1464 | ||
1465 | return true; | |
1466 | } | |
1467 | ||
1468 | static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off, | |
1469 | struct bpf_insn *insn_buf) | |
1470 | { | |
1471 | struct bpf_insn *insn = insn_buf; | |
1472 | ||
1473 | switch (ctx_off) { | |
1474 | case offsetof(struct __sk_buff, len): | |
1475 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4); | |
1476 | ||
1477 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
1478 | offsetof(struct sk_buff, len)); | |
1479 | break; | |
1480 | ||
1481 | case offsetof(struct __sk_buff, protocol): | |
1482 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); | |
1483 | ||
1484 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
1485 | offsetof(struct sk_buff, protocol)); | |
1486 | break; | |
1487 | ||
1488 | case offsetof(struct __sk_buff, vlan_proto): | |
1489 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); | |
1490 | ||
1491 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
1492 | offsetof(struct sk_buff, vlan_proto)); | |
1493 | break; | |
1494 | ||
1495 | case offsetof(struct __sk_buff, priority): | |
1496 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4); | |
1497 | ||
1498 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
1499 | offsetof(struct sk_buff, priority)); | |
1500 | break; | |
1501 | ||
1502 | case offsetof(struct __sk_buff, ingress_ifindex): | |
1503 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4); | |
1504 | ||
1505 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
1506 | offsetof(struct sk_buff, skb_iif)); | |
1507 | break; | |
1508 | ||
1509 | case offsetof(struct __sk_buff, ifindex): | |
1510 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); | |
1511 | ||
1512 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)), | |
1513 | dst_reg, src_reg, | |
1514 | offsetof(struct sk_buff, dev)); | |
1515 | *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1); | |
1516 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg, | |
1517 | offsetof(struct net_device, ifindex)); | |
1518 | break; | |
1519 | ||
1520 | case offsetof(struct __sk_buff, mark): | |
1521 | return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn); | |
1522 | ||
1523 | case offsetof(struct __sk_buff, pkt_type): | |
1524 | return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn); | |
1525 | ||
1526 | case offsetof(struct __sk_buff, queue_mapping): | |
1527 | return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn); | |
1528 | ||
1529 | case offsetof(struct __sk_buff, vlan_present): | |
1530 | return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, | |
1531 | dst_reg, src_reg, insn); | |
1532 | ||
1533 | case offsetof(struct __sk_buff, vlan_tci): | |
1534 | return convert_skb_access(SKF_AD_VLAN_TAG, | |
1535 | dst_reg, src_reg, insn); | |
1536 | } | |
1537 | ||
1538 | return insn - insn_buf; | |
1539 | } | |
1540 | ||
1541 | static const struct bpf_verifier_ops sk_filter_ops = { | |
1542 | .get_func_proto = sk_filter_func_proto, | |
1543 | .is_valid_access = sk_filter_is_valid_access, | |
1544 | .convert_ctx_access = sk_filter_convert_ctx_access, | |
1545 | }; | |
1546 | ||
1547 | static const struct bpf_verifier_ops tc_cls_act_ops = { | |
1548 | .get_func_proto = tc_cls_act_func_proto, | |
1549 | .is_valid_access = sk_filter_is_valid_access, | |
1550 | .convert_ctx_access = sk_filter_convert_ctx_access, | |
1551 | }; | |
1552 | ||
1553 | static struct bpf_prog_type_list sk_filter_type __read_mostly = { | |
1554 | .ops = &sk_filter_ops, | |
1555 | .type = BPF_PROG_TYPE_SOCKET_FILTER, | |
1556 | }; | |
1557 | ||
1558 | static struct bpf_prog_type_list sched_cls_type __read_mostly = { | |
1559 | .ops = &tc_cls_act_ops, | |
1560 | .type = BPF_PROG_TYPE_SCHED_CLS, | |
1561 | }; | |
1562 | ||
1563 | static struct bpf_prog_type_list sched_act_type __read_mostly = { | |
1564 | .ops = &tc_cls_act_ops, | |
1565 | .type = BPF_PROG_TYPE_SCHED_ACT, | |
1566 | }; | |
1567 | ||
1568 | static int __init register_sk_filter_ops(void) | |
1569 | { | |
1570 | bpf_register_prog_type(&sk_filter_type); | |
1571 | bpf_register_prog_type(&sched_cls_type); | |
1572 | bpf_register_prog_type(&sched_act_type); | |
1573 | ||
1574 | return 0; | |
1575 | } | |
1576 | late_initcall(register_sk_filter_ops); | |
1577 | ||
1578 | int sk_detach_filter(struct sock *sk) | |
1579 | { | |
1580 | int ret = -ENOENT; | |
1581 | struct sk_filter *filter; | |
1582 | ||
1583 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) | |
1584 | return -EPERM; | |
1585 | ||
1586 | filter = rcu_dereference_protected(sk->sk_filter, | |
1587 | sock_owned_by_user(sk)); | |
1588 | if (filter) { | |
1589 | RCU_INIT_POINTER(sk->sk_filter, NULL); | |
1590 | sk_filter_uncharge(sk, filter); | |
1591 | ret = 0; | |
1592 | } | |
1593 | ||
1594 | return ret; | |
1595 | } | |
1596 | EXPORT_SYMBOL_GPL(sk_detach_filter); | |
1597 | ||
1598 | int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, | |
1599 | unsigned int len) | |
1600 | { | |
1601 | struct sock_fprog_kern *fprog; | |
1602 | struct sk_filter *filter; | |
1603 | int ret = 0; | |
1604 | ||
1605 | lock_sock(sk); | |
1606 | filter = rcu_dereference_protected(sk->sk_filter, | |
1607 | sock_owned_by_user(sk)); | |
1608 | if (!filter) | |
1609 | goto out; | |
1610 | ||
1611 | /* We're copying the filter that has been originally attached, | |
1612 | * so no conversion/decode needed anymore. | |
1613 | */ | |
1614 | fprog = filter->prog->orig_prog; | |
1615 | ||
1616 | ret = fprog->len; | |
1617 | if (!len) | |
1618 | /* User space only enquires number of filter blocks. */ | |
1619 | goto out; | |
1620 | ||
1621 | ret = -EINVAL; | |
1622 | if (len < fprog->len) | |
1623 | goto out; | |
1624 | ||
1625 | ret = -EFAULT; | |
1626 | if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog))) | |
1627 | goto out; | |
1628 | ||
1629 | /* Instead of bytes, the API requests to return the number | |
1630 | * of filter blocks. | |
1631 | */ | |
1632 | ret = fprog->len; | |
1633 | out: | |
1634 | release_sock(sk); | |
1635 | return ret; | |
1636 | } |