blob: 05ad086ab71dc97d7de833cdf651cb0c28e94618 [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>
16#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070017#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070018#include <linux/license.h>
19#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070020#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010021#include <linux/kernel.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070022
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080023DEFINE_PER_CPU(int, bpf_prog_active);
24
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070025int sysctl_unprivileged_bpf_disabled __read_mostly;
26
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070027static LIST_HEAD(bpf_map_types);
28
29static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
30{
31 struct bpf_map_type_list *tl;
32 struct bpf_map *map;
33
34 list_for_each_entry(tl, &bpf_map_types, list_node) {
35 if (tl->type == attr->map_type) {
36 map = tl->ops->map_alloc(attr);
37 if (IS_ERR(map))
38 return map;
39 map->ops = tl->ops;
40 map->map_type = attr->map_type;
41 return map;
42 }
43 }
44 return ERR_PTR(-EINVAL);
45}
46
47/* boot time registration of different map implementations */
48void bpf_register_map_type(struct bpf_map_type_list *tl)
49{
50 list_add(&tl->list_node, &bpf_map_types);
51}
52
Alexei Starovoitov6c905982016-03-07 21:57:15 -080053int bpf_map_precharge_memlock(u32 pages)
54{
55 struct user_struct *user = get_current_user();
56 unsigned long memlock_limit, cur;
57
58 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
59 cur = atomic_long_read(&user->locked_vm);
60 free_uid(user);
61 if (cur + pages > memlock_limit)
62 return -EPERM;
63 return 0;
64}
65
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070066static int bpf_map_charge_memlock(struct bpf_map *map)
67{
68 struct user_struct *user = get_current_user();
69 unsigned long memlock_limit;
70
71 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
72
73 atomic_long_add(map->pages, &user->locked_vm);
74
75 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
76 atomic_long_sub(map->pages, &user->locked_vm);
77 free_uid(user);
78 return -EPERM;
79 }
80 map->user = user;
81 return 0;
82}
83
84static void bpf_map_uncharge_memlock(struct bpf_map *map)
85{
86 struct user_struct *user = map->user;
87
88 atomic_long_sub(map->pages, &user->locked_vm);
89 free_uid(user);
90}
91
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070092/* called from workqueue */
93static void bpf_map_free_deferred(struct work_struct *work)
94{
95 struct bpf_map *map = container_of(work, struct bpf_map, work);
96
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070097 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070098 /* implementation dependent freeing */
99 map->ops->map_free(map);
100}
101
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100102static void bpf_map_put_uref(struct bpf_map *map)
103{
104 if (atomic_dec_and_test(&map->usercnt)) {
105 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
106 bpf_fd_array_map_clear(map);
107 }
108}
109
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700110/* decrement map refcnt and schedule it for freeing via workqueue
111 * (unrelying map implementation ops->map_free() might sleep)
112 */
113void bpf_map_put(struct bpf_map *map)
114{
115 if (atomic_dec_and_test(&map->refcnt)) {
116 INIT_WORK(&map->work, bpf_map_free_deferred);
117 schedule_work(&map->work);
118 }
119}
120
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100121void bpf_map_put_with_uref(struct bpf_map *map)
122{
123 bpf_map_put_uref(map);
124 bpf_map_put(map);
125}
126
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700127static int bpf_map_release(struct inode *inode, struct file *filp)
128{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200129 struct bpf_map *map = filp->private_data;
130
131 if (map->ops->map_release)
132 map->ops->map_release(map, filp);
133
134 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700135 return 0;
136}
137
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100138#ifdef CONFIG_PROC_FS
139static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
140{
141 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100142 const struct bpf_array *array;
143 u32 owner_prog_type = 0;
144
145 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
146 array = container_of(map, struct bpf_array, map);
147 owner_prog_type = array->owner_prog_type;
148 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100149
150 seq_printf(m,
151 "map_type:\t%u\n"
152 "key_size:\t%u\n"
153 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100154 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100155 "map_flags:\t%#x\n"
156 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100157 map->map_type,
158 map->key_size,
159 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100160 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100161 map->map_flags,
162 map->pages * 1ULL << PAGE_SHIFT);
163
164 if (owner_prog_type)
165 seq_printf(m, "owner_prog_type:\t%u\n",
166 owner_prog_type);
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100167}
168#endif
169
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700170static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100171#ifdef CONFIG_PROC_FS
172 .show_fdinfo = bpf_map_show_fdinfo,
173#endif
174 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700175};
176
Daniel Borkmannb2197752015-10-29 14:58:09 +0100177int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100178{
179 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
180 O_RDWR | O_CLOEXEC);
181}
182
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700183/* helper macro to check that unused fields 'union bpf_attr' are zero */
184#define CHECK_ATTR(CMD) \
185 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
186 sizeof(attr->CMD##_LAST_FIELD), 0, \
187 sizeof(*attr) - \
188 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
189 sizeof(attr->CMD##_LAST_FIELD)) != NULL
190
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800191#define BPF_MAP_CREATE_LAST_FIELD map_flags
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700192/* called via syscall */
193static int map_create(union bpf_attr *attr)
194{
195 struct bpf_map *map;
196 int err;
197
198 err = CHECK_ATTR(BPF_MAP_CREATE);
199 if (err)
200 return -EINVAL;
201
202 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
203 map = find_and_alloc_map(attr);
204 if (IS_ERR(map))
205 return PTR_ERR(map);
206
207 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100208 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700209
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700210 err = bpf_map_charge_memlock(map);
211 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100212 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700213
Daniel Borkmannaa797812015-10-29 14:58:06 +0100214 err = bpf_map_new_fd(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700215 if (err < 0)
216 /* failed to allocate fd */
217 goto free_map;
218
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100219 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700220 return err;
221
222free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100223 bpf_map_uncharge_memlock(map);
224free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700225 map->ops->map_free(map);
226 return err;
227}
228
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700229/* if error is returned, fd is released.
230 * On success caller should complete fd access with matching fdput()
231 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100232struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700233{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700234 if (!f.file)
235 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700236 if (f.file->f_op != &bpf_map_fops) {
237 fdput(f);
238 return ERR_PTR(-EINVAL);
239 }
240
Daniel Borkmannc2101292015-10-29 14:58:07 +0100241 return f.file->private_data;
242}
243
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700244/* prog's and map's refcnt limit */
245#define BPF_MAX_REFCNT 32768
246
247struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100248{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700249 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
250 atomic_dec(&map->refcnt);
251 return ERR_PTR(-EBUSY);
252 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100253 if (uref)
254 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700255 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100256}
257
258struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100259{
260 struct fd f = fdget(ufd);
261 struct bpf_map *map;
262
263 map = __bpf_map_get(f);
264 if (IS_ERR(map))
265 return map;
266
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700267 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100268 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700269
270 return map;
271}
272
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800273int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
274{
275 return -ENOTSUPP;
276}
277
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700278/* last field in 'union bpf_attr' used by this command */
279#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
280
281static int map_lookup_elem(union bpf_attr *attr)
282{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100283 void __user *ukey = u64_to_user_ptr(attr->key);
284 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700285 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700286 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800287 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800288 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200289 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700290 int err;
291
292 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
293 return -EINVAL;
294
Daniel Borkmann592867b2015-09-08 18:00:09 +0200295 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100296 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700297 if (IS_ERR(map))
298 return PTR_ERR(map);
299
300 err = -ENOMEM;
301 key = kmalloc(map->key_size, GFP_USER);
302 if (!key)
303 goto err_put;
304
305 err = -EFAULT;
306 if (copy_from_user(key, ukey, map->key_size) != 0)
307 goto free_key;
308
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800309 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800310 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800311 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
312 value_size = round_up(map->value_size, 8) * num_possible_cpus();
313 else
314 value_size = map->value_size;
315
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800316 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800317 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700318 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800319 goto free_key;
320
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800321 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
322 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800323 err = bpf_percpu_hash_copy(map, key, value);
324 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
325 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800326 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
327 err = bpf_stackmap_copy(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800328 } else {
329 rcu_read_lock();
330 ptr = map->ops->map_lookup_elem(map, key);
331 if (ptr)
332 memcpy(value, ptr, value_size);
333 rcu_read_unlock();
334 err = ptr ? 0 : -ENOENT;
335 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800336
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800337 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800338 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700339
340 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800341 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800342 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700343
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100344 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700345 err = 0;
346
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800347free_value:
348 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700349free_key:
350 kfree(key);
351err_put:
352 fdput(f);
353 return err;
354}
355
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800356#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700357
358static int map_update_elem(union bpf_attr *attr)
359{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100360 void __user *ukey = u64_to_user_ptr(attr->key);
361 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700362 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700363 struct bpf_map *map;
364 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800365 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200366 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700367 int err;
368
369 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
370 return -EINVAL;
371
Daniel Borkmann592867b2015-09-08 18:00:09 +0200372 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100373 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700374 if (IS_ERR(map))
375 return PTR_ERR(map);
376
377 err = -ENOMEM;
378 key = kmalloc(map->key_size, GFP_USER);
379 if (!key)
380 goto err_put;
381
382 err = -EFAULT;
383 if (copy_from_user(key, ukey, map->key_size) != 0)
384 goto free_key;
385
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800386 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800387 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800388 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
389 value_size = round_up(map->value_size, 8) * num_possible_cpus();
390 else
391 value_size = map->value_size;
392
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700393 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800394 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700395 if (!value)
396 goto free_key;
397
398 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800399 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700400 goto free_value;
401
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800402 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
403 * inside bpf map update or delete otherwise deadlocks are possible
404 */
405 preempt_disable();
406 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800407 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
408 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800409 err = bpf_percpu_hash_update(map, key, value, attr->flags);
410 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
411 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200412 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700413 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
414 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200415 rcu_read_lock();
416 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
417 attr->flags);
418 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800419 } else {
420 rcu_read_lock();
421 err = map->ops->map_update_elem(map, key, value, attr->flags);
422 rcu_read_unlock();
423 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800424 __this_cpu_dec(bpf_prog_active);
425 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700426
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100427 if (!err)
428 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700429free_value:
430 kfree(value);
431free_key:
432 kfree(key);
433err_put:
434 fdput(f);
435 return err;
436}
437
438#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
439
440static int map_delete_elem(union bpf_attr *attr)
441{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100442 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700443 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700444 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200445 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700446 void *key;
447 int err;
448
449 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
450 return -EINVAL;
451
Daniel Borkmann592867b2015-09-08 18:00:09 +0200452 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100453 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700454 if (IS_ERR(map))
455 return PTR_ERR(map);
456
457 err = -ENOMEM;
458 key = kmalloc(map->key_size, GFP_USER);
459 if (!key)
460 goto err_put;
461
462 err = -EFAULT;
463 if (copy_from_user(key, ukey, map->key_size) != 0)
464 goto free_key;
465
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800466 preempt_disable();
467 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700468 rcu_read_lock();
469 err = map->ops->map_delete_elem(map, key);
470 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800471 __this_cpu_dec(bpf_prog_active);
472 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700473
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100474 if (!err)
475 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700476free_key:
477 kfree(key);
478err_put:
479 fdput(f);
480 return err;
481}
482
483/* last field in 'union bpf_attr' used by this command */
484#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
485
486static int map_get_next_key(union bpf_attr *attr)
487{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100488 void __user *ukey = u64_to_user_ptr(attr->key);
489 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700490 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700491 struct bpf_map *map;
492 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200493 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700494 int err;
495
496 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
497 return -EINVAL;
498
Daniel Borkmann592867b2015-09-08 18:00:09 +0200499 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100500 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700501 if (IS_ERR(map))
502 return PTR_ERR(map);
503
504 err = -ENOMEM;
505 key = kmalloc(map->key_size, GFP_USER);
506 if (!key)
507 goto err_put;
508
509 err = -EFAULT;
510 if (copy_from_user(key, ukey, map->key_size) != 0)
511 goto free_key;
512
513 err = -ENOMEM;
514 next_key = kmalloc(map->key_size, GFP_USER);
515 if (!next_key)
516 goto free_key;
517
518 rcu_read_lock();
519 err = map->ops->map_get_next_key(map, key, next_key);
520 rcu_read_unlock();
521 if (err)
522 goto free_next_key;
523
524 err = -EFAULT;
525 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
526 goto free_next_key;
527
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100528 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700529 err = 0;
530
531free_next_key:
532 kfree(next_key);
533free_key:
534 kfree(key);
535err_put:
536 fdput(f);
537 return err;
538}
539
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700540static LIST_HEAD(bpf_prog_types);
541
542static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
543{
544 struct bpf_prog_type_list *tl;
545
546 list_for_each_entry(tl, &bpf_prog_types, list_node) {
547 if (tl->type == type) {
548 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100549 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700550 return 0;
551 }
552 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100553
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700554 return -EINVAL;
555}
556
557void bpf_register_prog_type(struct bpf_prog_type_list *tl)
558{
559 list_add(&tl->list_node, &bpf_prog_types);
560}
561
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700562/* fixup insn->imm field of bpf_call instructions:
563 * if (insn->imm == BPF_FUNC_map_lookup_elem)
564 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
565 * else if (insn->imm == BPF_FUNC_map_update_elem)
566 * insn->imm = bpf_map_update_elem - __bpf_call_base;
567 * else ...
568 *
569 * this function is called after eBPF program passed verification
570 */
571static void fixup_bpf_calls(struct bpf_prog *prog)
572{
573 const struct bpf_func_proto *fn;
574 int i;
575
576 for (i = 0; i < prog->len; i++) {
577 struct bpf_insn *insn = &prog->insnsi[i];
578
579 if (insn->code == (BPF_JMP | BPF_CALL)) {
580 /* we reach here when program has bpf_call instructions
581 * and it passed bpf_check(), means that
582 * ops->get_func_proto must have been supplied, check it
583 */
584 BUG_ON(!prog->aux->ops->get_func_proto);
585
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200586 if (insn->imm == BPF_FUNC_get_route_realm)
587 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200588 if (insn->imm == BPF_FUNC_get_prandom_u32)
589 bpf_user_rnd_init_once();
Martin KaFai Lau17bedab2016-12-07 15:53:11 -0800590 if (insn->imm == BPF_FUNC_xdp_adjust_head)
591 prog->xdp_adjust_head = 1;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700592 if (insn->imm == BPF_FUNC_tail_call) {
593 /* mark bpf_tail_call as different opcode
594 * to avoid conditional branch in
595 * interpeter for every normal call
596 * and to prevent accidental JITing by
597 * JIT compiler that doesn't support
598 * bpf_tail_call yet
599 */
600 insn->imm = 0;
601 insn->code |= BPF_X;
602 continue;
603 }
604
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700605 fn = prog->aux->ops->get_func_proto(insn->imm);
606 /* all functions that have prototype and verifier allowed
607 * programs to call them, must be real in-kernel functions
608 */
609 BUG_ON(!fn->func);
610 insn->imm = fn->func - __bpf_call_base;
611 }
612 }
613}
614
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700615/* drop refcnt on maps used by eBPF program and free auxilary data */
616static void free_used_maps(struct bpf_prog_aux *aux)
617{
618 int i;
619
620 for (i = 0; i < aux->used_map_cnt; i++)
621 bpf_map_put(aux->used_maps[i]);
622
623 kfree(aux->used_maps);
624}
625
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100626int __bpf_prog_charge(struct user_struct *user, u32 pages)
627{
628 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
629 unsigned long user_bufs;
630
631 if (user) {
632 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
633 if (user_bufs > memlock_limit) {
634 atomic_long_sub(pages, &user->locked_vm);
635 return -EPERM;
636 }
637 }
638
639 return 0;
640}
641
642void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
643{
644 if (user)
645 atomic_long_sub(pages, &user->locked_vm);
646}
647
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700648static int bpf_prog_charge_memlock(struct bpf_prog *prog)
649{
650 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100651 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700652
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100653 ret = __bpf_prog_charge(user, prog->pages);
654 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700655 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100656 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700657 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100658
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700659 prog->aux->user = user;
660 return 0;
661}
662
663static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
664{
665 struct user_struct *user = prog->aux->user;
666
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100667 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700668 free_uid(user);
669}
670
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200671static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700672{
673 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
674
675 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700676 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700677 bpf_prog_free(aux->prog);
678}
679
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700680void bpf_prog_put(struct bpf_prog *prog)
681{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100682 if (atomic_dec_and_test(&prog->aux->refcnt)) {
683 trace_bpf_prog_put_rcu(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200684 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100685 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700686}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100687EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700688
689static int bpf_prog_release(struct inode *inode, struct file *filp)
690{
691 struct bpf_prog *prog = filp->private_data;
692
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200693 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700694 return 0;
695}
696
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100697#ifdef CONFIG_PROC_FS
698static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
699{
700 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100701 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100702
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100703 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100704 seq_printf(m,
705 "prog_type:\t%u\n"
706 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100707 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100708 "memlock:\t%llu\n",
709 prog->type,
710 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100711 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100712 prog->pages * 1ULL << PAGE_SHIFT);
713}
714#endif
715
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700716static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100717#ifdef CONFIG_PROC_FS
718 .show_fdinfo = bpf_prog_show_fdinfo,
719#endif
720 .release = bpf_prog_release,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700721};
722
Daniel Borkmannb2197752015-10-29 14:58:09 +0100723int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100724{
725 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
726 O_RDWR | O_CLOEXEC);
727}
728
Daniel Borkmann113214b2016-06-30 17:24:44 +0200729static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700730{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700731 if (!f.file)
732 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700733 if (f.file->f_op != &bpf_prog_fops) {
734 fdput(f);
735 return ERR_PTR(-EINVAL);
736 }
737
Daniel Borkmannc2101292015-10-29 14:58:07 +0100738 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700739}
740
Brenden Blanco59d36562016-07-19 12:16:46 -0700741struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700742{
Brenden Blanco59d36562016-07-19 12:16:46 -0700743 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
744 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700745 return ERR_PTR(-EBUSY);
746 }
747 return prog;
748}
Brenden Blanco59d36562016-07-19 12:16:46 -0700749EXPORT_SYMBOL_GPL(bpf_prog_add);
750
Daniel Borkmannc5405942016-11-09 22:02:34 +0100751void bpf_prog_sub(struct bpf_prog *prog, int i)
752{
753 /* Only to be used for undoing previous bpf_prog_add() in some
754 * error path. We still know that another entity in our call
755 * path holds a reference to the program, thus atomic_sub() can
756 * be safely used in such cases!
757 */
758 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
759}
760EXPORT_SYMBOL_GPL(bpf_prog_sub);
761
Brenden Blanco59d36562016-07-19 12:16:46 -0700762struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
763{
764 return bpf_prog_add(prog, 1);
765}
Daniel Borkmann97bc4022016-11-19 01:45:00 +0100766EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700767
Daniel Borkmann113214b2016-06-30 17:24:44 +0200768static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700769{
770 struct fd f = fdget(ufd);
771 struct bpf_prog *prog;
772
Daniel Borkmann113214b2016-06-30 17:24:44 +0200773 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700774 if (IS_ERR(prog))
775 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200776 if (type && prog->type != *type) {
777 prog = ERR_PTR(-EINVAL);
778 goto out;
779 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700780
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700781 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200782out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700783 fdput(f);
784 return prog;
785}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200786
787struct bpf_prog *bpf_prog_get(u32 ufd)
788{
789 return __bpf_prog_get(ufd, NULL);
790}
791
792struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
793{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100794 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
795
796 if (!IS_ERR(prog))
797 trace_bpf_prog_get_type(prog);
798 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200799}
800EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700801
802/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700803#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700804
805static int bpf_prog_load(union bpf_attr *attr)
806{
807 enum bpf_prog_type type = attr->prog_type;
808 struct bpf_prog *prog;
809 int err;
810 char license[128];
811 bool is_gpl;
812
813 if (CHECK_ATTR(BPF_PROG_LOAD))
814 return -EINVAL;
815
816 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100817 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700818 sizeof(license) - 1) < 0)
819 return -EFAULT;
820 license[sizeof(license) - 1] = 0;
821
822 /* eBPF programs must be GPL compatible to use GPL-ed functions */
823 is_gpl = license_is_gpl_compatible(license);
824
Daniel Borkmannef0915c2016-12-07 01:15:44 +0100825 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
826 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700827
Alexei Starovoitov25415172015-03-25 12:49:20 -0700828 if (type == BPF_PROG_TYPE_KPROBE &&
829 attr->kern_version != LINUX_VERSION_CODE)
830 return -EINVAL;
831
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700832 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
833 return -EPERM;
834
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700835 /* plain bpf_prog allocation */
836 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
837 if (!prog)
838 return -ENOMEM;
839
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700840 err = bpf_prog_charge_memlock(prog);
841 if (err)
842 goto free_prog_nouncharge;
843
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700844 prog->len = attr->insn_cnt;
845
846 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100847 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +0100848 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700849 goto free_prog;
850
851 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200852 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700853
854 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200855 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700856
857 /* find program type: socket_filter vs tracing_filter */
858 err = find_prog_type(type, prog);
859 if (err < 0)
860 goto free_prog;
861
862 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700863 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700864 if (err < 0)
865 goto free_used_maps;
866
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700867 /* fixup BPF_CALL->imm field */
868 fixup_bpf_calls(prog);
869
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700870 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200871 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700872 if (err < 0)
873 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700874
Daniel Borkmannaa797812015-10-29 14:58:06 +0100875 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700876 if (err < 0)
877 /* failed to allocate fd */
878 goto free_used_maps;
879
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100880 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700881 return err;
882
883free_used_maps:
884 free_used_maps(prog->aux);
885free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700886 bpf_prog_uncharge_memlock(prog);
887free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700888 bpf_prog_free(prog);
889 return err;
890}
891
Daniel Borkmannb2197752015-10-29 14:58:09 +0100892#define BPF_OBJ_LAST_FIELD bpf_fd
893
894static int bpf_obj_pin(const union bpf_attr *attr)
895{
896 if (CHECK_ATTR(BPF_OBJ))
897 return -EINVAL;
898
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100899 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100900}
901
902static int bpf_obj_get(const union bpf_attr *attr)
903{
904 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
905 return -EINVAL;
906
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100907 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +0100908}
909
Daniel Mackf4324552016-11-23 16:52:27 +0100910#ifdef CONFIG_CGROUP_BPF
911
912#define BPF_PROG_ATTACH_LAST_FIELD attach_type
913
914static int bpf_prog_attach(const union bpf_attr *attr)
915{
916 struct bpf_prog *prog;
917 struct cgroup *cgrp;
David Ahernb2cd1252016-12-01 08:48:03 -0800918 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +0100919
920 if (!capable(CAP_NET_ADMIN))
921 return -EPERM;
922
923 if (CHECK_ATTR(BPF_PROG_ATTACH))
924 return -EINVAL;
925
926 switch (attr->attach_type) {
927 case BPF_CGROUP_INET_INGRESS:
928 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -0800929 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +0100930 break;
David Ahern610236582016-12-01 08:48:04 -0800931 case BPF_CGROUP_INET_SOCK_CREATE:
932 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
933 break;
Daniel Mackf4324552016-11-23 16:52:27 +0100934 default:
935 return -EINVAL;
936 }
937
David Ahernb2cd1252016-12-01 08:48:03 -0800938 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
939 if (IS_ERR(prog))
940 return PTR_ERR(prog);
941
942 cgrp = cgroup_get_from_fd(attr->target_fd);
943 if (IS_ERR(cgrp)) {
944 bpf_prog_put(prog);
945 return PTR_ERR(cgrp);
946 }
947
948 cgroup_bpf_update(cgrp, prog, attr->attach_type);
949 cgroup_put(cgrp);
950
Daniel Mackf4324552016-11-23 16:52:27 +0100951 return 0;
952}
953
954#define BPF_PROG_DETACH_LAST_FIELD attach_type
955
956static int bpf_prog_detach(const union bpf_attr *attr)
957{
958 struct cgroup *cgrp;
959
960 if (!capable(CAP_NET_ADMIN))
961 return -EPERM;
962
963 if (CHECK_ATTR(BPF_PROG_DETACH))
964 return -EINVAL;
965
966 switch (attr->attach_type) {
967 case BPF_CGROUP_INET_INGRESS:
968 case BPF_CGROUP_INET_EGRESS:
David Ahern610236582016-12-01 08:48:04 -0800969 case BPF_CGROUP_INET_SOCK_CREATE:
Daniel Mackf4324552016-11-23 16:52:27 +0100970 cgrp = cgroup_get_from_fd(attr->target_fd);
971 if (IS_ERR(cgrp))
972 return PTR_ERR(cgrp);
973
974 cgroup_bpf_update(cgrp, NULL, attr->attach_type);
975 cgroup_put(cgrp);
976 break;
977
978 default:
979 return -EINVAL;
980 }
981
982 return 0;
983}
984#endif /* CONFIG_CGROUP_BPF */
985
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700986SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
987{
988 union bpf_attr attr = {};
989 int err;
990
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700991 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700992 return -EPERM;
993
994 if (!access_ok(VERIFY_READ, uattr, 1))
995 return -EFAULT;
996
997 if (size > PAGE_SIZE) /* silly large */
998 return -E2BIG;
999
1000 /* If we're handed a bigger struct than we know of,
1001 * ensure all the unknown bits are 0 - i.e. new
1002 * user-space does not rely on any kernel feature
1003 * extensions we dont know about yet.
1004 */
1005 if (size > sizeof(attr)) {
1006 unsigned char __user *addr;
1007 unsigned char __user *end;
1008 unsigned char val;
1009
1010 addr = (void __user *)uattr + sizeof(attr);
1011 end = (void __user *)uattr + size;
1012
1013 for (; addr < end; addr++) {
1014 err = get_user(val, addr);
1015 if (err)
1016 return err;
1017 if (val)
1018 return -E2BIG;
1019 }
1020 size = sizeof(attr);
1021 }
1022
1023 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1024 if (copy_from_user(&attr, uattr, size) != 0)
1025 return -EFAULT;
1026
1027 switch (cmd) {
1028 case BPF_MAP_CREATE:
1029 err = map_create(&attr);
1030 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001031 case BPF_MAP_LOOKUP_ELEM:
1032 err = map_lookup_elem(&attr);
1033 break;
1034 case BPF_MAP_UPDATE_ELEM:
1035 err = map_update_elem(&attr);
1036 break;
1037 case BPF_MAP_DELETE_ELEM:
1038 err = map_delete_elem(&attr);
1039 break;
1040 case BPF_MAP_GET_NEXT_KEY:
1041 err = map_get_next_key(&attr);
1042 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001043 case BPF_PROG_LOAD:
1044 err = bpf_prog_load(&attr);
1045 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001046 case BPF_OBJ_PIN:
1047 err = bpf_obj_pin(&attr);
1048 break;
1049 case BPF_OBJ_GET:
1050 err = bpf_obj_get(&attr);
1051 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001052
1053#ifdef CONFIG_CGROUP_BPF
1054 case BPF_PROG_ATTACH:
1055 err = bpf_prog_attach(&attr);
1056 break;
1057 case BPF_PROG_DETACH:
1058 err = bpf_prog_detach(&attr);
1059 break;
1060#endif
1061
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001062 default:
1063 err = -EINVAL;
1064 break;
1065 }
1066
1067 return err;
1068}