blob: ea55691cbf5eb096aa324ab075c1ad014f389033 [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>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070025
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080026DEFINE_PER_CPU(int, bpf_prog_active);
27
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070028int sysctl_unprivileged_bpf_disabled __read_mostly;
29
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070030static LIST_HEAD(bpf_map_types);
31
32static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
33{
34 struct bpf_map_type_list *tl;
35 struct bpf_map *map;
36
37 list_for_each_entry(tl, &bpf_map_types, list_node) {
38 if (tl->type == attr->map_type) {
39 map = tl->ops->map_alloc(attr);
40 if (IS_ERR(map))
41 return map;
42 map->ops = tl->ops;
43 map->map_type = attr->map_type;
44 return map;
45 }
46 }
47 return ERR_PTR(-EINVAL);
48}
49
50/* boot time registration of different map implementations */
51void bpf_register_map_type(struct bpf_map_type_list *tl)
52{
53 list_add(&tl->list_node, &bpf_map_types);
54}
55
Daniel Borkmannd407bd22017-01-18 15:14:17 +010056void *bpf_map_area_alloc(size_t size)
57{
58 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
59 * trigger under memory pressure as we really just want to
60 * fail instead.
61 */
62 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
63 void *area;
64
65 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
66 area = kmalloc(size, GFP_USER | flags);
67 if (area != NULL)
68 return area;
69 }
70
71 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | flags,
72 PAGE_KERNEL);
73}
74
75void bpf_map_area_free(void *area)
76{
77 kvfree(area);
78}
79
Alexei Starovoitov6c905982016-03-07 21:57:15 -080080int bpf_map_precharge_memlock(u32 pages)
81{
82 struct user_struct *user = get_current_user();
83 unsigned long memlock_limit, cur;
84
85 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
86 cur = atomic_long_read(&user->locked_vm);
87 free_uid(user);
88 if (cur + pages > memlock_limit)
89 return -EPERM;
90 return 0;
91}
92
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070093static int bpf_map_charge_memlock(struct bpf_map *map)
94{
95 struct user_struct *user = get_current_user();
96 unsigned long memlock_limit;
97
98 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
99
100 atomic_long_add(map->pages, &user->locked_vm);
101
102 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
103 atomic_long_sub(map->pages, &user->locked_vm);
104 free_uid(user);
105 return -EPERM;
106 }
107 map->user = user;
108 return 0;
109}
110
111static void bpf_map_uncharge_memlock(struct bpf_map *map)
112{
113 struct user_struct *user = map->user;
114
115 atomic_long_sub(map->pages, &user->locked_vm);
116 free_uid(user);
117}
118
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700119/* called from workqueue */
120static void bpf_map_free_deferred(struct work_struct *work)
121{
122 struct bpf_map *map = container_of(work, struct bpf_map, work);
123
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700124 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700125 /* implementation dependent freeing */
126 map->ops->map_free(map);
127}
128
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100129static void bpf_map_put_uref(struct bpf_map *map)
130{
131 if (atomic_dec_and_test(&map->usercnt)) {
132 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
133 bpf_fd_array_map_clear(map);
134 }
135}
136
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700137/* decrement map refcnt and schedule it for freeing via workqueue
138 * (unrelying map implementation ops->map_free() might sleep)
139 */
140void bpf_map_put(struct bpf_map *map)
141{
142 if (atomic_dec_and_test(&map->refcnt)) {
143 INIT_WORK(&map->work, bpf_map_free_deferred);
144 schedule_work(&map->work);
145 }
146}
147
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100148void bpf_map_put_with_uref(struct bpf_map *map)
149{
150 bpf_map_put_uref(map);
151 bpf_map_put(map);
152}
153
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700154static int bpf_map_release(struct inode *inode, struct file *filp)
155{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200156 struct bpf_map *map = filp->private_data;
157
158 if (map->ops->map_release)
159 map->ops->map_release(map, filp);
160
161 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700162 return 0;
163}
164
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100165#ifdef CONFIG_PROC_FS
166static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
167{
168 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100169 const struct bpf_array *array;
170 u32 owner_prog_type = 0;
171
172 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
173 array = container_of(map, struct bpf_array, map);
174 owner_prog_type = array->owner_prog_type;
175 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100176
177 seq_printf(m,
178 "map_type:\t%u\n"
179 "key_size:\t%u\n"
180 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100181 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100182 "map_flags:\t%#x\n"
183 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100184 map->map_type,
185 map->key_size,
186 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100187 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100188 map->map_flags,
189 map->pages * 1ULL << PAGE_SHIFT);
190
191 if (owner_prog_type)
192 seq_printf(m, "owner_prog_type:\t%u\n",
193 owner_prog_type);
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100194}
195#endif
196
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700197static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100198#ifdef CONFIG_PROC_FS
199 .show_fdinfo = bpf_map_show_fdinfo,
200#endif
201 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700202};
203
Daniel Borkmannb2197752015-10-29 14:58:09 +0100204int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100205{
206 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
207 O_RDWR | O_CLOEXEC);
208}
209
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700210/* helper macro to check that unused fields 'union bpf_attr' are zero */
211#define CHECK_ATTR(CMD) \
212 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
213 sizeof(attr->CMD##_LAST_FIELD), 0, \
214 sizeof(*attr) - \
215 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
216 sizeof(attr->CMD##_LAST_FIELD)) != NULL
217
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700218#define BPF_MAP_CREATE_LAST_FIELD inner_map_fd
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700219/* called via syscall */
220static int map_create(union bpf_attr *attr)
221{
222 struct bpf_map *map;
223 int err;
224
225 err = CHECK_ATTR(BPF_MAP_CREATE);
226 if (err)
227 return -EINVAL;
228
229 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
230 map = find_and_alloc_map(attr);
231 if (IS_ERR(map))
232 return PTR_ERR(map);
233
234 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100235 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700236
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700237 err = bpf_map_charge_memlock(map);
238 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100239 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700240
Daniel Borkmannaa797812015-10-29 14:58:06 +0100241 err = bpf_map_new_fd(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700242 if (err < 0)
243 /* failed to allocate fd */
244 goto free_map;
245
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100246 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700247 return err;
248
249free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100250 bpf_map_uncharge_memlock(map);
251free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700252 map->ops->map_free(map);
253 return err;
254}
255
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700256/* if error is returned, fd is released.
257 * On success caller should complete fd access with matching fdput()
258 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100259struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700260{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700261 if (!f.file)
262 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700263 if (f.file->f_op != &bpf_map_fops) {
264 fdput(f);
265 return ERR_PTR(-EINVAL);
266 }
267
Daniel Borkmannc2101292015-10-29 14:58:07 +0100268 return f.file->private_data;
269}
270
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700271/* prog's and map's refcnt limit */
272#define BPF_MAX_REFCNT 32768
273
274struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100275{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700276 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
277 atomic_dec(&map->refcnt);
278 return ERR_PTR(-EBUSY);
279 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100280 if (uref)
281 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700282 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100283}
284
285struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100286{
287 struct fd f = fdget(ufd);
288 struct bpf_map *map;
289
290 map = __bpf_map_get(f);
291 if (IS_ERR(map))
292 return map;
293
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700294 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100295 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700296
297 return map;
298}
299
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800300int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
301{
302 return -ENOTSUPP;
303}
304
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700305/* last field in 'union bpf_attr' used by this command */
306#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
307
308static int map_lookup_elem(union bpf_attr *attr)
309{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100310 void __user *ukey = u64_to_user_ptr(attr->key);
311 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700312 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700313 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800314 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800315 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200316 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700317 int err;
318
319 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
320 return -EINVAL;
321
Daniel Borkmann592867b2015-09-08 18:00:09 +0200322 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100323 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700324 if (IS_ERR(map))
325 return PTR_ERR(map);
326
327 err = -ENOMEM;
328 key = kmalloc(map->key_size, GFP_USER);
329 if (!key)
330 goto err_put;
331
332 err = -EFAULT;
333 if (copy_from_user(key, ukey, map->key_size) != 0)
334 goto free_key;
335
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800336 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800337 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800338 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
339 value_size = round_up(map->value_size, 8) * num_possible_cpus();
340 else
341 value_size = map->value_size;
342
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800343 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800344 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700345 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800346 goto free_key;
347
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800348 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
349 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800350 err = bpf_percpu_hash_copy(map, key, value);
351 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
352 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800353 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
354 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700355 } else if (map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
356 map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700357 err = -ENOTSUPP;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800358 } else {
359 rcu_read_lock();
360 ptr = map->ops->map_lookup_elem(map, key);
361 if (ptr)
362 memcpy(value, ptr, value_size);
363 rcu_read_unlock();
364 err = ptr ? 0 : -ENOENT;
365 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800366
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800367 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800368 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700369
370 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800371 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800372 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700373
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100374 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700375 err = 0;
376
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800377free_value:
378 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700379free_key:
380 kfree(key);
381err_put:
382 fdput(f);
383 return err;
384}
385
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800386#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700387
388static int map_update_elem(union bpf_attr *attr)
389{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100390 void __user *ukey = u64_to_user_ptr(attr->key);
391 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700392 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700393 struct bpf_map *map;
394 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800395 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200396 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700397 int err;
398
399 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
400 return -EINVAL;
401
Daniel Borkmann592867b2015-09-08 18:00:09 +0200402 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100403 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700404 if (IS_ERR(map))
405 return PTR_ERR(map);
406
407 err = -ENOMEM;
408 key = kmalloc(map->key_size, GFP_USER);
409 if (!key)
410 goto err_put;
411
412 err = -EFAULT;
413 if (copy_from_user(key, ukey, map->key_size) != 0)
414 goto free_key;
415
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800416 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800417 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800418 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
419 value_size = round_up(map->value_size, 8) * num_possible_cpus();
420 else
421 value_size = map->value_size;
422
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700423 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800424 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700425 if (!value)
426 goto free_key;
427
428 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800429 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700430 goto free_value;
431
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800432 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
433 * inside bpf map update or delete otherwise deadlocks are possible
434 */
435 preempt_disable();
436 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800437 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
438 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800439 err = bpf_percpu_hash_update(map, key, value, attr->flags);
440 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
441 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200442 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700443 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700444 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
445 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200446 rcu_read_lock();
447 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
448 attr->flags);
449 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700450 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
451 rcu_read_lock();
452 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
453 attr->flags);
454 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800455 } else {
456 rcu_read_lock();
457 err = map->ops->map_update_elem(map, key, value, attr->flags);
458 rcu_read_unlock();
459 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800460 __this_cpu_dec(bpf_prog_active);
461 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700462
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100463 if (!err)
464 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700465free_value:
466 kfree(value);
467free_key:
468 kfree(key);
469err_put:
470 fdput(f);
471 return err;
472}
473
474#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
475
476static int map_delete_elem(union bpf_attr *attr)
477{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100478 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700479 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700480 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200481 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700482 void *key;
483 int err;
484
485 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
486 return -EINVAL;
487
Daniel Borkmann592867b2015-09-08 18:00:09 +0200488 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100489 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700490 if (IS_ERR(map))
491 return PTR_ERR(map);
492
493 err = -ENOMEM;
494 key = kmalloc(map->key_size, GFP_USER);
495 if (!key)
496 goto err_put;
497
498 err = -EFAULT;
499 if (copy_from_user(key, ukey, map->key_size) != 0)
500 goto free_key;
501
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800502 preempt_disable();
503 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700504 rcu_read_lock();
505 err = map->ops->map_delete_elem(map, key);
506 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800507 __this_cpu_dec(bpf_prog_active);
508 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700509
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100510 if (!err)
511 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700512free_key:
513 kfree(key);
514err_put:
515 fdput(f);
516 return err;
517}
518
519/* last field in 'union bpf_attr' used by this command */
520#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
521
522static int map_get_next_key(union bpf_attr *attr)
523{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100524 void __user *ukey = u64_to_user_ptr(attr->key);
525 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700526 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700527 struct bpf_map *map;
528 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200529 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700530 int err;
531
532 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
533 return -EINVAL;
534
Daniel Borkmann592867b2015-09-08 18:00:09 +0200535 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100536 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700537 if (IS_ERR(map))
538 return PTR_ERR(map);
539
540 err = -ENOMEM;
541 key = kmalloc(map->key_size, GFP_USER);
542 if (!key)
543 goto err_put;
544
545 err = -EFAULT;
546 if (copy_from_user(key, ukey, map->key_size) != 0)
547 goto free_key;
548
549 err = -ENOMEM;
550 next_key = kmalloc(map->key_size, GFP_USER);
551 if (!next_key)
552 goto free_key;
553
554 rcu_read_lock();
555 err = map->ops->map_get_next_key(map, key, next_key);
556 rcu_read_unlock();
557 if (err)
558 goto free_next_key;
559
560 err = -EFAULT;
561 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
562 goto free_next_key;
563
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100564 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700565 err = 0;
566
567free_next_key:
568 kfree(next_key);
569free_key:
570 kfree(key);
571err_put:
572 fdput(f);
573 return err;
574}
575
Johannes Bergbe9370a2017-04-11 15:34:57 +0200576static const struct bpf_verifier_ops * const bpf_prog_types[] = {
577#define BPF_PROG_TYPE(_id, _ops) \
578 [_id] = &_ops,
579#include <linux/bpf_types.h>
580#undef BPF_PROG_TYPE
581};
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700582
583static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
584{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200585 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
586 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700587
Johannes Bergbe9370a2017-04-11 15:34:57 +0200588 prog->aux->ops = bpf_prog_types[type];
589 prog->type = type;
590 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700591}
592
593/* drop refcnt on maps used by eBPF program and free auxilary data */
594static void free_used_maps(struct bpf_prog_aux *aux)
595{
596 int i;
597
598 for (i = 0; i < aux->used_map_cnt; i++)
599 bpf_map_put(aux->used_maps[i]);
600
601 kfree(aux->used_maps);
602}
603
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100604int __bpf_prog_charge(struct user_struct *user, u32 pages)
605{
606 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
607 unsigned long user_bufs;
608
609 if (user) {
610 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
611 if (user_bufs > memlock_limit) {
612 atomic_long_sub(pages, &user->locked_vm);
613 return -EPERM;
614 }
615 }
616
617 return 0;
618}
619
620void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
621{
622 if (user)
623 atomic_long_sub(pages, &user->locked_vm);
624}
625
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700626static int bpf_prog_charge_memlock(struct bpf_prog *prog)
627{
628 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100629 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700630
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100631 ret = __bpf_prog_charge(user, prog->pages);
632 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700633 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100634 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700635 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100636
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700637 prog->aux->user = user;
638 return 0;
639}
640
641static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
642{
643 struct user_struct *user = prog->aux->user;
644
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100645 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700646 free_uid(user);
647}
648
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200649static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700650{
651 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
652
653 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700654 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700655 bpf_prog_free(aux->prog);
656}
657
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700658void bpf_prog_put(struct bpf_prog *prog)
659{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100660 if (atomic_dec_and_test(&prog->aux->refcnt)) {
661 trace_bpf_prog_put_rcu(prog);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100662 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200663 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100664 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700665}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100666EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700667
668static int bpf_prog_release(struct inode *inode, struct file *filp)
669{
670 struct bpf_prog *prog = filp->private_data;
671
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200672 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700673 return 0;
674}
675
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100676#ifdef CONFIG_PROC_FS
677static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
678{
679 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100680 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100681
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100682 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100683 seq_printf(m,
684 "prog_type:\t%u\n"
685 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100686 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100687 "memlock:\t%llu\n",
688 prog->type,
689 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100690 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100691 prog->pages * 1ULL << PAGE_SHIFT);
692}
693#endif
694
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700695static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100696#ifdef CONFIG_PROC_FS
697 .show_fdinfo = bpf_prog_show_fdinfo,
698#endif
699 .release = bpf_prog_release,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700700};
701
Daniel Borkmannb2197752015-10-29 14:58:09 +0100702int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100703{
704 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
705 O_RDWR | O_CLOEXEC);
706}
707
Daniel Borkmann113214b2016-06-30 17:24:44 +0200708static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700709{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700710 if (!f.file)
711 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700712 if (f.file->f_op != &bpf_prog_fops) {
713 fdput(f);
714 return ERR_PTR(-EINVAL);
715 }
716
Daniel Borkmannc2101292015-10-29 14:58:07 +0100717 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700718}
719
Brenden Blanco59d36562016-07-19 12:16:46 -0700720struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700721{
Brenden Blanco59d36562016-07-19 12:16:46 -0700722 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
723 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700724 return ERR_PTR(-EBUSY);
725 }
726 return prog;
727}
Brenden Blanco59d36562016-07-19 12:16:46 -0700728EXPORT_SYMBOL_GPL(bpf_prog_add);
729
Daniel Borkmannc5405942016-11-09 22:02:34 +0100730void bpf_prog_sub(struct bpf_prog *prog, int i)
731{
732 /* Only to be used for undoing previous bpf_prog_add() in some
733 * error path. We still know that another entity in our call
734 * path holds a reference to the program, thus atomic_sub() can
735 * be safely used in such cases!
736 */
737 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
738}
739EXPORT_SYMBOL_GPL(bpf_prog_sub);
740
Brenden Blanco59d36562016-07-19 12:16:46 -0700741struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
742{
743 return bpf_prog_add(prog, 1);
744}
Daniel Borkmann97bc4022016-11-19 01:45:00 +0100745EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700746
Daniel Borkmann113214b2016-06-30 17:24:44 +0200747static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700748{
749 struct fd f = fdget(ufd);
750 struct bpf_prog *prog;
751
Daniel Borkmann113214b2016-06-30 17:24:44 +0200752 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700753 if (IS_ERR(prog))
754 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200755 if (type && prog->type != *type) {
756 prog = ERR_PTR(-EINVAL);
757 goto out;
758 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700759
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700760 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200761out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700762 fdput(f);
763 return prog;
764}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200765
766struct bpf_prog *bpf_prog_get(u32 ufd)
767{
768 return __bpf_prog_get(ufd, NULL);
769}
770
771struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
772{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100773 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
774
775 if (!IS_ERR(prog))
776 trace_bpf_prog_get_type(prog);
777 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200778}
779EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700780
781/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700782#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700783
784static int bpf_prog_load(union bpf_attr *attr)
785{
786 enum bpf_prog_type type = attr->prog_type;
787 struct bpf_prog *prog;
788 int err;
789 char license[128];
790 bool is_gpl;
791
792 if (CHECK_ATTR(BPF_PROG_LOAD))
793 return -EINVAL;
794
795 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100796 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700797 sizeof(license) - 1) < 0)
798 return -EFAULT;
799 license[sizeof(license) - 1] = 0;
800
801 /* eBPF programs must be GPL compatible to use GPL-ed functions */
802 is_gpl = license_is_gpl_compatible(license);
803
Daniel Borkmannef0915c2016-12-07 01:15:44 +0100804 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
805 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700806
Alexei Starovoitov25415172015-03-25 12:49:20 -0700807 if (type == BPF_PROG_TYPE_KPROBE &&
808 attr->kern_version != LINUX_VERSION_CODE)
809 return -EINVAL;
810
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700811 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
812 return -EPERM;
813
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700814 /* plain bpf_prog allocation */
815 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
816 if (!prog)
817 return -ENOMEM;
818
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700819 err = bpf_prog_charge_memlock(prog);
820 if (err)
821 goto free_prog_nouncharge;
822
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700823 prog->len = attr->insn_cnt;
824
825 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100826 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100827 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700828 goto free_prog;
829
830 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200831 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700832
833 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200834 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700835
836 /* find program type: socket_filter vs tracing_filter */
837 err = find_prog_type(type, prog);
838 if (err < 0)
839 goto free_prog;
840
841 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700842 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700843 if (err < 0)
844 goto free_used_maps;
845
846 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200847 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700848 if (err < 0)
849 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700850
Daniel Borkmannaa797812015-10-29 14:58:06 +0100851 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700852 if (err < 0)
853 /* failed to allocate fd */
854 goto free_used_maps;
855
Daniel Borkmann74451e662017-02-16 22:24:50 +0100856 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100857 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700858 return err;
859
860free_used_maps:
861 free_used_maps(prog->aux);
862free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700863 bpf_prog_uncharge_memlock(prog);
864free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700865 bpf_prog_free(prog);
866 return err;
867}
868
Daniel Borkmannb2197752015-10-29 14:58:09 +0100869#define BPF_OBJ_LAST_FIELD bpf_fd
870
871static int bpf_obj_pin(const union bpf_attr *attr)
872{
873 if (CHECK_ATTR(BPF_OBJ))
874 return -EINVAL;
875
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100876 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100877}
878
879static int bpf_obj_get(const union bpf_attr *attr)
880{
881 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
882 return -EINVAL;
883
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100884 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100885}
886
Daniel Mackf4324552016-11-23 16:52:27 +0100887#ifdef CONFIG_CGROUP_BPF
888
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800889#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
Daniel Mackf4324552016-11-23 16:52:27 +0100890
891static int bpf_prog_attach(const union bpf_attr *attr)
892{
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800893 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +0100894 struct bpf_prog *prog;
895 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800896 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100897
898 if (!capable(CAP_NET_ADMIN))
899 return -EPERM;
900
901 if (CHECK_ATTR(BPF_PROG_ATTACH))
902 return -EINVAL;
903
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800904 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
905 return -EINVAL;
906
Daniel Mackf4324552016-11-23 16:52:27 +0100907 switch (attr->attach_type) {
908 case BPF_CGROUP_INET_INGRESS:
909 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -0800910 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +0100911 break;
David Ahern610236582016-12-01 08:48:04 -0800912 case BPF_CGROUP_INET_SOCK_CREATE:
913 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
914 break;
Daniel Mackf4324552016-11-23 16:52:27 +0100915 default:
916 return -EINVAL;
917 }
918
David Ahernb2cd1252016-12-01 08:48:03 -0800919 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
920 if (IS_ERR(prog))
921 return PTR_ERR(prog);
922
923 cgrp = cgroup_get_from_fd(attr->target_fd);
924 if (IS_ERR(cgrp)) {
925 bpf_prog_put(prog);
926 return PTR_ERR(cgrp);
927 }
928
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800929 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
930 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
931 if (ret)
932 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -0800933 cgroup_put(cgrp);
934
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800935 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100936}
937
938#define BPF_PROG_DETACH_LAST_FIELD attach_type
939
940static int bpf_prog_detach(const union bpf_attr *attr)
941{
942 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800943 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100944
945 if (!capable(CAP_NET_ADMIN))
946 return -EPERM;
947
948 if (CHECK_ATTR(BPF_PROG_DETACH))
949 return -EINVAL;
950
951 switch (attr->attach_type) {
952 case BPF_CGROUP_INET_INGRESS:
953 case BPF_CGROUP_INET_EGRESS:
David Ahern610236582016-12-01 08:48:04 -0800954 case BPF_CGROUP_INET_SOCK_CREATE:
Daniel Mackf4324552016-11-23 16:52:27 +0100955 cgrp = cgroup_get_from_fd(attr->target_fd);
956 if (IS_ERR(cgrp))
957 return PTR_ERR(cgrp);
958
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800959 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
Daniel Mackf4324552016-11-23 16:52:27 +0100960 cgroup_put(cgrp);
961 break;
962
963 default:
964 return -EINVAL;
965 }
966
Alexei Starovoitov7f677632017-02-10 20:28:24 -0800967 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +0100968}
969#endif /* CONFIG_CGROUP_BPF */
970
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700971#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
972
973static int bpf_prog_test_run(const union bpf_attr *attr,
974 union bpf_attr __user *uattr)
975{
976 struct bpf_prog *prog;
977 int ret = -ENOTSUPP;
978
979 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
980 return -EINVAL;
981
982 prog = bpf_prog_get(attr->test.prog_fd);
983 if (IS_ERR(prog))
984 return PTR_ERR(prog);
985
986 if (prog->aux->ops->test_run)
987 ret = prog->aux->ops->test_run(prog, attr, uattr);
988
989 bpf_prog_put(prog);
990 return ret;
991}
992
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700993SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
994{
995 union bpf_attr attr = {};
996 int err;
997
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700998 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700999 return -EPERM;
1000
1001 if (!access_ok(VERIFY_READ, uattr, 1))
1002 return -EFAULT;
1003
1004 if (size > PAGE_SIZE) /* silly large */
1005 return -E2BIG;
1006
1007 /* If we're handed a bigger struct than we know of,
1008 * ensure all the unknown bits are 0 - i.e. new
1009 * user-space does not rely on any kernel feature
1010 * extensions we dont know about yet.
1011 */
1012 if (size > sizeof(attr)) {
1013 unsigned char __user *addr;
1014 unsigned char __user *end;
1015 unsigned char val;
1016
1017 addr = (void __user *)uattr + sizeof(attr);
1018 end = (void __user *)uattr + size;
1019
1020 for (; addr < end; addr++) {
1021 err = get_user(val, addr);
1022 if (err)
1023 return err;
1024 if (val)
1025 return -E2BIG;
1026 }
1027 size = sizeof(attr);
1028 }
1029
1030 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1031 if (copy_from_user(&attr, uattr, size) != 0)
1032 return -EFAULT;
1033
1034 switch (cmd) {
1035 case BPF_MAP_CREATE:
1036 err = map_create(&attr);
1037 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001038 case BPF_MAP_LOOKUP_ELEM:
1039 err = map_lookup_elem(&attr);
1040 break;
1041 case BPF_MAP_UPDATE_ELEM:
1042 err = map_update_elem(&attr);
1043 break;
1044 case BPF_MAP_DELETE_ELEM:
1045 err = map_delete_elem(&attr);
1046 break;
1047 case BPF_MAP_GET_NEXT_KEY:
1048 err = map_get_next_key(&attr);
1049 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001050 case BPF_PROG_LOAD:
1051 err = bpf_prog_load(&attr);
1052 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001053 case BPF_OBJ_PIN:
1054 err = bpf_obj_pin(&attr);
1055 break;
1056 case BPF_OBJ_GET:
1057 err = bpf_obj_get(&attr);
1058 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001059#ifdef CONFIG_CGROUP_BPF
1060 case BPF_PROG_ATTACH:
1061 err = bpf_prog_attach(&attr);
1062 break;
1063 case BPF_PROG_DETACH:
1064 err = bpf_prog_detach(&attr);
1065 break;
1066#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001067 case BPF_PROG_TEST_RUN:
1068 err = bpf_prog_test_run(&attr, uattr);
1069 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001070 default:
1071 err = -EINVAL;
1072 break;
1073 }
1074
1075 return err;
1076}