bpf/selftests: use bpf_num_possible_cpus() in per-cpu map allocations
Subsystems (5)
PATCH 1/1 bpf/selftests: use bpf_num_possible_cpus() in per-cpu map allocations
bpf_map_value_size() uses num_possible_cpus() to determine map size, but some of the tests only allocate enough memory for online cpus. This results in out-of-bound writes in userspace during bpf(BPF_MAP_LOOKUP_ELEM) syscalls in cases when number of online cpus is lower than the number of possible cpus. Fix by switching from get_nprocs_conf() to bpf_num_possible_cpus() when determining the number of processors in these tests (test_progs/netcnt and test_cgroup_storage).
--- tools/testing/selftests/bpf/prog_tests/netcnt.c | 2 +- tools/testing/selftests/bpf/test_cgroup_storage.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/netcnt.c +1 -1
@@ -25,7 +25,7 @@ void serial_test_netcnt(void)if (!ASSERT_OK_PTR (skel,"netcnt_prog__open_and_load" ))return ; - nproc =get_nprocs_conf (); + nproc =bpf_num_possible_cpus (); percpu_netcnt =malloc (sizeof (*percpu_netcnt) * nproc);if (!ASSERT_OK_PTR (percpu_netcnt,"malloc(percpu_netcnt)" )) goto err;
tools/testing/selftests/bpf/test_cgroup_storage.c +2 -1
@@ -7,6 +7,7 @@#include <sys/sysinfo.h> #include "bpf_rlimit.h" +#include "bpf_util.h" #include "cgroup_helpers.h" #include "testing_helpers.h"
@@ -44,7 +45,7 @@ int main(int argc, char **argv)unsigned long long *percpu_value;int cpu, nproc; - nproc =get_nprocs_conf (); + nproc =bpf_num_possible_cpus (); percpu_value =malloc (sizeof (*percpu_value) * nproc);if (!percpu_value) {printf ("Not enough memory for per-cpu area (%d cpus)\n" , nproc);
Signature · 1 line
2.34.1
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 9 lines · On Wed, 6 Apr 2022 10:54:08 +0200 you wrote:
bpf_map_value_size()usesnum_possible_cpus()to determine map size, but some of the tests only allocate enough memory for online cpus. This results in out-of-bound writes in userspace duringbpf(BPF_MAP_LOOKUP_ELEM)syscalls in cases when number of online cpus is lower than the number of possible cpus. Fix by switching fromget_nprocs_conf()tobpf_num_possible_cpus()when determining the number of processors in these tests (test_progs/netcnt andtest_cgroup_storage).[...]
Here is the summary with links:
- [bpf-next] bpf/selftests: use bpf_num_possible_cpus() in per-cpu map allocations
https://git.kernel.org/bpf/bpf-next/c/ebaf24c589d7
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html