* [PATCH v4] nvme: nvme_identify_ns_descs: prevent oob
@ 2025-12-02 18:22 Eugene Korenevsky
2025-12-03 6:10 ` Christoph Hellwig
2025-12-04 16:09 ` Keith Busch
0 siblings, 2 replies; 3+ messages in thread
From: Eugene Korenevsky @ 2025-12-02 18:22 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
linux-nvme, linux-kernel
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 checking (header size + .nidl) against
remaining buffer length.
Signed-off-by: Eugene Korenevsky <ekorenevsky@aliyun.com>
---
v1->v2:
* Simplification: do not touch nvme_process_ns_desc()
* Update commit description
v2->v3:
* Even more simplification
* Replace while with do-while as first pre-loop condition
check is pointless
* Change `pos` type: int -> size_t
* Update commit description
v3->v4:
* Replace do-while with for. A bit exceeded 80 hard limit but still in
100 soft limit
* Restore `pos` type. This is subject of another patch if we prefer
minimalism
---
drivers/nvme/host/core.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index f1f719351f3f..253d35937f03 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -1540,6 +1540,7 @@ static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl,
bool csi_seen = false;
int status, pos, len;
void *data;
+ struct nvme_ns_id_desc *cur;
if (ctrl->vs < NVME_VS(1, 3, 0) && !nvme_multi_css(ctrl))
return 0;
@@ -1563,11 +1564,14 @@ 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;
+ for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE - sizeof(*cur); pos += len) {
+ cur = data + pos;
if (cur->nidl == 0)
break;
+ /* check ns id desc does not exceed remaining buffer by size */
+ if (cur->nidl + sizeof(*cur) > NVME_IDENTIFY_DATA_SIZE - pos)
+ break;
len = nvme_process_ns_desc(ctrl, &info->ids, cur, &csi_seen);
if (len < 0)
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v4] nvme: nvme_identify_ns_descs: prevent oob
2025-12-02 18:22 [PATCH v4] nvme: nvme_identify_ns_descs: prevent oob Eugene Korenevsky
@ 2025-12-03 6:10 ` Christoph Hellwig
2025-12-04 16:09 ` Keith Busch
1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2025-12-03 6:10 UTC (permalink / raw)
To: Eugene Korenevsky
Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
linux-nvme, linux-kernel
On Tue, Dec 02, 2025 at 09:22:13PM +0300, Eugene Korenevsky wrote:
> @@ -1563,11 +1564,14 @@ 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;
> + for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE - sizeof(*cur); pos += len) {
Please avoid the overly long line.
Otherwise this looks fine.
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v4] nvme: nvme_identify_ns_descs: prevent oob
2025-12-02 18:22 [PATCH v4] nvme: nvme_identify_ns_descs: prevent oob Eugene Korenevsky
2025-12-03 6:10 ` Christoph Hellwig
@ 2025-12-04 16:09 ` Keith Busch
1 sibling, 0 replies; 3+ messages in thread
From: Keith Busch @ 2025-12-04 16:09 UTC (permalink / raw)
To: Eugene Korenevsky
Cc: Jens Axboe, Christoph Hellwig, Sagi Grimberg, linux-nvme,
linux-kernel
On Tue, Dec 02, 2025 at 09:22:13PM +0300, Eugene Korenevsky wrote:
> 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 checking (header size + .nidl) against
> remaining buffer length.
Thanks, applied to nvme-6.19 with the line length wrap fixed up.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-12-04 16:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-02 18:22 [PATCH v4] nvme: nvme_identify_ns_descs: prevent oob Eugene Korenevsky
2025-12-03 6:10 ` Christoph Hellwig
2025-12-04 16:09 ` Keith Busch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox