public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] s390/tape: fix use-after-free bugs caused by tape_dnr delayed work
@ 2025-11-03  6:05 Duoming Zhou
  2025-11-03 11:19 ` Heiko Carstens
  0 siblings, 1 reply; 2+ messages in thread
From: Duoming Zhou @ 2025-11-03  6:05 UTC (permalink / raw)
  To: linux-s390
  Cc: linux-kernel, hca, gor, agordeev, borntraeger, svens, mingo, tglx,
	Duoming Zhou

The delayed work tape_dnr is initialized in tape_alloc_device(), which
is called from tape_generic_probe(), and is scheduled in the following
scenarios:

1. Starting an I/O operation fails with -EBUSY in __tape_start_io().
2. Canceling an I/O operation fails with -EBUSY in __tape_cancel_io().
3. A deferred error condition is detected in __tape_do_irq().

When the tape device is detached via tape_generic_remove(), the
tape_device structure might be deallocated after the final call to
tape_put_device(). However, if the delayed work tape_dnr is still
pending or executing at the time of detachment, it could lead to
use-after-free bugs when the work function tape_delayed_next_request()
accesses the already freed tape_device memory.

The race condition can occur as follows:

CPU 0(detach thread)      | CPU 1 (delayed work)
tape_generic_remove()     |
  tape_put_device(device) | tape_delayed_next_request
                          |   device = container_of(...) // USE
                          |   device-> // USE

Add disable_delayed_work_sync() in tape_generic_remove() to guarantee
proper cancellation of the delayed work item before tape_device is
deallocated.

This bug is identified by static analysis.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
---
 drivers/s390/char/tape_core.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/s390/char/tape_core.c b/drivers/s390/char/tape_core.c
index 6ec812280221..722dc4737a87 100644
--- a/drivers/s390/char/tape_core.c
+++ b/drivers/s390/char/tape_core.c
@@ -625,6 +625,7 @@ tape_generic_remove(struct ccw_device *cdev)
 	}
 	DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev);
 
+	disable_delayed_work_sync(&device->tape_dnr);
 	spin_lock_irq(get_ccwdev_lock(device->cdev));
 	switch (device->tape_state) {
 		case TS_INIT:
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-11-03 11:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-03  6:05 [PATCH] s390/tape: fix use-after-free bugs caused by tape_dnr delayed work Duoming Zhou
2025-11-03 11:19 ` Heiko Carstens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox