From: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com> mainline inclusion from mainline-v7.4-rc2 commit b1d1f6b1ee03285690f12c2ebf23616416e6d881 category: bugfix bugzilla: https://atomgit.com/src-openeuler/kernel/issues/21329 CVE: CVE-2026-99571 Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=... -------------------------------- nvme_tcp_free_ctrl() reads ctrl->ctrl.opts to know how many queue mutexes to destroy. When creating the controller fails, nvmf_create_ctrl() frees opts as soon as nvme_tcp_create_ctrl() returns, but a concurrent open of the character device can hold the last reference to the controller. With CONFIG_DEBUG_KOBJECT_RELEASE, which delays the release, every failed connect reads freed memory: BUG: KASAN: slab-use-after-free in nvme_tcp_free_ctrl+0x38b/0x430 Read of size 4 at addr ffff888005b93740 by task kworker/2:1/51 Keep the size of the queue array in the controller and use it instead. Fixes: 2eeeaa9c1ff0 ("nvme-tcp: Validate the queue state under the queue lock in the address attribute") Cc: stable@vger.kernel.org Reviewed-by: Nilay Shroff <nilay@linux.ibm.com> Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com> Signed-off-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Zizhi Wo <wozizhi@huawei.com> --- drivers/nvme/host/tcp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c index 45a2c5d4de50..acde1ac93633 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -172,10 +172,11 @@ struct nvme_tcp_ctrl { struct work_struct err_work; struct delayed_work connect_work; struct nvme_tcp_request async_req; u32 io_queues[HCTX_MAX_TYPES]; + unsigned int nr_allocated_queues; }; static LIST_HEAD(nvme_tcp_ctrl_list); static DEFINE_MUTEX(nvme_tcp_ctrl_mutex); static struct workqueue_struct *nvme_tcp_wq; @@ -2304,12 +2305,10 @@ static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl) } static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl) { struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); - /* the same count used to size and initialize the queue array */ - unsigned int nr_queues = NVME_TCP_OPT_NR_QUEUES(nctrl->opts); unsigned int i; if (list_empty(&ctrl->list)) goto free_ctrl; @@ -2317,11 +2316,11 @@ static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl) list_del(&ctrl->list); mutex_unlock(&nvme_tcp_ctrl_mutex); nvmf_free_options(nctrl->opts); free_ctrl: - for (i = 0; i < nr_queues; i++) + for (i = 0; i < ctrl->nr_allocated_queues; i++) mutex_destroy(&ctrl->queues[i].queue_lock); kfree(ctrl->queues); kfree(ctrl); } @@ -2708,11 +2707,12 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev, if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) { ret = -EALREADY; goto out_free_ctrl; } - ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues), + ctrl->nr_allocated_queues = ctrl->ctrl.queue_count; + ctrl->queues = kcalloc(ctrl->nr_allocated_queues, sizeof(*ctrl->queues), GFP_KERNEL); if (!ctrl->queues) { ret = -ENOMEM; goto out_free_ctrl; } @@ -2720,11 +2720,11 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev, /* * The queue locks are owned by the controller, not the queues: * they outlive the per-queue connect/disconnect cycles so that * sysfs readers can never race their destruction. */ - for (i = 0; i < ctrl->ctrl.queue_count; i++) + for (i = 0; i < ctrl->nr_allocated_queues; i++) mutex_init(&ctrl->queues[i].queue_lock); ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0); if (ret) goto out_kfree_queues; -- 2.52.0