blob: 57f4d076141bbcfc115a9cfb1ccc51eb379d7976 [file] [log] [blame]
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +010013#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +010014#include <linux/bpf_lirc.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070015#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070016#include <linux/syscalls.h>
17#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010018#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010019#include <linux/vmalloc.h>
20#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070021#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070022#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070023#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070024#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070025#include <linux/license.h>
26#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070027#include <linux/version.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010028#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070029#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070030#include <linux/cred.h>
31#include <linux/timekeeping.h>
32#include <linux/ctype.h>
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070033#include <linux/btf.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010034#include <linux/nospec.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070035
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070036#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
38 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
39 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
40#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
41#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
42
Chenbo Feng6e71b042017-10-18 13:00:22 -070043#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
44
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080045DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070046static DEFINE_IDR(prog_idr);
47static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070048static DEFINE_IDR(map_idr);
49static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080050
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070051int sysctl_unprivileged_bpf_disabled __read_mostly;
52
Johannes Berg40077e02017-04-11 15:34:58 +020053static const struct bpf_map_ops * const bpf_map_types[] = {
54#define BPF_PROG_TYPE(_id, _ops)
55#define BPF_MAP_TYPE(_id, _ops) \
56 [_id] = &_ops,
57#include <linux/bpf_types.h>
58#undef BPF_PROG_TYPE
59#undef BPF_MAP_TYPE
60};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070061
Mickaël Salaün752ba562017-08-07 20:45:20 +020062/*
63 * If we're handed a bigger struct than we know of, ensure all the unknown bits
64 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
65 * we don't know about yet.
66 *
67 * There is a ToCToU between this function call and the following
68 * copy_from_user() call. However, this is not a concern since this function is
69 * meant to be a future-proofing of bits.
70 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070071int bpf_check_uarg_tail_zero(void __user *uaddr,
72 size_t expected_size,
73 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020074{
75 unsigned char __user *addr;
76 unsigned char __user *end;
77 unsigned char val;
78 int err;
79
Mickaël Salaün752ba562017-08-07 20:45:20 +020080 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
81 return -E2BIG;
82
83 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
84 return -EFAULT;
85
Mickaël Salaün58291a72017-08-07 20:45:19 +020086 if (actual_size <= expected_size)
87 return 0;
88
89 addr = uaddr + expected_size;
90 end = uaddr + actual_size;
91
92 for (; addr < end; addr++) {
93 err = get_user(val, addr);
94 if (err)
95 return err;
96 if (val)
97 return -E2BIG;
98 }
99
100 return 0;
101}
102
Jakub Kicinskia3884572018-01-11 20:29:09 -0800103const struct bpf_map_ops bpf_map_offload_ops = {
104 .map_alloc = bpf_map_offload_map_alloc,
105 .map_free = bpf_map_offload_map_free,
106};
107
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700108static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
109{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800110 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100111 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700112 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800113 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700114
Mark Rutland9ef09e32018-05-03 17:04:59 +0100115 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800116 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100117 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
118 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800119 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200120 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700121
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800122 if (ops->map_alloc_check) {
123 err = ops->map_alloc_check(attr);
124 if (err)
125 return ERR_PTR(err);
126 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800127 if (attr->map_ifindex)
128 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800129 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200130 if (IS_ERR(map))
131 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800132 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100133 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200134 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700135}
136
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700137void *bpf_map_area_alloc(size_t size, int numa_node)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100138{
139 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
140 * trigger under memory pressure as we really just want to
141 * fail instead.
142 */
143 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
144 void *area;
145
146 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700147 area = kmalloc_node(size, GFP_USER | flags, numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100148 if (area != NULL)
149 return area;
150 }
151
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700152 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
153 __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
Jakub Kicinskibd475642018-01-11 20:29:06 -0800161void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
162{
163 map->map_type = attr->map_type;
164 map->key_size = attr->key_size;
165 map->value_size = attr->value_size;
166 map->max_entries = attr->max_entries;
167 map->map_flags = attr->map_flags;
168 map->numa_node = bpf_map_attr_numa_node(attr);
169}
170
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800171int bpf_map_precharge_memlock(u32 pages)
172{
173 struct user_struct *user = get_current_user();
174 unsigned long memlock_limit, cur;
175
176 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
177 cur = atomic_long_read(&user->locked_vm);
178 free_uid(user);
179 if (cur + pages > memlock_limit)
180 return -EPERM;
181 return 0;
182}
183
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700184static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700185{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700186 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700187
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700188 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
189 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700190 return -EPERM;
191 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700192 return 0;
193}
194
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700195static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
196{
197 atomic_long_sub(pages, &user->locked_vm);
198}
199
200static int bpf_map_init_memlock(struct bpf_map *map)
201{
202 struct user_struct *user = get_current_user();
203 int ret;
204
205 ret = bpf_charge_memlock(user, map->pages);
206 if (ret) {
207 free_uid(user);
208 return ret;
209 }
210 map->user = user;
211 return ret;
212}
213
214static void bpf_map_release_memlock(struct bpf_map *map)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700215{
216 struct user_struct *user = map->user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700217 bpf_uncharge_memlock(user, map->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700218 free_uid(user);
219}
220
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700221int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
222{
223 int ret;
224
225 ret = bpf_charge_memlock(map->user, pages);
226 if (ret)
227 return ret;
228 map->pages += pages;
229 return ret;
230}
231
232void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
233{
234 bpf_uncharge_memlock(map->user, pages);
235 map->pages -= pages;
236}
237
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700238static int bpf_map_alloc_id(struct bpf_map *map)
239{
240 int id;
241
Shaohua Lib76354c2018-03-27 11:53:21 -0700242 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700243 spin_lock_bh(&map_idr_lock);
244 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
245 if (id > 0)
246 map->id = id;
247 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700248 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700249
250 if (WARN_ON_ONCE(!id))
251 return -ENOSPC;
252
253 return id > 0 ? 0 : id;
254}
255
Jakub Kicinskia3884572018-01-11 20:29:09 -0800256void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700257{
Eric Dumazet930651a2017-09-19 09:15:59 -0700258 unsigned long flags;
259
Jakub Kicinskia3884572018-01-11 20:29:09 -0800260 /* Offloaded maps are removed from the IDR store when their device
261 * disappears - even if someone holds an fd to them they are unusable,
262 * the memory is gone, all ops will fail; they are simply waiting for
263 * refcnt to drop to be freed.
264 */
265 if (!map->id)
266 return;
267
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700268 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700269 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700270 else
271 __acquire(&map_idr_lock);
272
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700273 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800274 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700275
276 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700277 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700278 else
279 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700280}
281
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700282/* called from workqueue */
283static void bpf_map_free_deferred(struct work_struct *work)
284{
285 struct bpf_map *map = container_of(work, struct bpf_map, work);
286
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700287 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700288 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700289 /* implementation dependent freeing */
290 map->ops->map_free(map);
291}
292
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100293static void bpf_map_put_uref(struct bpf_map *map)
294{
295 if (atomic_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700296 if (map->ops->map_release_uref)
297 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100298 }
299}
300
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700301/* decrement map refcnt and schedule it for freeing via workqueue
302 * (unrelying map implementation ops->map_free() might sleep)
303 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700304static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700305{
306 if (atomic_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700307 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700308 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700309 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700310 INIT_WORK(&map->work, bpf_map_free_deferred);
311 schedule_work(&map->work);
312 }
313}
314
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700315void bpf_map_put(struct bpf_map *map)
316{
317 __bpf_map_put(map, true);
318}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700319EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700320
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100321void bpf_map_put_with_uref(struct bpf_map *map)
322{
323 bpf_map_put_uref(map);
324 bpf_map_put(map);
325}
326
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700327static int bpf_map_release(struct inode *inode, struct file *filp)
328{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200329 struct bpf_map *map = filp->private_data;
330
331 if (map->ops->map_release)
332 map->ops->map_release(map, filp);
333
334 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700335 return 0;
336}
337
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100338#ifdef CONFIG_PROC_FS
339static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
340{
341 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100342 const struct bpf_array *array;
343 u32 owner_prog_type = 0;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200344 u32 owner_jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100345
346 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
347 array = container_of(map, struct bpf_array, map);
348 owner_prog_type = array->owner_prog_type;
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200349 owner_jited = array->owner_jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100350 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100351
352 seq_printf(m,
353 "map_type:\t%u\n"
354 "key_size:\t%u\n"
355 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100356 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100357 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200358 "memlock:\t%llu\n"
359 "map_id:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100360 map->map_type,
361 map->key_size,
362 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100363 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100364 map->map_flags,
Daniel Borkmann4316b402018-06-02 23:06:34 +0200365 map->pages * 1ULL << PAGE_SHIFT,
366 map->id);
Daniel Borkmann21116b72016-11-26 01:28:07 +0100367
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200368 if (owner_prog_type) {
Daniel Borkmann21116b72016-11-26 01:28:07 +0100369 seq_printf(m, "owner_prog_type:\t%u\n",
370 owner_prog_type);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200371 seq_printf(m, "owner_jited:\t%u\n",
372 owner_jited);
373 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100374}
375#endif
376
Chenbo Feng6e71b042017-10-18 13:00:22 -0700377static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
378 loff_t *ppos)
379{
380 /* We need this handler such that alloc_file() enables
381 * f_mode with FMODE_CAN_READ.
382 */
383 return -EINVAL;
384}
385
386static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
387 size_t siz, loff_t *ppos)
388{
389 /* We need this handler such that alloc_file() enables
390 * f_mode with FMODE_CAN_WRITE.
391 */
392 return -EINVAL;
393}
394
Chenbo Fengf66e4482017-10-18 13:00:26 -0700395const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100396#ifdef CONFIG_PROC_FS
397 .show_fdinfo = bpf_map_show_fdinfo,
398#endif
399 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700400 .read = bpf_dummy_read,
401 .write = bpf_dummy_write,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700402};
403
Chenbo Feng6e71b042017-10-18 13:00:22 -0700404int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100405{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700406 int ret;
407
408 ret = security_bpf_map(map, OPEN_FMODE(flags));
409 if (ret < 0)
410 return ret;
411
Daniel Borkmannaa797812015-10-29 14:58:06 +0100412 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700413 flags | O_CLOEXEC);
414}
415
416int bpf_get_file_flag(int flags)
417{
418 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
419 return -EINVAL;
420 if (flags & BPF_F_RDONLY)
421 return O_RDONLY;
422 if (flags & BPF_F_WRONLY)
423 return O_WRONLY;
424 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100425}
426
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700427/* helper macro to check that unused fields 'union bpf_attr' are zero */
428#define CHECK_ATTR(CMD) \
429 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
430 sizeof(attr->CMD##_LAST_FIELD), 0, \
431 sizeof(*attr) - \
432 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
433 sizeof(attr->CMD##_LAST_FIELD)) != NULL
434
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700435/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
436 * Return 0 on success and < 0 on error.
437 */
438static int bpf_obj_name_cpy(char *dst, const char *src)
439{
440 const char *end = src + BPF_OBJ_NAME_LEN;
441
Martin KaFai Lau473d9732017-10-05 21:52:11 -0700442 memset(dst, 0, BPF_OBJ_NAME_LEN);
443
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700444 /* Copy all isalnum() and '_' char */
445 while (src < end && *src) {
446 if (!isalnum(*src) && *src != '_')
447 return -EINVAL;
448 *dst++ = *src++;
449 }
450
451 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
452 if (src == end)
453 return -EINVAL;
454
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700455 return 0;
456}
457
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700458#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700459/* called via syscall */
460static int map_create(union bpf_attr *attr)
461{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700462 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700463 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700464 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700465 int err;
466
467 err = CHECK_ATTR(BPF_MAP_CREATE);
468 if (err)
469 return -EINVAL;
470
Chenbo Feng6e71b042017-10-18 13:00:22 -0700471 f_flags = bpf_get_file_flag(attr->map_flags);
472 if (f_flags < 0)
473 return f_flags;
474
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700475 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700476 ((unsigned int)numa_node >= nr_node_ids ||
477 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700478 return -EINVAL;
479
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700480 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
481 map = find_and_alloc_map(attr);
482 if (IS_ERR(map))
483 return PTR_ERR(map);
484
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700485 err = bpf_obj_name_cpy(map->name, attr->map_name);
486 if (err)
487 goto free_map_nouncharge;
488
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700489 atomic_set(&map->refcnt, 1);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100490 atomic_set(&map->usercnt, 1);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700491
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700492 if (bpf_map_support_seq_show(map) &&
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700493 (attr->btf_key_type_id || attr->btf_value_type_id)) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700494 struct btf *btf;
495
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700496 if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700497 err = -EINVAL;
498 goto free_map_nouncharge;
499 }
500
501 btf = btf_get_by_fd(attr->btf_fd);
502 if (IS_ERR(btf)) {
503 err = PTR_ERR(btf);
504 goto free_map_nouncharge;
505 }
506
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700507 err = map->ops->map_check_btf(map, btf, attr->btf_key_type_id,
508 attr->btf_value_type_id);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700509 if (err) {
510 btf_put(btf);
511 goto free_map_nouncharge;
512 }
513
514 map->btf = btf;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700515 map->btf_key_type_id = attr->btf_key_type_id;
516 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700517 }
518
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700519 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700520 if (err)
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100521 goto free_map_nouncharge;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700522
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700523 err = bpf_map_init_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700524 if (err)
525 goto free_map_sec;
526
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700527 err = bpf_map_alloc_id(map);
528 if (err)
529 goto free_map;
530
Chenbo Feng6e71b042017-10-18 13:00:22 -0700531 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700532 if (err < 0) {
533 /* failed to allocate fd.
534 * bpf_map_put() is needed because the above
535 * bpf_map_alloc_id() has published the map
536 * to the userspace and the userspace may
537 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
538 */
539 bpf_map_put(map);
540 return err;
541 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700542
543 return err;
544
545free_map:
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700546 bpf_map_release_memlock(map);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700547free_map_sec:
548 security_bpf_map_free(map);
Daniel Borkmann20b2b242016-11-04 00:56:31 +0100549free_map_nouncharge:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700550 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700551 map->ops->map_free(map);
552 return err;
553}
554
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700555/* if error is returned, fd is released.
556 * On success caller should complete fd access with matching fdput()
557 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100558struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700559{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700560 if (!f.file)
561 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700562 if (f.file->f_op != &bpf_map_fops) {
563 fdput(f);
564 return ERR_PTR(-EINVAL);
565 }
566
Daniel Borkmannc2101292015-10-29 14:58:07 +0100567 return f.file->private_data;
568}
569
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700570/* prog's and map's refcnt limit */
571#define BPF_MAX_REFCNT 32768
572
573struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100574{
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700575 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
576 atomic_dec(&map->refcnt);
577 return ERR_PTR(-EBUSY);
578 }
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100579 if (uref)
580 atomic_inc(&map->usercnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700581 return map;
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100582}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700583EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100584
585struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100586{
587 struct fd f = fdget(ufd);
588 struct bpf_map *map;
589
590 map = __bpf_map_get(f);
591 if (IS_ERR(map))
592 return map;
593
Alexei Starovoitov92117d82016-04-27 18:56:20 -0700594 map = bpf_map_inc(map, true);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100595 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700596
597 return map;
598}
599
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700600/* map_idr_lock should have been held */
601static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
602 bool uref)
603{
604 int refold;
605
606 refold = __atomic_add_unless(&map->refcnt, 1, 0);
607
608 if (refold >= BPF_MAX_REFCNT) {
609 __bpf_map_put(map, false);
610 return ERR_PTR(-EBUSY);
611 }
612
613 if (!refold)
614 return ERR_PTR(-ENOENT);
615
616 if (uref)
617 atomic_inc(&map->usercnt);
618
619 return map;
620}
621
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800622int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
623{
624 return -ENOTSUPP;
625}
626
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700627/* last field in 'union bpf_attr' used by this command */
628#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
629
630static int map_lookup_elem(union bpf_attr *attr)
631{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100632 void __user *ukey = u64_to_user_ptr(attr->key);
633 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700634 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700635 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800636 void *key, *value, *ptr;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800637 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200638 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700639 int err;
640
641 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
642 return -EINVAL;
643
Daniel Borkmann592867b2015-09-08 18:00:09 +0200644 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100645 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700646 if (IS_ERR(map))
647 return PTR_ERR(map);
648
Chenbo Feng6e71b042017-10-18 13:00:22 -0700649 if (!(f.file->f_mode & FMODE_CAN_READ)) {
650 err = -EPERM;
651 goto err_put;
652 }
653
Al Viroe4448ed2017-05-13 18:43:00 -0400654 key = memdup_user(ukey, map->key_size);
655 if (IS_ERR(key)) {
656 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700657 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400658 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700659
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800660 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800661 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800662 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
663 value_size = round_up(map->value_size, 8) * num_possible_cpus();
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700664 else if (IS_FD_MAP(map))
665 value_size = sizeof(u32);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800666 else
667 value_size = map->value_size;
668
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800669 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800670 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700671 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800672 goto free_key;
673
Jakub Kicinskia3884572018-01-11 20:29:09 -0800674 if (bpf_map_is_dev_bound(map)) {
675 err = bpf_map_offload_lookup_elem(map, key, value);
676 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
677 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800678 err = bpf_percpu_hash_copy(map, key, value);
679 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
680 err = bpf_percpu_array_copy(map, key, value);
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800681 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
682 err = bpf_stackmap_copy(map, key, value);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700683 } else if (IS_FD_ARRAY(map)) {
684 err = bpf_fd_array_map_lookup_elem(map, key, value);
685 } else if (IS_FD_HASH(map)) {
686 err = bpf_fd_htab_map_lookup_elem(map, key, value);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700687 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
688 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800689 } else {
690 rcu_read_lock();
691 ptr = map->ops->map_lookup_elem(map, key);
692 if (ptr)
693 memcpy(value, ptr, value_size);
694 rcu_read_unlock();
695 err = ptr ? 0 : -ENOENT;
696 }
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800697
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800698 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800699 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700700
701 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800702 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800703 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700704
705 err = 0;
706
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800707free_value:
708 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700709free_key:
710 kfree(key);
711err_put:
712 fdput(f);
713 return err;
714}
715
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800716#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700717
718static int map_update_elem(union bpf_attr *attr)
719{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100720 void __user *ukey = u64_to_user_ptr(attr->key);
721 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700722 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700723 struct bpf_map *map;
724 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800725 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200726 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700727 int err;
728
729 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
730 return -EINVAL;
731
Daniel Borkmann592867b2015-09-08 18:00:09 +0200732 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100733 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700734 if (IS_ERR(map))
735 return PTR_ERR(map);
736
Chenbo Feng6e71b042017-10-18 13:00:22 -0700737 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
738 err = -EPERM;
739 goto err_put;
740 }
741
Al Viroe4448ed2017-05-13 18:43:00 -0400742 key = memdup_user(ukey, map->key_size);
743 if (IS_ERR(key)) {
744 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700745 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400746 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700747
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800748 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800749 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800750 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
751 value_size = round_up(map->value_size, 8) * num_possible_cpus();
752 else
753 value_size = map->value_size;
754
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700755 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800756 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700757 if (!value)
758 goto free_key;
759
760 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800761 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700762 goto free_value;
763
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200764 /* Need to create a kthread, thus must support schedule */
Jakub Kicinskia3884572018-01-11 20:29:09 -0800765 if (bpf_map_is_dev_bound(map)) {
766 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
767 goto out;
John Fastabend99ba2b52018-07-05 08:50:04 -0700768 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
769 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
770 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200771 err = map->ops->map_update_elem(map, key, value, attr->flags);
772 goto out;
773 }
774
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800775 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
776 * inside bpf map update or delete otherwise deadlocks are possible
777 */
778 preempt_disable();
779 __this_cpu_inc(bpf_prog_active);
Martin KaFai Lau8f844932016-11-11 10:55:10 -0800780 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
781 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800782 err = bpf_percpu_hash_update(map, key, value, attr->flags);
783 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
784 err = bpf_percpu_array_update(map, key, value, attr->flags);
Mickaël Salaün9c147b52018-01-26 00:54:02 +0100785 } else if (IS_FD_ARRAY(map)) {
Daniel Borkmannd056a782016-06-15 22:47:13 +0200786 rcu_read_lock();
787 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
788 attr->flags);
789 rcu_read_unlock();
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700790 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
791 rcu_read_lock();
792 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
793 attr->flags);
794 rcu_read_unlock();
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700795 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
796 /* rcu_read_lock() is not needed */
797 err = bpf_fd_reuseport_array_update_elem(map, key, value,
798 attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800799 } else {
800 rcu_read_lock();
801 err = map->ops->map_update_elem(map, key, value, attr->flags);
802 rcu_read_unlock();
803 }
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800804 __this_cpu_dec(bpf_prog_active);
805 preempt_enable();
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +0200806out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700807free_value:
808 kfree(value);
809free_key:
810 kfree(key);
811err_put:
812 fdput(f);
813 return err;
814}
815
816#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
817
818static int map_delete_elem(union bpf_attr *attr)
819{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100820 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700821 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700822 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200823 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700824 void *key;
825 int err;
826
827 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
828 return -EINVAL;
829
Daniel Borkmann592867b2015-09-08 18:00:09 +0200830 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100831 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700832 if (IS_ERR(map))
833 return PTR_ERR(map);
834
Chenbo Feng6e71b042017-10-18 13:00:22 -0700835 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
836 err = -EPERM;
837 goto err_put;
838 }
839
Al Viroe4448ed2017-05-13 18:43:00 -0400840 key = memdup_user(ukey, map->key_size);
841 if (IS_ERR(key)) {
842 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700843 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400844 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700845
Jakub Kicinskia3884572018-01-11 20:29:09 -0800846 if (bpf_map_is_dev_bound(map)) {
847 err = bpf_map_offload_delete_elem(map, key);
848 goto out;
849 }
850
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800851 preempt_disable();
852 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700853 rcu_read_lock();
854 err = map->ops->map_delete_elem(map, key);
855 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800856 __this_cpu_dec(bpf_prog_active);
857 preempt_enable();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800858out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700859 kfree(key);
860err_put:
861 fdput(f);
862 return err;
863}
864
865/* last field in 'union bpf_attr' used by this command */
866#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
867
868static int map_get_next_key(union bpf_attr *attr)
869{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100870 void __user *ukey = u64_to_user_ptr(attr->key);
871 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700872 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700873 struct bpf_map *map;
874 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200875 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700876 int err;
877
878 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
879 return -EINVAL;
880
Daniel Borkmann592867b2015-09-08 18:00:09 +0200881 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100882 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700883 if (IS_ERR(map))
884 return PTR_ERR(map);
885
Chenbo Feng6e71b042017-10-18 13:00:22 -0700886 if (!(f.file->f_mode & FMODE_CAN_READ)) {
887 err = -EPERM;
888 goto err_put;
889 }
890
Teng Qin8fe45922017-04-24 19:00:37 -0700891 if (ukey) {
Al Viroe4448ed2017-05-13 18:43:00 -0400892 key = memdup_user(ukey, map->key_size);
893 if (IS_ERR(key)) {
894 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -0700895 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -0400896 }
Teng Qin8fe45922017-04-24 19:00:37 -0700897 } else {
898 key = NULL;
899 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700900
901 err = -ENOMEM;
902 next_key = kmalloc(map->key_size, GFP_USER);
903 if (!next_key)
904 goto free_key;
905
Jakub Kicinskia3884572018-01-11 20:29:09 -0800906 if (bpf_map_is_dev_bound(map)) {
907 err = bpf_map_offload_get_next_key(map, key, next_key);
908 goto out;
909 }
910
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700911 rcu_read_lock();
912 err = map->ops->map_get_next_key(map, key, next_key);
913 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -0800914out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700915 if (err)
916 goto free_next_key;
917
918 err = -EFAULT;
919 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
920 goto free_next_key;
921
922 err = 0;
923
924free_next_key:
925 kfree(next_key);
926free_key:
927 kfree(key);
928err_put:
929 fdput(f);
930 return err;
931}
932
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700933static const struct bpf_prog_ops * const bpf_prog_types[] = {
934#define BPF_PROG_TYPE(_id, _name) \
935 [_id] = & _name ## _prog_ops,
936#define BPF_MAP_TYPE(_id, _ops)
937#include <linux/bpf_types.h>
938#undef BPF_PROG_TYPE
939#undef BPF_MAP_TYPE
940};
941
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700942static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
943{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +0200944 const struct bpf_prog_ops *ops;
945
946 if (type >= ARRAY_SIZE(bpf_prog_types))
947 return -EINVAL;
948 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
949 ops = bpf_prog_types[type];
950 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +0200951 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700952
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700953 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +0200954 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700955 else
956 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200957 prog->type = type;
958 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700959}
960
961/* drop refcnt on maps used by eBPF program and free auxilary data */
962static void free_used_maps(struct bpf_prog_aux *aux)
963{
964 int i;
965
Roman Gushchinde9cbba2018-08-02 14:27:18 -0700966 if (aux->cgroup_storage)
967 bpf_cgroup_storage_release(aux->prog, aux->cgroup_storage);
968
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700969 for (i = 0; i < aux->used_map_cnt; i++)
970 bpf_map_put(aux->used_maps[i]);
971
972 kfree(aux->used_maps);
973}
974
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100975int __bpf_prog_charge(struct user_struct *user, u32 pages)
976{
977 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
978 unsigned long user_bufs;
979
980 if (user) {
981 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
982 if (user_bufs > memlock_limit) {
983 atomic_long_sub(pages, &user->locked_vm);
984 return -EPERM;
985 }
986 }
987
988 return 0;
989}
990
991void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
992{
993 if (user)
994 atomic_long_sub(pages, &user->locked_vm);
995}
996
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700997static int bpf_prog_charge_memlock(struct bpf_prog *prog)
998{
999 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001000 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001001
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001002 ret = __bpf_prog_charge(user, prog->pages);
1003 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001004 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001005 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001006 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001007
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001008 prog->aux->user = user;
1009 return 0;
1010}
1011
1012static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1013{
1014 struct user_struct *user = prog->aux->user;
1015
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001016 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001017 free_uid(user);
1018}
1019
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001020static int bpf_prog_alloc_id(struct bpf_prog *prog)
1021{
1022 int id;
1023
Shaohua Lib76354c2018-03-27 11:53:21 -07001024 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001025 spin_lock_bh(&prog_idr_lock);
1026 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1027 if (id > 0)
1028 prog->aux->id = id;
1029 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001030 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001031
1032 /* id is in [1, INT_MAX) */
1033 if (WARN_ON_ONCE(!id))
1034 return -ENOSPC;
1035
1036 return id > 0 ? 0 : id;
1037}
1038
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001039void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001040{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001041 /* cBPF to eBPF migrations are currently not in the idr store.
1042 * Offloaded programs are removed from the store when their device
1043 * disappears - even if someone grabs an fd to them they are unusable,
1044 * simply waiting for refcnt to drop to be freed.
1045 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001046 if (!prog->aux->id)
1047 return;
1048
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001049 if (do_idr_lock)
1050 spin_lock_bh(&prog_idr_lock);
1051 else
1052 __acquire(&prog_idr_lock);
1053
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001054 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001055 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001056
1057 if (do_idr_lock)
1058 spin_unlock_bh(&prog_idr_lock);
1059 else
1060 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001061}
1062
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001063static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001064{
1065 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1066
1067 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001068 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001069 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001070 bpf_prog_free(aux->prog);
1071}
1072
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001073static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001074{
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001075 if (atomic_dec_and_test(&prog->aux->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001076 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001077 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001078 bpf_prog_kallsyms_del_all(prog);
Daniel Borkmann4f74d802017-12-20 13:42:56 +01001079
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001080 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001081 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001082}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001083
1084void bpf_prog_put(struct bpf_prog *prog)
1085{
1086 __bpf_prog_put(prog, true);
1087}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001088EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001089
1090static int bpf_prog_release(struct inode *inode, struct file *filp)
1091{
1092 struct bpf_prog *prog = filp->private_data;
1093
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001094 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001095 return 0;
1096}
1097
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001098#ifdef CONFIG_PROC_FS
1099static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1100{
1101 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001102 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001103
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001104 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001105 seq_printf(m,
1106 "prog_type:\t%u\n"
1107 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001108 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001109 "memlock:\t%llu\n"
1110 "prog_id:\t%u\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001111 prog->type,
1112 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001113 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001114 prog->pages * 1ULL << PAGE_SHIFT,
1115 prog->aux->id);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001116}
1117#endif
1118
Chenbo Fengf66e4482017-10-18 13:00:26 -07001119const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001120#ifdef CONFIG_PROC_FS
1121 .show_fdinfo = bpf_prog_show_fdinfo,
1122#endif
1123 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001124 .read = bpf_dummy_read,
1125 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001126};
1127
Daniel Borkmannb2197752015-10-29 14:58:09 +01001128int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001129{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001130 int ret;
1131
1132 ret = security_bpf_prog(prog);
1133 if (ret < 0)
1134 return ret;
1135
Daniel Borkmannaa797812015-10-29 14:58:06 +01001136 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1137 O_RDWR | O_CLOEXEC);
1138}
1139
Daniel Borkmann113214b2016-06-30 17:24:44 +02001140static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001141{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001142 if (!f.file)
1143 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001144 if (f.file->f_op != &bpf_prog_fops) {
1145 fdput(f);
1146 return ERR_PTR(-EINVAL);
1147 }
1148
Daniel Borkmannc2101292015-10-29 14:58:07 +01001149 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001150}
1151
Brenden Blanco59d36562016-07-19 12:16:46 -07001152struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001153{
Brenden Blanco59d36562016-07-19 12:16:46 -07001154 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1155 atomic_sub(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001156 return ERR_PTR(-EBUSY);
1157 }
1158 return prog;
1159}
Brenden Blanco59d36562016-07-19 12:16:46 -07001160EXPORT_SYMBOL_GPL(bpf_prog_add);
1161
Daniel Borkmannc5405942016-11-09 22:02:34 +01001162void bpf_prog_sub(struct bpf_prog *prog, int i)
1163{
1164 /* Only to be used for undoing previous bpf_prog_add() in some
1165 * error path. We still know that another entity in our call
1166 * path holds a reference to the program, thus atomic_sub() can
1167 * be safely used in such cases!
1168 */
1169 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1170}
1171EXPORT_SYMBOL_GPL(bpf_prog_sub);
1172
Brenden Blanco59d36562016-07-19 12:16:46 -07001173struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1174{
1175 return bpf_prog_add(prog, 1);
1176}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001177EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001178
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001179/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001180struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001181{
1182 int refold;
1183
1184 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1185
1186 if (refold >= BPF_MAX_REFCNT) {
1187 __bpf_prog_put(prog, false);
1188 return ERR_PTR(-EBUSY);
1189 }
1190
1191 if (!refold)
1192 return ERR_PTR(-ENOENT);
1193
1194 return prog;
1195}
John Fastabenda6f6df62017-08-15 22:32:22 -07001196EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001197
Al Viro040ee692017-12-02 20:20:38 -05001198bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001199 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001200{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001201 /* not an attachment, just a refcount inc, always allow */
1202 if (!attach_type)
1203 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001204
1205 if (prog->type != *attach_type)
1206 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001207 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001208 return false;
1209
1210 return true;
1211}
1212
1213static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001214 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001215{
1216 struct fd f = fdget(ufd);
1217 struct bpf_prog *prog;
1218
Daniel Borkmann113214b2016-06-30 17:24:44 +02001219 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001220 if (IS_ERR(prog))
1221 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001222 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001223 prog = ERR_PTR(-EINVAL);
1224 goto out;
1225 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001226
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001227 prog = bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001228out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001229 fdput(f);
1230 return prog;
1231}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001232
1233struct bpf_prog *bpf_prog_get(u32 ufd)
1234{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001235 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001236}
1237
Jakub Kicinski248f3462017-11-03 13:56:20 -07001238struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001239 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001240{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001241 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001242}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001243EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001244
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001245/* Initially all BPF programs could be loaded w/o specifying
1246 * expected_attach_type. Later for some of them specifying expected_attach_type
1247 * at load time became required so that program could be validated properly.
1248 * Programs of types that are allowed to be loaded both w/ and w/o (for
1249 * backward compatibility) expected_attach_type, should have the default attach
1250 * type assigned to expected_attach_type for the latter case, so that it can be
1251 * validated later at attach time.
1252 *
1253 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1254 * prog type requires it but has some attach types that have to be backward
1255 * compatible.
1256 */
1257static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1258{
1259 switch (attr->prog_type) {
1260 case BPF_PROG_TYPE_CGROUP_SOCK:
1261 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1262 * exist so checking for non-zero is the way to go here.
1263 */
1264 if (!attr->expected_attach_type)
1265 attr->expected_attach_type =
1266 BPF_CGROUP_INET_SOCK_CREATE;
1267 break;
1268 }
1269}
1270
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001271static int
1272bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1273 enum bpf_attach_type expected_attach_type)
1274{
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001275 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001276 case BPF_PROG_TYPE_CGROUP_SOCK:
1277 switch (expected_attach_type) {
1278 case BPF_CGROUP_INET_SOCK_CREATE:
1279 case BPF_CGROUP_INET4_POST_BIND:
1280 case BPF_CGROUP_INET6_POST_BIND:
1281 return 0;
1282 default:
1283 return -EINVAL;
1284 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001285 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1286 switch (expected_attach_type) {
1287 case BPF_CGROUP_INET4_BIND:
1288 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001289 case BPF_CGROUP_INET4_CONNECT:
1290 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001291 case BPF_CGROUP_UDP4_SENDMSG:
1292 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001293 return 0;
1294 default:
1295 return -EINVAL;
1296 }
1297 default:
1298 return 0;
1299 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001300}
1301
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001302/* last field in 'union bpf_attr' used by this command */
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001303#define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001304
1305static int bpf_prog_load(union bpf_attr *attr)
1306{
1307 enum bpf_prog_type type = attr->prog_type;
1308 struct bpf_prog *prog;
1309 int err;
1310 char license[128];
1311 bool is_gpl;
1312
1313 if (CHECK_ATTR(BPF_PROG_LOAD))
1314 return -EINVAL;
1315
David S. Millere07b98d2017-05-10 11:38:07 -07001316 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1317 return -EINVAL;
1318
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001319 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001320 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001321 sizeof(license) - 1) < 0)
1322 return -EFAULT;
1323 license[sizeof(license) - 1] = 0;
1324
1325 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1326 is_gpl = license_is_gpl_compatible(license);
1327
Daniel Borkmannef0915c2016-12-07 01:15:44 +01001328 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1329 return -E2BIG;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001330
Alexei Starovoitov25415172015-03-25 12:49:20 -07001331 if (type == BPF_PROG_TYPE_KPROBE &&
1332 attr->kern_version != LINUX_VERSION_CODE)
1333 return -EINVAL;
1334
Chenbo Feng80b7d812017-05-31 18:16:00 -07001335 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1336 type != BPF_PROG_TYPE_CGROUP_SKB &&
1337 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001338 return -EPERM;
1339
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001340 bpf_prog_load_fixup_attach_type(attr);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001341 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1342 return -EINVAL;
1343
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001344 /* plain bpf_prog allocation */
1345 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1346 if (!prog)
1347 return -ENOMEM;
1348
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001349 prog->expected_attach_type = attr->expected_attach_type;
1350
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001351 prog->aux->offload_requested = !!attr->prog_ifindex;
1352
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001353 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001354 if (err)
1355 goto free_prog_nouncharge;
1356
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001357 err = bpf_prog_charge_memlock(prog);
1358 if (err)
1359 goto free_prog_sec;
1360
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001361 prog->len = attr->insn_cnt;
1362
1363 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001364 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01001365 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001366 goto free_prog;
1367
1368 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001369 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001370
1371 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001372 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001373
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08001374 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001375 err = bpf_prog_offload_init(prog, attr);
1376 if (err)
1377 goto free_prog;
1378 }
1379
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001380 /* find program type: socket_filter vs tracing_filter */
1381 err = find_prog_type(type, prog);
1382 if (err < 0)
1383 goto free_prog;
1384
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001385 prog->aux->load_time = ktime_get_boot_ns();
1386 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1387 if (err)
1388 goto free_prog;
1389
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001390 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001391 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001392 if (err < 0)
1393 goto free_used_maps;
1394
Daniel Borkmann9facc332018-06-15 02:30:48 +02001395 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07001396 if (err < 0)
1397 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001398
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001399 err = bpf_prog_alloc_id(prog);
1400 if (err)
1401 goto free_used_maps;
1402
Daniel Borkmannaa797812015-10-29 14:58:06 +01001403 err = bpf_prog_new_fd(prog);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001404 if (err < 0) {
1405 /* failed to allocate fd.
1406 * bpf_prog_put() is needed because the above
1407 * bpf_prog_alloc_id() has published the prog
1408 * to the userspace and the userspace may
1409 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1410 */
1411 bpf_prog_put(prog);
1412 return err;
1413 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001414
Daniel Borkmann74451e662017-02-16 22:24:50 +01001415 bpf_prog_kallsyms_add(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001416 return err;
1417
1418free_used_maps:
Daniel Borkmann7d1982b2018-06-15 02:30:47 +02001419 bpf_prog_kallsyms_del_subprogs(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001420 free_used_maps(prog->aux);
1421free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001422 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001423free_prog_sec:
1424 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001425free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001426 bpf_prog_free(prog);
1427 return err;
1428}
1429
Chenbo Feng6e71b042017-10-18 13:00:22 -07001430#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01001431
1432static int bpf_obj_pin(const union bpf_attr *attr)
1433{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001434 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001435 return -EINVAL;
1436
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001437 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01001438}
1439
1440static int bpf_obj_get(const union bpf_attr *attr)
1441{
Chenbo Feng6e71b042017-10-18 13:00:22 -07001442 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1443 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01001444 return -EINVAL;
1445
Chenbo Feng6e71b042017-10-18 13:00:22 -07001446 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1447 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01001448}
1449
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07001450struct bpf_raw_tracepoint {
1451 struct bpf_raw_event_map *btp;
1452 struct bpf_prog *prog;
1453};
1454
1455static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1456{
1457 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1458
1459 if (raw_tp->prog) {
1460 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1461 bpf_prog_put(raw_tp->prog);
1462 }
1463 kfree(raw_tp);
1464 return 0;
1465}
1466
1467static const struct file_operations bpf_raw_tp_fops = {
1468 .release = bpf_raw_tracepoint_release,
1469 .read = bpf_dummy_read,
1470 .write = bpf_dummy_write,
1471};
1472
1473#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1474
1475static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1476{
1477 struct bpf_raw_tracepoint *raw_tp;
1478 struct bpf_raw_event_map *btp;
1479 struct bpf_prog *prog;
1480 char tp_name[128];
1481 int tp_fd, err;
1482
1483 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1484 sizeof(tp_name) - 1) < 0)
1485 return -EFAULT;
1486 tp_name[sizeof(tp_name) - 1] = 0;
1487
1488 btp = bpf_find_raw_tracepoint(tp_name);
1489 if (!btp)
1490 return -ENOENT;
1491
1492 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1493 if (!raw_tp)
1494 return -ENOMEM;
1495 raw_tp->btp = btp;
1496
1497 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1498 BPF_PROG_TYPE_RAW_TRACEPOINT);
1499 if (IS_ERR(prog)) {
1500 err = PTR_ERR(prog);
1501 goto out_free_tp;
1502 }
1503
1504 err = bpf_probe_register(raw_tp->btp, prog);
1505 if (err)
1506 goto out_put_prog;
1507
1508 raw_tp->prog = prog;
1509 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1510 O_CLOEXEC);
1511 if (tp_fd < 0) {
1512 bpf_probe_unregister(raw_tp->btp, prog);
1513 err = tp_fd;
1514 goto out_put_prog;
1515 }
1516 return tp_fd;
1517
1518out_put_prog:
1519 bpf_prog_put(prog);
1520out_free_tp:
1521 kfree(raw_tp);
1522 return err;
1523}
1524
Anders Roxell33491582018-04-03 14:09:47 +02001525static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1526 enum bpf_attach_type attach_type)
1527{
1528 switch (prog->type) {
1529 case BPF_PROG_TYPE_CGROUP_SOCK:
1530 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1531 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1532 default:
1533 return 0;
1534 }
1535}
1536
John Fastabend464bc0f2017-08-28 07:10:04 -07001537#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
John Fastabend174a79f2017-08-15 22:32:47 -07001538
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001539#define BPF_F_ATTACH_MASK \
1540 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1541
Daniel Mackf4324552016-11-23 16:52:27 +01001542static int bpf_prog_attach(const union bpf_attr *attr)
1543{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001544 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001545 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001546 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001547
1548 if (!capable(CAP_NET_ADMIN))
1549 return -EPERM;
1550
1551 if (CHECK_ATTR(BPF_PROG_ATTACH))
1552 return -EINVAL;
1553
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001554 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001555 return -EINVAL;
1556
Daniel Mackf4324552016-11-23 16:52:27 +01001557 switch (attr->attach_type) {
1558 case BPF_CGROUP_INET_INGRESS:
1559 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08001560 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01001561 break;
David Ahern610236582016-12-01 08:48:04 -08001562 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001563 case BPF_CGROUP_INET4_POST_BIND:
1564 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08001565 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1566 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001567 case BPF_CGROUP_INET4_BIND:
1568 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001569 case BPF_CGROUP_INET4_CONNECT:
1570 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001571 case BPF_CGROUP_UDP4_SENDMSG:
1572 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001573 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1574 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001575 case BPF_CGROUP_SOCK_OPS:
1576 ptype = BPF_PROG_TYPE_SOCK_OPS;
1577 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001578 case BPF_CGROUP_DEVICE:
1579 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1580 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001581 case BPF_SK_MSG_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001582 ptype = BPF_PROG_TYPE_SK_MSG;
1583 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07001584 case BPF_SK_SKB_STREAM_PARSER:
1585 case BPF_SK_SKB_STREAM_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001586 ptype = BPF_PROG_TYPE_SK_SKB;
1587 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01001588 case BPF_LIRC_MODE2:
Sean Youngfdb5c452018-06-19 00:04:24 +01001589 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1590 break;
Daniel Mackf4324552016-11-23 16:52:27 +01001591 default:
1592 return -EINVAL;
1593 }
1594
David Ahernb2cd1252016-12-01 08:48:03 -08001595 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1596 if (IS_ERR(prog))
1597 return PTR_ERR(prog);
1598
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001599 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1600 bpf_prog_put(prog);
1601 return -EINVAL;
1602 }
1603
Sean Youngfdb5c452018-06-19 00:04:24 +01001604 switch (ptype) {
1605 case BPF_PROG_TYPE_SK_SKB:
1606 case BPF_PROG_TYPE_SK_MSG:
1607 ret = sockmap_get_from_fd(attr, ptype, prog);
1608 break;
1609 case BPF_PROG_TYPE_LIRC_MODE2:
1610 ret = lirc_prog_attach(attr, prog);
1611 break;
1612 default:
1613 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
David Ahernb2cd1252016-12-01 08:48:03 -08001614 }
1615
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001616 if (ret)
1617 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08001618 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01001619}
1620
1621#define BPF_PROG_DETACH_LAST_FIELD attach_type
1622
1623static int bpf_prog_detach(const union bpf_attr *attr)
1624{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001625 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01001626
1627 if (!capable(CAP_NET_ADMIN))
1628 return -EPERM;
1629
1630 if (CHECK_ATTR(BPF_PROG_DETACH))
1631 return -EINVAL;
1632
1633 switch (attr->attach_type) {
1634 case BPF_CGROUP_INET_INGRESS:
1635 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001636 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1637 break;
David Ahern610236582016-12-01 08:48:04 -08001638 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001639 case BPF_CGROUP_INET4_POST_BIND:
1640 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001641 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1642 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001643 case BPF_CGROUP_INET4_BIND:
1644 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001645 case BPF_CGROUP_INET4_CONNECT:
1646 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001647 case BPF_CGROUP_UDP4_SENDMSG:
1648 case BPF_CGROUP_UDP6_SENDMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001649 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1650 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001651 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07001652 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01001653 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05001654 case BPF_CGROUP_DEVICE:
1655 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1656 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07001657 case BPF_SK_MSG_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001658 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, NULL);
John Fastabend5a67da22017-09-08 14:00:49 -07001659 case BPF_SK_SKB_STREAM_PARSER:
1660 case BPF_SK_SKB_STREAM_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01001661 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, NULL);
Sean Youngf4364dc2018-05-27 12:24:09 +01001662 case BPF_LIRC_MODE2:
1663 return lirc_prog_detach(attr);
Daniel Mackf4324552016-11-23 16:52:27 +01001664 default:
1665 return -EINVAL;
1666 }
1667
Sean Youngfdb5c452018-06-19 00:04:24 +01001668 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01001669}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07001670
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001671#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1672
1673static int bpf_prog_query(const union bpf_attr *attr,
1674 union bpf_attr __user *uattr)
1675{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001676 if (!capable(CAP_NET_ADMIN))
1677 return -EPERM;
1678 if (CHECK_ATTR(BPF_PROG_QUERY))
1679 return -EINVAL;
1680 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1681 return -EINVAL;
1682
1683 switch (attr->query.attach_type) {
1684 case BPF_CGROUP_INET_INGRESS:
1685 case BPF_CGROUP_INET_EGRESS:
1686 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001687 case BPF_CGROUP_INET4_BIND:
1688 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001689 case BPF_CGROUP_INET4_POST_BIND:
1690 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001691 case BPF_CGROUP_INET4_CONNECT:
1692 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001693 case BPF_CGROUP_UDP4_SENDMSG:
1694 case BPF_CGROUP_UDP6_SENDMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001695 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05001696 case BPF_CGROUP_DEVICE:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001697 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01001698 case BPF_LIRC_MODE2:
1699 return lirc_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001700 default:
1701 return -EINVAL;
1702 }
Sean Youngfdb5c452018-06-19 00:04:24 +01001703
1704 return cgroup_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07001705}
Daniel Mackf4324552016-11-23 16:52:27 +01001706
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001707#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1708
1709static int bpf_prog_test_run(const union bpf_attr *attr,
1710 union bpf_attr __user *uattr)
1711{
1712 struct bpf_prog *prog;
1713 int ret = -ENOTSUPP;
1714
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08001715 if (!capable(CAP_SYS_ADMIN))
1716 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07001717 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1718 return -EINVAL;
1719
1720 prog = bpf_prog_get(attr->test.prog_fd);
1721 if (IS_ERR(prog))
1722 return PTR_ERR(prog);
1723
1724 if (prog->aux->ops->test_run)
1725 ret = prog->aux->ops->test_run(prog, attr, uattr);
1726
1727 bpf_prog_put(prog);
1728 return ret;
1729}
1730
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001731#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1732
1733static int bpf_obj_get_next_id(const union bpf_attr *attr,
1734 union bpf_attr __user *uattr,
1735 struct idr *idr,
1736 spinlock_t *lock)
1737{
1738 u32 next_id = attr->start_id;
1739 int err = 0;
1740
1741 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1742 return -EINVAL;
1743
1744 if (!capable(CAP_SYS_ADMIN))
1745 return -EPERM;
1746
1747 next_id++;
1748 spin_lock_bh(lock);
1749 if (!idr_get_next(idr, &next_id))
1750 err = -ENOENT;
1751 spin_unlock_bh(lock);
1752
1753 if (!err)
1754 err = put_user(next_id, &uattr->next_id);
1755
1756 return err;
1757}
1758
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001759#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1760
1761static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1762{
1763 struct bpf_prog *prog;
1764 u32 id = attr->prog_id;
1765 int fd;
1766
1767 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1768 return -EINVAL;
1769
1770 if (!capable(CAP_SYS_ADMIN))
1771 return -EPERM;
1772
1773 spin_lock_bh(&prog_idr_lock);
1774 prog = idr_find(&prog_idr, id);
1775 if (prog)
1776 prog = bpf_prog_inc_not_zero(prog);
1777 else
1778 prog = ERR_PTR(-ENOENT);
1779 spin_unlock_bh(&prog_idr_lock);
1780
1781 if (IS_ERR(prog))
1782 return PTR_ERR(prog);
1783
1784 fd = bpf_prog_new_fd(prog);
1785 if (fd < 0)
1786 bpf_prog_put(prog);
1787
1788 return fd;
1789}
1790
Chenbo Feng6e71b042017-10-18 13:00:22 -07001791#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001792
1793static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1794{
1795 struct bpf_map *map;
1796 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001797 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001798 int fd;
1799
Chenbo Feng6e71b042017-10-18 13:00:22 -07001800 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1801 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001802 return -EINVAL;
1803
1804 if (!capable(CAP_SYS_ADMIN))
1805 return -EPERM;
1806
Chenbo Feng6e71b042017-10-18 13:00:22 -07001807 f_flags = bpf_get_file_flag(attr->open_flags);
1808 if (f_flags < 0)
1809 return f_flags;
1810
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001811 spin_lock_bh(&map_idr_lock);
1812 map = idr_find(&map_idr, id);
1813 if (map)
1814 map = bpf_map_inc_not_zero(map, true);
1815 else
1816 map = ERR_PTR(-ENOENT);
1817 spin_unlock_bh(&map_idr_lock);
1818
1819 if (IS_ERR(map))
1820 return PTR_ERR(map);
1821
Chenbo Feng6e71b042017-10-18 13:00:22 -07001822 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001823 if (fd < 0)
1824 bpf_map_put(map);
1825
1826 return fd;
1827}
1828
Daniel Borkmann7105e822017-12-20 13:42:57 +01001829static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1830 unsigned long addr)
1831{
1832 int i;
1833
1834 for (i = 0; i < prog->aux->used_map_cnt; i++)
1835 if (prog->aux->used_maps[i] == (void *)addr)
1836 return prog->aux->used_maps[i];
1837 return NULL;
1838}
1839
1840static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1841{
1842 const struct bpf_map *map;
1843 struct bpf_insn *insns;
1844 u64 imm;
1845 int i;
1846
1847 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1848 GFP_USER);
1849 if (!insns)
1850 return insns;
1851
1852 for (i = 0; i < prog->len; i++) {
1853 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1854 insns[i].code = BPF_JMP | BPF_CALL;
1855 insns[i].imm = BPF_FUNC_tail_call;
1856 /* fall-through */
1857 }
1858 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1859 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1860 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1861 insns[i].code = BPF_JMP | BPF_CALL;
1862 if (!bpf_dump_raw_ok())
1863 insns[i].imm = 0;
1864 continue;
1865 }
1866
1867 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1868 continue;
1869
1870 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1871 map = bpf_map_from_imm(prog, imm);
1872 if (map) {
1873 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1874 insns[i].imm = map->id;
1875 insns[i + 1].imm = 0;
1876 continue;
1877 }
1878
1879 if (!bpf_dump_raw_ok() &&
1880 imm == (unsigned long)prog->aux) {
1881 insns[i].imm = 0;
1882 insns[i + 1].imm = 0;
1883 continue;
1884 }
1885 }
1886
1887 return insns;
1888}
1889
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001890static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1891 const union bpf_attr *attr,
1892 union bpf_attr __user *uattr)
1893{
1894 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1895 struct bpf_prog_info info = {};
1896 u32 info_len = attr->info.info_len;
1897 char __user *uinsns;
1898 u32 ulen;
1899 int err;
1900
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07001901 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001902 if (err)
1903 return err;
1904 info_len = min_t(u32, sizeof(info), info_len);
1905
1906 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02001907 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001908
1909 info.type = prog->type;
1910 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001911 info.load_time = prog->aux->load_time;
1912 info.created_by_uid = from_kuid_munged(current_user_ns(),
1913 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02001914 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001915
1916 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001917 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1918
1919 ulen = info.nr_map_ids;
1920 info.nr_map_ids = prog->aux->used_map_cnt;
1921 ulen = min_t(u32, info.nr_map_ids, ulen);
1922 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07001923 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07001924 u32 i;
1925
1926 for (i = 0; i < ulen; i++)
1927 if (put_user(prog->aux->used_maps[i]->id,
1928 &user_map_ids[i]))
1929 return -EFAULT;
1930 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001931
1932 if (!capable(CAP_SYS_ADMIN)) {
1933 info.jited_prog_len = 0;
1934 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05301935 info.nr_jited_ksyms = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001936 goto done;
1937 }
1938
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001939 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02001940 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001941 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01001942 struct bpf_insn *insns_sanitized;
1943 bool fault;
1944
1945 if (prog->blinded && !bpf_dump_raw_ok()) {
1946 info.xlated_prog_insns = 0;
1947 goto done;
1948 }
1949 insns_sanitized = bpf_insn_prepare_dump(prog);
1950 if (!insns_sanitized)
1951 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001952 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1953 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01001954 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1955 kfree(insns_sanitized);
1956 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07001957 return -EFAULT;
1958 }
1959
Jakub Kicinski675fc272017-12-27 18:39:09 -08001960 if (bpf_prog_is_dev_bound(prog->aux)) {
1961 err = bpf_prog_offload_info_fill(&info, prog);
1962 if (err)
1963 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08001964 goto done;
1965 }
1966
1967 /* NOTE: the following code is supposed to be skipped for offload.
1968 * bpf_prog_offload_info_fill() is the place to fill similar fields
1969 * for offload.
1970 */
1971 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05301972 if (prog->aux->func_cnt) {
1973 u32 i;
1974
1975 info.jited_prog_len = 0;
1976 for (i = 0; i < prog->aux->func_cnt; i++)
1977 info.jited_prog_len += prog->aux->func[i]->jited_len;
1978 } else {
1979 info.jited_prog_len = prog->jited_len;
1980 }
1981
Jiong Wangfcfb1262018-01-16 16:05:19 -08001982 if (info.jited_prog_len && ulen) {
1983 if (bpf_dump_raw_ok()) {
1984 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1985 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05301986
1987 /* for multi-function programs, copy the JITed
1988 * instructions for all the functions
1989 */
1990 if (prog->aux->func_cnt) {
1991 u32 len, free, i;
1992 u8 *img;
1993
1994 free = ulen;
1995 for (i = 0; i < prog->aux->func_cnt; i++) {
1996 len = prog->aux->func[i]->jited_len;
1997 len = min_t(u32, len, free);
1998 img = (u8 *) prog->aux->func[i]->bpf_func;
1999 if (copy_to_user(uinsns, img, len))
2000 return -EFAULT;
2001 uinsns += len;
2002 free -= len;
2003 if (!free)
2004 break;
2005 }
2006 } else {
2007 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2008 return -EFAULT;
2009 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002010 } else {
2011 info.jited_prog_insns = 0;
2012 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002013 }
2014
Sandipan Dasdbecd732018-05-24 12:26:48 +05302015 ulen = info.nr_jited_ksyms;
2016 info.nr_jited_ksyms = prog->aux->func_cnt;
2017 if (info.nr_jited_ksyms && ulen) {
2018 if (bpf_dump_raw_ok()) {
2019 u64 __user *user_ksyms;
2020 ulong ksym_addr;
2021 u32 i;
2022
2023 /* copy the address of the kernel symbol
2024 * corresponding to each function
2025 */
2026 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2027 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2028 for (i = 0; i < ulen; i++) {
2029 ksym_addr = (ulong) prog->aux->func[i]->bpf_func;
2030 ksym_addr &= PAGE_MASK;
2031 if (put_user((u64) ksym_addr, &user_ksyms[i]))
2032 return -EFAULT;
2033 }
2034 } else {
2035 info.jited_ksyms = 0;
2036 }
2037 }
2038
Sandipan Das815581c2018-05-24 12:26:52 +05302039 ulen = info.nr_jited_func_lens;
2040 info.nr_jited_func_lens = prog->aux->func_cnt;
2041 if (info.nr_jited_func_lens && ulen) {
2042 if (bpf_dump_raw_ok()) {
2043 u32 __user *user_lens;
2044 u32 func_len, i;
2045
2046 /* copy the JITed image lengths for each function */
2047 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2048 user_lens = u64_to_user_ptr(info.jited_func_lens);
2049 for (i = 0; i < ulen; i++) {
2050 func_len = prog->aux->func[i]->jited_len;
2051 if (put_user(func_len, &user_lens[i]))
2052 return -EFAULT;
2053 }
2054 } else {
2055 info.jited_func_lens = 0;
2056 }
2057 }
2058
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002059done:
2060 if (copy_to_user(uinfo, &info, info_len) ||
2061 put_user(info_len, &uattr->info.info_len))
2062 return -EFAULT;
2063
2064 return 0;
2065}
2066
2067static int bpf_map_get_info_by_fd(struct bpf_map *map,
2068 const union bpf_attr *attr,
2069 union bpf_attr __user *uattr)
2070{
2071 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2072 struct bpf_map_info info = {};
2073 u32 info_len = attr->info.info_len;
2074 int err;
2075
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002076 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002077 if (err)
2078 return err;
2079 info_len = min_t(u32, sizeof(info), info_len);
2080
2081 info.type = map->map_type;
2082 info.id = map->id;
2083 info.key_size = map->key_size;
2084 info.value_size = map->value_size;
2085 info.max_entries = map->max_entries;
2086 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07002087 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002088
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002089 if (map->btf) {
2090 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07002091 info.btf_key_type_id = map->btf_key_type_id;
2092 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002093 }
2094
Jakub Kicinski52775b32018-01-17 19:13:28 -08002095 if (bpf_map_is_dev_bound(map)) {
2096 err = bpf_map_offload_info_fill(&info, map);
2097 if (err)
2098 return err;
2099 }
2100
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002101 if (copy_to_user(uinfo, &info, info_len) ||
2102 put_user(info_len, &uattr->info.info_len))
2103 return -EFAULT;
2104
2105 return 0;
2106}
2107
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002108static int bpf_btf_get_info_by_fd(struct btf *btf,
2109 const union bpf_attr *attr,
2110 union bpf_attr __user *uattr)
2111{
2112 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2113 u32 info_len = attr->info.info_len;
2114 int err;
2115
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002116 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002117 if (err)
2118 return err;
2119
2120 return btf_get_info_by_fd(btf, attr, uattr);
2121}
2122
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002123#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2124
2125static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2126 union bpf_attr __user *uattr)
2127{
2128 int ufd = attr->info.bpf_fd;
2129 struct fd f;
2130 int err;
2131
2132 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2133 return -EINVAL;
2134
2135 f = fdget(ufd);
2136 if (!f.file)
2137 return -EBADFD;
2138
2139 if (f.file->f_op == &bpf_prog_fops)
2140 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2141 uattr);
2142 else if (f.file->f_op == &bpf_map_fops)
2143 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2144 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07002145 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07002146 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002147 else
2148 err = -EINVAL;
2149
2150 fdput(f);
2151 return err;
2152}
2153
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002154#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2155
2156static int bpf_btf_load(const union bpf_attr *attr)
2157{
2158 if (CHECK_ATTR(BPF_BTF_LOAD))
2159 return -EINVAL;
2160
2161 if (!capable(CAP_SYS_ADMIN))
2162 return -EPERM;
2163
2164 return btf_new_fd(attr);
2165}
2166
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002167#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2168
2169static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2170{
2171 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2172 return -EINVAL;
2173
2174 if (!capable(CAP_SYS_ADMIN))
2175 return -EPERM;
2176
2177 return btf_get_fd_by_id(attr->btf_id);
2178}
2179
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002180static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2181 union bpf_attr __user *uattr,
2182 u32 prog_id, u32 fd_type,
2183 const char *buf, u64 probe_offset,
2184 u64 probe_addr)
2185{
2186 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2187 u32 len = buf ? strlen(buf) : 0, input_len;
2188 int err = 0;
2189
2190 if (put_user(len, &uattr->task_fd_query.buf_len))
2191 return -EFAULT;
2192 input_len = attr->task_fd_query.buf_len;
2193 if (input_len && ubuf) {
2194 if (!len) {
2195 /* nothing to copy, just make ubuf NULL terminated */
2196 char zero = '\0';
2197
2198 if (put_user(zero, ubuf))
2199 return -EFAULT;
2200 } else if (input_len >= len + 1) {
2201 /* ubuf can hold the string with NULL terminator */
2202 if (copy_to_user(ubuf, buf, len + 1))
2203 return -EFAULT;
2204 } else {
2205 /* ubuf cannot hold the string with NULL terminator,
2206 * do a partial copy with NULL terminator.
2207 */
2208 char zero = '\0';
2209
2210 err = -ENOSPC;
2211 if (copy_to_user(ubuf, buf, input_len - 1))
2212 return -EFAULT;
2213 if (put_user(zero, ubuf + input_len - 1))
2214 return -EFAULT;
2215 }
2216 }
2217
2218 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2219 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2220 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2221 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2222 return -EFAULT;
2223
2224 return err;
2225}
2226
2227#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2228
2229static int bpf_task_fd_query(const union bpf_attr *attr,
2230 union bpf_attr __user *uattr)
2231{
2232 pid_t pid = attr->task_fd_query.pid;
2233 u32 fd = attr->task_fd_query.fd;
2234 const struct perf_event *event;
2235 struct files_struct *files;
2236 struct task_struct *task;
2237 struct file *file;
2238 int err;
2239
2240 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2241 return -EINVAL;
2242
2243 if (!capable(CAP_SYS_ADMIN))
2244 return -EPERM;
2245
2246 if (attr->task_fd_query.flags != 0)
2247 return -EINVAL;
2248
2249 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2250 if (!task)
2251 return -ENOENT;
2252
2253 files = get_files_struct(task);
2254 put_task_struct(task);
2255 if (!files)
2256 return -ENOENT;
2257
2258 err = 0;
2259 spin_lock(&files->file_lock);
2260 file = fcheck_files(files, fd);
2261 if (!file)
2262 err = -EBADF;
2263 else
2264 get_file(file);
2265 spin_unlock(&files->file_lock);
2266 put_files_struct(files);
2267
2268 if (err)
2269 goto out;
2270
2271 if (file->f_op == &bpf_raw_tp_fops) {
2272 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2273 struct bpf_raw_event_map *btp = raw_tp->btp;
2274
2275 err = bpf_task_fd_query_copy(attr, uattr,
2276 raw_tp->prog->aux->id,
2277 BPF_FD_TYPE_RAW_TRACEPOINT,
2278 btp->tp->name, 0, 0);
2279 goto put_file;
2280 }
2281
2282 event = perf_get_event(file);
2283 if (!IS_ERR(event)) {
2284 u64 probe_offset, probe_addr;
2285 u32 prog_id, fd_type;
2286 const char *buf;
2287
2288 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2289 &buf, &probe_offset,
2290 &probe_addr);
2291 if (!err)
2292 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2293 fd_type, buf,
2294 probe_offset,
2295 probe_addr);
2296 goto put_file;
2297 }
2298
2299 err = -ENOTSUPP;
2300put_file:
2301 fput(file);
2302out:
2303 return err;
2304}
2305
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002306SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2307{
2308 union bpf_attr attr = {};
2309 int err;
2310
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07002311 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002312 return -EPERM;
2313
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002314 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002315 if (err)
2316 return err;
2317 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002318
2319 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2320 if (copy_from_user(&attr, uattr, size) != 0)
2321 return -EFAULT;
2322
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002323 err = security_bpf(cmd, &attr, size);
2324 if (err < 0)
2325 return err;
2326
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002327 switch (cmd) {
2328 case BPF_MAP_CREATE:
2329 err = map_create(&attr);
2330 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07002331 case BPF_MAP_LOOKUP_ELEM:
2332 err = map_lookup_elem(&attr);
2333 break;
2334 case BPF_MAP_UPDATE_ELEM:
2335 err = map_update_elem(&attr);
2336 break;
2337 case BPF_MAP_DELETE_ELEM:
2338 err = map_delete_elem(&attr);
2339 break;
2340 case BPF_MAP_GET_NEXT_KEY:
2341 err = map_get_next_key(&attr);
2342 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002343 case BPF_PROG_LOAD:
2344 err = bpf_prog_load(&attr);
2345 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01002346 case BPF_OBJ_PIN:
2347 err = bpf_obj_pin(&attr);
2348 break;
2349 case BPF_OBJ_GET:
2350 err = bpf_obj_get(&attr);
2351 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002352 case BPF_PROG_ATTACH:
2353 err = bpf_prog_attach(&attr);
2354 break;
2355 case BPF_PROG_DETACH:
2356 err = bpf_prog_detach(&attr);
2357 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002358 case BPF_PROG_QUERY:
2359 err = bpf_prog_query(&attr, uattr);
2360 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002361 case BPF_PROG_TEST_RUN:
2362 err = bpf_prog_test_run(&attr, uattr);
2363 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002364 case BPF_PROG_GET_NEXT_ID:
2365 err = bpf_obj_get_next_id(&attr, uattr,
2366 &prog_idr, &prog_idr_lock);
2367 break;
2368 case BPF_MAP_GET_NEXT_ID:
2369 err = bpf_obj_get_next_id(&attr, uattr,
2370 &map_idr, &map_idr_lock);
2371 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002372 case BPF_PROG_GET_FD_BY_ID:
2373 err = bpf_prog_get_fd_by_id(&attr);
2374 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002375 case BPF_MAP_GET_FD_BY_ID:
2376 err = bpf_map_get_fd_by_id(&attr);
2377 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002378 case BPF_OBJ_GET_INFO_BY_FD:
2379 err = bpf_obj_get_info_by_fd(&attr, uattr);
2380 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002381 case BPF_RAW_TRACEPOINT_OPEN:
2382 err = bpf_raw_tracepoint_open(&attr);
2383 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07002384 case BPF_BTF_LOAD:
2385 err = bpf_btf_load(&attr);
2386 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07002387 case BPF_BTF_GET_FD_BY_ID:
2388 err = bpf_btf_get_fd_by_id(&attr);
2389 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07002390 case BPF_TASK_FD_QUERY:
2391 err = bpf_task_fd_query(&attr, uattr);
2392 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002393 default:
2394 err = -EINVAL;
2395 break;
2396 }
2397
2398 return err;
2399}