]>
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> |
4a1e7c0c | 7 | #include <linux/bpf_verifier.h> |
f56a653c | 8 | #include <linux/btf.h> |
99c55f7d AS |
9 | #include <linux/syscalls.h> |
10 | #include <linux/slab.h> | |
3f07c014 | 11 | #include <linux/sched/signal.h> |
d407bd25 DB |
12 | #include <linux/vmalloc.h> |
13 | #include <linux/mmzone.h> | |
99c55f7d | 14 | #include <linux/anon_inodes.h> |
41bdc4b4 | 15 | #include <linux/fdtable.h> |
db20fd2b | 16 | #include <linux/file.h> |
41bdc4b4 | 17 | #include <linux/fs.h> |
09756af4 AS |
18 | #include <linux/license.h> |
19 | #include <linux/filter.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> |
bae141f5 | 26 | #include <linux/audit.h> |
ccfe29eb | 27 | #include <uapi/linux/btf.h> |
ca5999fd | 28 | #include <linux/pgtable.h> |
9e4e01df | 29 | #include <linux/bpf_lsm.h> |
457f4436 | 30 | #include <linux/poll.h> |
a3fd7cee | 31 | #include <linux/bpf-netns.h> |
1e6c62a8 | 32 | #include <linux/rcupdate_trace.h> |
48edc1f7 | 33 | #include <linux/memcontrol.h> |
99c55f7d | 34 | |
da765a2f DB |
35 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ |
36 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ | |
37 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) | |
38 | #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY) | |
14dc6f04 | 39 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) |
da765a2f DB |
40 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \ |
41 | IS_FD_HASH(map)) | |
14dc6f04 | 42 | |
6e71b04a CF |
43 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
44 | ||
b121d1e7 | 45 | DEFINE_PER_CPU(int, bpf_prog_active); |
dc4bb0e2 MKL |
46 | static DEFINE_IDR(prog_idr); |
47 | static DEFINE_SPINLOCK(prog_idr_lock); | |
f3f1c054 MKL |
48 | static DEFINE_IDR(map_idr); |
49 | static DEFINE_SPINLOCK(map_idr_lock); | |
a3b80e10 AN |
50 | static DEFINE_IDR(link_idr); |
51 | static DEFINE_SPINLOCK(link_idr_lock); | |
b121d1e7 | 52 | |
08389d88 DB |
53 | int sysctl_unprivileged_bpf_disabled __read_mostly = |
54 | IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0; | |
1be7f75d | 55 | |
40077e0c | 56 | static const struct bpf_map_ops * const bpf_map_types[] = { |
91cc1a99 | 57 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) |
40077e0c JB |
58 | #define BPF_MAP_TYPE(_id, _ops) \ |
59 | [_id] = &_ops, | |
f2e10bff | 60 | #define BPF_LINK_TYPE(_id, _name) |
40077e0c JB |
61 | #include <linux/bpf_types.h> |
62 | #undef BPF_PROG_TYPE | |
63 | #undef BPF_MAP_TYPE | |
f2e10bff | 64 | #undef BPF_LINK_TYPE |
40077e0c | 65 | }; |
99c55f7d | 66 | |
752ba56f MS |
67 | /* |
68 | * If we're handed a bigger struct than we know of, ensure all the unknown bits | |
69 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions | |
70 | * we don't know about yet. | |
71 | * | |
72 | * There is a ToCToU between this function call and the following | |
73 | * copy_from_user() call. However, this is not a concern since this function is | |
74 | * meant to be a future-proofing of bits. | |
75 | */ | |
af2ac3e1 | 76 | int bpf_check_uarg_tail_zero(bpfptr_t uaddr, |
dcab51f1 MKL |
77 | size_t expected_size, |
78 | size_t actual_size) | |
58291a74 | 79 | { |
b7e4b65f | 80 | int res; |
58291a74 | 81 | |
752ba56f MS |
82 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
83 | return -E2BIG; | |
84 | ||
58291a74 MS |
85 | if (actual_size <= expected_size) |
86 | return 0; | |
87 | ||
af2ac3e1 AS |
88 | if (uaddr.is_kernel) |
89 | res = memchr_inv(uaddr.kernel + expected_size, 0, | |
90 | actual_size - expected_size) == NULL; | |
91 | else | |
92 | res = check_zeroed_user(uaddr.user + expected_size, | |
93 | actual_size - expected_size); | |
b7e4b65f AV |
94 | if (res < 0) |
95 | return res; | |
96 | return res ? 0 : -E2BIG; | |
58291a74 MS |
97 | } |
98 | ||
a3884572 | 99 | const struct bpf_map_ops bpf_map_offload_ops = { |
f4d05259 | 100 | .map_meta_equal = bpf_map_meta_equal, |
a3884572 JK |
101 | .map_alloc = bpf_map_offload_map_alloc, |
102 | .map_free = bpf_map_offload_map_free, | |
e8d2bec0 | 103 | .map_check_btf = map_check_no_btf, |
a3884572 JK |
104 | }; |
105 | ||
99c55f7d AS |
106 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
107 | { | |
1110f3a9 | 108 | const struct bpf_map_ops *ops; |
9ef09e35 | 109 | u32 type = attr->map_type; |
99c55f7d | 110 | struct bpf_map *map; |
1110f3a9 | 111 | int err; |
99c55f7d | 112 | |
9ef09e35 | 113 | if (type >= ARRAY_SIZE(bpf_map_types)) |
1110f3a9 | 114 | return ERR_PTR(-EINVAL); |
9ef09e35 MR |
115 | type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); |
116 | ops = bpf_map_types[type]; | |
1110f3a9 | 117 | if (!ops) |
40077e0c | 118 | return ERR_PTR(-EINVAL); |
99c55f7d | 119 | |
1110f3a9 JK |
120 | if (ops->map_alloc_check) { |
121 | err = ops->map_alloc_check(attr); | |
122 | if (err) | |
123 | return ERR_PTR(err); | |
124 | } | |
a3884572 JK |
125 | if (attr->map_ifindex) |
126 | ops = &bpf_map_offload_ops; | |
1110f3a9 | 127 | map = ops->map_alloc(attr); |
40077e0c JB |
128 | if (IS_ERR(map)) |
129 | return map; | |
1110f3a9 | 130 | map->ops = ops; |
9ef09e35 | 131 | map->map_type = type; |
40077e0c | 132 | return map; |
99c55f7d AS |
133 | } |
134 | ||
80ee81e0 | 135 | static u32 bpf_map_value_size(const struct bpf_map *map) |
15c14a3d BV |
136 | { |
137 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || | |
138 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || | |
139 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || | |
140 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) | |
141 | return round_up(map->value_size, 8) * num_possible_cpus(); | |
142 | else if (IS_FD_MAP(map)) | |
143 | return sizeof(u32); | |
144 | else | |
145 | return map->value_size; | |
146 | } | |
147 | ||
148 | static void maybe_wait_bpf_programs(struct bpf_map *map) | |
149 | { | |
150 | /* Wait for any running BPF programs to complete so that | |
151 | * userspace, when we return to it, knows that all programs | |
152 | * that could be running use the new map value. | |
153 | */ | |
154 | if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || | |
155 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) | |
156 | synchronize_rcu(); | |
157 | } | |
158 | ||
159 | static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key, | |
160 | void *value, __u64 flags) | |
161 | { | |
162 | int err; | |
163 | ||
164 | /* Need to create a kthread, thus must support schedule */ | |
165 | if (bpf_map_is_dev_bound(map)) { | |
166 | return bpf_map_offload_update_elem(map, key, value, flags); | |
167 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || | |
15c14a3d BV |
168 | map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
169 | return map->ops->map_update_elem(map, key, value, flags); | |
13b79d3f LB |
170 | } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH || |
171 | map->map_type == BPF_MAP_TYPE_SOCKMAP) { | |
172 | return sock_map_update_elem_sys(map, key, value, flags); | |
15c14a3d BV |
173 | } else if (IS_FD_PROG_ARRAY(map)) { |
174 | return bpf_fd_array_map_update_elem(map, f.file, key, value, | |
175 | flags); | |
176 | } | |
177 | ||
b6e5dae1 | 178 | bpf_disable_instrumentation(); |
15c14a3d BV |
179 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
180 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
181 | err = bpf_percpu_hash_update(map, key, value, flags); | |
182 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
183 | err = bpf_percpu_array_update(map, key, value, flags); | |
184 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { | |
185 | err = bpf_percpu_cgroup_storage_update(map, key, value, | |
186 | flags); | |
187 | } else if (IS_FD_ARRAY(map)) { | |
188 | rcu_read_lock(); | |
189 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, | |
190 | flags); | |
191 | rcu_read_unlock(); | |
192 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { | |
193 | rcu_read_lock(); | |
194 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, | |
195 | flags); | |
196 | rcu_read_unlock(); | |
197 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { | |
198 | /* rcu_read_lock() is not needed */ | |
199 | err = bpf_fd_reuseport_array_update_elem(map, key, value, | |
200 | flags); | |
201 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || | |
202 | map->map_type == BPF_MAP_TYPE_STACK) { | |
203 | err = map->ops->map_push_elem(map, value, flags); | |
204 | } else { | |
205 | rcu_read_lock(); | |
206 | err = map->ops->map_update_elem(map, key, value, flags); | |
207 | rcu_read_unlock(); | |
208 | } | |
b6e5dae1 | 209 | bpf_enable_instrumentation(); |
15c14a3d BV |
210 | maybe_wait_bpf_programs(map); |
211 | ||
212 | return err; | |
213 | } | |
214 | ||
215 | static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, | |
216 | __u64 flags) | |
217 | { | |
218 | void *ptr; | |
219 | int err; | |
220 | ||
cb4d03ab BV |
221 | if (bpf_map_is_dev_bound(map)) |
222 | return bpf_map_offload_lookup_elem(map, key, value); | |
15c14a3d | 223 | |
b6e5dae1 | 224 | bpf_disable_instrumentation(); |
15c14a3d BV |
225 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
226 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
227 | err = bpf_percpu_hash_copy(map, key, value); | |
228 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { | |
229 | err = bpf_percpu_array_copy(map, key, value); | |
230 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { | |
231 | err = bpf_percpu_cgroup_storage_copy(map, key, value); | |
232 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { | |
233 | err = bpf_stackmap_copy(map, key, value); | |
234 | } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) { | |
235 | err = bpf_fd_array_map_lookup_elem(map, key, value); | |
236 | } else if (IS_FD_HASH(map)) { | |
237 | err = bpf_fd_htab_map_lookup_elem(map, key, value); | |
238 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { | |
239 | err = bpf_fd_reuseport_array_lookup_elem(map, key, value); | |
240 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || | |
241 | map->map_type == BPF_MAP_TYPE_STACK) { | |
242 | err = map->ops->map_peek_elem(map, value); | |
243 | } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { | |
244 | /* struct_ops map requires directly updating "value" */ | |
245 | err = bpf_struct_ops_map_sys_lookup_elem(map, key, value); | |
246 | } else { | |
247 | rcu_read_lock(); | |
248 | if (map->ops->map_lookup_elem_sys_only) | |
249 | ptr = map->ops->map_lookup_elem_sys_only(map, key); | |
250 | else | |
251 | ptr = map->ops->map_lookup_elem(map, key); | |
252 | if (IS_ERR(ptr)) { | |
253 | err = PTR_ERR(ptr); | |
254 | } else if (!ptr) { | |
255 | err = -ENOENT; | |
256 | } else { | |
257 | err = 0; | |
258 | if (flags & BPF_F_LOCK) | |
259 | /* lock 'ptr' and copy everything but lock */ | |
260 | copy_map_value_locked(map, value, ptr, true); | |
261 | else | |
262 | copy_map_value(map, value, ptr); | |
68134668 AS |
263 | /* mask lock and timer, since value wasn't zero inited */ |
264 | check_and_init_map_value(map, value); | |
15c14a3d BV |
265 | } |
266 | rcu_read_unlock(); | |
267 | } | |
268 | ||
b6e5dae1 | 269 | bpf_enable_instrumentation(); |
15c14a3d BV |
270 | maybe_wait_bpf_programs(map); |
271 | ||
272 | return err; | |
273 | } | |
274 | ||
d5299b67 RG |
275 | /* Please, do not use this function outside from the map creation path |
276 | * (e.g. in map update path) without taking care of setting the active | |
277 | * memory cgroup (see at bpf_map_kmalloc_node() for example). | |
278 | */ | |
196e8ca7 | 279 | static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) |
d407bd25 | 280 | { |
f01a7dbe MP |
281 | /* We really just want to fail instead of triggering OOM killer |
282 | * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, | |
283 | * which is used for lower order allocation requests. | |
284 | * | |
285 | * It has been observed that higher order allocation requests done by | |
286 | * vmalloc with __GFP_NORETRY being set might fail due to not trying | |
287 | * to reclaim memory from the page cache, thus we set | |
288 | * __GFP_RETRY_MAYFAIL to avoid such situations. | |
d407bd25 | 289 | */ |
f01a7dbe | 290 | |
d5299b67 | 291 | const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT; |
041de93f CH |
292 | unsigned int flags = 0; |
293 | unsigned long align = 1; | |
d407bd25 DB |
294 | void *area; |
295 | ||
196e8ca7 DB |
296 | if (size >= SIZE_MAX) |
297 | return NULL; | |
298 | ||
fc970227 | 299 | /* kmalloc()'ed memory can't be mmap()'ed */ |
041de93f CH |
300 | if (mmapable) { |
301 | BUG_ON(!PAGE_ALIGNED(size)); | |
302 | align = SHMLBA; | |
303 | flags = VM_USERMAP; | |
304 | } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { | |
305 | area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY, | |
f01a7dbe | 306 | numa_node); |
d407bd25 DB |
307 | if (area != NULL) |
308 | return area; | |
309 | } | |
041de93f CH |
310 | |
311 | return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, | |
312 | gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL, | |
313 | flags, numa_node, __builtin_return_address(0)); | |
d407bd25 DB |
314 | } |
315 | ||
196e8ca7 | 316 | void *bpf_map_area_alloc(u64 size, int numa_node) |
fc970227 AN |
317 | { |
318 | return __bpf_map_area_alloc(size, numa_node, false); | |
319 | } | |
320 | ||
196e8ca7 | 321 | void *bpf_map_area_mmapable_alloc(u64 size, int numa_node) |
fc970227 AN |
322 | { |
323 | return __bpf_map_area_alloc(size, numa_node, true); | |
324 | } | |
325 | ||
d407bd25 DB |
326 | void bpf_map_area_free(void *area) |
327 | { | |
328 | kvfree(area); | |
329 | } | |
330 | ||
be70bcd5 DB |
331 | static u32 bpf_map_flags_retain_permanent(u32 flags) |
332 | { | |
333 | /* Some map creation flags are not tied to the map object but | |
334 | * rather to the map fd instead, so they have no meaning upon | |
335 | * map object inspection since multiple file descriptors with | |
336 | * different (access) properties can exist here. Thus, given | |
337 | * this has zero meaning for the map itself, lets clear these | |
338 | * from here. | |
339 | */ | |
340 | return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); | |
341 | } | |
342 | ||
bd475643 JK |
343 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
344 | { | |
345 | map->map_type = attr->map_type; | |
346 | map->key_size = attr->key_size; | |
347 | map->value_size = attr->value_size; | |
348 | map->max_entries = attr->max_entries; | |
be70bcd5 | 349 | map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); |
bd475643 JK |
350 | map->numa_node = bpf_map_attr_numa_node(attr); |
351 | } | |
352 | ||
f3f1c054 MKL |
353 | static int bpf_map_alloc_id(struct bpf_map *map) |
354 | { | |
355 | int id; | |
356 | ||
b76354cd | 357 | idr_preload(GFP_KERNEL); |
f3f1c054 MKL |
358 | spin_lock_bh(&map_idr_lock); |
359 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); | |
360 | if (id > 0) | |
361 | map->id = id; | |
362 | spin_unlock_bh(&map_idr_lock); | |
b76354cd | 363 | idr_preload_end(); |
f3f1c054 MKL |
364 | |
365 | if (WARN_ON_ONCE(!id)) | |
366 | return -ENOSPC; | |
367 | ||
368 | return id > 0 ? 0 : id; | |
369 | } | |
370 | ||
a3884572 | 371 | void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) |
f3f1c054 | 372 | { |
930651a7 ED |
373 | unsigned long flags; |
374 | ||
a3884572 JK |
375 | /* Offloaded maps are removed from the IDR store when their device |
376 | * disappears - even if someone holds an fd to them they are unusable, | |
377 | * the memory is gone, all ops will fail; they are simply waiting for | |
378 | * refcnt to drop to be freed. | |
379 | */ | |
380 | if (!map->id) | |
381 | return; | |
382 | ||
bd5f5f4e | 383 | if (do_idr_lock) |
930651a7 | 384 | spin_lock_irqsave(&map_idr_lock, flags); |
bd5f5f4e MKL |
385 | else |
386 | __acquire(&map_idr_lock); | |
387 | ||
f3f1c054 | 388 | idr_remove(&map_idr, map->id); |
a3884572 | 389 | map->id = 0; |
bd5f5f4e MKL |
390 | |
391 | if (do_idr_lock) | |
930651a7 | 392 | spin_unlock_irqrestore(&map_idr_lock, flags); |
bd5f5f4e MKL |
393 | else |
394 | __release(&map_idr_lock); | |
f3f1c054 MKL |
395 | } |
396 | ||
48edc1f7 RG |
397 | #ifdef CONFIG_MEMCG_KMEM |
398 | static void bpf_map_save_memcg(struct bpf_map *map) | |
399 | { | |
400 | map->memcg = get_mem_cgroup_from_mm(current->mm); | |
401 | } | |
402 | ||
403 | static void bpf_map_release_memcg(struct bpf_map *map) | |
404 | { | |
405 | mem_cgroup_put(map->memcg); | |
406 | } | |
407 | ||
408 | void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, | |
409 | int node) | |
410 | { | |
411 | struct mem_cgroup *old_memcg; | |
412 | void *ptr; | |
413 | ||
414 | old_memcg = set_active_memcg(map->memcg); | |
415 | ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node); | |
416 | set_active_memcg(old_memcg); | |
417 | ||
418 | return ptr; | |
419 | } | |
420 | ||
421 | void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags) | |
422 | { | |
423 | struct mem_cgroup *old_memcg; | |
424 | void *ptr; | |
425 | ||
426 | old_memcg = set_active_memcg(map->memcg); | |
427 | ptr = kzalloc(size, flags | __GFP_ACCOUNT); | |
428 | set_active_memcg(old_memcg); | |
429 | ||
430 | return ptr; | |
431 | } | |
432 | ||
433 | void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, | |
434 | size_t align, gfp_t flags) | |
435 | { | |
436 | struct mem_cgroup *old_memcg; | |
437 | void __percpu *ptr; | |
438 | ||
439 | old_memcg = set_active_memcg(map->memcg); | |
440 | ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT); | |
441 | set_active_memcg(old_memcg); | |
442 | ||
443 | return ptr; | |
444 | } | |
445 | ||
446 | #else | |
447 | static void bpf_map_save_memcg(struct bpf_map *map) | |
448 | { | |
449 | } | |
450 | ||
451 | static void bpf_map_release_memcg(struct bpf_map *map) | |
452 | { | |
453 | } | |
454 | #endif | |
455 | ||
99c55f7d AS |
456 | /* called from workqueue */ |
457 | static void bpf_map_free_deferred(struct work_struct *work) | |
458 | { | |
459 | struct bpf_map *map = container_of(work, struct bpf_map, work); | |
460 | ||
afdb09c7 | 461 | security_bpf_map_free(map); |
48edc1f7 | 462 | bpf_map_release_memcg(map); |
99c55f7d AS |
463 | /* implementation dependent freeing */ |
464 | map->ops->map_free(map); | |
465 | } | |
466 | ||
c9da161c DB |
467 | static void bpf_map_put_uref(struct bpf_map *map) |
468 | { | |
1e0bd5a0 | 469 | if (atomic64_dec_and_test(&map->usercnt)) { |
ba6b8de4 JF |
470 | if (map->ops->map_release_uref) |
471 | map->ops->map_release_uref(map); | |
c9da161c DB |
472 | } |
473 | } | |
474 | ||
99c55f7d AS |
475 | /* decrement map refcnt and schedule it for freeing via workqueue |
476 | * (unrelying map implementation ops->map_free() might sleep) | |
477 | */ | |
bd5f5f4e | 478 | static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) |
99c55f7d | 479 | { |
1e0bd5a0 | 480 | if (atomic64_dec_and_test(&map->refcnt)) { |
34ad5580 | 481 | /* bpf_map_free_id() must be called first */ |
bd5f5f4e | 482 | bpf_map_free_id(map, do_idr_lock); |
78958fca | 483 | btf_put(map->btf); |
99c55f7d AS |
484 | INIT_WORK(&map->work, bpf_map_free_deferred); |
485 | schedule_work(&map->work); | |
486 | } | |
487 | } | |
488 | ||
bd5f5f4e MKL |
489 | void bpf_map_put(struct bpf_map *map) |
490 | { | |
491 | __bpf_map_put(map, true); | |
492 | } | |
630a4d38 | 493 | EXPORT_SYMBOL_GPL(bpf_map_put); |
bd5f5f4e | 494 | |
c9da161c | 495 | void bpf_map_put_with_uref(struct bpf_map *map) |
99c55f7d | 496 | { |
c9da161c | 497 | bpf_map_put_uref(map); |
99c55f7d | 498 | bpf_map_put(map); |
c9da161c DB |
499 | } |
500 | ||
501 | static int bpf_map_release(struct inode *inode, struct file *filp) | |
502 | { | |
61d1b6a4 DB |
503 | struct bpf_map *map = filp->private_data; |
504 | ||
505 | if (map->ops->map_release) | |
506 | map->ops->map_release(map, filp); | |
507 | ||
508 | bpf_map_put_with_uref(map); | |
99c55f7d AS |
509 | return 0; |
510 | } | |
511 | ||
87df15de DB |
512 | static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) |
513 | { | |
514 | fmode_t mode = f.file->f_mode; | |
515 | ||
516 | /* Our file permissions may have been overridden by global | |
517 | * map permissions facing syscall side. | |
518 | */ | |
519 | if (READ_ONCE(map->frozen)) | |
520 | mode &= ~FMODE_CAN_WRITE; | |
521 | return mode; | |
522 | } | |
523 | ||
f99bf205 | 524 | #ifdef CONFIG_PROC_FS |
80ee81e0 RG |
525 | /* Provides an approximation of the map's memory footprint. |
526 | * Used only to provide a backward compatibility and display | |
527 | * a reasonable "memlock" info. | |
528 | */ | |
529 | static unsigned long bpf_map_memory_footprint(const struct bpf_map *map) | |
530 | { | |
531 | unsigned long size; | |
532 | ||
533 | size = round_up(map->key_size + bpf_map_value_size(map), 8); | |
534 | ||
535 | return round_up(map->max_entries * size, PAGE_SIZE); | |
536 | } | |
537 | ||
f99bf205 DB |
538 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
539 | { | |
540 | const struct bpf_map *map = filp->private_data; | |
21116b70 | 541 | const struct bpf_array *array; |
2beee5f5 | 542 | u32 type = 0, jited = 0; |
21116b70 DB |
543 | |
544 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { | |
545 | array = container_of(map, struct bpf_array, map); | |
2beee5f5 DB |
546 | type = array->aux->type; |
547 | jited = array->aux->jited; | |
21116b70 | 548 | } |
f99bf205 DB |
549 | |
550 | seq_printf(m, | |
551 | "map_type:\t%u\n" | |
552 | "key_size:\t%u\n" | |
553 | "value_size:\t%u\n" | |
322cea2f | 554 | "max_entries:\t%u\n" |
21116b70 | 555 | "map_flags:\t%#x\n" |
80ee81e0 | 556 | "memlock:\t%lu\n" |
87df15de DB |
557 | "map_id:\t%u\n" |
558 | "frozen:\t%u\n", | |
f99bf205 DB |
559 | map->map_type, |
560 | map->key_size, | |
561 | map->value_size, | |
322cea2f | 562 | map->max_entries, |
21116b70 | 563 | map->map_flags, |
80ee81e0 | 564 | bpf_map_memory_footprint(map), |
87df15de DB |
565 | map->id, |
566 | READ_ONCE(map->frozen)); | |
2beee5f5 DB |
567 | if (type) { |
568 | seq_printf(m, "owner_prog_type:\t%u\n", type); | |
569 | seq_printf(m, "owner_jited:\t%u\n", jited); | |
9780c0ab | 570 | } |
f99bf205 DB |
571 | } |
572 | #endif | |
573 | ||
6e71b04a CF |
574 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
575 | loff_t *ppos) | |
576 | { | |
577 | /* We need this handler such that alloc_file() enables | |
578 | * f_mode with FMODE_CAN_READ. | |
579 | */ | |
580 | return -EINVAL; | |
581 | } | |
582 | ||
583 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, | |
584 | size_t siz, loff_t *ppos) | |
585 | { | |
586 | /* We need this handler such that alloc_file() enables | |
587 | * f_mode with FMODE_CAN_WRITE. | |
588 | */ | |
589 | return -EINVAL; | |
590 | } | |
591 | ||
fc970227 AN |
592 | /* called for any extra memory-mapped regions (except initial) */ |
593 | static void bpf_map_mmap_open(struct vm_area_struct *vma) | |
594 | { | |
595 | struct bpf_map *map = vma->vm_file->private_data; | |
596 | ||
1f6cb19b | 597 | if (vma->vm_flags & VM_MAYWRITE) { |
fc970227 AN |
598 | mutex_lock(&map->freeze_mutex); |
599 | map->writecnt++; | |
600 | mutex_unlock(&map->freeze_mutex); | |
601 | } | |
602 | } | |
603 | ||
604 | /* called for all unmapped memory region (including initial) */ | |
605 | static void bpf_map_mmap_close(struct vm_area_struct *vma) | |
606 | { | |
607 | struct bpf_map *map = vma->vm_file->private_data; | |
608 | ||
1f6cb19b | 609 | if (vma->vm_flags & VM_MAYWRITE) { |
fc970227 AN |
610 | mutex_lock(&map->freeze_mutex); |
611 | map->writecnt--; | |
612 | mutex_unlock(&map->freeze_mutex); | |
613 | } | |
fc970227 AN |
614 | } |
615 | ||
616 | static const struct vm_operations_struct bpf_map_default_vmops = { | |
617 | .open = bpf_map_mmap_open, | |
618 | .close = bpf_map_mmap_close, | |
619 | }; | |
620 | ||
621 | static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma) | |
622 | { | |
623 | struct bpf_map *map = filp->private_data; | |
624 | int err; | |
625 | ||
68134668 AS |
626 | if (!map->ops->map_mmap || map_value_has_spin_lock(map) || |
627 | map_value_has_timer(map)) | |
fc970227 AN |
628 | return -ENOTSUPP; |
629 | ||
630 | if (!(vma->vm_flags & VM_SHARED)) | |
631 | return -EINVAL; | |
632 | ||
633 | mutex_lock(&map->freeze_mutex); | |
634 | ||
dfeb376d AN |
635 | if (vma->vm_flags & VM_WRITE) { |
636 | if (map->frozen) { | |
637 | err = -EPERM; | |
638 | goto out; | |
639 | } | |
640 | /* map is meant to be read-only, so do not allow mapping as | |
641 | * writable, because it's possible to leak a writable page | |
642 | * reference and allows user-space to still modify it after | |
643 | * freezing, while verifier will assume contents do not change | |
644 | */ | |
645 | if (map->map_flags & BPF_F_RDONLY_PROG) { | |
646 | err = -EACCES; | |
647 | goto out; | |
648 | } | |
fc970227 AN |
649 | } |
650 | ||
651 | /* set default open/close callbacks */ | |
652 | vma->vm_ops = &bpf_map_default_vmops; | |
653 | vma->vm_private_data = map; | |
1f6cb19b AN |
654 | vma->vm_flags &= ~VM_MAYEXEC; |
655 | if (!(vma->vm_flags & VM_WRITE)) | |
656 | /* disallow re-mapping with PROT_WRITE */ | |
657 | vma->vm_flags &= ~VM_MAYWRITE; | |
fc970227 AN |
658 | |
659 | err = map->ops->map_mmap(map, vma); | |
660 | if (err) | |
661 | goto out; | |
662 | ||
1f6cb19b | 663 | if (vma->vm_flags & VM_MAYWRITE) |
fc970227 AN |
664 | map->writecnt++; |
665 | out: | |
666 | mutex_unlock(&map->freeze_mutex); | |
667 | return err; | |
668 | } | |
669 | ||
457f4436 AN |
670 | static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts) |
671 | { | |
672 | struct bpf_map *map = filp->private_data; | |
673 | ||
674 | if (map->ops->map_poll) | |
675 | return map->ops->map_poll(map, filp, pts); | |
676 | ||
677 | return EPOLLERR; | |
678 | } | |
679 | ||
f66e448c | 680 | const struct file_operations bpf_map_fops = { |
f99bf205 DB |
681 | #ifdef CONFIG_PROC_FS |
682 | .show_fdinfo = bpf_map_show_fdinfo, | |
683 | #endif | |
684 | .release = bpf_map_release, | |
6e71b04a CF |
685 | .read = bpf_dummy_read, |
686 | .write = bpf_dummy_write, | |
fc970227 | 687 | .mmap = bpf_map_mmap, |
457f4436 | 688 | .poll = bpf_map_poll, |
99c55f7d AS |
689 | }; |
690 | ||
6e71b04a | 691 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
aa79781b | 692 | { |
afdb09c7 CF |
693 | int ret; |
694 | ||
695 | ret = security_bpf_map(map, OPEN_FMODE(flags)); | |
696 | if (ret < 0) | |
697 | return ret; | |
698 | ||
aa79781b | 699 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
6e71b04a CF |
700 | flags | O_CLOEXEC); |
701 | } | |
702 | ||
703 | int bpf_get_file_flag(int flags) | |
704 | { | |
705 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) | |
706 | return -EINVAL; | |
707 | if (flags & BPF_F_RDONLY) | |
708 | return O_RDONLY; | |
709 | if (flags & BPF_F_WRONLY) | |
710 | return O_WRONLY; | |
711 | return O_RDWR; | |
aa79781b DB |
712 | } |
713 | ||
99c55f7d AS |
714 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
715 | #define CHECK_ATTR(CMD) \ | |
716 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ | |
717 | sizeof(attr->CMD##_LAST_FIELD), 0, \ | |
718 | sizeof(*attr) - \ | |
719 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ | |
720 | sizeof(attr->CMD##_LAST_FIELD)) != NULL | |
721 | ||
8e7ae251 MKL |
722 | /* dst and src must have at least "size" number of bytes. |
723 | * Return strlen on success and < 0 on error. | |
cb4d2b3f | 724 | */ |
8e7ae251 | 725 | int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size) |
cb4d2b3f | 726 | { |
8e7ae251 MKL |
727 | const char *end = src + size; |
728 | const char *orig_src = src; | |
cb4d2b3f | 729 | |
8e7ae251 | 730 | memset(dst, 0, size); |
3e0ddc4f | 731 | /* Copy all isalnum(), '_' and '.' chars. */ |
cb4d2b3f | 732 | while (src < end && *src) { |
3e0ddc4f DB |
733 | if (!isalnum(*src) && |
734 | *src != '_' && *src != '.') | |
cb4d2b3f MKL |
735 | return -EINVAL; |
736 | *dst++ = *src++; | |
737 | } | |
738 | ||
8e7ae251 | 739 | /* No '\0' found in "size" number of bytes */ |
cb4d2b3f MKL |
740 | if (src == end) |
741 | return -EINVAL; | |
742 | ||
8e7ae251 | 743 | return src - orig_src; |
cb4d2b3f MKL |
744 | } |
745 | ||
e8d2bec0 | 746 | int map_check_no_btf(const struct bpf_map *map, |
1b2b234b | 747 | const struct btf *btf, |
e8d2bec0 DB |
748 | const struct btf_type *key_type, |
749 | const struct btf_type *value_type) | |
750 | { | |
751 | return -ENOTSUPP; | |
752 | } | |
753 | ||
d83525ca | 754 | static int map_check_btf(struct bpf_map *map, const struct btf *btf, |
e8d2bec0 DB |
755 | u32 btf_key_id, u32 btf_value_id) |
756 | { | |
757 | const struct btf_type *key_type, *value_type; | |
758 | u32 key_size, value_size; | |
759 | int ret = 0; | |
760 | ||
2824ecb7 DB |
761 | /* Some maps allow key to be unspecified. */ |
762 | if (btf_key_id) { | |
763 | key_type = btf_type_id_size(btf, &btf_key_id, &key_size); | |
764 | if (!key_type || key_size != map->key_size) | |
765 | return -EINVAL; | |
766 | } else { | |
767 | key_type = btf_type_by_id(btf, 0); | |
768 | if (!map->ops->map_check_btf) | |
769 | return -EINVAL; | |
770 | } | |
e8d2bec0 DB |
771 | |
772 | value_type = btf_type_id_size(btf, &btf_value_id, &value_size); | |
773 | if (!value_type || value_size != map->value_size) | |
774 | return -EINVAL; | |
775 | ||
d83525ca AS |
776 | map->spin_lock_off = btf_find_spin_lock(btf, value_type); |
777 | ||
778 | if (map_value_has_spin_lock(map)) { | |
591fe988 DB |
779 | if (map->map_flags & BPF_F_RDONLY_PROG) |
780 | return -EACCES; | |
d83525ca | 781 | if (map->map_type != BPF_MAP_TYPE_HASH && |
e16d2f1a | 782 | map->map_type != BPF_MAP_TYPE_ARRAY && |
6ac99e8f | 783 | map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
8ea63684 | 784 | map->map_type != BPF_MAP_TYPE_SK_STORAGE && |
4cf1bc1f KS |
785 | map->map_type != BPF_MAP_TYPE_INODE_STORAGE && |
786 | map->map_type != BPF_MAP_TYPE_TASK_STORAGE) | |
d83525ca AS |
787 | return -ENOTSUPP; |
788 | if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > | |
789 | map->value_size) { | |
790 | WARN_ONCE(1, | |
791 | "verifier bug spin_lock_off %d value_size %d\n", | |
792 | map->spin_lock_off, map->value_size); | |
793 | return -EFAULT; | |
794 | } | |
795 | } | |
796 | ||
68134668 AS |
797 | map->timer_off = btf_find_timer(btf, value_type); |
798 | if (map_value_has_timer(map)) { | |
799 | if (map->map_flags & BPF_F_RDONLY_PROG) | |
800 | return -EACCES; | |
801 | if (map->map_type != BPF_MAP_TYPE_HASH && | |
802 | map->map_type != BPF_MAP_TYPE_LRU_HASH && | |
803 | map->map_type != BPF_MAP_TYPE_ARRAY) | |
804 | return -EOPNOTSUPP; | |
805 | } | |
806 | ||
e8d2bec0 | 807 | if (map->ops->map_check_btf) |
1b2b234b | 808 | ret = map->ops->map_check_btf(map, btf, key_type, value_type); |
e8d2bec0 DB |
809 | |
810 | return ret; | |
811 | } | |
812 | ||
85d33df3 | 813 | #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id |
99c55f7d AS |
814 | /* called via syscall */ |
815 | static int map_create(union bpf_attr *attr) | |
816 | { | |
96eabe7a | 817 | int numa_node = bpf_map_attr_numa_node(attr); |
99c55f7d | 818 | struct bpf_map *map; |
6e71b04a | 819 | int f_flags; |
99c55f7d AS |
820 | int err; |
821 | ||
822 | err = CHECK_ATTR(BPF_MAP_CREATE); | |
823 | if (err) | |
824 | return -EINVAL; | |
825 | ||
85d33df3 MKL |
826 | if (attr->btf_vmlinux_value_type_id) { |
827 | if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS || | |
828 | attr->btf_key_type_id || attr->btf_value_type_id) | |
829 | return -EINVAL; | |
830 | } else if (attr->btf_key_type_id && !attr->btf_value_type_id) { | |
831 | return -EINVAL; | |
832 | } | |
833 | ||
6e71b04a CF |
834 | f_flags = bpf_get_file_flag(attr->map_flags); |
835 | if (f_flags < 0) | |
836 | return f_flags; | |
837 | ||
96eabe7a | 838 | if (numa_node != NUMA_NO_NODE && |
96e5ae4e ED |
839 | ((unsigned int)numa_node >= nr_node_ids || |
840 | !node_online(numa_node))) | |
96eabe7a MKL |
841 | return -EINVAL; |
842 | ||
99c55f7d AS |
843 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
844 | map = find_and_alloc_map(attr); | |
845 | if (IS_ERR(map)) | |
846 | return PTR_ERR(map); | |
847 | ||
8e7ae251 MKL |
848 | err = bpf_obj_name_cpy(map->name, attr->map_name, |
849 | sizeof(attr->map_name)); | |
850 | if (err < 0) | |
b936ca64 | 851 | goto free_map; |
ad5b177b | 852 | |
1e0bd5a0 AN |
853 | atomic64_set(&map->refcnt, 1); |
854 | atomic64_set(&map->usercnt, 1); | |
fc970227 | 855 | mutex_init(&map->freeze_mutex); |
99c55f7d | 856 | |
85d33df3 | 857 | map->spin_lock_off = -EINVAL; |
68134668 | 858 | map->timer_off = -EINVAL; |
85d33df3 MKL |
859 | if (attr->btf_key_type_id || attr->btf_value_type_id || |
860 | /* Even the map's value is a kernel's struct, | |
861 | * the bpf_prog.o must have BTF to begin with | |
862 | * to figure out the corresponding kernel's | |
863 | * counter part. Thus, attr->btf_fd has | |
864 | * to be valid also. | |
865 | */ | |
866 | attr->btf_vmlinux_value_type_id) { | |
a26ca7c9 MKL |
867 | struct btf *btf; |
868 | ||
a26ca7c9 MKL |
869 | btf = btf_get_by_fd(attr->btf_fd); |
870 | if (IS_ERR(btf)) { | |
871 | err = PTR_ERR(btf); | |
b936ca64 | 872 | goto free_map; |
a26ca7c9 | 873 | } |
350a5c4d AS |
874 | if (btf_is_kernel(btf)) { |
875 | btf_put(btf); | |
876 | err = -EACCES; | |
877 | goto free_map; | |
878 | } | |
85d33df3 | 879 | map->btf = btf; |
a26ca7c9 | 880 | |
85d33df3 MKL |
881 | if (attr->btf_value_type_id) { |
882 | err = map_check_btf(map, btf, attr->btf_key_type_id, | |
883 | attr->btf_value_type_id); | |
884 | if (err) | |
885 | goto free_map; | |
a26ca7c9 MKL |
886 | } |
887 | ||
9b2cf328 MKL |
888 | map->btf_key_type_id = attr->btf_key_type_id; |
889 | map->btf_value_type_id = attr->btf_value_type_id; | |
85d33df3 MKL |
890 | map->btf_vmlinux_value_type_id = |
891 | attr->btf_vmlinux_value_type_id; | |
a26ca7c9 MKL |
892 | } |
893 | ||
afdb09c7 | 894 | err = security_bpf_map_alloc(map); |
aaac3ba9 | 895 | if (err) |
b936ca64 | 896 | goto free_map; |
afdb09c7 | 897 | |
f3f1c054 MKL |
898 | err = bpf_map_alloc_id(map); |
899 | if (err) | |
b936ca64 | 900 | goto free_map_sec; |
f3f1c054 | 901 | |
48edc1f7 RG |
902 | bpf_map_save_memcg(map); |
903 | ||
6e71b04a | 904 | err = bpf_map_new_fd(map, f_flags); |
bd5f5f4e MKL |
905 | if (err < 0) { |
906 | /* failed to allocate fd. | |
352d20d6 | 907 | * bpf_map_put_with_uref() is needed because the above |
bd5f5f4e MKL |
908 | * bpf_map_alloc_id() has published the map |
909 | * to the userspace and the userspace may | |
910 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. | |
911 | */ | |
352d20d6 | 912 | bpf_map_put_with_uref(map); |
bd5f5f4e MKL |
913 | return err; |
914 | } | |
99c55f7d AS |
915 | |
916 | return err; | |
917 | ||
afdb09c7 CF |
918 | free_map_sec: |
919 | security_bpf_map_free(map); | |
b936ca64 | 920 | free_map: |
a26ca7c9 | 921 | btf_put(map->btf); |
99c55f7d AS |
922 | map->ops->map_free(map); |
923 | return err; | |
924 | } | |
925 | ||
db20fd2b AS |
926 | /* if error is returned, fd is released. |
927 | * On success caller should complete fd access with matching fdput() | |
928 | */ | |
c2101297 | 929 | struct bpf_map *__bpf_map_get(struct fd f) |
db20fd2b | 930 | { |
db20fd2b AS |
931 | if (!f.file) |
932 | return ERR_PTR(-EBADF); | |
db20fd2b AS |
933 | if (f.file->f_op != &bpf_map_fops) { |
934 | fdput(f); | |
935 | return ERR_PTR(-EINVAL); | |
936 | } | |
937 | ||
c2101297 DB |
938 | return f.file->private_data; |
939 | } | |
940 | ||
1e0bd5a0 | 941 | void bpf_map_inc(struct bpf_map *map) |
c9da161c | 942 | { |
1e0bd5a0 | 943 | atomic64_inc(&map->refcnt); |
c9da161c | 944 | } |
630a4d38 | 945 | EXPORT_SYMBOL_GPL(bpf_map_inc); |
c9da161c | 946 | |
1e0bd5a0 AN |
947 | void bpf_map_inc_with_uref(struct bpf_map *map) |
948 | { | |
949 | atomic64_inc(&map->refcnt); | |
950 | atomic64_inc(&map->usercnt); | |
951 | } | |
952 | EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref); | |
953 | ||
1ed4d924 MKL |
954 | struct bpf_map *bpf_map_get(u32 ufd) |
955 | { | |
956 | struct fd f = fdget(ufd); | |
957 | struct bpf_map *map; | |
958 | ||
959 | map = __bpf_map_get(f); | |
960 | if (IS_ERR(map)) | |
961 | return map; | |
962 | ||
963 | bpf_map_inc(map); | |
964 | fdput(f); | |
965 | ||
966 | return map; | |
967 | } | |
968 | ||
c9da161c | 969 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
c2101297 DB |
970 | { |
971 | struct fd f = fdget(ufd); | |
972 | struct bpf_map *map; | |
973 | ||
974 | map = __bpf_map_get(f); | |
975 | if (IS_ERR(map)) | |
976 | return map; | |
977 | ||
1e0bd5a0 | 978 | bpf_map_inc_with_uref(map); |
c2101297 | 979 | fdput(f); |
db20fd2b AS |
980 | |
981 | return map; | |
982 | } | |
983 | ||
bd5f5f4e | 984 | /* map_idr_lock should have been held */ |
1e0bd5a0 | 985 | static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref) |
bd5f5f4e MKL |
986 | { |
987 | int refold; | |
988 | ||
1e0bd5a0 | 989 | refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0); |
bd5f5f4e MKL |
990 | if (!refold) |
991 | return ERR_PTR(-ENOENT); | |
bd5f5f4e | 992 | if (uref) |
1e0bd5a0 | 993 | atomic64_inc(&map->usercnt); |
bd5f5f4e MKL |
994 | |
995 | return map; | |
996 | } | |
997 | ||
1e0bd5a0 | 998 | struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map) |
b0e4701c SF |
999 | { |
1000 | spin_lock_bh(&map_idr_lock); | |
1e0bd5a0 | 1001 | map = __bpf_map_inc_not_zero(map, false); |
b0e4701c SF |
1002 | spin_unlock_bh(&map_idr_lock); |
1003 | ||
1004 | return map; | |
1005 | } | |
1006 | EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); | |
1007 | ||
b8cdc051 AS |
1008 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
1009 | { | |
1010 | return -ENOTSUPP; | |
1011 | } | |
1012 | ||
c9d29f46 MV |
1013 | static void *__bpf_copy_key(void __user *ukey, u64 key_size) |
1014 | { | |
1015 | if (key_size) | |
44779a4b | 1016 | return vmemdup_user(ukey, key_size); |
c9d29f46 MV |
1017 | |
1018 | if (ukey) | |
1019 | return ERR_PTR(-EINVAL); | |
1020 | ||
1021 | return NULL; | |
1022 | } | |
1023 | ||
af2ac3e1 AS |
1024 | static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size) |
1025 | { | |
1026 | if (key_size) | |
44779a4b | 1027 | return kvmemdup_bpfptr(ukey, key_size); |
af2ac3e1 AS |
1028 | |
1029 | if (!bpfptr_is_null(ukey)) | |
1030 | return ERR_PTR(-EINVAL); | |
1031 | ||
1032 | return NULL; | |
1033 | } | |
1034 | ||
db20fd2b | 1035 | /* last field in 'union bpf_attr' used by this command */ |
96049f3a | 1036 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags |
db20fd2b AS |
1037 | |
1038 | static int map_lookup_elem(union bpf_attr *attr) | |
1039 | { | |
535e7b4b MS |
1040 | void __user *ukey = u64_to_user_ptr(attr->key); |
1041 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
db20fd2b | 1042 | int ufd = attr->map_fd; |
db20fd2b | 1043 | struct bpf_map *map; |
15c14a3d | 1044 | void *key, *value; |
15a07b33 | 1045 | u32 value_size; |
592867bf | 1046 | struct fd f; |
db20fd2b AS |
1047 | int err; |
1048 | ||
1049 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) | |
1050 | return -EINVAL; | |
1051 | ||
96049f3a AS |
1052 | if (attr->flags & ~BPF_F_LOCK) |
1053 | return -EINVAL; | |
1054 | ||
592867bf | 1055 | f = fdget(ufd); |
c2101297 | 1056 | map = __bpf_map_get(f); |
db20fd2b AS |
1057 | if (IS_ERR(map)) |
1058 | return PTR_ERR(map); | |
87df15de | 1059 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
6e71b04a CF |
1060 | err = -EPERM; |
1061 | goto err_put; | |
1062 | } | |
1063 | ||
96049f3a AS |
1064 | if ((attr->flags & BPF_F_LOCK) && |
1065 | !map_value_has_spin_lock(map)) { | |
1066 | err = -EINVAL; | |
1067 | goto err_put; | |
1068 | } | |
1069 | ||
c9d29f46 | 1070 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
1071 | if (IS_ERR(key)) { |
1072 | err = PTR_ERR(key); | |
db20fd2b | 1073 | goto err_put; |
e4448ed8 | 1074 | } |
db20fd2b | 1075 | |
15c14a3d | 1076 | value_size = bpf_map_value_size(map); |
15a07b33 | 1077 | |
8ebe667c | 1078 | err = -ENOMEM; |
f0dce1d9 | 1079 | value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b | 1080 | if (!value) |
8ebe667c AS |
1081 | goto free_key; |
1082 | ||
15c14a3d | 1083 | err = bpf_map_copy_value(map, key, value, attr->flags); |
15a07b33 | 1084 | if (err) |
8ebe667c | 1085 | goto free_value; |
db20fd2b AS |
1086 | |
1087 | err = -EFAULT; | |
15a07b33 | 1088 | if (copy_to_user(uvalue, value, value_size) != 0) |
8ebe667c | 1089 | goto free_value; |
db20fd2b AS |
1090 | |
1091 | err = 0; | |
1092 | ||
8ebe667c | 1093 | free_value: |
f0dce1d9 | 1094 | kvfree(value); |
db20fd2b | 1095 | free_key: |
44779a4b | 1096 | kvfree(key); |
db20fd2b AS |
1097 | err_put: |
1098 | fdput(f); | |
1099 | return err; | |
1100 | } | |
1101 | ||
1ae80cf3 | 1102 | |
3274f520 | 1103 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
db20fd2b | 1104 | |
af2ac3e1 | 1105 | static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr) |
db20fd2b | 1106 | { |
af2ac3e1 AS |
1107 | bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel); |
1108 | bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel); | |
db20fd2b | 1109 | int ufd = attr->map_fd; |
db20fd2b AS |
1110 | struct bpf_map *map; |
1111 | void *key, *value; | |
15a07b33 | 1112 | u32 value_size; |
592867bf | 1113 | struct fd f; |
db20fd2b AS |
1114 | int err; |
1115 | ||
1116 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) | |
1117 | return -EINVAL; | |
1118 | ||
592867bf | 1119 | f = fdget(ufd); |
c2101297 | 1120 | map = __bpf_map_get(f); |
db20fd2b AS |
1121 | if (IS_ERR(map)) |
1122 | return PTR_ERR(map); | |
87df15de | 1123 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
6e71b04a CF |
1124 | err = -EPERM; |
1125 | goto err_put; | |
1126 | } | |
1127 | ||
96049f3a AS |
1128 | if ((attr->flags & BPF_F_LOCK) && |
1129 | !map_value_has_spin_lock(map)) { | |
1130 | err = -EINVAL; | |
1131 | goto err_put; | |
1132 | } | |
1133 | ||
af2ac3e1 | 1134 | key = ___bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
1135 | if (IS_ERR(key)) { |
1136 | err = PTR_ERR(key); | |
db20fd2b | 1137 | goto err_put; |
e4448ed8 | 1138 | } |
db20fd2b | 1139 | |
f0dce1d9 | 1140 | value_size = bpf_map_value_size(map); |
15a07b33 | 1141 | |
db20fd2b | 1142 | err = -ENOMEM; |
f0dce1d9 | 1143 | value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); |
db20fd2b AS |
1144 | if (!value) |
1145 | goto free_key; | |
1146 | ||
1147 | err = -EFAULT; | |
af2ac3e1 | 1148 | if (copy_from_bpfptr(value, uvalue, value_size) != 0) |
db20fd2b AS |
1149 | goto free_value; |
1150 | ||
15c14a3d | 1151 | err = bpf_map_update_value(map, f, key, value, attr->flags); |
6710e112 | 1152 | |
db20fd2b | 1153 | free_value: |
f0dce1d9 | 1154 | kvfree(value); |
db20fd2b | 1155 | free_key: |
44779a4b | 1156 | kvfree(key); |
db20fd2b AS |
1157 | err_put: |
1158 | fdput(f); | |
1159 | return err; | |
1160 | } | |
1161 | ||
1162 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key | |
1163 | ||
1164 | static int map_delete_elem(union bpf_attr *attr) | |
1165 | { | |
535e7b4b | 1166 | void __user *ukey = u64_to_user_ptr(attr->key); |
db20fd2b | 1167 | int ufd = attr->map_fd; |
db20fd2b | 1168 | struct bpf_map *map; |
592867bf | 1169 | struct fd f; |
db20fd2b AS |
1170 | void *key; |
1171 | int err; | |
1172 | ||
1173 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) | |
1174 | return -EINVAL; | |
1175 | ||
592867bf | 1176 | f = fdget(ufd); |
c2101297 | 1177 | map = __bpf_map_get(f); |
db20fd2b AS |
1178 | if (IS_ERR(map)) |
1179 | return PTR_ERR(map); | |
87df15de | 1180 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
6e71b04a CF |
1181 | err = -EPERM; |
1182 | goto err_put; | |
1183 | } | |
1184 | ||
c9d29f46 | 1185 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
1186 | if (IS_ERR(key)) { |
1187 | err = PTR_ERR(key); | |
db20fd2b | 1188 | goto err_put; |
e4448ed8 | 1189 | } |
db20fd2b | 1190 | |
a3884572 JK |
1191 | if (bpf_map_is_dev_bound(map)) { |
1192 | err = bpf_map_offload_delete_elem(map, key); | |
1193 | goto out; | |
85d33df3 MKL |
1194 | } else if (IS_FD_PROG_ARRAY(map) || |
1195 | map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { | |
1196 | /* These maps require sleepable context */ | |
da765a2f DB |
1197 | err = map->ops->map_delete_elem(map, key); |
1198 | goto out; | |
a3884572 JK |
1199 | } |
1200 | ||
b6e5dae1 | 1201 | bpf_disable_instrumentation(); |
db20fd2b AS |
1202 | rcu_read_lock(); |
1203 | err = map->ops->map_delete_elem(map, key); | |
1204 | rcu_read_unlock(); | |
b6e5dae1 | 1205 | bpf_enable_instrumentation(); |
1ae80cf3 | 1206 | maybe_wait_bpf_programs(map); |
a3884572 | 1207 | out: |
44779a4b | 1208 | kvfree(key); |
db20fd2b AS |
1209 | err_put: |
1210 | fdput(f); | |
1211 | return err; | |
1212 | } | |
1213 | ||
1214 | /* last field in 'union bpf_attr' used by this command */ | |
1215 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key | |
1216 | ||
1217 | static int map_get_next_key(union bpf_attr *attr) | |
1218 | { | |
535e7b4b MS |
1219 | void __user *ukey = u64_to_user_ptr(attr->key); |
1220 | void __user *unext_key = u64_to_user_ptr(attr->next_key); | |
db20fd2b | 1221 | int ufd = attr->map_fd; |
db20fd2b AS |
1222 | struct bpf_map *map; |
1223 | void *key, *next_key; | |
592867bf | 1224 | struct fd f; |
db20fd2b AS |
1225 | int err; |
1226 | ||
1227 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) | |
1228 | return -EINVAL; | |
1229 | ||
592867bf | 1230 | f = fdget(ufd); |
c2101297 | 1231 | map = __bpf_map_get(f); |
db20fd2b AS |
1232 | if (IS_ERR(map)) |
1233 | return PTR_ERR(map); | |
87df15de | 1234 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
6e71b04a CF |
1235 | err = -EPERM; |
1236 | goto err_put; | |
1237 | } | |
1238 | ||
8fe45924 | 1239 | if (ukey) { |
c9d29f46 | 1240 | key = __bpf_copy_key(ukey, map->key_size); |
e4448ed8 AV |
1241 | if (IS_ERR(key)) { |
1242 | err = PTR_ERR(key); | |
8fe45924 | 1243 | goto err_put; |
e4448ed8 | 1244 | } |
8fe45924 TQ |
1245 | } else { |
1246 | key = NULL; | |
1247 | } | |
db20fd2b AS |
1248 | |
1249 | err = -ENOMEM; | |
44779a4b | 1250 | next_key = kvmalloc(map->key_size, GFP_USER); |
db20fd2b AS |
1251 | if (!next_key) |
1252 | goto free_key; | |
1253 | ||
a3884572 JK |
1254 | if (bpf_map_is_dev_bound(map)) { |
1255 | err = bpf_map_offload_get_next_key(map, key, next_key); | |
1256 | goto out; | |
1257 | } | |
1258 | ||
db20fd2b AS |
1259 | rcu_read_lock(); |
1260 | err = map->ops->map_get_next_key(map, key, next_key); | |
1261 | rcu_read_unlock(); | |
a3884572 | 1262 | out: |
db20fd2b AS |
1263 | if (err) |
1264 | goto free_next_key; | |
1265 | ||
1266 | err = -EFAULT; | |
1267 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) | |
1268 | goto free_next_key; | |
1269 | ||
1270 | err = 0; | |
1271 | ||
1272 | free_next_key: | |
44779a4b | 1273 | kvfree(next_key); |
db20fd2b | 1274 | free_key: |
44779a4b | 1275 | kvfree(key); |
db20fd2b AS |
1276 | err_put: |
1277 | fdput(f); | |
1278 | return err; | |
1279 | } | |
1280 | ||
aa2e93b8 BV |
1281 | int generic_map_delete_batch(struct bpf_map *map, |
1282 | const union bpf_attr *attr, | |
1283 | union bpf_attr __user *uattr) | |
1284 | { | |
1285 | void __user *keys = u64_to_user_ptr(attr->batch.keys); | |
1286 | u32 cp, max_count; | |
1287 | int err = 0; | |
1288 | void *key; | |
1289 | ||
1290 | if (attr->batch.elem_flags & ~BPF_F_LOCK) | |
1291 | return -EINVAL; | |
1292 | ||
1293 | if ((attr->batch.elem_flags & BPF_F_LOCK) && | |
1294 | !map_value_has_spin_lock(map)) { | |
1295 | return -EINVAL; | |
1296 | } | |
1297 | ||
1298 | max_count = attr->batch.count; | |
1299 | if (!max_count) | |
1300 | return 0; | |
1301 | ||
44779a4b | 1302 | key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); |
2e3a94aa BV |
1303 | if (!key) |
1304 | return -ENOMEM; | |
1305 | ||
aa2e93b8 | 1306 | for (cp = 0; cp < max_count; cp++) { |
2e3a94aa BV |
1307 | err = -EFAULT; |
1308 | if (copy_from_user(key, keys + cp * map->key_size, | |
1309 | map->key_size)) | |
aa2e93b8 | 1310 | break; |
aa2e93b8 BV |
1311 | |
1312 | if (bpf_map_is_dev_bound(map)) { | |
1313 | err = bpf_map_offload_delete_elem(map, key); | |
1314 | break; | |
1315 | } | |
1316 | ||
b6e5dae1 | 1317 | bpf_disable_instrumentation(); |
aa2e93b8 BV |
1318 | rcu_read_lock(); |
1319 | err = map->ops->map_delete_elem(map, key); | |
1320 | rcu_read_unlock(); | |
b6e5dae1 | 1321 | bpf_enable_instrumentation(); |
aa2e93b8 BV |
1322 | maybe_wait_bpf_programs(map); |
1323 | if (err) | |
1324 | break; | |
1325 | } | |
1326 | if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) | |
1327 | err = -EFAULT; | |
2e3a94aa | 1328 | |
44779a4b | 1329 | kvfree(key); |
aa2e93b8 BV |
1330 | return err; |
1331 | } | |
1332 | ||
1333 | int generic_map_update_batch(struct bpf_map *map, | |
1334 | const union bpf_attr *attr, | |
1335 | union bpf_attr __user *uattr) | |
1336 | { | |
1337 | void __user *values = u64_to_user_ptr(attr->batch.values); | |
1338 | void __user *keys = u64_to_user_ptr(attr->batch.keys); | |
1339 | u32 value_size, cp, max_count; | |
1340 | int ufd = attr->map_fd; | |
1341 | void *key, *value; | |
1342 | struct fd f; | |
1343 | int err = 0; | |
1344 | ||
1345 | f = fdget(ufd); | |
1346 | if (attr->batch.elem_flags & ~BPF_F_LOCK) | |
1347 | return -EINVAL; | |
1348 | ||
1349 | if ((attr->batch.elem_flags & BPF_F_LOCK) && | |
1350 | !map_value_has_spin_lock(map)) { | |
1351 | return -EINVAL; | |
1352 | } | |
1353 | ||
1354 | value_size = bpf_map_value_size(map); | |
1355 | ||
1356 | max_count = attr->batch.count; | |
1357 | if (!max_count) | |
1358 | return 0; | |
1359 | ||
44779a4b | 1360 | key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); |
2e3a94aa BV |
1361 | if (!key) |
1362 | return -ENOMEM; | |
1363 | ||
f0dce1d9 | 1364 | value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); |
2e3a94aa | 1365 | if (!value) { |
44779a4b | 1366 | kvfree(key); |
aa2e93b8 | 1367 | return -ENOMEM; |
2e3a94aa | 1368 | } |
aa2e93b8 BV |
1369 | |
1370 | for (cp = 0; cp < max_count; cp++) { | |
aa2e93b8 | 1371 | err = -EFAULT; |
2e3a94aa BV |
1372 | if (copy_from_user(key, keys + cp * map->key_size, |
1373 | map->key_size) || | |
1374 | copy_from_user(value, values + cp * value_size, value_size)) | |
aa2e93b8 BV |
1375 | break; |
1376 | ||
1377 | err = bpf_map_update_value(map, f, key, value, | |
1378 | attr->batch.elem_flags); | |
1379 | ||
1380 | if (err) | |
1381 | break; | |
1382 | } | |
1383 | ||
1384 | if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp))) | |
1385 | err = -EFAULT; | |
1386 | ||
f0dce1d9 | 1387 | kvfree(value); |
44779a4b | 1388 | kvfree(key); |
aa2e93b8 BV |
1389 | return err; |
1390 | } | |
1391 | ||
cb4d03ab BV |
1392 | #define MAP_LOOKUP_RETRIES 3 |
1393 | ||
1394 | int generic_map_lookup_batch(struct bpf_map *map, | |
1395 | const union bpf_attr *attr, | |
1396 | union bpf_attr __user *uattr) | |
1397 | { | |
1398 | void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch); | |
1399 | void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch); | |
1400 | void __user *values = u64_to_user_ptr(attr->batch.values); | |
1401 | void __user *keys = u64_to_user_ptr(attr->batch.keys); | |
1402 | void *buf, *buf_prevkey, *prev_key, *key, *value; | |
1403 | int err, retry = MAP_LOOKUP_RETRIES; | |
1404 | u32 value_size, cp, max_count; | |
cb4d03ab BV |
1405 | |
1406 | if (attr->batch.elem_flags & ~BPF_F_LOCK) | |
1407 | return -EINVAL; | |
1408 | ||
1409 | if ((attr->batch.elem_flags & BPF_F_LOCK) && | |
1410 | !map_value_has_spin_lock(map)) | |
1411 | return -EINVAL; | |
1412 | ||
1413 | value_size = bpf_map_value_size(map); | |
1414 | ||
1415 | max_count = attr->batch.count; | |
1416 | if (!max_count) | |
1417 | return 0; | |
1418 | ||
1419 | if (put_user(0, &uattr->batch.count)) | |
1420 | return -EFAULT; | |
1421 | ||
44779a4b | 1422 | buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); |
cb4d03ab BV |
1423 | if (!buf_prevkey) |
1424 | return -ENOMEM; | |
1425 | ||
f0dce1d9 | 1426 | buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN); |
cb4d03ab | 1427 | if (!buf) { |
44779a4b | 1428 | kvfree(buf_prevkey); |
cb4d03ab BV |
1429 | return -ENOMEM; |
1430 | } | |
1431 | ||
1432 | err = -EFAULT; | |
cb4d03ab BV |
1433 | prev_key = NULL; |
1434 | if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size)) | |
1435 | goto free_buf; | |
1436 | key = buf; | |
1437 | value = key + map->key_size; | |
1438 | if (ubatch) | |
1439 | prev_key = buf_prevkey; | |
1440 | ||
1441 | for (cp = 0; cp < max_count;) { | |
1442 | rcu_read_lock(); | |
1443 | err = map->ops->map_get_next_key(map, prev_key, key); | |
1444 | rcu_read_unlock(); | |
1445 | if (err) | |
1446 | break; | |
1447 | err = bpf_map_copy_value(map, key, value, | |
1448 | attr->batch.elem_flags); | |
1449 | ||
1450 | if (err == -ENOENT) { | |
1451 | if (retry) { | |
1452 | retry--; | |
1453 | continue; | |
1454 | } | |
1455 | err = -EINTR; | |
1456 | break; | |
1457 | } | |
1458 | ||
1459 | if (err) | |
1460 | goto free_buf; | |
1461 | ||
1462 | if (copy_to_user(keys + cp * map->key_size, key, | |
1463 | map->key_size)) { | |
1464 | err = -EFAULT; | |
1465 | goto free_buf; | |
1466 | } | |
1467 | if (copy_to_user(values + cp * value_size, value, value_size)) { | |
1468 | err = -EFAULT; | |
1469 | goto free_buf; | |
1470 | } | |
1471 | ||
1472 | if (!prev_key) | |
1473 | prev_key = buf_prevkey; | |
1474 | ||
1475 | swap(prev_key, key); | |
1476 | retry = MAP_LOOKUP_RETRIES; | |
1477 | cp++; | |
1478 | } | |
1479 | ||
1480 | if (err == -EFAULT) | |
1481 | goto free_buf; | |
1482 | ||
1483 | if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) || | |
1484 | (cp && copy_to_user(uobatch, prev_key, map->key_size)))) | |
1485 | err = -EFAULT; | |
1486 | ||
1487 | free_buf: | |
44779a4b | 1488 | kvfree(buf_prevkey); |
f0dce1d9 | 1489 | kvfree(buf); |
cb4d03ab BV |
1490 | return err; |
1491 | } | |
1492 | ||
3e87f192 | 1493 | #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags |
bd513cd0 MV |
1494 | |
1495 | static int map_lookup_and_delete_elem(union bpf_attr *attr) | |
1496 | { | |
1497 | void __user *ukey = u64_to_user_ptr(attr->key); | |
1498 | void __user *uvalue = u64_to_user_ptr(attr->value); | |
1499 | int ufd = attr->map_fd; | |
1500 | struct bpf_map *map; | |
540fefc0 | 1501 | void *key, *value; |
bd513cd0 MV |
1502 | u32 value_size; |
1503 | struct fd f; | |
1504 | int err; | |
1505 | ||
1506 | if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) | |
1507 | return -EINVAL; | |
1508 | ||
3e87f192 DS |
1509 | if (attr->flags & ~BPF_F_LOCK) |
1510 | return -EINVAL; | |
1511 | ||
bd513cd0 MV |
1512 | f = fdget(ufd); |
1513 | map = __bpf_map_get(f); | |
1514 | if (IS_ERR(map)) | |
1515 | return PTR_ERR(map); | |
1ea0f912 AP |
1516 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) || |
1517 | !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { | |
bd513cd0 MV |
1518 | err = -EPERM; |
1519 | goto err_put; | |
1520 | } | |
1521 | ||
3e87f192 DS |
1522 | if (attr->flags && |
1523 | (map->map_type == BPF_MAP_TYPE_QUEUE || | |
1524 | map->map_type == BPF_MAP_TYPE_STACK)) { | |
1525 | err = -EINVAL; | |
1526 | goto err_put; | |
1527 | } | |
1528 | ||
1529 | if ((attr->flags & BPF_F_LOCK) && | |
1530 | !map_value_has_spin_lock(map)) { | |
1531 | err = -EINVAL; | |
1532 | goto err_put; | |
1533 | } | |
1534 | ||
bd513cd0 MV |
1535 | key = __bpf_copy_key(ukey, map->key_size); |
1536 | if (IS_ERR(key)) { | |
1537 | err = PTR_ERR(key); | |
1538 | goto err_put; | |
1539 | } | |
1540 | ||
3e87f192 | 1541 | value_size = bpf_map_value_size(map); |
bd513cd0 MV |
1542 | |
1543 | err = -ENOMEM; | |
f0dce1d9 | 1544 | value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN); |
bd513cd0 MV |
1545 | if (!value) |
1546 | goto free_key; | |
1547 | ||
3e87f192 | 1548 | err = -ENOTSUPP; |
bd513cd0 MV |
1549 | if (map->map_type == BPF_MAP_TYPE_QUEUE || |
1550 | map->map_type == BPF_MAP_TYPE_STACK) { | |
1551 | err = map->ops->map_pop_elem(map, value); | |
3e87f192 DS |
1552 | } else if (map->map_type == BPF_MAP_TYPE_HASH || |
1553 | map->map_type == BPF_MAP_TYPE_PERCPU_HASH || | |
1554 | map->map_type == BPF_MAP_TYPE_LRU_HASH || | |
1555 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { | |
1556 | if (!bpf_map_is_dev_bound(map)) { | |
1557 | bpf_disable_instrumentation(); | |
1558 | rcu_read_lock(); | |
1559 | err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags); | |
1560 | rcu_read_unlock(); | |
1561 | bpf_enable_instrumentation(); | |
1562 | } | |
bd513cd0 MV |
1563 | } |
1564 | ||
1565 | if (err) | |
1566 | goto free_value; | |
1567 | ||
7f645462 WY |
1568 | if (copy_to_user(uvalue, value, value_size) != 0) { |
1569 | err = -EFAULT; | |
bd513cd0 | 1570 | goto free_value; |
7f645462 | 1571 | } |
bd513cd0 MV |
1572 | |
1573 | err = 0; | |
1574 | ||
1575 | free_value: | |
f0dce1d9 | 1576 | kvfree(value); |
bd513cd0 | 1577 | free_key: |
44779a4b | 1578 | kvfree(key); |
bd513cd0 MV |
1579 | err_put: |
1580 | fdput(f); | |
1581 | return err; | |
1582 | } | |
1583 | ||
87df15de DB |
1584 | #define BPF_MAP_FREEZE_LAST_FIELD map_fd |
1585 | ||
1586 | static int map_freeze(const union bpf_attr *attr) | |
1587 | { | |
1588 | int err = 0, ufd = attr->map_fd; | |
1589 | struct bpf_map *map; | |
1590 | struct fd f; | |
1591 | ||
1592 | if (CHECK_ATTR(BPF_MAP_FREEZE)) | |
1593 | return -EINVAL; | |
1594 | ||
1595 | f = fdget(ufd); | |
1596 | map = __bpf_map_get(f); | |
1597 | if (IS_ERR(map)) | |
1598 | return PTR_ERR(map); | |
fc970227 | 1599 | |
68134668 AS |
1600 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS || |
1601 | map_value_has_timer(map)) { | |
849b4d94 MKL |
1602 | fdput(f); |
1603 | return -ENOTSUPP; | |
1604 | } | |
1605 | ||
fc970227 AN |
1606 | mutex_lock(&map->freeze_mutex); |
1607 | ||
1608 | if (map->writecnt) { | |
1609 | err = -EBUSY; | |
1610 | goto err_put; | |
1611 | } | |
87df15de DB |
1612 | if (READ_ONCE(map->frozen)) { |
1613 | err = -EBUSY; | |
1614 | goto err_put; | |
1615 | } | |
2c78ee89 | 1616 | if (!bpf_capable()) { |
87df15de DB |
1617 | err = -EPERM; |
1618 | goto err_put; | |
1619 | } | |
1620 | ||
1621 | WRITE_ONCE(map->frozen, true); | |
1622 | err_put: | |
fc970227 | 1623 | mutex_unlock(&map->freeze_mutex); |
87df15de DB |
1624 | fdput(f); |
1625 | return err; | |
1626 | } | |
1627 | ||
7de16e3a | 1628 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
91cc1a99 | 1629 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
7de16e3a JK |
1630 | [_id] = & _name ## _prog_ops, |
1631 | #define BPF_MAP_TYPE(_id, _ops) | |
f2e10bff | 1632 | #define BPF_LINK_TYPE(_id, _name) |
7de16e3a JK |
1633 | #include <linux/bpf_types.h> |
1634 | #undef BPF_PROG_TYPE | |
1635 | #undef BPF_MAP_TYPE | |
f2e10bff | 1636 | #undef BPF_LINK_TYPE |
7de16e3a JK |
1637 | }; |
1638 | ||
09756af4 AS |
1639 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
1640 | { | |
d0f1a451 DB |
1641 | const struct bpf_prog_ops *ops; |
1642 | ||
1643 | if (type >= ARRAY_SIZE(bpf_prog_types)) | |
1644 | return -EINVAL; | |
1645 | type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); | |
1646 | ops = bpf_prog_types[type]; | |
1647 | if (!ops) | |
be9370a7 | 1648 | return -EINVAL; |
09756af4 | 1649 | |
ab3f0063 | 1650 | if (!bpf_prog_is_dev_bound(prog->aux)) |
d0f1a451 | 1651 | prog->aux->ops = ops; |
ab3f0063 JK |
1652 | else |
1653 | prog->aux->ops = &bpf_offload_prog_ops; | |
be9370a7 JB |
1654 | prog->type = type; |
1655 | return 0; | |
09756af4 AS |
1656 | } |
1657 | ||
bae141f5 DB |
1658 | enum bpf_audit { |
1659 | BPF_AUDIT_LOAD, | |
1660 | BPF_AUDIT_UNLOAD, | |
1661 | BPF_AUDIT_MAX, | |
1662 | }; | |
1663 | ||
1664 | static const char * const bpf_audit_str[BPF_AUDIT_MAX] = { | |
1665 | [BPF_AUDIT_LOAD] = "LOAD", | |
1666 | [BPF_AUDIT_UNLOAD] = "UNLOAD", | |
1667 | }; | |
1668 | ||
1669 | static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op) | |
1670 | { | |
1671 | struct audit_context *ctx = NULL; | |
1672 | struct audit_buffer *ab; | |
1673 | ||
1674 | if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX)) | |
1675 | return; | |
1676 | if (audit_enabled == AUDIT_OFF) | |
1677 | return; | |
1678 | if (op == BPF_AUDIT_LOAD) | |
1679 | ctx = audit_context(); | |
1680 | ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF); | |
1681 | if (unlikely(!ab)) | |
1682 | return; | |
1683 | audit_log_format(ab, "prog-id=%u op=%s", | |
1684 | prog->aux->id, bpf_audit_str[op]); | |
1685 | audit_log_end(ab); | |
1686 | } | |
1687 | ||
dc4bb0e2 MKL |
1688 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
1689 | { | |
1690 | int id; | |
1691 | ||
b76354cd | 1692 | idr_preload(GFP_KERNEL); |
dc4bb0e2 MKL |
1693 | spin_lock_bh(&prog_idr_lock); |
1694 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); | |
1695 | if (id > 0) | |
1696 | prog->aux->id = id; | |
1697 | spin_unlock_bh(&prog_idr_lock); | |
b76354cd | 1698 | idr_preload_end(); |
dc4bb0e2 MKL |
1699 | |
1700 | /* id is in [1, INT_MAX) */ | |
1701 | if (WARN_ON_ONCE(!id)) | |
1702 | return -ENOSPC; | |
1703 | ||
1704 | return id > 0 ? 0 : id; | |
1705 | } | |
1706 | ||
ad8ad79f | 1707 | void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) |
dc4bb0e2 | 1708 | { |
d809e134 AS |
1709 | unsigned long flags; |
1710 | ||
ad8ad79f JK |
1711 | /* cBPF to eBPF migrations are currently not in the idr store. |
1712 | * Offloaded programs are removed from the store when their device | |
1713 | * disappears - even if someone grabs an fd to them they are unusable, | |
1714 | * simply waiting for refcnt to drop to be freed. | |
1715 | */ | |
dc4bb0e2 MKL |
1716 | if (!prog->aux->id) |
1717 | return; | |
1718 | ||
b16d9aa4 | 1719 | if (do_idr_lock) |
d809e134 | 1720 | spin_lock_irqsave(&prog_idr_lock, flags); |
b16d9aa4 MKL |
1721 | else |
1722 | __acquire(&prog_idr_lock); | |
1723 | ||
dc4bb0e2 | 1724 | idr_remove(&prog_idr, prog->aux->id); |
ad8ad79f | 1725 | prog->aux->id = 0; |
b16d9aa4 MKL |
1726 | |
1727 | if (do_idr_lock) | |
d809e134 | 1728 | spin_unlock_irqrestore(&prog_idr_lock, flags); |
b16d9aa4 MKL |
1729 | else |
1730 | __release(&prog_idr_lock); | |
dc4bb0e2 MKL |
1731 | } |
1732 | ||
1aacde3d | 1733 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
abf2e7d6 AS |
1734 | { |
1735 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); | |
1736 | ||
3b4d9eb2 | 1737 | kvfree(aux->func_info); |
8c1b6e69 | 1738 | kfree(aux->func_info_aux); |
3ac1f01b | 1739 | free_uid(aux->user); |
afdb09c7 | 1740 | security_bpf_prog_free(aux); |
abf2e7d6 AS |
1741 | bpf_prog_free(aux->prog); |
1742 | } | |
1743 | ||
cd7455f1 DB |
1744 | static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred) |
1745 | { | |
1746 | bpf_prog_kallsyms_del_all(prog); | |
1747 | btf_put(prog->aux->btf); | |
e16301fb MKL |
1748 | kvfree(prog->aux->jited_linfo); |
1749 | kvfree(prog->aux->linfo); | |
e6ac2450 | 1750 | kfree(prog->aux->kfunc_tab); |
22dc4a0f AN |
1751 | if (prog->aux->attach_btf) |
1752 | btf_put(prog->aux->attach_btf); | |
cd7455f1 | 1753 | |
1e6c62a8 AS |
1754 | if (deferred) { |
1755 | if (prog->aux->sleepable) | |
1756 | call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu); | |
1757 | else | |
1758 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); | |
1759 | } else { | |
cd7455f1 | 1760 | __bpf_prog_put_rcu(&prog->aux->rcu); |
1e6c62a8 | 1761 | } |
cd7455f1 DB |
1762 | } |
1763 | ||
d809e134 AS |
1764 | static void bpf_prog_put_deferred(struct work_struct *work) |
1765 | { | |
1766 | struct bpf_prog_aux *aux; | |
1767 | struct bpf_prog *prog; | |
1768 | ||
1769 | aux = container_of(work, struct bpf_prog_aux, work); | |
1770 | prog = aux->prog; | |
1771 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); | |
1772 | bpf_audit_prog(prog, BPF_AUDIT_UNLOAD); | |
1773 | __bpf_prog_put_noref(prog, true); | |
1774 | } | |
1775 | ||
b16d9aa4 | 1776 | static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) |
09756af4 | 1777 | { |
d809e134 AS |
1778 | struct bpf_prog_aux *aux = prog->aux; |
1779 | ||
1780 | if (atomic64_dec_and_test(&aux->refcnt)) { | |
34ad5580 | 1781 | /* bpf_prog_free_id() must be called first */ |
b16d9aa4 | 1782 | bpf_prog_free_id(prog, do_idr_lock); |
d809e134 AS |
1783 | |
1784 | if (in_irq() || irqs_disabled()) { | |
1785 | INIT_WORK(&aux->work, bpf_prog_put_deferred); | |
1786 | schedule_work(&aux->work); | |
1787 | } else { | |
1788 | bpf_prog_put_deferred(&aux->work); | |
1789 | } | |
a67edbf4 | 1790 | } |
09756af4 | 1791 | } |
b16d9aa4 MKL |
1792 | |
1793 | void bpf_prog_put(struct bpf_prog *prog) | |
1794 | { | |
1795 | __bpf_prog_put(prog, true); | |
1796 | } | |
e2e9b654 | 1797 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
09756af4 AS |
1798 | |
1799 | static int bpf_prog_release(struct inode *inode, struct file *filp) | |
1800 | { | |
1801 | struct bpf_prog *prog = filp->private_data; | |
1802 | ||
1aacde3d | 1803 | bpf_prog_put(prog); |
09756af4 AS |
1804 | return 0; |
1805 | } | |
1806 | ||
492ecee8 AS |
1807 | static void bpf_prog_get_stats(const struct bpf_prog *prog, |
1808 | struct bpf_prog_stats *stats) | |
1809 | { | |
9ed9e9ba | 1810 | u64 nsecs = 0, cnt = 0, misses = 0; |
492ecee8 AS |
1811 | int cpu; |
1812 | ||
1813 | for_each_possible_cpu(cpu) { | |
1814 | const struct bpf_prog_stats *st; | |
1815 | unsigned int start; | |
9ed9e9ba | 1816 | u64 tnsecs, tcnt, tmisses; |
492ecee8 | 1817 | |
700d4796 | 1818 | st = per_cpu_ptr(prog->stats, cpu); |
492ecee8 AS |
1819 | do { |
1820 | start = u64_stats_fetch_begin_irq(&st->syncp); | |
1821 | tnsecs = st->nsecs; | |
1822 | tcnt = st->cnt; | |
9ed9e9ba | 1823 | tmisses = st->misses; |
492ecee8 AS |
1824 | } while (u64_stats_fetch_retry_irq(&st->syncp, start)); |
1825 | nsecs += tnsecs; | |
1826 | cnt += tcnt; | |
9ed9e9ba | 1827 | misses += tmisses; |
492ecee8 AS |
1828 | } |
1829 | stats->nsecs = nsecs; | |
1830 | stats->cnt = cnt; | |
9ed9e9ba | 1831 | stats->misses = misses; |
492ecee8 AS |
1832 | } |
1833 | ||
7bd509e3 DB |
1834 | #ifdef CONFIG_PROC_FS |
1835 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) | |
1836 | { | |
1837 | const struct bpf_prog *prog = filp->private_data; | |
f1f7714e | 1838 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
492ecee8 | 1839 | struct bpf_prog_stats stats; |
7bd509e3 | 1840 | |
492ecee8 | 1841 | bpf_prog_get_stats(prog, &stats); |
f1f7714e | 1842 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
7bd509e3 DB |
1843 | seq_printf(m, |
1844 | "prog_type:\t%u\n" | |
1845 | "prog_jited:\t%u\n" | |
f1f7714e | 1846 | "prog_tag:\t%s\n" |
4316b409 | 1847 | "memlock:\t%llu\n" |
492ecee8 AS |
1848 | "prog_id:\t%u\n" |
1849 | "run_time_ns:\t%llu\n" | |
9ed9e9ba AS |
1850 | "run_cnt:\t%llu\n" |
1851 | "recursion_misses:\t%llu\n", | |
7bd509e3 DB |
1852 | prog->type, |
1853 | prog->jited, | |
f1f7714e | 1854 | prog_tag, |
4316b409 | 1855 | prog->pages * 1ULL << PAGE_SHIFT, |
492ecee8 AS |
1856 | prog->aux->id, |
1857 | stats.nsecs, | |
9ed9e9ba AS |
1858 | stats.cnt, |
1859 | stats.misses); | |
7bd509e3 DB |
1860 | } |
1861 | #endif | |
1862 | ||
f66e448c | 1863 | const struct file_operations bpf_prog_fops = { |
7bd509e3 DB |
1864 | #ifdef CONFIG_PROC_FS |
1865 | .show_fdinfo = bpf_prog_show_fdinfo, | |
1866 | #endif | |
1867 | .release = bpf_prog_release, | |
6e71b04a CF |
1868 | .read = bpf_dummy_read, |
1869 | .write = bpf_dummy_write, | |
09756af4 AS |
1870 | }; |
1871 | ||
b2197755 | 1872 | int bpf_prog_new_fd(struct bpf_prog *prog) |
aa79781b | 1873 | { |
afdb09c7 CF |
1874 | int ret; |
1875 | ||
1876 | ret = security_bpf_prog(prog); | |
1877 | if (ret < 0) | |
1878 | return ret; | |
1879 | ||
aa79781b DB |
1880 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
1881 | O_RDWR | O_CLOEXEC); | |
1882 | } | |
1883 | ||
113214be | 1884 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
09756af4 | 1885 | { |
09756af4 AS |
1886 | if (!f.file) |
1887 | return ERR_PTR(-EBADF); | |
09756af4 AS |
1888 | if (f.file->f_op != &bpf_prog_fops) { |
1889 | fdput(f); | |
1890 | return ERR_PTR(-EINVAL); | |
1891 | } | |
1892 | ||
c2101297 | 1893 | return f.file->private_data; |
09756af4 AS |
1894 | } |
1895 | ||
85192dbf | 1896 | void bpf_prog_add(struct bpf_prog *prog, int i) |
92117d84 | 1897 | { |
85192dbf | 1898 | atomic64_add(i, &prog->aux->refcnt); |
92117d84 | 1899 | } |
59d3656d BB |
1900 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
1901 | ||
c540594f DB |
1902 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
1903 | { | |
1904 | /* Only to be used for undoing previous bpf_prog_add() in some | |
1905 | * error path. We still know that another entity in our call | |
1906 | * path holds a reference to the program, thus atomic_sub() can | |
1907 | * be safely used in such cases! | |
1908 | */ | |
85192dbf | 1909 | WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0); |
c540594f DB |
1910 | } |
1911 | EXPORT_SYMBOL_GPL(bpf_prog_sub); | |
1912 | ||
85192dbf | 1913 | void bpf_prog_inc(struct bpf_prog *prog) |
59d3656d | 1914 | { |
85192dbf | 1915 | atomic64_inc(&prog->aux->refcnt); |
59d3656d | 1916 | } |
97bc402d | 1917 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
92117d84 | 1918 | |
b16d9aa4 | 1919 | /* prog_idr_lock should have been held */ |
a6f6df69 | 1920 | struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) |
b16d9aa4 MKL |
1921 | { |
1922 | int refold; | |
1923 | ||
85192dbf | 1924 | refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0); |
b16d9aa4 MKL |
1925 | |
1926 | if (!refold) | |
1927 | return ERR_PTR(-ENOENT); | |
1928 | ||
1929 | return prog; | |
1930 | } | |
a6f6df69 | 1931 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
b16d9aa4 | 1932 | |
040ee692 | 1933 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
288b3de5 | 1934 | enum bpf_prog_type *attach_type, bool attach_drv) |
248f346f | 1935 | { |
288b3de5 JK |
1936 | /* not an attachment, just a refcount inc, always allow */ |
1937 | if (!attach_type) | |
1938 | return true; | |
248f346f JK |
1939 | |
1940 | if (prog->type != *attach_type) | |
1941 | return false; | |
288b3de5 | 1942 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
248f346f JK |
1943 | return false; |
1944 | ||
1945 | return true; | |
1946 | } | |
1947 | ||
1948 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, | |
288b3de5 | 1949 | bool attach_drv) |
09756af4 AS |
1950 | { |
1951 | struct fd f = fdget(ufd); | |
1952 | struct bpf_prog *prog; | |
1953 | ||
113214be | 1954 | prog = ____bpf_prog_get(f); |
09756af4 AS |
1955 | if (IS_ERR(prog)) |
1956 | return prog; | |
288b3de5 | 1957 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
113214be DB |
1958 | prog = ERR_PTR(-EINVAL); |
1959 | goto out; | |
1960 | } | |
09756af4 | 1961 | |
85192dbf | 1962 | bpf_prog_inc(prog); |
113214be | 1963 | out: |
09756af4 AS |
1964 | fdput(f); |
1965 | return prog; | |
1966 | } | |
113214be DB |
1967 | |
1968 | struct bpf_prog *bpf_prog_get(u32 ufd) | |
1969 | { | |
288b3de5 | 1970 | return __bpf_prog_get(ufd, NULL, false); |
113214be DB |
1971 | } |
1972 | ||
248f346f | 1973 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
288b3de5 | 1974 | bool attach_drv) |
248f346f | 1975 | { |
4d220ed0 | 1976 | return __bpf_prog_get(ufd, &type, attach_drv); |
248f346f | 1977 | } |
6c8dfe21 | 1978 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
248f346f | 1979 | |
aac3fc32 AI |
1980 | /* Initially all BPF programs could be loaded w/o specifying |
1981 | * expected_attach_type. Later for some of them specifying expected_attach_type | |
1982 | * at load time became required so that program could be validated properly. | |
1983 | * Programs of types that are allowed to be loaded both w/ and w/o (for | |
1984 | * backward compatibility) expected_attach_type, should have the default attach | |
1985 | * type assigned to expected_attach_type for the latter case, so that it can be | |
1986 | * validated later at attach time. | |
1987 | * | |
1988 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if | |
1989 | * prog type requires it but has some attach types that have to be backward | |
1990 | * compatible. | |
1991 | */ | |
1992 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) | |
1993 | { | |
1994 | switch (attr->prog_type) { | |
1995 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
1996 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't | |
1997 | * exist so checking for non-zero is the way to go here. | |
1998 | */ | |
1999 | if (!attr->expected_attach_type) | |
2000 | attr->expected_attach_type = | |
2001 | BPF_CGROUP_INET_SOCK_CREATE; | |
2002 | break; | |
d5e4ddae KI |
2003 | case BPF_PROG_TYPE_SK_REUSEPORT: |
2004 | if (!attr->expected_attach_type) | |
2005 | attr->expected_attach_type = | |
2006 | BPF_SK_REUSEPORT_SELECT; | |
2007 | break; | |
aac3fc32 AI |
2008 | } |
2009 | } | |
2010 | ||
5e43f899 | 2011 | static int |
ccfe29eb AS |
2012 | bpf_prog_load_check_attach(enum bpf_prog_type prog_type, |
2013 | enum bpf_attach_type expected_attach_type, | |
290248a5 AN |
2014 | struct btf *attach_btf, u32 btf_id, |
2015 | struct bpf_prog *dst_prog) | |
5e43f899 | 2016 | { |
27ae7997 | 2017 | if (btf_id) { |
c108e3c1 AS |
2018 | if (btf_id > BTF_MAX_TYPE) |
2019 | return -EINVAL; | |
27ae7997 | 2020 | |
290248a5 AN |
2021 | if (!attach_btf && !dst_prog) |
2022 | return -EINVAL; | |
2023 | ||
27ae7997 MKL |
2024 | switch (prog_type) { |
2025 | case BPF_PROG_TYPE_TRACING: | |
9e4e01df | 2026 | case BPF_PROG_TYPE_LSM: |
27ae7997 | 2027 | case BPF_PROG_TYPE_STRUCT_OPS: |
be8704ff | 2028 | case BPF_PROG_TYPE_EXT: |
27ae7997 MKL |
2029 | break; |
2030 | default: | |
c108e3c1 | 2031 | return -EINVAL; |
27ae7997 | 2032 | } |
c108e3c1 AS |
2033 | } |
2034 | ||
290248a5 AN |
2035 | if (attach_btf && (!btf_id || dst_prog)) |
2036 | return -EINVAL; | |
2037 | ||
2038 | if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING && | |
be8704ff | 2039 | prog_type != BPF_PROG_TYPE_EXT) |
27ae7997 MKL |
2040 | return -EINVAL; |
2041 | ||
4fbac77d | 2042 | switch (prog_type) { |
aac3fc32 AI |
2043 | case BPF_PROG_TYPE_CGROUP_SOCK: |
2044 | switch (expected_attach_type) { | |
2045 | case BPF_CGROUP_INET_SOCK_CREATE: | |
f5836749 | 2046 | case BPF_CGROUP_INET_SOCK_RELEASE: |
aac3fc32 AI |
2047 | case BPF_CGROUP_INET4_POST_BIND: |
2048 | case BPF_CGROUP_INET6_POST_BIND: | |
2049 | return 0; | |
2050 | default: | |
2051 | return -EINVAL; | |
2052 | } | |
4fbac77d AI |
2053 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
2054 | switch (expected_attach_type) { | |
2055 | case BPF_CGROUP_INET4_BIND: | |
2056 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
2057 | case BPF_CGROUP_INET4_CONNECT: |
2058 | case BPF_CGROUP_INET6_CONNECT: | |
1b66d253 DB |
2059 | case BPF_CGROUP_INET4_GETPEERNAME: |
2060 | case BPF_CGROUP_INET6_GETPEERNAME: | |
2061 | case BPF_CGROUP_INET4_GETSOCKNAME: | |
2062 | case BPF_CGROUP_INET6_GETSOCKNAME: | |
1cedee13 AI |
2063 | case BPF_CGROUP_UDP4_SENDMSG: |
2064 | case BPF_CGROUP_UDP6_SENDMSG: | |
983695fa DB |
2065 | case BPF_CGROUP_UDP4_RECVMSG: |
2066 | case BPF_CGROUP_UDP6_RECVMSG: | |
4fbac77d AI |
2067 | return 0; |
2068 | default: | |
2069 | return -EINVAL; | |
2070 | } | |
5cf1e914 | 2071 | case BPF_PROG_TYPE_CGROUP_SKB: |
2072 | switch (expected_attach_type) { | |
2073 | case BPF_CGROUP_INET_INGRESS: | |
2074 | case BPF_CGROUP_INET_EGRESS: | |
2075 | return 0; | |
2076 | default: | |
2077 | return -EINVAL; | |
2078 | } | |
0d01da6a SF |
2079 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
2080 | switch (expected_attach_type) { | |
2081 | case BPF_CGROUP_SETSOCKOPT: | |
2082 | case BPF_CGROUP_GETSOCKOPT: | |
2083 | return 0; | |
2084 | default: | |
2085 | return -EINVAL; | |
2086 | } | |
e9ddbb77 JS |
2087 | case BPF_PROG_TYPE_SK_LOOKUP: |
2088 | if (expected_attach_type == BPF_SK_LOOKUP) | |
2089 | return 0; | |
2090 | return -EINVAL; | |
d5e4ddae KI |
2091 | case BPF_PROG_TYPE_SK_REUSEPORT: |
2092 | switch (expected_attach_type) { | |
2093 | case BPF_SK_REUSEPORT_SELECT: | |
2094 | case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE: | |
2095 | return 0; | |
2096 | default: | |
2097 | return -EINVAL; | |
2098 | } | |
79a7f8bd | 2099 | case BPF_PROG_TYPE_SYSCALL: |
be8704ff AS |
2100 | case BPF_PROG_TYPE_EXT: |
2101 | if (expected_attach_type) | |
2102 | return -EINVAL; | |
df561f66 | 2103 | fallthrough; |
4fbac77d AI |
2104 | default: |
2105 | return 0; | |
2106 | } | |
5e43f899 AI |
2107 | } |
2108 | ||
2c78ee89 AS |
2109 | static bool is_net_admin_prog_type(enum bpf_prog_type prog_type) |
2110 | { | |
2111 | switch (prog_type) { | |
2112 | case BPF_PROG_TYPE_SCHED_CLS: | |
2113 | case BPF_PROG_TYPE_SCHED_ACT: | |
2114 | case BPF_PROG_TYPE_XDP: | |
2115 | case BPF_PROG_TYPE_LWT_IN: | |
2116 | case BPF_PROG_TYPE_LWT_OUT: | |
2117 | case BPF_PROG_TYPE_LWT_XMIT: | |
2118 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: | |
2119 | case BPF_PROG_TYPE_SK_SKB: | |
2120 | case BPF_PROG_TYPE_SK_MSG: | |
2121 | case BPF_PROG_TYPE_LIRC_MODE2: | |
2122 | case BPF_PROG_TYPE_FLOW_DISSECTOR: | |
2123 | case BPF_PROG_TYPE_CGROUP_DEVICE: | |
2124 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
2125 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: | |
2126 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
2127 | case BPF_PROG_TYPE_CGROUP_SYSCTL: | |
2128 | case BPF_PROG_TYPE_SOCK_OPS: | |
2129 | case BPF_PROG_TYPE_EXT: /* extends any prog */ | |
2130 | return true; | |
2131 | case BPF_PROG_TYPE_CGROUP_SKB: | |
2132 | /* always unpriv */ | |
2133 | case BPF_PROG_TYPE_SK_REUSEPORT: | |
2134 | /* equivalent to SOCKET_FILTER. need CAP_BPF only */ | |
2135 | default: | |
2136 | return false; | |
2137 | } | |
2138 | } | |
2139 | ||
2140 | static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) | |
2141 | { | |
2142 | switch (prog_type) { | |
2143 | case BPF_PROG_TYPE_KPROBE: | |
2144 | case BPF_PROG_TYPE_TRACEPOINT: | |
2145 | case BPF_PROG_TYPE_PERF_EVENT: | |
2146 | case BPF_PROG_TYPE_RAW_TRACEPOINT: | |
2147 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: | |
2148 | case BPF_PROG_TYPE_TRACING: | |
2149 | case BPF_PROG_TYPE_LSM: | |
2150 | case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */ | |
2151 | case BPF_PROG_TYPE_EXT: /* extends any prog */ | |
2152 | return true; | |
2153 | default: | |
2154 | return false; | |
2155 | } | |
2156 | } | |
2157 | ||
09756af4 | 2158 | /* last field in 'union bpf_attr' used by this command */ |
387544bf | 2159 | #define BPF_PROG_LOAD_LAST_FIELD fd_array |
09756af4 | 2160 | |
af2ac3e1 | 2161 | static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) |
09756af4 AS |
2162 | { |
2163 | enum bpf_prog_type type = attr->prog_type; | |
290248a5 AN |
2164 | struct bpf_prog *prog, *dst_prog = NULL; |
2165 | struct btf *attach_btf = NULL; | |
09756af4 AS |
2166 | int err; |
2167 | char license[128]; | |
2168 | bool is_gpl; | |
2169 | ||
2170 | if (CHECK_ATTR(BPF_PROG_LOAD)) | |
2171 | return -EINVAL; | |
2172 | ||
c240eff6 JW |
2173 | if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | |
2174 | BPF_F_ANY_ALIGNMENT | | |
10d274e8 | 2175 | BPF_F_TEST_STATE_FREQ | |
1e6c62a8 | 2176 | BPF_F_SLEEPABLE | |
c240eff6 | 2177 | BPF_F_TEST_RND_HI32)) |
e07b98d9 DM |
2178 | return -EINVAL; |
2179 | ||
e9ee9efc DM |
2180 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && |
2181 | (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && | |
2c78ee89 | 2182 | !bpf_capable()) |
e9ee9efc DM |
2183 | return -EPERM; |
2184 | ||
09756af4 | 2185 | /* copy eBPF program license from user space */ |
af2ac3e1 AS |
2186 | if (strncpy_from_bpfptr(license, |
2187 | make_bpfptr(attr->license, uattr.is_kernel), | |
2188 | sizeof(license) - 1) < 0) | |
09756af4 AS |
2189 | return -EFAULT; |
2190 | license[sizeof(license) - 1] = 0; | |
2191 | ||
2192 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ | |
2193 | is_gpl = license_is_gpl_compatible(license); | |
2194 | ||
c04c0d2b | 2195 | if (attr->insn_cnt == 0 || |
2c78ee89 | 2196 | attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) |
ef0915ca | 2197 | return -E2BIG; |
80b7d819 CF |
2198 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
2199 | type != BPF_PROG_TYPE_CGROUP_SKB && | |
2c78ee89 AS |
2200 | !bpf_capable()) |
2201 | return -EPERM; | |
2202 | ||
b338cb92 | 2203 | if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN)) |
2c78ee89 AS |
2204 | return -EPERM; |
2205 | if (is_perfmon_prog_type(type) && !perfmon_capable()) | |
1be7f75d AS |
2206 | return -EPERM; |
2207 | ||
290248a5 AN |
2208 | /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog |
2209 | * or btf, we need to check which one it is | |
2210 | */ | |
2211 | if (attr->attach_prog_fd) { | |
2212 | dst_prog = bpf_prog_get(attr->attach_prog_fd); | |
2213 | if (IS_ERR(dst_prog)) { | |
2214 | dst_prog = NULL; | |
2215 | attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd); | |
2216 | if (IS_ERR(attach_btf)) | |
2217 | return -EINVAL; | |
2218 | if (!btf_is_kernel(attach_btf)) { | |
8bdd8e27 AN |
2219 | /* attaching through specifying bpf_prog's BTF |
2220 | * objects directly might be supported eventually | |
2221 | */ | |
290248a5 | 2222 | btf_put(attach_btf); |
8bdd8e27 | 2223 | return -ENOTSUPP; |
290248a5 AN |
2224 | } |
2225 | } | |
2226 | } else if (attr->attach_btf_id) { | |
2227 | /* fall back to vmlinux BTF, if BTF type ID is specified */ | |
2228 | attach_btf = bpf_get_btf_vmlinux(); | |
2229 | if (IS_ERR(attach_btf)) | |
2230 | return PTR_ERR(attach_btf); | |
2231 | if (!attach_btf) | |
2232 | return -EINVAL; | |
2233 | btf_get(attach_btf); | |
2234 | } | |
2235 | ||
aac3fc32 | 2236 | bpf_prog_load_fixup_attach_type(attr); |
ccfe29eb | 2237 | if (bpf_prog_load_check_attach(type, attr->expected_attach_type, |
290248a5 AN |
2238 | attach_btf, attr->attach_btf_id, |
2239 | dst_prog)) { | |
2240 | if (dst_prog) | |
2241 | bpf_prog_put(dst_prog); | |
2242 | if (attach_btf) | |
2243 | btf_put(attach_btf); | |
5e43f899 | 2244 | return -EINVAL; |
290248a5 | 2245 | } |
5e43f899 | 2246 | |
09756af4 AS |
2247 | /* plain bpf_prog allocation */ |
2248 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); | |
290248a5 AN |
2249 | if (!prog) { |
2250 | if (dst_prog) | |
2251 | bpf_prog_put(dst_prog); | |
2252 | if (attach_btf) | |
2253 | btf_put(attach_btf); | |
09756af4 | 2254 | return -ENOMEM; |
290248a5 | 2255 | } |
09756af4 | 2256 | |
5e43f899 | 2257 | prog->expected_attach_type = attr->expected_attach_type; |
290248a5 | 2258 | prog->aux->attach_btf = attach_btf; |
ccfe29eb | 2259 | prog->aux->attach_btf_id = attr->attach_btf_id; |
290248a5 | 2260 | prog->aux->dst_prog = dst_prog; |
9a18eedb | 2261 | prog->aux->offload_requested = !!attr->prog_ifindex; |
1e6c62a8 | 2262 | prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE; |
9a18eedb | 2263 | |
afdb09c7 | 2264 | err = security_bpf_prog_alloc(prog->aux); |
aaac3ba9 | 2265 | if (err) |
3ac1f01b | 2266 | goto free_prog; |
afdb09c7 | 2267 | |
3ac1f01b | 2268 | prog->aux->user = get_current_user(); |
09756af4 AS |
2269 | prog->len = attr->insn_cnt; |
2270 | ||
2271 | err = -EFAULT; | |
af2ac3e1 AS |
2272 | if (copy_from_bpfptr(prog->insns, |
2273 | make_bpfptr(attr->insns, uattr.is_kernel), | |
2274 | bpf_prog_insn_size(prog)) != 0) | |
3ac1f01b | 2275 | goto free_prog_sec; |
09756af4 AS |
2276 | |
2277 | prog->orig_prog = NULL; | |
a91263d5 | 2278 | prog->jited = 0; |
09756af4 | 2279 | |
85192dbf | 2280 | atomic64_set(&prog->aux->refcnt, 1); |
a91263d5 | 2281 | prog->gpl_compatible = is_gpl ? 1 : 0; |
09756af4 | 2282 | |
9a18eedb | 2283 | if (bpf_prog_is_dev_bound(prog->aux)) { |
ab3f0063 JK |
2284 | err = bpf_prog_offload_init(prog, attr); |
2285 | if (err) | |
3ac1f01b | 2286 | goto free_prog_sec; |
ab3f0063 JK |
2287 | } |
2288 | ||
09756af4 AS |
2289 | /* find program type: socket_filter vs tracing_filter */ |
2290 | err = find_prog_type(type, prog); | |
2291 | if (err < 0) | |
3ac1f01b | 2292 | goto free_prog_sec; |
09756af4 | 2293 | |
9285ec4c | 2294 | prog->aux->load_time = ktime_get_boottime_ns(); |
8e7ae251 MKL |
2295 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name, |
2296 | sizeof(attr->prog_name)); | |
2297 | if (err < 0) | |
3ac1f01b | 2298 | goto free_prog_sec; |
cb4d2b3f | 2299 | |
09756af4 | 2300 | /* run eBPF verifier */ |
838e9690 | 2301 | err = bpf_check(&prog, attr, uattr); |
09756af4 AS |
2302 | if (err < 0) |
2303 | goto free_used_maps; | |
2304 | ||
9facc336 | 2305 | prog = bpf_prog_select_runtime(prog, &err); |
04fd61ab AS |
2306 | if (err < 0) |
2307 | goto free_used_maps; | |
09756af4 | 2308 | |
dc4bb0e2 MKL |
2309 | err = bpf_prog_alloc_id(prog); |
2310 | if (err) | |
2311 | goto free_used_maps; | |
2312 | ||
c751798a DB |
2313 | /* Upon success of bpf_prog_alloc_id(), the BPF prog is |
2314 | * effectively publicly exposed. However, retrieving via | |
2315 | * bpf_prog_get_fd_by_id() will take another reference, | |
2316 | * therefore it cannot be gone underneath us. | |
2317 | * | |
2318 | * Only for the time /after/ successful bpf_prog_new_fd() | |
2319 | * and before returning to userspace, we might just hold | |
2320 | * one reference and any parallel close on that fd could | |
2321 | * rip everything out. Hence, below notifications must | |
2322 | * happen before bpf_prog_new_fd(). | |
2323 | * | |
2324 | * Also, any failure handling from this point onwards must | |
2325 | * be using bpf_prog_put() given the program is exposed. | |
2326 | */ | |
74451e66 | 2327 | bpf_prog_kallsyms_add(prog); |
6ee52e2a | 2328 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); |
bae141f5 | 2329 | bpf_audit_prog(prog, BPF_AUDIT_LOAD); |
c751798a DB |
2330 | |
2331 | err = bpf_prog_new_fd(prog); | |
2332 | if (err < 0) | |
2333 | bpf_prog_put(prog); | |
09756af4 AS |
2334 | return err; |
2335 | ||
2336 | free_used_maps: | |
cd7455f1 DB |
2337 | /* In case we have subprogs, we need to wait for a grace |
2338 | * period before we can tear down JIT memory since symbols | |
2339 | * are already exposed under kallsyms. | |
2340 | */ | |
2341 | __bpf_prog_put_noref(prog, prog->aux->func_cnt); | |
2342 | return err; | |
afdb09c7 | 2343 | free_prog_sec: |
3ac1f01b | 2344 | free_uid(prog->aux->user); |
afdb09c7 | 2345 | security_bpf_prog_free(prog->aux); |
3ac1f01b | 2346 | free_prog: |
22dc4a0f AN |
2347 | if (prog->aux->attach_btf) |
2348 | btf_put(prog->aux->attach_btf); | |
09756af4 AS |
2349 | bpf_prog_free(prog); |
2350 | return err; | |
2351 | } | |
2352 | ||
6e71b04a | 2353 | #define BPF_OBJ_LAST_FIELD file_flags |
b2197755 DB |
2354 | |
2355 | static int bpf_obj_pin(const union bpf_attr *attr) | |
2356 | { | |
6e71b04a | 2357 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
b2197755 DB |
2358 | return -EINVAL; |
2359 | ||
535e7b4b | 2360 | return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); |
b2197755 DB |
2361 | } |
2362 | ||
2363 | static int bpf_obj_get(const union bpf_attr *attr) | |
2364 | { | |
6e71b04a CF |
2365 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
2366 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) | |
b2197755 DB |
2367 | return -EINVAL; |
2368 | ||
6e71b04a CF |
2369 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
2370 | attr->file_flags); | |
b2197755 DB |
2371 | } |
2372 | ||
f2e10bff | 2373 | void bpf_link_init(struct bpf_link *link, enum bpf_link_type type, |
a3b80e10 | 2374 | const struct bpf_link_ops *ops, struct bpf_prog *prog) |
fec56f58 | 2375 | { |
70ed506c | 2376 | atomic64_set(&link->refcnt, 1); |
f2e10bff | 2377 | link->type = type; |
a3b80e10 | 2378 | link->id = 0; |
70ed506c AN |
2379 | link->ops = ops; |
2380 | link->prog = prog; | |
2381 | } | |
2382 | ||
a3b80e10 AN |
2383 | static void bpf_link_free_id(int id) |
2384 | { | |
2385 | if (!id) | |
2386 | return; | |
2387 | ||
2388 | spin_lock_bh(&link_idr_lock); | |
2389 | idr_remove(&link_idr, id); | |
2390 | spin_unlock_bh(&link_idr_lock); | |
2391 | } | |
2392 | ||
98868668 AN |
2393 | /* Clean up bpf_link and corresponding anon_inode file and FD. After |
2394 | * anon_inode is created, bpf_link can't be just kfree()'d due to deferred | |
a3b80e10 AN |
2395 | * anon_inode's release() call. This helper marksbpf_link as |
2396 | * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt | |
2397 | * is not decremented, it's the responsibility of a calling code that failed | |
2398 | * to complete bpf_link initialization. | |
98868668 | 2399 | */ |
a3b80e10 | 2400 | void bpf_link_cleanup(struct bpf_link_primer *primer) |
babf3164 | 2401 | { |
a3b80e10 AN |
2402 | primer->link->prog = NULL; |
2403 | bpf_link_free_id(primer->id); | |
2404 | fput(primer->file); | |
2405 | put_unused_fd(primer->fd); | |
babf3164 AN |
2406 | } |
2407 | ||
70ed506c AN |
2408 | void bpf_link_inc(struct bpf_link *link) |
2409 | { | |
2410 | atomic64_inc(&link->refcnt); | |
2411 | } | |
2412 | ||
2413 | /* bpf_link_free is guaranteed to be called from process context */ | |
2414 | static void bpf_link_free(struct bpf_link *link) | |
2415 | { | |
a3b80e10 | 2416 | bpf_link_free_id(link->id); |
babf3164 AN |
2417 | if (link->prog) { |
2418 | /* detach BPF program, clean up used resources */ | |
2419 | link->ops->release(link); | |
2420 | bpf_prog_put(link->prog); | |
2421 | } | |
2422 | /* free bpf_link and its containing memory */ | |
2423 | link->ops->dealloc(link); | |
70ed506c AN |
2424 | } |
2425 | ||
2426 | static void bpf_link_put_deferred(struct work_struct *work) | |
2427 | { | |
2428 | struct bpf_link *link = container_of(work, struct bpf_link, work); | |
2429 | ||
2430 | bpf_link_free(link); | |
2431 | } | |
2432 | ||
2433 | /* bpf_link_put can be called from atomic context, but ensures that resources | |
2434 | * are freed from process context | |
2435 | */ | |
2436 | void bpf_link_put(struct bpf_link *link) | |
2437 | { | |
2438 | if (!atomic64_dec_and_test(&link->refcnt)) | |
2439 | return; | |
2440 | ||
f00f2f7f AS |
2441 | if (in_atomic()) { |
2442 | INIT_WORK(&link->work, bpf_link_put_deferred); | |
2443 | schedule_work(&link->work); | |
2444 | } else { | |
2445 | bpf_link_free(link); | |
2446 | } | |
70ed506c AN |
2447 | } |
2448 | ||
2449 | static int bpf_link_release(struct inode *inode, struct file *filp) | |
2450 | { | |
2451 | struct bpf_link *link = filp->private_data; | |
2452 | ||
2453 | bpf_link_put(link); | |
fec56f58 AS |
2454 | return 0; |
2455 | } | |
2456 | ||
70ed506c | 2457 | #ifdef CONFIG_PROC_FS |
f2e10bff AN |
2458 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) |
2459 | #define BPF_MAP_TYPE(_id, _ops) | |
2460 | #define BPF_LINK_TYPE(_id, _name) [_id] = #_name, | |
2461 | static const char *bpf_link_type_strs[] = { | |
2462 | [BPF_LINK_TYPE_UNSPEC] = "<invalid>", | |
2463 | #include <linux/bpf_types.h> | |
2464 | }; | |
2465 | #undef BPF_PROG_TYPE | |
2466 | #undef BPF_MAP_TYPE | |
2467 | #undef BPF_LINK_TYPE | |
70ed506c AN |
2468 | |
2469 | static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) | |
2470 | { | |
2471 | const struct bpf_link *link = filp->private_data; | |
2472 | const struct bpf_prog *prog = link->prog; | |
2473 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; | |
70ed506c AN |
2474 | |
2475 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); | |
2476 | seq_printf(m, | |
2477 | "link_type:\t%s\n" | |
a3b80e10 | 2478 | "link_id:\t%u\n" |
70ed506c AN |
2479 | "prog_tag:\t%s\n" |
2480 | "prog_id:\t%u\n", | |
f2e10bff | 2481 | bpf_link_type_strs[link->type], |
a3b80e10 | 2482 | link->id, |
70ed506c AN |
2483 | prog_tag, |
2484 | prog->aux->id); | |
f2e10bff AN |
2485 | if (link->ops->show_fdinfo) |
2486 | link->ops->show_fdinfo(link, m); | |
70ed506c AN |
2487 | } |
2488 | #endif | |
2489 | ||
6f302bfb | 2490 | static const struct file_operations bpf_link_fops = { |
70ed506c AN |
2491 | #ifdef CONFIG_PROC_FS |
2492 | .show_fdinfo = bpf_link_show_fdinfo, | |
2493 | #endif | |
2494 | .release = bpf_link_release, | |
fec56f58 AS |
2495 | .read = bpf_dummy_read, |
2496 | .write = bpf_dummy_write, | |
2497 | }; | |
2498 | ||
a3b80e10 | 2499 | static int bpf_link_alloc_id(struct bpf_link *link) |
70ed506c | 2500 | { |
a3b80e10 | 2501 | int id; |
70ed506c | 2502 | |
a3b80e10 AN |
2503 | idr_preload(GFP_KERNEL); |
2504 | spin_lock_bh(&link_idr_lock); | |
2505 | id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC); | |
2506 | spin_unlock_bh(&link_idr_lock); | |
2507 | idr_preload_end(); | |
70ed506c | 2508 | |
a3b80e10 AN |
2509 | return id; |
2510 | } | |
2511 | ||
2512 | /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file, | |
2513 | * reserving unused FD and allocating ID from link_idr. This is to be paired | |
2514 | * with bpf_link_settle() to install FD and ID and expose bpf_link to | |
2515 | * user-space, if bpf_link is successfully attached. If not, bpf_link and | |
2516 | * pre-allocated resources are to be freed with bpf_cleanup() call. All the | |
2517 | * transient state is passed around in struct bpf_link_primer. | |
2518 | * This is preferred way to create and initialize bpf_link, especially when | |
2519 | * there are complicated and expensive operations inbetween creating bpf_link | |
2520 | * itself and attaching it to BPF hook. By using bpf_link_prime() and | |
2521 | * bpf_link_settle() kernel code using bpf_link doesn't have to perform | |
2522 | * expensive (and potentially failing) roll back operations in a rare case | |
2523 | * that file, FD, or ID can't be allocated. | |
babf3164 | 2524 | */ |
a3b80e10 | 2525 | int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer) |
babf3164 AN |
2526 | { |
2527 | struct file *file; | |
a3b80e10 | 2528 | int fd, id; |
babf3164 AN |
2529 | |
2530 | fd = get_unused_fd_flags(O_CLOEXEC); | |
2531 | if (fd < 0) | |
a3b80e10 | 2532 | return fd; |
babf3164 | 2533 | |
babf3164 | 2534 | |
a3b80e10 AN |
2535 | id = bpf_link_alloc_id(link); |
2536 | if (id < 0) { | |
2537 | put_unused_fd(fd); | |
a3b80e10 AN |
2538 | return id; |
2539 | } | |
babf3164 AN |
2540 | |
2541 | file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC); | |
2542 | if (IS_ERR(file)) { | |
138c6767 | 2543 | bpf_link_free_id(id); |
babf3164 | 2544 | put_unused_fd(fd); |
138c6767 | 2545 | return PTR_ERR(file); |
babf3164 AN |
2546 | } |
2547 | ||
a3b80e10 AN |
2548 | primer->link = link; |
2549 | primer->file = file; | |
2550 | primer->fd = fd; | |
2551 | primer->id = id; | |
2552 | return 0; | |
2553 | } | |
2554 | ||
2555 | int bpf_link_settle(struct bpf_link_primer *primer) | |
2556 | { | |
2557 | /* make bpf_link fetchable by ID */ | |
2558 | spin_lock_bh(&link_idr_lock); | |
2559 | primer->link->id = primer->id; | |
2560 | spin_unlock_bh(&link_idr_lock); | |
2561 | /* make bpf_link fetchable by FD */ | |
2562 | fd_install(primer->fd, primer->file); | |
2563 | /* pass through installed FD */ | |
2564 | return primer->fd; | |
2565 | } | |
2566 | ||
2567 | int bpf_link_new_fd(struct bpf_link *link) | |
2568 | { | |
2569 | return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC); | |
babf3164 AN |
2570 | } |
2571 | ||
70ed506c AN |
2572 | struct bpf_link *bpf_link_get_from_fd(u32 ufd) |
2573 | { | |
2574 | struct fd f = fdget(ufd); | |
2575 | struct bpf_link *link; | |
2576 | ||
2577 | if (!f.file) | |
2578 | return ERR_PTR(-EBADF); | |
2579 | if (f.file->f_op != &bpf_link_fops) { | |
2580 | fdput(f); | |
2581 | return ERR_PTR(-EINVAL); | |
2582 | } | |
2583 | ||
2584 | link = f.file->private_data; | |
2585 | bpf_link_inc(link); | |
2586 | fdput(f); | |
2587 | ||
2588 | return link; | |
2589 | } | |
2590 | ||
2591 | struct bpf_tracing_link { | |
2592 | struct bpf_link link; | |
f2e10bff | 2593 | enum bpf_attach_type attach_type; |
3aac1ead THJ |
2594 | struct bpf_trampoline *trampoline; |
2595 | struct bpf_prog *tgt_prog; | |
70ed506c AN |
2596 | }; |
2597 | ||
2598 | static void bpf_tracing_link_release(struct bpf_link *link) | |
babf3164 | 2599 | { |
3aac1ead THJ |
2600 | struct bpf_tracing_link *tr_link = |
2601 | container_of(link, struct bpf_tracing_link, link); | |
2602 | ||
2603 | WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog, | |
2604 | tr_link->trampoline)); | |
2605 | ||
2606 | bpf_trampoline_put(tr_link->trampoline); | |
2607 | ||
2608 | /* tgt_prog is NULL if target is a kernel function */ | |
2609 | if (tr_link->tgt_prog) | |
2610 | bpf_prog_put(tr_link->tgt_prog); | |
babf3164 AN |
2611 | } |
2612 | ||
2613 | static void bpf_tracing_link_dealloc(struct bpf_link *link) | |
70ed506c AN |
2614 | { |
2615 | struct bpf_tracing_link *tr_link = | |
2616 | container_of(link, struct bpf_tracing_link, link); | |
2617 | ||
70ed506c AN |
2618 | kfree(tr_link); |
2619 | } | |
2620 | ||
f2e10bff AN |
2621 | static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, |
2622 | struct seq_file *seq) | |
2623 | { | |
2624 | struct bpf_tracing_link *tr_link = | |
2625 | container_of(link, struct bpf_tracing_link, link); | |
2626 | ||
2627 | seq_printf(seq, | |
2628 | "attach_type:\t%d\n", | |
2629 | tr_link->attach_type); | |
2630 | } | |
2631 | ||
2632 | static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, | |
2633 | struct bpf_link_info *info) | |
2634 | { | |
2635 | struct bpf_tracing_link *tr_link = | |
2636 | container_of(link, struct bpf_tracing_link, link); | |
2637 | ||
2638 | info->tracing.attach_type = tr_link->attach_type; | |
441e8c66 THJ |
2639 | bpf_trampoline_unpack_key(tr_link->trampoline->key, |
2640 | &info->tracing.target_obj_id, | |
2641 | &info->tracing.target_btf_id); | |
f2e10bff AN |
2642 | |
2643 | return 0; | |
2644 | } | |
2645 | ||
70ed506c AN |
2646 | static const struct bpf_link_ops bpf_tracing_link_lops = { |
2647 | .release = bpf_tracing_link_release, | |
babf3164 | 2648 | .dealloc = bpf_tracing_link_dealloc, |
f2e10bff AN |
2649 | .show_fdinfo = bpf_tracing_link_show_fdinfo, |
2650 | .fill_link_info = bpf_tracing_link_fill_link_info, | |
70ed506c AN |
2651 | }; |
2652 | ||
4a1e7c0c THJ |
2653 | static int bpf_tracing_prog_attach(struct bpf_prog *prog, |
2654 | int tgt_prog_fd, | |
2655 | u32 btf_id) | |
fec56f58 | 2656 | { |
a3b80e10 | 2657 | struct bpf_link_primer link_primer; |
3aac1ead | 2658 | struct bpf_prog *tgt_prog = NULL; |
4a1e7c0c | 2659 | struct bpf_trampoline *tr = NULL; |
70ed506c | 2660 | struct bpf_tracing_link *link; |
4a1e7c0c | 2661 | u64 key = 0; |
a3b80e10 | 2662 | int err; |
fec56f58 | 2663 | |
9e4e01df KS |
2664 | switch (prog->type) { |
2665 | case BPF_PROG_TYPE_TRACING: | |
2666 | if (prog->expected_attach_type != BPF_TRACE_FENTRY && | |
2667 | prog->expected_attach_type != BPF_TRACE_FEXIT && | |
2668 | prog->expected_attach_type != BPF_MODIFY_RETURN) { | |
2669 | err = -EINVAL; | |
2670 | goto out_put_prog; | |
2671 | } | |
2672 | break; | |
2673 | case BPF_PROG_TYPE_EXT: | |
2674 | if (prog->expected_attach_type != 0) { | |
2675 | err = -EINVAL; | |
2676 | goto out_put_prog; | |
2677 | } | |
2678 | break; | |
2679 | case BPF_PROG_TYPE_LSM: | |
2680 | if (prog->expected_attach_type != BPF_LSM_MAC) { | |
2681 | err = -EINVAL; | |
2682 | goto out_put_prog; | |
2683 | } | |
2684 | break; | |
2685 | default: | |
fec56f58 AS |
2686 | err = -EINVAL; |
2687 | goto out_put_prog; | |
2688 | } | |
2689 | ||
4a1e7c0c THJ |
2690 | if (!!tgt_prog_fd != !!btf_id) { |
2691 | err = -EINVAL; | |
2692 | goto out_put_prog; | |
2693 | } | |
2694 | ||
2695 | if (tgt_prog_fd) { | |
2696 | /* For now we only allow new targets for BPF_PROG_TYPE_EXT */ | |
2697 | if (prog->type != BPF_PROG_TYPE_EXT) { | |
2698 | err = -EINVAL; | |
2699 | goto out_put_prog; | |
2700 | } | |
2701 | ||
2702 | tgt_prog = bpf_prog_get(tgt_prog_fd); | |
2703 | if (IS_ERR(tgt_prog)) { | |
2704 | err = PTR_ERR(tgt_prog); | |
2705 | tgt_prog = NULL; | |
2706 | goto out_put_prog; | |
2707 | } | |
2708 | ||
22dc4a0f | 2709 | key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id); |
4a1e7c0c THJ |
2710 | } |
2711 | ||
70ed506c AN |
2712 | link = kzalloc(sizeof(*link), GFP_USER); |
2713 | if (!link) { | |
2714 | err = -ENOMEM; | |
2715 | goto out_put_prog; | |
2716 | } | |
f2e10bff AN |
2717 | bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING, |
2718 | &bpf_tracing_link_lops, prog); | |
2719 | link->attach_type = prog->expected_attach_type; | |
70ed506c | 2720 | |
3aac1ead THJ |
2721 | mutex_lock(&prog->aux->dst_mutex); |
2722 | ||
4a1e7c0c THJ |
2723 | /* There are a few possible cases here: |
2724 | * | |
2725 | * - if prog->aux->dst_trampoline is set, the program was just loaded | |
2726 | * and not yet attached to anything, so we can use the values stored | |
2727 | * in prog->aux | |
2728 | * | |
2729 | * - if prog->aux->dst_trampoline is NULL, the program has already been | |
2730 | * attached to a target and its initial target was cleared (below) | |
2731 | * | |
2732 | * - if tgt_prog != NULL, the caller specified tgt_prog_fd + | |
2733 | * target_btf_id using the link_create API. | |
2734 | * | |
2735 | * - if tgt_prog == NULL when this function was called using the old | |
f3a95075 JO |
2736 | * raw_tracepoint_open API, and we need a target from prog->aux |
2737 | * | |
2738 | * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program | |
2739 | * was detached and is going for re-attachment. | |
4a1e7c0c THJ |
2740 | */ |
2741 | if (!prog->aux->dst_trampoline && !tgt_prog) { | |
f3a95075 JO |
2742 | /* |
2743 | * Allow re-attach for TRACING and LSM programs. If it's | |
2744 | * currently linked, bpf_trampoline_link_prog will fail. | |
2745 | * EXT programs need to specify tgt_prog_fd, so they | |
2746 | * re-attach in separate code path. | |
2747 | */ | |
2748 | if (prog->type != BPF_PROG_TYPE_TRACING && | |
2749 | prog->type != BPF_PROG_TYPE_LSM) { | |
2750 | err = -EINVAL; | |
2751 | goto out_unlock; | |
2752 | } | |
2753 | btf_id = prog->aux->attach_btf_id; | |
2754 | key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id); | |
babf3164 | 2755 | } |
4a1e7c0c THJ |
2756 | |
2757 | if (!prog->aux->dst_trampoline || | |
2758 | (key && key != prog->aux->dst_trampoline->key)) { | |
2759 | /* If there is no saved target, or the specified target is | |
2760 | * different from the destination specified at load time, we | |
2761 | * need a new trampoline and a check for compatibility | |
2762 | */ | |
2763 | struct bpf_attach_target_info tgt_info = {}; | |
2764 | ||
2765 | err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id, | |
2766 | &tgt_info); | |
2767 | if (err) | |
2768 | goto out_unlock; | |
2769 | ||
2770 | tr = bpf_trampoline_get(key, &tgt_info); | |
2771 | if (!tr) { | |
2772 | err = -ENOMEM; | |
2773 | goto out_unlock; | |
2774 | } | |
2775 | } else { | |
2776 | /* The caller didn't specify a target, or the target was the | |
2777 | * same as the destination supplied during program load. This | |
2778 | * means we can reuse the trampoline and reference from program | |
2779 | * load time, and there is no need to allocate a new one. This | |
2780 | * can only happen once for any program, as the saved values in | |
2781 | * prog->aux are cleared below. | |
2782 | */ | |
2783 | tr = prog->aux->dst_trampoline; | |
2784 | tgt_prog = prog->aux->dst_prog; | |
2785 | } | |
3aac1ead THJ |
2786 | |
2787 | err = bpf_link_prime(&link->link, &link_primer); | |
2788 | if (err) | |
2789 | goto out_unlock; | |
fec56f58 | 2790 | |
3aac1ead | 2791 | err = bpf_trampoline_link_prog(prog, tr); |
babf3164 | 2792 | if (err) { |
a3b80e10 | 2793 | bpf_link_cleanup(&link_primer); |
3aac1ead THJ |
2794 | link = NULL; |
2795 | goto out_unlock; | |
fec56f58 | 2796 | } |
babf3164 | 2797 | |
3aac1ead THJ |
2798 | link->tgt_prog = tgt_prog; |
2799 | link->trampoline = tr; | |
2800 | ||
4a1e7c0c THJ |
2801 | /* Always clear the trampoline and target prog from prog->aux to make |
2802 | * sure the original attach destination is not kept alive after a | |
2803 | * program is (re-)attached to another target. | |
2804 | */ | |
2805 | if (prog->aux->dst_prog && | |
2806 | (tgt_prog_fd || tr != prog->aux->dst_trampoline)) | |
2807 | /* got extra prog ref from syscall, or attaching to different prog */ | |
2808 | bpf_prog_put(prog->aux->dst_prog); | |
2809 | if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline) | |
2810 | /* we allocated a new trampoline, so free the old one */ | |
2811 | bpf_trampoline_put(prog->aux->dst_trampoline); | |
2812 | ||
3aac1ead THJ |
2813 | prog->aux->dst_prog = NULL; |
2814 | prog->aux->dst_trampoline = NULL; | |
2815 | mutex_unlock(&prog->aux->dst_mutex); | |
2816 | ||
a3b80e10 | 2817 | return bpf_link_settle(&link_primer); |
3aac1ead | 2818 | out_unlock: |
4a1e7c0c THJ |
2819 | if (tr && tr != prog->aux->dst_trampoline) |
2820 | bpf_trampoline_put(tr); | |
3aac1ead THJ |
2821 | mutex_unlock(&prog->aux->dst_mutex); |
2822 | kfree(link); | |
fec56f58 | 2823 | out_put_prog: |
4a1e7c0c THJ |
2824 | if (tgt_prog_fd && tgt_prog) |
2825 | bpf_prog_put(tgt_prog); | |
fec56f58 AS |
2826 | return err; |
2827 | } | |
2828 | ||
70ed506c AN |
2829 | struct bpf_raw_tp_link { |
2830 | struct bpf_link link; | |
c4f6699d | 2831 | struct bpf_raw_event_map *btp; |
c4f6699d AS |
2832 | }; |
2833 | ||
70ed506c | 2834 | static void bpf_raw_tp_link_release(struct bpf_link *link) |
c4f6699d | 2835 | { |
70ed506c AN |
2836 | struct bpf_raw_tp_link *raw_tp = |
2837 | container_of(link, struct bpf_raw_tp_link, link); | |
c4f6699d | 2838 | |
70ed506c | 2839 | bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog); |
a38d1107 | 2840 | bpf_put_raw_tracepoint(raw_tp->btp); |
babf3164 AN |
2841 | } |
2842 | ||
2843 | static void bpf_raw_tp_link_dealloc(struct bpf_link *link) | |
2844 | { | |
2845 | struct bpf_raw_tp_link *raw_tp = | |
2846 | container_of(link, struct bpf_raw_tp_link, link); | |
2847 | ||
c4f6699d | 2848 | kfree(raw_tp); |
c4f6699d AS |
2849 | } |
2850 | ||
f2e10bff AN |
2851 | static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link, |
2852 | struct seq_file *seq) | |
2853 | { | |
2854 | struct bpf_raw_tp_link *raw_tp_link = | |
2855 | container_of(link, struct bpf_raw_tp_link, link); | |
2856 | ||
2857 | seq_printf(seq, | |
2858 | "tp_name:\t%s\n", | |
2859 | raw_tp_link->btp->tp->name); | |
2860 | } | |
2861 | ||
2862 | static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, | |
2863 | struct bpf_link_info *info) | |
2864 | { | |
2865 | struct bpf_raw_tp_link *raw_tp_link = | |
2866 | container_of(link, struct bpf_raw_tp_link, link); | |
2867 | char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name); | |
2868 | const char *tp_name = raw_tp_link->btp->tp->name; | |
2869 | u32 ulen = info->raw_tracepoint.tp_name_len; | |
2870 | size_t tp_len = strlen(tp_name); | |
2871 | ||
b474959d | 2872 | if (!ulen ^ !ubuf) |
f2e10bff AN |
2873 | return -EINVAL; |
2874 | ||
2875 | info->raw_tracepoint.tp_name_len = tp_len + 1; | |
2876 | ||
2877 | if (!ubuf) | |
2878 | return 0; | |
2879 | ||
2880 | if (ulen >= tp_len + 1) { | |
2881 | if (copy_to_user(ubuf, tp_name, tp_len + 1)) | |
2882 | return -EFAULT; | |
2883 | } else { | |
2884 | char zero = '\0'; | |
2885 | ||
2886 | if (copy_to_user(ubuf, tp_name, ulen - 1)) | |
2887 | return -EFAULT; | |
2888 | if (put_user(zero, ubuf + ulen - 1)) | |
2889 | return -EFAULT; | |
2890 | return -ENOSPC; | |
2891 | } | |
2892 | ||
2893 | return 0; | |
2894 | } | |
2895 | ||
a3b80e10 | 2896 | static const struct bpf_link_ops bpf_raw_tp_link_lops = { |
70ed506c | 2897 | .release = bpf_raw_tp_link_release, |
babf3164 | 2898 | .dealloc = bpf_raw_tp_link_dealloc, |
f2e10bff AN |
2899 | .show_fdinfo = bpf_raw_tp_link_show_fdinfo, |
2900 | .fill_link_info = bpf_raw_tp_link_fill_link_info, | |
c4f6699d AS |
2901 | }; |
2902 | ||
b89fbfbb AN |
2903 | #ifdef CONFIG_PERF_EVENTS |
2904 | struct bpf_perf_link { | |
2905 | struct bpf_link link; | |
2906 | struct file *perf_file; | |
2907 | }; | |
2908 | ||
2909 | static void bpf_perf_link_release(struct bpf_link *link) | |
2910 | { | |
2911 | struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); | |
2912 | struct perf_event *event = perf_link->perf_file->private_data; | |
2913 | ||
2914 | perf_event_free_bpf_prog(event); | |
2915 | fput(perf_link->perf_file); | |
2916 | } | |
2917 | ||
2918 | static void bpf_perf_link_dealloc(struct bpf_link *link) | |
2919 | { | |
2920 | struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link); | |
2921 | ||
2922 | kfree(perf_link); | |
2923 | } | |
2924 | ||
2925 | static const struct bpf_link_ops bpf_perf_link_lops = { | |
2926 | .release = bpf_perf_link_release, | |
2927 | .dealloc = bpf_perf_link_dealloc, | |
2928 | }; | |
2929 | ||
2930 | static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) | |
2931 | { | |
2932 | struct bpf_link_primer link_primer; | |
2933 | struct bpf_perf_link *link; | |
2934 | struct perf_event *event; | |
2935 | struct file *perf_file; | |
2936 | int err; | |
2937 | ||
2938 | if (attr->link_create.flags) | |
2939 | return -EINVAL; | |
2940 | ||
2941 | perf_file = perf_event_get(attr->link_create.target_fd); | |
2942 | if (IS_ERR(perf_file)) | |
2943 | return PTR_ERR(perf_file); | |
2944 | ||
2945 | link = kzalloc(sizeof(*link), GFP_USER); | |
2946 | if (!link) { | |
2947 | err = -ENOMEM; | |
2948 | goto out_put_file; | |
2949 | } | |
2950 | bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog); | |
2951 | link->perf_file = perf_file; | |
2952 | ||
2953 | err = bpf_link_prime(&link->link, &link_primer); | |
2954 | if (err) { | |
2955 | kfree(link); | |
2956 | goto out_put_file; | |
2957 | } | |
2958 | ||
2959 | event = perf_file->private_data; | |
82e6b1ee | 2960 | err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie); |
b89fbfbb AN |
2961 | if (err) { |
2962 | bpf_link_cleanup(&link_primer); | |
2963 | goto out_put_file; | |
2964 | } | |
2965 | /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */ | |
2966 | bpf_prog_inc(prog); | |
2967 | ||
2968 | return bpf_link_settle(&link_primer); | |
2969 | ||
2970 | out_put_file: | |
2971 | fput(perf_file); | |
2972 | return err; | |
2973 | } | |
2974 | #endif /* CONFIG_PERF_EVENTS */ | |
2975 | ||
c4f6699d AS |
2976 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd |
2977 | ||
2978 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) | |
2979 | { | |
a3b80e10 | 2980 | struct bpf_link_primer link_primer; |
babf3164 | 2981 | struct bpf_raw_tp_link *link; |
c4f6699d AS |
2982 | struct bpf_raw_event_map *btp; |
2983 | struct bpf_prog *prog; | |
ac4414b5 AS |
2984 | const char *tp_name; |
2985 | char buf[128]; | |
a3b80e10 | 2986 | int err; |
c4f6699d | 2987 | |
ac4414b5 AS |
2988 | if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN)) |
2989 | return -EINVAL; | |
2990 | ||
2991 | prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); | |
2992 | if (IS_ERR(prog)) | |
2993 | return PTR_ERR(prog); | |
2994 | ||
9e4e01df KS |
2995 | switch (prog->type) { |
2996 | case BPF_PROG_TYPE_TRACING: | |
2997 | case BPF_PROG_TYPE_EXT: | |
2998 | case BPF_PROG_TYPE_LSM: | |
ac4414b5 | 2999 | if (attr->raw_tracepoint.name) { |
fec56f58 AS |
3000 | /* The attach point for this category of programs |
3001 | * should be specified via btf_id during program load. | |
ac4414b5 AS |
3002 | */ |
3003 | err = -EINVAL; | |
3004 | goto out_put_prog; | |
3005 | } | |
9e4e01df KS |
3006 | if (prog->type == BPF_PROG_TYPE_TRACING && |
3007 | prog->expected_attach_type == BPF_TRACE_RAW_TP) { | |
fec56f58 | 3008 | tp_name = prog->aux->attach_func_name; |
9e4e01df KS |
3009 | break; |
3010 | } | |
5541075a JO |
3011 | err = bpf_tracing_prog_attach(prog, 0, 0); |
3012 | if (err >= 0) | |
3013 | return err; | |
3014 | goto out_put_prog; | |
9e4e01df KS |
3015 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
3016 | case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: | |
ac4414b5 AS |
3017 | if (strncpy_from_user(buf, |
3018 | u64_to_user_ptr(attr->raw_tracepoint.name), | |
3019 | sizeof(buf) - 1) < 0) { | |
3020 | err = -EFAULT; | |
3021 | goto out_put_prog; | |
3022 | } | |
3023 | buf[sizeof(buf) - 1] = 0; | |
3024 | tp_name = buf; | |
9e4e01df KS |
3025 | break; |
3026 | default: | |
3027 | err = -EINVAL; | |
3028 | goto out_put_prog; | |
ac4414b5 | 3029 | } |
c4f6699d | 3030 | |
a38d1107 | 3031 | btp = bpf_get_raw_tracepoint(tp_name); |
ac4414b5 AS |
3032 | if (!btp) { |
3033 | err = -ENOENT; | |
3034 | goto out_put_prog; | |
3035 | } | |
c4f6699d | 3036 | |
babf3164 AN |
3037 | link = kzalloc(sizeof(*link), GFP_USER); |
3038 | if (!link) { | |
a38d1107 MM |
3039 | err = -ENOMEM; |
3040 | goto out_put_btp; | |
3041 | } | |
f2e10bff AN |
3042 | bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT, |
3043 | &bpf_raw_tp_link_lops, prog); | |
babf3164 | 3044 | link->btp = btp; |
c4f6699d | 3045 | |
a3b80e10 AN |
3046 | err = bpf_link_prime(&link->link, &link_primer); |
3047 | if (err) { | |
babf3164 | 3048 | kfree(link); |
babf3164 AN |
3049 | goto out_put_btp; |
3050 | } | |
c4f6699d | 3051 | |
babf3164 AN |
3052 | err = bpf_probe_register(link->btp, prog); |
3053 | if (err) { | |
a3b80e10 | 3054 | bpf_link_cleanup(&link_primer); |
babf3164 | 3055 | goto out_put_btp; |
c4f6699d | 3056 | } |
babf3164 | 3057 | |
a3b80e10 | 3058 | return bpf_link_settle(&link_primer); |
c4f6699d | 3059 | |
a38d1107 MM |
3060 | out_put_btp: |
3061 | bpf_put_raw_tracepoint(btp); | |
ac4414b5 AS |
3062 | out_put_prog: |
3063 | bpf_prog_put(prog); | |
c4f6699d AS |
3064 | return err; |
3065 | } | |
3066 | ||
33491588 AR |
3067 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
3068 | enum bpf_attach_type attach_type) | |
3069 | { | |
3070 | switch (prog->type) { | |
3071 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
3072 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: | |
0d01da6a | 3073 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
e9ddbb77 | 3074 | case BPF_PROG_TYPE_SK_LOOKUP: |
33491588 | 3075 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; |
5cf1e914 | 3076 | case BPF_PROG_TYPE_CGROUP_SKB: |
2c78ee89 AS |
3077 | if (!capable(CAP_NET_ADMIN)) |
3078 | /* cg-skb progs can be loaded by unpriv user. | |
3079 | * check permissions at attach time. | |
3080 | */ | |
3081 | return -EPERM; | |
5cf1e914 | 3082 | return prog->enforce_expected_attach_type && |
3083 | prog->expected_attach_type != attach_type ? | |
3084 | -EINVAL : 0; | |
33491588 AR |
3085 | default: |
3086 | return 0; | |
3087 | } | |
3088 | } | |
3089 | ||
e28784e3 AN |
3090 | static enum bpf_prog_type |
3091 | attach_type_to_prog_type(enum bpf_attach_type attach_type) | |
f4324551 | 3092 | { |
e28784e3 | 3093 | switch (attach_type) { |
f4324551 DM |
3094 | case BPF_CGROUP_INET_INGRESS: |
3095 | case BPF_CGROUP_INET_EGRESS: | |
e28784e3 | 3096 | return BPF_PROG_TYPE_CGROUP_SKB; |
61023658 | 3097 | case BPF_CGROUP_INET_SOCK_CREATE: |
f5836749 | 3098 | case BPF_CGROUP_INET_SOCK_RELEASE: |
aac3fc32 AI |
3099 | case BPF_CGROUP_INET4_POST_BIND: |
3100 | case BPF_CGROUP_INET6_POST_BIND: | |
e28784e3 | 3101 | return BPF_PROG_TYPE_CGROUP_SOCK; |
4fbac77d AI |
3102 | case BPF_CGROUP_INET4_BIND: |
3103 | case BPF_CGROUP_INET6_BIND: | |
d74bad4e AI |
3104 | case BPF_CGROUP_INET4_CONNECT: |
3105 | case BPF_CGROUP_INET6_CONNECT: | |
1b66d253 DB |
3106 | case BPF_CGROUP_INET4_GETPEERNAME: |
3107 | case BPF_CGROUP_INET6_GETPEERNAME: | |
3108 | case BPF_CGROUP_INET4_GETSOCKNAME: | |
3109 | case BPF_CGROUP_INET6_GETSOCKNAME: | |
1cedee13 AI |
3110 | case BPF_CGROUP_UDP4_SENDMSG: |
3111 | case BPF_CGROUP_UDP6_SENDMSG: | |
983695fa DB |
3112 | case BPF_CGROUP_UDP4_RECVMSG: |
3113 | case BPF_CGROUP_UDP6_RECVMSG: | |
e28784e3 | 3114 | return BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
40304b2a | 3115 | case BPF_CGROUP_SOCK_OPS: |
e28784e3 | 3116 | return BPF_PROG_TYPE_SOCK_OPS; |
ebc614f6 | 3117 | case BPF_CGROUP_DEVICE: |
e28784e3 | 3118 | return BPF_PROG_TYPE_CGROUP_DEVICE; |
4f738adb | 3119 | case BPF_SK_MSG_VERDICT: |
e28784e3 | 3120 | return BPF_PROG_TYPE_SK_MSG; |
464bc0fd JF |
3121 | case BPF_SK_SKB_STREAM_PARSER: |
3122 | case BPF_SK_SKB_STREAM_VERDICT: | |
a7ba4558 | 3123 | case BPF_SK_SKB_VERDICT: |
e28784e3 | 3124 | return BPF_PROG_TYPE_SK_SKB; |
f4364dcf | 3125 | case BPF_LIRC_MODE2: |
e28784e3 | 3126 | return BPF_PROG_TYPE_LIRC_MODE2; |
d58e468b | 3127 | case BPF_FLOW_DISSECTOR: |
e28784e3 | 3128 | return BPF_PROG_TYPE_FLOW_DISSECTOR; |
7b146ceb | 3129 | case BPF_CGROUP_SYSCTL: |
e28784e3 | 3130 | return BPF_PROG_TYPE_CGROUP_SYSCTL; |
0d01da6a SF |
3131 | case BPF_CGROUP_GETSOCKOPT: |
3132 | case BPF_CGROUP_SETSOCKOPT: | |
e28784e3 | 3133 | return BPF_PROG_TYPE_CGROUP_SOCKOPT; |
de4e05ca YS |
3134 | case BPF_TRACE_ITER: |
3135 | return BPF_PROG_TYPE_TRACING; | |
e9ddbb77 JS |
3136 | case BPF_SK_LOOKUP: |
3137 | return BPF_PROG_TYPE_SK_LOOKUP; | |
aa8d3a71 AN |
3138 | case BPF_XDP: |
3139 | return BPF_PROG_TYPE_XDP; | |
f4324551 | 3140 | default: |
e28784e3 | 3141 | return BPF_PROG_TYPE_UNSPEC; |
f4324551 | 3142 | } |
e28784e3 AN |
3143 | } |
3144 | ||
3145 | #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd | |
3146 | ||
3147 | #define BPF_F_ATTACH_MASK \ | |
3148 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE) | |
3149 | ||
3150 | static int bpf_prog_attach(const union bpf_attr *attr) | |
3151 | { | |
3152 | enum bpf_prog_type ptype; | |
3153 | struct bpf_prog *prog; | |
3154 | int ret; | |
3155 | ||
e28784e3 AN |
3156 | if (CHECK_ATTR(BPF_PROG_ATTACH)) |
3157 | return -EINVAL; | |
3158 | ||
3159 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) | |
3160 | return -EINVAL; | |
3161 | ||
3162 | ptype = attach_type_to_prog_type(attr->attach_type); | |
3163 | if (ptype == BPF_PROG_TYPE_UNSPEC) | |
3164 | return -EINVAL; | |
f4324551 | 3165 | |
b2cd1257 DA |
3166 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
3167 | if (IS_ERR(prog)) | |
3168 | return PTR_ERR(prog); | |
3169 | ||
5e43f899 AI |
3170 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
3171 | bpf_prog_put(prog); | |
3172 | return -EINVAL; | |
3173 | } | |
3174 | ||
fdb5c453 SY |
3175 | switch (ptype) { |
3176 | case BPF_PROG_TYPE_SK_SKB: | |
3177 | case BPF_PROG_TYPE_SK_MSG: | |
604326b4 | 3178 | ret = sock_map_get_from_fd(attr, prog); |
fdb5c453 SY |
3179 | break; |
3180 | case BPF_PROG_TYPE_LIRC_MODE2: | |
3181 | ret = lirc_prog_attach(attr, prog); | |
3182 | break; | |
d58e468b | 3183 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
a3fd7cee | 3184 | ret = netns_bpf_prog_attach(attr, prog); |
d58e468b | 3185 | break; |
e28784e3 AN |
3186 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
3187 | case BPF_PROG_TYPE_CGROUP_SKB: | |
3188 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
3189 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: | |
3190 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
3191 | case BPF_PROG_TYPE_CGROUP_SYSCTL: | |
3192 | case BPF_PROG_TYPE_SOCK_OPS: | |
fdb5c453 | 3193 | ret = cgroup_bpf_prog_attach(attr, ptype, prog); |
e28784e3 AN |
3194 | break; |
3195 | default: | |
3196 | ret = -EINVAL; | |
b2cd1257 DA |
3197 | } |
3198 | ||
7f677633 AS |
3199 | if (ret) |
3200 | bpf_prog_put(prog); | |
7f677633 | 3201 | return ret; |
f4324551 DM |
3202 | } |
3203 | ||
3204 | #define BPF_PROG_DETACH_LAST_FIELD attach_type | |
3205 | ||
3206 | static int bpf_prog_detach(const union bpf_attr *attr) | |
3207 | { | |
324bda9e | 3208 | enum bpf_prog_type ptype; |
f4324551 | 3209 | |
f4324551 DM |
3210 | if (CHECK_ATTR(BPF_PROG_DETACH)) |
3211 | return -EINVAL; | |
3212 | ||
e28784e3 AN |
3213 | ptype = attach_type_to_prog_type(attr->attach_type); |
3214 | ||
3215 | switch (ptype) { | |
3216 | case BPF_PROG_TYPE_SK_MSG: | |
3217 | case BPF_PROG_TYPE_SK_SKB: | |
bb0de313 | 3218 | return sock_map_prog_detach(attr, ptype); |
e28784e3 | 3219 | case BPF_PROG_TYPE_LIRC_MODE2: |
f4364dcf | 3220 | return lirc_prog_detach(attr); |
e28784e3 | 3221 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
4ac2add6 | 3222 | return netns_bpf_prog_detach(attr, ptype); |
e28784e3 AN |
3223 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
3224 | case BPF_PROG_TYPE_CGROUP_SKB: | |
3225 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
3226 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: | |
3227 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
3228 | case BPF_PROG_TYPE_CGROUP_SYSCTL: | |
3229 | case BPF_PROG_TYPE_SOCK_OPS: | |
3230 | return cgroup_bpf_prog_detach(attr, ptype); | |
f4324551 DM |
3231 | default: |
3232 | return -EINVAL; | |
3233 | } | |
f4324551 | 3234 | } |
40304b2a | 3235 | |
468e2f64 AS |
3236 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
3237 | ||
3238 | static int bpf_prog_query(const union bpf_attr *attr, | |
3239 | union bpf_attr __user *uattr) | |
3240 | { | |
468e2f64 AS |
3241 | if (!capable(CAP_NET_ADMIN)) |
3242 | return -EPERM; | |
3243 | if (CHECK_ATTR(BPF_PROG_QUERY)) | |
3244 | return -EINVAL; | |
3245 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) | |
3246 | return -EINVAL; | |
3247 | ||
3248 | switch (attr->query.attach_type) { | |
3249 | case BPF_CGROUP_INET_INGRESS: | |
3250 | case BPF_CGROUP_INET_EGRESS: | |
3251 | case BPF_CGROUP_INET_SOCK_CREATE: | |
f5836749 | 3252 | case BPF_CGROUP_INET_SOCK_RELEASE: |
4fbac77d AI |
3253 | case BPF_CGROUP_INET4_BIND: |
3254 | case BPF_CGROUP_INET6_BIND: | |
aac3fc32 AI |
3255 | case BPF_CGROUP_INET4_POST_BIND: |
3256 | case BPF_CGROUP_INET6_POST_BIND: | |
d74bad4e AI |
3257 | case BPF_CGROUP_INET4_CONNECT: |
3258 | case BPF_CGROUP_INET6_CONNECT: | |
1b66d253 DB |
3259 | case BPF_CGROUP_INET4_GETPEERNAME: |
3260 | case BPF_CGROUP_INET6_GETPEERNAME: | |
3261 | case BPF_CGROUP_INET4_GETSOCKNAME: | |
3262 | case BPF_CGROUP_INET6_GETSOCKNAME: | |
1cedee13 AI |
3263 | case BPF_CGROUP_UDP4_SENDMSG: |
3264 | case BPF_CGROUP_UDP6_SENDMSG: | |
983695fa DB |
3265 | case BPF_CGROUP_UDP4_RECVMSG: |
3266 | case BPF_CGROUP_UDP6_RECVMSG: | |
468e2f64 | 3267 | case BPF_CGROUP_SOCK_OPS: |
ebc614f6 | 3268 | case BPF_CGROUP_DEVICE: |
7b146ceb | 3269 | case BPF_CGROUP_SYSCTL: |
0d01da6a SF |
3270 | case BPF_CGROUP_GETSOCKOPT: |
3271 | case BPF_CGROUP_SETSOCKOPT: | |
e28784e3 | 3272 | return cgroup_bpf_prog_query(attr, uattr); |
f4364dcf SY |
3273 | case BPF_LIRC_MODE2: |
3274 | return lirc_prog_query(attr, uattr); | |
118c8e9a | 3275 | case BPF_FLOW_DISSECTOR: |
e9ddbb77 | 3276 | case BPF_SK_LOOKUP: |
a3fd7cee | 3277 | return netns_bpf_prog_query(attr, uattr); |
468e2f64 AS |
3278 | default: |
3279 | return -EINVAL; | |
3280 | } | |
468e2f64 | 3281 | } |
f4324551 | 3282 | |
1b4d60ec | 3283 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu |
1cf1cae9 AS |
3284 | |
3285 | static int bpf_prog_test_run(const union bpf_attr *attr, | |
3286 | union bpf_attr __user *uattr) | |
3287 | { | |
3288 | struct bpf_prog *prog; | |
3289 | int ret = -ENOTSUPP; | |
3290 | ||
3291 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) | |
3292 | return -EINVAL; | |
3293 | ||
b0b9395d SF |
3294 | if ((attr->test.ctx_size_in && !attr->test.ctx_in) || |
3295 | (!attr->test.ctx_size_in && attr->test.ctx_in)) | |
3296 | return -EINVAL; | |
3297 | ||
3298 | if ((attr->test.ctx_size_out && !attr->test.ctx_out) || | |
3299 | (!attr->test.ctx_size_out && attr->test.ctx_out)) | |
3300 | return -EINVAL; | |
3301 | ||
1cf1cae9 AS |
3302 | prog = bpf_prog_get(attr->test.prog_fd); |
3303 | if (IS_ERR(prog)) | |
3304 | return PTR_ERR(prog); | |
3305 | ||
3306 | if (prog->aux->ops->test_run) | |
3307 | ret = prog->aux->ops->test_run(prog, attr, uattr); | |
3308 | ||
3309 | bpf_prog_put(prog); | |
3310 | return ret; | |
3311 | } | |
3312 | ||
34ad5580 MKL |
3313 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
3314 | ||
3315 | static int bpf_obj_get_next_id(const union bpf_attr *attr, | |
3316 | union bpf_attr __user *uattr, | |
3317 | struct idr *idr, | |
3318 | spinlock_t *lock) | |
3319 | { | |
3320 | u32 next_id = attr->start_id; | |
3321 | int err = 0; | |
3322 | ||
3323 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) | |
3324 | return -EINVAL; | |
3325 | ||
3326 | if (!capable(CAP_SYS_ADMIN)) | |
3327 | return -EPERM; | |
3328 | ||
3329 | next_id++; | |
3330 | spin_lock_bh(lock); | |
3331 | if (!idr_get_next(idr, &next_id)) | |
3332 | err = -ENOENT; | |
3333 | spin_unlock_bh(lock); | |
3334 | ||
3335 | if (!err) | |
3336 | err = put_user(next_id, &uattr->next_id); | |
3337 | ||
3338 | return err; | |
3339 | } | |
3340 | ||
6086d29d YS |
3341 | struct bpf_map *bpf_map_get_curr_or_next(u32 *id) |
3342 | { | |
3343 | struct bpf_map *map; | |
3344 | ||
3345 | spin_lock_bh(&map_idr_lock); | |
3346 | again: | |
3347 | map = idr_get_next(&map_idr, id); | |
3348 | if (map) { | |
3349 | map = __bpf_map_inc_not_zero(map, false); | |
3350 | if (IS_ERR(map)) { | |
3351 | (*id)++; | |
3352 | goto again; | |
3353 | } | |
3354 | } | |
3355 | spin_unlock_bh(&map_idr_lock); | |
3356 | ||
3357 | return map; | |
3358 | } | |
3359 | ||
a228a64f AS |
3360 | struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id) |
3361 | { | |
3362 | struct bpf_prog *prog; | |
3363 | ||
3364 | spin_lock_bh(&prog_idr_lock); | |
3365 | again: | |
3366 | prog = idr_get_next(&prog_idr, id); | |
3367 | if (prog) { | |
3368 | prog = bpf_prog_inc_not_zero(prog); | |
3369 | if (IS_ERR(prog)) { | |
3370 | (*id)++; | |
3371 | goto again; | |
3372 | } | |
3373 | } | |
3374 | spin_unlock_bh(&prog_idr_lock); | |
3375 | ||
3376 | return prog; | |
3377 | } | |
3378 | ||
b16d9aa4 MKL |
3379 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
3380 | ||
7e6897f9 | 3381 | struct bpf_prog *bpf_prog_by_id(u32 id) |
b16d9aa4 MKL |
3382 | { |
3383 | struct bpf_prog *prog; | |
b16d9aa4 | 3384 | |
7e6897f9 BT |
3385 | if (!id) |
3386 | return ERR_PTR(-ENOENT); | |
b16d9aa4 MKL |
3387 | |
3388 | spin_lock_bh(&prog_idr_lock); | |
3389 | prog = idr_find(&prog_idr, id); | |
3390 | if (prog) | |
3391 | prog = bpf_prog_inc_not_zero(prog); | |
3392 | else | |
3393 | prog = ERR_PTR(-ENOENT); | |
3394 | spin_unlock_bh(&prog_idr_lock); | |
7e6897f9 BT |
3395 | return prog; |
3396 | } | |
3397 | ||
3398 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) | |
3399 | { | |
3400 | struct bpf_prog *prog; | |
3401 | u32 id = attr->prog_id; | |
3402 | int fd; | |
3403 | ||
3404 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) | |
3405 | return -EINVAL; | |
3406 | ||
3407 | if (!capable(CAP_SYS_ADMIN)) | |
3408 | return -EPERM; | |
b16d9aa4 | 3409 | |
7e6897f9 | 3410 | prog = bpf_prog_by_id(id); |
b16d9aa4 MKL |
3411 | if (IS_ERR(prog)) |
3412 | return PTR_ERR(prog); | |
3413 | ||
3414 | fd = bpf_prog_new_fd(prog); | |
3415 | if (fd < 0) | |
3416 | bpf_prog_put(prog); | |
3417 | ||
3418 | return fd; | |
3419 | } | |
3420 | ||
6e71b04a | 3421 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
bd5f5f4e MKL |
3422 | |
3423 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) | |
3424 | { | |
3425 | struct bpf_map *map; | |
3426 | u32 id = attr->map_id; | |
6e71b04a | 3427 | int f_flags; |
bd5f5f4e MKL |
3428 | int fd; |
3429 | ||
6e71b04a CF |
3430 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
3431 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) | |
bd5f5f4e MKL |
3432 | return -EINVAL; |
3433 | ||
3434 | if (!capable(CAP_SYS_ADMIN)) | |
3435 | return -EPERM; | |
3436 | ||
6e71b04a CF |
3437 | f_flags = bpf_get_file_flag(attr->open_flags); |
3438 | if (f_flags < 0) | |
3439 | return f_flags; | |
3440 | ||
bd5f5f4e MKL |
3441 | spin_lock_bh(&map_idr_lock); |
3442 | map = idr_find(&map_idr, id); | |
3443 | if (map) | |
b0e4701c | 3444 | map = __bpf_map_inc_not_zero(map, true); |
bd5f5f4e MKL |
3445 | else |
3446 | map = ERR_PTR(-ENOENT); | |
3447 | spin_unlock_bh(&map_idr_lock); | |
3448 | ||
3449 | if (IS_ERR(map)) | |
3450 | return PTR_ERR(map); | |
3451 | ||
6e71b04a | 3452 | fd = bpf_map_new_fd(map, f_flags); |
bd5f5f4e | 3453 | if (fd < 0) |
781e6282 | 3454 | bpf_map_put_with_uref(map); |
bd5f5f4e MKL |
3455 | |
3456 | return fd; | |
3457 | } | |
3458 | ||
7105e828 | 3459 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
d8eca5bb DB |
3460 | unsigned long addr, u32 *off, |
3461 | u32 *type) | |
7105e828 | 3462 | { |
d8eca5bb | 3463 | const struct bpf_map *map; |
7105e828 DB |
3464 | int i; |
3465 | ||
984fe94f | 3466 | mutex_lock(&prog->aux->used_maps_mutex); |
d8eca5bb DB |
3467 | for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { |
3468 | map = prog->aux->used_maps[i]; | |
3469 | if (map == (void *)addr) { | |
3470 | *type = BPF_PSEUDO_MAP_FD; | |
984fe94f | 3471 | goto out; |
d8eca5bb DB |
3472 | } |
3473 | if (!map->ops->map_direct_value_meta) | |
3474 | continue; | |
3475 | if (!map->ops->map_direct_value_meta(map, addr, off)) { | |
3476 | *type = BPF_PSEUDO_MAP_VALUE; | |
984fe94f | 3477 | goto out; |
d8eca5bb DB |
3478 | } |
3479 | } | |
984fe94f | 3480 | map = NULL; |
d8eca5bb | 3481 | |
984fe94f YZ |
3482 | out: |
3483 | mutex_unlock(&prog->aux->used_maps_mutex); | |
3484 | return map; | |
7105e828 DB |
3485 | } |
3486 | ||
63960260 KC |
3487 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog, |
3488 | const struct cred *f_cred) | |
7105e828 DB |
3489 | { |
3490 | const struct bpf_map *map; | |
3491 | struct bpf_insn *insns; | |
d8eca5bb | 3492 | u32 off, type; |
7105e828 | 3493 | u64 imm; |
29fcb05b | 3494 | u8 code; |
7105e828 DB |
3495 | int i; |
3496 | ||
3497 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), | |
3498 | GFP_USER); | |
3499 | if (!insns) | |
3500 | return insns; | |
3501 | ||
3502 | for (i = 0; i < prog->len; i++) { | |
29fcb05b AN |
3503 | code = insns[i].code; |
3504 | ||
3505 | if (code == (BPF_JMP | BPF_TAIL_CALL)) { | |
7105e828 DB |
3506 | insns[i].code = BPF_JMP | BPF_CALL; |
3507 | insns[i].imm = BPF_FUNC_tail_call; | |
3508 | /* fall-through */ | |
3509 | } | |
29fcb05b AN |
3510 | if (code == (BPF_JMP | BPF_CALL) || |
3511 | code == (BPF_JMP | BPF_CALL_ARGS)) { | |
3512 | if (code == (BPF_JMP | BPF_CALL_ARGS)) | |
7105e828 | 3513 | insns[i].code = BPF_JMP | BPF_CALL; |
63960260 | 3514 | if (!bpf_dump_raw_ok(f_cred)) |
7105e828 DB |
3515 | insns[i].imm = 0; |
3516 | continue; | |
3517 | } | |
29fcb05b AN |
3518 | if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) { |
3519 | insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM; | |
3520 | continue; | |
3521 | } | |
7105e828 | 3522 | |
29fcb05b | 3523 | if (code != (BPF_LD | BPF_IMM | BPF_DW)) |
7105e828 DB |
3524 | continue; |
3525 | ||
3526 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; | |
d8eca5bb | 3527 | map = bpf_map_from_imm(prog, imm, &off, &type); |
7105e828 | 3528 | if (map) { |
d8eca5bb | 3529 | insns[i].src_reg = type; |
7105e828 | 3530 | insns[i].imm = map->id; |
d8eca5bb | 3531 | insns[i + 1].imm = off; |
7105e828 DB |
3532 | continue; |
3533 | } | |
7105e828 DB |
3534 | } |
3535 | ||
3536 | return insns; | |
3537 | } | |
3538 | ||
c454a46b MKL |
3539 | static int set_info_rec_size(struct bpf_prog_info *info) |
3540 | { | |
3541 | /* | |
3542 | * Ensure info.*_rec_size is the same as kernel expected size | |
3543 | * | |
3544 | * or | |
3545 | * | |
3546 | * Only allow zero *_rec_size if both _rec_size and _cnt are | |
3547 | * zero. In this case, the kernel will set the expected | |
3548 | * _rec_size back to the info. | |
3549 | */ | |
3550 | ||
11d8b82d | 3551 | if ((info->nr_func_info || info->func_info_rec_size) && |
c454a46b MKL |
3552 | info->func_info_rec_size != sizeof(struct bpf_func_info)) |
3553 | return -EINVAL; | |
3554 | ||
11d8b82d | 3555 | if ((info->nr_line_info || info->line_info_rec_size) && |
c454a46b MKL |
3556 | info->line_info_rec_size != sizeof(struct bpf_line_info)) |
3557 | return -EINVAL; | |
3558 | ||
11d8b82d | 3559 | if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && |
c454a46b MKL |
3560 | info->jited_line_info_rec_size != sizeof(__u64)) |
3561 | return -EINVAL; | |
3562 | ||
3563 | info->func_info_rec_size = sizeof(struct bpf_func_info); | |
3564 | info->line_info_rec_size = sizeof(struct bpf_line_info); | |
3565 | info->jited_line_info_rec_size = sizeof(__u64); | |
3566 | ||
3567 | return 0; | |
3568 | } | |
3569 | ||
63960260 KC |
3570 | static int bpf_prog_get_info_by_fd(struct file *file, |
3571 | struct bpf_prog *prog, | |
1e270976 MKL |
3572 | const union bpf_attr *attr, |
3573 | union bpf_attr __user *uattr) | |
3574 | { | |
3575 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
5c6f2588 | 3576 | struct bpf_prog_info info; |
1e270976 | 3577 | u32 info_len = attr->info.info_len; |
5f8f8b93 | 3578 | struct bpf_prog_stats stats; |
1e270976 MKL |
3579 | char __user *uinsns; |
3580 | u32 ulen; | |
3581 | int err; | |
3582 | ||
af2ac3e1 | 3583 | err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); |
1e270976 MKL |
3584 | if (err) |
3585 | return err; | |
3586 | info_len = min_t(u32, sizeof(info), info_len); | |
3587 | ||
5c6f2588 | 3588 | memset(&info, 0, sizeof(info)); |
1e270976 | 3589 | if (copy_from_user(&info, uinfo, info_len)) |
89b09689 | 3590 | return -EFAULT; |
1e270976 MKL |
3591 | |
3592 | info.type = prog->type; | |
3593 | info.id = prog->aux->id; | |
cb4d2b3f MKL |
3594 | info.load_time = prog->aux->load_time; |
3595 | info.created_by_uid = from_kuid_munged(current_user_ns(), | |
3596 | prog->aux->user->uid); | |
b85fab0e | 3597 | info.gpl_compatible = prog->gpl_compatible; |
1e270976 MKL |
3598 | |
3599 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); | |
cb4d2b3f MKL |
3600 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
3601 | ||
984fe94f | 3602 | mutex_lock(&prog->aux->used_maps_mutex); |
cb4d2b3f MKL |
3603 | ulen = info.nr_map_ids; |
3604 | info.nr_map_ids = prog->aux->used_map_cnt; | |
3605 | ulen = min_t(u32, info.nr_map_ids, ulen); | |
3606 | if (ulen) { | |
721e08da | 3607 | u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); |
cb4d2b3f MKL |
3608 | u32 i; |
3609 | ||
3610 | for (i = 0; i < ulen; i++) | |
3611 | if (put_user(prog->aux->used_maps[i]->id, | |
984fe94f YZ |
3612 | &user_map_ids[i])) { |
3613 | mutex_unlock(&prog->aux->used_maps_mutex); | |
cb4d2b3f | 3614 | return -EFAULT; |
984fe94f | 3615 | } |
cb4d2b3f | 3616 | } |
984fe94f | 3617 | mutex_unlock(&prog->aux->used_maps_mutex); |
1e270976 | 3618 | |
c454a46b MKL |
3619 | err = set_info_rec_size(&info); |
3620 | if (err) | |
3621 | return err; | |
7337224f | 3622 | |
5f8f8b93 AS |
3623 | bpf_prog_get_stats(prog, &stats); |
3624 | info.run_time_ns = stats.nsecs; | |
3625 | info.run_cnt = stats.cnt; | |
9ed9e9ba | 3626 | info.recursion_misses = stats.misses; |
5f8f8b93 | 3627 | |
2c78ee89 | 3628 | if (!bpf_capable()) { |
1e270976 MKL |
3629 | info.jited_prog_len = 0; |
3630 | info.xlated_prog_len = 0; | |
dbecd738 | 3631 | info.nr_jited_ksyms = 0; |
28c2fae7 | 3632 | info.nr_jited_func_lens = 0; |
11d8b82d YS |
3633 | info.nr_func_info = 0; |
3634 | info.nr_line_info = 0; | |
3635 | info.nr_jited_line_info = 0; | |
1e270976 MKL |
3636 | goto done; |
3637 | } | |
3638 | ||
1e270976 | 3639 | ulen = info.xlated_prog_len; |
9975a54b | 3640 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
1e270976 | 3641 | if (info.xlated_prog_len && ulen) { |
7105e828 DB |
3642 | struct bpf_insn *insns_sanitized; |
3643 | bool fault; | |
3644 | ||
63960260 | 3645 | if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) { |
7105e828 DB |
3646 | info.xlated_prog_insns = 0; |
3647 | goto done; | |
3648 | } | |
63960260 | 3649 | insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred); |
7105e828 DB |
3650 | if (!insns_sanitized) |
3651 | return -ENOMEM; | |
1e270976 MKL |
3652 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
3653 | ulen = min_t(u32, info.xlated_prog_len, ulen); | |
7105e828 DB |
3654 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
3655 | kfree(insns_sanitized); | |
3656 | if (fault) | |
1e270976 MKL |
3657 | return -EFAULT; |
3658 | } | |
3659 | ||
675fc275 JK |
3660 | if (bpf_prog_is_dev_bound(prog->aux)) { |
3661 | err = bpf_prog_offload_info_fill(&info, prog); | |
3662 | if (err) | |
3663 | return err; | |
fcfb126d JW |
3664 | goto done; |
3665 | } | |
3666 | ||
3667 | /* NOTE: the following code is supposed to be skipped for offload. | |
3668 | * bpf_prog_offload_info_fill() is the place to fill similar fields | |
3669 | * for offload. | |
3670 | */ | |
3671 | ulen = info.jited_prog_len; | |
4d56a76e SD |
3672 | if (prog->aux->func_cnt) { |
3673 | u32 i; | |
3674 | ||
3675 | info.jited_prog_len = 0; | |
3676 | for (i = 0; i < prog->aux->func_cnt; i++) | |
3677 | info.jited_prog_len += prog->aux->func[i]->jited_len; | |
3678 | } else { | |
3679 | info.jited_prog_len = prog->jited_len; | |
3680 | } | |
3681 | ||
fcfb126d | 3682 | if (info.jited_prog_len && ulen) { |
63960260 | 3683 | if (bpf_dump_raw_ok(file->f_cred)) { |
fcfb126d JW |
3684 | uinsns = u64_to_user_ptr(info.jited_prog_insns); |
3685 | ulen = min_t(u32, info.jited_prog_len, ulen); | |
4d56a76e SD |
3686 | |
3687 | /* for multi-function programs, copy the JITed | |
3688 | * instructions for all the functions | |
3689 | */ | |
3690 | if (prog->aux->func_cnt) { | |
3691 | u32 len, free, i; | |
3692 | u8 *img; | |
3693 | ||
3694 | free = ulen; | |
3695 | for (i = 0; i < prog->aux->func_cnt; i++) { | |
3696 | len = prog->aux->func[i]->jited_len; | |
3697 | len = min_t(u32, len, free); | |
3698 | img = (u8 *) prog->aux->func[i]->bpf_func; | |
3699 | if (copy_to_user(uinsns, img, len)) | |
3700 | return -EFAULT; | |
3701 | uinsns += len; | |
3702 | free -= len; | |
3703 | if (!free) | |
3704 | break; | |
3705 | } | |
3706 | } else { | |
3707 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) | |
3708 | return -EFAULT; | |
3709 | } | |
fcfb126d JW |
3710 | } else { |
3711 | info.jited_prog_insns = 0; | |
3712 | } | |
675fc275 JK |
3713 | } |
3714 | ||
dbecd738 | 3715 | ulen = info.nr_jited_ksyms; |
ff1889fc | 3716 | info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; |
7a5725dd | 3717 | if (ulen) { |
63960260 | 3718 | if (bpf_dump_raw_ok(file->f_cred)) { |
ff1889fc | 3719 | unsigned long ksym_addr; |
dbecd738 | 3720 | u64 __user *user_ksyms; |
dbecd738 SD |
3721 | u32 i; |
3722 | ||
3723 | /* copy the address of the kernel symbol | |
3724 | * corresponding to each function | |
3725 | */ | |
3726 | ulen = min_t(u32, info.nr_jited_ksyms, ulen); | |
3727 | user_ksyms = u64_to_user_ptr(info.jited_ksyms); | |
ff1889fc SL |
3728 | if (prog->aux->func_cnt) { |
3729 | for (i = 0; i < ulen; i++) { | |
3730 | ksym_addr = (unsigned long) | |
3731 | prog->aux->func[i]->bpf_func; | |
3732 | if (put_user((u64) ksym_addr, | |
3733 | &user_ksyms[i])) | |
3734 | return -EFAULT; | |
3735 | } | |
3736 | } else { | |
3737 | ksym_addr = (unsigned long) prog->bpf_func; | |
3738 | if (put_user((u64) ksym_addr, &user_ksyms[0])) | |
dbecd738 SD |
3739 | return -EFAULT; |
3740 | } | |
3741 | } else { | |
3742 | info.jited_ksyms = 0; | |
3743 | } | |
3744 | } | |
3745 | ||
815581c1 | 3746 | ulen = info.nr_jited_func_lens; |
ff1889fc | 3747 | info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; |
7a5725dd | 3748 | if (ulen) { |
63960260 | 3749 | if (bpf_dump_raw_ok(file->f_cred)) { |
815581c1 SD |
3750 | u32 __user *user_lens; |
3751 | u32 func_len, i; | |
3752 | ||
3753 | /* copy the JITed image lengths for each function */ | |
3754 | ulen = min_t(u32, info.nr_jited_func_lens, ulen); | |
3755 | user_lens = u64_to_user_ptr(info.jited_func_lens); | |
ff1889fc SL |
3756 | if (prog->aux->func_cnt) { |
3757 | for (i = 0; i < ulen; i++) { | |
3758 | func_len = | |
3759 | prog->aux->func[i]->jited_len; | |
3760 | if (put_user(func_len, &user_lens[i])) | |
3761 | return -EFAULT; | |
3762 | } | |
3763 | } else { | |
3764 | func_len = prog->jited_len; | |
3765 | if (put_user(func_len, &user_lens[0])) | |
815581c1 SD |
3766 | return -EFAULT; |
3767 | } | |
3768 | } else { | |
3769 | info.jited_func_lens = 0; | |
3770 | } | |
3771 | } | |
3772 | ||
7337224f | 3773 | if (prog->aux->btf) |
22dc4a0f | 3774 | info.btf_id = btf_obj_id(prog->aux->btf); |
838e9690 | 3775 | |
11d8b82d YS |
3776 | ulen = info.nr_func_info; |
3777 | info.nr_func_info = prog->aux->func_info_cnt; | |
3778 | if (info.nr_func_info && ulen) { | |
9e794163 | 3779 | char __user *user_finfo; |
7337224f | 3780 | |
9e794163 MKL |
3781 | user_finfo = u64_to_user_ptr(info.func_info); |
3782 | ulen = min_t(u32, info.nr_func_info, ulen); | |
3783 | if (copy_to_user(user_finfo, prog->aux->func_info, | |
3784 | info.func_info_rec_size * ulen)) | |
3785 | return -EFAULT; | |
838e9690 YS |
3786 | } |
3787 | ||
11d8b82d YS |
3788 | ulen = info.nr_line_info; |
3789 | info.nr_line_info = prog->aux->nr_linfo; | |
3790 | if (info.nr_line_info && ulen) { | |
9e794163 | 3791 | __u8 __user *user_linfo; |
c454a46b | 3792 | |
9e794163 MKL |
3793 | user_linfo = u64_to_user_ptr(info.line_info); |
3794 | ulen = min_t(u32, info.nr_line_info, ulen); | |
3795 | if (copy_to_user(user_linfo, prog->aux->linfo, | |
3796 | info.line_info_rec_size * ulen)) | |
3797 | return -EFAULT; | |
c454a46b MKL |
3798 | } |
3799 | ||
11d8b82d | 3800 | ulen = info.nr_jited_line_info; |
c454a46b | 3801 | if (prog->aux->jited_linfo) |
11d8b82d | 3802 | info.nr_jited_line_info = prog->aux->nr_linfo; |
c454a46b | 3803 | else |
11d8b82d YS |
3804 | info.nr_jited_line_info = 0; |
3805 | if (info.nr_jited_line_info && ulen) { | |
63960260 | 3806 | if (bpf_dump_raw_ok(file->f_cred)) { |
c454a46b MKL |
3807 | __u64 __user *user_linfo; |
3808 | u32 i; | |
3809 | ||
3810 | user_linfo = u64_to_user_ptr(info.jited_line_info); | |
11d8b82d | 3811 | ulen = min_t(u32, info.nr_jited_line_info, ulen); |
c454a46b MKL |
3812 | for (i = 0; i < ulen; i++) { |
3813 | if (put_user((__u64)(long)prog->aux->jited_linfo[i], | |
3814 | &user_linfo[i])) | |
3815 | return -EFAULT; | |
3816 | } | |
3817 | } else { | |
3818 | info.jited_line_info = 0; | |
3819 | } | |
3820 | } | |
3821 | ||
c872bdb3 SL |
3822 | ulen = info.nr_prog_tags; |
3823 | info.nr_prog_tags = prog->aux->func_cnt ? : 1; | |
3824 | if (ulen) { | |
3825 | __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; | |
3826 | u32 i; | |
3827 | ||
3828 | user_prog_tags = u64_to_user_ptr(info.prog_tags); | |
3829 | ulen = min_t(u32, info.nr_prog_tags, ulen); | |
3830 | if (prog->aux->func_cnt) { | |
3831 | for (i = 0; i < ulen; i++) { | |
3832 | if (copy_to_user(user_prog_tags[i], | |
3833 | prog->aux->func[i]->tag, | |
3834 | BPF_TAG_SIZE)) | |
3835 | return -EFAULT; | |
3836 | } | |
3837 | } else { | |
3838 | if (copy_to_user(user_prog_tags[0], | |
3839 | prog->tag, BPF_TAG_SIZE)) | |
3840 | return -EFAULT; | |
3841 | } | |
3842 | } | |
3843 | ||
1e270976 MKL |
3844 | done: |
3845 | if (copy_to_user(uinfo, &info, info_len) || | |
3846 | put_user(info_len, &uattr->info.info_len)) | |
3847 | return -EFAULT; | |
3848 | ||
3849 | return 0; | |
3850 | } | |
3851 | ||
63960260 KC |
3852 | static int bpf_map_get_info_by_fd(struct file *file, |
3853 | struct bpf_map *map, | |
1e270976 MKL |
3854 | const union bpf_attr *attr, |
3855 | union bpf_attr __user *uattr) | |
3856 | { | |
3857 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
5c6f2588 | 3858 | struct bpf_map_info info; |
1e270976 MKL |
3859 | u32 info_len = attr->info.info_len; |
3860 | int err; | |
3861 | ||
af2ac3e1 | 3862 | err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); |
1e270976 MKL |
3863 | if (err) |
3864 | return err; | |
3865 | info_len = min_t(u32, sizeof(info), info_len); | |
3866 | ||
5c6f2588 | 3867 | memset(&info, 0, sizeof(info)); |
1e270976 MKL |
3868 | info.type = map->map_type; |
3869 | info.id = map->id; | |
3870 | info.key_size = map->key_size; | |
3871 | info.value_size = map->value_size; | |
3872 | info.max_entries = map->max_entries; | |
3873 | info.map_flags = map->map_flags; | |
ad5b177b | 3874 | memcpy(info.name, map->name, sizeof(map->name)); |
1e270976 | 3875 | |
78958fca | 3876 | if (map->btf) { |
22dc4a0f | 3877 | info.btf_id = btf_obj_id(map->btf); |
9b2cf328 MKL |
3878 | info.btf_key_type_id = map->btf_key_type_id; |
3879 | info.btf_value_type_id = map->btf_value_type_id; | |
78958fca | 3880 | } |
85d33df3 | 3881 | info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id; |
78958fca | 3882 | |
52775b33 JK |
3883 | if (bpf_map_is_dev_bound(map)) { |
3884 | err = bpf_map_offload_info_fill(&info, map); | |
3885 | if (err) | |
3886 | return err; | |
3887 | } | |
3888 | ||
1e270976 MKL |
3889 | if (copy_to_user(uinfo, &info, info_len) || |
3890 | put_user(info_len, &uattr->info.info_len)) | |
3891 | return -EFAULT; | |
3892 | ||
3893 | return 0; | |
3894 | } | |
3895 | ||
63960260 KC |
3896 | static int bpf_btf_get_info_by_fd(struct file *file, |
3897 | struct btf *btf, | |
62dab84c MKL |
3898 | const union bpf_attr *attr, |
3899 | union bpf_attr __user *uattr) | |
3900 | { | |
3901 | struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
3902 | u32 info_len = attr->info.info_len; | |
3903 | int err; | |
3904 | ||
af2ac3e1 | 3905 | err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len); |
62dab84c MKL |
3906 | if (err) |
3907 | return err; | |
3908 | ||
3909 | return btf_get_info_by_fd(btf, attr, uattr); | |
3910 | } | |
3911 | ||
63960260 KC |
3912 | static int bpf_link_get_info_by_fd(struct file *file, |
3913 | struct bpf_link *link, | |
f2e10bff AN |
3914 | const union bpf_attr *attr, |
3915 | union bpf_attr __user *uattr) | |
3916 | { | |
3917 | struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info); | |
3918 | struct bpf_link_info info; | |
3919 | u32 info_len = attr->info.info_len; | |
3920 | int err; | |
3921 | ||
af2ac3e1 | 3922 | err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len); |
f2e10bff AN |
3923 | if (err) |
3924 | return err; | |
3925 | info_len = min_t(u32, sizeof(info), info_len); | |
3926 | ||
3927 | memset(&info, 0, sizeof(info)); | |
3928 | if (copy_from_user(&info, uinfo, info_len)) | |
3929 | return -EFAULT; | |
3930 | ||
3931 | info.type = link->type; | |
3932 | info.id = link->id; | |
3933 | info.prog_id = link->prog->aux->id; | |
3934 | ||
3935 | if (link->ops->fill_link_info) { | |
3936 | err = link->ops->fill_link_info(link, &info); | |
3937 | if (err) | |
3938 | return err; | |
3939 | } | |
3940 | ||
3941 | if (copy_to_user(uinfo, &info, info_len) || | |
3942 | put_user(info_len, &uattr->info.info_len)) | |
3943 | return -EFAULT; | |
3944 | ||
3945 | return 0; | |
3946 | } | |
3947 | ||
3948 | ||
1e270976 MKL |
3949 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
3950 | ||
3951 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, | |
3952 | union bpf_attr __user *uattr) | |
3953 | { | |
3954 | int ufd = attr->info.bpf_fd; | |
3955 | struct fd f; | |
3956 | int err; | |
3957 | ||
3958 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) | |
3959 | return -EINVAL; | |
3960 | ||
3961 | f = fdget(ufd); | |
3962 | if (!f.file) | |
3963 | return -EBADFD; | |
3964 | ||
3965 | if (f.file->f_op == &bpf_prog_fops) | |
63960260 | 3966 | err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr, |
1e270976 MKL |
3967 | uattr); |
3968 | else if (f.file->f_op == &bpf_map_fops) | |
63960260 | 3969 | err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr, |
1e270976 | 3970 | uattr); |
60197cfb | 3971 | else if (f.file->f_op == &btf_fops) |
63960260 | 3972 | err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr); |
f2e10bff | 3973 | else if (f.file->f_op == &bpf_link_fops) |
63960260 | 3974 | err = bpf_link_get_info_by_fd(f.file, f.file->private_data, |
f2e10bff | 3975 | attr, uattr); |
1e270976 MKL |
3976 | else |
3977 | err = -EINVAL; | |
3978 | ||
3979 | fdput(f); | |
3980 | return err; | |
3981 | } | |
3982 | ||
f56a653c MKL |
3983 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
3984 | ||
c571bd75 | 3985 | static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr) |
f56a653c MKL |
3986 | { |
3987 | if (CHECK_ATTR(BPF_BTF_LOAD)) | |
3988 | return -EINVAL; | |
3989 | ||
2c78ee89 | 3990 | if (!bpf_capable()) |
f56a653c MKL |
3991 | return -EPERM; |
3992 | ||
c571bd75 | 3993 | return btf_new_fd(attr, uattr); |
f56a653c MKL |
3994 | } |
3995 | ||
78958fca MKL |
3996 | #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id |
3997 | ||
3998 | static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) | |
3999 | { | |
4000 | if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) | |
4001 | return -EINVAL; | |
4002 | ||
4003 | if (!capable(CAP_SYS_ADMIN)) | |
4004 | return -EPERM; | |
4005 | ||
4006 | return btf_get_fd_by_id(attr->btf_id); | |
4007 | } | |
4008 | ||
41bdc4b4 YS |
4009 | static int bpf_task_fd_query_copy(const union bpf_attr *attr, |
4010 | union bpf_attr __user *uattr, | |
4011 | u32 prog_id, u32 fd_type, | |
4012 | const char *buf, u64 probe_offset, | |
4013 | u64 probe_addr) | |
4014 | { | |
4015 | char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); | |
4016 | u32 len = buf ? strlen(buf) : 0, input_len; | |
4017 | int err = 0; | |
4018 | ||
4019 | if (put_user(len, &uattr->task_fd_query.buf_len)) | |
4020 | return -EFAULT; | |
4021 | input_len = attr->task_fd_query.buf_len; | |
4022 | if (input_len && ubuf) { | |
4023 | if (!len) { | |
4024 | /* nothing to copy, just make ubuf NULL terminated */ | |
4025 | char zero = '\0'; | |
4026 | ||
4027 | if (put_user(zero, ubuf)) | |
4028 | return -EFAULT; | |
4029 | } else if (input_len >= len + 1) { | |
4030 | /* ubuf can hold the string with NULL terminator */ | |
4031 | if (copy_to_user(ubuf, buf, len + 1)) | |
4032 | return -EFAULT; | |
4033 | } else { | |
4034 | /* ubuf cannot hold the string with NULL terminator, | |
4035 | * do a partial copy with NULL terminator. | |
4036 | */ | |
4037 | char zero = '\0'; | |
4038 | ||
4039 | err = -ENOSPC; | |
4040 | if (copy_to_user(ubuf, buf, input_len - 1)) | |
4041 | return -EFAULT; | |
4042 | if (put_user(zero, ubuf + input_len - 1)) | |
4043 | return -EFAULT; | |
4044 | } | |
4045 | } | |
4046 | ||
4047 | if (put_user(prog_id, &uattr->task_fd_query.prog_id) || | |
4048 | put_user(fd_type, &uattr->task_fd_query.fd_type) || | |
4049 | put_user(probe_offset, &uattr->task_fd_query.probe_offset) || | |
4050 | put_user(probe_addr, &uattr->task_fd_query.probe_addr)) | |
4051 | return -EFAULT; | |
4052 | ||
4053 | return err; | |
4054 | } | |
4055 | ||
4056 | #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr | |
4057 | ||
4058 | static int bpf_task_fd_query(const union bpf_attr *attr, | |
4059 | union bpf_attr __user *uattr) | |
4060 | { | |
4061 | pid_t pid = attr->task_fd_query.pid; | |
4062 | u32 fd = attr->task_fd_query.fd; | |
4063 | const struct perf_event *event; | |
41bdc4b4 YS |
4064 | struct task_struct *task; |
4065 | struct file *file; | |
4066 | int err; | |
4067 | ||
4068 | if (CHECK_ATTR(BPF_TASK_FD_QUERY)) | |
4069 | return -EINVAL; | |
4070 | ||
4071 | if (!capable(CAP_SYS_ADMIN)) | |
4072 | return -EPERM; | |
4073 | ||
4074 | if (attr->task_fd_query.flags != 0) | |
4075 | return -EINVAL; | |
4076 | ||
4077 | task = get_pid_task(find_vpid(pid), PIDTYPE_PID); | |
4078 | if (!task) | |
4079 | return -ENOENT; | |
4080 | ||
41bdc4b4 | 4081 | err = 0; |
b48845af EB |
4082 | file = fget_task(task, fd); |
4083 | put_task_struct(task); | |
41bdc4b4 | 4084 | if (!file) |
b48845af | 4085 | return -EBADF; |
41bdc4b4 | 4086 | |
70ed506c AN |
4087 | if (file->f_op == &bpf_link_fops) { |
4088 | struct bpf_link *link = file->private_data; | |
41bdc4b4 | 4089 | |
a3b80e10 | 4090 | if (link->ops == &bpf_raw_tp_link_lops) { |
70ed506c AN |
4091 | struct bpf_raw_tp_link *raw_tp = |
4092 | container_of(link, struct bpf_raw_tp_link, link); | |
4093 | struct bpf_raw_event_map *btp = raw_tp->btp; | |
4094 | ||
4095 | err = bpf_task_fd_query_copy(attr, uattr, | |
4096 | raw_tp->link.prog->aux->id, | |
4097 | BPF_FD_TYPE_RAW_TRACEPOINT, | |
4098 | btp->tp->name, 0, 0); | |
4099 | goto put_file; | |
4100 | } | |
4101 | goto out_not_supp; | |
41bdc4b4 YS |
4102 | } |
4103 | ||
4104 | event = perf_get_event(file); | |
4105 | if (!IS_ERR(event)) { | |
4106 | u64 probe_offset, probe_addr; | |
4107 | u32 prog_id, fd_type; | |
4108 | const char *buf; | |
4109 | ||
4110 | err = bpf_get_perf_event_info(event, &prog_id, &fd_type, | |
4111 | &buf, &probe_offset, | |
4112 | &probe_addr); | |
4113 | if (!err) | |
4114 | err = bpf_task_fd_query_copy(attr, uattr, prog_id, | |
4115 | fd_type, buf, | |
4116 | probe_offset, | |
4117 | probe_addr); | |
4118 | goto put_file; | |
4119 | } | |
4120 | ||
70ed506c | 4121 | out_not_supp: |
41bdc4b4 YS |
4122 | err = -ENOTSUPP; |
4123 | put_file: | |
4124 | fput(file); | |
41bdc4b4 YS |
4125 | return err; |
4126 | } | |
4127 | ||
cb4d03ab BV |
4128 | #define BPF_MAP_BATCH_LAST_FIELD batch.flags |
4129 | ||
4130 | #define BPF_DO_BATCH(fn) \ | |
4131 | do { \ | |
4132 | if (!fn) { \ | |
4133 | err = -ENOTSUPP; \ | |
4134 | goto err_put; \ | |
4135 | } \ | |
4136 | err = fn(map, attr, uattr); \ | |
4137 | } while (0) | |
4138 | ||
4139 | static int bpf_map_do_batch(const union bpf_attr *attr, | |
4140 | union bpf_attr __user *uattr, | |
4141 | int cmd) | |
4142 | { | |
4143 | struct bpf_map *map; | |
4144 | int err, ufd; | |
4145 | struct fd f; | |
4146 | ||
4147 | if (CHECK_ATTR(BPF_MAP_BATCH)) | |
4148 | return -EINVAL; | |
4149 | ||
4150 | ufd = attr->batch.map_fd; | |
4151 | f = fdget(ufd); | |
4152 | map = __bpf_map_get(f); | |
4153 | if (IS_ERR(map)) | |
4154 | return PTR_ERR(map); | |
4155 | ||
05799638 YS |
4156 | if ((cmd == BPF_MAP_LOOKUP_BATCH || |
4157 | cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) && | |
cb4d03ab BV |
4158 | !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
4159 | err = -EPERM; | |
4160 | goto err_put; | |
4161 | } | |
4162 | ||
4163 | if (cmd != BPF_MAP_LOOKUP_BATCH && | |
4164 | !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { | |
4165 | err = -EPERM; | |
4166 | goto err_put; | |
4167 | } | |
4168 | ||
4169 | if (cmd == BPF_MAP_LOOKUP_BATCH) | |
4170 | BPF_DO_BATCH(map->ops->map_lookup_batch); | |
05799638 YS |
4171 | else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) |
4172 | BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch); | |
aa2e93b8 BV |
4173 | else if (cmd == BPF_MAP_UPDATE_BATCH) |
4174 | BPF_DO_BATCH(map->ops->map_update_batch); | |
4175 | else | |
4176 | BPF_DO_BATCH(map->ops->map_delete_batch); | |
cb4d03ab BV |
4177 | |
4178 | err_put: | |
4179 | fdput(f); | |
4180 | return err; | |
4181 | } | |
4182 | ||
af2ac3e1 AS |
4183 | static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr, |
4184 | struct bpf_prog *prog) | |
de4e05ca | 4185 | { |
4a1e7c0c THJ |
4186 | if (attr->link_create.attach_type != prog->expected_attach_type) |
4187 | return -EINVAL; | |
de4e05ca | 4188 | |
4a1e7c0c | 4189 | if (prog->expected_attach_type == BPF_TRACE_ITER) |
af2ac3e1 | 4190 | return bpf_iter_link_attach(attr, uattr, prog); |
4a1e7c0c THJ |
4191 | else if (prog->type == BPF_PROG_TYPE_EXT) |
4192 | return bpf_tracing_prog_attach(prog, | |
4193 | attr->link_create.target_fd, | |
4194 | attr->link_create.target_btf_id); | |
de4e05ca YS |
4195 | return -EINVAL; |
4196 | } | |
4197 | ||
5e7b3020 | 4198 | #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len |
af2ac3e1 | 4199 | static int link_create(union bpf_attr *attr, bpfptr_t uattr) |
af6eea57 AN |
4200 | { |
4201 | enum bpf_prog_type ptype; | |
4202 | struct bpf_prog *prog; | |
4203 | int ret; | |
4204 | ||
af6eea57 AN |
4205 | if (CHECK_ATTR(BPF_LINK_CREATE)) |
4206 | return -EINVAL; | |
4207 | ||
4a1e7c0c | 4208 | prog = bpf_prog_get(attr->link_create.prog_fd); |
af6eea57 AN |
4209 | if (IS_ERR(prog)) |
4210 | return PTR_ERR(prog); | |
4211 | ||
4212 | ret = bpf_prog_attach_check_attach_type(prog, | |
4213 | attr->link_create.attach_type); | |
4214 | if (ret) | |
4a1e7c0c THJ |
4215 | goto out; |
4216 | ||
b89fbfbb AN |
4217 | switch (prog->type) { |
4218 | case BPF_PROG_TYPE_EXT: | |
af2ac3e1 | 4219 | ret = tracing_bpf_link_attach(attr, uattr, prog); |
4a1e7c0c | 4220 | goto out; |
b89fbfbb AN |
4221 | case BPF_PROG_TYPE_PERF_EVENT: |
4222 | case BPF_PROG_TYPE_KPROBE: | |
4223 | case BPF_PROG_TYPE_TRACEPOINT: | |
4224 | if (attr->link_create.attach_type != BPF_PERF_EVENT) { | |
4225 | ret = -EINVAL; | |
4226 | goto out; | |
4227 | } | |
4228 | ptype = prog->type; | |
4229 | break; | |
4230 | default: | |
4231 | ptype = attach_type_to_prog_type(attr->link_create.attach_type); | |
4232 | if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) { | |
4233 | ret = -EINVAL; | |
4234 | goto out; | |
4235 | } | |
4236 | break; | |
4a1e7c0c | 4237 | } |
af6eea57 AN |
4238 | |
4239 | switch (ptype) { | |
4240 | case BPF_PROG_TYPE_CGROUP_SKB: | |
4241 | case BPF_PROG_TYPE_CGROUP_SOCK: | |
4242 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: | |
4243 | case BPF_PROG_TYPE_SOCK_OPS: | |
4244 | case BPF_PROG_TYPE_CGROUP_DEVICE: | |
4245 | case BPF_PROG_TYPE_CGROUP_SYSCTL: | |
4246 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: | |
4247 | ret = cgroup_bpf_link_attach(attr, prog); | |
4248 | break; | |
de4e05ca | 4249 | case BPF_PROG_TYPE_TRACING: |
af2ac3e1 | 4250 | ret = tracing_bpf_link_attach(attr, uattr, prog); |
de4e05ca | 4251 | break; |
7f045a49 | 4252 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
e9ddbb77 | 4253 | case BPF_PROG_TYPE_SK_LOOKUP: |
7f045a49 JS |
4254 | ret = netns_bpf_link_create(attr, prog); |
4255 | break; | |
310ad797 | 4256 | #ifdef CONFIG_NET |
aa8d3a71 AN |
4257 | case BPF_PROG_TYPE_XDP: |
4258 | ret = bpf_xdp_link_attach(attr, prog); | |
4259 | break; | |
b89fbfbb AN |
4260 | #endif |
4261 | #ifdef CONFIG_PERF_EVENTS | |
4262 | case BPF_PROG_TYPE_PERF_EVENT: | |
4263 | case BPF_PROG_TYPE_TRACEPOINT: | |
4264 | case BPF_PROG_TYPE_KPROBE: | |
4265 | ret = bpf_perf_link_attach(attr, prog); | |
4266 | break; | |
310ad797 | 4267 | #endif |
af6eea57 AN |
4268 | default: |
4269 | ret = -EINVAL; | |
4270 | } | |
4271 | ||
4a1e7c0c | 4272 | out: |
af6eea57 AN |
4273 | if (ret < 0) |
4274 | bpf_prog_put(prog); | |
4275 | return ret; | |
4276 | } | |
4277 | ||
0c991ebc AN |
4278 | #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd |
4279 | ||
4280 | static int link_update(union bpf_attr *attr) | |
4281 | { | |
4282 | struct bpf_prog *old_prog = NULL, *new_prog; | |
4283 | struct bpf_link *link; | |
4284 | u32 flags; | |
4285 | int ret; | |
4286 | ||
0c991ebc AN |
4287 | if (CHECK_ATTR(BPF_LINK_UPDATE)) |
4288 | return -EINVAL; | |
4289 | ||
4290 | flags = attr->link_update.flags; | |
4291 | if (flags & ~BPF_F_REPLACE) | |
4292 | return -EINVAL; | |
4293 | ||
4294 | link = bpf_link_get_from_fd(attr->link_update.link_fd); | |
4295 | if (IS_ERR(link)) | |
4296 | return PTR_ERR(link); | |
4297 | ||
4298 | new_prog = bpf_prog_get(attr->link_update.new_prog_fd); | |
4adb7a4a AN |
4299 | if (IS_ERR(new_prog)) { |
4300 | ret = PTR_ERR(new_prog); | |
4301 | goto out_put_link; | |
4302 | } | |
0c991ebc AN |
4303 | |
4304 | if (flags & BPF_F_REPLACE) { | |
4305 | old_prog = bpf_prog_get(attr->link_update.old_prog_fd); | |
4306 | if (IS_ERR(old_prog)) { | |
4307 | ret = PTR_ERR(old_prog); | |
4308 | old_prog = NULL; | |
4309 | goto out_put_progs; | |
4310 | } | |
4adb7a4a AN |
4311 | } else if (attr->link_update.old_prog_fd) { |
4312 | ret = -EINVAL; | |
4313 | goto out_put_progs; | |
0c991ebc AN |
4314 | } |
4315 | ||
f9d04127 AN |
4316 | if (link->ops->update_prog) |
4317 | ret = link->ops->update_prog(link, new_prog, old_prog); | |
4318 | else | |
fe537393 | 4319 | ret = -EINVAL; |
0c991ebc AN |
4320 | |
4321 | out_put_progs: | |
4322 | if (old_prog) | |
4323 | bpf_prog_put(old_prog); | |
4324 | if (ret) | |
4325 | bpf_prog_put(new_prog); | |
4adb7a4a AN |
4326 | out_put_link: |
4327 | bpf_link_put(link); | |
0c991ebc AN |
4328 | return ret; |
4329 | } | |
4330 | ||
73b11c2a AN |
4331 | #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd |
4332 | ||
4333 | static int link_detach(union bpf_attr *attr) | |
4334 | { | |
4335 | struct bpf_link *link; | |
4336 | int ret; | |
4337 | ||
4338 | if (CHECK_ATTR(BPF_LINK_DETACH)) | |
4339 | return -EINVAL; | |
4340 | ||
4341 | link = bpf_link_get_from_fd(attr->link_detach.link_fd); | |
4342 | if (IS_ERR(link)) | |
4343 | return PTR_ERR(link); | |
4344 | ||
4345 | if (link->ops->detach) | |
4346 | ret = link->ops->detach(link); | |
4347 | else | |
4348 | ret = -EOPNOTSUPP; | |
4349 | ||
4350 | bpf_link_put(link); | |
4351 | return ret; | |
4352 | } | |
4353 | ||
005142b8 | 4354 | static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) |
2d602c8c | 4355 | { |
005142b8 | 4356 | return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT); |
2d602c8c AN |
4357 | } |
4358 | ||
005142b8 | 4359 | struct bpf_link *bpf_link_by_id(u32 id) |
2d602c8c AN |
4360 | { |
4361 | struct bpf_link *link; | |
2d602c8c | 4362 | |
005142b8 AS |
4363 | if (!id) |
4364 | return ERR_PTR(-ENOENT); | |
2d602c8c AN |
4365 | |
4366 | spin_lock_bh(&link_idr_lock); | |
2d602c8c | 4367 | /* before link is "settled", ID is 0, pretend it doesn't exist yet */ |
005142b8 | 4368 | link = idr_find(&link_idr, id); |
2d602c8c AN |
4369 | if (link) { |
4370 | if (link->id) | |
005142b8 | 4371 | link = bpf_link_inc_not_zero(link); |
2d602c8c | 4372 | else |
005142b8 | 4373 | link = ERR_PTR(-EAGAIN); |
2d602c8c | 4374 | } else { |
005142b8 | 4375 | link = ERR_PTR(-ENOENT); |
2d602c8c AN |
4376 | } |
4377 | spin_unlock_bh(&link_idr_lock); | |
005142b8 AS |
4378 | return link; |
4379 | } | |
2d602c8c | 4380 | |
005142b8 AS |
4381 | #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id |
4382 | ||
4383 | static int bpf_link_get_fd_by_id(const union bpf_attr *attr) | |
4384 | { | |
4385 | struct bpf_link *link; | |
4386 | u32 id = attr->link_id; | |
4387 | int fd; | |
4388 | ||
4389 | if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) | |
4390 | return -EINVAL; | |
4391 | ||
4392 | if (!capable(CAP_SYS_ADMIN)) | |
4393 | return -EPERM; | |
4394 | ||
4395 | link = bpf_link_by_id(id); | |
4396 | if (IS_ERR(link)) | |
4397 | return PTR_ERR(link); | |
2d602c8c AN |
4398 | |
4399 | fd = bpf_link_new_fd(link); | |
4400 | if (fd < 0) | |
4401 | bpf_link_put(link); | |
4402 | ||
4403 | return fd; | |
4404 | } | |
4405 | ||
d46edd67 SL |
4406 | DEFINE_MUTEX(bpf_stats_enabled_mutex); |
4407 | ||
4408 | static int bpf_stats_release(struct inode *inode, struct file *file) | |
4409 | { | |
4410 | mutex_lock(&bpf_stats_enabled_mutex); | |
4411 | static_key_slow_dec(&bpf_stats_enabled_key.key); | |
4412 | mutex_unlock(&bpf_stats_enabled_mutex); | |
4413 | return 0; | |
4414 | } | |
4415 | ||
4416 | static const struct file_operations bpf_stats_fops = { | |
4417 | .release = bpf_stats_release, | |
4418 | }; | |
4419 | ||
4420 | static int bpf_enable_runtime_stats(void) | |
4421 | { | |
4422 | int fd; | |
4423 | ||
4424 | mutex_lock(&bpf_stats_enabled_mutex); | |
4425 | ||
4426 | /* Set a very high limit to avoid overflow */ | |
4427 | if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) { | |
4428 | mutex_unlock(&bpf_stats_enabled_mutex); | |
4429 | return -EBUSY; | |
4430 | } | |
4431 | ||
4432 | fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC); | |
4433 | if (fd >= 0) | |
4434 | static_key_slow_inc(&bpf_stats_enabled_key.key); | |
4435 | ||
4436 | mutex_unlock(&bpf_stats_enabled_mutex); | |
4437 | return fd; | |
4438 | } | |
4439 | ||
4440 | #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type | |
4441 | ||
4442 | static int bpf_enable_stats(union bpf_attr *attr) | |
4443 | { | |
4444 | ||
4445 | if (CHECK_ATTR(BPF_ENABLE_STATS)) | |
4446 | return -EINVAL; | |
4447 | ||
4448 | if (!capable(CAP_SYS_ADMIN)) | |
4449 | return -EPERM; | |
4450 | ||
4451 | switch (attr->enable_stats.type) { | |
4452 | case BPF_STATS_RUN_TIME: | |
4453 | return bpf_enable_runtime_stats(); | |
4454 | default: | |
4455 | break; | |
4456 | } | |
4457 | return -EINVAL; | |
4458 | } | |
4459 | ||
ac51d99b YS |
4460 | #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags |
4461 | ||
4462 | static int bpf_iter_create(union bpf_attr *attr) | |
4463 | { | |
4464 | struct bpf_link *link; | |
4465 | int err; | |
4466 | ||
4467 | if (CHECK_ATTR(BPF_ITER_CREATE)) | |
4468 | return -EINVAL; | |
4469 | ||
4470 | if (attr->iter_create.flags) | |
4471 | return -EINVAL; | |
4472 | ||
4473 | link = bpf_link_get_from_fd(attr->iter_create.link_fd); | |
4474 | if (IS_ERR(link)) | |
4475 | return PTR_ERR(link); | |
4476 | ||
4477 | err = bpf_iter_new_fd(link); | |
4478 | bpf_link_put(link); | |
4479 | ||
4480 | return err; | |
4481 | } | |
4482 | ||
ef15314a YZ |
4483 | #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags |
4484 | ||
4485 | static int bpf_prog_bind_map(union bpf_attr *attr) | |
4486 | { | |
4487 | struct bpf_prog *prog; | |
4488 | struct bpf_map *map; | |
4489 | struct bpf_map **used_maps_old, **used_maps_new; | |
4490 | int i, ret = 0; | |
4491 | ||
4492 | if (CHECK_ATTR(BPF_PROG_BIND_MAP)) | |
4493 | return -EINVAL; | |
4494 | ||
4495 | if (attr->prog_bind_map.flags) | |
4496 | return -EINVAL; | |
4497 | ||
4498 | prog = bpf_prog_get(attr->prog_bind_map.prog_fd); | |
4499 | if (IS_ERR(prog)) | |
4500 | return PTR_ERR(prog); | |
4501 | ||
4502 | map = bpf_map_get(attr->prog_bind_map.map_fd); | |
4503 | if (IS_ERR(map)) { | |
4504 | ret = PTR_ERR(map); | |
4505 | goto out_prog_put; | |
4506 | } | |
4507 | ||
4508 | mutex_lock(&prog->aux->used_maps_mutex); | |
4509 | ||
4510 | used_maps_old = prog->aux->used_maps; | |
4511 | ||
4512 | for (i = 0; i < prog->aux->used_map_cnt; i++) | |
1028ae40 SF |
4513 | if (used_maps_old[i] == map) { |
4514 | bpf_map_put(map); | |
ef15314a | 4515 | goto out_unlock; |
1028ae40 | 4516 | } |
ef15314a YZ |
4517 | |
4518 | used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1, | |
4519 | sizeof(used_maps_new[0]), | |
4520 | GFP_KERNEL); | |
4521 | if (!used_maps_new) { | |
4522 | ret = -ENOMEM; | |
4523 | goto out_unlock; | |
4524 | } | |
4525 | ||
4526 | memcpy(used_maps_new, used_maps_old, | |
4527 | sizeof(used_maps_old[0]) * prog->aux->used_map_cnt); | |
4528 | used_maps_new[prog->aux->used_map_cnt] = map; | |
4529 | ||
4530 | prog->aux->used_map_cnt++; | |
4531 | prog->aux->used_maps = used_maps_new; | |
4532 | ||
4533 | kfree(used_maps_old); | |
4534 | ||
4535 | out_unlock: | |
4536 | mutex_unlock(&prog->aux->used_maps_mutex); | |
4537 | ||
4538 | if (ret) | |
4539 | bpf_map_put(map); | |
4540 | out_prog_put: | |
4541 | bpf_prog_put(prog); | |
4542 | return ret; | |
4543 | } | |
4544 | ||
af2ac3e1 | 4545 | static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size) |
99c55f7d | 4546 | { |
8096f229 | 4547 | union bpf_attr attr; |
99c55f7d AS |
4548 | int err; |
4549 | ||
2c78ee89 | 4550 | if (sysctl_unprivileged_bpf_disabled && !bpf_capable()) |
99c55f7d AS |
4551 | return -EPERM; |
4552 | ||
dcab51f1 | 4553 | err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); |
1e270976 MKL |
4554 | if (err) |
4555 | return err; | |
4556 | size = min_t(u32, size, sizeof(attr)); | |
99c55f7d AS |
4557 | |
4558 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ | |
8096f229 | 4559 | memset(&attr, 0, sizeof(attr)); |
af2ac3e1 | 4560 | if (copy_from_bpfptr(&attr, uattr, size) != 0) |
99c55f7d AS |
4561 | return -EFAULT; |
4562 | ||
afdb09c7 CF |
4563 | err = security_bpf(cmd, &attr, size); |
4564 | if (err < 0) | |
4565 | return err; | |
4566 | ||
99c55f7d AS |
4567 | switch (cmd) { |
4568 | case BPF_MAP_CREATE: | |
4569 | err = map_create(&attr); | |
4570 | break; | |
db20fd2b AS |
4571 | case BPF_MAP_LOOKUP_ELEM: |
4572 | err = map_lookup_elem(&attr); | |
4573 | break; | |
4574 | case BPF_MAP_UPDATE_ELEM: | |
af2ac3e1 | 4575 | err = map_update_elem(&attr, uattr); |
db20fd2b AS |
4576 | break; |
4577 | case BPF_MAP_DELETE_ELEM: | |
4578 | err = map_delete_elem(&attr); | |
4579 | break; | |
4580 | case BPF_MAP_GET_NEXT_KEY: | |
4581 | err = map_get_next_key(&attr); | |
4582 | break; | |
87df15de DB |
4583 | case BPF_MAP_FREEZE: |
4584 | err = map_freeze(&attr); | |
4585 | break; | |
09756af4 | 4586 | case BPF_PROG_LOAD: |
838e9690 | 4587 | err = bpf_prog_load(&attr, uattr); |
09756af4 | 4588 | break; |
b2197755 DB |
4589 | case BPF_OBJ_PIN: |
4590 | err = bpf_obj_pin(&attr); | |
4591 | break; | |
4592 | case BPF_OBJ_GET: | |
4593 | err = bpf_obj_get(&attr); | |
4594 | break; | |
f4324551 DM |
4595 | case BPF_PROG_ATTACH: |
4596 | err = bpf_prog_attach(&attr); | |
4597 | break; | |
4598 | case BPF_PROG_DETACH: | |
4599 | err = bpf_prog_detach(&attr); | |
4600 | break; | |
468e2f64 | 4601 | case BPF_PROG_QUERY: |
af2ac3e1 | 4602 | err = bpf_prog_query(&attr, uattr.user); |
468e2f64 | 4603 | break; |
1cf1cae9 | 4604 | case BPF_PROG_TEST_RUN: |
af2ac3e1 | 4605 | err = bpf_prog_test_run(&attr, uattr.user); |
1cf1cae9 | 4606 | break; |
34ad5580 | 4607 | case BPF_PROG_GET_NEXT_ID: |
af2ac3e1 | 4608 | err = bpf_obj_get_next_id(&attr, uattr.user, |
34ad5580 MKL |
4609 | &prog_idr, &prog_idr_lock); |
4610 | break; | |
4611 | case BPF_MAP_GET_NEXT_ID: | |
af2ac3e1 | 4612 | err = bpf_obj_get_next_id(&attr, uattr.user, |
34ad5580 MKL |
4613 | &map_idr, &map_idr_lock); |
4614 | break; | |
1b9ed84e | 4615 | case BPF_BTF_GET_NEXT_ID: |
af2ac3e1 | 4616 | err = bpf_obj_get_next_id(&attr, uattr.user, |
1b9ed84e QM |
4617 | &btf_idr, &btf_idr_lock); |
4618 | break; | |
b16d9aa4 MKL |
4619 | case BPF_PROG_GET_FD_BY_ID: |
4620 | err = bpf_prog_get_fd_by_id(&attr); | |
4621 | break; | |
bd5f5f4e MKL |
4622 | case BPF_MAP_GET_FD_BY_ID: |
4623 | err = bpf_map_get_fd_by_id(&attr); | |
4624 | break; | |
1e270976 | 4625 | case BPF_OBJ_GET_INFO_BY_FD: |
af2ac3e1 | 4626 | err = bpf_obj_get_info_by_fd(&attr, uattr.user); |
1e270976 | 4627 | break; |
c4f6699d AS |
4628 | case BPF_RAW_TRACEPOINT_OPEN: |
4629 | err = bpf_raw_tracepoint_open(&attr); | |
4630 | break; | |
f56a653c | 4631 | case BPF_BTF_LOAD: |
c571bd75 | 4632 | err = bpf_btf_load(&attr, uattr); |
f56a653c | 4633 | break; |
78958fca MKL |
4634 | case BPF_BTF_GET_FD_BY_ID: |
4635 | err = bpf_btf_get_fd_by_id(&attr); | |
4636 | break; | |
41bdc4b4 | 4637 | case BPF_TASK_FD_QUERY: |
af2ac3e1 | 4638 | err = bpf_task_fd_query(&attr, uattr.user); |
41bdc4b4 | 4639 | break; |
bd513cd0 MV |
4640 | case BPF_MAP_LOOKUP_AND_DELETE_ELEM: |
4641 | err = map_lookup_and_delete_elem(&attr); | |
4642 | break; | |
cb4d03ab | 4643 | case BPF_MAP_LOOKUP_BATCH: |
af2ac3e1 | 4644 | err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH); |
cb4d03ab | 4645 | break; |
05799638 | 4646 | case BPF_MAP_LOOKUP_AND_DELETE_BATCH: |
af2ac3e1 | 4647 | err = bpf_map_do_batch(&attr, uattr.user, |
05799638 YS |
4648 | BPF_MAP_LOOKUP_AND_DELETE_BATCH); |
4649 | break; | |
aa2e93b8 | 4650 | case BPF_MAP_UPDATE_BATCH: |
af2ac3e1 | 4651 | err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH); |
aa2e93b8 BV |
4652 | break; |
4653 | case BPF_MAP_DELETE_BATCH: | |
af2ac3e1 | 4654 | err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH); |
aa2e93b8 | 4655 | break; |
af6eea57 | 4656 | case BPF_LINK_CREATE: |
af2ac3e1 | 4657 | err = link_create(&attr, uattr); |
af6eea57 | 4658 | break; |
0c991ebc AN |
4659 | case BPF_LINK_UPDATE: |
4660 | err = link_update(&attr); | |
4661 | break; | |
2d602c8c AN |
4662 | case BPF_LINK_GET_FD_BY_ID: |
4663 | err = bpf_link_get_fd_by_id(&attr); | |
4664 | break; | |
4665 | case BPF_LINK_GET_NEXT_ID: | |
af2ac3e1 | 4666 | err = bpf_obj_get_next_id(&attr, uattr.user, |
2d602c8c AN |
4667 | &link_idr, &link_idr_lock); |
4668 | break; | |
d46edd67 SL |
4669 | case BPF_ENABLE_STATS: |
4670 | err = bpf_enable_stats(&attr); | |
4671 | break; | |
ac51d99b YS |
4672 | case BPF_ITER_CREATE: |
4673 | err = bpf_iter_create(&attr); | |
4674 | break; | |
73b11c2a AN |
4675 | case BPF_LINK_DETACH: |
4676 | err = link_detach(&attr); | |
4677 | break; | |
ef15314a YZ |
4678 | case BPF_PROG_BIND_MAP: |
4679 | err = bpf_prog_bind_map(&attr); | |
4680 | break; | |
99c55f7d AS |
4681 | default: |
4682 | err = -EINVAL; | |
4683 | break; | |
4684 | } | |
4685 | ||
4686 | return err; | |
4687 | } | |
79a7f8bd | 4688 | |
af2ac3e1 AS |
4689 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
4690 | { | |
4691 | return __sys_bpf(cmd, USER_BPFPTR(uattr), size); | |
4692 | } | |
4693 | ||
79a7f8bd AS |
4694 | static bool syscall_prog_is_valid_access(int off, int size, |
4695 | enum bpf_access_type type, | |
4696 | const struct bpf_prog *prog, | |
4697 | struct bpf_insn_access_aux *info) | |
4698 | { | |
4699 | if (off < 0 || off >= U16_MAX) | |
4700 | return false; | |
4701 | if (off % size != 0) | |
4702 | return false; | |
4703 | return true; | |
4704 | } | |
4705 | ||
4706 | BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size) | |
4707 | { | |
af2ac3e1 AS |
4708 | switch (cmd) { |
4709 | case BPF_MAP_CREATE: | |
4710 | case BPF_MAP_UPDATE_ELEM: | |
4711 | case BPF_MAP_FREEZE: | |
4712 | case BPF_PROG_LOAD: | |
c571bd75 | 4713 | case BPF_BTF_LOAD: |
af2ac3e1 AS |
4714 | break; |
4715 | /* case BPF_PROG_TEST_RUN: | |
4716 | * is not part of this list to prevent recursive test_run | |
4717 | */ | |
4718 | default: | |
4719 | return -EINVAL; | |
4720 | } | |
4721 | return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size); | |
79a7f8bd AS |
4722 | } |
4723 | ||
3a2daa72 | 4724 | static const struct bpf_func_proto bpf_sys_bpf_proto = { |
79a7f8bd AS |
4725 | .func = bpf_sys_bpf, |
4726 | .gpl_only = false, | |
4727 | .ret_type = RET_INTEGER, | |
4728 | .arg1_type = ARG_ANYTHING, | |
4729 | .arg2_type = ARG_PTR_TO_MEM, | |
4730 | .arg3_type = ARG_CONST_SIZE, | |
4731 | }; | |
4732 | ||
4733 | const struct bpf_func_proto * __weak | |
4734 | tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
4735 | { | |
4736 | return bpf_base_func_proto(func_id); | |
4737 | } | |
4738 | ||
3abea089 AS |
4739 | BPF_CALL_1(bpf_sys_close, u32, fd) |
4740 | { | |
4741 | /* When bpf program calls this helper there should not be | |
4742 | * an fdget() without matching completed fdput(). | |
4743 | * This helper is allowed in the following callchain only: | |
4744 | * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close | |
4745 | */ | |
4746 | return close_fd(fd); | |
4747 | } | |
4748 | ||
3a2daa72 | 4749 | static const struct bpf_func_proto bpf_sys_close_proto = { |
3abea089 AS |
4750 | .func = bpf_sys_close, |
4751 | .gpl_only = false, | |
4752 | .ret_type = RET_INTEGER, | |
4753 | .arg1_type = ARG_ANYTHING, | |
4754 | }; | |
4755 | ||
79a7f8bd AS |
4756 | static const struct bpf_func_proto * |
4757 | syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) | |
4758 | { | |
4759 | switch (func_id) { | |
4760 | case BPF_FUNC_sys_bpf: | |
4761 | return &bpf_sys_bpf_proto; | |
3d78417b AS |
4762 | case BPF_FUNC_btf_find_by_name_kind: |
4763 | return &bpf_btf_find_by_name_kind_proto; | |
3abea089 AS |
4764 | case BPF_FUNC_sys_close: |
4765 | return &bpf_sys_close_proto; | |
79a7f8bd AS |
4766 | default: |
4767 | return tracing_prog_func_proto(func_id, prog); | |
4768 | } | |
4769 | } | |
4770 | ||
4771 | const struct bpf_verifier_ops bpf_syscall_verifier_ops = { | |
4772 | .get_func_proto = syscall_prog_func_proto, | |
4773 | .is_valid_access = syscall_prog_is_valid_access, | |
4774 | }; | |
4775 | ||
4776 | const struct bpf_prog_ops bpf_syscall_prog_ops = { | |
4777 | .test_run = bpf_prog_test_run_syscall, | |
4778 | }; |