blob: e83b0818b52918b690afacde2a41b97fe0bde883 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003 */
4#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01005#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +01006#include <linux/bpf_lirc.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07007#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07008#include <linux/syscalls.h>
9#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010010#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010011#include <linux/vmalloc.h>
12#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070013#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070014#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070015#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070016#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070017#include <linux/license.h>
18#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070019#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010020#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070021#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070022#include <linux/cred.h>
23#include <linux/timekeeping.h>
24#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010025#include <linux/nospec.h>
Daniel Borkmannbae141f2019-12-06 22:49:34 +010026#include <linux/audit.h>
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -070027#include <uapi/linux/btf.h>
KP Singh9e4e01d2020-03-29 01:43:52 +010028#include <linux/bpf_lsm.h>
Andrii Nakryiko457f4432020-05-29 00:54:20 -070029#include <linux/poll.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070030
Daniel Borkmannda765a22019-11-22 21:07:58 +010031#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
34#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070035#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
Daniel Borkmannda765a22019-11-22 21:07:58 +010036#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
37 IS_FD_HASH(map))
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070038
Chenbo Feng6e71b042017-10-18 13:00:22 -070039#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
40
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080041DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070042static DEFINE_IDR(prog_idr);
43static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070044static DEFINE_IDR(map_idr);
45static DEFINE_SPINLOCK(map_idr_lock);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -070046static DEFINE_IDR(link_idr);
47static DEFINE_SPINLOCK(link_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080048
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070049int sysctl_unprivileged_bpf_disabled __read_mostly;
50
Johannes Berg40077e02017-04-11 15:34:58 +020051static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080052#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020053#define BPF_MAP_TYPE(_id, _ops) \
54 [_id] = &_ops,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070055#define BPF_LINK_TYPE(_id, _name)
Johannes Berg40077e02017-04-11 15:34:58 +020056#include <linux/bpf_types.h>
57#undef BPF_PROG_TYPE
58#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070059#undef BPF_LINK_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +020060};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070061
Mickaël Salaün752ba562017-08-07 20:45:20 +020062/*
63 * If we're handed a bigger struct than we know of, ensure all the unknown bits
64 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
65 * we don't know about yet.
66 *
67 * There is a ToCToU between this function call and the following
68 * copy_from_user() call. However, this is not a concern since this function is
69 * meant to be a future-proofing of bits.
70 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070071int bpf_check_uarg_tail_zero(void __user *uaddr,
72 size_t expected_size,
73 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020074{
75 unsigned char __user *addr;
76 unsigned char __user *end;
77 unsigned char val;
78 int err;
79
Mickaël Salaün752ba562017-08-07 20:45:20 +020080 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
81 return -E2BIG;
82
Linus Torvalds96d4f262019-01-03 18:57:57 -080083 if (unlikely(!access_ok(uaddr, actual_size)))
Mickaël Salaün752ba562017-08-07 20:45:20 +020084 return -EFAULT;
85
Mickaël Salaün58291a72017-08-07 20:45:19 +020086 if (actual_size <= expected_size)
87 return 0;
88
89 addr = uaddr + expected_size;
90 end = uaddr + actual_size;
91
92 for (; addr < end; addr++) {
93 err = get_user(val, addr);
94 if (err)
95 return err;
96 if (val)
97 return -E2BIG;
98 }
99
100 return 0;
101}
102
Jakub Kicinskia3884572018-01-11 20:29:09 -0800103const struct bpf_map_ops bpf_map_offload_ops = {
104 .map_alloc = bpf_map_offload_map_alloc,
105 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200106 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800107};
108
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700109static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
110{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800111 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100112 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700113 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800114 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700115
Mark Rutland9ef09e32018-05-03 17:04:59 +0100116 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800117 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100118 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
119 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800120 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200121 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700122
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800123 if (ops->map_alloc_check) {
124 err = ops->map_alloc_check(attr);
125 if (err)
126 return ERR_PTR(err);
127 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800128 if (attr->map_ifindex)
129 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800130 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200131 if (IS_ERR(map))
132 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800133 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100134 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200135 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700136}
137
Brian Vazquez15c14a32020-01-15 10:43:00 -0800138static u32 bpf_map_value_size(struct bpf_map *map)
139{
140 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
141 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
142 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
143 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
144 return round_up(map->value_size, 8) * num_possible_cpus();
145 else if (IS_FD_MAP(map))
146 return sizeof(u32);
147 else
148 return map->value_size;
149}
150
151static void maybe_wait_bpf_programs(struct bpf_map *map)
152{
153 /* Wait for any running BPF programs to complete so that
154 * userspace, when we return to it, knows that all programs
155 * that could be running use the new map value.
156 */
157 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
158 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
159 synchronize_rcu();
160}
161
162static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
163 void *value, __u64 flags)
164{
165 int err;
166
167 /* Need to create a kthread, thus must support schedule */
168 if (bpf_map_is_dev_bound(map)) {
169 return bpf_map_offload_update_elem(map, key, value, flags);
170 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
171 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
172 map->map_type == BPF_MAP_TYPE_SOCKMAP ||
173 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
174 return map->ops->map_update_elem(map, key, value, flags);
175 } else if (IS_FD_PROG_ARRAY(map)) {
176 return bpf_fd_array_map_update_elem(map, f.file, key, value,
177 flags);
178 }
179
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100180 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800181 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
182 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
183 err = bpf_percpu_hash_update(map, key, value, flags);
184 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
185 err = bpf_percpu_array_update(map, key, value, flags);
186 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
187 err = bpf_percpu_cgroup_storage_update(map, key, value,
188 flags);
189 } else if (IS_FD_ARRAY(map)) {
190 rcu_read_lock();
191 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
192 flags);
193 rcu_read_unlock();
194 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
195 rcu_read_lock();
196 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
197 flags);
198 rcu_read_unlock();
199 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
200 /* rcu_read_lock() is not needed */
201 err = bpf_fd_reuseport_array_update_elem(map, key, value,
202 flags);
203 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
204 map->map_type == BPF_MAP_TYPE_STACK) {
205 err = map->ops->map_push_elem(map, value, flags);
206 } else {
207 rcu_read_lock();
208 err = map->ops->map_update_elem(map, key, value, flags);
209 rcu_read_unlock();
210 }
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100211 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800212 maybe_wait_bpf_programs(map);
213
214 return err;
215}
216
217static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
218 __u64 flags)
219{
220 void *ptr;
221 int err;
222
Brian Vazquezcb4d03a2020-01-15 10:43:01 -0800223 if (bpf_map_is_dev_bound(map))
224 return bpf_map_offload_lookup_elem(map, key, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800225
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100226 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800227 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
228 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
229 err = bpf_percpu_hash_copy(map, key, value);
230 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
231 err = bpf_percpu_array_copy(map, key, value);
232 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
233 err = bpf_percpu_cgroup_storage_copy(map, key, value);
234 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
235 err = bpf_stackmap_copy(map, key, value);
236 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
237 err = bpf_fd_array_map_lookup_elem(map, key, value);
238 } else if (IS_FD_HASH(map)) {
239 err = bpf_fd_htab_map_lookup_elem(map, key, value);
240 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
241 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
242 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
243 map->map_type == BPF_MAP_TYPE_STACK) {
244 err = map->ops->map_peek_elem(map, value);
245 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
246 /* struct_ops map requires directly updating "value" */
247 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
248 } else {
249 rcu_read_lock();
250 if (map->ops->map_lookup_elem_sys_only)
251 ptr = map->ops->map_lookup_elem_sys_only(map, key);
252 else
253 ptr = map->ops->map_lookup_elem(map, key);
254 if (IS_ERR(ptr)) {
255 err = PTR_ERR(ptr);
256 } else if (!ptr) {
257 err = -ENOENT;
258 } else {
259 err = 0;
260 if (flags & BPF_F_LOCK)
261 /* lock 'ptr' and copy everything but lock */
262 copy_map_value_locked(map, value, ptr, true);
263 else
264 copy_map_value(map, value, ptr);
265 /* mask lock, since value wasn't zero inited */
266 check_and_init_map_lock(map, value);
267 }
268 rcu_read_unlock();
269 }
270
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100271 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800272 maybe_wait_bpf_programs(map);
273
274 return err;
275}
276
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100277static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100278{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100279 /* We really just want to fail instead of triggering OOM killer
280 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
281 * which is used for lower order allocation requests.
282 *
283 * It has been observed that higher order allocation requests done by
284 * vmalloc with __GFP_NORETRY being set might fail due to not trying
285 * to reclaim memory from the page cache, thus we set
286 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100287 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100288
289 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100290 void *area;
291
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100292 if (size >= SIZE_MAX)
293 return NULL;
294
Andrii Nakryikofc970222019-11-17 09:28:04 -0800295 /* kmalloc()'ed memory can't be mmap()'ed */
296 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100297 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
298 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100299 if (area != NULL)
300 return area;
301 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800302 if (mmapable) {
303 BUG_ON(!PAGE_ALIGNED(size));
304 return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
305 __GFP_RETRY_MAYFAIL | flags);
306 }
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100307 return __vmalloc_node_flags_caller(size, numa_node,
308 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
309 flags, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100310}
311
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100312void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800313{
314 return __bpf_map_area_alloc(size, numa_node, false);
315}
316
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100317void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800318{
319 return __bpf_map_area_alloc(size, numa_node, true);
320}
321
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100322void bpf_map_area_free(void *area)
323{
324 kvfree(area);
325}
326
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200327static u32 bpf_map_flags_retain_permanent(u32 flags)
328{
329 /* Some map creation flags are not tied to the map object but
330 * rather to the map fd instead, so they have no meaning upon
331 * map object inspection since multiple file descriptors with
332 * different (access) properties can exist here. Thus, given
333 * this has zero meaning for the map itself, lets clear these
334 * from here.
335 */
336 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
337}
338
Jakub Kicinskibd475642018-01-11 20:29:06 -0800339void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
340{
341 map->map_type = attr->map_type;
342 map->key_size = attr->key_size;
343 map->value_size = attr->value_size;
344 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200345 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800346 map->numa_node = bpf_map_attr_numa_node(attr);
347}
348
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700349static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700350{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700351 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700352
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700353 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
354 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700355 return -EPERM;
356 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700357 return 0;
358}
359
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700360static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
361{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700362 if (user)
363 atomic_long_sub(pages, &user->locked_vm);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700364}
365
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100366int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700367{
Roman Gushchinc85d6912019-05-29 18:03:59 -0700368 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
369 struct user_struct *user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700370 int ret;
371
Roman Gushchinc85d6912019-05-29 18:03:59 -0700372 if (size >= U32_MAX - PAGE_SIZE)
373 return -E2BIG;
374
375 user = get_current_user();
Roman Gushchinb936ca62019-05-29 18:03:58 -0700376 ret = bpf_charge_memlock(user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700377 if (ret) {
378 free_uid(user);
379 return ret;
380 }
Roman Gushchinb936ca62019-05-29 18:03:58 -0700381
382 mem->pages = pages;
383 mem->user = user;
384
385 return 0;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700386}
387
Roman Gushchinb936ca62019-05-29 18:03:58 -0700388void bpf_map_charge_finish(struct bpf_map_memory *mem)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700389{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700390 bpf_uncharge_memlock(mem->user, mem->pages);
391 free_uid(mem->user);
392}
Roman Gushchin3539b962019-05-29 18:03:57 -0700393
Roman Gushchinb936ca62019-05-29 18:03:58 -0700394void bpf_map_charge_move(struct bpf_map_memory *dst,
395 struct bpf_map_memory *src)
396{
397 *dst = *src;
398
399 /* Make sure src will not be used for the redundant uncharging. */
400 memset(src, 0, sizeof(struct bpf_map_memory));
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700401}
402
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700403int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
404{
405 int ret;
406
Roman Gushchin3539b962019-05-29 18:03:57 -0700407 ret = bpf_charge_memlock(map->memory.user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700408 if (ret)
409 return ret;
Roman Gushchin3539b962019-05-29 18:03:57 -0700410 map->memory.pages += pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700411 return ret;
412}
413
414void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
415{
Roman Gushchin3539b962019-05-29 18:03:57 -0700416 bpf_uncharge_memlock(map->memory.user, pages);
417 map->memory.pages -= pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700418}
419
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700420static int bpf_map_alloc_id(struct bpf_map *map)
421{
422 int id;
423
Shaohua Lib76354c2018-03-27 11:53:21 -0700424 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700425 spin_lock_bh(&map_idr_lock);
426 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
427 if (id > 0)
428 map->id = id;
429 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700430 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700431
432 if (WARN_ON_ONCE(!id))
433 return -ENOSPC;
434
435 return id > 0 ? 0 : id;
436}
437
Jakub Kicinskia3884572018-01-11 20:29:09 -0800438void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700439{
Eric Dumazet930651a2017-09-19 09:15:59 -0700440 unsigned long flags;
441
Jakub Kicinskia3884572018-01-11 20:29:09 -0800442 /* Offloaded maps are removed from the IDR store when their device
443 * disappears - even if someone holds an fd to them they are unusable,
444 * the memory is gone, all ops will fail; they are simply waiting for
445 * refcnt to drop to be freed.
446 */
447 if (!map->id)
448 return;
449
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700450 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700451 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700452 else
453 __acquire(&map_idr_lock);
454
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700455 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800456 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700457
458 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700459 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700460 else
461 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700462}
463
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700464/* called from workqueue */
465static void bpf_map_free_deferred(struct work_struct *work)
466{
467 struct bpf_map *map = container_of(work, struct bpf_map, work);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700468 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700469
Roman Gushchinb936ca62019-05-29 18:03:58 -0700470 bpf_map_charge_move(&mem, &map->memory);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700471 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700472 /* implementation dependent freeing */
473 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700474 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700475}
476
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100477static void bpf_map_put_uref(struct bpf_map *map)
478{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800479 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700480 if (map->ops->map_release_uref)
481 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100482 }
483}
484
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700485/* decrement map refcnt and schedule it for freeing via workqueue
486 * (unrelying map implementation ops->map_free() might sleep)
487 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700488static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700489{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800490 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700491 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700492 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700493 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700494 INIT_WORK(&map->work, bpf_map_free_deferred);
495 schedule_work(&map->work);
496 }
497}
498
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700499void bpf_map_put(struct bpf_map *map)
500{
501 __bpf_map_put(map, true);
502}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700503EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700504
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100505void bpf_map_put_with_uref(struct bpf_map *map)
506{
507 bpf_map_put_uref(map);
508 bpf_map_put(map);
509}
510
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700511static int bpf_map_release(struct inode *inode, struct file *filp)
512{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200513 struct bpf_map *map = filp->private_data;
514
515 if (map->ops->map_release)
516 map->ops->map_release(map, filp);
517
518 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700519 return 0;
520}
521
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200522static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
523{
524 fmode_t mode = f.file->f_mode;
525
526 /* Our file permissions may have been overridden by global
527 * map permissions facing syscall side.
528 */
529 if (READ_ONCE(map->frozen))
530 mode &= ~FMODE_CAN_WRITE;
531 return mode;
532}
533
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100534#ifdef CONFIG_PROC_FS
535static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
536{
537 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100538 const struct bpf_array *array;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100539 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100540
541 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
542 array = container_of(map, struct bpf_array, map);
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100543 type = array->aux->type;
544 jited = array->aux->jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100545 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100546
547 seq_printf(m,
548 "map_type:\t%u\n"
549 "key_size:\t%u\n"
550 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100551 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100552 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200553 "memlock:\t%llu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200554 "map_id:\t%u\n"
555 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100556 map->map_type,
557 map->key_size,
558 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100559 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100560 map->map_flags,
Roman Gushchin3539b962019-05-29 18:03:57 -0700561 map->memory.pages * 1ULL << PAGE_SHIFT,
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200562 map->id,
563 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100564 if (type) {
565 seq_printf(m, "owner_prog_type:\t%u\n", type);
566 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200567 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100568}
569#endif
570
Chenbo Feng6e71b042017-10-18 13:00:22 -0700571static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
572 loff_t *ppos)
573{
574 /* We need this handler such that alloc_file() enables
575 * f_mode with FMODE_CAN_READ.
576 */
577 return -EINVAL;
578}
579
580static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
581 size_t siz, loff_t *ppos)
582{
583 /* We need this handler such that alloc_file() enables
584 * f_mode with FMODE_CAN_WRITE.
585 */
586 return -EINVAL;
587}
588
Andrii Nakryikofc970222019-11-17 09:28:04 -0800589/* called for any extra memory-mapped regions (except initial) */
590static void bpf_map_mmap_open(struct vm_area_struct *vma)
591{
592 struct bpf_map *map = vma->vm_file->private_data;
593
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700594 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800595 mutex_lock(&map->freeze_mutex);
596 map->writecnt++;
597 mutex_unlock(&map->freeze_mutex);
598 }
599}
600
601/* called for all unmapped memory region (including initial) */
602static void bpf_map_mmap_close(struct vm_area_struct *vma)
603{
604 struct bpf_map *map = vma->vm_file->private_data;
605
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700606 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800607 mutex_lock(&map->freeze_mutex);
608 map->writecnt--;
609 mutex_unlock(&map->freeze_mutex);
610 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800611}
612
613static const struct vm_operations_struct bpf_map_default_vmops = {
614 .open = bpf_map_mmap_open,
615 .close = bpf_map_mmap_close,
616};
617
618static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
619{
620 struct bpf_map *map = filp->private_data;
621 int err;
622
623 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
624 return -ENOTSUPP;
625
626 if (!(vma->vm_flags & VM_SHARED))
627 return -EINVAL;
628
629 mutex_lock(&map->freeze_mutex);
630
Andrii Nakryikodfeb3762020-05-18 22:38:24 -0700631 if (vma->vm_flags & VM_WRITE) {
632 if (map->frozen) {
633 err = -EPERM;
634 goto out;
635 }
636 /* map is meant to be read-only, so do not allow mapping as
637 * writable, because it's possible to leak a writable page
638 * reference and allows user-space to still modify it after
639 * freezing, while verifier will assume contents do not change
640 */
641 if (map->map_flags & BPF_F_RDONLY_PROG) {
642 err = -EACCES;
643 goto out;
644 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800645 }
646
647 /* set default open/close callbacks */
648 vma->vm_ops = &bpf_map_default_vmops;
649 vma->vm_private_data = map;
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700650 vma->vm_flags &= ~VM_MAYEXEC;
651 if (!(vma->vm_flags & VM_WRITE))
652 /* disallow re-mapping with PROT_WRITE */
653 vma->vm_flags &= ~VM_MAYWRITE;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800654
655 err = map->ops->map_mmap(map, vma);
656 if (err)
657 goto out;
658
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700659 if (vma->vm_flags & VM_MAYWRITE)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800660 map->writecnt++;
661out:
662 mutex_unlock(&map->freeze_mutex);
663 return err;
664}
665
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700666static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
667{
668 struct bpf_map *map = filp->private_data;
669
670 if (map->ops->map_poll)
671 return map->ops->map_poll(map, filp, pts);
672
673 return EPOLLERR;
674}
675
Chenbo Fengf66e4482017-10-18 13:00:26 -0700676const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100677#ifdef CONFIG_PROC_FS
678 .show_fdinfo = bpf_map_show_fdinfo,
679#endif
680 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700681 .read = bpf_dummy_read,
682 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800683 .mmap = bpf_map_mmap,
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700684 .poll = bpf_map_poll,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700685};
686
Chenbo Feng6e71b042017-10-18 13:00:22 -0700687int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100688{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700689 int ret;
690
691 ret = security_bpf_map(map, OPEN_FMODE(flags));
692 if (ret < 0)
693 return ret;
694
Daniel Borkmannaa797812015-10-29 14:58:06 +0100695 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700696 flags | O_CLOEXEC);
697}
698
699int bpf_get_file_flag(int flags)
700{
701 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
702 return -EINVAL;
703 if (flags & BPF_F_RDONLY)
704 return O_RDONLY;
705 if (flags & BPF_F_WRONLY)
706 return O_WRONLY;
707 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100708}
709
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700710/* helper macro to check that unused fields 'union bpf_attr' are zero */
711#define CHECK_ATTR(CMD) \
712 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
713 sizeof(attr->CMD##_LAST_FIELD), 0, \
714 sizeof(*attr) - \
715 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
716 sizeof(attr->CMD##_LAST_FIELD)) != NULL
717
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700718/* dst and src must have at least "size" number of bytes.
719 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700720 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700721int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700722{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700723 const char *end = src + size;
724 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700725
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700726 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200727 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700728 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200729 if (!isalnum(*src) &&
730 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700731 return -EINVAL;
732 *dst++ = *src++;
733 }
734
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700735 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700736 if (src == end)
737 return -EINVAL;
738
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700739 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700740}
741
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200742int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800743 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200744 const struct btf_type *key_type,
745 const struct btf_type *value_type)
746{
747 return -ENOTSUPP;
748}
749
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800750static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200751 u32 btf_key_id, u32 btf_value_id)
752{
753 const struct btf_type *key_type, *value_type;
754 u32 key_size, value_size;
755 int ret = 0;
756
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200757 /* Some maps allow key to be unspecified. */
758 if (btf_key_id) {
759 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
760 if (!key_type || key_size != map->key_size)
761 return -EINVAL;
762 } else {
763 key_type = btf_type_by_id(btf, 0);
764 if (!map->ops->map_check_btf)
765 return -EINVAL;
766 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200767
768 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
769 if (!value_type || value_size != map->value_size)
770 return -EINVAL;
771
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800772 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
773
774 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200775 if (map->map_flags & BPF_F_RDONLY_PROG)
776 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800777 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800778 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700779 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
780 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800781 return -ENOTSUPP;
782 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
783 map->value_size) {
784 WARN_ONCE(1,
785 "verifier bug spin_lock_off %d value_size %d\n",
786 map->spin_lock_off, map->value_size);
787 return -EFAULT;
788 }
789 }
790
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200791 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800792 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200793
794 return ret;
795}
796
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800797#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700798/* called via syscall */
799static int map_create(union bpf_attr *attr)
800{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700801 int numa_node = bpf_map_attr_numa_node(attr);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700802 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700803 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700804 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700805 int err;
806
807 err = CHECK_ATTR(BPF_MAP_CREATE);
808 if (err)
809 return -EINVAL;
810
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800811 if (attr->btf_vmlinux_value_type_id) {
812 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
813 attr->btf_key_type_id || attr->btf_value_type_id)
814 return -EINVAL;
815 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
816 return -EINVAL;
817 }
818
Chenbo Feng6e71b042017-10-18 13:00:22 -0700819 f_flags = bpf_get_file_flag(attr->map_flags);
820 if (f_flags < 0)
821 return f_flags;
822
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700823 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700824 ((unsigned int)numa_node >= nr_node_ids ||
825 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700826 return -EINVAL;
827
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700828 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
829 map = find_and_alloc_map(attr);
830 if (IS_ERR(map))
831 return PTR_ERR(map);
832
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700833 err = bpf_obj_name_cpy(map->name, attr->map_name,
834 sizeof(attr->map_name));
835 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700836 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700837
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800838 atomic64_set(&map->refcnt, 1);
839 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800840 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700841
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800842 map->spin_lock_off = -EINVAL;
843 if (attr->btf_key_type_id || attr->btf_value_type_id ||
844 /* Even the map's value is a kernel's struct,
845 * the bpf_prog.o must have BTF to begin with
846 * to figure out the corresponding kernel's
847 * counter part. Thus, attr->btf_fd has
848 * to be valid also.
849 */
850 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700851 struct btf *btf;
852
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700853 btf = btf_get_by_fd(attr->btf_fd);
854 if (IS_ERR(btf)) {
855 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700856 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700857 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800858 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700859
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800860 if (attr->btf_value_type_id) {
861 err = map_check_btf(map, btf, attr->btf_key_type_id,
862 attr->btf_value_type_id);
863 if (err)
864 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700865 }
866
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700867 map->btf_key_type_id = attr->btf_key_type_id;
868 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800869 map->btf_vmlinux_value_type_id =
870 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700871 }
872
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700873 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700874 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700875 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700876
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700877 err = bpf_map_alloc_id(map);
878 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700879 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700880
Chenbo Feng6e71b042017-10-18 13:00:22 -0700881 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700882 if (err < 0) {
883 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800884 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700885 * bpf_map_alloc_id() has published the map
886 * to the userspace and the userspace may
887 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
888 */
Peng Sun352d20d2019-02-27 22:36:25 +0800889 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700890 return err;
891 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700892
893 return err;
894
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700895free_map_sec:
896 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700897free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700898 btf_put(map->btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700899 bpf_map_charge_move(&mem, &map->memory);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700900 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700901 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700902 return err;
903}
904
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700905/* if error is returned, fd is released.
906 * On success caller should complete fd access with matching fdput()
907 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100908struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700909{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700910 if (!f.file)
911 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700912 if (f.file->f_op != &bpf_map_fops) {
913 fdput(f);
914 return ERR_PTR(-EINVAL);
915 }
916
Daniel Borkmannc2101292015-10-29 14:58:07 +0100917 return f.file->private_data;
918}
919
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800920void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100921{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800922 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100923}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700924EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100925
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800926void bpf_map_inc_with_uref(struct bpf_map *map)
927{
928 atomic64_inc(&map->refcnt);
929 atomic64_inc(&map->usercnt);
930}
931EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
932
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -0800933struct bpf_map *bpf_map_get(u32 ufd)
934{
935 struct fd f = fdget(ufd);
936 struct bpf_map *map;
937
938 map = __bpf_map_get(f);
939 if (IS_ERR(map))
940 return map;
941
942 bpf_map_inc(map);
943 fdput(f);
944
945 return map;
946}
947
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100948struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100949{
950 struct fd f = fdget(ufd);
951 struct bpf_map *map;
952
953 map = __bpf_map_get(f);
954 if (IS_ERR(map))
955 return map;
956
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800957 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100958 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700959
960 return map;
961}
962
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700963/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800964static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700965{
966 int refold;
967
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800968 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700969 if (!refold)
970 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700971 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800972 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700973
974 return map;
975}
976
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800977struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700978{
979 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800980 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700981 spin_unlock_bh(&map_idr_lock);
982
983 return map;
984}
985EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
986
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800987int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
988{
989 return -ENOTSUPP;
990}
991
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200992static void *__bpf_copy_key(void __user *ukey, u64 key_size)
993{
994 if (key_size)
995 return memdup_user(ukey, key_size);
996
997 if (ukey)
998 return ERR_PTR(-EINVAL);
999
1000 return NULL;
1001}
1002
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001003/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001004#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001005
1006static int map_lookup_elem(union bpf_attr *attr)
1007{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001008 void __user *ukey = u64_to_user_ptr(attr->key);
1009 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001010 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001011 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -08001012 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001013 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001014 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001015 int err;
1016
1017 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1018 return -EINVAL;
1019
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001020 if (attr->flags & ~BPF_F_LOCK)
1021 return -EINVAL;
1022
Daniel Borkmann592867b2015-09-08 18:00:09 +02001023 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001024 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001025 if (IS_ERR(map))
1026 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001027 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001028 err = -EPERM;
1029 goto err_put;
1030 }
1031
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001032 if ((attr->flags & BPF_F_LOCK) &&
1033 !map_value_has_spin_lock(map)) {
1034 err = -EINVAL;
1035 goto err_put;
1036 }
1037
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001038 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001039 if (IS_ERR(key)) {
1040 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001041 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001042 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001043
Brian Vazquez15c14a32020-01-15 10:43:00 -08001044 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001045
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001046 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001047 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001048 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001049 goto free_key;
1050
Brian Vazquez15c14a32020-01-15 10:43:00 -08001051 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001052 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001053 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001054
1055 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001056 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001057 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001058
1059 err = 0;
1060
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001061free_value:
1062 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001063free_key:
1064 kfree(key);
1065err_put:
1066 fdput(f);
1067 return err;
1068}
1069
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001070
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001071#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001072
1073static int map_update_elem(union bpf_attr *attr)
1074{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001075 void __user *ukey = u64_to_user_ptr(attr->key);
1076 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001077 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001078 struct bpf_map *map;
1079 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001080 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001081 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001082 int err;
1083
1084 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1085 return -EINVAL;
1086
Daniel Borkmann592867b2015-09-08 18:00:09 +02001087 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001088 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001089 if (IS_ERR(map))
1090 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001091 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001092 err = -EPERM;
1093 goto err_put;
1094 }
1095
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001096 if ((attr->flags & BPF_F_LOCK) &&
1097 !map_value_has_spin_lock(map)) {
1098 err = -EINVAL;
1099 goto err_put;
1100 }
1101
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001102 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001103 if (IS_ERR(key)) {
1104 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001105 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001106 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001107
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001108 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001109 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +00001110 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1111 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001112 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1113 else
1114 value_size = map->value_size;
1115
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001116 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001117 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001118 if (!value)
1119 goto free_key;
1120
1121 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001122 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001123 goto free_value;
1124
Brian Vazquez15c14a32020-01-15 10:43:00 -08001125 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001126
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001127free_value:
1128 kfree(value);
1129free_key:
1130 kfree(key);
1131err_put:
1132 fdput(f);
1133 return err;
1134}
1135
1136#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1137
1138static int map_delete_elem(union bpf_attr *attr)
1139{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001140 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001141 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001142 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001143 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001144 void *key;
1145 int err;
1146
1147 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1148 return -EINVAL;
1149
Daniel Borkmann592867b2015-09-08 18:00:09 +02001150 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001151 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001152 if (IS_ERR(map))
1153 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001154 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001155 err = -EPERM;
1156 goto err_put;
1157 }
1158
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001159 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001160 if (IS_ERR(key)) {
1161 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001162 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001163 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001164
Jakub Kicinskia3884572018-01-11 20:29:09 -08001165 if (bpf_map_is_dev_bound(map)) {
1166 err = bpf_map_offload_delete_elem(map, key);
1167 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001168 } else if (IS_FD_PROG_ARRAY(map) ||
1169 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1170 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001171 err = map->ops->map_delete_elem(map, key);
1172 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001173 }
1174
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001175 bpf_disable_instrumentation();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001176 rcu_read_lock();
1177 err = map->ops->map_delete_elem(map, key);
1178 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001179 bpf_enable_instrumentation();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001180 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001181out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001182 kfree(key);
1183err_put:
1184 fdput(f);
1185 return err;
1186}
1187
1188/* last field in 'union bpf_attr' used by this command */
1189#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1190
1191static int map_get_next_key(union bpf_attr *attr)
1192{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001193 void __user *ukey = u64_to_user_ptr(attr->key);
1194 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001195 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001196 struct bpf_map *map;
1197 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001198 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001199 int err;
1200
1201 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1202 return -EINVAL;
1203
Daniel Borkmann592867b2015-09-08 18:00:09 +02001204 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001205 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001206 if (IS_ERR(map))
1207 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001208 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001209 err = -EPERM;
1210 goto err_put;
1211 }
1212
Teng Qin8fe45922017-04-24 19:00:37 -07001213 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001214 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001215 if (IS_ERR(key)) {
1216 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001217 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001218 }
Teng Qin8fe45922017-04-24 19:00:37 -07001219 } else {
1220 key = NULL;
1221 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001222
1223 err = -ENOMEM;
1224 next_key = kmalloc(map->key_size, GFP_USER);
1225 if (!next_key)
1226 goto free_key;
1227
Jakub Kicinskia3884572018-01-11 20:29:09 -08001228 if (bpf_map_is_dev_bound(map)) {
1229 err = bpf_map_offload_get_next_key(map, key, next_key);
1230 goto out;
1231 }
1232
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001233 rcu_read_lock();
1234 err = map->ops->map_get_next_key(map, key, next_key);
1235 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001236out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001237 if (err)
1238 goto free_next_key;
1239
1240 err = -EFAULT;
1241 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1242 goto free_next_key;
1243
1244 err = 0;
1245
1246free_next_key:
1247 kfree(next_key);
1248free_key:
1249 kfree(key);
1250err_put:
1251 fdput(f);
1252 return err;
1253}
1254
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001255int generic_map_delete_batch(struct bpf_map *map,
1256 const union bpf_attr *attr,
1257 union bpf_attr __user *uattr)
1258{
1259 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1260 u32 cp, max_count;
1261 int err = 0;
1262 void *key;
1263
1264 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1265 return -EINVAL;
1266
1267 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1268 !map_value_has_spin_lock(map)) {
1269 return -EINVAL;
1270 }
1271
1272 max_count = attr->batch.count;
1273 if (!max_count)
1274 return 0;
1275
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001276 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1277 if (!key)
1278 return -ENOMEM;
1279
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001280 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001281 err = -EFAULT;
1282 if (copy_from_user(key, keys + cp * map->key_size,
1283 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001284 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001285
1286 if (bpf_map_is_dev_bound(map)) {
1287 err = bpf_map_offload_delete_elem(map, key);
1288 break;
1289 }
1290
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001291 bpf_disable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001292 rcu_read_lock();
1293 err = map->ops->map_delete_elem(map, key);
1294 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001295 bpf_enable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001296 maybe_wait_bpf_programs(map);
1297 if (err)
1298 break;
1299 }
1300 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1301 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001302
1303 kfree(key);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001304 return err;
1305}
1306
1307int generic_map_update_batch(struct bpf_map *map,
1308 const union bpf_attr *attr,
1309 union bpf_attr __user *uattr)
1310{
1311 void __user *values = u64_to_user_ptr(attr->batch.values);
1312 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1313 u32 value_size, cp, max_count;
1314 int ufd = attr->map_fd;
1315 void *key, *value;
1316 struct fd f;
1317 int err = 0;
1318
1319 f = fdget(ufd);
1320 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1321 return -EINVAL;
1322
1323 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1324 !map_value_has_spin_lock(map)) {
1325 return -EINVAL;
1326 }
1327
1328 value_size = bpf_map_value_size(map);
1329
1330 max_count = attr->batch.count;
1331 if (!max_count)
1332 return 0;
1333
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001334 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1335 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001336 return -ENOMEM;
1337
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001338 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1339 if (!value) {
1340 kfree(key);
1341 return -ENOMEM;
1342 }
1343
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001344 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001345 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001346 if (copy_from_user(key, keys + cp * map->key_size,
1347 map->key_size) ||
1348 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001349 break;
1350
1351 err = bpf_map_update_value(map, f, key, value,
1352 attr->batch.elem_flags);
1353
1354 if (err)
1355 break;
1356 }
1357
1358 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1359 err = -EFAULT;
1360
1361 kfree(value);
1362 kfree(key);
1363 return err;
1364}
1365
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001366#define MAP_LOOKUP_RETRIES 3
1367
1368int generic_map_lookup_batch(struct bpf_map *map,
1369 const union bpf_attr *attr,
1370 union bpf_attr __user *uattr)
1371{
1372 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1373 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1374 void __user *values = u64_to_user_ptr(attr->batch.values);
1375 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1376 void *buf, *buf_prevkey, *prev_key, *key, *value;
1377 int err, retry = MAP_LOOKUP_RETRIES;
1378 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001379
1380 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1381 return -EINVAL;
1382
1383 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1384 !map_value_has_spin_lock(map))
1385 return -EINVAL;
1386
1387 value_size = bpf_map_value_size(map);
1388
1389 max_count = attr->batch.count;
1390 if (!max_count)
1391 return 0;
1392
1393 if (put_user(0, &uattr->batch.count))
1394 return -EFAULT;
1395
1396 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1397 if (!buf_prevkey)
1398 return -ENOMEM;
1399
1400 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1401 if (!buf) {
Denis Efremovbb2359f2020-06-01 19:28:14 +03001402 kfree(buf_prevkey);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001403 return -ENOMEM;
1404 }
1405
1406 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001407 prev_key = NULL;
1408 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1409 goto free_buf;
1410 key = buf;
1411 value = key + map->key_size;
1412 if (ubatch)
1413 prev_key = buf_prevkey;
1414
1415 for (cp = 0; cp < max_count;) {
1416 rcu_read_lock();
1417 err = map->ops->map_get_next_key(map, prev_key, key);
1418 rcu_read_unlock();
1419 if (err)
1420 break;
1421 err = bpf_map_copy_value(map, key, value,
1422 attr->batch.elem_flags);
1423
1424 if (err == -ENOENT) {
1425 if (retry) {
1426 retry--;
1427 continue;
1428 }
1429 err = -EINTR;
1430 break;
1431 }
1432
1433 if (err)
1434 goto free_buf;
1435
1436 if (copy_to_user(keys + cp * map->key_size, key,
1437 map->key_size)) {
1438 err = -EFAULT;
1439 goto free_buf;
1440 }
1441 if (copy_to_user(values + cp * value_size, value, value_size)) {
1442 err = -EFAULT;
1443 goto free_buf;
1444 }
1445
1446 if (!prev_key)
1447 prev_key = buf_prevkey;
1448
1449 swap(prev_key, key);
1450 retry = MAP_LOOKUP_RETRIES;
1451 cp++;
1452 }
1453
1454 if (err == -EFAULT)
1455 goto free_buf;
1456
1457 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1458 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1459 err = -EFAULT;
1460
1461free_buf:
1462 kfree(buf_prevkey);
1463 kfree(buf);
1464 return err;
1465}
1466
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001467#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1468
1469static int map_lookup_and_delete_elem(union bpf_attr *attr)
1470{
1471 void __user *ukey = u64_to_user_ptr(attr->key);
1472 void __user *uvalue = u64_to_user_ptr(attr->value);
1473 int ufd = attr->map_fd;
1474 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001475 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001476 u32 value_size;
1477 struct fd f;
1478 int err;
1479
1480 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1481 return -EINVAL;
1482
1483 f = fdget(ufd);
1484 map = __bpf_map_get(f);
1485 if (IS_ERR(map))
1486 return PTR_ERR(map);
Anton Protopopov1ea0f912020-05-27 18:56:59 +00001487 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1488 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001489 err = -EPERM;
1490 goto err_put;
1491 }
1492
1493 key = __bpf_copy_key(ukey, map->key_size);
1494 if (IS_ERR(key)) {
1495 err = PTR_ERR(key);
1496 goto err_put;
1497 }
1498
1499 value_size = map->value_size;
1500
1501 err = -ENOMEM;
1502 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1503 if (!value)
1504 goto free_key;
1505
1506 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1507 map->map_type == BPF_MAP_TYPE_STACK) {
1508 err = map->ops->map_pop_elem(map, value);
1509 } else {
1510 err = -ENOTSUPP;
1511 }
1512
1513 if (err)
1514 goto free_value;
1515
Wei Yongjun7f645462020-04-30 08:18:51 +00001516 if (copy_to_user(uvalue, value, value_size) != 0) {
1517 err = -EFAULT;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001518 goto free_value;
Wei Yongjun7f645462020-04-30 08:18:51 +00001519 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001520
1521 err = 0;
1522
1523free_value:
1524 kfree(value);
1525free_key:
1526 kfree(key);
1527err_put:
1528 fdput(f);
1529 return err;
1530}
1531
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001532#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1533
1534static int map_freeze(const union bpf_attr *attr)
1535{
1536 int err = 0, ufd = attr->map_fd;
1537 struct bpf_map *map;
1538 struct fd f;
1539
1540 if (CHECK_ATTR(BPF_MAP_FREEZE))
1541 return -EINVAL;
1542
1543 f = fdget(ufd);
1544 map = __bpf_map_get(f);
1545 if (IS_ERR(map))
1546 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001547
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001548 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1549 fdput(f);
1550 return -ENOTSUPP;
1551 }
1552
Andrii Nakryikofc970222019-11-17 09:28:04 -08001553 mutex_lock(&map->freeze_mutex);
1554
1555 if (map->writecnt) {
1556 err = -EBUSY;
1557 goto err_put;
1558 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001559 if (READ_ONCE(map->frozen)) {
1560 err = -EBUSY;
1561 goto err_put;
1562 }
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07001563 if (!bpf_capable()) {
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001564 err = -EPERM;
1565 goto err_put;
1566 }
1567
1568 WRITE_ONCE(map->frozen, true);
1569err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001570 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001571 fdput(f);
1572 return err;
1573}
1574
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001575static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001576#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001577 [_id] = & _name ## _prog_ops,
1578#define BPF_MAP_TYPE(_id, _ops)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001579#define BPF_LINK_TYPE(_id, _name)
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001580#include <linux/bpf_types.h>
1581#undef BPF_PROG_TYPE
1582#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001583#undef BPF_LINK_TYPE
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001584};
1585
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001586static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1587{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001588 const struct bpf_prog_ops *ops;
1589
1590 if (type >= ARRAY_SIZE(bpf_prog_types))
1591 return -EINVAL;
1592 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1593 ops = bpf_prog_types[type];
1594 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001595 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001596
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001597 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001598 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001599 else
1600 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001601 prog->type = type;
1602 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001603}
1604
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001605enum bpf_audit {
1606 BPF_AUDIT_LOAD,
1607 BPF_AUDIT_UNLOAD,
1608 BPF_AUDIT_MAX,
1609};
1610
1611static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1612 [BPF_AUDIT_LOAD] = "LOAD",
1613 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1614};
1615
1616static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1617{
1618 struct audit_context *ctx = NULL;
1619 struct audit_buffer *ab;
1620
1621 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1622 return;
1623 if (audit_enabled == AUDIT_OFF)
1624 return;
1625 if (op == BPF_AUDIT_LOAD)
1626 ctx = audit_context();
1627 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1628 if (unlikely(!ab))
1629 return;
1630 audit_log_format(ab, "prog-id=%u op=%s",
1631 prog->aux->id, bpf_audit_str[op]);
1632 audit_log_end(ab);
1633}
1634
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001635int __bpf_prog_charge(struct user_struct *user, u32 pages)
1636{
1637 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1638 unsigned long user_bufs;
1639
1640 if (user) {
1641 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1642 if (user_bufs > memlock_limit) {
1643 atomic_long_sub(pages, &user->locked_vm);
1644 return -EPERM;
1645 }
1646 }
1647
1648 return 0;
1649}
1650
1651void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1652{
1653 if (user)
1654 atomic_long_sub(pages, &user->locked_vm);
1655}
1656
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001657static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1658{
1659 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001660 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001661
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001662 ret = __bpf_prog_charge(user, prog->pages);
1663 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001664 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001665 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001666 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001667
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001668 prog->aux->user = user;
1669 return 0;
1670}
1671
1672static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1673{
1674 struct user_struct *user = prog->aux->user;
1675
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001676 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001677 free_uid(user);
1678}
1679
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001680static int bpf_prog_alloc_id(struct bpf_prog *prog)
1681{
1682 int id;
1683
Shaohua Lib76354c2018-03-27 11:53:21 -07001684 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001685 spin_lock_bh(&prog_idr_lock);
1686 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1687 if (id > 0)
1688 prog->aux->id = id;
1689 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001690 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001691
1692 /* id is in [1, INT_MAX) */
1693 if (WARN_ON_ONCE(!id))
1694 return -ENOSPC;
1695
1696 return id > 0 ? 0 : id;
1697}
1698
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001699void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001700{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001701 /* cBPF to eBPF migrations are currently not in the idr store.
1702 * Offloaded programs are removed from the store when their device
1703 * disappears - even if someone grabs an fd to them they are unusable,
1704 * simply waiting for refcnt to drop to be freed.
1705 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001706 if (!prog->aux->id)
1707 return;
1708
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001709 if (do_idr_lock)
1710 spin_lock_bh(&prog_idr_lock);
1711 else
1712 __acquire(&prog_idr_lock);
1713
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001714 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001715 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001716
1717 if (do_idr_lock)
1718 spin_unlock_bh(&prog_idr_lock);
1719 else
1720 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001721}
1722
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001723static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001724{
1725 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1726
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001727 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001728 kfree(aux->func_info_aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001729 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001730 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001731 bpf_prog_free(aux->prog);
1732}
1733
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001734static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1735{
1736 bpf_prog_kallsyms_del_all(prog);
1737 btf_put(prog->aux->btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001738 bpf_prog_free_linfo(prog);
1739
1740 if (deferred)
1741 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1742 else
1743 __bpf_prog_put_rcu(&prog->aux->rcu);
1744}
1745
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001746static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001747{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001748 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001749 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001750 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001751 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001752 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001753 __bpf_prog_put_noref(prog, true);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001754 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001755}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001756
1757void bpf_prog_put(struct bpf_prog *prog)
1758{
1759 __bpf_prog_put(prog, true);
1760}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001761EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001762
1763static int bpf_prog_release(struct inode *inode, struct file *filp)
1764{
1765 struct bpf_prog *prog = filp->private_data;
1766
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001767 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001768 return 0;
1769}
1770
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001771static void bpf_prog_get_stats(const struct bpf_prog *prog,
1772 struct bpf_prog_stats *stats)
1773{
1774 u64 nsecs = 0, cnt = 0;
1775 int cpu;
1776
1777 for_each_possible_cpu(cpu) {
1778 const struct bpf_prog_stats *st;
1779 unsigned int start;
1780 u64 tnsecs, tcnt;
1781
1782 st = per_cpu_ptr(prog->aux->stats, cpu);
1783 do {
1784 start = u64_stats_fetch_begin_irq(&st->syncp);
1785 tnsecs = st->nsecs;
1786 tcnt = st->cnt;
1787 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1788 nsecs += tnsecs;
1789 cnt += tcnt;
1790 }
1791 stats->nsecs = nsecs;
1792 stats->cnt = cnt;
1793}
1794
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001795#ifdef CONFIG_PROC_FS
1796static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1797{
1798 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001799 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001800 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001801
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001802 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001803 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001804 seq_printf(m,
1805 "prog_type:\t%u\n"
1806 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001807 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001808 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001809 "prog_id:\t%u\n"
1810 "run_time_ns:\t%llu\n"
1811 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001812 prog->type,
1813 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001814 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001815 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001816 prog->aux->id,
1817 stats.nsecs,
1818 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001819}
1820#endif
1821
Chenbo Fengf66e4482017-10-18 13:00:26 -07001822const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001823#ifdef CONFIG_PROC_FS
1824 .show_fdinfo = bpf_prog_show_fdinfo,
1825#endif
1826 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001827 .read = bpf_dummy_read,
1828 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001829};
1830
Daniel Borkmannb2197752015-10-29 14:58:09 +01001831int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001832{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001833 int ret;
1834
1835 ret = security_bpf_prog(prog);
1836 if (ret < 0)
1837 return ret;
1838
Daniel Borkmannaa797812015-10-29 14:58:06 +01001839 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1840 O_RDWR | O_CLOEXEC);
1841}
1842
Daniel Borkmann113214b2016-06-30 17:24:44 +02001843static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001844{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001845 if (!f.file)
1846 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001847 if (f.file->f_op != &bpf_prog_fops) {
1848 fdput(f);
1849 return ERR_PTR(-EINVAL);
1850 }
1851
Daniel Borkmannc2101292015-10-29 14:58:07 +01001852 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001853}
1854
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001855void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001856{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001857 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001858}
Brenden Blanco59d36562016-07-19 12:16:46 -07001859EXPORT_SYMBOL_GPL(bpf_prog_add);
1860
Daniel Borkmannc5405942016-11-09 22:02:34 +01001861void bpf_prog_sub(struct bpf_prog *prog, int i)
1862{
1863 /* Only to be used for undoing previous bpf_prog_add() in some
1864 * error path. We still know that another entity in our call
1865 * path holds a reference to the program, thus atomic_sub() can
1866 * be safely used in such cases!
1867 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001868 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001869}
1870EXPORT_SYMBOL_GPL(bpf_prog_sub);
1871
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001872void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001873{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001874 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001875}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001876EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001877
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001878/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001879struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001880{
1881 int refold;
1882
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001883 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001884
1885 if (!refold)
1886 return ERR_PTR(-ENOENT);
1887
1888 return prog;
1889}
John Fastabenda6f6df62017-08-15 22:32:22 -07001890EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001891
Al Viro040ee692017-12-02 20:20:38 -05001892bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001893 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001894{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001895 /* not an attachment, just a refcount inc, always allow */
1896 if (!attach_type)
1897 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001898
1899 if (prog->type != *attach_type)
1900 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001901 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001902 return false;
1903
1904 return true;
1905}
1906
1907static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001908 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001909{
1910 struct fd f = fdget(ufd);
1911 struct bpf_prog *prog;
1912
Daniel Borkmann113214b2016-06-30 17:24:44 +02001913 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001914 if (IS_ERR(prog))
1915 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001916 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001917 prog = ERR_PTR(-EINVAL);
1918 goto out;
1919 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001920
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001921 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001922out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001923 fdput(f);
1924 return prog;
1925}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001926
1927struct bpf_prog *bpf_prog_get(u32 ufd)
1928{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001929 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001930}
1931
Jakub Kicinski248f3462017-11-03 13:56:20 -07001932struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001933 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001934{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001935 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001936}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001937EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001938
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001939/* Initially all BPF programs could be loaded w/o specifying
1940 * expected_attach_type. Later for some of them specifying expected_attach_type
1941 * at load time became required so that program could be validated properly.
1942 * Programs of types that are allowed to be loaded both w/ and w/o (for
1943 * backward compatibility) expected_attach_type, should have the default attach
1944 * type assigned to expected_attach_type for the latter case, so that it can be
1945 * validated later at attach time.
1946 *
1947 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1948 * prog type requires it but has some attach types that have to be backward
1949 * compatible.
1950 */
1951static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1952{
1953 switch (attr->prog_type) {
1954 case BPF_PROG_TYPE_CGROUP_SOCK:
1955 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1956 * exist so checking for non-zero is the way to go here.
1957 */
1958 if (!attr->expected_attach_type)
1959 attr->expected_attach_type =
1960 BPF_CGROUP_INET_SOCK_CREATE;
1961 break;
1962 }
1963}
1964
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001965static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001966bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1967 enum bpf_attach_type expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001968 u32 btf_id, u32 prog_fd)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001969{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001970 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001971 if (btf_id > BTF_MAX_TYPE)
1972 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001973
1974 switch (prog_type) {
1975 case BPF_PROG_TYPE_TRACING:
KP Singh9e4e01d2020-03-29 01:43:52 +01001976 case BPF_PROG_TYPE_LSM:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001977 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001978 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001979 break;
1980 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001981 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001982 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001983 }
1984
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001985 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1986 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001987 return -EINVAL;
1988
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001989 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001990 case BPF_PROG_TYPE_CGROUP_SOCK:
1991 switch (expected_attach_type) {
1992 case BPF_CGROUP_INET_SOCK_CREATE:
1993 case BPF_CGROUP_INET4_POST_BIND:
1994 case BPF_CGROUP_INET6_POST_BIND:
1995 return 0;
1996 default:
1997 return -EINVAL;
1998 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001999 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2000 switch (expected_attach_type) {
2001 case BPF_CGROUP_INET4_BIND:
2002 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002003 case BPF_CGROUP_INET4_CONNECT:
2004 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002005 case BPF_CGROUP_INET4_GETPEERNAME:
2006 case BPF_CGROUP_INET6_GETPEERNAME:
2007 case BPF_CGROUP_INET4_GETSOCKNAME:
2008 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002009 case BPF_CGROUP_UDP4_SENDMSG:
2010 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002011 case BPF_CGROUP_UDP4_RECVMSG:
2012 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002013 return 0;
2014 default:
2015 return -EINVAL;
2016 }
brakmo5cf1e912019-05-28 16:59:36 -07002017 case BPF_PROG_TYPE_CGROUP_SKB:
2018 switch (expected_attach_type) {
2019 case BPF_CGROUP_INET_INGRESS:
2020 case BPF_CGROUP_INET_EGRESS:
2021 return 0;
2022 default:
2023 return -EINVAL;
2024 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002025 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2026 switch (expected_attach_type) {
2027 case BPF_CGROUP_SETSOCKOPT:
2028 case BPF_CGROUP_GETSOCKOPT:
2029 return 0;
2030 default:
2031 return -EINVAL;
2032 }
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002033 case BPF_PROG_TYPE_EXT:
2034 if (expected_attach_type)
2035 return -EINVAL;
2036 /* fallthrough */
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002037 default:
2038 return 0;
2039 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002040}
2041
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002042static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2043{
2044 switch (prog_type) {
2045 case BPF_PROG_TYPE_SCHED_CLS:
2046 case BPF_PROG_TYPE_SCHED_ACT:
2047 case BPF_PROG_TYPE_XDP:
2048 case BPF_PROG_TYPE_LWT_IN:
2049 case BPF_PROG_TYPE_LWT_OUT:
2050 case BPF_PROG_TYPE_LWT_XMIT:
2051 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2052 case BPF_PROG_TYPE_SK_SKB:
2053 case BPF_PROG_TYPE_SK_MSG:
2054 case BPF_PROG_TYPE_LIRC_MODE2:
2055 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2056 case BPF_PROG_TYPE_CGROUP_DEVICE:
2057 case BPF_PROG_TYPE_CGROUP_SOCK:
2058 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2059 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2060 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2061 case BPF_PROG_TYPE_SOCK_OPS:
2062 case BPF_PROG_TYPE_EXT: /* extends any prog */
2063 return true;
2064 case BPF_PROG_TYPE_CGROUP_SKB:
2065 /* always unpriv */
2066 case BPF_PROG_TYPE_SK_REUSEPORT:
2067 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2068 default:
2069 return false;
2070 }
2071}
2072
2073static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2074{
2075 switch (prog_type) {
2076 case BPF_PROG_TYPE_KPROBE:
2077 case BPF_PROG_TYPE_TRACEPOINT:
2078 case BPF_PROG_TYPE_PERF_EVENT:
2079 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2080 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2081 case BPF_PROG_TYPE_TRACING:
2082 case BPF_PROG_TYPE_LSM:
2083 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2084 case BPF_PROG_TYPE_EXT: /* extends any prog */
2085 return true;
2086 default:
2087 return false;
2088 }
2089}
2090
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002091/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002092#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002093
Yonghong Song838e9692018-11-19 15:29:11 -08002094static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002095{
2096 enum bpf_prog_type type = attr->prog_type;
2097 struct bpf_prog *prog;
2098 int err;
2099 char license[128];
2100 bool is_gpl;
2101
2102 if (CHECK_ATTR(BPF_PROG_LOAD))
2103 return -EINVAL;
2104
Jiong Wangc240eff2019-05-24 23:25:16 +01002105 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2106 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002107 BPF_F_TEST_STATE_FREQ |
Jiong Wangc240eff2019-05-24 23:25:16 +01002108 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07002109 return -EINVAL;
2110
David Millere9ee9ef2018-11-30 21:08:14 -08002111 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2112 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002113 !bpf_capable())
David Millere9ee9ef2018-11-30 21:08:14 -08002114 return -EPERM;
2115
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002116 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002117 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002118 sizeof(license) - 1) < 0)
2119 return -EFAULT;
2120 license[sizeof(license) - 1] = 0;
2121
2122 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2123 is_gpl = license_is_gpl_compatible(license);
2124
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002125 if (attr->insn_cnt == 0 ||
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002126 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002127 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002128 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2129 type != BPF_PROG_TYPE_CGROUP_SKB &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002130 !bpf_capable())
2131 return -EPERM;
2132
2133 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN))
2134 return -EPERM;
2135 if (is_perfmon_prog_type(type) && !perfmon_capable())
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002136 return -EPERM;
2137
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002138 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002139 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002140 attr->attach_btf_id,
2141 attr->attach_prog_fd))
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002142 return -EINVAL;
2143
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002144 /* plain bpf_prog allocation */
2145 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2146 if (!prog)
2147 return -ENOMEM;
2148
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002149 prog->expected_attach_type = attr->expected_attach_type;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002150 prog->aux->attach_btf_id = attr->attach_btf_id;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002151 if (attr->attach_prog_fd) {
2152 struct bpf_prog *tgt_prog;
2153
2154 tgt_prog = bpf_prog_get(attr->attach_prog_fd);
2155 if (IS_ERR(tgt_prog)) {
2156 err = PTR_ERR(tgt_prog);
2157 goto free_prog_nouncharge;
2158 }
2159 prog->aux->linked_prog = tgt_prog;
2160 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002161
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002162 prog->aux->offload_requested = !!attr->prog_ifindex;
2163
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002164 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002165 if (err)
2166 goto free_prog_nouncharge;
2167
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002168 err = bpf_prog_charge_memlock(prog);
2169 if (err)
2170 goto free_prog_sec;
2171
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002172 prog->len = attr->insn_cnt;
2173
2174 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002175 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01002176 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002177 goto free_prog;
2178
2179 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002180 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002181
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002182 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002183 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002184
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002185 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002186 err = bpf_prog_offload_init(prog, attr);
2187 if (err)
2188 goto free_prog;
2189 }
2190
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002191 /* find program type: socket_filter vs tracing_filter */
2192 err = find_prog_type(type, prog);
2193 if (err < 0)
2194 goto free_prog;
2195
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002196 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002197 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2198 sizeof(attr->prog_name));
2199 if (err < 0)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002200 goto free_prog;
2201
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002202 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002203 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002204 if (err < 0)
2205 goto free_used_maps;
2206
Daniel Borkmann9facc332018-06-15 02:30:48 +02002207 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002208 if (err < 0)
2209 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002210
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002211 err = bpf_prog_alloc_id(prog);
2212 if (err)
2213 goto free_used_maps;
2214
Daniel Borkmannc7517982019-08-23 22:14:23 +02002215 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2216 * effectively publicly exposed. However, retrieving via
2217 * bpf_prog_get_fd_by_id() will take another reference,
2218 * therefore it cannot be gone underneath us.
2219 *
2220 * Only for the time /after/ successful bpf_prog_new_fd()
2221 * and before returning to userspace, we might just hold
2222 * one reference and any parallel close on that fd could
2223 * rip everything out. Hence, below notifications must
2224 * happen before bpf_prog_new_fd().
2225 *
2226 * Also, any failure handling from this point onwards must
2227 * be using bpf_prog_put() given the program is exposed.
2228 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002229 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002230 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002231 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002232
2233 err = bpf_prog_new_fd(prog);
2234 if (err < 0)
2235 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002236 return err;
2237
2238free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002239 /* In case we have subprogs, we need to wait for a grace
2240 * period before we can tear down JIT memory since symbols
2241 * are already exposed under kallsyms.
2242 */
2243 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2244 return err;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002245free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002246 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002247free_prog_sec:
2248 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002249free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002250 bpf_prog_free(prog);
2251 return err;
2252}
2253
Chenbo Feng6e71b042017-10-18 13:00:22 -07002254#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002255
2256static int bpf_obj_pin(const union bpf_attr *attr)
2257{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002258 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002259 return -EINVAL;
2260
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002261 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002262}
2263
2264static int bpf_obj_get(const union bpf_attr *attr)
2265{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002266 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2267 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002268 return -EINVAL;
2269
Chenbo Feng6e71b042017-10-18 13:00:22 -07002270 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2271 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002272}
2273
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002274void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002275 const struct bpf_link_ops *ops, struct bpf_prog *prog)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002276{
2277 atomic64_set(&link->refcnt, 1);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002278 link->type = type;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002279 link->id = 0;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002280 link->ops = ops;
2281 link->prog = prog;
2282}
2283
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002284static void bpf_link_free_id(int id)
2285{
2286 if (!id)
2287 return;
2288
2289 spin_lock_bh(&link_idr_lock);
2290 idr_remove(&link_idr, id);
2291 spin_unlock_bh(&link_idr_lock);
2292}
2293
Andrii Nakryiko98868662020-03-12 17:21:28 -07002294/* Clean up bpf_link and corresponding anon_inode file and FD. After
2295 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002296 * anon_inode's release() call. This helper marksbpf_link as
2297 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2298 * is not decremented, it's the responsibility of a calling code that failed
2299 * to complete bpf_link initialization.
Andrii Nakryiko98868662020-03-12 17:21:28 -07002300 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002301void bpf_link_cleanup(struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002302{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002303 primer->link->prog = NULL;
2304 bpf_link_free_id(primer->id);
2305 fput(primer->file);
2306 put_unused_fd(primer->fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002307}
2308
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002309void bpf_link_inc(struct bpf_link *link)
2310{
2311 atomic64_inc(&link->refcnt);
2312}
2313
2314/* bpf_link_free is guaranteed to be called from process context */
2315static void bpf_link_free(struct bpf_link *link)
2316{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002317 bpf_link_free_id(link->id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002318 if (link->prog) {
2319 /* detach BPF program, clean up used resources */
2320 link->ops->release(link);
2321 bpf_prog_put(link->prog);
2322 }
2323 /* free bpf_link and its containing memory */
2324 link->ops->dealloc(link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002325}
2326
2327static void bpf_link_put_deferred(struct work_struct *work)
2328{
2329 struct bpf_link *link = container_of(work, struct bpf_link, work);
2330
2331 bpf_link_free(link);
2332}
2333
2334/* bpf_link_put can be called from atomic context, but ensures that resources
2335 * are freed from process context
2336 */
2337void bpf_link_put(struct bpf_link *link)
2338{
2339 if (!atomic64_dec_and_test(&link->refcnt))
2340 return;
2341
2342 if (in_atomic()) {
2343 INIT_WORK(&link->work, bpf_link_put_deferred);
2344 schedule_work(&link->work);
2345 } else {
2346 bpf_link_free(link);
2347 }
2348}
2349
2350static int bpf_link_release(struct inode *inode, struct file *filp)
2351{
2352 struct bpf_link *link = filp->private_data;
2353
2354 bpf_link_put(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002355 return 0;
2356}
2357
Arnd Bergmann449e14b2020-04-29 15:21:58 +02002358#ifdef CONFIG_PROC_FS
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002359#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2360#define BPF_MAP_TYPE(_id, _ops)
2361#define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2362static const char *bpf_link_type_strs[] = {
2363 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2364#include <linux/bpf_types.h>
2365};
2366#undef BPF_PROG_TYPE
2367#undef BPF_MAP_TYPE
2368#undef BPF_LINK_TYPE
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002369
2370static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2371{
2372 const struct bpf_link *link = filp->private_data;
2373 const struct bpf_prog *prog = link->prog;
2374 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002375
2376 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2377 seq_printf(m,
2378 "link_type:\t%s\n"
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002379 "link_id:\t%u\n"
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002380 "prog_tag:\t%s\n"
2381 "prog_id:\t%u\n",
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002382 bpf_link_type_strs[link->type],
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002383 link->id,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002384 prog_tag,
2385 prog->aux->id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002386 if (link->ops->show_fdinfo)
2387 link->ops->show_fdinfo(link, m);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002388}
2389#endif
2390
Zou Wei6f302bf2020-04-23 10:32:40 +08002391static const struct file_operations bpf_link_fops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002392#ifdef CONFIG_PROC_FS
2393 .show_fdinfo = bpf_link_show_fdinfo,
2394#endif
2395 .release = bpf_link_release,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002396 .read = bpf_dummy_read,
2397 .write = bpf_dummy_write,
2398};
2399
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002400static int bpf_link_alloc_id(struct bpf_link *link)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002401{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002402 int id;
2403
2404 idr_preload(GFP_KERNEL);
2405 spin_lock_bh(&link_idr_lock);
2406 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2407 spin_unlock_bh(&link_idr_lock);
2408 idr_preload_end();
2409
2410 return id;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002411}
2412
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002413/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2414 * reserving unused FD and allocating ID from link_idr. This is to be paired
2415 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2416 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2417 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2418 * transient state is passed around in struct bpf_link_primer.
2419 * This is preferred way to create and initialize bpf_link, especially when
2420 * there are complicated and expensive operations inbetween creating bpf_link
2421 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2422 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2423 * expensive (and potentially failing) roll back operations in a rare case
2424 * that file, FD, or ID can't be allocated.
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002425 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002426int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002427{
2428 struct file *file;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002429 int fd, id;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002430
2431 fd = get_unused_fd_flags(O_CLOEXEC);
2432 if (fd < 0)
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002433 return fd;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002434
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002435
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002436 id = bpf_link_alloc_id(link);
2437 if (id < 0) {
2438 put_unused_fd(fd);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002439 return id;
2440 }
2441
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002442 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2443 if (IS_ERR(file)) {
2444 bpf_link_free_id(id);
2445 put_unused_fd(fd);
2446 return PTR_ERR(file);
2447 }
2448
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002449 primer->link = link;
2450 primer->file = file;
2451 primer->fd = fd;
2452 primer->id = id;
2453 return 0;
2454}
2455
2456int bpf_link_settle(struct bpf_link_primer *primer)
2457{
2458 /* make bpf_link fetchable by ID */
2459 spin_lock_bh(&link_idr_lock);
2460 primer->link->id = primer->id;
2461 spin_unlock_bh(&link_idr_lock);
2462 /* make bpf_link fetchable by FD */
2463 fd_install(primer->fd, primer->file);
2464 /* pass through installed FD */
2465 return primer->fd;
2466}
2467
2468int bpf_link_new_fd(struct bpf_link *link)
2469{
2470 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002471}
2472
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002473struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2474{
2475 struct fd f = fdget(ufd);
2476 struct bpf_link *link;
2477
2478 if (!f.file)
2479 return ERR_PTR(-EBADF);
2480 if (f.file->f_op != &bpf_link_fops) {
2481 fdput(f);
2482 return ERR_PTR(-EINVAL);
2483 }
2484
2485 link = f.file->private_data;
2486 bpf_link_inc(link);
2487 fdput(f);
2488
2489 return link;
2490}
2491
2492struct bpf_tracing_link {
2493 struct bpf_link link;
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002494 enum bpf_attach_type attach_type;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002495};
2496
2497static void bpf_tracing_link_release(struct bpf_link *link)
2498{
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002499 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog));
2500}
2501
2502static void bpf_tracing_link_dealloc(struct bpf_link *link)
2503{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002504 struct bpf_tracing_link *tr_link =
2505 container_of(link, struct bpf_tracing_link, link);
2506
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002507 kfree(tr_link);
2508}
2509
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002510static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2511 struct seq_file *seq)
2512{
2513 struct bpf_tracing_link *tr_link =
2514 container_of(link, struct bpf_tracing_link, link);
2515
2516 seq_printf(seq,
2517 "attach_type:\t%d\n",
2518 tr_link->attach_type);
2519}
2520
2521static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2522 struct bpf_link_info *info)
2523{
2524 struct bpf_tracing_link *tr_link =
2525 container_of(link, struct bpf_tracing_link, link);
2526
2527 info->tracing.attach_type = tr_link->attach_type;
2528
2529 return 0;
2530}
2531
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002532static const struct bpf_link_ops bpf_tracing_link_lops = {
2533 .release = bpf_tracing_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002534 .dealloc = bpf_tracing_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002535 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2536 .fill_link_info = bpf_tracing_link_fill_link_info,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002537};
2538
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002539static int bpf_tracing_prog_attach(struct bpf_prog *prog)
2540{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002541 struct bpf_link_primer link_primer;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002542 struct bpf_tracing_link *link;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002543 int err;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002544
KP Singh9e4e01d2020-03-29 01:43:52 +01002545 switch (prog->type) {
2546 case BPF_PROG_TYPE_TRACING:
2547 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2548 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2549 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2550 err = -EINVAL;
2551 goto out_put_prog;
2552 }
2553 break;
2554 case BPF_PROG_TYPE_EXT:
2555 if (prog->expected_attach_type != 0) {
2556 err = -EINVAL;
2557 goto out_put_prog;
2558 }
2559 break;
2560 case BPF_PROG_TYPE_LSM:
2561 if (prog->expected_attach_type != BPF_LSM_MAC) {
2562 err = -EINVAL;
2563 goto out_put_prog;
2564 }
2565 break;
2566 default:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002567 err = -EINVAL;
2568 goto out_put_prog;
2569 }
2570
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002571 link = kzalloc(sizeof(*link), GFP_USER);
2572 if (!link) {
2573 err = -ENOMEM;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002574 goto out_put_prog;
2575 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002576 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2577 &bpf_tracing_link_lops, prog);
2578 link->attach_type = prog->expected_attach_type;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002579
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002580 err = bpf_link_prime(&link->link, &link_primer);
2581 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002582 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002583 goto out_put_prog;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002584 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002585
2586 err = bpf_trampoline_link_prog(prog);
2587 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002588 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002589 goto out_put_prog;
2590 }
2591
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002592 return bpf_link_settle(&link_primer);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002593out_put_prog:
2594 bpf_prog_put(prog);
2595 return err;
2596}
2597
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002598struct bpf_raw_tp_link {
2599 struct bpf_link link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002600 struct bpf_raw_event_map *btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002601};
2602
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002603static void bpf_raw_tp_link_release(struct bpf_link *link)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002604{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002605 struct bpf_raw_tp_link *raw_tp =
2606 container_of(link, struct bpf_raw_tp_link, link);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002607
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002608 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002609 bpf_put_raw_tracepoint(raw_tp->btp);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002610}
2611
2612static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2613{
2614 struct bpf_raw_tp_link *raw_tp =
2615 container_of(link, struct bpf_raw_tp_link, link);
2616
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002617 kfree(raw_tp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002618}
2619
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002620static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2621 struct seq_file *seq)
2622{
2623 struct bpf_raw_tp_link *raw_tp_link =
2624 container_of(link, struct bpf_raw_tp_link, link);
2625
2626 seq_printf(seq,
2627 "tp_name:\t%s\n",
2628 raw_tp_link->btp->tp->name);
2629}
2630
2631static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2632 struct bpf_link_info *info)
2633{
2634 struct bpf_raw_tp_link *raw_tp_link =
2635 container_of(link, struct bpf_raw_tp_link, link);
2636 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2637 const char *tp_name = raw_tp_link->btp->tp->name;
2638 u32 ulen = info->raw_tracepoint.tp_name_len;
2639 size_t tp_len = strlen(tp_name);
2640
2641 if (ulen && !ubuf)
2642 return -EINVAL;
2643
2644 info->raw_tracepoint.tp_name_len = tp_len + 1;
2645
2646 if (!ubuf)
2647 return 0;
2648
2649 if (ulen >= tp_len + 1) {
2650 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2651 return -EFAULT;
2652 } else {
2653 char zero = '\0';
2654
2655 if (copy_to_user(ubuf, tp_name, ulen - 1))
2656 return -EFAULT;
2657 if (put_user(zero, ubuf + ulen - 1))
2658 return -EFAULT;
2659 return -ENOSPC;
2660 }
2661
2662 return 0;
2663}
2664
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002665static const struct bpf_link_ops bpf_raw_tp_link_lops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002666 .release = bpf_raw_tp_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002667 .dealloc = bpf_raw_tp_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002668 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2669 .fill_link_info = bpf_raw_tp_link_fill_link_info,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002670};
2671
2672#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2673
2674static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2675{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002676 struct bpf_link_primer link_primer;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002677 struct bpf_raw_tp_link *link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002678 struct bpf_raw_event_map *btp;
2679 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002680 const char *tp_name;
2681 char buf[128];
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002682 int err;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002683
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002684 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2685 return -EINVAL;
2686
2687 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2688 if (IS_ERR(prog))
2689 return PTR_ERR(prog);
2690
KP Singh9e4e01d2020-03-29 01:43:52 +01002691 switch (prog->type) {
2692 case BPF_PROG_TYPE_TRACING:
2693 case BPF_PROG_TYPE_EXT:
2694 case BPF_PROG_TYPE_LSM:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002695 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002696 /* The attach point for this category of programs
2697 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002698 */
2699 err = -EINVAL;
2700 goto out_put_prog;
2701 }
KP Singh9e4e01d2020-03-29 01:43:52 +01002702 if (prog->type == BPF_PROG_TYPE_TRACING &&
2703 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002704 tp_name = prog->aux->attach_func_name;
KP Singh9e4e01d2020-03-29 01:43:52 +01002705 break;
2706 }
2707 return bpf_tracing_prog_attach(prog);
2708 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2709 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002710 if (strncpy_from_user(buf,
2711 u64_to_user_ptr(attr->raw_tracepoint.name),
2712 sizeof(buf) - 1) < 0) {
2713 err = -EFAULT;
2714 goto out_put_prog;
2715 }
2716 buf[sizeof(buf) - 1] = 0;
2717 tp_name = buf;
KP Singh9e4e01d2020-03-29 01:43:52 +01002718 break;
2719 default:
2720 err = -EINVAL;
2721 goto out_put_prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002722 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002723
Matt Mullinsa38d1102018-12-12 16:42:37 -08002724 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002725 if (!btp) {
2726 err = -ENOENT;
2727 goto out_put_prog;
2728 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002729
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002730 link = kzalloc(sizeof(*link), GFP_USER);
2731 if (!link) {
Matt Mullinsa38d1102018-12-12 16:42:37 -08002732 err = -ENOMEM;
2733 goto out_put_btp;
2734 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002735 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2736 &bpf_raw_tp_link_lops, prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002737 link->btp = btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002738
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002739 err = bpf_link_prime(&link->link, &link_primer);
2740 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002741 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002742 goto out_put_btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002743 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002744
2745 err = bpf_probe_register(link->btp, prog);
2746 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002747 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002748 goto out_put_btp;
2749 }
2750
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002751 return bpf_link_settle(&link_primer);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002752
Matt Mullinsa38d1102018-12-12 16:42:37 -08002753out_put_btp:
2754 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002755out_put_prog:
2756 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002757 return err;
2758}
2759
Anders Roxell33491582018-04-03 14:09:47 +02002760static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2761 enum bpf_attach_type attach_type)
2762{
2763 switch (prog->type) {
2764 case BPF_PROG_TYPE_CGROUP_SOCK:
2765 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002766 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Anders Roxell33491582018-04-03 14:09:47 +02002767 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07002768 case BPF_PROG_TYPE_CGROUP_SKB:
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002769 if (!capable(CAP_NET_ADMIN))
2770 /* cg-skb progs can be loaded by unpriv user.
2771 * check permissions at attach time.
2772 */
2773 return -EPERM;
brakmo5cf1e912019-05-28 16:59:36 -07002774 return prog->enforce_expected_attach_type &&
2775 prog->expected_attach_type != attach_type ?
2776 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02002777 default:
2778 return 0;
2779 }
2780}
2781
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002782static enum bpf_prog_type
2783attach_type_to_prog_type(enum bpf_attach_type attach_type)
2784{
2785 switch (attach_type) {
2786 case BPF_CGROUP_INET_INGRESS:
2787 case BPF_CGROUP_INET_EGRESS:
2788 return BPF_PROG_TYPE_CGROUP_SKB;
2789 break;
2790 case BPF_CGROUP_INET_SOCK_CREATE:
2791 case BPF_CGROUP_INET4_POST_BIND:
2792 case BPF_CGROUP_INET6_POST_BIND:
2793 return BPF_PROG_TYPE_CGROUP_SOCK;
2794 case BPF_CGROUP_INET4_BIND:
2795 case BPF_CGROUP_INET6_BIND:
2796 case BPF_CGROUP_INET4_CONNECT:
2797 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002798 case BPF_CGROUP_INET4_GETPEERNAME:
2799 case BPF_CGROUP_INET6_GETPEERNAME:
2800 case BPF_CGROUP_INET4_GETSOCKNAME:
2801 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002802 case BPF_CGROUP_UDP4_SENDMSG:
2803 case BPF_CGROUP_UDP6_SENDMSG:
2804 case BPF_CGROUP_UDP4_RECVMSG:
2805 case BPF_CGROUP_UDP6_RECVMSG:
2806 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2807 case BPF_CGROUP_SOCK_OPS:
2808 return BPF_PROG_TYPE_SOCK_OPS;
2809 case BPF_CGROUP_DEVICE:
2810 return BPF_PROG_TYPE_CGROUP_DEVICE;
2811 case BPF_SK_MSG_VERDICT:
2812 return BPF_PROG_TYPE_SK_MSG;
2813 case BPF_SK_SKB_STREAM_PARSER:
2814 case BPF_SK_SKB_STREAM_VERDICT:
2815 return BPF_PROG_TYPE_SK_SKB;
2816 case BPF_LIRC_MODE2:
2817 return BPF_PROG_TYPE_LIRC_MODE2;
2818 case BPF_FLOW_DISSECTOR:
2819 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2820 case BPF_CGROUP_SYSCTL:
2821 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2822 case BPF_CGROUP_GETSOCKOPT:
2823 case BPF_CGROUP_SETSOCKOPT:
2824 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
Yonghong Songde4e05c2020-05-09 10:59:01 -07002825 case BPF_TRACE_ITER:
2826 return BPF_PROG_TYPE_TRACING;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002827 default:
2828 return BPF_PROG_TYPE_UNSPEC;
2829 }
2830}
2831
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002832#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07002833
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002834#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002835 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002836
Daniel Mackf4324552016-11-23 16:52:27 +01002837static int bpf_prog_attach(const union bpf_attr *attr)
2838{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002839 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002840 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002841 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002842
Daniel Mackf4324552016-11-23 16:52:27 +01002843 if (CHECK_ATTR(BPF_PROG_ATTACH))
2844 return -EINVAL;
2845
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002846 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002847 return -EINVAL;
2848
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002849 ptype = attach_type_to_prog_type(attr->attach_type);
2850 if (ptype == BPF_PROG_TYPE_UNSPEC)
Daniel Mackf4324552016-11-23 16:52:27 +01002851 return -EINVAL;
Daniel Mackf4324552016-11-23 16:52:27 +01002852
David Ahernb2cd1252016-12-01 08:48:03 -08002853 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2854 if (IS_ERR(prog))
2855 return PTR_ERR(prog);
2856
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002857 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2858 bpf_prog_put(prog);
2859 return -EINVAL;
2860 }
2861
Sean Youngfdb5c452018-06-19 00:04:24 +01002862 switch (ptype) {
2863 case BPF_PROG_TYPE_SK_SKB:
2864 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002865 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01002866 break;
2867 case BPF_PROG_TYPE_LIRC_MODE2:
2868 ret = lirc_prog_attach(attr, prog);
2869 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07002870 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2871 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
2872 break;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002873 case BPF_PROG_TYPE_CGROUP_DEVICE:
2874 case BPF_PROG_TYPE_CGROUP_SKB:
2875 case BPF_PROG_TYPE_CGROUP_SOCK:
2876 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2877 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2878 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2879 case BPF_PROG_TYPE_SOCK_OPS:
Sean Youngfdb5c452018-06-19 00:04:24 +01002880 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002881 break;
2882 default:
2883 ret = -EINVAL;
David Ahernb2cd1252016-12-01 08:48:03 -08002884 }
2885
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002886 if (ret)
2887 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002888 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002889}
2890
2891#define BPF_PROG_DETACH_LAST_FIELD attach_type
2892
2893static int bpf_prog_detach(const union bpf_attr *attr)
2894{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002895 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002896
Daniel Mackf4324552016-11-23 16:52:27 +01002897 if (CHECK_ATTR(BPF_PROG_DETACH))
2898 return -EINVAL;
2899
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002900 ptype = attach_type_to_prog_type(attr->attach_type);
2901
2902 switch (ptype) {
2903 case BPF_PROG_TYPE_SK_MSG:
2904 case BPF_PROG_TYPE_SK_SKB:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002905 return sock_map_get_from_fd(attr, NULL);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002906 case BPF_PROG_TYPE_LIRC_MODE2:
Sean Youngf4364dc2018-05-27 12:24:09 +01002907 return lirc_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002908 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002909 if (!capable(CAP_NET_ADMIN))
2910 return -EPERM;
Petar Penkovd58e4682018-09-14 07:46:18 -07002911 return skb_flow_dissector_bpf_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002912 case BPF_PROG_TYPE_CGROUP_DEVICE:
2913 case BPF_PROG_TYPE_CGROUP_SKB:
2914 case BPF_PROG_TYPE_CGROUP_SOCK:
2915 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2916 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2917 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2918 case BPF_PROG_TYPE_SOCK_OPS:
2919 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01002920 default:
2921 return -EINVAL;
2922 }
Daniel Mackf4324552016-11-23 16:52:27 +01002923}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002924
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002925#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2926
2927static int bpf_prog_query(const union bpf_attr *attr,
2928 union bpf_attr __user *uattr)
2929{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002930 if (!capable(CAP_NET_ADMIN))
2931 return -EPERM;
2932 if (CHECK_ATTR(BPF_PROG_QUERY))
2933 return -EINVAL;
2934 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2935 return -EINVAL;
2936
2937 switch (attr->query.attach_type) {
2938 case BPF_CGROUP_INET_INGRESS:
2939 case BPF_CGROUP_INET_EGRESS:
2940 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002941 case BPF_CGROUP_INET4_BIND:
2942 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002943 case BPF_CGROUP_INET4_POST_BIND:
2944 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002945 case BPF_CGROUP_INET4_CONNECT:
2946 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002947 case BPF_CGROUP_INET4_GETPEERNAME:
2948 case BPF_CGROUP_INET6_GETPEERNAME:
2949 case BPF_CGROUP_INET4_GETSOCKNAME:
2950 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002951 case BPF_CGROUP_UDP4_SENDMSG:
2952 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002953 case BPF_CGROUP_UDP4_RECVMSG:
2954 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002955 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05002956 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002957 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002958 case BPF_CGROUP_GETSOCKOPT:
2959 case BPF_CGROUP_SETSOCKOPT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002960 return cgroup_bpf_prog_query(attr, uattr);
Sean Youngf4364dc2018-05-27 12:24:09 +01002961 case BPF_LIRC_MODE2:
2962 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07002963 case BPF_FLOW_DISSECTOR:
2964 return skb_flow_dissector_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002965 default:
2966 return -EINVAL;
2967 }
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002968}
Daniel Mackf4324552016-11-23 16:52:27 +01002969
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002970#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002971
2972static int bpf_prog_test_run(const union bpf_attr *attr,
2973 union bpf_attr __user *uattr)
2974{
2975 struct bpf_prog *prog;
2976 int ret = -ENOTSUPP;
2977
2978 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2979 return -EINVAL;
2980
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002981 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2982 (!attr->test.ctx_size_in && attr->test.ctx_in))
2983 return -EINVAL;
2984
2985 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2986 (!attr->test.ctx_size_out && attr->test.ctx_out))
2987 return -EINVAL;
2988
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002989 prog = bpf_prog_get(attr->test.prog_fd);
2990 if (IS_ERR(prog))
2991 return PTR_ERR(prog);
2992
2993 if (prog->aux->ops->test_run)
2994 ret = prog->aux->ops->test_run(prog, attr, uattr);
2995
2996 bpf_prog_put(prog);
2997 return ret;
2998}
2999
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003000#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3001
3002static int bpf_obj_get_next_id(const union bpf_attr *attr,
3003 union bpf_attr __user *uattr,
3004 struct idr *idr,
3005 spinlock_t *lock)
3006{
3007 u32 next_id = attr->start_id;
3008 int err = 0;
3009
3010 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3011 return -EINVAL;
3012
3013 if (!capable(CAP_SYS_ADMIN))
3014 return -EPERM;
3015
3016 next_id++;
3017 spin_lock_bh(lock);
3018 if (!idr_get_next(idr, &next_id))
3019 err = -ENOENT;
3020 spin_unlock_bh(lock);
3021
3022 if (!err)
3023 err = put_user(next_id, &uattr->next_id);
3024
3025 return err;
3026}
3027
Yonghong Song6086d292020-05-09 10:59:09 -07003028struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3029{
3030 struct bpf_map *map;
3031
3032 spin_lock_bh(&map_idr_lock);
3033again:
3034 map = idr_get_next(&map_idr, id);
3035 if (map) {
3036 map = __bpf_map_inc_not_zero(map, false);
3037 if (IS_ERR(map)) {
3038 (*id)++;
3039 goto again;
3040 }
3041 }
3042 spin_unlock_bh(&map_idr_lock);
3043
3044 return map;
3045}
3046
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003047#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3048
Björn Töpel7e6897f2019-12-13 18:51:09 +01003049struct bpf_prog *bpf_prog_by_id(u32 id)
3050{
3051 struct bpf_prog *prog;
3052
3053 if (!id)
3054 return ERR_PTR(-ENOENT);
3055
3056 spin_lock_bh(&prog_idr_lock);
3057 prog = idr_find(&prog_idr, id);
3058 if (prog)
3059 prog = bpf_prog_inc_not_zero(prog);
3060 else
3061 prog = ERR_PTR(-ENOENT);
3062 spin_unlock_bh(&prog_idr_lock);
3063 return prog;
3064}
3065
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003066static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3067{
3068 struct bpf_prog *prog;
3069 u32 id = attr->prog_id;
3070 int fd;
3071
3072 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3073 return -EINVAL;
3074
3075 if (!capable(CAP_SYS_ADMIN))
3076 return -EPERM;
3077
Björn Töpel7e6897f2019-12-13 18:51:09 +01003078 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003079 if (IS_ERR(prog))
3080 return PTR_ERR(prog);
3081
3082 fd = bpf_prog_new_fd(prog);
3083 if (fd < 0)
3084 bpf_prog_put(prog);
3085
3086 return fd;
3087}
3088
Chenbo Feng6e71b042017-10-18 13:00:22 -07003089#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003090
3091static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3092{
3093 struct bpf_map *map;
3094 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07003095 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003096 int fd;
3097
Chenbo Feng6e71b042017-10-18 13:00:22 -07003098 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3099 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003100 return -EINVAL;
3101
3102 if (!capable(CAP_SYS_ADMIN))
3103 return -EPERM;
3104
Chenbo Feng6e71b042017-10-18 13:00:22 -07003105 f_flags = bpf_get_file_flag(attr->open_flags);
3106 if (f_flags < 0)
3107 return f_flags;
3108
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003109 spin_lock_bh(&map_idr_lock);
3110 map = idr_find(&map_idr, id);
3111 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07003112 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003113 else
3114 map = ERR_PTR(-ENOENT);
3115 spin_unlock_bh(&map_idr_lock);
3116
3117 if (IS_ERR(map))
3118 return PTR_ERR(map);
3119
Chenbo Feng6e71b042017-10-18 13:00:22 -07003120 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003121 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08003122 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003123
3124 return fd;
3125}
3126
Daniel Borkmann7105e822017-12-20 13:42:57 +01003127static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003128 unsigned long addr, u32 *off,
3129 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003130{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003131 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003132 int i;
3133
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003134 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3135 map = prog->aux->used_maps[i];
3136 if (map == (void *)addr) {
3137 *type = BPF_PSEUDO_MAP_FD;
3138 return map;
3139 }
3140 if (!map->ops->map_direct_value_meta)
3141 continue;
3142 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3143 *type = BPF_PSEUDO_MAP_VALUE;
3144 return map;
3145 }
3146 }
3147
Daniel Borkmann7105e822017-12-20 13:42:57 +01003148 return NULL;
3149}
3150
3151static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
3152{
3153 const struct bpf_map *map;
3154 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003155 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003156 u64 imm;
3157 int i;
3158
3159 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3160 GFP_USER);
3161 if (!insns)
3162 return insns;
3163
3164 for (i = 0; i < prog->len; i++) {
3165 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
3166 insns[i].code = BPF_JMP | BPF_CALL;
3167 insns[i].imm = BPF_FUNC_tail_call;
3168 /* fall-through */
3169 }
3170 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
3171 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
3172 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
3173 insns[i].code = BPF_JMP | BPF_CALL;
3174 if (!bpf_dump_raw_ok())
3175 insns[i].imm = 0;
3176 continue;
3177 }
3178
3179 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
3180 continue;
3181
3182 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003183 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003184 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003185 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003186 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003187 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003188 continue;
3189 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003190 }
3191
3192 return insns;
3193}
3194
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003195static int set_info_rec_size(struct bpf_prog_info *info)
3196{
3197 /*
3198 * Ensure info.*_rec_size is the same as kernel expected size
3199 *
3200 * or
3201 *
3202 * Only allow zero *_rec_size if both _rec_size and _cnt are
3203 * zero. In this case, the kernel will set the expected
3204 * _rec_size back to the info.
3205 */
3206
Yonghong Song11d8b822018-12-10 14:14:08 -08003207 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003208 info->func_info_rec_size != sizeof(struct bpf_func_info))
3209 return -EINVAL;
3210
Yonghong Song11d8b822018-12-10 14:14:08 -08003211 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003212 info->line_info_rec_size != sizeof(struct bpf_line_info))
3213 return -EINVAL;
3214
Yonghong Song11d8b822018-12-10 14:14:08 -08003215 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003216 info->jited_line_info_rec_size != sizeof(__u64))
3217 return -EINVAL;
3218
3219 info->func_info_rec_size = sizeof(struct bpf_func_info);
3220 info->line_info_rec_size = sizeof(struct bpf_line_info);
3221 info->jited_line_info_rec_size = sizeof(__u64);
3222
3223 return 0;
3224}
3225
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003226static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
3227 const union bpf_attr *attr,
3228 union bpf_attr __user *uattr)
3229{
3230 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003231 struct bpf_prog_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003232 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003233 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003234 char __user *uinsns;
3235 u32 ulen;
3236 int err;
3237
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003238 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003239 if (err)
3240 return err;
3241 info_len = min_t(u32, sizeof(info), info_len);
3242
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003243 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003244 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02003245 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003246
3247 info.type = prog->type;
3248 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003249 info.load_time = prog->aux->load_time;
3250 info.created_by_uid = from_kuid_munged(current_user_ns(),
3251 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02003252 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003253
3254 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003255 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3256
3257 ulen = info.nr_map_ids;
3258 info.nr_map_ids = prog->aux->used_map_cnt;
3259 ulen = min_t(u32, info.nr_map_ids, ulen);
3260 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07003261 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003262 u32 i;
3263
3264 for (i = 0; i < ulen; i++)
3265 if (put_user(prog->aux->used_maps[i]->id,
3266 &user_map_ids[i]))
3267 return -EFAULT;
3268 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003269
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003270 err = set_info_rec_size(&info);
3271 if (err)
3272 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08003273
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003274 bpf_prog_get_stats(prog, &stats);
3275 info.run_time_ns = stats.nsecs;
3276 info.run_cnt = stats.cnt;
3277
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003278 if (!bpf_capable()) {
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003279 info.jited_prog_len = 0;
3280 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303281 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01003282 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08003283 info.nr_func_info = 0;
3284 info.nr_line_info = 0;
3285 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003286 goto done;
3287 }
3288
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003289 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02003290 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003291 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003292 struct bpf_insn *insns_sanitized;
3293 bool fault;
3294
3295 if (prog->blinded && !bpf_dump_raw_ok()) {
3296 info.xlated_prog_insns = 0;
3297 goto done;
3298 }
3299 insns_sanitized = bpf_insn_prepare_dump(prog);
3300 if (!insns_sanitized)
3301 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003302 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3303 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003304 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3305 kfree(insns_sanitized);
3306 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003307 return -EFAULT;
3308 }
3309
Jakub Kicinski675fc272017-12-27 18:39:09 -08003310 if (bpf_prog_is_dev_bound(prog->aux)) {
3311 err = bpf_prog_offload_info_fill(&info, prog);
3312 if (err)
3313 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08003314 goto done;
3315 }
3316
3317 /* NOTE: the following code is supposed to be skipped for offload.
3318 * bpf_prog_offload_info_fill() is the place to fill similar fields
3319 * for offload.
3320 */
3321 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05303322 if (prog->aux->func_cnt) {
3323 u32 i;
3324
3325 info.jited_prog_len = 0;
3326 for (i = 0; i < prog->aux->func_cnt; i++)
3327 info.jited_prog_len += prog->aux->func[i]->jited_len;
3328 } else {
3329 info.jited_prog_len = prog->jited_len;
3330 }
3331
Jiong Wangfcfb1262018-01-16 16:05:19 -08003332 if (info.jited_prog_len && ulen) {
3333 if (bpf_dump_raw_ok()) {
3334 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3335 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05303336
3337 /* for multi-function programs, copy the JITed
3338 * instructions for all the functions
3339 */
3340 if (prog->aux->func_cnt) {
3341 u32 len, free, i;
3342 u8 *img;
3343
3344 free = ulen;
3345 for (i = 0; i < prog->aux->func_cnt; i++) {
3346 len = prog->aux->func[i]->jited_len;
3347 len = min_t(u32, len, free);
3348 img = (u8 *) prog->aux->func[i]->bpf_func;
3349 if (copy_to_user(uinsns, img, len))
3350 return -EFAULT;
3351 uinsns += len;
3352 free -= len;
3353 if (!free)
3354 break;
3355 }
3356 } else {
3357 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3358 return -EFAULT;
3359 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08003360 } else {
3361 info.jited_prog_insns = 0;
3362 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08003363 }
3364
Sandipan Dasdbecd732018-05-24 12:26:48 +05303365 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07003366 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003367 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05303368 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07003369 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303370 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303371 u32 i;
3372
3373 /* copy the address of the kernel symbol
3374 * corresponding to each function
3375 */
3376 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3377 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07003378 if (prog->aux->func_cnt) {
3379 for (i = 0; i < ulen; i++) {
3380 ksym_addr = (unsigned long)
3381 prog->aux->func[i]->bpf_func;
3382 if (put_user((u64) ksym_addr,
3383 &user_ksyms[i]))
3384 return -EFAULT;
3385 }
3386 } else {
3387 ksym_addr = (unsigned long) prog->bpf_func;
3388 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05303389 return -EFAULT;
3390 }
3391 } else {
3392 info.jited_ksyms = 0;
3393 }
3394 }
3395
Sandipan Das815581c2018-05-24 12:26:52 +05303396 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07003397 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003398 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05303399 if (bpf_dump_raw_ok()) {
3400 u32 __user *user_lens;
3401 u32 func_len, i;
3402
3403 /* copy the JITed image lengths for each function */
3404 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3405 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07003406 if (prog->aux->func_cnt) {
3407 for (i = 0; i < ulen; i++) {
3408 func_len =
3409 prog->aux->func[i]->jited_len;
3410 if (put_user(func_len, &user_lens[i]))
3411 return -EFAULT;
3412 }
3413 } else {
3414 func_len = prog->jited_len;
3415 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05303416 return -EFAULT;
3417 }
3418 } else {
3419 info.jited_func_lens = 0;
3420 }
3421 }
3422
Martin KaFai Lau73372242018-12-05 17:35:43 -08003423 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08003424 info.btf_id = btf_id(prog->aux->btf);
3425
Yonghong Song11d8b822018-12-10 14:14:08 -08003426 ulen = info.nr_func_info;
3427 info.nr_func_info = prog->aux->func_info_cnt;
3428 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003429 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08003430
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003431 user_finfo = u64_to_user_ptr(info.func_info);
3432 ulen = min_t(u32, info.nr_func_info, ulen);
3433 if (copy_to_user(user_finfo, prog->aux->func_info,
3434 info.func_info_rec_size * ulen))
3435 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08003436 }
3437
Yonghong Song11d8b822018-12-10 14:14:08 -08003438 ulen = info.nr_line_info;
3439 info.nr_line_info = prog->aux->nr_linfo;
3440 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003441 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003442
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003443 user_linfo = u64_to_user_ptr(info.line_info);
3444 ulen = min_t(u32, info.nr_line_info, ulen);
3445 if (copy_to_user(user_linfo, prog->aux->linfo,
3446 info.line_info_rec_size * ulen))
3447 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003448 }
3449
Yonghong Song11d8b822018-12-10 14:14:08 -08003450 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003451 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08003452 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003453 else
Yonghong Song11d8b822018-12-10 14:14:08 -08003454 info.nr_jited_line_info = 0;
3455 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003456 if (bpf_dump_raw_ok()) {
3457 __u64 __user *user_linfo;
3458 u32 i;
3459
3460 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08003461 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003462 for (i = 0; i < ulen; i++) {
3463 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3464 &user_linfo[i]))
3465 return -EFAULT;
3466 }
3467 } else {
3468 info.jited_line_info = 0;
3469 }
3470 }
3471
Song Liuc872bdb2018-12-12 09:37:46 -08003472 ulen = info.nr_prog_tags;
3473 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3474 if (ulen) {
3475 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3476 u32 i;
3477
3478 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3479 ulen = min_t(u32, info.nr_prog_tags, ulen);
3480 if (prog->aux->func_cnt) {
3481 for (i = 0; i < ulen; i++) {
3482 if (copy_to_user(user_prog_tags[i],
3483 prog->aux->func[i]->tag,
3484 BPF_TAG_SIZE))
3485 return -EFAULT;
3486 }
3487 } else {
3488 if (copy_to_user(user_prog_tags[0],
3489 prog->tag, BPF_TAG_SIZE))
3490 return -EFAULT;
3491 }
3492 }
3493
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003494done:
3495 if (copy_to_user(uinfo, &info, info_len) ||
3496 put_user(info_len, &uattr->info.info_len))
3497 return -EFAULT;
3498
3499 return 0;
3500}
3501
3502static int bpf_map_get_info_by_fd(struct bpf_map *map,
3503 const union bpf_attr *attr,
3504 union bpf_attr __user *uattr)
3505{
3506 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003507 struct bpf_map_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003508 u32 info_len = attr->info.info_len;
3509 int err;
3510
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003511 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003512 if (err)
3513 return err;
3514 info_len = min_t(u32, sizeof(info), info_len);
3515
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003516 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003517 info.type = map->map_type;
3518 info.id = map->id;
3519 info.key_size = map->key_size;
3520 info.value_size = map->value_size;
3521 info.max_entries = map->max_entries;
3522 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07003523 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003524
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003525 if (map->btf) {
3526 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07003527 info.btf_key_type_id = map->btf_key_type_id;
3528 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003529 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08003530 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003531
Jakub Kicinski52775b32018-01-17 19:13:28 -08003532 if (bpf_map_is_dev_bound(map)) {
3533 err = bpf_map_offload_info_fill(&info, map);
3534 if (err)
3535 return err;
3536 }
3537
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003538 if (copy_to_user(uinfo, &info, info_len) ||
3539 put_user(info_len, &uattr->info.info_len))
3540 return -EFAULT;
3541
3542 return 0;
3543}
3544
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003545static int bpf_btf_get_info_by_fd(struct btf *btf,
3546 const union bpf_attr *attr,
3547 union bpf_attr __user *uattr)
3548{
3549 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3550 u32 info_len = attr->info.info_len;
3551 int err;
3552
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003553 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003554 if (err)
3555 return err;
3556
3557 return btf_get_info_by_fd(btf, attr, uattr);
3558}
3559
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003560static int bpf_link_get_info_by_fd(struct bpf_link *link,
3561 const union bpf_attr *attr,
3562 union bpf_attr __user *uattr)
3563{
3564 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3565 struct bpf_link_info info;
3566 u32 info_len = attr->info.info_len;
3567 int err;
3568
3569 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3570 if (err)
3571 return err;
3572 info_len = min_t(u32, sizeof(info), info_len);
3573
3574 memset(&info, 0, sizeof(info));
3575 if (copy_from_user(&info, uinfo, info_len))
3576 return -EFAULT;
3577
3578 info.type = link->type;
3579 info.id = link->id;
3580 info.prog_id = link->prog->aux->id;
3581
3582 if (link->ops->fill_link_info) {
3583 err = link->ops->fill_link_info(link, &info);
3584 if (err)
3585 return err;
3586 }
3587
3588 if (copy_to_user(uinfo, &info, info_len) ||
3589 put_user(info_len, &uattr->info.info_len))
3590 return -EFAULT;
3591
3592 return 0;
3593}
3594
3595
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003596#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3597
3598static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3599 union bpf_attr __user *uattr)
3600{
3601 int ufd = attr->info.bpf_fd;
3602 struct fd f;
3603 int err;
3604
3605 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3606 return -EINVAL;
3607
3608 f = fdget(ufd);
3609 if (!f.file)
3610 return -EBADFD;
3611
3612 if (f.file->f_op == &bpf_prog_fops)
3613 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
3614 uattr);
3615 else if (f.file->f_op == &bpf_map_fops)
3616 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
3617 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07003618 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003619 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003620 else if (f.file->f_op == &bpf_link_fops)
3621 err = bpf_link_get_info_by_fd(f.file->private_data,
3622 attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003623 else
3624 err = -EINVAL;
3625
3626 fdput(f);
3627 return err;
3628}
3629
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003630#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3631
3632static int bpf_btf_load(const union bpf_attr *attr)
3633{
3634 if (CHECK_ATTR(BPF_BTF_LOAD))
3635 return -EINVAL;
3636
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003637 if (!bpf_capable())
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003638 return -EPERM;
3639
3640 return btf_new_fd(attr);
3641}
3642
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003643#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3644
3645static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3646{
3647 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3648 return -EINVAL;
3649
3650 if (!capable(CAP_SYS_ADMIN))
3651 return -EPERM;
3652
3653 return btf_get_fd_by_id(attr->btf_id);
3654}
3655
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003656static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3657 union bpf_attr __user *uattr,
3658 u32 prog_id, u32 fd_type,
3659 const char *buf, u64 probe_offset,
3660 u64 probe_addr)
3661{
3662 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3663 u32 len = buf ? strlen(buf) : 0, input_len;
3664 int err = 0;
3665
3666 if (put_user(len, &uattr->task_fd_query.buf_len))
3667 return -EFAULT;
3668 input_len = attr->task_fd_query.buf_len;
3669 if (input_len && ubuf) {
3670 if (!len) {
3671 /* nothing to copy, just make ubuf NULL terminated */
3672 char zero = '\0';
3673
3674 if (put_user(zero, ubuf))
3675 return -EFAULT;
3676 } else if (input_len >= len + 1) {
3677 /* ubuf can hold the string with NULL terminator */
3678 if (copy_to_user(ubuf, buf, len + 1))
3679 return -EFAULT;
3680 } else {
3681 /* ubuf cannot hold the string with NULL terminator,
3682 * do a partial copy with NULL terminator.
3683 */
3684 char zero = '\0';
3685
3686 err = -ENOSPC;
3687 if (copy_to_user(ubuf, buf, input_len - 1))
3688 return -EFAULT;
3689 if (put_user(zero, ubuf + input_len - 1))
3690 return -EFAULT;
3691 }
3692 }
3693
3694 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3695 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3696 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3697 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3698 return -EFAULT;
3699
3700 return err;
3701}
3702
3703#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3704
3705static int bpf_task_fd_query(const union bpf_attr *attr,
3706 union bpf_attr __user *uattr)
3707{
3708 pid_t pid = attr->task_fd_query.pid;
3709 u32 fd = attr->task_fd_query.fd;
3710 const struct perf_event *event;
3711 struct files_struct *files;
3712 struct task_struct *task;
3713 struct file *file;
3714 int err;
3715
3716 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3717 return -EINVAL;
3718
3719 if (!capable(CAP_SYS_ADMIN))
3720 return -EPERM;
3721
3722 if (attr->task_fd_query.flags != 0)
3723 return -EINVAL;
3724
3725 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3726 if (!task)
3727 return -ENOENT;
3728
3729 files = get_files_struct(task);
3730 put_task_struct(task);
3731 if (!files)
3732 return -ENOENT;
3733
3734 err = 0;
3735 spin_lock(&files->file_lock);
3736 file = fcheck_files(files, fd);
3737 if (!file)
3738 err = -EBADF;
3739 else
3740 get_file(file);
3741 spin_unlock(&files->file_lock);
3742 put_files_struct(files);
3743
3744 if (err)
3745 goto out;
3746
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003747 if (file->f_op == &bpf_link_fops) {
3748 struct bpf_link *link = file->private_data;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003749
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003750 if (link->ops == &bpf_raw_tp_link_lops) {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003751 struct bpf_raw_tp_link *raw_tp =
3752 container_of(link, struct bpf_raw_tp_link, link);
3753 struct bpf_raw_event_map *btp = raw_tp->btp;
3754
3755 err = bpf_task_fd_query_copy(attr, uattr,
3756 raw_tp->link.prog->aux->id,
3757 BPF_FD_TYPE_RAW_TRACEPOINT,
3758 btp->tp->name, 0, 0);
3759 goto put_file;
3760 }
3761 goto out_not_supp;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003762 }
3763
3764 event = perf_get_event(file);
3765 if (!IS_ERR(event)) {
3766 u64 probe_offset, probe_addr;
3767 u32 prog_id, fd_type;
3768 const char *buf;
3769
3770 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3771 &buf, &probe_offset,
3772 &probe_addr);
3773 if (!err)
3774 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3775 fd_type, buf,
3776 probe_offset,
3777 probe_addr);
3778 goto put_file;
3779 }
3780
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003781out_not_supp:
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003782 err = -ENOTSUPP;
3783put_file:
3784 fput(file);
3785out:
3786 return err;
3787}
3788
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003789#define BPF_MAP_BATCH_LAST_FIELD batch.flags
3790
3791#define BPF_DO_BATCH(fn) \
3792 do { \
3793 if (!fn) { \
3794 err = -ENOTSUPP; \
3795 goto err_put; \
3796 } \
3797 err = fn(map, attr, uattr); \
3798 } while (0)
3799
3800static int bpf_map_do_batch(const union bpf_attr *attr,
3801 union bpf_attr __user *uattr,
3802 int cmd)
3803{
3804 struct bpf_map *map;
3805 int err, ufd;
3806 struct fd f;
3807
3808 if (CHECK_ATTR(BPF_MAP_BATCH))
3809 return -EINVAL;
3810
3811 ufd = attr->batch.map_fd;
3812 f = fdget(ufd);
3813 map = __bpf_map_get(f);
3814 if (IS_ERR(map))
3815 return PTR_ERR(map);
3816
Yonghong Song05799632020-01-15 10:43:04 -08003817 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3818 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003819 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3820 err = -EPERM;
3821 goto err_put;
3822 }
3823
3824 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3825 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3826 err = -EPERM;
3827 goto err_put;
3828 }
3829
3830 if (cmd == BPF_MAP_LOOKUP_BATCH)
3831 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08003832 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
3833 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08003834 else if (cmd == BPF_MAP_UPDATE_BATCH)
3835 BPF_DO_BATCH(map->ops->map_update_batch);
3836 else
3837 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003838
3839err_put:
3840 fdput(f);
3841 return err;
3842}
3843
Yonghong Songde4e05c2020-05-09 10:59:01 -07003844static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3845{
3846 if (attr->link_create.attach_type == BPF_TRACE_ITER &&
3847 prog->expected_attach_type == BPF_TRACE_ITER)
3848 return bpf_iter_link_attach(attr, prog);
3849
3850 return -EINVAL;
3851}
3852
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07003853#define BPF_LINK_CREATE_LAST_FIELD link_create.flags
3854static int link_create(union bpf_attr *attr)
3855{
3856 enum bpf_prog_type ptype;
3857 struct bpf_prog *prog;
3858 int ret;
3859
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07003860 if (CHECK_ATTR(BPF_LINK_CREATE))
3861 return -EINVAL;
3862
3863 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
3864 if (ptype == BPF_PROG_TYPE_UNSPEC)
3865 return -EINVAL;
3866
3867 prog = bpf_prog_get_type(attr->link_create.prog_fd, ptype);
3868 if (IS_ERR(prog))
3869 return PTR_ERR(prog);
3870
3871 ret = bpf_prog_attach_check_attach_type(prog,
3872 attr->link_create.attach_type);
3873 if (ret)
3874 goto err_out;
3875
3876 switch (ptype) {
3877 case BPF_PROG_TYPE_CGROUP_SKB:
3878 case BPF_PROG_TYPE_CGROUP_SOCK:
3879 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3880 case BPF_PROG_TYPE_SOCK_OPS:
3881 case BPF_PROG_TYPE_CGROUP_DEVICE:
3882 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3883 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3884 ret = cgroup_bpf_link_attach(attr, prog);
3885 break;
Yonghong Songde4e05c2020-05-09 10:59:01 -07003886 case BPF_PROG_TYPE_TRACING:
3887 ret = tracing_bpf_link_attach(attr, prog);
3888 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07003889 default:
3890 ret = -EINVAL;
3891 }
3892
3893err_out:
3894 if (ret < 0)
3895 bpf_prog_put(prog);
3896 return ret;
3897}
3898
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003899#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
3900
3901static int link_update(union bpf_attr *attr)
3902{
3903 struct bpf_prog *old_prog = NULL, *new_prog;
3904 struct bpf_link *link;
3905 u32 flags;
3906 int ret;
3907
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003908 if (CHECK_ATTR(BPF_LINK_UPDATE))
3909 return -EINVAL;
3910
3911 flags = attr->link_update.flags;
3912 if (flags & ~BPF_F_REPLACE)
3913 return -EINVAL;
3914
3915 link = bpf_link_get_from_fd(attr->link_update.link_fd);
3916 if (IS_ERR(link))
3917 return PTR_ERR(link);
3918
3919 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07003920 if (IS_ERR(new_prog)) {
3921 ret = PTR_ERR(new_prog);
3922 goto out_put_link;
3923 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003924
3925 if (flags & BPF_F_REPLACE) {
3926 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
3927 if (IS_ERR(old_prog)) {
3928 ret = PTR_ERR(old_prog);
3929 old_prog = NULL;
3930 goto out_put_progs;
3931 }
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07003932 } else if (attr->link_update.old_prog_fd) {
3933 ret = -EINVAL;
3934 goto out_put_progs;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003935 }
3936
Andrii Nakryikof9d04122020-04-28 17:16:05 -07003937 if (link->ops->update_prog)
3938 ret = link->ops->update_prog(link, new_prog, old_prog);
3939 else
Jakub Sitnickife537392020-05-25 14:29:28 +02003940 ret = -EINVAL;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003941
3942out_put_progs:
3943 if (old_prog)
3944 bpf_prog_put(old_prog);
3945 if (ret)
3946 bpf_prog_put(new_prog);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07003947out_put_link:
3948 bpf_link_put(link);
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003949 return ret;
3950}
3951
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07003952static int bpf_link_inc_not_zero(struct bpf_link *link)
3953{
3954 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? 0 : -ENOENT;
3955}
3956
3957#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
3958
3959static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
3960{
3961 struct bpf_link *link;
3962 u32 id = attr->link_id;
3963 int fd, err;
3964
3965 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
3966 return -EINVAL;
3967
3968 if (!capable(CAP_SYS_ADMIN))
3969 return -EPERM;
3970
3971 spin_lock_bh(&link_idr_lock);
3972 link = idr_find(&link_idr, id);
3973 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
3974 if (link) {
3975 if (link->id)
3976 err = bpf_link_inc_not_zero(link);
3977 else
3978 err = -EAGAIN;
3979 } else {
3980 err = -ENOENT;
3981 }
3982 spin_unlock_bh(&link_idr_lock);
3983
3984 if (err)
3985 return err;
3986
3987 fd = bpf_link_new_fd(link);
3988 if (fd < 0)
3989 bpf_link_put(link);
3990
3991 return fd;
3992}
3993
Song Liud46edd62020-04-30 00:15:04 -07003994DEFINE_MUTEX(bpf_stats_enabled_mutex);
3995
3996static int bpf_stats_release(struct inode *inode, struct file *file)
3997{
3998 mutex_lock(&bpf_stats_enabled_mutex);
3999 static_key_slow_dec(&bpf_stats_enabled_key.key);
4000 mutex_unlock(&bpf_stats_enabled_mutex);
4001 return 0;
4002}
4003
4004static const struct file_operations bpf_stats_fops = {
4005 .release = bpf_stats_release,
4006};
4007
4008static int bpf_enable_runtime_stats(void)
4009{
4010 int fd;
4011
4012 mutex_lock(&bpf_stats_enabled_mutex);
4013
4014 /* Set a very high limit to avoid overflow */
4015 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4016 mutex_unlock(&bpf_stats_enabled_mutex);
4017 return -EBUSY;
4018 }
4019
4020 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4021 if (fd >= 0)
4022 static_key_slow_inc(&bpf_stats_enabled_key.key);
4023
4024 mutex_unlock(&bpf_stats_enabled_mutex);
4025 return fd;
4026}
4027
4028#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4029
4030static int bpf_enable_stats(union bpf_attr *attr)
4031{
4032
4033 if (CHECK_ATTR(BPF_ENABLE_STATS))
4034 return -EINVAL;
4035
4036 if (!capable(CAP_SYS_ADMIN))
4037 return -EPERM;
4038
4039 switch (attr->enable_stats.type) {
4040 case BPF_STATS_RUN_TIME:
4041 return bpf_enable_runtime_stats();
4042 default:
4043 break;
4044 }
4045 return -EINVAL;
4046}
4047
Yonghong Songac51d992020-05-09 10:59:05 -07004048#define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4049
4050static int bpf_iter_create(union bpf_attr *attr)
4051{
4052 struct bpf_link *link;
4053 int err;
4054
4055 if (CHECK_ATTR(BPF_ITER_CREATE))
4056 return -EINVAL;
4057
4058 if (attr->iter_create.flags)
4059 return -EINVAL;
4060
4061 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4062 if (IS_ERR(link))
4063 return PTR_ERR(link);
4064
4065 err = bpf_iter_new_fd(link);
4066 bpf_link_put(link);
4067
4068 return err;
4069}
4070
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004071SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4072{
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004073 union bpf_attr attr;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004074 int err;
4075
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07004076 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004077 return -EPERM;
4078
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07004079 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004080 if (err)
4081 return err;
4082 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004083
4084 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004085 memset(&attr, 0, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004086 if (copy_from_user(&attr, uattr, size) != 0)
4087 return -EFAULT;
4088
Chenbo Fengafdb09c2017-10-18 13:00:24 -07004089 err = security_bpf(cmd, &attr, size);
4090 if (err < 0)
4091 return err;
4092
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004093 switch (cmd) {
4094 case BPF_MAP_CREATE:
4095 err = map_create(&attr);
4096 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004097 case BPF_MAP_LOOKUP_ELEM:
4098 err = map_lookup_elem(&attr);
4099 break;
4100 case BPF_MAP_UPDATE_ELEM:
4101 err = map_update_elem(&attr);
4102 break;
4103 case BPF_MAP_DELETE_ELEM:
4104 err = map_delete_elem(&attr);
4105 break;
4106 case BPF_MAP_GET_NEXT_KEY:
4107 err = map_get_next_key(&attr);
4108 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02004109 case BPF_MAP_FREEZE:
4110 err = map_freeze(&attr);
4111 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004112 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08004113 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004114 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01004115 case BPF_OBJ_PIN:
4116 err = bpf_obj_pin(&attr);
4117 break;
4118 case BPF_OBJ_GET:
4119 err = bpf_obj_get(&attr);
4120 break;
Daniel Mackf4324552016-11-23 16:52:27 +01004121 case BPF_PROG_ATTACH:
4122 err = bpf_prog_attach(&attr);
4123 break;
4124 case BPF_PROG_DETACH:
4125 err = bpf_prog_detach(&attr);
4126 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004127 case BPF_PROG_QUERY:
4128 err = bpf_prog_query(&attr, uattr);
4129 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004130 case BPF_PROG_TEST_RUN:
4131 err = bpf_prog_test_run(&attr, uattr);
4132 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004133 case BPF_PROG_GET_NEXT_ID:
4134 err = bpf_obj_get_next_id(&attr, uattr,
4135 &prog_idr, &prog_idr_lock);
4136 break;
4137 case BPF_MAP_GET_NEXT_ID:
4138 err = bpf_obj_get_next_id(&attr, uattr,
4139 &map_idr, &map_idr_lock);
4140 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004141 case BPF_BTF_GET_NEXT_ID:
4142 err = bpf_obj_get_next_id(&attr, uattr,
4143 &btf_idr, &btf_idr_lock);
4144 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07004145 case BPF_PROG_GET_FD_BY_ID:
4146 err = bpf_prog_get_fd_by_id(&attr);
4147 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07004148 case BPF_MAP_GET_FD_BY_ID:
4149 err = bpf_map_get_fd_by_id(&attr);
4150 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004151 case BPF_OBJ_GET_INFO_BY_FD:
4152 err = bpf_obj_get_info_by_fd(&attr, uattr);
4153 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07004154 case BPF_RAW_TRACEPOINT_OPEN:
4155 err = bpf_raw_tracepoint_open(&attr);
4156 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004157 case BPF_BTF_LOAD:
4158 err = bpf_btf_load(&attr);
4159 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004160 case BPF_BTF_GET_FD_BY_ID:
4161 err = bpf_btf_get_fd_by_id(&attr);
4162 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004163 case BPF_TASK_FD_QUERY:
4164 err = bpf_task_fd_query(&attr, uattr);
4165 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02004166 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4167 err = map_lookup_and_delete_elem(&attr);
4168 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004169 case BPF_MAP_LOOKUP_BATCH:
4170 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4171 break;
Yonghong Song05799632020-01-15 10:43:04 -08004172 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4173 err = bpf_map_do_batch(&attr, uattr,
4174 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4175 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004176 case BPF_MAP_UPDATE_BATCH:
4177 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4178 break;
4179 case BPF_MAP_DELETE_BATCH:
4180 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4181 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004182 case BPF_LINK_CREATE:
4183 err = link_create(&attr);
4184 break;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004185 case BPF_LINK_UPDATE:
4186 err = link_update(&attr);
4187 break;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004188 case BPF_LINK_GET_FD_BY_ID:
4189 err = bpf_link_get_fd_by_id(&attr);
4190 break;
4191 case BPF_LINK_GET_NEXT_ID:
4192 err = bpf_obj_get_next_id(&attr, uattr,
4193 &link_idr, &link_idr_lock);
4194 break;
Song Liud46edd62020-04-30 00:15:04 -07004195 case BPF_ENABLE_STATS:
4196 err = bpf_enable_stats(&attr);
4197 break;
Yonghong Songac51d992020-05-09 10:59:05 -07004198 case BPF_ITER_CREATE:
4199 err = bpf_iter_create(&attr);
4200 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004201 default:
4202 err = -EINVAL;
4203 break;
4204 }
4205
4206 return err;
4207}