bloom_map_alloc() can under-allocate bloom filter storage on 32-bit kernels when the bitset size reaches the U32_MAX fallback case. BITS_TO_BYTES(U32_MAX) is evaluated with 32-bit arithmetic and wraps to 0, while bitset_mask remains U32_MAX. As a result, later bloom filter updates can write beyond the allocated object. I reached local privilege escalation using only this bug from a binary with CAP_BPF. Compute the bitset size from bitset_mask in u64 and check the final allocation size before calling bpf_map_area_alloc(). Fixes: 9330986c0300 ("bpf: Add bloom filter map implementation") Cc: bpf@vger.kernel.org Signed-off-by: Jérémy Jean --- kernel/bpf/bloom_filter.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/bloom_filter.c b/kernel/bpf/bloom_filter.c index b73336c976b..8e6532fa418 100644 --- a/kernel/bpf/bloom_filter.c +++ b/kernel/bpf/bloom_filter.c @@ -94,9 +94,10 @@ static int bloom_map_alloc_check(union bpf_attr *attr) static struct bpf_map *bloom_map_alloc(union bpf_attr *attr) { - u32 bitset_bytes, bitset_mask, nr_hash_funcs, nr_bits; + u32 bitset_mask, nr_hash_funcs, nr_bits; int numa_node = bpf_map_attr_numa_node(attr); struct bpf_bloom_filter *bloom; + u64 bitset_bytes, alloc_size; if (attr->key_size != 0 || attr->value_size == 0 || attr->max_entries == 0 || @@ -127,23 +128,21 @@ static struct bpf_map *bloom_map_alloc(union bpf_attr *attr) if (check_mul_overflow(attr->max_entries, nr_hash_funcs, &nr_bits) || check_mul_overflow(nr_bits / 5, (u32)7, &nr_bits) || nr_bits > (1UL << 31)) { - /* The bit array size is 2^32 bits but to avoid overflowing the - * u32, we use U32_MAX, which will round up to the equivalent - * number of bytes - */ - bitset_bytes = BITS_TO_BYTES(U32_MAX); bitset_mask = U32_MAX; } else { if (nr_bits <= BITS_PER_LONG) nr_bits = BITS_PER_LONG; else nr_bits = roundup_pow_of_two(nr_bits); - bitset_bytes = BITS_TO_BYTES(nr_bits); bitset_mask = nr_bits - 1; } + bitset_bytes = BITS_TO_BYTES((u64)bitset_mask + 1); bitset_bytes = roundup(bitset_bytes, sizeof(unsigned long)); - bloom = bpf_map_area_alloc(sizeof(*bloom) + bitset_bytes, numa_node); + if (check_add_overflow((u64)sizeof(*bloom), bitset_bytes, &alloc_size)) + return ERR_PTR(-E2BIG); + + bloom = bpf_map_area_alloc(alloc_size, numa_node); if (!bloom) return ERR_PTR(-ENOMEM); -- 2.47.3