summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/verifier_uprobe_write_ctx.c
blob: f5ed453b179823bd7a17657678437f580b8b3d39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// SPDX-License-Identifier: GPL-2.0

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"

/*
 * A uprobe prog can write only the registers that ptrace can set to any
 * value. The fields the kernel restores on the way to user space (segments,
 * flags, privilege state) are read-only.
 */

#define WRITE_CTX(name, field, sz)					\
__naked void name(void)							\
{									\
	asm volatile ("							\
	r2 = 0;								\
	*(" #sz " *)(r1 + %[off]) = r2;					\
	r0 = 0;								\
	exit;								\
"	:								\
	: __imm_const(off, offsetof(struct pt_regs, field))		\
	: __clobber_all);						\
}

#define READ_CTX(name, field)						\
__naked void name(void)							\
{									\
	asm volatile ("							\
	r0 = *(u64 *)(r1 + %[off]);					\
	r0 = 0;								\
	exit;								\
"	:								\
	: __imm_const(off, offsetof(struct pt_regs, field))		\
	: __clobber_all);						\
}

#if defined(__TARGET_ARCH_x86)

SEC("uprobe")
__description("uprobe: write ctx->ax")
__success
WRITE_CTX(write_ax, ax, u64)

SEC("uprobe")
__description("uprobe: write ctx->r15")
__success
WRITE_CTX(write_r15, r15, u64)

SEC("uprobe")
__description("uprobe: write ctx->di")
__success
WRITE_CTX(write_di, di, u64)

SEC("uprobe")
__description("uprobe: write ctx->ip")
__success
WRITE_CTX(write_ip, ip, u64)

SEC("uprobe")
__description("uprobe: write ctx->sp")
__success
WRITE_CTX(write_sp, sp, u64)

SEC("uprobe")
__description("uprobe: write lower half of ctx->ax")
__success
WRITE_CTX(write_ax_u32, ax, u32)

SEC("uprobe")
__description("uprobe: write ctx->orig_ax")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_orig_ax, orig_ax, u64)

SEC("uprobe")
__description("uprobe: write ctx->cs")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_cs, cs, u64)

SEC("uprobe")
__description("uprobe: write lower half of ctx->cs")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_cs_u32, cs, u32)

SEC("uprobe")
__description("uprobe: write ctx->flags")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_flags, flags, u64)

SEC("uprobe")
__description("uprobe: write ctx->ss")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_ss, ss, u64)

SEC("uprobe")
__description("uprobe: read ctx->flags")
__success
READ_CTX(read_flags, flags)

SEC("kprobe")
__description("kprobe: write ctx->flags")
__failure __msg("invalid bpf_context access")
WRITE_CTX(kprobe_write_flags, flags, u64)

#elif defined(__TARGET_ARCH_arm64)

SEC("uprobe")
__description("uprobe: write ctx->regs[0]")
__success
WRITE_CTX(write_x0, regs[0], u64)

SEC("uprobe")
__description("uprobe: write ctx->regs[30]")
__success
WRITE_CTX(write_x30, regs[30], u64)

SEC("uprobe")
__description("uprobe: write ctx->sp")
__success
WRITE_CTX(write_sp, sp, u64)

SEC("uprobe")
__description("uprobe: write ctx->pc")
__success
WRITE_CTX(write_pc, pc, u64)

SEC("uprobe")
__description("uprobe: write ctx->pstate")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_pstate, pstate, u64)

SEC("uprobe")
__description("uprobe: write lower half of ctx->pstate")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_pstate_u32, pstate, u32)

SEC("uprobe")
__description("uprobe: write ctx->orig_x0")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_orig_x0, orig_x0, u64)

SEC("uprobe")
__description("uprobe: read ctx->pstate")
__success
READ_CTX(read_pstate, pstate)

#elif defined(__TARGET_ARCH_riscv)

SEC("uprobe")
__description("uprobe: write ctx->epc")
__success
WRITE_CTX(write_epc, epc, u64)

SEC("uprobe")
__description("uprobe: write ctx->a0")
__success
WRITE_CTX(write_a0, a0, u64)

SEC("uprobe")
__description("uprobe: write ctx->t6")
__success
WRITE_CTX(write_t6, t6, u64)

SEC("uprobe")
__description("uprobe: write ctx->status")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_status, status, u64)

SEC("uprobe")
__description("uprobe: write ctx->badaddr")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_badaddr, badaddr, u64)

SEC("uprobe")
__description("uprobe: write ctx->cause")
__failure __msg("invalid bpf_context access")
WRITE_CTX(write_cause, cause, u64)

SEC("uprobe")
__description("uprobe: read ctx->status")
__success
READ_CTX(read_status, status)

#else

SEC("uprobe")
__description("uprobe: ctx writes not checked on this arch")
__success
__naked void unsupported(void)
{
	asm volatile ("r0 = 0; exit;" ::: __clobber_all);
}

#endif

char _license[] SEC("license") = "GPL";