blob: 0c9276b54c88436a95716c039835f270fa670700 [file] [log] [blame]
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010013#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +010014#include <linux/bpf_lirc.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070015#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070016#include <linux/syscalls.h>
17#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010018#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010019#include <linux/vmalloc.h>
20#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070021#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070022#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070023#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070024#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070025#include <linux/license.h>
26#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070027#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010028#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070029#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070030#include <linux/cred.h>
31#include <linux/timekeeping.h>
32#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010033#include <linux/nospec.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070034
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070035#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
38 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
39#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
40#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
41
Chenbo Feng6e71b042017-10-18 13:00:22 -070042#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
43
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080044DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070045static DEFINE_IDR(prog_idr);
46static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070047static DEFINE_IDR(map_idr);
48static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080049
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070050int sysctl_unprivileged_bpf_disabled __read_mostly;
51
Johannes Berg40077e02017-04-11 15:34:58 +020052static const struct bpf_map_ops * const bpf_map_types[] = {
53#define BPF_PROG_TYPE(_id, _ops)
54#define BPF_MAP_TYPE(_id, _ops) \
55 [_id] = &_ops,
56#include <linux/bpf_types.h>
57#undef BPF_PROG_TYPE
58#undef BPF_MAP_TYPE
59};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070060
Mickaël Salaün752ba562017-08-07 20:45:20 +020061/*
62 * If we're handed a bigger struct than we know of, ensure all the unknown bits
63 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
64 * we don't know about yet.
65 *
66 * There is a ToCToU between this function call and the following
67 * copy_from_user() call. However, this is not a concern since this function is
68 * meant to be a future-proofing of bits.
69 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070070int bpf_check_uarg_tail_zero(void __user *uaddr,
71 size_t expected_size,
72 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020073{
74 unsigned char __user *addr;
75 unsigned char __user *end;
76 unsigned char val;
77 int err;
78
Mickaël Salaün752ba562017-08-07 20:45:20 +020079 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
80 return -E2BIG;
81
Linus Torvalds96d4f262019-01-03 18:57:57 -080082 if (unlikely(!access_ok(uaddr, actual_size)))
Mickaël Salaün752ba562017-08-07 20:45:20 +020083 return -EFAULT;
84
Mickaël Salaün58291a72017-08-07 20:45:19 +020085 if (actual_size <= expected_size)
86 return 0;
87
88 addr = uaddr + expected_size;
89 end = uaddr + actual_size;
90
91 for (; addr < end; addr++) {
92 err = get_user(val, addr);
93 if (err)
94 return err;
95 if (val)
96 return -E2BIG;
97 }
98
99 return 0;
100}
101
Jakub Kicinskia3884572018-01-11 20:29:09 -0800102const struct bpf_map_ops bpf_map_offload_ops = {
103 .map_alloc = bpf_map_offload_map_alloc,
104 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200105 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800106};
107
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700108static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
109{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800110 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100111 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700112 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800113 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700114
Mark Rutland9ef09e32018-05-03 17:04:59 +0100115 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800116 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100117 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
118 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800119 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200120 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700121
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800122 if (ops->map_alloc_check) {
123 err = ops->map_alloc_check(attr);
124 if (err)
125 return ERR_PTR(err);
126 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800127 if (attr->map_ifindex)
128 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800129 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200130 if (IS_ERR(map))
131 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800132 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100133 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200134 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700135}
136
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700137void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100138{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100139 /* We really just want to fail instead of triggering OOM killer
140 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
141 * which is used for lower order allocation requests.
142 *
143 * It has been observed that higher order allocation requests done by
144 * vmalloc with __GFP_NORETRY being set might fail due to not trying
145 * to reclaim memory from the page cache, thus we set
146 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100147 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100148
149 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100150 void *area;
151
152 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100153 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
154 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100155 if (area != NULL)
156 return area;
157 }
158
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100159 return __vmalloc_node_flags_caller(size, numa_node,
160 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
161 flags, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100162}
163
164void bpf_map_area_free(void *area)
165{
166 kvfree(area);
167}
168
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200169static u32 bpf_map_flags_retain_permanent(u32 flags)
170{
171 /* Some map creation flags are not tied to the map object but
172 * rather to the map fd instead, so they have no meaning upon
173 * map object inspection since multiple file descriptors with
174 * different (access) properties can exist here. Thus, given
175 * this has zero meaning for the map itself, lets clear these
176 * from here.
177 */
178 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
179}
180
Jakub Kicinskibd475642018-01-11 20:29:06 -0800181void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
182{
183 map->map_type = attr->map_type;
184 map->key_size = attr->key_size;
185 map->value_size = attr->value_size;
186 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200187 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800188 map->numa_node = bpf_map_attr_numa_node(attr);
189}
190
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800191int bpf_map_precharge_memlock(u32 pages)
192{
193 struct user_struct *user = get_current_user();
194 unsigned long memlock_limit, cur;
195
196 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
197 cur = atomic_long_read(&user->locked_vm);
198 free_uid(user);
199 if (cur + pages > memlock_limit)
200 return -EPERM;
201 return 0;
202}
203
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700204static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700205{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700206 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700207
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700208 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
209 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700210 return -EPERM;
211 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700212 return 0;
213}
214
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700215static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
216{
217 atomic_long_sub(pages, &user->locked_vm);
218}
219
220static int bpf_map_init_memlock(struct bpf_map *map)
221{
222 struct user_struct *user = get_current_user();
223 int ret;
224
225 ret = bpf_charge_memlock(user, map->pages);
226 if (ret) {
227 free_uid(user);
228 return ret;
229 }
230 map->user = user;
231 return ret;
232}
233
234static void bpf_map_release_memlock(struct bpf_map *map)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700235{
236 struct user_struct *user = map->user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700237 bpf_uncharge_memlock(user, map->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700238 free_uid(user);
239}
240
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700241int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
242{
243 int ret;
244
245 ret = bpf_charge_memlock(map->user, pages);
246 if (ret)
247 return ret;
248 map->pages += pages;
249 return ret;
250}
251
252void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
253{
254 bpf_uncharge_memlock(map->user, pages);
255 map->pages -= pages;
256}
257
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700258static int bpf_map_alloc_id(struct bpf_map *map)
259{
260 int id;
261
Shaohua Lib76354c2018-03-27 11:53:21 -0700262 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700263 spin_lock_bh(&map_idr_lock);
264 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
265 if (id > 0)
266 map->id = id;
267 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700268 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700269
270 if (WARN_ON_ONCE(!id))
271 return -ENOSPC;
272
273 return id > 0 ? 0 : id;
274}
275
Jakub Kicinskia3884572018-01-11 20:29:09 -0800276void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700277{
Eric Dumazet930651a2017-09-19 09:15:59 -0700278 unsigned long flags;
279
Jakub Kicinskia3884572018-01-11 20:29:09 -0800280 /* Offloaded maps are removed from the IDR store when their device
281 * disappears - even if someone holds an fd to them they are unusable,
282 * the memory is gone, all ops will fail; they are simply waiting for
283 * refcnt to drop to be freed.
284 */
285 if (!map->id)
286 return;
287
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700288 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700289 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700290 else
291 __acquire(&map_idr_lock);
292
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700293 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800294 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700295
296 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700297 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700298 else
299 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700300}
301
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700302/* called from workqueue */
303static void bpf_map_free_deferred(struct work_struct *work)
304{
305 struct bpf_map *map = container_of(work, struct bpf_map, work);
306
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700307 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700308 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700309 /* implementation dependent freeing */
310 map->ops->map_free(map);
311}
312
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100313static void bpf_map_put_uref(struct bpf_map *map)
314{
315 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700316 if (map->ops->map_release_uref)
317 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100318 }
319}
320
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700321/* decrement map refcnt and schedule it for freeing via workqueue
322 * (unrelying map implementation ops->map_free() might sleep)
323 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700324static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700325{
326 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700327 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700328 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700329 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700330 INIT_WORK(&map->work, bpf_map_free_deferred);
331 schedule_work(&map->work);
332 }
333}
334
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700335void bpf_map_put(struct bpf_map *map)
336{
337 __bpf_map_put(map, true);
338}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700339EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700340
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100341void bpf_map_put_with_uref(struct bpf_map *map)
342{
343 bpf_map_put_uref(map);
344 bpf_map_put(map);
345}
346
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700347static int bpf_map_release(struct inode *inode, struct file *filp)
348{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200349 struct bpf_map *map = filp->private_data;
350
351 if (map->ops->map_release)
352 map->ops->map_release(map, filp);
353
354 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700355 return 0;
356}
357
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100358#ifdef CONFIG_PROC_FS
359static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
360{
361 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100362 const struct bpf_array *array;
363 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200364 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100365
366 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
367 array = container_of(map, struct bpf_array, map);
368 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200369 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100370 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100371
372 seq_printf(m,
373 "map_type:\t%u\n"
374 "key_size:\t%u\n"
375 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100376 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100377 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200378 "memlock:\t%llu\n"
379 "map_id:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100380 map->map_type,
381 map->key_size,
382 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100383 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100384 map->map_flags,
Daniel Borkmann4316b402018-06-02 23:06:34 +0200385 map->pages * 1ULL << PAGE_SHIFT,
386 map->id);
Daniel Borkmann21116b72016-11-26 01:28:07 +0100387
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200388 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100389 seq_printf(m, "owner_prog_type:\t%u\n",
390 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200391 seq_printf(m, "owner_jited:\t%u\n",
392 owner_jited);
393 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100394}
395#endif
396
Chenbo Feng6e71b042017-10-18 13:00:22 -0700397static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
398 loff_t *ppos)
399{
400 /* We need this handler such that alloc_file() enables
401 * f_mode with FMODE_CAN_READ.
402 */
403 return -EINVAL;
404}
405
406static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
407 size_t siz, loff_t *ppos)
408{
409 /* We need this handler such that alloc_file() enables
410 * f_mode with FMODE_CAN_WRITE.
411 */
412 return -EINVAL;
413}
414
Chenbo Fengf66e4482017-10-18 13:00:26 -0700415const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100416#ifdef CONFIG_PROC_FS
417 .show_fdinfo = bpf_map_show_fdinfo,
418#endif
419 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700420 .read = bpf_dummy_read,
421 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700422};
423
Chenbo Feng6e71b042017-10-18 13:00:22 -0700424int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100425{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700426 int ret;
427
428 ret = security_bpf_map(map, OPEN_FMODE(flags));
429 if (ret < 0)
430 return ret;
431
Daniel Borkmannaa797812015-10-29 14:58:06 +0100432 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700433 flags | O_CLOEXEC);
434}
435
436int bpf_get_file_flag(int flags)
437{
438 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
439 return -EINVAL;
440 if (flags & BPF_F_RDONLY)
441 return O_RDONLY;
442 if (flags & BPF_F_WRONLY)
443 return O_WRONLY;
444 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100445}
446
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700447/* helper macro to check that unused fields 'union bpf_attr' are zero */
448#define CHECK_ATTR(CMD) \
449 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
450 sizeof(attr->CMD##_LAST_FIELD), 0, \
451 sizeof(*attr) - \
452 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
453 sizeof(attr->CMD##_LAST_FIELD)) != NULL
454
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700455/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
456 * Return 0 on success and < 0 on error.
457 */
458static int bpf_obj_name_cpy(char *dst, const char *src)
459{
460 const char *end = src + BPF_OBJ_NAME_LEN;
461
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700462 memset(dst, 0, BPF_OBJ_NAME_LEN);
463
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700464 /* Copy all isalnum() and '_' char */
465 while (src < end && *src) {
466 if (!isalnum(*src) && *src != '_')
467 return -EINVAL;
468 *dst++ = *src++;
469 }
470
471 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
472 if (src == end)
473 return -EINVAL;
474
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700475 return 0;
476}
477
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200478int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800479 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200480 const struct btf_type *key_type,
481 const struct btf_type *value_type)
482{
483 return -ENOTSUPP;
484}
485
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800486static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200487 u32 btf_key_id, u32 btf_value_id)
488{
489 const struct btf_type *key_type, *value_type;
490 u32 key_size, value_size;
491 int ret = 0;
492
493 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
494 if (!key_type || key_size != map->key_size)
495 return -EINVAL;
496
497 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
498 if (!value_type || value_size != map->value_size)
499 return -EINVAL;
500
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800501 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
502
503 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200504 if (map->map_flags & BPF_F_RDONLY_PROG)
505 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800506 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800507 map->map_type != BPF_MAP_TYPE_ARRAY &&
508 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800509 return -ENOTSUPP;
510 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
511 map->value_size) {
512 WARN_ONCE(1,
513 "verifier bug spin_lock_off %d value_size %d\n",
514 map->spin_lock_off, map->value_size);
515 return -EFAULT;
516 }
517 }
518
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200519 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800520 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200521
522 return ret;
523}
524
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700525#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700526/* called via syscall */
527static int map_create(union bpf_attr *attr)
528{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700529 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700530 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700531 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700532 int err;
533
534 err = CHECK_ATTR(BPF_MAP_CREATE);
535 if (err)
536 return -EINVAL;
537
Chenbo Feng6e71b042017-10-18 13:00:22 -0700538 f_flags = bpf_get_file_flag(attr->map_flags);
539 if (f_flags < 0)
540 return f_flags;
541
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700542 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700543 ((unsigned int)numa_node >= nr_node_ids ||
544 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700545 return -EINVAL;
546
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700547 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
548 map = find_and_alloc_map(attr);
549 if (IS_ERR(map))
550 return PTR_ERR(map);
551
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700552 err = bpf_obj_name_cpy(map->name, attr->map_name);
553 if (err)
554 goto free_map_nouncharge;
555
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700556 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100557 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700558
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200559 if (attr->btf_key_type_id || attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700560 struct btf *btf;
561
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700562 if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700563 err = -EINVAL;
564 goto free_map_nouncharge;
565 }
566
567 btf = btf_get_by_fd(attr->btf_fd);
568 if (IS_ERR(btf)) {
569 err = PTR_ERR(btf);
570 goto free_map_nouncharge;
571 }
572
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200573 err = map_check_btf(map, btf, attr->btf_key_type_id,
574 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700575 if (err) {
576 btf_put(btf);
577 goto free_map_nouncharge;
578 }
579
580 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700581 map->btf_key_type_id = attr->btf_key_type_id;
582 map->btf_value_type_id = attr->btf_value_type_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800583 } else {
584 map->spin_lock_off = -EINVAL;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700585 }
586
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700587 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700588 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100589 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700590
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700591 err = bpf_map_init_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700592 if (err)
593 goto free_map_sec;
594
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700595 err = bpf_map_alloc_id(map);
596 if (err)
597 goto free_map;
598
Chenbo Feng6e71b042017-10-18 13:00:22 -0700599 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700600 if (err < 0) {
601 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800602 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700603 * bpf_map_alloc_id() has published the map
604 * to the userspace and the userspace may
605 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
606 */
Peng Sun352d20d2019-02-27 22:36:25 +0800607 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700608 return err;
609 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700610
611 return err;
612
613free_map:
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700614 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700615free_map_sec:
616 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100617free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700618 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700619 map->ops->map_free(map);
620 return err;
621}
622
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700623/* if error is returned, fd is released.
624 * On success caller should complete fd access with matching fdput()
625 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100626struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700627{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700628 if (!f.file)
629 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700630 if (f.file->f_op != &bpf_map_fops) {
631 fdput(f);
632 return ERR_PTR(-EINVAL);
633 }
634
Daniel Borkmannc2101292015-10-29 14:58:07 +0100635 return f.file->private_data;
636}
637
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700638/* prog's and map's refcnt limit */
639#define BPF_MAX_REFCNT 32768
640
641struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100642{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700643 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
644 atomic_dec(&map->refcnt);
645 return ERR_PTR(-EBUSY);
646 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100647 if (uref)
648 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700649 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100650}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700651EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100652
653struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100654{
655 struct fd f = fdget(ufd);
656 struct bpf_map *map;
657
658 map = __bpf_map_get(f);
659 if (IS_ERR(map))
660 return map;
661
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700662 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100663 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700664
665 return map;
666}
667
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700668/* map_idr_lock should have been held */
669static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
670 bool uref)
671{
672 int refold;
673
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100674 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700675
676 if (refold >= BPF_MAX_REFCNT) {
677 __bpf_map_put(map, false);
678 return ERR_PTR(-EBUSY);
679 }
680
681 if (!refold)
682 return ERR_PTR(-ENOENT);
683
684 if (uref)
685 atomic_inc(&map->usercnt);
686
687 return map;
688}
689
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800690int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
691{
692 return -ENOTSUPP;
693}
694
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200695static void *__bpf_copy_key(void __user *ukey, u64 key_size)
696{
697 if (key_size)
698 return memdup_user(ukey, key_size);
699
700 if (ukey)
701 return ERR_PTR(-EINVAL);
702
703 return NULL;
704}
705
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700706/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800707#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700708
709static int map_lookup_elem(union bpf_attr *attr)
710{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100711 void __user *ukey = u64_to_user_ptr(attr->key);
712 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700713 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700714 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800715 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800716 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200717 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700718 int err;
719
720 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
721 return -EINVAL;
722
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800723 if (attr->flags & ~BPF_F_LOCK)
724 return -EINVAL;
725
Daniel Borkmann592867b2015-09-08 18:00:09 +0200726 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100727 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700728 if (IS_ERR(map))
729 return PTR_ERR(map);
730
Chenbo Feng6e71b042017-10-18 13:00:22 -0700731 if (!(f.file->f_mode & FMODE_CAN_READ)) {
732 err = -EPERM;
733 goto err_put;
734 }
735
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800736 if ((attr->flags & BPF_F_LOCK) &&
737 !map_value_has_spin_lock(map)) {
738 err = -EINVAL;
739 goto err_put;
740 }
741
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200742 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400743 if (IS_ERR(key)) {
744 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700745 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400746 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700747
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800748 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800749 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000750 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
751 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800752 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700753 else if (IS_FD_MAP(map))
754 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800755 else
756 value_size = map->value_size;
757
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800758 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800759 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700760 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800761 goto free_key;
762
Jakub Kicinskia3884572018-01-11 20:29:09 -0800763 if (bpf_map_is_dev_bound(map)) {
764 err = bpf_map_offload_lookup_elem(map, key, value);
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800765 goto done;
766 }
767
768 preempt_disable();
769 this_cpu_inc(bpf_prog_active);
770 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
771 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800772 err = bpf_percpu_hash_copy(map, key, value);
773 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
774 err = bpf_percpu_array_copy(map, key, value);
Roman Gushchinb741f162018-09-28 14:45:43 +0000775 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
776 err = bpf_percpu_cgroup_storage_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800777 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
778 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700779 } else if (IS_FD_ARRAY(map)) {
780 err = bpf_fd_array_map_lookup_elem(map, key, value);
781 } else if (IS_FD_HASH(map)) {
782 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700783 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
784 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200785 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
786 map->map_type == BPF_MAP_TYPE_STACK) {
787 err = map->ops->map_peek_elem(map, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800788 } else {
789 rcu_read_lock();
790 ptr = map->ops->map_lookup_elem(map, key);
Prashant Bhole509db282018-10-09 10:04:49 +0900791 if (IS_ERR(ptr)) {
792 err = PTR_ERR(ptr);
793 } else if (!ptr) {
794 err = -ENOENT;
795 } else {
796 err = 0;
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800797 if (attr->flags & BPF_F_LOCK)
798 /* lock 'ptr' and copy everything but lock */
799 copy_map_value_locked(map, value, ptr, true);
800 else
801 copy_map_value(map, value, ptr);
802 /* mask lock, since value wasn't zero inited */
803 check_and_init_map_lock(map, value);
Prashant Bhole509db282018-10-09 10:04:49 +0900804 }
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800805 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800806 }
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800807 this_cpu_dec(bpf_prog_active);
808 preempt_enable();
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800809
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800810done:
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800811 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800812 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700813
814 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800815 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800816 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700817
818 err = 0;
819
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800820free_value:
821 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700822free_key:
823 kfree(key);
824err_put:
825 fdput(f);
826 return err;
827}
828
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700829static void maybe_wait_bpf_programs(struct bpf_map *map)
830{
831 /* Wait for any running BPF programs to complete so that
832 * userspace, when we return to it, knows that all programs
833 * that could be running use the new map value.
834 */
835 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
836 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
837 synchronize_rcu();
838}
839
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800840#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700841
842static int map_update_elem(union bpf_attr *attr)
843{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100844 void __user *ukey = u64_to_user_ptr(attr->key);
845 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700846 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700847 struct bpf_map *map;
848 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800849 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200850 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700851 int err;
852
853 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
854 return -EINVAL;
855
Daniel Borkmann592867b2015-09-08 18:00:09 +0200856 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100857 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700858 if (IS_ERR(map))
859 return PTR_ERR(map);
860
Chenbo Feng6e71b042017-10-18 13:00:22 -0700861 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
862 err = -EPERM;
863 goto err_put;
864 }
865
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800866 if ((attr->flags & BPF_F_LOCK) &&
867 !map_value_has_spin_lock(map)) {
868 err = -EINVAL;
869 goto err_put;
870 }
871
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200872 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400873 if (IS_ERR(key)) {
874 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700875 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400876 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700877
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800878 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800879 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000880 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
881 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800882 value_size = round_up(map->value_size, 8) * num_possible_cpus();
883 else
884 value_size = map->value_size;
885
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700886 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800887 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700888 if (!value)
889 goto free_key;
890
891 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800892 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700893 goto free_value;
894
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200895 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800896 if (bpf_map_is_dev_bound(map)) {
897 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
898 goto out;
John Fastabend99ba2b52018-07-05 08:50:04 -0700899 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
900 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
901 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200902 err = map->ops->map_update_elem(map, key, value, attr->flags);
903 goto out;
904 }
905
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800906 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
907 * inside bpf map update or delete otherwise deadlocks are possible
908 */
909 preempt_disable();
910 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800911 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
912 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800913 err = bpf_percpu_hash_update(map, key, value, attr->flags);
914 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
915 err = bpf_percpu_array_update(map, key, value, attr->flags);
Roman Gushchinb741f162018-09-28 14:45:43 +0000916 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
917 err = bpf_percpu_cgroup_storage_update(map, key, value,
918 attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100919 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200920 rcu_read_lock();
921 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
922 attr->flags);
923 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700924 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
925 rcu_read_lock();
926 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
927 attr->flags);
928 rcu_read_unlock();
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700929 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
930 /* rcu_read_lock() is not needed */
931 err = bpf_fd_reuseport_array_update_elem(map, key, value,
932 attr->flags);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200933 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
934 map->map_type == BPF_MAP_TYPE_STACK) {
935 err = map->ops->map_push_elem(map, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800936 } else {
937 rcu_read_lock();
938 err = map->ops->map_update_elem(map, key, value, attr->flags);
939 rcu_read_unlock();
940 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800941 __this_cpu_dec(bpf_prog_active);
942 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700943 maybe_wait_bpf_programs(map);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200944out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700945free_value:
946 kfree(value);
947free_key:
948 kfree(key);
949err_put:
950 fdput(f);
951 return err;
952}
953
954#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
955
956static int map_delete_elem(union bpf_attr *attr)
957{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100958 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700959 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700960 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200961 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700962 void *key;
963 int err;
964
965 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
966 return -EINVAL;
967
Daniel Borkmann592867b2015-09-08 18:00:09 +0200968 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100969 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700970 if (IS_ERR(map))
971 return PTR_ERR(map);
972
Chenbo Feng6e71b042017-10-18 13:00:22 -0700973 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
974 err = -EPERM;
975 goto err_put;
976 }
977
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200978 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400979 if (IS_ERR(key)) {
980 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700981 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400982 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700983
Jakub Kicinskia3884572018-01-11 20:29:09 -0800984 if (bpf_map_is_dev_bound(map)) {
985 err = bpf_map_offload_delete_elem(map, key);
986 goto out;
987 }
988
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800989 preempt_disable();
990 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700991 rcu_read_lock();
992 err = map->ops->map_delete_elem(map, key);
993 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800994 __this_cpu_dec(bpf_prog_active);
995 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700996 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800997out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700998 kfree(key);
999err_put:
1000 fdput(f);
1001 return err;
1002}
1003
1004/* last field in 'union bpf_attr' used by this command */
1005#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1006
1007static int map_get_next_key(union bpf_attr *attr)
1008{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001009 void __user *ukey = u64_to_user_ptr(attr->key);
1010 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001011 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001012 struct bpf_map *map;
1013 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001014 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001015 int err;
1016
1017 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1018 return -EINVAL;
1019
Daniel Borkmann592867b2015-09-08 18:00:09 +02001020 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001021 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001022 if (IS_ERR(map))
1023 return PTR_ERR(map);
1024
Chenbo Feng6e71b042017-10-18 13:00:22 -07001025 if (!(f.file->f_mode & FMODE_CAN_READ)) {
1026 err = -EPERM;
1027 goto err_put;
1028 }
1029
Teng Qin8fe45922017-04-24 19:00:37 -07001030 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001031 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001032 if (IS_ERR(key)) {
1033 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001034 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001035 }
Teng Qin8fe45922017-04-24 19:00:37 -07001036 } else {
1037 key = NULL;
1038 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001039
1040 err = -ENOMEM;
1041 next_key = kmalloc(map->key_size, GFP_USER);
1042 if (!next_key)
1043 goto free_key;
1044
Jakub Kicinskia3884572018-01-11 20:29:09 -08001045 if (bpf_map_is_dev_bound(map)) {
1046 err = bpf_map_offload_get_next_key(map, key, next_key);
1047 goto out;
1048 }
1049
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001050 rcu_read_lock();
1051 err = map->ops->map_get_next_key(map, key, next_key);
1052 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001053out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001054 if (err)
1055 goto free_next_key;
1056
1057 err = -EFAULT;
1058 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1059 goto free_next_key;
1060
1061 err = 0;
1062
1063free_next_key:
1064 kfree(next_key);
1065free_key:
1066 kfree(key);
1067err_put:
1068 fdput(f);
1069 return err;
1070}
1071
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001072#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1073
1074static int map_lookup_and_delete_elem(union bpf_attr *attr)
1075{
1076 void __user *ukey = u64_to_user_ptr(attr->key);
1077 void __user *uvalue = u64_to_user_ptr(attr->value);
1078 int ufd = attr->map_fd;
1079 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001080 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001081 u32 value_size;
1082 struct fd f;
1083 int err;
1084
1085 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1086 return -EINVAL;
1087
1088 f = fdget(ufd);
1089 map = __bpf_map_get(f);
1090 if (IS_ERR(map))
1091 return PTR_ERR(map);
1092
1093 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
1094 err = -EPERM;
1095 goto err_put;
1096 }
1097
1098 key = __bpf_copy_key(ukey, map->key_size);
1099 if (IS_ERR(key)) {
1100 err = PTR_ERR(key);
1101 goto err_put;
1102 }
1103
1104 value_size = map->value_size;
1105
1106 err = -ENOMEM;
1107 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1108 if (!value)
1109 goto free_key;
1110
1111 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1112 map->map_type == BPF_MAP_TYPE_STACK) {
1113 err = map->ops->map_pop_elem(map, value);
1114 } else {
1115 err = -ENOTSUPP;
1116 }
1117
1118 if (err)
1119 goto free_value;
1120
1121 if (copy_to_user(uvalue, value, value_size) != 0)
1122 goto free_value;
1123
1124 err = 0;
1125
1126free_value:
1127 kfree(value);
1128free_key:
1129 kfree(key);
1130err_put:
1131 fdput(f);
1132 return err;
1133}
1134
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001135static const struct bpf_prog_ops * const bpf_prog_types[] = {
1136#define BPF_PROG_TYPE(_id, _name) \
1137 [_id] = & _name ## _prog_ops,
1138#define BPF_MAP_TYPE(_id, _ops)
1139#include <linux/bpf_types.h>
1140#undef BPF_PROG_TYPE
1141#undef BPF_MAP_TYPE
1142};
1143
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001144static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1145{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001146 const struct bpf_prog_ops *ops;
1147
1148 if (type >= ARRAY_SIZE(bpf_prog_types))
1149 return -EINVAL;
1150 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1151 ops = bpf_prog_types[type];
1152 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001153 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001154
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001155 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001156 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001157 else
1158 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001159 prog->type = type;
1160 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001161}
1162
1163/* drop refcnt on maps used by eBPF program and free auxilary data */
1164static void free_used_maps(struct bpf_prog_aux *aux)
1165{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001166 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001167 int i;
1168
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001169 for_each_cgroup_storage_type(stype) {
1170 if (!aux->cgroup_storage[stype])
1171 continue;
1172 bpf_cgroup_storage_release(aux->prog,
1173 aux->cgroup_storage[stype]);
1174 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07001175
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001176 for (i = 0; i < aux->used_map_cnt; i++)
1177 bpf_map_put(aux->used_maps[i]);
1178
1179 kfree(aux->used_maps);
1180}
1181
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001182int __bpf_prog_charge(struct user_struct *user, u32 pages)
1183{
1184 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1185 unsigned long user_bufs;
1186
1187 if (user) {
1188 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1189 if (user_bufs > memlock_limit) {
1190 atomic_long_sub(pages, &user->locked_vm);
1191 return -EPERM;
1192 }
1193 }
1194
1195 return 0;
1196}
1197
1198void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1199{
1200 if (user)
1201 atomic_long_sub(pages, &user->locked_vm);
1202}
1203
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001204static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1205{
1206 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001207 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001208
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001209 ret = __bpf_prog_charge(user, prog->pages);
1210 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001211 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001212 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001213 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001214
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001215 prog->aux->user = user;
1216 return 0;
1217}
1218
1219static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1220{
1221 struct user_struct *user = prog->aux->user;
1222
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001223 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001224 free_uid(user);
1225}
1226
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001227static int bpf_prog_alloc_id(struct bpf_prog *prog)
1228{
1229 int id;
1230
Shaohua Lib76354c2018-03-27 11:53:21 -07001231 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001232 spin_lock_bh(&prog_idr_lock);
1233 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1234 if (id > 0)
1235 prog->aux->id = id;
1236 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001237 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001238
1239 /* id is in [1, INT_MAX) */
1240 if (WARN_ON_ONCE(!id))
1241 return -ENOSPC;
1242
1243 return id > 0 ? 0 : id;
1244}
1245
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001246void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001247{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001248 /* cBPF to eBPF migrations are currently not in the idr store.
1249 * Offloaded programs are removed from the store when their device
1250 * disappears - even if someone grabs an fd to them they are unusable,
1251 * simply waiting for refcnt to drop to be freed.
1252 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001253 if (!prog->aux->id)
1254 return;
1255
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001256 if (do_idr_lock)
1257 spin_lock_bh(&prog_idr_lock);
1258 else
1259 __acquire(&prog_idr_lock);
1260
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001261 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001262 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001263
1264 if (do_idr_lock)
1265 spin_unlock_bh(&prog_idr_lock);
1266 else
1267 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001268}
1269
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001270static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001271{
1272 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1273
1274 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001275 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001276 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001277 bpf_prog_free(aux->prog);
1278}
1279
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001280static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001281{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001282 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001283 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001284 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001285 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001286 bpf_prog_kallsyms_del_all(prog);
Yonghong Song838e9692018-11-19 15:29:11 -08001287 btf_put(prog->aux->btf);
Yonghong Songba64e7d2018-11-24 23:20:44 -08001288 kvfree(prog->aux->func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001289 bpf_prog_free_linfo(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001290
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001291 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001292 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001293}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001294
1295void bpf_prog_put(struct bpf_prog *prog)
1296{
1297 __bpf_prog_put(prog, true);
1298}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001299EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001300
1301static int bpf_prog_release(struct inode *inode, struct file *filp)
1302{
1303 struct bpf_prog *prog = filp->private_data;
1304
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001305 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001306 return 0;
1307}
1308
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001309static void bpf_prog_get_stats(const struct bpf_prog *prog,
1310 struct bpf_prog_stats *stats)
1311{
1312 u64 nsecs = 0, cnt = 0;
1313 int cpu;
1314
1315 for_each_possible_cpu(cpu) {
1316 const struct bpf_prog_stats *st;
1317 unsigned int start;
1318 u64 tnsecs, tcnt;
1319
1320 st = per_cpu_ptr(prog->aux->stats, cpu);
1321 do {
1322 start = u64_stats_fetch_begin_irq(&st->syncp);
1323 tnsecs = st->nsecs;
1324 tcnt = st->cnt;
1325 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1326 nsecs += tnsecs;
1327 cnt += tcnt;
1328 }
1329 stats->nsecs = nsecs;
1330 stats->cnt = cnt;
1331}
1332
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001333#ifdef CONFIG_PROC_FS
1334static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1335{
1336 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001337 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001338 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001339
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001340 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001341 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001342 seq_printf(m,
1343 "prog_type:\t%u\n"
1344 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001345 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001346 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001347 "prog_id:\t%u\n"
1348 "run_time_ns:\t%llu\n"
1349 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001350 prog->type,
1351 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001352 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001353 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001354 prog->aux->id,
1355 stats.nsecs,
1356 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001357}
1358#endif
1359
Chenbo Fengf66e4482017-10-18 13:00:26 -07001360const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001361#ifdef CONFIG_PROC_FS
1362 .show_fdinfo = bpf_prog_show_fdinfo,
1363#endif
1364 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001365 .read = bpf_dummy_read,
1366 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001367};
1368
Daniel Borkmannb2197752015-10-29 14:58:09 +01001369int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001370{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001371 int ret;
1372
1373 ret = security_bpf_prog(prog);
1374 if (ret < 0)
1375 return ret;
1376
Daniel Borkmannaa797812015-10-29 14:58:06 +01001377 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1378 O_RDWR | O_CLOEXEC);
1379}
1380
Daniel Borkmann113214b2016-06-30 17:24:44 +02001381static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001382{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001383 if (!f.file)
1384 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001385 if (f.file->f_op != &bpf_prog_fops) {
1386 fdput(f);
1387 return ERR_PTR(-EINVAL);
1388 }
1389
Daniel Borkmannc2101292015-10-29 14:58:07 +01001390 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001391}
1392
Brenden Blanco59d36562016-07-19 12:16:46 -07001393struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001394{
Brenden Blanco59d36562016-07-19 12:16:46 -07001395 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1396 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001397 return ERR_PTR(-EBUSY);
1398 }
1399 return prog;
1400}
Brenden Blanco59d36562016-07-19 12:16:46 -07001401EXPORT_SYMBOL_GPL(bpf_prog_add);
1402
Daniel Borkmannc5405942016-11-09 22:02:34 +01001403void bpf_prog_sub(struct bpf_prog *prog, int i)
1404{
1405 /* Only to be used for undoing previous bpf_prog_add() in some
1406 * error path. We still know that another entity in our call
1407 * path holds a reference to the program, thus atomic_sub() can
1408 * be safely used in such cases!
1409 */
1410 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1411}
1412EXPORT_SYMBOL_GPL(bpf_prog_sub);
1413
Brenden Blanco59d36562016-07-19 12:16:46 -07001414struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1415{
1416 return bpf_prog_add(prog, 1);
1417}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001418EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001419
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001420/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001421struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001422{
1423 int refold;
1424
Mark Rutlandbfc18e32018-06-21 13:13:04 +01001425 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001426
1427 if (refold >= BPF_MAX_REFCNT) {
1428 __bpf_prog_put(prog, false);
1429 return ERR_PTR(-EBUSY);
1430 }
1431
1432 if (!refold)
1433 return ERR_PTR(-ENOENT);
1434
1435 return prog;
1436}
John Fastabenda6f6df62017-08-15 22:32:22 -07001437EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001438
Al Viro040ee692017-12-02 20:20:38 -05001439bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001440 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001441{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001442 /* not an attachment, just a refcount inc, always allow */
1443 if (!attach_type)
1444 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001445
1446 if (prog->type != *attach_type)
1447 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001448 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001449 return false;
1450
1451 return true;
1452}
1453
1454static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001455 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001456{
1457 struct fd f = fdget(ufd);
1458 struct bpf_prog *prog;
1459
Daniel Borkmann113214b2016-06-30 17:24:44 +02001460 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001461 if (IS_ERR(prog))
1462 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001463 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001464 prog = ERR_PTR(-EINVAL);
1465 goto out;
1466 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001467
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001468 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001469out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001470 fdput(f);
1471 return prog;
1472}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001473
1474struct bpf_prog *bpf_prog_get(u32 ufd)
1475{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001476 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001477}
1478
Jakub Kicinski248f3462017-11-03 13:56:20 -07001479struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001480 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001481{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001482 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001483}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001484EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001485
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001486/* Initially all BPF programs could be loaded w/o specifying
1487 * expected_attach_type. Later for some of them specifying expected_attach_type
1488 * at load time became required so that program could be validated properly.
1489 * Programs of types that are allowed to be loaded both w/ and w/o (for
1490 * backward compatibility) expected_attach_type, should have the default attach
1491 * type assigned to expected_attach_type for the latter case, so that it can be
1492 * validated later at attach time.
1493 *
1494 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1495 * prog type requires it but has some attach types that have to be backward
1496 * compatible.
1497 */
1498static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1499{
1500 switch (attr->prog_type) {
1501 case BPF_PROG_TYPE_CGROUP_SOCK:
1502 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1503 * exist so checking for non-zero is the way to go here.
1504 */
1505 if (!attr->expected_attach_type)
1506 attr->expected_attach_type =
1507 BPF_CGROUP_INET_SOCK_CREATE;
1508 break;
1509 }
1510}
1511
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001512static int
1513bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1514 enum bpf_attach_type expected_attach_type)
1515{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001516 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001517 case BPF_PROG_TYPE_CGROUP_SOCK:
1518 switch (expected_attach_type) {
1519 case BPF_CGROUP_INET_SOCK_CREATE:
1520 case BPF_CGROUP_INET4_POST_BIND:
1521 case BPF_CGROUP_INET6_POST_BIND:
1522 return 0;
1523 default:
1524 return -EINVAL;
1525 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001526 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1527 switch (expected_attach_type) {
1528 case BPF_CGROUP_INET4_BIND:
1529 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001530 case BPF_CGROUP_INET4_CONNECT:
1531 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001532 case BPF_CGROUP_UDP4_SENDMSG:
1533 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001534 return 0;
1535 default:
1536 return -EINVAL;
1537 }
1538 default:
1539 return 0;
1540 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001541}
1542
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001543/* last field in 'union bpf_attr' used by this command */
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001544#define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001545
Yonghong Song838e9692018-11-19 15:29:11 -08001546static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001547{
1548 enum bpf_prog_type type = attr->prog_type;
1549 struct bpf_prog *prog;
1550 int err;
1551 char license[128];
1552 bool is_gpl;
1553
1554 if (CHECK_ATTR(BPF_PROG_LOAD))
1555 return -EINVAL;
1556
David Millere9ee9ef2018-11-30 21:08:14 -08001557 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT))
David S. Millere07b98d2017-05-10 11:38:07 -07001558 return -EINVAL;
1559
David Millere9ee9ef2018-11-30 21:08:14 -08001560 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1561 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1562 !capable(CAP_SYS_ADMIN))
1563 return -EPERM;
1564
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001565 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001566 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001567 sizeof(license) - 1) < 0)
1568 return -EFAULT;
1569 license[sizeof(license) - 1] = 0;
1570
1571 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1572 is_gpl = license_is_gpl_compatible(license);
1573
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07001574 if (attr->insn_cnt == 0 ||
1575 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001576 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07001577 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1578 type != BPF_PROG_TYPE_CGROUP_SKB &&
1579 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001580 return -EPERM;
1581
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001582 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001583 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1584 return -EINVAL;
1585
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001586 /* plain bpf_prog allocation */
1587 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1588 if (!prog)
1589 return -ENOMEM;
1590
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001591 prog->expected_attach_type = attr->expected_attach_type;
1592
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001593 prog->aux->offload_requested = !!attr->prog_ifindex;
1594
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001595 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001596 if (err)
1597 goto free_prog_nouncharge;
1598
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001599 err = bpf_prog_charge_memlock(prog);
1600 if (err)
1601 goto free_prog_sec;
1602
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001603 prog->len = attr->insn_cnt;
1604
1605 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001606 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001607 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001608 goto free_prog;
1609
1610 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001611 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001612
1613 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001614 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001615
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001616 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001617 err = bpf_prog_offload_init(prog, attr);
1618 if (err)
1619 goto free_prog;
1620 }
1621
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001622 /* find program type: socket_filter vs tracing_filter */
1623 err = find_prog_type(type, prog);
1624 if (err < 0)
1625 goto free_prog;
1626
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001627 prog->aux->load_time = ktime_get_boot_ns();
1628 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1629 if (err)
1630 goto free_prog;
1631
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001632 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08001633 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001634 if (err < 0)
1635 goto free_used_maps;
1636
Daniel Borkmann9facc332018-06-15 02:30:48 +02001637 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001638 if (err < 0)
1639 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001640
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001641 err = bpf_prog_alloc_id(prog);
1642 if (err)
1643 goto free_used_maps;
1644
Daniel Borkmannaa797812015-10-29 14:58:06 +01001645 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001646 if (err < 0) {
1647 /* failed to allocate fd.
1648 * bpf_prog_put() is needed because the above
1649 * bpf_prog_alloc_id() has published the prog
1650 * to the userspace and the userspace may
1651 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1652 */
1653 bpf_prog_put(prog);
1654 return err;
1655 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001656
Daniel Borkmann74451e662017-02-16 22:24:50 +01001657 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08001658 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001659 return err;
1660
1661free_used_maps:
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001662 bpf_prog_free_linfo(prog);
Martin KaFai Lau5482e9a2018-12-01 17:08:44 -08001663 kvfree(prog->aux->func_info);
1664 btf_put(prog->aux->btf);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001665 bpf_prog_kallsyms_del_subprogs(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001666 free_used_maps(prog->aux);
1667free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001668 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001669free_prog_sec:
1670 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001671free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001672 bpf_prog_free(prog);
1673 return err;
1674}
1675
Chenbo Feng6e71b042017-10-18 13:00:22 -07001676#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001677
1678static int bpf_obj_pin(const union bpf_attr *attr)
1679{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001680 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001681 return -EINVAL;
1682
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001683 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001684}
1685
1686static int bpf_obj_get(const union bpf_attr *attr)
1687{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001688 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1689 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001690 return -EINVAL;
1691
Chenbo Feng6e71b042017-10-18 13:00:22 -07001692 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1693 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001694}
1695
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001696struct bpf_raw_tracepoint {
1697 struct bpf_raw_event_map *btp;
1698 struct bpf_prog *prog;
1699};
1700
1701static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1702{
1703 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1704
1705 if (raw_tp->prog) {
1706 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1707 bpf_prog_put(raw_tp->prog);
1708 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001709 bpf_put_raw_tracepoint(raw_tp->btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001710 kfree(raw_tp);
1711 return 0;
1712}
1713
1714static const struct file_operations bpf_raw_tp_fops = {
1715 .release = bpf_raw_tracepoint_release,
1716 .read = bpf_dummy_read,
1717 .write = bpf_dummy_write,
1718};
1719
1720#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1721
1722static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1723{
1724 struct bpf_raw_tracepoint *raw_tp;
1725 struct bpf_raw_event_map *btp;
1726 struct bpf_prog *prog;
1727 char tp_name[128];
1728 int tp_fd, err;
1729
1730 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1731 sizeof(tp_name) - 1) < 0)
1732 return -EFAULT;
1733 tp_name[sizeof(tp_name) - 1] = 0;
1734
Matt Mullinsa38d1102018-12-12 16:42:37 -08001735 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001736 if (!btp)
1737 return -ENOENT;
1738
1739 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001740 if (!raw_tp) {
1741 err = -ENOMEM;
1742 goto out_put_btp;
1743 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001744 raw_tp->btp = btp;
1745
1746 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1747 BPF_PROG_TYPE_RAW_TRACEPOINT);
1748 if (IS_ERR(prog)) {
1749 err = PTR_ERR(prog);
1750 goto out_free_tp;
1751 }
1752
1753 err = bpf_probe_register(raw_tp->btp, prog);
1754 if (err)
1755 goto out_put_prog;
1756
1757 raw_tp->prog = prog;
1758 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1759 O_CLOEXEC);
1760 if (tp_fd < 0) {
1761 bpf_probe_unregister(raw_tp->btp, prog);
1762 err = tp_fd;
1763 goto out_put_prog;
1764 }
1765 return tp_fd;
1766
1767out_put_prog:
1768 bpf_prog_put(prog);
1769out_free_tp:
1770 kfree(raw_tp);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001771out_put_btp:
1772 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001773 return err;
1774}
1775
Anders Roxell33491582018-04-03 14:09:47 +02001776static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1777 enum bpf_attach_type attach_type)
1778{
1779 switch (prog->type) {
1780 case BPF_PROG_TYPE_CGROUP_SOCK:
1781 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1782 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1783 default:
1784 return 0;
1785 }
1786}
1787
John Fastabend464bc0f2017-08-28 07:10:04 -07001788#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001789
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001790#define BPF_F_ATTACH_MASK \
1791 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1792
Daniel Mackf4324552016-11-23 16:52:27 +01001793static int bpf_prog_attach(const union bpf_attr *attr)
1794{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001795 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001796 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001797 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001798
1799 if (!capable(CAP_NET_ADMIN))
1800 return -EPERM;
1801
1802 if (CHECK_ATTR(BPF_PROG_ATTACH))
1803 return -EINVAL;
1804
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001805 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001806 return -EINVAL;
1807
Daniel Mackf4324552016-11-23 16:52:27 +01001808 switch (attr->attach_type) {
1809 case BPF_CGROUP_INET_INGRESS:
1810 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001811 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001812 break;
David Ahern610236582016-12-01 08:48:04 -08001813 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001814 case BPF_CGROUP_INET4_POST_BIND:
1815 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001816 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1817 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001818 case BPF_CGROUP_INET4_BIND:
1819 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001820 case BPF_CGROUP_INET4_CONNECT:
1821 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001822 case BPF_CGROUP_UDP4_SENDMSG:
1823 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001824 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1825 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001826 case BPF_CGROUP_SOCK_OPS:
1827 ptype = BPF_PROG_TYPE_SOCK_OPS;
1828 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001829 case BPF_CGROUP_DEVICE:
1830 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1831 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001832 case BPF_SK_MSG_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001833 ptype = BPF_PROG_TYPE_SK_MSG;
1834 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001835 case BPF_SK_SKB_STREAM_PARSER:
1836 case BPF_SK_SKB_STREAM_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001837 ptype = BPF_PROG_TYPE_SK_SKB;
1838 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01001839 case BPF_LIRC_MODE2:
Sean Youngfdb5c452018-06-19 00:04:24 +01001840 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1841 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001842 case BPF_FLOW_DISSECTOR:
1843 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1844 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001845 default:
1846 return -EINVAL;
1847 }
1848
David Ahernb2cd1252016-12-01 08:48:03 -08001849 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1850 if (IS_ERR(prog))
1851 return PTR_ERR(prog);
1852
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001853 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1854 bpf_prog_put(prog);
1855 return -EINVAL;
1856 }
1857
Sean Youngfdb5c452018-06-19 00:04:24 +01001858 switch (ptype) {
1859 case BPF_PROG_TYPE_SK_SKB:
1860 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001861 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01001862 break;
1863 case BPF_PROG_TYPE_LIRC_MODE2:
1864 ret = lirc_prog_attach(attr, prog);
1865 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001866 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1867 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1868 break;
Sean Youngfdb5c452018-06-19 00:04:24 +01001869 default:
1870 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001871 }
1872
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001873 if (ret)
1874 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001875 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001876}
1877
1878#define BPF_PROG_DETACH_LAST_FIELD attach_type
1879
1880static int bpf_prog_detach(const union bpf_attr *attr)
1881{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001882 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001883
1884 if (!capable(CAP_NET_ADMIN))
1885 return -EPERM;
1886
1887 if (CHECK_ATTR(BPF_PROG_DETACH))
1888 return -EINVAL;
1889
1890 switch (attr->attach_type) {
1891 case BPF_CGROUP_INET_INGRESS:
1892 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001893 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1894 break;
David Ahern610236582016-12-01 08:48:04 -08001895 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001896 case BPF_CGROUP_INET4_POST_BIND:
1897 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001898 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1899 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001900 case BPF_CGROUP_INET4_BIND:
1901 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001902 case BPF_CGROUP_INET4_CONNECT:
1903 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001904 case BPF_CGROUP_UDP4_SENDMSG:
1905 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001906 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1907 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001908 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001909 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001910 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001911 case BPF_CGROUP_DEVICE:
1912 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1913 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001914 case BPF_SK_MSG_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001915 return sock_map_get_from_fd(attr, NULL);
John Fastabend5a67da22017-09-08 14:00:49 -07001916 case BPF_SK_SKB_STREAM_PARSER:
1917 case BPF_SK_SKB_STREAM_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001918 return sock_map_get_from_fd(attr, NULL);
Sean Youngf4364dc2018-05-27 12:24:09 +01001919 case BPF_LIRC_MODE2:
1920 return lirc_prog_detach(attr);
Petar Penkovd58e4682018-09-14 07:46:18 -07001921 case BPF_FLOW_DISSECTOR:
1922 return skb_flow_dissector_bpf_prog_detach(attr);
Daniel Mackf4324552016-11-23 16:52:27 +01001923 default:
1924 return -EINVAL;
1925 }
1926
Sean Youngfdb5c452018-06-19 00:04:24 +01001927 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01001928}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001929
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001930#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1931
1932static int bpf_prog_query(const union bpf_attr *attr,
1933 union bpf_attr __user *uattr)
1934{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001935 if (!capable(CAP_NET_ADMIN))
1936 return -EPERM;
1937 if (CHECK_ATTR(BPF_PROG_QUERY))
1938 return -EINVAL;
1939 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1940 return -EINVAL;
1941
1942 switch (attr->query.attach_type) {
1943 case BPF_CGROUP_INET_INGRESS:
1944 case BPF_CGROUP_INET_EGRESS:
1945 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001946 case BPF_CGROUP_INET4_BIND:
1947 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001948 case BPF_CGROUP_INET4_POST_BIND:
1949 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001950 case BPF_CGROUP_INET4_CONNECT:
1951 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001952 case BPF_CGROUP_UDP4_SENDMSG:
1953 case BPF_CGROUP_UDP6_SENDMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001954 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001955 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001956 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01001957 case BPF_LIRC_MODE2:
1958 return lirc_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001959 default:
1960 return -EINVAL;
1961 }
Sean Youngfdb5c452018-06-19 00:04:24 +01001962
1963 return cgroup_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001964}
Daniel Mackf4324552016-11-23 16:52:27 +01001965
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001966#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1967
1968static int bpf_prog_test_run(const union bpf_attr *attr,
1969 union bpf_attr __user *uattr)
1970{
1971 struct bpf_prog *prog;
1972 int ret = -ENOTSUPP;
1973
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001974 if (!capable(CAP_SYS_ADMIN))
1975 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001976 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1977 return -EINVAL;
1978
1979 prog = bpf_prog_get(attr->test.prog_fd);
1980 if (IS_ERR(prog))
1981 return PTR_ERR(prog);
1982
1983 if (prog->aux->ops->test_run)
1984 ret = prog->aux->ops->test_run(prog, attr, uattr);
1985
1986 bpf_prog_put(prog);
1987 return ret;
1988}
1989
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001990#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1991
1992static int bpf_obj_get_next_id(const union bpf_attr *attr,
1993 union bpf_attr __user *uattr,
1994 struct idr *idr,
1995 spinlock_t *lock)
1996{
1997 u32 next_id = attr->start_id;
1998 int err = 0;
1999
2000 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2001 return -EINVAL;
2002
2003 if (!capable(CAP_SYS_ADMIN))
2004 return -EPERM;
2005
2006 next_id++;
2007 spin_lock_bh(lock);
2008 if (!idr_get_next(idr, &next_id))
2009 err = -ENOENT;
2010 spin_unlock_bh(lock);
2011
2012 if (!err)
2013 err = put_user(next_id, &uattr->next_id);
2014
2015 return err;
2016}
2017
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002018#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2019
2020static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2021{
2022 struct bpf_prog *prog;
2023 u32 id = attr->prog_id;
2024 int fd;
2025
2026 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2027 return -EINVAL;
2028
2029 if (!capable(CAP_SYS_ADMIN))
2030 return -EPERM;
2031
2032 spin_lock_bh(&prog_idr_lock);
2033 prog = idr_find(&prog_idr, id);
2034 if (prog)
2035 prog = bpf_prog_inc_not_zero(prog);
2036 else
2037 prog = ERR_PTR(-ENOENT);
2038 spin_unlock_bh(&prog_idr_lock);
2039
2040 if (IS_ERR(prog))
2041 return PTR_ERR(prog);
2042
2043 fd = bpf_prog_new_fd(prog);
2044 if (fd < 0)
2045 bpf_prog_put(prog);
2046
2047 return fd;
2048}
2049
Chenbo Feng6e71b042017-10-18 13:00:22 -07002050#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002051
2052static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2053{
2054 struct bpf_map *map;
2055 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002056 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002057 int fd;
2058
Chenbo Feng6e71b042017-10-18 13:00:22 -07002059 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2060 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002061 return -EINVAL;
2062
2063 if (!capable(CAP_SYS_ADMIN))
2064 return -EPERM;
2065
Chenbo Feng6e71b042017-10-18 13:00:22 -07002066 f_flags = bpf_get_file_flag(attr->open_flags);
2067 if (f_flags < 0)
2068 return f_flags;
2069
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002070 spin_lock_bh(&map_idr_lock);
2071 map = idr_find(&map_idr, id);
2072 if (map)
2073 map = bpf_map_inc_not_zero(map, true);
2074 else
2075 map = ERR_PTR(-ENOENT);
2076 spin_unlock_bh(&map_idr_lock);
2077
2078 if (IS_ERR(map))
2079 return PTR_ERR(map);
2080
Chenbo Feng6e71b042017-10-18 13:00:22 -07002081 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002082 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002083 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002084
2085 return fd;
2086}
2087
Daniel Borkmann7105e822017-12-20 13:42:57 +01002088static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002089 unsigned long addr, u32 *off,
2090 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002091{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002092 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002093 int i;
2094
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002095 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2096 map = prog->aux->used_maps[i];
2097 if (map == (void *)addr) {
2098 *type = BPF_PSEUDO_MAP_FD;
2099 return map;
2100 }
2101 if (!map->ops->map_direct_value_meta)
2102 continue;
2103 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2104 *type = BPF_PSEUDO_MAP_VALUE;
2105 return map;
2106 }
2107 }
2108
Daniel Borkmann7105e822017-12-20 13:42:57 +01002109 return NULL;
2110}
2111
2112static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2113{
2114 const struct bpf_map *map;
2115 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002116 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002117 u64 imm;
2118 int i;
2119
2120 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2121 GFP_USER);
2122 if (!insns)
2123 return insns;
2124
2125 for (i = 0; i < prog->len; i++) {
2126 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2127 insns[i].code = BPF_JMP | BPF_CALL;
2128 insns[i].imm = BPF_FUNC_tail_call;
2129 /* fall-through */
2130 }
2131 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2132 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2133 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2134 insns[i].code = BPF_JMP | BPF_CALL;
2135 if (!bpf_dump_raw_ok())
2136 insns[i].imm = 0;
2137 continue;
2138 }
2139
2140 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2141 continue;
2142
2143 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002144 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002145 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002146 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002147 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002148 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002149 continue;
2150 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002151 }
2152
2153 return insns;
2154}
2155
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002156static int set_info_rec_size(struct bpf_prog_info *info)
2157{
2158 /*
2159 * Ensure info.*_rec_size is the same as kernel expected size
2160 *
2161 * or
2162 *
2163 * Only allow zero *_rec_size if both _rec_size and _cnt are
2164 * zero. In this case, the kernel will set the expected
2165 * _rec_size back to the info.
2166 */
2167
Yonghong Song11d8b822018-12-10 14:14:08 -08002168 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002169 info->func_info_rec_size != sizeof(struct bpf_func_info))
2170 return -EINVAL;
2171
Yonghong Song11d8b822018-12-10 14:14:08 -08002172 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002173 info->line_info_rec_size != sizeof(struct bpf_line_info))
2174 return -EINVAL;
2175
Yonghong Song11d8b822018-12-10 14:14:08 -08002176 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002177 info->jited_line_info_rec_size != sizeof(__u64))
2178 return -EINVAL;
2179
2180 info->func_info_rec_size = sizeof(struct bpf_func_info);
2181 info->line_info_rec_size = sizeof(struct bpf_line_info);
2182 info->jited_line_info_rec_size = sizeof(__u64);
2183
2184 return 0;
2185}
2186
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002187static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2188 const union bpf_attr *attr,
2189 union bpf_attr __user *uattr)
2190{
2191 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2192 struct bpf_prog_info info = {};
2193 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002194 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002195 char __user *uinsns;
2196 u32 ulen;
2197 int err;
2198
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002199 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002200 if (err)
2201 return err;
2202 info_len = min_t(u32, sizeof(info), info_len);
2203
2204 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02002205 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002206
2207 info.type = prog->type;
2208 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002209 info.load_time = prog->aux->load_time;
2210 info.created_by_uid = from_kuid_munged(current_user_ns(),
2211 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02002212 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002213
2214 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002215 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2216
2217 ulen = info.nr_map_ids;
2218 info.nr_map_ids = prog->aux->used_map_cnt;
2219 ulen = min_t(u32, info.nr_map_ids, ulen);
2220 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07002221 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002222 u32 i;
2223
2224 for (i = 0; i < ulen; i++)
2225 if (put_user(prog->aux->used_maps[i]->id,
2226 &user_map_ids[i]))
2227 return -EFAULT;
2228 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002229
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002230 err = set_info_rec_size(&info);
2231 if (err)
2232 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08002233
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002234 bpf_prog_get_stats(prog, &stats);
2235 info.run_time_ns = stats.nsecs;
2236 info.run_cnt = stats.cnt;
2237
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002238 if (!capable(CAP_SYS_ADMIN)) {
2239 info.jited_prog_len = 0;
2240 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302241 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01002242 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08002243 info.nr_func_info = 0;
2244 info.nr_line_info = 0;
2245 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002246 goto done;
2247 }
2248
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002249 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02002250 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002251 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01002252 struct bpf_insn *insns_sanitized;
2253 bool fault;
2254
2255 if (prog->blinded && !bpf_dump_raw_ok()) {
2256 info.xlated_prog_insns = 0;
2257 goto done;
2258 }
2259 insns_sanitized = bpf_insn_prepare_dump(prog);
2260 if (!insns_sanitized)
2261 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002262 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2263 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002264 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2265 kfree(insns_sanitized);
2266 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002267 return -EFAULT;
2268 }
2269
Jakub Kicinski675fc272017-12-27 18:39:09 -08002270 if (bpf_prog_is_dev_bound(prog->aux)) {
2271 err = bpf_prog_offload_info_fill(&info, prog);
2272 if (err)
2273 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08002274 goto done;
2275 }
2276
2277 /* NOTE: the following code is supposed to be skipped for offload.
2278 * bpf_prog_offload_info_fill() is the place to fill similar fields
2279 * for offload.
2280 */
2281 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05302282 if (prog->aux->func_cnt) {
2283 u32 i;
2284
2285 info.jited_prog_len = 0;
2286 for (i = 0; i < prog->aux->func_cnt; i++)
2287 info.jited_prog_len += prog->aux->func[i]->jited_len;
2288 } else {
2289 info.jited_prog_len = prog->jited_len;
2290 }
2291
Jiong Wangfcfb1262018-01-16 16:05:19 -08002292 if (info.jited_prog_len && ulen) {
2293 if (bpf_dump_raw_ok()) {
2294 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2295 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05302296
2297 /* for multi-function programs, copy the JITed
2298 * instructions for all the functions
2299 */
2300 if (prog->aux->func_cnt) {
2301 u32 len, free, i;
2302 u8 *img;
2303
2304 free = ulen;
2305 for (i = 0; i < prog->aux->func_cnt; i++) {
2306 len = prog->aux->func[i]->jited_len;
2307 len = min_t(u32, len, free);
2308 img = (u8 *) prog->aux->func[i]->bpf_func;
2309 if (copy_to_user(uinsns, img, len))
2310 return -EFAULT;
2311 uinsns += len;
2312 free -= len;
2313 if (!free)
2314 break;
2315 }
2316 } else {
2317 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2318 return -EFAULT;
2319 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002320 } else {
2321 info.jited_prog_insns = 0;
2322 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002323 }
2324
Sandipan Dasdbecd732018-05-24 12:26:48 +05302325 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07002326 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002327 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05302328 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07002329 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302330 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302331 u32 i;
2332
2333 /* copy the address of the kernel symbol
2334 * corresponding to each function
2335 */
2336 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2337 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07002338 if (prog->aux->func_cnt) {
2339 for (i = 0; i < ulen; i++) {
2340 ksym_addr = (unsigned long)
2341 prog->aux->func[i]->bpf_func;
2342 if (put_user((u64) ksym_addr,
2343 &user_ksyms[i]))
2344 return -EFAULT;
2345 }
2346 } else {
2347 ksym_addr = (unsigned long) prog->bpf_func;
2348 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05302349 return -EFAULT;
2350 }
2351 } else {
2352 info.jited_ksyms = 0;
2353 }
2354 }
2355
Sandipan Das815581c2018-05-24 12:26:52 +05302356 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07002357 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002358 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05302359 if (bpf_dump_raw_ok()) {
2360 u32 __user *user_lens;
2361 u32 func_len, i;
2362
2363 /* copy the JITed image lengths for each function */
2364 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2365 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07002366 if (prog->aux->func_cnt) {
2367 for (i = 0; i < ulen; i++) {
2368 func_len =
2369 prog->aux->func[i]->jited_len;
2370 if (put_user(func_len, &user_lens[i]))
2371 return -EFAULT;
2372 }
2373 } else {
2374 func_len = prog->jited_len;
2375 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05302376 return -EFAULT;
2377 }
2378 } else {
2379 info.jited_func_lens = 0;
2380 }
2381 }
2382
Martin KaFai Lau73372242018-12-05 17:35:43 -08002383 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08002384 info.btf_id = btf_id(prog->aux->btf);
2385
Yonghong Song11d8b822018-12-10 14:14:08 -08002386 ulen = info.nr_func_info;
2387 info.nr_func_info = prog->aux->func_info_cnt;
2388 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002389 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08002390
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002391 user_finfo = u64_to_user_ptr(info.func_info);
2392 ulen = min_t(u32, info.nr_func_info, ulen);
2393 if (copy_to_user(user_finfo, prog->aux->func_info,
2394 info.func_info_rec_size * ulen))
2395 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08002396 }
2397
Yonghong Song11d8b822018-12-10 14:14:08 -08002398 ulen = info.nr_line_info;
2399 info.nr_line_info = prog->aux->nr_linfo;
2400 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002401 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002402
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002403 user_linfo = u64_to_user_ptr(info.line_info);
2404 ulen = min_t(u32, info.nr_line_info, ulen);
2405 if (copy_to_user(user_linfo, prog->aux->linfo,
2406 info.line_info_rec_size * ulen))
2407 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002408 }
2409
Yonghong Song11d8b822018-12-10 14:14:08 -08002410 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002411 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08002412 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002413 else
Yonghong Song11d8b822018-12-10 14:14:08 -08002414 info.nr_jited_line_info = 0;
2415 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002416 if (bpf_dump_raw_ok()) {
2417 __u64 __user *user_linfo;
2418 u32 i;
2419
2420 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08002421 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002422 for (i = 0; i < ulen; i++) {
2423 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2424 &user_linfo[i]))
2425 return -EFAULT;
2426 }
2427 } else {
2428 info.jited_line_info = 0;
2429 }
2430 }
2431
Song Liuc872bdb2018-12-12 09:37:46 -08002432 ulen = info.nr_prog_tags;
2433 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2434 if (ulen) {
2435 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2436 u32 i;
2437
2438 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2439 ulen = min_t(u32, info.nr_prog_tags, ulen);
2440 if (prog->aux->func_cnt) {
2441 for (i = 0; i < ulen; i++) {
2442 if (copy_to_user(user_prog_tags[i],
2443 prog->aux->func[i]->tag,
2444 BPF_TAG_SIZE))
2445 return -EFAULT;
2446 }
2447 } else {
2448 if (copy_to_user(user_prog_tags[0],
2449 prog->tag, BPF_TAG_SIZE))
2450 return -EFAULT;
2451 }
2452 }
2453
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002454done:
2455 if (copy_to_user(uinfo, &info, info_len) ||
2456 put_user(info_len, &uattr->info.info_len))
2457 return -EFAULT;
2458
2459 return 0;
2460}
2461
2462static int bpf_map_get_info_by_fd(struct bpf_map *map,
2463 const union bpf_attr *attr,
2464 union bpf_attr __user *uattr)
2465{
2466 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2467 struct bpf_map_info info = {};
2468 u32 info_len = attr->info.info_len;
2469 int err;
2470
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002471 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002472 if (err)
2473 return err;
2474 info_len = min_t(u32, sizeof(info), info_len);
2475
2476 info.type = map->map_type;
2477 info.id = map->id;
2478 info.key_size = map->key_size;
2479 info.value_size = map->value_size;
2480 info.max_entries = map->max_entries;
2481 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002482 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002483
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002484 if (map->btf) {
2485 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002486 info.btf_key_type_id = map->btf_key_type_id;
2487 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002488 }
2489
Jakub Kicinski52775b32018-01-17 19:13:28 -08002490 if (bpf_map_is_dev_bound(map)) {
2491 err = bpf_map_offload_info_fill(&info, map);
2492 if (err)
2493 return err;
2494 }
2495
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002496 if (copy_to_user(uinfo, &info, info_len) ||
2497 put_user(info_len, &uattr->info.info_len))
2498 return -EFAULT;
2499
2500 return 0;
2501}
2502
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002503static int bpf_btf_get_info_by_fd(struct btf *btf,
2504 const union bpf_attr *attr,
2505 union bpf_attr __user *uattr)
2506{
2507 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2508 u32 info_len = attr->info.info_len;
2509 int err;
2510
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002511 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002512 if (err)
2513 return err;
2514
2515 return btf_get_info_by_fd(btf, attr, uattr);
2516}
2517
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002518#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2519
2520static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2521 union bpf_attr __user *uattr)
2522{
2523 int ufd = attr->info.bpf_fd;
2524 struct fd f;
2525 int err;
2526
2527 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2528 return -EINVAL;
2529
2530 f = fdget(ufd);
2531 if (!f.file)
2532 return -EBADFD;
2533
2534 if (f.file->f_op == &bpf_prog_fops)
2535 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2536 uattr);
2537 else if (f.file->f_op == &bpf_map_fops)
2538 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2539 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002540 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002541 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002542 else
2543 err = -EINVAL;
2544
2545 fdput(f);
2546 return err;
2547}
2548
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002549#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2550
2551static int bpf_btf_load(const union bpf_attr *attr)
2552{
2553 if (CHECK_ATTR(BPF_BTF_LOAD))
2554 return -EINVAL;
2555
2556 if (!capable(CAP_SYS_ADMIN))
2557 return -EPERM;
2558
2559 return btf_new_fd(attr);
2560}
2561
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002562#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2563
2564static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2565{
2566 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2567 return -EINVAL;
2568
2569 if (!capable(CAP_SYS_ADMIN))
2570 return -EPERM;
2571
2572 return btf_get_fd_by_id(attr->btf_id);
2573}
2574
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002575static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2576 union bpf_attr __user *uattr,
2577 u32 prog_id, u32 fd_type,
2578 const char *buf, u64 probe_offset,
2579 u64 probe_addr)
2580{
2581 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2582 u32 len = buf ? strlen(buf) : 0, input_len;
2583 int err = 0;
2584
2585 if (put_user(len, &uattr->task_fd_query.buf_len))
2586 return -EFAULT;
2587 input_len = attr->task_fd_query.buf_len;
2588 if (input_len && ubuf) {
2589 if (!len) {
2590 /* nothing to copy, just make ubuf NULL terminated */
2591 char zero = '\0';
2592
2593 if (put_user(zero, ubuf))
2594 return -EFAULT;
2595 } else if (input_len >= len + 1) {
2596 /* ubuf can hold the string with NULL terminator */
2597 if (copy_to_user(ubuf, buf, len + 1))
2598 return -EFAULT;
2599 } else {
2600 /* ubuf cannot hold the string with NULL terminator,
2601 * do a partial copy with NULL terminator.
2602 */
2603 char zero = '\0';
2604
2605 err = -ENOSPC;
2606 if (copy_to_user(ubuf, buf, input_len - 1))
2607 return -EFAULT;
2608 if (put_user(zero, ubuf + input_len - 1))
2609 return -EFAULT;
2610 }
2611 }
2612
2613 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2614 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2615 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2616 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2617 return -EFAULT;
2618
2619 return err;
2620}
2621
2622#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2623
2624static int bpf_task_fd_query(const union bpf_attr *attr,
2625 union bpf_attr __user *uattr)
2626{
2627 pid_t pid = attr->task_fd_query.pid;
2628 u32 fd = attr->task_fd_query.fd;
2629 const struct perf_event *event;
2630 struct files_struct *files;
2631 struct task_struct *task;
2632 struct file *file;
2633 int err;
2634
2635 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2636 return -EINVAL;
2637
2638 if (!capable(CAP_SYS_ADMIN))
2639 return -EPERM;
2640
2641 if (attr->task_fd_query.flags != 0)
2642 return -EINVAL;
2643
2644 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2645 if (!task)
2646 return -ENOENT;
2647
2648 files = get_files_struct(task);
2649 put_task_struct(task);
2650 if (!files)
2651 return -ENOENT;
2652
2653 err = 0;
2654 spin_lock(&files->file_lock);
2655 file = fcheck_files(files, fd);
2656 if (!file)
2657 err = -EBADF;
2658 else
2659 get_file(file);
2660 spin_unlock(&files->file_lock);
2661 put_files_struct(files);
2662
2663 if (err)
2664 goto out;
2665
2666 if (file->f_op == &bpf_raw_tp_fops) {
2667 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2668 struct bpf_raw_event_map *btp = raw_tp->btp;
2669
2670 err = bpf_task_fd_query_copy(attr, uattr,
2671 raw_tp->prog->aux->id,
2672 BPF_FD_TYPE_RAW_TRACEPOINT,
2673 btp->tp->name, 0, 0);
2674 goto put_file;
2675 }
2676
2677 event = perf_get_event(file);
2678 if (!IS_ERR(event)) {
2679 u64 probe_offset, probe_addr;
2680 u32 prog_id, fd_type;
2681 const char *buf;
2682
2683 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2684 &buf, &probe_offset,
2685 &probe_addr);
2686 if (!err)
2687 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2688 fd_type, buf,
2689 probe_offset,
2690 probe_addr);
2691 goto put_file;
2692 }
2693
2694 err = -ENOTSUPP;
2695put_file:
2696 fput(file);
2697out:
2698 return err;
2699}
2700
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002701SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2702{
2703 union bpf_attr attr = {};
2704 int err;
2705
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002706 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002707 return -EPERM;
2708
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002709 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002710 if (err)
2711 return err;
2712 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002713
2714 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2715 if (copy_from_user(&attr, uattr, size) != 0)
2716 return -EFAULT;
2717
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002718 err = security_bpf(cmd, &attr, size);
2719 if (err < 0)
2720 return err;
2721
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002722 switch (cmd) {
2723 case BPF_MAP_CREATE:
2724 err = map_create(&attr);
2725 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002726 case BPF_MAP_LOOKUP_ELEM:
2727 err = map_lookup_elem(&attr);
2728 break;
2729 case BPF_MAP_UPDATE_ELEM:
2730 err = map_update_elem(&attr);
2731 break;
2732 case BPF_MAP_DELETE_ELEM:
2733 err = map_delete_elem(&attr);
2734 break;
2735 case BPF_MAP_GET_NEXT_KEY:
2736 err = map_get_next_key(&attr);
2737 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002738 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08002739 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002740 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002741 case BPF_OBJ_PIN:
2742 err = bpf_obj_pin(&attr);
2743 break;
2744 case BPF_OBJ_GET:
2745 err = bpf_obj_get(&attr);
2746 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002747 case BPF_PROG_ATTACH:
2748 err = bpf_prog_attach(&attr);
2749 break;
2750 case BPF_PROG_DETACH:
2751 err = bpf_prog_detach(&attr);
2752 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002753 case BPF_PROG_QUERY:
2754 err = bpf_prog_query(&attr, uattr);
2755 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002756 case BPF_PROG_TEST_RUN:
2757 err = bpf_prog_test_run(&attr, uattr);
2758 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002759 case BPF_PROG_GET_NEXT_ID:
2760 err = bpf_obj_get_next_id(&attr, uattr,
2761 &prog_idr, &prog_idr_lock);
2762 break;
2763 case BPF_MAP_GET_NEXT_ID:
2764 err = bpf_obj_get_next_id(&attr, uattr,
2765 &map_idr, &map_idr_lock);
2766 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002767 case BPF_PROG_GET_FD_BY_ID:
2768 err = bpf_prog_get_fd_by_id(&attr);
2769 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002770 case BPF_MAP_GET_FD_BY_ID:
2771 err = bpf_map_get_fd_by_id(&attr);
2772 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002773 case BPF_OBJ_GET_INFO_BY_FD:
2774 err = bpf_obj_get_info_by_fd(&attr, uattr);
2775 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002776 case BPF_RAW_TRACEPOINT_OPEN:
2777 err = bpf_raw_tracepoint_open(&attr);
2778 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002779 case BPF_BTF_LOAD:
2780 err = bpf_btf_load(&attr);
2781 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002782 case BPF_BTF_GET_FD_BY_ID:
2783 err = bpf_btf_get_fd_by_id(&attr);
2784 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002785 case BPF_TASK_FD_QUERY:
2786 err = bpf_task_fd_query(&attr, uattr);
2787 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02002788 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2789 err = map_lookup_and_delete_elem(&attr);
2790 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002791 default:
2792 err = -EINVAL;
2793 break;
2794 }
2795
2796 return err;
2797}