Commit 8adc6142 authored by Nicholas Bellinger's avatar Nicholas Bellinger Committed by Greg Kroah-Hartman

target: Avoid mappedlun symlink creation during lun shutdown

commit 49cb77e2 upstream.

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>
Signed-off-by: default avatarNicholas Bellinger <nab@linux-iscsi.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 4ad69d5f
...@@ -84,6 +84,11 @@ static int target_fabric_mappedlun_link( ...@@ -84,6 +84,11 @@ static int target_fabric_mappedlun_link(
"_tpg does not exist\n"); "_tpg 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_sep->sep_tpg; se_tpg = lun->lun_sep->sep_tpg;
nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item; nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
......
...@@ -843,6 +843,8 @@ void core_tpg_remove_lun( ...@@ -843,6 +843,8 @@ void core_tpg_remove_lun(
struct se_portal_group *tpg, struct se_portal_group *tpg,
struct se_lun *lun) struct se_lun *lun)
{ {
lun->lun_shutdown = true;
core_clear_lun_from_tpg(lun, tpg); core_clear_lun_from_tpg(lun, tpg);
transport_clear_lun_ref(lun); transport_clear_lun_ref(lun);
...@@ -850,6 +852,7 @@ void core_tpg_remove_lun( ...@@ -850,6 +852,7 @@ void core_tpg_remove_lun(
spin_lock(&tpg->tpg_lun_lock); spin_lock(&tpg->tpg_lun_lock);
lun->lun_status = TRANSPORT_LUN_STATUS_FREE; lun->lun_status = TRANSPORT_LUN_STATUS_FREE;
lun->lun_shutdown = false;
spin_unlock(&tpg->tpg_lun_lock); spin_unlock(&tpg->tpg_lun_lock);
percpu_ref_exit(&lun->lun_ref); percpu_ref_exit(&lun->lun_ref);
......
...@@ -719,6 +719,7 @@ struct se_lun { ...@@ -719,6 +719,7 @@ struct se_lun {
u32 lun_access; u32 lun_access;
u32 lun_flags; u32 lun_flags;
u32 unpacked_lun; u32 unpacked_lun;
bool lun_shutdown;
atomic_t lun_acl_count; atomic_t lun_acl_count;
spinlock_t lun_acl_lock; spinlock_t lun_acl_lock;
spinlock_t lun_sep_lock; spinlock_t lun_sep_lock;
......
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