nvmet: fix max_qid race between configfs and controller allocation
Subsystems (2)
PATCH v2 1/1 nvmet: fix max_qid race between configfs and controller allocation
The function nvmet_subsys_attr_qid_max_store() can race against nvmet_alloc_ctrl() when a subsystem's max_qid limit is modified.
Suppose max_qid is currently 64. If nvmet_alloc_ctrl() executes:ctrl->sqs = kzalloc_objs(struct nvmet_sq *, subsys->max_qid + 1); and at this exact point, a userspace process changes max_qid to 128, nvmet_subsys_attr_qid_max_store() will set the new max_qid value. It attempts to delete active controllers to force a reconnect, but the new controller won't be deleted because it hasn't been added to the subsys->ctrls list yet.
nvmet_alloc_ctrl() then proceeds and adds the new controller to the subsys->ctrls list. Later, when nvmet_install_queue() is called, it will see max_qid set to 128, but the memory allocated for sqs is only sized for 64 entries. This results in a KASAN out-of-bounds warning and potential memory corruptions.
Fix this by protecting the queue allocations and list insertion in nvmet_alloc_ctrl() with down_read(&nvmet_config_sem). Because nvmet_subsys_attr_qid_max_store() acquires down_write(&nvmet_config_sem) to modify the attribute, this safely prevents the configfs writer from modifying max_qid during controller creation.
Copy the max_qid from the subsystem to the controller's structure during the allocation; ctrl->max_qid never changes as long as the controller remains in LIVE state, so this will prevent similar race conditions.
---
V2: copy the max_qid from the subsys's structure to controller's structure
drivers/nvme/target/admin-cmd.c | 8 ++--- drivers/nvme/target/core.c | 49 +++++++++++++++++-------------- drivers/nvme/target/fabrics-cmd.c | 2 +- drivers/nvme/target/nvmet.h | 6 ++++ drivers/nvme/target/pci-epf.c | 2 +- 5 files changed, 39 insertions(+), 28 deletions(-)
drivers/nvme/target/admin-cmd.c +4 -4
@@ -1335,7 +1335,7 @@ static u16 nvmet_set_feat_arbitration(struct nvmet_req *req)void nvmet_execute_set_features (struct nvmet_req *req) { -struct nvmet_subsys *subsys =nvmet_req_subsys (req); +struct nvmet_ctrl *ctrl =nvmet_req_ctrl (req);u32 cdw10 =le32_to_cpu (req->cmd->common.cdw10);u32 cdw11 =le32_to_cpu (req->cmd->common.cdw11);u16 status =0 ;
@@ -1357,7 +1357,7 @@ void nvmet_execute_set_features(struct nvmet_req *req)break ; }nvmet_set_result (req, - (subsys->max_qid -1 ) | ((subsys->max_qid -1 ) <<16 )); + (ctrl->max_qid -1 ) | ((ctrl->max_qid -1 ) <<16 ));break ;case NVME_FEAT_IRQ_COALESCE : status =nvmet_set_feat_irq_coalesce (req);
@@ -1494,7 +1494,7 @@ void nvmet_get_feat_async_event(struct nvmet_req *req)void nvmet_execute_get_features (struct nvmet_req *req) { -struct nvmet_subsys *subsys =nvmet_req_subsys (req); +struct nvmet_ctrl *ctrl =nvmet_req_ctrl (req);u32 cdw10 =le32_to_cpu (req->cmd->common.cdw10);u16 status =0 ;
@@ -1534,7 +1534,7 @@ void nvmet_execute_get_features(struct nvmet_req *req)break ;case NVME_FEAT_NUM_QUEUES :nvmet_set_result (req, - (subsys->max_qid-1 ) | ((subsys->max_qid-1 ) <<16 )); + (ctrl->max_qid-1 ) | ((ctrl->max_qid-1 ) <<16 ));break ;case NVME_FEAT_KATO :nvmet_get_feat_kato (req);
drivers/nvme/target/core.c +27 -22
94 lines not on this page · raw message
drivers/nvme/target/fabrics-cmd.c +1 -1
@@ -370,7 +370,7 @@ static void nvmet_execute_io_connect(struct nvmet_req *req) goto out; } -if (unlikely (qid > ctrl->subsys->max_qid)) { +if (unlikely (qid > ctrl->max_qid)) {pr_warn ("invalid queue id (%d)\n" , qid); status =NVME_SC_CONNECT_INVALID_PARAM |NVME_STATUS_DNR ; req->cqe->result.u32 =IPO_IATTR_CONNECT_SQE (qid);
drivers/nvme/target/nvmet.h +6 -0
@@ -265,6 +265,7 @@ struct nvmet_ctrl {uuid_t hostid;u16 cntlid; +u16 max_qid;u32 kato;struct nvmet_port *port;
@@ -753,6 +754,11 @@ static inline struct nvmet_subsys *nvmet_req_subsys(struct nvmet_req *req)return req->sq->ctrl->subsys; } +static inline struct nvmet_ctrl *nvmet_req_ctrl (struct nvmet_req *req) +{ +return req->sq->ctrl; +} +static inline bool nvmet_is_disc_subsys (struct nvmet_subsys *subsys) {return subsys->type !=NVME_NQN_NVME ;
drivers/nvme/target/pci-epf.c +1 -1
@@ -2077,7 +2077,7 @@ static int nvmet_pci_epf_create_ctrl(struct nvmet_pci_epf *nvme_epf, }/* Allocate our queues, up to the maximum number. */ - ctrl->nr_queues =min (ctrl->tctrl->subsys->max_qid +1 , max_nr_queues); + ctrl->nr_queues =min (ctrl->tctrl->max_qid +1 , max_nr_queues); ret =nvmet_pci_epf_alloc_queues (ctrl);if (ret) goto out_put_ctrl;
Signature · 1 line
2.55.0
Quoted 2 lines · Maurizio Lombardi
The function
nvmet_subsys_attr_qid_max_store()can race againstnvmet_alloc_ctrl()when a subsystem'smax_qidlimit is modified.
Thanks, applied to nvme-7.3.