-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Martin KaFai Lau
authored and
Alexei Starovoitov
committed
Oct 19, 2020
1 parent
e710bcc
commit 8568c3c
Showing
2 changed files
with
70 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
tools/testing/selftests/bpf/progs/test_ksyms_btf_null_check.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; |