Commit | Line | Data |
---|---|---|
5b497af4 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
99c55f7d | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
99c55f7d AS |
3 | */ |
4 | #include <linux/bpf.h> | |
a67edbf4 | 5 | #include <linux/bpf_trace.h> |
f4364dcf | 6 | #include <linux/bpf_lirc.h> |
f56a653c | 7 | #include <linux/btf.h> |
99c55f7d AS |
8 | #include <linux/syscalls.h> |
9 | #include <linux/slab.h> | |
3f07c014 | 10 | #include <linux/sched/signal.h> |
d407bd25 DB |
11 | #include <linux/vmalloc.h> |
12 | #include <linux/mmzone.h> | |
99c55f7d | 13 | #include <linux/anon_inodes.h> |
41bdc4b4 | 14 | #include <linux/fdtable.h> |
db20fd2b | 15 | #include <linux/file.h> |
41bdc4b4 | 16 | #include <linux/fs.h> |
09756af4 AS |
17 | #include <linux/license.h> |
18 | #include <linux/filter.h> | |
2541517c | 19 | #include <linux/version.h> |
535e7b4b | 20 | #include <linux/kernel.h> |
dc4bb0e2 | 21 | #include <linux/idr.h> |
cb4d2b3f MKL |
22 | #include <linux/cred.h> |
23 | #include <linux/timekeeping.h> | |
24 | #include <linux/ctype.h> | |
9ef09e35 | 25 | #include <linux/nospec.h> |
99c55f7d | 26 | |
14dc6f04 MKL |
27 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
28 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ | |
29 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ | |
30 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) | |
31 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) | |
32 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) | |
33 | ||
6e71b04a CF |
34 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
35 | ||
b121d1e7 | 36 | DEFINE_PER_CPU(int, bpf_prog_active); |
dc4bb0e2 MKL |
37 | static DEFINE_IDR(prog_idr); |
38 | static DEFINE_SPINLOCK(prog_idr_lock); | |
f3f1c054 MKL |
39 | static DEFINE_IDR(map_idr); |
40 | static DEFINE_SPINLOCK(map_idr_lock); | |
b121d1e7 | 41 | |
1be7f75d AS |
42 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
43 | ||
40077e0c JB |
44 | static const struct bpf_map_ops * const bpf_map_types[] = { |
45 | #define BPF_PROG_TYPE(_id, _ops) | |
46 | #define BPF_MAP_TYPE(_id, _ops) \ | |
47 | [_id] = &_ops, | |
48 | #include <linux/bpf_types.h> | |
49 | #undef BPF_PROG_TYPE | |
50 | #undef BPF_MAP_TYPE | |
51 | }; | |
99c55f7d | 52 | |
752ba56f MS |
53 | /* |
54 | * If we're handed a bigger struct than we know of, ensure all the unknown bits | |
55 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions | |
56 | * we don't know about yet. | |
57 | * | |
58 | * There is a ToCToU between this function call and the following | |
59 | * copy_from_user() call. However, this is not a concern since this function is | |
60 | * meant to be a future-proofing of bits. | |
61 | */ | |
dcab51f1 MKL |
62 | int bpf_check_uarg_tail_zero(void __user *uaddr, |
63 | size_t expected_size, | |
64 | size_t actual_size) | |
58291a74 MS |
65 | { |
66 | unsigned char __user *addr; | |
67 | unsigned char __user *end; | |
68 | unsigned char val; | |
69 | int err; | |
70 | ||
752ba56f MS |
71 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
72 | return -E2BIG; | |
73 | ||
96d4f267 | 74 | if (unlikely(!access_ok(uaddr, actual_size))) |
752ba56f MS |
75 | return -EFAULT; |
76 | ||
58291a74 MS |
77 | if (actual_size <= expected_size) |
78 | return 0; | |
79 | ||
80 | addr = uaddr + expected_size; | |
81 | end = uaddr + actual_size; | |
82 | ||
83 | for (; addr < end; addr++) { | |
84 | err = get_user(val, addr); | |
85 | if (err) | |
86 | return err; | |
87 | if (val) | |
88 | return -E2BIG; | |
89 | } | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
a3884572 JK |
94 | const struct bpf_map_ops bpf_map_offload_ops = { |
95 | .map_alloc = bpf_map_offload_map_alloc, | |
96 | .map_free = bpf_map_offload_map_free, | |
e8d2bec0 | 97 | .map_check_btf = map_check_no_btf, |
a3884572 JK |
98 | }; |
99 | ||
99c55f7d AS |
100 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
101 | { | |
1110f3a9 | 102 | const struct bpf_map_ops *ops; |
9ef09e35 | 103 | u32 type = attr->map_type; |
99c55f7d | 104 | struct bpf_map *map; |
1110f3a9 | 105 | int err; |
99c55f7d | 106 | |
9ef09e35 | 107 | if (type >= ARRAY_SIZE(bpf_map_types)) |
1110f3a9 | 108 | return ERR_PTR(-EINVAL); |
9ef09e35 MR |
109 | type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); |
110 | ops = bpf_map_types[type]; | |
1110f3a9 | 111 | if (!ops) |
40077e0c | 112 | return ERR_PTR(-EINVAL); |
99c55f7d | 113 | |
1110f3a9 JK |
114 | if (ops->map_alloc_check) { |
115 | err = ops->map_alloc_check(attr); | |
116 | if (err) | |
117 | return ERR_PTR(err); | |
118 | } | |
a3884572 JK |
119 | if (attr->map_ifindex) |
120 | ops = &bpf_map_offload_ops; | |
1110f3a9 | 121 | map = ops->map_alloc(attr); |
40077e0c JB |
122 | if (IS_ERR(map)) |
123 | return map; | |
1110f3a9 | 124 | map->ops = ops; |
9ef09e35 | 125 | map->map_type = type; |
40077e0c | 126 | return map; |
99c55f7d AS |
127 | } |
128 | ||
96eabe7a | 129 | void *bpf_map_area_alloc(size_t size, int numa_node) |
d407bd25 | 130 | { |
f01a7dbe MP |
131 | /* We really just want to fail instead of triggering OOM killer |
132 | * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, | |
133 | * which is used for lower order allocation requests. | |
134 | * | |
135 | * It has been observed that higher order allocation requests done by | |
136 | * vmalloc with __GFP_NORETRY being set might fail due to not trying | |
137 | * to reclaim memory from the page cache, thus we set | |
138 | * __GFP_RETRY_MAYFAIL to avoid such situations. | |
d407bd25 | 139 | */ |
f01a7dbe MP |
140 | |
141 | const gfp_t flags = __GFP_NOWARN | __GFP_ZERO; | |
d407bd25 DB |
142 | void *area; |
143 | ||
144 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { | |
f01a7dbe MP |
145 | area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags, |
146 | numa_node); | |
d407bd25 DB |
147 | if (area != NULL) |
148 | return area; | |
149 | } | |
150 | ||
f01a7dbe MP |
151 | return __vmalloc_node_flags_caller(size, numa_node, |
152 | GFP_KERNEL | __GFP_RETRY_MAYFAIL | | |
153 | flags, __builtin_return_address(0)); | |
d407bd25 DB |
154 | } |
155 | ||
156 | void bpf_map_area_free(void *area) | |
157 | { | |
158 | kvfree(area); | |
159 | } | |
160 | ||
be70bcd5 DB |
161 | static u32 bpf_map_flags_retain_permanent(u32 flags) |
162 | { | |
163 | /* Some map creation flags are not tied to the map object but | |
164 | * rather to the map fd instead, so they have no meaning upon | |
165 | * map object inspection since multiple file descriptors with | |
166 | * different (access) properties can exist here. Thus, given | |
167 | * this has zero meaning for the map itself, lets clear these | |
168 | * from here. | |
169 | */ | |
170 | return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); | |
171 | } | |
172 | ||
bd475643 JK |
173 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
174 | { | |
175 | map->map_type = attr->map_type; | |
176 | map->key_size = attr->key_size; | |
177 | map->value_size = attr->value_size; | |
178 | map->max_entries = attr->max_entries; | |
be70bcd5 | 179 | map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); |
bd475643 JK |
180 | map->numa_node = bpf_map_attr_numa_node(attr); |
181 | } | |
182 | ||
0a4c58f5 | 183 | static int bpf_charge_memlock(struct user_struct *user, u32 pages) |
aaac3ba9 | 184 | { |
0a4c58f5 | 185 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
aaac3ba9 | 186 | |
0a4c58f5 RG |
187 | if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) { |
188 | atomic_long_sub(pages, &user->locked_vm); | |
189 | return -EPERM; | |
190 | } | |
191 | return 0; | |
192 | } | |
aaac3ba9 | 193 | |
0a4c58f5 RG |
194 | static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) |
195 | { | |
b936ca64 RG |
196 | if (user) |
197 | atomic_long_sub(pages, &user->locked_vm); | |
0a4c58f5 | 198 | } |
aaac3ba9 | 199 | |
c85d6913 | 200 | int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size) |
0a4c58f5 | 201 | { |
c85d6913 RG |
202 | u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT; |
203 | struct user_struct *user; | |
0a4c58f5 | 204 | int ret; |
aaac3ba9 | 205 | |
c85d6913 RG |
206 | if (size >= U32_MAX - PAGE_SIZE) |
207 | return -E2BIG; | |
208 | ||
209 | user = get_current_user(); | |
b936ca64 | 210 | ret = bpf_charge_memlock(user, pages); |
0a4c58f5 | 211 | if (ret) { |
aaac3ba9 | 212 | free_uid(user); |
0a4c58f5 | 213 | return ret; |
aaac3ba9 | 214 | } |
b936ca64 RG |
215 | |
216 | mem->pages = pages; | |
217 | mem->user = user; | |
218 | ||
219 | return 0; | |
aaac3ba9 AS |
220 | } |
221 | ||
b936ca64 | 222 | void bpf_map_charge_finish(struct bpf_map_memory *mem) |
aaac3ba9 | 223 | { |
b936ca64 RG |
224 | bpf_uncharge_memlock(mem->user, mem->pages); |
225 | free_uid(mem->user); | |
226 | } | |
227 | ||
228 | void bpf_map_charge_move(struct bpf_map_memory *dst, | |
229 | struct bpf_map_memory *src) | |
230 | { | |
231 | *dst = *src; | |
3539b96e | 232 | |
b936ca64 RG |
233 | /* Make sure src will not be used for the redundant uncharging. */ |
234 | memset(src, 0, sizeof(struct bpf_map_memory)); | |
aaac3ba9 AS |
235 | } |
236 | ||
0a4c58f5 RG |
237 | int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) |
238 | { | |
239 | int ret; | |
240 | ||
3539b96e | 241 | ret = bpf_charge_memlock(map->memory.user, pages); |
0a4c58f5 RG |
242 | if (ret) |
243 | return ret; | |
3539b96e | 244 | map->memory.pages += pages; |
0a4c58f5 RG |
245 | return ret; |
246 | } | |
247 | ||
248 | void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) | |
249 | { | |
3539b96e RG |
250 | bpf_uncharge_memlock(map->memory.user, pages); |
251 | map->memory.pages -= pages; | |
0a4c58f5 RG |
252 | } |
253 | ||
f3f1c054 MKL |
254 | static int bpf_map_alloc_id(struct bpf_map *map) |
255 | { | |
256 | int id; | |
257 | ||
b76354cd | 258 | idr_preload(GFP_KERNEL); |
f3f1c054 MKL |
259 | spin_lock_bh(&map_idr_lock); |
260 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); | |
261 | if (id > 0) | |
262 | map->id = id; | |
263 | spin_unlock_bh(&map_idr_lock); | |
b76354cd | 264 | idr_preload_end(); |
f3f1c054 MKL |
265 | |
266 | if (WARN_ON_ONCE(!id)) | |
267 | return -ENOSPC; | |
268 | ||
269 | return id > 0 ? 0 : id; | |
270 | } | |
271 | ||
a3884572 | 272 | void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) |
f3f1c054 | 273 | { |
930651a7 ED |
274 | unsigned long flags; |
275 | ||
a3884572 JK |
276 | /* Offloaded maps are removed from the IDR store when their device |
277 | * disappears - even if someone holds an fd to them they are unusable, | |
278 | * the memory is gone, all ops will fail; they are simply waiting for | |
279 | * refcnt to drop to be freed. | |
280 | */ | |
281 | if (!map->id) | |
282 | return; | |
283 | ||
bd5f5f4e | 284 | if (do_idr_lock) |
930651a7 | 285 | spin_lock_irqsave(&map_idr_lock, flags); |
bd5f5f4e MKL |
286 | else |
287 | __acquire(&map_idr_lock); | |
288 | ||
f3f1c054 | 289 | idr_remove(&map_idr, map->id); |
a3884572 | 290 | map->id = 0; |
bd5f5f4e MKL |
291 | |
292 | if (do_idr_lock) | |
930651a7 | 293 | spin_unlock_irqrestore(&map_idr_lock, flags); |
bd5f5f4e MKL |
294 | else |
295 | __release(&map_idr_lock); | |
f3f1c054 MKL |
296 | } |
297 | ||
99c55f7d AS |
298 | /* called from workqueue */ |
299 | static void bpf_map_free_deferred(struct work_struct *work) | |
300 | { | |
301 | struct bpf_map *map = container_of(work, struct bpf_map, work); | |
b936ca64 | 302 | struct bpf_map_memory mem; |
99c55f7d | 303 | |
b936ca64 | 304 | bpf_map_charge_move(&mem, &map->memory); |
afdb09c7 | 305 | security_bpf_map_free(map); |
99c55f7d AS |
306 | /* implementation dependent freeing */ |
307 | map->ops->map_free(map); | |
b936ca64 | 308 | bpf_map_charge_finish(&mem); |
99c55f7d AS |
309 | } |
310 | ||
c9da161c DB |
311 | static void bpf_map_put_uref(struct bpf_map *map) |
312 | { | |
313 | if (atomic_dec_and_test(&map->usercnt)) { | |
ba6b8de4 JF |
314 | if (map->ops->map_release_uref) |
315 | map->ops->map_release_uref(map); | |
c9da161c DB |
316 | } |
317 | } | |
318 | ||
99c55f7d AS |
319 | /* decrement map refcnt and schedule it for freeing via workqueue |
320 | * (unrelying map implementation ops->map_free() might sleep) | |
321 | */ | |
bd5f5f4e | 322 | static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) |
99c55f7d AS |
323 | { |
324 | if (atomic_dec_and_test(&map->refcnt)) { | |
34ad5580 | 325 | /* bpf_map_free_id() must be called first */ |
bd5f5f4e | 326 | bpf_map_free_id(map, do_idr_lock); |
78958fca | 327 | btf_put(map->btf); |
99c55f7d AS |
328 | INIT_WORK(&map->work, bpf_map_free_deferred); |
329 | schedule_work(&map->work); | |
330 | } | |
331 | } | |
332 | ||
bd5f5f4e MKL |
333 | void bpf_map_put(struct bpf_map *map) |
334 | { | |
335 | __bpf_map_put(map, true); | |
336 | } | |
630a4d38 | 337 | EXPORT_SYMBOL_GPL(bpf_map_put); |
bd5f5f4e | 338 | |
c9da161c | 339 | void bpf_map_put_with_uref(struct bpf_map *map) |
99c55f7d | 340 | { |
c9da161c | 341 | bpf_map_put_uref(map); |
99c55f7d | 342 | bpf_map_put(map); |
c9da161c DB |
343 | } |
344 | ||
345 | static int bpf_map_release(struct inode *inode, struct file *filp) | |
346 | { | |
61d1b6a4 DB |
347 | struct bpf_map *map = filp->private_data; |
348 | ||
349 | if (map->ops->map_release) | |
350 | map->ops->map_release(map, filp); | |
351 | ||
352 | bpf_map_put_with_uref(map); | |
99c55f7d AS |
353 | return 0; |
354 | } | |
355 | ||
87df15de DB |
356 | static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) |
357 | { | |
358 | fmode_t mode = f.file->f_mode; | |
359 | ||
360 | /* Our file permissions may have been overridden by global | |
361 | * map permissions facing syscall side. | |
362 | */ | |
363 | if (READ_ONCE(map->frozen)) | |
364 | mode &= ~FMODE_CAN_WRITE; | |
365 | return mode; | |
366 | } | |
367 | ||
f99bf205 DB |
368 | #ifdef CONFIG_PROC_FS |
369 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) | |
370 | { | |
371 | const struct bpf_map *map = filp->private_data; | |
21116b70 DB |
372 | const struct bpf_array *array; |
373 | u32 owner_prog_type = 0; | |
9780c0ab | 374 | u32 owner_jited = 0; |
21116b70 DB |
375 | |
376 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { | |
377 | array = container_of(map, struct bpf_array, map); | |
378 | owner_prog_type = array->owner_prog_type; | |
9780c0ab | 379 | owner_jited = array->owner_jited; |
21116b70 | 380 | } |
f99bf205 DB |
381 | |
382 | seq_printf(m, | |
383 | "map_type:\t%u\n" | |
384 | "key_size:\t%u\n" | |
385 | "value_size:\t%u\n" | |
322cea2f | 386 | "max_entries:\t%u\n" |
21116b70 | 387 | "map_flags:\t%#x\n" |
4316b409 | 388 | "memlock:\t%llu\n" |
87df15de DB |
389 | "map_id:\t%u\n" |
390 | "frozen:\t%u\n", | |
f99bf205 DB |
391 | map->map_type, |
392 | map->key_size, | |
393 | map->value_size, | |
322cea2f | 394 | map->max_entries, |
21116b70 | 395 | map->map_flags, |
3539b96e | 396 | map->memory.pages * 1ULL << PAGE_SHIFT, |
87df15de DB |
397 | map->id, |
398 | READ_ONCE(map->frozen)); | |
21116b70 | 399 | |
9780c0ab | 400 | if (owner_prog_type) { |
21116b70 DB |
401 | seq_printf(m, "owner_prog_type:\t%u\n", |
402 | owner_prog_type); | |
9780c0ab DB |
403 | seq_printf(m, "owner_jited:\t%u\n", |
404 | owner_jited); | |
405 | } | |
f99bf205 DB |
406 | } |
407 | #endif | |
408 | ||
6e71b04a CF |
409 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
410 | loff_t *ppos) | |
411 | { | |
412 | /* We need this handler such that alloc_file() enables | |
413 | * f_mode with FMODE_CAN_READ. | |
414 | */ | |
415 | return -EINVAL; | |
416 | } | |
417 | ||
418 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, | |
419 | size_t siz, loff_t *ppos) | |
420 | { | |
421 | /* We need this handler such that alloc_file() enables | |
422 | * f_mode with FMODE_CAN_WRITE. | |
423 | */ | |
424 | return -EINVAL; | |
425 | } | |
426 | ||
f66e448c | 427 | const struct file_operations bpf_map_fops = { |
f99bf205 DB |
428 | #ifdef CONFIG_PROC_FS |
429 | .show_fdinfo = bpf_map_show_fdinfo, | |
430 | #endif | |
431 | .release = bpf_map_release, | |
6e71b04a CF |
432 | .read = bpf_dummy_read, |
433 | .write = bpf_dummy_write, | |
99c55f7d AS |
434 | }; |
435 | ||
6e71b04a | 436 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
aa79781b | 437 | { |
afdb09c7 CF |
438 | int ret; |
439 | ||
440 | ret = security_bpf_map(map, OPEN_FMODE(flags)); | |
441 | if (ret < 0) | |
442 | return ret; | |
443 | ||
aa79781b | 444 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
6e71b04a CF |
445 | flags | O_CLOEXEC); |
446 | } | |
447 | ||
448 | int bpf_get_file_flag(int flags) | |
449 | { | |
450 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) | |
451 | return -EINVAL; | |
452 | if (flags & BPF_F_RDONLY) | |
453 | return O_RDONLY; | |
454 | if (flags & BPF_F_WRONLY) | |
455 | return O_WRONLY; | |
456 | return O_RDWR; | |
aa79781b DB |
457 | } |
458 | ||
99c55f7d AS |
459 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
460 | #define CHECK_ATTR(CMD) \ | |
461 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ | |
462 | sizeof(attr->CMD##_LAST_FIELD), 0, \ | |
463 | sizeof(*attr) - \ | |
464 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ | |
465 | sizeof(attr->CMD##_LAST_FIELD)) != NULL | |
466 | ||
cb4d2b3f MKL |
467 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
468 | * Return 0 on success and < 0 on error. | |
469 | */ | |
470 | static int bpf_obj_name_cpy(char *dst, const char *src) | |
471 | { | |
472 | const char *end = src + BPF_OBJ_NAME_LEN; | |
473 | ||
473d9734 | 474 | memset(dst, 0, BPF_OBJ_NAME_LEN); |
3e0ddc4f | 475 | /* Copy all isalnum(), '_' and '.' chars. */ |
cb4d2b3f | 476 | while (src < end && *src) { |
3e0ddc4f DB |
477 | if (!isalnum(*src) && |
478 | *src != '_' && *src != '.') | |
cb4d2b3f MKL |
479 | return -EINVAL; |
480 | *dst++ = *src++; | |
481 | } | |
482 | ||
483 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ | |
484 | if (src == end) | |
485 | return -EINVAL; | |
486 | ||
cb4d2b3f MKL |
487 | return 0; |
488 | } | |
489 | ||
e8d2bec0 | 490 | int map_check_no_btf(const struct bpf_map *map, |
1b2b234b | 491 | const struct btf *btf, |
e8d2bec0 DB |
492 | const struct btf_type *key_type, |
493 | const struct btf_type *value_type) | |
494 | { | |
495 | return -ENOTSUPP; | |
496 | } | |
497 | ||
d83525ca | 498 | static int map_check_btf(struct bpf_map *map, const struct btf *btf, |
e8d2bec0 DB |
499 | u32 btf_key_id, u32 btf_value_id) |
500 | { | |
501 | const struct btf_type *key_type, *value_type; | |
502 | u32 key_size, value_size; | |
503 | int ret = 0; | |
504 | ||
2824ecb7 DB |
505 | /* Some maps allow key to be unspecified. */ |
506 | if (btf_key_id) { | |
507 | key_type = btf_type_id_size(btf, &btf_key_id, &key_size); | |
508 | if (!key_type || key_size != map->key_size) | |
509 | return -EINVAL; | |
510 | } else { | |
511 | key_type = btf_type_by_id(btf, 0); | |
512 | if (!map->ops->map_check_btf) | |
513 | return -EINVAL; | |
514 | } | |
e8d2bec0 DB |
515 | |
516 | value_type = btf_type_id_size(btf, &btf_value_id, &value_size); | |
517 | if (!value_type || value_size != map->value_size) | |
518 | return -EINVAL; | |
519 | ||
d83525ca AS |
520 | map->spin_lock_off = btf_find_spin_lock(btf, value_type); |
521 | ||
522 | if (map_value_has_spin_lock(map)) { | |
591fe988 DB |
523 | if (map->map_flags & BPF_F_RDONLY_PROG) |
524 | return -EACCES; | |
d83525ca | 525 | if (map->map_type != BPF_MAP_TYPE_HASH && |
e16d2f1a | 526 | map->map_type != BPF_MAP_TYPE_ARRAY && |
6ac99e8f MKL |
527 | map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
528 | map->map_type != BPF_MAP_TYPE_SK_STORAGE) | |
d83525ca AS |
529 | return -ENOTSUPP; |
530 | if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > | |
531 | map->value_size) { | |
532 | WARN_ONCE(1, | |
533 | "verifier bug spin_lock_off %d value_size %d\n", | |
534 | map->spin_lock_off, map->value_size); | |
535 | return -EFAULT; | |
536 | } | |
537 | } | |
538 | ||
e8d2bec0 | 539 | if (map->ops->map_check_btf) |
1b2b234b | 540 | ret = map->ops->map_check_btf(map, btf, key_type, value_type); |
e8d2bec0 DB |
541 | |
542 | return ret; | |
543 | } | |
544 | ||
9b2cf328 | 545 | #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id |
99c55f7d AS |
546 | /* called via syscall */ |
547 | static int map_create(union bpf_attr *attr) | |
548 | { | |
96eabe7a | 549 | int numa_node = bpf_map_attr_numa_node(attr); |
b936ca64 | 550 | struct bpf_map_memory mem; |
99c55f7d | 551 | struct bpf_map *map; |
6e71b04a | 552 | int f_flags; |
99c55f7d AS |
553 | int err; |
554 | ||
555 | err = CHECK_ATTR(BPF_MAP_CREATE); | |
556 | if (err) | |
557 | return -EINVAL; | |
558 | ||
6e71b04a CF |
559 | f_flags = bpf_get_file_flag(attr->map_flags); |
560 | if (f_flags < 0) | |
561 | return f_flags; | |
562 | ||
96eabe7a | 563 | if (numa_node != NUMA_NO_NODE && |
96e5ae4e ED |
564 | ((unsigned int)numa_node >= nr_node_ids || |
565 | !node_online(numa_node))) | |
96eabe7a MKL |
566 | return -EINVAL; |
567 | ||
99c55f7d AS |
568 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
569 | map = find_and_alloc_map(attr); | |
570 | if (IS_ERR(map)) | |
571 | return PTR_ERR(map); | |
572 | ||
ad5b177b MKL |
573 | err = bpf_obj_name_cpy(map->name, attr->map_name); |
574 | if (err) | |
b936ca64 | 575 | goto free_map; |
ad5b177b | 576 | |
99c55f7d | 577 | atomic_set(&map->refcnt, 1); |
c9da161c | 578 | atomic_set(&map->usercnt, 1); |
99c55f7d | 579 | |
e8d2bec0 | 580 | if (attr->btf_key_type_id || attr->btf_value_type_id) { |
a26ca7c9 MKL |
581 | struct btf *btf; |
582 | ||
2824ecb7 | 583 | if (!attr->btf_value_type_id) { |
a26ca7c9 | 584 | err = -EINVAL; |
b936ca64 | 585 | goto free_map; |
a26ca7c9 MKL |
586 | } |
587 | ||
588 | btf = btf_get_by_fd(attr->btf_fd); | |
589 | if (IS_ERR(btf)) { | |
590 | err = PTR_ERR(btf); | |
b936ca64 | 591 | goto free_map; |
a26ca7c9 MKL |
592 | } |
593 | ||
e8d2bec0 DB |
594 | err = map_check_btf(map, btf, attr->btf_key_type_id, |
595 | attr->btf_value_type_id); | |
a26ca7c9 MKL |
596 | if (err) { |
597 | btf_put(btf); | |
b936ca64 | 598 | goto free_map; |
a26ca7c9 MKL |
599 | } |
600 | ||
601 | map->btf = btf; | |
9b2cf328 MKL |
602 | map->btf_key_type_id = attr->btf_key_type_id; |
603 | map->btf_value_type_id = attr->btf_value_type_id; | |
d83525ca AS |
604 | } else { |
605 | map->spin_lock_off = -EINVAL; | |
a26ca7c9 MKL |
606 | } |
607 | ||
afdb09c7 | 608 | err = security_bpf_map_alloc(map); |
aaac3ba9 | 609 | if (err) |
b936ca64 | 610 | goto free_map; |
afdb09c7 | 611 | |
f3f1c054 MKL |
612 | err = bpf_map_alloc_id(map); |
613 | if (err) | |
b936ca64 | 614 | goto free_map_sec; |
f3f1c054 | 615 | |
6e71b04a | 616 | err = bpf_map_new_fd(map, f_flags); |
bd5f5f4e MKL |
617 | if (err < 0) { |
618 | /* failed to allocate fd. | |
352d20d6 | 619 | * bpf_map_put_with_uref() is needed because the above |
bd5f5f4e MKL |
620 | * bpf_map_alloc_id() has published the map |
621 | * to the userspace and the userspace may | |
622 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. | |
623 | */ | |
352d20d6 | 624 | bpf_map_put_with_uref(map); |
bd5f5f4e MKL |
625 | return err; |
626 | } | |
99c55f7d AS |
627 | |
628 | return err; | |
629 | ||
afdb09c7 CF |
630 | free_map_sec: |
631 | security_bpf_map_free(map); | |
b936ca64 | 632 | free_map: |
a26ca7c9 | 633 | btf_put(map->btf); |
b936ca64 | 634 | bpf_map_charge_move(&mem, &map->memory); |
99c55f7d | 635 | map->ops->map_free(map); |
b936ca64 | 636 | bpf_map_charge_finish(&mem); |
99c55f7d AS |
637 | return err; |
638 | } | |
639 | ||
db20fd2b AS |
640 | /* if error is returned, fd is released. |
641 | * On success caller should complete fd access with matching fdput() | |
642 | */ | |
c2101297 | 643 | struct bpf_map *__bpf_map_get(struct fd f) |
db20fd2b | 644 | { |
db20fd2b AS |
645 | if (!f.file) |
646 | return ERR_PTR(-EBADF); | |
db20fd2b AS |
647 | if (f.file->f_op != &bpf_map_fops) { |
648 | fdput(f); | |
649 | return ERR_PTR(-EINVAL); | |
650 | } | |
651 | ||
c2101297 DB |
652 | return f.file->private_data; |
653 | } | |
654 | ||
92117d84 AS |
655 | /* prog's and map's refcnt limit */ |
656 | #define BPF_MAX_REFCNT 32768 | |
657 | ||
658 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) | |
c9da161c | 659 | { |
92117d84 AS |
660 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
661 | atomic_dec(&map->refcnt); | |
662 | return ERR_PTR(-EBUSY); | |
663 | } | |
c9da161c DB |
664 | if (uref) |
665 | atomic_inc(&map->usercnt); | |
92117d84 | 666 | return map; |
c9da161c | 667 | } |
630a4d38 | 668 | EXPORT_SYMBOL_GPL(bpf_map_inc); |
c9da161c DB |
669 | |
670 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) | |
c2101297 DB |
671 | { |
672 | struct fd f = fdget(ufd); | |
673 | struct bpf_map *map; | |
674 | ||
675 | map = __bpf_map_get(f); | |
676 | if (IS_ERR(map)) | |
677 | return map; | |
678 | ||
92117d84 | 679 | map = bpf_map_inc(map, true); |
c2101297 | 680 | fdput(f); |
db20fd2b AS |
681 | |
682 | return map; | |
683 | } | |
684 | ||
bd5f5f4e | 685 | /* map_idr_lock should have been held */ |
b0e4701c SF |
686 | static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, |
687 | bool uref) | |
bd5f5f4e MKL |
688 | { |
689 | int refold; | |
690 | ||
bfc18e38 | 691 | refold = atomic_fetch_add_unless(&map->refcnt, 1, 0); |
bd5f5f4e MKL |
692 | |
693 | if (refold >= BPF_MAX_REFCNT) { | |
694 | __bpf_map_put(map, false); | |
695 | return ERR_PTR(-EBUSY); | |
696 | } | |
697 | ||
698 | if (!refold) | |
699 | return ERR_PTR(-ENOENT); | |
700 | ||
701 | if (uref) | |
702 | atomic_inc(&map->usercnt); | |
703 | ||
704 | return map; | |
705 | } | |
706 | ||
b0e4701c SF |
707 | struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, bool uref) |
708 | { | |
709 | spin_lock_bh(&map_idr_lock); | |
710 | map = __bpf_map_inc_not_zero(map, uref); | |
711 | spin_unlock_bh(&map_idr_lock); | |
712 | ||
713 | return map; | |
714 | } | |
715 | EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); | |
716 | ||
b8cdc051 AS |
717 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
718 | { | |
719 | return -ENOTSUPP; | |
720 | } | |
721 | ||
c9d29f46 MV |
722 | static void *__bpf_copy_key(void __user *ukey, u64 key_size) |
723 | { | |
724 | if (key_size) | |
725 | return memdup_user(ukey, key_size); | |
726 | ||
727 | if (ukey) | |
728 | return ERR_PTR(-EINVAL); | |
729 | ||
730 | return NULL; | |
731 | } | |
732 | ||
db20fd2b | 733 | /* last field in 'union bpf_attr' used by this command */ |
96049f3a | 734 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags |
db20fd2b AS |
735 | |
736 | static int map_lookup_elem(union bpf_attr *attr) | |
737 | { | |
535e7b4b MS |
738 | void __user *ukey = u64_to_user_ptr(attr->key); |
739 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 740 | int ufd = attr->map_fd; |
db20fd2b | 741 | struct bpf_map *map; |
8ebe667c | 742 | void *key, *value, *ptr; |
15a07b33 | 743 | u32 value_size; |
592867bf | 744 | struct fd f; |
db20fd2b AS |
745 | int err; |
746 | ||
747 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) | |
748 | return -EINVAL; | |
749 | ||
96049f3a AS |
750 | if (attr->flags & ~BPF_F_LOCK) |
751 | return -EINVAL; | |
752 | ||
592867bf | 753 | f = fdget(ufd); |
c2101297 | 754 | map = __bpf_map_get(f); |
db20fd2b AS |
755 | if (IS_ERR(map)) |
756 | return PTR_ERR(map); | |
87df15de | 757 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
6e71b04a CF |
758 | err = -EPERM; |
759 | goto err_put; | |
760 | } | |
761 | ||
96049f3a AS |
762 | if ((attr->flags & BPF_F_LOCK) && |
763 | !map_value_has_spin_lock(map)) { | |
764 | err = -EINVAL; | |
765 | goto err_put; | |
766 | } | |
767 | ||
c9d29f46 | 768 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
769 | if (IS_ERR(key)) { |
770 | err = PTR_ERR(key); | |
db20fd2b | 771 | goto err_put; |
e4448ed8 | 772 | } |
db20fd2b | 773 | |
15a07b33 | 774 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 775 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
b741f163 RG |
776 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
777 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
15a07b33 | 778 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
14dc6f04 MKL |
779 | else if (IS_FD_MAP(map)) |
780 | value_size = sizeof(u32); | |
15a07b33 AS |
781 | else |
782 | value_size = map->value_size; | |
783 | ||
8ebe667c | 784 | err = -ENOMEM; |
15a07b33 | 785 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b | 786 | if (!value) |
8ebe667c AS |
787 | goto free_key; |
788 | ||
a3884572 JK |
789 | if (bpf_map_is_dev_bound(map)) { |
790 | err = bpf_map_offload_lookup_elem(map, key, value); | |
7c4cd051 MKL |
791 | goto done; |
792 | } | |
793 | ||
794 | preempt_disable(); | |
795 | this_cpu_inc(bpf_prog_active); | |
796 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || | |
797 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
798 | err = bpf_percpu_hash_copy(map, key, value); |
799 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
800 | err = bpf_percpu_array_copy(map, key, value); | |
b741f163 RG |
801 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
802 | err = bpf_percpu_cgroup_storage_copy(map, key, value); | |
557c0c6e AS |
803 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
804 | err = bpf_stackmap_copy(map, key, value); | |
14dc6f04 MKL |
805 | } else if (IS_FD_ARRAY(map)) { |
806 | err = bpf_fd_array_map_lookup_elem(map, key, value); | |
807 | } else if (IS_FD_HASH(map)) { | |
808 | err = bpf_fd_htab_map_lookup_elem(map, key, value); | |
5dc4c4b7 MKL |
809 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
810 | err = bpf_fd_reuseport_array_lookup_elem(map, key, value); | |
f1a2e44a MV |
811 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
812 | map->map_type == BPF_MAP_TYPE_STACK) { | |
813 | err = map->ops->map_peek_elem(map, value); | |
15a07b33 AS |
814 | } else { |
815 | rcu_read_lock(); | |
c6110222 DB |
816 | if (map->ops->map_lookup_elem_sys_only) |
817 | ptr = map->ops->map_lookup_elem_sys_only(map, key); | |
818 | else | |
819 | ptr = map->ops->map_lookup_elem(map, key); | |
509db283 PB |
820 | if (IS_ERR(ptr)) { |
821 | err = PTR_ERR(ptr); | |
822 | } else if (!ptr) { | |
823 | err = -ENOENT; | |
824 | } else { | |
825 | err = 0; | |
96049f3a AS |
826 | if (attr->flags & BPF_F_LOCK) |
827 | /* lock 'ptr' and copy everything but lock */ | |
828 | copy_map_value_locked(map, value, ptr, true); | |
829 | else | |
830 | copy_map_value(map, value, ptr); | |
831 | /* mask lock, since value wasn't zero inited */ | |
832 | check_and_init_map_lock(map, value); | |
509db283 | 833 | } |
15a07b33 | 834 | rcu_read_unlock(); |
15a07b33 | 835 | } |
7c4cd051 MKL |
836 | this_cpu_dec(bpf_prog_active); |
837 | preempt_enable(); | |
8ebe667c | 838 | |
7c4cd051 | 839 | done: |
15a07b33 | 840 | if (err) |
8ebe667c | 841 | goto free_value; |
db20fd2b AS |
842 | |
843 | err = -EFAULT; | |
15a07b33 | 844 | if (copy_to_user(uvalue, value, value_size) != 0) |
8ebe667c | 845 | goto free_value; |
db20fd2b AS |
846 | |
847 | err = 0; | |
848 | ||
8ebe667c AS |
849 | free_value: |
850 | kfree(value); | |
db20fd2b AS |
851 | free_key: |
852 | kfree(key); | |
853 | err_put: | |
854 | fdput(f); | |
855 | return err; | |
856 | } | |
857 | ||
1ae80cf3 DC |
858 | static void maybe_wait_bpf_programs(struct bpf_map *map) |
859 | { | |
860 | /* Wait for any running BPF programs to complete so that | |
861 | * userspace, when we return to it, knows that all programs | |
862 | * that could be running use the new map value. | |
863 | */ | |
864 | if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || | |
865 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) | |
866 | synchronize_rcu(); | |
867 | } | |
868 | ||
3274f520 | 869 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
db20fd2b AS |
870 | |
871 | static int map_update_elem(union bpf_attr *attr) | |
872 | { | |
535e7b4b MS |
873 | void __user *ukey = u64_to_user_ptr(attr->key); |
874 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 875 | int ufd = attr->map_fd; |
db20fd2b AS |
876 | struct bpf_map *map; |
877 | void *key, *value; | |
15a07b33 | 878 | u32 value_size; |
592867bf | 879 | struct fd f; |
db20fd2b AS |
880 | int err; |
881 | ||
882 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) | |
883 | return -EINVAL; | |
884 | ||
592867bf | 885 | f = fdget(ufd); |
c2101297 | 886 | map = __bpf_map_get(f); |
db20fd2b AS |
887 | if (IS_ERR(map)) |
888 | return PTR_ERR(map); | |
87df15de | 889 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
6e71b04a CF |
890 | err = -EPERM; |
891 | goto err_put; | |
892 | } | |
893 | ||
96049f3a AS |
894 | if ((attr->flags & BPF_F_LOCK) && |
895 | !map_value_has_spin_lock(map)) { | |
896 | err = -EINVAL; | |
897 | goto err_put; | |
898 | } | |
899 | ||
c9d29f46 | 900 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
901 | if (IS_ERR(key)) { |
902 | err = PTR_ERR(key); | |
db20fd2b | 903 | goto err_put; |
e4448ed8 | 904 | } |
db20fd2b | 905 | |
15a07b33 | 906 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
8f844938 | 907 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
b741f163 RG |
908 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
909 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
15a07b33 AS |
910 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
911 | else | |
912 | value_size = map->value_size; | |
913 | ||
db20fd2b | 914 | err = -ENOMEM; |
15a07b33 | 915 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b AS |
916 | if (!value) |
917 | goto free_key; | |
918 | ||
919 | err = -EFAULT; | |
15a07b33 | 920 | if (copy_from_user(value, uvalue, value_size) != 0) |
db20fd2b AS |
921 | goto free_value; |
922 | ||
6710e112 | 923 | /* Need to create a kthread, thus must support schedule */ |
a3884572 JK |
924 | if (bpf_map_is_dev_bound(map)) { |
925 | err = bpf_map_offload_update_elem(map, key, value, attr->flags); | |
926 | goto out; | |
99ba2b5a JF |
927 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || |
928 | map->map_type == BPF_MAP_TYPE_SOCKHASH || | |
929 | map->map_type == BPF_MAP_TYPE_SOCKMAP) { | |
6710e112 JDB |
930 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
931 | goto out; | |
932 | } | |
933 | ||
b121d1e7 AS |
934 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
935 | * inside bpf map update or delete otherwise deadlocks are possible | |
936 | */ | |
937 | preempt_disable(); | |
938 | __this_cpu_inc(bpf_prog_active); | |
8f844938 MKL |
939 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
940 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
15a07b33 AS |
941 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
942 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
943 | err = bpf_percpu_array_update(map, key, value, attr->flags); | |
b741f163 RG |
944 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
945 | err = bpf_percpu_cgroup_storage_update(map, key, value, | |
946 | attr->flags); | |
9c147b56 | 947 | } else if (IS_FD_ARRAY(map)) { |
d056a788 DB |
948 | rcu_read_lock(); |
949 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, | |
950 | attr->flags); | |
951 | rcu_read_unlock(); | |
bcc6b1b7 MKL |
952 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
953 | rcu_read_lock(); | |
954 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, | |
955 | attr->flags); | |
956 | rcu_read_unlock(); | |
5dc4c4b7 MKL |
957 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
958 | /* rcu_read_lock() is not needed */ | |
959 | err = bpf_fd_reuseport_array_update_elem(map, key, value, | |
960 | attr->flags); | |
f1a2e44a MV |
961 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
962 | map->map_type == BPF_MAP_TYPE_STACK) { | |
963 | err = map->ops->map_push_elem(map, value, attr->flags); | |
15a07b33 AS |
964 | } else { |
965 | rcu_read_lock(); | |
966 | err = map->ops->map_update_elem(map, key, value, attr->flags); | |
967 | rcu_read_unlock(); | |
968 | } | |
b121d1e7 AS |
969 | __this_cpu_dec(bpf_prog_active); |
970 | preempt_enable(); | |
1ae80cf3 | 971 | maybe_wait_bpf_programs(map); |
6710e112 | 972 | out: |
db20fd2b AS |
973 | free_value: |
974 | kfree(value); | |
975 | free_key: | |
976 | kfree(key); | |
977 | err_put: | |
978 | fdput(f); | |
979 | return err; | |
980 | } | |
981 | ||
982 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key | |
983 | ||
984 | static int map_delete_elem(union bpf_attr *attr) | |
985 | { | |
535e7b4b | 986 | void __user *ukey = u64_to_user_ptr(attr->key); |
db20fd2b | 987 | int ufd = attr->map_fd; |
db20fd2b | 988 | struct bpf_map *map; |
592867bf | 989 | struct fd f; |
db20fd2b AS |
990 | void *key; |
991 | int err; | |
992 | ||
993 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) | |
994 | return -EINVAL; | |
995 | ||
592867bf | 996 | f = fdget(ufd); |
c2101297 | 997 | map = __bpf_map_get(f); |
db20fd2b AS |
998 | if (IS_ERR(map)) |
999 | return PTR_ERR(map); | |
87df15de | 1000 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
6e71b04a CF |
1001 | err = -EPERM; |
1002 | goto err_put; | |
1003 | } | |
1004 | ||
c9d29f46 | 1005 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
1006 | if (IS_ERR(key)) { |
1007 | err = PTR_ERR(key); | |
db20fd2b | 1008 | goto err_put; |
e4448ed8 | 1009 | } |
db20fd2b | 1010 | |
a3884572 JK |
1011 | if (bpf_map_is_dev_bound(map)) { |
1012 | err = bpf_map_offload_delete_elem(map, key); | |
1013 | goto out; | |
1014 | } | |
1015 | ||
b121d1e7 AS |
1016 | preempt_disable(); |
1017 | __this_cpu_inc(bpf_prog_active); | |
db20fd2b AS |
1018 | rcu_read_lock(); |
1019 | err = map->ops->map_delete_elem(map, key); | |
1020 | rcu_read_unlock(); | |
b121d1e7 AS |
1021 | __this_cpu_dec(bpf_prog_active); |
1022 | preempt_enable(); | |
1ae80cf3 | 1023 | maybe_wait_bpf_programs(map); |
a3884572 | 1024 | out: |
db20fd2b AS |
1025 | kfree(key); |
1026 | err_put: | |
1027 | fdput(f); | |
1028 | return err; | |
1029 | } | |
1030 | ||
1031 | /* last field in 'union bpf_attr' used by this command */ | |
1032 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key | |
1033 | ||
1034 | static int map_get_next_key(union bpf_attr *attr) | |
1035 | { | |
535e7b4b MS |
1036 | void __user *ukey = u64_to_user_ptr(attr->key); |
1037 | void __user *unext_key = u64_to_user_ptr(attr->next_key); | |
db20fd2b | 1038 | int ufd = attr->map_fd; |
db20fd2b AS |
1039 | struct bpf_map *map; |
1040 | void *key, *next_key; | |
592867bf | 1041 | struct fd f; |
db20fd2b AS |
1042 | int err; |
1043 | ||
1044 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) | |
1045 | return -EINVAL; | |
1046 | ||
592867bf | 1047 | f = fdget(ufd); |
c2101297 | 1048 | map = __bpf_map_get(f); |
db20fd2b AS |
1049 | if (IS_ERR(map)) |
1050 | return PTR_ERR(map); | |
87df15de | 1051 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
6e71b04a CF |
1052 | err = -EPERM; |
1053 | goto err_put; | |
1054 | } | |
1055 | ||
8fe45924 | 1056 | if (ukey) { |
c9d29f46 | 1057 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
1058 | if (IS_ERR(key)) { |
1059 | err = PTR_ERR(key); | |
8fe45924 | 1060 | goto err_put; |
e4448ed8 | 1061 | } |
8fe45924 TQ |
1062 | } else { |
1063 | key = NULL; | |
1064 | } | |
db20fd2b AS |
1065 | |
1066 | err = -ENOMEM; | |
1067 | next_key = kmalloc(map->key_size, GFP_USER); | |
1068 | if (!next_key) | |
1069 | goto free_key; | |
1070 | ||
a3884572 JK |
1071 | if (bpf_map_is_dev_bound(map)) { |
1072 | err = bpf_map_offload_get_next_key(map, key, next_key); | |
1073 | goto out; | |
1074 | } | |
1075 | ||
db20fd2b AS |
1076 | rcu_read_lock(); |
1077 | err = map->ops->map_get_next_key(map, key, next_key); | |
1078 | rcu_read_unlock(); | |
a3884572 | 1079 | out: |
db20fd2b AS |
1080 | if (err) |
1081 | goto free_next_key; | |
1082 | ||
1083 | err = -EFAULT; | |
1084 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) | |
1085 | goto free_next_key; | |
1086 | ||
1087 | err = 0; | |
1088 | ||
1089 | free_next_key: | |
1090 | kfree(next_key); | |
1091 | free_key: | |
1092 | kfree(key); | |
1093 | err_put: | |
1094 | fdput(f); | |
1095 | return err; | |
1096 | } | |
1097 | ||
bd513cd0 MV |
1098 | #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value |
1099 | ||
1100 | static int map_lookup_and_delete_elem(union bpf_attr *attr) | |
1101 | { | |
1102 | void __user *ukey = u64_to_user_ptr(attr->key); | |
1103 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
1104 | int ufd = attr->map_fd; | |
1105 | struct bpf_map *map; | |
540fefc0 | 1106 | void *key, *value; |
bd513cd0 MV |
1107 | u32 value_size; |
1108 | struct fd f; | |
1109 | int err; | |
1110 | ||
1111 | if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) | |
1112 | return -EINVAL; | |
1113 | ||
1114 | f = fdget(ufd); | |
1115 | map = __bpf_map_get(f); | |
1116 | if (IS_ERR(map)) | |
1117 | return PTR_ERR(map); | |
87df15de | 1118 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
bd513cd0 MV |
1119 | err = -EPERM; |
1120 | goto err_put; | |
1121 | } | |
1122 | ||
1123 | key = __bpf_copy_key(ukey, map->key_size); | |
1124 | if (IS_ERR(key)) { | |
1125 | err = PTR_ERR(key); | |
1126 | goto err_put; | |
1127 | } | |
1128 | ||
1129 | value_size = map->value_size; | |
1130 | ||
1131 | err = -ENOMEM; | |
1132 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); | |
1133 | if (!value) | |
1134 | goto free_key; | |
1135 | ||
1136 | if (map->map_type == BPF_MAP_TYPE_QUEUE || | |
1137 | map->map_type == BPF_MAP_TYPE_STACK) { | |
1138 | err = map->ops->map_pop_elem(map, value); | |
1139 | } else { | |
1140 | err = -ENOTSUPP; | |
1141 | } | |
1142 | ||
1143 | if (err) | |
1144 | goto free_value; | |
1145 | ||
1146 | if (copy_to_user(uvalue, value, value_size) != 0) | |
1147 | goto free_value; | |
1148 | ||
1149 | err = 0; | |
1150 | ||
1151 | free_value: | |
1152 | kfree(value); | |
1153 | free_key: | |
1154 | kfree(key); | |
1155 | err_put: | |
1156 | fdput(f); | |
1157 | return err; | |
1158 | } | |
1159 | ||
87df15de DB |
1160 | #define BPF_MAP_FREEZE_LAST_FIELD map_fd |
1161 | ||
1162 | static int map_freeze(const union bpf_attr *attr) | |
1163 | { | |
1164 | int err = 0, ufd = attr->map_fd; | |
1165 | struct bpf_map *map; | |
1166 | struct fd f; | |
1167 | ||
1168 | if (CHECK_ATTR(BPF_MAP_FREEZE)) | |
1169 | return -EINVAL; | |
1170 | ||
1171 | f = fdget(ufd); | |
1172 | map = __bpf_map_get(f); | |
1173 | if (IS_ERR(map)) | |
1174 | return PTR_ERR(map); | |
1175 | if (READ_ONCE(map->frozen)) { | |
1176 | err = -EBUSY; | |
1177 | goto err_put; | |
1178 | } | |
1179 | if (!capable(CAP_SYS_ADMIN)) { | |
1180 | err = -EPERM; | |
1181 | goto err_put; | |
1182 | } | |
1183 | ||
1184 | WRITE_ONCE(map->frozen, true); | |
1185 | err_put: | |
1186 | fdput(f); | |
1187 | return err; | |
1188 | } | |
1189 | ||
7de16e3a JK |
1190 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
1191 | #define BPF_PROG_TYPE(_id, _name) \ | |
1192 | [_id] = & _name ## _prog_ops, | |
1193 | #define BPF_MAP_TYPE(_id, _ops) | |
1194 | #include <linux/bpf_types.h> | |
1195 | #undef BPF_PROG_TYPE | |
1196 | #undef BPF_MAP_TYPE | |
1197 | }; | |
1198 | ||
09756af4 AS |
1199 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
1200 | { | |
d0f1a451 DB |
1201 | const struct bpf_prog_ops *ops; |
1202 | ||
1203 | if (type >= ARRAY_SIZE(bpf_prog_types)) | |
1204 | return -EINVAL; | |
1205 | type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); | |
1206 | ops = bpf_prog_types[type]; | |
1207 | if (!ops) | |
be9370a7 | 1208 | return -EINVAL; |
09756af4 | 1209 | |
ab3f0063 | 1210 | if (!bpf_prog_is_dev_bound(prog->aux)) |
d0f1a451 | 1211 | prog->aux->ops = ops; |
ab3f0063 JK |
1212 | else |
1213 | prog->aux->ops = &bpf_offload_prog_ops; | |
be9370a7 JB |
1214 | prog->type = type; |
1215 | return 0; | |
09756af4 AS |
1216 | } |
1217 | ||
1218 | /* drop refcnt on maps used by eBPF program and free auxilary data */ | |
1219 | static void free_used_maps(struct bpf_prog_aux *aux) | |
1220 | { | |
8bad74f9 | 1221 | enum bpf_cgroup_storage_type stype; |
09756af4 AS |
1222 | int i; |
1223 | ||
8bad74f9 RG |
1224 | for_each_cgroup_storage_type(stype) { |
1225 | if (!aux->cgroup_storage[stype]) | |
1226 | continue; | |
1227 | bpf_cgroup_storage_release(aux->prog, | |
1228 | aux->cgroup_storage[stype]); | |
1229 | } | |
de9cbbaa | 1230 | |
09756af4 AS |
1231 | for (i = 0; i < aux->used_map_cnt; i++) |
1232 | bpf_map_put(aux->used_maps[i]); | |
1233 | ||
1234 | kfree(aux->used_maps); | |
1235 | } | |
1236 | ||
5ccb071e DB |
1237 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
1238 | { | |
1239 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
1240 | unsigned long user_bufs; | |
1241 | ||
1242 | if (user) { | |
1243 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); | |
1244 | if (user_bufs > memlock_limit) { | |
1245 | atomic_long_sub(pages, &user->locked_vm); | |
1246 | return -EPERM; | |
1247 | } | |
1248 | } | |
1249 | ||
1250 | return 0; | |
1251 | } | |
1252 | ||
1253 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) | |
1254 | { | |
1255 | if (user) | |
1256 | atomic_long_sub(pages, &user->locked_vm); | |
1257 | } | |
1258 | ||
aaac3ba9 AS |
1259 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
1260 | { | |
1261 | struct user_struct *user = get_current_user(); | |
5ccb071e | 1262 | int ret; |
aaac3ba9 | 1263 | |
5ccb071e DB |
1264 | ret = __bpf_prog_charge(user, prog->pages); |
1265 | if (ret) { | |
aaac3ba9 | 1266 | free_uid(user); |
5ccb071e | 1267 | return ret; |
aaac3ba9 | 1268 | } |
5ccb071e | 1269 | |
aaac3ba9 AS |
1270 | prog->aux->user = user; |
1271 | return 0; | |
1272 | } | |
1273 | ||
1274 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) | |
1275 | { | |
1276 | struct user_struct *user = prog->aux->user; | |
1277 | ||
5ccb071e | 1278 | __bpf_prog_uncharge(user, prog->pages); |
aaac3ba9 AS |
1279 | free_uid(user); |
1280 | } | |
1281 | ||
dc4bb0e2 MKL |
1282 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
1283 | { | |
1284 | int id; | |
1285 | ||
b76354cd | 1286 | idr_preload(GFP_KERNEL); |
dc4bb0e2 MKL |
1287 | spin_lock_bh(&prog_idr_lock); |
1288 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); | |
1289 | if (id > 0) | |
1290 | prog->aux->id = id; | |
1291 | spin_unlock_bh(&prog_idr_lock); | |
b76354cd | 1292 | idr_preload_end(); |
dc4bb0e2 MKL |
1293 | |
1294 | /* id is in [1, INT_MAX) */ | |
1295 | if (WARN_ON_ONCE(!id)) | |
1296 | return -ENOSPC; | |
1297 | ||
1298 | return id > 0 ? 0 : id; | |
1299 | } | |
1300 | ||
ad8ad79f | 1301 | void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) |
dc4bb0e2 | 1302 | { |
ad8ad79f JK |
1303 | /* cBPF to eBPF migrations are currently not in the idr store. |
1304 | * Offloaded programs are removed from the store when their device | |
1305 | * disappears - even if someone grabs an fd to them they are unusable, | |
1306 | * simply waiting for refcnt to drop to be freed. | |
1307 | */ | |
dc4bb0e2 MKL |
1308 | if (!prog->aux->id) |
1309 | return; | |
1310 | ||
b16d9aa4 MKL |
1311 | if (do_idr_lock) |
1312 | spin_lock_bh(&prog_idr_lock); | |
1313 | else | |
1314 | __acquire(&prog_idr_lock); | |
1315 | ||
dc4bb0e2 | 1316 | idr_remove(&prog_idr, prog->aux->id); |
ad8ad79f | 1317 | prog->aux->id = 0; |
b16d9aa4 MKL |
1318 | |
1319 | if (do_idr_lock) | |
1320 | spin_unlock_bh(&prog_idr_lock); | |
1321 | else | |
1322 | __release(&prog_idr_lock); | |
dc4bb0e2 MKL |
1323 | } |
1324 | ||
1aacde3d | 1325 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
abf2e7d6 AS |
1326 | { |
1327 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); | |
1328 | ||
1329 | free_used_maps(aux); | |
aaac3ba9 | 1330 | bpf_prog_uncharge_memlock(aux->prog); |
afdb09c7 | 1331 | security_bpf_prog_free(aux); |
abf2e7d6 AS |
1332 | bpf_prog_free(aux->prog); |
1333 | } | |
1334 | ||
b16d9aa4 | 1335 | static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) |
09756af4 | 1336 | { |
a67edbf4 | 1337 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
6ee52e2a | 1338 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); |
34ad5580 | 1339 | /* bpf_prog_free_id() must be called first */ |
b16d9aa4 | 1340 | bpf_prog_free_id(prog, do_idr_lock); |
7d1982b4 | 1341 | bpf_prog_kallsyms_del_all(prog); |
838e9690 | 1342 | btf_put(prog->aux->btf); |
ba64e7d8 | 1343 | kvfree(prog->aux->func_info); |
c454a46b | 1344 | bpf_prog_free_linfo(prog); |
4f74d809 | 1345 | |
1aacde3d | 1346 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
a67edbf4 | 1347 | } |
09756af4 | 1348 | } |
b16d9aa4 MKL |
1349 | |
1350 | void bpf_prog_put(struct bpf_prog *prog) | |
1351 | { | |
1352 | __bpf_prog_put(prog, true); | |
1353 | } | |
e2e9b654 | 1354 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
09756af4 AS |
1355 | |
1356 | static int bpf_prog_release(struct inode *inode, struct file *filp) | |
1357 | { | |
1358 | struct bpf_prog *prog = filp->private_data; | |
1359 | ||
1aacde3d | 1360 | bpf_prog_put(prog); |
09756af4 AS |
1361 | return 0; |
1362 | } | |
1363 | ||
492ecee8 AS |
1364 | static void bpf_prog_get_stats(const struct bpf_prog *prog, |
1365 | struct bpf_prog_stats *stats) | |
1366 | { | |
1367 | u64 nsecs = 0, cnt = 0; | |
1368 | int cpu; | |
1369 | ||
1370 | for_each_possible_cpu(cpu) { | |
1371 | const struct bpf_prog_stats *st; | |
1372 | unsigned int start; | |
1373 | u64 tnsecs, tcnt; | |
1374 | ||
1375 | st = per_cpu_ptr(prog->aux->stats, cpu); | |
1376 | do { | |
1377 | start = u64_stats_fetch_begin_irq(&st->syncp); | |
1378 | tnsecs = st->nsecs; | |
1379 | tcnt = st->cnt; | |
1380 | } while (u64_stats_fetch_retry_irq(&st->syncp, start)); | |
1381 | nsecs += tnsecs; | |
1382 | cnt += tcnt; | |
1383 | } | |
1384 | stats->nsecs = nsecs; | |
1385 | stats->cnt = cnt; | |
1386 | } | |
1387 | ||
7bd509e3 DB |
1388 | #ifdef CONFIG_PROC_FS |
1389 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) | |
1390 | { | |
1391 | const struct bpf_prog *prog = filp->private_data; | |
f1f7714e | 1392 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
492ecee8 | 1393 | struct bpf_prog_stats stats; |
7bd509e3 | 1394 | |
492ecee8 | 1395 | bpf_prog_get_stats(prog, &stats); |
f1f7714e | 1396 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
7bd509e3 DB |
1397 | seq_printf(m, |
1398 | "prog_type:\t%u\n" | |
1399 | "prog_jited:\t%u\n" | |
f1f7714e | 1400 | "prog_tag:\t%s\n" |
4316b409 | 1401 | "memlock:\t%llu\n" |
492ecee8 AS |
1402 | "prog_id:\t%u\n" |
1403 | "run_time_ns:\t%llu\n" | |
1404 | "run_cnt:\t%llu\n", | |
7bd509e3 DB |
1405 | prog->type, |
1406 | prog->jited, | |
f1f7714e | 1407 | prog_tag, |
4316b409 | 1408 | prog->pages * 1ULL << PAGE_SHIFT, |
492ecee8 AS |
1409 | prog->aux->id, |
1410 | stats.nsecs, | |
1411 | stats.cnt); | |
7bd509e3 DB |
1412 | } |
1413 | #endif | |
1414 | ||
f66e448c | 1415 | const struct file_operations bpf_prog_fops = { |
7bd509e3 DB |
1416 | #ifdef CONFIG_PROC_FS |
1417 | .show_fdinfo = bpf_prog_show_fdinfo, | |
1418 | #endif | |
1419 | .release = bpf_prog_release, | |
6e71b04a CF |
1420 | .read = bpf_dummy_read, |
1421 | .write = bpf_dummy_write, | |
09756af4 AS |
1422 | }; |
1423 | ||
b2197755 | 1424 | int bpf_prog_new_fd(struct bpf_prog *prog) |
aa79781b | 1425 | { |
afdb09c7 CF |
1426 | int ret; |
1427 | ||
1428 | ret = security_bpf_prog(prog); | |
1429 | if (ret < 0) | |
1430 | return ret; | |
1431 | ||
aa79781b DB |
1432 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
1433 | O_RDWR | O_CLOEXEC); | |
1434 | } | |
1435 | ||
113214be | 1436 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
09756af4 | 1437 | { |
09756af4 AS |
1438 | if (!f.file) |
1439 | return ERR_PTR(-EBADF); | |
09756af4 AS |
1440 | if (f.file->f_op != &bpf_prog_fops) { |
1441 | fdput(f); | |
1442 | return ERR_PTR(-EINVAL); | |
1443 | } | |
1444 | ||
c2101297 | 1445 | return f.file->private_data; |
09756af4 AS |
1446 | } |
1447 | ||
59d3656d | 1448 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
92117d84 | 1449 | { |
59d3656d BB |
1450 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
1451 | atomic_sub(i, &prog->aux->refcnt); | |
92117d84 AS |
1452 | return ERR_PTR(-EBUSY); |
1453 | } | |
1454 | return prog; | |
1455 | } | |
59d3656d BB |
1456 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
1457 | ||
c540594f DB |
1458 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
1459 | { | |
1460 | /* Only to be used for undoing previous bpf_prog_add() in some | |
1461 | * error path. We still know that another entity in our call | |
1462 | * path holds a reference to the program, thus atomic_sub() can | |
1463 | * be safely used in such cases! | |
1464 | */ | |
1465 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); | |
1466 | } | |
1467 | EXPORT_SYMBOL_GPL(bpf_prog_sub); | |
1468 | ||
59d3656d BB |
1469 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
1470 | { | |
1471 | return bpf_prog_add(prog, 1); | |
1472 | } | |
97bc402d | 1473 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
92117d84 | 1474 | |
b16d9aa4 | 1475 | /* prog_idr_lock should have been held */ |
a6f6df69 | 1476 | struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) |
b16d9aa4 MKL |
1477 | { |
1478 | int refold; | |
1479 | ||
bfc18e38 | 1480 | refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0); |
b16d9aa4 MKL |
1481 | |
1482 | if (refold >= BPF_MAX_REFCNT) { | |
1483 | __bpf_prog_put(prog, false); | |
1484 | return ERR_PTR(-EBUSY); | |
1485 | } | |
1486 | ||
1487 | if (!refold) | |
1488 | return ERR_PTR(-ENOENT); | |
1489 | ||
1490 | return prog; | |
1491 | } | |
a6f6df69 | 1492 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
b16d9aa4 | 1493 | |
040ee692 | 1494 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
288b3de5 | 1495 | enum bpf_prog_type *attach_type, bool attach_drv) |
248f346f | 1496 | { |
288b3de5 JK |
1497 | /* not an attachment, just a refcount inc, always allow */ |
1498 | if (!attach_type) | |
1499 | return true; | |
248f346f JK |
1500 | |
1501 | if (prog->type != *attach_type) | |
1502 | return false; | |
288b3de5 | 1503 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
248f346f JK |
1504 | return false; |
1505 | ||
1506 | return true; | |
1507 | } | |
1508 | ||
1509 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, | |
288b3de5 | 1510 | bool attach_drv) |
09756af4 AS |
1511 | { |
1512 | struct fd f = fdget(ufd); | |
1513 | struct bpf_prog *prog; | |
1514 | ||
113214be | 1515 | prog = ____bpf_prog_get(f); |
09756af4 AS |
1516 | if (IS_ERR(prog)) |
1517 | return prog; | |
288b3de5 | 1518 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
113214be DB |
1519 | prog = ERR_PTR(-EINVAL); |
1520 | goto out; | |
1521 | } | |
09756af4 | 1522 | |
92117d84 | 1523 | prog = bpf_prog_inc(prog); |
113214be | 1524 | out: |
09756af4 AS |
1525 | fdput(f); |
1526 | return prog; | |
1527 | } | |
113214be DB |
1528 | |
1529 | struct bpf_prog *bpf_prog_get(u32 ufd) | |
1530 | { | |
288b3de5 | 1531 | return __bpf_prog_get(ufd, NULL, false); |
113214be DB |
1532 | } |
1533 | ||
248f346f | 1534 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
288b3de5 | 1535 | bool attach_drv) |
248f346f | 1536 | { |
4d220ed0 | 1537 | return __bpf_prog_get(ufd, &type, attach_drv); |
248f346f | 1538 | } |
6c8dfe21 | 1539 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
248f346f | 1540 | |
aac3fc32 AI |
1541 | /* Initially all BPF programs could be loaded w/o specifying |
1542 | * expected_attach_type. Later for some of them specifying expected_attach_type | |
1543 | * at load time became required so that program could be validated properly. | |
1544 | * Programs of types that are allowed to be loaded both w/ and w/o (for | |
1545 | * backward compatibility) expected_attach_type, should have the default attach | |
1546 | * type assigned to expected_attach_type for the latter case, so that it can be | |
1547 | * validated later at attach time. | |
1548 | * | |
1549 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if | |
1550 | * prog type requires it but has some attach types that have to be backward | |
1551 | * compatible. | |
1552 | */ | |
1553 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) | |
1554 | { | |
1555 | switch (attr->prog_type) { | |
1556 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
1557 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't | |
1558 | * exist so checking for non-zero is the way to go here. | |
1559 | */ | |
1560 | if (!attr->expected_attach_type) | |
1561 | attr->expected_attach_type = | |
1562 | BPF_CGROUP_INET_SOCK_CREATE; | |
1563 | break; | |
1564 | } | |
1565 | } | |
1566 | ||
5e43f899 AI |
1567 | static int |
1568 | bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, | |
1569 | enum bpf_attach_type expected_attach_type) | |
1570 | { | |
4fbac77d | 1571 | switch (prog_type) { |
aac3fc32 AI |
1572 | case BPF_PROG_TYPE_CGROUP_SOCK: |
1573 | switch (expected_attach_type) { | |
1574 | case BPF_CGROUP_INET_SOCK_CREATE: | |
1575 | case BPF_CGROUP_INET4_POST_BIND: | |
1576 | case BPF_CGROUP_INET6_POST_BIND: | |
1577 | return 0; | |
1578 | default: | |
1579 | return -EINVAL; | |
1580 | } | |
4fbac77d AI |
1581 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
1582 | switch (expected_attach_type) { | |
1583 | case BPF_CGROUP_INET4_BIND: | |
1584 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
1585 | case BPF_CGROUP_INET4_CONNECT: |
1586 | case BPF_CGROUP_INET6_CONNECT: | |
1cedee13 AI |
1587 | case BPF_CGROUP_UDP4_SENDMSG: |
1588 | case BPF_CGROUP_UDP6_SENDMSG: | |
983695fa DB |
1589 | case BPF_CGROUP_UDP4_RECVMSG: |
1590 | case BPF_CGROUP_UDP6_RECVMSG: | |
4fbac77d AI |
1591 | return 0; |
1592 | default: | |
1593 | return -EINVAL; | |
1594 | } | |
5cf1e914 | 1595 | case BPF_PROG_TYPE_CGROUP_SKB: |
1596 | switch (expected_attach_type) { | |
1597 | case BPF_CGROUP_INET_INGRESS: | |
1598 | case BPF_CGROUP_INET_EGRESS: | |
1599 | return 0; | |
1600 | default: | |
1601 | return -EINVAL; | |
1602 | } | |
0d01da6a SF |
1603 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
1604 | switch (expected_attach_type) { | |
1605 | case BPF_CGROUP_SETSOCKOPT: | |
1606 | case BPF_CGROUP_GETSOCKOPT: | |
1607 | return 0; | |
1608 | default: | |
1609 | return -EINVAL; | |
1610 | } | |
4fbac77d AI |
1611 | default: |
1612 | return 0; | |
1613 | } | |
5e43f899 AI |
1614 | } |
1615 | ||
09756af4 | 1616 | /* last field in 'union bpf_attr' used by this command */ |
c454a46b | 1617 | #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt |
09756af4 | 1618 | |
838e9690 | 1619 | static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) |
09756af4 AS |
1620 | { |
1621 | enum bpf_prog_type type = attr->prog_type; | |
1622 | struct bpf_prog *prog; | |
1623 | int err; | |
1624 | char license[128]; | |
1625 | bool is_gpl; | |
1626 | ||
1627 | if (CHECK_ATTR(BPF_PROG_LOAD)) | |
1628 | return -EINVAL; | |
1629 | ||
c240eff6 JW |
1630 | if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | |
1631 | BPF_F_ANY_ALIGNMENT | | |
10d274e8 | 1632 | BPF_F_TEST_STATE_FREQ | |
c240eff6 | 1633 | BPF_F_TEST_RND_HI32)) |
e07b98d9 DM |
1634 | return -EINVAL; |
1635 | ||
e9ee9efc DM |
1636 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && |
1637 | (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && | |
1638 | !capable(CAP_SYS_ADMIN)) | |
1639 | return -EPERM; | |
1640 | ||
09756af4 | 1641 | /* copy eBPF program license from user space */ |
535e7b4b | 1642 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
09756af4 AS |
1643 | sizeof(license) - 1) < 0) |
1644 | return -EFAULT; | |
1645 | license[sizeof(license) - 1] = 0; | |
1646 | ||
1647 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
1648 | is_gpl = license_is_gpl_compatible(license); | |
1649 | ||
c04c0d2b AS |
1650 | if (attr->insn_cnt == 0 || |
1651 | attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) | |
ef0915ca | 1652 | return -E2BIG; |
80b7d819 CF |
1653 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
1654 | type != BPF_PROG_TYPE_CGROUP_SKB && | |
1655 | !capable(CAP_SYS_ADMIN)) | |
1be7f75d AS |
1656 | return -EPERM; |
1657 | ||
aac3fc32 | 1658 | bpf_prog_load_fixup_attach_type(attr); |
5e43f899 AI |
1659 | if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) |
1660 | return -EINVAL; | |
1661 | ||
09756af4 AS |
1662 | /* plain bpf_prog allocation */ |
1663 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); | |
1664 | if (!prog) | |
1665 | return -ENOMEM; | |
1666 | ||
5e43f899 AI |
1667 | prog->expected_attach_type = attr->expected_attach_type; |
1668 | ||
9a18eedb JK |
1669 | prog->aux->offload_requested = !!attr->prog_ifindex; |
1670 | ||
afdb09c7 | 1671 | err = security_bpf_prog_alloc(prog->aux); |
aaac3ba9 AS |
1672 | if (err) |
1673 | goto free_prog_nouncharge; | |
1674 | ||
afdb09c7 CF |
1675 | err = bpf_prog_charge_memlock(prog); |
1676 | if (err) | |
1677 | goto free_prog_sec; | |
1678 | ||
09756af4 AS |
1679 | prog->len = attr->insn_cnt; |
1680 | ||
1681 | err = -EFAULT; | |
535e7b4b | 1682 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
aafe6ae9 | 1683 | bpf_prog_insn_size(prog)) != 0) |
09756af4 AS |
1684 | goto free_prog; |
1685 | ||
1686 | prog->orig_prog = NULL; | |
a91263d5 | 1687 | prog->jited = 0; |
09756af4 AS |
1688 | |
1689 | atomic_set(&prog->aux->refcnt, 1); | |
a91263d5 | 1690 | prog->gpl_compatible = is_gpl ? 1 : 0; |
09756af4 | 1691 | |
9a18eedb | 1692 | if (bpf_prog_is_dev_bound(prog->aux)) { |
ab3f0063 JK |
1693 | err = bpf_prog_offload_init(prog, attr); |
1694 | if (err) | |
1695 | goto free_prog; | |
1696 | } | |
1697 | ||
09756af4 AS |
1698 | /* find program type: socket_filter vs tracing_filter */ |
1699 | err = find_prog_type(type, prog); | |
1700 | if (err < 0) | |
1701 | goto free_prog; | |
1702 | ||
9285ec4c | 1703 | prog->aux->load_time = ktime_get_boottime_ns(); |
cb4d2b3f MKL |
1704 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); |
1705 | if (err) | |
1706 | goto free_prog; | |
1707 | ||
09756af4 | 1708 | /* run eBPF verifier */ |
838e9690 | 1709 | err = bpf_check(&prog, attr, uattr); |
09756af4 AS |
1710 | if (err < 0) |
1711 | goto free_used_maps; | |
1712 | ||
9facc336 | 1713 | prog = bpf_prog_select_runtime(prog, &err); |
04fd61ab AS |
1714 | if (err < 0) |
1715 | goto free_used_maps; | |
09756af4 | 1716 | |
dc4bb0e2 MKL |
1717 | err = bpf_prog_alloc_id(prog); |
1718 | if (err) | |
1719 | goto free_used_maps; | |
1720 | ||
c751798a DB |
1721 | /* Upon success of bpf_prog_alloc_id(), the BPF prog is |
1722 | * effectively publicly exposed. However, retrieving via | |
1723 | * bpf_prog_get_fd_by_id() will take another reference, | |
1724 | * therefore it cannot be gone underneath us. | |
1725 | * | |
1726 | * Only for the time /after/ successful bpf_prog_new_fd() | |
1727 | * and before returning to userspace, we might just hold | |
1728 | * one reference and any parallel close on that fd could | |
1729 | * rip everything out. Hence, below notifications must | |
1730 | * happen before bpf_prog_new_fd(). | |
1731 | * | |
1732 | * Also, any failure handling from this point onwards must | |
1733 | * be using bpf_prog_put() given the program is exposed. | |
1734 | */ | |
74451e66 | 1735 | bpf_prog_kallsyms_add(prog); |
6ee52e2a | 1736 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); |
c751798a DB |
1737 | |
1738 | err = bpf_prog_new_fd(prog); | |
1739 | if (err < 0) | |
1740 | bpf_prog_put(prog); | |
09756af4 AS |
1741 | return err; |
1742 | ||
1743 | free_used_maps: | |
c454a46b | 1744 | bpf_prog_free_linfo(prog); |
5482e9a9 MKL |
1745 | kvfree(prog->aux->func_info); |
1746 | btf_put(prog->aux->btf); | |
7d1982b4 | 1747 | bpf_prog_kallsyms_del_subprogs(prog); |
09756af4 AS |
1748 | free_used_maps(prog->aux); |
1749 | free_prog: | |
aaac3ba9 | 1750 | bpf_prog_uncharge_memlock(prog); |
afdb09c7 CF |
1751 | free_prog_sec: |
1752 | security_bpf_prog_free(prog->aux); | |
aaac3ba9 | 1753 | free_prog_nouncharge: |
09756af4 AS |
1754 | bpf_prog_free(prog); |
1755 | return err; | |
1756 | } | |
1757 | ||
6e71b04a | 1758 | #define BPF_OBJ_LAST_FIELD file_flags |
b2197755 DB |
1759 | |
1760 | static int bpf_obj_pin(const union bpf_attr *attr) | |
1761 | { | |
6e71b04a | 1762 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
b2197755 DB |
1763 | return -EINVAL; |
1764 | ||
535e7b4b | 1765 | return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); |
b2197755 DB |
1766 | } |
1767 | ||
1768 | static int bpf_obj_get(const union bpf_attr *attr) | |
1769 | { | |
6e71b04a CF |
1770 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
1771 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) | |
b2197755 DB |
1772 | return -EINVAL; |
1773 | ||
6e71b04a CF |
1774 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
1775 | attr->file_flags); | |
b2197755 DB |
1776 | } |
1777 | ||
c4f6699d AS |
1778 | struct bpf_raw_tracepoint { |
1779 | struct bpf_raw_event_map *btp; | |
1780 | struct bpf_prog *prog; | |
1781 | }; | |
1782 | ||
1783 | static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) | |
1784 | { | |
1785 | struct bpf_raw_tracepoint *raw_tp = filp->private_data; | |
1786 | ||
1787 | if (raw_tp->prog) { | |
1788 | bpf_probe_unregister(raw_tp->btp, raw_tp->prog); | |
1789 | bpf_prog_put(raw_tp->prog); | |
1790 | } | |
a38d1107 | 1791 | bpf_put_raw_tracepoint(raw_tp->btp); |
c4f6699d AS |
1792 | kfree(raw_tp); |
1793 | return 0; | |
1794 | } | |
1795 | ||
1796 | static const struct file_operations bpf_raw_tp_fops = { | |
1797 | .release = bpf_raw_tracepoint_release, | |
1798 | .read = bpf_dummy_read, | |
1799 | .write = bpf_dummy_write, | |
1800 | }; | |
1801 | ||
1802 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd | |
1803 | ||
1804 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) | |
1805 | { | |
1806 | struct bpf_raw_tracepoint *raw_tp; | |
1807 | struct bpf_raw_event_map *btp; | |
1808 | struct bpf_prog *prog; | |
1809 | char tp_name[128]; | |
1810 | int tp_fd, err; | |
1811 | ||
1812 | if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), | |
1813 | sizeof(tp_name) - 1) < 0) | |
1814 | return -EFAULT; | |
1815 | tp_name[sizeof(tp_name) - 1] = 0; | |
1816 | ||
a38d1107 | 1817 | btp = bpf_get_raw_tracepoint(tp_name); |
c4f6699d AS |
1818 | if (!btp) |
1819 | return -ENOENT; | |
1820 | ||
1821 | raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); | |
a38d1107 MM |
1822 | if (!raw_tp) { |
1823 | err = -ENOMEM; | |
1824 | goto out_put_btp; | |
1825 | } | |
c4f6699d AS |
1826 | raw_tp->btp = btp; |
1827 | ||
9df1c28b | 1828 | prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); |
c4f6699d AS |
1829 | if (IS_ERR(prog)) { |
1830 | err = PTR_ERR(prog); | |
1831 | goto out_free_tp; | |
1832 | } | |
9df1c28b MM |
1833 | if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT && |
1834 | prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) { | |
1835 | err = -EINVAL; | |
1836 | goto out_put_prog; | |
1837 | } | |
c4f6699d AS |
1838 | |
1839 | err = bpf_probe_register(raw_tp->btp, prog); | |
1840 | if (err) | |
1841 | goto out_put_prog; | |
1842 | ||
1843 | raw_tp->prog = prog; | |
1844 | tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, | |
1845 | O_CLOEXEC); | |
1846 | if (tp_fd < 0) { | |
1847 | bpf_probe_unregister(raw_tp->btp, prog); | |
1848 | err = tp_fd; | |
1849 | goto out_put_prog; | |
1850 | } | |
1851 | return tp_fd; | |
1852 | ||
1853 | out_put_prog: | |
1854 | bpf_prog_put(prog); | |
1855 | out_free_tp: | |
1856 | kfree(raw_tp); | |
a38d1107 MM |
1857 | out_put_btp: |
1858 | bpf_put_raw_tracepoint(btp); | |
c4f6699d AS |
1859 | return err; |
1860 | } | |
1861 | ||
33491588 AR |
1862 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
1863 | enum bpf_attach_type attach_type) | |
1864 | { | |
1865 | switch (prog->type) { | |
1866 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
1867 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: | |
0d01da6a | 1868 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
33491588 | 1869 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; |
5cf1e914 | 1870 | case BPF_PROG_TYPE_CGROUP_SKB: |
1871 | return prog->enforce_expected_attach_type && | |
1872 | prog->expected_attach_type != attach_type ? | |
1873 | -EINVAL : 0; | |
33491588 AR |
1874 | default: |
1875 | return 0; | |
1876 | } | |
1877 | } | |
1878 | ||
464bc0fd | 1879 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
174a79ff | 1880 | |
324bda9e AS |
1881 | #define BPF_F_ATTACH_MASK \ |
1882 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) | |
1883 | ||
f4324551 DM |
1884 | static int bpf_prog_attach(const union bpf_attr *attr) |
1885 | { | |
7f677633 | 1886 | enum bpf_prog_type ptype; |
f4324551 | 1887 | struct bpf_prog *prog; |
7f677633 | 1888 | int ret; |
f4324551 DM |
1889 | |
1890 | if (!capable(CAP_NET_ADMIN)) | |
1891 | return -EPERM; | |
1892 | ||
1893 | if (CHECK_ATTR(BPF_PROG_ATTACH)) | |
1894 | return -EINVAL; | |
1895 | ||
324bda9e | 1896 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
7f677633 AS |
1897 | return -EINVAL; |
1898 | ||
f4324551 DM |
1899 | switch (attr->attach_type) { |
1900 | case BPF_CGROUP_INET_INGRESS: | |
1901 | case BPF_CGROUP_INET_EGRESS: | |
b2cd1257 | 1902 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
f4324551 | 1903 | break; |
61023658 | 1904 | case BPF_CGROUP_INET_SOCK_CREATE: |
aac3fc32 AI |
1905 | case BPF_CGROUP_INET4_POST_BIND: |
1906 | case BPF_CGROUP_INET6_POST_BIND: | |
61023658 DA |
1907 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
1908 | break; | |
4fbac77d AI |
1909 | case BPF_CGROUP_INET4_BIND: |
1910 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
1911 | case BPF_CGROUP_INET4_CONNECT: |
1912 | case BPF_CGROUP_INET6_CONNECT: | |
1cedee13 AI |
1913 | case BPF_CGROUP_UDP4_SENDMSG: |
1914 | case BPF_CGROUP_UDP6_SENDMSG: | |
983695fa DB |
1915 | case BPF_CGROUP_UDP4_RECVMSG: |
1916 | case BPF_CGROUP_UDP6_RECVMSG: | |
4fbac77d AI |
1917 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
1918 | break; | |
40304b2a LB |
1919 | case BPF_CGROUP_SOCK_OPS: |
1920 | ptype = BPF_PROG_TYPE_SOCK_OPS; | |
1921 | break; | |
ebc614f6 RG |
1922 | case BPF_CGROUP_DEVICE: |
1923 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; | |
1924 | break; | |
4f738adb | 1925 | case BPF_SK_MSG_VERDICT: |
fdb5c453 SY |
1926 | ptype = BPF_PROG_TYPE_SK_MSG; |
1927 | break; | |
464bc0fd JF |
1928 | case BPF_SK_SKB_STREAM_PARSER: |
1929 | case BPF_SK_SKB_STREAM_VERDICT: | |
fdb5c453 SY |
1930 | ptype = BPF_PROG_TYPE_SK_SKB; |
1931 | break; | |
f4364dcf | 1932 | case BPF_LIRC_MODE2: |
fdb5c453 SY |
1933 | ptype = BPF_PROG_TYPE_LIRC_MODE2; |
1934 | break; | |
d58e468b PP |
1935 | case BPF_FLOW_DISSECTOR: |
1936 | ptype = BPF_PROG_TYPE_FLOW_DISSECTOR; | |
1937 | break; | |
7b146ceb AI |
1938 | case BPF_CGROUP_SYSCTL: |
1939 | ptype = BPF_PROG_TYPE_CGROUP_SYSCTL; | |
1940 | break; | |
0d01da6a SF |
1941 | case BPF_CGROUP_GETSOCKOPT: |
1942 | case BPF_CGROUP_SETSOCKOPT: | |
1943 | ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT; | |
1944 | break; | |
f4324551 DM |
1945 | default: |
1946 | return -EINVAL; | |
1947 | } | |
1948 | ||
b2cd1257 DA |
1949 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
1950 | if (IS_ERR(prog)) | |
1951 | return PTR_ERR(prog); | |
1952 | ||
5e43f899 AI |
1953 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
1954 | bpf_prog_put(prog); | |
1955 | return -EINVAL; | |
1956 | } | |
1957 | ||
fdb5c453 SY |
1958 | switch (ptype) { |
1959 | case BPF_PROG_TYPE_SK_SKB: | |
1960 | case BPF_PROG_TYPE_SK_MSG: | |
604326b4 | 1961 | ret = sock_map_get_from_fd(attr, prog); |
fdb5c453 SY |
1962 | break; |
1963 | case BPF_PROG_TYPE_LIRC_MODE2: | |
1964 | ret = lirc_prog_attach(attr, prog); | |
1965 | break; | |
d58e468b PP |
1966 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
1967 | ret = skb_flow_dissector_bpf_prog_attach(attr, prog); | |
1968 | break; | |
fdb5c453 SY |
1969 | default: |
1970 | ret = cgroup_bpf_prog_attach(attr, ptype, prog); | |
b2cd1257 DA |
1971 | } |
1972 | ||
7f677633 AS |
1973 | if (ret) |
1974 | bpf_prog_put(prog); | |
7f677633 | 1975 | return ret; |
f4324551 DM |
1976 | } |
1977 | ||
1978 | #define BPF_PROG_DETACH_LAST_FIELD attach_type | |
1979 | ||
1980 | static int bpf_prog_detach(const union bpf_attr *attr) | |
1981 | { | |
324bda9e | 1982 | enum bpf_prog_type ptype; |
f4324551 DM |
1983 | |
1984 | if (!capable(CAP_NET_ADMIN)) | |
1985 | return -EPERM; | |
1986 | ||
1987 | if (CHECK_ATTR(BPF_PROG_DETACH)) | |
1988 | return -EINVAL; | |
1989 | ||
1990 | switch (attr->attach_type) { | |
1991 | case BPF_CGROUP_INET_INGRESS: | |
1992 | case BPF_CGROUP_INET_EGRESS: | |
324bda9e AS |
1993 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
1994 | break; | |
61023658 | 1995 | case BPF_CGROUP_INET_SOCK_CREATE: |
aac3fc32 AI |
1996 | case BPF_CGROUP_INET4_POST_BIND: |
1997 | case BPF_CGROUP_INET6_POST_BIND: | |
324bda9e AS |
1998 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
1999 | break; | |
4fbac77d AI |
2000 | case BPF_CGROUP_INET4_BIND: |
2001 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
2002 | case BPF_CGROUP_INET4_CONNECT: |
2003 | case BPF_CGROUP_INET6_CONNECT: | |
1cedee13 AI |
2004 | case BPF_CGROUP_UDP4_SENDMSG: |
2005 | case BPF_CGROUP_UDP6_SENDMSG: | |
983695fa DB |
2006 | case BPF_CGROUP_UDP4_RECVMSG: |
2007 | case BPF_CGROUP_UDP6_RECVMSG: | |
4fbac77d AI |
2008 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
2009 | break; | |
40304b2a | 2010 | case BPF_CGROUP_SOCK_OPS: |
324bda9e | 2011 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
f4324551 | 2012 | break; |
ebc614f6 RG |
2013 | case BPF_CGROUP_DEVICE: |
2014 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; | |
2015 | break; | |
4f738adb | 2016 | case BPF_SK_MSG_VERDICT: |
604326b4 | 2017 | return sock_map_get_from_fd(attr, NULL); |
5a67da2a JF |
2018 | case BPF_SK_SKB_STREAM_PARSER: |
2019 | case BPF_SK_SKB_STREAM_VERDICT: | |
604326b4 | 2020 | return sock_map_get_from_fd(attr, NULL); |
f4364dcf SY |
2021 | case BPF_LIRC_MODE2: |
2022 | return lirc_prog_detach(attr); | |
d58e468b PP |
2023 | case BPF_FLOW_DISSECTOR: |
2024 | return skb_flow_dissector_bpf_prog_detach(attr); | |
7b146ceb AI |
2025 | case BPF_CGROUP_SYSCTL: |
2026 | ptype = BPF_PROG_TYPE_CGROUP_SYSCTL; | |
2027 | break; | |
0d01da6a SF |
2028 | case BPF_CGROUP_GETSOCKOPT: |
2029 | case BPF_CGROUP_SETSOCKOPT: | |
2030 | ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT; | |
2031 | break; | |
f4324551 DM |
2032 | default: |
2033 | return -EINVAL; | |
2034 | } | |
2035 | ||
fdb5c453 | 2036 | return cgroup_bpf_prog_detach(attr, ptype); |
f4324551 | 2037 | } |
40304b2a | 2038 | |
468e2f64 AS |
2039 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
2040 | ||
2041 | static int bpf_prog_query(const union bpf_attr *attr, | |
2042 | union bpf_attr __user *uattr) | |
2043 | { | |
468e2f64 AS |
2044 | if (!capable(CAP_NET_ADMIN)) |
2045 | return -EPERM; | |
2046 | if (CHECK_ATTR(BPF_PROG_QUERY)) | |
2047 | return -EINVAL; | |
2048 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) | |
2049 | return -EINVAL; | |
2050 | ||
2051 | switch (attr->query.attach_type) { | |
2052 | case BPF_CGROUP_INET_INGRESS: | |
2053 | case BPF_CGROUP_INET_EGRESS: | |
2054 | case BPF_CGROUP_INET_SOCK_CREATE: | |
4fbac77d AI |
2055 | case BPF_CGROUP_INET4_BIND: |
2056 | case BPF_CGROUP_INET6_BIND: | |
aac3fc32 AI |
2057 | case BPF_CGROUP_INET4_POST_BIND: |
2058 | case BPF_CGROUP_INET6_POST_BIND: | |
d74bad4e AI |
2059 | case BPF_CGROUP_INET4_CONNECT: |
2060 | case BPF_CGROUP_INET6_CONNECT: | |
1cedee13 AI |
2061 | case BPF_CGROUP_UDP4_SENDMSG: |
2062 | case BPF_CGROUP_UDP6_SENDMSG: | |
983695fa DB |
2063 | case BPF_CGROUP_UDP4_RECVMSG: |
2064 | case BPF_CGROUP_UDP6_RECVMSG: | |
468e2f64 | 2065 | case BPF_CGROUP_SOCK_OPS: |
ebc614f6 | 2066 | case BPF_CGROUP_DEVICE: |
7b146ceb | 2067 | case BPF_CGROUP_SYSCTL: |
0d01da6a SF |
2068 | case BPF_CGROUP_GETSOCKOPT: |
2069 | case BPF_CGROUP_SETSOCKOPT: | |
468e2f64 | 2070 | break; |
f4364dcf SY |
2071 | case BPF_LIRC_MODE2: |
2072 | return lirc_prog_query(attr, uattr); | |
118c8e9a SF |
2073 | case BPF_FLOW_DISSECTOR: |
2074 | return skb_flow_dissector_prog_query(attr, uattr); | |
468e2f64 AS |
2075 | default: |
2076 | return -EINVAL; | |
2077 | } | |
fdb5c453 SY |
2078 | |
2079 | return cgroup_bpf_prog_query(attr, uattr); | |
468e2f64 | 2080 | } |
f4324551 | 2081 | |
b0b9395d | 2082 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out |
1cf1cae9 AS |
2083 | |
2084 | static int bpf_prog_test_run(const union bpf_attr *attr, | |
2085 | union bpf_attr __user *uattr) | |
2086 | { | |
2087 | struct bpf_prog *prog; | |
2088 | int ret = -ENOTSUPP; | |
2089 | ||
61f3c964 AS |
2090 | if (!capable(CAP_SYS_ADMIN)) |
2091 | return -EPERM; | |
1cf1cae9 AS |
2092 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
2093 | return -EINVAL; | |
2094 | ||
b0b9395d SF |
2095 | if ((attr->test.ctx_size_in && !attr->test.ctx_in) || |
2096 | (!attr->test.ctx_size_in && attr->test.ctx_in)) | |
2097 | return -EINVAL; | |
2098 | ||
2099 | if ((attr->test.ctx_size_out && !attr->test.ctx_out) || | |
2100 | (!attr->test.ctx_size_out && attr->test.ctx_out)) | |
2101 | return -EINVAL; | |
2102 | ||
1cf1cae9 AS |
2103 | prog = bpf_prog_get(attr->test.prog_fd); |
2104 | if (IS_ERR(prog)) | |
2105 | return PTR_ERR(prog); | |
2106 | ||
2107 | if (prog->aux->ops->test_run) | |
2108 | ret = prog->aux->ops->test_run(prog, attr, uattr); | |
2109 | ||
2110 | bpf_prog_put(prog); | |
2111 | return ret; | |
2112 | } | |
2113 | ||
34ad5580 MKL |
2114 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
2115 | ||
2116 | static int bpf_obj_get_next_id(const union bpf_attr *attr, | |
2117 | union bpf_attr __user *uattr, | |
2118 | struct idr *idr, | |
2119 | spinlock_t *lock) | |
2120 | { | |
2121 | u32 next_id = attr->start_id; | |
2122 | int err = 0; | |
2123 | ||
2124 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) | |
2125 | return -EINVAL; | |
2126 | ||
2127 | if (!capable(CAP_SYS_ADMIN)) | |
2128 | return -EPERM; | |
2129 | ||
2130 | next_id++; | |
2131 | spin_lock_bh(lock); | |
2132 | if (!idr_get_next(idr, &next_id)) | |
2133 | err = -ENOENT; | |
2134 | spin_unlock_bh(lock); | |
2135 | ||
2136 | if (!err) | |
2137 | err = put_user(next_id, &uattr->next_id); | |
2138 | ||
2139 | return err; | |
2140 | } | |
2141 | ||
b16d9aa4 MKL |
2142 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
2143 | ||
2144 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) | |
2145 | { | |
2146 | struct bpf_prog *prog; | |
2147 | u32 id = attr->prog_id; | |
2148 | int fd; | |
2149 | ||
2150 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) | |
2151 | return -EINVAL; | |
2152 | ||
2153 | if (!capable(CAP_SYS_ADMIN)) | |
2154 | return -EPERM; | |
2155 | ||
2156 | spin_lock_bh(&prog_idr_lock); | |
2157 | prog = idr_find(&prog_idr, id); | |
2158 | if (prog) | |
2159 | prog = bpf_prog_inc_not_zero(prog); | |
2160 | else | |
2161 | prog = ERR_PTR(-ENOENT); | |
2162 | spin_unlock_bh(&prog_idr_lock); | |
2163 | ||
2164 | if (IS_ERR(prog)) | |
2165 | return PTR_ERR(prog); | |
2166 | ||
2167 | fd = bpf_prog_new_fd(prog); | |
2168 | if (fd < 0) | |
2169 | bpf_prog_put(prog); | |
2170 | ||
2171 | return fd; | |
2172 | } | |
2173 | ||
6e71b04a | 2174 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
bd5f5f4e MKL |
2175 | |
2176 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) | |
2177 | { | |
2178 | struct bpf_map *map; | |
2179 | u32 id = attr->map_id; | |
6e71b04a | 2180 | int f_flags; |
bd5f5f4e MKL |
2181 | int fd; |
2182 | ||
6e71b04a CF |
2183 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
2184 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) | |
bd5f5f4e MKL |
2185 | return -EINVAL; |
2186 | ||
2187 | if (!capable(CAP_SYS_ADMIN)) | |
2188 | return -EPERM; | |
2189 | ||
6e71b04a CF |
2190 | f_flags = bpf_get_file_flag(attr->open_flags); |
2191 | if (f_flags < 0) | |
2192 | return f_flags; | |
2193 | ||
bd5f5f4e MKL |
2194 | spin_lock_bh(&map_idr_lock); |
2195 | map = idr_find(&map_idr, id); | |
2196 | if (map) | |
b0e4701c | 2197 | map = __bpf_map_inc_not_zero(map, true); |
bd5f5f4e MKL |
2198 | else |
2199 | map = ERR_PTR(-ENOENT); | |
2200 | spin_unlock_bh(&map_idr_lock); | |
2201 | ||
2202 | if (IS_ERR(map)) | |
2203 | return PTR_ERR(map); | |
2204 | ||
6e71b04a | 2205 | fd = bpf_map_new_fd(map, f_flags); |
bd5f5f4e | 2206 | if (fd < 0) |
781e6282 | 2207 | bpf_map_put_with_uref(map); |
bd5f5f4e MKL |
2208 | |
2209 | return fd; | |
2210 | } | |
2211 | ||
7105e828 | 2212 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
d8eca5bb DB |
2213 | unsigned long addr, u32 *off, |
2214 | u32 *type) | |
7105e828 | 2215 | { |
d8eca5bb | 2216 | const struct bpf_map *map; |
7105e828 DB |
2217 | int i; |
2218 | ||
d8eca5bb DB |
2219 | for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { |
2220 | map = prog->aux->used_maps[i]; | |
2221 | if (map == (void *)addr) { | |
2222 | *type = BPF_PSEUDO_MAP_FD; | |
2223 | return map; | |
2224 | } | |
2225 | if (!map->ops->map_direct_value_meta) | |
2226 | continue; | |
2227 | if (!map->ops->map_direct_value_meta(map, addr, off)) { | |
2228 | *type = BPF_PSEUDO_MAP_VALUE; | |
2229 | return map; | |
2230 | } | |
2231 | } | |
2232 | ||
7105e828 DB |
2233 | return NULL; |
2234 | } | |
2235 | ||
2236 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) | |
2237 | { | |
2238 | const struct bpf_map *map; | |
2239 | struct bpf_insn *insns; | |
d8eca5bb | 2240 | u32 off, type; |
7105e828 DB |
2241 | u64 imm; |
2242 | int i; | |
2243 | ||
2244 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), | |
2245 | GFP_USER); | |
2246 | if (!insns) | |
2247 | return insns; | |
2248 | ||
2249 | for (i = 0; i < prog->len; i++) { | |
2250 | if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { | |
2251 | insns[i].code = BPF_JMP | BPF_CALL; | |
2252 | insns[i].imm = BPF_FUNC_tail_call; | |
2253 | /* fall-through */ | |
2254 | } | |
2255 | if (insns[i].code == (BPF_JMP | BPF_CALL) || | |
2256 | insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { | |
2257 | if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) | |
2258 | insns[i].code = BPF_JMP | BPF_CALL; | |
2259 | if (!bpf_dump_raw_ok()) | |
2260 | insns[i].imm = 0; | |
2261 | continue; | |
2262 | } | |
2263 | ||
2264 | if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) | |
2265 | continue; | |
2266 | ||
2267 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; | |
d8eca5bb | 2268 | map = bpf_map_from_imm(prog, imm, &off, &type); |
7105e828 | 2269 | if (map) { |
d8eca5bb | 2270 | insns[i].src_reg = type; |
7105e828 | 2271 | insns[i].imm = map->id; |
d8eca5bb | 2272 | insns[i + 1].imm = off; |
7105e828 DB |
2273 | continue; |
2274 | } | |
7105e828 DB |
2275 | } |
2276 | ||
2277 | return insns; | |
2278 | } | |
2279 | ||
c454a46b MKL |
2280 | static int set_info_rec_size(struct bpf_prog_info *info) |
2281 | { | |
2282 | /* | |
2283 | * Ensure info.*_rec_size is the same as kernel expected size | |
2284 | * | |
2285 | * or | |
2286 | * | |
2287 | * Only allow zero *_rec_size if both _rec_size and _cnt are | |
2288 | * zero. In this case, the kernel will set the expected | |
2289 | * _rec_size back to the info. | |
2290 | */ | |
2291 | ||
11d8b82d | 2292 | if ((info->nr_func_info || info->func_info_rec_size) && |
c454a46b MKL |
2293 | info->func_info_rec_size != sizeof(struct bpf_func_info)) |
2294 | return -EINVAL; | |
2295 | ||
11d8b82d | 2296 | if ((info->nr_line_info || info->line_info_rec_size) && |
c454a46b MKL |
2297 | info->line_info_rec_size != sizeof(struct bpf_line_info)) |
2298 | return -EINVAL; | |
2299 | ||
11d8b82d | 2300 | if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && |
c454a46b MKL |
2301 | info->jited_line_info_rec_size != sizeof(__u64)) |
2302 | return -EINVAL; | |
2303 | ||
2304 | info->func_info_rec_size = sizeof(struct bpf_func_info); | |
2305 | info->line_info_rec_size = sizeof(struct bpf_line_info); | |
2306 | info->jited_line_info_rec_size = sizeof(__u64); | |
2307 | ||
2308 | return 0; | |
2309 | } | |
2310 | ||
1e270976 MKL |
2311 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
2312 | const union bpf_attr *attr, | |
2313 | union bpf_attr __user *uattr) | |
2314 | { | |
2315 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
2316 | struct bpf_prog_info info = {}; | |
2317 | u32 info_len = attr->info.info_len; | |
5f8f8b93 | 2318 | struct bpf_prog_stats stats; |
1e270976 MKL |
2319 | char __user *uinsns; |
2320 | u32 ulen; | |
2321 | int err; | |
2322 | ||
dcab51f1 | 2323 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
1e270976 MKL |
2324 | if (err) |
2325 | return err; | |
2326 | info_len = min_t(u32, sizeof(info), info_len); | |
2327 | ||
2328 | if (copy_from_user(&info, uinfo, info_len)) | |
89b09689 | 2329 | return -EFAULT; |
1e270976 MKL |
2330 | |
2331 | info.type = prog->type; | |
2332 | info.id = prog->aux->id; | |
cb4d2b3f MKL |
2333 | info.load_time = prog->aux->load_time; |
2334 | info.created_by_uid = from_kuid_munged(current_user_ns(), | |
2335 | prog->aux->user->uid); | |
b85fab0e | 2336 | info.gpl_compatible = prog->gpl_compatible; |
1e270976 MKL |
2337 | |
2338 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); | |
cb4d2b3f MKL |
2339 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
2340 | ||
2341 | ulen = info.nr_map_ids; | |
2342 | info.nr_map_ids = prog->aux->used_map_cnt; | |
2343 | ulen = min_t(u32, info.nr_map_ids, ulen); | |
2344 | if (ulen) { | |
721e08da | 2345 | u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); |
cb4d2b3f MKL |
2346 | u32 i; |
2347 | ||
2348 | for (i = 0; i < ulen; i++) | |
2349 | if (put_user(prog->aux->used_maps[i]->id, | |
2350 | &user_map_ids[i])) | |
2351 | return -EFAULT; | |
2352 | } | |
1e270976 | 2353 | |
c454a46b MKL |
2354 | err = set_info_rec_size(&info); |
2355 | if (err) | |
2356 | return err; | |
7337224f | 2357 | |
5f8f8b93 AS |
2358 | bpf_prog_get_stats(prog, &stats); |
2359 | info.run_time_ns = stats.nsecs; | |
2360 | info.run_cnt = stats.cnt; | |
2361 | ||
1e270976 MKL |
2362 | if (!capable(CAP_SYS_ADMIN)) { |
2363 | info.jited_prog_len = 0; | |
2364 | info.xlated_prog_len = 0; | |
dbecd738 | 2365 | info.nr_jited_ksyms = 0; |
28c2fae7 | 2366 | info.nr_jited_func_lens = 0; |
11d8b82d YS |
2367 | info.nr_func_info = 0; |
2368 | info.nr_line_info = 0; | |
2369 | info.nr_jited_line_info = 0; | |
1e270976 MKL |
2370 | goto done; |
2371 | } | |
2372 | ||
1e270976 | 2373 | ulen = info.xlated_prog_len; |
9975a54b | 2374 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
1e270976 | 2375 | if (info.xlated_prog_len && ulen) { |
7105e828 DB |
2376 | struct bpf_insn *insns_sanitized; |
2377 | bool fault; | |
2378 | ||
2379 | if (prog->blinded && !bpf_dump_raw_ok()) { | |
2380 | info.xlated_prog_insns = 0; | |
2381 | goto done; | |
2382 | } | |
2383 | insns_sanitized = bpf_insn_prepare_dump(prog); | |
2384 | if (!insns_sanitized) | |
2385 | return -ENOMEM; | |
1e270976 MKL |
2386 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
2387 | ulen = min_t(u32, info.xlated_prog_len, ulen); | |
7105e828 DB |
2388 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
2389 | kfree(insns_sanitized); | |
2390 | if (fault) | |
1e270976 MKL |
2391 | return -EFAULT; |
2392 | } | |
2393 | ||
675fc275 JK |
2394 | if (bpf_prog_is_dev_bound(prog->aux)) { |
2395 | err = bpf_prog_offload_info_fill(&info, prog); | |
2396 | if (err) | |
2397 | return err; | |
fcfb126d JW |
2398 | goto done; |
2399 | } | |
2400 | ||
2401 | /* NOTE: the following code is supposed to be skipped for offload. | |
2402 | * bpf_prog_offload_info_fill() is the place to fill similar fields | |
2403 | * for offload. | |
2404 | */ | |
2405 | ulen = info.jited_prog_len; | |
4d56a76e SD |
2406 | if (prog->aux->func_cnt) { |
2407 | u32 i; | |
2408 | ||
2409 | info.jited_prog_len = 0; | |
2410 | for (i = 0; i < prog->aux->func_cnt; i++) | |
2411 | info.jited_prog_len += prog->aux->func[i]->jited_len; | |
2412 | } else { | |
2413 | info.jited_prog_len = prog->jited_len; | |
2414 | } | |
2415 | ||
fcfb126d JW |
2416 | if (info.jited_prog_len && ulen) { |
2417 | if (bpf_dump_raw_ok()) { | |
2418 | uinsns = u64_to_user_ptr(info.jited_prog_insns); | |
2419 | ulen = min_t(u32, info.jited_prog_len, ulen); | |
4d56a76e SD |
2420 | |
2421 | /* for multi-function programs, copy the JITed | |
2422 | * instructions for all the functions | |
2423 | */ | |
2424 | if (prog->aux->func_cnt) { | |
2425 | u32 len, free, i; | |
2426 | u8 *img; | |
2427 | ||
2428 | free = ulen; | |
2429 | for (i = 0; i < prog->aux->func_cnt; i++) { | |
2430 | len = prog->aux->func[i]->jited_len; | |
2431 | len = min_t(u32, len, free); | |
2432 | img = (u8 *) prog->aux->func[i]->bpf_func; | |
2433 | if (copy_to_user(uinsns, img, len)) | |
2434 | return -EFAULT; | |
2435 | uinsns += len; | |
2436 | free -= len; | |
2437 | if (!free) | |
2438 | break; | |
2439 | } | |
2440 | } else { | |
2441 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) | |
2442 | return -EFAULT; | |
2443 | } | |
fcfb126d JW |
2444 | } else { |
2445 | info.jited_prog_insns = 0; | |
2446 | } | |
675fc275 JK |
2447 | } |
2448 | ||
dbecd738 | 2449 | ulen = info.nr_jited_ksyms; |
ff1889fc | 2450 | info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; |
7a5725dd | 2451 | if (ulen) { |
dbecd738 | 2452 | if (bpf_dump_raw_ok()) { |
ff1889fc | 2453 | unsigned long ksym_addr; |
dbecd738 | 2454 | u64 __user *user_ksyms; |
dbecd738 SD |
2455 | u32 i; |
2456 | ||
2457 | /* copy the address of the kernel symbol | |
2458 | * corresponding to each function | |
2459 | */ | |
2460 | ulen = min_t(u32, info.nr_jited_ksyms, ulen); | |
2461 | user_ksyms = u64_to_user_ptr(info.jited_ksyms); | |
ff1889fc SL |
2462 | if (prog->aux->func_cnt) { |
2463 | for (i = 0; i < ulen; i++) { | |
2464 | ksym_addr = (unsigned long) | |
2465 | prog->aux->func[i]->bpf_func; | |
2466 | if (put_user((u64) ksym_addr, | |
2467 | &user_ksyms[i])) | |
2468 | return -EFAULT; | |
2469 | } | |
2470 | } else { | |
2471 | ksym_addr = (unsigned long) prog->bpf_func; | |
2472 | if (put_user((u64) ksym_addr, &user_ksyms[0])) | |
dbecd738 SD |
2473 | return -EFAULT; |
2474 | } | |
2475 | } else { | |
2476 | info.jited_ksyms = 0; | |
2477 | } | |
2478 | } | |
2479 | ||
815581c1 | 2480 | ulen = info.nr_jited_func_lens; |
ff1889fc | 2481 | info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; |
7a5725dd | 2482 | if (ulen) { |
815581c1 SD |
2483 | if (bpf_dump_raw_ok()) { |
2484 | u32 __user *user_lens; | |
2485 | u32 func_len, i; | |
2486 | ||
2487 | /* copy the JITed image lengths for each function */ | |
2488 | ulen = min_t(u32, info.nr_jited_func_lens, ulen); | |
2489 | user_lens = u64_to_user_ptr(info.jited_func_lens); | |
ff1889fc SL |
2490 | if (prog->aux->func_cnt) { |
2491 | for (i = 0; i < ulen; i++) { | |
2492 | func_len = | |
2493 | prog->aux->func[i]->jited_len; | |
2494 | if (put_user(func_len, &user_lens[i])) | |
2495 | return -EFAULT; | |
2496 | } | |
2497 | } else { | |
2498 | func_len = prog->jited_len; | |
2499 | if (put_user(func_len, &user_lens[0])) | |
815581c1 SD |
2500 | return -EFAULT; |
2501 | } | |
2502 | } else { | |
2503 | info.jited_func_lens = 0; | |
2504 | } | |
2505 | } | |
2506 | ||
7337224f | 2507 | if (prog->aux->btf) |
838e9690 YS |
2508 | info.btf_id = btf_id(prog->aux->btf); |
2509 | ||
11d8b82d YS |
2510 | ulen = info.nr_func_info; |
2511 | info.nr_func_info = prog->aux->func_info_cnt; | |
2512 | if (info.nr_func_info && ulen) { | |
9e794163 | 2513 | char __user *user_finfo; |
7337224f | 2514 | |
9e794163 MKL |
2515 | user_finfo = u64_to_user_ptr(info.func_info); |
2516 | ulen = min_t(u32, info.nr_func_info, ulen); | |
2517 | if (copy_to_user(user_finfo, prog->aux->func_info, | |
2518 | info.func_info_rec_size * ulen)) | |
2519 | return -EFAULT; | |
838e9690 YS |
2520 | } |
2521 | ||
11d8b82d YS |
2522 | ulen = info.nr_line_info; |
2523 | info.nr_line_info = prog->aux->nr_linfo; | |
2524 | if (info.nr_line_info && ulen) { | |
9e794163 | 2525 | __u8 __user *user_linfo; |
c454a46b | 2526 | |
9e794163 MKL |
2527 | user_linfo = u64_to_user_ptr(info.line_info); |
2528 | ulen = min_t(u32, info.nr_line_info, ulen); | |
2529 | if (copy_to_user(user_linfo, prog->aux->linfo, | |
2530 | info.line_info_rec_size * ulen)) | |
2531 | return -EFAULT; | |
c454a46b MKL |
2532 | } |
2533 | ||
11d8b82d | 2534 | ulen = info.nr_jited_line_info; |
c454a46b | 2535 | if (prog->aux->jited_linfo) |
11d8b82d | 2536 | info.nr_jited_line_info = prog->aux->nr_linfo; |
c454a46b | 2537 | else |
11d8b82d YS |
2538 | info.nr_jited_line_info = 0; |
2539 | if (info.nr_jited_line_info && ulen) { | |
c454a46b MKL |
2540 | if (bpf_dump_raw_ok()) { |
2541 | __u64 __user *user_linfo; | |
2542 | u32 i; | |
2543 | ||
2544 | user_linfo = u64_to_user_ptr(info.jited_line_info); | |
11d8b82d | 2545 | ulen = min_t(u32, info.nr_jited_line_info, ulen); |
c454a46b MKL |
2546 | for (i = 0; i < ulen; i++) { |
2547 | if (put_user((__u64)(long)prog->aux->jited_linfo[i], | |
2548 | &user_linfo[i])) | |
2549 | return -EFAULT; | |
2550 | } | |
2551 | } else { | |
2552 | info.jited_line_info = 0; | |
2553 | } | |
2554 | } | |
2555 | ||
c872bdb3 SL |
2556 | ulen = info.nr_prog_tags; |
2557 | info.nr_prog_tags = prog->aux->func_cnt ? : 1; | |
2558 | if (ulen) { | |
2559 | __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; | |
2560 | u32 i; | |
2561 | ||
2562 | user_prog_tags = u64_to_user_ptr(info.prog_tags); | |
2563 | ulen = min_t(u32, info.nr_prog_tags, ulen); | |
2564 | if (prog->aux->func_cnt) { | |
2565 | for (i = 0; i < ulen; i++) { | |
2566 | if (copy_to_user(user_prog_tags[i], | |
2567 | prog->aux->func[i]->tag, | |
2568 | BPF_TAG_SIZE)) | |
2569 | return -EFAULT; | |
2570 | } | |
2571 | } else { | |
2572 | if (copy_to_user(user_prog_tags[0], | |
2573 | prog->tag, BPF_TAG_SIZE)) | |
2574 | return -EFAULT; | |
2575 | } | |
2576 | } | |
2577 | ||
1e270976 MKL |
2578 | done: |
2579 | if (copy_to_user(uinfo, &info, info_len) || | |
2580 | put_user(info_len, &uattr->info.info_len)) | |
2581 | return -EFAULT; | |
2582 | ||
2583 | return 0; | |
2584 | } | |
2585 | ||
2586 | static int bpf_map_get_info_by_fd(struct bpf_map *map, | |
2587 | const union bpf_attr *attr, | |
2588 | union bpf_attr __user *uattr) | |
2589 | { | |
2590 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
2591 | struct bpf_map_info info = {}; | |
2592 | u32 info_len = attr->info.info_len; | |
2593 | int err; | |
2594 | ||
dcab51f1 | 2595 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
1e270976 MKL |
2596 | if (err) |
2597 | return err; | |
2598 | info_len = min_t(u32, sizeof(info), info_len); | |
2599 | ||
2600 | info.type = map->map_type; | |
2601 | info.id = map->id; | |
2602 | info.key_size = map->key_size; | |
2603 | info.value_size = map->value_size; | |
2604 | info.max_entries = map->max_entries; | |
2605 | info.map_flags = map->map_flags; | |
ad5b177b | 2606 | memcpy(info.name, map->name, sizeof(map->name)); |
1e270976 | 2607 | |
78958fca MKL |
2608 | if (map->btf) { |
2609 | info.btf_id = btf_id(map->btf); | |
9b2cf328 MKL |
2610 | info.btf_key_type_id = map->btf_key_type_id; |
2611 | info.btf_value_type_id = map->btf_value_type_id; | |
78958fca MKL |
2612 | } |
2613 | ||
52775b33 JK |
2614 | if (bpf_map_is_dev_bound(map)) { |
2615 | err = bpf_map_offload_info_fill(&info, map); | |
2616 | if (err) | |
2617 | return err; | |
2618 | } | |
2619 | ||
1e270976 MKL |
2620 | if (copy_to_user(uinfo, &info, info_len) || |
2621 | put_user(info_len, &uattr->info.info_len)) | |
2622 | return -EFAULT; | |
2623 | ||
2624 | return 0; | |
2625 | } | |
2626 | ||
62dab84c MKL |
2627 | static int bpf_btf_get_info_by_fd(struct btf *btf, |
2628 | const union bpf_attr *attr, | |
2629 | union bpf_attr __user *uattr) | |
2630 | { | |
2631 | struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
2632 | u32 info_len = attr->info.info_len; | |
2633 | int err; | |
2634 | ||
dcab51f1 | 2635 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); |
62dab84c MKL |
2636 | if (err) |
2637 | return err; | |
2638 | ||
2639 | return btf_get_info_by_fd(btf, attr, uattr); | |
2640 | } | |
2641 | ||
1e270976 MKL |
2642 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
2643 | ||
2644 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, | |
2645 | union bpf_attr __user *uattr) | |
2646 | { | |
2647 | int ufd = attr->info.bpf_fd; | |
2648 | struct fd f; | |
2649 | int err; | |
2650 | ||
2651 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) | |
2652 | return -EINVAL; | |
2653 | ||
2654 | f = fdget(ufd); | |
2655 | if (!f.file) | |
2656 | return -EBADFD; | |
2657 | ||
2658 | if (f.file->f_op == &bpf_prog_fops) | |
2659 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, | |
2660 | uattr); | |
2661 | else if (f.file->f_op == &bpf_map_fops) | |
2662 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, | |
2663 | uattr); | |
60197cfb | 2664 | else if (f.file->f_op == &btf_fops) |
62dab84c | 2665 | err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr); |
1e270976 MKL |
2666 | else |
2667 | err = -EINVAL; | |
2668 | ||
2669 | fdput(f); | |
2670 | return err; | |
2671 | } | |
2672 | ||
f56a653c MKL |
2673 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
2674 | ||
2675 | static int bpf_btf_load(const union bpf_attr *attr) | |
2676 | { | |
2677 | if (CHECK_ATTR(BPF_BTF_LOAD)) | |
2678 | return -EINVAL; | |
2679 | ||
2680 | if (!capable(CAP_SYS_ADMIN)) | |
2681 | return -EPERM; | |
2682 | ||
2683 | return btf_new_fd(attr); | |
2684 | } | |
2685 | ||
78958fca MKL |
2686 | #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id |
2687 | ||
2688 | static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) | |
2689 | { | |
2690 | if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) | |
2691 | return -EINVAL; | |
2692 | ||
2693 | if (!capable(CAP_SYS_ADMIN)) | |
2694 | return -EPERM; | |
2695 | ||
2696 | return btf_get_fd_by_id(attr->btf_id); | |
2697 | } | |
2698 | ||
41bdc4b4 YS |
2699 | static int bpf_task_fd_query_copy(const union bpf_attr *attr, |
2700 | union bpf_attr __user *uattr, | |
2701 | u32 prog_id, u32 fd_type, | |
2702 | const char *buf, u64 probe_offset, | |
2703 | u64 probe_addr) | |
2704 | { | |
2705 | char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); | |
2706 | u32 len = buf ? strlen(buf) : 0, input_len; | |
2707 | int err = 0; | |
2708 | ||
2709 | if (put_user(len, &uattr->task_fd_query.buf_len)) | |
2710 | return -EFAULT; | |
2711 | input_len = attr->task_fd_query.buf_len; | |
2712 | if (input_len && ubuf) { | |
2713 | if (!len) { | |
2714 | /* nothing to copy, just make ubuf NULL terminated */ | |
2715 | char zero = '\0'; | |
2716 | ||
2717 | if (put_user(zero, ubuf)) | |
2718 | return -EFAULT; | |
2719 | } else if (input_len >= len + 1) { | |
2720 | /* ubuf can hold the string with NULL terminator */ | |
2721 | if (copy_to_user(ubuf, buf, len + 1)) | |
2722 | return -EFAULT; | |
2723 | } else { | |
2724 | /* ubuf cannot hold the string with NULL terminator, | |
2725 | * do a partial copy with NULL terminator. | |
2726 | */ | |
2727 | char zero = '\0'; | |
2728 | ||
2729 | err = -ENOSPC; | |
2730 | if (copy_to_user(ubuf, buf, input_len - 1)) | |
2731 | return -EFAULT; | |
2732 | if (put_user(zero, ubuf + input_len - 1)) | |
2733 | return -EFAULT; | |
2734 | } | |
2735 | } | |
2736 | ||
2737 | if (put_user(prog_id, &uattr->task_fd_query.prog_id) || | |
2738 | put_user(fd_type, &uattr->task_fd_query.fd_type) || | |
2739 | put_user(probe_offset, &uattr->task_fd_query.probe_offset) || | |
2740 | put_user(probe_addr, &uattr->task_fd_query.probe_addr)) | |
2741 | return -EFAULT; | |
2742 | ||
2743 | return err; | |
2744 | } | |
2745 | ||
2746 | #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr | |
2747 | ||
2748 | static int bpf_task_fd_query(const union bpf_attr *attr, | |
2749 | union bpf_attr __user *uattr) | |
2750 | { | |
2751 | pid_t pid = attr->task_fd_query.pid; | |
2752 | u32 fd = attr->task_fd_query.fd; | |
2753 | const struct perf_event *event; | |
2754 | struct files_struct *files; | |
2755 | struct task_struct *task; | |
2756 | struct file *file; | |
2757 | int err; | |
2758 | ||
2759 | if (CHECK_ATTR(BPF_TASK_FD_QUERY)) | |
2760 | return -EINVAL; | |
2761 | ||
2762 | if (!capable(CAP_SYS_ADMIN)) | |
2763 | return -EPERM; | |
2764 | ||
2765 | if (attr->task_fd_query.flags != 0) | |
2766 | return -EINVAL; | |
2767 | ||
2768 | task = get_pid_task(find_vpid(pid), PIDTYPE_PID); | |
2769 | if (!task) | |
2770 | return -ENOENT; | |
2771 | ||
2772 | files = get_files_struct(task); | |
2773 | put_task_struct(task); | |
2774 | if (!files) | |
2775 | return -ENOENT; | |
2776 | ||
2777 | err = 0; | |
2778 | spin_lock(&files->file_lock); | |
2779 | file = fcheck_files(files, fd); | |
2780 | if (!file) | |
2781 | err = -EBADF; | |
2782 | else | |
2783 | get_file(file); | |
2784 | spin_unlock(&files->file_lock); | |
2785 | put_files_struct(files); | |
2786 | ||
2787 | if (err) | |
2788 | goto out; | |
2789 | ||
2790 | if (file->f_op == &bpf_raw_tp_fops) { | |
2791 | struct bpf_raw_tracepoint *raw_tp = file->private_data; | |
2792 | struct bpf_raw_event_map *btp = raw_tp->btp; | |
2793 | ||
2794 | err = bpf_task_fd_query_copy(attr, uattr, | |
2795 | raw_tp->prog->aux->id, | |
2796 | BPF_FD_TYPE_RAW_TRACEPOINT, | |
2797 | btp->tp->name, 0, 0); | |
2798 | goto put_file; | |
2799 | } | |
2800 | ||
2801 | event = perf_get_event(file); | |
2802 | if (!IS_ERR(event)) { | |
2803 | u64 probe_offset, probe_addr; | |
2804 | u32 prog_id, fd_type; | |
2805 | const char *buf; | |
2806 | ||
2807 | err = bpf_get_perf_event_info(event, &prog_id, &fd_type, | |
2808 | &buf, &probe_offset, | |
2809 | &probe_addr); | |
2810 | if (!err) | |
2811 | err = bpf_task_fd_query_copy(attr, uattr, prog_id, | |
2812 | fd_type, buf, | |
2813 | probe_offset, | |
2814 | probe_addr); | |
2815 | goto put_file; | |
2816 | } | |
2817 | ||
2818 | err = -ENOTSUPP; | |
2819 | put_file: | |
2820 | fput(file); | |
2821 | out: | |
2822 | return err; | |
2823 | } | |
2824 | ||
99c55f7d AS |
2825 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
2826 | { | |
2827 | union bpf_attr attr = {}; | |
2828 | int err; | |
2829 | ||
0fa4fe85 | 2830 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
99c55f7d AS |
2831 | return -EPERM; |
2832 | ||
dcab51f1 | 2833 | err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); |
1e270976 MKL |
2834 | if (err) |
2835 | return err; | |
2836 | size = min_t(u32, size, sizeof(attr)); | |
99c55f7d AS |
2837 | |
2838 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ | |
2839 | if (copy_from_user(&attr, uattr, size) != 0) | |
2840 | return -EFAULT; | |
2841 | ||
afdb09c7 CF |
2842 | err = security_bpf(cmd, &attr, size); |
2843 | if (err < 0) | |
2844 | return err; | |
2845 | ||
99c55f7d AS |
2846 | switch (cmd) { |
2847 | case BPF_MAP_CREATE: | |
2848 | err = map_create(&attr); | |
2849 | break; | |
db20fd2b AS |
2850 | case BPF_MAP_LOOKUP_ELEM: |
2851 | err = map_lookup_elem(&attr); | |
2852 | break; | |
2853 | case BPF_MAP_UPDATE_ELEM: | |
2854 | err = map_update_elem(&attr); | |
2855 | break; | |
2856 | case BPF_MAP_DELETE_ELEM: | |
2857 | err = map_delete_elem(&attr); | |
2858 | break; | |
2859 | case BPF_MAP_GET_NEXT_KEY: | |
2860 | err = map_get_next_key(&attr); | |
2861 | break; | |
87df15de DB |
2862 | case BPF_MAP_FREEZE: |
2863 | err = map_freeze(&attr); | |
2864 | break; | |
09756af4 | 2865 | case BPF_PROG_LOAD: |
838e9690 | 2866 | err = bpf_prog_load(&attr, uattr); |
09756af4 | 2867 | break; |
b2197755 DB |
2868 | case BPF_OBJ_PIN: |
2869 | err = bpf_obj_pin(&attr); | |
2870 | break; | |
2871 | case BPF_OBJ_GET: | |
2872 | err = bpf_obj_get(&attr); | |
2873 | break; | |
f4324551 DM |
2874 | case BPF_PROG_ATTACH: |
2875 | err = bpf_prog_attach(&attr); | |
2876 | break; | |
2877 | case BPF_PROG_DETACH: | |
2878 | err = bpf_prog_detach(&attr); | |
2879 | break; | |
468e2f64 AS |
2880 | case BPF_PROG_QUERY: |
2881 | err = bpf_prog_query(&attr, uattr); | |
2882 | break; | |
1cf1cae9 AS |
2883 | case BPF_PROG_TEST_RUN: |
2884 | err = bpf_prog_test_run(&attr, uattr); | |
2885 | break; | |
34ad5580 MKL |
2886 | case BPF_PROG_GET_NEXT_ID: |
2887 | err = bpf_obj_get_next_id(&attr, uattr, | |
2888 | &prog_idr, &prog_idr_lock); | |
2889 | break; | |
2890 | case BPF_MAP_GET_NEXT_ID: | |
2891 | err = bpf_obj_get_next_id(&attr, uattr, | |
2892 | &map_idr, &map_idr_lock); | |
2893 | break; | |
1b9ed84e QM |
2894 | case BPF_BTF_GET_NEXT_ID: |
2895 | err = bpf_obj_get_next_id(&attr, uattr, | |
2896 | &btf_idr, &btf_idr_lock); | |
2897 | break; | |
b16d9aa4 MKL |
2898 | case BPF_PROG_GET_FD_BY_ID: |
2899 | err = bpf_prog_get_fd_by_id(&attr); | |
2900 | break; | |
bd5f5f4e MKL |
2901 | case BPF_MAP_GET_FD_BY_ID: |
2902 | err = bpf_map_get_fd_by_id(&attr); | |
2903 | break; | |
1e270976 MKL |
2904 | case BPF_OBJ_GET_INFO_BY_FD: |
2905 | err = bpf_obj_get_info_by_fd(&attr, uattr); | |
2906 | break; | |
c4f6699d AS |
2907 | case BPF_RAW_TRACEPOINT_OPEN: |
2908 | err = bpf_raw_tracepoint_open(&attr); | |
2909 | break; | |
f56a653c MKL |
2910 | case BPF_BTF_LOAD: |
2911 | err = bpf_btf_load(&attr); | |
2912 | break; | |
78958fca MKL |
2913 | case BPF_BTF_GET_FD_BY_ID: |
2914 | err = bpf_btf_get_fd_by_id(&attr); | |
2915 | break; | |
41bdc4b4 YS |
2916 | case BPF_TASK_FD_QUERY: |
2917 | err = bpf_task_fd_query(&attr, uattr); | |
2918 | break; | |
bd513cd0 MV |
2919 | case BPF_MAP_LOOKUP_AND_DELETE_ELEM: |
2920 | err = map_lookup_and_delete_elem(&attr); | |
2921 | break; | |
99c55f7d AS |
2922 | default: |
2923 | err = -EINVAL; | |
2924 | break; | |
2925 | } | |
2926 | ||
2927 | return err; | |
2928 | } |