]>
Commit | Line | Data |
---|---|---|
99c55f7d AS |
1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
2 | * | |
3 | * This program is free software; you can redistribute it and/or | |
4 | * modify it under the terms of version 2 of the GNU General Public | |
5 | * License as published by the Free Software Foundation. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, but | |
8 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
10 | * General Public License for more details. | |
11 | */ | |
12 | #include <linux/bpf.h> | |
a67edbf4 | 13 | #include <linux/bpf_trace.h> |
99c55f7d AS |
14 | #include <linux/syscalls.h> |
15 | #include <linux/slab.h> | |
3f07c014 | 16 | #include <linux/sched/signal.h> |
d407bd25 DB |
17 | #include <linux/vmalloc.h> |
18 | #include <linux/mmzone.h> | |
99c55f7d | 19 | #include <linux/anon_inodes.h> |
db20fd2b | 20 | #include <linux/file.h> |
09756af4 AS |
21 | #include <linux/license.h> |
22 | #include <linux/filter.h> | |
2541517c | 23 | #include <linux/version.h> |
535e7b4b | 24 | #include <linux/kernel.h> |
dc4bb0e2 | 25 | #include <linux/idr.h> |
cb4d2b3f MKL |
26 | #include <linux/cred.h> |
27 | #include <linux/timekeeping.h> | |
28 | #include <linux/ctype.h> | |
38c61037 | 29 | #include <linux/nospec.h> |
99c55f7d | 30 | |
14dc6f04 MKL |
31 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
32 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ | |
33 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ | |
34 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) | |
35 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) | |
36 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) | |
37 | ||
6e71b04a CF |
38 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
39 | ||
b121d1e7 | 40 | DEFINE_PER_CPU(int, bpf_prog_active); |
dc4bb0e2 MKL |
41 | static DEFINE_IDR(prog_idr); |
42 | static DEFINE_SPINLOCK(prog_idr_lock); | |
f3f1c054 MKL |
43 | static DEFINE_IDR(map_idr); |
44 | static DEFINE_SPINLOCK(map_idr_lock); | |
b121d1e7 | 45 | |
1be7f75d AS |
46 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
47 | ||
40077e0c JB |
48 | static const struct bpf_map_ops * const bpf_map_types[] = { |
49 | #define BPF_PROG_TYPE(_id, _ops) | |
50 | #define BPF_MAP_TYPE(_id, _ops) \ | |
51 | [_id] = &_ops, | |
52 | #include <linux/bpf_types.h> | |
53 | #undef BPF_PROG_TYPE | |
54 | #undef BPF_MAP_TYPE | |
55 | }; | |
99c55f7d | 56 | |
752ba56f MS |
57 | /* |
58 | * If we're handed a bigger struct than we know of, ensure all the unknown bits | |
59 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions | |
60 | * we don't know about yet. | |
61 | * | |
62 | * There is a ToCToU between this function call and the following | |
63 | * copy_from_user() call. However, this is not a concern since this function is | |
64 | * meant to be a future-proofing of bits. | |
65 | */ | |
58291a74 MS |
66 | static int check_uarg_tail_zero(void __user *uaddr, |
67 | size_t expected_size, | |
68 | size_t actual_size) | |
69 | { | |
70 | unsigned char __user *addr; | |
71 | unsigned char __user *end; | |
72 | unsigned char val; | |
73 | int err; | |
74 | ||
752ba56f MS |
75 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
76 | return -E2BIG; | |
77 | ||
78 | if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size))) | |
79 | return -EFAULT; | |
80 | ||
58291a74 MS |
81 | if (actual_size <= expected_size) |
82 | return 0; | |
83 | ||
84 | addr = uaddr + expected_size; | |
85 | end = uaddr + actual_size; | |
86 | ||
87 | for (; addr < end; addr++) { | |
88 | err = get_user(val, addr); | |
89 | if (err) | |
90 | return err; | |
91 | if (val) | |
92 | return -E2BIG; | |
93 | } | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
99c55f7d AS |
98 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
99 | { | |
144bc489 | 100 | const struct bpf_map_ops *ops; |
38c61037 | 101 | u32 type = attr->map_type; |
99c55f7d | 102 | struct bpf_map *map; |
144bc489 | 103 | int err; |
99c55f7d | 104 | |
38c61037 | 105 | if (type >= ARRAY_SIZE(bpf_map_types)) |
144bc489 | 106 | return ERR_PTR(-EINVAL); |
38c61037 MR |
107 | type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); |
108 | ops = bpf_map_types[type]; | |
144bc489 | 109 | if (!ops) |
40077e0c | 110 | return ERR_PTR(-EINVAL); |
99c55f7d | 111 | |
144bc489 JK |
112 | if (ops->map_alloc_check) { |
113 | err = ops->map_alloc_check(attr); | |
114 | if (err) | |
115 | return ERR_PTR(err); | |
116 | } | |
117 | map = ops->map_alloc(attr); | |
40077e0c JB |
118 | if (IS_ERR(map)) |
119 | return map; | |
144bc489 | 120 | map->ops = ops; |
38c61037 | 121 | map->map_type = type; |
40077e0c | 122 | return map; |
99c55f7d AS |
123 | } |
124 | ||
96eabe7a | 125 | void *bpf_map_area_alloc(size_t size, int numa_node) |
d407bd25 DB |
126 | { |
127 | /* We definitely need __GFP_NORETRY, so OOM killer doesn't | |
128 | * trigger under memory pressure as we really just want to | |
129 | * fail instead. | |
130 | */ | |
131 | const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; | |
132 | void *area; | |
133 | ||
134 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { | |
96eabe7a | 135 | area = kmalloc_node(size, GFP_USER | flags, numa_node); |
d407bd25 DB |
136 | if (area != NULL) |
137 | return area; | |
138 | } | |
139 | ||
96eabe7a MKL |
140 | return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags, |
141 | __builtin_return_address(0)); | |
d407bd25 DB |
142 | } |
143 | ||
144 | void bpf_map_area_free(void *area) | |
145 | { | |
146 | kvfree(area); | |
147 | } | |
148 | ||
6c905981 AS |
149 | int bpf_map_precharge_memlock(u32 pages) |
150 | { | |
151 | struct user_struct *user = get_current_user(); | |
152 | unsigned long memlock_limit, cur; | |
153 | ||
154 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
155 | cur = atomic_long_read(&user->locked_vm); | |
156 | free_uid(user); | |
157 | if (cur + pages > memlock_limit) | |
158 | return -EPERM; | |
159 | return 0; | |
160 | } | |
161 | ||
aaac3ba9 AS |
162 | static int bpf_map_charge_memlock(struct bpf_map *map) |
163 | { | |
164 | struct user_struct *user = get_current_user(); | |
165 | unsigned long memlock_limit; | |
166 | ||
167 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
168 | ||
169 | atomic_long_add(map->pages, &user->locked_vm); | |
170 | ||
171 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { | |
172 | atomic_long_sub(map->pages, &user->locked_vm); | |
173 | free_uid(user); | |
174 | return -EPERM; | |
175 | } | |
176 | map->user = user; | |
177 | return 0; | |
178 | } | |
179 | ||
180 | static void bpf_map_uncharge_memlock(struct bpf_map *map) | |
181 | { | |
182 | struct user_struct *user = map->user; | |
183 | ||
184 | atomic_long_sub(map->pages, &user->locked_vm); | |
185 | free_uid(user); | |
186 | } | |
187 | ||
f3f1c054 MKL |
188 | static int bpf_map_alloc_id(struct bpf_map *map) |
189 | { | |
190 | int id; | |
191 | ||
192 | spin_lock_bh(&map_idr_lock); | |
193 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); | |
194 | if (id > 0) | |
195 | map->id = id; | |
196 | spin_unlock_bh(&map_idr_lock); | |
197 | ||
198 | if (WARN_ON_ONCE(!id)) | |
199 | return -ENOSPC; | |
200 | ||
201 | return id > 0 ? 0 : id; | |
202 | } | |
203 | ||
bd5f5f4e | 204 | static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) |
f3f1c054 | 205 | { |
930651a7 ED |
206 | unsigned long flags; |
207 | ||
bd5f5f4e | 208 | if (do_idr_lock) |
930651a7 | 209 | spin_lock_irqsave(&map_idr_lock, flags); |
bd5f5f4e MKL |
210 | else |
211 | __acquire(&map_idr_lock); | |
212 | ||
f3f1c054 | 213 | idr_remove(&map_idr, map->id); |
bd5f5f4e MKL |
214 | |
215 | if (do_idr_lock) | |
930651a7 | 216 | spin_unlock_irqrestore(&map_idr_lock, flags); |
bd5f5f4e MKL |
217 | else |
218 | __release(&map_idr_lock); | |
f3f1c054 MKL |
219 | } |
220 | ||
99c55f7d AS |
221 | /* called from workqueue */ |
222 | static void bpf_map_free_deferred(struct work_struct *work) | |
223 | { | |
224 | struct bpf_map *map = container_of(work, struct bpf_map, work); | |
225 | ||
aaac3ba9 | 226 | bpf_map_uncharge_memlock(map); |
afdb09c7 | 227 | security_bpf_map_free(map); |
99c55f7d AS |
228 | /* implementation dependent freeing */ |
229 | map->ops->map_free(map); | |
230 | } | |
231 | ||
c9da161c DB |
232 | static void bpf_map_put_uref(struct bpf_map *map) |
233 | { | |
234 | if (atomic_dec_and_test(&map->usercnt)) { | |
235 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) | |
236 | bpf_fd_array_map_clear(map); | |
237 | } | |
238 | } | |
239 | ||
99c55f7d AS |
240 | /* decrement map refcnt and schedule it for freeing via workqueue |
241 | * (unrelying map implementation ops->map_free() might sleep) | |
242 | */ | |
bd5f5f4e | 243 | static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) |
99c55f7d AS |
244 | { |
245 | if (atomic_dec_and_test(&map->refcnt)) { | |
34ad5580 | 246 | /* bpf_map_free_id() must be called first */ |
bd5f5f4e | 247 | bpf_map_free_id(map, do_idr_lock); |
99c55f7d AS |
248 | INIT_WORK(&map->work, bpf_map_free_deferred); |
249 | schedule_work(&map->work); | |
250 | } | |
251 | } | |
252 | ||
bd5f5f4e MKL |
253 | void bpf_map_put(struct bpf_map *map) |
254 | { | |
255 | __bpf_map_put(map, true); | |
256 | } | |
257 | ||
c9da161c | 258 | void bpf_map_put_with_uref(struct bpf_map *map) |
99c55f7d | 259 | { |
c9da161c | 260 | bpf_map_put_uref(map); |
99c55f7d | 261 | bpf_map_put(map); |
c9da161c DB |
262 | } |
263 | ||
264 | static int bpf_map_release(struct inode *inode, struct file *filp) | |
265 | { | |
61d1b6a4 DB |
266 | struct bpf_map *map = filp->private_data; |
267 | ||
268 | if (map->ops->map_release) | |
269 | map->ops->map_release(map, filp); | |
270 | ||
271 | bpf_map_put_with_uref(map); | |
99c55f7d AS |
272 | return 0; |
273 | } | |
274 | ||
f99bf205 DB |
275 | #ifdef CONFIG_PROC_FS |
276 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) | |
277 | { | |
278 | const struct bpf_map *map = filp->private_data; | |
21116b70 DB |
279 | const struct bpf_array *array; |
280 | u32 owner_prog_type = 0; | |
9780c0ab | 281 | u32 owner_jited = 0; |
21116b70 DB |
282 | |
283 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { | |
284 | array = container_of(map, struct bpf_array, map); | |
285 | owner_prog_type = array->owner_prog_type; | |
9780c0ab | 286 | owner_jited = array->owner_jited; |
21116b70 | 287 | } |
f99bf205 DB |
288 | |
289 | seq_printf(m, | |
290 | "map_type:\t%u\n" | |
291 | "key_size:\t%u\n" | |
292 | "value_size:\t%u\n" | |
322cea2f | 293 | "max_entries:\t%u\n" |
21116b70 DB |
294 | "map_flags:\t%#x\n" |
295 | "memlock:\t%llu\n", | |
f99bf205 DB |
296 | map->map_type, |
297 | map->key_size, | |
298 | map->value_size, | |
322cea2f | 299 | map->max_entries, |
21116b70 DB |
300 | map->map_flags, |
301 | map->pages * 1ULL << PAGE_SHIFT); | |
302 | ||
9780c0ab | 303 | if (owner_prog_type) { |
21116b70 DB |
304 | seq_printf(m, "owner_prog_type:\t%u\n", |
305 | owner_prog_type); | |
9780c0ab DB |
306 | seq_printf(m, "owner_jited:\t%u\n", |
307 | owner_jited); | |
308 | } | |
f99bf205 DB |
309 | } |
310 | #endif | |
311 | ||
6e71b04a CF |
312 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
313 | loff_t *ppos) | |
314 | { | |
315 | /* We need this handler such that alloc_file() enables | |
316 | * f_mode with FMODE_CAN_READ. | |
317 | */ | |
318 | return -EINVAL; | |
319 | } | |
320 | ||
321 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, | |
322 | size_t siz, loff_t *ppos) | |
323 | { | |
324 | /* We need this handler such that alloc_file() enables | |
325 | * f_mode with FMODE_CAN_WRITE. | |
326 | */ | |
327 | return -EINVAL; | |
328 | } | |
329 | ||
f66e448c | 330 | const struct file_operations bpf_map_fops = { |
f99bf205 DB |
331 | #ifdef CONFIG_PROC_FS |
332 | .show_fdinfo = bpf_map_show_fdinfo, | |
333 | #endif | |
334 | .release = bpf_map_release, | |
6e71b04a CF |
335 | .read = bpf_dummy_read, |
336 | .write = bpf_dummy_write, | |
99c55f7d AS |
337 | }; |
338 | ||
6e71b04a | 339 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
aa79781b | 340 | { |
afdb09c7 CF |
341 | int ret; |
342 | ||
343 | ret = security_bpf_map(map, OPEN_FMODE(flags)); | |
344 | if (ret < 0) | |
345 | return ret; | |
346 | ||
aa79781b | 347 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
6e71b04a CF |
348 | flags | O_CLOEXEC); |
349 | } | |
350 | ||
351 | int bpf_get_file_flag(int flags) | |
352 | { | |
353 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) | |
354 | return -EINVAL; | |
355 | if (flags & BPF_F_RDONLY) | |
356 | return O_RDONLY; | |
357 | if (flags & BPF_F_WRONLY) | |
358 | return O_WRONLY; | |
359 | return O_RDWR; | |
aa79781b DB |
360 | } |
361 | ||
99c55f7d AS |
362 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
363 | #define CHECK_ATTR(CMD) \ | |
364 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ | |
365 | sizeof(attr->CMD##_LAST_FIELD), 0, \ | |
366 | sizeof(*attr) - \ | |
367 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ | |
368 | sizeof(attr->CMD##_LAST_FIELD)) != NULL | |
369 | ||
cb4d2b3f MKL |
370 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
371 | * Return 0 on success and < 0 on error. | |
372 | */ | |
373 | static int bpf_obj_name_cpy(char *dst, const char *src) | |
374 | { | |
375 | const char *end = src + BPF_OBJ_NAME_LEN; | |
376 | ||
473d9734 MKL |
377 | memset(dst, 0, BPF_OBJ_NAME_LEN); |
378 | ||
cb4d2b3f MKL |
379 | /* Copy all isalnum() and '_' char */ |
380 | while (src < end && *src) { | |
381 | if (!isalnum(*src) && *src != '_') | |
382 | return -EINVAL; | |
383 | *dst++ = *src++; | |
384 | } | |
385 | ||
386 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ | |
387 | if (src == end) | |
388 | return -EINVAL; | |
389 | ||
cb4d2b3f MKL |
390 | return 0; |
391 | } | |
392 | ||
ad5b177b | 393 | #define BPF_MAP_CREATE_LAST_FIELD map_name |
99c55f7d AS |
394 | /* called via syscall */ |
395 | static int map_create(union bpf_attr *attr) | |
396 | { | |
96eabe7a | 397 | int numa_node = bpf_map_attr_numa_node(attr); |
99c55f7d | 398 | struct bpf_map *map; |
6e71b04a | 399 | int f_flags; |
99c55f7d AS |
400 | int err; |
401 | ||
402 | err = CHECK_ATTR(BPF_MAP_CREATE); | |
403 | if (err) | |
404 | return -EINVAL; | |
405 | ||
6e71b04a CF |
406 | f_flags = bpf_get_file_flag(attr->map_flags); |
407 | if (f_flags < 0) | |
408 | return f_flags; | |
409 | ||
96eabe7a | 410 | if (numa_node != NUMA_NO_NODE && |
96e5ae4e ED |
411 | ((unsigned int)numa_node >= nr_node_ids || |
412 | !node_online(numa_node))) | |
96eabe7a MKL |
413 | return -EINVAL; |
414 | ||
99c55f7d AS |
415 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
416 | map = find_and_alloc_map(attr); | |
417 | if (IS_ERR(map)) | |
418 | return PTR_ERR(map); | |
419 | ||
ad5b177b MKL |
420 | err = bpf_obj_name_cpy(map->name, attr->map_name); |
421 | if (err) | |
422 | goto free_map_nouncharge; | |
423 | ||
99c55f7d | 424 | atomic_set(&map->refcnt, 1); |
c9da161c | 425 | atomic_set(&map->usercnt, 1); |
99c55f7d | 426 | |
afdb09c7 | 427 | err = security_bpf_map_alloc(map); |
aaac3ba9 | 428 | if (err) |
20b2b24f | 429 | goto free_map_nouncharge; |
aaac3ba9 | 430 | |
afdb09c7 CF |
431 | err = bpf_map_charge_memlock(map); |
432 | if (err) | |
433 | goto free_map_sec; | |
434 | ||
f3f1c054 MKL |
435 | err = bpf_map_alloc_id(map); |
436 | if (err) | |
437 | goto free_map; | |
438 | ||
6e71b04a | 439 | err = bpf_map_new_fd(map, f_flags); |
bd5f5f4e MKL |
440 | if (err < 0) { |
441 | /* failed to allocate fd. | |
442 | * bpf_map_put() is needed because the above | |
443 | * bpf_map_alloc_id() has published the map | |
444 | * to the userspace and the userspace may | |
445 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. | |
446 | */ | |
447 | bpf_map_put(map); | |
448 | return err; | |
449 | } | |
99c55f7d | 450 | |
a67edbf4 | 451 | trace_bpf_map_create(map, err); |
99c55f7d AS |
452 | return err; |
453 | ||
454 | free_map: | |
20b2b24f | 455 | bpf_map_uncharge_memlock(map); |
afdb09c7 CF |
456 | free_map_sec: |
457 | security_bpf_map_free(map); | |
20b2b24f | 458 | free_map_nouncharge: |
99c55f7d AS |
459 | map->ops->map_free(map); |
460 | return err; | |
461 | } | |
462 | ||
db20fd2b AS |
463 | /* if error is returned, fd is released. |
464 | * On success caller should complete fd access with matching fdput() | |
465 | */ | |
c2101297 | 466 | struct bpf_map *__bpf_map_get(struct fd f) |
db20fd2b | 467 | { |
db20fd2b AS |
468 | if (!f.file) |
469 | return ERR_PTR(-EBADF); | |
db20fd2b AS |
470 | if (f.file->f_op != &bpf_map_fops) { |
471 | fdput(f); | |
472 | return ERR_PTR(-EINVAL); | |
473 | } | |
474 | ||
c2101297 DB |
475 | return f.file->private_data; |
476 | } | |
477 | ||
92117d84 AS |
478 | /* prog's and map's refcnt limit */ |
479 | #define BPF_MAX_REFCNT 32768 | |
480 | ||
481 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) | |
c9da161c | 482 | { |
92117d84 AS |
483 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
484 | atomic_dec(&map->refcnt); | |
485 | return ERR_PTR(-EBUSY); | |
486 | } | |
c9da161c DB |
487 | if (uref) |
488 | atomic_inc(&map->usercnt); | |
92117d84 | 489 | return map; |
c9da161c DB |
490 | } |
491 | ||
492 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) | |
c2101297 DB |
493 | { |
494 | struct fd f = fdget(ufd); | |
495 | struct bpf_map *map; | |
496 | ||
497 | map = __bpf_map_get(f); | |
498 | if (IS_ERR(map)) | |
499 | return map; | |
500 | ||
92117d84 | 501 | map = bpf_map_inc(map, true); |
c2101297 | 502 | fdput(f); |
db20fd2b AS |
503 | |
504 | return map; | |
505 | } | |
506 | ||
bd5f5f4e MKL |
507 | /* map_idr_lock should have been held */ |
508 | static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, | |
509 | bool uref) | |
510 | { | |
511 | int refold; | |
512 | ||
513 | refold = __atomic_add_unless(&map->refcnt, 1, 0); | |
514 | ||
515 | if (refold >= BPF_MAX_REFCNT) { | |
516 | __bpf_map_put(map, false); | |
517 | return ERR_PTR(-EBUSY); | |
518 | } | |
519 | ||
520 | if (!refold) | |
521 | return ERR_PTR(-ENOENT); | |
522 | ||
523 | if (uref) | |
524 | atomic_inc(&map->usercnt); | |
525 | ||
526 | return map; | |
527 | } | |
528 | ||
b8cdc051 AS |
529 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
530 | { | |
531 | return -ENOTSUPP; | |
532 | } | |
533 | ||
db20fd2b AS |
534 | /* last field in 'union bpf_attr' used by this command */ |
535 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value | |
536 | ||
537 | static int map_lookup_elem(union bpf_attr *attr) | |
538 | { | |
535e7b4b MS |
539 | void __user *ukey = u64_to_user_ptr(attr->key); |
540 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 541 | int ufd = attr->map_fd; |
db20fd2b | 542 | struct bpf_map *map; |
8ebe667c | 543 | void *key, *value, *ptr; |
15a07b33 | 544 | u32 value_size; |
592867bf | 545 | struct fd f; |
db20fd2b AS |
546 | int err; |
547 | ||
548 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) | |
549 | return -EINVAL; | |
550 | ||
592867bf | 551 | f = fdget(ufd); |
c2101297 | 552 | map = __bpf_map_get(f); |
db20fd2b AS |
553 | if (IS_ERR(map)) |
554 | return PTR_ERR(map); | |
555 | ||
6e71b04a CF |
556 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
557 | err = -EPERM; | |
558 | goto err_put; | |
559 | } | |
560 | ||
e4448ed8 AV |
561 | key = memdup_user(ukey, map->key_size); |
562 | if (IS_ERR(key)) { | |
563 | err = PTR_ERR(key); | |
db20fd2b | 564 | goto err_put; |
e4448ed8 | 565 | } |
db20fd2b | 566 | |
15a07b33 | 567 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 568 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
15a07b33 AS |
569 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
570 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); | |
14dc6f04 MKL |
571 | else if (IS_FD_MAP(map)) |
572 | value_size = sizeof(u32); | |
15a07b33 AS |
573 | else |
574 | value_size = map->value_size; | |
575 | ||
8ebe667c | 576 | err = -ENOMEM; |
15a07b33 | 577 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b | 578 | if (!value) |
8ebe667c AS |
579 | goto free_key; |
580 | ||
8f844938 MKL |
581 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
582 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
583 | err = bpf_percpu_hash_copy(map, key, value); |
584 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
585 | err = bpf_percpu_array_copy(map, key, value); | |
557c0c6e AS |
586 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
587 | err = bpf_stackmap_copy(map, key, value); | |
14dc6f04 MKL |
588 | } else if (IS_FD_ARRAY(map)) { |
589 | err = bpf_fd_array_map_lookup_elem(map, key, value); | |
590 | } else if (IS_FD_HASH(map)) { | |
591 | err = bpf_fd_htab_map_lookup_elem(map, key, value); | |
15a07b33 AS |
592 | } else { |
593 | rcu_read_lock(); | |
594 | ptr = map->ops->map_lookup_elem(map, key); | |
595 | if (ptr) | |
596 | memcpy(value, ptr, value_size); | |
597 | rcu_read_unlock(); | |
598 | err = ptr ? 0 : -ENOENT; | |
599 | } | |
8ebe667c | 600 | |
15a07b33 | 601 | if (err) |
8ebe667c | 602 | goto free_value; |
db20fd2b AS |
603 | |
604 | err = -EFAULT; | |
15a07b33 | 605 | if (copy_to_user(uvalue, value, value_size) != 0) |
8ebe667c | 606 | goto free_value; |
db20fd2b | 607 | |
a67edbf4 | 608 | trace_bpf_map_lookup_elem(map, ufd, key, value); |
db20fd2b AS |
609 | err = 0; |
610 | ||
8ebe667c AS |
611 | free_value: |
612 | kfree(value); | |
db20fd2b AS |
613 | free_key: |
614 | kfree(key); | |
615 | err_put: | |
616 | fdput(f); | |
617 | return err; | |
618 | } | |
619 | ||
3274f520 | 620 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
db20fd2b AS |
621 | |
622 | static int map_update_elem(union bpf_attr *attr) | |
623 | { | |
535e7b4b MS |
624 | void __user *ukey = u64_to_user_ptr(attr->key); |
625 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 626 | int ufd = attr->map_fd; |
db20fd2b AS |
627 | struct bpf_map *map; |
628 | void *key, *value; | |
15a07b33 | 629 | u32 value_size; |
592867bf | 630 | struct fd f; |
db20fd2b AS |
631 | int err; |
632 | ||
633 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) | |
634 | return -EINVAL; | |
635 | ||
592867bf | 636 | f = fdget(ufd); |
c2101297 | 637 | map = __bpf_map_get(f); |
db20fd2b AS |
638 | if (IS_ERR(map)) |
639 | return PTR_ERR(map); | |
640 | ||
6e71b04a CF |
641 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
642 | err = -EPERM; | |
643 | goto err_put; | |
644 | } | |
645 | ||
e4448ed8 AV |
646 | key = memdup_user(ukey, map->key_size); |
647 | if (IS_ERR(key)) { | |
648 | err = PTR_ERR(key); | |
db20fd2b | 649 | goto err_put; |
e4448ed8 | 650 | } |
db20fd2b | 651 | |
15a07b33 | 652 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 653 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
15a07b33 AS |
654 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
655 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); | |
656 | else | |
657 | value_size = map->value_size; | |
658 | ||
db20fd2b | 659 | err = -ENOMEM; |
15a07b33 | 660 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b AS |
661 | if (!value) |
662 | goto free_key; | |
663 | ||
664 | err = -EFAULT; | |
15a07b33 | 665 | if (copy_from_user(value, uvalue, value_size) != 0) |
db20fd2b AS |
666 | goto free_value; |
667 | ||
6710e112 JDB |
668 | /* Need to create a kthread, thus must support schedule */ |
669 | if (map->map_type == BPF_MAP_TYPE_CPUMAP) { | |
670 | err = map->ops->map_update_elem(map, key, value, attr->flags); | |
671 | goto out; | |
672 | } | |
673 | ||
b121d1e7 AS |
674 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
675 | * inside bpf map update or delete otherwise deadlocks are possible | |
676 | */ | |
677 | preempt_disable(); | |
678 | __this_cpu_inc(bpf_prog_active); | |
8f844938 MKL |
679 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
680 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
681 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
682 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
683 | err = bpf_percpu_array_update(map, key, value, attr->flags); | |
d056a788 | 684 | } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || |
4ed8ec52 | 685 | map->map_type == BPF_MAP_TYPE_PROG_ARRAY || |
56f668df MKL |
686 | map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || |
687 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) { | |
d056a788 DB |
688 | rcu_read_lock(); |
689 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, | |
690 | attr->flags); | |
691 | rcu_read_unlock(); | |
bcc6b1b7 MKL |
692 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
693 | rcu_read_lock(); | |
694 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, | |
695 | attr->flags); | |
696 | rcu_read_unlock(); | |
15a07b33 AS |
697 | } else { |
698 | rcu_read_lock(); | |
699 | err = map->ops->map_update_elem(map, key, value, attr->flags); | |
700 | rcu_read_unlock(); | |
701 | } | |
b121d1e7 AS |
702 | __this_cpu_dec(bpf_prog_active); |
703 | preempt_enable(); | |
6710e112 | 704 | out: |
a67edbf4 DB |
705 | if (!err) |
706 | trace_bpf_map_update_elem(map, ufd, key, value); | |
db20fd2b AS |
707 | free_value: |
708 | kfree(value); | |
709 | free_key: | |
710 | kfree(key); | |
711 | err_put: | |
712 | fdput(f); | |
713 | return err; | |
714 | } | |
715 | ||
716 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key | |
717 | ||
718 | static int map_delete_elem(union bpf_attr *attr) | |
719 | { | |
535e7b4b | 720 | void __user *ukey = u64_to_user_ptr(attr->key); |
db20fd2b | 721 | int ufd = attr->map_fd; |
db20fd2b | 722 | struct bpf_map *map; |
592867bf | 723 | struct fd f; |
db20fd2b AS |
724 | void *key; |
725 | int err; | |
726 | ||
727 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) | |
728 | return -EINVAL; | |
729 | ||
592867bf | 730 | f = fdget(ufd); |
c2101297 | 731 | map = __bpf_map_get(f); |
db20fd2b AS |
732 | if (IS_ERR(map)) |
733 | return PTR_ERR(map); | |
734 | ||
6e71b04a CF |
735 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
736 | err = -EPERM; | |
737 | goto err_put; | |
738 | } | |
739 | ||
e4448ed8 AV |
740 | key = memdup_user(ukey, map->key_size); |
741 | if (IS_ERR(key)) { | |
742 | err = PTR_ERR(key); | |
db20fd2b | 743 | goto err_put; |
e4448ed8 | 744 | } |
db20fd2b | 745 | |
b121d1e7 AS |
746 | preempt_disable(); |
747 | __this_cpu_inc(bpf_prog_active); | |
db20fd2b AS |
748 | rcu_read_lock(); |
749 | err = map->ops->map_delete_elem(map, key); | |
750 | rcu_read_unlock(); | |
b121d1e7 AS |
751 | __this_cpu_dec(bpf_prog_active); |
752 | preempt_enable(); | |
db20fd2b | 753 | |
a67edbf4 DB |
754 | if (!err) |
755 | trace_bpf_map_delete_elem(map, ufd, key); | |
db20fd2b AS |
756 | kfree(key); |
757 | err_put: | |
758 | fdput(f); | |
759 | return err; | |
760 | } | |
761 | ||
762 | /* last field in 'union bpf_attr' used by this command */ | |
763 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key | |
764 | ||
765 | static int map_get_next_key(union bpf_attr *attr) | |
766 | { | |
535e7b4b MS |
767 | void __user *ukey = u64_to_user_ptr(attr->key); |
768 | void __user *unext_key = u64_to_user_ptr(attr->next_key); | |
db20fd2b | 769 | int ufd = attr->map_fd; |
db20fd2b AS |
770 | struct bpf_map *map; |
771 | void *key, *next_key; | |
592867bf | 772 | struct fd f; |
db20fd2b AS |
773 | int err; |
774 | ||
775 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) | |
776 | return -EINVAL; | |
777 | ||
592867bf | 778 | f = fdget(ufd); |
c2101297 | 779 | map = __bpf_map_get(f); |
db20fd2b AS |
780 | if (IS_ERR(map)) |
781 | return PTR_ERR(map); | |
782 | ||
6e71b04a CF |
783 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
784 | err = -EPERM; | |
785 | goto err_put; | |
786 | } | |
787 | ||
8fe45924 | 788 | if (ukey) { |
e4448ed8 AV |
789 | key = memdup_user(ukey, map->key_size); |
790 | if (IS_ERR(key)) { | |
791 | err = PTR_ERR(key); | |
8fe45924 | 792 | goto err_put; |
e4448ed8 | 793 | } |
8fe45924 TQ |
794 | } else { |
795 | key = NULL; | |
796 | } | |
db20fd2b AS |
797 | |
798 | err = -ENOMEM; | |
799 | next_key = kmalloc(map->key_size, GFP_USER); | |
800 | if (!next_key) | |
801 | goto free_key; | |
802 | ||
803 | rcu_read_lock(); | |
804 | err = map->ops->map_get_next_key(map, key, next_key); | |
805 | rcu_read_unlock(); | |
806 | if (err) | |
807 | goto free_next_key; | |
808 | ||
809 | err = -EFAULT; | |
810 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) | |
811 | goto free_next_key; | |
812 | ||
a67edbf4 | 813 | trace_bpf_map_next_key(map, ufd, key, next_key); |
db20fd2b AS |
814 | err = 0; |
815 | ||
816 | free_next_key: | |
817 | kfree(next_key); | |
818 | free_key: | |
819 | kfree(key); | |
820 | err_put: | |
821 | fdput(f); | |
822 | return err; | |
823 | } | |
824 | ||
7de16e3a JK |
825 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
826 | #define BPF_PROG_TYPE(_id, _name) \ | |
827 | [_id] = & _name ## _prog_ops, | |
828 | #define BPF_MAP_TYPE(_id, _ops) | |
829 | #include <linux/bpf_types.h> | |
830 | #undef BPF_PROG_TYPE | |
831 | #undef BPF_MAP_TYPE | |
832 | }; | |
833 | ||
09756af4 AS |
834 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
835 | { | |
6a9d2be7 DB |
836 | const struct bpf_prog_ops *ops; |
837 | ||
838 | if (type >= ARRAY_SIZE(bpf_prog_types)) | |
839 | return -EINVAL; | |
840 | type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); | |
841 | ops = bpf_prog_types[type]; | |
842 | if (!ops) | |
be9370a7 | 843 | return -EINVAL; |
09756af4 | 844 | |
ab3f0063 | 845 | if (!bpf_prog_is_dev_bound(prog->aux)) |
6a9d2be7 | 846 | prog->aux->ops = ops; |
ab3f0063 JK |
847 | else |
848 | prog->aux->ops = &bpf_offload_prog_ops; | |
be9370a7 JB |
849 | prog->type = type; |
850 | return 0; | |
09756af4 AS |
851 | } |
852 | ||
853 | /* drop refcnt on maps used by eBPF program and free auxilary data */ | |
854 | static void free_used_maps(struct bpf_prog_aux *aux) | |
855 | { | |
856 | int i; | |
857 | ||
858 | for (i = 0; i < aux->used_map_cnt; i++) | |
859 | bpf_map_put(aux->used_maps[i]); | |
860 | ||
861 | kfree(aux->used_maps); | |
862 | } | |
863 | ||
5ccb071e DB |
864 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
865 | { | |
866 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
867 | unsigned long user_bufs; | |
868 | ||
869 | if (user) { | |
870 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); | |
871 | if (user_bufs > memlock_limit) { | |
872 | atomic_long_sub(pages, &user->locked_vm); | |
873 | return -EPERM; | |
874 | } | |
875 | } | |
876 | ||
877 | return 0; | |
878 | } | |
879 | ||
880 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) | |
881 | { | |
882 | if (user) | |
883 | atomic_long_sub(pages, &user->locked_vm); | |
884 | } | |
885 | ||
aaac3ba9 AS |
886 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
887 | { | |
888 | struct user_struct *user = get_current_user(); | |
5ccb071e | 889 | int ret; |
aaac3ba9 | 890 | |
5ccb071e DB |
891 | ret = __bpf_prog_charge(user, prog->pages); |
892 | if (ret) { | |
aaac3ba9 | 893 | free_uid(user); |
5ccb071e | 894 | return ret; |
aaac3ba9 | 895 | } |
5ccb071e | 896 | |
aaac3ba9 AS |
897 | prog->aux->user = user; |
898 | return 0; | |
899 | } | |
900 | ||
901 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) | |
902 | { | |
903 | struct user_struct *user = prog->aux->user; | |
904 | ||
5ccb071e | 905 | __bpf_prog_uncharge(user, prog->pages); |
aaac3ba9 AS |
906 | free_uid(user); |
907 | } | |
908 | ||
dc4bb0e2 MKL |
909 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
910 | { | |
911 | int id; | |
912 | ||
913 | spin_lock_bh(&prog_idr_lock); | |
914 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); | |
915 | if (id > 0) | |
916 | prog->aux->id = id; | |
917 | spin_unlock_bh(&prog_idr_lock); | |
918 | ||
919 | /* id is in [1, INT_MAX) */ | |
920 | if (WARN_ON_ONCE(!id)) | |
921 | return -ENOSPC; | |
922 | ||
923 | return id > 0 ? 0 : id; | |
924 | } | |
925 | ||
b16d9aa4 | 926 | static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) |
dc4bb0e2 MKL |
927 | { |
928 | /* cBPF to eBPF migrations are currently not in the idr store. */ | |
929 | if (!prog->aux->id) | |
930 | return; | |
931 | ||
b16d9aa4 MKL |
932 | if (do_idr_lock) |
933 | spin_lock_bh(&prog_idr_lock); | |
934 | else | |
935 | __acquire(&prog_idr_lock); | |
936 | ||
dc4bb0e2 | 937 | idr_remove(&prog_idr, prog->aux->id); |
b16d9aa4 MKL |
938 | |
939 | if (do_idr_lock) | |
940 | spin_unlock_bh(&prog_idr_lock); | |
941 | else | |
942 | __release(&prog_idr_lock); | |
dc4bb0e2 MKL |
943 | } |
944 | ||
1aacde3d | 945 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
abf2e7d6 AS |
946 | { |
947 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); | |
948 | ||
949 | free_used_maps(aux); | |
aaac3ba9 | 950 | bpf_prog_uncharge_memlock(aux->prog); |
afdb09c7 | 951 | security_bpf_prog_free(aux); |
abf2e7d6 AS |
952 | bpf_prog_free(aux->prog); |
953 | } | |
954 | ||
b16d9aa4 | 955 | static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) |
09756af4 | 956 | { |
a67edbf4 DB |
957 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
958 | trace_bpf_prog_put_rcu(prog); | |
34ad5580 | 959 | /* bpf_prog_free_id() must be called first */ |
b16d9aa4 | 960 | bpf_prog_free_id(prog, do_idr_lock); |
74451e66 | 961 | bpf_prog_kallsyms_del(prog); |
1aacde3d | 962 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
a67edbf4 | 963 | } |
09756af4 | 964 | } |
b16d9aa4 MKL |
965 | |
966 | void bpf_prog_put(struct bpf_prog *prog) | |
967 | { | |
968 | __bpf_prog_put(prog, true); | |
969 | } | |
e2e9b654 | 970 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
09756af4 AS |
971 | |
972 | static int bpf_prog_release(struct inode *inode, struct file *filp) | |
973 | { | |
974 | struct bpf_prog *prog = filp->private_data; | |
975 | ||
1aacde3d | 976 | bpf_prog_put(prog); |
09756af4 AS |
977 | return 0; |
978 | } | |
979 | ||
7bd509e3 DB |
980 | #ifdef CONFIG_PROC_FS |
981 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) | |
982 | { | |
983 | const struct bpf_prog *prog = filp->private_data; | |
f1f7714e | 984 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
7bd509e3 | 985 | |
f1f7714e | 986 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
7bd509e3 DB |
987 | seq_printf(m, |
988 | "prog_type:\t%u\n" | |
989 | "prog_jited:\t%u\n" | |
f1f7714e | 990 | "prog_tag:\t%s\n" |
7bd509e3 DB |
991 | "memlock:\t%llu\n", |
992 | prog->type, | |
993 | prog->jited, | |
f1f7714e | 994 | prog_tag, |
7bd509e3 DB |
995 | prog->pages * 1ULL << PAGE_SHIFT); |
996 | } | |
997 | #endif | |
998 | ||
f66e448c | 999 | const struct file_operations bpf_prog_fops = { |
7bd509e3 DB |
1000 | #ifdef CONFIG_PROC_FS |
1001 | .show_fdinfo = bpf_prog_show_fdinfo, | |
1002 | #endif | |
1003 | .release = bpf_prog_release, | |
6e71b04a CF |
1004 | .read = bpf_dummy_read, |
1005 | .write = bpf_dummy_write, | |
09756af4 AS |
1006 | }; |
1007 | ||
b2197755 | 1008 | int bpf_prog_new_fd(struct bpf_prog *prog) |
aa79781b | 1009 | { |
afdb09c7 CF |
1010 | int ret; |
1011 | ||
1012 | ret = security_bpf_prog(prog); | |
1013 | if (ret < 0) | |
1014 | return ret; | |
1015 | ||
aa79781b DB |
1016 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
1017 | O_RDWR | O_CLOEXEC); | |
1018 | } | |
1019 | ||
113214be | 1020 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
09756af4 | 1021 | { |
09756af4 AS |
1022 | if (!f.file) |
1023 | return ERR_PTR(-EBADF); | |
09756af4 AS |
1024 | if (f.file->f_op != &bpf_prog_fops) { |
1025 | fdput(f); | |
1026 | return ERR_PTR(-EINVAL); | |
1027 | } | |
1028 | ||
c2101297 | 1029 | return f.file->private_data; |
09756af4 AS |
1030 | } |
1031 | ||
59d3656d | 1032 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
92117d84 | 1033 | { |
59d3656d BB |
1034 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
1035 | atomic_sub(i, &prog->aux->refcnt); | |
92117d84 AS |
1036 | return ERR_PTR(-EBUSY); |
1037 | } | |
1038 | return prog; | |
1039 | } | |
59d3656d BB |
1040 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
1041 | ||
c540594f DB |
1042 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
1043 | { | |
1044 | /* Only to be used for undoing previous bpf_prog_add() in some | |
1045 | * error path. We still know that another entity in our call | |
1046 | * path holds a reference to the program, thus atomic_sub() can | |
1047 | * be safely used in such cases! | |
1048 | */ | |
1049 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); | |
1050 | } | |
1051 | EXPORT_SYMBOL_GPL(bpf_prog_sub); | |
1052 | ||
59d3656d BB |
1053 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
1054 | { | |
1055 | return bpf_prog_add(prog, 1); | |
1056 | } | |
97bc402d | 1057 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
92117d84 | 1058 | |
b16d9aa4 | 1059 | /* prog_idr_lock should have been held */ |
a6f6df69 | 1060 | struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) |
b16d9aa4 MKL |
1061 | { |
1062 | int refold; | |
1063 | ||
1064 | refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0); | |
1065 | ||
1066 | if (refold >= BPF_MAX_REFCNT) { | |
1067 | __bpf_prog_put(prog, false); | |
1068 | return ERR_PTR(-EBUSY); | |
1069 | } | |
1070 | ||
1071 | if (!refold) | |
1072 | return ERR_PTR(-ENOENT); | |
1073 | ||
1074 | return prog; | |
1075 | } | |
a6f6df69 | 1076 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
b16d9aa4 | 1077 | |
040ee692 | 1078 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
288b3de5 | 1079 | enum bpf_prog_type *attach_type, bool attach_drv) |
248f346f | 1080 | { |
288b3de5 JK |
1081 | /* not an attachment, just a refcount inc, always allow */ |
1082 | if (!attach_type) | |
1083 | return true; | |
248f346f JK |
1084 | |
1085 | if (prog->type != *attach_type) | |
1086 | return false; | |
288b3de5 | 1087 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
248f346f JK |
1088 | return false; |
1089 | ||
1090 | return true; | |
1091 | } | |
1092 | ||
1093 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, | |
288b3de5 | 1094 | bool attach_drv) |
09756af4 AS |
1095 | { |
1096 | struct fd f = fdget(ufd); | |
1097 | struct bpf_prog *prog; | |
1098 | ||
113214be | 1099 | prog = ____bpf_prog_get(f); |
09756af4 AS |
1100 | if (IS_ERR(prog)) |
1101 | return prog; | |
288b3de5 | 1102 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
113214be DB |
1103 | prog = ERR_PTR(-EINVAL); |
1104 | goto out; | |
1105 | } | |
09756af4 | 1106 | |
92117d84 | 1107 | prog = bpf_prog_inc(prog); |
113214be | 1108 | out: |
09756af4 AS |
1109 | fdput(f); |
1110 | return prog; | |
1111 | } | |
113214be DB |
1112 | |
1113 | struct bpf_prog *bpf_prog_get(u32 ufd) | |
1114 | { | |
288b3de5 | 1115 | return __bpf_prog_get(ufd, NULL, false); |
113214be DB |
1116 | } |
1117 | ||
248f346f | 1118 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
288b3de5 | 1119 | bool attach_drv) |
248f346f | 1120 | { |
288b3de5 | 1121 | struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv); |
248f346f JK |
1122 | |
1123 | if (!IS_ERR(prog)) | |
1124 | trace_bpf_prog_get_type(prog); | |
1125 | return prog; | |
1126 | } | |
6c8dfe21 | 1127 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
248f346f | 1128 | |
09756af4 | 1129 | /* last field in 'union bpf_attr' used by this command */ |
1f6f4cb7 | 1130 | #define BPF_PROG_LOAD_LAST_FIELD prog_ifindex |
09756af4 AS |
1131 | |
1132 | static int bpf_prog_load(union bpf_attr *attr) | |
1133 | { | |
1134 | enum bpf_prog_type type = attr->prog_type; | |
1135 | struct bpf_prog *prog; | |
1136 | int err; | |
1137 | char license[128]; | |
1138 | bool is_gpl; | |
1139 | ||
1140 | if (CHECK_ATTR(BPF_PROG_LOAD)) | |
1141 | return -EINVAL; | |
1142 | ||
e07b98d9 DM |
1143 | if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT) |
1144 | return -EINVAL; | |
1145 | ||
09756af4 | 1146 | /* copy eBPF program license from user space */ |
535e7b4b | 1147 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
09756af4 AS |
1148 | sizeof(license) - 1) < 0) |
1149 | return -EFAULT; | |
1150 | license[sizeof(license) - 1] = 0; | |
1151 | ||
1152 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
1153 | is_gpl = license_is_gpl_compatible(license); | |
1154 | ||
ef0915ca DB |
1155 | if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) |
1156 | return -E2BIG; | |
09756af4 | 1157 | |
2541517c AS |
1158 | if (type == BPF_PROG_TYPE_KPROBE && |
1159 | attr->kern_version != LINUX_VERSION_CODE) | |
1160 | return -EINVAL; | |
1161 | ||
80b7d819 CF |
1162 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
1163 | type != BPF_PROG_TYPE_CGROUP_SKB && | |
1164 | !capable(CAP_SYS_ADMIN)) | |
1be7f75d AS |
1165 | return -EPERM; |
1166 | ||
09756af4 AS |
1167 | /* plain bpf_prog allocation */ |
1168 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); | |
1169 | if (!prog) | |
1170 | return -ENOMEM; | |
1171 | ||
afdb09c7 | 1172 | err = security_bpf_prog_alloc(prog->aux); |
aaac3ba9 AS |
1173 | if (err) |
1174 | goto free_prog_nouncharge; | |
1175 | ||
afdb09c7 CF |
1176 | err = bpf_prog_charge_memlock(prog); |
1177 | if (err) | |
1178 | goto free_prog_sec; | |
1179 | ||
09756af4 AS |
1180 | prog->len = attr->insn_cnt; |
1181 | ||
1182 | err = -EFAULT; | |
535e7b4b | 1183 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
aafe6ae9 | 1184 | bpf_prog_insn_size(prog)) != 0) |
09756af4 AS |
1185 | goto free_prog; |
1186 | ||
1187 | prog->orig_prog = NULL; | |
a91263d5 | 1188 | prog->jited = 0; |
09756af4 AS |
1189 | |
1190 | atomic_set(&prog->aux->refcnt, 1); | |
a91263d5 | 1191 | prog->gpl_compatible = is_gpl ? 1 : 0; |
09756af4 | 1192 | |
1f6f4cb7 | 1193 | if (attr->prog_ifindex) { |
ab3f0063 JK |
1194 | err = bpf_prog_offload_init(prog, attr); |
1195 | if (err) | |
1196 | goto free_prog; | |
1197 | } | |
1198 | ||
09756af4 AS |
1199 | /* find program type: socket_filter vs tracing_filter */ |
1200 | err = find_prog_type(type, prog); | |
1201 | if (err < 0) | |
1202 | goto free_prog; | |
1203 | ||
cb4d2b3f MKL |
1204 | prog->aux->load_time = ktime_get_boot_ns(); |
1205 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); | |
1206 | if (err) | |
1207 | goto free_prog; | |
1208 | ||
09756af4 | 1209 | /* run eBPF verifier */ |
9bac3d6d | 1210 | err = bpf_check(&prog, attr); |
09756af4 AS |
1211 | if (err < 0) |
1212 | goto free_used_maps; | |
1213 | ||
1214 | /* eBPF program is ready to be JITed */ | |
d1c55ab5 | 1215 | prog = bpf_prog_select_runtime(prog, &err); |
04fd61ab AS |
1216 | if (err < 0) |
1217 | goto free_used_maps; | |
09756af4 | 1218 | |
dc4bb0e2 MKL |
1219 | err = bpf_prog_alloc_id(prog); |
1220 | if (err) | |
1221 | goto free_used_maps; | |
1222 | ||
aa79781b | 1223 | err = bpf_prog_new_fd(prog); |
b16d9aa4 MKL |
1224 | if (err < 0) { |
1225 | /* failed to allocate fd. | |
1226 | * bpf_prog_put() is needed because the above | |
1227 | * bpf_prog_alloc_id() has published the prog | |
1228 | * to the userspace and the userspace may | |
1229 | * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. | |
1230 | */ | |
1231 | bpf_prog_put(prog); | |
1232 | return err; | |
1233 | } | |
09756af4 | 1234 | |
74451e66 | 1235 | bpf_prog_kallsyms_add(prog); |
a67edbf4 | 1236 | trace_bpf_prog_load(prog, err); |
09756af4 AS |
1237 | return err; |
1238 | ||
1239 | free_used_maps: | |
1240 | free_used_maps(prog->aux); | |
1241 | free_prog: | |
aaac3ba9 | 1242 | bpf_prog_uncharge_memlock(prog); |
afdb09c7 CF |
1243 | free_prog_sec: |
1244 | security_bpf_prog_free(prog->aux); | |
aaac3ba9 | 1245 | free_prog_nouncharge: |
09756af4 AS |
1246 | bpf_prog_free(prog); |
1247 | return err; | |
1248 | } | |
1249 | ||
6e71b04a | 1250 | #define BPF_OBJ_LAST_FIELD file_flags |
b2197755 DB |
1251 | |
1252 | static int bpf_obj_pin(const union bpf_attr *attr) | |
1253 | { | |
6e71b04a | 1254 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
b2197755 DB |
1255 | return -EINVAL; |
1256 | ||
535e7b4b | 1257 | return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); |
b2197755 DB |
1258 | } |
1259 | ||
1260 | static int bpf_obj_get(const union bpf_attr *attr) | |
1261 | { | |
6e71b04a CF |
1262 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
1263 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) | |
b2197755 DB |
1264 | return -EINVAL; |
1265 | ||
6e71b04a CF |
1266 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
1267 | attr->file_flags); | |
b2197755 DB |
1268 | } |
1269 | ||
f4324551 DM |
1270 | #ifdef CONFIG_CGROUP_BPF |
1271 | ||
464bc0fd | 1272 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
174a79ff | 1273 | |
5a67da2a | 1274 | static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach) |
174a79ff | 1275 | { |
5a67da2a | 1276 | struct bpf_prog *prog = NULL; |
174a79ff JF |
1277 | int ufd = attr->target_fd; |
1278 | struct bpf_map *map; | |
1279 | struct fd f; | |
1280 | int err; | |
1281 | ||
1282 | f = fdget(ufd); | |
1283 | map = __bpf_map_get(f); | |
1284 | if (IS_ERR(map)) | |
1285 | return PTR_ERR(map); | |
1286 | ||
5a67da2a JF |
1287 | if (attach) { |
1288 | prog = bpf_prog_get_type(attr->attach_bpf_fd, | |
1289 | BPF_PROG_TYPE_SK_SKB); | |
1290 | if (IS_ERR(prog)) { | |
1291 | fdput(f); | |
1292 | return PTR_ERR(prog); | |
1293 | } | |
174a79ff JF |
1294 | } |
1295 | ||
5a67da2a | 1296 | err = sock_map_prog(map, prog, attr->attach_type); |
174a79ff JF |
1297 | if (err) { |
1298 | fdput(f); | |
5a67da2a JF |
1299 | if (prog) |
1300 | bpf_prog_put(prog); | |
ae2b27b8 | 1301 | return err; |
174a79ff JF |
1302 | } |
1303 | ||
1304 | fdput(f); | |
ae2b27b8 | 1305 | return 0; |
174a79ff | 1306 | } |
f4324551 | 1307 | |
324bda9e AS |
1308 | #define BPF_F_ATTACH_MASK \ |
1309 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) | |
1310 | ||
f4324551 DM |
1311 | static int bpf_prog_attach(const union bpf_attr *attr) |
1312 | { | |
7f677633 | 1313 | enum bpf_prog_type ptype; |
f4324551 DM |
1314 | struct bpf_prog *prog; |
1315 | struct cgroup *cgrp; | |
7f677633 | 1316 | int ret; |
f4324551 DM |
1317 | |
1318 | if (!capable(CAP_NET_ADMIN)) | |
1319 | return -EPERM; | |
1320 | ||
1321 | if (CHECK_ATTR(BPF_PROG_ATTACH)) | |
1322 | return -EINVAL; | |
1323 | ||
324bda9e | 1324 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
7f677633 AS |
1325 | return -EINVAL; |
1326 | ||
f4324551 DM |
1327 | switch (attr->attach_type) { |
1328 | case BPF_CGROUP_INET_INGRESS: | |
1329 | case BPF_CGROUP_INET_EGRESS: | |
b2cd1257 | 1330 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
f4324551 | 1331 | break; |
61023658 DA |
1332 | case BPF_CGROUP_INET_SOCK_CREATE: |
1333 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; | |
1334 | break; | |
40304b2a LB |
1335 | case BPF_CGROUP_SOCK_OPS: |
1336 | ptype = BPF_PROG_TYPE_SOCK_OPS; | |
1337 | break; | |
ebc614f6 RG |
1338 | case BPF_CGROUP_DEVICE: |
1339 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; | |
1340 | break; | |
464bc0fd JF |
1341 | case BPF_SK_SKB_STREAM_PARSER: |
1342 | case BPF_SK_SKB_STREAM_VERDICT: | |
5a67da2a | 1343 | return sockmap_get_from_fd(attr, true); |
f4324551 DM |
1344 | default: |
1345 | return -EINVAL; | |
1346 | } | |
1347 | ||
b2cd1257 DA |
1348 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
1349 | if (IS_ERR(prog)) | |
1350 | return PTR_ERR(prog); | |
1351 | ||
1352 | cgrp = cgroup_get_from_fd(attr->target_fd); | |
1353 | if (IS_ERR(cgrp)) { | |
1354 | bpf_prog_put(prog); | |
1355 | return PTR_ERR(cgrp); | |
1356 | } | |
1357 | ||
324bda9e AS |
1358 | ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type, |
1359 | attr->attach_flags); | |
7f677633 AS |
1360 | if (ret) |
1361 | bpf_prog_put(prog); | |
b2cd1257 DA |
1362 | cgroup_put(cgrp); |
1363 | ||
7f677633 | 1364 | return ret; |
f4324551 DM |
1365 | } |
1366 | ||
1367 | #define BPF_PROG_DETACH_LAST_FIELD attach_type | |
1368 | ||
1369 | static int bpf_prog_detach(const union bpf_attr *attr) | |
1370 | { | |
324bda9e AS |
1371 | enum bpf_prog_type ptype; |
1372 | struct bpf_prog *prog; | |
f4324551 | 1373 | struct cgroup *cgrp; |
7f677633 | 1374 | int ret; |
f4324551 DM |
1375 | |
1376 | if (!capable(CAP_NET_ADMIN)) | |
1377 | return -EPERM; | |
1378 | ||
1379 | if (CHECK_ATTR(BPF_PROG_DETACH)) | |
1380 | return -EINVAL; | |
1381 | ||
1382 | switch (attr->attach_type) { | |
1383 | case BPF_CGROUP_INET_INGRESS: | |
1384 | case BPF_CGROUP_INET_EGRESS: | |
324bda9e AS |
1385 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
1386 | break; | |
61023658 | 1387 | case BPF_CGROUP_INET_SOCK_CREATE: |
324bda9e AS |
1388 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
1389 | break; | |
40304b2a | 1390 | case BPF_CGROUP_SOCK_OPS: |
324bda9e | 1391 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
f4324551 | 1392 | break; |
ebc614f6 RG |
1393 | case BPF_CGROUP_DEVICE: |
1394 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; | |
1395 | break; | |
5a67da2a JF |
1396 | case BPF_SK_SKB_STREAM_PARSER: |
1397 | case BPF_SK_SKB_STREAM_VERDICT: | |
324bda9e | 1398 | return sockmap_get_from_fd(attr, false); |
f4324551 DM |
1399 | default: |
1400 | return -EINVAL; | |
1401 | } | |
1402 | ||
324bda9e AS |
1403 | cgrp = cgroup_get_from_fd(attr->target_fd); |
1404 | if (IS_ERR(cgrp)) | |
1405 | return PTR_ERR(cgrp); | |
1406 | ||
1407 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); | |
1408 | if (IS_ERR(prog)) | |
1409 | prog = NULL; | |
1410 | ||
1411 | ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0); | |
1412 | if (prog) | |
1413 | bpf_prog_put(prog); | |
1414 | cgroup_put(cgrp); | |
7f677633 | 1415 | return ret; |
f4324551 | 1416 | } |
40304b2a | 1417 | |
468e2f64 AS |
1418 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
1419 | ||
1420 | static int bpf_prog_query(const union bpf_attr *attr, | |
1421 | union bpf_attr __user *uattr) | |
1422 | { | |
1423 | struct cgroup *cgrp; | |
1424 | int ret; | |
1425 | ||
1426 | if (!capable(CAP_NET_ADMIN)) | |
1427 | return -EPERM; | |
1428 | if (CHECK_ATTR(BPF_PROG_QUERY)) | |
1429 | return -EINVAL; | |
1430 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) | |
1431 | return -EINVAL; | |
1432 | ||
1433 | switch (attr->query.attach_type) { | |
1434 | case BPF_CGROUP_INET_INGRESS: | |
1435 | case BPF_CGROUP_INET_EGRESS: | |
1436 | case BPF_CGROUP_INET_SOCK_CREATE: | |
1437 | case BPF_CGROUP_SOCK_OPS: | |
ebc614f6 | 1438 | case BPF_CGROUP_DEVICE: |
468e2f64 AS |
1439 | break; |
1440 | default: | |
1441 | return -EINVAL; | |
1442 | } | |
1443 | cgrp = cgroup_get_from_fd(attr->query.target_fd); | |
1444 | if (IS_ERR(cgrp)) | |
1445 | return PTR_ERR(cgrp); | |
1446 | ret = cgroup_bpf_query(cgrp, attr, uattr); | |
1447 | cgroup_put(cgrp); | |
1448 | return ret; | |
1449 | } | |
f4324551 DM |
1450 | #endif /* CONFIG_CGROUP_BPF */ |
1451 | ||
1cf1cae9 AS |
1452 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration |
1453 | ||
1454 | static int bpf_prog_test_run(const union bpf_attr *attr, | |
1455 | union bpf_attr __user *uattr) | |
1456 | { | |
1457 | struct bpf_prog *prog; | |
1458 | int ret = -ENOTSUPP; | |
1459 | ||
1460 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) | |
1461 | return -EINVAL; | |
1462 | ||
1463 | prog = bpf_prog_get(attr->test.prog_fd); | |
1464 | if (IS_ERR(prog)) | |
1465 | return PTR_ERR(prog); | |
1466 | ||
1467 | if (prog->aux->ops->test_run) | |
1468 | ret = prog->aux->ops->test_run(prog, attr, uattr); | |
1469 | ||
1470 | bpf_prog_put(prog); | |
1471 | return ret; | |
1472 | } | |
1473 | ||
34ad5580 MKL |
1474 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
1475 | ||
1476 | static int bpf_obj_get_next_id(const union bpf_attr *attr, | |
1477 | union bpf_attr __user *uattr, | |
1478 | struct idr *idr, | |
1479 | spinlock_t *lock) | |
1480 | { | |
1481 | u32 next_id = attr->start_id; | |
1482 | int err = 0; | |
1483 | ||
1484 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) | |
1485 | return -EINVAL; | |
1486 | ||
1487 | if (!capable(CAP_SYS_ADMIN)) | |
1488 | return -EPERM; | |
1489 | ||
1490 | next_id++; | |
1491 | spin_lock_bh(lock); | |
1492 | if (!idr_get_next(idr, &next_id)) | |
1493 | err = -ENOENT; | |
1494 | spin_unlock_bh(lock); | |
1495 | ||
1496 | if (!err) | |
1497 | err = put_user(next_id, &uattr->next_id); | |
1498 | ||
1499 | return err; | |
1500 | } | |
1501 | ||
b16d9aa4 MKL |
1502 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
1503 | ||
1504 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) | |
1505 | { | |
1506 | struct bpf_prog *prog; | |
1507 | u32 id = attr->prog_id; | |
1508 | int fd; | |
1509 | ||
1510 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) | |
1511 | return -EINVAL; | |
1512 | ||
1513 | if (!capable(CAP_SYS_ADMIN)) | |
1514 | return -EPERM; | |
1515 | ||
1516 | spin_lock_bh(&prog_idr_lock); | |
1517 | prog = idr_find(&prog_idr, id); | |
1518 | if (prog) | |
1519 | prog = bpf_prog_inc_not_zero(prog); | |
1520 | else | |
1521 | prog = ERR_PTR(-ENOENT); | |
1522 | spin_unlock_bh(&prog_idr_lock); | |
1523 | ||
1524 | if (IS_ERR(prog)) | |
1525 | return PTR_ERR(prog); | |
1526 | ||
1527 | fd = bpf_prog_new_fd(prog); | |
1528 | if (fd < 0) | |
1529 | bpf_prog_put(prog); | |
1530 | ||
1531 | return fd; | |
1532 | } | |
1533 | ||
6e71b04a | 1534 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
bd5f5f4e MKL |
1535 | |
1536 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) | |
1537 | { | |
1538 | struct bpf_map *map; | |
1539 | u32 id = attr->map_id; | |
6e71b04a | 1540 | int f_flags; |
bd5f5f4e MKL |
1541 | int fd; |
1542 | ||
6e71b04a CF |
1543 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
1544 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) | |
bd5f5f4e MKL |
1545 | return -EINVAL; |
1546 | ||
1547 | if (!capable(CAP_SYS_ADMIN)) | |
1548 | return -EPERM; | |
1549 | ||
6e71b04a CF |
1550 | f_flags = bpf_get_file_flag(attr->open_flags); |
1551 | if (f_flags < 0) | |
1552 | return f_flags; | |
1553 | ||
bd5f5f4e MKL |
1554 | spin_lock_bh(&map_idr_lock); |
1555 | map = idr_find(&map_idr, id); | |
1556 | if (map) | |
1557 | map = bpf_map_inc_not_zero(map, true); | |
1558 | else | |
1559 | map = ERR_PTR(-ENOENT); | |
1560 | spin_unlock_bh(&map_idr_lock); | |
1561 | ||
1562 | if (IS_ERR(map)) | |
1563 | return PTR_ERR(map); | |
1564 | ||
6e71b04a | 1565 | fd = bpf_map_new_fd(map, f_flags); |
bd5f5f4e MKL |
1566 | if (fd < 0) |
1567 | bpf_map_put(map); | |
1568 | ||
1569 | return fd; | |
1570 | } | |
1571 | ||
1e270976 MKL |
1572 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
1573 | const union bpf_attr *attr, | |
1574 | union bpf_attr __user *uattr) | |
1575 | { | |
1576 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
1577 | struct bpf_prog_info info = {}; | |
1578 | u32 info_len = attr->info.info_len; | |
1579 | char __user *uinsns; | |
1580 | u32 ulen; | |
1581 | int err; | |
1582 | ||
1583 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); | |
1584 | if (err) | |
1585 | return err; | |
1586 | info_len = min_t(u32, sizeof(info), info_len); | |
1587 | ||
1588 | if (copy_from_user(&info, uinfo, info_len)) | |
89b09689 | 1589 | return -EFAULT; |
1e270976 MKL |
1590 | |
1591 | info.type = prog->type; | |
1592 | info.id = prog->aux->id; | |
cb4d2b3f MKL |
1593 | info.load_time = prog->aux->load_time; |
1594 | info.created_by_uid = from_kuid_munged(current_user_ns(), | |
1595 | prog->aux->user->uid); | |
1e270976 MKL |
1596 | |
1597 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); | |
cb4d2b3f MKL |
1598 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
1599 | ||
1600 | ulen = info.nr_map_ids; | |
1601 | info.nr_map_ids = prog->aux->used_map_cnt; | |
1602 | ulen = min_t(u32, info.nr_map_ids, ulen); | |
1603 | if (ulen) { | |
721e08da | 1604 | u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); |
cb4d2b3f MKL |
1605 | u32 i; |
1606 | ||
1607 | for (i = 0; i < ulen; i++) | |
1608 | if (put_user(prog->aux->used_maps[i]->id, | |
1609 | &user_map_ids[i])) | |
1610 | return -EFAULT; | |
1611 | } | |
1e270976 MKL |
1612 | |
1613 | if (!capable(CAP_SYS_ADMIN)) { | |
1614 | info.jited_prog_len = 0; | |
1615 | info.xlated_prog_len = 0; | |
1616 | goto done; | |
1617 | } | |
1618 | ||
1619 | ulen = info.jited_prog_len; | |
1620 | info.jited_prog_len = prog->jited_len; | |
1621 | if (info.jited_prog_len && ulen) { | |
1622 | uinsns = u64_to_user_ptr(info.jited_prog_insns); | |
1623 | ulen = min_t(u32, info.jited_prog_len, ulen); | |
1624 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) | |
1625 | return -EFAULT; | |
1626 | } | |
1627 | ||
1628 | ulen = info.xlated_prog_len; | |
9975a54b | 1629 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
1e270976 MKL |
1630 | if (info.xlated_prog_len && ulen) { |
1631 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); | |
1632 | ulen = min_t(u32, info.xlated_prog_len, ulen); | |
1633 | if (copy_to_user(uinsns, prog->insnsi, ulen)) | |
1634 | return -EFAULT; | |
1635 | } | |
1636 | ||
1637 | done: | |
1638 | if (copy_to_user(uinfo, &info, info_len) || | |
1639 | put_user(info_len, &uattr->info.info_len)) | |
1640 | return -EFAULT; | |
1641 | ||
1642 | return 0; | |
1643 | } | |
1644 | ||
1645 | static int bpf_map_get_info_by_fd(struct bpf_map *map, | |
1646 | const union bpf_attr *attr, | |
1647 | union bpf_attr __user *uattr) | |
1648 | { | |
1649 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
1650 | struct bpf_map_info info = {}; | |
1651 | u32 info_len = attr->info.info_len; | |
1652 | int err; | |
1653 | ||
1654 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); | |
1655 | if (err) | |
1656 | return err; | |
1657 | info_len = min_t(u32, sizeof(info), info_len); | |
1658 | ||
1659 | info.type = map->map_type; | |
1660 | info.id = map->id; | |
1661 | info.key_size = map->key_size; | |
1662 | info.value_size = map->value_size; | |
1663 | info.max_entries = map->max_entries; | |
1664 | info.map_flags = map->map_flags; | |
ad5b177b | 1665 | memcpy(info.name, map->name, sizeof(map->name)); |
1e270976 MKL |
1666 | |
1667 | if (copy_to_user(uinfo, &info, info_len) || | |
1668 | put_user(info_len, &uattr->info.info_len)) | |
1669 | return -EFAULT; | |
1670 | ||
1671 | return 0; | |
1672 | } | |
1673 | ||
1674 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info | |
1675 | ||
1676 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, | |
1677 | union bpf_attr __user *uattr) | |
1678 | { | |
1679 | int ufd = attr->info.bpf_fd; | |
1680 | struct fd f; | |
1681 | int err; | |
1682 | ||
1683 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) | |
1684 | return -EINVAL; | |
1685 | ||
1686 | f = fdget(ufd); | |
1687 | if (!f.file) | |
1688 | return -EBADFD; | |
1689 | ||
1690 | if (f.file->f_op == &bpf_prog_fops) | |
1691 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, | |
1692 | uattr); | |
1693 | else if (f.file->f_op == &bpf_map_fops) | |
1694 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, | |
1695 | uattr); | |
1696 | else | |
1697 | err = -EINVAL; | |
1698 | ||
1699 | fdput(f); | |
1700 | return err; | |
1701 | } | |
1702 | ||
99c55f7d AS |
1703 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
1704 | { | |
1705 | union bpf_attr attr = {}; | |
1706 | int err; | |
1707 | ||
4a7070b7 | 1708 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
99c55f7d AS |
1709 | return -EPERM; |
1710 | ||
1e270976 MKL |
1711 | err = check_uarg_tail_zero(uattr, sizeof(attr), size); |
1712 | if (err) | |
1713 | return err; | |
1714 | size = min_t(u32, size, sizeof(attr)); | |
99c55f7d AS |
1715 | |
1716 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ | |
1717 | if (copy_from_user(&attr, uattr, size) != 0) | |
1718 | return -EFAULT; | |
1719 | ||
afdb09c7 CF |
1720 | err = security_bpf(cmd, &attr, size); |
1721 | if (err < 0) | |
1722 | return err; | |
1723 | ||
99c55f7d AS |
1724 | switch (cmd) { |
1725 | case BPF_MAP_CREATE: | |
1726 | err = map_create(&attr); | |
1727 | break; | |
db20fd2b AS |
1728 | case BPF_MAP_LOOKUP_ELEM: |
1729 | err = map_lookup_elem(&attr); | |
1730 | break; | |
1731 | case BPF_MAP_UPDATE_ELEM: | |
1732 | err = map_update_elem(&attr); | |
1733 | break; | |
1734 | case BPF_MAP_DELETE_ELEM: | |
1735 | err = map_delete_elem(&attr); | |
1736 | break; | |
1737 | case BPF_MAP_GET_NEXT_KEY: | |
1738 | err = map_get_next_key(&attr); | |
1739 | break; | |
09756af4 AS |
1740 | case BPF_PROG_LOAD: |
1741 | err = bpf_prog_load(&attr); | |
1742 | break; | |
b2197755 DB |
1743 | case BPF_OBJ_PIN: |
1744 | err = bpf_obj_pin(&attr); | |
1745 | break; | |
1746 | case BPF_OBJ_GET: | |
1747 | err = bpf_obj_get(&attr); | |
1748 | break; | |
f4324551 DM |
1749 | #ifdef CONFIG_CGROUP_BPF |
1750 | case BPF_PROG_ATTACH: | |
1751 | err = bpf_prog_attach(&attr); | |
1752 | break; | |
1753 | case BPF_PROG_DETACH: | |
1754 | err = bpf_prog_detach(&attr); | |
1755 | break; | |
468e2f64 AS |
1756 | case BPF_PROG_QUERY: |
1757 | err = bpf_prog_query(&attr, uattr); | |
1758 | break; | |
f4324551 | 1759 | #endif |
1cf1cae9 AS |
1760 | case BPF_PROG_TEST_RUN: |
1761 | err = bpf_prog_test_run(&attr, uattr); | |
1762 | break; | |
34ad5580 MKL |
1763 | case BPF_PROG_GET_NEXT_ID: |
1764 | err = bpf_obj_get_next_id(&attr, uattr, | |
1765 | &prog_idr, &prog_idr_lock); | |
1766 | break; | |
1767 | case BPF_MAP_GET_NEXT_ID: | |
1768 | err = bpf_obj_get_next_id(&attr, uattr, | |
1769 | &map_idr, &map_idr_lock); | |
1770 | break; | |
b16d9aa4 MKL |
1771 | case BPF_PROG_GET_FD_BY_ID: |
1772 | err = bpf_prog_get_fd_by_id(&attr); | |
1773 | break; | |
bd5f5f4e MKL |
1774 | case BPF_MAP_GET_FD_BY_ID: |
1775 | err = bpf_map_get_fd_by_id(&attr); | |
1776 | break; | |
1e270976 MKL |
1777 | case BPF_OBJ_GET_INFO_BY_FD: |
1778 | err = bpf_obj_get_info_by_fd(&attr, uattr); | |
1779 | break; | |
99c55f7d AS |
1780 | default: |
1781 | err = -EINVAL; | |
1782 | break; | |
1783 | } | |
1784 | ||
1785 | return err; | |
1786 | } |