Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include <linux/bpf.h> |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 5 | #include <linux/bpf_trace.h> |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 6 | #include <linux/bpf_lirc.h> |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 7 | #include <linux/btf.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 8 | #include <linux/syscalls.h> |
| 9 | #include <linux/slab.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 10 | #include <linux/sched/signal.h> |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 11 | #include <linux/vmalloc.h> |
| 12 | #include <linux/mmzone.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 13 | #include <linux/anon_inodes.h> |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 14 | #include <linux/fdtable.h> |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 15 | #include <linux/file.h> |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 17 | #include <linux/license.h> |
| 18 | #include <linux/filter.h> |
Alexei Starovoitov | 2541517 | 2015-03-25 12:49:20 -0700 | [diff] [blame] | 19 | #include <linux/version.h> |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 21 | #include <linux/idr.h> |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 22 | #include <linux/cred.h> |
| 23 | #include <linux/timekeeping.h> |
| 24 | #include <linux/ctype.h> |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 25 | #include <linux/nospec.h> |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 26 | |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 27 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
| 28 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ |
| 29 | (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \ |
| 30 | (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 31 | #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) |
| 32 | #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map)) |
| 33 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 34 | #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY) |
| 35 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 36 | DEFINE_PER_CPU(int, bpf_prog_active); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 37 | static DEFINE_IDR(prog_idr); |
| 38 | static DEFINE_SPINLOCK(prog_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 39 | static DEFINE_IDR(map_idr); |
| 40 | static DEFINE_SPINLOCK(map_idr_lock); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 41 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 42 | int sysctl_unprivileged_bpf_disabled __read_mostly; |
| 43 | |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 44 | static const struct bpf_map_ops * const bpf_map_types[] = { |
| 45 | #define BPF_PROG_TYPE(_id, _ops) |
| 46 | #define BPF_MAP_TYPE(_id, _ops) \ |
| 47 | [_id] = &_ops, |
| 48 | #include <linux/bpf_types.h> |
| 49 | #undef BPF_PROG_TYPE |
| 50 | #undef BPF_MAP_TYPE |
| 51 | }; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 52 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 53 | /* |
| 54 | * If we're handed a bigger struct than we know of, ensure all the unknown bits |
| 55 | * are 0 - i.e. new user-space does not rely on any kernel feature extensions |
| 56 | * we don't know about yet. |
| 57 | * |
| 58 | * There is a ToCToU between this function call and the following |
| 59 | * copy_from_user() call. However, this is not a concern since this function is |
| 60 | * meant to be a future-proofing of bits. |
| 61 | */ |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 62 | int bpf_check_uarg_tail_zero(void __user *uaddr, |
| 63 | size_t expected_size, |
| 64 | size_t actual_size) |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 65 | { |
| 66 | unsigned char __user *addr; |
| 67 | unsigned char __user *end; |
| 68 | unsigned char val; |
| 69 | int err; |
| 70 | |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 71 | if (unlikely(actual_size > PAGE_SIZE)) /* silly large */ |
| 72 | return -E2BIG; |
| 73 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 74 | if (unlikely(!access_ok(uaddr, actual_size))) |
Mickaël Salaün | 752ba56 | 2017-08-07 20:45:20 +0200 | [diff] [blame] | 75 | return -EFAULT; |
| 76 | |
Mickaël Salaün | 58291a7 | 2017-08-07 20:45:19 +0200 | [diff] [blame] | 77 | if (actual_size <= expected_size) |
| 78 | return 0; |
| 79 | |
| 80 | addr = uaddr + expected_size; |
| 81 | end = uaddr + actual_size; |
| 82 | |
| 83 | for (; addr < end; addr++) { |
| 84 | err = get_user(val, addr); |
| 85 | if (err) |
| 86 | return err; |
| 87 | if (val) |
| 88 | return -E2BIG; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 94 | const struct bpf_map_ops bpf_map_offload_ops = { |
| 95 | .map_alloc = bpf_map_offload_map_alloc, |
| 96 | .map_free = bpf_map_offload_map_free, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 97 | .map_check_btf = map_check_no_btf, |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 100 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
| 101 | { |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 102 | const struct bpf_map_ops *ops; |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 103 | u32 type = attr->map_type; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 104 | struct bpf_map *map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 105 | int err; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 106 | |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 107 | if (type >= ARRAY_SIZE(bpf_map_types)) |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 108 | return ERR_PTR(-EINVAL); |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 109 | type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); |
| 110 | ops = bpf_map_types[type]; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 111 | if (!ops) |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 112 | return ERR_PTR(-EINVAL); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 113 | |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 114 | if (ops->map_alloc_check) { |
| 115 | err = ops->map_alloc_check(attr); |
| 116 | if (err) |
| 117 | return ERR_PTR(err); |
| 118 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 119 | if (attr->map_ifindex) |
| 120 | ops = &bpf_map_offload_ops; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 121 | map = ops->map_alloc(attr); |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 122 | if (IS_ERR(map)) |
| 123 | return map; |
Jakub Kicinski | 1110f3a | 2018-01-11 20:29:03 -0800 | [diff] [blame] | 124 | map->ops = ops; |
Mark Rutland | 9ef09e3 | 2018-05-03 17:04:59 +0100 | [diff] [blame] | 125 | map->map_type = type; |
Johannes Berg | 40077e0 | 2017-04-11 15:34:58 +0200 | [diff] [blame] | 126 | return map; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 129 | void *bpf_map_area_alloc(size_t size, int numa_node) |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 130 | { |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 131 | /* We really just want to fail instead of triggering OOM killer |
| 132 | * under memory pressure, therefore we set __GFP_NORETRY to kmalloc, |
| 133 | * which is used for lower order allocation requests. |
| 134 | * |
| 135 | * It has been observed that higher order allocation requests done by |
| 136 | * vmalloc with __GFP_NORETRY being set might fail due to not trying |
| 137 | * to reclaim memory from the page cache, thus we set |
| 138 | * __GFP_RETRY_MAYFAIL to avoid such situations. |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 139 | */ |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 140 | |
| 141 | const gfp_t flags = __GFP_NOWARN | __GFP_ZERO; |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 142 | void *area; |
| 143 | |
| 144 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 145 | area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags, |
| 146 | numa_node); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 147 | if (area != NULL) |
| 148 | return area; |
| 149 | } |
| 150 | |
Martynas Pumputis | f01a7db | 2019-03-18 16:10:26 +0100 | [diff] [blame] | 151 | return __vmalloc_node_flags_caller(size, numa_node, |
| 152 | GFP_KERNEL | __GFP_RETRY_MAYFAIL | |
| 153 | flags, __builtin_return_address(0)); |
Daniel Borkmann | d407bd2 | 2017-01-18 15:14:17 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void bpf_map_area_free(void *area) |
| 157 | { |
| 158 | kvfree(area); |
| 159 | } |
| 160 | |
Daniel Borkmann | be70bcd | 2019-04-09 23:20:04 +0200 | [diff] [blame] | 161 | static u32 bpf_map_flags_retain_permanent(u32 flags) |
| 162 | { |
| 163 | /* Some map creation flags are not tied to the map object but |
| 164 | * rather to the map fd instead, so they have no meaning upon |
| 165 | * map object inspection since multiple file descriptors with |
| 166 | * different (access) properties can exist here. Thus, given |
| 167 | * this has zero meaning for the map itself, lets clear these |
| 168 | * from here. |
| 169 | */ |
| 170 | return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY); |
| 171 | } |
| 172 | |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 173 | void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr) |
| 174 | { |
| 175 | map->map_type = attr->map_type; |
| 176 | map->key_size = attr->key_size; |
| 177 | map->value_size = attr->value_size; |
| 178 | map->max_entries = attr->max_entries; |
Daniel Borkmann | be70bcd | 2019-04-09 23:20:04 +0200 | [diff] [blame] | 179 | map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags); |
Jakub Kicinski | bd47564 | 2018-01-11 20:29:06 -0800 | [diff] [blame] | 180 | map->numa_node = bpf_map_attr_numa_node(attr); |
| 181 | } |
| 182 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 183 | static int bpf_charge_memlock(struct user_struct *user, u32 pages) |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 184 | { |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 185 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 186 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 187 | if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) { |
| 188 | atomic_long_sub(pages, &user->locked_vm); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 189 | return -EPERM; |
| 190 | } |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 194 | static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) |
| 195 | { |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 196 | if (user) |
| 197 | atomic_long_sub(pages, &user->locked_vm); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Roman Gushchin | c85d691 | 2019-05-29 18:03:59 -0700 | [diff] [blame] | 200 | int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size) |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 201 | { |
Roman Gushchin | c85d691 | 2019-05-29 18:03:59 -0700 | [diff] [blame] | 202 | u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT; |
| 203 | struct user_struct *user; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 204 | int ret; |
| 205 | |
Roman Gushchin | c85d691 | 2019-05-29 18:03:59 -0700 | [diff] [blame] | 206 | if (size >= U32_MAX - PAGE_SIZE) |
| 207 | return -E2BIG; |
| 208 | |
| 209 | user = get_current_user(); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 210 | ret = bpf_charge_memlock(user, pages); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 211 | if (ret) { |
| 212 | free_uid(user); |
| 213 | return ret; |
| 214 | } |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 215 | |
| 216 | mem->pages = pages; |
| 217 | mem->user = user; |
| 218 | |
| 219 | return 0; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 222 | void bpf_map_charge_finish(struct bpf_map_memory *mem) |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 223 | { |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 224 | bpf_uncharge_memlock(mem->user, mem->pages); |
| 225 | free_uid(mem->user); |
| 226 | } |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 227 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 228 | void bpf_map_charge_move(struct bpf_map_memory *dst, |
| 229 | struct bpf_map_memory *src) |
| 230 | { |
| 231 | *dst = *src; |
| 232 | |
| 233 | /* Make sure src will not be used for the redundant uncharging. */ |
| 234 | memset(src, 0, sizeof(struct bpf_map_memory)); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 237 | int bpf_map_charge_memlock(struct bpf_map *map, u32 pages) |
| 238 | { |
| 239 | int ret; |
| 240 | |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 241 | ret = bpf_charge_memlock(map->memory.user, pages); |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 242 | if (ret) |
| 243 | return ret; |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 244 | map->memory.pages += pages; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages) |
| 249 | { |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 250 | bpf_uncharge_memlock(map->memory.user, pages); |
| 251 | map->memory.pages -= pages; |
Roman Gushchin | 0a4c58f | 2018-08-02 14:27:17 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 254 | static int bpf_map_alloc_id(struct bpf_map *map) |
| 255 | { |
| 256 | int id; |
| 257 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 258 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 259 | spin_lock_bh(&map_idr_lock); |
| 260 | id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); |
| 261 | if (id > 0) |
| 262 | map->id = id; |
| 263 | spin_unlock_bh(&map_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 264 | idr_preload_end(); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 265 | |
| 266 | if (WARN_ON_ONCE(!id)) |
| 267 | return -ENOSPC; |
| 268 | |
| 269 | return id > 0 ? 0 : id; |
| 270 | } |
| 271 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 272 | 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] | 273 | { |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 274 | unsigned long flags; |
| 275 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 276 | /* Offloaded maps are removed from the IDR store when their device |
| 277 | * disappears - even if someone holds an fd to them they are unusable, |
| 278 | * the memory is gone, all ops will fail; they are simply waiting for |
| 279 | * refcnt to drop to be freed. |
| 280 | */ |
| 281 | if (!map->id) |
| 282 | return; |
| 283 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 284 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 285 | spin_lock_irqsave(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 286 | else |
| 287 | __acquire(&map_idr_lock); |
| 288 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 289 | idr_remove(&map_idr, map->id); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 290 | map->id = 0; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 291 | |
| 292 | if (do_idr_lock) |
Eric Dumazet | 930651a | 2017-09-19 09:15:59 -0700 | [diff] [blame] | 293 | spin_unlock_irqrestore(&map_idr_lock, flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 294 | else |
| 295 | __release(&map_idr_lock); |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 298 | /* called from workqueue */ |
| 299 | static void bpf_map_free_deferred(struct work_struct *work) |
| 300 | { |
| 301 | struct bpf_map *map = container_of(work, struct bpf_map, work); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 302 | struct bpf_map_memory mem; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 303 | |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 304 | bpf_map_charge_move(&mem, &map->memory); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 305 | security_bpf_map_free(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 306 | /* implementation dependent freeing */ |
| 307 | map->ops->map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 308 | bpf_map_charge_finish(&mem); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 311 | static void bpf_map_put_uref(struct bpf_map *map) |
| 312 | { |
| 313 | if (atomic_dec_and_test(&map->usercnt)) { |
John Fastabend | ba6b8de | 2018-04-23 15:39:23 -0700 | [diff] [blame] | 314 | if (map->ops->map_release_uref) |
| 315 | map->ops->map_release_uref(map); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 319 | /* decrement map refcnt and schedule it for freeing via workqueue |
| 320 | * (unrelying map implementation ops->map_free() might sleep) |
| 321 | */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 322 | 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] | 323 | { |
| 324 | if (atomic_dec_and_test(&map->refcnt)) { |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 325 | /* bpf_map_free_id() must be called first */ |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 326 | bpf_map_free_id(map, do_idr_lock); |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 327 | btf_put(map->btf); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 328 | INIT_WORK(&map->work, bpf_map_free_deferred); |
| 329 | schedule_work(&map->work); |
| 330 | } |
| 331 | } |
| 332 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 333 | void bpf_map_put(struct bpf_map *map) |
| 334 | { |
| 335 | __bpf_map_put(map, true); |
| 336 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 337 | EXPORT_SYMBOL_GPL(bpf_map_put); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 338 | |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 339 | void bpf_map_put_with_uref(struct bpf_map *map) |
| 340 | { |
| 341 | bpf_map_put_uref(map); |
| 342 | bpf_map_put(map); |
| 343 | } |
| 344 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 345 | static int bpf_map_release(struct inode *inode, struct file *filp) |
| 346 | { |
Daniel Borkmann | 61d1b6a | 2016-06-15 22:47:12 +0200 | [diff] [blame] | 347 | struct bpf_map *map = filp->private_data; |
| 348 | |
| 349 | if (map->ops->map_release) |
| 350 | map->ops->map_release(map, filp); |
| 351 | |
| 352 | bpf_map_put_with_uref(map); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 356 | static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f) |
| 357 | { |
| 358 | fmode_t mode = f.file->f_mode; |
| 359 | |
| 360 | /* Our file permissions may have been overridden by global |
| 361 | * map permissions facing syscall side. |
| 362 | */ |
| 363 | if (READ_ONCE(map->frozen)) |
| 364 | mode &= ~FMODE_CAN_WRITE; |
| 365 | return mode; |
| 366 | } |
| 367 | |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 368 | #ifdef CONFIG_PROC_FS |
| 369 | static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp) |
| 370 | { |
| 371 | const struct bpf_map *map = filp->private_data; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 372 | const struct bpf_array *array; |
| 373 | u32 owner_prog_type = 0; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 374 | u32 owner_jited = 0; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 375 | |
| 376 | if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 377 | array = container_of(map, struct bpf_array, map); |
| 378 | owner_prog_type = array->owner_prog_type; |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 379 | owner_jited = array->owner_jited; |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 380 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 381 | |
| 382 | seq_printf(m, |
| 383 | "map_type:\t%u\n" |
| 384 | "key_size:\t%u\n" |
| 385 | "value_size:\t%u\n" |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 386 | "max_entries:\t%u\n" |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 387 | "map_flags:\t%#x\n" |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 388 | "memlock:\t%llu\n" |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 389 | "map_id:\t%u\n" |
| 390 | "frozen:\t%u\n", |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 391 | map->map_type, |
| 392 | map->key_size, |
| 393 | map->value_size, |
Daniel Borkmann | 322cea2 | 2016-03-25 00:30:25 +0100 | [diff] [blame] | 394 | map->max_entries, |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 395 | map->map_flags, |
Roman Gushchin | 3539b96 | 2019-05-29 18:03:57 -0700 | [diff] [blame] | 396 | map->memory.pages * 1ULL << PAGE_SHIFT, |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 397 | map->id, |
| 398 | READ_ONCE(map->frozen)); |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 399 | |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 400 | if (owner_prog_type) { |
Daniel Borkmann | 21116b7 | 2016-11-26 01:28:07 +0100 | [diff] [blame] | 401 | seq_printf(m, "owner_prog_type:\t%u\n", |
| 402 | owner_prog_type); |
Daniel Borkmann | 9780c0a | 2017-07-02 02:13:28 +0200 | [diff] [blame] | 403 | seq_printf(m, "owner_jited:\t%u\n", |
| 404 | owner_jited); |
| 405 | } |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 406 | } |
| 407 | #endif |
| 408 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 409 | static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz, |
| 410 | loff_t *ppos) |
| 411 | { |
| 412 | /* We need this handler such that alloc_file() enables |
| 413 | * f_mode with FMODE_CAN_READ. |
| 414 | */ |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | |
| 418 | static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf, |
| 419 | size_t siz, loff_t *ppos) |
| 420 | { |
| 421 | /* We need this handler such that alloc_file() enables |
| 422 | * f_mode with FMODE_CAN_WRITE. |
| 423 | */ |
| 424 | return -EINVAL; |
| 425 | } |
| 426 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 427 | const struct file_operations bpf_map_fops = { |
Daniel Borkmann | f99bf20 | 2015-11-19 11:56:22 +0100 | [diff] [blame] | 428 | #ifdef CONFIG_PROC_FS |
| 429 | .show_fdinfo = bpf_map_show_fdinfo, |
| 430 | #endif |
| 431 | .release = bpf_map_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 432 | .read = bpf_dummy_read, |
| 433 | .write = bpf_dummy_write, |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 434 | }; |
| 435 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 436 | int bpf_map_new_fd(struct bpf_map *map, int flags) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 437 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 438 | int ret; |
| 439 | |
| 440 | ret = security_bpf_map(map, OPEN_FMODE(flags)); |
| 441 | if (ret < 0) |
| 442 | return ret; |
| 443 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 444 | return anon_inode_getfd("bpf-map", &bpf_map_fops, map, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 445 | flags | O_CLOEXEC); |
| 446 | } |
| 447 | |
| 448 | int bpf_get_file_flag(int flags) |
| 449 | { |
| 450 | if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY)) |
| 451 | return -EINVAL; |
| 452 | if (flags & BPF_F_RDONLY) |
| 453 | return O_RDONLY; |
| 454 | if (flags & BPF_F_WRONLY) |
| 455 | return O_WRONLY; |
| 456 | return O_RDWR; |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 459 | /* helper macro to check that unused fields 'union bpf_attr' are zero */ |
| 460 | #define CHECK_ATTR(CMD) \ |
| 461 | memchr_inv((void *) &attr->CMD##_LAST_FIELD + \ |
| 462 | sizeof(attr->CMD##_LAST_FIELD), 0, \ |
| 463 | sizeof(*attr) - \ |
| 464 | offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ |
| 465 | sizeof(attr->CMD##_LAST_FIELD)) != NULL |
| 466 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 467 | /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes. |
| 468 | * Return 0 on success and < 0 on error. |
| 469 | */ |
| 470 | static int bpf_obj_name_cpy(char *dst, const char *src) |
| 471 | { |
| 472 | const char *end = src + BPF_OBJ_NAME_LEN; |
| 473 | |
Martin KaFai Lau | 473d973 | 2017-10-05 21:52:11 -0700 | [diff] [blame] | 474 | memset(dst, 0, BPF_OBJ_NAME_LEN); |
Daniel Borkmann | 3e0ddc4f | 2019-04-09 23:20:07 +0200 | [diff] [blame] | 475 | /* Copy all isalnum(), '_' and '.' chars. */ |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 476 | while (src < end && *src) { |
Daniel Borkmann | 3e0ddc4f | 2019-04-09 23:20:07 +0200 | [diff] [blame] | 477 | if (!isalnum(*src) && |
| 478 | *src != '_' && *src != '.') |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 479 | return -EINVAL; |
| 480 | *dst++ = *src++; |
| 481 | } |
| 482 | |
| 483 | /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */ |
| 484 | if (src == end) |
| 485 | return -EINVAL; |
| 486 | |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 490 | int map_check_no_btf(const struct bpf_map *map, |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 491 | const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 492 | const struct btf_type *key_type, |
| 493 | const struct btf_type *value_type) |
| 494 | { |
| 495 | return -ENOTSUPP; |
| 496 | } |
| 497 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 498 | static int map_check_btf(struct bpf_map *map, const struct btf *btf, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 499 | u32 btf_key_id, u32 btf_value_id) |
| 500 | { |
| 501 | const struct btf_type *key_type, *value_type; |
| 502 | u32 key_size, value_size; |
| 503 | int ret = 0; |
| 504 | |
Daniel Borkmann | 2824ecb | 2019-04-09 23:20:10 +0200 | [diff] [blame] | 505 | /* Some maps allow key to be unspecified. */ |
| 506 | if (btf_key_id) { |
| 507 | key_type = btf_type_id_size(btf, &btf_key_id, &key_size); |
| 508 | if (!key_type || key_size != map->key_size) |
| 509 | return -EINVAL; |
| 510 | } else { |
| 511 | key_type = btf_type_by_id(btf, 0); |
| 512 | if (!map->ops->map_check_btf) |
| 513 | return -EINVAL; |
| 514 | } |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 515 | |
| 516 | value_type = btf_type_id_size(btf, &btf_value_id, &value_size); |
| 517 | if (!value_type || value_size != map->value_size) |
| 518 | return -EINVAL; |
| 519 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 520 | map->spin_lock_off = btf_find_spin_lock(btf, value_type); |
| 521 | |
| 522 | if (map_value_has_spin_lock(map)) { |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 523 | if (map->map_flags & BPF_F_RDONLY_PROG) |
| 524 | return -EACCES; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 525 | if (map->map_type != BPF_MAP_TYPE_HASH && |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 526 | map->map_type != BPF_MAP_TYPE_ARRAY && |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 527 | map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 528 | map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 529 | return -ENOTSUPP; |
| 530 | if (map->spin_lock_off + sizeof(struct bpf_spin_lock) > |
| 531 | map->value_size) { |
| 532 | WARN_ONCE(1, |
| 533 | "verifier bug spin_lock_off %d value_size %d\n", |
| 534 | map->spin_lock_off, map->value_size); |
| 535 | return -EFAULT; |
| 536 | } |
| 537 | } |
| 538 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 539 | if (map->ops->map_check_btf) |
Roman Gushchin | 1b2b234 | 2018-12-10 15:43:00 -0800 | [diff] [blame] | 540 | ret = map->ops->map_check_btf(map, btf, key_type, value_type); |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 541 | |
| 542 | return ret; |
| 543 | } |
| 544 | |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 545 | #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 546 | /* called via syscall */ |
| 547 | static int map_create(union bpf_attr *attr) |
| 548 | { |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 549 | int numa_node = bpf_map_attr_numa_node(attr); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 550 | struct bpf_map_memory mem; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 551 | struct bpf_map *map; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 552 | int f_flags; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 553 | int err; |
| 554 | |
| 555 | err = CHECK_ATTR(BPF_MAP_CREATE); |
| 556 | if (err) |
| 557 | return -EINVAL; |
| 558 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 559 | f_flags = bpf_get_file_flag(attr->map_flags); |
| 560 | if (f_flags < 0) |
| 561 | return f_flags; |
| 562 | |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 563 | if (numa_node != NUMA_NO_NODE && |
Eric Dumazet | 96e5ae4 | 2017-09-04 22:41:02 -0700 | [diff] [blame] | 564 | ((unsigned int)numa_node >= nr_node_ids || |
| 565 | !node_online(numa_node))) |
Martin KaFai Lau | 96eabe7 | 2017-08-18 11:28:00 -0700 | [diff] [blame] | 566 | return -EINVAL; |
| 567 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 568 | /* find map type and init map: hashtable vs rbtree vs bloom vs ... */ |
| 569 | map = find_and_alloc_map(attr); |
| 570 | if (IS_ERR(map)) |
| 571 | return PTR_ERR(map); |
| 572 | |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 573 | err = bpf_obj_name_cpy(map->name, attr->map_name); |
| 574 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 575 | goto free_map; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 576 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 577 | atomic_set(&map->refcnt, 1); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 578 | atomic_set(&map->usercnt, 1); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 579 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 580 | if (attr->btf_key_type_id || attr->btf_value_type_id) { |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 581 | struct btf *btf; |
| 582 | |
Daniel Borkmann | 2824ecb | 2019-04-09 23:20:10 +0200 | [diff] [blame] | 583 | if (!attr->btf_value_type_id) { |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 584 | err = -EINVAL; |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 585 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | btf = btf_get_by_fd(attr->btf_fd); |
| 589 | if (IS_ERR(btf)) { |
| 590 | err = PTR_ERR(btf); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 591 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 594 | err = map_check_btf(map, btf, attr->btf_key_type_id, |
| 595 | attr->btf_value_type_id); |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 596 | if (err) { |
| 597 | btf_put(btf); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 598 | goto free_map; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | map->btf = btf; |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 602 | map->btf_key_type_id = attr->btf_key_type_id; |
| 603 | map->btf_value_type_id = attr->btf_value_type_id; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 604 | } else { |
| 605 | map->spin_lock_off = -EINVAL; |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 608 | err = security_bpf_map_alloc(map); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 609 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 610 | goto free_map; |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 611 | |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 612 | err = bpf_map_alloc_id(map); |
| 613 | if (err) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 614 | goto free_map_sec; |
Martin KaFai Lau | f3f1c05 | 2017-06-05 12:15:47 -0700 | [diff] [blame] | 615 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 616 | err = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 617 | if (err < 0) { |
| 618 | /* failed to allocate fd. |
Peng Sun | 352d20d | 2019-02-27 22:36:25 +0800 | [diff] [blame] | 619 | * bpf_map_put_with_uref() is needed because the above |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 620 | * bpf_map_alloc_id() has published the map |
| 621 | * to the userspace and the userspace may |
| 622 | * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID. |
| 623 | */ |
Peng Sun | 352d20d | 2019-02-27 22:36:25 +0800 | [diff] [blame] | 624 | bpf_map_put_with_uref(map); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 625 | return err; |
| 626 | } |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 627 | |
| 628 | return err; |
| 629 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 630 | free_map_sec: |
| 631 | security_bpf_map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 632 | free_map: |
Martin KaFai Lau | a26ca7c | 2018-04-18 15:56:03 -0700 | [diff] [blame] | 633 | btf_put(map->btf); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 634 | bpf_map_charge_move(&mem, &map->memory); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 635 | map->ops->map_free(map); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 636 | bpf_map_charge_finish(&mem); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 637 | return err; |
| 638 | } |
| 639 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 640 | /* if error is returned, fd is released. |
| 641 | * On success caller should complete fd access with matching fdput() |
| 642 | */ |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 643 | struct bpf_map *__bpf_map_get(struct fd f) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 644 | { |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 645 | if (!f.file) |
| 646 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 647 | if (f.file->f_op != &bpf_map_fops) { |
| 648 | fdput(f); |
| 649 | return ERR_PTR(-EINVAL); |
| 650 | } |
| 651 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 652 | return f.file->private_data; |
| 653 | } |
| 654 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 655 | /* prog's and map's refcnt limit */ |
| 656 | #define BPF_MAX_REFCNT 32768 |
| 657 | |
| 658 | struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref) |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 659 | { |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 660 | if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) { |
| 661 | atomic_dec(&map->refcnt); |
| 662 | return ERR_PTR(-EBUSY); |
| 663 | } |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 664 | if (uref) |
| 665 | atomic_inc(&map->usercnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 666 | return map; |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 667 | } |
Jakub Kicinski | 630a4d3 | 2018-05-03 18:37:09 -0700 | [diff] [blame] | 668 | EXPORT_SYMBOL_GPL(bpf_map_inc); |
Daniel Borkmann | c9da161 | 2015-11-24 21:28:15 +0100 | [diff] [blame] | 669 | |
| 670 | struct bpf_map *bpf_map_get_with_uref(u32 ufd) |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 671 | { |
| 672 | struct fd f = fdget(ufd); |
| 673 | struct bpf_map *map; |
| 674 | |
| 675 | map = __bpf_map_get(f); |
| 676 | if (IS_ERR(map)) |
| 677 | return map; |
| 678 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 679 | map = bpf_map_inc(map, true); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 680 | fdput(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 681 | |
| 682 | return map; |
| 683 | } |
| 684 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 685 | /* map_idr_lock should have been held */ |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 686 | static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, |
| 687 | bool uref) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 688 | { |
| 689 | int refold; |
| 690 | |
Mark Rutland | bfc18e3 | 2018-06-21 13:13:04 +0100 | [diff] [blame] | 691 | refold = atomic_fetch_add_unless(&map->refcnt, 1, 0); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 692 | |
| 693 | if (refold >= BPF_MAX_REFCNT) { |
| 694 | __bpf_map_put(map, false); |
| 695 | return ERR_PTR(-EBUSY); |
| 696 | } |
| 697 | |
| 698 | if (!refold) |
| 699 | return ERR_PTR(-ENOENT); |
| 700 | |
| 701 | if (uref) |
| 702 | atomic_inc(&map->usercnt); |
| 703 | |
| 704 | return map; |
| 705 | } |
| 706 | |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 707 | struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, bool uref) |
| 708 | { |
| 709 | spin_lock_bh(&map_idr_lock); |
| 710 | map = __bpf_map_inc_not_zero(map, uref); |
| 711 | spin_unlock_bh(&map_idr_lock); |
| 712 | |
| 713 | return map; |
| 714 | } |
| 715 | EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); |
| 716 | |
Alexei Starovoitov | b8cdc05 | 2016-03-09 18:56:49 -0800 | [diff] [blame] | 717 | int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) |
| 718 | { |
| 719 | return -ENOTSUPP; |
| 720 | } |
| 721 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 722 | static void *__bpf_copy_key(void __user *ukey, u64 key_size) |
| 723 | { |
| 724 | if (key_size) |
| 725 | return memdup_user(ukey, key_size); |
| 726 | |
| 727 | if (ukey) |
| 728 | return ERR_PTR(-EINVAL); |
| 729 | |
| 730 | return NULL; |
| 731 | } |
| 732 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 733 | /* last field in 'union bpf_attr' used by this command */ |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 734 | #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 735 | |
| 736 | static int map_lookup_elem(union bpf_attr *attr) |
| 737 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 738 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 739 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 740 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 741 | struct bpf_map *map; |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 742 | void *key, *value, *ptr; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 743 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 744 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 745 | int err; |
| 746 | |
| 747 | if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) |
| 748 | return -EINVAL; |
| 749 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 750 | if (attr->flags & ~BPF_F_LOCK) |
| 751 | return -EINVAL; |
| 752 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 753 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 754 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 755 | if (IS_ERR(map)) |
| 756 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 757 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 758 | err = -EPERM; |
| 759 | goto err_put; |
| 760 | } |
| 761 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 762 | if ((attr->flags & BPF_F_LOCK) && |
| 763 | !map_value_has_spin_lock(map)) { |
| 764 | err = -EINVAL; |
| 765 | goto err_put; |
| 766 | } |
| 767 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 768 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 769 | if (IS_ERR(key)) { |
| 770 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 771 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 772 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 773 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 774 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 775 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 776 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
| 777 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 778 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 779 | else if (IS_FD_MAP(map)) |
| 780 | value_size = sizeof(u32); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 781 | else |
| 782 | value_size = map->value_size; |
| 783 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 784 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 785 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 786 | if (!value) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 787 | goto free_key; |
| 788 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 789 | if (bpf_map_is_dev_bound(map)) { |
| 790 | err = bpf_map_offload_lookup_elem(map, key, value); |
Martin KaFai Lau | 7c4cd05 | 2019-01-30 18:12:45 -0800 | [diff] [blame] | 791 | goto done; |
| 792 | } |
| 793 | |
| 794 | preempt_disable(); |
| 795 | this_cpu_inc(bpf_prog_active); |
| 796 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 797 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 798 | err = bpf_percpu_hash_copy(map, key, value); |
| 799 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 800 | err = bpf_percpu_array_copy(map, key, value); |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 801 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
| 802 | err = bpf_percpu_cgroup_storage_copy(map, key, value); |
Alexei Starovoitov | 557c0c6 | 2016-03-07 21:57:17 -0800 | [diff] [blame] | 803 | } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { |
| 804 | err = bpf_stackmap_copy(map, key, value); |
Martin KaFai Lau | 14dc6f0 | 2017-06-27 23:08:34 -0700 | [diff] [blame] | 805 | } else if (IS_FD_ARRAY(map)) { |
| 806 | err = bpf_fd_array_map_lookup_elem(map, key, value); |
| 807 | } else if (IS_FD_HASH(map)) { |
| 808 | err = bpf_fd_htab_map_lookup_elem(map, key, value); |
Martin KaFai Lau | 5dc4c4b | 2018-08-08 01:01:24 -0700 | [diff] [blame] | 809 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
| 810 | err = bpf_fd_reuseport_array_lookup_elem(map, key, value); |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 811 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 812 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 813 | err = map->ops->map_peek_elem(map, value); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 814 | } else { |
| 815 | rcu_read_lock(); |
Daniel Borkmann | c611022 | 2019-05-14 01:18:55 +0200 | [diff] [blame] | 816 | if (map->ops->map_lookup_elem_sys_only) |
| 817 | ptr = map->ops->map_lookup_elem_sys_only(map, key); |
| 818 | else |
| 819 | ptr = map->ops->map_lookup_elem(map, key); |
Prashant Bhole | 509db28 | 2018-10-09 10:04:49 +0900 | [diff] [blame] | 820 | if (IS_ERR(ptr)) { |
| 821 | err = PTR_ERR(ptr); |
| 822 | } else if (!ptr) { |
| 823 | err = -ENOENT; |
| 824 | } else { |
| 825 | err = 0; |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 826 | if (attr->flags & BPF_F_LOCK) |
| 827 | /* lock 'ptr' and copy everything but lock */ |
| 828 | copy_map_value_locked(map, value, ptr, true); |
| 829 | else |
| 830 | copy_map_value(map, value, ptr); |
| 831 | /* mask lock, since value wasn't zero inited */ |
| 832 | check_and_init_map_lock(map, value); |
Prashant Bhole | 509db28 | 2018-10-09 10:04:49 +0900 | [diff] [blame] | 833 | } |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 834 | rcu_read_unlock(); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 835 | } |
Martin KaFai Lau | 7c4cd05 | 2019-01-30 18:12:45 -0800 | [diff] [blame] | 836 | this_cpu_dec(bpf_prog_active); |
| 837 | preempt_enable(); |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 838 | |
Martin KaFai Lau | 7c4cd05 | 2019-01-30 18:12:45 -0800 | [diff] [blame] | 839 | done: |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 840 | if (err) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 841 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 842 | |
| 843 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 844 | if (copy_to_user(uvalue, value, value_size) != 0) |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 845 | goto free_value; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 846 | |
| 847 | err = 0; |
| 848 | |
Alexei Starovoitov | 8ebe667 | 2015-01-22 17:11:08 -0800 | [diff] [blame] | 849 | free_value: |
| 850 | kfree(value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 851 | free_key: |
| 852 | kfree(key); |
| 853 | err_put: |
| 854 | fdput(f); |
| 855 | return err; |
| 856 | } |
| 857 | |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 858 | static void maybe_wait_bpf_programs(struct bpf_map *map) |
| 859 | { |
| 860 | /* Wait for any running BPF programs to complete so that |
| 861 | * userspace, when we return to it, knows that all programs |
| 862 | * that could be running use the new map value. |
| 863 | */ |
| 864 | if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS || |
| 865 | map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) |
| 866 | synchronize_rcu(); |
| 867 | } |
| 868 | |
Alexei Starovoitov | 3274f52 | 2014-11-13 17:36:44 -0800 | [diff] [blame] | 869 | #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 870 | |
| 871 | static int map_update_elem(union bpf_attr *attr) |
| 872 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 873 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 874 | void __user *uvalue = u64_to_user_ptr(attr->value); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 875 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 876 | struct bpf_map *map; |
| 877 | void *key, *value; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 878 | u32 value_size; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 879 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 880 | int err; |
| 881 | |
| 882 | if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) |
| 883 | return -EINVAL; |
| 884 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 885 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 886 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 887 | if (IS_ERR(map)) |
| 888 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 889 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 890 | err = -EPERM; |
| 891 | goto err_put; |
| 892 | } |
| 893 | |
Alexei Starovoitov | 96049f3 | 2019-01-31 15:40:09 -0800 | [diff] [blame] | 894 | if ((attr->flags & BPF_F_LOCK) && |
| 895 | !map_value_has_spin_lock(map)) { |
| 896 | err = -EINVAL; |
| 897 | goto err_put; |
| 898 | } |
| 899 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 900 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 901 | if (IS_ERR(key)) { |
| 902 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 903 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 904 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 905 | |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 906 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 907 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 908 | map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || |
| 909 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 910 | value_size = round_up(map->value_size, 8) * num_possible_cpus(); |
| 911 | else |
| 912 | value_size = map->value_size; |
| 913 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 914 | err = -ENOMEM; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 915 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 916 | if (!value) |
| 917 | goto free_key; |
| 918 | |
| 919 | err = -EFAULT; |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 920 | if (copy_from_user(value, uvalue, value_size) != 0) |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 921 | goto free_value; |
| 922 | |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 923 | /* Need to create a kthread, thus must support schedule */ |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 924 | if (bpf_map_is_dev_bound(map)) { |
| 925 | err = bpf_map_offload_update_elem(map, key, value, attr->flags); |
| 926 | goto out; |
John Fastabend | 99ba2b5 | 2018-07-05 08:50:04 -0700 | [diff] [blame] | 927 | } else if (map->map_type == BPF_MAP_TYPE_CPUMAP || |
| 928 | map->map_type == BPF_MAP_TYPE_SOCKHASH || |
| 929 | map->map_type == BPF_MAP_TYPE_SOCKMAP) { |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 930 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 931 | goto out; |
| 932 | } |
| 933 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 934 | /* must increment bpf_prog_active to avoid kprobe+bpf triggering from |
| 935 | * inside bpf map update or delete otherwise deadlocks are possible |
| 936 | */ |
| 937 | preempt_disable(); |
| 938 | __this_cpu_inc(bpf_prog_active); |
Martin KaFai Lau | 8f84493 | 2016-11-11 10:55:10 -0800 | [diff] [blame] | 939 | if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || |
| 940 | map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 941 | err = bpf_percpu_hash_update(map, key, value, attr->flags); |
| 942 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { |
| 943 | err = bpf_percpu_array_update(map, key, value, attr->flags); |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 944 | } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { |
| 945 | err = bpf_percpu_cgroup_storage_update(map, key, value, |
| 946 | attr->flags); |
Mickaël Salaün | 9c147b5 | 2018-01-26 00:54:02 +0100 | [diff] [blame] | 947 | } else if (IS_FD_ARRAY(map)) { |
Daniel Borkmann | d056a78 | 2016-06-15 22:47:13 +0200 | [diff] [blame] | 948 | rcu_read_lock(); |
| 949 | err = bpf_fd_array_map_update_elem(map, f.file, key, value, |
| 950 | attr->flags); |
| 951 | rcu_read_unlock(); |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 952 | } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
| 953 | rcu_read_lock(); |
| 954 | err = bpf_fd_htab_map_update_elem(map, f.file, key, value, |
| 955 | attr->flags); |
| 956 | rcu_read_unlock(); |
Martin KaFai Lau | 5dc4c4b | 2018-08-08 01:01:24 -0700 | [diff] [blame] | 957 | } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { |
| 958 | /* rcu_read_lock() is not needed */ |
| 959 | err = bpf_fd_reuseport_array_update_elem(map, key, value, |
| 960 | attr->flags); |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 961 | } else if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 962 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 963 | err = map->ops->map_push_elem(map, value, attr->flags); |
Alexei Starovoitov | 15a07b3 | 2016-02-01 22:39:55 -0800 | [diff] [blame] | 964 | } else { |
| 965 | rcu_read_lock(); |
| 966 | err = map->ops->map_update_elem(map, key, value, attr->flags); |
| 967 | rcu_read_unlock(); |
| 968 | } |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 969 | __this_cpu_dec(bpf_prog_active); |
| 970 | preempt_enable(); |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 971 | maybe_wait_bpf_programs(map); |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 972 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 973 | free_value: |
| 974 | kfree(value); |
| 975 | free_key: |
| 976 | kfree(key); |
| 977 | err_put: |
| 978 | fdput(f); |
| 979 | return err; |
| 980 | } |
| 981 | |
| 982 | #define BPF_MAP_DELETE_ELEM_LAST_FIELD key |
| 983 | |
| 984 | static int map_delete_elem(union bpf_attr *attr) |
| 985 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 986 | void __user *ukey = u64_to_user_ptr(attr->key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 987 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 988 | struct bpf_map *map; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 989 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 990 | void *key; |
| 991 | int err; |
| 992 | |
| 993 | if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) |
| 994 | return -EINVAL; |
| 995 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 996 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 997 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 998 | if (IS_ERR(map)) |
| 999 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1000 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1001 | err = -EPERM; |
| 1002 | goto err_put; |
| 1003 | } |
| 1004 | |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1005 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1006 | if (IS_ERR(key)) { |
| 1007 | err = PTR_ERR(key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1008 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1009 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1010 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1011 | if (bpf_map_is_dev_bound(map)) { |
| 1012 | err = bpf_map_offload_delete_elem(map, key); |
| 1013 | goto out; |
| 1014 | } |
| 1015 | |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 1016 | preempt_disable(); |
| 1017 | __this_cpu_inc(bpf_prog_active); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1018 | rcu_read_lock(); |
| 1019 | err = map->ops->map_delete_elem(map, key); |
| 1020 | rcu_read_unlock(); |
Alexei Starovoitov | b121d1e | 2016-03-07 21:57:13 -0800 | [diff] [blame] | 1021 | __this_cpu_dec(bpf_prog_active); |
| 1022 | preempt_enable(); |
Daniel Colascione | 1ae80cf | 2018-10-12 03:54:27 -0700 | [diff] [blame] | 1023 | maybe_wait_bpf_programs(map); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1024 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1025 | kfree(key); |
| 1026 | err_put: |
| 1027 | fdput(f); |
| 1028 | return err; |
| 1029 | } |
| 1030 | |
| 1031 | /* last field in 'union bpf_attr' used by this command */ |
| 1032 | #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key |
| 1033 | |
| 1034 | static int map_get_next_key(union bpf_attr *attr) |
| 1035 | { |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1036 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1037 | void __user *unext_key = u64_to_user_ptr(attr->next_key); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1038 | int ufd = attr->map_fd; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1039 | struct bpf_map *map; |
| 1040 | void *key, *next_key; |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1041 | struct fd f; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1042 | int err; |
| 1043 | |
| 1044 | if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) |
| 1045 | return -EINVAL; |
| 1046 | |
Daniel Borkmann | 592867b | 2015-09-08 18:00:09 +0200 | [diff] [blame] | 1047 | f = fdget(ufd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1048 | map = __bpf_map_get(f); |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1049 | if (IS_ERR(map)) |
| 1050 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1051 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1052 | err = -EPERM; |
| 1053 | goto err_put; |
| 1054 | } |
| 1055 | |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1056 | if (ukey) { |
Mauricio Vasquez B | c9d29f4 | 2018-10-18 15:16:14 +0200 | [diff] [blame] | 1057 | key = __bpf_copy_key(ukey, map->key_size); |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1058 | if (IS_ERR(key)) { |
| 1059 | err = PTR_ERR(key); |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1060 | goto err_put; |
Al Viro | e4448ed | 2017-05-13 18:43:00 -0400 | [diff] [blame] | 1061 | } |
Teng Qin | 8fe4592 | 2017-04-24 19:00:37 -0700 | [diff] [blame] | 1062 | } else { |
| 1063 | key = NULL; |
| 1064 | } |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1065 | |
| 1066 | err = -ENOMEM; |
| 1067 | next_key = kmalloc(map->key_size, GFP_USER); |
| 1068 | if (!next_key) |
| 1069 | goto free_key; |
| 1070 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1071 | if (bpf_map_is_dev_bound(map)) { |
| 1072 | err = bpf_map_offload_get_next_key(map, key, next_key); |
| 1073 | goto out; |
| 1074 | } |
| 1075 | |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1076 | rcu_read_lock(); |
| 1077 | err = map->ops->map_get_next_key(map, key, next_key); |
| 1078 | rcu_read_unlock(); |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 1079 | out: |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 1080 | if (err) |
| 1081 | goto free_next_key; |
| 1082 | |
| 1083 | err = -EFAULT; |
| 1084 | if (copy_to_user(unext_key, next_key, map->key_size) != 0) |
| 1085 | goto free_next_key; |
| 1086 | |
| 1087 | err = 0; |
| 1088 | |
| 1089 | free_next_key: |
| 1090 | kfree(next_key); |
| 1091 | free_key: |
| 1092 | kfree(key); |
| 1093 | err_put: |
| 1094 | fdput(f); |
| 1095 | return err; |
| 1096 | } |
| 1097 | |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1098 | #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value |
| 1099 | |
| 1100 | static int map_lookup_and_delete_elem(union bpf_attr *attr) |
| 1101 | { |
| 1102 | void __user *ukey = u64_to_user_ptr(attr->key); |
| 1103 | void __user *uvalue = u64_to_user_ptr(attr->value); |
| 1104 | int ufd = attr->map_fd; |
| 1105 | struct bpf_map *map; |
Alexei Starovoitov | 540fefc | 2018-10-19 13:52:38 -0700 | [diff] [blame] | 1106 | void *key, *value; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1107 | u32 value_size; |
| 1108 | struct fd f; |
| 1109 | int err; |
| 1110 | |
| 1111 | if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM)) |
| 1112 | return -EINVAL; |
| 1113 | |
| 1114 | f = fdget(ufd); |
| 1115 | map = __bpf_map_get(f); |
| 1116 | if (IS_ERR(map)) |
| 1117 | return PTR_ERR(map); |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1118 | if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) { |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 1119 | err = -EPERM; |
| 1120 | goto err_put; |
| 1121 | } |
| 1122 | |
| 1123 | key = __bpf_copy_key(ukey, map->key_size); |
| 1124 | if (IS_ERR(key)) { |
| 1125 | err = PTR_ERR(key); |
| 1126 | goto err_put; |
| 1127 | } |
| 1128 | |
| 1129 | value_size = map->value_size; |
| 1130 | |
| 1131 | err = -ENOMEM; |
| 1132 | value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); |
| 1133 | if (!value) |
| 1134 | goto free_key; |
| 1135 | |
| 1136 | if (map->map_type == BPF_MAP_TYPE_QUEUE || |
| 1137 | map->map_type == BPF_MAP_TYPE_STACK) { |
| 1138 | err = map->ops->map_pop_elem(map, value); |
| 1139 | } else { |
| 1140 | err = -ENOTSUPP; |
| 1141 | } |
| 1142 | |
| 1143 | if (err) |
| 1144 | goto free_value; |
| 1145 | |
| 1146 | if (copy_to_user(uvalue, value, value_size) != 0) |
| 1147 | goto free_value; |
| 1148 | |
| 1149 | err = 0; |
| 1150 | |
| 1151 | free_value: |
| 1152 | kfree(value); |
| 1153 | free_key: |
| 1154 | kfree(key); |
| 1155 | err_put: |
| 1156 | fdput(f); |
| 1157 | return err; |
| 1158 | } |
| 1159 | |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 1160 | #define BPF_MAP_FREEZE_LAST_FIELD map_fd |
| 1161 | |
| 1162 | static int map_freeze(const union bpf_attr *attr) |
| 1163 | { |
| 1164 | int err = 0, ufd = attr->map_fd; |
| 1165 | struct bpf_map *map; |
| 1166 | struct fd f; |
| 1167 | |
| 1168 | if (CHECK_ATTR(BPF_MAP_FREEZE)) |
| 1169 | return -EINVAL; |
| 1170 | |
| 1171 | f = fdget(ufd); |
| 1172 | map = __bpf_map_get(f); |
| 1173 | if (IS_ERR(map)) |
| 1174 | return PTR_ERR(map); |
| 1175 | if (READ_ONCE(map->frozen)) { |
| 1176 | err = -EBUSY; |
| 1177 | goto err_put; |
| 1178 | } |
| 1179 | if (!capable(CAP_SYS_ADMIN)) { |
| 1180 | err = -EPERM; |
| 1181 | goto err_put; |
| 1182 | } |
| 1183 | |
| 1184 | WRITE_ONCE(map->frozen, true); |
| 1185 | err_put: |
| 1186 | fdput(f); |
| 1187 | return err; |
| 1188 | } |
| 1189 | |
Jakub Kicinski | 7de16e3 | 2017-10-16 16:40:53 -0700 | [diff] [blame] | 1190 | static const struct bpf_prog_ops * const bpf_prog_types[] = { |
| 1191 | #define BPF_PROG_TYPE(_id, _name) \ |
| 1192 | [_id] = & _name ## _prog_ops, |
| 1193 | #define BPF_MAP_TYPE(_id, _ops) |
| 1194 | #include <linux/bpf_types.h> |
| 1195 | #undef BPF_PROG_TYPE |
| 1196 | #undef BPF_MAP_TYPE |
| 1197 | }; |
| 1198 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1199 | static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog) |
| 1200 | { |
Daniel Borkmann | d0f1a45 | 2018-05-04 02:13:57 +0200 | [diff] [blame] | 1201 | const struct bpf_prog_ops *ops; |
| 1202 | |
| 1203 | if (type >= ARRAY_SIZE(bpf_prog_types)) |
| 1204 | return -EINVAL; |
| 1205 | type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types)); |
| 1206 | ops = bpf_prog_types[type]; |
| 1207 | if (!ops) |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 1208 | return -EINVAL; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1209 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1210 | if (!bpf_prog_is_dev_bound(prog->aux)) |
Daniel Borkmann | d0f1a45 | 2018-05-04 02:13:57 +0200 | [diff] [blame] | 1211 | prog->aux->ops = ops; |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1212 | else |
| 1213 | prog->aux->ops = &bpf_offload_prog_ops; |
Johannes Berg | be9370a | 2017-04-11 15:34:57 +0200 | [diff] [blame] | 1214 | prog->type = type; |
| 1215 | return 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /* drop refcnt on maps used by eBPF program and free auxilary data */ |
| 1219 | static void free_used_maps(struct bpf_prog_aux *aux) |
| 1220 | { |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 1221 | enum bpf_cgroup_storage_type stype; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1222 | int i; |
| 1223 | |
Roman Gushchin | 8bad74f | 2018-09-28 14:45:36 +0000 | [diff] [blame] | 1224 | for_each_cgroup_storage_type(stype) { |
| 1225 | if (!aux->cgroup_storage[stype]) |
| 1226 | continue; |
| 1227 | bpf_cgroup_storage_release(aux->prog, |
| 1228 | aux->cgroup_storage[stype]); |
| 1229 | } |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 1230 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1231 | for (i = 0; i < aux->used_map_cnt; i++) |
| 1232 | bpf_map_put(aux->used_maps[i]); |
| 1233 | |
| 1234 | kfree(aux->used_maps); |
| 1235 | } |
| 1236 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1237 | int __bpf_prog_charge(struct user_struct *user, u32 pages) |
| 1238 | { |
| 1239 | unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 1240 | unsigned long user_bufs; |
| 1241 | |
| 1242 | if (user) { |
| 1243 | user_bufs = atomic_long_add_return(pages, &user->locked_vm); |
| 1244 | if (user_bufs > memlock_limit) { |
| 1245 | atomic_long_sub(pages, &user->locked_vm); |
| 1246 | return -EPERM; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | void __bpf_prog_uncharge(struct user_struct *user, u32 pages) |
| 1254 | { |
| 1255 | if (user) |
| 1256 | atomic_long_sub(pages, &user->locked_vm); |
| 1257 | } |
| 1258 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1259 | static int bpf_prog_charge_memlock(struct bpf_prog *prog) |
| 1260 | { |
| 1261 | struct user_struct *user = get_current_user(); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1262 | int ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1263 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1264 | ret = __bpf_prog_charge(user, prog->pages); |
| 1265 | if (ret) { |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1266 | free_uid(user); |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1267 | return ret; |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1268 | } |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1269 | |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1270 | prog->aux->user = user; |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | static void bpf_prog_uncharge_memlock(struct bpf_prog *prog) |
| 1275 | { |
| 1276 | struct user_struct *user = prog->aux->user; |
| 1277 | |
Daniel Borkmann | 5ccb071 | 2016-12-18 01:52:58 +0100 | [diff] [blame] | 1278 | __bpf_prog_uncharge(user, prog->pages); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1279 | free_uid(user); |
| 1280 | } |
| 1281 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1282 | static int bpf_prog_alloc_id(struct bpf_prog *prog) |
| 1283 | { |
| 1284 | int id; |
| 1285 | |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 1286 | idr_preload(GFP_KERNEL); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1287 | spin_lock_bh(&prog_idr_lock); |
| 1288 | id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); |
| 1289 | if (id > 0) |
| 1290 | prog->aux->id = id; |
| 1291 | spin_unlock_bh(&prog_idr_lock); |
Shaohua Li | b76354c | 2018-03-27 11:53:21 -0700 | [diff] [blame] | 1292 | idr_preload_end(); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1293 | |
| 1294 | /* id is in [1, INT_MAX) */ |
| 1295 | if (WARN_ON_ONCE(!id)) |
| 1296 | return -ENOSPC; |
| 1297 | |
| 1298 | return id > 0 ? 0 : id; |
| 1299 | } |
| 1300 | |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1301 | 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] | 1302 | { |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1303 | /* cBPF to eBPF migrations are currently not in the idr store. |
| 1304 | * Offloaded programs are removed from the store when their device |
| 1305 | * disappears - even if someone grabs an fd to them they are unusable, |
| 1306 | * simply waiting for refcnt to drop to be freed. |
| 1307 | */ |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1308 | if (!prog->aux->id) |
| 1309 | return; |
| 1310 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1311 | if (do_idr_lock) |
| 1312 | spin_lock_bh(&prog_idr_lock); |
| 1313 | else |
| 1314 | __acquire(&prog_idr_lock); |
| 1315 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1316 | idr_remove(&prog_idr, prog->aux->id); |
Jakub Kicinski | ad8ad79 | 2017-12-27 18:39:07 -0800 | [diff] [blame] | 1317 | prog->aux->id = 0; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1318 | |
| 1319 | if (do_idr_lock) |
| 1320 | spin_unlock_bh(&prog_idr_lock); |
| 1321 | else |
| 1322 | __release(&prog_idr_lock); |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1325 | static void __bpf_prog_put_rcu(struct rcu_head *rcu) |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1326 | { |
| 1327 | struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu); |
| 1328 | |
| 1329 | free_used_maps(aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1330 | bpf_prog_uncharge_memlock(aux->prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1331 | security_bpf_prog_free(aux); |
Alexei Starovoitov | abf2e7d | 2015-05-28 19:26:02 -0700 | [diff] [blame] | 1332 | bpf_prog_free(aux->prog); |
| 1333 | } |
| 1334 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1335 | 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] | 1336 | { |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1337 | if (atomic_dec_and_test(&prog->aux->refcnt)) { |
Song Liu | 6ee52e2 | 2019-01-17 08:15:15 -0800 | [diff] [blame] | 1338 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0); |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 1339 | /* bpf_prog_free_id() must be called first */ |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1340 | bpf_prog_free_id(prog, do_idr_lock); |
Daniel Borkmann | 7d1982b | 2018-06-15 02:30:47 +0200 | [diff] [blame] | 1341 | bpf_prog_kallsyms_del_all(prog); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 1342 | btf_put(prog->aux->btf); |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 1343 | kvfree(prog->aux->func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 1344 | bpf_prog_free_linfo(prog); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 1345 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1346 | call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu); |
Daniel Borkmann | a67edbf | 2017-01-25 02:28:18 +0100 | [diff] [blame] | 1347 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1348 | } |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1349 | |
| 1350 | void bpf_prog_put(struct bpf_prog *prog) |
| 1351 | { |
| 1352 | __bpf_prog_put(prog, true); |
| 1353 | } |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 1354 | EXPORT_SYMBOL_GPL(bpf_prog_put); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1355 | |
| 1356 | static int bpf_prog_release(struct inode *inode, struct file *filp) |
| 1357 | { |
| 1358 | struct bpf_prog *prog = filp->private_data; |
| 1359 | |
Daniel Borkmann | 1aacde3 | 2016-06-30 17:24:43 +0200 | [diff] [blame] | 1360 | bpf_prog_put(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1361 | return 0; |
| 1362 | } |
| 1363 | |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1364 | static void bpf_prog_get_stats(const struct bpf_prog *prog, |
| 1365 | struct bpf_prog_stats *stats) |
| 1366 | { |
| 1367 | u64 nsecs = 0, cnt = 0; |
| 1368 | int cpu; |
| 1369 | |
| 1370 | for_each_possible_cpu(cpu) { |
| 1371 | const struct bpf_prog_stats *st; |
| 1372 | unsigned int start; |
| 1373 | u64 tnsecs, tcnt; |
| 1374 | |
| 1375 | st = per_cpu_ptr(prog->aux->stats, cpu); |
| 1376 | do { |
| 1377 | start = u64_stats_fetch_begin_irq(&st->syncp); |
| 1378 | tnsecs = st->nsecs; |
| 1379 | tcnt = st->cnt; |
| 1380 | } while (u64_stats_fetch_retry_irq(&st->syncp, start)); |
| 1381 | nsecs += tnsecs; |
| 1382 | cnt += tcnt; |
| 1383 | } |
| 1384 | stats->nsecs = nsecs; |
| 1385 | stats->cnt = cnt; |
| 1386 | } |
| 1387 | |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1388 | #ifdef CONFIG_PROC_FS |
| 1389 | static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) |
| 1390 | { |
| 1391 | const struct bpf_prog *prog = filp->private_data; |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1392 | char prog_tag[sizeof(prog->tag) * 2 + 1] = { }; |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1393 | struct bpf_prog_stats stats; |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1394 | |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1395 | bpf_prog_get_stats(prog, &stats); |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1396 | bin2hex(prog_tag, prog->tag, sizeof(prog->tag)); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1397 | seq_printf(m, |
| 1398 | "prog_type:\t%u\n" |
| 1399 | "prog_jited:\t%u\n" |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1400 | "prog_tag:\t%s\n" |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 1401 | "memlock:\t%llu\n" |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1402 | "prog_id:\t%u\n" |
| 1403 | "run_time_ns:\t%llu\n" |
| 1404 | "run_cnt:\t%llu\n", |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1405 | prog->type, |
| 1406 | prog->jited, |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 1407 | prog_tag, |
Daniel Borkmann | 4316b40 | 2018-06-02 23:06:34 +0200 | [diff] [blame] | 1408 | prog->pages * 1ULL << PAGE_SHIFT, |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 1409 | prog->aux->id, |
| 1410 | stats.nsecs, |
| 1411 | stats.cnt); |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1412 | } |
| 1413 | #endif |
| 1414 | |
Chenbo Feng | f66e448 | 2017-10-18 13:00:26 -0700 | [diff] [blame] | 1415 | const struct file_operations bpf_prog_fops = { |
Daniel Borkmann | 7bd509e | 2016-12-04 23:19:41 +0100 | [diff] [blame] | 1416 | #ifdef CONFIG_PROC_FS |
| 1417 | .show_fdinfo = bpf_prog_show_fdinfo, |
| 1418 | #endif |
| 1419 | .release = bpf_prog_release, |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1420 | .read = bpf_dummy_read, |
| 1421 | .write = bpf_dummy_write, |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1422 | }; |
| 1423 | |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1424 | int bpf_prog_new_fd(struct bpf_prog *prog) |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1425 | { |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1426 | int ret; |
| 1427 | |
| 1428 | ret = security_bpf_prog(prog); |
| 1429 | if (ret < 0) |
| 1430 | return ret; |
| 1431 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1432 | return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, |
| 1433 | O_RDWR | O_CLOEXEC); |
| 1434 | } |
| 1435 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1436 | static struct bpf_prog *____bpf_prog_get(struct fd f) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1437 | { |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1438 | if (!f.file) |
| 1439 | return ERR_PTR(-EBADF); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1440 | if (f.file->f_op != &bpf_prog_fops) { |
| 1441 | fdput(f); |
| 1442 | return ERR_PTR(-EINVAL); |
| 1443 | } |
| 1444 | |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 1445 | return f.file->private_data; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1448 | struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i) |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1449 | { |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1450 | if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) { |
| 1451 | atomic_sub(i, &prog->aux->refcnt); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1452 | return ERR_PTR(-EBUSY); |
| 1453 | } |
| 1454 | return prog; |
| 1455 | } |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1456 | EXPORT_SYMBOL_GPL(bpf_prog_add); |
| 1457 | |
Daniel Borkmann | c540594 | 2016-11-09 22:02:34 +0100 | [diff] [blame] | 1458 | void bpf_prog_sub(struct bpf_prog *prog, int i) |
| 1459 | { |
| 1460 | /* Only to be used for undoing previous bpf_prog_add() in some |
| 1461 | * error path. We still know that another entity in our call |
| 1462 | * path holds a reference to the program, thus atomic_sub() can |
| 1463 | * be safely used in such cases! |
| 1464 | */ |
| 1465 | WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0); |
| 1466 | } |
| 1467 | EXPORT_SYMBOL_GPL(bpf_prog_sub); |
| 1468 | |
Brenden Blanco | 59d3656 | 2016-07-19 12:16:46 -0700 | [diff] [blame] | 1469 | struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog) |
| 1470 | { |
| 1471 | return bpf_prog_add(prog, 1); |
| 1472 | } |
Daniel Borkmann | 97bc402 | 2016-11-19 01:45:00 +0100 | [diff] [blame] | 1473 | EXPORT_SYMBOL_GPL(bpf_prog_inc); |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1474 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1475 | /* prog_idr_lock should have been held */ |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1476 | 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] | 1477 | { |
| 1478 | int refold; |
| 1479 | |
Mark Rutland | bfc18e3 | 2018-06-21 13:13:04 +0100 | [diff] [blame] | 1480 | refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1481 | |
| 1482 | if (refold >= BPF_MAX_REFCNT) { |
| 1483 | __bpf_prog_put(prog, false); |
| 1484 | return ERR_PTR(-EBUSY); |
| 1485 | } |
| 1486 | |
| 1487 | if (!refold) |
| 1488 | return ERR_PTR(-ENOENT); |
| 1489 | |
| 1490 | return prog; |
| 1491 | } |
John Fastabend | a6f6df6 | 2017-08-15 22:32:22 -0700 | [diff] [blame] | 1492 | EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1493 | |
Al Viro | 040ee69 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 1494 | bool bpf_prog_get_ok(struct bpf_prog *prog, |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1495 | enum bpf_prog_type *attach_type, bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1496 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1497 | /* not an attachment, just a refcount inc, always allow */ |
| 1498 | if (!attach_type) |
| 1499 | return true; |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1500 | |
| 1501 | if (prog->type != *attach_type) |
| 1502 | return false; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1503 | if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1504 | return false; |
| 1505 | |
| 1506 | return true; |
| 1507 | } |
| 1508 | |
| 1509 | 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] | 1510 | bool attach_drv) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1511 | { |
| 1512 | struct fd f = fdget(ufd); |
| 1513 | struct bpf_prog *prog; |
| 1514 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1515 | prog = ____bpf_prog_get(f); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1516 | if (IS_ERR(prog)) |
| 1517 | return prog; |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1518 | if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) { |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1519 | prog = ERR_PTR(-EINVAL); |
| 1520 | goto out; |
| 1521 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1522 | |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 1523 | prog = bpf_prog_inc(prog); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1524 | out: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1525 | fdput(f); |
| 1526 | return prog; |
| 1527 | } |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1528 | |
| 1529 | struct bpf_prog *bpf_prog_get(u32 ufd) |
| 1530 | { |
Jakub Kicinski | 288b3de | 2017-11-20 15:21:54 -0800 | [diff] [blame] | 1531 | return __bpf_prog_get(ufd, NULL, false); |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1534 | 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] | 1535 | bool attach_drv) |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1536 | { |
Alexei Starovoitov | 4d220ed | 2018-04-28 19:56:37 -0700 | [diff] [blame] | 1537 | return __bpf_prog_get(ufd, &type, attach_drv); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1538 | } |
Jakub Kicinski | 6c8dfe2 | 2017-11-03 13:56:21 -0700 | [diff] [blame] | 1539 | EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev); |
Jakub Kicinski | 248f346 | 2017-11-03 13:56:20 -0700 | [diff] [blame] | 1540 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1541 | /* Initially all BPF programs could be loaded w/o specifying |
| 1542 | * expected_attach_type. Later for some of them specifying expected_attach_type |
| 1543 | * at load time became required so that program could be validated properly. |
| 1544 | * Programs of types that are allowed to be loaded both w/ and w/o (for |
| 1545 | * backward compatibility) expected_attach_type, should have the default attach |
| 1546 | * type assigned to expected_attach_type for the latter case, so that it can be |
| 1547 | * validated later at attach time. |
| 1548 | * |
| 1549 | * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if |
| 1550 | * prog type requires it but has some attach types that have to be backward |
| 1551 | * compatible. |
| 1552 | */ |
| 1553 | static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) |
| 1554 | { |
| 1555 | switch (attr->prog_type) { |
| 1556 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1557 | /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't |
| 1558 | * exist so checking for non-zero is the way to go here. |
| 1559 | */ |
| 1560 | if (!attr->expected_attach_type) |
| 1561 | attr->expected_attach_type = |
| 1562 | BPF_CGROUP_INET_SOCK_CREATE; |
| 1563 | break; |
| 1564 | } |
| 1565 | } |
| 1566 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1567 | static int |
| 1568 | bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, |
| 1569 | enum bpf_attach_type expected_attach_type) |
| 1570 | { |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1571 | switch (prog_type) { |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1572 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1573 | switch (expected_attach_type) { |
| 1574 | case BPF_CGROUP_INET_SOCK_CREATE: |
| 1575 | case BPF_CGROUP_INET4_POST_BIND: |
| 1576 | case BPF_CGROUP_INET6_POST_BIND: |
| 1577 | return 0; |
| 1578 | default: |
| 1579 | return -EINVAL; |
| 1580 | } |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1581 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 1582 | switch (expected_attach_type) { |
| 1583 | case BPF_CGROUP_INET4_BIND: |
| 1584 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1585 | case BPF_CGROUP_INET4_CONNECT: |
| 1586 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 1587 | case BPF_CGROUP_UDP4_SENDMSG: |
| 1588 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 1589 | case BPF_CGROUP_UDP4_RECVMSG: |
| 1590 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1591 | return 0; |
| 1592 | default: |
| 1593 | return -EINVAL; |
| 1594 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 1595 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 1596 | switch (expected_attach_type) { |
| 1597 | case BPF_CGROUP_INET_INGRESS: |
| 1598 | case BPF_CGROUP_INET_EGRESS: |
| 1599 | return 0; |
| 1600 | default: |
| 1601 | return -EINVAL; |
| 1602 | } |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1603 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 1604 | switch (expected_attach_type) { |
| 1605 | case BPF_CGROUP_SETSOCKOPT: |
| 1606 | case BPF_CGROUP_GETSOCKOPT: |
| 1607 | return 0; |
| 1608 | default: |
| 1609 | return -EINVAL; |
| 1610 | } |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1611 | default: |
| 1612 | return 0; |
| 1613 | } |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1616 | /* last field in 'union bpf_attr' used by this command */ |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 1617 | #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1618 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 1619 | static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1620 | { |
| 1621 | enum bpf_prog_type type = attr->prog_type; |
| 1622 | struct bpf_prog *prog; |
| 1623 | int err; |
| 1624 | char license[128]; |
| 1625 | bool is_gpl; |
| 1626 | |
| 1627 | if (CHECK_ATTR(BPF_PROG_LOAD)) |
| 1628 | return -EINVAL; |
| 1629 | |
Jiong Wang | c240eff | 2019-05-24 23:25:16 +0100 | [diff] [blame] | 1630 | if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | |
| 1631 | BPF_F_ANY_ALIGNMENT | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame^] | 1632 | BPF_F_TEST_STATE_FREQ | |
Jiong Wang | c240eff | 2019-05-24 23:25:16 +0100 | [diff] [blame] | 1633 | BPF_F_TEST_RND_HI32)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1634 | return -EINVAL; |
| 1635 | |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 1636 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && |
| 1637 | (attr->prog_flags & BPF_F_ANY_ALIGNMENT) && |
| 1638 | !capable(CAP_SYS_ADMIN)) |
| 1639 | return -EPERM; |
| 1640 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1641 | /* copy eBPF program license from user space */ |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1642 | if (strncpy_from_user(license, u64_to_user_ptr(attr->license), |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1643 | sizeof(license) - 1) < 0) |
| 1644 | return -EFAULT; |
| 1645 | license[sizeof(license) - 1] = 0; |
| 1646 | |
| 1647 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
| 1648 | is_gpl = license_is_gpl_compatible(license); |
| 1649 | |
Alexei Starovoitov | c04c0d2 | 2019-04-01 21:27:45 -0700 | [diff] [blame] | 1650 | if (attr->insn_cnt == 0 || |
| 1651 | attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) |
Daniel Borkmann | ef0915c | 2016-12-07 01:15:44 +0100 | [diff] [blame] | 1652 | return -E2BIG; |
Chenbo Feng | 80b7d81 | 2017-05-31 18:16:00 -0700 | [diff] [blame] | 1653 | if (type != BPF_PROG_TYPE_SOCKET_FILTER && |
| 1654 | type != BPF_PROG_TYPE_CGROUP_SKB && |
| 1655 | !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1656 | return -EPERM; |
| 1657 | |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1658 | bpf_prog_load_fixup_attach_type(attr); |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1659 | if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) |
| 1660 | return -EINVAL; |
| 1661 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1662 | /* plain bpf_prog allocation */ |
| 1663 | prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER); |
| 1664 | if (!prog) |
| 1665 | return -ENOMEM; |
| 1666 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1667 | prog->expected_attach_type = attr->expected_attach_type; |
| 1668 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1669 | prog->aux->offload_requested = !!attr->prog_ifindex; |
| 1670 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1671 | err = security_bpf_prog_alloc(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1672 | if (err) |
| 1673 | goto free_prog_nouncharge; |
| 1674 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1675 | err = bpf_prog_charge_memlock(prog); |
| 1676 | if (err) |
| 1677 | goto free_prog_sec; |
| 1678 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1679 | prog->len = attr->insn_cnt; |
| 1680 | |
| 1681 | err = -EFAULT; |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1682 | if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns), |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 1683 | bpf_prog_insn_size(prog)) != 0) |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1684 | goto free_prog; |
| 1685 | |
| 1686 | prog->orig_prog = NULL; |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1687 | prog->jited = 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1688 | |
| 1689 | atomic_set(&prog->aux->refcnt, 1); |
Daniel Borkmann | a91263d | 2015-09-30 01:41:50 +0200 | [diff] [blame] | 1690 | prog->gpl_compatible = is_gpl ? 1 : 0; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1691 | |
Jakub Kicinski | 9a18eed | 2017-12-27 18:39:04 -0800 | [diff] [blame] | 1692 | if (bpf_prog_is_dev_bound(prog->aux)) { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 1693 | err = bpf_prog_offload_init(prog, attr); |
| 1694 | if (err) |
| 1695 | goto free_prog; |
| 1696 | } |
| 1697 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1698 | /* find program type: socket_filter vs tracing_filter */ |
| 1699 | err = find_prog_type(type, prog); |
| 1700 | if (err < 0) |
| 1701 | goto free_prog; |
| 1702 | |
Jason A. Donenfeld | 9285ec4 | 2019-06-21 22:32:48 +0200 | [diff] [blame] | 1703 | prog->aux->load_time = ktime_get_boottime_ns(); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 1704 | err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name); |
| 1705 | if (err) |
| 1706 | goto free_prog; |
| 1707 | |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1708 | /* run eBPF verifier */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 1709 | err = bpf_check(&prog, attr, uattr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1710 | if (err < 0) |
| 1711 | goto free_used_maps; |
| 1712 | |
Daniel Borkmann | 9facc33 | 2018-06-15 02:30:48 +0200 | [diff] [blame] | 1713 | prog = bpf_prog_select_runtime(prog, &err); |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1714 | if (err < 0) |
| 1715 | goto free_used_maps; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1716 | |
Martin KaFai Lau | dc4bb0e | 2017-06-05 12:15:46 -0700 | [diff] [blame] | 1717 | err = bpf_prog_alloc_id(prog); |
| 1718 | if (err) |
| 1719 | goto free_used_maps; |
| 1720 | |
Daniel Borkmann | aa79781 | 2015-10-29 14:58:06 +0100 | [diff] [blame] | 1721 | err = bpf_prog_new_fd(prog); |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 1722 | if (err < 0) { |
| 1723 | /* failed to allocate fd. |
| 1724 | * bpf_prog_put() is needed because the above |
| 1725 | * bpf_prog_alloc_id() has published the prog |
| 1726 | * to the userspace and the userspace may |
| 1727 | * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID. |
| 1728 | */ |
| 1729 | bpf_prog_put(prog); |
| 1730 | return err; |
| 1731 | } |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1732 | |
Daniel Borkmann | 74451e66 | 2017-02-16 22:24:50 +0100 | [diff] [blame] | 1733 | bpf_prog_kallsyms_add(prog); |
Song Liu | 6ee52e2 | 2019-01-17 08:15:15 -0800 | [diff] [blame] | 1734 | perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1735 | return err; |
| 1736 | |
| 1737 | free_used_maps: |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 1738 | bpf_prog_free_linfo(prog); |
Martin KaFai Lau | 5482e9a | 2018-12-01 17:08:44 -0800 | [diff] [blame] | 1739 | kvfree(prog->aux->func_info); |
| 1740 | btf_put(prog->aux->btf); |
Daniel Borkmann | 7d1982b | 2018-06-15 02:30:47 +0200 | [diff] [blame] | 1741 | bpf_prog_kallsyms_del_subprogs(prog); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1742 | free_used_maps(prog->aux); |
| 1743 | free_prog: |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1744 | bpf_prog_uncharge_memlock(prog); |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 1745 | free_prog_sec: |
| 1746 | security_bpf_prog_free(prog->aux); |
Alexei Starovoitov | aaac3ba | 2015-10-07 22:23:22 -0700 | [diff] [blame] | 1747 | free_prog_nouncharge: |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 1748 | bpf_prog_free(prog); |
| 1749 | return err; |
| 1750 | } |
| 1751 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1752 | #define BPF_OBJ_LAST_FIELD file_flags |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1753 | |
| 1754 | static int bpf_obj_pin(const union bpf_attr *attr) |
| 1755 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1756 | if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1757 | return -EINVAL; |
| 1758 | |
Mickaël Salaün | 535e7b4b | 2016-11-13 19:44:03 +0100 | [diff] [blame] | 1759 | 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] | 1760 | } |
| 1761 | |
| 1762 | static int bpf_obj_get(const union bpf_attr *attr) |
| 1763 | { |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1764 | if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 || |
| 1765 | attr->file_flags & ~BPF_OBJ_FLAG_MASK) |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1766 | return -EINVAL; |
| 1767 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 1768 | return bpf_obj_get_user(u64_to_user_ptr(attr->pathname), |
| 1769 | attr->file_flags); |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 1770 | } |
| 1771 | |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1772 | struct bpf_raw_tracepoint { |
| 1773 | struct bpf_raw_event_map *btp; |
| 1774 | struct bpf_prog *prog; |
| 1775 | }; |
| 1776 | |
| 1777 | static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) |
| 1778 | { |
| 1779 | struct bpf_raw_tracepoint *raw_tp = filp->private_data; |
| 1780 | |
| 1781 | if (raw_tp->prog) { |
| 1782 | bpf_probe_unregister(raw_tp->btp, raw_tp->prog); |
| 1783 | bpf_prog_put(raw_tp->prog); |
| 1784 | } |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1785 | bpf_put_raw_tracepoint(raw_tp->btp); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1786 | kfree(raw_tp); |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| 1790 | static const struct file_operations bpf_raw_tp_fops = { |
| 1791 | .release = bpf_raw_tracepoint_release, |
| 1792 | .read = bpf_dummy_read, |
| 1793 | .write = bpf_dummy_write, |
| 1794 | }; |
| 1795 | |
| 1796 | #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd |
| 1797 | |
| 1798 | static int bpf_raw_tracepoint_open(const union bpf_attr *attr) |
| 1799 | { |
| 1800 | struct bpf_raw_tracepoint *raw_tp; |
| 1801 | struct bpf_raw_event_map *btp; |
| 1802 | struct bpf_prog *prog; |
| 1803 | char tp_name[128]; |
| 1804 | int tp_fd, err; |
| 1805 | |
| 1806 | if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), |
| 1807 | sizeof(tp_name) - 1) < 0) |
| 1808 | return -EFAULT; |
| 1809 | tp_name[sizeof(tp_name) - 1] = 0; |
| 1810 | |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1811 | btp = bpf_get_raw_tracepoint(tp_name); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1812 | if (!btp) |
| 1813 | return -ENOENT; |
| 1814 | |
| 1815 | raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1816 | if (!raw_tp) { |
| 1817 | err = -ENOMEM; |
| 1818 | goto out_put_btp; |
| 1819 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1820 | raw_tp->btp = btp; |
| 1821 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 1822 | prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1823 | if (IS_ERR(prog)) { |
| 1824 | err = PTR_ERR(prog); |
| 1825 | goto out_free_tp; |
| 1826 | } |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 1827 | if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT && |
| 1828 | prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) { |
| 1829 | err = -EINVAL; |
| 1830 | goto out_put_prog; |
| 1831 | } |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1832 | |
| 1833 | err = bpf_probe_register(raw_tp->btp, prog); |
| 1834 | if (err) |
| 1835 | goto out_put_prog; |
| 1836 | |
| 1837 | raw_tp->prog = prog; |
| 1838 | tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, |
| 1839 | O_CLOEXEC); |
| 1840 | if (tp_fd < 0) { |
| 1841 | bpf_probe_unregister(raw_tp->btp, prog); |
| 1842 | err = tp_fd; |
| 1843 | goto out_put_prog; |
| 1844 | } |
| 1845 | return tp_fd; |
| 1846 | |
| 1847 | out_put_prog: |
| 1848 | bpf_prog_put(prog); |
| 1849 | out_free_tp: |
| 1850 | kfree(raw_tp); |
Matt Mullins | a38d110 | 2018-12-12 16:42:37 -0800 | [diff] [blame] | 1851 | out_put_btp: |
| 1852 | bpf_put_raw_tracepoint(btp); |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 1853 | return err; |
| 1854 | } |
| 1855 | |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 1856 | static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, |
| 1857 | enum bpf_attach_type attach_type) |
| 1858 | { |
| 1859 | switch (prog->type) { |
| 1860 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 1861 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1862 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 1863 | return attach_type == prog->expected_attach_type ? 0 : -EINVAL; |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 1864 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 1865 | return prog->enforce_expected_attach_type && |
| 1866 | prog->expected_attach_type != attach_type ? |
| 1867 | -EINVAL : 0; |
Anders Roxell | 3349158 | 2018-04-03 14:09:47 +0200 | [diff] [blame] | 1868 | default: |
| 1869 | return 0; |
| 1870 | } |
| 1871 | } |
| 1872 | |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1873 | #define BPF_PROG_ATTACH_LAST_FIELD attach_flags |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1874 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1875 | #define BPF_F_ATTACH_MASK \ |
| 1876 | (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI) |
| 1877 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1878 | static int bpf_prog_attach(const union bpf_attr *attr) |
| 1879 | { |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1880 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1881 | struct bpf_prog *prog; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1882 | int ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1883 | |
| 1884 | if (!capable(CAP_NET_ADMIN)) |
| 1885 | return -EPERM; |
| 1886 | |
| 1887 | if (CHECK_ATTR(BPF_PROG_ATTACH)) |
| 1888 | return -EINVAL; |
| 1889 | |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1890 | if (attr->attach_flags & ~BPF_F_ATTACH_MASK) |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1891 | return -EINVAL; |
| 1892 | |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1893 | switch (attr->attach_type) { |
| 1894 | case BPF_CGROUP_INET_INGRESS: |
| 1895 | case BPF_CGROUP_INET_EGRESS: |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1896 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1897 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1898 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1899 | case BPF_CGROUP_INET4_POST_BIND: |
| 1900 | case BPF_CGROUP_INET6_POST_BIND: |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1901 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 1902 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1903 | case BPF_CGROUP_INET4_BIND: |
| 1904 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1905 | case BPF_CGROUP_INET4_CONNECT: |
| 1906 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 1907 | case BPF_CGROUP_UDP4_SENDMSG: |
| 1908 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 1909 | case BPF_CGROUP_UDP4_RECVMSG: |
| 1910 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1911 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 1912 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 1913 | case BPF_CGROUP_SOCK_OPS: |
| 1914 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
| 1915 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 1916 | case BPF_CGROUP_DEVICE: |
| 1917 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 1918 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 1919 | case BPF_SK_MSG_VERDICT: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 1920 | ptype = BPF_PROG_TYPE_SK_MSG; |
| 1921 | break; |
John Fastabend | 464bc0f | 2017-08-28 07:10:04 -0700 | [diff] [blame] | 1922 | case BPF_SK_SKB_STREAM_PARSER: |
| 1923 | case BPF_SK_SKB_STREAM_VERDICT: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 1924 | ptype = BPF_PROG_TYPE_SK_SKB; |
| 1925 | break; |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 1926 | case BPF_LIRC_MODE2: |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 1927 | ptype = BPF_PROG_TYPE_LIRC_MODE2; |
| 1928 | break; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1929 | case BPF_FLOW_DISSECTOR: |
| 1930 | ptype = BPF_PROG_TYPE_FLOW_DISSECTOR; |
| 1931 | break; |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 1932 | case BPF_CGROUP_SYSCTL: |
| 1933 | ptype = BPF_PROG_TYPE_CGROUP_SYSCTL; |
| 1934 | break; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 1935 | case BPF_CGROUP_GETSOCKOPT: |
| 1936 | case BPF_CGROUP_SETSOCKOPT: |
| 1937 | ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT; |
| 1938 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1939 | default: |
| 1940 | return -EINVAL; |
| 1941 | } |
| 1942 | |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1943 | prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype); |
| 1944 | if (IS_ERR(prog)) |
| 1945 | return PTR_ERR(prog); |
| 1946 | |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 1947 | if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) { |
| 1948 | bpf_prog_put(prog); |
| 1949 | return -EINVAL; |
| 1950 | } |
| 1951 | |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 1952 | switch (ptype) { |
| 1953 | case BPF_PROG_TYPE_SK_SKB: |
| 1954 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 1955 | ret = sock_map_get_from_fd(attr, prog); |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 1956 | break; |
| 1957 | case BPF_PROG_TYPE_LIRC_MODE2: |
| 1958 | ret = lirc_prog_attach(attr, prog); |
| 1959 | break; |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 1960 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 1961 | ret = skb_flow_dissector_bpf_prog_attach(attr, prog); |
| 1962 | break; |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 1963 | default: |
| 1964 | ret = cgroup_bpf_prog_attach(attr, ptype, prog); |
David Ahern | b2cd125 | 2016-12-01 08:48:03 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1967 | if (ret) |
| 1968 | bpf_prog_put(prog); |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 1969 | return ret; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | #define BPF_PROG_DETACH_LAST_FIELD attach_type |
| 1973 | |
| 1974 | static int bpf_prog_detach(const union bpf_attr *attr) |
| 1975 | { |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1976 | enum bpf_prog_type ptype; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 1977 | |
| 1978 | if (!capable(CAP_NET_ADMIN)) |
| 1979 | return -EPERM; |
| 1980 | |
| 1981 | if (CHECK_ATTR(BPF_PROG_DETACH)) |
| 1982 | return -EINVAL; |
| 1983 | |
| 1984 | switch (attr->attach_type) { |
| 1985 | case BPF_CGROUP_INET_INGRESS: |
| 1986 | case BPF_CGROUP_INET_EGRESS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1987 | ptype = BPF_PROG_TYPE_CGROUP_SKB; |
| 1988 | break; |
David Ahern | 61023658 | 2016-12-01 08:48:04 -0800 | [diff] [blame] | 1989 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 1990 | case BPF_CGROUP_INET4_POST_BIND: |
| 1991 | case BPF_CGROUP_INET6_POST_BIND: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 1992 | ptype = BPF_PROG_TYPE_CGROUP_SOCK; |
| 1993 | break; |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 1994 | case BPF_CGROUP_INET4_BIND: |
| 1995 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 1996 | case BPF_CGROUP_INET4_CONNECT: |
| 1997 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 1998 | case BPF_CGROUP_UDP4_SENDMSG: |
| 1999 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 2000 | case BPF_CGROUP_UDP4_RECVMSG: |
| 2001 | case BPF_CGROUP_UDP6_RECVMSG: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2002 | ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; |
| 2003 | break; |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 2004 | case BPF_CGROUP_SOCK_OPS: |
Alexei Starovoitov | 324bda9e6 | 2017-10-02 22:50:21 -0700 | [diff] [blame] | 2005 | ptype = BPF_PROG_TYPE_SOCK_OPS; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2006 | break; |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 2007 | case BPF_CGROUP_DEVICE: |
| 2008 | ptype = BPF_PROG_TYPE_CGROUP_DEVICE; |
| 2009 | break; |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 2010 | case BPF_SK_MSG_VERDICT: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 2011 | return sock_map_get_from_fd(attr, NULL); |
John Fastabend | 5a67da2 | 2017-09-08 14:00:49 -0700 | [diff] [blame] | 2012 | case BPF_SK_SKB_STREAM_PARSER: |
| 2013 | case BPF_SK_SKB_STREAM_VERDICT: |
Daniel Borkmann | 604326b | 2018-10-13 02:45:58 +0200 | [diff] [blame] | 2014 | return sock_map_get_from_fd(attr, NULL); |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 2015 | case BPF_LIRC_MODE2: |
| 2016 | return lirc_prog_detach(attr); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2017 | case BPF_FLOW_DISSECTOR: |
| 2018 | return skb_flow_dissector_bpf_prog_detach(attr); |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 2019 | case BPF_CGROUP_SYSCTL: |
| 2020 | ptype = BPF_PROG_TYPE_CGROUP_SYSCTL; |
| 2021 | break; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2022 | case BPF_CGROUP_GETSOCKOPT: |
| 2023 | case BPF_CGROUP_SETSOCKOPT: |
| 2024 | ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT; |
| 2025 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2026 | default: |
| 2027 | return -EINVAL; |
| 2028 | } |
| 2029 | |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2030 | return cgroup_bpf_prog_detach(attr, ptype); |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2031 | } |
Lawrence Brakmo | 40304b2 | 2017-06-30 20:02:40 -0700 | [diff] [blame] | 2032 | |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2033 | #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt |
| 2034 | |
| 2035 | static int bpf_prog_query(const union bpf_attr *attr, |
| 2036 | union bpf_attr __user *uattr) |
| 2037 | { |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2038 | if (!capable(CAP_NET_ADMIN)) |
| 2039 | return -EPERM; |
| 2040 | if (CHECK_ATTR(BPF_PROG_QUERY)) |
| 2041 | return -EINVAL; |
| 2042 | if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE) |
| 2043 | return -EINVAL; |
| 2044 | |
| 2045 | switch (attr->query.attach_type) { |
| 2046 | case BPF_CGROUP_INET_INGRESS: |
| 2047 | case BPF_CGROUP_INET_EGRESS: |
| 2048 | case BPF_CGROUP_INET_SOCK_CREATE: |
Andrey Ignatov | 4fbac77 | 2018-03-30 15:08:02 -0700 | [diff] [blame] | 2049 | case BPF_CGROUP_INET4_BIND: |
| 2050 | case BPF_CGROUP_INET6_BIND: |
Andrey Ignatov | aac3fc3 | 2018-03-30 15:08:07 -0700 | [diff] [blame] | 2051 | case BPF_CGROUP_INET4_POST_BIND: |
| 2052 | case BPF_CGROUP_INET6_POST_BIND: |
Andrey Ignatov | d74bad4 | 2018-03-30 15:08:05 -0700 | [diff] [blame] | 2053 | case BPF_CGROUP_INET4_CONNECT: |
| 2054 | case BPF_CGROUP_INET6_CONNECT: |
Andrey Ignatov | 1cedee1 | 2018-05-25 08:55:23 -0700 | [diff] [blame] | 2055 | case BPF_CGROUP_UDP4_SENDMSG: |
| 2056 | case BPF_CGROUP_UDP6_SENDMSG: |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 2057 | case BPF_CGROUP_UDP4_RECVMSG: |
| 2058 | case BPF_CGROUP_UDP6_RECVMSG: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2059 | case BPF_CGROUP_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 2060 | case BPF_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 2061 | case BPF_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 2062 | case BPF_CGROUP_GETSOCKOPT: |
| 2063 | case BPF_CGROUP_SETSOCKOPT: |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2064 | break; |
Sean Young | f4364dc | 2018-05-27 12:24:09 +0100 | [diff] [blame] | 2065 | case BPF_LIRC_MODE2: |
| 2066 | return lirc_prog_query(attr, uattr); |
Stanislav Fomichev | 118c8e9 | 2019-04-25 14:37:23 -0700 | [diff] [blame] | 2067 | case BPF_FLOW_DISSECTOR: |
| 2068 | return skb_flow_dissector_prog_query(attr, uattr); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2069 | default: |
| 2070 | return -EINVAL; |
| 2071 | } |
Sean Young | fdb5c45 | 2018-06-19 00:04:24 +0100 | [diff] [blame] | 2072 | |
| 2073 | return cgroup_bpf_prog_query(attr, uattr); |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2074 | } |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2075 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 2076 | #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2077 | |
| 2078 | static int bpf_prog_test_run(const union bpf_attr *attr, |
| 2079 | union bpf_attr __user *uattr) |
| 2080 | { |
| 2081 | struct bpf_prog *prog; |
| 2082 | int ret = -ENOTSUPP; |
| 2083 | |
Alexei Starovoitov | 61f3c96 | 2018-01-17 16:52:02 -0800 | [diff] [blame] | 2084 | if (!capable(CAP_SYS_ADMIN)) |
| 2085 | return -EPERM; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2086 | if (CHECK_ATTR(BPF_PROG_TEST_RUN)) |
| 2087 | return -EINVAL; |
| 2088 | |
Stanislav Fomichev | b0b9395 | 2019-04-09 11:49:09 -0700 | [diff] [blame] | 2089 | if ((attr->test.ctx_size_in && !attr->test.ctx_in) || |
| 2090 | (!attr->test.ctx_size_in && attr->test.ctx_in)) |
| 2091 | return -EINVAL; |
| 2092 | |
| 2093 | if ((attr->test.ctx_size_out && !attr->test.ctx_out) || |
| 2094 | (!attr->test.ctx_size_out && attr->test.ctx_out)) |
| 2095 | return -EINVAL; |
| 2096 | |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2097 | prog = bpf_prog_get(attr->test.prog_fd); |
| 2098 | if (IS_ERR(prog)) |
| 2099 | return PTR_ERR(prog); |
| 2100 | |
| 2101 | if (prog->aux->ops->test_run) |
| 2102 | ret = prog->aux->ops->test_run(prog, attr, uattr); |
| 2103 | |
| 2104 | bpf_prog_put(prog); |
| 2105 | return ret; |
| 2106 | } |
| 2107 | |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 2108 | #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id |
| 2109 | |
| 2110 | static int bpf_obj_get_next_id(const union bpf_attr *attr, |
| 2111 | union bpf_attr __user *uattr, |
| 2112 | struct idr *idr, |
| 2113 | spinlock_t *lock) |
| 2114 | { |
| 2115 | u32 next_id = attr->start_id; |
| 2116 | int err = 0; |
| 2117 | |
| 2118 | if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX) |
| 2119 | return -EINVAL; |
| 2120 | |
| 2121 | if (!capable(CAP_SYS_ADMIN)) |
| 2122 | return -EPERM; |
| 2123 | |
| 2124 | next_id++; |
| 2125 | spin_lock_bh(lock); |
| 2126 | if (!idr_get_next(idr, &next_id)) |
| 2127 | err = -ENOENT; |
| 2128 | spin_unlock_bh(lock); |
| 2129 | |
| 2130 | if (!err) |
| 2131 | err = put_user(next_id, &uattr->next_id); |
| 2132 | |
| 2133 | return err; |
| 2134 | } |
| 2135 | |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 2136 | #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id |
| 2137 | |
| 2138 | static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) |
| 2139 | { |
| 2140 | struct bpf_prog *prog; |
| 2141 | u32 id = attr->prog_id; |
| 2142 | int fd; |
| 2143 | |
| 2144 | if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) |
| 2145 | return -EINVAL; |
| 2146 | |
| 2147 | if (!capable(CAP_SYS_ADMIN)) |
| 2148 | return -EPERM; |
| 2149 | |
| 2150 | spin_lock_bh(&prog_idr_lock); |
| 2151 | prog = idr_find(&prog_idr, id); |
| 2152 | if (prog) |
| 2153 | prog = bpf_prog_inc_not_zero(prog); |
| 2154 | else |
| 2155 | prog = ERR_PTR(-ENOENT); |
| 2156 | spin_unlock_bh(&prog_idr_lock); |
| 2157 | |
| 2158 | if (IS_ERR(prog)) |
| 2159 | return PTR_ERR(prog); |
| 2160 | |
| 2161 | fd = bpf_prog_new_fd(prog); |
| 2162 | if (fd < 0) |
| 2163 | bpf_prog_put(prog); |
| 2164 | |
| 2165 | return fd; |
| 2166 | } |
| 2167 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2168 | #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2169 | |
| 2170 | static int bpf_map_get_fd_by_id(const union bpf_attr *attr) |
| 2171 | { |
| 2172 | struct bpf_map *map; |
| 2173 | u32 id = attr->map_id; |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2174 | int f_flags; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2175 | int fd; |
| 2176 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2177 | if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) || |
| 2178 | attr->open_flags & ~BPF_OBJ_FLAG_MASK) |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2179 | return -EINVAL; |
| 2180 | |
| 2181 | if (!capable(CAP_SYS_ADMIN)) |
| 2182 | return -EPERM; |
| 2183 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2184 | f_flags = bpf_get_file_flag(attr->open_flags); |
| 2185 | if (f_flags < 0) |
| 2186 | return f_flags; |
| 2187 | |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2188 | spin_lock_bh(&map_idr_lock); |
| 2189 | map = idr_find(&map_idr, id); |
| 2190 | if (map) |
Stanislav Fomichev | b0e4701 | 2019-08-14 10:37:48 -0700 | [diff] [blame] | 2191 | map = __bpf_map_inc_not_zero(map, true); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2192 | else |
| 2193 | map = ERR_PTR(-ENOENT); |
| 2194 | spin_unlock_bh(&map_idr_lock); |
| 2195 | |
| 2196 | if (IS_ERR(map)) |
| 2197 | return PTR_ERR(map); |
| 2198 | |
Chenbo Feng | 6e71b04 | 2017-10-18 13:00:22 -0700 | [diff] [blame] | 2199 | fd = bpf_map_new_fd(map, f_flags); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2200 | if (fd < 0) |
Peng Sun | 781e628 | 2019-02-26 22:15:37 +0800 | [diff] [blame] | 2201 | bpf_map_put_with_uref(map); |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2202 | |
| 2203 | return fd; |
| 2204 | } |
| 2205 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2206 | static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog, |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2207 | unsigned long addr, u32 *off, |
| 2208 | u32 *type) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2209 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2210 | const struct bpf_map *map; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2211 | int i; |
| 2212 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2213 | for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) { |
| 2214 | map = prog->aux->used_maps[i]; |
| 2215 | if (map == (void *)addr) { |
| 2216 | *type = BPF_PSEUDO_MAP_FD; |
| 2217 | return map; |
| 2218 | } |
| 2219 | if (!map->ops->map_direct_value_meta) |
| 2220 | continue; |
| 2221 | if (!map->ops->map_direct_value_meta(map, addr, off)) { |
| 2222 | *type = BPF_PSEUDO_MAP_VALUE; |
| 2223 | return map; |
| 2224 | } |
| 2225 | } |
| 2226 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2227 | return NULL; |
| 2228 | } |
| 2229 | |
| 2230 | static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog) |
| 2231 | { |
| 2232 | const struct bpf_map *map; |
| 2233 | struct bpf_insn *insns; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2234 | u32 off, type; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2235 | u64 imm; |
| 2236 | int i; |
| 2237 | |
| 2238 | insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog), |
| 2239 | GFP_USER); |
| 2240 | if (!insns) |
| 2241 | return insns; |
| 2242 | |
| 2243 | for (i = 0; i < prog->len; i++) { |
| 2244 | if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) { |
| 2245 | insns[i].code = BPF_JMP | BPF_CALL; |
| 2246 | insns[i].imm = BPF_FUNC_tail_call; |
| 2247 | /* fall-through */ |
| 2248 | } |
| 2249 | if (insns[i].code == (BPF_JMP | BPF_CALL) || |
| 2250 | insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) { |
| 2251 | if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) |
| 2252 | insns[i].code = BPF_JMP | BPF_CALL; |
| 2253 | if (!bpf_dump_raw_ok()) |
| 2254 | insns[i].imm = 0; |
| 2255 | continue; |
| 2256 | } |
| 2257 | |
| 2258 | if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW)) |
| 2259 | continue; |
| 2260 | |
| 2261 | imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2262 | map = bpf_map_from_imm(prog, imm, &off, &type); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2263 | if (map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2264 | insns[i].src_reg = type; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2265 | insns[i].imm = map->id; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 2266 | insns[i + 1].imm = off; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2267 | continue; |
| 2268 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | return insns; |
| 2272 | } |
| 2273 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2274 | static int set_info_rec_size(struct bpf_prog_info *info) |
| 2275 | { |
| 2276 | /* |
| 2277 | * Ensure info.*_rec_size is the same as kernel expected size |
| 2278 | * |
| 2279 | * or |
| 2280 | * |
| 2281 | * Only allow zero *_rec_size if both _rec_size and _cnt are |
| 2282 | * zero. In this case, the kernel will set the expected |
| 2283 | * _rec_size back to the info. |
| 2284 | */ |
| 2285 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2286 | if ((info->nr_func_info || info->func_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2287 | info->func_info_rec_size != sizeof(struct bpf_func_info)) |
| 2288 | return -EINVAL; |
| 2289 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2290 | if ((info->nr_line_info || info->line_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2291 | info->line_info_rec_size != sizeof(struct bpf_line_info)) |
| 2292 | return -EINVAL; |
| 2293 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2294 | if ((info->nr_jited_line_info || info->jited_line_info_rec_size) && |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2295 | info->jited_line_info_rec_size != sizeof(__u64)) |
| 2296 | return -EINVAL; |
| 2297 | |
| 2298 | info->func_info_rec_size = sizeof(struct bpf_func_info); |
| 2299 | info->line_info_rec_size = sizeof(struct bpf_line_info); |
| 2300 | info->jited_line_info_rec_size = sizeof(__u64); |
| 2301 | |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2305 | static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, |
| 2306 | const union bpf_attr *attr, |
| 2307 | union bpf_attr __user *uattr) |
| 2308 | { |
| 2309 | struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 2310 | struct bpf_prog_info info = {}; |
| 2311 | u32 info_len = attr->info.info_len; |
Alexei Starovoitov | 5f8f8b9 | 2019-02-25 14:28:40 -0800 | [diff] [blame] | 2312 | struct bpf_prog_stats stats; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2313 | char __user *uinsns; |
| 2314 | u32 ulen; |
| 2315 | int err; |
| 2316 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 2317 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2318 | if (err) |
| 2319 | return err; |
| 2320 | info_len = min_t(u32, sizeof(info), info_len); |
| 2321 | |
| 2322 | if (copy_from_user(&info, uinfo, info_len)) |
Daniel Borkmann | 89b0968 | 2017-07-27 21:02:46 +0200 | [diff] [blame] | 2323 | return -EFAULT; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2324 | |
| 2325 | info.type = prog->type; |
| 2326 | info.id = prog->aux->id; |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 2327 | info.load_time = prog->aux->load_time; |
| 2328 | info.created_by_uid = from_kuid_munged(current_user_ns(), |
| 2329 | prog->aux->user->uid); |
Jiri Olsa | b85fab0 | 2018-04-25 19:41:06 +0200 | [diff] [blame] | 2330 | info.gpl_compatible = prog->gpl_compatible; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2331 | |
| 2332 | memcpy(info.tag, prog->tag, sizeof(prog->tag)); |
Martin KaFai Lau | cb4d2b3 | 2017-09-27 14:37:52 -0700 | [diff] [blame] | 2333 | memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); |
| 2334 | |
| 2335 | ulen = info.nr_map_ids; |
| 2336 | info.nr_map_ids = prog->aux->used_map_cnt; |
| 2337 | ulen = min_t(u32, info.nr_map_ids, ulen); |
| 2338 | if (ulen) { |
Martin KaFai Lau | 721e08d | 2017-09-29 10:52:17 -0700 | [diff] [blame] | 2339 | 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] | 2340 | u32 i; |
| 2341 | |
| 2342 | for (i = 0; i < ulen; i++) |
| 2343 | if (put_user(prog->aux->used_maps[i]->id, |
| 2344 | &user_map_ids[i])) |
| 2345 | return -EFAULT; |
| 2346 | } |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2347 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2348 | err = set_info_rec_size(&info); |
| 2349 | if (err) |
| 2350 | return err; |
Martin KaFai Lau | 7337224 | 2018-12-05 17:35:43 -0800 | [diff] [blame] | 2351 | |
Alexei Starovoitov | 5f8f8b9 | 2019-02-25 14:28:40 -0800 | [diff] [blame] | 2352 | bpf_prog_get_stats(prog, &stats); |
| 2353 | info.run_time_ns = stats.nsecs; |
| 2354 | info.run_cnt = stats.cnt; |
| 2355 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2356 | if (!capable(CAP_SYS_ADMIN)) { |
| 2357 | info.jited_prog_len = 0; |
| 2358 | info.xlated_prog_len = 0; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2359 | info.nr_jited_ksyms = 0; |
Daniel Borkmann | 28c2fae | 2018-11-02 11:35:46 +0100 | [diff] [blame] | 2360 | info.nr_jited_func_lens = 0; |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2361 | info.nr_func_info = 0; |
| 2362 | info.nr_line_info = 0; |
| 2363 | info.nr_jited_line_info = 0; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2364 | goto done; |
| 2365 | } |
| 2366 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2367 | ulen = info.xlated_prog_len; |
Daniel Borkmann | 9975a54 | 2017-07-28 17:05:25 +0200 | [diff] [blame] | 2368 | info.xlated_prog_len = bpf_prog_insn_size(prog); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2369 | if (info.xlated_prog_len && ulen) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2370 | struct bpf_insn *insns_sanitized; |
| 2371 | bool fault; |
| 2372 | |
| 2373 | if (prog->blinded && !bpf_dump_raw_ok()) { |
| 2374 | info.xlated_prog_insns = 0; |
| 2375 | goto done; |
| 2376 | } |
| 2377 | insns_sanitized = bpf_insn_prepare_dump(prog); |
| 2378 | if (!insns_sanitized) |
| 2379 | return -ENOMEM; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2380 | uinsns = u64_to_user_ptr(info.xlated_prog_insns); |
| 2381 | ulen = min_t(u32, info.xlated_prog_len, ulen); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 2382 | fault = copy_to_user(uinsns, insns_sanitized, ulen); |
| 2383 | kfree(insns_sanitized); |
| 2384 | if (fault) |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2385 | return -EFAULT; |
| 2386 | } |
| 2387 | |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 2388 | if (bpf_prog_is_dev_bound(prog->aux)) { |
| 2389 | err = bpf_prog_offload_info_fill(&info, prog); |
| 2390 | if (err) |
| 2391 | return err; |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 2392 | goto done; |
| 2393 | } |
| 2394 | |
| 2395 | /* NOTE: the following code is supposed to be skipped for offload. |
| 2396 | * bpf_prog_offload_info_fill() is the place to fill similar fields |
| 2397 | * for offload. |
| 2398 | */ |
| 2399 | ulen = info.jited_prog_len; |
Sandipan Das | 4d56a76 | 2018-05-24 12:26:51 +0530 | [diff] [blame] | 2400 | if (prog->aux->func_cnt) { |
| 2401 | u32 i; |
| 2402 | |
| 2403 | info.jited_prog_len = 0; |
| 2404 | for (i = 0; i < prog->aux->func_cnt; i++) |
| 2405 | info.jited_prog_len += prog->aux->func[i]->jited_len; |
| 2406 | } else { |
| 2407 | info.jited_prog_len = prog->jited_len; |
| 2408 | } |
| 2409 | |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 2410 | if (info.jited_prog_len && ulen) { |
| 2411 | if (bpf_dump_raw_ok()) { |
| 2412 | uinsns = u64_to_user_ptr(info.jited_prog_insns); |
| 2413 | ulen = min_t(u32, info.jited_prog_len, ulen); |
Sandipan Das | 4d56a76 | 2018-05-24 12:26:51 +0530 | [diff] [blame] | 2414 | |
| 2415 | /* for multi-function programs, copy the JITed |
| 2416 | * instructions for all the functions |
| 2417 | */ |
| 2418 | if (prog->aux->func_cnt) { |
| 2419 | u32 len, free, i; |
| 2420 | u8 *img; |
| 2421 | |
| 2422 | free = ulen; |
| 2423 | for (i = 0; i < prog->aux->func_cnt; i++) { |
| 2424 | len = prog->aux->func[i]->jited_len; |
| 2425 | len = min_t(u32, len, free); |
| 2426 | img = (u8 *) prog->aux->func[i]->bpf_func; |
| 2427 | if (copy_to_user(uinsns, img, len)) |
| 2428 | return -EFAULT; |
| 2429 | uinsns += len; |
| 2430 | free -= len; |
| 2431 | if (!free) |
| 2432 | break; |
| 2433 | } |
| 2434 | } else { |
| 2435 | if (copy_to_user(uinsns, prog->bpf_func, ulen)) |
| 2436 | return -EFAULT; |
| 2437 | } |
Jiong Wang | fcfb126 | 2018-01-16 16:05:19 -0800 | [diff] [blame] | 2438 | } else { |
| 2439 | info.jited_prog_insns = 0; |
| 2440 | } |
Jakub Kicinski | 675fc27 | 2017-12-27 18:39:09 -0800 | [diff] [blame] | 2441 | } |
| 2442 | |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2443 | ulen = info.nr_jited_ksyms; |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2444 | info.nr_jited_ksyms = prog->aux->func_cnt ? : 1; |
Song Liu | 7a5725d | 2018-12-10 11:17:50 -0800 | [diff] [blame] | 2445 | if (ulen) { |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2446 | if (bpf_dump_raw_ok()) { |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2447 | unsigned long ksym_addr; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2448 | u64 __user *user_ksyms; |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2449 | u32 i; |
| 2450 | |
| 2451 | /* copy the address of the kernel symbol |
| 2452 | * corresponding to each function |
| 2453 | */ |
| 2454 | ulen = min_t(u32, info.nr_jited_ksyms, ulen); |
| 2455 | user_ksyms = u64_to_user_ptr(info.jited_ksyms); |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2456 | if (prog->aux->func_cnt) { |
| 2457 | for (i = 0; i < ulen; i++) { |
| 2458 | ksym_addr = (unsigned long) |
| 2459 | prog->aux->func[i]->bpf_func; |
| 2460 | if (put_user((u64) ksym_addr, |
| 2461 | &user_ksyms[i])) |
| 2462 | return -EFAULT; |
| 2463 | } |
| 2464 | } else { |
| 2465 | ksym_addr = (unsigned long) prog->bpf_func; |
| 2466 | if (put_user((u64) ksym_addr, &user_ksyms[0])) |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 2467 | return -EFAULT; |
| 2468 | } |
| 2469 | } else { |
| 2470 | info.jited_ksyms = 0; |
| 2471 | } |
| 2472 | } |
| 2473 | |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 2474 | ulen = info.nr_jited_func_lens; |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2475 | info.nr_jited_func_lens = prog->aux->func_cnt ? : 1; |
Song Liu | 7a5725d | 2018-12-10 11:17:50 -0800 | [diff] [blame] | 2476 | if (ulen) { |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 2477 | if (bpf_dump_raw_ok()) { |
| 2478 | u32 __user *user_lens; |
| 2479 | u32 func_len, i; |
| 2480 | |
| 2481 | /* copy the JITed image lengths for each function */ |
| 2482 | ulen = min_t(u32, info.nr_jited_func_lens, ulen); |
| 2483 | user_lens = u64_to_user_ptr(info.jited_func_lens); |
Song Liu | ff1889f | 2018-11-02 10:16:17 -0700 | [diff] [blame] | 2484 | if (prog->aux->func_cnt) { |
| 2485 | for (i = 0; i < ulen; i++) { |
| 2486 | func_len = |
| 2487 | prog->aux->func[i]->jited_len; |
| 2488 | if (put_user(func_len, &user_lens[i])) |
| 2489 | return -EFAULT; |
| 2490 | } |
| 2491 | } else { |
| 2492 | func_len = prog->jited_len; |
| 2493 | if (put_user(func_len, &user_lens[0])) |
Sandipan Das | 815581c | 2018-05-24 12:26:52 +0530 | [diff] [blame] | 2494 | return -EFAULT; |
| 2495 | } |
| 2496 | } else { |
| 2497 | info.jited_func_lens = 0; |
| 2498 | } |
| 2499 | } |
| 2500 | |
Martin KaFai Lau | 7337224 | 2018-12-05 17:35:43 -0800 | [diff] [blame] | 2501 | if (prog->aux->btf) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2502 | info.btf_id = btf_id(prog->aux->btf); |
| 2503 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2504 | ulen = info.nr_func_info; |
| 2505 | info.nr_func_info = prog->aux->func_info_cnt; |
| 2506 | if (info.nr_func_info && ulen) { |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2507 | char __user *user_finfo; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2508 | |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2509 | user_finfo = u64_to_user_ptr(info.func_info); |
| 2510 | ulen = min_t(u32, info.nr_func_info, ulen); |
| 2511 | if (copy_to_user(user_finfo, prog->aux->func_info, |
| 2512 | info.func_info_rec_size * ulen)) |
| 2513 | return -EFAULT; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2514 | } |
| 2515 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2516 | ulen = info.nr_line_info; |
| 2517 | info.nr_line_info = prog->aux->nr_linfo; |
| 2518 | if (info.nr_line_info && ulen) { |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2519 | __u8 __user *user_linfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2520 | |
Martin KaFai Lau | 9e79416 | 2018-12-12 10:18:21 -0800 | [diff] [blame] | 2521 | user_linfo = u64_to_user_ptr(info.line_info); |
| 2522 | ulen = min_t(u32, info.nr_line_info, ulen); |
| 2523 | if (copy_to_user(user_linfo, prog->aux->linfo, |
| 2524 | info.line_info_rec_size * ulen)) |
| 2525 | return -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2526 | } |
| 2527 | |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2528 | ulen = info.nr_jited_line_info; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2529 | if (prog->aux->jited_linfo) |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2530 | info.nr_jited_line_info = prog->aux->nr_linfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2531 | else |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2532 | info.nr_jited_line_info = 0; |
| 2533 | if (info.nr_jited_line_info && ulen) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2534 | if (bpf_dump_raw_ok()) { |
| 2535 | __u64 __user *user_linfo; |
| 2536 | u32 i; |
| 2537 | |
| 2538 | user_linfo = u64_to_user_ptr(info.jited_line_info); |
Yonghong Song | 11d8b82 | 2018-12-10 14:14:08 -0800 | [diff] [blame] | 2539 | ulen = min_t(u32, info.nr_jited_line_info, ulen); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 2540 | for (i = 0; i < ulen; i++) { |
| 2541 | if (put_user((__u64)(long)prog->aux->jited_linfo[i], |
| 2542 | &user_linfo[i])) |
| 2543 | return -EFAULT; |
| 2544 | } |
| 2545 | } else { |
| 2546 | info.jited_line_info = 0; |
| 2547 | } |
| 2548 | } |
| 2549 | |
Song Liu | c872bdb | 2018-12-12 09:37:46 -0800 | [diff] [blame] | 2550 | ulen = info.nr_prog_tags; |
| 2551 | info.nr_prog_tags = prog->aux->func_cnt ? : 1; |
| 2552 | if (ulen) { |
| 2553 | __u8 __user (*user_prog_tags)[BPF_TAG_SIZE]; |
| 2554 | u32 i; |
| 2555 | |
| 2556 | user_prog_tags = u64_to_user_ptr(info.prog_tags); |
| 2557 | ulen = min_t(u32, info.nr_prog_tags, ulen); |
| 2558 | if (prog->aux->func_cnt) { |
| 2559 | for (i = 0; i < ulen; i++) { |
| 2560 | if (copy_to_user(user_prog_tags[i], |
| 2561 | prog->aux->func[i]->tag, |
| 2562 | BPF_TAG_SIZE)) |
| 2563 | return -EFAULT; |
| 2564 | } |
| 2565 | } else { |
| 2566 | if (copy_to_user(user_prog_tags[0], |
| 2567 | prog->tag, BPF_TAG_SIZE)) |
| 2568 | return -EFAULT; |
| 2569 | } |
| 2570 | } |
| 2571 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2572 | done: |
| 2573 | if (copy_to_user(uinfo, &info, info_len) || |
| 2574 | put_user(info_len, &uattr->info.info_len)) |
| 2575 | return -EFAULT; |
| 2576 | |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
| 2580 | static int bpf_map_get_info_by_fd(struct bpf_map *map, |
| 2581 | const union bpf_attr *attr, |
| 2582 | union bpf_attr __user *uattr) |
| 2583 | { |
| 2584 | struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 2585 | struct bpf_map_info info = {}; |
| 2586 | u32 info_len = attr->info.info_len; |
| 2587 | int err; |
| 2588 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 2589 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2590 | if (err) |
| 2591 | return err; |
| 2592 | info_len = min_t(u32, sizeof(info), info_len); |
| 2593 | |
| 2594 | info.type = map->map_type; |
| 2595 | info.id = map->id; |
| 2596 | info.key_size = map->key_size; |
| 2597 | info.value_size = map->value_size; |
| 2598 | info.max_entries = map->max_entries; |
| 2599 | info.map_flags = map->map_flags; |
Martin KaFai Lau | ad5b177 | 2017-09-27 14:37:53 -0700 | [diff] [blame] | 2600 | memcpy(info.name, map->name, sizeof(map->name)); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2601 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2602 | if (map->btf) { |
| 2603 | info.btf_id = btf_id(map->btf); |
Martin KaFai Lau | 9b2cf32 | 2018-05-22 14:57:21 -0700 | [diff] [blame] | 2604 | info.btf_key_type_id = map->btf_key_type_id; |
| 2605 | info.btf_value_type_id = map->btf_value_type_id; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2606 | } |
| 2607 | |
Jakub Kicinski | 52775b3 | 2018-01-17 19:13:28 -0800 | [diff] [blame] | 2608 | if (bpf_map_is_dev_bound(map)) { |
| 2609 | err = bpf_map_offload_info_fill(&info, map); |
| 2610 | if (err) |
| 2611 | return err; |
| 2612 | } |
| 2613 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2614 | if (copy_to_user(uinfo, &info, info_len) || |
| 2615 | put_user(info_len, &uattr->info.info_len)) |
| 2616 | return -EFAULT; |
| 2617 | |
| 2618 | return 0; |
| 2619 | } |
| 2620 | |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 2621 | static int bpf_btf_get_info_by_fd(struct btf *btf, |
| 2622 | const union bpf_attr *attr, |
| 2623 | union bpf_attr __user *uattr) |
| 2624 | { |
| 2625 | struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info); |
| 2626 | u32 info_len = attr->info.info_len; |
| 2627 | int err; |
| 2628 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 2629 | err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len); |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 2630 | if (err) |
| 2631 | return err; |
| 2632 | |
| 2633 | return btf_get_info_by_fd(btf, attr, uattr); |
| 2634 | } |
| 2635 | |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2636 | #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info |
| 2637 | |
| 2638 | static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, |
| 2639 | union bpf_attr __user *uattr) |
| 2640 | { |
| 2641 | int ufd = attr->info.bpf_fd; |
| 2642 | struct fd f; |
| 2643 | int err; |
| 2644 | |
| 2645 | if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD)) |
| 2646 | return -EINVAL; |
| 2647 | |
| 2648 | f = fdget(ufd); |
| 2649 | if (!f.file) |
| 2650 | return -EBADFD; |
| 2651 | |
| 2652 | if (f.file->f_op == &bpf_prog_fops) |
| 2653 | err = bpf_prog_get_info_by_fd(f.file->private_data, attr, |
| 2654 | uattr); |
| 2655 | else if (f.file->f_op == &bpf_map_fops) |
| 2656 | err = bpf_map_get_info_by_fd(f.file->private_data, attr, |
| 2657 | uattr); |
Martin KaFai Lau | 60197cf | 2018-04-18 15:56:02 -0700 | [diff] [blame] | 2658 | else if (f.file->f_op == &btf_fops) |
Martin KaFai Lau | 62dab84 | 2018-05-04 14:49:52 -0700 | [diff] [blame] | 2659 | err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2660 | else |
| 2661 | err = -EINVAL; |
| 2662 | |
| 2663 | fdput(f); |
| 2664 | return err; |
| 2665 | } |
| 2666 | |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 2667 | #define BPF_BTF_LOAD_LAST_FIELD btf_log_level |
| 2668 | |
| 2669 | static int bpf_btf_load(const union bpf_attr *attr) |
| 2670 | { |
| 2671 | if (CHECK_ATTR(BPF_BTF_LOAD)) |
| 2672 | return -EINVAL; |
| 2673 | |
| 2674 | if (!capable(CAP_SYS_ADMIN)) |
| 2675 | return -EPERM; |
| 2676 | |
| 2677 | return btf_new_fd(attr); |
| 2678 | } |
| 2679 | |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2680 | #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id |
| 2681 | |
| 2682 | static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) |
| 2683 | { |
| 2684 | if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID)) |
| 2685 | return -EINVAL; |
| 2686 | |
| 2687 | if (!capable(CAP_SYS_ADMIN)) |
| 2688 | return -EPERM; |
| 2689 | |
| 2690 | return btf_get_fd_by_id(attr->btf_id); |
| 2691 | } |
| 2692 | |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 2693 | static int bpf_task_fd_query_copy(const union bpf_attr *attr, |
| 2694 | union bpf_attr __user *uattr, |
| 2695 | u32 prog_id, u32 fd_type, |
| 2696 | const char *buf, u64 probe_offset, |
| 2697 | u64 probe_addr) |
| 2698 | { |
| 2699 | char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf); |
| 2700 | u32 len = buf ? strlen(buf) : 0, input_len; |
| 2701 | int err = 0; |
| 2702 | |
| 2703 | if (put_user(len, &uattr->task_fd_query.buf_len)) |
| 2704 | return -EFAULT; |
| 2705 | input_len = attr->task_fd_query.buf_len; |
| 2706 | if (input_len && ubuf) { |
| 2707 | if (!len) { |
| 2708 | /* nothing to copy, just make ubuf NULL terminated */ |
| 2709 | char zero = '\0'; |
| 2710 | |
| 2711 | if (put_user(zero, ubuf)) |
| 2712 | return -EFAULT; |
| 2713 | } else if (input_len >= len + 1) { |
| 2714 | /* ubuf can hold the string with NULL terminator */ |
| 2715 | if (copy_to_user(ubuf, buf, len + 1)) |
| 2716 | return -EFAULT; |
| 2717 | } else { |
| 2718 | /* ubuf cannot hold the string with NULL terminator, |
| 2719 | * do a partial copy with NULL terminator. |
| 2720 | */ |
| 2721 | char zero = '\0'; |
| 2722 | |
| 2723 | err = -ENOSPC; |
| 2724 | if (copy_to_user(ubuf, buf, input_len - 1)) |
| 2725 | return -EFAULT; |
| 2726 | if (put_user(zero, ubuf + input_len - 1)) |
| 2727 | return -EFAULT; |
| 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | if (put_user(prog_id, &uattr->task_fd_query.prog_id) || |
| 2732 | put_user(fd_type, &uattr->task_fd_query.fd_type) || |
| 2733 | put_user(probe_offset, &uattr->task_fd_query.probe_offset) || |
| 2734 | put_user(probe_addr, &uattr->task_fd_query.probe_addr)) |
| 2735 | return -EFAULT; |
| 2736 | |
| 2737 | return err; |
| 2738 | } |
| 2739 | |
| 2740 | #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr |
| 2741 | |
| 2742 | static int bpf_task_fd_query(const union bpf_attr *attr, |
| 2743 | union bpf_attr __user *uattr) |
| 2744 | { |
| 2745 | pid_t pid = attr->task_fd_query.pid; |
| 2746 | u32 fd = attr->task_fd_query.fd; |
| 2747 | const struct perf_event *event; |
| 2748 | struct files_struct *files; |
| 2749 | struct task_struct *task; |
| 2750 | struct file *file; |
| 2751 | int err; |
| 2752 | |
| 2753 | if (CHECK_ATTR(BPF_TASK_FD_QUERY)) |
| 2754 | return -EINVAL; |
| 2755 | |
| 2756 | if (!capable(CAP_SYS_ADMIN)) |
| 2757 | return -EPERM; |
| 2758 | |
| 2759 | if (attr->task_fd_query.flags != 0) |
| 2760 | return -EINVAL; |
| 2761 | |
| 2762 | task = get_pid_task(find_vpid(pid), PIDTYPE_PID); |
| 2763 | if (!task) |
| 2764 | return -ENOENT; |
| 2765 | |
| 2766 | files = get_files_struct(task); |
| 2767 | put_task_struct(task); |
| 2768 | if (!files) |
| 2769 | return -ENOENT; |
| 2770 | |
| 2771 | err = 0; |
| 2772 | spin_lock(&files->file_lock); |
| 2773 | file = fcheck_files(files, fd); |
| 2774 | if (!file) |
| 2775 | err = -EBADF; |
| 2776 | else |
| 2777 | get_file(file); |
| 2778 | spin_unlock(&files->file_lock); |
| 2779 | put_files_struct(files); |
| 2780 | |
| 2781 | if (err) |
| 2782 | goto out; |
| 2783 | |
| 2784 | if (file->f_op == &bpf_raw_tp_fops) { |
| 2785 | struct bpf_raw_tracepoint *raw_tp = file->private_data; |
| 2786 | struct bpf_raw_event_map *btp = raw_tp->btp; |
| 2787 | |
| 2788 | err = bpf_task_fd_query_copy(attr, uattr, |
| 2789 | raw_tp->prog->aux->id, |
| 2790 | BPF_FD_TYPE_RAW_TRACEPOINT, |
| 2791 | btp->tp->name, 0, 0); |
| 2792 | goto put_file; |
| 2793 | } |
| 2794 | |
| 2795 | event = perf_get_event(file); |
| 2796 | if (!IS_ERR(event)) { |
| 2797 | u64 probe_offset, probe_addr; |
| 2798 | u32 prog_id, fd_type; |
| 2799 | const char *buf; |
| 2800 | |
| 2801 | err = bpf_get_perf_event_info(event, &prog_id, &fd_type, |
| 2802 | &buf, &probe_offset, |
| 2803 | &probe_addr); |
| 2804 | if (!err) |
| 2805 | err = bpf_task_fd_query_copy(attr, uattr, prog_id, |
| 2806 | fd_type, buf, |
| 2807 | probe_offset, |
| 2808 | probe_addr); |
| 2809 | goto put_file; |
| 2810 | } |
| 2811 | |
| 2812 | err = -ENOTSUPP; |
| 2813 | put_file: |
| 2814 | fput(file); |
| 2815 | out: |
| 2816 | return err; |
| 2817 | } |
| 2818 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2819 | SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size) |
| 2820 | { |
| 2821 | union bpf_attr attr = {}; |
| 2822 | int err; |
| 2823 | |
Chenbo Feng | 0fa4fe8 | 2018-03-19 17:57:27 -0700 | [diff] [blame] | 2824 | if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN)) |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2825 | return -EPERM; |
| 2826 | |
Martin KaFai Lau | dcab51f | 2018-05-22 15:03:31 -0700 | [diff] [blame] | 2827 | err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2828 | if (err) |
| 2829 | return err; |
| 2830 | size = min_t(u32, size, sizeof(attr)); |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2831 | |
| 2832 | /* copy attributes from user space, may be less than sizeof(bpf_attr) */ |
| 2833 | if (copy_from_user(&attr, uattr, size) != 0) |
| 2834 | return -EFAULT; |
| 2835 | |
Chenbo Feng | afdb09c | 2017-10-18 13:00:24 -0700 | [diff] [blame] | 2836 | err = security_bpf(cmd, &attr, size); |
| 2837 | if (err < 0) |
| 2838 | return err; |
| 2839 | |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2840 | switch (cmd) { |
| 2841 | case BPF_MAP_CREATE: |
| 2842 | err = map_create(&attr); |
| 2843 | break; |
Alexei Starovoitov | db20fd2 | 2014-09-26 00:16:59 -0700 | [diff] [blame] | 2844 | case BPF_MAP_LOOKUP_ELEM: |
| 2845 | err = map_lookup_elem(&attr); |
| 2846 | break; |
| 2847 | case BPF_MAP_UPDATE_ELEM: |
| 2848 | err = map_update_elem(&attr); |
| 2849 | break; |
| 2850 | case BPF_MAP_DELETE_ELEM: |
| 2851 | err = map_delete_elem(&attr); |
| 2852 | break; |
| 2853 | case BPF_MAP_GET_NEXT_KEY: |
| 2854 | err = map_get_next_key(&attr); |
| 2855 | break; |
Daniel Borkmann | 87df15d | 2019-04-09 23:20:06 +0200 | [diff] [blame] | 2856 | case BPF_MAP_FREEZE: |
| 2857 | err = map_freeze(&attr); |
| 2858 | break; |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2859 | case BPF_PROG_LOAD: |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 2860 | err = bpf_prog_load(&attr, uattr); |
Alexei Starovoitov | 09756af | 2014-09-26 00:17:00 -0700 | [diff] [blame] | 2861 | break; |
Daniel Borkmann | b219775 | 2015-10-29 14:58:09 +0100 | [diff] [blame] | 2862 | case BPF_OBJ_PIN: |
| 2863 | err = bpf_obj_pin(&attr); |
| 2864 | break; |
| 2865 | case BPF_OBJ_GET: |
| 2866 | err = bpf_obj_get(&attr); |
| 2867 | break; |
Daniel Mack | f432455 | 2016-11-23 16:52:27 +0100 | [diff] [blame] | 2868 | case BPF_PROG_ATTACH: |
| 2869 | err = bpf_prog_attach(&attr); |
| 2870 | break; |
| 2871 | case BPF_PROG_DETACH: |
| 2872 | err = bpf_prog_detach(&attr); |
| 2873 | break; |
Alexei Starovoitov | 468e2f6 | 2017-10-02 22:50:22 -0700 | [diff] [blame] | 2874 | case BPF_PROG_QUERY: |
| 2875 | err = bpf_prog_query(&attr, uattr); |
| 2876 | break; |
Alexei Starovoitov | 1cf1cae | 2017-03-30 21:45:38 -0700 | [diff] [blame] | 2877 | case BPF_PROG_TEST_RUN: |
| 2878 | err = bpf_prog_test_run(&attr, uattr); |
| 2879 | break; |
Martin KaFai Lau | 34ad558 | 2017-06-05 12:15:48 -0700 | [diff] [blame] | 2880 | case BPF_PROG_GET_NEXT_ID: |
| 2881 | err = bpf_obj_get_next_id(&attr, uattr, |
| 2882 | &prog_idr, &prog_idr_lock); |
| 2883 | break; |
| 2884 | case BPF_MAP_GET_NEXT_ID: |
| 2885 | err = bpf_obj_get_next_id(&attr, uattr, |
| 2886 | &map_idr, &map_idr_lock); |
| 2887 | break; |
Quentin Monnet | 1b9ed84 | 2019-08-20 10:31:50 +0100 | [diff] [blame] | 2888 | case BPF_BTF_GET_NEXT_ID: |
| 2889 | err = bpf_obj_get_next_id(&attr, uattr, |
| 2890 | &btf_idr, &btf_idr_lock); |
| 2891 | break; |
Martin KaFai Lau | b16d9aa | 2017-06-05 12:15:49 -0700 | [diff] [blame] | 2892 | case BPF_PROG_GET_FD_BY_ID: |
| 2893 | err = bpf_prog_get_fd_by_id(&attr); |
| 2894 | break; |
Martin KaFai Lau | bd5f5f4e | 2017-06-05 12:15:50 -0700 | [diff] [blame] | 2895 | case BPF_MAP_GET_FD_BY_ID: |
| 2896 | err = bpf_map_get_fd_by_id(&attr); |
| 2897 | break; |
Martin KaFai Lau | 1e27097 | 2017-06-05 12:15:52 -0700 | [diff] [blame] | 2898 | case BPF_OBJ_GET_INFO_BY_FD: |
| 2899 | err = bpf_obj_get_info_by_fd(&attr, uattr); |
| 2900 | break; |
Alexei Starovoitov | c4f6699 | 2018-03-28 12:05:37 -0700 | [diff] [blame] | 2901 | case BPF_RAW_TRACEPOINT_OPEN: |
| 2902 | err = bpf_raw_tracepoint_open(&attr); |
| 2903 | break; |
Martin KaFai Lau | f56a653 | 2018-04-18 15:56:01 -0700 | [diff] [blame] | 2904 | case BPF_BTF_LOAD: |
| 2905 | err = bpf_btf_load(&attr); |
| 2906 | break; |
Martin KaFai Lau | 78958fc | 2018-05-04 14:49:51 -0700 | [diff] [blame] | 2907 | case BPF_BTF_GET_FD_BY_ID: |
| 2908 | err = bpf_btf_get_fd_by_id(&attr); |
| 2909 | break; |
Yonghong Song | 41bdc4b | 2018-05-24 11:21:09 -0700 | [diff] [blame] | 2910 | case BPF_TASK_FD_QUERY: |
| 2911 | err = bpf_task_fd_query(&attr, uattr); |
| 2912 | break; |
Mauricio Vasquez B | bd513cd | 2018-10-18 15:16:30 +0200 | [diff] [blame] | 2913 | case BPF_MAP_LOOKUP_AND_DELETE_ELEM: |
| 2914 | err = map_lookup_and_delete_elem(&attr); |
| 2915 | break; |
Alexei Starovoitov | 99c55f7 | 2014-09-26 00:16:57 -0700 | [diff] [blame] | 2916 | default: |
| 2917 | err = -EINVAL; |
| 2918 | break; |
| 2919 | } |
| 2920 | |
| 2921 | return err; |
| 2922 | } |