nvmet: take subsys->lock while force-reconnecting in nvmet_subsys_attr_qid_max_store()
Subsystems (3)
PATCH 1/1 nvmet: take subsys->lock while force-reconnecting in nvmet_subsys_attr_qid_max_store()
nvmet_subsys_attr_qid_max_store() sets subsys->max_qid and then walks subsys->ctrls to force every controller to reconnect, but it holds only nvmet_config_sem (for write). subsys->ctrls is protected by subsys->lock:nvmet_alloc_ctrl() adds an entry under subsys->lock, and nvmet_ctrl_free() removes it under subsys->lock with list_del() + kfree() (not kfree_rcu). The free path takes no nvmet_config_sem, so the two locks are independent.
A controller teardown (host disconnect, keep-alive timeout or fatal error -> nvmet_sq_destroy() -> nvmet_ctrl_put() -> nvmet_ctrl_free()) can therefore list_del() an entry while qid_max_store() is iterating the list, corrupting the walk.
KASAN, concurrent "echo 128 > .../subsystems/<nqn>/attr_qid_max" racing nvme-loop controllers connecting and disconnecting, panics in seconds:
BUG: KASAN: null-ptr-deref in nvmet_subsys_attr_qid_max_store+0x10d/0x1b0 Read of size 8 at addr 0000000000000090 Call Trace: nvmet_subsys_attr_qid_max_store+0x10d/0x1b0 configfs_write_iter+0x1a7/0x250 vfs_write+0x5df/0x940 ksys_write+0xfd/0x1e0
(also seen as a general-protection fault dereferencing a LIST_POISON pointer at the same instruction.)
Take subsys->lock around the list_for_each_entry() loop, matching every other subsys->ctrls accessor. ->delete_ctrl() only queues asynchronous controller teardown, so it does not re-enter subsys->lock under the loop.
--- drivers/nvme/target/configfs.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/nvme/target/configfs.c +8 -1
@@ -1683,9 +1683,16 @@ static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,down_write (&nvmet_config_sem); subsys->max_qid = qid_max; -/* Force reconnect */ +/* +* Force reconnect. subsys->ctrls is protected by subsys->lock +* (nvmet_alloc_ctrl()/nvmet_ctrl_free() add and remove entries under +* it); take it here too so a concurrent controller teardown cannot +* list_del() an entry while this loop walks the list. +*/ +mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) ctrl->ops->delete_ctrl (ctrl); +mutex_unlock (&subsys->lock);up_write (&nvmet_config_sem);return cnt;
--
2.43.0
Quoted 61 lines · Yu Zhang
nvmet_subsys_attr_qid_max_store()setssubsys->max_qidand then walkssubsys->ctrlsto force every controller to reconnect, but it holds onlynvmet_config_sem(for write).subsys->ctrlsis protected bysubsys->lock:nvmet_alloc_ctrl()adds an entry undersubsys->lock, andnvmet_ctrl_free()removes it undersubsys->lockwithlist_del()+kfree()(notkfree_rcu). The free path takes nonvmet_config_sem, so the two locks are independent.A controller teardown (host disconnect, keep-alive timeout or fatal error ->
nvmet_sq_destroy()->nvmet_ctrl_put()->nvmet_ctrl_free()) can thereforelist_del()an entry whileqid_max_store()is iterating the list, corrupting the walk.KASAN, concurrent "echo 128 > .../subsystems/<nqn>/attr_qid_max" racing nvme-loop controllers connecting and disconnecting, panics in seconds:
BUG: KASAN: null-ptr-deref in nvmet_subsys_attr_qid_max_store+0x10d/0x1b0 Read of size 8 at addr 0000000000000090 Call Trace: nvmet_subsys_attr_qid_max_store+0x10d/0x1b0 configfs_write_iter+0x1a7/0x250 vfs_write+0x5df/0x940 ksys_write+0xfd/0x1e0(also seen as a general-protection fault dereferencing a
LIST_POISONpointer at the same instruction.)Take
subsys->lockaround thelist_for_each_entry()loop, matching every othersubsys->ctrlsaccessor. ->delete_ctrl() only queues asynchronous controller teardown, so it does not re-entersubsys->lockunder the loop.Fixes 3e980f5995e0 ("nvmet: expose max queues to configfs")Signed-off-by Yu Zhang <[email protected]>Tested-by Yu Zhang <[email protected]>--- drivers/nvme/target/configfs.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)drivers/nvme/target/configfs.c +8
-2@@ -1683,9 +1683,16 @@ static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item,down_write (&nvmet_config_sem); subsys->max_qid = qid_max; -/* Force reconnect */ +/* +* Force reconnect. subsys->ctrls is protected by subsys->lock +* (nvmet_alloc_ctrl()/nvmet_ctrl_free() add and remove entries under +* it); take it here too so a concurrent controller teardown cannot +* list_del() an entry while this loop walks the list. +*/ +mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) ctrl->ops->delete_ctrl (ctrl); +mutex_unlock (&subsys->lock);up_write (&nvmet_config_sem);return cnt; --
Quoted 1 line
2.43.0
I don't think we need a comment in the source code just to explain that. It's an established pattern all over the target driver code that accesses to the subsys->ctrls list are protected by subsys->lock, so this is already implied.
Anyway the fix looks good to me.
PATCH v2 1/1 nvmet: take subsys->lock while force-reconnecting in nvmet_subsys_attr_qid_max_store()
In reply to Yu Zhangnvmet_subsys_attr_qid_max_store() sets subsys->max_qid and then walks subsys->ctrls to force every controller to reconnect, but it holds only nvmet_config_sem (for write). subsys->ctrls is protected by subsys->lock:nvmet_alloc_ctrl() adds an entry under subsys->lock, and nvmet_ctrl_free() removes it under subsys->lock with list_del() + kfree() (not kfree_rcu). The free path takes no nvmet_config_sem, so the two locks are independent.
A controller teardown (host disconnect, keep-alive timeout or fatal error -> nvmet_sq_destroy() -> nvmet_ctrl_put() -> nvmet_ctrl_free()) can therefore list_del() an entry while qid_max_store() is iterating the list, corrupting the walk.
KASAN, concurrent "echo 128 > .../subsystems/<nqn>/attr_qid_max" racing nvme-loop controllers connecting and disconnecting, panics in seconds:
BUG: KASAN: null-ptr-deref in nvmet_subsys_attr_qid_max_store+0x10d/0x1b0 Read of size 8 at addr 0000000000000090 Call Trace: nvmet_subsys_attr_qid_max_store+0x10d/0x1b0 configfs_write_iter+0x1a7/0x250 vfs_write+0x5df/0x940 ksys_write+0xfd/0x1e0
(also seen as a general-protection fault dereferencing a LIST_POISON pointer at the same instruction.)
Take subsys->lock around the list_for_each_entry() loop, matching every other subsys->ctrls accessor. ->delete_ctrl() only queues asynchronous controller teardown, so it does not re-enter subsys->lock under the loop.
--- drivers/nvme/target/configfs.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/nvme/target/configfs.c +2 -0
@@ -1684,8 +1684,10 @@ static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item, subsys->max_qid = qid_max;/* Force reconnect */ +mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) ctrl->ops->delete_ctrl (ctrl); +mutex_unlock (&subsys->lock);up_write (&nvmet_config_sem);return cnt;
Signature · 1 line
2.43.0
Quoted 43 lines · Yu Zhang
nvmet_subsys_attr_qid_max_store()setssubsys->max_qidand then walkssubsys->ctrlsto force every controller to reconnect, but it holds onlynvmet_config_sem(for write).subsys->ctrlsis protected bysubsys->lock:nvmet_alloc_ctrl()adds an entry undersubsys->lock, andnvmet_ctrl_free()removes it undersubsys->lockwithlist_del()+kfree()(notkfree_rcu). The free path takes nonvmet_config_sem, so the two locks are independent.A controller teardown (host disconnect, keep-alive timeout or fatal error ->
nvmet_sq_destroy()->nvmet_ctrl_put()->nvmet_ctrl_free()) can thereforelist_del()an entry whileqid_max_store()is iterating the list, corrupting the walk.KASAN, concurrent "echo 128 > .../subsystems/<nqn>/attr_qid_max" racing nvme-loop controllers connecting and disconnecting, panics in seconds:
BUG: KASAN: null-ptr-deref in nvmet_subsys_attr_qid_max_store+0x10d/0x1b0 Read of size 8 at addr 0000000000000090 Call Trace: nvmet_subsys_attr_qid_max_store+0x10d/0x1b0 configfs_write_iter+0x1a7/0x250 vfs_write+0x5df/0x940 ksys_write+0xfd/0x1e0(also seen as a general-protection fault dereferencing a
LIST_POISONpointer at the same instruction.)Take
subsys->lockaround thelist_for_each_entry()loop, matching every othersubsys->ctrlsaccessor. ->delete_ctrl() only queues asynchronous controller teardown, so it does not re-entersubsys->lockunder the loop.Fixes 3e980f5995e0 ("nvmet: expose max queues to configfs")Signed-off-by Yu Zhang <[email protected]>Tested-by Yu Zhang <[email protected]>--- drivers/nvme/target/configfs.c | 2 ++ 1 file changed, 2 insertions(+)drivers/nvme/target/configfs.c +0
-0
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item, · line 1693
Quoted 10 lines
drivers/nvme/target/configfs.c +2
-0@@ -1684,8 +1684,10 @@ static ssize_t nvmet_subsys_attr_qid_max_store(struct config_item *item, subsys->max_qid = qid_max;/* Force reconnect */ +mutex_lock (&subsys->lock);list_for_each_entry (ctrl, &subsys->ctrls, subsys_entry) ctrl->ops->delete_ctrl (ctrl); +mutex_unlock (&subsys->lock);up_write (&nvmet_config_sem);return cnt;
Quoted 11 lines · Yu Zhang
nvmet_subsys_attr_qid_max_store()setssubsys->max_qidand then walkssubsys->ctrlsto force every controller to reconnect, but it holds onlynvmet_config_sem(for write).subsys->ctrlsis protected bysubsys->lock:nvmet_alloc_ctrl()adds an entry undersubsys->lock, andnvmet_ctrl_free()removes it undersubsys->lockwithlist_del()+kfree()(notkfree_rcu). The free path takes nonvmet_config_sem, so the two locks are independent.A controller teardown (host disconnect, keep-alive timeout or fatal error ->
nvmet_sq_destroy()->nvmet_ctrl_put()->nvmet_ctrl_free()) can thereforelist_del()an entry whileqid_max_store()is iterating the list, corrupting the walk.
Thanks, applied to nvme-7.4.