Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpf/selftests: Test PTR_TO_RDONLY_MEM
commit 9497c45 upstream. This test verifies that a ksym of non-struct can not be directly updated. Signed-off-by: Hao Luo <haoluo@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20211217003152.48334-10-haoluo@google.com Cc: stable@vger.kernel.org # 5.16.x Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
- Loading branch information
1 parent
4a6c35d
commit 80b7205
Showing
2 changed files
with
43 additions
and
0 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
29 changes: 29 additions & 0 deletions
29
tools/testing/selftests/bpf/progs/test_ksyms_btf_write_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,29 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2021 Google */ | ||
|
||
#include "vmlinux.h" | ||
|
||
#include <bpf/bpf_helpers.h> | ||
|
||
extern const int bpf_prog_active __ksym; /* int type global var. */ | ||
|
||
SEC("raw_tp/sys_enter") | ||
int handler(const void *ctx) | ||
{ | ||
int *active; | ||
__u32 cpu; | ||
|
||
cpu = bpf_get_smp_processor_id(); | ||
active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu); | ||
if (active) { | ||
/* Kernel memory obtained from bpf_{per,this}_cpu_ptr | ||
* is read-only, should _not_ pass verification. | ||
*/ | ||
/* WRITE_ONCE */ | ||
*(volatile int *)active = -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |