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