Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <laoar.shao@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20240217114152.1623-3-laoar.shao@gmail.com

authored by

Yafang Shao and committed by
Daniel Borkmann
5c138a8a 5f2ae606

+12 -1
+1
tools/testing/selftests/bpf/prog_tests/iters.c
··· 193 193 ASSERT_EQ(skel->bss->procs_cnt, 1, "procs_cnt"); 194 194 ASSERT_EQ(skel->bss->threads_cnt, thread_num + 1, "threads_cnt"); 195 195 ASSERT_EQ(skel->bss->proc_threads_cnt, thread_num + 1, "proc_threads_cnt"); 196 + ASSERT_EQ(skel->bss->invalid_cnt, 0, "invalid_cnt"); 196 197 pthread_mutex_unlock(&do_nothing_mutex); 197 198 for (int i = 0; i < thread_num; i++) 198 199 ASSERT_OK(pthread_join(thread_ids[i], &ret), "pthread_join");
+11 -1
tools/testing/selftests/bpf/progs/iters_task.c
··· 10 10 char _license[] SEC("license") = "GPL"; 11 11 12 12 pid_t target_pid; 13 - int procs_cnt, threads_cnt, proc_threads_cnt; 13 + int procs_cnt, threads_cnt, proc_threads_cnt, invalid_cnt; 14 14 15 15 void bpf_rcu_read_lock(void) __ksym; 16 16 void bpf_rcu_read_unlock(void) __ksym; ··· 26 26 procs_cnt = threads_cnt = proc_threads_cnt = 0; 27 27 28 28 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 + 29 39 bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_PROCS) 30 40 if (pos->pid == target_pid) 31 41 procs_cnt++;