FAILED: patch "[PATCH] nvmet: fix pre-auth out-of-bounds heap read in Discovery Get" failed to apply to 5.10-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x 53cd102a7a56079b11b897835bd9b94c14e6322c
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026071542-blip-estate-70e4@gregkh' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 53cd102a7a56079b11b897835bd9b94c14e6322c Mon Sep 17 00:00:00 2001
From: Bryam Vargas <[email protected]>
Date: Wed, 27 May 2026 15:00:00 -0500
Subject: [PATCH] nvmet: fix pre-auth out-of-bounds heap read in Discovery Get
Log Page
nvmet_execute_disc_get_log_page() validates only the dword alignment of the host-supplied Log Page Offset (lpo). The 64-bit offset is then added to a small kzalloc'd buffer that holds the discovery log page and the result is passed straight to nvmet_copy_to_sgl(), which memcpy()s data_len bytes out to the host with no source-side bound check:
u64 offset = nvmet_get_log_page_offset(req->cmd); /* 64-bit host */
size_t data_len = nvmet_get_log_page_len(req->cmd); /* 32-bit host */
...
if (offset & 0x3) { ... } /* only check */
...
alloc_len = sizeof(*hdr) + entry_size * discovery_log_entries(req);
buffer = kzalloc(alloc_len, GFP_KERNEL);
...
status = nvmet_copy_to_sgl(req, 0, buffer + offset, data_len);The Discovery controller is unauthenticated -- nvmet_host_allowed() returns true unconditionally for the discovery subsystem -- so the call is reachable pre-authentication by any TCP/RDMA/FC peer that can reach the nvmet target. With a discovery log page of ~1 KiB, an attacker requesting up to 4 KiB starting at offset == alloc_len reads the next slab page out and gets its content returned over the fabric (an empirical run on a default nvmet-tcp loopback target leaked 81 canonical kernel pointers in one Get Log Page response). Pointing the offset at unmapped kernel memory faults the in-kernel memcpy and crashes (or panics, on panic_on_oops=1) the target host instead.
The attacker-controlled source-side offset pattern "nvmet_copy_to_sgl(req, 0, buffer + ATTACKER_OFFSET, ...)" is unique to nvmet_execute_disc_get_log_page in the entire nvmet codebase: every other Get Log Page handler in admin-cmd.c either ignores lpo (and silently starts every response at offset 0) or tracks a local destination offset with a fixed source pointer.
Validate the host-supplied offset against the log page size, cap the copy length to what is actually available, and zero-fill any remainder of the host transfer buffer. The zero-fill matches the existing short-response pattern in nvmet_execute_get_log_changed_ns() (admin-cmd.c) and prevents leaking transport SGL contents when the host asks for more bytes than the log page contains.
drivers/nvme/target/discovery.c +22 -1
@@ -166,6 +166,7 @@ static void nvmet_execute_disc_get_log_page(struct nvmet_req *req)u64 offset =nvmet_get_log_page_offset (req->cmd);size_t data_len =nvmet_get_log_page_len (req->cmd);size_t alloc_len; +size_t copy_len;struct nvmet_subsys_link *p;struct nvmet_port *r;u32 numrec =0 ;
@@ -242,7 +243,27 @@ static void nvmet_execute_disc_get_log_page(struct nvmet_req *req)up_read (&nvmet_config_sem); - status =nvmet_copy_to_sgl (req,0 , buffer + offset, data_len); +/* +* Validate the host-supplied log page offset before copying out. +* Without this check, the host controls a 64-bit byte offset into +* a small kzalloc'd buffer: a value past the log page lets the +* subsequent memcpy read adjacent kernel heap, and a value aimed +* at unmapped kernel memory faults the in-kernel copy and crashes +* the target host. The Discovery controller is unauthenticated, +* so the bug is reachable from any reachable fabric peer. +*/ +if (offset > alloc_len) { + req->error_loc = + offsetof(struct nvme_get_log_page_command , lpo); + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; + goto out_free_buffer; + } + + copy_len =min_t (size_t, data_len, alloc_len - offset); + status =nvmet_copy_to_sgl (req,0 , buffer + offset, copy_len); +if (!status && copy_len < data_len) + status =nvmet_zero_sgl (req, copy_len, data_len - copy_len); +out_free_buffer:kfree (buffer); out:nvmet_req_complete (req, status);
Sasha Levin
maintainerPATCH 1/3 nvme: fix status magic numbers
From: Weiwen Hu <[email protected]>
[ Upstream commit d89a5c6705998ddc42b104f8eabd3c4b9e8fde08 ]
Replaced some magic numbers about SC and SCT with enum and macro.
Stable-dep-of: 53cd102a7a56 ("nvmet: fix pre-auth out-of-bounds heap read in Discovery Get Log Page")
--- drivers/nvme/host/core.c | 2 +- drivers/nvme/host/multipath.c | 2 +- drivers/nvme/host/nvme.h | 4 ++-- include/linux/nvme.h | 14 ++++++++++++-- 4 files changed, 16 insertions(+), 6 deletions(-)
drivers/nvme/host/core.c +1 -1
@@ -216,7 +216,7 @@ static void nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)static blk_status_t nvme_error_status (u16 status) { -switch (status &0x7ff ) { +switch (status &NVME_SCT_SC_MASK ) {case NVME_SC_SUCCESS :return BLK_STS_OK ;case NVME_SC_CAP_EXCEEDED :
drivers/nvme/host/multipath.c +1 -1
@@ -68,7 +68,7 @@ void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,void nvme_failover_req (struct request *req) {struct nvme_ns *ns = req->q->queuedata; -u16 status =nvme_req (req)->status &0x7ff ; +u16 status =nvme_req (req)->status &NVME_SCT_SC_MASK ;unsigned long flags;nvme_mpath_clear_current_path (ns);
drivers/nvme/host/nvme.h +2 -2
@@ -586,7 +586,7 @@ static inline u32 nvme_bytes_to_numd(size_t len)static inline bool nvme_is_ana_error (u16 status) { -switch (status &0x7ff ) { +switch (status &NVME_SCT_SC_MASK ) {case NVME_SC_ANA_TRANSITION :case NVME_SC_ANA_INACCESSIBLE :case NVME_SC_ANA_PERSISTENT_LOSS :
@@ -599,7 +599,7 @@ static inline bool nvme_is_ana_error(u16 status)static inline bool nvme_is_path_error (u16 status) {/* check for a status code type of 'path related status' */ -return (status &0x700 ) ==0x300 ; +return (status &NVME_SCT_MASK ) ==NVME_SCT_PATH ; }/*
include/linux/nvme.h +12 -2
@@ -1463,6 +1463,7 @@ enum {/* * Generic Command Status: */ +NVME_SCT_GENERIC =0x0 ,NVME_SC_SUCCESS =0x0 ,NVME_SC_INVALID_OPCODE =0x1 , NVME_SC_INVALID_FIELD = 0x2,
@@ -1499,6 +1500,7 @@ enum {/* * Command Specific Status: */ +NVME_SCT_COMMAND_SPECIFIC =0x100 ,NVME_SC_CQ_INVALID =0x100 ,NVME_SC_QID_INVALID =0x101 , NVME_SC_QUEUE_SIZE = 0x102,
@@ -1565,6 +1567,7 @@ enum {/* * Media and Data Integrity Errors: */ +NVME_SCT_MEDIA_ERROR =0x200 ,NVME_SC_WRITE_FAULT =0x280 ,NVME_SC_READ_ERROR =0x281 , NVME_SC_GUARD_CHECK = 0x282,
@@ -1577,16 +1580,23 @@ enum {/* * Path-related Errors: */ +NVME_SCT_PATH =0x300 ,NVME_SC_ANA_PERSISTENT_LOSS =0x301 ,NVME_SC_ANA_INACCESSIBLE =0x302 ,NVME_SC_ANA_TRANSITION =0x303 ,NVME_SC_HOST_PATH_ERROR =0x370 ,NVME_SC_HOST_ABORTED_CMD =0x371 , -NVME_SC_CRD =0x1800 , - NVME_SC_DNR = 0x4000, +NVME_SC_MASK =0x00ff ,/* Status Code */ +NVME_SCT_MASK =0x0700 ,/* Status Code Type */ +NVME_SCT_SC_MASK =NVME_SCT_MASK |NVME_SC_MASK , + +NVME_SC_CRD =0x1800 ,/* Command Retry Delayed */ + NVME_SC_DNR = 0x4000, /* Do Not Retry */ }; +#define NVME_SCT (status) ((status) >> 8 & 7) + struct nvme_completion {/* * Used by Admin and Fabrics commands to return data:
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 nvme: rename CDR/MORE/DNR to NVME_STATUS_*
From: Weiwen Hu <[email protected]>
[ Upstream commit dd0b0a4a2c5d7209457dc172997d1243ad269cfa ]
CDR/MORE/DNR fields are not belonging to SC in the NVMe spec, rename them to NVME_STATUS_* to avoid confusion.
Stable-dep-of: 53cd102a7a56 ("nvmet: fix pre-auth out-of-bounds heap read in Discovery Get Log Page")
--- drivers/nvme/host/core.c | 12 ++++----- drivers/nvme/host/fabrics.c | 8 +++--- drivers/nvme/host/fault_inject.c | 2 +- drivers/nvme/target/admin-cmd.c | 28 ++++++++++----------- drivers/nvme/target/core.c | 42 +++++++++++++++---------------- drivers/nvme/target/discovery.c | 14 +++++------ drivers/nvme/target/fabrics-cmd.c | 30 +++++++++++----------- drivers/nvme/target/io-cmd-bdev.c | 14 +++++------ drivers/nvme/target/io-cmd-file.c | 2 +- drivers/nvme/target/passthru.c | 12 ++++----- drivers/nvme/target/rdma.c | 10 ++++---- drivers/nvme/target/tcp.c | 4 +-- include/linux/nvme.h | 4 +-- 13 files changed, 91 insertions(+), 91 deletions(-)
drivers/nvme/host/core.c +6 -6
@@ -263,7 +263,7 @@ static void nvme_retry_req(struct request *req)u16 crd;/* The mask and shift result must be <= 3 */ - crd = (nvme_req (req)->status &NVME_SC_CRD ) >>11 ; + crd = (nvme_req (req)->status &NVME_STATUS_CRD ) >>11 ;if (ns && crd) delay = ns->ctrl->crdt[crd -1 ] *100 ;
@@ -284,7 +284,7 @@ static inline enum nvme_disposition nvme_decide_disposition(struct request *req)return COMPLETE ;if (blk_noretry_request (req) || - (nvme_req (req)->status &NVME_SC_DNR ) || + (nvme_req (req)->status &NVME_STATUS_DNR ) ||nvme_req (req)->retries >= nvme_max_retries)return COMPLETE ;
@@ -1421,7 +1421,7 @@ static int nvme_identify_ns(struct nvme_ctrl *ctrl, unsigned nsid, goto out_free_id; } - error =NVME_SC_INVALID_NS |NVME_SC_DNR ; + error =NVME_SC_INVALID_NS |NVME_STATUS_DNR ;if ((*id)->ncap ==0 )/* namespace not allocated or attached */ goto out_free_id;
@@ -4074,7 +4074,7 @@ static void nvme_ns_remove_by_nsid(struct nvme_ctrl *ctrl, u32 nsid)static void nvme_validate_ns (struct nvme_ns *ns,struct nvme_ns_ids *ids) {struct nvme_id_ns *id; -int ret =NVME_SC_INVALID_NS |NVME_SC_DNR ; +int ret =NVME_SC_INVALID_NS |NVME_STATUS_DNR ;if (test_bit (NVME_NS_DEAD , &ns->flags)) goto out;
@@ -4083,7 +4083,7 @@ static void nvme_validate_ns(struct nvme_ns *ns, struct nvme_ns_ids *ids)if (ret) goto out; - ret =NVME_SC_INVALID_NS |NVME_SC_DNR ; + ret =NVME_SC_INVALID_NS |NVME_STATUS_DNR ;if (!nvme_ns_ids_equal (&ns->head->ids, ids)) {dev_err (ns->ctrl->device,"identifiers changed for nsid %d\n" , ns->head->ns_id);
@@ -4101,7 +4101,7 @@ static void nvme_validate_ns(struct nvme_ns *ns, struct nvme_ns_ids *ids)* * TODO: we should probably schedule a delayed retry here. */ -if (ret >0 && (ret &NVME_SC_DNR )) +if (ret >0 && (ret &NVME_STATUS_DNR ))nvme_ns_remove (ns); }
drivers/nvme/host/fabrics.c +4 -4
@@ -158,7 +158,7 @@ int nvmf_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)if (unlikely (ret !=0 ))dev_err (ctrl->device,"Property Get error: %d, offset %#x\n" , - ret >0 ? ret & ~NVME_SC_DNR : ret, off); + ret >0 ? ret & ~NVME_STATUS_DNR : ret, off);return ret; }
@@ -205,7 +205,7 @@ int nvmf_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)if (unlikely (ret !=0 ))dev_err (ctrl->device,"Property Get error: %d, offset %#x\n" , - ret >0 ? ret & ~NVME_SC_DNR : ret, off); + ret >0 ? ret & ~NVME_STATUS_DNR : ret, off);return ret; }EXPORT_SYMBOL_GPL (nvmf_reg_read64);
@@ -248,7 +248,7 @@ int nvmf_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)if (unlikely (ret))dev_err (ctrl->device,"Property Set error: %d, offset %#x\n" , - ret >0 ? ret & ~NVME_SC_DNR : ret, off); + ret >0 ? ret & ~NVME_STATUS_DNR : ret, off);return ret; }EXPORT_SYMBOL_GPL (nvmf_reg_write32);
@@ -287,7 +287,7 @@ static void nvmf_log_connect_error(struct nvme_ctrl *ctrl, int errval, int offset, struct nvme_command *cmd, struct nvmf_connect_data *data) { - int err_sctype = errval & (~NVME_SC_DNR); + int err_sctype = errval & (~NVME_STATUS_DNR);switch (err_sctype) {
drivers/nvme/host/fault_inject.c +1 -1
@@ -75,7 +75,7 @@ void nvme_should_fail(struct request *req)/* inject status code and DNR bit */ status = fault_inject->status;if (fault_inject->dont_retry) - status |=NVME_SC_DNR ; + status |=NVME_STATUS_DNR ;nvme_req (req)->status = status; } }
drivers/nvme/target/admin-cmd.c +14 -14
@@ -316,7 +316,7 @@ static void nvmet_execute_get_log_page(struct nvmet_req *req)pr_debug ("unhandled lid %d on qid %d\n" , req->cmd->get_log_page.lid, req->sq->qid); req->error_loc = offsetof(struct nvme_get_log_page_command , lid); -nvmet_req_complete (req,NVME_SC_INVALID_FIELD |NVME_SC_DNR ); +nvmet_req_complete (req,NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ); } static void nvmet_id_set_model_number(struct nvme_id_ctrl *id,
@@ -474,7 +474,7 @@ static void nvmet_execute_identify_ns(struct nvmet_req *req)if (le32_to_cpu (req->cmd->identify.nsid) ==NVME_NSID_ALL ) { req->error_loc = offsetof(struct nvme_identify , nsid); - status =NVME_SC_INVALID_NS |NVME_SC_DNR ; + status =NVME_SC_INVALID_NS |NVME_STATUS_DNR ; goto out; }
@@ -613,7 +613,7 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req) ns =nvmet_find_namespace (req->sq->ctrl, req->cmd->identify.nsid);if (!ns) { req->error_loc = offsetof(struct nvme_identify , nsid); - status =NVME_SC_INVALID_NS |NVME_SC_DNR ; + status =NVME_SC_INVALID_NS |NVME_STATUS_DNR ; goto out; }
@@ -634,7 +634,7 @@ static void nvmet_execute_identify_desclist(struct nvmet_req *req)if (sg_zero_buffer (req->sg, req->sg_cnt,NVME_IDENTIFY_DATA_SIZE - off, off) !=NVME_IDENTIFY_DATA_SIZE - off) - status =NVME_SC_INTERNAL |NVME_SC_DNR ; + status =NVME_SC_INTERNAL |NVME_STATUS_DNR ; out_put_ns:nvmet_put_namespace (ns); out:
@@ -660,7 +660,7 @@ static void nvmet_execute_identify(struct nvmet_req *req)pr_debug ("unhandled identify cns %d on qid %d\n" , req->cmd->identify.cns, req->sq->qid); req->error_loc = offsetof(struct nvme_identify , cns); -nvmet_req_complete (req,NVME_SC_INVALID_FIELD |NVME_SC_DNR ); +nvmet_req_complete (req,NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ); }/*
@@ -745,7 +745,7 @@ u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask)if (val32 & ~mask) { req->error_loc = offsetof(struct nvme_common_command , cdw11); -return NVME_SC_INVALID_FIELD |NVME_SC_DNR ; +return NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; }WRITE_ONCE (req->sq->ctrl->aen_enabled, val32);
@@ -771,7 +771,7 @@ void nvmet_execute_set_features(struct nvmet_req *req) ncqr = (cdw11 >>16 ) &0xffff ; nsqr = cdw11 &0xffff ;if (ncqr ==0xffff || nsqr ==0xffff ) { - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ;break ; } nvmet_set_result(req,
@@ -784,14 +784,14 @@ void nvmet_execute_set_features(struct nvmet_req *req) status =nvmet_set_feat_async_event (req,NVMET_AEN_CFG_ALL );break ;case NVME_FEAT_HOST_ID : - status =NVME_SC_CMD_SEQ_ERROR |NVME_SC_DNR ; + status =NVME_SC_CMD_SEQ_ERROR |NVME_STATUS_DNR ;break ;case NVME_FEAT_WRITE_PROTECT : status =nvmet_set_feat_write_protect (req);break ;default : req->error_loc = offsetof(struct nvme_common_command , cdw10); - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ;break ; }
@@ -806,7 +806,7 @@ static u16 nvmet_get_feat_write_protect(struct nvmet_req *req) req->ns =nvmet_find_namespace (req->sq->ctrl, req->cmd->common.nsid);if (!req->ns) { req->error_loc = offsetof(struct nvme_common_command , nsid); -return NVME_SC_INVALID_NS |NVME_SC_DNR ; +return NVME_SC_INVALID_NS |NVME_STATUS_DNR ; }mutex_lock (&subsys->lock);if (req->ns->readonly == true)
@@ -878,7 +878,7 @@ void nvmet_execute_get_features(struct nvmet_req *req)if (!(req->cmd->common.cdw11 &cpu_to_le32 (1 <<0 ))) { req->error_loc = offsetof(struct nvme_common_command , cdw11); - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ;break ; }
@@ -891,7 +891,7 @@ void nvmet_execute_get_features(struct nvmet_req *req)default : req->error_loc = offsetof(struct nvme_common_command , cdw10); - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ;break ; }
@@ -908,7 +908,7 @@ void nvmet_execute_async_event(struct nvmet_req *req)mutex_lock (&ctrl->lock);if (ctrl->nr_async_event_cmds >=NVMET_ASYNC_EVENTS ) {mutex_unlock (&ctrl->lock); -nvmet_req_complete (req,NVME_SC_ASYNC_LIMIT |NVME_SC_DNR ); +nvmet_req_complete (req,NVME_SC_ASYNC_LIMIT |NVME_STATUS_DNR );return ; } ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
@@ -975,5 +975,5 @@ u16 nvmet_parse_admin_cmd(struct nvmet_req *req)pr_debug ("unhandled cmd %d on qid %d\n" , cmd->common.opcode, req->sq->qid); req->error_loc = offsetof(struct nvme_common_command , opcode); -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; }
drivers/nvme/target/core.c +21 -21
160 lines not on this page · raw message
drivers/nvme/target/discovery.c +7 -7
@@ -177,7 +177,7 @@ static void nvmet_execute_disc_get_log_page(struct nvmet_req *req)if (req->cmd->get_log_page.lid !=NVME_LOG_DISC ) { req->error_loc = offsetof(struct nvme_get_log_page_command , lid); - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; goto out; }
@@ -185,7 +185,7 @@ static void nvmet_execute_disc_get_log_page(struct nvmet_req *req)if (offset &0x3 ) { req->error_loc = offsetof(struct nvme_get_log_page_command , lpo); - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; goto out; }
@@ -251,7 +251,7 @@ static void nvmet_execute_disc_identify(struct nvmet_req *req)if (req->cmd->identify.cns !=NVME_ID_CNS_CTRL ) { req->error_loc = offsetof(struct nvme_identify , cns); - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; goto out; }
@@ -314,7 +314,7 @@ static void nvmet_execute_disc_set_features(struct nvmet_req *req)default : req->error_loc = offsetof(struct nvme_common_command , cdw10); - stat =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + stat =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ;break ; }
@@ -339,7 +339,7 @@ static void nvmet_execute_disc_get_features(struct nvmet_req *req)default : req->error_loc = offsetof(struct nvme_common_command , cdw10); - stat =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + stat =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ;break ; }
@@ -355,7 +355,7 @@ u16 nvmet_parse_discovery_cmd(struct nvmet_req *req) cmd->common.opcode); req->error_loc = offsetof(struct nvme_common_command , opcode); -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; }switch (cmd->common.opcode) {
@@ -380,7 +380,7 @@ u16 nvmet_parse_discovery_cmd(struct nvmet_req *req)default :pr_err ("unhandled cmd %d\n" , cmd->common.opcode); req->error_loc = offsetof(struct nvme_common_command , opcode); -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; } }
drivers/nvme/target/fabrics-cmd.c +15 -15
@@ -18,7 +18,7 @@ static void nvmet_execute_prop_set(struct nvmet_req *req)if (req->cmd->prop_set.attrib &1 ) { req->error_loc = offsetof(struct nvmf_property_set_command , attrib); - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; goto out; }
@@ -29,7 +29,7 @@ static void nvmet_execute_prop_set(struct nvmet_req *req)default : req->error_loc = offsetof(struct nvmf_property_set_command , offset); - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; } out:nvmet_req_complete (req, status);
@@ -50,7 +50,7 @@ static void nvmet_execute_prop_get(struct nvmet_req *req) val = ctrl->cap;break ;default : - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ;break ; } }else {
@@ -65,7 +65,7 @@ static void nvmet_execute_prop_get(struct nvmet_req *req) val = ctrl->csts;break ;default : - status =NVME_SC_INVALID_FIELD |NVME_SC_DNR ; + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ;break ; } }
@@ -97,7 +97,7 @@ u16 nvmet_parse_fabrics_cmd(struct nvmet_req *req)pr_err ("received unknown capsule type 0x%x\n" , cmd->fabrics.fctype); req->error_loc = offsetof(struct nvmf_common_command , fctype); -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; }return 0 ;
@@ -115,13 +115,13 @@ static u16 nvmet_install_queue(struct nvmet_ctrl *ctrl, struct nvmet_req *req)if (old) {pr_warn ("queue already connected!\n" ); req->error_loc = offsetof(struct nvmf_connect_command , opcode); -return NVME_SC_CONNECT_CTRL_BUSY |NVME_SC_DNR ; +return NVME_SC_CONNECT_CTRL_BUSY |NVME_STATUS_DNR ; }if (!sqsize) {pr_warn ("queue size zero!\n" ); req->error_loc = offsetof(struct nvmf_connect_command , sqsize); req->cqe->result.u32 =IPO_IATTR_CONNECT_SQE (sqsize); - ret =NVME_SC_CONNECT_INVALID_PARAM |NVME_SC_DNR ; + ret =NVME_SC_CONNECT_INVALID_PARAM |NVME_STATUS_DNR ; goto err; }
@@ -177,14 +177,14 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)pr_warn ("invalid connect version (%d).\n" ,le16_to_cpu (c->recfmt)); req->error_loc = offsetof(struct nvmf_connect_command , recfmt); - status =NVME_SC_CONNECT_FORMAT |NVME_SC_DNR ; + status =NVME_SC_CONNECT_FORMAT |NVME_STATUS_DNR ; goto out; }if (unlikely (d->cntlid !=cpu_to_le16 (0xffff ))) {pr_warn ("connect attempt for invalid controller ID %#x\n" , d->cntlid); - status =NVME_SC_CONNECT_INVALID_PARAM |NVME_SC_DNR ; + status =NVME_SC_CONNECT_INVALID_PARAM |NVME_STATUS_DNR ; req->cqe->result.u32 =IPO_IATTR_CONNECT_DATA (cntlid); goto out; }
@@ -194,7 +194,7 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req) status =nvmet_alloc_ctrl (d->subsysnqn, d->hostnqn, req,le32_to_cpu (c->kato), &ctrl);if (status) { -if (status == (NVME_SC_INVALID_FIELD |NVME_SC_DNR )) +if (status == (NVME_SC_INVALID_FIELD |NVME_STATUS_DNR )) req->error_loc = offsetof(struct nvme_common_command , opcode); goto out;
@@ -248,7 +248,7 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)if (c->recfmt !=0 ) {pr_warn ("invalid connect version (%d).\n" ,le16_to_cpu (c->recfmt)); - status =NVME_SC_CONNECT_FORMAT |NVME_SC_DNR ; + status =NVME_SC_CONNECT_FORMAT |NVME_STATUS_DNR ; goto out; }
@@ -257,13 +257,13 @@ static void nvmet_execute_io_connect(struct nvmet_req *req) ctrl =nvmet_ctrl_find_get (d->subsysnqn, d->hostnqn,le16_to_cpu (d->cntlid), req);if (!ctrl) { - status =NVME_SC_CONNECT_INVALID_PARAM |NVME_SC_DNR ; + status =NVME_SC_CONNECT_INVALID_PARAM |NVME_STATUS_DNR ; goto out; }if (unlikely (qid > ctrl->subsys->max_qid)) {pr_warn ("invalid queue id (%d)\n" , qid); - status =NVME_SC_CONNECT_INVALID_PARAM |NVME_SC_DNR ; + status =NVME_SC_CONNECT_INVALID_PARAM |NVME_STATUS_DNR ; req->cqe->result.u32 =IPO_IATTR_CONNECT_SQE (qid); goto out_ctrl_put; }
@@ -296,13 +296,13 @@ u16 nvmet_parse_connect_cmd(struct nvmet_req *req)pr_err ("invalid command 0x%x on unconnected queue.\n" , cmd->fabrics.opcode); req->error_loc = offsetof(struct nvme_common_command , opcode); -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; }if (cmd->fabrics.fctype != nvme_fabrics_type_connect) {pr_err ("invalid capsule type 0x%x on unconnected queue.\n" , cmd->fabrics.fctype); req->error_loc = offsetof(struct nvmf_common_command , fctype); -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; }if (cmd->connect.qid ==0 )
drivers/nvme/target/io-cmd-bdev.c +7 -7
@@ -116,11 +116,11 @@ static u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)*/ switch (blk_sts) {case BLK_STS_NOSPC : - status =NVME_SC_CAP_EXCEEDED |NVME_SC_DNR ; + status =NVME_SC_CAP_EXCEEDED |NVME_STATUS_DNR ; req->error_loc = offsetof(struct nvme_rw_command , length);break ;case BLK_STS_TARGET : - status =NVME_SC_LBA_RANGE |NVME_SC_DNR ; + status =NVME_SC_LBA_RANGE |NVME_STATUS_DNR ; req->error_loc = offsetof(struct nvme_rw_command , slba);break ;case BLK_STS_NOTSUPP :
@@ -128,10 +128,10 @@ static u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)switch (req->cmd->common.opcode) {case nvme_cmd_dsm:case nvme_cmd_write_zeroes: - status =NVME_SC_ONCS_NOT_SUPPORTED |NVME_SC_DNR ; + status =NVME_SC_ONCS_NOT_SUPPORTED |NVME_STATUS_DNR ;break ;default : - status =NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; + status =NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; }break ;case BLK_STS_MEDIUM :
@@ -140,7 +140,7 @@ static u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)break ;case BLK_STS_IOERR :default : - status =NVME_SC_INTERNAL |NVME_SC_DNR ; + status =NVME_SC_INTERNAL |NVME_STATUS_DNR ; req->error_loc = offsetof(struct nvme_common_command , opcode); }
@@ -333,7 +333,7 @@ static void nvmet_bdev_execute_flush(struct nvmet_req *req)u16 nvmet_bdev_flush (struct nvmet_req *req) {if (blkdev_issue_flush (req->ns->bdev,GFP_KERNEL )) -return NVME_SC_INTERNAL |NVME_SC_DNR ; +return NVME_SC_INTERNAL |NVME_STATUS_DNR ;return 0 ; }
@@ -452,6 +452,6 @@ u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req)pr_err ("unhandled cmd %d on qid %d\n" , cmd->common.opcode, req->sq->qid); req->error_loc = offsetof(struct nvme_common_command , opcode); -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; } }
drivers/nvme/target/io-cmd-file.c +1 -1
@@ -407,6 +407,6 @@ u16 nvmet_file_parse_io_cmd(struct nvmet_req *req)pr_err ("unhandled cmd for file ns %d on qid %d\n" , cmd->common.opcode, req->sq->qid); req->error_loc = offsetof(struct nvme_common_command , opcode); -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; } }
drivers/nvme/target/passthru.c +6 -6
@@ -237,7 +237,7 @@ static void nvmet_passthru_execute_cmd(struct nvmet_req *req) ns =nvme_find_get_ns (ctrl, nsid);if (unlikely (!ns)) {pr_err ("failed to get passthru ns nsid:%u\n" , nsid); - status =NVME_SC_INVALID_NS |NVME_SC_DNR ; + status =NVME_SC_INVALID_NS |NVME_STATUS_DNR ; goto out; }
@@ -350,7 +350,7 @@ u16 nvmet_parse_passthru_io_cmd(struct nvmet_req *req)* emulated in the future if regular targets grow support for * this feature. */ -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; }return nvmet_setup_passthru_command (req);
@@ -402,7 +402,7 @@ static u16 nvmet_passthru_get_set_features(struct nvmet_req *req)case NVME_FEAT_RESV_PERSIST :/* No reservations, see nvmet_parse_passthru_io_cmd() */ default : -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; } }
@@ -470,7 +470,7 @@ u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req) req->p.use_workqueue = true;return NVME_SC_SUCCESS ; } -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ;case NVME_ID_CNS_NS : req->execute = nvmet_passthru_execute_cmd; req->p.use_workqueue = true;
@@ -482,7 +482,7 @@ u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req) req->p.use_workqueue = true;return NVME_SC_SUCCESS ; } -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ;default :return nvmet_setup_passthru_command (req); }
@@ -490,7 +490,7 @@ u16 nvmet_parse_passthru_admin_cmd(struct nvmet_req *req)return nvmet_setup_passthru_command (req);default :/* Reject commands not in the allowlist above */ -return NVME_SC_INVALID_OPCODE |NVME_SC_DNR ; +return NVME_SC_INVALID_OPCODE |NVME_STATUS_DNR ; } }
drivers/nvme/target/rdma.c +5 -5
@@ -850,12 +850,12 @@ static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp)if (!nvme_is_write (rsp->req.cmd)) { rsp->req.error_loc = offsetof(struct nvme_common_command , opcode); -return NVME_SC_INVALID_FIELD |NVME_SC_DNR ; +return NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; }if (off + len > rsp->queue->dev->inline_data_size) {pr_err ("invalid inline data offset!\n" ); -return NVME_SC_SGL_INVALID_OFFSET |NVME_SC_DNR ; +return NVME_SC_SGL_INVALID_OFFSET |NVME_STATUS_DNR ; }/* no data command? */
@@ -919,7 +919,7 @@ static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp)pr_err ("invalid SGL subtype: %#x\n" , sgl->type); rsp->req.error_loc = offsetof(struct nvme_common_command , dptr); -return NVME_SC_INVALID_FIELD |NVME_SC_DNR ; +return NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; }case NVME_KEY_SGL_FMT_DATA_DESC :switch (sgl->type &0xf ) {
@@ -931,12 +931,12 @@ static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp)pr_err ("invalid SGL subtype: %#x\n" , sgl->type); rsp->req.error_loc = offsetof(struct nvme_common_command , dptr); -return NVME_SC_INVALID_FIELD |NVME_SC_DNR ; +return NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; }default :pr_err ("invalid SGL type: %#x\n" , sgl->type); rsp->req.error_loc = offsetof(struct nvme_common_command , dptr); -return NVME_SC_SGL_INVALID_TYPE |NVME_SC_DNR ; +return NVME_SC_SGL_INVALID_TYPE |NVME_STATUS_DNR ; } }
drivers/nvme/target/tcp.c +2 -2
@@ -372,10 +372,10 @@ static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd)if (sgl->type == ((NVME_SGL_FMT_DATA_DESC <<4 ) |NVME_SGL_FMT_OFFSET )) {if (!nvme_is_write (cmd->req.cmd)) -return NVME_SC_INVALID_FIELD |NVME_SC_DNR ; +return NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ;if (len > cmd->req.port->inline_data_size) -return NVME_SC_SGL_INVALID_OFFSET |NVME_SC_DNR ; +return NVME_SC_SGL_INVALID_OFFSET |NVME_STATUS_DNR ; cmd->pdu_len = len; } cmd->req.transfer_len += len;
include/linux/nvme.h +2 -2
@@ -1591,8 +1591,8 @@ enum {NVME_SCT_MASK =0x0700 ,/* Status Code Type */ NVME_SCT_SC_MASK =NVME_SCT_MASK |NVME_SC_MASK , -NVME_SC_CRD =0x1800 ,/* Command Retry Delayed */ - NVME_SC_DNR = 0x4000, /* Do Not Retry */ +NVME_STATUS_CRD =0x1800 ,/* Command Retry Delayed */ + NVME_STATUS_DNR = 0x4000, /* Do Not Retry */ };#define NVME_SCT (status) ((status) >> 8 & 7)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 nvmet: fix pre-auth out-of-bounds heap read in Discovery Get Log Page
In reply to Sasha LevinFrom: Bryam Vargas <[email protected]>
[ Upstream commit 53cd102a7a56079b11b897835bd9b94c14e6322c ]
nvmet_execute_disc_get_log_page() validates only the dword alignment of the host-supplied Log Page Offset (lpo). The 64-bit offset is then added to a small kzalloc'd buffer that holds the discovery log page and the result is passed straight to nvmet_copy_to_sgl(), which memcpy()s data_len bytes out to the host with no source-side bound check:
u64 offset = nvmet_get_log_page_offset(req->cmd); /* 64-bit host */
size_t data_len = nvmet_get_log_page_len(req->cmd); /* 32-bit host */
...
if (offset & 0x3) { ... } /* only check */
...
alloc_len = sizeof(*hdr) + entry_size * discovery_log_entries(req);
buffer = kzalloc(alloc_len, GFP_KERNEL);
...
status = nvmet_copy_to_sgl(req, 0, buffer + offset, data_len);The Discovery controller is unauthenticated -- nvmet_host_allowed() returns true unconditionally for the discovery subsystem -- so the call is reachable pre-authentication by any TCP/RDMA/FC peer that can reach the nvmet target. With a discovery log page of ~1 KiB, an attacker requesting up to 4 KiB starting at offset == alloc_len reads the next slab page out and gets its content returned over the fabric (an empirical run on a default nvmet-tcp loopback target leaked 81 canonical kernel pointers in one Get Log Page response). Pointing the offset at unmapped kernel memory faults the in-kernel memcpy and crashes (or panics, on panic_on_oops=1) the target host instead.
The attacker-controlled source-side offset pattern "nvmet_copy_to_sgl(req, 0, buffer + ATTACKER_OFFSET, ...)" is unique to nvmet_execute_disc_get_log_page in the entire nvmet codebase: every other Get Log Page handler in admin-cmd.c either ignores lpo (and silently starts every response at offset 0) or tracks a local destination offset with a fixed source pointer.
Validate the host-supplied offset against the log page size, cap the copy length to what is actually available, and zero-fill any remainder of the host transfer buffer. The zero-fill matches the existing short-response pattern in nvmet_execute_get_log_changed_ns() (admin-cmd.c) and prevents leaking transport SGL contents when the host asks for more bytes than the log page contains.
--- drivers/nvme/target/discovery.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/nvme/target/discovery.c +22 -1
@@ -165,6 +165,7 @@ static void nvmet_execute_disc_get_log_page(struct nvmet_req *req)u64 offset =nvmet_get_log_page_offset (req->cmd);size_t data_len =nvmet_get_log_page_len (req->cmd);size_t alloc_len; +size_t copy_len;struct nvmet_subsys_link *p;struct nvmet_port *r;u32 numrec =0 ;
@@ -233,7 +234,27 @@ static void nvmet_execute_disc_get_log_page(struct nvmet_req *req)up_read (&nvmet_config_sem); - status =nvmet_copy_to_sgl (req,0 , buffer + offset, data_len); +/* +* Validate the host-supplied log page offset before copying out. +* Without this check, the host controls a 64-bit byte offset into +* a small kzalloc'd buffer: a value past the log page lets the +* subsequent memcpy read adjacent kernel heap, and a value aimed +* at unmapped kernel memory faults the in-kernel copy and crashes +* the target host. The Discovery controller is unauthenticated, +* so the bug is reachable from any reachable fabric peer. +*/ +if (offset > alloc_len) { + req->error_loc = + offsetof(struct nvme_get_log_page_command , lpo); + status =NVME_SC_INVALID_FIELD |NVME_STATUS_DNR ; + goto out_free_buffer; + } + + copy_len =min_t (size_t, data_len, alloc_len - offset); + status =nvmet_copy_to_sgl (req,0 , buffer + offset, copy_len); +if (!status && copy_len < data_len) + status =nvmet_zero_sgl (req, copy_len, data_len - copy_len); +out_free_buffer:kfree (buffer); out:nvmet_req_complete (req, status);
Signature · 1 line
2.53.0