Skip to content

Commit 49cb77e

Browse files
author
Nicholas Bellinger
committed
target: Avoid mappedlun symlink creation during lun shutdown
This patch closes a race between se_lun deletion during configfs unlink in target_fabric_port_unlink() -> core_dev_del_lun() -> core_tpg_remove_lun(), when transport_clear_lun_ref() blocks waiting for percpu_ref RCU grace period to finish, but a new NodeACL mappedlun is added before the RCU grace period has completed. This can happen in target_fabric_mappedlun_link() because it only checks for se_lun->lun_se_dev, which is not cleared until after transport_clear_lun_ref() percpu_ref RCU grace period finishes. This bug originally manifested as NULL pointer dereference OOPsen in target_stat_scsi_att_intr_port_show_attr_dev() on v4.1.y code, because it dereferences lun->lun_se_dev without a explicit NULL pointer check. In post v4.1 code with target-core RCU conversion, the code in target_stat_scsi_att_intr_port_show_attr_dev() no longer uses se_lun->lun_se_dev, but the same race still exists. To address the bug, go ahead and set se_lun>lun_shutdown as early as possible in core_tpg_remove_lun(), and ensure new NodeACL mappedlun creation in target_fabric_mappedlun_link() fails during se_lun shutdown. Reported-by: James Shen <jcs@datera.io> Cc: James Shen <jcs@datera.io> Tested-by: James Shen <jcs@datera.io> Cc: stable@vger.kernel.org # 3.10+ Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
1 parent efb2ea7 commit 49cb77e

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

drivers/target/target_core_fabric_configfs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ static int target_fabric_mappedlun_link(
9292
pr_err("Source se_lun->lun_se_dev does not exist\n");
9393
return -EINVAL;
9494
}
95+
if (lun->lun_shutdown) {
96+
pr_err("Unable to create mappedlun symlink because"
97+
" lun->lun_shutdown=true\n");
98+
return -EINVAL;
99+
}
95100
se_tpg = lun->lun_tpg;
96101

97102
nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;

drivers/target/target_core_tpg.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ void core_tpg_remove_lun(
642642
*/
643643
struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
644644

645+
lun->lun_shutdown = true;
646+
645647
core_clear_lun_from_tpg(lun, tpg);
646648
/*
647649
* Wait for any active I/O references to percpu se_lun->lun_ref to
@@ -663,6 +665,8 @@ void core_tpg_remove_lun(
663665
}
664666
if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
665667
hlist_del_rcu(&lun->link);
668+
669+
lun->lun_shutdown = false;
666670
mutex_unlock(&tpg->tpg_lun_mutex);
667671

668672
percpu_ref_exit(&lun->lun_ref);

include/target/target_core_base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ struct se_lun {
705705
u64 unpacked_lun;
706706
#define SE_LUN_LINK_MAGIC 0xffff7771
707707
u32 lun_link_magic;
708+
bool lun_shutdown;
708709
bool lun_access_ro;
709710
u32 lun_index;
710711

0 commit comments

Comments
 (0)