[PATCH] s390/tape: fix use-after-free bugs caused by tape_dnr delayed work

Duoming Zhou posted 1 patch 3 months, 1 week ago
drivers/s390/char/tape_core.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] s390/tape: fix use-after-free bugs caused by tape_dnr delayed work
Posted by Duoming Zhou 3 months, 1 week ago
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
Re: [PATCH] s390/tape: fix use-after-free bugs caused by tape_dnr delayed work
Posted by Heiko Carstens 3 months ago
[full quote below - adding Jan]

On Mon, Nov 03, 2025 at 02:05:44PM +0800, Duoming Zhou wrote:
> 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:

Jan, could you please have a look at this?