Skip to contentSkip to conversation

workqueue: unlink pwqs from wq->pwqs list in alloc_and_link_pwqs() error path

PATCH Applied Breno Leitao 2 participants 3 messages Mar 23 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 workqueue: unlink pwqs from wq->pwqs list in alloc_and_link_pwqs() error path

When alloc_and_link_pwqs() fails partway through the per-cpu allocation loop, some pool_workqueues may have already been linked into wq->pwqs via link_pwq(). The error path frees these pwqs with kmem_cache_free() but never removes them from the wq->pwqs list, leaving dangling pointers in the list.

Currently this is not exploitable because the workqueue was never added to the global workqueues list and the caller frees the wq immediately after. However, this makes sure that alloc_and_link_pwqs() doesn't leave any half baked structure, which may have side effect is not properly used.

Fix this by unlinking each pwq from wq->pwqs before freeing it. No locking is needed as the workqueue has not been published yet, thus no concurrency is possible.

---
 kernel/workqueue.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/workqueue.c +9 -1
index b77119d71641a..70581fa7b4e24 100644
@@ -5623,8 +5623,16 @@ static int alloc_and_link_pwqs(struct workqueue_struct *wq)
 		for_each_possible_cpu(cpu) {
 			struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu);
 
-			if (pwq)
+			if (pwq) {
+				/*
+				 * Unlink pwq from wq->pwqs since link_pwq()
+				 * may have already added it. wq->mutex is not
+				 * needed as the wq has not been published yet.
+				 */
+				if (!list_empty(&pwq->pwqs_node))
+					list_del_rcu(&pwq->pwqs_node);
 				kmem_cache_free(pwq_cache, pwq);
+			}
 		}
 		free_percpu(wq->cpu_pwq);
 		wq->cpu_pwq = NULL;

---
base-commit: c369299895a591d96745d6492d4888259b004a9e
change-id: 20260323-workqueue_link-d1503237f242

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
maintainer Applied
#1 lore raw

Applied to wq/for-7.1 with a minor commit message edit:

"which may have side effect is not properly used" -> "which may have side effects if not properly cleaned up"

Thanks.

--
tejun

#2 lore raw
Quoted 4 lines · Tejun Heo

Applied to wq/for-7.1 with a minor commit message edit:

"which may have side effect is not properly used" -> "which may have side effects if not properly cleaned up"

ack. Thanks for fixing it.

--breno