Commit 5c138a8a authored by Yafang Shao's avatar Yafang Shao Committed by Daniel Borkmann
Browse files

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: default avatarYafang Shao <laoar.shao@gmail.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20240217114152.1623-3-laoar.shao@gmail.com
parent 5f2ae606
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -193,6 +193,7 @@ static void subtest_task_iters(void)
	ASSERT_EQ(skel->bss->procs_cnt, 1, "procs_cnt");
	ASSERT_EQ(skel->bss->threads_cnt, thread_num + 1, "threads_cnt");
	ASSERT_EQ(skel->bss->proc_threads_cnt, thread_num + 1, "proc_threads_cnt");
	ASSERT_EQ(skel->bss->invalid_cnt, 0, "invalid_cnt");
	pthread_mutex_unlock(&do_nothing_mutex);
	for (int i = 0; i < thread_num; i++)
		ASSERT_OK(pthread_join(thread_ids[i], &ret), "pthread_join");
+11 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@
char _license[] SEC("license") = "GPL";

pid_t target_pid;
int procs_cnt, threads_cnt, proc_threads_cnt;
int procs_cnt, threads_cnt, proc_threads_cnt, invalid_cnt;

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

	bpf_rcu_read_lock();
	bpf_for_each(task, pos, NULL, ~0U) {
		/* Below instructions shouldn't be executed for invalid flags */
		invalid_cnt++;
	}

	bpf_for_each(task, pos, NULL, BPF_TASK_ITER_PROC_THREADS) {
		/* Below instructions shouldn't be executed for invalid task__nullable */
		invalid_cnt++;
	}

	bpf_for_each(task, pos, NULL, BPF_TASK_ITER_ALL_PROCS)
		if (pos->pid == target_pid)
			procs_cnt++;