Skip to contentSkip to conversation

NVMe: Add a buffer length parameter to struct nvme_user_io. Check buffer length in nvme_submit_io to avoid buffer overflow.

PATCH New David Darrington 3 participants 5 messages Nov 4, 2013

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

PATCH 1/1 NVMe: Add a buffer length parameter to struct nvme_user_io. Check buffer length in nvme_submit_io to avoid buffer overflow.

Added a buffer length parameter to struct nvme_user_io so that nvme_submit_io can prevent writing past the end of the user buffer.

Signed-off-by David Darrington <david.darrington at hgst.com>
---
 drivers/block/nvme-core.c |    6 +++++-
 include/uapi/linux/nvme.h |    4 +++-
 2 files changed, 8 insertions(+), 2 deletions(-)
drivers/block/nvme-core.c +5 -1
index da52092..2aa4346 100644
@@ -1381,6 +1381,10 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
 	if (copy_from_user(&io, uio, sizeof(io)))
 		return -EFAULT;
 	length = (io.nblocks + 1) << ns->lba_shift;
+
+	if (io.dxfer_len < length)
+		return -EINVAL;
+
 	meta_len = (io.nblocks + 1) * ns->ms;
 
 	if (meta_len && ((io.metadata & 3) || !io.metadata))
@@ -1390,7 +1394,7 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
 	case nvme_cmd_write:
 	case nvme_cmd_read:
 	case nvme_cmd_compare:
-		iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
+		iod = nvme_map_user_pages(dev, io.opcode & 1, io.dxferp, length);
 		break;
 	default:
 		return -EINVAL;
