Skip to contentSkip to conversation

scsi: target: tcmu: Fix a use after free in tcmu_check_expired_queue_cmd()

PATCH Applied Dan Carpenter 5 participants 6 messages May 23, 2020 – May 27, 2020

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

PATCH 1/1 scsi: target: tcmu: Fix a use after free in tcmu_check_expired_queue_cmd()

The pr_debug() dereferences "cmd" after we already freed it by calling tcmu_free_cmd(cmd). The debug printk needs to be done earlier.

Fixes 61fb24822166 ("scsi: target: tcmu: Userspace must not complete queued commands")
---
 drivers/target/target_core_user.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/target/target_core_user.c +3 -3
index 904d8a8373f2..28fb9441de7a 100644
@@ -1292,13 +1292,13 @@ static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd)
 	if (!time_after(jiffies, cmd->deadline))
 		return;
 
+	pr_debug("Timing out queued cmd %p on dev %s.\n",
+		  cmd, cmd->tcmu_dev->name);
+
 	list_del_init(&cmd->queue_entry);
 	se_cmd = cmd->se_cmd;
 	tcmu_free_cmd(cmd);
 
-	pr_debug("Timing out queued cmd %p on dev %s.\n",
-		  cmd, cmd->tcmu_dev->name);
-
 	target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL);
 }
 
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Dan Carpenter

The pr_debug() dereferences "cmd" after we already freed it by calling tcmu_free_cmd(cmd). The debug printk needs to be done earlier.

Fixes 61fb24822166 ("scsi: target: tcmu: Userspace must not complete queued commands")

Looks like this could be squashed in with the change that it fixes, given that 5.8/scsi-queue hasn't gone out yet.

Cheers, David

Reviewed-by on v1 1/1
#2 lore raw
In reply to Dan Carpenter
Quoted 20 lines · Dan Carpenter

The pr_debug() dereferences "cmd" after we already freed it by calling tcmu_free_cmd(cmd). The debug printk needs to be done earlier.

Fixes 61fb24822166 ("scsi: target: tcmu: Userspace must not complete queued commands")
---
 drivers/target/target_core_user.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/target/target_core_user.c +3 -0
index 904d8a8373f2..28fb9441de7a 100644
@@ -1292,13 +1292,13 @@ static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd)
 	if (!time_after(jiffies, cmd->deadline))
 		return;
 
+	pr_debug("Timing out queued cmd %p on dev %s.\n",
+		  cmd, cmd->tcmu_dev->name);
+

drivers/target/target_core_user.c static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd) · line 1304

Quoted 10 lines
drivers/target/target_core_user.c +0 -3
 	list_del_init(&cmd->queue_entry);
 	se_cmd = cmd->se_cmd;
 	tcmu_free_cmd(cmd);
 
-	pr_debug("Timing out queued cmd %p on dev %s.\n",
-		  cmd, cmd->tcmu_dev->name);
-
 	target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL);
 }
 

Thanks.

#3 lore raw
In reply to Dan Carpenter
Quoted 9 lines · Dan Carpenter

The pr_debug() dereferences "cmd" after we already freed it by calling tcmu_free_cmd(cmd). The debug printk needs to be done earlier.

Fixes 61fb24822166 ("scsi: target: tcmu: Userspace must not complete queued commands")
---
  drivers/target/target_core_user.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)

Thank you.

I'm very sorry for this stupid bug.

BR, Bodo

#4 lore raw
Quoted 11 lines · Bodo Stroesser
Quoted 9 lines · Dan Carpenter

The pr_debug() dereferences "cmd" after we already freed it by calling tcmu_free_cmd(cmd). The debug printk needs to be done earlier.

Fixes 61fb24822166 ("scsi: target: tcmu: Userspace must not complete queued commands")
---
  drivers/target/target_core_user.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 3 lines

Thank you.

I'm very sorry for this stupid bug.

Bugs like this are super common. Part of being human. It would have been hard to discover via testing because you have disable memory poisoning on free and debugging at the same time.

regards,
dan carpenter