Skip to contentSkip to conversation

nvme: sanitize metadata bounce buffer for reads

PATCH Superseded Keith Busch 3 participants 5 messages Oct 16, 2023 NVMe

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme: sanitize metadata bounce buffer for reads

From: Keith Busch <[email protected]>

User can request more metadata bytes than the device will write. Ensure kernel buffer is initialized so we're not leaking unsanitized memory on the copy-out.

Fixes 0b7f1f26f95a51a ("nvme: use the block layer for userspace passthrough metadata")
---
 drivers/nvme/host/ioctl.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nvme/host/ioctl.c +2 -0
index 788b36e7915ab..154eb7c944e76 100644
@@ -39,6 +39,8 @@ static void *nvme_add_user_metadata(struct request *req, void __user *ubuf,
 	ret = -EFAULT;
 	if ((req_op(req) == REQ_OP_DRV_OUT) && copy_from_user(buf, ubuf, len))
 		goto out_free_meta;
+	else
+		memset(buf, 0, len);
 
 	bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
 	if (IS_ERR(bip)) {
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 17 lines · Keith Busch

From: Keith Busch <[email protected]>

User can request more metadata bytes than the device will write. Ensure kernel buffer is initialized so we're not leaking unsanitized memory on the copy-out.

Fixes 0b7f1f26f95a51a ("nvme: use the block layer for userspace passthrough metadata")
---
 drivers/nvme/host/ioctl.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nvme/host/ioctl.c +0 -0
index 788b36e7915ab..154eb7c944e76 100644

drivers/nvme/host/ioctl.c static void *nvme_add_user_metadata(struct request *req, void __user *ubuf, · line 43

Quoted 5 lines
drivers/nvme/host/ioctl.c +2 -0
@@ -39,6 +39,8 @@ static void *nvme_add_user_metadata(struct request *req, void __user *ubuf,
 	ret = -EFAULT;
 	if ((req_op(req) == REQ_OP_DRV_OUT) && copy_from_user(buf, ubuf, len))
 		goto out_free_meta;
+	else
+		memset(buf, 0, len);

Do you need this else?

Signature · 1 line
Jens Axboe
maintainer
#2 lore raw
Quoted 7 lines · Jens Axboe

drivers/nvme/host/ioctl.c static void *nvme_add_user_metadata(struct request *req, void __user *ubuf, · line 43

Quoted 4 lines · Keith Busch
drivers/nvme/host/ioctl.c +2 -0
 	if ((req_op(req) == REQ_OP_DRV_OUT) && copy_from_user(buf, ubuf, len))
 		goto out_free_meta;
+	else
+		memset(buf, 0, len);

Do you need this else?

The 'if' condition just above copies user data into the 'buf', so yes, we need the else to separate how buf is initialized for each direction.

maintainer
#3 lore raw
Quoted 11 lines · Keith Busch
Quoted 7 lines · Jens Axboe

drivers/nvme/host/ioctl.c static void *nvme_add_user_metadata(struct request *req, void __user *ubuf, · line 43

Quoted 4 lines · Keith Busch
drivers/nvme/host/ioctl.c +2 -0
 	if ((req_op(req) == REQ_OP_DRV_OUT) && copy_from_user(buf, ubuf, len))
 		goto out_free_meta;
+	else
+		memset(buf, 0, len);

Do you need this else?

The 'if' condition just above copies user data into the 'buf', so yes, we need the else to separate how buf is initialized for each direction.

It still looks wrong to me. If it's writing to the drive AND we fail copying in data, -EFAULT. But the opposite of that isn't that it's a read from device.

	if (req_op(req) == REQ_OP_DRV_OUT) {
		ret = -EFAULT;
		if (copy_from_user(buf, ubuf, len))
			goto out_free_meta;
	} else {
		memset(buf, 0, len);
	}

would look more readable, as it splits it on direction, rather than on direction and the status of the copy. And follows the normal idioms for erroring on the copy condition.

Signature · 1 line
Jens Axboe
maintainer
#4 lore raw
Quoted 12 lines · Jens Axboe

It still looks wrong to me. If it's writing to the drive AND we fail copying in data, -EFAULT. But the opposite of that isn't that it's a read from device.

	if (req_op(req) == REQ_OP_DRV_OUT) {
		ret = -EFAULT;
		if (copy_from_user(buf, ubuf, len))
			goto out_free_meta;
	} else {
		memset(buf, 0, len);
	}
Quoted 3 lines

would look more readable, as it splits it on direction, rather than on direction and the status of the copy. And follows the normal idioms for erroring on the copy condition.

Thanks! When you put it that way, the mistake was clear! Fixing for v2.