Skip to content

Commit 9497c45

Browse files
haoluo1022Alexei Starovoitov
authored andcommitted
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 <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 216e3cd commit 9497c45

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "test_ksyms_btf_null_check.skel.h"
99
#include "test_ksyms_weak.skel.h"
1010
#include "test_ksyms_weak.lskel.h"
11+
#include "test_ksyms_btf_write_check.skel.h"
1112

1213
static int duration;
1314

@@ -137,6 +138,16 @@ static void test_weak_syms_lskel(void)
137138
test_ksyms_weak_lskel__destroy(skel);
138139
}
139140

141+
static void test_write_check(void)
142+
{
143+
struct test_ksyms_btf_write_check *skel;
144+
145+
skel = test_ksyms_btf_write_check__open_and_load();
146+
ASSERT_ERR_PTR(skel, "unexpected load of a prog writing to ksym memory\n");
147+
148+
test_ksyms_btf_write_check__destroy(skel);
149+
}
150+
140151
void test_ksyms_btf(void)
141152
{
142153
int percpu_datasec;
@@ -167,4 +178,7 @@ void test_ksyms_btf(void)
167178

168179
if (test__start_subtest("weak_ksyms_lskel"))
169180
test_weak_syms_lskel();
181+
182+
if (test__start_subtest("write_check"))
183+
test_write_check();
170184
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2021 Google */
3+
4+
#include "vmlinux.h"
5+
6+
#include <bpf/bpf_helpers.h>
7+
8+
extern const int bpf_prog_active __ksym; /* int type global var. */
9+
10+
SEC("raw_tp/sys_enter")
11+
int handler(const void *ctx)
12+
{
13+
int *active;
14+
__u32 cpu;
15+
16+
cpu = bpf_get_smp_processor_id();
17+
active = (int *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
18+
if (active) {
19+
/* Kernel memory obtained from bpf_{per,this}_cpu_ptr
20+
* is read-only, should _not_ pass verification.
21+
*/
22+
/* WRITE_ONCE */
23+
*(volatile int *)active = -1;
24+
}
25+
26+
return 0;
27+
}
28+
29+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)