diff options
author | Mark Rutland <mark.rutland@arm.com> | 2018-05-03 12:04:59 -0400 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-05-03 19:16:11 -0400 |
commit | 9ef09e35e521bf0df5325cc9cffa726a8f5f3c1b (patch) | |
tree | eb370cdca5ac43b3ed7e9ce1e0e8ab14a54a673b /kernel/bpf/syscall.c | |
parent | a8d7aa17bbc970971ccdf71988ea19230ab368b1 (diff) |
bpf: fix possible spectre-v1 in find_and_alloc_map()
It's possible for userspace to control attr->map_type. Sanitize it when
using it as an array index to prevent an out-of-bounds value being used
under speculation.
Found by smatch.
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: netdev@vger.kernel.org
Acked-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'kernel/bpf/syscall.c')
-rw-r--r-- | kernel/bpf/syscall.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index ebfe9f29dae8..8f434485abd2 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c | |||
@@ -26,6 +26,7 @@ | |||
26 | #include <linux/cred.h> | 26 | #include <linux/cred.h> |
27 | #include <linux/timekeeping.h> | 27 | #include <linux/timekeeping.h> |
28 | #include <linux/ctype.h> | 28 | #include <linux/ctype.h> |
29 | #include <linux/nospec.h> | ||
29 | 30 | ||
30 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ | 31 | #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ |
31 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ | 32 | (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ |
@@ -102,12 +103,14 @@ const struct bpf_map_ops bpf_map_offload_ops = { | |||
102 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) | 103 | static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) |
103 | { | 104 | { |
104 | const struct bpf_map_ops *ops; | 105 | const struct bpf_map_ops *ops; |
106 | u32 type = attr->map_type; | ||
105 | struct bpf_map *map; | 107 | struct bpf_map *map; |
106 | int err; | 108 | int err; |
107 | 109 | ||
108 | if (attr->map_type >= ARRAY_SIZE(bpf_map_types)) | 110 | if (type >= ARRAY_SIZE(bpf_map_types)) |
109 | return ERR_PTR(-EINVAL); | 111 | return ERR_PTR(-EINVAL); |
110 | ops = bpf_map_types[attr->map_type]; | 112 | type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); |
113 | ops = bpf_map_types[type]; | ||
111 | if (!ops) | 114 | if (!ops) |
112 | return ERR_PTR(-EINVAL); | 115 | return ERR_PTR(-EINVAL); |
113 | 116 | ||
@@ -122,7 +125,7 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) | |||
122 | if (IS_ERR(map)) | 125 | if (IS_ERR(map)) |
123 | return map; | 126 | return map; |
124 | map->ops = ops; | 127 | map->ops = ops; |
125 | map->map_type = attr->map_type; | 128 | map->map_type = type; |
126 | return map; | 129 | return map; |
127 | } | 130 | } |
128 | 131 | ||