diff options
author | Martin KaFai Lau <kafai@fb.com> | 2020-10-19 12:42:25 -0700 |
---|---|---|
committer | Alexei Starovoitov <ast@kernel.org> | 2020-10-19 15:57:42 -0700 |
commit | 8568c3cefd5143fa0dc09f90e1bc9dc8905292f4 (patch) | |
tree | 90ff2079a7ea6260ffe6399e1d00f457cabea8cf /tools/testing/selftests/bpf/progs/test_ksyms_btf_null_check.c | |
parent | bpf: selftest: Ensure the return value of bpf_skc_to helpers must be checked (diff) | |
download | wireguard-linux-8568c3cefd5143fa0dc09f90e1bc9dc8905292f4.tar.xz wireguard-linux-8568c3cefd5143fa0dc09f90e1bc9dc8905292f4.zip |
bpf: selftest: Ensure the return value of the bpf_per_cpu_ptr() must be checked
This patch tests all pointers returned by bpf_per_cpu_ptr() must be
tested for NULL first before it can be accessed.
This patch adds a subtest "null_check", so it moves the ".data..percpu"
existence check to the very beginning and before doing any subtest.
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20201019194225.1051596-1-kafai@fb.com
Diffstat (limited to '')
-rw-r--r-- | tools/testing/selftests/bpf/progs/test_ksyms_btf_null_check.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_btf_null_check.c b/tools/testing/selftests/bpf/progs/test_ksyms_btf_null_check.c new file mode 100644 index 000000000000..8bc8f7c637bc --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_ksyms_btf_null_check.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Facebook */ + +#include "vmlinux.h" + +#include <bpf/bpf_helpers.h> + +extern const struct rq runqueues __ksym; /* struct type global var. */ +extern const int bpf_prog_active __ksym; /* int type global var. */ + +SEC("raw_tp/sys_enter") +int handler(const void *ctx) +{ + struct rq *rq; + int *active; + __u32 cpu; + + cpu = bpf_get_smp_processor_id(); + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, cpu); + active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu); + if (active) { + /* READ_ONCE */ + *(volatile int *)active; + /* !rq has not been tested, so verifier should reject. */ + *(volatile int *)(&rq->cpu); + } + + return 0; +} + +char _license[] SEC("license") = "GPL"; |