selftests/bpf: Add selftests for bpf_task_from_pid()
Add some selftest testcases that validate the expected behavior of the bpf_task_from_pid() kfunc that was added in the prior patch. Signed-off-by: David Vernet <void@manifault.com> Link: https://lore.kernel.org/r/20221122145300.251210-3-void@manifault.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
3f0e6f2b41
commit
f471748b7f
|
@ -78,6 +78,9 @@ static const char * const success_tests[] = {
|
|||
"test_task_xchg_release",
|
||||
"test_task_get_release",
|
||||
"test_task_current_acquire_release",
|
||||
"test_task_from_pid_arg",
|
||||
"test_task_from_pid_current",
|
||||
"test_task_from_pid_invalid",
|
||||
};
|
||||
|
||||
static struct {
|
||||
|
@ -99,6 +102,7 @@ static struct {
|
|||
{"task_kfunc_release_fp", "arg#0 pointer type STRUCT task_struct must point"},
|
||||
{"task_kfunc_release_null", "arg#0 is ptr_or_null_ expected ptr_ or socket"},
|
||||
{"task_kfunc_release_unacquired", "release kernel function bpf_task_release expects"},
|
||||
{"task_kfunc_from_pid_no_null_check", "arg#0 is ptr_or_null_ expected ptr_ or socket"},
|
||||
};
|
||||
|
||||
static void verify_fail(const char *prog_name, const char *expected_err_msg)
|
||||
|
|
|
@ -23,6 +23,7 @@ struct hash_map {
|
|||
struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
|
||||
struct task_struct *bpf_task_kptr_get(struct task_struct **pp) __ksym;
|
||||
void bpf_task_release(struct task_struct *p) __ksym;
|
||||
struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
|
||||
|
||||
static inline struct __tasks_kfunc_map_value *tasks_kfunc_map_value_lookup(struct task_struct *p)
|
||||
{
|
||||
|
|
|
@ -258,3 +258,16 @@ int BPF_PROG(task_kfunc_release_unacquired, struct task_struct *task, u64 clone_
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("tp_btf/task_newtask")
|
||||
int BPF_PROG(task_kfunc_from_pid_no_null_check, struct task_struct *task, u64 clone_flags)
|
||||
{
|
||||
struct task_struct *acquired;
|
||||
|
||||
acquired = bpf_task_from_pid(task->pid);
|
||||
|
||||
/* Releasing bpf_task_from_pid() lookup without a NULL check. */
|
||||
bpf_task_release(acquired);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -147,3 +147,76 @@ int BPF_PROG(test_task_current_acquire_release, struct task_struct *task, u64 cl
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void lookup_compare_pid(const struct task_struct *p)
|
||||
{
|
||||
struct task_struct *acquired;
|
||||
|
||||
acquired = bpf_task_from_pid(p->pid);
|
||||
if (!acquired) {
|
||||
err = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (acquired->pid != p->pid)
|
||||
err = 2;
|
||||
bpf_task_release(acquired);
|
||||
}
|
||||
|
||||
SEC("tp_btf/task_newtask")
|
||||
int BPF_PROG(test_task_from_pid_arg, struct task_struct *task, u64 clone_flags)
|
||||
{
|
||||
struct task_struct *acquired;
|
||||
|
||||
if (!is_test_kfunc_task())
|
||||
return 0;
|
||||
|
||||
lookup_compare_pid(task);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("tp_btf/task_newtask")
|
||||
int BPF_PROG(test_task_from_pid_current, struct task_struct *task, u64 clone_flags)
|
||||
{
|
||||
struct task_struct *current, *acquired;
|
||||
|
||||
if (!is_test_kfunc_task())
|
||||
return 0;
|
||||
|
||||
lookup_compare_pid(bpf_get_current_task_btf());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int is_pid_lookup_valid(s32 pid)
|
||||
{
|
||||
struct task_struct *acquired;
|
||||
|
||||
acquired = bpf_task_from_pid(pid);
|
||||
if (acquired) {
|
||||
bpf_task_release(acquired);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("tp_btf/task_newtask")
|
||||
int BPF_PROG(test_task_from_pid_invalid, struct task_struct *task, u64 clone_flags)
|
||||
{
|
||||
struct task_struct *acquired;
|
||||
|
||||
if (!is_test_kfunc_task())
|
||||
return 0;
|
||||
|
||||
if (is_pid_lookup_valid(-1)) {
|
||||
err = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (is_pid_lookup_valid(0xcafef00d)) {
|
||||
err = 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue