bpf: Fix an issue in bpf_iter_task
Subsystems (4)
v2 cover bpf: Fix an issue in bpf_iter_task
The uninitialized bpf_iter_task variable poses a risk of triggering a kernel panic. To fix this potential issue, it's imperative to ensure proper initialization of the variable. This problem surfaced during the implementation phase of the bits iterator [0].
[0]. https://lwn.net/ml/bpf/CALOAHbDJWHOB+viBz6SUqdeF+Nkxmh4gLZo5Ad_keQXjBWHAsQ@mail.gmail.com
v1->v2:
- Correct the fixes tag (Chuyi)
Yafang Shao (2):
bpf: Fix an issue due to uninitialized bpf_iter_task selftests/bpf: Add negtive test cases for task iter
kernel/bpf/task_iter.c | 2 ++ tools/testing/selftests/bpf/prog_tests/iters.c | 1 + tools/testing/selftests/bpf/progs/iters_task.c | 12 +++++++++++- 3 files changed, 14 insertions(+), 1 deletion(-)
Signature · 1 line
2.39.1
PATCH v2 1/2 bpf: Fix an issue due to uninitialized bpf_iter_task
Failure to initialize it->pos, coupled with the presence of an invalid value in the flags variable, can lead to it->pos referencing an invalid task, potentially resulting in a kernel panic. To mitigate this risk, it's crucial to ensure proper initialization of it->pos to NULL.
--- kernel/bpf/task_iter.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/bpf/task_iter.c +2 -0
@@ -978,6 +978,8 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,BUILD_BUG_ON (__alignof__(struct bpf_iter_task_kern ) != __alignof__(struct bpf_iter_task )); + kit->pos =NULL ; +switch (flags) {case BPF_TASK_ITER_ALL_THREADS :case BPF_TASK_ITER_ALL_PROCS :
Signature · 1 line
2.39.1
Re: [PATCH v2 bpf-next 1/2] bpf: Fix an issue due to uninitialized bpf_iter_task
Quoted 7 lines · Yafang Shao
Failure to initialize
it->pos, coupled with the presence of an invalid value in the flags variable, can lead toit->posreferencing an invalid task, potentially resulting in a kernel panic. To mitigate this risk, it's crucial to ensure proper initialization ofit->posto NULL.Fixes ac8148d957f5 ("bpf: bpf_iter_task_next: use next_task(kit->task) rather than next_task(kit->pos)")
Confused...
Does this mean that bpf_iter_task_next() (the only user of ->pos) can be called even if bpf_iter_task_new() returns -EINVAL ?
Oleg.
Quoted 24 lines
Signed-off-by Yafang Shao <[email protected]>Acked-by Yonghong Song <[email protected]>Cc Chuyi Zhou <[email protected]>Cc Oleg Nesterov <[email protected]>--- kernel/bpf/task_iter.c | 2 ++ 1 file changed, 2 insertions(+)kernel/bpf/task_iter.c +2
-0@@ -978,6 +978,8 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,BUILD_BUG_ON (__alignof__(struct bpf_iter_task_kern ) != __alignof__(struct bpf_iter_task )); + kit->pos =NULL ; +switch (flags) {case BPF_TASK_ITER_ALL_THREADS :case BPF_TASK_ITER_ALL_PROCS :Signature · 1 line
2.39.1
Re: [PATCH v2 bpf-next 1/2] bpf: Fix an issue due to uninitialized bpf_iter_task
Quoted 8 lines · Oleg Nesterov
Quoted 6 lines · Yafang Shao
Failure to initialize
it->pos, coupled with the presence of an invalid value in the flags variable, can lead toit->posreferencing an invalid task, potentially resulting in a kernel panic. To mitigate this risk, it's crucial to ensure proper initialization ofit->posto NULL.
Quoted 6 lines
Quoted 1 line
Fixes ac8148d957f5 ("bpf: bpf_iter_task_next: use next_task(kit->task) rather than next_task(kit->pos)")Confused...
Does this mean that
bpf_iter_task_next()(the only user of ->pos) can be called even ifbpf_iter_task_new()returns-EINVAL?
Right. The bpf_for_each() doesn't check the return value of bpf_iter_task_new (), see also https://lore.kernel.org/bpf/[email protected]/
Even if we check the return value of bpf_iter_task_new() in bpf_for_each(), we still need to fix it in the kernel.
Quoted 28 lines
Oleg.
Quoted 24 lines
Signed-off-by Yafang Shao <[email protected]>Acked-by Yonghong Song <[email protected]>Cc Chuyi Zhou <[email protected]>Cc Oleg Nesterov <[email protected]>--- kernel/bpf/task_iter.c | 2 ++ 1 file changed, 2 insertions(+)kernel/bpf/task_iter.c +2
-1@@ -978,6 +978,8 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,BUILD_BUG_ON (__alignof__(struct bpf_iter_task_kern ) != __alignof__(struct bpf_iter_task )); + kit->pos =NULL ; +switch (flags) {case BPF_TASK_ITER_ALL_THREADS :case BPF_TASK_ITER_ALL_PROCS : --2.39.1
Signature · 2 lines
Regards Yafang
Oleg Nesterov
Acked-byRe: [PATCH v2 bpf-next 1/2] bpf: Fix an issue due to uninitialized bpf_iter_task
Quoted 7 lines · Yafang Shao
Quoted 5 lines · Oleg Nesterov
Quoted 2 lines
Fixes ac8148d957f5 ("bpf: bpf_iter_task_next: use next_task(kit->task) rather than next_task(kit->pos)")Confused...
Quoted 8 lines
Quoted 2 lines
Does this mean that
bpf_iter_task_next()(the only user of ->pos) can be called even ifbpf_iter_task_new()returns-EINVAL?Right. The
bpf_for_each()doesn't check the return value ofbpf_iter_task_new(), see also https://lore.kernel.org/bpf/[email protected]/Even if we check the return value of
bpf_iter_task_new()inbpf_for_each(), we still need to fix it in the kernel.
Hmm, OK. Somehow I naively thought there must be an in-kernel check that would that prevent bpf_iter_task_next() if bpf_iter_task_new() failed.
Thanks for your explanations. FWIW,
PATCH v2 2/2 selftests/bpf: Add negtive test cases for task iter
In reply to Yafang ShaoIncorporate 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.
--- tools/testing/selftests/bpf/prog_tests/iters.c | 1 + tools/testing/selftests/bpf/progs/iters_task.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/iters.c +1 -0
@@ -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" );
tools/testing/selftests/bpf/progs/iters_task.c +11 -1
@@ -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++;
Signature · 1 line
2.39.1
This series was applied to bpf/bpf.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Daniel Borkmann <[email protected]>:
Quoted 8 lines · On Sat, 17 Feb 2024 19:41:50 +0800 you wrote:
The uninitialized
bpf_iter_taskvariable poses a risk of triggering a kernel panic. To fix this potential issue, it's imperative to ensure proper initialization of the variable. This problem surfaced during the implementation phase of the bits iterator [0].[0]. https://lwn.net/ml/bpf/CALOAHbDJWHOB+viBz6SUqdeF+Nkxmh4gLZo5Ad_keQXjBWHAsQ@mail.gmail.com
[...]
Here is the summary with links:
- [v2,bpf-next,1/2] bpf: Fix an issue due to uninitialized bpf_iter_task
https://git.kernel.org/bpf/bpf/c/5f2ae606cb5a - [v2,bpf-next,2/2] selftests/bpf: Add negtive test cases for task iter https://git.kernel.org/bpf/bpf/c/5c138a8a4abe
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html