Skip to contentSkip to conversation

backport to fix a race condition/UAF in padata_reorder

PATCH New Bin Lan 1 participant 3 messages Apr 25

Subsystems (2)
lore
Share & tools
lore

Bin Lan

author
#0 lore raw

cover backport to fix a race condition/UAF in padata_reorder

After applying these patches to v6.12.y, the padata.c file matches the upstream version at commit 71203f68c774, with two exceptions:

1. cpumask_next_wrap API difference (line 310): The v6.12.y kernel still uses the old 4-argument form cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1, false), whereas upstream uses the newer 2-argument form cpumask_next_wrap(cpu, pd->cpumask.pcpu). These are functionally equivalent — upstream simply changed the API signature in a separate commit (dc5bb9b769c9), which is too invasive to backport.

2. Divide-by-zero guard in padata_mt_helper (after line 496): The v6.12.y doesn't include an extra check that forces chunk_size to at least 1 when the caller sets min_chunk to 0. This prevents a divide-by-zero panic.

Thanks
Bin Lan

Herbert Xu (2):
padata: Fix pd UAF once and for all
padata: Remove comment for reorder_work

 include/linux/padata.h |   4 --
 kernel/padata.c        | 136 +++++++++++------------------------------
 2 files changed, 37 insertions(+), 103 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 padata: Fix pd UAF once and for all

From: Herbert Xu <[email protected]>

[ Upstream commit 71203f68c7749609d7fc8ae6ad054bdedeb24f91 ]

There is a race condition/UAF in padata_reorder that goes back to the initial commit. A reference count is taken at the start of the process in padata_do_parallel, and released at the end in padata_serial_worker.

This reference count is (and only is) required for padata_replace to function correctly. If padata_replace is never called then there is no issue.

In the function padata_reorder which serves as the core of padata, as soon as padata is added to queue->serial.list, and the associated spin lock released, that padata may be processed and the reference count on pd would go away.

Fix this by getting the next padata before the squeue->serial lock is released.

In order to make this possible, simplify padata_reorder by only calling it once the next padata arrives.

Fixes 16295bec6398 ("padata: Generic parallelization/serialization interface")

[ Adjust context of padata_find_next(). Replace cpumask_next_wrap(cpu, pd->cpumask.pcpu) with cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1, false) in padata_reorder() in v6.12 according to dc5bb9b769c9 ("cpumask: deprecate cpumask_next_wrap()") and f954a2d37637 ("padata: switch padata_find_next() to using cpumask_next_wrap()") . ]

---
 include/linux/padata.h |   3 -
 kernel/padata.c        | 136 +++++++++++------------------------------
 2 files changed, 37 insertions(+), 102 deletions(-)
include/linux/padata.h +0 -3
index 0146daf34430..b486c7359de2 100644
@@ -91,7 +91,6 @@ struct padata_cpumask {
  * @cpu: Next CPU to be processed.
  * @cpumask: The cpumasks in use for parallel and serial workers.
  * @reorder_work: work struct for reordering.
- * @lock: Reorder lock.
  */
 struct parallel_data {
 	struct padata_shell		*ps;
@@ -102,8 +101,6 @@ struct parallel_data {
 	unsigned int			processed;
 	int				cpu;
 	struct padata_cpumask		cpumask;
-	struct work_struct		reorder_work;
-	spinlock_t                      ____cacheline_aligned lock;
 };
 
 /**
kernel/padata.c +37 -99

202 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/2 padata: Remove comment for reorder_work

In reply to Bin Lan

From: Herbert Xu <[email protected]>

[ Upstream commit 82a0302e7167d0b7c6cde56613db3748f8dd806d ]

Remove comment for reorder_work which no longer exists.

Fixes 71203f68c774 ("padata: Fix pd UAF once and for all")
---
 include/linux/padata.h | 1 -
 1 file changed, 1 deletion(-)
include/linux/padata.h +0 -1
index b486c7359de2..765f2778e264 100644
@@ -90,7 +90,6 @@ struct padata_cpumask {
  * @processed: Number of already processed objects.
  * @cpu: Next CPU to be processed.
  * @cpumask: The cpumasks in use for parallel and serial workers.
- * @reorder_work: work struct for reordering.
  */
 struct parallel_data {
 	struct padata_shell		*ps;
Signature · 1 line
2.43.0