blob: cb17e1cd1d434dc2e052a2a9fb0aea967fcf4417 [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>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070026
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070027#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
33
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080034DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070035static DEFINE_IDR(prog_idr);
36static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070037static DEFINE_IDR(map_idr);
38static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080039
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070040int sysctl_unprivileged_bpf_disabled __read_mostly;
41
Johannes Berg40077e02017-04-11 15:34:58 +020042static const struct bpf_map_ops * const bpf_map_types[] = {
43#define BPF_PROG_TYPE(_id, _ops)
44#define BPF_MAP_TYPE(_id, _ops) \
45 [_id] = &_ops,
46#include <linux/bpf_types.h>
47#undef BPF_PROG_TYPE
48#undef BPF_MAP_TYPE
49};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070050
Mickaël Salaün752ba562017-08-07 20:45:20 +020051/*
52 * If we're handed a bigger struct than we know of, ensure all the unknown bits
53 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
54 * we don't know about yet.
55 *
56 * There is a ToCToU between this function call and the following
57 * copy_from_user() call. However, this is not a concern since this function is
58 * meant to be a future-proofing of bits.
59 */
Mickaël Salaün58291a72017-08-07 20:45:19 +020060static int check_uarg_tail_zero(void __user *uaddr,
61 size_t expected_size,
62 size_t actual_size)
63{
64 unsigned char __user *addr;
65 unsigned char __user *end;
66 unsigned char val;
67 int err;
68
Mickaël Salaün752ba562017-08-07 20:45:20 +020069 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
70 return -E2BIG;
71
72 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
73 return -EFAULT;
74
Mickaël Salaün58291a72017-08-07 20:45:19 +020075 if (actual_size <= expected_size)
76 return 0;
77
78 addr = uaddr + expected_size;
79 end = uaddr + actual_size;
80
81 for (; addr < end; addr++) {
82 err = get_user(val, addr);
83 if (err)
84 return err;
85 if (val)
86 return -E2BIG;
87 }
88
89 return 0;
90}
91
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070092static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
93{
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070094 struct bpf_map *map;
95
Johannes Berg40077e02017-04-11 15:34:58 +020096 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
97 !bpf_map_types[attr->map_type])
98 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070099
Johannes Berg40077e02017-04-11 15:34:58 +0200100 map = bpf_map_types[attr->map_type]->map_alloc(attr);
101 if (IS_ERR(map))
102 return map;
103 map->ops = bpf_map_types[attr->map_type];
104 map->map_type = attr->map_type;
105 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700106}
107
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700108void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100109{
110 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
111 * trigger under memory pressure as we really just want to
112 * fail instead.
113 */
114 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
115 void *area;
116
117 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700118 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100119 if (area != NULL)
120 return area;
121 }
122
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700123 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
124 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100125}
126
127void bpf_map_area_free(void *area)
128{
129 kvfree(area);
130}
131
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800132int bpf_map_precharge_memlock(u32 pages)
133{
134 struct user_struct *user = get_current_user();
135 unsigned long memlock_limit, cur;
136
137 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
138 cur = atomic_long_read(&user->locked_vm);
139 free_uid(user);
140 if (cur + pages > memlock_limit)
141 return -EPERM;
142 return 0;
143}
144
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700145static int bpf_map_charge_memlock(struct bpf_map *map)
146{
147 struct user_struct *user = get_current_user();
148 unsigned long memlock_limit;
149
150 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
151
152 atomic_long_add(map->pages, &user->locked_vm);
153
154 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
155 atomic_long_sub(map->pages, &user->locked_vm);
156 free_uid(user);
157 return -EPERM;
158 }
159 map->user = user;
160 return 0;
161}
162
163static void bpf_map_uncharge_memlock(struct bpf_map *map)
164{
165 struct user_struct *user = map->user;
166
167 atomic_long_sub(map->pages, &user->locked_vm);
168 free_uid(user);
169}
170
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700171static int bpf_map_alloc_id(struct bpf_map *map)
172{
173 int id;
174
175 spin_lock_bh(&map_idr_lock);
176 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
177 if (id > 0)
178 map->id = id;
179 spin_unlock_bh(&map_idr_lock);
180
181 if (WARN_ON_ONCE(!id))
182 return -ENOSPC;
183
184 return id > 0 ? 0 : id;
185}
186
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700187static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700188{
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700189 if (do_idr_lock)
190 spin_lock_bh(&map_idr_lock);
191 else
192 __acquire(&map_idr_lock);
193
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700194 idr_remove(&map_idr, map->id);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700195
196 if (do_idr_lock)
197 spin_unlock_bh(&map_idr_lock);
198 else
199 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700200}
201
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700202/* called from workqueue */
203static void bpf_map_free_deferred(struct work_struct *work)
204{
205 struct bpf_map *map = container_of(work, struct bpf_map, work);
206
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700207 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700208 /* implementation dependent freeing */
209 map->ops->map_free(map);
210}
211
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100212static void bpf_map_put_uref(struct bpf_map *map)
213{
214 if (atomic_dec_and_test(&map->usercnt)) {
215 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
216 bpf_fd_array_map_clear(map);
217 }
218}
219
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700220/* decrement map refcnt and schedule it for freeing via workqueue
221 * (unrelying map implementation ops->map_free() might sleep)
222 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700223static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700224{
225 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700226 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700227 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700228 INIT_WORK(&map->work, bpf_map_free_deferred);
229 schedule_work(&map->work);
230 }
231}
232
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700233void bpf_map_put(struct bpf_map *map)
234{
235 __bpf_map_put(map, true);
236}
237
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100238void bpf_map_put_with_uref(struct bpf_map *map)
239{
240 bpf_map_put_uref(map);
241 bpf_map_put(map);
242}
243
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700244static int bpf_map_release(struct inode *inode, struct file *filp)
245{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200246 struct bpf_map *map = filp->private_data;
247
248 if (map->ops->map_release)
249 map->ops->map_release(map, filp);
250
251 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700252 return 0;
253}
254
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100255#ifdef CONFIG_PROC_FS
256static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
257{
258 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100259 const struct bpf_array *array;
260 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200261 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100262
263 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
264 array = container_of(map, struct bpf_array, map);
265 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200266 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100267 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100268
269 seq_printf(m,
270 "map_type:\t%u\n"
271 "key_size:\t%u\n"
272 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100273 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100274 "map_flags:\t%#x\n"
275 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100276 map->map_type,
277 map->key_size,
278 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100279 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100280 map->map_flags,
281 map->pages * 1ULL << PAGE_SHIFT);
282
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200283 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100284 seq_printf(m, "owner_prog_type:\t%u\n",
285 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200286 seq_printf(m, "owner_jited:\t%u\n",
287 owner_jited);
288 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100289}
290#endif
291
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700292static const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100293#ifdef CONFIG_PROC_FS
294 .show_fdinfo = bpf_map_show_fdinfo,
295#endif
296 .release = bpf_map_release,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700297};
298
Daniel Borkmannb2197752015-10-29 14:58:09 +0100299int bpf_map_new_fd(struct bpf_map *map)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100300{
301 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
302 O_RDWR | O_CLOEXEC);
303}
304
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700305/* helper macro to check that unused fields 'union bpf_attr' are zero */
306#define CHECK_ATTR(CMD) \
307 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
308 sizeof(attr->CMD##_LAST_FIELD), 0, \
309 sizeof(*attr) - \
310 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
311 sizeof(attr->CMD##_LAST_FIELD)) != NULL
312
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700313#define BPF_MAP_CREATE_LAST_FIELD numa_node
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700314/* called via syscall */
315static int map_create(union bpf_attr *attr)
316{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700317 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700318 struct bpf_map *map;
319 int err;
320
321 err = CHECK_ATTR(BPF_MAP_CREATE);
322 if (err)
323 return -EINVAL;
324
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700325 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700326 ((unsigned int)numa_node >= nr_node_ids ||
327 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700328 return -EINVAL;
329
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700330 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
331 map = find_and_alloc_map(attr);
332 if (IS_ERR(map))
333 return PTR_ERR(map);
334
335 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100336 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700337
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700338 err = bpf_map_charge_memlock(map);
339 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100340 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700341
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700342 err = bpf_map_alloc_id(map);
343 if (err)
344 goto free_map;
345
Daniel Borkmannaa797812015-10-29 14:58:06 +0100346 err = bpf_map_new_fd(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700347 if (err < 0) {
348 /* failed to allocate fd.
349 * bpf_map_put() is needed because the above
350 * bpf_map_alloc_id() has published the map
351 * to the userspace and the userspace may
352 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
353 */
354 bpf_map_put(map);
355 return err;
356 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700357
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100358 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700359 return err;
360
361free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100362 bpf_map_uncharge_memlock(map);
363free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700364 map->ops->map_free(map);
365 return err;
366}
367
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700368/* if error is returned, fd is released.
369 * On success caller should complete fd access with matching fdput()
370 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100371struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700372{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700373 if (!f.file)
374 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700375 if (f.file->f_op != &bpf_map_fops) {
376 fdput(f);
377 return ERR_PTR(-EINVAL);
378 }
379
Daniel Borkmannc2101292015-10-29 14:58:07 +0100380 return f.file->private_data;
381}
382
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700383/* prog's and map's refcnt limit */
384#define BPF_MAX_REFCNT 32768
385
386struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100387{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700388 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
389 atomic_dec(&map->refcnt);
390 return ERR_PTR(-EBUSY);
391 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100392 if (uref)
393 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700394 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100395}
396
397struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100398{
399 struct fd f = fdget(ufd);
400 struct bpf_map *map;
401
402 map = __bpf_map_get(f);
403 if (IS_ERR(map))
404 return map;
405
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700406 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100407 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700408
409 return map;
410}
411
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700412/* map_idr_lock should have been held */
413static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
414 bool uref)
415{
416 int refold;
417
418 refold = __atomic_add_unless(&map->refcnt, 1, 0);
419
420 if (refold >= BPF_MAX_REFCNT) {
421 __bpf_map_put(map, false);
422 return ERR_PTR(-EBUSY);
423 }
424
425 if (!refold)
426 return ERR_PTR(-ENOENT);
427
428 if (uref)
429 atomic_inc(&map->usercnt);
430
431 return map;
432}
433
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800434int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
435{
436 return -ENOTSUPP;
437}
438
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700439/* last field in 'union bpf_attr' used by this command */
440#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
441
442static int map_lookup_elem(union bpf_attr *attr)
443{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100444 void __user *ukey = u64_to_user_ptr(attr->key);
445 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700446 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700447 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800448 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800449 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200450 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700451 int err;
452
453 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
454 return -EINVAL;
455
Daniel Borkmann592867b2015-09-08 18:00:09 +0200456 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100457 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700458 if (IS_ERR(map))
459 return PTR_ERR(map);
460
Al Viroe4448ed2017-05-13 18:43:00 -0400461 key = memdup_user(ukey, map->key_size);
462 if (IS_ERR(key)) {
463 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700464 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400465 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700466
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800467 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800468 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800469 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
470 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700471 else if (IS_FD_MAP(map))
472 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800473 else
474 value_size = map->value_size;
475
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800476 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800477 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700478 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800479 goto free_key;
480
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800481 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
482 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800483 err = bpf_percpu_hash_copy(map, key, value);
484 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
485 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800486 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
487 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700488 } else if (IS_FD_ARRAY(map)) {
489 err = bpf_fd_array_map_lookup_elem(map, key, value);
490 } else if (IS_FD_HASH(map)) {
491 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800492 } else {
493 rcu_read_lock();
494 ptr = map->ops->map_lookup_elem(map, key);
495 if (ptr)
496 memcpy(value, ptr, value_size);
497 rcu_read_unlock();
498 err = ptr ? 0 : -ENOENT;
499 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800500
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800501 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800502 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700503
504 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800505 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800506 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700507
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100508 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700509 err = 0;
510
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800511free_value:
512 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700513free_key:
514 kfree(key);
515err_put:
516 fdput(f);
517 return err;
518}
519
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800520#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700521
522static int map_update_elem(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 *uvalue = u64_to_user_ptr(attr->value);
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, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800529 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200530 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700531 int err;
532
533 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
534 return -EINVAL;
535
Daniel Borkmann592867b2015-09-08 18:00:09 +0200536 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100537 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700538 if (IS_ERR(map))
539 return PTR_ERR(map);
540
Al Viroe4448ed2017-05-13 18:43:00 -0400541 key = memdup_user(ukey, map->key_size);
542 if (IS_ERR(key)) {
543 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700544 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400545 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700546
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800547 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800548 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800549 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
550 value_size = round_up(map->value_size, 8) * num_possible_cpus();
551 else
552 value_size = map->value_size;
553
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700554 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800555 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700556 if (!value)
557 goto free_key;
558
559 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800560 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700561 goto free_value;
562
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800563 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
564 * inside bpf map update or delete otherwise deadlocks are possible
565 */
566 preempt_disable();
567 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800568 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
569 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800570 err = bpf_percpu_hash_update(map, key, value, attr->flags);
571 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
572 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200573 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700574 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700575 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
576 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200577 rcu_read_lock();
578 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
579 attr->flags);
580 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700581 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
582 rcu_read_lock();
583 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
584 attr->flags);
585 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800586 } else {
587 rcu_read_lock();
588 err = map->ops->map_update_elem(map, key, value, attr->flags);
589 rcu_read_unlock();
590 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800591 __this_cpu_dec(bpf_prog_active);
592 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700593
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100594 if (!err)
595 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700596free_value:
597 kfree(value);
598free_key:
599 kfree(key);
600err_put:
601 fdput(f);
602 return err;
603}
604
605#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
606
607static int map_delete_elem(union bpf_attr *attr)
608{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100609 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700610 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700611 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200612 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700613 void *key;
614 int err;
615
616 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
617 return -EINVAL;
618
Daniel Borkmann592867b2015-09-08 18:00:09 +0200619 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100620 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700621 if (IS_ERR(map))
622 return PTR_ERR(map);
623
Al Viroe4448ed2017-05-13 18:43:00 -0400624 key = memdup_user(ukey, map->key_size);
625 if (IS_ERR(key)) {
626 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700627 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400628 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700629
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800630 preempt_disable();
631 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700632 rcu_read_lock();
633 err = map->ops->map_delete_elem(map, key);
634 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800635 __this_cpu_dec(bpf_prog_active);
636 preempt_enable();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700637
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100638 if (!err)
639 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700640 kfree(key);
641err_put:
642 fdput(f);
643 return err;
644}
645
646/* last field in 'union bpf_attr' used by this command */
647#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
648
649static int map_get_next_key(union bpf_attr *attr)
650{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100651 void __user *ukey = u64_to_user_ptr(attr->key);
652 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700653 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700654 struct bpf_map *map;
655 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200656 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700657 int err;
658
659 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
660 return -EINVAL;
661
Daniel Borkmann592867b2015-09-08 18:00:09 +0200662 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100663 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700664 if (IS_ERR(map))
665 return PTR_ERR(map);
666
Teng Qin8fe45922017-04-24 19:00:37 -0700667 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400668 key = memdup_user(ukey, map->key_size);
669 if (IS_ERR(key)) {
670 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700671 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400672 }
Teng Qin8fe45922017-04-24 19:00:37 -0700673 } else {
674 key = NULL;
675 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700676
677 err = -ENOMEM;
678 next_key = kmalloc(map->key_size, GFP_USER);
679 if (!next_key)
680 goto free_key;
681
682 rcu_read_lock();
683 err = map->ops->map_get_next_key(map, key, next_key);
684 rcu_read_unlock();
685 if (err)
686 goto free_next_key;
687
688 err = -EFAULT;
689 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
690 goto free_next_key;
691
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100692 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700693 err = 0;
694
695free_next_key:
696 kfree(next_key);
697free_key:
698 kfree(key);
699err_put:
700 fdput(f);
701 return err;
702}
703
Johannes Bergbe9370a2017-04-11 15:34:57 +0200704static const struct bpf_verifier_ops * const bpf_prog_types[] = {
705#define BPF_PROG_TYPE(_id, _ops) \
706 [_id] = &_ops,
Johannes Berg40077e02017-04-11 15:34:58 +0200707#define BPF_MAP_TYPE(_id, _ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +0200708#include <linux/bpf_types.h>
709#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +0200710#undef BPF_MAP_TYPE
Johannes Bergbe9370a2017-04-11 15:34:57 +0200711};
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700712
713static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
714{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200715 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
716 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700717
Johannes Bergbe9370a2017-04-11 15:34:57 +0200718 prog->aux->ops = bpf_prog_types[type];
719 prog->type = type;
720 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700721}
722
723/* drop refcnt on maps used by eBPF program and free auxilary data */
724static void free_used_maps(struct bpf_prog_aux *aux)
725{
726 int i;
727
728 for (i = 0; i < aux->used_map_cnt; i++)
729 bpf_map_put(aux->used_maps[i]);
730
731 kfree(aux->used_maps);
732}
733
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100734int __bpf_prog_charge(struct user_struct *user, u32 pages)
735{
736 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
737 unsigned long user_bufs;
738
739 if (user) {
740 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
741 if (user_bufs > memlock_limit) {
742 atomic_long_sub(pages, &user->locked_vm);
743 return -EPERM;
744 }
745 }
746
747 return 0;
748}
749
750void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
751{
752 if (user)
753 atomic_long_sub(pages, &user->locked_vm);
754}
755
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700756static int bpf_prog_charge_memlock(struct bpf_prog *prog)
757{
758 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100759 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700760
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100761 ret = __bpf_prog_charge(user, prog->pages);
762 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700763 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100764 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700765 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100766
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700767 prog->aux->user = user;
768 return 0;
769}
770
771static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
772{
773 struct user_struct *user = prog->aux->user;
774
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100775 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700776 free_uid(user);
777}
778
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700779static int bpf_prog_alloc_id(struct bpf_prog *prog)
780{
781 int id;
782
783 spin_lock_bh(&prog_idr_lock);
784 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
785 if (id > 0)
786 prog->aux->id = id;
787 spin_unlock_bh(&prog_idr_lock);
788
789 /* id is in [1, INT_MAX) */
790 if (WARN_ON_ONCE(!id))
791 return -ENOSPC;
792
793 return id > 0 ? 0 : id;
794}
795
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700796static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700797{
798 /* cBPF to eBPF migrations are currently not in the idr store. */
799 if (!prog->aux->id)
800 return;
801
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700802 if (do_idr_lock)
803 spin_lock_bh(&prog_idr_lock);
804 else
805 __acquire(&prog_idr_lock);
806
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700807 idr_remove(&prog_idr, prog->aux->id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700808
809 if (do_idr_lock)
810 spin_unlock_bh(&prog_idr_lock);
811 else
812 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700813}
814
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200815static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700816{
817 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
818
819 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700820 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700821 bpf_prog_free(aux->prog);
822}
823
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700824static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700825{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100826 if (atomic_dec_and_test(&prog->aux->refcnt)) {
827 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700828 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700829 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann74451e662017-02-16 22:24:50 +0100830 bpf_prog_kallsyms_del(prog);
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200831 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100832 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700833}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700834
835void bpf_prog_put(struct bpf_prog *prog)
836{
837 __bpf_prog_put(prog, true);
838}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100839EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700840
841static int bpf_prog_release(struct inode *inode, struct file *filp)
842{
843 struct bpf_prog *prog = filp->private_data;
844
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200845 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700846 return 0;
847}
848
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100849#ifdef CONFIG_PROC_FS
850static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
851{
852 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100853 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100854
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100855 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100856 seq_printf(m,
857 "prog_type:\t%u\n"
858 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100859 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100860 "memlock:\t%llu\n",
861 prog->type,
862 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100863 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100864 prog->pages * 1ULL << PAGE_SHIFT);
865}
866#endif
867
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700868static const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +0100869#ifdef CONFIG_PROC_FS
870 .show_fdinfo = bpf_prog_show_fdinfo,
871#endif
872 .release = bpf_prog_release,
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700873};
874
Daniel Borkmannb2197752015-10-29 14:58:09 +0100875int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100876{
877 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
878 O_RDWR | O_CLOEXEC);
879}
880
Daniel Borkmann113214b2016-06-30 17:24:44 +0200881static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700882{
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700883 if (!f.file)
884 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700885 if (f.file->f_op != &bpf_prog_fops) {
886 fdput(f);
887 return ERR_PTR(-EINVAL);
888 }
889
Daniel Borkmannc2101292015-10-29 14:58:07 +0100890 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700891}
892
Brenden Blanco59d36562016-07-19 12:16:46 -0700893struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700894{
Brenden Blanco59d36562016-07-19 12:16:46 -0700895 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
896 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700897 return ERR_PTR(-EBUSY);
898 }
899 return prog;
900}
Brenden Blanco59d36562016-07-19 12:16:46 -0700901EXPORT_SYMBOL_GPL(bpf_prog_add);
902
Daniel Borkmannc5405942016-11-09 22:02:34 +0100903void bpf_prog_sub(struct bpf_prog *prog, int i)
904{
905 /* Only to be used for undoing previous bpf_prog_add() in some
906 * error path. We still know that another entity in our call
907 * path holds a reference to the program, thus atomic_sub() can
908 * be safely used in such cases!
909 */
910 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
911}
912EXPORT_SYMBOL_GPL(bpf_prog_sub);
913
Brenden Blanco59d36562016-07-19 12:16:46 -0700914struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
915{
916 return bpf_prog_add(prog, 1);
917}
Daniel Borkmann97bc4022016-11-19 01:45:00 +0100918EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700919
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700920/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -0700921struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700922{
923 int refold;
924
925 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
926
927 if (refold >= BPF_MAX_REFCNT) {
928 __bpf_prog_put(prog, false);
929 return ERR_PTR(-EBUSY);
930 }
931
932 if (!refold)
933 return ERR_PTR(-ENOENT);
934
935 return prog;
936}
John Fastabenda6f6df62017-08-15 22:32:22 -0700937EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700938
Daniel Borkmann113214b2016-06-30 17:24:44 +0200939static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700940{
941 struct fd f = fdget(ufd);
942 struct bpf_prog *prog;
943
Daniel Borkmann113214b2016-06-30 17:24:44 +0200944 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700945 if (IS_ERR(prog))
946 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200947 if (type && prog->type != *type) {
948 prog = ERR_PTR(-EINVAL);
949 goto out;
950 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700951
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700952 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +0200953out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700954 fdput(f);
955 return prog;
956}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200957
958struct bpf_prog *bpf_prog_get(u32 ufd)
959{
960 return __bpf_prog_get(ufd, NULL);
961}
962
963struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
964{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100965 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
966
967 if (!IS_ERR(prog))
968 trace_bpf_prog_get_type(prog);
969 return prog;
Daniel Borkmann113214b2016-06-30 17:24:44 +0200970}
971EXPORT_SYMBOL_GPL(bpf_prog_get_type);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700972
973/* last field in 'union bpf_attr' used by this command */
David S. Millere07b98d2017-05-10 11:38:07 -0700974#define BPF_PROG_LOAD_LAST_FIELD prog_flags
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700975
976static int bpf_prog_load(union bpf_attr *attr)
977{
978 enum bpf_prog_type type = attr->prog_type;
979 struct bpf_prog *prog;
980 int err;
981 char license[128];
982 bool is_gpl;
983
984 if (CHECK_ATTR(BPF_PROG_LOAD))
985 return -EINVAL;
986
David S. Millere07b98d2017-05-10 11:38:07 -0700987 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
988 return -EINVAL;
989
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700990 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100991 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700992 sizeof(license) - 1) < 0)
993 return -EFAULT;
994 license[sizeof(license) - 1] = 0;
995
996 /* eBPF programs must be GPL compatible to use GPL-ed functions */
997 is_gpl = license_is_gpl_compatible(license);
998
Daniel Borkmannef0915c2016-12-07 01:15:44 +0100999 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1000 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001001
Alexei Starovoitov25415172015-03-25 12:49:20 -07001002 if (type == BPF_PROG_TYPE_KPROBE &&
1003 attr->kern_version != LINUX_VERSION_CODE)
1004 return -EINVAL;
1005
Chenbo Feng80b7d812017-05-31 18:16:00 -07001006 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1007 type != BPF_PROG_TYPE_CGROUP_SKB &&
1008 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001009 return -EPERM;
1010
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001011 /* plain bpf_prog allocation */
1012 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1013 if (!prog)
1014 return -ENOMEM;
1015
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001016 err = bpf_prog_charge_memlock(prog);
1017 if (err)
1018 goto free_prog_nouncharge;
1019
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001020 prog->len = attr->insn_cnt;
1021
1022 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001023 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001024 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001025 goto free_prog;
1026
1027 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001028 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001029
1030 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001031 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001032
1033 /* find program type: socket_filter vs tracing_filter */
1034 err = find_prog_type(type, prog);
1035 if (err < 0)
1036 goto free_prog;
1037
1038 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001039 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001040 if (err < 0)
1041 goto free_used_maps;
1042
1043 /* eBPF program is ready to be JITed */
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001044 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001045 if (err < 0)
1046 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001047
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001048 err = bpf_prog_alloc_id(prog);
1049 if (err)
1050 goto free_used_maps;
1051
Daniel Borkmannaa797812015-10-29 14:58:06 +01001052 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001053 if (err < 0) {
1054 /* failed to allocate fd.
1055 * bpf_prog_put() is needed because the above
1056 * bpf_prog_alloc_id() has published the prog
1057 * to the userspace and the userspace may
1058 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1059 */
1060 bpf_prog_put(prog);
1061 return err;
1062 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001063
Daniel Borkmann74451e662017-02-16 22:24:50 +01001064 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001065 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001066 return err;
1067
1068free_used_maps:
1069 free_used_maps(prog->aux);
1070free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001071 bpf_prog_uncharge_memlock(prog);
1072free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001073 bpf_prog_free(prog);
1074 return err;
1075}
1076
Daniel Borkmannb2197752015-10-29 14:58:09 +01001077#define BPF_OBJ_LAST_FIELD bpf_fd
1078
1079static int bpf_obj_pin(const union bpf_attr *attr)
1080{
1081 if (CHECK_ATTR(BPF_OBJ))
1082 return -EINVAL;
1083
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001084 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001085}
1086
1087static int bpf_obj_get(const union bpf_attr *attr)
1088{
1089 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
1090 return -EINVAL;
1091
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001092 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001093}
1094
Daniel Mackf4324552016-11-23 16:52:27 +01001095#ifdef CONFIG_CGROUP_BPF
1096
John Fastabend464bc0f2017-08-28 07:10:04 -07001097#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001098
John Fastabend5a67da22017-09-08 14:00:49 -07001099static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001100{
John Fastabend5a67da22017-09-08 14:00:49 -07001101 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001102 int ufd = attr->target_fd;
1103 struct bpf_map *map;
1104 struct fd f;
1105 int err;
1106
1107 f = fdget(ufd);
1108 map = __bpf_map_get(f);
1109 if (IS_ERR(map))
1110 return PTR_ERR(map);
1111
John Fastabend5a67da22017-09-08 14:00:49 -07001112 if (attach) {
1113 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1114 BPF_PROG_TYPE_SK_SKB);
1115 if (IS_ERR(prog)) {
1116 fdput(f);
1117 return PTR_ERR(prog);
1118 }
John Fastabend174a79f2017-08-15 22:32:47 -07001119 }
1120
John Fastabend5a67da22017-09-08 14:00:49 -07001121 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001122 if (err) {
1123 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001124 if (prog)
1125 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001126 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001127 }
1128
1129 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001130 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001131}
Daniel Mackf4324552016-11-23 16:52:27 +01001132
1133static int bpf_prog_attach(const union bpf_attr *attr)
1134{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001135 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001136 struct bpf_prog *prog;
1137 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001138 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001139
1140 if (!capable(CAP_NET_ADMIN))
1141 return -EPERM;
1142
1143 if (CHECK_ATTR(BPF_PROG_ATTACH))
1144 return -EINVAL;
1145
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001146 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
1147 return -EINVAL;
1148
Daniel Mackf4324552016-11-23 16:52:27 +01001149 switch (attr->attach_type) {
1150 case BPF_CGROUP_INET_INGRESS:
1151 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001152 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001153 break;
David Ahern610236582016-12-01 08:48:04 -08001154 case BPF_CGROUP_INET_SOCK_CREATE:
1155 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1156 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001157 case BPF_CGROUP_SOCK_OPS:
1158 ptype = BPF_PROG_TYPE_SOCK_OPS;
1159 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001160 case BPF_SK_SKB_STREAM_PARSER:
1161 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend5a67da22017-09-08 14:00:49 -07001162 return sockmap_get_from_fd(attr, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001163 default:
1164 return -EINVAL;
1165 }
1166
David Ahernb2cd1252016-12-01 08:48:03 -08001167 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1168 if (IS_ERR(prog))
1169 return PTR_ERR(prog);
1170
1171 cgrp = cgroup_get_from_fd(attr->target_fd);
1172 if (IS_ERR(cgrp)) {
1173 bpf_prog_put(prog);
1174 return PTR_ERR(cgrp);
1175 }
1176
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001177 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
1178 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
1179 if (ret)
1180 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001181 cgroup_put(cgrp);
1182
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001183 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001184}
1185
1186#define BPF_PROG_DETACH_LAST_FIELD attach_type
1187
1188static int bpf_prog_detach(const union bpf_attr *attr)
1189{
1190 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001191 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001192
1193 if (!capable(CAP_NET_ADMIN))
1194 return -EPERM;
1195
1196 if (CHECK_ATTR(BPF_PROG_DETACH))
1197 return -EINVAL;
1198
1199 switch (attr->attach_type) {
1200 case BPF_CGROUP_INET_INGRESS:
1201 case BPF_CGROUP_INET_EGRESS:
David Ahern610236582016-12-01 08:48:04 -08001202 case BPF_CGROUP_INET_SOCK_CREATE:
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001203 case BPF_CGROUP_SOCK_OPS:
Daniel Mackf4324552016-11-23 16:52:27 +01001204 cgrp = cgroup_get_from_fd(attr->target_fd);
1205 if (IS_ERR(cgrp))
1206 return PTR_ERR(cgrp);
1207
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001208 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001209 cgroup_put(cgrp);
1210 break;
John Fastabend5a67da22017-09-08 14:00:49 -07001211 case BPF_SK_SKB_STREAM_PARSER:
1212 case BPF_SK_SKB_STREAM_VERDICT:
1213 ret = sockmap_get_from_fd(attr, false);
1214 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001215 default:
1216 return -EINVAL;
1217 }
1218
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001219 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001220}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001221
Daniel Mackf4324552016-11-23 16:52:27 +01001222#endif /* CONFIG_CGROUP_BPF */
1223
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001224#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1225
1226static int bpf_prog_test_run(const union bpf_attr *attr,
1227 union bpf_attr __user *uattr)
1228{
1229 struct bpf_prog *prog;
1230 int ret = -ENOTSUPP;
1231
1232 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1233 return -EINVAL;
1234
1235 prog = bpf_prog_get(attr->test.prog_fd);
1236 if (IS_ERR(prog))
1237 return PTR_ERR(prog);
1238
1239 if (prog->aux->ops->test_run)
1240 ret = prog->aux->ops->test_run(prog, attr, uattr);
1241
1242 bpf_prog_put(prog);
1243 return ret;
1244}
1245
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001246#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1247
1248static int bpf_obj_get_next_id(const union bpf_attr *attr,
1249 union bpf_attr __user *uattr,
1250 struct idr *idr,
1251 spinlock_t *lock)
1252{
1253 u32 next_id = attr->start_id;
1254 int err = 0;
1255
1256 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1257 return -EINVAL;
1258
1259 if (!capable(CAP_SYS_ADMIN))
1260 return -EPERM;
1261
1262 next_id++;
1263 spin_lock_bh(lock);
1264 if (!idr_get_next(idr, &next_id))
1265 err = -ENOENT;
1266 spin_unlock_bh(lock);
1267
1268 if (!err)
1269 err = put_user(next_id, &uattr->next_id);
1270
1271 return err;
1272}
1273
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001274#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1275
1276static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1277{
1278 struct bpf_prog *prog;
1279 u32 id = attr->prog_id;
1280 int fd;
1281
1282 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1283 return -EINVAL;
1284
1285 if (!capable(CAP_SYS_ADMIN))
1286 return -EPERM;
1287
1288 spin_lock_bh(&prog_idr_lock);
1289 prog = idr_find(&prog_idr, id);
1290 if (prog)
1291 prog = bpf_prog_inc_not_zero(prog);
1292 else
1293 prog = ERR_PTR(-ENOENT);
1294 spin_unlock_bh(&prog_idr_lock);
1295
1296 if (IS_ERR(prog))
1297 return PTR_ERR(prog);
1298
1299 fd = bpf_prog_new_fd(prog);
1300 if (fd < 0)
1301 bpf_prog_put(prog);
1302
1303 return fd;
1304}
1305
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001306#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1307
1308static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1309{
1310 struct bpf_map *map;
1311 u32 id = attr->map_id;
1312 int fd;
1313
1314 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
1315 return -EINVAL;
1316
1317 if (!capable(CAP_SYS_ADMIN))
1318 return -EPERM;
1319
1320 spin_lock_bh(&map_idr_lock);
1321 map = idr_find(&map_idr, id);
1322 if (map)
1323 map = bpf_map_inc_not_zero(map, true);
1324 else
1325 map = ERR_PTR(-ENOENT);
1326 spin_unlock_bh(&map_idr_lock);
1327
1328 if (IS_ERR(map))
1329 return PTR_ERR(map);
1330
1331 fd = bpf_map_new_fd(map);
1332 if (fd < 0)
1333 bpf_map_put(map);
1334
1335 return fd;
1336}
1337
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001338static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1339 const union bpf_attr *attr,
1340 union bpf_attr __user *uattr)
1341{
1342 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1343 struct bpf_prog_info info = {};
1344 u32 info_len = attr->info.info_len;
1345 char __user *uinsns;
1346 u32 ulen;
1347 int err;
1348
1349 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1350 if (err)
1351 return err;
1352 info_len = min_t(u32, sizeof(info), info_len);
1353
1354 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001355 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001356
1357 info.type = prog->type;
1358 info.id = prog->aux->id;
1359
1360 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1361
1362 if (!capable(CAP_SYS_ADMIN)) {
1363 info.jited_prog_len = 0;
1364 info.xlated_prog_len = 0;
1365 goto done;
1366 }
1367
1368 ulen = info.jited_prog_len;
1369 info.jited_prog_len = prog->jited_len;
1370 if (info.jited_prog_len && ulen) {
1371 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1372 ulen = min_t(u32, info.jited_prog_len, ulen);
1373 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1374 return -EFAULT;
1375 }
1376
1377 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001378 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001379 if (info.xlated_prog_len && ulen) {
1380 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1381 ulen = min_t(u32, info.xlated_prog_len, ulen);
1382 if (copy_to_user(uinsns, prog->insnsi, ulen))
1383 return -EFAULT;
1384 }
1385
1386done:
1387 if (copy_to_user(uinfo, &info, info_len) ||
1388 put_user(info_len, &uattr->info.info_len))
1389 return -EFAULT;
1390
1391 return 0;
1392}
1393
1394static int bpf_map_get_info_by_fd(struct bpf_map *map,
1395 const union bpf_attr *attr,
1396 union bpf_attr __user *uattr)
1397{
1398 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1399 struct bpf_map_info info = {};
1400 u32 info_len = attr->info.info_len;
1401 int err;
1402
1403 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1404 if (err)
1405 return err;
1406 info_len = min_t(u32, sizeof(info), info_len);
1407
1408 info.type = map->map_type;
1409 info.id = map->id;
1410 info.key_size = map->key_size;
1411 info.value_size = map->value_size;
1412 info.max_entries = map->max_entries;
1413 info.map_flags = map->map_flags;
1414
1415 if (copy_to_user(uinfo, &info, info_len) ||
1416 put_user(info_len, &uattr->info.info_len))
1417 return -EFAULT;
1418
1419 return 0;
1420}
1421
1422#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1423
1424static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1425 union bpf_attr __user *uattr)
1426{
1427 int ufd = attr->info.bpf_fd;
1428 struct fd f;
1429 int err;
1430
1431 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1432 return -EINVAL;
1433
1434 f = fdget(ufd);
1435 if (!f.file)
1436 return -EBADFD;
1437
1438 if (f.file->f_op == &bpf_prog_fops)
1439 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1440 uattr);
1441 else if (f.file->f_op == &bpf_map_fops)
1442 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1443 uattr);
1444 else
1445 err = -EINVAL;
1446
1447 fdput(f);
1448 return err;
1449}
1450
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001451SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1452{
1453 union bpf_attr attr = {};
1454 int err;
1455
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001456 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001457 return -EPERM;
1458
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001459 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1460 if (err)
1461 return err;
1462 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001463
1464 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1465 if (copy_from_user(&attr, uattr, size) != 0)
1466 return -EFAULT;
1467
1468 switch (cmd) {
1469 case BPF_MAP_CREATE:
1470 err = map_create(&attr);
1471 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001472 case BPF_MAP_LOOKUP_ELEM:
1473 err = map_lookup_elem(&attr);
1474 break;
1475 case BPF_MAP_UPDATE_ELEM:
1476 err = map_update_elem(&attr);
1477 break;
1478 case BPF_MAP_DELETE_ELEM:
1479 err = map_delete_elem(&attr);
1480 break;
1481 case BPF_MAP_GET_NEXT_KEY:
1482 err = map_get_next_key(&attr);
1483 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001484 case BPF_PROG_LOAD:
1485 err = bpf_prog_load(&attr);
1486 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001487 case BPF_OBJ_PIN:
1488 err = bpf_obj_pin(&attr);
1489 break;
1490 case BPF_OBJ_GET:
1491 err = bpf_obj_get(&attr);
1492 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001493#ifdef CONFIG_CGROUP_BPF
1494 case BPF_PROG_ATTACH:
1495 err = bpf_prog_attach(&attr);
1496 break;
1497 case BPF_PROG_DETACH:
1498 err = bpf_prog_detach(&attr);
1499 break;
1500#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001501 case BPF_PROG_TEST_RUN:
1502 err = bpf_prog_test_run(&attr, uattr);
1503 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001504 case BPF_PROG_GET_NEXT_ID:
1505 err = bpf_obj_get_next_id(&attr, uattr,
1506 &prog_idr, &prog_idr_lock);
1507 break;
1508 case BPF_MAP_GET_NEXT_ID:
1509 err = bpf_obj_get_next_id(&attr, uattr,
1510 &map_idr, &map_idr_lock);
1511 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001512 case BPF_PROG_GET_FD_BY_ID:
1513 err = bpf_prog_get_fd_by_id(&attr);
1514 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001515 case BPF_MAP_GET_FD_BY_ID:
1516 err = bpf_map_get_fd_by_id(&attr);
1517 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001518 case BPF_OBJ_GET_INFO_BY_FD:
1519 err = bpf_obj_get_info_by_fd(&attr, uattr);
1520 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001521 default:
1522 err = -EINVAL;
1523 break;
1524 }
1525
1526 return err;
1527}