Skip to content

Commit 8568c3c

Browse files
iamkafaiAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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 <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent e710bcc commit 8568c3c

File tree

2 files changed

+70
-18
lines changed

2 files changed

+70
-18
lines changed

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

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
#include <bpf/libbpf.h>
66
#include <bpf/btf.h>
77
#include "test_ksyms_btf.skel.h"
8+
#include "test_ksyms_btf_null_check.skel.h"
89

910
static int duration;
1011

11-
void test_ksyms_btf(void)
12+
static void test_basic(void)
1213
{
1314
__u64 runqueues_addr, bpf_prog_active_addr;
1415
__u32 this_rq_cpu;
1516
int this_bpf_prog_active;
1617
struct test_ksyms_btf *skel = NULL;
1718
struct test_ksyms_btf__data *data;
18-
struct btf *btf;
19-
int percpu_datasec;
2019
int err;
2120

2221
err = kallsyms_find("runqueues", &runqueues_addr);
@@ -31,20 +30,6 @@ void test_ksyms_btf(void)
3130
if (CHECK(err == -ENOENT, "ksym_find", "symbol 'bpf_prog_active' not found\n"))
3231
return;
3332

34-
btf = libbpf_find_kernel_btf();
35-
if (CHECK(IS_ERR(btf), "btf_exists", "failed to load kernel BTF: %ld\n",
36-
PTR_ERR(btf)))
37-
return;
38-
39-
percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu",
40-
BTF_KIND_DATASEC);
41-
if (percpu_datasec < 0) {
42-
printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n",
43-
__func__);
44-
test__skip();
45-
goto cleanup;
46-
}
47-
4833
skel = test_ksyms_btf__open_and_load();
4934
if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n"))
5035
goto cleanup;
@@ -83,6 +68,42 @@ void test_ksyms_btf(void)
8368
data->out__bpf_prog_active);
8469

8570
cleanup:
86-
btf__free(btf);
8771
test_ksyms_btf__destroy(skel);
8872
}
73+
74+
static void test_null_check(void)
75+
{
76+
struct test_ksyms_btf_null_check *skel;
77+
78+
skel = test_ksyms_btf_null_check__open_and_load();
79+
CHECK(skel, "skel_open", "unexpected load of a prog missing null check\n");
80+
81+
test_ksyms_btf_null_check__destroy(skel);
82+
}
83+
84+
void test_ksyms_btf(void)
85+
{
86+
int percpu_datasec;
87+
struct btf *btf;
88+
89+
btf = libbpf_find_kernel_btf();
90+
if (CHECK(IS_ERR(btf), "btf_exists", "failed to load kernel BTF: %ld\n",
91+
PTR_ERR(btf)))
92+
return;
93+
94+
percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu",
95+
BTF_KIND_DATASEC);
96+
btf__free(btf);
97+
if (percpu_datasec < 0) {
98+
printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n",
99+
__func__);
100+
test__skip();
101+
return;
102+
}
103+
104+
if (test__start_subtest("basic"))
105+
test_basic();
106+
107+
if (test__start_subtest("null_check"))
108+
test_null_check();
109+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2020 Facebook */
3+
4+
#include "vmlinux.h"
5+
6+
#include <bpf/bpf_helpers.h>
7+
8+
extern const struct rq runqueues __ksym; /* struct type global var. */
9+
extern const int bpf_prog_active __ksym; /* int type global var. */
10+
11+
SEC("raw_tp/sys_enter")
12+
int handler(const void *ctx)
13+
{
14+
struct rq *rq;
15+
int *active;
16+
__u32 cpu;
17+
18+
cpu = bpf_get_smp_processor_id();
19+
rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, cpu);
20+
active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
21+
if (active) {
22+
/* READ_ONCE */
23+
*(volatile int *)active;
24+
/* !rq has not been tested, so verifier should reject. */
25+
*(volatile int *)(&rq->cpu);
26+
}
27+
28+
return 0;
29+
}
30+
31+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)