Skip to content

Commit b71cb08

Browse files
guixinliu1995martinkpetersen
authored andcommitted
scsi: target: tcm_loop: Fix NULL ptr dereference
The TCM_LOOP LUN creation process calls device_register() to create the device, which in turn invokes tcm_loop_driver_probe() registered with the TCM_LOOP bus to create and register the scsi_host. However, if the scsi_host memory allocation fails or scsi_add_host() fails, the device_register() process still returns success. Subsequently, when the user binds the LUN to a specific backend device, it accesses the NULL or freed scsi_host. Crash Call Trace: RIP: 0010:scsi_is_host_device+0x7/0x20 scsi_alloc_target+0x32/0x2c0 __scsi_add_device+0x41/0xf0 scsi_add_device+0xd/0x30 tcm_loop_port_link+0x25/0x50 [tcm_loop] target_fabric_port_link+0x9c/0xb0 [target_core_mod] ... This issue is fixed by: 1. Setting the tcm_loop_hba's scsi_host to NULL, if scsi_add_host() fails. 2. Checking the tcm_loop_hba's scsi_host after device_register(). 3. Checking the tcm_loop_hba's scsi_host in tcm_loop_driver_remove(). Fixes: 3703b2c ("[SCSI] tcm_loop: Add multi-fabric Linux/SCSI LLD fabric module") Signed-off-by: Guixin Liu <kanie@linux.alibaba.com> Reviewed-by: Mike Christie <michael.christie@oracle.com> Link: https://patch.msgid.link/20260424013923.25998-1-kanie@linux.alibaba.com Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent b52a8d5 commit b71cb08

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

drivers/target/loopback/tcm_loop.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ static int tcm_loop_driver_probe(struct device *dev)
393393
if (error) {
394394
pr_err("%s: scsi_add_host failed\n", __func__);
395395
scsi_host_put(sh);
396+
tl_hba->sh = NULL;
396397
return -ENODEV;
397398
}
398399
return 0;
@@ -406,8 +407,10 @@ static void tcm_loop_driver_remove(struct device *dev)
406407
tl_hba = to_tcm_loop_hba(dev);
407408
sh = tl_hba->sh;
408409

409-
scsi_remove_host(sh);
410-
scsi_host_put(sh);
410+
if (sh) {
411+
scsi_remove_host(sh);
412+
scsi_host_put(sh);
413+
}
411414
}
412415

413416
static void tcm_loop_release_adapter(struct device *dev)
@@ -436,6 +439,11 @@ static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host
436439
return -ENODEV;
437440
}
438441

442+
if (!tl_hba->sh) {
443+
device_unregister(&tl_hba->dev);
444+
return -ENODEV;
445+
}
446+
439447
return 0;
440448
}
441449

0 commit comments

Comments
 (0)