blob: 6438f29ff26650b240be40d7d80953dc28f13cb0 [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>
Will Drewry46b325c2012-04-12 16:47:52 -050041#include <linux/seccomp.h>
Eric Dumazetf3335032012-10-27 02:26:17 +000042#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jan Seiffertf03fb3f2012-03-30 05:08:19 +000044/* No hurry in this branch
45 *
46 * Exported for the bpf jit load helper.
47 */
48void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 u8 *ptr = NULL;
51
52 if (k >= SKF_NET_OFF)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070053 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 else if (k >= SKF_LL_OFF)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070055 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Eric Dumazet4bc65dd2010-12-07 22:26:15 +000057 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return ptr;
59 return NULL;
60}
61
Eric Dumazet62ab0812010-12-06 20:50:09 +000062static inline void *load_pointer(const struct sk_buff *skb, int k,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090063 unsigned int size, void *buffer)
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070064{
65 if (k >= 0)
66 return skb_header_pointer(skb, k, size, buffer);
Jan Seiffertf03fb3f2012-03-30 05:08:19 +000067 return bpf_internal_load_pointer_neg_helper(skb, k, size);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070068}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/**
Stephen Hemminger43db6d62008-04-10 01:43:09 -070071 * sk_filter - run a packet through a socket filter
72 * @sk: sock associated with &sk_buff
73 * @skb: buffer to filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -070074 *
75 * Run the filter code and then cut skb->data to correct size returned by
76 * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
77 * than pkt_len we keep whole skb->data. This is the socket level
78 * wrapper to sk_run_filter. It returns 0 if the packet should
79 * be accepted or -EPERM if the packet should be tossed.
80 *
81 */
82int sk_filter(struct sock *sk, struct sk_buff *skb)
83{
84 int err;
85 struct sk_filter *filter;
86
Mel Gormanc93bdd02012-07-31 16:44:19 -070087 /*
88 * If the skb was allocated from pfmemalloc reserves, only
89 * allow SOCK_MEMALLOC sockets to use it as this socket is
90 * helping free memory
91 */
92 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
93 return -ENOMEM;
94
Stephen Hemminger43db6d62008-04-10 01:43:09 -070095 err = security_sock_rcv_skb(sk, skb);
96 if (err)
97 return err;
98
Eric Dumazet80f8f102011-01-18 07:46:52 +000099 rcu_read_lock();
100 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700101 if (filter) {
Eric Dumazet0a148422011-04-20 09:27:32 +0000102 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
Eric Dumazet0d7da9d2010-10-25 03:47:05 +0000103
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700104 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
105 }
Eric Dumazet80f8f102011-01-18 07:46:52 +0000106 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700107
108 return err;
109}
110EXPORT_SYMBOL(sk_filter);
111
112/**
Kris Katterjohn2966b662006-01-23 16:26:16 -0800113 * sk_run_filter - run a filter on a socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * @skb: buffer to run the filter on
Randy Dunlap697d0e32011-01-08 17:41:42 +0000115 * @fentry: filter to apply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *
117 * Decode and apply filter instructions to the skb->data.
Eric Dumazet93aaae22010-11-19 09:49:59 -0800118 * Return length to keep, 0 for none. @skb is the data we are
119 * filtering, @filter is the array of filter instructions.
120 * Because all jumps are guaranteed to be before last instruction,
121 * and last instruction guaranteed to be a RET, we dont need to check
122 * flen. (We used to pass to this function the length of filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
Eric Dumazet62ab0812010-12-06 20:50:09 +0000124unsigned int sk_run_filter(const struct sk_buff *skb,
125 const struct sock_filter *fentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700127 void *ptr;
Kris Katterjohn2966b662006-01-23 16:26:16 -0800128 u32 A = 0; /* Accumulator */
129 u32 X = 0; /* Index Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700131 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /*
135 * Process array of filter instructions.
136 */
Eric Dumazet93aaae22010-11-19 09:49:59 -0800137 for (;; fentry++) {
138#if defined(CONFIG_X86_32)
139#define K (fentry->k)
140#else
141 const u32 K = fentry->k;
142#endif
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 switch (fentry->code) {
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000145 case BPF_S_ALU_ADD_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_ADD_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_SUB_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 A -= X;
153 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000154 case BPF_S_ALU_SUB_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800155 A -= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000157 case BPF_S_ALU_MUL_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 A *= X;
159 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000160 case BPF_S_ALU_MUL_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800161 A *= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000163 case BPF_S_ALU_DIV_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (X == 0)
165 return 0;
166 A /= X;
167 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000168 case BPF_S_ALU_DIV_K:
Eric Dumazetc26aed42010-11-18 22:04:46 +0000169 A = reciprocal_divide(A, K);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 continue;
Eric Dumazetb6069a92012-09-07 22:03:35 +0000171 case BPF_S_ALU_MOD_X:
172 if (X == 0)
173 return 0;
174 A %= X;
175 continue;
176 case BPF_S_ALU_MOD_K:
177 A %= K;
178 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000179 case BPF_S_ALU_AND_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_AND_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_OR_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 A |= X;
187 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000188 case BPF_S_ALU_OR_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800189 A |= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 continue;
Daniel Borkmann9e49e882012-09-24 02:23:59 +0000191 case BPF_S_ANC_ALU_XOR_X:
192 case BPF_S_ALU_XOR_X:
193 A ^= X;
194 continue;
195 case BPF_S_ALU_XOR_K:
196 A ^= K;
197 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000198 case BPF_S_ALU_LSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 A <<= X;
200 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000201 case BPF_S_ALU_LSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800202 A <<= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000204 case BPF_S_ALU_RSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 A >>= X;
206 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000207 case BPF_S_ALU_RSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800208 A >>= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000210 case BPF_S_ALU_NEG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 A = -A;
212 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000213 case BPF_S_JMP_JA:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800214 fentry += K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000216 case BPF_S_JMP_JGT_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800217 fentry += (A > K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000219 case BPF_S_JMP_JGE_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800220 fentry += (A >= K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000222 case BPF_S_JMP_JEQ_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800223 fentry += (A == K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000225 case BPF_S_JMP_JSET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800226 fentry += (A & K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000228 case BPF_S_JMP_JGT_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800229 fentry += (A > X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000231 case BPF_S_JMP_JGE_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800232 fentry += (A >= X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000234 case BPF_S_JMP_JEQ_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800235 fentry += (A == X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000237 case BPF_S_JMP_JSET_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800238 fentry += (A & X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000240 case BPF_S_LD_W_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800241 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800242load_w:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700243 ptr = load_pointer(skb, k, 4, &tmp);
244 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700245 A = get_unaligned_be32(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700246 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000248 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000249 case BPF_S_LD_H_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800250 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800251load_h:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700252 ptr = load_pointer(skb, k, 2, &tmp);
253 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700254 A = get_unaligned_be16(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700255 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000257 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000258 case BPF_S_LD_B_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800259 k = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260load_b:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700261 ptr = load_pointer(skb, k, 1, &tmp);
262 if (ptr != NULL) {
263 A = *(u8 *)ptr;
264 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000266 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000267 case BPF_S_LD_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700268 A = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000270 case BPF_S_LDX_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700271 X = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000273 case BPF_S_LD_W_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800274 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto load_w;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000276 case BPF_S_LD_H_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800277 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto load_h;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000279 case BPF_S_LD_B_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800280 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 goto load_b;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000282 case BPF_S_LDX_B_MSH:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800283 ptr = load_pointer(skb, K, 1, &tmp);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700284 if (ptr != NULL) {
285 X = (*(u8 *)ptr & 0xf) << 2;
286 continue;
287 }
288 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000289 case BPF_S_LD_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800290 A = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000292 case BPF_S_LDX_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800293 X = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000295 case BPF_S_LD_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000296 A = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000298 case BPF_S_LDX_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000299 X = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000301 case BPF_S_MISC_TAX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 X = A;
303 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000304 case BPF_S_MISC_TXA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 A = X;
306 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000307 case BPF_S_RET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800308 return K;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000309 case BPF_S_RET_A:
Kris Katterjohn4bad4dc2006-01-06 13:08:20 -0800310 return A;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000311 case BPF_S_ST:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800312 mem[K] = A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000314 case BPF_S_STX:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800315 mem[K] = X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000317 case BPF_S_ANC_PROTOCOL:
Al Viro252e3342006-11-14 20:48:11 -0800318 A = ntohs(skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000320 case BPF_S_ANC_PKTTYPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 A = skb->pkt_type;
322 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000323 case BPF_S_ANC_IFINDEX:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000324 if (!skb->dev)
325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 A = skb->dev->ifindex;
327 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000328 case BPF_S_ANC_MARK:
jamal7e75f932009-10-19 02:17:56 +0000329 A = skb->mark;
330 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000331 case BPF_S_ANC_QUEUE:
Eric Dumazetd19742f2009-10-20 01:06:22 -0700332 A = skb->queue_mapping;
333 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000334 case BPF_S_ANC_HATYPE:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000335 if (!skb->dev)
336 return 0;
337 A = skb->dev->type;
338 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000339 case BPF_S_ANC_RXHASH:
Eric Dumazetda2033c2010-11-30 21:45:56 +0000340 A = skb->rxhash;
341 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000342 case BPF_S_ANC_CPU:
Eric Dumazetda2033c2010-11-30 21:45:56 +0000343 A = raw_smp_processor_id();
344 continue;
Eric Dumazetf3335032012-10-27 02:26:17 +0000345 case BPF_S_ANC_VLAN_TAG:
346 A = vlan_tx_tag_get(skb);
347 continue;
348 case BPF_S_ANC_VLAN_TAG_PRESENT:
349 A = !!vlan_tx_tag_present(skb);
350 continue;
Daniel Borkmann3e5289d2013-03-19 06:39:31 +0000351 case BPF_S_ANC_PAY_OFFSET:
352 A = __skb_get_poff(skb);
353 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000354 case BPF_S_ANC_NLATTR: {
Patrick McHardy4738c1d2008-04-10 02:02:28 -0700355 struct nlattr *nla;
356
357 if (skb_is_nonlinear(skb))
358 return 0;
359 if (A > skb->len - sizeof(struct nlattr))
360 return 0;
361
362 nla = nla_find((struct nlattr *)&skb->data[A],
363 skb->len - A, X);
364 if (nla)
365 A = (void *)nla - (void *)skb->data;
366 else
367 A = 0;
368 continue;
369 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000370 case BPF_S_ANC_NLATTR_NEST: {
Pablo Neira Ayusod214c752008-11-20 00:49:27 -0800371 struct nlattr *nla;
372
373 if (skb_is_nonlinear(skb))
374 return 0;
375 if (A > skb->len - sizeof(struct nlattr))
376 return 0;
377
378 nla = (struct nlattr *)&skb->data[A];
379 if (nla->nla_len > A - skb->len)
380 return 0;
381
382 nla = nla_find_nested(nla, X);
383 if (nla)
384 A = (void *)nla - (void *)skb->data;
385 else
386 A = 0;
387 continue;
388 }
Will Drewry46b325c2012-04-12 16:47:52 -0500389#ifdef CONFIG_SECCOMP_FILTER
390 case BPF_S_ANC_SECCOMP_LD_W:
391 A = seccomp_bpf_load(fentry->k);
392 continue;
393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 default:
Joe Perches6c4a5cb2011-05-21 07:48:40 +0000395 WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n",
396 fentry->code, fentry->jt,
397 fentry->jf, fentry->k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399 }
400 }
401
402 return 0;
403}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700404EXPORT_SYMBOL(sk_run_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000406/*
407 * Security :
408 * A BPF program is able to use 16 cells of memory to store intermediate
409 * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter())
410 * As we dont want to clear mem[] array for each packet going through
411 * sk_run_filter(), we check that filter loaded by user never try to read
412 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300413 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000414 */
415static int check_load_and_stores(struct sock_filter *filter, int flen)
416{
417 u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */
418 int pc, ret = 0;
419
420 BUILD_BUG_ON(BPF_MEMWORDS > 16);
421 masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
422 if (!masks)
423 return -ENOMEM;
424 memset(masks, 0xff, flen * sizeof(*masks));
425
426 for (pc = 0; pc < flen; pc++) {
427 memvalid &= masks[pc];
428
429 switch (filter[pc].code) {
430 case BPF_S_ST:
431 case BPF_S_STX:
432 memvalid |= (1 << filter[pc].k);
433 break;
434 case BPF_S_LD_MEM:
435 case BPF_S_LDX_MEM:
436 if (!(memvalid & (1 << filter[pc].k))) {
437 ret = -EINVAL;
438 goto error;
439 }
440 break;
441 case BPF_S_JMP_JA:
442 /* a jump must set masks on target */
443 masks[pc + 1 + filter[pc].k] &= memvalid;
444 memvalid = ~0;
445 break;
446 case BPF_S_JMP_JEQ_K:
447 case BPF_S_JMP_JEQ_X:
448 case BPF_S_JMP_JGE_K:
449 case BPF_S_JMP_JGE_X:
450 case BPF_S_JMP_JGT_K:
451 case BPF_S_JMP_JGT_X:
452 case BPF_S_JMP_JSET_X:
453 case BPF_S_JMP_JSET_K:
454 /* a jump must set masks on targets */
455 masks[pc + 1 + filter[pc].jt] &= memvalid;
456 masks[pc + 1 + filter[pc].jf] &= memvalid;
457 memvalid = ~0;
458 break;
459 }
460 }
461error:
462 kfree(masks);
463 return ret;
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/**
467 * sk_chk_filter - verify socket filter code
468 * @filter: filter to verify
469 * @flen: length of filter
470 *
471 * Check the user's filter code. If we let some ugly
472 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800473 * no references or jumps that are out of range, no illegal
474 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800476 * All jumps are forward as they are not signed.
477 *
478 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
Dan Carpenter4f25af22011-10-17 21:04:20 +0000480int sk_chk_filter(struct sock_filter *filter, unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Tetsuo Handacba328f2010-11-16 15:19:51 +0000482 /*
483 * Valid instructions are initialized to non-0.
484 * Invalid instructions are initialized to 0.
485 */
486 static const u8 codes[] = {
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000487 [BPF_ALU|BPF_ADD|BPF_K] = BPF_S_ALU_ADD_K,
488 [BPF_ALU|BPF_ADD|BPF_X] = BPF_S_ALU_ADD_X,
489 [BPF_ALU|BPF_SUB|BPF_K] = BPF_S_ALU_SUB_K,
490 [BPF_ALU|BPF_SUB|BPF_X] = BPF_S_ALU_SUB_X,
491 [BPF_ALU|BPF_MUL|BPF_K] = BPF_S_ALU_MUL_K,
492 [BPF_ALU|BPF_MUL|BPF_X] = BPF_S_ALU_MUL_X,
493 [BPF_ALU|BPF_DIV|BPF_X] = BPF_S_ALU_DIV_X,
Eric Dumazetb6069a92012-09-07 22:03:35 +0000494 [BPF_ALU|BPF_MOD|BPF_K] = BPF_S_ALU_MOD_K,
495 [BPF_ALU|BPF_MOD|BPF_X] = BPF_S_ALU_MOD_X,
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000496 [BPF_ALU|BPF_AND|BPF_K] = BPF_S_ALU_AND_K,
497 [BPF_ALU|BPF_AND|BPF_X] = BPF_S_ALU_AND_X,
498 [BPF_ALU|BPF_OR|BPF_K] = BPF_S_ALU_OR_K,
499 [BPF_ALU|BPF_OR|BPF_X] = BPF_S_ALU_OR_X,
Daniel Borkmann9e49e882012-09-24 02:23:59 +0000500 [BPF_ALU|BPF_XOR|BPF_K] = BPF_S_ALU_XOR_K,
501 [BPF_ALU|BPF_XOR|BPF_X] = BPF_S_ALU_XOR_X,
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000502 [BPF_ALU|BPF_LSH|BPF_K] = BPF_S_ALU_LSH_K,
503 [BPF_ALU|BPF_LSH|BPF_X] = BPF_S_ALU_LSH_X,
504 [BPF_ALU|BPF_RSH|BPF_K] = BPF_S_ALU_RSH_K,
505 [BPF_ALU|BPF_RSH|BPF_X] = BPF_S_ALU_RSH_X,
506 [BPF_ALU|BPF_NEG] = BPF_S_ALU_NEG,
507 [BPF_LD|BPF_W|BPF_ABS] = BPF_S_LD_W_ABS,
508 [BPF_LD|BPF_H|BPF_ABS] = BPF_S_LD_H_ABS,
509 [BPF_LD|BPF_B|BPF_ABS] = BPF_S_LD_B_ABS,
510 [BPF_LD|BPF_W|BPF_LEN] = BPF_S_LD_W_LEN,
511 [BPF_LD|BPF_W|BPF_IND] = BPF_S_LD_W_IND,
512 [BPF_LD|BPF_H|BPF_IND] = BPF_S_LD_H_IND,
513 [BPF_LD|BPF_B|BPF_IND] = BPF_S_LD_B_IND,
514 [BPF_LD|BPF_IMM] = BPF_S_LD_IMM,
515 [BPF_LDX|BPF_W|BPF_LEN] = BPF_S_LDX_W_LEN,
516 [BPF_LDX|BPF_B|BPF_MSH] = BPF_S_LDX_B_MSH,
517 [BPF_LDX|BPF_IMM] = BPF_S_LDX_IMM,
518 [BPF_MISC|BPF_TAX] = BPF_S_MISC_TAX,
519 [BPF_MISC|BPF_TXA] = BPF_S_MISC_TXA,
520 [BPF_RET|BPF_K] = BPF_S_RET_K,
521 [BPF_RET|BPF_A] = BPF_S_RET_A,
522 [BPF_ALU|BPF_DIV|BPF_K] = BPF_S_ALU_DIV_K,
523 [BPF_LD|BPF_MEM] = BPF_S_LD_MEM,
524 [BPF_LDX|BPF_MEM] = BPF_S_LDX_MEM,
525 [BPF_ST] = BPF_S_ST,
526 [BPF_STX] = BPF_S_STX,
527 [BPF_JMP|BPF_JA] = BPF_S_JMP_JA,
528 [BPF_JMP|BPF_JEQ|BPF_K] = BPF_S_JMP_JEQ_K,
529 [BPF_JMP|BPF_JEQ|BPF_X] = BPF_S_JMP_JEQ_X,
530 [BPF_JMP|BPF_JGE|BPF_K] = BPF_S_JMP_JGE_K,
531 [BPF_JMP|BPF_JGE|BPF_X] = BPF_S_JMP_JGE_X,
532 [BPF_JMP|BPF_JGT|BPF_K] = BPF_S_JMP_JGT_K,
533 [BPF_JMP|BPF_JGT|BPF_X] = BPF_S_JMP_JGT_X,
534 [BPF_JMP|BPF_JSET|BPF_K] = BPF_S_JMP_JSET_K,
535 [BPF_JMP|BPF_JSET|BPF_X] = BPF_S_JMP_JSET_X,
Tetsuo Handacba328f2010-11-16 15:19:51 +0000536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int pc;
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000538 bool anc_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
David S. Miller1b93ae642005-12-27 13:57:59 -0800540 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -EINVAL;
542
543 /* check the filter code now */
544 for (pc = 0; pc < flen; pc++) {
Tetsuo Handacba328f2010-11-16 15:19:51 +0000545 struct sock_filter *ftest = &filter[pc];
546 u16 code = ftest->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Tetsuo Handacba328f2010-11-16 15:19:51 +0000548 if (code >= ARRAY_SIZE(codes))
549 return -EINVAL;
550 code = codes[code];
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000551 if (!code)
Tetsuo Handacba328f2010-11-16 15:19:51 +0000552 return -EINVAL;
Kris Katterjohn93699862006-01-04 13:58:36 -0800553 /* Some instructions need special checks */
Tetsuo Handacba328f2010-11-16 15:19:51 +0000554 switch (code) {
555 case BPF_S_ALU_DIV_K:
Kris Katterjohn93699862006-01-04 13:58:36 -0800556 /* check for division by zero */
557 if (ftest->k == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EINVAL;
Eric Dumazetc26aed42010-11-18 22:04:46 +0000559 ftest->k = reciprocal_value(ftest->k);
Kris Katterjohn93699862006-01-04 13:58:36 -0800560 break;
Eric Dumazetb6069a92012-09-07 22:03:35 +0000561 case BPF_S_ALU_MOD_K:
562 /* check for division by zero */
563 if (ftest->k == 0)
564 return -EINVAL;
565 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000566 case BPF_S_LD_MEM:
567 case BPF_S_LDX_MEM:
568 case BPF_S_ST:
569 case BPF_S_STX:
570 /* check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800571 if (ftest->k >= BPF_MEMWORDS)
572 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000573 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000574 case BPF_S_JMP_JA:
Kris Katterjohn93699862006-01-04 13:58:36 -0800575 /*
576 * Note, the large ftest->k might cause loops.
577 * Compare this with conditional jumps below,
578 * where offsets are limited. --ANK (981016)
579 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000580 if (ftest->k >= (unsigned int)(flen-pc-1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800581 return -EINVAL;
582 break;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000583 case BPF_S_JMP_JEQ_K:
584 case BPF_S_JMP_JEQ_X:
585 case BPF_S_JMP_JGE_K:
586 case BPF_S_JMP_JGE_X:
587 case BPF_S_JMP_JGT_K:
588 case BPF_S_JMP_JGT_X:
589 case BPF_S_JMP_JSET_X:
590 case BPF_S_JMP_JSET_K:
Tetsuo Handacba328f2010-11-16 15:19:51 +0000591 /* for conditionals both must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000592 if (pc + ftest->jt + 1 >= flen ||
593 pc + ftest->jf + 1 >= flen)
594 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000595 break;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000596 case BPF_S_LD_W_ABS:
597 case BPF_S_LD_H_ABS:
598 case BPF_S_LD_B_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000599 anc_found = false;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000600#define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
601 code = BPF_S_ANC_##CODE; \
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000602 anc_found = true; \
Eric Dumazet12b16da2010-12-15 19:45:28 +0000603 break
604 switch (ftest->k) {
605 ANCILLARY(PROTOCOL);
606 ANCILLARY(PKTTYPE);
607 ANCILLARY(IFINDEX);
608 ANCILLARY(NLATTR);
609 ANCILLARY(NLATTR_NEST);
610 ANCILLARY(MARK);
611 ANCILLARY(QUEUE);
612 ANCILLARY(HATYPE);
613 ANCILLARY(RXHASH);
614 ANCILLARY(CPU);
Jiri Pirkoffe06c12012-03-31 11:01:20 +0000615 ANCILLARY(ALU_XOR_X);
Eric Dumazetf3335032012-10-27 02:26:17 +0000616 ANCILLARY(VLAN_TAG);
617 ANCILLARY(VLAN_TAG_PRESENT);
Daniel Borkmann3e5289d2013-03-19 06:39:31 +0000618 ANCILLARY(PAY_OFFSET);
Eric Dumazet12b16da2010-12-15 19:45:28 +0000619 }
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000620
621 /* ancillary operation unknown or unsupported */
622 if (anc_found == false && ftest->k >= SKF_AD_OFF)
623 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000624 }
Tetsuo Handacba328f2010-11-16 15:19:51 +0000625 ftest->code = code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000628 /* last instruction must be a RET code */
629 switch (filter[flen - 1].code) {
630 case BPF_S_RET_K:
631 case BPF_S_RET_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000632 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000633 }
634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700636EXPORT_SYMBOL(sk_chk_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800639 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700640 * @rcu: rcu_head that contains the sk_filter to free
641 */
Eric Dumazet46bcf142010-12-06 09:29:43 -0800642void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700643{
644 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
645
Eric Dumazet0a148422011-04-20 09:27:32 +0000646 bpf_jit_free(fp);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800647 kfree(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700648}
Eric Dumazet46bcf142010-12-06 09:29:43 -0800649EXPORT_SYMBOL(sk_filter_release_rcu);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700650
Jiri Pirko302d6632012-03-31 11:01:19 +0000651static int __sk_prepare_filter(struct sk_filter *fp)
652{
653 int err;
654
655 fp->bpf_func = sk_run_filter;
656
657 err = sk_chk_filter(fp->insns, fp->len);
658 if (err)
659 return err;
660
661 bpf_jit_compile(fp);
662 return 0;
663}
664
665/**
666 * sk_unattached_filter_create - create an unattached filter
667 * @fprog: the filter program
Randy Dunlapc6c4b972012-06-08 14:01:44 +0000668 * @pfp: the unattached filter that is created
Jiri Pirko302d6632012-03-31 11:01:19 +0000669 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +0000670 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +0000671 * sanity checks on it to make sure it does not explode on us later.
672 * If an error occurs or there is insufficient memory for the filter
673 * a negative errno code is returned. On success the return is zero.
674 */
675int sk_unattached_filter_create(struct sk_filter **pfp,
676 struct sock_fprog *fprog)
677{
678 struct sk_filter *fp;
679 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
680 int err;
681
682 /* Make sure new filter is there and in the right amounts. */
683 if (fprog->filter == NULL)
684 return -EINVAL;
685
686 fp = kmalloc(fsize + sizeof(*fp), GFP_KERNEL);
687 if (!fp)
688 return -ENOMEM;
689 memcpy(fp->insns, fprog->filter, fsize);
690
691 atomic_set(&fp->refcnt, 1);
692 fp->len = fprog->len;
693
694 err = __sk_prepare_filter(fp);
695 if (err)
696 goto free_mem;
697
698 *pfp = fp;
699 return 0;
700free_mem:
701 kfree(fp);
702 return err;
703}
704EXPORT_SYMBOL_GPL(sk_unattached_filter_create);
705
706void sk_unattached_filter_destroy(struct sk_filter *fp)
707{
708 sk_filter_release(fp);
709}
710EXPORT_SYMBOL_GPL(sk_unattached_filter_destroy);
711
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700712/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * sk_attach_filter - attach a socket filter
714 * @fprog: the filter program
715 * @sk: the socket to use
716 *
717 * Attach the user's filter code. We first run some sanity checks on
718 * it to make sure it does not explode on us later. If an error
719 * occurs or there is insufficient memory for the filter a negative
720 * errno code is returned. On success the return is zero.
721 */
722int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
723{
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700724 struct sk_filter *fp, *old_fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
726 int err;
727
Vincent Bernatd59577b2013-01-16 22:55:49 +0100728 if (sock_flag(sk, SOCK_FILTER_LOCKED))
729 return -EPERM;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /* Make sure new filter is there and in the right amounts. */
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800732 if (fprog->filter == NULL)
733 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
736 if (!fp)
737 return -ENOMEM;
738 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900739 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EFAULT;
741 }
742
743 atomic_set(&fp->refcnt, 1);
744 fp->len = fprog->len;
745
Jiri Pirko302d6632012-03-31 11:01:19 +0000746 err = __sk_prepare_filter(fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700747 if (err) {
748 sk_filter_uncharge(sk, fp);
749 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000752 old_fp = rcu_dereference_protected(sk->sk_filter,
753 sock_owned_by_user(sk));
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700754 rcu_assign_pointer(sk->sk_filter, fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700755
Olof Johansson9b013e02007-10-18 21:48:39 -0700756 if (old_fp)
Eric Dumazet46bcf142010-12-06 09:29:43 -0800757 sk_filter_uncharge(sk, old_fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000760EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700762int sk_detach_filter(struct sock *sk)
763{
764 int ret = -ENOENT;
765 struct sk_filter *filter;
766
Vincent Bernatd59577b2013-01-16 22:55:49 +0100767 if (sock_flag(sk, SOCK_FILTER_LOCKED))
768 return -EPERM;
769
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000770 filter = rcu_dereference_protected(sk->sk_filter,
771 sock_owned_by_user(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700772 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000773 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800774 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700775 ret = 0;
776 }
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700777 return ret;
778}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000779EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +0000780
Nicolas Dichteled139982013-06-05 15:30:55 +0200781void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +0000782{
783 static const u16 decodes[] = {
784 [BPF_S_ALU_ADD_K] = BPF_ALU|BPF_ADD|BPF_K,
785 [BPF_S_ALU_ADD_X] = BPF_ALU|BPF_ADD|BPF_X,
786 [BPF_S_ALU_SUB_K] = BPF_ALU|BPF_SUB|BPF_K,
787 [BPF_S_ALU_SUB_X] = BPF_ALU|BPF_SUB|BPF_X,
788 [BPF_S_ALU_MUL_K] = BPF_ALU|BPF_MUL|BPF_K,
789 [BPF_S_ALU_MUL_X] = BPF_ALU|BPF_MUL|BPF_X,
790 [BPF_S_ALU_DIV_X] = BPF_ALU|BPF_DIV|BPF_X,
791 [BPF_S_ALU_MOD_K] = BPF_ALU|BPF_MOD|BPF_K,
792 [BPF_S_ALU_MOD_X] = BPF_ALU|BPF_MOD|BPF_X,
793 [BPF_S_ALU_AND_K] = BPF_ALU|BPF_AND|BPF_K,
794 [BPF_S_ALU_AND_X] = BPF_ALU|BPF_AND|BPF_X,
795 [BPF_S_ALU_OR_K] = BPF_ALU|BPF_OR|BPF_K,
796 [BPF_S_ALU_OR_X] = BPF_ALU|BPF_OR|BPF_X,
797 [BPF_S_ALU_XOR_K] = BPF_ALU|BPF_XOR|BPF_K,
798 [BPF_S_ALU_XOR_X] = BPF_ALU|BPF_XOR|BPF_X,
799 [BPF_S_ALU_LSH_K] = BPF_ALU|BPF_LSH|BPF_K,
800 [BPF_S_ALU_LSH_X] = BPF_ALU|BPF_LSH|BPF_X,
801 [BPF_S_ALU_RSH_K] = BPF_ALU|BPF_RSH|BPF_K,
802 [BPF_S_ALU_RSH_X] = BPF_ALU|BPF_RSH|BPF_X,
803 [BPF_S_ALU_NEG] = BPF_ALU|BPF_NEG,
804 [BPF_S_LD_W_ABS] = BPF_LD|BPF_W|BPF_ABS,
805 [BPF_S_LD_H_ABS] = BPF_LD|BPF_H|BPF_ABS,
806 [BPF_S_LD_B_ABS] = BPF_LD|BPF_B|BPF_ABS,
807 [BPF_S_ANC_PROTOCOL] = BPF_LD|BPF_B|BPF_ABS,
808 [BPF_S_ANC_PKTTYPE] = BPF_LD|BPF_B|BPF_ABS,
809 [BPF_S_ANC_IFINDEX] = BPF_LD|BPF_B|BPF_ABS,
810 [BPF_S_ANC_NLATTR] = BPF_LD|BPF_B|BPF_ABS,
811 [BPF_S_ANC_NLATTR_NEST] = BPF_LD|BPF_B|BPF_ABS,
812 [BPF_S_ANC_MARK] = BPF_LD|BPF_B|BPF_ABS,
813 [BPF_S_ANC_QUEUE] = BPF_LD|BPF_B|BPF_ABS,
814 [BPF_S_ANC_HATYPE] = BPF_LD|BPF_B|BPF_ABS,
815 [BPF_S_ANC_RXHASH] = BPF_LD|BPF_B|BPF_ABS,
816 [BPF_S_ANC_CPU] = BPF_LD|BPF_B|BPF_ABS,
817 [BPF_S_ANC_ALU_XOR_X] = BPF_LD|BPF_B|BPF_ABS,
818 [BPF_S_ANC_SECCOMP_LD_W] = BPF_LD|BPF_B|BPF_ABS,
819 [BPF_S_ANC_VLAN_TAG] = BPF_LD|BPF_B|BPF_ABS,
820 [BPF_S_ANC_VLAN_TAG_PRESENT] = BPF_LD|BPF_B|BPF_ABS,
Daniel Borkmann3e5289d2013-03-19 06:39:31 +0000821 [BPF_S_ANC_PAY_OFFSET] = BPF_LD|BPF_B|BPF_ABS,
Pavel Emelyanova8fc9272012-11-01 02:01:48 +0000822 [BPF_S_LD_W_LEN] = BPF_LD|BPF_W|BPF_LEN,
823 [BPF_S_LD_W_IND] = BPF_LD|BPF_W|BPF_IND,
824 [BPF_S_LD_H_IND] = BPF_LD|BPF_H|BPF_IND,
825 [BPF_S_LD_B_IND] = BPF_LD|BPF_B|BPF_IND,
826 [BPF_S_LD_IMM] = BPF_LD|BPF_IMM,
827 [BPF_S_LDX_W_LEN] = BPF_LDX|BPF_W|BPF_LEN,
828 [BPF_S_LDX_B_MSH] = BPF_LDX|BPF_B|BPF_MSH,
829 [BPF_S_LDX_IMM] = BPF_LDX|BPF_IMM,
830 [BPF_S_MISC_TAX] = BPF_MISC|BPF_TAX,
831 [BPF_S_MISC_TXA] = BPF_MISC|BPF_TXA,
832 [BPF_S_RET_K] = BPF_RET|BPF_K,
833 [BPF_S_RET_A] = BPF_RET|BPF_A,
834 [BPF_S_ALU_DIV_K] = BPF_ALU|BPF_DIV|BPF_K,
835 [BPF_S_LD_MEM] = BPF_LD|BPF_MEM,
836 [BPF_S_LDX_MEM] = BPF_LDX|BPF_MEM,
837 [BPF_S_ST] = BPF_ST,
838 [BPF_S_STX] = BPF_STX,
839 [BPF_S_JMP_JA] = BPF_JMP|BPF_JA,
840 [BPF_S_JMP_JEQ_K] = BPF_JMP|BPF_JEQ|BPF_K,
841 [BPF_S_JMP_JEQ_X] = BPF_JMP|BPF_JEQ|BPF_X,
842 [BPF_S_JMP_JGE_K] = BPF_JMP|BPF_JGE|BPF_K,
843 [BPF_S_JMP_JGE_X] = BPF_JMP|BPF_JGE|BPF_X,
844 [BPF_S_JMP_JGT_K] = BPF_JMP|BPF_JGT|BPF_K,
845 [BPF_S_JMP_JGT_X] = BPF_JMP|BPF_JGT|BPF_X,
846 [BPF_S_JMP_JSET_K] = BPF_JMP|BPF_JSET|BPF_K,
847 [BPF_S_JMP_JSET_X] = BPF_JMP|BPF_JSET|BPF_X,
848 };
849 u16 code;
850
851 code = filt->code;
852
853 to->code = decodes[code];
854 to->jt = filt->jt;
855 to->jf = filt->jf;
856
857 if (code == BPF_S_ALU_DIV_K) {
858 /*
859 * When loaded this rule user gave us X, which was
860 * translated into R = r(X). Now we calculate the
861 * RR = r(R) and report it back. If next time this
862 * value is loaded and RRR = r(RR) is calculated
863 * then the R == RRR will be true.
864 *
865 * One exception. X == 1 translates into R == 0 and
866 * we can't calculate RR out of it with r().
867 */
868
869 if (filt->k == 0)
870 to->k = 1;
871 else
872 to->k = reciprocal_value(filt->k);
873
874 BUG_ON(reciprocal_value(to->k) != filt->k);
875 } else
876 to->k = filt->k;
877}
878
879int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, unsigned int len)
880{
881 struct sk_filter *filter;
882 int i, ret;
883
884 lock_sock(sk);
885 filter = rcu_dereference_protected(sk->sk_filter,
886 sock_owned_by_user(sk));
887 ret = 0;
888 if (!filter)
889 goto out;
890 ret = filter->len;
891 if (!len)
892 goto out;
893 ret = -EINVAL;
894 if (len < filter->len)
895 goto out;
896
897 ret = -EFAULT;
898 for (i = 0; i < filter->len; i++) {
899 struct sock_filter fb;
900
901 sk_decode_filter(&filter->insns[i], &fb);
902 if (copy_to_user(&ubuf[i], &fb, sizeof(fb)))
903 goto out;
904 }
905
906 ret = filter->len;
907out:
908 release_sock(sk);
909 return ret;
910}