bpf: Fix UAF in task local storage
Subsystems (4)
PATCH v2 1/1 bpf: Fix UAF in task local storage
When task local storage was generalized for tracing programs, the bpf_task_local_storage callback was moved from a BPF LSM hook callback for security_task_free LSM hook to it's own callback. But a failure case in bad_fork_cleanup_security was missed which, when triggered, led to a dangling task owner pointer and a subsequent use-after-free. Move the bpf_task_storage_free to the very end of free_task to handle all failure cases.
This issue was noticed when a BPF LSM program was attached to the task_alloc hook on a kernel with KASAN enabled. The program used bpf_task_storage_get to copy the task local storage from the current task to the new task being created.
---
* v1 -> v2
Move the bpf_task_storage_free to free_task as suggested by Martin
kernel/fork.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/fork.c +1 -1
@@ -627,6 +627,7 @@ void free_task(struct task_struct *tsk)arch_release_task_struct (tsk);if (tsk->flags &PF_KTHREAD )free_kthread_struct (tsk); +bpf_task_storage_free (tsk);free_task_struct (tsk); }EXPORT_SYMBOL (free_task);
@@ -979,7 +980,6 @@ void __put_task_struct(struct task_struct *tsk)cgroup_free (tsk);task_numa_free (tsk, true);security_task_free (tsk); -bpf_task_storage_free (tsk);exit_creds (tsk);delayacct_tsk_free (tsk);put_signal_struct (tsk->signal);
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Quoted 9 lines · KP Singh
When task local storage was generalized for tracing programs, the
bpf_task_local_storagecallback was moved from a BPF LSM hook callback forsecurity_task_freeLSM hook to it's own callback. But a failure case inbad_fork_cleanup_securitywas missed which, when triggered, led to a dangling task owner pointer and a subsequent use-after-free. Move thebpf_task_storage_freeto the very end offree_taskto handle all failure cases.
Quoted 8 lines
This issue was noticed when a BPF LSM program was attached to the
task_allochook on a kernel with KASAN enabled. The program usedbpf_task_storage_getto copy the task local storage from the current task to the new task being created.Fixes a10787e6d58c ("bpf: Enable task local storage for tracing programs")Reported-by Kuba Piecuch <[email protected]>Signed-off-by KP Singh <[email protected]>
Thanks,
Song
Quoted 33 lines
---
* v1 -> v2
Move the
bpf_task_storage_freetofree_taskas suggested by Martinkernel/fork.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/fork.c +1
-2@@ -627,6 +627,7 @@ void free_task(struct task_struct *tsk)arch_release_task_struct (tsk);if (tsk->flags &PF_KTHREAD )free_kthread_struct (tsk); +bpf_task_storage_free (tsk);free_task_struct (tsk); }EXPORT_SYMBOL (free_task);@@ -979,7 +980,6 @@ void __put_task_struct(struct task_struct *tsk)cgroup_free (tsk);task_numa_free (tsk, true);security_task_free (tsk); -bpf_task_storage_free (tsk);exit_creds (tsk);delayacct_tsk_free (tsk);put_signal_struct (tsk->signal); --2.41.0.rc0.172.g3f132b7071-goog
Quoted 30 lines · KP Singh
When task local storage was generalized for tracing programs, the
bpf_task_local_storagecallback was moved from a BPF LSM hook callback forsecurity_task_freeLSM hook to it's own callback. But a failure case inbad_fork_cleanup_securitywas missed which, when triggered, led to a dangling task owner pointer and a subsequent use-after-free. Move thebpf_task_storage_freeto the very end offree_taskto handle all failure cases.This issue was noticed when a BPF LSM program was attached to the
task_allochook on a kernel with KASAN enabled. The program usedbpf_task_storage_getto copy the task local storage from the current task to the new task being created.Fixes a10787e6d58c ("bpf: Enable task local storage for tracing programs")Reported-by Kuba Piecuch <[email protected]>Signed-off-by KP Singh <[email protected]>---
* v1 -> v2
Move the
bpf_task_storage_freetofree_taskas suggested by Martinkernel/fork.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/fork.c +0
-0
kernel/fork.c void free_task(struct task_struct *tsk) · line 630
Quoted 4 lines
kernel/fork.c +1
-0@@ -627,6 +627,7 @@ void free_task(struct task_struct *tsk)arch_release_task_struct (tsk);if (tsk->flags &PF_KTHREAD )free_kthread_struct (tsk); +bpf_task_storage_free (tsk);
Applied. Thanks for the fix.
A followup question, does it need a "notrace" to be added to bpf_task_storage_free to ensure no task storage can be recreated?
Quoted 11 lines
kernel/fork.c +0
-1free_task_struct (tsk); }EXPORT_SYMBOL (free_task);@@ -979,7 +980,6 @@ void __put_task_struct(struct task_struct *tsk)cgroup_free (tsk);task_numa_free (tsk, true);security_task_free (tsk); -bpf_task_storage_free (tsk);exit_creds (tsk);delayacct_tsk_free (tsk);put_signal_struct (tsk->signal);
Quoted 39 lines · Martin KaFai Lau
Quoted 30 lines · KP Singh
When task local storage was generalized for tracing programs, the
bpf_task_local_storagecallback was moved from a BPF LSM hook callback forsecurity_task_freeLSM hook to it's own callback. But a failure case inbad_fork_cleanup_securitywas missed which, when triggered, led to a dangling task owner pointer and a subsequent use-after-free. Move thebpf_task_storage_freeto the very end offree_taskto handle all failure cases.This issue was noticed when a BPF LSM program was attached to the
task_allochook on a kernel with KASAN enabled. The program usedbpf_task_storage_getto copy the task local storage from the current task to the new task being created.Fixes a10787e6d58c ("bpf: Enable task local storage for tracing programs")Reported-by Kuba Piecuch <[email protected]>Signed-off-by KP Singh <[email protected]>---
* v1 -> v2
Move the
bpf_task_storage_freetofree_taskas suggested by Martinkernel/fork.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/fork.c +0
-0kernel/fork.c void free_task(struct task_struct *tsk) · line 630
Quoted 4 lines
kernel/fork.c +1
-0@@ -627,6 +627,7 @@ void free_task(struct task_struct *tsk)arch_release_task_struct (tsk);if (tsk->flags &PF_KTHREAD )free_kthread_struct (tsk); +bpf_task_storage_free (tsk);Applied. Thanks for the fix.
Quoted 2 lines
A followup question, does it need a "notrace" to be added to
bpf_task_storage_freeto ensure no task storage can be recreated?
Should we do a notrace or a BTF set based deny list?