Skip to content

Commit 9ef09e3

Browse files
mrutland-armborkmann
authored andcommitted
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 <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Dan Carpenter <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: [email protected] Acked-by: David S. Miller <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent a8d7aa1 commit 9ef09e3

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

kernel/bpf/syscall.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/cred.h>
2727
#include <linux/timekeeping.h>
2828
#include <linux/ctype.h>
29+
#include <linux/nospec.h>
2930

3031
#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
3132
(map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
@@ -102,12 +103,14 @@ const struct bpf_map_ops bpf_map_offload_ops = {
102103
static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
103104
{
104105
const struct bpf_map_ops *ops;
106+
u32 type = attr->map_type;
105107
struct bpf_map *map;
106108
int err;
107109

108-
if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
110+
if (type >= ARRAY_SIZE(bpf_map_types))
109111
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];
111114
if (!ops)
112115
return ERR_PTR(-EINVAL);
113116

@@ -122,7 +125,7 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
122125
if (IS_ERR(map))
123126
return map;
124127
map->ops = ops;
125-
map->map_type = attr->map_type;
128+
map->map_type = type;
126129
return map;
127130
}
128131

0 commit comments

Comments
 (0)