Skip to contentSkip to conversation

libata: fix NULL sdev dereference race in atapi_qc_complete()

PATCH In review Tejun Heo 2 participants 3 messages Nov 1, 2010 – Nov 2, 2010 libata

lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

PATCH 1/1 libata: fix NULL sdev dereference race in atapi_qc_complete()

SCSI commands may be issued between __scsi_add_device() and dev->sdev assignment, so it's unsafe for ata_qc_complete() to dereference dev->sdev->locked without checking whether it's NULL or not. Fix it.

---
I actually hit this race condition. Lucky me. :-)

Thanks.

 drivers/ata/libata-scsi.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/ata/libata-scsi.c +4 -1
Index: work/drivers/ata/libata-scsi.c
===================================================================
@@ -2552,8 +2552,11 @@ static void atapi_qc_complete(struct ata
 		 *
 		 * If door lock fails, always clear sdev->locked to
 		 * avoid this infinite loop.
+		 *
+		 * This may happen before SCSI scan is complete.  Make
+		 * sure qc->dev->sdev isn't NULL before dereferencing.
 		 */
-		if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
+		if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
 			qc->dev->sdev->locked = 0;
 
 		qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
#1 lore raw
Quoted 21 lines · Tejun Heo

SCSI commands may be issued between __scsi_add_device() and dev->sdev assignment, so it's unsafe for ata_qc_complete() to dereference dev->sdev->locked without checking whether it's NULL or not. Fix it.

---
I actually hit this race condition. Lucky me. :-)

Thanks.

  drivers/ata/libata-scsi.c |    5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)

Index: work/drivers/ata/libata-scsi.c
===================================================================

drivers/ata/libata-scsi.c +0 -0
@@ -2552,8 +2552,11 @@ static void atapi_qc_complete(struct ata
  		 *
  		 * If door lock fails, always clear sdev->locked to

drivers/ata/libata-scsi.c static void atapi_qc_complete(struct ata · line 2562

Quoted 10 lines
drivers/ata/libata-scsi.c +4 -1
  		 * avoid this infinite loop.
+		 *
+		 * This may happen before SCSI scan is complete.  Make
+		 * sure qc->dev->sdev isn't NULL before dereferencing.
  		 */
-		if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
+		if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL&&  qc->dev->sdev)
  			qc->dev->sdev->locked = 0;
 
  		qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;

Mmmm.. for some reason, this just screams "band-aid" to me, and makes me worry deeply about the underlaying race condition it all suggests..

James?

author
#2 lore raw

Hello, Mark.

Quoted 11 lines · Mark Lord
Quoted 6 lines · Tejun Heo

SCSI commands may be issued between __scsi_add_device() and dev->sdev assignment, so it's unsafe for ata_qc_complete() to dereference dev->sdev->locked without checking whether it's NULL or not. Fix it.

Mmmm.. for some reason, this just screams "band-aid" to me, and makes me worry deeply about the underlaying race condition it all suggests..

Yeah, the coupling between sdev and ata_dev may look somewhat band-aidy but AFAICS all others are explicitly checking whether dev->sdev is set. It's somewhat inevitable given the current probing sequence (ATA dev comes up first and then tells SCSI to probe itself).

Thanks.

Signature · 1 line
tejun