bpf: fix the crash caused by task iterators over vma
Subsystems (4)
cover bpf: fix the crash caused by task iterators over vma
This issue is related to task iterators over vma. A system crash can occur when a task iterator travels through vma of tasks as the death of a task will clear the pointer to its mm, even though the task_struct is still held. As a result, an unexpected crash happens due to a null pointer. To address this problem, a reference to mm is kept on the iterator to make sure that the pointer is always valid. This patch set provides a solution for this crash by properly referencing mm on task iterators over vma.
Kui-Feng Lee (2):
bpf: keep a reference to the mm, in case the task is dead. selftests/bpf: create new processes repeatedly in the background.
kernel/bpf/task_iter.c | 39 ++++++--- .../selftests/bpf/prog_tests/bpf_iter.c | 79 +++++++++++++++++++ 2 files changed, 106 insertions(+), 12 deletions(-)
Signature · 1 line
2.30.2
PATCH 1/2 bpf: keep a reference to the mm, in case the task is dead.
Fix the system crash that happens when a task iterator travel through vma of tasks.
In task iterators, we used to access mm by following the pointer on the task_struct; however, the death of a task will clear the pointer, even though we still hold the task_struct. That can cause an unexpected crash for a null pointer when an iterator is visiting a task that dies during the visit. Keeping a reference of mm on the iterator ensures we always have a valid pointer to mm.
--- kernel/bpf/task_iter.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-)
kernel/bpf/task_iter.c +27 -12
@@ -438,6 +438,7 @@ struct bpf_iter_seq_task_vma_info {*/ struct bpf_iter_seq_task_common common;struct task_struct *task; +struct mm_struct *mm;struct vm_area_struct *vma;u32 tid;unsigned long prev_vm_start;
@@ -456,16 +457,19 @@ task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)enum bpf_task_vma_iter_find_op op;struct vm_area_struct *curr_vma;struct task_struct *curr_task; +struct mm_struct *curr_mm;u32 saved_tid = info->tid;/* If this function returns a non-NULL vma, it holds a reference to -* the task_struct, and holds read lock on vma->mm->mmap_lock. +* the task_struct, holds a refcount on mm->mm_users, and holds +* read lock on vma->mm->mmap_lock. * If this function returns NULL, it does not hold any reference or * lock. */ if (info->task) { curr_task = info->task; curr_vma = info->vma; + curr_mm = info->mm;/* In case of lock contention, drop mmap_lock to unblock * the writer. *
@@ -504,13 +508,15 @@ task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)* 4.2) VMA2 and VMA2' covers different ranges, process * VMA2'. */ -if (mmap_lock_is_contended (curr_task->mm)) { +if (mmap_lock_is_contended (curr_mm)) { info->prev_vm_start = curr_vma->vm_start; info->prev_vm_end = curr_vma->vm_end; op = task_vma_iter_find_vma; -mmap_read_unlock (curr_task->mm); -if (mmap_read_lock_killable (curr_task->mm)) +mmap_read_unlock (curr_mm); +if (mmap_read_lock_killable (curr_mm)) { +mmput (curr_mm); goto finish; + } }else { op = task_vma_iter_next_vma; }
@@ -535,42 +541,47 @@ task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info) op = task_vma_iter_find_vma; } -if (!curr_task->mm) + curr_mm =get_task_mm (curr_task); +if (!curr_mm) goto next_task; -if (mmap_read_lock_killable (curr_task->mm)) +if (mmap_read_lock_killable (curr_mm)) { +mmput (curr_mm); goto finish; + } }switch (op) {case task_vma_iter_first_vma: - curr_vma =find_vma (curr_task->mm,0 ); + curr_vma =find_vma (curr_mm,0 );break ;case task_vma_iter_next_vma: - curr_vma =find_vma (curr_task->mm, curr_vma->vm_end); + curr_vma =find_vma (curr_mm, curr_vma->vm_end);break ;case task_vma_iter_find_vma:/* We dropped mmap_lock so it is necessary to use find_vma * to find the next vma. This is similar to the mechanism * in show_smaps_rollup(). */ - curr_vma =find_vma (curr_task->mm, info->prev_vm_end -1 ); + curr_vma =find_vma (curr_mm, info->prev_vm_end -1 );/* case 1) and 4.2) above just use curr_vma */ /* check for case 2) or case 4.1) above */ if (curr_vma && curr_vma->vm_start == info->prev_vm_start && curr_vma->vm_end == info->prev_vm_end) - curr_vma =find_vma (curr_task->mm, curr_vma->vm_end); + curr_vma =find_vma (curr_mm, curr_vma->vm_end);break ; }if (!curr_vma) {/* case 3) above, or case 2) 4.1) with vma->next == NULL */ -mmap_read_unlock (curr_task->mm); +mmap_read_unlock (curr_mm); +mmput (curr_mm); goto next_task; } info->task = curr_task; info->vma = curr_vma; + info->mm = curr_mm;return curr_vma; next_task:
@@ -579,6 +590,7 @@ task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)put_task_struct (curr_task); info->task =NULL ; + info->mm =NULL ; info->tid++; goto again;
@@ -587,6 +599,7 @@ task_vma_seq_get_next(struct bpf_iter_seq_task_vma_info *info)put_task_struct (curr_task); info->task =NULL ; info->vma =NULL ; + info->mm =NULL ;return NULL ; }
@@ -658,7 +671,9 @@ static void task_vma_seq_stop(struct seq_file *seq, void *v)*/ info->prev_vm_start = ~0UL ; info->prev_vm_end = info->vma->vm_end; -mmap_read_unlock (info->task->mm); +mmap_read_unlock (info->mm); +mmput (info->mm); + info->mm =NULL ;put_task_struct (info->task); info->task =NULL ; }
Signature · 1 line
2.30.2
Re: [PATCH bpf-next 1/2] bpf: keep a reference to the mm, in case the task is dead.
Quoted 10 lines · Kui-Feng Lee
Fix the system crash that happens when a task iterator travel through vma of tasks.
In task iterators, we used to access mm by following the pointer on the
task_struct; however, the death of a task will clear the pointer, even though we still hold thetask_struct. That can cause an unexpected crash for a null pointer when an iterator is visiting a task that dies during the visit. Keeping a reference of mm on the iterator ensures we always have a valid pointer to mm.
Quoted 4 lines
Co-developed-by Song Liu <[email protected]>Signed-off-by Song Liu <[email protected]>Signed-off-by Kui-Feng Lee <[email protected]>Reported-by Nathan Slingerland <[email protected]>
PATCH 2/2 selftests/bpf: create new processes repeatedly in the background.
In reply to Kui-Feng LeeAccording to a report, the system may crash when a task iterator travels vma(s). The investigation shows it takes place if the visiting task dies during the visit.
This test case creates iterators repeatedly and forks short-lived processes in the background to detect this bug. The test will last for 3 seconds to get the chance to trigger the issue.
--- .../selftests/bpf/prog_tests/bpf_iter.c | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+)
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +79 -0
@@ -1465,6 +1465,83 @@ static void test_task_vma_common(struct bpf_iter_attach_opts *opts)bpf_iter_task_vma__destroy (skel); } +static void test_task_vma_dead_task (void ) +{ +int err, iter_fd =-1 ; +struct bpf_iter_task_vma *skel; +int wstatus, child_pid =-1 ; +time_t start_tm, cur_tm; +int wait_sec =3 ; + + skel =bpf_iter_task_vma__open (); +if (!ASSERT_OK_PTR (skel,"bpf_iter_task_vma__open" )) +return ; + + skel->bss->pid =getpid (); + + err =bpf_iter_task_vma__load (skel); +if (!ASSERT_OK (err,"bpf_iter_task_vma__load" )) + goto out; + + skel->links.proc_maps =bpf_program__attach_iter ( + skel->progs.proc_maps,NULL ); + +if (!ASSERT_OK_PTR (skel->links.proc_maps,"bpf_program__attach_iter" )) { + skel->links.proc_maps =NULL ; + goto out; + } + + start_tm =time (NULL ); +if (start_tm <0 ) + goto out; + cur_tm = start_tm; + + child_pid =fork (); +if (child_pid ==0 ) { +/* Fork short-lived processes in the background. */ +while (cur_tm < start_tm + wait_sec) { +system ("echo > /dev/null" ); + cur_tm =time (NULL ); +if (cur_tm <0 ) +exit (1 ); + } +exit (0 ); + } + +if (!ASSERT_GE (child_pid,0 ,"fork_child" )) + goto out; + +while (cur_tm < start_tm + wait_sec) { + iter_fd =bpf_iter_create (bpf_link__fd (skel->links.proc_maps)); +if (!ASSERT_GE (iter_fd,0 ,"create_iter" )) + goto out; + +/* Drain all data from iter_fd. */ +while (cur_tm < start_tm + wait_sec) { + err =read_fd_into_buffer (iter_fd, task_vma_output,CMP_BUFFER_SIZE ); +if (!ASSERT_GE (err,0 ,"read_iter_fd" )) + goto out; + + cur_tm =time (NULL ); +if (cur_tm <0 ) + goto out; + +if (err ==0 ) +break ; + } + +close (iter_fd); + iter_fd =-1 ; + } + +check_bpf_link_info (skel->progs.proc_maps); + +out: +waitpid (child_pid, &wstatus,0 ); +close (iter_fd); +bpf_iter_task_vma__destroy (skel); +} +void test_bpf_sockmap_map_iter_fd (void ) {struct bpf_iter_sockmap *skel;
@@ -1586,6 +1663,8 @@ void test_bpf_iter(void)test_task_file ();if (test__start_subtest ("task_vma" ))test_task_vma (); +if (test__start_subtest ("task_vma_dead_task" )) +test_task_vma_dead_task ();if (test__start_subtest ("task_btf" ))test_task_btf ();if (test__start_subtest ("tcp4" ))
Signature · 1 line
2.30.2
Re: [PATCH bpf-next 2/2] selftests/bpf: create new processes repeatedly in the background.
Quoted 1 line · Kui-Feng Lee
According to a report, the system may crash when a task iterator
There is no context about this 'a report'. You can just remove it and say:
When a task iterator traverses vma(s), it is possible task->mm might become invalid in the middle of traversal and this may cause kernel misbehave (e.g., crash).
Quoted 5 lines
travels vma(s). The investigation shows it takes place if the visiting task dies during the visit. >
This test case creates iterators repeatedly and forks short-lived processes in the background to detect this bug. The test will last for 3 seconds to get the chance to trigger the issue.
The subject is not precise. The test is not about "create new processes repeatedly in the background." It is about
"Add a test for iter/task_vma with shortlived processes"
Quoted 2 lines
Signed-off-by Kui-Feng Lee <[email protected]>
Ack with a few nits.
Quoted 9 lines
--- .../selftests/bpf/prog_tests/bpf_iter.c | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+)tools/testing/selftests/bpf/prog_tests/bpf_iter.c +0
-0
tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_task_vma_common(struct bpf_iter_attach_opts *opts) · line 1474
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +7
-0@@ -1465,6 +1465,83 @@ static void test_task_vma_common(struct bpf_iter_attach_opts *opts)bpf_iter_task_vma__destroy (skel); } +static void test_task_vma_dead_task (void ) +{ +int err, iter_fd =-1 ; +struct bpf_iter_task_vma *skel; +int wstatus, child_pid =-1 ; +time_t start_tm, cur_tm; +int wait_sec =3 ;
Since it is new code, maybe reverse Christmas tree coding style.
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +12
-0+ + skel =bpf_iter_task_vma__open (); +if (!ASSERT_OK_PTR (skel,"bpf_iter_task_vma__open" )) +return ; + + skel->bss->pid =getpid (); + + err =bpf_iter_task_vma__load (skel); +if (!ASSERT_OK (err,"bpf_iter_task_vma__load" )) + goto out; + + skel->links.proc_maps = bpf_program__attach_iter(
tools/testing/selftests/bpf/prog_tests/bpf_iter.c static void test_task_vma_common(struct bpf_iter_attach_opts *opts) · line 1496
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +10
-0+ skel->progs.proc_maps, NULL); + +if (!ASSERT_OK_PTR (skel->links.proc_maps,"bpf_program__attach_iter" )) { + skel->links.proc_maps =NULL ; + goto out; + } + + start_tm =time (NULL ); +if (start_tm <0 ) + goto out;
From the man page, start_tm should not fail. Note that you didn't put an ASSERT* either. So I think you can remove it. The same for a few instances below.
Quoted 60 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +50
-0+ cur_tm = start_tm; + + child_pid =fork (); +if (child_pid ==0 ) { +/* Fork short-lived processes in the background. */ +while (cur_tm < start_tm + wait_sec) { +system ("echo > /dev/null" ); + cur_tm =time (NULL ); +if (cur_tm <0 ) +exit (1 ); + } +exit (0 ); + } + +if (!ASSERT_GE (child_pid,0 ,"fork_child" )) + goto out; + +while (cur_tm < start_tm + wait_sec) { + iter_fd =bpf_iter_create (bpf_link__fd (skel->links.proc_maps)); +if (!ASSERT_GE (iter_fd,0 ,"create_iter" )) + goto out; + +/* Drain all data from iter_fd. */ +while (cur_tm < start_tm + wait_sec) { + err =read_fd_into_buffer (iter_fd, task_vma_output,CMP_BUFFER_SIZE ); +if (!ASSERT_GE (err,0 ,"read_iter_fd" )) + goto out; + + cur_tm =time (NULL ); +if (cur_tm <0 ) + goto out; + +if (err ==0 ) +break ; + } + +close (iter_fd); + iter_fd =-1 ; + } + +check_bpf_link_info (skel->progs.proc_maps); + +out: +waitpid (child_pid, &wstatus,0 ); +close (iter_fd); +bpf_iter_task_vma__destroy (skel); +} +void test_bpf_sockmap_map_iter_fd (void ) {struct bpf_iter_sockmap *skel;@@ -1586,6 +1663,8 @@ void test_bpf_iter(void)test_task_file ();if (test__start_subtest ("task_vma" ))test_task_vma (); +if (test__start_subtest ("task_vma_dead_task" )) +test_task_vma_dead_task ();if (test__start_subtest ("task_btf" ))test_task_btf ();if (test__start_subtest ("tcp4" ))
Re: [PATCH bpf-next 2/2] selftests/bpf: create new processes repeatedly in the background.
Quoted 23 lines · Yonghong Song
Quoted 1 line · Kui-Feng Lee
According to a report, the system may crash when a task iterator
There is no context about this 'a report'. You can just remove it and say:
When a task iterator traverses vma(s), it is possibletask->mmmight become invalid in the middle of traversal and this may cause kernel misbehave (e.g., crash).Quoted 5 lines
travels vma(s). The investigation shows it takes place if the visiting task dies during the visit. >
This test case creates iterators repeatedly and forks short-lived processes in the background to detect this bug. The test will last for 3 seconds to get the chance to trigger the issue.The subject is not precise. The test is not about "create new processes repeatedly in the background." It is about
"Add a test for iter/task_vma with shortlived processes"Quoted 1 line
Quoted 6 lines
Quoted 1 line
Signed-off-by Kui-Feng Lee <[email protected]>Ack with a few nits.
Acked-by Yonghong Song <[email protected]>
Thank you for the review.
Quoted 16 lines
Quoted 16 lines
---
.../selftests/bpf/prog_tests/bpf_iter.c | 79
+++++++++++++++++++1 file changed, 79 insertions(+)tools/testing/selftests/bpf/prog_tests/bpf_iter.c +0
-0@@ -1465,6 +1465,83 @@ static void test_task_vma_common(struct bpf_iter_attach_opts *opts) bpf_iter_task_vma__destroy(skel); }
+static voidtest_task_vma_dead_task(void)
Quoted 8 lines
Quoted 6 lines
+{
+ int err,iter_fd= -1;
+ structbpf_iter_task_vma*skel;
+ int wstatus,child_pid= -1;
+ time_tstart_tm,cur_tm;
+ intwait_sec= 3;Since it is new code, maybe reverse Christmas tree coding style.
Got it!
Quoted 21 lines
Quoted 21 lines
tools/testing/selftests/bpf/prog_tests/bpf_iter.c +15
-0+ + skel = bpf_iter_task_vma__open(); + if (!ASSERT_OK_PTR(skel, "bpf_iter_task_vma__open")) + return; + + skel->bss->pid = getpid(); + + err = bpf_iter_task_vma__load(skel); + if (!ASSERT_OK(err, "bpf_iter_task_vma__load")) + goto out; + + skel->links.proc_maps = bpf_program__attach_iter( + skel->progs.proc_maps, NULL); + + if (!ASSERT_OK_PTR(skel->links.proc_maps,"
bpf_program__attach_iter")) {
+ skel->links.proc_maps = NULL;
+ goto out;
+ }
+
Quoted 8 lines
Quoted 3 lines
+ start_tm = time(NULL);
+ if (start_tm< 0)
+ goto out;From the man page,
start_tmshould not fail. Note that you didn't put
an ASSERT* either. So I think you can remove it. The same for a few instances below.
The only reason that mentioned in the man page to fail is passing an invalid pointer. But, in our case, passing a NULL pointer, you are right.
Quoted 63 lines
Quoted 63 lines
+ cur_tm =
start_tm;tools/testing/selftests/bpf/prog_tests/bpf_iter.c +18
-0+ + child_pid = fork(); + if (child_pid == 0) { + /* Fork short-lived processes in the background. */ + while (cur_tm < start_tm + wait_sec) { + system("echo > /dev/null"); + cur_tm = time(NULL); + if (cur_tm < 0) + exit(1); + } + exit(0); + } + + if (!ASSERT_GE(child_pid, 0, "fork_child")) + goto out; + + while (cur_tm < start_tm + wait_sec) { + iter_fd = bpf_iter_create(bpf_link__fd(skel-Quoted 1 line
links.proc_maps));
+ if (!ASSERT_GE(iter_fd, 0, "
create_iter")) + goto out;
+
+ /* Drain all data fromiter_fd. */
+ while (cur_tm<start_tm+wait_sec) { + err =read_fd_into_buffer(iter_fd, task_vma_output, CMP_BUFFER_SIZE);
+ if (!ASSERT_GE(err, 0, "read_iter_fd")) + goto out;tools/testing/selftests/bpf/prog_tests/bpf_iter.c +21
-0+ + cur_tm = time(NULL); + if (cur_tm < 0) + goto out; + + if (err == 0) + break; + } + + close(iter_fd); + iter_fd = -1; + } + + check_bpf_link_info(skel->progs.proc_maps); + +out: + waitpid(child_pid, &wstatus, 0); + close(iter_fd); + bpf_iter_task_vma__destroy(skel); +} +void
test_bpf_sockmap_map_iter_fd(void)
{
structbpf_iter_sockmap*skel;tools/testing/selftests/bpf/prog_tests/bpf_iter.c +2
-0@@ -1586,6 +1663,8 @@ void test_bpf_iter(void) test_task_file(); if (test__start_subtest("task_vma")) test_task_vma(); + if (test__start_subtest("task_vma_dead_task")) + test_task_vma_dead_task(); if (test__start_subtest("task_btf")) test_task_btf(); if (test__start_subtest("tcp4"))