Skip to contentSkip to conversation

task_work: kasan: record task_work_add() call stack

PATCH Applied Walter Wu 5 participants 5 messages Mar 16, 2021 – Mar 18, 2021

Subsystems (3)
lore
Share & tools
lore

Walter Wu

author
#0 lore raw

PATCH v2 1/1 task_work: kasan: record task_work_add() call stack

Why record task_work_add() call stack?
Syzbot reports many use-after-free issues for task_work, see [1]. After see the free stack and the current auxiliary stack, we think they are useless, we don't know where register the work, this work may be the free call stack, so that we miss the root cause and don't solve the use-after-free.

Add task_work_add() call stack into KASAN auxiliary stack in order to improve KASAN report. It is useful for programmers to solve use-after-free issues.

[1]: https://groups.google.com/g/syzkaller-bugs/search?q=kasan%20use-after-free%20task_work_run

Cc Andrey Konovalov <[email protected]>
Cc Andrey Ryabinin <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Matthias Brugger <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Oleg Nesterov <[email protected]>

---

v2: Fix kasan_record_aux_stack() calling sequence issue.
Thanks for Dmitry's suggestion

---
 kernel/task_work.c | 3 +++
 mm/kasan/kasan.h   | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
kernel/task_work.c +3 -0
index 9cde961875c0..3d4852891fa8 100644
@@ -34,6 +34,9 @@ int task_work_add(struct task_struct *task, struct callback_head *work,
 {
 	struct callback_head *head;
 
+	/* record the work call stack in order to print it in KASAN reports */
+	kasan_record_aux_stack(work);
+
 	do {
 		head = READ_ONCE(task->task_works);
 		if (unlikely(head == &work_exited))
mm/kasan/kasan.h +1 -1
index 3436c6bf7c0c..e4629a971a3c 100644
@@ -146,7 +146,7 @@ struct kasan_alloc_meta {
 	struct kasan_track alloc_track;
 #ifdef CONFIG_KASAN_GENERIC
 	/*
-	 * call_rcu() call stack is stored into struct kasan_alloc_meta.
+	 * The auxiliary stack is stored into struct kasan_alloc_meta.
 	 * The free stack is stored into struct kasan_free_meta.
 	 */
 	depot_stack_handle_t aux_stack[2];
Signature · 1 line
2.18.0
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 24 lines · Walter Wu

Why record task_work_add() call stack?
Syzbot reports many use-after-free issues for task_work, see [1]. After see the free stack and the current auxiliary stack, we think they are useless, we don't know where register the work, this work may be the free call stack, so that we miss the root cause and don't solve the use-after-free.

Add task_work_add() call stack into KASAN auxiliary stack in order to improve KASAN report. It is useful for programmers to solve use-after-free issues.

[1]: https://groups.google.com/g/syzkaller-bugs/search?q=kasan%20use-after-free%20task_work_run

Cc Andrey Konovalov <[email protected]>
Cc Andrey Ryabinin <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Matthias Brugger <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Oleg Nesterov <[email protected]>
Quoted 4 lines

---

v2: Fix kasan_record_aux_stack() calling sequence issue.
Thanks for Dmitry's suggestion

Quoted 39 lines
---
 kernel/task_work.c | 3 +++
 mm/kasan/kasan.h   | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
kernel/task_work.c +3 -0
index 9cde961875c0..3d4852891fa8 100644
@@ -34,6 +34,9 @@ int task_work_add(struct task_struct *task, struct callback_head *work,
 {
        struct callback_head *head;
 
+       /* record the work call stack in order to print it in KASAN reports */
+       kasan_record_aux_stack(work);
+
        do {
                head = READ_ONCE(task->task_works);
                if (unlikely(head == &work_exited))
mm/kasan/kasan.h +1 -2
index 3436c6bf7c0c..e4629a971a3c 100644
@@ -146,7 +146,7 @@ struct kasan_alloc_meta {
        struct kasan_track alloc_track;
 #ifdef CONFIG_KASAN_GENERIC
        /*
-        * call_rcu() call stack is stored into struct kasan_alloc_meta.
+        * The auxiliary stack is stored into struct kasan_alloc_meta.
         * The free stack is stored into struct kasan_free_meta.
         */
        depot_stack_handle_t aux_stack[2];
--

2.18.0

--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20210316024410.19967-1-walter-zh.wu%40mediatek.com.

Reviewed-by on v2 1/1
#2 lore raw
In reply to Walter Wu
Quoted 10 lines · Walter Wu

Why record task_work_add() call stack?
Syzbot reports many use-after-free issues for task_work, see [1]. After see the free stack and the current auxiliary stack, we think they are useless, we don't know where register the work, this work may be the free call stack, so that we miss the root cause and don't solve the use-after-free.

Add task_work_add() call stack into KASAN auxiliary stack in order to improve KASAN report. It is useful for programmers to solve use-after-free issues.

I think this is a very useful addition, especially as task_work proliferates.

Signature · 1 line
Jens Axboe
Acked-by on v2 1/1
#3 lore raw
In reply to Walter Wu

kernel/task_work.c int task_work_add(struct task_struct *task, struct callback_head *work, · line 42

Quoted 13 lines · Walter Wu
kernel/task_work.c +3 -0
@@ -34,6 +34,9 @@ int task_work_add(struct task_struct *task, struct callback_head *work,
 {
 	struct callback_head *head;
 
+	/* record the work call stack in order to print it in KASAN reports */
+	kasan_record_aux_stack(work);
+
 	do {
 		head = READ_ONCE(task->task_works);
 		if (unlikely(head == &work_exited))
Acked-by on v2 1/1
#4 lore raw
In reply to Walter Wu
Quoted 53 lines · Walter Wu

Why record task_work_add() call stack?
Syzbot reports many use-after-free issues for task_work, see [1]. After see the free stack and the current auxiliary stack, we think they are useless, we don't know where register the work, this work may be the free call stack, so that we miss the root cause and don't solve the use-after-free.

Add task_work_add() call stack into KASAN auxiliary stack in order to improve KASAN report. It is useful for programmers to solve use-after-free issues.

[1]: https://groups.google.com/g/syzkaller-bugs/search?q=kasan%20use-after-free%20task_work_run

Cc Andrey Konovalov <[email protected]>
Cc Andrey Ryabinin <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Alexander Potapenko <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Matthias Brugger <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Oleg Nesterov <[email protected]>

---

v2: Fix kasan_record_aux_stack() calling sequence issue.
Thanks for Dmitry's suggestion

---
 kernel/task_work.c | 3 +++
 mm/kasan/kasan.h   | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
kernel/task_work.c +3 -0
index 9cde961875c0..3d4852891fa8 100644
@@ -34,6 +34,9 @@ int task_work_add(struct task_struct *task, struct callback_head *work,
 {
        struct callback_head *head;
 
+       /* record the work call stack in order to print it in KASAN reports */
+       kasan_record_aux_stack(work);
+
        do {
                head = READ_ONCE(task->task_works);
                if (unlikely(head == &work_exited))
mm/kasan/kasan.h +0 -0
index 3436c6bf7c0c..e4629a971a3c 100644

mm/kasan/kasan.h struct kasan_alloc_meta {

Quoted 9 lines
mm/kasan/kasan.h +1 -2
@@ -146,7 +146,7 @@ struct kasan_alloc_meta {
        struct kasan_track alloc_track;
 #ifdef CONFIG_KASAN_GENERIC
        /*
-        * call_rcu() call stack is stored into struct kasan_alloc_meta.
+        * The auxiliary stack is stored into struct kasan_alloc_meta.
         * The free stack is stored into struct kasan_free_meta.
         */
        depot_stack_handle_t aux_stack[2];
--