blob: ad3ccf82f31dd01480e662eb489baa99925d688b [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 Starovoitove16d2f1a2019-01-31 15:40:05 -0800528 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700529 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
530 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800531 return -ENOTSUPP;
532 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
533 map->value_size) {
534 WARN_ONCE(1,
535 "verifier bug spin_lock_off %d value_size %d\n",
536 map->spin_lock_off, map->value_size);
537 return -EFAULT;
538 }
539 }
540
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200541 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800542 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200543
544 return ret;
545}
546
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700547#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700548/* called via syscall */
549static int map_create(union bpf_attr *attr)
550{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700551 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700552 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700553 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700554 int err;
555
556 err = CHECK_ATTR(BPF_MAP_CREATE);
557 if (err)
558 return -EINVAL;
559
Chenbo Feng6e71b042017-10-18 13:00:22 -0700560 f_flags = bpf_get_file_flag(attr->map_flags);
561 if (f_flags < 0)
562 return f_flags;
563
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700564 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700565 ((unsigned int)numa_node >= nr_node_ids ||
566 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700567 return -EINVAL;
568
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700569 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
570 map = find_and_alloc_map(attr);
571 if (IS_ERR(map))
572 return PTR_ERR(map);
573
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700574 err = bpf_obj_name_cpy(map->name, attr->map_name);
575 if (err)
576 goto free_map_nouncharge;
577
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700578 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100579 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700580
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200581 if (attr->btf_key_type_id || attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700582 struct btf *btf;
583
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200584 if (!attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700585 err = -EINVAL;
586 goto free_map_nouncharge;
587 }
588
589 btf = btf_get_by_fd(attr->btf_fd);
590 if (IS_ERR(btf)) {
591 err = PTR_ERR(btf);
592 goto free_map_nouncharge;
593 }
594
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200595 err = map_check_btf(map, btf, attr->btf_key_type_id,
596 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700597 if (err) {
598 btf_put(btf);
599 goto free_map_nouncharge;
600 }
601
602 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700603 map->btf_key_type_id = attr->btf_key_type_id;
604 map->btf_value_type_id = attr->btf_value_type_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800605 } else {
606 map->spin_lock_off = -EINVAL;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700607 }
608
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700609 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700610 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100611 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700612
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700613 err = bpf_map_init_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700614 if (err)
615 goto free_map_sec;
616
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700617 err = bpf_map_alloc_id(map);
618 if (err)
619 goto free_map;
620
Chenbo Feng6e71b042017-10-18 13:00:22 -0700621 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700622 if (err < 0) {
623 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800624 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700625 * bpf_map_alloc_id() has published the map
626 * to the userspace and the userspace may
627 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
628 */
Peng Sun352d20d2019-02-27 22:36:25 +0800629 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700630 return err;
631 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700632
633 return err;
634
635free_map:
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700636 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700637free_map_sec:
638 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100639free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700640 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700641 map->ops->map_free(map);
642 return err;
643}
644
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700645/* if error is returned, fd is released.
646 * On success caller should complete fd access with matching fdput()
647 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100648struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700649{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700650 if (!f.file)
651 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700652 if (f.file->f_op != &bpf_map_fops) {
653 fdput(f);
654 return ERR_PTR(-EINVAL);
655 }
656
Daniel Borkmannc2101292015-10-29 14:58:07 +0100657 return f.file->private_data;
658}
659
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700660/* prog's and map's refcnt limit */
661#define BPF_MAX_REFCNT 32768
662
663struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100664{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700665 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
666 atomic_dec(&map->refcnt);
667 return ERR_PTR(-EBUSY);
668 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100669 if (uref)
670 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700671 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100672}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700673EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100674
675struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100676{
677 struct fd f = fdget(ufd);
678 struct bpf_map *map;
679
680 map = __bpf_map_get(f);
681 if (IS_ERR(map))
682 return map;
683
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700684 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100685 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700686
687 return map;
688}
689
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700690/* map_idr_lock should have been held */
691static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
692 bool uref)
693{
694 int refold;
695
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100696 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700697
698 if (refold >= BPF_MAX_REFCNT) {
699 __bpf_map_put(map, false);
700 return ERR_PTR(-EBUSY);
701 }
702
703 if (!refold)
704 return ERR_PTR(-ENOENT);
705
706 if (uref)
707 atomic_inc(&map->usercnt);
708
709 return map;
710}
711
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800712int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
713{
714 return -ENOTSUPP;
715}
716
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200717static void *__bpf_copy_key(void __user *ukey, u64 key_size)
718{
719 if (key_size)
720 return memdup_user(ukey, key_size);
721
722 if (ukey)
723 return ERR_PTR(-EINVAL);
724
725 return NULL;
726}
727
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700728/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800729#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700730
731static int map_lookup_elem(union bpf_attr *attr)
732{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100733 void __user *ukey = u64_to_user_ptr(attr->key);
734 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700735 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700736 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800737 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800738 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200739 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700740 int err;
741
742 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
743 return -EINVAL;
744
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800745 if (attr->flags & ~BPF_F_LOCK)
746 return -EINVAL;
747
Daniel Borkmann592867b2015-09-08 18:00:09 +0200748 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100749 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700750 if (IS_ERR(map))
751 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200752 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700753 err = -EPERM;
754 goto err_put;
755 }
756
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800757 if ((attr->flags & BPF_F_LOCK) &&
758 !map_value_has_spin_lock(map)) {
759 err = -EINVAL;
760 goto err_put;
761 }
762
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200763 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400764 if (IS_ERR(key)) {
765 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700766 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400767 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700768
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800769 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800770 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000771 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
772 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800773 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700774 else if (IS_FD_MAP(map))
775 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800776 else
777 value_size = map->value_size;
778
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800779 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800780 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700781 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800782 goto free_key;
783
Jakub Kicinskia3884572018-01-11 20:29:09 -0800784 if (bpf_map_is_dev_bound(map)) {
785 err = bpf_map_offload_lookup_elem(map, key, value);
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800786 goto done;
787 }
788
789 preempt_disable();
790 this_cpu_inc(bpf_prog_active);
791 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
792 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800793 err = bpf_percpu_hash_copy(map, key, value);
794 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
795 err = bpf_percpu_array_copy(map, key, value);
Roman Gushchinb741f162018-09-28 14:45:43 +0000796 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
797 err = bpf_percpu_cgroup_storage_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800798 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
799 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700800 } else if (IS_FD_ARRAY(map)) {
801 err = bpf_fd_array_map_lookup_elem(map, key, value);
802 } else if (IS_FD_HASH(map)) {
803 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700804 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
805 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200806 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
807 map->map_type == BPF_MAP_TYPE_STACK) {
808 err = map->ops->map_peek_elem(map, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800809 } else {
810 rcu_read_lock();
811 ptr = map->ops->map_lookup_elem(map, key);
Prashant Bhole509db282018-10-09 10:04:49 +0900812 if (IS_ERR(ptr)) {
813 err = PTR_ERR(ptr);
814 } else if (!ptr) {
815 err = -ENOENT;
816 } else {
817 err = 0;
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800818 if (attr->flags & BPF_F_LOCK)
819 /* lock 'ptr' and copy everything but lock */
820 copy_map_value_locked(map, value, ptr, true);
821 else
822 copy_map_value(map, value, ptr);
823 /* mask lock, since value wasn't zero inited */
824 check_and_init_map_lock(map, value);
Prashant Bhole509db282018-10-09 10:04:49 +0900825 }
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800826 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800827 }
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800828 this_cpu_dec(bpf_prog_active);
829 preempt_enable();
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800830
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800831done:
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800832 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800833 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700834
835 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800836 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800837 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700838
839 err = 0;
840
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800841free_value:
842 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700843free_key:
844 kfree(key);
845err_put:
846 fdput(f);
847 return err;
848}
849
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700850static void maybe_wait_bpf_programs(struct bpf_map *map)
851{
852 /* Wait for any running BPF programs to complete so that
853 * userspace, when we return to it, knows that all programs
854 * that could be running use the new map value.
855 */
856 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
857 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
858 synchronize_rcu();
859}
860
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800861#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700862
863static int map_update_elem(union bpf_attr *attr)
864{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100865 void __user *ukey = u64_to_user_ptr(attr->key);
866 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700867 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700868 struct bpf_map *map;
869 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800870 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200871 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700872 int err;
873
874 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
875 return -EINVAL;
876
Daniel Borkmann592867b2015-09-08 18:00:09 +0200877 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100878 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700879 if (IS_ERR(map))
880 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200881 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700882 err = -EPERM;
883 goto err_put;
884 }
885
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800886 if ((attr->flags & BPF_F_LOCK) &&
887 !map_value_has_spin_lock(map)) {
888 err = -EINVAL;
889 goto err_put;
890 }
891
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200892 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400893 if (IS_ERR(key)) {
894 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700895 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400896 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700897
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800898 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800899 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000900 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
901 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800902 value_size = round_up(map->value_size, 8) * num_possible_cpus();
903 else
904 value_size = map->value_size;
905
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700906 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800907 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700908 if (!value)
909 goto free_key;
910
911 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800912 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700913 goto free_value;
914
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200915 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800916 if (bpf_map_is_dev_bound(map)) {
917 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
918 goto out;
John Fastabend99ba2b52018-07-05 08:50:04 -0700919 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
920 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
921 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200922 err = map->ops->map_update_elem(map, key, value, attr->flags);
923 goto out;
924 }
925
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800926 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
927 * inside bpf map update or delete otherwise deadlocks are possible
928 */
929 preempt_disable();
930 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800931 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
932 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800933 err = bpf_percpu_hash_update(map, key, value, attr->flags);
934 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
935 err = bpf_percpu_array_update(map, key, value, attr->flags);
Roman Gushchinb741f162018-09-28 14:45:43 +0000936 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
937 err = bpf_percpu_cgroup_storage_update(map, key, value,
938 attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100939 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200940 rcu_read_lock();
941 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
942 attr->flags);
943 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700944 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
945 rcu_read_lock();
946 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
947 attr->flags);
948 rcu_read_unlock();
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700949 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
950 /* rcu_read_lock() is not needed */
951 err = bpf_fd_reuseport_array_update_elem(map, key, value,
952 attr->flags);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200953 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
954 map->map_type == BPF_MAP_TYPE_STACK) {
955 err = map->ops->map_push_elem(map, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800956 } else {
957 rcu_read_lock();
958 err = map->ops->map_update_elem(map, key, value, attr->flags);
959 rcu_read_unlock();
960 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800961 __this_cpu_dec(bpf_prog_active);
962 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700963 maybe_wait_bpf_programs(map);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200964out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700965free_value:
966 kfree(value);
967free_key:
968 kfree(key);
969err_put:
970 fdput(f);
971 return err;
972}
973
974#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
975
976static int map_delete_elem(union bpf_attr *attr)
977{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100978 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700979 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700980 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200981 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700982 void *key;
983 int err;
984
985 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
986 return -EINVAL;
987
Daniel Borkmann592867b2015-09-08 18:00:09 +0200988 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100989 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700990 if (IS_ERR(map))
991 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200992 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700993 err = -EPERM;
994 goto err_put;
995 }
996
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200997 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400998 if (IS_ERR(key)) {
999 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001000 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001001 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001002
Jakub Kicinskia3884572018-01-11 20:29:09 -08001003 if (bpf_map_is_dev_bound(map)) {
1004 err = bpf_map_offload_delete_elem(map, key);
1005 goto out;
1006 }
1007
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001008 preempt_disable();
1009 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001010 rcu_read_lock();
1011 err = map->ops->map_delete_elem(map, key);
1012 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001013 __this_cpu_dec(bpf_prog_active);
1014 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001015 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001016out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001017 kfree(key);
1018err_put:
1019 fdput(f);
1020 return err;
1021}
1022
1023/* last field in 'union bpf_attr' used by this command */
1024#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1025
1026static int map_get_next_key(union bpf_attr *attr)
1027{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001028 void __user *ukey = u64_to_user_ptr(attr->key);
1029 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001030 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001031 struct bpf_map *map;
1032 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001033 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001034 int err;
1035
1036 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1037 return -EINVAL;
1038
Daniel Borkmann592867b2015-09-08 18:00:09 +02001039 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001040 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001041 if (IS_ERR(map))
1042 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001043 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001044 err = -EPERM;
1045 goto err_put;
1046 }
1047
Teng Qin8fe45922017-04-24 19:00:37 -07001048 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001049 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001050 if (IS_ERR(key)) {
1051 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001052 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001053 }
Teng Qin8fe45922017-04-24 19:00:37 -07001054 } else {
1055 key = NULL;
1056 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001057
1058 err = -ENOMEM;
1059 next_key = kmalloc(map->key_size, GFP_USER);
1060 if (!next_key)
1061 goto free_key;
1062
Jakub Kicinskia3884572018-01-11 20:29:09 -08001063 if (bpf_map_is_dev_bound(map)) {
1064 err = bpf_map_offload_get_next_key(map, key, next_key);
1065 goto out;
1066 }
1067
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001068 rcu_read_lock();
1069 err = map->ops->map_get_next_key(map, key, next_key);
1070 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001071out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001072 if (err)
1073 goto free_next_key;
1074
1075 err = -EFAULT;
1076 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1077 goto free_next_key;
1078
1079 err = 0;
1080
1081free_next_key:
1082 kfree(next_key);
1083free_key:
1084 kfree(key);
1085err_put:
1086 fdput(f);
1087 return err;
1088}
1089
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001090#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1091
1092static int map_lookup_and_delete_elem(union bpf_attr *attr)
1093{
1094 void __user *ukey = u64_to_user_ptr(attr->key);
1095 void __user *uvalue = u64_to_user_ptr(attr->value);
1096 int ufd = attr->map_fd;
1097 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001098 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001099 u32 value_size;
1100 struct fd f;
1101 int err;
1102
1103 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1104 return -EINVAL;
1105
1106 f = fdget(ufd);
1107 map = __bpf_map_get(f);
1108 if (IS_ERR(map))
1109 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001110 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001111 err = -EPERM;
1112 goto err_put;
1113 }
1114
1115 key = __bpf_copy_key(ukey, map->key_size);
1116 if (IS_ERR(key)) {
1117 err = PTR_ERR(key);
1118 goto err_put;
1119 }
1120
1121 value_size = map->value_size;
1122
1123 err = -ENOMEM;
1124 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1125 if (!value)
1126 goto free_key;
1127
1128 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1129 map->map_type == BPF_MAP_TYPE_STACK) {
1130 err = map->ops->map_pop_elem(map, value);
1131 } else {
1132 err = -ENOTSUPP;
1133 }
1134
1135 if (err)
1136 goto free_value;
1137
1138 if (copy_to_user(uvalue, value, value_size) != 0)
1139 goto free_value;
1140
1141 err = 0;
1142
1143free_value:
1144 kfree(value);
1145free_key:
1146 kfree(key);
1147err_put:
1148 fdput(f);
1149 return err;
1150}
1151
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001152#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1153
1154static int map_freeze(const union bpf_attr *attr)
1155{
1156 int err = 0, ufd = attr->map_fd;
1157 struct bpf_map *map;
1158 struct fd f;
1159
1160 if (CHECK_ATTR(BPF_MAP_FREEZE))
1161 return -EINVAL;
1162
1163 f = fdget(ufd);
1164 map = __bpf_map_get(f);
1165 if (IS_ERR(map))
1166 return PTR_ERR(map);
1167 if (READ_ONCE(map->frozen)) {
1168 err = -EBUSY;
1169 goto err_put;
1170 }
1171 if (!capable(CAP_SYS_ADMIN)) {
1172 err = -EPERM;
1173 goto err_put;
1174 }
1175
1176 WRITE_ONCE(map->frozen, true);
1177err_put:
1178 fdput(f);
1179 return err;
1180}
1181
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001182static const struct bpf_prog_ops * const bpf_prog_types[] = {
1183#define BPF_PROG_TYPE(_id, _name) \
1184 [_id] = & _name ## _prog_ops,
1185#define BPF_MAP_TYPE(_id, _ops)
1186#include <linux/bpf_types.h>
1187#undef BPF_PROG_TYPE
1188#undef BPF_MAP_TYPE
1189};
1190
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001191static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1192{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001193 const struct bpf_prog_ops *ops;
1194
1195 if (type >= ARRAY_SIZE(bpf_prog_types))
1196 return -EINVAL;
1197 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1198 ops = bpf_prog_types[type];
1199 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001200 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001201
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001202 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001203 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001204 else
1205 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001206 prog->type = type;
1207 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001208}
1209
1210/* drop refcnt on maps used by eBPF program and free auxilary data */
1211static void free_used_maps(struct bpf_prog_aux *aux)
1212{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001213 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001214 int i;
1215
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001216 for_each_cgroup_storage_type(stype) {
1217 if (!aux->cgroup_storage[stype])
1218 continue;
1219 bpf_cgroup_storage_release(aux->prog,
1220 aux->cgroup_storage[stype]);
1221 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07001222
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001223 for (i = 0; i < aux->used_map_cnt; i++)
1224 bpf_map_put(aux->used_maps[i]);
1225
1226 kfree(aux->used_maps);
1227}
1228
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001229int __bpf_prog_charge(struct user_struct *user, u32 pages)
1230{
1231 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1232 unsigned long user_bufs;
1233
1234 if (user) {
1235 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1236 if (user_bufs > memlock_limit) {
1237 atomic_long_sub(pages, &user->locked_vm);
1238 return -EPERM;
1239 }
1240 }
1241
1242 return 0;
1243}
1244
1245void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1246{
1247 if (user)
1248 atomic_long_sub(pages, &user->locked_vm);
1249}
1250
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001251static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1252{
1253 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001254 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001255
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001256 ret = __bpf_prog_charge(user, prog->pages);
1257 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001258 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001259 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001260 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001261
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001262 prog->aux->user = user;
1263 return 0;
1264}
1265
1266static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1267{
1268 struct user_struct *user = prog->aux->user;
1269
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001270 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001271 free_uid(user);
1272}
1273
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001274static int bpf_prog_alloc_id(struct bpf_prog *prog)
1275{
1276 int id;
1277
Shaohua Lib76354c2018-03-27 11:53:21 -07001278 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001279 spin_lock_bh(&prog_idr_lock);
1280 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1281 if (id > 0)
1282 prog->aux->id = id;
1283 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001284 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001285
1286 /* id is in [1, INT_MAX) */
1287 if (WARN_ON_ONCE(!id))
1288 return -ENOSPC;
1289
1290 return id > 0 ? 0 : id;
1291}
1292
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001293void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001294{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001295 /* cBPF to eBPF migrations are currently not in the idr store.
1296 * Offloaded programs are removed from the store when their device
1297 * disappears - even if someone grabs an fd to them they are unusable,
1298 * simply waiting for refcnt to drop to be freed.
1299 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001300 if (!prog->aux->id)
1301 return;
1302
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001303 if (do_idr_lock)
1304 spin_lock_bh(&prog_idr_lock);
1305 else
1306 __acquire(&prog_idr_lock);
1307
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001308 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001309 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001310
1311 if (do_idr_lock)
1312 spin_unlock_bh(&prog_idr_lock);
1313 else
1314 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001315}
1316
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001317static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001318{
1319 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1320
1321 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001322 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001323 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001324 bpf_prog_free(aux->prog);
1325}
1326
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001327static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001328{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001329 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001330 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001331 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001332 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001333 bpf_prog_kallsyms_del_all(prog);
Yonghong Song838e9692018-11-19 15:29:11 -08001334 btf_put(prog->aux->btf);
Yonghong Songba64e7d2018-11-24 23:20:44 -08001335 kvfree(prog->aux->func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001336 bpf_prog_free_linfo(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001337
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001338 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001339 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001340}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001341
1342void bpf_prog_put(struct bpf_prog *prog)
1343{
1344 __bpf_prog_put(prog, true);
1345}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001346EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001347
1348static int bpf_prog_release(struct inode *inode, struct file *filp)
1349{
1350 struct bpf_prog *prog = filp->private_data;
1351
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001352 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001353 return 0;
1354}
1355
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001356static void bpf_prog_get_stats(const struct bpf_prog *prog,
1357 struct bpf_prog_stats *stats)
1358{
1359 u64 nsecs = 0, cnt = 0;
1360 int cpu;
1361
1362 for_each_possible_cpu(cpu) {
1363 const struct bpf_prog_stats *st;
1364 unsigned int start;
1365 u64 tnsecs, tcnt;
1366
1367 st = per_cpu_ptr(prog->aux->stats, cpu);
1368 do {
1369 start = u64_stats_fetch_begin_irq(&st->syncp);
1370 tnsecs = st->nsecs;
1371 tcnt = st->cnt;
1372 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1373 nsecs += tnsecs;
1374 cnt += tcnt;
1375 }
1376 stats->nsecs = nsecs;
1377 stats->cnt = cnt;
1378}
1379
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001380#ifdef CONFIG_PROC_FS
1381static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1382{
1383 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001384 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001385 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001386
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001387 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001388 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001389 seq_printf(m,
1390 "prog_type:\t%u\n"
1391 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001392 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001393 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001394 "prog_id:\t%u\n"
1395 "run_time_ns:\t%llu\n"
1396 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001397 prog->type,
1398 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001399 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001400 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001401 prog->aux->id,
1402 stats.nsecs,
1403 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001404}
1405#endif
1406
Chenbo Fengf66e4482017-10-18 13:00:26 -07001407const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001408#ifdef CONFIG_PROC_FS
1409 .show_fdinfo = bpf_prog_show_fdinfo,
1410#endif
1411 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001412 .read = bpf_dummy_read,
1413 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001414};
1415
Daniel Borkmannb2197752015-10-29 14:58:09 +01001416int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001417{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001418 int ret;
1419
1420 ret = security_bpf_prog(prog);
1421 if (ret < 0)
1422 return ret;
1423
Daniel Borkmannaa797812015-10-29 14:58:06 +01001424 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1425 O_RDWR | O_CLOEXEC);
1426}
1427
Daniel Borkmann113214b2016-06-30 17:24:44 +02001428static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001429{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001430 if (!f.file)
1431 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001432 if (f.file->f_op != &bpf_prog_fops) {
1433 fdput(f);
1434 return ERR_PTR(-EINVAL);
1435 }
1436
Daniel Borkmannc2101292015-10-29 14:58:07 +01001437 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001438}
1439
Brenden Blanco59d36562016-07-19 12:16:46 -07001440struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001441{
Brenden Blanco59d36562016-07-19 12:16:46 -07001442 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1443 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001444 return ERR_PTR(-EBUSY);
1445 }
1446 return prog;
1447}
Brenden Blanco59d36562016-07-19 12:16:46 -07001448EXPORT_SYMBOL_GPL(bpf_prog_add);
1449
Daniel Borkmannc5405942016-11-09 22:02:34 +01001450void bpf_prog_sub(struct bpf_prog *prog, int i)
1451{
1452 /* Only to be used for undoing previous bpf_prog_add() in some
1453 * error path. We still know that another entity in our call
1454 * path holds a reference to the program, thus atomic_sub() can
1455 * be safely used in such cases!
1456 */
1457 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1458}
1459EXPORT_SYMBOL_GPL(bpf_prog_sub);
1460
Brenden Blanco59d36562016-07-19 12:16:46 -07001461struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1462{
1463 return bpf_prog_add(prog, 1);
1464}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001465EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001466
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001467/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001468struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001469{
1470 int refold;
1471
Mark Rutlandbfc18e32018-06-21 13:13:04 +01001472 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001473
1474 if (refold >= BPF_MAX_REFCNT) {
1475 __bpf_prog_put(prog, false);
1476 return ERR_PTR(-EBUSY);
1477 }
1478
1479 if (!refold)
1480 return ERR_PTR(-ENOENT);
1481
1482 return prog;
1483}
John Fastabenda6f6df62017-08-15 22:32:22 -07001484EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001485
Al Viro040ee692017-12-02 20:20:38 -05001486bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001487 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001488{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001489 /* not an attachment, just a refcount inc, always allow */
1490 if (!attach_type)
1491 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001492
1493 if (prog->type != *attach_type)
1494 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001495 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001496 return false;
1497
1498 return true;
1499}
1500
1501static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001502 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001503{
1504 struct fd f = fdget(ufd);
1505 struct bpf_prog *prog;
1506
Daniel Borkmann113214b2016-06-30 17:24:44 +02001507 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001508 if (IS_ERR(prog))
1509 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001510 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001511 prog = ERR_PTR(-EINVAL);
1512 goto out;
1513 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001514
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001515 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001516out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001517 fdput(f);
1518 return prog;
1519}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001520
1521struct bpf_prog *bpf_prog_get(u32 ufd)
1522{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001523 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001524}
1525
Jakub Kicinski248f3462017-11-03 13:56:20 -07001526struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001527 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001528{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001529 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001530}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001531EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001532
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001533/* Initially all BPF programs could be loaded w/o specifying
1534 * expected_attach_type. Later for some of them specifying expected_attach_type
1535 * at load time became required so that program could be validated properly.
1536 * Programs of types that are allowed to be loaded both w/ and w/o (for
1537 * backward compatibility) expected_attach_type, should have the default attach
1538 * type assigned to expected_attach_type for the latter case, so that it can be
1539 * validated later at attach time.
1540 *
1541 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1542 * prog type requires it but has some attach types that have to be backward
1543 * compatible.
1544 */
1545static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1546{
1547 switch (attr->prog_type) {
1548 case BPF_PROG_TYPE_CGROUP_SOCK:
1549 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1550 * exist so checking for non-zero is the way to go here.
1551 */
1552 if (!attr->expected_attach_type)
1553 attr->expected_attach_type =
1554 BPF_CGROUP_INET_SOCK_CREATE;
1555 break;
1556 }
1557}
1558
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001559static int
1560bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1561 enum bpf_attach_type expected_attach_type)
1562{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001563 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001564 case BPF_PROG_TYPE_CGROUP_SOCK:
1565 switch (expected_attach_type) {
1566 case BPF_CGROUP_INET_SOCK_CREATE:
1567 case BPF_CGROUP_INET4_POST_BIND:
1568 case BPF_CGROUP_INET6_POST_BIND:
1569 return 0;
1570 default:
1571 return -EINVAL;
1572 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001573 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1574 switch (expected_attach_type) {
1575 case BPF_CGROUP_INET4_BIND:
1576 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001577 case BPF_CGROUP_INET4_CONNECT:
1578 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001579 case BPF_CGROUP_UDP4_SENDMSG:
1580 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001581 return 0;
1582 default:
1583 return -EINVAL;
1584 }
1585 default:
1586 return 0;
1587 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001588}
1589
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001590/* last field in 'union bpf_attr' used by this command */
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001591#define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001592
Yonghong Song838e9692018-11-19 15:29:11 -08001593static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001594{
1595 enum bpf_prog_type type = attr->prog_type;
1596 struct bpf_prog *prog;
1597 int err;
1598 char license[128];
1599 bool is_gpl;
1600
1601 if (CHECK_ATTR(BPF_PROG_LOAD))
1602 return -EINVAL;
1603
David Millere9ee9ef2018-11-30 21:08:14 -08001604 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT))
David S. Millere07b98d2017-05-10 11:38:07 -07001605 return -EINVAL;
1606
David Millere9ee9ef2018-11-30 21:08:14 -08001607 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1608 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1609 !capable(CAP_SYS_ADMIN))
1610 return -EPERM;
1611
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001612 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001613 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001614 sizeof(license) - 1) < 0)
1615 return -EFAULT;
1616 license[sizeof(license) - 1] = 0;
1617
1618 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1619 is_gpl = license_is_gpl_compatible(license);
1620
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07001621 if (attr->insn_cnt == 0 ||
1622 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001623 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07001624 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1625 type != BPF_PROG_TYPE_CGROUP_SKB &&
1626 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001627 return -EPERM;
1628
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001629 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001630 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1631 return -EINVAL;
1632
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001633 /* plain bpf_prog allocation */
1634 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1635 if (!prog)
1636 return -ENOMEM;
1637
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001638 prog->expected_attach_type = attr->expected_attach_type;
1639
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001640 prog->aux->offload_requested = !!attr->prog_ifindex;
1641
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001642 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001643 if (err)
1644 goto free_prog_nouncharge;
1645
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001646 err = bpf_prog_charge_memlock(prog);
1647 if (err)
1648 goto free_prog_sec;
1649
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001650 prog->len = attr->insn_cnt;
1651
1652 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001653 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001654 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001655 goto free_prog;
1656
1657 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001658 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001659
1660 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001661 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001662
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001663 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001664 err = bpf_prog_offload_init(prog, attr);
1665 if (err)
1666 goto free_prog;
1667 }
1668
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001669 /* find program type: socket_filter vs tracing_filter */
1670 err = find_prog_type(type, prog);
1671 if (err < 0)
1672 goto free_prog;
1673
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001674 prog->aux->load_time = ktime_get_boot_ns();
1675 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1676 if (err)
1677 goto free_prog;
1678
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001679 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08001680 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001681 if (err < 0)
1682 goto free_used_maps;
1683
Daniel Borkmann9facc332018-06-15 02:30:48 +02001684 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001685 if (err < 0)
1686 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001687
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001688 err = bpf_prog_alloc_id(prog);
1689 if (err)
1690 goto free_used_maps;
1691
Daniel Borkmannaa797812015-10-29 14:58:06 +01001692 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001693 if (err < 0) {
1694 /* failed to allocate fd.
1695 * bpf_prog_put() is needed because the above
1696 * bpf_prog_alloc_id() has published the prog
1697 * to the userspace and the userspace may
1698 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1699 */
1700 bpf_prog_put(prog);
1701 return err;
1702 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001703
Daniel Borkmann74451e662017-02-16 22:24:50 +01001704 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08001705 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001706 return err;
1707
1708free_used_maps:
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001709 bpf_prog_free_linfo(prog);
Martin KaFai Lau5482e9a2018-12-01 17:08:44 -08001710 kvfree(prog->aux->func_info);
1711 btf_put(prog->aux->btf);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001712 bpf_prog_kallsyms_del_subprogs(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001713 free_used_maps(prog->aux);
1714free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001715 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001716free_prog_sec:
1717 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001718free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001719 bpf_prog_free(prog);
1720 return err;
1721}
1722
Chenbo Feng6e71b042017-10-18 13:00:22 -07001723#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001724
1725static int bpf_obj_pin(const union bpf_attr *attr)
1726{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001727 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001728 return -EINVAL;
1729
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001730 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001731}
1732
1733static int bpf_obj_get(const union bpf_attr *attr)
1734{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001735 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1736 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001737 return -EINVAL;
1738
Chenbo Feng6e71b042017-10-18 13:00:22 -07001739 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1740 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001741}
1742
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001743struct bpf_raw_tracepoint {
1744 struct bpf_raw_event_map *btp;
1745 struct bpf_prog *prog;
1746};
1747
1748static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1749{
1750 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1751
1752 if (raw_tp->prog) {
1753 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1754 bpf_prog_put(raw_tp->prog);
1755 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001756 bpf_put_raw_tracepoint(raw_tp->btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001757 kfree(raw_tp);
1758 return 0;
1759}
1760
1761static const struct file_operations bpf_raw_tp_fops = {
1762 .release = bpf_raw_tracepoint_release,
1763 .read = bpf_dummy_read,
1764 .write = bpf_dummy_write,
1765};
1766
1767#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1768
1769static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1770{
1771 struct bpf_raw_tracepoint *raw_tp;
1772 struct bpf_raw_event_map *btp;
1773 struct bpf_prog *prog;
1774 char tp_name[128];
1775 int tp_fd, err;
1776
1777 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1778 sizeof(tp_name) - 1) < 0)
1779 return -EFAULT;
1780 tp_name[sizeof(tp_name) - 1] = 0;
1781
Matt Mullinsa38d1102018-12-12 16:42:37 -08001782 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001783 if (!btp)
1784 return -ENOENT;
1785
1786 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001787 if (!raw_tp) {
1788 err = -ENOMEM;
1789 goto out_put_btp;
1790 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001791 raw_tp->btp = btp;
1792
Matt Mullins9df1c282019-04-26 11:49:47 -07001793 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001794 if (IS_ERR(prog)) {
1795 err = PTR_ERR(prog);
1796 goto out_free_tp;
1797 }
Matt Mullins9df1c282019-04-26 11:49:47 -07001798 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1799 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1800 err = -EINVAL;
1801 goto out_put_prog;
1802 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001803
1804 err = bpf_probe_register(raw_tp->btp, prog);
1805 if (err)
1806 goto out_put_prog;
1807
1808 raw_tp->prog = prog;
1809 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1810 O_CLOEXEC);
1811 if (tp_fd < 0) {
1812 bpf_probe_unregister(raw_tp->btp, prog);
1813 err = tp_fd;
1814 goto out_put_prog;
1815 }
1816 return tp_fd;
1817
1818out_put_prog:
1819 bpf_prog_put(prog);
1820out_free_tp:
1821 kfree(raw_tp);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001822out_put_btp:
1823 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001824 return err;
1825}
1826
Anders Roxell33491582018-04-03 14:09:47 +02001827static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1828 enum bpf_attach_type attach_type)
1829{
1830 switch (prog->type) {
1831 case BPF_PROG_TYPE_CGROUP_SOCK:
1832 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1833 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1834 default:
1835 return 0;
1836 }
1837}
1838
John Fastabend464bc0f2017-08-28 07:10:04 -07001839#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001840
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001841#define BPF_F_ATTACH_MASK \
1842 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1843
Daniel Mackf4324552016-11-23 16:52:27 +01001844static int bpf_prog_attach(const union bpf_attr *attr)
1845{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001846 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001847 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001848 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001849
1850 if (!capable(CAP_NET_ADMIN))
1851 return -EPERM;
1852
1853 if (CHECK_ATTR(BPF_PROG_ATTACH))
1854 return -EINVAL;
1855
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001856 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001857 return -EINVAL;
1858
Daniel Mackf4324552016-11-23 16:52:27 +01001859 switch (attr->attach_type) {
1860 case BPF_CGROUP_INET_INGRESS:
1861 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001862 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001863 break;
David Ahern610236582016-12-01 08:48:04 -08001864 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001865 case BPF_CGROUP_INET4_POST_BIND:
1866 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001867 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1868 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001869 case BPF_CGROUP_INET4_BIND:
1870 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001871 case BPF_CGROUP_INET4_CONNECT:
1872 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001873 case BPF_CGROUP_UDP4_SENDMSG:
1874 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001875 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1876 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001877 case BPF_CGROUP_SOCK_OPS:
1878 ptype = BPF_PROG_TYPE_SOCK_OPS;
1879 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001880 case BPF_CGROUP_DEVICE:
1881 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1882 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001883 case BPF_SK_MSG_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001884 ptype = BPF_PROG_TYPE_SK_MSG;
1885 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001886 case BPF_SK_SKB_STREAM_PARSER:
1887 case BPF_SK_SKB_STREAM_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001888 ptype = BPF_PROG_TYPE_SK_SKB;
1889 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01001890 case BPF_LIRC_MODE2:
Sean Youngfdb5c452018-06-19 00:04:24 +01001891 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1892 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001893 case BPF_FLOW_DISSECTOR:
1894 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1895 break;
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001896 case BPF_CGROUP_SYSCTL:
1897 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1898 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001899 default:
1900 return -EINVAL;
1901 }
1902
David Ahernb2cd1252016-12-01 08:48:03 -08001903 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1904 if (IS_ERR(prog))
1905 return PTR_ERR(prog);
1906
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001907 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1908 bpf_prog_put(prog);
1909 return -EINVAL;
1910 }
1911
Sean Youngfdb5c452018-06-19 00:04:24 +01001912 switch (ptype) {
1913 case BPF_PROG_TYPE_SK_SKB:
1914 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001915 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01001916 break;
1917 case BPF_PROG_TYPE_LIRC_MODE2:
1918 ret = lirc_prog_attach(attr, prog);
1919 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001920 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1921 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1922 break;
Sean Youngfdb5c452018-06-19 00:04:24 +01001923 default:
1924 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001925 }
1926
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001927 if (ret)
1928 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001929 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001930}
1931
1932#define BPF_PROG_DETACH_LAST_FIELD attach_type
1933
1934static int bpf_prog_detach(const union bpf_attr *attr)
1935{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001936 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001937
1938 if (!capable(CAP_NET_ADMIN))
1939 return -EPERM;
1940
1941 if (CHECK_ATTR(BPF_PROG_DETACH))
1942 return -EINVAL;
1943
1944 switch (attr->attach_type) {
1945 case BPF_CGROUP_INET_INGRESS:
1946 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001947 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1948 break;
David Ahern610236582016-12-01 08:48:04 -08001949 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001950 case BPF_CGROUP_INET4_POST_BIND:
1951 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001952 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1953 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001954 case BPF_CGROUP_INET4_BIND:
1955 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001956 case BPF_CGROUP_INET4_CONNECT:
1957 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001958 case BPF_CGROUP_UDP4_SENDMSG:
1959 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001960 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1961 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001962 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001963 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001964 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001965 case BPF_CGROUP_DEVICE:
1966 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1967 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001968 case BPF_SK_MSG_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001969 return sock_map_get_from_fd(attr, NULL);
John Fastabend5a67da22017-09-08 14:00:49 -07001970 case BPF_SK_SKB_STREAM_PARSER:
1971 case BPF_SK_SKB_STREAM_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001972 return sock_map_get_from_fd(attr, NULL);
Sean Youngf4364dc2018-05-27 12:24:09 +01001973 case BPF_LIRC_MODE2:
1974 return lirc_prog_detach(attr);
Petar Penkovd58e4682018-09-14 07:46:18 -07001975 case BPF_FLOW_DISSECTOR:
1976 return skb_flow_dissector_bpf_prog_detach(attr);
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001977 case BPF_CGROUP_SYSCTL:
1978 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1979 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001980 default:
1981 return -EINVAL;
1982 }
1983
Sean Youngfdb5c452018-06-19 00:04:24 +01001984 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01001985}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001986
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001987#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1988
1989static int bpf_prog_query(const union bpf_attr *attr,
1990 union bpf_attr __user *uattr)
1991{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001992 if (!capable(CAP_NET_ADMIN))
1993 return -EPERM;
1994 if (CHECK_ATTR(BPF_PROG_QUERY))
1995 return -EINVAL;
1996 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1997 return -EINVAL;
1998
1999 switch (attr->query.attach_type) {
2000 case BPF_CGROUP_INET_INGRESS:
2001 case BPF_CGROUP_INET_EGRESS:
2002 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002003 case BPF_CGROUP_INET4_BIND:
2004 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002005 case BPF_CGROUP_INET4_POST_BIND:
2006 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002007 case BPF_CGROUP_INET4_CONNECT:
2008 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002009 case BPF_CGROUP_UDP4_SENDMSG:
2010 case BPF_CGROUP_UDP6_SENDMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002011 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05002012 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002013 case BPF_CGROUP_SYSCTL:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002014 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01002015 case BPF_LIRC_MODE2:
2016 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07002017 case BPF_FLOW_DISSECTOR:
2018 return skb_flow_dissector_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002019 default:
2020 return -EINVAL;
2021 }
Sean Youngfdb5c452018-06-19 00:04:24 +01002022
2023 return cgroup_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002024}
Daniel Mackf4324552016-11-23 16:52:27 +01002025
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002026#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002027
2028static int bpf_prog_test_run(const union bpf_attr *attr,
2029 union bpf_attr __user *uattr)
2030{
2031 struct bpf_prog *prog;
2032 int ret = -ENOTSUPP;
2033
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08002034 if (!capable(CAP_SYS_ADMIN))
2035 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002036 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2037 return -EINVAL;
2038
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002039 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2040 (!attr->test.ctx_size_in && attr->test.ctx_in))
2041 return -EINVAL;
2042
2043 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2044 (!attr->test.ctx_size_out && attr->test.ctx_out))
2045 return -EINVAL;
2046
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002047 prog = bpf_prog_get(attr->test.prog_fd);
2048 if (IS_ERR(prog))
2049 return PTR_ERR(prog);
2050
2051 if (prog->aux->ops->test_run)
2052 ret = prog->aux->ops->test_run(prog, attr, uattr);
2053
2054 bpf_prog_put(prog);
2055 return ret;
2056}
2057
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002058#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2059
2060static int bpf_obj_get_next_id(const union bpf_attr *attr,
2061 union bpf_attr __user *uattr,
2062 struct idr *idr,
2063 spinlock_t *lock)
2064{
2065 u32 next_id = attr->start_id;
2066 int err = 0;
2067
2068 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2069 return -EINVAL;
2070
2071 if (!capable(CAP_SYS_ADMIN))
2072 return -EPERM;
2073
2074 next_id++;
2075 spin_lock_bh(lock);
2076 if (!idr_get_next(idr, &next_id))
2077 err = -ENOENT;
2078 spin_unlock_bh(lock);
2079
2080 if (!err)
2081 err = put_user(next_id, &uattr->next_id);
2082
2083 return err;
2084}
2085
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002086#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2087
2088static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2089{
2090 struct bpf_prog *prog;
2091 u32 id = attr->prog_id;
2092 int fd;
2093
2094 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2095 return -EINVAL;
2096
2097 if (!capable(CAP_SYS_ADMIN))
2098 return -EPERM;
2099
2100 spin_lock_bh(&prog_idr_lock);
2101 prog = idr_find(&prog_idr, id);
2102 if (prog)
2103 prog = bpf_prog_inc_not_zero(prog);
2104 else
2105 prog = ERR_PTR(-ENOENT);
2106 spin_unlock_bh(&prog_idr_lock);
2107
2108 if (IS_ERR(prog))
2109 return PTR_ERR(prog);
2110
2111 fd = bpf_prog_new_fd(prog);
2112 if (fd < 0)
2113 bpf_prog_put(prog);
2114
2115 return fd;
2116}
2117
Chenbo Feng6e71b042017-10-18 13:00:22 -07002118#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002119
2120static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2121{
2122 struct bpf_map *map;
2123 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002124 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002125 int fd;
2126
Chenbo Feng6e71b042017-10-18 13:00:22 -07002127 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2128 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002129 return -EINVAL;
2130
2131 if (!capable(CAP_SYS_ADMIN))
2132 return -EPERM;
2133
Chenbo Feng6e71b042017-10-18 13:00:22 -07002134 f_flags = bpf_get_file_flag(attr->open_flags);
2135 if (f_flags < 0)
2136 return f_flags;
2137
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002138 spin_lock_bh(&map_idr_lock);
2139 map = idr_find(&map_idr, id);
2140 if (map)
2141 map = bpf_map_inc_not_zero(map, true);
2142 else
2143 map = ERR_PTR(-ENOENT);
2144 spin_unlock_bh(&map_idr_lock);
2145
2146 if (IS_ERR(map))
2147 return PTR_ERR(map);
2148
Chenbo Feng6e71b042017-10-18 13:00:22 -07002149 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002150 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002151 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002152
2153 return fd;
2154}
2155
Daniel Borkmann7105e822017-12-20 13:42:57 +01002156static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002157 unsigned long addr, u32 *off,
2158 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002159{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002160 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002161 int i;
2162
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002163 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2164 map = prog->aux->used_maps[i];
2165 if (map == (void *)addr) {
2166 *type = BPF_PSEUDO_MAP_FD;
2167 return map;
2168 }
2169 if (!map->ops->map_direct_value_meta)
2170 continue;
2171 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2172 *type = BPF_PSEUDO_MAP_VALUE;
2173 return map;
2174 }
2175 }
2176
Daniel Borkmann7105e822017-12-20 13:42:57 +01002177 return NULL;
2178}
2179
2180static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2181{
2182 const struct bpf_map *map;
2183 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002184 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002185 u64 imm;
2186 int i;
2187
2188 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2189 GFP_USER);
2190 if (!insns)
2191 return insns;
2192
2193 for (i = 0; i < prog->len; i++) {
2194 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2195 insns[i].code = BPF_JMP | BPF_CALL;
2196 insns[i].imm = BPF_FUNC_tail_call;
2197 /* fall-through */
2198 }
2199 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2200 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2201 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2202 insns[i].code = BPF_JMP | BPF_CALL;
2203 if (!bpf_dump_raw_ok())
2204 insns[i].imm = 0;
2205 continue;
2206 }
2207
2208 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2209 continue;
2210
2211 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002212 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002213 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002214 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002215 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002216 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002217 continue;
2218 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002219 }
2220
2221 return insns;
2222}
2223
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002224static int set_info_rec_size(struct bpf_prog_info *info)
2225{
2226 /*
2227 * Ensure info.*_rec_size is the same as kernel expected size
2228 *
2229 * or
2230 *
2231 * Only allow zero *_rec_size if both _rec_size and _cnt are
2232 * zero. In this case, the kernel will set the expected
2233 * _rec_size back to the info.
2234 */
2235
Yonghong Song11d8b822018-12-10 14:14:08 -08002236 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002237 info->func_info_rec_size != sizeof(struct bpf_func_info))
2238 return -EINVAL;
2239
Yonghong Song11d8b822018-12-10 14:14:08 -08002240 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002241 info->line_info_rec_size != sizeof(struct bpf_line_info))
2242 return -EINVAL;
2243
Yonghong Song11d8b822018-12-10 14:14:08 -08002244 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002245 info->jited_line_info_rec_size != sizeof(__u64))
2246 return -EINVAL;
2247
2248 info->func_info_rec_size = sizeof(struct bpf_func_info);
2249 info->line_info_rec_size = sizeof(struct bpf_line_info);
2250 info->jited_line_info_rec_size = sizeof(__u64);
2251
2252 return 0;
2253}
2254
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002255static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2256 const union bpf_attr *attr,
2257 union bpf_attr __user *uattr)
2258{
2259 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2260 struct bpf_prog_info info = {};
2261 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002262 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002263 char __user *uinsns;
2264 u32 ulen;
2265 int err;
2266
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002267 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002268 if (err)
2269 return err;
2270 info_len = min_t(u32, sizeof(info), info_len);
2271
2272 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02002273 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002274
2275 info.type = prog->type;
2276 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002277 info.load_time = prog->aux->load_time;
2278 info.created_by_uid = from_kuid_munged(current_user_ns(),
2279 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02002280 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002281
2282 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002283 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2284
2285 ulen = info.nr_map_ids;
2286 info.nr_map_ids = prog->aux->used_map_cnt;
2287 ulen = min_t(u32, info.nr_map_ids, ulen);
2288 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07002289 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002290 u32 i;
2291
2292 for (i = 0; i < ulen; i++)
2293 if (put_user(prog->aux->used_maps[i]->id,
2294 &user_map_ids[i]))
2295 return -EFAULT;
2296 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002297
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002298 err = set_info_rec_size(&info);
2299 if (err)
2300 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08002301
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002302 bpf_prog_get_stats(prog, &stats);
2303 info.run_time_ns = stats.nsecs;
2304 info.run_cnt = stats.cnt;
2305
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002306 if (!capable(CAP_SYS_ADMIN)) {
2307 info.jited_prog_len = 0;
2308 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302309 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01002310 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08002311 info.nr_func_info = 0;
2312 info.nr_line_info = 0;
2313 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002314 goto done;
2315 }
2316
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002317 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02002318 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002319 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01002320 struct bpf_insn *insns_sanitized;
2321 bool fault;
2322
2323 if (prog->blinded && !bpf_dump_raw_ok()) {
2324 info.xlated_prog_insns = 0;
2325 goto done;
2326 }
2327 insns_sanitized = bpf_insn_prepare_dump(prog);
2328 if (!insns_sanitized)
2329 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002330 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2331 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002332 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2333 kfree(insns_sanitized);
2334 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002335 return -EFAULT;
2336 }
2337
Jakub Kicinski675fc272017-12-27 18:39:09 -08002338 if (bpf_prog_is_dev_bound(prog->aux)) {
2339 err = bpf_prog_offload_info_fill(&info, prog);
2340 if (err)
2341 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08002342 goto done;
2343 }
2344
2345 /* NOTE: the following code is supposed to be skipped for offload.
2346 * bpf_prog_offload_info_fill() is the place to fill similar fields
2347 * for offload.
2348 */
2349 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05302350 if (prog->aux->func_cnt) {
2351 u32 i;
2352
2353 info.jited_prog_len = 0;
2354 for (i = 0; i < prog->aux->func_cnt; i++)
2355 info.jited_prog_len += prog->aux->func[i]->jited_len;
2356 } else {
2357 info.jited_prog_len = prog->jited_len;
2358 }
2359
Jiong Wangfcfb1262018-01-16 16:05:19 -08002360 if (info.jited_prog_len && ulen) {
2361 if (bpf_dump_raw_ok()) {
2362 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2363 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05302364
2365 /* for multi-function programs, copy the JITed
2366 * instructions for all the functions
2367 */
2368 if (prog->aux->func_cnt) {
2369 u32 len, free, i;
2370 u8 *img;
2371
2372 free = ulen;
2373 for (i = 0; i < prog->aux->func_cnt; i++) {
2374 len = prog->aux->func[i]->jited_len;
2375 len = min_t(u32, len, free);
2376 img = (u8 *) prog->aux->func[i]->bpf_func;
2377 if (copy_to_user(uinsns, img, len))
2378 return -EFAULT;
2379 uinsns += len;
2380 free -= len;
2381 if (!free)
2382 break;
2383 }
2384 } else {
2385 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2386 return -EFAULT;
2387 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002388 } else {
2389 info.jited_prog_insns = 0;
2390 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002391 }
2392
Sandipan Dasdbecd732018-05-24 12:26:48 +05302393 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07002394 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002395 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05302396 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07002397 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302398 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302399 u32 i;
2400
2401 /* copy the address of the kernel symbol
2402 * corresponding to each function
2403 */
2404 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2405 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07002406 if (prog->aux->func_cnt) {
2407 for (i = 0; i < ulen; i++) {
2408 ksym_addr = (unsigned long)
2409 prog->aux->func[i]->bpf_func;
2410 if (put_user((u64) ksym_addr,
2411 &user_ksyms[i]))
2412 return -EFAULT;
2413 }
2414 } else {
2415 ksym_addr = (unsigned long) prog->bpf_func;
2416 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05302417 return -EFAULT;
2418 }
2419 } else {
2420 info.jited_ksyms = 0;
2421 }
2422 }
2423
Sandipan Das815581c2018-05-24 12:26:52 +05302424 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07002425 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002426 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05302427 if (bpf_dump_raw_ok()) {
2428 u32 __user *user_lens;
2429 u32 func_len, i;
2430
2431 /* copy the JITed image lengths for each function */
2432 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2433 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07002434 if (prog->aux->func_cnt) {
2435 for (i = 0; i < ulen; i++) {
2436 func_len =
2437 prog->aux->func[i]->jited_len;
2438 if (put_user(func_len, &user_lens[i]))
2439 return -EFAULT;
2440 }
2441 } else {
2442 func_len = prog->jited_len;
2443 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05302444 return -EFAULT;
2445 }
2446 } else {
2447 info.jited_func_lens = 0;
2448 }
2449 }
2450
Martin KaFai Lau73372242018-12-05 17:35:43 -08002451 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08002452 info.btf_id = btf_id(prog->aux->btf);
2453
Yonghong Song11d8b822018-12-10 14:14:08 -08002454 ulen = info.nr_func_info;
2455 info.nr_func_info = prog->aux->func_info_cnt;
2456 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002457 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08002458
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002459 user_finfo = u64_to_user_ptr(info.func_info);
2460 ulen = min_t(u32, info.nr_func_info, ulen);
2461 if (copy_to_user(user_finfo, prog->aux->func_info,
2462 info.func_info_rec_size * ulen))
2463 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08002464 }
2465
Yonghong Song11d8b822018-12-10 14:14:08 -08002466 ulen = info.nr_line_info;
2467 info.nr_line_info = prog->aux->nr_linfo;
2468 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002469 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002470
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002471 user_linfo = u64_to_user_ptr(info.line_info);
2472 ulen = min_t(u32, info.nr_line_info, ulen);
2473 if (copy_to_user(user_linfo, prog->aux->linfo,
2474 info.line_info_rec_size * ulen))
2475 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002476 }
2477
Yonghong Song11d8b822018-12-10 14:14:08 -08002478 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002479 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08002480 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002481 else
Yonghong Song11d8b822018-12-10 14:14:08 -08002482 info.nr_jited_line_info = 0;
2483 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002484 if (bpf_dump_raw_ok()) {
2485 __u64 __user *user_linfo;
2486 u32 i;
2487
2488 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08002489 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002490 for (i = 0; i < ulen; i++) {
2491 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2492 &user_linfo[i]))
2493 return -EFAULT;
2494 }
2495 } else {
2496 info.jited_line_info = 0;
2497 }
2498 }
2499
Song Liuc872bdb2018-12-12 09:37:46 -08002500 ulen = info.nr_prog_tags;
2501 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2502 if (ulen) {
2503 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2504 u32 i;
2505
2506 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2507 ulen = min_t(u32, info.nr_prog_tags, ulen);
2508 if (prog->aux->func_cnt) {
2509 for (i = 0; i < ulen; i++) {
2510 if (copy_to_user(user_prog_tags[i],
2511 prog->aux->func[i]->tag,
2512 BPF_TAG_SIZE))
2513 return -EFAULT;
2514 }
2515 } else {
2516 if (copy_to_user(user_prog_tags[0],
2517 prog->tag, BPF_TAG_SIZE))
2518 return -EFAULT;
2519 }
2520 }
2521
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002522done:
2523 if (copy_to_user(uinfo, &info, info_len) ||
2524 put_user(info_len, &uattr->info.info_len))
2525 return -EFAULT;
2526
2527 return 0;
2528}
2529
2530static int bpf_map_get_info_by_fd(struct bpf_map *map,
2531 const union bpf_attr *attr,
2532 union bpf_attr __user *uattr)
2533{
2534 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2535 struct bpf_map_info info = {};
2536 u32 info_len = attr->info.info_len;
2537 int err;
2538
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002539 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002540 if (err)
2541 return err;
2542 info_len = min_t(u32, sizeof(info), info_len);
2543
2544 info.type = map->map_type;
2545 info.id = map->id;
2546 info.key_size = map->key_size;
2547 info.value_size = map->value_size;
2548 info.max_entries = map->max_entries;
2549 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002550 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002551
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002552 if (map->btf) {
2553 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002554 info.btf_key_type_id = map->btf_key_type_id;
2555 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002556 }
2557
Jakub Kicinski52775b32018-01-17 19:13:28 -08002558 if (bpf_map_is_dev_bound(map)) {
2559 err = bpf_map_offload_info_fill(&info, map);
2560 if (err)
2561 return err;
2562 }
2563
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002564 if (copy_to_user(uinfo, &info, info_len) ||
2565 put_user(info_len, &uattr->info.info_len))
2566 return -EFAULT;
2567
2568 return 0;
2569}
2570
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002571static int bpf_btf_get_info_by_fd(struct btf *btf,
2572 const union bpf_attr *attr,
2573 union bpf_attr __user *uattr)
2574{
2575 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2576 u32 info_len = attr->info.info_len;
2577 int err;
2578
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002579 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002580 if (err)
2581 return err;
2582
2583 return btf_get_info_by_fd(btf, attr, uattr);
2584}
2585
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002586#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2587
2588static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2589 union bpf_attr __user *uattr)
2590{
2591 int ufd = attr->info.bpf_fd;
2592 struct fd f;
2593 int err;
2594
2595 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2596 return -EINVAL;
2597
2598 f = fdget(ufd);
2599 if (!f.file)
2600 return -EBADFD;
2601
2602 if (f.file->f_op == &bpf_prog_fops)
2603 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2604 uattr);
2605 else if (f.file->f_op == &bpf_map_fops)
2606 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2607 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002608 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002609 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002610 else
2611 err = -EINVAL;
2612
2613 fdput(f);
2614 return err;
2615}
2616
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002617#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2618
2619static int bpf_btf_load(const union bpf_attr *attr)
2620{
2621 if (CHECK_ATTR(BPF_BTF_LOAD))
2622 return -EINVAL;
2623
2624 if (!capable(CAP_SYS_ADMIN))
2625 return -EPERM;
2626
2627 return btf_new_fd(attr);
2628}
2629
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002630#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2631
2632static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2633{
2634 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2635 return -EINVAL;
2636
2637 if (!capable(CAP_SYS_ADMIN))
2638 return -EPERM;
2639
2640 return btf_get_fd_by_id(attr->btf_id);
2641}
2642
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002643static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2644 union bpf_attr __user *uattr,
2645 u32 prog_id, u32 fd_type,
2646 const char *buf, u64 probe_offset,
2647 u64 probe_addr)
2648{
2649 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2650 u32 len = buf ? strlen(buf) : 0, input_len;
2651 int err = 0;
2652
2653 if (put_user(len, &uattr->task_fd_query.buf_len))
2654 return -EFAULT;
2655 input_len = attr->task_fd_query.buf_len;
2656 if (input_len && ubuf) {
2657 if (!len) {
2658 /* nothing to copy, just make ubuf NULL terminated */
2659 char zero = '\0';
2660
2661 if (put_user(zero, ubuf))
2662 return -EFAULT;
2663 } else if (input_len >= len + 1) {
2664 /* ubuf can hold the string with NULL terminator */
2665 if (copy_to_user(ubuf, buf, len + 1))
2666 return -EFAULT;
2667 } else {
2668 /* ubuf cannot hold the string with NULL terminator,
2669 * do a partial copy with NULL terminator.
2670 */
2671 char zero = '\0';
2672
2673 err = -ENOSPC;
2674 if (copy_to_user(ubuf, buf, input_len - 1))
2675 return -EFAULT;
2676 if (put_user(zero, ubuf + input_len - 1))
2677 return -EFAULT;
2678 }
2679 }
2680
2681 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2682 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2683 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2684 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2685 return -EFAULT;
2686
2687 return err;
2688}
2689
2690#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2691
2692static int bpf_task_fd_query(const union bpf_attr *attr,
2693 union bpf_attr __user *uattr)
2694{
2695 pid_t pid = attr->task_fd_query.pid;
2696 u32 fd = attr->task_fd_query.fd;
2697 const struct perf_event *event;
2698 struct files_struct *files;
2699 struct task_struct *task;
2700 struct file *file;
2701 int err;
2702
2703 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2704 return -EINVAL;
2705
2706 if (!capable(CAP_SYS_ADMIN))
2707 return -EPERM;
2708
2709 if (attr->task_fd_query.flags != 0)
2710 return -EINVAL;
2711
2712 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2713 if (!task)
2714 return -ENOENT;
2715
2716 files = get_files_struct(task);
2717 put_task_struct(task);
2718 if (!files)
2719 return -ENOENT;
2720
2721 err = 0;
2722 spin_lock(&files->file_lock);
2723 file = fcheck_files(files, fd);
2724 if (!file)
2725 err = -EBADF;
2726 else
2727 get_file(file);
2728 spin_unlock(&files->file_lock);
2729 put_files_struct(files);
2730
2731 if (err)
2732 goto out;
2733
2734 if (file->f_op == &bpf_raw_tp_fops) {
2735 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2736 struct bpf_raw_event_map *btp = raw_tp->btp;
2737
2738 err = bpf_task_fd_query_copy(attr, uattr,
2739 raw_tp->prog->aux->id,
2740 BPF_FD_TYPE_RAW_TRACEPOINT,
2741 btp->tp->name, 0, 0);
2742 goto put_file;
2743 }
2744
2745 event = perf_get_event(file);
2746 if (!IS_ERR(event)) {
2747 u64 probe_offset, probe_addr;
2748 u32 prog_id, fd_type;
2749 const char *buf;
2750
2751 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2752 &buf, &probe_offset,
2753 &probe_addr);
2754 if (!err)
2755 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2756 fd_type, buf,
2757 probe_offset,
2758 probe_addr);
2759 goto put_file;
2760 }
2761
2762 err = -ENOTSUPP;
2763put_file:
2764 fput(file);
2765out:
2766 return err;
2767}
2768
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002769SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2770{
2771 union bpf_attr attr = {};
2772 int err;
2773
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002774 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002775 return -EPERM;
2776
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002777 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002778 if (err)
2779 return err;
2780 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002781
2782 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2783 if (copy_from_user(&attr, uattr, size) != 0)
2784 return -EFAULT;
2785
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002786 err = security_bpf(cmd, &attr, size);
2787 if (err < 0)
2788 return err;
2789
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002790 switch (cmd) {
2791 case BPF_MAP_CREATE:
2792 err = map_create(&attr);
2793 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002794 case BPF_MAP_LOOKUP_ELEM:
2795 err = map_lookup_elem(&attr);
2796 break;
2797 case BPF_MAP_UPDATE_ELEM:
2798 err = map_update_elem(&attr);
2799 break;
2800 case BPF_MAP_DELETE_ELEM:
2801 err = map_delete_elem(&attr);
2802 break;
2803 case BPF_MAP_GET_NEXT_KEY:
2804 err = map_get_next_key(&attr);
2805 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02002806 case BPF_MAP_FREEZE:
2807 err = map_freeze(&attr);
2808 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002809 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08002810 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002811 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002812 case BPF_OBJ_PIN:
2813 err = bpf_obj_pin(&attr);
2814 break;
2815 case BPF_OBJ_GET:
2816 err = bpf_obj_get(&attr);
2817 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002818 case BPF_PROG_ATTACH:
2819 err = bpf_prog_attach(&attr);
2820 break;
2821 case BPF_PROG_DETACH:
2822 err = bpf_prog_detach(&attr);
2823 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002824 case BPF_PROG_QUERY:
2825 err = bpf_prog_query(&attr, uattr);
2826 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002827 case BPF_PROG_TEST_RUN:
2828 err = bpf_prog_test_run(&attr, uattr);
2829 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002830 case BPF_PROG_GET_NEXT_ID:
2831 err = bpf_obj_get_next_id(&attr, uattr,
2832 &prog_idr, &prog_idr_lock);
2833 break;
2834 case BPF_MAP_GET_NEXT_ID:
2835 err = bpf_obj_get_next_id(&attr, uattr,
2836 &map_idr, &map_idr_lock);
2837 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002838 case BPF_PROG_GET_FD_BY_ID:
2839 err = bpf_prog_get_fd_by_id(&attr);
2840 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002841 case BPF_MAP_GET_FD_BY_ID:
2842 err = bpf_map_get_fd_by_id(&attr);
2843 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002844 case BPF_OBJ_GET_INFO_BY_FD:
2845 err = bpf_obj_get_info_by_fd(&attr, uattr);
2846 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002847 case BPF_RAW_TRACEPOINT_OPEN:
2848 err = bpf_raw_tracepoint_open(&attr);
2849 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002850 case BPF_BTF_LOAD:
2851 err = bpf_btf_load(&attr);
2852 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002853 case BPF_BTF_GET_FD_BY_ID:
2854 err = bpf_btf_get_fd_by_id(&attr);
2855 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002856 case BPF_TASK_FD_QUERY:
2857 err = bpf_task_fd_query(&attr, uattr);
2858 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02002859 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2860 err = map_lookup_and_delete_elem(&attr);
2861 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002862 default:
2863 err = -EINVAL;
2864 break;
2865 }
2866
2867 return err;
2868}