blob: e254526d6744ed622dcdd066358d5559943a14be [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>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070021#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070022#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070023#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070024#include <linux/license.h>
25#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070026#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010027#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070028#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070029#include <linux/cred.h>
30#include <linux/timekeeping.h>
31#include <linux/ctype.h>
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070032#include <linux/btf.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
82 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
83 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,
105};
106
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700107static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
108{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800109 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100110 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700111 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800112 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700113
Mark Rutland9ef09e32018-05-03 17:04:59 +0100114 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800115 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100116 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
117 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800118 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200119 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700120
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800121 if (ops->map_alloc_check) {
122 err = ops->map_alloc_check(attr);
123 if (err)
124 return ERR_PTR(err);
125 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800126 if (attr->map_ifindex)
127 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800128 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200129 if (IS_ERR(map))
130 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800131 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100132 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200133 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700134}
135
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700136void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100137{
138 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
139 * trigger under memory pressure as we really just want to
140 * fail instead.
141 */
142 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
143 void *area;
144
145 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700146 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100147 if (area != NULL)
148 return area;
149 }
150
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700151 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
152 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100153}
154
155void bpf_map_area_free(void *area)
156{
157 kvfree(area);
158}
159
Jakub Kicinskibd475642018-01-11 20:29:06 -0800160void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
161{
162 map->map_type = attr->map_type;
163 map->key_size = attr->key_size;
164 map->value_size = attr->value_size;
165 map->max_entries = attr->max_entries;
166 map->map_flags = attr->map_flags;
167 map->numa_node = bpf_map_attr_numa_node(attr);
168}
169
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800170int bpf_map_precharge_memlock(u32 pages)
171{
172 struct user_struct *user = get_current_user();
173 unsigned long memlock_limit, cur;
174
175 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
176 cur = atomic_long_read(&user->locked_vm);
177 free_uid(user);
178 if (cur + pages > memlock_limit)
179 return -EPERM;
180 return 0;
181}
182
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700183static int bpf_map_charge_memlock(struct bpf_map *map)
184{
185 struct user_struct *user = get_current_user();
186 unsigned long memlock_limit;
187
188 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
189
190 atomic_long_add(map->pages, &user->locked_vm);
191
192 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
193 atomic_long_sub(map->pages, &user->locked_vm);
194 free_uid(user);
195 return -EPERM;
196 }
197 map->user = user;
198 return 0;
199}
200
201static void bpf_map_uncharge_memlock(struct bpf_map *map)
202{
203 struct user_struct *user = map->user;
204
205 atomic_long_sub(map->pages, &user->locked_vm);
206 free_uid(user);
207}
208
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700209static int bpf_map_alloc_id(struct bpf_map *map)
210{
211 int id;
212
Shaohua Lib76354c2018-03-27 11:53:21 -0700213 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700214 spin_lock_bh(&map_idr_lock);
215 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
216 if (id > 0)
217 map->id = id;
218 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700219 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700220
221 if (WARN_ON_ONCE(!id))
222 return -ENOSPC;
223
224 return id > 0 ? 0 : id;
225}
226
Jakub Kicinskia3884572018-01-11 20:29:09 -0800227void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700228{
Eric Dumazet930651a2017-09-19 09:15:59 -0700229 unsigned long flags;
230
Jakub Kicinskia3884572018-01-11 20:29:09 -0800231 /* Offloaded maps are removed from the IDR store when their device
232 * disappears - even if someone holds an fd to them they are unusable,
233 * the memory is gone, all ops will fail; they are simply waiting for
234 * refcnt to drop to be freed.
235 */
236 if (!map->id)
237 return;
238
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700239 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700240 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700241 else
242 __acquire(&map_idr_lock);
243
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700244 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800245 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700246
247 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700248 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700249 else
250 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700251}
252
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700253/* called from workqueue */
254static void bpf_map_free_deferred(struct work_struct *work)
255{
256 struct bpf_map *map = container_of(work, struct bpf_map, work);
257
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700258 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700259 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700260 /* implementation dependent freeing */
261 map->ops->map_free(map);
262}
263
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100264static void bpf_map_put_uref(struct bpf_map *map)
265{
266 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700267 if (map->ops->map_release_uref)
268 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100269 }
270}
271
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700272/* decrement map refcnt and schedule it for freeing via workqueue
273 * (unrelying map implementation ops->map_free() might sleep)
274 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700275static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700276{
277 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700278 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700279 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700280 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700281 INIT_WORK(&map->work, bpf_map_free_deferred);
282 schedule_work(&map->work);
283 }
284}
285
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700286void bpf_map_put(struct bpf_map *map)
287{
288 __bpf_map_put(map, true);
289}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700290EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700291
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100292void bpf_map_put_with_uref(struct bpf_map *map)
293{
294 bpf_map_put_uref(map);
295 bpf_map_put(map);
296}
297
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700298static int bpf_map_release(struct inode *inode, struct file *filp)
299{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200300 struct bpf_map *map = filp->private_data;
301
302 if (map->ops->map_release)
303 map->ops->map_release(map, filp);
304
305 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700306 return 0;
307}
308
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100309#ifdef CONFIG_PROC_FS
310static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
311{
312 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100313 const struct bpf_array *array;
314 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200315 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100316
317 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
318 array = container_of(map, struct bpf_array, map);
319 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200320 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100321 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100322
323 seq_printf(m,
324 "map_type:\t%u\n"
325 "key_size:\t%u\n"
326 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100327 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100328 "map_flags:\t%#x\n"
329 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100330 map->map_type,
331 map->key_size,
332 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100333 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100334 map->map_flags,
335 map->pages * 1ULL << PAGE_SHIFT);
336
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200337 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100338 seq_printf(m, "owner_prog_type:\t%u\n",
339 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200340 seq_printf(m, "owner_jited:\t%u\n",
341 owner_jited);
342 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100343}
344#endif
345
Chenbo Feng6e71b042017-10-18 13:00:22 -0700346static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
347 loff_t *ppos)
348{
349 /* We need this handler such that alloc_file() enables
350 * f_mode with FMODE_CAN_READ.
351 */
352 return -EINVAL;
353}
354
355static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
356 size_t siz, loff_t *ppos)
357{
358 /* We need this handler such that alloc_file() enables
359 * f_mode with FMODE_CAN_WRITE.
360 */
361 return -EINVAL;
362}
363
Chenbo Fengf66e4482017-10-18 13:00:26 -0700364const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100365#ifdef CONFIG_PROC_FS
366 .show_fdinfo = bpf_map_show_fdinfo,
367#endif
368 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700369 .read = bpf_dummy_read,
370 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700371};
372
Chenbo Feng6e71b042017-10-18 13:00:22 -0700373int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100374{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700375 int ret;
376
377 ret = security_bpf_map(map, OPEN_FMODE(flags));
378 if (ret < 0)
379 return ret;
380
Daniel Borkmannaa797812015-10-29 14:58:06 +0100381 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700382 flags | O_CLOEXEC);
383}
384
385int bpf_get_file_flag(int flags)
386{
387 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
388 return -EINVAL;
389 if (flags & BPF_F_RDONLY)
390 return O_RDONLY;
391 if (flags & BPF_F_WRONLY)
392 return O_WRONLY;
393 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100394}
395
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700396/* helper macro to check that unused fields 'union bpf_attr' are zero */
397#define CHECK_ATTR(CMD) \
398 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
399 sizeof(attr->CMD##_LAST_FIELD), 0, \
400 sizeof(*attr) - \
401 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
402 sizeof(attr->CMD##_LAST_FIELD)) != NULL
403
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700404/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
405 * Return 0 on success and < 0 on error.
406 */
407static int bpf_obj_name_cpy(char *dst, const char *src)
408{
409 const char *end = src + BPF_OBJ_NAME_LEN;
410
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700411 memset(dst, 0, BPF_OBJ_NAME_LEN);
412
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700413 /* Copy all isalnum() and '_' char */
414 while (src < end && *src) {
415 if (!isalnum(*src) && *src != '_')
416 return -EINVAL;
417 *dst++ = *src++;
418 }
419
420 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
421 if (src == end)
422 return -EINVAL;
423
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700424 return 0;
425}
426
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700427#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700428/* called via syscall */
429static int map_create(union bpf_attr *attr)
430{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700431 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700432 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700433 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700434 int err;
435
436 err = CHECK_ATTR(BPF_MAP_CREATE);
437 if (err)
438 return -EINVAL;
439
Chenbo Feng6e71b042017-10-18 13:00:22 -0700440 f_flags = bpf_get_file_flag(attr->map_flags);
441 if (f_flags < 0)
442 return f_flags;
443
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700444 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700445 ((unsigned int)numa_node >= nr_node_ids ||
446 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700447 return -EINVAL;
448
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700449 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
450 map = find_and_alloc_map(attr);
451 if (IS_ERR(map))
452 return PTR_ERR(map);
453
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700454 err = bpf_obj_name_cpy(map->name, attr->map_name);
455 if (err)
456 goto free_map_nouncharge;
457
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700458 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100459 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700460
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700461 if (bpf_map_support_seq_show(map) &&
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700462 (attr->btf_key_type_id || attr->btf_value_type_id)) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700463 struct btf *btf;
464
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700465 if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700466 err = -EINVAL;
467 goto free_map_nouncharge;
468 }
469
470 btf = btf_get_by_fd(attr->btf_fd);
471 if (IS_ERR(btf)) {
472 err = PTR_ERR(btf);
473 goto free_map_nouncharge;
474 }
475
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700476 err = map->ops->map_check_btf(map, btf, attr->btf_key_type_id,
477 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700478 if (err) {
479 btf_put(btf);
480 goto free_map_nouncharge;
481 }
482
483 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700484 map->btf_key_type_id = attr->btf_key_type_id;
485 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700486 }
487
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700488 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700489 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100490 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700491
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700492 err = bpf_map_charge_memlock(map);
493 if (err)
494 goto free_map_sec;
495
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700496 err = bpf_map_alloc_id(map);
497 if (err)
498 goto free_map;
499
Chenbo Feng6e71b042017-10-18 13:00:22 -0700500 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700501 if (err < 0) {
502 /* failed to allocate fd.
503 * bpf_map_put() is needed because the above
504 * bpf_map_alloc_id() has published the map
505 * to the userspace and the userspace may
506 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
507 */
508 bpf_map_put(map);
509 return err;
510 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700511
512 return err;
513
514free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100515 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700516free_map_sec:
517 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100518free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700519 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700520 map->ops->map_free(map);
521 return err;
522}
523
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700524/* if error is returned, fd is released.
525 * On success caller should complete fd access with matching fdput()
526 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100527struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700528{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700529 if (!f.file)
530 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700531 if (f.file->f_op != &bpf_map_fops) {
532 fdput(f);
533 return ERR_PTR(-EINVAL);
534 }
535
Daniel Borkmannc2101292015-10-29 14:58:07 +0100536 return f.file->private_data;
537}
538
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700539/* prog's and map's refcnt limit */
540#define BPF_MAX_REFCNT 32768
541
542struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100543{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700544 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
545 atomic_dec(&map->refcnt);
546 return ERR_PTR(-EBUSY);
547 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100548 if (uref)
549 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700550 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100551}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700552EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100553
554struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100555{
556 struct fd f = fdget(ufd);
557 struct bpf_map *map;
558
559 map = __bpf_map_get(f);
560 if (IS_ERR(map))
561 return map;
562
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700563 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100564 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700565
566 return map;
567}
568
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700569/* map_idr_lock should have been held */
570static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
571 bool uref)
572{
573 int refold;
574
575 refold = __atomic_add_unless(&map->refcnt, 1, 0);
576
577 if (refold >= BPF_MAX_REFCNT) {
578 __bpf_map_put(map, false);
579 return ERR_PTR(-EBUSY);
580 }
581
582 if (!refold)
583 return ERR_PTR(-ENOENT);
584
585 if (uref)
586 atomic_inc(&map->usercnt);
587
588 return map;
589}
590
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800591int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
592{
593 return -ENOTSUPP;
594}
595
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700596/* last field in 'union bpf_attr' used by this command */
597#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
598
599static int map_lookup_elem(union bpf_attr *attr)
600{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100601 void __user *ukey = u64_to_user_ptr(attr->key);
602 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700603 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700604 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800605 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800606 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200607 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700608 int err;
609
610 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
611 return -EINVAL;
612
Daniel Borkmann592867b2015-09-08 18:00:09 +0200613 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100614 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700615 if (IS_ERR(map))
616 return PTR_ERR(map);
617
Chenbo Feng6e71b042017-10-18 13:00:22 -0700618 if (!(f.file->f_mode & FMODE_CAN_READ)) {
619 err = -EPERM;
620 goto err_put;
621 }
622
Al Viroe4448ed2017-05-13 18:43:00 -0400623 key = memdup_user(ukey, map->key_size);
624 if (IS_ERR(key)) {
625 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700626 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400627 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700628
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800629 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800630 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800631 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
632 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700633 else if (IS_FD_MAP(map))
634 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800635 else
636 value_size = map->value_size;
637
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800638 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800639 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700640 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800641 goto free_key;
642
Jakub Kicinskia3884572018-01-11 20:29:09 -0800643 if (bpf_map_is_dev_bound(map)) {
644 err = bpf_map_offload_lookup_elem(map, key, value);
645 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
646 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800647 err = bpf_percpu_hash_copy(map, key, value);
648 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
649 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800650 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
651 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700652 } else if (IS_FD_ARRAY(map)) {
653 err = bpf_fd_array_map_lookup_elem(map, key, value);
654 } else if (IS_FD_HASH(map)) {
655 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800656 } else {
657 rcu_read_lock();
658 ptr = map->ops->map_lookup_elem(map, key);
659 if (ptr)
660 memcpy(value, ptr, value_size);
661 rcu_read_unlock();
662 err = ptr ? 0 : -ENOENT;
663 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800664
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800665 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800666 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700667
668 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800669 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800670 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700671
672 err = 0;
673
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800674free_value:
675 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700676free_key:
677 kfree(key);
678err_put:
679 fdput(f);
680 return err;
681}
682
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800683#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700684
685static int map_update_elem(union bpf_attr *attr)
686{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100687 void __user *ukey = u64_to_user_ptr(attr->key);
688 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700689 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700690 struct bpf_map *map;
691 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800692 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200693 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700694 int err;
695
696 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
697 return -EINVAL;
698
Daniel Borkmann592867b2015-09-08 18:00:09 +0200699 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100700 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700701 if (IS_ERR(map))
702 return PTR_ERR(map);
703
Chenbo Feng6e71b042017-10-18 13:00:22 -0700704 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
705 err = -EPERM;
706 goto err_put;
707 }
708
Al Viroe4448ed2017-05-13 18:43:00 -0400709 key = memdup_user(ukey, map->key_size);
710 if (IS_ERR(key)) {
711 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700712 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400713 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700714
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800715 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800716 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800717 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
718 value_size = round_up(map->value_size, 8) * num_possible_cpus();
719 else
720 value_size = map->value_size;
721
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700722 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800723 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700724 if (!value)
725 goto free_key;
726
727 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800728 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700729 goto free_value;
730
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200731 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800732 if (bpf_map_is_dev_bound(map)) {
733 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
734 goto out;
735 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200736 err = map->ops->map_update_elem(map, key, value, attr->flags);
737 goto out;
738 }
739
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800740 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
741 * inside bpf map update or delete otherwise deadlocks are possible
742 */
743 preempt_disable();
744 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800745 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
746 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800747 err = bpf_percpu_hash_update(map, key, value, attr->flags);
748 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
749 err = bpf_percpu_array_update(map, key, value, attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100750 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200751 rcu_read_lock();
752 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
753 attr->flags);
754 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700755 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
756 rcu_read_lock();
757 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
758 attr->flags);
759 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800760 } else {
761 rcu_read_lock();
762 err = map->ops->map_update_elem(map, key, value, attr->flags);
763 rcu_read_unlock();
764 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800765 __this_cpu_dec(bpf_prog_active);
766 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200767out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700768free_value:
769 kfree(value);
770free_key:
771 kfree(key);
772err_put:
773 fdput(f);
774 return err;
775}
776
777#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
778
779static int map_delete_elem(union bpf_attr *attr)
780{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100781 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700782 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700783 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200784 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700785 void *key;
786 int err;
787
788 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
789 return -EINVAL;
790
Daniel Borkmann592867b2015-09-08 18:00:09 +0200791 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100792 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700793 if (IS_ERR(map))
794 return PTR_ERR(map);
795
Chenbo Feng6e71b042017-10-18 13:00:22 -0700796 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
797 err = -EPERM;
798 goto err_put;
799 }
800
Al Viroe4448ed2017-05-13 18:43:00 -0400801 key = memdup_user(ukey, map->key_size);
802 if (IS_ERR(key)) {
803 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700804 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400805 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700806
Jakub Kicinskia3884572018-01-11 20:29:09 -0800807 if (bpf_map_is_dev_bound(map)) {
808 err = bpf_map_offload_delete_elem(map, key);
809 goto out;
810 }
811
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800812 preempt_disable();
813 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700814 rcu_read_lock();
815 err = map->ops->map_delete_elem(map, key);
816 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800817 __this_cpu_dec(bpf_prog_active);
818 preempt_enable();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800819out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700820 kfree(key);
821err_put:
822 fdput(f);
823 return err;
824}
825
826/* last field in 'union bpf_attr' used by this command */
827#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
828
829static int map_get_next_key(union bpf_attr *attr)
830{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100831 void __user *ukey = u64_to_user_ptr(attr->key);
832 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700833 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700834 struct bpf_map *map;
835 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200836 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700837 int err;
838
839 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
840 return -EINVAL;
841
Daniel Borkmann592867b2015-09-08 18:00:09 +0200842 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100843 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700844 if (IS_ERR(map))
845 return PTR_ERR(map);
846
Chenbo Feng6e71b042017-10-18 13:00:22 -0700847 if (!(f.file->f_mode & FMODE_CAN_READ)) {
848 err = -EPERM;
849 goto err_put;
850 }
851
Teng Qin8fe45922017-04-24 19:00:37 -0700852 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400853 key = memdup_user(ukey, map->key_size);
854 if (IS_ERR(key)) {
855 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700856 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400857 }
Teng Qin8fe45922017-04-24 19:00:37 -0700858 } else {
859 key = NULL;
860 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700861
862 err = -ENOMEM;
863 next_key = kmalloc(map->key_size, GFP_USER);
864 if (!next_key)
865 goto free_key;
866
Jakub Kicinskia3884572018-01-11 20:29:09 -0800867 if (bpf_map_is_dev_bound(map)) {
868 err = bpf_map_offload_get_next_key(map, key, next_key);
869 goto out;
870 }
871
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700872 rcu_read_lock();
873 err = map->ops->map_get_next_key(map, key, next_key);
874 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800875out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700876 if (err)
877 goto free_next_key;
878
879 err = -EFAULT;
880 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
881 goto free_next_key;
882
883 err = 0;
884
885free_next_key:
886 kfree(next_key);
887free_key:
888 kfree(key);
889err_put:
890 fdput(f);
891 return err;
892}
893
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700894static const struct bpf_prog_ops * const bpf_prog_types[] = {
895#define BPF_PROG_TYPE(_id, _name) \
896 [_id] = & _name ## _prog_ops,
897#define BPF_MAP_TYPE(_id, _ops)
898#include <linux/bpf_types.h>
899#undef BPF_PROG_TYPE
900#undef BPF_MAP_TYPE
901};
902
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700903static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
904{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +0200905 const struct bpf_prog_ops *ops;
906
907 if (type >= ARRAY_SIZE(bpf_prog_types))
908 return -EINVAL;
909 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
910 ops = bpf_prog_types[type];
911 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +0200912 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700913
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700914 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +0200915 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700916 else
917 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200918 prog->type = type;
919 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700920}
921
922/* drop refcnt on maps used by eBPF program and free auxilary data */
923static void free_used_maps(struct bpf_prog_aux *aux)
924{
925 int i;
926
927 for (i = 0; i < aux->used_map_cnt; i++)
928 bpf_map_put(aux->used_maps[i]);
929
930 kfree(aux->used_maps);
931}
932
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100933int __bpf_prog_charge(struct user_struct *user, u32 pages)
934{
935 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
936 unsigned long user_bufs;
937
938 if (user) {
939 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
940 if (user_bufs > memlock_limit) {
941 atomic_long_sub(pages, &user->locked_vm);
942 return -EPERM;
943 }
944 }
945
946 return 0;
947}
948
949void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
950{
951 if (user)
952 atomic_long_sub(pages, &user->locked_vm);
953}
954
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700955static int bpf_prog_charge_memlock(struct bpf_prog *prog)
956{
957 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100958 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700959
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100960 ret = __bpf_prog_charge(user, prog->pages);
961 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700962 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100963 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700964 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100965
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700966 prog->aux->user = user;
967 return 0;
968}
969
970static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
971{
972 struct user_struct *user = prog->aux->user;
973
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100974 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700975 free_uid(user);
976}
977
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700978static int bpf_prog_alloc_id(struct bpf_prog *prog)
979{
980 int id;
981
Shaohua Lib76354c2018-03-27 11:53:21 -0700982 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700983 spin_lock_bh(&prog_idr_lock);
984 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
985 if (id > 0)
986 prog->aux->id = id;
987 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700988 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700989
990 /* id is in [1, INT_MAX) */
991 if (WARN_ON_ONCE(!id))
992 return -ENOSPC;
993
994 return id > 0 ? 0 : id;
995}
996
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800997void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700998{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800999 /* cBPF to eBPF migrations are currently not in the idr store.
1000 * Offloaded programs are removed from the store when their device
1001 * disappears - even if someone grabs an fd to them they are unusable,
1002 * simply waiting for refcnt to drop to be freed.
1003 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001004 if (!prog->aux->id)
1005 return;
1006
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001007 if (do_idr_lock)
1008 spin_lock_bh(&prog_idr_lock);
1009 else
1010 __acquire(&prog_idr_lock);
1011
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001012 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001013 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001014
1015 if (do_idr_lock)
1016 spin_unlock_bh(&prog_idr_lock);
1017 else
1018 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001019}
1020
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001021static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001022{
1023 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1024
1025 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001026 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001027 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001028 bpf_prog_free(aux->prog);
1029}
1030
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001031static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001032{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001033 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001034 int i;
1035
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001036 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001037 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001038
1039 for (i = 0; i < prog->aux->func_cnt; i++)
1040 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +01001041 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001042
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001043 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001044 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001045}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001046
1047void bpf_prog_put(struct bpf_prog *prog)
1048{
1049 __bpf_prog_put(prog, true);
1050}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001051EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001052
1053static int bpf_prog_release(struct inode *inode, struct file *filp)
1054{
1055 struct bpf_prog *prog = filp->private_data;
1056
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001057 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001058 return 0;
1059}
1060
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001061#ifdef CONFIG_PROC_FS
1062static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1063{
1064 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001065 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001066
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001067 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001068 seq_printf(m,
1069 "prog_type:\t%u\n"
1070 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001071 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001072 "memlock:\t%llu\n",
1073 prog->type,
1074 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001075 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001076 prog->pages * 1ULL << PAGE_SHIFT);
1077}
1078#endif
1079
Chenbo Fengf66e4482017-10-18 13:00:26 -07001080const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001081#ifdef CONFIG_PROC_FS
1082 .show_fdinfo = bpf_prog_show_fdinfo,
1083#endif
1084 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001085 .read = bpf_dummy_read,
1086 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001087};
1088
Daniel Borkmannb2197752015-10-29 14:58:09 +01001089int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001090{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001091 int ret;
1092
1093 ret = security_bpf_prog(prog);
1094 if (ret < 0)
1095 return ret;
1096
Daniel Borkmannaa797812015-10-29 14:58:06 +01001097 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1098 O_RDWR | O_CLOEXEC);
1099}
1100
Daniel Borkmann113214b2016-06-30 17:24:44 +02001101static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001102{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001103 if (!f.file)
1104 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001105 if (f.file->f_op != &bpf_prog_fops) {
1106 fdput(f);
1107 return ERR_PTR(-EINVAL);
1108 }
1109
Daniel Borkmannc2101292015-10-29 14:58:07 +01001110 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001111}
1112
Brenden Blanco59d36562016-07-19 12:16:46 -07001113struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001114{
Brenden Blanco59d36562016-07-19 12:16:46 -07001115 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1116 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001117 return ERR_PTR(-EBUSY);
1118 }
1119 return prog;
1120}
Brenden Blanco59d36562016-07-19 12:16:46 -07001121EXPORT_SYMBOL_GPL(bpf_prog_add);
1122
Daniel Borkmannc5405942016-11-09 22:02:34 +01001123void bpf_prog_sub(struct bpf_prog *prog, int i)
1124{
1125 /* Only to be used for undoing previous bpf_prog_add() in some
1126 * error path. We still know that another entity in our call
1127 * path holds a reference to the program, thus atomic_sub() can
1128 * be safely used in such cases!
1129 */
1130 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1131}
1132EXPORT_SYMBOL_GPL(bpf_prog_sub);
1133
Brenden Blanco59d36562016-07-19 12:16:46 -07001134struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1135{
1136 return bpf_prog_add(prog, 1);
1137}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001138EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001139
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001140/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001141struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001142{
1143 int refold;
1144
1145 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1146
1147 if (refold >= BPF_MAX_REFCNT) {
1148 __bpf_prog_put(prog, false);
1149 return ERR_PTR(-EBUSY);
1150 }
1151
1152 if (!refold)
1153 return ERR_PTR(-ENOENT);
1154
1155 return prog;
1156}
John Fastabenda6f6df62017-08-15 22:32:22 -07001157EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001158
Al Viro040ee692017-12-02 20:20:38 -05001159bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001160 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001161{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001162 /* not an attachment, just a refcount inc, always allow */
1163 if (!attach_type)
1164 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001165
1166 if (prog->type != *attach_type)
1167 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001168 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001169 return false;
1170
1171 return true;
1172}
1173
1174static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001175 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001176{
1177 struct fd f = fdget(ufd);
1178 struct bpf_prog *prog;
1179
Daniel Borkmann113214b2016-06-30 17:24:44 +02001180 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001181 if (IS_ERR(prog))
1182 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001183 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001184 prog = ERR_PTR(-EINVAL);
1185 goto out;
1186 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001187
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001188 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001189out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001190 fdput(f);
1191 return prog;
1192}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001193
1194struct bpf_prog *bpf_prog_get(u32 ufd)
1195{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001196 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001197}
1198
Jakub Kicinski248f3462017-11-03 13:56:20 -07001199struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001200 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001201{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001202 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001203}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001204EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001205
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001206/* Initially all BPF programs could be loaded w/o specifying
1207 * expected_attach_type. Later for some of them specifying expected_attach_type
1208 * at load time became required so that program could be validated properly.
1209 * Programs of types that are allowed to be loaded both w/ and w/o (for
1210 * backward compatibility) expected_attach_type, should have the default attach
1211 * type assigned to expected_attach_type for the latter case, so that it can be
1212 * validated later at attach time.
1213 *
1214 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1215 * prog type requires it but has some attach types that have to be backward
1216 * compatible.
1217 */
1218static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1219{
1220 switch (attr->prog_type) {
1221 case BPF_PROG_TYPE_CGROUP_SOCK:
1222 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1223 * exist so checking for non-zero is the way to go here.
1224 */
1225 if (!attr->expected_attach_type)
1226 attr->expected_attach_type =
1227 BPF_CGROUP_INET_SOCK_CREATE;
1228 break;
1229 }
1230}
1231
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001232static int
1233bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1234 enum bpf_attach_type expected_attach_type)
1235{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001236 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001237 case BPF_PROG_TYPE_CGROUP_SOCK:
1238 switch (expected_attach_type) {
1239 case BPF_CGROUP_INET_SOCK_CREATE:
1240 case BPF_CGROUP_INET4_POST_BIND:
1241 case BPF_CGROUP_INET6_POST_BIND:
1242 return 0;
1243 default:
1244 return -EINVAL;
1245 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001246 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1247 switch (expected_attach_type) {
1248 case BPF_CGROUP_INET4_BIND:
1249 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001250 case BPF_CGROUP_INET4_CONNECT:
1251 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001252 case BPF_CGROUP_UDP4_SENDMSG:
1253 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001254 return 0;
1255 default:
1256 return -EINVAL;
1257 }
1258 default:
1259 return 0;
1260 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001261}
1262
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001263/* last field in 'union bpf_attr' used by this command */
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001264#define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001265
1266static int bpf_prog_load(union bpf_attr *attr)
1267{
1268 enum bpf_prog_type type = attr->prog_type;
1269 struct bpf_prog *prog;
1270 int err;
1271 char license[128];
1272 bool is_gpl;
1273
1274 if (CHECK_ATTR(BPF_PROG_LOAD))
1275 return -EINVAL;
1276
David S. Millere07b98d2017-05-10 11:38:07 -07001277 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1278 return -EINVAL;
1279
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001280 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001281 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001282 sizeof(license) - 1) < 0)
1283 return -EFAULT;
1284 license[sizeof(license) - 1] = 0;
1285
1286 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1287 is_gpl = license_is_gpl_compatible(license);
1288
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001289 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1290 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001291
Alexei Starovoitov25415172015-03-25 12:49:20 -07001292 if (type == BPF_PROG_TYPE_KPROBE &&
1293 attr->kern_version != LINUX_VERSION_CODE)
1294 return -EINVAL;
1295
Chenbo Feng80b7d812017-05-31 18:16:00 -07001296 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1297 type != BPF_PROG_TYPE_CGROUP_SKB &&
1298 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001299 return -EPERM;
1300
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001301 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001302 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1303 return -EINVAL;
1304
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001305 /* plain bpf_prog allocation */
1306 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1307 if (!prog)
1308 return -ENOMEM;
1309
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001310 prog->expected_attach_type = attr->expected_attach_type;
1311
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001312 prog->aux->offload_requested = !!attr->prog_ifindex;
1313
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001314 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001315 if (err)
1316 goto free_prog_nouncharge;
1317
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001318 err = bpf_prog_charge_memlock(prog);
1319 if (err)
1320 goto free_prog_sec;
1321
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001322 prog->len = attr->insn_cnt;
1323
1324 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001325 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001326 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001327 goto free_prog;
1328
1329 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001330 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001331
1332 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001333 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001334
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001335 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001336 err = bpf_prog_offload_init(prog, attr);
1337 if (err)
1338 goto free_prog;
1339 }
1340
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001341 /* find program type: socket_filter vs tracing_filter */
1342 err = find_prog_type(type, prog);
1343 if (err < 0)
1344 goto free_prog;
1345
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001346 prog->aux->load_time = ktime_get_boot_ns();
1347 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1348 if (err)
1349 goto free_prog;
1350
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001351 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001352 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001353 if (err < 0)
1354 goto free_used_maps;
1355
1356 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001357 if (!prog->bpf_func)
1358 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001359 if (err < 0)
1360 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001361
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001362 err = bpf_prog_alloc_id(prog);
1363 if (err)
1364 goto free_used_maps;
1365
Daniel Borkmannaa797812015-10-29 14:58:06 +01001366 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001367 if (err < 0) {
1368 /* failed to allocate fd.
1369 * bpf_prog_put() is needed because the above
1370 * bpf_prog_alloc_id() has published the prog
1371 * to the userspace and the userspace may
1372 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1373 */
1374 bpf_prog_put(prog);
1375 return err;
1376 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001377
Daniel Borkmann74451e662017-02-16 22:24:50 +01001378 bpf_prog_kallsyms_add(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001379 return err;
1380
1381free_used_maps:
1382 free_used_maps(prog->aux);
1383free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001384 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001385free_prog_sec:
1386 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001387free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001388 bpf_prog_free(prog);
1389 return err;
1390}
1391
Chenbo Feng6e71b042017-10-18 13:00:22 -07001392#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001393
1394static int bpf_obj_pin(const union bpf_attr *attr)
1395{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001396 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001397 return -EINVAL;
1398
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001399 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001400}
1401
1402static int bpf_obj_get(const union bpf_attr *attr)
1403{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001404 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1405 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001406 return -EINVAL;
1407
Chenbo Feng6e71b042017-10-18 13:00:22 -07001408 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1409 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001410}
1411
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001412struct bpf_raw_tracepoint {
1413 struct bpf_raw_event_map *btp;
1414 struct bpf_prog *prog;
1415};
1416
1417static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1418{
1419 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1420
1421 if (raw_tp->prog) {
1422 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1423 bpf_prog_put(raw_tp->prog);
1424 }
1425 kfree(raw_tp);
1426 return 0;
1427}
1428
1429static const struct file_operations bpf_raw_tp_fops = {
1430 .release = bpf_raw_tracepoint_release,
1431 .read = bpf_dummy_read,
1432 .write = bpf_dummy_write,
1433};
1434
1435#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1436
1437static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1438{
1439 struct bpf_raw_tracepoint *raw_tp;
1440 struct bpf_raw_event_map *btp;
1441 struct bpf_prog *prog;
1442 char tp_name[128];
1443 int tp_fd, err;
1444
1445 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1446 sizeof(tp_name) - 1) < 0)
1447 return -EFAULT;
1448 tp_name[sizeof(tp_name) - 1] = 0;
1449
1450 btp = bpf_find_raw_tracepoint(tp_name);
1451 if (!btp)
1452 return -ENOENT;
1453
1454 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1455 if (!raw_tp)
1456 return -ENOMEM;
1457 raw_tp->btp = btp;
1458
1459 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1460 BPF_PROG_TYPE_RAW_TRACEPOINT);
1461 if (IS_ERR(prog)) {
1462 err = PTR_ERR(prog);
1463 goto out_free_tp;
1464 }
1465
1466 err = bpf_probe_register(raw_tp->btp, prog);
1467 if (err)
1468 goto out_put_prog;
1469
1470 raw_tp->prog = prog;
1471 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1472 O_CLOEXEC);
1473 if (tp_fd < 0) {
1474 bpf_probe_unregister(raw_tp->btp, prog);
1475 err = tp_fd;
1476 goto out_put_prog;
1477 }
1478 return tp_fd;
1479
1480out_put_prog:
1481 bpf_prog_put(prog);
1482out_free_tp:
1483 kfree(raw_tp);
1484 return err;
1485}
1486
Daniel Mackf4324552016-11-23 16:52:27 +01001487#ifdef CONFIG_CGROUP_BPF
1488
Anders Roxell33491582018-04-03 14:09:47 +02001489static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1490 enum bpf_attach_type attach_type)
1491{
1492 switch (prog->type) {
1493 case BPF_PROG_TYPE_CGROUP_SOCK:
1494 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1495 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1496 default:
1497 return 0;
1498 }
1499}
1500
John Fastabend464bc0f2017-08-28 07:10:04 -07001501#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001502
John Fastabend4f738ad2018-03-18 12:57:10 -07001503static int sockmap_get_from_fd(const union bpf_attr *attr,
1504 int type, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001505{
John Fastabend5a67da22017-09-08 14:00:49 -07001506 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001507 int ufd = attr->target_fd;
1508 struct bpf_map *map;
1509 struct fd f;
1510 int err;
1511
1512 f = fdget(ufd);
1513 map = __bpf_map_get(f);
1514 if (IS_ERR(map))
1515 return PTR_ERR(map);
1516
John Fastabend5a67da22017-09-08 14:00:49 -07001517 if (attach) {
John Fastabend4f738ad2018-03-18 12:57:10 -07001518 prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
John Fastabend5a67da22017-09-08 14:00:49 -07001519 if (IS_ERR(prog)) {
1520 fdput(f);
1521 return PTR_ERR(prog);
1522 }
John Fastabend174a79f2017-08-15 22:32:47 -07001523 }
1524
John Fastabend5a67da22017-09-08 14:00:49 -07001525 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001526 if (err) {
1527 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001528 if (prog)
1529 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001530 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001531 }
1532
1533 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001534 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001535}
Daniel Mackf4324552016-11-23 16:52:27 +01001536
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001537#define BPF_F_ATTACH_MASK \
1538 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1539
Daniel Mackf4324552016-11-23 16:52:27 +01001540static int bpf_prog_attach(const union bpf_attr *attr)
1541{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001542 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001543 struct bpf_prog *prog;
1544 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001545 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001546
1547 if (!capable(CAP_NET_ADMIN))
1548 return -EPERM;
1549
1550 if (CHECK_ATTR(BPF_PROG_ATTACH))
1551 return -EINVAL;
1552
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001553 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001554 return -EINVAL;
1555
Daniel Mackf4324552016-11-23 16:52:27 +01001556 switch (attr->attach_type) {
1557 case BPF_CGROUP_INET_INGRESS:
1558 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001559 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001560 break;
David Ahern610236582016-12-01 08:48:04 -08001561 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001562 case BPF_CGROUP_INET4_POST_BIND:
1563 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001564 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1565 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001566 case BPF_CGROUP_INET4_BIND:
1567 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001568 case BPF_CGROUP_INET4_CONNECT:
1569 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001570 case BPF_CGROUP_UDP4_SENDMSG:
1571 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001572 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1573 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001574 case BPF_CGROUP_SOCK_OPS:
1575 ptype = BPF_PROG_TYPE_SOCK_OPS;
1576 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001577 case BPF_CGROUP_DEVICE:
1578 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1579 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001580 case BPF_SK_MSG_VERDICT:
1581 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
John Fastabend464bc0f2017-08-28 07:10:04 -07001582 case BPF_SK_SKB_STREAM_PARSER:
1583 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001584 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001585 default:
1586 return -EINVAL;
1587 }
1588
David Ahernb2cd1252016-12-01 08:48:03 -08001589 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1590 if (IS_ERR(prog))
1591 return PTR_ERR(prog);
1592
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001593 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1594 bpf_prog_put(prog);
1595 return -EINVAL;
1596 }
1597
David Ahernb2cd1252016-12-01 08:48:03 -08001598 cgrp = cgroup_get_from_fd(attr->target_fd);
1599 if (IS_ERR(cgrp)) {
1600 bpf_prog_put(prog);
1601 return PTR_ERR(cgrp);
1602 }
1603
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001604 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1605 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001606 if (ret)
1607 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001608 cgroup_put(cgrp);
1609
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001610 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001611}
1612
1613#define BPF_PROG_DETACH_LAST_FIELD attach_type
1614
1615static int bpf_prog_detach(const union bpf_attr *attr)
1616{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001617 enum bpf_prog_type ptype;
1618 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001619 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001620 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001621
1622 if (!capable(CAP_NET_ADMIN))
1623 return -EPERM;
1624
1625 if (CHECK_ATTR(BPF_PROG_DETACH))
1626 return -EINVAL;
1627
1628 switch (attr->attach_type) {
1629 case BPF_CGROUP_INET_INGRESS:
1630 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001631 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1632 break;
David Ahern610236582016-12-01 08:48:04 -08001633 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001634 case BPF_CGROUP_INET4_POST_BIND:
1635 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001636 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1637 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001638 case BPF_CGROUP_INET4_BIND:
1639 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001640 case BPF_CGROUP_INET4_CONNECT:
1641 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001642 case BPF_CGROUP_UDP4_SENDMSG:
1643 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001644 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1645 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001646 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001647 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001648 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001649 case BPF_CGROUP_DEVICE:
1650 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1651 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001652 case BPF_SK_MSG_VERDICT:
1653 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
John Fastabend5a67da22017-09-08 14:00:49 -07001654 case BPF_SK_SKB_STREAM_PARSER:
1655 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001656 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001657 default:
1658 return -EINVAL;
1659 }
1660
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001661 cgrp = cgroup_get_from_fd(attr->target_fd);
1662 if (IS_ERR(cgrp))
1663 return PTR_ERR(cgrp);
1664
1665 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1666 if (IS_ERR(prog))
1667 prog = NULL;
1668
1669 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1670 if (prog)
1671 bpf_prog_put(prog);
1672 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001673 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001674}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001675
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001676#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1677
1678static int bpf_prog_query(const union bpf_attr *attr,
1679 union bpf_attr __user *uattr)
1680{
1681 struct cgroup *cgrp;
1682 int ret;
1683
1684 if (!capable(CAP_NET_ADMIN))
1685 return -EPERM;
1686 if (CHECK_ATTR(BPF_PROG_QUERY))
1687 return -EINVAL;
1688 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1689 return -EINVAL;
1690
1691 switch (attr->query.attach_type) {
1692 case BPF_CGROUP_INET_INGRESS:
1693 case BPF_CGROUP_INET_EGRESS:
1694 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001695 case BPF_CGROUP_INET4_BIND:
1696 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001697 case BPF_CGROUP_INET4_POST_BIND:
1698 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001699 case BPF_CGROUP_INET4_CONNECT:
1700 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001701 case BPF_CGROUP_UDP4_SENDMSG:
1702 case BPF_CGROUP_UDP6_SENDMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001703 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001704 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001705 break;
1706 default:
1707 return -EINVAL;
1708 }
1709 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1710 if (IS_ERR(cgrp))
1711 return PTR_ERR(cgrp);
1712 ret = cgroup_bpf_query(cgrp, attr, uattr);
1713 cgroup_put(cgrp);
1714 return ret;
1715}
Daniel Mackf4324552016-11-23 16:52:27 +01001716#endif /* CONFIG_CGROUP_BPF */
1717
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001718#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1719
1720static int bpf_prog_test_run(const union bpf_attr *attr,
1721 union bpf_attr __user *uattr)
1722{
1723 struct bpf_prog *prog;
1724 int ret = -ENOTSUPP;
1725
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001726 if (!capable(CAP_SYS_ADMIN))
1727 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001728 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1729 return -EINVAL;
1730
1731 prog = bpf_prog_get(attr->test.prog_fd);
1732 if (IS_ERR(prog))
1733 return PTR_ERR(prog);
1734
1735 if (prog->aux->ops->test_run)
1736 ret = prog->aux->ops->test_run(prog, attr, uattr);
1737
1738 bpf_prog_put(prog);
1739 return ret;
1740}
1741
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001742#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1743
1744static int bpf_obj_get_next_id(const union bpf_attr *attr,
1745 union bpf_attr __user *uattr,
1746 struct idr *idr,
1747 spinlock_t *lock)
1748{
1749 u32 next_id = attr->start_id;
1750 int err = 0;
1751
1752 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1753 return -EINVAL;
1754
1755 if (!capable(CAP_SYS_ADMIN))
1756 return -EPERM;
1757
1758 next_id++;
1759 spin_lock_bh(lock);
1760 if (!idr_get_next(idr, &next_id))
1761 err = -ENOENT;
1762 spin_unlock_bh(lock);
1763
1764 if (!err)
1765 err = put_user(next_id, &uattr->next_id);
1766
1767 return err;
1768}
1769
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001770#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1771
1772static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1773{
1774 struct bpf_prog *prog;
1775 u32 id = attr->prog_id;
1776 int fd;
1777
1778 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1779 return -EINVAL;
1780
1781 if (!capable(CAP_SYS_ADMIN))
1782 return -EPERM;
1783
1784 spin_lock_bh(&prog_idr_lock);
1785 prog = idr_find(&prog_idr, id);
1786 if (prog)
1787 prog = bpf_prog_inc_not_zero(prog);
1788 else
1789 prog = ERR_PTR(-ENOENT);
1790 spin_unlock_bh(&prog_idr_lock);
1791
1792 if (IS_ERR(prog))
1793 return PTR_ERR(prog);
1794
1795 fd = bpf_prog_new_fd(prog);
1796 if (fd < 0)
1797 bpf_prog_put(prog);
1798
1799 return fd;
1800}
1801
Chenbo Feng6e71b042017-10-18 13:00:22 -07001802#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001803
1804static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1805{
1806 struct bpf_map *map;
1807 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001808 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001809 int fd;
1810
Chenbo Feng6e71b042017-10-18 13:00:22 -07001811 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1812 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001813 return -EINVAL;
1814
1815 if (!capable(CAP_SYS_ADMIN))
1816 return -EPERM;
1817
Chenbo Feng6e71b042017-10-18 13:00:22 -07001818 f_flags = bpf_get_file_flag(attr->open_flags);
1819 if (f_flags < 0)
1820 return f_flags;
1821
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001822 spin_lock_bh(&map_idr_lock);
1823 map = idr_find(&map_idr, id);
1824 if (map)
1825 map = bpf_map_inc_not_zero(map, true);
1826 else
1827 map = ERR_PTR(-ENOENT);
1828 spin_unlock_bh(&map_idr_lock);
1829
1830 if (IS_ERR(map))
1831 return PTR_ERR(map);
1832
Chenbo Feng6e71b042017-10-18 13:00:22 -07001833 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001834 if (fd < 0)
1835 bpf_map_put(map);
1836
1837 return fd;
1838}
1839
Daniel Borkmann7105e822017-12-20 13:42:57 +01001840static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1841 unsigned long addr)
1842{
1843 int i;
1844
1845 for (i = 0; i < prog->aux->used_map_cnt; i++)
1846 if (prog->aux->used_maps[i] == (void *)addr)
1847 return prog->aux->used_maps[i];
1848 return NULL;
1849}
1850
1851static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1852{
1853 const struct bpf_map *map;
1854 struct bpf_insn *insns;
1855 u64 imm;
1856 int i;
1857
1858 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1859 GFP_USER);
1860 if (!insns)
1861 return insns;
1862
1863 for (i = 0; i < prog->len; i++) {
1864 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1865 insns[i].code = BPF_JMP | BPF_CALL;
1866 insns[i].imm = BPF_FUNC_tail_call;
1867 /* fall-through */
1868 }
1869 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1870 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1871 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1872 insns[i].code = BPF_JMP | BPF_CALL;
1873 if (!bpf_dump_raw_ok())
1874 insns[i].imm = 0;
1875 continue;
1876 }
1877
1878 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1879 continue;
1880
1881 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1882 map = bpf_map_from_imm(prog, imm);
1883 if (map) {
1884 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1885 insns[i].imm = map->id;
1886 insns[i + 1].imm = 0;
1887 continue;
1888 }
1889
1890 if (!bpf_dump_raw_ok() &&
1891 imm == (unsigned long)prog->aux) {
1892 insns[i].imm = 0;
1893 insns[i + 1].imm = 0;
1894 continue;
1895 }
1896 }
1897
1898 return insns;
1899}
1900
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001901static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1902 const union bpf_attr *attr,
1903 union bpf_attr __user *uattr)
1904{
1905 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1906 struct bpf_prog_info info = {};
1907 u32 info_len = attr->info.info_len;
1908 char __user *uinsns;
1909 u32 ulen;
1910 int err;
1911
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07001912 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001913 if (err)
1914 return err;
1915 info_len = min_t(u32, sizeof(info), info_len);
1916
1917 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001918 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001919
1920 info.type = prog->type;
1921 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001922 info.load_time = prog->aux->load_time;
1923 info.created_by_uid = from_kuid_munged(current_user_ns(),
1924 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02001925 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001926
1927 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001928 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1929
1930 ulen = info.nr_map_ids;
1931 info.nr_map_ids = prog->aux->used_map_cnt;
1932 ulen = min_t(u32, info.nr_map_ids, ulen);
1933 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001934 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001935 u32 i;
1936
1937 for (i = 0; i < ulen; i++)
1938 if (put_user(prog->aux->used_maps[i]->id,
1939 &user_map_ids[i]))
1940 return -EFAULT;
1941 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001942
1943 if (!capable(CAP_SYS_ADMIN)) {
1944 info.jited_prog_len = 0;
1945 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05301946 info.nr_jited_ksyms = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001947 goto done;
1948 }
1949
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001950 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001951 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001952 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001953 struct bpf_insn *insns_sanitized;
1954 bool fault;
1955
1956 if (prog->blinded && !bpf_dump_raw_ok()) {
1957 info.xlated_prog_insns = 0;
1958 goto done;
1959 }
1960 insns_sanitized = bpf_insn_prepare_dump(prog);
1961 if (!insns_sanitized)
1962 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001963 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1964 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001965 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1966 kfree(insns_sanitized);
1967 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001968 return -EFAULT;
1969 }
1970
Jakub Kicinski675fc272017-12-27 18:39:09 -08001971 if (bpf_prog_is_dev_bound(prog->aux)) {
1972 err = bpf_prog_offload_info_fill(&info, prog);
1973 if (err)
1974 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001975 goto done;
1976 }
1977
1978 /* NOTE: the following code is supposed to be skipped for offload.
1979 * bpf_prog_offload_info_fill() is the place to fill similar fields
1980 * for offload.
1981 */
1982 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05301983 if (prog->aux->func_cnt) {
1984 u32 i;
1985
1986 info.jited_prog_len = 0;
1987 for (i = 0; i < prog->aux->func_cnt; i++)
1988 info.jited_prog_len += prog->aux->func[i]->jited_len;
1989 } else {
1990 info.jited_prog_len = prog->jited_len;
1991 }
1992
Jiong Wangfcfb1262018-01-16 16:05:19 -08001993 if (info.jited_prog_len && ulen) {
1994 if (bpf_dump_raw_ok()) {
1995 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1996 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05301997
1998 /* for multi-function programs, copy the JITed
1999 * instructions for all the functions
2000 */
2001 if (prog->aux->func_cnt) {
2002 u32 len, free, i;
2003 u8 *img;
2004
2005 free = ulen;
2006 for (i = 0; i < prog->aux->func_cnt; i++) {
2007 len = prog->aux->func[i]->jited_len;
2008 len = min_t(u32, len, free);
2009 img = (u8 *) prog->aux->func[i]->bpf_func;
2010 if (copy_to_user(uinsns, img, len))
2011 return -EFAULT;
2012 uinsns += len;
2013 free -= len;
2014 if (!free)
2015 break;
2016 }
2017 } else {
2018 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2019 return -EFAULT;
2020 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002021 } else {
2022 info.jited_prog_insns = 0;
2023 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002024 }
2025
Sandipan Dasdbecd732018-05-24 12:26:48 +05302026 ulen = info.nr_jited_ksyms;
2027 info.nr_jited_ksyms = prog->aux->func_cnt;
2028 if (info.nr_jited_ksyms && ulen) {
2029 if (bpf_dump_raw_ok()) {
2030 u64 __user *user_ksyms;
2031 ulong ksym_addr;
2032 u32 i;
2033
2034 /* copy the address of the kernel symbol
2035 * corresponding to each function
2036 */
2037 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2038 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2039 for (i = 0; i < ulen; i++) {
2040 ksym_addr = (ulong) prog->aux->func[i]->bpf_func;
2041 ksym_addr &= PAGE_MASK;
2042 if (put_user((u64) ksym_addr, &user_ksyms[i]))
2043 return -EFAULT;
2044 }
2045 } else {
2046 info.jited_ksyms = 0;
2047 }
2048 }
2049
Sandipan Das815581c2018-05-24 12:26:52 +05302050 ulen = info.nr_jited_func_lens;
2051 info.nr_jited_func_lens = prog->aux->func_cnt;
2052 if (info.nr_jited_func_lens && ulen) {
2053 if (bpf_dump_raw_ok()) {
2054 u32 __user *user_lens;
2055 u32 func_len, i;
2056
2057 /* copy the JITed image lengths for each function */
2058 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2059 user_lens = u64_to_user_ptr(info.jited_func_lens);
2060 for (i = 0; i < ulen; i++) {
2061 func_len = prog->aux->func[i]->jited_len;
2062 if (put_user(func_len, &user_lens[i]))
2063 return -EFAULT;
2064 }
2065 } else {
2066 info.jited_func_lens = 0;
2067 }
2068 }
2069
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002070done:
2071 if (copy_to_user(uinfo, &info, info_len) ||
2072 put_user(info_len, &uattr->info.info_len))
2073 return -EFAULT;
2074
2075 return 0;
2076}
2077
2078static int bpf_map_get_info_by_fd(struct bpf_map *map,
2079 const union bpf_attr *attr,
2080 union bpf_attr __user *uattr)
2081{
2082 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2083 struct bpf_map_info info = {};
2084 u32 info_len = attr->info.info_len;
2085 int err;
2086
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002087 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002088 if (err)
2089 return err;
2090 info_len = min_t(u32, sizeof(info), info_len);
2091
2092 info.type = map->map_type;
2093 info.id = map->id;
2094 info.key_size = map->key_size;
2095 info.value_size = map->value_size;
2096 info.max_entries = map->max_entries;
2097 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002098 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002099
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002100 if (map->btf) {
2101 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002102 info.btf_key_type_id = map->btf_key_type_id;
2103 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002104 }
2105
Jakub Kicinski52775b32018-01-17 19:13:28 -08002106 if (bpf_map_is_dev_bound(map)) {
2107 err = bpf_map_offload_info_fill(&info, map);
2108 if (err)
2109 return err;
2110 }
2111
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002112 if (copy_to_user(uinfo, &info, info_len) ||
2113 put_user(info_len, &uattr->info.info_len))
2114 return -EFAULT;
2115
2116 return 0;
2117}
2118
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002119static int bpf_btf_get_info_by_fd(struct btf *btf,
2120 const union bpf_attr *attr,
2121 union bpf_attr __user *uattr)
2122{
2123 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2124 u32 info_len = attr->info.info_len;
2125 int err;
2126
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002127 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002128 if (err)
2129 return err;
2130
2131 return btf_get_info_by_fd(btf, attr, uattr);
2132}
2133
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002134#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2135
2136static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2137 union bpf_attr __user *uattr)
2138{
2139 int ufd = attr->info.bpf_fd;
2140 struct fd f;
2141 int err;
2142
2143 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2144 return -EINVAL;
2145
2146 f = fdget(ufd);
2147 if (!f.file)
2148 return -EBADFD;
2149
2150 if (f.file->f_op == &bpf_prog_fops)
2151 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2152 uattr);
2153 else if (f.file->f_op == &bpf_map_fops)
2154 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2155 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002156 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002157 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002158 else
2159 err = -EINVAL;
2160
2161 fdput(f);
2162 return err;
2163}
2164
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002165#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2166
2167static int bpf_btf_load(const union bpf_attr *attr)
2168{
2169 if (CHECK_ATTR(BPF_BTF_LOAD))
2170 return -EINVAL;
2171
2172 if (!capable(CAP_SYS_ADMIN))
2173 return -EPERM;
2174
2175 return btf_new_fd(attr);
2176}
2177
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002178#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2179
2180static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2181{
2182 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2183 return -EINVAL;
2184
2185 if (!capable(CAP_SYS_ADMIN))
2186 return -EPERM;
2187
2188 return btf_get_fd_by_id(attr->btf_id);
2189}
2190
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002191static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2192 union bpf_attr __user *uattr,
2193 u32 prog_id, u32 fd_type,
2194 const char *buf, u64 probe_offset,
2195 u64 probe_addr)
2196{
2197 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2198 u32 len = buf ? strlen(buf) : 0, input_len;
2199 int err = 0;
2200
2201 if (put_user(len, &uattr->task_fd_query.buf_len))
2202 return -EFAULT;
2203 input_len = attr->task_fd_query.buf_len;
2204 if (input_len && ubuf) {
2205 if (!len) {
2206 /* nothing to copy, just make ubuf NULL terminated */
2207 char zero = '\0';
2208
2209 if (put_user(zero, ubuf))
2210 return -EFAULT;
2211 } else if (input_len >= len + 1) {
2212 /* ubuf can hold the string with NULL terminator */
2213 if (copy_to_user(ubuf, buf, len + 1))
2214 return -EFAULT;
2215 } else {
2216 /* ubuf cannot hold the string with NULL terminator,
2217 * do a partial copy with NULL terminator.
2218 */
2219 char zero = '\0';
2220
2221 err = -ENOSPC;
2222 if (copy_to_user(ubuf, buf, input_len - 1))
2223 return -EFAULT;
2224 if (put_user(zero, ubuf + input_len - 1))
2225 return -EFAULT;
2226 }
2227 }
2228
2229 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2230 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2231 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2232 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2233 return -EFAULT;
2234
2235 return err;
2236}
2237
2238#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2239
2240static int bpf_task_fd_query(const union bpf_attr *attr,
2241 union bpf_attr __user *uattr)
2242{
2243 pid_t pid = attr->task_fd_query.pid;
2244 u32 fd = attr->task_fd_query.fd;
2245 const struct perf_event *event;
2246 struct files_struct *files;
2247 struct task_struct *task;
2248 struct file *file;
2249 int err;
2250
2251 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2252 return -EINVAL;
2253
2254 if (!capable(CAP_SYS_ADMIN))
2255 return -EPERM;
2256
2257 if (attr->task_fd_query.flags != 0)
2258 return -EINVAL;
2259
2260 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2261 if (!task)
2262 return -ENOENT;
2263
2264 files = get_files_struct(task);
2265 put_task_struct(task);
2266 if (!files)
2267 return -ENOENT;
2268
2269 err = 0;
2270 spin_lock(&files->file_lock);
2271 file = fcheck_files(files, fd);
2272 if (!file)
2273 err = -EBADF;
2274 else
2275 get_file(file);
2276 spin_unlock(&files->file_lock);
2277 put_files_struct(files);
2278
2279 if (err)
2280 goto out;
2281
2282 if (file->f_op == &bpf_raw_tp_fops) {
2283 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2284 struct bpf_raw_event_map *btp = raw_tp->btp;
2285
2286 err = bpf_task_fd_query_copy(attr, uattr,
2287 raw_tp->prog->aux->id,
2288 BPF_FD_TYPE_RAW_TRACEPOINT,
2289 btp->tp->name, 0, 0);
2290 goto put_file;
2291 }
2292
2293 event = perf_get_event(file);
2294 if (!IS_ERR(event)) {
2295 u64 probe_offset, probe_addr;
2296 u32 prog_id, fd_type;
2297 const char *buf;
2298
2299 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2300 &buf, &probe_offset,
2301 &probe_addr);
2302 if (!err)
2303 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2304 fd_type, buf,
2305 probe_offset,
2306 probe_addr);
2307 goto put_file;
2308 }
2309
2310 err = -ENOTSUPP;
2311put_file:
2312 fput(file);
2313out:
2314 return err;
2315}
2316
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002317SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2318{
2319 union bpf_attr attr = {};
2320 int err;
2321
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002322 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002323 return -EPERM;
2324
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002325 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002326 if (err)
2327 return err;
2328 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002329
2330 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2331 if (copy_from_user(&attr, uattr, size) != 0)
2332 return -EFAULT;
2333
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002334 err = security_bpf(cmd, &attr, size);
2335 if (err < 0)
2336 return err;
2337
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002338 switch (cmd) {
2339 case BPF_MAP_CREATE:
2340 err = map_create(&attr);
2341 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002342 case BPF_MAP_LOOKUP_ELEM:
2343 err = map_lookup_elem(&attr);
2344 break;
2345 case BPF_MAP_UPDATE_ELEM:
2346 err = map_update_elem(&attr);
2347 break;
2348 case BPF_MAP_DELETE_ELEM:
2349 err = map_delete_elem(&attr);
2350 break;
2351 case BPF_MAP_GET_NEXT_KEY:
2352 err = map_get_next_key(&attr);
2353 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002354 case BPF_PROG_LOAD:
2355 err = bpf_prog_load(&attr);
2356 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002357 case BPF_OBJ_PIN:
2358 err = bpf_obj_pin(&attr);
2359 break;
2360 case BPF_OBJ_GET:
2361 err = bpf_obj_get(&attr);
2362 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002363#ifdef CONFIG_CGROUP_BPF
2364 case BPF_PROG_ATTACH:
2365 err = bpf_prog_attach(&attr);
2366 break;
2367 case BPF_PROG_DETACH:
2368 err = bpf_prog_detach(&attr);
2369 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002370 case BPF_PROG_QUERY:
2371 err = bpf_prog_query(&attr, uattr);
2372 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002373#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002374 case BPF_PROG_TEST_RUN:
2375 err = bpf_prog_test_run(&attr, uattr);
2376 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002377 case BPF_PROG_GET_NEXT_ID:
2378 err = bpf_obj_get_next_id(&attr, uattr,
2379 &prog_idr, &prog_idr_lock);
2380 break;
2381 case BPF_MAP_GET_NEXT_ID:
2382 err = bpf_obj_get_next_id(&attr, uattr,
2383 &map_idr, &map_idr_lock);
2384 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002385 case BPF_PROG_GET_FD_BY_ID:
2386 err = bpf_prog_get_fd_by_id(&attr);
2387 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002388 case BPF_MAP_GET_FD_BY_ID:
2389 err = bpf_map_get_fd_by_id(&attr);
2390 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002391 case BPF_OBJ_GET_INFO_BY_FD:
2392 err = bpf_obj_get_info_by_fd(&attr, uattr);
2393 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002394 case BPF_RAW_TRACEPOINT_OPEN:
2395 err = bpf_raw_tracepoint_open(&attr);
2396 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002397 case BPF_BTF_LOAD:
2398 err = bpf_btf_load(&attr);
2399 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002400 case BPF_BTF_GET_FD_BY_ID:
2401 err = bpf_btf_get_fd_by_id(&attr);
2402 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002403 case BPF_TASK_FD_QUERY:
2404 err = bpf_task_fd_query(&attr, uattr);
2405 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002406 default:
2407 err = -EINVAL;
2408 break;
2409 }
2410
2411 return err;
2412}