blob: d995eedfdd1666d16653d179587f4b1d19da1af0 [file] [log] [blame]
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010013#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +010014#include <linux/bpf_lirc.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070015#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070016#include <linux/syscalls.h>
17#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010018#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010019#include <linux/vmalloc.h>
20#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070021#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070022#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070023#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070024#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070025#include <linux/license.h>
26#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070027#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010028#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070029#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070030#include <linux/cred.h>
31#include <linux/timekeeping.h>
32#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010033#include <linux/nospec.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070034
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070035#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
38 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
39#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
40#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
41
Chenbo Feng6e71b042017-10-18 13:00:22 -070042#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
43
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080044DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070045static DEFINE_IDR(prog_idr);
46static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070047static DEFINE_IDR(map_idr);
48static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080049
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070050int sysctl_unprivileged_bpf_disabled __read_mostly;
51
Johannes Berg40077e02017-04-11 15:34:58 +020052static const struct bpf_map_ops * const bpf_map_types[] = {
53#define BPF_PROG_TYPE(_id, _ops)
54#define BPF_MAP_TYPE(_id, _ops) \
55 [_id] = &_ops,
56#include <linux/bpf_types.h>
57#undef BPF_PROG_TYPE
58#undef BPF_MAP_TYPE
59};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070060
Mickaël Salaün752ba562017-08-07 20:45:20 +020061/*
62 * If we're handed a bigger struct than we know of, ensure all the unknown bits
63 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
64 * we don't know about yet.
65 *
66 * There is a ToCToU between this function call and the following
67 * copy_from_user() call. However, this is not a concern since this function is
68 * meant to be a future-proofing of bits.
69 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070070int bpf_check_uarg_tail_zero(void __user *uaddr,
71 size_t expected_size,
72 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020073{
74 unsigned char __user *addr;
75 unsigned char __user *end;
76 unsigned char val;
77 int err;
78
Mickaël Salaün752ba562017-08-07 20:45:20 +020079 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
80 return -E2BIG;
81
Linus Torvalds96d4f262019-01-03 18:57:57 -080082 if (unlikely(!access_ok(uaddr, actual_size)))
Mickaël Salaün752ba562017-08-07 20:45:20 +020083 return -EFAULT;
84
Mickaël Salaün58291a72017-08-07 20:45:19 +020085 if (actual_size <= expected_size)
86 return 0;
87
88 addr = uaddr + expected_size;
89 end = uaddr + actual_size;
90
91 for (; addr < end; addr++) {
92 err = get_user(val, addr);
93 if (err)
94 return err;
95 if (val)
96 return -E2BIG;
97 }
98
99 return 0;
100}
101
Jakub Kicinskia3884572018-01-11 20:29:09 -0800102const struct bpf_map_ops bpf_map_offload_ops = {
103 .map_alloc = bpf_map_offload_map_alloc,
104 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200105 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800106};
107
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700108static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
109{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800110 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100111 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700112 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800113 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700114
Mark Rutland9ef09e32018-05-03 17:04:59 +0100115 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800116 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100117 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
118 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800119 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200120 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700121
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800122 if (ops->map_alloc_check) {
123 err = ops->map_alloc_check(attr);
124 if (err)
125 return ERR_PTR(err);
126 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800127 if (attr->map_ifindex)
128 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800129 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200130 if (IS_ERR(map))
131 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800132 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100133 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200134 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700135}
136
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700137void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100138{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100139 /* We really just want to fail instead of triggering OOM killer
140 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
141 * which is used for lower order allocation requests.
142 *
143 * It has been observed that higher order allocation requests done by
144 * vmalloc with __GFP_NORETRY being set might fail due to not trying
145 * to reclaim memory from the page cache, thus we set
146 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100147 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100148
149 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100150 void *area;
151
152 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100153 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
154 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100155 if (area != NULL)
156 return area;
157 }
158
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100159 return __vmalloc_node_flags_caller(size, numa_node,
160 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
161 flags, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100162}
163
164void bpf_map_area_free(void *area)
165{
166 kvfree(area);
167}
168
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200169static u32 bpf_map_flags_retain_permanent(u32 flags)
170{
171 /* Some map creation flags are not tied to the map object but
172 * rather to the map fd instead, so they have no meaning upon
173 * map object inspection since multiple file descriptors with
174 * different (access) properties can exist here. Thus, given
175 * this has zero meaning for the map itself, lets clear these
176 * from here.
177 */
178 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
179}
180
Jakub Kicinskibd475642018-01-11 20:29:06 -0800181void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
182{
183 map->map_type = attr->map_type;
184 map->key_size = attr->key_size;
185 map->value_size = attr->value_size;
186 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200187 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800188 map->numa_node = bpf_map_attr_numa_node(attr);
189}
190
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800191int bpf_map_precharge_memlock(u32 pages)
192{
193 struct user_struct *user = get_current_user();
194 unsigned long memlock_limit, cur;
195
196 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
197 cur = atomic_long_read(&user->locked_vm);
198 free_uid(user);
199 if (cur + pages > memlock_limit)
200 return -EPERM;
201 return 0;
202}
203
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700204static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700205{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700206 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700207
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700208 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
209 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700210 return -EPERM;
211 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700212 return 0;
213}
214
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700215static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
216{
217 atomic_long_sub(pages, &user->locked_vm);
218}
219
220static int bpf_map_init_memlock(struct bpf_map *map)
221{
222 struct user_struct *user = get_current_user();
223 int ret;
224
225 ret = bpf_charge_memlock(user, map->pages);
226 if (ret) {
227 free_uid(user);
228 return ret;
229 }
230 map->user = user;
231 return ret;
232}
233
234static void bpf_map_release_memlock(struct bpf_map *map)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700235{
236 struct user_struct *user = map->user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700237 bpf_uncharge_memlock(user, map->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700238 free_uid(user);
239}
240
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700241int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
242{
243 int ret;
244
245 ret = bpf_charge_memlock(map->user, pages);
246 if (ret)
247 return ret;
248 map->pages += pages;
249 return ret;
250}
251
252void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
253{
254 bpf_uncharge_memlock(map->user, pages);
255 map->pages -= pages;
256}
257
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700258static int bpf_map_alloc_id(struct bpf_map *map)
259{
260 int id;
261
Shaohua Lib76354c2018-03-27 11:53:21 -0700262 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700263 spin_lock_bh(&map_idr_lock);
264 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
265 if (id > 0)
266 map->id = id;
267 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700268 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700269
270 if (WARN_ON_ONCE(!id))
271 return -ENOSPC;
272
273 return id > 0 ? 0 : id;
274}
275
Jakub Kicinskia3884572018-01-11 20:29:09 -0800276void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700277{
Eric Dumazet930651a2017-09-19 09:15:59 -0700278 unsigned long flags;
279
Jakub Kicinskia3884572018-01-11 20:29:09 -0800280 /* Offloaded maps are removed from the IDR store when their device
281 * disappears - even if someone holds an fd to them they are unusable,
282 * the memory is gone, all ops will fail; they are simply waiting for
283 * refcnt to drop to be freed.
284 */
285 if (!map->id)
286 return;
287
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700288 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700289 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700290 else
291 __acquire(&map_idr_lock);
292
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700293 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800294 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700295
296 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700297 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700298 else
299 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700300}
301
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700302/* called from workqueue */
303static void bpf_map_free_deferred(struct work_struct *work)
304{
305 struct bpf_map *map = container_of(work, struct bpf_map, work);
306
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700307 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700308 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700309 /* implementation dependent freeing */
310 map->ops->map_free(map);
311}
312
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100313static void bpf_map_put_uref(struct bpf_map *map)
314{
315 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700316 if (map->ops->map_release_uref)
317 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100318 }
319}
320
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700321/* decrement map refcnt and schedule it for freeing via workqueue
322 * (unrelying map implementation ops->map_free() might sleep)
323 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700324static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700325{
326 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700327 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700328 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700329 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700330 INIT_WORK(&map->work, bpf_map_free_deferred);
331 schedule_work(&map->work);
332 }
333}
334
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700335void bpf_map_put(struct bpf_map *map)
336{
337 __bpf_map_put(map, true);
338}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700339EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700340
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100341void bpf_map_put_with_uref(struct bpf_map *map)
342{
343 bpf_map_put_uref(map);
344 bpf_map_put(map);
345}
346
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700347static int bpf_map_release(struct inode *inode, struct file *filp)
348{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200349 struct bpf_map *map = filp->private_data;
350
351 if (map->ops->map_release)
352 map->ops->map_release(map, filp);
353
354 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700355 return 0;
356}
357
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200358static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
359{
360 fmode_t mode = f.file->f_mode;
361
362 /* Our file permissions may have been overridden by global
363 * map permissions facing syscall side.
364 */
365 if (READ_ONCE(map->frozen))
366 mode &= ~FMODE_CAN_WRITE;
367 return mode;
368}
369
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100370#ifdef CONFIG_PROC_FS
371static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
372{
373 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100374 const struct bpf_array *array;
375 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200376 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100377
378 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
379 array = container_of(map, struct bpf_array, map);
380 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200381 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100382 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100383
384 seq_printf(m,
385 "map_type:\t%u\n"
386 "key_size:\t%u\n"
387 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100388 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100389 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200390 "memlock:\t%llu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200391 "map_id:\t%u\n"
392 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100393 map->map_type,
394 map->key_size,
395 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100396 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100397 map->map_flags,
Daniel Borkmann4316b402018-06-02 23:06:34 +0200398 map->pages * 1ULL << PAGE_SHIFT,
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200399 map->id,
400 READ_ONCE(map->frozen));
Daniel Borkmann21116b72016-11-26 01:28:07 +0100401
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200402 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100403 seq_printf(m, "owner_prog_type:\t%u\n",
404 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200405 seq_printf(m, "owner_jited:\t%u\n",
406 owner_jited);
407 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100408}
409#endif
410
Chenbo Feng6e71b042017-10-18 13:00:22 -0700411static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
412 loff_t *ppos)
413{
414 /* We need this handler such that alloc_file() enables
415 * f_mode with FMODE_CAN_READ.
416 */
417 return -EINVAL;
418}
419
420static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
421 size_t siz, loff_t *ppos)
422{
423 /* We need this handler such that alloc_file() enables
424 * f_mode with FMODE_CAN_WRITE.
425 */
426 return -EINVAL;
427}
428
Chenbo Fengf66e4482017-10-18 13:00:26 -0700429const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100430#ifdef CONFIG_PROC_FS
431 .show_fdinfo = bpf_map_show_fdinfo,
432#endif
433 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700434 .read = bpf_dummy_read,
435 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700436};
437
Chenbo Feng6e71b042017-10-18 13:00:22 -0700438int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100439{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700440 int ret;
441
442 ret = security_bpf_map(map, OPEN_FMODE(flags));
443 if (ret < 0)
444 return ret;
445
Daniel Borkmannaa797812015-10-29 14:58:06 +0100446 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700447 flags | O_CLOEXEC);
448}
449
450int bpf_get_file_flag(int flags)
451{
452 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
453 return -EINVAL;
454 if (flags & BPF_F_RDONLY)
455 return O_RDONLY;
456 if (flags & BPF_F_WRONLY)
457 return O_WRONLY;
458 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100459}
460
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700461/* helper macro to check that unused fields 'union bpf_attr' are zero */
462#define CHECK_ATTR(CMD) \
463 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
464 sizeof(attr->CMD##_LAST_FIELD), 0, \
465 sizeof(*attr) - \
466 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
467 sizeof(attr->CMD##_LAST_FIELD)) != NULL
468
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700469/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
470 * Return 0 on success and < 0 on error.
471 */
472static int bpf_obj_name_cpy(char *dst, const char *src)
473{
474 const char *end = src + BPF_OBJ_NAME_LEN;
475
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700476 memset(dst, 0, BPF_OBJ_NAME_LEN);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200477 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700478 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200479 if (!isalnum(*src) &&
480 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700481 return -EINVAL;
482 *dst++ = *src++;
483 }
484
485 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
486 if (src == end)
487 return -EINVAL;
488
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700489 return 0;
490}
491
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200492int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800493 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200494 const struct btf_type *key_type,
495 const struct btf_type *value_type)
496{
497 return -ENOTSUPP;
498}
499
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800500static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200501 u32 btf_key_id, u32 btf_value_id)
502{
503 const struct btf_type *key_type, *value_type;
504 u32 key_size, value_size;
505 int ret = 0;
506
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200507 /* Some maps allow key to be unspecified. */
508 if (btf_key_id) {
509 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
510 if (!key_type || key_size != map->key_size)
511 return -EINVAL;
512 } else {
513 key_type = btf_type_by_id(btf, 0);
514 if (!map->ops->map_check_btf)
515 return -EINVAL;
516 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200517
518 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
519 if (!value_type || value_size != map->value_size)
520 return -EINVAL;
521
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800522 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
523
524 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200525 if (map->map_flags & BPF_F_RDONLY_PROG)
526 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800527 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800528 map->map_type != BPF_MAP_TYPE_ARRAY &&
529 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800530 return -ENOTSUPP;
531 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
532 map->value_size) {
533 WARN_ONCE(1,
534 "verifier bug spin_lock_off %d value_size %d\n",
535 map->spin_lock_off, map->value_size);
536 return -EFAULT;
537 }
538 }
539
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200540 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800541 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200542
543 return ret;
544}
545
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700546#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700547/* called via syscall */
548static int map_create(union bpf_attr *attr)
549{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700550 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700551 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700552 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700553 int err;
554
555 err = CHECK_ATTR(BPF_MAP_CREATE);
556 if (err)
557 return -EINVAL;
558
Chenbo Feng6e71b042017-10-18 13:00:22 -0700559 f_flags = bpf_get_file_flag(attr->map_flags);
560 if (f_flags < 0)
561 return f_flags;
562
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700563 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700564 ((unsigned int)numa_node >= nr_node_ids ||
565 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700566 return -EINVAL;
567
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700568 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
569 map = find_and_alloc_map(attr);
570 if (IS_ERR(map))
571 return PTR_ERR(map);
572
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700573 err = bpf_obj_name_cpy(map->name, attr->map_name);
574 if (err)
575 goto free_map_nouncharge;
576
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700577 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100578 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700579
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200580 if (attr->btf_key_type_id || attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700581 struct btf *btf;
582
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200583 if (!attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700584 err = -EINVAL;
585 goto free_map_nouncharge;
586 }
587
588 btf = btf_get_by_fd(attr->btf_fd);
589 if (IS_ERR(btf)) {
590 err = PTR_ERR(btf);
591 goto free_map_nouncharge;
592 }
593
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200594 err = map_check_btf(map, btf, attr->btf_key_type_id,
595 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700596 if (err) {
597 btf_put(btf);
598 goto free_map_nouncharge;
599 }
600
601 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700602 map->btf_key_type_id = attr->btf_key_type_id;
603 map->btf_value_type_id = attr->btf_value_type_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800604 } else {
605 map->spin_lock_off = -EINVAL;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700606 }
607
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700608 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700609 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100610 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700611
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700612 err = bpf_map_init_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700613 if (err)
614 goto free_map_sec;
615
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700616 err = bpf_map_alloc_id(map);
617 if (err)
618 goto free_map;
619
Chenbo Feng6e71b042017-10-18 13:00:22 -0700620 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700621 if (err < 0) {
622 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800623 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700624 * bpf_map_alloc_id() has published the map
625 * to the userspace and the userspace may
626 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
627 */
Peng Sun352d20d2019-02-27 22:36:25 +0800628 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700629 return err;
630 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700631
632 return err;
633
634free_map:
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700635 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700636free_map_sec:
637 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100638free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700639 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700640 map->ops->map_free(map);
641 return err;
642}
643
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700644/* if error is returned, fd is released.
645 * On success caller should complete fd access with matching fdput()
646 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100647struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700648{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700649 if (!f.file)
650 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700651 if (f.file->f_op != &bpf_map_fops) {
652 fdput(f);
653 return ERR_PTR(-EINVAL);
654 }
655
Daniel Borkmannc2101292015-10-29 14:58:07 +0100656 return f.file->private_data;
657}
658
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700659/* prog's and map's refcnt limit */
660#define BPF_MAX_REFCNT 32768
661
662struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100663{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700664 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
665 atomic_dec(&map->refcnt);
666 return ERR_PTR(-EBUSY);
667 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100668 if (uref)
669 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700670 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100671}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700672EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100673
674struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100675{
676 struct fd f = fdget(ufd);
677 struct bpf_map *map;
678
679 map = __bpf_map_get(f);
680 if (IS_ERR(map))
681 return map;
682
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700683 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100684 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700685
686 return map;
687}
688
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700689/* map_idr_lock should have been held */
690static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
691 bool uref)
692{
693 int refold;
694
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100695 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700696
697 if (refold >= BPF_MAX_REFCNT) {
698 __bpf_map_put(map, false);
699 return ERR_PTR(-EBUSY);
700 }
701
702 if (!refold)
703 return ERR_PTR(-ENOENT);
704
705 if (uref)
706 atomic_inc(&map->usercnt);
707
708 return map;
709}
710
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800711int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
712{
713 return -ENOTSUPP;
714}
715
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200716static void *__bpf_copy_key(void __user *ukey, u64 key_size)
717{
718 if (key_size)
719 return memdup_user(ukey, key_size);
720
721 if (ukey)
722 return ERR_PTR(-EINVAL);
723
724 return NULL;
725}
726
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700727/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800728#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700729
730static int map_lookup_elem(union bpf_attr *attr)
731{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100732 void __user *ukey = u64_to_user_ptr(attr->key);
733 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700734 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700735 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800736 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800737 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200738 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700739 int err;
740
741 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
742 return -EINVAL;
743
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800744 if (attr->flags & ~BPF_F_LOCK)
745 return -EINVAL;
746
Daniel Borkmann592867b2015-09-08 18:00:09 +0200747 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100748 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700749 if (IS_ERR(map))
750 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200751 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700752 err = -EPERM;
753 goto err_put;
754 }
755
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800756 if ((attr->flags & BPF_F_LOCK) &&
757 !map_value_has_spin_lock(map)) {
758 err = -EINVAL;
759 goto err_put;
760 }
761
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200762 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400763 if (IS_ERR(key)) {
764 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700765 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400766 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700767
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800768 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800769 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000770 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
771 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800772 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700773 else if (IS_FD_MAP(map))
774 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800775 else
776 value_size = map->value_size;
777
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800778 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800779 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700780 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800781 goto free_key;
782
Jakub Kicinskia3884572018-01-11 20:29:09 -0800783 if (bpf_map_is_dev_bound(map)) {
784 err = bpf_map_offload_lookup_elem(map, key, value);
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800785 goto done;
786 }
787
788 preempt_disable();
789 this_cpu_inc(bpf_prog_active);
790 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
791 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800792 err = bpf_percpu_hash_copy(map, key, value);
793 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
794 err = bpf_percpu_array_copy(map, key, value);
Roman Gushchinb741f162018-09-28 14:45:43 +0000795 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
796 err = bpf_percpu_cgroup_storage_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800797 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
798 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700799 } else if (IS_FD_ARRAY(map)) {
800 err = bpf_fd_array_map_lookup_elem(map, key, value);
801 } else if (IS_FD_HASH(map)) {
802 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700803 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
804 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200805 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
806 map->map_type == BPF_MAP_TYPE_STACK) {
807 err = map->ops->map_peek_elem(map, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800808 } else {
809 rcu_read_lock();
810 ptr = map->ops->map_lookup_elem(map, key);
Prashant Bhole509db282018-10-09 10:04:49 +0900811 if (IS_ERR(ptr)) {
812 err = PTR_ERR(ptr);
813 } else if (!ptr) {
814 err = -ENOENT;
815 } else {
816 err = 0;
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800817 if (attr->flags & BPF_F_LOCK)
818 /* lock 'ptr' and copy everything but lock */
819 copy_map_value_locked(map, value, ptr, true);
820 else
821 copy_map_value(map, value, ptr);
822 /* mask lock, since value wasn't zero inited */
823 check_and_init_map_lock(map, value);
Prashant Bhole509db282018-10-09 10:04:49 +0900824 }
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800825 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800826 }
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800827 this_cpu_dec(bpf_prog_active);
828 preempt_enable();
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800829
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800830done:
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800831 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800832 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700833
834 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800835 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800836 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700837
838 err = 0;
839
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800840free_value:
841 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700842free_key:
843 kfree(key);
844err_put:
845 fdput(f);
846 return err;
847}
848
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700849static void maybe_wait_bpf_programs(struct bpf_map *map)
850{
851 /* Wait for any running BPF programs to complete so that
852 * userspace, when we return to it, knows that all programs
853 * that could be running use the new map value.
854 */
855 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
856 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
857 synchronize_rcu();
858}
859
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800860#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700861
862static int map_update_elem(union bpf_attr *attr)
863{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100864 void __user *ukey = u64_to_user_ptr(attr->key);
865 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700866 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700867 struct bpf_map *map;
868 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800869 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200870 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700871 int err;
872
873 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
874 return -EINVAL;
875
Daniel Borkmann592867b2015-09-08 18:00:09 +0200876 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100877 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700878 if (IS_ERR(map))
879 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200880 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700881 err = -EPERM;
882 goto err_put;
883 }
884
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800885 if ((attr->flags & BPF_F_LOCK) &&
886 !map_value_has_spin_lock(map)) {
887 err = -EINVAL;
888 goto err_put;
889 }
890
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200891 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400892 if (IS_ERR(key)) {
893 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700894 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400895 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700896
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800897 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800898 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000899 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
900 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800901 value_size = round_up(map->value_size, 8) * num_possible_cpus();
902 else
903 value_size = map->value_size;
904
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700905 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800906 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700907 if (!value)
908 goto free_key;
909
910 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800911 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700912 goto free_value;
913
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200914 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800915 if (bpf_map_is_dev_bound(map)) {
916 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
917 goto out;
John Fastabend99ba2b52018-07-05 08:50:04 -0700918 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
919 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
920 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200921 err = map->ops->map_update_elem(map, key, value, attr->flags);
922 goto out;
923 }
924
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800925 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
926 * inside bpf map update or delete otherwise deadlocks are possible
927 */
928 preempt_disable();
929 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800930 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
931 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800932 err = bpf_percpu_hash_update(map, key, value, attr->flags);
933 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
934 err = bpf_percpu_array_update(map, key, value, attr->flags);
Roman Gushchinb741f162018-09-28 14:45:43 +0000935 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
936 err = bpf_percpu_cgroup_storage_update(map, key, value,
937 attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100938 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200939 rcu_read_lock();
940 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
941 attr->flags);
942 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700943 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
944 rcu_read_lock();
945 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
946 attr->flags);
947 rcu_read_unlock();
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700948 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
949 /* rcu_read_lock() is not needed */
950 err = bpf_fd_reuseport_array_update_elem(map, key, value,
951 attr->flags);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200952 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
953 map->map_type == BPF_MAP_TYPE_STACK) {
954 err = map->ops->map_push_elem(map, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800955 } else {
956 rcu_read_lock();
957 err = map->ops->map_update_elem(map, key, value, attr->flags);
958 rcu_read_unlock();
959 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800960 __this_cpu_dec(bpf_prog_active);
961 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700962 maybe_wait_bpf_programs(map);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200963out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700964free_value:
965 kfree(value);
966free_key:
967 kfree(key);
968err_put:
969 fdput(f);
970 return err;
971}
972
973#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
974
975static int map_delete_elem(union bpf_attr *attr)
976{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100977 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700978 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700979 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200980 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700981 void *key;
982 int err;
983
984 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
985 return -EINVAL;
986
Daniel Borkmann592867b2015-09-08 18:00:09 +0200987 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100988 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700989 if (IS_ERR(map))
990 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200991 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700992 err = -EPERM;
993 goto err_put;
994 }
995
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200996 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400997 if (IS_ERR(key)) {
998 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700999 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001000 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001001
Jakub Kicinskia3884572018-01-11 20:29:09 -08001002 if (bpf_map_is_dev_bound(map)) {
1003 err = bpf_map_offload_delete_elem(map, key);
1004 goto out;
1005 }
1006
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001007 preempt_disable();
1008 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001009 rcu_read_lock();
1010 err = map->ops->map_delete_elem(map, key);
1011 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001012 __this_cpu_dec(bpf_prog_active);
1013 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001014 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001015out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001016 kfree(key);
1017err_put:
1018 fdput(f);
1019 return err;
1020}
1021
1022/* last field in 'union bpf_attr' used by this command */
1023#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1024
1025static int map_get_next_key(union bpf_attr *attr)
1026{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001027 void __user *ukey = u64_to_user_ptr(attr->key);
1028 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001029 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001030 struct bpf_map *map;
1031 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001032 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001033 int err;
1034
1035 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1036 return -EINVAL;
1037
Daniel Borkmann592867b2015-09-08 18:00:09 +02001038 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001039 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001040 if (IS_ERR(map))
1041 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001042 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001043 err = -EPERM;
1044 goto err_put;
1045 }
1046
Teng Qin8fe45922017-04-24 19:00:37 -07001047 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001048 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001049 if (IS_ERR(key)) {
1050 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001051 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001052 }
Teng Qin8fe45922017-04-24 19:00:37 -07001053 } else {
1054 key = NULL;
1055 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001056
1057 err = -ENOMEM;
1058 next_key = kmalloc(map->key_size, GFP_USER);
1059 if (!next_key)
1060 goto free_key;
1061
Jakub Kicinskia3884572018-01-11 20:29:09 -08001062 if (bpf_map_is_dev_bound(map)) {
1063 err = bpf_map_offload_get_next_key(map, key, next_key);
1064 goto out;
1065 }
1066
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001067 rcu_read_lock();
1068 err = map->ops->map_get_next_key(map, key, next_key);
1069 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001070out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001071 if (err)
1072 goto free_next_key;
1073
1074 err = -EFAULT;
1075 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1076 goto free_next_key;
1077
1078 err = 0;
1079
1080free_next_key:
1081 kfree(next_key);
1082free_key:
1083 kfree(key);
1084err_put:
1085 fdput(f);
1086 return err;
1087}
1088
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001089#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1090
1091static int map_lookup_and_delete_elem(union bpf_attr *attr)
1092{
1093 void __user *ukey = u64_to_user_ptr(attr->key);
1094 void __user *uvalue = u64_to_user_ptr(attr->value);
1095 int ufd = attr->map_fd;
1096 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001097 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001098 u32 value_size;
1099 struct fd f;
1100 int err;
1101
1102 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1103 return -EINVAL;
1104
1105 f = fdget(ufd);
1106 map = __bpf_map_get(f);
1107 if (IS_ERR(map))
1108 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001109 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001110 err = -EPERM;
1111 goto err_put;
1112 }
1113
1114 key = __bpf_copy_key(ukey, map->key_size);
1115 if (IS_ERR(key)) {
1116 err = PTR_ERR(key);
1117 goto err_put;
1118 }
1119
1120 value_size = map->value_size;
1121
1122 err = -ENOMEM;
1123 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1124 if (!value)
1125 goto free_key;
1126
1127 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1128 map->map_type == BPF_MAP_TYPE_STACK) {
1129 err = map->ops->map_pop_elem(map, value);
1130 } else {
1131 err = -ENOTSUPP;
1132 }
1133
1134 if (err)
1135 goto free_value;
1136
1137 if (copy_to_user(uvalue, value, value_size) != 0)
1138 goto free_value;
1139
1140 err = 0;
1141
1142free_value:
1143 kfree(value);
1144free_key:
1145 kfree(key);
1146err_put:
1147 fdput(f);
1148 return err;
1149}
1150
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001151#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1152
1153static int map_freeze(const union bpf_attr *attr)
1154{
1155 int err = 0, ufd = attr->map_fd;
1156 struct bpf_map *map;
1157 struct fd f;
1158
1159 if (CHECK_ATTR(BPF_MAP_FREEZE))
1160 return -EINVAL;
1161
1162 f = fdget(ufd);
1163 map = __bpf_map_get(f);
1164 if (IS_ERR(map))
1165 return PTR_ERR(map);
1166 if (READ_ONCE(map->frozen)) {
1167 err = -EBUSY;
1168 goto err_put;
1169 }
1170 if (!capable(CAP_SYS_ADMIN)) {
1171 err = -EPERM;
1172 goto err_put;
1173 }
1174
1175 WRITE_ONCE(map->frozen, true);
1176err_put:
1177 fdput(f);
1178 return err;
1179}
1180
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001181static const struct bpf_prog_ops * const bpf_prog_types[] = {
1182#define BPF_PROG_TYPE(_id, _name) \
1183 [_id] = & _name ## _prog_ops,
1184#define BPF_MAP_TYPE(_id, _ops)
1185#include <linux/bpf_types.h>
1186#undef BPF_PROG_TYPE
1187#undef BPF_MAP_TYPE
1188};
1189
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001190static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1191{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001192 const struct bpf_prog_ops *ops;
1193
1194 if (type >= ARRAY_SIZE(bpf_prog_types))
1195 return -EINVAL;
1196 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1197 ops = bpf_prog_types[type];
1198 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001199 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001200
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001201 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001202 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001203 else
1204 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001205 prog->type = type;
1206 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001207}
1208
1209/* drop refcnt on maps used by eBPF program and free auxilary data */
1210static void free_used_maps(struct bpf_prog_aux *aux)
1211{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001212 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001213 int i;
1214
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001215 for_each_cgroup_storage_type(stype) {
1216 if (!aux->cgroup_storage[stype])
1217 continue;
1218 bpf_cgroup_storage_release(aux->prog,
1219 aux->cgroup_storage[stype]);
1220 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07001221
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001222 for (i = 0; i < aux->used_map_cnt; i++)
1223 bpf_map_put(aux->used_maps[i]);
1224
1225 kfree(aux->used_maps);
1226}
1227
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001228int __bpf_prog_charge(struct user_struct *user, u32 pages)
1229{
1230 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1231 unsigned long user_bufs;
1232
1233 if (user) {
1234 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1235 if (user_bufs > memlock_limit) {
1236 atomic_long_sub(pages, &user->locked_vm);
1237 return -EPERM;
1238 }
1239 }
1240
1241 return 0;
1242}
1243
1244void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1245{
1246 if (user)
1247 atomic_long_sub(pages, &user->locked_vm);
1248}
1249
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001250static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1251{
1252 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001253 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001254
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001255 ret = __bpf_prog_charge(user, prog->pages);
1256 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001257 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001258 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001259 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001260
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001261 prog->aux->user = user;
1262 return 0;
1263}
1264
1265static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1266{
1267 struct user_struct *user = prog->aux->user;
1268
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001269 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001270 free_uid(user);
1271}
1272
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001273static int bpf_prog_alloc_id(struct bpf_prog *prog)
1274{
1275 int id;
1276
Shaohua Lib76354c2018-03-27 11:53:21 -07001277 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001278 spin_lock_bh(&prog_idr_lock);
1279 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1280 if (id > 0)
1281 prog->aux->id = id;
1282 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001283 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001284
1285 /* id is in [1, INT_MAX) */
1286 if (WARN_ON_ONCE(!id))
1287 return -ENOSPC;
1288
1289 return id > 0 ? 0 : id;
1290}
1291
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001292void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001293{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001294 /* cBPF to eBPF migrations are currently not in the idr store.
1295 * Offloaded programs are removed from the store when their device
1296 * disappears - even if someone grabs an fd to them they are unusable,
1297 * simply waiting for refcnt to drop to be freed.
1298 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001299 if (!prog->aux->id)
1300 return;
1301
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001302 if (do_idr_lock)
1303 spin_lock_bh(&prog_idr_lock);
1304 else
1305 __acquire(&prog_idr_lock);
1306
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001307 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001308 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001309
1310 if (do_idr_lock)
1311 spin_unlock_bh(&prog_idr_lock);
1312 else
1313 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001314}
1315
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001316static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001317{
1318 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1319
1320 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001321 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001322 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001323 bpf_prog_free(aux->prog);
1324}
1325
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001326static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001327{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001328 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001329 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001330 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001331 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001332 bpf_prog_kallsyms_del_all(prog);
Yonghong Song838e9692018-11-19 15:29:11 -08001333 btf_put(prog->aux->btf);
Yonghong Songba64e7d2018-11-24 23:20:44 -08001334 kvfree(prog->aux->func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001335 bpf_prog_free_linfo(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001336
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001337 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001338 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001339}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001340
1341void bpf_prog_put(struct bpf_prog *prog)
1342{
1343 __bpf_prog_put(prog, true);
1344}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001345EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001346
1347static int bpf_prog_release(struct inode *inode, struct file *filp)
1348{
1349 struct bpf_prog *prog = filp->private_data;
1350
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001351 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001352 return 0;
1353}
1354
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001355static void bpf_prog_get_stats(const struct bpf_prog *prog,
1356 struct bpf_prog_stats *stats)
1357{
1358 u64 nsecs = 0, cnt = 0;
1359 int cpu;
1360
1361 for_each_possible_cpu(cpu) {
1362 const struct bpf_prog_stats *st;
1363 unsigned int start;
1364 u64 tnsecs, tcnt;
1365
1366 st = per_cpu_ptr(prog->aux->stats, cpu);
1367 do {
1368 start = u64_stats_fetch_begin_irq(&st->syncp);
1369 tnsecs = st->nsecs;
1370 tcnt = st->cnt;
1371 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1372 nsecs += tnsecs;
1373 cnt += tcnt;
1374 }
1375 stats->nsecs = nsecs;
1376 stats->cnt = cnt;
1377}
1378
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001379#ifdef CONFIG_PROC_FS
1380static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1381{
1382 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001383 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001384 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001385
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001386 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001387 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001388 seq_printf(m,
1389 "prog_type:\t%u\n"
1390 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001391 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001392 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001393 "prog_id:\t%u\n"
1394 "run_time_ns:\t%llu\n"
1395 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001396 prog->type,
1397 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001398 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001399 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001400 prog->aux->id,
1401 stats.nsecs,
1402 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001403}
1404#endif
1405
Chenbo Fengf66e4482017-10-18 13:00:26 -07001406const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001407#ifdef CONFIG_PROC_FS
1408 .show_fdinfo = bpf_prog_show_fdinfo,
1409#endif
1410 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001411 .read = bpf_dummy_read,
1412 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001413};
1414
Daniel Borkmannb2197752015-10-29 14:58:09 +01001415int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001416{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001417 int ret;
1418
1419 ret = security_bpf_prog(prog);
1420 if (ret < 0)
1421 return ret;
1422
Daniel Borkmannaa797812015-10-29 14:58:06 +01001423 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1424 O_RDWR | O_CLOEXEC);
1425}
1426
Daniel Borkmann113214b2016-06-30 17:24:44 +02001427static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001428{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001429 if (!f.file)
1430 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001431 if (f.file->f_op != &bpf_prog_fops) {
1432 fdput(f);
1433 return ERR_PTR(-EINVAL);
1434 }
1435
Daniel Borkmannc2101292015-10-29 14:58:07 +01001436 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001437}
1438
Brenden Blanco59d36562016-07-19 12:16:46 -07001439struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001440{
Brenden Blanco59d36562016-07-19 12:16:46 -07001441 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1442 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001443 return ERR_PTR(-EBUSY);
1444 }
1445 return prog;
1446}
Brenden Blanco59d36562016-07-19 12:16:46 -07001447EXPORT_SYMBOL_GPL(bpf_prog_add);
1448
Daniel Borkmannc5405942016-11-09 22:02:34 +01001449void bpf_prog_sub(struct bpf_prog *prog, int i)
1450{
1451 /* Only to be used for undoing previous bpf_prog_add() in some
1452 * error path. We still know that another entity in our call
1453 * path holds a reference to the program, thus atomic_sub() can
1454 * be safely used in such cases!
1455 */
1456 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1457}
1458EXPORT_SYMBOL_GPL(bpf_prog_sub);
1459
Brenden Blanco59d36562016-07-19 12:16:46 -07001460struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1461{
1462 return bpf_prog_add(prog, 1);
1463}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001464EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001465
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001466/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001467struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001468{
1469 int refold;
1470
Mark Rutlandbfc18e32018-06-21 13:13:04 +01001471 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001472
1473 if (refold >= BPF_MAX_REFCNT) {
1474 __bpf_prog_put(prog, false);
1475 return ERR_PTR(-EBUSY);
1476 }
1477
1478 if (!refold)
1479 return ERR_PTR(-ENOENT);
1480
1481 return prog;
1482}
John Fastabenda6f6df62017-08-15 22:32:22 -07001483EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001484
Al Viro040ee692017-12-02 20:20:38 -05001485bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001486 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001487{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001488 /* not an attachment, just a refcount inc, always allow */
1489 if (!attach_type)
1490 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001491
1492 if (prog->type != *attach_type)
1493 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001494 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001495 return false;
1496
1497 return true;
1498}
1499
1500static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001501 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001502{
1503 struct fd f = fdget(ufd);
1504 struct bpf_prog *prog;
1505
Daniel Borkmann113214b2016-06-30 17:24:44 +02001506 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001507 if (IS_ERR(prog))
1508 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001509 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001510 prog = ERR_PTR(-EINVAL);
1511 goto out;
1512 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001513
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001514 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001515out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001516 fdput(f);
1517 return prog;
1518}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001519
1520struct bpf_prog *bpf_prog_get(u32 ufd)
1521{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001522 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001523}
1524
Jakub Kicinski248f3462017-11-03 13:56:20 -07001525struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001526 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001527{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001528 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001529}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001530EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001531
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001532/* Initially all BPF programs could be loaded w/o specifying
1533 * expected_attach_type. Later for some of them specifying expected_attach_type
1534 * at load time became required so that program could be validated properly.
1535 * Programs of types that are allowed to be loaded both w/ and w/o (for
1536 * backward compatibility) expected_attach_type, should have the default attach
1537 * type assigned to expected_attach_type for the latter case, so that it can be
1538 * validated later at attach time.
1539 *
1540 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1541 * prog type requires it but has some attach types that have to be backward
1542 * compatible.
1543 */
1544static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1545{
1546 switch (attr->prog_type) {
1547 case BPF_PROG_TYPE_CGROUP_SOCK:
1548 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1549 * exist so checking for non-zero is the way to go here.
1550 */
1551 if (!attr->expected_attach_type)
1552 attr->expected_attach_type =
1553 BPF_CGROUP_INET_SOCK_CREATE;
1554 break;
1555 }
1556}
1557
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001558static int
1559bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1560 enum bpf_attach_type expected_attach_type)
1561{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001562 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001563 case BPF_PROG_TYPE_CGROUP_SOCK:
1564 switch (expected_attach_type) {
1565 case BPF_CGROUP_INET_SOCK_CREATE:
1566 case BPF_CGROUP_INET4_POST_BIND:
1567 case BPF_CGROUP_INET6_POST_BIND:
1568 return 0;
1569 default:
1570 return -EINVAL;
1571 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001572 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1573 switch (expected_attach_type) {
1574 case BPF_CGROUP_INET4_BIND:
1575 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001576 case BPF_CGROUP_INET4_CONNECT:
1577 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001578 case BPF_CGROUP_UDP4_SENDMSG:
1579 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001580 return 0;
1581 default:
1582 return -EINVAL;
1583 }
1584 default:
1585 return 0;
1586 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001587}
1588
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001589/* last field in 'union bpf_attr' used by this command */
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001590#define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001591
Yonghong Song838e9692018-11-19 15:29:11 -08001592static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001593{
1594 enum bpf_prog_type type = attr->prog_type;
1595 struct bpf_prog *prog;
1596 int err;
1597 char license[128];
1598 bool is_gpl;
1599
1600 if (CHECK_ATTR(BPF_PROG_LOAD))
1601 return -EINVAL;
1602
David Millere9ee9ef2018-11-30 21:08:14 -08001603 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT))
David S. Millere07b98d2017-05-10 11:38:07 -07001604 return -EINVAL;
1605
David Millere9ee9ef2018-11-30 21:08:14 -08001606 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1607 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1608 !capable(CAP_SYS_ADMIN))
1609 return -EPERM;
1610
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001611 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001612 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001613 sizeof(license) - 1) < 0)
1614 return -EFAULT;
1615 license[sizeof(license) - 1] = 0;
1616
1617 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1618 is_gpl = license_is_gpl_compatible(license);
1619
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07001620 if (attr->insn_cnt == 0 ||
1621 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001622 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07001623 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1624 type != BPF_PROG_TYPE_CGROUP_SKB &&
1625 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001626 return -EPERM;
1627
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001628 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001629 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1630 return -EINVAL;
1631
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001632 /* plain bpf_prog allocation */
1633 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1634 if (!prog)
1635 return -ENOMEM;
1636
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001637 prog->expected_attach_type = attr->expected_attach_type;
1638
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001639 prog->aux->offload_requested = !!attr->prog_ifindex;
1640
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001641 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001642 if (err)
1643 goto free_prog_nouncharge;
1644
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001645 err = bpf_prog_charge_memlock(prog);
1646 if (err)
1647 goto free_prog_sec;
1648
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001649 prog->len = attr->insn_cnt;
1650
1651 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001652 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001653 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001654 goto free_prog;
1655
1656 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001657 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001658
1659 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001660 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001661
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001662 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001663 err = bpf_prog_offload_init(prog, attr);
1664 if (err)
1665 goto free_prog;
1666 }
1667
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001668 /* find program type: socket_filter vs tracing_filter */
1669 err = find_prog_type(type, prog);
1670 if (err < 0)
1671 goto free_prog;
1672
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001673 prog->aux->load_time = ktime_get_boot_ns();
1674 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1675 if (err)
1676 goto free_prog;
1677
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001678 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08001679 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001680 if (err < 0)
1681 goto free_used_maps;
1682
Daniel Borkmann9facc332018-06-15 02:30:48 +02001683 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001684 if (err < 0)
1685 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001686
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001687 err = bpf_prog_alloc_id(prog);
1688 if (err)
1689 goto free_used_maps;
1690
Daniel Borkmannaa797812015-10-29 14:58:06 +01001691 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001692 if (err < 0) {
1693 /* failed to allocate fd.
1694 * bpf_prog_put() is needed because the above
1695 * bpf_prog_alloc_id() has published the prog
1696 * to the userspace and the userspace may
1697 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1698 */
1699 bpf_prog_put(prog);
1700 return err;
1701 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001702
Daniel Borkmann74451e662017-02-16 22:24:50 +01001703 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08001704 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001705 return err;
1706
1707free_used_maps:
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001708 bpf_prog_free_linfo(prog);
Martin KaFai Lau5482e9a2018-12-01 17:08:44 -08001709 kvfree(prog->aux->func_info);
1710 btf_put(prog->aux->btf);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001711 bpf_prog_kallsyms_del_subprogs(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001712 free_used_maps(prog->aux);
1713free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001714 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001715free_prog_sec:
1716 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001717free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001718 bpf_prog_free(prog);
1719 return err;
1720}
1721
Chenbo Feng6e71b042017-10-18 13:00:22 -07001722#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001723
1724static int bpf_obj_pin(const union bpf_attr *attr)
1725{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001726 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001727 return -EINVAL;
1728
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001729 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001730}
1731
1732static int bpf_obj_get(const union bpf_attr *attr)
1733{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001734 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1735 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001736 return -EINVAL;
1737
Chenbo Feng6e71b042017-10-18 13:00:22 -07001738 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1739 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001740}
1741
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001742struct bpf_raw_tracepoint {
1743 struct bpf_raw_event_map *btp;
1744 struct bpf_prog *prog;
1745};
1746
1747static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1748{
1749 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1750
1751 if (raw_tp->prog) {
1752 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1753 bpf_prog_put(raw_tp->prog);
1754 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001755 bpf_put_raw_tracepoint(raw_tp->btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001756 kfree(raw_tp);
1757 return 0;
1758}
1759
1760static const struct file_operations bpf_raw_tp_fops = {
1761 .release = bpf_raw_tracepoint_release,
1762 .read = bpf_dummy_read,
1763 .write = bpf_dummy_write,
1764};
1765
1766#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1767
1768static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1769{
1770 struct bpf_raw_tracepoint *raw_tp;
1771 struct bpf_raw_event_map *btp;
1772 struct bpf_prog *prog;
1773 char tp_name[128];
1774 int tp_fd, err;
1775
1776 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1777 sizeof(tp_name) - 1) < 0)
1778 return -EFAULT;
1779 tp_name[sizeof(tp_name) - 1] = 0;
1780
Matt Mullinsa38d1102018-12-12 16:42:37 -08001781 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001782 if (!btp)
1783 return -ENOENT;
1784
1785 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001786 if (!raw_tp) {
1787 err = -ENOMEM;
1788 goto out_put_btp;
1789 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001790 raw_tp->btp = btp;
1791
1792 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1793 BPF_PROG_TYPE_RAW_TRACEPOINT);
1794 if (IS_ERR(prog)) {
1795 err = PTR_ERR(prog);
1796 goto out_free_tp;
1797 }
1798
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;
Daniel Mackf4324552016-11-23 16:52:27 +01001891 default:
1892 return -EINVAL;
1893 }
1894
David Ahernb2cd1252016-12-01 08:48:03 -08001895 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1896 if (IS_ERR(prog))
1897 return PTR_ERR(prog);
1898
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001899 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1900 bpf_prog_put(prog);
1901 return -EINVAL;
1902 }
1903
Sean Youngfdb5c452018-06-19 00:04:24 +01001904 switch (ptype) {
1905 case BPF_PROG_TYPE_SK_SKB:
1906 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001907 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01001908 break;
1909 case BPF_PROG_TYPE_LIRC_MODE2:
1910 ret = lirc_prog_attach(attr, prog);
1911 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001912 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1913 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1914 break;
Sean Youngfdb5c452018-06-19 00:04:24 +01001915 default:
1916 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001917 }
1918
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001919 if (ret)
1920 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001921 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001922}
1923
1924#define BPF_PROG_DETACH_LAST_FIELD attach_type
1925
1926static int bpf_prog_detach(const union bpf_attr *attr)
1927{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001928 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001929
1930 if (!capable(CAP_NET_ADMIN))
1931 return -EPERM;
1932
1933 if (CHECK_ATTR(BPF_PROG_DETACH))
1934 return -EINVAL;
1935
1936 switch (attr->attach_type) {
1937 case BPF_CGROUP_INET_INGRESS:
1938 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001939 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1940 break;
David Ahern610236582016-12-01 08:48:04 -08001941 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001942 case BPF_CGROUP_INET4_POST_BIND:
1943 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001944 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1945 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001946 case BPF_CGROUP_INET4_BIND:
1947 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001948 case BPF_CGROUP_INET4_CONNECT:
1949 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001950 case BPF_CGROUP_UDP4_SENDMSG:
1951 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001952 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1953 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001954 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001955 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001956 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001957 case BPF_CGROUP_DEVICE:
1958 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1959 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001960 case BPF_SK_MSG_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001961 return sock_map_get_from_fd(attr, NULL);
John Fastabend5a67da22017-09-08 14:00:49 -07001962 case BPF_SK_SKB_STREAM_PARSER:
1963 case BPF_SK_SKB_STREAM_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001964 return sock_map_get_from_fd(attr, NULL);
Sean Youngf4364dc2018-05-27 12:24:09 +01001965 case BPF_LIRC_MODE2:
1966 return lirc_prog_detach(attr);
Petar Penkovd58e4682018-09-14 07:46:18 -07001967 case BPF_FLOW_DISSECTOR:
1968 return skb_flow_dissector_bpf_prog_detach(attr);
Daniel Mackf4324552016-11-23 16:52:27 +01001969 default:
1970 return -EINVAL;
1971 }
1972
Sean Youngfdb5c452018-06-19 00:04:24 +01001973 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01001974}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001975
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001976#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1977
1978static int bpf_prog_query(const union bpf_attr *attr,
1979 union bpf_attr __user *uattr)
1980{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001981 if (!capable(CAP_NET_ADMIN))
1982 return -EPERM;
1983 if (CHECK_ATTR(BPF_PROG_QUERY))
1984 return -EINVAL;
1985 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1986 return -EINVAL;
1987
1988 switch (attr->query.attach_type) {
1989 case BPF_CGROUP_INET_INGRESS:
1990 case BPF_CGROUP_INET_EGRESS:
1991 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001992 case BPF_CGROUP_INET4_BIND:
1993 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001994 case BPF_CGROUP_INET4_POST_BIND:
1995 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001996 case BPF_CGROUP_INET4_CONNECT:
1997 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001998 case BPF_CGROUP_UDP4_SENDMSG:
1999 case BPF_CGROUP_UDP6_SENDMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002000 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05002001 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002002 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01002003 case BPF_LIRC_MODE2:
2004 return lirc_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002005 default:
2006 return -EINVAL;
2007 }
Sean Youngfdb5c452018-06-19 00:04:24 +01002008
2009 return cgroup_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002010}
Daniel Mackf4324552016-11-23 16:52:27 +01002011
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002012#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002013
2014static int bpf_prog_test_run(const union bpf_attr *attr,
2015 union bpf_attr __user *uattr)
2016{
2017 struct bpf_prog *prog;
2018 int ret = -ENOTSUPP;
2019
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08002020 if (!capable(CAP_SYS_ADMIN))
2021 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002022 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2023 return -EINVAL;
2024
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002025 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2026 (!attr->test.ctx_size_in && attr->test.ctx_in))
2027 return -EINVAL;
2028
2029 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2030 (!attr->test.ctx_size_out && attr->test.ctx_out))
2031 return -EINVAL;
2032
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002033 prog = bpf_prog_get(attr->test.prog_fd);
2034 if (IS_ERR(prog))
2035 return PTR_ERR(prog);
2036
2037 if (prog->aux->ops->test_run)
2038 ret = prog->aux->ops->test_run(prog, attr, uattr);
2039
2040 bpf_prog_put(prog);
2041 return ret;
2042}
2043
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002044#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2045
2046static int bpf_obj_get_next_id(const union bpf_attr *attr,
2047 union bpf_attr __user *uattr,
2048 struct idr *idr,
2049 spinlock_t *lock)
2050{
2051 u32 next_id = attr->start_id;
2052 int err = 0;
2053
2054 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2055 return -EINVAL;
2056
2057 if (!capable(CAP_SYS_ADMIN))
2058 return -EPERM;
2059
2060 next_id++;
2061 spin_lock_bh(lock);
2062 if (!idr_get_next(idr, &next_id))
2063 err = -ENOENT;
2064 spin_unlock_bh(lock);
2065
2066 if (!err)
2067 err = put_user(next_id, &uattr->next_id);
2068
2069 return err;
2070}
2071
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002072#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2073
2074static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2075{
2076 struct bpf_prog *prog;
2077 u32 id = attr->prog_id;
2078 int fd;
2079
2080 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2081 return -EINVAL;
2082
2083 if (!capable(CAP_SYS_ADMIN))
2084 return -EPERM;
2085
2086 spin_lock_bh(&prog_idr_lock);
2087 prog = idr_find(&prog_idr, id);
2088 if (prog)
2089 prog = bpf_prog_inc_not_zero(prog);
2090 else
2091 prog = ERR_PTR(-ENOENT);
2092 spin_unlock_bh(&prog_idr_lock);
2093
2094 if (IS_ERR(prog))
2095 return PTR_ERR(prog);
2096
2097 fd = bpf_prog_new_fd(prog);
2098 if (fd < 0)
2099 bpf_prog_put(prog);
2100
2101 return fd;
2102}
2103
Chenbo Feng6e71b042017-10-18 13:00:22 -07002104#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002105
2106static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2107{
2108 struct bpf_map *map;
2109 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002110 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002111 int fd;
2112
Chenbo Feng6e71b042017-10-18 13:00:22 -07002113 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2114 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002115 return -EINVAL;
2116
2117 if (!capable(CAP_SYS_ADMIN))
2118 return -EPERM;
2119
Chenbo Feng6e71b042017-10-18 13:00:22 -07002120 f_flags = bpf_get_file_flag(attr->open_flags);
2121 if (f_flags < 0)
2122 return f_flags;
2123
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002124 spin_lock_bh(&map_idr_lock);
2125 map = idr_find(&map_idr, id);
2126 if (map)
2127 map = bpf_map_inc_not_zero(map, true);
2128 else
2129 map = ERR_PTR(-ENOENT);
2130 spin_unlock_bh(&map_idr_lock);
2131
2132 if (IS_ERR(map))
2133 return PTR_ERR(map);
2134
Chenbo Feng6e71b042017-10-18 13:00:22 -07002135 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002136 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002137 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002138
2139 return fd;
2140}
2141
Daniel Borkmann7105e822017-12-20 13:42:57 +01002142static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002143 unsigned long addr, u32 *off,
2144 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002145{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002146 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002147 int i;
2148
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002149 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2150 map = prog->aux->used_maps[i];
2151 if (map == (void *)addr) {
2152 *type = BPF_PSEUDO_MAP_FD;
2153 return map;
2154 }
2155 if (!map->ops->map_direct_value_meta)
2156 continue;
2157 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2158 *type = BPF_PSEUDO_MAP_VALUE;
2159 return map;
2160 }
2161 }
2162
Daniel Borkmann7105e822017-12-20 13:42:57 +01002163 return NULL;
2164}
2165
2166static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2167{
2168 const struct bpf_map *map;
2169 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002170 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002171 u64 imm;
2172 int i;
2173
2174 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2175 GFP_USER);
2176 if (!insns)
2177 return insns;
2178
2179 for (i = 0; i < prog->len; i++) {
2180 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2181 insns[i].code = BPF_JMP | BPF_CALL;
2182 insns[i].imm = BPF_FUNC_tail_call;
2183 /* fall-through */
2184 }
2185 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2186 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2187 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2188 insns[i].code = BPF_JMP | BPF_CALL;
2189 if (!bpf_dump_raw_ok())
2190 insns[i].imm = 0;
2191 continue;
2192 }
2193
2194 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2195 continue;
2196
2197 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002198 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002199 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002200 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002201 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002202 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002203 continue;
2204 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002205 }
2206
2207 return insns;
2208}
2209
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002210static int set_info_rec_size(struct bpf_prog_info *info)
2211{
2212 /*
2213 * Ensure info.*_rec_size is the same as kernel expected size
2214 *
2215 * or
2216 *
2217 * Only allow zero *_rec_size if both _rec_size and _cnt are
2218 * zero. In this case, the kernel will set the expected
2219 * _rec_size back to the info.
2220 */
2221
Yonghong Song11d8b822018-12-10 14:14:08 -08002222 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002223 info->func_info_rec_size != sizeof(struct bpf_func_info))
2224 return -EINVAL;
2225
Yonghong Song11d8b822018-12-10 14:14:08 -08002226 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002227 info->line_info_rec_size != sizeof(struct bpf_line_info))
2228 return -EINVAL;
2229
Yonghong Song11d8b822018-12-10 14:14:08 -08002230 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002231 info->jited_line_info_rec_size != sizeof(__u64))
2232 return -EINVAL;
2233
2234 info->func_info_rec_size = sizeof(struct bpf_func_info);
2235 info->line_info_rec_size = sizeof(struct bpf_line_info);
2236 info->jited_line_info_rec_size = sizeof(__u64);
2237
2238 return 0;
2239}
2240
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002241static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2242 const union bpf_attr *attr,
2243 union bpf_attr __user *uattr)
2244{
2245 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2246 struct bpf_prog_info info = {};
2247 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002248 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002249 char __user *uinsns;
2250 u32 ulen;
2251 int err;
2252
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002253 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002254 if (err)
2255 return err;
2256 info_len = min_t(u32, sizeof(info), info_len);
2257
2258 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02002259 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002260
2261 info.type = prog->type;
2262 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002263 info.load_time = prog->aux->load_time;
2264 info.created_by_uid = from_kuid_munged(current_user_ns(),
2265 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02002266 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002267
2268 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002269 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2270
2271 ulen = info.nr_map_ids;
2272 info.nr_map_ids = prog->aux->used_map_cnt;
2273 ulen = min_t(u32, info.nr_map_ids, ulen);
2274 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07002275 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002276 u32 i;
2277
2278 for (i = 0; i < ulen; i++)
2279 if (put_user(prog->aux->used_maps[i]->id,
2280 &user_map_ids[i]))
2281 return -EFAULT;
2282 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002283
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002284 err = set_info_rec_size(&info);
2285 if (err)
2286 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08002287
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002288 bpf_prog_get_stats(prog, &stats);
2289 info.run_time_ns = stats.nsecs;
2290 info.run_cnt = stats.cnt;
2291
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002292 if (!capable(CAP_SYS_ADMIN)) {
2293 info.jited_prog_len = 0;
2294 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302295 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01002296 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08002297 info.nr_func_info = 0;
2298 info.nr_line_info = 0;
2299 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002300 goto done;
2301 }
2302
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002303 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02002304 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002305 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01002306 struct bpf_insn *insns_sanitized;
2307 bool fault;
2308
2309 if (prog->blinded && !bpf_dump_raw_ok()) {
2310 info.xlated_prog_insns = 0;
2311 goto done;
2312 }
2313 insns_sanitized = bpf_insn_prepare_dump(prog);
2314 if (!insns_sanitized)
2315 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002316 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2317 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002318 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2319 kfree(insns_sanitized);
2320 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002321 return -EFAULT;
2322 }
2323
Jakub Kicinski675fc272017-12-27 18:39:09 -08002324 if (bpf_prog_is_dev_bound(prog->aux)) {
2325 err = bpf_prog_offload_info_fill(&info, prog);
2326 if (err)
2327 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08002328 goto done;
2329 }
2330
2331 /* NOTE: the following code is supposed to be skipped for offload.
2332 * bpf_prog_offload_info_fill() is the place to fill similar fields
2333 * for offload.
2334 */
2335 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05302336 if (prog->aux->func_cnt) {
2337 u32 i;
2338
2339 info.jited_prog_len = 0;
2340 for (i = 0; i < prog->aux->func_cnt; i++)
2341 info.jited_prog_len += prog->aux->func[i]->jited_len;
2342 } else {
2343 info.jited_prog_len = prog->jited_len;
2344 }
2345
Jiong Wangfcfb1262018-01-16 16:05:19 -08002346 if (info.jited_prog_len && ulen) {
2347 if (bpf_dump_raw_ok()) {
2348 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2349 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05302350
2351 /* for multi-function programs, copy the JITed
2352 * instructions for all the functions
2353 */
2354 if (prog->aux->func_cnt) {
2355 u32 len, free, i;
2356 u8 *img;
2357
2358 free = ulen;
2359 for (i = 0; i < prog->aux->func_cnt; i++) {
2360 len = prog->aux->func[i]->jited_len;
2361 len = min_t(u32, len, free);
2362 img = (u8 *) prog->aux->func[i]->bpf_func;
2363 if (copy_to_user(uinsns, img, len))
2364 return -EFAULT;
2365 uinsns += len;
2366 free -= len;
2367 if (!free)
2368 break;
2369 }
2370 } else {
2371 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2372 return -EFAULT;
2373 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002374 } else {
2375 info.jited_prog_insns = 0;
2376 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002377 }
2378
Sandipan Dasdbecd732018-05-24 12:26:48 +05302379 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07002380 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002381 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05302382 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07002383 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302384 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302385 u32 i;
2386
2387 /* copy the address of the kernel symbol
2388 * corresponding to each function
2389 */
2390 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2391 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07002392 if (prog->aux->func_cnt) {
2393 for (i = 0; i < ulen; i++) {
2394 ksym_addr = (unsigned long)
2395 prog->aux->func[i]->bpf_func;
2396 if (put_user((u64) ksym_addr,
2397 &user_ksyms[i]))
2398 return -EFAULT;
2399 }
2400 } else {
2401 ksym_addr = (unsigned long) prog->bpf_func;
2402 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05302403 return -EFAULT;
2404 }
2405 } else {
2406 info.jited_ksyms = 0;
2407 }
2408 }
2409
Sandipan Das815581c2018-05-24 12:26:52 +05302410 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07002411 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002412 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05302413 if (bpf_dump_raw_ok()) {
2414 u32 __user *user_lens;
2415 u32 func_len, i;
2416
2417 /* copy the JITed image lengths for each function */
2418 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2419 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07002420 if (prog->aux->func_cnt) {
2421 for (i = 0; i < ulen; i++) {
2422 func_len =
2423 prog->aux->func[i]->jited_len;
2424 if (put_user(func_len, &user_lens[i]))
2425 return -EFAULT;
2426 }
2427 } else {
2428 func_len = prog->jited_len;
2429 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05302430 return -EFAULT;
2431 }
2432 } else {
2433 info.jited_func_lens = 0;
2434 }
2435 }
2436
Martin KaFai Lau73372242018-12-05 17:35:43 -08002437 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08002438 info.btf_id = btf_id(prog->aux->btf);
2439
Yonghong Song11d8b822018-12-10 14:14:08 -08002440 ulen = info.nr_func_info;
2441 info.nr_func_info = prog->aux->func_info_cnt;
2442 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002443 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08002444
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002445 user_finfo = u64_to_user_ptr(info.func_info);
2446 ulen = min_t(u32, info.nr_func_info, ulen);
2447 if (copy_to_user(user_finfo, prog->aux->func_info,
2448 info.func_info_rec_size * ulen))
2449 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08002450 }
2451
Yonghong Song11d8b822018-12-10 14:14:08 -08002452 ulen = info.nr_line_info;
2453 info.nr_line_info = prog->aux->nr_linfo;
2454 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002455 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002456
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002457 user_linfo = u64_to_user_ptr(info.line_info);
2458 ulen = min_t(u32, info.nr_line_info, ulen);
2459 if (copy_to_user(user_linfo, prog->aux->linfo,
2460 info.line_info_rec_size * ulen))
2461 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002462 }
2463
Yonghong Song11d8b822018-12-10 14:14:08 -08002464 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002465 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08002466 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002467 else
Yonghong Song11d8b822018-12-10 14:14:08 -08002468 info.nr_jited_line_info = 0;
2469 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002470 if (bpf_dump_raw_ok()) {
2471 __u64 __user *user_linfo;
2472 u32 i;
2473
2474 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08002475 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002476 for (i = 0; i < ulen; i++) {
2477 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2478 &user_linfo[i]))
2479 return -EFAULT;
2480 }
2481 } else {
2482 info.jited_line_info = 0;
2483 }
2484 }
2485
Song Liuc872bdb2018-12-12 09:37:46 -08002486 ulen = info.nr_prog_tags;
2487 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2488 if (ulen) {
2489 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2490 u32 i;
2491
2492 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2493 ulen = min_t(u32, info.nr_prog_tags, ulen);
2494 if (prog->aux->func_cnt) {
2495 for (i = 0; i < ulen; i++) {
2496 if (copy_to_user(user_prog_tags[i],
2497 prog->aux->func[i]->tag,
2498 BPF_TAG_SIZE))
2499 return -EFAULT;
2500 }
2501 } else {
2502 if (copy_to_user(user_prog_tags[0],
2503 prog->tag, BPF_TAG_SIZE))
2504 return -EFAULT;
2505 }
2506 }
2507
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002508done:
2509 if (copy_to_user(uinfo, &info, info_len) ||
2510 put_user(info_len, &uattr->info.info_len))
2511 return -EFAULT;
2512
2513 return 0;
2514}
2515
2516static int bpf_map_get_info_by_fd(struct bpf_map *map,
2517 const union bpf_attr *attr,
2518 union bpf_attr __user *uattr)
2519{
2520 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2521 struct bpf_map_info info = {};
2522 u32 info_len = attr->info.info_len;
2523 int err;
2524
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002525 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002526 if (err)
2527 return err;
2528 info_len = min_t(u32, sizeof(info), info_len);
2529
2530 info.type = map->map_type;
2531 info.id = map->id;
2532 info.key_size = map->key_size;
2533 info.value_size = map->value_size;
2534 info.max_entries = map->max_entries;
2535 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002536 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002537
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002538 if (map->btf) {
2539 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002540 info.btf_key_type_id = map->btf_key_type_id;
2541 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002542 }
2543
Jakub Kicinski52775b32018-01-17 19:13:28 -08002544 if (bpf_map_is_dev_bound(map)) {
2545 err = bpf_map_offload_info_fill(&info, map);
2546 if (err)
2547 return err;
2548 }
2549
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002550 if (copy_to_user(uinfo, &info, info_len) ||
2551 put_user(info_len, &uattr->info.info_len))
2552 return -EFAULT;
2553
2554 return 0;
2555}
2556
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002557static int bpf_btf_get_info_by_fd(struct btf *btf,
2558 const union bpf_attr *attr,
2559 union bpf_attr __user *uattr)
2560{
2561 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2562 u32 info_len = attr->info.info_len;
2563 int err;
2564
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002565 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002566 if (err)
2567 return err;
2568
2569 return btf_get_info_by_fd(btf, attr, uattr);
2570}
2571
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002572#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2573
2574static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2575 union bpf_attr __user *uattr)
2576{
2577 int ufd = attr->info.bpf_fd;
2578 struct fd f;
2579 int err;
2580
2581 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2582 return -EINVAL;
2583
2584 f = fdget(ufd);
2585 if (!f.file)
2586 return -EBADFD;
2587
2588 if (f.file->f_op == &bpf_prog_fops)
2589 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2590 uattr);
2591 else if (f.file->f_op == &bpf_map_fops)
2592 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2593 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002594 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002595 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002596 else
2597 err = -EINVAL;
2598
2599 fdput(f);
2600 return err;
2601}
2602
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002603#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2604
2605static int bpf_btf_load(const union bpf_attr *attr)
2606{
2607 if (CHECK_ATTR(BPF_BTF_LOAD))
2608 return -EINVAL;
2609
2610 if (!capable(CAP_SYS_ADMIN))
2611 return -EPERM;
2612
2613 return btf_new_fd(attr);
2614}
2615
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002616#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2617
2618static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2619{
2620 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2621 return -EINVAL;
2622
2623 if (!capable(CAP_SYS_ADMIN))
2624 return -EPERM;
2625
2626 return btf_get_fd_by_id(attr->btf_id);
2627}
2628
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002629static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2630 union bpf_attr __user *uattr,
2631 u32 prog_id, u32 fd_type,
2632 const char *buf, u64 probe_offset,
2633 u64 probe_addr)
2634{
2635 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2636 u32 len = buf ? strlen(buf) : 0, input_len;
2637 int err = 0;
2638
2639 if (put_user(len, &uattr->task_fd_query.buf_len))
2640 return -EFAULT;
2641 input_len = attr->task_fd_query.buf_len;
2642 if (input_len && ubuf) {
2643 if (!len) {
2644 /* nothing to copy, just make ubuf NULL terminated */
2645 char zero = '\0';
2646
2647 if (put_user(zero, ubuf))
2648 return -EFAULT;
2649 } else if (input_len >= len + 1) {
2650 /* ubuf can hold the string with NULL terminator */
2651 if (copy_to_user(ubuf, buf, len + 1))
2652 return -EFAULT;
2653 } else {
2654 /* ubuf cannot hold the string with NULL terminator,
2655 * do a partial copy with NULL terminator.
2656 */
2657 char zero = '\0';
2658
2659 err = -ENOSPC;
2660 if (copy_to_user(ubuf, buf, input_len - 1))
2661 return -EFAULT;
2662 if (put_user(zero, ubuf + input_len - 1))
2663 return -EFAULT;
2664 }
2665 }
2666
2667 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2668 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2669 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2670 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2671 return -EFAULT;
2672
2673 return err;
2674}
2675
2676#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2677
2678static int bpf_task_fd_query(const union bpf_attr *attr,
2679 union bpf_attr __user *uattr)
2680{
2681 pid_t pid = attr->task_fd_query.pid;
2682 u32 fd = attr->task_fd_query.fd;
2683 const struct perf_event *event;
2684 struct files_struct *files;
2685 struct task_struct *task;
2686 struct file *file;
2687 int err;
2688
2689 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2690 return -EINVAL;
2691
2692 if (!capable(CAP_SYS_ADMIN))
2693 return -EPERM;
2694
2695 if (attr->task_fd_query.flags != 0)
2696 return -EINVAL;
2697
2698 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2699 if (!task)
2700 return -ENOENT;
2701
2702 files = get_files_struct(task);
2703 put_task_struct(task);
2704 if (!files)
2705 return -ENOENT;
2706
2707 err = 0;
2708 spin_lock(&files->file_lock);
2709 file = fcheck_files(files, fd);
2710 if (!file)
2711 err = -EBADF;
2712 else
2713 get_file(file);
2714 spin_unlock(&files->file_lock);
2715 put_files_struct(files);
2716
2717 if (err)
2718 goto out;
2719
2720 if (file->f_op == &bpf_raw_tp_fops) {
2721 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2722 struct bpf_raw_event_map *btp = raw_tp->btp;
2723
2724 err = bpf_task_fd_query_copy(attr, uattr,
2725 raw_tp->prog->aux->id,
2726 BPF_FD_TYPE_RAW_TRACEPOINT,
2727 btp->tp->name, 0, 0);
2728 goto put_file;
2729 }
2730
2731 event = perf_get_event(file);
2732 if (!IS_ERR(event)) {
2733 u64 probe_offset, probe_addr;
2734 u32 prog_id, fd_type;
2735 const char *buf;
2736
2737 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2738 &buf, &probe_offset,
2739 &probe_addr);
2740 if (!err)
2741 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2742 fd_type, buf,
2743 probe_offset,
2744 probe_addr);
2745 goto put_file;
2746 }
2747
2748 err = -ENOTSUPP;
2749put_file:
2750 fput(file);
2751out:
2752 return err;
2753}
2754
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002755SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2756{
2757 union bpf_attr attr = {};
2758 int err;
2759
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002760 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002761 return -EPERM;
2762
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002763 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002764 if (err)
2765 return err;
2766 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002767
2768 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2769 if (copy_from_user(&attr, uattr, size) != 0)
2770 return -EFAULT;
2771
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002772 err = security_bpf(cmd, &attr, size);
2773 if (err < 0)
2774 return err;
2775
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002776 switch (cmd) {
2777 case BPF_MAP_CREATE:
2778 err = map_create(&attr);
2779 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002780 case BPF_MAP_LOOKUP_ELEM:
2781 err = map_lookup_elem(&attr);
2782 break;
2783 case BPF_MAP_UPDATE_ELEM:
2784 err = map_update_elem(&attr);
2785 break;
2786 case BPF_MAP_DELETE_ELEM:
2787 err = map_delete_elem(&attr);
2788 break;
2789 case BPF_MAP_GET_NEXT_KEY:
2790 err = map_get_next_key(&attr);
2791 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02002792 case BPF_MAP_FREEZE:
2793 err = map_freeze(&attr);
2794 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002795 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08002796 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002797 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002798 case BPF_OBJ_PIN:
2799 err = bpf_obj_pin(&attr);
2800 break;
2801 case BPF_OBJ_GET:
2802 err = bpf_obj_get(&attr);
2803 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002804 case BPF_PROG_ATTACH:
2805 err = bpf_prog_attach(&attr);
2806 break;
2807 case BPF_PROG_DETACH:
2808 err = bpf_prog_detach(&attr);
2809 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002810 case BPF_PROG_QUERY:
2811 err = bpf_prog_query(&attr, uattr);
2812 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002813 case BPF_PROG_TEST_RUN:
2814 err = bpf_prog_test_run(&attr, uattr);
2815 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002816 case BPF_PROG_GET_NEXT_ID:
2817 err = bpf_obj_get_next_id(&attr, uattr,
2818 &prog_idr, &prog_idr_lock);
2819 break;
2820 case BPF_MAP_GET_NEXT_ID:
2821 err = bpf_obj_get_next_id(&attr, uattr,
2822 &map_idr, &map_idr_lock);
2823 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002824 case BPF_PROG_GET_FD_BY_ID:
2825 err = bpf_prog_get_fd_by_id(&attr);
2826 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002827 case BPF_MAP_GET_FD_BY_ID:
2828 err = bpf_map_get_fd_by_id(&attr);
2829 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002830 case BPF_OBJ_GET_INFO_BY_FD:
2831 err = bpf_obj_get_info_by_fd(&attr, uattr);
2832 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002833 case BPF_RAW_TRACEPOINT_OPEN:
2834 err = bpf_raw_tracepoint_open(&attr);
2835 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002836 case BPF_BTF_LOAD:
2837 err = bpf_btf_load(&attr);
2838 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002839 case BPF_BTF_GET_FD_BY_ID:
2840 err = bpf_btf_get_fd_by_id(&attr);
2841 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002842 case BPF_TASK_FD_QUERY:
2843 err = bpf_task_fd_query(&attr, uattr);
2844 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02002845 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2846 err = map_lookup_and_delete_elem(&attr);
2847 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002848 default:
2849 err = -EINVAL;
2850 break;
2851 }
2852
2853 return err;
2854}