2 * Linux Socket Filter - Kernel level socket filtering
5 * Jay Schulist <jschlst@samba.org>
7 * Based on the design of:
8 * - The Berkeley Packet Filter
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * Andi Kleen - Fix a few bad bugs and races.
16 * Kris Katterjohn - Added many additional checks in sk_chk_filter()
19 #include <linux/module.h>
20 #include <linux/types.h>
22 #include <linux/fcntl.h>
23 #include <linux/socket.h>
25 #include <linux/inet.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_packet.h>
28 #include <linux/gfp.h>
30 #include <net/protocol.h>
31 #include <net/netlink.h>
32 #include <linux/skbuff.h>
34 #include <linux/errno.h>
35 #include <linux/timer.h>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 #include <asm/unaligned.h>
39 #include <linux/filter.h>
40 #include <linux/reciprocal_div.h>
41 #include <linux/ratelimit.h>
43 /* No hurry in this branch */
44 static void *__load_pointer(const struct sk_buff *skb, int k, unsigned int size)
49 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
50 else if (k >= SKF_LL_OFF)
51 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
53 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
58 static inline void *load_pointer(const struct sk_buff *skb, int k,
59 unsigned int size, void *buffer)
62 return skb_header_pointer(skb, k, size, buffer);
63 return __load_pointer(skb, k, size);
67 * sk_filter - run a packet through a socket filter
68 * @sk: sock associated with &sk_buff
69 * @skb: buffer to filter
71 * Run the filter code and then cut skb->data to correct size returned by
72 * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
73 * than pkt_len we keep whole skb->data. This is the socket level
74 * wrapper to sk_run_filter. It returns 0 if the packet should
75 * be accepted or -EPERM if the packet should be tossed.
78 int sk_filter(struct sock *sk, struct sk_buff *skb)
81 struct sk_filter *filter;
83 err = security_sock_rcv_skb(sk, skb);
88 filter = rcu_dereference(sk->sk_filter);
90 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
92 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
98 EXPORT_SYMBOL(sk_filter);
101 * sk_run_filter - run a filter on a socket
102 * @skb: buffer to run the filter on
103 * @fentry: filter to apply
105 * Decode and apply filter instructions to the skb->data.
106 * Return length to keep, 0 for none. @skb is the data we are
107 * filtering, @filter is the array of filter instructions.
108 * Because all jumps are guaranteed to be before last instruction,
109 * and last instruction guaranteed to be a RET, we dont need to check
110 * flen. (We used to pass to this function the length of filter)
112 unsigned int sk_run_filter(const struct sk_buff *skb,
113 const struct sock_filter *fentry)
116 u32 A = 0; /* Accumulator */
117 u32 X = 0; /* Index Register */
118 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
123 * Process array of filter instructions.
126 #if defined(CONFIG_X86_32)
127 #define K (fentry->k)
129 const u32 K = fentry->k;
132 switch (fentry->code) {
133 case BPF_S_ALU_ADD_X:
136 case BPF_S_ALU_ADD_K:
139 case BPF_S_ALU_SUB_X:
142 case BPF_S_ALU_SUB_K:
145 case BPF_S_ALU_MUL_X:
148 case BPF_S_ALU_MUL_K:
151 case BPF_S_ALU_DIV_X:
156 case BPF_S_ALU_DIV_K:
157 A = reciprocal_divide(A, K);
159 case BPF_S_ALU_AND_X:
162 case BPF_S_ALU_AND_K:
171 case BPF_S_ALU_LSH_X:
174 case BPF_S_ALU_LSH_K:
177 case BPF_S_ALU_RSH_X:
180 case BPF_S_ALU_RSH_K:
189 case BPF_S_JMP_JGT_K:
190 fentry += (A > K) ? fentry->jt : fentry->jf;
192 case BPF_S_JMP_JGE_K:
193 fentry += (A >= K) ? fentry->jt : fentry->jf;
195 case BPF_S_JMP_JEQ_K:
196 fentry += (A == K) ? fentry->jt : fentry->jf;
198 case BPF_S_JMP_JSET_K:
199 fentry += (A & K) ? fentry->jt : fentry->jf;
201 case BPF_S_JMP_JGT_X:
202 fentry += (A > X) ? fentry->jt : fentry->jf;
204 case BPF_S_JMP_JGE_X:
205 fentry += (A >= X) ? fentry->jt : fentry->jf;
207 case BPF_S_JMP_JEQ_X:
208 fentry += (A == X) ? fentry->jt : fentry->jf;
210 case BPF_S_JMP_JSET_X:
211 fentry += (A & X) ? fentry->jt : fentry->jf;
216 ptr = load_pointer(skb, k, 4, &tmp);
218 A = get_unaligned_be32(ptr);
225 ptr = load_pointer(skb, k, 2, &tmp);
227 A = get_unaligned_be16(ptr);
234 ptr = load_pointer(skb, k, 1, &tmp);
243 case BPF_S_LDX_W_LEN:
255 case BPF_S_LDX_B_MSH:
256 ptr = load_pointer(skb, K, 1, &tmp);
258 X = (*(u8 *)ptr & 0xf) << 2;
290 case BPF_S_ANC_PROTOCOL:
291 A = ntohs(skb->protocol);
293 case BPF_S_ANC_PKTTYPE:
296 case BPF_S_ANC_IFINDEX:
299 A = skb->dev->ifindex;
304 case BPF_S_ANC_QUEUE:
305 A = skb->queue_mapping;
307 case BPF_S_ANC_HATYPE:
312 case BPF_S_ANC_RXHASH:
316 A = raw_smp_processor_id();
318 case BPF_S_ANC_NLATTR: {
321 if (skb_is_nonlinear(skb))
323 if (A > skb->len - sizeof(struct nlattr))
326 nla = nla_find((struct nlattr *)&skb->data[A],
329 A = (void *)nla - (void *)skb->data;
334 case BPF_S_ANC_NLATTR_NEST: {
337 if (skb_is_nonlinear(skb))
339 if (A > skb->len - sizeof(struct nlattr))
342 nla = (struct nlattr *)&skb->data[A];
343 if (nla->nla_len > A - skb->len)
346 nla = nla_find_nested(nla, X);
348 A = (void *)nla - (void *)skb->data;
354 WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n",
355 fentry->code, fentry->jt,
356 fentry->jf, fentry->k);
363 EXPORT_SYMBOL(sk_run_filter);
367 * A BPF program is able to use 16 cells of memory to store intermediate
368 * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter())
369 * As we dont want to clear mem[] array for each packet going through
370 * sk_run_filter(), we check that filter loaded by user never try to read
371 * a cell if not previously written, and we check all branches to be sure
372 * a malicious user doesn't try to abuse us.
374 static int check_load_and_stores(struct sock_filter *filter, int flen)
376 u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */
379 BUILD_BUG_ON(BPF_MEMWORDS > 16);
380 masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
383 memset(masks, 0xff, flen * sizeof(*masks));
385 for (pc = 0; pc < flen; pc++) {
386 memvalid &= masks[pc];
388 switch (filter[pc].code) {
391 memvalid |= (1 << filter[pc].k);
395 if (!(memvalid & (1 << filter[pc].k))) {
401 /* a jump must set masks on target */
402 masks[pc + 1 + filter[pc].k] &= memvalid;
405 case BPF_S_JMP_JEQ_K:
406 case BPF_S_JMP_JEQ_X:
407 case BPF_S_JMP_JGE_K:
408 case BPF_S_JMP_JGE_X:
409 case BPF_S_JMP_JGT_K:
410 case BPF_S_JMP_JGT_X:
411 case BPF_S_JMP_JSET_X:
412 case BPF_S_JMP_JSET_K:
413 /* a jump must set masks on targets */
414 masks[pc + 1 + filter[pc].jt] &= memvalid;
415 masks[pc + 1 + filter[pc].jf] &= memvalid;
426 * sk_chk_filter - verify socket filter code
427 * @filter: filter to verify
428 * @flen: length of filter
430 * Check the user's filter code. If we let some ugly
431 * filter code slip through kaboom! The filter must contain
432 * no references or jumps that are out of range, no illegal
433 * instructions, and must end with a RET instruction.
435 * All jumps are forward as they are not signed.
437 * Returns 0 if the rule set is legal or -EINVAL if not.
439 int sk_chk_filter(struct sock_filter *filter, int flen)
442 * Valid instructions are initialized to non-0.
443 * Invalid instructions are initialized to 0.
445 static const u8 codes[] = {
446 [BPF_ALU|BPF_ADD|BPF_K] = BPF_S_ALU_ADD_K,
447 [BPF_ALU|BPF_ADD|BPF_X] = BPF_S_ALU_ADD_X,
448 [BPF_ALU|BPF_SUB|BPF_K] = BPF_S_ALU_SUB_K,
449 [BPF_ALU|BPF_SUB|BPF_X] = BPF_S_ALU_SUB_X,
450 [BPF_ALU|BPF_MUL|BPF_K] = BPF_S_ALU_MUL_K,
451 [BPF_ALU|BPF_MUL|BPF_X] = BPF_S_ALU_MUL_X,
452 [BPF_ALU|BPF_DIV|BPF_X] = BPF_S_ALU_DIV_X,
453 [BPF_ALU|BPF_AND|BPF_K] = BPF_S_ALU_AND_K,
454 [BPF_ALU|BPF_AND|BPF_X] = BPF_S_ALU_AND_X,
455 [BPF_ALU|BPF_OR|BPF_K] = BPF_S_ALU_OR_K,
456 [BPF_ALU|BPF_OR|BPF_X] = BPF_S_ALU_OR_X,
457 [BPF_ALU|BPF_LSH|BPF_K] = BPF_S_ALU_LSH_K,
458 [BPF_ALU|BPF_LSH|BPF_X] = BPF_S_ALU_LSH_X,
459 [BPF_ALU|BPF_RSH|BPF_K] = BPF_S_ALU_RSH_K,
460 [BPF_ALU|BPF_RSH|BPF_X] = BPF_S_ALU_RSH_X,
461 [BPF_ALU|BPF_NEG] = BPF_S_ALU_NEG,
462 [BPF_LD|BPF_W|BPF_ABS] = BPF_S_LD_W_ABS,
463 [BPF_LD|BPF_H|BPF_ABS] = BPF_S_LD_H_ABS,
464 [BPF_LD|BPF_B|BPF_ABS] = BPF_S_LD_B_ABS,
465 [BPF_LD|BPF_W|BPF_LEN] = BPF_S_LD_W_LEN,
466 [BPF_LD|BPF_W|BPF_IND] = BPF_S_LD_W_IND,
467 [BPF_LD|BPF_H|BPF_IND] = BPF_S_LD_H_IND,
468 [BPF_LD|BPF_B|BPF_IND] = BPF_S_LD_B_IND,
469 [BPF_LD|BPF_IMM] = BPF_S_LD_IMM,
470 [BPF_LDX|BPF_W|BPF_LEN] = BPF_S_LDX_W_LEN,
471 [BPF_LDX|BPF_B|BPF_MSH] = BPF_S_LDX_B_MSH,
472 [BPF_LDX|BPF_IMM] = BPF_S_LDX_IMM,
473 [BPF_MISC|BPF_TAX] = BPF_S_MISC_TAX,
474 [BPF_MISC|BPF_TXA] = BPF_S_MISC_TXA,
475 [BPF_RET|BPF_K] = BPF_S_RET_K,
476 [BPF_RET|BPF_A] = BPF_S_RET_A,
477 [BPF_ALU|BPF_DIV|BPF_K] = BPF_S_ALU_DIV_K,
478 [BPF_LD|BPF_MEM] = BPF_S_LD_MEM,
479 [BPF_LDX|BPF_MEM] = BPF_S_LDX_MEM,
481 [BPF_STX] = BPF_S_STX,
482 [BPF_JMP|BPF_JA] = BPF_S_JMP_JA,
483 [BPF_JMP|BPF_JEQ|BPF_K] = BPF_S_JMP_JEQ_K,
484 [BPF_JMP|BPF_JEQ|BPF_X] = BPF_S_JMP_JEQ_X,
485 [BPF_JMP|BPF_JGE|BPF_K] = BPF_S_JMP_JGE_K,
486 [BPF_JMP|BPF_JGE|BPF_X] = BPF_S_JMP_JGE_X,
487 [BPF_JMP|BPF_JGT|BPF_K] = BPF_S_JMP_JGT_K,
488 [BPF_JMP|BPF_JGT|BPF_X] = BPF_S_JMP_JGT_X,
489 [BPF_JMP|BPF_JSET|BPF_K] = BPF_S_JMP_JSET_K,
490 [BPF_JMP|BPF_JSET|BPF_X] = BPF_S_JMP_JSET_X,
494 if (flen == 0 || flen > BPF_MAXINSNS)
497 /* check the filter code now */
498 for (pc = 0; pc < flen; pc++) {
499 struct sock_filter *ftest = &filter[pc];
500 u16 code = ftest->code;
502 if (code >= ARRAY_SIZE(codes))
507 /* Some instructions need special checks */
509 case BPF_S_ALU_DIV_K:
510 /* check for division by zero */
513 ftest->k = reciprocal_value(ftest->k);
519 /* check for invalid memory addresses */
520 if (ftest->k >= BPF_MEMWORDS)
525 * Note, the large ftest->k might cause loops.
526 * Compare this with conditional jumps below,
527 * where offsets are limited. --ANK (981016)
529 if (ftest->k >= (unsigned)(flen-pc-1))
532 case BPF_S_JMP_JEQ_K:
533 case BPF_S_JMP_JEQ_X:
534 case BPF_S_JMP_JGE_K:
535 case BPF_S_JMP_JGE_X:
536 case BPF_S_JMP_JGT_K:
537 case BPF_S_JMP_JGT_X:
538 case BPF_S_JMP_JSET_X:
539 case BPF_S_JMP_JSET_K:
540 /* for conditionals both must be safe */
541 if (pc + ftest->jt + 1 >= flen ||
542 pc + ftest->jf + 1 >= flen)
548 #define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
549 code = BPF_S_ANC_##CODE; \
556 ANCILLARY(NLATTR_NEST);
567 /* last instruction must be a RET code */
568 switch (filter[flen - 1].code) {
571 return check_load_and_stores(filter, flen);
575 EXPORT_SYMBOL(sk_chk_filter);
578 * sk_filter_release_rcu - Release a socket filter by rcu_head
579 * @rcu: rcu_head that contains the sk_filter to free
581 void sk_filter_release_rcu(struct rcu_head *rcu)
583 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
588 EXPORT_SYMBOL(sk_filter_release_rcu);
591 * sk_attach_filter - attach a socket filter
592 * @fprog: the filter program
593 * @sk: the socket to use
595 * Attach the user's filter code. We first run some sanity checks on
596 * it to make sure it does not explode on us later. If an error
597 * occurs or there is insufficient memory for the filter a negative
598 * errno code is returned. On success the return is zero.
600 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
602 struct sk_filter *fp, *old_fp;
603 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
606 /* Make sure new filter is there and in the right amounts. */
607 if (fprog->filter == NULL)
610 fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
613 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
614 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
618 atomic_set(&fp->refcnt, 1);
619 fp->len = fprog->len;
620 fp->bpf_func = sk_run_filter;
622 err = sk_chk_filter(fp->insns, fp->len);
624 sk_filter_uncharge(sk, fp);
630 old_fp = rcu_dereference_protected(sk->sk_filter,
631 sock_owned_by_user(sk));
632 rcu_assign_pointer(sk->sk_filter, fp);
635 sk_filter_uncharge(sk, old_fp);
638 EXPORT_SYMBOL_GPL(sk_attach_filter);
640 int sk_detach_filter(struct sock *sk)
643 struct sk_filter *filter;
645 filter = rcu_dereference_protected(sk->sk_filter,
646 sock_owned_by_user(sk));
648 rcu_assign_pointer(sk->sk_filter, NULL);
649 sk_filter_uncharge(sk, filter);
654 EXPORT_SYMBOL_GPL(sk_detach_filter);