blob: e24aa3241387de91483a89160e50ee41deede775 [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);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100712 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200713 rcu_read_lock();
714 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
715 attr->flags);
716 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700717 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
718 rcu_read_lock();
719 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
720 attr->flags);
721 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800722 } else {
723 rcu_read_lock();
724 err = map->ops->map_update_elem(map, key, value, attr->flags);
725 rcu_read_unlock();
726 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800727 __this_cpu_dec(bpf_prog_active);
728 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200729out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100730 if (!err)
731 trace_bpf_map_update_elem(map, ufd, key, value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700732free_value:
733 kfree(value);
734free_key:
735 kfree(key);
736err_put:
737 fdput(f);
738 return err;
739}
740
741#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
742
743static int map_delete_elem(union bpf_attr *attr)
744{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100745 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700746 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700747 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200748 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700749 void *key;
750 int err;
751
752 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
753 return -EINVAL;
754
Daniel Borkmann592867b2015-09-08 18:00:09 +0200755 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100756 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700757 if (IS_ERR(map))
758 return PTR_ERR(map);
759
Chenbo Feng6e71b042017-10-18 13:00:22 -0700760 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
761 err = -EPERM;
762 goto err_put;
763 }
764
Al Viroe4448ed2017-05-13 18:43:00 -0400765 key = memdup_user(ukey, map->key_size);
766 if (IS_ERR(key)) {
767 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700768 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400769 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700770
Jakub Kicinskia3884572018-01-11 20:29:09 -0800771 if (bpf_map_is_dev_bound(map)) {
772 err = bpf_map_offload_delete_elem(map, key);
773 goto out;
774 }
775
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800776 preempt_disable();
777 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700778 rcu_read_lock();
779 err = map->ops->map_delete_elem(map, key);
780 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800781 __this_cpu_dec(bpf_prog_active);
782 preempt_enable();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800783out:
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100784 if (!err)
785 trace_bpf_map_delete_elem(map, ufd, key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700786 kfree(key);
787err_put:
788 fdput(f);
789 return err;
790}
791
792/* last field in 'union bpf_attr' used by this command */
793#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
794
795static int map_get_next_key(union bpf_attr *attr)
796{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100797 void __user *ukey = u64_to_user_ptr(attr->key);
798 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700799 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700800 struct bpf_map *map;
801 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200802 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700803 int err;
804
805 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
806 return -EINVAL;
807
Daniel Borkmann592867b2015-09-08 18:00:09 +0200808 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100809 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700810 if (IS_ERR(map))
811 return PTR_ERR(map);
812
Chenbo Feng6e71b042017-10-18 13:00:22 -0700813 if (!(f.file->f_mode & FMODE_CAN_READ)) {
814 err = -EPERM;
815 goto err_put;
816 }
817
Teng Qin8fe45922017-04-24 19:00:37 -0700818 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400819 key = memdup_user(ukey, map->key_size);
820 if (IS_ERR(key)) {
821 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700822 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400823 }
Teng Qin8fe45922017-04-24 19:00:37 -0700824 } else {
825 key = NULL;
826 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700827
828 err = -ENOMEM;
829 next_key = kmalloc(map->key_size, GFP_USER);
830 if (!next_key)
831 goto free_key;
832
Jakub Kicinskia3884572018-01-11 20:29:09 -0800833 if (bpf_map_is_dev_bound(map)) {
834 err = bpf_map_offload_get_next_key(map, key, next_key);
835 goto out;
836 }
837
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700838 rcu_read_lock();
839 err = map->ops->map_get_next_key(map, key, next_key);
840 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800841out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700842 if (err)
843 goto free_next_key;
844
845 err = -EFAULT;
846 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
847 goto free_next_key;
848
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100849 trace_bpf_map_next_key(map, ufd, key, next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700850 err = 0;
851
852free_next_key:
853 kfree(next_key);
854free_key:
855 kfree(key);
856err_put:
857 fdput(f);
858 return err;
859}
860
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700861static const struct bpf_prog_ops * const bpf_prog_types[] = {
862#define BPF_PROG_TYPE(_id, _name) \
863 [_id] = & _name ## _prog_ops,
864#define BPF_MAP_TYPE(_id, _ops)
865#include <linux/bpf_types.h>
866#undef BPF_PROG_TYPE
867#undef BPF_MAP_TYPE
868};
869
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700870static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
871{
Johannes Bergbe9370a2017-04-11 15:34:57 +0200872 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
873 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700874
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700875 if (!bpf_prog_is_dev_bound(prog->aux))
876 prog->aux->ops = bpf_prog_types[type];
877 else
878 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200879 prog->type = type;
880 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700881}
882
883/* drop refcnt on maps used by eBPF program and free auxilary data */
884static void free_used_maps(struct bpf_prog_aux *aux)
885{
886 int i;
887
888 for (i = 0; i < aux->used_map_cnt; i++)
889 bpf_map_put(aux->used_maps[i]);
890
891 kfree(aux->used_maps);
892}
893
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100894int __bpf_prog_charge(struct user_struct *user, u32 pages)
895{
896 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
897 unsigned long user_bufs;
898
899 if (user) {
900 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
901 if (user_bufs > memlock_limit) {
902 atomic_long_sub(pages, &user->locked_vm);
903 return -EPERM;
904 }
905 }
906
907 return 0;
908}
909
910void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
911{
912 if (user)
913 atomic_long_sub(pages, &user->locked_vm);
914}
915
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700916static int bpf_prog_charge_memlock(struct bpf_prog *prog)
917{
918 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100919 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700920
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100921 ret = __bpf_prog_charge(user, prog->pages);
922 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700923 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100924 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700925 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100926
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700927 prog->aux->user = user;
928 return 0;
929}
930
931static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
932{
933 struct user_struct *user = prog->aux->user;
934
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100935 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700936 free_uid(user);
937}
938
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700939static int bpf_prog_alloc_id(struct bpf_prog *prog)
940{
941 int id;
942
943 spin_lock_bh(&prog_idr_lock);
944 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
945 if (id > 0)
946 prog->aux->id = id;
947 spin_unlock_bh(&prog_idr_lock);
948
949 /* id is in [1, INT_MAX) */
950 if (WARN_ON_ONCE(!id))
951 return -ENOSPC;
952
953 return id > 0 ? 0 : id;
954}
955
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800956void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700957{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800958 /* cBPF to eBPF migrations are currently not in the idr store.
959 * Offloaded programs are removed from the store when their device
960 * disappears - even if someone grabs an fd to them they are unusable,
961 * simply waiting for refcnt to drop to be freed.
962 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700963 if (!prog->aux->id)
964 return;
965
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700966 if (do_idr_lock)
967 spin_lock_bh(&prog_idr_lock);
968 else
969 __acquire(&prog_idr_lock);
970
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700971 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800972 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700973
974 if (do_idr_lock)
975 spin_unlock_bh(&prog_idr_lock);
976 else
977 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700978}
979
Daniel Borkmann1aacde32016-06-30 17:24:43 +0200980static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700981{
982 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
983
984 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700985 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700986 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700987 bpf_prog_free(aux->prog);
988}
989
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700990static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700991{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100992 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Daniel Borkmann4f74d802017-12-20 13:42:56 +0100993 int i;
994
Daniel Borkmanna67edbf2017-01-25 02:28:18 +0100995 trace_bpf_prog_put_rcu(prog);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700996 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -0700997 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann4f74d802017-12-20 13:42:56 +0100998
999 for (i = 0; i < prog->aux->func_cnt; i++)
1000 bpf_prog_kallsyms_del(prog->aux->func[i]);
Daniel Borkmann74451e662017-02-16 22:24:50 +01001001 bpf_prog_kallsyms_del(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001002
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001003 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001004 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001005}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001006
1007void bpf_prog_put(struct bpf_prog *prog)
1008{
1009 __bpf_prog_put(prog, true);
1010}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001011EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001012
1013static int bpf_prog_release(struct inode *inode, struct file *filp)
1014{
1015 struct bpf_prog *prog = filp->private_data;
1016
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001017 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001018 return 0;
1019}
1020
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001021#ifdef CONFIG_PROC_FS
1022static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1023{
1024 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001025 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001026
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001027 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001028 seq_printf(m,
1029 "prog_type:\t%u\n"
1030 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001031 "prog_tag:\t%s\n"
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001032 "memlock:\t%llu\n",
1033 prog->type,
1034 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001035 prog_tag,
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001036 prog->pages * 1ULL << PAGE_SHIFT);
1037}
1038#endif
1039
Chenbo Fengf66e4482017-10-18 13:00:26 -07001040const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001041#ifdef CONFIG_PROC_FS
1042 .show_fdinfo = bpf_prog_show_fdinfo,
1043#endif
1044 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001045 .read = bpf_dummy_read,
1046 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001047};
1048
Daniel Borkmannb2197752015-10-29 14:58:09 +01001049int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001050{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001051 int ret;
1052
1053 ret = security_bpf_prog(prog);
1054 if (ret < 0)
1055 return ret;
1056
Daniel Borkmannaa797812015-10-29 14:58:06 +01001057 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1058 O_RDWR | O_CLOEXEC);
1059}
1060
Daniel Borkmann113214b2016-06-30 17:24:44 +02001061static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001062{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001063 if (!f.file)
1064 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001065 if (f.file->f_op != &bpf_prog_fops) {
1066 fdput(f);
1067 return ERR_PTR(-EINVAL);
1068 }
1069
Daniel Borkmannc2101292015-10-29 14:58:07 +01001070 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001071}
1072
Brenden Blanco59d36562016-07-19 12:16:46 -07001073struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001074{
Brenden Blanco59d36562016-07-19 12:16:46 -07001075 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1076 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001077 return ERR_PTR(-EBUSY);
1078 }
1079 return prog;
1080}
Brenden Blanco59d36562016-07-19 12:16:46 -07001081EXPORT_SYMBOL_GPL(bpf_prog_add);
1082
Daniel Borkmannc5405942016-11-09 22:02:34 +01001083void bpf_prog_sub(struct bpf_prog *prog, int i)
1084{
1085 /* Only to be used for undoing previous bpf_prog_add() in some
1086 * error path. We still know that another entity in our call
1087 * path holds a reference to the program, thus atomic_sub() can
1088 * be safely used in such cases!
1089 */
1090 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1091}
1092EXPORT_SYMBOL_GPL(bpf_prog_sub);
1093
Brenden Blanco59d36562016-07-19 12:16:46 -07001094struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1095{
1096 return bpf_prog_add(prog, 1);
1097}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001098EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001099
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001100/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001101struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001102{
1103 int refold;
1104
1105 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1106
1107 if (refold >= BPF_MAX_REFCNT) {
1108 __bpf_prog_put(prog, false);
1109 return ERR_PTR(-EBUSY);
1110 }
1111
1112 if (!refold)
1113 return ERR_PTR(-ENOENT);
1114
1115 return prog;
1116}
John Fastabenda6f6df62017-08-15 22:32:22 -07001117EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001118
Al Viro040ee692017-12-02 20:20:38 -05001119bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001120 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001121{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001122 /* not an attachment, just a refcount inc, always allow */
1123 if (!attach_type)
1124 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001125
1126 if (prog->type != *attach_type)
1127 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001128 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001129 return false;
1130
1131 return true;
1132}
1133
1134static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001135 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001136{
1137 struct fd f = fdget(ufd);
1138 struct bpf_prog *prog;
1139
Daniel Borkmann113214b2016-06-30 17:24:44 +02001140 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001141 if (IS_ERR(prog))
1142 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001143 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001144 prog = ERR_PTR(-EINVAL);
1145 goto out;
1146 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001147
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001148 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001149out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001150 fdput(f);
1151 return prog;
1152}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001153
1154struct bpf_prog *bpf_prog_get(u32 ufd)
1155{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001156 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001157}
1158
Jakub Kicinski248f3462017-11-03 13:56:20 -07001159struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001160 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001161{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001162 struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001163
1164 if (!IS_ERR(prog))
1165 trace_bpf_prog_get_type(prog);
1166 return prog;
1167}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001168EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001169
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001170/* last field in 'union bpf_attr' used by this command */
Jakub Kicinski1f6f4cb2017-11-20 15:21:53 -08001171#define BPF_PROG_LOAD_LAST_FIELD prog_ifindex
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001172
1173static int bpf_prog_load(union bpf_attr *attr)
1174{
1175 enum bpf_prog_type type = attr->prog_type;
1176 struct bpf_prog *prog;
1177 int err;
1178 char license[128];
1179 bool is_gpl;
1180
1181 if (CHECK_ATTR(BPF_PROG_LOAD))
1182 return -EINVAL;
1183
David S. Millere07b98d2017-05-10 11:38:07 -07001184 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1185 return -EINVAL;
1186
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001187 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001188 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001189 sizeof(license) - 1) < 0)
1190 return -EFAULT;
1191 license[sizeof(license) - 1] = 0;
1192
1193 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1194 is_gpl = license_is_gpl_compatible(license);
1195
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001196 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1197 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001198
Alexei Starovoitov25415172015-03-25 12:49:20 -07001199 if (type == BPF_PROG_TYPE_KPROBE &&
1200 attr->kern_version != LINUX_VERSION_CODE)
1201 return -EINVAL;
1202
Chenbo Feng80b7d812017-05-31 18:16:00 -07001203 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1204 type != BPF_PROG_TYPE_CGROUP_SKB &&
1205 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001206 return -EPERM;
1207
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001208 /* plain bpf_prog allocation */
1209 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1210 if (!prog)
1211 return -ENOMEM;
1212
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001213 prog->aux->offload_requested = !!attr->prog_ifindex;
1214
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001215 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001216 if (err)
1217 goto free_prog_nouncharge;
1218
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001219 err = bpf_prog_charge_memlock(prog);
1220 if (err)
1221 goto free_prog_sec;
1222
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001223 prog->len = attr->insn_cnt;
1224
1225 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001226 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001227 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001228 goto free_prog;
1229
1230 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001231 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001232
1233 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001234 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001235
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001236 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001237 err = bpf_prog_offload_init(prog, attr);
1238 if (err)
1239 goto free_prog;
1240 }
1241
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001242 /* find program type: socket_filter vs tracing_filter */
1243 err = find_prog_type(type, prog);
1244 if (err < 0)
1245 goto free_prog;
1246
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001247 prog->aux->load_time = ktime_get_boot_ns();
1248 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1249 if (err)
1250 goto free_prog;
1251
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001252 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001253 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001254 if (err < 0)
1255 goto free_used_maps;
1256
1257 /* eBPF program is ready to be JITed */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08001258 if (!prog->bpf_func)
1259 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001260 if (err < 0)
1261 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001262
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001263 err = bpf_prog_alloc_id(prog);
1264 if (err)
1265 goto free_used_maps;
1266
Daniel Borkmannaa797812015-10-29 14:58:06 +01001267 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001268 if (err < 0) {
1269 /* failed to allocate fd.
1270 * bpf_prog_put() is needed because the above
1271 * bpf_prog_alloc_id() has published the prog
1272 * to the userspace and the userspace may
1273 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1274 */
1275 bpf_prog_put(prog);
1276 return err;
1277 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001278
Daniel Borkmann74451e662017-02-16 22:24:50 +01001279 bpf_prog_kallsyms_add(prog);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001280 trace_bpf_prog_load(prog, err);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001281 return err;
1282
1283free_used_maps:
1284 free_used_maps(prog->aux);
1285free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001286 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001287free_prog_sec:
1288 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001289free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001290 bpf_prog_free(prog);
1291 return err;
1292}
1293
Chenbo Feng6e71b042017-10-18 13:00:22 -07001294#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001295
1296static int bpf_obj_pin(const union bpf_attr *attr)
1297{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001298 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001299 return -EINVAL;
1300
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001301 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001302}
1303
1304static int bpf_obj_get(const union bpf_attr *attr)
1305{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001306 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1307 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001308 return -EINVAL;
1309
Chenbo Feng6e71b042017-10-18 13:00:22 -07001310 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1311 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001312}
1313
Daniel Mackf4324552016-11-23 16:52:27 +01001314#ifdef CONFIG_CGROUP_BPF
1315
John Fastabend464bc0f2017-08-28 07:10:04 -07001316#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001317
John Fastabend5a67da22017-09-08 14:00:49 -07001318static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
John Fastabend174a79f2017-08-15 22:32:47 -07001319{
John Fastabend5a67da22017-09-08 14:00:49 -07001320 struct bpf_prog *prog = NULL;
John Fastabend174a79f2017-08-15 22:32:47 -07001321 int ufd = attr->target_fd;
1322 struct bpf_map *map;
1323 struct fd f;
1324 int err;
1325
1326 f = fdget(ufd);
1327 map = __bpf_map_get(f);
1328 if (IS_ERR(map))
1329 return PTR_ERR(map);
1330
John Fastabend5a67da22017-09-08 14:00:49 -07001331 if (attach) {
1332 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1333 BPF_PROG_TYPE_SK_SKB);
1334 if (IS_ERR(prog)) {
1335 fdput(f);
1336 return PTR_ERR(prog);
1337 }
John Fastabend174a79f2017-08-15 22:32:47 -07001338 }
1339
John Fastabend5a67da22017-09-08 14:00:49 -07001340 err = sock_map_prog(map, prog, attr->attach_type);
John Fastabend174a79f2017-08-15 22:32:47 -07001341 if (err) {
1342 fdput(f);
John Fastabend5a67da22017-09-08 14:00:49 -07001343 if (prog)
1344 bpf_prog_put(prog);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001345 return err;
John Fastabend174a79f2017-08-15 22:32:47 -07001346 }
1347
1348 fdput(f);
Dan Carpenterae2b27b2017-08-18 10:27:02 +03001349 return 0;
John Fastabend174a79f2017-08-15 22:32:47 -07001350}
Daniel Mackf4324552016-11-23 16:52:27 +01001351
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001352#define BPF_F_ATTACH_MASK \
1353 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1354
Daniel Mackf4324552016-11-23 16:52:27 +01001355static int bpf_prog_attach(const union bpf_attr *attr)
1356{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001357 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001358 struct bpf_prog *prog;
1359 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001360 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001361
1362 if (!capable(CAP_NET_ADMIN))
1363 return -EPERM;
1364
1365 if (CHECK_ATTR(BPF_PROG_ATTACH))
1366 return -EINVAL;
1367
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001368 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001369 return -EINVAL;
1370
Daniel Mackf4324552016-11-23 16:52:27 +01001371 switch (attr->attach_type) {
1372 case BPF_CGROUP_INET_INGRESS:
1373 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001374 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001375 break;
David Ahern610236582016-12-01 08:48:04 -08001376 case BPF_CGROUP_INET_SOCK_CREATE:
1377 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1378 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001379 case BPF_CGROUP_SOCK_OPS:
1380 ptype = BPF_PROG_TYPE_SOCK_OPS;
1381 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001382 case BPF_CGROUP_DEVICE:
1383 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1384 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001385 case BPF_SK_SKB_STREAM_PARSER:
1386 case BPF_SK_SKB_STREAM_VERDICT:
John Fastabend5a67da22017-09-08 14:00:49 -07001387 return sockmap_get_from_fd(attr, true);
Daniel Mackf4324552016-11-23 16:52:27 +01001388 default:
1389 return -EINVAL;
1390 }
1391
David Ahernb2cd1252016-12-01 08:48:03 -08001392 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1393 if (IS_ERR(prog))
1394 return PTR_ERR(prog);
1395
1396 cgrp = cgroup_get_from_fd(attr->target_fd);
1397 if (IS_ERR(cgrp)) {
1398 bpf_prog_put(prog);
1399 return PTR_ERR(cgrp);
1400 }
1401
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001402 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1403 attr->attach_flags);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001404 if (ret)
1405 bpf_prog_put(prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001406 cgroup_put(cgrp);
1407
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001408 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001409}
1410
1411#define BPF_PROG_DETACH_LAST_FIELD attach_type
1412
1413static int bpf_prog_detach(const union bpf_attr *attr)
1414{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001415 enum bpf_prog_type ptype;
1416 struct bpf_prog *prog;
Daniel Mackf4324552016-11-23 16:52:27 +01001417 struct cgroup *cgrp;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001418 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001419
1420 if (!capable(CAP_NET_ADMIN))
1421 return -EPERM;
1422
1423 if (CHECK_ATTR(BPF_PROG_DETACH))
1424 return -EINVAL;
1425
1426 switch (attr->attach_type) {
1427 case BPF_CGROUP_INET_INGRESS:
1428 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001429 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1430 break;
David Ahern610236582016-12-01 08:48:04 -08001431 case BPF_CGROUP_INET_SOCK_CREATE:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001432 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1433 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001434 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001435 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001436 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001437 case BPF_CGROUP_DEVICE:
1438 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1439 break;
John Fastabend5a67da22017-09-08 14:00:49 -07001440 case BPF_SK_SKB_STREAM_PARSER:
1441 case BPF_SK_SKB_STREAM_VERDICT:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001442 return sockmap_get_from_fd(attr, false);
Daniel Mackf4324552016-11-23 16:52:27 +01001443 default:
1444 return -EINVAL;
1445 }
1446
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001447 cgrp = cgroup_get_from_fd(attr->target_fd);
1448 if (IS_ERR(cgrp))
1449 return PTR_ERR(cgrp);
1450
1451 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1452 if (IS_ERR(prog))
1453 prog = NULL;
1454
1455 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1456 if (prog)
1457 bpf_prog_put(prog);
1458 cgroup_put(cgrp);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001459 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001460}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001461
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001462#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1463
1464static int bpf_prog_query(const union bpf_attr *attr,
1465 union bpf_attr __user *uattr)
1466{
1467 struct cgroup *cgrp;
1468 int ret;
1469
1470 if (!capable(CAP_NET_ADMIN))
1471 return -EPERM;
1472 if (CHECK_ATTR(BPF_PROG_QUERY))
1473 return -EINVAL;
1474 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1475 return -EINVAL;
1476
1477 switch (attr->query.attach_type) {
1478 case BPF_CGROUP_INET_INGRESS:
1479 case BPF_CGROUP_INET_EGRESS:
1480 case BPF_CGROUP_INET_SOCK_CREATE:
1481 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001482 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001483 break;
1484 default:
1485 return -EINVAL;
1486 }
1487 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1488 if (IS_ERR(cgrp))
1489 return PTR_ERR(cgrp);
1490 ret = cgroup_bpf_query(cgrp, attr, uattr);
1491 cgroup_put(cgrp);
1492 return ret;
1493}
Daniel Mackf4324552016-11-23 16:52:27 +01001494#endif /* CONFIG_CGROUP_BPF */
1495
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001496#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1497
1498static int bpf_prog_test_run(const union bpf_attr *attr,
1499 union bpf_attr __user *uattr)
1500{
1501 struct bpf_prog *prog;
1502 int ret = -ENOTSUPP;
1503
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001504 if (!capable(CAP_SYS_ADMIN))
1505 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001506 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1507 return -EINVAL;
1508
1509 prog = bpf_prog_get(attr->test.prog_fd);
1510 if (IS_ERR(prog))
1511 return PTR_ERR(prog);
1512
1513 if (prog->aux->ops->test_run)
1514 ret = prog->aux->ops->test_run(prog, attr, uattr);
1515
1516 bpf_prog_put(prog);
1517 return ret;
1518}
1519
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001520#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1521
1522static int bpf_obj_get_next_id(const union bpf_attr *attr,
1523 union bpf_attr __user *uattr,
1524 struct idr *idr,
1525 spinlock_t *lock)
1526{
1527 u32 next_id = attr->start_id;
1528 int err = 0;
1529
1530 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1531 return -EINVAL;
1532
1533 if (!capable(CAP_SYS_ADMIN))
1534 return -EPERM;
1535
1536 next_id++;
1537 spin_lock_bh(lock);
1538 if (!idr_get_next(idr, &next_id))
1539 err = -ENOENT;
1540 spin_unlock_bh(lock);
1541
1542 if (!err)
1543 err = put_user(next_id, &uattr->next_id);
1544
1545 return err;
1546}
1547
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001548#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1549
1550static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1551{
1552 struct bpf_prog *prog;
1553 u32 id = attr->prog_id;
1554 int fd;
1555
1556 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1557 return -EINVAL;
1558
1559 if (!capable(CAP_SYS_ADMIN))
1560 return -EPERM;
1561
1562 spin_lock_bh(&prog_idr_lock);
1563 prog = idr_find(&prog_idr, id);
1564 if (prog)
1565 prog = bpf_prog_inc_not_zero(prog);
1566 else
1567 prog = ERR_PTR(-ENOENT);
1568 spin_unlock_bh(&prog_idr_lock);
1569
1570 if (IS_ERR(prog))
1571 return PTR_ERR(prog);
1572
1573 fd = bpf_prog_new_fd(prog);
1574 if (fd < 0)
1575 bpf_prog_put(prog);
1576
1577 return fd;
1578}
1579
Chenbo Feng6e71b042017-10-18 13:00:22 -07001580#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001581
1582static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1583{
1584 struct bpf_map *map;
1585 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001586 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001587 int fd;
1588
Chenbo Feng6e71b042017-10-18 13:00:22 -07001589 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1590 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001591 return -EINVAL;
1592
1593 if (!capable(CAP_SYS_ADMIN))
1594 return -EPERM;
1595
Chenbo Feng6e71b042017-10-18 13:00:22 -07001596 f_flags = bpf_get_file_flag(attr->open_flags);
1597 if (f_flags < 0)
1598 return f_flags;
1599
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001600 spin_lock_bh(&map_idr_lock);
1601 map = idr_find(&map_idr, id);
1602 if (map)
1603 map = bpf_map_inc_not_zero(map, true);
1604 else
1605 map = ERR_PTR(-ENOENT);
1606 spin_unlock_bh(&map_idr_lock);
1607
1608 if (IS_ERR(map))
1609 return PTR_ERR(map);
1610
Chenbo Feng6e71b042017-10-18 13:00:22 -07001611 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001612 if (fd < 0)
1613 bpf_map_put(map);
1614
1615 return fd;
1616}
1617
Daniel Borkmann7105e822017-12-20 13:42:57 +01001618static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1619 unsigned long addr)
1620{
1621 int i;
1622
1623 for (i = 0; i < prog->aux->used_map_cnt; i++)
1624 if (prog->aux->used_maps[i] == (void *)addr)
1625 return prog->aux->used_maps[i];
1626 return NULL;
1627}
1628
1629static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1630{
1631 const struct bpf_map *map;
1632 struct bpf_insn *insns;
1633 u64 imm;
1634 int i;
1635
1636 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1637 GFP_USER);
1638 if (!insns)
1639 return insns;
1640
1641 for (i = 0; i < prog->len; i++) {
1642 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1643 insns[i].code = BPF_JMP | BPF_CALL;
1644 insns[i].imm = BPF_FUNC_tail_call;
1645 /* fall-through */
1646 }
1647 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1648 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1649 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1650 insns[i].code = BPF_JMP | BPF_CALL;
1651 if (!bpf_dump_raw_ok())
1652 insns[i].imm = 0;
1653 continue;
1654 }
1655
1656 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1657 continue;
1658
1659 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1660 map = bpf_map_from_imm(prog, imm);
1661 if (map) {
1662 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1663 insns[i].imm = map->id;
1664 insns[i + 1].imm = 0;
1665 continue;
1666 }
1667
1668 if (!bpf_dump_raw_ok() &&
1669 imm == (unsigned long)prog->aux) {
1670 insns[i].imm = 0;
1671 insns[i + 1].imm = 0;
1672 continue;
1673 }
1674 }
1675
1676 return insns;
1677}
1678
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001679static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1680 const union bpf_attr *attr,
1681 union bpf_attr __user *uattr)
1682{
1683 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1684 struct bpf_prog_info info = {};
1685 u32 info_len = attr->info.info_len;
1686 char __user *uinsns;
1687 u32 ulen;
1688 int err;
1689
1690 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1691 if (err)
1692 return err;
1693 info_len = min_t(u32, sizeof(info), info_len);
1694
1695 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001696 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001697
1698 info.type = prog->type;
1699 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001700 info.load_time = prog->aux->load_time;
1701 info.created_by_uid = from_kuid_munged(current_user_ns(),
1702 prog->aux->user->uid);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001703
1704 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001705 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1706
1707 ulen = info.nr_map_ids;
1708 info.nr_map_ids = prog->aux->used_map_cnt;
1709 ulen = min_t(u32, info.nr_map_ids, ulen);
1710 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001711 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001712 u32 i;
1713
1714 for (i = 0; i < ulen; i++)
1715 if (put_user(prog->aux->used_maps[i]->id,
1716 &user_map_ids[i]))
1717 return -EFAULT;
1718 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001719
1720 if (!capable(CAP_SYS_ADMIN)) {
1721 info.jited_prog_len = 0;
1722 info.xlated_prog_len = 0;
1723 goto done;
1724 }
1725
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001726 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001727 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001728 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001729 struct bpf_insn *insns_sanitized;
1730 bool fault;
1731
1732 if (prog->blinded && !bpf_dump_raw_ok()) {
1733 info.xlated_prog_insns = 0;
1734 goto done;
1735 }
1736 insns_sanitized = bpf_insn_prepare_dump(prog);
1737 if (!insns_sanitized)
1738 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001739 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1740 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001741 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1742 kfree(insns_sanitized);
1743 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001744 return -EFAULT;
1745 }
1746
Jakub Kicinski675fc272017-12-27 18:39:09 -08001747 if (bpf_prog_is_dev_bound(prog->aux)) {
1748 err = bpf_prog_offload_info_fill(&info, prog);
1749 if (err)
1750 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001751 goto done;
1752 }
1753
1754 /* NOTE: the following code is supposed to be skipped for offload.
1755 * bpf_prog_offload_info_fill() is the place to fill similar fields
1756 * for offload.
1757 */
1758 ulen = info.jited_prog_len;
1759 info.jited_prog_len = prog->jited_len;
1760 if (info.jited_prog_len && ulen) {
1761 if (bpf_dump_raw_ok()) {
1762 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1763 ulen = min_t(u32, info.jited_prog_len, ulen);
1764 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1765 return -EFAULT;
1766 } else {
1767 info.jited_prog_insns = 0;
1768 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08001769 }
1770
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001771done:
1772 if (copy_to_user(uinfo, &info, info_len) ||
1773 put_user(info_len, &uattr->info.info_len))
1774 return -EFAULT;
1775
1776 return 0;
1777}
1778
1779static int bpf_map_get_info_by_fd(struct bpf_map *map,
1780 const union bpf_attr *attr,
1781 union bpf_attr __user *uattr)
1782{
1783 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1784 struct bpf_map_info info = {};
1785 u32 info_len = attr->info.info_len;
1786 int err;
1787
1788 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1789 if (err)
1790 return err;
1791 info_len = min_t(u32, sizeof(info), info_len);
1792
1793 info.type = map->map_type;
1794 info.id = map->id;
1795 info.key_size = map->key_size;
1796 info.value_size = map->value_size;
1797 info.max_entries = map->max_entries;
1798 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07001799 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001800
Jakub Kicinski52775b32018-01-17 19:13:28 -08001801 if (bpf_map_is_dev_bound(map)) {
1802 err = bpf_map_offload_info_fill(&info, map);
1803 if (err)
1804 return err;
1805 }
1806
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001807 if (copy_to_user(uinfo, &info, info_len) ||
1808 put_user(info_len, &uattr->info.info_len))
1809 return -EFAULT;
1810
1811 return 0;
1812}
1813
1814#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1815
1816static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1817 union bpf_attr __user *uattr)
1818{
1819 int ufd = attr->info.bpf_fd;
1820 struct fd f;
1821 int err;
1822
1823 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1824 return -EINVAL;
1825
1826 f = fdget(ufd);
1827 if (!f.file)
1828 return -EBADFD;
1829
1830 if (f.file->f_op == &bpf_prog_fops)
1831 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1832 uattr);
1833 else if (f.file->f_op == &bpf_map_fops)
1834 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1835 uattr);
1836 else
1837 err = -EINVAL;
1838
1839 fdput(f);
1840 return err;
1841}
1842
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001843SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1844{
1845 union bpf_attr attr = {};
1846 int err;
1847
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001848 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001849 return -EPERM;
1850
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001851 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1852 if (err)
1853 return err;
1854 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001855
1856 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1857 if (copy_from_user(&attr, uattr, size) != 0)
1858 return -EFAULT;
1859
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001860 err = security_bpf(cmd, &attr, size);
1861 if (err < 0)
1862 return err;
1863
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001864 switch (cmd) {
1865 case BPF_MAP_CREATE:
1866 err = map_create(&attr);
1867 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001868 case BPF_MAP_LOOKUP_ELEM:
1869 err = map_lookup_elem(&attr);
1870 break;
1871 case BPF_MAP_UPDATE_ELEM:
1872 err = map_update_elem(&attr);
1873 break;
1874 case BPF_MAP_DELETE_ELEM:
1875 err = map_delete_elem(&attr);
1876 break;
1877 case BPF_MAP_GET_NEXT_KEY:
1878 err = map_get_next_key(&attr);
1879 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001880 case BPF_PROG_LOAD:
1881 err = bpf_prog_load(&attr);
1882 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01001883 case BPF_OBJ_PIN:
1884 err = bpf_obj_pin(&attr);
1885 break;
1886 case BPF_OBJ_GET:
1887 err = bpf_obj_get(&attr);
1888 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001889#ifdef CONFIG_CGROUP_BPF
1890 case BPF_PROG_ATTACH:
1891 err = bpf_prog_attach(&attr);
1892 break;
1893 case BPF_PROG_DETACH:
1894 err = bpf_prog_detach(&attr);
1895 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001896 case BPF_PROG_QUERY:
1897 err = bpf_prog_query(&attr, uattr);
1898 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001899#endif
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001900 case BPF_PROG_TEST_RUN:
1901 err = bpf_prog_test_run(&attr, uattr);
1902 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001903 case BPF_PROG_GET_NEXT_ID:
1904 err = bpf_obj_get_next_id(&attr, uattr,
1905 &prog_idr, &prog_idr_lock);
1906 break;
1907 case BPF_MAP_GET_NEXT_ID:
1908 err = bpf_obj_get_next_id(&attr, uattr,
1909 &map_idr, &map_idr_lock);
1910 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001911 case BPF_PROG_GET_FD_BY_ID:
1912 err = bpf_prog_get_fd_by_id(&attr);
1913 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001914 case BPF_MAP_GET_FD_BY_ID:
1915 err = bpf_map_get_fd_by_id(&attr);
1916 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001917 case BPF_OBJ_GET_INFO_BY_FD:
1918 err = bpf_obj_get_info_by_fd(&attr, uattr);
1919 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001920 default:
1921 err = -EINVAL;
1922 break;
1923 }
1924
1925 return err;
1926}