blob: bb3c76458ca9cc3c2f6aefc8022a224055c5fa65 [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>
David S. Miller86e4ca62011-05-26 15:00:31 -040039#include <linux/ratelimit.h>
Will Drewry46b325c2012-04-12 16:47:52 -050040#include <linux/seccomp.h>
Eric Dumazetf3335032012-10-27 02:26:17 +000041#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jan Seiffertf03fb3f2012-03-30 05:08:19 +000043/* No hurry in this branch
44 *
45 * Exported for the bpf jit load helper.
46 */
47void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 u8 *ptr = NULL;
50
51 if (k >= SKF_NET_OFF)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070052 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 else if (k >= SKF_LL_OFF)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070054 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Eric Dumazet4bc65dd2010-12-07 22:26:15 +000056 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return ptr;
58 return NULL;
59}
60
Eric Dumazet62ab0812010-12-06 20:50:09 +000061static inline void *load_pointer(const struct sk_buff *skb, int k,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090062 unsigned int size, void *buffer)
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070063{
64 if (k >= 0)
65 return skb_header_pointer(skb, k, size, buffer);
Jan Seiffertf03fb3f2012-03-30 05:08:19 +000066 return bpf_internal_load_pointer_neg_helper(skb, k, size);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070067}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/**
Stephen Hemminger43db6d62008-04-10 01:43:09 -070070 * sk_filter - run a packet through a socket filter
71 * @sk: sock associated with &sk_buff
72 * @skb: buffer to filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -070073 *
74 * Run the filter code and then cut skb->data to correct size returned by
75 * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
76 * than pkt_len we keep whole skb->data. This is the socket level
77 * wrapper to sk_run_filter. It returns 0 if the packet should
78 * be accepted or -EPERM if the packet should be tossed.
79 *
80 */
81int sk_filter(struct sock *sk, struct sk_buff *skb)
82{
83 int err;
84 struct sk_filter *filter;
85
Mel Gormanc93bdd02012-07-31 16:44:19 -070086 /*
87 * If the skb was allocated from pfmemalloc reserves, only
88 * allow SOCK_MEMALLOC sockets to use it as this socket is
89 * helping free memory
90 */
91 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
92 return -ENOMEM;
93
Stephen Hemminger43db6d62008-04-10 01:43:09 -070094 err = security_sock_rcv_skb(sk, skb);
95 if (err)
96 return err;
97
Eric Dumazet80f8f102011-01-18 07:46:52 +000098 rcu_read_lock();
99 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700100 if (filter) {
Eric Dumazet0a148422011-04-20 09:27:32 +0000101 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
Eric Dumazet0d7da9d2010-10-25 03:47:05 +0000102
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700103 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
104 }
Eric Dumazet80f8f102011-01-18 07:46:52 +0000105 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700106
107 return err;
108}
109EXPORT_SYMBOL(sk_filter);
110
111/**
Kris Katterjohn2966b662006-01-23 16:26:16 -0800112 * sk_run_filter - run a filter on a socket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * @skb: buffer to run the filter on
Randy Dunlap697d0e32011-01-08 17:41:42 +0000114 * @fentry: filter to apply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
116 * Decode and apply filter instructions to the skb->data.
Eric Dumazet93aaae22010-11-19 09:49:59 -0800117 * Return length to keep, 0 for none. @skb is the data we are
118 * filtering, @filter is the array of filter instructions.
119 * Because all jumps are guaranteed to be before last instruction,
120 * and last instruction guaranteed to be a RET, we dont need to check
121 * flen. (We used to pass to this function the length of filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
Eric Dumazet62ab0812010-12-06 20:50:09 +0000123unsigned int sk_run_filter(const struct sk_buff *skb,
124 const struct sock_filter *fentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700126 void *ptr;
Kris Katterjohn2966b662006-01-23 16:26:16 -0800127 u32 A = 0; /* Accumulator */
128 u32 X = 0; /* Index Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 u32 mem[BPF_MEMWORDS]; /* Scratch Memory Store */
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700130 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /*
134 * Process array of filter instructions.
135 */
Eric Dumazet93aaae22010-11-19 09:49:59 -0800136 for (;; fentry++) {
137#if defined(CONFIG_X86_32)
138#define K (fentry->k)
139#else
140 const u32 K = fentry->k;
141#endif
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 switch (fentry->code) {
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000144 case BPF_S_ALU_ADD_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 A += X;
146 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000147 case BPF_S_ALU_ADD_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800148 A += K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000150 case BPF_S_ALU_SUB_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 A -= X;
152 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000153 case BPF_S_ALU_SUB_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800154 A -= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000156 case BPF_S_ALU_MUL_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 A *= X;
158 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000159 case BPF_S_ALU_MUL_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800160 A *= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000162 case BPF_S_ALU_DIV_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (X == 0)
164 return 0;
165 A /= X;
166 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000167 case BPF_S_ALU_DIV_K:
Eric Dumazetaee636c2014-01-15 06:50:07 -0800168 A /= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 continue;
Eric Dumazetb6069a92012-09-07 22:03:35 +0000170 case BPF_S_ALU_MOD_X:
171 if (X == 0)
172 return 0;
173 A %= X;
174 continue;
175 case BPF_S_ALU_MOD_K:
176 A %= K;
177 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000178 case BPF_S_ALU_AND_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 A &= X;
180 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000181 case BPF_S_ALU_AND_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800182 A &= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000184 case BPF_S_ALU_OR_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 A |= X;
186 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000187 case BPF_S_ALU_OR_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800188 A |= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 continue;
Daniel Borkmann9e49e882012-09-24 02:23:59 +0000190 case BPF_S_ANC_ALU_XOR_X:
191 case BPF_S_ALU_XOR_X:
192 A ^= X;
193 continue;
194 case BPF_S_ALU_XOR_K:
195 A ^= K;
196 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000197 case BPF_S_ALU_LSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 A <<= X;
199 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000200 case BPF_S_ALU_LSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800201 A <<= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000203 case BPF_S_ALU_RSH_X:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 A >>= X;
205 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000206 case BPF_S_ALU_RSH_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800207 A >>= K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000209 case BPF_S_ALU_NEG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 A = -A;
211 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000212 case BPF_S_JMP_JA:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800213 fentry += K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000215 case BPF_S_JMP_JGT_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800216 fentry += (A > K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000218 case BPF_S_JMP_JGE_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800219 fentry += (A >= K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000221 case BPF_S_JMP_JEQ_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800222 fentry += (A == K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000224 case BPF_S_JMP_JSET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800225 fentry += (A & K) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000227 case BPF_S_JMP_JGT_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800228 fentry += (A > X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000230 case BPF_S_JMP_JGE_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800231 fentry += (A >= X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000233 case BPF_S_JMP_JEQ_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800234 fentry += (A == X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000236 case BPF_S_JMP_JSET_X:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800237 fentry += (A & X) ? fentry->jt : fentry->jf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000239 case BPF_S_LD_W_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800240 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800241load_w:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700242 ptr = load_pointer(skb, k, 4, &tmp);
243 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700244 A = get_unaligned_be32(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700245 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000247 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000248 case BPF_S_LD_H_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800249 k = K;
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800250load_h:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700251 ptr = load_pointer(skb, k, 2, &tmp);
252 if (ptr != NULL) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700253 A = get_unaligned_be16(ptr);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700254 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000256 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000257 case BPF_S_LD_B_ABS:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800258 k = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259load_b:
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700260 ptr = load_pointer(skb, k, 1, &tmp);
261 if (ptr != NULL) {
262 A = *(u8 *)ptr;
263 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000265 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000266 case BPF_S_LD_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700267 A = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000269 case BPF_S_LDX_W_LEN:
Patrick McHardy3154e542005-07-05 14:10:40 -0700270 X = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000272 case BPF_S_LD_W_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800273 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto load_w;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000275 case BPF_S_LD_H_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800276 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto load_h;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000278 case BPF_S_LD_B_IND:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800279 k = X + K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 goto load_b;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000281 case BPF_S_LDX_B_MSH:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800282 ptr = load_pointer(skb, K, 1, &tmp);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -0700283 if (ptr != NULL) {
284 X = (*(u8 *)ptr & 0xf) << 2;
285 continue;
286 }
287 return 0;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000288 case BPF_S_LD_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800289 A = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000291 case BPF_S_LDX_IMM:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800292 X = K;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000294 case BPF_S_LD_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000295 A = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000297 case BPF_S_LDX_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000298 X = mem[K];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000300 case BPF_S_MISC_TAX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 X = A;
302 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000303 case BPF_S_MISC_TXA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 A = X;
305 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000306 case BPF_S_RET_K:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800307 return K;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000308 case BPF_S_RET_A:
Kris Katterjohn4bad4dc2006-01-06 13:08:20 -0800309 return A;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000310 case BPF_S_ST:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800311 mem[K] = A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 continue;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000313 case BPF_S_STX:
Eric Dumazet93aaae22010-11-19 09:49:59 -0800314 mem[K] = X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000316 case BPF_S_ANC_PROTOCOL:
Al Viro252e3342006-11-14 20:48:11 -0800317 A = ntohs(skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000319 case BPF_S_ANC_PKTTYPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 A = skb->pkt_type;
321 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000322 case BPF_S_ANC_IFINDEX:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000323 if (!skb->dev)
324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 A = skb->dev->ifindex;
326 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000327 case BPF_S_ANC_MARK:
jamal7e75f932009-10-19 02:17:56 +0000328 A = skb->mark;
329 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000330 case BPF_S_ANC_QUEUE:
Eric Dumazetd19742f2009-10-20 01:06:22 -0700331 A = skb->queue_mapping;
332 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000333 case BPF_S_ANC_HATYPE:
Paul LeoNerd Evans40eaf962010-04-22 03:32:22 +0000334 if (!skb->dev)
335 return 0;
336 A = skb->dev->type;
337 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000338 case BPF_S_ANC_RXHASH:
Tom Herbert61b905d2014-03-24 15:34:47 -0700339 A = skb->hash;
Eric Dumazetda2033c2010-11-30 21:45:56 +0000340 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000341 case BPF_S_ANC_CPU:
Eric Dumazetda2033c2010-11-30 21:45:56 +0000342 A = raw_smp_processor_id();
343 continue;
Eric Dumazetf3335032012-10-27 02:26:17 +0000344 case BPF_S_ANC_VLAN_TAG:
345 A = vlan_tx_tag_get(skb);
346 continue;
347 case BPF_S_ANC_VLAN_TAG_PRESENT:
348 A = !!vlan_tx_tag_present(skb);
349 continue;
Daniel Borkmann3e5289d2013-03-19 06:39:31 +0000350 case BPF_S_ANC_PAY_OFFSET:
351 A = __skb_get_poff(skb);
352 continue;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000353 case BPF_S_ANC_NLATTR: {
Patrick McHardy4738c1d2008-04-10 02:02:28 -0700354 struct nlattr *nla;
355
356 if (skb_is_nonlinear(skb))
357 return 0;
358 if (A > skb->len - sizeof(struct nlattr))
359 return 0;
360
361 nla = nla_find((struct nlattr *)&skb->data[A],
362 skb->len - A, X);
363 if (nla)
364 A = (void *)nla - (void *)skb->data;
365 else
366 A = 0;
367 continue;
368 }
Eric Dumazet12b16da2010-12-15 19:45:28 +0000369 case BPF_S_ANC_NLATTR_NEST: {
Pablo Neira Ayusod214c752008-11-20 00:49:27 -0800370 struct nlattr *nla;
371
372 if (skb_is_nonlinear(skb))
373 return 0;
374 if (A > skb->len - sizeof(struct nlattr))
375 return 0;
376
377 nla = (struct nlattr *)&skb->data[A];
378 if (nla->nla_len > A - skb->len)
379 return 0;
380
381 nla = nla_find_nested(nla, X);
382 if (nla)
383 A = (void *)nla - (void *)skb->data;
384 else
385 A = 0;
386 continue;
387 }
Will Drewry46b325c2012-04-12 16:47:52 -0500388#ifdef CONFIG_SECCOMP_FILTER
389 case BPF_S_ANC_SECCOMP_LD_W:
390 A = seccomp_bpf_load(fentry->k);
391 continue;
392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 default:
Joe Perches6c4a5cb2011-05-21 07:48:40 +0000394 WARN_RATELIMIT(1, "Unknown code:%u jt:%u tf:%u k:%u\n",
395 fentry->code, fentry->jt,
396 fentry->jf, fentry->k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 0;
398 }
399 }
400
401 return 0;
402}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700403EXPORT_SYMBOL(sk_run_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000405/*
406 * Security :
407 * A BPF program is able to use 16 cells of memory to store intermediate
408 * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter())
409 * As we dont want to clear mem[] array for each packet going through
410 * sk_run_filter(), we check that filter loaded by user never try to read
411 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300412 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000413 */
414static int check_load_and_stores(struct sock_filter *filter, int flen)
415{
416 u16 *masks, memvalid = 0; /* one bit per cell, 16 cells */
417 int pc, ret = 0;
418
419 BUILD_BUG_ON(BPF_MEMWORDS > 16);
420 masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
421 if (!masks)
422 return -ENOMEM;
423 memset(masks, 0xff, flen * sizeof(*masks));
424
425 for (pc = 0; pc < flen; pc++) {
426 memvalid &= masks[pc];
427
428 switch (filter[pc].code) {
429 case BPF_S_ST:
430 case BPF_S_STX:
431 memvalid |= (1 << filter[pc].k);
432 break;
433 case BPF_S_LD_MEM:
434 case BPF_S_LDX_MEM:
435 if (!(memvalid & (1 << filter[pc].k))) {
436 ret = -EINVAL;
437 goto error;
438 }
439 break;
440 case BPF_S_JMP_JA:
441 /* a jump must set masks on target */
442 masks[pc + 1 + filter[pc].k] &= memvalid;
443 memvalid = ~0;
444 break;
445 case BPF_S_JMP_JEQ_K:
446 case BPF_S_JMP_JEQ_X:
447 case BPF_S_JMP_JGE_K:
448 case BPF_S_JMP_JGE_X:
449 case BPF_S_JMP_JGT_K:
450 case BPF_S_JMP_JGT_X:
451 case BPF_S_JMP_JSET_X:
452 case BPF_S_JMP_JSET_K:
453 /* a jump must set masks on targets */
454 masks[pc + 1 + filter[pc].jt] &= memvalid;
455 masks[pc + 1 + filter[pc].jf] &= memvalid;
456 memvalid = ~0;
457 break;
458 }
459 }
460error:
461 kfree(masks);
462 return ret;
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/**
466 * sk_chk_filter - verify socket filter code
467 * @filter: filter to verify
468 * @flen: length of filter
469 *
470 * Check the user's filter code. If we let some ugly
471 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800472 * no references or jumps that are out of range, no illegal
473 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800475 * All jumps are forward as they are not signed.
476 *
477 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
Dan Carpenter4f25af22011-10-17 21:04:20 +0000479int sk_chk_filter(struct sock_filter *filter, unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Tetsuo Handacba328f2010-11-16 15:19:51 +0000481 /*
482 * Valid instructions are initialized to non-0.
483 * Invalid instructions are initialized to 0.
484 */
485 static const u8 codes[] = {
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000486 [BPF_ALU|BPF_ADD|BPF_K] = BPF_S_ALU_ADD_K,
487 [BPF_ALU|BPF_ADD|BPF_X] = BPF_S_ALU_ADD_X,
488 [BPF_ALU|BPF_SUB|BPF_K] = BPF_S_ALU_SUB_K,
489 [BPF_ALU|BPF_SUB|BPF_X] = BPF_S_ALU_SUB_X,
490 [BPF_ALU|BPF_MUL|BPF_K] = BPF_S_ALU_MUL_K,
491 [BPF_ALU|BPF_MUL|BPF_X] = BPF_S_ALU_MUL_X,
492 [BPF_ALU|BPF_DIV|BPF_X] = BPF_S_ALU_DIV_X,
Eric Dumazetb6069a92012-09-07 22:03:35 +0000493 [BPF_ALU|BPF_MOD|BPF_K] = BPF_S_ALU_MOD_K,
494 [BPF_ALU|BPF_MOD|BPF_X] = BPF_S_ALU_MOD_X,
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000495 [BPF_ALU|BPF_AND|BPF_K] = BPF_S_ALU_AND_K,
496 [BPF_ALU|BPF_AND|BPF_X] = BPF_S_ALU_AND_X,
497 [BPF_ALU|BPF_OR|BPF_K] = BPF_S_ALU_OR_K,
498 [BPF_ALU|BPF_OR|BPF_X] = BPF_S_ALU_OR_X,
Daniel Borkmann9e49e882012-09-24 02:23:59 +0000499 [BPF_ALU|BPF_XOR|BPF_K] = BPF_S_ALU_XOR_K,
500 [BPF_ALU|BPF_XOR|BPF_X] = BPF_S_ALU_XOR_X,
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000501 [BPF_ALU|BPF_LSH|BPF_K] = BPF_S_ALU_LSH_K,
502 [BPF_ALU|BPF_LSH|BPF_X] = BPF_S_ALU_LSH_X,
503 [BPF_ALU|BPF_RSH|BPF_K] = BPF_S_ALU_RSH_K,
504 [BPF_ALU|BPF_RSH|BPF_X] = BPF_S_ALU_RSH_X,
505 [BPF_ALU|BPF_NEG] = BPF_S_ALU_NEG,
506 [BPF_LD|BPF_W|BPF_ABS] = BPF_S_LD_W_ABS,
507 [BPF_LD|BPF_H|BPF_ABS] = BPF_S_LD_H_ABS,
508 [BPF_LD|BPF_B|BPF_ABS] = BPF_S_LD_B_ABS,
509 [BPF_LD|BPF_W|BPF_LEN] = BPF_S_LD_W_LEN,
510 [BPF_LD|BPF_W|BPF_IND] = BPF_S_LD_W_IND,
511 [BPF_LD|BPF_H|BPF_IND] = BPF_S_LD_H_IND,
512 [BPF_LD|BPF_B|BPF_IND] = BPF_S_LD_B_IND,
513 [BPF_LD|BPF_IMM] = BPF_S_LD_IMM,
514 [BPF_LDX|BPF_W|BPF_LEN] = BPF_S_LDX_W_LEN,
515 [BPF_LDX|BPF_B|BPF_MSH] = BPF_S_LDX_B_MSH,
516 [BPF_LDX|BPF_IMM] = BPF_S_LDX_IMM,
517 [BPF_MISC|BPF_TAX] = BPF_S_MISC_TAX,
518 [BPF_MISC|BPF_TXA] = BPF_S_MISC_TXA,
519 [BPF_RET|BPF_K] = BPF_S_RET_K,
520 [BPF_RET|BPF_A] = BPF_S_RET_A,
521 [BPF_ALU|BPF_DIV|BPF_K] = BPF_S_ALU_DIV_K,
522 [BPF_LD|BPF_MEM] = BPF_S_LD_MEM,
523 [BPF_LDX|BPF_MEM] = BPF_S_LDX_MEM,
524 [BPF_ST] = BPF_S_ST,
525 [BPF_STX] = BPF_S_STX,
526 [BPF_JMP|BPF_JA] = BPF_S_JMP_JA,
527 [BPF_JMP|BPF_JEQ|BPF_K] = BPF_S_JMP_JEQ_K,
528 [BPF_JMP|BPF_JEQ|BPF_X] = BPF_S_JMP_JEQ_X,
529 [BPF_JMP|BPF_JGE|BPF_K] = BPF_S_JMP_JGE_K,
530 [BPF_JMP|BPF_JGE|BPF_X] = BPF_S_JMP_JGE_X,
531 [BPF_JMP|BPF_JGT|BPF_K] = BPF_S_JMP_JGT_K,
532 [BPF_JMP|BPF_JGT|BPF_X] = BPF_S_JMP_JGT_X,
533 [BPF_JMP|BPF_JSET|BPF_K] = BPF_S_JMP_JSET_K,
534 [BPF_JMP|BPF_JSET|BPF_X] = BPF_S_JMP_JSET_X,
Tetsuo Handacba328f2010-11-16 15:19:51 +0000535 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int pc;
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000537 bool anc_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David S. Miller1b93ae642005-12-27 13:57:59 -0800539 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EINVAL;
541
542 /* check the filter code now */
543 for (pc = 0; pc < flen; pc++) {
Tetsuo Handacba328f2010-11-16 15:19:51 +0000544 struct sock_filter *ftest = &filter[pc];
545 u16 code = ftest->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Tetsuo Handacba328f2010-11-16 15:19:51 +0000547 if (code >= ARRAY_SIZE(codes))
548 return -EINVAL;
549 code = codes[code];
Eric Dumazet8c1592d2010-11-18 21:56:38 +0000550 if (!code)
Tetsuo Handacba328f2010-11-16 15:19:51 +0000551 return -EINVAL;
Kris Katterjohn93699862006-01-04 13:58:36 -0800552 /* Some instructions need special checks */
Tetsuo Handacba328f2010-11-16 15:19:51 +0000553 switch (code) {
554 case BPF_S_ALU_DIV_K:
Eric Dumazetb6069a92012-09-07 22:03:35 +0000555 case BPF_S_ALU_MOD_K:
556 /* check for division by zero */
557 if (ftest->k == 0)
558 return -EINVAL;
559 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000560 case BPF_S_LD_MEM:
561 case BPF_S_LDX_MEM:
562 case BPF_S_ST:
563 case BPF_S_STX:
564 /* check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800565 if (ftest->k >= BPF_MEMWORDS)
566 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000567 break;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000568 case BPF_S_JMP_JA:
Kris Katterjohn93699862006-01-04 13:58:36 -0800569 /*
570 * Note, the large ftest->k might cause loops.
571 * Compare this with conditional jumps below,
572 * where offsets are limited. --ANK (981016)
573 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000574 if (ftest->k >= (unsigned int)(flen-pc-1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800575 return -EINVAL;
576 break;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000577 case BPF_S_JMP_JEQ_K:
578 case BPF_S_JMP_JEQ_X:
579 case BPF_S_JMP_JGE_K:
580 case BPF_S_JMP_JGE_X:
581 case BPF_S_JMP_JGT_K:
582 case BPF_S_JMP_JGT_X:
583 case BPF_S_JMP_JSET_X:
584 case BPF_S_JMP_JSET_K:
Tetsuo Handacba328f2010-11-16 15:19:51 +0000585 /* for conditionals both must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000586 if (pc + ftest->jt + 1 >= flen ||
587 pc + ftest->jf + 1 >= flen)
588 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000589 break;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000590 case BPF_S_LD_W_ABS:
591 case BPF_S_LD_H_ABS:
592 case BPF_S_LD_B_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000593 anc_found = false;
Eric Dumazet12b16da2010-12-15 19:45:28 +0000594#define ANCILLARY(CODE) case SKF_AD_OFF + SKF_AD_##CODE: \
595 code = BPF_S_ANC_##CODE; \
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000596 anc_found = true; \
Eric Dumazet12b16da2010-12-15 19:45:28 +0000597 break
598 switch (ftest->k) {
599 ANCILLARY(PROTOCOL);
600 ANCILLARY(PKTTYPE);
601 ANCILLARY(IFINDEX);
602 ANCILLARY(NLATTR);
603 ANCILLARY(NLATTR_NEST);
604 ANCILLARY(MARK);
605 ANCILLARY(QUEUE);
606 ANCILLARY(HATYPE);
607 ANCILLARY(RXHASH);
608 ANCILLARY(CPU);
Jiri Pirkoffe06c12012-03-31 11:01:20 +0000609 ANCILLARY(ALU_XOR_X);
Eric Dumazetf3335032012-10-27 02:26:17 +0000610 ANCILLARY(VLAN_TAG);
611 ANCILLARY(VLAN_TAG_PRESENT);
Daniel Borkmann3e5289d2013-03-19 06:39:31 +0000612 ANCILLARY(PAY_OFFSET);
Eric Dumazet12b16da2010-12-15 19:45:28 +0000613 }
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000614
615 /* ancillary operation unknown or unsupported */
616 if (anc_found == false && ftest->k >= SKF_AD_OFF)
617 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000618 }
Tetsuo Handacba328f2010-11-16 15:19:51 +0000619 ftest->code = code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000622 /* last instruction must be a RET code */
623 switch (filter[flen - 1].code) {
624 case BPF_S_RET_K:
625 case BPF_S_RET_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000626 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000627 }
628 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
Stephen Hemmingerb7156312008-04-10 01:33:47 -0700630EXPORT_SYMBOL(sk_chk_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800633 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700634 * @rcu: rcu_head that contains the sk_filter to free
635 */
Eric Dumazet46bcf142010-12-06 09:29:43 -0800636void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700637{
638 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
639
Eric Dumazet0a148422011-04-20 09:27:32 +0000640 bpf_jit_free(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700641}
Eric Dumazet46bcf142010-12-06 09:29:43 -0800642EXPORT_SYMBOL(sk_filter_release_rcu);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700643
Jiri Pirko302d6632012-03-31 11:01:19 +0000644static int __sk_prepare_filter(struct sk_filter *fp)
645{
646 int err;
647
648 fp->bpf_func = sk_run_filter;
Daniel Borkmannf8bbbfc2014-03-28 18:58:18 +0100649 fp->jited = 0;
Jiri Pirko302d6632012-03-31 11:01:19 +0000650
651 err = sk_chk_filter(fp->insns, fp->len);
652 if (err)
653 return err;
654
655 bpf_jit_compile(fp);
656 return 0;
657}
658
659/**
660 * sk_unattached_filter_create - create an unattached filter
661 * @fprog: the filter program
Randy Dunlapc6c4b972012-06-08 14:01:44 +0000662 * @pfp: the unattached filter that is created
Jiri Pirko302d6632012-03-31 11:01:19 +0000663 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +0000664 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +0000665 * sanity checks on it to make sure it does not explode on us later.
666 * If an error occurs or there is insufficient memory for the filter
667 * a negative errno code is returned. On success the return is zero.
668 */
669int sk_unattached_filter_create(struct sk_filter **pfp,
670 struct sock_fprog *fprog)
671{
672 struct sk_filter *fp;
673 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
674 int err;
675
676 /* Make sure new filter is there and in the right amounts. */
677 if (fprog->filter == NULL)
678 return -EINVAL;
679
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700680 fp = kmalloc(sk_filter_size(fprog->len), GFP_KERNEL);
Jiri Pirko302d6632012-03-31 11:01:19 +0000681 if (!fp)
682 return -ENOMEM;
683 memcpy(fp->insns, fprog->filter, fsize);
684
685 atomic_set(&fp->refcnt, 1);
686 fp->len = fprog->len;
687
688 err = __sk_prepare_filter(fp);
689 if (err)
690 goto free_mem;
691
692 *pfp = fp;
693 return 0;
694free_mem:
695 kfree(fp);
696 return err;
697}
698EXPORT_SYMBOL_GPL(sk_unattached_filter_create);
699
700void sk_unattached_filter_destroy(struct sk_filter *fp)
701{
702 sk_filter_release(fp);
703}
704EXPORT_SYMBOL_GPL(sk_unattached_filter_destroy);
705
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700706/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * sk_attach_filter - attach a socket filter
708 * @fprog: the filter program
709 * @sk: the socket to use
710 *
711 * Attach the user's filter code. We first run some sanity checks on
712 * it to make sure it does not explode on us later. If an error
713 * occurs or there is insufficient memory for the filter a negative
714 * errno code is returned. On success the return is zero.
715 */
716int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
717{
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700718 struct sk_filter *fp, *old_fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned int fsize = sizeof(struct sock_filter) * fprog->len;
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700720 unsigned int sk_fsize = sk_filter_size(fprog->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int err;
722
Vincent Bernatd59577b2013-01-16 22:55:49 +0100723 if (sock_flag(sk, SOCK_FILTER_LOCKED))
724 return -EPERM;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Make sure new filter is there and in the right amounts. */
Kris Katterjohne35bedf2006-01-17 02:25:52 -0800727 if (fprog->filter == NULL)
728 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700730 fp = sock_kmalloc(sk, sk_fsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!fp)
732 return -ENOMEM;
733 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -0700734 sock_kfree_s(sk, fp, sk_fsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EFAULT;
736 }
737
738 atomic_set(&fp->refcnt, 1);
739 fp->len = fprog->len;
740
Jiri Pirko302d6632012-03-31 11:01:19 +0000741 err = __sk_prepare_filter(fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700742 if (err) {
743 sk_filter_uncharge(sk, fp);
744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000747 old_fp = rcu_dereference_protected(sk->sk_filter,
748 sock_owned_by_user(sk));
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700749 rcu_assign_pointer(sk->sk_filter, fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700750
Olof Johansson9b013e02007-10-18 21:48:39 -0700751 if (old_fp)
Eric Dumazet46bcf142010-12-06 09:29:43 -0800752 sk_filter_uncharge(sk, old_fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -0700753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000755EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700757int sk_detach_filter(struct sock *sk)
758{
759 int ret = -ENOENT;
760 struct sk_filter *filter;
761
Vincent Bernatd59577b2013-01-16 22:55:49 +0100762 if (sock_flag(sk, SOCK_FILTER_LOCKED))
763 return -EPERM;
764
Eric Dumazetf91ff5b2010-09-27 06:07:30 +0000765 filter = rcu_dereference_protected(sk->sk_filter,
766 sock_owned_by_user(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700767 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000768 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -0800769 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700770 ret = 0;
771 }
Pavel Emelyanov55b33322007-10-17 21:21:26 -0700772 return ret;
773}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +0000774EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +0000775
Nicolas Dichteled139982013-06-05 15:30:55 +0200776void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +0000777{
778 static const u16 decodes[] = {
779 [BPF_S_ALU_ADD_K] = BPF_ALU|BPF_ADD|BPF_K,
780 [BPF_S_ALU_ADD_X] = BPF_ALU|BPF_ADD|BPF_X,
781 [BPF_S_ALU_SUB_K] = BPF_ALU|BPF_SUB|BPF_K,
782 [BPF_S_ALU_SUB_X] = BPF_ALU|BPF_SUB|BPF_X,
783 [BPF_S_ALU_MUL_K] = BPF_ALU|BPF_MUL|BPF_K,
784 [BPF_S_ALU_MUL_X] = BPF_ALU|BPF_MUL|BPF_X,
785 [BPF_S_ALU_DIV_X] = BPF_ALU|BPF_DIV|BPF_X,
786 [BPF_S_ALU_MOD_K] = BPF_ALU|BPF_MOD|BPF_K,
787 [BPF_S_ALU_MOD_X] = BPF_ALU|BPF_MOD|BPF_X,
788 [BPF_S_ALU_AND_K] = BPF_ALU|BPF_AND|BPF_K,
789 [BPF_S_ALU_AND_X] = BPF_ALU|BPF_AND|BPF_X,
790 [BPF_S_ALU_OR_K] = BPF_ALU|BPF_OR|BPF_K,
791 [BPF_S_ALU_OR_X] = BPF_ALU|BPF_OR|BPF_X,
792 [BPF_S_ALU_XOR_K] = BPF_ALU|BPF_XOR|BPF_K,
793 [BPF_S_ALU_XOR_X] = BPF_ALU|BPF_XOR|BPF_X,
794 [BPF_S_ALU_LSH_K] = BPF_ALU|BPF_LSH|BPF_K,
795 [BPF_S_ALU_LSH_X] = BPF_ALU|BPF_LSH|BPF_X,
796 [BPF_S_ALU_RSH_K] = BPF_ALU|BPF_RSH|BPF_K,
797 [BPF_S_ALU_RSH_X] = BPF_ALU|BPF_RSH|BPF_X,
798 [BPF_S_ALU_NEG] = BPF_ALU|BPF_NEG,
799 [BPF_S_LD_W_ABS] = BPF_LD|BPF_W|BPF_ABS,
800 [BPF_S_LD_H_ABS] = BPF_LD|BPF_H|BPF_ABS,
801 [BPF_S_LD_B_ABS] = BPF_LD|BPF_B|BPF_ABS,
802 [BPF_S_ANC_PROTOCOL] = BPF_LD|BPF_B|BPF_ABS,
803 [BPF_S_ANC_PKTTYPE] = BPF_LD|BPF_B|BPF_ABS,
804 [BPF_S_ANC_IFINDEX] = BPF_LD|BPF_B|BPF_ABS,
805 [BPF_S_ANC_NLATTR] = BPF_LD|BPF_B|BPF_ABS,
806 [BPF_S_ANC_NLATTR_NEST] = BPF_LD|BPF_B|BPF_ABS,
807 [BPF_S_ANC_MARK] = BPF_LD|BPF_B|BPF_ABS,
808 [BPF_S_ANC_QUEUE] = BPF_LD|BPF_B|BPF_ABS,
809 [BPF_S_ANC_HATYPE] = BPF_LD|BPF_B|BPF_ABS,
810 [BPF_S_ANC_RXHASH] = BPF_LD|BPF_B|BPF_ABS,
811 [BPF_S_ANC_CPU] = BPF_LD|BPF_B|BPF_ABS,
812 [BPF_S_ANC_ALU_XOR_X] = BPF_LD|BPF_B|BPF_ABS,
813 [BPF_S_ANC_SECCOMP_LD_W] = BPF_LD|BPF_B|BPF_ABS,
814 [BPF_S_ANC_VLAN_TAG] = BPF_LD|BPF_B|BPF_ABS,
815 [BPF_S_ANC_VLAN_TAG_PRESENT] = BPF_LD|BPF_B|BPF_ABS,
Daniel Borkmann3e5289d2013-03-19 06:39:31 +0000816 [BPF_S_ANC_PAY_OFFSET] = BPF_LD|BPF_B|BPF_ABS,
Pavel Emelyanova8fc9272012-11-01 02:01:48 +0000817 [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;
Eric Dumazetaee636c2014-01-15 06:50:07 -0800851 to->k = filt->k;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +0000852}
853
854int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, unsigned int len)
855{
856 struct sk_filter *filter;
857 int i, ret;
858
859 lock_sock(sk);
860 filter = rcu_dereference_protected(sk->sk_filter,
861 sock_owned_by_user(sk));
862 ret = 0;
863 if (!filter)
864 goto out;
865 ret = filter->len;
866 if (!len)
867 goto out;
868 ret = -EINVAL;
869 if (len < filter->len)
870 goto out;
871
872 ret = -EFAULT;
873 for (i = 0; i < filter->len; i++) {
874 struct sock_filter fb;
875
876 sk_decode_filter(&filter->insns[i], &fb);
877 if (copy_to_user(&ubuf[i], &fb, sizeof(fb)))
878 goto out;
879 }
880
881 ret = filter->len;
882out:
883 release_sock(sk);
884 return ret;
885}