blob: cd8ebadc66ebd133209c461cac07c106db229cfe [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>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070014#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070015#include <linux/syscalls.h>
16#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010017#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010018#include <linux/vmalloc.h>
19#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070020#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070021#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070022#include <linux/license.h>
23#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070024#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010025#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070026#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070027#include <linux/cred.h>
28#include <linux/timekeeping.h>
29#include <linux/ctype.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070030
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070031#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
34 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
35#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
36#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
37
Chenbo Feng6e71b042017-10-18 13:00:22 -070038#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
39
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080040DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070041static DEFINE_IDR(prog_idr);
42static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070043static DEFINE_IDR(map_idr);
44static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080045
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070046int sysctl_unprivileged_bpf_disabled __read_mostly;
47
Johannes Berg40077e02017-04-11 15:34:58 +020048static const struct bpf_map_ops * const bpf_map_types[] = {
49#define BPF_PROG_TYPE(_id, _ops)
50#define BPF_MAP_TYPE(_id, _ops) \
51 [_id] = &_ops,
52#include <linux/bpf_types.h>
53#undef BPF_PROG_TYPE
54#undef BPF_MAP_TYPE
55};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070056
Mickaël Salaün752ba562017-08-07 20:45:20 +020057/*
58 * If we're handed a bigger struct than we know of, ensure all the unknown bits
59 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
60 * we don't know about yet.
61 *
62 * There is a ToCToU between this function call and the following
63 * copy_from_user() call. However, this is not a concern since this function is
64 * meant to be a future-proofing of bits.
65 */
Mickaël Salaün58291a72017-08-07 20:45:19 +020066static int check_uarg_tail_zero(void __user *uaddr,
67 size_t expected_size,
68 size_t actual_size)
69{
70 unsigned char __user *addr;
71 unsigned char __user *end;
72 unsigned char val;
73 int err;
74
Mickaël Salaün752ba562017-08-07 20:45:20 +020075 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
76 return -E2BIG;
77
78 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
79 return -EFAULT;
80
Mickaël Salaün58291a72017-08-07 20:45:19 +020081 if (actual_size <= expected_size)
82 return 0;
83
84 addr = uaddr + expected_size;
85 end = uaddr + actual_size;
86
87 for (; addr < end; addr++) {
88 err = get_user(val, addr);
89 if (err)
90 return err;
91 if (val)
92 return -E2BIG;
93 }
94
95 return 0;
96}
97
Jakub Kicinskia3884572018-01-11 20:29:09 -080098const struct bpf_map_ops bpf_map_offload_ops = {
99 .map_alloc = bpf_map_offload_map_alloc,
100 .map_free = bpf_map_offload_map_free,
101};
102
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700103static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
104{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800105 const struct bpf_map_ops *ops;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700106 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800107 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700108
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800109 if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
110 return ERR_PTR(-EINVAL);
111 ops = bpf_map_types[attr->map_type];
112 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200113 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700114
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800115 if (ops->map_alloc_check) {
116 err = ops->map_alloc_check(attr);
117 if (err)
118 return ERR_PTR(err);
119 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800120 if (attr->map_ifindex)
121 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800122 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200123 if (IS_ERR(map))
124 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800125 map->ops = ops;
Johannes Berg40077e02017-04-11 15:34:58 +0200126 map->map_type = attr->map_type;
127 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700128}
129
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700130void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100131{
132 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
133 * trigger under memory pressure as we really just want to
134 * fail instead.
135 */
136 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
137 void *area;
138
139 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700140 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100141 if (area != NULL)
142 return area;
143 }
144
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700145 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
146 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100147}
148
149void bpf_map_area_free(void *area)
150{
151 kvfree(area);
152}
153
Jakub Kicinskibd475642018-01-11 20:29:06 -0800154void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
155{
156 map->map_type = attr->map_type;
157 map->key_size = attr->key_size;
158 map->value_size = attr->value_size;
159 map->max_entries = attr->max_entries;
160 map->map_flags = attr->map_flags;
161 map->numa_node = bpf_map_attr_numa_node(attr);
162}
163
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800164int bpf_map_precharge_memlock(u32 pages)
165{
166 struct user_struct *user = get_current_user();
167 unsigned long memlock_limit, cur;
168
169 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
170 cur = atomic_long_read(&user->locked_vm);
171 free_uid(user);
172 if (cur + pages > memlock_limit)
173 return -EPERM;
174 return 0;
175}
176
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700177static int bpf_map_charge_memlock(struct bpf_map *map)
178{
179 struct user_struct *user = get_current_user();
180 unsigned long memlock_limit;
181
182 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
183
184 atomic_long_add(map->pages, &user->locked_vm);
185
186 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
187 atomic_long_sub(map->pages, &user->locked_vm);
188 free_uid(user);
189 return -EPERM;
190 }
191 map->user = user;
192 return 0;
193}
194
195static void bpf_map_uncharge_memlock(struct bpf_map *map)
196{
197 struct user_struct *user = map->user;
198
199 atomic_long_sub(map->pages, &user->locked_vm);
200 free_uid(user);
201}
202
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700203static int bpf_map_alloc_id(struct bpf_map *map)
204{
205 int id;
206
Shaohua Lib76354c2018-03-27 11:53:21 -0700207 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700208 spin_lock_bh(&map_idr_lock);
209 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
210 if (id > 0)
211 map->id = id;
212 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700213 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700214
215 if (WARN_ON_ONCE(!id))
216 return -ENOSPC;
217
218 return id > 0 ? 0 : id;
219}
220
Jakub Kicinskia3884572018-01-11 20:29:09 -0800221void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700222{
Eric Dumazet930651a2017-09-19 09:15:59 -0700223 unsigned long flags;
224
Jakub Kicinskia3884572018-01-11 20:29:09 -0800225 /* Offloaded maps are removed from the IDR store when their device
226 * disappears - even if someone holds an fd to them they are unusable,
227 * the memory is gone, all ops will fail; they are simply waiting for
228 * refcnt to drop to be freed.
229 */
230 if (!map->id)
231 return;
232
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700233 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700234 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700235 else
236 __acquire(&map_idr_lock);
237
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700238 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800239 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700240
241 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700242 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700243 else
244 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700245}
246
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700247/* called from workqueue */
248static void bpf_map_free_deferred(struct work_struct *work)
249{
250 struct bpf_map *map = container_of(work, struct bpf_map, work);
251
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700252 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700253 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700254 /* implementation dependent freeing */
255 map->ops->map_free(map);
256}
257
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100258static void bpf_map_put_uref(struct bpf_map *map)
259{
260 if (atomic_dec_and_test(&map->usercnt)) {
261 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
262 bpf_fd_array_map_clear(map);
263 }
264}
265
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700266/* decrement map refcnt and schedule it for freeing via workqueue
267 * (unrelying map implementation ops->map_free() might sleep)
268 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700269static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700270{
271 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700272 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700273 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700274 INIT_WORK(&map->work, bpf_map_free_deferred);
275 schedule_work(&map->work);
276 }
277}
278
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700279void bpf_map_put(struct bpf_map *map)
280{
281 __bpf_map_put(map, true);
282}
283
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100284void bpf_map_put_with_uref(struct bpf_map *map)
285{
286 bpf_map_put_uref(map);
287 bpf_map_put(map);
288}
289
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700290static int bpf_map_release(struct inode *inode, struct file *filp)
291{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200292 struct bpf_map *map = filp->private_data;
293
294 if (map->ops->map_release)
295 map->ops->map_release(map, filp);
296
297 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700298 return 0;
299}
300
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100301#ifdef CONFIG_PROC_FS
302static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
303{
304 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100305 const struct bpf_array *array;
306 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200307 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100308
309 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
310 array = container_of(map, struct bpf_array, map);
311 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200312 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100313 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100314
315 seq_printf(m,
316 "map_type:\t%u\n"
317 "key_size:\t%u\n"
318 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100319 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100320 "map_flags:\t%#x\n"
321 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100322 map->map_type,
323 map->key_size,
324 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100325 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100326 map->map_flags,
327 map->pages * 1ULL << PAGE_SHIFT);
328
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200329 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100330 seq_printf(m, "owner_prog_type:\t%u\n",
331 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200332 seq_printf(m, "owner_jited:\t%u\n",
333 owner_jited);
334 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100335}
336#endif
337
Chenbo Feng6e71b042017-10-18 13:00:22 -0700338static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
339 loff_t *ppos)
340{
341 /* We need this handler such that alloc_file() enables
342 * f_mode with FMODE_CAN_READ.
343 */
344 return -EINVAL;
345}
346
347static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
348 size_t siz, loff_t *ppos)
349{
350 /* We need this handler such that alloc_file() enables
351 * f_mode with FMODE_CAN_WRITE.
352 */
353 return -EINVAL;
354}
355
Chenbo Fengf66e4482017-10-18 13:00:26 -0700356const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100357#ifdef CONFIG_PROC_FS
358 .show_fdinfo = bpf_map_show_fdinfo,
359#endif
360 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700361 .read = bpf_dummy_read,
362 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700363};
364
Chenbo Feng6e71b042017-10-18 13:00:22 -0700365int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100366{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700367 int ret;
368
369 ret = security_bpf_map(map, OPEN_FMODE(flags));
370 if (ret < 0)
371 return ret;
372
Daniel Borkmannaa797812015-10-29 14:58:06 +0100373 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700374 flags | O_CLOEXEC);
375}
376
377int bpf_get_file_flag(int flags)
378{
379 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
380 return -EINVAL;
381 if (flags & BPF_F_RDONLY)
382 return O_RDONLY;
383 if (flags & BPF_F_WRONLY)
384 return O_WRONLY;
385 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100386}
387
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700388/* helper macro to check that unused fields 'union bpf_attr' are zero */
389#define CHECK_ATTR(CMD) \
390 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
391 sizeof(attr->CMD##_LAST_FIELD), 0, \
392 sizeof(*attr) - \
393 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
394 sizeof(attr->CMD##_LAST_FIELD)) != NULL
395
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700396/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
397 * Return 0 on success and < 0 on error.
398 */
399static int bpf_obj_name_cpy(char *dst, const char *src)
400{
401 const char *end = src + BPF_OBJ_NAME_LEN;
402
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700403 memset(dst, 0, BPF_OBJ_NAME_LEN);
404
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700405 /* Copy all isalnum() and '_' char */
406 while (src < end && *src) {
407 if (!isalnum(*src) && *src != '_')
408 return -EINVAL;
409 *dst++ = *src++;
410 }
411
412 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
413 if (src == end)
414 return -EINVAL;
415
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700416 return 0;
417}
418
Jakub Kicinskia3884572018-01-11 20:29:09 -0800419#define BPF_MAP_CREATE_LAST_FIELD map_ifindex
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700420/* called via syscall */
421static int map_create(union bpf_attr *attr)
422{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700423 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700424 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700425 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700426 int err;
427
428 err = CHECK_ATTR(BPF_MAP_CREATE);
429 if (err)
430 return -EINVAL;
431
Chenbo Feng6e71b042017-10-18 13:00:22 -0700432 f_flags = bpf_get_file_flag(attr->map_flags);
433 if (f_flags < 0)
434 return f_flags;
435
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700436 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700437 ((unsigned int)numa_node >= nr_node_ids ||
438 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700439 return -EINVAL;
440
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700441 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
442 map = find_and_alloc_map(attr);
443 if (IS_ERR(map))
444 return PTR_ERR(map);
445
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700446 err = bpf_obj_name_cpy(map->name, attr->map_name);
447 if (err)
448 goto free_map_nouncharge;
449
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700450 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100451 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700452
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700453 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700454 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100455 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700456
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700457 err = bpf_map_charge_memlock(map);
458 if (err)
459 goto free_map_sec;
460
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700461 err = bpf_map_alloc_id(map);
462 if (err)
463 goto free_map;
464
Chenbo Feng6e71b042017-10-18 13:00:22 -0700465 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700466 if (err < 0) {
467 /* failed to allocate fd.
468 * bpf_map_put() is needed because the above
469 * bpf_map_alloc_id() has published the map
470 * to the userspace and the userspace may
471 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
472 */
473 bpf_map_put(map);
474 return err;
475 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700476
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100477 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700478 return err;
479
480free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100481 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700482free_map_sec:
483 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100484free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700485 map->ops->map_free(map);
486 return err;
487}
488
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700489/* if error is returned, fd is released.
490 * On success caller should complete fd access with matching fdput()
491 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100492struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700493{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700494 if (!f.file)
495 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700496 if (f.file->f_op != &bpf_map_fops) {
497 fdput(f);
498 return ERR_PTR(-EINVAL);
499 }
500
Daniel Borkmannc2101292015-10-29 14:58:07 +0100501 return f.file->private_data;
502}
503
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700504/* prog's and map's refcnt limit */
505#define BPF_MAX_REFCNT 32768
506
507struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100508{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700509 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
510 atomic_dec(&map->refcnt);
511 return ERR_PTR(-EBUSY);
512 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100513 if (uref)
514 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700515 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100516}
517
518struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100519{
520 struct fd f = fdget(ufd);
521 struct bpf_map *map;
522
523 map = __bpf_map_get(f);
524 if (IS_ERR(map))
525 return map;
526
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700527 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100528 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700529
530 return map;
531}
532
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700533/* map_idr_lock should have been held */
534static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
535 bool uref)
536{
537 int refold;
538
539 refold = __atomic_add_unless(&map->refcnt, 1, 0);
540
541 if (refold >= BPF_MAX_REFCNT) {
542 __bpf_map_put(map, false);
543 return ERR_PTR(-EBUSY);
544 }
545
546 if (!refold)
547 return ERR_PTR(-ENOENT);
548
549 if (uref)
550 atomic_inc(&map->usercnt);
551
552 return map;
553}
554
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800555int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
556{
557 return -ENOTSUPP;
558}
559
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700560/* last field in 'union bpf_attr' used by this command */
561#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
562
563static int map_lookup_elem(union bpf_attr *attr)
564{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100565 void __user *ukey = u64_to_user_ptr(attr->key);
566 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700567 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700568 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800569 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800570 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200571 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700572 int err;
573
574 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
575 return -EINVAL;
576
Daniel Borkmann592867b2015-09-08 18:00:09 +0200577 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100578 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700579 if (IS_ERR(map))
580 return PTR_ERR(map);
581
Chenbo Feng6e71b042017-10-18 13:00:22 -0700582 if (!(f.file->f_mode & FMODE_CAN_READ)) {
583 err = -EPERM;
584 goto err_put;
585 }
586
Al Viroe4448ed2017-05-13 18:43:00 -0400587 key = memdup_user(ukey, map->key_size);
588 if (IS_ERR(key)) {
589 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700590 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400591 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700592
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800593 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800594 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800595 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
596 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700597 else if (IS_FD_MAP(map))
598 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800599 else
600 value_size = map->value_size;
601
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800602 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800603 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700604 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800605 goto free_key;
606
Jakub Kicinskia3884572018-01-11 20:29:09 -0800607 if (bpf_map_is_dev_bound(map)) {
608 err = bpf_map_offload_lookup_elem(map, key, value);
609 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
610 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800611 err = bpf_percpu_hash_copy(map, key, value);
612 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
613 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800614 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
615 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700616 } else if (IS_FD_ARRAY(map)) {
617 err = bpf_fd_array_map_lookup_elem(map, key, value);
618 } else if (IS_FD_HASH(map)) {
619 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800620 } else {
621 rcu_read_lock();
622 ptr = map->ops->map_lookup_elem(map, key);
623 if (ptr)
624 memcpy(value, ptr, value_size);
625 rcu_read_unlock();
626 err = ptr ? 0 : -ENOENT;
627 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800628
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800629 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800630 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700631
632 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800633 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800634 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700635
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100636 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700637 err = 0;
638
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800639free_value:
640 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700641free_key:
642 kfree(key);
643err_put:
644 fdput(f);
645 return err;
646}
647
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800648#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700649
650static int map_update_elem(union bpf_attr *attr)
651{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100652 void __user *ukey = u64_to_user_ptr(attr->key);
653 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700654 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700655 struct bpf_map *map;
656 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800657 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200658 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700659 int err;
660
661 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
662 return -EINVAL;
663
Daniel Borkmann592867b2015-09-08 18:00:09 +0200664 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100665 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700666 if (IS_ERR(map))
667 return PTR_ERR(map);
668
Chenbo Feng6e71b042017-10-18 13:00:22 -0700669 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
670 err = -EPERM;
671 goto err_put;
672 }
673
Al Viroe4448ed2017-05-13 18:43:00 -0400674 key = memdup_user(ukey, map->key_size);
675 if (IS_ERR(key)) {
676 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700677 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400678 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700679
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800680 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800681 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800682 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
683 value_size = round_up(map->value_size, 8) * num_possible_cpus();
684 else
685 value_size = map->value_size;
686
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700687 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800688 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700689 if (!value)
690 goto free_key;
691
692 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800693 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700694 goto free_value;
695
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200696 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800697 if (bpf_map_is_dev_bound(map)) {
698 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
699 goto out;
700 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200701 err = map->ops->map_update_elem(map, key, value, attr->flags);
702 goto out;
703 }
704
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800705 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
706 * inside bpf map update or delete otherwise deadlocks are possible
707 */
708 preempt_disable();
709 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800710 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
711 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800712 err = bpf_percpu_hash_update(map, key, value, attr->flags);
713 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
714 err = bpf_percpu_array_update(map, key, value, attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100715 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200716 rcu_read_lock();
717 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
718 attr->flags);
719 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700720 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
721 rcu_read_lock();
722 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
723 attr->flags);
724 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800725 } else {
726 rcu_read_lock();
727 err = map->ops->map_update_elem(map, key, value, attr->flags);
728 rcu_read_unlock();
729 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800730 __this_cpu_dec(bpf_prog_active);
731 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200732out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100733 if (!err)
734 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700735free_value:
736 kfree(value);
737free_key:
738 kfree(key);
739err_put:
740 fdput(f);
741 return err;
742}
743
744#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
745
746static int map_delete_elem(union bpf_attr *attr)
747{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100748 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700749 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700750 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200751 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700752 void *key;
753 int err;
754
755 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
756 return -EINVAL;
757
Daniel Borkmann592867b2015-09-08 18:00:09 +0200758 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100759 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700760 if (IS_ERR(map))
761 return PTR_ERR(map);
762
Chenbo Feng6e71b042017-10-18 13:00:22 -0700763 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
764 err = -EPERM;
765 goto err_put;
766 }
767
Al Viroe4448ed2017-05-13 18:43:00 -0400768 key = memdup_user(ukey, map->key_size);
769 if (IS_ERR(key)) {
770 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700771 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400772 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700773
Jakub Kicinskia3884572018-01-11 20:29:09 -0800774 if (bpf_map_is_dev_bound(map)) {
775 err = bpf_map_offload_delete_elem(map, key);
776 goto out;
777 }
778
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800779 preempt_disable();
780 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700781 rcu_read_lock();
782 err = map->ops->map_delete_elem(map, key);
783 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800784 __this_cpu_dec(bpf_prog_active);
785 preempt_enable();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800786out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100787 if (!err)
788 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700789 kfree(key);
790err_put:
791 fdput(f);
792 return err;
793}
794
795/* last field in 'union bpf_attr' used by this command */
796#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
797
798static int map_get_next_key(union bpf_attr *attr)
799{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100800 void __user *ukey = u64_to_user_ptr(attr->key);
801 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700802 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700803 struct bpf_map *map;
804 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200805 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700806 int err;
807
808 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
809 return -EINVAL;
810
Daniel Borkmann592867b2015-09-08 18:00:09 +0200811 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100812 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700813 if (IS_ERR(map))
814 return PTR_ERR(map);
815
Chenbo Feng6e71b042017-10-18 13:00:22 -0700816 if (!(f.file->f_mode & FMODE_CAN_READ)) {
817 err = -EPERM;
818 goto err_put;
819 }
820
Teng Qin8fe45922017-04-24 19:00:37 -0700821 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400822 key = memdup_user(ukey, map->key_size);
823 if (IS_ERR(key)) {
824 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700825 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400826 }
Teng Qin8fe45922017-04-24 19:00:37 -0700827 } else {
828 key = NULL;
829 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700830
831 err = -ENOMEM;
832 next_key = kmalloc(map->key_size, GFP_USER);
833 if (!next_key)
834 goto free_key;
835
Jakub Kicinskia3884572018-01-11 20:29:09 -0800836 if (bpf_map_is_dev_bound(map)) {
837 err = bpf_map_offload_get_next_key(map, key, next_key);
838 goto out;
839 }
840
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700841 rcu_read_lock();
842 err = map->ops->map_get_next_key(map, key, next_key);
843 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800844out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700845 if (err)
846 goto free_next_key;
847
848 err = -EFAULT;
849 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
850 goto free_next_key;
851
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100852 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700853 err = 0;
854
855free_next_key:
856 kfree(next_key);
857free_key:
858 kfree(key);
859err_put:
860 fdput(f);
861 return err;
862}
863
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700864static const struct bpf_prog_ops * const bpf_prog_types[] = {
865#define BPF_PROG_TYPE(_id, _name) \
866 [_id] = & _name ## _prog_ops,
867#define BPF_MAP_TYPE(_id, _ops)
868#include <linux/bpf_types.h>
869#undef BPF_PROG_TYPE
870#undef BPF_MAP_TYPE
871};
872
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700873static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
874{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200875 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
876 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700877
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700878 if (!bpf_prog_is_dev_bound(prog->aux))
879 prog->aux->ops = bpf_prog_types[type];
880 else
881 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200882 prog->type = type;
883 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700884}
885
886/* drop refcnt on maps used by eBPF program and free auxilary data */
887static void free_used_maps(struct bpf_prog_aux *aux)
888{
889 int i;
890
891 for (i = 0; i < aux->used_map_cnt; i++)
892 bpf_map_put(aux->used_maps[i]);
893
894 kfree(aux->used_maps);
895}
896
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100897int __bpf_prog_charge(struct user_struct *user, u32 pages)
898{
899 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
900 unsigned long user_bufs;
901
902 if (user) {
903 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
904 if (user_bufs > memlock_limit) {
905 atomic_long_sub(pages, &user->locked_vm);
906 return -EPERM;
907 }
908 }
909
910 return 0;
911}
912
913void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
914{
915 if (user)
916 atomic_long_sub(pages, &user->locked_vm);
917}
918
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700919static int bpf_prog_charge_memlock(struct bpf_prog *prog)
920{
921 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100922 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700923
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100924 ret = __bpf_prog_charge(user, prog->pages);
925 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700926 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100927 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700928 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100929
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700930 prog->aux->user = user;
931 return 0;
932}
933
934static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
935{
936 struct user_struct *user = prog->aux->user;
937
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100938 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700939 free_uid(user);
940}
941
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700942static int bpf_prog_alloc_id(struct bpf_prog *prog)
943{
944 int id;
945
Shaohua Lib76354c2018-03-27 11:53:21 -0700946 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700947 spin_lock_bh(&prog_idr_lock);
948 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
949 if (id > 0)
950 prog->aux->id = id;
951 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700952 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700953
954 /* id is in [1, INT_MAX) */
955 if (WARN_ON_ONCE(!id))
956 return -ENOSPC;
957
958 return id > 0 ? 0 : id;
959}
960
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800961void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700962{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800963 /* cBPF to eBPF migrations are currently not in the idr store.
964 * Offloaded programs are removed from the store when their device
965 * disappears - even if someone grabs an fd to them they are unusable,
966 * simply waiting for refcnt to drop to be freed.
967 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700968 if (!prog->aux->id)
969 return;
970
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700971 if (do_idr_lock)
972 spin_lock_bh(&prog_idr_lock);
973 else
974 __acquire(&prog_idr_lock);
975
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700976 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800977 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700978
979 if (do_idr_lock)
980 spin_unlock_bh(&prog_idr_lock);
981 else
982 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700983}
984
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200985static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700986{
987 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
988
989 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700990 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700991 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700992 bpf_prog_free(aux->prog);
993}
994
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700995static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700996{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100997 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +0100998 int i;
999
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001000 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001001 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001002 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001003
1004 for (i = 0; i < prog->aux->func_cnt; i++)
1005 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +01001006 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001007
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001008 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001009 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001010}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001011
1012void bpf_prog_put(struct bpf_prog *prog)
1013{
1014 __bpf_prog_put(prog, true);
1015}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001016EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001017
1018static int bpf_prog_release(struct inode *inode, struct file *filp)
1019{
1020 struct bpf_prog *prog = filp->private_data;
1021
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001022 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001023 return 0;
1024}
1025
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001026#ifdef CONFIG_PROC_FS
1027static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1028{
1029 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001030 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001031
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001032 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001033 seq_printf(m,
1034 "prog_type:\t%u\n"
1035 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001036 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001037 "memlock:\t%llu\n",
1038 prog->type,
1039 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001040 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001041 prog->pages * 1ULL << PAGE_SHIFT);
1042}
1043#endif
1044
Chenbo Fengf66e4482017-10-18 13:00:26 -07001045const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001046#ifdef CONFIG_PROC_FS
1047 .show_fdinfo = bpf_prog_show_fdinfo,
1048#endif
1049 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001050 .read = bpf_dummy_read,
1051 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001052};
1053
Daniel Borkmannb2197752015-10-29 14:58:09 +01001054int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001055{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001056 int ret;
1057
1058 ret = security_bpf_prog(prog);
1059 if (ret < 0)
1060 return ret;
1061
Daniel Borkmannaa797812015-10-29 14:58:06 +01001062 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1063 O_RDWR | O_CLOEXEC);
1064}
1065
Daniel Borkmann113214b2016-06-30 17:24:44 +02001066static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001067{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001068 if (!f.file)
1069 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001070 if (f.file->f_op != &bpf_prog_fops) {
1071 fdput(f);
1072 return ERR_PTR(-EINVAL);
1073 }
1074
Daniel Borkmannc2101292015-10-29 14:58:07 +01001075 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001076}
1077
Brenden Blanco59d36562016-07-19 12:16:46 -07001078struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001079{
Brenden Blanco59d36562016-07-19 12:16:46 -07001080 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1081 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001082 return ERR_PTR(-EBUSY);
1083 }
1084 return prog;
1085}
Brenden Blanco59d36562016-07-19 12:16:46 -07001086EXPORT_SYMBOL_GPL(bpf_prog_add);
1087
Daniel Borkmannc5405942016-11-09 22:02:34 +01001088void bpf_prog_sub(struct bpf_prog *prog, int i)
1089{
1090 /* Only to be used for undoing previous bpf_prog_add() in some
1091 * error path. We still know that another entity in our call
1092 * path holds a reference to the program, thus atomic_sub() can
1093 * be safely used in such cases!
1094 */
1095 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1096}
1097EXPORT_SYMBOL_GPL(bpf_prog_sub);
1098
Brenden Blanco59d36562016-07-19 12:16:46 -07001099struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1100{
1101 return bpf_prog_add(prog, 1);
1102}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001103EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001104
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001105/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001106struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001107{
1108 int refold;
1109
1110 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1111
1112 if (refold >= BPF_MAX_REFCNT) {
1113 __bpf_prog_put(prog, false);
1114 return ERR_PTR(-EBUSY);
1115 }
1116
1117 if (!refold)
1118 return ERR_PTR(-ENOENT);
1119
1120 return prog;
1121}
John Fastabenda6f6df62017-08-15 22:32:22 -07001122EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001123
Al Viro040ee692017-12-02 20:20:38 -05001124bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001125 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001126{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001127 /* not an attachment, just a refcount inc, always allow */
1128 if (!attach_type)
1129 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001130
1131 if (prog->type != *attach_type)
1132 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001133 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001134 return false;
1135
1136 return true;
1137}
1138
1139static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001140 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001141{
1142 struct fd f = fdget(ufd);
1143 struct bpf_prog *prog;
1144
Daniel Borkmann113214b2016-06-30 17:24:44 +02001145 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001146 if (IS_ERR(prog))
1147 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001148 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001149 prog = ERR_PTR(-EINVAL);
1150 goto out;
1151 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001152
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001153 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001154out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001155 fdput(f);
1156 return prog;
1157}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001158
1159struct bpf_prog *bpf_prog_get(u32 ufd)
1160{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001161 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001162}
1163
Jakub Kicinski248f3462017-11-03 13:56:20 -07001164struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001165 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001166{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001167 struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001168
1169 if (!IS_ERR(prog))
1170 trace_bpf_prog_get_type(prog);
1171 return prog;
1172}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001173EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001174
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001175/* Initially all BPF programs could be loaded w/o specifying
1176 * expected_attach_type. Later for some of them specifying expected_attach_type
1177 * at load time became required so that program could be validated properly.
1178 * Programs of types that are allowed to be loaded both w/ and w/o (for
1179 * backward compatibility) expected_attach_type, should have the default attach
1180 * type assigned to expected_attach_type for the latter case, so that it can be
1181 * validated later at attach time.
1182 *
1183 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1184 * prog type requires it but has some attach types that have to be backward
1185 * compatible.
1186 */
1187static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1188{
1189 switch (attr->prog_type) {
1190 case BPF_PROG_TYPE_CGROUP_SOCK:
1191 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1192 * exist so checking for non-zero is the way to go here.
1193 */
1194 if (!attr->expected_attach_type)
1195 attr->expected_attach_type =
1196 BPF_CGROUP_INET_SOCK_CREATE;
1197 break;
1198 }
1199}
1200
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001201static int
1202bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1203 enum bpf_attach_type expected_attach_type)
1204{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001205 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001206 case BPF_PROG_TYPE_CGROUP_SOCK:
1207 switch (expected_attach_type) {
1208 case BPF_CGROUP_INET_SOCK_CREATE:
1209 case BPF_CGROUP_INET4_POST_BIND:
1210 case BPF_CGROUP_INET6_POST_BIND:
1211 return 0;
1212 default:
1213 return -EINVAL;
1214 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001215 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1216 switch (expected_attach_type) {
1217 case BPF_CGROUP_INET4_BIND:
1218 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001219 case BPF_CGROUP_INET4_CONNECT:
1220 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001221 return 0;
1222 default:
1223 return -EINVAL;
1224 }
1225 default:
1226 return 0;
1227 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001228}
1229
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001230/* last field in 'union bpf_attr' used by this command */
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001231#define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001232
1233static int bpf_prog_load(union bpf_attr *attr)
1234{
1235 enum bpf_prog_type type = attr->prog_type;
1236 struct bpf_prog *prog;
1237 int err;
1238 char license[128];
1239 bool is_gpl;
1240
1241 if (CHECK_ATTR(BPF_PROG_LOAD))
1242 return -EINVAL;
1243
David S. Millere07b98d2017-05-10 11:38:07 -07001244 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1245 return -EINVAL;
1246
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001247 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001248 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001249 sizeof(license) - 1) < 0)
1250 return -EFAULT;
1251 license[sizeof(license) - 1] = 0;
1252
1253 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1254 is_gpl = license_is_gpl_compatible(license);
1255
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001256 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1257 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001258
Alexei Starovoitov25415172015-03-25 12:49:20 -07001259 if (type == BPF_PROG_TYPE_KPROBE &&
1260 attr->kern_version != LINUX_VERSION_CODE)
1261 return -EINVAL;
1262
Chenbo Feng80b7d812017-05-31 18:16:00 -07001263 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1264 type != BPF_PROG_TYPE_CGROUP_SKB &&
1265 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001266 return -EPERM;
1267
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001268 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001269 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1270 return -EINVAL;
1271
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001272 /* plain bpf_prog allocation */
1273 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1274 if (!prog)
1275 return -ENOMEM;
1276
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001277 prog->expected_attach_type = attr->expected_attach_type;
1278
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001279 prog->aux->offload_requested = !!attr->prog_ifindex;
1280
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001281 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001282 if (err)
1283 goto free_prog_nouncharge;
1284
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001285 err = bpf_prog_charge_memlock(prog);
1286 if (err)
1287 goto free_prog_sec;
1288
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001289 prog->len = attr->insn_cnt;
1290
1291 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001292 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001293 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001294 goto free_prog;
1295
1296 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001297 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001298
1299 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001300 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001301
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001302 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001303 err = bpf_prog_offload_init(prog, attr);
1304 if (err)
1305 goto free_prog;
1306 }
1307
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001308 /* find program type: socket_filter vs tracing_filter */
1309 err = find_prog_type(type, prog);
1310 if (err < 0)
1311 goto free_prog;
1312
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001313 prog->aux->load_time = ktime_get_boot_ns();
1314 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1315 if (err)
1316 goto free_prog;
1317
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001318 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001319 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001320 if (err < 0)
1321 goto free_used_maps;
1322
1323 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001324 if (!prog->bpf_func)
1325 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001326 if (err < 0)
1327 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001328
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001329 err = bpf_prog_alloc_id(prog);
1330 if (err)
1331 goto free_used_maps;
1332
Daniel Borkmannaa797812015-10-29 14:58:06 +01001333 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001334 if (err < 0) {
1335 /* failed to allocate fd.
1336 * bpf_prog_put() is needed because the above
1337 * bpf_prog_alloc_id() has published the prog
1338 * to the userspace and the userspace may
1339 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1340 */
1341 bpf_prog_put(prog);
1342 return err;
1343 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001344
Daniel Borkmann74451e662017-02-16 22:24:50 +01001345 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001346 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001347 return err;
1348
1349free_used_maps:
1350 free_used_maps(prog->aux);
1351free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001352 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001353free_prog_sec:
1354 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001355free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001356 bpf_prog_free(prog);
1357 return err;
1358}
1359
Chenbo Feng6e71b042017-10-18 13:00:22 -07001360#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001361
1362static int bpf_obj_pin(const union bpf_attr *attr)
1363{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001364 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001365 return -EINVAL;
1366
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001367 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001368}
1369
1370static int bpf_obj_get(const union bpf_attr *attr)
1371{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001372 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1373 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001374 return -EINVAL;
1375
Chenbo Feng6e71b042017-10-18 13:00:22 -07001376 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1377 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001378}
1379
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001380struct bpf_raw_tracepoint {
1381 struct bpf_raw_event_map *btp;
1382 struct bpf_prog *prog;
1383};
1384
1385static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1386{
1387 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1388
1389 if (raw_tp->prog) {
1390 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1391 bpf_prog_put(raw_tp->prog);
1392 }
1393 kfree(raw_tp);
1394 return 0;
1395}
1396
1397static const struct file_operations bpf_raw_tp_fops = {
1398 .release = bpf_raw_tracepoint_release,
1399 .read = bpf_dummy_read,
1400 .write = bpf_dummy_write,
1401};
1402
1403#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1404
1405static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1406{
1407 struct bpf_raw_tracepoint *raw_tp;
1408 struct bpf_raw_event_map *btp;
1409 struct bpf_prog *prog;
1410 char tp_name[128];
1411 int tp_fd, err;
1412
1413 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1414 sizeof(tp_name) - 1) < 0)
1415 return -EFAULT;
1416 tp_name[sizeof(tp_name) - 1] = 0;
1417
1418 btp = bpf_find_raw_tracepoint(tp_name);
1419 if (!btp)
1420 return -ENOENT;
1421
1422 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1423 if (!raw_tp)
1424 return -ENOMEM;
1425 raw_tp->btp = btp;
1426
1427 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1428 BPF_PROG_TYPE_RAW_TRACEPOINT);
1429 if (IS_ERR(prog)) {
1430 err = PTR_ERR(prog);
1431 goto out_free_tp;
1432 }
1433
1434 err = bpf_probe_register(raw_tp->btp, prog);
1435 if (err)
1436 goto out_put_prog;
1437
1438 raw_tp->prog = prog;
1439 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1440 O_CLOEXEC);
1441 if (tp_fd < 0) {
1442 bpf_probe_unregister(raw_tp->btp, prog);
1443 err = tp_fd;
1444 goto out_put_prog;
1445 }
1446 return tp_fd;
1447
1448out_put_prog:
1449 bpf_prog_put(prog);
1450out_free_tp:
1451 kfree(raw_tp);
1452 return err;
1453}
1454
Daniel Mackf4324552016-11-23 16:52:27 +01001455#ifdef CONFIG_CGROUP_BPF
1456
Anders Roxell33491582018-04-03 14:09:47 +02001457static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1458 enum bpf_attach_type attach_type)
1459{
1460 switch (prog->type) {
1461 case BPF_PROG_TYPE_CGROUP_SOCK:
1462 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1463 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1464 default:
1465 return 0;
1466 }
1467}
1468
John Fastabend464bc0f2017-08-28 07:10:04 -07001469#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001470
John Fastabend4f738ad2018-03-18 12:57:10 -07001471static int sockmap_get_from_fd(const union bpf_attr *attr,
1472 int type, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001473{
John Fastabend5a67da22017-09-08 14:00:49 -07001474 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001475 int ufd = attr->target_fd;
1476 struct bpf_map *map;
1477 struct fd f;
1478 int err;
1479
1480 f = fdget(ufd);
1481 map = __bpf_map_get(f);
1482 if (IS_ERR(map))
1483 return PTR_ERR(map);
1484
John Fastabend5a67da22017-09-08 14:00:49 -07001485 if (attach) {
John Fastabend4f738ad2018-03-18 12:57:10 -07001486 prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
John Fastabend5a67da22017-09-08 14:00:49 -07001487 if (IS_ERR(prog)) {
1488 fdput(f);
1489 return PTR_ERR(prog);
1490 }
John Fastabend174a79f2017-08-15 22:32:47 -07001491 }
1492
John Fastabend5a67da22017-09-08 14:00:49 -07001493 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001494 if (err) {
1495 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001496 if (prog)
1497 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001498 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001499 }
1500
1501 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001502 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001503}
Daniel Mackf4324552016-11-23 16:52:27 +01001504
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001505#define BPF_F_ATTACH_MASK \
1506 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1507
Daniel Mackf4324552016-11-23 16:52:27 +01001508static int bpf_prog_attach(const union bpf_attr *attr)
1509{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001510 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001511 struct bpf_prog *prog;
1512 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001513 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001514
1515 if (!capable(CAP_NET_ADMIN))
1516 return -EPERM;
1517
1518 if (CHECK_ATTR(BPF_PROG_ATTACH))
1519 return -EINVAL;
1520
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001521 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001522 return -EINVAL;
1523
Daniel Mackf4324552016-11-23 16:52:27 +01001524 switch (attr->attach_type) {
1525 case BPF_CGROUP_INET_INGRESS:
1526 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001527 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001528 break;
David Ahern610236582016-12-01 08:48:04 -08001529 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001530 case BPF_CGROUP_INET4_POST_BIND:
1531 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001532 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1533 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001534 case BPF_CGROUP_INET4_BIND:
1535 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001536 case BPF_CGROUP_INET4_CONNECT:
1537 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001538 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1539 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001540 case BPF_CGROUP_SOCK_OPS:
1541 ptype = BPF_PROG_TYPE_SOCK_OPS;
1542 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001543 case BPF_CGROUP_DEVICE:
1544 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1545 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001546 case BPF_SK_MSG_VERDICT:
1547 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
John Fastabend464bc0f2017-08-28 07:10:04 -07001548 case BPF_SK_SKB_STREAM_PARSER:
1549 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001550 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001551 default:
1552 return -EINVAL;
1553 }
1554
David Ahernb2cd1252016-12-01 08:48:03 -08001555 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1556 if (IS_ERR(prog))
1557 return PTR_ERR(prog);
1558
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001559 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1560 bpf_prog_put(prog);
1561 return -EINVAL;
1562 }
1563
David Ahernb2cd1252016-12-01 08:48:03 -08001564 cgrp = cgroup_get_from_fd(attr->target_fd);
1565 if (IS_ERR(cgrp)) {
1566 bpf_prog_put(prog);
1567 return PTR_ERR(cgrp);
1568 }
1569
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001570 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1571 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001572 if (ret)
1573 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001574 cgroup_put(cgrp);
1575
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001576 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001577}
1578
1579#define BPF_PROG_DETACH_LAST_FIELD attach_type
1580
1581static int bpf_prog_detach(const union bpf_attr *attr)
1582{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001583 enum bpf_prog_type ptype;
1584 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001585 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001586 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001587
1588 if (!capable(CAP_NET_ADMIN))
1589 return -EPERM;
1590
1591 if (CHECK_ATTR(BPF_PROG_DETACH))
1592 return -EINVAL;
1593
1594 switch (attr->attach_type) {
1595 case BPF_CGROUP_INET_INGRESS:
1596 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001597 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1598 break;
David Ahern610236582016-12-01 08:48:04 -08001599 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001600 case BPF_CGROUP_INET4_POST_BIND:
1601 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001602 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1603 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001604 case BPF_CGROUP_INET4_BIND:
1605 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001606 case BPF_CGROUP_INET4_CONNECT:
1607 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001608 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1609 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001610 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001611 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001612 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001613 case BPF_CGROUP_DEVICE:
1614 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1615 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001616 case BPF_SK_MSG_VERDICT:
1617 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
John Fastabend5a67da22017-09-08 14:00:49 -07001618 case BPF_SK_SKB_STREAM_PARSER:
1619 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend4f738ad2018-03-18 12:57:10 -07001620 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001621 default:
1622 return -EINVAL;
1623 }
1624
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001625 cgrp = cgroup_get_from_fd(attr->target_fd);
1626 if (IS_ERR(cgrp))
1627 return PTR_ERR(cgrp);
1628
1629 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1630 if (IS_ERR(prog))
1631 prog = NULL;
1632
1633 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1634 if (prog)
1635 bpf_prog_put(prog);
1636 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001637 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001638}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001639
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001640#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1641
1642static int bpf_prog_query(const union bpf_attr *attr,
1643 union bpf_attr __user *uattr)
1644{
1645 struct cgroup *cgrp;
1646 int ret;
1647
1648 if (!capable(CAP_NET_ADMIN))
1649 return -EPERM;
1650 if (CHECK_ATTR(BPF_PROG_QUERY))
1651 return -EINVAL;
1652 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1653 return -EINVAL;
1654
1655 switch (attr->query.attach_type) {
1656 case BPF_CGROUP_INET_INGRESS:
1657 case BPF_CGROUP_INET_EGRESS:
1658 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001659 case BPF_CGROUP_INET4_BIND:
1660 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001661 case BPF_CGROUP_INET4_POST_BIND:
1662 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001663 case BPF_CGROUP_INET4_CONNECT:
1664 case BPF_CGROUP_INET6_CONNECT:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001665 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001666 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001667 break;
1668 default:
1669 return -EINVAL;
1670 }
1671 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1672 if (IS_ERR(cgrp))
1673 return PTR_ERR(cgrp);
1674 ret = cgroup_bpf_query(cgrp, attr, uattr);
1675 cgroup_put(cgrp);
1676 return ret;
1677}
Daniel Mackf4324552016-11-23 16:52:27 +01001678#endif /* CONFIG_CGROUP_BPF */
1679
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001680#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1681
1682static int bpf_prog_test_run(const union bpf_attr *attr,
1683 union bpf_attr __user *uattr)
1684{
1685 struct bpf_prog *prog;
1686 int ret = -ENOTSUPP;
1687
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001688 if (!capable(CAP_SYS_ADMIN))
1689 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001690 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1691 return -EINVAL;
1692
1693 prog = bpf_prog_get(attr->test.prog_fd);
1694 if (IS_ERR(prog))
1695 return PTR_ERR(prog);
1696
1697 if (prog->aux->ops->test_run)
1698 ret = prog->aux->ops->test_run(prog, attr, uattr);
1699
1700 bpf_prog_put(prog);
1701 return ret;
1702}
1703
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001704#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1705
1706static int bpf_obj_get_next_id(const union bpf_attr *attr,
1707 union bpf_attr __user *uattr,
1708 struct idr *idr,
1709 spinlock_t *lock)
1710{
1711 u32 next_id = attr->start_id;
1712 int err = 0;
1713
1714 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1715 return -EINVAL;
1716
1717 if (!capable(CAP_SYS_ADMIN))
1718 return -EPERM;
1719
1720 next_id++;
1721 spin_lock_bh(lock);
1722 if (!idr_get_next(idr, &next_id))
1723 err = -ENOENT;
1724 spin_unlock_bh(lock);
1725
1726 if (!err)
1727 err = put_user(next_id, &uattr->next_id);
1728
1729 return err;
1730}
1731
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001732#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1733
1734static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1735{
1736 struct bpf_prog *prog;
1737 u32 id = attr->prog_id;
1738 int fd;
1739
1740 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1741 return -EINVAL;
1742
1743 if (!capable(CAP_SYS_ADMIN))
1744 return -EPERM;
1745
1746 spin_lock_bh(&prog_idr_lock);
1747 prog = idr_find(&prog_idr, id);
1748 if (prog)
1749 prog = bpf_prog_inc_not_zero(prog);
1750 else
1751 prog = ERR_PTR(-ENOENT);
1752 spin_unlock_bh(&prog_idr_lock);
1753
1754 if (IS_ERR(prog))
1755 return PTR_ERR(prog);
1756
1757 fd = bpf_prog_new_fd(prog);
1758 if (fd < 0)
1759 bpf_prog_put(prog);
1760
1761 return fd;
1762}
1763
Chenbo Feng6e71b042017-10-18 13:00:22 -07001764#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001765
1766static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1767{
1768 struct bpf_map *map;
1769 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001770 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001771 int fd;
1772
Chenbo Feng6e71b042017-10-18 13:00:22 -07001773 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1774 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001775 return -EINVAL;
1776
1777 if (!capable(CAP_SYS_ADMIN))
1778 return -EPERM;
1779
Chenbo Feng6e71b042017-10-18 13:00:22 -07001780 f_flags = bpf_get_file_flag(attr->open_flags);
1781 if (f_flags < 0)
1782 return f_flags;
1783
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001784 spin_lock_bh(&map_idr_lock);
1785 map = idr_find(&map_idr, id);
1786 if (map)
1787 map = bpf_map_inc_not_zero(map, true);
1788 else
1789 map = ERR_PTR(-ENOENT);
1790 spin_unlock_bh(&map_idr_lock);
1791
1792 if (IS_ERR(map))
1793 return PTR_ERR(map);
1794
Chenbo Feng6e71b042017-10-18 13:00:22 -07001795 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001796 if (fd < 0)
1797 bpf_map_put(map);
1798
1799 return fd;
1800}
1801
Daniel Borkmann7105e822017-12-20 13:42:57 +01001802static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1803 unsigned long addr)
1804{
1805 int i;
1806
1807 for (i = 0; i < prog->aux->used_map_cnt; i++)
1808 if (prog->aux->used_maps[i] == (void *)addr)
1809 return prog->aux->used_maps[i];
1810 return NULL;
1811}
1812
1813static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1814{
1815 const struct bpf_map *map;
1816 struct bpf_insn *insns;
1817 u64 imm;
1818 int i;
1819
1820 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1821 GFP_USER);
1822 if (!insns)
1823 return insns;
1824
1825 for (i = 0; i < prog->len; i++) {
1826 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1827 insns[i].code = BPF_JMP | BPF_CALL;
1828 insns[i].imm = BPF_FUNC_tail_call;
1829 /* fall-through */
1830 }
1831 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1832 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1833 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1834 insns[i].code = BPF_JMP | BPF_CALL;
1835 if (!bpf_dump_raw_ok())
1836 insns[i].imm = 0;
1837 continue;
1838 }
1839
1840 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1841 continue;
1842
1843 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1844 map = bpf_map_from_imm(prog, imm);
1845 if (map) {
1846 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1847 insns[i].imm = map->id;
1848 insns[i + 1].imm = 0;
1849 continue;
1850 }
1851
1852 if (!bpf_dump_raw_ok() &&
1853 imm == (unsigned long)prog->aux) {
1854 insns[i].imm = 0;
1855 insns[i + 1].imm = 0;
1856 continue;
1857 }
1858 }
1859
1860 return insns;
1861}
1862
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001863static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1864 const union bpf_attr *attr,
1865 union bpf_attr __user *uattr)
1866{
1867 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1868 struct bpf_prog_info info = {};
1869 u32 info_len = attr->info.info_len;
1870 char __user *uinsns;
1871 u32 ulen;
1872 int err;
1873
1874 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1875 if (err)
1876 return err;
1877 info_len = min_t(u32, sizeof(info), info_len);
1878
1879 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001880 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001881
1882 info.type = prog->type;
1883 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001884 info.load_time = prog->aux->load_time;
1885 info.created_by_uid = from_kuid_munged(current_user_ns(),
1886 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001887
1888 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001889 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1890
1891 ulen = info.nr_map_ids;
1892 info.nr_map_ids = prog->aux->used_map_cnt;
1893 ulen = min_t(u32, info.nr_map_ids, ulen);
1894 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001895 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001896 u32 i;
1897
1898 for (i = 0; i < ulen; i++)
1899 if (put_user(prog->aux->used_maps[i]->id,
1900 &user_map_ids[i]))
1901 return -EFAULT;
1902 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001903
1904 if (!capable(CAP_SYS_ADMIN)) {
1905 info.jited_prog_len = 0;
1906 info.xlated_prog_len = 0;
1907 goto done;
1908 }
1909
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001910 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001911 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001912 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001913 struct bpf_insn *insns_sanitized;
1914 bool fault;
1915
1916 if (prog->blinded && !bpf_dump_raw_ok()) {
1917 info.xlated_prog_insns = 0;
1918 goto done;
1919 }
1920 insns_sanitized = bpf_insn_prepare_dump(prog);
1921 if (!insns_sanitized)
1922 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001923 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1924 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001925 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1926 kfree(insns_sanitized);
1927 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001928 return -EFAULT;
1929 }
1930
Jakub Kicinski675fc272017-12-27 18:39:09 -08001931 if (bpf_prog_is_dev_bound(prog->aux)) {
1932 err = bpf_prog_offload_info_fill(&info, prog);
1933 if (err)
1934 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001935 goto done;
1936 }
1937
1938 /* NOTE: the following code is supposed to be skipped for offload.
1939 * bpf_prog_offload_info_fill() is the place to fill similar fields
1940 * for offload.
1941 */
1942 ulen = info.jited_prog_len;
1943 info.jited_prog_len = prog->jited_len;
1944 if (info.jited_prog_len && ulen) {
1945 if (bpf_dump_raw_ok()) {
1946 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1947 ulen = min_t(u32, info.jited_prog_len, ulen);
1948 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1949 return -EFAULT;
1950 } else {
1951 info.jited_prog_insns = 0;
1952 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08001953 }
1954
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001955done:
1956 if (copy_to_user(uinfo, &info, info_len) ||
1957 put_user(info_len, &uattr->info.info_len))
1958 return -EFAULT;
1959
1960 return 0;
1961}
1962
1963static int bpf_map_get_info_by_fd(struct bpf_map *map,
1964 const union bpf_attr *attr,
1965 union bpf_attr __user *uattr)
1966{
1967 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1968 struct bpf_map_info info = {};
1969 u32 info_len = attr->info.info_len;
1970 int err;
1971
1972 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1973 if (err)
1974 return err;
1975 info_len = min_t(u32, sizeof(info), info_len);
1976
1977 info.type = map->map_type;
1978 info.id = map->id;
1979 info.key_size = map->key_size;
1980 info.value_size = map->value_size;
1981 info.max_entries = map->max_entries;
1982 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07001983 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001984
Jakub Kicinski52775b32018-01-17 19:13:28 -08001985 if (bpf_map_is_dev_bound(map)) {
1986 err = bpf_map_offload_info_fill(&info, map);
1987 if (err)
1988 return err;
1989 }
1990
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001991 if (copy_to_user(uinfo, &info, info_len) ||
1992 put_user(info_len, &uattr->info.info_len))
1993 return -EFAULT;
1994
1995 return 0;
1996}
1997
1998#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1999
2000static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2001 union bpf_attr __user *uattr)
2002{
2003 int ufd = attr->info.bpf_fd;
2004 struct fd f;
2005 int err;
2006
2007 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2008 return -EINVAL;
2009
2010 f = fdget(ufd);
2011 if (!f.file)
2012 return -EBADFD;
2013
2014 if (f.file->f_op == &bpf_prog_fops)
2015 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2016 uattr);
2017 else if (f.file->f_op == &bpf_map_fops)
2018 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2019 uattr);
2020 else
2021 err = -EINVAL;
2022
2023 fdput(f);
2024 return err;
2025}
2026
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002027#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2028
2029static int bpf_btf_load(const union bpf_attr *attr)
2030{
2031 if (CHECK_ATTR(BPF_BTF_LOAD))
2032 return -EINVAL;
2033
2034 if (!capable(CAP_SYS_ADMIN))
2035 return -EPERM;
2036
2037 return btf_new_fd(attr);
2038}
2039
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002040SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2041{
2042 union bpf_attr attr = {};
2043 int err;
2044
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002045 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002046 return -EPERM;
2047
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002048 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
2049 if (err)
2050 return err;
2051 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002052
2053 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2054 if (copy_from_user(&attr, uattr, size) != 0)
2055 return -EFAULT;
2056
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002057 err = security_bpf(cmd, &attr, size);
2058 if (err < 0)
2059 return err;
2060
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002061 switch (cmd) {
2062 case BPF_MAP_CREATE:
2063 err = map_create(&attr);
2064 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002065 case BPF_MAP_LOOKUP_ELEM:
2066 err = map_lookup_elem(&attr);
2067 break;
2068 case BPF_MAP_UPDATE_ELEM:
2069 err = map_update_elem(&attr);
2070 break;
2071 case BPF_MAP_DELETE_ELEM:
2072 err = map_delete_elem(&attr);
2073 break;
2074 case BPF_MAP_GET_NEXT_KEY:
2075 err = map_get_next_key(&attr);
2076 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002077 case BPF_PROG_LOAD:
2078 err = bpf_prog_load(&attr);
2079 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002080 case BPF_OBJ_PIN:
2081 err = bpf_obj_pin(&attr);
2082 break;
2083 case BPF_OBJ_GET:
2084 err = bpf_obj_get(&attr);
2085 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002086#ifdef CONFIG_CGROUP_BPF
2087 case BPF_PROG_ATTACH:
2088 err = bpf_prog_attach(&attr);
2089 break;
2090 case BPF_PROG_DETACH:
2091 err = bpf_prog_detach(&attr);
2092 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002093 case BPF_PROG_QUERY:
2094 err = bpf_prog_query(&attr, uattr);
2095 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002096#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002097 case BPF_PROG_TEST_RUN:
2098 err = bpf_prog_test_run(&attr, uattr);
2099 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002100 case BPF_PROG_GET_NEXT_ID:
2101 err = bpf_obj_get_next_id(&attr, uattr,
2102 &prog_idr, &prog_idr_lock);
2103 break;
2104 case BPF_MAP_GET_NEXT_ID:
2105 err = bpf_obj_get_next_id(&attr, uattr,
2106 &map_idr, &map_idr_lock);
2107 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002108 case BPF_PROG_GET_FD_BY_ID:
2109 err = bpf_prog_get_fd_by_id(&attr);
2110 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002111 case BPF_MAP_GET_FD_BY_ID:
2112 err = bpf_map_get_fd_by_id(&attr);
2113 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002114 case BPF_OBJ_GET_INFO_BY_FD:
2115 err = bpf_obj_get_info_by_fd(&attr, uattr);
2116 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002117 case BPF_RAW_TRACEPOINT_OPEN:
2118 err = bpf_raw_tracepoint_open(&attr);
2119 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002120 case BPF_BTF_LOAD:
2121 err = bpf_btf_load(&attr);
2122 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002123 default:
2124 err = -EINVAL;
2125 break;
2126 }
2127
2128 return err;
2129}