Commit 54ac2c94 authored by Eduard Zingerman's avatar Eduard Zingerman Committed by Alexei Starovoitov
Browse files

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: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: default avatarEduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20250704230354.1323244-7-eddyz87@gmail.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent aaa0e57e
Loading
Loading
Loading
Loading
+81 −0
Original line number Diff line number Diff line
@@ -179,4 +179,85 @@ int BPF_PROG(trusted_acq_rel, struct task_struct *task, u64 clone_flags)
	return subprog_trusted_acq_rel(task);
}

__weak int subprog_untrusted_bad_tags(struct task_struct *task __arg_untrusted __arg_nullable)
{
	return task->pid;
}

SEC("tp_btf/sys_enter")
__failure
__msg("arg#0 untrusted cannot be combined with any other tags")
int untrusted_bad_tags(void *ctx)
{
	return subprog_untrusted_bad_tags(0);
}

struct local_type_wont_be_accepted {};

__weak int subprog_untrusted_bad_type(struct local_type_wont_be_accepted *p __arg_untrusted)
{
	return 0;
}

SEC("tp_btf/sys_enter")
__failure
__msg("arg#0 reference type('STRUCT local_type_wont_be_accepted') has no matches")
int untrusted_bad_type(void *ctx)
{
	return subprog_untrusted_bad_type(bpf_rdonly_cast(0, 0));
}

__weak int subprog_untrusted(const volatile struct task_struct *restrict task __arg_untrusted)
{
	return task->pid;
}

SEC("tp_btf/sys_enter")
__success
__log_level(2)
__msg("r1 = {{.*}}; {{.*}}R1_w=trusted_ptr_task_struct()")
__msg("Func#1 ('subprog_untrusted') is global and assumed valid.")
__msg("Validating subprog_untrusted() func#1...")
__msg(": R1=untrusted_ptr_task_struct")
int trusted_to_untrusted(void *ctx)
{
	return subprog_untrusted(bpf_get_current_task_btf());
}

char mem[16];
u32 off;

SEC("tp_btf/sys_enter")
__success
int anything_to_untrusted(void *ctx)
{
	/* untrusted to untrusted */
	subprog_untrusted(bpf_core_cast(0, struct task_struct));
	/* wrong type to untrusted */
	subprog_untrusted((void *)bpf_core_cast(0, struct bpf_verifier_env));
	/* map value to untrusted */
	subprog_untrusted((void *)mem);
	/* scalar to untrusted */
	subprog_untrusted(0);
	/* variable offset to untrusted (map) */
	subprog_untrusted((void *)mem + off);
	/* variable offset to untrusted (trusted) */
	subprog_untrusted((void *)bpf_get_current_task_btf() + off);
	return 0;
}

__weak int subprog_untrusted2(struct task_struct *task __arg_untrusted)
{
	return subprog_trusted_task_nullable(task);
}

SEC("tp_btf/sys_enter")
__failure
__msg("R1 type=untrusted_ptr_ expected=ptr_, trusted_ptr_, rcu_ptr_")
__msg("Caller passes invalid args into func#{{.*}} ('subprog_trusted_task_nullable')")
int untrusted_to_trusted(void *ctx)
{
	return subprog_untrusted2(bpf_get_current_task_btf());
}

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