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