blob: 6f755cca45206934444464da8b8bcb0289921717 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
4 * Author:
5 * Jay Schulist <jschlst@samba.org>
6 *
7 * Based on the design of:
8 * - The Berkeley Packet Filter
9 *
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.
14 *
15 * Andi Kleen - Fix a few bad bugs and races.
Kris Katterjohn93699862006-01-04 13:58:36 -080016 * Kris Katterjohn - Added many additional checks in sk_chk_filter()
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/module.h>
20#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/fcntl.h>
23#include <linux/socket.h>
24#include <linux/in.h>
25#include <linux/inet.h>
26#include <linux/netdevice.h>
27#include <linux/if_packet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/ip.h>
30#include <net/protocol.h>
Patrick McHardy4738c1d2008-04-10 02:02:28 -070031#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/skbuff.h>
33#include <net/sock.h>
34#include <linux/errno.h>
35#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
Dmitry Mishin40daafc2006-04-18 14:50:10 -070037#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/filter.h>
Eric Dumazetc26aed42010-11-18 22:04:46 +000039#include <linux/reciprocal_div.h>
David S. Miller86e4ca62011-05-26 15:00:31 -040040#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jan Seiffertf03fb3f2012-03-30 05:08:19 +000042/* No hurry in this branch
43 *
44 * Exported for the bpf jit load helper.
45 */
46void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 u8 *ptr = NULL;
49
50 if (k >= SKF_NET_OFF)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070051 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 else if (k >= SKF_LL_OFF)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070053 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Eric Dumazet4bc65dd2010-12-07 22:26:15 +000055 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return ptr;
57 return NULL;
58}
59
Eric Dumazet62ab0812010-12-06 20:50:09 +000060static inline void *load_pointer(const struct sk_buff *skb, int k,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090061 unsigned int size, void *buffer)
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070062{
63 if (k >= 0)
64 return skb_header_pointer(skb, k, size, buffer);
Jan Seiffertf03fb3f2012-03-30 05:08:19 +000065 return bpf_internal_load_pointer_neg_helper(skb, k, size);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070066}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/**
Stephen Hemminger43db6d62008-04-10 01:43:09 -070069 * sk_filter - run a packet through a socket filter
70 * @sk: sock associated with &sk_buff
71 * @skb: buffer to filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -070072 *
73 * Run the filter code and then cut skb->data to correct size returned by
74 * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
75 * than pkt_len we keep whole skb->data. This is the socket level
76 * wrapper to sk_run_filter. It returns 0 if the packet should
77 * be accepted or -EPERM if the packet should be tossed.
78 *
79 */
80int sk_filter(struct sock *sk, struct sk_buff *skb)
81{
82 int err;
83 struct sk_filter *filter;
84
85 err = security_sock_rcv_skb(sk, skb);
86 if (err)
87 return err;
88
Eric Dumazet80f8f102011-01-18 07:46:52 +000089 rcu_read_lock();
90 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -070091 if (filter) {
Eric Dumazet0a148422011-04-20 09:27:32 +000092 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
Eric Dumazet0d7da9d2010-10-25 03:47:05 +000093
Stephen Hemminger43db6d62008-04-10 01:43:09 -070094 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
95 }
Eric Dumazet80f8f102011-01-18 07:46:52 +000096 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -070097
98 return err;
99}
100EXPORT_SYMBOL(sk_filter);
101
102/**
Kris Katterjohn2966b662006-01-23 16:26:16 -0800103 * sk_run_filter - run a filter on a socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * @skb: buffer to run the filter on
Randy Dunlap697d0e32011-01-08 17:41:42 +0000105 * @fentry: filter to apply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
107 * Decode and apply filter instructions to the skb->data.
Eric Dumazet93aaae22010-11-19 09:49:59 -0800108 * Return length to keep, 0 for none. @skb is the data we are
109 * filtering, @filter is the array of filter instructions.
110 * Because all jumps are guaranteed to be before last instruction,
111 * and last instruction guaranteed to be a RET, we dont need to check
112 * flen. (We used to pass to this function the length of filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
Eric Dumazet62ab0812010-12-06 20:50:09 +0000114unsigned int sk_run_filter(const struct sk_buff *skb,
115 const struct sock_filter *fentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700117 void *ptr;
Kris Katterjohn2966b662006-01-23 16:26:16 -0800118 u32 A = 0; /* Accumulator */
119 u32 X = 0; /* Index Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700121 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /*
125 * Process array of filter instructions.
126 */
Eric Dumazet93aaae22010-11-19 09:49:59 -0800127 for (;; fentry++) {
128#if defined(CONFIG_X86_32)
129#define K (fentry->k)
130#else
131 const u32 K = fentry->k;
132#endif
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 switch (fentry->code) {
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000135 case BPF_S_ALU_ADD_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 A += X;
137 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000138 case BPF_S_ALU_ADD_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800139 A += K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000141 case BPF_S_ALU_SUB_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 A -= X;
143 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000144 case BPF_S_ALU_SUB_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800145 A -= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000147 case BPF_S_ALU_MUL_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 A *= X;
149 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000150 case BPF_S_ALU_MUL_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800151 A *= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000153 case BPF_S_ALU_DIV_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (X == 0)
155 return 0;
156 A /= X;
157 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000158 case BPF_S_ALU_DIV_K:
Eric Dumazetc26aed42010-11-18 22:04:46 +0000159 A = reciprocal_divide(A, K);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000161 case BPF_S_ALU_AND_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 A &= X;
163 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000164 case BPF_S_ALU_AND_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800165 A &= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000167 case BPF_S_ALU_OR_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 A |= X;
169 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000170 case BPF_S_ALU_OR_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800171 A |= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000173 case BPF_S_ALU_LSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 A <<= X;
175 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000176 case BPF_S_ALU_LSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800177 A <<= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000179 case BPF_S_ALU_RSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 A >>= X;
181 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000182 case BPF_S_ALU_RSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800183 A >>= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000185 case BPF_S_ALU_NEG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 A = -A;
187 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000188 case BPF_S_JMP_JA:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800189 fentry += K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000191 case BPF_S_JMP_JGT_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800192 fentry += (A > K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000194 case BPF_S_JMP_JGE_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800195 fentry += (A >= K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000197 case BPF_S_JMP_JEQ_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800198 fentry += (A == K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000200 case BPF_S_JMP_JSET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800201 fentry += (A & K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000203 case BPF_S_JMP_JGT_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800204 fentry += (A > X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000206 case BPF_S_JMP_JGE_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800207 fentry += (A >= X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000209 case BPF_S_JMP_JEQ_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800210 fentry += (A == X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000212 case BPF_S_JMP_JSET_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800213 fentry += (A & X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000215 case BPF_S_LD_W_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800216 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800217load_w:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700218 ptr = load_pointer(skb, k, 4, &tmp);
219 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700220 A = get_unaligned_be32(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700221 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000223 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000224 case BPF_S_LD_H_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800225 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800226load_h:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700227 ptr = load_pointer(skb, k, 2, &tmp);
228 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700229 A = get_unaligned_be16(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700230 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000232 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000233 case BPF_S_LD_B_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800234 k = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235load_b:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700236 ptr = load_pointer(skb, k, 1, &tmp);
237 if (ptr != NULL) {
238 A = *(u8 *)ptr;
239 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000241 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000242 case BPF_S_LD_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700243 A = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000245 case BPF_S_LDX_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700246 X = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000248 case BPF_S_LD_W_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800249 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto load_w;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000251 case BPF_S_LD_H_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800252 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto load_h;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000254 case BPF_S_LD_B_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800255 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 goto load_b;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000257 case BPF_S_LDX_B_MSH:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800258 ptr = load_pointer(skb, K, 1, &tmp);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700259 if (ptr != NULL) {
260 X = (*(u8 *)ptr & 0xf) << 2;
261 continue;
262 }
263 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000264 case BPF_S_LD_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800265 A = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000267 case BPF_S_LDX_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800268 X = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000270 case BPF_S_LD_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000271 A = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000273 case BPF_S_LDX_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000274 X = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000276 case BPF_S_MISC_TAX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 X = A;
278 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000279 case BPF_S_MISC_TXA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 A = X;
281 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000282 case BPF_S_RET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800283 return K;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000284 case BPF_S_RET_A:
Kris Katterjohn4bad4dc2006-01-06 13:08:20 -0800285 return A;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000286 case BPF_S_ST:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800287 mem[K] = A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000289 case BPF_S_STX:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800290 mem[K] = X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000292 case BPF_S_ANC_PROTOCOL:
Al Viro252e3342006-11-14 20:48:11 -0800293 A = ntohs(skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000295 case BPF_S_ANC_PKTTYPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 A = skb->pkt_type;
297 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000298 case BPF_S_ANC_IFINDEX:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000299 if (!skb->dev)
300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 A = skb->dev->ifindex;
302 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000303 case BPF_S_ANC_MARK:
jamal7e75f932009-10-19 02:17:56 +0000304 A = skb->mark;
305 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000306 case BPF_S_ANC_QUEUE:
Eric Dumazetd19742f2009-10-20 01:06:22 -0700307 A = skb->queue_mapping;
308 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000309 case BPF_S_ANC_HATYPE:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000310 if (!skb->dev)
311 return 0;
312 A = skb->dev->type;
313 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000314 case BPF_S_ANC_RXHASH:
Eric Dumazetda2033c2010-11-30 21:45:56 +0000315 A = skb->rxhash;
316 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000317 case BPF_S_ANC_CPU:
Eric Dumazetda2033c2010-11-30 21:45:56 +0000318 A = raw_smp_processor_id();
319 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000320 case BPF_S_ANC_NLATTR: {
Patrick McHardy4738c1d2008-04-10 02:02:28 -0700321 struct nlattr *nla;
322
323 if (skb_is_nonlinear(skb))
324 return 0;
325 if (A > skb->len - sizeof(struct nlattr))
326 return 0;
327
328 nla = nla_find((struct nlattr *)&skb->data[A],
329 skb->len - A, X);
330 if (nla)
331 A = (void *)nla - (void *)skb->data;
332 else
333 A = 0;
334 continue;
335 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000336 case BPF_S_ANC_NLATTR_NEST: {
Pablo Neira Ayusod214c752008-11-20 00:49:27 -0800337 struct nlattr *nla;
338
339 if (skb_is_nonlinear(skb))
340 return 0;
341 if (A > skb->len - sizeof(struct nlattr))
342 return 0;
343
344 nla = (struct nlattr *)&skb->data[A];
345 if (nla->nla_len > A - skb->len)
346 return 0;
347
348 nla = nla_find_nested(nla, X);
349 if (nla)
350 A = (void *)nla - (void *)skb->data;
351 else
352 A = 0;
353 continue;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 default:
Joe Perches6c4a5cb2011-05-21 07:48:40 +0000356 WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n",
357 fentry->code, fentry->jt,
358 fentry->jf, fentry->k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360 }
361 }
362
363 return 0;
364}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700365EXPORT_SYMBOL(sk_run_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000367/*
368 * Security :
369 * A BPF program is able to use 16 cells of memory to store intermediate
370 * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter())
371 * As we dont want to clear mem[] array for each packet going through
372 * sk_run_filter(), we check that filter loaded by user never try to read
373 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300374 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000375 */
376static int check_load_and_stores(struct sock_filter *filter, int flen)
377{
378 u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */
379 int pc, ret = 0;
380
381 BUILD_BUG_ON(BPF_MEMWORDS > 16);
382 masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
383 if (!masks)
384 return -ENOMEM;
385 memset(masks, 0xff, flen * sizeof(*masks));
386
387 for (pc = 0; pc < flen; pc++) {
388 memvalid &= masks[pc];
389
390 switch (filter[pc].code) {
391 case BPF_S_ST:
392 case BPF_S_STX:
393 memvalid |= (1 << filter[pc].k);
394 break;
395 case BPF_S_LD_MEM:
396 case BPF_S_LDX_MEM:
397 if (!(memvalid & (1 << filter[pc].k))) {
398 ret = -EINVAL;
399 goto error;
400 }
401 break;
402 case BPF_S_JMP_JA:
403 /* a jump must set masks on target */
404 masks[pc + 1 + filter[pc].k] &= memvalid;
405 memvalid = ~0;
406 break;
407 case BPF_S_JMP_JEQ_K:
408 case BPF_S_JMP_JEQ_X:
409 case BPF_S_JMP_JGE_K:
410 case BPF_S_JMP_JGE_X:
411 case BPF_S_JMP_JGT_K:
412 case BPF_S_JMP_JGT_X:
413 case BPF_S_JMP_JSET_X:
414 case BPF_S_JMP_JSET_K:
415 /* a jump must set masks on targets */
416 masks[pc + 1 + filter[pc].jt] &= memvalid;
417 masks[pc + 1 + filter[pc].jf] &= memvalid;
418 memvalid = ~0;
419 break;
420 }
421 }
422error:
423 kfree(masks);
424 return ret;
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
428 * sk_chk_filter - verify socket filter code
429 * @filter: filter to verify
430 * @flen: length of filter
431 *
432 * Check the user's filter code. If we let some ugly
433 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800434 * no references or jumps that are out of range, no illegal
435 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800437 * All jumps are forward as they are not signed.
438 *
439 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
Dan Carpenter4f25af22011-10-17 21:04:20 +0000441int sk_chk_filter(struct sock_filter *filter, unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Tetsuo Handacba328f2010-11-16 15:19:51 +0000443 /*
444 * Valid instructions are initialized to non-0.
445 * Invalid instructions are initialized to 0.
446 */
447 static const u8 codes[] = {
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000448 [BPF_ALU|BPF_ADD|BPF_K] = BPF_S_ALU_ADD_K,
449 [BPF_ALU|BPF_ADD|BPF_X] = BPF_S_ALU_ADD_X,
450 [BPF_ALU|BPF_SUB|BPF_K] = BPF_S_ALU_SUB_K,
451 [BPF_ALU|BPF_SUB|BPF_X] = BPF_S_ALU_SUB_X,
452 [BPF_ALU|BPF_MUL|BPF_K] = BPF_S_ALU_MUL_K,
453 [BPF_ALU|BPF_MUL|BPF_X] = BPF_S_ALU_MUL_X,
454 [BPF_ALU|BPF_DIV|BPF_X] = BPF_S_ALU_DIV_X,
455 [BPF_ALU|BPF_AND|BPF_K] = BPF_S_ALU_AND_K,
456 [BPF_ALU|BPF_AND|BPF_X] = BPF_S_ALU_AND_X,
457 [BPF_ALU|BPF_OR|BPF_K] = BPF_S_ALU_OR_K,
458 [BPF_ALU|BPF_OR|BPF_X] = BPF_S_ALU_OR_X,
459 [BPF_ALU|BPF_LSH|BPF_K] = BPF_S_ALU_LSH_K,
460 [BPF_ALU|BPF_LSH|BPF_X] = BPF_S_ALU_LSH_X,
461 [BPF_ALU|BPF_RSH|BPF_K] = BPF_S_ALU_RSH_K,
462 [BPF_ALU|BPF_RSH|BPF_X] = BPF_S_ALU_RSH_X,
463 [BPF_ALU|BPF_NEG] = BPF_S_ALU_NEG,
464 [BPF_LD|BPF_W|BPF_ABS] = BPF_S_LD_W_ABS,
465 [BPF_LD|BPF_H|BPF_ABS] = BPF_S_LD_H_ABS,
466 [BPF_LD|BPF_B|BPF_ABS] = BPF_S_LD_B_ABS,
467 [BPF_LD|BPF_W|BPF_LEN] = BPF_S_LD_W_LEN,
468 [BPF_LD|BPF_W|BPF_IND] = BPF_S_LD_W_IND,
469 [BPF_LD|BPF_H|BPF_IND] = BPF_S_LD_H_IND,
470 [BPF_LD|BPF_B|BPF_IND] = BPF_S_LD_B_IND,
471 [BPF_LD|BPF_IMM] = BPF_S_LD_IMM,
472 [BPF_LDX|BPF_W|BPF_LEN] = BPF_S_LDX_W_LEN,
473 [BPF_LDX|BPF_B|BPF_MSH] = BPF_S_LDX_B_MSH,
474 [BPF_LDX|BPF_IMM] = BPF_S_LDX_IMM,
475 [BPF_MISC|BPF_TAX] = BPF_S_MISC_TAX,
476 [BPF_MISC|BPF_TXA] = BPF_S_MISC_TXA,
477 [BPF_RET|BPF_K] = BPF_S_RET_K,
478 [BPF_RET|BPF_A] = BPF_S_RET_A,
479 [BPF_ALU|BPF_DIV|BPF_K] = BPF_S_ALU_DIV_K,
480 [BPF_LD|BPF_MEM] = BPF_S_LD_MEM,
481 [BPF_LDX|BPF_MEM] = BPF_S_LDX_MEM,
482 [BPF_ST] = BPF_S_ST,
483 [BPF_STX] = BPF_S_STX,
484 [BPF_JMP|BPF_JA] = BPF_S_JMP_JA,
485 [BPF_JMP|BPF_JEQ|BPF_K] = BPF_S_JMP_JEQ_K,
486 [BPF_JMP|BPF_JEQ|BPF_X] = BPF_S_JMP_JEQ_X,
487 [BPF_JMP|BPF_JGE|BPF_K] = BPF_S_JMP_JGE_K,
488 [BPF_JMP|BPF_JGE|BPF_X] = BPF_S_JMP_JGE_X,
489 [BPF_JMP|BPF_JGT|BPF_K] = BPF_S_JMP_JGT_K,
490 [BPF_JMP|BPF_JGT|BPF_X] = BPF_S_JMP_JGT_X,
491 [BPF_JMP|BPF_JSET|BPF_K] = BPF_S_JMP_JSET_K,
492 [BPF_JMP|BPF_JSET|BPF_X] = BPF_S_JMP_JSET_X,
Tetsuo Handacba328f2010-11-16 15:19:51 +0000493 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int pc;
495
David S. Miller1b93ae642005-12-27 13:57:59 -0800496 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return -EINVAL;
498
499 /* check the filter code now */
500 for (pc = 0; pc < flen; pc++) {
Tetsuo Handacba328f2010-11-16 15:19:51 +0000501 struct sock_filter *ftest = &filter[pc];
502 u16 code = ftest->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Tetsuo Handacba328f2010-11-16 15:19:51 +0000504 if (code >= ARRAY_SIZE(codes))
505 return -EINVAL;
506 code = codes[code];
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000507 if (!code)
Tetsuo Handacba328f2010-11-16 15:19:51 +0000508 return -EINVAL;
Kris Katterjohn93699862006-01-04 13:58:36 -0800509 /* Some instructions need special checks */
Tetsuo Handacba328f2010-11-16 15:19:51 +0000510 switch (code) {
511 case BPF_S_ALU_DIV_K:
Kris Katterjohn93699862006-01-04 13:58:36 -0800512 /* check for division by zero */
513 if (ftest->k == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -EINVAL;
Eric Dumazetc26aed42010-11-18 22:04:46 +0000515 ftest->k = reciprocal_value(ftest->k);
Kris Katterjohn93699862006-01-04 13:58:36 -0800516 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000517 case BPF_S_LD_MEM:
518 case BPF_S_LDX_MEM:
519 case BPF_S_ST:
520 case BPF_S_STX:
521 /* check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800522 if (ftest->k >= BPF_MEMWORDS)
523 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000524 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000525 case BPF_S_JMP_JA:
Kris Katterjohn93699862006-01-04 13:58:36 -0800526 /*
527 * Note, the large ftest->k might cause loops.
528 * Compare this with conditional jumps below,
529 * where offsets are limited. --ANK (981016)
530 */
531 if (ftest->k >= (unsigned)(flen-pc-1))
532 return -EINVAL;
533 break;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000534 case BPF_S_JMP_JEQ_K:
535 case BPF_S_JMP_JEQ_X:
536 case BPF_S_JMP_JGE_K:
537 case BPF_S_JMP_JGE_X:
538 case BPF_S_JMP_JGT_K:
539 case BPF_S_JMP_JGT_X:
540 case BPF_S_JMP_JSET_X:
541 case BPF_S_JMP_JSET_K:
Tetsuo Handacba328f2010-11-16 15:19:51 +0000542 /* for conditionals both must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000543 if (pc + ftest->jt + 1 >= flen ||
544 pc + ftest->jf + 1 >= flen)
545 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000546 break;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000547 case BPF_S_LD_W_ABS:
548 case BPF_S_LD_H_ABS:
549 case BPF_S_LD_B_ABS:
550#define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
551 code = BPF_S_ANC_##CODE; \
552 break
553 switch (ftest->k) {
554 ANCILLARY(PROTOCOL);
555 ANCILLARY(PKTTYPE);
556 ANCILLARY(IFINDEX);
557 ANCILLARY(NLATTR);
558 ANCILLARY(NLATTR_NEST);
559 ANCILLARY(MARK);
560 ANCILLARY(QUEUE);
561 ANCILLARY(HATYPE);
562 ANCILLARY(RXHASH);
563 ANCILLARY(CPU);
564 }
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000565 }
Tetsuo Handacba328f2010-11-16 15:19:51 +0000566 ftest->code = code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000569 /* last instruction must be a RET code */
570 switch (filter[flen - 1].code) {
571 case BPF_S_RET_K:
572 case BPF_S_RET_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000573 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000574 }
575 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700577EXPORT_SYMBOL(sk_chk_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800580 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700581 * @rcu: rcu_head that contains the sk_filter to free
582 */
Eric Dumazet46bcf142010-12-06 09:29:43 -0800583void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700584{
585 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
586
Eric Dumazet0a148422011-04-20 09:27:32 +0000587 bpf_jit_free(fp);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800588 kfree(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700589}
Eric Dumazet46bcf142010-12-06 09:29:43 -0800590EXPORT_SYMBOL(sk_filter_release_rcu);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700591
592/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * sk_attach_filter - attach a socket filter
594 * @fprog: the filter program
595 * @sk: the socket to use
596 *
597 * Attach the user's filter code. We first run some sanity checks on
598 * it to make sure it does not explode on us later. If an error
599 * occurs or there is insufficient memory for the filter a negative
600 * errno code is returned. On success the return is zero.
601 */
602int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
603{
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700604 struct sk_filter *fp, *old_fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
606 int err;
607
608 /* Make sure new filter is there and in the right amounts. */
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800609 if (fprog->filter == NULL)
610 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
613 if (!fp)
614 return -ENOMEM;
615 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900616 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -EFAULT;
618 }
619
620 atomic_set(&fp->refcnt, 1);
621 fp->len = fprog->len;
Eric Dumazet0a148422011-04-20 09:27:32 +0000622 fp->bpf_func = sk_run_filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 err = sk_chk_filter(fp->insns, fp->len);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700625 if (err) {
626 sk_filter_uncharge(sk, fp);
627 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
Eric Dumazet0a148422011-04-20 09:27:32 +0000630 bpf_jit_compile(fp);
631
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000632 old_fp = rcu_dereference_protected(sk->sk_filter,
633 sock_owned_by_user(sk));
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700634 rcu_assign_pointer(sk->sk_filter, fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700635
Olof Johansson9b013e02007-10-18 21:48:39 -0700636 if (old_fp)
Eric Dumazet46bcf142010-12-06 09:29:43 -0800637 sk_filter_uncharge(sk, old_fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000640EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700642int sk_detach_filter(struct sock *sk)
643{
644 int ret = -ENOENT;
645 struct sk_filter *filter;
646
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000647 filter = rcu_dereference_protected(sk->sk_filter,
648 sock_owned_by_user(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700649 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000650 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800651 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700652 ret = 0;
653 }
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700654 return ret;
655}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000656EXPORT_SYMBOL_GPL(sk_detach_filter);