]> git.ipfire.org Git - thirdparty/linux.git/blame - net/core/filter.c
Merge branch 'cxgb4-fcoe'
[thirdparty/linux.git] / net / core / filter.c
CommitLineData
1da177e4
LT
1/*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
bd4cf0ed
AS
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
1da177e4 6 *
bd4cf0ed
AS
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
8 *
9 * Authors:
10 *
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
1da177e4
LT
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 * Andi Kleen - Fix a few bad bugs and races.
4df95ff4 21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
1da177e4
LT
22 */
23
24#include <linux/module.h>
25#include <linux/types.h>
1da177e4
LT
26#include <linux/mm.h>
27#include <linux/fcntl.h>
28#include <linux/socket.h>
29#include <linux/in.h>
30#include <linux/inet.h>
31#include <linux/netdevice.h>
32#include <linux/if_packet.h>
5a0e3ad6 33#include <linux/gfp.h>
1da177e4
LT
34#include <net/ip.h>
35#include <net/protocol.h>
4738c1db 36#include <net/netlink.h>
1da177e4
LT
37#include <linux/skbuff.h>
38#include <net/sock.h>
39#include <linux/errno.h>
40#include <linux/timer.h>
1da177e4 41#include <asm/uaccess.h>
40daafc8 42#include <asm/unaligned.h>
1da177e4 43#include <linux/filter.h>
86e4ca66 44#include <linux/ratelimit.h>
46b325c7 45#include <linux/seccomp.h>
f3335031 46#include <linux/if_vlan.h>
89aa0758 47#include <linux/bpf.h>
1da177e4 48
43db6d65
SH
49/**
50 * sk_filter - run a packet through a socket filter
51 * @sk: sock associated with &sk_buff
52 * @skb: buffer to filter
43db6d65
SH
53 *
54 * Run the filter code and then cut skb->data to correct size returned by
8ea6e345 55 * SK_RUN_FILTER. If pkt_len is 0 we toss packet. If skb->len is smaller
43db6d65 56 * than pkt_len we keep whole skb->data. This is the socket level
8ea6e345 57 * wrapper to SK_RUN_FILTER. It returns 0 if the packet should
43db6d65
SH
58 * be accepted or -EPERM if the packet should be tossed.
59 *
60 */
61int sk_filter(struct sock *sk, struct sk_buff *skb)
62{
63 int err;
64 struct sk_filter *filter;
65
c93bdd0e
MG
66 /*
67 * If the skb was allocated from pfmemalloc reserves, only
68 * allow SOCK_MEMALLOC sockets to use it as this socket is
69 * helping free memory
70 */
71 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
72 return -ENOMEM;
73
43db6d65
SH
74 err = security_sock_rcv_skb(sk, skb);
75 if (err)
76 return err;
77
80f8f102
ED
78 rcu_read_lock();
79 filter = rcu_dereference(sk->sk_filter);
43db6d65 80 if (filter) {
0a14842f 81 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
0d7da9dd 82
43db6d65
SH
83 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
84 }
80f8f102 85 rcu_read_unlock();
43db6d65
SH
86
87 return err;
88}
89EXPORT_SYMBOL(sk_filter);
90
30743837 91static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
bd4cf0ed 92{
56193d1b 93 return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
bd4cf0ed
AS
94}
95
30743837 96static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
bd4cf0ed 97{
eb9672f4 98 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
bd4cf0ed
AS
99 struct nlattr *nla;
100
101 if (skb_is_nonlinear(skb))
102 return 0;
103
05ab8f26
MK
104 if (skb->len < sizeof(struct nlattr))
105 return 0;
106
30743837 107 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
108 return 0;
109
30743837 110 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
bd4cf0ed
AS
111 if (nla)
112 return (void *) nla - (void *) skb->data;
113
114 return 0;
115}
116
30743837 117static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
bd4cf0ed 118{
eb9672f4 119 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
bd4cf0ed
AS
120 struct nlattr *nla;
121
122 if (skb_is_nonlinear(skb))
123 return 0;
124
05ab8f26
MK
125 if (skb->len < sizeof(struct nlattr))
126 return 0;
127
30743837 128 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
129 return 0;
130
30743837
DB
131 nla = (struct nlattr *) &skb->data[a];
132 if (nla->nla_len > skb->len - a)
bd4cf0ed
AS
133 return 0;
134
30743837 135 nla = nla_find_nested(nla, x);
bd4cf0ed
AS
136 if (nla)
137 return (void *) nla - (void *) skb->data;
138
139 return 0;
140}
141
30743837 142static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
bd4cf0ed
AS
143{
144 return raw_smp_processor_id();
145}
146
4cd3675e 147/* note that this only generates 32-bit random numbers */
30743837 148static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
4cd3675e 149{
eb9672f4 150 return prandom_u32();
4cd3675e
CG
151}
152
9bac3d6d
AS
153static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
154 struct bpf_insn *insn_buf)
155{
156 struct bpf_insn *insn = insn_buf;
157
158 switch (skb_field) {
159 case SKF_AD_MARK:
160 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
161
162 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
163 offsetof(struct sk_buff, mark));
164 break;
165
166 case SKF_AD_PKTTYPE:
167 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
168 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
169#ifdef __BIG_ENDIAN_BITFIELD
170 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
171#endif
172 break;
173
174 case SKF_AD_QUEUE:
175 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
176
177 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
178 offsetof(struct sk_buff, queue_mapping));
179 break;
c2497395 180
c2497395
AS
181 case SKF_AD_VLAN_TAG:
182 case SKF_AD_VLAN_TAG_PRESENT:
183 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
184 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
185
186 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
187 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
188 offsetof(struct sk_buff, vlan_tci));
189 if (skb_field == SKF_AD_VLAN_TAG) {
190 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
191 ~VLAN_TAG_PRESENT);
192 } else {
193 /* dst_reg >>= 12 */
194 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
195 /* dst_reg &= 1 */
196 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
197 }
198 break;
9bac3d6d
AS
199 }
200
201 return insn - insn_buf;
202}
203
bd4cf0ed 204static bool convert_bpf_extensions(struct sock_filter *fp,
2695fb55 205 struct bpf_insn **insnp)
bd4cf0ed 206{
2695fb55 207 struct bpf_insn *insn = *insnp;
9bac3d6d 208 u32 cnt;
bd4cf0ed
AS
209
210 switch (fp->k) {
211 case SKF_AD_OFF + SKF_AD_PROTOCOL:
0b8c707d
DB
212 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
213
214 /* A = *(u16 *) (CTX + offsetof(protocol)) */
215 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
216 offsetof(struct sk_buff, protocol));
217 /* A = ntohs(A) [emitting a nop or swap16] */
218 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
bd4cf0ed
AS
219 break;
220
221 case SKF_AD_OFF + SKF_AD_PKTTYPE:
9bac3d6d
AS
222 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
223 insn += cnt - 1;
bd4cf0ed
AS
224 break;
225
226 case SKF_AD_OFF + SKF_AD_IFINDEX:
227 case SKF_AD_OFF + SKF_AD_HATYPE:
bd4cf0ed
AS
228 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
229 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
f8f6d679
DB
230 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
231
232 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
233 BPF_REG_TMP, BPF_REG_CTX,
234 offsetof(struct sk_buff, dev));
235 /* if (tmp != 0) goto pc + 1 */
236 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
237 *insn++ = BPF_EXIT_INSN();
238 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
239 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
240 offsetof(struct net_device, ifindex));
241 else
242 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
243 offsetof(struct net_device, type));
bd4cf0ed
AS
244 break;
245
246 case SKF_AD_OFF + SKF_AD_MARK:
9bac3d6d
AS
247 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
248 insn += cnt - 1;
bd4cf0ed
AS
249 break;
250
251 case SKF_AD_OFF + SKF_AD_RXHASH:
252 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
253
9739eef1
AS
254 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
255 offsetof(struct sk_buff, hash));
bd4cf0ed
AS
256 break;
257
258 case SKF_AD_OFF + SKF_AD_QUEUE:
9bac3d6d
AS
259 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
260 insn += cnt - 1;
bd4cf0ed
AS
261 break;
262
263 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
c2497395
AS
264 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
265 BPF_REG_A, BPF_REG_CTX, insn);
266 insn += cnt - 1;
267 break;
bd4cf0ed 268
c2497395
AS
269 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
270 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
271 BPF_REG_A, BPF_REG_CTX, insn);
272 insn += cnt - 1;
bd4cf0ed
AS
273 break;
274
275 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
276 case SKF_AD_OFF + SKF_AD_NLATTR:
277 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
278 case SKF_AD_OFF + SKF_AD_CPU:
4cd3675e 279 case SKF_AD_OFF + SKF_AD_RANDOM:
e430f34e 280 /* arg1 = CTX */
f8f6d679 281 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
bd4cf0ed 282 /* arg2 = A */
f8f6d679 283 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
bd4cf0ed 284 /* arg3 = X */
f8f6d679 285 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
e430f34e 286 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
bd4cf0ed
AS
287 switch (fp->k) {
288 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
f8f6d679 289 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
bd4cf0ed
AS
290 break;
291 case SKF_AD_OFF + SKF_AD_NLATTR:
f8f6d679 292 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
bd4cf0ed
AS
293 break;
294 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
f8f6d679 295 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
bd4cf0ed
AS
296 break;
297 case SKF_AD_OFF + SKF_AD_CPU:
f8f6d679 298 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
bd4cf0ed 299 break;
4cd3675e 300 case SKF_AD_OFF + SKF_AD_RANDOM:
f8f6d679 301 *insn = BPF_EMIT_CALL(__get_random_u32);
4cd3675e 302 break;
bd4cf0ed
AS
303 }
304 break;
305
306 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
9739eef1
AS
307 /* A ^= X */
308 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
309 break;
310
311 default:
312 /* This is just a dummy call to avoid letting the compiler
313 * evict __bpf_call_base() as an optimization. Placed here
314 * where no-one bothers.
315 */
316 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
317 return false;
318 }
319
320 *insnp = insn;
321 return true;
322}
323
324/**
8fb575ca 325 * bpf_convert_filter - convert filter program
bd4cf0ed
AS
326 * @prog: the user passed filter program
327 * @len: the length of the user passed filter program
328 * @new_prog: buffer where converted program will be stored
329 * @new_len: pointer to store length of converted program
330 *
331 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
332 * Conversion workflow:
333 *
334 * 1) First pass for calculating the new program length:
8fb575ca 335 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
bd4cf0ed
AS
336 *
337 * 2) 2nd pass to remap in two passes: 1st pass finds new
338 * jump offsets, 2nd pass remapping:
2695fb55 339 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
8fb575ca 340 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
bd4cf0ed
AS
341 *
342 * User BPF's register A is mapped to our BPF register 6, user BPF
343 * register X is mapped to BPF register 7; frame pointer is always
344 * register 10; Context 'void *ctx' is stored in register 1, that is,
345 * for socket filters: ctx == 'struct sk_buff *', for seccomp:
346 * ctx == 'struct seccomp_data *'.
347 */
8fb575ca
AS
348int bpf_convert_filter(struct sock_filter *prog, int len,
349 struct bpf_insn *new_prog, int *new_len)
bd4cf0ed
AS
350{
351 int new_flen = 0, pass = 0, target, i;
2695fb55 352 struct bpf_insn *new_insn;
bd4cf0ed
AS
353 struct sock_filter *fp;
354 int *addrs = NULL;
355 u8 bpf_src;
356
357 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
30743837 358 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
bd4cf0ed 359
6f9a093b 360 if (len <= 0 || len > BPF_MAXINSNS)
bd4cf0ed
AS
361 return -EINVAL;
362
363 if (new_prog) {
99e72a0f 364 addrs = kcalloc(len, sizeof(*addrs), GFP_KERNEL);
bd4cf0ed
AS
365 if (!addrs)
366 return -ENOMEM;
367 }
368
369do_pass:
370 new_insn = new_prog;
371 fp = prog;
372
f8f6d679
DB
373 if (new_insn)
374 *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
bd4cf0ed
AS
375 new_insn++;
376
377 for (i = 0; i < len; fp++, i++) {
2695fb55
AS
378 struct bpf_insn tmp_insns[6] = { };
379 struct bpf_insn *insn = tmp_insns;
bd4cf0ed
AS
380
381 if (addrs)
382 addrs[i] = new_insn - new_prog;
383
384 switch (fp->code) {
385 /* All arithmetic insns and skb loads map as-is. */
386 case BPF_ALU | BPF_ADD | BPF_X:
387 case BPF_ALU | BPF_ADD | BPF_K:
388 case BPF_ALU | BPF_SUB | BPF_X:
389 case BPF_ALU | BPF_SUB | BPF_K:
390 case BPF_ALU | BPF_AND | BPF_X:
391 case BPF_ALU | BPF_AND | BPF_K:
392 case BPF_ALU | BPF_OR | BPF_X:
393 case BPF_ALU | BPF_OR | BPF_K:
394 case BPF_ALU | BPF_LSH | BPF_X:
395 case BPF_ALU | BPF_LSH | BPF_K:
396 case BPF_ALU | BPF_RSH | BPF_X:
397 case BPF_ALU | BPF_RSH | BPF_K:
398 case BPF_ALU | BPF_XOR | BPF_X:
399 case BPF_ALU | BPF_XOR | BPF_K:
400 case BPF_ALU | BPF_MUL | BPF_X:
401 case BPF_ALU | BPF_MUL | BPF_K:
402 case BPF_ALU | BPF_DIV | BPF_X:
403 case BPF_ALU | BPF_DIV | BPF_K:
404 case BPF_ALU | BPF_MOD | BPF_X:
405 case BPF_ALU | BPF_MOD | BPF_K:
406 case BPF_ALU | BPF_NEG:
407 case BPF_LD | BPF_ABS | BPF_W:
408 case BPF_LD | BPF_ABS | BPF_H:
409 case BPF_LD | BPF_ABS | BPF_B:
410 case BPF_LD | BPF_IND | BPF_W:
411 case BPF_LD | BPF_IND | BPF_H:
412 case BPF_LD | BPF_IND | BPF_B:
413 /* Check for overloaded BPF extension and
414 * directly convert it if found, otherwise
415 * just move on with mapping.
416 */
417 if (BPF_CLASS(fp->code) == BPF_LD &&
418 BPF_MODE(fp->code) == BPF_ABS &&
419 convert_bpf_extensions(fp, &insn))
420 break;
421
f8f6d679 422 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
bd4cf0ed
AS
423 break;
424
f8f6d679
DB
425 /* Jump transformation cannot use BPF block macros
426 * everywhere as offset calculation and target updates
427 * require a bit more work than the rest, i.e. jump
428 * opcodes map as-is, but offsets need adjustment.
429 */
430
431#define BPF_EMIT_JMP \
bd4cf0ed
AS
432 do { \
433 if (target >= len || target < 0) \
434 goto err; \
435 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
436 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
437 insn->off -= insn - tmp_insns; \
438 } while (0)
439
f8f6d679
DB
440 case BPF_JMP | BPF_JA:
441 target = i + fp->k + 1;
442 insn->code = fp->code;
443 BPF_EMIT_JMP;
bd4cf0ed
AS
444 break;
445
446 case BPF_JMP | BPF_JEQ | BPF_K:
447 case BPF_JMP | BPF_JEQ | BPF_X:
448 case BPF_JMP | BPF_JSET | BPF_K:
449 case BPF_JMP | BPF_JSET | BPF_X:
450 case BPF_JMP | BPF_JGT | BPF_K:
451 case BPF_JMP | BPF_JGT | BPF_X:
452 case BPF_JMP | BPF_JGE | BPF_K:
453 case BPF_JMP | BPF_JGE | BPF_X:
454 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
455 /* BPF immediates are signed, zero extend
456 * immediate into tmp register and use it
457 * in compare insn.
458 */
f8f6d679 459 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
bd4cf0ed 460
e430f34e
AS
461 insn->dst_reg = BPF_REG_A;
462 insn->src_reg = BPF_REG_TMP;
bd4cf0ed
AS
463 bpf_src = BPF_X;
464 } else {
e430f34e
AS
465 insn->dst_reg = BPF_REG_A;
466 insn->src_reg = BPF_REG_X;
bd4cf0ed
AS
467 insn->imm = fp->k;
468 bpf_src = BPF_SRC(fp->code);
1da177e4 469 }
bd4cf0ed
AS
470
471 /* Common case where 'jump_false' is next insn. */
472 if (fp->jf == 0) {
473 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
474 target = i + fp->jt + 1;
f8f6d679 475 BPF_EMIT_JMP;
bd4cf0ed 476 break;
1da177e4 477 }
bd4cf0ed
AS
478
479 /* Convert JEQ into JNE when 'jump_true' is next insn. */
480 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
481 insn->code = BPF_JMP | BPF_JNE | bpf_src;
482 target = i + fp->jf + 1;
f8f6d679 483 BPF_EMIT_JMP;
bd4cf0ed 484 break;
0b05b2a4 485 }
bd4cf0ed
AS
486
487 /* Other jumps are mapped into two insns: Jxx and JA. */
488 target = i + fp->jt + 1;
489 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
f8f6d679 490 BPF_EMIT_JMP;
bd4cf0ed
AS
491 insn++;
492
493 insn->code = BPF_JMP | BPF_JA;
494 target = i + fp->jf + 1;
f8f6d679 495 BPF_EMIT_JMP;
bd4cf0ed
AS
496 break;
497
498 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
499 case BPF_LDX | BPF_MSH | BPF_B:
9739eef1 500 /* tmp = A */
f8f6d679 501 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
1268e253 502 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
f8f6d679 503 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
9739eef1 504 /* A &= 0xf */
f8f6d679 505 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
9739eef1 506 /* A <<= 2 */
f8f6d679 507 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
9739eef1 508 /* X = A */
f8f6d679 509 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
9739eef1 510 /* A = tmp */
f8f6d679 511 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
bd4cf0ed
AS
512 break;
513
514 /* RET_K, RET_A are remaped into 2 insns. */
515 case BPF_RET | BPF_A:
516 case BPF_RET | BPF_K:
f8f6d679
DB
517 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
518 BPF_K : BPF_X, BPF_REG_0,
519 BPF_REG_A, fp->k);
9739eef1 520 *insn = BPF_EXIT_INSN();
bd4cf0ed
AS
521 break;
522
523 /* Store to stack. */
524 case BPF_ST:
525 case BPF_STX:
f8f6d679
DB
526 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
527 BPF_ST ? BPF_REG_A : BPF_REG_X,
528 -(BPF_MEMWORDS - fp->k) * 4);
bd4cf0ed
AS
529 break;
530
531 /* Load from stack. */
532 case BPF_LD | BPF_MEM:
533 case BPF_LDX | BPF_MEM:
f8f6d679
DB
534 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
535 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
536 -(BPF_MEMWORDS - fp->k) * 4);
bd4cf0ed
AS
537 break;
538
539 /* A = K or X = K */
540 case BPF_LD | BPF_IMM:
541 case BPF_LDX | BPF_IMM:
f8f6d679
DB
542 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
543 BPF_REG_A : BPF_REG_X, fp->k);
bd4cf0ed
AS
544 break;
545
546 /* X = A */
547 case BPF_MISC | BPF_TAX:
f8f6d679 548 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
bd4cf0ed
AS
549 break;
550
551 /* A = X */
552 case BPF_MISC | BPF_TXA:
f8f6d679 553 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
554 break;
555
556 /* A = skb->len or X = skb->len */
557 case BPF_LD | BPF_W | BPF_LEN:
558 case BPF_LDX | BPF_W | BPF_LEN:
f8f6d679
DB
559 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
560 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
561 offsetof(struct sk_buff, len));
bd4cf0ed
AS
562 break;
563
f8f6d679 564 /* Access seccomp_data fields. */
bd4cf0ed 565 case BPF_LDX | BPF_ABS | BPF_W:
9739eef1
AS
566 /* A = *(u32 *) (ctx + K) */
567 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
bd4cf0ed
AS
568 break;
569
ca9f1fd2 570 /* Unknown instruction. */
1da177e4 571 default:
bd4cf0ed 572 goto err;
1da177e4 573 }
bd4cf0ed
AS
574
575 insn++;
576 if (new_prog)
577 memcpy(new_insn, tmp_insns,
578 sizeof(*insn) * (insn - tmp_insns));
bd4cf0ed 579 new_insn += insn - tmp_insns;
1da177e4
LT
580 }
581
bd4cf0ed
AS
582 if (!new_prog) {
583 /* Only calculating new length. */
584 *new_len = new_insn - new_prog;
585 return 0;
586 }
587
588 pass++;
589 if (new_flen != new_insn - new_prog) {
590 new_flen = new_insn - new_prog;
591 if (pass > 2)
592 goto err;
bd4cf0ed
AS
593 goto do_pass;
594 }
595
596 kfree(addrs);
597 BUG_ON(*new_len != new_flen);
1da177e4 598 return 0;
bd4cf0ed
AS
599err:
600 kfree(addrs);
601 return -EINVAL;
1da177e4
LT
602}
603
bd4cf0ed 604/* Security:
bd4cf0ed 605 *
2d5311e4 606 * As we dont want to clear mem[] array for each packet going through
8ea6e345 607 * __bpf_prog_run(), we check that filter loaded by user never try to read
2d5311e4 608 * a cell if not previously written, and we check all branches to be sure
25985edc 609 * a malicious user doesn't try to abuse us.
2d5311e4 610 */
ec31a05c 611static int check_load_and_stores(const struct sock_filter *filter, int flen)
2d5311e4 612{
34805931 613 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
2d5311e4
ED
614 int pc, ret = 0;
615
616 BUILD_BUG_ON(BPF_MEMWORDS > 16);
34805931 617
99e72a0f 618 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
2d5311e4
ED
619 if (!masks)
620 return -ENOMEM;
34805931 621
2d5311e4
ED
622 memset(masks, 0xff, flen * sizeof(*masks));
623
624 for (pc = 0; pc < flen; pc++) {
625 memvalid &= masks[pc];
626
627 switch (filter[pc].code) {
34805931
DB
628 case BPF_ST:
629 case BPF_STX:
2d5311e4
ED
630 memvalid |= (1 << filter[pc].k);
631 break;
34805931
DB
632 case BPF_LD | BPF_MEM:
633 case BPF_LDX | BPF_MEM:
2d5311e4
ED
634 if (!(memvalid & (1 << filter[pc].k))) {
635 ret = -EINVAL;
636 goto error;
637 }
638 break;
34805931
DB
639 case BPF_JMP | BPF_JA:
640 /* A jump must set masks on target */
2d5311e4
ED
641 masks[pc + 1 + filter[pc].k] &= memvalid;
642 memvalid = ~0;
643 break;
34805931
DB
644 case BPF_JMP | BPF_JEQ | BPF_K:
645 case BPF_JMP | BPF_JEQ | BPF_X:
646 case BPF_JMP | BPF_JGE | BPF_K:
647 case BPF_JMP | BPF_JGE | BPF_X:
648 case BPF_JMP | BPF_JGT | BPF_K:
649 case BPF_JMP | BPF_JGT | BPF_X:
650 case BPF_JMP | BPF_JSET | BPF_K:
651 case BPF_JMP | BPF_JSET | BPF_X:
652 /* A jump must set masks on targets */
2d5311e4
ED
653 masks[pc + 1 + filter[pc].jt] &= memvalid;
654 masks[pc + 1 + filter[pc].jf] &= memvalid;
655 memvalid = ~0;
656 break;
657 }
658 }
659error:
660 kfree(masks);
661 return ret;
662}
663
34805931
DB
664static bool chk_code_allowed(u16 code_to_probe)
665{
666 static const bool codes[] = {
667 /* 32 bit ALU operations */
668 [BPF_ALU | BPF_ADD | BPF_K] = true,
669 [BPF_ALU | BPF_ADD | BPF_X] = true,
670 [BPF_ALU | BPF_SUB | BPF_K] = true,
671 [BPF_ALU | BPF_SUB | BPF_X] = true,
672 [BPF_ALU | BPF_MUL | BPF_K] = true,
673 [BPF_ALU | BPF_MUL | BPF_X] = true,
674 [BPF_ALU | BPF_DIV | BPF_K] = true,
675 [BPF_ALU | BPF_DIV | BPF_X] = true,
676 [BPF_ALU | BPF_MOD | BPF_K] = true,
677 [BPF_ALU | BPF_MOD | BPF_X] = true,
678 [BPF_ALU | BPF_AND | BPF_K] = true,
679 [BPF_ALU | BPF_AND | BPF_X] = true,
680 [BPF_ALU | BPF_OR | BPF_K] = true,
681 [BPF_ALU | BPF_OR | BPF_X] = true,
682 [BPF_ALU | BPF_XOR | BPF_K] = true,
683 [BPF_ALU | BPF_XOR | BPF_X] = true,
684 [BPF_ALU | BPF_LSH | BPF_K] = true,
685 [BPF_ALU | BPF_LSH | BPF_X] = true,
686 [BPF_ALU | BPF_RSH | BPF_K] = true,
687 [BPF_ALU | BPF_RSH | BPF_X] = true,
688 [BPF_ALU | BPF_NEG] = true,
689 /* Load instructions */
690 [BPF_LD | BPF_W | BPF_ABS] = true,
691 [BPF_LD | BPF_H | BPF_ABS] = true,
692 [BPF_LD | BPF_B | BPF_ABS] = true,
693 [BPF_LD | BPF_W | BPF_LEN] = true,
694 [BPF_LD | BPF_W | BPF_IND] = true,
695 [BPF_LD | BPF_H | BPF_IND] = true,
696 [BPF_LD | BPF_B | BPF_IND] = true,
697 [BPF_LD | BPF_IMM] = true,
698 [BPF_LD | BPF_MEM] = true,
699 [BPF_LDX | BPF_W | BPF_LEN] = true,
700 [BPF_LDX | BPF_B | BPF_MSH] = true,
701 [BPF_LDX | BPF_IMM] = true,
702 [BPF_LDX | BPF_MEM] = true,
703 /* Store instructions */
704 [BPF_ST] = true,
705 [BPF_STX] = true,
706 /* Misc instructions */
707 [BPF_MISC | BPF_TAX] = true,
708 [BPF_MISC | BPF_TXA] = true,
709 /* Return instructions */
710 [BPF_RET | BPF_K] = true,
711 [BPF_RET | BPF_A] = true,
712 /* Jump instructions */
713 [BPF_JMP | BPF_JA] = true,
714 [BPF_JMP | BPF_JEQ | BPF_K] = true,
715 [BPF_JMP | BPF_JEQ | BPF_X] = true,
716 [BPF_JMP | BPF_JGE | BPF_K] = true,
717 [BPF_JMP | BPF_JGE | BPF_X] = true,
718 [BPF_JMP | BPF_JGT | BPF_K] = true,
719 [BPF_JMP | BPF_JGT | BPF_X] = true,
720 [BPF_JMP | BPF_JSET | BPF_K] = true,
721 [BPF_JMP | BPF_JSET | BPF_X] = true,
722 };
723
724 if (code_to_probe >= ARRAY_SIZE(codes))
725 return false;
726
727 return codes[code_to_probe];
728}
729
1da177e4 730/**
4df95ff4 731 * bpf_check_classic - verify socket filter code
1da177e4
LT
732 * @filter: filter to verify
733 * @flen: length of filter
734 *
735 * Check the user's filter code. If we let some ugly
736 * filter code slip through kaboom! The filter must contain
93699863
KK
737 * no references or jumps that are out of range, no illegal
738 * instructions, and must end with a RET instruction.
1da177e4 739 *
7b11f69f
KK
740 * All jumps are forward as they are not signed.
741 *
742 * Returns 0 if the rule set is legal or -EINVAL if not.
1da177e4 743 */
4df95ff4 744int bpf_check_classic(const struct sock_filter *filter, unsigned int flen)
1da177e4 745{
aa1113d9 746 bool anc_found;
34805931 747 int pc;
1da177e4 748
1b93ae64 749 if (flen == 0 || flen > BPF_MAXINSNS)
1da177e4
LT
750 return -EINVAL;
751
34805931 752 /* Check the filter code now */
1da177e4 753 for (pc = 0; pc < flen; pc++) {
ec31a05c 754 const struct sock_filter *ftest = &filter[pc];
93699863 755
34805931
DB
756 /* May we actually operate on this code? */
757 if (!chk_code_allowed(ftest->code))
cba328fc 758 return -EINVAL;
34805931 759
93699863 760 /* Some instructions need special checks */
34805931
DB
761 switch (ftest->code) {
762 case BPF_ALU | BPF_DIV | BPF_K:
763 case BPF_ALU | BPF_MOD | BPF_K:
764 /* Check for division by zero */
b6069a95
ED
765 if (ftest->k == 0)
766 return -EINVAL;
767 break;
34805931
DB
768 case BPF_LD | BPF_MEM:
769 case BPF_LDX | BPF_MEM:
770 case BPF_ST:
771 case BPF_STX:
772 /* Check for invalid memory addresses */
93699863
KK
773 if (ftest->k >= BPF_MEMWORDS)
774 return -EINVAL;
775 break;
34805931
DB
776 case BPF_JMP | BPF_JA:
777 /* Note, the large ftest->k might cause loops.
93699863
KK
778 * Compare this with conditional jumps below,
779 * where offsets are limited. --ANK (981016)
780 */
34805931 781 if (ftest->k >= (unsigned int)(flen - pc - 1))
93699863 782 return -EINVAL;
01f2f3f6 783 break;
34805931
DB
784 case BPF_JMP | BPF_JEQ | BPF_K:
785 case BPF_JMP | BPF_JEQ | BPF_X:
786 case BPF_JMP | BPF_JGE | BPF_K:
787 case BPF_JMP | BPF_JGE | BPF_X:
788 case BPF_JMP | BPF_JGT | BPF_K:
789 case BPF_JMP | BPF_JGT | BPF_X:
790 case BPF_JMP | BPF_JSET | BPF_K:
791 case BPF_JMP | BPF_JSET | BPF_X:
792 /* Both conditionals must be safe */
e35bedf3 793 if (pc + ftest->jt + 1 >= flen ||
93699863
KK
794 pc + ftest->jf + 1 >= flen)
795 return -EINVAL;
cba328fc 796 break;
34805931
DB
797 case BPF_LD | BPF_W | BPF_ABS:
798 case BPF_LD | BPF_H | BPF_ABS:
799 case BPF_LD | BPF_B | BPF_ABS:
aa1113d9 800 anc_found = false;
34805931
DB
801 if (bpf_anc_helper(ftest) & BPF_ANC)
802 anc_found = true;
803 /* Ancillary operation unknown or unsupported */
aa1113d9
DB
804 if (anc_found == false && ftest->k >= SKF_AD_OFF)
805 return -EINVAL;
01f2f3f6
HPP
806 }
807 }
93699863 808
34805931 809 /* Last instruction must be a RET code */
01f2f3f6 810 switch (filter[flen - 1].code) {
34805931
DB
811 case BPF_RET | BPF_K:
812 case BPF_RET | BPF_A:
2d5311e4 813 return check_load_and_stores(filter, flen);
cba328fc 814 }
34805931 815
cba328fc 816 return -EINVAL;
1da177e4 817}
4df95ff4 818EXPORT_SYMBOL(bpf_check_classic);
1da177e4 819
7ae457c1
AS
820static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
821 const struct sock_fprog *fprog)
a3ea269b 822{
009937e7 823 unsigned int fsize = bpf_classic_proglen(fprog);
a3ea269b
DB
824 struct sock_fprog_kern *fkprog;
825
826 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
827 if (!fp->orig_prog)
828 return -ENOMEM;
829
830 fkprog = fp->orig_prog;
831 fkprog->len = fprog->len;
832 fkprog->filter = kmemdup(fp->insns, fsize, GFP_KERNEL);
833 if (!fkprog->filter) {
834 kfree(fp->orig_prog);
835 return -ENOMEM;
836 }
837
838 return 0;
839}
840
7ae457c1 841static void bpf_release_orig_filter(struct bpf_prog *fp)
a3ea269b
DB
842{
843 struct sock_fprog_kern *fprog = fp->orig_prog;
844
845 if (fprog) {
846 kfree(fprog->filter);
847 kfree(fprog);
848 }
849}
850
7ae457c1
AS
851static void __bpf_prog_release(struct bpf_prog *prog)
852{
24701ece 853 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
89aa0758
AS
854 bpf_prog_put(prog);
855 } else {
856 bpf_release_orig_filter(prog);
857 bpf_prog_free(prog);
858 }
7ae457c1
AS
859}
860
34c5bd66
PN
861static void __sk_filter_release(struct sk_filter *fp)
862{
7ae457c1
AS
863 __bpf_prog_release(fp->prog);
864 kfree(fp);
34c5bd66
PN
865}
866
47e958ea 867/**
46bcf14f 868 * sk_filter_release_rcu - Release a socket filter by rcu_head
47e958ea
PE
869 * @rcu: rcu_head that contains the sk_filter to free
870 */
fbc907f0 871static void sk_filter_release_rcu(struct rcu_head *rcu)
47e958ea
PE
872{
873 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
874
34c5bd66 875 __sk_filter_release(fp);
47e958ea 876}
fbc907f0
DB
877
878/**
879 * sk_filter_release - release a socket filter
880 * @fp: filter to remove
881 *
882 * Remove a filter from a socket and release its resources.
883 */
884static void sk_filter_release(struct sk_filter *fp)
885{
886 if (atomic_dec_and_test(&fp->refcnt))
887 call_rcu(&fp->rcu, sk_filter_release_rcu);
888}
889
890void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
891{
7ae457c1 892 u32 filter_size = bpf_prog_size(fp->prog->len);
fbc907f0 893
278571ba
AS
894 atomic_sub(filter_size, &sk->sk_omem_alloc);
895 sk_filter_release(fp);
fbc907f0 896}
47e958ea 897
278571ba
AS
898/* try to charge the socket memory if there is space available
899 * return true on success
900 */
901bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
bd4cf0ed 902{
7ae457c1 903 u32 filter_size = bpf_prog_size(fp->prog->len);
278571ba
AS
904
905 /* same check as in sock_kmalloc() */
906 if (filter_size <= sysctl_optmem_max &&
907 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
908 atomic_inc(&fp->refcnt);
909 atomic_add(filter_size, &sk->sk_omem_alloc);
910 return true;
bd4cf0ed 911 }
278571ba 912 return false;
bd4cf0ed
AS
913}
914
7ae457c1 915static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
bd4cf0ed
AS
916{
917 struct sock_filter *old_prog;
7ae457c1 918 struct bpf_prog *old_fp;
34805931 919 int err, new_len, old_len = fp->len;
bd4cf0ed
AS
920
921 /* We are free to overwrite insns et al right here as it
922 * won't be used at this point in time anymore internally
923 * after the migration to the internal BPF instruction
924 * representation.
925 */
926 BUILD_BUG_ON(sizeof(struct sock_filter) !=
2695fb55 927 sizeof(struct bpf_insn));
bd4cf0ed 928
bd4cf0ed
AS
929 /* Conversion cannot happen on overlapping memory areas,
930 * so we need to keep the user BPF around until the 2nd
931 * pass. At this time, the user BPF is stored in fp->insns.
932 */
933 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
934 GFP_KERNEL);
935 if (!old_prog) {
936 err = -ENOMEM;
937 goto out_err;
938 }
939
940 /* 1st pass: calculate the new program length. */
8fb575ca 941 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
bd4cf0ed
AS
942 if (err)
943 goto out_err_free;
944
945 /* Expand fp for appending the new filter representation. */
946 old_fp = fp;
60a3b225 947 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
bd4cf0ed
AS
948 if (!fp) {
949 /* The old_fp is still around in case we couldn't
950 * allocate new memory, so uncharge on that one.
951 */
952 fp = old_fp;
953 err = -ENOMEM;
954 goto out_err_free;
955 }
956
bd4cf0ed
AS
957 fp->len = new_len;
958
2695fb55 959 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
8fb575ca 960 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
bd4cf0ed 961 if (err)
8fb575ca 962 /* 2nd bpf_convert_filter() can fail only if it fails
bd4cf0ed
AS
963 * to allocate memory, remapping must succeed. Note,
964 * that at this time old_fp has already been released
278571ba 965 * by krealloc().
bd4cf0ed
AS
966 */
967 goto out_err_free;
968
7ae457c1 969 bpf_prog_select_runtime(fp);
5fe821a9 970
bd4cf0ed
AS
971 kfree(old_prog);
972 return fp;
973
974out_err_free:
975 kfree(old_prog);
976out_err:
7ae457c1 977 __bpf_prog_release(fp);
bd4cf0ed
AS
978 return ERR_PTR(err);
979}
980
7ae457c1 981static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp)
302d6637
JP
982{
983 int err;
984
bd4cf0ed 985 fp->bpf_func = NULL;
286aad3c 986 fp->jited = false;
302d6637 987
4df95ff4 988 err = bpf_check_classic(fp->insns, fp->len);
418c96ac 989 if (err) {
7ae457c1 990 __bpf_prog_release(fp);
bd4cf0ed 991 return ERR_PTR(err);
418c96ac 992 }
302d6637 993
bd4cf0ed
AS
994 /* Probe if we can JIT compile the filter and if so, do
995 * the compilation of the filter.
996 */
302d6637 997 bpf_jit_compile(fp);
bd4cf0ed
AS
998
999 /* JIT compiler couldn't process this filter, so do the
1000 * internal BPF translation for the optimized interpreter.
1001 */
5fe821a9 1002 if (!fp->jited)
7ae457c1 1003 fp = bpf_migrate_filter(fp);
bd4cf0ed
AS
1004
1005 return fp;
302d6637
JP
1006}
1007
1008/**
7ae457c1 1009 * bpf_prog_create - create an unattached filter
c6c4b97c 1010 * @pfp: the unattached filter that is created
677a9fd3 1011 * @fprog: the filter program
302d6637 1012 *
c6c4b97c 1013 * Create a filter independent of any socket. We first run some
302d6637
JP
1014 * sanity checks on it to make sure it does not explode on us later.
1015 * If an error occurs or there is insufficient memory for the filter
1016 * a negative errno code is returned. On success the return is zero.
1017 */
7ae457c1 1018int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
302d6637 1019{
009937e7 1020 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1 1021 struct bpf_prog *fp;
302d6637
JP
1022
1023 /* Make sure new filter is there and in the right amounts. */
1024 if (fprog->filter == NULL)
1025 return -EINVAL;
1026
60a3b225 1027 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
302d6637
JP
1028 if (!fp)
1029 return -ENOMEM;
a3ea269b 1030
302d6637
JP
1031 memcpy(fp->insns, fprog->filter, fsize);
1032
302d6637 1033 fp->len = fprog->len;
a3ea269b
DB
1034 /* Since unattached filters are not copied back to user
1035 * space through sk_get_filter(), we do not need to hold
1036 * a copy here, and can spare us the work.
1037 */
1038 fp->orig_prog = NULL;
302d6637 1039
7ae457c1 1040 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1041 * memory in case something goes wrong.
1042 */
7ae457c1 1043 fp = bpf_prepare_filter(fp);
bd4cf0ed
AS
1044 if (IS_ERR(fp))
1045 return PTR_ERR(fp);
302d6637
JP
1046
1047 *pfp = fp;
1048 return 0;
302d6637 1049}
7ae457c1 1050EXPORT_SYMBOL_GPL(bpf_prog_create);
302d6637 1051
7ae457c1 1052void bpf_prog_destroy(struct bpf_prog *fp)
302d6637 1053{
7ae457c1 1054 __bpf_prog_release(fp);
302d6637 1055}
7ae457c1 1056EXPORT_SYMBOL_GPL(bpf_prog_destroy);
302d6637 1057
49b31e57
DB
1058static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1059{
1060 struct sk_filter *fp, *old_fp;
1061
1062 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1063 if (!fp)
1064 return -ENOMEM;
1065
1066 fp->prog = prog;
1067 atomic_set(&fp->refcnt, 0);
1068
1069 if (!sk_filter_charge(sk, fp)) {
1070 kfree(fp);
1071 return -ENOMEM;
1072 }
1073
1074 old_fp = rcu_dereference_protected(sk->sk_filter,
1075 sock_owned_by_user(sk));
1076 rcu_assign_pointer(sk->sk_filter, fp);
1077
1078 if (old_fp)
1079 sk_filter_uncharge(sk, old_fp);
1080
1081 return 0;
1082}
1083
1da177e4
LT
1084/**
1085 * sk_attach_filter - attach a socket filter
1086 * @fprog: the filter program
1087 * @sk: the socket to use
1088 *
1089 * Attach the user's filter code. We first run some sanity checks on
1090 * it to make sure it does not explode on us later. If an error
1091 * occurs or there is insufficient memory for the filter a negative
1092 * errno code is returned. On success the return is zero.
1093 */
1094int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1095{
009937e7 1096 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1
AS
1097 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1098 struct bpf_prog *prog;
1da177e4
LT
1099 int err;
1100
d59577b6
VB
1101 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1102 return -EPERM;
1103
1da177e4 1104 /* Make sure new filter is there and in the right amounts. */
e35bedf3
KK
1105 if (fprog->filter == NULL)
1106 return -EINVAL;
1da177e4 1107
60a3b225 1108 prog = bpf_prog_alloc(bpf_fsize, 0);
7ae457c1 1109 if (!prog)
1da177e4 1110 return -ENOMEM;
a3ea269b 1111
7ae457c1 1112 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
c0d1379a 1113 __bpf_prog_free(prog);
1da177e4
LT
1114 return -EFAULT;
1115 }
1116
7ae457c1 1117 prog->len = fprog->len;
1da177e4 1118
7ae457c1 1119 err = bpf_prog_store_orig_filter(prog, fprog);
a3ea269b 1120 if (err) {
c0d1379a 1121 __bpf_prog_free(prog);
a3ea269b
DB
1122 return -ENOMEM;
1123 }
1124
7ae457c1 1125 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1126 * memory in case something goes wrong.
1127 */
7ae457c1
AS
1128 prog = bpf_prepare_filter(prog);
1129 if (IS_ERR(prog))
1130 return PTR_ERR(prog);
1131
49b31e57
DB
1132 err = __sk_attach_prog(prog, sk);
1133 if (err < 0) {
7ae457c1 1134 __bpf_prog_release(prog);
49b31e57 1135 return err;
278571ba
AS
1136 }
1137
d3904b73 1138 return 0;
1da177e4 1139}
5ff3f073 1140EXPORT_SYMBOL_GPL(sk_attach_filter);
1da177e4 1141
89aa0758
AS
1142int sk_attach_bpf(u32 ufd, struct sock *sk)
1143{
89aa0758 1144 struct bpf_prog *prog;
49b31e57 1145 int err;
89aa0758
AS
1146
1147 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1148 return -EPERM;
1149
1150 prog = bpf_prog_get(ufd);
198bf1b0
AS
1151 if (IS_ERR(prog))
1152 return PTR_ERR(prog);
89aa0758 1153
24701ece 1154 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
89aa0758
AS
1155 bpf_prog_put(prog);
1156 return -EINVAL;
1157 }
1158
49b31e57
DB
1159 err = __sk_attach_prog(prog, sk);
1160 if (err < 0) {
89aa0758 1161 bpf_prog_put(prog);
49b31e57 1162 return err;
89aa0758
AS
1163 }
1164
89aa0758
AS
1165 return 0;
1166}
1167
d4052c4a
DB
1168static const struct bpf_func_proto *
1169sk_filter_func_proto(enum bpf_func_id func_id)
89aa0758
AS
1170{
1171 switch (func_id) {
1172 case BPF_FUNC_map_lookup_elem:
1173 return &bpf_map_lookup_elem_proto;
1174 case BPF_FUNC_map_update_elem:
1175 return &bpf_map_update_elem_proto;
1176 case BPF_FUNC_map_delete_elem:
1177 return &bpf_map_delete_elem_proto;
03e69b50
DB
1178 case BPF_FUNC_get_prandom_u32:
1179 return &bpf_get_prandom_u32_proto;
c04167ce
DB
1180 case BPF_FUNC_get_smp_processor_id:
1181 return &bpf_get_smp_processor_id_proto;
89aa0758
AS
1182 default:
1183 return NULL;
1184 }
1185}
1186
d4052c4a
DB
1187static bool sk_filter_is_valid_access(int off, int size,
1188 enum bpf_access_type type)
89aa0758 1189{
9bac3d6d
AS
1190 /* only read is allowed */
1191 if (type != BPF_READ)
1192 return false;
1193
1194 /* check bounds */
1195 if (off < 0 || off >= sizeof(struct __sk_buff))
1196 return false;
1197
1198 /* disallow misaligned access */
1199 if (off % size != 0)
1200 return false;
1201
1202 /* all __sk_buff fields are __u32 */
1203 if (size != 4)
1204 return false;
1205
1206 return true;
1207}
1208
1209static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off,
1210 struct bpf_insn *insn_buf)
1211{
1212 struct bpf_insn *insn = insn_buf;
1213
1214 switch (ctx_off) {
1215 case offsetof(struct __sk_buff, len):
1216 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
1217
1218 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1219 offsetof(struct sk_buff, len));
1220 break;
1221
0b8c707d
DB
1222 case offsetof(struct __sk_buff, protocol):
1223 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1224
1225 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1226 offsetof(struct sk_buff, protocol));
1227 break;
1228
9bac3d6d
AS
1229 case offsetof(struct __sk_buff, mark):
1230 return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn);
1231
1232 case offsetof(struct __sk_buff, pkt_type):
1233 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
1234
1235 case offsetof(struct __sk_buff, queue_mapping):
1236 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
c2497395 1237
c2497395
AS
1238 case offsetof(struct __sk_buff, vlan_present):
1239 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
1240 dst_reg, src_reg, insn);
1241
1242 case offsetof(struct __sk_buff, vlan_tci):
1243 return convert_skb_access(SKF_AD_VLAN_TAG,
1244 dst_reg, src_reg, insn);
9bac3d6d
AS
1245 }
1246
1247 return insn - insn_buf;
89aa0758
AS
1248}
1249
d4052c4a
DB
1250static const struct bpf_verifier_ops sk_filter_ops = {
1251 .get_func_proto = sk_filter_func_proto,
1252 .is_valid_access = sk_filter_is_valid_access,
9bac3d6d 1253 .convert_ctx_access = sk_filter_convert_ctx_access,
89aa0758
AS
1254};
1255
d4052c4a
DB
1256static struct bpf_prog_type_list sk_filter_type __read_mostly = {
1257 .ops = &sk_filter_ops,
89aa0758
AS
1258 .type = BPF_PROG_TYPE_SOCKET_FILTER,
1259};
1260
96be4325
DB
1261static struct bpf_prog_type_list sched_cls_type __read_mostly = {
1262 .ops = &sk_filter_ops,
1263 .type = BPF_PROG_TYPE_SCHED_CLS,
1264};
1265
94caee8c
DB
1266static struct bpf_prog_type_list sched_act_type __read_mostly = {
1267 .ops = &sk_filter_ops,
1268 .type = BPF_PROG_TYPE_SCHED_ACT,
1269};
1270
d4052c4a 1271static int __init register_sk_filter_ops(void)
89aa0758 1272{
d4052c4a 1273 bpf_register_prog_type(&sk_filter_type);
96be4325 1274 bpf_register_prog_type(&sched_cls_type);
94caee8c 1275 bpf_register_prog_type(&sched_act_type);
96be4325 1276
89aa0758
AS
1277 return 0;
1278}
d4052c4a
DB
1279late_initcall(register_sk_filter_ops);
1280
55b33325
PE
1281int sk_detach_filter(struct sock *sk)
1282{
1283 int ret = -ENOENT;
1284 struct sk_filter *filter;
1285
d59577b6
VB
1286 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1287 return -EPERM;
1288
f91ff5b9
ED
1289 filter = rcu_dereference_protected(sk->sk_filter,
1290 sock_owned_by_user(sk));
55b33325 1291 if (filter) {
a9b3cd7f 1292 RCU_INIT_POINTER(sk->sk_filter, NULL);
46bcf14f 1293 sk_filter_uncharge(sk, filter);
55b33325
PE
1294 ret = 0;
1295 }
a3ea269b 1296
55b33325
PE
1297 return ret;
1298}
5ff3f073 1299EXPORT_SYMBOL_GPL(sk_detach_filter);
a8fc9277 1300
a3ea269b
DB
1301int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
1302 unsigned int len)
a8fc9277 1303{
a3ea269b 1304 struct sock_fprog_kern *fprog;
a8fc9277 1305 struct sk_filter *filter;
a3ea269b 1306 int ret = 0;
a8fc9277
PE
1307
1308 lock_sock(sk);
1309 filter = rcu_dereference_protected(sk->sk_filter,
a3ea269b 1310 sock_owned_by_user(sk));
a8fc9277
PE
1311 if (!filter)
1312 goto out;
a3ea269b
DB
1313
1314 /* We're copying the filter that has been originally attached,
1315 * so no conversion/decode needed anymore.
1316 */
7ae457c1 1317 fprog = filter->prog->orig_prog;
a3ea269b
DB
1318
1319 ret = fprog->len;
a8fc9277 1320 if (!len)
a3ea269b 1321 /* User space only enquires number of filter blocks. */
a8fc9277 1322 goto out;
a3ea269b 1323
a8fc9277 1324 ret = -EINVAL;
a3ea269b 1325 if (len < fprog->len)
a8fc9277
PE
1326 goto out;
1327
1328 ret = -EFAULT;
009937e7 1329 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
a3ea269b 1330 goto out;
a8fc9277 1331
a3ea269b
DB
1332 /* Instead of bytes, the API requests to return the number
1333 * of filter blocks.
1334 */
1335 ret = fprog->len;
a8fc9277
PE
1336out:
1337 release_sock(sk);
1338 return ret;
1339}