libata: fix NULL sdev dereference race in atapi_qc_complete()
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
@@ -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 ;
Quoted 21 lines · Tejun Heo
SCSI commands may be issued between
__scsi_add_device()anddev->sdevassignment, so it's unsafe forata_qc_complete()to dereferencedev->sdev->lockedwithout checking whether it's NULL or not. Fix it.Signed-off-by Tejun Heo<[email protected]>---
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?
Hello, Mark.
Quoted 11 lines · Mark Lord
Quoted 6 lines · Tejun Heo
SCSI commands may be issued between
__scsi_add_device()anddev->sdevassignment, so it's unsafe forata_qc_complete()to dereferencedev->sdev->lockedwithout checking whether it's NULL or not. Fix it.Signed-off-by Tejun Heo<[email protected]>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