mirror of
https://github.com/torvalds/linux.git
synced 2024-11-29 07:31:29 +00:00
bpf: Check the validity of nr_words in bpf_iter_bits_new()
Check the validity of nr_words in bpf_iter_bits_new(). Without this
check, when multiplication overflow occurs for nr_bits (e.g., when
nr_words = 0x0400-0001, nr_bits becomes 64), stack corruption may occur
due to bpf_probe_read_kernel_common(..., nr_bytes = 0x2000-0008).
Fix it by limiting the maximum value of nr_words to 511. The value is
derived from the current implementation of BPF memory allocator. To
ensure compatibility if the BPF memory allocator's size limitation
changes in the future, use the helper bpf_mem_alloc_check_size() to
check whether nr_bytes is too larger. And return -E2BIG instead of
-ENOMEM for oversized nr_bytes.
Fixes: 4665415975
("bpf: Add bits iterator")
Signed-off-by: Hou Tao <houtao1@huawei.com>
Link: https://lore.kernel.org/r/20241030100516.3633640-4-houtao@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
62a898b07b
commit
393397fbdc
@ -2851,6 +2851,8 @@ struct bpf_iter_bits {
|
|||||||
__u64 __opaque[2];
|
__u64 __opaque[2];
|
||||||
} __aligned(8);
|
} __aligned(8);
|
||||||
|
|
||||||
|
#define BITS_ITER_NR_WORDS_MAX 511
|
||||||
|
|
||||||
struct bpf_iter_bits_kern {
|
struct bpf_iter_bits_kern {
|
||||||
union {
|
union {
|
||||||
unsigned long *bits;
|
unsigned long *bits;
|
||||||
@ -2865,7 +2867,8 @@ struct bpf_iter_bits_kern {
|
|||||||
* @it: The new bpf_iter_bits to be created
|
* @it: The new bpf_iter_bits to be created
|
||||||
* @unsafe_ptr__ign: A pointer pointing to a memory area to be iterated over
|
* @unsafe_ptr__ign: A pointer pointing to a memory area to be iterated over
|
||||||
* @nr_words: The size of the specified memory area, measured in 8-byte units.
|
* @nr_words: The size of the specified memory area, measured in 8-byte units.
|
||||||
* Due to the limitation of memalloc, it can't be greater than 512.
|
* The maximum value of @nr_words is @BITS_ITER_NR_WORDS_MAX. This limit may be
|
||||||
|
* further reduced by the BPF memory allocator implementation.
|
||||||
*
|
*
|
||||||
* This function initializes a new bpf_iter_bits structure for iterating over
|
* This function initializes a new bpf_iter_bits structure for iterating over
|
||||||
* a memory area which is specified by the @unsafe_ptr__ign and @nr_words. It
|
* a memory area which is specified by the @unsafe_ptr__ign and @nr_words. It
|
||||||
@ -2892,6 +2895,8 @@ bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign, u32 nr_w
|
|||||||
|
|
||||||
if (!unsafe_ptr__ign || !nr_words)
|
if (!unsafe_ptr__ign || !nr_words)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
if (nr_words > BITS_ITER_NR_WORDS_MAX)
|
||||||
|
return -E2BIG;
|
||||||
|
|
||||||
/* Optimization for u64 mask */
|
/* Optimization for u64 mask */
|
||||||
if (nr_bits == 64) {
|
if (nr_bits == 64) {
|
||||||
@ -2903,6 +2908,9 @@ bpf_iter_bits_new(struct bpf_iter_bits *it, const u64 *unsafe_ptr__ign, u32 nr_w
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bpf_mem_alloc_check_size(false, nr_bytes))
|
||||||
|
return -E2BIG;
|
||||||
|
|
||||||
/* Fallback to memalloc */
|
/* Fallback to memalloc */
|
||||||
kit->bits = bpf_mem_alloc(&bpf_global_ma, nr_bytes);
|
kit->bits = bpf_mem_alloc(&bpf_global_ma, nr_bytes);
|
||||||
if (!kit->bits)
|
if (!kit->bits)
|
||||||
|
Loading…
Reference in New Issue
Block a user