aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
diff options
context:
space:
mode:
authorHao Luo <haoluo@google.com>2021-12-16 16:31:52 -0800
committerAlexei Starovoitov <ast@kernel.org>2021-12-18 13:27:41 -0800
commit9497c458c10b049438ef6e6ddda898edbc3ec6a8 (patch)
treebc4fe9822abbf115f8c1183461bc9fdcb96f9212 /tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
parentbpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem. (diff)
downloadlinux-dev-9497c458c10b049438ef6e6ddda898edbc3ec6a8.tar.xz
linux-dev-9497c458c10b049438ef6e6ddda898edbc3ec6a8.zip
bpf/selftests: Test PTR_TO_RDONLY_MEM
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
Diffstat (limited to '')
-rw-r--r--tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c b/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
new file mode 100644
index 000000000000..2180c41cd890
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c
@@ -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";