nvme: nvme_identify_ns_descs: prevent oob
PATCH v2 1/1 nvme: nvme_identify_ns_descs: prevent oob
Broken or malicious controller can send invalid ns id. Out-of-band memory access may occur if remaining buffer size is less than .nidl (ns id length) field of struct nvme_ns_id_desc
Fix this issue by counting remaining buffer length and checking .nidl against it.
---v1->v2:
* Simplification: do not touch nvme_process_ns_desc()
* Update commit description
--- drivers/nvme/host/core.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/nvme/host/core.c +9 -4
@@ -1538,7 +1538,8 @@ static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, {struct nvme_command c = { };bool csi_seen = false; -int status, pos, len; +int status, len, remain; +struct nvme_ns_id_desc *cur;void *data;if (ctrl->vs <NVME_VS (1 ,3 ,0 ) && !nvme_multi_css (ctrl))
@@ -1563,17 +1564,21 @@ static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, goto free_data; } -for (pos =0 ; pos <NVME_IDENTIFY_DATA_SIZE ; pos += len) { -struct nvme_ns_id_desc *cur = data + pos; - + remain =NVME_IDENTIFY_DATA_SIZE ; + cur = data; +while (remain >=sizeof (*cur)) {if (cur->nidl ==0 )break ; +if (sizeof (*cur) + cur->nidl > remain) +break ; len =nvme_process_ns_desc (ctrl, &info->ids, cur, &csi_seen);if (len <0 )break ; len +=sizeof (*cur); + remain -= len; + cur += len; }if (nvme_multi_css (ctrl) && !csi_seen) {
Signature · 1 line
2.47.3
Christoph Hellwig
maintainerdrivers/nvme/host/core.c static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, · line 1541
Quoted 2 lines · Eugene Korenevsky
drivers/nvme/host/core.c +1
-1-int status, pos, len; +int status, len, remain;
Pre-existing issue, but all but len here should be unsigned.
Quoted 6 lines
drivers/nvme/host/core.c +4
-0+ remain =NVME_IDENTIFY_DATA_SIZE ; + cur = data; +while (remain >=sizeof (*cur)) {if (cur->nidl ==0 )break ; +if (sizeof (*cur) + cur->nidl > remain)
drivers/nvme/host/core.c static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, · line 1582
Quoted 10 lines
drivers/nvme/host/core.c +3
-0+break ; len =nvme_process_ns_desc (ctrl, &info->ids, cur, &csi_seen);if (len <0 )break ; len +=sizeof (*cur); + remain -= len; + cur += len; }
I don't think this works, cur is a nvme_ns_id_desc pointer, so it will be increased by len multiplied by the size of it.
I think this would work muc heasier if you'd do away with remain, use and just use cur locally, and instead maintain an offet; Something like:
offset = 0;
do {
cur = data + offset;
...
offset += len;
} while (offset < NVME_IDENTIFY_DATA_SIZE - sizeof(*cur));