blob: 6d49c2e1634c9465f53f379c20388b9673dd9144 [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>
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02007#include <linux/bpf_verifier.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07008#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07009#include <linux/syscalls.h>
10#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010011#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010012#include <linux/vmalloc.h>
13#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070014#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070015#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070016#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070017#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070018#include <linux/license.h>
19#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070020#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010021#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070022#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070023#include <linux/cred.h>
24#include <linux/timekeeping.h>
25#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010026#include <linux/nospec.h>
Daniel Borkmannbae141f2019-12-06 22:49:34 +010027#include <linux/audit.h>
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -070028#include <uapi/linux/btf.h>
Mike Rapoportca5999f2020-06-08 21:32:38 -070029#include <linux/pgtable.h>
KP Singh9e4e01d2020-03-29 01:43:52 +010030#include <linux/bpf_lsm.h>
Andrii Nakryiko457f4432020-05-29 00:54:20 -070031#include <linux/poll.h>
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +020032#include <linux/bpf-netns.h>
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -070033#include <linux/rcupdate_trace.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070034
Daniel Borkmannda765a22019-11-22 21:07:58 +010035#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
38#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070039#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
Daniel Borkmannda765a22019-11-22 21:07:58 +010040#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
41 IS_FD_HASH(map))
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070042
Chenbo Feng6e71b042017-10-18 13:00:22 -070043#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
44
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080045DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070046static DEFINE_IDR(prog_idr);
47static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070048static DEFINE_IDR(map_idr);
49static DEFINE_SPINLOCK(map_idr_lock);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -070050static DEFINE_IDR(link_idr);
51static DEFINE_SPINLOCK(link_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080052
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070053int sysctl_unprivileged_bpf_disabled __read_mostly;
54
Johannes Berg40077e02017-04-11 15:34:58 +020055static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080056#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020057#define BPF_MAP_TYPE(_id, _ops) \
58 [_id] = &_ops,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070059#define BPF_LINK_TYPE(_id, _name)
Johannes Berg40077e02017-04-11 15:34:58 +020060#include <linux/bpf_types.h>
61#undef BPF_PROG_TYPE
62#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070063#undef BPF_LINK_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +020064};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070065
Mickaël Salaün752ba562017-08-07 20:45:20 +020066/*
67 * If we're handed a bigger struct than we know of, ensure all the unknown bits
68 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
69 * we don't know about yet.
70 *
71 * There is a ToCToU between this function call and the following
72 * copy_from_user() call. However, this is not a concern since this function is
73 * meant to be a future-proofing of bits.
74 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070075int bpf_check_uarg_tail_zero(void __user *uaddr,
76 size_t expected_size,
77 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020078{
Al Virob7e4b652020-05-08 00:16:31 -040079 unsigned char __user *addr = uaddr + expected_size;
80 int res;
Mickaël Salaün58291a72017-08-07 20:45:19 +020081
Mickaël Salaün752ba562017-08-07 20:45:20 +020082 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
83 return -E2BIG;
84
Mickaël Salaün58291a72017-08-07 20:45:19 +020085 if (actual_size <= expected_size)
86 return 0;
87
Al Virob7e4b652020-05-08 00:16:31 -040088 res = check_zeroed_user(addr, actual_size - expected_size);
89 if (res < 0)
90 return res;
91 return res ? 0 : -E2BIG;
Mickaël Salaün58291a72017-08-07 20:45:19 +020092}
93
Jakub Kicinskia3884572018-01-11 20:29:09 -080094const struct bpf_map_ops bpf_map_offload_ops = {
Martin KaFai Lauf4d05252020-08-27 18:18:06 -070095 .map_meta_equal = bpf_map_meta_equal,
Jakub Kicinskia3884572018-01-11 20:29:09 -080096 .map_alloc = bpf_map_offload_map_alloc,
97 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020098 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -080099};
100
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700101static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
102{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800103 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100104 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700105 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800106 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700107
Mark Rutland9ef09e32018-05-03 17:04:59 +0100108 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800109 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100110 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
111 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800112 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200113 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700114
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800115 if (ops->map_alloc_check) {
116 err = ops->map_alloc_check(attr);
117 if (err)
118 return ERR_PTR(err);
119 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800120 if (attr->map_ifindex)
121 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800122 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200123 if (IS_ERR(map))
124 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800125 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100126 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200127 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700128}
129
Brian Vazquez15c14a32020-01-15 10:43:00 -0800130static u32 bpf_map_value_size(struct bpf_map *map)
131{
132 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
133 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
134 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
135 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
136 return round_up(map->value_size, 8) * num_possible_cpus();
137 else if (IS_FD_MAP(map))
138 return sizeof(u32);
139 else
140 return map->value_size;
141}
142
143static void maybe_wait_bpf_programs(struct bpf_map *map)
144{
145 /* Wait for any running BPF programs to complete so that
146 * userspace, when we return to it, knows that all programs
147 * that could be running use the new map value.
148 */
149 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
150 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
151 synchronize_rcu();
152}
153
154static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
155 void *value, __u64 flags)
156{
157 int err;
158
159 /* Need to create a kthread, thus must support schedule */
160 if (bpf_map_is_dev_bound(map)) {
161 return bpf_map_offload_update_elem(map, key, value, flags);
162 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
Brian Vazquez15c14a32020-01-15 10:43:00 -0800163 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
164 return map->ops->map_update_elem(map, key, value, flags);
Lorenz Bauer13b79d32020-08-21 11:29:45 +0100165 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
166 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
167 return sock_map_update_elem_sys(map, key, value, flags);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800168 } else if (IS_FD_PROG_ARRAY(map)) {
169 return bpf_fd_array_map_update_elem(map, f.file, key, value,
170 flags);
171 }
172
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100173 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800174 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
175 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
176 err = bpf_percpu_hash_update(map, key, value, flags);
177 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
178 err = bpf_percpu_array_update(map, key, value, flags);
179 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
180 err = bpf_percpu_cgroup_storage_update(map, key, value,
181 flags);
182 } else if (IS_FD_ARRAY(map)) {
183 rcu_read_lock();
184 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
185 flags);
186 rcu_read_unlock();
187 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
188 rcu_read_lock();
189 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
190 flags);
191 rcu_read_unlock();
192 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
193 /* rcu_read_lock() is not needed */
194 err = bpf_fd_reuseport_array_update_elem(map, key, value,
195 flags);
196 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
197 map->map_type == BPF_MAP_TYPE_STACK) {
198 err = map->ops->map_push_elem(map, value, flags);
199 } else {
200 rcu_read_lock();
201 err = map->ops->map_update_elem(map, key, value, flags);
202 rcu_read_unlock();
203 }
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100204 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800205 maybe_wait_bpf_programs(map);
206
207 return err;
208}
209
210static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
211 __u64 flags)
212{
213 void *ptr;
214 int err;
215
Brian Vazquezcb4d03a2020-01-15 10:43:01 -0800216 if (bpf_map_is_dev_bound(map))
217 return bpf_map_offload_lookup_elem(map, key, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800218
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100219 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800220 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
221 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
222 err = bpf_percpu_hash_copy(map, key, value);
223 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
224 err = bpf_percpu_array_copy(map, key, value);
225 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
226 err = bpf_percpu_cgroup_storage_copy(map, key, value);
227 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
228 err = bpf_stackmap_copy(map, key, value);
229 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
230 err = bpf_fd_array_map_lookup_elem(map, key, value);
231 } else if (IS_FD_HASH(map)) {
232 err = bpf_fd_htab_map_lookup_elem(map, key, value);
233 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
234 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
235 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
236 map->map_type == BPF_MAP_TYPE_STACK) {
237 err = map->ops->map_peek_elem(map, value);
238 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
239 /* struct_ops map requires directly updating "value" */
240 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
241 } else {
242 rcu_read_lock();
243 if (map->ops->map_lookup_elem_sys_only)
244 ptr = map->ops->map_lookup_elem_sys_only(map, key);
245 else
246 ptr = map->ops->map_lookup_elem(map, key);
247 if (IS_ERR(ptr)) {
248 err = PTR_ERR(ptr);
249 } else if (!ptr) {
250 err = -ENOENT;
251 } else {
252 err = 0;
253 if (flags & BPF_F_LOCK)
254 /* lock 'ptr' and copy everything but lock */
255 copy_map_value_locked(map, value, ptr, true);
256 else
257 copy_map_value(map, value, ptr);
258 /* mask lock, since value wasn't zero inited */
259 check_and_init_map_lock(map, value);
260 }
261 rcu_read_unlock();
262 }
263
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100264 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800265 maybe_wait_bpf_programs(map);
266
267 return err;
268}
269
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100270static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100271{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100272 /* We really just want to fail instead of triggering OOM killer
273 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
274 * which is used for lower order allocation requests.
275 *
276 * It has been observed that higher order allocation requests done by
277 * vmalloc with __GFP_NORETRY being set might fail due to not trying
278 * to reclaim memory from the page cache, thus we set
279 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100280 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100281
Christoph Hellwig041de932020-06-01 21:52:02 -0700282 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO;
283 unsigned int flags = 0;
284 unsigned long align = 1;
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 */
Christoph Hellwig041de932020-06-01 21:52:02 -0700291 if (mmapable) {
292 BUG_ON(!PAGE_ALIGNED(size));
293 align = SHMLBA;
294 flags = VM_USERMAP;
295 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
296 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100297 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100298 if (area != NULL)
299 return area;
300 }
Christoph Hellwig041de932020-06-01 21:52:02 -0700301
302 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
303 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
304 flags, numa_node, __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
Andrii Nakryikodfeb3762020-05-18 22:38:24 -0700626 if (vma->vm_flags & VM_WRITE) {
627 if (map->frozen) {
628 err = -EPERM;
629 goto out;
630 }
631 /* map is meant to be read-only, so do not allow mapping as
632 * writable, because it's possible to leak a writable page
633 * reference and allows user-space to still modify it after
634 * freezing, while verifier will assume contents do not change
635 */
636 if (map->map_flags & BPF_F_RDONLY_PROG) {
637 err = -EACCES;
638 goto out;
639 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800640 }
641
642 /* set default open/close callbacks */
643 vma->vm_ops = &bpf_map_default_vmops;
644 vma->vm_private_data = map;
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700645 vma->vm_flags &= ~VM_MAYEXEC;
646 if (!(vma->vm_flags & VM_WRITE))
647 /* disallow re-mapping with PROT_WRITE */
648 vma->vm_flags &= ~VM_MAYWRITE;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800649
650 err = map->ops->map_mmap(map, vma);
651 if (err)
652 goto out;
653
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700654 if (vma->vm_flags & VM_MAYWRITE)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800655 map->writecnt++;
656out:
657 mutex_unlock(&map->freeze_mutex);
658 return err;
659}
660
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700661static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
662{
663 struct bpf_map *map = filp->private_data;
664
665 if (map->ops->map_poll)
666 return map->ops->map_poll(map, filp, pts);
667
668 return EPOLLERR;
669}
670
Chenbo Fengf66e4482017-10-18 13:00:26 -0700671const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100672#ifdef CONFIG_PROC_FS
673 .show_fdinfo = bpf_map_show_fdinfo,
674#endif
675 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700676 .read = bpf_dummy_read,
677 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800678 .mmap = bpf_map_mmap,
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700679 .poll = bpf_map_poll,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700680};
681
Chenbo Feng6e71b042017-10-18 13:00:22 -0700682int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100683{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700684 int ret;
685
686 ret = security_bpf_map(map, OPEN_FMODE(flags));
687 if (ret < 0)
688 return ret;
689
Daniel Borkmannaa797812015-10-29 14:58:06 +0100690 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700691 flags | O_CLOEXEC);
692}
693
694int bpf_get_file_flag(int flags)
695{
696 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
697 return -EINVAL;
698 if (flags & BPF_F_RDONLY)
699 return O_RDONLY;
700 if (flags & BPF_F_WRONLY)
701 return O_WRONLY;
702 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100703}
704
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700705/* helper macro to check that unused fields 'union bpf_attr' are zero */
706#define CHECK_ATTR(CMD) \
707 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
708 sizeof(attr->CMD##_LAST_FIELD), 0, \
709 sizeof(*attr) - \
710 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
711 sizeof(attr->CMD##_LAST_FIELD)) != NULL
712
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700713/* dst and src must have at least "size" number of bytes.
714 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700715 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700716int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700717{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700718 const char *end = src + size;
719 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700720
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700721 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200722 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700723 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200724 if (!isalnum(*src) &&
725 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700726 return -EINVAL;
727 *dst++ = *src++;
728 }
729
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700730 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700731 if (src == end)
732 return -EINVAL;
733
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700734 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700735}
736
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200737int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800738 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200739 const struct btf_type *key_type,
740 const struct btf_type *value_type)
741{
742 return -ENOTSUPP;
743}
744
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800745static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200746 u32 btf_key_id, u32 btf_value_id)
747{
748 const struct btf_type *key_type, *value_type;
749 u32 key_size, value_size;
750 int ret = 0;
751
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200752 /* Some maps allow key to be unspecified. */
753 if (btf_key_id) {
754 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
755 if (!key_type || key_size != map->key_size)
756 return -EINVAL;
757 } else {
758 key_type = btf_type_by_id(btf, 0);
759 if (!map->ops->map_check_btf)
760 return -EINVAL;
761 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200762
763 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
764 if (!value_type || value_size != map->value_size)
765 return -EINVAL;
766
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800767 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
768
769 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200770 if (map->map_flags & BPF_F_RDONLY_PROG)
771 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800772 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800773 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700774 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
KP Singh8ea63682020-08-25 20:29:17 +0200775 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
776 map->map_type != BPF_MAP_TYPE_INODE_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800777 return -ENOTSUPP;
778 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
779 map->value_size) {
780 WARN_ONCE(1,
781 "verifier bug spin_lock_off %d value_size %d\n",
782 map->spin_lock_off, map->value_size);
783 return -EFAULT;
784 }
785 }
786
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200787 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800788 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200789
790 return ret;
791}
792
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800793#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700794/* called via syscall */
795static int map_create(union bpf_attr *attr)
796{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700797 int numa_node = bpf_map_attr_numa_node(attr);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700798 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700799 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700800 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700801 int err;
802
803 err = CHECK_ATTR(BPF_MAP_CREATE);
804 if (err)
805 return -EINVAL;
806
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800807 if (attr->btf_vmlinux_value_type_id) {
808 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
809 attr->btf_key_type_id || attr->btf_value_type_id)
810 return -EINVAL;
811 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
812 return -EINVAL;
813 }
814
Chenbo Feng6e71b042017-10-18 13:00:22 -0700815 f_flags = bpf_get_file_flag(attr->map_flags);
816 if (f_flags < 0)
817 return f_flags;
818
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700819 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700820 ((unsigned int)numa_node >= nr_node_ids ||
821 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700822 return -EINVAL;
823
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700824 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
825 map = find_and_alloc_map(attr);
826 if (IS_ERR(map))
827 return PTR_ERR(map);
828
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700829 err = bpf_obj_name_cpy(map->name, attr->map_name,
830 sizeof(attr->map_name));
831 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700832 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700833
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800834 atomic64_set(&map->refcnt, 1);
835 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800836 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700837
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800838 map->spin_lock_off = -EINVAL;
839 if (attr->btf_key_type_id || attr->btf_value_type_id ||
840 /* Even the map's value is a kernel's struct,
841 * the bpf_prog.o must have BTF to begin with
842 * to figure out the corresponding kernel's
843 * counter part. Thus, attr->btf_fd has
844 * to be valid also.
845 */
846 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700847 struct btf *btf;
848
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700849 btf = btf_get_by_fd(attr->btf_fd);
850 if (IS_ERR(btf)) {
851 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700852 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700853 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800854 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700855
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800856 if (attr->btf_value_type_id) {
857 err = map_check_btf(map, btf, attr->btf_key_type_id,
858 attr->btf_value_type_id);
859 if (err)
860 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700861 }
862
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700863 map->btf_key_type_id = attr->btf_key_type_id;
864 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800865 map->btf_vmlinux_value_type_id =
866 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700867 }
868
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700869 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700870 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700871 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700872
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700873 err = bpf_map_alloc_id(map);
874 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700875 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700876
Chenbo Feng6e71b042017-10-18 13:00:22 -0700877 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700878 if (err < 0) {
879 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800880 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700881 * bpf_map_alloc_id() has published the map
882 * to the userspace and the userspace may
883 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
884 */
Peng Sun352d20d2019-02-27 22:36:25 +0800885 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700886 return err;
887 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700888
889 return err;
890
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700891free_map_sec:
892 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700893free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700894 btf_put(map->btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700895 bpf_map_charge_move(&mem, &map->memory);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700896 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700897 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700898 return err;
899}
900
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700901/* if error is returned, fd is released.
902 * On success caller should complete fd access with matching fdput()
903 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100904struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700905{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700906 if (!f.file)
907 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700908 if (f.file->f_op != &bpf_map_fops) {
909 fdput(f);
910 return ERR_PTR(-EINVAL);
911 }
912
Daniel Borkmannc2101292015-10-29 14:58:07 +0100913 return f.file->private_data;
914}
915
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800916void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100917{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800918 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100919}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700920EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100921
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800922void bpf_map_inc_with_uref(struct bpf_map *map)
923{
924 atomic64_inc(&map->refcnt);
925 atomic64_inc(&map->usercnt);
926}
927EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
928
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -0800929struct bpf_map *bpf_map_get(u32 ufd)
930{
931 struct fd f = fdget(ufd);
932 struct bpf_map *map;
933
934 map = __bpf_map_get(f);
935 if (IS_ERR(map))
936 return map;
937
938 bpf_map_inc(map);
939 fdput(f);
940
941 return map;
942}
943
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100944struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100945{
946 struct fd f = fdget(ufd);
947 struct bpf_map *map;
948
949 map = __bpf_map_get(f);
950 if (IS_ERR(map))
951 return map;
952
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800953 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100954 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700955
956 return map;
957}
958
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700959/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800960static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700961{
962 int refold;
963
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800964 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700965 if (!refold)
966 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700967 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800968 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700969
970 return map;
971}
972
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800973struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700974{
975 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800976 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700977 spin_unlock_bh(&map_idr_lock);
978
979 return map;
980}
981EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
982
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800983int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
984{
985 return -ENOTSUPP;
986}
987
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200988static void *__bpf_copy_key(void __user *ukey, u64 key_size)
989{
990 if (key_size)
991 return memdup_user(ukey, key_size);
992
993 if (ukey)
994 return ERR_PTR(-EINVAL);
995
996 return NULL;
997}
998
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700999/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001000#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001001
1002static int map_lookup_elem(union bpf_attr *attr)
1003{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001004 void __user *ukey = u64_to_user_ptr(attr->key);
1005 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001006 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001007 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -08001008 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001009 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001010 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001011 int err;
1012
1013 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1014 return -EINVAL;
1015
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001016 if (attr->flags & ~BPF_F_LOCK)
1017 return -EINVAL;
1018
Daniel Borkmann592867b2015-09-08 18:00:09 +02001019 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001020 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001021 if (IS_ERR(map))
1022 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001023 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001024 err = -EPERM;
1025 goto err_put;
1026 }
1027
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001028 if ((attr->flags & BPF_F_LOCK) &&
1029 !map_value_has_spin_lock(map)) {
1030 err = -EINVAL;
1031 goto err_put;
1032 }
1033
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001034 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001035 if (IS_ERR(key)) {
1036 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001037 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001038 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001039
Brian Vazquez15c14a32020-01-15 10:43:00 -08001040 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001041
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001042 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001043 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001044 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001045 goto free_key;
1046
Brian Vazquez15c14a32020-01-15 10:43:00 -08001047 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001048 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001049 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001050
1051 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001052 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001053 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001054
1055 err = 0;
1056
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001057free_value:
1058 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001059free_key:
1060 kfree(key);
1061err_put:
1062 fdput(f);
1063 return err;
1064}
1065
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001066
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001067#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001068
1069static int map_update_elem(union bpf_attr *attr)
1070{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001071 void __user *ukey = u64_to_user_ptr(attr->key);
1072 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001073 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001074 struct bpf_map *map;
1075 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001076 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001077 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001078 int err;
1079
1080 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1081 return -EINVAL;
1082
Daniel Borkmann592867b2015-09-08 18:00:09 +02001083 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001084 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001085 if (IS_ERR(map))
1086 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001087 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001088 err = -EPERM;
1089 goto err_put;
1090 }
1091
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001092 if ((attr->flags & BPF_F_LOCK) &&
1093 !map_value_has_spin_lock(map)) {
1094 err = -EINVAL;
1095 goto err_put;
1096 }
1097
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001098 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001099 if (IS_ERR(key)) {
1100 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001101 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001102 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001103
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001104 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001105 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +00001106 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1107 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001108 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1109 else
1110 value_size = map->value_size;
1111
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001112 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001113 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001114 if (!value)
1115 goto free_key;
1116
1117 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001118 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001119 goto free_value;
1120
Brian Vazquez15c14a32020-01-15 10:43:00 -08001121 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001122
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001123free_value:
1124 kfree(value);
1125free_key:
1126 kfree(key);
1127err_put:
1128 fdput(f);
1129 return err;
1130}
1131
1132#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1133
1134static int map_delete_elem(union bpf_attr *attr)
1135{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001136 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001137 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001138 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001139 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001140 void *key;
1141 int err;
1142
1143 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1144 return -EINVAL;
1145
Daniel Borkmann592867b2015-09-08 18:00:09 +02001146 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001147 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001148 if (IS_ERR(map))
1149 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001150 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001151 err = -EPERM;
1152 goto err_put;
1153 }
1154
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001155 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001156 if (IS_ERR(key)) {
1157 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001158 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001159 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001160
Jakub Kicinskia3884572018-01-11 20:29:09 -08001161 if (bpf_map_is_dev_bound(map)) {
1162 err = bpf_map_offload_delete_elem(map, key);
1163 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001164 } else if (IS_FD_PROG_ARRAY(map) ||
1165 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1166 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001167 err = map->ops->map_delete_elem(map, key);
1168 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001169 }
1170
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001171 bpf_disable_instrumentation();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001172 rcu_read_lock();
1173 err = map->ops->map_delete_elem(map, key);
1174 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001175 bpf_enable_instrumentation();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001176 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001177out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001178 kfree(key);
1179err_put:
1180 fdput(f);
1181 return err;
1182}
1183
1184/* last field in 'union bpf_attr' used by this command */
1185#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1186
1187static int map_get_next_key(union bpf_attr *attr)
1188{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001189 void __user *ukey = u64_to_user_ptr(attr->key);
1190 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001191 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001192 struct bpf_map *map;
1193 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001194 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001195 int err;
1196
1197 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1198 return -EINVAL;
1199
Daniel Borkmann592867b2015-09-08 18:00:09 +02001200 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001201 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001202 if (IS_ERR(map))
1203 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001204 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001205 err = -EPERM;
1206 goto err_put;
1207 }
1208
Teng Qin8fe45922017-04-24 19:00:37 -07001209 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001210 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001211 if (IS_ERR(key)) {
1212 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001213 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001214 }
Teng Qin8fe45922017-04-24 19:00:37 -07001215 } else {
1216 key = NULL;
1217 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001218
1219 err = -ENOMEM;
1220 next_key = kmalloc(map->key_size, GFP_USER);
1221 if (!next_key)
1222 goto free_key;
1223
Jakub Kicinskia3884572018-01-11 20:29:09 -08001224 if (bpf_map_is_dev_bound(map)) {
1225 err = bpf_map_offload_get_next_key(map, key, next_key);
1226 goto out;
1227 }
1228
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001229 rcu_read_lock();
1230 err = map->ops->map_get_next_key(map, key, next_key);
1231 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001232out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001233 if (err)
1234 goto free_next_key;
1235
1236 err = -EFAULT;
1237 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1238 goto free_next_key;
1239
1240 err = 0;
1241
1242free_next_key:
1243 kfree(next_key);
1244free_key:
1245 kfree(key);
1246err_put:
1247 fdput(f);
1248 return err;
1249}
1250
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001251int generic_map_delete_batch(struct bpf_map *map,
1252 const union bpf_attr *attr,
1253 union bpf_attr __user *uattr)
1254{
1255 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1256 u32 cp, max_count;
1257 int err = 0;
1258 void *key;
1259
1260 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1261 return -EINVAL;
1262
1263 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1264 !map_value_has_spin_lock(map)) {
1265 return -EINVAL;
1266 }
1267
1268 max_count = attr->batch.count;
1269 if (!max_count)
1270 return 0;
1271
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001272 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1273 if (!key)
1274 return -ENOMEM;
1275
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001276 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001277 err = -EFAULT;
1278 if (copy_from_user(key, keys + cp * map->key_size,
1279 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001280 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001281
1282 if (bpf_map_is_dev_bound(map)) {
1283 err = bpf_map_offload_delete_elem(map, key);
1284 break;
1285 }
1286
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001287 bpf_disable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001288 rcu_read_lock();
1289 err = map->ops->map_delete_elem(map, key);
1290 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001291 bpf_enable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001292 maybe_wait_bpf_programs(map);
1293 if (err)
1294 break;
1295 }
1296 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1297 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001298
1299 kfree(key);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001300 return err;
1301}
1302
1303int generic_map_update_batch(struct bpf_map *map,
1304 const union bpf_attr *attr,
1305 union bpf_attr __user *uattr)
1306{
1307 void __user *values = u64_to_user_ptr(attr->batch.values);
1308 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1309 u32 value_size, cp, max_count;
1310 int ufd = attr->map_fd;
1311 void *key, *value;
1312 struct fd f;
1313 int err = 0;
1314
1315 f = fdget(ufd);
1316 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1317 return -EINVAL;
1318
1319 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1320 !map_value_has_spin_lock(map)) {
1321 return -EINVAL;
1322 }
1323
1324 value_size = bpf_map_value_size(map);
1325
1326 max_count = attr->batch.count;
1327 if (!max_count)
1328 return 0;
1329
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001330 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1331 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001332 return -ENOMEM;
1333
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001334 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1335 if (!value) {
1336 kfree(key);
1337 return -ENOMEM;
1338 }
1339
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001340 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001341 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001342 if (copy_from_user(key, keys + cp * map->key_size,
1343 map->key_size) ||
1344 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001345 break;
1346
1347 err = bpf_map_update_value(map, f, key, value,
1348 attr->batch.elem_flags);
1349
1350 if (err)
1351 break;
1352 }
1353
1354 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1355 err = -EFAULT;
1356
1357 kfree(value);
1358 kfree(key);
1359 return err;
1360}
1361
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001362#define MAP_LOOKUP_RETRIES 3
1363
1364int generic_map_lookup_batch(struct bpf_map *map,
1365 const union bpf_attr *attr,
1366 union bpf_attr __user *uattr)
1367{
1368 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1369 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1370 void __user *values = u64_to_user_ptr(attr->batch.values);
1371 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1372 void *buf, *buf_prevkey, *prev_key, *key, *value;
1373 int err, retry = MAP_LOOKUP_RETRIES;
1374 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001375
1376 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1377 return -EINVAL;
1378
1379 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1380 !map_value_has_spin_lock(map))
1381 return -EINVAL;
1382
1383 value_size = bpf_map_value_size(map);
1384
1385 max_count = attr->batch.count;
1386 if (!max_count)
1387 return 0;
1388
1389 if (put_user(0, &uattr->batch.count))
1390 return -EFAULT;
1391
1392 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1393 if (!buf_prevkey)
1394 return -ENOMEM;
1395
1396 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1397 if (!buf) {
Denis Efremovbb2359f2020-06-01 19:28:14 +03001398 kfree(buf_prevkey);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001399 return -ENOMEM;
1400 }
1401
1402 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001403 prev_key = NULL;
1404 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1405 goto free_buf;
1406 key = buf;
1407 value = key + map->key_size;
1408 if (ubatch)
1409 prev_key = buf_prevkey;
1410
1411 for (cp = 0; cp < max_count;) {
1412 rcu_read_lock();
1413 err = map->ops->map_get_next_key(map, prev_key, key);
1414 rcu_read_unlock();
1415 if (err)
1416 break;
1417 err = bpf_map_copy_value(map, key, value,
1418 attr->batch.elem_flags);
1419
1420 if (err == -ENOENT) {
1421 if (retry) {
1422 retry--;
1423 continue;
1424 }
1425 err = -EINTR;
1426 break;
1427 }
1428
1429 if (err)
1430 goto free_buf;
1431
1432 if (copy_to_user(keys + cp * map->key_size, key,
1433 map->key_size)) {
1434 err = -EFAULT;
1435 goto free_buf;
1436 }
1437 if (copy_to_user(values + cp * value_size, value, value_size)) {
1438 err = -EFAULT;
1439 goto free_buf;
1440 }
1441
1442 if (!prev_key)
1443 prev_key = buf_prevkey;
1444
1445 swap(prev_key, key);
1446 retry = MAP_LOOKUP_RETRIES;
1447 cp++;
1448 }
1449
1450 if (err == -EFAULT)
1451 goto free_buf;
1452
1453 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1454 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1455 err = -EFAULT;
1456
1457free_buf:
1458 kfree(buf_prevkey);
1459 kfree(buf);
1460 return err;
1461}
1462
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001463#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1464
1465static int map_lookup_and_delete_elem(union bpf_attr *attr)
1466{
1467 void __user *ukey = u64_to_user_ptr(attr->key);
1468 void __user *uvalue = u64_to_user_ptr(attr->value);
1469 int ufd = attr->map_fd;
1470 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001471 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001472 u32 value_size;
1473 struct fd f;
1474 int err;
1475
1476 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1477 return -EINVAL;
1478
1479 f = fdget(ufd);
1480 map = __bpf_map_get(f);
1481 if (IS_ERR(map))
1482 return PTR_ERR(map);
Anton Protopopov1ea0f912020-05-27 18:56:59 +00001483 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1484 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001485 err = -EPERM;
1486 goto err_put;
1487 }
1488
1489 key = __bpf_copy_key(ukey, map->key_size);
1490 if (IS_ERR(key)) {
1491 err = PTR_ERR(key);
1492 goto err_put;
1493 }
1494
1495 value_size = map->value_size;
1496
1497 err = -ENOMEM;
1498 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1499 if (!value)
1500 goto free_key;
1501
1502 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1503 map->map_type == BPF_MAP_TYPE_STACK) {
1504 err = map->ops->map_pop_elem(map, value);
1505 } else {
1506 err = -ENOTSUPP;
1507 }
1508
1509 if (err)
1510 goto free_value;
1511
Wei Yongjun7f645462020-04-30 08:18:51 +00001512 if (copy_to_user(uvalue, value, value_size) != 0) {
1513 err = -EFAULT;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001514 goto free_value;
Wei Yongjun7f645462020-04-30 08:18:51 +00001515 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001516
1517 err = 0;
1518
1519free_value:
1520 kfree(value);
1521free_key:
1522 kfree(key);
1523err_put:
1524 fdput(f);
1525 return err;
1526}
1527
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001528#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1529
1530static int map_freeze(const union bpf_attr *attr)
1531{
1532 int err = 0, ufd = attr->map_fd;
1533 struct bpf_map *map;
1534 struct fd f;
1535
1536 if (CHECK_ATTR(BPF_MAP_FREEZE))
1537 return -EINVAL;
1538
1539 f = fdget(ufd);
1540 map = __bpf_map_get(f);
1541 if (IS_ERR(map))
1542 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001543
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001544 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1545 fdput(f);
1546 return -ENOTSUPP;
1547 }
1548
Andrii Nakryikofc970222019-11-17 09:28:04 -08001549 mutex_lock(&map->freeze_mutex);
1550
1551 if (map->writecnt) {
1552 err = -EBUSY;
1553 goto err_put;
1554 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001555 if (READ_ONCE(map->frozen)) {
1556 err = -EBUSY;
1557 goto err_put;
1558 }
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07001559 if (!bpf_capable()) {
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001560 err = -EPERM;
1561 goto err_put;
1562 }
1563
1564 WRITE_ONCE(map->frozen, true);
1565err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001566 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001567 fdput(f);
1568 return err;
1569}
1570
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001571static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001572#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001573 [_id] = & _name ## _prog_ops,
1574#define BPF_MAP_TYPE(_id, _ops)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001575#define BPF_LINK_TYPE(_id, _name)
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001576#include <linux/bpf_types.h>
1577#undef BPF_PROG_TYPE
1578#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001579#undef BPF_LINK_TYPE
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001580};
1581
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001582static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1583{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001584 const struct bpf_prog_ops *ops;
1585
1586 if (type >= ARRAY_SIZE(bpf_prog_types))
1587 return -EINVAL;
1588 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1589 ops = bpf_prog_types[type];
1590 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001591 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001592
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001593 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001594 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001595 else
1596 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001597 prog->type = type;
1598 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001599}
1600
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001601enum bpf_audit {
1602 BPF_AUDIT_LOAD,
1603 BPF_AUDIT_UNLOAD,
1604 BPF_AUDIT_MAX,
1605};
1606
1607static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1608 [BPF_AUDIT_LOAD] = "LOAD",
1609 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1610};
1611
1612static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1613{
1614 struct audit_context *ctx = NULL;
1615 struct audit_buffer *ab;
1616
1617 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1618 return;
1619 if (audit_enabled == AUDIT_OFF)
1620 return;
1621 if (op == BPF_AUDIT_LOAD)
1622 ctx = audit_context();
1623 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1624 if (unlikely(!ab))
1625 return;
1626 audit_log_format(ab, "prog-id=%u op=%s",
1627 prog->aux->id, bpf_audit_str[op]);
1628 audit_log_end(ab);
1629}
1630
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001631int __bpf_prog_charge(struct user_struct *user, u32 pages)
1632{
1633 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1634 unsigned long user_bufs;
1635
1636 if (user) {
1637 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1638 if (user_bufs > memlock_limit) {
1639 atomic_long_sub(pages, &user->locked_vm);
1640 return -EPERM;
1641 }
1642 }
1643
1644 return 0;
1645}
1646
1647void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1648{
1649 if (user)
1650 atomic_long_sub(pages, &user->locked_vm);
1651}
1652
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001653static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1654{
1655 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001656 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001657
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001658 ret = __bpf_prog_charge(user, prog->pages);
1659 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001660 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001661 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001662 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001663
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001664 prog->aux->user = user;
1665 return 0;
1666}
1667
1668static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1669{
1670 struct user_struct *user = prog->aux->user;
1671
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001672 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001673 free_uid(user);
1674}
1675
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001676static int bpf_prog_alloc_id(struct bpf_prog *prog)
1677{
1678 int id;
1679
Shaohua Lib76354c2018-03-27 11:53:21 -07001680 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001681 spin_lock_bh(&prog_idr_lock);
1682 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1683 if (id > 0)
1684 prog->aux->id = id;
1685 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001686 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001687
1688 /* id is in [1, INT_MAX) */
1689 if (WARN_ON_ONCE(!id))
1690 return -ENOSPC;
1691
1692 return id > 0 ? 0 : id;
1693}
1694
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001695void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001696{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001697 /* cBPF to eBPF migrations are currently not in the idr store.
1698 * Offloaded programs are removed from the store when their device
1699 * disappears - even if someone grabs an fd to them they are unusable,
1700 * simply waiting for refcnt to drop to be freed.
1701 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001702 if (!prog->aux->id)
1703 return;
1704
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001705 if (do_idr_lock)
1706 spin_lock_bh(&prog_idr_lock);
1707 else
1708 __acquire(&prog_idr_lock);
1709
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001710 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001711 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001712
1713 if (do_idr_lock)
1714 spin_unlock_bh(&prog_idr_lock);
1715 else
1716 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001717}
1718
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001719static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001720{
1721 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1722
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001723 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001724 kfree(aux->func_info_aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001725 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001726 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001727 bpf_prog_free(aux->prog);
1728}
1729
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001730static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1731{
1732 bpf_prog_kallsyms_del_all(prog);
1733 btf_put(prog->aux->btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001734 bpf_prog_free_linfo(prog);
1735
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001736 if (deferred) {
1737 if (prog->aux->sleepable)
1738 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1739 else
1740 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1741 } else {
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001742 __bpf_prog_put_rcu(&prog->aux->rcu);
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001743 }
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001744}
1745
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001746static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001747{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001748 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001749 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001750 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001751 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001752 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001753 __bpf_prog_put_noref(prog, true);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001754 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001755}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001756
1757void bpf_prog_put(struct bpf_prog *prog)
1758{
1759 __bpf_prog_put(prog, true);
1760}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001761EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001762
1763static int bpf_prog_release(struct inode *inode, struct file *filp)
1764{
1765 struct bpf_prog *prog = filp->private_data;
1766
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001767 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001768 return 0;
1769}
1770
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001771static void bpf_prog_get_stats(const struct bpf_prog *prog,
1772 struct bpf_prog_stats *stats)
1773{
1774 u64 nsecs = 0, cnt = 0;
1775 int cpu;
1776
1777 for_each_possible_cpu(cpu) {
1778 const struct bpf_prog_stats *st;
1779 unsigned int start;
1780 u64 tnsecs, tcnt;
1781
1782 st = per_cpu_ptr(prog->aux->stats, cpu);
1783 do {
1784 start = u64_stats_fetch_begin_irq(&st->syncp);
1785 tnsecs = st->nsecs;
1786 tcnt = st->cnt;
1787 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1788 nsecs += tnsecs;
1789 cnt += tcnt;
1790 }
1791 stats->nsecs = nsecs;
1792 stats->cnt = cnt;
1793}
1794
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001795#ifdef CONFIG_PROC_FS
1796static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1797{
1798 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001799 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001800 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001801
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001802 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001803 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001804 seq_printf(m,
1805 "prog_type:\t%u\n"
1806 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001807 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001808 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001809 "prog_id:\t%u\n"
1810 "run_time_ns:\t%llu\n"
1811 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001812 prog->type,
1813 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001814 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001815 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001816 prog->aux->id,
1817 stats.nsecs,
1818 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001819}
1820#endif
1821
Chenbo Fengf66e4482017-10-18 13:00:26 -07001822const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001823#ifdef CONFIG_PROC_FS
1824 .show_fdinfo = bpf_prog_show_fdinfo,
1825#endif
1826 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001827 .read = bpf_dummy_read,
1828 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001829};
1830
Daniel Borkmannb2197752015-10-29 14:58:09 +01001831int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001832{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001833 int ret;
1834
1835 ret = security_bpf_prog(prog);
1836 if (ret < 0)
1837 return ret;
1838
Daniel Borkmannaa797812015-10-29 14:58:06 +01001839 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1840 O_RDWR | O_CLOEXEC);
1841}
1842
Daniel Borkmann113214b2016-06-30 17:24:44 +02001843static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001844{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001845 if (!f.file)
1846 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001847 if (f.file->f_op != &bpf_prog_fops) {
1848 fdput(f);
1849 return ERR_PTR(-EINVAL);
1850 }
1851
Daniel Borkmannc2101292015-10-29 14:58:07 +01001852 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001853}
1854
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001855void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001856{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001857 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001858}
Brenden Blanco59d36562016-07-19 12:16:46 -07001859EXPORT_SYMBOL_GPL(bpf_prog_add);
1860
Daniel Borkmannc5405942016-11-09 22:02:34 +01001861void bpf_prog_sub(struct bpf_prog *prog, int i)
1862{
1863 /* Only to be used for undoing previous bpf_prog_add() in some
1864 * error path. We still know that another entity in our call
1865 * path holds a reference to the program, thus atomic_sub() can
1866 * be safely used in such cases!
1867 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001868 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001869}
1870EXPORT_SYMBOL_GPL(bpf_prog_sub);
1871
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001872void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001873{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001874 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001875}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001876EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001877
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001878/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001879struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001880{
1881 int refold;
1882
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001883 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001884
1885 if (!refold)
1886 return ERR_PTR(-ENOENT);
1887
1888 return prog;
1889}
John Fastabenda6f6df62017-08-15 22:32:22 -07001890EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001891
Al Viro040ee692017-12-02 20:20:38 -05001892bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001893 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001894{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001895 /* not an attachment, just a refcount inc, always allow */
1896 if (!attach_type)
1897 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001898
1899 if (prog->type != *attach_type)
1900 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001901 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001902 return false;
1903
1904 return true;
1905}
1906
1907static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001908 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001909{
1910 struct fd f = fdget(ufd);
1911 struct bpf_prog *prog;
1912
Daniel Borkmann113214b2016-06-30 17:24:44 +02001913 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001914 if (IS_ERR(prog))
1915 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001916 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001917 prog = ERR_PTR(-EINVAL);
1918 goto out;
1919 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001920
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001921 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001922out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001923 fdput(f);
1924 return prog;
1925}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001926
1927struct bpf_prog *bpf_prog_get(u32 ufd)
1928{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001929 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001930}
1931
Jakub Kicinski248f3462017-11-03 13:56:20 -07001932struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001933 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001934{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001935 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001936}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001937EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001938
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001939/* Initially all BPF programs could be loaded w/o specifying
1940 * expected_attach_type. Later for some of them specifying expected_attach_type
1941 * at load time became required so that program could be validated properly.
1942 * Programs of types that are allowed to be loaded both w/ and w/o (for
1943 * backward compatibility) expected_attach_type, should have the default attach
1944 * type assigned to expected_attach_type for the latter case, so that it can be
1945 * validated later at attach time.
1946 *
1947 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1948 * prog type requires it but has some attach types that have to be backward
1949 * compatible.
1950 */
1951static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1952{
1953 switch (attr->prog_type) {
1954 case BPF_PROG_TYPE_CGROUP_SOCK:
1955 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1956 * exist so checking for non-zero is the way to go here.
1957 */
1958 if (!attr->expected_attach_type)
1959 attr->expected_attach_type =
1960 BPF_CGROUP_INET_SOCK_CREATE;
1961 break;
1962 }
1963}
1964
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001965static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001966bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1967 enum bpf_attach_type expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001968 u32 btf_id, u32 prog_fd)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001969{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001970 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001971 if (btf_id > BTF_MAX_TYPE)
1972 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001973
1974 switch (prog_type) {
1975 case BPF_PROG_TYPE_TRACING:
KP Singh9e4e01d2020-03-29 01:43:52 +01001976 case BPF_PROG_TYPE_LSM:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001977 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001978 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001979 break;
1980 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001981 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001982 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001983 }
1984
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001985 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1986 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001987 return -EINVAL;
1988
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001989 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001990 case BPF_PROG_TYPE_CGROUP_SOCK:
1991 switch (expected_attach_type) {
1992 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07001993 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001994 case BPF_CGROUP_INET4_POST_BIND:
1995 case BPF_CGROUP_INET6_POST_BIND:
1996 return 0;
1997 default:
1998 return -EINVAL;
1999 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002000 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2001 switch (expected_attach_type) {
2002 case BPF_CGROUP_INET4_BIND:
2003 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002004 case BPF_CGROUP_INET4_CONNECT:
2005 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002006 case BPF_CGROUP_INET4_GETPEERNAME:
2007 case BPF_CGROUP_INET6_GETPEERNAME:
2008 case BPF_CGROUP_INET4_GETSOCKNAME:
2009 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002010 case BPF_CGROUP_UDP4_SENDMSG:
2011 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002012 case BPF_CGROUP_UDP4_RECVMSG:
2013 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002014 return 0;
2015 default:
2016 return -EINVAL;
2017 }
brakmo5cf1e912019-05-28 16:59:36 -07002018 case BPF_PROG_TYPE_CGROUP_SKB:
2019 switch (expected_attach_type) {
2020 case BPF_CGROUP_INET_INGRESS:
2021 case BPF_CGROUP_INET_EGRESS:
2022 return 0;
2023 default:
2024 return -EINVAL;
2025 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002026 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2027 switch (expected_attach_type) {
2028 case BPF_CGROUP_SETSOCKOPT:
2029 case BPF_CGROUP_GETSOCKOPT:
2030 return 0;
2031 default:
2032 return -EINVAL;
2033 }
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002034 case BPF_PROG_TYPE_SK_LOOKUP:
2035 if (expected_attach_type == BPF_SK_LOOKUP)
2036 return 0;
2037 return -EINVAL;
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002038 case BPF_PROG_TYPE_EXT:
2039 if (expected_attach_type)
2040 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002041 fallthrough;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002042 default:
2043 return 0;
2044 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002045}
2046
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002047static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2048{
2049 switch (prog_type) {
2050 case BPF_PROG_TYPE_SCHED_CLS:
2051 case BPF_PROG_TYPE_SCHED_ACT:
2052 case BPF_PROG_TYPE_XDP:
2053 case BPF_PROG_TYPE_LWT_IN:
2054 case BPF_PROG_TYPE_LWT_OUT:
2055 case BPF_PROG_TYPE_LWT_XMIT:
2056 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2057 case BPF_PROG_TYPE_SK_SKB:
2058 case BPF_PROG_TYPE_SK_MSG:
2059 case BPF_PROG_TYPE_LIRC_MODE2:
2060 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2061 case BPF_PROG_TYPE_CGROUP_DEVICE:
2062 case BPF_PROG_TYPE_CGROUP_SOCK:
2063 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2064 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2065 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2066 case BPF_PROG_TYPE_SOCK_OPS:
2067 case BPF_PROG_TYPE_EXT: /* extends any prog */
2068 return true;
2069 case BPF_PROG_TYPE_CGROUP_SKB:
2070 /* always unpriv */
2071 case BPF_PROG_TYPE_SK_REUSEPORT:
2072 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2073 default:
2074 return false;
2075 }
2076}
2077
2078static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2079{
2080 switch (prog_type) {
2081 case BPF_PROG_TYPE_KPROBE:
2082 case BPF_PROG_TYPE_TRACEPOINT:
2083 case BPF_PROG_TYPE_PERF_EVENT:
2084 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2085 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2086 case BPF_PROG_TYPE_TRACING:
2087 case BPF_PROG_TYPE_LSM:
2088 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2089 case BPF_PROG_TYPE_EXT: /* extends any prog */
2090 return true;
2091 default:
2092 return false;
2093 }
2094}
2095
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002096/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002097#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002098
Yonghong Song838e9692018-11-19 15:29:11 -08002099static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002100{
2101 enum bpf_prog_type type = attr->prog_type;
2102 struct bpf_prog *prog;
2103 int err;
2104 char license[128];
2105 bool is_gpl;
2106
2107 if (CHECK_ATTR(BPF_PROG_LOAD))
2108 return -EINVAL;
2109
Jiong Wangc240eff2019-05-24 23:25:16 +01002110 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2111 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002112 BPF_F_TEST_STATE_FREQ |
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002113 BPF_F_SLEEPABLE |
Jiong Wangc240eff2019-05-24 23:25:16 +01002114 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07002115 return -EINVAL;
2116
David Millere9ee9ef2018-11-30 21:08:14 -08002117 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2118 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002119 !bpf_capable())
David Millere9ee9ef2018-11-30 21:08:14 -08002120 return -EPERM;
2121
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002122 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002123 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002124 sizeof(license) - 1) < 0)
2125 return -EFAULT;
2126 license[sizeof(license) - 1] = 0;
2127
2128 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2129 is_gpl = license_is_gpl_compatible(license);
2130
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002131 if (attr->insn_cnt == 0 ||
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002132 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002133 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002134 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2135 type != BPF_PROG_TYPE_CGROUP_SKB &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002136 !bpf_capable())
2137 return -EPERM;
2138
Maciej Żenczykowskib338cb92020-06-20 14:26:16 -07002139 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002140 return -EPERM;
2141 if (is_perfmon_prog_type(type) && !perfmon_capable())
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002142 return -EPERM;
2143
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002144 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002145 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002146 attr->attach_btf_id,
2147 attr->attach_prog_fd))
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002148 return -EINVAL;
2149
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002150 /* plain bpf_prog allocation */
2151 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2152 if (!prog)
2153 return -ENOMEM;
2154
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002155 prog->expected_attach_type = attr->expected_attach_type;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002156 prog->aux->attach_btf_id = attr->attach_btf_id;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002157 if (attr->attach_prog_fd) {
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002158 struct bpf_prog *dst_prog;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002159
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002160 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2161 if (IS_ERR(dst_prog)) {
2162 err = PTR_ERR(dst_prog);
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002163 goto free_prog_nouncharge;
2164 }
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002165 prog->aux->dst_prog = dst_prog;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002166 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002167
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002168 prog->aux->offload_requested = !!attr->prog_ifindex;
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002169 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002170
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002171 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002172 if (err)
2173 goto free_prog_nouncharge;
2174
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002175 err = bpf_prog_charge_memlock(prog);
2176 if (err)
2177 goto free_prog_sec;
2178
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002179 prog->len = attr->insn_cnt;
2180
2181 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002182 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01002183 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002184 goto free_prog;
2185
2186 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002187 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002188
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002189 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002190 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002191
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002192 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002193 err = bpf_prog_offload_init(prog, attr);
2194 if (err)
2195 goto free_prog;
2196 }
2197
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002198 /* find program type: socket_filter vs tracing_filter */
2199 err = find_prog_type(type, prog);
2200 if (err < 0)
2201 goto free_prog;
2202
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002203 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002204 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2205 sizeof(attr->prog_name));
2206 if (err < 0)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002207 goto free_prog;
2208
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002209 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002210 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002211 if (err < 0)
2212 goto free_used_maps;
2213
Daniel Borkmann9facc332018-06-15 02:30:48 +02002214 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002215 if (err < 0)
2216 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002217
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002218 err = bpf_prog_alloc_id(prog);
2219 if (err)
2220 goto free_used_maps;
2221
Daniel Borkmannc7517982019-08-23 22:14:23 +02002222 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2223 * effectively publicly exposed. However, retrieving via
2224 * bpf_prog_get_fd_by_id() will take another reference,
2225 * therefore it cannot be gone underneath us.
2226 *
2227 * Only for the time /after/ successful bpf_prog_new_fd()
2228 * and before returning to userspace, we might just hold
2229 * one reference and any parallel close on that fd could
2230 * rip everything out. Hence, below notifications must
2231 * happen before bpf_prog_new_fd().
2232 *
2233 * Also, any failure handling from this point onwards must
2234 * be using bpf_prog_put() given the program is exposed.
2235 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002236 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002237 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002238 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002239
2240 err = bpf_prog_new_fd(prog);
2241 if (err < 0)
2242 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002243 return err;
2244
2245free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002246 /* In case we have subprogs, we need to wait for a grace
2247 * period before we can tear down JIT memory since symbols
2248 * are already exposed under kallsyms.
2249 */
2250 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2251 return err;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002252free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002253 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002254free_prog_sec:
2255 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002256free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002257 bpf_prog_free(prog);
2258 return err;
2259}
2260
Chenbo Feng6e71b042017-10-18 13:00:22 -07002261#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002262
2263static int bpf_obj_pin(const union bpf_attr *attr)
2264{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002265 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002266 return -EINVAL;
2267
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002268 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002269}
2270
2271static int bpf_obj_get(const union bpf_attr *attr)
2272{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002273 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2274 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002275 return -EINVAL;
2276
Chenbo Feng6e71b042017-10-18 13:00:22 -07002277 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2278 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002279}
2280
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002281void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002282 const struct bpf_link_ops *ops, struct bpf_prog *prog)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002283{
2284 atomic64_set(&link->refcnt, 1);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002285 link->type = type;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002286 link->id = 0;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002287 link->ops = ops;
2288 link->prog = prog;
2289}
2290
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002291static void bpf_link_free_id(int id)
2292{
2293 if (!id)
2294 return;
2295
2296 spin_lock_bh(&link_idr_lock);
2297 idr_remove(&link_idr, id);
2298 spin_unlock_bh(&link_idr_lock);
2299}
2300
Andrii Nakryiko98868662020-03-12 17:21:28 -07002301/* Clean up bpf_link and corresponding anon_inode file and FD. After
2302 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002303 * anon_inode's release() call. This helper marksbpf_link as
2304 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2305 * is not decremented, it's the responsibility of a calling code that failed
2306 * to complete bpf_link initialization.
Andrii Nakryiko98868662020-03-12 17:21:28 -07002307 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002308void bpf_link_cleanup(struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002309{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002310 primer->link->prog = NULL;
2311 bpf_link_free_id(primer->id);
2312 fput(primer->file);
2313 put_unused_fd(primer->fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002314}
2315
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002316void bpf_link_inc(struct bpf_link *link)
2317{
2318 atomic64_inc(&link->refcnt);
2319}
2320
2321/* bpf_link_free is guaranteed to be called from process context */
2322static void bpf_link_free(struct bpf_link *link)
2323{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002324 bpf_link_free_id(link->id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002325 if (link->prog) {
2326 /* detach BPF program, clean up used resources */
2327 link->ops->release(link);
2328 bpf_prog_put(link->prog);
2329 }
2330 /* free bpf_link and its containing memory */
2331 link->ops->dealloc(link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002332}
2333
2334static void bpf_link_put_deferred(struct work_struct *work)
2335{
2336 struct bpf_link *link = container_of(work, struct bpf_link, work);
2337
2338 bpf_link_free(link);
2339}
2340
2341/* bpf_link_put can be called from atomic context, but ensures that resources
2342 * are freed from process context
2343 */
2344void bpf_link_put(struct bpf_link *link)
2345{
2346 if (!atomic64_dec_and_test(&link->refcnt))
2347 return;
2348
Alexei Starovoitovf00f2f72020-09-23 19:10:38 -07002349 if (in_atomic()) {
2350 INIT_WORK(&link->work, bpf_link_put_deferred);
2351 schedule_work(&link->work);
2352 } else {
2353 bpf_link_free(link);
2354 }
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002355}
2356
2357static int bpf_link_release(struct inode *inode, struct file *filp)
2358{
2359 struct bpf_link *link = filp->private_data;
2360
2361 bpf_link_put(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002362 return 0;
2363}
2364
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002365#ifdef CONFIG_PROC_FS
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002366#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2367#define BPF_MAP_TYPE(_id, _ops)
2368#define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2369static const char *bpf_link_type_strs[] = {
2370 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2371#include <linux/bpf_types.h>
2372};
2373#undef BPF_PROG_TYPE
2374#undef BPF_MAP_TYPE
2375#undef BPF_LINK_TYPE
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002376
2377static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2378{
2379 const struct bpf_link *link = filp->private_data;
2380 const struct bpf_prog *prog = link->prog;
2381 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002382
2383 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2384 seq_printf(m,
2385 "link_type:\t%s\n"
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002386 "link_id:\t%u\n"
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002387 "prog_tag:\t%s\n"
2388 "prog_id:\t%u\n",
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002389 bpf_link_type_strs[link->type],
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002390 link->id,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002391 prog_tag,
2392 prog->aux->id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002393 if (link->ops->show_fdinfo)
2394 link->ops->show_fdinfo(link, m);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002395}
2396#endif
2397
Zou Wei6f302bf2020-04-23 10:32:40 +08002398static const struct file_operations bpf_link_fops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002399#ifdef CONFIG_PROC_FS
2400 .show_fdinfo = bpf_link_show_fdinfo,
2401#endif
2402 .release = bpf_link_release,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002403 .read = bpf_dummy_read,
2404 .write = bpf_dummy_write,
2405};
2406
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002407static int bpf_link_alloc_id(struct bpf_link *link)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002408{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002409 int id;
2410
2411 idr_preload(GFP_KERNEL);
2412 spin_lock_bh(&link_idr_lock);
2413 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2414 spin_unlock_bh(&link_idr_lock);
2415 idr_preload_end();
2416
2417 return id;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002418}
2419
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002420/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2421 * reserving unused FD and allocating ID from link_idr. This is to be paired
2422 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2423 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2424 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2425 * transient state is passed around in struct bpf_link_primer.
2426 * This is preferred way to create and initialize bpf_link, especially when
2427 * there are complicated and expensive operations inbetween creating bpf_link
2428 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2429 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2430 * expensive (and potentially failing) roll back operations in a rare case
2431 * that file, FD, or ID can't be allocated.
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002432 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002433int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002434{
2435 struct file *file;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002436 int fd, id;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002437
2438 fd = get_unused_fd_flags(O_CLOEXEC);
2439 if (fd < 0)
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002440 return fd;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002441
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002442
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002443 id = bpf_link_alloc_id(link);
2444 if (id < 0) {
2445 put_unused_fd(fd);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002446 return id;
2447 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002448
2449 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2450 if (IS_ERR(file)) {
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002451 bpf_link_free_id(id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002452 put_unused_fd(fd);
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002453 return PTR_ERR(file);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002454 }
2455
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002456 primer->link = link;
2457 primer->file = file;
2458 primer->fd = fd;
2459 primer->id = id;
2460 return 0;
2461}
2462
2463int bpf_link_settle(struct bpf_link_primer *primer)
2464{
2465 /* make bpf_link fetchable by ID */
2466 spin_lock_bh(&link_idr_lock);
2467 primer->link->id = primer->id;
2468 spin_unlock_bh(&link_idr_lock);
2469 /* make bpf_link fetchable by FD */
2470 fd_install(primer->fd, primer->file);
2471 /* pass through installed FD */
2472 return primer->fd;
2473}
2474
2475int bpf_link_new_fd(struct bpf_link *link)
2476{
2477 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002478}
2479
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002480struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2481{
2482 struct fd f = fdget(ufd);
2483 struct bpf_link *link;
2484
2485 if (!f.file)
2486 return ERR_PTR(-EBADF);
2487 if (f.file->f_op != &bpf_link_fops) {
2488 fdput(f);
2489 return ERR_PTR(-EINVAL);
2490 }
2491
2492 link = f.file->private_data;
2493 bpf_link_inc(link);
2494 fdput(f);
2495
2496 return link;
2497}
2498
2499struct bpf_tracing_link {
2500 struct bpf_link link;
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002501 enum bpf_attach_type attach_type;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002502 struct bpf_trampoline *trampoline;
2503 struct bpf_prog *tgt_prog;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002504};
2505
2506static void bpf_tracing_link_release(struct bpf_link *link)
2507{
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002508 struct bpf_tracing_link *tr_link =
2509 container_of(link, struct bpf_tracing_link, link);
2510
2511 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2512 tr_link->trampoline));
2513
2514 bpf_trampoline_put(tr_link->trampoline);
2515
2516 /* tgt_prog is NULL if target is a kernel function */
2517 if (tr_link->tgt_prog)
2518 bpf_prog_put(tr_link->tgt_prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002519}
2520
2521static void bpf_tracing_link_dealloc(struct bpf_link *link)
2522{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002523 struct bpf_tracing_link *tr_link =
2524 container_of(link, struct bpf_tracing_link, link);
2525
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002526 kfree(tr_link);
2527}
2528
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002529static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2530 struct seq_file *seq)
2531{
2532 struct bpf_tracing_link *tr_link =
2533 container_of(link, struct bpf_tracing_link, link);
2534
2535 seq_printf(seq,
2536 "attach_type:\t%d\n",
2537 tr_link->attach_type);
2538}
2539
2540static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2541 struct bpf_link_info *info)
2542{
2543 struct bpf_tracing_link *tr_link =
2544 container_of(link, struct bpf_tracing_link, link);
2545
2546 info->tracing.attach_type = tr_link->attach_type;
2547
2548 return 0;
2549}
2550
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002551static const struct bpf_link_ops bpf_tracing_link_lops = {
2552 .release = bpf_tracing_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002553 .dealloc = bpf_tracing_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002554 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2555 .fill_link_info = bpf_tracing_link_fill_link_info,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002556};
2557
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002558static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2559 int tgt_prog_fd,
2560 u32 btf_id)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002561{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002562 struct bpf_link_primer link_primer;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002563 struct bpf_prog *tgt_prog = NULL;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002564 struct bpf_trampoline *tr = NULL;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002565 struct bpf_tracing_link *link;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002566 u64 key = 0;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002567 int err;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002568
KP Singh9e4e01d2020-03-29 01:43:52 +01002569 switch (prog->type) {
2570 case BPF_PROG_TYPE_TRACING:
2571 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2572 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2573 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2574 err = -EINVAL;
2575 goto out_put_prog;
2576 }
2577 break;
2578 case BPF_PROG_TYPE_EXT:
2579 if (prog->expected_attach_type != 0) {
2580 err = -EINVAL;
2581 goto out_put_prog;
2582 }
2583 break;
2584 case BPF_PROG_TYPE_LSM:
2585 if (prog->expected_attach_type != BPF_LSM_MAC) {
2586 err = -EINVAL;
2587 goto out_put_prog;
2588 }
2589 break;
2590 default:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002591 err = -EINVAL;
2592 goto out_put_prog;
2593 }
2594
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002595 if (!!tgt_prog_fd != !!btf_id) {
2596 err = -EINVAL;
2597 goto out_put_prog;
2598 }
2599
2600 if (tgt_prog_fd) {
2601 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2602 if (prog->type != BPF_PROG_TYPE_EXT) {
2603 err = -EINVAL;
2604 goto out_put_prog;
2605 }
2606
2607 tgt_prog = bpf_prog_get(tgt_prog_fd);
2608 if (IS_ERR(tgt_prog)) {
2609 err = PTR_ERR(tgt_prog);
2610 tgt_prog = NULL;
2611 goto out_put_prog;
2612 }
2613
2614 key = bpf_trampoline_compute_key(tgt_prog, btf_id);
2615 }
2616
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002617 link = kzalloc(sizeof(*link), GFP_USER);
2618 if (!link) {
2619 err = -ENOMEM;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002620 goto out_put_prog;
2621 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002622 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2623 &bpf_tracing_link_lops, prog);
2624 link->attach_type = prog->expected_attach_type;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002625
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002626 mutex_lock(&prog->aux->dst_mutex);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002627
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002628 /* There are a few possible cases here:
2629 *
2630 * - if prog->aux->dst_trampoline is set, the program was just loaded
2631 * and not yet attached to anything, so we can use the values stored
2632 * in prog->aux
2633 *
2634 * - if prog->aux->dst_trampoline is NULL, the program has already been
2635 * attached to a target and its initial target was cleared (below)
2636 *
2637 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2638 * target_btf_id using the link_create API.
2639 *
2640 * - if tgt_prog == NULL when this function was called using the old
2641 * raw_tracepoint_open API, and we need a target from prog->aux
2642 *
2643 * The combination of no saved target in prog->aux, and no target
2644 * specified on load is illegal, and we reject that here.
2645 */
2646 if (!prog->aux->dst_trampoline && !tgt_prog) {
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002647 err = -ENOENT;
2648 goto out_unlock;
2649 }
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002650
2651 if (!prog->aux->dst_trampoline ||
2652 (key && key != prog->aux->dst_trampoline->key)) {
2653 /* If there is no saved target, or the specified target is
2654 * different from the destination specified at load time, we
2655 * need a new trampoline and a check for compatibility
2656 */
2657 struct bpf_attach_target_info tgt_info = {};
2658
2659 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2660 &tgt_info);
2661 if (err)
2662 goto out_unlock;
2663
2664 tr = bpf_trampoline_get(key, &tgt_info);
2665 if (!tr) {
2666 err = -ENOMEM;
2667 goto out_unlock;
2668 }
2669 } else {
2670 /* The caller didn't specify a target, or the target was the
2671 * same as the destination supplied during program load. This
2672 * means we can reuse the trampoline and reference from program
2673 * load time, and there is no need to allocate a new one. This
2674 * can only happen once for any program, as the saved values in
2675 * prog->aux are cleared below.
2676 */
2677 tr = prog->aux->dst_trampoline;
2678 tgt_prog = prog->aux->dst_prog;
2679 }
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002680
2681 err = bpf_link_prime(&link->link, &link_primer);
2682 if (err)
2683 goto out_unlock;
2684
2685 err = bpf_trampoline_link_prog(prog, tr);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002686 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002687 bpf_link_cleanup(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002688 link = NULL;
2689 goto out_unlock;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002690 }
2691
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002692 link->tgt_prog = tgt_prog;
2693 link->trampoline = tr;
2694
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002695 /* Always clear the trampoline and target prog from prog->aux to make
2696 * sure the original attach destination is not kept alive after a
2697 * program is (re-)attached to another target.
2698 */
2699 if (prog->aux->dst_prog &&
2700 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2701 /* got extra prog ref from syscall, or attaching to different prog */
2702 bpf_prog_put(prog->aux->dst_prog);
2703 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2704 /* we allocated a new trampoline, so free the old one */
2705 bpf_trampoline_put(prog->aux->dst_trampoline);
2706
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002707 prog->aux->dst_prog = NULL;
2708 prog->aux->dst_trampoline = NULL;
2709 mutex_unlock(&prog->aux->dst_mutex);
2710
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002711 return bpf_link_settle(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002712out_unlock:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002713 if (tr && tr != prog->aux->dst_trampoline)
2714 bpf_trampoline_put(tr);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002715 mutex_unlock(&prog->aux->dst_mutex);
2716 kfree(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002717out_put_prog:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002718 if (tgt_prog_fd && tgt_prog)
2719 bpf_prog_put(tgt_prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002720 bpf_prog_put(prog);
2721 return err;
2722}
2723
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002724struct bpf_raw_tp_link {
2725 struct bpf_link link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002726 struct bpf_raw_event_map *btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002727};
2728
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002729static void bpf_raw_tp_link_release(struct bpf_link *link)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002730{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002731 struct bpf_raw_tp_link *raw_tp =
2732 container_of(link, struct bpf_raw_tp_link, link);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002733
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002734 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002735 bpf_put_raw_tracepoint(raw_tp->btp);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002736}
2737
2738static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2739{
2740 struct bpf_raw_tp_link *raw_tp =
2741 container_of(link, struct bpf_raw_tp_link, link);
2742
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002743 kfree(raw_tp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002744}
2745
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002746static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2747 struct seq_file *seq)
2748{
2749 struct bpf_raw_tp_link *raw_tp_link =
2750 container_of(link, struct bpf_raw_tp_link, link);
2751
2752 seq_printf(seq,
2753 "tp_name:\t%s\n",
2754 raw_tp_link->btp->tp->name);
2755}
2756
2757static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2758 struct bpf_link_info *info)
2759{
2760 struct bpf_raw_tp_link *raw_tp_link =
2761 container_of(link, struct bpf_raw_tp_link, link);
2762 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2763 const char *tp_name = raw_tp_link->btp->tp->name;
2764 u32 ulen = info->raw_tracepoint.tp_name_len;
2765 size_t tp_len = strlen(tp_name);
2766
Yonghong Songb4749592020-08-21 12:10:54 -07002767 if (!ulen ^ !ubuf)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002768 return -EINVAL;
2769
2770 info->raw_tracepoint.tp_name_len = tp_len + 1;
2771
2772 if (!ubuf)
2773 return 0;
2774
2775 if (ulen >= tp_len + 1) {
2776 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2777 return -EFAULT;
2778 } else {
2779 char zero = '\0';
2780
2781 if (copy_to_user(ubuf, tp_name, ulen - 1))
2782 return -EFAULT;
2783 if (put_user(zero, ubuf + ulen - 1))
2784 return -EFAULT;
2785 return -ENOSPC;
2786 }
2787
2788 return 0;
2789}
2790
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002791static const struct bpf_link_ops bpf_raw_tp_link_lops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002792 .release = bpf_raw_tp_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002793 .dealloc = bpf_raw_tp_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002794 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2795 .fill_link_info = bpf_raw_tp_link_fill_link_info,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002796};
2797
2798#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2799
2800static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2801{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002802 struct bpf_link_primer link_primer;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002803 struct bpf_raw_tp_link *link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002804 struct bpf_raw_event_map *btp;
2805 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002806 const char *tp_name;
2807 char buf[128];
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002808 int err;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002809
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002810 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2811 return -EINVAL;
2812
2813 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2814 if (IS_ERR(prog))
2815 return PTR_ERR(prog);
2816
KP Singh9e4e01d2020-03-29 01:43:52 +01002817 switch (prog->type) {
2818 case BPF_PROG_TYPE_TRACING:
2819 case BPF_PROG_TYPE_EXT:
2820 case BPF_PROG_TYPE_LSM:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002821 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002822 /* The attach point for this category of programs
2823 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002824 */
2825 err = -EINVAL;
2826 goto out_put_prog;
2827 }
KP Singh9e4e01d2020-03-29 01:43:52 +01002828 if (prog->type == BPF_PROG_TYPE_TRACING &&
2829 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002830 tp_name = prog->aux->attach_func_name;
KP Singh9e4e01d2020-03-29 01:43:52 +01002831 break;
2832 }
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002833 return bpf_tracing_prog_attach(prog, 0, 0);
KP Singh9e4e01d2020-03-29 01:43:52 +01002834 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2835 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002836 if (strncpy_from_user(buf,
2837 u64_to_user_ptr(attr->raw_tracepoint.name),
2838 sizeof(buf) - 1) < 0) {
2839 err = -EFAULT;
2840 goto out_put_prog;
2841 }
2842 buf[sizeof(buf) - 1] = 0;
2843 tp_name = buf;
KP Singh9e4e01d2020-03-29 01:43:52 +01002844 break;
2845 default:
2846 err = -EINVAL;
2847 goto out_put_prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002848 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002849
Matt Mullinsa38d1102018-12-12 16:42:37 -08002850 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002851 if (!btp) {
2852 err = -ENOENT;
2853 goto out_put_prog;
2854 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002855
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002856 link = kzalloc(sizeof(*link), GFP_USER);
2857 if (!link) {
Matt Mullinsa38d1102018-12-12 16:42:37 -08002858 err = -ENOMEM;
2859 goto out_put_btp;
2860 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002861 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2862 &bpf_raw_tp_link_lops, prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002863 link->btp = btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002864
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002865 err = bpf_link_prime(&link->link, &link_primer);
2866 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002867 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002868 goto out_put_btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002869 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002870
2871 err = bpf_probe_register(link->btp, prog);
2872 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002873 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002874 goto out_put_btp;
2875 }
2876
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002877 return bpf_link_settle(&link_primer);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002878
Matt Mullinsa38d1102018-12-12 16:42:37 -08002879out_put_btp:
2880 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002881out_put_prog:
2882 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002883 return err;
2884}
2885
Anders Roxell33491582018-04-03 14:09:47 +02002886static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2887 enum bpf_attach_type attach_type)
2888{
2889 switch (prog->type) {
2890 case BPF_PROG_TYPE_CGROUP_SOCK:
2891 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002892 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002893 case BPF_PROG_TYPE_SK_LOOKUP:
Anders Roxell33491582018-04-03 14:09:47 +02002894 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07002895 case BPF_PROG_TYPE_CGROUP_SKB:
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002896 if (!capable(CAP_NET_ADMIN))
2897 /* cg-skb progs can be loaded by unpriv user.
2898 * check permissions at attach time.
2899 */
2900 return -EPERM;
brakmo5cf1e912019-05-28 16:59:36 -07002901 return prog->enforce_expected_attach_type &&
2902 prog->expected_attach_type != attach_type ?
2903 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02002904 default:
2905 return 0;
2906 }
2907}
2908
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002909static enum bpf_prog_type
2910attach_type_to_prog_type(enum bpf_attach_type attach_type)
2911{
2912 switch (attach_type) {
2913 case BPF_CGROUP_INET_INGRESS:
2914 case BPF_CGROUP_INET_EGRESS:
2915 return BPF_PROG_TYPE_CGROUP_SKB;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002916 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07002917 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002918 case BPF_CGROUP_INET4_POST_BIND:
2919 case BPF_CGROUP_INET6_POST_BIND:
2920 return BPF_PROG_TYPE_CGROUP_SOCK;
2921 case BPF_CGROUP_INET4_BIND:
2922 case BPF_CGROUP_INET6_BIND:
2923 case BPF_CGROUP_INET4_CONNECT:
2924 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002925 case BPF_CGROUP_INET4_GETPEERNAME:
2926 case BPF_CGROUP_INET6_GETPEERNAME:
2927 case BPF_CGROUP_INET4_GETSOCKNAME:
2928 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002929 case BPF_CGROUP_UDP4_SENDMSG:
2930 case BPF_CGROUP_UDP6_SENDMSG:
2931 case BPF_CGROUP_UDP4_RECVMSG:
2932 case BPF_CGROUP_UDP6_RECVMSG:
2933 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2934 case BPF_CGROUP_SOCK_OPS:
2935 return BPF_PROG_TYPE_SOCK_OPS;
2936 case BPF_CGROUP_DEVICE:
2937 return BPF_PROG_TYPE_CGROUP_DEVICE;
2938 case BPF_SK_MSG_VERDICT:
2939 return BPF_PROG_TYPE_SK_MSG;
2940 case BPF_SK_SKB_STREAM_PARSER:
2941 case BPF_SK_SKB_STREAM_VERDICT:
2942 return BPF_PROG_TYPE_SK_SKB;
2943 case BPF_LIRC_MODE2:
2944 return BPF_PROG_TYPE_LIRC_MODE2;
2945 case BPF_FLOW_DISSECTOR:
2946 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2947 case BPF_CGROUP_SYSCTL:
2948 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2949 case BPF_CGROUP_GETSOCKOPT:
2950 case BPF_CGROUP_SETSOCKOPT:
2951 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
Yonghong Songde4e05c2020-05-09 10:59:01 -07002952 case BPF_TRACE_ITER:
2953 return BPF_PROG_TYPE_TRACING;
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002954 case BPF_SK_LOOKUP:
2955 return BPF_PROG_TYPE_SK_LOOKUP;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07002956 case BPF_XDP:
2957 return BPF_PROG_TYPE_XDP;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002958 default:
2959 return BPF_PROG_TYPE_UNSPEC;
2960 }
2961}
2962
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002963#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07002964
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002965#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002966 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002967
Daniel Mackf4324552016-11-23 16:52:27 +01002968static int bpf_prog_attach(const union bpf_attr *attr)
2969{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002970 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002971 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002972 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002973
Daniel Mackf4324552016-11-23 16:52:27 +01002974 if (CHECK_ATTR(BPF_PROG_ATTACH))
2975 return -EINVAL;
2976
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002977 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002978 return -EINVAL;
2979
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002980 ptype = attach_type_to_prog_type(attr->attach_type);
2981 if (ptype == BPF_PROG_TYPE_UNSPEC)
Daniel Mackf4324552016-11-23 16:52:27 +01002982 return -EINVAL;
Daniel Mackf4324552016-11-23 16:52:27 +01002983
David Ahernb2cd1252016-12-01 08:48:03 -08002984 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2985 if (IS_ERR(prog))
2986 return PTR_ERR(prog);
2987
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002988 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2989 bpf_prog_put(prog);
2990 return -EINVAL;
2991 }
2992
Sean Youngfdb5c452018-06-19 00:04:24 +01002993 switch (ptype) {
2994 case BPF_PROG_TYPE_SK_SKB:
2995 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002996 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01002997 break;
2998 case BPF_PROG_TYPE_LIRC_MODE2:
2999 ret = lirc_prog_attach(attr, prog);
3000 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07003001 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003002 ret = netns_bpf_prog_attach(attr, prog);
Petar Penkovd58e4682018-09-14 07:46:18 -07003003 break;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003004 case BPF_PROG_TYPE_CGROUP_DEVICE:
3005 case BPF_PROG_TYPE_CGROUP_SKB:
3006 case BPF_PROG_TYPE_CGROUP_SOCK:
3007 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3008 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3009 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3010 case BPF_PROG_TYPE_SOCK_OPS:
Sean Youngfdb5c452018-06-19 00:04:24 +01003011 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003012 break;
3013 default:
3014 ret = -EINVAL;
David Ahernb2cd1252016-12-01 08:48:03 -08003015 }
3016
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003017 if (ret)
3018 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003019 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003020}
3021
3022#define BPF_PROG_DETACH_LAST_FIELD attach_type
3023
3024static int bpf_prog_detach(const union bpf_attr *attr)
3025{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003026 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003027
Daniel Mackf4324552016-11-23 16:52:27 +01003028 if (CHECK_ATTR(BPF_PROG_DETACH))
3029 return -EINVAL;
3030
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003031 ptype = attach_type_to_prog_type(attr->attach_type);
3032
3033 switch (ptype) {
3034 case BPF_PROG_TYPE_SK_MSG:
3035 case BPF_PROG_TYPE_SK_SKB:
Lorenz Bauerbb0de312020-06-29 10:56:28 +01003036 return sock_map_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003037 case BPF_PROG_TYPE_LIRC_MODE2:
Sean Youngf4364dc2018-05-27 12:24:09 +01003038 return lirc_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003039 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Lorenz Bauer4ac2add2020-06-29 10:56:26 +01003040 return netns_bpf_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003041 case BPF_PROG_TYPE_CGROUP_DEVICE:
3042 case BPF_PROG_TYPE_CGROUP_SKB:
3043 case BPF_PROG_TYPE_CGROUP_SOCK:
3044 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3045 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3046 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3047 case BPF_PROG_TYPE_SOCK_OPS:
3048 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01003049 default:
3050 return -EINVAL;
3051 }
Daniel Mackf4324552016-11-23 16:52:27 +01003052}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07003053
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003054#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3055
3056static int bpf_prog_query(const union bpf_attr *attr,
3057 union bpf_attr __user *uattr)
3058{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003059 if (!capable(CAP_NET_ADMIN))
3060 return -EPERM;
3061 if (CHECK_ATTR(BPF_PROG_QUERY))
3062 return -EINVAL;
3063 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3064 return -EINVAL;
3065
3066 switch (attr->query.attach_type) {
3067 case BPF_CGROUP_INET_INGRESS:
3068 case BPF_CGROUP_INET_EGRESS:
3069 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07003070 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07003071 case BPF_CGROUP_INET4_BIND:
3072 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07003073 case BPF_CGROUP_INET4_POST_BIND:
3074 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07003075 case BPF_CGROUP_INET4_CONNECT:
3076 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02003077 case BPF_CGROUP_INET4_GETPEERNAME:
3078 case BPF_CGROUP_INET6_GETPEERNAME:
3079 case BPF_CGROUP_INET4_GETSOCKNAME:
3080 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07003081 case BPF_CGROUP_UDP4_SENDMSG:
3082 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02003083 case BPF_CGROUP_UDP4_RECVMSG:
3084 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003085 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003086 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08003087 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07003088 case BPF_CGROUP_GETSOCKOPT:
3089 case BPF_CGROUP_SETSOCKOPT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003090 return cgroup_bpf_prog_query(attr, uattr);
Sean Youngf4364dc2018-05-27 12:24:09 +01003091 case BPF_LIRC_MODE2:
3092 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07003093 case BPF_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003094 case BPF_SK_LOOKUP:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003095 return netns_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003096 default:
3097 return -EINVAL;
3098 }
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003099}
Daniel Mackf4324552016-11-23 16:52:27 +01003100
Song Liu1b4d60e2020-09-25 13:54:29 -07003101#define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003102
3103static int bpf_prog_test_run(const union bpf_attr *attr,
3104 union bpf_attr __user *uattr)
3105{
3106 struct bpf_prog *prog;
3107 int ret = -ENOTSUPP;
3108
3109 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3110 return -EINVAL;
3111
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07003112 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3113 (!attr->test.ctx_size_in && attr->test.ctx_in))
3114 return -EINVAL;
3115
3116 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3117 (!attr->test.ctx_size_out && attr->test.ctx_out))
3118 return -EINVAL;
3119
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003120 prog = bpf_prog_get(attr->test.prog_fd);
3121 if (IS_ERR(prog))
3122 return PTR_ERR(prog);
3123
3124 if (prog->aux->ops->test_run)
3125 ret = prog->aux->ops->test_run(prog, attr, uattr);
3126
3127 bpf_prog_put(prog);
3128 return ret;
3129}
3130
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003131#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3132
3133static int bpf_obj_get_next_id(const union bpf_attr *attr,
3134 union bpf_attr __user *uattr,
3135 struct idr *idr,
3136 spinlock_t *lock)
3137{
3138 u32 next_id = attr->start_id;
3139 int err = 0;
3140
3141 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3142 return -EINVAL;
3143
3144 if (!capable(CAP_SYS_ADMIN))
3145 return -EPERM;
3146
3147 next_id++;
3148 spin_lock_bh(lock);
3149 if (!idr_get_next(idr, &next_id))
3150 err = -ENOENT;
3151 spin_unlock_bh(lock);
3152
3153 if (!err)
3154 err = put_user(next_id, &uattr->next_id);
3155
3156 return err;
3157}
3158
Yonghong Song6086d292020-05-09 10:59:09 -07003159struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3160{
3161 struct bpf_map *map;
3162
3163 spin_lock_bh(&map_idr_lock);
3164again:
3165 map = idr_get_next(&map_idr, id);
3166 if (map) {
3167 map = __bpf_map_inc_not_zero(map, false);
3168 if (IS_ERR(map)) {
3169 (*id)++;
3170 goto again;
3171 }
3172 }
3173 spin_unlock_bh(&map_idr_lock);
3174
3175 return map;
3176}
3177
Alexei Starovoitova228a642020-07-01 18:10:18 -07003178struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3179{
3180 struct bpf_prog *prog;
3181
3182 spin_lock_bh(&prog_idr_lock);
3183again:
3184 prog = idr_get_next(&prog_idr, id);
3185 if (prog) {
3186 prog = bpf_prog_inc_not_zero(prog);
3187 if (IS_ERR(prog)) {
3188 (*id)++;
3189 goto again;
3190 }
3191 }
3192 spin_unlock_bh(&prog_idr_lock);
3193
3194 return prog;
3195}
3196
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003197#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3198
Björn Töpel7e6897f2019-12-13 18:51:09 +01003199struct bpf_prog *bpf_prog_by_id(u32 id)
3200{
3201 struct bpf_prog *prog;
3202
3203 if (!id)
3204 return ERR_PTR(-ENOENT);
3205
3206 spin_lock_bh(&prog_idr_lock);
3207 prog = idr_find(&prog_idr, id);
3208 if (prog)
3209 prog = bpf_prog_inc_not_zero(prog);
3210 else
3211 prog = ERR_PTR(-ENOENT);
3212 spin_unlock_bh(&prog_idr_lock);
3213 return prog;
3214}
3215
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003216static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3217{
3218 struct bpf_prog *prog;
3219 u32 id = attr->prog_id;
3220 int fd;
3221
3222 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3223 return -EINVAL;
3224
3225 if (!capable(CAP_SYS_ADMIN))
3226 return -EPERM;
3227
Björn Töpel7e6897f2019-12-13 18:51:09 +01003228 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003229 if (IS_ERR(prog))
3230 return PTR_ERR(prog);
3231
3232 fd = bpf_prog_new_fd(prog);
3233 if (fd < 0)
3234 bpf_prog_put(prog);
3235
3236 return fd;
3237}
3238
Chenbo Feng6e71b042017-10-18 13:00:22 -07003239#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003240
3241static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3242{
3243 struct bpf_map *map;
3244 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07003245 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003246 int fd;
3247
Chenbo Feng6e71b042017-10-18 13:00:22 -07003248 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3249 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003250 return -EINVAL;
3251
3252 if (!capable(CAP_SYS_ADMIN))
3253 return -EPERM;
3254
Chenbo Feng6e71b042017-10-18 13:00:22 -07003255 f_flags = bpf_get_file_flag(attr->open_flags);
3256 if (f_flags < 0)
3257 return f_flags;
3258
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003259 spin_lock_bh(&map_idr_lock);
3260 map = idr_find(&map_idr, id);
3261 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07003262 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003263 else
3264 map = ERR_PTR(-ENOENT);
3265 spin_unlock_bh(&map_idr_lock);
3266
3267 if (IS_ERR(map))
3268 return PTR_ERR(map);
3269
Chenbo Feng6e71b042017-10-18 13:00:22 -07003270 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003271 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08003272 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003273
3274 return fd;
3275}
3276
Daniel Borkmann7105e822017-12-20 13:42:57 +01003277static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003278 unsigned long addr, u32 *off,
3279 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003280{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003281 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003282 int i;
3283
YiFei Zhu984fe942020-09-15 16:45:39 -07003284 mutex_lock(&prog->aux->used_maps_mutex);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003285 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3286 map = prog->aux->used_maps[i];
3287 if (map == (void *)addr) {
3288 *type = BPF_PSEUDO_MAP_FD;
YiFei Zhu984fe942020-09-15 16:45:39 -07003289 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003290 }
3291 if (!map->ops->map_direct_value_meta)
3292 continue;
3293 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3294 *type = BPF_PSEUDO_MAP_VALUE;
YiFei Zhu984fe942020-09-15 16:45:39 -07003295 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003296 }
3297 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003298 map = NULL;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003299
YiFei Zhu984fe942020-09-15 16:45:39 -07003300out:
3301 mutex_unlock(&prog->aux->used_maps_mutex);
3302 return map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003303}
3304
Kees Cook63960262020-07-02 15:45:23 -07003305static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3306 const struct cred *f_cred)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003307{
3308 const struct bpf_map *map;
3309 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003310 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003311 u64 imm;
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003312 u8 code;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003313 int i;
3314
3315 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3316 GFP_USER);
3317 if (!insns)
3318 return insns;
3319
3320 for (i = 0; i < prog->len; i++) {
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003321 code = insns[i].code;
3322
3323 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003324 insns[i].code = BPF_JMP | BPF_CALL;
3325 insns[i].imm = BPF_FUNC_tail_call;
3326 /* fall-through */
3327 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003328 if (code == (BPF_JMP | BPF_CALL) ||
3329 code == (BPF_JMP | BPF_CALL_ARGS)) {
3330 if (code == (BPF_JMP | BPF_CALL_ARGS))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003331 insns[i].code = BPF_JMP | BPF_CALL;
Kees Cook63960262020-07-02 15:45:23 -07003332 if (!bpf_dump_raw_ok(f_cred))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003333 insns[i].imm = 0;
3334 continue;
3335 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003336 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3337 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3338 continue;
3339 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003340
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003341 if (code != (BPF_LD | BPF_IMM | BPF_DW))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003342 continue;
3343
3344 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003345 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003346 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003347 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003348 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003349 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003350 continue;
3351 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003352 }
3353
3354 return insns;
3355}
3356
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003357static int set_info_rec_size(struct bpf_prog_info *info)
3358{
3359 /*
3360 * Ensure info.*_rec_size is the same as kernel expected size
3361 *
3362 * or
3363 *
3364 * Only allow zero *_rec_size if both _rec_size and _cnt are
3365 * zero. In this case, the kernel will set the expected
3366 * _rec_size back to the info.
3367 */
3368
Yonghong Song11d8b822018-12-10 14:14:08 -08003369 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003370 info->func_info_rec_size != sizeof(struct bpf_func_info))
3371 return -EINVAL;
3372
Yonghong Song11d8b822018-12-10 14:14:08 -08003373 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003374 info->line_info_rec_size != sizeof(struct bpf_line_info))
3375 return -EINVAL;
3376
Yonghong Song11d8b822018-12-10 14:14:08 -08003377 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003378 info->jited_line_info_rec_size != sizeof(__u64))
3379 return -EINVAL;
3380
3381 info->func_info_rec_size = sizeof(struct bpf_func_info);
3382 info->line_info_rec_size = sizeof(struct bpf_line_info);
3383 info->jited_line_info_rec_size = sizeof(__u64);
3384
3385 return 0;
3386}
3387
Kees Cook63960262020-07-02 15:45:23 -07003388static int bpf_prog_get_info_by_fd(struct file *file,
3389 struct bpf_prog *prog,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003390 const union bpf_attr *attr,
3391 union bpf_attr __user *uattr)
3392{
3393 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003394 struct bpf_prog_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003395 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003396 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003397 char __user *uinsns;
3398 u32 ulen;
3399 int err;
3400
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003401 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003402 if (err)
3403 return err;
3404 info_len = min_t(u32, sizeof(info), info_len);
3405
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003406 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003407 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02003408 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003409
3410 info.type = prog->type;
3411 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003412 info.load_time = prog->aux->load_time;
3413 info.created_by_uid = from_kuid_munged(current_user_ns(),
3414 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02003415 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003416
3417 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003418 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3419
YiFei Zhu984fe942020-09-15 16:45:39 -07003420 mutex_lock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003421 ulen = info.nr_map_ids;
3422 info.nr_map_ids = prog->aux->used_map_cnt;
3423 ulen = min_t(u32, info.nr_map_ids, ulen);
3424 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07003425 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003426 u32 i;
3427
3428 for (i = 0; i < ulen; i++)
3429 if (put_user(prog->aux->used_maps[i]->id,
YiFei Zhu984fe942020-09-15 16:45:39 -07003430 &user_map_ids[i])) {
3431 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003432 return -EFAULT;
YiFei Zhu984fe942020-09-15 16:45:39 -07003433 }
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003434 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003435 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003436
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003437 err = set_info_rec_size(&info);
3438 if (err)
3439 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08003440
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003441 bpf_prog_get_stats(prog, &stats);
3442 info.run_time_ns = stats.nsecs;
3443 info.run_cnt = stats.cnt;
3444
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003445 if (!bpf_capable()) {
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003446 info.jited_prog_len = 0;
3447 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303448 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01003449 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08003450 info.nr_func_info = 0;
3451 info.nr_line_info = 0;
3452 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003453 goto done;
3454 }
3455
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003456 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02003457 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003458 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003459 struct bpf_insn *insns_sanitized;
3460 bool fault;
3461
Kees Cook63960262020-07-02 15:45:23 -07003462 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003463 info.xlated_prog_insns = 0;
3464 goto done;
3465 }
Kees Cook63960262020-07-02 15:45:23 -07003466 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003467 if (!insns_sanitized)
3468 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003469 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3470 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003471 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3472 kfree(insns_sanitized);
3473 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003474 return -EFAULT;
3475 }
3476
Jakub Kicinski675fc272017-12-27 18:39:09 -08003477 if (bpf_prog_is_dev_bound(prog->aux)) {
3478 err = bpf_prog_offload_info_fill(&info, prog);
3479 if (err)
3480 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08003481 goto done;
3482 }
3483
3484 /* NOTE: the following code is supposed to be skipped for offload.
3485 * bpf_prog_offload_info_fill() is the place to fill similar fields
3486 * for offload.
3487 */
3488 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05303489 if (prog->aux->func_cnt) {
3490 u32 i;
3491
3492 info.jited_prog_len = 0;
3493 for (i = 0; i < prog->aux->func_cnt; i++)
3494 info.jited_prog_len += prog->aux->func[i]->jited_len;
3495 } else {
3496 info.jited_prog_len = prog->jited_len;
3497 }
3498
Jiong Wangfcfb1262018-01-16 16:05:19 -08003499 if (info.jited_prog_len && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003500 if (bpf_dump_raw_ok(file->f_cred)) {
Jiong Wangfcfb1262018-01-16 16:05:19 -08003501 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3502 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05303503
3504 /* for multi-function programs, copy the JITed
3505 * instructions for all the functions
3506 */
3507 if (prog->aux->func_cnt) {
3508 u32 len, free, i;
3509 u8 *img;
3510
3511 free = ulen;
3512 for (i = 0; i < prog->aux->func_cnt; i++) {
3513 len = prog->aux->func[i]->jited_len;
3514 len = min_t(u32, len, free);
3515 img = (u8 *) prog->aux->func[i]->bpf_func;
3516 if (copy_to_user(uinsns, img, len))
3517 return -EFAULT;
3518 uinsns += len;
3519 free -= len;
3520 if (!free)
3521 break;
3522 }
3523 } else {
3524 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3525 return -EFAULT;
3526 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08003527 } else {
3528 info.jited_prog_insns = 0;
3529 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08003530 }
3531
Sandipan Dasdbecd732018-05-24 12:26:48 +05303532 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07003533 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003534 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003535 if (bpf_dump_raw_ok(file->f_cred)) {
Song Liuff1889f2018-11-02 10:16:17 -07003536 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303537 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303538 u32 i;
3539
3540 /* copy the address of the kernel symbol
3541 * corresponding to each function
3542 */
3543 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3544 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07003545 if (prog->aux->func_cnt) {
3546 for (i = 0; i < ulen; i++) {
3547 ksym_addr = (unsigned long)
3548 prog->aux->func[i]->bpf_func;
3549 if (put_user((u64) ksym_addr,
3550 &user_ksyms[i]))
3551 return -EFAULT;
3552 }
3553 } else {
3554 ksym_addr = (unsigned long) prog->bpf_func;
3555 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05303556 return -EFAULT;
3557 }
3558 } else {
3559 info.jited_ksyms = 0;
3560 }
3561 }
3562
Sandipan Das815581c2018-05-24 12:26:52 +05303563 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07003564 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003565 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003566 if (bpf_dump_raw_ok(file->f_cred)) {
Sandipan Das815581c2018-05-24 12:26:52 +05303567 u32 __user *user_lens;
3568 u32 func_len, i;
3569
3570 /* copy the JITed image lengths for each function */
3571 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3572 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07003573 if (prog->aux->func_cnt) {
3574 for (i = 0; i < ulen; i++) {
3575 func_len =
3576 prog->aux->func[i]->jited_len;
3577 if (put_user(func_len, &user_lens[i]))
3578 return -EFAULT;
3579 }
3580 } else {
3581 func_len = prog->jited_len;
3582 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05303583 return -EFAULT;
3584 }
3585 } else {
3586 info.jited_func_lens = 0;
3587 }
3588 }
3589
Martin KaFai Lau73372242018-12-05 17:35:43 -08003590 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08003591 info.btf_id = btf_id(prog->aux->btf);
3592
Yonghong Song11d8b822018-12-10 14:14:08 -08003593 ulen = info.nr_func_info;
3594 info.nr_func_info = prog->aux->func_info_cnt;
3595 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003596 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08003597
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003598 user_finfo = u64_to_user_ptr(info.func_info);
3599 ulen = min_t(u32, info.nr_func_info, ulen);
3600 if (copy_to_user(user_finfo, prog->aux->func_info,
3601 info.func_info_rec_size * ulen))
3602 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08003603 }
3604
Yonghong Song11d8b822018-12-10 14:14:08 -08003605 ulen = info.nr_line_info;
3606 info.nr_line_info = prog->aux->nr_linfo;
3607 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003608 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003609
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003610 user_linfo = u64_to_user_ptr(info.line_info);
3611 ulen = min_t(u32, info.nr_line_info, ulen);
3612 if (copy_to_user(user_linfo, prog->aux->linfo,
3613 info.line_info_rec_size * ulen))
3614 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003615 }
3616
Yonghong Song11d8b822018-12-10 14:14:08 -08003617 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003618 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08003619 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003620 else
Yonghong Song11d8b822018-12-10 14:14:08 -08003621 info.nr_jited_line_info = 0;
3622 if (info.nr_jited_line_info && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003623 if (bpf_dump_raw_ok(file->f_cred)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003624 __u64 __user *user_linfo;
3625 u32 i;
3626
3627 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08003628 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003629 for (i = 0; i < ulen; i++) {
3630 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3631 &user_linfo[i]))
3632 return -EFAULT;
3633 }
3634 } else {
3635 info.jited_line_info = 0;
3636 }
3637 }
3638
Song Liuc872bdb2018-12-12 09:37:46 -08003639 ulen = info.nr_prog_tags;
3640 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3641 if (ulen) {
3642 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3643 u32 i;
3644
3645 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3646 ulen = min_t(u32, info.nr_prog_tags, ulen);
3647 if (prog->aux->func_cnt) {
3648 for (i = 0; i < ulen; i++) {
3649 if (copy_to_user(user_prog_tags[i],
3650 prog->aux->func[i]->tag,
3651 BPF_TAG_SIZE))
3652 return -EFAULT;
3653 }
3654 } else {
3655 if (copy_to_user(user_prog_tags[0],
3656 prog->tag, BPF_TAG_SIZE))
3657 return -EFAULT;
3658 }
3659 }
3660
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003661done:
3662 if (copy_to_user(uinfo, &info, info_len) ||
3663 put_user(info_len, &uattr->info.info_len))
3664 return -EFAULT;
3665
3666 return 0;
3667}
3668
Kees Cook63960262020-07-02 15:45:23 -07003669static int bpf_map_get_info_by_fd(struct file *file,
3670 struct bpf_map *map,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003671 const union bpf_attr *attr,
3672 union bpf_attr __user *uattr)
3673{
3674 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003675 struct bpf_map_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003676 u32 info_len = attr->info.info_len;
3677 int err;
3678
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003679 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003680 if (err)
3681 return err;
3682 info_len = min_t(u32, sizeof(info), info_len);
3683
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003684 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003685 info.type = map->map_type;
3686 info.id = map->id;
3687 info.key_size = map->key_size;
3688 info.value_size = map->value_size;
3689 info.max_entries = map->max_entries;
3690 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07003691 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003692
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003693 if (map->btf) {
3694 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07003695 info.btf_key_type_id = map->btf_key_type_id;
3696 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003697 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08003698 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003699
Jakub Kicinski52775b32018-01-17 19:13:28 -08003700 if (bpf_map_is_dev_bound(map)) {
3701 err = bpf_map_offload_info_fill(&info, map);
3702 if (err)
3703 return err;
3704 }
3705
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003706 if (copy_to_user(uinfo, &info, info_len) ||
3707 put_user(info_len, &uattr->info.info_len))
3708 return -EFAULT;
3709
3710 return 0;
3711}
3712
Kees Cook63960262020-07-02 15:45:23 -07003713static int bpf_btf_get_info_by_fd(struct file *file,
3714 struct btf *btf,
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003715 const union bpf_attr *attr,
3716 union bpf_attr __user *uattr)
3717{
3718 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3719 u32 info_len = attr->info.info_len;
3720 int err;
3721
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003722 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003723 if (err)
3724 return err;
3725
3726 return btf_get_info_by_fd(btf, attr, uattr);
3727}
3728
Kees Cook63960262020-07-02 15:45:23 -07003729static int bpf_link_get_info_by_fd(struct file *file,
3730 struct bpf_link *link,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003731 const union bpf_attr *attr,
3732 union bpf_attr __user *uattr)
3733{
3734 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3735 struct bpf_link_info info;
3736 u32 info_len = attr->info.info_len;
3737 int err;
3738
3739 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3740 if (err)
3741 return err;
3742 info_len = min_t(u32, sizeof(info), info_len);
3743
3744 memset(&info, 0, sizeof(info));
3745 if (copy_from_user(&info, uinfo, info_len))
3746 return -EFAULT;
3747
3748 info.type = link->type;
3749 info.id = link->id;
3750 info.prog_id = link->prog->aux->id;
3751
3752 if (link->ops->fill_link_info) {
3753 err = link->ops->fill_link_info(link, &info);
3754 if (err)
3755 return err;
3756 }
3757
3758 if (copy_to_user(uinfo, &info, info_len) ||
3759 put_user(info_len, &uattr->info.info_len))
3760 return -EFAULT;
3761
3762 return 0;
3763}
3764
3765
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003766#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3767
3768static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3769 union bpf_attr __user *uattr)
3770{
3771 int ufd = attr->info.bpf_fd;
3772 struct fd f;
3773 int err;
3774
3775 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3776 return -EINVAL;
3777
3778 f = fdget(ufd);
3779 if (!f.file)
3780 return -EBADFD;
3781
3782 if (f.file->f_op == &bpf_prog_fops)
Kees Cook63960262020-07-02 15:45:23 -07003783 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003784 uattr);
3785 else if (f.file->f_op == &bpf_map_fops)
Kees Cook63960262020-07-02 15:45:23 -07003786 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003787 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07003788 else if (f.file->f_op == &btf_fops)
Kees Cook63960262020-07-02 15:45:23 -07003789 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003790 else if (f.file->f_op == &bpf_link_fops)
Kees Cook63960262020-07-02 15:45:23 -07003791 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003792 attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003793 else
3794 err = -EINVAL;
3795
3796 fdput(f);
3797 return err;
3798}
3799
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003800#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3801
3802static int bpf_btf_load(const union bpf_attr *attr)
3803{
3804 if (CHECK_ATTR(BPF_BTF_LOAD))
3805 return -EINVAL;
3806
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003807 if (!bpf_capable())
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003808 return -EPERM;
3809
3810 return btf_new_fd(attr);
3811}
3812
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003813#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3814
3815static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3816{
3817 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3818 return -EINVAL;
3819
3820 if (!capable(CAP_SYS_ADMIN))
3821 return -EPERM;
3822
3823 return btf_get_fd_by_id(attr->btf_id);
3824}
3825
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003826static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3827 union bpf_attr __user *uattr,
3828 u32 prog_id, u32 fd_type,
3829 const char *buf, u64 probe_offset,
3830 u64 probe_addr)
3831{
3832 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3833 u32 len = buf ? strlen(buf) : 0, input_len;
3834 int err = 0;
3835
3836 if (put_user(len, &uattr->task_fd_query.buf_len))
3837 return -EFAULT;
3838 input_len = attr->task_fd_query.buf_len;
3839 if (input_len && ubuf) {
3840 if (!len) {
3841 /* nothing to copy, just make ubuf NULL terminated */
3842 char zero = '\0';
3843
3844 if (put_user(zero, ubuf))
3845 return -EFAULT;
3846 } else if (input_len >= len + 1) {
3847 /* ubuf can hold the string with NULL terminator */
3848 if (copy_to_user(ubuf, buf, len + 1))
3849 return -EFAULT;
3850 } else {
3851 /* ubuf cannot hold the string with NULL terminator,
3852 * do a partial copy with NULL terminator.
3853 */
3854 char zero = '\0';
3855
3856 err = -ENOSPC;
3857 if (copy_to_user(ubuf, buf, input_len - 1))
3858 return -EFAULT;
3859 if (put_user(zero, ubuf + input_len - 1))
3860 return -EFAULT;
3861 }
3862 }
3863
3864 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3865 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3866 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3867 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3868 return -EFAULT;
3869
3870 return err;
3871}
3872
3873#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3874
3875static int bpf_task_fd_query(const union bpf_attr *attr,
3876 union bpf_attr __user *uattr)
3877{
3878 pid_t pid = attr->task_fd_query.pid;
3879 u32 fd = attr->task_fd_query.fd;
3880 const struct perf_event *event;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003881 struct task_struct *task;
3882 struct file *file;
3883 int err;
3884
3885 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3886 return -EINVAL;
3887
3888 if (!capable(CAP_SYS_ADMIN))
3889 return -EPERM;
3890
3891 if (attr->task_fd_query.flags != 0)
3892 return -EINVAL;
3893
3894 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3895 if (!task)
3896 return -ENOENT;
3897
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003898 err = 0;
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06003899 file = fget_task(task, fd);
3900 put_task_struct(task);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003901 if (!file)
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06003902 return -EBADF;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003903
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003904 if (file->f_op == &bpf_link_fops) {
3905 struct bpf_link *link = file->private_data;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003906
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003907 if (link->ops == &bpf_raw_tp_link_lops) {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003908 struct bpf_raw_tp_link *raw_tp =
3909 container_of(link, struct bpf_raw_tp_link, link);
3910 struct bpf_raw_event_map *btp = raw_tp->btp;
3911
3912 err = bpf_task_fd_query_copy(attr, uattr,
3913 raw_tp->link.prog->aux->id,
3914 BPF_FD_TYPE_RAW_TRACEPOINT,
3915 btp->tp->name, 0, 0);
3916 goto put_file;
3917 }
3918 goto out_not_supp;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003919 }
3920
3921 event = perf_get_event(file);
3922 if (!IS_ERR(event)) {
3923 u64 probe_offset, probe_addr;
3924 u32 prog_id, fd_type;
3925 const char *buf;
3926
3927 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3928 &buf, &probe_offset,
3929 &probe_addr);
3930 if (!err)
3931 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3932 fd_type, buf,
3933 probe_offset,
3934 probe_addr);
3935 goto put_file;
3936 }
3937
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003938out_not_supp:
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003939 err = -ENOTSUPP;
3940put_file:
3941 fput(file);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003942 return err;
3943}
3944
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003945#define BPF_MAP_BATCH_LAST_FIELD batch.flags
3946
3947#define BPF_DO_BATCH(fn) \
3948 do { \
3949 if (!fn) { \
3950 err = -ENOTSUPP; \
3951 goto err_put; \
3952 } \
3953 err = fn(map, attr, uattr); \
3954 } while (0)
3955
3956static int bpf_map_do_batch(const union bpf_attr *attr,
3957 union bpf_attr __user *uattr,
3958 int cmd)
3959{
3960 struct bpf_map *map;
3961 int err, ufd;
3962 struct fd f;
3963
3964 if (CHECK_ATTR(BPF_MAP_BATCH))
3965 return -EINVAL;
3966
3967 ufd = attr->batch.map_fd;
3968 f = fdget(ufd);
3969 map = __bpf_map_get(f);
3970 if (IS_ERR(map))
3971 return PTR_ERR(map);
3972
Yonghong Song05799632020-01-15 10:43:04 -08003973 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3974 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003975 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3976 err = -EPERM;
3977 goto err_put;
3978 }
3979
3980 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3981 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3982 err = -EPERM;
3983 goto err_put;
3984 }
3985
3986 if (cmd == BPF_MAP_LOOKUP_BATCH)
3987 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08003988 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
3989 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08003990 else if (cmd == BPF_MAP_UPDATE_BATCH)
3991 BPF_DO_BATCH(map->ops->map_update_batch);
3992 else
3993 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003994
3995err_put:
3996 fdput(f);
3997 return err;
3998}
3999
Yonghong Songde4e05c2020-05-09 10:59:01 -07004000static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
4001{
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004002 if (attr->link_create.attach_type != prog->expected_attach_type)
4003 return -EINVAL;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004004
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004005 if (prog->expected_attach_type == BPF_TRACE_ITER)
4006 return bpf_iter_link_attach(attr, prog);
4007 else if (prog->type == BPF_PROG_TYPE_EXT)
4008 return bpf_tracing_prog_attach(prog,
4009 attr->link_create.target_fd,
4010 attr->link_create.target_btf_id);
Yonghong Songde4e05c2020-05-09 10:59:01 -07004011 return -EINVAL;
4012}
4013
Yonghong Song5e7b3022020-08-04 22:50:56 -07004014#define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004015static int link_create(union bpf_attr *attr)
4016{
4017 enum bpf_prog_type ptype;
4018 struct bpf_prog *prog;
4019 int ret;
4020
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004021 if (CHECK_ATTR(BPF_LINK_CREATE))
4022 return -EINVAL;
4023
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004024 prog = bpf_prog_get(attr->link_create.prog_fd);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004025 if (IS_ERR(prog))
4026 return PTR_ERR(prog);
4027
4028 ret = bpf_prog_attach_check_attach_type(prog,
4029 attr->link_create.attach_type);
4030 if (ret)
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004031 goto out;
4032
4033 if (prog->type == BPF_PROG_TYPE_EXT) {
4034 ret = tracing_bpf_link_attach(attr, prog);
4035 goto out;
4036 }
4037
4038 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4039 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4040 ret = -EINVAL;
4041 goto out;
4042 }
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004043
4044 switch (ptype) {
4045 case BPF_PROG_TYPE_CGROUP_SKB:
4046 case BPF_PROG_TYPE_CGROUP_SOCK:
4047 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4048 case BPF_PROG_TYPE_SOCK_OPS:
4049 case BPF_PROG_TYPE_CGROUP_DEVICE:
4050 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4051 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4052 ret = cgroup_bpf_link_attach(attr, prog);
4053 break;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004054 case BPF_PROG_TYPE_TRACING:
4055 ret = tracing_bpf_link_attach(attr, prog);
4056 break;
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004057 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02004058 case BPF_PROG_TYPE_SK_LOOKUP:
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004059 ret = netns_bpf_link_create(attr, prog);
4060 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004061#ifdef CONFIG_NET
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07004062 case BPF_PROG_TYPE_XDP:
4063 ret = bpf_xdp_link_attach(attr, prog);
4064 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004065#endif
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004066 default:
4067 ret = -EINVAL;
4068 }
4069
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004070out:
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004071 if (ret < 0)
4072 bpf_prog_put(prog);
4073 return ret;
4074}
4075
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004076#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4077
4078static int link_update(union bpf_attr *attr)
4079{
4080 struct bpf_prog *old_prog = NULL, *new_prog;
4081 struct bpf_link *link;
4082 u32 flags;
4083 int ret;
4084
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004085 if (CHECK_ATTR(BPF_LINK_UPDATE))
4086 return -EINVAL;
4087
4088 flags = attr->link_update.flags;
4089 if (flags & ~BPF_F_REPLACE)
4090 return -EINVAL;
4091
4092 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4093 if (IS_ERR(link))
4094 return PTR_ERR(link);
4095
4096 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004097 if (IS_ERR(new_prog)) {
4098 ret = PTR_ERR(new_prog);
4099 goto out_put_link;
4100 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004101
4102 if (flags & BPF_F_REPLACE) {
4103 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4104 if (IS_ERR(old_prog)) {
4105 ret = PTR_ERR(old_prog);
4106 old_prog = NULL;
4107 goto out_put_progs;
4108 }
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004109 } else if (attr->link_update.old_prog_fd) {
4110 ret = -EINVAL;
4111 goto out_put_progs;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004112 }
4113
Andrii Nakryikof9d04122020-04-28 17:16:05 -07004114 if (link->ops->update_prog)
4115 ret = link->ops->update_prog(link, new_prog, old_prog);
4116 else
Jakub Sitnickife537392020-05-25 14:29:28 +02004117 ret = -EINVAL;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004118
4119out_put_progs:
4120 if (old_prog)
4121 bpf_prog_put(old_prog);
4122 if (ret)
4123 bpf_prog_put(new_prog);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004124out_put_link:
4125 bpf_link_put(link);
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004126 return ret;
4127}
4128
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004129#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4130
4131static int link_detach(union bpf_attr *attr)
4132{
4133 struct bpf_link *link;
4134 int ret;
4135
4136 if (CHECK_ATTR(BPF_LINK_DETACH))
4137 return -EINVAL;
4138
4139 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4140 if (IS_ERR(link))
4141 return PTR_ERR(link);
4142
4143 if (link->ops->detach)
4144 ret = link->ops->detach(link);
4145 else
4146 ret = -EOPNOTSUPP;
4147
4148 bpf_link_put(link);
4149 return ret;
4150}
4151
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004152static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004153{
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004154 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4155}
4156
4157struct bpf_link *bpf_link_by_id(u32 id)
4158{
4159 struct bpf_link *link;
4160
4161 if (!id)
4162 return ERR_PTR(-ENOENT);
4163
4164 spin_lock_bh(&link_idr_lock);
4165 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4166 link = idr_find(&link_idr, id);
4167 if (link) {
4168 if (link->id)
4169 link = bpf_link_inc_not_zero(link);
4170 else
4171 link = ERR_PTR(-EAGAIN);
4172 } else {
4173 link = ERR_PTR(-ENOENT);
4174 }
4175 spin_unlock_bh(&link_idr_lock);
4176 return link;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004177}
4178
4179#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4180
4181static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4182{
4183 struct bpf_link *link;
4184 u32 id = attr->link_id;
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004185 int fd;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004186
4187 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4188 return -EINVAL;
4189
4190 if (!capable(CAP_SYS_ADMIN))
4191 return -EPERM;
4192
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004193 link = bpf_link_by_id(id);
4194 if (IS_ERR(link))
4195 return PTR_ERR(link);
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004196
4197 fd = bpf_link_new_fd(link);
4198 if (fd < 0)
4199 bpf_link_put(link);
4200
4201 return fd;
4202}
4203
Song Liud46edd62020-04-30 00:15:04 -07004204DEFINE_MUTEX(bpf_stats_enabled_mutex);
4205
4206static int bpf_stats_release(struct inode *inode, struct file *file)
4207{
4208 mutex_lock(&bpf_stats_enabled_mutex);
4209 static_key_slow_dec(&bpf_stats_enabled_key.key);
4210 mutex_unlock(&bpf_stats_enabled_mutex);
4211 return 0;
4212}
4213
4214static const struct file_operations bpf_stats_fops = {
4215 .release = bpf_stats_release,
4216};
4217
4218static int bpf_enable_runtime_stats(void)
4219{
4220 int fd;
4221
4222 mutex_lock(&bpf_stats_enabled_mutex);
4223
4224 /* Set a very high limit to avoid overflow */
4225 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4226 mutex_unlock(&bpf_stats_enabled_mutex);
4227 return -EBUSY;
4228 }
4229
4230 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4231 if (fd >= 0)
4232 static_key_slow_inc(&bpf_stats_enabled_key.key);
4233
4234 mutex_unlock(&bpf_stats_enabled_mutex);
4235 return fd;
4236}
4237
4238#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4239
4240static int bpf_enable_stats(union bpf_attr *attr)
4241{
4242
4243 if (CHECK_ATTR(BPF_ENABLE_STATS))
4244 return -EINVAL;
4245
4246 if (!capable(CAP_SYS_ADMIN))
4247 return -EPERM;
4248
4249 switch (attr->enable_stats.type) {
4250 case BPF_STATS_RUN_TIME:
4251 return bpf_enable_runtime_stats();
4252 default:
4253 break;
4254 }
4255 return -EINVAL;
4256}
4257
Yonghong Songac51d992020-05-09 10:59:05 -07004258#define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4259
4260static int bpf_iter_create(union bpf_attr *attr)
4261{
4262 struct bpf_link *link;
4263 int err;
4264
4265 if (CHECK_ATTR(BPF_ITER_CREATE))
4266 return -EINVAL;
4267
4268 if (attr->iter_create.flags)
4269 return -EINVAL;
4270
4271 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4272 if (IS_ERR(link))
4273 return PTR_ERR(link);
4274
4275 err = bpf_iter_new_fd(link);
4276 bpf_link_put(link);
4277
4278 return err;
4279}
4280
YiFei Zhuef153142020-09-15 16:45:40 -07004281#define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4282
4283static int bpf_prog_bind_map(union bpf_attr *attr)
4284{
4285 struct bpf_prog *prog;
4286 struct bpf_map *map;
4287 struct bpf_map **used_maps_old, **used_maps_new;
4288 int i, ret = 0;
4289
4290 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4291 return -EINVAL;
4292
4293 if (attr->prog_bind_map.flags)
4294 return -EINVAL;
4295
4296 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4297 if (IS_ERR(prog))
4298 return PTR_ERR(prog);
4299
4300 map = bpf_map_get(attr->prog_bind_map.map_fd);
4301 if (IS_ERR(map)) {
4302 ret = PTR_ERR(map);
4303 goto out_prog_put;
4304 }
4305
4306 mutex_lock(&prog->aux->used_maps_mutex);
4307
4308 used_maps_old = prog->aux->used_maps;
4309
4310 for (i = 0; i < prog->aux->used_map_cnt; i++)
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004311 if (used_maps_old[i] == map) {
4312 bpf_map_put(map);
YiFei Zhuef153142020-09-15 16:45:40 -07004313 goto out_unlock;
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004314 }
YiFei Zhuef153142020-09-15 16:45:40 -07004315
4316 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4317 sizeof(used_maps_new[0]),
4318 GFP_KERNEL);
4319 if (!used_maps_new) {
4320 ret = -ENOMEM;
4321 goto out_unlock;
4322 }
4323
4324 memcpy(used_maps_new, used_maps_old,
4325 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4326 used_maps_new[prog->aux->used_map_cnt] = map;
4327
4328 prog->aux->used_map_cnt++;
4329 prog->aux->used_maps = used_maps_new;
4330
4331 kfree(used_maps_old);
4332
4333out_unlock:
4334 mutex_unlock(&prog->aux->used_maps_mutex);
4335
4336 if (ret)
4337 bpf_map_put(map);
4338out_prog_put:
4339 bpf_prog_put(prog);
4340 return ret;
4341}
4342
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004343SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4344{
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004345 union bpf_attr attr;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004346 int err;
4347
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07004348 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004349 return -EPERM;
4350
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07004351 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004352 if (err)
4353 return err;
4354 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004355
4356 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004357 memset(&attr, 0, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004358 if (copy_from_user(&attr, uattr, size) != 0)
4359 return -EFAULT;
4360
Chenbo Fengafdb09c2017-10-18 13:00:24 -07004361 err = security_bpf(cmd, &attr, size);
4362 if (err < 0)
4363 return err;
4364
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004365 switch (cmd) {
4366 case BPF_MAP_CREATE:
4367 err = map_create(&attr);
4368 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004369 case BPF_MAP_LOOKUP_ELEM:
4370 err = map_lookup_elem(&attr);
4371 break;
4372 case BPF_MAP_UPDATE_ELEM:
4373 err = map_update_elem(&attr);
4374 break;
4375 case BPF_MAP_DELETE_ELEM:
4376 err = map_delete_elem(&attr);
4377 break;
4378 case BPF_MAP_GET_NEXT_KEY:
4379 err = map_get_next_key(&attr);
4380 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02004381 case BPF_MAP_FREEZE:
4382 err = map_freeze(&attr);
4383 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004384 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08004385 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004386 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01004387 case BPF_OBJ_PIN:
4388 err = bpf_obj_pin(&attr);
4389 break;
4390 case BPF_OBJ_GET:
4391 err = bpf_obj_get(&attr);
4392 break;
Daniel Mackf4324552016-11-23 16:52:27 +01004393 case BPF_PROG_ATTACH:
4394 err = bpf_prog_attach(&attr);
4395 break;
4396 case BPF_PROG_DETACH:
4397 err = bpf_prog_detach(&attr);
4398 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004399 case BPF_PROG_QUERY:
4400 err = bpf_prog_query(&attr, uattr);
4401 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004402 case BPF_PROG_TEST_RUN:
4403 err = bpf_prog_test_run(&attr, uattr);
4404 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004405 case BPF_PROG_GET_NEXT_ID:
4406 err = bpf_obj_get_next_id(&attr, uattr,
4407 &prog_idr, &prog_idr_lock);
4408 break;
4409 case BPF_MAP_GET_NEXT_ID:
4410 err = bpf_obj_get_next_id(&attr, uattr,
4411 &map_idr, &map_idr_lock);
4412 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004413 case BPF_BTF_GET_NEXT_ID:
4414 err = bpf_obj_get_next_id(&attr, uattr,
4415 &btf_idr, &btf_idr_lock);
4416 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07004417 case BPF_PROG_GET_FD_BY_ID:
4418 err = bpf_prog_get_fd_by_id(&attr);
4419 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07004420 case BPF_MAP_GET_FD_BY_ID:
4421 err = bpf_map_get_fd_by_id(&attr);
4422 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004423 case BPF_OBJ_GET_INFO_BY_FD:
4424 err = bpf_obj_get_info_by_fd(&attr, uattr);
4425 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07004426 case BPF_RAW_TRACEPOINT_OPEN:
4427 err = bpf_raw_tracepoint_open(&attr);
4428 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004429 case BPF_BTF_LOAD:
4430 err = bpf_btf_load(&attr);
4431 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004432 case BPF_BTF_GET_FD_BY_ID:
4433 err = bpf_btf_get_fd_by_id(&attr);
4434 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004435 case BPF_TASK_FD_QUERY:
4436 err = bpf_task_fd_query(&attr, uattr);
4437 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02004438 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4439 err = map_lookup_and_delete_elem(&attr);
4440 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004441 case BPF_MAP_LOOKUP_BATCH:
4442 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4443 break;
Yonghong Song05799632020-01-15 10:43:04 -08004444 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4445 err = bpf_map_do_batch(&attr, uattr,
4446 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4447 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004448 case BPF_MAP_UPDATE_BATCH:
4449 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4450 break;
4451 case BPF_MAP_DELETE_BATCH:
4452 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4453 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004454 case BPF_LINK_CREATE:
4455 err = link_create(&attr);
4456 break;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004457 case BPF_LINK_UPDATE:
4458 err = link_update(&attr);
4459 break;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004460 case BPF_LINK_GET_FD_BY_ID:
4461 err = bpf_link_get_fd_by_id(&attr);
4462 break;
4463 case BPF_LINK_GET_NEXT_ID:
4464 err = bpf_obj_get_next_id(&attr, uattr,
4465 &link_idr, &link_idr_lock);
4466 break;
Song Liud46edd62020-04-30 00:15:04 -07004467 case BPF_ENABLE_STATS:
4468 err = bpf_enable_stats(&attr);
4469 break;
Yonghong Songac51d992020-05-09 10:59:05 -07004470 case BPF_ITER_CREATE:
4471 err = bpf_iter_create(&attr);
4472 break;
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004473 case BPF_LINK_DETACH:
4474 err = link_detach(&attr);
4475 break;
YiFei Zhuef153142020-09-15 16:45:40 -07004476 case BPF_PROG_BIND_MAP:
4477 err = bpf_prog_bind_map(&attr);
4478 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004479 default:
4480 err = -EINVAL;
4481 break;
4482 }
4483
4484 return err;
4485}