Commit 49cb77e2 authored by Nicholas Bellinger's avatar Nicholas Bellinger

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: default avatarJames Shen <jcs@datera.io>
Cc: James Shen <jcs@datera.io>
Tested-by: default avatarJames Shen <jcs@datera.io>
Cc: stable@vger.kernel.org # 3.10+
Signed-off-by: default avatarNicholas Bellinger <nab@linux-iscsi.org>
parent efb2ea77
...@@ -92,6 +92,11 @@ static int target_fabric_mappedlun_link( ...@@ -92,6 +92,11 @@ static int target_fabric_mappedlun_link(
pr_err("Source se_lun->lun_se_dev does not exist\n"); pr_err("Source se_lun->lun_se_dev does not exist\n");
return -EINVAL; return -EINVAL;
} }
if (lun->lun_shutdown) {
pr_err("Unable to create mappedlun symlink because"
" lun->lun_shutdown=true\n");
return -EINVAL;
}
se_tpg = lun->lun_tpg; se_tpg = lun->lun_tpg;
nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item; nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
......
...@@ -642,6 +642,8 @@ void core_tpg_remove_lun( ...@@ -642,6 +642,8 @@ void core_tpg_remove_lun(
*/ */
struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev); struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
lun->lun_shutdown = true;
core_clear_lun_from_tpg(lun, tpg); core_clear_lun_from_tpg(lun, tpg);
/* /*
* Wait for any active I/O references to percpu se_lun->lun_ref to * Wait for any active I/O references to percpu se_lun->lun_ref to
...@@ -663,6 +665,8 @@ void core_tpg_remove_lun( ...@@ -663,6 +665,8 @@ void core_tpg_remove_lun(
} }
if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) if (!(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
hlist_del_rcu(&lun->link); hlist_del_rcu(&lun->link);
lun->lun_shutdown = false;
mutex_unlock(&tpg->tpg_lun_mutex); mutex_unlock(&tpg->tpg_lun_mutex);
percpu_ref_exit(&lun->lun_ref); percpu_ref_exit(&lun->lun_ref);
......
...@@ -705,6 +705,7 @@ struct se_lun { ...@@ -705,6 +705,7 @@ struct se_lun {
u64 unpacked_lun; u64 unpacked_lun;
#define SE_LUN_LINK_MAGIC 0xffff7771 #define SE_LUN_LINK_MAGIC 0xffff7771
u32 lun_link_magic; u32 lun_link_magic;
bool lun_shutdown;
bool lun_access_ro; bool lun_access_ro;
u32 lun_index; u32 lun_index;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment