blob: 5dea452792155fffa062a59641c1862e26f19fe0 [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>
David S. Miller86e4ca62011-05-26 15:00:31 -040041#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* No hurry in this branch */
Eric Dumazet4bc65dd2010-12-07 22:26:15 +000044static void *__load_pointer(const struct sk_buff *skb, int k, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 u8 *ptr = NULL;
47
48 if (k >= SKF_NET_OFF)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070049 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 else if (k >= SKF_LL_OFF)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070051 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Eric Dumazet4bc65dd2010-12-07 22:26:15 +000053 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return ptr;
55 return NULL;
56}
57
Eric Dumazet62ab0812010-12-06 20:50:09 +000058static inline void *load_pointer(const struct sk_buff *skb, int k,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090059 unsigned int size, void *buffer)
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070060{
61 if (k >= 0)
62 return skb_header_pointer(skb, k, size, buffer);
Eric Dumazet12b16da2010-12-15 19:45:28 +000063 return __load_pointer(skb, k, size);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070064}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/**
Stephen Hemminger43db6d62008-04-10 01:43:09 -070067 * sk_filter - run a packet through a socket filter
68 * @sk: sock associated with &sk_buff
69 * @skb: buffer to filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -070070 *
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.
76 *
77 */
78int sk_filter(struct sock *sk, struct sk_buff *skb)
79{
80 int err;
81 struct sk_filter *filter;
82
83 err = security_sock_rcv_skb(sk, skb);
84 if (err)
85 return err;
86
Eric Dumazet80f8f102011-01-18 07:46:52 +000087 rcu_read_lock();
88 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -070089 if (filter) {
Eric Dumazet0a148422011-04-20 09:27:32 +000090 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
Eric Dumazet0d7da9d2010-10-25 03:47:05 +000091
Stephen Hemminger43db6d62008-04-10 01:43:09 -070092 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
93 }
Eric Dumazet80f8f102011-01-18 07:46:52 +000094 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -070095
96 return err;
97}
98EXPORT_SYMBOL(sk_filter);
99
100/**
Kris Katterjohn2966b662006-01-23 16:26:16 -0800101 * sk_run_filter - run a filter on a socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * @skb: buffer to run the filter on
Randy Dunlap697d0e32011-01-08 17:41:42 +0000103 * @fentry: filter to apply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *
105 * Decode and apply filter instructions to the skb->data.
Eric Dumazet93aaae22010-11-19 09:49:59 -0800106 * 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Eric Dumazet62ab0812010-12-06 20:50:09 +0000112unsigned int sk_run_filter(const struct sk_buff *skb,
113 const struct sock_filter *fentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700115 void *ptr;
Kris Katterjohn2966b662006-01-23 16:26:16 -0800116 u32 A = 0; /* Accumulator */
117 u32 X = 0; /* Index Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700119 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 /*
123 * Process array of filter instructions.
124 */
Eric Dumazet93aaae22010-11-19 09:49:59 -0800125 for (;; fentry++) {
126#if defined(CONFIG_X86_32)
127#define K (fentry->k)
128#else
129 const u32 K = fentry->k;
130#endif
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 switch (fentry->code) {
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000133 case BPF_S_ALU_ADD_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 A += X;
135 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000136 case BPF_S_ALU_ADD_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800137 A += K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000139 case BPF_S_ALU_SUB_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 A -= X;
141 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000142 case BPF_S_ALU_SUB_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800143 A -= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000145 case BPF_S_ALU_MUL_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 A *= X;
147 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000148 case BPF_S_ALU_MUL_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800149 A *= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000151 case BPF_S_ALU_DIV_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (X == 0)
153 return 0;
154 A /= X;
155 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000156 case BPF_S_ALU_DIV_K:
Eric Dumazetc26aed42010-11-18 22:04:46 +0000157 A = reciprocal_divide(A, K);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000159 case BPF_S_ALU_AND_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 A &= X;
161 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000162 case BPF_S_ALU_AND_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800163 A &= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000165 case BPF_S_ALU_OR_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 A |= X;
167 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000168 case BPF_S_ALU_OR_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800169 A |= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000171 case BPF_S_ALU_LSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 A <<= X;
173 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000174 case BPF_S_ALU_LSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800175 A <<= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000177 case BPF_S_ALU_RSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 A >>= X;
179 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000180 case BPF_S_ALU_RSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800181 A >>= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000183 case BPF_S_ALU_NEG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 A = -A;
185 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000186 case BPF_S_JMP_JA:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800187 fentry += K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000189 case BPF_S_JMP_JGT_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800190 fentry += (A > K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000192 case BPF_S_JMP_JGE_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800193 fentry += (A >= K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000195 case BPF_S_JMP_JEQ_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800196 fentry += (A == K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000198 case BPF_S_JMP_JSET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800199 fentry += (A & K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000201 case BPF_S_JMP_JGT_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800202 fentry += (A > X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000204 case BPF_S_JMP_JGE_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800205 fentry += (A >= X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000207 case BPF_S_JMP_JEQ_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800208 fentry += (A == X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000210 case BPF_S_JMP_JSET_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800211 fentry += (A & X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000213 case BPF_S_LD_W_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800214 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800215load_w:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700216 ptr = load_pointer(skb, k, 4, &tmp);
217 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700218 A = get_unaligned_be32(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700219 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000221 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000222 case BPF_S_LD_H_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800223 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800224load_h:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700225 ptr = load_pointer(skb, k, 2, &tmp);
226 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700227 A = get_unaligned_be16(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700228 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000230 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000231 case BPF_S_LD_B_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800232 k = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233load_b:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700234 ptr = load_pointer(skb, k, 1, &tmp);
235 if (ptr != NULL) {
236 A = *(u8 *)ptr;
237 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000239 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000240 case BPF_S_LD_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700241 A = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000243 case BPF_S_LDX_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700244 X = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000246 case BPF_S_LD_W_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800247 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto load_w;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000249 case BPF_S_LD_H_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800250 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 goto load_h;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000252 case BPF_S_LD_B_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800253 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto load_b;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000255 case BPF_S_LDX_B_MSH:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800256 ptr = load_pointer(skb, K, 1, &tmp);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700257 if (ptr != NULL) {
258 X = (*(u8 *)ptr & 0xf) << 2;
259 continue;
260 }
261 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000262 case BPF_S_LD_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800263 A = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000265 case BPF_S_LDX_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800266 X = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000268 case BPF_S_LD_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000269 A = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000271 case BPF_S_LDX_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000272 X = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000274 case BPF_S_MISC_TAX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 X = A;
276 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000277 case BPF_S_MISC_TXA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 A = X;
279 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000280 case BPF_S_RET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800281 return K;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000282 case BPF_S_RET_A:
Kris Katterjohn4bad4dc2006-01-06 13:08:20 -0800283 return A;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000284 case BPF_S_ST:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800285 mem[K] = A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000287 case BPF_S_STX:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800288 mem[K] = X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000290 case BPF_S_ANC_PROTOCOL:
Al Viro252e3342006-11-14 20:48:11 -0800291 A = ntohs(skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000293 case BPF_S_ANC_PKTTYPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 A = skb->pkt_type;
295 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000296 case BPF_S_ANC_IFINDEX:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000297 if (!skb->dev)
298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 A = skb->dev->ifindex;
300 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000301 case BPF_S_ANC_MARK:
jamal7e75f932009-10-19 02:17:56 +0000302 A = skb->mark;
303 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000304 case BPF_S_ANC_QUEUE:
Eric Dumazetd19742f2009-10-20 01:06:22 -0700305 A = skb->queue_mapping;
306 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000307 case BPF_S_ANC_HATYPE:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000308 if (!skb->dev)
309 return 0;
310 A = skb->dev->type;
311 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000312 case BPF_S_ANC_RXHASH:
Eric Dumazetda2033c2010-11-30 21:45:56 +0000313 A = skb->rxhash;
314 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000315 case BPF_S_ANC_CPU:
Eric Dumazetda2033c2010-11-30 21:45:56 +0000316 A = raw_smp_processor_id();
317 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000318 case BPF_S_ANC_NLATTR: {
Patrick McHardy4738c1d2008-04-10 02:02:28 -0700319 struct nlattr *nla;
320
321 if (skb_is_nonlinear(skb))
322 return 0;
323 if (A > skb->len - sizeof(struct nlattr))
324 return 0;
325
326 nla = nla_find((struct nlattr *)&skb->data[A],
327 skb->len - A, X);
328 if (nla)
329 A = (void *)nla - (void *)skb->data;
330 else
331 A = 0;
332 continue;
333 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000334 case BPF_S_ANC_NLATTR_NEST: {
Pablo Neira Ayusod214c752008-11-20 00:49:27 -0800335 struct nlattr *nla;
336
337 if (skb_is_nonlinear(skb))
338 return 0;
339 if (A > skb->len - sizeof(struct nlattr))
340 return 0;
341
342 nla = (struct nlattr *)&skb->data[A];
343 if (nla->nla_len > A - skb->len)
344 return 0;
345
346 nla = nla_find_nested(nla, X);
347 if (nla)
348 A = (void *)nla - (void *)skb->data;
349 else
350 A = 0;
351 continue;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 default:
Joe Perches6c4a5cb2011-05-21 07:48:40 +0000354 WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n",
355 fentry->code, fentry->jt,
356 fentry->jf, fentry->k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358 }
359 }
360
361 return 0;
362}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700363EXPORT_SYMBOL(sk_run_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000365/*
366 * Security :
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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300372 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000373 */
374static int check_load_and_stores(struct sock_filter *filter, int flen)
375{
376 u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */
377 int pc, ret = 0;
378
379 BUILD_BUG_ON(BPF_MEMWORDS > 16);
380 masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
381 if (!masks)
382 return -ENOMEM;
383 memset(masks, 0xff, flen * sizeof(*masks));
384
385 for (pc = 0; pc < flen; pc++) {
386 memvalid &= masks[pc];
387
388 switch (filter[pc].code) {
389 case BPF_S_ST:
390 case BPF_S_STX:
391 memvalid |= (1 << filter[pc].k);
392 break;
393 case BPF_S_LD_MEM:
394 case BPF_S_LDX_MEM:
395 if (!(memvalid & (1 << filter[pc].k))) {
396 ret = -EINVAL;
397 goto error;
398 }
399 break;
400 case BPF_S_JMP_JA:
401 /* a jump must set masks on target */
402 masks[pc + 1 + filter[pc].k] &= memvalid;
403 memvalid = ~0;
404 break;
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;
416 memvalid = ~0;
417 break;
418 }
419 }
420error:
421 kfree(masks);
422 return ret;
423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/**
426 * sk_chk_filter - verify socket filter code
427 * @filter: filter to verify
428 * @flen: length of filter
429 *
430 * Check the user's filter code. If we let some ugly
431 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800432 * no references or jumps that are out of range, no illegal
433 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800435 * All jumps are forward as they are not signed.
436 *
437 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Dan Carpenter4f25af22011-10-17 21:04:20 +0000439int sk_chk_filter(struct sock_filter *filter, unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Tetsuo Handacba328f2010-11-16 15:19:51 +0000441 /*
442 * Valid instructions are initialized to non-0.
443 * Invalid instructions are initialized to 0.
444 */
445 static const u8 codes[] = {
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000446 [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,
480 [BPF_ST] = BPF_S_ST,
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,
Tetsuo Handacba328f2010-11-16 15:19:51 +0000491 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int pc;
493
David S. Miller1b93ae642005-12-27 13:57:59 -0800494 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return -EINVAL;
496
497 /* check the filter code now */
498 for (pc = 0; pc < flen; pc++) {
Tetsuo Handacba328f2010-11-16 15:19:51 +0000499 struct sock_filter *ftest = &filter[pc];
500 u16 code = ftest->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Tetsuo Handacba328f2010-11-16 15:19:51 +0000502 if (code >= ARRAY_SIZE(codes))
503 return -EINVAL;
504 code = codes[code];
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000505 if (!code)
Tetsuo Handacba328f2010-11-16 15:19:51 +0000506 return -EINVAL;
Kris Katterjohn93699862006-01-04 13:58:36 -0800507 /* Some instructions need special checks */
Tetsuo Handacba328f2010-11-16 15:19:51 +0000508 switch (code) {
509 case BPF_S_ALU_DIV_K:
Kris Katterjohn93699862006-01-04 13:58:36 -0800510 /* check for division by zero */
511 if (ftest->k == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EINVAL;
Eric Dumazetc26aed42010-11-18 22:04:46 +0000513 ftest->k = reciprocal_value(ftest->k);
Kris Katterjohn93699862006-01-04 13:58:36 -0800514 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000515 case BPF_S_LD_MEM:
516 case BPF_S_LDX_MEM:
517 case BPF_S_ST:
518 case BPF_S_STX:
519 /* check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800520 if (ftest->k >= BPF_MEMWORDS)
521 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000522 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000523 case BPF_S_JMP_JA:
Kris Katterjohn93699862006-01-04 13:58:36 -0800524 /*
525 * Note, the large ftest->k might cause loops.
526 * Compare this with conditional jumps below,
527 * where offsets are limited. --ANK (981016)
528 */
529 if (ftest->k >= (unsigned)(flen-pc-1))
530 return -EINVAL;
531 break;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000532 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:
Tetsuo Handacba328f2010-11-16 15:19:51 +0000540 /* for conditionals both must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000541 if (pc + ftest->jt + 1 >= flen ||
542 pc + ftest->jf + 1 >= flen)
543 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000544 break;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000545 case BPF_S_LD_W_ABS:
546 case BPF_S_LD_H_ABS:
547 case BPF_S_LD_B_ABS:
548#define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
549 code = BPF_S_ANC_##CODE; \
550 break
551 switch (ftest->k) {
552 ANCILLARY(PROTOCOL);
553 ANCILLARY(PKTTYPE);
554 ANCILLARY(IFINDEX);
555 ANCILLARY(NLATTR);
556 ANCILLARY(NLATTR_NEST);
557 ANCILLARY(MARK);
558 ANCILLARY(QUEUE);
559 ANCILLARY(HATYPE);
560 ANCILLARY(RXHASH);
561 ANCILLARY(CPU);
562 }
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000563 }
Tetsuo Handacba328f2010-11-16 15:19:51 +0000564 ftest->code = code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000567 /* last instruction must be a RET code */
568 switch (filter[flen - 1].code) {
569 case BPF_S_RET_K:
570 case BPF_S_RET_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000571 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000572 }
573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700575EXPORT_SYMBOL(sk_chk_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800578 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700579 * @rcu: rcu_head that contains the sk_filter to free
580 */
Eric Dumazet46bcf142010-12-06 09:29:43 -0800581void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700582{
583 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
584
Eric Dumazet0a148422011-04-20 09:27:32 +0000585 bpf_jit_free(fp);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800586 kfree(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700587}
Eric Dumazet46bcf142010-12-06 09:29:43 -0800588EXPORT_SYMBOL(sk_filter_release_rcu);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700589
590/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * sk_attach_filter - attach a socket filter
592 * @fprog: the filter program
593 * @sk: the socket to use
594 *
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.
599 */
600int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
601{
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700602 struct sk_filter *fp, *old_fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
604 int err;
605
606 /* Make sure new filter is there and in the right amounts. */
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800607 if (fprog->filter == NULL)
608 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
611 if (!fp)
612 return -ENOMEM;
613 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900614 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -EFAULT;
616 }
617
618 atomic_set(&fp->refcnt, 1);
619 fp->len = fprog->len;
Eric Dumazet0a148422011-04-20 09:27:32 +0000620 fp->bpf_func = sk_run_filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 err = sk_chk_filter(fp->insns, fp->len);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700623 if (err) {
624 sk_filter_uncharge(sk, fp);
625 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
Eric Dumazet0a148422011-04-20 09:27:32 +0000628 bpf_jit_compile(fp);
629
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000630 old_fp = rcu_dereference_protected(sk->sk_filter,
631 sock_owned_by_user(sk));
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700632 rcu_assign_pointer(sk->sk_filter, fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700633
Olof Johansson9b013e02007-10-18 21:48:39 -0700634 if (old_fp)
Eric Dumazet46bcf142010-12-06 09:29:43 -0800635 sk_filter_uncharge(sk, old_fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000638EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700640int sk_detach_filter(struct sock *sk)
641{
642 int ret = -ENOENT;
643 struct sk_filter *filter;
644
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000645 filter = rcu_dereference_protected(sk->sk_filter,
646 sock_owned_by_user(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700647 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000648 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800649 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700650 ret = 0;
651 }
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700652 return ret;
653}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000654EXPORT_SYMBOL_GPL(sk_detach_filter);