blob: fe23dc5a3ec48be4215c39ef742c50a2bf3203bb [file] [log] [blame]
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010013#include <linux/bpf_trace.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070014#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070015#include <linux/syscalls.h>
16#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010017#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010018#include <linux/vmalloc.h>
19#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070020#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070021#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070022#include <linux/license.h>
23#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070024#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010025#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070026#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070027#include <linux/cred.h>
28#include <linux/timekeeping.h>
29#include <linux/ctype.h>
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070030#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070031
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070032#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
34 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
35 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
36#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
37#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
38
Chenbo Feng6e71b042017-10-18 13:00:22 -070039#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
40
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080041DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070042static DEFINE_IDR(prog_idr);
43static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070044static DEFINE_IDR(map_idr);
45static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080046
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070047int sysctl_unprivileged_bpf_disabled __read_mostly;
48
Johannes Berg40077e02017-04-11 15:34:58 +020049static const struct bpf_map_ops * const bpf_map_types[] = {
50#define BPF_PROG_TYPE(_id, _ops)
51#define BPF_MAP_TYPE(_id, _ops) \
52 [_id] = &_ops,
53#include <linux/bpf_types.h>
54#undef BPF_PROG_TYPE
55#undef BPF_MAP_TYPE
56};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070057
Mickaël Salaün752ba562017-08-07 20:45:20 +020058/*
59 * If we're handed a bigger struct than we know of, ensure all the unknown bits
60 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
61 * we don't know about yet.
62 *
63 * There is a ToCToU between this function call and the following
64 * copy_from_user() call. However, this is not a concern since this function is
65 * meant to be a future-proofing of bits.
66 */
Mickaël Salaün58291a72017-08-07 20:45:19 +020067static int check_uarg_tail_zero(void __user *uaddr,
68 size_t expected_size,
69 size_t actual_size)
70{
71 unsigned char __user *addr;
72 unsigned char __user *end;
73 unsigned char val;
74 int err;
75
Mickaël Salaün752ba562017-08-07 20:45:20 +020076 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
77 return -E2BIG;
78
79 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
80 return -EFAULT;
81
Mickaël Salaün58291a72017-08-07 20:45:19 +020082 if (actual_size <= expected_size)
83 return 0;
84
85 addr = uaddr + expected_size;
86 end = uaddr + actual_size;
87
88 for (; addr < end; addr++) {
89 err = get_user(val, addr);
90 if (err)
91 return err;
92 if (val)
93 return -E2BIG;
94 }
95
96 return 0;
97}
98
Jakub Kicinskia3884572018-01-11 20:29:09 -080099const struct bpf_map_ops bpf_map_offload_ops = {
100 .map_alloc = bpf_map_offload_map_alloc,
101 .map_free = bpf_map_offload_map_free,
102};
103
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700104static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
105{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800106 const struct bpf_map_ops *ops;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700107 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800108 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700109
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800110 if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
111 return ERR_PTR(-EINVAL);
112 ops = bpf_map_types[attr->map_type];
113 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200114 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700115
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800116 if (ops->map_alloc_check) {
117 err = ops->map_alloc_check(attr);
118 if (err)
119 return ERR_PTR(err);
120 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800121 if (attr->map_ifindex)
122 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800123 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200124 if (IS_ERR(map))
125 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800126 map->ops = ops;
Johannes Berg40077e02017-04-11 15:34:58 +0200127 map->map_type = attr->map_type;
128 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700129}
130
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700131void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100132{
133 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
134 * trigger under memory pressure as we really just want to
135 * fail instead.
136 */
137 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
138 void *area;
139
140 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700141 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100142 if (area != NULL)
143 return area;
144 }
145
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700146 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
147 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100148}
149
150void bpf_map_area_free(void *area)
151{
152 kvfree(area);
153}
154
Jakub Kicinskibd475642018-01-11 20:29:06 -0800155void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
156{
157 map->map_type = attr->map_type;
158 map->key_size = attr->key_size;
159 map->value_size = attr->value_size;
160 map->max_entries = attr->max_entries;
161 map->map_flags = attr->map_flags;
162 map->numa_node = bpf_map_attr_numa_node(attr);
163}
164
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800165int bpf_map_precharge_memlock(u32 pages)
166{
167 struct user_struct *user = get_current_user();
168 unsigned long memlock_limit, cur;
169
170 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
171 cur = atomic_long_read(&user->locked_vm);
172 free_uid(user);
173 if (cur + pages > memlock_limit)
174 return -EPERM;
175 return 0;
176}
177
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700178static int bpf_map_charge_memlock(struct bpf_map *map)
179{
180 struct user_struct *user = get_current_user();
181 unsigned long memlock_limit;
182
183 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
184
185 atomic_long_add(map->pages, &user->locked_vm);
186
187 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
188 atomic_long_sub(map->pages, &user->locked_vm);
189 free_uid(user);
190 return -EPERM;
191 }
192 map->user = user;
193 return 0;
194}
195
196static void bpf_map_uncharge_memlock(struct bpf_map *map)
197{
198 struct user_struct *user = map->user;
199
200 atomic_long_sub(map->pages, &user->locked_vm);
201 free_uid(user);
202}
203
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700204static int bpf_map_alloc_id(struct bpf_map *map)
205{
206 int id;
207
Shaohua Lib76354c2018-03-27 11:53:21 -0700208 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700209 spin_lock_bh(&map_idr_lock);
210 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
211 if (id > 0)
212 map->id = id;
213 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700214 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700215
216 if (WARN_ON_ONCE(!id))
217 return -ENOSPC;
218
219 return id > 0 ? 0 : id;
220}
221
Jakub Kicinskia3884572018-01-11 20:29:09 -0800222void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700223{
Eric Dumazet930651a2017-09-19 09:15:59 -0700224 unsigned long flags;
225
Jakub Kicinskia3884572018-01-11 20:29:09 -0800226 /* Offloaded maps are removed from the IDR store when their device
227 * disappears - even if someone holds an fd to them they are unusable,
228 * the memory is gone, all ops will fail; they are simply waiting for
229 * refcnt to drop to be freed.
230 */
231 if (!map->id)
232 return;
233
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700234 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700235 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700236 else
237 __acquire(&map_idr_lock);
238
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700239 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800240 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700241
242 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700243 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700244 else
245 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700246}
247
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700248/* called from workqueue */
249static void bpf_map_free_deferred(struct work_struct *work)
250{
251 struct bpf_map *map = container_of(work, struct bpf_map, work);
252
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700253 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700254 security_bpf_map_free(map);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700255 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700256 /* implementation dependent freeing */
257 map->ops->map_free(map);
258}
259
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100260static void bpf_map_put_uref(struct bpf_map *map)
261{
262 if (atomic_dec_and_test(&map->usercnt)) {
263 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
264 bpf_fd_array_map_clear(map);
265 }
266}
267
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700268/* decrement map refcnt and schedule it for freeing via workqueue
269 * (unrelying map implementation ops->map_free() might sleep)
270 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700271static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700272{
273 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700274 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700275 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700276 INIT_WORK(&map->work, bpf_map_free_deferred);
277 schedule_work(&map->work);
278 }
279}
280
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700281void bpf_map_put(struct bpf_map *map)
282{
283 __bpf_map_put(map, true);
284}
285
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100286void bpf_map_put_with_uref(struct bpf_map *map)
287{
288 bpf_map_put_uref(map);
289 bpf_map_put(map);
290}
291
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700292static int bpf_map_release(struct inode *inode, struct file *filp)
293{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200294 struct bpf_map *map = filp->private_data;
295
296 if (map->ops->map_release)
297 map->ops->map_release(map, filp);
298
299 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700300 return 0;
301}
302
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100303#ifdef CONFIG_PROC_FS
304static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
305{
306 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100307 const struct bpf_array *array;
308 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200309 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100310
311 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
312 array = container_of(map, struct bpf_array, map);
313 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200314 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100315 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100316
317 seq_printf(m,
318 "map_type:\t%u\n"
319 "key_size:\t%u\n"
320 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100321 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100322 "map_flags:\t%#x\n"
323 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100324 map->map_type,
325 map->key_size,
326 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100327 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100328 map->map_flags,
329 map->pages * 1ULL << PAGE_SHIFT);
330
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200331 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100332 seq_printf(m, "owner_prog_type:\t%u\n",
333 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200334 seq_printf(m, "owner_jited:\t%u\n",
335 owner_jited);
336 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100337}
338#endif
339
Chenbo Feng6e71b042017-10-18 13:00:22 -0700340static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
341 loff_t *ppos)
342{
343 /* We need this handler such that alloc_file() enables
344 * f_mode with FMODE_CAN_READ.
345 */
346 return -EINVAL;
347}
348
349static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
350 size_t siz, loff_t *ppos)
351{
352 /* We need this handler such that alloc_file() enables
353 * f_mode with FMODE_CAN_WRITE.
354 */
355 return -EINVAL;
356}
357
Chenbo Fengf66e4482017-10-18 13:00:26 -0700358const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100359#ifdef CONFIG_PROC_FS
360 .show_fdinfo = bpf_map_show_fdinfo,
361#endif
362 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700363 .read = bpf_dummy_read,
364 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700365};
366
Chenbo Feng6e71b042017-10-18 13:00:22 -0700367int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100368{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700369 int ret;
370
371 ret = security_bpf_map(map, OPEN_FMODE(flags));
372 if (ret < 0)
373 return ret;
374
Daniel Borkmannaa797812015-10-29 14:58:06 +0100375 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700376 flags | O_CLOEXEC);
377}
378
379int bpf_get_file_flag(int flags)
380{
381 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
382 return -EINVAL;
383 if (flags & BPF_F_RDONLY)
384 return O_RDONLY;
385 if (flags & BPF_F_WRONLY)
386 return O_WRONLY;
387 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100388}
389
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700390/* helper macro to check that unused fields 'union bpf_attr' are zero */
391#define CHECK_ATTR(CMD) \
392 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
393 sizeof(attr->CMD##_LAST_FIELD), 0, \
394 sizeof(*attr) - \
395 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
396 sizeof(attr->CMD##_LAST_FIELD)) != NULL
397
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700398/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
399 * Return 0 on success and < 0 on error.
400 */
401static int bpf_obj_name_cpy(char *dst, const char *src)
402{
403 const char *end = src + BPF_OBJ_NAME_LEN;
404
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700405 memset(dst, 0, BPF_OBJ_NAME_LEN);
406
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700407 /* Copy all isalnum() and '_' char */
408 while (src < end && *src) {
409 if (!isalnum(*src) && *src != '_')
410 return -EINVAL;
411 *dst++ = *src++;
412 }
413
414 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
415 if (src == end)
416 return -EINVAL;
417
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700418 return 0;
419}
420
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700421#define BPF_MAP_CREATE_LAST_FIELD btf_value_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700422/* called via syscall */
423static int map_create(union bpf_attr *attr)
424{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700425 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700426 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700427 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700428 int err;
429
430 err = CHECK_ATTR(BPF_MAP_CREATE);
431 if (err)
432 return -EINVAL;
433
Chenbo Feng6e71b042017-10-18 13:00:22 -0700434 f_flags = bpf_get_file_flag(attr->map_flags);
435 if (f_flags < 0)
436 return f_flags;
437
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700438 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700439 ((unsigned int)numa_node >= nr_node_ids ||
440 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700441 return -EINVAL;
442
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700443 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
444 map = find_and_alloc_map(attr);
445 if (IS_ERR(map))
446 return PTR_ERR(map);
447
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700448 err = bpf_obj_name_cpy(map->name, attr->map_name);
449 if (err)
450 goto free_map_nouncharge;
451
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700452 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100453 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700454
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700455 if (bpf_map_support_seq_show(map) &&
456 (attr->btf_key_id || attr->btf_value_id)) {
457 struct btf *btf;
458
459 if (!attr->btf_key_id || !attr->btf_value_id) {
460 err = -EINVAL;
461 goto free_map_nouncharge;
462 }
463
464 btf = btf_get_by_fd(attr->btf_fd);
465 if (IS_ERR(btf)) {
466 err = PTR_ERR(btf);
467 goto free_map_nouncharge;
468 }
469
470 err = map->ops->map_check_btf(map, btf, attr->btf_key_id,
471 attr->btf_value_id);
472 if (err) {
473 btf_put(btf);
474 goto free_map_nouncharge;
475 }
476
477 map->btf = btf;
478 map->btf_key_id = attr->btf_key_id;
479 map->btf_value_id = attr->btf_value_id;
480 }
481
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700482 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700483 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100484 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700485
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700486 err = bpf_map_charge_memlock(map);
487 if (err)
488 goto free_map_sec;
489
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700490 err = bpf_map_alloc_id(map);
491 if (err)
492 goto free_map;
493
Chenbo Feng6e71b042017-10-18 13:00:22 -0700494 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700495 if (err < 0) {
496 /* failed to allocate fd.
497 * bpf_map_put() is needed because the above
498 * bpf_map_alloc_id() has published the map
499 * to the userspace and the userspace may
500 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
501 */
502 bpf_map_put(map);
503 return err;
504 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700505
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100506 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700507 return err;
508
509free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100510 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700511free_map_sec:
512 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100513free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700514 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700515 map->ops->map_free(map);
516 return err;
517}
518
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700519/* if error is returned, fd is released.
520 * On success caller should complete fd access with matching fdput()
521 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100522struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700523{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700524 if (!f.file)
525 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700526 if (f.file->f_op != &bpf_map_fops) {
527 fdput(f);
528 return ERR_PTR(-EINVAL);
529 }
530
Daniel Borkmannc2101292015-10-29 14:58:07 +0100531 return f.file->private_data;
532}
533
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700534/* prog's and map's refcnt limit */
535#define BPF_MAX_REFCNT 32768
536
537struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100538{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700539 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
540 atomic_dec(&map->refcnt);
541 return ERR_PTR(-EBUSY);
542 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100543 if (uref)
544 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700545 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100546}
547
548struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100549{
550 struct fd f = fdget(ufd);
551 struct bpf_map *map;
552
553 map = __bpf_map_get(f);
554 if (IS_ERR(map))
555 return map;
556
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700557 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100558 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700559
560 return map;
561}
562
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700563/* map_idr_lock should have been held */
564static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
565 bool uref)
566{
567 int refold;
568
569 refold = __atomic_add_unless(&map->refcnt, 1, 0);
570
571 if (refold >= BPF_MAX_REFCNT) {
572 __bpf_map_put(map, false);
573 return ERR_PTR(-EBUSY);
574 }
575
576 if (!refold)
577 return ERR_PTR(-ENOENT);
578
579 if (uref)
580 atomic_inc(&map->usercnt);
581
582 return map;
583}
584
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800585int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
586{
587 return -ENOTSUPP;
588}
589
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700590/* last field in 'union bpf_attr' used by this command */
591#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
592
593static int map_lookup_elem(union bpf_attr *attr)
594{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100595 void __user *ukey = u64_to_user_ptr(attr->key);
596 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700597 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700598 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800599 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800600 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200601 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700602 int err;
603
604 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
605 return -EINVAL;
606
Daniel Borkmann592867b2015-09-08 18:00:09 +0200607 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100608 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700609 if (IS_ERR(map))
610 return PTR_ERR(map);
611
Chenbo Feng6e71b042017-10-18 13:00:22 -0700612 if (!(f.file->f_mode & FMODE_CAN_READ)) {
613 err = -EPERM;
614 goto err_put;
615 }
616
Al Viroe4448ed2017-05-13 18:43:00 -0400617 key = memdup_user(ukey, map->key_size);
618 if (IS_ERR(key)) {
619 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700620 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400621 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700622
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800623 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800624 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800625 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
626 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700627 else if (IS_FD_MAP(map))
628 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800629 else
630 value_size = map->value_size;
631
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800632 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800633 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700634 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800635 goto free_key;
636
Jakub Kicinskia3884572018-01-11 20:29:09 -0800637 if (bpf_map_is_dev_bound(map)) {
638 err = bpf_map_offload_lookup_elem(map, key, value);
639 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
640 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800641 err = bpf_percpu_hash_copy(map, key, value);
642 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
643 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800644 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
645 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700646 } else if (IS_FD_ARRAY(map)) {
647 err = bpf_fd_array_map_lookup_elem(map, key, value);
648 } else if (IS_FD_HASH(map)) {
649 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800650 } else {
651 rcu_read_lock();
652 ptr = map->ops->map_lookup_elem(map, key);
653 if (ptr)
654 memcpy(value, ptr, value_size);
655 rcu_read_unlock();
656 err = ptr ? 0 : -ENOENT;
657 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800658
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800659 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800660 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700661
662 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800663 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800664 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700665
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100666 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700667 err = 0;
668
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800669free_value:
670 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700671free_key:
672 kfree(key);
673err_put:
674 fdput(f);
675 return err;
676}
677
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800678#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700679
680static int map_update_elem(union bpf_attr *attr)
681{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100682 void __user *ukey = u64_to_user_ptr(attr->key);
683 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700684 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700685 struct bpf_map *map;
686 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800687 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200688 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700689 int err;
690
691 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
692 return -EINVAL;
693
Daniel Borkmann592867b2015-09-08 18:00:09 +0200694 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100695 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700696 if (IS_ERR(map))
697 return PTR_ERR(map);
698
Chenbo Feng6e71b042017-10-18 13:00:22 -0700699 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
700 err = -EPERM;
701 goto err_put;
702 }
703
Al Viroe4448ed2017-05-13 18:43:00 -0400704 key = memdup_user(ukey, map->key_size);
705 if (IS_ERR(key)) {
706 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700707 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400708 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700709
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800710 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800711 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800712 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
713 value_size = round_up(map->value_size, 8) * num_possible_cpus();
714 else
715 value_size = map->value_size;
716
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700717 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800718 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700719 if (!value)
720 goto free_key;
721
722 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800723 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700724 goto free_value;
725
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200726 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800727 if (bpf_map_is_dev_bound(map)) {
728 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
729 goto out;
730 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200731 err = map->ops->map_update_elem(map, key, value, attr->flags);
732 goto out;
733 }
734
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800735 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
736 * inside bpf map update or delete otherwise deadlocks are possible
737 */
738 preempt_disable();
739 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800740 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
741 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800742 err = bpf_percpu_hash_update(map, key, value, attr->flags);
743 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
744 err = bpf_percpu_array_update(map, key, value, attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100745 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200746 rcu_read_lock();
747 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
748 attr->flags);
749 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700750 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
751 rcu_read_lock();
752 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
753 attr->flags);
754 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800755 } else {
756 rcu_read_lock();
757 err = map->ops->map_update_elem(map, key, value, attr->flags);
758 rcu_read_unlock();
759 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800760 __this_cpu_dec(bpf_prog_active);
761 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200762out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100763 if (!err)
764 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700765free_value:
766 kfree(value);
767free_key:
768 kfree(key);
769err_put:
770 fdput(f);
771 return err;
772}
773
774#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
775
776static int map_delete_elem(union bpf_attr *attr)
777{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100778 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700779 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700780 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200781 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700782 void *key;
783 int err;
784
785 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
786 return -EINVAL;
787
Daniel Borkmann592867b2015-09-08 18:00:09 +0200788 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100789 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700790 if (IS_ERR(map))
791 return PTR_ERR(map);
792
Chenbo Feng6e71b042017-10-18 13:00:22 -0700793 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
794 err = -EPERM;
795 goto err_put;
796 }
797
Al Viroe4448ed2017-05-13 18:43:00 -0400798 key = memdup_user(ukey, map->key_size);
799 if (IS_ERR(key)) {
800 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700801 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400802 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700803
Jakub Kicinskia3884572018-01-11 20:29:09 -0800804 if (bpf_map_is_dev_bound(map)) {
805 err = bpf_map_offload_delete_elem(map, key);
806 goto out;
807 }
808
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800809 preempt_disable();
810 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700811 rcu_read_lock();
812 err = map->ops->map_delete_elem(map, key);
813 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800814 __this_cpu_dec(bpf_prog_active);
815 preempt_enable();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800816out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100817 if (!err)
818 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700819 kfree(key);
820err_put:
821 fdput(f);
822 return err;
823}
824
825/* last field in 'union bpf_attr' used by this command */
826#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
827
828static int map_get_next_key(union bpf_attr *attr)
829{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100830 void __user *ukey = u64_to_user_ptr(attr->key);
831 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700832 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700833 struct bpf_map *map;
834 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200835 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700836 int err;
837
838 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
839 return -EINVAL;
840
Daniel Borkmann592867b2015-09-08 18:00:09 +0200841 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100842 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700843 if (IS_ERR(map))
844 return PTR_ERR(map);
845
Chenbo Feng6e71b042017-10-18 13:00:22 -0700846 if (!(f.file->f_mode & FMODE_CAN_READ)) {
847 err = -EPERM;
848 goto err_put;
849 }
850
Teng Qin8fe45922017-04-24 19:00:37 -0700851 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400852 key = memdup_user(ukey, map->key_size);
853 if (IS_ERR(key)) {
854 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700855 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400856 }
Teng Qin8fe45922017-04-24 19:00:37 -0700857 } else {
858 key = NULL;
859 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700860
861 err = -ENOMEM;
862 next_key = kmalloc(map->key_size, GFP_USER);
863 if (!next_key)
864 goto free_key;
865
Jakub Kicinskia3884572018-01-11 20:29:09 -0800866 if (bpf_map_is_dev_bound(map)) {
867 err = bpf_map_offload_get_next_key(map, key, next_key);
868 goto out;
869 }
870
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700871 rcu_read_lock();
872 err = map->ops->map_get_next_key(map, key, next_key);
873 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800874out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700875 if (err)
876 goto free_next_key;
877
878 err = -EFAULT;
879 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
880 goto free_next_key;
881
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100882 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700883 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{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200905 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
906 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700907
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700908 if (!bpf_prog_is_dev_bound(prog->aux))
909 prog->aux->ops = bpf_prog_types[type];
910 else
911 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200912 prog->type = type;
913 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700914}
915
916/* drop refcnt on maps used by eBPF program and free auxilary data */
917static void free_used_maps(struct bpf_prog_aux *aux)
918{
919 int i;
920
921 for (i = 0; i < aux->used_map_cnt; i++)
922 bpf_map_put(aux->used_maps[i]);
923
924 kfree(aux->used_maps);
925}
926
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100927int __bpf_prog_charge(struct user_struct *user, u32 pages)
928{
929 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
930 unsigned long user_bufs;
931
932 if (user) {
933 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
934 if (user_bufs > memlock_limit) {
935 atomic_long_sub(pages, &user->locked_vm);
936 return -EPERM;
937 }
938 }
939
940 return 0;
941}
942
943void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
944{
945 if (user)
946 atomic_long_sub(pages, &user->locked_vm);
947}
948
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700949static int bpf_prog_charge_memlock(struct bpf_prog *prog)
950{
951 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100952 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700953
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100954 ret = __bpf_prog_charge(user, prog->pages);
955 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700956 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100957 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700958 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100959
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700960 prog->aux->user = user;
961 return 0;
962}
963
964static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
965{
966 struct user_struct *user = prog->aux->user;
967
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100968 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700969 free_uid(user);
970}
971
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700972static int bpf_prog_alloc_id(struct bpf_prog *prog)
973{
974 int id;
975
Shaohua Lib76354c2018-03-27 11:53:21 -0700976 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700977 spin_lock_bh(&prog_idr_lock);
978 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
979 if (id > 0)
980 prog->aux->id = id;
981 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700982 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700983
984 /* id is in [1, INT_MAX) */
985 if (WARN_ON_ONCE(!id))
986 return -ENOSPC;
987
988 return id > 0 ? 0 : id;
989}
990
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800991void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700992{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800993 /* cBPF to eBPF migrations are currently not in the idr store.
994 * Offloaded programs are removed from the store when their device
995 * disappears - even if someone grabs an fd to them they are unusable,
996 * simply waiting for refcnt to drop to be freed.
997 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700998 if (!prog->aux->id)
999 return;
1000
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001001 if (do_idr_lock)
1002 spin_lock_bh(&prog_idr_lock);
1003 else
1004 __acquire(&prog_idr_lock);
1005
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001006 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001007 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001008
1009 if (do_idr_lock)
1010 spin_unlock_bh(&prog_idr_lock);
1011 else
1012 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001013}
1014
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001015static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001016{
1017 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1018
1019 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001020 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001021 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001022 bpf_prog_free(aux->prog);
1023}
1024
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001025static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001026{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001027 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001028 int i;
1029
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001030 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001031 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001032 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001033
1034 for (i = 0; i < prog->aux->func_cnt; i++)
1035 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +01001036 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001037
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001038 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001039 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001040}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001041
1042void bpf_prog_put(struct bpf_prog *prog)
1043{
1044 __bpf_prog_put(prog, true);
1045}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001046EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001047
1048static int bpf_prog_release(struct inode *inode, struct file *filp)
1049{
1050 struct bpf_prog *prog = filp->private_data;
1051
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001052 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001053 return 0;
1054}
1055
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001056#ifdef CONFIG_PROC_FS
1057static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1058{
1059 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001060 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001061
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001062 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001063 seq_printf(m,
1064 "prog_type:\t%u\n"
1065 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001066 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001067 "memlock:\t%llu\n",
1068 prog->type,
1069 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001070 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001071 prog->pages * 1ULL << PAGE_SHIFT);
1072}
1073#endif
1074
Chenbo Fengf66e4482017-10-18 13:00:26 -07001075const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001076#ifdef CONFIG_PROC_FS
1077 .show_fdinfo = bpf_prog_show_fdinfo,
1078#endif
1079 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001080 .read = bpf_dummy_read,
1081 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001082};
1083
Daniel Borkmannb2197752015-10-29 14:58:09 +01001084int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001085{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001086 int ret;
1087
1088 ret = security_bpf_prog(prog);
1089 if (ret < 0)
1090 return ret;
1091
Daniel Borkmannaa797812015-10-29 14:58:06 +01001092 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1093 O_RDWR | O_CLOEXEC);
1094}
1095
Daniel Borkmann113214b2016-06-30 17:24:44 +02001096static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001097{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001098 if (!f.file)
1099 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001100 if (f.file->f_op != &bpf_prog_fops) {
1101 fdput(f);
1102 return ERR_PTR(-EINVAL);
1103 }
1104
Daniel Borkmannc2101292015-10-29 14:58:07 +01001105 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001106}
1107
Brenden Blanco59d36562016-07-19 12:16:46 -07001108struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001109{
Brenden Blanco59d36562016-07-19 12:16:46 -07001110 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1111 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001112 return ERR_PTR(-EBUSY);
1113 }
1114 return prog;
1115}
Brenden Blanco59d36562016-07-19 12:16:46 -07001116EXPORT_SYMBOL_GPL(bpf_prog_add);
1117
Daniel Borkmannc5405942016-11-09 22:02:34 +01001118void bpf_prog_sub(struct bpf_prog *prog, int i)
1119{
1120 /* Only to be used for undoing previous bpf_prog_add() in some
1121 * error path. We still know that another entity in our call
1122 * path holds a reference to the program, thus atomic_sub() can
1123 * be safely used in such cases!
1124 */
1125 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1126}
1127EXPORT_SYMBOL_GPL(bpf_prog_sub);
1128
Brenden Blanco59d36562016-07-19 12:16:46 -07001129struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1130{
1131 return bpf_prog_add(prog, 1);
1132}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001133EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001134
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001135/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001136struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001137{
1138 int refold;
1139
1140 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1141
1142 if (refold >= BPF_MAX_REFCNT) {
1143 __bpf_prog_put(prog, false);
1144 return ERR_PTR(-EBUSY);
1145 }
1146
1147 if (!refold)
1148 return ERR_PTR(-ENOENT);
1149
1150 return prog;
1151}
John Fastabenda6f6df62017-08-15 22:32:22 -07001152EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001153
Al Viro040ee692017-12-02 20:20:38 -05001154bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001155 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001156{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001157 /* not an attachment, just a refcount inc, always allow */
1158 if (!attach_type)
1159 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001160
1161 if (prog->type != *attach_type)
1162 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001163 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001164 return false;
1165
1166 return true;
1167}
1168
1169static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001170 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001171{
1172 struct fd f = fdget(ufd);
1173 struct bpf_prog *prog;
1174
Daniel Borkmann113214b2016-06-30 17:24:44 +02001175 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001176 if (IS_ERR(prog))
1177 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001178 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001179 prog = ERR_PTR(-EINVAL);
1180 goto out;
1181 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001182
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001183 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001184out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001185 fdput(f);
1186 return prog;
1187}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001188
1189struct bpf_prog *bpf_prog_get(u32 ufd)
1190{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001191 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001192}
1193
Jakub Kicinski248f3462017-11-03 13:56:20 -07001194struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001195 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001196{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001197 struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001198
1199 if (!IS_ERR(prog))
1200 trace_bpf_prog_get_type(prog);
1201 return prog;
1202}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001203EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001204
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001205/* Initially all BPF programs could be loaded w/o specifying
1206 * expected_attach_type. Later for some of them specifying expected_attach_type
1207 * at load time became required so that program could be validated properly.
1208 * Programs of types that are allowed to be loaded both w/ and w/o (for
1209 * backward compatibility) expected_attach_type, should have the default attach
1210 * type assigned to expected_attach_type for the latter case, so that it can be
1211 * validated later at attach time.
1212 *
1213 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1214 * prog type requires it but has some attach types that have to be backward
1215 * compatible.
1216 */
1217static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1218{
1219 switch (attr->prog_type) {
1220 case BPF_PROG_TYPE_CGROUP_SOCK:
1221 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1222 * exist so checking for non-zero is the way to go here.
1223 */
1224 if (!attr->expected_attach_type)
1225 attr->expected_attach_type =
1226 BPF_CGROUP_INET_SOCK_CREATE;
1227 break;
1228 }
1229}
1230
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001231static int
1232bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1233 enum bpf_attach_type expected_attach_type)
1234{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001235 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001236 case BPF_PROG_TYPE_CGROUP_SOCK:
1237 switch (expected_attach_type) {
1238 case BPF_CGROUP_INET_SOCK_CREATE:
1239 case BPF_CGROUP_INET4_POST_BIND:
1240 case BPF_CGROUP_INET6_POST_BIND:
1241 return 0;
1242 default:
1243 return -EINVAL;
1244 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001245 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1246 switch (expected_attach_type) {
1247 case BPF_CGROUP_INET4_BIND:
1248 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001249 case BPF_CGROUP_INET4_CONNECT:
1250 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001251 return 0;
1252 default:
1253 return -EINVAL;
1254 }
1255 default:
1256 return 0;
1257 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001258}
1259
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001260/* last field in 'union bpf_attr' used by this command */
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001261#define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001262
1263static int bpf_prog_load(union bpf_attr *attr)
1264{
1265 enum bpf_prog_type type = attr->prog_type;
1266 struct bpf_prog *prog;
1267 int err;
1268 char license[128];
1269 bool is_gpl;
1270
1271 if (CHECK_ATTR(BPF_PROG_LOAD))
1272 return -EINVAL;
1273
David S. Millere07b98d2017-05-10 11:38:07 -07001274 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1275 return -EINVAL;
1276
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001277 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001278 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001279 sizeof(license) - 1) < 0)
1280 return -EFAULT;
1281 license[sizeof(license) - 1] = 0;
1282
1283 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1284 is_gpl = license_is_gpl_compatible(license);
1285
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001286 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1287 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001288
Alexei Starovoitov25415172015-03-25 12:49:20 -07001289 if (type == BPF_PROG_TYPE_KPROBE &&
1290 attr->kern_version != LINUX_VERSION_CODE)
1291 return -EINVAL;
1292
Chenbo Feng80b7d812017-05-31 18:16:00 -07001293 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1294 type != BPF_PROG_TYPE_CGROUP_SKB &&
1295 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001296 return -EPERM;
1297
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001298 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001299 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1300 return -EINVAL;
1301
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001302 /* plain bpf_prog allocation */
1303 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1304 if (!prog)
1305 return -ENOMEM;
1306
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001307 prog->expected_attach_type = attr->expected_attach_type;
1308
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001309 prog->aux->offload_requested = !!attr->prog_ifindex;
1310
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001311 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001312 if (err)
1313 goto free_prog_nouncharge;
1314
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001315 err = bpf_prog_charge_memlock(prog);
1316 if (err)
1317 goto free_prog_sec;
1318
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001319 prog->len = attr->insn_cnt;
1320
1321 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001322 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001323 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001324 goto free_prog;
1325
1326 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001327 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001328
1329 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001330 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001331
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001332 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001333 err = bpf_prog_offload_init(prog, attr);
1334 if (err)
1335 goto free_prog;
1336 }
1337
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001338 /* find program type: socket_filter vs tracing_filter */
1339 err = find_prog_type(type, prog);
1340 if (err < 0)
1341 goto free_prog;
1342
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001343 prog->aux->load_time = ktime_get_boot_ns();
1344 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1345 if (err)
1346 goto free_prog;
1347
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001348 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001349 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001350 if (err < 0)
1351 goto free_used_maps;
1352
1353 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001354 if (!prog->bpf_func)
1355 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001356 if (err < 0)
1357 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001358
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001359 err = bpf_prog_alloc_id(prog);
1360 if (err)
1361 goto free_used_maps;
1362
Daniel Borkmannaa797812015-10-29 14:58:06 +01001363 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001364 if (err < 0) {
1365 /* failed to allocate fd.
1366 * bpf_prog_put() is needed because the above
1367 * bpf_prog_alloc_id() has published the prog
1368 * to the userspace and the userspace may
1369 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1370 */
1371 bpf_prog_put(prog);
1372 return err;
1373 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001374
Daniel Borkmann74451e662017-02-16 22:24:50 +01001375 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001376 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001377 return err;
1378
1379free_used_maps:
1380 free_used_maps(prog->aux);
1381free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001382 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001383free_prog_sec:
1384 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001385free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001386 bpf_prog_free(prog);
1387 return err;
1388}
1389
Chenbo Feng6e71b042017-10-18 13:00:22 -07001390#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001391
1392static int bpf_obj_pin(const union bpf_attr *attr)
1393{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001394 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001395 return -EINVAL;
1396
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001397 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001398}
1399
1400static int bpf_obj_get(const union bpf_attr *attr)
1401{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001402 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1403 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001404 return -EINVAL;
1405
Chenbo Feng6e71b042017-10-18 13:00:22 -07001406 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1407 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001408}
1409
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001410struct bpf_raw_tracepoint {
1411 struct bpf_raw_event_map *btp;
1412 struct bpf_prog *prog;
1413};
1414
1415static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1416{
1417 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1418
1419 if (raw_tp->prog) {
1420 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1421 bpf_prog_put(raw_tp->prog);
1422 }
1423 kfree(raw_tp);
1424 return 0;
1425}
1426
1427static const struct file_operations bpf_raw_tp_fops = {
1428 .release = bpf_raw_tracepoint_release,
1429 .read = bpf_dummy_read,
1430 .write = bpf_dummy_write,
1431};
1432
1433#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1434
1435static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1436{
1437 struct bpf_raw_tracepoint *raw_tp;
1438 struct bpf_raw_event_map *btp;
1439 struct bpf_prog *prog;
1440 char tp_name[128];
1441 int tp_fd, err;
1442
1443 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1444 sizeof(tp_name) - 1) < 0)
1445 return -EFAULT;
1446 tp_name[sizeof(tp_name) - 1] = 0;
1447
1448 btp = bpf_find_raw_tracepoint(tp_name);
1449 if (!btp)
1450 return -ENOENT;
1451
1452 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1453 if (!raw_tp)
1454 return -ENOMEM;
1455 raw_tp->btp = btp;
1456
1457 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1458 BPF_PROG_TYPE_RAW_TRACEPOINT);
1459 if (IS_ERR(prog)) {
1460 err = PTR_ERR(prog);
1461 goto out_free_tp;
1462 }
1463
1464 err = bpf_probe_register(raw_tp->btp, prog);
1465 if (err)
1466 goto out_put_prog;
1467
1468 raw_tp->prog = prog;
1469 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1470 O_CLOEXEC);
1471 if (tp_fd < 0) {
1472 bpf_probe_unregister(raw_tp->btp, prog);
1473 err = tp_fd;
1474 goto out_put_prog;
1475 }
1476 return tp_fd;
1477
1478out_put_prog:
1479 bpf_prog_put(prog);
1480out_free_tp:
1481 kfree(raw_tp);
1482 return err;
1483}
1484
Daniel Mackf4324552016-11-23 16:52:27 +01001485#ifdef CONFIG_CGROUP_BPF
1486
Anders Roxell33491582018-04-03 14:09:47 +02001487static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1488 enum bpf_attach_type attach_type)
1489{
1490 switch (prog->type) {
1491 case BPF_PROG_TYPE_CGROUP_SOCK:
1492 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1493 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1494 default:
1495 return 0;
1496 }
1497}
1498
John Fastabend464bc0f2017-08-28 07:10:04 -07001499#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001500
John Fastabend4f738ad2018-03-18 12:57:10 -07001501static int sockmap_get_from_fd(const union bpf_attr *attr,
1502 int type, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001503{
John Fastabend5a67da22017-09-08 14:00:49 -07001504 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001505 int ufd = attr->target_fd;
1506 struct bpf_map *map;
1507 struct fd f;
1508 int err;
1509
1510 f = fdget(ufd);
1511 map = __bpf_map_get(f);
1512 if (IS_ERR(map))
1513 return PTR_ERR(map);
1514
John Fastabend5a67da22017-09-08 14:00:49 -07001515 if (attach) {
John Fastabend4f738ad2018-03-18 12:57:10 -07001516 prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
John Fastabend5a67da22017-09-08 14:00:49 -07001517 if (IS_ERR(prog)) {
1518 fdput(f);
1519 return PTR_ERR(prog);
1520 }
John Fastabend174a79f2017-08-15 22:32:47 -07001521 }
1522
John Fastabend5a67da22017-09-08 14:00:49 -07001523 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001524 if (err) {
1525 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001526 if (prog)
1527 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001528 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001529 }
1530
1531 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001532 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001533}
Daniel Mackf4324552016-11-23 16:52:27 +01001534
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001535#define BPF_F_ATTACH_MASK \
1536 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1537
Daniel Mackf4324552016-11-23 16:52:27 +01001538static int bpf_prog_attach(const union bpf_attr *attr)
1539{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001540 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001541 struct bpf_prog *prog;
1542 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001543 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001544
1545 if (!capable(CAP_NET_ADMIN))
1546 return -EPERM;
1547
1548 if (CHECK_ATTR(BPF_PROG_ATTACH))
1549 return -EINVAL;
1550
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001551 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001552 return -EINVAL;
1553
Daniel Mackf4324552016-11-23 16:52:27 +01001554 switch (attr->attach_type) {
1555 case BPF_CGROUP_INET_INGRESS:
1556 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001557 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001558 break;
David Ahern610236582016-12-01 08:48:04 -08001559 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001560 case BPF_CGROUP_INET4_POST_BIND:
1561 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001562 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1563 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001564 case BPF_CGROUP_INET4_BIND:
1565 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001566 case BPF_CGROUP_INET4_CONNECT:
1567 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001568 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1569 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001570 case BPF_CGROUP_SOCK_OPS:
1571 ptype = BPF_PROG_TYPE_SOCK_OPS;
1572 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001573 case BPF_CGROUP_DEVICE:
1574 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1575 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001576 case BPF_SK_MSG_VERDICT:
1577 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
John Fastabend464bc0f2017-08-28 07:10:04 -07001578 case BPF_SK_SKB_STREAM_PARSER:
1579 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001580 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001581 default:
1582 return -EINVAL;
1583 }
1584
David Ahernb2cd1252016-12-01 08:48:03 -08001585 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1586 if (IS_ERR(prog))
1587 return PTR_ERR(prog);
1588
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001589 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1590 bpf_prog_put(prog);
1591 return -EINVAL;
1592 }
1593
David Ahernb2cd1252016-12-01 08:48:03 -08001594 cgrp = cgroup_get_from_fd(attr->target_fd);
1595 if (IS_ERR(cgrp)) {
1596 bpf_prog_put(prog);
1597 return PTR_ERR(cgrp);
1598 }
1599
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001600 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1601 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001602 if (ret)
1603 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001604 cgroup_put(cgrp);
1605
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001606 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001607}
1608
1609#define BPF_PROG_DETACH_LAST_FIELD attach_type
1610
1611static int bpf_prog_detach(const union bpf_attr *attr)
1612{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001613 enum bpf_prog_type ptype;
1614 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001615 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001616 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001617
1618 if (!capable(CAP_NET_ADMIN))
1619 return -EPERM;
1620
1621 if (CHECK_ATTR(BPF_PROG_DETACH))
1622 return -EINVAL;
1623
1624 switch (attr->attach_type) {
1625 case BPF_CGROUP_INET_INGRESS:
1626 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001627 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1628 break;
David Ahern610236582016-12-01 08:48:04 -08001629 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001630 case BPF_CGROUP_INET4_POST_BIND:
1631 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001632 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1633 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001634 case BPF_CGROUP_INET4_BIND:
1635 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001636 case BPF_CGROUP_INET4_CONNECT:
1637 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001638 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1639 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001640 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001641 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001642 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001643 case BPF_CGROUP_DEVICE:
1644 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1645 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001646 case BPF_SK_MSG_VERDICT:
1647 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
John Fastabend5a67da22017-09-08 14:00:49 -07001648 case BPF_SK_SKB_STREAM_PARSER:
1649 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001650 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001651 default:
1652 return -EINVAL;
1653 }
1654
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001655 cgrp = cgroup_get_from_fd(attr->target_fd);
1656 if (IS_ERR(cgrp))
1657 return PTR_ERR(cgrp);
1658
1659 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1660 if (IS_ERR(prog))
1661 prog = NULL;
1662
1663 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1664 if (prog)
1665 bpf_prog_put(prog);
1666 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001667 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001668}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001669
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001670#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1671
1672static int bpf_prog_query(const union bpf_attr *attr,
1673 union bpf_attr __user *uattr)
1674{
1675 struct cgroup *cgrp;
1676 int ret;
1677
1678 if (!capable(CAP_NET_ADMIN))
1679 return -EPERM;
1680 if (CHECK_ATTR(BPF_PROG_QUERY))
1681 return -EINVAL;
1682 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1683 return -EINVAL;
1684
1685 switch (attr->query.attach_type) {
1686 case BPF_CGROUP_INET_INGRESS:
1687 case BPF_CGROUP_INET_EGRESS:
1688 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001689 case BPF_CGROUP_INET4_BIND:
1690 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001691 case BPF_CGROUP_INET4_POST_BIND:
1692 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001693 case BPF_CGROUP_INET4_CONNECT:
1694 case BPF_CGROUP_INET6_CONNECT:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001695 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001696 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001697 break;
1698 default:
1699 return -EINVAL;
1700 }
1701 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1702 if (IS_ERR(cgrp))
1703 return PTR_ERR(cgrp);
1704 ret = cgroup_bpf_query(cgrp, attr, uattr);
1705 cgroup_put(cgrp);
1706 return ret;
1707}
Daniel Mackf4324552016-11-23 16:52:27 +01001708#endif /* CONFIG_CGROUP_BPF */
1709
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001710#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1711
1712static int bpf_prog_test_run(const union bpf_attr *attr,
1713 union bpf_attr __user *uattr)
1714{
1715 struct bpf_prog *prog;
1716 int ret = -ENOTSUPP;
1717
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001718 if (!capable(CAP_SYS_ADMIN))
1719 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001720 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1721 return -EINVAL;
1722
1723 prog = bpf_prog_get(attr->test.prog_fd);
1724 if (IS_ERR(prog))
1725 return PTR_ERR(prog);
1726
1727 if (prog->aux->ops->test_run)
1728 ret = prog->aux->ops->test_run(prog, attr, uattr);
1729
1730 bpf_prog_put(prog);
1731 return ret;
1732}
1733
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001734#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1735
1736static int bpf_obj_get_next_id(const union bpf_attr *attr,
1737 union bpf_attr __user *uattr,
1738 struct idr *idr,
1739 spinlock_t *lock)
1740{
1741 u32 next_id = attr->start_id;
1742 int err = 0;
1743
1744 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1745 return -EINVAL;
1746
1747 if (!capable(CAP_SYS_ADMIN))
1748 return -EPERM;
1749
1750 next_id++;
1751 spin_lock_bh(lock);
1752 if (!idr_get_next(idr, &next_id))
1753 err = -ENOENT;
1754 spin_unlock_bh(lock);
1755
1756 if (!err)
1757 err = put_user(next_id, &uattr->next_id);
1758
1759 return err;
1760}
1761
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001762#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1763
1764static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1765{
1766 struct bpf_prog *prog;
1767 u32 id = attr->prog_id;
1768 int fd;
1769
1770 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1771 return -EINVAL;
1772
1773 if (!capable(CAP_SYS_ADMIN))
1774 return -EPERM;
1775
1776 spin_lock_bh(&prog_idr_lock);
1777 prog = idr_find(&prog_idr, id);
1778 if (prog)
1779 prog = bpf_prog_inc_not_zero(prog);
1780 else
1781 prog = ERR_PTR(-ENOENT);
1782 spin_unlock_bh(&prog_idr_lock);
1783
1784 if (IS_ERR(prog))
1785 return PTR_ERR(prog);
1786
1787 fd = bpf_prog_new_fd(prog);
1788 if (fd < 0)
1789 bpf_prog_put(prog);
1790
1791 return fd;
1792}
1793
Chenbo Feng6e71b042017-10-18 13:00:22 -07001794#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001795
1796static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1797{
1798 struct bpf_map *map;
1799 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001800 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001801 int fd;
1802
Chenbo Feng6e71b042017-10-18 13:00:22 -07001803 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1804 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001805 return -EINVAL;
1806
1807 if (!capable(CAP_SYS_ADMIN))
1808 return -EPERM;
1809
Chenbo Feng6e71b042017-10-18 13:00:22 -07001810 f_flags = bpf_get_file_flag(attr->open_flags);
1811 if (f_flags < 0)
1812 return f_flags;
1813
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001814 spin_lock_bh(&map_idr_lock);
1815 map = idr_find(&map_idr, id);
1816 if (map)
1817 map = bpf_map_inc_not_zero(map, true);
1818 else
1819 map = ERR_PTR(-ENOENT);
1820 spin_unlock_bh(&map_idr_lock);
1821
1822 if (IS_ERR(map))
1823 return PTR_ERR(map);
1824
Chenbo Feng6e71b042017-10-18 13:00:22 -07001825 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001826 if (fd < 0)
1827 bpf_map_put(map);
1828
1829 return fd;
1830}
1831
Daniel Borkmann7105e822017-12-20 13:42:57 +01001832static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1833 unsigned long addr)
1834{
1835 int i;
1836
1837 for (i = 0; i < prog->aux->used_map_cnt; i++)
1838 if (prog->aux->used_maps[i] == (void *)addr)
1839 return prog->aux->used_maps[i];
1840 return NULL;
1841}
1842
1843static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1844{
1845 const struct bpf_map *map;
1846 struct bpf_insn *insns;
1847 u64 imm;
1848 int i;
1849
1850 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1851 GFP_USER);
1852 if (!insns)
1853 return insns;
1854
1855 for (i = 0; i < prog->len; i++) {
1856 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1857 insns[i].code = BPF_JMP | BPF_CALL;
1858 insns[i].imm = BPF_FUNC_tail_call;
1859 /* fall-through */
1860 }
1861 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1862 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1863 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1864 insns[i].code = BPF_JMP | BPF_CALL;
1865 if (!bpf_dump_raw_ok())
1866 insns[i].imm = 0;
1867 continue;
1868 }
1869
1870 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1871 continue;
1872
1873 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1874 map = bpf_map_from_imm(prog, imm);
1875 if (map) {
1876 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1877 insns[i].imm = map->id;
1878 insns[i + 1].imm = 0;
1879 continue;
1880 }
1881
1882 if (!bpf_dump_raw_ok() &&
1883 imm == (unsigned long)prog->aux) {
1884 insns[i].imm = 0;
1885 insns[i + 1].imm = 0;
1886 continue;
1887 }
1888 }
1889
1890 return insns;
1891}
1892
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001893static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1894 const union bpf_attr *attr,
1895 union bpf_attr __user *uattr)
1896{
1897 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1898 struct bpf_prog_info info = {};
1899 u32 info_len = attr->info.info_len;
1900 char __user *uinsns;
1901 u32 ulen;
1902 int err;
1903
1904 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1905 if (err)
1906 return err;
1907 info_len = min_t(u32, sizeof(info), info_len);
1908
1909 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001910 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001911
1912 info.type = prog->type;
1913 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001914 info.load_time = prog->aux->load_time;
1915 info.created_by_uid = from_kuid_munged(current_user_ns(),
1916 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001917
1918 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001919 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1920
1921 ulen = info.nr_map_ids;
1922 info.nr_map_ids = prog->aux->used_map_cnt;
1923 ulen = min_t(u32, info.nr_map_ids, ulen);
1924 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001925 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001926 u32 i;
1927
1928 for (i = 0; i < ulen; i++)
1929 if (put_user(prog->aux->used_maps[i]->id,
1930 &user_map_ids[i]))
1931 return -EFAULT;
1932 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001933
1934 if (!capable(CAP_SYS_ADMIN)) {
1935 info.jited_prog_len = 0;
1936 info.xlated_prog_len = 0;
1937 goto done;
1938 }
1939
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001940 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001941 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001942 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001943 struct bpf_insn *insns_sanitized;
1944 bool fault;
1945
1946 if (prog->blinded && !bpf_dump_raw_ok()) {
1947 info.xlated_prog_insns = 0;
1948 goto done;
1949 }
1950 insns_sanitized = bpf_insn_prepare_dump(prog);
1951 if (!insns_sanitized)
1952 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001953 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1954 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001955 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1956 kfree(insns_sanitized);
1957 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001958 return -EFAULT;
1959 }
1960
Jakub Kicinski675fc272017-12-27 18:39:09 -08001961 if (bpf_prog_is_dev_bound(prog->aux)) {
1962 err = bpf_prog_offload_info_fill(&info, prog);
1963 if (err)
1964 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001965 goto done;
1966 }
1967
1968 /* NOTE: the following code is supposed to be skipped for offload.
1969 * bpf_prog_offload_info_fill() is the place to fill similar fields
1970 * for offload.
1971 */
1972 ulen = info.jited_prog_len;
1973 info.jited_prog_len = prog->jited_len;
1974 if (info.jited_prog_len && ulen) {
1975 if (bpf_dump_raw_ok()) {
1976 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1977 ulen = min_t(u32, info.jited_prog_len, ulen);
1978 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1979 return -EFAULT;
1980 } else {
1981 info.jited_prog_insns = 0;
1982 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08001983 }
1984
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001985done:
1986 if (copy_to_user(uinfo, &info, info_len) ||
1987 put_user(info_len, &uattr->info.info_len))
1988 return -EFAULT;
1989
1990 return 0;
1991}
1992
1993static int bpf_map_get_info_by_fd(struct bpf_map *map,
1994 const union bpf_attr *attr,
1995 union bpf_attr __user *uattr)
1996{
1997 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1998 struct bpf_map_info info = {};
1999 u32 info_len = attr->info.info_len;
2000 int err;
2001
2002 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2003 if (err)
2004 return err;
2005 info_len = min_t(u32, sizeof(info), info_len);
2006
2007 info.type = map->map_type;
2008 info.id = map->id;
2009 info.key_size = map->key_size;
2010 info.value_size = map->value_size;
2011 info.max_entries = map->max_entries;
2012 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002013 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002014
Jakub Kicinski52775b32018-01-17 19:13:28 -08002015 if (bpf_map_is_dev_bound(map)) {
2016 err = bpf_map_offload_info_fill(&info, map);
2017 if (err)
2018 return err;
2019 }
2020
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002021 if (copy_to_user(uinfo, &info, info_len) ||
2022 put_user(info_len, &uattr->info.info_len))
2023 return -EFAULT;
2024
2025 return 0;
2026}
2027
2028#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2029
2030static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2031 union bpf_attr __user *uattr)
2032{
2033 int ufd = attr->info.bpf_fd;
2034 struct fd f;
2035 int err;
2036
2037 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2038 return -EINVAL;
2039
2040 f = fdget(ufd);
2041 if (!f.file)
2042 return -EBADFD;
2043
2044 if (f.file->f_op == &bpf_prog_fops)
2045 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2046 uattr);
2047 else if (f.file->f_op == &bpf_map_fops)
2048 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2049 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002050 else if (f.file->f_op == &btf_fops)
2051 err = btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002052 else
2053 err = -EINVAL;
2054
2055 fdput(f);
2056 return err;
2057}
2058
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002059#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2060
2061static int bpf_btf_load(const union bpf_attr *attr)
2062{
2063 if (CHECK_ATTR(BPF_BTF_LOAD))
2064 return -EINVAL;
2065
2066 if (!capable(CAP_SYS_ADMIN))
2067 return -EPERM;
2068
2069 return btf_new_fd(attr);
2070}
2071
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002072SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2073{
2074 union bpf_attr attr = {};
2075 int err;
2076
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002077 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002078 return -EPERM;
2079
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002080 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
2081 if (err)
2082 return err;
2083 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002084
2085 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2086 if (copy_from_user(&attr, uattr, size) != 0)
2087 return -EFAULT;
2088
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002089 err = security_bpf(cmd, &attr, size);
2090 if (err < 0)
2091 return err;
2092
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002093 switch (cmd) {
2094 case BPF_MAP_CREATE:
2095 err = map_create(&attr);
2096 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002097 case BPF_MAP_LOOKUP_ELEM:
2098 err = map_lookup_elem(&attr);
2099 break;
2100 case BPF_MAP_UPDATE_ELEM:
2101 err = map_update_elem(&attr);
2102 break;
2103 case BPF_MAP_DELETE_ELEM:
2104 err = map_delete_elem(&attr);
2105 break;
2106 case BPF_MAP_GET_NEXT_KEY:
2107 err = map_get_next_key(&attr);
2108 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002109 case BPF_PROG_LOAD:
2110 err = bpf_prog_load(&attr);
2111 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002112 case BPF_OBJ_PIN:
2113 err = bpf_obj_pin(&attr);
2114 break;
2115 case BPF_OBJ_GET:
2116 err = bpf_obj_get(&attr);
2117 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002118#ifdef CONFIG_CGROUP_BPF
2119 case BPF_PROG_ATTACH:
2120 err = bpf_prog_attach(&attr);
2121 break;
2122 case BPF_PROG_DETACH:
2123 err = bpf_prog_detach(&attr);
2124 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002125 case BPF_PROG_QUERY:
2126 err = bpf_prog_query(&attr, uattr);
2127 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002128#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002129 case BPF_PROG_TEST_RUN:
2130 err = bpf_prog_test_run(&attr, uattr);
2131 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002132 case BPF_PROG_GET_NEXT_ID:
2133 err = bpf_obj_get_next_id(&attr, uattr,
2134 &prog_idr, &prog_idr_lock);
2135 break;
2136 case BPF_MAP_GET_NEXT_ID:
2137 err = bpf_obj_get_next_id(&attr, uattr,
2138 &map_idr, &map_idr_lock);
2139 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002140 case BPF_PROG_GET_FD_BY_ID:
2141 err = bpf_prog_get_fd_by_id(&attr);
2142 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002143 case BPF_MAP_GET_FD_BY_ID:
2144 err = bpf_map_get_fd_by_id(&attr);
2145 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002146 case BPF_OBJ_GET_INFO_BY_FD:
2147 err = bpf_obj_get_info_by_fd(&attr, uattr);
2148 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002149 case BPF_RAW_TRACEPOINT_OPEN:
2150 err = bpf_raw_tracepoint_open(&attr);
2151 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002152 case BPF_BTF_LOAD:
2153 err = bpf_btf_load(&attr);
2154 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002155 default:
2156 err = -EINVAL;
2157 break;
2158 }
2159
2160 return err;
2161}