Skip to content

Commit

Permalink
selftests/bpf: Use bpf_num_possible_cpus() in per-cpu map allocations
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
sm00th authored and anakryiko committed Apr 6, 2022
1 parent a8d600f commit ebaf24c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/prog_tests/netcnt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion tools/testing/selftests/bpf/test_cgroup_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sys/sysinfo.h>

#include "bpf_rlimit.h"
#include "bpf_util.h"
#include "cgroup_helpers.h"
#include "testing_helpers.h"

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ebaf24c

Please sign in to comment.