1 /*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
6 *
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>
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.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
22 */
23
24 #include <linux/module.h>
25 #include <linux/types.h>
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>
33 #include <linux/gfp.h>
34 #include <net/ip.h>
35 #include <net/protocol.h>
36 #include <net/netlink.h>
37 #include <linux/skbuff.h>
38 #include <net/sock.h>
39 #include <net/flow_dissector.h>
40 #include <linux/errno.h>
41 #include <linux/timer.h>
42 #include <asm/uaccess.h>
43 #include <asm/unaligned.h>
44 #include <linux/filter.h>
45 #include <linux/ratelimit.h>
46 #include <linux/seccomp.h>
47 #include <linux/if_vlan.h>
48 #include <linux/bpf.h>
49 #include <net/sch_generic.h>
50 #include <net/cls_cgroup.h>
51 #include <net/dst_metadata.h>
52 #include <net/dst.h>
53
54 /**
55 * sk_filter_trim_cap - run a packet through a socket filter
56 * @sk: sock associated with &sk_buff
57 * @skb: buffer to filter
58 * @cap: limit on how short the eBPF program may trim the packet
59 *
60 * Run the eBPF program and then cut skb->data to correct size returned by
61 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
62 * than pkt_len we keep whole skb->data. This is the socket level
63 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
64 * be accepted or -EPERM if the packet should be tossed.
65 *
66 */
sk_filter_trim_cap(struct sock * sk,struct sk_buff * skb,unsigned int cap)67 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
68 {
69 int err;
70 struct sk_filter *filter;
71
72 /*
73 * If the skb was allocated from pfmemalloc reserves, only
74 * allow SOCK_MEMALLOC sockets to use it as this socket is
75 * helping free memory
76 */
77 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
78 return -ENOMEM;
79
80 err = security_sock_rcv_skb(sk, skb);
81 if (err)
82 return err;
83
84 rcu_read_lock();
85 filter = rcu_dereference(sk->sk_filter);
86 if (filter) {
87 unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
88 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
89 }
90 rcu_read_unlock();
91
92 return err;
93 }
94 EXPORT_SYMBOL(sk_filter_trim_cap);
95
__skb_get_pay_offset(u64 ctx,u64 a,u64 x,u64 r4,u64 r5)96 static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
97 {
98 return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
99 }
100
__skb_get_nlattr(u64 ctx,u64 a,u64 x,u64 r4,u64 r5)101 static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
102 {
103 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
104 struct nlattr *nla;
105
106 if (skb_is_nonlinear(skb))
107 return 0;
108
109 if (skb->len < sizeof(struct nlattr))
110 return 0;
111
112 if (a > skb->len - sizeof(struct nlattr))
113 return 0;
114
115 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
116 if (nla)
117 return (void *) nla - (void *) skb->data;
118
119 return 0;
120 }
121
__skb_get_nlattr_nest(u64 ctx,u64 a,u64 x,u64 r4,u64 r5)122 static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
123 {
124 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
125 struct nlattr *nla;
126
127 if (skb_is_nonlinear(skb))
128 return 0;
129
130 if (skb->len < sizeof(struct nlattr))
131 return 0;
132
133 if (a > skb->len - sizeof(struct nlattr))
134 return 0;
135
136 nla = (struct nlattr *) &skb->data[a];
137 if (nla->nla_len > skb->len - a)
138 return 0;
139
140 nla = nla_find_nested(nla, x);
141 if (nla)
142 return (void *) nla - (void *) skb->data;
143
144 return 0;
145 }
146
__get_raw_cpu_id(u64 ctx,u64 a,u64 x,u64 r4,u64 r5)147 static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
148 {
149 return raw_smp_processor_id();
150 }
151
convert_skb_access(int skb_field,int dst_reg,int src_reg,struct bpf_insn * insn_buf)152 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
153 struct bpf_insn *insn_buf)
154 {
155 struct bpf_insn *insn = insn_buf;
156
157 switch (skb_field) {
158 case SKF_AD_MARK:
159 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
160
161 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
162 offsetof(struct sk_buff, mark));
163 break;
164
165 case SKF_AD_PKTTYPE:
166 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
167 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
168 #ifdef __BIG_ENDIAN_BITFIELD
169 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
170 #endif
171 break;
172
173 case SKF_AD_QUEUE:
174 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
175
176 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
177 offsetof(struct sk_buff, queue_mapping));
178 break;
179
180 case SKF_AD_VLAN_TAG:
181 case SKF_AD_VLAN_TAG_PRESENT:
182 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
183 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
184
185 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
186 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
187 offsetof(struct sk_buff, vlan_tci));
188 if (skb_field == SKF_AD_VLAN_TAG) {
189 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
190 ~VLAN_TAG_PRESENT);
191 } else {
192 /* dst_reg >>= 12 */
193 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
194 /* dst_reg &= 1 */
195 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
196 }
197 break;
198 }
199
200 return insn - insn_buf;
201 }
202
convert_bpf_extensions(struct sock_filter * fp,struct bpf_insn ** insnp)203 static bool convert_bpf_extensions(struct sock_filter *fp,
204 struct bpf_insn **insnp)
205 {
206 struct bpf_insn *insn = *insnp;
207 u32 cnt;
208
209 switch (fp->k) {
210 case SKF_AD_OFF + SKF_AD_PROTOCOL:
211 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
212
213 /* A = *(u16 *) (CTX + offsetof(protocol)) */
214 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
215 offsetof(struct sk_buff, protocol));
216 /* A = ntohs(A) [emitting a nop or swap16] */
217 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
218 break;
219
220 case SKF_AD_OFF + SKF_AD_PKTTYPE:
221 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
222 insn += cnt - 1;
223 break;
224
225 case SKF_AD_OFF + SKF_AD_IFINDEX:
226 case SKF_AD_OFF + SKF_AD_HATYPE:
227 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
228 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
229 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
230
231 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
232 BPF_REG_TMP, BPF_REG_CTX,
233 offsetof(struct sk_buff, dev));
234 /* if (tmp != 0) goto pc + 1 */
235 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
236 *insn++ = BPF_EXIT_INSN();
237 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
238 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
239 offsetof(struct net_device, ifindex));
240 else
241 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
242 offsetof(struct net_device, type));
243 break;
244
245 case SKF_AD_OFF + SKF_AD_MARK:
246 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
247 insn += cnt - 1;
248 break;
249
250 case SKF_AD_OFF + SKF_AD_RXHASH:
251 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
252
253 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
254 offsetof(struct sk_buff, hash));
255 break;
256
257 case SKF_AD_OFF + SKF_AD_QUEUE:
258 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
259 insn += cnt - 1;
260 break;
261
262 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
263 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
264 BPF_REG_A, BPF_REG_CTX, insn);
265 insn += cnt - 1;
266 break;
267
268 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
269 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
270 BPF_REG_A, BPF_REG_CTX, insn);
271 insn += cnt - 1;
272 break;
273
274 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
275 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
276
277 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
278 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
279 offsetof(struct sk_buff, vlan_proto));
280 /* A = ntohs(A) [emitting a nop or swap16] */
281 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
282 break;
283
284 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
285 case SKF_AD_OFF + SKF_AD_NLATTR:
286 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
287 case SKF_AD_OFF + SKF_AD_CPU:
288 case SKF_AD_OFF + SKF_AD_RANDOM:
289 /* arg1 = CTX */
290 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
291 /* arg2 = A */
292 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
293 /* arg3 = X */
294 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
295 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
296 switch (fp->k) {
297 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
298 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
299 break;
300 case SKF_AD_OFF + SKF_AD_NLATTR:
301 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
302 break;
303 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
304 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
305 break;
306 case SKF_AD_OFF + SKF_AD_CPU:
307 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
308 break;
309 case SKF_AD_OFF + SKF_AD_RANDOM:
310 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
311 bpf_user_rnd_init_once();
312 break;
313 }
314 break;
315
316 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
317 /* A ^= X */
318 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
319 break;
320
321 default:
322 /* This is just a dummy call to avoid letting the compiler
323 * evict __bpf_call_base() as an optimization. Placed here
324 * where no-one bothers.
325 */
326 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
327 return false;
328 }
329
330 *insnp = insn;
331 return true;
332 }
333
334 /**
335 * bpf_convert_filter - convert filter program
336 * @prog: the user passed filter program
337 * @len: the length of the user passed filter program
338 * @new_prog: buffer where converted program will be stored
339 * @new_len: pointer to store length of converted program
340 *
341 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
342 * Conversion workflow:
343 *
344 * 1) First pass for calculating the new program length:
345 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
346 *
347 * 2) 2nd pass to remap in two passes: 1st pass finds new
348 * jump offsets, 2nd pass remapping:
349 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
350 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
351 *
352 * User BPF's register A is mapped to our BPF register 6, user BPF
353 * register X is mapped to BPF register 7; frame pointer is always
354 * register 10; Context 'void *ctx' is stored in register 1, that is,
355 * for socket filters: ctx == 'struct sk_buff *', for seccomp:
356 * ctx == 'struct seccomp_data *'.
357 */
bpf_convert_filter(struct sock_filter * prog,int len,struct bpf_insn * new_prog,int * new_len)358 static int bpf_convert_filter(struct sock_filter *prog, int len,
359 struct bpf_insn *new_prog, int *new_len)
360 {
361 int new_flen = 0, pass = 0, target, i;
362 struct bpf_insn *new_insn;
363 struct sock_filter *fp;
364 int *addrs = NULL;
365 u8 bpf_src;
366
367 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
368 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
369
370 if (len <= 0 || len > BPF_MAXINSNS)
371 return -EINVAL;
372
373 if (new_prog) {
374 addrs = kcalloc(len, sizeof(*addrs),
375 GFP_KERNEL | __GFP_NOWARN);
376 if (!addrs)
377 return -ENOMEM;
378 }
379
380 do_pass:
381 new_insn = new_prog;
382 fp = prog;
383
384 if (new_insn)
385 *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
386 new_insn++;
387
388 for (i = 0; i < len; fp++, i++) {
389 struct bpf_insn tmp_insns[6] = { };
390 struct bpf_insn *insn = tmp_insns;
391
392 if (addrs)
393 addrs[i] = new_insn - new_prog;
394
395 switch (fp->code) {
396 /* All arithmetic insns and skb loads map as-is. */
397 case BPF_ALU | BPF_ADD | BPF_X:
398 case BPF_ALU | BPF_ADD | BPF_K:
399 case BPF_ALU | BPF_SUB | BPF_X:
400 case BPF_ALU | BPF_SUB | BPF_K:
401 case BPF_ALU | BPF_AND | BPF_X:
402 case BPF_ALU | BPF_AND | BPF_K:
403 case BPF_ALU | BPF_OR | BPF_X:
404 case BPF_ALU | BPF_OR | BPF_K:
405 case BPF_ALU | BPF_LSH | BPF_X:
406 case BPF_ALU | BPF_LSH | BPF_K:
407 case BPF_ALU | BPF_RSH | BPF_X:
408 case BPF_ALU | BPF_RSH | BPF_K:
409 case BPF_ALU | BPF_XOR | BPF_X:
410 case BPF_ALU | BPF_XOR | BPF_K:
411 case BPF_ALU | BPF_MUL | BPF_X:
412 case BPF_ALU | BPF_MUL | BPF_K:
413 case BPF_ALU | BPF_DIV | BPF_X:
414 case BPF_ALU | BPF_DIV | BPF_K:
415 case BPF_ALU | BPF_MOD | BPF_X:
416 case BPF_ALU | BPF_MOD | BPF_K:
417 case BPF_ALU | BPF_NEG:
418 case BPF_LD | BPF_ABS | BPF_W:
419 case BPF_LD | BPF_ABS | BPF_H:
420 case BPF_LD | BPF_ABS | BPF_B:
421 case BPF_LD | BPF_IND | BPF_W:
422 case BPF_LD | BPF_IND | BPF_H:
423 case BPF_LD | BPF_IND | BPF_B:
424 /* Check for overloaded BPF extension and
425 * directly convert it if found, otherwise
426 * just move on with mapping.
427 */
428 if (BPF_CLASS(fp->code) == BPF_LD &&
429 BPF_MODE(fp->code) == BPF_ABS &&
430 convert_bpf_extensions(fp, &insn))
431 break;
432
433 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
434 fp->code == (BPF_ALU | BPF_MOD | BPF_X))
435 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
436
437 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
438 break;
439
440 /* Jump transformation cannot use BPF block macros
441 * everywhere as offset calculation and target updates
442 * require a bit more work than the rest, i.e. jump
443 * opcodes map as-is, but offsets need adjustment.
444 */
445
446 #define BPF_EMIT_JMP \
447 do { \
448 if (target >= len || target < 0) \
449 goto err; \
450 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
451 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
452 insn->off -= insn - tmp_insns; \
453 } while (0)
454
455 case BPF_JMP | BPF_JA:
456 target = i + fp->k + 1;
457 insn->code = fp->code;
458 BPF_EMIT_JMP;
459 break;
460
461 case BPF_JMP | BPF_JEQ | BPF_K:
462 case BPF_JMP | BPF_JEQ | BPF_X:
463 case BPF_JMP | BPF_JSET | BPF_K:
464 case BPF_JMP | BPF_JSET | BPF_X:
465 case BPF_JMP | BPF_JGT | BPF_K:
466 case BPF_JMP | BPF_JGT | BPF_X:
467 case BPF_JMP | BPF_JGE | BPF_K:
468 case BPF_JMP | BPF_JGE | BPF_X:
469 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
470 /* BPF immediates are signed, zero extend
471 * immediate into tmp register and use it
472 * in compare insn.
473 */
474 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
475
476 insn->dst_reg = BPF_REG_A;
477 insn->src_reg = BPF_REG_TMP;
478 bpf_src = BPF_X;
479 } else {
480 insn->dst_reg = BPF_REG_A;
481 insn->imm = fp->k;
482 bpf_src = BPF_SRC(fp->code);
483 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
484 }
485
486 /* Common case where 'jump_false' is next insn. */
487 if (fp->jf == 0) {
488 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
489 target = i + fp->jt + 1;
490 BPF_EMIT_JMP;
491 break;
492 }
493
494 /* Convert JEQ into JNE when 'jump_true' is next insn. */
495 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
496 insn->code = BPF_JMP | BPF_JNE | bpf_src;
497 target = i + fp->jf + 1;
498 BPF_EMIT_JMP;
499 break;
500 }
501
502 /* Other jumps are mapped into two insns: Jxx and JA. */
503 target = i + fp->jt + 1;
504 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
505 BPF_EMIT_JMP;
506 insn++;
507
508 insn->code = BPF_JMP | BPF_JA;
509 target = i + fp->jf + 1;
510 BPF_EMIT_JMP;
511 break;
512
513 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
514 case BPF_LDX | BPF_MSH | BPF_B:
515 /* tmp = A */
516 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
517 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
518 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
519 /* A &= 0xf */
520 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
521 /* A <<= 2 */
522 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
523 /* X = A */
524 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
525 /* A = tmp */
526 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
527 break;
528
529 /* RET_K, RET_A are remaped into 2 insns. */
530 case BPF_RET | BPF_A:
531 case BPF_RET | BPF_K:
532 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
533 BPF_K : BPF_X, BPF_REG_0,
534 BPF_REG_A, fp->k);
535 *insn = BPF_EXIT_INSN();
536 break;
537
538 /* Store to stack. */
539 case BPF_ST:
540 case BPF_STX:
541 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
542 BPF_ST ? BPF_REG_A : BPF_REG_X,
543 -(BPF_MEMWORDS - fp->k) * 4);
544 break;
545
546 /* Load from stack. */
547 case BPF_LD | BPF_MEM:
548 case BPF_LDX | BPF_MEM:
549 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
550 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
551 -(BPF_MEMWORDS - fp->k) * 4);
552 break;
553
554 /* A = K or X = K */
555 case BPF_LD | BPF_IMM:
556 case BPF_LDX | BPF_IMM:
557 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
558 BPF_REG_A : BPF_REG_X, fp->k);
559 break;
560
561 /* X = A */
562 case BPF_MISC | BPF_TAX:
563 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
564 break;
565
566 /* A = X */
567 case BPF_MISC | BPF_TXA:
568 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
569 break;
570
571 /* A = skb->len or X = skb->len */
572 case BPF_LD | BPF_W | BPF_LEN:
573 case BPF_LDX | BPF_W | BPF_LEN:
574 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
575 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
576 offsetof(struct sk_buff, len));
577 break;
578
579 /* Access seccomp_data fields. */
580 case BPF_LDX | BPF_ABS | BPF_W:
581 /* A = *(u32 *) (ctx + K) */
582 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
583 break;
584
585 /* Unknown instruction. */
586 default:
587 goto err;
588 }
589
590 insn++;
591 if (new_prog)
592 memcpy(new_insn, tmp_insns,
593 sizeof(*insn) * (insn - tmp_insns));
594 new_insn += insn - tmp_insns;
595 }
596
597 if (!new_prog) {
598 /* Only calculating new length. */
599 *new_len = new_insn - new_prog;
600 return 0;
601 }
602
603 pass++;
604 if (new_flen != new_insn - new_prog) {
605 new_flen = new_insn - new_prog;
606 if (pass > 2)
607 goto err;
608 goto do_pass;
609 }
610
611 kfree(addrs);
612 BUG_ON(*new_len != new_flen);
613 return 0;
614 err:
615 kfree(addrs);
616 return -EINVAL;
617 }
618
619 /* Security:
620 *
621 * As we dont want to clear mem[] array for each packet going through
622 * __bpf_prog_run(), we check that filter loaded by user never try to read
623 * a cell if not previously written, and we check all branches to be sure
624 * a malicious user doesn't try to abuse us.
625 */
check_load_and_stores(const struct sock_filter * filter,int flen)626 static int check_load_and_stores(const struct sock_filter *filter, int flen)
627 {
628 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
629 int pc, ret = 0;
630
631 BUILD_BUG_ON(BPF_MEMWORDS > 16);
632
633 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
634 if (!masks)
635 return -ENOMEM;
636
637 memset(masks, 0xff, flen * sizeof(*masks));
638
639 for (pc = 0; pc < flen; pc++) {
640 memvalid &= masks[pc];
641
642 switch (filter[pc].code) {
643 case BPF_ST:
644 case BPF_STX:
645 memvalid |= (1 << filter[pc].k);
646 break;
647 case BPF_LD | BPF_MEM:
648 case BPF_LDX | BPF_MEM:
649 if (!(memvalid & (1 << filter[pc].k))) {
650 ret = -EINVAL;
651 goto error;
652 }
653 break;
654 case BPF_JMP | BPF_JA:
655 /* A jump must set masks on target */
656 masks[pc + 1 + filter[pc].k] &= memvalid;
657 memvalid = ~0;
658 break;
659 case BPF_JMP | BPF_JEQ | BPF_K:
660 case BPF_JMP | BPF_JEQ | BPF_X:
661 case BPF_JMP | BPF_JGE | BPF_K:
662 case BPF_JMP | BPF_JGE | BPF_X:
663 case BPF_JMP | BPF_JGT | BPF_K:
664 case BPF_JMP | BPF_JGT | BPF_X:
665 case BPF_JMP | BPF_JSET | BPF_K:
666 case BPF_JMP | BPF_JSET | BPF_X:
667 /* A jump must set masks on targets */
668 masks[pc + 1 + filter[pc].jt] &= memvalid;
669 masks[pc + 1 + filter[pc].jf] &= memvalid;
670 memvalid = ~0;
671 break;
672 }
673 }
674 error:
675 kfree(masks);
676 return ret;
677 }
678
chk_code_allowed(u16 code_to_probe)679 static bool chk_code_allowed(u16 code_to_probe)
680 {
681 static const bool codes[] = {
682 /* 32 bit ALU operations */
683 [BPF_ALU | BPF_ADD | BPF_K] = true,
684 [BPF_ALU | BPF_ADD | BPF_X] = true,
685 [BPF_ALU | BPF_SUB | BPF_K] = true,
686 [BPF_ALU | BPF_SUB | BPF_X] = true,
687 [BPF_ALU | BPF_MUL | BPF_K] = true,
688 [BPF_ALU | BPF_MUL | BPF_X] = true,
689 [BPF_ALU | BPF_DIV | BPF_K] = true,
690 [BPF_ALU | BPF_DIV | BPF_X] = true,
691 [BPF_ALU | BPF_MOD | BPF_K] = true,
692 [BPF_ALU | BPF_MOD | BPF_X] = true,
693 [BPF_ALU | BPF_AND | BPF_K] = true,
694 [BPF_ALU | BPF_AND | BPF_X] = true,
695 [BPF_ALU | BPF_OR | BPF_K] = true,
696 [BPF_ALU | BPF_OR | BPF_X] = true,
697 [BPF_ALU | BPF_XOR | BPF_K] = true,
698 [BPF_ALU | BPF_XOR | BPF_X] = true,
699 [BPF_ALU | BPF_LSH | BPF_K] = true,
700 [BPF_ALU | BPF_LSH | BPF_X] = true,
701 [BPF_ALU | BPF_RSH | BPF_K] = true,
702 [BPF_ALU | BPF_RSH | BPF_X] = true,
703 [BPF_ALU | BPF_NEG] = true,
704 /* Load instructions */
705 [BPF_LD | BPF_W | BPF_ABS] = true,
706 [BPF_LD | BPF_H | BPF_ABS] = true,
707 [BPF_LD | BPF_B | BPF_ABS] = true,
708 [BPF_LD | BPF_W | BPF_LEN] = true,
709 [BPF_LD | BPF_W | BPF_IND] = true,
710 [BPF_LD | BPF_H | BPF_IND] = true,
711 [BPF_LD | BPF_B | BPF_IND] = true,
712 [BPF_LD | BPF_IMM] = true,
713 [BPF_LD | BPF_MEM] = true,
714 [BPF_LDX | BPF_W | BPF_LEN] = true,
715 [BPF_LDX | BPF_B | BPF_MSH] = true,
716 [BPF_LDX | BPF_IMM] = true,
717 [BPF_LDX | BPF_MEM] = true,
718 /* Store instructions */
719 [BPF_ST] = true,
720 [BPF_STX] = true,
721 /* Misc instructions */
722 [BPF_MISC | BPF_TAX] = true,
723 [BPF_MISC | BPF_TXA] = true,
724 /* Return instructions */
725 [BPF_RET | BPF_K] = true,
726 [BPF_RET | BPF_A] = true,
727 /* Jump instructions */
728 [BPF_JMP | BPF_JA] = true,
729 [BPF_JMP | BPF_JEQ | BPF_K] = true,
730 [BPF_JMP | BPF_JEQ | BPF_X] = true,
731 [BPF_JMP | BPF_JGE | BPF_K] = true,
732 [BPF_JMP | BPF_JGE | BPF_X] = true,
733 [BPF_JMP | BPF_JGT | BPF_K] = true,
734 [BPF_JMP | BPF_JGT | BPF_X] = true,
735 [BPF_JMP | BPF_JSET | BPF_K] = true,
736 [BPF_JMP | BPF_JSET | BPF_X] = true,
737 };
738
739 if (code_to_probe >= ARRAY_SIZE(codes))
740 return false;
741
742 return codes[code_to_probe];
743 }
744
bpf_check_basics_ok(const struct sock_filter * filter,unsigned int flen)745 static bool bpf_check_basics_ok(const struct sock_filter *filter,
746 unsigned int flen)
747 {
748 if (filter == NULL)
749 return false;
750 if (flen == 0 || flen > BPF_MAXINSNS)
751 return false;
752
753 return true;
754 }
755
756 /**
757 * bpf_check_classic - verify socket filter code
758 * @filter: filter to verify
759 * @flen: length of filter
760 *
761 * Check the user's filter code. If we let some ugly
762 * filter code slip through kaboom! The filter must contain
763 * no references or jumps that are out of range, no illegal
764 * instructions, and must end with a RET instruction.
765 *
766 * All jumps are forward as they are not signed.
767 *
768 * Returns 0 if the rule set is legal or -EINVAL if not.
769 */
bpf_check_classic(const struct sock_filter * filter,unsigned int flen)770 static int bpf_check_classic(const struct sock_filter *filter,
771 unsigned int flen)
772 {
773 bool anc_found;
774 int pc;
775
776 /* Check the filter code now */
777 for (pc = 0; pc < flen; pc++) {
778 const struct sock_filter *ftest = &filter[pc];
779
780 /* May we actually operate on this code? */
781 if (!chk_code_allowed(ftest->code))
782 return -EINVAL;
783
784 /* Some instructions need special checks */
785 switch (ftest->code) {
786 case BPF_ALU | BPF_DIV | BPF_K:
787 case BPF_ALU | BPF_MOD | BPF_K:
788 /* Check for division by zero */
789 if (ftest->k == 0)
790 return -EINVAL;
791 break;
792 case BPF_ALU | BPF_LSH | BPF_K:
793 case BPF_ALU | BPF_RSH | BPF_K:
794 if (ftest->k >= 32)
795 return -EINVAL;
796 break;
797 case BPF_LD | BPF_MEM:
798 case BPF_LDX | BPF_MEM:
799 case BPF_ST:
800 case BPF_STX:
801 /* Check for invalid memory addresses */
802 if (ftest->k >= BPF_MEMWORDS)
803 return -EINVAL;
804 break;
805 case BPF_JMP | BPF_JA:
806 /* Note, the large ftest->k might cause loops.
807 * Compare this with conditional jumps below,
808 * where offsets are limited. --ANK (981016)
809 */
810 if (ftest->k >= (unsigned int)(flen - pc - 1))
811 return -EINVAL;
812 break;
813 case BPF_JMP | BPF_JEQ | BPF_K:
814 case BPF_JMP | BPF_JEQ | BPF_X:
815 case BPF_JMP | BPF_JGE | BPF_K:
816 case BPF_JMP | BPF_JGE | BPF_X:
817 case BPF_JMP | BPF_JGT | BPF_K:
818 case BPF_JMP | BPF_JGT | BPF_X:
819 case BPF_JMP | BPF_JSET | BPF_K:
820 case BPF_JMP | BPF_JSET | BPF_X:
821 /* Both conditionals must be safe */
822 if (pc + ftest->jt + 1 >= flen ||
823 pc + ftest->jf + 1 >= flen)
824 return -EINVAL;
825 break;
826 case BPF_LD | BPF_W | BPF_ABS:
827 case BPF_LD | BPF_H | BPF_ABS:
828 case BPF_LD | BPF_B | BPF_ABS:
829 anc_found = false;
830 if (bpf_anc_helper(ftest) & BPF_ANC)
831 anc_found = true;
832 /* Ancillary operation unknown or unsupported */
833 if (anc_found == false && ftest->k >= SKF_AD_OFF)
834 return -EINVAL;
835 }
836 }
837
838 /* Last instruction must be a RET code */
839 switch (filter[flen - 1].code) {
840 case BPF_RET | BPF_K:
841 case BPF_RET | BPF_A:
842 return check_load_and_stores(filter, flen);
843 }
844
845 return -EINVAL;
846 }
847
bpf_prog_store_orig_filter(struct bpf_prog * fp,const struct sock_fprog * fprog)848 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
849 const struct sock_fprog *fprog)
850 {
851 unsigned int fsize = bpf_classic_proglen(fprog);
852 struct sock_fprog_kern *fkprog;
853
854 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
855 if (!fp->orig_prog)
856 return -ENOMEM;
857
858 fkprog = fp->orig_prog;
859 fkprog->len = fprog->len;
860
861 fkprog->filter = kmemdup(fp->insns, fsize,
862 GFP_KERNEL | __GFP_NOWARN);
863 if (!fkprog->filter) {
864 kfree(fp->orig_prog);
865 return -ENOMEM;
866 }
867
868 return 0;
869 }
870
bpf_release_orig_filter(struct bpf_prog * fp)871 static void bpf_release_orig_filter(struct bpf_prog *fp)
872 {
873 struct sock_fprog_kern *fprog = fp->orig_prog;
874
875 if (fprog) {
876 kfree(fprog->filter);
877 kfree(fprog);
878 }
879 }
880
__bpf_prog_release(struct bpf_prog * prog)881 static void __bpf_prog_release(struct bpf_prog *prog)
882 {
883 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
884 bpf_prog_put(prog);
885 } else {
886 bpf_release_orig_filter(prog);
887 bpf_prog_free(prog);
888 }
889 }
890
__sk_filter_release(struct sk_filter * fp)891 static void __sk_filter_release(struct sk_filter *fp)
892 {
893 __bpf_prog_release(fp->prog);
894 kfree(fp);
895 }
896
897 /**
898 * sk_filter_release_rcu - Release a socket filter by rcu_head
899 * @rcu: rcu_head that contains the sk_filter to free
900 */
sk_filter_release_rcu(struct rcu_head * rcu)901 static void sk_filter_release_rcu(struct rcu_head *rcu)
902 {
903 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
904
905 __sk_filter_release(fp);
906 }
907
908 /**
909 * sk_filter_release - release a socket filter
910 * @fp: filter to remove
911 *
912 * Remove a filter from a socket and release its resources.
913 */
sk_filter_release(struct sk_filter * fp)914 static void sk_filter_release(struct sk_filter *fp)
915 {
916 if (atomic_dec_and_test(&fp->refcnt))
917 call_rcu(&fp->rcu, sk_filter_release_rcu);
918 }
919
sk_filter_uncharge(struct sock * sk,struct sk_filter * fp)920 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
921 {
922 u32 filter_size = bpf_prog_size(fp->prog->len);
923
924 atomic_sub(filter_size, &sk->sk_omem_alloc);
925 sk_filter_release(fp);
926 }
927
928 /* try to charge the socket memory if there is space available
929 * return true on success
930 */
sk_filter_charge(struct sock * sk,struct sk_filter * fp)931 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
932 {
933 u32 filter_size = bpf_prog_size(fp->prog->len);
934
935 /* same check as in sock_kmalloc() */
936 if (filter_size <= sysctl_optmem_max &&
937 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
938 atomic_inc(&fp->refcnt);
939 atomic_add(filter_size, &sk->sk_omem_alloc);
940 return true;
941 }
942 return false;
943 }
944
bpf_migrate_filter(struct bpf_prog * fp)945 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
946 {
947 struct sock_filter *old_prog;
948 struct bpf_prog *old_fp;
949 int err, new_len, old_len = fp->len;
950
951 /* We are free to overwrite insns et al right here as it
952 * won't be used at this point in time anymore internally
953 * after the migration to the internal BPF instruction
954 * representation.
955 */
956 BUILD_BUG_ON(sizeof(struct sock_filter) !=
957 sizeof(struct bpf_insn));
958
959 /* Conversion cannot happen on overlapping memory areas,
960 * so we need to keep the user BPF around until the 2nd
961 * pass. At this time, the user BPF is stored in fp->insns.
962 */
963 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
964 GFP_KERNEL | __GFP_NOWARN);
965 if (!old_prog) {
966 err = -ENOMEM;
967 goto out_err;
968 }
969
970 /* 1st pass: calculate the new program length. */
971 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
972 if (err)
973 goto out_err_free;
974
975 /* Expand fp for appending the new filter representation. */
976 old_fp = fp;
977 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
978 if (!fp) {
979 /* The old_fp is still around in case we couldn't
980 * allocate new memory, so uncharge on that one.
981 */
982 fp = old_fp;
983 err = -ENOMEM;
984 goto out_err_free;
985 }
986
987 fp->len = new_len;
988
989 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
990 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
991 if (err)
992 /* 2nd bpf_convert_filter() can fail only if it fails
993 * to allocate memory, remapping must succeed. Note,
994 * that at this time old_fp has already been released
995 * by krealloc().
996 */
997 goto out_err_free;
998
999 err = bpf_prog_select_runtime(fp);
1000 if (err)
1001 goto out_err_free;
1002
1003 kfree(old_prog);
1004 return fp;
1005
1006 out_err_free:
1007 kfree(old_prog);
1008 out_err:
1009 __bpf_prog_release(fp);
1010 return ERR_PTR(err);
1011 }
1012
bpf_prepare_filter(struct bpf_prog * fp,bpf_aux_classic_check_t trans)1013 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1014 bpf_aux_classic_check_t trans)
1015 {
1016 int err;
1017
1018 fp->bpf_func = NULL;
1019 fp->jited = 0;
1020
1021 err = bpf_check_classic(fp->insns, fp->len);
1022 if (err) {
1023 __bpf_prog_release(fp);
1024 return ERR_PTR(err);
1025 }
1026
1027 /* There might be additional checks and transformations
1028 * needed on classic filters, f.e. in case of seccomp.
1029 */
1030 if (trans) {
1031 err = trans(fp->insns, fp->len);
1032 if (err) {
1033 __bpf_prog_release(fp);
1034 return ERR_PTR(err);
1035 }
1036 }
1037
1038 /* Probe if we can JIT compile the filter and if so, do
1039 * the compilation of the filter.
1040 */
1041 bpf_jit_compile(fp);
1042
1043 /* JIT compiler couldn't process this filter, so do the
1044 * internal BPF translation for the optimized interpreter.
1045 */
1046 if (!fp->jited)
1047 fp = bpf_migrate_filter(fp);
1048
1049 return fp;
1050 }
1051
1052 /**
1053 * bpf_prog_create - create an unattached filter
1054 * @pfp: the unattached filter that is created
1055 * @fprog: the filter program
1056 *
1057 * Create a filter independent of any socket. We first run some
1058 * sanity checks on it to make sure it does not explode on us later.
1059 * If an error occurs or there is insufficient memory for the filter
1060 * a negative errno code is returned. On success the return is zero.
1061 */
bpf_prog_create(struct bpf_prog ** pfp,struct sock_fprog_kern * fprog)1062 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1063 {
1064 unsigned int fsize = bpf_classic_proglen(fprog);
1065 struct bpf_prog *fp;
1066
1067 /* Make sure new filter is there and in the right amounts. */
1068 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1069 return -EINVAL;
1070
1071 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1072 if (!fp)
1073 return -ENOMEM;
1074
1075 memcpy(fp->insns, fprog->filter, fsize);
1076
1077 fp->len = fprog->len;
1078 /* Since unattached filters are not copied back to user
1079 * space through sk_get_filter(), we do not need to hold
1080 * a copy here, and can spare us the work.
1081 */
1082 fp->orig_prog = NULL;
1083
1084 /* bpf_prepare_filter() already takes care of freeing
1085 * memory in case something goes wrong.
1086 */
1087 fp = bpf_prepare_filter(fp, NULL);
1088 if (IS_ERR(fp))
1089 return PTR_ERR(fp);
1090
1091 *pfp = fp;
1092 return 0;
1093 }
1094 EXPORT_SYMBOL_GPL(bpf_prog_create);
1095
1096 /**
1097 * bpf_prog_create_from_user - create an unattached filter from user buffer
1098 * @pfp: the unattached filter that is created
1099 * @fprog: the filter program
1100 * @trans: post-classic verifier transformation handler
1101 * @save_orig: save classic BPF program
1102 *
1103 * This function effectively does the same as bpf_prog_create(), only
1104 * that it builds up its insns buffer from user space provided buffer.
1105 * It also allows for passing a bpf_aux_classic_check_t handler.
1106 */
bpf_prog_create_from_user(struct bpf_prog ** pfp,struct sock_fprog * fprog,bpf_aux_classic_check_t trans,bool save_orig)1107 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1108 bpf_aux_classic_check_t trans, bool save_orig)
1109 {
1110 unsigned int fsize = bpf_classic_proglen(fprog);
1111 struct bpf_prog *fp;
1112 int err;
1113
1114 /* Make sure new filter is there and in the right amounts. */
1115 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1116 return -EINVAL;
1117
1118 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1119 if (!fp)
1120 return -ENOMEM;
1121
1122 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1123 __bpf_prog_free(fp);
1124 return -EFAULT;
1125 }
1126
1127 fp->len = fprog->len;
1128 fp->orig_prog = NULL;
1129
1130 if (save_orig) {
1131 err = bpf_prog_store_orig_filter(fp, fprog);
1132 if (err) {
1133 __bpf_prog_free(fp);
1134 return -ENOMEM;
1135 }
1136 }
1137
1138 /* bpf_prepare_filter() already takes care of freeing
1139 * memory in case something goes wrong.
1140 */
1141 fp = bpf_prepare_filter(fp, trans);
1142 if (IS_ERR(fp))
1143 return PTR_ERR(fp);
1144
1145 *pfp = fp;
1146 return 0;
1147 }
1148 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1149
bpf_prog_destroy(struct bpf_prog * fp)1150 void bpf_prog_destroy(struct bpf_prog *fp)
1151 {
1152 __bpf_prog_release(fp);
1153 }
1154 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1155
__sk_attach_prog(struct bpf_prog * prog,struct sock * sk,bool locked)1156 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk,
1157 bool locked)
1158 {
1159 struct sk_filter *fp, *old_fp;
1160
1161 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1162 if (!fp)
1163 return -ENOMEM;
1164
1165 fp->prog = prog;
1166 atomic_set(&fp->refcnt, 0);
1167
1168 if (!sk_filter_charge(sk, fp)) {
1169 kfree(fp);
1170 return -ENOMEM;
1171 }
1172
1173 old_fp = rcu_dereference_protected(sk->sk_filter, locked);
1174 rcu_assign_pointer(sk->sk_filter, fp);
1175 if (old_fp)
1176 sk_filter_uncharge(sk, old_fp);
1177
1178 return 0;
1179 }
1180
1181 /**
1182 * sk_attach_filter - attach a socket filter
1183 * @fprog: the filter program
1184 * @sk: the socket to use
1185 *
1186 * Attach the user's filter code. We first run some sanity checks on
1187 * it to make sure it does not explode on us later. If an error
1188 * occurs or there is insufficient memory for the filter a negative
1189 * errno code is returned. On success the return is zero.
1190 */
__sk_attach_filter(struct sock_fprog * fprog,struct sock * sk,bool locked)1191 int __sk_attach_filter(struct sock_fprog *fprog, struct sock *sk,
1192 bool locked)
1193 {
1194 unsigned int fsize = bpf_classic_proglen(fprog);
1195 struct bpf_prog *prog;
1196 int err;
1197
1198 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1199 return -EPERM;
1200
1201 /* Make sure new filter is there and in the right amounts. */
1202 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1203 return -EINVAL;
1204
1205 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1206 if (!prog)
1207 return -ENOMEM;
1208
1209 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1210 __bpf_prog_free(prog);
1211 return -EFAULT;
1212 }
1213
1214 prog->len = fprog->len;
1215
1216 err = bpf_prog_store_orig_filter(prog, fprog);
1217 if (err) {
1218 __bpf_prog_free(prog);
1219 return -ENOMEM;
1220 }
1221
1222 /* bpf_prepare_filter() already takes care of freeing
1223 * memory in case something goes wrong.
1224 */
1225 prog = bpf_prepare_filter(prog, NULL);
1226 if (IS_ERR(prog))
1227 return PTR_ERR(prog);
1228
1229 err = __sk_attach_prog(prog, sk, locked);
1230 if (err < 0) {
1231 __bpf_prog_release(prog);
1232 return err;
1233 }
1234
1235 return 0;
1236 }
1237 EXPORT_SYMBOL_GPL(__sk_attach_filter);
1238
sk_attach_filter(struct sock_fprog * fprog,struct sock * sk)1239 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1240 {
1241 return __sk_attach_filter(fprog, sk, sock_owned_by_user(sk));
1242 }
1243
sk_attach_bpf(u32 ufd,struct sock * sk)1244 int sk_attach_bpf(u32 ufd, struct sock *sk)
1245 {
1246 struct bpf_prog *prog;
1247 int err;
1248
1249 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1250 return -EPERM;
1251
1252 prog = bpf_prog_get(ufd);
1253 if (IS_ERR(prog))
1254 return PTR_ERR(prog);
1255
1256 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1257 bpf_prog_put(prog);
1258 return -EINVAL;
1259 }
1260
1261 err = __sk_attach_prog(prog, sk, sock_owned_by_user(sk));
1262 if (err < 0) {
1263 bpf_prog_put(prog);
1264 return err;
1265 }
1266
1267 return 0;
1268 }
1269
1270 #define BPF_RECOMPUTE_CSUM(flags) ((flags) & 1)
1271
bpf_skb_store_bytes(u64 r1,u64 r2,u64 r3,u64 r4,u64 flags)1272 static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
1273 {
1274 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1275 int offset = (int) r2;
1276 void *from = (void *) (long) r3;
1277 unsigned int len = (unsigned int) r4;
1278 char buf[16];
1279 void *ptr;
1280
1281 /* bpf verifier guarantees that:
1282 * 'from' pointer points to bpf program stack
1283 * 'len' bytes of it were initialized
1284 * 'len' > 0
1285 * 'skb' is a valid pointer to 'struct sk_buff'
1286 *
1287 * so check for invalid 'offset' and too large 'len'
1288 */
1289 if (unlikely((u32) offset > 0xffff || len > sizeof(buf)))
1290 return -EFAULT;
1291 if (unlikely(skb_try_make_writable(skb, offset + len)))
1292 return -EFAULT;
1293
1294 ptr = skb_header_pointer(skb, offset, len, buf);
1295 if (unlikely(!ptr))
1296 return -EFAULT;
1297
1298 if (BPF_RECOMPUTE_CSUM(flags))
1299 skb_postpull_rcsum(skb, ptr, len);
1300
1301 memcpy(ptr, from, len);
1302
1303 if (ptr == buf)
1304 /* skb_store_bits cannot return -EFAULT here */
1305 skb_store_bits(skb, offset, ptr, len);
1306
1307 if (BPF_RECOMPUTE_CSUM(flags) && skb->ip_summed == CHECKSUM_COMPLETE)
1308 skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0));
1309 return 0;
1310 }
1311
1312 const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1313 .func = bpf_skb_store_bytes,
1314 .gpl_only = false,
1315 .ret_type = RET_INTEGER,
1316 .arg1_type = ARG_PTR_TO_CTX,
1317 .arg2_type = ARG_ANYTHING,
1318 .arg3_type = ARG_PTR_TO_STACK,
1319 .arg4_type = ARG_CONST_STACK_SIZE,
1320 .arg5_type = ARG_ANYTHING,
1321 };
1322
1323 #define BPF_HEADER_FIELD_SIZE(flags) ((flags) & 0x0f)
1324 #define BPF_IS_PSEUDO_HEADER(flags) ((flags) & 0x10)
1325
bpf_l3_csum_replace(u64 r1,u64 r2,u64 from,u64 to,u64 flags)1326 static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1327 {
1328 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1329 int offset = (int) r2;
1330 __sum16 sum, *ptr;
1331
1332 if (unlikely((u32) offset > 0xffff))
1333 return -EFAULT;
1334
1335 if (unlikely(skb_try_make_writable(skb, offset + sizeof(sum))))
1336 return -EFAULT;
1337
1338 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1339 if (unlikely(!ptr))
1340 return -EFAULT;
1341
1342 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1343 case 2:
1344 csum_replace2(ptr, from, to);
1345 break;
1346 case 4:
1347 csum_replace4(ptr, from, to);
1348 break;
1349 default:
1350 return -EINVAL;
1351 }
1352
1353 if (ptr == &sum)
1354 /* skb_store_bits guaranteed to not return -EFAULT here */
1355 skb_store_bits(skb, offset, ptr, sizeof(sum));
1356
1357 return 0;
1358 }
1359
1360 const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1361 .func = bpf_l3_csum_replace,
1362 .gpl_only = false,
1363 .ret_type = RET_INTEGER,
1364 .arg1_type = ARG_PTR_TO_CTX,
1365 .arg2_type = ARG_ANYTHING,
1366 .arg3_type = ARG_ANYTHING,
1367 .arg4_type = ARG_ANYTHING,
1368 .arg5_type = ARG_ANYTHING,
1369 };
1370
bpf_l4_csum_replace(u64 r1,u64 r2,u64 from,u64 to,u64 flags)1371 static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1372 {
1373 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1374 bool is_pseudo = !!BPF_IS_PSEUDO_HEADER(flags);
1375 int offset = (int) r2;
1376 __sum16 sum, *ptr;
1377
1378 if (unlikely((u32) offset > 0xffff))
1379 return -EFAULT;
1380 if (unlikely(skb_try_make_writable(skb, offset + sizeof(sum))))
1381 return -EFAULT;
1382
1383 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1384 if (unlikely(!ptr))
1385 return -EFAULT;
1386
1387 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1388 case 2:
1389 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1390 break;
1391 case 4:
1392 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1393 break;
1394 default:
1395 return -EINVAL;
1396 }
1397
1398 if (ptr == &sum)
1399 /* skb_store_bits guaranteed to not return -EFAULT here */
1400 skb_store_bits(skb, offset, ptr, sizeof(sum));
1401
1402 return 0;
1403 }
1404
1405 const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1406 .func = bpf_l4_csum_replace,
1407 .gpl_only = false,
1408 .ret_type = RET_INTEGER,
1409 .arg1_type = ARG_PTR_TO_CTX,
1410 .arg2_type = ARG_ANYTHING,
1411 .arg3_type = ARG_ANYTHING,
1412 .arg4_type = ARG_ANYTHING,
1413 .arg5_type = ARG_ANYTHING,
1414 };
1415
1416 #define BPF_IS_REDIRECT_INGRESS(flags) ((flags) & 1)
1417
bpf_clone_redirect(u64 r1,u64 ifindex,u64 flags,u64 r4,u64 r5)1418 static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1419 {
1420 struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2;
1421 struct net_device *dev;
1422
1423 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1424 if (unlikely(!dev))
1425 return -EINVAL;
1426
1427 skb2 = skb_clone(skb, GFP_ATOMIC);
1428 if (unlikely(!skb2))
1429 return -ENOMEM;
1430
1431 if (BPF_IS_REDIRECT_INGRESS(flags))
1432 return dev_forward_skb(dev, skb2);
1433
1434 skb2->dev = dev;
1435 skb_sender_cpu_clear(skb2);
1436 return dev_queue_xmit(skb2);
1437 }
1438
1439 const struct bpf_func_proto bpf_clone_redirect_proto = {
1440 .func = bpf_clone_redirect,
1441 .gpl_only = false,
1442 .ret_type = RET_INTEGER,
1443 .arg1_type = ARG_PTR_TO_CTX,
1444 .arg2_type = ARG_ANYTHING,
1445 .arg3_type = ARG_ANYTHING,
1446 };
1447
1448 struct redirect_info {
1449 u32 ifindex;
1450 u32 flags;
1451 };
1452
1453 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
bpf_redirect(u64 ifindex,u64 flags,u64 r3,u64 r4,u64 r5)1454 static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5)
1455 {
1456 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1457
1458 ri->ifindex = ifindex;
1459 ri->flags = flags;
1460 return TC_ACT_REDIRECT;
1461 }
1462
skb_do_redirect(struct sk_buff * skb)1463 int skb_do_redirect(struct sk_buff *skb)
1464 {
1465 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1466 struct net_device *dev;
1467
1468 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1469 ri->ifindex = 0;
1470 if (unlikely(!dev)) {
1471 kfree_skb(skb);
1472 return -EINVAL;
1473 }
1474
1475 if (BPF_IS_REDIRECT_INGRESS(ri->flags))
1476 return dev_forward_skb(dev, skb);
1477
1478 skb->dev = dev;
1479 skb_sender_cpu_clear(skb);
1480 return dev_queue_xmit(skb);
1481 }
1482
1483 const struct bpf_func_proto bpf_redirect_proto = {
1484 .func = bpf_redirect,
1485 .gpl_only = false,
1486 .ret_type = RET_INTEGER,
1487 .arg1_type = ARG_ANYTHING,
1488 .arg2_type = ARG_ANYTHING,
1489 };
1490
bpf_get_cgroup_classid(u64 r1,u64 r2,u64 r3,u64 r4,u64 r5)1491 static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1492 {
1493 return task_get_classid((struct sk_buff *) (unsigned long) r1);
1494 }
1495
1496 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1497 .func = bpf_get_cgroup_classid,
1498 .gpl_only = false,
1499 .ret_type = RET_INTEGER,
1500 .arg1_type = ARG_PTR_TO_CTX,
1501 };
1502
bpf_get_route_realm(u64 r1,u64 r2,u64 r3,u64 r4,u64 r5)1503 static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1504 {
1505 #ifdef CONFIG_IP_ROUTE_CLASSID
1506 const struct dst_entry *dst;
1507
1508 dst = skb_dst((struct sk_buff *) (unsigned long) r1);
1509 if (dst)
1510 return dst->tclassid;
1511 #endif
1512 return 0;
1513 }
1514
1515 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1516 .func = bpf_get_route_realm,
1517 .gpl_only = false,
1518 .ret_type = RET_INTEGER,
1519 .arg1_type = ARG_PTR_TO_CTX,
1520 };
1521
bpf_skb_vlan_push(u64 r1,u64 r2,u64 vlan_tci,u64 r4,u64 r5)1522 static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
1523 {
1524 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1525 __be16 vlan_proto = (__force __be16) r2;
1526
1527 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1528 vlan_proto != htons(ETH_P_8021AD)))
1529 vlan_proto = htons(ETH_P_8021Q);
1530
1531 return skb_vlan_push(skb, vlan_proto, vlan_tci);
1532 }
1533
1534 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1535 .func = bpf_skb_vlan_push,
1536 .gpl_only = false,
1537 .ret_type = RET_INTEGER,
1538 .arg1_type = ARG_PTR_TO_CTX,
1539 .arg2_type = ARG_ANYTHING,
1540 .arg3_type = ARG_ANYTHING,
1541 };
1542 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1543
bpf_skb_vlan_pop(u64 r1,u64 r2,u64 r3,u64 r4,u64 r5)1544 static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1545 {
1546 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1547
1548 return skb_vlan_pop(skb);
1549 }
1550
1551 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1552 .func = bpf_skb_vlan_pop,
1553 .gpl_only = false,
1554 .ret_type = RET_INTEGER,
1555 .arg1_type = ARG_PTR_TO_CTX,
1556 };
1557 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
1558
bpf_helper_changes_skb_data(void * func)1559 bool bpf_helper_changes_skb_data(void *func)
1560 {
1561 if (func == bpf_skb_vlan_push)
1562 return true;
1563 if (func == bpf_skb_vlan_pop)
1564 return true;
1565 if (func == bpf_skb_store_bytes)
1566 return true;
1567 if (func == bpf_l3_csum_replace)
1568 return true;
1569 if (func == bpf_l4_csum_replace)
1570 return true;
1571
1572 return false;
1573 }
1574
bpf_skb_get_tunnel_key(u64 r1,u64 r2,u64 size,u64 flags,u64 r5)1575 static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1576 {
1577 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1578 struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2;
1579 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1580
1581 if (unlikely(size != sizeof(struct bpf_tunnel_key) || flags || !info))
1582 return -EINVAL;
1583 if (ip_tunnel_info_af(info) != AF_INET)
1584 return -EINVAL;
1585
1586 to->tunnel_id = be64_to_cpu(info->key.tun_id);
1587 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
1588
1589 return 0;
1590 }
1591
1592 const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
1593 .func = bpf_skb_get_tunnel_key,
1594 .gpl_only = false,
1595 .ret_type = RET_INTEGER,
1596 .arg1_type = ARG_PTR_TO_CTX,
1597 .arg2_type = ARG_PTR_TO_STACK,
1598 .arg3_type = ARG_CONST_STACK_SIZE,
1599 .arg4_type = ARG_ANYTHING,
1600 };
1601
1602 static struct metadata_dst __percpu *md_dst;
1603
bpf_skb_set_tunnel_key(u64 r1,u64 r2,u64 size,u64 flags,u64 r5)1604 static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1605 {
1606 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1607 struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2;
1608 struct metadata_dst *md = this_cpu_ptr(md_dst);
1609 struct ip_tunnel_info *info;
1610
1611 if (unlikely(size != sizeof(struct bpf_tunnel_key) || flags))
1612 return -EINVAL;
1613
1614 skb_dst_drop(skb);
1615 dst_hold((struct dst_entry *) md);
1616 skb_dst_set(skb, (struct dst_entry *) md);
1617
1618 info = &md->u.tun_info;
1619 info->mode = IP_TUNNEL_INFO_TX;
1620 info->key.tun_flags = TUNNEL_KEY;
1621 info->key.tun_id = cpu_to_be64(from->tunnel_id);
1622 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
1623
1624 return 0;
1625 }
1626
1627 const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
1628 .func = bpf_skb_set_tunnel_key,
1629 .gpl_only = false,
1630 .ret_type = RET_INTEGER,
1631 .arg1_type = ARG_PTR_TO_CTX,
1632 .arg2_type = ARG_PTR_TO_STACK,
1633 .arg3_type = ARG_CONST_STACK_SIZE,
1634 .arg4_type = ARG_ANYTHING,
1635 };
1636
bpf_get_skb_set_tunnel_key_proto(void)1637 static const struct bpf_func_proto *bpf_get_skb_set_tunnel_key_proto(void)
1638 {
1639 if (!md_dst) {
1640 /* race is not possible, since it's called from
1641 * verifier that is holding verifier mutex
1642 */
1643 md_dst = metadata_dst_alloc_percpu(0, GFP_KERNEL);
1644 if (!md_dst)
1645 return NULL;
1646 }
1647 return &bpf_skb_set_tunnel_key_proto;
1648 }
1649
1650 static const struct bpf_func_proto *
sk_filter_func_proto(enum bpf_func_id func_id)1651 sk_filter_func_proto(enum bpf_func_id func_id)
1652 {
1653 switch (func_id) {
1654 case BPF_FUNC_map_lookup_elem:
1655 return &bpf_map_lookup_elem_proto;
1656 case BPF_FUNC_map_update_elem:
1657 return &bpf_map_update_elem_proto;
1658 case BPF_FUNC_map_delete_elem:
1659 return &bpf_map_delete_elem_proto;
1660 case BPF_FUNC_get_prandom_u32:
1661 return &bpf_get_prandom_u32_proto;
1662 case BPF_FUNC_get_smp_processor_id:
1663 return &bpf_get_smp_processor_id_proto;
1664 case BPF_FUNC_tail_call:
1665 return &bpf_tail_call_proto;
1666 case BPF_FUNC_ktime_get_ns:
1667 return &bpf_ktime_get_ns_proto;
1668 case BPF_FUNC_trace_printk:
1669 if (capable(CAP_SYS_ADMIN))
1670 return bpf_get_trace_printk_proto();
1671 default:
1672 return NULL;
1673 }
1674 }
1675
1676 static const struct bpf_func_proto *
tc_cls_act_func_proto(enum bpf_func_id func_id)1677 tc_cls_act_func_proto(enum bpf_func_id func_id)
1678 {
1679 switch (func_id) {
1680 case BPF_FUNC_skb_store_bytes:
1681 return &bpf_skb_store_bytes_proto;
1682 case BPF_FUNC_l3_csum_replace:
1683 return &bpf_l3_csum_replace_proto;
1684 case BPF_FUNC_l4_csum_replace:
1685 return &bpf_l4_csum_replace_proto;
1686 case BPF_FUNC_clone_redirect:
1687 return &bpf_clone_redirect_proto;
1688 case BPF_FUNC_get_cgroup_classid:
1689 return &bpf_get_cgroup_classid_proto;
1690 case BPF_FUNC_skb_vlan_push:
1691 return &bpf_skb_vlan_push_proto;
1692 case BPF_FUNC_skb_vlan_pop:
1693 return &bpf_skb_vlan_pop_proto;
1694 case BPF_FUNC_skb_get_tunnel_key:
1695 return &bpf_skb_get_tunnel_key_proto;
1696 case BPF_FUNC_skb_set_tunnel_key:
1697 return bpf_get_skb_set_tunnel_key_proto();
1698 case BPF_FUNC_redirect:
1699 return &bpf_redirect_proto;
1700 case BPF_FUNC_get_route_realm:
1701 return &bpf_get_route_realm_proto;
1702 default:
1703 return sk_filter_func_proto(func_id);
1704 }
1705 }
1706
__is_valid_access(int off,int size,enum bpf_access_type type)1707 static bool __is_valid_access(int off, int size, enum bpf_access_type type)
1708 {
1709 /* check bounds */
1710 if (off < 0 || off >= sizeof(struct __sk_buff))
1711 return false;
1712
1713 /* disallow misaligned access */
1714 if (off % size != 0)
1715 return false;
1716
1717 /* all __sk_buff fields are __u32 */
1718 if (size != 4)
1719 return false;
1720
1721 return true;
1722 }
1723
sk_filter_is_valid_access(int off,int size,enum bpf_access_type type)1724 static bool sk_filter_is_valid_access(int off, int size,
1725 enum bpf_access_type type)
1726 {
1727 if (off == offsetof(struct __sk_buff, tc_classid))
1728 return false;
1729
1730 if (type == BPF_WRITE) {
1731 switch (off) {
1732 case offsetof(struct __sk_buff, cb[0]) ...
1733 offsetof(struct __sk_buff, cb[4]):
1734 break;
1735 default:
1736 return false;
1737 }
1738 }
1739
1740 return __is_valid_access(off, size, type);
1741 }
1742
tc_cls_act_is_valid_access(int off,int size,enum bpf_access_type type)1743 static bool tc_cls_act_is_valid_access(int off, int size,
1744 enum bpf_access_type type)
1745 {
1746 if (off == offsetof(struct __sk_buff, tc_classid))
1747 return type == BPF_WRITE ? true : false;
1748
1749 if (type == BPF_WRITE) {
1750 switch (off) {
1751 case offsetof(struct __sk_buff, mark):
1752 case offsetof(struct __sk_buff, tc_index):
1753 case offsetof(struct __sk_buff, priority):
1754 case offsetof(struct __sk_buff, cb[0]) ...
1755 offsetof(struct __sk_buff, cb[4]):
1756 break;
1757 default:
1758 return false;
1759 }
1760 }
1761 return __is_valid_access(off, size, type);
1762 }
1763
bpf_net_convert_ctx_access(enum bpf_access_type type,int dst_reg,int src_reg,int ctx_off,struct bpf_insn * insn_buf,struct bpf_prog * prog)1764 static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
1765 int src_reg, int ctx_off,
1766 struct bpf_insn *insn_buf,
1767 struct bpf_prog *prog)
1768 {
1769 struct bpf_insn *insn = insn_buf;
1770
1771 switch (ctx_off) {
1772 case offsetof(struct __sk_buff, len):
1773 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
1774
1775 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1776 offsetof(struct sk_buff, len));
1777 break;
1778
1779 case offsetof(struct __sk_buff, protocol):
1780 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1781
1782 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1783 offsetof(struct sk_buff, protocol));
1784 break;
1785
1786 case offsetof(struct __sk_buff, vlan_proto):
1787 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
1788
1789 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1790 offsetof(struct sk_buff, vlan_proto));
1791 break;
1792
1793 case offsetof(struct __sk_buff, priority):
1794 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
1795
1796 if (type == BPF_WRITE)
1797 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
1798 offsetof(struct sk_buff, priority));
1799 else
1800 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1801 offsetof(struct sk_buff, priority));
1802 break;
1803
1804 case offsetof(struct __sk_buff, ingress_ifindex):
1805 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
1806
1807 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1808 offsetof(struct sk_buff, skb_iif));
1809 break;
1810
1811 case offsetof(struct __sk_buff, ifindex):
1812 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
1813
1814 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
1815 dst_reg, src_reg,
1816 offsetof(struct sk_buff, dev));
1817 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
1818 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
1819 offsetof(struct net_device, ifindex));
1820 break;
1821
1822 case offsetof(struct __sk_buff, hash):
1823 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1824
1825 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1826 offsetof(struct sk_buff, hash));
1827 break;
1828
1829 case offsetof(struct __sk_buff, mark):
1830 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
1831
1832 if (type == BPF_WRITE)
1833 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
1834 offsetof(struct sk_buff, mark));
1835 else
1836 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1837 offsetof(struct sk_buff, mark));
1838 break;
1839
1840 case offsetof(struct __sk_buff, pkt_type):
1841 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
1842
1843 case offsetof(struct __sk_buff, queue_mapping):
1844 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
1845
1846 case offsetof(struct __sk_buff, vlan_present):
1847 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
1848 dst_reg, src_reg, insn);
1849
1850 case offsetof(struct __sk_buff, vlan_tci):
1851 return convert_skb_access(SKF_AD_VLAN_TAG,
1852 dst_reg, src_reg, insn);
1853
1854 case offsetof(struct __sk_buff, cb[0]) ...
1855 offsetof(struct __sk_buff, cb[4]):
1856 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
1857
1858 prog->cb_access = 1;
1859 ctx_off -= offsetof(struct __sk_buff, cb[0]);
1860 ctx_off += offsetof(struct sk_buff, cb);
1861 ctx_off += offsetof(struct qdisc_skb_cb, data);
1862 if (type == BPF_WRITE)
1863 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
1864 else
1865 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
1866 break;
1867
1868 case offsetof(struct __sk_buff, tc_classid):
1869 ctx_off -= offsetof(struct __sk_buff, tc_classid);
1870 ctx_off += offsetof(struct sk_buff, cb);
1871 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
1872 WARN_ON(type != BPF_WRITE);
1873 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
1874 break;
1875
1876 case offsetof(struct __sk_buff, tc_index):
1877 #ifdef CONFIG_NET_SCHED
1878 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
1879
1880 if (type == BPF_WRITE)
1881 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
1882 offsetof(struct sk_buff, tc_index));
1883 else
1884 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1885 offsetof(struct sk_buff, tc_index));
1886 break;
1887 #else
1888 if (type == BPF_WRITE)
1889 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
1890 else
1891 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
1892 break;
1893 #endif
1894 }
1895
1896 return insn - insn_buf;
1897 }
1898
1899 static const struct bpf_verifier_ops sk_filter_ops = {
1900 .get_func_proto = sk_filter_func_proto,
1901 .is_valid_access = sk_filter_is_valid_access,
1902 .convert_ctx_access = bpf_net_convert_ctx_access,
1903 };
1904
1905 static const struct bpf_verifier_ops tc_cls_act_ops = {
1906 .get_func_proto = tc_cls_act_func_proto,
1907 .is_valid_access = tc_cls_act_is_valid_access,
1908 .convert_ctx_access = bpf_net_convert_ctx_access,
1909 };
1910
1911 static struct bpf_prog_type_list sk_filter_type __read_mostly = {
1912 .ops = &sk_filter_ops,
1913 .type = BPF_PROG_TYPE_SOCKET_FILTER,
1914 };
1915
1916 static struct bpf_prog_type_list sched_cls_type __read_mostly = {
1917 .ops = &tc_cls_act_ops,
1918 .type = BPF_PROG_TYPE_SCHED_CLS,
1919 };
1920
1921 static struct bpf_prog_type_list sched_act_type __read_mostly = {
1922 .ops = &tc_cls_act_ops,
1923 .type = BPF_PROG_TYPE_SCHED_ACT,
1924 };
1925
register_sk_filter_ops(void)1926 static int __init register_sk_filter_ops(void)
1927 {
1928 bpf_register_prog_type(&sk_filter_type);
1929 bpf_register_prog_type(&sched_cls_type);
1930 bpf_register_prog_type(&sched_act_type);
1931
1932 return 0;
1933 }
1934 late_initcall(register_sk_filter_ops);
1935
__sk_detach_filter(struct sock * sk,bool locked)1936 int __sk_detach_filter(struct sock *sk, bool locked)
1937 {
1938 int ret = -ENOENT;
1939 struct sk_filter *filter;
1940
1941 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1942 return -EPERM;
1943
1944 filter = rcu_dereference_protected(sk->sk_filter, locked);
1945 if (filter) {
1946 RCU_INIT_POINTER(sk->sk_filter, NULL);
1947 sk_filter_uncharge(sk, filter);
1948 ret = 0;
1949 }
1950
1951 return ret;
1952 }
1953 EXPORT_SYMBOL_GPL(__sk_detach_filter);
1954
sk_detach_filter(struct sock * sk)1955 int sk_detach_filter(struct sock *sk)
1956 {
1957 return __sk_detach_filter(sk, sock_owned_by_user(sk));
1958 }
1959
sk_get_filter(struct sock * sk,struct sock_filter __user * ubuf,unsigned int len)1960 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
1961 unsigned int len)
1962 {
1963 struct sock_fprog_kern *fprog;
1964 struct sk_filter *filter;
1965 int ret = 0;
1966
1967 lock_sock(sk);
1968 filter = rcu_dereference_protected(sk->sk_filter,
1969 sock_owned_by_user(sk));
1970 if (!filter)
1971 goto out;
1972
1973 /* We're copying the filter that has been originally attached,
1974 * so no conversion/decode needed anymore. eBPF programs that
1975 * have no original program cannot be dumped through this.
1976 */
1977 ret = -EACCES;
1978 fprog = filter->prog->orig_prog;
1979 if (!fprog)
1980 goto out;
1981
1982 ret = fprog->len;
1983 if (!len)
1984 /* User space only enquires number of filter blocks. */
1985 goto out;
1986
1987 ret = -EINVAL;
1988 if (len < fprog->len)
1989 goto out;
1990
1991 ret = -EFAULT;
1992 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
1993 goto out;
1994
1995 /* Instead of bytes, the API requests to return the number
1996 * of filter blocks.
1997 */
1998 ret = fprog->len;
1999 out:
2000 release_sock(sk);
2001 return ret;
2002 }
2003