1 |
From foo@baz Thu Feb 17 08:07:01 PM CET 2022 |
2 |
From: Hao Luo <haoluo@google.com> |
3 |
Date: Wed, 16 Feb 2022 14:52:09 -0800 |
4 |
Subject: bpf/selftests: Test PTR_TO_RDONLY_MEM |
5 |
To: Greg KH <gregkh@linuxfoundation.org> |
6 |
Cc: Alexei Starovoitov <ast@kernel.org>, Andrii Nakryiko <andrii@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, laura@labbott.name, stable@vger.kernel.org, Hao Luo <haoluo@google.com> |
7 |
Message-ID: <20220216225209.2196865-10-haoluo@google.com> |
8 |
|
9 |
From: Hao Luo <haoluo@google.com> |
10 |
|
11 |
commit 9497c458c10b049438ef6e6ddda898edbc3ec6a8 upstream. |
12 |
|
13 |
This test verifies that a ksym of non-struct can not be directly |
14 |
updated. |
15 |
|
16 |
Signed-off-by: Hao Luo <haoluo@google.com> |
17 |
Signed-off-by: Alexei Starovoitov <ast@kernel.org> |
18 |
Acked-by: Andrii Nakryiko <andrii@kernel.org> |
19 |
Link: https://lore.kernel.org/bpf/20211217003152.48334-10-haoluo@google.com |
20 |
Cc: stable@vger.kernel.org # 5.16.x |
21 |
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> |
22 |
--- |
23 |
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c | 14 ++++ |
24 |
tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c | 29 ++++++++++ |
25 |
2 files changed, 43 insertions(+) |
26 |
create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c |
27 |
|
28 |
--- a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c |
29 |
+++ b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c |
30 |
@@ -8,6 +8,7 @@ |
31 |
#include "test_ksyms_btf_null_check.skel.h" |
32 |
#include "test_ksyms_weak.skel.h" |
33 |
#include "test_ksyms_weak.lskel.h" |
34 |
+#include "test_ksyms_btf_write_check.skel.h" |
35 |
|
36 |
static int duration; |
37 |
|
38 |
@@ -137,6 +138,16 @@ cleanup: |
39 |
test_ksyms_weak_lskel__destroy(skel); |
40 |
} |
41 |
|
42 |
+static void test_write_check(void) |
43 |
+{ |
44 |
+ struct test_ksyms_btf_write_check *skel; |
45 |
+ |
46 |
+ skel = test_ksyms_btf_write_check__open_and_load(); |
47 |
+ ASSERT_ERR_PTR(skel, "unexpected load of a prog writing to ksym memory\n"); |
48 |
+ |
49 |
+ test_ksyms_btf_write_check__destroy(skel); |
50 |
+} |
51 |
+ |
52 |
void test_ksyms_btf(void) |
53 |
{ |
54 |
int percpu_datasec; |
55 |
@@ -167,4 +178,7 @@ void test_ksyms_btf(void) |
56 |
|
57 |
if (test__start_subtest("weak_ksyms_lskel")) |
58 |
test_weak_syms_lskel(); |
59 |
+ |
60 |
+ if (test__start_subtest("write_check")) |
61 |
+ test_write_check(); |
62 |
} |
63 |
--- /dev/null |
64 |
+++ b/tools/testing/selftests/bpf/progs/test_ksyms_btf_write_check.c |
65 |
@@ -0,0 +1,29 @@ |
66 |
+// SPDX-License-Identifier: GPL-2.0 |
67 |
+/* Copyright (c) 2021 Google */ |
68 |
+ |
69 |
+#include "vmlinux.h" |
70 |
+ |
71 |
+#include <bpf/bpf_helpers.h> |
72 |
+ |
73 |
+extern const int bpf_prog_active __ksym; /* int type global var. */ |
74 |
+ |
75 |
+SEC("raw_tp/sys_enter") |
76 |
+int handler(const void *ctx) |
77 |
+{ |
78 |
+ int *active; |
79 |
+ __u32 cpu; |
80 |
+ |
81 |
+ cpu = bpf_get_smp_processor_id(); |
82 |
+ active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu); |
83 |
+ if (active) { |
84 |
+ /* Kernel memory obtained from bpf_{per,this}_cpu_ptr |
85 |
+ * is read-only, should _not_ pass verification. |
86 |
+ */ |
87 |
+ /* WRITE_ONCE */ |
88 |
+ *(volatile int *)active = -1; |
89 |
+ } |
90 |
+ |
91 |
+ return 0; |
92 |
+} |
93 |
+ |
94 |
+char _license[] SEC("license") = "GPL"; |