[Devel] [PATCH hci-8.2 v8 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement
Alexander Ivanov
alexander.ivanov at virtuozzo.com
Wed Nov 18 16:29:57 MSK 2026
>> diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
>> index 799b9d61dcdb..3b5f37a58bb1 100644
>> --- a/drivers/md/dm-ploop-target.c
>> +++ b/drivers/md/dm-ploop-target.c
>> @@ -194,6 +194,13 @@ static void ploop_destroy(struct ploop *ploop)
>> if (ploop->kt_allocator)
>> kthread_stop(ploop->kt_allocator->task);
>>
>> + /* BAT writes may still be completing from aio context */
>> + wait_event(ploop->inflight_md_wq,
>> + !atomic_read(&ploop->inflight_md));
>> + /* Wait for ploop_dec_inflight_md() to drop the lock */
>> + spin_lock_irq(&ploop->inflight_md_wq.lock);
>> + spin_unlock_irq(&ploop->inflight_md_wq.lock);
>> +
> [Severity: Medium]
> Is this wait too late? It runs after kthread_stop() has already stopped and released the runners, kt_worker and kt_allocator. A BAT write that is still in flight can complete during this wait_event(). ploop_put_piwb() then calls ploop_bat_write_finish(), which reaches ploop_dispatch_pios() or ploop_complete_cow() and ends up in ploop_schedule_work(). That calls wake_up_process(ploop->kt_worker->task) on a task that has already exited, so it can be a use-after-free of the task_struct. Any pios queued this way are never processed, because the threads are gone.
>
> This is reachable when ploop_postsuspend() skipped its own inflight_md wait because the device is in standby (the !ploop_is_standby() check), and dtr then runs with BAT writes still in flight. The patch protects the lifetime of the ploop, the pio_pool and the waitqueue, but not that of the stopped kthreads.
>
> Would it be better to drain inflight_md before the kthread_stop() calls? The worker would still have to be able to process the pios dispatched by those completions.
This race is older than the patch and is not specific to BAT writes
(ploop_complete_cow() has it too), so it is out of scope here.
More information about the Devel
mailing list