blob: ef63d26622f2f58561c09c0760310da40ce11671 [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 Starovoitov99c55f72014-09-26 00:16:57 -070026
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070027#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
33
Chenbo Feng6e71b042017-10-18 13:00:22 -070034#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
35
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080036DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070037static DEFINE_IDR(prog_idr);
38static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070039static DEFINE_IDR(map_idr);
40static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080041
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070042int sysctl_unprivileged_bpf_disabled __read_mostly;
43
Johannes Berg40077e02017-04-11 15:34:58 +020044static const struct bpf_map_ops * const bpf_map_types[] = {
45#define BPF_PROG_TYPE(_id, _ops)
46#define BPF_MAP_TYPE(_id, _ops) \
47 [_id] = &_ops,
48#include <linux/bpf_types.h>
49#undef BPF_PROG_TYPE
50#undef BPF_MAP_TYPE
51};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070052
Mickaël Salaün752ba562017-08-07 20:45:20 +020053/*
54 * If we're handed a bigger struct than we know of, ensure all the unknown bits
55 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
56 * we don't know about yet.
57 *
58 * There is a ToCToU between this function call and the following
59 * copy_from_user() call. However, this is not a concern since this function is
60 * meant to be a future-proofing of bits.
61 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070062int bpf_check_uarg_tail_zero(void __user *uaddr,
63 size_t expected_size,
64 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020065{
66 unsigned char __user *addr;
67 unsigned char __user *end;
68 unsigned char val;
69 int err;
70
Mickaël Salaün752ba562017-08-07 20:45:20 +020071 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
72 return -E2BIG;
73
Linus Torvalds96d4f262019-01-03 18:57:57 -080074 if (unlikely(!access_ok(uaddr, actual_size)))
Mickaël Salaün752ba562017-08-07 20:45:20 +020075 return -EFAULT;
76
Mickaël Salaün58291a72017-08-07 20:45:19 +020077 if (actual_size <= expected_size)
78 return 0;
79
80 addr = uaddr + expected_size;
81 end = uaddr + actual_size;
82
83 for (; addr < end; addr++) {
84 err = get_user(val, addr);
85 if (err)
86 return err;
87 if (val)
88 return -E2BIG;
89 }
90
91 return 0;
92}
93
Jakub Kicinskia3884572018-01-11 20:29:09 -080094const struct bpf_map_ops bpf_map_offload_ops = {
95 .map_alloc = bpf_map_offload_map_alloc,
96 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020097 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -080098};
99
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700100static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
101{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800102 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100103 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700104 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800105 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700106
Mark Rutland9ef09e32018-05-03 17:04:59 +0100107 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800108 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100109 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
110 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800111 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200112 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700113
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800114 if (ops->map_alloc_check) {
115 err = ops->map_alloc_check(attr);
116 if (err)
117 return ERR_PTR(err);
118 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800119 if (attr->map_ifindex)
120 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800121 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200122 if (IS_ERR(map))
123 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800124 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100125 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200126 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700127}
128
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700129void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100130{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100131 /* We really just want to fail instead of triggering OOM killer
132 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
133 * which is used for lower order allocation requests.
134 *
135 * It has been observed that higher order allocation requests done by
136 * vmalloc with __GFP_NORETRY being set might fail due to not trying
137 * to reclaim memory from the page cache, thus we set
138 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100139 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100140
141 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100142 void *area;
143
144 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100145 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
146 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100147 if (area != NULL)
148 return area;
149 }
150
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100151 return __vmalloc_node_flags_caller(size, numa_node,
152 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
153 flags, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100154}
155
156void bpf_map_area_free(void *area)
157{
158 kvfree(area);
159}
160
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200161static u32 bpf_map_flags_retain_permanent(u32 flags)
162{
163 /* Some map creation flags are not tied to the map object but
164 * rather to the map fd instead, so they have no meaning upon
165 * map object inspection since multiple file descriptors with
166 * different (access) properties can exist here. Thus, given
167 * this has zero meaning for the map itself, lets clear these
168 * from here.
169 */
170 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
171}
172
Jakub Kicinskibd475642018-01-11 20:29:06 -0800173void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
174{
175 map->map_type = attr->map_type;
176 map->key_size = attr->key_size;
177 map->value_size = attr->value_size;
178 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200179 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800180 map->numa_node = bpf_map_attr_numa_node(attr);
181}
182
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800183int bpf_map_precharge_memlock(u32 pages)
184{
185 struct user_struct *user = get_current_user();
186 unsigned long memlock_limit, cur;
187
188 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
189 cur = atomic_long_read(&user->locked_vm);
190 free_uid(user);
191 if (cur + pages > memlock_limit)
192 return -EPERM;
193 return 0;
194}
195
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700196static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700197{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700198 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700199
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700200 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
201 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700202 return -EPERM;
203 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700204 return 0;
205}
206
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700207static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
208{
209 atomic_long_sub(pages, &user->locked_vm);
210}
211
212static int bpf_map_init_memlock(struct bpf_map *map)
213{
214 struct user_struct *user = get_current_user();
215 int ret;
216
217 ret = bpf_charge_memlock(user, map->pages);
218 if (ret) {
219 free_uid(user);
220 return ret;
221 }
222 map->user = user;
223 return ret;
224}
225
226static void bpf_map_release_memlock(struct bpf_map *map)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700227{
228 struct user_struct *user = map->user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700229 bpf_uncharge_memlock(user, map->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700230 free_uid(user);
231}
232
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700233int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
234{
235 int ret;
236
237 ret = bpf_charge_memlock(map->user, pages);
238 if (ret)
239 return ret;
240 map->pages += pages;
241 return ret;
242}
243
244void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
245{
246 bpf_uncharge_memlock(map->user, pages);
247 map->pages -= pages;
248}
249
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700250static int bpf_map_alloc_id(struct bpf_map *map)
251{
252 int id;
253
Shaohua Lib76354c2018-03-27 11:53:21 -0700254 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700255 spin_lock_bh(&map_idr_lock);
256 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
257 if (id > 0)
258 map->id = id;
259 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700260 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700261
262 if (WARN_ON_ONCE(!id))
263 return -ENOSPC;
264
265 return id > 0 ? 0 : id;
266}
267
Jakub Kicinskia3884572018-01-11 20:29:09 -0800268void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700269{
Eric Dumazet930651a2017-09-19 09:15:59 -0700270 unsigned long flags;
271
Jakub Kicinskia3884572018-01-11 20:29:09 -0800272 /* Offloaded maps are removed from the IDR store when their device
273 * disappears - even if someone holds an fd to them they are unusable,
274 * the memory is gone, all ops will fail; they are simply waiting for
275 * refcnt to drop to be freed.
276 */
277 if (!map->id)
278 return;
279
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700280 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700281 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700282 else
283 __acquire(&map_idr_lock);
284
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700285 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800286 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700287
288 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700289 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700290 else
291 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700292}
293
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700294/* called from workqueue */
295static void bpf_map_free_deferred(struct work_struct *work)
296{
297 struct bpf_map *map = container_of(work, struct bpf_map, work);
298
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700299 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700300 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700301 /* implementation dependent freeing */
302 map->ops->map_free(map);
303}
304
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100305static void bpf_map_put_uref(struct bpf_map *map)
306{
307 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700308 if (map->ops->map_release_uref)
309 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100310 }
311}
312
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700313/* decrement map refcnt and schedule it for freeing via workqueue
314 * (unrelying map implementation ops->map_free() might sleep)
315 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700316static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700317{
318 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700319 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700320 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700321 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700322 INIT_WORK(&map->work, bpf_map_free_deferred);
323 schedule_work(&map->work);
324 }
325}
326
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700327void bpf_map_put(struct bpf_map *map)
328{
329 __bpf_map_put(map, true);
330}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700331EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700332
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100333void bpf_map_put_with_uref(struct bpf_map *map)
334{
335 bpf_map_put_uref(map);
336 bpf_map_put(map);
337}
338
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700339static int bpf_map_release(struct inode *inode, struct file *filp)
340{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200341 struct bpf_map *map = filp->private_data;
342
343 if (map->ops->map_release)
344 map->ops->map_release(map, filp);
345
346 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700347 return 0;
348}
349
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200350static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
351{
352 fmode_t mode = f.file->f_mode;
353
354 /* Our file permissions may have been overridden by global
355 * map permissions facing syscall side.
356 */
357 if (READ_ONCE(map->frozen))
358 mode &= ~FMODE_CAN_WRITE;
359 return mode;
360}
361
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100362#ifdef CONFIG_PROC_FS
363static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
364{
365 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100366 const struct bpf_array *array;
367 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200368 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100369
370 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
371 array = container_of(map, struct bpf_array, map);
372 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200373 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100374 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100375
376 seq_printf(m,
377 "map_type:\t%u\n"
378 "key_size:\t%u\n"
379 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100380 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100381 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200382 "memlock:\t%llu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200383 "map_id:\t%u\n"
384 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100385 map->map_type,
386 map->key_size,
387 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100388 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100389 map->map_flags,
Daniel Borkmann4316b402018-06-02 23:06:34 +0200390 map->pages * 1ULL << PAGE_SHIFT,
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200391 map->id,
392 READ_ONCE(map->frozen));
Daniel Borkmann21116b72016-11-26 01:28:07 +0100393
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200394 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100395 seq_printf(m, "owner_prog_type:\t%u\n",
396 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200397 seq_printf(m, "owner_jited:\t%u\n",
398 owner_jited);
399 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100400}
401#endif
402
Chenbo Feng6e71b042017-10-18 13:00:22 -0700403static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
404 loff_t *ppos)
405{
406 /* We need this handler such that alloc_file() enables
407 * f_mode with FMODE_CAN_READ.
408 */
409 return -EINVAL;
410}
411
412static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
413 size_t siz, loff_t *ppos)
414{
415 /* We need this handler such that alloc_file() enables
416 * f_mode with FMODE_CAN_WRITE.
417 */
418 return -EINVAL;
419}
420
Chenbo Fengf66e4482017-10-18 13:00:26 -0700421const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100422#ifdef CONFIG_PROC_FS
423 .show_fdinfo = bpf_map_show_fdinfo,
424#endif
425 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700426 .read = bpf_dummy_read,
427 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700428};
429
Chenbo Feng6e71b042017-10-18 13:00:22 -0700430int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100431{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700432 int ret;
433
434 ret = security_bpf_map(map, OPEN_FMODE(flags));
435 if (ret < 0)
436 return ret;
437
Daniel Borkmannaa797812015-10-29 14:58:06 +0100438 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700439 flags | O_CLOEXEC);
440}
441
442int bpf_get_file_flag(int flags)
443{
444 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
445 return -EINVAL;
446 if (flags & BPF_F_RDONLY)
447 return O_RDONLY;
448 if (flags & BPF_F_WRONLY)
449 return O_WRONLY;
450 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100451}
452
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700453/* helper macro to check that unused fields 'union bpf_attr' are zero */
454#define CHECK_ATTR(CMD) \
455 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
456 sizeof(attr->CMD##_LAST_FIELD), 0, \
457 sizeof(*attr) - \
458 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
459 sizeof(attr->CMD##_LAST_FIELD)) != NULL
460
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700461/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
462 * Return 0 on success and < 0 on error.
463 */
464static int bpf_obj_name_cpy(char *dst, const char *src)
465{
466 const char *end = src + BPF_OBJ_NAME_LEN;
467
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700468 memset(dst, 0, BPF_OBJ_NAME_LEN);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200469 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700470 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200471 if (!isalnum(*src) &&
472 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700473 return -EINVAL;
474 *dst++ = *src++;
475 }
476
477 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
478 if (src == end)
479 return -EINVAL;
480
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700481 return 0;
482}
483
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200484int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800485 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200486 const struct btf_type *key_type,
487 const struct btf_type *value_type)
488{
489 return -ENOTSUPP;
490}
491
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800492static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200493 u32 btf_key_id, u32 btf_value_id)
494{
495 const struct btf_type *key_type, *value_type;
496 u32 key_size, value_size;
497 int ret = 0;
498
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200499 /* Some maps allow key to be unspecified. */
500 if (btf_key_id) {
501 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
502 if (!key_type || key_size != map->key_size)
503 return -EINVAL;
504 } else {
505 key_type = btf_type_by_id(btf, 0);
506 if (!map->ops->map_check_btf)
507 return -EINVAL;
508 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200509
510 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
511 if (!value_type || value_size != map->value_size)
512 return -EINVAL;
513
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800514 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
515
516 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200517 if (map->map_flags & BPF_F_RDONLY_PROG)
518 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800519 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f1a2019-01-31 15:40:05 -0800520 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700521 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
522 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800523 return -ENOTSUPP;
524 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
525 map->value_size) {
526 WARN_ONCE(1,
527 "verifier bug spin_lock_off %d value_size %d\n",
528 map->spin_lock_off, map->value_size);
529 return -EFAULT;
530 }
531 }
532
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200533 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800534 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200535
536 return ret;
537}
538
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700539#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700540/* called via syscall */
541static int map_create(union bpf_attr *attr)
542{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700543 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700544 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700545 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700546 int err;
547
548 err = CHECK_ATTR(BPF_MAP_CREATE);
549 if (err)
550 return -EINVAL;
551
Chenbo Feng6e71b042017-10-18 13:00:22 -0700552 f_flags = bpf_get_file_flag(attr->map_flags);
553 if (f_flags < 0)
554 return f_flags;
555
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700556 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700557 ((unsigned int)numa_node >= nr_node_ids ||
558 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700559 return -EINVAL;
560
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700561 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
562 map = find_and_alloc_map(attr);
563 if (IS_ERR(map))
564 return PTR_ERR(map);
565
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700566 err = bpf_obj_name_cpy(map->name, attr->map_name);
567 if (err)
568 goto free_map_nouncharge;
569
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700570 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100571 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700572
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200573 if (attr->btf_key_type_id || attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700574 struct btf *btf;
575
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200576 if (!attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700577 err = -EINVAL;
578 goto free_map_nouncharge;
579 }
580
581 btf = btf_get_by_fd(attr->btf_fd);
582 if (IS_ERR(btf)) {
583 err = PTR_ERR(btf);
584 goto free_map_nouncharge;
585 }
586
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200587 err = map_check_btf(map, btf, attr->btf_key_type_id,
588 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700589 if (err) {
590 btf_put(btf);
591 goto free_map_nouncharge;
592 }
593
594 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700595 map->btf_key_type_id = attr->btf_key_type_id;
596 map->btf_value_type_id = attr->btf_value_type_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800597 } else {
598 map->spin_lock_off = -EINVAL;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700599 }
600
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700601 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700602 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100603 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700604
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700605 err = bpf_map_init_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700606 if (err)
607 goto free_map_sec;
608
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700609 err = bpf_map_alloc_id(map);
610 if (err)
611 goto free_map;
612
Chenbo Feng6e71b042017-10-18 13:00:22 -0700613 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700614 if (err < 0) {
615 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800616 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700617 * bpf_map_alloc_id() has published the map
618 * to the userspace and the userspace may
619 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
620 */
Peng Sun352d20d2019-02-27 22:36:25 +0800621 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700622 return err;
623 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700624
625 return err;
626
627free_map:
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700628 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700629free_map_sec:
630 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100631free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700632 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700633 map->ops->map_free(map);
634 return err;
635}
636
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700637/* if error is returned, fd is released.
638 * On success caller should complete fd access with matching fdput()
639 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100640struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700641{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700642 if (!f.file)
643 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700644 if (f.file->f_op != &bpf_map_fops) {
645 fdput(f);
646 return ERR_PTR(-EINVAL);
647 }
648
Daniel Borkmannc2101292015-10-29 14:58:07 +0100649 return f.file->private_data;
650}
651
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700652/* prog's and map's refcnt limit */
653#define BPF_MAX_REFCNT 32768
654
655struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100656{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700657 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
658 atomic_dec(&map->refcnt);
659 return ERR_PTR(-EBUSY);
660 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100661 if (uref)
662 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700663 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100664}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700665EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100666
667struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100668{
669 struct fd f = fdget(ufd);
670 struct bpf_map *map;
671
672 map = __bpf_map_get(f);
673 if (IS_ERR(map))
674 return map;
675
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700676 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100677 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700678
679 return map;
680}
681
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700682/* map_idr_lock should have been held */
683static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
684 bool uref)
685{
686 int refold;
687
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100688 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700689
690 if (refold >= BPF_MAX_REFCNT) {
691 __bpf_map_put(map, false);
692 return ERR_PTR(-EBUSY);
693 }
694
695 if (!refold)
696 return ERR_PTR(-ENOENT);
697
698 if (uref)
699 atomic_inc(&map->usercnt);
700
701 return map;
702}
703
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800704int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
705{
706 return -ENOTSUPP;
707}
708
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200709static void *__bpf_copy_key(void __user *ukey, u64 key_size)
710{
711 if (key_size)
712 return memdup_user(ukey, key_size);
713
714 if (ukey)
715 return ERR_PTR(-EINVAL);
716
717 return NULL;
718}
719
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700720/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800721#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700722
723static int map_lookup_elem(union bpf_attr *attr)
724{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100725 void __user *ukey = u64_to_user_ptr(attr->key);
726 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700727 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700728 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800729 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800730 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200731 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700732 int err;
733
734 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
735 return -EINVAL;
736
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800737 if (attr->flags & ~BPF_F_LOCK)
738 return -EINVAL;
739
Daniel Borkmann592867b2015-09-08 18:00:09 +0200740 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100741 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700742 if (IS_ERR(map))
743 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200744 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700745 err = -EPERM;
746 goto err_put;
747 }
748
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800749 if ((attr->flags & BPF_F_LOCK) &&
750 !map_value_has_spin_lock(map)) {
751 err = -EINVAL;
752 goto err_put;
753 }
754
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200755 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400756 if (IS_ERR(key)) {
757 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700758 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400759 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700760
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800761 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800762 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000763 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
764 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800765 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700766 else if (IS_FD_MAP(map))
767 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800768 else
769 value_size = map->value_size;
770
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800771 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800772 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700773 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800774 goto free_key;
775
Jakub Kicinskia3884572018-01-11 20:29:09 -0800776 if (bpf_map_is_dev_bound(map)) {
777 err = bpf_map_offload_lookup_elem(map, key, value);
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800778 goto done;
779 }
780
781 preempt_disable();
782 this_cpu_inc(bpf_prog_active);
783 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
784 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800785 err = bpf_percpu_hash_copy(map, key, value);
786 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
787 err = bpf_percpu_array_copy(map, key, value);
Roman Gushchinb741f162018-09-28 14:45:43 +0000788 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
789 err = bpf_percpu_cgroup_storage_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800790 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
791 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700792 } else if (IS_FD_ARRAY(map)) {
793 err = bpf_fd_array_map_lookup_elem(map, key, value);
794 } else if (IS_FD_HASH(map)) {
795 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700796 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
797 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200798 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
799 map->map_type == BPF_MAP_TYPE_STACK) {
800 err = map->ops->map_peek_elem(map, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800801 } else {
802 rcu_read_lock();
Daniel Borkmannc6110222019-05-14 01:18:55 +0200803 if (map->ops->map_lookup_elem_sys_only)
804 ptr = map->ops->map_lookup_elem_sys_only(map, key);
805 else
806 ptr = map->ops->map_lookup_elem(map, key);
Prashant Bhole509db282018-10-09 10:04:49 +0900807 if (IS_ERR(ptr)) {
808 err = PTR_ERR(ptr);
809 } else if (!ptr) {
810 err = -ENOENT;
811 } else {
812 err = 0;
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800813 if (attr->flags & BPF_F_LOCK)
814 /* lock 'ptr' and copy everything but lock */
815 copy_map_value_locked(map, value, ptr, true);
816 else
817 copy_map_value(map, value, ptr);
818 /* mask lock, since value wasn't zero inited */
819 check_and_init_map_lock(map, value);
Prashant Bhole509db282018-10-09 10:04:49 +0900820 }
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800821 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800822 }
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800823 this_cpu_dec(bpf_prog_active);
824 preempt_enable();
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800825
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800826done:
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800827 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800828 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700829
830 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800831 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800832 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700833
834 err = 0;
835
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800836free_value:
837 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700838free_key:
839 kfree(key);
840err_put:
841 fdput(f);
842 return err;
843}
844
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700845static void maybe_wait_bpf_programs(struct bpf_map *map)
846{
847 /* Wait for any running BPF programs to complete so that
848 * userspace, when we return to it, knows that all programs
849 * that could be running use the new map value.
850 */
851 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
852 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
853 synchronize_rcu();
854}
855
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800856#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700857
858static int map_update_elem(union bpf_attr *attr)
859{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100860 void __user *ukey = u64_to_user_ptr(attr->key);
861 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700862 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700863 struct bpf_map *map;
864 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800865 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200866 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700867 int err;
868
869 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
870 return -EINVAL;
871
Daniel Borkmann592867b2015-09-08 18:00:09 +0200872 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100873 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700874 if (IS_ERR(map))
875 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200876 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700877 err = -EPERM;
878 goto err_put;
879 }
880
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800881 if ((attr->flags & BPF_F_LOCK) &&
882 !map_value_has_spin_lock(map)) {
883 err = -EINVAL;
884 goto err_put;
885 }
886
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200887 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400888 if (IS_ERR(key)) {
889 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700890 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400891 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700892
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800893 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800894 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000895 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
896 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800897 value_size = round_up(map->value_size, 8) * num_possible_cpus();
898 else
899 value_size = map->value_size;
900
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700901 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800902 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700903 if (!value)
904 goto free_key;
905
906 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800907 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700908 goto free_value;
909
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200910 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800911 if (bpf_map_is_dev_bound(map)) {
912 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
913 goto out;
John Fastabend99ba2b52018-07-05 08:50:04 -0700914 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
915 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
916 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200917 err = map->ops->map_update_elem(map, key, value, attr->flags);
918 goto out;
919 }
920
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800921 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
922 * inside bpf map update or delete otherwise deadlocks are possible
923 */
924 preempt_disable();
925 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800926 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
927 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800928 err = bpf_percpu_hash_update(map, key, value, attr->flags);
929 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
930 err = bpf_percpu_array_update(map, key, value, attr->flags);
Roman Gushchinb741f162018-09-28 14:45:43 +0000931 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
932 err = bpf_percpu_cgroup_storage_update(map, key, value,
933 attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100934 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200935 rcu_read_lock();
936 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
937 attr->flags);
938 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700939 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
940 rcu_read_lock();
941 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
942 attr->flags);
943 rcu_read_unlock();
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700944 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
945 /* rcu_read_lock() is not needed */
946 err = bpf_fd_reuseport_array_update_elem(map, key, value,
947 attr->flags);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200948 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
949 map->map_type == BPF_MAP_TYPE_STACK) {
950 err = map->ops->map_push_elem(map, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800951 } else {
952 rcu_read_lock();
953 err = map->ops->map_update_elem(map, key, value, attr->flags);
954 rcu_read_unlock();
955 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800956 __this_cpu_dec(bpf_prog_active);
957 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700958 maybe_wait_bpf_programs(map);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200959out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700960free_value:
961 kfree(value);
962free_key:
963 kfree(key);
964err_put:
965 fdput(f);
966 return err;
967}
968
969#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
970
971static int map_delete_elem(union bpf_attr *attr)
972{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100973 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700974 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700975 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200976 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700977 void *key;
978 int err;
979
980 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
981 return -EINVAL;
982
Daniel Borkmann592867b2015-09-08 18:00:09 +0200983 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100984 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700985 if (IS_ERR(map))
986 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200987 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700988 err = -EPERM;
989 goto err_put;
990 }
991
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200992 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400993 if (IS_ERR(key)) {
994 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700995 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400996 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700997
Jakub Kicinskia3884572018-01-11 20:29:09 -0800998 if (bpf_map_is_dev_bound(map)) {
999 err = bpf_map_offload_delete_elem(map, key);
1000 goto out;
1001 }
1002
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001003 preempt_disable();
1004 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001005 rcu_read_lock();
1006 err = map->ops->map_delete_elem(map, key);
1007 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001008 __this_cpu_dec(bpf_prog_active);
1009 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001010 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001011out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001012 kfree(key);
1013err_put:
1014 fdput(f);
1015 return err;
1016}
1017
1018/* last field in 'union bpf_attr' used by this command */
1019#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1020
1021static int map_get_next_key(union bpf_attr *attr)
1022{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001023 void __user *ukey = u64_to_user_ptr(attr->key);
1024 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001025 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001026 struct bpf_map *map;
1027 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001028 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001029 int err;
1030
1031 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1032 return -EINVAL;
1033
Daniel Borkmann592867b2015-09-08 18:00:09 +02001034 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001035 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001036 if (IS_ERR(map))
1037 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001038 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001039 err = -EPERM;
1040 goto err_put;
1041 }
1042
Teng Qin8fe45922017-04-24 19:00:37 -07001043 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001044 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001045 if (IS_ERR(key)) {
1046 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001047 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001048 }
Teng Qin8fe45922017-04-24 19:00:37 -07001049 } else {
1050 key = NULL;
1051 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001052
1053 err = -ENOMEM;
1054 next_key = kmalloc(map->key_size, GFP_USER);
1055 if (!next_key)
1056 goto free_key;
1057
Jakub Kicinskia3884572018-01-11 20:29:09 -08001058 if (bpf_map_is_dev_bound(map)) {
1059 err = bpf_map_offload_get_next_key(map, key, next_key);
1060 goto out;
1061 }
1062
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001063 rcu_read_lock();
1064 err = map->ops->map_get_next_key(map, key, next_key);
1065 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001066out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001067 if (err)
1068 goto free_next_key;
1069
1070 err = -EFAULT;
1071 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1072 goto free_next_key;
1073
1074 err = 0;
1075
1076free_next_key:
1077 kfree(next_key);
1078free_key:
1079 kfree(key);
1080err_put:
1081 fdput(f);
1082 return err;
1083}
1084
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001085#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1086
1087static int map_lookup_and_delete_elem(union bpf_attr *attr)
1088{
1089 void __user *ukey = u64_to_user_ptr(attr->key);
1090 void __user *uvalue = u64_to_user_ptr(attr->value);
1091 int ufd = attr->map_fd;
1092 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001093 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001094 u32 value_size;
1095 struct fd f;
1096 int err;
1097
1098 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1099 return -EINVAL;
1100
1101 f = fdget(ufd);
1102 map = __bpf_map_get(f);
1103 if (IS_ERR(map))
1104 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001105 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001106 err = -EPERM;
1107 goto err_put;
1108 }
1109
1110 key = __bpf_copy_key(ukey, map->key_size);
1111 if (IS_ERR(key)) {
1112 err = PTR_ERR(key);
1113 goto err_put;
1114 }
1115
1116 value_size = map->value_size;
1117
1118 err = -ENOMEM;
1119 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1120 if (!value)
1121 goto free_key;
1122
1123 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1124 map->map_type == BPF_MAP_TYPE_STACK) {
1125 err = map->ops->map_pop_elem(map, value);
1126 } else {
1127 err = -ENOTSUPP;
1128 }
1129
1130 if (err)
1131 goto free_value;
1132
1133 if (copy_to_user(uvalue, value, value_size) != 0)
1134 goto free_value;
1135
1136 err = 0;
1137
1138free_value:
1139 kfree(value);
1140free_key:
1141 kfree(key);
1142err_put:
1143 fdput(f);
1144 return err;
1145}
1146
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001147#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1148
1149static int map_freeze(const union bpf_attr *attr)
1150{
1151 int err = 0, ufd = attr->map_fd;
1152 struct bpf_map *map;
1153 struct fd f;
1154
1155 if (CHECK_ATTR(BPF_MAP_FREEZE))
1156 return -EINVAL;
1157
1158 f = fdget(ufd);
1159 map = __bpf_map_get(f);
1160 if (IS_ERR(map))
1161 return PTR_ERR(map);
1162 if (READ_ONCE(map->frozen)) {
1163 err = -EBUSY;
1164 goto err_put;
1165 }
1166 if (!capable(CAP_SYS_ADMIN)) {
1167 err = -EPERM;
1168 goto err_put;
1169 }
1170
1171 WRITE_ONCE(map->frozen, true);
1172err_put:
1173 fdput(f);
1174 return err;
1175}
1176
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001177static const struct bpf_prog_ops * const bpf_prog_types[] = {
1178#define BPF_PROG_TYPE(_id, _name) \
1179 [_id] = & _name ## _prog_ops,
1180#define BPF_MAP_TYPE(_id, _ops)
1181#include <linux/bpf_types.h>
1182#undef BPF_PROG_TYPE
1183#undef BPF_MAP_TYPE
1184};
1185
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001186static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1187{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001188 const struct bpf_prog_ops *ops;
1189
1190 if (type >= ARRAY_SIZE(bpf_prog_types))
1191 return -EINVAL;
1192 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1193 ops = bpf_prog_types[type];
1194 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001195 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001196
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001197 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001198 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001199 else
1200 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001201 prog->type = type;
1202 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001203}
1204
1205/* drop refcnt on maps used by eBPF program and free auxilary data */
1206static void free_used_maps(struct bpf_prog_aux *aux)
1207{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001208 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001209 int i;
1210
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001211 for_each_cgroup_storage_type(stype) {
1212 if (!aux->cgroup_storage[stype])
1213 continue;
1214 bpf_cgroup_storage_release(aux->prog,
1215 aux->cgroup_storage[stype]);
1216 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07001217
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001218 for (i = 0; i < aux->used_map_cnt; i++)
1219 bpf_map_put(aux->used_maps[i]);
1220
1221 kfree(aux->used_maps);
1222}
1223
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001224int __bpf_prog_charge(struct user_struct *user, u32 pages)
1225{
1226 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1227 unsigned long user_bufs;
1228
1229 if (user) {
1230 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1231 if (user_bufs > memlock_limit) {
1232 atomic_long_sub(pages, &user->locked_vm);
1233 return -EPERM;
1234 }
1235 }
1236
1237 return 0;
1238}
1239
1240void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1241{
1242 if (user)
1243 atomic_long_sub(pages, &user->locked_vm);
1244}
1245
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001246static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1247{
1248 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001249 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001250
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001251 ret = __bpf_prog_charge(user, prog->pages);
1252 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001253 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001254 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001255 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001256
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001257 prog->aux->user = user;
1258 return 0;
1259}
1260
1261static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1262{
1263 struct user_struct *user = prog->aux->user;
1264
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001265 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001266 free_uid(user);
1267}
1268
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001269static int bpf_prog_alloc_id(struct bpf_prog *prog)
1270{
1271 int id;
1272
Shaohua Lib76354c2018-03-27 11:53:21 -07001273 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001274 spin_lock_bh(&prog_idr_lock);
1275 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1276 if (id > 0)
1277 prog->aux->id = id;
1278 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001279 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001280
1281 /* id is in [1, INT_MAX) */
1282 if (WARN_ON_ONCE(!id))
1283 return -ENOSPC;
1284
1285 return id > 0 ? 0 : id;
1286}
1287
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001288void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001289{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001290 /* cBPF to eBPF migrations are currently not in the idr store.
1291 * Offloaded programs are removed from the store when their device
1292 * disappears - even if someone grabs an fd to them they are unusable,
1293 * simply waiting for refcnt to drop to be freed.
1294 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001295 if (!prog->aux->id)
1296 return;
1297
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001298 if (do_idr_lock)
1299 spin_lock_bh(&prog_idr_lock);
1300 else
1301 __acquire(&prog_idr_lock);
1302
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001303 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001304 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001305
1306 if (do_idr_lock)
1307 spin_unlock_bh(&prog_idr_lock);
1308 else
1309 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001310}
1311
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001312static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001313{
1314 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1315
1316 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001317 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001318 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001319 bpf_prog_free(aux->prog);
1320}
1321
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001322static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001323{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001324 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001325 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001326 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001327 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001328 bpf_prog_kallsyms_del_all(prog);
Yonghong Song838e9692018-11-19 15:29:11 -08001329 btf_put(prog->aux->btf);
Yonghong Songba64e7d2018-11-24 23:20:44 -08001330 kvfree(prog->aux->func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001331 bpf_prog_free_linfo(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001332
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001333 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001334 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001335}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001336
1337void bpf_prog_put(struct bpf_prog *prog)
1338{
1339 __bpf_prog_put(prog, true);
1340}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001341EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001342
1343static int bpf_prog_release(struct inode *inode, struct file *filp)
1344{
1345 struct bpf_prog *prog = filp->private_data;
1346
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001347 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001348 return 0;
1349}
1350
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001351static void bpf_prog_get_stats(const struct bpf_prog *prog,
1352 struct bpf_prog_stats *stats)
1353{
1354 u64 nsecs = 0, cnt = 0;
1355 int cpu;
1356
1357 for_each_possible_cpu(cpu) {
1358 const struct bpf_prog_stats *st;
1359 unsigned int start;
1360 u64 tnsecs, tcnt;
1361
1362 st = per_cpu_ptr(prog->aux->stats, cpu);
1363 do {
1364 start = u64_stats_fetch_begin_irq(&st->syncp);
1365 tnsecs = st->nsecs;
1366 tcnt = st->cnt;
1367 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1368 nsecs += tnsecs;
1369 cnt += tcnt;
1370 }
1371 stats->nsecs = nsecs;
1372 stats->cnt = cnt;
1373}
1374
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001375#ifdef CONFIG_PROC_FS
1376static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1377{
1378 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001379 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001380 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001381
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001382 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001383 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001384 seq_printf(m,
1385 "prog_type:\t%u\n"
1386 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001387 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001388 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001389 "prog_id:\t%u\n"
1390 "run_time_ns:\t%llu\n"
1391 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001392 prog->type,
1393 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001394 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001395 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001396 prog->aux->id,
1397 stats.nsecs,
1398 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001399}
1400#endif
1401
Chenbo Fengf66e4482017-10-18 13:00:26 -07001402const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001403#ifdef CONFIG_PROC_FS
1404 .show_fdinfo = bpf_prog_show_fdinfo,
1405#endif
1406 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001407 .read = bpf_dummy_read,
1408 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001409};
1410
Daniel Borkmannb2197752015-10-29 14:58:09 +01001411int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001412{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001413 int ret;
1414
1415 ret = security_bpf_prog(prog);
1416 if (ret < 0)
1417 return ret;
1418
Daniel Borkmannaa797812015-10-29 14:58:06 +01001419 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1420 O_RDWR | O_CLOEXEC);
1421}
1422
Daniel Borkmann113214b2016-06-30 17:24:44 +02001423static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001424{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001425 if (!f.file)
1426 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001427 if (f.file->f_op != &bpf_prog_fops) {
1428 fdput(f);
1429 return ERR_PTR(-EINVAL);
1430 }
1431
Daniel Borkmannc2101292015-10-29 14:58:07 +01001432 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001433}
1434
Brenden Blanco59d36562016-07-19 12:16:46 -07001435struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001436{
Brenden Blanco59d36562016-07-19 12:16:46 -07001437 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1438 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001439 return ERR_PTR(-EBUSY);
1440 }
1441 return prog;
1442}
Brenden Blanco59d36562016-07-19 12:16:46 -07001443EXPORT_SYMBOL_GPL(bpf_prog_add);
1444
Daniel Borkmannc5405942016-11-09 22:02:34 +01001445void bpf_prog_sub(struct bpf_prog *prog, int i)
1446{
1447 /* Only to be used for undoing previous bpf_prog_add() in some
1448 * error path. We still know that another entity in our call
1449 * path holds a reference to the program, thus atomic_sub() can
1450 * be safely used in such cases!
1451 */
1452 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1453}
1454EXPORT_SYMBOL_GPL(bpf_prog_sub);
1455
Brenden Blanco59d36562016-07-19 12:16:46 -07001456struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1457{
1458 return bpf_prog_add(prog, 1);
1459}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001460EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001461
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001462/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001463struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001464{
1465 int refold;
1466
Mark Rutlandbfc18e32018-06-21 13:13:04 +01001467 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001468
1469 if (refold >= BPF_MAX_REFCNT) {
1470 __bpf_prog_put(prog, false);
1471 return ERR_PTR(-EBUSY);
1472 }
1473
1474 if (!refold)
1475 return ERR_PTR(-ENOENT);
1476
1477 return prog;
1478}
John Fastabenda6f6df62017-08-15 22:32:22 -07001479EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001480
Al Viro040ee692017-12-02 20:20:38 -05001481bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001482 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001483{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001484 /* not an attachment, just a refcount inc, always allow */
1485 if (!attach_type)
1486 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001487
1488 if (prog->type != *attach_type)
1489 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001490 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001491 return false;
1492
1493 return true;
1494}
1495
1496static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001497 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001498{
1499 struct fd f = fdget(ufd);
1500 struct bpf_prog *prog;
1501
Daniel Borkmann113214b2016-06-30 17:24:44 +02001502 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001503 if (IS_ERR(prog))
1504 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001505 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001506 prog = ERR_PTR(-EINVAL);
1507 goto out;
1508 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001509
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001510 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001511out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001512 fdput(f);
1513 return prog;
1514}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001515
1516struct bpf_prog *bpf_prog_get(u32 ufd)
1517{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001518 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001519}
1520
Jakub Kicinski248f3462017-11-03 13:56:20 -07001521struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001522 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001523{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001524 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001525}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001526EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001527
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001528/* Initially all BPF programs could be loaded w/o specifying
1529 * expected_attach_type. Later for some of them specifying expected_attach_type
1530 * at load time became required so that program could be validated properly.
1531 * Programs of types that are allowed to be loaded both w/ and w/o (for
1532 * backward compatibility) expected_attach_type, should have the default attach
1533 * type assigned to expected_attach_type for the latter case, so that it can be
1534 * validated later at attach time.
1535 *
1536 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1537 * prog type requires it but has some attach types that have to be backward
1538 * compatible.
1539 */
1540static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1541{
1542 switch (attr->prog_type) {
1543 case BPF_PROG_TYPE_CGROUP_SOCK:
1544 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1545 * exist so checking for non-zero is the way to go here.
1546 */
1547 if (!attr->expected_attach_type)
1548 attr->expected_attach_type =
1549 BPF_CGROUP_INET_SOCK_CREATE;
1550 break;
1551 }
1552}
1553
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001554static int
1555bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1556 enum bpf_attach_type expected_attach_type)
1557{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001558 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001559 case BPF_PROG_TYPE_CGROUP_SOCK:
1560 switch (expected_attach_type) {
1561 case BPF_CGROUP_INET_SOCK_CREATE:
1562 case BPF_CGROUP_INET4_POST_BIND:
1563 case BPF_CGROUP_INET6_POST_BIND:
1564 return 0;
1565 default:
1566 return -EINVAL;
1567 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001568 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1569 switch (expected_attach_type) {
1570 case BPF_CGROUP_INET4_BIND:
1571 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001572 case BPF_CGROUP_INET4_CONNECT:
1573 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001574 case BPF_CGROUP_UDP4_SENDMSG:
1575 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001576 return 0;
1577 default:
1578 return -EINVAL;
1579 }
1580 default:
1581 return 0;
1582 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001583}
1584
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001585/* last field in 'union bpf_attr' used by this command */
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001586#define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001587
Yonghong Song838e9692018-11-19 15:29:11 -08001588static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001589{
1590 enum bpf_prog_type type = attr->prog_type;
1591 struct bpf_prog *prog;
1592 int err;
1593 char license[128];
1594 bool is_gpl;
1595
1596 if (CHECK_ATTR(BPF_PROG_LOAD))
1597 return -EINVAL;
1598
David Millere9ee9ef2018-11-30 21:08:14 -08001599 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT))
David S. Millere07b98d2017-05-10 11:38:07 -07001600 return -EINVAL;
1601
David Millere9ee9ef2018-11-30 21:08:14 -08001602 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1603 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1604 !capable(CAP_SYS_ADMIN))
1605 return -EPERM;
1606
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001607 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001608 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001609 sizeof(license) - 1) < 0)
1610 return -EFAULT;
1611 license[sizeof(license) - 1] = 0;
1612
1613 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1614 is_gpl = license_is_gpl_compatible(license);
1615
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07001616 if (attr->insn_cnt == 0 ||
1617 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001618 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07001619 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1620 type != BPF_PROG_TYPE_CGROUP_SKB &&
1621 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001622 return -EPERM;
1623
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001624 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001625 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1626 return -EINVAL;
1627
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001628 /* plain bpf_prog allocation */
1629 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1630 if (!prog)
1631 return -ENOMEM;
1632
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001633 prog->expected_attach_type = attr->expected_attach_type;
1634
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001635 prog->aux->offload_requested = !!attr->prog_ifindex;
1636
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001637 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001638 if (err)
1639 goto free_prog_nouncharge;
1640
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001641 err = bpf_prog_charge_memlock(prog);
1642 if (err)
1643 goto free_prog_sec;
1644
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001645 prog->len = attr->insn_cnt;
1646
1647 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001648 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001649 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001650 goto free_prog;
1651
1652 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001653 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001654
1655 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001656 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001657
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001658 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001659 err = bpf_prog_offload_init(prog, attr);
1660 if (err)
1661 goto free_prog;
1662 }
1663
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001664 /* find program type: socket_filter vs tracing_filter */
1665 err = find_prog_type(type, prog);
1666 if (err < 0)
1667 goto free_prog;
1668
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001669 prog->aux->load_time = ktime_get_boot_ns();
1670 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1671 if (err)
1672 goto free_prog;
1673
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001674 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08001675 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001676 if (err < 0)
1677 goto free_used_maps;
1678
Daniel Borkmann9facc332018-06-15 02:30:48 +02001679 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001680 if (err < 0)
1681 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001682
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001683 err = bpf_prog_alloc_id(prog);
1684 if (err)
1685 goto free_used_maps;
1686
Daniel Borkmannaa797812015-10-29 14:58:06 +01001687 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001688 if (err < 0) {
1689 /* failed to allocate fd.
1690 * bpf_prog_put() is needed because the above
1691 * bpf_prog_alloc_id() has published the prog
1692 * to the userspace and the userspace may
1693 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1694 */
1695 bpf_prog_put(prog);
1696 return err;
1697 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001698
Daniel Borkmann74451e662017-02-16 22:24:50 +01001699 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08001700 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001701 return err;
1702
1703free_used_maps:
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001704 bpf_prog_free_linfo(prog);
Martin KaFai Lau5482e9a2018-12-01 17:08:44 -08001705 kvfree(prog->aux->func_info);
1706 btf_put(prog->aux->btf);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001707 bpf_prog_kallsyms_del_subprogs(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001708 free_used_maps(prog->aux);
1709free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001710 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001711free_prog_sec:
1712 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001713free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001714 bpf_prog_free(prog);
1715 return err;
1716}
1717
Chenbo Feng6e71b042017-10-18 13:00:22 -07001718#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001719
1720static int bpf_obj_pin(const union bpf_attr *attr)
1721{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001722 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001723 return -EINVAL;
1724
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001725 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001726}
1727
1728static int bpf_obj_get(const union bpf_attr *attr)
1729{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001730 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1731 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001732 return -EINVAL;
1733
Chenbo Feng6e71b042017-10-18 13:00:22 -07001734 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1735 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001736}
1737
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001738struct bpf_raw_tracepoint {
1739 struct bpf_raw_event_map *btp;
1740 struct bpf_prog *prog;
1741};
1742
1743static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1744{
1745 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1746
1747 if (raw_tp->prog) {
1748 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1749 bpf_prog_put(raw_tp->prog);
1750 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001751 bpf_put_raw_tracepoint(raw_tp->btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001752 kfree(raw_tp);
1753 return 0;
1754}
1755
1756static const struct file_operations bpf_raw_tp_fops = {
1757 .release = bpf_raw_tracepoint_release,
1758 .read = bpf_dummy_read,
1759 .write = bpf_dummy_write,
1760};
1761
1762#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1763
1764static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1765{
1766 struct bpf_raw_tracepoint *raw_tp;
1767 struct bpf_raw_event_map *btp;
1768 struct bpf_prog *prog;
1769 char tp_name[128];
1770 int tp_fd, err;
1771
1772 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1773 sizeof(tp_name) - 1) < 0)
1774 return -EFAULT;
1775 tp_name[sizeof(tp_name) - 1] = 0;
1776
Matt Mullinsa38d1102018-12-12 16:42:37 -08001777 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001778 if (!btp)
1779 return -ENOENT;
1780
1781 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001782 if (!raw_tp) {
1783 err = -ENOMEM;
1784 goto out_put_btp;
1785 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001786 raw_tp->btp = btp;
1787
Matt Mullins9df1c282019-04-26 11:49:47 -07001788 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001789 if (IS_ERR(prog)) {
1790 err = PTR_ERR(prog);
1791 goto out_free_tp;
1792 }
Matt Mullins9df1c282019-04-26 11:49:47 -07001793 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1794 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1795 err = -EINVAL;
1796 goto out_put_prog;
1797 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001798
1799 err = bpf_probe_register(raw_tp->btp, prog);
1800 if (err)
1801 goto out_put_prog;
1802
1803 raw_tp->prog = prog;
1804 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1805 O_CLOEXEC);
1806 if (tp_fd < 0) {
1807 bpf_probe_unregister(raw_tp->btp, prog);
1808 err = tp_fd;
1809 goto out_put_prog;
1810 }
1811 return tp_fd;
1812
1813out_put_prog:
1814 bpf_prog_put(prog);
1815out_free_tp:
1816 kfree(raw_tp);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001817out_put_btp:
1818 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001819 return err;
1820}
1821
Anders Roxell33491582018-04-03 14:09:47 +02001822static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1823 enum bpf_attach_type attach_type)
1824{
1825 switch (prog->type) {
1826 case BPF_PROG_TYPE_CGROUP_SOCK:
1827 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1828 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1829 default:
1830 return 0;
1831 }
1832}
1833
John Fastabend464bc0f2017-08-28 07:10:04 -07001834#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001835
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001836#define BPF_F_ATTACH_MASK \
1837 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1838
Daniel Mackf4324552016-11-23 16:52:27 +01001839static int bpf_prog_attach(const union bpf_attr *attr)
1840{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001841 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001842 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001843 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001844
1845 if (!capable(CAP_NET_ADMIN))
1846 return -EPERM;
1847
1848 if (CHECK_ATTR(BPF_PROG_ATTACH))
1849 return -EINVAL;
1850
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001851 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001852 return -EINVAL;
1853
Daniel Mackf4324552016-11-23 16:52:27 +01001854 switch (attr->attach_type) {
1855 case BPF_CGROUP_INET_INGRESS:
1856 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001857 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001858 break;
David Ahern610236582016-12-01 08:48:04 -08001859 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001860 case BPF_CGROUP_INET4_POST_BIND:
1861 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001862 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1863 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001864 case BPF_CGROUP_INET4_BIND:
1865 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001866 case BPF_CGROUP_INET4_CONNECT:
1867 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001868 case BPF_CGROUP_UDP4_SENDMSG:
1869 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001870 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1871 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001872 case BPF_CGROUP_SOCK_OPS:
1873 ptype = BPF_PROG_TYPE_SOCK_OPS;
1874 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001875 case BPF_CGROUP_DEVICE:
1876 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1877 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001878 case BPF_SK_MSG_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001879 ptype = BPF_PROG_TYPE_SK_MSG;
1880 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001881 case BPF_SK_SKB_STREAM_PARSER:
1882 case BPF_SK_SKB_STREAM_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001883 ptype = BPF_PROG_TYPE_SK_SKB;
1884 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01001885 case BPF_LIRC_MODE2:
Sean Youngfdb5c452018-06-19 00:04:24 +01001886 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1887 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001888 case BPF_FLOW_DISSECTOR:
1889 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1890 break;
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001891 case BPF_CGROUP_SYSCTL:
1892 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1893 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001894 default:
1895 return -EINVAL;
1896 }
1897
David Ahernb2cd1252016-12-01 08:48:03 -08001898 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1899 if (IS_ERR(prog))
1900 return PTR_ERR(prog);
1901
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001902 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1903 bpf_prog_put(prog);
1904 return -EINVAL;
1905 }
1906
Sean Youngfdb5c452018-06-19 00:04:24 +01001907 switch (ptype) {
1908 case BPF_PROG_TYPE_SK_SKB:
1909 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001910 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01001911 break;
1912 case BPF_PROG_TYPE_LIRC_MODE2:
1913 ret = lirc_prog_attach(attr, prog);
1914 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001915 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1916 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1917 break;
Sean Youngfdb5c452018-06-19 00:04:24 +01001918 default:
1919 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001920 }
1921
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001922 if (ret)
1923 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001924 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001925}
1926
1927#define BPF_PROG_DETACH_LAST_FIELD attach_type
1928
1929static int bpf_prog_detach(const union bpf_attr *attr)
1930{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001931 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001932
1933 if (!capable(CAP_NET_ADMIN))
1934 return -EPERM;
1935
1936 if (CHECK_ATTR(BPF_PROG_DETACH))
1937 return -EINVAL;
1938
1939 switch (attr->attach_type) {
1940 case BPF_CGROUP_INET_INGRESS:
1941 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001942 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1943 break;
David Ahern610236582016-12-01 08:48:04 -08001944 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001945 case BPF_CGROUP_INET4_POST_BIND:
1946 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001947 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1948 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001949 case BPF_CGROUP_INET4_BIND:
1950 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001951 case BPF_CGROUP_INET4_CONNECT:
1952 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001953 case BPF_CGROUP_UDP4_SENDMSG:
1954 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001955 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1956 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001957 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001958 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001959 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001960 case BPF_CGROUP_DEVICE:
1961 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1962 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001963 case BPF_SK_MSG_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001964 return sock_map_get_from_fd(attr, NULL);
John Fastabend5a67da22017-09-08 14:00:49 -07001965 case BPF_SK_SKB_STREAM_PARSER:
1966 case BPF_SK_SKB_STREAM_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001967 return sock_map_get_from_fd(attr, NULL);
Sean Youngf4364dc2018-05-27 12:24:09 +01001968 case BPF_LIRC_MODE2:
1969 return lirc_prog_detach(attr);
Petar Penkovd58e4682018-09-14 07:46:18 -07001970 case BPF_FLOW_DISSECTOR:
1971 return skb_flow_dissector_bpf_prog_detach(attr);
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001972 case BPF_CGROUP_SYSCTL:
1973 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1974 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001975 default:
1976 return -EINVAL;
1977 }
1978
Sean Youngfdb5c452018-06-19 00:04:24 +01001979 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01001980}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001981
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001982#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1983
1984static int bpf_prog_query(const union bpf_attr *attr,
1985 union bpf_attr __user *uattr)
1986{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001987 if (!capable(CAP_NET_ADMIN))
1988 return -EPERM;
1989 if (CHECK_ATTR(BPF_PROG_QUERY))
1990 return -EINVAL;
1991 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1992 return -EINVAL;
1993
1994 switch (attr->query.attach_type) {
1995 case BPF_CGROUP_INET_INGRESS:
1996 case BPF_CGROUP_INET_EGRESS:
1997 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001998 case BPF_CGROUP_INET4_BIND:
1999 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002000 case BPF_CGROUP_INET4_POST_BIND:
2001 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002002 case BPF_CGROUP_INET4_CONNECT:
2003 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002004 case BPF_CGROUP_UDP4_SENDMSG:
2005 case BPF_CGROUP_UDP6_SENDMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002006 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05002007 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002008 case BPF_CGROUP_SYSCTL:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002009 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01002010 case BPF_LIRC_MODE2:
2011 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07002012 case BPF_FLOW_DISSECTOR:
2013 return skb_flow_dissector_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002014 default:
2015 return -EINVAL;
2016 }
Sean Youngfdb5c452018-06-19 00:04:24 +01002017
2018 return cgroup_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002019}
Daniel Mackf4324552016-11-23 16:52:27 +01002020
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002021#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002022
2023static int bpf_prog_test_run(const union bpf_attr *attr,
2024 union bpf_attr __user *uattr)
2025{
2026 struct bpf_prog *prog;
2027 int ret = -ENOTSUPP;
2028
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08002029 if (!capable(CAP_SYS_ADMIN))
2030 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002031 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2032 return -EINVAL;
2033
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002034 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2035 (!attr->test.ctx_size_in && attr->test.ctx_in))
2036 return -EINVAL;
2037
2038 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2039 (!attr->test.ctx_size_out && attr->test.ctx_out))
2040 return -EINVAL;
2041
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002042 prog = bpf_prog_get(attr->test.prog_fd);
2043 if (IS_ERR(prog))
2044 return PTR_ERR(prog);
2045
2046 if (prog->aux->ops->test_run)
2047 ret = prog->aux->ops->test_run(prog, attr, uattr);
2048
2049 bpf_prog_put(prog);
2050 return ret;
2051}
2052
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002053#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2054
2055static int bpf_obj_get_next_id(const union bpf_attr *attr,
2056 union bpf_attr __user *uattr,
2057 struct idr *idr,
2058 spinlock_t *lock)
2059{
2060 u32 next_id = attr->start_id;
2061 int err = 0;
2062
2063 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2064 return -EINVAL;
2065
2066 if (!capable(CAP_SYS_ADMIN))
2067 return -EPERM;
2068
2069 next_id++;
2070 spin_lock_bh(lock);
2071 if (!idr_get_next(idr, &next_id))
2072 err = -ENOENT;
2073 spin_unlock_bh(lock);
2074
2075 if (!err)
2076 err = put_user(next_id, &uattr->next_id);
2077
2078 return err;
2079}
2080
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002081#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2082
2083static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2084{
2085 struct bpf_prog *prog;
2086 u32 id = attr->prog_id;
2087 int fd;
2088
2089 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2090 return -EINVAL;
2091
2092 if (!capable(CAP_SYS_ADMIN))
2093 return -EPERM;
2094
2095 spin_lock_bh(&prog_idr_lock);
2096 prog = idr_find(&prog_idr, id);
2097 if (prog)
2098 prog = bpf_prog_inc_not_zero(prog);
2099 else
2100 prog = ERR_PTR(-ENOENT);
2101 spin_unlock_bh(&prog_idr_lock);
2102
2103 if (IS_ERR(prog))
2104 return PTR_ERR(prog);
2105
2106 fd = bpf_prog_new_fd(prog);
2107 if (fd < 0)
2108 bpf_prog_put(prog);
2109
2110 return fd;
2111}
2112
Chenbo Feng6e71b042017-10-18 13:00:22 -07002113#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002114
2115static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2116{
2117 struct bpf_map *map;
2118 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002119 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002120 int fd;
2121
Chenbo Feng6e71b042017-10-18 13:00:22 -07002122 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2123 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002124 return -EINVAL;
2125
2126 if (!capable(CAP_SYS_ADMIN))
2127 return -EPERM;
2128
Chenbo Feng6e71b042017-10-18 13:00:22 -07002129 f_flags = bpf_get_file_flag(attr->open_flags);
2130 if (f_flags < 0)
2131 return f_flags;
2132
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002133 spin_lock_bh(&map_idr_lock);
2134 map = idr_find(&map_idr, id);
2135 if (map)
2136 map = bpf_map_inc_not_zero(map, true);
2137 else
2138 map = ERR_PTR(-ENOENT);
2139 spin_unlock_bh(&map_idr_lock);
2140
2141 if (IS_ERR(map))
2142 return PTR_ERR(map);
2143
Chenbo Feng6e71b042017-10-18 13:00:22 -07002144 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002145 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002146 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002147
2148 return fd;
2149}
2150
Daniel Borkmann7105e822017-12-20 13:42:57 +01002151static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002152 unsigned long addr, u32 *off,
2153 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002154{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002155 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002156 int i;
2157
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002158 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2159 map = prog->aux->used_maps[i];
2160 if (map == (void *)addr) {
2161 *type = BPF_PSEUDO_MAP_FD;
2162 return map;
2163 }
2164 if (!map->ops->map_direct_value_meta)
2165 continue;
2166 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2167 *type = BPF_PSEUDO_MAP_VALUE;
2168 return map;
2169 }
2170 }
2171
Daniel Borkmann7105e822017-12-20 13:42:57 +01002172 return NULL;
2173}
2174
2175static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2176{
2177 const struct bpf_map *map;
2178 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002179 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002180 u64 imm;
2181 int i;
2182
2183 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2184 GFP_USER);
2185 if (!insns)
2186 return insns;
2187
2188 for (i = 0; i < prog->len; i++) {
2189 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2190 insns[i].code = BPF_JMP | BPF_CALL;
2191 insns[i].imm = BPF_FUNC_tail_call;
2192 /* fall-through */
2193 }
2194 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2195 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2196 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2197 insns[i].code = BPF_JMP | BPF_CALL;
2198 if (!bpf_dump_raw_ok())
2199 insns[i].imm = 0;
2200 continue;
2201 }
2202
2203 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2204 continue;
2205
2206 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002207 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002208 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002209 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002210 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002211 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002212 continue;
2213 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002214 }
2215
2216 return insns;
2217}
2218
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002219static int set_info_rec_size(struct bpf_prog_info *info)
2220{
2221 /*
2222 * Ensure info.*_rec_size is the same as kernel expected size
2223 *
2224 * or
2225 *
2226 * Only allow zero *_rec_size if both _rec_size and _cnt are
2227 * zero. In this case, the kernel will set the expected
2228 * _rec_size back to the info.
2229 */
2230
Yonghong Song11d8b822018-12-10 14:14:08 -08002231 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002232 info->func_info_rec_size != sizeof(struct bpf_func_info))
2233 return -EINVAL;
2234
Yonghong Song11d8b822018-12-10 14:14:08 -08002235 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002236 info->line_info_rec_size != sizeof(struct bpf_line_info))
2237 return -EINVAL;
2238
Yonghong Song11d8b822018-12-10 14:14:08 -08002239 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002240 info->jited_line_info_rec_size != sizeof(__u64))
2241 return -EINVAL;
2242
2243 info->func_info_rec_size = sizeof(struct bpf_func_info);
2244 info->line_info_rec_size = sizeof(struct bpf_line_info);
2245 info->jited_line_info_rec_size = sizeof(__u64);
2246
2247 return 0;
2248}
2249
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002250static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2251 const union bpf_attr *attr,
2252 union bpf_attr __user *uattr)
2253{
2254 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2255 struct bpf_prog_info info = {};
2256 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002257 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002258 char __user *uinsns;
2259 u32 ulen;
2260 int err;
2261
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002262 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002263 if (err)
2264 return err;
2265 info_len = min_t(u32, sizeof(info), info_len);
2266
2267 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02002268 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002269
2270 info.type = prog->type;
2271 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002272 info.load_time = prog->aux->load_time;
2273 info.created_by_uid = from_kuid_munged(current_user_ns(),
2274 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02002275 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002276
2277 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002278 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2279
2280 ulen = info.nr_map_ids;
2281 info.nr_map_ids = prog->aux->used_map_cnt;
2282 ulen = min_t(u32, info.nr_map_ids, ulen);
2283 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07002284 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002285 u32 i;
2286
2287 for (i = 0; i < ulen; i++)
2288 if (put_user(prog->aux->used_maps[i]->id,
2289 &user_map_ids[i]))
2290 return -EFAULT;
2291 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002292
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002293 err = set_info_rec_size(&info);
2294 if (err)
2295 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08002296
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002297 bpf_prog_get_stats(prog, &stats);
2298 info.run_time_ns = stats.nsecs;
2299 info.run_cnt = stats.cnt;
2300
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002301 if (!capable(CAP_SYS_ADMIN)) {
2302 info.jited_prog_len = 0;
2303 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302304 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01002305 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08002306 info.nr_func_info = 0;
2307 info.nr_line_info = 0;
2308 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002309 goto done;
2310 }
2311
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002312 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02002313 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002314 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01002315 struct bpf_insn *insns_sanitized;
2316 bool fault;
2317
2318 if (prog->blinded && !bpf_dump_raw_ok()) {
2319 info.xlated_prog_insns = 0;
2320 goto done;
2321 }
2322 insns_sanitized = bpf_insn_prepare_dump(prog);
2323 if (!insns_sanitized)
2324 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002325 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2326 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002327 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2328 kfree(insns_sanitized);
2329 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002330 return -EFAULT;
2331 }
2332
Jakub Kicinski675fc272017-12-27 18:39:09 -08002333 if (bpf_prog_is_dev_bound(prog->aux)) {
2334 err = bpf_prog_offload_info_fill(&info, prog);
2335 if (err)
2336 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08002337 goto done;
2338 }
2339
2340 /* NOTE: the following code is supposed to be skipped for offload.
2341 * bpf_prog_offload_info_fill() is the place to fill similar fields
2342 * for offload.
2343 */
2344 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05302345 if (prog->aux->func_cnt) {
2346 u32 i;
2347
2348 info.jited_prog_len = 0;
2349 for (i = 0; i < prog->aux->func_cnt; i++)
2350 info.jited_prog_len += prog->aux->func[i]->jited_len;
2351 } else {
2352 info.jited_prog_len = prog->jited_len;
2353 }
2354
Jiong Wangfcfb1262018-01-16 16:05:19 -08002355 if (info.jited_prog_len && ulen) {
2356 if (bpf_dump_raw_ok()) {
2357 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2358 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05302359
2360 /* for multi-function programs, copy the JITed
2361 * instructions for all the functions
2362 */
2363 if (prog->aux->func_cnt) {
2364 u32 len, free, i;
2365 u8 *img;
2366
2367 free = ulen;
2368 for (i = 0; i < prog->aux->func_cnt; i++) {
2369 len = prog->aux->func[i]->jited_len;
2370 len = min_t(u32, len, free);
2371 img = (u8 *) prog->aux->func[i]->bpf_func;
2372 if (copy_to_user(uinsns, img, len))
2373 return -EFAULT;
2374 uinsns += len;
2375 free -= len;
2376 if (!free)
2377 break;
2378 }
2379 } else {
2380 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2381 return -EFAULT;
2382 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002383 } else {
2384 info.jited_prog_insns = 0;
2385 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002386 }
2387
Sandipan Dasdbecd732018-05-24 12:26:48 +05302388 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07002389 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002390 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05302391 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07002392 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302393 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302394 u32 i;
2395
2396 /* copy the address of the kernel symbol
2397 * corresponding to each function
2398 */
2399 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2400 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07002401 if (prog->aux->func_cnt) {
2402 for (i = 0; i < ulen; i++) {
2403 ksym_addr = (unsigned long)
2404 prog->aux->func[i]->bpf_func;
2405 if (put_user((u64) ksym_addr,
2406 &user_ksyms[i]))
2407 return -EFAULT;
2408 }
2409 } else {
2410 ksym_addr = (unsigned long) prog->bpf_func;
2411 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05302412 return -EFAULT;
2413 }
2414 } else {
2415 info.jited_ksyms = 0;
2416 }
2417 }
2418
Sandipan Das815581c2018-05-24 12:26:52 +05302419 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07002420 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002421 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05302422 if (bpf_dump_raw_ok()) {
2423 u32 __user *user_lens;
2424 u32 func_len, i;
2425
2426 /* copy the JITed image lengths for each function */
2427 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2428 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07002429 if (prog->aux->func_cnt) {
2430 for (i = 0; i < ulen; i++) {
2431 func_len =
2432 prog->aux->func[i]->jited_len;
2433 if (put_user(func_len, &user_lens[i]))
2434 return -EFAULT;
2435 }
2436 } else {
2437 func_len = prog->jited_len;
2438 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05302439 return -EFAULT;
2440 }
2441 } else {
2442 info.jited_func_lens = 0;
2443 }
2444 }
2445
Martin KaFai Lau73372242018-12-05 17:35:43 -08002446 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08002447 info.btf_id = btf_id(prog->aux->btf);
2448
Yonghong Song11d8b822018-12-10 14:14:08 -08002449 ulen = info.nr_func_info;
2450 info.nr_func_info = prog->aux->func_info_cnt;
2451 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002452 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08002453
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002454 user_finfo = u64_to_user_ptr(info.func_info);
2455 ulen = min_t(u32, info.nr_func_info, ulen);
2456 if (copy_to_user(user_finfo, prog->aux->func_info,
2457 info.func_info_rec_size * ulen))
2458 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08002459 }
2460
Yonghong Song11d8b822018-12-10 14:14:08 -08002461 ulen = info.nr_line_info;
2462 info.nr_line_info = prog->aux->nr_linfo;
2463 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002464 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002465
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002466 user_linfo = u64_to_user_ptr(info.line_info);
2467 ulen = min_t(u32, info.nr_line_info, ulen);
2468 if (copy_to_user(user_linfo, prog->aux->linfo,
2469 info.line_info_rec_size * ulen))
2470 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002471 }
2472
Yonghong Song11d8b822018-12-10 14:14:08 -08002473 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002474 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08002475 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002476 else
Yonghong Song11d8b822018-12-10 14:14:08 -08002477 info.nr_jited_line_info = 0;
2478 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002479 if (bpf_dump_raw_ok()) {
2480 __u64 __user *user_linfo;
2481 u32 i;
2482
2483 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08002484 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002485 for (i = 0; i < ulen; i++) {
2486 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2487 &user_linfo[i]))
2488 return -EFAULT;
2489 }
2490 } else {
2491 info.jited_line_info = 0;
2492 }
2493 }
2494
Song Liuc872bdb2018-12-12 09:37:46 -08002495 ulen = info.nr_prog_tags;
2496 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2497 if (ulen) {
2498 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2499 u32 i;
2500
2501 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2502 ulen = min_t(u32, info.nr_prog_tags, ulen);
2503 if (prog->aux->func_cnt) {
2504 for (i = 0; i < ulen; i++) {
2505 if (copy_to_user(user_prog_tags[i],
2506 prog->aux->func[i]->tag,
2507 BPF_TAG_SIZE))
2508 return -EFAULT;
2509 }
2510 } else {
2511 if (copy_to_user(user_prog_tags[0],
2512 prog->tag, BPF_TAG_SIZE))
2513 return -EFAULT;
2514 }
2515 }
2516
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002517done:
2518 if (copy_to_user(uinfo, &info, info_len) ||
2519 put_user(info_len, &uattr->info.info_len))
2520 return -EFAULT;
2521
2522 return 0;
2523}
2524
2525static int bpf_map_get_info_by_fd(struct bpf_map *map,
2526 const union bpf_attr *attr,
2527 union bpf_attr __user *uattr)
2528{
2529 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2530 struct bpf_map_info info = {};
2531 u32 info_len = attr->info.info_len;
2532 int err;
2533
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002534 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002535 if (err)
2536 return err;
2537 info_len = min_t(u32, sizeof(info), info_len);
2538
2539 info.type = map->map_type;
2540 info.id = map->id;
2541 info.key_size = map->key_size;
2542 info.value_size = map->value_size;
2543 info.max_entries = map->max_entries;
2544 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002545 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002546
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002547 if (map->btf) {
2548 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002549 info.btf_key_type_id = map->btf_key_type_id;
2550 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002551 }
2552
Jakub Kicinski52775b32018-01-17 19:13:28 -08002553 if (bpf_map_is_dev_bound(map)) {
2554 err = bpf_map_offload_info_fill(&info, map);
2555 if (err)
2556 return err;
2557 }
2558
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002559 if (copy_to_user(uinfo, &info, info_len) ||
2560 put_user(info_len, &uattr->info.info_len))
2561 return -EFAULT;
2562
2563 return 0;
2564}
2565
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002566static int bpf_btf_get_info_by_fd(struct btf *btf,
2567 const union bpf_attr *attr,
2568 union bpf_attr __user *uattr)
2569{
2570 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2571 u32 info_len = attr->info.info_len;
2572 int err;
2573
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002574 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002575 if (err)
2576 return err;
2577
2578 return btf_get_info_by_fd(btf, attr, uattr);
2579}
2580
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002581#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2582
2583static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2584 union bpf_attr __user *uattr)
2585{
2586 int ufd = attr->info.bpf_fd;
2587 struct fd f;
2588 int err;
2589
2590 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2591 return -EINVAL;
2592
2593 f = fdget(ufd);
2594 if (!f.file)
2595 return -EBADFD;
2596
2597 if (f.file->f_op == &bpf_prog_fops)
2598 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2599 uattr);
2600 else if (f.file->f_op == &bpf_map_fops)
2601 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2602 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002603 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002604 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002605 else
2606 err = -EINVAL;
2607
2608 fdput(f);
2609 return err;
2610}
2611
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002612#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2613
2614static int bpf_btf_load(const union bpf_attr *attr)
2615{
2616 if (CHECK_ATTR(BPF_BTF_LOAD))
2617 return -EINVAL;
2618
2619 if (!capable(CAP_SYS_ADMIN))
2620 return -EPERM;
2621
2622 return btf_new_fd(attr);
2623}
2624
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002625#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2626
2627static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2628{
2629 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2630 return -EINVAL;
2631
2632 if (!capable(CAP_SYS_ADMIN))
2633 return -EPERM;
2634
2635 return btf_get_fd_by_id(attr->btf_id);
2636}
2637
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002638static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2639 union bpf_attr __user *uattr,
2640 u32 prog_id, u32 fd_type,
2641 const char *buf, u64 probe_offset,
2642 u64 probe_addr)
2643{
2644 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2645 u32 len = buf ? strlen(buf) : 0, input_len;
2646 int err = 0;
2647
2648 if (put_user(len, &uattr->task_fd_query.buf_len))
2649 return -EFAULT;
2650 input_len = attr->task_fd_query.buf_len;
2651 if (input_len && ubuf) {
2652 if (!len) {
2653 /* nothing to copy, just make ubuf NULL terminated */
2654 char zero = '\0';
2655
2656 if (put_user(zero, ubuf))
2657 return -EFAULT;
2658 } else if (input_len >= len + 1) {
2659 /* ubuf can hold the string with NULL terminator */
2660 if (copy_to_user(ubuf, buf, len + 1))
2661 return -EFAULT;
2662 } else {
2663 /* ubuf cannot hold the string with NULL terminator,
2664 * do a partial copy with NULL terminator.
2665 */
2666 char zero = '\0';
2667
2668 err = -ENOSPC;
2669 if (copy_to_user(ubuf, buf, input_len - 1))
2670 return -EFAULT;
2671 if (put_user(zero, ubuf + input_len - 1))
2672 return -EFAULT;
2673 }
2674 }
2675
2676 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2677 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2678 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2679 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2680 return -EFAULT;
2681
2682 return err;
2683}
2684
2685#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2686
2687static int bpf_task_fd_query(const union bpf_attr *attr,
2688 union bpf_attr __user *uattr)
2689{
2690 pid_t pid = attr->task_fd_query.pid;
2691 u32 fd = attr->task_fd_query.fd;
2692 const struct perf_event *event;
2693 struct files_struct *files;
2694 struct task_struct *task;
2695 struct file *file;
2696 int err;
2697
2698 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2699 return -EINVAL;
2700
2701 if (!capable(CAP_SYS_ADMIN))
2702 return -EPERM;
2703
2704 if (attr->task_fd_query.flags != 0)
2705 return -EINVAL;
2706
2707 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2708 if (!task)
2709 return -ENOENT;
2710
2711 files = get_files_struct(task);
2712 put_task_struct(task);
2713 if (!files)
2714 return -ENOENT;
2715
2716 err = 0;
2717 spin_lock(&files->file_lock);
2718 file = fcheck_files(files, fd);
2719 if (!file)
2720 err = -EBADF;
2721 else
2722 get_file(file);
2723 spin_unlock(&files->file_lock);
2724 put_files_struct(files);
2725
2726 if (err)
2727 goto out;
2728
2729 if (file->f_op == &bpf_raw_tp_fops) {
2730 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2731 struct bpf_raw_event_map *btp = raw_tp->btp;
2732
2733 err = bpf_task_fd_query_copy(attr, uattr,
2734 raw_tp->prog->aux->id,
2735 BPF_FD_TYPE_RAW_TRACEPOINT,
2736 btp->tp->name, 0, 0);
2737 goto put_file;
2738 }
2739
2740 event = perf_get_event(file);
2741 if (!IS_ERR(event)) {
2742 u64 probe_offset, probe_addr;
2743 u32 prog_id, fd_type;
2744 const char *buf;
2745
2746 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2747 &buf, &probe_offset,
2748 &probe_addr);
2749 if (!err)
2750 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2751 fd_type, buf,
2752 probe_offset,
2753 probe_addr);
2754 goto put_file;
2755 }
2756
2757 err = -ENOTSUPP;
2758put_file:
2759 fput(file);
2760out:
2761 return err;
2762}
2763
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002764SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2765{
2766 union bpf_attr attr = {};
2767 int err;
2768
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002769 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002770 return -EPERM;
2771
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002772 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002773 if (err)
2774 return err;
2775 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002776
2777 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2778 if (copy_from_user(&attr, uattr, size) != 0)
2779 return -EFAULT;
2780
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002781 err = security_bpf(cmd, &attr, size);
2782 if (err < 0)
2783 return err;
2784
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002785 switch (cmd) {
2786 case BPF_MAP_CREATE:
2787 err = map_create(&attr);
2788 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002789 case BPF_MAP_LOOKUP_ELEM:
2790 err = map_lookup_elem(&attr);
2791 break;
2792 case BPF_MAP_UPDATE_ELEM:
2793 err = map_update_elem(&attr);
2794 break;
2795 case BPF_MAP_DELETE_ELEM:
2796 err = map_delete_elem(&attr);
2797 break;
2798 case BPF_MAP_GET_NEXT_KEY:
2799 err = map_get_next_key(&attr);
2800 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02002801 case BPF_MAP_FREEZE:
2802 err = map_freeze(&attr);
2803 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002804 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08002805 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002806 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002807 case BPF_OBJ_PIN:
2808 err = bpf_obj_pin(&attr);
2809 break;
2810 case BPF_OBJ_GET:
2811 err = bpf_obj_get(&attr);
2812 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002813 case BPF_PROG_ATTACH:
2814 err = bpf_prog_attach(&attr);
2815 break;
2816 case BPF_PROG_DETACH:
2817 err = bpf_prog_detach(&attr);
2818 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002819 case BPF_PROG_QUERY:
2820 err = bpf_prog_query(&attr, uattr);
2821 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002822 case BPF_PROG_TEST_RUN:
2823 err = bpf_prog_test_run(&attr, uattr);
2824 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002825 case BPF_PROG_GET_NEXT_ID:
2826 err = bpf_obj_get_next_id(&attr, uattr,
2827 &prog_idr, &prog_idr_lock);
2828 break;
2829 case BPF_MAP_GET_NEXT_ID:
2830 err = bpf_obj_get_next_id(&attr, uattr,
2831 &map_idr, &map_idr_lock);
2832 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002833 case BPF_PROG_GET_FD_BY_ID:
2834 err = bpf_prog_get_fd_by_id(&attr);
2835 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002836 case BPF_MAP_GET_FD_BY_ID:
2837 err = bpf_map_get_fd_by_id(&attr);
2838 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002839 case BPF_OBJ_GET_INFO_BY_FD:
2840 err = bpf_obj_get_info_by_fd(&attr, uattr);
2841 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002842 case BPF_RAW_TRACEPOINT_OPEN:
2843 err = bpf_raw_tracepoint_open(&attr);
2844 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002845 case BPF_BTF_LOAD:
2846 err = bpf_btf_load(&attr);
2847 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002848 case BPF_BTF_GET_FD_BY_ID:
2849 err = bpf_btf_get_fd_by_id(&attr);
2850 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002851 case BPF_TASK_FD_QUERY:
2852 err = bpf_task_fd_query(&attr, uattr);
2853 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02002854 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2855 err = map_lookup_and_delete_elem(&attr);
2856 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002857 default:
2858 err = -EINVAL;
2859 break;
2860 }
2861
2862 return err;
2863}