Skip to content

Commit ebaf24c

Browse files
sm00thanakryiko
authored andcommitted
selftests/bpf: 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). Signed-off-by: Artem Savkov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent a8d600f commit ebaf24c

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

tools/testing/selftests/bpf/prog_tests/netcnt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void serial_test_netcnt(void)
2525
if (!ASSERT_OK_PTR(skel, "netcnt_prog__open_and_load"))
2626
return;
2727

28-
nproc = get_nprocs_conf();
28+
nproc = bpf_num_possible_cpus();
2929
percpu_netcnt = malloc(sizeof(*percpu_netcnt) * nproc);
3030
if (!ASSERT_OK_PTR(percpu_netcnt, "malloc(percpu_netcnt)"))
3131
goto err;

tools/testing/selftests/bpf/test_cgroup_storage.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <sys/sysinfo.h>
88

99
#include "bpf_rlimit.h"
10+
#include "bpf_util.h"
1011
#include "cgroup_helpers.h"
1112
#include "testing_helpers.h"
1213

@@ -44,7 +45,7 @@ int main(int argc, char **argv)
4445
unsigned long long *percpu_value;
4546
int cpu, nproc;
4647

47-
nproc = get_nprocs_conf();
48+
nproc = bpf_num_possible_cpus();
4849
percpu_value = malloc(sizeof(*percpu_value) * nproc);
4950
if (!percpu_value) {
5051
printf("Not enough memory for per-cpu area (%d cpus)\n", nproc);

0 commit comments

Comments
 (0)