include/uapi/linux/nvme.h +3 -1
index 989c04e..40b5b52 100644
@@ -441,7 +441,9 @@ struct nvme_user_io {
 	__u16	nblocks;
 	__u16	rsvd;
 	__u64	metadata;
-	__u64	addr;
+	__u32	rsvd1;
+	__u32	dxfer_len;	/* length of data xfer buffer */
+	__u64	dxferp;		/* pointer to data xfer buffer */
 	__u64	slba;
 	__u32	dsmgmt;
 	__u32	reftag;
Signature · 1 line
1.7.1
#1 lore raw

PATCH 1/1 NVMe: Add a buffer length parameter to struct nvme_user_io. Check buffer length in nvme_submit_io to avoid buffer overflow.

Quoted 2 lines · David Darrington

Added a buffer length parameter to struct nvme_user_io so that nvme_submit_io can prevent writing past the end of the user buffer.

This extra check seems redundant. Doesn't get_user_pages_fast already fail if the user buffer is too small?

Quoted 55 lines
Signed-off-by David Darrington <david.darrington at hgst.com>

---

drivers/block/nvme-core.c |    6 +++++-
include/uapi/linux/nvme.h |    4 +++-
2 files changed, 8 insertions(+), 2 deletions(-)
drivers/block/nvme-core.c +5 -1
index da52092..2aa4346 100644
@@ -1381,6 +1381,10 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
 	if (copy_from_user(&io, uio, sizeof(io)))
 		return -EFAULT;
 	length = (io.nblocks + 1) << ns->lba_shift;
+
+	if (io.dxfer_len < length)
+		return -EINVAL;
+
 	meta_len = (io.nblocks + 1) * ns->ms;
 
 	if (meta_len && ((io.metadata & 3) || !io.metadata))
@@ -1390,7 +1394,7 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
 	case nvme_cmd_write:
 	case nvme_cmd_read:
 	case nvme_cmd_compare:
-		iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
+		iod = nvme_map_user_pages(dev, io.opcode & 1, io.dxferp, length);
 		break;
 	default:
 		return -EINVAL;
include/uapi/linux/nvme.h +3 -1
index 989c04e..40b5b52 100644
@@ -441,7 +441,9 @@ struct nvme_user_io {
 	__u16	nblocks;
 	__u16	rsvd;
 	__u64	metadata;
-	__u64	addr;
+	__u32	rsvd1;
+	__u32	dxfer_len;	/* length of data xfer buffer */
+	__u64	dxferp;		/* pointer to data xfer buffer */
 	__u64	slba;
 	__u32	dsmgmt;
 	__u32	reftag;
Signature · 7 lines
1.7.1


_______________________________________________
Linux-nvme mailing list
Linux-nvme at lists.infradead.org
http://merlin.infradead.org/mailman/listinfo/linux-nvme
#2 lore raw

PATCH 1/1 NVMe: Add a buffer length parameter to struct nvme_user_io. Check buffer length in nvme_submit_io to avoid buffer overflow.

The patch was for a reported problem. I have a testcase that segfaults after the user buffer is over written. I created a request to read 1 LBA, and passed in a buffer of less than 512 bytes. The driver wrote past the buffer, corrupting storage leading to the segfault. There is a similar param and length check in the sg ioctl path. Look near the end of nvme_trans_io. In my case, the buffer is allocated on the stack, as a local variable. that might explain why get_user_pages does not catch the mistake.

"Linux-nvme" <linux-nvme-bounces at lists.infradead.org> wrote on 11/04/2013 11:27:29 AM:

Quoted 30 lines · David Darrington
Quoted 3 lines

Added a buffer length parameter to struct nvme_user_io so that nvme_submit_io can prevent writing past the end of the user buffer.

This extra check seems redundant. Doesn't get_user_pages_fast already fail if the user buffer is too small?

drivers/block/nvme-core.c static int nvme_submit_io(struct nvme_ns

Quoted 12 lines
Signed-off-by David Darrington <david.darrington at hgst.com>

---

drivers/block/nvme-core.c |    6 +++++-
include/uapi/linux/nvme.h |    4 +++-
2 files changed, 8 insertions(+), 2 deletions(-)
drivers/block/nvme-core.c +0 -0
index da52092..2aa4346 100644
@@ -1381,6 +1381,10 @@ static int nvme_submit_io(struct nvme_ns 

*ns, struct nvme_user_io __user *uio)

Quoted 9 lines
drivers/block/nvme-core.c +4 -0
   if (copy_from_user(&io, uio, sizeof(io)))
      return -EFAULT;
   length = (io.nblocks + 1) << ns->lba_shift;
+
+   if (io.dxfer_len < length)
+      return -EINVAL;
+
   meta_len = (io.nblocks + 1) * ns->ms;
 
Quoted 8 lines

drivers/block/nvme-core.c static int nvme_submit_io(struct nvme_ns

Quoted 2 lines
drivers/block/nvme-core.c +0 -0
   if (meta_len && ((io.metadata & 3) || !io.metadata))
@@ -1390,7 +1394,7 @@ static int nvme_submit_io(struct nvme_ns 

*ns, struct nvme_user_io __user *uio)

Quoted 5 lines
drivers/block/nvme-core.c +1 -1
   case nvme_cmd_write:
   case nvme_cmd_read:
   case nvme_cmd_compare:
-      iod = nvme_map_user_pages(dev, io.opcode & 1, io.addr, length);
+      iod = nvme_map_user_pages(dev, io.opcode & 1, io.dxferp, 

length);

Quoted 32 lines
Quoted 27 lines
drivers/block/nvme-core.c +0 -0
      break;
   default:
      return -EINVAL;
include/uapi/linux/nvme.h +3 -1
index 989c04e..40b5b52 100644
@@ -441,7 +441,9 @@ struct nvme_user_io {
   __u16   nblocks;
   __u16   rsvd;
   __u64   metadata;
-   __u64   addr;
+   __u32   rsvd1;
+   __u32   dxfer_len;   /* length of data xfer buffer */
+   __u64   dxferp;      /* pointer to data xfer buffer */
   __u64   slba;
   __u32   dsmgmt;
   __u32   reftag;
Signature · 7 lines
1.7.1


_______________________________________________
Linux-nvme mailing list
Linux-nvme at lists.infradead.org
http://merlin.infradead.org/mailman/listinfo/linux-nvme

_______________________________________________
Linux-nvme mailing list
Linux-nvme at lists.infradead.org
http://merlin.infradead.org/mailman/listinfo/linux-nvme

#3 lore raw

PATCH 1/1 NVMe: Add a buffer length parameter to struct nvme_user_io. Check buffer length in nvme_submit_io to avoid buffer overflow.

In reply to David Darrington

include/uapi/linux/nvme.h struct nvme_user_io { · line 449

Quoted 11 lines · David Darrington
include/uapi/linux/nvme.h +3 -1
@@ -441,7 +441,9 @@ struct nvme_user_io {
 	__u16	nblocks;
 	__u16	rsvd;
 	__u64	metadata;
-	__u64	addr;
+	__u32	rsvd1;
+	__u32	dxfer_len;	/* length of data xfer buffer */
+	__u64	dxferp;		/* pointer to data xfer buffer */
 	__u64	slba;
 	__u32	dsmgmt;
 	__u32	reftag;

You can't just change the size of nvme_user_io; that breaks the ABI. We'd need a _V2 version of the ioctl or something if we actually need to change it.

#4 lore raw

PATCH 1/1 NVMe: Add a buffer length parameter to struct nvme_user_io. Check buffer length in nvme_submit_io to avoid buffer overflow.

So we have these choices:

1. leave the code as is, with a known buffer overflow problem.
2. create a _V2 version of the ioctl (in which case the v1 version still has the issue)
3. Fix the problem in some other way.

Perhaps 1) is not as bad as it sounds. Even if we fix the problem, a pgm could pass in the wrong length and get the same result.

The SG_IO ioctl does include a length, so at least the fix is consistent.

Matthew Wilcox <willy at linux.intel.com>
Sent by: "Linux-nvme" <linux-nvme-bounces at lists.infradead.org> 11/04/2013 12:26 PM

To
David Darrington <david.darrington at hgst.com>
cc
linux-nvme at lists.infradead.org
Subject
Re: [PATCH] NVMe: Add a buffer length parameter to struct nvme_user_io. Check buffer length in nvme_submit_io to avoid buffer overflow.

include/uapi/linux/nvme.h struct nvme_user_io { · line 445

Quoted 7 lines · David Darrington
include/uapi/linux/nvme.h +2 -1
@@ -441,7 +441,9 @@ struct nvme_user_io {
               __u16           nblocks;
               __u16           rsvd;
               __u64           metadata;
-              __u64           addr;
+              __u32           rsvd1;
+              __u32           dxfer_len;              /* length of data 

xfer buffer */

include/uapi/linux/nvme.h struct nvme_user_io { · line 449

Quoted 4 lines
include/uapi/linux/nvme.h +1 -0
+              __u64           dxferp; /* pointer to data xfer buffer */
               __u64           slba;
               __u32           dsmgmt;
               __u32           reftag;

You can't just change the size of nvme_user_io; that breaks the ABI. We'd need a _V2 version of the ioctl or something if we actually need to change it.

_______________________________________________
Linux-nvme mailing list
Linux-nvme at lists.infradead.org
http://merlin.infradead.org/mailman/listinfo/linux-nvme