]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - net/core/filter.c
2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
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.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/if_packet.h>
33 #include <linux/gfp.h>
35 #include <net/protocol.h>
36 #include <net/netlink.h>
37 #include <linux/skbuff.h>
39 #include <linux/errno.h>
40 #include <linux/timer.h>
41 #include <asm/uaccess.h>
42 #include <asm/unaligned.h>
43 #include <linux/filter.h>
44 #include <linux/ratelimit.h>
45 #include <linux/seccomp.h>
46 #include <linux/if_vlan.h>
49 * sk_filter - run a packet through a socket filter
50 * @sk: sock associated with &sk_buff
51 * @skb: buffer to filter
53 * Run the filter code and then cut skb->data to correct size returned by
54 * SK_RUN_FILTER. If pkt_len is 0 we toss packet. If skb->len is smaller
55 * than pkt_len we keep whole skb->data. This is the socket level
56 * wrapper to SK_RUN_FILTER. It returns 0 if the packet should
57 * be accepted or -EPERM if the packet should be tossed.
60 int sk_filter(struct sock
*sk
, struct sk_buff
*skb
)
63 struct sk_filter
*filter
;
66 * If the skb was allocated from pfmemalloc reserves, only
67 * allow SOCK_MEMALLOC sockets to use it as this socket is
70 if (skb_pfmemalloc(skb
) && !sock_flag(sk
, SOCK_MEMALLOC
))
73 err
= security_sock_rcv_skb(sk
, skb
);
78 filter
= rcu_dereference(sk
->sk_filter
);
80 unsigned int pkt_len
= SK_RUN_FILTER(filter
, skb
);
82 err
= pkt_len
? pskb_trim(skb
, pkt_len
) : -EPERM
;
88 EXPORT_SYMBOL(sk_filter
);
90 static u64
__skb_get_pay_offset(u64 ctx
, u64 a
, u64 x
, u64 r4
, u64 r5
)
92 return skb_get_poff((struct sk_buff
*)(unsigned long) ctx
);
95 static u64
__skb_get_nlattr(u64 ctx
, u64 a
, u64 x
, u64 r4
, u64 r5
)
97 struct sk_buff
*skb
= (struct sk_buff
*)(unsigned long) ctx
;
100 if (skb_is_nonlinear(skb
))
103 if (skb
->len
< sizeof(struct nlattr
))
106 if (a
> skb
->len
- sizeof(struct nlattr
))
109 nla
= nla_find((struct nlattr
*) &skb
->data
[a
], skb
->len
- a
, x
);
111 return (void *) nla
- (void *) skb
->data
;
116 static u64
__skb_get_nlattr_nest(u64 ctx
, u64 a
, u64 x
, u64 r4
, u64 r5
)
118 struct sk_buff
*skb
= (struct sk_buff
*)(unsigned long) ctx
;
121 if (skb_is_nonlinear(skb
))
124 if (skb
->len
< sizeof(struct nlattr
))
127 if (a
> skb
->len
- sizeof(struct nlattr
))
130 nla
= (struct nlattr
*) &skb
->data
[a
];
131 if (nla
->nla_len
> skb
->len
- a
)
134 nla
= nla_find_nested(nla
, x
);
136 return (void *) nla
- (void *) skb
->data
;
141 static u64
__get_raw_cpu_id(u64 ctx
, u64 a
, u64 x
, u64 r4
, u64 r5
)
143 return raw_smp_processor_id();
146 /* note that this only generates 32-bit random numbers */
147 static u64
__get_random_u32(u64 ctx
, u64 a
, u64 x
, u64 r4
, u64 r5
)
149 return prandom_u32();
152 static bool convert_bpf_extensions(struct sock_filter
*fp
,
153 struct bpf_insn
**insnp
)
155 struct bpf_insn
*insn
= *insnp
;
158 case SKF_AD_OFF
+ SKF_AD_PROTOCOL
:
159 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, protocol
) != 2);
161 /* A = *(u16 *) (CTX + offsetof(protocol)) */
162 *insn
++ = BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_CTX
,
163 offsetof(struct sk_buff
, protocol
));
164 /* A = ntohs(A) [emitting a nop or swap16] */
165 *insn
= BPF_ENDIAN(BPF_FROM_BE
, BPF_REG_A
, 16);
168 case SKF_AD_OFF
+ SKF_AD_PKTTYPE
:
169 *insn
++ = BPF_LDX_MEM(BPF_B
, BPF_REG_A
, BPF_REG_CTX
,
171 *insn
= BPF_ALU32_IMM(BPF_AND
, BPF_REG_A
, PKT_TYPE_MAX
);
172 #ifdef __BIG_ENDIAN_BITFIELD
174 *insn
= BPF_ALU32_IMM(BPF_RSH
, BPF_REG_A
, 5);
178 case SKF_AD_OFF
+ SKF_AD_IFINDEX
:
179 case SKF_AD_OFF
+ SKF_AD_HATYPE
:
180 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, ifindex
) != 4);
181 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, type
) != 2);
182 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff
, dev
)) < 0);
184 *insn
++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff
, dev
)),
185 BPF_REG_TMP
, BPF_REG_CTX
,
186 offsetof(struct sk_buff
, dev
));
187 /* if (tmp != 0) goto pc + 1 */
188 *insn
++ = BPF_JMP_IMM(BPF_JNE
, BPF_REG_TMP
, 0, 1);
189 *insn
++ = BPF_EXIT_INSN();
190 if (fp
->k
== SKF_AD_OFF
+ SKF_AD_IFINDEX
)
191 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_TMP
,
192 offsetof(struct net_device
, ifindex
));
194 *insn
= BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_TMP
,
195 offsetof(struct net_device
, type
));
198 case SKF_AD_OFF
+ SKF_AD_MARK
:
199 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, mark
) != 4);
201 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_CTX
,
202 offsetof(struct sk_buff
, mark
));
205 case SKF_AD_OFF
+ SKF_AD_RXHASH
:
206 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, hash
) != 4);
208 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_CTX
,
209 offsetof(struct sk_buff
, hash
));
212 case SKF_AD_OFF
+ SKF_AD_QUEUE
:
213 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, queue_mapping
) != 2);
215 *insn
= BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_CTX
,
216 offsetof(struct sk_buff
, queue_mapping
));
219 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG
:
220 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
:
221 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, vlan_tci
) != 2);
222 BUILD_BUG_ON(VLAN_TAG_PRESENT
!= 0x1000);
224 /* A = *(u16 *) (CTX + offsetof(vlan_tci)) */
225 *insn
++ = BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_CTX
,
226 offsetof(struct sk_buff
, vlan_tci
));
227 if (fp
->k
== SKF_AD_OFF
+ SKF_AD_VLAN_TAG
) {
228 *insn
= BPF_ALU32_IMM(BPF_AND
, BPF_REG_A
,
232 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, BPF_REG_A
, 12);
234 *insn
= BPF_ALU32_IMM(BPF_AND
, BPF_REG_A
, 1);
238 case SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
:
239 case SKF_AD_OFF
+ SKF_AD_NLATTR
:
240 case SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
:
241 case SKF_AD_OFF
+ SKF_AD_CPU
:
242 case SKF_AD_OFF
+ SKF_AD_RANDOM
:
244 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG1
, BPF_REG_CTX
);
246 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG2
, BPF_REG_A
);
248 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG3
, BPF_REG_X
);
249 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
251 case SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
:
252 *insn
= BPF_EMIT_CALL(__skb_get_pay_offset
);
254 case SKF_AD_OFF
+ SKF_AD_NLATTR
:
255 *insn
= BPF_EMIT_CALL(__skb_get_nlattr
);
257 case SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
:
258 *insn
= BPF_EMIT_CALL(__skb_get_nlattr_nest
);
260 case SKF_AD_OFF
+ SKF_AD_CPU
:
261 *insn
= BPF_EMIT_CALL(__get_raw_cpu_id
);
263 case SKF_AD_OFF
+ SKF_AD_RANDOM
:
264 *insn
= BPF_EMIT_CALL(__get_random_u32
);
269 case SKF_AD_OFF
+ SKF_AD_ALU_XOR_X
:
271 *insn
= BPF_ALU32_REG(BPF_XOR
, BPF_REG_A
, BPF_REG_X
);
275 /* This is just a dummy call to avoid letting the compiler
276 * evict __bpf_call_base() as an optimization. Placed here
277 * where no-one bothers.
279 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
288 * bpf_convert_filter - convert filter program
289 * @prog: the user passed filter program
290 * @len: the length of the user passed filter program
291 * @new_prog: buffer where converted program will be stored
292 * @new_len: pointer to store length of converted program
294 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
295 * Conversion workflow:
297 * 1) First pass for calculating the new program length:
298 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
300 * 2) 2nd pass to remap in two passes: 1st pass finds new
301 * jump offsets, 2nd pass remapping:
302 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
303 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
305 * User BPF's register A is mapped to our BPF register 6, user BPF
306 * register X is mapped to BPF register 7; frame pointer is always
307 * register 10; Context 'void *ctx' is stored in register 1, that is,
308 * for socket filters: ctx == 'struct sk_buff *', for seccomp:
309 * ctx == 'struct seccomp_data *'.
311 int bpf_convert_filter(struct sock_filter
*prog
, int len
,
312 struct bpf_insn
*new_prog
, int *new_len
)
314 int new_flen
= 0, pass
= 0, target
, i
;
315 struct bpf_insn
*new_insn
;
316 struct sock_filter
*fp
;
320 BUILD_BUG_ON(BPF_MEMWORDS
* sizeof(u32
) > MAX_BPF_STACK
);
321 BUILD_BUG_ON(BPF_REG_FP
+ 1 != MAX_BPF_REG
);
323 if (len
<= 0 || len
> BPF_MAXINSNS
)
327 addrs
= kcalloc(len
, sizeof(*addrs
), GFP_KERNEL
);
337 *new_insn
= BPF_MOV64_REG(BPF_REG_CTX
, BPF_REG_ARG1
);
340 for (i
= 0; i
< len
; fp
++, i
++) {
341 struct bpf_insn tmp_insns
[6] = { };
342 struct bpf_insn
*insn
= tmp_insns
;
345 addrs
[i
] = new_insn
- new_prog
;
348 /* All arithmetic insns and skb loads map as-is. */
349 case BPF_ALU
| BPF_ADD
| BPF_X
:
350 case BPF_ALU
| BPF_ADD
| BPF_K
:
351 case BPF_ALU
| BPF_SUB
| BPF_X
:
352 case BPF_ALU
| BPF_SUB
| BPF_K
:
353 case BPF_ALU
| BPF_AND
| BPF_X
:
354 case BPF_ALU
| BPF_AND
| BPF_K
:
355 case BPF_ALU
| BPF_OR
| BPF_X
:
356 case BPF_ALU
| BPF_OR
| BPF_K
:
357 case BPF_ALU
| BPF_LSH
| BPF_X
:
358 case BPF_ALU
| BPF_LSH
| BPF_K
:
359 case BPF_ALU
| BPF_RSH
| BPF_X
:
360 case BPF_ALU
| BPF_RSH
| BPF_K
:
361 case BPF_ALU
| BPF_XOR
| BPF_X
:
362 case BPF_ALU
| BPF_XOR
| BPF_K
:
363 case BPF_ALU
| BPF_MUL
| BPF_X
:
364 case BPF_ALU
| BPF_MUL
| BPF_K
:
365 case BPF_ALU
| BPF_DIV
| BPF_X
:
366 case BPF_ALU
| BPF_DIV
| BPF_K
:
367 case BPF_ALU
| BPF_MOD
| BPF_X
:
368 case BPF_ALU
| BPF_MOD
| BPF_K
:
369 case BPF_ALU
| BPF_NEG
:
370 case BPF_LD
| BPF_ABS
| BPF_W
:
371 case BPF_LD
| BPF_ABS
| BPF_H
:
372 case BPF_LD
| BPF_ABS
| BPF_B
:
373 case BPF_LD
| BPF_IND
| BPF_W
:
374 case BPF_LD
| BPF_IND
| BPF_H
:
375 case BPF_LD
| BPF_IND
| BPF_B
:
376 /* Check for overloaded BPF extension and
377 * directly convert it if found, otherwise
378 * just move on with mapping.
380 if (BPF_CLASS(fp
->code
) == BPF_LD
&&
381 BPF_MODE(fp
->code
) == BPF_ABS
&&
382 convert_bpf_extensions(fp
, &insn
))
385 *insn
= BPF_RAW_INSN(fp
->code
, BPF_REG_A
, BPF_REG_X
, 0, fp
->k
);
388 /* Jump transformation cannot use BPF block macros
389 * everywhere as offset calculation and target updates
390 * require a bit more work than the rest, i.e. jump
391 * opcodes map as-is, but offsets need adjustment.
394 #define BPF_EMIT_JMP \
396 if (target >= len || target < 0) \
398 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
399 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
400 insn->off -= insn - tmp_insns; \
403 case BPF_JMP
| BPF_JA
:
404 target
= i
+ fp
->k
+ 1;
405 insn
->code
= fp
->code
;
409 case BPF_JMP
| BPF_JEQ
| BPF_K
:
410 case BPF_JMP
| BPF_JEQ
| BPF_X
:
411 case BPF_JMP
| BPF_JSET
| BPF_K
:
412 case BPF_JMP
| BPF_JSET
| BPF_X
:
413 case BPF_JMP
| BPF_JGT
| BPF_K
:
414 case BPF_JMP
| BPF_JGT
| BPF_X
:
415 case BPF_JMP
| BPF_JGE
| BPF_K
:
416 case BPF_JMP
| BPF_JGE
| BPF_X
:
417 if (BPF_SRC(fp
->code
) == BPF_K
&& (int) fp
->k
< 0) {
418 /* BPF immediates are signed, zero extend
419 * immediate into tmp register and use it
422 *insn
++ = BPF_MOV32_IMM(BPF_REG_TMP
, fp
->k
);
424 insn
->dst_reg
= BPF_REG_A
;
425 insn
->src_reg
= BPF_REG_TMP
;
428 insn
->dst_reg
= BPF_REG_A
;
429 insn
->src_reg
= BPF_REG_X
;
431 bpf_src
= BPF_SRC(fp
->code
);
434 /* Common case where 'jump_false' is next insn. */
436 insn
->code
= BPF_JMP
| BPF_OP(fp
->code
) | bpf_src
;
437 target
= i
+ fp
->jt
+ 1;
442 /* Convert JEQ into JNE when 'jump_true' is next insn. */
443 if (fp
->jt
== 0 && BPF_OP(fp
->code
) == BPF_JEQ
) {
444 insn
->code
= BPF_JMP
| BPF_JNE
| bpf_src
;
445 target
= i
+ fp
->jf
+ 1;
450 /* Other jumps are mapped into two insns: Jxx and JA. */
451 target
= i
+ fp
->jt
+ 1;
452 insn
->code
= BPF_JMP
| BPF_OP(fp
->code
) | bpf_src
;
456 insn
->code
= BPF_JMP
| BPF_JA
;
457 target
= i
+ fp
->jf
+ 1;
461 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
462 case BPF_LDX
| BPF_MSH
| BPF_B
:
464 *insn
++ = BPF_MOV64_REG(BPF_REG_TMP
, BPF_REG_A
);
465 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
466 *insn
++ = BPF_LD_ABS(BPF_B
, fp
->k
);
468 *insn
++ = BPF_ALU32_IMM(BPF_AND
, BPF_REG_A
, 0xf);
470 *insn
++ = BPF_ALU32_IMM(BPF_LSH
, BPF_REG_A
, 2);
472 *insn
++ = BPF_MOV64_REG(BPF_REG_X
, BPF_REG_A
);
474 *insn
= BPF_MOV64_REG(BPF_REG_A
, BPF_REG_TMP
);
477 /* RET_K, RET_A are remaped into 2 insns. */
478 case BPF_RET
| BPF_A
:
479 case BPF_RET
| BPF_K
:
480 *insn
++ = BPF_MOV32_RAW(BPF_RVAL(fp
->code
) == BPF_K
?
481 BPF_K
: BPF_X
, BPF_REG_0
,
483 *insn
= BPF_EXIT_INSN();
486 /* Store to stack. */
489 *insn
= BPF_STX_MEM(BPF_W
, BPF_REG_FP
, BPF_CLASS(fp
->code
) ==
490 BPF_ST
? BPF_REG_A
: BPF_REG_X
,
491 -(BPF_MEMWORDS
- fp
->k
) * 4);
494 /* Load from stack. */
495 case BPF_LD
| BPF_MEM
:
496 case BPF_LDX
| BPF_MEM
:
497 *insn
= BPF_LDX_MEM(BPF_W
, BPF_CLASS(fp
->code
) == BPF_LD
?
498 BPF_REG_A
: BPF_REG_X
, BPF_REG_FP
,
499 -(BPF_MEMWORDS
- fp
->k
) * 4);
503 case BPF_LD
| BPF_IMM
:
504 case BPF_LDX
| BPF_IMM
:
505 *insn
= BPF_MOV32_IMM(BPF_CLASS(fp
->code
) == BPF_LD
?
506 BPF_REG_A
: BPF_REG_X
, fp
->k
);
510 case BPF_MISC
| BPF_TAX
:
511 *insn
= BPF_MOV64_REG(BPF_REG_X
, BPF_REG_A
);
515 case BPF_MISC
| BPF_TXA
:
516 *insn
= BPF_MOV64_REG(BPF_REG_A
, BPF_REG_X
);
519 /* A = skb->len or X = skb->len */
520 case BPF_LD
| BPF_W
| BPF_LEN
:
521 case BPF_LDX
| BPF_W
| BPF_LEN
:
522 *insn
= BPF_LDX_MEM(BPF_W
, BPF_CLASS(fp
->code
) == BPF_LD
?
523 BPF_REG_A
: BPF_REG_X
, BPF_REG_CTX
,
524 offsetof(struct sk_buff
, len
));
527 /* Access seccomp_data fields. */
528 case BPF_LDX
| BPF_ABS
| BPF_W
:
529 /* A = *(u32 *) (ctx + K) */
530 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_CTX
, fp
->k
);
533 /* Unkown instruction. */
540 memcpy(new_insn
, tmp_insns
,
541 sizeof(*insn
) * (insn
- tmp_insns
));
542 new_insn
+= insn
- tmp_insns
;
546 /* Only calculating new length. */
547 *new_len
= new_insn
- new_prog
;
552 if (new_flen
!= new_insn
- new_prog
) {
553 new_flen
= new_insn
- new_prog
;
560 BUG_ON(*new_len
!= new_flen
);
569 * As we dont want to clear mem[] array for each packet going through
570 * __bpf_prog_run(), we check that filter loaded by user never try to read
571 * a cell if not previously written, and we check all branches to be sure
572 * a malicious user doesn't try to abuse us.
574 static int check_load_and_stores(const struct sock_filter
*filter
, int flen
)
576 u16
*masks
, memvalid
= 0; /* One bit per cell, 16 cells */
579 BUILD_BUG_ON(BPF_MEMWORDS
> 16);
581 masks
= kmalloc_array(flen
, sizeof(*masks
), GFP_KERNEL
);
585 memset(masks
, 0xff, flen
* sizeof(*masks
));
587 for (pc
= 0; pc
< flen
; pc
++) {
588 memvalid
&= masks
[pc
];
590 switch (filter
[pc
].code
) {
593 memvalid
|= (1 << filter
[pc
].k
);
595 case BPF_LD
| BPF_MEM
:
596 case BPF_LDX
| BPF_MEM
:
597 if (!(memvalid
& (1 << filter
[pc
].k
))) {
602 case BPF_JMP
| BPF_JA
:
603 /* A jump must set masks on target */
604 masks
[pc
+ 1 + filter
[pc
].k
] &= memvalid
;
607 case BPF_JMP
| BPF_JEQ
| BPF_K
:
608 case BPF_JMP
| BPF_JEQ
| BPF_X
:
609 case BPF_JMP
| BPF_JGE
| BPF_K
:
610 case BPF_JMP
| BPF_JGE
| BPF_X
:
611 case BPF_JMP
| BPF_JGT
| BPF_K
:
612 case BPF_JMP
| BPF_JGT
| BPF_X
:
613 case BPF_JMP
| BPF_JSET
| BPF_K
:
614 case BPF_JMP
| BPF_JSET
| BPF_X
:
615 /* A jump must set masks on targets */
616 masks
[pc
+ 1 + filter
[pc
].jt
] &= memvalid
;
617 masks
[pc
+ 1 + filter
[pc
].jf
] &= memvalid
;
627 static bool chk_code_allowed(u16 code_to_probe
)
629 static const bool codes
[] = {
630 /* 32 bit ALU operations */
631 [BPF_ALU
| BPF_ADD
| BPF_K
] = true,
632 [BPF_ALU
| BPF_ADD
| BPF_X
] = true,
633 [BPF_ALU
| BPF_SUB
| BPF_K
] = true,
634 [BPF_ALU
| BPF_SUB
| BPF_X
] = true,
635 [BPF_ALU
| BPF_MUL
| BPF_K
] = true,
636 [BPF_ALU
| BPF_MUL
| BPF_X
] = true,
637 [BPF_ALU
| BPF_DIV
| BPF_K
] = true,
638 [BPF_ALU
| BPF_DIV
| BPF_X
] = true,
639 [BPF_ALU
| BPF_MOD
| BPF_K
] = true,
640 [BPF_ALU
| BPF_MOD
| BPF_X
] = true,
641 [BPF_ALU
| BPF_AND
| BPF_K
] = true,
642 [BPF_ALU
| BPF_AND
| BPF_X
] = true,
643 [BPF_ALU
| BPF_OR
| BPF_K
] = true,
644 [BPF_ALU
| BPF_OR
| BPF_X
] = true,
645 [BPF_ALU
| BPF_XOR
| BPF_K
] = true,
646 [BPF_ALU
| BPF_XOR
| BPF_X
] = true,
647 [BPF_ALU
| BPF_LSH
| BPF_K
] = true,
648 [BPF_ALU
| BPF_LSH
| BPF_X
] = true,
649 [BPF_ALU
| BPF_RSH
| BPF_K
] = true,
650 [BPF_ALU
| BPF_RSH
| BPF_X
] = true,
651 [BPF_ALU
| BPF_NEG
] = true,
652 /* Load instructions */
653 [BPF_LD
| BPF_W
| BPF_ABS
] = true,
654 [BPF_LD
| BPF_H
| BPF_ABS
] = true,
655 [BPF_LD
| BPF_B
| BPF_ABS
] = true,
656 [BPF_LD
| BPF_W
| BPF_LEN
] = true,
657 [BPF_LD
| BPF_W
| BPF_IND
] = true,
658 [BPF_LD
| BPF_H
| BPF_IND
] = true,
659 [BPF_LD
| BPF_B
| BPF_IND
] = true,
660 [BPF_LD
| BPF_IMM
] = true,
661 [BPF_LD
| BPF_MEM
] = true,
662 [BPF_LDX
| BPF_W
| BPF_LEN
] = true,
663 [BPF_LDX
| BPF_B
| BPF_MSH
] = true,
664 [BPF_LDX
| BPF_IMM
] = true,
665 [BPF_LDX
| BPF_MEM
] = true,
666 /* Store instructions */
669 /* Misc instructions */
670 [BPF_MISC
| BPF_TAX
] = true,
671 [BPF_MISC
| BPF_TXA
] = true,
672 /* Return instructions */
673 [BPF_RET
| BPF_K
] = true,
674 [BPF_RET
| BPF_A
] = true,
675 /* Jump instructions */
676 [BPF_JMP
| BPF_JA
] = true,
677 [BPF_JMP
| BPF_JEQ
| BPF_K
] = true,
678 [BPF_JMP
| BPF_JEQ
| BPF_X
] = true,
679 [BPF_JMP
| BPF_JGE
| BPF_K
] = true,
680 [BPF_JMP
| BPF_JGE
| BPF_X
] = true,
681 [BPF_JMP
| BPF_JGT
| BPF_K
] = true,
682 [BPF_JMP
| BPF_JGT
| BPF_X
] = true,
683 [BPF_JMP
| BPF_JSET
| BPF_K
] = true,
684 [BPF_JMP
| BPF_JSET
| BPF_X
] = true,
687 if (code_to_probe
>= ARRAY_SIZE(codes
))
690 return codes
[code_to_probe
];
694 * bpf_check_classic - verify socket filter code
695 * @filter: filter to verify
696 * @flen: length of filter
698 * Check the user's filter code. If we let some ugly
699 * filter code slip through kaboom! The filter must contain
700 * no references or jumps that are out of range, no illegal
701 * instructions, and must end with a RET instruction.
703 * All jumps are forward as they are not signed.
705 * Returns 0 if the rule set is legal or -EINVAL if not.
707 int bpf_check_classic(const struct sock_filter
*filter
, unsigned int flen
)
712 if (flen
== 0 || flen
> BPF_MAXINSNS
)
715 /* Check the filter code now */
716 for (pc
= 0; pc
< flen
; pc
++) {
717 const struct sock_filter
*ftest
= &filter
[pc
];
719 /* May we actually operate on this code? */
720 if (!chk_code_allowed(ftest
->code
))
723 /* Some instructions need special checks */
724 switch (ftest
->code
) {
725 case BPF_ALU
| BPF_DIV
| BPF_K
:
726 case BPF_ALU
| BPF_MOD
| BPF_K
:
727 /* Check for division by zero */
731 case BPF_LD
| BPF_MEM
:
732 case BPF_LDX
| BPF_MEM
:
735 /* Check for invalid memory addresses */
736 if (ftest
->k
>= BPF_MEMWORDS
)
739 case BPF_JMP
| BPF_JA
:
740 /* Note, the large ftest->k might cause loops.
741 * Compare this with conditional jumps below,
742 * where offsets are limited. --ANK (981016)
744 if (ftest
->k
>= (unsigned int)(flen
- pc
- 1))
747 case BPF_JMP
| BPF_JEQ
| BPF_K
:
748 case BPF_JMP
| BPF_JEQ
| BPF_X
:
749 case BPF_JMP
| BPF_JGE
| BPF_K
:
750 case BPF_JMP
| BPF_JGE
| BPF_X
:
751 case BPF_JMP
| BPF_JGT
| BPF_K
:
752 case BPF_JMP
| BPF_JGT
| BPF_X
:
753 case BPF_JMP
| BPF_JSET
| BPF_K
:
754 case BPF_JMP
| BPF_JSET
| BPF_X
:
755 /* Both conditionals must be safe */
756 if (pc
+ ftest
->jt
+ 1 >= flen
||
757 pc
+ ftest
->jf
+ 1 >= flen
)
760 case BPF_LD
| BPF_W
| BPF_ABS
:
761 case BPF_LD
| BPF_H
| BPF_ABS
:
762 case BPF_LD
| BPF_B
| BPF_ABS
:
764 if (bpf_anc_helper(ftest
) & BPF_ANC
)
766 /* Ancillary operation unknown or unsupported */
767 if (anc_found
== false && ftest
->k
>= SKF_AD_OFF
)
772 /* Last instruction must be a RET code */
773 switch (filter
[flen
- 1].code
) {
774 case BPF_RET
| BPF_K
:
775 case BPF_RET
| BPF_A
:
776 return check_load_and_stores(filter
, flen
);
781 EXPORT_SYMBOL(bpf_check_classic
);
783 static int bpf_prog_store_orig_filter(struct bpf_prog
*fp
,
784 const struct sock_fprog
*fprog
)
786 unsigned int fsize
= bpf_classic_proglen(fprog
);
787 struct sock_fprog_kern
*fkprog
;
789 fp
->orig_prog
= kmalloc(sizeof(*fkprog
), GFP_KERNEL
);
793 fkprog
= fp
->orig_prog
;
794 fkprog
->len
= fprog
->len
;
795 fkprog
->filter
= kmemdup(fp
->insns
, fsize
, GFP_KERNEL
);
796 if (!fkprog
->filter
) {
797 kfree(fp
->orig_prog
);
804 static void bpf_release_orig_filter(struct bpf_prog
*fp
)
806 struct sock_fprog_kern
*fprog
= fp
->orig_prog
;
809 kfree(fprog
->filter
);
814 static void __bpf_prog_release(struct bpf_prog
*prog
)
816 bpf_release_orig_filter(prog
);
820 static void __sk_filter_release(struct sk_filter
*fp
)
822 __bpf_prog_release(fp
->prog
);
827 * sk_filter_release_rcu - Release a socket filter by rcu_head
828 * @rcu: rcu_head that contains the sk_filter to free
830 static void sk_filter_release_rcu(struct rcu_head
*rcu
)
832 struct sk_filter
*fp
= container_of(rcu
, struct sk_filter
, rcu
);
834 __sk_filter_release(fp
);
838 * sk_filter_release - release a socket filter
839 * @fp: filter to remove
841 * Remove a filter from a socket and release its resources.
843 static void sk_filter_release(struct sk_filter
*fp
)
845 if (atomic_dec_and_test(&fp
->refcnt
))
846 call_rcu(&fp
->rcu
, sk_filter_release_rcu
);
849 void sk_filter_uncharge(struct sock
*sk
, struct sk_filter
*fp
)
851 u32 filter_size
= bpf_prog_size(fp
->prog
->len
);
853 atomic_sub(filter_size
, &sk
->sk_omem_alloc
);
854 sk_filter_release(fp
);
857 /* try to charge the socket memory if there is space available
858 * return true on success
860 bool sk_filter_charge(struct sock
*sk
, struct sk_filter
*fp
)
862 u32 filter_size
= bpf_prog_size(fp
->prog
->len
);
864 /* same check as in sock_kmalloc() */
865 if (filter_size
<= sysctl_optmem_max
&&
866 atomic_read(&sk
->sk_omem_alloc
) + filter_size
< sysctl_optmem_max
) {
867 atomic_inc(&fp
->refcnt
);
868 atomic_add(filter_size
, &sk
->sk_omem_alloc
);
874 static struct bpf_prog
*bpf_migrate_filter(struct bpf_prog
*fp
)
876 struct sock_filter
*old_prog
;
877 struct bpf_prog
*old_fp
;
878 int err
, new_len
, old_len
= fp
->len
;
880 /* We are free to overwrite insns et al right here as it
881 * won't be used at this point in time anymore internally
882 * after the migration to the internal BPF instruction
885 BUILD_BUG_ON(sizeof(struct sock_filter
) !=
886 sizeof(struct bpf_insn
));
888 /* Conversion cannot happen on overlapping memory areas,
889 * so we need to keep the user BPF around until the 2nd
890 * pass. At this time, the user BPF is stored in fp->insns.
892 old_prog
= kmemdup(fp
->insns
, old_len
* sizeof(struct sock_filter
),
899 /* 1st pass: calculate the new program length. */
900 err
= bpf_convert_filter(old_prog
, old_len
, NULL
, &new_len
);
904 /* Expand fp for appending the new filter representation. */
906 fp
= bpf_prog_realloc(old_fp
, bpf_prog_size(new_len
), 0);
908 /* The old_fp is still around in case we couldn't
909 * allocate new memory, so uncharge on that one.
918 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
919 err
= bpf_convert_filter(old_prog
, old_len
, fp
->insnsi
, &new_len
);
921 /* 2nd bpf_convert_filter() can fail only if it fails
922 * to allocate memory, remapping must succeed. Note,
923 * that at this time old_fp has already been released
928 bpf_prog_select_runtime(fp
);
936 __bpf_prog_release(fp
);
940 static struct bpf_prog
*bpf_prepare_filter(struct bpf_prog
*fp
)
947 err
= bpf_check_classic(fp
->insns
, fp
->len
);
949 __bpf_prog_release(fp
);
953 /* Probe if we can JIT compile the filter and if so, do
954 * the compilation of the filter.
958 /* JIT compiler couldn't process this filter, so do the
959 * internal BPF translation for the optimized interpreter.
962 fp
= bpf_migrate_filter(fp
);
968 * bpf_prog_create - create an unattached filter
969 * @pfp: the unattached filter that is created
970 * @fprog: the filter program
972 * Create a filter independent of any socket. We first run some
973 * sanity checks on it to make sure it does not explode on us later.
974 * If an error occurs or there is insufficient memory for the filter
975 * a negative errno code is returned. On success the return is zero.
977 int bpf_prog_create(struct bpf_prog
**pfp
, struct sock_fprog_kern
*fprog
)
979 unsigned int fsize
= bpf_classic_proglen(fprog
);
982 /* Make sure new filter is there and in the right amounts. */
983 if (fprog
->filter
== NULL
)
986 fp
= bpf_prog_alloc(bpf_prog_size(fprog
->len
), 0);
990 memcpy(fp
->insns
, fprog
->filter
, fsize
);
992 fp
->len
= fprog
->len
;
993 /* Since unattached filters are not copied back to user
994 * space through sk_get_filter(), we do not need to hold
995 * a copy here, and can spare us the work.
997 fp
->orig_prog
= NULL
;
999 /* bpf_prepare_filter() already takes care of freeing
1000 * memory in case something goes wrong.
1002 fp
= bpf_prepare_filter(fp
);
1009 EXPORT_SYMBOL_GPL(bpf_prog_create
);
1011 void bpf_prog_destroy(struct bpf_prog
*fp
)
1013 __bpf_prog_release(fp
);
1015 EXPORT_SYMBOL_GPL(bpf_prog_destroy
);
1018 * sk_attach_filter - attach a socket filter
1019 * @fprog: the filter program
1020 * @sk: the socket to use
1022 * Attach the user's filter code. We first run some sanity checks on
1023 * it to make sure it does not explode on us later. If an error
1024 * occurs or there is insufficient memory for the filter a negative
1025 * errno code is returned. On success the return is zero.
1027 int sk_attach_filter(struct sock_fprog
*fprog
, struct sock
*sk
)
1029 struct sk_filter
*fp
, *old_fp
;
1030 unsigned int fsize
= bpf_classic_proglen(fprog
);
1031 unsigned int bpf_fsize
= bpf_prog_size(fprog
->len
);
1032 struct bpf_prog
*prog
;
1035 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
1038 /* Make sure new filter is there and in the right amounts. */
1039 if (fprog
->filter
== NULL
)
1042 prog
= bpf_prog_alloc(bpf_fsize
, 0);
1046 if (copy_from_user(prog
->insns
, fprog
->filter
, fsize
)) {
1047 __bpf_prog_free(prog
);
1051 prog
->len
= fprog
->len
;
1053 err
= bpf_prog_store_orig_filter(prog
, fprog
);
1055 __bpf_prog_free(prog
);
1059 /* bpf_prepare_filter() already takes care of freeing
1060 * memory in case something goes wrong.
1062 prog
= bpf_prepare_filter(prog
);
1064 return PTR_ERR(prog
);
1066 fp
= kmalloc(sizeof(*fp
), GFP_KERNEL
);
1068 __bpf_prog_release(prog
);
1073 atomic_set(&fp
->refcnt
, 0);
1075 if (!sk_filter_charge(sk
, fp
)) {
1076 __sk_filter_release(fp
);
1080 old_fp
= rcu_dereference_protected(sk
->sk_filter
,
1081 sock_owned_by_user(sk
));
1082 rcu_assign_pointer(sk
->sk_filter
, fp
);
1085 sk_filter_uncharge(sk
, old_fp
);
1089 EXPORT_SYMBOL_GPL(sk_attach_filter
);
1091 int sk_detach_filter(struct sock
*sk
)
1094 struct sk_filter
*filter
;
1096 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
1099 filter
= rcu_dereference_protected(sk
->sk_filter
,
1100 sock_owned_by_user(sk
));
1102 RCU_INIT_POINTER(sk
->sk_filter
, NULL
);
1103 sk_filter_uncharge(sk
, filter
);
1109 EXPORT_SYMBOL_GPL(sk_detach_filter
);
1111 int sk_get_filter(struct sock
*sk
, struct sock_filter __user
*ubuf
,
1114 struct sock_fprog_kern
*fprog
;
1115 struct sk_filter
*filter
;
1119 filter
= rcu_dereference_protected(sk
->sk_filter
,
1120 sock_owned_by_user(sk
));
1124 /* We're copying the filter that has been originally attached,
1125 * so no conversion/decode needed anymore.
1127 fprog
= filter
->prog
->orig_prog
;
1131 /* User space only enquires number of filter blocks. */
1135 if (len
< fprog
->len
)
1139 if (copy_to_user(ubuf
, fprog
->filter
, bpf_classic_proglen(fprog
)))
1142 /* Instead of bytes, the API requests to return the number