blob: 2e20b55a7830c43e25bdd4221cc134f374d7148e [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;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000351 case BPF_S_ANC_NLATTR: {
Patrick McHardy4738c1d2008-04-10 02:02:28 -0700352 struct nlattr *nla;
353
354 if (skb_is_nonlinear(skb))
355 return 0;
356 if (A > skb->len - sizeof(struct nlattr))
357 return 0;
358
359 nla = nla_find((struct nlattr *)&skb->data[A],
360 skb->len - A, X);
361 if (nla)
362 A = (void *)nla - (void *)skb->data;
363 else
364 A = 0;
365 continue;
366 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000367 case BPF_S_ANC_NLATTR_NEST: {
Pablo Neira Ayusod214c752008-11-20 00:49:27 -0800368 struct nlattr *nla;
369
370 if (skb_is_nonlinear(skb))
371 return 0;
372 if (A > skb->len - sizeof(struct nlattr))
373 return 0;
374
375 nla = (struct nlattr *)&skb->data[A];
376 if (nla->nla_len > A - skb->len)
377 return 0;
378
379 nla = nla_find_nested(nla, X);
380 if (nla)
381 A = (void *)nla - (void *)skb->data;
382 else
383 A = 0;
384 continue;
385 }
Will Drewry46b325c2012-04-12 16:47:52 -0500386#ifdef CONFIG_SECCOMP_FILTER
387 case BPF_S_ANC_SECCOMP_LD_W:
388 A = seccomp_bpf_load(fentry->k);
389 continue;
390#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 default:
Joe Perches6c4a5cb2011-05-21 07:48:40 +0000392 WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n",
393 fentry->code, fentry->jt,
394 fentry->jf, fentry->k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396 }
397 }
398
399 return 0;
400}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700401EXPORT_SYMBOL(sk_run_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000403/*
404 * Security :
405 * A BPF program is able to use 16 cells of memory to store intermediate
406 * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter())
407 * As we dont want to clear mem[] array for each packet going through
408 * sk_run_filter(), we check that filter loaded by user never try to read
409 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300410 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000411 */
412static int check_load_and_stores(struct sock_filter *filter, int flen)
413{
414 u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */
415 int pc, ret = 0;
416
417 BUILD_BUG_ON(BPF_MEMWORDS > 16);
418 masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
419 if (!masks)
420 return -ENOMEM;
421 memset(masks, 0xff, flen * sizeof(*masks));
422
423 for (pc = 0; pc < flen; pc++) {
424 memvalid &= masks[pc];
425
426 switch (filter[pc].code) {
427 case BPF_S_ST:
428 case BPF_S_STX:
429 memvalid |= (1 << filter[pc].k);
430 break;
431 case BPF_S_LD_MEM:
432 case BPF_S_LDX_MEM:
433 if (!(memvalid & (1 << filter[pc].k))) {
434 ret = -EINVAL;
435 goto error;
436 }
437 break;
438 case BPF_S_JMP_JA:
439 /* a jump must set masks on target */
440 masks[pc + 1 + filter[pc].k] &= memvalid;
441 memvalid = ~0;
442 break;
443 case BPF_S_JMP_JEQ_K:
444 case BPF_S_JMP_JEQ_X:
445 case BPF_S_JMP_JGE_K:
446 case BPF_S_JMP_JGE_X:
447 case BPF_S_JMP_JGT_K:
448 case BPF_S_JMP_JGT_X:
449 case BPF_S_JMP_JSET_X:
450 case BPF_S_JMP_JSET_K:
451 /* a jump must set masks on targets */
452 masks[pc + 1 + filter[pc].jt] &= memvalid;
453 masks[pc + 1 + filter[pc].jf] &= memvalid;
454 memvalid = ~0;
455 break;
456 }
457 }
458error:
459 kfree(masks);
460 return ret;
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/**
464 * sk_chk_filter - verify socket filter code
465 * @filter: filter to verify
466 * @flen: length of filter
467 *
468 * Check the user's filter code. If we let some ugly
469 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800470 * no references or jumps that are out of range, no illegal
471 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800473 * All jumps are forward as they are not signed.
474 *
475 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
Dan Carpenter4f25af22011-10-17 21:04:20 +0000477int sk_chk_filter(struct sock_filter *filter, unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Tetsuo Handacba328f2010-11-16 15:19:51 +0000479 /*
480 * Valid instructions are initialized to non-0.
481 * Invalid instructions are initialized to 0.
482 */
483 static const u8 codes[] = {
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000484 [BPF_ALU|BPF_ADD|BPF_K] = BPF_S_ALU_ADD_K,
485 [BPF_ALU|BPF_ADD|BPF_X] = BPF_S_ALU_ADD_X,
486 [BPF_ALU|BPF_SUB|BPF_K] = BPF_S_ALU_SUB_K,
487 [BPF_ALU|BPF_SUB|BPF_X] = BPF_S_ALU_SUB_X,
488 [BPF_ALU|BPF_MUL|BPF_K] = BPF_S_ALU_MUL_K,
489 [BPF_ALU|BPF_MUL|BPF_X] = BPF_S_ALU_MUL_X,
490 [BPF_ALU|BPF_DIV|BPF_X] = BPF_S_ALU_DIV_X,
Eric Dumazetb6069a92012-09-07 22:03:35 +0000491 [BPF_ALU|BPF_MOD|BPF_K] = BPF_S_ALU_MOD_K,
492 [BPF_ALU|BPF_MOD|BPF_X] = BPF_S_ALU_MOD_X,
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000493 [BPF_ALU|BPF_AND|BPF_K] = BPF_S_ALU_AND_K,
494 [BPF_ALU|BPF_AND|BPF_X] = BPF_S_ALU_AND_X,
495 [BPF_ALU|BPF_OR|BPF_K] = BPF_S_ALU_OR_K,
496 [BPF_ALU|BPF_OR|BPF_X] = BPF_S_ALU_OR_X,
Daniel Borkmann9e49e882012-09-24 02:23:59 +0000497 [BPF_ALU|BPF_XOR|BPF_K] = BPF_S_ALU_XOR_K,
498 [BPF_ALU|BPF_XOR|BPF_X] = BPF_S_ALU_XOR_X,
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000499 [BPF_ALU|BPF_LSH|BPF_K] = BPF_S_ALU_LSH_K,
500 [BPF_ALU|BPF_LSH|BPF_X] = BPF_S_ALU_LSH_X,
501 [BPF_ALU|BPF_RSH|BPF_K] = BPF_S_ALU_RSH_K,
502 [BPF_ALU|BPF_RSH|BPF_X] = BPF_S_ALU_RSH_X,
503 [BPF_ALU|BPF_NEG] = BPF_S_ALU_NEG,
504 [BPF_LD|BPF_W|BPF_ABS] = BPF_S_LD_W_ABS,
505 [BPF_LD|BPF_H|BPF_ABS] = BPF_S_LD_H_ABS,
506 [BPF_LD|BPF_B|BPF_ABS] = BPF_S_LD_B_ABS,
507 [BPF_LD|BPF_W|BPF_LEN] = BPF_S_LD_W_LEN,
508 [BPF_LD|BPF_W|BPF_IND] = BPF_S_LD_W_IND,
509 [BPF_LD|BPF_H|BPF_IND] = BPF_S_LD_H_IND,
510 [BPF_LD|BPF_B|BPF_IND] = BPF_S_LD_B_IND,
511 [BPF_LD|BPF_IMM] = BPF_S_LD_IMM,
512 [BPF_LDX|BPF_W|BPF_LEN] = BPF_S_LDX_W_LEN,
513 [BPF_LDX|BPF_B|BPF_MSH] = BPF_S_LDX_B_MSH,
514 [BPF_LDX|BPF_IMM] = BPF_S_LDX_IMM,
515 [BPF_MISC|BPF_TAX] = BPF_S_MISC_TAX,
516 [BPF_MISC|BPF_TXA] = BPF_S_MISC_TXA,
517 [BPF_RET|BPF_K] = BPF_S_RET_K,
518 [BPF_RET|BPF_A] = BPF_S_RET_A,
519 [BPF_ALU|BPF_DIV|BPF_K] = BPF_S_ALU_DIV_K,
520 [BPF_LD|BPF_MEM] = BPF_S_LD_MEM,
521 [BPF_LDX|BPF_MEM] = BPF_S_LDX_MEM,
522 [BPF_ST] = BPF_S_ST,
523 [BPF_STX] = BPF_S_STX,
524 [BPF_JMP|BPF_JA] = BPF_S_JMP_JA,
525 [BPF_JMP|BPF_JEQ|BPF_K] = BPF_S_JMP_JEQ_K,
526 [BPF_JMP|BPF_JEQ|BPF_X] = BPF_S_JMP_JEQ_X,
527 [BPF_JMP|BPF_JGE|BPF_K] = BPF_S_JMP_JGE_K,
528 [BPF_JMP|BPF_JGE|BPF_X] = BPF_S_JMP_JGE_X,
529 [BPF_JMP|BPF_JGT|BPF_K] = BPF_S_JMP_JGT_K,
530 [BPF_JMP|BPF_JGT|BPF_X] = BPF_S_JMP_JGT_X,
531 [BPF_JMP|BPF_JSET|BPF_K] = BPF_S_JMP_JSET_K,
532 [BPF_JMP|BPF_JSET|BPF_X] = BPF_S_JMP_JSET_X,
Tetsuo Handacba328f2010-11-16 15:19:51 +0000533 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int pc;
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000535 bool anc_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
David S. Miller1b93ae642005-12-27 13:57:59 -0800537 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -EINVAL;
539
540 /* check the filter code now */
541 for (pc = 0; pc < flen; pc++) {
Tetsuo Handacba328f2010-11-16 15:19:51 +0000542 struct sock_filter *ftest = &filter[pc];
543 u16 code = ftest->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Tetsuo Handacba328f2010-11-16 15:19:51 +0000545 if (code >= ARRAY_SIZE(codes))
546 return -EINVAL;
547 code = codes[code];
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000548 if (!code)
Tetsuo Handacba328f2010-11-16 15:19:51 +0000549 return -EINVAL;
Kris Katterjohn93699862006-01-04 13:58:36 -0800550 /* Some instructions need special checks */
Tetsuo Handacba328f2010-11-16 15:19:51 +0000551 switch (code) {
552 case BPF_S_ALU_DIV_K:
Kris Katterjohn93699862006-01-04 13:58:36 -0800553 /* check for division by zero */
554 if (ftest->k == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -EINVAL;
Eric Dumazetc26aed42010-11-18 22:04:46 +0000556 ftest->k = reciprocal_value(ftest->k);
Kris Katterjohn93699862006-01-04 13:58:36 -0800557 break;
Eric Dumazetb6069a92012-09-07 22:03:35 +0000558 case BPF_S_ALU_MOD_K:
559 /* check for division by zero */
560 if (ftest->k == 0)
561 return -EINVAL;
562 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000563 case BPF_S_LD_MEM:
564 case BPF_S_LDX_MEM:
565 case BPF_S_ST:
566 case BPF_S_STX:
567 /* check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800568 if (ftest->k >= BPF_MEMWORDS)
569 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000570 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000571 case BPF_S_JMP_JA:
Kris Katterjohn93699862006-01-04 13:58:36 -0800572 /*
573 * Note, the large ftest->k might cause loops.
574 * Compare this with conditional jumps below,
575 * where offsets are limited. --ANK (981016)
576 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000577 if (ftest->k >= (unsigned int)(flen-pc-1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800578 return -EINVAL;
579 break;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000580 case BPF_S_JMP_JEQ_K:
581 case BPF_S_JMP_JEQ_X:
582 case BPF_S_JMP_JGE_K:
583 case BPF_S_JMP_JGE_X:
584 case BPF_S_JMP_JGT_K:
585 case BPF_S_JMP_JGT_X:
586 case BPF_S_JMP_JSET_X:
587 case BPF_S_JMP_JSET_K:
Tetsuo Handacba328f2010-11-16 15:19:51 +0000588 /* for conditionals both must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000589 if (pc + ftest->jt + 1 >= flen ||
590 pc + ftest->jf + 1 >= flen)
591 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000592 break;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000593 case BPF_S_LD_W_ABS:
594 case BPF_S_LD_H_ABS:
595 case BPF_S_LD_B_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000596 anc_found = false;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000597#define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
598 code = BPF_S_ANC_##CODE; \
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000599 anc_found = true; \
Eric Dumazet12b16da2010-12-15 19:45:28 +0000600 break
601 switch (ftest->k) {
602 ANCILLARY(PROTOCOL);
603 ANCILLARY(PKTTYPE);
604 ANCILLARY(IFINDEX);
605 ANCILLARY(NLATTR);
606 ANCILLARY(NLATTR_NEST);
607 ANCILLARY(MARK);
608 ANCILLARY(QUEUE);
609 ANCILLARY(HATYPE);
610 ANCILLARY(RXHASH);
611 ANCILLARY(CPU);
Jiri Pirkoffe06c12012-03-31 11:01:20 +0000612 ANCILLARY(ALU_XOR_X);
Eric Dumazetf3335032012-10-27 02:26:17 +0000613 ANCILLARY(VLAN_TAG);
614 ANCILLARY(VLAN_TAG_PRESENT);
Eric Dumazet12b16da2010-12-15 19:45:28 +0000615 }
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000616
617 /* ancillary operation unknown or unsupported */
618 if (anc_found == false && ftest->k >= SKF_AD_OFF)
619 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000620 }
Tetsuo Handacba328f2010-11-16 15:19:51 +0000621 ftest->code = code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000624 /* last instruction must be a RET code */
625 switch (filter[flen - 1].code) {
626 case BPF_S_RET_K:
627 case BPF_S_RET_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000628 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000629 }
630 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700632EXPORT_SYMBOL(sk_chk_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800635 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700636 * @rcu: rcu_head that contains the sk_filter to free
637 */
Eric Dumazet46bcf142010-12-06 09:29:43 -0800638void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700639{
640 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
641
Eric Dumazet0a148422011-04-20 09:27:32 +0000642 bpf_jit_free(fp);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800643 kfree(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700644}
Eric Dumazet46bcf142010-12-06 09:29:43 -0800645EXPORT_SYMBOL(sk_filter_release_rcu);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700646
Jiri Pirko302d6632012-03-31 11:01:19 +0000647static int __sk_prepare_filter(struct sk_filter *fp)
648{
649 int err;
650
651 fp->bpf_func = sk_run_filter;
652
653 err = sk_chk_filter(fp->insns, fp->len);
654 if (err)
655 return err;
656
657 bpf_jit_compile(fp);
658 return 0;
659}
660
661/**
662 * sk_unattached_filter_create - create an unattached filter
663 * @fprog: the filter program
Randy Dunlapc6c4b972012-06-08 14:01:44 +0000664 * @pfp: the unattached filter that is created
Jiri Pirko302d6632012-03-31 11:01:19 +0000665 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +0000666 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +0000667 * sanity checks on it to make sure it does not explode on us later.
668 * If an error occurs or there is insufficient memory for the filter
669 * a negative errno code is returned. On success the return is zero.
670 */
671int sk_unattached_filter_create(struct sk_filter **pfp,
672 struct sock_fprog *fprog)
673{
674 struct sk_filter *fp;
675 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
676 int err;
677
678 /* Make sure new filter is there and in the right amounts. */
679 if (fprog->filter == NULL)
680 return -EINVAL;
681
682 fp = kmalloc(fsize + sizeof(*fp), GFP_KERNEL);
683 if (!fp)
684 return -ENOMEM;
685 memcpy(fp->insns, fprog->filter, fsize);
686
687 atomic_set(&fp->refcnt, 1);
688 fp->len = fprog->len;
689
690 err = __sk_prepare_filter(fp);
691 if (err)
692 goto free_mem;
693
694 *pfp = fp;
695 return 0;
696free_mem:
697 kfree(fp);
698 return err;
699}
700EXPORT_SYMBOL_GPL(sk_unattached_filter_create);
701
702void sk_unattached_filter_destroy(struct sk_filter *fp)
703{
704 sk_filter_release(fp);
705}
706EXPORT_SYMBOL_GPL(sk_unattached_filter_destroy);
707
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700708/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * sk_attach_filter - attach a socket filter
710 * @fprog: the filter program
711 * @sk: the socket to use
712 *
713 * Attach the user's filter code. We first run some sanity checks on
714 * it to make sure it does not explode on us later. If an error
715 * occurs or there is insufficient memory for the filter a negative
716 * errno code is returned. On success the return is zero.
717 */
718int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
719{
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700720 struct sk_filter *fp, *old_fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
722 int err;
723
Vincent Bernatd59577b2013-01-16 22:55:49 +0100724 if (sock_flag(sk, SOCK_FILTER_LOCKED))
725 return -EPERM;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* Make sure new filter is there and in the right amounts. */
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800728 if (fprog->filter == NULL)
729 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 fp = sock_kmalloc(sk, fsize+sizeof(*fp), GFP_KERNEL);
732 if (!fp)
733 return -ENOMEM;
734 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900735 sock_kfree_s(sk, fp, fsize+sizeof(*fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return -EFAULT;
737 }
738
739 atomic_set(&fp->refcnt, 1);
740 fp->len = fprog->len;
741
Jiri Pirko302d6632012-03-31 11:01:19 +0000742 err = __sk_prepare_filter(fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700743 if (err) {
744 sk_filter_uncharge(sk, fp);
745 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000748 old_fp = rcu_dereference_protected(sk->sk_filter,
749 sock_owned_by_user(sk));
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700750 rcu_assign_pointer(sk->sk_filter, fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700751
Olof Johansson9b013e02007-10-18 21:48:39 -0700752 if (old_fp)
Eric Dumazet46bcf142010-12-06 09:29:43 -0800753 sk_filter_uncharge(sk, old_fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000756EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700758int sk_detach_filter(struct sock *sk)
759{
760 int ret = -ENOENT;
761 struct sk_filter *filter;
762
Vincent Bernatd59577b2013-01-16 22:55:49 +0100763 if (sock_flag(sk, SOCK_FILTER_LOCKED))
764 return -EPERM;
765
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000766 filter = rcu_dereference_protected(sk->sk_filter,
767 sock_owned_by_user(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700768 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000769 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800770 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700771 ret = 0;
772 }
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700773 return ret;
774}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000775EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +0000776
777static void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to)
778{
779 static const u16 decodes[] = {
780 [BPF_S_ALU_ADD_K] = BPF_ALU|BPF_ADD|BPF_K,
781 [BPF_S_ALU_ADD_X] = BPF_ALU|BPF_ADD|BPF_X,
782 [BPF_S_ALU_SUB_K] = BPF_ALU|BPF_SUB|BPF_K,
783 [BPF_S_ALU_SUB_X] = BPF_ALU|BPF_SUB|BPF_X,
784 [BPF_S_ALU_MUL_K] = BPF_ALU|BPF_MUL|BPF_K,
785 [BPF_S_ALU_MUL_X] = BPF_ALU|BPF_MUL|BPF_X,
786 [BPF_S_ALU_DIV_X] = BPF_ALU|BPF_DIV|BPF_X,
787 [BPF_S_ALU_MOD_K] = BPF_ALU|BPF_MOD|BPF_K,
788 [BPF_S_ALU_MOD_X] = BPF_ALU|BPF_MOD|BPF_X,
789 [BPF_S_ALU_AND_K] = BPF_ALU|BPF_AND|BPF_K,
790 [BPF_S_ALU_AND_X] = BPF_ALU|BPF_AND|BPF_X,
791 [BPF_S_ALU_OR_K] = BPF_ALU|BPF_OR|BPF_K,
792 [BPF_S_ALU_OR_X] = BPF_ALU|BPF_OR|BPF_X,
793 [BPF_S_ALU_XOR_K] = BPF_ALU|BPF_XOR|BPF_K,
794 [BPF_S_ALU_XOR_X] = BPF_ALU|BPF_XOR|BPF_X,
795 [BPF_S_ALU_LSH_K] = BPF_ALU|BPF_LSH|BPF_K,
796 [BPF_S_ALU_LSH_X] = BPF_ALU|BPF_LSH|BPF_X,
797 [BPF_S_ALU_RSH_K] = BPF_ALU|BPF_RSH|BPF_K,
798 [BPF_S_ALU_RSH_X] = BPF_ALU|BPF_RSH|BPF_X,
799 [BPF_S_ALU_NEG] = BPF_ALU|BPF_NEG,
800 [BPF_S_LD_W_ABS] = BPF_LD|BPF_W|BPF_ABS,
801 [BPF_S_LD_H_ABS] = BPF_LD|BPF_H|BPF_ABS,
802 [BPF_S_LD_B_ABS] = BPF_LD|BPF_B|BPF_ABS,
803 [BPF_S_ANC_PROTOCOL] = BPF_LD|BPF_B|BPF_ABS,
804 [BPF_S_ANC_PKTTYPE] = BPF_LD|BPF_B|BPF_ABS,
805 [BPF_S_ANC_IFINDEX] = BPF_LD|BPF_B|BPF_ABS,
806 [BPF_S_ANC_NLATTR] = BPF_LD|BPF_B|BPF_ABS,
807 [BPF_S_ANC_NLATTR_NEST] = BPF_LD|BPF_B|BPF_ABS,
808 [BPF_S_ANC_MARK] = BPF_LD|BPF_B|BPF_ABS,
809 [BPF_S_ANC_QUEUE] = BPF_LD|BPF_B|BPF_ABS,
810 [BPF_S_ANC_HATYPE] = BPF_LD|BPF_B|BPF_ABS,
811 [BPF_S_ANC_RXHASH] = BPF_LD|BPF_B|BPF_ABS,
812 [BPF_S_ANC_CPU] = BPF_LD|BPF_B|BPF_ABS,
813 [BPF_S_ANC_ALU_XOR_X] = BPF_LD|BPF_B|BPF_ABS,
814 [BPF_S_ANC_SECCOMP_LD_W] = BPF_LD|BPF_B|BPF_ABS,
815 [BPF_S_ANC_VLAN_TAG] = BPF_LD|BPF_B|BPF_ABS,
816 [BPF_S_ANC_VLAN_TAG_PRESENT] = BPF_LD|BPF_B|BPF_ABS,
817 [BPF_S_LD_W_LEN] = BPF_LD|BPF_W|BPF_LEN,
818 [BPF_S_LD_W_IND] = BPF_LD|BPF_W|BPF_IND,
819 [BPF_S_LD_H_IND] = BPF_LD|BPF_H|BPF_IND,
820 [BPF_S_LD_B_IND] = BPF_LD|BPF_B|BPF_IND,
821 [BPF_S_LD_IMM] = BPF_LD|BPF_IMM,
822 [BPF_S_LDX_W_LEN] = BPF_LDX|BPF_W|BPF_LEN,
823 [BPF_S_LDX_B_MSH] = BPF_LDX|BPF_B|BPF_MSH,
824 [BPF_S_LDX_IMM] = BPF_LDX|BPF_IMM,
825 [BPF_S_MISC_TAX] = BPF_MISC|BPF_TAX,
826 [BPF_S_MISC_TXA] = BPF_MISC|BPF_TXA,
827 [BPF_S_RET_K] = BPF_RET|BPF_K,
828 [BPF_S_RET_A] = BPF_RET|BPF_A,
829 [BPF_S_ALU_DIV_K] = BPF_ALU|BPF_DIV|BPF_K,
830 [BPF_S_LD_MEM] = BPF_LD|BPF_MEM,
831 [BPF_S_LDX_MEM] = BPF_LDX|BPF_MEM,
832 [BPF_S_ST] = BPF_ST,
833 [BPF_S_STX] = BPF_STX,
834 [BPF_S_JMP_JA] = BPF_JMP|BPF_JA,
835 [BPF_S_JMP_JEQ_K] = BPF_JMP|BPF_JEQ|BPF_K,
836 [BPF_S_JMP_JEQ_X] = BPF_JMP|BPF_JEQ|BPF_X,
837 [BPF_S_JMP_JGE_K] = BPF_JMP|BPF_JGE|BPF_K,
838 [BPF_S_JMP_JGE_X] = BPF_JMP|BPF_JGE|BPF_X,
839 [BPF_S_JMP_JGT_K] = BPF_JMP|BPF_JGT|BPF_K,
840 [BPF_S_JMP_JGT_X] = BPF_JMP|BPF_JGT|BPF_X,
841 [BPF_S_JMP_JSET_K] = BPF_JMP|BPF_JSET|BPF_K,
842 [BPF_S_JMP_JSET_X] = BPF_JMP|BPF_JSET|BPF_X,
843 };
844 u16 code;
845
846 code = filt->code;
847
848 to->code = decodes[code];
849 to->jt = filt->jt;
850 to->jf = filt->jf;
851
852 if (code == BPF_S_ALU_DIV_K) {
853 /*
854 * When loaded this rule user gave us X, which was
855 * translated into R = r(X). Now we calculate the
856 * RR = r(R) and report it back. If next time this
857 * value is loaded and RRR = r(RR) is calculated
858 * then the R == RRR will be true.
859 *
860 * One exception. X == 1 translates into R == 0 and
861 * we can't calculate RR out of it with r().
862 */
863
864 if (filt->k == 0)
865 to->k = 1;
866 else
867 to->k = reciprocal_value(filt->k);
868
869 BUG_ON(reciprocal_value(to->k) != filt->k);
870 } else
871 to->k = filt->k;
872}
873
874int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, unsigned int len)
875{
876 struct sk_filter *filter;
877 int i, ret;
878
879 lock_sock(sk);
880 filter = rcu_dereference_protected(sk->sk_filter,
881 sock_owned_by_user(sk));
882 ret = 0;
883 if (!filter)
884 goto out;
885 ret = filter->len;
886 if (!len)
887 goto out;
888 ret = -EINVAL;
889 if (len < filter->len)
890 goto out;
891
892 ret = -EFAULT;
893 for (i = 0; i < filter->len; i++) {
894 struct sock_filter fb;
895
896 sk_decode_filter(&filter->insns[i], &fb);
897 if (copy_to_user(&ubuf[i], &fb, sizeof(fb)))
898 goto out;
899 }
900
901 ret = filter->len;
902out:
903 release_sock(sk);
904 return ret;
905}