NVMe: Do not take nsid while a passthrough IO command is being issued via a block device file descriptor
Subsystems (2)
PATCH 1/1 NVMe: Do not take nsid while a passthrough IO command is being issued via a block device file descriptor
When a passthrough IO command is issued with a specific block device file descriptor. It should be applied at the namespace which is associated with that block device file descriptor. This patch makes such passthrough command ingore nsid in nvme_passthru_cmd structure. Instead it takes the namespace ID asscoiated with the block device descriptor.
--- drivers/block/nvme-core.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/block/nvme-core.c +6 -6
@@ -1734,7 +1734,7 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)return status; } -static int nvme_user_cmd (struct nvme_dev *dev, +static int nvme_user_cmd (struct nvme_dev *dev,struct nvme_ns *ns,struct nvme_passthru_cmd __user *ucmd,bool ioq) {struct nvme_passthru_cmd cmd;
@@ -1751,7 +1751,7 @@ static int nvme_user_cmd(struct nvme_dev *dev,memset (&c,0 ,sizeof (c)); c.common.opcode = cmd.opcode; c.common.flags = cmd.flags; - c.common.nsid =cpu_to_le32 (cmd.nsid); + c.common.nsid = ns ?cpu_to_le32 (ns->ns_id) :cpu_to_le32 (cmd.nsid); c.common.cdw2[0 ] =cpu_to_le32 (cmd.cdw2); c.common.cdw2[1 ] =cpu_to_le32 (cmd.cdw3); c.common.cdw10[0 ] =cpu_to_le32 (cmd.cdw10);
@@ -1804,9 +1804,9 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,force_successful_syscall_return ();return ns->ns_id;case NVME_IOCTL_ADMIN_CMD : -return nvme_user_cmd (ns->dev, (void __user *)arg, false); +return nvme_user_cmd (ns->dev,NULL , (void __user *)arg, false);case NVME_IOCTL_IO_CMD : -return nvme_user_cmd (ns->dev, (void __user *)arg, true); +return nvme_user_cmd (ns->dev, ns, (void __user *)arg, true);case NVME_IOCTL_SUBMIT_IO :return nvme_submit_io (ns, (void __user *)arg);case SG_GET_VERSION_NUM :
@@ -2762,9 +2762,9 @@ static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)struct nvme_dev *dev = f->private_data;switch (cmd) {case NVME_IOCTL_ADMIN_CMD : -return nvme_user_cmd (dev, (void __user *)arg, false); +return nvme_user_cmd (dev,NULL , (void __user *)arg, false);case NVME_IOCTL_IO_CMD : -return nvme_user_cmd (dev, (void __user *)arg, true); +return nvme_user_cmd (dev,NULL , (void __user *)arg, true);default :return -ENOTTY ; }
Signature · 1 line
1.9.1
Quoted 6 lines · Yan Liu
When a passthrough IO command is issued with a specific block device file descriptor. It should be applied at the namespace which is associated with that block device file descriptor. This patch makes such passthrough command ingore nsid in
nvme_passthru_cmdstructure. Instead it takes the namespace ID asscoiated with the block device descriptor.Signed-off-by Yan Liu <yan at purestorage.com>
Oh it doesn't look like you tested this through the character handle. You've got it set to use the admin queue's request_queue for IO passthrough commands, so that can't be right.
The IOCTL's purpose was to let someone submit completely arbitrary commands on IO queues. This technically shouldn't even need a namespace handle, but we don't have a request_queue associated to IO queues without one like the admin queue has. In fact, we ought to fix that so we can issue IO commands without namespaces.
Anyway, namespaces may be hidden or some vendor special NSID trickery, who knows. Point is we don't want to tie the passthrough command's NSID down to the namespace providing the request_queue. If it is your intention to use that NSID, you can get the NSID using the NVME_IOCTL_ID prior to setting up the passthrough command.
Quoted 5 lines · Keith Busch
The IOCTL's purpose was to let someone submit completely arbitrary commands on IO queues. This technically shouldn't even need a namespace handle, but we don't have a
request_queueassociated to IO queues without one like the admin queue has. In fact, we ought to fix that so we can issue IO commands without namespaces.
Honestly, this sounds like a horrible idea. As namespaces aren't really any different from SCSI LUNs they should only be accessible through the device associated with the namespaces, and admin commands should only be allowed through the character device (if at all).
For these security and usability reasons we did get rid of the SG_FLAG_LUN_INHIBIT flag in the SCSI passthrough interface, which allowed for similar horrible things in the distant past.
Quoted 4 lines · Yan Liu
For IO passthrough command, it uses an IO queue associated with the device. Actually, this patch does not modify that part.
This patch is not really focused on io queues; instead, it is more about namespace protection from other namespace's user ios. The patch here doesn't prevent a user?submitting completely arbitrary?commands on IO queues. One still can do it through a char dev file descriptor. However, when a user?explicitly chooses a namespace's file descriptor, it is unlikely that she/he tries to issue an io command to a different namespace. Maybe in that case someone should to use a new admin command.
Oh, I just realized your patch is not for the blk-mq version of this driver. The mainline kernel uses blk-mq and is a little different in this path. My mistake, you're right the command works the same as before using the character device here. In that case, this seems very reasonable, but doesn't merge upstream.
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Keith Busch
The IOCTL's purpose was to let someone submit completely arbitrary commands on IO queues. This technically shouldn't even need a namespace handle, but we don't have a
request_queueassociated to IO queues without one like the admin queue has. In fact, we ought to fix that so we can issue IO commands without namespaces.Honestly, this sounds like a horrible idea. As namespaces aren't really any different from SCSI LUNs they should only be accessible through the device associated with the namespaces, and admin commands should only be allowed through the character device (if at all).
The case I considered was the "hidden" attribute in the NVMe LBA Range Type feature. It only indicates the storage should be hidden from the OS for general use, but the host may still use it for special purposes. In truth, the driver doesn't handle the hidden attribute very well and it doesn't seem like a well thought out feature in the spec anyway.
But if you really need to restrict namespace access, shouldn't that be enforced on the target side with reservations or similar mechanism?
I agree on your last point. Admin commands through namespaces carried over from before the management device existed, but removing it now will break some customer tooling. There's probably a responsible way to migrate.
Quoted 3 lines
For these security and usability reasons we did get rid of the
SG_FLAG_LUN_INHIBITflag in the SCSI passthrough interface, which allowed for similar horrible things in the distant past.
Quoted 5 lines · Keith Busch
The case I considered was the "hidden" attribute in the NVMe LBA Range Type feature. It only indicates the storage should be hidden from the OS for general use, but the host may still use it for special purposes. In truth, the driver doesn't handle the hidden attribute very well and it doesn't seem like a well thought out feature in the spec anyway.
At least for Linux we should simply ignore that attribute.
Quoted 2 lines
But if you really need to restrict namespace access, shouldn't that be enforced on the target side with reservations or similar mechanism?
Think for example about containers where we give eah container access to a single nvme namespace, including container root access. Here you don't really want container A to be able to submit I/O for another container. A similar case exists for virtualization where we had problems with SCSI passthrough from guests.
Quoted 9 lines · Christoph Hellwig
Quoted 2 lines · Keith Busch
But if you really need to restrict namespace access, shouldn't that be enforced on the target side with reservations or similar mechanism?
Think for example about containers where we give eah container access to a single nvme namespace, including container root access. Here you don't really want container A to be able to submit I/O for another container. A similar case exists for virtualization where we had problems with SCSI passthrough from guests.
Okay, that's a great point.
Yan, we should apply this if you can submit a patch for the linux-block tree.