Skip to contentSkip to conversation

workqueue: Fix NULL pointer dereference

PATCH Applied Li Bin 3 participants 5 messages Oct 24, 2017 – Oct 27, 2017 Workqueue

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Li Bin

author
#0 lore raw

PATCH 1/1 workqueue: Fix NULL pointer dereference

When queue_work() is used in irq handler, there is a potential case that trigger NULL pointer dereference.
----------------------------------------------------------------
worker_thread()
|-spin_lock_irq()
|-process_one_work()

	|-worker->current_pwq = pwq
	|-spin_unlock_irq()
	|-worker->current_func(work)
	|-spin_lock_irq()

|-worker->current_pwq = NULL
|-spin_unlock_irq()

				//interrupt here
				|-irq_handler
					|-__queue_work()
						//assuming that the wq is draining
						|-is_chained_work(wq)
							|-current_wq_worker()
							//Here, 'current' is the interrupted worker!
								|-current->current_pwq is NULL here!

|-schedule()
----------------------------------------------------------------

Avoid it by checking for irq context in current_wq_worker(), and if in irq context, we shouldn't use the 'current' to check the condition.

---
 kernel/workqueue_internal.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/workqueue_internal.h +2 -1
index 8635417..d81cb9b 100644
@@ -9,6 +9,7 @@
 
 #include <linux/workqueue.h>
 #include <linux/kthread.h>
+#include <linux/preempt.h>
 
 struct worker_pool;
 
@@ -59,7 +60,7 @@ struct worker {
  */
 static inline struct worker *current_wq_worker(void)
 {
-	if (current->flags & PF_WQ_WORKER)
+	if (!in_irq() && (current->flags & PF_WQ_WORKER))
 		return kthread_data(current);
 	return NULL;
 }
Signature · 1 line
1.7.12.4
maintainer Applied
#1 lore raw
Quoted 24 lines · Li Bin

When queue_work() is used in irq handler, there is a potential case that trigger NULL pointer dereference.
----------------------------------------------------------------
worker_thread()
|-spin_lock_irq()
|-process_one_work()

	|-worker->current_pwq = pwq
	|-spin_unlock_irq()
	|-worker->current_func(work)
	|-spin_lock_irq()

|-worker->current_pwq = NULL
|-spin_unlock_irq()

				//interrupt here
				|-irq_handler
					|-__queue_work()
						//assuming that the wq is draining
						|-is_chained_work(wq)
							|-current_wq_worker()
							//Here, 'current' is the interrupted worker!
								|-current->current_pwq is NULL here!

|-schedule()
----------------------------------------------------------------

Quoted 6 lines

Avoid it by checking for irq context in current_wq_worker(), and if in irq context, we shouldn't use the 'current' to check the condition.

Applied to wq/for-4.14-fixes.

Thanks.

Signature · 1 line
tejun
reviewer
#2 lore raw
In reply to Li Bin
Quoted 13 lines · Li Bin

When queue_work() is used in irq handler, there is a potential case that trigger NULL pointer dereference.
----------------------------------------------------------------
worker_thread()
|-spin_lock_irq()
|-process_one_work()

        |-worker->current_pwq = pwq
        |-spin_unlock_irq()
        |-worker->current_func(work)
        |-spin_lock_irq()
        |-worker->current_pwq = NULL

|-spin_unlock_irq()

Quoted 8 lines
                                //interrupt here
                                |-irq_handler
                                        |-__queue_work()
                                                //assuming that the wq is draining
                                                |-is_chained_work(wq)
                                                        |-current_wq_worker()
                                                        //Here, 'current' is the interrupted worker!
                                                                |-current->current_pwq is NULL here!

I remember that softirq can be invoked when irq_eixt(), and in this case the current->current_pwq is also NULL if __queue_work() is called in the soft irq.

So in_task() might be better than !in_irq() for the fix?

Quoted 37 lines

|-schedule()
----------------------------------------------------------------

Avoid it by checking for irq context in current_wq_worker(), and if in irq context, we shouldn't use the 'current' to check the condition.

---
 kernel/workqueue_internal.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/workqueue_internal.h +2 -2
index 8635417..d81cb9b 100644
@@ -9,6 +9,7 @@
 
 #include <linux/workqueue.h>
 #include <linux/kthread.h>
+#include <linux/preempt.h>
 
 struct worker_pool;
 
@@ -59,7 +60,7 @@ struct worker {
  */
 static inline struct worker *current_wq_worker(void)
 {
-       if (current->flags & PF_WQ_WORKER)
+       if (!in_irq() && (current->flags & PF_WQ_WORKER))
                return kthread_data(current);
        return NULL;
 }
--

1.7.12.4

maintainer
#3 lore raw

Hello,

Quoted 5 lines · Lai Jiangshan

I remember that softirq can be invoked when irq_eixt(), and in this case the current->current_pwq is also NULL if __queue_work() is called in the soft irq.

So in_task() might be better than !in_irq() for the fix?

Reverted the patch for now. Li, what do you think?

Thanks.

Signature · 1 line
tejun
author
#4 lore raw
In reply to Lai Jiangshan

Hi, Jiangshan

Quoted 9 lines · Lai Jiangshan

On Tue, Oct 24, 2017 at 9:18 AM, Li Bin <[email protected]> wrote:

I remember that softirq can be invoked when irq_eixt(), and in this case the current->current_pwq is also NULL if __queue_work() is called in the soft irq.

So in_task() might be better than !in_irq() for the fix?

Good catch, I will fix it and resend the patch.

Thanks,
Li Bin

Quoted 40 lines
Quoted 35 lines

|-schedule()
----------------------------------------------------------------

Avoid it by checking for irq context in current_wq_worker(), and if in irq context, we shouldn't use the 'current' to check the condition.

---
 kernel/workqueue_internal.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/workqueue_internal.h +2 -2
index 8635417..d81cb9b 100644
@@ -9,6 +9,7 @@
 
 #include <linux/workqueue.h>
 #include <linux/kthread.h>
+#include <linux/preempt.h>
 
 struct worker_pool;
 
@@ -59,7 +60,7 @@ struct worker {
  */
 static inline struct worker *current_wq_worker(void)
 {
-       if (current->flags & PF_WQ_WORKER)
+       if (!in_irq() && (current->flags & PF_WQ_WORKER))
                return kthread_data(current);
        return NULL;
 }
--
Quoted 2 lines

1.7.12.4

.