Skip to content

Commit f471748

Browse files
Byte-LabAlexei Starovoitov
authored andcommitted
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>
1 parent 3f0e6f2 commit f471748

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

tools/testing/selftests/bpf/prog_tests/task_kfunc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ static const char * const success_tests[] = {
7878
"test_task_xchg_release",
7979
"test_task_get_release",
8080
"test_task_current_acquire_release",
81+
"test_task_from_pid_arg",
82+
"test_task_from_pid_current",
83+
"test_task_from_pid_invalid",
8184
};
8285

8386
static struct {
@@ -99,6 +102,7 @@ static struct {
99102
{"task_kfunc_release_fp", "arg#0 pointer type STRUCT task_struct must point"},
100103
{"task_kfunc_release_null", "arg#0 is ptr_or_null_ expected ptr_ or socket"},
101104
{"task_kfunc_release_unacquired", "release kernel function bpf_task_release expects"},
105+
{"task_kfunc_from_pid_no_null_check", "arg#0 is ptr_or_null_ expected ptr_ or socket"},
102106
};
103107

104108
static void verify_fail(const char *prog_name, const char *expected_err_msg)

tools/testing/selftests/bpf/progs/task_kfunc_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct hash_map {
2323
struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
2424
struct task_struct *bpf_task_kptr_get(struct task_struct **pp) __ksym;
2525
void bpf_task_release(struct task_struct *p) __ksym;
26+
struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
2627

2728
static inline struct __tasks_kfunc_map_value *tasks_kfunc_map_value_lookup(struct task_struct *p)
2829
{

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,16 @@ int BPF_PROG(task_kfunc_release_unacquired, struct task_struct *task, u64 clone_
258258

259259
return 0;
260260
}
261+
262+
SEC("tp_btf/task_newtask")
263+
int BPF_PROG(task_kfunc_from_pid_no_null_check, struct task_struct *task, u64 clone_flags)
264+
{
265+
struct task_struct *acquired;
266+
267+
acquired = bpf_task_from_pid(task->pid);
268+
269+
/* Releasing bpf_task_from_pid() lookup without a NULL check. */
270+
bpf_task_release(acquired);
271+
272+
return 0;
273+
}

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,76 @@ int BPF_PROG(test_task_current_acquire_release, struct task_struct *task, u64 cl
147147

148148
return 0;
149149
}
150+
151+
static void lookup_compare_pid(const struct task_struct *p)
152+
{
153+
struct task_struct *acquired;
154+
155+
acquired = bpf_task_from_pid(p->pid);
156+
if (!acquired) {
157+
err = 1;
158+
return;
159+
}
160+
161+
if (acquired->pid != p->pid)
162+
err = 2;
163+
bpf_task_release(acquired);
164+
}
165+
166+
SEC("tp_btf/task_newtask")
167+
int BPF_PROG(test_task_from_pid_arg, struct task_struct *task, u64 clone_flags)
168+
{
169+
struct task_struct *acquired;
170+
171+
if (!is_test_kfunc_task())
172+
return 0;
173+
174+
lookup_compare_pid(task);
175+
return 0;
176+
}
177+
178+
SEC("tp_btf/task_newtask")
179+
int BPF_PROG(test_task_from_pid_current, struct task_struct *task, u64 clone_flags)
180+
{
181+
struct task_struct *current, *acquired;
182+
183+
if (!is_test_kfunc_task())
184+
return 0;
185+
186+
lookup_compare_pid(bpf_get_current_task_btf());
187+
return 0;
188+
}
189+
190+
static int is_pid_lookup_valid(s32 pid)
191+
{
192+
struct task_struct *acquired;
193+
194+
acquired = bpf_task_from_pid(pid);
195+
if (acquired) {
196+
bpf_task_release(acquired);
197+
return 1;
198+
}
199+
200+
return 0;
201+
}
202+
203+
SEC("tp_btf/task_newtask")
204+
int BPF_PROG(test_task_from_pid_invalid, struct task_struct *task, u64 clone_flags)
205+
{
206+
struct task_struct *acquired;
207+
208+
if (!is_test_kfunc_task())
209+
return 0;
210+
211+
if (is_pid_lookup_valid(-1)) {
212+
err = 1;
213+
return 0;
214+
}
215+
216+
if (is_pid_lookup_valid(0xcafef00d)) {
217+
err = 2;
218+
return 0;
219+
}
220+
221+
return 0;
222+
}

0 commit comments

Comments
 (0)