Skip to content

Commit 5c138a8

Browse files
laoarborkmann
authored andcommitted
selftests/bpf: Add negtive test cases for task iter
Incorporate a test case to assess the handling of invalid flags or task__nullable parameters passed to bpf_iter_task_new(). Prior to the preceding commit, this scenario could potentially trigger a kernel panic. However, with the previous commit, this test case is expected to function correctly. Signed-off-by: Yafang Shao <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5f2ae60 commit 5c138a8

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ static void subtest_task_iters(void)
193193
ASSERT_EQ(skel->bss->procs_cnt, 1, "procs_cnt");
194194
ASSERT_EQ(skel->bss->threads_cnt, thread_num + 1, "threads_cnt");
195195
ASSERT_EQ(skel->bss->proc_threads_cnt, thread_num + 1, "proc_threads_cnt");
196+
ASSERT_EQ(skel->bss->invalid_cnt, 0, "invalid_cnt");
196197
pthread_mutex_unlock(&do_nothing_mutex);
197198
for (int i = 0; i < thread_num; i++)
198199
ASSERT_OK(pthread_join(thread_ids[i], &ret), "pthread_join");

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
char _license[] SEC("license") = "GPL";
1111

1212
pid_t target_pid;
13-
int procs_cnt, threads_cnt, proc_threads_cnt;
13+
int procs_cnt, threads_cnt, proc_threads_cnt, invalid_cnt;
1414

1515
void bpf_rcu_read_lock(void) __ksym;
1616
void bpf_rcu_read_unlock(void) __ksym;
@@ -26,6 +26,16 @@ int iter_task_for_each_sleep(void *ctx)
2626
procs_cnt = threads_cnt = proc_threads_cnt = 0;
2727

2828
bpf_rcu_read_lock();
29+
bpf_for_each(task, pos, NULL, ~0U) {
30+
/* Below instructions shouldn't be executed for invalid flags */
31+
invalid_cnt++;
32+
}
33+
34+
bpf_for_each(task, pos, NULL, BPF_TASK_ITER_PROC_THREADS) {
35+
/* Below instructions shouldn't be executed for invalid task__nullable */
36+
invalid_cnt++;
37+
}
38+
2939
bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_PROCS)
3040
if (pos->pid == target_pid)
3141
procs_cnt++;

0 commit comments

Comments
 (0)