blob: 97a825ffc7639526b163b936d9eae0862f9758c9 [file] [log] [blame]
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010013#include <linux/bpf_trace.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070014#include <linux/syscalls.h>
15#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010016#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010017#include <linux/vmalloc.h>
18#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070019#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070020#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070021#include <linux/license.h>
22#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070023#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010024#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070025#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070026#include <linux/cred.h>
27#include <linux/timekeeping.h>
28#include <linux/ctype.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070029
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070030#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
34#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
35#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
36
Chenbo Feng6e71b042017-10-18 13:00:22 -070037#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
38
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080039DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070040static DEFINE_IDR(prog_idr);
41static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070042static DEFINE_IDR(map_idr);
43static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080044
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070045int sysctl_unprivileged_bpf_disabled __read_mostly;
46
Johannes Berg40077e02017-04-11 15:34:58 +020047static const struct bpf_map_ops * const bpf_map_types[] = {
48#define BPF_PROG_TYPE(_id, _ops)
49#define BPF_MAP_TYPE(_id, _ops) \
50 [_id] = &_ops,
51#include <linux/bpf_types.h>
52#undef BPF_PROG_TYPE
53#undef BPF_MAP_TYPE
54};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070055
Mickaël Salaün752ba562017-08-07 20:45:20 +020056/*
57 * If we're handed a bigger struct than we know of, ensure all the unknown bits
58 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
59 * we don't know about yet.
60 *
61 * There is a ToCToU between this function call and the following
62 * copy_from_user() call. However, this is not a concern since this function is
63 * meant to be a future-proofing of bits.
64 */
Mickaël Salaün58291a72017-08-07 20:45:19 +020065static int check_uarg_tail_zero(void __user *uaddr,
66 size_t expected_size,
67 size_t actual_size)
68{
69 unsigned char __user *addr;
70 unsigned char __user *end;
71 unsigned char val;
72 int err;
73
Mickaël Salaün752ba562017-08-07 20:45:20 +020074 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
75 return -E2BIG;
76
77 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
78 return -EFAULT;
79
Mickaël Salaün58291a72017-08-07 20:45:19 +020080 if (actual_size <= expected_size)
81 return 0;
82
83 addr = uaddr + expected_size;
84 end = uaddr + actual_size;
85
86 for (; addr < end; addr++) {
87 err = get_user(val, addr);
88 if (err)
89 return err;
90 if (val)
91 return -E2BIG;
92 }
93
94 return 0;
95}
96
Jakub Kicinskia3884572018-01-11 20:29:09 -080097const struct bpf_map_ops bpf_map_offload_ops = {
98 .map_alloc = bpf_map_offload_map_alloc,
99 .map_free = bpf_map_offload_map_free,
100};
101
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700102static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
103{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800104 const struct bpf_map_ops *ops;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700105 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800106 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700107
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800108 if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
109 return ERR_PTR(-EINVAL);
110 ops = bpf_map_types[attr->map_type];
111 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200112 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700113
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800114 if (ops->map_alloc_check) {
115 err = ops->map_alloc_check(attr);
116 if (err)
117 return ERR_PTR(err);
118 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800119 if (attr->map_ifindex)
120 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800121 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200122 if (IS_ERR(map))
123 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800124 map->ops = ops;
Johannes Berg40077e02017-04-11 15:34:58 +0200125 map->map_type = attr->map_type;
126 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700127}
128
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700129void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100130{
131 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
132 * trigger under memory pressure as we really just want to
133 * fail instead.
134 */
135 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
136 void *area;
137
138 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700139 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100140 if (area != NULL)
141 return area;
142 }
143
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700144 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
145 __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100146}
147
148void bpf_map_area_free(void *area)
149{
150 kvfree(area);
151}
152
Jakub Kicinskibd475642018-01-11 20:29:06 -0800153void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
154{
155 map->map_type = attr->map_type;
156 map->key_size = attr->key_size;
157 map->value_size = attr->value_size;
158 map->max_entries = attr->max_entries;
159 map->map_flags = attr->map_flags;
160 map->numa_node = bpf_map_attr_numa_node(attr);
161}
162
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800163int bpf_map_precharge_memlock(u32 pages)
164{
165 struct user_struct *user = get_current_user();
166 unsigned long memlock_limit, cur;
167
168 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
169 cur = atomic_long_read(&user->locked_vm);
170 free_uid(user);
171 if (cur + pages > memlock_limit)
172 return -EPERM;
173 return 0;
174}
175
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700176static int bpf_map_charge_memlock(struct bpf_map *map)
177{
178 struct user_struct *user = get_current_user();
179 unsigned long memlock_limit;
180
181 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
182
183 atomic_long_add(map->pages, &user->locked_vm);
184
185 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
186 atomic_long_sub(map->pages, &user->locked_vm);
187 free_uid(user);
188 return -EPERM;
189 }
190 map->user = user;
191 return 0;
192}
193
194static void bpf_map_uncharge_memlock(struct bpf_map *map)
195{
196 struct user_struct *user = map->user;
197
198 atomic_long_sub(map->pages, &user->locked_vm);
199 free_uid(user);
200}
201
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700202static int bpf_map_alloc_id(struct bpf_map *map)
203{
204 int id;
205
206 spin_lock_bh(&map_idr_lock);
207 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
208 if (id > 0)
209 map->id = id;
210 spin_unlock_bh(&map_idr_lock);
211
212 if (WARN_ON_ONCE(!id))
213 return -ENOSPC;
214
215 return id > 0 ? 0 : id;
216}
217
Jakub Kicinskia3884572018-01-11 20:29:09 -0800218void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700219{
Eric Dumazet930651a2017-09-19 09:15:59 -0700220 unsigned long flags;
221
Jakub Kicinskia3884572018-01-11 20:29:09 -0800222 /* Offloaded maps are removed from the IDR store when their device
223 * disappears - even if someone holds an fd to them they are unusable,
224 * the memory is gone, all ops will fail; they are simply waiting for
225 * refcnt to drop to be freed.
226 */
227 if (!map->id)
228 return;
229
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700230 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700231 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700232 else
233 __acquire(&map_idr_lock);
234
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700235 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800236 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700237
238 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700239 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700240 else
241 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700242}
243
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700244/* called from workqueue */
245static void bpf_map_free_deferred(struct work_struct *work)
246{
247 struct bpf_map *map = container_of(work, struct bpf_map, work);
248
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700249 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700250 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700251 /* implementation dependent freeing */
252 map->ops->map_free(map);
253}
254
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100255static void bpf_map_put_uref(struct bpf_map *map)
256{
257 if (atomic_dec_and_test(&map->usercnt)) {
258 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
259 bpf_fd_array_map_clear(map);
260 }
261}
262
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700263/* decrement map refcnt and schedule it for freeing via workqueue
264 * (unrelying map implementation ops->map_free() might sleep)
265 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700266static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700267{
268 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700269 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700270 bpf_map_free_id(map, do_idr_lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700271 INIT_WORK(&map->work, bpf_map_free_deferred);
272 schedule_work(&map->work);
273 }
274}
275
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700276void bpf_map_put(struct bpf_map *map)
277{
278 __bpf_map_put(map, true);
279}
280
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100281void bpf_map_put_with_uref(struct bpf_map *map)
282{
283 bpf_map_put_uref(map);
284 bpf_map_put(map);
285}
286
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700287static int bpf_map_release(struct inode *inode, struct file *filp)
288{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200289 struct bpf_map *map = filp->private_data;
290
291 if (map->ops->map_release)
292 map->ops->map_release(map, filp);
293
294 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700295 return 0;
296}
297
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100298#ifdef CONFIG_PROC_FS
299static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
300{
301 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100302 const struct bpf_array *array;
303 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200304 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100305
306 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
307 array = container_of(map, struct bpf_array, map);
308 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200309 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100310 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100311
312 seq_printf(m,
313 "map_type:\t%u\n"
314 "key_size:\t%u\n"
315 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100316 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100317 "map_flags:\t%#x\n"
318 "memlock:\t%llu\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100319 map->map_type,
320 map->key_size,
321 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100322 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100323 map->map_flags,
324 map->pages * 1ULL << PAGE_SHIFT);
325
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200326 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100327 seq_printf(m, "owner_prog_type:\t%u\n",
328 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200329 seq_printf(m, "owner_jited:\t%u\n",
330 owner_jited);
331 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100332}
333#endif
334
Chenbo Feng6e71b042017-10-18 13:00:22 -0700335static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
336 loff_t *ppos)
337{
338 /* We need this handler such that alloc_file() enables
339 * f_mode with FMODE_CAN_READ.
340 */
341 return -EINVAL;
342}
343
344static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
345 size_t siz, loff_t *ppos)
346{
347 /* We need this handler such that alloc_file() enables
348 * f_mode with FMODE_CAN_WRITE.
349 */
350 return -EINVAL;
351}
352
Chenbo Fengf66e4482017-10-18 13:00:26 -0700353const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100354#ifdef CONFIG_PROC_FS
355 .show_fdinfo = bpf_map_show_fdinfo,
356#endif
357 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700358 .read = bpf_dummy_read,
359 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700360};
361
Chenbo Feng6e71b042017-10-18 13:00:22 -0700362int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100363{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700364 int ret;
365
366 ret = security_bpf_map(map, OPEN_FMODE(flags));
367 if (ret < 0)
368 return ret;
369
Daniel Borkmannaa797812015-10-29 14:58:06 +0100370 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700371 flags | O_CLOEXEC);
372}
373
374int bpf_get_file_flag(int flags)
375{
376 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
377 return -EINVAL;
378 if (flags & BPF_F_RDONLY)
379 return O_RDONLY;
380 if (flags & BPF_F_WRONLY)
381 return O_WRONLY;
382 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100383}
384
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700385/* helper macro to check that unused fields 'union bpf_attr' are zero */
386#define CHECK_ATTR(CMD) \
387 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
388 sizeof(attr->CMD##_LAST_FIELD), 0, \
389 sizeof(*attr) - \
390 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
391 sizeof(attr->CMD##_LAST_FIELD)) != NULL
392
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700393/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
394 * Return 0 on success and < 0 on error.
395 */
396static int bpf_obj_name_cpy(char *dst, const char *src)
397{
398 const char *end = src + BPF_OBJ_NAME_LEN;
399
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700400 memset(dst, 0, BPF_OBJ_NAME_LEN);
401
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700402 /* Copy all isalnum() and '_' char */
403 while (src < end && *src) {
404 if (!isalnum(*src) && *src != '_')
405 return -EINVAL;
406 *dst++ = *src++;
407 }
408
409 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
410 if (src == end)
411 return -EINVAL;
412
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700413 return 0;
414}
415
Jakub Kicinskia3884572018-01-11 20:29:09 -0800416#define BPF_MAP_CREATE_LAST_FIELD map_ifindex
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700417/* called via syscall */
418static int map_create(union bpf_attr *attr)
419{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700420 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700421 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700422 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700423 int err;
424
425 err = CHECK_ATTR(BPF_MAP_CREATE);
426 if (err)
427 return -EINVAL;
428
Chenbo Feng6e71b042017-10-18 13:00:22 -0700429 f_flags = bpf_get_file_flag(attr->map_flags);
430 if (f_flags < 0)
431 return f_flags;
432
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700433 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700434 ((unsigned int)numa_node >= nr_node_ids ||
435 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700436 return -EINVAL;
437
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700438 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
439 map = find_and_alloc_map(attr);
440 if (IS_ERR(map))
441 return PTR_ERR(map);
442
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700443 err = bpf_obj_name_cpy(map->name, attr->map_name);
444 if (err)
445 goto free_map_nouncharge;
446
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700447 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100448 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700449
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700450 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700451 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100452 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700453
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700454 err = bpf_map_charge_memlock(map);
455 if (err)
456 goto free_map_sec;
457
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700458 err = bpf_map_alloc_id(map);
459 if (err)
460 goto free_map;
461
Chenbo Feng6e71b042017-10-18 13:00:22 -0700462 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700463 if (err < 0) {
464 /* failed to allocate fd.
465 * bpf_map_put() is needed because the above
466 * bpf_map_alloc_id() has published the map
467 * to the userspace and the userspace may
468 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
469 */
470 bpf_map_put(map);
471 return err;
472 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700473
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100474 trace_bpf_map_create(map, err);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700475 return err;
476
477free_map:
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100478 bpf_map_uncharge_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700479free_map_sec:
480 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100481free_map_nouncharge:
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700482 map->ops->map_free(map);
483 return err;
484}
485
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700486/* if error is returned, fd is released.
487 * On success caller should complete fd access with matching fdput()
488 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100489struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700490{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700491 if (!f.file)
492 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700493 if (f.file->f_op != &bpf_map_fops) {
494 fdput(f);
495 return ERR_PTR(-EINVAL);
496 }
497
Daniel Borkmannc2101292015-10-29 14:58:07 +0100498 return f.file->private_data;
499}
500
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700501/* prog's and map's refcnt limit */
502#define BPF_MAX_REFCNT 32768
503
504struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100505{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700506 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
507 atomic_dec(&map->refcnt);
508 return ERR_PTR(-EBUSY);
509 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100510 if (uref)
511 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700512 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100513}
514
515struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100516{
517 struct fd f = fdget(ufd);
518 struct bpf_map *map;
519
520 map = __bpf_map_get(f);
521 if (IS_ERR(map))
522 return map;
523
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700524 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100525 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700526
527 return map;
528}
529
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700530/* map_idr_lock should have been held */
531static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
532 bool uref)
533{
534 int refold;
535
536 refold = __atomic_add_unless(&map->refcnt, 1, 0);
537
538 if (refold >= BPF_MAX_REFCNT) {
539 __bpf_map_put(map, false);
540 return ERR_PTR(-EBUSY);
541 }
542
543 if (!refold)
544 return ERR_PTR(-ENOENT);
545
546 if (uref)
547 atomic_inc(&map->usercnt);
548
549 return map;
550}
551
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800552int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
553{
554 return -ENOTSUPP;
555}
556
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700557/* last field in 'union bpf_attr' used by this command */
558#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
559
560static int map_lookup_elem(union bpf_attr *attr)
561{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100562 void __user *ukey = u64_to_user_ptr(attr->key);
563 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700564 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700565 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800566 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800567 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200568 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700569 int err;
570
571 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
572 return -EINVAL;
573
Daniel Borkmann592867b2015-09-08 18:00:09 +0200574 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100575 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700576 if (IS_ERR(map))
577 return PTR_ERR(map);
578
Chenbo Feng6e71b042017-10-18 13:00:22 -0700579 if (!(f.file->f_mode & FMODE_CAN_READ)) {
580 err = -EPERM;
581 goto err_put;
582 }
583
Al Viroe4448ed2017-05-13 18:43:00 -0400584 key = memdup_user(ukey, map->key_size);
585 if (IS_ERR(key)) {
586 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700587 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400588 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700589
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800590 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800591 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800592 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
593 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700594 else if (IS_FD_MAP(map))
595 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800596 else
597 value_size = map->value_size;
598
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800599 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800600 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700601 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800602 goto free_key;
603
Jakub Kicinskia3884572018-01-11 20:29:09 -0800604 if (bpf_map_is_dev_bound(map)) {
605 err = bpf_map_offload_lookup_elem(map, key, value);
606 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
607 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800608 err = bpf_percpu_hash_copy(map, key, value);
609 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
610 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800611 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
612 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700613 } else if (IS_FD_ARRAY(map)) {
614 err = bpf_fd_array_map_lookup_elem(map, key, value);
615 } else if (IS_FD_HASH(map)) {
616 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800617 } else {
618 rcu_read_lock();
619 ptr = map->ops->map_lookup_elem(map, key);
620 if (ptr)
621 memcpy(value, ptr, value_size);
622 rcu_read_unlock();
623 err = ptr ? 0 : -ENOENT;
624 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800625
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800626 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800627 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700628
629 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800630 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800631 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700632
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100633 trace_bpf_map_lookup_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700634 err = 0;
635
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800636free_value:
637 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700638free_key:
639 kfree(key);
640err_put:
641 fdput(f);
642 return err;
643}
644
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800645#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700646
647static int map_update_elem(union bpf_attr *attr)
648{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100649 void __user *ukey = u64_to_user_ptr(attr->key);
650 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700651 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700652 struct bpf_map *map;
653 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800654 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200655 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700656 int err;
657
658 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
659 return -EINVAL;
660
Daniel Borkmann592867b2015-09-08 18:00:09 +0200661 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100662 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700663 if (IS_ERR(map))
664 return PTR_ERR(map);
665
Chenbo Feng6e71b042017-10-18 13:00:22 -0700666 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
667 err = -EPERM;
668 goto err_put;
669 }
670
Al Viroe4448ed2017-05-13 18:43:00 -0400671 key = memdup_user(ukey, map->key_size);
672 if (IS_ERR(key)) {
673 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700674 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400675 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700676
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800677 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800678 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800679 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
680 value_size = round_up(map->value_size, 8) * num_possible_cpus();
681 else
682 value_size = map->value_size;
683
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700684 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800685 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700686 if (!value)
687 goto free_key;
688
689 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800690 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700691 goto free_value;
692
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200693 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800694 if (bpf_map_is_dev_bound(map)) {
695 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
696 goto out;
697 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200698 err = map->ops->map_update_elem(map, key, value, attr->flags);
699 goto out;
700 }
701
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800702 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
703 * inside bpf map update or delete otherwise deadlocks are possible
704 */
705 preempt_disable();
706 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800707 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
708 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800709 err = bpf_percpu_hash_update(map, key, value, attr->flags);
710 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
711 err = bpf_percpu_array_update(map, key, value, attr->flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200712 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -0700713 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -0700714 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
715 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
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
946 spin_lock_bh(&prog_idr_lock);
947 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
948 if (id > 0)
949 prog->aux->id = id;
950 spin_unlock_bh(&prog_idr_lock);
951
952 /* id is in [1, INT_MAX) */
953 if (WARN_ON_ONCE(!id))
954 return -ENOSPC;
955
956 return id > 0 ? 0 : id;
957}
958
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800959void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700960{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800961 /* cBPF to eBPF migrations are currently not in the idr store.
962 * Offloaded programs are removed from the store when their device
963 * disappears - even if someone grabs an fd to them they are unusable,
964 * simply waiting for refcnt to drop to be freed.
965 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700966 if (!prog->aux->id)
967 return;
968
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700969 if (do_idr_lock)
970 spin_lock_bh(&prog_idr_lock);
971 else
972 __acquire(&prog_idr_lock);
973
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700974 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800975 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700976
977 if (do_idr_lock)
978 spin_unlock_bh(&prog_idr_lock);
979 else
980 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700981}
982
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200983static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700984{
985 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
986
987 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700988 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700989 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700990 bpf_prog_free(aux->prog);
991}
992
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700993static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700994{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100995 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +0100996 int i;
997
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100998 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700999 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001000 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001001
1002 for (i = 0; i < prog->aux->func_cnt; i++)
1003 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +01001004 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001005
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001006 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001007 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001008}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001009
1010void bpf_prog_put(struct bpf_prog *prog)
1011{
1012 __bpf_prog_put(prog, true);
1013}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001014EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001015
1016static int bpf_prog_release(struct inode *inode, struct file *filp)
1017{
1018 struct bpf_prog *prog = filp->private_data;
1019
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001020 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001021 return 0;
1022}
1023
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001024#ifdef CONFIG_PROC_FS
1025static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1026{
1027 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001028 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001029
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001030 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001031 seq_printf(m,
1032 "prog_type:\t%u\n"
1033 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001034 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001035 "memlock:\t%llu\n",
1036 prog->type,
1037 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001038 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001039 prog->pages * 1ULL << PAGE_SHIFT);
1040}
1041#endif
1042
Chenbo Fengf66e4482017-10-18 13:00:26 -07001043const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001044#ifdef CONFIG_PROC_FS
1045 .show_fdinfo = bpf_prog_show_fdinfo,
1046#endif
1047 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001048 .read = bpf_dummy_read,
1049 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001050};
1051
Daniel Borkmannb2197752015-10-29 14:58:09 +01001052int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001053{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001054 int ret;
1055
1056 ret = security_bpf_prog(prog);
1057 if (ret < 0)
1058 return ret;
1059
Daniel Borkmannaa797812015-10-29 14:58:06 +01001060 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1061 O_RDWR | O_CLOEXEC);
1062}
1063
Daniel Borkmann113214b2016-06-30 17:24:44 +02001064static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001065{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001066 if (!f.file)
1067 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001068 if (f.file->f_op != &bpf_prog_fops) {
1069 fdput(f);
1070 return ERR_PTR(-EINVAL);
1071 }
1072
Daniel Borkmannc2101292015-10-29 14:58:07 +01001073 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001074}
1075
Brenden Blanco59d36562016-07-19 12:16:46 -07001076struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001077{
Brenden Blanco59d36562016-07-19 12:16:46 -07001078 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1079 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001080 return ERR_PTR(-EBUSY);
1081 }
1082 return prog;
1083}
Brenden Blanco59d36562016-07-19 12:16:46 -07001084EXPORT_SYMBOL_GPL(bpf_prog_add);
1085
Daniel Borkmannc5405942016-11-09 22:02:34 +01001086void bpf_prog_sub(struct bpf_prog *prog, int i)
1087{
1088 /* Only to be used for undoing previous bpf_prog_add() in some
1089 * error path. We still know that another entity in our call
1090 * path holds a reference to the program, thus atomic_sub() can
1091 * be safely used in such cases!
1092 */
1093 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1094}
1095EXPORT_SYMBOL_GPL(bpf_prog_sub);
1096
Brenden Blanco59d36562016-07-19 12:16:46 -07001097struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1098{
1099 return bpf_prog_add(prog, 1);
1100}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001101EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001102
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001103/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001104struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001105{
1106 int refold;
1107
1108 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1109
1110 if (refold >= BPF_MAX_REFCNT) {
1111 __bpf_prog_put(prog, false);
1112 return ERR_PTR(-EBUSY);
1113 }
1114
1115 if (!refold)
1116 return ERR_PTR(-ENOENT);
1117
1118 return prog;
1119}
John Fastabenda6f6df62017-08-15 22:32:22 -07001120EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001121
Al Viro040ee692017-12-02 20:20:38 -05001122bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001123 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001124{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001125 /* not an attachment, just a refcount inc, always allow */
1126 if (!attach_type)
1127 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001128
1129 if (prog->type != *attach_type)
1130 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001131 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001132 return false;
1133
1134 return true;
1135}
1136
1137static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001138 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001139{
1140 struct fd f = fdget(ufd);
1141 struct bpf_prog *prog;
1142
Daniel Borkmann113214b2016-06-30 17:24:44 +02001143 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001144 if (IS_ERR(prog))
1145 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001146 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001147 prog = ERR_PTR(-EINVAL);
1148 goto out;
1149 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001150
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001151 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001152out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001153 fdput(f);
1154 return prog;
1155}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001156
1157struct bpf_prog *bpf_prog_get(u32 ufd)
1158{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001159 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001160}
1161
Jakub Kicinski248f3462017-11-03 13:56:20 -07001162struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001163 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001164{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001165 struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001166
1167 if (!IS_ERR(prog))
1168 trace_bpf_prog_get_type(prog);
1169 return prog;
1170}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001171EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001172
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001173/* last field in 'union bpf_attr' used by this command */
Jakub Kicinski1f6f4cb2017-11-20 15:21:53 -08001174#define BPF_PROG_LOAD_LAST_FIELD prog_ifindex
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001175
1176static int bpf_prog_load(union bpf_attr *attr)
1177{
1178 enum bpf_prog_type type = attr->prog_type;
1179 struct bpf_prog *prog;
1180 int err;
1181 char license[128];
1182 bool is_gpl;
1183
1184 if (CHECK_ATTR(BPF_PROG_LOAD))
1185 return -EINVAL;
1186
David S. Millere07b98d2017-05-10 11:38:07 -07001187 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1188 return -EINVAL;
1189
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001190 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001191 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001192 sizeof(license) - 1) < 0)
1193 return -EFAULT;
1194 license[sizeof(license) - 1] = 0;
1195
1196 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1197 is_gpl = license_is_gpl_compatible(license);
1198
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001199 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1200 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001201
Alexei Starovoitov25415172015-03-25 12:49:20 -07001202 if (type == BPF_PROG_TYPE_KPROBE &&
1203 attr->kern_version != LINUX_VERSION_CODE)
1204 return -EINVAL;
1205
Chenbo Feng80b7d812017-05-31 18:16:00 -07001206 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1207 type != BPF_PROG_TYPE_CGROUP_SKB &&
1208 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001209 return -EPERM;
1210
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001211 /* plain bpf_prog allocation */
1212 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1213 if (!prog)
1214 return -ENOMEM;
1215
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001216 prog->aux->offload_requested = !!attr->prog_ifindex;
1217
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001218 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001219 if (err)
1220 goto free_prog_nouncharge;
1221
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001222 err = bpf_prog_charge_memlock(prog);
1223 if (err)
1224 goto free_prog_sec;
1225
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001226 prog->len = attr->insn_cnt;
1227
1228 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001229 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001230 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001231 goto free_prog;
1232
1233 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001234 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001235
1236 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001237 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001238
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001239 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001240 err = bpf_prog_offload_init(prog, attr);
1241 if (err)
1242 goto free_prog;
1243 }
1244
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001245 /* find program type: socket_filter vs tracing_filter */
1246 err = find_prog_type(type, prog);
1247 if (err < 0)
1248 goto free_prog;
1249
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001250 prog->aux->load_time = ktime_get_boot_ns();
1251 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1252 if (err)
1253 goto free_prog;
1254
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001255 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001256 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001257 if (err < 0)
1258 goto free_used_maps;
1259
1260 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001261 if (!prog->bpf_func)
1262 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001263 if (err < 0)
1264 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001265
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001266 err = bpf_prog_alloc_id(prog);
1267 if (err)
1268 goto free_used_maps;
1269
Daniel Borkmannaa797812015-10-29 14:58:06 +01001270 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001271 if (err < 0) {
1272 /* failed to allocate fd.
1273 * bpf_prog_put() is needed because the above
1274 * bpf_prog_alloc_id() has published the prog
1275 * to the userspace and the userspace may
1276 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1277 */
1278 bpf_prog_put(prog);
1279 return err;
1280 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001281
Daniel Borkmann74451e662017-02-16 22:24:50 +01001282 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001283 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001284 return err;
1285
1286free_used_maps:
1287 free_used_maps(prog->aux);
1288free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001289 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001290free_prog_sec:
1291 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001292free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001293 bpf_prog_free(prog);
1294 return err;
1295}
1296
Chenbo Feng6e71b042017-10-18 13:00:22 -07001297#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001298
1299static int bpf_obj_pin(const union bpf_attr *attr)
1300{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001301 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001302 return -EINVAL;
1303
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001304 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001305}
1306
1307static int bpf_obj_get(const union bpf_attr *attr)
1308{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001309 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1310 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001311 return -EINVAL;
1312
Chenbo Feng6e71b042017-10-18 13:00:22 -07001313 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1314 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001315}
1316
Daniel Mackf4324552016-11-23 16:52:27 +01001317#ifdef CONFIG_CGROUP_BPF
1318
John Fastabend464bc0f2017-08-28 07:10:04 -07001319#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001320
John Fastabend5a67da22017-09-08 14:00:49 -07001321static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001322{
John Fastabend5a67da22017-09-08 14:00:49 -07001323 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001324 int ufd = attr->target_fd;
1325 struct bpf_map *map;
1326 struct fd f;
1327 int err;
1328
1329 f = fdget(ufd);
1330 map = __bpf_map_get(f);
1331 if (IS_ERR(map))
1332 return PTR_ERR(map);
1333
John Fastabend5a67da22017-09-08 14:00:49 -07001334 if (attach) {
1335 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1336 BPF_PROG_TYPE_SK_SKB);
1337 if (IS_ERR(prog)) {
1338 fdput(f);
1339 return PTR_ERR(prog);
1340 }
John Fastabend174a79f2017-08-15 22:32:47 -07001341 }
1342
John Fastabend5a67da22017-09-08 14:00:49 -07001343 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001344 if (err) {
1345 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001346 if (prog)
1347 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001348 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001349 }
1350
1351 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001352 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001353}
Daniel Mackf4324552016-11-23 16:52:27 +01001354
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001355#define BPF_F_ATTACH_MASK \
1356 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1357
Daniel Mackf4324552016-11-23 16:52:27 +01001358static int bpf_prog_attach(const union bpf_attr *attr)
1359{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001360 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001361 struct bpf_prog *prog;
1362 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001363 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001364
1365 if (!capable(CAP_NET_ADMIN))
1366 return -EPERM;
1367
1368 if (CHECK_ATTR(BPF_PROG_ATTACH))
1369 return -EINVAL;
1370
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001371 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001372 return -EINVAL;
1373
Daniel Mackf4324552016-11-23 16:52:27 +01001374 switch (attr->attach_type) {
1375 case BPF_CGROUP_INET_INGRESS:
1376 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001377 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001378 break;
David Ahern610236582016-12-01 08:48:04 -08001379 case BPF_CGROUP_INET_SOCK_CREATE:
1380 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1381 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001382 case BPF_CGROUP_SOCK_OPS:
1383 ptype = BPF_PROG_TYPE_SOCK_OPS;
1384 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001385 case BPF_CGROUP_DEVICE:
1386 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1387 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001388 case BPF_SK_SKB_STREAM_PARSER:
1389 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend5a67da22017-09-08 14:00:49 -07001390 return sockmap_get_from_fd(attr, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001391 default:
1392 return -EINVAL;
1393 }
1394
David Ahernb2cd1252016-12-01 08:48:03 -08001395 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1396 if (IS_ERR(prog))
1397 return PTR_ERR(prog);
1398
1399 cgrp = cgroup_get_from_fd(attr->target_fd);
1400 if (IS_ERR(cgrp)) {
1401 bpf_prog_put(prog);
1402 return PTR_ERR(cgrp);
1403 }
1404
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001405 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1406 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001407 if (ret)
1408 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001409 cgroup_put(cgrp);
1410
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001411 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001412}
1413
1414#define BPF_PROG_DETACH_LAST_FIELD attach_type
1415
1416static int bpf_prog_detach(const union bpf_attr *attr)
1417{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001418 enum bpf_prog_type ptype;
1419 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001420 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001421 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001422
1423 if (!capable(CAP_NET_ADMIN))
1424 return -EPERM;
1425
1426 if (CHECK_ATTR(BPF_PROG_DETACH))
1427 return -EINVAL;
1428
1429 switch (attr->attach_type) {
1430 case BPF_CGROUP_INET_INGRESS:
1431 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001432 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1433 break;
David Ahern610236582016-12-01 08:48:04 -08001434 case BPF_CGROUP_INET_SOCK_CREATE:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001435 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1436 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001437 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001438 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001439 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001440 case BPF_CGROUP_DEVICE:
1441 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1442 break;
John Fastabend5a67da22017-09-08 14:00:49 -07001443 case BPF_SK_SKB_STREAM_PARSER:
1444 case BPF_SK_SKB_STREAM_VERDICT:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001445 return sockmap_get_from_fd(attr, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001446 default:
1447 return -EINVAL;
1448 }
1449
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001450 cgrp = cgroup_get_from_fd(attr->target_fd);
1451 if (IS_ERR(cgrp))
1452 return PTR_ERR(cgrp);
1453
1454 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1455 if (IS_ERR(prog))
1456 prog = NULL;
1457
1458 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1459 if (prog)
1460 bpf_prog_put(prog);
1461 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001462 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001463}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001464
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001465#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1466
1467static int bpf_prog_query(const union bpf_attr *attr,
1468 union bpf_attr __user *uattr)
1469{
1470 struct cgroup *cgrp;
1471 int ret;
1472
1473 if (!capable(CAP_NET_ADMIN))
1474 return -EPERM;
1475 if (CHECK_ATTR(BPF_PROG_QUERY))
1476 return -EINVAL;
1477 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1478 return -EINVAL;
1479
1480 switch (attr->query.attach_type) {
1481 case BPF_CGROUP_INET_INGRESS:
1482 case BPF_CGROUP_INET_EGRESS:
1483 case BPF_CGROUP_INET_SOCK_CREATE:
1484 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001485 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001486 break;
1487 default:
1488 return -EINVAL;
1489 }
1490 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1491 if (IS_ERR(cgrp))
1492 return PTR_ERR(cgrp);
1493 ret = cgroup_bpf_query(cgrp, attr, uattr);
1494 cgroup_put(cgrp);
1495 return ret;
1496}
Daniel Mackf4324552016-11-23 16:52:27 +01001497#endif /* CONFIG_CGROUP_BPF */
1498
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001499#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1500
1501static int bpf_prog_test_run(const union bpf_attr *attr,
1502 union bpf_attr __user *uattr)
1503{
1504 struct bpf_prog *prog;
1505 int ret = -ENOTSUPP;
1506
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001507 if (!capable(CAP_SYS_ADMIN))
1508 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001509 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1510 return -EINVAL;
1511
1512 prog = bpf_prog_get(attr->test.prog_fd);
1513 if (IS_ERR(prog))
1514 return PTR_ERR(prog);
1515
1516 if (prog->aux->ops->test_run)
1517 ret = prog->aux->ops->test_run(prog, attr, uattr);
1518
1519 bpf_prog_put(prog);
1520 return ret;
1521}
1522
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001523#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1524
1525static int bpf_obj_get_next_id(const union bpf_attr *attr,
1526 union bpf_attr __user *uattr,
1527 struct idr *idr,
1528 spinlock_t *lock)
1529{
1530 u32 next_id = attr->start_id;
1531 int err = 0;
1532
1533 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1534 return -EINVAL;
1535
1536 if (!capable(CAP_SYS_ADMIN))
1537 return -EPERM;
1538
1539 next_id++;
1540 spin_lock_bh(lock);
1541 if (!idr_get_next(idr, &next_id))
1542 err = -ENOENT;
1543 spin_unlock_bh(lock);
1544
1545 if (!err)
1546 err = put_user(next_id, &uattr->next_id);
1547
1548 return err;
1549}
1550
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001551#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1552
1553static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1554{
1555 struct bpf_prog *prog;
1556 u32 id = attr->prog_id;
1557 int fd;
1558
1559 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1560 return -EINVAL;
1561
1562 if (!capable(CAP_SYS_ADMIN))
1563 return -EPERM;
1564
1565 spin_lock_bh(&prog_idr_lock);
1566 prog = idr_find(&prog_idr, id);
1567 if (prog)
1568 prog = bpf_prog_inc_not_zero(prog);
1569 else
1570 prog = ERR_PTR(-ENOENT);
1571 spin_unlock_bh(&prog_idr_lock);
1572
1573 if (IS_ERR(prog))
1574 return PTR_ERR(prog);
1575
1576 fd = bpf_prog_new_fd(prog);
1577 if (fd < 0)
1578 bpf_prog_put(prog);
1579
1580 return fd;
1581}
1582
Chenbo Feng6e71b042017-10-18 13:00:22 -07001583#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001584
1585static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1586{
1587 struct bpf_map *map;
1588 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001589 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001590 int fd;
1591
Chenbo Feng6e71b042017-10-18 13:00:22 -07001592 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1593 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001594 return -EINVAL;
1595
1596 if (!capable(CAP_SYS_ADMIN))
1597 return -EPERM;
1598
Chenbo Feng6e71b042017-10-18 13:00:22 -07001599 f_flags = bpf_get_file_flag(attr->open_flags);
1600 if (f_flags < 0)
1601 return f_flags;
1602
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001603 spin_lock_bh(&map_idr_lock);
1604 map = idr_find(&map_idr, id);
1605 if (map)
1606 map = bpf_map_inc_not_zero(map, true);
1607 else
1608 map = ERR_PTR(-ENOENT);
1609 spin_unlock_bh(&map_idr_lock);
1610
1611 if (IS_ERR(map))
1612 return PTR_ERR(map);
1613
Chenbo Feng6e71b042017-10-18 13:00:22 -07001614 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001615 if (fd < 0)
1616 bpf_map_put(map);
1617
1618 return fd;
1619}
1620
Daniel Borkmann7105e822017-12-20 13:42:57 +01001621static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1622 unsigned long addr)
1623{
1624 int i;
1625
1626 for (i = 0; i < prog->aux->used_map_cnt; i++)
1627 if (prog->aux->used_maps[i] == (void *)addr)
1628 return prog->aux->used_maps[i];
1629 return NULL;
1630}
1631
1632static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1633{
1634 const struct bpf_map *map;
1635 struct bpf_insn *insns;
1636 u64 imm;
1637 int i;
1638
1639 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1640 GFP_USER);
1641 if (!insns)
1642 return insns;
1643
1644 for (i = 0; i < prog->len; i++) {
1645 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1646 insns[i].code = BPF_JMP | BPF_CALL;
1647 insns[i].imm = BPF_FUNC_tail_call;
1648 /* fall-through */
1649 }
1650 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1651 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1652 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1653 insns[i].code = BPF_JMP | BPF_CALL;
1654 if (!bpf_dump_raw_ok())
1655 insns[i].imm = 0;
1656 continue;
1657 }
1658
1659 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1660 continue;
1661
1662 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1663 map = bpf_map_from_imm(prog, imm);
1664 if (map) {
1665 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1666 insns[i].imm = map->id;
1667 insns[i + 1].imm = 0;
1668 continue;
1669 }
1670
1671 if (!bpf_dump_raw_ok() &&
1672 imm == (unsigned long)prog->aux) {
1673 insns[i].imm = 0;
1674 insns[i + 1].imm = 0;
1675 continue;
1676 }
1677 }
1678
1679 return insns;
1680}
1681
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001682static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1683 const union bpf_attr *attr,
1684 union bpf_attr __user *uattr)
1685{
1686 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1687 struct bpf_prog_info info = {};
1688 u32 info_len = attr->info.info_len;
1689 char __user *uinsns;
1690 u32 ulen;
1691 int err;
1692
1693 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1694 if (err)
1695 return err;
1696 info_len = min_t(u32, sizeof(info), info_len);
1697
1698 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001699 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001700
1701 info.type = prog->type;
1702 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001703 info.load_time = prog->aux->load_time;
1704 info.created_by_uid = from_kuid_munged(current_user_ns(),
1705 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001706
1707 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001708 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1709
1710 ulen = info.nr_map_ids;
1711 info.nr_map_ids = prog->aux->used_map_cnt;
1712 ulen = min_t(u32, info.nr_map_ids, ulen);
1713 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001714 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001715 u32 i;
1716
1717 for (i = 0; i < ulen; i++)
1718 if (put_user(prog->aux->used_maps[i]->id,
1719 &user_map_ids[i]))
1720 return -EFAULT;
1721 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001722
1723 if (!capable(CAP_SYS_ADMIN)) {
1724 info.jited_prog_len = 0;
1725 info.xlated_prog_len = 0;
1726 goto done;
1727 }
1728
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001729 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001730 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001731 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001732 struct bpf_insn *insns_sanitized;
1733 bool fault;
1734
1735 if (prog->blinded && !bpf_dump_raw_ok()) {
1736 info.xlated_prog_insns = 0;
1737 goto done;
1738 }
1739 insns_sanitized = bpf_insn_prepare_dump(prog);
1740 if (!insns_sanitized)
1741 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001742 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1743 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001744 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1745 kfree(insns_sanitized);
1746 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001747 return -EFAULT;
1748 }
1749
Jakub Kicinski675fc272017-12-27 18:39:09 -08001750 if (bpf_prog_is_dev_bound(prog->aux)) {
1751 err = bpf_prog_offload_info_fill(&info, prog);
1752 if (err)
1753 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001754 goto done;
1755 }
1756
1757 /* NOTE: the following code is supposed to be skipped for offload.
1758 * bpf_prog_offload_info_fill() is the place to fill similar fields
1759 * for offload.
1760 */
1761 ulen = info.jited_prog_len;
1762 info.jited_prog_len = prog->jited_len;
1763 if (info.jited_prog_len && ulen) {
1764 if (bpf_dump_raw_ok()) {
1765 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1766 ulen = min_t(u32, info.jited_prog_len, ulen);
1767 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1768 return -EFAULT;
1769 } else {
1770 info.jited_prog_insns = 0;
1771 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08001772 }
1773
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001774done:
1775 if (copy_to_user(uinfo, &info, info_len) ||
1776 put_user(info_len, &uattr->info.info_len))
1777 return -EFAULT;
1778
1779 return 0;
1780}
1781
1782static int bpf_map_get_info_by_fd(struct bpf_map *map,
1783 const union bpf_attr *attr,
1784 union bpf_attr __user *uattr)
1785{
1786 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1787 struct bpf_map_info info = {};
1788 u32 info_len = attr->info.info_len;
1789 int err;
1790
1791 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1792 if (err)
1793 return err;
1794 info_len = min_t(u32, sizeof(info), info_len);
1795
1796 info.type = map->map_type;
1797 info.id = map->id;
1798 info.key_size = map->key_size;
1799 info.value_size = map->value_size;
1800 info.max_entries = map->max_entries;
1801 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07001802 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001803
1804 if (copy_to_user(uinfo, &info, info_len) ||
1805 put_user(info_len, &uattr->info.info_len))
1806 return -EFAULT;
1807
1808 return 0;
1809}
1810
1811#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1812
1813static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1814 union bpf_attr __user *uattr)
1815{
1816 int ufd = attr->info.bpf_fd;
1817 struct fd f;
1818 int err;
1819
1820 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1821 return -EINVAL;
1822
1823 f = fdget(ufd);
1824 if (!f.file)
1825 return -EBADFD;
1826
1827 if (f.file->f_op == &bpf_prog_fops)
1828 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1829 uattr);
1830 else if (f.file->f_op == &bpf_map_fops)
1831 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1832 uattr);
1833 else
1834 err = -EINVAL;
1835
1836 fdput(f);
1837 return err;
1838}
1839
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001840SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1841{
1842 union bpf_attr attr = {};
1843 int err;
1844
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001845 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001846 return -EPERM;
1847
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001848 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1849 if (err)
1850 return err;
1851 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001852
1853 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1854 if (copy_from_user(&attr, uattr, size) != 0)
1855 return -EFAULT;
1856
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001857 err = security_bpf(cmd, &attr, size);
1858 if (err < 0)
1859 return err;
1860
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001861 switch (cmd) {
1862 case BPF_MAP_CREATE:
1863 err = map_create(&attr);
1864 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001865 case BPF_MAP_LOOKUP_ELEM:
1866 err = map_lookup_elem(&attr);
1867 break;
1868 case BPF_MAP_UPDATE_ELEM:
1869 err = map_update_elem(&attr);
1870 break;
1871 case BPF_MAP_DELETE_ELEM:
1872 err = map_delete_elem(&attr);
1873 break;
1874 case BPF_MAP_GET_NEXT_KEY:
1875 err = map_get_next_key(&attr);
1876 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001877 case BPF_PROG_LOAD:
1878 err = bpf_prog_load(&attr);
1879 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001880 case BPF_OBJ_PIN:
1881 err = bpf_obj_pin(&attr);
1882 break;
1883 case BPF_OBJ_GET:
1884 err = bpf_obj_get(&attr);
1885 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001886#ifdef CONFIG_CGROUP_BPF
1887 case BPF_PROG_ATTACH:
1888 err = bpf_prog_attach(&attr);
1889 break;
1890 case BPF_PROG_DETACH:
1891 err = bpf_prog_detach(&attr);
1892 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001893 case BPF_PROG_QUERY:
1894 err = bpf_prog_query(&attr, uattr);
1895 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001896#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001897 case BPF_PROG_TEST_RUN:
1898 err = bpf_prog_test_run(&attr, uattr);
1899 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001900 case BPF_PROG_GET_NEXT_ID:
1901 err = bpf_obj_get_next_id(&attr, uattr,
1902 &prog_idr, &prog_idr_lock);
1903 break;
1904 case BPF_MAP_GET_NEXT_ID:
1905 err = bpf_obj_get_next_id(&attr, uattr,
1906 &map_idr, &map_idr_lock);
1907 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001908 case BPF_PROG_GET_FD_BY_ID:
1909 err = bpf_prog_get_fd_by_id(&attr);
1910 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001911 case BPF_MAP_GET_FD_BY_ID:
1912 err = bpf_map_get_fd_by_id(&attr);
1913 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001914 case BPF_OBJ_GET_INFO_BY_FD:
1915 err = bpf_obj_get_info_by_fd(&attr, uattr);
1916 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001917 default:
1918 err = -EINVAL;
1919 break;
1920 }
1921
1922 return err;
1923}