Skip to content

Commit 54ac2c9

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: test cases for __arg_untrusted
Check usage of __arg_untrusted parameters with PTR_TO_BTF_ID: - combining __arg_untrusted with other tags is forbidden; - non-kernel (program local) types for __arg_untrusted are forbidden; - passing of {trusted, untrusted, map value, scalar value, values with variable offset} to untrusted is ok; - passing of PTR_TO_BTF_ID with a different type to untrusted is ok; - passing of untrusted to trusted is forbidden. Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent aaa0e57 commit 54ac2c9

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

tools/testing/selftests/bpf/progs/verifier_global_ptr_args.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,85 @@ int BPF_PROG(trusted_acq_rel, struct task_struct *task, u64 clone_flags)
179179
return subprog_trusted_acq_rel(task);
180180
}
181181

182+
__weak int subprog_untrusted_bad_tags(struct task_struct *task __arg_untrusted __arg_nullable)
183+
{
184+
return task->pid;
185+
}
186+
187+
SEC("tp_btf/sys_enter")
188+
__failure
189+
__msg("arg#0 untrusted cannot be combined with any other tags")
190+
int untrusted_bad_tags(void *ctx)
191+
{
192+
return subprog_untrusted_bad_tags(0);
193+
}
194+
195+
struct local_type_wont_be_accepted {};
196+
197+
__weak int subprog_untrusted_bad_type(struct local_type_wont_be_accepted *p __arg_untrusted)
198+
{
199+
return 0;
200+
}
201+
202+
SEC("tp_btf/sys_enter")
203+
__failure
204+
__msg("arg#0 reference type('STRUCT local_type_wont_be_accepted') has no matches")
205+
int untrusted_bad_type(void *ctx)
206+
{
207+
return subprog_untrusted_bad_type(bpf_rdonly_cast(0, 0));
208+
}
209+
210+
__weak int subprog_untrusted(const volatile struct task_struct *restrict task __arg_untrusted)
211+
{
212+
return task->pid;
213+
}
214+
215+
SEC("tp_btf/sys_enter")
216+
__success
217+
__log_level(2)
218+
__msg("r1 = {{.*}}; {{.*}}R1_w=trusted_ptr_task_struct()")
219+
__msg("Func#1 ('subprog_untrusted') is global and assumed valid.")
220+
__msg("Validating subprog_untrusted() func#1...")
221+
__msg(": R1=untrusted_ptr_task_struct")
222+
int trusted_to_untrusted(void *ctx)
223+
{
224+
return subprog_untrusted(bpf_get_current_task_btf());
225+
}
226+
227+
char mem[16];
228+
u32 off;
229+
230+
SEC("tp_btf/sys_enter")
231+
__success
232+
int anything_to_untrusted(void *ctx)
233+
{
234+
/* untrusted to untrusted */
235+
subprog_untrusted(bpf_core_cast(0, struct task_struct));
236+
/* wrong type to untrusted */
237+
subprog_untrusted((void *)bpf_core_cast(0, struct bpf_verifier_env));
238+
/* map value to untrusted */
239+
subprog_untrusted((void *)mem);
240+
/* scalar to untrusted */
241+
subprog_untrusted(0);
242+
/* variable offset to untrusted (map) */
243+
subprog_untrusted((void *)mem + off);
244+
/* variable offset to untrusted (trusted) */
245+
subprog_untrusted((void *)bpf_get_current_task_btf() + off);
246+
return 0;
247+
}
248+
249+
__weak int subprog_untrusted2(struct task_struct *task __arg_untrusted)
250+
{
251+
return subprog_trusted_task_nullable(task);
252+
}
253+
254+
SEC("tp_btf/sys_enter")
255+
__failure
256+
__msg("R1 type=untrusted_ptr_ expected=ptr_, trusted_ptr_, rcu_ptr_")
257+
__msg("Caller passes invalid args into func#{{.*}} ('subprog_trusted_task_nullable')")
258+
int untrusted_to_trusted(void *ctx)
259+
{
260+
return subprog_untrusted2(bpf_get_current_task_btf());
261+
}
262+
182263
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)