blob: 9d3b572d4dec9f483bb2613f0434e6a4b03a17b2 [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>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070014#include <linux/syscalls.h>
15#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010017#include <linux/vmalloc.h>
18#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070019#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070020#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070021#include <linux/license.h>
22#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070023#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010024#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070025#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070026#include <linux/cred.h>
27#include <linux/timekeeping.h>
28#include <linux/ctype.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070029
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070030#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
34#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
35#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
36
Chenbo Feng6e71b042017-10-18 13:00:22 -070037#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
38
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080039DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070040static DEFINE_IDR(prog_idr);
41static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070042static DEFINE_IDR(map_idr);
43static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080044
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070045int sysctl_unprivileged_bpf_disabled __read_mostly;
46
Johannes Berg40077e02017-04-11 15:34:58 +020047static const struct bpf_map_ops * const bpf_map_types[] = {
48#define BPF_PROG_TYPE(_id, _ops)
49#define BPF_MAP_TYPE(_id, _ops) \
50 [_id] = &_ops,
51#include <linux/bpf_types.h>
52#undef BPF_PROG_TYPE
53#undef BPF_MAP_TYPE
54};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070055
Mickaël Salaün752ba562017-08-07 20:45:20 +020056/*
57 * If we're handed a bigger struct than we know of, ensure all the unknown bits
58 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
59 * we don't know about yet.
60 *
61 * There is a ToCToU between this function call and the following
62 * copy_from_user() call. However, this is not a concern since this function is
63 * meant to be a future-proofing of bits.
64 */
Mickaël Salaün58291a72017-08-07 20:45:19 +020065static int check_uarg_tail_zero(void __user *uaddr,
66 size_t expected_size,
67 size_t actual_size)
68{
69 unsigned char __user *addr;
70 unsigned char __user *end;
71 unsigned char val;
72 int err;
73
Mickaël Salaün752ba562017-08-07 20:45:20 +020074 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
75 return -E2BIG;
76
77 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
78 return -EFAULT;
79
Mickaël Salaün58291a72017-08-07 20:45:19 +020080 if (actual_size <= expected_size)
81 return 0;
82
83 addr = uaddr + expected_size;
84 end = uaddr + actual_size;
85
86 for (; addr < end; addr++) {
87 err = get_user(val, addr);
88 if (err)
89 return err;
90 if (val)
91 return -E2BIG;
92 }
93
94 return 0;
95}
96
Jakub Kicinskia3884572018-01-11 20:29:09 -080097const struct bpf_map_ops bpf_map_offload_ops = {
98 .map_alloc = bpf_map_offload_map_alloc,
99 .map_free = bpf_map_offload_map_free,
100};
101
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700102static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
103{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800104 const struct bpf_map_ops *ops;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700105 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800106 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700107
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800108 if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
109 return ERR_PTR(-EINVAL);
110 ops = bpf_map_types[attr->map_type];
111 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200112 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700113
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800114 if (ops->map_alloc_check) {
115 err = ops->map_alloc_check(attr);
116 if (err)
117 return ERR_PTR(err);
118 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800119 if (attr->map_ifindex)
120 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800121 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200122 if (IS_ERR(map))
123 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800124 map->ops = ops;
Johannes Berg40077e02017-04-11 15:34:58 +0200125 map->map_type = attr->map_type;
126 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700127}
128
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700129void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100130{
131 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
132 * trigger under memory pressure as we really just want to
133 * fail instead.
134 */
135 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
136 void *area;
137
138 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700139 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100140 if (area != NULL)
141 return area;
142 }
143
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700144 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
145 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100146}
147
148void bpf_map_area_free(void *area)
149{
150 kvfree(area);
151}
152
Jakub Kicinskibd475642018-01-11 20:29:06 -0800153void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
154{
155 map->map_type = attr->map_type;
156 map->key_size = attr->key_size;
157 map->value_size = attr->value_size;
158 map->max_entries = attr->max_entries;
159 map->map_flags = attr->map_flags;
160 map->numa_node = bpf_map_attr_numa_node(attr);
161}
162
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800163int bpf_map_precharge_memlock(u32 pages)
164{
165 struct user_struct *user = get_current_user();
166 unsigned long memlock_limit, cur;
167
168 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
169 cur = atomic_long_read(&user->locked_vm);
170 free_uid(user);
171 if (cur + pages > memlock_limit)
172 return -EPERM;
173 return 0;
174}
175
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700176static int bpf_map_charge_memlock(struct bpf_map *map)
177{
178 struct user_struct *user = get_current_user();
179 unsigned long memlock_limit;
180
181 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
182
183 atomic_long_add(map->pages, &user->locked_vm);
184
185 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
186 atomic_long_sub(map->pages, &user->locked_vm);
187 free_uid(user);
188 return -EPERM;
189 }
190 map->user = user;
191 return 0;
192}
193
194static void bpf_map_uncharge_memlock(struct bpf_map *map)
195{
196 struct user_struct *user = map->user;
197
198 atomic_long_sub(map->pages, &user->locked_vm);
199 free_uid(user);
200}
201
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700202static int bpf_map_alloc_id(struct bpf_map *map)
203{
204 int id;
205
Shaohua Lib76354c2018-03-27 11:53:21 -0700206 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700207 spin_lock_bh(&map_idr_lock);
208 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
209 if (id > 0)
210 map->id = id;
211 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700212 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700213
214 if (WARN_ON_ONCE(!id))
215 return -ENOSPC;
216
217 return id > 0 ? 0 : id;
218}
219
Jakub Kicinskia3884572018-01-11 20:29:09 -0800220void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700221{
Eric Dumazet930651a2017-09-19 09:15:59 -0700222 unsigned long flags;
223
Jakub Kicinskia3884572018-01-11 20:29:09 -0800224 /* Offloaded maps are removed from the IDR store when their device
225 * disappears - even if someone holds an fd to them they are unusable,
226 * the memory is gone, all ops will fail; they are simply waiting for
227 * refcnt to drop to be freed.
228 */
229 if (!map->id)
230 return;
231
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700232 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700233 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700234 else
235 __acquire(&map_idr_lock);
236
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700237 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800238 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700239
240 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700241 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700242 else
243 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700244}
245
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700246/* called from workqueue */
247static void bpf_map_free_deferred(struct work_struct *work)
248{
249 struct bpf_map *map = container_of(work, struct bpf_map, work);
250
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700251 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700252 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700253 /* implementation dependent freeing */
254 map->ops->map_free(map);
255}
256
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100257static void bpf_map_put_uref(struct bpf_map *map)
258{
259 if (atomic_dec_and_test(&map->usercnt)) {
260 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
261 bpf_fd_array_map_clear(map);
262 }
263}
264
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700265/* decrement map refcnt and schedule it for freeing via workqueue
266 * (unrelying map implementation ops->map_free() might sleep)
267 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700268static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700269{
270 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700271 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700272 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700273 INIT_WORK(&map->work, bpf_map_free_deferred);
274 schedule_work(&map->work);
275 }
276}
277
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700278void bpf_map_put(struct bpf_map *map)
279{
280 __bpf_map_put(map, true);
281}
282
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100283void bpf_map_put_with_uref(struct bpf_map *map)
284{
285 bpf_map_put_uref(map);
286 bpf_map_put(map);
287}
288
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700289static int bpf_map_release(struct inode *inode, struct file *filp)
290{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200291 struct bpf_map *map = filp->private_data;
292
293 if (map->ops->map_release)
294 map->ops->map_release(map, filp);
295
296 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700297 return 0;
298}
299
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100300#ifdef CONFIG_PROC_FS
301static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
302{
303 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100304 const struct bpf_array *array;
305 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200306 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100307
308 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
309 array = container_of(map, struct bpf_array, map);
310 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200311 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100312 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100313
314 seq_printf(m,
315 "map_type:\t%u\n"
316 "key_size:\t%u\n"
317 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100318 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100319 "map_flags:\t%#x\n"
320 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100321 map->map_type,
322 map->key_size,
323 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100324 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100325 map->map_flags,
326 map->pages * 1ULL << PAGE_SHIFT);
327
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200328 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100329 seq_printf(m, "owner_prog_type:\t%u\n",
330 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200331 seq_printf(m, "owner_jited:\t%u\n",
332 owner_jited);
333 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100334}
335#endif
336
Chenbo Feng6e71b042017-10-18 13:00:22 -0700337static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
338 loff_t *ppos)
339{
340 /* We need this handler such that alloc_file() enables
341 * f_mode with FMODE_CAN_READ.
342 */
343 return -EINVAL;
344}
345
346static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
347 size_t siz, loff_t *ppos)
348{
349 /* We need this handler such that alloc_file() enables
350 * f_mode with FMODE_CAN_WRITE.
351 */
352 return -EINVAL;
353}
354
Chenbo Fengf66e4482017-10-18 13:00:26 -0700355const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100356#ifdef CONFIG_PROC_FS
357 .show_fdinfo = bpf_map_show_fdinfo,
358#endif
359 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700360 .read = bpf_dummy_read,
361 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700362};
363
Chenbo Feng6e71b042017-10-18 13:00:22 -0700364int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100365{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700366 int ret;
367
368 ret = security_bpf_map(map, OPEN_FMODE(flags));
369 if (ret < 0)
370 return ret;
371
Daniel Borkmannaa797812015-10-29 14:58:06 +0100372 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700373 flags | O_CLOEXEC);
374}
375
376int bpf_get_file_flag(int flags)
377{
378 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
379 return -EINVAL;
380 if (flags & BPF_F_RDONLY)
381 return O_RDONLY;
382 if (flags & BPF_F_WRONLY)
383 return O_WRONLY;
384 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100385}
386
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700387/* helper macro to check that unused fields 'union bpf_attr' are zero */
388#define CHECK_ATTR(CMD) \
389 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
390 sizeof(attr->CMD##_LAST_FIELD), 0, \
391 sizeof(*attr) - \
392 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
393 sizeof(attr->CMD##_LAST_FIELD)) != NULL
394
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700395/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
396 * Return 0 on success and < 0 on error.
397 */
398static int bpf_obj_name_cpy(char *dst, const char *src)
399{
400 const char *end = src + BPF_OBJ_NAME_LEN;
401
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700402 memset(dst, 0, BPF_OBJ_NAME_LEN);
403
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700404 /* Copy all isalnum() and '_' char */
405 while (src < end && *src) {
406 if (!isalnum(*src) && *src != '_')
407 return -EINVAL;
408 *dst++ = *src++;
409 }
410
411 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
412 if (src == end)
413 return -EINVAL;
414
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700415 return 0;
416}
417
Jakub Kicinskia3884572018-01-11 20:29:09 -0800418#define BPF_MAP_CREATE_LAST_FIELD map_ifindex
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700419/* called via syscall */
420static int map_create(union bpf_attr *attr)
421{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700422 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700423 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700424 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700425 int err;
426
427 err = CHECK_ATTR(BPF_MAP_CREATE);
428 if (err)
429 return -EINVAL;
430
Chenbo Feng6e71b042017-10-18 13:00:22 -0700431 f_flags = bpf_get_file_flag(attr->map_flags);
432 if (f_flags < 0)
433 return f_flags;
434
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700435 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700436 ((unsigned int)numa_node >= nr_node_ids ||
437 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700438 return -EINVAL;
439
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700440 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
441 map = find_and_alloc_map(attr);
442 if (IS_ERR(map))
443 return PTR_ERR(map);
444
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700445 err = bpf_obj_name_cpy(map->name, attr->map_name);
446 if (err)
447 goto free_map_nouncharge;
448
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700449 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100450 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700451
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700452 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700453 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100454 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700455
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700456 err = bpf_map_charge_memlock(map);
457 if (err)
458 goto free_map_sec;
459
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700460 err = bpf_map_alloc_id(map);
461 if (err)
462 goto free_map;
463
Chenbo Feng6e71b042017-10-18 13:00:22 -0700464 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700465 if (err < 0) {
466 /* failed to allocate fd.
467 * bpf_map_put() is needed because the above
468 * bpf_map_alloc_id() has published the map
469 * to the userspace and the userspace may
470 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
471 */
472 bpf_map_put(map);
473 return err;
474 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700475
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100476 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700477 return err;
478
479free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100480 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700481free_map_sec:
482 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100483free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700484 map->ops->map_free(map);
485 return err;
486}
487
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700488/* if error is returned, fd is released.
489 * On success caller should complete fd access with matching fdput()
490 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100491struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700492{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700493 if (!f.file)
494 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700495 if (f.file->f_op != &bpf_map_fops) {
496 fdput(f);
497 return ERR_PTR(-EINVAL);
498 }
499
Daniel Borkmannc2101292015-10-29 14:58:07 +0100500 return f.file->private_data;
501}
502
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700503/* prog's and map's refcnt limit */
504#define BPF_MAX_REFCNT 32768
505
506struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100507{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700508 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
509 atomic_dec(&map->refcnt);
510 return ERR_PTR(-EBUSY);
511 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100512 if (uref)
513 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700514 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100515}
516
517struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100518{
519 struct fd f = fdget(ufd);
520 struct bpf_map *map;
521
522 map = __bpf_map_get(f);
523 if (IS_ERR(map))
524 return map;
525
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700526 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100527 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700528
529 return map;
530}
531
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700532/* map_idr_lock should have been held */
533static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
534 bool uref)
535{
536 int refold;
537
538 refold = __atomic_add_unless(&map->refcnt, 1, 0);
539
540 if (refold >= BPF_MAX_REFCNT) {
541 __bpf_map_put(map, false);
542 return ERR_PTR(-EBUSY);
543 }
544
545 if (!refold)
546 return ERR_PTR(-ENOENT);
547
548 if (uref)
549 atomic_inc(&map->usercnt);
550
551 return map;
552}
553
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800554int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
555{
556 return -ENOTSUPP;
557}
558
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700559/* last field in 'union bpf_attr' used by this command */
560#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
561
562static int map_lookup_elem(union bpf_attr *attr)
563{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100564 void __user *ukey = u64_to_user_ptr(attr->key);
565 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700566 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700567 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800568 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800569 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200570 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700571 int err;
572
573 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
574 return -EINVAL;
575
Daniel Borkmann592867b2015-09-08 18:00:09 +0200576 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100577 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700578 if (IS_ERR(map))
579 return PTR_ERR(map);
580
Chenbo Feng6e71b042017-10-18 13:00:22 -0700581 if (!(f.file->f_mode & FMODE_CAN_READ)) {
582 err = -EPERM;
583 goto err_put;
584 }
585
Al Viroe4448ed2017-05-13 18:43:00 -0400586 key = memdup_user(ukey, map->key_size);
587 if (IS_ERR(key)) {
588 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700589 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400590 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700591
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800592 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800593 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800594 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
595 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700596 else if (IS_FD_MAP(map))
597 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800598 else
599 value_size = map->value_size;
600
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800601 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800602 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700603 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800604 goto free_key;
605
Jakub Kicinskia3884572018-01-11 20:29:09 -0800606 if (bpf_map_is_dev_bound(map)) {
607 err = bpf_map_offload_lookup_elem(map, key, value);
608 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
609 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800610 err = bpf_percpu_hash_copy(map, key, value);
611 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
612 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800613 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
614 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700615 } else if (IS_FD_ARRAY(map)) {
616 err = bpf_fd_array_map_lookup_elem(map, key, value);
617 } else if (IS_FD_HASH(map)) {
618 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800619 } else {
620 rcu_read_lock();
621 ptr = map->ops->map_lookup_elem(map, key);
622 if (ptr)
623 memcpy(value, ptr, value_size);
624 rcu_read_unlock();
625 err = ptr ? 0 : -ENOENT;
626 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800627
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800628 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800629 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700630
631 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800632 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800633 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700634
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100635 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700636 err = 0;
637
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800638free_value:
639 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700640free_key:
641 kfree(key);
642err_put:
643 fdput(f);
644 return err;
645}
646
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800647#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700648
649static int map_update_elem(union bpf_attr *attr)
650{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100651 void __user *ukey = u64_to_user_ptr(attr->key);
652 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700653 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700654 struct bpf_map *map;
655 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800656 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200657 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700658 int err;
659
660 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
661 return -EINVAL;
662
Daniel Borkmann592867b2015-09-08 18:00:09 +0200663 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100664 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700665 if (IS_ERR(map))
666 return PTR_ERR(map);
667
Chenbo Feng6e71b042017-10-18 13:00:22 -0700668 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
669 err = -EPERM;
670 goto err_put;
671 }
672
Al Viroe4448ed2017-05-13 18:43:00 -0400673 key = memdup_user(ukey, map->key_size);
674 if (IS_ERR(key)) {
675 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700676 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400677 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700678
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800679 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800680 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800681 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
682 value_size = round_up(map->value_size, 8) * num_possible_cpus();
683 else
684 value_size = map->value_size;
685
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700686 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800687 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700688 if (!value)
689 goto free_key;
690
691 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800692 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700693 goto free_value;
694
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200695 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800696 if (bpf_map_is_dev_bound(map)) {
697 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
698 goto out;
699 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200700 err = map->ops->map_update_elem(map, key, value, attr->flags);
701 goto out;
702 }
703
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800704 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
705 * inside bpf map update or delete otherwise deadlocks are possible
706 */
707 preempt_disable();
708 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800709 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
710 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800711 err = bpf_percpu_hash_update(map, key, value, attr->flags);
712 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
713 err = bpf_percpu_array_update(map, key, value, attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100714 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200715 rcu_read_lock();
716 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
717 attr->flags);
718 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700719 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
720 rcu_read_lock();
721 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
722 attr->flags);
723 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800724 } else {
725 rcu_read_lock();
726 err = map->ops->map_update_elem(map, key, value, attr->flags);
727 rcu_read_unlock();
728 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800729 __this_cpu_dec(bpf_prog_active);
730 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200731out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100732 if (!err)
733 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700734free_value:
735 kfree(value);
736free_key:
737 kfree(key);
738err_put:
739 fdput(f);
740 return err;
741}
742
743#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
744
745static int map_delete_elem(union bpf_attr *attr)
746{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100747 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700748 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700749 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200750 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700751 void *key;
752 int err;
753
754 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
755 return -EINVAL;
756
Daniel Borkmann592867b2015-09-08 18:00:09 +0200757 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100758 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700759 if (IS_ERR(map))
760 return PTR_ERR(map);
761
Chenbo Feng6e71b042017-10-18 13:00:22 -0700762 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
763 err = -EPERM;
764 goto err_put;
765 }
766
Al Viroe4448ed2017-05-13 18:43:00 -0400767 key = memdup_user(ukey, map->key_size);
768 if (IS_ERR(key)) {
769 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700770 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400771 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700772
Jakub Kicinskia3884572018-01-11 20:29:09 -0800773 if (bpf_map_is_dev_bound(map)) {
774 err = bpf_map_offload_delete_elem(map, key);
775 goto out;
776 }
777
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800778 preempt_disable();
779 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700780 rcu_read_lock();
781 err = map->ops->map_delete_elem(map, key);
782 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800783 __this_cpu_dec(bpf_prog_active);
784 preempt_enable();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800785out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100786 if (!err)
787 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700788 kfree(key);
789err_put:
790 fdput(f);
791 return err;
792}
793
794/* last field in 'union bpf_attr' used by this command */
795#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
796
797static int map_get_next_key(union bpf_attr *attr)
798{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100799 void __user *ukey = u64_to_user_ptr(attr->key);
800 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700801 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700802 struct bpf_map *map;
803 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200804 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700805 int err;
806
807 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
808 return -EINVAL;
809
Daniel Borkmann592867b2015-09-08 18:00:09 +0200810 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100811 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700812 if (IS_ERR(map))
813 return PTR_ERR(map);
814
Chenbo Feng6e71b042017-10-18 13:00:22 -0700815 if (!(f.file->f_mode & FMODE_CAN_READ)) {
816 err = -EPERM;
817 goto err_put;
818 }
819
Teng Qin8fe45922017-04-24 19:00:37 -0700820 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400821 key = memdup_user(ukey, map->key_size);
822 if (IS_ERR(key)) {
823 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700824 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400825 }
Teng Qin8fe45922017-04-24 19:00:37 -0700826 } else {
827 key = NULL;
828 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700829
830 err = -ENOMEM;
831 next_key = kmalloc(map->key_size, GFP_USER);
832 if (!next_key)
833 goto free_key;
834
Jakub Kicinskia3884572018-01-11 20:29:09 -0800835 if (bpf_map_is_dev_bound(map)) {
836 err = bpf_map_offload_get_next_key(map, key, next_key);
837 goto out;
838 }
839
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700840 rcu_read_lock();
841 err = map->ops->map_get_next_key(map, key, next_key);
842 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800843out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700844 if (err)
845 goto free_next_key;
846
847 err = -EFAULT;
848 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
849 goto free_next_key;
850
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100851 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700852 err = 0;
853
854free_next_key:
855 kfree(next_key);
856free_key:
857 kfree(key);
858err_put:
859 fdput(f);
860 return err;
861}
862
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700863static const struct bpf_prog_ops * const bpf_prog_types[] = {
864#define BPF_PROG_TYPE(_id, _name) \
865 [_id] = & _name ## _prog_ops,
866#define BPF_MAP_TYPE(_id, _ops)
867#include <linux/bpf_types.h>
868#undef BPF_PROG_TYPE
869#undef BPF_MAP_TYPE
870};
871
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700872static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
873{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200874 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
875 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700876
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700877 if (!bpf_prog_is_dev_bound(prog->aux))
878 prog->aux->ops = bpf_prog_types[type];
879 else
880 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200881 prog->type = type;
882 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700883}
884
885/* drop refcnt on maps used by eBPF program and free auxilary data */
886static void free_used_maps(struct bpf_prog_aux *aux)
887{
888 int i;
889
890 for (i = 0; i < aux->used_map_cnt; i++)
891 bpf_map_put(aux->used_maps[i]);
892
893 kfree(aux->used_maps);
894}
895
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100896int __bpf_prog_charge(struct user_struct *user, u32 pages)
897{
898 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
899 unsigned long user_bufs;
900
901 if (user) {
902 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
903 if (user_bufs > memlock_limit) {
904 atomic_long_sub(pages, &user->locked_vm);
905 return -EPERM;
906 }
907 }
908
909 return 0;
910}
911
912void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
913{
914 if (user)
915 atomic_long_sub(pages, &user->locked_vm);
916}
917
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700918static int bpf_prog_charge_memlock(struct bpf_prog *prog)
919{
920 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100921 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700922
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100923 ret = __bpf_prog_charge(user, prog->pages);
924 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700925 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100926 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700927 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100928
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700929 prog->aux->user = user;
930 return 0;
931}
932
933static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
934{
935 struct user_struct *user = prog->aux->user;
936
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100937 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700938 free_uid(user);
939}
940
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700941static int bpf_prog_alloc_id(struct bpf_prog *prog)
942{
943 int id;
944
Shaohua Lib76354c2018-03-27 11:53:21 -0700945 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700946 spin_lock_bh(&prog_idr_lock);
947 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
948 if (id > 0)
949 prog->aux->id = id;
950 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700951 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700952
953 /* id is in [1, INT_MAX) */
954 if (WARN_ON_ONCE(!id))
955 return -ENOSPC;
956
957 return id > 0 ? 0 : id;
958}
959
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800960void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700961{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800962 /* cBPF to eBPF migrations are currently not in the idr store.
963 * Offloaded programs are removed from the store when their device
964 * disappears - even if someone grabs an fd to them they are unusable,
965 * simply waiting for refcnt to drop to be freed.
966 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700967 if (!prog->aux->id)
968 return;
969
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700970 if (do_idr_lock)
971 spin_lock_bh(&prog_idr_lock);
972 else
973 __acquire(&prog_idr_lock);
974
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700975 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800976 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700977
978 if (do_idr_lock)
979 spin_unlock_bh(&prog_idr_lock);
980 else
981 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700982}
983
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200984static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700985{
986 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
987
988 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700989 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700990 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700991 bpf_prog_free(aux->prog);
992}
993
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700994static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700995{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100996 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +0100997 int i;
998
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100999 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001000 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001001 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001002
1003 for (i = 0; i < prog->aux->func_cnt; i++)
1004 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +01001005 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001006
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001007 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001008 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001009}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001010
1011void bpf_prog_put(struct bpf_prog *prog)
1012{
1013 __bpf_prog_put(prog, true);
1014}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001015EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001016
1017static int bpf_prog_release(struct inode *inode, struct file *filp)
1018{
1019 struct bpf_prog *prog = filp->private_data;
1020
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001021 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001022 return 0;
1023}
1024
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001025#ifdef CONFIG_PROC_FS
1026static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1027{
1028 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001029 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001030
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001031 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001032 seq_printf(m,
1033 "prog_type:\t%u\n"
1034 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001035 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001036 "memlock:\t%llu\n",
1037 prog->type,
1038 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001039 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001040 prog->pages * 1ULL << PAGE_SHIFT);
1041}
1042#endif
1043
Chenbo Fengf66e4482017-10-18 13:00:26 -07001044const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001045#ifdef CONFIG_PROC_FS
1046 .show_fdinfo = bpf_prog_show_fdinfo,
1047#endif
1048 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001049 .read = bpf_dummy_read,
1050 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001051};
1052
Daniel Borkmannb2197752015-10-29 14:58:09 +01001053int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001054{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001055 int ret;
1056
1057 ret = security_bpf_prog(prog);
1058 if (ret < 0)
1059 return ret;
1060
Daniel Borkmannaa797812015-10-29 14:58:06 +01001061 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1062 O_RDWR | O_CLOEXEC);
1063}
1064
Daniel Borkmann113214b2016-06-30 17:24:44 +02001065static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001066{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001067 if (!f.file)
1068 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001069 if (f.file->f_op != &bpf_prog_fops) {
1070 fdput(f);
1071 return ERR_PTR(-EINVAL);
1072 }
1073
Daniel Borkmannc2101292015-10-29 14:58:07 +01001074 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001075}
1076
Brenden Blanco59d36562016-07-19 12:16:46 -07001077struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001078{
Brenden Blanco59d36562016-07-19 12:16:46 -07001079 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1080 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001081 return ERR_PTR(-EBUSY);
1082 }
1083 return prog;
1084}
Brenden Blanco59d36562016-07-19 12:16:46 -07001085EXPORT_SYMBOL_GPL(bpf_prog_add);
1086
Daniel Borkmannc5405942016-11-09 22:02:34 +01001087void bpf_prog_sub(struct bpf_prog *prog, int i)
1088{
1089 /* Only to be used for undoing previous bpf_prog_add() in some
1090 * error path. We still know that another entity in our call
1091 * path holds a reference to the program, thus atomic_sub() can
1092 * be safely used in such cases!
1093 */
1094 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1095}
1096EXPORT_SYMBOL_GPL(bpf_prog_sub);
1097
Brenden Blanco59d36562016-07-19 12:16:46 -07001098struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1099{
1100 return bpf_prog_add(prog, 1);
1101}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001102EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001103
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001104/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001105struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001106{
1107 int refold;
1108
1109 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1110
1111 if (refold >= BPF_MAX_REFCNT) {
1112 __bpf_prog_put(prog, false);
1113 return ERR_PTR(-EBUSY);
1114 }
1115
1116 if (!refold)
1117 return ERR_PTR(-ENOENT);
1118
1119 return prog;
1120}
John Fastabenda6f6df62017-08-15 22:32:22 -07001121EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001122
Al Viro040ee692017-12-02 20:20:38 -05001123bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001124 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001125{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001126 /* not an attachment, just a refcount inc, always allow */
1127 if (!attach_type)
1128 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001129
1130 if (prog->type != *attach_type)
1131 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001132 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001133 return false;
1134
1135 return true;
1136}
1137
1138static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001139 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001140{
1141 struct fd f = fdget(ufd);
1142 struct bpf_prog *prog;
1143
Daniel Borkmann113214b2016-06-30 17:24:44 +02001144 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001145 if (IS_ERR(prog))
1146 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001147 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001148 prog = ERR_PTR(-EINVAL);
1149 goto out;
1150 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001151
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001152 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001153out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001154 fdput(f);
1155 return prog;
1156}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001157
1158struct bpf_prog *bpf_prog_get(u32 ufd)
1159{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001160 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001161}
1162
Jakub Kicinski248f3462017-11-03 13:56:20 -07001163struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001164 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001165{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001166 struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001167
1168 if (!IS_ERR(prog))
1169 trace_bpf_prog_get_type(prog);
1170 return prog;
1171}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001172EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001173
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001174static int
1175bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1176 enum bpf_attach_type expected_attach_type)
1177{
1178 /* There are currently no prog types that require specifying
1179 * attach_type at load time.
1180 */
1181 return 0;
1182}
1183
1184static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1185 enum bpf_attach_type attach_type)
1186{
1187 /* There are currently no prog types that require specifying
1188 * attach_type at load time.
1189 */
1190 return 0;
1191}
1192
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001193/* last field in 'union bpf_attr' used by this command */
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001194#define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001195
1196static int bpf_prog_load(union bpf_attr *attr)
1197{
1198 enum bpf_prog_type type = attr->prog_type;
1199 struct bpf_prog *prog;
1200 int err;
1201 char license[128];
1202 bool is_gpl;
1203
1204 if (CHECK_ATTR(BPF_PROG_LOAD))
1205 return -EINVAL;
1206
David S. Millere07b98d2017-05-10 11:38:07 -07001207 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1208 return -EINVAL;
1209
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001210 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001211 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001212 sizeof(license) - 1) < 0)
1213 return -EFAULT;
1214 license[sizeof(license) - 1] = 0;
1215
1216 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1217 is_gpl = license_is_gpl_compatible(license);
1218
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001219 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1220 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001221
Alexei Starovoitov25415172015-03-25 12:49:20 -07001222 if (type == BPF_PROG_TYPE_KPROBE &&
1223 attr->kern_version != LINUX_VERSION_CODE)
1224 return -EINVAL;
1225
Chenbo Feng80b7d812017-05-31 18:16:00 -07001226 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1227 type != BPF_PROG_TYPE_CGROUP_SKB &&
1228 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001229 return -EPERM;
1230
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001231 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1232 return -EINVAL;
1233
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001234 /* plain bpf_prog allocation */
1235 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1236 if (!prog)
1237 return -ENOMEM;
1238
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001239 prog->expected_attach_type = attr->expected_attach_type;
1240
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001241 prog->aux->offload_requested = !!attr->prog_ifindex;
1242
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001243 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001244 if (err)
1245 goto free_prog_nouncharge;
1246
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001247 err = bpf_prog_charge_memlock(prog);
1248 if (err)
1249 goto free_prog_sec;
1250
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001251 prog->len = attr->insn_cnt;
1252
1253 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001254 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001255 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001256 goto free_prog;
1257
1258 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001259 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001260
1261 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001262 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001263
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001264 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001265 err = bpf_prog_offload_init(prog, attr);
1266 if (err)
1267 goto free_prog;
1268 }
1269
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001270 /* find program type: socket_filter vs tracing_filter */
1271 err = find_prog_type(type, prog);
1272 if (err < 0)
1273 goto free_prog;
1274
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001275 prog->aux->load_time = ktime_get_boot_ns();
1276 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1277 if (err)
1278 goto free_prog;
1279
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001280 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001281 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001282 if (err < 0)
1283 goto free_used_maps;
1284
1285 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001286 if (!prog->bpf_func)
1287 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001288 if (err < 0)
1289 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001290
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001291 err = bpf_prog_alloc_id(prog);
1292 if (err)
1293 goto free_used_maps;
1294
Daniel Borkmannaa797812015-10-29 14:58:06 +01001295 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001296 if (err < 0) {
1297 /* failed to allocate fd.
1298 * bpf_prog_put() is needed because the above
1299 * bpf_prog_alloc_id() has published the prog
1300 * to the userspace and the userspace may
1301 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1302 */
1303 bpf_prog_put(prog);
1304 return err;
1305 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001306
Daniel Borkmann74451e662017-02-16 22:24:50 +01001307 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001308 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001309 return err;
1310
1311free_used_maps:
1312 free_used_maps(prog->aux);
1313free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001314 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001315free_prog_sec:
1316 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001317free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001318 bpf_prog_free(prog);
1319 return err;
1320}
1321
Chenbo Feng6e71b042017-10-18 13:00:22 -07001322#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001323
1324static int bpf_obj_pin(const union bpf_attr *attr)
1325{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001326 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001327 return -EINVAL;
1328
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001329 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001330}
1331
1332static int bpf_obj_get(const union bpf_attr *attr)
1333{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001334 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1335 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001336 return -EINVAL;
1337
Chenbo Feng6e71b042017-10-18 13:00:22 -07001338 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1339 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001340}
1341
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001342struct bpf_raw_tracepoint {
1343 struct bpf_raw_event_map *btp;
1344 struct bpf_prog *prog;
1345};
1346
1347static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1348{
1349 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1350
1351 if (raw_tp->prog) {
1352 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1353 bpf_prog_put(raw_tp->prog);
1354 }
1355 kfree(raw_tp);
1356 return 0;
1357}
1358
1359static const struct file_operations bpf_raw_tp_fops = {
1360 .release = bpf_raw_tracepoint_release,
1361 .read = bpf_dummy_read,
1362 .write = bpf_dummy_write,
1363};
1364
1365#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1366
1367static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1368{
1369 struct bpf_raw_tracepoint *raw_tp;
1370 struct bpf_raw_event_map *btp;
1371 struct bpf_prog *prog;
1372 char tp_name[128];
1373 int tp_fd, err;
1374
1375 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1376 sizeof(tp_name) - 1) < 0)
1377 return -EFAULT;
1378 tp_name[sizeof(tp_name) - 1] = 0;
1379
1380 btp = bpf_find_raw_tracepoint(tp_name);
1381 if (!btp)
1382 return -ENOENT;
1383
1384 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1385 if (!raw_tp)
1386 return -ENOMEM;
1387 raw_tp->btp = btp;
1388
1389 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1390 BPF_PROG_TYPE_RAW_TRACEPOINT);
1391 if (IS_ERR(prog)) {
1392 err = PTR_ERR(prog);
1393 goto out_free_tp;
1394 }
1395
1396 err = bpf_probe_register(raw_tp->btp, prog);
1397 if (err)
1398 goto out_put_prog;
1399
1400 raw_tp->prog = prog;
1401 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1402 O_CLOEXEC);
1403 if (tp_fd < 0) {
1404 bpf_probe_unregister(raw_tp->btp, prog);
1405 err = tp_fd;
1406 goto out_put_prog;
1407 }
1408 return tp_fd;
1409
1410out_put_prog:
1411 bpf_prog_put(prog);
1412out_free_tp:
1413 kfree(raw_tp);
1414 return err;
1415}
1416
Daniel Mackf4324552016-11-23 16:52:27 +01001417#ifdef CONFIG_CGROUP_BPF
1418
John Fastabend464bc0f2017-08-28 07:10:04 -07001419#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001420
John Fastabend4f738ad2018-03-18 12:57:10 -07001421static int sockmap_get_from_fd(const union bpf_attr *attr,
1422 int type, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001423{
John Fastabend5a67da22017-09-08 14:00:49 -07001424 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001425 int ufd = attr->target_fd;
1426 struct bpf_map *map;
1427 struct fd f;
1428 int err;
1429
1430 f = fdget(ufd);
1431 map = __bpf_map_get(f);
1432 if (IS_ERR(map))
1433 return PTR_ERR(map);
1434
John Fastabend5a67da22017-09-08 14:00:49 -07001435 if (attach) {
John Fastabend4f738ad2018-03-18 12:57:10 -07001436 prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
John Fastabend5a67da22017-09-08 14:00:49 -07001437 if (IS_ERR(prog)) {
1438 fdput(f);
1439 return PTR_ERR(prog);
1440 }
John Fastabend174a79f2017-08-15 22:32:47 -07001441 }
1442
John Fastabend5a67da22017-09-08 14:00:49 -07001443 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001444 if (err) {
1445 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001446 if (prog)
1447 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001448 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001449 }
1450
1451 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001452 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001453}
Daniel Mackf4324552016-11-23 16:52:27 +01001454
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001455#define BPF_F_ATTACH_MASK \
1456 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1457
Daniel Mackf4324552016-11-23 16:52:27 +01001458static int bpf_prog_attach(const union bpf_attr *attr)
1459{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001460 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001461 struct bpf_prog *prog;
1462 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001463 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001464
1465 if (!capable(CAP_NET_ADMIN))
1466 return -EPERM;
1467
1468 if (CHECK_ATTR(BPF_PROG_ATTACH))
1469 return -EINVAL;
1470
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001471 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001472 return -EINVAL;
1473
Daniel Mackf4324552016-11-23 16:52:27 +01001474 switch (attr->attach_type) {
1475 case BPF_CGROUP_INET_INGRESS:
1476 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001477 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001478 break;
David Ahern610236582016-12-01 08:48:04 -08001479 case BPF_CGROUP_INET_SOCK_CREATE:
1480 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1481 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001482 case BPF_CGROUP_SOCK_OPS:
1483 ptype = BPF_PROG_TYPE_SOCK_OPS;
1484 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001485 case BPF_CGROUP_DEVICE:
1486 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1487 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001488 case BPF_SK_MSG_VERDICT:
1489 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
John Fastabend464bc0f2017-08-28 07:10:04 -07001490 case BPF_SK_SKB_STREAM_PARSER:
1491 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001492 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001493 default:
1494 return -EINVAL;
1495 }
1496
David Ahernb2cd1252016-12-01 08:48:03 -08001497 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1498 if (IS_ERR(prog))
1499 return PTR_ERR(prog);
1500
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001501 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1502 bpf_prog_put(prog);
1503 return -EINVAL;
1504 }
1505
David Ahernb2cd1252016-12-01 08:48:03 -08001506 cgrp = cgroup_get_from_fd(attr->target_fd);
1507 if (IS_ERR(cgrp)) {
1508 bpf_prog_put(prog);
1509 return PTR_ERR(cgrp);
1510 }
1511
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001512 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1513 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001514 if (ret)
1515 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001516 cgroup_put(cgrp);
1517
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001518 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001519}
1520
1521#define BPF_PROG_DETACH_LAST_FIELD attach_type
1522
1523static int bpf_prog_detach(const union bpf_attr *attr)
1524{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001525 enum bpf_prog_type ptype;
1526 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001527 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001528 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001529
1530 if (!capable(CAP_NET_ADMIN))
1531 return -EPERM;
1532
1533 if (CHECK_ATTR(BPF_PROG_DETACH))
1534 return -EINVAL;
1535
1536 switch (attr->attach_type) {
1537 case BPF_CGROUP_INET_INGRESS:
1538 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001539 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1540 break;
David Ahern610236582016-12-01 08:48:04 -08001541 case BPF_CGROUP_INET_SOCK_CREATE:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001542 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1543 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001544 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001545 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001546 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001547 case BPF_CGROUP_DEVICE:
1548 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1549 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001550 case BPF_SK_MSG_VERDICT:
1551 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
John Fastabend5a67da22017-09-08 14:00:49 -07001552 case BPF_SK_SKB_STREAM_PARSER:
1553 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001554 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001555 default:
1556 return -EINVAL;
1557 }
1558
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001559 cgrp = cgroup_get_from_fd(attr->target_fd);
1560 if (IS_ERR(cgrp))
1561 return PTR_ERR(cgrp);
1562
1563 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1564 if (IS_ERR(prog))
1565 prog = NULL;
1566
1567 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1568 if (prog)
1569 bpf_prog_put(prog);
1570 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001571 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001572}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001573
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001574#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1575
1576static int bpf_prog_query(const union bpf_attr *attr,
1577 union bpf_attr __user *uattr)
1578{
1579 struct cgroup *cgrp;
1580 int ret;
1581
1582 if (!capable(CAP_NET_ADMIN))
1583 return -EPERM;
1584 if (CHECK_ATTR(BPF_PROG_QUERY))
1585 return -EINVAL;
1586 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1587 return -EINVAL;
1588
1589 switch (attr->query.attach_type) {
1590 case BPF_CGROUP_INET_INGRESS:
1591 case BPF_CGROUP_INET_EGRESS:
1592 case BPF_CGROUP_INET_SOCK_CREATE:
1593 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001594 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001595 break;
1596 default:
1597 return -EINVAL;
1598 }
1599 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1600 if (IS_ERR(cgrp))
1601 return PTR_ERR(cgrp);
1602 ret = cgroup_bpf_query(cgrp, attr, uattr);
1603 cgroup_put(cgrp);
1604 return ret;
1605}
Daniel Mackf4324552016-11-23 16:52:27 +01001606#endif /* CONFIG_CGROUP_BPF */
1607
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001608#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1609
1610static int bpf_prog_test_run(const union bpf_attr *attr,
1611 union bpf_attr __user *uattr)
1612{
1613 struct bpf_prog *prog;
1614 int ret = -ENOTSUPP;
1615
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001616 if (!capable(CAP_SYS_ADMIN))
1617 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001618 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1619 return -EINVAL;
1620
1621 prog = bpf_prog_get(attr->test.prog_fd);
1622 if (IS_ERR(prog))
1623 return PTR_ERR(prog);
1624
1625 if (prog->aux->ops->test_run)
1626 ret = prog->aux->ops->test_run(prog, attr, uattr);
1627
1628 bpf_prog_put(prog);
1629 return ret;
1630}
1631
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001632#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1633
1634static int bpf_obj_get_next_id(const union bpf_attr *attr,
1635 union bpf_attr __user *uattr,
1636 struct idr *idr,
1637 spinlock_t *lock)
1638{
1639 u32 next_id = attr->start_id;
1640 int err = 0;
1641
1642 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1643 return -EINVAL;
1644
1645 if (!capable(CAP_SYS_ADMIN))
1646 return -EPERM;
1647
1648 next_id++;
1649 spin_lock_bh(lock);
1650 if (!idr_get_next(idr, &next_id))
1651 err = -ENOENT;
1652 spin_unlock_bh(lock);
1653
1654 if (!err)
1655 err = put_user(next_id, &uattr->next_id);
1656
1657 return err;
1658}
1659
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001660#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1661
1662static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1663{
1664 struct bpf_prog *prog;
1665 u32 id = attr->prog_id;
1666 int fd;
1667
1668 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1669 return -EINVAL;
1670
1671 if (!capable(CAP_SYS_ADMIN))
1672 return -EPERM;
1673
1674 spin_lock_bh(&prog_idr_lock);
1675 prog = idr_find(&prog_idr, id);
1676 if (prog)
1677 prog = bpf_prog_inc_not_zero(prog);
1678 else
1679 prog = ERR_PTR(-ENOENT);
1680 spin_unlock_bh(&prog_idr_lock);
1681
1682 if (IS_ERR(prog))
1683 return PTR_ERR(prog);
1684
1685 fd = bpf_prog_new_fd(prog);
1686 if (fd < 0)
1687 bpf_prog_put(prog);
1688
1689 return fd;
1690}
1691
Chenbo Feng6e71b042017-10-18 13:00:22 -07001692#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001693
1694static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1695{
1696 struct bpf_map *map;
1697 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001698 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001699 int fd;
1700
Chenbo Feng6e71b042017-10-18 13:00:22 -07001701 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1702 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001703 return -EINVAL;
1704
1705 if (!capable(CAP_SYS_ADMIN))
1706 return -EPERM;
1707
Chenbo Feng6e71b042017-10-18 13:00:22 -07001708 f_flags = bpf_get_file_flag(attr->open_flags);
1709 if (f_flags < 0)
1710 return f_flags;
1711
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001712 spin_lock_bh(&map_idr_lock);
1713 map = idr_find(&map_idr, id);
1714 if (map)
1715 map = bpf_map_inc_not_zero(map, true);
1716 else
1717 map = ERR_PTR(-ENOENT);
1718 spin_unlock_bh(&map_idr_lock);
1719
1720 if (IS_ERR(map))
1721 return PTR_ERR(map);
1722
Chenbo Feng6e71b042017-10-18 13:00:22 -07001723 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001724 if (fd < 0)
1725 bpf_map_put(map);
1726
1727 return fd;
1728}
1729
Daniel Borkmann7105e822017-12-20 13:42:57 +01001730static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1731 unsigned long addr)
1732{
1733 int i;
1734
1735 for (i = 0; i < prog->aux->used_map_cnt; i++)
1736 if (prog->aux->used_maps[i] == (void *)addr)
1737 return prog->aux->used_maps[i];
1738 return NULL;
1739}
1740
1741static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1742{
1743 const struct bpf_map *map;
1744 struct bpf_insn *insns;
1745 u64 imm;
1746 int i;
1747
1748 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1749 GFP_USER);
1750 if (!insns)
1751 return insns;
1752
1753 for (i = 0; i < prog->len; i++) {
1754 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1755 insns[i].code = BPF_JMP | BPF_CALL;
1756 insns[i].imm = BPF_FUNC_tail_call;
1757 /* fall-through */
1758 }
1759 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1760 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1761 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1762 insns[i].code = BPF_JMP | BPF_CALL;
1763 if (!bpf_dump_raw_ok())
1764 insns[i].imm = 0;
1765 continue;
1766 }
1767
1768 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1769 continue;
1770
1771 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1772 map = bpf_map_from_imm(prog, imm);
1773 if (map) {
1774 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1775 insns[i].imm = map->id;
1776 insns[i + 1].imm = 0;
1777 continue;
1778 }
1779
1780 if (!bpf_dump_raw_ok() &&
1781 imm == (unsigned long)prog->aux) {
1782 insns[i].imm = 0;
1783 insns[i + 1].imm = 0;
1784 continue;
1785 }
1786 }
1787
1788 return insns;
1789}
1790
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001791static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1792 const union bpf_attr *attr,
1793 union bpf_attr __user *uattr)
1794{
1795 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1796 struct bpf_prog_info info = {};
1797 u32 info_len = attr->info.info_len;
1798 char __user *uinsns;
1799 u32 ulen;
1800 int err;
1801
1802 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1803 if (err)
1804 return err;
1805 info_len = min_t(u32, sizeof(info), info_len);
1806
1807 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001808 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001809
1810 info.type = prog->type;
1811 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001812 info.load_time = prog->aux->load_time;
1813 info.created_by_uid = from_kuid_munged(current_user_ns(),
1814 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001815
1816 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001817 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1818
1819 ulen = info.nr_map_ids;
1820 info.nr_map_ids = prog->aux->used_map_cnt;
1821 ulen = min_t(u32, info.nr_map_ids, ulen);
1822 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001823 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001824 u32 i;
1825
1826 for (i = 0; i < ulen; i++)
1827 if (put_user(prog->aux->used_maps[i]->id,
1828 &user_map_ids[i]))
1829 return -EFAULT;
1830 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001831
1832 if (!capable(CAP_SYS_ADMIN)) {
1833 info.jited_prog_len = 0;
1834 info.xlated_prog_len = 0;
1835 goto done;
1836 }
1837
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001838 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001839 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001840 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001841 struct bpf_insn *insns_sanitized;
1842 bool fault;
1843
1844 if (prog->blinded && !bpf_dump_raw_ok()) {
1845 info.xlated_prog_insns = 0;
1846 goto done;
1847 }
1848 insns_sanitized = bpf_insn_prepare_dump(prog);
1849 if (!insns_sanitized)
1850 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001851 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1852 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001853 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1854 kfree(insns_sanitized);
1855 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001856 return -EFAULT;
1857 }
1858
Jakub Kicinski675fc272017-12-27 18:39:09 -08001859 if (bpf_prog_is_dev_bound(prog->aux)) {
1860 err = bpf_prog_offload_info_fill(&info, prog);
1861 if (err)
1862 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001863 goto done;
1864 }
1865
1866 /* NOTE: the following code is supposed to be skipped for offload.
1867 * bpf_prog_offload_info_fill() is the place to fill similar fields
1868 * for offload.
1869 */
1870 ulen = info.jited_prog_len;
1871 info.jited_prog_len = prog->jited_len;
1872 if (info.jited_prog_len && ulen) {
1873 if (bpf_dump_raw_ok()) {
1874 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1875 ulen = min_t(u32, info.jited_prog_len, ulen);
1876 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1877 return -EFAULT;
1878 } else {
1879 info.jited_prog_insns = 0;
1880 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08001881 }
1882
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001883done:
1884 if (copy_to_user(uinfo, &info, info_len) ||
1885 put_user(info_len, &uattr->info.info_len))
1886 return -EFAULT;
1887
1888 return 0;
1889}
1890
1891static int bpf_map_get_info_by_fd(struct bpf_map *map,
1892 const union bpf_attr *attr,
1893 union bpf_attr __user *uattr)
1894{
1895 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1896 struct bpf_map_info info = {};
1897 u32 info_len = attr->info.info_len;
1898 int err;
1899
1900 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1901 if (err)
1902 return err;
1903 info_len = min_t(u32, sizeof(info), info_len);
1904
1905 info.type = map->map_type;
1906 info.id = map->id;
1907 info.key_size = map->key_size;
1908 info.value_size = map->value_size;
1909 info.max_entries = map->max_entries;
1910 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07001911 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001912
Jakub Kicinski52775b32018-01-17 19:13:28 -08001913 if (bpf_map_is_dev_bound(map)) {
1914 err = bpf_map_offload_info_fill(&info, map);
1915 if (err)
1916 return err;
1917 }
1918
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001919 if (copy_to_user(uinfo, &info, info_len) ||
1920 put_user(info_len, &uattr->info.info_len))
1921 return -EFAULT;
1922
1923 return 0;
1924}
1925
1926#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1927
1928static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1929 union bpf_attr __user *uattr)
1930{
1931 int ufd = attr->info.bpf_fd;
1932 struct fd f;
1933 int err;
1934
1935 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1936 return -EINVAL;
1937
1938 f = fdget(ufd);
1939 if (!f.file)
1940 return -EBADFD;
1941
1942 if (f.file->f_op == &bpf_prog_fops)
1943 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1944 uattr);
1945 else if (f.file->f_op == &bpf_map_fops)
1946 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1947 uattr);
1948 else
1949 err = -EINVAL;
1950
1951 fdput(f);
1952 return err;
1953}
1954
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001955SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1956{
1957 union bpf_attr attr = {};
1958 int err;
1959
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07001960 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001961 return -EPERM;
1962
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001963 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1964 if (err)
1965 return err;
1966 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001967
1968 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1969 if (copy_from_user(&attr, uattr, size) != 0)
1970 return -EFAULT;
1971
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001972 err = security_bpf(cmd, &attr, size);
1973 if (err < 0)
1974 return err;
1975
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001976 switch (cmd) {
1977 case BPF_MAP_CREATE:
1978 err = map_create(&attr);
1979 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001980 case BPF_MAP_LOOKUP_ELEM:
1981 err = map_lookup_elem(&attr);
1982 break;
1983 case BPF_MAP_UPDATE_ELEM:
1984 err = map_update_elem(&attr);
1985 break;
1986 case BPF_MAP_DELETE_ELEM:
1987 err = map_delete_elem(&attr);
1988 break;
1989 case BPF_MAP_GET_NEXT_KEY:
1990 err = map_get_next_key(&attr);
1991 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001992 case BPF_PROG_LOAD:
1993 err = bpf_prog_load(&attr);
1994 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001995 case BPF_OBJ_PIN:
1996 err = bpf_obj_pin(&attr);
1997 break;
1998 case BPF_OBJ_GET:
1999 err = bpf_obj_get(&attr);
2000 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002001#ifdef CONFIG_CGROUP_BPF
2002 case BPF_PROG_ATTACH:
2003 err = bpf_prog_attach(&attr);
2004 break;
2005 case BPF_PROG_DETACH:
2006 err = bpf_prog_detach(&attr);
2007 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002008 case BPF_PROG_QUERY:
2009 err = bpf_prog_query(&attr, uattr);
2010 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002011#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002012 case BPF_PROG_TEST_RUN:
2013 err = bpf_prog_test_run(&attr, uattr);
2014 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002015 case BPF_PROG_GET_NEXT_ID:
2016 err = bpf_obj_get_next_id(&attr, uattr,
2017 &prog_idr, &prog_idr_lock);
2018 break;
2019 case BPF_MAP_GET_NEXT_ID:
2020 err = bpf_obj_get_next_id(&attr, uattr,
2021 &map_idr, &map_idr_lock);
2022 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002023 case BPF_PROG_GET_FD_BY_ID:
2024 err = bpf_prog_get_fd_by_id(&attr);
2025 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002026 case BPF_MAP_GET_FD_BY_ID:
2027 err = bpf_map_get_fd_by_id(&attr);
2028 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002029 case BPF_OBJ_GET_INFO_BY_FD:
2030 err = bpf_obj_get_info_by_fd(&attr, uattr);
2031 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002032 case BPF_RAW_TRACEPOINT_OPEN:
2033 err = bpf_raw_tracepoint_open(&attr);
2034 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002035 default:
2036 err = -EINVAL;
2037 break;
2038 }
2039
2040 return err;
2041}