aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/bpf/progs/verifier_int_ptr.c
blob: 9fc3fae5cd833b61b0807faad6c1d08169a25801 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/* Converted from tools/testing/selftests/bpf/verifier/int_ptr.c */

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

SEC("socket")
__description("ARG_PTR_TO_LONG uninitialized")
__success
__failure_unpriv __msg_unpriv("invalid indirect read from stack R4 off -16+0 size 8")
__naked void arg_ptr_to_long_uninitialized(void)
{
	asm volatile ("					\
	/* bpf_strtoul arg1 (buf) */			\
	r7 = r10;					\
	r7 += -8;					\
	r0 = 0x00303036;				\
	*(u64*)(r7 + 0) = r0;				\
	r1 = r7;					\
	/* bpf_strtoul arg2 (buf_len) */		\
	r2 = 4;						\
	/* bpf_strtoul arg3 (flags) */			\
	r3 = 0;						\
	/* bpf_strtoul arg4 (res) */			\
	r7 += -8;					\
	r4 = r7;					\
	/* bpf_strtoul() */				\
	call %[bpf_strtoul];				\
	r0 = 1;						\
	exit;						\
"	:
	: __imm(bpf_strtoul)
	: __clobber_all);
}

SEC("socket")
__description("ARG_PTR_TO_LONG half-uninitialized")
/* in privileged mode reads from uninitialized stack locations are permitted */
__success __failure_unpriv
__msg_unpriv("invalid indirect read from stack R4 off -16+4 size 8")
__retval(0)
__naked void ptr_to_long_half_uninitialized(void)
{
	asm volatile ("					\
	/* bpf_strtoul arg1 (buf) */			\
	r7 = r10;					\
	r7 += -8;					\
	r0 = 0x00303036;				\
	*(u64*)(r7 + 0) = r0;				\
	r1 = r7;					\
	/* bpf_strtoul arg2 (buf_len) */		\
	r2 = 4;						\
	/* bpf_strtoul arg3 (flags) */			\
	r3 = 0;						\
	/* bpf_strtoul arg4 (res) */			\
	r7 += -8;					\
	*(u32*)(r7 + 0) = r0;				\
	r4 = r7;					\
	/* bpf_strtoul() */				\
	call %[bpf_strtoul];				\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_strtoul)
	: __clobber_all);
}

SEC("cgroup/sysctl")
__description("ARG_PTR_TO_LONG misaligned")
__failure __msg("misaligned stack access off 0+-20+0 size 8")
__naked void arg_ptr_to_long_misaligned(void)
{
	asm volatile ("					\
	/* bpf_strtoul arg1 (buf) */			\
	r7 = r10;					\
	r7 += -8;					\
	r0 = 0x00303036;				\
	*(u64*)(r7 + 0) = r0;				\
	r1 = r7;					\
	/* bpf_strtoul arg2 (buf_len) */		\
	r2 = 4;						\
	/* bpf_strtoul arg3 (flags) */			\
	r3 = 0;						\
	/* bpf_strtoul arg4 (res) */			\
	r7 += -12;					\
	r0 = 0;						\
	*(u32*)(r7 + 0) = r0;				\
	*(u64*)(r7 + 4) = r0;				\
	r4 = r7;					\
	/* bpf_strtoul() */				\
	call %[bpf_strtoul];				\
	r0 = 1;						\
	exit;						\
"	:
	: __imm(bpf_strtoul)
	: __clobber_all);
}

SEC("cgroup/sysctl")
__description("ARG_PTR_TO_LONG size < sizeof(long)")
__failure __msg("invalid indirect access to stack R4 off=-4 size=8")
__naked void to_long_size_sizeof_long(void)
{
	asm volatile ("					\
	/* bpf_strtoul arg1 (buf) */			\
	r7 = r10;					\
	r7 += -16;					\
	r0 = 0x00303036;				\
	*(u64*)(r7 + 0) = r0;				\
	r1 = r7;					\
	/* bpf_strtoul arg2 (buf_len) */		\
	r2 = 4;						\
	/* bpf_strtoul arg3 (flags) */			\
	r3 = 0;						\
	/* bpf_strtoul arg4 (res) */			\
	r7 += 12;					\
	*(u32*)(r7 + 0) = r0;				\
	r4 = r7;					\
	/* bpf_strtoul() */				\
	call %[bpf_strtoul];				\
	r0 = 1;						\
	exit;						\
"	:
	: __imm(bpf_strtoul)
	: __clobber_all);
}

SEC("cgroup/sysctl")
__description("ARG_PTR_TO_LONG initialized")
__success
__naked void arg_ptr_to_long_initialized(void)
{
	asm volatile ("					\
	/* bpf_strtoul arg1 (buf) */			\
	r7 = r10;					\
	r7 += -8;					\
	r0 = 0x00303036;				\
	*(u64*)(r7 + 0) = r0;				\
	r1 = r7;					\
	/* bpf_strtoul arg2 (buf_len) */		\
	r2 = 4;						\
	/* bpf_strtoul arg3 (flags) */			\
	r3 = 0;						\
	/* bpf_strtoul arg4 (res) */			\
	r7 += -8;					\
	*(u64*)(r7 + 0) = r0;				\
	r4 = r7;					\
	/* bpf_strtoul() */				\
	call %[bpf_strtoul];				\
	r0 = 1;						\
	exit;						\
"	:
	: __imm(bpf_strtoul)
	: __clobber_all);
}

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