blob: b904d56ec6860828898894ab5bab47b5d21fac61 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003 */
4#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01005#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +01006#include <linux/bpf_lirc.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07007#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07008#include <linux/syscalls.h>
9#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010010#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010011#include <linux/vmalloc.h>
12#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070013#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070014#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070015#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070016#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070017#include <linux/license.h>
18#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070019#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010020#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070021#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070022#include <linux/cred.h>
23#include <linux/timekeeping.h>
24#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010025#include <linux/nospec.h>
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -070026#include <uapi/linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070027
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070028#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
32#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
33#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
34
Chenbo Feng6e71b042017-10-18 13:00:22 -070035#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
36
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080037DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070038static DEFINE_IDR(prog_idr);
39static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070040static DEFINE_IDR(map_idr);
41static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080042
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070043int sysctl_unprivileged_bpf_disabled __read_mostly;
44
Johannes Berg40077e02017-04-11 15:34:58 +020045static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080046#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020047#define BPF_MAP_TYPE(_id, _ops) \
48 [_id] = &_ops,
49#include <linux/bpf_types.h>
50#undef BPF_PROG_TYPE
51#undef BPF_MAP_TYPE
52};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070053
Mickaël Salaün752ba562017-08-07 20:45:20 +020054/*
55 * If we're handed a bigger struct than we know of, ensure all the unknown bits
56 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
57 * we don't know about yet.
58 *
59 * There is a ToCToU between this function call and the following
60 * copy_from_user() call. However, this is not a concern since this function is
61 * meant to be a future-proofing of bits.
62 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070063int bpf_check_uarg_tail_zero(void __user *uaddr,
64 size_t expected_size,
65 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020066{
67 unsigned char __user *addr;
68 unsigned char __user *end;
69 unsigned char val;
70 int err;
71
Mickaël Salaün752ba562017-08-07 20:45:20 +020072 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
73 return -E2BIG;
74
Linus Torvalds96d4f262019-01-03 18:57:57 -080075 if (unlikely(!access_ok(uaddr, actual_size)))
Mickaël Salaün752ba562017-08-07 20:45:20 +020076 return -EFAULT;
77
Mickaël Salaün58291a72017-08-07 20:45:19 +020078 if (actual_size <= expected_size)
79 return 0;
80
81 addr = uaddr + expected_size;
82 end = uaddr + actual_size;
83
84 for (; addr < end; addr++) {
85 err = get_user(val, addr);
86 if (err)
87 return err;
88 if (val)
89 return -E2BIG;
90 }
91
92 return 0;
93}
94
Jakub Kicinskia3884572018-01-11 20:29:09 -080095const struct bpf_map_ops bpf_map_offload_ops = {
96 .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
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100130static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100131{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100132 /* We really just want to fail instead of triggering OOM killer
133 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
134 * which is used for lower order allocation requests.
135 *
136 * It has been observed that higher order allocation requests done by
137 * vmalloc with __GFP_NORETRY being set might fail due to not trying
138 * to reclaim memory from the page cache, thus we set
139 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100140 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100141
142 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100143 void *area;
144
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100145 if (size >= SIZE_MAX)
146 return NULL;
147
Andrii Nakryikofc970222019-11-17 09:28:04 -0800148 /* kmalloc()'ed memory can't be mmap()'ed */
149 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100150 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
151 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100152 if (area != NULL)
153 return area;
154 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800155 if (mmapable) {
156 BUG_ON(!PAGE_ALIGNED(size));
157 return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
158 __GFP_RETRY_MAYFAIL | flags);
159 }
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100160 return __vmalloc_node_flags_caller(size, numa_node,
161 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
162 flags, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100163}
164
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100165void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800166{
167 return __bpf_map_area_alloc(size, numa_node, false);
168}
169
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100170void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800171{
172 return __bpf_map_area_alloc(size, numa_node, true);
173}
174
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100175void bpf_map_area_free(void *area)
176{
177 kvfree(area);
178}
179
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200180static u32 bpf_map_flags_retain_permanent(u32 flags)
181{
182 /* Some map creation flags are not tied to the map object but
183 * rather to the map fd instead, so they have no meaning upon
184 * map object inspection since multiple file descriptors with
185 * different (access) properties can exist here. Thus, given
186 * this has zero meaning for the map itself, lets clear these
187 * from here.
188 */
189 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
190}
191
Jakub Kicinskibd475642018-01-11 20:29:06 -0800192void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
193{
194 map->map_type = attr->map_type;
195 map->key_size = attr->key_size;
196 map->value_size = attr->value_size;
197 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200198 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800199 map->numa_node = bpf_map_attr_numa_node(attr);
200}
201
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700202static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700203{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700204 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700205
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700206 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
207 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700208 return -EPERM;
209 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700210 return 0;
211}
212
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700213static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
214{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700215 if (user)
216 atomic_long_sub(pages, &user->locked_vm);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700217}
218
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100219int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700220{
Roman Gushchinc85d6912019-05-29 18:03:59 -0700221 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
222 struct user_struct *user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700223 int ret;
224
Roman Gushchinc85d6912019-05-29 18:03:59 -0700225 if (size >= U32_MAX - PAGE_SIZE)
226 return -E2BIG;
227
228 user = get_current_user();
Roman Gushchinb936ca62019-05-29 18:03:58 -0700229 ret = bpf_charge_memlock(user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700230 if (ret) {
231 free_uid(user);
232 return ret;
233 }
Roman Gushchinb936ca62019-05-29 18:03:58 -0700234
235 mem->pages = pages;
236 mem->user = user;
237
238 return 0;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700239}
240
Roman Gushchinb936ca62019-05-29 18:03:58 -0700241void bpf_map_charge_finish(struct bpf_map_memory *mem)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700242{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700243 bpf_uncharge_memlock(mem->user, mem->pages);
244 free_uid(mem->user);
245}
Roman Gushchin3539b962019-05-29 18:03:57 -0700246
Roman Gushchinb936ca62019-05-29 18:03:58 -0700247void bpf_map_charge_move(struct bpf_map_memory *dst,
248 struct bpf_map_memory *src)
249{
250 *dst = *src;
251
252 /* Make sure src will not be used for the redundant uncharging. */
253 memset(src, 0, sizeof(struct bpf_map_memory));
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700254}
255
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700256int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
257{
258 int ret;
259
Roman Gushchin3539b962019-05-29 18:03:57 -0700260 ret = bpf_charge_memlock(map->memory.user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700261 if (ret)
262 return ret;
Roman Gushchin3539b962019-05-29 18:03:57 -0700263 map->memory.pages += pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700264 return ret;
265}
266
267void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
268{
Roman Gushchin3539b962019-05-29 18:03:57 -0700269 bpf_uncharge_memlock(map->memory.user, pages);
270 map->memory.pages -= pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700271}
272
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700273static int bpf_map_alloc_id(struct bpf_map *map)
274{
275 int id;
276
Shaohua Lib76354c2018-03-27 11:53:21 -0700277 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700278 spin_lock_bh(&map_idr_lock);
279 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
280 if (id > 0)
281 map->id = id;
282 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700283 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700284
285 if (WARN_ON_ONCE(!id))
286 return -ENOSPC;
287
288 return id > 0 ? 0 : id;
289}
290
Jakub Kicinskia3884572018-01-11 20:29:09 -0800291void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700292{
Eric Dumazet930651a2017-09-19 09:15:59 -0700293 unsigned long flags;
294
Jakub Kicinskia3884572018-01-11 20:29:09 -0800295 /* Offloaded maps are removed from the IDR store when their device
296 * disappears - even if someone holds an fd to them they are unusable,
297 * the memory is gone, all ops will fail; they are simply waiting for
298 * refcnt to drop to be freed.
299 */
300 if (!map->id)
301 return;
302
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700303 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700304 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700305 else
306 __acquire(&map_idr_lock);
307
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700308 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800309 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700310
311 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700312 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700313 else
314 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700315}
316
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700317/* called from workqueue */
318static void bpf_map_free_deferred(struct work_struct *work)
319{
320 struct bpf_map *map = container_of(work, struct bpf_map, work);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700321 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700322
Roman Gushchinb936ca62019-05-29 18:03:58 -0700323 bpf_map_charge_move(&mem, &map->memory);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700324 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700325 /* implementation dependent freeing */
326 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700327 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700328}
329
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100330static void bpf_map_put_uref(struct bpf_map *map)
331{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800332 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700333 if (map->ops->map_release_uref)
334 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100335 }
336}
337
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700338/* decrement map refcnt and schedule it for freeing via workqueue
339 * (unrelying map implementation ops->map_free() might sleep)
340 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700341static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700342{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800343 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700344 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700345 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700346 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700347 INIT_WORK(&map->work, bpf_map_free_deferred);
348 schedule_work(&map->work);
349 }
350}
351
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700352void bpf_map_put(struct bpf_map *map)
353{
354 __bpf_map_put(map, true);
355}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700356EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700357
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100358void bpf_map_put_with_uref(struct bpf_map *map)
359{
360 bpf_map_put_uref(map);
361 bpf_map_put(map);
362}
363
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700364static int bpf_map_release(struct inode *inode, struct file *filp)
365{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200366 struct bpf_map *map = filp->private_data;
367
368 if (map->ops->map_release)
369 map->ops->map_release(map, filp);
370
371 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700372 return 0;
373}
374
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200375static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
376{
377 fmode_t mode = f.file->f_mode;
378
379 /* Our file permissions may have been overridden by global
380 * map permissions facing syscall side.
381 */
382 if (READ_ONCE(map->frozen))
383 mode &= ~FMODE_CAN_WRITE;
384 return mode;
385}
386
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100387#ifdef CONFIG_PROC_FS
388static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
389{
390 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100391 const struct bpf_array *array;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100392 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100393
394 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
395 array = container_of(map, struct bpf_array, map);
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100396 type = array->aux->type;
397 jited = array->aux->jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100398 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100399
400 seq_printf(m,
401 "map_type:\t%u\n"
402 "key_size:\t%u\n"
403 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100404 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100405 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200406 "memlock:\t%llu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200407 "map_id:\t%u\n"
408 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100409 map->map_type,
410 map->key_size,
411 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100412 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100413 map->map_flags,
Roman Gushchin3539b962019-05-29 18:03:57 -0700414 map->memory.pages * 1ULL << PAGE_SHIFT,
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200415 map->id,
416 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100417 if (type) {
418 seq_printf(m, "owner_prog_type:\t%u\n", type);
419 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200420 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100421}
422#endif
423
Chenbo Feng6e71b042017-10-18 13:00:22 -0700424static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
425 loff_t *ppos)
426{
427 /* We need this handler such that alloc_file() enables
428 * f_mode with FMODE_CAN_READ.
429 */
430 return -EINVAL;
431}
432
433static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
434 size_t siz, loff_t *ppos)
435{
436 /* We need this handler such that alloc_file() enables
437 * f_mode with FMODE_CAN_WRITE.
438 */
439 return -EINVAL;
440}
441
Andrii Nakryikofc970222019-11-17 09:28:04 -0800442/* called for any extra memory-mapped regions (except initial) */
443static void bpf_map_mmap_open(struct vm_area_struct *vma)
444{
445 struct bpf_map *map = vma->vm_file->private_data;
446
447 bpf_map_inc_with_uref(map);
448
449 if (vma->vm_flags & VM_WRITE) {
450 mutex_lock(&map->freeze_mutex);
451 map->writecnt++;
452 mutex_unlock(&map->freeze_mutex);
453 }
454}
455
456/* called for all unmapped memory region (including initial) */
457static void bpf_map_mmap_close(struct vm_area_struct *vma)
458{
459 struct bpf_map *map = vma->vm_file->private_data;
460
461 if (vma->vm_flags & VM_WRITE) {
462 mutex_lock(&map->freeze_mutex);
463 map->writecnt--;
464 mutex_unlock(&map->freeze_mutex);
465 }
466
467 bpf_map_put_with_uref(map);
468}
469
470static const struct vm_operations_struct bpf_map_default_vmops = {
471 .open = bpf_map_mmap_open,
472 .close = bpf_map_mmap_close,
473};
474
475static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
476{
477 struct bpf_map *map = filp->private_data;
478 int err;
479
480 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
481 return -ENOTSUPP;
482
483 if (!(vma->vm_flags & VM_SHARED))
484 return -EINVAL;
485
486 mutex_lock(&map->freeze_mutex);
487
488 if ((vma->vm_flags & VM_WRITE) && map->frozen) {
489 err = -EPERM;
490 goto out;
491 }
492
493 /* set default open/close callbacks */
494 vma->vm_ops = &bpf_map_default_vmops;
495 vma->vm_private_data = map;
496
497 err = map->ops->map_mmap(map, vma);
498 if (err)
499 goto out;
500
501 bpf_map_inc_with_uref(map);
502
503 if (vma->vm_flags & VM_WRITE)
504 map->writecnt++;
505out:
506 mutex_unlock(&map->freeze_mutex);
507 return err;
508}
509
Chenbo Fengf66e4482017-10-18 13:00:26 -0700510const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100511#ifdef CONFIG_PROC_FS
512 .show_fdinfo = bpf_map_show_fdinfo,
513#endif
514 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700515 .read = bpf_dummy_read,
516 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800517 .mmap = bpf_map_mmap,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700518};
519
Chenbo Feng6e71b042017-10-18 13:00:22 -0700520int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100521{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700522 int ret;
523
524 ret = security_bpf_map(map, OPEN_FMODE(flags));
525 if (ret < 0)
526 return ret;
527
Daniel Borkmannaa797812015-10-29 14:58:06 +0100528 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700529 flags | O_CLOEXEC);
530}
531
532int bpf_get_file_flag(int flags)
533{
534 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
535 return -EINVAL;
536 if (flags & BPF_F_RDONLY)
537 return O_RDONLY;
538 if (flags & BPF_F_WRONLY)
539 return O_WRONLY;
540 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100541}
542
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700543/* helper macro to check that unused fields 'union bpf_attr' are zero */
544#define CHECK_ATTR(CMD) \
545 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
546 sizeof(attr->CMD##_LAST_FIELD), 0, \
547 sizeof(*attr) - \
548 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
549 sizeof(attr->CMD##_LAST_FIELD)) != NULL
550
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700551/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
552 * Return 0 on success and < 0 on error.
553 */
554static int bpf_obj_name_cpy(char *dst, const char *src)
555{
556 const char *end = src + BPF_OBJ_NAME_LEN;
557
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700558 memset(dst, 0, BPF_OBJ_NAME_LEN);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200559 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700560 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200561 if (!isalnum(*src) &&
562 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700563 return -EINVAL;
564 *dst++ = *src++;
565 }
566
567 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
568 if (src == end)
569 return -EINVAL;
570
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700571 return 0;
572}
573
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200574int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800575 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200576 const struct btf_type *key_type,
577 const struct btf_type *value_type)
578{
579 return -ENOTSUPP;
580}
581
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800582static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200583 u32 btf_key_id, u32 btf_value_id)
584{
585 const struct btf_type *key_type, *value_type;
586 u32 key_size, value_size;
587 int ret = 0;
588
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200589 /* Some maps allow key to be unspecified. */
590 if (btf_key_id) {
591 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
592 if (!key_type || key_size != map->key_size)
593 return -EINVAL;
594 } else {
595 key_type = btf_type_by_id(btf, 0);
596 if (!map->ops->map_check_btf)
597 return -EINVAL;
598 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200599
600 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
601 if (!value_type || value_size != map->value_size)
602 return -EINVAL;
603
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800604 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
605
606 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200607 if (map->map_flags & BPF_F_RDONLY_PROG)
608 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800609 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800610 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700611 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
612 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800613 return -ENOTSUPP;
614 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
615 map->value_size) {
616 WARN_ONCE(1,
617 "verifier bug spin_lock_off %d value_size %d\n",
618 map->spin_lock_off, map->value_size);
619 return -EFAULT;
620 }
621 }
622
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200623 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800624 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200625
626 return ret;
627}
628
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700629#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700630/* called via syscall */
631static int map_create(union bpf_attr *attr)
632{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700633 int numa_node = bpf_map_attr_numa_node(attr);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700634 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700635 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700636 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700637 int err;
638
639 err = CHECK_ATTR(BPF_MAP_CREATE);
640 if (err)
641 return -EINVAL;
642
Chenbo Feng6e71b042017-10-18 13:00:22 -0700643 f_flags = bpf_get_file_flag(attr->map_flags);
644 if (f_flags < 0)
645 return f_flags;
646
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700647 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700648 ((unsigned int)numa_node >= nr_node_ids ||
649 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700650 return -EINVAL;
651
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700652 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
653 map = find_and_alloc_map(attr);
654 if (IS_ERR(map))
655 return PTR_ERR(map);
656
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700657 err = bpf_obj_name_cpy(map->name, attr->map_name);
658 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700659 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700660
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800661 atomic64_set(&map->refcnt, 1);
662 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800663 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700664
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200665 if (attr->btf_key_type_id || attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700666 struct btf *btf;
667
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200668 if (!attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700669 err = -EINVAL;
Roman Gushchinb936ca62019-05-29 18:03:58 -0700670 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700671 }
672
673 btf = btf_get_by_fd(attr->btf_fd);
674 if (IS_ERR(btf)) {
675 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700676 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700677 }
678
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200679 err = map_check_btf(map, btf, attr->btf_key_type_id,
680 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700681 if (err) {
682 btf_put(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700683 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700684 }
685
686 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700687 map->btf_key_type_id = attr->btf_key_type_id;
688 map->btf_value_type_id = attr->btf_value_type_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800689 } else {
690 map->spin_lock_off = -EINVAL;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700691 }
692
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700693 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700694 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700695 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700696
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700697 err = bpf_map_alloc_id(map);
698 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700699 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700700
Chenbo Feng6e71b042017-10-18 13:00:22 -0700701 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700702 if (err < 0) {
703 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800704 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700705 * bpf_map_alloc_id() has published the map
706 * to the userspace and the userspace may
707 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
708 */
Peng Sun352d20d2019-02-27 22:36:25 +0800709 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700710 return err;
711 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700712
713 return err;
714
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700715free_map_sec:
716 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700717free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700718 btf_put(map->btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700719 bpf_map_charge_move(&mem, &map->memory);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700720 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700721 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700722 return err;
723}
724
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700725/* if error is returned, fd is released.
726 * On success caller should complete fd access with matching fdput()
727 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100728struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700729{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700730 if (!f.file)
731 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700732 if (f.file->f_op != &bpf_map_fops) {
733 fdput(f);
734 return ERR_PTR(-EINVAL);
735 }
736
Daniel Borkmannc2101292015-10-29 14:58:07 +0100737 return f.file->private_data;
738}
739
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800740void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100741{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800742 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100743}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700744EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100745
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800746void bpf_map_inc_with_uref(struct bpf_map *map)
747{
748 atomic64_inc(&map->refcnt);
749 atomic64_inc(&map->usercnt);
750}
751EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
752
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100753struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100754{
755 struct fd f = fdget(ufd);
756 struct bpf_map *map;
757
758 map = __bpf_map_get(f);
759 if (IS_ERR(map))
760 return map;
761
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800762 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100763 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700764
765 return map;
766}
767
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700768/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800769static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700770{
771 int refold;
772
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800773 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700774 if (!refold)
775 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700776 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800777 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700778
779 return map;
780}
781
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800782struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700783{
784 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800785 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700786 spin_unlock_bh(&map_idr_lock);
787
788 return map;
789}
790EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
791
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800792int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
793{
794 return -ENOTSUPP;
795}
796
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200797static void *__bpf_copy_key(void __user *ukey, u64 key_size)
798{
799 if (key_size)
800 return memdup_user(ukey, key_size);
801
802 if (ukey)
803 return ERR_PTR(-EINVAL);
804
805 return NULL;
806}
807
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700808/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800809#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700810
811static int map_lookup_elem(union bpf_attr *attr)
812{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100813 void __user *ukey = u64_to_user_ptr(attr->key);
814 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700815 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700816 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800817 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800818 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200819 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700820 int err;
821
822 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
823 return -EINVAL;
824
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800825 if (attr->flags & ~BPF_F_LOCK)
826 return -EINVAL;
827
Daniel Borkmann592867b2015-09-08 18:00:09 +0200828 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100829 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700830 if (IS_ERR(map))
831 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200832 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700833 err = -EPERM;
834 goto err_put;
835 }
836
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800837 if ((attr->flags & BPF_F_LOCK) &&
838 !map_value_has_spin_lock(map)) {
839 err = -EINVAL;
840 goto err_put;
841 }
842
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200843 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400844 if (IS_ERR(key)) {
845 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700846 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400847 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700848
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800849 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800850 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000851 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
852 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800853 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700854 else if (IS_FD_MAP(map))
855 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800856 else
857 value_size = map->value_size;
858
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800859 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800860 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700861 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800862 goto free_key;
863
Jakub Kicinskia3884572018-01-11 20:29:09 -0800864 if (bpf_map_is_dev_bound(map)) {
865 err = bpf_map_offload_lookup_elem(map, key, value);
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800866 goto done;
867 }
868
869 preempt_disable();
870 this_cpu_inc(bpf_prog_active);
871 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
872 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800873 err = bpf_percpu_hash_copy(map, key, value);
874 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
875 err = bpf_percpu_array_copy(map, key, value);
Roman Gushchinb741f162018-09-28 14:45:43 +0000876 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
877 err = bpf_percpu_cgroup_storage_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800878 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
879 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700880 } else if (IS_FD_ARRAY(map)) {
881 err = bpf_fd_array_map_lookup_elem(map, key, value);
882 } else if (IS_FD_HASH(map)) {
883 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700884 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
885 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200886 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
887 map->map_type == BPF_MAP_TYPE_STACK) {
888 err = map->ops->map_peek_elem(map, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800889 } else {
890 rcu_read_lock();
Daniel Borkmannc6110222019-05-14 01:18:55 +0200891 if (map->ops->map_lookup_elem_sys_only)
892 ptr = map->ops->map_lookup_elem_sys_only(map, key);
893 else
894 ptr = map->ops->map_lookup_elem(map, key);
Prashant Bhole509db282018-10-09 10:04:49 +0900895 if (IS_ERR(ptr)) {
896 err = PTR_ERR(ptr);
897 } else if (!ptr) {
898 err = -ENOENT;
899 } else {
900 err = 0;
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800901 if (attr->flags & BPF_F_LOCK)
902 /* lock 'ptr' and copy everything but lock */
903 copy_map_value_locked(map, value, ptr, true);
904 else
905 copy_map_value(map, value, ptr);
906 /* mask lock, since value wasn't zero inited */
907 check_and_init_map_lock(map, value);
Prashant Bhole509db282018-10-09 10:04:49 +0900908 }
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800909 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800910 }
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800911 this_cpu_dec(bpf_prog_active);
912 preempt_enable();
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800913
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800914done:
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800915 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800916 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700917
918 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800919 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800920 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700921
922 err = 0;
923
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800924free_value:
925 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700926free_key:
927 kfree(key);
928err_put:
929 fdput(f);
930 return err;
931}
932
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700933static void maybe_wait_bpf_programs(struct bpf_map *map)
934{
935 /* Wait for any running BPF programs to complete so that
936 * userspace, when we return to it, knows that all programs
937 * that could be running use the new map value.
938 */
939 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
940 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
941 synchronize_rcu();
942}
943
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800944#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700945
946static int map_update_elem(union bpf_attr *attr)
947{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100948 void __user *ukey = u64_to_user_ptr(attr->key);
949 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700950 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700951 struct bpf_map *map;
952 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800953 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200954 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700955 int err;
956
957 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
958 return -EINVAL;
959
Daniel Borkmann592867b2015-09-08 18:00:09 +0200960 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100961 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700962 if (IS_ERR(map))
963 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200964 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700965 err = -EPERM;
966 goto err_put;
967 }
968
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800969 if ((attr->flags & BPF_F_LOCK) &&
970 !map_value_has_spin_lock(map)) {
971 err = -EINVAL;
972 goto err_put;
973 }
974
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200975 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400976 if (IS_ERR(key)) {
977 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700978 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400979 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700980
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800981 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800982 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000983 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
984 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800985 value_size = round_up(map->value_size, 8) * num_possible_cpus();
986 else
987 value_size = map->value_size;
988
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700989 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800990 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700991 if (!value)
992 goto free_key;
993
994 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800995 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700996 goto free_value;
997
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200998 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800999 if (bpf_map_is_dev_bound(map)) {
1000 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
1001 goto out;
John Fastabend99ba2b52018-07-05 08:50:04 -07001002 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
1003 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
1004 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001005 err = map->ops->map_update_elem(map, key, value, attr->flags);
1006 goto out;
1007 }
1008
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001009 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
1010 * inside bpf map update or delete otherwise deadlocks are possible
1011 */
1012 preempt_disable();
1013 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001014 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1015 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001016 err = bpf_percpu_hash_update(map, key, value, attr->flags);
1017 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
1018 err = bpf_percpu_array_update(map, key, value, attr->flags);
Roman Gushchinb741f162018-09-28 14:45:43 +00001019 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
1020 err = bpf_percpu_cgroup_storage_update(map, key, value,
1021 attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +01001022 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +02001023 rcu_read_lock();
1024 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
1025 attr->flags);
1026 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07001027 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
1028 rcu_read_lock();
1029 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
1030 attr->flags);
1031 rcu_read_unlock();
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -07001032 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
1033 /* rcu_read_lock() is not needed */
1034 err = bpf_fd_reuseport_array_update_elem(map, key, value,
1035 attr->flags);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02001036 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1037 map->map_type == BPF_MAP_TYPE_STACK) {
1038 err = map->ops->map_push_elem(map, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001039 } else {
1040 rcu_read_lock();
1041 err = map->ops->map_update_elem(map, key, value, attr->flags);
1042 rcu_read_unlock();
1043 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001044 __this_cpu_dec(bpf_prog_active);
1045 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001046 maybe_wait_bpf_programs(map);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001047out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001048free_value:
1049 kfree(value);
1050free_key:
1051 kfree(key);
1052err_put:
1053 fdput(f);
1054 return err;
1055}
1056
1057#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1058
1059static int map_delete_elem(union bpf_attr *attr)
1060{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001061 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001062 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001063 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001064 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001065 void *key;
1066 int err;
1067
1068 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1069 return -EINVAL;
1070
Daniel Borkmann592867b2015-09-08 18:00:09 +02001071 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001072 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001073 if (IS_ERR(map))
1074 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001075 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001076 err = -EPERM;
1077 goto err_put;
1078 }
1079
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001080 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001081 if (IS_ERR(key)) {
1082 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001083 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001084 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001085
Jakub Kicinskia3884572018-01-11 20:29:09 -08001086 if (bpf_map_is_dev_bound(map)) {
1087 err = bpf_map_offload_delete_elem(map, key);
1088 goto out;
1089 }
1090
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001091 preempt_disable();
1092 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001093 rcu_read_lock();
1094 err = map->ops->map_delete_elem(map, key);
1095 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001096 __this_cpu_dec(bpf_prog_active);
1097 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001098 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001099out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001100 kfree(key);
1101err_put:
1102 fdput(f);
1103 return err;
1104}
1105
1106/* last field in 'union bpf_attr' used by this command */
1107#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1108
1109static int map_get_next_key(union bpf_attr *attr)
1110{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001111 void __user *ukey = u64_to_user_ptr(attr->key);
1112 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001113 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001114 struct bpf_map *map;
1115 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001116 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001117 int err;
1118
1119 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1120 return -EINVAL;
1121
Daniel Borkmann592867b2015-09-08 18:00:09 +02001122 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001123 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001124 if (IS_ERR(map))
1125 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001126 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001127 err = -EPERM;
1128 goto err_put;
1129 }
1130
Teng Qin8fe45922017-04-24 19:00:37 -07001131 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001132 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001133 if (IS_ERR(key)) {
1134 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001135 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001136 }
Teng Qin8fe45922017-04-24 19:00:37 -07001137 } else {
1138 key = NULL;
1139 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001140
1141 err = -ENOMEM;
1142 next_key = kmalloc(map->key_size, GFP_USER);
1143 if (!next_key)
1144 goto free_key;
1145
Jakub Kicinskia3884572018-01-11 20:29:09 -08001146 if (bpf_map_is_dev_bound(map)) {
1147 err = bpf_map_offload_get_next_key(map, key, next_key);
1148 goto out;
1149 }
1150
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001151 rcu_read_lock();
1152 err = map->ops->map_get_next_key(map, key, next_key);
1153 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001154out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001155 if (err)
1156 goto free_next_key;
1157
1158 err = -EFAULT;
1159 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1160 goto free_next_key;
1161
1162 err = 0;
1163
1164free_next_key:
1165 kfree(next_key);
1166free_key:
1167 kfree(key);
1168err_put:
1169 fdput(f);
1170 return err;
1171}
1172
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001173#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1174
1175static int map_lookup_and_delete_elem(union bpf_attr *attr)
1176{
1177 void __user *ukey = u64_to_user_ptr(attr->key);
1178 void __user *uvalue = u64_to_user_ptr(attr->value);
1179 int ufd = attr->map_fd;
1180 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001181 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001182 u32 value_size;
1183 struct fd f;
1184 int err;
1185
1186 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1187 return -EINVAL;
1188
1189 f = fdget(ufd);
1190 map = __bpf_map_get(f);
1191 if (IS_ERR(map))
1192 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001193 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001194 err = -EPERM;
1195 goto err_put;
1196 }
1197
1198 key = __bpf_copy_key(ukey, map->key_size);
1199 if (IS_ERR(key)) {
1200 err = PTR_ERR(key);
1201 goto err_put;
1202 }
1203
1204 value_size = map->value_size;
1205
1206 err = -ENOMEM;
1207 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1208 if (!value)
1209 goto free_key;
1210
1211 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1212 map->map_type == BPF_MAP_TYPE_STACK) {
1213 err = map->ops->map_pop_elem(map, value);
1214 } else {
1215 err = -ENOTSUPP;
1216 }
1217
1218 if (err)
1219 goto free_value;
1220
1221 if (copy_to_user(uvalue, value, value_size) != 0)
1222 goto free_value;
1223
1224 err = 0;
1225
1226free_value:
1227 kfree(value);
1228free_key:
1229 kfree(key);
1230err_put:
1231 fdput(f);
1232 return err;
1233}
1234
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001235#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1236
1237static int map_freeze(const union bpf_attr *attr)
1238{
1239 int err = 0, ufd = attr->map_fd;
1240 struct bpf_map *map;
1241 struct fd f;
1242
1243 if (CHECK_ATTR(BPF_MAP_FREEZE))
1244 return -EINVAL;
1245
1246 f = fdget(ufd);
1247 map = __bpf_map_get(f);
1248 if (IS_ERR(map))
1249 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001250
1251 mutex_lock(&map->freeze_mutex);
1252
1253 if (map->writecnt) {
1254 err = -EBUSY;
1255 goto err_put;
1256 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001257 if (READ_ONCE(map->frozen)) {
1258 err = -EBUSY;
1259 goto err_put;
1260 }
1261 if (!capable(CAP_SYS_ADMIN)) {
1262 err = -EPERM;
1263 goto err_put;
1264 }
1265
1266 WRITE_ONCE(map->frozen, true);
1267err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001268 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001269 fdput(f);
1270 return err;
1271}
1272
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001273static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001274#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001275 [_id] = & _name ## _prog_ops,
1276#define BPF_MAP_TYPE(_id, _ops)
1277#include <linux/bpf_types.h>
1278#undef BPF_PROG_TYPE
1279#undef BPF_MAP_TYPE
1280};
1281
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001282static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1283{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001284 const struct bpf_prog_ops *ops;
1285
1286 if (type >= ARRAY_SIZE(bpf_prog_types))
1287 return -EINVAL;
1288 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1289 ops = bpf_prog_types[type];
1290 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001291 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001292
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001293 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001294 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001295 else
1296 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001297 prog->type = type;
1298 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001299}
1300
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001301int __bpf_prog_charge(struct user_struct *user, u32 pages)
1302{
1303 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1304 unsigned long user_bufs;
1305
1306 if (user) {
1307 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1308 if (user_bufs > memlock_limit) {
1309 atomic_long_sub(pages, &user->locked_vm);
1310 return -EPERM;
1311 }
1312 }
1313
1314 return 0;
1315}
1316
1317void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1318{
1319 if (user)
1320 atomic_long_sub(pages, &user->locked_vm);
1321}
1322
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001323static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1324{
1325 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001326 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001327
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001328 ret = __bpf_prog_charge(user, prog->pages);
1329 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001330 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001331 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001332 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001333
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001334 prog->aux->user = user;
1335 return 0;
1336}
1337
1338static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1339{
1340 struct user_struct *user = prog->aux->user;
1341
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001342 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001343 free_uid(user);
1344}
1345
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001346static int bpf_prog_alloc_id(struct bpf_prog *prog)
1347{
1348 int id;
1349
Shaohua Lib76354c2018-03-27 11:53:21 -07001350 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001351 spin_lock_bh(&prog_idr_lock);
1352 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1353 if (id > 0)
1354 prog->aux->id = id;
1355 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001356 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001357
1358 /* id is in [1, INT_MAX) */
1359 if (WARN_ON_ONCE(!id))
1360 return -ENOSPC;
1361
1362 return id > 0 ? 0 : id;
1363}
1364
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001365void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001366{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001367 /* cBPF to eBPF migrations are currently not in the idr store.
1368 * Offloaded programs are removed from the store when their device
1369 * disappears - even if someone grabs an fd to them they are unusable,
1370 * simply waiting for refcnt to drop to be freed.
1371 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001372 if (!prog->aux->id)
1373 return;
1374
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001375 if (do_idr_lock)
1376 spin_lock_bh(&prog_idr_lock);
1377 else
1378 __acquire(&prog_idr_lock);
1379
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001380 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001381 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001382
1383 if (do_idr_lock)
1384 spin_unlock_bh(&prog_idr_lock);
1385 else
1386 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001387}
1388
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001389static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001390{
1391 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1392
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001393 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001394 kfree(aux->func_info_aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001395 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001396 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001397 bpf_prog_free(aux->prog);
1398}
1399
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001400static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1401{
1402 bpf_prog_kallsyms_del_all(prog);
1403 btf_put(prog->aux->btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001404 bpf_prog_free_linfo(prog);
1405
1406 if (deferred)
1407 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1408 else
1409 __bpf_prog_put_rcu(&prog->aux->rcu);
1410}
1411
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001412static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001413{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001414 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001415 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001416 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001417 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001418 __bpf_prog_put_noref(prog, true);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001419 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001420}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001421
1422void bpf_prog_put(struct bpf_prog *prog)
1423{
1424 __bpf_prog_put(prog, true);
1425}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001426EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001427
1428static int bpf_prog_release(struct inode *inode, struct file *filp)
1429{
1430 struct bpf_prog *prog = filp->private_data;
1431
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001432 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001433 return 0;
1434}
1435
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001436static void bpf_prog_get_stats(const struct bpf_prog *prog,
1437 struct bpf_prog_stats *stats)
1438{
1439 u64 nsecs = 0, cnt = 0;
1440 int cpu;
1441
1442 for_each_possible_cpu(cpu) {
1443 const struct bpf_prog_stats *st;
1444 unsigned int start;
1445 u64 tnsecs, tcnt;
1446
1447 st = per_cpu_ptr(prog->aux->stats, cpu);
1448 do {
1449 start = u64_stats_fetch_begin_irq(&st->syncp);
1450 tnsecs = st->nsecs;
1451 tcnt = st->cnt;
1452 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1453 nsecs += tnsecs;
1454 cnt += tcnt;
1455 }
1456 stats->nsecs = nsecs;
1457 stats->cnt = cnt;
1458}
1459
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001460#ifdef CONFIG_PROC_FS
1461static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1462{
1463 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001464 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001465 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001466
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001467 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001468 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001469 seq_printf(m,
1470 "prog_type:\t%u\n"
1471 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001472 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001473 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001474 "prog_id:\t%u\n"
1475 "run_time_ns:\t%llu\n"
1476 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001477 prog->type,
1478 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001479 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001480 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001481 prog->aux->id,
1482 stats.nsecs,
1483 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001484}
1485#endif
1486
Chenbo Fengf66e4482017-10-18 13:00:26 -07001487const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001488#ifdef CONFIG_PROC_FS
1489 .show_fdinfo = bpf_prog_show_fdinfo,
1490#endif
1491 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001492 .read = bpf_dummy_read,
1493 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001494};
1495
Daniel Borkmannb2197752015-10-29 14:58:09 +01001496int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001497{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001498 int ret;
1499
1500 ret = security_bpf_prog(prog);
1501 if (ret < 0)
1502 return ret;
1503
Daniel Borkmannaa797812015-10-29 14:58:06 +01001504 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1505 O_RDWR | O_CLOEXEC);
1506}
1507
Daniel Borkmann113214b2016-06-30 17:24:44 +02001508static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001509{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001510 if (!f.file)
1511 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001512 if (f.file->f_op != &bpf_prog_fops) {
1513 fdput(f);
1514 return ERR_PTR(-EINVAL);
1515 }
1516
Daniel Borkmannc2101292015-10-29 14:58:07 +01001517 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001518}
1519
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001520void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001521{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001522 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001523}
Brenden Blanco59d36562016-07-19 12:16:46 -07001524EXPORT_SYMBOL_GPL(bpf_prog_add);
1525
Daniel Borkmannc5405942016-11-09 22:02:34 +01001526void bpf_prog_sub(struct bpf_prog *prog, int i)
1527{
1528 /* Only to be used for undoing previous bpf_prog_add() in some
1529 * error path. We still know that another entity in our call
1530 * path holds a reference to the program, thus atomic_sub() can
1531 * be safely used in such cases!
1532 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001533 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001534}
1535EXPORT_SYMBOL_GPL(bpf_prog_sub);
1536
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001537void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001538{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001539 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001540}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001541EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001542
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001543/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001544struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001545{
1546 int refold;
1547
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001548 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001549
1550 if (!refold)
1551 return ERR_PTR(-ENOENT);
1552
1553 return prog;
1554}
John Fastabenda6f6df62017-08-15 22:32:22 -07001555EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001556
Al Viro040ee692017-12-02 20:20:38 -05001557bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001558 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001559{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001560 /* not an attachment, just a refcount inc, always allow */
1561 if (!attach_type)
1562 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001563
1564 if (prog->type != *attach_type)
1565 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001566 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001567 return false;
1568
1569 return true;
1570}
1571
1572static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001573 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001574{
1575 struct fd f = fdget(ufd);
1576 struct bpf_prog *prog;
1577
Daniel Borkmann113214b2016-06-30 17:24:44 +02001578 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001579 if (IS_ERR(prog))
1580 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001581 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001582 prog = ERR_PTR(-EINVAL);
1583 goto out;
1584 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001585
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001586 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001587out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001588 fdput(f);
1589 return prog;
1590}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001591
1592struct bpf_prog *bpf_prog_get(u32 ufd)
1593{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001594 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001595}
1596
Jakub Kicinski248f3462017-11-03 13:56:20 -07001597struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001598 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001599{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001600 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001601}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001602EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001603
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001604/* Initially all BPF programs could be loaded w/o specifying
1605 * expected_attach_type. Later for some of them specifying expected_attach_type
1606 * at load time became required so that program could be validated properly.
1607 * Programs of types that are allowed to be loaded both w/ and w/o (for
1608 * backward compatibility) expected_attach_type, should have the default attach
1609 * type assigned to expected_attach_type for the latter case, so that it can be
1610 * validated later at attach time.
1611 *
1612 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1613 * prog type requires it but has some attach types that have to be backward
1614 * compatible.
1615 */
1616static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1617{
1618 switch (attr->prog_type) {
1619 case BPF_PROG_TYPE_CGROUP_SOCK:
1620 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1621 * exist so checking for non-zero is the way to go here.
1622 */
1623 if (!attr->expected_attach_type)
1624 attr->expected_attach_type =
1625 BPF_CGROUP_INET_SOCK_CREATE;
1626 break;
1627 }
1628}
1629
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001630static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001631bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1632 enum bpf_attach_type expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001633 u32 btf_id, u32 prog_fd)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001634{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001635 switch (prog_type) {
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001636 case BPF_PROG_TYPE_TRACING:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001637 if (btf_id > BTF_MAX_TYPE)
1638 return -EINVAL;
1639 break;
1640 default:
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001641 if (btf_id || prog_fd)
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001642 return -EINVAL;
1643 break;
1644 }
1645
1646 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001647 case BPF_PROG_TYPE_CGROUP_SOCK:
1648 switch (expected_attach_type) {
1649 case BPF_CGROUP_INET_SOCK_CREATE:
1650 case BPF_CGROUP_INET4_POST_BIND:
1651 case BPF_CGROUP_INET6_POST_BIND:
1652 return 0;
1653 default:
1654 return -EINVAL;
1655 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001656 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1657 switch (expected_attach_type) {
1658 case BPF_CGROUP_INET4_BIND:
1659 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001660 case BPF_CGROUP_INET4_CONNECT:
1661 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001662 case BPF_CGROUP_UDP4_SENDMSG:
1663 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02001664 case BPF_CGROUP_UDP4_RECVMSG:
1665 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001666 return 0;
1667 default:
1668 return -EINVAL;
1669 }
brakmo5cf1e912019-05-28 16:59:36 -07001670 case BPF_PROG_TYPE_CGROUP_SKB:
1671 switch (expected_attach_type) {
1672 case BPF_CGROUP_INET_INGRESS:
1673 case BPF_CGROUP_INET_EGRESS:
1674 return 0;
1675 default:
1676 return -EINVAL;
1677 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001678 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1679 switch (expected_attach_type) {
1680 case BPF_CGROUP_SETSOCKOPT:
1681 case BPF_CGROUP_GETSOCKOPT:
1682 return 0;
1683 default:
1684 return -EINVAL;
1685 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001686 default:
1687 return 0;
1688 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001689}
1690
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001691/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001692#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001693
Yonghong Song838e9692018-11-19 15:29:11 -08001694static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001695{
1696 enum bpf_prog_type type = attr->prog_type;
1697 struct bpf_prog *prog;
1698 int err;
1699 char license[128];
1700 bool is_gpl;
1701
1702 if (CHECK_ATTR(BPF_PROG_LOAD))
1703 return -EINVAL;
1704
Jiong Wangc240eff2019-05-24 23:25:16 +01001705 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
1706 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07001707 BPF_F_TEST_STATE_FREQ |
Jiong Wangc240eff2019-05-24 23:25:16 +01001708 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07001709 return -EINVAL;
1710
David Millere9ee9ef2018-11-30 21:08:14 -08001711 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1712 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1713 !capable(CAP_SYS_ADMIN))
1714 return -EPERM;
1715
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001716 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001717 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001718 sizeof(license) - 1) < 0)
1719 return -EFAULT;
1720 license[sizeof(license) - 1] = 0;
1721
1722 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1723 is_gpl = license_is_gpl_compatible(license);
1724
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07001725 if (attr->insn_cnt == 0 ||
1726 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001727 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07001728 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1729 type != BPF_PROG_TYPE_CGROUP_SKB &&
1730 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001731 return -EPERM;
1732
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001733 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001734 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001735 attr->attach_btf_id,
1736 attr->attach_prog_fd))
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001737 return -EINVAL;
1738
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001739 /* plain bpf_prog allocation */
1740 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1741 if (!prog)
1742 return -ENOMEM;
1743
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001744 prog->expected_attach_type = attr->expected_attach_type;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001745 prog->aux->attach_btf_id = attr->attach_btf_id;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001746 if (attr->attach_prog_fd) {
1747 struct bpf_prog *tgt_prog;
1748
1749 tgt_prog = bpf_prog_get(attr->attach_prog_fd);
1750 if (IS_ERR(tgt_prog)) {
1751 err = PTR_ERR(tgt_prog);
1752 goto free_prog_nouncharge;
1753 }
1754 prog->aux->linked_prog = tgt_prog;
1755 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001756
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001757 prog->aux->offload_requested = !!attr->prog_ifindex;
1758
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001759 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001760 if (err)
1761 goto free_prog_nouncharge;
1762
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001763 err = bpf_prog_charge_memlock(prog);
1764 if (err)
1765 goto free_prog_sec;
1766
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001767 prog->len = attr->insn_cnt;
1768
1769 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001770 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001771 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001772 goto free_prog;
1773
1774 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001775 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001776
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001777 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001778 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001779
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001780 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001781 err = bpf_prog_offload_init(prog, attr);
1782 if (err)
1783 goto free_prog;
1784 }
1785
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001786 /* find program type: socket_filter vs tracing_filter */
1787 err = find_prog_type(type, prog);
1788 if (err < 0)
1789 goto free_prog;
1790
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02001791 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001792 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1793 if (err)
1794 goto free_prog;
1795
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001796 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08001797 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001798 if (err < 0)
1799 goto free_used_maps;
1800
Daniel Borkmann9facc332018-06-15 02:30:48 +02001801 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001802 if (err < 0)
1803 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001804
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001805 err = bpf_prog_alloc_id(prog);
1806 if (err)
1807 goto free_used_maps;
1808
Daniel Borkmannc7517982019-08-23 22:14:23 +02001809 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
1810 * effectively publicly exposed. However, retrieving via
1811 * bpf_prog_get_fd_by_id() will take another reference,
1812 * therefore it cannot be gone underneath us.
1813 *
1814 * Only for the time /after/ successful bpf_prog_new_fd()
1815 * and before returning to userspace, we might just hold
1816 * one reference and any parallel close on that fd could
1817 * rip everything out. Hence, below notifications must
1818 * happen before bpf_prog_new_fd().
1819 *
1820 * Also, any failure handling from this point onwards must
1821 * be using bpf_prog_put() given the program is exposed.
1822 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01001823 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08001824 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannc7517982019-08-23 22:14:23 +02001825
1826 err = bpf_prog_new_fd(prog);
1827 if (err < 0)
1828 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001829 return err;
1830
1831free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001832 /* In case we have subprogs, we need to wait for a grace
1833 * period before we can tear down JIT memory since symbols
1834 * are already exposed under kallsyms.
1835 */
1836 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
1837 return err;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001838free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001839 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001840free_prog_sec:
1841 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001842free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001843 bpf_prog_free(prog);
1844 return err;
1845}
1846
Chenbo Feng6e71b042017-10-18 13:00:22 -07001847#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001848
1849static int bpf_obj_pin(const union bpf_attr *attr)
1850{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001851 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001852 return -EINVAL;
1853
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001854 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001855}
1856
1857static int bpf_obj_get(const union bpf_attr *attr)
1858{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001859 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1860 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001861 return -EINVAL;
1862
Chenbo Feng6e71b042017-10-18 13:00:22 -07001863 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1864 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001865}
1866
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08001867static int bpf_tracing_prog_release(struct inode *inode, struct file *filp)
1868{
1869 struct bpf_prog *prog = filp->private_data;
1870
1871 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
1872 bpf_prog_put(prog);
1873 return 0;
1874}
1875
1876static const struct file_operations bpf_tracing_prog_fops = {
1877 .release = bpf_tracing_prog_release,
1878 .read = bpf_dummy_read,
1879 .write = bpf_dummy_write,
1880};
1881
1882static int bpf_tracing_prog_attach(struct bpf_prog *prog)
1883{
1884 int tr_fd, err;
1885
1886 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
1887 prog->expected_attach_type != BPF_TRACE_FEXIT) {
1888 err = -EINVAL;
1889 goto out_put_prog;
1890 }
1891
1892 err = bpf_trampoline_link_prog(prog);
1893 if (err)
1894 goto out_put_prog;
1895
1896 tr_fd = anon_inode_getfd("bpf-tracing-prog", &bpf_tracing_prog_fops,
1897 prog, O_CLOEXEC);
1898 if (tr_fd < 0) {
1899 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
1900 err = tr_fd;
1901 goto out_put_prog;
1902 }
1903 return tr_fd;
1904
1905out_put_prog:
1906 bpf_prog_put(prog);
1907 return err;
1908}
1909
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001910struct bpf_raw_tracepoint {
1911 struct bpf_raw_event_map *btp;
1912 struct bpf_prog *prog;
1913};
1914
1915static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1916{
1917 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1918
1919 if (raw_tp->prog) {
1920 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1921 bpf_prog_put(raw_tp->prog);
1922 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001923 bpf_put_raw_tracepoint(raw_tp->btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001924 kfree(raw_tp);
1925 return 0;
1926}
1927
1928static const struct file_operations bpf_raw_tp_fops = {
1929 .release = bpf_raw_tracepoint_release,
1930 .read = bpf_dummy_read,
1931 .write = bpf_dummy_write,
1932};
1933
1934#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1935
1936static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1937{
1938 struct bpf_raw_tracepoint *raw_tp;
1939 struct bpf_raw_event_map *btp;
1940 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07001941 const char *tp_name;
1942 char buf[128];
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001943 int tp_fd, err;
1944
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07001945 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
1946 return -EINVAL;
1947
1948 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1949 if (IS_ERR(prog))
1950 return PTR_ERR(prog);
1951
1952 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001953 prog->type != BPF_PROG_TYPE_TRACING &&
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07001954 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1955 err = -EINVAL;
1956 goto out_put_prog;
1957 }
1958
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07001959 if (prog->type == BPF_PROG_TYPE_TRACING) {
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07001960 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08001961 /* The attach point for this category of programs
1962 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07001963 */
1964 err = -EINVAL;
1965 goto out_put_prog;
1966 }
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08001967 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
1968 tp_name = prog->aux->attach_func_name;
1969 else
1970 return bpf_tracing_prog_attach(prog);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07001971 } else {
1972 if (strncpy_from_user(buf,
1973 u64_to_user_ptr(attr->raw_tracepoint.name),
1974 sizeof(buf) - 1) < 0) {
1975 err = -EFAULT;
1976 goto out_put_prog;
1977 }
1978 buf[sizeof(buf) - 1] = 0;
1979 tp_name = buf;
1980 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001981
Matt Mullinsa38d1102018-12-12 16:42:37 -08001982 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07001983 if (!btp) {
1984 err = -ENOENT;
1985 goto out_put_prog;
1986 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001987
1988 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001989 if (!raw_tp) {
1990 err = -ENOMEM;
1991 goto out_put_btp;
1992 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001993 raw_tp->btp = btp;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07001994 raw_tp->prog = prog;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001995
1996 err = bpf_probe_register(raw_tp->btp, prog);
1997 if (err)
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07001998 goto out_free_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001999
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002000 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
2001 O_CLOEXEC);
2002 if (tp_fd < 0) {
2003 bpf_probe_unregister(raw_tp->btp, prog);
2004 err = tp_fd;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002005 goto out_free_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002006 }
2007 return tp_fd;
2008
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002009out_free_tp:
2010 kfree(raw_tp);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002011out_put_btp:
2012 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002013out_put_prog:
2014 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002015 return err;
2016}
2017
Anders Roxell33491582018-04-03 14:09:47 +02002018static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2019 enum bpf_attach_type attach_type)
2020{
2021 switch (prog->type) {
2022 case BPF_PROG_TYPE_CGROUP_SOCK:
2023 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002024 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Anders Roxell33491582018-04-03 14:09:47 +02002025 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07002026 case BPF_PROG_TYPE_CGROUP_SKB:
2027 return prog->enforce_expected_attach_type &&
2028 prog->expected_attach_type != attach_type ?
2029 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02002030 default:
2031 return 0;
2032 }
2033}
2034
John Fastabend464bc0f2017-08-28 07:10:04 -07002035#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07002036
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002037#define BPF_F_ATTACH_MASK \
2038 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
2039
Daniel Mackf4324552016-11-23 16:52:27 +01002040static int bpf_prog_attach(const union bpf_attr *attr)
2041{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002042 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002043 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002044 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002045
2046 if (!capable(CAP_NET_ADMIN))
2047 return -EPERM;
2048
2049 if (CHECK_ATTR(BPF_PROG_ATTACH))
2050 return -EINVAL;
2051
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002052 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002053 return -EINVAL;
2054
Daniel Mackf4324552016-11-23 16:52:27 +01002055 switch (attr->attach_type) {
2056 case BPF_CGROUP_INET_INGRESS:
2057 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08002058 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01002059 break;
David Ahern610236582016-12-01 08:48:04 -08002060 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002061 case BPF_CGROUP_INET4_POST_BIND:
2062 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08002063 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2064 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002065 case BPF_CGROUP_INET4_BIND:
2066 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002067 case BPF_CGROUP_INET4_CONNECT:
2068 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002069 case BPF_CGROUP_UDP4_SENDMSG:
2070 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002071 case BPF_CGROUP_UDP4_RECVMSG:
2072 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002073 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2074 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002075 case BPF_CGROUP_SOCK_OPS:
2076 ptype = BPF_PROG_TYPE_SOCK_OPS;
2077 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05002078 case BPF_CGROUP_DEVICE:
2079 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2080 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07002081 case BPF_SK_MSG_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01002082 ptype = BPF_PROG_TYPE_SK_MSG;
2083 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07002084 case BPF_SK_SKB_STREAM_PARSER:
2085 case BPF_SK_SKB_STREAM_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01002086 ptype = BPF_PROG_TYPE_SK_SKB;
2087 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01002088 case BPF_LIRC_MODE2:
Sean Youngfdb5c452018-06-19 00:04:24 +01002089 ptype = BPF_PROG_TYPE_LIRC_MODE2;
2090 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07002091 case BPF_FLOW_DISSECTOR:
2092 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
2093 break;
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002094 case BPF_CGROUP_SYSCTL:
2095 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2096 break;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002097 case BPF_CGROUP_GETSOCKOPT:
2098 case BPF_CGROUP_SETSOCKOPT:
2099 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2100 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002101 default:
2102 return -EINVAL;
2103 }
2104
David Ahernb2cd1252016-12-01 08:48:03 -08002105 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2106 if (IS_ERR(prog))
2107 return PTR_ERR(prog);
2108
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002109 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2110 bpf_prog_put(prog);
2111 return -EINVAL;
2112 }
2113
Sean Youngfdb5c452018-06-19 00:04:24 +01002114 switch (ptype) {
2115 case BPF_PROG_TYPE_SK_SKB:
2116 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002117 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01002118 break;
2119 case BPF_PROG_TYPE_LIRC_MODE2:
2120 ret = lirc_prog_attach(attr, prog);
2121 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07002122 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2123 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
2124 break;
Sean Youngfdb5c452018-06-19 00:04:24 +01002125 default:
2126 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
David Ahernb2cd1252016-12-01 08:48:03 -08002127 }
2128
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002129 if (ret)
2130 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002131 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002132}
2133
2134#define BPF_PROG_DETACH_LAST_FIELD attach_type
2135
2136static int bpf_prog_detach(const union bpf_attr *attr)
2137{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002138 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002139
2140 if (!capable(CAP_NET_ADMIN))
2141 return -EPERM;
2142
2143 if (CHECK_ATTR(BPF_PROG_DETACH))
2144 return -EINVAL;
2145
2146 switch (attr->attach_type) {
2147 case BPF_CGROUP_INET_INGRESS:
2148 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002149 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2150 break;
David Ahern610236582016-12-01 08:48:04 -08002151 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002152 case BPF_CGROUP_INET4_POST_BIND:
2153 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002154 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2155 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002156 case BPF_CGROUP_INET4_BIND:
2157 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002158 case BPF_CGROUP_INET4_CONNECT:
2159 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002160 case BPF_CGROUP_UDP4_SENDMSG:
2161 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002162 case BPF_CGROUP_UDP4_RECVMSG:
2163 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002164 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2165 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002166 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002167 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01002168 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05002169 case BPF_CGROUP_DEVICE:
2170 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2171 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07002172 case BPF_SK_MSG_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002173 return sock_map_get_from_fd(attr, NULL);
John Fastabend5a67da22017-09-08 14:00:49 -07002174 case BPF_SK_SKB_STREAM_PARSER:
2175 case BPF_SK_SKB_STREAM_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002176 return sock_map_get_from_fd(attr, NULL);
Sean Youngf4364dc2018-05-27 12:24:09 +01002177 case BPF_LIRC_MODE2:
2178 return lirc_prog_detach(attr);
Petar Penkovd58e4682018-09-14 07:46:18 -07002179 case BPF_FLOW_DISSECTOR:
2180 return skb_flow_dissector_bpf_prog_detach(attr);
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002181 case BPF_CGROUP_SYSCTL:
2182 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2183 break;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002184 case BPF_CGROUP_GETSOCKOPT:
2185 case BPF_CGROUP_SETSOCKOPT:
2186 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2187 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002188 default:
2189 return -EINVAL;
2190 }
2191
Sean Youngfdb5c452018-06-19 00:04:24 +01002192 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01002193}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002194
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002195#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2196
2197static int bpf_prog_query(const union bpf_attr *attr,
2198 union bpf_attr __user *uattr)
2199{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002200 if (!capable(CAP_NET_ADMIN))
2201 return -EPERM;
2202 if (CHECK_ATTR(BPF_PROG_QUERY))
2203 return -EINVAL;
2204 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2205 return -EINVAL;
2206
2207 switch (attr->query.attach_type) {
2208 case BPF_CGROUP_INET_INGRESS:
2209 case BPF_CGROUP_INET_EGRESS:
2210 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002211 case BPF_CGROUP_INET4_BIND:
2212 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002213 case BPF_CGROUP_INET4_POST_BIND:
2214 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002215 case BPF_CGROUP_INET4_CONNECT:
2216 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002217 case BPF_CGROUP_UDP4_SENDMSG:
2218 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002219 case BPF_CGROUP_UDP4_RECVMSG:
2220 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002221 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05002222 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002223 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002224 case BPF_CGROUP_GETSOCKOPT:
2225 case BPF_CGROUP_SETSOCKOPT:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002226 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01002227 case BPF_LIRC_MODE2:
2228 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07002229 case BPF_FLOW_DISSECTOR:
2230 return skb_flow_dissector_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002231 default:
2232 return -EINVAL;
2233 }
Sean Youngfdb5c452018-06-19 00:04:24 +01002234
2235 return cgroup_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002236}
Daniel Mackf4324552016-11-23 16:52:27 +01002237
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002238#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002239
2240static int bpf_prog_test_run(const union bpf_attr *attr,
2241 union bpf_attr __user *uattr)
2242{
2243 struct bpf_prog *prog;
2244 int ret = -ENOTSUPP;
2245
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08002246 if (!capable(CAP_SYS_ADMIN))
2247 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002248 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2249 return -EINVAL;
2250
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002251 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2252 (!attr->test.ctx_size_in && attr->test.ctx_in))
2253 return -EINVAL;
2254
2255 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2256 (!attr->test.ctx_size_out && attr->test.ctx_out))
2257 return -EINVAL;
2258
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002259 prog = bpf_prog_get(attr->test.prog_fd);
2260 if (IS_ERR(prog))
2261 return PTR_ERR(prog);
2262
2263 if (prog->aux->ops->test_run)
2264 ret = prog->aux->ops->test_run(prog, attr, uattr);
2265
2266 bpf_prog_put(prog);
2267 return ret;
2268}
2269
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002270#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2271
2272static int bpf_obj_get_next_id(const union bpf_attr *attr,
2273 union bpf_attr __user *uattr,
2274 struct idr *idr,
2275 spinlock_t *lock)
2276{
2277 u32 next_id = attr->start_id;
2278 int err = 0;
2279
2280 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2281 return -EINVAL;
2282
2283 if (!capable(CAP_SYS_ADMIN))
2284 return -EPERM;
2285
2286 next_id++;
2287 spin_lock_bh(lock);
2288 if (!idr_get_next(idr, &next_id))
2289 err = -ENOENT;
2290 spin_unlock_bh(lock);
2291
2292 if (!err)
2293 err = put_user(next_id, &uattr->next_id);
2294
2295 return err;
2296}
2297
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002298#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2299
2300static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2301{
2302 struct bpf_prog *prog;
2303 u32 id = attr->prog_id;
2304 int fd;
2305
2306 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2307 return -EINVAL;
2308
2309 if (!capable(CAP_SYS_ADMIN))
2310 return -EPERM;
2311
2312 spin_lock_bh(&prog_idr_lock);
2313 prog = idr_find(&prog_idr, id);
2314 if (prog)
2315 prog = bpf_prog_inc_not_zero(prog);
2316 else
2317 prog = ERR_PTR(-ENOENT);
2318 spin_unlock_bh(&prog_idr_lock);
2319
2320 if (IS_ERR(prog))
2321 return PTR_ERR(prog);
2322
2323 fd = bpf_prog_new_fd(prog);
2324 if (fd < 0)
2325 bpf_prog_put(prog);
2326
2327 return fd;
2328}
2329
Chenbo Feng6e71b042017-10-18 13:00:22 -07002330#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002331
2332static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2333{
2334 struct bpf_map *map;
2335 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002336 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002337 int fd;
2338
Chenbo Feng6e71b042017-10-18 13:00:22 -07002339 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2340 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002341 return -EINVAL;
2342
2343 if (!capable(CAP_SYS_ADMIN))
2344 return -EPERM;
2345
Chenbo Feng6e71b042017-10-18 13:00:22 -07002346 f_flags = bpf_get_file_flag(attr->open_flags);
2347 if (f_flags < 0)
2348 return f_flags;
2349
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002350 spin_lock_bh(&map_idr_lock);
2351 map = idr_find(&map_idr, id);
2352 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07002353 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002354 else
2355 map = ERR_PTR(-ENOENT);
2356 spin_unlock_bh(&map_idr_lock);
2357
2358 if (IS_ERR(map))
2359 return PTR_ERR(map);
2360
Chenbo Feng6e71b042017-10-18 13:00:22 -07002361 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002362 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002363 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002364
2365 return fd;
2366}
2367
Daniel Borkmann7105e822017-12-20 13:42:57 +01002368static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002369 unsigned long addr, u32 *off,
2370 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002371{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002372 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002373 int i;
2374
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002375 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2376 map = prog->aux->used_maps[i];
2377 if (map == (void *)addr) {
2378 *type = BPF_PSEUDO_MAP_FD;
2379 return map;
2380 }
2381 if (!map->ops->map_direct_value_meta)
2382 continue;
2383 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2384 *type = BPF_PSEUDO_MAP_VALUE;
2385 return map;
2386 }
2387 }
2388
Daniel Borkmann7105e822017-12-20 13:42:57 +01002389 return NULL;
2390}
2391
2392static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2393{
2394 const struct bpf_map *map;
2395 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002396 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002397 u64 imm;
2398 int i;
2399
2400 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2401 GFP_USER);
2402 if (!insns)
2403 return insns;
2404
2405 for (i = 0; i < prog->len; i++) {
2406 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2407 insns[i].code = BPF_JMP | BPF_CALL;
2408 insns[i].imm = BPF_FUNC_tail_call;
2409 /* fall-through */
2410 }
2411 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2412 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2413 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2414 insns[i].code = BPF_JMP | BPF_CALL;
2415 if (!bpf_dump_raw_ok())
2416 insns[i].imm = 0;
2417 continue;
2418 }
2419
2420 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2421 continue;
2422
2423 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002424 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002425 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002426 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002427 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002428 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002429 continue;
2430 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002431 }
2432
2433 return insns;
2434}
2435
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002436static int set_info_rec_size(struct bpf_prog_info *info)
2437{
2438 /*
2439 * Ensure info.*_rec_size is the same as kernel expected size
2440 *
2441 * or
2442 *
2443 * Only allow zero *_rec_size if both _rec_size and _cnt are
2444 * zero. In this case, the kernel will set the expected
2445 * _rec_size back to the info.
2446 */
2447
Yonghong Song11d8b822018-12-10 14:14:08 -08002448 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002449 info->func_info_rec_size != sizeof(struct bpf_func_info))
2450 return -EINVAL;
2451
Yonghong Song11d8b822018-12-10 14:14:08 -08002452 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002453 info->line_info_rec_size != sizeof(struct bpf_line_info))
2454 return -EINVAL;
2455
Yonghong Song11d8b822018-12-10 14:14:08 -08002456 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002457 info->jited_line_info_rec_size != sizeof(__u64))
2458 return -EINVAL;
2459
2460 info->func_info_rec_size = sizeof(struct bpf_func_info);
2461 info->line_info_rec_size = sizeof(struct bpf_line_info);
2462 info->jited_line_info_rec_size = sizeof(__u64);
2463
2464 return 0;
2465}
2466
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002467static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2468 const union bpf_attr *attr,
2469 union bpf_attr __user *uattr)
2470{
2471 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2472 struct bpf_prog_info info = {};
2473 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002474 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002475 char __user *uinsns;
2476 u32 ulen;
2477 int err;
2478
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002479 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002480 if (err)
2481 return err;
2482 info_len = min_t(u32, sizeof(info), info_len);
2483
2484 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02002485 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002486
2487 info.type = prog->type;
2488 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002489 info.load_time = prog->aux->load_time;
2490 info.created_by_uid = from_kuid_munged(current_user_ns(),
2491 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02002492 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002493
2494 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002495 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2496
2497 ulen = info.nr_map_ids;
2498 info.nr_map_ids = prog->aux->used_map_cnt;
2499 ulen = min_t(u32, info.nr_map_ids, ulen);
2500 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07002501 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002502 u32 i;
2503
2504 for (i = 0; i < ulen; i++)
2505 if (put_user(prog->aux->used_maps[i]->id,
2506 &user_map_ids[i]))
2507 return -EFAULT;
2508 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002509
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002510 err = set_info_rec_size(&info);
2511 if (err)
2512 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08002513
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002514 bpf_prog_get_stats(prog, &stats);
2515 info.run_time_ns = stats.nsecs;
2516 info.run_cnt = stats.cnt;
2517
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002518 if (!capable(CAP_SYS_ADMIN)) {
2519 info.jited_prog_len = 0;
2520 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302521 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01002522 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08002523 info.nr_func_info = 0;
2524 info.nr_line_info = 0;
2525 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002526 goto done;
2527 }
2528
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002529 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02002530 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002531 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01002532 struct bpf_insn *insns_sanitized;
2533 bool fault;
2534
2535 if (prog->blinded && !bpf_dump_raw_ok()) {
2536 info.xlated_prog_insns = 0;
2537 goto done;
2538 }
2539 insns_sanitized = bpf_insn_prepare_dump(prog);
2540 if (!insns_sanitized)
2541 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002542 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2543 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002544 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2545 kfree(insns_sanitized);
2546 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002547 return -EFAULT;
2548 }
2549
Jakub Kicinski675fc272017-12-27 18:39:09 -08002550 if (bpf_prog_is_dev_bound(prog->aux)) {
2551 err = bpf_prog_offload_info_fill(&info, prog);
2552 if (err)
2553 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08002554 goto done;
2555 }
2556
2557 /* NOTE: the following code is supposed to be skipped for offload.
2558 * bpf_prog_offload_info_fill() is the place to fill similar fields
2559 * for offload.
2560 */
2561 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05302562 if (prog->aux->func_cnt) {
2563 u32 i;
2564
2565 info.jited_prog_len = 0;
2566 for (i = 0; i < prog->aux->func_cnt; i++)
2567 info.jited_prog_len += prog->aux->func[i]->jited_len;
2568 } else {
2569 info.jited_prog_len = prog->jited_len;
2570 }
2571
Jiong Wangfcfb1262018-01-16 16:05:19 -08002572 if (info.jited_prog_len && ulen) {
2573 if (bpf_dump_raw_ok()) {
2574 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2575 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05302576
2577 /* for multi-function programs, copy the JITed
2578 * instructions for all the functions
2579 */
2580 if (prog->aux->func_cnt) {
2581 u32 len, free, i;
2582 u8 *img;
2583
2584 free = ulen;
2585 for (i = 0; i < prog->aux->func_cnt; i++) {
2586 len = prog->aux->func[i]->jited_len;
2587 len = min_t(u32, len, free);
2588 img = (u8 *) prog->aux->func[i]->bpf_func;
2589 if (copy_to_user(uinsns, img, len))
2590 return -EFAULT;
2591 uinsns += len;
2592 free -= len;
2593 if (!free)
2594 break;
2595 }
2596 } else {
2597 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2598 return -EFAULT;
2599 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002600 } else {
2601 info.jited_prog_insns = 0;
2602 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002603 }
2604
Sandipan Dasdbecd732018-05-24 12:26:48 +05302605 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07002606 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002607 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05302608 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07002609 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302610 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302611 u32 i;
2612
2613 /* copy the address of the kernel symbol
2614 * corresponding to each function
2615 */
2616 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2617 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07002618 if (prog->aux->func_cnt) {
2619 for (i = 0; i < ulen; i++) {
2620 ksym_addr = (unsigned long)
2621 prog->aux->func[i]->bpf_func;
2622 if (put_user((u64) ksym_addr,
2623 &user_ksyms[i]))
2624 return -EFAULT;
2625 }
2626 } else {
2627 ksym_addr = (unsigned long) prog->bpf_func;
2628 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05302629 return -EFAULT;
2630 }
2631 } else {
2632 info.jited_ksyms = 0;
2633 }
2634 }
2635
Sandipan Das815581c2018-05-24 12:26:52 +05302636 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07002637 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002638 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05302639 if (bpf_dump_raw_ok()) {
2640 u32 __user *user_lens;
2641 u32 func_len, i;
2642
2643 /* copy the JITed image lengths for each function */
2644 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2645 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07002646 if (prog->aux->func_cnt) {
2647 for (i = 0; i < ulen; i++) {
2648 func_len =
2649 prog->aux->func[i]->jited_len;
2650 if (put_user(func_len, &user_lens[i]))
2651 return -EFAULT;
2652 }
2653 } else {
2654 func_len = prog->jited_len;
2655 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05302656 return -EFAULT;
2657 }
2658 } else {
2659 info.jited_func_lens = 0;
2660 }
2661 }
2662
Martin KaFai Lau73372242018-12-05 17:35:43 -08002663 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08002664 info.btf_id = btf_id(prog->aux->btf);
2665
Yonghong Song11d8b822018-12-10 14:14:08 -08002666 ulen = info.nr_func_info;
2667 info.nr_func_info = prog->aux->func_info_cnt;
2668 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002669 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08002670
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002671 user_finfo = u64_to_user_ptr(info.func_info);
2672 ulen = min_t(u32, info.nr_func_info, ulen);
2673 if (copy_to_user(user_finfo, prog->aux->func_info,
2674 info.func_info_rec_size * ulen))
2675 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08002676 }
2677
Yonghong Song11d8b822018-12-10 14:14:08 -08002678 ulen = info.nr_line_info;
2679 info.nr_line_info = prog->aux->nr_linfo;
2680 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002681 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002682
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002683 user_linfo = u64_to_user_ptr(info.line_info);
2684 ulen = min_t(u32, info.nr_line_info, ulen);
2685 if (copy_to_user(user_linfo, prog->aux->linfo,
2686 info.line_info_rec_size * ulen))
2687 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002688 }
2689
Yonghong Song11d8b822018-12-10 14:14:08 -08002690 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002691 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08002692 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002693 else
Yonghong Song11d8b822018-12-10 14:14:08 -08002694 info.nr_jited_line_info = 0;
2695 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002696 if (bpf_dump_raw_ok()) {
2697 __u64 __user *user_linfo;
2698 u32 i;
2699
2700 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08002701 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002702 for (i = 0; i < ulen; i++) {
2703 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2704 &user_linfo[i]))
2705 return -EFAULT;
2706 }
2707 } else {
2708 info.jited_line_info = 0;
2709 }
2710 }
2711
Song Liuc872bdb2018-12-12 09:37:46 -08002712 ulen = info.nr_prog_tags;
2713 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2714 if (ulen) {
2715 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2716 u32 i;
2717
2718 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2719 ulen = min_t(u32, info.nr_prog_tags, ulen);
2720 if (prog->aux->func_cnt) {
2721 for (i = 0; i < ulen; i++) {
2722 if (copy_to_user(user_prog_tags[i],
2723 prog->aux->func[i]->tag,
2724 BPF_TAG_SIZE))
2725 return -EFAULT;
2726 }
2727 } else {
2728 if (copy_to_user(user_prog_tags[0],
2729 prog->tag, BPF_TAG_SIZE))
2730 return -EFAULT;
2731 }
2732 }
2733
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002734done:
2735 if (copy_to_user(uinfo, &info, info_len) ||
2736 put_user(info_len, &uattr->info.info_len))
2737 return -EFAULT;
2738
2739 return 0;
2740}
2741
2742static int bpf_map_get_info_by_fd(struct bpf_map *map,
2743 const union bpf_attr *attr,
2744 union bpf_attr __user *uattr)
2745{
2746 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2747 struct bpf_map_info info = {};
2748 u32 info_len = attr->info.info_len;
2749 int err;
2750
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002751 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002752 if (err)
2753 return err;
2754 info_len = min_t(u32, sizeof(info), info_len);
2755
2756 info.type = map->map_type;
2757 info.id = map->id;
2758 info.key_size = map->key_size;
2759 info.value_size = map->value_size;
2760 info.max_entries = map->max_entries;
2761 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002762 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002763
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002764 if (map->btf) {
2765 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002766 info.btf_key_type_id = map->btf_key_type_id;
2767 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002768 }
2769
Jakub Kicinski52775b32018-01-17 19:13:28 -08002770 if (bpf_map_is_dev_bound(map)) {
2771 err = bpf_map_offload_info_fill(&info, map);
2772 if (err)
2773 return err;
2774 }
2775
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002776 if (copy_to_user(uinfo, &info, info_len) ||
2777 put_user(info_len, &uattr->info.info_len))
2778 return -EFAULT;
2779
2780 return 0;
2781}
2782
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002783static int bpf_btf_get_info_by_fd(struct btf *btf,
2784 const union bpf_attr *attr,
2785 union bpf_attr __user *uattr)
2786{
2787 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2788 u32 info_len = attr->info.info_len;
2789 int err;
2790
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002791 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002792 if (err)
2793 return err;
2794
2795 return btf_get_info_by_fd(btf, attr, uattr);
2796}
2797
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002798#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2799
2800static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2801 union bpf_attr __user *uattr)
2802{
2803 int ufd = attr->info.bpf_fd;
2804 struct fd f;
2805 int err;
2806
2807 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2808 return -EINVAL;
2809
2810 f = fdget(ufd);
2811 if (!f.file)
2812 return -EBADFD;
2813
2814 if (f.file->f_op == &bpf_prog_fops)
2815 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2816 uattr);
2817 else if (f.file->f_op == &bpf_map_fops)
2818 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2819 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002820 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002821 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002822 else
2823 err = -EINVAL;
2824
2825 fdput(f);
2826 return err;
2827}
2828
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002829#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2830
2831static int bpf_btf_load(const union bpf_attr *attr)
2832{
2833 if (CHECK_ATTR(BPF_BTF_LOAD))
2834 return -EINVAL;
2835
2836 if (!capable(CAP_SYS_ADMIN))
2837 return -EPERM;
2838
2839 return btf_new_fd(attr);
2840}
2841
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002842#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2843
2844static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2845{
2846 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2847 return -EINVAL;
2848
2849 if (!capable(CAP_SYS_ADMIN))
2850 return -EPERM;
2851
2852 return btf_get_fd_by_id(attr->btf_id);
2853}
2854
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002855static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2856 union bpf_attr __user *uattr,
2857 u32 prog_id, u32 fd_type,
2858 const char *buf, u64 probe_offset,
2859 u64 probe_addr)
2860{
2861 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2862 u32 len = buf ? strlen(buf) : 0, input_len;
2863 int err = 0;
2864
2865 if (put_user(len, &uattr->task_fd_query.buf_len))
2866 return -EFAULT;
2867 input_len = attr->task_fd_query.buf_len;
2868 if (input_len && ubuf) {
2869 if (!len) {
2870 /* nothing to copy, just make ubuf NULL terminated */
2871 char zero = '\0';
2872
2873 if (put_user(zero, ubuf))
2874 return -EFAULT;
2875 } else if (input_len >= len + 1) {
2876 /* ubuf can hold the string with NULL terminator */
2877 if (copy_to_user(ubuf, buf, len + 1))
2878 return -EFAULT;
2879 } else {
2880 /* ubuf cannot hold the string with NULL terminator,
2881 * do a partial copy with NULL terminator.
2882 */
2883 char zero = '\0';
2884
2885 err = -ENOSPC;
2886 if (copy_to_user(ubuf, buf, input_len - 1))
2887 return -EFAULT;
2888 if (put_user(zero, ubuf + input_len - 1))
2889 return -EFAULT;
2890 }
2891 }
2892
2893 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2894 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2895 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2896 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2897 return -EFAULT;
2898
2899 return err;
2900}
2901
2902#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2903
2904static int bpf_task_fd_query(const union bpf_attr *attr,
2905 union bpf_attr __user *uattr)
2906{
2907 pid_t pid = attr->task_fd_query.pid;
2908 u32 fd = attr->task_fd_query.fd;
2909 const struct perf_event *event;
2910 struct files_struct *files;
2911 struct task_struct *task;
2912 struct file *file;
2913 int err;
2914
2915 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2916 return -EINVAL;
2917
2918 if (!capable(CAP_SYS_ADMIN))
2919 return -EPERM;
2920
2921 if (attr->task_fd_query.flags != 0)
2922 return -EINVAL;
2923
2924 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2925 if (!task)
2926 return -ENOENT;
2927
2928 files = get_files_struct(task);
2929 put_task_struct(task);
2930 if (!files)
2931 return -ENOENT;
2932
2933 err = 0;
2934 spin_lock(&files->file_lock);
2935 file = fcheck_files(files, fd);
2936 if (!file)
2937 err = -EBADF;
2938 else
2939 get_file(file);
2940 spin_unlock(&files->file_lock);
2941 put_files_struct(files);
2942
2943 if (err)
2944 goto out;
2945
2946 if (file->f_op == &bpf_raw_tp_fops) {
2947 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2948 struct bpf_raw_event_map *btp = raw_tp->btp;
2949
2950 err = bpf_task_fd_query_copy(attr, uattr,
2951 raw_tp->prog->aux->id,
2952 BPF_FD_TYPE_RAW_TRACEPOINT,
2953 btp->tp->name, 0, 0);
2954 goto put_file;
2955 }
2956
2957 event = perf_get_event(file);
2958 if (!IS_ERR(event)) {
2959 u64 probe_offset, probe_addr;
2960 u32 prog_id, fd_type;
2961 const char *buf;
2962
2963 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2964 &buf, &probe_offset,
2965 &probe_addr);
2966 if (!err)
2967 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2968 fd_type, buf,
2969 probe_offset,
2970 probe_addr);
2971 goto put_file;
2972 }
2973
2974 err = -ENOTSUPP;
2975put_file:
2976 fput(file);
2977out:
2978 return err;
2979}
2980
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002981SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2982{
2983 union bpf_attr attr = {};
2984 int err;
2985
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002986 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002987 return -EPERM;
2988
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002989 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002990 if (err)
2991 return err;
2992 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002993
2994 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2995 if (copy_from_user(&attr, uattr, size) != 0)
2996 return -EFAULT;
2997
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002998 err = security_bpf(cmd, &attr, size);
2999 if (err < 0)
3000 return err;
3001
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003002 switch (cmd) {
3003 case BPF_MAP_CREATE:
3004 err = map_create(&attr);
3005 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07003006 case BPF_MAP_LOOKUP_ELEM:
3007 err = map_lookup_elem(&attr);
3008 break;
3009 case BPF_MAP_UPDATE_ELEM:
3010 err = map_update_elem(&attr);
3011 break;
3012 case BPF_MAP_DELETE_ELEM:
3013 err = map_delete_elem(&attr);
3014 break;
3015 case BPF_MAP_GET_NEXT_KEY:
3016 err = map_get_next_key(&attr);
3017 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02003018 case BPF_MAP_FREEZE:
3019 err = map_freeze(&attr);
3020 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07003021 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08003022 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07003023 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01003024 case BPF_OBJ_PIN:
3025 err = bpf_obj_pin(&attr);
3026 break;
3027 case BPF_OBJ_GET:
3028 err = bpf_obj_get(&attr);
3029 break;
Daniel Mackf4324552016-11-23 16:52:27 +01003030 case BPF_PROG_ATTACH:
3031 err = bpf_prog_attach(&attr);
3032 break;
3033 case BPF_PROG_DETACH:
3034 err = bpf_prog_detach(&attr);
3035 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003036 case BPF_PROG_QUERY:
3037 err = bpf_prog_query(&attr, uattr);
3038 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003039 case BPF_PROG_TEST_RUN:
3040 err = bpf_prog_test_run(&attr, uattr);
3041 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003042 case BPF_PROG_GET_NEXT_ID:
3043 err = bpf_obj_get_next_id(&attr, uattr,
3044 &prog_idr, &prog_idr_lock);
3045 break;
3046 case BPF_MAP_GET_NEXT_ID:
3047 err = bpf_obj_get_next_id(&attr, uattr,
3048 &map_idr, &map_idr_lock);
3049 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01003050 case BPF_BTF_GET_NEXT_ID:
3051 err = bpf_obj_get_next_id(&attr, uattr,
3052 &btf_idr, &btf_idr_lock);
3053 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003054 case BPF_PROG_GET_FD_BY_ID:
3055 err = bpf_prog_get_fd_by_id(&attr);
3056 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003057 case BPF_MAP_GET_FD_BY_ID:
3058 err = bpf_map_get_fd_by_id(&attr);
3059 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003060 case BPF_OBJ_GET_INFO_BY_FD:
3061 err = bpf_obj_get_info_by_fd(&attr, uattr);
3062 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003063 case BPF_RAW_TRACEPOINT_OPEN:
3064 err = bpf_raw_tracepoint_open(&attr);
3065 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003066 case BPF_BTF_LOAD:
3067 err = bpf_btf_load(&attr);
3068 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003069 case BPF_BTF_GET_FD_BY_ID:
3070 err = bpf_btf_get_fd_by_id(&attr);
3071 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003072 case BPF_TASK_FD_QUERY:
3073 err = bpf_task_fd_query(&attr, uattr);
3074 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02003075 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
3076 err = map_lookup_and_delete_elem(&attr);
3077 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003078 default:
3079 err = -EINVAL;
3080 break;
3081 }
3082
3083 return err;
3084}