backport to fix a race condition/UAF in the
Subsystems (2)
cover backport to fix a race condition/UAF in the
This series backports the padata use-after-free fix and its follow-up cleanup to the 5.15.y stable branch. There is a race condition in padata_reorder() dating back to the initial padata commit.
Backport notes for 5.15.y:
- The upstream fix (commit 71203f68c774) was written against mainline which uses the 2-argument cpumask_next_wrap(cpu, mask) API introduced by dc5bb9b769c9 ("cpumask: deprecate cpumask_next_wrap()"). Since 6.1.y still has the original 4-argument API, the call in padata_reorder() is adapted to:
cpumask_next_wrap(cpu, pd->cpumask.pcpu, -1, false)
This is functionally equivalent.
- The context in padata_find_next() differs from mainline due to f954a2d37637 ("padata: switch padata_find_next() to using cpumask_next_wrap()") not being present in 6.1.y. The conflict was resolved.
After applying this series, kernel/padata.c matches the upstream file at commit 71203f68c774 with a few differences. None of these differences affect the fix, the core changes to padata_find_next(), padata_reorder(), and padata_do_serial() are identical.
Testing:
- Built and booted on x86_64 (4 CPUs, 5.15.202-yocto-standard)
- All 4 test cases passed:
* Basic parallel->serial reorder (64 jobs)
* Out-of-order parallel completion reorder (64 jobs)
* Concurrent jobs + padata_replace race (64 jobs)
* Stress test (10 iterations x 64 jobs, random delays)
- No KASAN/BUG/WARNING/UAF detected in dmesg
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
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.
[ 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 v5.15 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
@@ -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
PATCH 2/2 padata: Remove comment for reorder_work
In reply to Bin LanFrom: Herbert Xu <[email protected]>
[ Upstream commit 82a0302e7167d0b7c6cde56613db3748f8dd806d ]
Remove comment for reorder_work which no longer exists.
--- include/linux/padata.h | 1 - 1 file changed, 1 deletion(-)
include/linux/padata.h +0 -1
@@ -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