Skip to contentSkip to conversation

scsi: libsas: Fix use-after-free in sas_eh_handle_resets()

PATCH In review Wentao Liang 3 participants 4 messages Sep 17 – Nov 7

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: libsas: Fix use-after-free in sas_eh_handle_resets()

sas_eh_handle_resets() drops the reference it took on the domain device with sas_put_device() and then accesses dev->state again while holding ha->lock. The put may release the last reference, so the clear_bit() can touch freed memory.

Clear the pending flag first and drop the reference last.

Fixes 5db45bdc87ce ("[SCSI] libsas: enforce eh strategy handlers only in eh context")
---
 drivers/scsi/libsas/sas_scsi_host.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/scsi/libsas/sas_scsi_host.c +6 -1
index c83282733ec4..14ca19c93c32 100644
@@ -680,10 +680,15 @@ static void sas_eh_handle_resets(struct Scsi_Host *shost)
 		if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
 			i->dft->lldd_I_T_nexus_reset(dev);
 
-		sas_put_device(dev);
 		spin_lock_irq(&ha->lock);
 		clear_bit(SAS_DEV_EH_PENDING, &dev->state);
 		ha->eh_active--;
+		spin_unlock_irq(&ha->lock);
+
+		/* dev->state is no longer used, this may be the last reference */
+		sas_put_device(dev);
+
+		spin_lock_irq(&ha->lock);
 	}
 	spin_unlock_irq(&ha->lock);
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 25 lines · Wentao Liang

sas_eh_handle_resets() drops the reference it took on the domain device with sas_put_device() and then accesses dev->state again while holding ha->lock. The put may release the last reference, so the clear_bit() can touch freed memory.

Clear the pending flag first and drop the reference last.

Fixes 5db45bdc87ce ("[SCSI] libsas: enforce eh strategy handlers only in eh context")
---
  drivers/scsi/libsas/sas_scsi_host.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/scsi/libsas/sas_scsi_host.c +0 -1
index c83282733ec4..14ca19c93c32 100644
@@ -680,10 +680,15 @@ static void sas_eh_handle_resets(struct Scsi_Host *shost)
  		if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
  			i->dft->lldd_I_T_nexus_reset(dev);
 
-		sas_put_device(dev);
  		spin_lock_irq(&ha->lock);
  		clear_bit(SAS_DEV_EH_PENDING, &dev->state);

drivers/scsi/libsas/sas_scsi_host.c static void sas_eh_handle_resets(struct Scsi_Host *shost) · line 694

Quoted 10 lines
drivers/scsi/libsas/sas_scsi_host.c +6 -0
  		ha->eh_active--;
+		spin_unlock_irq(&ha->lock);
+
+		/* dev->state is no longer used, this may be the last reference */
+		sas_put_device(dev);
+
+		spin_lock_irq(&ha->lock);
  	}
  	spin_unlock_irq(&ha->lock);
  }

I think that the following is a better change:

----8<-----

drivers/scsi/libsas/sas_scsi_host.c +1 -1
@@ -695,9 +695,9 @@ static void sas_eh_handle_resets(struct Scsi_Host *shost)
                if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
                        i->dft->lldd_I_T_nexus_reset(dev);
 
+               clear_bit(SAS_DEV_EH_PENDING, &dev->state);
                sas_put_device(dev);
                spin_lock_irq(&ha->lock);
-               clear_bit(SAS_DEV_EH_PENDING, &dev->state);
                ha->eh_active--;
        }
        spin_unlock_irq(&ha->lock);

---->8-----

#2 lore raw

PATCH v2 1/1 scsi: libsas: Fix use-after-free in sas_eh_handle_resets()

sas_eh_handle_resets() drops the reference it took on the domain device with sas_put_device() and then accesses dev->state again while holding ha->lock. The put may release the last reference, so the clear_bit() can touch freed memory.

Clear the pending flag before dropping the reference.

The candidate was found with static analysis tools; DeepSeek and GPT-6.1 Sol were used to audit it and to help prepare this fix.

Fixes 5db45bdc87ce ("[SCSI] libsas: enforce eh strategy handlers only in eh context")

---
Changes in v2:
- Adopt John's suggestion: clear SAS_DEV_EH_PENDING before sas_put_device(), so the locked section is back to just ha->eh_active--. Sorry for the late reply.
- Rebuilt on v7.4-rc1 with W=1; drivers/scsi/libsas/ builds cleanly.
- Link to v1: https://lore.kernel.org/all/[email protected]/

 drivers/scsi/libsas/sas_scsi_host.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/scsi/libsas/sas_scsi_host.c +1 -1
index 76517c8f0684..448f35de67fb 100644
@@ -695,9 +695,9 @@ static void sas_eh_handle_resets(struct Scsi_Host *shost)
 		if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
 			i->dft->lldd_I_T_nexus_reset(dev);
 
+		clear_bit(SAS_DEV_EH_PENDING, &dev->state);
 		sas_put_device(dev);
 		spin_lock_irq(&ha->lock);
-		clear_bit(SAS_DEV_EH_PENDING, &dev->state);
 		ha->eh_active--;
 	}
 	spin_unlock_irq(&ha->lock);
maintainer
#3 lore raw
Quoted 7 lines · Wentao Liang

sas_eh_handle_resets() drops the reference it took on the domain device with sas_put_device() and then accesses dev->state again while holding ha->lock. The put may release the last reference, so the clear_bit() can touch freed memory.

Clear the pending flag before dropping the reference.

Quoted 7 lines

The candidate was found with static analysis tools; DeepSeek and GPT- 6.1 Sol were used to audit it and to help prepare this fix.

Fixes 5db45bdc87ce ("[SCSI] libsas: enforce eh strategy handlers

only in eh context")

You just adopted the patch John sent wholesale, so the signoff above is now wrong and you either need his signoff or at least a Co-developed- by.

I get that this change is likely too small to have copyright implications, but you want to make sure your process works in cases where it does matter.

Regards,

James