Skip to contentSkip to conversation

scsi: fix use-after-free in scsi_init_io()

PATCH In review Tejun Heo 2 participants 3 messages Aug 16, 2010 SCSI

lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

PATCH 1/1 scsi: fix use-after-free in scsi_init_io()

scsi_init_io() dereferences scsi_cmnd after putting it in the error path leading to oops. Fix it.

---
 drivers/scsi/scsi_lib.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/scsi/scsi_lib.c +3 -1
Index: block/drivers/scsi/scsi_lib.c
===================================================================
@@ -968,7 +968,9 @@ static int scsi_init_sgtable(struct requ
  */
 int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
 {
+	struct request *req = cmd->request;
 	int error = scsi_init_sgtable(cmd->request, &cmd->sdb, gfp_mask);
+
 	if (error)
 		goto err_exit;
 
@@ -1012,7 +1014,7 @@ int scsi_init_io(struct scsi_cmnd *cmd,
 err_exit:
 	scsi_release_buffers(cmd);
 	scsi_put_command(cmd);
-	cmd->request->special = NULL;
+	req->special = NULL;
 	return error;
 }
 EXPORT_SYMBOL(scsi_init_io);
#1 lore raw
Quoted 24 lines · Tejun Heo

scsi_init_io() dereferences scsi_cmnd after putting it in the error path leading to oops. Fix it.

---
 drivers/scsi/scsi_lib.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Index: block/drivers/scsi/scsi_lib.c
===================================================================

drivers/scsi/scsi_lib.c +2 -0
@@ -968,7 +968,9 @@ static int scsi_init_sgtable(struct requ
  */
 int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
 {
+	struct request *req = cmd->request;
 	int error = scsi_init_sgtable(cmd->request, &cmd->sdb, gfp_mask);
+
 	if (error)
 		goto err_exit;
 

drivers/scsi/scsi_lib.c int scsi_init_io(struct scsi_cmnd *cmd, · line 1017

Quoted 5 lines
drivers/scsi/scsi_lib.c +1 -1
@@ -1012,7 +1014,7 @@ int scsi_init_io(struct scsi_cmnd *cmd,
 err_exit:
 	scsi_release_buffers(cmd);
 	scsi_put_command(cmd);
-	cmd->request->special = NULL;
+	req->special = NULL;

Thanks for the bug report. The fix is a bit heavy handed; what about this?

James

---

drivers/scsi/scsi_lib.c +1 -1
index 9ade720..ee02d38 100644
@@ -1011,8 +1011,8 @@ int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
 
 err_exit:
 	scsi_release_buffers(cmd);
-	scsi_put_command(cmd);
 	cmd->request->special = NULL;
+	scsi_put_command(cmd);
 	return error;
 }
 EXPORT_SYMBOL(scsi_init_io);
author
#2 lore raw

Hello,

Quoted 2 lines · James Bottomley

Thanks for the bug report. The fix is a bit heavy handed; what about this?

Yeap, sure. Looks good to me.

Signature · 1 line
tejun