2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
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.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/if_packet.h>
33 #include <linux/gfp.h>
35 #include <net/protocol.h>
36 #include <net/netlink.h>
37 #include <linux/skbuff.h>
39 #include <linux/errno.h>
40 #include <linux/timer.h>
41 #include <asm/uaccess.h>
42 #include <asm/unaligned.h>
43 #include <linux/filter.h>
44 #include <linux/ratelimit.h>
45 #include <linux/seccomp.h>
46 #include <linux/if_vlan.h>
49 * sk_filter - run a packet through a socket filter
50 * @sk: sock associated with &sk_buff
51 * @skb: buffer to filter
53 * Run the filter code and then cut skb->data to correct size returned by
54 * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
55 * than pkt_len we keep whole skb->data. This is the socket level
56 * wrapper to sk_run_filter. It returns 0 if the packet should
57 * be accepted or -EPERM if the packet should be tossed.
60 int sk_filter(struct sock *sk, struct sk_buff *skb)
63 struct sk_filter *filter;
66 * If the skb was allocated from pfmemalloc reserves, only
67 * allow SOCK_MEMALLOC sockets to use it as this socket is
70 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
73 err = security_sock_rcv_skb(sk, skb);
78 filter = rcu_dereference(sk->sk_filter);
80 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
82 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
88 EXPORT_SYMBOL(sk_filter);
90 /* Helper to find the offset of pkt_type in sk_buff structure. We want
91 * to make sure its still a 3bit field starting at a byte boundary;
92 * taken from arch/x86/net/bpf_jit_comp.c.
94 #ifdef __BIG_ENDIAN_BITFIELD
95 #define PKT_TYPE_MAX (7 << 5)
97 #define PKT_TYPE_MAX 7
99 static unsigned int pkt_type_offset(void)
101 struct sk_buff skb_probe = { .pkt_type = ~0, };
102 u8 *ct = (u8 *) &skb_probe;
105 for (off = 0; off < sizeof(struct sk_buff); off++) {
106 if (ct[off] == PKT_TYPE_MAX)
110 pr_err_once("Please fix %s, as pkt_type couldn't be found!\n", __func__);
114 static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
116 return __skb_get_poff((struct sk_buff *)(unsigned long) ctx);
119 static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
121 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
124 if (skb_is_nonlinear(skb))
127 if (skb->len < sizeof(struct nlattr))
130 if (a > skb->len - sizeof(struct nlattr))
133 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
135 return (void *) nla - (void *) skb->data;
140 static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
142 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
145 if (skb_is_nonlinear(skb))
148 if (skb->len < sizeof(struct nlattr))
151 if (a > skb->len - sizeof(struct nlattr))
154 nla = (struct nlattr *) &skb->data[a];
155 if (nla->nla_len > skb->len - a)
158 nla = nla_find_nested(nla, x);
160 return (void *) nla - (void *) skb->data;
165 static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
167 return raw_smp_processor_id();
170 /* note that this only generates 32-bit random numbers */
171 static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
173 return prandom_u32();
176 static bool convert_bpf_extensions(struct sock_filter *fp,
177 struct bpf_insn **insnp)
179 struct bpf_insn *insn = *insnp;
182 case SKF_AD_OFF + SKF_AD_PROTOCOL:
183 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
185 /* A = *(u16 *) (CTX + offsetof(protocol)) */
186 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
187 offsetof(struct sk_buff, protocol));
188 /* A = ntohs(A) [emitting a nop or swap16] */
189 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
192 case SKF_AD_OFF + SKF_AD_PKTTYPE:
193 *insn = BPF_LDX_MEM(BPF_B, BPF_REG_A, BPF_REG_CTX,
198 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, PKT_TYPE_MAX);
199 #ifdef __BIG_ENDIAN_BITFIELD
201 *insn = BPF_ALU32_IMM(BPF_RSH, BPF_REG_A, 5);
205 case SKF_AD_OFF + SKF_AD_IFINDEX:
206 case SKF_AD_OFF + SKF_AD_HATYPE:
207 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
208 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
209 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
211 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
212 BPF_REG_TMP, BPF_REG_CTX,
213 offsetof(struct sk_buff, dev));
214 /* if (tmp != 0) goto pc + 1 */
215 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
216 *insn++ = BPF_EXIT_INSN();
217 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
218 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
219 offsetof(struct net_device, ifindex));
221 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
222 offsetof(struct net_device, type));
225 case SKF_AD_OFF + SKF_AD_MARK:
226 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
228 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
229 offsetof(struct sk_buff, mark));
232 case SKF_AD_OFF + SKF_AD_RXHASH:
233 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
235 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
236 offsetof(struct sk_buff, hash));
239 case SKF_AD_OFF + SKF_AD_QUEUE:
240 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
242 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
243 offsetof(struct sk_buff, queue_mapping));
246 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
247 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
248 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
249 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
251 /* A = *(u16 *) (CTX + offsetof(vlan_tci)) */
252 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
253 offsetof(struct sk_buff, vlan_tci));
254 if (fp->k == SKF_AD_OFF + SKF_AD_VLAN_TAG) {
255 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A,
259 *insn++ = BPF_ALU32_IMM(BPF_RSH, BPF_REG_A, 12);
261 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 1);
265 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
266 case SKF_AD_OFF + SKF_AD_NLATTR:
267 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
268 case SKF_AD_OFF + SKF_AD_CPU:
269 case SKF_AD_OFF + SKF_AD_RANDOM:
271 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
273 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
275 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
276 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
278 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
279 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
281 case SKF_AD_OFF + SKF_AD_NLATTR:
282 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
284 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
285 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
287 case SKF_AD_OFF + SKF_AD_CPU:
288 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
290 case SKF_AD_OFF + SKF_AD_RANDOM:
291 *insn = BPF_EMIT_CALL(__get_random_u32);
296 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
298 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
302 /* This is just a dummy call to avoid letting the compiler
303 * evict __bpf_call_base() as an optimization. Placed here
304 * where no-one bothers.
306 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
315 * bpf_convert_filter - convert filter program
316 * @prog: the user passed filter program
317 * @len: the length of the user passed filter program
318 * @new_prog: buffer where converted program will be stored
319 * @new_len: pointer to store length of converted program
321 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
322 * Conversion workflow:
324 * 1) First pass for calculating the new program length:
325 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
327 * 2) 2nd pass to remap in two passes: 1st pass finds new
328 * jump offsets, 2nd pass remapping:
329 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
330 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
332 * User BPF's register A is mapped to our BPF register 6, user BPF
333 * register X is mapped to BPF register 7; frame pointer is always
334 * register 10; Context 'void *ctx' is stored in register 1, that is,
335 * for socket filters: ctx == 'struct sk_buff *', for seccomp:
336 * ctx == 'struct seccomp_data *'.
338 int bpf_convert_filter(struct sock_filter *prog, int len,
339 struct bpf_insn *new_prog, int *new_len)
341 int new_flen = 0, pass = 0, target, i;
342 struct bpf_insn *new_insn;
343 struct sock_filter *fp;
347 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
348 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
350 if (len <= 0 || len > BPF_MAXINSNS)
354 addrs = kcalloc(len, sizeof(*addrs), GFP_KERNEL);
364 *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
367 for (i = 0; i < len; fp++, i++) {
368 struct bpf_insn tmp_insns[6] = { };
369 struct bpf_insn *insn = tmp_insns;
372 addrs[i] = new_insn - new_prog;
375 /* All arithmetic insns and skb loads map as-is. */
376 case BPF_ALU | BPF_ADD | BPF_X:
377 case BPF_ALU | BPF_ADD | BPF_K:
378 case BPF_ALU | BPF_SUB | BPF_X:
379 case BPF_ALU | BPF_SUB | BPF_K:
380 case BPF_ALU | BPF_AND | BPF_X:
381 case BPF_ALU | BPF_AND | BPF_K:
382 case BPF_ALU | BPF_OR | BPF_X:
383 case BPF_ALU | BPF_OR | BPF_K:
384 case BPF_ALU | BPF_LSH | BPF_X:
385 case BPF_ALU | BPF_LSH | BPF_K:
386 case BPF_ALU | BPF_RSH | BPF_X:
387 case BPF_ALU | BPF_RSH | BPF_K:
388 case BPF_ALU | BPF_XOR | BPF_X:
389 case BPF_ALU | BPF_XOR | BPF_K:
390 case BPF_ALU | BPF_MUL | BPF_X:
391 case BPF_ALU | BPF_MUL | BPF_K:
392 case BPF_ALU | BPF_DIV | BPF_X:
393 case BPF_ALU | BPF_DIV | BPF_K:
394 case BPF_ALU | BPF_MOD | BPF_X:
395 case BPF_ALU | BPF_MOD | BPF_K:
396 case BPF_ALU | BPF_NEG:
397 case BPF_LD | BPF_ABS | BPF_W:
398 case BPF_LD | BPF_ABS | BPF_H:
399 case BPF_LD | BPF_ABS | BPF_B:
400 case BPF_LD | BPF_IND | BPF_W:
401 case BPF_LD | BPF_IND | BPF_H:
402 case BPF_LD | BPF_IND | BPF_B:
403 /* Check for overloaded BPF extension and
404 * directly convert it if found, otherwise
405 * just move on with mapping.
407 if (BPF_CLASS(fp->code) == BPF_LD &&
408 BPF_MODE(fp->code) == BPF_ABS &&
409 convert_bpf_extensions(fp, &insn))
412 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
415 /* Jump transformation cannot use BPF block macros
416 * everywhere as offset calculation and target updates
417 * require a bit more work than the rest, i.e. jump
418 * opcodes map as-is, but offsets need adjustment.
421 #define BPF_EMIT_JMP \
423 if (target >= len || target < 0) \
425 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
426 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
427 insn->off -= insn - tmp_insns; \
430 case BPF_JMP | BPF_JA:
431 target = i + fp->k + 1;
432 insn->code = fp->code;
436 case BPF_JMP | BPF_JEQ | BPF_K:
437 case BPF_JMP | BPF_JEQ | BPF_X:
438 case BPF_JMP | BPF_JSET | BPF_K:
439 case BPF_JMP | BPF_JSET | BPF_X:
440 case BPF_JMP | BPF_JGT | BPF_K:
441 case BPF_JMP | BPF_JGT | BPF_X:
442 case BPF_JMP | BPF_JGE | BPF_K:
443 case BPF_JMP | BPF_JGE | BPF_X:
444 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
445 /* BPF immediates are signed, zero extend
446 * immediate into tmp register and use it
449 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
451 insn->dst_reg = BPF_REG_A;
452 insn->src_reg = BPF_REG_TMP;
455 insn->dst_reg = BPF_REG_A;
456 insn->src_reg = BPF_REG_X;
458 bpf_src = BPF_SRC(fp->code);
461 /* Common case where 'jump_false' is next insn. */
463 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
464 target = i + fp->jt + 1;
469 /* Convert JEQ into JNE when 'jump_true' is next insn. */
470 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
471 insn->code = BPF_JMP | BPF_JNE | bpf_src;
472 target = i + fp->jf + 1;
477 /* Other jumps are mapped into two insns: Jxx and JA. */
478 target = i + fp->jt + 1;
479 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
483 insn->code = BPF_JMP | BPF_JA;
484 target = i + fp->jf + 1;
488 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
489 case BPF_LDX | BPF_MSH | BPF_B:
491 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
492 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
493 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
495 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
497 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
499 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
501 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
504 /* RET_K, RET_A are remaped into 2 insns. */
505 case BPF_RET | BPF_A:
506 case BPF_RET | BPF_K:
507 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
508 BPF_K : BPF_X, BPF_REG_0,
510 *insn = BPF_EXIT_INSN();
513 /* Store to stack. */
516 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
517 BPF_ST ? BPF_REG_A : BPF_REG_X,
518 -(BPF_MEMWORDS - fp->k) * 4);
521 /* Load from stack. */
522 case BPF_LD | BPF_MEM:
523 case BPF_LDX | BPF_MEM:
524 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
525 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
526 -(BPF_MEMWORDS - fp->k) * 4);
530 case BPF_LD | BPF_IMM:
531 case BPF_LDX | BPF_IMM:
532 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
533 BPF_REG_A : BPF_REG_X, fp->k);
537 case BPF_MISC | BPF_TAX:
538 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
542 case BPF_MISC | BPF_TXA:
543 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
546 /* A = skb->len or X = skb->len */
547 case BPF_LD | BPF_W | BPF_LEN:
548 case BPF_LDX | BPF_W | BPF_LEN:
549 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
550 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
551 offsetof(struct sk_buff, len));
554 /* Access seccomp_data fields. */
555 case BPF_LDX | BPF_ABS | BPF_W:
556 /* A = *(u32 *) (ctx + K) */
557 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
560 /* Unkown instruction. */
567 memcpy(new_insn, tmp_insns,
568 sizeof(*insn) * (insn - tmp_insns));
569 new_insn += insn - tmp_insns;
573 /* Only calculating new length. */
574 *new_len = new_insn - new_prog;
579 if (new_flen != new_insn - new_prog) {
580 new_flen = new_insn - new_prog;
587 BUG_ON(*new_len != new_flen);
596 * A BPF program is able to use 16 cells of memory to store intermediate
597 * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter()).
599 * As we dont want to clear mem[] array for each packet going through
600 * sk_run_filter(), we check that filter loaded by user never try to read
601 * a cell if not previously written, and we check all branches to be sure
602 * a malicious user doesn't try to abuse us.
604 static int check_load_and_stores(const struct sock_filter *filter, int flen)
606 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
609 BUILD_BUG_ON(BPF_MEMWORDS > 16);
611 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
615 memset(masks, 0xff, flen * sizeof(*masks));
617 for (pc = 0; pc < flen; pc++) {
618 memvalid &= masks[pc];
620 switch (filter[pc].code) {
623 memvalid |= (1 << filter[pc].k);
625 case BPF_LD | BPF_MEM:
626 case BPF_LDX | BPF_MEM:
627 if (!(memvalid & (1 << filter[pc].k))) {
632 case BPF_JMP | BPF_JA:
633 /* A jump must set masks on target */
634 masks[pc + 1 + filter[pc].k] &= memvalid;
637 case BPF_JMP | BPF_JEQ | BPF_K:
638 case BPF_JMP | BPF_JEQ | BPF_X:
639 case BPF_JMP | BPF_JGE | BPF_K:
640 case BPF_JMP | BPF_JGE | BPF_X:
641 case BPF_JMP | BPF_JGT | BPF_K:
642 case BPF_JMP | BPF_JGT | BPF_X:
643 case BPF_JMP | BPF_JSET | BPF_K:
644 case BPF_JMP | BPF_JSET | BPF_X:
645 /* A jump must set masks on targets */
646 masks[pc + 1 + filter[pc].jt] &= memvalid;
647 masks[pc + 1 + filter[pc].jf] &= memvalid;
657 static bool chk_code_allowed(u16 code_to_probe)
659 static const bool codes[] = {
660 /* 32 bit ALU operations */
661 [BPF_ALU | BPF_ADD | BPF_K] = true,
662 [BPF_ALU | BPF_ADD | BPF_X] = true,
663 [BPF_ALU | BPF_SUB | BPF_K] = true,
664 [BPF_ALU | BPF_SUB | BPF_X] = true,
665 [BPF_ALU | BPF_MUL | BPF_K] = true,
666 [BPF_ALU | BPF_MUL | BPF_X] = true,
667 [BPF_ALU | BPF_DIV | BPF_K] = true,
668 [BPF_ALU | BPF_DIV | BPF_X] = true,
669 [BPF_ALU | BPF_MOD | BPF_K] = true,
670 [BPF_ALU | BPF_MOD | BPF_X] = true,
671 [BPF_ALU | BPF_AND | BPF_K] = true,
672 [BPF_ALU | BPF_AND | BPF_X] = true,
673 [BPF_ALU | BPF_OR | BPF_K] = true,
674 [BPF_ALU | BPF_OR | BPF_X] = true,
675 [BPF_ALU | BPF_XOR | BPF_K] = true,
676 [BPF_ALU | BPF_XOR | BPF_X] = true,
677 [BPF_ALU | BPF_LSH | BPF_K] = true,
678 [BPF_ALU | BPF_LSH | BPF_X] = true,
679 [BPF_ALU | BPF_RSH | BPF_K] = true,
680 [BPF_ALU | BPF_RSH | BPF_X] = true,
681 [BPF_ALU | BPF_NEG] = true,
682 /* Load instructions */
683 [BPF_LD | BPF_W | BPF_ABS] = true,
684 [BPF_LD | BPF_H | BPF_ABS] = true,
685 [BPF_LD | BPF_B | BPF_ABS] = true,
686 [BPF_LD | BPF_W | BPF_LEN] = true,
687 [BPF_LD | BPF_W | BPF_IND] = true,
688 [BPF_LD | BPF_H | BPF_IND] = true,
689 [BPF_LD | BPF_B | BPF_IND] = true,
690 [BPF_LD | BPF_IMM] = true,
691 [BPF_LD | BPF_MEM] = true,
692 [BPF_LDX | BPF_W | BPF_LEN] = true,
693 [BPF_LDX | BPF_B | BPF_MSH] = true,
694 [BPF_LDX | BPF_IMM] = true,
695 [BPF_LDX | BPF_MEM] = true,
696 /* Store instructions */
699 /* Misc instructions */
700 [BPF_MISC | BPF_TAX] = true,
701 [BPF_MISC | BPF_TXA] = true,
702 /* Return instructions */
703 [BPF_RET | BPF_K] = true,
704 [BPF_RET | BPF_A] = true,
705 /* Jump instructions */
706 [BPF_JMP | BPF_JA] = true,
707 [BPF_JMP | BPF_JEQ | BPF_K] = true,
708 [BPF_JMP | BPF_JEQ | BPF_X] = true,
709 [BPF_JMP | BPF_JGE | BPF_K] = true,
710 [BPF_JMP | BPF_JGE | BPF_X] = true,
711 [BPF_JMP | BPF_JGT | BPF_K] = true,
712 [BPF_JMP | BPF_JGT | BPF_X] = true,
713 [BPF_JMP | BPF_JSET | BPF_K] = true,
714 [BPF_JMP | BPF_JSET | BPF_X] = true,
717 if (code_to_probe >= ARRAY_SIZE(codes))
720 return codes[code_to_probe];
724 * bpf_check_classic - verify socket filter code
725 * @filter: filter to verify
726 * @flen: length of filter
728 * Check the user's filter code. If we let some ugly
729 * filter code slip through kaboom! The filter must contain
730 * no references or jumps that are out of range, no illegal
731 * instructions, and must end with a RET instruction.
733 * All jumps are forward as they are not signed.
735 * Returns 0 if the rule set is legal or -EINVAL if not.
737 int bpf_check_classic(const struct sock_filter *filter, unsigned int flen)
742 if (flen == 0 || flen > BPF_MAXINSNS)
745 /* Check the filter code now */
746 for (pc = 0; pc < flen; pc++) {
747 const struct sock_filter *ftest = &filter[pc];
749 /* May we actually operate on this code? */
750 if (!chk_code_allowed(ftest->code))
753 /* Some instructions need special checks */
754 switch (ftest->code) {
755 case BPF_ALU | BPF_DIV | BPF_K:
756 case BPF_ALU | BPF_MOD | BPF_K:
757 /* Check for division by zero */
761 case BPF_LD | BPF_MEM:
762 case BPF_LDX | BPF_MEM:
765 /* Check for invalid memory addresses */
766 if (ftest->k >= BPF_MEMWORDS)
769 case BPF_JMP | BPF_JA:
770 /* Note, the large ftest->k might cause loops.
771 * Compare this with conditional jumps below,
772 * where offsets are limited. --ANK (981016)
774 if (ftest->k >= (unsigned int)(flen - pc - 1))
777 case BPF_JMP | BPF_JEQ | BPF_K:
778 case BPF_JMP | BPF_JEQ | BPF_X:
779 case BPF_JMP | BPF_JGE | BPF_K:
780 case BPF_JMP | BPF_JGE | BPF_X:
781 case BPF_JMP | BPF_JGT | BPF_K:
782 case BPF_JMP | BPF_JGT | BPF_X:
783 case BPF_JMP | BPF_JSET | BPF_K:
784 case BPF_JMP | BPF_JSET | BPF_X:
785 /* Both conditionals must be safe */
786 if (pc + ftest->jt + 1 >= flen ||
787 pc + ftest->jf + 1 >= flen)
790 case BPF_LD | BPF_W | BPF_ABS:
791 case BPF_LD | BPF_H | BPF_ABS:
792 case BPF_LD | BPF_B | BPF_ABS:
794 if (bpf_anc_helper(ftest) & BPF_ANC)
796 /* Ancillary operation unknown or unsupported */
797 if (anc_found == false && ftest->k >= SKF_AD_OFF)
802 /* Last instruction must be a RET code */
803 switch (filter[flen - 1].code) {
804 case BPF_RET | BPF_K:
805 case BPF_RET | BPF_A:
806 return check_load_and_stores(filter, flen);
811 EXPORT_SYMBOL(bpf_check_classic);
813 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
814 const struct sock_fprog *fprog)
816 unsigned int fsize = bpf_classic_proglen(fprog);
817 struct sock_fprog_kern *fkprog;
819 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
823 fkprog = fp->orig_prog;
824 fkprog->len = fprog->len;
825 fkprog->filter = kmemdup(fp->insns, fsize, GFP_KERNEL);
826 if (!fkprog->filter) {
827 kfree(fp->orig_prog);
834 static void bpf_release_orig_filter(struct bpf_prog *fp)
836 struct sock_fprog_kern *fprog = fp->orig_prog;
839 kfree(fprog->filter);
844 static void __bpf_prog_release(struct bpf_prog *prog)
846 bpf_release_orig_filter(prog);
850 static void __sk_filter_release(struct sk_filter *fp)
852 __bpf_prog_release(fp->prog);
857 * sk_filter_release_rcu - Release a socket filter by rcu_head
858 * @rcu: rcu_head that contains the sk_filter to free
860 static void sk_filter_release_rcu(struct rcu_head *rcu)
862 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
864 __sk_filter_release(fp);
868 * sk_filter_release - release a socket filter
869 * @fp: filter to remove
871 * Remove a filter from a socket and release its resources.
873 static void sk_filter_release(struct sk_filter *fp)
875 if (atomic_dec_and_test(&fp->refcnt))
876 call_rcu(&fp->rcu, sk_filter_release_rcu);
879 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
881 u32 filter_size = bpf_prog_size(fp->prog->len);
883 atomic_sub(filter_size, &sk->sk_omem_alloc);
884 sk_filter_release(fp);
887 /* try to charge the socket memory if there is space available
888 * return true on success
890 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
892 u32 filter_size = bpf_prog_size(fp->prog->len);
894 /* same check as in sock_kmalloc() */
895 if (filter_size <= sysctl_optmem_max &&
896 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
897 atomic_inc(&fp->refcnt);
898 atomic_add(filter_size, &sk->sk_omem_alloc);
904 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
906 struct sock_filter *old_prog;
907 struct bpf_prog *old_fp;
908 int err, new_len, old_len = fp->len;
910 /* We are free to overwrite insns et al right here as it
911 * won't be used at this point in time anymore internally
912 * after the migration to the internal BPF instruction
915 BUILD_BUG_ON(sizeof(struct sock_filter) !=
916 sizeof(struct bpf_insn));
918 /* Conversion cannot happen on overlapping memory areas,
919 * so we need to keep the user BPF around until the 2nd
920 * pass. At this time, the user BPF is stored in fp->insns.
922 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
929 /* 1st pass: calculate the new program length. */
930 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
934 /* Expand fp for appending the new filter representation. */
936 fp = krealloc(old_fp, bpf_prog_size(new_len), GFP_KERNEL);
938 /* The old_fp is still around in case we couldn't
939 * allocate new memory, so uncharge on that one.
948 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
949 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
951 /* 2nd bpf_convert_filter() can fail only if it fails
952 * to allocate memory, remapping must succeed. Note,
953 * that at this time old_fp has already been released
958 bpf_prog_select_runtime(fp);
966 __bpf_prog_release(fp);
970 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp)
977 err = bpf_check_classic(fp->insns, fp->len);
979 __bpf_prog_release(fp);
983 /* Probe if we can JIT compile the filter and if so, do
984 * the compilation of the filter.
988 /* JIT compiler couldn't process this filter, so do the
989 * internal BPF translation for the optimized interpreter.
992 fp = bpf_migrate_filter(fp);
998 * bpf_prog_create - create an unattached filter
999 * @pfp: the unattached filter that is created
1000 * @fprog: the filter program
1002 * Create a filter independent of any socket. We first run some
1003 * sanity checks on it to make sure it does not explode on us later.
1004 * If an error occurs or there is insufficient memory for the filter
1005 * a negative errno code is returned. On success the return is zero.
1007 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1009 unsigned int fsize = bpf_classic_proglen(fprog);
1010 struct bpf_prog *fp;
1012 /* Make sure new filter is there and in the right amounts. */
1013 if (fprog->filter == NULL)
1016 fp = kmalloc(bpf_prog_size(fprog->len), GFP_KERNEL);
1020 memcpy(fp->insns, fprog->filter, fsize);
1022 fp->len = fprog->len;
1023 /* Since unattached filters are not copied back to user
1024 * space through sk_get_filter(), we do not need to hold
1025 * a copy here, and can spare us the work.
1027 fp->orig_prog = NULL;
1029 /* bpf_prepare_filter() already takes care of freeing
1030 * memory in case something goes wrong.
1032 fp = bpf_prepare_filter(fp);
1039 EXPORT_SYMBOL_GPL(bpf_prog_create);
1041 void bpf_prog_destroy(struct bpf_prog *fp)
1043 __bpf_prog_release(fp);
1045 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1048 * sk_attach_filter - attach a socket filter
1049 * @fprog: the filter program
1050 * @sk: the socket to use
1052 * Attach the user's filter code. We first run some sanity checks on
1053 * it to make sure it does not explode on us later. If an error
1054 * occurs or there is insufficient memory for the filter a negative
1055 * errno code is returned. On success the return is zero.
1057 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1059 struct sk_filter *fp, *old_fp;
1060 unsigned int fsize = bpf_classic_proglen(fprog);
1061 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1062 struct bpf_prog *prog;
1065 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1068 /* Make sure new filter is there and in the right amounts. */
1069 if (fprog->filter == NULL)
1072 prog = kmalloc(bpf_fsize, GFP_KERNEL);
1076 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1081 prog->len = fprog->len;
1083 err = bpf_prog_store_orig_filter(prog, fprog);
1089 /* bpf_prepare_filter() already takes care of freeing
1090 * memory in case something goes wrong.
1092 prog = bpf_prepare_filter(prog);
1094 return PTR_ERR(prog);
1096 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1098 __bpf_prog_release(prog);
1103 atomic_set(&fp->refcnt, 0);
1105 if (!sk_filter_charge(sk, fp)) {
1106 __sk_filter_release(fp);
1110 old_fp = rcu_dereference_protected(sk->sk_filter,
1111 sock_owned_by_user(sk));
1112 rcu_assign_pointer(sk->sk_filter, fp);
1115 sk_filter_uncharge(sk, old_fp);
1119 EXPORT_SYMBOL_GPL(sk_attach_filter);
1121 int sk_detach_filter(struct sock *sk)
1124 struct sk_filter *filter;
1126 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1129 filter = rcu_dereference_protected(sk->sk_filter,
1130 sock_owned_by_user(sk));
1132 RCU_INIT_POINTER(sk->sk_filter, NULL);
1133 sk_filter_uncharge(sk, filter);
1139 EXPORT_SYMBOL_GPL(sk_detach_filter);
1141 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
1144 struct sock_fprog_kern *fprog;
1145 struct sk_filter *filter;
1149 filter = rcu_dereference_protected(sk->sk_filter,
1150 sock_owned_by_user(sk));
1154 /* We're copying the filter that has been originally attached,
1155 * so no conversion/decode needed anymore.
1157 fprog = filter->prog->orig_prog;
1161 /* User space only enquires number of filter blocks. */
1165 if (len < fprog->len)
1169 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
1172 /* Instead of bytes, the API requests to return the number