Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 1 | /* 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 Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 13 | #include <linux/bpf_trace.h> |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 14 | #include <linux/btf.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 15 | #include <linux/syscalls.h> |
| 16 | #include <linux/slab.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 18 | #include <linux/vmalloc.h> |
| 19 | #include <linux/mmzone.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 20 | #include <linux/anon_inodes.h> |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 21 | #include <linux/file.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 22 | #include <linux/license.h> |
| 23 | #include <linux/filter.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 24 | #include <linux/version.h> |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 26 | #include <linux/idr.h> |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 27 | #include <linux/cred.h> |
| 28 | #include <linux/timekeeping.h> |
| 29 | #include <linux/ctype.h> |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 30 | #include <linux/btf.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 31 | |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 32 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
| 33 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ |
| 34 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ |
| 35 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 36 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) |
| 37 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) |
| 38 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 39 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
| 40 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 41 | DEFINE_PER_CPU(int, bpf_prog_active); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 42 | static DEFINE_IDR(prog_idr); |
| 43 | static DEFINE_SPINLOCK(prog_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 44 | static DEFINE_IDR(map_idr); |
| 45 | static DEFINE_SPINLOCK(map_idr_lock); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 46 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 47 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
| 48 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 49 | static const struct bpf_map_ops * const bpf_map_types[] = { |
| 50 | #define BPF_PROG_TYPE(_id, _ops) |
| 51 | #define BPF_MAP_TYPE(_id, _ops) \ |
| 52 | [_id] = &_ops, |
| 53 | #include <linux/bpf_types.h> |
| 54 | #undef BPF_PROG_TYPE |
| 55 | #undef BPF_MAP_TYPE |
| 56 | }; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 57 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 58 | /* |
| 59 | * If we're handed a bigger struct than we know of, ensure all the unknown bits |
| 60 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions |
| 61 | * we don't know about yet. |
| 62 | * |
| 63 | * There is a ToCToU between this function call and the following |
| 64 | * copy_from_user() call. However, this is not a concern since this function is |
| 65 | * meant to be a future-proofing of bits. |
| 66 | */ |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 67 | static int check_uarg_tail_zero(void __user *uaddr, |
| 68 | size_t expected_size, |
| 69 | size_t actual_size) |
| 70 | { |
| 71 | unsigned char __user *addr; |
| 72 | unsigned char __user *end; |
| 73 | unsigned char val; |
| 74 | int err; |
| 75 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 76 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
| 77 | return -E2BIG; |
| 78 | |
| 79 | if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size))) |
| 80 | return -EFAULT; |
| 81 | |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 82 | if (actual_size <= expected_size) |
| 83 | return 0; |
| 84 | |
| 85 | addr = uaddr + expected_size; |
| 86 | end = uaddr + actual_size; |
| 87 | |
| 88 | for (; addr < end; addr++) { |
| 89 | err = get_user(val, addr); |
| 90 | if (err) |
| 91 | return err; |
| 92 | if (val) |
| 93 | return -E2BIG; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 99 | const struct bpf_map_ops bpf_map_offload_ops = { |
| 100 | .map_alloc = bpf_map_offload_map_alloc, |
| 101 | .map_free = bpf_map_offload_map_free, |
| 102 | }; |
| 103 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 104 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
| 105 | { |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 106 | const struct bpf_map_ops *ops; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 107 | struct bpf_map *map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 108 | int err; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 109 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 110 | if (attr->map_type >= ARRAY_SIZE(bpf_map_types)) |
| 111 | return ERR_PTR(-EINVAL); |
| 112 | ops = bpf_map_types[attr->map_type]; |
| 113 | if (!ops) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 114 | return ERR_PTR(-EINVAL); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 115 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 116 | if (ops->map_alloc_check) { |
| 117 | err = ops->map_alloc_check(attr); |
| 118 | if (err) |
| 119 | return ERR_PTR(err); |
| 120 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 121 | if (attr->map_ifindex) |
| 122 | ops = &bpf_map_offload_ops; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 123 | map = ops->map_alloc(attr); |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 124 | if (IS_ERR(map)) |
| 125 | return map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 126 | map->ops = ops; |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 127 | map->map_type = attr->map_type; |
| 128 | return map; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 131 | void *bpf_map_area_alloc(size_t size, int numa_node) |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 132 | { |
| 133 | /* We definitely need __GFP_NORETRY, so OOM killer doesn't |
| 134 | * trigger under memory pressure as we really just want to |
| 135 | * fail instead. |
| 136 | */ |
| 137 | const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO; |
| 138 | void *area; |
| 139 | |
| 140 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 141 | area = kmalloc_node(size, GFP_USER | flags, numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 142 | if (area != NULL) |
| 143 | return area; |
| 144 | } |
| 145 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 146 | return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags, |
| 147 | __builtin_return_address(0)); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void bpf_map_area_free(void *area) |
| 151 | { |
| 152 | kvfree(area); |
| 153 | } |
| 154 | |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 155 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
| 156 | { |
| 157 | map->map_type = attr->map_type; |
| 158 | map->key_size = attr->key_size; |
| 159 | map->value_size = attr->value_size; |
| 160 | map->max_entries = attr->max_entries; |
| 161 | map->map_flags = attr->map_flags; |
| 162 | map->numa_node = bpf_map_attr_numa_node(attr); |
| 163 | } |
| 164 | |
Alexei Starovoitov | 6c90598 | 2016-03-07 21:57:15 -0800 | [diff] [blame] | 165 | int bpf_map_precharge_memlock(u32 pages) |
| 166 | { |
| 167 | struct user_struct *user = get_current_user(); |
| 168 | unsigned long memlock_limit, cur; |
| 169 | |
| 170 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 171 | cur = atomic_long_read(&user->locked_vm); |
| 172 | free_uid(user); |
| 173 | if (cur + pages > memlock_limit) |
| 174 | return -EPERM; |
| 175 | return 0; |
| 176 | } |
| 177 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 178 | static int bpf_map_charge_memlock(struct bpf_map *map) |
| 179 | { |
| 180 | struct user_struct *user = get_current_user(); |
| 181 | unsigned long memlock_limit; |
| 182 | |
| 183 | memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 184 | |
| 185 | atomic_long_add(map->pages, &user->locked_vm); |
| 186 | |
| 187 | if (atomic_long_read(&user->locked_vm) > memlock_limit) { |
| 188 | atomic_long_sub(map->pages, &user->locked_vm); |
| 189 | free_uid(user); |
| 190 | return -EPERM; |
| 191 | } |
| 192 | map->user = user; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static void bpf_map_uncharge_memlock(struct bpf_map *map) |
| 197 | { |
| 198 | struct user_struct *user = map->user; |
| 199 | |
| 200 | atomic_long_sub(map->pages, &user->locked_vm); |
| 201 | free_uid(user); |
| 202 | } |
| 203 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 204 | static int bpf_map_alloc_id(struct bpf_map *map) |
| 205 | { |
| 206 | int id; |
| 207 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 208 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 209 | spin_lock_bh(&map_idr_lock); |
| 210 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); |
| 211 | if (id > 0) |
| 212 | map->id = id; |
| 213 | spin_unlock_bh(&map_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 214 | idr_preload_end(); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 215 | |
| 216 | if (WARN_ON_ONCE(!id)) |
| 217 | return -ENOSPC; |
| 218 | |
| 219 | return id > 0 ? 0 : id; |
| 220 | } |
| 221 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 222 | void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock) |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 223 | { |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 224 | unsigned long flags; |
| 225 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 226 | /* Offloaded maps are removed from the IDR store when their device |
| 227 | * disappears - even if someone holds an fd to them they are unusable, |
| 228 | * the memory is gone, all ops will fail; they are simply waiting for |
| 229 | * refcnt to drop to be freed. |
| 230 | */ |
| 231 | if (!map->id) |
| 232 | return; |
| 233 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 234 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 235 | spin_lock_irqsave(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 236 | else |
| 237 | __acquire(&map_idr_lock); |
| 238 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 239 | idr_remove(&map_idr, map->id); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 240 | map->id = 0; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 241 | |
| 242 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 243 | spin_unlock_irqrestore(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 244 | else |
| 245 | __release(&map_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 248 | /* called from workqueue */ |
| 249 | static void bpf_map_free_deferred(struct work_struct *work) |
| 250 | { |
| 251 | struct bpf_map *map = container_of(work, struct bpf_map, work); |
| 252 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 253 | bpf_map_uncharge_memlock(map); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 254 | security_bpf_map_free(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 255 | /* implementation dependent freeing */ |
| 256 | map->ops->map_free(map); |
| 257 | } |
| 258 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 259 | static void bpf_map_put_uref(struct bpf_map *map) |
| 260 | { |
| 261 | if (atomic_dec_and_test(&map->usercnt)) { |
John Fastabend | ba6b8de | 2018-04-23 15:39:23 -0700 | [diff] [blame] | 262 | if (map->ops->map_release_uref) |
| 263 | map->ops->map_release_uref(map); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 267 | /* decrement map refcnt and schedule it for freeing via workqueue |
| 268 | * (unrelying map implementation ops->map_free() might sleep) |
| 269 | */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 270 | static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 271 | { |
| 272 | if (atomic_dec_and_test(&map->refcnt)) { |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 273 | /* bpf_map_free_id() must be called first */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 274 | bpf_map_free_id(map, do_idr_lock); |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 275 | btf_put(map->btf); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 276 | INIT_WORK(&map->work, bpf_map_free_deferred); |
| 277 | schedule_work(&map->work); |
| 278 | } |
| 279 | } |
| 280 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 281 | void bpf_map_put(struct bpf_map *map) |
| 282 | { |
| 283 | __bpf_map_put(map, true); |
| 284 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 285 | EXPORT_SYMBOL_GPL(bpf_map_put); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 286 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 287 | void bpf_map_put_with_uref(struct bpf_map *map) |
| 288 | { |
| 289 | bpf_map_put_uref(map); |
| 290 | bpf_map_put(map); |
| 291 | } |
| 292 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 293 | static int bpf_map_release(struct inode *inode, struct file *filp) |
| 294 | { |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 295 | struct bpf_map *map = filp->private_data; |
| 296 | |
| 297 | if (map->ops->map_release) |
| 298 | map->ops->map_release(map, filp); |
| 299 | |
| 300 | bpf_map_put_with_uref(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 304 | #ifdef CONFIG_PROC_FS |
| 305 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
| 306 | { |
| 307 | const struct bpf_map *map = filp->private_data; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 308 | const struct bpf_array *array; |
| 309 | u32 owner_prog_type = 0; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 310 | u32 owner_jited = 0; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 311 | |
| 312 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 313 | array = container_of(map, struct bpf_array, map); |
| 314 | owner_prog_type = array->owner_prog_type; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 315 | owner_jited = array->owner_jited; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 316 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 317 | |
| 318 | seq_printf(m, |
| 319 | "map_type:\t%u\n" |
| 320 | "key_size:\t%u\n" |
| 321 | "value_size:\t%u\n" |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 322 | "max_entries:\t%u\n" |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 323 | "map_flags:\t%#x\n" |
| 324 | "memlock:\t%llu\n", |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 325 | map->map_type, |
| 326 | map->key_size, |
| 327 | map->value_size, |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 328 | map->max_entries, |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 329 | map->map_flags, |
| 330 | map->pages * 1ULL << PAGE_SHIFT); |
| 331 | |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 332 | if (owner_prog_type) { |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 333 | seq_printf(m, "owner_prog_type:\t%u\n", |
| 334 | owner_prog_type); |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 335 | seq_printf(m, "owner_jited:\t%u\n", |
| 336 | owner_jited); |
| 337 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 338 | } |
| 339 | #endif |
| 340 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 341 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
| 342 | loff_t *ppos) |
| 343 | { |
| 344 | /* We need this handler such that alloc_file() enables |
| 345 | * f_mode with FMODE_CAN_READ. |
| 346 | */ |
| 347 | return -EINVAL; |
| 348 | } |
| 349 | |
| 350 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, |
| 351 | size_t siz, loff_t *ppos) |
| 352 | { |
| 353 | /* We need this handler such that alloc_file() enables |
| 354 | * f_mode with FMODE_CAN_WRITE. |
| 355 | */ |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 359 | const struct file_operations bpf_map_fops = { |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 360 | #ifdef CONFIG_PROC_FS |
| 361 | .show_fdinfo = bpf_map_show_fdinfo, |
| 362 | #endif |
| 363 | .release = bpf_map_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 364 | .read = bpf_dummy_read, |
| 365 | .write = bpf_dummy_write, |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 366 | }; |
| 367 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 368 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 369 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 370 | int ret; |
| 371 | |
| 372 | ret = security_bpf_map(map, OPEN_FMODE(flags)); |
| 373 | if (ret < 0) |
| 374 | return ret; |
| 375 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 376 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 377 | flags | O_CLOEXEC); |
| 378 | } |
| 379 | |
| 380 | int bpf_get_file_flag(int flags) |
| 381 | { |
| 382 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) |
| 383 | return -EINVAL; |
| 384 | if (flags & BPF_F_RDONLY) |
| 385 | return O_RDONLY; |
| 386 | if (flags & BPF_F_WRONLY) |
| 387 | return O_WRONLY; |
| 388 | return O_RDWR; |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 391 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
| 392 | #define CHECK_ATTR(CMD) \ |
| 393 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ |
| 394 | sizeof(attr->CMD##_LAST_FIELD), 0, \ |
| 395 | sizeof(*attr) - \ |
| 396 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ |
| 397 | sizeof(attr->CMD##_LAST_FIELD)) != NULL |
| 398 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 399 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
| 400 | * Return 0 on success and < 0 on error. |
| 401 | */ |
| 402 | static int bpf_obj_name_cpy(char *dst, const char *src) |
| 403 | { |
| 404 | const char *end = src + BPF_OBJ_NAME_LEN; |
| 405 | |
Martin KaFai Lau | 473d973 | 2017-10-05 21:52:11 -0700 | [diff] [blame] | 406 | memset(dst, 0, BPF_OBJ_NAME_LEN); |
| 407 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 408 | /* Copy all isalnum() and '_' char */ |
| 409 | while (src < end && *src) { |
| 410 | if (!isalnum(*src) && *src != '_') |
| 411 | return -EINVAL; |
| 412 | *dst++ = *src++; |
| 413 | } |
| 414 | |
| 415 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ |
| 416 | if (src == end) |
| 417 | return -EINVAL; |
| 418 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 422 | #define BPF_MAP_CREATE_LAST_FIELD btf_value_id |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 423 | /* called via syscall */ |
| 424 | static int map_create(union bpf_attr *attr) |
| 425 | { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 426 | int numa_node = bpf_map_attr_numa_node(attr); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 427 | struct bpf_map *map; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 428 | int f_flags; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 429 | int err; |
| 430 | |
| 431 | err = CHECK_ATTR(BPF_MAP_CREATE); |
| 432 | if (err) |
| 433 | return -EINVAL; |
| 434 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 435 | f_flags = bpf_get_file_flag(attr->map_flags); |
| 436 | if (f_flags < 0) |
| 437 | return f_flags; |
| 438 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 439 | if (numa_node != NUMA_NO_NODE && |
Eric Dumazet | 96e5ae4 | 2017-09-04 22:41:02 -0700 | [diff] [blame] | 440 | ((unsigned int)numa_node >= nr_node_ids || |
| 441 | !node_online(numa_node))) |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 442 | return -EINVAL; |
| 443 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 444 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
| 445 | map = find_and_alloc_map(attr); |
| 446 | if (IS_ERR(map)) |
| 447 | return PTR_ERR(map); |
| 448 | |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 449 | err = bpf_obj_name_cpy(map->name, attr->map_name); |
| 450 | if (err) |
| 451 | goto free_map_nouncharge; |
| 452 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 453 | atomic_set(&map->refcnt, 1); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 454 | atomic_set(&map->usercnt, 1); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 455 | |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 456 | if (bpf_map_support_seq_show(map) && |
| 457 | (attr->btf_key_id || attr->btf_value_id)) { |
| 458 | struct btf *btf; |
| 459 | |
| 460 | if (!attr->btf_key_id || !attr->btf_value_id) { |
| 461 | err = -EINVAL; |
| 462 | goto free_map_nouncharge; |
| 463 | } |
| 464 | |
| 465 | btf = btf_get_by_fd(attr->btf_fd); |
| 466 | if (IS_ERR(btf)) { |
| 467 | err = PTR_ERR(btf); |
| 468 | goto free_map_nouncharge; |
| 469 | } |
| 470 | |
| 471 | err = map->ops->map_check_btf(map, btf, attr->btf_key_id, |
| 472 | attr->btf_value_id); |
| 473 | if (err) { |
| 474 | btf_put(btf); |
| 475 | goto free_map_nouncharge; |
| 476 | } |
| 477 | |
| 478 | map->btf = btf; |
| 479 | map->btf_key_id = attr->btf_key_id; |
| 480 | map->btf_value_id = attr->btf_value_id; |
| 481 | } |
| 482 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 483 | err = security_bpf_map_alloc(map); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 484 | if (err) |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 485 | goto free_map_nouncharge; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 486 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 487 | err = bpf_map_charge_memlock(map); |
| 488 | if (err) |
| 489 | goto free_map_sec; |
| 490 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 491 | err = bpf_map_alloc_id(map); |
| 492 | if (err) |
| 493 | goto free_map; |
| 494 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 495 | err = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 496 | if (err < 0) { |
| 497 | /* failed to allocate fd. |
| 498 | * bpf_map_put() is needed because the above |
| 499 | * bpf_map_alloc_id() has published the map |
| 500 | * to the userspace and the userspace may |
| 501 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. |
| 502 | */ |
| 503 | bpf_map_put(map); |
| 504 | return err; |
| 505 | } |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 506 | |
| 507 | return err; |
| 508 | |
| 509 | free_map: |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 510 | bpf_map_uncharge_memlock(map); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 511 | free_map_sec: |
| 512 | security_bpf_map_free(map); |
Daniel Borkmann | 20b2b24 | 2016-11-04 00:56:31 +0100 | [diff] [blame] | 513 | free_map_nouncharge: |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 514 | btf_put(map->btf); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 515 | map->ops->map_free(map); |
| 516 | return err; |
| 517 | } |
| 518 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 519 | /* if error is returned, fd is released. |
| 520 | * On success caller should complete fd access with matching fdput() |
| 521 | */ |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 522 | struct bpf_map *__bpf_map_get(struct fd f) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 523 | { |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 524 | if (!f.file) |
| 525 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 526 | if (f.file->f_op != &bpf_map_fops) { |
| 527 | fdput(f); |
| 528 | return ERR_PTR(-EINVAL); |
| 529 | } |
| 530 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 531 | return f.file->private_data; |
| 532 | } |
| 533 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 534 | /* prog's and map's refcnt limit */ |
| 535 | #define BPF_MAX_REFCNT 32768 |
| 536 | |
| 537 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 538 | { |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 539 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
| 540 | atomic_dec(&map->refcnt); |
| 541 | return ERR_PTR(-EBUSY); |
| 542 | } |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 543 | if (uref) |
| 544 | atomic_inc(&map->usercnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 545 | return map; |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 546 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 547 | EXPORT_SYMBOL_GPL(bpf_map_inc); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 548 | |
| 549 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 550 | { |
| 551 | struct fd f = fdget(ufd); |
| 552 | struct bpf_map *map; |
| 553 | |
| 554 | map = __bpf_map_get(f); |
| 555 | if (IS_ERR(map)) |
| 556 | return map; |
| 557 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 558 | map = bpf_map_inc(map, true); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 559 | fdput(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 560 | |
| 561 | return map; |
| 562 | } |
| 563 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 564 | /* map_idr_lock should have been held */ |
| 565 | static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, |
| 566 | bool uref) |
| 567 | { |
| 568 | int refold; |
| 569 | |
| 570 | refold = __atomic_add_unless(&map->refcnt, 1, 0); |
| 571 | |
| 572 | if (refold >= BPF_MAX_REFCNT) { |
| 573 | __bpf_map_put(map, false); |
| 574 | return ERR_PTR(-EBUSY); |
| 575 | } |
| 576 | |
| 577 | if (!refold) |
| 578 | return ERR_PTR(-ENOENT); |
| 579 | |
| 580 | if (uref) |
| 581 | atomic_inc(&map->usercnt); |
| 582 | |
| 583 | return map; |
| 584 | } |
| 585 | |
Alexei Starovoitov | b8cdc05 | 2016-03-09 18:56:49 -0800 | [diff] [blame] | 586 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
| 587 | { |
| 588 | return -ENOTSUPP; |
| 589 | } |
| 590 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 591 | /* last field in 'union bpf_attr' used by this command */ |
| 592 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value |
| 593 | |
| 594 | static int map_lookup_elem(union bpf_attr *attr) |
| 595 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 596 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 597 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 598 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 599 | struct bpf_map *map; |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 600 | void *key, *value, *ptr; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 601 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 602 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 603 | int err; |
| 604 | |
| 605 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) |
| 606 | return -EINVAL; |
| 607 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 608 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 609 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 610 | if (IS_ERR(map)) |
| 611 | return PTR_ERR(map); |
| 612 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 613 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
| 614 | err = -EPERM; |
| 615 | goto err_put; |
| 616 | } |
| 617 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 618 | key = memdup_user(ukey, map->key_size); |
| 619 | if (IS_ERR(key)) { |
| 620 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 621 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 622 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 623 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 624 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 625 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 626 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 627 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 628 | else if (IS_FD_MAP(map)) |
| 629 | value_size = sizeof(u32); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 630 | else |
| 631 | value_size = map->value_size; |
| 632 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 633 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 634 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 635 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 636 | goto free_key; |
| 637 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 638 | if (bpf_map_is_dev_bound(map)) { |
| 639 | err = bpf_map_offload_lookup_elem(map, key, value); |
| 640 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 641 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 642 | err = bpf_percpu_hash_copy(map, key, value); |
| 643 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 644 | err = bpf_percpu_array_copy(map, key, value); |
Alexei Starovoitov | 557c0c6 | 2016-03-07 21:57:17 -0800 | [diff] [blame] | 645 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 646 | err = bpf_stackmap_copy(map, key, value); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 647 | } else if (IS_FD_ARRAY(map)) { |
| 648 | err = bpf_fd_array_map_lookup_elem(map, key, value); |
| 649 | } else if (IS_FD_HASH(map)) { |
| 650 | err = bpf_fd_htab_map_lookup_elem(map, key, value); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 651 | } else { |
| 652 | rcu_read_lock(); |
| 653 | ptr = map->ops->map_lookup_elem(map, key); |
| 654 | if (ptr) |
| 655 | memcpy(value, ptr, value_size); |
| 656 | rcu_read_unlock(); |
| 657 | err = ptr ? 0 : -ENOENT; |
| 658 | } |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 659 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 660 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 661 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 662 | |
| 663 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 664 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 665 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 666 | |
| 667 | err = 0; |
| 668 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 669 | free_value: |
| 670 | kfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 671 | free_key: |
| 672 | kfree(key); |
| 673 | err_put: |
| 674 | fdput(f); |
| 675 | return err; |
| 676 | } |
| 677 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 678 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 679 | |
| 680 | static int map_update_elem(union bpf_attr *attr) |
| 681 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 682 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 683 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 684 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 685 | struct bpf_map *map; |
| 686 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 687 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 688 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 689 | int err; |
| 690 | |
| 691 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 692 | return -EINVAL; |
| 693 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 694 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 695 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 696 | if (IS_ERR(map)) |
| 697 | return PTR_ERR(map); |
| 698 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 699 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
| 700 | err = -EPERM; |
| 701 | goto err_put; |
| 702 | } |
| 703 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 704 | key = memdup_user(ukey, map->key_size); |
| 705 | if (IS_ERR(key)) { |
| 706 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 707 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 708 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 709 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 710 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 711 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 712 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) |
| 713 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 714 | else |
| 715 | value_size = map->value_size; |
| 716 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 717 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 718 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 719 | if (!value) |
| 720 | goto free_key; |
| 721 | |
| 722 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 723 | if (copy_from_user(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 724 | goto free_value; |
| 725 | |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 726 | /* Need to create a kthread, thus must support schedule */ |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 727 | if (bpf_map_is_dev_bound(map)) { |
| 728 | err = bpf_map_offload_update_elem(map, key, value, attr->flags); |
| 729 | goto out; |
| 730 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) { |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 731 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 732 | goto out; |
| 733 | } |
| 734 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 735 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
| 736 | * inside bpf map update or delete otherwise deadlocks are possible |
| 737 | */ |
| 738 | preempt_disable(); |
| 739 | __this_cpu_inc(bpf_prog_active); |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 740 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 741 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 742 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
| 743 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 744 | err = bpf_percpu_array_update(map, key, value, attr->flags); |
Mickaël Salaün | 9c147b5 | 2018-01-26 00:54:02 +0100 | [diff] [blame] | 745 | } else if (IS_FD_ARRAY(map)) { |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 746 | rcu_read_lock(); |
| 747 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 748 | attr->flags); |
| 749 | rcu_read_unlock(); |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 750 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 751 | rcu_read_lock(); |
| 752 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, |
| 753 | attr->flags); |
| 754 | rcu_read_unlock(); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 755 | } else { |
| 756 | rcu_read_lock(); |
| 757 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 758 | rcu_read_unlock(); |
| 759 | } |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 760 | __this_cpu_dec(bpf_prog_active); |
| 761 | preempt_enable(); |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 762 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 763 | free_value: |
| 764 | kfree(value); |
| 765 | free_key: |
| 766 | kfree(key); |
| 767 | err_put: |
| 768 | fdput(f); |
| 769 | return err; |
| 770 | } |
| 771 | |
| 772 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 773 | |
| 774 | static int map_delete_elem(union bpf_attr *attr) |
| 775 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 776 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 777 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 778 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 779 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 780 | void *key; |
| 781 | int err; |
| 782 | |
| 783 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 784 | return -EINVAL; |
| 785 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 786 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 787 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 788 | if (IS_ERR(map)) |
| 789 | return PTR_ERR(map); |
| 790 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 791 | if (!(f.file->f_mode & FMODE_CAN_WRITE)) { |
| 792 | err = -EPERM; |
| 793 | goto err_put; |
| 794 | } |
| 795 | |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 796 | key = memdup_user(ukey, map->key_size); |
| 797 | if (IS_ERR(key)) { |
| 798 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 799 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 800 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 801 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 802 | if (bpf_map_is_dev_bound(map)) { |
| 803 | err = bpf_map_offload_delete_elem(map, key); |
| 804 | goto out; |
| 805 | } |
| 806 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 807 | preempt_disable(); |
| 808 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 809 | rcu_read_lock(); |
| 810 | err = map->ops->map_delete_elem(map, key); |
| 811 | rcu_read_unlock(); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 812 | __this_cpu_dec(bpf_prog_active); |
| 813 | preempt_enable(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 814 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 815 | kfree(key); |
| 816 | err_put: |
| 817 | fdput(f); |
| 818 | return err; |
| 819 | } |
| 820 | |
| 821 | /* last field in 'union bpf_attr' used by this command */ |
| 822 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 823 | |
| 824 | static int map_get_next_key(union bpf_attr *attr) |
| 825 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 826 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 827 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 828 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 829 | struct bpf_map *map; |
| 830 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 831 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 832 | int err; |
| 833 | |
| 834 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 835 | return -EINVAL; |
| 836 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 837 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 838 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 839 | if (IS_ERR(map)) |
| 840 | return PTR_ERR(map); |
| 841 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 842 | if (!(f.file->f_mode & FMODE_CAN_READ)) { |
| 843 | err = -EPERM; |
| 844 | goto err_put; |
| 845 | } |
| 846 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 847 | if (ukey) { |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 848 | key = memdup_user(ukey, map->key_size); |
| 849 | if (IS_ERR(key)) { |
| 850 | err = PTR_ERR(key); |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 851 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 852 | } |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 853 | } else { |
| 854 | key = NULL; |
| 855 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 856 | |
| 857 | err = -ENOMEM; |
| 858 | next_key = kmalloc(map->key_size, GFP_USER); |
| 859 | if (!next_key) |
| 860 | goto free_key; |
| 861 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 862 | if (bpf_map_is_dev_bound(map)) { |
| 863 | err = bpf_map_offload_get_next_key(map, key, next_key); |
| 864 | goto out; |
| 865 | } |
| 866 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 867 | rcu_read_lock(); |
| 868 | err = map->ops->map_get_next_key(map, key, next_key); |
| 869 | rcu_read_unlock(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 870 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 871 | if (err) |
| 872 | goto free_next_key; |
| 873 | |
| 874 | err = -EFAULT; |
| 875 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 876 | goto free_next_key; |
| 877 | |
| 878 | err = 0; |
| 879 | |
| 880 | free_next_key: |
| 881 | kfree(next_key); |
| 882 | free_key: |
| 883 | kfree(key); |
| 884 | err_put: |
| 885 | fdput(f); |
| 886 | return err; |
| 887 | } |
| 888 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 889 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
| 890 | #define BPF_PROG_TYPE(_id, _name) \ |
| 891 | [_id] = & _name ## _prog_ops, |
| 892 | #define BPF_MAP_TYPE(_id, _ops) |
| 893 | #include <linux/bpf_types.h> |
| 894 | #undef BPF_PROG_TYPE |
| 895 | #undef BPF_MAP_TYPE |
| 896 | }; |
| 897 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 898 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 899 | { |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 900 | if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type]) |
| 901 | return -EINVAL; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 902 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 903 | if (!bpf_prog_is_dev_bound(prog->aux)) |
| 904 | prog->aux->ops = bpf_prog_types[type]; |
| 905 | else |
| 906 | prog->aux->ops = &bpf_offload_prog_ops; |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 907 | prog->type = type; |
| 908 | return 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /* drop refcnt on maps used by eBPF program and free auxilary data */ |
| 912 | static void free_used_maps(struct bpf_prog_aux *aux) |
| 913 | { |
| 914 | int i; |
| 915 | |
| 916 | for (i = 0; i < aux->used_map_cnt; i++) |
| 917 | bpf_map_put(aux->used_maps[i]); |
| 918 | |
| 919 | kfree(aux->used_maps); |
| 920 | } |
| 921 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 922 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
| 923 | { |
| 924 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 925 | unsigned long user_bufs; |
| 926 | |
| 927 | if (user) { |
| 928 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); |
| 929 | if (user_bufs > memlock_limit) { |
| 930 | atomic_long_sub(pages, &user->locked_vm); |
| 931 | return -EPERM; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) |
| 939 | { |
| 940 | if (user) |
| 941 | atomic_long_sub(pages, &user->locked_vm); |
| 942 | } |
| 943 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 944 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
| 945 | { |
| 946 | struct user_struct *user = get_current_user(); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 947 | int ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 948 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 949 | ret = __bpf_prog_charge(user, prog->pages); |
| 950 | if (ret) { |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 951 | free_uid(user); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 952 | return ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 953 | } |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 954 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 955 | prog->aux->user = user; |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) |
| 960 | { |
| 961 | struct user_struct *user = prog->aux->user; |
| 962 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 963 | __bpf_prog_uncharge(user, prog->pages); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 964 | free_uid(user); |
| 965 | } |
| 966 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 967 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
| 968 | { |
| 969 | int id; |
| 970 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 971 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 972 | spin_lock_bh(&prog_idr_lock); |
| 973 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); |
| 974 | if (id > 0) |
| 975 | prog->aux->id = id; |
| 976 | spin_unlock_bh(&prog_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 977 | idr_preload_end(); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 978 | |
| 979 | /* id is in [1, INT_MAX) */ |
| 980 | if (WARN_ON_ONCE(!id)) |
| 981 | return -ENOSPC; |
| 982 | |
| 983 | return id > 0 ? 0 : id; |
| 984 | } |
| 985 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 986 | void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock) |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 987 | { |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 988 | /* cBPF to eBPF migrations are currently not in the idr store. |
| 989 | * Offloaded programs are removed from the store when their device |
| 990 | * disappears - even if someone grabs an fd to them they are unusable, |
| 991 | * simply waiting for refcnt to drop to be freed. |
| 992 | */ |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 993 | if (!prog->aux->id) |
| 994 | return; |
| 995 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 996 | if (do_idr_lock) |
| 997 | spin_lock_bh(&prog_idr_lock); |
| 998 | else |
| 999 | __acquire(&prog_idr_lock); |
| 1000 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1001 | idr_remove(&prog_idr, prog->aux->id); |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1002 | prog->aux->id = 0; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1003 | |
| 1004 | if (do_idr_lock) |
| 1005 | spin_unlock_bh(&prog_idr_lock); |
| 1006 | else |
| 1007 | __release(&prog_idr_lock); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1010 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1011 | { |
| 1012 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 1013 | |
| 1014 | free_used_maps(aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1015 | bpf_prog_uncharge_memlock(aux->prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1016 | security_bpf_prog_free(aux); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1017 | bpf_prog_free(aux->prog); |
| 1018 | } |
| 1019 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1020 | static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1021 | { |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1022 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 1023 | int i; |
| 1024 | |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1025 | /* bpf_prog_free_id() must be called first */ |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1026 | bpf_prog_free_id(prog, do_idr_lock); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 1027 | |
| 1028 | for (i = 0; i < prog->aux->func_cnt; i++) |
| 1029 | bpf_prog_kallsyms_del(prog->aux->func[i]); |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 1030 | bpf_prog_kallsyms_del(prog); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 1031 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1032 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1033 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1034 | } |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1035 | |
| 1036 | void bpf_prog_put(struct bpf_prog *prog) |
| 1037 | { |
| 1038 | __bpf_prog_put(prog, true); |
| 1039 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 1040 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1041 | |
| 1042 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 1043 | { |
| 1044 | struct bpf_prog *prog = filp->private_data; |
| 1045 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1046 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1047 | return 0; |
| 1048 | } |
| 1049 | |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1050 | #ifdef CONFIG_PROC_FS |
| 1051 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) |
| 1052 | { |
| 1053 | const struct bpf_prog *prog = filp->private_data; |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1054 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1055 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1056 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1057 | seq_printf(m, |
| 1058 | "prog_type:\t%u\n" |
| 1059 | "prog_jited:\t%u\n" |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1060 | "prog_tag:\t%s\n" |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1061 | "memlock:\t%llu\n", |
| 1062 | prog->type, |
| 1063 | prog->jited, |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1064 | prog_tag, |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1065 | prog->pages * 1ULL << PAGE_SHIFT); |
| 1066 | } |
| 1067 | #endif |
| 1068 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1069 | const struct file_operations bpf_prog_fops = { |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1070 | #ifdef CONFIG_PROC_FS |
| 1071 | .show_fdinfo = bpf_prog_show_fdinfo, |
| 1072 | #endif |
| 1073 | .release = bpf_prog_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1074 | .read = bpf_dummy_read, |
| 1075 | .write = bpf_dummy_write, |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1076 | }; |
| 1077 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1078 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1079 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1080 | int ret; |
| 1081 | |
| 1082 | ret = security_bpf_prog(prog); |
| 1083 | if (ret < 0) |
| 1084 | return ret; |
| 1085 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1086 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 1087 | O_RDWR | O_CLOEXEC); |
| 1088 | } |
| 1089 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1090 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1091 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1092 | if (!f.file) |
| 1093 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1094 | if (f.file->f_op != &bpf_prog_fops) { |
| 1095 | fdput(f); |
| 1096 | return ERR_PTR(-EINVAL); |
| 1097 | } |
| 1098 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1099 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1102 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1103 | { |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1104 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
| 1105 | atomic_sub(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1106 | return ERR_PTR(-EBUSY); |
| 1107 | } |
| 1108 | return prog; |
| 1109 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1110 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 1111 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1112 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 1113 | { |
| 1114 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 1115 | * error path. We still know that another entity in our call |
| 1116 | * path holds a reference to the program, thus atomic_sub() can |
| 1117 | * be safely used in such cases! |
| 1118 | */ |
| 1119 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); |
| 1120 | } |
| 1121 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 1122 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1123 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
| 1124 | { |
| 1125 | return bpf_prog_add(prog, 1); |
| 1126 | } |
Daniel Borkmann | 97bc402 | 2016-11-19 01:45:00 +0100 | [diff] [blame] | 1127 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1128 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1129 | /* prog_idr_lock should have been held */ |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1130 | struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog) |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1131 | { |
| 1132 | int refold; |
| 1133 | |
| 1134 | refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0); |
| 1135 | |
| 1136 | if (refold >= BPF_MAX_REFCNT) { |
| 1137 | __bpf_prog_put(prog, false); |
| 1138 | return ERR_PTR(-EBUSY); |
| 1139 | } |
| 1140 | |
| 1141 | if (!refold) |
| 1142 | return ERR_PTR(-ENOENT); |
| 1143 | |
| 1144 | return prog; |
| 1145 | } |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1146 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1147 | |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1148 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1149 | enum bpf_prog_type *attach_type, bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1150 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1151 | /* not an attachment, just a refcount inc, always allow */ |
| 1152 | if (!attach_type) |
| 1153 | return true; |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1154 | |
| 1155 | if (prog->type != *attach_type) |
| 1156 | return false; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1157 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1158 | return false; |
| 1159 | |
| 1160 | return true; |
| 1161 | } |
| 1162 | |
| 1163 | static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1164 | bool attach_drv) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1165 | { |
| 1166 | struct fd f = fdget(ufd); |
| 1167 | struct bpf_prog *prog; |
| 1168 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1169 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1170 | if (IS_ERR(prog)) |
| 1171 | return prog; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1172 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1173 | prog = ERR_PTR(-EINVAL); |
| 1174 | goto out; |
| 1175 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1176 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1177 | prog = bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1178 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1179 | fdput(f); |
| 1180 | return prog; |
| 1181 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1182 | |
| 1183 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 1184 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1185 | return __bpf_prog_get(ufd, NULL, false); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1188 | struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1189 | bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1190 | { |
Alexei Starovoitov | 4d220ed | 2018-04-28 19:56:37 -0700 | [diff] [blame] | 1191 | return __bpf_prog_get(ufd, &type, attach_drv); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1192 | } |
Jakub Kicinski | 6c8dfe2 | 2017-11-03 13:56:21 -0700 | [diff] [blame] | 1193 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1194 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1195 | /* Initially all BPF programs could be loaded w/o specifying |
| 1196 | * expected_attach_type. Later for some of them specifying expected_attach_type |
| 1197 | * at load time became required so that program could be validated properly. |
| 1198 | * Programs of types that are allowed to be loaded both w/ and w/o (for |
| 1199 | * backward compatibility) expected_attach_type, should have the default attach |
| 1200 | * type assigned to expected_attach_type for the latter case, so that it can be |
| 1201 | * validated later at attach time. |
| 1202 | * |
| 1203 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if |
| 1204 | * prog type requires it but has some attach types that have to be backward |
| 1205 | * compatible. |
| 1206 | */ |
| 1207 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) |
| 1208 | { |
| 1209 | switch (attr->prog_type) { |
| 1210 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1211 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't |
| 1212 | * exist so checking for non-zero is the way to go here. |
| 1213 | */ |
| 1214 | if (!attr->expected_attach_type) |
| 1215 | attr->expected_attach_type = |
| 1216 | BPF_CGROUP_INET_SOCK_CREATE; |
| 1217 | break; |
| 1218 | } |
| 1219 | } |
| 1220 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1221 | static int |
| 1222 | bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, |
| 1223 | enum bpf_attach_type expected_attach_type) |
| 1224 | { |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1225 | switch (prog_type) { |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1226 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1227 | switch (expected_attach_type) { |
| 1228 | case BPF_CGROUP_INET_SOCK_CREATE: |
| 1229 | case BPF_CGROUP_INET4_POST_BIND: |
| 1230 | case BPF_CGROUP_INET6_POST_BIND: |
| 1231 | return 0; |
| 1232 | default: |
| 1233 | return -EINVAL; |
| 1234 | } |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1235 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 1236 | switch (expected_attach_type) { |
| 1237 | case BPF_CGROUP_INET4_BIND: |
| 1238 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1239 | case BPF_CGROUP_INET4_CONNECT: |
| 1240 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1241 | return 0; |
| 1242 | default: |
| 1243 | return -EINVAL; |
| 1244 | } |
| 1245 | default: |
| 1246 | return 0; |
| 1247 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1250 | /* last field in 'union bpf_attr' used by this command */ |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1251 | #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1252 | |
| 1253 | static int bpf_prog_load(union bpf_attr *attr) |
| 1254 | { |
| 1255 | enum bpf_prog_type type = attr->prog_type; |
| 1256 | struct bpf_prog *prog; |
| 1257 | int err; |
| 1258 | char license[128]; |
| 1259 | bool is_gpl; |
| 1260 | |
| 1261 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 1262 | return -EINVAL; |
| 1263 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1264 | if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT) |
| 1265 | return -EINVAL; |
| 1266 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1267 | /* copy eBPF program license from user space */ |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1268 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1269 | sizeof(license) - 1) < 0) |
| 1270 | return -EFAULT; |
| 1271 | license[sizeof(license) - 1] = 0; |
| 1272 | |
| 1273 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 1274 | is_gpl = license_is_gpl_compatible(license); |
| 1275 | |
Daniel Borkmann | ef0915c | 2016-12-07 01:15:44 +0100 | [diff] [blame] | 1276 | if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS) |
| 1277 | return -E2BIG; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1278 | |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 1279 | if (type == BPF_PROG_TYPE_KPROBE && |
| 1280 | attr->kern_version != LINUX_VERSION_CODE) |
| 1281 | return -EINVAL; |
| 1282 | |
Chenbo Feng | 80b7d81 | 2017-05-31 18:16:00 -0700 | [diff] [blame] | 1283 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
| 1284 | type != BPF_PROG_TYPE_CGROUP_SKB && |
| 1285 | !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1286 | return -EPERM; |
| 1287 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1288 | bpf_prog_load_fixup_attach_type(attr); |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1289 | if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) |
| 1290 | return -EINVAL; |
| 1291 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1292 | /* plain bpf_prog allocation */ |
| 1293 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
| 1294 | if (!prog) |
| 1295 | return -ENOMEM; |
| 1296 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1297 | prog->expected_attach_type = attr->expected_attach_type; |
| 1298 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1299 | prog->aux->offload_requested = !!attr->prog_ifindex; |
| 1300 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1301 | err = security_bpf_prog_alloc(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1302 | if (err) |
| 1303 | goto free_prog_nouncharge; |
| 1304 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1305 | err = bpf_prog_charge_memlock(prog); |
| 1306 | if (err) |
| 1307 | goto free_prog_sec; |
| 1308 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1309 | prog->len = attr->insn_cnt; |
| 1310 | |
| 1311 | err = -EFAULT; |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1312 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 1313 | bpf_prog_insn_size(prog)) != 0) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1314 | goto free_prog; |
| 1315 | |
| 1316 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1317 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1318 | |
| 1319 | atomic_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1320 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1321 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1322 | if (bpf_prog_is_dev_bound(prog->aux)) { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1323 | err = bpf_prog_offload_init(prog, attr); |
| 1324 | if (err) |
| 1325 | goto free_prog; |
| 1326 | } |
| 1327 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1328 | /* find program type: socket_filter vs tracing_filter */ |
| 1329 | err = find_prog_type(type, prog); |
| 1330 | if (err < 0) |
| 1331 | goto free_prog; |
| 1332 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1333 | prog->aux->load_time = ktime_get_boot_ns(); |
| 1334 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); |
| 1335 | if (err) |
| 1336 | goto free_prog; |
| 1337 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1338 | /* run eBPF verifier */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1339 | err = bpf_check(&prog, attr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1340 | if (err < 0) |
| 1341 | goto free_used_maps; |
| 1342 | |
| 1343 | /* eBPF program is ready to be JITed */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 1344 | if (!prog->bpf_func) |
| 1345 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1346 | if (err < 0) |
| 1347 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1348 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1349 | err = bpf_prog_alloc_id(prog); |
| 1350 | if (err) |
| 1351 | goto free_used_maps; |
| 1352 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1353 | err = bpf_prog_new_fd(prog); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1354 | if (err < 0) { |
| 1355 | /* failed to allocate fd. |
| 1356 | * bpf_prog_put() is needed because the above |
| 1357 | * bpf_prog_alloc_id() has published the prog |
| 1358 | * to the userspace and the userspace may |
| 1359 | * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. |
| 1360 | */ |
| 1361 | bpf_prog_put(prog); |
| 1362 | return err; |
| 1363 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1364 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 1365 | bpf_prog_kallsyms_add(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1366 | return err; |
| 1367 | |
| 1368 | free_used_maps: |
| 1369 | free_used_maps(prog->aux); |
| 1370 | free_prog: |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1371 | bpf_prog_uncharge_memlock(prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1372 | free_prog_sec: |
| 1373 | security_bpf_prog_free(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1374 | free_prog_nouncharge: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1375 | bpf_prog_free(prog); |
| 1376 | return err; |
| 1377 | } |
| 1378 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1379 | #define BPF_OBJ_LAST_FIELD file_flags |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1380 | |
| 1381 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 1382 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1383 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1384 | return -EINVAL; |
| 1385 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1386 | return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname)); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | static int bpf_obj_get(const union bpf_attr *attr) |
| 1390 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1391 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
| 1392 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1393 | return -EINVAL; |
| 1394 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1395 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
| 1396 | attr->file_flags); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1397 | } |
| 1398 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1399 | struct bpf_raw_tracepoint { |
| 1400 | struct bpf_raw_event_map *btp; |
| 1401 | struct bpf_prog *prog; |
| 1402 | }; |
| 1403 | |
| 1404 | static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) |
| 1405 | { |
| 1406 | struct bpf_raw_tracepoint *raw_tp = filp->private_data; |
| 1407 | |
| 1408 | if (raw_tp->prog) { |
| 1409 | bpf_probe_unregister(raw_tp->btp, raw_tp->prog); |
| 1410 | bpf_prog_put(raw_tp->prog); |
| 1411 | } |
| 1412 | kfree(raw_tp); |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | static const struct file_operations bpf_raw_tp_fops = { |
| 1417 | .release = bpf_raw_tracepoint_release, |
| 1418 | .read = bpf_dummy_read, |
| 1419 | .write = bpf_dummy_write, |
| 1420 | }; |
| 1421 | |
| 1422 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd |
| 1423 | |
| 1424 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) |
| 1425 | { |
| 1426 | struct bpf_raw_tracepoint *raw_tp; |
| 1427 | struct bpf_raw_event_map *btp; |
| 1428 | struct bpf_prog *prog; |
| 1429 | char tp_name[128]; |
| 1430 | int tp_fd, err; |
| 1431 | |
| 1432 | if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), |
| 1433 | sizeof(tp_name) - 1) < 0) |
| 1434 | return -EFAULT; |
| 1435 | tp_name[sizeof(tp_name) - 1] = 0; |
| 1436 | |
| 1437 | btp = bpf_find_raw_tracepoint(tp_name); |
| 1438 | if (!btp) |
| 1439 | return -ENOENT; |
| 1440 | |
| 1441 | raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); |
| 1442 | if (!raw_tp) |
| 1443 | return -ENOMEM; |
| 1444 | raw_tp->btp = btp; |
| 1445 | |
| 1446 | prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd, |
| 1447 | BPF_PROG_TYPE_RAW_TRACEPOINT); |
| 1448 | if (IS_ERR(prog)) { |
| 1449 | err = PTR_ERR(prog); |
| 1450 | goto out_free_tp; |
| 1451 | } |
| 1452 | |
| 1453 | err = bpf_probe_register(raw_tp->btp, prog); |
| 1454 | if (err) |
| 1455 | goto out_put_prog; |
| 1456 | |
| 1457 | raw_tp->prog = prog; |
| 1458 | tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, |
| 1459 | O_CLOEXEC); |
| 1460 | if (tp_fd < 0) { |
| 1461 | bpf_probe_unregister(raw_tp->btp, prog); |
| 1462 | err = tp_fd; |
| 1463 | goto out_put_prog; |
| 1464 | } |
| 1465 | return tp_fd; |
| 1466 | |
| 1467 | out_put_prog: |
| 1468 | bpf_prog_put(prog); |
| 1469 | out_free_tp: |
| 1470 | kfree(raw_tp); |
| 1471 | return err; |
| 1472 | } |
| 1473 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1474 | #ifdef CONFIG_CGROUP_BPF |
| 1475 | |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 1476 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
| 1477 | enum bpf_attach_type attach_type) |
| 1478 | { |
| 1479 | switch (prog->type) { |
| 1480 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1481 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 1482 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; |
| 1483 | default: |
| 1484 | return 0; |
| 1485 | } |
| 1486 | } |
| 1487 | |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1488 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1489 | |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1490 | static int sockmap_get_from_fd(const union bpf_attr *attr, |
| 1491 | int type, bool attach) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1492 | { |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1493 | struct bpf_prog *prog = NULL; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1494 | int ufd = attr->target_fd; |
| 1495 | struct bpf_map *map; |
| 1496 | struct fd f; |
| 1497 | int err; |
| 1498 | |
| 1499 | f = fdget(ufd); |
| 1500 | map = __bpf_map_get(f); |
| 1501 | if (IS_ERR(map)) |
| 1502 | return PTR_ERR(map); |
| 1503 | |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1504 | if (attach) { |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1505 | prog = bpf_prog_get_type(attr->attach_bpf_fd, type); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1506 | if (IS_ERR(prog)) { |
| 1507 | fdput(f); |
| 1508 | return PTR_ERR(prog); |
| 1509 | } |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1512 | err = sock_map_prog(map, prog, attr->attach_type); |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1513 | if (err) { |
| 1514 | fdput(f); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1515 | if (prog) |
| 1516 | bpf_prog_put(prog); |
Dan Carpenter | ae2b27b | 2017-08-18 10:27:02 +0300 | [diff] [blame] | 1517 | return err; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | fdput(f); |
Dan Carpenter | ae2b27b | 2017-08-18 10:27:02 +0300 | [diff] [blame] | 1521 | return 0; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1522 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1523 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1524 | #define BPF_F_ATTACH_MASK \ |
| 1525 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) |
| 1526 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1527 | static int bpf_prog_attach(const union bpf_attr *attr) |
| 1528 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1529 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1530 | struct bpf_prog *prog; |
| 1531 | struct cgroup *cgrp; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1532 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1533 | |
| 1534 | if (!capable(CAP_NET_ADMIN)) |
| 1535 | return -EPERM; |
| 1536 | |
| 1537 | if (CHECK_ATTR(BPF_PROG_ATTACH)) |
| 1538 | return -EINVAL; |
| 1539 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1540 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1541 | return -EINVAL; |
| 1542 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1543 | switch (attr->attach_type) { |
| 1544 | case BPF_CGROUP_INET_INGRESS: |
| 1545 | case BPF_CGROUP_INET_EGRESS: |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1546 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1547 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1548 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1549 | case BPF_CGROUP_INET4_POST_BIND: |
| 1550 | case BPF_CGROUP_INET6_POST_BIND: |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1551 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 1552 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1553 | case BPF_CGROUP_INET4_BIND: |
| 1554 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1555 | case BPF_CGROUP_INET4_CONNECT: |
| 1556 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1557 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 1558 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1559 | case BPF_CGROUP_SOCK_OPS: |
| 1560 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
| 1561 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1562 | case BPF_CGROUP_DEVICE: |
| 1563 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 1564 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1565 | case BPF_SK_MSG_VERDICT: |
| 1566 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true); |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1567 | case BPF_SK_SKB_STREAM_PARSER: |
| 1568 | case BPF_SK_SKB_STREAM_VERDICT: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1569 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1570 | default: |
| 1571 | return -EINVAL; |
| 1572 | } |
| 1573 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1574 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 1575 | if (IS_ERR(prog)) |
| 1576 | return PTR_ERR(prog); |
| 1577 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1578 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
| 1579 | bpf_prog_put(prog); |
| 1580 | return -EINVAL; |
| 1581 | } |
| 1582 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1583 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 1584 | if (IS_ERR(cgrp)) { |
| 1585 | bpf_prog_put(prog); |
| 1586 | return PTR_ERR(cgrp); |
| 1587 | } |
| 1588 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1589 | ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type, |
| 1590 | attr->attach_flags); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1591 | if (ret) |
| 1592 | bpf_prog_put(prog); |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1593 | cgroup_put(cgrp); |
| 1594 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1595 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | #define BPF_PROG_DETACH_LAST_FIELD attach_type |
| 1599 | |
| 1600 | static int bpf_prog_detach(const union bpf_attr *attr) |
| 1601 | { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1602 | enum bpf_prog_type ptype; |
| 1603 | struct bpf_prog *prog; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1604 | struct cgroup *cgrp; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1605 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1606 | |
| 1607 | if (!capable(CAP_NET_ADMIN)) |
| 1608 | return -EPERM; |
| 1609 | |
| 1610 | if (CHECK_ATTR(BPF_PROG_DETACH)) |
| 1611 | return -EINVAL; |
| 1612 | |
| 1613 | switch (attr->attach_type) { |
| 1614 | case BPF_CGROUP_INET_INGRESS: |
| 1615 | case BPF_CGROUP_INET_EGRESS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1616 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
| 1617 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1618 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1619 | case BPF_CGROUP_INET4_POST_BIND: |
| 1620 | case BPF_CGROUP_INET6_POST_BIND: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1621 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 1622 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1623 | case BPF_CGROUP_INET4_BIND: |
| 1624 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1625 | case BPF_CGROUP_INET4_CONNECT: |
| 1626 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1627 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 1628 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1629 | case BPF_CGROUP_SOCK_OPS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1630 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1631 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1632 | case BPF_CGROUP_DEVICE: |
| 1633 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 1634 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1635 | case BPF_SK_MSG_VERDICT: |
| 1636 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 1637 | case BPF_SK_SKB_STREAM_PARSER: |
| 1638 | case BPF_SK_SKB_STREAM_VERDICT: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1639 | return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1640 | default: |
| 1641 | return -EINVAL; |
| 1642 | } |
| 1643 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1644 | cgrp = cgroup_get_from_fd(attr->target_fd); |
| 1645 | if (IS_ERR(cgrp)) |
| 1646 | return PTR_ERR(cgrp); |
| 1647 | |
| 1648 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 1649 | if (IS_ERR(prog)) |
| 1650 | prog = NULL; |
| 1651 | |
| 1652 | ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0); |
| 1653 | if (prog) |
| 1654 | bpf_prog_put(prog); |
| 1655 | cgroup_put(cgrp); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1656 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1657 | } |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1658 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1659 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
| 1660 | |
| 1661 | static int bpf_prog_query(const union bpf_attr *attr, |
| 1662 | union bpf_attr __user *uattr) |
| 1663 | { |
| 1664 | struct cgroup *cgrp; |
| 1665 | int ret; |
| 1666 | |
| 1667 | if (!capable(CAP_NET_ADMIN)) |
| 1668 | return -EPERM; |
| 1669 | if (CHECK_ATTR(BPF_PROG_QUERY)) |
| 1670 | return -EINVAL; |
| 1671 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) |
| 1672 | return -EINVAL; |
| 1673 | |
| 1674 | switch (attr->query.attach_type) { |
| 1675 | case BPF_CGROUP_INET_INGRESS: |
| 1676 | case BPF_CGROUP_INET_EGRESS: |
| 1677 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1678 | case BPF_CGROUP_INET4_BIND: |
| 1679 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1680 | case BPF_CGROUP_INET4_POST_BIND: |
| 1681 | case BPF_CGROUP_INET6_POST_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1682 | case BPF_CGROUP_INET4_CONNECT: |
| 1683 | case BPF_CGROUP_INET6_CONNECT: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1684 | case BPF_CGROUP_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1685 | case BPF_CGROUP_DEVICE: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 1686 | break; |
| 1687 | default: |
| 1688 | return -EINVAL; |
| 1689 | } |
| 1690 | cgrp = cgroup_get_from_fd(attr->query.target_fd); |
| 1691 | if (IS_ERR(cgrp)) |
| 1692 | return PTR_ERR(cgrp); |
| 1693 | ret = cgroup_bpf_query(cgrp, attr, uattr); |
| 1694 | cgroup_put(cgrp); |
| 1695 | return ret; |
| 1696 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1697 | #endif /* CONFIG_CGROUP_BPF */ |
| 1698 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 1699 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration |
| 1700 | |
| 1701 | static int bpf_prog_test_run(const union bpf_attr *attr, |
| 1702 | union bpf_attr __user *uattr) |
| 1703 | { |
| 1704 | struct bpf_prog *prog; |
| 1705 | int ret = -ENOTSUPP; |
| 1706 | |
Alexei Starovoitov | 61f3c96 | 2018-01-17 16:52:02 -0800 | [diff] [blame] | 1707 | if (!capable(CAP_SYS_ADMIN)) |
| 1708 | return -EPERM; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 1709 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
| 1710 | return -EINVAL; |
| 1711 | |
| 1712 | prog = bpf_prog_get(attr->test.prog_fd); |
| 1713 | if (IS_ERR(prog)) |
| 1714 | return PTR_ERR(prog); |
| 1715 | |
| 1716 | if (prog->aux->ops->test_run) |
| 1717 | ret = prog->aux->ops->test_run(prog, attr, uattr); |
| 1718 | |
| 1719 | bpf_prog_put(prog); |
| 1720 | return ret; |
| 1721 | } |
| 1722 | |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1723 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
| 1724 | |
| 1725 | static int bpf_obj_get_next_id(const union bpf_attr *attr, |
| 1726 | union bpf_attr __user *uattr, |
| 1727 | struct idr *idr, |
| 1728 | spinlock_t *lock) |
| 1729 | { |
| 1730 | u32 next_id = attr->start_id; |
| 1731 | int err = 0; |
| 1732 | |
| 1733 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) |
| 1734 | return -EINVAL; |
| 1735 | |
| 1736 | if (!capable(CAP_SYS_ADMIN)) |
| 1737 | return -EPERM; |
| 1738 | |
| 1739 | next_id++; |
| 1740 | spin_lock_bh(lock); |
| 1741 | if (!idr_get_next(idr, &next_id)) |
| 1742 | err = -ENOENT; |
| 1743 | spin_unlock_bh(lock); |
| 1744 | |
| 1745 | if (!err) |
| 1746 | err = put_user(next_id, &uattr->next_id); |
| 1747 | |
| 1748 | return err; |
| 1749 | } |
| 1750 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1751 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
| 1752 | |
| 1753 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) |
| 1754 | { |
| 1755 | struct bpf_prog *prog; |
| 1756 | u32 id = attr->prog_id; |
| 1757 | int fd; |
| 1758 | |
| 1759 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) |
| 1760 | return -EINVAL; |
| 1761 | |
| 1762 | if (!capable(CAP_SYS_ADMIN)) |
| 1763 | return -EPERM; |
| 1764 | |
| 1765 | spin_lock_bh(&prog_idr_lock); |
| 1766 | prog = idr_find(&prog_idr, id); |
| 1767 | if (prog) |
| 1768 | prog = bpf_prog_inc_not_zero(prog); |
| 1769 | else |
| 1770 | prog = ERR_PTR(-ENOENT); |
| 1771 | spin_unlock_bh(&prog_idr_lock); |
| 1772 | |
| 1773 | if (IS_ERR(prog)) |
| 1774 | return PTR_ERR(prog); |
| 1775 | |
| 1776 | fd = bpf_prog_new_fd(prog); |
| 1777 | if (fd < 0) |
| 1778 | bpf_prog_put(prog); |
| 1779 | |
| 1780 | return fd; |
| 1781 | } |
| 1782 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1783 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1784 | |
| 1785 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) |
| 1786 | { |
| 1787 | struct bpf_map *map; |
| 1788 | u32 id = attr->map_id; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1789 | int f_flags; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1790 | int fd; |
| 1791 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1792 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
| 1793 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1794 | return -EINVAL; |
| 1795 | |
| 1796 | if (!capable(CAP_SYS_ADMIN)) |
| 1797 | return -EPERM; |
| 1798 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1799 | f_flags = bpf_get_file_flag(attr->open_flags); |
| 1800 | if (f_flags < 0) |
| 1801 | return f_flags; |
| 1802 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1803 | spin_lock_bh(&map_idr_lock); |
| 1804 | map = idr_find(&map_idr, id); |
| 1805 | if (map) |
| 1806 | map = bpf_map_inc_not_zero(map, true); |
| 1807 | else |
| 1808 | map = ERR_PTR(-ENOENT); |
| 1809 | spin_unlock_bh(&map_idr_lock); |
| 1810 | |
| 1811 | if (IS_ERR(map)) |
| 1812 | return PTR_ERR(map); |
| 1813 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1814 | fd = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 1815 | if (fd < 0) |
| 1816 | bpf_map_put(map); |
| 1817 | |
| 1818 | return fd; |
| 1819 | } |
| 1820 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 1821 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
| 1822 | unsigned long addr) |
| 1823 | { |
| 1824 | int i; |
| 1825 | |
| 1826 | for (i = 0; i < prog->aux->used_map_cnt; i++) |
| 1827 | if (prog->aux->used_maps[i] == (void *)addr) |
| 1828 | return prog->aux->used_maps[i]; |
| 1829 | return NULL; |
| 1830 | } |
| 1831 | |
| 1832 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) |
| 1833 | { |
| 1834 | const struct bpf_map *map; |
| 1835 | struct bpf_insn *insns; |
| 1836 | u64 imm; |
| 1837 | int i; |
| 1838 | |
| 1839 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), |
| 1840 | GFP_USER); |
| 1841 | if (!insns) |
| 1842 | return insns; |
| 1843 | |
| 1844 | for (i = 0; i < prog->len; i++) { |
| 1845 | if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { |
| 1846 | insns[i].code = BPF_JMP | BPF_CALL; |
| 1847 | insns[i].imm = BPF_FUNC_tail_call; |
| 1848 | /* fall-through */ |
| 1849 | } |
| 1850 | if (insns[i].code == (BPF_JMP | BPF_CALL) || |
| 1851 | insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { |
| 1852 | if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) |
| 1853 | insns[i].code = BPF_JMP | BPF_CALL; |
| 1854 | if (!bpf_dump_raw_ok()) |
| 1855 | insns[i].imm = 0; |
| 1856 | continue; |
| 1857 | } |
| 1858 | |
| 1859 | if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) |
| 1860 | continue; |
| 1861 | |
| 1862 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; |
| 1863 | map = bpf_map_from_imm(prog, imm); |
| 1864 | if (map) { |
| 1865 | insns[i].src_reg = BPF_PSEUDO_MAP_FD; |
| 1866 | insns[i].imm = map->id; |
| 1867 | insns[i + 1].imm = 0; |
| 1868 | continue; |
| 1869 | } |
| 1870 | |
| 1871 | if (!bpf_dump_raw_ok() && |
| 1872 | imm == (unsigned long)prog->aux) { |
| 1873 | insns[i].imm = 0; |
| 1874 | insns[i + 1].imm = 0; |
| 1875 | continue; |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | return insns; |
| 1880 | } |
| 1881 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1882 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
| 1883 | const union bpf_attr *attr, |
| 1884 | union bpf_attr __user *uattr) |
| 1885 | { |
| 1886 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 1887 | struct bpf_prog_info info = {}; |
| 1888 | u32 info_len = attr->info.info_len; |
| 1889 | char __user *uinsns; |
| 1890 | u32 ulen; |
| 1891 | int err; |
| 1892 | |
| 1893 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
| 1894 | if (err) |
| 1895 | return err; |
| 1896 | info_len = min_t(u32, sizeof(info), info_len); |
| 1897 | |
| 1898 | if (copy_from_user(&info, uinfo, info_len)) |
Daniel Borkmann | 89b0968 | 2017-07-27 21:02:46 +0200 | [diff] [blame] | 1899 | return -EFAULT; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1900 | |
| 1901 | info.type = prog->type; |
| 1902 | info.id = prog->aux->id; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1903 | info.load_time = prog->aux->load_time; |
| 1904 | info.created_by_uid = from_kuid_munged(current_user_ns(), |
| 1905 | prog->aux->user->uid); |
Jiri Olsa | b85fab0 | 2018-04-25 19:41:06 +0200 | [diff] [blame] | 1906 | info.gpl_compatible = prog->gpl_compatible; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1907 | |
| 1908 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1909 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
| 1910 | |
| 1911 | ulen = info.nr_map_ids; |
| 1912 | info.nr_map_ids = prog->aux->used_map_cnt; |
| 1913 | ulen = min_t(u32, info.nr_map_ids, ulen); |
| 1914 | if (ulen) { |
Martin KaFai Lau | 721e08d | 2017-09-29 10:52:17 -0700 | [diff] [blame] | 1915 | u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1916 | u32 i; |
| 1917 | |
| 1918 | for (i = 0; i < ulen; i++) |
| 1919 | if (put_user(prog->aux->used_maps[i]->id, |
| 1920 | &user_map_ids[i])) |
| 1921 | return -EFAULT; |
| 1922 | } |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1923 | |
| 1924 | if (!capable(CAP_SYS_ADMIN)) { |
| 1925 | info.jited_prog_len = 0; |
| 1926 | info.xlated_prog_len = 0; |
| 1927 | goto done; |
| 1928 | } |
| 1929 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1930 | ulen = info.xlated_prog_len; |
Daniel Borkmann | 9975a54 | 2017-07-28 17:05:25 +0200 | [diff] [blame] | 1931 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1932 | if (info.xlated_prog_len && ulen) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 1933 | struct bpf_insn *insns_sanitized; |
| 1934 | bool fault; |
| 1935 | |
| 1936 | if (prog->blinded && !bpf_dump_raw_ok()) { |
| 1937 | info.xlated_prog_insns = 0; |
| 1938 | goto done; |
| 1939 | } |
| 1940 | insns_sanitized = bpf_insn_prepare_dump(prog); |
| 1941 | if (!insns_sanitized) |
| 1942 | return -ENOMEM; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1943 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
| 1944 | ulen = min_t(u32, info.xlated_prog_len, ulen); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 1945 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
| 1946 | kfree(insns_sanitized); |
| 1947 | if (fault) |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1948 | return -EFAULT; |
| 1949 | } |
| 1950 | |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 1951 | if (bpf_prog_is_dev_bound(prog->aux)) { |
| 1952 | err = bpf_prog_offload_info_fill(&info, prog); |
| 1953 | if (err) |
| 1954 | return err; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 1955 | goto done; |
| 1956 | } |
| 1957 | |
| 1958 | /* NOTE: the following code is supposed to be skipped for offload. |
| 1959 | * bpf_prog_offload_info_fill() is the place to fill similar fields |
| 1960 | * for offload. |
| 1961 | */ |
| 1962 | ulen = info.jited_prog_len; |
| 1963 | info.jited_prog_len = prog->jited_len; |
| 1964 | if (info.jited_prog_len && ulen) { |
| 1965 | if (bpf_dump_raw_ok()) { |
| 1966 | uinsns = u64_to_user_ptr(info.jited_prog_insns); |
| 1967 | ulen = min_t(u32, info.jited_prog_len, ulen); |
| 1968 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) |
| 1969 | return -EFAULT; |
| 1970 | } else { |
| 1971 | info.jited_prog_insns = 0; |
| 1972 | } |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 1973 | } |
| 1974 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 1975 | done: |
| 1976 | if (copy_to_user(uinfo, &info, info_len) || |
| 1977 | put_user(info_len, &uattr->info.info_len)) |
| 1978 | return -EFAULT; |
| 1979 | |
| 1980 | return 0; |
| 1981 | } |
| 1982 | |
| 1983 | static int bpf_map_get_info_by_fd(struct bpf_map *map, |
| 1984 | const union bpf_attr *attr, |
| 1985 | union bpf_attr __user *uattr) |
| 1986 | { |
| 1987 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 1988 | struct bpf_map_info info = {}; |
| 1989 | u32 info_len = attr->info.info_len; |
| 1990 | int err; |
| 1991 | |
| 1992 | err = check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
| 1993 | if (err) |
| 1994 | return err; |
| 1995 | info_len = min_t(u32, sizeof(info), info_len); |
| 1996 | |
| 1997 | info.type = map->map_type; |
| 1998 | info.id = map->id; |
| 1999 | info.key_size = map->key_size; |
| 2000 | info.value_size = map->value_size; |
| 2001 | info.max_entries = map->max_entries; |
| 2002 | info.map_flags = map->map_flags; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 2003 | memcpy(info.name, map->name, sizeof(map->name)); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2004 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2005 | if (map->btf) { |
| 2006 | info.btf_id = btf_id(map->btf); |
| 2007 | info.btf_key_id = map->btf_key_id; |
| 2008 | info.btf_value_id = map->btf_value_id; |
| 2009 | } |
| 2010 | |
Jakub Kicinski | 52775b3 | 2018-01-17 19:13:28 -0800 | [diff] [blame] | 2011 | if (bpf_map_is_dev_bound(map)) { |
| 2012 | err = bpf_map_offload_info_fill(&info, map); |
| 2013 | if (err) |
| 2014 | return err; |
| 2015 | } |
| 2016 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2017 | if (copy_to_user(uinfo, &info, info_len) || |
| 2018 | put_user(info_len, &uattr->info.info_len)) |
| 2019 | return -EFAULT; |
| 2020 | |
| 2021 | return 0; |
| 2022 | } |
| 2023 | |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame^] | 2024 | static int bpf_btf_get_info_by_fd(struct btf *btf, |
| 2025 | const union bpf_attr *attr, |
| 2026 | union bpf_attr __user *uattr) |
| 2027 | { |
| 2028 | struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 2029 | u32 info_len = attr->info.info_len; |
| 2030 | int err; |
| 2031 | |
| 2032 | err = check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); |
| 2033 | if (err) |
| 2034 | return err; |
| 2035 | |
| 2036 | return btf_get_info_by_fd(btf, attr, uattr); |
| 2037 | } |
| 2038 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2039 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
| 2040 | |
| 2041 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, |
| 2042 | union bpf_attr __user *uattr) |
| 2043 | { |
| 2044 | int ufd = attr->info.bpf_fd; |
| 2045 | struct fd f; |
| 2046 | int err; |
| 2047 | |
| 2048 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) |
| 2049 | return -EINVAL; |
| 2050 | |
| 2051 | f = fdget(ufd); |
| 2052 | if (!f.file) |
| 2053 | return -EBADFD; |
| 2054 | |
| 2055 | if (f.file->f_op == &bpf_prog_fops) |
| 2056 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, |
| 2057 | uattr); |
| 2058 | else if (f.file->f_op == &bpf_map_fops) |
| 2059 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, |
| 2060 | uattr); |
Martin KaFai Lau | 60197cf | 2018-04-18 15:56:02 -0700 | [diff] [blame] | 2061 | else if (f.file->f_op == &btf_fops) |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame^] | 2062 | err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2063 | else |
| 2064 | err = -EINVAL; |
| 2065 | |
| 2066 | fdput(f); |
| 2067 | return err; |
| 2068 | } |
| 2069 | |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 2070 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
| 2071 | |
| 2072 | static int bpf_btf_load(const union bpf_attr *attr) |
| 2073 | { |
| 2074 | if (CHECK_ATTR(BPF_BTF_LOAD)) |
| 2075 | return -EINVAL; |
| 2076 | |
| 2077 | if (!capable(CAP_SYS_ADMIN)) |
| 2078 | return -EPERM; |
| 2079 | |
| 2080 | return btf_new_fd(attr); |
| 2081 | } |
| 2082 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2083 | #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id |
| 2084 | |
| 2085 | static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) |
| 2086 | { |
| 2087 | if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) |
| 2088 | return -EINVAL; |
| 2089 | |
| 2090 | if (!capable(CAP_SYS_ADMIN)) |
| 2091 | return -EPERM; |
| 2092 | |
| 2093 | return btf_get_fd_by_id(attr->btf_id); |
| 2094 | } |
| 2095 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2096 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 2097 | { |
| 2098 | union bpf_attr attr = {}; |
| 2099 | int err; |
| 2100 | |
Chenbo Feng | 0fa4fe8 | 2018-03-19 17:57:27 -0700 | [diff] [blame] | 2101 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2102 | return -EPERM; |
| 2103 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2104 | err = check_uarg_tail_zero(uattr, sizeof(attr), size); |
| 2105 | if (err) |
| 2106 | return err; |
| 2107 | size = min_t(u32, size, sizeof(attr)); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2108 | |
| 2109 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ |
| 2110 | if (copy_from_user(&attr, uattr, size) != 0) |
| 2111 | return -EFAULT; |
| 2112 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2113 | err = security_bpf(cmd, &attr, size); |
| 2114 | if (err < 0) |
| 2115 | return err; |
| 2116 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2117 | switch (cmd) { |
| 2118 | case BPF_MAP_CREATE: |
| 2119 | err = map_create(&attr); |
| 2120 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 2121 | case BPF_MAP_LOOKUP_ELEM: |
| 2122 | err = map_lookup_elem(&attr); |
| 2123 | break; |
| 2124 | case BPF_MAP_UPDATE_ELEM: |
| 2125 | err = map_update_elem(&attr); |
| 2126 | break; |
| 2127 | case BPF_MAP_DELETE_ELEM: |
| 2128 | err = map_delete_elem(&attr); |
| 2129 | break; |
| 2130 | case BPF_MAP_GET_NEXT_KEY: |
| 2131 | err = map_get_next_key(&attr); |
| 2132 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2133 | case BPF_PROG_LOAD: |
| 2134 | err = bpf_prog_load(&attr); |
| 2135 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2136 | case BPF_OBJ_PIN: |
| 2137 | err = bpf_obj_pin(&attr); |
| 2138 | break; |
| 2139 | case BPF_OBJ_GET: |
| 2140 | err = bpf_obj_get(&attr); |
| 2141 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2142 | #ifdef CONFIG_CGROUP_BPF |
| 2143 | case BPF_PROG_ATTACH: |
| 2144 | err = bpf_prog_attach(&attr); |
| 2145 | break; |
| 2146 | case BPF_PROG_DETACH: |
| 2147 | err = bpf_prog_detach(&attr); |
| 2148 | break; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2149 | case BPF_PROG_QUERY: |
| 2150 | err = bpf_prog_query(&attr, uattr); |
| 2151 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2152 | #endif |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2153 | case BPF_PROG_TEST_RUN: |
| 2154 | err = bpf_prog_test_run(&attr, uattr); |
| 2155 | break; |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 2156 | case BPF_PROG_GET_NEXT_ID: |
| 2157 | err = bpf_obj_get_next_id(&attr, uattr, |
| 2158 | &prog_idr, &prog_idr_lock); |
| 2159 | break; |
| 2160 | case BPF_MAP_GET_NEXT_ID: |
| 2161 | err = bpf_obj_get_next_id(&attr, uattr, |
| 2162 | &map_idr, &map_idr_lock); |
| 2163 | break; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 2164 | case BPF_PROG_GET_FD_BY_ID: |
| 2165 | err = bpf_prog_get_fd_by_id(&attr); |
| 2166 | break; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2167 | case BPF_MAP_GET_FD_BY_ID: |
| 2168 | err = bpf_map_get_fd_by_id(&attr); |
| 2169 | break; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2170 | case BPF_OBJ_GET_INFO_BY_FD: |
| 2171 | err = bpf_obj_get_info_by_fd(&attr, uattr); |
| 2172 | break; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2173 | case BPF_RAW_TRACEPOINT_OPEN: |
| 2174 | err = bpf_raw_tracepoint_open(&attr); |
| 2175 | break; |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 2176 | case BPF_BTF_LOAD: |
| 2177 | err = bpf_btf_load(&attr); |
| 2178 | break; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2179 | case BPF_BTF_GET_FD_BY_ID: |
| 2180 | err = bpf_btf_get_fd_by_id(&attr); |
| 2181 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2182 | default: |
| 2183 | err = -EINVAL; |
| 2184 | break; |
| 2185 | } |
| 2186 | |
| 2187 | return err; |
| 2188 | } |