Skip to contentSkip to conversation

scsi: fix crash when disconnecting usb storage

PATCH In review [email protected] 1 participant 1 message Nov 17, 2009 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 scsi: fix crash when disconnecting usb storage

From: Alexey Kuznetsov <[email protected]>

__scsi_remove_device() in scsi_forget_host() is executed out of scan_mutex and races with scsi_destroy_sdev() <- scsi_sysfs_add_devices() <- scsi_finish_async_scan(). The result is use after free and/or double free, oops.

The fix is simple, move scsi_forget_host() under scan_mutex.

scsi_forget_host() is just sequence of __scsi_remove_device(). All another calls of __scsi_remove_device() are made under scan_mutex. So that it is safe.

---

 drivers/scsi/hosts.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/hosts.c renamed from drivers/scsi/hosts.c~scsi-fix-crash-when-disconnecting-usb-storage +1 -1
diff -puN drivers/scsi/hosts.c~scsi-fix-crash-when-disconnecting-usb-storage drivers/scsi/hosts.c
@@ -164,8 +164,8 @@ void scsi_remove_host(struct Scsi_Host *
 			return;
 		}
 	spin_unlock_irqrestore(shost->host_lock, flags);
-	mutex_unlock(&shost->scan_mutex);
 	scsi_forget_host(shost);
+	mutex_unlock(&shost->scan_mutex);
 	scsi_proc_host_rm(shost);
 
 	spin_lock_irqsave(shost->host_lock, flags);

_