Skip to contentSkip to conversation

nvmet: don't return memory contents beyond valid data when reading ANA log page

PATCH Applied Ewan D. Milne 2 participants 3 messages Nov 4

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet: don't return memory contents beyond valid data when reading ANA log page

The host side does not know how much valid data will be returned by the target so it will typically provide a buffer larger than the amount of valid data in the ANA log page. The memory allocation used by the target does not guarantee that prior kernel memory contents will not be returned past the end of the valid page data. Fix this to ensure that the SGL is zeroed before copying the data.

Fixes 72efd25dcf4f ("nvmet: add minimal ANA support")
---
 drivers/nvme/target/admin-cmd.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/nvme/target/admin-cmd.c +9 -0
index 26028be8fe4d..bfd5750da27b 100644
@@ -543,6 +543,15 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
 	u16 ngrps = 0;
 	u16 status;
 
+	/*
+	 * The header and one descriptor per enabled group are the only bytes
+	 * written below; zero the whole transfer first so the unwritten tail
+	 * does not leak stale kernel memory to the host.
+	 */
+	status = nvmet_zero_sgl(req, 0, req->transfer_len);
+	if (status)
+		goto out;
+
 	status = NVME_SC_INTERNAL;
 	desc = kmalloc_flex(*desc, nsids, NVMET_MAX_NAMESPACES);
 	if (!desc)
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 20 lines · Ewan D. Milne

The host side does not know how much valid data will be returned by the target so it will typically provide a buffer larger than the amount of valid data in the ANA log page. The memory allocation used by the target does not guarantee that prior kernel memory contents will not be returned past the end of the valid page data. Fix this to ensure that the SGL is zeroed before copying the data.

Fixes 72efd25dcf4f ("nvmet: add minimal ANA support")
---
 drivers/nvme/target/admin-cmd.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/nvme/target/admin-cmd.c +0 -0
index 26028be8fe4d..bfd5750da27b 100644
@@ -543,6 +543,15 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
 	u16 ngrps = 0;
 	u16 status;

drivers/nvme/target/admin-cmd.c static void nvmet_execute_get_log_page_ana(struct nvmet_req *req) · line 554

Quoted 10 lines
drivers/nvme/target/admin-cmd.c +9 -0
 
+	/*
+	 * The header and one descriptor per enabled group are the only bytes
+	 * written below; zero the whole transfer first so the unwritten tail
+	 * does not leak stale kernel memory to the host.
+	 */
+	status = nvmet_zero_sgl(req, 0, req->transfer_len);
+	if (status)
+		goto out;
+

Just a minor optimization suggestion, it looks safe to move the zero'ing to after the code fills out the entries so that we're not writing over the memory twice. Something like this after the for-loop:
So

	if (!status && offset < req->transfer_len)
		nvmet_zero_sgl(req, offset, req->transfer_len - offset);
maintainer Applied
#2 lore raw
In reply to Ewan D. Milne
Quoted 8 lines · Ewan D. Milne

The host side does not know how much valid data will be returned by the target so it will typically provide a buffer larger than the amount of valid data in the ANA log page. The memory allocation used by the target does not guarantee that prior kernel memory contents will not be returned past the end of the valid page data. Fix this to ensure that the SGL is zeroed before copying the data.

[...]

Applied, thanks!

[1/1] nvmet: don't return memory contents beyond valid data when reading ANA log page

      commit: 29b8758b4700357909f06705a0c48c7a60c556d5

Best regards,

Signature · 1 line
Keith Busch <[email protected]>