blob: 828518bb947bf442e25938b27cd8500ca7717ac1 [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
Jakub Kicinskibd475642018-01-11 20:29:06 -0800169void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
170{
171 map->map_type = attr->map_type;
172 map->key_size = attr->key_size;
173 map->value_size = attr->value_size;
174 map->max_entries = attr->max_entries;
175 map->map_flags = attr->map_flags;
176 map->numa_node = bpf_map_attr_numa_node(attr);
177}
178
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800179int bpf_map_precharge_memlock(u32 pages)
180{
181 struct user_struct *user = get_current_user();
182 unsigned long memlock_limit, cur;
183
184 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
185 cur = atomic_long_read(&user->locked_vm);
186 free_uid(user);
187 if (cur + pages > memlock_limit)
188 return -EPERM;
189 return 0;
190}
191
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700192static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700193{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700194 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700195
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700196 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
197 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700198 return -EPERM;
199 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700200 return 0;
201}
202
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700203static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
204{
205 atomic_long_sub(pages, &user->locked_vm);
206}
207
208static int bpf_map_init_memlock(struct bpf_map *map)
209{
210 struct user_struct *user = get_current_user();
211 int ret;
212
213 ret = bpf_charge_memlock(user, map->pages);
214 if (ret) {
215 free_uid(user);
216 return ret;
217 }
218 map->user = user;
219 return ret;
220}
221
222static void bpf_map_release_memlock(struct bpf_map *map)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700223{
224 struct user_struct *user = map->user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700225 bpf_uncharge_memlock(user, map->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700226 free_uid(user);
227}
228
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700229int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
230{
231 int ret;
232
233 ret = bpf_charge_memlock(map->user, pages);
234 if (ret)
235 return ret;
236 map->pages += pages;
237 return ret;
238}
239
240void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
241{
242 bpf_uncharge_memlock(map->user, pages);
243 map->pages -= pages;
244}
245
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700246static int bpf_map_alloc_id(struct bpf_map *map)
247{
248 int id;
249
Shaohua Lib76354c2018-03-27 11:53:21 -0700250 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700251 spin_lock_bh(&map_idr_lock);
252 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
253 if (id > 0)
254 map->id = id;
255 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700256 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700257
258 if (WARN_ON_ONCE(!id))
259 return -ENOSPC;
260
261 return id > 0 ? 0 : id;
262}
263
Jakub Kicinskia3884572018-01-11 20:29:09 -0800264void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700265{
Eric Dumazet930651a2017-09-19 09:15:59 -0700266 unsigned long flags;
267
Jakub Kicinskia3884572018-01-11 20:29:09 -0800268 /* Offloaded maps are removed from the IDR store when their device
269 * disappears - even if someone holds an fd to them they are unusable,
270 * the memory is gone, all ops will fail; they are simply waiting for
271 * refcnt to drop to be freed.
272 */
273 if (!map->id)
274 return;
275
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700276 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700277 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700278 else
279 __acquire(&map_idr_lock);
280
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700281 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800282 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700283
284 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700285 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700286 else
287 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700288}
289
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700290/* called from workqueue */
291static void bpf_map_free_deferred(struct work_struct *work)
292{
293 struct bpf_map *map = container_of(work, struct bpf_map, work);
294
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700295 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700296 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700297 /* implementation dependent freeing */
298 map->ops->map_free(map);
299}
300
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100301static void bpf_map_put_uref(struct bpf_map *map)
302{
303 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700304 if (map->ops->map_release_uref)
305 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100306 }
307}
308
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700309/* decrement map refcnt and schedule it for freeing via workqueue
310 * (unrelying map implementation ops->map_free() might sleep)
311 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700312static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700313{
314 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700315 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700316 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700317 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700318 INIT_WORK(&map->work, bpf_map_free_deferred);
319 schedule_work(&map->work);
320 }
321}
322
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700323void bpf_map_put(struct bpf_map *map)
324{
325 __bpf_map_put(map, true);
326}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700327EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700328
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100329void bpf_map_put_with_uref(struct bpf_map *map)
330{
331 bpf_map_put_uref(map);
332 bpf_map_put(map);
333}
334
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700335static int bpf_map_release(struct inode *inode, struct file *filp)
336{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200337 struct bpf_map *map = filp->private_data;
338
339 if (map->ops->map_release)
340 map->ops->map_release(map, filp);
341
342 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700343 return 0;
344}
345
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100346#ifdef CONFIG_PROC_FS
347static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
348{
349 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100350 const struct bpf_array *array;
351 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200352 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100353
354 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
355 array = container_of(map, struct bpf_array, map);
356 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200357 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100358 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100359
360 seq_printf(m,
361 "map_type:\t%u\n"
362 "key_size:\t%u\n"
363 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100364 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100365 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200366 "memlock:\t%llu\n"
367 "map_id:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100368 map->map_type,
369 map->key_size,
370 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100371 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100372 map->map_flags,
Daniel Borkmann4316b402018-06-02 23:06:34 +0200373 map->pages * 1ULL << PAGE_SHIFT,
374 map->id);
Daniel Borkmann21116b72016-11-26 01:28:07 +0100375
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200376 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100377 seq_printf(m, "owner_prog_type:\t%u\n",
378 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200379 seq_printf(m, "owner_jited:\t%u\n",
380 owner_jited);
381 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100382}
383#endif
384
Chenbo Feng6e71b042017-10-18 13:00:22 -0700385static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
386 loff_t *ppos)
387{
388 /* We need this handler such that alloc_file() enables
389 * f_mode with FMODE_CAN_READ.
390 */
391 return -EINVAL;
392}
393
394static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
395 size_t siz, loff_t *ppos)
396{
397 /* We need this handler such that alloc_file() enables
398 * f_mode with FMODE_CAN_WRITE.
399 */
400 return -EINVAL;
401}
402
Chenbo Fengf66e4482017-10-18 13:00:26 -0700403const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100404#ifdef CONFIG_PROC_FS
405 .show_fdinfo = bpf_map_show_fdinfo,
406#endif
407 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700408 .read = bpf_dummy_read,
409 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700410};
411
Chenbo Feng6e71b042017-10-18 13:00:22 -0700412int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100413{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700414 int ret;
415
416 ret = security_bpf_map(map, OPEN_FMODE(flags));
417 if (ret < 0)
418 return ret;
419
Daniel Borkmannaa797812015-10-29 14:58:06 +0100420 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700421 flags | O_CLOEXEC);
422}
423
424int bpf_get_file_flag(int flags)
425{
426 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
427 return -EINVAL;
428 if (flags & BPF_F_RDONLY)
429 return O_RDONLY;
430 if (flags & BPF_F_WRONLY)
431 return O_WRONLY;
432 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100433}
434
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700435/* helper macro to check that unused fields 'union bpf_attr' are zero */
436#define CHECK_ATTR(CMD) \
437 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
438 sizeof(attr->CMD##_LAST_FIELD), 0, \
439 sizeof(*attr) - \
440 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
441 sizeof(attr->CMD##_LAST_FIELD)) != NULL
442
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700443/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
444 * Return 0 on success and < 0 on error.
445 */
446static int bpf_obj_name_cpy(char *dst, const char *src)
447{
448 const char *end = src + BPF_OBJ_NAME_LEN;
449
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700450 memset(dst, 0, BPF_OBJ_NAME_LEN);
451
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700452 /* Copy all isalnum() and '_' char */
453 while (src < end && *src) {
454 if (!isalnum(*src) && *src != '_')
455 return -EINVAL;
456 *dst++ = *src++;
457 }
458
459 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
460 if (src == end)
461 return -EINVAL;
462
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700463 return 0;
464}
465
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200466int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800467 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200468 const struct btf_type *key_type,
469 const struct btf_type *value_type)
470{
471 return -ENOTSUPP;
472}
473
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800474static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200475 u32 btf_key_id, u32 btf_value_id)
476{
477 const struct btf_type *key_type, *value_type;
478 u32 key_size, value_size;
479 int ret = 0;
480
481 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
482 if (!key_type || key_size != map->key_size)
483 return -EINVAL;
484
485 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
486 if (!value_type || value_size != map->value_size)
487 return -EINVAL;
488
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800489 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
490
491 if (map_value_has_spin_lock(map)) {
492 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f1a2019-01-31 15:40:05 -0800493 map->map_type != BPF_MAP_TYPE_ARRAY &&
494 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800495 return -ENOTSUPP;
496 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
497 map->value_size) {
498 WARN_ONCE(1,
499 "verifier bug spin_lock_off %d value_size %d\n",
500 map->spin_lock_off, map->value_size);
501 return -EFAULT;
502 }
503 }
504
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200505 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800506 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200507
508 return ret;
509}
510
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700511#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700512/* called via syscall */
513static int map_create(union bpf_attr *attr)
514{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700515 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700516 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700517 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700518 int err;
519
520 err = CHECK_ATTR(BPF_MAP_CREATE);
521 if (err)
522 return -EINVAL;
523
Chenbo Feng6e71b042017-10-18 13:00:22 -0700524 f_flags = bpf_get_file_flag(attr->map_flags);
525 if (f_flags < 0)
526 return f_flags;
527
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700528 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700529 ((unsigned int)numa_node >= nr_node_ids ||
530 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700531 return -EINVAL;
532
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700533 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
534 map = find_and_alloc_map(attr);
535 if (IS_ERR(map))
536 return PTR_ERR(map);
537
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700538 err = bpf_obj_name_cpy(map->name, attr->map_name);
539 if (err)
540 goto free_map_nouncharge;
541
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700542 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100543 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700544
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200545 if (attr->btf_key_type_id || attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700546 struct btf *btf;
547
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700548 if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700549 err = -EINVAL;
550 goto free_map_nouncharge;
551 }
552
553 btf = btf_get_by_fd(attr->btf_fd);
554 if (IS_ERR(btf)) {
555 err = PTR_ERR(btf);
556 goto free_map_nouncharge;
557 }
558
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200559 err = map_check_btf(map, btf, attr->btf_key_type_id,
560 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700561 if (err) {
562 btf_put(btf);
563 goto free_map_nouncharge;
564 }
565
566 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700567 map->btf_key_type_id = attr->btf_key_type_id;
568 map->btf_value_type_id = attr->btf_value_type_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800569 } else {
570 map->spin_lock_off = -EINVAL;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700571 }
572
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700573 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700574 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100575 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700576
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700577 err = bpf_map_init_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700578 if (err)
579 goto free_map_sec;
580
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700581 err = bpf_map_alloc_id(map);
582 if (err)
583 goto free_map;
584
Chenbo Feng6e71b042017-10-18 13:00:22 -0700585 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700586 if (err < 0) {
587 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800588 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700589 * bpf_map_alloc_id() has published the map
590 * to the userspace and the userspace may
591 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
592 */
Peng Sun352d20d2019-02-27 22:36:25 +0800593 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700594 return err;
595 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700596
597 return err;
598
599free_map:
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700600 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700601free_map_sec:
602 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100603free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700604 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700605 map->ops->map_free(map);
606 return err;
607}
608
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700609/* if error is returned, fd is released.
610 * On success caller should complete fd access with matching fdput()
611 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100612struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700613{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700614 if (!f.file)
615 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700616 if (f.file->f_op != &bpf_map_fops) {
617 fdput(f);
618 return ERR_PTR(-EINVAL);
619 }
620
Daniel Borkmannc2101292015-10-29 14:58:07 +0100621 return f.file->private_data;
622}
623
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700624/* prog's and map's refcnt limit */
625#define BPF_MAX_REFCNT 32768
626
627struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100628{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700629 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
630 atomic_dec(&map->refcnt);
631 return ERR_PTR(-EBUSY);
632 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100633 if (uref)
634 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700635 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100636}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700637EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100638
639struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100640{
641 struct fd f = fdget(ufd);
642 struct bpf_map *map;
643
644 map = __bpf_map_get(f);
645 if (IS_ERR(map))
646 return map;
647
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700648 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100649 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700650
651 return map;
652}
653
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700654/* map_idr_lock should have been held */
655static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
656 bool uref)
657{
658 int refold;
659
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100660 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700661
662 if (refold >= BPF_MAX_REFCNT) {
663 __bpf_map_put(map, false);
664 return ERR_PTR(-EBUSY);
665 }
666
667 if (!refold)
668 return ERR_PTR(-ENOENT);
669
670 if (uref)
671 atomic_inc(&map->usercnt);
672
673 return map;
674}
675
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800676int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
677{
678 return -ENOTSUPP;
679}
680
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200681static void *__bpf_copy_key(void __user *ukey, u64 key_size)
682{
683 if (key_size)
684 return memdup_user(ukey, key_size);
685
686 if (ukey)
687 return ERR_PTR(-EINVAL);
688
689 return NULL;
690}
691
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700692/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800693#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700694
695static int map_lookup_elem(union bpf_attr *attr)
696{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100697 void __user *ukey = u64_to_user_ptr(attr->key);
698 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700699 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700700 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800701 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800702 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200703 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700704 int err;
705
706 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
707 return -EINVAL;
708
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800709 if (attr->flags & ~BPF_F_LOCK)
710 return -EINVAL;
711
Daniel Borkmann592867b2015-09-08 18:00:09 +0200712 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100713 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700714 if (IS_ERR(map))
715 return PTR_ERR(map);
716
Chenbo Feng6e71b042017-10-18 13:00:22 -0700717 if (!(f.file->f_mode & FMODE_CAN_READ)) {
718 err = -EPERM;
719 goto err_put;
720 }
721
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800722 if ((attr->flags & BPF_F_LOCK) &&
723 !map_value_has_spin_lock(map)) {
724 err = -EINVAL;
725 goto err_put;
726 }
727
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200728 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400729 if (IS_ERR(key)) {
730 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700731 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400732 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700733
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800734 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800735 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000736 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
737 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800738 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700739 else if (IS_FD_MAP(map))
740 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800741 else
742 value_size = map->value_size;
743
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800744 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800745 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700746 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800747 goto free_key;
748
Jakub Kicinskia3884572018-01-11 20:29:09 -0800749 if (bpf_map_is_dev_bound(map)) {
750 err = bpf_map_offload_lookup_elem(map, key, value);
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800751 goto done;
752 }
753
754 preempt_disable();
755 this_cpu_inc(bpf_prog_active);
756 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
757 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800758 err = bpf_percpu_hash_copy(map, key, value);
759 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
760 err = bpf_percpu_array_copy(map, key, value);
Roman Gushchinb741f162018-09-28 14:45:43 +0000761 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
762 err = bpf_percpu_cgroup_storage_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800763 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
764 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700765 } else if (IS_FD_ARRAY(map)) {
766 err = bpf_fd_array_map_lookup_elem(map, key, value);
767 } else if (IS_FD_HASH(map)) {
768 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700769 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
770 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200771 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
772 map->map_type == BPF_MAP_TYPE_STACK) {
773 err = map->ops->map_peek_elem(map, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800774 } else {
775 rcu_read_lock();
776 ptr = map->ops->map_lookup_elem(map, key);
Prashant Bhole509db282018-10-09 10:04:49 +0900777 if (IS_ERR(ptr)) {
778 err = PTR_ERR(ptr);
779 } else if (!ptr) {
780 err = -ENOENT;
781 } else {
782 err = 0;
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800783 if (attr->flags & BPF_F_LOCK)
784 /* lock 'ptr' and copy everything but lock */
785 copy_map_value_locked(map, value, ptr, true);
786 else
787 copy_map_value(map, value, ptr);
788 /* mask lock, since value wasn't zero inited */
789 check_and_init_map_lock(map, value);
Prashant Bhole509db282018-10-09 10:04:49 +0900790 }
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800791 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800792 }
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800793 this_cpu_dec(bpf_prog_active);
794 preempt_enable();
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800795
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800796done:
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800797 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800798 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700799
800 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800801 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800802 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700803
804 err = 0;
805
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800806free_value:
807 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700808free_key:
809 kfree(key);
810err_put:
811 fdput(f);
812 return err;
813}
814
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700815static void maybe_wait_bpf_programs(struct bpf_map *map)
816{
817 /* Wait for any running BPF programs to complete so that
818 * userspace, when we return to it, knows that all programs
819 * that could be running use the new map value.
820 */
821 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
822 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
823 synchronize_rcu();
824}
825
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800826#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700827
828static int map_update_elem(union bpf_attr *attr)
829{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100830 void __user *ukey = u64_to_user_ptr(attr->key);
831 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700832 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700833 struct bpf_map *map;
834 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800835 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200836 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700837 int err;
838
839 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
840 return -EINVAL;
841
Daniel Borkmann592867b2015-09-08 18:00:09 +0200842 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100843 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700844 if (IS_ERR(map))
845 return PTR_ERR(map);
846
Chenbo Feng6e71b042017-10-18 13:00:22 -0700847 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
848 err = -EPERM;
849 goto err_put;
850 }
851
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800852 if ((attr->flags & BPF_F_LOCK) &&
853 !map_value_has_spin_lock(map)) {
854 err = -EINVAL;
855 goto err_put;
856 }
857
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200858 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400859 if (IS_ERR(key)) {
860 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700861 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400862 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700863
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800864 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800865 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000866 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
867 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800868 value_size = round_up(map->value_size, 8) * num_possible_cpus();
869 else
870 value_size = map->value_size;
871
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700872 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800873 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700874 if (!value)
875 goto free_key;
876
877 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800878 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700879 goto free_value;
880
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200881 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800882 if (bpf_map_is_dev_bound(map)) {
883 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
884 goto out;
John Fastabend99ba2b52018-07-05 08:50:04 -0700885 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
886 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
887 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200888 err = map->ops->map_update_elem(map, key, value, attr->flags);
889 goto out;
890 }
891
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800892 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
893 * inside bpf map update or delete otherwise deadlocks are possible
894 */
895 preempt_disable();
896 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800897 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
898 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800899 err = bpf_percpu_hash_update(map, key, value, attr->flags);
900 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
901 err = bpf_percpu_array_update(map, key, value, attr->flags);
Roman Gushchinb741f162018-09-28 14:45:43 +0000902 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
903 err = bpf_percpu_cgroup_storage_update(map, key, value,
904 attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100905 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200906 rcu_read_lock();
907 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
908 attr->flags);
909 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700910 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
911 rcu_read_lock();
912 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
913 attr->flags);
914 rcu_read_unlock();
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700915 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
916 /* rcu_read_lock() is not needed */
917 err = bpf_fd_reuseport_array_update_elem(map, key, value,
918 attr->flags);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200919 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
920 map->map_type == BPF_MAP_TYPE_STACK) {
921 err = map->ops->map_push_elem(map, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800922 } else {
923 rcu_read_lock();
924 err = map->ops->map_update_elem(map, key, value, attr->flags);
925 rcu_read_unlock();
926 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800927 __this_cpu_dec(bpf_prog_active);
928 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700929 maybe_wait_bpf_programs(map);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200930out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700931free_value:
932 kfree(value);
933free_key:
934 kfree(key);
935err_put:
936 fdput(f);
937 return err;
938}
939
940#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
941
942static int map_delete_elem(union bpf_attr *attr)
943{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100944 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700945 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700946 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200947 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700948 void *key;
949 int err;
950
951 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
952 return -EINVAL;
953
Daniel Borkmann592867b2015-09-08 18:00:09 +0200954 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100955 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700956 if (IS_ERR(map))
957 return PTR_ERR(map);
958
Chenbo Feng6e71b042017-10-18 13:00:22 -0700959 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
960 err = -EPERM;
961 goto err_put;
962 }
963
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200964 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400965 if (IS_ERR(key)) {
966 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700967 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400968 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700969
Jakub Kicinskia3884572018-01-11 20:29:09 -0800970 if (bpf_map_is_dev_bound(map)) {
971 err = bpf_map_offload_delete_elem(map, key);
972 goto out;
973 }
974
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800975 preempt_disable();
976 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700977 rcu_read_lock();
978 err = map->ops->map_delete_elem(map, key);
979 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800980 __this_cpu_dec(bpf_prog_active);
981 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700982 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800983out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700984 kfree(key);
985err_put:
986 fdput(f);
987 return err;
988}
989
990/* last field in 'union bpf_attr' used by this command */
991#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
992
993static int map_get_next_key(union bpf_attr *attr)
994{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100995 void __user *ukey = u64_to_user_ptr(attr->key);
996 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700997 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700998 struct bpf_map *map;
999 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001000 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001001 int err;
1002
1003 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1004 return -EINVAL;
1005
Daniel Borkmann592867b2015-09-08 18:00:09 +02001006 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001007 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001008 if (IS_ERR(map))
1009 return PTR_ERR(map);
1010
Chenbo Feng6e71b042017-10-18 13:00:22 -07001011 if (!(f.file->f_mode & FMODE_CAN_READ)) {
1012 err = -EPERM;
1013 goto err_put;
1014 }
1015
Teng Qin8fe45922017-04-24 19:00:37 -07001016 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001017 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001018 if (IS_ERR(key)) {
1019 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001020 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001021 }
Teng Qin8fe45922017-04-24 19:00:37 -07001022 } else {
1023 key = NULL;
1024 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001025
1026 err = -ENOMEM;
1027 next_key = kmalloc(map->key_size, GFP_USER);
1028 if (!next_key)
1029 goto free_key;
1030
Jakub Kicinskia3884572018-01-11 20:29:09 -08001031 if (bpf_map_is_dev_bound(map)) {
1032 err = bpf_map_offload_get_next_key(map, key, next_key);
1033 goto out;
1034 }
1035
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001036 rcu_read_lock();
1037 err = map->ops->map_get_next_key(map, key, next_key);
1038 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001039out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001040 if (err)
1041 goto free_next_key;
1042
1043 err = -EFAULT;
1044 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1045 goto free_next_key;
1046
1047 err = 0;
1048
1049free_next_key:
1050 kfree(next_key);
1051free_key:
1052 kfree(key);
1053err_put:
1054 fdput(f);
1055 return err;
1056}
1057
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001058#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1059
1060static int map_lookup_and_delete_elem(union bpf_attr *attr)
1061{
1062 void __user *ukey = u64_to_user_ptr(attr->key);
1063 void __user *uvalue = u64_to_user_ptr(attr->value);
1064 int ufd = attr->map_fd;
1065 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001066 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001067 u32 value_size;
1068 struct fd f;
1069 int err;
1070
1071 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1072 return -EINVAL;
1073
1074 f = fdget(ufd);
1075 map = __bpf_map_get(f);
1076 if (IS_ERR(map))
1077 return PTR_ERR(map);
1078
1079 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
1080 err = -EPERM;
1081 goto err_put;
1082 }
1083
1084 key = __bpf_copy_key(ukey, map->key_size);
1085 if (IS_ERR(key)) {
1086 err = PTR_ERR(key);
1087 goto err_put;
1088 }
1089
1090 value_size = map->value_size;
1091
1092 err = -ENOMEM;
1093 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1094 if (!value)
1095 goto free_key;
1096
1097 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1098 map->map_type == BPF_MAP_TYPE_STACK) {
1099 err = map->ops->map_pop_elem(map, value);
1100 } else {
1101 err = -ENOTSUPP;
1102 }
1103
1104 if (err)
1105 goto free_value;
1106
1107 if (copy_to_user(uvalue, value, value_size) != 0)
1108 goto free_value;
1109
1110 err = 0;
1111
1112free_value:
1113 kfree(value);
1114free_key:
1115 kfree(key);
1116err_put:
1117 fdput(f);
1118 return err;
1119}
1120
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001121static const struct bpf_prog_ops * const bpf_prog_types[] = {
1122#define BPF_PROG_TYPE(_id, _name) \
1123 [_id] = & _name ## _prog_ops,
1124#define BPF_MAP_TYPE(_id, _ops)
1125#include <linux/bpf_types.h>
1126#undef BPF_PROG_TYPE
1127#undef BPF_MAP_TYPE
1128};
1129
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001130static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1131{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001132 const struct bpf_prog_ops *ops;
1133
1134 if (type >= ARRAY_SIZE(bpf_prog_types))
1135 return -EINVAL;
1136 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1137 ops = bpf_prog_types[type];
1138 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001139 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001140
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001141 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001142 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001143 else
1144 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001145 prog->type = type;
1146 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001147}
1148
1149/* drop refcnt on maps used by eBPF program and free auxilary data */
1150static void free_used_maps(struct bpf_prog_aux *aux)
1151{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001152 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001153 int i;
1154
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001155 for_each_cgroup_storage_type(stype) {
1156 if (!aux->cgroup_storage[stype])
1157 continue;
1158 bpf_cgroup_storage_release(aux->prog,
1159 aux->cgroup_storage[stype]);
1160 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07001161
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001162 for (i = 0; i < aux->used_map_cnt; i++)
1163 bpf_map_put(aux->used_maps[i]);
1164
1165 kfree(aux->used_maps);
1166}
1167
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001168int __bpf_prog_charge(struct user_struct *user, u32 pages)
1169{
1170 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1171 unsigned long user_bufs;
1172
1173 if (user) {
1174 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1175 if (user_bufs > memlock_limit) {
1176 atomic_long_sub(pages, &user->locked_vm);
1177 return -EPERM;
1178 }
1179 }
1180
1181 return 0;
1182}
1183
1184void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1185{
1186 if (user)
1187 atomic_long_sub(pages, &user->locked_vm);
1188}
1189
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001190static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1191{
1192 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001193 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001194
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001195 ret = __bpf_prog_charge(user, prog->pages);
1196 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001197 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001198 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001199 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001200
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001201 prog->aux->user = user;
1202 return 0;
1203}
1204
1205static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1206{
1207 struct user_struct *user = prog->aux->user;
1208
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001209 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001210 free_uid(user);
1211}
1212
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001213static int bpf_prog_alloc_id(struct bpf_prog *prog)
1214{
1215 int id;
1216
Shaohua Lib76354c2018-03-27 11:53:21 -07001217 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001218 spin_lock_bh(&prog_idr_lock);
1219 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1220 if (id > 0)
1221 prog->aux->id = id;
1222 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001223 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001224
1225 /* id is in [1, INT_MAX) */
1226 if (WARN_ON_ONCE(!id))
1227 return -ENOSPC;
1228
1229 return id > 0 ? 0 : id;
1230}
1231
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001232void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001233{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001234 /* cBPF to eBPF migrations are currently not in the idr store.
1235 * Offloaded programs are removed from the store when their device
1236 * disappears - even if someone grabs an fd to them they are unusable,
1237 * simply waiting for refcnt to drop to be freed.
1238 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001239 if (!prog->aux->id)
1240 return;
1241
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001242 if (do_idr_lock)
1243 spin_lock_bh(&prog_idr_lock);
1244 else
1245 __acquire(&prog_idr_lock);
1246
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001247 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001248 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001249
1250 if (do_idr_lock)
1251 spin_unlock_bh(&prog_idr_lock);
1252 else
1253 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001254}
1255
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001256static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001257{
1258 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1259
1260 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001261 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001262 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001263 bpf_prog_free(aux->prog);
1264}
1265
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001266static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001267{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001268 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001269 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001270 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001271 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001272 bpf_prog_kallsyms_del_all(prog);
Yonghong Song838e9692018-11-19 15:29:11 -08001273 btf_put(prog->aux->btf);
Yonghong Songba64e7d2018-11-24 23:20:44 -08001274 kvfree(prog->aux->func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001275 bpf_prog_free_linfo(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001276
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001277 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001278 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001279}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001280
1281void bpf_prog_put(struct bpf_prog *prog)
1282{
1283 __bpf_prog_put(prog, true);
1284}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001285EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001286
1287static int bpf_prog_release(struct inode *inode, struct file *filp)
1288{
1289 struct bpf_prog *prog = filp->private_data;
1290
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001291 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001292 return 0;
1293}
1294
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001295static void bpf_prog_get_stats(const struct bpf_prog *prog,
1296 struct bpf_prog_stats *stats)
1297{
1298 u64 nsecs = 0, cnt = 0;
1299 int cpu;
1300
1301 for_each_possible_cpu(cpu) {
1302 const struct bpf_prog_stats *st;
1303 unsigned int start;
1304 u64 tnsecs, tcnt;
1305
1306 st = per_cpu_ptr(prog->aux->stats, cpu);
1307 do {
1308 start = u64_stats_fetch_begin_irq(&st->syncp);
1309 tnsecs = st->nsecs;
1310 tcnt = st->cnt;
1311 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1312 nsecs += tnsecs;
1313 cnt += tcnt;
1314 }
1315 stats->nsecs = nsecs;
1316 stats->cnt = cnt;
1317}
1318
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001319#ifdef CONFIG_PROC_FS
1320static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1321{
1322 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001323 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001324 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001325
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001326 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001327 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001328 seq_printf(m,
1329 "prog_type:\t%u\n"
1330 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001331 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001332 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001333 "prog_id:\t%u\n"
1334 "run_time_ns:\t%llu\n"
1335 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001336 prog->type,
1337 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001338 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001339 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001340 prog->aux->id,
1341 stats.nsecs,
1342 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001343}
1344#endif
1345
Chenbo Fengf66e4482017-10-18 13:00:26 -07001346const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001347#ifdef CONFIG_PROC_FS
1348 .show_fdinfo = bpf_prog_show_fdinfo,
1349#endif
1350 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001351 .read = bpf_dummy_read,
1352 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001353};
1354
Daniel Borkmannb2197752015-10-29 14:58:09 +01001355int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001356{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001357 int ret;
1358
1359 ret = security_bpf_prog(prog);
1360 if (ret < 0)
1361 return ret;
1362
Daniel Borkmannaa797812015-10-29 14:58:06 +01001363 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1364 O_RDWR | O_CLOEXEC);
1365}
1366
Daniel Borkmann113214b2016-06-30 17:24:44 +02001367static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001368{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001369 if (!f.file)
1370 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001371 if (f.file->f_op != &bpf_prog_fops) {
1372 fdput(f);
1373 return ERR_PTR(-EINVAL);
1374 }
1375
Daniel Borkmannc2101292015-10-29 14:58:07 +01001376 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001377}
1378
Brenden Blanco59d36562016-07-19 12:16:46 -07001379struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001380{
Brenden Blanco59d36562016-07-19 12:16:46 -07001381 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1382 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001383 return ERR_PTR(-EBUSY);
1384 }
1385 return prog;
1386}
Brenden Blanco59d36562016-07-19 12:16:46 -07001387EXPORT_SYMBOL_GPL(bpf_prog_add);
1388
Daniel Borkmannc5405942016-11-09 22:02:34 +01001389void bpf_prog_sub(struct bpf_prog *prog, int i)
1390{
1391 /* Only to be used for undoing previous bpf_prog_add() in some
1392 * error path. We still know that another entity in our call
1393 * path holds a reference to the program, thus atomic_sub() can
1394 * be safely used in such cases!
1395 */
1396 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1397}
1398EXPORT_SYMBOL_GPL(bpf_prog_sub);
1399
Brenden Blanco59d36562016-07-19 12:16:46 -07001400struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1401{
1402 return bpf_prog_add(prog, 1);
1403}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001404EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001405
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001406/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001407struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001408{
1409 int refold;
1410
Mark Rutlandbfc18e32018-06-21 13:13:04 +01001411 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001412
1413 if (refold >= BPF_MAX_REFCNT) {
1414 __bpf_prog_put(prog, false);
1415 return ERR_PTR(-EBUSY);
1416 }
1417
1418 if (!refold)
1419 return ERR_PTR(-ENOENT);
1420
1421 return prog;
1422}
John Fastabenda6f6df62017-08-15 22:32:22 -07001423EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001424
Al Viro040ee692017-12-02 20:20:38 -05001425bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001426 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001427{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001428 /* not an attachment, just a refcount inc, always allow */
1429 if (!attach_type)
1430 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001431
1432 if (prog->type != *attach_type)
1433 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001434 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001435 return false;
1436
1437 return true;
1438}
1439
1440static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001441 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001442{
1443 struct fd f = fdget(ufd);
1444 struct bpf_prog *prog;
1445
Daniel Borkmann113214b2016-06-30 17:24:44 +02001446 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001447 if (IS_ERR(prog))
1448 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001449 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001450 prog = ERR_PTR(-EINVAL);
1451 goto out;
1452 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001453
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001454 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001455out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001456 fdput(f);
1457 return prog;
1458}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001459
1460struct bpf_prog *bpf_prog_get(u32 ufd)
1461{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001462 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001463}
1464
Jakub Kicinski248f3462017-11-03 13:56:20 -07001465struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001466 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001467{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001468 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001469}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001470EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001471
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001472/* Initially all BPF programs could be loaded w/o specifying
1473 * expected_attach_type. Later for some of them specifying expected_attach_type
1474 * at load time became required so that program could be validated properly.
1475 * Programs of types that are allowed to be loaded both w/ and w/o (for
1476 * backward compatibility) expected_attach_type, should have the default attach
1477 * type assigned to expected_attach_type for the latter case, so that it can be
1478 * validated later at attach time.
1479 *
1480 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1481 * prog type requires it but has some attach types that have to be backward
1482 * compatible.
1483 */
1484static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1485{
1486 switch (attr->prog_type) {
1487 case BPF_PROG_TYPE_CGROUP_SOCK:
1488 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1489 * exist so checking for non-zero is the way to go here.
1490 */
1491 if (!attr->expected_attach_type)
1492 attr->expected_attach_type =
1493 BPF_CGROUP_INET_SOCK_CREATE;
1494 break;
1495 }
1496}
1497
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001498static int
1499bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1500 enum bpf_attach_type expected_attach_type)
1501{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001502 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001503 case BPF_PROG_TYPE_CGROUP_SOCK:
1504 switch (expected_attach_type) {
1505 case BPF_CGROUP_INET_SOCK_CREATE:
1506 case BPF_CGROUP_INET4_POST_BIND:
1507 case BPF_CGROUP_INET6_POST_BIND:
1508 return 0;
1509 default:
1510 return -EINVAL;
1511 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001512 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1513 switch (expected_attach_type) {
1514 case BPF_CGROUP_INET4_BIND:
1515 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001516 case BPF_CGROUP_INET4_CONNECT:
1517 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001518 case BPF_CGROUP_UDP4_SENDMSG:
1519 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001520 return 0;
1521 default:
1522 return -EINVAL;
1523 }
1524 default:
1525 return 0;
1526 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001527}
1528
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001529/* last field in 'union bpf_attr' used by this command */
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001530#define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001531
Yonghong Song838e9692018-11-19 15:29:11 -08001532static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001533{
1534 enum bpf_prog_type type = attr->prog_type;
1535 struct bpf_prog *prog;
1536 int err;
1537 char license[128];
1538 bool is_gpl;
1539
1540 if (CHECK_ATTR(BPF_PROG_LOAD))
1541 return -EINVAL;
1542
David Millere9ee9ef2018-11-30 21:08:14 -08001543 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT))
David S. Millere07b98d2017-05-10 11:38:07 -07001544 return -EINVAL;
1545
David Millere9ee9ef2018-11-30 21:08:14 -08001546 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1547 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1548 !capable(CAP_SYS_ADMIN))
1549 return -EPERM;
1550
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001551 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001552 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001553 sizeof(license) - 1) < 0)
1554 return -EFAULT;
1555 license[sizeof(license) - 1] = 0;
1556
1557 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1558 is_gpl = license_is_gpl_compatible(license);
1559
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07001560 if (attr->insn_cnt == 0 ||
1561 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001562 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07001563 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1564 type != BPF_PROG_TYPE_CGROUP_SKB &&
1565 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001566 return -EPERM;
1567
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001568 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001569 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1570 return -EINVAL;
1571
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001572 /* plain bpf_prog allocation */
1573 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1574 if (!prog)
1575 return -ENOMEM;
1576
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001577 prog->expected_attach_type = attr->expected_attach_type;
1578
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001579 prog->aux->offload_requested = !!attr->prog_ifindex;
1580
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001581 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001582 if (err)
1583 goto free_prog_nouncharge;
1584
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001585 err = bpf_prog_charge_memlock(prog);
1586 if (err)
1587 goto free_prog_sec;
1588
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001589 prog->len = attr->insn_cnt;
1590
1591 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001592 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001593 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001594 goto free_prog;
1595
1596 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001597 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001598
1599 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001600 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001601
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001602 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001603 err = bpf_prog_offload_init(prog, attr);
1604 if (err)
1605 goto free_prog;
1606 }
1607
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001608 /* find program type: socket_filter vs tracing_filter */
1609 err = find_prog_type(type, prog);
1610 if (err < 0)
1611 goto free_prog;
1612
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001613 prog->aux->load_time = ktime_get_boot_ns();
1614 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1615 if (err)
1616 goto free_prog;
1617
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001618 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08001619 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001620 if (err < 0)
1621 goto free_used_maps;
1622
Daniel Borkmann9facc332018-06-15 02:30:48 +02001623 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001624 if (err < 0)
1625 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001626
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001627 err = bpf_prog_alloc_id(prog);
1628 if (err)
1629 goto free_used_maps;
1630
Daniel Borkmannaa797812015-10-29 14:58:06 +01001631 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001632 if (err < 0) {
1633 /* failed to allocate fd.
1634 * bpf_prog_put() is needed because the above
1635 * bpf_prog_alloc_id() has published the prog
1636 * to the userspace and the userspace may
1637 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1638 */
1639 bpf_prog_put(prog);
1640 return err;
1641 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001642
Daniel Borkmann74451e662017-02-16 22:24:50 +01001643 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08001644 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001645 return err;
1646
1647free_used_maps:
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001648 bpf_prog_free_linfo(prog);
Martin KaFai Lau5482e9a2018-12-01 17:08:44 -08001649 kvfree(prog->aux->func_info);
1650 btf_put(prog->aux->btf);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001651 bpf_prog_kallsyms_del_subprogs(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001652 free_used_maps(prog->aux);
1653free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001654 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001655free_prog_sec:
1656 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001657free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001658 bpf_prog_free(prog);
1659 return err;
1660}
1661
Chenbo Feng6e71b042017-10-18 13:00:22 -07001662#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001663
1664static int bpf_obj_pin(const union bpf_attr *attr)
1665{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001666 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001667 return -EINVAL;
1668
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001669 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001670}
1671
1672static int bpf_obj_get(const union bpf_attr *attr)
1673{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001674 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1675 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001676 return -EINVAL;
1677
Chenbo Feng6e71b042017-10-18 13:00:22 -07001678 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1679 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001680}
1681
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001682struct bpf_raw_tracepoint {
1683 struct bpf_raw_event_map *btp;
1684 struct bpf_prog *prog;
1685};
1686
1687static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1688{
1689 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1690
1691 if (raw_tp->prog) {
1692 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1693 bpf_prog_put(raw_tp->prog);
1694 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001695 bpf_put_raw_tracepoint(raw_tp->btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001696 kfree(raw_tp);
1697 return 0;
1698}
1699
1700static const struct file_operations bpf_raw_tp_fops = {
1701 .release = bpf_raw_tracepoint_release,
1702 .read = bpf_dummy_read,
1703 .write = bpf_dummy_write,
1704};
1705
1706#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1707
1708static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1709{
1710 struct bpf_raw_tracepoint *raw_tp;
1711 struct bpf_raw_event_map *btp;
1712 struct bpf_prog *prog;
1713 char tp_name[128];
1714 int tp_fd, err;
1715
1716 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1717 sizeof(tp_name) - 1) < 0)
1718 return -EFAULT;
1719 tp_name[sizeof(tp_name) - 1] = 0;
1720
Matt Mullinsa38d1102018-12-12 16:42:37 -08001721 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001722 if (!btp)
1723 return -ENOENT;
1724
1725 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001726 if (!raw_tp) {
1727 err = -ENOMEM;
1728 goto out_put_btp;
1729 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001730 raw_tp->btp = btp;
1731
1732 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1733 BPF_PROG_TYPE_RAW_TRACEPOINT);
1734 if (IS_ERR(prog)) {
1735 err = PTR_ERR(prog);
1736 goto out_free_tp;
1737 }
1738
1739 err = bpf_probe_register(raw_tp->btp, prog);
1740 if (err)
1741 goto out_put_prog;
1742
1743 raw_tp->prog = prog;
1744 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1745 O_CLOEXEC);
1746 if (tp_fd < 0) {
1747 bpf_probe_unregister(raw_tp->btp, prog);
1748 err = tp_fd;
1749 goto out_put_prog;
1750 }
1751 return tp_fd;
1752
1753out_put_prog:
1754 bpf_prog_put(prog);
1755out_free_tp:
1756 kfree(raw_tp);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001757out_put_btp:
1758 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001759 return err;
1760}
1761
Anders Roxell33491582018-04-03 14:09:47 +02001762static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1763 enum bpf_attach_type attach_type)
1764{
1765 switch (prog->type) {
1766 case BPF_PROG_TYPE_CGROUP_SOCK:
1767 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1768 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1769 default:
1770 return 0;
1771 }
1772}
1773
John Fastabend464bc0f2017-08-28 07:10:04 -07001774#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001775
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001776#define BPF_F_ATTACH_MASK \
1777 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1778
Daniel Mackf4324552016-11-23 16:52:27 +01001779static int bpf_prog_attach(const union bpf_attr *attr)
1780{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001781 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001782 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001783 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001784
1785 if (!capable(CAP_NET_ADMIN))
1786 return -EPERM;
1787
1788 if (CHECK_ATTR(BPF_PROG_ATTACH))
1789 return -EINVAL;
1790
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001791 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001792 return -EINVAL;
1793
Daniel Mackf4324552016-11-23 16:52:27 +01001794 switch (attr->attach_type) {
1795 case BPF_CGROUP_INET_INGRESS:
1796 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001797 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001798 break;
David Ahern610236582016-12-01 08:48:04 -08001799 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001800 case BPF_CGROUP_INET4_POST_BIND:
1801 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001802 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1803 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001804 case BPF_CGROUP_INET4_BIND:
1805 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001806 case BPF_CGROUP_INET4_CONNECT:
1807 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001808 case BPF_CGROUP_UDP4_SENDMSG:
1809 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001810 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1811 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001812 case BPF_CGROUP_SOCK_OPS:
1813 ptype = BPF_PROG_TYPE_SOCK_OPS;
1814 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001815 case BPF_CGROUP_DEVICE:
1816 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1817 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001818 case BPF_SK_MSG_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001819 ptype = BPF_PROG_TYPE_SK_MSG;
1820 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001821 case BPF_SK_SKB_STREAM_PARSER:
1822 case BPF_SK_SKB_STREAM_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001823 ptype = BPF_PROG_TYPE_SK_SKB;
1824 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01001825 case BPF_LIRC_MODE2:
Sean Youngfdb5c452018-06-19 00:04:24 +01001826 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1827 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001828 case BPF_FLOW_DISSECTOR:
1829 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1830 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001831 default:
1832 return -EINVAL;
1833 }
1834
David Ahernb2cd1252016-12-01 08:48:03 -08001835 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1836 if (IS_ERR(prog))
1837 return PTR_ERR(prog);
1838
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001839 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1840 bpf_prog_put(prog);
1841 return -EINVAL;
1842 }
1843
Sean Youngfdb5c452018-06-19 00:04:24 +01001844 switch (ptype) {
1845 case BPF_PROG_TYPE_SK_SKB:
1846 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001847 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01001848 break;
1849 case BPF_PROG_TYPE_LIRC_MODE2:
1850 ret = lirc_prog_attach(attr, prog);
1851 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001852 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1853 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1854 break;
Sean Youngfdb5c452018-06-19 00:04:24 +01001855 default:
1856 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001857 }
1858
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001859 if (ret)
1860 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001861 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001862}
1863
1864#define BPF_PROG_DETACH_LAST_FIELD attach_type
1865
1866static int bpf_prog_detach(const union bpf_attr *attr)
1867{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001868 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001869
1870 if (!capable(CAP_NET_ADMIN))
1871 return -EPERM;
1872
1873 if (CHECK_ATTR(BPF_PROG_DETACH))
1874 return -EINVAL;
1875
1876 switch (attr->attach_type) {
1877 case BPF_CGROUP_INET_INGRESS:
1878 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001879 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1880 break;
David Ahern610236582016-12-01 08:48:04 -08001881 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001882 case BPF_CGROUP_INET4_POST_BIND:
1883 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001884 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1885 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001886 case BPF_CGROUP_INET4_BIND:
1887 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001888 case BPF_CGROUP_INET4_CONNECT:
1889 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001890 case BPF_CGROUP_UDP4_SENDMSG:
1891 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001892 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1893 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001894 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001895 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001896 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001897 case BPF_CGROUP_DEVICE:
1898 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1899 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001900 case BPF_SK_MSG_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001901 return sock_map_get_from_fd(attr, NULL);
John Fastabend5a67da22017-09-08 14:00:49 -07001902 case BPF_SK_SKB_STREAM_PARSER:
1903 case BPF_SK_SKB_STREAM_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001904 return sock_map_get_from_fd(attr, NULL);
Sean Youngf4364dc2018-05-27 12:24:09 +01001905 case BPF_LIRC_MODE2:
1906 return lirc_prog_detach(attr);
Petar Penkovd58e4682018-09-14 07:46:18 -07001907 case BPF_FLOW_DISSECTOR:
1908 return skb_flow_dissector_bpf_prog_detach(attr);
Daniel Mackf4324552016-11-23 16:52:27 +01001909 default:
1910 return -EINVAL;
1911 }
1912
Sean Youngfdb5c452018-06-19 00:04:24 +01001913 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01001914}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001915
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001916#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1917
1918static int bpf_prog_query(const union bpf_attr *attr,
1919 union bpf_attr __user *uattr)
1920{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001921 if (!capable(CAP_NET_ADMIN))
1922 return -EPERM;
1923 if (CHECK_ATTR(BPF_PROG_QUERY))
1924 return -EINVAL;
1925 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1926 return -EINVAL;
1927
1928 switch (attr->query.attach_type) {
1929 case BPF_CGROUP_INET_INGRESS:
1930 case BPF_CGROUP_INET_EGRESS:
1931 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001932 case BPF_CGROUP_INET4_BIND:
1933 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001934 case BPF_CGROUP_INET4_POST_BIND:
1935 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001936 case BPF_CGROUP_INET4_CONNECT:
1937 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001938 case BPF_CGROUP_UDP4_SENDMSG:
1939 case BPF_CGROUP_UDP6_SENDMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001940 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001941 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001942 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01001943 case BPF_LIRC_MODE2:
1944 return lirc_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001945 default:
1946 return -EINVAL;
1947 }
Sean Youngfdb5c452018-06-19 00:04:24 +01001948
1949 return cgroup_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001950}
Daniel Mackf4324552016-11-23 16:52:27 +01001951
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001952#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1953
1954static int bpf_prog_test_run(const union bpf_attr *attr,
1955 union bpf_attr __user *uattr)
1956{
1957 struct bpf_prog *prog;
1958 int ret = -ENOTSUPP;
1959
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001960 if (!capable(CAP_SYS_ADMIN))
1961 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001962 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1963 return -EINVAL;
1964
1965 prog = bpf_prog_get(attr->test.prog_fd);
1966 if (IS_ERR(prog))
1967 return PTR_ERR(prog);
1968
1969 if (prog->aux->ops->test_run)
1970 ret = prog->aux->ops->test_run(prog, attr, uattr);
1971
1972 bpf_prog_put(prog);
1973 return ret;
1974}
1975
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001976#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1977
1978static int bpf_obj_get_next_id(const union bpf_attr *attr,
1979 union bpf_attr __user *uattr,
1980 struct idr *idr,
1981 spinlock_t *lock)
1982{
1983 u32 next_id = attr->start_id;
1984 int err = 0;
1985
1986 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1987 return -EINVAL;
1988
1989 if (!capable(CAP_SYS_ADMIN))
1990 return -EPERM;
1991
1992 next_id++;
1993 spin_lock_bh(lock);
1994 if (!idr_get_next(idr, &next_id))
1995 err = -ENOENT;
1996 spin_unlock_bh(lock);
1997
1998 if (!err)
1999 err = put_user(next_id, &uattr->next_id);
2000
2001 return err;
2002}
2003
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002004#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2005
2006static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2007{
2008 struct bpf_prog *prog;
2009 u32 id = attr->prog_id;
2010 int fd;
2011
2012 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2013 return -EINVAL;
2014
2015 if (!capable(CAP_SYS_ADMIN))
2016 return -EPERM;
2017
2018 spin_lock_bh(&prog_idr_lock);
2019 prog = idr_find(&prog_idr, id);
2020 if (prog)
2021 prog = bpf_prog_inc_not_zero(prog);
2022 else
2023 prog = ERR_PTR(-ENOENT);
2024 spin_unlock_bh(&prog_idr_lock);
2025
2026 if (IS_ERR(prog))
2027 return PTR_ERR(prog);
2028
2029 fd = bpf_prog_new_fd(prog);
2030 if (fd < 0)
2031 bpf_prog_put(prog);
2032
2033 return fd;
2034}
2035
Chenbo Feng6e71b042017-10-18 13:00:22 -07002036#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002037
2038static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2039{
2040 struct bpf_map *map;
2041 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002042 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002043 int fd;
2044
Chenbo Feng6e71b042017-10-18 13:00:22 -07002045 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2046 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002047 return -EINVAL;
2048
2049 if (!capable(CAP_SYS_ADMIN))
2050 return -EPERM;
2051
Chenbo Feng6e71b042017-10-18 13:00:22 -07002052 f_flags = bpf_get_file_flag(attr->open_flags);
2053 if (f_flags < 0)
2054 return f_flags;
2055
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002056 spin_lock_bh(&map_idr_lock);
2057 map = idr_find(&map_idr, id);
2058 if (map)
2059 map = bpf_map_inc_not_zero(map, true);
2060 else
2061 map = ERR_PTR(-ENOENT);
2062 spin_unlock_bh(&map_idr_lock);
2063
2064 if (IS_ERR(map))
2065 return PTR_ERR(map);
2066
Chenbo Feng6e71b042017-10-18 13:00:22 -07002067 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002068 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002069 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002070
2071 return fd;
2072}
2073
Daniel Borkmann7105e822017-12-20 13:42:57 +01002074static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002075 unsigned long addr, u32 *off,
2076 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002077{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002078 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002079 int i;
2080
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002081 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2082 map = prog->aux->used_maps[i];
2083 if (map == (void *)addr) {
2084 *type = BPF_PSEUDO_MAP_FD;
2085 return map;
2086 }
2087 if (!map->ops->map_direct_value_meta)
2088 continue;
2089 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2090 *type = BPF_PSEUDO_MAP_VALUE;
2091 return map;
2092 }
2093 }
2094
Daniel Borkmann7105e822017-12-20 13:42:57 +01002095 return NULL;
2096}
2097
2098static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2099{
2100 const struct bpf_map *map;
2101 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002102 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002103 u64 imm;
2104 int i;
2105
2106 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2107 GFP_USER);
2108 if (!insns)
2109 return insns;
2110
2111 for (i = 0; i < prog->len; i++) {
2112 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2113 insns[i].code = BPF_JMP | BPF_CALL;
2114 insns[i].imm = BPF_FUNC_tail_call;
2115 /* fall-through */
2116 }
2117 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2118 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2119 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2120 insns[i].code = BPF_JMP | BPF_CALL;
2121 if (!bpf_dump_raw_ok())
2122 insns[i].imm = 0;
2123 continue;
2124 }
2125
2126 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2127 continue;
2128
2129 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002130 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002131 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002132 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002133 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002134 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002135 continue;
2136 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002137 }
2138
2139 return insns;
2140}
2141
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002142static int set_info_rec_size(struct bpf_prog_info *info)
2143{
2144 /*
2145 * Ensure info.*_rec_size is the same as kernel expected size
2146 *
2147 * or
2148 *
2149 * Only allow zero *_rec_size if both _rec_size and _cnt are
2150 * zero. In this case, the kernel will set the expected
2151 * _rec_size back to the info.
2152 */
2153
Yonghong Song11d8b822018-12-10 14:14:08 -08002154 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002155 info->func_info_rec_size != sizeof(struct bpf_func_info))
2156 return -EINVAL;
2157
Yonghong Song11d8b822018-12-10 14:14:08 -08002158 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002159 info->line_info_rec_size != sizeof(struct bpf_line_info))
2160 return -EINVAL;
2161
Yonghong Song11d8b822018-12-10 14:14:08 -08002162 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002163 info->jited_line_info_rec_size != sizeof(__u64))
2164 return -EINVAL;
2165
2166 info->func_info_rec_size = sizeof(struct bpf_func_info);
2167 info->line_info_rec_size = sizeof(struct bpf_line_info);
2168 info->jited_line_info_rec_size = sizeof(__u64);
2169
2170 return 0;
2171}
2172
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002173static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2174 const union bpf_attr *attr,
2175 union bpf_attr __user *uattr)
2176{
2177 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2178 struct bpf_prog_info info = {};
2179 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002180 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002181 char __user *uinsns;
2182 u32 ulen;
2183 int err;
2184
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002185 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002186 if (err)
2187 return err;
2188 info_len = min_t(u32, sizeof(info), info_len);
2189
2190 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02002191 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002192
2193 info.type = prog->type;
2194 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002195 info.load_time = prog->aux->load_time;
2196 info.created_by_uid = from_kuid_munged(current_user_ns(),
2197 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02002198 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002199
2200 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002201 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2202
2203 ulen = info.nr_map_ids;
2204 info.nr_map_ids = prog->aux->used_map_cnt;
2205 ulen = min_t(u32, info.nr_map_ids, ulen);
2206 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07002207 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002208 u32 i;
2209
2210 for (i = 0; i < ulen; i++)
2211 if (put_user(prog->aux->used_maps[i]->id,
2212 &user_map_ids[i]))
2213 return -EFAULT;
2214 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002215
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002216 err = set_info_rec_size(&info);
2217 if (err)
2218 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08002219
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002220 bpf_prog_get_stats(prog, &stats);
2221 info.run_time_ns = stats.nsecs;
2222 info.run_cnt = stats.cnt;
2223
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002224 if (!capable(CAP_SYS_ADMIN)) {
2225 info.jited_prog_len = 0;
2226 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302227 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01002228 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08002229 info.nr_func_info = 0;
2230 info.nr_line_info = 0;
2231 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002232 goto done;
2233 }
2234
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002235 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02002236 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002237 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01002238 struct bpf_insn *insns_sanitized;
2239 bool fault;
2240
2241 if (prog->blinded && !bpf_dump_raw_ok()) {
2242 info.xlated_prog_insns = 0;
2243 goto done;
2244 }
2245 insns_sanitized = bpf_insn_prepare_dump(prog);
2246 if (!insns_sanitized)
2247 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002248 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2249 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002250 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2251 kfree(insns_sanitized);
2252 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002253 return -EFAULT;
2254 }
2255
Jakub Kicinski675fc272017-12-27 18:39:09 -08002256 if (bpf_prog_is_dev_bound(prog->aux)) {
2257 err = bpf_prog_offload_info_fill(&info, prog);
2258 if (err)
2259 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08002260 goto done;
2261 }
2262
2263 /* NOTE: the following code is supposed to be skipped for offload.
2264 * bpf_prog_offload_info_fill() is the place to fill similar fields
2265 * for offload.
2266 */
2267 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05302268 if (prog->aux->func_cnt) {
2269 u32 i;
2270
2271 info.jited_prog_len = 0;
2272 for (i = 0; i < prog->aux->func_cnt; i++)
2273 info.jited_prog_len += prog->aux->func[i]->jited_len;
2274 } else {
2275 info.jited_prog_len = prog->jited_len;
2276 }
2277
Jiong Wangfcfb1262018-01-16 16:05:19 -08002278 if (info.jited_prog_len && ulen) {
2279 if (bpf_dump_raw_ok()) {
2280 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2281 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05302282
2283 /* for multi-function programs, copy the JITed
2284 * instructions for all the functions
2285 */
2286 if (prog->aux->func_cnt) {
2287 u32 len, free, i;
2288 u8 *img;
2289
2290 free = ulen;
2291 for (i = 0; i < prog->aux->func_cnt; i++) {
2292 len = prog->aux->func[i]->jited_len;
2293 len = min_t(u32, len, free);
2294 img = (u8 *) prog->aux->func[i]->bpf_func;
2295 if (copy_to_user(uinsns, img, len))
2296 return -EFAULT;
2297 uinsns += len;
2298 free -= len;
2299 if (!free)
2300 break;
2301 }
2302 } else {
2303 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2304 return -EFAULT;
2305 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002306 } else {
2307 info.jited_prog_insns = 0;
2308 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002309 }
2310
Sandipan Dasdbecd732018-05-24 12:26:48 +05302311 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07002312 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002313 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05302314 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07002315 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302316 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302317 u32 i;
2318
2319 /* copy the address of the kernel symbol
2320 * corresponding to each function
2321 */
2322 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2323 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07002324 if (prog->aux->func_cnt) {
2325 for (i = 0; i < ulen; i++) {
2326 ksym_addr = (unsigned long)
2327 prog->aux->func[i]->bpf_func;
2328 if (put_user((u64) ksym_addr,
2329 &user_ksyms[i]))
2330 return -EFAULT;
2331 }
2332 } else {
2333 ksym_addr = (unsigned long) prog->bpf_func;
2334 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05302335 return -EFAULT;
2336 }
2337 } else {
2338 info.jited_ksyms = 0;
2339 }
2340 }
2341
Sandipan Das815581c2018-05-24 12:26:52 +05302342 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07002343 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002344 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05302345 if (bpf_dump_raw_ok()) {
2346 u32 __user *user_lens;
2347 u32 func_len, i;
2348
2349 /* copy the JITed image lengths for each function */
2350 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2351 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07002352 if (prog->aux->func_cnt) {
2353 for (i = 0; i < ulen; i++) {
2354 func_len =
2355 prog->aux->func[i]->jited_len;
2356 if (put_user(func_len, &user_lens[i]))
2357 return -EFAULT;
2358 }
2359 } else {
2360 func_len = prog->jited_len;
2361 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05302362 return -EFAULT;
2363 }
2364 } else {
2365 info.jited_func_lens = 0;
2366 }
2367 }
2368
Martin KaFai Lau73372242018-12-05 17:35:43 -08002369 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08002370 info.btf_id = btf_id(prog->aux->btf);
2371
Yonghong Song11d8b822018-12-10 14:14:08 -08002372 ulen = info.nr_func_info;
2373 info.nr_func_info = prog->aux->func_info_cnt;
2374 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002375 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08002376
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002377 user_finfo = u64_to_user_ptr(info.func_info);
2378 ulen = min_t(u32, info.nr_func_info, ulen);
2379 if (copy_to_user(user_finfo, prog->aux->func_info,
2380 info.func_info_rec_size * ulen))
2381 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08002382 }
2383
Yonghong Song11d8b822018-12-10 14:14:08 -08002384 ulen = info.nr_line_info;
2385 info.nr_line_info = prog->aux->nr_linfo;
2386 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002387 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002388
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002389 user_linfo = u64_to_user_ptr(info.line_info);
2390 ulen = min_t(u32, info.nr_line_info, ulen);
2391 if (copy_to_user(user_linfo, prog->aux->linfo,
2392 info.line_info_rec_size * ulen))
2393 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002394 }
2395
Yonghong Song11d8b822018-12-10 14:14:08 -08002396 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002397 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08002398 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002399 else
Yonghong Song11d8b822018-12-10 14:14:08 -08002400 info.nr_jited_line_info = 0;
2401 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002402 if (bpf_dump_raw_ok()) {
2403 __u64 __user *user_linfo;
2404 u32 i;
2405
2406 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08002407 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002408 for (i = 0; i < ulen; i++) {
2409 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2410 &user_linfo[i]))
2411 return -EFAULT;
2412 }
2413 } else {
2414 info.jited_line_info = 0;
2415 }
2416 }
2417
Song Liuc872bdb2018-12-12 09:37:46 -08002418 ulen = info.nr_prog_tags;
2419 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2420 if (ulen) {
2421 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2422 u32 i;
2423
2424 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2425 ulen = min_t(u32, info.nr_prog_tags, ulen);
2426 if (prog->aux->func_cnt) {
2427 for (i = 0; i < ulen; i++) {
2428 if (copy_to_user(user_prog_tags[i],
2429 prog->aux->func[i]->tag,
2430 BPF_TAG_SIZE))
2431 return -EFAULT;
2432 }
2433 } else {
2434 if (copy_to_user(user_prog_tags[0],
2435 prog->tag, BPF_TAG_SIZE))
2436 return -EFAULT;
2437 }
2438 }
2439
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002440done:
2441 if (copy_to_user(uinfo, &info, info_len) ||
2442 put_user(info_len, &uattr->info.info_len))
2443 return -EFAULT;
2444
2445 return 0;
2446}
2447
2448static int bpf_map_get_info_by_fd(struct bpf_map *map,
2449 const union bpf_attr *attr,
2450 union bpf_attr __user *uattr)
2451{
2452 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2453 struct bpf_map_info info = {};
2454 u32 info_len = attr->info.info_len;
2455 int err;
2456
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002457 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002458 if (err)
2459 return err;
2460 info_len = min_t(u32, sizeof(info), info_len);
2461
2462 info.type = map->map_type;
2463 info.id = map->id;
2464 info.key_size = map->key_size;
2465 info.value_size = map->value_size;
2466 info.max_entries = map->max_entries;
2467 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002468 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002469
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002470 if (map->btf) {
2471 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002472 info.btf_key_type_id = map->btf_key_type_id;
2473 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002474 }
2475
Jakub Kicinski52775b32018-01-17 19:13:28 -08002476 if (bpf_map_is_dev_bound(map)) {
2477 err = bpf_map_offload_info_fill(&info, map);
2478 if (err)
2479 return err;
2480 }
2481
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002482 if (copy_to_user(uinfo, &info, info_len) ||
2483 put_user(info_len, &uattr->info.info_len))
2484 return -EFAULT;
2485
2486 return 0;
2487}
2488
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002489static int bpf_btf_get_info_by_fd(struct btf *btf,
2490 const union bpf_attr *attr,
2491 union bpf_attr __user *uattr)
2492{
2493 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2494 u32 info_len = attr->info.info_len;
2495 int err;
2496
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002497 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002498 if (err)
2499 return err;
2500
2501 return btf_get_info_by_fd(btf, attr, uattr);
2502}
2503
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002504#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2505
2506static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2507 union bpf_attr __user *uattr)
2508{
2509 int ufd = attr->info.bpf_fd;
2510 struct fd f;
2511 int err;
2512
2513 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2514 return -EINVAL;
2515
2516 f = fdget(ufd);
2517 if (!f.file)
2518 return -EBADFD;
2519
2520 if (f.file->f_op == &bpf_prog_fops)
2521 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2522 uattr);
2523 else if (f.file->f_op == &bpf_map_fops)
2524 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2525 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002526 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002527 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002528 else
2529 err = -EINVAL;
2530
2531 fdput(f);
2532 return err;
2533}
2534
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002535#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2536
2537static int bpf_btf_load(const union bpf_attr *attr)
2538{
2539 if (CHECK_ATTR(BPF_BTF_LOAD))
2540 return -EINVAL;
2541
2542 if (!capable(CAP_SYS_ADMIN))
2543 return -EPERM;
2544
2545 return btf_new_fd(attr);
2546}
2547
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002548#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2549
2550static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2551{
2552 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2553 return -EINVAL;
2554
2555 if (!capable(CAP_SYS_ADMIN))
2556 return -EPERM;
2557
2558 return btf_get_fd_by_id(attr->btf_id);
2559}
2560
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002561static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2562 union bpf_attr __user *uattr,
2563 u32 prog_id, u32 fd_type,
2564 const char *buf, u64 probe_offset,
2565 u64 probe_addr)
2566{
2567 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2568 u32 len = buf ? strlen(buf) : 0, input_len;
2569 int err = 0;
2570
2571 if (put_user(len, &uattr->task_fd_query.buf_len))
2572 return -EFAULT;
2573 input_len = attr->task_fd_query.buf_len;
2574 if (input_len && ubuf) {
2575 if (!len) {
2576 /* nothing to copy, just make ubuf NULL terminated */
2577 char zero = '\0';
2578
2579 if (put_user(zero, ubuf))
2580 return -EFAULT;
2581 } else if (input_len >= len + 1) {
2582 /* ubuf can hold the string with NULL terminator */
2583 if (copy_to_user(ubuf, buf, len + 1))
2584 return -EFAULT;
2585 } else {
2586 /* ubuf cannot hold the string with NULL terminator,
2587 * do a partial copy with NULL terminator.
2588 */
2589 char zero = '\0';
2590
2591 err = -ENOSPC;
2592 if (copy_to_user(ubuf, buf, input_len - 1))
2593 return -EFAULT;
2594 if (put_user(zero, ubuf + input_len - 1))
2595 return -EFAULT;
2596 }
2597 }
2598
2599 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2600 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2601 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2602 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2603 return -EFAULT;
2604
2605 return err;
2606}
2607
2608#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2609
2610static int bpf_task_fd_query(const union bpf_attr *attr,
2611 union bpf_attr __user *uattr)
2612{
2613 pid_t pid = attr->task_fd_query.pid;
2614 u32 fd = attr->task_fd_query.fd;
2615 const struct perf_event *event;
2616 struct files_struct *files;
2617 struct task_struct *task;
2618 struct file *file;
2619 int err;
2620
2621 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2622 return -EINVAL;
2623
2624 if (!capable(CAP_SYS_ADMIN))
2625 return -EPERM;
2626
2627 if (attr->task_fd_query.flags != 0)
2628 return -EINVAL;
2629
2630 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2631 if (!task)
2632 return -ENOENT;
2633
2634 files = get_files_struct(task);
2635 put_task_struct(task);
2636 if (!files)
2637 return -ENOENT;
2638
2639 err = 0;
2640 spin_lock(&files->file_lock);
2641 file = fcheck_files(files, fd);
2642 if (!file)
2643 err = -EBADF;
2644 else
2645 get_file(file);
2646 spin_unlock(&files->file_lock);
2647 put_files_struct(files);
2648
2649 if (err)
2650 goto out;
2651
2652 if (file->f_op == &bpf_raw_tp_fops) {
2653 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2654 struct bpf_raw_event_map *btp = raw_tp->btp;
2655
2656 err = bpf_task_fd_query_copy(attr, uattr,
2657 raw_tp->prog->aux->id,
2658 BPF_FD_TYPE_RAW_TRACEPOINT,
2659 btp->tp->name, 0, 0);
2660 goto put_file;
2661 }
2662
2663 event = perf_get_event(file);
2664 if (!IS_ERR(event)) {
2665 u64 probe_offset, probe_addr;
2666 u32 prog_id, fd_type;
2667 const char *buf;
2668
2669 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2670 &buf, &probe_offset,
2671 &probe_addr);
2672 if (!err)
2673 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2674 fd_type, buf,
2675 probe_offset,
2676 probe_addr);
2677 goto put_file;
2678 }
2679
2680 err = -ENOTSUPP;
2681put_file:
2682 fput(file);
2683out:
2684 return err;
2685}
2686
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002687SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2688{
2689 union bpf_attr attr = {};
2690 int err;
2691
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002692 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002693 return -EPERM;
2694
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002695 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002696 if (err)
2697 return err;
2698 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002699
2700 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2701 if (copy_from_user(&attr, uattr, size) != 0)
2702 return -EFAULT;
2703
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002704 err = security_bpf(cmd, &attr, size);
2705 if (err < 0)
2706 return err;
2707
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002708 switch (cmd) {
2709 case BPF_MAP_CREATE:
2710 err = map_create(&attr);
2711 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002712 case BPF_MAP_LOOKUP_ELEM:
2713 err = map_lookup_elem(&attr);
2714 break;
2715 case BPF_MAP_UPDATE_ELEM:
2716 err = map_update_elem(&attr);
2717 break;
2718 case BPF_MAP_DELETE_ELEM:
2719 err = map_delete_elem(&attr);
2720 break;
2721 case BPF_MAP_GET_NEXT_KEY:
2722 err = map_get_next_key(&attr);
2723 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002724 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08002725 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002726 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002727 case BPF_OBJ_PIN:
2728 err = bpf_obj_pin(&attr);
2729 break;
2730 case BPF_OBJ_GET:
2731 err = bpf_obj_get(&attr);
2732 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002733 case BPF_PROG_ATTACH:
2734 err = bpf_prog_attach(&attr);
2735 break;
2736 case BPF_PROG_DETACH:
2737 err = bpf_prog_detach(&attr);
2738 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002739 case BPF_PROG_QUERY:
2740 err = bpf_prog_query(&attr, uattr);
2741 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002742 case BPF_PROG_TEST_RUN:
2743 err = bpf_prog_test_run(&attr, uattr);
2744 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002745 case BPF_PROG_GET_NEXT_ID:
2746 err = bpf_obj_get_next_id(&attr, uattr,
2747 &prog_idr, &prog_idr_lock);
2748 break;
2749 case BPF_MAP_GET_NEXT_ID:
2750 err = bpf_obj_get_next_id(&attr, uattr,
2751 &map_idr, &map_idr_lock);
2752 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002753 case BPF_PROG_GET_FD_BY_ID:
2754 err = bpf_prog_get_fd_by_id(&attr);
2755 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002756 case BPF_MAP_GET_FD_BY_ID:
2757 err = bpf_map_get_fd_by_id(&attr);
2758 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002759 case BPF_OBJ_GET_INFO_BY_FD:
2760 err = bpf_obj_get_info_by_fd(&attr, uattr);
2761 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002762 case BPF_RAW_TRACEPOINT_OPEN:
2763 err = bpf_raw_tracepoint_open(&attr);
2764 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002765 case BPF_BTF_LOAD:
2766 err = bpf_btf_load(&attr);
2767 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002768 case BPF_BTF_GET_FD_BY_ID:
2769 err = bpf_btf_get_fd_by_id(&attr);
2770 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002771 case BPF_TASK_FD_QUERY:
2772 err = bpf_task_fd_query(&attr, uattr);
2773 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02002774 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2775 err = map_lookup_and_delete_elem(&attr);
2776 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002777 default:
2778 err = -EINVAL;
2779 break;
2780 }
2781
2782 return err;
2783}