blob: 5d141f16f6fa95dd21fbacdc6000521d26a394c0 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003 */
4#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01005#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +01006#include <linux/bpf_lirc.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07007#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07008#include <linux/syscalls.h>
9#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010010#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010011#include <linux/vmalloc.h>
12#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070013#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070014#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070015#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070016#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070017#include <linux/license.h>
18#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070019#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010020#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070021#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070022#include <linux/cred.h>
23#include <linux/timekeeping.h>
24#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010025#include <linux/nospec.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070026
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070027#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
33
Chenbo Feng6e71b042017-10-18 13:00:22 -070034#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
35
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080036DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070037static DEFINE_IDR(prog_idr);
38static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070039static DEFINE_IDR(map_idr);
40static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080041
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070042int sysctl_unprivileged_bpf_disabled __read_mostly;
43
Johannes Berg40077e02017-04-11 15:34:58 +020044static const struct bpf_map_ops * const bpf_map_types[] = {
45#define BPF_PROG_TYPE(_id, _ops)
46#define BPF_MAP_TYPE(_id, _ops) \
47 [_id] = &_ops,
48#include <linux/bpf_types.h>
49#undef BPF_PROG_TYPE
50#undef BPF_MAP_TYPE
51};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070052
Mickaël Salaün752ba562017-08-07 20:45:20 +020053/*
54 * If we're handed a bigger struct than we know of, ensure all the unknown bits
55 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
56 * we don't know about yet.
57 *
58 * There is a ToCToU between this function call and the following
59 * copy_from_user() call. However, this is not a concern since this function is
60 * meant to be a future-proofing of bits.
61 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070062int bpf_check_uarg_tail_zero(void __user *uaddr,
63 size_t expected_size,
64 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020065{
66 unsigned char __user *addr;
67 unsigned char __user *end;
68 unsigned char val;
69 int err;
70
Mickaël Salaün752ba562017-08-07 20:45:20 +020071 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
72 return -E2BIG;
73
Linus Torvalds96d4f262019-01-03 18:57:57 -080074 if (unlikely(!access_ok(uaddr, actual_size)))
Mickaël Salaün752ba562017-08-07 20:45:20 +020075 return -EFAULT;
76
Mickaël Salaün58291a72017-08-07 20:45:19 +020077 if (actual_size <= expected_size)
78 return 0;
79
80 addr = uaddr + expected_size;
81 end = uaddr + actual_size;
82
83 for (; addr < end; addr++) {
84 err = get_user(val, addr);
85 if (err)
86 return err;
87 if (val)
88 return -E2BIG;
89 }
90
91 return 0;
92}
93
Jakub Kicinskia3884572018-01-11 20:29:09 -080094const struct bpf_map_ops bpf_map_offload_ops = {
95 .map_alloc = bpf_map_offload_map_alloc,
96 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020097 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -080098};
99
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700100static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
101{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800102 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100103 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700104 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800105 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700106
Mark Rutland9ef09e32018-05-03 17:04:59 +0100107 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800108 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100109 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
110 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800111 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;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100125 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200126 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{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100131 /* We really just want to fail instead of triggering OOM killer
132 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
133 * which is used for lower order allocation requests.
134 *
135 * It has been observed that higher order allocation requests done by
136 * vmalloc with __GFP_NORETRY being set might fail due to not trying
137 * to reclaim memory from the page cache, thus we set
138 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100139 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100140
141 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100142 void *area;
143
144 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100145 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
146 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100147 if (area != NULL)
148 return area;
149 }
150
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100151 return __vmalloc_node_flags_caller(size, numa_node,
152 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
153 flags, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100154}
155
156void bpf_map_area_free(void *area)
157{
158 kvfree(area);
159}
160
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200161static u32 bpf_map_flags_retain_permanent(u32 flags)
162{
163 /* Some map creation flags are not tied to the map object but
164 * rather to the map fd instead, so they have no meaning upon
165 * map object inspection since multiple file descriptors with
166 * different (access) properties can exist here. Thus, given
167 * this has zero meaning for the map itself, lets clear these
168 * from here.
169 */
170 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
171}
172
Jakub Kicinskibd475642018-01-11 20:29:06 -0800173void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
174{
175 map->map_type = attr->map_type;
176 map->key_size = attr->key_size;
177 map->value_size = attr->value_size;
178 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200179 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800180 map->numa_node = bpf_map_attr_numa_node(attr);
181}
182
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700183static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700184{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700185 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700186
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700187 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
188 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700189 return -EPERM;
190 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700191 return 0;
192}
193
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700194static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
195{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700196 if (user)
197 atomic_long_sub(pages, &user->locked_vm);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700198}
199
Roman Gushchinc85d6912019-05-29 18:03:59 -0700200int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size)
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700201{
Roman Gushchinc85d6912019-05-29 18:03:59 -0700202 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
203 struct user_struct *user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700204 int ret;
205
Roman Gushchinc85d6912019-05-29 18:03:59 -0700206 if (size >= U32_MAX - PAGE_SIZE)
207 return -E2BIG;
208
209 user = get_current_user();
Roman Gushchinb936ca62019-05-29 18:03:58 -0700210 ret = bpf_charge_memlock(user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700211 if (ret) {
212 free_uid(user);
213 return ret;
214 }
Roman Gushchinb936ca62019-05-29 18:03:58 -0700215
216 mem->pages = pages;
217 mem->user = user;
218
219 return 0;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700220}
221
Roman Gushchinb936ca62019-05-29 18:03:58 -0700222void bpf_map_charge_finish(struct bpf_map_memory *mem)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700223{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700224 bpf_uncharge_memlock(mem->user, mem->pages);
225 free_uid(mem->user);
226}
Roman Gushchin3539b962019-05-29 18:03:57 -0700227
Roman Gushchinb936ca62019-05-29 18:03:58 -0700228void bpf_map_charge_move(struct bpf_map_memory *dst,
229 struct bpf_map_memory *src)
230{
231 *dst = *src;
232
233 /* Make sure src will not be used for the redundant uncharging. */
234 memset(src, 0, sizeof(struct bpf_map_memory));
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700235}
236
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700237int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
238{
239 int ret;
240
Roman Gushchin3539b962019-05-29 18:03:57 -0700241 ret = bpf_charge_memlock(map->memory.user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700242 if (ret)
243 return ret;
Roman Gushchin3539b962019-05-29 18:03:57 -0700244 map->memory.pages += pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700245 return ret;
246}
247
248void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
249{
Roman Gushchin3539b962019-05-29 18:03:57 -0700250 bpf_uncharge_memlock(map->memory.user, pages);
251 map->memory.pages -= pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700252}
253
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700254static int bpf_map_alloc_id(struct bpf_map *map)
255{
256 int id;
257
Shaohua Lib76354c2018-03-27 11:53:21 -0700258 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700259 spin_lock_bh(&map_idr_lock);
260 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
261 if (id > 0)
262 map->id = id;
263 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700264 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700265
266 if (WARN_ON_ONCE(!id))
267 return -ENOSPC;
268
269 return id > 0 ? 0 : id;
270}
271
Jakub Kicinskia3884572018-01-11 20:29:09 -0800272void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700273{
Eric Dumazet930651a2017-09-19 09:15:59 -0700274 unsigned long flags;
275
Jakub Kicinskia3884572018-01-11 20:29:09 -0800276 /* Offloaded maps are removed from the IDR store when their device
277 * disappears - even if someone holds an fd to them they are unusable,
278 * the memory is gone, all ops will fail; they are simply waiting for
279 * refcnt to drop to be freed.
280 */
281 if (!map->id)
282 return;
283
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700284 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700285 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700286 else
287 __acquire(&map_idr_lock);
288
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700289 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800290 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700291
292 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700293 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700294 else
295 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700296}
297
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700298/* called from workqueue */
299static void bpf_map_free_deferred(struct work_struct *work)
300{
301 struct bpf_map *map = container_of(work, struct bpf_map, work);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700302 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700303
Roman Gushchinb936ca62019-05-29 18:03:58 -0700304 bpf_map_charge_move(&mem, &map->memory);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700305 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700306 /* implementation dependent freeing */
307 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700308 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700309}
310
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100311static void bpf_map_put_uref(struct bpf_map *map)
312{
313 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700314 if (map->ops->map_release_uref)
315 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100316 }
317}
318
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700319/* decrement map refcnt and schedule it for freeing via workqueue
320 * (unrelying map implementation ops->map_free() might sleep)
321 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700322static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700323{
324 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700325 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700326 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700327 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700328 INIT_WORK(&map->work, bpf_map_free_deferred);
329 schedule_work(&map->work);
330 }
331}
332
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700333void bpf_map_put(struct bpf_map *map)
334{
335 __bpf_map_put(map, true);
336}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700337EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700338
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100339void bpf_map_put_with_uref(struct bpf_map *map)
340{
341 bpf_map_put_uref(map);
342 bpf_map_put(map);
343}
344
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700345static int bpf_map_release(struct inode *inode, struct file *filp)
346{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200347 struct bpf_map *map = filp->private_data;
348
349 if (map->ops->map_release)
350 map->ops->map_release(map, filp);
351
352 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700353 return 0;
354}
355
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200356static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
357{
358 fmode_t mode = f.file->f_mode;
359
360 /* Our file permissions may have been overridden by global
361 * map permissions facing syscall side.
362 */
363 if (READ_ONCE(map->frozen))
364 mode &= ~FMODE_CAN_WRITE;
365 return mode;
366}
367
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100368#ifdef CONFIG_PROC_FS
369static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
370{
371 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100372 const struct bpf_array *array;
373 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200374 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100375
376 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
377 array = container_of(map, struct bpf_array, map);
378 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200379 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100380 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100381
382 seq_printf(m,
383 "map_type:\t%u\n"
384 "key_size:\t%u\n"
385 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100386 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100387 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200388 "memlock:\t%llu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200389 "map_id:\t%u\n"
390 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100391 map->map_type,
392 map->key_size,
393 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100394 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100395 map->map_flags,
Roman Gushchin3539b962019-05-29 18:03:57 -0700396 map->memory.pages * 1ULL << PAGE_SHIFT,
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200397 map->id,
398 READ_ONCE(map->frozen));
Daniel Borkmann21116b72016-11-26 01:28:07 +0100399
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200400 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100401 seq_printf(m, "owner_prog_type:\t%u\n",
402 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200403 seq_printf(m, "owner_jited:\t%u\n",
404 owner_jited);
405 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100406}
407#endif
408
Chenbo Feng6e71b042017-10-18 13:00:22 -0700409static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
410 loff_t *ppos)
411{
412 /* We need this handler such that alloc_file() enables
413 * f_mode with FMODE_CAN_READ.
414 */
415 return -EINVAL;
416}
417
418static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
419 size_t siz, loff_t *ppos)
420{
421 /* We need this handler such that alloc_file() enables
422 * f_mode with FMODE_CAN_WRITE.
423 */
424 return -EINVAL;
425}
426
Chenbo Fengf66e4482017-10-18 13:00:26 -0700427const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100428#ifdef CONFIG_PROC_FS
429 .show_fdinfo = bpf_map_show_fdinfo,
430#endif
431 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700432 .read = bpf_dummy_read,
433 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700434};
435
Chenbo Feng6e71b042017-10-18 13:00:22 -0700436int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100437{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700438 int ret;
439
440 ret = security_bpf_map(map, OPEN_FMODE(flags));
441 if (ret < 0)
442 return ret;
443
Daniel Borkmannaa797812015-10-29 14:58:06 +0100444 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700445 flags | O_CLOEXEC);
446}
447
448int bpf_get_file_flag(int flags)
449{
450 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
451 return -EINVAL;
452 if (flags & BPF_F_RDONLY)
453 return O_RDONLY;
454 if (flags & BPF_F_WRONLY)
455 return O_WRONLY;
456 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100457}
458
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700459/* helper macro to check that unused fields 'union bpf_attr' are zero */
460#define CHECK_ATTR(CMD) \
461 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
462 sizeof(attr->CMD##_LAST_FIELD), 0, \
463 sizeof(*attr) - \
464 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
465 sizeof(attr->CMD##_LAST_FIELD)) != NULL
466
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700467/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
468 * Return 0 on success and < 0 on error.
469 */
470static int bpf_obj_name_cpy(char *dst, const char *src)
471{
472 const char *end = src + BPF_OBJ_NAME_LEN;
473
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700474 memset(dst, 0, BPF_OBJ_NAME_LEN);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200475 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700476 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200477 if (!isalnum(*src) &&
478 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700479 return -EINVAL;
480 *dst++ = *src++;
481 }
482
483 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
484 if (src == end)
485 return -EINVAL;
486
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700487 return 0;
488}
489
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200490int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800491 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200492 const struct btf_type *key_type,
493 const struct btf_type *value_type)
494{
495 return -ENOTSUPP;
496}
497
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800498static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200499 u32 btf_key_id, u32 btf_value_id)
500{
501 const struct btf_type *key_type, *value_type;
502 u32 key_size, value_size;
503 int ret = 0;
504
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200505 /* Some maps allow key to be unspecified. */
506 if (btf_key_id) {
507 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
508 if (!key_type || key_size != map->key_size)
509 return -EINVAL;
510 } else {
511 key_type = btf_type_by_id(btf, 0);
512 if (!map->ops->map_check_btf)
513 return -EINVAL;
514 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200515
516 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
517 if (!value_type || value_size != map->value_size)
518 return -EINVAL;
519
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800520 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
521
522 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200523 if (map->map_flags & BPF_F_RDONLY_PROG)
524 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800525 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800526 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700527 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
528 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800529 return -ENOTSUPP;
530 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
531 map->value_size) {
532 WARN_ONCE(1,
533 "verifier bug spin_lock_off %d value_size %d\n",
534 map->spin_lock_off, map->value_size);
535 return -EFAULT;
536 }
537 }
538
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200539 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800540 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200541
542 return ret;
543}
544
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700545#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700546/* called via syscall */
547static int map_create(union bpf_attr *attr)
548{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700549 int numa_node = bpf_map_attr_numa_node(attr);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700550 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700551 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700552 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700553 int err;
554
555 err = CHECK_ATTR(BPF_MAP_CREATE);
556 if (err)
557 return -EINVAL;
558
Chenbo Feng6e71b042017-10-18 13:00:22 -0700559 f_flags = bpf_get_file_flag(attr->map_flags);
560 if (f_flags < 0)
561 return f_flags;
562
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700563 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700564 ((unsigned int)numa_node >= nr_node_ids ||
565 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700566 return -EINVAL;
567
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700568 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
569 map = find_and_alloc_map(attr);
570 if (IS_ERR(map))
571 return PTR_ERR(map);
572
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700573 err = bpf_obj_name_cpy(map->name, attr->map_name);
574 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700575 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700576
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700577 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100578 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700579
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200580 if (attr->btf_key_type_id || attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700581 struct btf *btf;
582
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200583 if (!attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700584 err = -EINVAL;
Roman Gushchinb936ca62019-05-29 18:03:58 -0700585 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700586 }
587
588 btf = btf_get_by_fd(attr->btf_fd);
589 if (IS_ERR(btf)) {
590 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700591 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700592 }
593
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200594 err = map_check_btf(map, btf, attr->btf_key_type_id,
595 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700596 if (err) {
597 btf_put(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700598 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700599 }
600
601 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700602 map->btf_key_type_id = attr->btf_key_type_id;
603 map->btf_value_type_id = attr->btf_value_type_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800604 } else {
605 map->spin_lock_off = -EINVAL;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700606 }
607
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700608 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700609 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700610 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700611
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700612 err = bpf_map_alloc_id(map);
613 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700614 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700615
Chenbo Feng6e71b042017-10-18 13:00:22 -0700616 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700617 if (err < 0) {
618 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800619 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700620 * bpf_map_alloc_id() has published the map
621 * to the userspace and the userspace may
622 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
623 */
Peng Sun352d20d2019-02-27 22:36:25 +0800624 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700625 return err;
626 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700627
628 return err;
629
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700630free_map_sec:
631 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700632free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700633 btf_put(map->btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700634 bpf_map_charge_move(&mem, &map->memory);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700635 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700636 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700637 return err;
638}
639
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700640/* if error is returned, fd is released.
641 * On success caller should complete fd access with matching fdput()
642 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100643struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700644{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700645 if (!f.file)
646 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700647 if (f.file->f_op != &bpf_map_fops) {
648 fdput(f);
649 return ERR_PTR(-EINVAL);
650 }
651
Daniel Borkmannc2101292015-10-29 14:58:07 +0100652 return f.file->private_data;
653}
654
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700655/* prog's and map's refcnt limit */
656#define BPF_MAX_REFCNT 32768
657
658struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100659{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700660 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
661 atomic_dec(&map->refcnt);
662 return ERR_PTR(-EBUSY);
663 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100664 if (uref)
665 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700666 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100667}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700668EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100669
670struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100671{
672 struct fd f = fdget(ufd);
673 struct bpf_map *map;
674
675 map = __bpf_map_get(f);
676 if (IS_ERR(map))
677 return map;
678
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700679 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100680 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700681
682 return map;
683}
684
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700685/* map_idr_lock should have been held */
686static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
687 bool uref)
688{
689 int refold;
690
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100691 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700692
693 if (refold >= BPF_MAX_REFCNT) {
694 __bpf_map_put(map, false);
695 return ERR_PTR(-EBUSY);
696 }
697
698 if (!refold)
699 return ERR_PTR(-ENOENT);
700
701 if (uref)
702 atomic_inc(&map->usercnt);
703
704 return map;
705}
706
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800707int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
708{
709 return -ENOTSUPP;
710}
711
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200712static void *__bpf_copy_key(void __user *ukey, u64 key_size)
713{
714 if (key_size)
715 return memdup_user(ukey, key_size);
716
717 if (ukey)
718 return ERR_PTR(-EINVAL);
719
720 return NULL;
721}
722
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700723/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800724#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700725
726static int map_lookup_elem(union bpf_attr *attr)
727{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100728 void __user *ukey = u64_to_user_ptr(attr->key);
729 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700730 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700731 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800732 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800733 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200734 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700735 int err;
736
737 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
738 return -EINVAL;
739
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800740 if (attr->flags & ~BPF_F_LOCK)
741 return -EINVAL;
742
Daniel Borkmann592867b2015-09-08 18:00:09 +0200743 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100744 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700745 if (IS_ERR(map))
746 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200747 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700748 err = -EPERM;
749 goto err_put;
750 }
751
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800752 if ((attr->flags & BPF_F_LOCK) &&
753 !map_value_has_spin_lock(map)) {
754 err = -EINVAL;
755 goto err_put;
756 }
757
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200758 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400759 if (IS_ERR(key)) {
760 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700761 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400762 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700763
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800764 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800765 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000766 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
767 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800768 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700769 else if (IS_FD_MAP(map))
770 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800771 else
772 value_size = map->value_size;
773
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800774 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800775 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700776 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800777 goto free_key;
778
Jakub Kicinskia3884572018-01-11 20:29:09 -0800779 if (bpf_map_is_dev_bound(map)) {
780 err = bpf_map_offload_lookup_elem(map, key, value);
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800781 goto done;
782 }
783
784 preempt_disable();
785 this_cpu_inc(bpf_prog_active);
786 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
787 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800788 err = bpf_percpu_hash_copy(map, key, value);
789 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
790 err = bpf_percpu_array_copy(map, key, value);
Roman Gushchinb741f162018-09-28 14:45:43 +0000791 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
792 err = bpf_percpu_cgroup_storage_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800793 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
794 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700795 } else if (IS_FD_ARRAY(map)) {
796 err = bpf_fd_array_map_lookup_elem(map, key, value);
797 } else if (IS_FD_HASH(map)) {
798 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700799 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
800 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200801 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
802 map->map_type == BPF_MAP_TYPE_STACK) {
803 err = map->ops->map_peek_elem(map, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800804 } else {
805 rcu_read_lock();
Daniel Borkmannc6110222019-05-14 01:18:55 +0200806 if (map->ops->map_lookup_elem_sys_only)
807 ptr = map->ops->map_lookup_elem_sys_only(map, key);
808 else
809 ptr = map->ops->map_lookup_elem(map, key);
Prashant Bhole509db282018-10-09 10:04:49 +0900810 if (IS_ERR(ptr)) {
811 err = PTR_ERR(ptr);
812 } else if (!ptr) {
813 err = -ENOENT;
814 } else {
815 err = 0;
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800816 if (attr->flags & BPF_F_LOCK)
817 /* lock 'ptr' and copy everything but lock */
818 copy_map_value_locked(map, value, ptr, true);
819 else
820 copy_map_value(map, value, ptr);
821 /* mask lock, since value wasn't zero inited */
822 check_and_init_map_lock(map, value);
Prashant Bhole509db282018-10-09 10:04:49 +0900823 }
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800824 rcu_read_unlock();
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800825 }
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800826 this_cpu_dec(bpf_prog_active);
827 preempt_enable();
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800828
Martin KaFai Lau7c4cd052019-01-30 18:12:45 -0800829done:
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800830 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800831 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700832
833 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800834 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800835 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700836
837 err = 0;
838
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800839free_value:
840 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700841free_key:
842 kfree(key);
843err_put:
844 fdput(f);
845 return err;
846}
847
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700848static void maybe_wait_bpf_programs(struct bpf_map *map)
849{
850 /* Wait for any running BPF programs to complete so that
851 * userspace, when we return to it, knows that all programs
852 * that could be running use the new map value.
853 */
854 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
855 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
856 synchronize_rcu();
857}
858
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800859#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700860
861static int map_update_elem(union bpf_attr *attr)
862{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100863 void __user *ukey = u64_to_user_ptr(attr->key);
864 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700865 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700866 struct bpf_map *map;
867 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800868 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200869 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700870 int err;
871
872 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
873 return -EINVAL;
874
Daniel Borkmann592867b2015-09-08 18:00:09 +0200875 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100876 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700877 if (IS_ERR(map))
878 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200879 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700880 err = -EPERM;
881 goto err_put;
882 }
883
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800884 if ((attr->flags & BPF_F_LOCK) &&
885 !map_value_has_spin_lock(map)) {
886 err = -EINVAL;
887 goto err_put;
888 }
889
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200890 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400891 if (IS_ERR(key)) {
892 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700893 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400894 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700895
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800896 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800897 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +0000898 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
899 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800900 value_size = round_up(map->value_size, 8) * num_possible_cpus();
901 else
902 value_size = map->value_size;
903
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700904 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800905 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700906 if (!value)
907 goto free_key;
908
909 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800910 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700911 goto free_value;
912
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200913 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800914 if (bpf_map_is_dev_bound(map)) {
915 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
916 goto out;
John Fastabend99ba2b52018-07-05 08:50:04 -0700917 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
918 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
919 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200920 err = map->ops->map_update_elem(map, key, value, attr->flags);
921 goto out;
922 }
923
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800924 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
925 * inside bpf map update or delete otherwise deadlocks are possible
926 */
927 preempt_disable();
928 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800929 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
930 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800931 err = bpf_percpu_hash_update(map, key, value, attr->flags);
932 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
933 err = bpf_percpu_array_update(map, key, value, attr->flags);
Roman Gushchinb741f162018-09-28 14:45:43 +0000934 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
935 err = bpf_percpu_cgroup_storage_update(map, key, value,
936 attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100937 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200938 rcu_read_lock();
939 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
940 attr->flags);
941 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700942 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
943 rcu_read_lock();
944 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
945 attr->flags);
946 rcu_read_unlock();
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700947 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
948 /* rcu_read_lock() is not needed */
949 err = bpf_fd_reuseport_array_update_elem(map, key, value,
950 attr->flags);
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +0200951 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
952 map->map_type == BPF_MAP_TYPE_STACK) {
953 err = map->ops->map_push_elem(map, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800954 } else {
955 rcu_read_lock();
956 err = map->ops->map_update_elem(map, key, value, attr->flags);
957 rcu_read_unlock();
958 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800959 __this_cpu_dec(bpf_prog_active);
960 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -0700961 maybe_wait_bpf_programs(map);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200962out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700963free_value:
964 kfree(value);
965free_key:
966 kfree(key);
967err_put:
968 fdput(f);
969 return err;
970}
971
972#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
973
974static int map_delete_elem(union bpf_attr *attr)
975{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100976 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700977 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700978 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200979 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700980 void *key;
981 int err;
982
983 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
984 return -EINVAL;
985
Daniel Borkmann592867b2015-09-08 18:00:09 +0200986 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100987 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700988 if (IS_ERR(map))
989 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200990 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700991 err = -EPERM;
992 goto err_put;
993 }
994
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200995 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -0400996 if (IS_ERR(key)) {
997 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700998 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400999 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001000
Jakub Kicinskia3884572018-01-11 20:29:09 -08001001 if (bpf_map_is_dev_bound(map)) {
1002 err = bpf_map_offload_delete_elem(map, key);
1003 goto out;
1004 }
1005
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001006 preempt_disable();
1007 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001008 rcu_read_lock();
1009 err = map->ops->map_delete_elem(map, key);
1010 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001011 __this_cpu_dec(bpf_prog_active);
1012 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001013 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001014out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001015 kfree(key);
1016err_put:
1017 fdput(f);
1018 return err;
1019}
1020
1021/* last field in 'union bpf_attr' used by this command */
1022#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1023
1024static int map_get_next_key(union bpf_attr *attr)
1025{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001026 void __user *ukey = u64_to_user_ptr(attr->key);
1027 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001028 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001029 struct bpf_map *map;
1030 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001031 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001032 int err;
1033
1034 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1035 return -EINVAL;
1036
Daniel Borkmann592867b2015-09-08 18:00:09 +02001037 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001038 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001039 if (IS_ERR(map))
1040 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001041 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001042 err = -EPERM;
1043 goto err_put;
1044 }
1045
Teng Qin8fe45922017-04-24 19:00:37 -07001046 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001047 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001048 if (IS_ERR(key)) {
1049 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001050 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001051 }
Teng Qin8fe45922017-04-24 19:00:37 -07001052 } else {
1053 key = NULL;
1054 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001055
1056 err = -ENOMEM;
1057 next_key = kmalloc(map->key_size, GFP_USER);
1058 if (!next_key)
1059 goto free_key;
1060
Jakub Kicinskia3884572018-01-11 20:29:09 -08001061 if (bpf_map_is_dev_bound(map)) {
1062 err = bpf_map_offload_get_next_key(map, key, next_key);
1063 goto out;
1064 }
1065
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001066 rcu_read_lock();
1067 err = map->ops->map_get_next_key(map, key, next_key);
1068 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001069out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001070 if (err)
1071 goto free_next_key;
1072
1073 err = -EFAULT;
1074 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1075 goto free_next_key;
1076
1077 err = 0;
1078
1079free_next_key:
1080 kfree(next_key);
1081free_key:
1082 kfree(key);
1083err_put:
1084 fdput(f);
1085 return err;
1086}
1087
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001088#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1089
1090static int map_lookup_and_delete_elem(union bpf_attr *attr)
1091{
1092 void __user *ukey = u64_to_user_ptr(attr->key);
1093 void __user *uvalue = u64_to_user_ptr(attr->value);
1094 int ufd = attr->map_fd;
1095 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001096 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001097 u32 value_size;
1098 struct fd f;
1099 int err;
1100
1101 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1102 return -EINVAL;
1103
1104 f = fdget(ufd);
1105 map = __bpf_map_get(f);
1106 if (IS_ERR(map))
1107 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001108 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001109 err = -EPERM;
1110 goto err_put;
1111 }
1112
1113 key = __bpf_copy_key(ukey, map->key_size);
1114 if (IS_ERR(key)) {
1115 err = PTR_ERR(key);
1116 goto err_put;
1117 }
1118
1119 value_size = map->value_size;
1120
1121 err = -ENOMEM;
1122 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1123 if (!value)
1124 goto free_key;
1125
1126 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1127 map->map_type == BPF_MAP_TYPE_STACK) {
1128 err = map->ops->map_pop_elem(map, value);
1129 } else {
1130 err = -ENOTSUPP;
1131 }
1132
1133 if (err)
1134 goto free_value;
1135
1136 if (copy_to_user(uvalue, value, value_size) != 0)
1137 goto free_value;
1138
1139 err = 0;
1140
1141free_value:
1142 kfree(value);
1143free_key:
1144 kfree(key);
1145err_put:
1146 fdput(f);
1147 return err;
1148}
1149
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001150#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1151
1152static int map_freeze(const union bpf_attr *attr)
1153{
1154 int err = 0, ufd = attr->map_fd;
1155 struct bpf_map *map;
1156 struct fd f;
1157
1158 if (CHECK_ATTR(BPF_MAP_FREEZE))
1159 return -EINVAL;
1160
1161 f = fdget(ufd);
1162 map = __bpf_map_get(f);
1163 if (IS_ERR(map))
1164 return PTR_ERR(map);
1165 if (READ_ONCE(map->frozen)) {
1166 err = -EBUSY;
1167 goto err_put;
1168 }
1169 if (!capable(CAP_SYS_ADMIN)) {
1170 err = -EPERM;
1171 goto err_put;
1172 }
1173
1174 WRITE_ONCE(map->frozen, true);
1175err_put:
1176 fdput(f);
1177 return err;
1178}
1179
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001180static const struct bpf_prog_ops * const bpf_prog_types[] = {
1181#define BPF_PROG_TYPE(_id, _name) \
1182 [_id] = & _name ## _prog_ops,
1183#define BPF_MAP_TYPE(_id, _ops)
1184#include <linux/bpf_types.h>
1185#undef BPF_PROG_TYPE
1186#undef BPF_MAP_TYPE
1187};
1188
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001189static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1190{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001191 const struct bpf_prog_ops *ops;
1192
1193 if (type >= ARRAY_SIZE(bpf_prog_types))
1194 return -EINVAL;
1195 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1196 ops = bpf_prog_types[type];
1197 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001198 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001199
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001200 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001201 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001202 else
1203 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001204 prog->type = type;
1205 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001206}
1207
1208/* drop refcnt on maps used by eBPF program and free auxilary data */
1209static void free_used_maps(struct bpf_prog_aux *aux)
1210{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001211 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001212 int i;
1213
Roman Gushchin8bad74f2018-09-28 14:45:36 +00001214 for_each_cgroup_storage_type(stype) {
1215 if (!aux->cgroup_storage[stype])
1216 continue;
1217 bpf_cgroup_storage_release(aux->prog,
1218 aux->cgroup_storage[stype]);
1219 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07001220
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001221 for (i = 0; i < aux->used_map_cnt; i++)
1222 bpf_map_put(aux->used_maps[i]);
1223
1224 kfree(aux->used_maps);
1225}
1226
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001227int __bpf_prog_charge(struct user_struct *user, u32 pages)
1228{
1229 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1230 unsigned long user_bufs;
1231
1232 if (user) {
1233 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1234 if (user_bufs > memlock_limit) {
1235 atomic_long_sub(pages, &user->locked_vm);
1236 return -EPERM;
1237 }
1238 }
1239
1240 return 0;
1241}
1242
1243void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1244{
1245 if (user)
1246 atomic_long_sub(pages, &user->locked_vm);
1247}
1248
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001249static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1250{
1251 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001252 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001253
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001254 ret = __bpf_prog_charge(user, prog->pages);
1255 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001256 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001257 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001258 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001259
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001260 prog->aux->user = user;
1261 return 0;
1262}
1263
1264static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1265{
1266 struct user_struct *user = prog->aux->user;
1267
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001268 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001269 free_uid(user);
1270}
1271
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001272static int bpf_prog_alloc_id(struct bpf_prog *prog)
1273{
1274 int id;
1275
Shaohua Lib76354c2018-03-27 11:53:21 -07001276 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001277 spin_lock_bh(&prog_idr_lock);
1278 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1279 if (id > 0)
1280 prog->aux->id = id;
1281 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001282 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001283
1284 /* id is in [1, INT_MAX) */
1285 if (WARN_ON_ONCE(!id))
1286 return -ENOSPC;
1287
1288 return id > 0 ? 0 : id;
1289}
1290
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001291void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001292{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001293 /* cBPF to eBPF migrations are currently not in the idr store.
1294 * Offloaded programs are removed from the store when their device
1295 * disappears - even if someone grabs an fd to them they are unusable,
1296 * simply waiting for refcnt to drop to be freed.
1297 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001298 if (!prog->aux->id)
1299 return;
1300
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001301 if (do_idr_lock)
1302 spin_lock_bh(&prog_idr_lock);
1303 else
1304 __acquire(&prog_idr_lock);
1305
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001306 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001307 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001308
1309 if (do_idr_lock)
1310 spin_unlock_bh(&prog_idr_lock);
1311 else
1312 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001313}
1314
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001315static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001316{
1317 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1318
1319 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001320 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001321 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001322 bpf_prog_free(aux->prog);
1323}
1324
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001325static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001326{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001327 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001328 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001329 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001330 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001331 bpf_prog_kallsyms_del_all(prog);
Yonghong Song838e9692018-11-19 15:29:11 -08001332 btf_put(prog->aux->btf);
Yonghong Songba64e7d2018-11-24 23:20:44 -08001333 kvfree(prog->aux->func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001334 bpf_prog_free_linfo(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001335
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001336 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001337 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001338}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001339
1340void bpf_prog_put(struct bpf_prog *prog)
1341{
1342 __bpf_prog_put(prog, true);
1343}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001344EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001345
1346static int bpf_prog_release(struct inode *inode, struct file *filp)
1347{
1348 struct bpf_prog *prog = filp->private_data;
1349
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001350 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001351 return 0;
1352}
1353
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001354static void bpf_prog_get_stats(const struct bpf_prog *prog,
1355 struct bpf_prog_stats *stats)
1356{
1357 u64 nsecs = 0, cnt = 0;
1358 int cpu;
1359
1360 for_each_possible_cpu(cpu) {
1361 const struct bpf_prog_stats *st;
1362 unsigned int start;
1363 u64 tnsecs, tcnt;
1364
1365 st = per_cpu_ptr(prog->aux->stats, cpu);
1366 do {
1367 start = u64_stats_fetch_begin_irq(&st->syncp);
1368 tnsecs = st->nsecs;
1369 tcnt = st->cnt;
1370 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1371 nsecs += tnsecs;
1372 cnt += tcnt;
1373 }
1374 stats->nsecs = nsecs;
1375 stats->cnt = cnt;
1376}
1377
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001378#ifdef CONFIG_PROC_FS
1379static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1380{
1381 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001382 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001383 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001384
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001385 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001386 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001387 seq_printf(m,
1388 "prog_type:\t%u\n"
1389 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001390 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001391 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001392 "prog_id:\t%u\n"
1393 "run_time_ns:\t%llu\n"
1394 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001395 prog->type,
1396 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001397 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001398 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001399 prog->aux->id,
1400 stats.nsecs,
1401 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001402}
1403#endif
1404
Chenbo Fengf66e4482017-10-18 13:00:26 -07001405const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001406#ifdef CONFIG_PROC_FS
1407 .show_fdinfo = bpf_prog_show_fdinfo,
1408#endif
1409 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001410 .read = bpf_dummy_read,
1411 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001412};
1413
Daniel Borkmannb2197752015-10-29 14:58:09 +01001414int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001415{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001416 int ret;
1417
1418 ret = security_bpf_prog(prog);
1419 if (ret < 0)
1420 return ret;
1421
Daniel Borkmannaa797812015-10-29 14:58:06 +01001422 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1423 O_RDWR | O_CLOEXEC);
1424}
1425
Daniel Borkmann113214b2016-06-30 17:24:44 +02001426static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001427{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001428 if (!f.file)
1429 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001430 if (f.file->f_op != &bpf_prog_fops) {
1431 fdput(f);
1432 return ERR_PTR(-EINVAL);
1433 }
1434
Daniel Borkmannc2101292015-10-29 14:58:07 +01001435 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001436}
1437
Brenden Blanco59d36562016-07-19 12:16:46 -07001438struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001439{
Brenden Blanco59d36562016-07-19 12:16:46 -07001440 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1441 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001442 return ERR_PTR(-EBUSY);
1443 }
1444 return prog;
1445}
Brenden Blanco59d36562016-07-19 12:16:46 -07001446EXPORT_SYMBOL_GPL(bpf_prog_add);
1447
Daniel Borkmannc5405942016-11-09 22:02:34 +01001448void bpf_prog_sub(struct bpf_prog *prog, int i)
1449{
1450 /* Only to be used for undoing previous bpf_prog_add() in some
1451 * error path. We still know that another entity in our call
1452 * path holds a reference to the program, thus atomic_sub() can
1453 * be safely used in such cases!
1454 */
1455 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1456}
1457EXPORT_SYMBOL_GPL(bpf_prog_sub);
1458
Brenden Blanco59d36562016-07-19 12:16:46 -07001459struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1460{
1461 return bpf_prog_add(prog, 1);
1462}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001463EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001464
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001465/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001466struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001467{
1468 int refold;
1469
Mark Rutlandbfc18e32018-06-21 13:13:04 +01001470 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001471
1472 if (refold >= BPF_MAX_REFCNT) {
1473 __bpf_prog_put(prog, false);
1474 return ERR_PTR(-EBUSY);
1475 }
1476
1477 if (!refold)
1478 return ERR_PTR(-ENOENT);
1479
1480 return prog;
1481}
John Fastabenda6f6df62017-08-15 22:32:22 -07001482EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001483
Al Viro040ee692017-12-02 20:20:38 -05001484bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001485 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001486{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001487 /* not an attachment, just a refcount inc, always allow */
1488 if (!attach_type)
1489 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001490
1491 if (prog->type != *attach_type)
1492 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001493 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001494 return false;
1495
1496 return true;
1497}
1498
1499static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001500 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001501{
1502 struct fd f = fdget(ufd);
1503 struct bpf_prog *prog;
1504
Daniel Borkmann113214b2016-06-30 17:24:44 +02001505 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001506 if (IS_ERR(prog))
1507 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001508 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001509 prog = ERR_PTR(-EINVAL);
1510 goto out;
1511 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001512
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001513 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001514out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001515 fdput(f);
1516 return prog;
1517}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001518
1519struct bpf_prog *bpf_prog_get(u32 ufd)
1520{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001521 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001522}
1523
Jakub Kicinski248f3462017-11-03 13:56:20 -07001524struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001525 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001526{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001527 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001528}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001529EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001530
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001531/* Initially all BPF programs could be loaded w/o specifying
1532 * expected_attach_type. Later for some of them specifying expected_attach_type
1533 * at load time became required so that program could be validated properly.
1534 * Programs of types that are allowed to be loaded both w/ and w/o (for
1535 * backward compatibility) expected_attach_type, should have the default attach
1536 * type assigned to expected_attach_type for the latter case, so that it can be
1537 * validated later at attach time.
1538 *
1539 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1540 * prog type requires it but has some attach types that have to be backward
1541 * compatible.
1542 */
1543static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1544{
1545 switch (attr->prog_type) {
1546 case BPF_PROG_TYPE_CGROUP_SOCK:
1547 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1548 * exist so checking for non-zero is the way to go here.
1549 */
1550 if (!attr->expected_attach_type)
1551 attr->expected_attach_type =
1552 BPF_CGROUP_INET_SOCK_CREATE;
1553 break;
1554 }
1555}
1556
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001557static int
1558bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1559 enum bpf_attach_type expected_attach_type)
1560{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001561 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001562 case BPF_PROG_TYPE_CGROUP_SOCK:
1563 switch (expected_attach_type) {
1564 case BPF_CGROUP_INET_SOCK_CREATE:
1565 case BPF_CGROUP_INET4_POST_BIND:
1566 case BPF_CGROUP_INET6_POST_BIND:
1567 return 0;
1568 default:
1569 return -EINVAL;
1570 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001571 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1572 switch (expected_attach_type) {
1573 case BPF_CGROUP_INET4_BIND:
1574 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001575 case BPF_CGROUP_INET4_CONNECT:
1576 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001577 case BPF_CGROUP_UDP4_SENDMSG:
1578 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02001579 case BPF_CGROUP_UDP4_RECVMSG:
1580 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001581 return 0;
1582 default:
1583 return -EINVAL;
1584 }
brakmo5cf1e912019-05-28 16:59:36 -07001585 case BPF_PROG_TYPE_CGROUP_SKB:
1586 switch (expected_attach_type) {
1587 case BPF_CGROUP_INET_INGRESS:
1588 case BPF_CGROUP_INET_EGRESS:
1589 return 0;
1590 default:
1591 return -EINVAL;
1592 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001593 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1594 switch (expected_attach_type) {
1595 case BPF_CGROUP_SETSOCKOPT:
1596 case BPF_CGROUP_GETSOCKOPT:
1597 return 0;
1598 default:
1599 return -EINVAL;
1600 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001601 default:
1602 return 0;
1603 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001604}
1605
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001606/* last field in 'union bpf_attr' used by this command */
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001607#define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001608
Yonghong Song838e9692018-11-19 15:29:11 -08001609static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001610{
1611 enum bpf_prog_type type = attr->prog_type;
1612 struct bpf_prog *prog;
1613 int err;
1614 char license[128];
1615 bool is_gpl;
1616
1617 if (CHECK_ATTR(BPF_PROG_LOAD))
1618 return -EINVAL;
1619
Jiong Wangc240eff2019-05-24 23:25:16 +01001620 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
1621 BPF_F_ANY_ALIGNMENT |
1622 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07001623 return -EINVAL;
1624
David Millere9ee9ef2018-11-30 21:08:14 -08001625 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1626 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1627 !capable(CAP_SYS_ADMIN))
1628 return -EPERM;
1629
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001630 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001631 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001632 sizeof(license) - 1) < 0)
1633 return -EFAULT;
1634 license[sizeof(license) - 1] = 0;
1635
1636 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1637 is_gpl = license_is_gpl_compatible(license);
1638
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07001639 if (attr->insn_cnt == 0 ||
1640 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001641 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07001642 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1643 type != BPF_PROG_TYPE_CGROUP_SKB &&
1644 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001645 return -EPERM;
1646
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001647 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001648 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1649 return -EINVAL;
1650
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001651 /* plain bpf_prog allocation */
1652 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1653 if (!prog)
1654 return -ENOMEM;
1655
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001656 prog->expected_attach_type = attr->expected_attach_type;
1657
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001658 prog->aux->offload_requested = !!attr->prog_ifindex;
1659
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001660 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001661 if (err)
1662 goto free_prog_nouncharge;
1663
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001664 err = bpf_prog_charge_memlock(prog);
1665 if (err)
1666 goto free_prog_sec;
1667
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001668 prog->len = attr->insn_cnt;
1669
1670 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001671 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001672 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001673 goto free_prog;
1674
1675 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001676 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001677
1678 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001679 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001680
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001681 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001682 err = bpf_prog_offload_init(prog, attr);
1683 if (err)
1684 goto free_prog;
1685 }
1686
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001687 /* find program type: socket_filter vs tracing_filter */
1688 err = find_prog_type(type, prog);
1689 if (err < 0)
1690 goto free_prog;
1691
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02001692 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001693 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1694 if (err)
1695 goto free_prog;
1696
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001697 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08001698 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001699 if (err < 0)
1700 goto free_used_maps;
1701
Daniel Borkmann9facc332018-06-15 02:30:48 +02001702 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001703 if (err < 0)
1704 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001705
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001706 err = bpf_prog_alloc_id(prog);
1707 if (err)
1708 goto free_used_maps;
1709
Daniel Borkmannaa797812015-10-29 14:58:06 +01001710 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001711 if (err < 0) {
1712 /* failed to allocate fd.
1713 * bpf_prog_put() is needed because the above
1714 * bpf_prog_alloc_id() has published the prog
1715 * to the userspace and the userspace may
1716 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1717 */
1718 bpf_prog_put(prog);
1719 return err;
1720 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001721
Daniel Borkmann74451e662017-02-16 22:24:50 +01001722 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08001723 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001724 return err;
1725
1726free_used_maps:
Martin KaFai Lauc454a462018-12-07 16:42:25 -08001727 bpf_prog_free_linfo(prog);
Martin KaFai Lau5482e9a2018-12-01 17:08:44 -08001728 kvfree(prog->aux->func_info);
1729 btf_put(prog->aux->btf);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001730 bpf_prog_kallsyms_del_subprogs(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001731 free_used_maps(prog->aux);
1732free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001733 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001734free_prog_sec:
1735 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001736free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001737 bpf_prog_free(prog);
1738 return err;
1739}
1740
Chenbo Feng6e71b042017-10-18 13:00:22 -07001741#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001742
1743static int bpf_obj_pin(const union bpf_attr *attr)
1744{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001745 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001746 return -EINVAL;
1747
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001748 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001749}
1750
1751static int bpf_obj_get(const union bpf_attr *attr)
1752{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001753 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1754 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001755 return -EINVAL;
1756
Chenbo Feng6e71b042017-10-18 13:00:22 -07001757 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1758 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001759}
1760
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001761struct bpf_raw_tracepoint {
1762 struct bpf_raw_event_map *btp;
1763 struct bpf_prog *prog;
1764};
1765
1766static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1767{
1768 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1769
1770 if (raw_tp->prog) {
1771 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1772 bpf_prog_put(raw_tp->prog);
1773 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08001774 bpf_put_raw_tracepoint(raw_tp->btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001775 kfree(raw_tp);
1776 return 0;
1777}
1778
1779static const struct file_operations bpf_raw_tp_fops = {
1780 .release = bpf_raw_tracepoint_release,
1781 .read = bpf_dummy_read,
1782 .write = bpf_dummy_write,
1783};
1784
1785#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1786
1787static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1788{
1789 struct bpf_raw_tracepoint *raw_tp;
1790 struct bpf_raw_event_map *btp;
1791 struct bpf_prog *prog;
1792 char tp_name[128];
1793 int tp_fd, err;
1794
1795 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1796 sizeof(tp_name) - 1) < 0)
1797 return -EFAULT;
1798 tp_name[sizeof(tp_name) - 1] = 0;
1799
Matt Mullinsa38d1102018-12-12 16:42:37 -08001800 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001801 if (!btp)
1802 return -ENOENT;
1803
1804 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001805 if (!raw_tp) {
1806 err = -ENOMEM;
1807 goto out_put_btp;
1808 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001809 raw_tp->btp = btp;
1810
Matt Mullins9df1c282019-04-26 11:49:47 -07001811 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001812 if (IS_ERR(prog)) {
1813 err = PTR_ERR(prog);
1814 goto out_free_tp;
1815 }
Matt Mullins9df1c282019-04-26 11:49:47 -07001816 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1817 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1818 err = -EINVAL;
1819 goto out_put_prog;
1820 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001821
1822 err = bpf_probe_register(raw_tp->btp, prog);
1823 if (err)
1824 goto out_put_prog;
1825
1826 raw_tp->prog = prog;
1827 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1828 O_CLOEXEC);
1829 if (tp_fd < 0) {
1830 bpf_probe_unregister(raw_tp->btp, prog);
1831 err = tp_fd;
1832 goto out_put_prog;
1833 }
1834 return tp_fd;
1835
1836out_put_prog:
1837 bpf_prog_put(prog);
1838out_free_tp:
1839 kfree(raw_tp);
Matt Mullinsa38d1102018-12-12 16:42:37 -08001840out_put_btp:
1841 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001842 return err;
1843}
1844
Anders Roxell33491582018-04-03 14:09:47 +02001845static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1846 enum bpf_attach_type attach_type)
1847{
1848 switch (prog->type) {
1849 case BPF_PROG_TYPE_CGROUP_SOCK:
1850 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001851 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Anders Roxell33491582018-04-03 14:09:47 +02001852 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07001853 case BPF_PROG_TYPE_CGROUP_SKB:
1854 return prog->enforce_expected_attach_type &&
1855 prog->expected_attach_type != attach_type ?
1856 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02001857 default:
1858 return 0;
1859 }
1860}
1861
John Fastabend464bc0f2017-08-28 07:10:04 -07001862#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001863
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001864#define BPF_F_ATTACH_MASK \
1865 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1866
Daniel Mackf4324552016-11-23 16:52:27 +01001867static int bpf_prog_attach(const union bpf_attr *attr)
1868{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001869 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001870 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001871 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001872
1873 if (!capable(CAP_NET_ADMIN))
1874 return -EPERM;
1875
1876 if (CHECK_ATTR(BPF_PROG_ATTACH))
1877 return -EINVAL;
1878
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001879 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001880 return -EINVAL;
1881
Daniel Mackf4324552016-11-23 16:52:27 +01001882 switch (attr->attach_type) {
1883 case BPF_CGROUP_INET_INGRESS:
1884 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001885 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001886 break;
David Ahern610236582016-12-01 08:48:04 -08001887 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001888 case BPF_CGROUP_INET4_POST_BIND:
1889 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001890 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1891 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001892 case BPF_CGROUP_INET4_BIND:
1893 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001894 case BPF_CGROUP_INET4_CONNECT:
1895 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001896 case BPF_CGROUP_UDP4_SENDMSG:
1897 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02001898 case BPF_CGROUP_UDP4_RECVMSG:
1899 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001900 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1901 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001902 case BPF_CGROUP_SOCK_OPS:
1903 ptype = BPF_PROG_TYPE_SOCK_OPS;
1904 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001905 case BPF_CGROUP_DEVICE:
1906 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1907 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001908 case BPF_SK_MSG_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001909 ptype = BPF_PROG_TYPE_SK_MSG;
1910 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001911 case BPF_SK_SKB_STREAM_PARSER:
1912 case BPF_SK_SKB_STREAM_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001913 ptype = BPF_PROG_TYPE_SK_SKB;
1914 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01001915 case BPF_LIRC_MODE2:
Sean Youngfdb5c452018-06-19 00:04:24 +01001916 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1917 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001918 case BPF_FLOW_DISSECTOR:
1919 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1920 break;
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08001921 case BPF_CGROUP_SYSCTL:
1922 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1923 break;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001924 case BPF_CGROUP_GETSOCKOPT:
1925 case BPF_CGROUP_SETSOCKOPT:
1926 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
1927 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001928 default:
1929 return -EINVAL;
1930 }
1931
David Ahernb2cd1252016-12-01 08:48:03 -08001932 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1933 if (IS_ERR(prog))
1934 return PTR_ERR(prog);
1935
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001936 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1937 bpf_prog_put(prog);
1938 return -EINVAL;
1939 }
1940
Sean Youngfdb5c452018-06-19 00:04:24 +01001941 switch (ptype) {
1942 case BPF_PROG_TYPE_SK_SKB:
1943 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02001944 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01001945 break;
1946 case BPF_PROG_TYPE_LIRC_MODE2:
1947 ret = lirc_prog_attach(attr, prog);
1948 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07001949 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1950 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1951 break;
Sean Youngfdb5c452018-06-19 00:04:24 +01001952 default:
1953 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001954 }
1955
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001956 if (ret)
1957 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001958 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001959}
1960
1961#define BPF_PROG_DETACH_LAST_FIELD attach_type
1962
1963static int bpf_prog_detach(const union bpf_attr *attr)
1964{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001965 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001966
1967 if (!capable(CAP_NET_ADMIN))
1968 return -EPERM;
1969
1970 if (CHECK_ATTR(BPF_PROG_DETACH))
1971 return -EINVAL;
1972
1973 switch (attr->attach_type) {
1974 case BPF_CGROUP_INET_INGRESS:
1975 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001976 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1977 break;
David Ahern610236582016-12-01 08:48:04 -08001978 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001979 case BPF_CGROUP_INET4_POST_BIND:
1980 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001981 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1982 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001983 case BPF_CGROUP_INET4_BIND:
1984 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001985 case BPF_CGROUP_INET4_CONNECT:
1986 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001987 case BPF_CGROUP_UDP4_SENDMSG:
1988 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02001989 case BPF_CGROUP_UDP4_RECVMSG:
1990 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001991 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1992 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001993 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001994 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001995 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001996 case BPF_CGROUP_DEVICE:
1997 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1998 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001999 case BPF_SK_MSG_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002000 return sock_map_get_from_fd(attr, NULL);
John Fastabend5a67da22017-09-08 14:00:49 -07002001 case BPF_SK_SKB_STREAM_PARSER:
2002 case BPF_SK_SKB_STREAM_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002003 return sock_map_get_from_fd(attr, NULL);
Sean Youngf4364dc2018-05-27 12:24:09 +01002004 case BPF_LIRC_MODE2:
2005 return lirc_prog_detach(attr);
Petar Penkovd58e4682018-09-14 07:46:18 -07002006 case BPF_FLOW_DISSECTOR:
2007 return skb_flow_dissector_bpf_prog_detach(attr);
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002008 case BPF_CGROUP_SYSCTL:
2009 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2010 break;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002011 case BPF_CGROUP_GETSOCKOPT:
2012 case BPF_CGROUP_SETSOCKOPT:
2013 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2014 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002015 default:
2016 return -EINVAL;
2017 }
2018
Sean Youngfdb5c452018-06-19 00:04:24 +01002019 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01002020}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002021
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002022#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2023
2024static int bpf_prog_query(const union bpf_attr *attr,
2025 union bpf_attr __user *uattr)
2026{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002027 if (!capable(CAP_NET_ADMIN))
2028 return -EPERM;
2029 if (CHECK_ATTR(BPF_PROG_QUERY))
2030 return -EINVAL;
2031 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2032 return -EINVAL;
2033
2034 switch (attr->query.attach_type) {
2035 case BPF_CGROUP_INET_INGRESS:
2036 case BPF_CGROUP_INET_EGRESS:
2037 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002038 case BPF_CGROUP_INET4_BIND:
2039 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002040 case BPF_CGROUP_INET4_POST_BIND:
2041 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002042 case BPF_CGROUP_INET4_CONNECT:
2043 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002044 case BPF_CGROUP_UDP4_SENDMSG:
2045 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002046 case BPF_CGROUP_UDP4_RECVMSG:
2047 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002048 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05002049 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002050 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002051 case BPF_CGROUP_GETSOCKOPT:
2052 case BPF_CGROUP_SETSOCKOPT:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002053 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01002054 case BPF_LIRC_MODE2:
2055 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07002056 case BPF_FLOW_DISSECTOR:
2057 return skb_flow_dissector_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002058 default:
2059 return -EINVAL;
2060 }
Sean Youngfdb5c452018-06-19 00:04:24 +01002061
2062 return cgroup_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002063}
Daniel Mackf4324552016-11-23 16:52:27 +01002064
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002065#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002066
2067static int bpf_prog_test_run(const union bpf_attr *attr,
2068 union bpf_attr __user *uattr)
2069{
2070 struct bpf_prog *prog;
2071 int ret = -ENOTSUPP;
2072
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08002073 if (!capable(CAP_SYS_ADMIN))
2074 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002075 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2076 return -EINVAL;
2077
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002078 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2079 (!attr->test.ctx_size_in && attr->test.ctx_in))
2080 return -EINVAL;
2081
2082 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2083 (!attr->test.ctx_size_out && attr->test.ctx_out))
2084 return -EINVAL;
2085
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002086 prog = bpf_prog_get(attr->test.prog_fd);
2087 if (IS_ERR(prog))
2088 return PTR_ERR(prog);
2089
2090 if (prog->aux->ops->test_run)
2091 ret = prog->aux->ops->test_run(prog, attr, uattr);
2092
2093 bpf_prog_put(prog);
2094 return ret;
2095}
2096
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002097#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2098
2099static int bpf_obj_get_next_id(const union bpf_attr *attr,
2100 union bpf_attr __user *uattr,
2101 struct idr *idr,
2102 spinlock_t *lock)
2103{
2104 u32 next_id = attr->start_id;
2105 int err = 0;
2106
2107 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2108 return -EINVAL;
2109
2110 if (!capable(CAP_SYS_ADMIN))
2111 return -EPERM;
2112
2113 next_id++;
2114 spin_lock_bh(lock);
2115 if (!idr_get_next(idr, &next_id))
2116 err = -ENOENT;
2117 spin_unlock_bh(lock);
2118
2119 if (!err)
2120 err = put_user(next_id, &uattr->next_id);
2121
2122 return err;
2123}
2124
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002125#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2126
2127static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2128{
2129 struct bpf_prog *prog;
2130 u32 id = attr->prog_id;
2131 int fd;
2132
2133 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2134 return -EINVAL;
2135
2136 if (!capable(CAP_SYS_ADMIN))
2137 return -EPERM;
2138
2139 spin_lock_bh(&prog_idr_lock);
2140 prog = idr_find(&prog_idr, id);
2141 if (prog)
2142 prog = bpf_prog_inc_not_zero(prog);
2143 else
2144 prog = ERR_PTR(-ENOENT);
2145 spin_unlock_bh(&prog_idr_lock);
2146
2147 if (IS_ERR(prog))
2148 return PTR_ERR(prog);
2149
2150 fd = bpf_prog_new_fd(prog);
2151 if (fd < 0)
2152 bpf_prog_put(prog);
2153
2154 return fd;
2155}
2156
Chenbo Feng6e71b042017-10-18 13:00:22 -07002157#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002158
2159static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2160{
2161 struct bpf_map *map;
2162 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002163 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002164 int fd;
2165
Chenbo Feng6e71b042017-10-18 13:00:22 -07002166 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2167 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002168 return -EINVAL;
2169
2170 if (!capable(CAP_SYS_ADMIN))
2171 return -EPERM;
2172
Chenbo Feng6e71b042017-10-18 13:00:22 -07002173 f_flags = bpf_get_file_flag(attr->open_flags);
2174 if (f_flags < 0)
2175 return f_flags;
2176
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002177 spin_lock_bh(&map_idr_lock);
2178 map = idr_find(&map_idr, id);
2179 if (map)
2180 map = bpf_map_inc_not_zero(map, true);
2181 else
2182 map = ERR_PTR(-ENOENT);
2183 spin_unlock_bh(&map_idr_lock);
2184
2185 if (IS_ERR(map))
2186 return PTR_ERR(map);
2187
Chenbo Feng6e71b042017-10-18 13:00:22 -07002188 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002189 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002190 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002191
2192 return fd;
2193}
2194
Daniel Borkmann7105e822017-12-20 13:42:57 +01002195static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002196 unsigned long addr, u32 *off,
2197 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002198{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002199 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002200 int i;
2201
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002202 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2203 map = prog->aux->used_maps[i];
2204 if (map == (void *)addr) {
2205 *type = BPF_PSEUDO_MAP_FD;
2206 return map;
2207 }
2208 if (!map->ops->map_direct_value_meta)
2209 continue;
2210 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2211 *type = BPF_PSEUDO_MAP_VALUE;
2212 return map;
2213 }
2214 }
2215
Daniel Borkmann7105e822017-12-20 13:42:57 +01002216 return NULL;
2217}
2218
2219static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2220{
2221 const struct bpf_map *map;
2222 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002223 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002224 u64 imm;
2225 int i;
2226
2227 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2228 GFP_USER);
2229 if (!insns)
2230 return insns;
2231
2232 for (i = 0; i < prog->len; i++) {
2233 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2234 insns[i].code = BPF_JMP | BPF_CALL;
2235 insns[i].imm = BPF_FUNC_tail_call;
2236 /* fall-through */
2237 }
2238 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2239 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2240 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2241 insns[i].code = BPF_JMP | BPF_CALL;
2242 if (!bpf_dump_raw_ok())
2243 insns[i].imm = 0;
2244 continue;
2245 }
2246
2247 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2248 continue;
2249
2250 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002251 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002252 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002253 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002254 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002255 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002256 continue;
2257 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002258 }
2259
2260 return insns;
2261}
2262
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002263static int set_info_rec_size(struct bpf_prog_info *info)
2264{
2265 /*
2266 * Ensure info.*_rec_size is the same as kernel expected size
2267 *
2268 * or
2269 *
2270 * Only allow zero *_rec_size if both _rec_size and _cnt are
2271 * zero. In this case, the kernel will set the expected
2272 * _rec_size back to the info.
2273 */
2274
Yonghong Song11d8b822018-12-10 14:14:08 -08002275 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002276 info->func_info_rec_size != sizeof(struct bpf_func_info))
2277 return -EINVAL;
2278
Yonghong Song11d8b822018-12-10 14:14:08 -08002279 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002280 info->line_info_rec_size != sizeof(struct bpf_line_info))
2281 return -EINVAL;
2282
Yonghong Song11d8b822018-12-10 14:14:08 -08002283 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002284 info->jited_line_info_rec_size != sizeof(__u64))
2285 return -EINVAL;
2286
2287 info->func_info_rec_size = sizeof(struct bpf_func_info);
2288 info->line_info_rec_size = sizeof(struct bpf_line_info);
2289 info->jited_line_info_rec_size = sizeof(__u64);
2290
2291 return 0;
2292}
2293
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002294static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2295 const union bpf_attr *attr,
2296 union bpf_attr __user *uattr)
2297{
2298 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2299 struct bpf_prog_info info = {};
2300 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002301 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002302 char __user *uinsns;
2303 u32 ulen;
2304 int err;
2305
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002306 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002307 if (err)
2308 return err;
2309 info_len = min_t(u32, sizeof(info), info_len);
2310
2311 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02002312 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002313
2314 info.type = prog->type;
2315 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002316 info.load_time = prog->aux->load_time;
2317 info.created_by_uid = from_kuid_munged(current_user_ns(),
2318 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02002319 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002320
2321 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002322 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2323
2324 ulen = info.nr_map_ids;
2325 info.nr_map_ids = prog->aux->used_map_cnt;
2326 ulen = min_t(u32, info.nr_map_ids, ulen);
2327 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07002328 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002329 u32 i;
2330
2331 for (i = 0; i < ulen; i++)
2332 if (put_user(prog->aux->used_maps[i]->id,
2333 &user_map_ids[i]))
2334 return -EFAULT;
2335 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002336
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002337 err = set_info_rec_size(&info);
2338 if (err)
2339 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08002340
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002341 bpf_prog_get_stats(prog, &stats);
2342 info.run_time_ns = stats.nsecs;
2343 info.run_cnt = stats.cnt;
2344
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002345 if (!capable(CAP_SYS_ADMIN)) {
2346 info.jited_prog_len = 0;
2347 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302348 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01002349 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08002350 info.nr_func_info = 0;
2351 info.nr_line_info = 0;
2352 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002353 goto done;
2354 }
2355
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002356 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02002357 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002358 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01002359 struct bpf_insn *insns_sanitized;
2360 bool fault;
2361
2362 if (prog->blinded && !bpf_dump_raw_ok()) {
2363 info.xlated_prog_insns = 0;
2364 goto done;
2365 }
2366 insns_sanitized = bpf_insn_prepare_dump(prog);
2367 if (!insns_sanitized)
2368 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002369 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2370 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002371 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2372 kfree(insns_sanitized);
2373 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002374 return -EFAULT;
2375 }
2376
Jakub Kicinski675fc272017-12-27 18:39:09 -08002377 if (bpf_prog_is_dev_bound(prog->aux)) {
2378 err = bpf_prog_offload_info_fill(&info, prog);
2379 if (err)
2380 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08002381 goto done;
2382 }
2383
2384 /* NOTE: the following code is supposed to be skipped for offload.
2385 * bpf_prog_offload_info_fill() is the place to fill similar fields
2386 * for offload.
2387 */
2388 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05302389 if (prog->aux->func_cnt) {
2390 u32 i;
2391
2392 info.jited_prog_len = 0;
2393 for (i = 0; i < prog->aux->func_cnt; i++)
2394 info.jited_prog_len += prog->aux->func[i]->jited_len;
2395 } else {
2396 info.jited_prog_len = prog->jited_len;
2397 }
2398
Jiong Wangfcfb1262018-01-16 16:05:19 -08002399 if (info.jited_prog_len && ulen) {
2400 if (bpf_dump_raw_ok()) {
2401 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2402 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05302403
2404 /* for multi-function programs, copy the JITed
2405 * instructions for all the functions
2406 */
2407 if (prog->aux->func_cnt) {
2408 u32 len, free, i;
2409 u8 *img;
2410
2411 free = ulen;
2412 for (i = 0; i < prog->aux->func_cnt; i++) {
2413 len = prog->aux->func[i]->jited_len;
2414 len = min_t(u32, len, free);
2415 img = (u8 *) prog->aux->func[i]->bpf_func;
2416 if (copy_to_user(uinsns, img, len))
2417 return -EFAULT;
2418 uinsns += len;
2419 free -= len;
2420 if (!free)
2421 break;
2422 }
2423 } else {
2424 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2425 return -EFAULT;
2426 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002427 } else {
2428 info.jited_prog_insns = 0;
2429 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002430 }
2431
Sandipan Dasdbecd732018-05-24 12:26:48 +05302432 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07002433 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002434 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05302435 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07002436 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302437 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302438 u32 i;
2439
2440 /* copy the address of the kernel symbol
2441 * corresponding to each function
2442 */
2443 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2444 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07002445 if (prog->aux->func_cnt) {
2446 for (i = 0; i < ulen; i++) {
2447 ksym_addr = (unsigned long)
2448 prog->aux->func[i]->bpf_func;
2449 if (put_user((u64) ksym_addr,
2450 &user_ksyms[i]))
2451 return -EFAULT;
2452 }
2453 } else {
2454 ksym_addr = (unsigned long) prog->bpf_func;
2455 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05302456 return -EFAULT;
2457 }
2458 } else {
2459 info.jited_ksyms = 0;
2460 }
2461 }
2462
Sandipan Das815581c2018-05-24 12:26:52 +05302463 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07002464 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002465 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05302466 if (bpf_dump_raw_ok()) {
2467 u32 __user *user_lens;
2468 u32 func_len, i;
2469
2470 /* copy the JITed image lengths for each function */
2471 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2472 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07002473 if (prog->aux->func_cnt) {
2474 for (i = 0; i < ulen; i++) {
2475 func_len =
2476 prog->aux->func[i]->jited_len;
2477 if (put_user(func_len, &user_lens[i]))
2478 return -EFAULT;
2479 }
2480 } else {
2481 func_len = prog->jited_len;
2482 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05302483 return -EFAULT;
2484 }
2485 } else {
2486 info.jited_func_lens = 0;
2487 }
2488 }
2489
Martin KaFai Lau73372242018-12-05 17:35:43 -08002490 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08002491 info.btf_id = btf_id(prog->aux->btf);
2492
Yonghong Song11d8b822018-12-10 14:14:08 -08002493 ulen = info.nr_func_info;
2494 info.nr_func_info = prog->aux->func_info_cnt;
2495 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002496 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08002497
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002498 user_finfo = u64_to_user_ptr(info.func_info);
2499 ulen = min_t(u32, info.nr_func_info, ulen);
2500 if (copy_to_user(user_finfo, prog->aux->func_info,
2501 info.func_info_rec_size * ulen))
2502 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08002503 }
2504
Yonghong Song11d8b822018-12-10 14:14:08 -08002505 ulen = info.nr_line_info;
2506 info.nr_line_info = prog->aux->nr_linfo;
2507 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002508 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002509
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002510 user_linfo = u64_to_user_ptr(info.line_info);
2511 ulen = min_t(u32, info.nr_line_info, ulen);
2512 if (copy_to_user(user_linfo, prog->aux->linfo,
2513 info.line_info_rec_size * ulen))
2514 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002515 }
2516
Yonghong Song11d8b822018-12-10 14:14:08 -08002517 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002518 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08002519 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002520 else
Yonghong Song11d8b822018-12-10 14:14:08 -08002521 info.nr_jited_line_info = 0;
2522 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002523 if (bpf_dump_raw_ok()) {
2524 __u64 __user *user_linfo;
2525 u32 i;
2526
2527 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08002528 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002529 for (i = 0; i < ulen; i++) {
2530 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2531 &user_linfo[i]))
2532 return -EFAULT;
2533 }
2534 } else {
2535 info.jited_line_info = 0;
2536 }
2537 }
2538
Song Liuc872bdb2018-12-12 09:37:46 -08002539 ulen = info.nr_prog_tags;
2540 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2541 if (ulen) {
2542 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2543 u32 i;
2544
2545 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2546 ulen = min_t(u32, info.nr_prog_tags, ulen);
2547 if (prog->aux->func_cnt) {
2548 for (i = 0; i < ulen; i++) {
2549 if (copy_to_user(user_prog_tags[i],
2550 prog->aux->func[i]->tag,
2551 BPF_TAG_SIZE))
2552 return -EFAULT;
2553 }
2554 } else {
2555 if (copy_to_user(user_prog_tags[0],
2556 prog->tag, BPF_TAG_SIZE))
2557 return -EFAULT;
2558 }
2559 }
2560
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002561done:
2562 if (copy_to_user(uinfo, &info, info_len) ||
2563 put_user(info_len, &uattr->info.info_len))
2564 return -EFAULT;
2565
2566 return 0;
2567}
2568
2569static int bpf_map_get_info_by_fd(struct bpf_map *map,
2570 const union bpf_attr *attr,
2571 union bpf_attr __user *uattr)
2572{
2573 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2574 struct bpf_map_info info = {};
2575 u32 info_len = attr->info.info_len;
2576 int err;
2577
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002578 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002579 if (err)
2580 return err;
2581 info_len = min_t(u32, sizeof(info), info_len);
2582
2583 info.type = map->map_type;
2584 info.id = map->id;
2585 info.key_size = map->key_size;
2586 info.value_size = map->value_size;
2587 info.max_entries = map->max_entries;
2588 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002589 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002590
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002591 if (map->btf) {
2592 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002593 info.btf_key_type_id = map->btf_key_type_id;
2594 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002595 }
2596
Jakub Kicinski52775b32018-01-17 19:13:28 -08002597 if (bpf_map_is_dev_bound(map)) {
2598 err = bpf_map_offload_info_fill(&info, map);
2599 if (err)
2600 return err;
2601 }
2602
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002603 if (copy_to_user(uinfo, &info, info_len) ||
2604 put_user(info_len, &uattr->info.info_len))
2605 return -EFAULT;
2606
2607 return 0;
2608}
2609
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002610static int bpf_btf_get_info_by_fd(struct btf *btf,
2611 const union bpf_attr *attr,
2612 union bpf_attr __user *uattr)
2613{
2614 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2615 u32 info_len = attr->info.info_len;
2616 int err;
2617
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002618 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002619 if (err)
2620 return err;
2621
2622 return btf_get_info_by_fd(btf, attr, uattr);
2623}
2624
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002625#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2626
2627static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2628 union bpf_attr __user *uattr)
2629{
2630 int ufd = attr->info.bpf_fd;
2631 struct fd f;
2632 int err;
2633
2634 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2635 return -EINVAL;
2636
2637 f = fdget(ufd);
2638 if (!f.file)
2639 return -EBADFD;
2640
2641 if (f.file->f_op == &bpf_prog_fops)
2642 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2643 uattr);
2644 else if (f.file->f_op == &bpf_map_fops)
2645 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2646 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002647 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002648 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002649 else
2650 err = -EINVAL;
2651
2652 fdput(f);
2653 return err;
2654}
2655
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002656#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2657
2658static int bpf_btf_load(const union bpf_attr *attr)
2659{
2660 if (CHECK_ATTR(BPF_BTF_LOAD))
2661 return -EINVAL;
2662
2663 if (!capable(CAP_SYS_ADMIN))
2664 return -EPERM;
2665
2666 return btf_new_fd(attr);
2667}
2668
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002669#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2670
2671static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2672{
2673 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2674 return -EINVAL;
2675
2676 if (!capable(CAP_SYS_ADMIN))
2677 return -EPERM;
2678
2679 return btf_get_fd_by_id(attr->btf_id);
2680}
2681
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002682static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2683 union bpf_attr __user *uattr,
2684 u32 prog_id, u32 fd_type,
2685 const char *buf, u64 probe_offset,
2686 u64 probe_addr)
2687{
2688 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2689 u32 len = buf ? strlen(buf) : 0, input_len;
2690 int err = 0;
2691
2692 if (put_user(len, &uattr->task_fd_query.buf_len))
2693 return -EFAULT;
2694 input_len = attr->task_fd_query.buf_len;
2695 if (input_len && ubuf) {
2696 if (!len) {
2697 /* nothing to copy, just make ubuf NULL terminated */
2698 char zero = '\0';
2699
2700 if (put_user(zero, ubuf))
2701 return -EFAULT;
2702 } else if (input_len >= len + 1) {
2703 /* ubuf can hold the string with NULL terminator */
2704 if (copy_to_user(ubuf, buf, len + 1))
2705 return -EFAULT;
2706 } else {
2707 /* ubuf cannot hold the string with NULL terminator,
2708 * do a partial copy with NULL terminator.
2709 */
2710 char zero = '\0';
2711
2712 err = -ENOSPC;
2713 if (copy_to_user(ubuf, buf, input_len - 1))
2714 return -EFAULT;
2715 if (put_user(zero, ubuf + input_len - 1))
2716 return -EFAULT;
2717 }
2718 }
2719
2720 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2721 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2722 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2723 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2724 return -EFAULT;
2725
2726 return err;
2727}
2728
2729#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2730
2731static int bpf_task_fd_query(const union bpf_attr *attr,
2732 union bpf_attr __user *uattr)
2733{
2734 pid_t pid = attr->task_fd_query.pid;
2735 u32 fd = attr->task_fd_query.fd;
2736 const struct perf_event *event;
2737 struct files_struct *files;
2738 struct task_struct *task;
2739 struct file *file;
2740 int err;
2741
2742 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2743 return -EINVAL;
2744
2745 if (!capable(CAP_SYS_ADMIN))
2746 return -EPERM;
2747
2748 if (attr->task_fd_query.flags != 0)
2749 return -EINVAL;
2750
2751 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2752 if (!task)
2753 return -ENOENT;
2754
2755 files = get_files_struct(task);
2756 put_task_struct(task);
2757 if (!files)
2758 return -ENOENT;
2759
2760 err = 0;
2761 spin_lock(&files->file_lock);
2762 file = fcheck_files(files, fd);
2763 if (!file)
2764 err = -EBADF;
2765 else
2766 get_file(file);
2767 spin_unlock(&files->file_lock);
2768 put_files_struct(files);
2769
2770 if (err)
2771 goto out;
2772
2773 if (file->f_op == &bpf_raw_tp_fops) {
2774 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2775 struct bpf_raw_event_map *btp = raw_tp->btp;
2776
2777 err = bpf_task_fd_query_copy(attr, uattr,
2778 raw_tp->prog->aux->id,
2779 BPF_FD_TYPE_RAW_TRACEPOINT,
2780 btp->tp->name, 0, 0);
2781 goto put_file;
2782 }
2783
2784 event = perf_get_event(file);
2785 if (!IS_ERR(event)) {
2786 u64 probe_offset, probe_addr;
2787 u32 prog_id, fd_type;
2788 const char *buf;
2789
2790 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2791 &buf, &probe_offset,
2792 &probe_addr);
2793 if (!err)
2794 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2795 fd_type, buf,
2796 probe_offset,
2797 probe_addr);
2798 goto put_file;
2799 }
2800
2801 err = -ENOTSUPP;
2802put_file:
2803 fput(file);
2804out:
2805 return err;
2806}
2807
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002808SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2809{
2810 union bpf_attr attr = {};
2811 int err;
2812
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002813 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002814 return -EPERM;
2815
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002816 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002817 if (err)
2818 return err;
2819 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002820
2821 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2822 if (copy_from_user(&attr, uattr, size) != 0)
2823 return -EFAULT;
2824
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002825 err = security_bpf(cmd, &attr, size);
2826 if (err < 0)
2827 return err;
2828
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002829 switch (cmd) {
2830 case BPF_MAP_CREATE:
2831 err = map_create(&attr);
2832 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002833 case BPF_MAP_LOOKUP_ELEM:
2834 err = map_lookup_elem(&attr);
2835 break;
2836 case BPF_MAP_UPDATE_ELEM:
2837 err = map_update_elem(&attr);
2838 break;
2839 case BPF_MAP_DELETE_ELEM:
2840 err = map_delete_elem(&attr);
2841 break;
2842 case BPF_MAP_GET_NEXT_KEY:
2843 err = map_get_next_key(&attr);
2844 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02002845 case BPF_MAP_FREEZE:
2846 err = map_freeze(&attr);
2847 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002848 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08002849 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002850 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002851 case BPF_OBJ_PIN:
2852 err = bpf_obj_pin(&attr);
2853 break;
2854 case BPF_OBJ_GET:
2855 err = bpf_obj_get(&attr);
2856 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002857 case BPF_PROG_ATTACH:
2858 err = bpf_prog_attach(&attr);
2859 break;
2860 case BPF_PROG_DETACH:
2861 err = bpf_prog_detach(&attr);
2862 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002863 case BPF_PROG_QUERY:
2864 err = bpf_prog_query(&attr, uattr);
2865 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002866 case BPF_PROG_TEST_RUN:
2867 err = bpf_prog_test_run(&attr, uattr);
2868 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002869 case BPF_PROG_GET_NEXT_ID:
2870 err = bpf_obj_get_next_id(&attr, uattr,
2871 &prog_idr, &prog_idr_lock);
2872 break;
2873 case BPF_MAP_GET_NEXT_ID:
2874 err = bpf_obj_get_next_id(&attr, uattr,
2875 &map_idr, &map_idr_lock);
2876 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002877 case BPF_PROG_GET_FD_BY_ID:
2878 err = bpf_prog_get_fd_by_id(&attr);
2879 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002880 case BPF_MAP_GET_FD_BY_ID:
2881 err = bpf_map_get_fd_by_id(&attr);
2882 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002883 case BPF_OBJ_GET_INFO_BY_FD:
2884 err = bpf_obj_get_info_by_fd(&attr, uattr);
2885 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002886 case BPF_RAW_TRACEPOINT_OPEN:
2887 err = bpf_raw_tracepoint_open(&attr);
2888 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002889 case BPF_BTF_LOAD:
2890 err = bpf_btf_load(&attr);
2891 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002892 case BPF_BTF_GET_FD_BY_ID:
2893 err = bpf_btf_get_fd_by_id(&attr);
2894 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002895 case BPF_TASK_FD_QUERY:
2896 err = bpf_task_fd_query(&attr, uattr);
2897 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02002898 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2899 err = map_lookup_and_delete_elem(&attr);
2900 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002901 default:
2902 err = -EINVAL;
2903 break;
2904 }
2905
2906 return err;
2907}