Patch "bpf, array: fix overflow in max_entries and undefined behavior in index_mask" has been added to the 4.14-stable tree
Subsystems (2)
This is a note to let you know that I've just added the patch titled
bpf, array: fix overflow in max_entries and undefined behavior in index_mask
to the 4.14-stable tree which can be found at:
http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary
The filename of the patch is:
bpf-array-fix-overflow-in-max_entries-and-undefined-behavior-in-index_mask.patch
and it can be found in the queue-4.14 subdirectory.
If you, or anyone else, feels it should not be added to the stable tree, please let <[email protected]> know about it.
Quoted 1 line
From
bbeb6e4323dad9b5e0ee9f60c223dd532e2403b1Mon Sep 17 00:00:00 2001
From: Daniel Borkmann <[email protected]>
Date: Wed, 10 Jan 2018 23:25:05 +0100
Subject: bpf, array: fix overflow in max_entries and undefined behavior in index_mask
From: Daniel Borkmann <[email protected]>
commit bbeb6e4323dad9b5e0ee9f60c223dd532e2403b1 upstream.
syzkaller tried to alloc a map with 0xfffffffd entries out of a userns, and thus unprivileged. With the recently added logic in b2157399cc98 ("bpf: prevent out-of-bounds speculation") we round this up to the next power of two value for max_entries for unprivileged such that we can apply proper masking into potentially zeroed out map slots.
However, this will generate an index_mask of 0xffffffff, and therefore a + 1 will let this overflow into new max_entries of 0. This will pass allocation, etc, and later on map access we still enforce on the original attr->max_entries value which was 0xfffffffd, therefore triggering GPF all over the place. Thus bail out on overflow in such case.
Moreover, on 32 bit archs roundup_pow_of_two() can also not be used, since fls_long(max_entries - 1) can result in 32 and 1UL << 32 in 32 bit space is undefined. Therefore, do this by hand in a 64 bit variable.
This fixes all the issues triggered by syzkaller's reproducers.
--- kernel/bpf/arraymap.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
kernel/bpf/arraymap.c +15 -3
@@ -53,7 +53,7 @@ static struct bpf_map *array_map_alloc(uu32 elem_size, index_mask, max_entries;bool unpriv = !capable (CAP_SYS_ADMIN );struct bpf_array *array; -u64 array_size; +u64 array_size, mask64;/* check sanity of attributes */ if (attr->max_entries == 0 || attr->key_size != 4 ||
@@ -70,13 +70,25 @@ static struct bpf_map *array_map_alloc(u elem_size =round_up (attr->value_size,8 ); max_entries = attr->max_entries; - index_mask =roundup_pow_of_two (max_entries) -1 ; -if (unpriv) +/* On 32 bit archs roundup_pow_of_two() with max_entries that has +* upper most bit set in u32 space is undefined behavior due to +* resulting 1U << 32, so do it manually here in u64 space. +*/ + mask64 =fls_long (max_entries -1 ); + mask64 =1ULL << mask64; + mask64 -=1 ; + + index_mask = mask64; +if (unpriv) {/* round up array size to nearest power of 2, * since cpu will speculate within index_mask limits */ max_entries = index_mask +1 ; +/* Check for overflows. */ +if (max_entries < attr->max_entries) +return ERR_PTR (-E2BIG ); + } array_size =sizeof (*array);if (percpu)
Patches currently in stable-queue which might be from [email protected] are
queue-4.14/bpf-array-fix-overflow-in-max_entries-and-undefined-behavior-in-index_mask.patch queue-4.14/bpf-prevent-out-of-bounds-speculation.patch queue-4.14/bpf-arsh-is-not-supported-in-32-bit-alu-thus-reject-it.patch