diff options
Diffstat (limited to 'kernel/bpf/arraymap.c')
-rw-r--r-- | kernel/bpf/arraymap.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index aaa319848e7d..ab94d304a634 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c | |||
@@ -56,7 +56,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr) | |||
56 | u32 elem_size, index_mask, max_entries; | 56 | u32 elem_size, index_mask, max_entries; |
57 | bool unpriv = !capable(CAP_SYS_ADMIN); | 57 | bool unpriv = !capable(CAP_SYS_ADMIN); |
58 | struct bpf_array *array; | 58 | struct bpf_array *array; |
59 | u64 array_size; | 59 | u64 array_size, mask64; |
60 | 60 | ||
61 | /* check sanity of attributes */ | 61 | /* check sanity of attributes */ |
62 | if (attr->max_entries == 0 || attr->key_size != 4 || | 62 | if (attr->max_entries == 0 || attr->key_size != 4 || |
@@ -74,13 +74,25 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr) | |||
74 | elem_size = round_up(attr->value_size, 8); | 74 | elem_size = round_up(attr->value_size, 8); |
75 | 75 | ||
76 | max_entries = attr->max_entries; | 76 | max_entries = attr->max_entries; |
77 | index_mask = roundup_pow_of_two(max_entries) - 1; | ||
78 | 77 | ||
79 | if (unpriv) | 78 | /* On 32 bit archs roundup_pow_of_two() with max_entries that has |
79 | * upper most bit set in u32 space is undefined behavior due to | ||
80 | * resulting 1U << 32, so do it manually here in u64 space. | ||
81 | */ | ||
82 | mask64 = fls_long(max_entries - 1); | ||
83 | mask64 = 1ULL << mask64; | ||
84 | mask64 -= 1; | ||
85 | |||
86 | index_mask = mask64; | ||
87 | if (unpriv) { | ||
80 | /* round up array size to nearest power of 2, | 88 | /* round up array size to nearest power of 2, |
81 | * since cpu will speculate within index_mask limits | 89 | * since cpu will speculate within index_mask limits |
82 | */ | 90 | */ |
83 | max_entries = index_mask + 1; | 91 | max_entries = index_mask + 1; |
92 | /* Check for overflows. */ | ||
93 | if (max_entries < attr->max_entries) | ||
94 | return ERR_PTR(-E2BIG); | ||
95 | } | ||
84 | 96 | ||
85 | array_size = sizeof(*array); | 97 | array_size = sizeof(*array); |
86 | if (percpu) | 98 | if (percpu) |