blob: 232b1873bb28988069ef1b1642ad8cd7ef689b8f [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>
36#include <asm/system.h>
37#include <asm/uaccess.h>
Dmitry Mishin40daafc2006-04-18 14:50:10 -070038#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/filter.h>
Eric Dumazetc26aed42010-11-18 22:04:46 +000040#include <linux/reciprocal_div.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Changli Gao4c3710a2010-11-16 20:28:24 +000042enum {
Eric Dumazet8c1592d2010-11-18 21:56:38 +000043 BPF_S_RET_K = 1,
Changli Gao4c3710a2010-11-16 20:28:24 +000044 BPF_S_RET_A,
45 BPF_S_ALU_ADD_K,
46 BPF_S_ALU_ADD_X,
47 BPF_S_ALU_SUB_K,
48 BPF_S_ALU_SUB_X,
49 BPF_S_ALU_MUL_K,
50 BPF_S_ALU_MUL_X,
51 BPF_S_ALU_DIV_X,
52 BPF_S_ALU_AND_K,
53 BPF_S_ALU_AND_X,
54 BPF_S_ALU_OR_K,
55 BPF_S_ALU_OR_X,
56 BPF_S_ALU_LSH_K,
57 BPF_S_ALU_LSH_X,
58 BPF_S_ALU_RSH_K,
59 BPF_S_ALU_RSH_X,
60 BPF_S_ALU_NEG,
61 BPF_S_LD_W_ABS,
62 BPF_S_LD_H_ABS,
63 BPF_S_LD_B_ABS,
64 BPF_S_LD_W_LEN,
65 BPF_S_LD_W_IND,
66 BPF_S_LD_H_IND,
67 BPF_S_LD_B_IND,
68 BPF_S_LD_IMM,
69 BPF_S_LDX_W_LEN,
70 BPF_S_LDX_B_MSH,
71 BPF_S_LDX_IMM,
72 BPF_S_MISC_TAX,
73 BPF_S_MISC_TXA,
74 BPF_S_ALU_DIV_K,
75 BPF_S_LD_MEM,
76 BPF_S_LDX_MEM,
77 BPF_S_ST,
78 BPF_S_STX,
79 BPF_S_JMP_JA,
80 BPF_S_JMP_JEQ_K,
81 BPF_S_JMP_JEQ_X,
82 BPF_S_JMP_JGE_K,
83 BPF_S_JMP_JGE_X,
84 BPF_S_JMP_JGT_K,
85 BPF_S_JMP_JGT_X,
86 BPF_S_JMP_JSET_K,
87 BPF_S_JMP_JSET_X,
Eric Dumazet12b16da2010-12-15 19:45:28 +000088 /* Ancillary data */
89 BPF_S_ANC_PROTOCOL,
90 BPF_S_ANC_PKTTYPE,
91 BPF_S_ANC_IFINDEX,
92 BPF_S_ANC_NLATTR,
93 BPF_S_ANC_NLATTR_NEST,
94 BPF_S_ANC_MARK,
95 BPF_S_ANC_QUEUE,
96 BPF_S_ANC_HATYPE,
97 BPF_S_ANC_RXHASH,
98 BPF_S_ANC_CPU,
Changli Gao4c3710a2010-11-16 20:28:24 +000099};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* No hurry in this branch */
Eric Dumazet4bc65dd2010-12-07 22:26:15 +0000102static void *__load_pointer(const struct sk_buff *skb, int k, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 u8 *ptr = NULL;
105
106 if (k >= SKF_NET_OFF)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700107 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 else if (k >= SKF_LL_OFF)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700109 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Eric Dumazet4bc65dd2010-12-07 22:26:15 +0000111 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return ptr;
113 return NULL;
114}
115
Eric Dumazet62ab0812010-12-06 20:50:09 +0000116static inline void *load_pointer(const struct sk_buff *skb, int k,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900117 unsigned int size, void *buffer)
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700118{
119 if (k >= 0)
120 return skb_header_pointer(skb, k, size, buffer);
Eric Dumazet12b16da2010-12-15 19:45:28 +0000121 return __load_pointer(skb, k, size);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/**
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700125 * sk_filter - run a packet through a socket filter
126 * @sk: sock associated with &sk_buff
127 * @skb: buffer to filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700128 *
129 * Run the filter code and then cut skb->data to correct size returned by
130 * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
131 * than pkt_len we keep whole skb->data. This is the socket level
132 * wrapper to sk_run_filter. It returns 0 if the packet should
133 * be accepted or -EPERM if the packet should be tossed.
134 *
135 */
136int sk_filter(struct sock *sk, struct sk_buff *skb)
137{
138 int err;
139 struct sk_filter *filter;
140
141 err = security_sock_rcv_skb(sk, skb);
142 if (err)
143 return err;
144
Eric Dumazet80f8f102011-01-18 07:46:52 +0000145 rcu_read_lock();
146 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700147 if (filter) {
Eric Dumazet93aaae22010-11-19 09:49:59 -0800148 unsigned int pkt_len = sk_run_filter(skb, filter->insns);
Eric Dumazet0d7da9d2010-10-25 03:47:05 +0000149
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700150 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
151 }
Eric Dumazet80f8f102011-01-18 07:46:52 +0000152 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700153
154 return err;
155}
156EXPORT_SYMBOL(sk_filter);
157
158/**
Kris Katterjohn2966b662006-01-23 16:26:16 -0800159 * sk_run_filter - run a filter on a socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * @skb: buffer to run the filter on
Randy Dunlap697d0e32011-01-08 17:41:42 +0000161 * @fentry: filter to apply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
163 * Decode and apply filter instructions to the skb->data.
Eric Dumazet93aaae22010-11-19 09:49:59 -0800164 * Return length to keep, 0 for none. @skb is the data we are
165 * filtering, @filter is the array of filter instructions.
166 * Because all jumps are guaranteed to be before last instruction,
167 * and last instruction guaranteed to be a RET, we dont need to check
168 * flen. (We used to pass to this function the length of filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Eric Dumazet62ab0812010-12-06 20:50:09 +0000170unsigned int sk_run_filter(const struct sk_buff *skb,
171 const struct sock_filter *fentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700173 void *ptr;
Kris Katterjohn2966b662006-01-23 16:26:16 -0800174 u32 A = 0; /* Accumulator */
175 u32 X = 0; /* Index Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700177 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /*
181 * Process array of filter instructions.
182 */
Eric Dumazet93aaae22010-11-19 09:49:59 -0800183 for (;; fentry++) {
184#if defined(CONFIG_X86_32)
185#define K (fentry->k)
186#else
187 const u32 K = fentry->k;
188#endif
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 switch (fentry->code) {
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000191 case BPF_S_ALU_ADD_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 A += X;
193 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000194 case BPF_S_ALU_ADD_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800195 A += K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000197 case BPF_S_ALU_SUB_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 A -= X;
199 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000200 case BPF_S_ALU_SUB_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800201 A -= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000203 case BPF_S_ALU_MUL_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 A *= X;
205 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000206 case BPF_S_ALU_MUL_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800207 A *= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000209 case BPF_S_ALU_DIV_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (X == 0)
211 return 0;
212 A /= X;
213 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000214 case BPF_S_ALU_DIV_K:
Eric Dumazetc26aed42010-11-18 22:04:46 +0000215 A = reciprocal_divide(A, K);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000217 case BPF_S_ALU_AND_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 A &= X;
219 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000220 case BPF_S_ALU_AND_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800221 A &= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000223 case BPF_S_ALU_OR_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 A |= X;
225 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000226 case BPF_S_ALU_OR_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800227 A |= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000229 case BPF_S_ALU_LSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 A <<= X;
231 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000232 case BPF_S_ALU_LSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800233 A <<= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000235 case BPF_S_ALU_RSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 A >>= X;
237 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000238 case BPF_S_ALU_RSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800239 A >>= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000241 case BPF_S_ALU_NEG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 A = -A;
243 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000244 case BPF_S_JMP_JA:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800245 fentry += K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000247 case BPF_S_JMP_JGT_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800248 fentry += (A > K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000250 case BPF_S_JMP_JGE_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800251 fentry += (A >= K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000253 case BPF_S_JMP_JEQ_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800254 fentry += (A == K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000256 case BPF_S_JMP_JSET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800257 fentry += (A & K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000259 case BPF_S_JMP_JGT_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800260 fentry += (A > X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000262 case BPF_S_JMP_JGE_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800263 fentry += (A >= X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000265 case BPF_S_JMP_JEQ_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800266 fentry += (A == X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000268 case BPF_S_JMP_JSET_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800269 fentry += (A & X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000271 case BPF_S_LD_W_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800272 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800273load_w:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700274 ptr = load_pointer(skb, k, 4, &tmp);
275 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700276 A = get_unaligned_be32(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700277 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000279 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000280 case BPF_S_LD_H_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800281 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800282load_h:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700283 ptr = load_pointer(skb, k, 2, &tmp);
284 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700285 A = get_unaligned_be16(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700286 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000288 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000289 case BPF_S_LD_B_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800290 k = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291load_b:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700292 ptr = load_pointer(skb, k, 1, &tmp);
293 if (ptr != NULL) {
294 A = *(u8 *)ptr;
295 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000297 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000298 case BPF_S_LD_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700299 A = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000301 case BPF_S_LDX_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700302 X = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000304 case BPF_S_LD_W_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800305 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 goto load_w;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000307 case BPF_S_LD_H_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800308 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto load_h;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000310 case BPF_S_LD_B_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800311 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto load_b;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000313 case BPF_S_LDX_B_MSH:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800314 ptr = load_pointer(skb, K, 1, &tmp);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700315 if (ptr != NULL) {
316 X = (*(u8 *)ptr & 0xf) << 2;
317 continue;
318 }
319 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000320 case BPF_S_LD_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800321 A = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000323 case BPF_S_LDX_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800324 X = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000326 case BPF_S_LD_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000327 A = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000329 case BPF_S_LDX_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000330 X = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000332 case BPF_S_MISC_TAX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 X = A;
334 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000335 case BPF_S_MISC_TXA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 A = X;
337 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000338 case BPF_S_RET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800339 return K;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000340 case BPF_S_RET_A:
Kris Katterjohn4bad4dc2006-01-06 13:08:20 -0800341 return A;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000342 case BPF_S_ST:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800343 mem[K] = A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000345 case BPF_S_STX:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800346 mem[K] = X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000348 case BPF_S_ANC_PROTOCOL:
Al Viro252e3342006-11-14 20:48:11 -0800349 A = ntohs(skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000351 case BPF_S_ANC_PKTTYPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 A = skb->pkt_type;
353 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000354 case BPF_S_ANC_IFINDEX:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000355 if (!skb->dev)
356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 A = skb->dev->ifindex;
358 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000359 case BPF_S_ANC_MARK:
jamal7e75f932009-10-19 02:17:56 +0000360 A = skb->mark;
361 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000362 case BPF_S_ANC_QUEUE:
Eric Dumazetd19742f2009-10-20 01:06:22 -0700363 A = skb->queue_mapping;
364 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000365 case BPF_S_ANC_HATYPE:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000366 if (!skb->dev)
367 return 0;
368 A = skb->dev->type;
369 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000370 case BPF_S_ANC_RXHASH:
Eric Dumazetda2033c2010-11-30 21:45:56 +0000371 A = skb->rxhash;
372 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000373 case BPF_S_ANC_CPU:
Eric Dumazetda2033c2010-11-30 21:45:56 +0000374 A = raw_smp_processor_id();
375 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000376 case BPF_S_ANC_NLATTR: {
Patrick McHardy4738c1d2008-04-10 02:02:28 -0700377 struct nlattr *nla;
378
379 if (skb_is_nonlinear(skb))
380 return 0;
381 if (A > skb->len - sizeof(struct nlattr))
382 return 0;
383
384 nla = nla_find((struct nlattr *)&skb->data[A],
385 skb->len - A, X);
386 if (nla)
387 A = (void *)nla - (void *)skb->data;
388 else
389 A = 0;
390 continue;
391 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000392 case BPF_S_ANC_NLATTR_NEST: {
Pablo Neira Ayusod214c752008-11-20 00:49:27 -0800393 struct nlattr *nla;
394
395 if (skb_is_nonlinear(skb))
396 return 0;
397 if (A > skb->len - sizeof(struct nlattr))
398 return 0;
399
400 nla = (struct nlattr *)&skb->data[A];
401 if (nla->nla_len > A - skb->len)
402 return 0;
403
404 nla = nla_find_nested(nla, X);
405 if (nla)
406 A = (void *)nla - (void *)skb->data;
407 else
408 A = 0;
409 continue;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 default:
Eric Dumazet12b16da2010-12-15 19:45:28 +0000412 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 0;
414 }
415 }
416
417 return 0;
418}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700419EXPORT_SYMBOL(sk_run_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000421/*
422 * Security :
423 * A BPF program is able to use 16 cells of memory to store intermediate
424 * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter())
425 * As we dont want to clear mem[] array for each packet going through
426 * sk_run_filter(), we check that filter loaded by user never try to read
427 * a cell if not previously written, and we check all branches to be sure
428 * a malicious user doesnt try to abuse us.
429 */
430static int check_load_and_stores(struct sock_filter *filter, int flen)
431{
432 u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */
433 int pc, ret = 0;
434
435 BUILD_BUG_ON(BPF_MEMWORDS > 16);
436 masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
437 if (!masks)
438 return -ENOMEM;
439 memset(masks, 0xff, flen * sizeof(*masks));
440
441 for (pc = 0; pc < flen; pc++) {
442 memvalid &= masks[pc];
443
444 switch (filter[pc].code) {
445 case BPF_S_ST:
446 case BPF_S_STX:
447 memvalid |= (1 << filter[pc].k);
448 break;
449 case BPF_S_LD_MEM:
450 case BPF_S_LDX_MEM:
451 if (!(memvalid & (1 << filter[pc].k))) {
452 ret = -EINVAL;
453 goto error;
454 }
455 break;
456 case BPF_S_JMP_JA:
457 /* a jump must set masks on target */
458 masks[pc + 1 + filter[pc].k] &= memvalid;
459 memvalid = ~0;
460 break;
461 case BPF_S_JMP_JEQ_K:
462 case BPF_S_JMP_JEQ_X:
463 case BPF_S_JMP_JGE_K:
464 case BPF_S_JMP_JGE_X:
465 case BPF_S_JMP_JGT_K:
466 case BPF_S_JMP_JGT_X:
467 case BPF_S_JMP_JSET_X:
468 case BPF_S_JMP_JSET_K:
469 /* a jump must set masks on targets */
470 masks[pc + 1 + filter[pc].jt] &= memvalid;
471 masks[pc + 1 + filter[pc].jf] &= memvalid;
472 memvalid = ~0;
473 break;
474 }
475 }
476error:
477 kfree(masks);
478 return ret;
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/**
482 * sk_chk_filter - verify socket filter code
483 * @filter: filter to verify
484 * @flen: length of filter
485 *
486 * Check the user's filter code. If we let some ugly
487 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800488 * no references or jumps that are out of range, no illegal
489 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800491 * All jumps are forward as they are not signed.
492 *
493 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
495int sk_chk_filter(struct sock_filter *filter, int flen)
496{
Tetsuo Handacba328f2010-11-16 15:19:51 +0000497 /*
498 * Valid instructions are initialized to non-0.
499 * Invalid instructions are initialized to 0.
500 */
501 static const u8 codes[] = {
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000502 [BPF_ALU|BPF_ADD|BPF_K] = BPF_S_ALU_ADD_K,
503 [BPF_ALU|BPF_ADD|BPF_X] = BPF_S_ALU_ADD_X,
504 [BPF_ALU|BPF_SUB|BPF_K] = BPF_S_ALU_SUB_K,
505 [BPF_ALU|BPF_SUB|BPF_X] = BPF_S_ALU_SUB_X,
506 [BPF_ALU|BPF_MUL|BPF_K] = BPF_S_ALU_MUL_K,
507 [BPF_ALU|BPF_MUL|BPF_X] = BPF_S_ALU_MUL_X,
508 [BPF_ALU|BPF_DIV|BPF_X] = BPF_S_ALU_DIV_X,
509 [BPF_ALU|BPF_AND|BPF_K] = BPF_S_ALU_AND_K,
510 [BPF_ALU|BPF_AND|BPF_X] = BPF_S_ALU_AND_X,
511 [BPF_ALU|BPF_OR|BPF_K] = BPF_S_ALU_OR_K,
512 [BPF_ALU|BPF_OR|BPF_X] = BPF_S_ALU_OR_X,
513 [BPF_ALU|BPF_LSH|BPF_K] = BPF_S_ALU_LSH_K,
514 [BPF_ALU|BPF_LSH|BPF_X] = BPF_S_ALU_LSH_X,
515 [BPF_ALU|BPF_RSH|BPF_K] = BPF_S_ALU_RSH_K,
516 [BPF_ALU|BPF_RSH|BPF_X] = BPF_S_ALU_RSH_X,
517 [BPF_ALU|BPF_NEG] = BPF_S_ALU_NEG,
518 [BPF_LD|BPF_W|BPF_ABS] = BPF_S_LD_W_ABS,
519 [BPF_LD|BPF_H|BPF_ABS] = BPF_S_LD_H_ABS,
520 [BPF_LD|BPF_B|BPF_ABS] = BPF_S_LD_B_ABS,
521 [BPF_LD|BPF_W|BPF_LEN] = BPF_S_LD_W_LEN,
522 [BPF_LD|BPF_W|BPF_IND] = BPF_S_LD_W_IND,
523 [BPF_LD|BPF_H|BPF_IND] = BPF_S_LD_H_IND,
524 [BPF_LD|BPF_B|BPF_IND] = BPF_S_LD_B_IND,
525 [BPF_LD|BPF_IMM] = BPF_S_LD_IMM,
526 [BPF_LDX|BPF_W|BPF_LEN] = BPF_S_LDX_W_LEN,
527 [BPF_LDX|BPF_B|BPF_MSH] = BPF_S_LDX_B_MSH,
528 [BPF_LDX|BPF_IMM] = BPF_S_LDX_IMM,
529 [BPF_MISC|BPF_TAX] = BPF_S_MISC_TAX,
530 [BPF_MISC|BPF_TXA] = BPF_S_MISC_TXA,
531 [BPF_RET|BPF_K] = BPF_S_RET_K,
532 [BPF_RET|BPF_A] = BPF_S_RET_A,
533 [BPF_ALU|BPF_DIV|BPF_K] = BPF_S_ALU_DIV_K,
534 [BPF_LD|BPF_MEM] = BPF_S_LD_MEM,
535 [BPF_LDX|BPF_MEM] = BPF_S_LDX_MEM,
536 [BPF_ST] = BPF_S_ST,
537 [BPF_STX] = BPF_S_STX,
538 [BPF_JMP|BPF_JA] = BPF_S_JMP_JA,
539 [BPF_JMP|BPF_JEQ|BPF_K] = BPF_S_JMP_JEQ_K,
540 [BPF_JMP|BPF_JEQ|BPF_X] = BPF_S_JMP_JEQ_X,
541 [BPF_JMP|BPF_JGE|BPF_K] = BPF_S_JMP_JGE_K,
542 [BPF_JMP|BPF_JGE|BPF_X] = BPF_S_JMP_JGE_X,
543 [BPF_JMP|BPF_JGT|BPF_K] = BPF_S_JMP_JGT_K,
544 [BPF_JMP|BPF_JGT|BPF_X] = BPF_S_JMP_JGT_X,
545 [BPF_JMP|BPF_JSET|BPF_K] = BPF_S_JMP_JSET_K,
546 [BPF_JMP|BPF_JSET|BPF_X] = BPF_S_JMP_JSET_X,
Tetsuo Handacba328f2010-11-16 15:19:51 +0000547 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int pc;
549
David S. Miller1b93ae642005-12-27 13:57:59 -0800550 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return -EINVAL;
552
553 /* check the filter code now */
554 for (pc = 0; pc < flen; pc++) {
Tetsuo Handacba328f2010-11-16 15:19:51 +0000555 struct sock_filter *ftest = &filter[pc];
556 u16 code = ftest->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Tetsuo Handacba328f2010-11-16 15:19:51 +0000558 if (code >= ARRAY_SIZE(codes))
559 return -EINVAL;
560 code = codes[code];
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000561 if (!code)
Tetsuo Handacba328f2010-11-16 15:19:51 +0000562 return -EINVAL;
Kris Katterjohn93699862006-01-04 13:58:36 -0800563 /* Some instructions need special checks */
Tetsuo Handacba328f2010-11-16 15:19:51 +0000564 switch (code) {
565 case BPF_S_ALU_DIV_K:
Kris Katterjohn93699862006-01-04 13:58:36 -0800566 /* check for division by zero */
567 if (ftest->k == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -EINVAL;
Eric Dumazetc26aed42010-11-18 22:04:46 +0000569 ftest->k = reciprocal_value(ftest->k);
Kris Katterjohn93699862006-01-04 13:58:36 -0800570 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000571 case BPF_S_LD_MEM:
572 case BPF_S_LDX_MEM:
573 case BPF_S_ST:
574 case BPF_S_STX:
575 /* check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800576 if (ftest->k >= BPF_MEMWORDS)
577 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000578 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000579 case BPF_S_JMP_JA:
Kris Katterjohn93699862006-01-04 13:58:36 -0800580 /*
581 * Note, the large ftest->k might cause loops.
582 * Compare this with conditional jumps below,
583 * where offsets are limited. --ANK (981016)
584 */
585 if (ftest->k >= (unsigned)(flen-pc-1))
586 return -EINVAL;
587 break;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000588 case BPF_S_JMP_JEQ_K:
589 case BPF_S_JMP_JEQ_X:
590 case BPF_S_JMP_JGE_K:
591 case BPF_S_JMP_JGE_X:
592 case BPF_S_JMP_JGT_K:
593 case BPF_S_JMP_JGT_X:
594 case BPF_S_JMP_JSET_X:
595 case BPF_S_JMP_JSET_K:
Tetsuo Handacba328f2010-11-16 15:19:51 +0000596 /* for conditionals both must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000597 if (pc + ftest->jt + 1 >= flen ||
598 pc + ftest->jf + 1 >= flen)
599 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000600 break;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000601 case BPF_S_LD_W_ABS:
602 case BPF_S_LD_H_ABS:
603 case BPF_S_LD_B_ABS:
604#define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
605 code = BPF_S_ANC_##CODE; \
606 break
607 switch (ftest->k) {
608 ANCILLARY(PROTOCOL);
609 ANCILLARY(PKTTYPE);
610 ANCILLARY(IFINDEX);
611 ANCILLARY(NLATTR);
612 ANCILLARY(NLATTR_NEST);
613 ANCILLARY(MARK);
614 ANCILLARY(QUEUE);
615 ANCILLARY(HATYPE);
616 ANCILLARY(RXHASH);
617 ANCILLARY(CPU);
618 }
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000619 }
Tetsuo Handacba328f2010-11-16 15:19:51 +0000620 ftest->code = code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000623 /* last instruction must be a RET code */
624 switch (filter[flen - 1].code) {
625 case BPF_S_RET_K:
626 case BPF_S_RET_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000627 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000628 }
629 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700631EXPORT_SYMBOL(sk_chk_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800634 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700635 * @rcu: rcu_head that contains the sk_filter to free
636 */
Eric Dumazet46bcf142010-12-06 09:29:43 -0800637void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700638{
639 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
640
Eric Dumazet46bcf142010-12-06 09:29:43 -0800641 kfree(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700642}
Eric Dumazet46bcf142010-12-06 09:29:43 -0800643EXPORT_SYMBOL(sk_filter_release_rcu);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700644
645/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 * sk_attach_filter - attach a socket filter
647 * @fprog: the filter program
648 * @sk: the socket to use
649 *
650 * Attach the user's filter code. We first run some sanity checks on
651 * it to make sure it does not explode on us later. If an error
652 * occurs or there is insufficient memory for the filter a negative
653 * errno code is returned. On success the return is zero.
654 */
655int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
656{
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700657 struct sk_filter *fp, *old_fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
659 int err;
660
661 /* Make sure new filter is there and in the right amounts. */
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800662 if (fprog->filter == NULL)
663 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
666 if (!fp)
667 return -ENOMEM;
668 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900669 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -EFAULT;
671 }
672
673 atomic_set(&fp->refcnt, 1);
674 fp->len = fprog->len;
675
676 err = sk_chk_filter(fp->insns, fp->len);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700677 if (err) {
678 sk_filter_uncharge(sk, fp);
679 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000682 old_fp = rcu_dereference_protected(sk->sk_filter,
683 sock_owned_by_user(sk));
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700684 rcu_assign_pointer(sk->sk_filter, fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700685
Olof Johansson9b013e02007-10-18 21:48:39 -0700686 if (old_fp)
Eric Dumazet46bcf142010-12-06 09:29:43 -0800687 sk_filter_uncharge(sk, old_fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000690EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700692int sk_detach_filter(struct sock *sk)
693{
694 int ret = -ENOENT;
695 struct sk_filter *filter;
696
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000697 filter = rcu_dereference_protected(sk->sk_filter,
698 sock_owned_by_user(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700699 if (filter) {
700 rcu_assign_pointer(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800701 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700702 ret = 0;
703 }
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700704 return ret;
705}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000706EXPORT_SYMBOL_GPL(sk_detach_filter);