blob: 5cb56d06b48d55f88bcdf45d5777abd291ba39e3 [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);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700215 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700216 /* implementation dependent freeing */
217 map->ops->map_free(map);
218}
219
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100220static void bpf_map_put_uref(struct bpf_map *map)
221{
222 if (atomic_dec_and_test(&map->usercnt)) {
223 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
224 bpf_fd_array_map_clear(map);
225 }
226}
227
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700228/* decrement map refcnt and schedule it for freeing via workqueue
229 * (unrelying map implementation ops->map_free() might sleep)
230 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700231static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700232{
233 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700234 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700235 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700236 INIT_WORK(&map->work, bpf_map_free_deferred);
237 schedule_work(&map->work);
238 }
239}
240
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700241void bpf_map_put(struct bpf_map *map)
242{
243 __bpf_map_put(map, true);
244}
245
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100246void bpf_map_put_with_uref(struct bpf_map *map)
247{
248 bpf_map_put_uref(map);
249 bpf_map_put(map);
250}
251
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700252static int bpf_map_release(struct inode *inode, struct file *filp)
253{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200254 struct bpf_map *map = filp->private_data;
255
256 if (map->ops->map_release)
257 map->ops->map_release(map, filp);
258
259 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700260 return 0;
261}
262
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100263#ifdef CONFIG_PROC_FS
264static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
265{
266 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100267 const struct bpf_array *array;
268 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200269 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100270
271 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
272 array = container_of(map, struct bpf_array, map);
273 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200274 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100275 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100276
277 seq_printf(m,
278 "map_type:\t%u\n"
279 "key_size:\t%u\n"
280 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100281 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100282 "map_flags:\t%#x\n"
283 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100284 map->map_type,
285 map->key_size,
286 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100287 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100288 map->map_flags,
289 map->pages * 1ULL << PAGE_SHIFT);
290
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200291 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100292 seq_printf(m, "owner_prog_type:\t%u\n",
293 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200294 seq_printf(m, "owner_jited:\t%u\n",
295 owner_jited);
296 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100297}
298#endif
299
Chenbo Feng6e71b042017-10-18 13:00:22 -0700300static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
301 loff_t *ppos)
302{
303 /* We need this handler such that alloc_file() enables
304 * f_mode with FMODE_CAN_READ.
305 */
306 return -EINVAL;
307}
308
309static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
310 size_t siz, loff_t *ppos)
311{
312 /* We need this handler such that alloc_file() enables
313 * f_mode with FMODE_CAN_WRITE.
314 */
315 return -EINVAL;
316}
317
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700318static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100319#ifdef CONFIG_PROC_FS
320 .show_fdinfo = bpf_map_show_fdinfo,
321#endif
322 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700323 .read = bpf_dummy_read,
324 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700325};
326
Chenbo Feng6e71b042017-10-18 13:00:22 -0700327int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100328{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700329 int ret;
330
331 ret = security_bpf_map(map, OPEN_FMODE(flags));
332 if (ret < 0)
333 return ret;
334
Daniel Borkmannaa797812015-10-29 14:58:06 +0100335 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700336 flags | O_CLOEXEC);
337}
338
339int bpf_get_file_flag(int flags)
340{
341 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
342 return -EINVAL;
343 if (flags & BPF_F_RDONLY)
344 return O_RDONLY;
345 if (flags & BPF_F_WRONLY)
346 return O_WRONLY;
347 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100348}
349
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700350/* helper macro to check that unused fields 'union bpf_attr' are zero */
351#define CHECK_ATTR(CMD) \
352 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
353 sizeof(attr->CMD##_LAST_FIELD), 0, \
354 sizeof(*attr) - \
355 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
356 sizeof(attr->CMD##_LAST_FIELD)) != NULL
357
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700358/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
359 * Return 0 on success and < 0 on error.
360 */
361static int bpf_obj_name_cpy(char *dst, const char *src)
362{
363 const char *end = src + BPF_OBJ_NAME_LEN;
364
Martin KaFai Lau473d97342017-10-05 21:52:11 -0700365 memset(dst, 0, BPF_OBJ_NAME_LEN);
366
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700367 /* Copy all isalnum() and '_' char */
368 while (src < end && *src) {
369 if (!isalnum(*src) && *src != '_')
370 return -EINVAL;
371 *dst++ = *src++;
372 }
373
374 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
375 if (src == end)
376 return -EINVAL;
377
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700378 return 0;
379}
380
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700381#define BPF_MAP_CREATE_LAST_FIELD map_name
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700382/* called via syscall */
383static int map_create(union bpf_attr *attr)
384{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700385 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700386 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700387 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700388 int err;
389
390 err = CHECK_ATTR(BPF_MAP_CREATE);
391 if (err)
392 return -EINVAL;
393
Chenbo Feng6e71b042017-10-18 13:00:22 -0700394 f_flags = bpf_get_file_flag(attr->map_flags);
395 if (f_flags < 0)
396 return f_flags;
397
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700398 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700399 ((unsigned int)numa_node >= nr_node_ids ||
400 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700401 return -EINVAL;
402
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700403 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
404 map = find_and_alloc_map(attr);
405 if (IS_ERR(map))
406 return PTR_ERR(map);
407
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700408 err = bpf_obj_name_cpy(map->name, attr->map_name);
409 if (err)
410 goto free_map_nouncharge;
411
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700412 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100413 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700414
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700415 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700416 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100417 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700418
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700419 err = bpf_map_charge_memlock(map);
420 if (err)
421 goto free_map_sec;
422
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700423 err = bpf_map_alloc_id(map);
424 if (err)
425 goto free_map;
426
Chenbo Feng6e71b042017-10-18 13:00:22 -0700427 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700428 if (err < 0) {
429 /* failed to allocate fd.
430 * bpf_map_put() is needed because the above
431 * bpf_map_alloc_id() has published the map
432 * to the userspace and the userspace may
433 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
434 */
435 bpf_map_put(map);
436 return err;
437 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700438
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100439 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700440 return err;
441
442free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100443 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700444free_map_sec:
445 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100446free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700447 map->ops->map_free(map);
448 return err;
449}
450
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700451/* if error is returned, fd is released.
452 * On success caller should complete fd access with matching fdput()
453 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100454struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700455{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700456 if (!f.file)
457 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700458 if (f.file->f_op != &bpf_map_fops) {
459 fdput(f);
460 return ERR_PTR(-EINVAL);
461 }
462
Daniel Borkmannc2101292015-10-29 14:58:07 +0100463 return f.file->private_data;
464}
465
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700466/* prog's and map's refcnt limit */
467#define BPF_MAX_REFCNT 32768
468
469struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100470{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700471 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
472 atomic_dec(&map->refcnt);
473 return ERR_PTR(-EBUSY);
474 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100475 if (uref)
476 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700477 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100478}
479
480struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100481{
482 struct fd f = fdget(ufd);
483 struct bpf_map *map;
484
485 map = __bpf_map_get(f);
486 if (IS_ERR(map))
487 return map;
488
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700489 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100490 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700491
492 return map;
493}
494
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700495/* map_idr_lock should have been held */
496static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
497 bool uref)
498{
499 int refold;
500
501 refold = __atomic_add_unless(&map->refcnt, 1, 0);
502
503 if (refold >= BPF_MAX_REFCNT) {
504 __bpf_map_put(map, false);
505 return ERR_PTR(-EBUSY);
506 }
507
508 if (!refold)
509 return ERR_PTR(-ENOENT);
510
511 if (uref)
512 atomic_inc(&map->usercnt);
513
514 return map;
515}
516
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800517int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
518{
519 return -ENOTSUPP;
520}
521
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700522/* last field in 'union bpf_attr' used by this command */
523#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
524
525static int map_lookup_elem(union bpf_attr *attr)
526{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100527 void __user *ukey = u64_to_user_ptr(attr->key);
528 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700529 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700530 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800531 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800532 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200533 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700534 int err;
535
536 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
537 return -EINVAL;
538
Daniel Borkmann592867b2015-09-08 18:00:09 +0200539 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100540 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700541 if (IS_ERR(map))
542 return PTR_ERR(map);
543
Chenbo Feng6e71b042017-10-18 13:00:22 -0700544 if (!(f.file->f_mode & FMODE_CAN_READ)) {
545 err = -EPERM;
546 goto err_put;
547 }
548
Al Viroe4448ed2017-05-13 18:43:00 -0400549 key = memdup_user(ukey, map->key_size);
550 if (IS_ERR(key)) {
551 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700552 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400553 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700554
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800555 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800556 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800557 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
558 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700559 else if (IS_FD_MAP(map))
560 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800561 else
562 value_size = map->value_size;
563
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800564 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800565 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700566 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800567 goto free_key;
568
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800569 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
570 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800571 err = bpf_percpu_hash_copy(map, key, value);
572 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
573 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800574 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
575 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700576 } else if (IS_FD_ARRAY(map)) {
577 err = bpf_fd_array_map_lookup_elem(map, key, value);
578 } else if (IS_FD_HASH(map)) {
579 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800580 } else {
581 rcu_read_lock();
582 ptr = map->ops->map_lookup_elem(map, key);
583 if (ptr)
584 memcpy(value, ptr, value_size);
585 rcu_read_unlock();
586 err = ptr ? 0 : -ENOENT;
587 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800588
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800589 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800590 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700591
592 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800593 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800594 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700595
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100596 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700597 err = 0;
598
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800599free_value:
600 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700601free_key:
602 kfree(key);
603err_put:
604 fdput(f);
605 return err;
606}
607
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800608#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700609
610static int map_update_elem(union bpf_attr *attr)
611{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100612 void __user *ukey = u64_to_user_ptr(attr->key);
613 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700614 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700615 struct bpf_map *map;
616 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800617 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200618 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700619 int err;
620
621 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
622 return -EINVAL;
623
Daniel Borkmann592867b2015-09-08 18:00:09 +0200624 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100625 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700626 if (IS_ERR(map))
627 return PTR_ERR(map);
628
Chenbo Feng6e71b042017-10-18 13:00:22 -0700629 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
630 err = -EPERM;
631 goto err_put;
632 }
633
Al Viroe4448ed2017-05-13 18:43:00 -0400634 key = memdup_user(ukey, map->key_size);
635 if (IS_ERR(key)) {
636 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700637 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400638 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700639
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800640 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800641 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800642 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
643 value_size = round_up(map->value_size, 8) * num_possible_cpus();
644 else
645 value_size = map->value_size;
646
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700647 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800648 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700649 if (!value)
650 goto free_key;
651
652 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800653 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700654 goto free_value;
655
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200656 /* Need to create a kthread, thus must support schedule */
657 if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
658 err = map->ops->map_update_elem(map, key, value, attr->flags);
659 goto out;
660 }
661
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800662 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
663 * inside bpf map update or delete otherwise deadlocks are possible
664 */
665 preempt_disable();
666 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800667 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
668 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800669 err = bpf_percpu_hash_update(map, key, value, attr->flags);
670 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
671 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200672 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700673 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700674 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
675 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200676 rcu_read_lock();
677 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
678 attr->flags);
679 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700680 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
681 rcu_read_lock();
682 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
683 attr->flags);
684 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800685 } else {
686 rcu_read_lock();
687 err = map->ops->map_update_elem(map, key, value, attr->flags);
688 rcu_read_unlock();
689 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800690 __this_cpu_dec(bpf_prog_active);
691 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200692out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100693 if (!err)
694 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700695free_value:
696 kfree(value);
697free_key:
698 kfree(key);
699err_put:
700 fdput(f);
701 return err;
702}
703
704#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
705
706static int map_delete_elem(union bpf_attr *attr)
707{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100708 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700709 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700710 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200711 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700712 void *key;
713 int err;
714
715 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
716 return -EINVAL;
717
Daniel Borkmann592867b2015-09-08 18:00:09 +0200718 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100719 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700720 if (IS_ERR(map))
721 return PTR_ERR(map);
722
Chenbo Feng6e71b042017-10-18 13:00:22 -0700723 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
724 err = -EPERM;
725 goto err_put;
726 }
727
Al Viroe4448ed2017-05-13 18:43:00 -0400728 key = memdup_user(ukey, map->key_size);
729 if (IS_ERR(key)) {
730 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700731 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400732 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700733
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800734 preempt_disable();
735 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700736 rcu_read_lock();
737 err = map->ops->map_delete_elem(map, key);
738 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800739 __this_cpu_dec(bpf_prog_active);
740 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700741
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100742 if (!err)
743 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700744 kfree(key);
745err_put:
746 fdput(f);
747 return err;
748}
749
750/* last field in 'union bpf_attr' used by this command */
751#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
752
753static int map_get_next_key(union bpf_attr *attr)
754{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100755 void __user *ukey = u64_to_user_ptr(attr->key);
756 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700757 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700758 struct bpf_map *map;
759 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200760 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700761 int err;
762
763 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
764 return -EINVAL;
765
Daniel Borkmann592867b2015-09-08 18:00:09 +0200766 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100767 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700768 if (IS_ERR(map))
769 return PTR_ERR(map);
770
Chenbo Feng6e71b042017-10-18 13:00:22 -0700771 if (!(f.file->f_mode & FMODE_CAN_READ)) {
772 err = -EPERM;
773 goto err_put;
774 }
775
Teng Qin8fe45922017-04-24 19:00:37 -0700776 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400777 key = memdup_user(ukey, map->key_size);
778 if (IS_ERR(key)) {
779 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700780 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400781 }
Teng Qin8fe45922017-04-24 19:00:37 -0700782 } else {
783 key = NULL;
784 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700785
786 err = -ENOMEM;
787 next_key = kmalloc(map->key_size, GFP_USER);
788 if (!next_key)
789 goto free_key;
790
791 rcu_read_lock();
792 err = map->ops->map_get_next_key(map, key, next_key);
793 rcu_read_unlock();
794 if (err)
795 goto free_next_key;
796
797 err = -EFAULT;
798 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
799 goto free_next_key;
800
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100801 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700802 err = 0;
803
804free_next_key:
805 kfree(next_key);
806free_key:
807 kfree(key);
808err_put:
809 fdput(f);
810 return err;
811}
812
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700813static const struct bpf_prog_ops * const bpf_prog_types[] = {
814#define BPF_PROG_TYPE(_id, _name) \
815 [_id] = & _name ## _prog_ops,
816#define BPF_MAP_TYPE(_id, _ops)
817#include <linux/bpf_types.h>
818#undef BPF_PROG_TYPE
819#undef BPF_MAP_TYPE
820};
821
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700822static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
823{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200824 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
825 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700826
Johannes Bergbe9370a2017-04-11 15:34:57 +0200827 prog->aux->ops = bpf_prog_types[type];
828 prog->type = type;
829 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700830}
831
832/* drop refcnt on maps used by eBPF program and free auxilary data */
833static void free_used_maps(struct bpf_prog_aux *aux)
834{
835 int i;
836
837 for (i = 0; i < aux->used_map_cnt; i++)
838 bpf_map_put(aux->used_maps[i]);
839
840 kfree(aux->used_maps);
841}
842
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100843int __bpf_prog_charge(struct user_struct *user, u32 pages)
844{
845 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
846 unsigned long user_bufs;
847
848 if (user) {
849 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
850 if (user_bufs > memlock_limit) {
851 atomic_long_sub(pages, &user->locked_vm);
852 return -EPERM;
853 }
854 }
855
856 return 0;
857}
858
859void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
860{
861 if (user)
862 atomic_long_sub(pages, &user->locked_vm);
863}
864
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700865static int bpf_prog_charge_memlock(struct bpf_prog *prog)
866{
867 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100868 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700869
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100870 ret = __bpf_prog_charge(user, prog->pages);
871 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700872 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100873 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700874 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100875
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700876 prog->aux->user = user;
877 return 0;
878}
879
880static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
881{
882 struct user_struct *user = prog->aux->user;
883
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100884 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700885 free_uid(user);
886}
887
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700888static int bpf_prog_alloc_id(struct bpf_prog *prog)
889{
890 int id;
891
892 spin_lock_bh(&prog_idr_lock);
893 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
894 if (id > 0)
895 prog->aux->id = id;
896 spin_unlock_bh(&prog_idr_lock);
897
898 /* id is in [1, INT_MAX) */
899 if (WARN_ON_ONCE(!id))
900 return -ENOSPC;
901
902 return id > 0 ? 0 : id;
903}
904
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700905static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700906{
907 /* cBPF to eBPF migrations are currently not in the idr store. */
908 if (!prog->aux->id)
909 return;
910
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700911 if (do_idr_lock)
912 spin_lock_bh(&prog_idr_lock);
913 else
914 __acquire(&prog_idr_lock);
915
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700916 idr_remove(&prog_idr, prog->aux->id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700917
918 if (do_idr_lock)
919 spin_unlock_bh(&prog_idr_lock);
920 else
921 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700922}
923
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200924static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700925{
926 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
927
928 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700929 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700930 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700931 bpf_prog_free(aux->prog);
932}
933
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700934static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700935{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100936 if (atomic_dec_and_test(&prog->aux->refcnt)) {
937 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700938 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700939 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100940 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200941 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100942 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700943}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700944
945void bpf_prog_put(struct bpf_prog *prog)
946{
947 __bpf_prog_put(prog, true);
948}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100949EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700950
951static int bpf_prog_release(struct inode *inode, struct file *filp)
952{
953 struct bpf_prog *prog = filp->private_data;
954
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200955 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700956 return 0;
957}
958
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100959#ifdef CONFIG_PROC_FS
960static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
961{
962 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100963 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100964
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100965 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100966 seq_printf(m,
967 "prog_type:\t%u\n"
968 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100969 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100970 "memlock:\t%llu\n",
971 prog->type,
972 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100973 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100974 prog->pages * 1ULL << PAGE_SHIFT);
975}
976#endif
977
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700978static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100979#ifdef CONFIG_PROC_FS
980 .show_fdinfo = bpf_prog_show_fdinfo,
981#endif
982 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700983 .read = bpf_dummy_read,
984 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700985};
986
Daniel Borkmannb2197752015-10-29 14:58:09 +0100987int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100988{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700989 int ret;
990
991 ret = security_bpf_prog(prog);
992 if (ret < 0)
993 return ret;
994
Daniel Borkmannaa797812015-10-29 14:58:06 +0100995 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
996 O_RDWR | O_CLOEXEC);
997}
998
Daniel Borkmann113214b2016-06-30 17:24:44 +0200999static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001000{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001001 if (!f.file)
1002 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001003 if (f.file->f_op != &bpf_prog_fops) {
1004 fdput(f);
1005 return ERR_PTR(-EINVAL);
1006 }
1007
Daniel Borkmannc2101292015-10-29 14:58:07 +01001008 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001009}
1010
Brenden Blanco59d36562016-07-19 12:16:46 -07001011struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001012{
Brenden Blanco59d36562016-07-19 12:16:46 -07001013 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1014 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001015 return ERR_PTR(-EBUSY);
1016 }
1017 return prog;
1018}
Brenden Blanco59d36562016-07-19 12:16:46 -07001019EXPORT_SYMBOL_GPL(bpf_prog_add);
1020
Daniel Borkmannc5405942016-11-09 22:02:34 +01001021void bpf_prog_sub(struct bpf_prog *prog, int i)
1022{
1023 /* Only to be used for undoing previous bpf_prog_add() in some
1024 * error path. We still know that another entity in our call
1025 * path holds a reference to the program, thus atomic_sub() can
1026 * be safely used in such cases!
1027 */
1028 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1029}
1030EXPORT_SYMBOL_GPL(bpf_prog_sub);
1031
Brenden Blanco59d36562016-07-19 12:16:46 -07001032struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1033{
1034 return bpf_prog_add(prog, 1);
1035}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001036EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001037
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001038/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001039struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001040{
1041 int refold;
1042
1043 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1044
1045 if (refold >= BPF_MAX_REFCNT) {
1046 __bpf_prog_put(prog, false);
1047 return ERR_PTR(-EBUSY);
1048 }
1049
1050 if (!refold)
1051 return ERR_PTR(-ENOENT);
1052
1053 return prog;
1054}
John Fastabenda6f6df62017-08-15 22:32:22 -07001055EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001056
Daniel Borkmann113214b2016-06-30 17:24:44 +02001057static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001058{
1059 struct fd f = fdget(ufd);
1060 struct bpf_prog *prog;
1061
Daniel Borkmann113214b2016-06-30 17:24:44 +02001062 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001063 if (IS_ERR(prog))
1064 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +02001065 if (type && prog->type != *type) {
1066 prog = ERR_PTR(-EINVAL);
1067 goto out;
1068 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001069
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001070 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001071out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001072 fdput(f);
1073 return prog;
1074}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001075
1076struct bpf_prog *bpf_prog_get(u32 ufd)
1077{
1078 return __bpf_prog_get(ufd, NULL);
1079}
1080
1081struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
1082{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001083 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
1084
1085 if (!IS_ERR(prog))
1086 trace_bpf_prog_get_type(prog);
1087 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +02001088}
1089EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001090
1091/* last field in 'union bpf_attr' used by this command */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001092#define BPF_PROG_LOAD_LAST_FIELD prog_name
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001093
1094static int bpf_prog_load(union bpf_attr *attr)
1095{
1096 enum bpf_prog_type type = attr->prog_type;
1097 struct bpf_prog *prog;
1098 int err;
1099 char license[128];
1100 bool is_gpl;
1101
1102 if (CHECK_ATTR(BPF_PROG_LOAD))
1103 return -EINVAL;
1104
David S. Millere07b98d2017-05-10 11:38:07 -07001105 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1106 return -EINVAL;
1107
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001108 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001109 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001110 sizeof(license) - 1) < 0)
1111 return -EFAULT;
1112 license[sizeof(license) - 1] = 0;
1113
1114 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1115 is_gpl = license_is_gpl_compatible(license);
1116
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001117 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1118 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001119
Alexei Starovoitov25415172015-03-25 12:49:20 -07001120 if (type == BPF_PROG_TYPE_KPROBE &&
1121 attr->kern_version != LINUX_VERSION_CODE)
1122 return -EINVAL;
1123
Chenbo Feng80b7d812017-05-31 18:16:00 -07001124 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1125 type != BPF_PROG_TYPE_CGROUP_SKB &&
1126 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001127 return -EPERM;
1128
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001129 /* plain bpf_prog allocation */
1130 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1131 if (!prog)
1132 return -ENOMEM;
1133
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001134 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001135 if (err)
1136 goto free_prog_nouncharge;
1137
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001138 err = bpf_prog_charge_memlock(prog);
1139 if (err)
1140 goto free_prog_sec;
1141
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001142 prog->len = attr->insn_cnt;
1143
1144 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001145 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001146 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001147 goto free_prog;
1148
1149 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001150 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001151
1152 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001153 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001154
1155 /* find program type: socket_filter vs tracing_filter */
1156 err = find_prog_type(type, prog);
1157 if (err < 0)
1158 goto free_prog;
1159
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001160 prog->aux->load_time = ktime_get_boot_ns();
1161 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1162 if (err)
1163 goto free_prog;
1164
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001165 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001166 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001167 if (err < 0)
1168 goto free_used_maps;
1169
1170 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001171 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001172 if (err < 0)
1173 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001174
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001175 err = bpf_prog_alloc_id(prog);
1176 if (err)
1177 goto free_used_maps;
1178
Daniel Borkmannaa797812015-10-29 14:58:06 +01001179 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001180 if (err < 0) {
1181 /* failed to allocate fd.
1182 * bpf_prog_put() is needed because the above
1183 * bpf_prog_alloc_id() has published the prog
1184 * to the userspace and the userspace may
1185 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1186 */
1187 bpf_prog_put(prog);
1188 return err;
1189 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001190
Daniel Borkmann74451e662017-02-16 22:24:50 +01001191 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001192 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001193 return err;
1194
1195free_used_maps:
1196 free_used_maps(prog->aux);
1197free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001198 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001199free_prog_sec:
1200 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001201free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001202 bpf_prog_free(prog);
1203 return err;
1204}
1205
Chenbo Feng6e71b042017-10-18 13:00:22 -07001206#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001207
1208static int bpf_obj_pin(const union bpf_attr *attr)
1209{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001210 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001211 return -EINVAL;
1212
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001213 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001214}
1215
1216static int bpf_obj_get(const union bpf_attr *attr)
1217{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001218 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1219 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001220 return -EINVAL;
1221
Chenbo Feng6e71b042017-10-18 13:00:22 -07001222 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1223 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001224}
1225
Daniel Mackf4324552016-11-23 16:52:27 +01001226#ifdef CONFIG_CGROUP_BPF
1227
John Fastabend464bc0f2017-08-28 07:10:04 -07001228#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001229
John Fastabend5a67da22017-09-08 14:00:49 -07001230static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001231{
John Fastabend5a67da22017-09-08 14:00:49 -07001232 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001233 int ufd = attr->target_fd;
1234 struct bpf_map *map;
1235 struct fd f;
1236 int err;
1237
1238 f = fdget(ufd);
1239 map = __bpf_map_get(f);
1240 if (IS_ERR(map))
1241 return PTR_ERR(map);
1242
John Fastabend5a67da22017-09-08 14:00:49 -07001243 if (attach) {
1244 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1245 BPF_PROG_TYPE_SK_SKB);
1246 if (IS_ERR(prog)) {
1247 fdput(f);
1248 return PTR_ERR(prog);
1249 }
John Fastabend174a79f2017-08-15 22:32:47 -07001250 }
1251
John Fastabend5a67da22017-09-08 14:00:49 -07001252 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001253 if (err) {
1254 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001255 if (prog)
1256 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001257 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001258 }
1259
1260 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001261 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001262}
Daniel Mackf4324552016-11-23 16:52:27 +01001263
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001264#define BPF_F_ATTACH_MASK \
1265 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1266
Daniel Mackf4324552016-11-23 16:52:27 +01001267static int bpf_prog_attach(const union bpf_attr *attr)
1268{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001269 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001270 struct bpf_prog *prog;
1271 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001272 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001273
1274 if (!capable(CAP_NET_ADMIN))
1275 return -EPERM;
1276
1277 if (CHECK_ATTR(BPF_PROG_ATTACH))
1278 return -EINVAL;
1279
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001280 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001281 return -EINVAL;
1282
Daniel Mackf4324552016-11-23 16:52:27 +01001283 switch (attr->attach_type) {
1284 case BPF_CGROUP_INET_INGRESS:
1285 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001286 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001287 break;
David Ahern610236582016-12-01 08:48:04 -08001288 case BPF_CGROUP_INET_SOCK_CREATE:
1289 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1290 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001291 case BPF_CGROUP_SOCK_OPS:
1292 ptype = BPF_PROG_TYPE_SOCK_OPS;
1293 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001294 case BPF_SK_SKB_STREAM_PARSER:
1295 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend5a67da22017-09-08 14:00:49 -07001296 return sockmap_get_from_fd(attr, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001297 default:
1298 return -EINVAL;
1299 }
1300
David Ahernb2cd1252016-12-01 08:48:03 -08001301 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1302 if (IS_ERR(prog))
1303 return PTR_ERR(prog);
1304
1305 cgrp = cgroup_get_from_fd(attr->target_fd);
1306 if (IS_ERR(cgrp)) {
1307 bpf_prog_put(prog);
1308 return PTR_ERR(cgrp);
1309 }
1310
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001311 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1312 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001313 if (ret)
1314 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001315 cgroup_put(cgrp);
1316
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001317 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001318}
1319
1320#define BPF_PROG_DETACH_LAST_FIELD attach_type
1321
1322static int bpf_prog_detach(const union bpf_attr *attr)
1323{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001324 enum bpf_prog_type ptype;
1325 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001326 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001327 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001328
1329 if (!capable(CAP_NET_ADMIN))
1330 return -EPERM;
1331
1332 if (CHECK_ATTR(BPF_PROG_DETACH))
1333 return -EINVAL;
1334
1335 switch (attr->attach_type) {
1336 case BPF_CGROUP_INET_INGRESS:
1337 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001338 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1339 break;
David Ahern610236582016-12-01 08:48:04 -08001340 case BPF_CGROUP_INET_SOCK_CREATE:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001341 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1342 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001343 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001344 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001345 break;
John Fastabend5a67da22017-09-08 14:00:49 -07001346 case BPF_SK_SKB_STREAM_PARSER:
1347 case BPF_SK_SKB_STREAM_VERDICT:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001348 return sockmap_get_from_fd(attr, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001349 default:
1350 return -EINVAL;
1351 }
1352
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001353 cgrp = cgroup_get_from_fd(attr->target_fd);
1354 if (IS_ERR(cgrp))
1355 return PTR_ERR(cgrp);
1356
1357 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1358 if (IS_ERR(prog))
1359 prog = NULL;
1360
1361 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1362 if (prog)
1363 bpf_prog_put(prog);
1364 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001365 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001366}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001367
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001368#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1369
1370static int bpf_prog_query(const union bpf_attr *attr,
1371 union bpf_attr __user *uattr)
1372{
1373 struct cgroup *cgrp;
1374 int ret;
1375
1376 if (!capable(CAP_NET_ADMIN))
1377 return -EPERM;
1378 if (CHECK_ATTR(BPF_PROG_QUERY))
1379 return -EINVAL;
1380 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1381 return -EINVAL;
1382
1383 switch (attr->query.attach_type) {
1384 case BPF_CGROUP_INET_INGRESS:
1385 case BPF_CGROUP_INET_EGRESS:
1386 case BPF_CGROUP_INET_SOCK_CREATE:
1387 case BPF_CGROUP_SOCK_OPS:
1388 break;
1389 default:
1390 return -EINVAL;
1391 }
1392 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1393 if (IS_ERR(cgrp))
1394 return PTR_ERR(cgrp);
1395 ret = cgroup_bpf_query(cgrp, attr, uattr);
1396 cgroup_put(cgrp);
1397 return ret;
1398}
Daniel Mackf4324552016-11-23 16:52:27 +01001399#endif /* CONFIG_CGROUP_BPF */
1400
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001401#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1402
1403static int bpf_prog_test_run(const union bpf_attr *attr,
1404 union bpf_attr __user *uattr)
1405{
1406 struct bpf_prog *prog;
1407 int ret = -ENOTSUPP;
1408
1409 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1410 return -EINVAL;
1411
1412 prog = bpf_prog_get(attr->test.prog_fd);
1413 if (IS_ERR(prog))
1414 return PTR_ERR(prog);
1415
1416 if (prog->aux->ops->test_run)
1417 ret = prog->aux->ops->test_run(prog, attr, uattr);
1418
1419 bpf_prog_put(prog);
1420 return ret;
1421}
1422
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001423#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1424
1425static int bpf_obj_get_next_id(const union bpf_attr *attr,
1426 union bpf_attr __user *uattr,
1427 struct idr *idr,
1428 spinlock_t *lock)
1429{
1430 u32 next_id = attr->start_id;
1431 int err = 0;
1432
1433 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1434 return -EINVAL;
1435
1436 if (!capable(CAP_SYS_ADMIN))
1437 return -EPERM;
1438
1439 next_id++;
1440 spin_lock_bh(lock);
1441 if (!idr_get_next(idr, &next_id))
1442 err = -ENOENT;
1443 spin_unlock_bh(lock);
1444
1445 if (!err)
1446 err = put_user(next_id, &uattr->next_id);
1447
1448 return err;
1449}
1450
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001451#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1452
1453static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1454{
1455 struct bpf_prog *prog;
1456 u32 id = attr->prog_id;
1457 int fd;
1458
1459 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1460 return -EINVAL;
1461
1462 if (!capable(CAP_SYS_ADMIN))
1463 return -EPERM;
1464
1465 spin_lock_bh(&prog_idr_lock);
1466 prog = idr_find(&prog_idr, id);
1467 if (prog)
1468 prog = bpf_prog_inc_not_zero(prog);
1469 else
1470 prog = ERR_PTR(-ENOENT);
1471 spin_unlock_bh(&prog_idr_lock);
1472
1473 if (IS_ERR(prog))
1474 return PTR_ERR(prog);
1475
1476 fd = bpf_prog_new_fd(prog);
1477 if (fd < 0)
1478 bpf_prog_put(prog);
1479
1480 return fd;
1481}
1482
Chenbo Feng6e71b042017-10-18 13:00:22 -07001483#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001484
1485static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1486{
1487 struct bpf_map *map;
1488 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001489 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001490 int fd;
1491
Chenbo Feng6e71b042017-10-18 13:00:22 -07001492 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1493 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001494 return -EINVAL;
1495
1496 if (!capable(CAP_SYS_ADMIN))
1497 return -EPERM;
1498
Chenbo Feng6e71b042017-10-18 13:00:22 -07001499 f_flags = bpf_get_file_flag(attr->open_flags);
1500 if (f_flags < 0)
1501 return f_flags;
1502
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001503 spin_lock_bh(&map_idr_lock);
1504 map = idr_find(&map_idr, id);
1505 if (map)
1506 map = bpf_map_inc_not_zero(map, true);
1507 else
1508 map = ERR_PTR(-ENOENT);
1509 spin_unlock_bh(&map_idr_lock);
1510
1511 if (IS_ERR(map))
1512 return PTR_ERR(map);
1513
Chenbo Feng6e71b042017-10-18 13:00:22 -07001514 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001515 if (fd < 0)
1516 bpf_map_put(map);
1517
1518 return fd;
1519}
1520
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001521static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1522 const union bpf_attr *attr,
1523 union bpf_attr __user *uattr)
1524{
1525 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1526 struct bpf_prog_info info = {};
1527 u32 info_len = attr->info.info_len;
1528 char __user *uinsns;
1529 u32 ulen;
1530 int err;
1531
1532 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1533 if (err)
1534 return err;
1535 info_len = min_t(u32, sizeof(info), info_len);
1536
1537 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001538 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001539
1540 info.type = prog->type;
1541 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001542 info.load_time = prog->aux->load_time;
1543 info.created_by_uid = from_kuid_munged(current_user_ns(),
1544 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001545
1546 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001547 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1548
1549 ulen = info.nr_map_ids;
1550 info.nr_map_ids = prog->aux->used_map_cnt;
1551 ulen = min_t(u32, info.nr_map_ids, ulen);
1552 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001553 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001554 u32 i;
1555
1556 for (i = 0; i < ulen; i++)
1557 if (put_user(prog->aux->used_maps[i]->id,
1558 &user_map_ids[i]))
1559 return -EFAULT;
1560 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001561
1562 if (!capable(CAP_SYS_ADMIN)) {
1563 info.jited_prog_len = 0;
1564 info.xlated_prog_len = 0;
1565 goto done;
1566 }
1567
1568 ulen = info.jited_prog_len;
1569 info.jited_prog_len = prog->jited_len;
1570 if (info.jited_prog_len && ulen) {
1571 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1572 ulen = min_t(u32, info.jited_prog_len, ulen);
1573 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1574 return -EFAULT;
1575 }
1576
1577 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001578 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001579 if (info.xlated_prog_len && ulen) {
1580 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1581 ulen = min_t(u32, info.xlated_prog_len, ulen);
1582 if (copy_to_user(uinsns, prog->insnsi, ulen))
1583 return -EFAULT;
1584 }
1585
1586done:
1587 if (copy_to_user(uinfo, &info, info_len) ||
1588 put_user(info_len, &uattr->info.info_len))
1589 return -EFAULT;
1590
1591 return 0;
1592}
1593
1594static int bpf_map_get_info_by_fd(struct bpf_map *map,
1595 const union bpf_attr *attr,
1596 union bpf_attr __user *uattr)
1597{
1598 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1599 struct bpf_map_info info = {};
1600 u32 info_len = attr->info.info_len;
1601 int err;
1602
1603 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1604 if (err)
1605 return err;
1606 info_len = min_t(u32, sizeof(info), info_len);
1607
1608 info.type = map->map_type;
1609 info.id = map->id;
1610 info.key_size = map->key_size;
1611 info.value_size = map->value_size;
1612 info.max_entries = map->max_entries;
1613 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07001614 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001615
1616 if (copy_to_user(uinfo, &info, info_len) ||
1617 put_user(info_len, &uattr->info.info_len))
1618 return -EFAULT;
1619
1620 return 0;
1621}
1622
1623#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1624
1625static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1626 union bpf_attr __user *uattr)
1627{
1628 int ufd = attr->info.bpf_fd;
1629 struct fd f;
1630 int err;
1631
1632 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1633 return -EINVAL;
1634
1635 f = fdget(ufd);
1636 if (!f.file)
1637 return -EBADFD;
1638
1639 if (f.file->f_op == &bpf_prog_fops)
1640 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1641 uattr);
1642 else if (f.file->f_op == &bpf_map_fops)
1643 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1644 uattr);
1645 else
1646 err = -EINVAL;
1647
1648 fdput(f);
1649 return err;
1650}
1651
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001652SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1653{
1654 union bpf_attr attr = {};
1655 int err;
1656
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001657 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001658 return -EPERM;
1659
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001660 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1661 if (err)
1662 return err;
1663 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001664
1665 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1666 if (copy_from_user(&attr, uattr, size) != 0)
1667 return -EFAULT;
1668
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001669 err = security_bpf(cmd, &attr, size);
1670 if (err < 0)
1671 return err;
1672
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001673 switch (cmd) {
1674 case BPF_MAP_CREATE:
1675 err = map_create(&attr);
1676 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001677 case BPF_MAP_LOOKUP_ELEM:
1678 err = map_lookup_elem(&attr);
1679 break;
1680 case BPF_MAP_UPDATE_ELEM:
1681 err = map_update_elem(&attr);
1682 break;
1683 case BPF_MAP_DELETE_ELEM:
1684 err = map_delete_elem(&attr);
1685 break;
1686 case BPF_MAP_GET_NEXT_KEY:
1687 err = map_get_next_key(&attr);
1688 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001689 case BPF_PROG_LOAD:
1690 err = bpf_prog_load(&attr);
1691 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001692 case BPF_OBJ_PIN:
1693 err = bpf_obj_pin(&attr);
1694 break;
1695 case BPF_OBJ_GET:
1696 err = bpf_obj_get(&attr);
1697 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001698#ifdef CONFIG_CGROUP_BPF
1699 case BPF_PROG_ATTACH:
1700 err = bpf_prog_attach(&attr);
1701 break;
1702 case BPF_PROG_DETACH:
1703 err = bpf_prog_detach(&attr);
1704 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001705 case BPF_PROG_QUERY:
1706 err = bpf_prog_query(&attr, uattr);
1707 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001708#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001709 case BPF_PROG_TEST_RUN:
1710 err = bpf_prog_test_run(&attr, uattr);
1711 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001712 case BPF_PROG_GET_NEXT_ID:
1713 err = bpf_obj_get_next_id(&attr, uattr,
1714 &prog_idr, &prog_idr_lock);
1715 break;
1716 case BPF_MAP_GET_NEXT_ID:
1717 err = bpf_obj_get_next_id(&attr, uattr,
1718 &map_idr, &map_idr_lock);
1719 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001720 case BPF_PROG_GET_FD_BY_ID:
1721 err = bpf_prog_get_fd_by_id(&attr);
1722 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001723 case BPF_MAP_GET_FD_BY_ID:
1724 err = bpf_map_get_fd_by_id(&attr);
1725 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001726 case BPF_OBJ_GET_INFO_BY_FD:
1727 err = bpf_obj_get_info_by_fd(&attr, uattr);
1728 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001729 default:
1730 err = -EINVAL;
1731 break;
1732 }
1733
1734 return err;
1735}