There is currently a scenario where cancel_work_sync() / disable_work_sync() does not wait for the work to finish execution on an ordered workqueue (__WQ_ORDERED). This is because ordered workqueues contain multiple workqueues internally (pwqs). Consider the following scenario:
1. @work starts executing on pwq A (pool A), the oldest and only unplugged pwq in wq->pwqs. 2. Writing a new cpumask to /sys/devices/virtual/workqueue/cpumask allocates a new dfl_pwq (pwq B, backed by pool B) with pwq->plugged = true and appends it to wq->pwqs behind pwq A. 3. While @work is still executing on pool A, it is re-queued to the same ordered workqueue. Unlike normal workqueues, __queue_work() skips last_pool redirection on ordered workqueues so re-queued items do not jump ahead of work already waiting on pwq B, so @work is placed on pwq B->inactive_works and work->data is overwritten to point to pwq B. 4. cancel_work_sync() or disable_work_sync() steals @work from pwq B, and try_to_grab_pending() records pwq B->pool->id (pool B) in work->data. 5. start_flush_work() only inspects the single pool recorded in work->data (pool B, where @work has never executed), finds no worker running @work, and returns while @work is still executing on pool A. Unfortunately, this scenario can lead to UAF if a driver uses cancel_work_sync() and then frees the work item (or resources used by the work item). And this happens on ordered workqueues in practice: - drm/sched: drm_sched_wqueue_stop() calling cancel_work_sync() on work_run_job and work_free_job before scheduler teardown. - drm/xe: gt_worker_disable() calling disable_work_sync() on gt->reset.worker and gt->wedge.worker. - drm/panthor: panthor_device_suspend() calling cancel_work_sync() on ptdev->reset.work before suspending hardware. Thus, fix try_to_grab_pending() to record the pool of the oldest pwq in wq->pwqs when stealing a pending work item from an ordered workqueue, so start_flush_work() flushes pool A and waits for @work to finish. If @work is not actually executing on pool A, find_worker_executing_work() simply returns NULL. It is also needed for the soundness of the upcoming Rust ScopedWork abstraction, which binds a work item to a single workqueue and relies on cancel_work_sync() in Drop to guarantee that data borrowed by the work item is no longer accessed once drop() returns. Cc: [email protected] Fixes: 58629d4871e8 ("workqueue: Always queue work items to the newest PWQ for order workqueues") Signed-off-by: Alice Ryhl <[email protected]> --- kernel/workqueue.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index af845214e78b..d7acedfb8b27 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -2263,6 +2263,7 @@ static int try_to_grab_pending(struct work_struct *work, u32 cflags, pwq = get_work_pwq(work); if (pwq && pwq->pool == pool) { unsigned long work_data = *work_data_bits(work); + struct worker_pool *offq_pool = pool; debug_work_deactivate(work); @@ -2288,9 +2289,24 @@ static int try_to_grab_pending(struct work_struct *work, u32 cflags, /* * work->data points to pwq iff queued. Let's point to pool. As * this destroys work->data needed by the next step, stash it. + * + * For ordered workqueues, __queue_work() skips last_pool + * redirection so @pwq may be plugged while @work is still + * executing on the oldest pwq after a cpumask update. Only the + * oldest pwq is unplugged and can be executing @work, so record + * its pool instead. */ - set_work_pool_and_keep_pending(work, pool->id, - pool_offq_flags(pool)); + if (unlikely(pwq->wq->flags & __WQ_ORDERED)) { + struct pool_workqueue *oldest_pwq = + list_first_or_null_rcu(&pwq->wq->pwqs, + struct pool_workqueue, + pwqs_node); + + if (oldest_pwq) + offq_pool = oldest_pwq->pool; + } + set_work_pool_and_keep_pending(work, offq_pool->id, + pool_offq_flags(offq_pool)); /* must be the last step, see the function comment */ pwq_dec_nr_in_flight(pwq, work_data); -- 2.56.1.703.gf848ed807c-goog
