blob: f5358e1462eb6ba5d2adf345855c20effdd773a3 [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>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070029
Daniel Borkmannda765a22019-11-22 21:07:58 +010030#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
33#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070034#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
Daniel Borkmannda765a22019-11-22 21:07:58 +010035#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
36 IS_FD_HASH(map))
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070037
Chenbo Feng6e71b042017-10-18 13:00:22 -070038#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
39
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080040DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070041static DEFINE_IDR(prog_idr);
42static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070043static DEFINE_IDR(map_idr);
44static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080045
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070046int sysctl_unprivileged_bpf_disabled __read_mostly;
47
Johannes Berg40077e02017-04-11 15:34:58 +020048static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080049#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020050#define BPF_MAP_TYPE(_id, _ops) \
51 [_id] = &_ops,
52#include <linux/bpf_types.h>
53#undef BPF_PROG_TYPE
54#undef BPF_MAP_TYPE
55};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070056
Mickaël Salaün752ba562017-08-07 20:45:20 +020057/*
58 * If we're handed a bigger struct than we know of, ensure all the unknown bits
59 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
60 * we don't know about yet.
61 *
62 * There is a ToCToU between this function call and the following
63 * copy_from_user() call. However, this is not a concern since this function is
64 * meant to be a future-proofing of bits.
65 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070066int bpf_check_uarg_tail_zero(void __user *uaddr,
67 size_t expected_size,
68 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020069{
70 unsigned char __user *addr;
71 unsigned char __user *end;
72 unsigned char val;
73 int err;
74
Mickaël Salaün752ba562017-08-07 20:45:20 +020075 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
76 return -E2BIG;
77
Linus Torvalds96d4f262019-01-03 18:57:57 -080078 if (unlikely(!access_ok(uaddr, actual_size)))
Mickaël Salaün752ba562017-08-07 20:45:20 +020079 return -EFAULT;
80
Mickaël Salaün58291a72017-08-07 20:45:19 +020081 if (actual_size <= expected_size)
82 return 0;
83
84 addr = uaddr + expected_size;
85 end = uaddr + actual_size;
86
87 for (; addr < end; addr++) {
88 err = get_user(val, addr);
89 if (err)
90 return err;
91 if (val)
92 return -E2BIG;
93 }
94
95 return 0;
96}
97
Jakub Kicinskia3884572018-01-11 20:29:09 -080098const struct bpf_map_ops bpf_map_offload_ops = {
99 .map_alloc = bpf_map_offload_map_alloc,
100 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200101 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800102};
103
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700104static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
105{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800106 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100107 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700108 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800109 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700110
Mark Rutland9ef09e32018-05-03 17:04:59 +0100111 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800112 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100113 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
114 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800115 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200116 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700117
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800118 if (ops->map_alloc_check) {
119 err = ops->map_alloc_check(attr);
120 if (err)
121 return ERR_PTR(err);
122 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800123 if (attr->map_ifindex)
124 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800125 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200126 if (IS_ERR(map))
127 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800128 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100129 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200130 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700131}
132
Brian Vazquez15c14a32020-01-15 10:43:00 -0800133static u32 bpf_map_value_size(struct bpf_map *map)
134{
135 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
136 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
137 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
138 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
139 return round_up(map->value_size, 8) * num_possible_cpus();
140 else if (IS_FD_MAP(map))
141 return sizeof(u32);
142 else
143 return map->value_size;
144}
145
146static void maybe_wait_bpf_programs(struct bpf_map *map)
147{
148 /* Wait for any running BPF programs to complete so that
149 * userspace, when we return to it, knows that all programs
150 * that could be running use the new map value.
151 */
152 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
153 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
154 synchronize_rcu();
155}
156
157static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
158 void *value, __u64 flags)
159{
160 int err;
161
162 /* Need to create a kthread, thus must support schedule */
163 if (bpf_map_is_dev_bound(map)) {
164 return bpf_map_offload_update_elem(map, key, value, flags);
165 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
166 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
167 map->map_type == BPF_MAP_TYPE_SOCKMAP ||
168 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
169 return map->ops->map_update_elem(map, key, value, flags);
170 } else if (IS_FD_PROG_ARRAY(map)) {
171 return bpf_fd_array_map_update_elem(map, f.file, key, value,
172 flags);
173 }
174
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100175 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800176 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
177 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
178 err = bpf_percpu_hash_update(map, key, value, flags);
179 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
180 err = bpf_percpu_array_update(map, key, value, flags);
181 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
182 err = bpf_percpu_cgroup_storage_update(map, key, value,
183 flags);
184 } else if (IS_FD_ARRAY(map)) {
185 rcu_read_lock();
186 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
187 flags);
188 rcu_read_unlock();
189 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
190 rcu_read_lock();
191 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
192 flags);
193 rcu_read_unlock();
194 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
195 /* rcu_read_lock() is not needed */
196 err = bpf_fd_reuseport_array_update_elem(map, key, value,
197 flags);
198 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
199 map->map_type == BPF_MAP_TYPE_STACK) {
200 err = map->ops->map_push_elem(map, value, flags);
201 } else {
202 rcu_read_lock();
203 err = map->ops->map_update_elem(map, key, value, flags);
204 rcu_read_unlock();
205 }
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100206 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800207 maybe_wait_bpf_programs(map);
208
209 return err;
210}
211
212static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
213 __u64 flags)
214{
215 void *ptr;
216 int err;
217
Brian Vazquezcb4d03a2020-01-15 10:43:01 -0800218 if (bpf_map_is_dev_bound(map))
219 return bpf_map_offload_lookup_elem(map, key, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800220
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100221 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800222 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
223 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
224 err = bpf_percpu_hash_copy(map, key, value);
225 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
226 err = bpf_percpu_array_copy(map, key, value);
227 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
228 err = bpf_percpu_cgroup_storage_copy(map, key, value);
229 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
230 err = bpf_stackmap_copy(map, key, value);
231 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
232 err = bpf_fd_array_map_lookup_elem(map, key, value);
233 } else if (IS_FD_HASH(map)) {
234 err = bpf_fd_htab_map_lookup_elem(map, key, value);
235 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
236 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
237 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
238 map->map_type == BPF_MAP_TYPE_STACK) {
239 err = map->ops->map_peek_elem(map, value);
240 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
241 /* struct_ops map requires directly updating "value" */
242 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
243 } else {
244 rcu_read_lock();
245 if (map->ops->map_lookup_elem_sys_only)
246 ptr = map->ops->map_lookup_elem_sys_only(map, key);
247 else
248 ptr = map->ops->map_lookup_elem(map, key);
249 if (IS_ERR(ptr)) {
250 err = PTR_ERR(ptr);
251 } else if (!ptr) {
252 err = -ENOENT;
253 } else {
254 err = 0;
255 if (flags & BPF_F_LOCK)
256 /* lock 'ptr' and copy everything but lock */
257 copy_map_value_locked(map, value, ptr, true);
258 else
259 copy_map_value(map, value, ptr);
260 /* mask lock, since value wasn't zero inited */
261 check_and_init_map_lock(map, value);
262 }
263 rcu_read_unlock();
264 }
265
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100266 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800267 maybe_wait_bpf_programs(map);
268
269 return err;
270}
271
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100272static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100273{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100274 /* We really just want to fail instead of triggering OOM killer
275 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
276 * which is used for lower order allocation requests.
277 *
278 * It has been observed that higher order allocation requests done by
279 * vmalloc with __GFP_NORETRY being set might fail due to not trying
280 * to reclaim memory from the page cache, thus we set
281 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100282 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100283
284 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100285 void *area;
286
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100287 if (size >= SIZE_MAX)
288 return NULL;
289
Andrii Nakryikofc970222019-11-17 09:28:04 -0800290 /* kmalloc()'ed memory can't be mmap()'ed */
291 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100292 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
293 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100294 if (area != NULL)
295 return area;
296 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800297 if (mmapable) {
298 BUG_ON(!PAGE_ALIGNED(size));
299 return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
300 __GFP_RETRY_MAYFAIL | flags);
301 }
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100302 return __vmalloc_node_flags_caller(size, numa_node,
303 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
304 flags, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100305}
306
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100307void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800308{
309 return __bpf_map_area_alloc(size, numa_node, false);
310}
311
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100312void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800313{
314 return __bpf_map_area_alloc(size, numa_node, true);
315}
316
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100317void bpf_map_area_free(void *area)
318{
319 kvfree(area);
320}
321
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200322static u32 bpf_map_flags_retain_permanent(u32 flags)
323{
324 /* Some map creation flags are not tied to the map object but
325 * rather to the map fd instead, so they have no meaning upon
326 * map object inspection since multiple file descriptors with
327 * different (access) properties can exist here. Thus, given
328 * this has zero meaning for the map itself, lets clear these
329 * from here.
330 */
331 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
332}
333
Jakub Kicinskibd475642018-01-11 20:29:06 -0800334void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
335{
336 map->map_type = attr->map_type;
337 map->key_size = attr->key_size;
338 map->value_size = attr->value_size;
339 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200340 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800341 map->numa_node = bpf_map_attr_numa_node(attr);
342}
343
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700344static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700345{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700346 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700347
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700348 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
349 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700350 return -EPERM;
351 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700352 return 0;
353}
354
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700355static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
356{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700357 if (user)
358 atomic_long_sub(pages, &user->locked_vm);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700359}
360
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100361int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700362{
Roman Gushchinc85d6912019-05-29 18:03:59 -0700363 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
364 struct user_struct *user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700365 int ret;
366
Roman Gushchinc85d6912019-05-29 18:03:59 -0700367 if (size >= U32_MAX - PAGE_SIZE)
368 return -E2BIG;
369
370 user = get_current_user();
Roman Gushchinb936ca62019-05-29 18:03:58 -0700371 ret = bpf_charge_memlock(user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700372 if (ret) {
373 free_uid(user);
374 return ret;
375 }
Roman Gushchinb936ca62019-05-29 18:03:58 -0700376
377 mem->pages = pages;
378 mem->user = user;
379
380 return 0;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700381}
382
Roman Gushchinb936ca62019-05-29 18:03:58 -0700383void bpf_map_charge_finish(struct bpf_map_memory *mem)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700384{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700385 bpf_uncharge_memlock(mem->user, mem->pages);
386 free_uid(mem->user);
387}
Roman Gushchin3539b962019-05-29 18:03:57 -0700388
Roman Gushchinb936ca62019-05-29 18:03:58 -0700389void bpf_map_charge_move(struct bpf_map_memory *dst,
390 struct bpf_map_memory *src)
391{
392 *dst = *src;
393
394 /* Make sure src will not be used for the redundant uncharging. */
395 memset(src, 0, sizeof(struct bpf_map_memory));
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700396}
397
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700398int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
399{
400 int ret;
401
Roman Gushchin3539b962019-05-29 18:03:57 -0700402 ret = bpf_charge_memlock(map->memory.user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700403 if (ret)
404 return ret;
Roman Gushchin3539b962019-05-29 18:03:57 -0700405 map->memory.pages += pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700406 return ret;
407}
408
409void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
410{
Roman Gushchin3539b962019-05-29 18:03:57 -0700411 bpf_uncharge_memlock(map->memory.user, pages);
412 map->memory.pages -= pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700413}
414
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700415static int bpf_map_alloc_id(struct bpf_map *map)
416{
417 int id;
418
Shaohua Lib76354c2018-03-27 11:53:21 -0700419 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700420 spin_lock_bh(&map_idr_lock);
421 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
422 if (id > 0)
423 map->id = id;
424 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700425 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700426
427 if (WARN_ON_ONCE(!id))
428 return -ENOSPC;
429
430 return id > 0 ? 0 : id;
431}
432
Jakub Kicinskia3884572018-01-11 20:29:09 -0800433void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700434{
Eric Dumazet930651a2017-09-19 09:15:59 -0700435 unsigned long flags;
436
Jakub Kicinskia3884572018-01-11 20:29:09 -0800437 /* Offloaded maps are removed from the IDR store when their device
438 * disappears - even if someone holds an fd to them they are unusable,
439 * the memory is gone, all ops will fail; they are simply waiting for
440 * refcnt to drop to be freed.
441 */
442 if (!map->id)
443 return;
444
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700445 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700446 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700447 else
448 __acquire(&map_idr_lock);
449
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700450 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800451 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700452
453 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700454 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700455 else
456 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700457}
458
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700459/* called from workqueue */
460static void bpf_map_free_deferred(struct work_struct *work)
461{
462 struct bpf_map *map = container_of(work, struct bpf_map, work);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700463 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700464
Roman Gushchinb936ca62019-05-29 18:03:58 -0700465 bpf_map_charge_move(&mem, &map->memory);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700466 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700467 /* implementation dependent freeing */
468 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700469 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700470}
471
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100472static void bpf_map_put_uref(struct bpf_map *map)
473{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800474 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700475 if (map->ops->map_release_uref)
476 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100477 }
478}
479
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700480/* decrement map refcnt and schedule it for freeing via workqueue
481 * (unrelying map implementation ops->map_free() might sleep)
482 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700483static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700484{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800485 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700486 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700487 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700488 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700489 INIT_WORK(&map->work, bpf_map_free_deferred);
490 schedule_work(&map->work);
491 }
492}
493
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700494void bpf_map_put(struct bpf_map *map)
495{
496 __bpf_map_put(map, true);
497}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700498EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700499
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100500void bpf_map_put_with_uref(struct bpf_map *map)
501{
502 bpf_map_put_uref(map);
503 bpf_map_put(map);
504}
505
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700506static int bpf_map_release(struct inode *inode, struct file *filp)
507{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200508 struct bpf_map *map = filp->private_data;
509
510 if (map->ops->map_release)
511 map->ops->map_release(map, filp);
512
513 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700514 return 0;
515}
516
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200517static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
518{
519 fmode_t mode = f.file->f_mode;
520
521 /* Our file permissions may have been overridden by global
522 * map permissions facing syscall side.
523 */
524 if (READ_ONCE(map->frozen))
525 mode &= ~FMODE_CAN_WRITE;
526 return mode;
527}
528
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100529#ifdef CONFIG_PROC_FS
530static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
531{
532 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100533 const struct bpf_array *array;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100534 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100535
536 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
537 array = container_of(map, struct bpf_array, map);
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100538 type = array->aux->type;
539 jited = array->aux->jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100540 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100541
542 seq_printf(m,
543 "map_type:\t%u\n"
544 "key_size:\t%u\n"
545 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100546 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100547 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200548 "memlock:\t%llu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200549 "map_id:\t%u\n"
550 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100551 map->map_type,
552 map->key_size,
553 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100554 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100555 map->map_flags,
Roman Gushchin3539b962019-05-29 18:03:57 -0700556 map->memory.pages * 1ULL << PAGE_SHIFT,
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200557 map->id,
558 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100559 if (type) {
560 seq_printf(m, "owner_prog_type:\t%u\n", type);
561 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200562 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100563}
564#endif
565
Chenbo Feng6e71b042017-10-18 13:00:22 -0700566static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
567 loff_t *ppos)
568{
569 /* We need this handler such that alloc_file() enables
570 * f_mode with FMODE_CAN_READ.
571 */
572 return -EINVAL;
573}
574
575static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
576 size_t siz, loff_t *ppos)
577{
578 /* We need this handler such that alloc_file() enables
579 * f_mode with FMODE_CAN_WRITE.
580 */
581 return -EINVAL;
582}
583
Andrii Nakryikofc970222019-11-17 09:28:04 -0800584/* called for any extra memory-mapped regions (except initial) */
585static void bpf_map_mmap_open(struct vm_area_struct *vma)
586{
587 struct bpf_map *map = vma->vm_file->private_data;
588
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700589 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800590 mutex_lock(&map->freeze_mutex);
591 map->writecnt++;
592 mutex_unlock(&map->freeze_mutex);
593 }
594}
595
596/* called for all unmapped memory region (including initial) */
597static void bpf_map_mmap_close(struct vm_area_struct *vma)
598{
599 struct bpf_map *map = vma->vm_file->private_data;
600
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700601 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800602 mutex_lock(&map->freeze_mutex);
603 map->writecnt--;
604 mutex_unlock(&map->freeze_mutex);
605 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800606}
607
608static const struct vm_operations_struct bpf_map_default_vmops = {
609 .open = bpf_map_mmap_open,
610 .close = bpf_map_mmap_close,
611};
612
613static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
614{
615 struct bpf_map *map = filp->private_data;
616 int err;
617
618 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
619 return -ENOTSUPP;
620
621 if (!(vma->vm_flags & VM_SHARED))
622 return -EINVAL;
623
624 mutex_lock(&map->freeze_mutex);
625
626 if ((vma->vm_flags & VM_WRITE) && map->frozen) {
627 err = -EPERM;
628 goto out;
629 }
630
631 /* set default open/close callbacks */
632 vma->vm_ops = &bpf_map_default_vmops;
633 vma->vm_private_data = map;
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700634 vma->vm_flags &= ~VM_MAYEXEC;
635 if (!(vma->vm_flags & VM_WRITE))
636 /* disallow re-mapping with PROT_WRITE */
637 vma->vm_flags &= ~VM_MAYWRITE;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800638
639 err = map->ops->map_mmap(map, vma);
640 if (err)
641 goto out;
642
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700643 if (vma->vm_flags & VM_MAYWRITE)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800644 map->writecnt++;
645out:
646 mutex_unlock(&map->freeze_mutex);
647 return err;
648}
649
Chenbo Fengf66e4482017-10-18 13:00:26 -0700650const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100651#ifdef CONFIG_PROC_FS
652 .show_fdinfo = bpf_map_show_fdinfo,
653#endif
654 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700655 .read = bpf_dummy_read,
656 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800657 .mmap = bpf_map_mmap,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700658};
659
Chenbo Feng6e71b042017-10-18 13:00:22 -0700660int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100661{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700662 int ret;
663
664 ret = security_bpf_map(map, OPEN_FMODE(flags));
665 if (ret < 0)
666 return ret;
667
Daniel Borkmannaa797812015-10-29 14:58:06 +0100668 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700669 flags | O_CLOEXEC);
670}
671
672int bpf_get_file_flag(int flags)
673{
674 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
675 return -EINVAL;
676 if (flags & BPF_F_RDONLY)
677 return O_RDONLY;
678 if (flags & BPF_F_WRONLY)
679 return O_WRONLY;
680 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100681}
682
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700683/* helper macro to check that unused fields 'union bpf_attr' are zero */
684#define CHECK_ATTR(CMD) \
685 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
686 sizeof(attr->CMD##_LAST_FIELD), 0, \
687 sizeof(*attr) - \
688 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
689 sizeof(attr->CMD##_LAST_FIELD)) != NULL
690
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700691/* dst and src must have at least "size" number of bytes.
692 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700693 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700694int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700695{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700696 const char *end = src + size;
697 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700698
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700699 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200700 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700701 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200702 if (!isalnum(*src) &&
703 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700704 return -EINVAL;
705 *dst++ = *src++;
706 }
707
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700708 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700709 if (src == end)
710 return -EINVAL;
711
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700712 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700713}
714
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200715int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800716 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200717 const struct btf_type *key_type,
718 const struct btf_type *value_type)
719{
720 return -ENOTSUPP;
721}
722
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800723static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200724 u32 btf_key_id, u32 btf_value_id)
725{
726 const struct btf_type *key_type, *value_type;
727 u32 key_size, value_size;
728 int ret = 0;
729
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200730 /* Some maps allow key to be unspecified. */
731 if (btf_key_id) {
732 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
733 if (!key_type || key_size != map->key_size)
734 return -EINVAL;
735 } else {
736 key_type = btf_type_by_id(btf, 0);
737 if (!map->ops->map_check_btf)
738 return -EINVAL;
739 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200740
741 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
742 if (!value_type || value_size != map->value_size)
743 return -EINVAL;
744
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800745 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
746
747 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200748 if (map->map_flags & BPF_F_RDONLY_PROG)
749 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800750 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800751 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700752 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
753 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800754 return -ENOTSUPP;
755 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
756 map->value_size) {
757 WARN_ONCE(1,
758 "verifier bug spin_lock_off %d value_size %d\n",
759 map->spin_lock_off, map->value_size);
760 return -EFAULT;
761 }
762 }
763
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200764 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800765 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200766
767 return ret;
768}
769
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800770#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700771/* called via syscall */
772static int map_create(union bpf_attr *attr)
773{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700774 int numa_node = bpf_map_attr_numa_node(attr);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700775 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700776 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700777 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700778 int err;
779
780 err = CHECK_ATTR(BPF_MAP_CREATE);
781 if (err)
782 return -EINVAL;
783
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800784 if (attr->btf_vmlinux_value_type_id) {
785 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
786 attr->btf_key_type_id || attr->btf_value_type_id)
787 return -EINVAL;
788 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
789 return -EINVAL;
790 }
791
Chenbo Feng6e71b042017-10-18 13:00:22 -0700792 f_flags = bpf_get_file_flag(attr->map_flags);
793 if (f_flags < 0)
794 return f_flags;
795
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700796 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700797 ((unsigned int)numa_node >= nr_node_ids ||
798 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700799 return -EINVAL;
800
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700801 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
802 map = find_and_alloc_map(attr);
803 if (IS_ERR(map))
804 return PTR_ERR(map);
805
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700806 err = bpf_obj_name_cpy(map->name, attr->map_name,
807 sizeof(attr->map_name));
808 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700809 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700810
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800811 atomic64_set(&map->refcnt, 1);
812 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800813 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700814
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800815 map->spin_lock_off = -EINVAL;
816 if (attr->btf_key_type_id || attr->btf_value_type_id ||
817 /* Even the map's value is a kernel's struct,
818 * the bpf_prog.o must have BTF to begin with
819 * to figure out the corresponding kernel's
820 * counter part. Thus, attr->btf_fd has
821 * to be valid also.
822 */
823 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700824 struct btf *btf;
825
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700826 btf = btf_get_by_fd(attr->btf_fd);
827 if (IS_ERR(btf)) {
828 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700829 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700830 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800831 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700832
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800833 if (attr->btf_value_type_id) {
834 err = map_check_btf(map, btf, attr->btf_key_type_id,
835 attr->btf_value_type_id);
836 if (err)
837 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700838 }
839
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700840 map->btf_key_type_id = attr->btf_key_type_id;
841 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800842 map->btf_vmlinux_value_type_id =
843 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700844 }
845
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700846 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700847 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700848 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700849
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700850 err = bpf_map_alloc_id(map);
851 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700852 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700853
Chenbo Feng6e71b042017-10-18 13:00:22 -0700854 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700855 if (err < 0) {
856 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800857 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700858 * bpf_map_alloc_id() has published the map
859 * to the userspace and the userspace may
860 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
861 */
Peng Sun352d20d2019-02-27 22:36:25 +0800862 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700863 return err;
864 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700865
866 return err;
867
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700868free_map_sec:
869 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700870free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700871 btf_put(map->btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700872 bpf_map_charge_move(&mem, &map->memory);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700873 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700874 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700875 return err;
876}
877
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700878/* if error is returned, fd is released.
879 * On success caller should complete fd access with matching fdput()
880 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100881struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700882{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700883 if (!f.file)
884 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700885 if (f.file->f_op != &bpf_map_fops) {
886 fdput(f);
887 return ERR_PTR(-EINVAL);
888 }
889
Daniel Borkmannc2101292015-10-29 14:58:07 +0100890 return f.file->private_data;
891}
892
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800893void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100894{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800895 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100896}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700897EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100898
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800899void bpf_map_inc_with_uref(struct bpf_map *map)
900{
901 atomic64_inc(&map->refcnt);
902 atomic64_inc(&map->usercnt);
903}
904EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
905
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -0800906struct bpf_map *bpf_map_get(u32 ufd)
907{
908 struct fd f = fdget(ufd);
909 struct bpf_map *map;
910
911 map = __bpf_map_get(f);
912 if (IS_ERR(map))
913 return map;
914
915 bpf_map_inc(map);
916 fdput(f);
917
918 return map;
919}
920
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100921struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100922{
923 struct fd f = fdget(ufd);
924 struct bpf_map *map;
925
926 map = __bpf_map_get(f);
927 if (IS_ERR(map))
928 return map;
929
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800930 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100931 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700932
933 return map;
934}
935
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700936/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800937static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700938{
939 int refold;
940
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800941 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700942 if (!refold)
943 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700944 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800945 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700946
947 return map;
948}
949
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800950struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700951{
952 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800953 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700954 spin_unlock_bh(&map_idr_lock);
955
956 return map;
957}
958EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
959
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800960int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
961{
962 return -ENOTSUPP;
963}
964
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200965static void *__bpf_copy_key(void __user *ukey, u64 key_size)
966{
967 if (key_size)
968 return memdup_user(ukey, key_size);
969
970 if (ukey)
971 return ERR_PTR(-EINVAL);
972
973 return NULL;
974}
975
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700976/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800977#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700978
979static int map_lookup_elem(union bpf_attr *attr)
980{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100981 void __user *ukey = u64_to_user_ptr(attr->key);
982 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700983 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700984 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -0800985 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800986 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200987 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700988 int err;
989
990 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
991 return -EINVAL;
992
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800993 if (attr->flags & ~BPF_F_LOCK)
994 return -EINVAL;
995
Daniel Borkmann592867b2015-09-08 18:00:09 +0200996 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100997 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700998 if (IS_ERR(map))
999 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001000 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001001 err = -EPERM;
1002 goto err_put;
1003 }
1004
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001005 if ((attr->flags & BPF_F_LOCK) &&
1006 !map_value_has_spin_lock(map)) {
1007 err = -EINVAL;
1008 goto err_put;
1009 }
1010
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001011 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001012 if (IS_ERR(key)) {
1013 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001014 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001015 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001016
Brian Vazquez15c14a32020-01-15 10:43:00 -08001017 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001018
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001019 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001020 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001021 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001022 goto free_key;
1023
Brian Vazquez15c14a32020-01-15 10:43:00 -08001024 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001025 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001026 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001027
1028 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001029 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001030 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001031
1032 err = 0;
1033
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001034free_value:
1035 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001036free_key:
1037 kfree(key);
1038err_put:
1039 fdput(f);
1040 return err;
1041}
1042
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001043
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001044#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001045
1046static int map_update_elem(union bpf_attr *attr)
1047{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001048 void __user *ukey = u64_to_user_ptr(attr->key);
1049 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001050 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001051 struct bpf_map *map;
1052 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001053 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001054 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001055 int err;
1056
1057 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1058 return -EINVAL;
1059
Daniel Borkmann592867b2015-09-08 18:00:09 +02001060 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001061 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001062 if (IS_ERR(map))
1063 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001064 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001065 err = -EPERM;
1066 goto err_put;
1067 }
1068
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001069 if ((attr->flags & BPF_F_LOCK) &&
1070 !map_value_has_spin_lock(map)) {
1071 err = -EINVAL;
1072 goto err_put;
1073 }
1074
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001075 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001076 if (IS_ERR(key)) {
1077 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001078 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001079 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001080
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001081 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001082 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +00001083 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1084 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001085 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1086 else
1087 value_size = map->value_size;
1088
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001089 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001090 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001091 if (!value)
1092 goto free_key;
1093
1094 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001095 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001096 goto free_value;
1097
Brian Vazquez15c14a32020-01-15 10:43:00 -08001098 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001099
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001100free_value:
1101 kfree(value);
1102free_key:
1103 kfree(key);
1104err_put:
1105 fdput(f);
1106 return err;
1107}
1108
1109#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1110
1111static int map_delete_elem(union bpf_attr *attr)
1112{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001113 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001114 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001115 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001116 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001117 void *key;
1118 int err;
1119
1120 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1121 return -EINVAL;
1122
Daniel Borkmann592867b2015-09-08 18:00:09 +02001123 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001124 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001125 if (IS_ERR(map))
1126 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001127 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001128 err = -EPERM;
1129 goto err_put;
1130 }
1131
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001132 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001133 if (IS_ERR(key)) {
1134 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001135 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001136 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001137
Jakub Kicinskia3884572018-01-11 20:29:09 -08001138 if (bpf_map_is_dev_bound(map)) {
1139 err = bpf_map_offload_delete_elem(map, key);
1140 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001141 } else if (IS_FD_PROG_ARRAY(map) ||
1142 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1143 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001144 err = map->ops->map_delete_elem(map, key);
1145 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001146 }
1147
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001148 bpf_disable_instrumentation();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001149 rcu_read_lock();
1150 err = map->ops->map_delete_elem(map, key);
1151 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001152 bpf_enable_instrumentation();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001153 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001154out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001155 kfree(key);
1156err_put:
1157 fdput(f);
1158 return err;
1159}
1160
1161/* last field in 'union bpf_attr' used by this command */
1162#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1163
1164static int map_get_next_key(union bpf_attr *attr)
1165{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001166 void __user *ukey = u64_to_user_ptr(attr->key);
1167 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001168 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001169 struct bpf_map *map;
1170 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001171 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001172 int err;
1173
1174 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1175 return -EINVAL;
1176
Daniel Borkmann592867b2015-09-08 18:00:09 +02001177 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001178 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001179 if (IS_ERR(map))
1180 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001181 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001182 err = -EPERM;
1183 goto err_put;
1184 }
1185
Teng Qin8fe45922017-04-24 19:00:37 -07001186 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001187 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001188 if (IS_ERR(key)) {
1189 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001190 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001191 }
Teng Qin8fe45922017-04-24 19:00:37 -07001192 } else {
1193 key = NULL;
1194 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001195
1196 err = -ENOMEM;
1197 next_key = kmalloc(map->key_size, GFP_USER);
1198 if (!next_key)
1199 goto free_key;
1200
Jakub Kicinskia3884572018-01-11 20:29:09 -08001201 if (bpf_map_is_dev_bound(map)) {
1202 err = bpf_map_offload_get_next_key(map, key, next_key);
1203 goto out;
1204 }
1205
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001206 rcu_read_lock();
1207 err = map->ops->map_get_next_key(map, key, next_key);
1208 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001209out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001210 if (err)
1211 goto free_next_key;
1212
1213 err = -EFAULT;
1214 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1215 goto free_next_key;
1216
1217 err = 0;
1218
1219free_next_key:
1220 kfree(next_key);
1221free_key:
1222 kfree(key);
1223err_put:
1224 fdput(f);
1225 return err;
1226}
1227
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001228int generic_map_delete_batch(struct bpf_map *map,
1229 const union bpf_attr *attr,
1230 union bpf_attr __user *uattr)
1231{
1232 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1233 u32 cp, max_count;
1234 int err = 0;
1235 void *key;
1236
1237 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1238 return -EINVAL;
1239
1240 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1241 !map_value_has_spin_lock(map)) {
1242 return -EINVAL;
1243 }
1244
1245 max_count = attr->batch.count;
1246 if (!max_count)
1247 return 0;
1248
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001249 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1250 if (!key)
1251 return -ENOMEM;
1252
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001253 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001254 err = -EFAULT;
1255 if (copy_from_user(key, keys + cp * map->key_size,
1256 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001257 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001258
1259 if (bpf_map_is_dev_bound(map)) {
1260 err = bpf_map_offload_delete_elem(map, key);
1261 break;
1262 }
1263
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001264 bpf_disable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001265 rcu_read_lock();
1266 err = map->ops->map_delete_elem(map, key);
1267 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001268 bpf_enable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001269 maybe_wait_bpf_programs(map);
1270 if (err)
1271 break;
1272 }
1273 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1274 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001275
1276 kfree(key);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001277 return err;
1278}
1279
1280int generic_map_update_batch(struct bpf_map *map,
1281 const union bpf_attr *attr,
1282 union bpf_attr __user *uattr)
1283{
1284 void __user *values = u64_to_user_ptr(attr->batch.values);
1285 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1286 u32 value_size, cp, max_count;
1287 int ufd = attr->map_fd;
1288 void *key, *value;
1289 struct fd f;
1290 int err = 0;
1291
1292 f = fdget(ufd);
1293 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1294 return -EINVAL;
1295
1296 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1297 !map_value_has_spin_lock(map)) {
1298 return -EINVAL;
1299 }
1300
1301 value_size = bpf_map_value_size(map);
1302
1303 max_count = attr->batch.count;
1304 if (!max_count)
1305 return 0;
1306
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001307 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1308 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001309 return -ENOMEM;
1310
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001311 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1312 if (!value) {
1313 kfree(key);
1314 return -ENOMEM;
1315 }
1316
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001317 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001318 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001319 if (copy_from_user(key, keys + cp * map->key_size,
1320 map->key_size) ||
1321 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001322 break;
1323
1324 err = bpf_map_update_value(map, f, key, value,
1325 attr->batch.elem_flags);
1326
1327 if (err)
1328 break;
1329 }
1330
1331 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1332 err = -EFAULT;
1333
1334 kfree(value);
1335 kfree(key);
1336 return err;
1337}
1338
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001339#define MAP_LOOKUP_RETRIES 3
1340
1341int generic_map_lookup_batch(struct bpf_map *map,
1342 const union bpf_attr *attr,
1343 union bpf_attr __user *uattr)
1344{
1345 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1346 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1347 void __user *values = u64_to_user_ptr(attr->batch.values);
1348 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1349 void *buf, *buf_prevkey, *prev_key, *key, *value;
1350 int err, retry = MAP_LOOKUP_RETRIES;
1351 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001352
1353 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1354 return -EINVAL;
1355
1356 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1357 !map_value_has_spin_lock(map))
1358 return -EINVAL;
1359
1360 value_size = bpf_map_value_size(map);
1361
1362 max_count = attr->batch.count;
1363 if (!max_count)
1364 return 0;
1365
1366 if (put_user(0, &uattr->batch.count))
1367 return -EFAULT;
1368
1369 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1370 if (!buf_prevkey)
1371 return -ENOMEM;
1372
1373 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1374 if (!buf) {
1375 kvfree(buf_prevkey);
1376 return -ENOMEM;
1377 }
1378
1379 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001380 prev_key = NULL;
1381 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1382 goto free_buf;
1383 key = buf;
1384 value = key + map->key_size;
1385 if (ubatch)
1386 prev_key = buf_prevkey;
1387
1388 for (cp = 0; cp < max_count;) {
1389 rcu_read_lock();
1390 err = map->ops->map_get_next_key(map, prev_key, key);
1391 rcu_read_unlock();
1392 if (err)
1393 break;
1394 err = bpf_map_copy_value(map, key, value,
1395 attr->batch.elem_flags);
1396
1397 if (err == -ENOENT) {
1398 if (retry) {
1399 retry--;
1400 continue;
1401 }
1402 err = -EINTR;
1403 break;
1404 }
1405
1406 if (err)
1407 goto free_buf;
1408
1409 if (copy_to_user(keys + cp * map->key_size, key,
1410 map->key_size)) {
1411 err = -EFAULT;
1412 goto free_buf;
1413 }
1414 if (copy_to_user(values + cp * value_size, value, value_size)) {
1415 err = -EFAULT;
1416 goto free_buf;
1417 }
1418
1419 if (!prev_key)
1420 prev_key = buf_prevkey;
1421
1422 swap(prev_key, key);
1423 retry = MAP_LOOKUP_RETRIES;
1424 cp++;
1425 }
1426
1427 if (err == -EFAULT)
1428 goto free_buf;
1429
1430 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1431 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1432 err = -EFAULT;
1433
1434free_buf:
1435 kfree(buf_prevkey);
1436 kfree(buf);
1437 return err;
1438}
1439
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001440#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1441
1442static int map_lookup_and_delete_elem(union bpf_attr *attr)
1443{
1444 void __user *ukey = u64_to_user_ptr(attr->key);
1445 void __user *uvalue = u64_to_user_ptr(attr->value);
1446 int ufd = attr->map_fd;
1447 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001448 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001449 u32 value_size;
1450 struct fd f;
1451 int err;
1452
1453 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1454 return -EINVAL;
1455
1456 f = fdget(ufd);
1457 map = __bpf_map_get(f);
1458 if (IS_ERR(map))
1459 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001460 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001461 err = -EPERM;
1462 goto err_put;
1463 }
1464
1465 key = __bpf_copy_key(ukey, map->key_size);
1466 if (IS_ERR(key)) {
1467 err = PTR_ERR(key);
1468 goto err_put;
1469 }
1470
1471 value_size = map->value_size;
1472
1473 err = -ENOMEM;
1474 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1475 if (!value)
1476 goto free_key;
1477
1478 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1479 map->map_type == BPF_MAP_TYPE_STACK) {
1480 err = map->ops->map_pop_elem(map, value);
1481 } else {
1482 err = -ENOTSUPP;
1483 }
1484
1485 if (err)
1486 goto free_value;
1487
1488 if (copy_to_user(uvalue, value, value_size) != 0)
1489 goto free_value;
1490
1491 err = 0;
1492
1493free_value:
1494 kfree(value);
1495free_key:
1496 kfree(key);
1497err_put:
1498 fdput(f);
1499 return err;
1500}
1501
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001502#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1503
1504static int map_freeze(const union bpf_attr *attr)
1505{
1506 int err = 0, ufd = attr->map_fd;
1507 struct bpf_map *map;
1508 struct fd f;
1509
1510 if (CHECK_ATTR(BPF_MAP_FREEZE))
1511 return -EINVAL;
1512
1513 f = fdget(ufd);
1514 map = __bpf_map_get(f);
1515 if (IS_ERR(map))
1516 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001517
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001518 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1519 fdput(f);
1520 return -ENOTSUPP;
1521 }
1522
Andrii Nakryikofc970222019-11-17 09:28:04 -08001523 mutex_lock(&map->freeze_mutex);
1524
1525 if (map->writecnt) {
1526 err = -EBUSY;
1527 goto err_put;
1528 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001529 if (READ_ONCE(map->frozen)) {
1530 err = -EBUSY;
1531 goto err_put;
1532 }
1533 if (!capable(CAP_SYS_ADMIN)) {
1534 err = -EPERM;
1535 goto err_put;
1536 }
1537
1538 WRITE_ONCE(map->frozen, true);
1539err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001540 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001541 fdput(f);
1542 return err;
1543}
1544
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001545static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001546#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001547 [_id] = & _name ## _prog_ops,
1548#define BPF_MAP_TYPE(_id, _ops)
1549#include <linux/bpf_types.h>
1550#undef BPF_PROG_TYPE
1551#undef BPF_MAP_TYPE
1552};
1553
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001554static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1555{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001556 const struct bpf_prog_ops *ops;
1557
1558 if (type >= ARRAY_SIZE(bpf_prog_types))
1559 return -EINVAL;
1560 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1561 ops = bpf_prog_types[type];
1562 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001563 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001564
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001565 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001566 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001567 else
1568 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001569 prog->type = type;
1570 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001571}
1572
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001573enum bpf_audit {
1574 BPF_AUDIT_LOAD,
1575 BPF_AUDIT_UNLOAD,
1576 BPF_AUDIT_MAX,
1577};
1578
1579static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1580 [BPF_AUDIT_LOAD] = "LOAD",
1581 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1582};
1583
1584static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1585{
1586 struct audit_context *ctx = NULL;
1587 struct audit_buffer *ab;
1588
1589 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1590 return;
1591 if (audit_enabled == AUDIT_OFF)
1592 return;
1593 if (op == BPF_AUDIT_LOAD)
1594 ctx = audit_context();
1595 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1596 if (unlikely(!ab))
1597 return;
1598 audit_log_format(ab, "prog-id=%u op=%s",
1599 prog->aux->id, bpf_audit_str[op]);
1600 audit_log_end(ab);
1601}
1602
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001603int __bpf_prog_charge(struct user_struct *user, u32 pages)
1604{
1605 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1606 unsigned long user_bufs;
1607
1608 if (user) {
1609 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1610 if (user_bufs > memlock_limit) {
1611 atomic_long_sub(pages, &user->locked_vm);
1612 return -EPERM;
1613 }
1614 }
1615
1616 return 0;
1617}
1618
1619void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1620{
1621 if (user)
1622 atomic_long_sub(pages, &user->locked_vm);
1623}
1624
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001625static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1626{
1627 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001628 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001629
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001630 ret = __bpf_prog_charge(user, prog->pages);
1631 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001632 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001633 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001634 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001635
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001636 prog->aux->user = user;
1637 return 0;
1638}
1639
1640static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1641{
1642 struct user_struct *user = prog->aux->user;
1643
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001644 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001645 free_uid(user);
1646}
1647
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001648static int bpf_prog_alloc_id(struct bpf_prog *prog)
1649{
1650 int id;
1651
Shaohua Lib76354c2018-03-27 11:53:21 -07001652 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001653 spin_lock_bh(&prog_idr_lock);
1654 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1655 if (id > 0)
1656 prog->aux->id = id;
1657 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001658 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001659
1660 /* id is in [1, INT_MAX) */
1661 if (WARN_ON_ONCE(!id))
1662 return -ENOSPC;
1663
1664 return id > 0 ? 0 : id;
1665}
1666
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001667void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001668{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001669 /* cBPF to eBPF migrations are currently not in the idr store.
1670 * Offloaded programs are removed from the store when their device
1671 * disappears - even if someone grabs an fd to them they are unusable,
1672 * simply waiting for refcnt to drop to be freed.
1673 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001674 if (!prog->aux->id)
1675 return;
1676
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001677 if (do_idr_lock)
1678 spin_lock_bh(&prog_idr_lock);
1679 else
1680 __acquire(&prog_idr_lock);
1681
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001682 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001683 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001684
1685 if (do_idr_lock)
1686 spin_unlock_bh(&prog_idr_lock);
1687 else
1688 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001689}
1690
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001691static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001692{
1693 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1694
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001695 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001696 kfree(aux->func_info_aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001697 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001698 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001699 bpf_prog_free(aux->prog);
1700}
1701
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001702static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1703{
1704 bpf_prog_kallsyms_del_all(prog);
1705 btf_put(prog->aux->btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001706 bpf_prog_free_linfo(prog);
1707
1708 if (deferred)
1709 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1710 else
1711 __bpf_prog_put_rcu(&prog->aux->rcu);
1712}
1713
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001714static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001715{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001716 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001717 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001718 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001719 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001720 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001721 __bpf_prog_put_noref(prog, true);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001722 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001723}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001724
1725void bpf_prog_put(struct bpf_prog *prog)
1726{
1727 __bpf_prog_put(prog, true);
1728}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001729EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001730
1731static int bpf_prog_release(struct inode *inode, struct file *filp)
1732{
1733 struct bpf_prog *prog = filp->private_data;
1734
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001735 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001736 return 0;
1737}
1738
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001739static void bpf_prog_get_stats(const struct bpf_prog *prog,
1740 struct bpf_prog_stats *stats)
1741{
1742 u64 nsecs = 0, cnt = 0;
1743 int cpu;
1744
1745 for_each_possible_cpu(cpu) {
1746 const struct bpf_prog_stats *st;
1747 unsigned int start;
1748 u64 tnsecs, tcnt;
1749
1750 st = per_cpu_ptr(prog->aux->stats, cpu);
1751 do {
1752 start = u64_stats_fetch_begin_irq(&st->syncp);
1753 tnsecs = st->nsecs;
1754 tcnt = st->cnt;
1755 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1756 nsecs += tnsecs;
1757 cnt += tcnt;
1758 }
1759 stats->nsecs = nsecs;
1760 stats->cnt = cnt;
1761}
1762
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001763#ifdef CONFIG_PROC_FS
1764static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1765{
1766 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001767 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001768 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001769
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001770 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001771 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001772 seq_printf(m,
1773 "prog_type:\t%u\n"
1774 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001775 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001776 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001777 "prog_id:\t%u\n"
1778 "run_time_ns:\t%llu\n"
1779 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001780 prog->type,
1781 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001782 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001783 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001784 prog->aux->id,
1785 stats.nsecs,
1786 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001787}
1788#endif
1789
Chenbo Fengf66e4482017-10-18 13:00:26 -07001790const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001791#ifdef CONFIG_PROC_FS
1792 .show_fdinfo = bpf_prog_show_fdinfo,
1793#endif
1794 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001795 .read = bpf_dummy_read,
1796 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001797};
1798
Daniel Borkmannb2197752015-10-29 14:58:09 +01001799int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001800{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001801 int ret;
1802
1803 ret = security_bpf_prog(prog);
1804 if (ret < 0)
1805 return ret;
1806
Daniel Borkmannaa797812015-10-29 14:58:06 +01001807 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1808 O_RDWR | O_CLOEXEC);
1809}
1810
Daniel Borkmann113214b2016-06-30 17:24:44 +02001811static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001812{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001813 if (!f.file)
1814 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001815 if (f.file->f_op != &bpf_prog_fops) {
1816 fdput(f);
1817 return ERR_PTR(-EINVAL);
1818 }
1819
Daniel Borkmannc2101292015-10-29 14:58:07 +01001820 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001821}
1822
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001823void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001824{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001825 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001826}
Brenden Blanco59d36562016-07-19 12:16:46 -07001827EXPORT_SYMBOL_GPL(bpf_prog_add);
1828
Daniel Borkmannc5405942016-11-09 22:02:34 +01001829void bpf_prog_sub(struct bpf_prog *prog, int i)
1830{
1831 /* Only to be used for undoing previous bpf_prog_add() in some
1832 * error path. We still know that another entity in our call
1833 * path holds a reference to the program, thus atomic_sub() can
1834 * be safely used in such cases!
1835 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001836 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001837}
1838EXPORT_SYMBOL_GPL(bpf_prog_sub);
1839
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001840void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001841{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001842 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001843}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001844EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001845
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001846/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001847struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001848{
1849 int refold;
1850
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001851 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001852
1853 if (!refold)
1854 return ERR_PTR(-ENOENT);
1855
1856 return prog;
1857}
John Fastabenda6f6df62017-08-15 22:32:22 -07001858EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001859
Al Viro040ee692017-12-02 20:20:38 -05001860bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001861 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001862{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001863 /* not an attachment, just a refcount inc, always allow */
1864 if (!attach_type)
1865 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001866
1867 if (prog->type != *attach_type)
1868 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001869 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001870 return false;
1871
1872 return true;
1873}
1874
1875static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001876 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001877{
1878 struct fd f = fdget(ufd);
1879 struct bpf_prog *prog;
1880
Daniel Borkmann113214b2016-06-30 17:24:44 +02001881 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001882 if (IS_ERR(prog))
1883 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001884 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001885 prog = ERR_PTR(-EINVAL);
1886 goto out;
1887 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001888
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001889 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001890out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001891 fdput(f);
1892 return prog;
1893}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001894
1895struct bpf_prog *bpf_prog_get(u32 ufd)
1896{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001897 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001898}
1899
Jakub Kicinski248f3462017-11-03 13:56:20 -07001900struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001901 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001902{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001903 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001904}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001905EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001906
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001907/* Initially all BPF programs could be loaded w/o specifying
1908 * expected_attach_type. Later for some of them specifying expected_attach_type
1909 * at load time became required so that program could be validated properly.
1910 * Programs of types that are allowed to be loaded both w/ and w/o (for
1911 * backward compatibility) expected_attach_type, should have the default attach
1912 * type assigned to expected_attach_type for the latter case, so that it can be
1913 * validated later at attach time.
1914 *
1915 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1916 * prog type requires it but has some attach types that have to be backward
1917 * compatible.
1918 */
1919static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1920{
1921 switch (attr->prog_type) {
1922 case BPF_PROG_TYPE_CGROUP_SOCK:
1923 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1924 * exist so checking for non-zero is the way to go here.
1925 */
1926 if (!attr->expected_attach_type)
1927 attr->expected_attach_type =
1928 BPF_CGROUP_INET_SOCK_CREATE;
1929 break;
1930 }
1931}
1932
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001933static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001934bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1935 enum bpf_attach_type expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001936 u32 btf_id, u32 prog_fd)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001937{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001938 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001939 if (btf_id > BTF_MAX_TYPE)
1940 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001941
1942 switch (prog_type) {
1943 case BPF_PROG_TYPE_TRACING:
KP Singh9e4e01d2020-03-29 01:43:52 +01001944 case BPF_PROG_TYPE_LSM:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001945 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001946 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001947 break;
1948 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001949 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001950 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001951 }
1952
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001953 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1954 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001955 return -EINVAL;
1956
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001957 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001958 case BPF_PROG_TYPE_CGROUP_SOCK:
1959 switch (expected_attach_type) {
1960 case BPF_CGROUP_INET_SOCK_CREATE:
1961 case BPF_CGROUP_INET4_POST_BIND:
1962 case BPF_CGROUP_INET6_POST_BIND:
1963 return 0;
1964 default:
1965 return -EINVAL;
1966 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001967 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1968 switch (expected_attach_type) {
1969 case BPF_CGROUP_INET4_BIND:
1970 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001971 case BPF_CGROUP_INET4_CONNECT:
1972 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001973 case BPF_CGROUP_UDP4_SENDMSG:
1974 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02001975 case BPF_CGROUP_UDP4_RECVMSG:
1976 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001977 return 0;
1978 default:
1979 return -EINVAL;
1980 }
brakmo5cf1e912019-05-28 16:59:36 -07001981 case BPF_PROG_TYPE_CGROUP_SKB:
1982 switch (expected_attach_type) {
1983 case BPF_CGROUP_INET_INGRESS:
1984 case BPF_CGROUP_INET_EGRESS:
1985 return 0;
1986 default:
1987 return -EINVAL;
1988 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001989 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1990 switch (expected_attach_type) {
1991 case BPF_CGROUP_SETSOCKOPT:
1992 case BPF_CGROUP_GETSOCKOPT:
1993 return 0;
1994 default:
1995 return -EINVAL;
1996 }
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001997 case BPF_PROG_TYPE_EXT:
1998 if (expected_attach_type)
1999 return -EINVAL;
2000 /* fallthrough */
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002001 default:
2002 return 0;
2003 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002004}
2005
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002006/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002007#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002008
Yonghong Song838e9692018-11-19 15:29:11 -08002009static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002010{
2011 enum bpf_prog_type type = attr->prog_type;
2012 struct bpf_prog *prog;
2013 int err;
2014 char license[128];
2015 bool is_gpl;
2016
2017 if (CHECK_ATTR(BPF_PROG_LOAD))
2018 return -EINVAL;
2019
Jiong Wangc240eff2019-05-24 23:25:16 +01002020 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2021 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002022 BPF_F_TEST_STATE_FREQ |
Jiong Wangc240eff2019-05-24 23:25:16 +01002023 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07002024 return -EINVAL;
2025
David Millere9ee9ef2018-11-30 21:08:14 -08002026 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2027 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2028 !capable(CAP_SYS_ADMIN))
2029 return -EPERM;
2030
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002031 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002032 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002033 sizeof(license) - 1) < 0)
2034 return -EFAULT;
2035 license[sizeof(license) - 1] = 0;
2036
2037 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2038 is_gpl = license_is_gpl_compatible(license);
2039
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002040 if (attr->insn_cnt == 0 ||
2041 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002042 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002043 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2044 type != BPF_PROG_TYPE_CGROUP_SKB &&
2045 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002046 return -EPERM;
2047
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002048 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002049 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002050 attr->attach_btf_id,
2051 attr->attach_prog_fd))
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002052 return -EINVAL;
2053
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002054 /* plain bpf_prog allocation */
2055 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2056 if (!prog)
2057 return -ENOMEM;
2058
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002059 prog->expected_attach_type = attr->expected_attach_type;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002060 prog->aux->attach_btf_id = attr->attach_btf_id;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002061 if (attr->attach_prog_fd) {
2062 struct bpf_prog *tgt_prog;
2063
2064 tgt_prog = bpf_prog_get(attr->attach_prog_fd);
2065 if (IS_ERR(tgt_prog)) {
2066 err = PTR_ERR(tgt_prog);
2067 goto free_prog_nouncharge;
2068 }
2069 prog->aux->linked_prog = tgt_prog;
2070 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002071
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002072 prog->aux->offload_requested = !!attr->prog_ifindex;
2073
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002074 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002075 if (err)
2076 goto free_prog_nouncharge;
2077
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002078 err = bpf_prog_charge_memlock(prog);
2079 if (err)
2080 goto free_prog_sec;
2081
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002082 prog->len = attr->insn_cnt;
2083
2084 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002085 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01002086 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002087 goto free_prog;
2088
2089 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002090 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002091
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002092 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002093 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002094
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002095 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002096 err = bpf_prog_offload_init(prog, attr);
2097 if (err)
2098 goto free_prog;
2099 }
2100
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002101 /* find program type: socket_filter vs tracing_filter */
2102 err = find_prog_type(type, prog);
2103 if (err < 0)
2104 goto free_prog;
2105
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002106 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002107 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2108 sizeof(attr->prog_name));
2109 if (err < 0)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002110 goto free_prog;
2111
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002112 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002113 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002114 if (err < 0)
2115 goto free_used_maps;
2116
Daniel Borkmann9facc332018-06-15 02:30:48 +02002117 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002118 if (err < 0)
2119 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002120
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002121 err = bpf_prog_alloc_id(prog);
2122 if (err)
2123 goto free_used_maps;
2124
Daniel Borkmannc7517982019-08-23 22:14:23 +02002125 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2126 * effectively publicly exposed. However, retrieving via
2127 * bpf_prog_get_fd_by_id() will take another reference,
2128 * therefore it cannot be gone underneath us.
2129 *
2130 * Only for the time /after/ successful bpf_prog_new_fd()
2131 * and before returning to userspace, we might just hold
2132 * one reference and any parallel close on that fd could
2133 * rip everything out. Hence, below notifications must
2134 * happen before bpf_prog_new_fd().
2135 *
2136 * Also, any failure handling from this point onwards must
2137 * be using bpf_prog_put() given the program is exposed.
2138 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002139 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002140 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002141 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002142
2143 err = bpf_prog_new_fd(prog);
2144 if (err < 0)
2145 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002146 return err;
2147
2148free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002149 /* In case we have subprogs, we need to wait for a grace
2150 * period before we can tear down JIT memory since symbols
2151 * are already exposed under kallsyms.
2152 */
2153 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2154 return err;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002155free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002156 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002157free_prog_sec:
2158 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002159free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002160 bpf_prog_free(prog);
2161 return err;
2162}
2163
Chenbo Feng6e71b042017-10-18 13:00:22 -07002164#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002165
2166static int bpf_obj_pin(const union bpf_attr *attr)
2167{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002168 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002169 return -EINVAL;
2170
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002171 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002172}
2173
2174static int bpf_obj_get(const union bpf_attr *attr)
2175{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002176 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2177 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002178 return -EINVAL;
2179
Chenbo Feng6e71b042017-10-18 13:00:22 -07002180 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2181 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002182}
2183
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002184void bpf_link_init(struct bpf_link *link, const struct bpf_link_ops *ops,
2185 struct bpf_prog *prog)
2186{
2187 atomic64_set(&link->refcnt, 1);
2188 link->ops = ops;
2189 link->prog = prog;
2190}
2191
Andrii Nakryiko98868662020-03-12 17:21:28 -07002192/* Clean up bpf_link and corresponding anon_inode file and FD. After
2193 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2194 * anon_inode's release() call. This helper manages marking bpf_link as
2195 * defunct, releases anon_inode file and puts reserved FD.
2196 */
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07002197void bpf_link_cleanup(struct bpf_link *link, struct file *link_file,
2198 int link_fd)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002199{
2200 link->prog = NULL;
Andrii Nakryiko98868662020-03-12 17:21:28 -07002201 fput(link_file);
2202 put_unused_fd(link_fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002203}
2204
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002205void bpf_link_inc(struct bpf_link *link)
2206{
2207 atomic64_inc(&link->refcnt);
2208}
2209
2210/* bpf_link_free is guaranteed to be called from process context */
2211static void bpf_link_free(struct bpf_link *link)
2212{
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002213 if (link->prog) {
2214 /* detach BPF program, clean up used resources */
2215 link->ops->release(link);
2216 bpf_prog_put(link->prog);
2217 }
2218 /* free bpf_link and its containing memory */
2219 link->ops->dealloc(link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002220}
2221
2222static void bpf_link_put_deferred(struct work_struct *work)
2223{
2224 struct bpf_link *link = container_of(work, struct bpf_link, work);
2225
2226 bpf_link_free(link);
2227}
2228
2229/* bpf_link_put can be called from atomic context, but ensures that resources
2230 * are freed from process context
2231 */
2232void bpf_link_put(struct bpf_link *link)
2233{
2234 if (!atomic64_dec_and_test(&link->refcnt))
2235 return;
2236
2237 if (in_atomic()) {
2238 INIT_WORK(&link->work, bpf_link_put_deferred);
2239 schedule_work(&link->work);
2240 } else {
2241 bpf_link_free(link);
2242 }
2243}
2244
2245static int bpf_link_release(struct inode *inode, struct file *filp)
2246{
2247 struct bpf_link *link = filp->private_data;
2248
2249 bpf_link_put(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002250 return 0;
2251}
2252
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002253#ifdef CONFIG_PROC_FS
2254static const struct bpf_link_ops bpf_raw_tp_lops;
2255static const struct bpf_link_ops bpf_tracing_link_lops;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002256
2257static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2258{
2259 const struct bpf_link *link = filp->private_data;
2260 const struct bpf_prog *prog = link->prog;
2261 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2262 const char *link_type;
2263
2264 if (link->ops == &bpf_raw_tp_lops)
2265 link_type = "raw_tracepoint";
2266 else if (link->ops == &bpf_tracing_link_lops)
2267 link_type = "tracing";
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07002268#ifdef CONFIG_CGROUP_BPF
2269 else if (link->ops == &bpf_cgroup_link_lops)
2270 link_type = "cgroup";
2271#endif
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002272 else
2273 link_type = "unknown";
2274
2275 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2276 seq_printf(m,
2277 "link_type:\t%s\n"
2278 "prog_tag:\t%s\n"
2279 "prog_id:\t%u\n",
2280 link_type,
2281 prog_tag,
2282 prog->aux->id);
2283}
2284#endif
2285
Zou Wei6f302bf2020-04-23 10:32:40 +08002286static const struct file_operations bpf_link_fops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002287#ifdef CONFIG_PROC_FS
2288 .show_fdinfo = bpf_link_show_fdinfo,
2289#endif
2290 .release = bpf_link_release,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002291 .read = bpf_dummy_read,
2292 .write = bpf_dummy_write,
2293};
2294
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002295int bpf_link_new_fd(struct bpf_link *link)
2296{
2297 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2298}
2299
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002300/* Similar to bpf_link_new_fd, create anon_inode for given bpf_link, but
2301 * instead of immediately installing fd in fdtable, just reserve it and
2302 * return. Caller then need to either install it with fd_install(fd, file) or
2303 * release with put_unused_fd(fd).
2304 * This is useful for cases when bpf_link attachment/detachment are
2305 * complicated and expensive operations and should be delayed until all the fd
2306 * reservation and anon_inode creation succeeds.
2307 */
2308struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd)
2309{
2310 struct file *file;
2311 int fd;
2312
2313 fd = get_unused_fd_flags(O_CLOEXEC);
2314 if (fd < 0)
2315 return ERR_PTR(fd);
2316
2317 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2318 if (IS_ERR(file)) {
2319 put_unused_fd(fd);
2320 return file;
2321 }
2322
2323 *reserved_fd = fd;
2324 return file;
2325}
2326
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002327struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2328{
2329 struct fd f = fdget(ufd);
2330 struct bpf_link *link;
2331
2332 if (!f.file)
2333 return ERR_PTR(-EBADF);
2334 if (f.file->f_op != &bpf_link_fops) {
2335 fdput(f);
2336 return ERR_PTR(-EINVAL);
2337 }
2338
2339 link = f.file->private_data;
2340 bpf_link_inc(link);
2341 fdput(f);
2342
2343 return link;
2344}
2345
2346struct bpf_tracing_link {
2347 struct bpf_link link;
2348};
2349
2350static void bpf_tracing_link_release(struct bpf_link *link)
2351{
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002352 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog));
2353}
2354
2355static void bpf_tracing_link_dealloc(struct bpf_link *link)
2356{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002357 struct bpf_tracing_link *tr_link =
2358 container_of(link, struct bpf_tracing_link, link);
2359
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002360 kfree(tr_link);
2361}
2362
2363static const struct bpf_link_ops bpf_tracing_link_lops = {
2364 .release = bpf_tracing_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002365 .dealloc = bpf_tracing_link_dealloc,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002366};
2367
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002368static int bpf_tracing_prog_attach(struct bpf_prog *prog)
2369{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002370 struct bpf_tracing_link *link;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002371 struct file *link_file;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002372 int link_fd, err;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002373
KP Singh9e4e01d2020-03-29 01:43:52 +01002374 switch (prog->type) {
2375 case BPF_PROG_TYPE_TRACING:
2376 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2377 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2378 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2379 err = -EINVAL;
2380 goto out_put_prog;
2381 }
2382 break;
2383 case BPF_PROG_TYPE_EXT:
2384 if (prog->expected_attach_type != 0) {
2385 err = -EINVAL;
2386 goto out_put_prog;
2387 }
2388 break;
2389 case BPF_PROG_TYPE_LSM:
2390 if (prog->expected_attach_type != BPF_LSM_MAC) {
2391 err = -EINVAL;
2392 goto out_put_prog;
2393 }
2394 break;
2395 default:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002396 err = -EINVAL;
2397 goto out_put_prog;
2398 }
2399
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002400 link = kzalloc(sizeof(*link), GFP_USER);
2401 if (!link) {
2402 err = -ENOMEM;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002403 goto out_put_prog;
2404 }
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002405 bpf_link_init(&link->link, &bpf_tracing_link_lops, prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002406
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002407 link_file = bpf_link_new_file(&link->link, &link_fd);
2408 if (IS_ERR(link_file)) {
2409 kfree(link);
2410 err = PTR_ERR(link_file);
2411 goto out_put_prog;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002412 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002413
2414 err = bpf_trampoline_link_prog(prog);
2415 if (err) {
Andrii Nakryiko98868662020-03-12 17:21:28 -07002416 bpf_link_cleanup(&link->link, link_file, link_fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002417 goto out_put_prog;
2418 }
2419
2420 fd_install(link_fd, link_file);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002421 return link_fd;
2422
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002423out_put_prog:
2424 bpf_prog_put(prog);
2425 return err;
2426}
2427
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002428struct bpf_raw_tp_link {
2429 struct bpf_link link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002430 struct bpf_raw_event_map *btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002431};
2432
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002433static void bpf_raw_tp_link_release(struct bpf_link *link)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002434{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002435 struct bpf_raw_tp_link *raw_tp =
2436 container_of(link, struct bpf_raw_tp_link, link);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002437
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002438 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002439 bpf_put_raw_tracepoint(raw_tp->btp);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002440}
2441
2442static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2443{
2444 struct bpf_raw_tp_link *raw_tp =
2445 container_of(link, struct bpf_raw_tp_link, link);
2446
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002447 kfree(raw_tp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002448}
2449
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002450static const struct bpf_link_ops bpf_raw_tp_lops = {
2451 .release = bpf_raw_tp_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002452 .dealloc = bpf_raw_tp_link_dealloc,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002453};
2454
2455#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2456
2457static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2458{
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002459 struct bpf_raw_tp_link *link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002460 struct bpf_raw_event_map *btp;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002461 struct file *link_file;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002462 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002463 const char *tp_name;
2464 char buf[128];
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002465 int link_fd, err;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002466
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002467 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2468 return -EINVAL;
2469
2470 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2471 if (IS_ERR(prog))
2472 return PTR_ERR(prog);
2473
KP Singh9e4e01d2020-03-29 01:43:52 +01002474 switch (prog->type) {
2475 case BPF_PROG_TYPE_TRACING:
2476 case BPF_PROG_TYPE_EXT:
2477 case BPF_PROG_TYPE_LSM:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002478 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002479 /* The attach point for this category of programs
2480 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002481 */
2482 err = -EINVAL;
2483 goto out_put_prog;
2484 }
KP Singh9e4e01d2020-03-29 01:43:52 +01002485 if (prog->type == BPF_PROG_TYPE_TRACING &&
2486 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002487 tp_name = prog->aux->attach_func_name;
KP Singh9e4e01d2020-03-29 01:43:52 +01002488 break;
2489 }
2490 return bpf_tracing_prog_attach(prog);
2491 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2492 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002493 if (strncpy_from_user(buf,
2494 u64_to_user_ptr(attr->raw_tracepoint.name),
2495 sizeof(buf) - 1) < 0) {
2496 err = -EFAULT;
2497 goto out_put_prog;
2498 }
2499 buf[sizeof(buf) - 1] = 0;
2500 tp_name = buf;
KP Singh9e4e01d2020-03-29 01:43:52 +01002501 break;
2502 default:
2503 err = -EINVAL;
2504 goto out_put_prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002505 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002506
Matt Mullinsa38d1102018-12-12 16:42:37 -08002507 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002508 if (!btp) {
2509 err = -ENOENT;
2510 goto out_put_prog;
2511 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002512
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002513 link = kzalloc(sizeof(*link), GFP_USER);
2514 if (!link) {
Matt Mullinsa38d1102018-12-12 16:42:37 -08002515 err = -ENOMEM;
2516 goto out_put_btp;
2517 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002518 bpf_link_init(&link->link, &bpf_raw_tp_lops, prog);
2519 link->btp = btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002520
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002521 link_file = bpf_link_new_file(&link->link, &link_fd);
2522 if (IS_ERR(link_file)) {
2523 kfree(link);
2524 err = PTR_ERR(link_file);
2525 goto out_put_btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002526 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002527
2528 err = bpf_probe_register(link->btp, prog);
2529 if (err) {
Andrii Nakryiko98868662020-03-12 17:21:28 -07002530 bpf_link_cleanup(&link->link, link_file, link_fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002531 goto out_put_btp;
2532 }
2533
2534 fd_install(link_fd, link_file);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002535 return link_fd;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002536
Matt Mullinsa38d1102018-12-12 16:42:37 -08002537out_put_btp:
2538 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002539out_put_prog:
2540 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002541 return err;
2542}
2543
Anders Roxell33491582018-04-03 14:09:47 +02002544static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2545 enum bpf_attach_type attach_type)
2546{
2547 switch (prog->type) {
2548 case BPF_PROG_TYPE_CGROUP_SOCK:
2549 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002550 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Anders Roxell33491582018-04-03 14:09:47 +02002551 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07002552 case BPF_PROG_TYPE_CGROUP_SKB:
2553 return prog->enforce_expected_attach_type &&
2554 prog->expected_attach_type != attach_type ?
2555 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02002556 default:
2557 return 0;
2558 }
2559}
2560
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002561static enum bpf_prog_type
2562attach_type_to_prog_type(enum bpf_attach_type attach_type)
2563{
2564 switch (attach_type) {
2565 case BPF_CGROUP_INET_INGRESS:
2566 case BPF_CGROUP_INET_EGRESS:
2567 return BPF_PROG_TYPE_CGROUP_SKB;
2568 break;
2569 case BPF_CGROUP_INET_SOCK_CREATE:
2570 case BPF_CGROUP_INET4_POST_BIND:
2571 case BPF_CGROUP_INET6_POST_BIND:
2572 return BPF_PROG_TYPE_CGROUP_SOCK;
2573 case BPF_CGROUP_INET4_BIND:
2574 case BPF_CGROUP_INET6_BIND:
2575 case BPF_CGROUP_INET4_CONNECT:
2576 case BPF_CGROUP_INET6_CONNECT:
2577 case BPF_CGROUP_UDP4_SENDMSG:
2578 case BPF_CGROUP_UDP6_SENDMSG:
2579 case BPF_CGROUP_UDP4_RECVMSG:
2580 case BPF_CGROUP_UDP6_RECVMSG:
2581 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2582 case BPF_CGROUP_SOCK_OPS:
2583 return BPF_PROG_TYPE_SOCK_OPS;
2584 case BPF_CGROUP_DEVICE:
2585 return BPF_PROG_TYPE_CGROUP_DEVICE;
2586 case BPF_SK_MSG_VERDICT:
2587 return BPF_PROG_TYPE_SK_MSG;
2588 case BPF_SK_SKB_STREAM_PARSER:
2589 case BPF_SK_SKB_STREAM_VERDICT:
2590 return BPF_PROG_TYPE_SK_SKB;
2591 case BPF_LIRC_MODE2:
2592 return BPF_PROG_TYPE_LIRC_MODE2;
2593 case BPF_FLOW_DISSECTOR:
2594 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2595 case BPF_CGROUP_SYSCTL:
2596 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2597 case BPF_CGROUP_GETSOCKOPT:
2598 case BPF_CGROUP_SETSOCKOPT:
2599 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
2600 default:
2601 return BPF_PROG_TYPE_UNSPEC;
2602 }
2603}
2604
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002605#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07002606
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002607#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002608 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002609
Daniel Mackf4324552016-11-23 16:52:27 +01002610static int bpf_prog_attach(const union bpf_attr *attr)
2611{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002612 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002613 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002614 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002615
2616 if (!capable(CAP_NET_ADMIN))
2617 return -EPERM;
2618
2619 if (CHECK_ATTR(BPF_PROG_ATTACH))
2620 return -EINVAL;
2621
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002622 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002623 return -EINVAL;
2624
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002625 ptype = attach_type_to_prog_type(attr->attach_type);
2626 if (ptype == BPF_PROG_TYPE_UNSPEC)
Daniel Mackf4324552016-11-23 16:52:27 +01002627 return -EINVAL;
Daniel Mackf4324552016-11-23 16:52:27 +01002628
David Ahernb2cd1252016-12-01 08:48:03 -08002629 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2630 if (IS_ERR(prog))
2631 return PTR_ERR(prog);
2632
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002633 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2634 bpf_prog_put(prog);
2635 return -EINVAL;
2636 }
2637
Sean Youngfdb5c452018-06-19 00:04:24 +01002638 switch (ptype) {
2639 case BPF_PROG_TYPE_SK_SKB:
2640 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002641 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01002642 break;
2643 case BPF_PROG_TYPE_LIRC_MODE2:
2644 ret = lirc_prog_attach(attr, prog);
2645 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07002646 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2647 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
2648 break;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002649 case BPF_PROG_TYPE_CGROUP_DEVICE:
2650 case BPF_PROG_TYPE_CGROUP_SKB:
2651 case BPF_PROG_TYPE_CGROUP_SOCK:
2652 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2653 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2654 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2655 case BPF_PROG_TYPE_SOCK_OPS:
Sean Youngfdb5c452018-06-19 00:04:24 +01002656 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002657 break;
2658 default:
2659 ret = -EINVAL;
David Ahernb2cd1252016-12-01 08:48:03 -08002660 }
2661
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002662 if (ret)
2663 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002664 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002665}
2666
2667#define BPF_PROG_DETACH_LAST_FIELD attach_type
2668
2669static int bpf_prog_detach(const union bpf_attr *attr)
2670{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002671 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002672
2673 if (!capable(CAP_NET_ADMIN))
2674 return -EPERM;
2675
2676 if (CHECK_ATTR(BPF_PROG_DETACH))
2677 return -EINVAL;
2678
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002679 ptype = attach_type_to_prog_type(attr->attach_type);
2680
2681 switch (ptype) {
2682 case BPF_PROG_TYPE_SK_MSG:
2683 case BPF_PROG_TYPE_SK_SKB:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002684 return sock_map_get_from_fd(attr, NULL);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002685 case BPF_PROG_TYPE_LIRC_MODE2:
Sean Youngf4364dc2018-05-27 12:24:09 +01002686 return lirc_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002687 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Petar Penkovd58e4682018-09-14 07:46:18 -07002688 return skb_flow_dissector_bpf_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002689 case BPF_PROG_TYPE_CGROUP_DEVICE:
2690 case BPF_PROG_TYPE_CGROUP_SKB:
2691 case BPF_PROG_TYPE_CGROUP_SOCK:
2692 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2693 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2694 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2695 case BPF_PROG_TYPE_SOCK_OPS:
2696 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01002697 default:
2698 return -EINVAL;
2699 }
Daniel Mackf4324552016-11-23 16:52:27 +01002700}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002701
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002702#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2703
2704static int bpf_prog_query(const union bpf_attr *attr,
2705 union bpf_attr __user *uattr)
2706{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002707 if (!capable(CAP_NET_ADMIN))
2708 return -EPERM;
2709 if (CHECK_ATTR(BPF_PROG_QUERY))
2710 return -EINVAL;
2711 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2712 return -EINVAL;
2713
2714 switch (attr->query.attach_type) {
2715 case BPF_CGROUP_INET_INGRESS:
2716 case BPF_CGROUP_INET_EGRESS:
2717 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002718 case BPF_CGROUP_INET4_BIND:
2719 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002720 case BPF_CGROUP_INET4_POST_BIND:
2721 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002722 case BPF_CGROUP_INET4_CONNECT:
2723 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002724 case BPF_CGROUP_UDP4_SENDMSG:
2725 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002726 case BPF_CGROUP_UDP4_RECVMSG:
2727 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002728 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05002729 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002730 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002731 case BPF_CGROUP_GETSOCKOPT:
2732 case BPF_CGROUP_SETSOCKOPT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002733 return cgroup_bpf_prog_query(attr, uattr);
Sean Youngf4364dc2018-05-27 12:24:09 +01002734 case BPF_LIRC_MODE2:
2735 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07002736 case BPF_FLOW_DISSECTOR:
2737 return skb_flow_dissector_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002738 default:
2739 return -EINVAL;
2740 }
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002741}
Daniel Mackf4324552016-11-23 16:52:27 +01002742
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002743#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002744
2745static int bpf_prog_test_run(const union bpf_attr *attr,
2746 union bpf_attr __user *uattr)
2747{
2748 struct bpf_prog *prog;
2749 int ret = -ENOTSUPP;
2750
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08002751 if (!capable(CAP_SYS_ADMIN))
2752 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002753 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2754 return -EINVAL;
2755
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002756 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2757 (!attr->test.ctx_size_in && attr->test.ctx_in))
2758 return -EINVAL;
2759
2760 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2761 (!attr->test.ctx_size_out && attr->test.ctx_out))
2762 return -EINVAL;
2763
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002764 prog = bpf_prog_get(attr->test.prog_fd);
2765 if (IS_ERR(prog))
2766 return PTR_ERR(prog);
2767
2768 if (prog->aux->ops->test_run)
2769 ret = prog->aux->ops->test_run(prog, attr, uattr);
2770
2771 bpf_prog_put(prog);
2772 return ret;
2773}
2774
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002775#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2776
2777static int bpf_obj_get_next_id(const union bpf_attr *attr,
2778 union bpf_attr __user *uattr,
2779 struct idr *idr,
2780 spinlock_t *lock)
2781{
2782 u32 next_id = attr->start_id;
2783 int err = 0;
2784
2785 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2786 return -EINVAL;
2787
2788 if (!capable(CAP_SYS_ADMIN))
2789 return -EPERM;
2790
2791 next_id++;
2792 spin_lock_bh(lock);
2793 if (!idr_get_next(idr, &next_id))
2794 err = -ENOENT;
2795 spin_unlock_bh(lock);
2796
2797 if (!err)
2798 err = put_user(next_id, &uattr->next_id);
2799
2800 return err;
2801}
2802
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002803#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2804
Björn Töpel7e6897f2019-12-13 18:51:09 +01002805struct bpf_prog *bpf_prog_by_id(u32 id)
2806{
2807 struct bpf_prog *prog;
2808
2809 if (!id)
2810 return ERR_PTR(-ENOENT);
2811
2812 spin_lock_bh(&prog_idr_lock);
2813 prog = idr_find(&prog_idr, id);
2814 if (prog)
2815 prog = bpf_prog_inc_not_zero(prog);
2816 else
2817 prog = ERR_PTR(-ENOENT);
2818 spin_unlock_bh(&prog_idr_lock);
2819 return prog;
2820}
2821
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002822static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2823{
2824 struct bpf_prog *prog;
2825 u32 id = attr->prog_id;
2826 int fd;
2827
2828 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2829 return -EINVAL;
2830
2831 if (!capable(CAP_SYS_ADMIN))
2832 return -EPERM;
2833
Björn Töpel7e6897f2019-12-13 18:51:09 +01002834 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002835 if (IS_ERR(prog))
2836 return PTR_ERR(prog);
2837
2838 fd = bpf_prog_new_fd(prog);
2839 if (fd < 0)
2840 bpf_prog_put(prog);
2841
2842 return fd;
2843}
2844
Chenbo Feng6e71b042017-10-18 13:00:22 -07002845#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002846
2847static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2848{
2849 struct bpf_map *map;
2850 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002851 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002852 int fd;
2853
Chenbo Feng6e71b042017-10-18 13:00:22 -07002854 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2855 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002856 return -EINVAL;
2857
2858 if (!capable(CAP_SYS_ADMIN))
2859 return -EPERM;
2860
Chenbo Feng6e71b042017-10-18 13:00:22 -07002861 f_flags = bpf_get_file_flag(attr->open_flags);
2862 if (f_flags < 0)
2863 return f_flags;
2864
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002865 spin_lock_bh(&map_idr_lock);
2866 map = idr_find(&map_idr, id);
2867 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07002868 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002869 else
2870 map = ERR_PTR(-ENOENT);
2871 spin_unlock_bh(&map_idr_lock);
2872
2873 if (IS_ERR(map))
2874 return PTR_ERR(map);
2875
Chenbo Feng6e71b042017-10-18 13:00:22 -07002876 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002877 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002878 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002879
2880 return fd;
2881}
2882
Daniel Borkmann7105e822017-12-20 13:42:57 +01002883static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002884 unsigned long addr, u32 *off,
2885 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002886{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002887 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002888 int i;
2889
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002890 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2891 map = prog->aux->used_maps[i];
2892 if (map == (void *)addr) {
2893 *type = BPF_PSEUDO_MAP_FD;
2894 return map;
2895 }
2896 if (!map->ops->map_direct_value_meta)
2897 continue;
2898 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2899 *type = BPF_PSEUDO_MAP_VALUE;
2900 return map;
2901 }
2902 }
2903
Daniel Borkmann7105e822017-12-20 13:42:57 +01002904 return NULL;
2905}
2906
2907static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2908{
2909 const struct bpf_map *map;
2910 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002911 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002912 u64 imm;
2913 int i;
2914
2915 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2916 GFP_USER);
2917 if (!insns)
2918 return insns;
2919
2920 for (i = 0; i < prog->len; i++) {
2921 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2922 insns[i].code = BPF_JMP | BPF_CALL;
2923 insns[i].imm = BPF_FUNC_tail_call;
2924 /* fall-through */
2925 }
2926 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2927 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2928 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2929 insns[i].code = BPF_JMP | BPF_CALL;
2930 if (!bpf_dump_raw_ok())
2931 insns[i].imm = 0;
2932 continue;
2933 }
2934
2935 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2936 continue;
2937
2938 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002939 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002940 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002941 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002942 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002943 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002944 continue;
2945 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002946 }
2947
2948 return insns;
2949}
2950
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002951static int set_info_rec_size(struct bpf_prog_info *info)
2952{
2953 /*
2954 * Ensure info.*_rec_size is the same as kernel expected size
2955 *
2956 * or
2957 *
2958 * Only allow zero *_rec_size if both _rec_size and _cnt are
2959 * zero. In this case, the kernel will set the expected
2960 * _rec_size back to the info.
2961 */
2962
Yonghong Song11d8b822018-12-10 14:14:08 -08002963 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002964 info->func_info_rec_size != sizeof(struct bpf_func_info))
2965 return -EINVAL;
2966
Yonghong Song11d8b822018-12-10 14:14:08 -08002967 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002968 info->line_info_rec_size != sizeof(struct bpf_line_info))
2969 return -EINVAL;
2970
Yonghong Song11d8b822018-12-10 14:14:08 -08002971 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002972 info->jited_line_info_rec_size != sizeof(__u64))
2973 return -EINVAL;
2974
2975 info->func_info_rec_size = sizeof(struct bpf_func_info);
2976 info->line_info_rec_size = sizeof(struct bpf_line_info);
2977 info->jited_line_info_rec_size = sizeof(__u64);
2978
2979 return 0;
2980}
2981
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002982static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2983 const union bpf_attr *attr,
2984 union bpf_attr __user *uattr)
2985{
2986 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01002987 struct bpf_prog_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002988 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002989 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002990 char __user *uinsns;
2991 u32 ulen;
2992 int err;
2993
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002994 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002995 if (err)
2996 return err;
2997 info_len = min_t(u32, sizeof(info), info_len);
2998
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01002999 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003000 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02003001 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003002
3003 info.type = prog->type;
3004 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003005 info.load_time = prog->aux->load_time;
3006 info.created_by_uid = from_kuid_munged(current_user_ns(),
3007 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02003008 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003009
3010 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003011 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3012
3013 ulen = info.nr_map_ids;
3014 info.nr_map_ids = prog->aux->used_map_cnt;
3015 ulen = min_t(u32, info.nr_map_ids, ulen);
3016 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07003017 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003018 u32 i;
3019
3020 for (i = 0; i < ulen; i++)
3021 if (put_user(prog->aux->used_maps[i]->id,
3022 &user_map_ids[i]))
3023 return -EFAULT;
3024 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003025
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003026 err = set_info_rec_size(&info);
3027 if (err)
3028 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08003029
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003030 bpf_prog_get_stats(prog, &stats);
3031 info.run_time_ns = stats.nsecs;
3032 info.run_cnt = stats.cnt;
3033
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003034 if (!capable(CAP_SYS_ADMIN)) {
3035 info.jited_prog_len = 0;
3036 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303037 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01003038 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08003039 info.nr_func_info = 0;
3040 info.nr_line_info = 0;
3041 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003042 goto done;
3043 }
3044
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003045 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02003046 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003047 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003048 struct bpf_insn *insns_sanitized;
3049 bool fault;
3050
3051 if (prog->blinded && !bpf_dump_raw_ok()) {
3052 info.xlated_prog_insns = 0;
3053 goto done;
3054 }
3055 insns_sanitized = bpf_insn_prepare_dump(prog);
3056 if (!insns_sanitized)
3057 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003058 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3059 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003060 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3061 kfree(insns_sanitized);
3062 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003063 return -EFAULT;
3064 }
3065
Jakub Kicinski675fc272017-12-27 18:39:09 -08003066 if (bpf_prog_is_dev_bound(prog->aux)) {
3067 err = bpf_prog_offload_info_fill(&info, prog);
3068 if (err)
3069 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08003070 goto done;
3071 }
3072
3073 /* NOTE: the following code is supposed to be skipped for offload.
3074 * bpf_prog_offload_info_fill() is the place to fill similar fields
3075 * for offload.
3076 */
3077 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05303078 if (prog->aux->func_cnt) {
3079 u32 i;
3080
3081 info.jited_prog_len = 0;
3082 for (i = 0; i < prog->aux->func_cnt; i++)
3083 info.jited_prog_len += prog->aux->func[i]->jited_len;
3084 } else {
3085 info.jited_prog_len = prog->jited_len;
3086 }
3087
Jiong Wangfcfb1262018-01-16 16:05:19 -08003088 if (info.jited_prog_len && ulen) {
3089 if (bpf_dump_raw_ok()) {
3090 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3091 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05303092
3093 /* for multi-function programs, copy the JITed
3094 * instructions for all the functions
3095 */
3096 if (prog->aux->func_cnt) {
3097 u32 len, free, i;
3098 u8 *img;
3099
3100 free = ulen;
3101 for (i = 0; i < prog->aux->func_cnt; i++) {
3102 len = prog->aux->func[i]->jited_len;
3103 len = min_t(u32, len, free);
3104 img = (u8 *) prog->aux->func[i]->bpf_func;
3105 if (copy_to_user(uinsns, img, len))
3106 return -EFAULT;
3107 uinsns += len;
3108 free -= len;
3109 if (!free)
3110 break;
3111 }
3112 } else {
3113 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3114 return -EFAULT;
3115 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08003116 } else {
3117 info.jited_prog_insns = 0;
3118 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08003119 }
3120
Sandipan Dasdbecd732018-05-24 12:26:48 +05303121 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07003122 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003123 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05303124 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07003125 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303126 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303127 u32 i;
3128
3129 /* copy the address of the kernel symbol
3130 * corresponding to each function
3131 */
3132 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3133 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07003134 if (prog->aux->func_cnt) {
3135 for (i = 0; i < ulen; i++) {
3136 ksym_addr = (unsigned long)
3137 prog->aux->func[i]->bpf_func;
3138 if (put_user((u64) ksym_addr,
3139 &user_ksyms[i]))
3140 return -EFAULT;
3141 }
3142 } else {
3143 ksym_addr = (unsigned long) prog->bpf_func;
3144 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05303145 return -EFAULT;
3146 }
3147 } else {
3148 info.jited_ksyms = 0;
3149 }
3150 }
3151
Sandipan Das815581c2018-05-24 12:26:52 +05303152 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07003153 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003154 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05303155 if (bpf_dump_raw_ok()) {
3156 u32 __user *user_lens;
3157 u32 func_len, i;
3158
3159 /* copy the JITed image lengths for each function */
3160 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3161 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07003162 if (prog->aux->func_cnt) {
3163 for (i = 0; i < ulen; i++) {
3164 func_len =
3165 prog->aux->func[i]->jited_len;
3166 if (put_user(func_len, &user_lens[i]))
3167 return -EFAULT;
3168 }
3169 } else {
3170 func_len = prog->jited_len;
3171 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05303172 return -EFAULT;
3173 }
3174 } else {
3175 info.jited_func_lens = 0;
3176 }
3177 }
3178
Martin KaFai Lau73372242018-12-05 17:35:43 -08003179 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08003180 info.btf_id = btf_id(prog->aux->btf);
3181
Yonghong Song11d8b822018-12-10 14:14:08 -08003182 ulen = info.nr_func_info;
3183 info.nr_func_info = prog->aux->func_info_cnt;
3184 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003185 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08003186
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003187 user_finfo = u64_to_user_ptr(info.func_info);
3188 ulen = min_t(u32, info.nr_func_info, ulen);
3189 if (copy_to_user(user_finfo, prog->aux->func_info,
3190 info.func_info_rec_size * ulen))
3191 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08003192 }
3193
Yonghong Song11d8b822018-12-10 14:14:08 -08003194 ulen = info.nr_line_info;
3195 info.nr_line_info = prog->aux->nr_linfo;
3196 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003197 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003198
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003199 user_linfo = u64_to_user_ptr(info.line_info);
3200 ulen = min_t(u32, info.nr_line_info, ulen);
3201 if (copy_to_user(user_linfo, prog->aux->linfo,
3202 info.line_info_rec_size * ulen))
3203 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003204 }
3205
Yonghong Song11d8b822018-12-10 14:14:08 -08003206 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003207 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08003208 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003209 else
Yonghong Song11d8b822018-12-10 14:14:08 -08003210 info.nr_jited_line_info = 0;
3211 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003212 if (bpf_dump_raw_ok()) {
3213 __u64 __user *user_linfo;
3214 u32 i;
3215
3216 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08003217 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003218 for (i = 0; i < ulen; i++) {
3219 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3220 &user_linfo[i]))
3221 return -EFAULT;
3222 }
3223 } else {
3224 info.jited_line_info = 0;
3225 }
3226 }
3227
Song Liuc872bdb2018-12-12 09:37:46 -08003228 ulen = info.nr_prog_tags;
3229 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3230 if (ulen) {
3231 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3232 u32 i;
3233
3234 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3235 ulen = min_t(u32, info.nr_prog_tags, ulen);
3236 if (prog->aux->func_cnt) {
3237 for (i = 0; i < ulen; i++) {
3238 if (copy_to_user(user_prog_tags[i],
3239 prog->aux->func[i]->tag,
3240 BPF_TAG_SIZE))
3241 return -EFAULT;
3242 }
3243 } else {
3244 if (copy_to_user(user_prog_tags[0],
3245 prog->tag, BPF_TAG_SIZE))
3246 return -EFAULT;
3247 }
3248 }
3249
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003250done:
3251 if (copy_to_user(uinfo, &info, info_len) ||
3252 put_user(info_len, &uattr->info.info_len))
3253 return -EFAULT;
3254
3255 return 0;
3256}
3257
3258static int bpf_map_get_info_by_fd(struct bpf_map *map,
3259 const union bpf_attr *attr,
3260 union bpf_attr __user *uattr)
3261{
3262 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003263 struct bpf_map_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003264 u32 info_len = attr->info.info_len;
3265 int err;
3266
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003267 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003268 if (err)
3269 return err;
3270 info_len = min_t(u32, sizeof(info), info_len);
3271
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003272 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003273 info.type = map->map_type;
3274 info.id = map->id;
3275 info.key_size = map->key_size;
3276 info.value_size = map->value_size;
3277 info.max_entries = map->max_entries;
3278 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07003279 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003280
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003281 if (map->btf) {
3282 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07003283 info.btf_key_type_id = map->btf_key_type_id;
3284 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003285 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08003286 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003287
Jakub Kicinski52775b32018-01-17 19:13:28 -08003288 if (bpf_map_is_dev_bound(map)) {
3289 err = bpf_map_offload_info_fill(&info, map);
3290 if (err)
3291 return err;
3292 }
3293
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003294 if (copy_to_user(uinfo, &info, info_len) ||
3295 put_user(info_len, &uattr->info.info_len))
3296 return -EFAULT;
3297
3298 return 0;
3299}
3300
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003301static int bpf_btf_get_info_by_fd(struct btf *btf,
3302 const union bpf_attr *attr,
3303 union bpf_attr __user *uattr)
3304{
3305 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3306 u32 info_len = attr->info.info_len;
3307 int err;
3308
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003309 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003310 if (err)
3311 return err;
3312
3313 return btf_get_info_by_fd(btf, attr, uattr);
3314}
3315
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003316#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3317
3318static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3319 union bpf_attr __user *uattr)
3320{
3321 int ufd = attr->info.bpf_fd;
3322 struct fd f;
3323 int err;
3324
3325 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3326 return -EINVAL;
3327
3328 f = fdget(ufd);
3329 if (!f.file)
3330 return -EBADFD;
3331
3332 if (f.file->f_op == &bpf_prog_fops)
3333 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
3334 uattr);
3335 else if (f.file->f_op == &bpf_map_fops)
3336 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
3337 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07003338 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003339 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003340 else
3341 err = -EINVAL;
3342
3343 fdput(f);
3344 return err;
3345}
3346
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003347#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3348
3349static int bpf_btf_load(const union bpf_attr *attr)
3350{
3351 if (CHECK_ATTR(BPF_BTF_LOAD))
3352 return -EINVAL;
3353
3354 if (!capable(CAP_SYS_ADMIN))
3355 return -EPERM;
3356
3357 return btf_new_fd(attr);
3358}
3359
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003360#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3361
3362static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3363{
3364 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3365 return -EINVAL;
3366
3367 if (!capable(CAP_SYS_ADMIN))
3368 return -EPERM;
3369
3370 return btf_get_fd_by_id(attr->btf_id);
3371}
3372
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003373static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3374 union bpf_attr __user *uattr,
3375 u32 prog_id, u32 fd_type,
3376 const char *buf, u64 probe_offset,
3377 u64 probe_addr)
3378{
3379 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3380 u32 len = buf ? strlen(buf) : 0, input_len;
3381 int err = 0;
3382
3383 if (put_user(len, &uattr->task_fd_query.buf_len))
3384 return -EFAULT;
3385 input_len = attr->task_fd_query.buf_len;
3386 if (input_len && ubuf) {
3387 if (!len) {
3388 /* nothing to copy, just make ubuf NULL terminated */
3389 char zero = '\0';
3390
3391 if (put_user(zero, ubuf))
3392 return -EFAULT;
3393 } else if (input_len >= len + 1) {
3394 /* ubuf can hold the string with NULL terminator */
3395 if (copy_to_user(ubuf, buf, len + 1))
3396 return -EFAULT;
3397 } else {
3398 /* ubuf cannot hold the string with NULL terminator,
3399 * do a partial copy with NULL terminator.
3400 */
3401 char zero = '\0';
3402
3403 err = -ENOSPC;
3404 if (copy_to_user(ubuf, buf, input_len - 1))
3405 return -EFAULT;
3406 if (put_user(zero, ubuf + input_len - 1))
3407 return -EFAULT;
3408 }
3409 }
3410
3411 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3412 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3413 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3414 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3415 return -EFAULT;
3416
3417 return err;
3418}
3419
3420#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3421
3422static int bpf_task_fd_query(const union bpf_attr *attr,
3423 union bpf_attr __user *uattr)
3424{
3425 pid_t pid = attr->task_fd_query.pid;
3426 u32 fd = attr->task_fd_query.fd;
3427 const struct perf_event *event;
3428 struct files_struct *files;
3429 struct task_struct *task;
3430 struct file *file;
3431 int err;
3432
3433 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3434 return -EINVAL;
3435
3436 if (!capable(CAP_SYS_ADMIN))
3437 return -EPERM;
3438
3439 if (attr->task_fd_query.flags != 0)
3440 return -EINVAL;
3441
3442 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3443 if (!task)
3444 return -ENOENT;
3445
3446 files = get_files_struct(task);
3447 put_task_struct(task);
3448 if (!files)
3449 return -ENOENT;
3450
3451 err = 0;
3452 spin_lock(&files->file_lock);
3453 file = fcheck_files(files, fd);
3454 if (!file)
3455 err = -EBADF;
3456 else
3457 get_file(file);
3458 spin_unlock(&files->file_lock);
3459 put_files_struct(files);
3460
3461 if (err)
3462 goto out;
3463
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003464 if (file->f_op == &bpf_link_fops) {
3465 struct bpf_link *link = file->private_data;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003466
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003467 if (link->ops == &bpf_raw_tp_lops) {
3468 struct bpf_raw_tp_link *raw_tp =
3469 container_of(link, struct bpf_raw_tp_link, link);
3470 struct bpf_raw_event_map *btp = raw_tp->btp;
3471
3472 err = bpf_task_fd_query_copy(attr, uattr,
3473 raw_tp->link.prog->aux->id,
3474 BPF_FD_TYPE_RAW_TRACEPOINT,
3475 btp->tp->name, 0, 0);
3476 goto put_file;
3477 }
3478 goto out_not_supp;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003479 }
3480
3481 event = perf_get_event(file);
3482 if (!IS_ERR(event)) {
3483 u64 probe_offset, probe_addr;
3484 u32 prog_id, fd_type;
3485 const char *buf;
3486
3487 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3488 &buf, &probe_offset,
3489 &probe_addr);
3490 if (!err)
3491 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3492 fd_type, buf,
3493 probe_offset,
3494 probe_addr);
3495 goto put_file;
3496 }
3497
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003498out_not_supp:
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003499 err = -ENOTSUPP;
3500put_file:
3501 fput(file);
3502out:
3503 return err;
3504}
3505
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003506#define BPF_MAP_BATCH_LAST_FIELD batch.flags
3507
3508#define BPF_DO_BATCH(fn) \
3509 do { \
3510 if (!fn) { \
3511 err = -ENOTSUPP; \
3512 goto err_put; \
3513 } \
3514 err = fn(map, attr, uattr); \
3515 } while (0)
3516
3517static int bpf_map_do_batch(const union bpf_attr *attr,
3518 union bpf_attr __user *uattr,
3519 int cmd)
3520{
3521 struct bpf_map *map;
3522 int err, ufd;
3523 struct fd f;
3524
3525 if (CHECK_ATTR(BPF_MAP_BATCH))
3526 return -EINVAL;
3527
3528 ufd = attr->batch.map_fd;
3529 f = fdget(ufd);
3530 map = __bpf_map_get(f);
3531 if (IS_ERR(map))
3532 return PTR_ERR(map);
3533
Yonghong Song05799632020-01-15 10:43:04 -08003534 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3535 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003536 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3537 err = -EPERM;
3538 goto err_put;
3539 }
3540
3541 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3542 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3543 err = -EPERM;
3544 goto err_put;
3545 }
3546
3547 if (cmd == BPF_MAP_LOOKUP_BATCH)
3548 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08003549 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
3550 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08003551 else if (cmd == BPF_MAP_UPDATE_BATCH)
3552 BPF_DO_BATCH(map->ops->map_update_batch);
3553 else
3554 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003555
3556err_put:
3557 fdput(f);
3558 return err;
3559}
3560
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07003561#define BPF_LINK_CREATE_LAST_FIELD link_create.flags
3562static int link_create(union bpf_attr *attr)
3563{
3564 enum bpf_prog_type ptype;
3565 struct bpf_prog *prog;
3566 int ret;
3567
3568 if (!capable(CAP_NET_ADMIN))
3569 return -EPERM;
3570
3571 if (CHECK_ATTR(BPF_LINK_CREATE))
3572 return -EINVAL;
3573
3574 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
3575 if (ptype == BPF_PROG_TYPE_UNSPEC)
3576 return -EINVAL;
3577
3578 prog = bpf_prog_get_type(attr->link_create.prog_fd, ptype);
3579 if (IS_ERR(prog))
3580 return PTR_ERR(prog);
3581
3582 ret = bpf_prog_attach_check_attach_type(prog,
3583 attr->link_create.attach_type);
3584 if (ret)
3585 goto err_out;
3586
3587 switch (ptype) {
3588 case BPF_PROG_TYPE_CGROUP_SKB:
3589 case BPF_PROG_TYPE_CGROUP_SOCK:
3590 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3591 case BPF_PROG_TYPE_SOCK_OPS:
3592 case BPF_PROG_TYPE_CGROUP_DEVICE:
3593 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3594 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3595 ret = cgroup_bpf_link_attach(attr, prog);
3596 break;
3597 default:
3598 ret = -EINVAL;
3599 }
3600
3601err_out:
3602 if (ret < 0)
3603 bpf_prog_put(prog);
3604 return ret;
3605}
3606
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003607#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
3608
3609static int link_update(union bpf_attr *attr)
3610{
3611 struct bpf_prog *old_prog = NULL, *new_prog;
3612 struct bpf_link *link;
3613 u32 flags;
3614 int ret;
3615
3616 if (!capable(CAP_NET_ADMIN))
3617 return -EPERM;
3618
3619 if (CHECK_ATTR(BPF_LINK_UPDATE))
3620 return -EINVAL;
3621
3622 flags = attr->link_update.flags;
3623 if (flags & ~BPF_F_REPLACE)
3624 return -EINVAL;
3625
3626 link = bpf_link_get_from_fd(attr->link_update.link_fd);
3627 if (IS_ERR(link))
3628 return PTR_ERR(link);
3629
3630 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07003631 if (IS_ERR(new_prog)) {
3632 ret = PTR_ERR(new_prog);
3633 goto out_put_link;
3634 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003635
3636 if (flags & BPF_F_REPLACE) {
3637 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
3638 if (IS_ERR(old_prog)) {
3639 ret = PTR_ERR(old_prog);
3640 old_prog = NULL;
3641 goto out_put_progs;
3642 }
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07003643 } else if (attr->link_update.old_prog_fd) {
3644 ret = -EINVAL;
3645 goto out_put_progs;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003646 }
3647
Andrii Nakryikof9d04122020-04-28 17:16:05 -07003648 if (link->ops->update_prog)
3649 ret = link->ops->update_prog(link, new_prog, old_prog);
3650 else
3651 ret = EINVAL;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003652
3653out_put_progs:
3654 if (old_prog)
3655 bpf_prog_put(old_prog);
3656 if (ret)
3657 bpf_prog_put(new_prog);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07003658out_put_link:
3659 bpf_link_put(link);
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003660 return ret;
3661}
3662
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003663SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
3664{
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01003665 union bpf_attr attr;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003666 int err;
3667
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07003668 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003669 return -EPERM;
3670
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003671 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003672 if (err)
3673 return err;
3674 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003675
3676 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01003677 memset(&attr, 0, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003678 if (copy_from_user(&attr, uattr, size) != 0)
3679 return -EFAULT;
3680
Chenbo Fengafdb09c2017-10-18 13:00:24 -07003681 err = security_bpf(cmd, &attr, size);
3682 if (err < 0)
3683 return err;
3684
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003685 switch (cmd) {
3686 case BPF_MAP_CREATE:
3687 err = map_create(&attr);
3688 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07003689 case BPF_MAP_LOOKUP_ELEM:
3690 err = map_lookup_elem(&attr);
3691 break;
3692 case BPF_MAP_UPDATE_ELEM:
3693 err = map_update_elem(&attr);
3694 break;
3695 case BPF_MAP_DELETE_ELEM:
3696 err = map_delete_elem(&attr);
3697 break;
3698 case BPF_MAP_GET_NEXT_KEY:
3699 err = map_get_next_key(&attr);
3700 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02003701 case BPF_MAP_FREEZE:
3702 err = map_freeze(&attr);
3703 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07003704 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08003705 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07003706 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01003707 case BPF_OBJ_PIN:
3708 err = bpf_obj_pin(&attr);
3709 break;
3710 case BPF_OBJ_GET:
3711 err = bpf_obj_get(&attr);
3712 break;
Daniel Mackf4324552016-11-23 16:52:27 +01003713 case BPF_PROG_ATTACH:
3714 err = bpf_prog_attach(&attr);
3715 break;
3716 case BPF_PROG_DETACH:
3717 err = bpf_prog_detach(&attr);
3718 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003719 case BPF_PROG_QUERY:
3720 err = bpf_prog_query(&attr, uattr);
3721 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003722 case BPF_PROG_TEST_RUN:
3723 err = bpf_prog_test_run(&attr, uattr);
3724 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003725 case BPF_PROG_GET_NEXT_ID:
3726 err = bpf_obj_get_next_id(&attr, uattr,
3727 &prog_idr, &prog_idr_lock);
3728 break;
3729 case BPF_MAP_GET_NEXT_ID:
3730 err = bpf_obj_get_next_id(&attr, uattr,
3731 &map_idr, &map_idr_lock);
3732 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01003733 case BPF_BTF_GET_NEXT_ID:
3734 err = bpf_obj_get_next_id(&attr, uattr,
3735 &btf_idr, &btf_idr_lock);
3736 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003737 case BPF_PROG_GET_FD_BY_ID:
3738 err = bpf_prog_get_fd_by_id(&attr);
3739 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003740 case BPF_MAP_GET_FD_BY_ID:
3741 err = bpf_map_get_fd_by_id(&attr);
3742 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003743 case BPF_OBJ_GET_INFO_BY_FD:
3744 err = bpf_obj_get_info_by_fd(&attr, uattr);
3745 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003746 case BPF_RAW_TRACEPOINT_OPEN:
3747 err = bpf_raw_tracepoint_open(&attr);
3748 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003749 case BPF_BTF_LOAD:
3750 err = bpf_btf_load(&attr);
3751 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003752 case BPF_BTF_GET_FD_BY_ID:
3753 err = bpf_btf_get_fd_by_id(&attr);
3754 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003755 case BPF_TASK_FD_QUERY:
3756 err = bpf_task_fd_query(&attr, uattr);
3757 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02003758 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
3759 err = map_lookup_and_delete_elem(&attr);
3760 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003761 case BPF_MAP_LOOKUP_BATCH:
3762 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
3763 break;
Yonghong Song05799632020-01-15 10:43:04 -08003764 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
3765 err = bpf_map_do_batch(&attr, uattr,
3766 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
3767 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08003768 case BPF_MAP_UPDATE_BATCH:
3769 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
3770 break;
3771 case BPF_MAP_DELETE_BATCH:
3772 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
3773 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07003774 case BPF_LINK_CREATE:
3775 err = link_create(&attr);
3776 break;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003777 case BPF_LINK_UPDATE:
3778 err = link_update(&attr);
3779 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003780 default:
3781 err = -EINVAL;
3782 break;
3783 }
3784
3785 return err;
3786}