Skip to contentSkip to conversation

workqueue: Add a new flag to spot the potential UAF error

PATCH Applied Richard Clark 3 participants 4 messages Dec 13, 2022 – Jan 4, 2023 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 workqueue: Add a new flag to spot the potential UAF error

Currently if the user queues a new work item unintentionally into a wq after the destroy_workqueue(wq), the work still can be queued and scheduled without any noticeable kernel message before the end of a RCU grace period.

As a debug-aid facility, this commit adds a new flag __WQ_DESTROYING to spot that issue by triggering a kernel WARN message.

---
 include/linux/workqueue.h |  1 +
 kernel/workqueue.c        | 15 ++++++++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)
include/linux/workqueue.h +1 -0
index a0143dd24430..ac551b8ee7d9 100644
@@ -335,6 +335,7 @@ enum {
 	 */
 	WQ_POWER_EFFICIENT	= 1 << 7,
 
+	__WQ_DESTROYING		= 1 << 15, /* internal: workqueue is destroying */
 	__WQ_DRAINING		= 1 << 16, /* internal: workqueue is draining */
 	__WQ_ORDERED		= 1 << 17, /* internal: workqueue is ordered */
 	__WQ_LEGACY		= 1 << 18, /* internal: create*_workqueue() */
kernel/workqueue.c +12 -3
index 39060a5d0905..30dc6869b3fd 100644
@@ -1433,9 +1433,13 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
 	lockdep_assert_irqs_disabled();
 
 
-	/* if draining, only works from the same workqueue are allowed */
-	if (unlikely(wq->flags & __WQ_DRAINING) &&
-	    WARN_ON_ONCE(!is_chained_work(wq)))
+	/*
+	 * For a draining wq, only works from the same workqueue are
+	 * allowed. The __WQ_DESTROYING helps to spot the issue that
+	 * queues a new work item to a wq after destroy_workqueue(wq).
+	 */
+	if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING)
+		&& WARN_ON_ONCE(!is_chained_work(wq))))
 		return;
 	rcu_read_lock();
 retry:
@@ -4414,6 +4418,11 @@ void destroy_workqueue(struct workqueue_struct *wq)
 	 */
 	workqueue_sysfs_unregister(wq);
 
+	/* mark the workqueue destruction is in progress */
+	mutex_lock(&wq->mutex);
+	wq->flags |= __WQ_DESTROYING;
+	mutex_unlock(&wq->mutex);
+
 	/* drain it before proceeding with destruction */
 	drain_workqueue(wq);
 
Signature · 1 line
2.37.2
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Richard Clark

Currently if the user queues a new work item unintentionally into a wq after the destroy_workqueue(wq), the work still can be queued and scheduled without any noticeable kernel message before the end of a RCU grace period.

As a debug-aid facility, this commit adds a new flag __WQ_DESTROYING to spot that issue by triggering a kernel WARN message.

#2 lore raw

Hello TJ,

Quoted 9 lines · Lai Jiangshan
Quoted 6 lines · Richard Clark

Currently if the user queues a new work item unintentionally into a wq after the destroy_workqueue(wq), the work still can be queued and scheduled without any noticeable kernel message before the end of a RCU grace period.

Quoted 7 lines
Quoted 5 lines

As a debug-aid facility, this commit adds a new flag __WQ_DESTROYING to spot that issue by triggering a kernel WARN message.

What do I need to do for this patch next?

Thanks

maintainer Applied
#3 lore raw
In reply to Richard Clark
Quoted 10 lines · Richard Clark

Currently if the user queues a new work item unintentionally into a wq after the destroy_workqueue(wq), the work still can be queued and scheduled without any noticeable kernel message before the end of a RCU grace period.

As a debug-aid facility, this commit adds a new flag __WQ_DESTROYING to spot that issue by triggering a kernel WARN message.

Applied to wq/for-6.3 w/ whitespace adjustments.

Thanks.

Signature · 1 line
tejun