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