]> www.infradead.org Git - users/dwmw2/linux.git/commit
bpf: Check the validity of nr_words in bpf_iter_bits_new()
authorHou Tao <houtao1@huawei.com>
Wed, 30 Oct 2024 10:05:14 +0000 (18:05 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 30 Oct 2024 19:13:46 +0000 (12:13 -0700)
commit393397fbdcad7396639d7077c33f86169184ba99
tree736e0d75c7b2152ef05de5881337a75b87d5b0b5
parent62a898b07b83f6f407003d8a70f0827a5af08a59
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: 4665415975b0 ("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>
kernel/bpf/helpers.c