Skip to content

Commit 23599ad

Browse files
borkmannAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Add selftest cases for ctx_or_null argument type
Add various tests to make sure the verifier keeps catching them: # ./test_verifier [...] torvalds#230/p pass ctx or null check, 1: ctx OK torvalds#231/p pass ctx or null check, 2: null OK torvalds#232/p pass ctx or null check, 3: 1 OK torvalds#233/p pass ctx or null check, 4: ctx - const OK torvalds#234/p pass ctx or null check, 5: null (connect) OK torvalds#235/p pass ctx or null check, 6: null (bind) OK torvalds#236/p pass ctx or null check, 7: ctx (bind) OK torvalds#237/p pass ctx or null check, 8: null (bind) OK [...] Summary: 1595 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/c74758d07b1b678036465ef7f068a49e9efd3548.1585323121.git.daniel@iogearbox.net
1 parent 834ebca commit 23599ad

File tree

1 file changed

+105
-0
lines changed
  • tools/testing/selftests/bpf/verifier

1 file changed

+105
-0
lines changed

tools/testing/selftests/bpf/verifier/ctx.c

+105
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,108 @@
9191
.result = REJECT,
9292
.errstr = "variable ctx access var_off=(0x0; 0x4)",
9393
},
94+
{
95+
"pass ctx or null check, 1: ctx",
96+
.insns = {
97+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
98+
BPF_FUNC_get_netns_cookie),
99+
BPF_MOV64_IMM(BPF_REG_0, 0),
100+
BPF_EXIT_INSN(),
101+
},
102+
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
103+
.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG,
104+
.result = ACCEPT,
105+
},
106+
{
107+
"pass ctx or null check, 2: null",
108+
.insns = {
109+
BPF_MOV64_IMM(BPF_REG_1, 0),
110+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
111+
BPF_FUNC_get_netns_cookie),
112+
BPF_MOV64_IMM(BPF_REG_0, 0),
113+
BPF_EXIT_INSN(),
114+
},
115+
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
116+
.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG,
117+
.result = ACCEPT,
118+
},
119+
{
120+
"pass ctx or null check, 3: 1",
121+
.insns = {
122+
BPF_MOV64_IMM(BPF_REG_1, 1),
123+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
124+
BPF_FUNC_get_netns_cookie),
125+
BPF_MOV64_IMM(BPF_REG_0, 0),
126+
BPF_EXIT_INSN(),
127+
},
128+
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
129+
.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG,
130+
.result = REJECT,
131+
.errstr = "R1 type=inv expected=ctx",
132+
},
133+
{
134+
"pass ctx or null check, 4: ctx - const",
135+
.insns = {
136+
BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -612),
137+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
138+
BPF_FUNC_get_netns_cookie),
139+
BPF_MOV64_IMM(BPF_REG_0, 0),
140+
BPF_EXIT_INSN(),
141+
},
142+
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
143+
.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG,
144+
.result = REJECT,
145+
.errstr = "dereference of modified ctx ptr",
146+
},
147+
{
148+
"pass ctx or null check, 5: null (connect)",
149+
.insns = {
150+
BPF_MOV64_IMM(BPF_REG_1, 0),
151+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
152+
BPF_FUNC_get_netns_cookie),
153+
BPF_MOV64_IMM(BPF_REG_0, 0),
154+
BPF_EXIT_INSN(),
155+
},
156+
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
157+
.expected_attach_type = BPF_CGROUP_INET4_CONNECT,
158+
.result = ACCEPT,
159+
},
160+
{
161+
"pass ctx or null check, 6: null (bind)",
162+
.insns = {
163+
BPF_MOV64_IMM(BPF_REG_1, 0),
164+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
165+
BPF_FUNC_get_netns_cookie),
166+
BPF_MOV64_IMM(BPF_REG_0, 0),
167+
BPF_EXIT_INSN(),
168+
},
169+
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
170+
.expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
171+
.result = ACCEPT,
172+
},
173+
{
174+
"pass ctx or null check, 7: ctx (bind)",
175+
.insns = {
176+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
177+
BPF_FUNC_get_socket_cookie),
178+
BPF_MOV64_IMM(BPF_REG_0, 0),
179+
BPF_EXIT_INSN(),
180+
},
181+
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
182+
.expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
183+
.result = ACCEPT,
184+
},
185+
{
186+
"pass ctx or null check, 8: null (bind)",
187+
.insns = {
188+
BPF_MOV64_IMM(BPF_REG_1, 0),
189+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
190+
BPF_FUNC_get_socket_cookie),
191+
BPF_MOV64_IMM(BPF_REG_0, 0),
192+
BPF_EXIT_INSN(),
193+
},
194+
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK,
195+
.expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
196+
.result = REJECT,
197+
.errstr = "R1 type=inv expected=ctx",
198+
},

0 commit comments

Comments
 (0)