blob: 676a06e6b32242a1d019041d18156606279c1592 [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
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070097static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
98{
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070099 struct bpf_map *map;
100
Johannes Berg40077e02017-04-11 15:34:58 +0200101 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
102 !bpf_map_types[attr->map_type])
103 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700104
Johannes Berg40077e02017-04-11 15:34:58 +0200105 map = bpf_map_types[attr->map_type]->map_alloc(attr);
106 if (IS_ERR(map))
107 return map;
108 map->ops = bpf_map_types[attr->map_type];
109 map->map_type = attr->map_type;
110 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700111}
112
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700113void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100114{
115 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
116 * trigger under memory pressure as we really just want to
117 * fail instead.
118 */
119 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
120 void *area;
121
122 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700123 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100124 if (area != NULL)
125 return area;
126 }
127
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700128 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
129 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100130}
131
132void bpf_map_area_free(void *area)
133{
134 kvfree(area);
135}
136
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800137int bpf_map_precharge_memlock(u32 pages)
138{
139 struct user_struct *user = get_current_user();
140 unsigned long memlock_limit, cur;
141
142 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
143 cur = atomic_long_read(&user->locked_vm);
144 free_uid(user);
145 if (cur + pages > memlock_limit)
146 return -EPERM;
147 return 0;
148}
149
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700150static int bpf_map_charge_memlock(struct bpf_map *map)
151{
152 struct user_struct *user = get_current_user();
153 unsigned long memlock_limit;
154
155 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
156
157 atomic_long_add(map->pages, &user->locked_vm);
158
159 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
160 atomic_long_sub(map->pages, &user->locked_vm);
161 free_uid(user);
162 return -EPERM;
163 }
164 map->user = user;
165 return 0;
166}
167
168static void bpf_map_uncharge_memlock(struct bpf_map *map)
169{
170 struct user_struct *user = map->user;
171
172 atomic_long_sub(map->pages, &user->locked_vm);
173 free_uid(user);
174}
175
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700176static int bpf_map_alloc_id(struct bpf_map *map)
177{
178 int id;
179
180 spin_lock_bh(&map_idr_lock);
181 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
182 if (id > 0)
183 map->id = id;
184 spin_unlock_bh(&map_idr_lock);
185
186 if (WARN_ON_ONCE(!id))
187 return -ENOSPC;
188
189 return id > 0 ? 0 : id;
190}
191
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700192static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700193{
Eric Dumazet930651a2017-09-19 09:15:59 -0700194 unsigned long flags;
195
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700196 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700197 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700198 else
199 __acquire(&map_idr_lock);
200
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700201 idr_remove(&map_idr, map->id);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700202
203 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700204 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700205 else
206 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700207}
208
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700209/* called from workqueue */
210static void bpf_map_free_deferred(struct work_struct *work)
211{
212 struct bpf_map *map = container_of(work, struct bpf_map, work);
213
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700214 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700215 /* implementation dependent freeing */
216 map->ops->map_free(map);
217}
218
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100219static void bpf_map_put_uref(struct bpf_map *map)
220{
221 if (atomic_dec_and_test(&map->usercnt)) {
222 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
223 bpf_fd_array_map_clear(map);
224 }
225}
226
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700227/* decrement map refcnt and schedule it for freeing via workqueue
228 * (unrelying map implementation ops->map_free() might sleep)
229 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700230static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700231{
232 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700233 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700234 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700235 INIT_WORK(&map->work, bpf_map_free_deferred);
236 schedule_work(&map->work);
237 }
238}
239
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700240void bpf_map_put(struct bpf_map *map)
241{
242 __bpf_map_put(map, true);
243}
244
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100245void bpf_map_put_with_uref(struct bpf_map *map)
246{
247 bpf_map_put_uref(map);
248 bpf_map_put(map);
249}
250
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700251static int bpf_map_release(struct inode *inode, struct file *filp)
252{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200253 struct bpf_map *map = filp->private_data;
254
255 if (map->ops->map_release)
256 map->ops->map_release(map, filp);
257
258 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700259 return 0;
260}
261
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100262#ifdef CONFIG_PROC_FS
263static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
264{
265 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100266 const struct bpf_array *array;
267 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200268 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100269
270 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
271 array = container_of(map, struct bpf_array, map);
272 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200273 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100274 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100275
276 seq_printf(m,
277 "map_type:\t%u\n"
278 "key_size:\t%u\n"
279 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100280 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100281 "map_flags:\t%#x\n"
282 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100283 map->map_type,
284 map->key_size,
285 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100286 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100287 map->map_flags,
288 map->pages * 1ULL << PAGE_SHIFT);
289
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200290 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100291 seq_printf(m, "owner_prog_type:\t%u\n",
292 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200293 seq_printf(m, "owner_jited:\t%u\n",
294 owner_jited);
295 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100296}
297#endif
298
Chenbo Feng6e71b042017-10-18 13:00:22 -0700299static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
300 loff_t *ppos)
301{
302 /* We need this handler such that alloc_file() enables
303 * f_mode with FMODE_CAN_READ.
304 */
305 return -EINVAL;
306}
307
308static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
309 size_t siz, loff_t *ppos)
310{
311 /* We need this handler such that alloc_file() enables
312 * f_mode with FMODE_CAN_WRITE.
313 */
314 return -EINVAL;
315}
316
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700317static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100318#ifdef CONFIG_PROC_FS
319 .show_fdinfo = bpf_map_show_fdinfo,
320#endif
321 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700322 .read = bpf_dummy_read,
323 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700324};
325
Chenbo Feng6e71b042017-10-18 13:00:22 -0700326int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100327{
328 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700329 flags | O_CLOEXEC);
330}
331
332int bpf_get_file_flag(int flags)
333{
334 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
335 return -EINVAL;
336 if (flags & BPF_F_RDONLY)
337 return O_RDONLY;
338 if (flags & BPF_F_WRONLY)
339 return O_WRONLY;
340 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100341}
342
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700343/* helper macro to check that unused fields 'union bpf_attr' are zero */
344#define CHECK_ATTR(CMD) \
345 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
346 sizeof(attr->CMD##_LAST_FIELD), 0, \
347 sizeof(*attr) - \
348 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
349 sizeof(attr->CMD##_LAST_FIELD)) != NULL
350
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700351/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
352 * Return 0 on success and < 0 on error.
353 */
354static int bpf_obj_name_cpy(char *dst, const char *src)
355{
356 const char *end = src + BPF_OBJ_NAME_LEN;
357
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700358 memset(dst, 0, BPF_OBJ_NAME_LEN);
359
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700360 /* Copy all isalnum() and '_' char */
361 while (src < end && *src) {
362 if (!isalnum(*src) && *src != '_')
363 return -EINVAL;
364 *dst++ = *src++;
365 }
366
367 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
368 if (src == end)
369 return -EINVAL;
370
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700371 return 0;
372}
373
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700374#define BPF_MAP_CREATE_LAST_FIELD map_name
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700375/* called via syscall */
376static int map_create(union bpf_attr *attr)
377{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700378 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700379 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700380 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700381 int err;
382
383 err = CHECK_ATTR(BPF_MAP_CREATE);
384 if (err)
385 return -EINVAL;
386
Chenbo Feng6e71b042017-10-18 13:00:22 -0700387 f_flags = bpf_get_file_flag(attr->map_flags);
388 if (f_flags < 0)
389 return f_flags;
390
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700391 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700392 ((unsigned int)numa_node >= nr_node_ids ||
393 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700394 return -EINVAL;
395
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700396 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
397 map = find_and_alloc_map(attr);
398 if (IS_ERR(map))
399 return PTR_ERR(map);
400
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700401 err = bpf_obj_name_cpy(map->name, attr->map_name);
402 if (err)
403 goto free_map_nouncharge;
404
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700405 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100406 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700407
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700408 err = bpf_map_charge_memlock(map);
409 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100410 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700411
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700412 err = bpf_map_alloc_id(map);
413 if (err)
414 goto free_map;
415
Chenbo Feng6e71b042017-10-18 13:00:22 -0700416 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700417 if (err < 0) {
418 /* failed to allocate fd.
419 * bpf_map_put() is needed because the above
420 * bpf_map_alloc_id() has published the map
421 * to the userspace and the userspace may
422 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
423 */
424 bpf_map_put(map);
425 return err;
426 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700427
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100428 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700429 return err;
430
431free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100432 bpf_map_uncharge_memlock(map);
433free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700434 map->ops->map_free(map);
435 return err;
436}
437
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700438/* if error is returned, fd is released.
439 * On success caller should complete fd access with matching fdput()
440 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100441struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700442{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700443 if (!f.file)
444 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700445 if (f.file->f_op != &bpf_map_fops) {
446 fdput(f);
447 return ERR_PTR(-EINVAL);
448 }
449
Daniel Borkmannc2101292015-10-29 14:58:07 +0100450 return f.file->private_data;
451}
452
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700453/* prog's and map's refcnt limit */
454#define BPF_MAX_REFCNT 32768
455
456struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100457{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700458 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
459 atomic_dec(&map->refcnt);
460 return ERR_PTR(-EBUSY);
461 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100462 if (uref)
463 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700464 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100465}
466
467struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100468{
469 struct fd f = fdget(ufd);
470 struct bpf_map *map;
471
472 map = __bpf_map_get(f);
473 if (IS_ERR(map))
474 return map;
475
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700476 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100477 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700478
479 return map;
480}
481
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700482/* map_idr_lock should have been held */
483static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
484 bool uref)
485{
486 int refold;
487
488 refold = __atomic_add_unless(&map->refcnt, 1, 0);
489
490 if (refold >= BPF_MAX_REFCNT) {
491 __bpf_map_put(map, false);
492 return ERR_PTR(-EBUSY);
493 }
494
495 if (!refold)
496 return ERR_PTR(-ENOENT);
497
498 if (uref)
499 atomic_inc(&map->usercnt);
500
501 return map;
502}
503
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800504int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
505{
506 return -ENOTSUPP;
507}
508
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700509/* last field in 'union bpf_attr' used by this command */
510#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
511
512static int map_lookup_elem(union bpf_attr *attr)
513{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100514 void __user *ukey = u64_to_user_ptr(attr->key);
515 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700516 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700517 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800518 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800519 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200520 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700521 int err;
522
523 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
524 return -EINVAL;
525
Daniel Borkmann592867b2015-09-08 18:00:09 +0200526 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100527 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700528 if (IS_ERR(map))
529 return PTR_ERR(map);
530
Chenbo Feng6e71b042017-10-18 13:00:22 -0700531 if (!(f.file->f_mode & FMODE_CAN_READ)) {
532 err = -EPERM;
533 goto err_put;
534 }
535
Al Viroe4448ed2017-05-13 18:43:00 -0400536 key = memdup_user(ukey, map->key_size);
537 if (IS_ERR(key)) {
538 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700539 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400540 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700541
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800542 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800543 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800544 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
545 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700546 else if (IS_FD_MAP(map))
547 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800548 else
549 value_size = map->value_size;
550
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800551 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800552 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700553 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800554 goto free_key;
555
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800556 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
557 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800558 err = bpf_percpu_hash_copy(map, key, value);
559 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
560 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800561 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
562 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700563 } else if (IS_FD_ARRAY(map)) {
564 err = bpf_fd_array_map_lookup_elem(map, key, value);
565 } else if (IS_FD_HASH(map)) {
566 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800567 } else {
568 rcu_read_lock();
569 ptr = map->ops->map_lookup_elem(map, key);
570 if (ptr)
571 memcpy(value, ptr, value_size);
572 rcu_read_unlock();
573 err = ptr ? 0 : -ENOENT;
574 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800575
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800576 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800577 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700578
579 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800580 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800581 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700582
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100583 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700584 err = 0;
585
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800586free_value:
587 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700588free_key:
589 kfree(key);
590err_put:
591 fdput(f);
592 return err;
593}
594
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800595#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700596
597static int map_update_elem(union bpf_attr *attr)
598{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100599 void __user *ukey = u64_to_user_ptr(attr->key);
600 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700601 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700602 struct bpf_map *map;
603 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800604 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200605 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700606 int err;
607
608 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
609 return -EINVAL;
610
Daniel Borkmann592867b2015-09-08 18:00:09 +0200611 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100612 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700613 if (IS_ERR(map))
614 return PTR_ERR(map);
615
Chenbo Feng6e71b042017-10-18 13:00:22 -0700616 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
617 err = -EPERM;
618 goto err_put;
619 }
620
Al Viroe4448ed2017-05-13 18:43:00 -0400621 key = memdup_user(ukey, map->key_size);
622 if (IS_ERR(key)) {
623 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700624 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400625 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700626
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800627 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800628 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800629 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
630 value_size = round_up(map->value_size, 8) * num_possible_cpus();
631 else
632 value_size = map->value_size;
633
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700634 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800635 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700636 if (!value)
637 goto free_key;
638
639 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800640 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700641 goto free_value;
642
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200643 /* Need to create a kthread, thus must support schedule */
644 if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
645 err = map->ops->map_update_elem(map, key, value, attr->flags);
646 goto out;
647 }
648
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800649 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
650 * inside bpf map update or delete otherwise deadlocks are possible
651 */
652 preempt_disable();
653 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800654 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
655 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800656 err = bpf_percpu_hash_update(map, key, value, attr->flags);
657 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
658 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200659 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700660 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700661 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
662 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200663 rcu_read_lock();
664 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
665 attr->flags);
666 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700667 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
668 rcu_read_lock();
669 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
670 attr->flags);
671 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800672 } else {
673 rcu_read_lock();
674 err = map->ops->map_update_elem(map, key, value, attr->flags);
675 rcu_read_unlock();
676 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800677 __this_cpu_dec(bpf_prog_active);
678 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200679out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100680 if (!err)
681 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700682free_value:
683 kfree(value);
684free_key:
685 kfree(key);
686err_put:
687 fdput(f);
688 return err;
689}
690
691#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
692
693static int map_delete_elem(union bpf_attr *attr)
694{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100695 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700696 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700697 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200698 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700699 void *key;
700 int err;
701
702 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
703 return -EINVAL;
704
Daniel Borkmann592867b2015-09-08 18:00:09 +0200705 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100706 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700707 if (IS_ERR(map))
708 return PTR_ERR(map);
709
Chenbo Feng6e71b042017-10-18 13:00:22 -0700710 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
711 err = -EPERM;
712 goto err_put;
713 }
714
Al Viroe4448ed2017-05-13 18:43:00 -0400715 key = memdup_user(ukey, map->key_size);
716 if (IS_ERR(key)) {
717 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700718 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400719 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700720
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800721 preempt_disable();
722 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700723 rcu_read_lock();
724 err = map->ops->map_delete_elem(map, key);
725 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800726 __this_cpu_dec(bpf_prog_active);
727 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700728
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100729 if (!err)
730 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700731 kfree(key);
732err_put:
733 fdput(f);
734 return err;
735}
736
737/* last field in 'union bpf_attr' used by this command */
738#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
739
740static int map_get_next_key(union bpf_attr *attr)
741{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100742 void __user *ukey = u64_to_user_ptr(attr->key);
743 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700744 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700745 struct bpf_map *map;
746 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200747 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700748 int err;
749
750 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
751 return -EINVAL;
752
Daniel Borkmann592867b2015-09-08 18:00:09 +0200753 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100754 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700755 if (IS_ERR(map))
756 return PTR_ERR(map);
757
Chenbo Feng6e71b042017-10-18 13:00:22 -0700758 if (!(f.file->f_mode & FMODE_CAN_READ)) {
759 err = -EPERM;
760 goto err_put;
761 }
762
Teng Qin8fe45922017-04-24 19:00:37 -0700763 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400764 key = memdup_user(ukey, map->key_size);
765 if (IS_ERR(key)) {
766 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700767 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400768 }
Teng Qin8fe45922017-04-24 19:00:37 -0700769 } else {
770 key = NULL;
771 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700772
773 err = -ENOMEM;
774 next_key = kmalloc(map->key_size, GFP_USER);
775 if (!next_key)
776 goto free_key;
777
778 rcu_read_lock();
779 err = map->ops->map_get_next_key(map, key, next_key);
780 rcu_read_unlock();
781 if (err)
782 goto free_next_key;
783
784 err = -EFAULT;
785 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
786 goto free_next_key;
787
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100788 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700789 err = 0;
790
791free_next_key:
792 kfree(next_key);
793free_key:
794 kfree(key);
795err_put:
796 fdput(f);
797 return err;
798}
799
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700800static const struct bpf_prog_ops * const bpf_prog_types[] = {
801#define BPF_PROG_TYPE(_id, _name) \
802 [_id] = & _name ## _prog_ops,
803#define BPF_MAP_TYPE(_id, _ops)
804#include <linux/bpf_types.h>
805#undef BPF_PROG_TYPE
806#undef BPF_MAP_TYPE
807};
808
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700809static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
810{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200811 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
812 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700813
Johannes Bergbe9370a2017-04-11 15:34:57 +0200814 prog->aux->ops = bpf_prog_types[type];
815 prog->type = type;
816 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700817}
818
819/* drop refcnt on maps used by eBPF program and free auxilary data */
820static void free_used_maps(struct bpf_prog_aux *aux)
821{
822 int i;
823
824 for (i = 0; i < aux->used_map_cnt; i++)
825 bpf_map_put(aux->used_maps[i]);
826
827 kfree(aux->used_maps);
828}
829
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100830int __bpf_prog_charge(struct user_struct *user, u32 pages)
831{
832 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
833 unsigned long user_bufs;
834
835 if (user) {
836 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
837 if (user_bufs > memlock_limit) {
838 atomic_long_sub(pages, &user->locked_vm);
839 return -EPERM;
840 }
841 }
842
843 return 0;
844}
845
846void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
847{
848 if (user)
849 atomic_long_sub(pages, &user->locked_vm);
850}
851
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700852static int bpf_prog_charge_memlock(struct bpf_prog *prog)
853{
854 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100855 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700856
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100857 ret = __bpf_prog_charge(user, prog->pages);
858 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700859 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100860 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700861 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100862
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700863 prog->aux->user = user;
864 return 0;
865}
866
867static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
868{
869 struct user_struct *user = prog->aux->user;
870
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100871 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700872 free_uid(user);
873}
874
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700875static int bpf_prog_alloc_id(struct bpf_prog *prog)
876{
877 int id;
878
879 spin_lock_bh(&prog_idr_lock);
880 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
881 if (id > 0)
882 prog->aux->id = id;
883 spin_unlock_bh(&prog_idr_lock);
884
885 /* id is in [1, INT_MAX) */
886 if (WARN_ON_ONCE(!id))
887 return -ENOSPC;
888
889 return id > 0 ? 0 : id;
890}
891
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700892static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700893{
894 /* cBPF to eBPF migrations are currently not in the idr store. */
895 if (!prog->aux->id)
896 return;
897
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700898 if (do_idr_lock)
899 spin_lock_bh(&prog_idr_lock);
900 else
901 __acquire(&prog_idr_lock);
902
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700903 idr_remove(&prog_idr, prog->aux->id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700904
905 if (do_idr_lock)
906 spin_unlock_bh(&prog_idr_lock);
907 else
908 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700909}
910
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200911static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700912{
913 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
914
915 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700916 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700917 bpf_prog_free(aux->prog);
918}
919
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700920static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700921{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100922 if (atomic_dec_and_test(&prog->aux->refcnt)) {
923 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700924 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700925 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100926 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200927 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100928 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700929}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700930
931void bpf_prog_put(struct bpf_prog *prog)
932{
933 __bpf_prog_put(prog, true);
934}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100935EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700936
937static int bpf_prog_release(struct inode *inode, struct file *filp)
938{
939 struct bpf_prog *prog = filp->private_data;
940
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200941 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700942 return 0;
943}
944
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100945#ifdef CONFIG_PROC_FS
946static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
947{
948 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100949 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100950
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100951 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100952 seq_printf(m,
953 "prog_type:\t%u\n"
954 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100955 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100956 "memlock:\t%llu\n",
957 prog->type,
958 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100959 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100960 prog->pages * 1ULL << PAGE_SHIFT);
961}
962#endif
963
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700964static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100965#ifdef CONFIG_PROC_FS
966 .show_fdinfo = bpf_prog_show_fdinfo,
967#endif
968 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700969 .read = bpf_dummy_read,
970 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700971};
972
Daniel Borkmannb2197752015-10-29 14:58:09 +0100973int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100974{
975 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
976 O_RDWR | O_CLOEXEC);
977}
978
Daniel Borkmann113214b2016-06-30 17:24:44 +0200979static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700980{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700981 if (!f.file)
982 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700983 if (f.file->f_op != &bpf_prog_fops) {
984 fdput(f);
985 return ERR_PTR(-EINVAL);
986 }
987
Daniel Borkmannc2101292015-10-29 14:58:07 +0100988 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700989}
990
Brenden Blanco59d36562016-07-19 12:16:46 -0700991struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700992{
Brenden Blanco59d36562016-07-19 12:16:46 -0700993 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
994 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700995 return ERR_PTR(-EBUSY);
996 }
997 return prog;
998}
Brenden Blanco59d36562016-07-19 12:16:46 -0700999EXPORT_SYMBOL_GPL(bpf_prog_add);
1000
Daniel Borkmannc5405942016-11-09 22:02:34 +01001001void bpf_prog_sub(struct bpf_prog *prog, int i)
1002{
1003 /* Only to be used for undoing previous bpf_prog_add() in some
1004 * error path. We still know that another entity in our call
1005 * path holds a reference to the program, thus atomic_sub() can
1006 * be safely used in such cases!
1007 */
1008 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1009}
1010EXPORT_SYMBOL_GPL(bpf_prog_sub);
1011
Brenden Blanco59d36562016-07-19 12:16:46 -07001012struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1013{
1014 return bpf_prog_add(prog, 1);
1015}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001016EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001017
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001018/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001019struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001020{
1021 int refold;
1022
1023 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1024
1025 if (refold >= BPF_MAX_REFCNT) {
1026 __bpf_prog_put(prog, false);
1027 return ERR_PTR(-EBUSY);
1028 }
1029
1030 if (!refold)
1031 return ERR_PTR(-ENOENT);
1032
1033 return prog;
1034}
John Fastabenda6f6df62017-08-15 22:32:22 -07001035EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001036
Daniel Borkmann113214b2016-06-30 17:24:44 +02001037static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001038{
1039 struct fd f = fdget(ufd);
1040 struct bpf_prog *prog;
1041
Daniel Borkmann113214b2016-06-30 17:24:44 +02001042 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001043 if (IS_ERR(prog))
1044 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +02001045 if (type && prog->type != *type) {
1046 prog = ERR_PTR(-EINVAL);
1047 goto out;
1048 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001049
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001050 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001051out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001052 fdput(f);
1053 return prog;
1054}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001055
1056struct bpf_prog *bpf_prog_get(u32 ufd)
1057{
1058 return __bpf_prog_get(ufd, NULL);
1059}
1060
1061struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
1062{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001063 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
1064
1065 if (!IS_ERR(prog))
1066 trace_bpf_prog_get_type(prog);
1067 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +02001068}
1069EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001070
1071/* last field in 'union bpf_attr' used by this command */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001072#define BPF_PROG_LOAD_LAST_FIELD prog_name
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001073
1074static int bpf_prog_load(union bpf_attr *attr)
1075{
1076 enum bpf_prog_type type = attr->prog_type;
1077 struct bpf_prog *prog;
1078 int err;
1079 char license[128];
1080 bool is_gpl;
1081
1082 if (CHECK_ATTR(BPF_PROG_LOAD))
1083 return -EINVAL;
1084
David S. Millere07b98d2017-05-10 11:38:07 -07001085 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1086 return -EINVAL;
1087
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001088 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001089 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001090 sizeof(license) - 1) < 0)
1091 return -EFAULT;
1092 license[sizeof(license) - 1] = 0;
1093
1094 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1095 is_gpl = license_is_gpl_compatible(license);
1096
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001097 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1098 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001099
Alexei Starovoitov25415172015-03-25 12:49:20 -07001100 if (type == BPF_PROG_TYPE_KPROBE &&
1101 attr->kern_version != LINUX_VERSION_CODE)
1102 return -EINVAL;
1103
Chenbo Feng80b7d812017-05-31 18:16:00 -07001104 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1105 type != BPF_PROG_TYPE_CGROUP_SKB &&
1106 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001107 return -EPERM;
1108
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001109 /* plain bpf_prog allocation */
1110 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1111 if (!prog)
1112 return -ENOMEM;
1113
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001114 err = bpf_prog_charge_memlock(prog);
1115 if (err)
1116 goto free_prog_nouncharge;
1117
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001118 prog->len = attr->insn_cnt;
1119
1120 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001121 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001122 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001123 goto free_prog;
1124
1125 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001126 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001127
1128 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001129 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001130
1131 /* find program type: socket_filter vs tracing_filter */
1132 err = find_prog_type(type, prog);
1133 if (err < 0)
1134 goto free_prog;
1135
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001136 prog->aux->load_time = ktime_get_boot_ns();
1137 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1138 if (err)
1139 goto free_prog;
1140
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001141 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001142 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001143 if (err < 0)
1144 goto free_used_maps;
1145
1146 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001147 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001148 if (err < 0)
1149 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001150
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001151 err = bpf_prog_alloc_id(prog);
1152 if (err)
1153 goto free_used_maps;
1154
Daniel Borkmannaa797812015-10-29 14:58:06 +01001155 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001156 if (err < 0) {
1157 /* failed to allocate fd.
1158 * bpf_prog_put() is needed because the above
1159 * bpf_prog_alloc_id() has published the prog
1160 * to the userspace and the userspace may
1161 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1162 */
1163 bpf_prog_put(prog);
1164 return err;
1165 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001166
Daniel Borkmann74451e662017-02-16 22:24:50 +01001167 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001168 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001169 return err;
1170
1171free_used_maps:
1172 free_used_maps(prog->aux);
1173free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001174 bpf_prog_uncharge_memlock(prog);
1175free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001176 bpf_prog_free(prog);
1177 return err;
1178}
1179
Chenbo Feng6e71b042017-10-18 13:00:22 -07001180#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001181
1182static int bpf_obj_pin(const union bpf_attr *attr)
1183{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001184 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001185 return -EINVAL;
1186
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001187 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001188}
1189
1190static int bpf_obj_get(const union bpf_attr *attr)
1191{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001192 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1193 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001194 return -EINVAL;
1195
Chenbo Feng6e71b042017-10-18 13:00:22 -07001196 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1197 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001198}
1199
Daniel Mackf4324552016-11-23 16:52:27 +01001200#ifdef CONFIG_CGROUP_BPF
1201
John Fastabend464bc0f2017-08-28 07:10:04 -07001202#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001203
John Fastabend5a67da22017-09-08 14:00:49 -07001204static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001205{
John Fastabend5a67da22017-09-08 14:00:49 -07001206 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001207 int ufd = attr->target_fd;
1208 struct bpf_map *map;
1209 struct fd f;
1210 int err;
1211
1212 f = fdget(ufd);
1213 map = __bpf_map_get(f);
1214 if (IS_ERR(map))
1215 return PTR_ERR(map);
1216
John Fastabend5a67da22017-09-08 14:00:49 -07001217 if (attach) {
1218 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1219 BPF_PROG_TYPE_SK_SKB);
1220 if (IS_ERR(prog)) {
1221 fdput(f);
1222 return PTR_ERR(prog);
1223 }
John Fastabend174a79f2017-08-15 22:32:47 -07001224 }
1225
John Fastabend5a67da22017-09-08 14:00:49 -07001226 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001227 if (err) {
1228 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001229 if (prog)
1230 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001231 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001232 }
1233
1234 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001235 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001236}
Daniel Mackf4324552016-11-23 16:52:27 +01001237
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001238#define BPF_F_ATTACH_MASK \
1239 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1240
Daniel Mackf4324552016-11-23 16:52:27 +01001241static int bpf_prog_attach(const union bpf_attr *attr)
1242{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001243 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001244 struct bpf_prog *prog;
1245 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001246 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001247
1248 if (!capable(CAP_NET_ADMIN))
1249 return -EPERM;
1250
1251 if (CHECK_ATTR(BPF_PROG_ATTACH))
1252 return -EINVAL;
1253
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001254 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001255 return -EINVAL;
1256
Daniel Mackf4324552016-11-23 16:52:27 +01001257 switch (attr->attach_type) {
1258 case BPF_CGROUP_INET_INGRESS:
1259 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001260 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001261 break;
David Ahern610236582016-12-01 08:48:04 -08001262 case BPF_CGROUP_INET_SOCK_CREATE:
1263 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1264 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001265 case BPF_CGROUP_SOCK_OPS:
1266 ptype = BPF_PROG_TYPE_SOCK_OPS;
1267 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001268 case BPF_SK_SKB_STREAM_PARSER:
1269 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend5a67da22017-09-08 14:00:49 -07001270 return sockmap_get_from_fd(attr, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001271 default:
1272 return -EINVAL;
1273 }
1274
David Ahernb2cd1252016-12-01 08:48:03 -08001275 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1276 if (IS_ERR(prog))
1277 return PTR_ERR(prog);
1278
1279 cgrp = cgroup_get_from_fd(attr->target_fd);
1280 if (IS_ERR(cgrp)) {
1281 bpf_prog_put(prog);
1282 return PTR_ERR(cgrp);
1283 }
1284
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001285 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1286 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001287 if (ret)
1288 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001289 cgroup_put(cgrp);
1290
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001291 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001292}
1293
1294#define BPF_PROG_DETACH_LAST_FIELD attach_type
1295
1296static int bpf_prog_detach(const union bpf_attr *attr)
1297{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001298 enum bpf_prog_type ptype;
1299 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001300 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001301 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001302
1303 if (!capable(CAP_NET_ADMIN))
1304 return -EPERM;
1305
1306 if (CHECK_ATTR(BPF_PROG_DETACH))
1307 return -EINVAL;
1308
1309 switch (attr->attach_type) {
1310 case BPF_CGROUP_INET_INGRESS:
1311 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001312 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1313 break;
David Ahern610236582016-12-01 08:48:04 -08001314 case BPF_CGROUP_INET_SOCK_CREATE:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001315 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1316 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001317 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001318 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001319 break;
John Fastabend5a67da22017-09-08 14:00:49 -07001320 case BPF_SK_SKB_STREAM_PARSER:
1321 case BPF_SK_SKB_STREAM_VERDICT:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001322 return sockmap_get_from_fd(attr, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001323 default:
1324 return -EINVAL;
1325 }
1326
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001327 cgrp = cgroup_get_from_fd(attr->target_fd);
1328 if (IS_ERR(cgrp))
1329 return PTR_ERR(cgrp);
1330
1331 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1332 if (IS_ERR(prog))
1333 prog = NULL;
1334
1335 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1336 if (prog)
1337 bpf_prog_put(prog);
1338 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001339 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001340}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001341
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001342#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1343
1344static int bpf_prog_query(const union bpf_attr *attr,
1345 union bpf_attr __user *uattr)
1346{
1347 struct cgroup *cgrp;
1348 int ret;
1349
1350 if (!capable(CAP_NET_ADMIN))
1351 return -EPERM;
1352 if (CHECK_ATTR(BPF_PROG_QUERY))
1353 return -EINVAL;
1354 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1355 return -EINVAL;
1356
1357 switch (attr->query.attach_type) {
1358 case BPF_CGROUP_INET_INGRESS:
1359 case BPF_CGROUP_INET_EGRESS:
1360 case BPF_CGROUP_INET_SOCK_CREATE:
1361 case BPF_CGROUP_SOCK_OPS:
1362 break;
1363 default:
1364 return -EINVAL;
1365 }
1366 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1367 if (IS_ERR(cgrp))
1368 return PTR_ERR(cgrp);
1369 ret = cgroup_bpf_query(cgrp, attr, uattr);
1370 cgroup_put(cgrp);
1371 return ret;
1372}
Daniel Mackf4324552016-11-23 16:52:27 +01001373#endif /* CONFIG_CGROUP_BPF */
1374
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001375#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1376
1377static int bpf_prog_test_run(const union bpf_attr *attr,
1378 union bpf_attr __user *uattr)
1379{
1380 struct bpf_prog *prog;
1381 int ret = -ENOTSUPP;
1382
1383 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1384 return -EINVAL;
1385
1386 prog = bpf_prog_get(attr->test.prog_fd);
1387 if (IS_ERR(prog))
1388 return PTR_ERR(prog);
1389
1390 if (prog->aux->ops->test_run)
1391 ret = prog->aux->ops->test_run(prog, attr, uattr);
1392
1393 bpf_prog_put(prog);
1394 return ret;
1395}
1396
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001397#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1398
1399static int bpf_obj_get_next_id(const union bpf_attr *attr,
1400 union bpf_attr __user *uattr,
1401 struct idr *idr,
1402 spinlock_t *lock)
1403{
1404 u32 next_id = attr->start_id;
1405 int err = 0;
1406
1407 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1408 return -EINVAL;
1409
1410 if (!capable(CAP_SYS_ADMIN))
1411 return -EPERM;
1412
1413 next_id++;
1414 spin_lock_bh(lock);
1415 if (!idr_get_next(idr, &next_id))
1416 err = -ENOENT;
1417 spin_unlock_bh(lock);
1418
1419 if (!err)
1420 err = put_user(next_id, &uattr->next_id);
1421
1422 return err;
1423}
1424
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001425#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1426
1427static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1428{
1429 struct bpf_prog *prog;
1430 u32 id = attr->prog_id;
1431 int fd;
1432
1433 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1434 return -EINVAL;
1435
1436 if (!capable(CAP_SYS_ADMIN))
1437 return -EPERM;
1438
1439 spin_lock_bh(&prog_idr_lock);
1440 prog = idr_find(&prog_idr, id);
1441 if (prog)
1442 prog = bpf_prog_inc_not_zero(prog);
1443 else
1444 prog = ERR_PTR(-ENOENT);
1445 spin_unlock_bh(&prog_idr_lock);
1446
1447 if (IS_ERR(prog))
1448 return PTR_ERR(prog);
1449
1450 fd = bpf_prog_new_fd(prog);
1451 if (fd < 0)
1452 bpf_prog_put(prog);
1453
1454 return fd;
1455}
1456
Chenbo Feng6e71b042017-10-18 13:00:22 -07001457#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001458
1459static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1460{
1461 struct bpf_map *map;
1462 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001463 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001464 int fd;
1465
Chenbo Feng6e71b042017-10-18 13:00:22 -07001466 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1467 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001468 return -EINVAL;
1469
1470 if (!capable(CAP_SYS_ADMIN))
1471 return -EPERM;
1472
Chenbo Feng6e71b042017-10-18 13:00:22 -07001473 f_flags = bpf_get_file_flag(attr->open_flags);
1474 if (f_flags < 0)
1475 return f_flags;
1476
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001477 spin_lock_bh(&map_idr_lock);
1478 map = idr_find(&map_idr, id);
1479 if (map)
1480 map = bpf_map_inc_not_zero(map, true);
1481 else
1482 map = ERR_PTR(-ENOENT);
1483 spin_unlock_bh(&map_idr_lock);
1484
1485 if (IS_ERR(map))
1486 return PTR_ERR(map);
1487
Chenbo Feng6e71b042017-10-18 13:00:22 -07001488 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001489 if (fd < 0)
1490 bpf_map_put(map);
1491
1492 return fd;
1493}
1494
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001495static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1496 const union bpf_attr *attr,
1497 union bpf_attr __user *uattr)
1498{
1499 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1500 struct bpf_prog_info info = {};
1501 u32 info_len = attr->info.info_len;
1502 char __user *uinsns;
1503 u32 ulen;
1504 int err;
1505
1506 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1507 if (err)
1508 return err;
1509 info_len = min_t(u32, sizeof(info), info_len);
1510
1511 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001512 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001513
1514 info.type = prog->type;
1515 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001516 info.load_time = prog->aux->load_time;
1517 info.created_by_uid = from_kuid_munged(current_user_ns(),
1518 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001519
1520 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001521 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1522
1523 ulen = info.nr_map_ids;
1524 info.nr_map_ids = prog->aux->used_map_cnt;
1525 ulen = min_t(u32, info.nr_map_ids, ulen);
1526 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001527 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001528 u32 i;
1529
1530 for (i = 0; i < ulen; i++)
1531 if (put_user(prog->aux->used_maps[i]->id,
1532 &user_map_ids[i]))
1533 return -EFAULT;
1534 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001535
1536 if (!capable(CAP_SYS_ADMIN)) {
1537 info.jited_prog_len = 0;
1538 info.xlated_prog_len = 0;
1539 goto done;
1540 }
1541
1542 ulen = info.jited_prog_len;
1543 info.jited_prog_len = prog->jited_len;
1544 if (info.jited_prog_len && ulen) {
1545 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1546 ulen = min_t(u32, info.jited_prog_len, ulen);
1547 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1548 return -EFAULT;
1549 }
1550
1551 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001552 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001553 if (info.xlated_prog_len && ulen) {
1554 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1555 ulen = min_t(u32, info.xlated_prog_len, ulen);
1556 if (copy_to_user(uinsns, prog->insnsi, ulen))
1557 return -EFAULT;
1558 }
1559
1560done:
1561 if (copy_to_user(uinfo, &info, info_len) ||
1562 put_user(info_len, &uattr->info.info_len))
1563 return -EFAULT;
1564
1565 return 0;
1566}
1567
1568static int bpf_map_get_info_by_fd(struct bpf_map *map,
1569 const union bpf_attr *attr,
1570 union bpf_attr __user *uattr)
1571{
1572 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1573 struct bpf_map_info info = {};
1574 u32 info_len = attr->info.info_len;
1575 int err;
1576
1577 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1578 if (err)
1579 return err;
1580 info_len = min_t(u32, sizeof(info), info_len);
1581
1582 info.type = map->map_type;
1583 info.id = map->id;
1584 info.key_size = map->key_size;
1585 info.value_size = map->value_size;
1586 info.max_entries = map->max_entries;
1587 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07001588 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001589
1590 if (copy_to_user(uinfo, &info, info_len) ||
1591 put_user(info_len, &uattr->info.info_len))
1592 return -EFAULT;
1593
1594 return 0;
1595}
1596
1597#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1598
1599static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1600 union bpf_attr __user *uattr)
1601{
1602 int ufd = attr->info.bpf_fd;
1603 struct fd f;
1604 int err;
1605
1606 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1607 return -EINVAL;
1608
1609 f = fdget(ufd);
1610 if (!f.file)
1611 return -EBADFD;
1612
1613 if (f.file->f_op == &bpf_prog_fops)
1614 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1615 uattr);
1616 else if (f.file->f_op == &bpf_map_fops)
1617 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1618 uattr);
1619 else
1620 err = -EINVAL;
1621
1622 fdput(f);
1623 return err;
1624}
1625
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001626SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1627{
1628 union bpf_attr attr = {};
1629 int err;
1630
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001631 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001632 return -EPERM;
1633
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001634 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1635 if (err)
1636 return err;
1637 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001638
1639 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1640 if (copy_from_user(&attr, uattr, size) != 0)
1641 return -EFAULT;
1642
1643 switch (cmd) {
1644 case BPF_MAP_CREATE:
1645 err = map_create(&attr);
1646 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001647 case BPF_MAP_LOOKUP_ELEM:
1648 err = map_lookup_elem(&attr);
1649 break;
1650 case BPF_MAP_UPDATE_ELEM:
1651 err = map_update_elem(&attr);
1652 break;
1653 case BPF_MAP_DELETE_ELEM:
1654 err = map_delete_elem(&attr);
1655 break;
1656 case BPF_MAP_GET_NEXT_KEY:
1657 err = map_get_next_key(&attr);
1658 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001659 case BPF_PROG_LOAD:
1660 err = bpf_prog_load(&attr);
1661 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001662 case BPF_OBJ_PIN:
1663 err = bpf_obj_pin(&attr);
1664 break;
1665 case BPF_OBJ_GET:
1666 err = bpf_obj_get(&attr);
1667 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001668#ifdef CONFIG_CGROUP_BPF
1669 case BPF_PROG_ATTACH:
1670 err = bpf_prog_attach(&attr);
1671 break;
1672 case BPF_PROG_DETACH:
1673 err = bpf_prog_detach(&attr);
1674 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001675 case BPF_PROG_QUERY:
1676 err = bpf_prog_query(&attr, uattr);
1677 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001678#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001679 case BPF_PROG_TEST_RUN:
1680 err = bpf_prog_test_run(&attr, uattr);
1681 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001682 case BPF_PROG_GET_NEXT_ID:
1683 err = bpf_obj_get_next_id(&attr, uattr,
1684 &prog_idr, &prog_idr_lock);
1685 break;
1686 case BPF_MAP_GET_NEXT_ID:
1687 err = bpf_obj_get_next_id(&attr, uattr,
1688 &map_idr, &map_idr_lock);
1689 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001690 case BPF_PROG_GET_FD_BY_ID:
1691 err = bpf_prog_get_fd_by_id(&attr);
1692 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001693 case BPF_MAP_GET_FD_BY_ID:
1694 err = bpf_map_get_fd_by_id(&attr);
1695 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001696 case BPF_OBJ_GET_INFO_BY_FD:
1697 err = bpf_obj_get_info_by_fd(&attr, uattr);
1698 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001699 default:
1700 err = -EINVAL;
1701 break;
1702 }
1703
1704 return err;
1705}