blob: 068a4fc79ddb5dcaad3473f0e8376095f05f3d31 [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>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070014#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070015#include <linux/syscalls.h>
16#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010017#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010018#include <linux/vmalloc.h>
19#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070020#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070021#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070022#include <linux/license.h>
23#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070024#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010025#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070026#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070027#include <linux/cred.h>
28#include <linux/timekeeping.h>
29#include <linux/ctype.h>
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070030#include <linux/btf.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010031#include <linux/nospec.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070032
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070033#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
34 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
35 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
37#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
38#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
39
Chenbo Feng6e71b042017-10-18 13:00:22 -070040#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
41
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080042DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070043static DEFINE_IDR(prog_idr);
44static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070045static DEFINE_IDR(map_idr);
46static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080047
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070048int sysctl_unprivileged_bpf_disabled __read_mostly;
49
Johannes Berg40077e02017-04-11 15:34:58 +020050static const struct bpf_map_ops * const bpf_map_types[] = {
51#define BPF_PROG_TYPE(_id, _ops)
52#define BPF_MAP_TYPE(_id, _ops) \
53 [_id] = &_ops,
54#include <linux/bpf_types.h>
55#undef BPF_PROG_TYPE
56#undef BPF_MAP_TYPE
57};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070058
Mickaël Salaün752ba562017-08-07 20:45:20 +020059/*
60 * If we're handed a bigger struct than we know of, ensure all the unknown bits
61 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
62 * we don't know about yet.
63 *
64 * There is a ToCToU between this function call and the following
65 * copy_from_user() call. However, this is not a concern since this function is
66 * meant to be a future-proofing of bits.
67 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070068int bpf_check_uarg_tail_zero(void __user *uaddr,
69 size_t expected_size,
70 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020071{
72 unsigned char __user *addr;
73 unsigned char __user *end;
74 unsigned char val;
75 int err;
76
Mickaël Salaün752ba562017-08-07 20:45:20 +020077 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
78 return -E2BIG;
79
80 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
81 return -EFAULT;
82
Mickaël Salaün58291a72017-08-07 20:45:19 +020083 if (actual_size <= expected_size)
84 return 0;
85
86 addr = uaddr + expected_size;
87 end = uaddr + actual_size;
88
89 for (; addr < end; addr++) {
90 err = get_user(val, addr);
91 if (err)
92 return err;
93 if (val)
94 return -E2BIG;
95 }
96
97 return 0;
98}
99
Jakub Kicinskia3884572018-01-11 20:29:09 -0800100const struct bpf_map_ops bpf_map_offload_ops = {
101 .map_alloc = bpf_map_offload_map_alloc,
102 .map_free = bpf_map_offload_map_free,
103};
104
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700105static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
106{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800107 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100108 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700109 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800110 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700111
Mark Rutland9ef09e32018-05-03 17:04:59 +0100112 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800113 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100114 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
115 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800116 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200117 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700118
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800119 if (ops->map_alloc_check) {
120 err = ops->map_alloc_check(attr);
121 if (err)
122 return ERR_PTR(err);
123 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800124 if (attr->map_ifindex)
125 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800126 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200127 if (IS_ERR(map))
128 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800129 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100130 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200131 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700132}
133
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700134void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100135{
136 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
137 * trigger under memory pressure as we really just want to
138 * fail instead.
139 */
140 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
141 void *area;
142
143 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700144 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100145 if (area != NULL)
146 return area;
147 }
148
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700149 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
150 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100151}
152
153void bpf_map_area_free(void *area)
154{
155 kvfree(area);
156}
157
Jakub Kicinskibd475642018-01-11 20:29:06 -0800158void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
159{
160 map->map_type = attr->map_type;
161 map->key_size = attr->key_size;
162 map->value_size = attr->value_size;
163 map->max_entries = attr->max_entries;
164 map->map_flags = attr->map_flags;
165 map->numa_node = bpf_map_attr_numa_node(attr);
166}
167
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800168int bpf_map_precharge_memlock(u32 pages)
169{
170 struct user_struct *user = get_current_user();
171 unsigned long memlock_limit, cur;
172
173 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
174 cur = atomic_long_read(&user->locked_vm);
175 free_uid(user);
176 if (cur + pages > memlock_limit)
177 return -EPERM;
178 return 0;
179}
180
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700181static int bpf_map_charge_memlock(struct bpf_map *map)
182{
183 struct user_struct *user = get_current_user();
184 unsigned long memlock_limit;
185
186 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
187
188 atomic_long_add(map->pages, &user->locked_vm);
189
190 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
191 atomic_long_sub(map->pages, &user->locked_vm);
192 free_uid(user);
193 return -EPERM;
194 }
195 map->user = user;
196 return 0;
197}
198
199static void bpf_map_uncharge_memlock(struct bpf_map *map)
200{
201 struct user_struct *user = map->user;
202
203 atomic_long_sub(map->pages, &user->locked_vm);
204 free_uid(user);
205}
206
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700207static int bpf_map_alloc_id(struct bpf_map *map)
208{
209 int id;
210
Shaohua Lib76354c2018-03-27 11:53:21 -0700211 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700212 spin_lock_bh(&map_idr_lock);
213 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
214 if (id > 0)
215 map->id = id;
216 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700217 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700218
219 if (WARN_ON_ONCE(!id))
220 return -ENOSPC;
221
222 return id > 0 ? 0 : id;
223}
224
Jakub Kicinskia3884572018-01-11 20:29:09 -0800225void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700226{
Eric Dumazet930651a2017-09-19 09:15:59 -0700227 unsigned long flags;
228
Jakub Kicinskia3884572018-01-11 20:29:09 -0800229 /* Offloaded maps are removed from the IDR store when their device
230 * disappears - even if someone holds an fd to them they are unusable,
231 * the memory is gone, all ops will fail; they are simply waiting for
232 * refcnt to drop to be freed.
233 */
234 if (!map->id)
235 return;
236
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700237 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700238 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700239 else
240 __acquire(&map_idr_lock);
241
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700242 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800243 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700244
245 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700246 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700247 else
248 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700249}
250
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700251/* called from workqueue */
252static void bpf_map_free_deferred(struct work_struct *work)
253{
254 struct bpf_map *map = container_of(work, struct bpf_map, work);
255
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700256 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700257 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700258 /* implementation dependent freeing */
259 map->ops->map_free(map);
260}
261
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100262static void bpf_map_put_uref(struct bpf_map *map)
263{
264 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700265 if (map->ops->map_release_uref)
266 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100267 }
268}
269
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700270/* decrement map refcnt and schedule it for freeing via workqueue
271 * (unrelying map implementation ops->map_free() might sleep)
272 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700273static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700274{
275 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700276 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700277 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700278 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700279 INIT_WORK(&map->work, bpf_map_free_deferred);
280 schedule_work(&map->work);
281 }
282}
283
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700284void bpf_map_put(struct bpf_map *map)
285{
286 __bpf_map_put(map, true);
287}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700288EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700289
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100290void bpf_map_put_with_uref(struct bpf_map *map)
291{
292 bpf_map_put_uref(map);
293 bpf_map_put(map);
294}
295
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700296static int bpf_map_release(struct inode *inode, struct file *filp)
297{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200298 struct bpf_map *map = filp->private_data;
299
300 if (map->ops->map_release)
301 map->ops->map_release(map, filp);
302
303 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700304 return 0;
305}
306
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100307#ifdef CONFIG_PROC_FS
308static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
309{
310 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100311 const struct bpf_array *array;
312 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200313 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100314
315 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
316 array = container_of(map, struct bpf_array, map);
317 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200318 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100319 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100320
321 seq_printf(m,
322 "map_type:\t%u\n"
323 "key_size:\t%u\n"
324 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100325 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100326 "map_flags:\t%#x\n"
327 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100328 map->map_type,
329 map->key_size,
330 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100331 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100332 map->map_flags,
333 map->pages * 1ULL << PAGE_SHIFT);
334
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200335 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100336 seq_printf(m, "owner_prog_type:\t%u\n",
337 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200338 seq_printf(m, "owner_jited:\t%u\n",
339 owner_jited);
340 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100341}
342#endif
343
Chenbo Feng6e71b042017-10-18 13:00:22 -0700344static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
345 loff_t *ppos)
346{
347 /* We need this handler such that alloc_file() enables
348 * f_mode with FMODE_CAN_READ.
349 */
350 return -EINVAL;
351}
352
353static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
354 size_t siz, loff_t *ppos)
355{
356 /* We need this handler such that alloc_file() enables
357 * f_mode with FMODE_CAN_WRITE.
358 */
359 return -EINVAL;
360}
361
Chenbo Fengf66e4482017-10-18 13:00:26 -0700362const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100363#ifdef CONFIG_PROC_FS
364 .show_fdinfo = bpf_map_show_fdinfo,
365#endif
366 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700367 .read = bpf_dummy_read,
368 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700369};
370
Chenbo Feng6e71b042017-10-18 13:00:22 -0700371int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100372{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700373 int ret;
374
375 ret = security_bpf_map(map, OPEN_FMODE(flags));
376 if (ret < 0)
377 return ret;
378
Daniel Borkmannaa797812015-10-29 14:58:06 +0100379 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700380 flags | O_CLOEXEC);
381}
382
383int bpf_get_file_flag(int flags)
384{
385 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
386 return -EINVAL;
387 if (flags & BPF_F_RDONLY)
388 return O_RDONLY;
389 if (flags & BPF_F_WRONLY)
390 return O_WRONLY;
391 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100392}
393
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700394/* helper macro to check that unused fields 'union bpf_attr' are zero */
395#define CHECK_ATTR(CMD) \
396 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
397 sizeof(attr->CMD##_LAST_FIELD), 0, \
398 sizeof(*attr) - \
399 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
400 sizeof(attr->CMD##_LAST_FIELD)) != NULL
401
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700402/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
403 * Return 0 on success and < 0 on error.
404 */
405static int bpf_obj_name_cpy(char *dst, const char *src)
406{
407 const char *end = src + BPF_OBJ_NAME_LEN;
408
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700409 memset(dst, 0, BPF_OBJ_NAME_LEN);
410
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700411 /* Copy all isalnum() and '_' char */
412 while (src < end && *src) {
413 if (!isalnum(*src) && *src != '_')
414 return -EINVAL;
415 *dst++ = *src++;
416 }
417
418 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
419 if (src == end)
420 return -EINVAL;
421
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700422 return 0;
423}
424
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700425#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700426/* called via syscall */
427static int map_create(union bpf_attr *attr)
428{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700429 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700430 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700431 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700432 int err;
433
434 err = CHECK_ATTR(BPF_MAP_CREATE);
435 if (err)
436 return -EINVAL;
437
Chenbo Feng6e71b042017-10-18 13:00:22 -0700438 f_flags = bpf_get_file_flag(attr->map_flags);
439 if (f_flags < 0)
440 return f_flags;
441
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700442 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700443 ((unsigned int)numa_node >= nr_node_ids ||
444 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700445 return -EINVAL;
446
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700447 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
448 map = find_and_alloc_map(attr);
449 if (IS_ERR(map))
450 return PTR_ERR(map);
451
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700452 err = bpf_obj_name_cpy(map->name, attr->map_name);
453 if (err)
454 goto free_map_nouncharge;
455
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700456 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100457 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700458
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700459 if (bpf_map_support_seq_show(map) &&
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700460 (attr->btf_key_type_id || attr->btf_value_type_id)) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700461 struct btf *btf;
462
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700463 if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700464 err = -EINVAL;
465 goto free_map_nouncharge;
466 }
467
468 btf = btf_get_by_fd(attr->btf_fd);
469 if (IS_ERR(btf)) {
470 err = PTR_ERR(btf);
471 goto free_map_nouncharge;
472 }
473
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700474 err = map->ops->map_check_btf(map, btf, attr->btf_key_type_id,
475 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700476 if (err) {
477 btf_put(btf);
478 goto free_map_nouncharge;
479 }
480
481 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700482 map->btf_key_type_id = attr->btf_key_type_id;
483 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700484 }
485
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700486 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700487 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100488 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700489
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700490 err = bpf_map_charge_memlock(map);
491 if (err)
492 goto free_map_sec;
493
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700494 err = bpf_map_alloc_id(map);
495 if (err)
496 goto free_map;
497
Chenbo Feng6e71b042017-10-18 13:00:22 -0700498 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700499 if (err < 0) {
500 /* failed to allocate fd.
501 * bpf_map_put() is needed because the above
502 * bpf_map_alloc_id() has published the map
503 * to the userspace and the userspace may
504 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
505 */
506 bpf_map_put(map);
507 return err;
508 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700509
510 return err;
511
512free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100513 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700514free_map_sec:
515 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100516free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700517 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700518 map->ops->map_free(map);
519 return err;
520}
521
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700522/* if error is returned, fd is released.
523 * On success caller should complete fd access with matching fdput()
524 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100525struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700526{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700527 if (!f.file)
528 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700529 if (f.file->f_op != &bpf_map_fops) {
530 fdput(f);
531 return ERR_PTR(-EINVAL);
532 }
533
Daniel Borkmannc2101292015-10-29 14:58:07 +0100534 return f.file->private_data;
535}
536
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700537/* prog's and map's refcnt limit */
538#define BPF_MAX_REFCNT 32768
539
540struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100541{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700542 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
543 atomic_dec(&map->refcnt);
544 return ERR_PTR(-EBUSY);
545 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100546 if (uref)
547 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700548 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100549}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700550EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100551
552struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100553{
554 struct fd f = fdget(ufd);
555 struct bpf_map *map;
556
557 map = __bpf_map_get(f);
558 if (IS_ERR(map))
559 return map;
560
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700561 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100562 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700563
564 return map;
565}
566
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700567/* map_idr_lock should have been held */
568static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
569 bool uref)
570{
571 int refold;
572
573 refold = __atomic_add_unless(&map->refcnt, 1, 0);
574
575 if (refold >= BPF_MAX_REFCNT) {
576 __bpf_map_put(map, false);
577 return ERR_PTR(-EBUSY);
578 }
579
580 if (!refold)
581 return ERR_PTR(-ENOENT);
582
583 if (uref)
584 atomic_inc(&map->usercnt);
585
586 return map;
587}
588
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800589int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
590{
591 return -ENOTSUPP;
592}
593
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700594/* last field in 'union bpf_attr' used by this command */
595#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
596
597static int map_lookup_elem(union bpf_attr *attr)
598{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100599 void __user *ukey = u64_to_user_ptr(attr->key);
600 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700601 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700602 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800603 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800604 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200605 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700606 int err;
607
608 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
609 return -EINVAL;
610
Daniel Borkmann592867b2015-09-08 18:00:09 +0200611 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100612 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700613 if (IS_ERR(map))
614 return PTR_ERR(map);
615
Chenbo Feng6e71b042017-10-18 13:00:22 -0700616 if (!(f.file->f_mode & FMODE_CAN_READ)) {
617 err = -EPERM;
618 goto err_put;
619 }
620
Al Viroe4448ed2017-05-13 18:43:00 -0400621 key = memdup_user(ukey, map->key_size);
622 if (IS_ERR(key)) {
623 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700624 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400625 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700626
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800627 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800628 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800629 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
630 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700631 else if (IS_FD_MAP(map))
632 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800633 else
634 value_size = map->value_size;
635
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800636 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800637 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700638 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800639 goto free_key;
640
Jakub Kicinskia3884572018-01-11 20:29:09 -0800641 if (bpf_map_is_dev_bound(map)) {
642 err = bpf_map_offload_lookup_elem(map, key, value);
643 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
644 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800645 err = bpf_percpu_hash_copy(map, key, value);
646 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
647 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800648 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
649 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700650 } else if (IS_FD_ARRAY(map)) {
651 err = bpf_fd_array_map_lookup_elem(map, key, value);
652 } else if (IS_FD_HASH(map)) {
653 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800654 } else {
655 rcu_read_lock();
656 ptr = map->ops->map_lookup_elem(map, key);
657 if (ptr)
658 memcpy(value, ptr, value_size);
659 rcu_read_unlock();
660 err = ptr ? 0 : -ENOENT;
661 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800662
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800663 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800664 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700665
666 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800667 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800668 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700669
670 err = 0;
671
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800672free_value:
673 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700674free_key:
675 kfree(key);
676err_put:
677 fdput(f);
678 return err;
679}
680
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800681#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700682
683static int map_update_elem(union bpf_attr *attr)
684{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100685 void __user *ukey = u64_to_user_ptr(attr->key);
686 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700687 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700688 struct bpf_map *map;
689 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800690 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200691 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700692 int err;
693
694 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
695 return -EINVAL;
696
Daniel Borkmann592867b2015-09-08 18:00:09 +0200697 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100698 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700699 if (IS_ERR(map))
700 return PTR_ERR(map);
701
Chenbo Feng6e71b042017-10-18 13:00:22 -0700702 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
703 err = -EPERM;
704 goto err_put;
705 }
706
Al Viroe4448ed2017-05-13 18:43:00 -0400707 key = memdup_user(ukey, map->key_size);
708 if (IS_ERR(key)) {
709 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700710 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400711 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700712
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800713 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800714 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800715 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
716 value_size = round_up(map->value_size, 8) * num_possible_cpus();
717 else
718 value_size = map->value_size;
719
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700720 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800721 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700722 if (!value)
723 goto free_key;
724
725 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800726 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700727 goto free_value;
728
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200729 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800730 if (bpf_map_is_dev_bound(map)) {
731 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
732 goto out;
733 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200734 err = map->ops->map_update_elem(map, key, value, attr->flags);
735 goto out;
736 }
737
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800738 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
739 * inside bpf map update or delete otherwise deadlocks are possible
740 */
741 preempt_disable();
742 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800743 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
744 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800745 err = bpf_percpu_hash_update(map, key, value, attr->flags);
746 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
747 err = bpf_percpu_array_update(map, key, value, attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100748 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200749 rcu_read_lock();
750 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
751 attr->flags);
752 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700753 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
754 rcu_read_lock();
755 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
756 attr->flags);
757 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800758 } else {
759 rcu_read_lock();
760 err = map->ops->map_update_elem(map, key, value, attr->flags);
761 rcu_read_unlock();
762 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800763 __this_cpu_dec(bpf_prog_active);
764 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200765out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700766free_value:
767 kfree(value);
768free_key:
769 kfree(key);
770err_put:
771 fdput(f);
772 return err;
773}
774
775#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
776
777static int map_delete_elem(union bpf_attr *attr)
778{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100779 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700780 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700781 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200782 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700783 void *key;
784 int err;
785
786 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
787 return -EINVAL;
788
Daniel Borkmann592867b2015-09-08 18:00:09 +0200789 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100790 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700791 if (IS_ERR(map))
792 return PTR_ERR(map);
793
Chenbo Feng6e71b042017-10-18 13:00:22 -0700794 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
795 err = -EPERM;
796 goto err_put;
797 }
798
Al Viroe4448ed2017-05-13 18:43:00 -0400799 key = memdup_user(ukey, map->key_size);
800 if (IS_ERR(key)) {
801 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700802 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400803 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700804
Jakub Kicinskia3884572018-01-11 20:29:09 -0800805 if (bpf_map_is_dev_bound(map)) {
806 err = bpf_map_offload_delete_elem(map, key);
807 goto out;
808 }
809
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800810 preempt_disable();
811 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700812 rcu_read_lock();
813 err = map->ops->map_delete_elem(map, key);
814 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800815 __this_cpu_dec(bpf_prog_active);
816 preempt_enable();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800817out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700818 kfree(key);
819err_put:
820 fdput(f);
821 return err;
822}
823
824/* last field in 'union bpf_attr' used by this command */
825#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
826
827static int map_get_next_key(union bpf_attr *attr)
828{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100829 void __user *ukey = u64_to_user_ptr(attr->key);
830 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700831 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700832 struct bpf_map *map;
833 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200834 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700835 int err;
836
837 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
838 return -EINVAL;
839
Daniel Borkmann592867b2015-09-08 18:00:09 +0200840 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100841 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700842 if (IS_ERR(map))
843 return PTR_ERR(map);
844
Chenbo Feng6e71b042017-10-18 13:00:22 -0700845 if (!(f.file->f_mode & FMODE_CAN_READ)) {
846 err = -EPERM;
847 goto err_put;
848 }
849
Teng Qin8fe45922017-04-24 19:00:37 -0700850 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400851 key = memdup_user(ukey, map->key_size);
852 if (IS_ERR(key)) {
853 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700854 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400855 }
Teng Qin8fe45922017-04-24 19:00:37 -0700856 } else {
857 key = NULL;
858 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700859
860 err = -ENOMEM;
861 next_key = kmalloc(map->key_size, GFP_USER);
862 if (!next_key)
863 goto free_key;
864
Jakub Kicinskia3884572018-01-11 20:29:09 -0800865 if (bpf_map_is_dev_bound(map)) {
866 err = bpf_map_offload_get_next_key(map, key, next_key);
867 goto out;
868 }
869
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700870 rcu_read_lock();
871 err = map->ops->map_get_next_key(map, key, next_key);
872 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800873out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700874 if (err)
875 goto free_next_key;
876
877 err = -EFAULT;
878 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
879 goto free_next_key;
880
881 err = 0;
882
883free_next_key:
884 kfree(next_key);
885free_key:
886 kfree(key);
887err_put:
888 fdput(f);
889 return err;
890}
891
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700892static const struct bpf_prog_ops * const bpf_prog_types[] = {
893#define BPF_PROG_TYPE(_id, _name) \
894 [_id] = & _name ## _prog_ops,
895#define BPF_MAP_TYPE(_id, _ops)
896#include <linux/bpf_types.h>
897#undef BPF_PROG_TYPE
898#undef BPF_MAP_TYPE
899};
900
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700901static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
902{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +0200903 const struct bpf_prog_ops *ops;
904
905 if (type >= ARRAY_SIZE(bpf_prog_types))
906 return -EINVAL;
907 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
908 ops = bpf_prog_types[type];
909 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +0200910 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700911
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700912 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +0200913 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700914 else
915 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200916 prog->type = type;
917 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700918}
919
920/* drop refcnt on maps used by eBPF program and free auxilary data */
921static void free_used_maps(struct bpf_prog_aux *aux)
922{
923 int i;
924
925 for (i = 0; i < aux->used_map_cnt; i++)
926 bpf_map_put(aux->used_maps[i]);
927
928 kfree(aux->used_maps);
929}
930
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100931int __bpf_prog_charge(struct user_struct *user, u32 pages)
932{
933 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
934 unsigned long user_bufs;
935
936 if (user) {
937 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
938 if (user_bufs > memlock_limit) {
939 atomic_long_sub(pages, &user->locked_vm);
940 return -EPERM;
941 }
942 }
943
944 return 0;
945}
946
947void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
948{
949 if (user)
950 atomic_long_sub(pages, &user->locked_vm);
951}
952
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700953static int bpf_prog_charge_memlock(struct bpf_prog *prog)
954{
955 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100956 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700957
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100958 ret = __bpf_prog_charge(user, prog->pages);
959 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700960 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100961 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700962 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100963
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700964 prog->aux->user = user;
965 return 0;
966}
967
968static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
969{
970 struct user_struct *user = prog->aux->user;
971
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100972 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700973 free_uid(user);
974}
975
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700976static int bpf_prog_alloc_id(struct bpf_prog *prog)
977{
978 int id;
979
Shaohua Lib76354c2018-03-27 11:53:21 -0700980 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700981 spin_lock_bh(&prog_idr_lock);
982 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
983 if (id > 0)
984 prog->aux->id = id;
985 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700986 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700987
988 /* id is in [1, INT_MAX) */
989 if (WARN_ON_ONCE(!id))
990 return -ENOSPC;
991
992 return id > 0 ? 0 : id;
993}
994
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800995void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700996{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800997 /* cBPF to eBPF migrations are currently not in the idr store.
998 * Offloaded programs are removed from the store when their device
999 * disappears - even if someone grabs an fd to them they are unusable,
1000 * simply waiting for refcnt to drop to be freed.
1001 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001002 if (!prog->aux->id)
1003 return;
1004
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001005 if (do_idr_lock)
1006 spin_lock_bh(&prog_idr_lock);
1007 else
1008 __acquire(&prog_idr_lock);
1009
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001010 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001011 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001012
1013 if (do_idr_lock)
1014 spin_unlock_bh(&prog_idr_lock);
1015 else
1016 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001017}
1018
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001019static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001020{
1021 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1022
1023 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001024 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001025 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001026 bpf_prog_free(aux->prog);
1027}
1028
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001029static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001030{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001031 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001032 int i;
1033
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001034 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001035 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001036
1037 for (i = 0; i < prog->aux->func_cnt; i++)
1038 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +01001039 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001040
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001041 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001042 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001043}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001044
1045void bpf_prog_put(struct bpf_prog *prog)
1046{
1047 __bpf_prog_put(prog, true);
1048}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001049EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001050
1051static int bpf_prog_release(struct inode *inode, struct file *filp)
1052{
1053 struct bpf_prog *prog = filp->private_data;
1054
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001055 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001056 return 0;
1057}
1058
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001059#ifdef CONFIG_PROC_FS
1060static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1061{
1062 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001063 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001064
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001065 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001066 seq_printf(m,
1067 "prog_type:\t%u\n"
1068 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001069 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001070 "memlock:\t%llu\n",
1071 prog->type,
1072 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001073 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001074 prog->pages * 1ULL << PAGE_SHIFT);
1075}
1076#endif
1077
Chenbo Fengf66e4482017-10-18 13:00:26 -07001078const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001079#ifdef CONFIG_PROC_FS
1080 .show_fdinfo = bpf_prog_show_fdinfo,
1081#endif
1082 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001083 .read = bpf_dummy_read,
1084 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001085};
1086
Daniel Borkmannb2197752015-10-29 14:58:09 +01001087int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001088{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001089 int ret;
1090
1091 ret = security_bpf_prog(prog);
1092 if (ret < 0)
1093 return ret;
1094
Daniel Borkmannaa797812015-10-29 14:58:06 +01001095 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1096 O_RDWR | O_CLOEXEC);
1097}
1098
Daniel Borkmann113214b2016-06-30 17:24:44 +02001099static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001100{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001101 if (!f.file)
1102 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001103 if (f.file->f_op != &bpf_prog_fops) {
1104 fdput(f);
1105 return ERR_PTR(-EINVAL);
1106 }
1107
Daniel Borkmannc2101292015-10-29 14:58:07 +01001108 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001109}
1110
Brenden Blanco59d36562016-07-19 12:16:46 -07001111struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001112{
Brenden Blanco59d36562016-07-19 12:16:46 -07001113 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1114 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001115 return ERR_PTR(-EBUSY);
1116 }
1117 return prog;
1118}
Brenden Blanco59d36562016-07-19 12:16:46 -07001119EXPORT_SYMBOL_GPL(bpf_prog_add);
1120
Daniel Borkmannc5405942016-11-09 22:02:34 +01001121void bpf_prog_sub(struct bpf_prog *prog, int i)
1122{
1123 /* Only to be used for undoing previous bpf_prog_add() in some
1124 * error path. We still know that another entity in our call
1125 * path holds a reference to the program, thus atomic_sub() can
1126 * be safely used in such cases!
1127 */
1128 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1129}
1130EXPORT_SYMBOL_GPL(bpf_prog_sub);
1131
Brenden Blanco59d36562016-07-19 12:16:46 -07001132struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1133{
1134 return bpf_prog_add(prog, 1);
1135}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001136EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001137
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001138/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001139struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001140{
1141 int refold;
1142
1143 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1144
1145 if (refold >= BPF_MAX_REFCNT) {
1146 __bpf_prog_put(prog, false);
1147 return ERR_PTR(-EBUSY);
1148 }
1149
1150 if (!refold)
1151 return ERR_PTR(-ENOENT);
1152
1153 return prog;
1154}
John Fastabenda6f6df62017-08-15 22:32:22 -07001155EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001156
Al Viro040ee692017-12-02 20:20:38 -05001157bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001158 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001159{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001160 /* not an attachment, just a refcount inc, always allow */
1161 if (!attach_type)
1162 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001163
1164 if (prog->type != *attach_type)
1165 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001166 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001167 return false;
1168
1169 return true;
1170}
1171
1172static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001173 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001174{
1175 struct fd f = fdget(ufd);
1176 struct bpf_prog *prog;
1177
Daniel Borkmann113214b2016-06-30 17:24:44 +02001178 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001179 if (IS_ERR(prog))
1180 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001181 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001182 prog = ERR_PTR(-EINVAL);
1183 goto out;
1184 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001185
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001186 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001187out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001188 fdput(f);
1189 return prog;
1190}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001191
1192struct bpf_prog *bpf_prog_get(u32 ufd)
1193{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001194 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001195}
1196
Jakub Kicinski248f3462017-11-03 13:56:20 -07001197struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001198 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001199{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001200 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001201}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001202EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001203
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001204/* Initially all BPF programs could be loaded w/o specifying
1205 * expected_attach_type. Later for some of them specifying expected_attach_type
1206 * at load time became required so that program could be validated properly.
1207 * Programs of types that are allowed to be loaded both w/ and w/o (for
1208 * backward compatibility) expected_attach_type, should have the default attach
1209 * type assigned to expected_attach_type for the latter case, so that it can be
1210 * validated later at attach time.
1211 *
1212 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1213 * prog type requires it but has some attach types that have to be backward
1214 * compatible.
1215 */
1216static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1217{
1218 switch (attr->prog_type) {
1219 case BPF_PROG_TYPE_CGROUP_SOCK:
1220 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1221 * exist so checking for non-zero is the way to go here.
1222 */
1223 if (!attr->expected_attach_type)
1224 attr->expected_attach_type =
1225 BPF_CGROUP_INET_SOCK_CREATE;
1226 break;
1227 }
1228}
1229
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001230static int
1231bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1232 enum bpf_attach_type expected_attach_type)
1233{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001234 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001235 case BPF_PROG_TYPE_CGROUP_SOCK:
1236 switch (expected_attach_type) {
1237 case BPF_CGROUP_INET_SOCK_CREATE:
1238 case BPF_CGROUP_INET4_POST_BIND:
1239 case BPF_CGROUP_INET6_POST_BIND:
1240 return 0;
1241 default:
1242 return -EINVAL;
1243 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001244 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1245 switch (expected_attach_type) {
1246 case BPF_CGROUP_INET4_BIND:
1247 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001248 case BPF_CGROUP_INET4_CONNECT:
1249 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001250 return 0;
1251 default:
1252 return -EINVAL;
1253 }
1254 default:
1255 return 0;
1256 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001257}
1258
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001259/* last field in 'union bpf_attr' used by this command */
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001260#define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001261
1262static int bpf_prog_load(union bpf_attr *attr)
1263{
1264 enum bpf_prog_type type = attr->prog_type;
1265 struct bpf_prog *prog;
1266 int err;
1267 char license[128];
1268 bool is_gpl;
1269
1270 if (CHECK_ATTR(BPF_PROG_LOAD))
1271 return -EINVAL;
1272
David S. Millere07b98d2017-05-10 11:38:07 -07001273 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1274 return -EINVAL;
1275
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001276 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001277 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001278 sizeof(license) - 1) < 0)
1279 return -EFAULT;
1280 license[sizeof(license) - 1] = 0;
1281
1282 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1283 is_gpl = license_is_gpl_compatible(license);
1284
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001285 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1286 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001287
Alexei Starovoitov25415172015-03-25 12:49:20 -07001288 if (type == BPF_PROG_TYPE_KPROBE &&
1289 attr->kern_version != LINUX_VERSION_CODE)
1290 return -EINVAL;
1291
Chenbo Feng80b7d812017-05-31 18:16:00 -07001292 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1293 type != BPF_PROG_TYPE_CGROUP_SKB &&
1294 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001295 return -EPERM;
1296
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001297 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001298 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1299 return -EINVAL;
1300
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001301 /* plain bpf_prog allocation */
1302 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1303 if (!prog)
1304 return -ENOMEM;
1305
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001306 prog->expected_attach_type = attr->expected_attach_type;
1307
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001308 prog->aux->offload_requested = !!attr->prog_ifindex;
1309
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001310 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001311 if (err)
1312 goto free_prog_nouncharge;
1313
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001314 err = bpf_prog_charge_memlock(prog);
1315 if (err)
1316 goto free_prog_sec;
1317
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001318 prog->len = attr->insn_cnt;
1319
1320 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001321 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001322 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001323 goto free_prog;
1324
1325 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001326 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001327
1328 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001329 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001330
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001331 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001332 err = bpf_prog_offload_init(prog, attr);
1333 if (err)
1334 goto free_prog;
1335 }
1336
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001337 /* find program type: socket_filter vs tracing_filter */
1338 err = find_prog_type(type, prog);
1339 if (err < 0)
1340 goto free_prog;
1341
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001342 prog->aux->load_time = ktime_get_boot_ns();
1343 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1344 if (err)
1345 goto free_prog;
1346
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001347 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001348 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001349 if (err < 0)
1350 goto free_used_maps;
1351
1352 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001353 if (!prog->bpf_func)
1354 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001355 if (err < 0)
1356 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001357
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001358 err = bpf_prog_alloc_id(prog);
1359 if (err)
1360 goto free_used_maps;
1361
Daniel Borkmannaa797812015-10-29 14:58:06 +01001362 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001363 if (err < 0) {
1364 /* failed to allocate fd.
1365 * bpf_prog_put() is needed because the above
1366 * bpf_prog_alloc_id() has published the prog
1367 * to the userspace and the userspace may
1368 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1369 */
1370 bpf_prog_put(prog);
1371 return err;
1372 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001373
Daniel Borkmann74451e662017-02-16 22:24:50 +01001374 bpf_prog_kallsyms_add(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001375 return err;
1376
1377free_used_maps:
1378 free_used_maps(prog->aux);
1379free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001380 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001381free_prog_sec:
1382 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001383free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001384 bpf_prog_free(prog);
1385 return err;
1386}
1387
Chenbo Feng6e71b042017-10-18 13:00:22 -07001388#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001389
1390static int bpf_obj_pin(const union bpf_attr *attr)
1391{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001392 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001393 return -EINVAL;
1394
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001395 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001396}
1397
1398static int bpf_obj_get(const union bpf_attr *attr)
1399{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001400 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1401 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001402 return -EINVAL;
1403
Chenbo Feng6e71b042017-10-18 13:00:22 -07001404 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1405 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001406}
1407
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001408struct bpf_raw_tracepoint {
1409 struct bpf_raw_event_map *btp;
1410 struct bpf_prog *prog;
1411};
1412
1413static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1414{
1415 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1416
1417 if (raw_tp->prog) {
1418 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1419 bpf_prog_put(raw_tp->prog);
1420 }
1421 kfree(raw_tp);
1422 return 0;
1423}
1424
1425static const struct file_operations bpf_raw_tp_fops = {
1426 .release = bpf_raw_tracepoint_release,
1427 .read = bpf_dummy_read,
1428 .write = bpf_dummy_write,
1429};
1430
1431#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1432
1433static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1434{
1435 struct bpf_raw_tracepoint *raw_tp;
1436 struct bpf_raw_event_map *btp;
1437 struct bpf_prog *prog;
1438 char tp_name[128];
1439 int tp_fd, err;
1440
1441 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1442 sizeof(tp_name) - 1) < 0)
1443 return -EFAULT;
1444 tp_name[sizeof(tp_name) - 1] = 0;
1445
1446 btp = bpf_find_raw_tracepoint(tp_name);
1447 if (!btp)
1448 return -ENOENT;
1449
1450 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1451 if (!raw_tp)
1452 return -ENOMEM;
1453 raw_tp->btp = btp;
1454
1455 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1456 BPF_PROG_TYPE_RAW_TRACEPOINT);
1457 if (IS_ERR(prog)) {
1458 err = PTR_ERR(prog);
1459 goto out_free_tp;
1460 }
1461
1462 err = bpf_probe_register(raw_tp->btp, prog);
1463 if (err)
1464 goto out_put_prog;
1465
1466 raw_tp->prog = prog;
1467 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1468 O_CLOEXEC);
1469 if (tp_fd < 0) {
1470 bpf_probe_unregister(raw_tp->btp, prog);
1471 err = tp_fd;
1472 goto out_put_prog;
1473 }
1474 return tp_fd;
1475
1476out_put_prog:
1477 bpf_prog_put(prog);
1478out_free_tp:
1479 kfree(raw_tp);
1480 return err;
1481}
1482
Daniel Mackf4324552016-11-23 16:52:27 +01001483#ifdef CONFIG_CGROUP_BPF
1484
Anders Roxell33491582018-04-03 14:09:47 +02001485static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1486 enum bpf_attach_type attach_type)
1487{
1488 switch (prog->type) {
1489 case BPF_PROG_TYPE_CGROUP_SOCK:
1490 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1491 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1492 default:
1493 return 0;
1494 }
1495}
1496
John Fastabend464bc0f2017-08-28 07:10:04 -07001497#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001498
John Fastabend4f738ad2018-03-18 12:57:10 -07001499static int sockmap_get_from_fd(const union bpf_attr *attr,
1500 int type, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001501{
John Fastabend5a67da22017-09-08 14:00:49 -07001502 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001503 int ufd = attr->target_fd;
1504 struct bpf_map *map;
1505 struct fd f;
1506 int err;
1507
1508 f = fdget(ufd);
1509 map = __bpf_map_get(f);
1510 if (IS_ERR(map))
1511 return PTR_ERR(map);
1512
John Fastabend5a67da22017-09-08 14:00:49 -07001513 if (attach) {
John Fastabend4f738ad2018-03-18 12:57:10 -07001514 prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
John Fastabend5a67da22017-09-08 14:00:49 -07001515 if (IS_ERR(prog)) {
1516 fdput(f);
1517 return PTR_ERR(prog);
1518 }
John Fastabend174a79f2017-08-15 22:32:47 -07001519 }
1520
John Fastabend5a67da22017-09-08 14:00:49 -07001521 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001522 if (err) {
1523 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001524 if (prog)
1525 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001526 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001527 }
1528
1529 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001530 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001531}
Daniel Mackf4324552016-11-23 16:52:27 +01001532
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001533#define BPF_F_ATTACH_MASK \
1534 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1535
Daniel Mackf4324552016-11-23 16:52:27 +01001536static int bpf_prog_attach(const union bpf_attr *attr)
1537{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001538 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001539 struct bpf_prog *prog;
1540 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001541 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001542
1543 if (!capable(CAP_NET_ADMIN))
1544 return -EPERM;
1545
1546 if (CHECK_ATTR(BPF_PROG_ATTACH))
1547 return -EINVAL;
1548
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001549 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001550 return -EINVAL;
1551
Daniel Mackf4324552016-11-23 16:52:27 +01001552 switch (attr->attach_type) {
1553 case BPF_CGROUP_INET_INGRESS:
1554 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001555 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001556 break;
David Ahern610236582016-12-01 08:48:04 -08001557 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001558 case BPF_CGROUP_INET4_POST_BIND:
1559 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001560 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1561 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001562 case BPF_CGROUP_INET4_BIND:
1563 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001564 case BPF_CGROUP_INET4_CONNECT:
1565 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001566 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1567 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001568 case BPF_CGROUP_SOCK_OPS:
1569 ptype = BPF_PROG_TYPE_SOCK_OPS;
1570 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001571 case BPF_CGROUP_DEVICE:
1572 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1573 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001574 case BPF_SK_MSG_VERDICT:
1575 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
John Fastabend464bc0f2017-08-28 07:10:04 -07001576 case BPF_SK_SKB_STREAM_PARSER:
1577 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001578 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001579 default:
1580 return -EINVAL;
1581 }
1582
David Ahernb2cd1252016-12-01 08:48:03 -08001583 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1584 if (IS_ERR(prog))
1585 return PTR_ERR(prog);
1586
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001587 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1588 bpf_prog_put(prog);
1589 return -EINVAL;
1590 }
1591
David Ahernb2cd1252016-12-01 08:48:03 -08001592 cgrp = cgroup_get_from_fd(attr->target_fd);
1593 if (IS_ERR(cgrp)) {
1594 bpf_prog_put(prog);
1595 return PTR_ERR(cgrp);
1596 }
1597
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001598 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1599 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001600 if (ret)
1601 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001602 cgroup_put(cgrp);
1603
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001604 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001605}
1606
1607#define BPF_PROG_DETACH_LAST_FIELD attach_type
1608
1609static int bpf_prog_detach(const union bpf_attr *attr)
1610{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001611 enum bpf_prog_type ptype;
1612 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001613 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001614 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001615
1616 if (!capable(CAP_NET_ADMIN))
1617 return -EPERM;
1618
1619 if (CHECK_ATTR(BPF_PROG_DETACH))
1620 return -EINVAL;
1621
1622 switch (attr->attach_type) {
1623 case BPF_CGROUP_INET_INGRESS:
1624 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001625 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1626 break;
David Ahern610236582016-12-01 08:48:04 -08001627 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001628 case BPF_CGROUP_INET4_POST_BIND:
1629 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001630 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1631 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001632 case BPF_CGROUP_INET4_BIND:
1633 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001634 case BPF_CGROUP_INET4_CONNECT:
1635 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001636 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1637 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001638 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001639 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001640 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001641 case BPF_CGROUP_DEVICE:
1642 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1643 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001644 case BPF_SK_MSG_VERDICT:
1645 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
John Fastabend5a67da22017-09-08 14:00:49 -07001646 case BPF_SK_SKB_STREAM_PARSER:
1647 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001648 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001649 default:
1650 return -EINVAL;
1651 }
1652
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001653 cgrp = cgroup_get_from_fd(attr->target_fd);
1654 if (IS_ERR(cgrp))
1655 return PTR_ERR(cgrp);
1656
1657 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1658 if (IS_ERR(prog))
1659 prog = NULL;
1660
1661 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1662 if (prog)
1663 bpf_prog_put(prog);
1664 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001665 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001666}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001667
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001668#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1669
1670static int bpf_prog_query(const union bpf_attr *attr,
1671 union bpf_attr __user *uattr)
1672{
1673 struct cgroup *cgrp;
1674 int ret;
1675
1676 if (!capable(CAP_NET_ADMIN))
1677 return -EPERM;
1678 if (CHECK_ATTR(BPF_PROG_QUERY))
1679 return -EINVAL;
1680 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1681 return -EINVAL;
1682
1683 switch (attr->query.attach_type) {
1684 case BPF_CGROUP_INET_INGRESS:
1685 case BPF_CGROUP_INET_EGRESS:
1686 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001687 case BPF_CGROUP_INET4_BIND:
1688 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001689 case BPF_CGROUP_INET4_POST_BIND:
1690 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001691 case BPF_CGROUP_INET4_CONNECT:
1692 case BPF_CGROUP_INET6_CONNECT:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001693 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001694 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001695 break;
1696 default:
1697 return -EINVAL;
1698 }
1699 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1700 if (IS_ERR(cgrp))
1701 return PTR_ERR(cgrp);
1702 ret = cgroup_bpf_query(cgrp, attr, uattr);
1703 cgroup_put(cgrp);
1704 return ret;
1705}
Daniel Mackf4324552016-11-23 16:52:27 +01001706#endif /* CONFIG_CGROUP_BPF */
1707
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001708#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1709
1710static int bpf_prog_test_run(const union bpf_attr *attr,
1711 union bpf_attr __user *uattr)
1712{
1713 struct bpf_prog *prog;
1714 int ret = -ENOTSUPP;
1715
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001716 if (!capable(CAP_SYS_ADMIN))
1717 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001718 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1719 return -EINVAL;
1720
1721 prog = bpf_prog_get(attr->test.prog_fd);
1722 if (IS_ERR(prog))
1723 return PTR_ERR(prog);
1724
1725 if (prog->aux->ops->test_run)
1726 ret = prog->aux->ops->test_run(prog, attr, uattr);
1727
1728 bpf_prog_put(prog);
1729 return ret;
1730}
1731
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001732#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1733
1734static int bpf_obj_get_next_id(const union bpf_attr *attr,
1735 union bpf_attr __user *uattr,
1736 struct idr *idr,
1737 spinlock_t *lock)
1738{
1739 u32 next_id = attr->start_id;
1740 int err = 0;
1741
1742 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1743 return -EINVAL;
1744
1745 if (!capable(CAP_SYS_ADMIN))
1746 return -EPERM;
1747
1748 next_id++;
1749 spin_lock_bh(lock);
1750 if (!idr_get_next(idr, &next_id))
1751 err = -ENOENT;
1752 spin_unlock_bh(lock);
1753
1754 if (!err)
1755 err = put_user(next_id, &uattr->next_id);
1756
1757 return err;
1758}
1759
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001760#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1761
1762static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1763{
1764 struct bpf_prog *prog;
1765 u32 id = attr->prog_id;
1766 int fd;
1767
1768 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1769 return -EINVAL;
1770
1771 if (!capable(CAP_SYS_ADMIN))
1772 return -EPERM;
1773
1774 spin_lock_bh(&prog_idr_lock);
1775 prog = idr_find(&prog_idr, id);
1776 if (prog)
1777 prog = bpf_prog_inc_not_zero(prog);
1778 else
1779 prog = ERR_PTR(-ENOENT);
1780 spin_unlock_bh(&prog_idr_lock);
1781
1782 if (IS_ERR(prog))
1783 return PTR_ERR(prog);
1784
1785 fd = bpf_prog_new_fd(prog);
1786 if (fd < 0)
1787 bpf_prog_put(prog);
1788
1789 return fd;
1790}
1791
Chenbo Feng6e71b042017-10-18 13:00:22 -07001792#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001793
1794static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1795{
1796 struct bpf_map *map;
1797 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001798 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001799 int fd;
1800
Chenbo Feng6e71b042017-10-18 13:00:22 -07001801 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1802 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001803 return -EINVAL;
1804
1805 if (!capable(CAP_SYS_ADMIN))
1806 return -EPERM;
1807
Chenbo Feng6e71b042017-10-18 13:00:22 -07001808 f_flags = bpf_get_file_flag(attr->open_flags);
1809 if (f_flags < 0)
1810 return f_flags;
1811
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001812 spin_lock_bh(&map_idr_lock);
1813 map = idr_find(&map_idr, id);
1814 if (map)
1815 map = bpf_map_inc_not_zero(map, true);
1816 else
1817 map = ERR_PTR(-ENOENT);
1818 spin_unlock_bh(&map_idr_lock);
1819
1820 if (IS_ERR(map))
1821 return PTR_ERR(map);
1822
Chenbo Feng6e71b042017-10-18 13:00:22 -07001823 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001824 if (fd < 0)
1825 bpf_map_put(map);
1826
1827 return fd;
1828}
1829
Daniel Borkmann7105e822017-12-20 13:42:57 +01001830static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1831 unsigned long addr)
1832{
1833 int i;
1834
1835 for (i = 0; i < prog->aux->used_map_cnt; i++)
1836 if (prog->aux->used_maps[i] == (void *)addr)
1837 return prog->aux->used_maps[i];
1838 return NULL;
1839}
1840
1841static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1842{
1843 const struct bpf_map *map;
1844 struct bpf_insn *insns;
1845 u64 imm;
1846 int i;
1847
1848 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1849 GFP_USER);
1850 if (!insns)
1851 return insns;
1852
1853 for (i = 0; i < prog->len; i++) {
1854 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1855 insns[i].code = BPF_JMP | BPF_CALL;
1856 insns[i].imm = BPF_FUNC_tail_call;
1857 /* fall-through */
1858 }
1859 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1860 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1861 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1862 insns[i].code = BPF_JMP | BPF_CALL;
1863 if (!bpf_dump_raw_ok())
1864 insns[i].imm = 0;
1865 continue;
1866 }
1867
1868 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1869 continue;
1870
1871 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1872 map = bpf_map_from_imm(prog, imm);
1873 if (map) {
1874 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1875 insns[i].imm = map->id;
1876 insns[i + 1].imm = 0;
1877 continue;
1878 }
1879
1880 if (!bpf_dump_raw_ok() &&
1881 imm == (unsigned long)prog->aux) {
1882 insns[i].imm = 0;
1883 insns[i + 1].imm = 0;
1884 continue;
1885 }
1886 }
1887
1888 return insns;
1889}
1890
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001891static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1892 const union bpf_attr *attr,
1893 union bpf_attr __user *uattr)
1894{
1895 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1896 struct bpf_prog_info info = {};
1897 u32 info_len = attr->info.info_len;
1898 char __user *uinsns;
1899 u32 ulen;
1900 int err;
1901
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07001902 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001903 if (err)
1904 return err;
1905 info_len = min_t(u32, sizeof(info), info_len);
1906
1907 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001908 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001909
1910 info.type = prog->type;
1911 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001912 info.load_time = prog->aux->load_time;
1913 info.created_by_uid = from_kuid_munged(current_user_ns(),
1914 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02001915 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001916
1917 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001918 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1919
1920 ulen = info.nr_map_ids;
1921 info.nr_map_ids = prog->aux->used_map_cnt;
1922 ulen = min_t(u32, info.nr_map_ids, ulen);
1923 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001924 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001925 u32 i;
1926
1927 for (i = 0; i < ulen; i++)
1928 if (put_user(prog->aux->used_maps[i]->id,
1929 &user_map_ids[i]))
1930 return -EFAULT;
1931 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001932
1933 if (!capable(CAP_SYS_ADMIN)) {
1934 info.jited_prog_len = 0;
1935 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05301936 info.nr_jited_ksyms = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001937 goto done;
1938 }
1939
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001940 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001941 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001942 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001943 struct bpf_insn *insns_sanitized;
1944 bool fault;
1945
1946 if (prog->blinded && !bpf_dump_raw_ok()) {
1947 info.xlated_prog_insns = 0;
1948 goto done;
1949 }
1950 insns_sanitized = bpf_insn_prepare_dump(prog);
1951 if (!insns_sanitized)
1952 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001953 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1954 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001955 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1956 kfree(insns_sanitized);
1957 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001958 return -EFAULT;
1959 }
1960
Jakub Kicinski675fc272017-12-27 18:39:09 -08001961 if (bpf_prog_is_dev_bound(prog->aux)) {
1962 err = bpf_prog_offload_info_fill(&info, prog);
1963 if (err)
1964 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001965 goto done;
1966 }
1967
1968 /* NOTE: the following code is supposed to be skipped for offload.
1969 * bpf_prog_offload_info_fill() is the place to fill similar fields
1970 * for offload.
1971 */
1972 ulen = info.jited_prog_len;
1973 info.jited_prog_len = prog->jited_len;
1974 if (info.jited_prog_len && ulen) {
1975 if (bpf_dump_raw_ok()) {
1976 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1977 ulen = min_t(u32, info.jited_prog_len, ulen);
1978 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1979 return -EFAULT;
1980 } else {
1981 info.jited_prog_insns = 0;
1982 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08001983 }
1984
Sandipan Dasdbecd732018-05-24 12:26:48 +05301985 ulen = info.nr_jited_ksyms;
1986 info.nr_jited_ksyms = prog->aux->func_cnt;
1987 if (info.nr_jited_ksyms && ulen) {
1988 if (bpf_dump_raw_ok()) {
1989 u64 __user *user_ksyms;
1990 ulong ksym_addr;
1991 u32 i;
1992
1993 /* copy the address of the kernel symbol
1994 * corresponding to each function
1995 */
1996 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
1997 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
1998 for (i = 0; i < ulen; i++) {
1999 ksym_addr = (ulong) prog->aux->func[i]->bpf_func;
2000 ksym_addr &= PAGE_MASK;
2001 if (put_user((u64) ksym_addr, &user_ksyms[i]))
2002 return -EFAULT;
2003 }
2004 } else {
2005 info.jited_ksyms = 0;
2006 }
2007 }
2008
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002009done:
2010 if (copy_to_user(uinfo, &info, info_len) ||
2011 put_user(info_len, &uattr->info.info_len))
2012 return -EFAULT;
2013
2014 return 0;
2015}
2016
2017static int bpf_map_get_info_by_fd(struct bpf_map *map,
2018 const union bpf_attr *attr,
2019 union bpf_attr __user *uattr)
2020{
2021 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2022 struct bpf_map_info info = {};
2023 u32 info_len = attr->info.info_len;
2024 int err;
2025
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002026 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002027 if (err)
2028 return err;
2029 info_len = min_t(u32, sizeof(info), info_len);
2030
2031 info.type = map->map_type;
2032 info.id = map->id;
2033 info.key_size = map->key_size;
2034 info.value_size = map->value_size;
2035 info.max_entries = map->max_entries;
2036 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002037 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002038
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002039 if (map->btf) {
2040 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002041 info.btf_key_type_id = map->btf_key_type_id;
2042 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002043 }
2044
Jakub Kicinski52775b32018-01-17 19:13:28 -08002045 if (bpf_map_is_dev_bound(map)) {
2046 err = bpf_map_offload_info_fill(&info, map);
2047 if (err)
2048 return err;
2049 }
2050
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002051 if (copy_to_user(uinfo, &info, info_len) ||
2052 put_user(info_len, &uattr->info.info_len))
2053 return -EFAULT;
2054
2055 return 0;
2056}
2057
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002058static int bpf_btf_get_info_by_fd(struct btf *btf,
2059 const union bpf_attr *attr,
2060 union bpf_attr __user *uattr)
2061{
2062 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2063 u32 info_len = attr->info.info_len;
2064 int err;
2065
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002066 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002067 if (err)
2068 return err;
2069
2070 return btf_get_info_by_fd(btf, attr, uattr);
2071}
2072
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002073#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2074
2075static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2076 union bpf_attr __user *uattr)
2077{
2078 int ufd = attr->info.bpf_fd;
2079 struct fd f;
2080 int err;
2081
2082 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2083 return -EINVAL;
2084
2085 f = fdget(ufd);
2086 if (!f.file)
2087 return -EBADFD;
2088
2089 if (f.file->f_op == &bpf_prog_fops)
2090 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2091 uattr);
2092 else if (f.file->f_op == &bpf_map_fops)
2093 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2094 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002095 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002096 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002097 else
2098 err = -EINVAL;
2099
2100 fdput(f);
2101 return err;
2102}
2103
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002104#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2105
2106static int bpf_btf_load(const union bpf_attr *attr)
2107{
2108 if (CHECK_ATTR(BPF_BTF_LOAD))
2109 return -EINVAL;
2110
2111 if (!capable(CAP_SYS_ADMIN))
2112 return -EPERM;
2113
2114 return btf_new_fd(attr);
2115}
2116
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002117#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2118
2119static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2120{
2121 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2122 return -EINVAL;
2123
2124 if (!capable(CAP_SYS_ADMIN))
2125 return -EPERM;
2126
2127 return btf_get_fd_by_id(attr->btf_id);
2128}
2129
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002130SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2131{
2132 union bpf_attr attr = {};
2133 int err;
2134
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002135 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002136 return -EPERM;
2137
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002138 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002139 if (err)
2140 return err;
2141 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002142
2143 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2144 if (copy_from_user(&attr, uattr, size) != 0)
2145 return -EFAULT;
2146
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002147 err = security_bpf(cmd, &attr, size);
2148 if (err < 0)
2149 return err;
2150
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002151 switch (cmd) {
2152 case BPF_MAP_CREATE:
2153 err = map_create(&attr);
2154 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002155 case BPF_MAP_LOOKUP_ELEM:
2156 err = map_lookup_elem(&attr);
2157 break;
2158 case BPF_MAP_UPDATE_ELEM:
2159 err = map_update_elem(&attr);
2160 break;
2161 case BPF_MAP_DELETE_ELEM:
2162 err = map_delete_elem(&attr);
2163 break;
2164 case BPF_MAP_GET_NEXT_KEY:
2165 err = map_get_next_key(&attr);
2166 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002167 case BPF_PROG_LOAD:
2168 err = bpf_prog_load(&attr);
2169 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002170 case BPF_OBJ_PIN:
2171 err = bpf_obj_pin(&attr);
2172 break;
2173 case BPF_OBJ_GET:
2174 err = bpf_obj_get(&attr);
2175 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002176#ifdef CONFIG_CGROUP_BPF
2177 case BPF_PROG_ATTACH:
2178 err = bpf_prog_attach(&attr);
2179 break;
2180 case BPF_PROG_DETACH:
2181 err = bpf_prog_detach(&attr);
2182 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002183 case BPF_PROG_QUERY:
2184 err = bpf_prog_query(&attr, uattr);
2185 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002186#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002187 case BPF_PROG_TEST_RUN:
2188 err = bpf_prog_test_run(&attr, uattr);
2189 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002190 case BPF_PROG_GET_NEXT_ID:
2191 err = bpf_obj_get_next_id(&attr, uattr,
2192 &prog_idr, &prog_idr_lock);
2193 break;
2194 case BPF_MAP_GET_NEXT_ID:
2195 err = bpf_obj_get_next_id(&attr, uattr,
2196 &map_idr, &map_idr_lock);
2197 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002198 case BPF_PROG_GET_FD_BY_ID:
2199 err = bpf_prog_get_fd_by_id(&attr);
2200 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002201 case BPF_MAP_GET_FD_BY_ID:
2202 err = bpf_map_get_fd_by_id(&attr);
2203 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002204 case BPF_OBJ_GET_INFO_BY_FD:
2205 err = bpf_obj_get_info_by_fd(&attr, uattr);
2206 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002207 case BPF_RAW_TRACEPOINT_OPEN:
2208 err = bpf_raw_tracepoint_open(&attr);
2209 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002210 case BPF_BTF_LOAD:
2211 err = bpf_btf_load(&attr);
2212 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002213 case BPF_BTF_GET_FD_BY_ID:
2214 err = bpf_btf_get_fd_by_id(&attr);
2215 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002216 default:
2217 err = -EINVAL;
2218 break;
2219 }
2220
2221 return err;
2222}