nvme: fix corruption for passthrough meta/data
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): CGME20231013052157epcas5p3dc0698c56f9846191d315fa8d33ccb5c@epcas5p3.samsung.com
PATCH v4 1/1 nvme: fix corruption for passthrough meta/data
User can specify a smaller meta buffer than what the device is wired to update/access. Kernel makes a copy of the meta buffer into which the device does DMA.
As a result, the device overwrites the unrelated kernel memory, causing random kernel crashes.
Same issue is possible for extended-lba case also. When user specifies a short unaligned buffer, the kernel makes a copy and uses that for DMA.
Detect these situations and prevent corruption for unprivileged user passthrough. No change to status-quo for privileged/root user.
---
Changes since v3:
- Block only unprivileged user
- Harden the checks by disallowing everything for which data length (nlb) can not be determined
- Separate the bounce buffer checks to a different function
- Factor in CSIs beyond NVM and ZNS
Changes since v2:
- Handle extended-lba case: short unaligned buffer IO
- Reduce the scope of check to only well-known commands
- Do not check early. Move it deeper so that check gets executed less often
- Combine two patches into one.
Changes since v1:
- Revise the check to exclude PRACT=1 case
drivers/nvme/host/ioctl.c | 116 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+)
drivers/nvme/host/ioctl.c +116 -0
@@ -96,6 +96,76 @@ static void __user *nvme_to_user_ptr(uintptr_t ptrval)return (void __user *)ptrval; } +static inline bool nvme_nlb_in_cdw12 (struct nvme_ns *ns,u8 opcode) +{ +u8 csi = ns->head->ids.csi; + +if (csi !=NVME_CSI_NVM && csi !=NVME_CSI_ZNS ) +return false; + +switch (opcode) { +case nvme_cmd_read: +case nvme_cmd_write: +case nvme_cmd_compare: +case nvme_cmd_zone_append: +return true; +default : +return false; + } +} + +/* +* NVMe has no separate field to encode the metadata length expected +* (except when using SGLs). +* +* Because of that we can't allow to transfer arbitrary metadata, as +* a metadata buffer that is shorted than what the device expects for +* the command will lead to arbitrary kernel (if bounce buffering) or +* userspace (if not) memory corruption. +* +* Check that external metadata is only specified for the few commands +* where we know the length based of other fields, and that it fits +* the actual data transfer from/to the device. +*/ +static bool nvme_validate_metadata_len (struct request *req,unsigned meta_len) +{ +struct nvme_ns *ns = req->q->queuedata; +struct nvme_command *c =nvme_req (req)->cmd; +u32 len_by_nlb; + +/* Do not guard admin */ +if (capable (CAP_SYS_ADMIN )) +return true; + +/* Block commands that do not have nlb in cdw12 */ +if (!nvme_nlb_in_cdw12 (ns, c->common.opcode)) { +dev_err (ns->ctrl->device, +"unknown metadata command %c\n" , c->common.opcode); +return false; + } + +/* Skip when PI is inserted or stripped and not transferred */ +if (ns->ms == ns->pi_size && + (c->rw.control &cpu_to_le16 (NVME_RW_PRINFO_PRACT ))) +return true; + +if (ns->features &NVME_NS_EXT_LBAS ) { +dev_err (ns->ctrl->device, +"requires extended LBAs for metadata\n" ); +return false; + } + + len_by_nlb = (le16_to_cpu (c->rw.length) +1 ) * ns->ms; +if (meta_len < len_by_nlb) { +dev_err (ns->ctrl->device, +"metadata length (%u instad of %u) is too small.\n" , + meta_len, len_by_nlb); +return false; + } + +return true; +} +static void *nvme_add_user_metadata (struct request *req,void __user *ubuf,unsigned len,u32 seed) {
@@ -104,6 +174,9 @@ static void *nvme_add_user_metadata(struct request *req, void __user *ubuf,void *buf;struct bio *bio = req->bio; +if (!nvme_validate_metadata_len (req, len)) +return ERR_PTR (-EINVAL ); + buf =kmalloc (len,GFP_KERNEL );if (!buf) goto out;
@@ -134,6 +207,41 @@ static void *nvme_add_user_metadata(struct request *req, void __user *ubuf,return ERR_PTR (ret); } +static bool nvme_validate_buffer_len (struct nvme_ns *ns,struct nvme_command *c, +unsigned meta_len,unsigned data_len) +{ +u32 mlen_by_nlb, dlen_by_nlb; + +/* Do not guard admin */ +if (capable (CAP_SYS_ADMIN )) +return true; + +/* Block commands that do not have nlb in cdw12 */ +if (!nvme_nlb_in_cdw12 (ns, c->common.opcode)) { +dev_err (ns->ctrl->device, +"unknown metadata command %c.\n" , c->common.opcode); +return false; + } + +/* When PI is inserted or stripped and not transferred.*/ +if (ns->ms == ns->pi_size && + (c->rw.control &cpu_to_le16 (NVME_RW_PRINFO_PRACT ))) + mlen_by_nlb =0 ; +else + mlen_by_nlb = (le16_to_cpu (c->rw.length) +1 ) * ns->ms; + + dlen_by_nlb = (le16_to_cpu (c->rw.length) +1 ) << ns->lba_shift; + +if (data_len < (dlen_by_nlb + mlen_by_nlb)) { +dev_err (ns->ctrl->device, +"buffer length (%u instad of %u) is too small.\n" , + data_len, dlen_by_nlb + mlen_by_nlb); +return false; + } + +return true; +} +static int nvme_finish_user_metadata (struct request *req,void __user *ubuf,void *meta,unsigned len,int ret) {
@@ -202,6 +310,14 @@ static int nvme_map_user_request(struct request *req, u64 ubuffer, } *metap = meta; } +/* Guard for a short bounce buffer */ +if (bio->bi_private) { +if (!nvme_validate_buffer_len (ns,nvme_req (req)->cmd, + meta_len, bufflen)) { + ret = -EINVAL ; + goto out_unmap; + } + }return ret;
Signature · 1 line
2.25.1
Christoph Hellwig
maintainerQuoted 2 lines · Kanchan Joshi
Changes since v3:
- Block only unprivileged user
That's not really what at least I had in mind. I'd much rather completely disable unprivileged passthrough for now as an easy backportable patch. And then only re-enable it later in a way where it does require using SGLs for all data transfers.
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Kanchan Joshi
Changes since v3:
- Block only unprivileged userThat's not really what at least I had in mind. I'd much rather completely disable unprivileged passthrough for now as an easy backportable patch.
As you deem fit, but I think even this will be bakported upto the patch that introduced unprivileged passthrough.
Quoted 9 lines · Christoph Hellwig
Quoted 2 lines · Kanchan Joshi
Changes since v3:
- Block only unprivileged userThat's not really what at least I had in mind. I'd much rather completely disable unprivileged passthrough for now as an easy backportable patch. And then only re-enable it later in a way where it does require using SGLs for all data transfers.
I did not get how forcing SGLs can solve the issue at hand. The problem happened because (i) user specified short buffer/len, and (ii) kernel allocated buffer. Whether the buffer is fed to device using PRP or SGL does not seem to solve the large DMA problem.
Quoted 11 lines · Kanchan Joshi
Quoted 9 lines · Christoph Hellwig
Quoted 2 lines · Kanchan Joshi
Changes since v3:
- Block only unprivileged userThat's not really what at least I had in mind. I'd much rather completely disable unprivileged passthrough for now as an easy backportable patch. And then only re-enable it later in a way where it does require using SGLs for all data transfers.
Quoted 5 lines
I did not get how forcing SGLs can solve the issue at hand. The problem happened because (i) user specified short buffer/len, and (ii) kernel allocated buffer. Whether the buffer is fed to device using PRP or SGL does not seem to solve the large DMA problem.
FWIW, this is the test-patch I wrote to force passthrough to use SGL.
--- drivers/nvme/host/ioctl.c | 2 ++ drivers/nvme/host/nvme.h | 1 + drivers/nvme/host/pci.c | 8 +++++--- 3 files changed, 8 insertions(+), 3 deletions(-)
drivers/nvme/host/ioctl.c +0 -0
@@ -202,6 +202,8 @@ static int nvme_map_user_request(struct request
*req, u64 ubuffer,
}
*metap = meta;
}+ /* force sgl for data transfer */
+ nvme_req(req)->flags |= NVME_REQ_FORCE_SGL;
return ret;
drivers/nvme/host/nvme.h +1 -0
@@ -184,6 +184,7 @@ enum {NVME_REQ_CANCELLED = (1 <<0 ),NVME_REQ_USERCMD = (1 <<1 ),NVME_MPATH_IO_STATS = (1 <<2 ), + NVME_REQ_FORCE_SGL = (1 << 3), };static inline struct nvme_request *nvme_req (struct request *req)
drivers/nvme/host/pci.c +0 -0
@@ -772,18 +772,20 @@ static blk_status_t nvme_map_data(struct nvme_dev
*dev, struct request *req,
struct nvme_iod *iod = blk_mq_rq_to_pdu(req);
blk_status_t ret = BLK_STS_RESOURCE;
int rc;+ bool force_sgl = nvme_req(req)->flags & NVME_REQ_FORCE_SGL;
if (blk_rq_nr_phys_segments(req) == 1) {
struct nvme_queue *nvmeq = req->mq_hctx->driver_data;
struct bio_vec bv = req_bvec(req);
if (!is_pci_p2pdma_page(bv.bv_page)) {- if (bv.bv_offset + bv.bv_len <=NVME_CTRL_PAGE_SIZE * 2)
+ if (!force_sgl &&
+ bv.bv_offset + bv.bv_len <=NVME_CTRL_PAGE_SIZE * 2)
return nvme_setup_prp_simple(dev, req,
&cmnd->rw,
&bv);
- if (nvmeq->qid && sgl_threshold &&
- nvme_ctrl_sgl_supported(&dev->ctrl))
+ if (nvmeq->qid &&nvme_ctrl_sgl_supported(&dev->ctrl)
+ && (sgl_threshold || force_sgl))
return nvme_setup_sgl_simple(dev, req,
&cmnd->rw,
&bv);
}Keith Busch
maintainerQuoted 11 lines · Kanchan Joshi
Quoted 9 lines · Christoph Hellwig
Quoted 2 lines · Kanchan Joshi
Changes since v3:
- Block only unprivileged userThat's not really what at least I had in mind. I'd much rather completely disable unprivileged passthrough for now as an easy backportable patch. And then only re-enable it later in a way where it does require using SGLs for all data transfers.
Quoted 4 lines
I did not get how forcing SGLs can solve the issue at hand. The problem happened because (i) user specified short buffer/len, and (ii) kernel allocated buffer. Whether the buffer is fed to device using PRP or SGL does not seem to solve the large DMA problem.
The problem is a disconnect between the buffer size provided and the implied size of the command. The idea with SGL is that it requires an explicit buffer size, so the device will know the buffer is short and return an appropriate error.
Quoted 17 lines · Keith Busch
Quoted 11 lines · Kanchan Joshi
Quoted 9 lines · Christoph Hellwig
Quoted 2 lines · Kanchan Joshi
Changes since v3:
- Block only unprivileged userThat's not really what at least I had in mind. I'd much rather completely disable unprivileged passthrough for now as an easy backportable patch. And then only re-enable it later in a way where it does require using SGLs for all data transfers.
Quoted 4 lines
I did not get how forcing SGLs can solve the issue at hand. The problem happened because (i) user specified short buffer/len, and (ii) kernel allocated buffer. Whether the buffer is fed to device using PRP or SGL does not seem to solve the large DMA problem.
Quoted 4 lines
The problem is a disconnect between the buffer size provided and the implied size of the command. The idea with SGL is that it requires an explicit buffer size, so the device will know the buffer is short and return an appropriate error.
Thanks for clearing this up.
It seems we will have two limitations with this approach - (i) sgl for the external metadata buffer, and (ii) using sgl for data-transfer will reduce the speed of passthrough io, perhaps more than what can happen using the checks. And if we make the sgl opt-in, that means leaving the hole for the case when this was not chosen.
Christoph Hellwig
maintainerQuoted 5 lines · Kanchan Joshi
It seems we will have two limitations with this approach - (i) sgl for the external metadata buffer, and (ii) using sgl for data-transfer will reduce the speed of passthrough io, perhaps more than what can happen using the checks. And if we make the sgl opt-in, that means leaving the hole for the case when this was not chosen.
The main limitation is that the device needs to support SGLs, and we need to as well (we currently don't for metadata). But for any non-stupid workload SGLs should be at least as fast if not faster with modern hardware. But I see no way out.
Now can we please get a patch to disable the unprivileged passthrough ASAP to fix this probably exploitable hole? Or should I write one?
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Kanchan Joshi
It seems we will have two limitations with this approach - (i) sgl for the external metadata buffer, and (ii) using sgl for data-transfer will reduce the speed of passthrough io, perhaps more than what can happen using the checks. And if we make the sgl opt-in, that means leaving the hole for the case when this was not chosen.
The main limitation is that the device needs to support SGLs, and
Indeed. Particularly on non-enterprise drives, SGL is a luxury.
Quoted 3 lines
we need to as well (we currently don't for metadata). But for any non-stupid workload SGLs should be at least as fast if not faster with modern hardware.
But nvme-pcie selects PRP for the small IO.
Quoted 3 lines
But I see no way out.
Now can we please get a patch to disable the unprivileged passthrough ASAP to fix this probably exploitable hole? Or should I write one?
I can write. I was waiting to see whether Keith has any different opinion on the route that v4 takes.
It seems this is a no go from him.
Disabling is possible with a simple patch that just returns false from nvme_cmd_allowed() if CAP_SYS_ADMIN is not present.
I assume that is not sought? But a deep revert that removes all the things such as carrying the file-mode to various functions. Hope tomorrow is ok for that.
Keith Busch
maintainerQuoted 7 lines · Kanchan Joshi
Quoted 3 lines · Christoph Hellwig
But I see no way out.
Now can we please get a patch to disable the unprivileged passthrough ASAP to fix this probably exploitable hole? Or should I write one?I can write. I was waiting to see whether Keith has any different opinion on the route that v4 takes.
Sorry for the delay, I had some trouble with my test machine last week.
It sounds like the kernel memory is the only reason for the concern, and you don't really care if we're corrupting user memory. If so, let's just use that instead of kernel bounce buffers. (Minor digression, the current bounce 'buf' is leaking kernel memory on reads since it doesn't zero it).
Anyway, here's a diff that maps userspace buffer for "integrity" payloads and tests okay on my side. It enforces that you can't cross virtual page boundaries, so you can't corrupt anything outside your vma. The allocation error handling isn't here, but it's just a proof of concept right now.
---
block/bio-integrity.c +56 -0
@@ -91,6 +91,19 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio, }EXPORT_SYMBOL (bio_integrity_alloc); +void bio_integrity_unmap_user (struct bio_integrity_payload *bip) +{ +struct bvec_iter iter; +struct bio_vec bv; +bool dirty =bio_data_dir (bip->bip_bio) ==READ ; + +bip_for_each_vec (bv, bip, iter) { +if (dirty && !PageCompound (bv.bv_page)) +set_page_dirty_lock (bv.bv_page); +unpin_user_page (bv.bv_page); + } +} +/** * bio_integrity_free - Free bio integrity payload * @bio: bio containing bip to be freed
@@ -105,6 +118,8 @@ void bio_integrity_free(struct bio *bio)if (bip->bip_flags &BIP_BLOCK_INTEGRITY )kfree (bvec_virt (bip->bip_vec)); +if (bip->bip_flags &BIP_INTEGRITY_USER ) +bio_integrity_unmap_user (bip);;__bio_integrity_free (bs, bip); bio->bi_integrity =NULL ;
@@ -160,6 +175,47 @@ int bio_integrity_add_page(struct bio *bio, struct page *page, }EXPORT_SYMBOL (bio_integrity_add_page); +int bio_integrity_map_user (struct bio *bio,void __user *ubuf,unsigned int len, +u32 seed,u32 maxvecs) +{ +struct request_queue *q =bdev_get_queue (bio->bi_bdev); +unsigned long align = q->dma_pad_mask |queue_dma_alignment (q); +struct page *stack_pages[UIO_FASTIOV ]; +size_t offset =offset_in_page (ubuf); +struct page **pages = stack_pages; +struct bio_integrity_payload *bip; +unsigned long ptr = (uintptr_t )ubuf; +int npages, ret, p; +u32 bytes; + +if (ptr & align) +return -EINVAL ; + + bip =bio_integrity_alloc (bio,GFP_KERNEL , maxvecs); +if (IS_ERR (bip)) +return PTR_ERR (bip); + + ret =pin_user_pages_fast (ptr,UIO_FASTIOV ,FOLL_WRITE , pages); +if (unlikely (ret <0 )) +return ret; + + npages = ret; + bytes =min_t (u32, len,PAGE_SIZE - offset); +for (p =0 ; p < npages; p++) { + ret =bio_integrity_add_page (bio, pages[p], bytes, offset); +if (ret != bytes) +return -EINVAL ; + len -= ret; + offset =0 ; + bytes =min_t (u32, len,PAGE_SIZE ); + } + + bip->bip_iter.bi_sector = seed; + bip->bip_flags |=BIP_INTEGRITY_USER ; +return 0 ; +} +EXPORT_SYMBOL (bio_integrity_map_user); +/** * bio_integrity_process - Process integrity metadata for a bio * @bio: bio to generate/verify integrity metadata for
drivers/nvme/host/ioctl.c +11 -60
150 lines not on this page · raw message
include/linux/bio.h +8 -0
@@ -324,6 +324,7 @@ enum bip_flags {BIP_CTRL_NOCHECK =1 <<2 ,/* disable HBA integrity checking */ BIP_DISK_NOCHECK =1 <<3 ,/* disable disk integrity checking */ BIP_IP_CHECKSUM =1 <<4 ,/* IP checksum */ + BIP_INTEGRITY_USER = 1 << 5, /* Integrity payload is user address */ };/*
@@ -720,6 +721,7 @@ static inline bool bioset_initialized(struct bio_set *bs)extern struct bio_integrity_payload *bio_integrity_alloc (struct bio *,gfp_t ,unsigned int );extern int bio_integrity_add_page (struct bio *,struct page *,unsigned int ,unsigned int ); +extern int bio_integrity_map_user (struct bio *bio,void __user *ubuf,unsigned int len,u32 seed,u32 maxvecs);extern bool bio_integrity_prep (struct bio *);extern void bio_integrity_advance (struct bio *,unsigned int );extern void bio_integrity_trim (struct bio *);
@@ -789,6 +791,12 @@ static inline int bio_integrity_add_page(struct bio *bio, struct page *page,return 0 ; } +static inline int bio_integrity_map_user (struct bio *bio,void __user *ubuf, +unsigned int len,u32 seed,u32 maxvecs) +{ + return -EINVAL +} +#endif /* CONFIG_BLK_DEV_INTEGRITY */ /*
--
Christoph Hellwig
maintainerQuoted 5 lines · Keith Busch
It sounds like the kernel memory is the only reason for the concern, and you don't really care if we're corrupting user memory. If so, let's just use that instead of kernel bounce buffers. (Minor digression, the current bounce 'buf' is leaking kernel memory on reads since it doesn't zero it).
No, arbitrary memory overwrite is always an issue, userspace or kernel, data or metadata buffer.
Note that even without block layer bounce buffering, there can always be other kernel memory involved, e.g. swiotlb.
We need to get the fix to disable the unprivileged passthrough in ASAP.
Quoted 4 lines · Christoph Hellwig
The main limitation is that the device needs to support SGLs, and we need to as well (we currently don't for metadata). But for any non-stupid workload SGLs should be at least as fast if not faster with modern hardware. But I see no way out.
You may agree that it's a hardware-assisted way out. It is offloading the checks to a SGL-capable device.
I wrote some quick code in that direction but could not readily get my hands on a device that exposes metadata-with-sgl capability. That reminded me that we are limiting unprivileged-passthrough to a niche set of devices/users. That is the opposite of what the feature was for.
OTOH, this patch implemented a software-only way out. There are some checks, but someone (either SW or HW) has to do those to keep things right.
The patch ensures the regular user cannot exploit the hole and that the root user continues to work as before (Keith's concern). So, I really wonder why we don't want to go for the way that solves it generically.
Christoph Hellwig
maintainerQuoted 3 lines · Kanchan Joshi
OTOH, this patch implemented a software-only way out. There are some checks, but someone (either SW or HW) has to do those to keep things right.
It only verifies it to the read/write family of commands by interpreting these commands. It still leaves a wide hole for any other command.
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Kanchan Joshi
OTOH, this patch implemented a software-only way out. There are some checks, but someone (either SW or HW) has to do those to keep things right.
It only verifies it to the read/write family of commands by interpreting these commands. It still leaves a wide hole for any other command.
Can you please explain for what command do you see the hole? I am trying to see if it is really impossible to fix this hole for good.
We only need to check for specific io commands of the NVM/ZNS command set that can do extra DMA.
Along the same lines, disallowing KV does not seem necessary either. For admin commands, we allow only very few, and for those this hole does not exist.
Keith Busch
maintainerQuoted 11 lines · Kanchan Joshi
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Kanchan Joshi
OTOH, this patch implemented a software-only way out. There are some checks, but someone (either SW or HW) has to do those to keep things right.
It only verifies it to the read/write family of commands by interpreting these commands. It still leaves a wide hole for any other command.
Quoted 5 lines
Can you please explain for what command do you see the hole? I am trying to see if it is really impossible to fix this hole for good.
We only need to check for specific io commands of the NVM/ZNS command set that can do extra DMA.
The spec defines a few commands that may use MPTR, but most of the possible opcodes that could use it are not defined in spec. You'd have to break forward compatibility through this interface for non-root users by limiting its use to only the known opcodes and reject everything else.
Quoted 16 lines · Keith Busch
Quoted 14 lines · Kanchan Joshi
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Kanchan Joshi
OTOH, this patch implemented a software-only way out. There are some checks, but someone (either SW or HW) has to do those to keep things right.
It only verifies it to the read/write family of commands by interpreting these commands. It still leaves a wide hole for any other command.
Can you please explain for what command do you see the hole? I am trying to see if it is really impossible to fix this hole for good.
Quoted 8 lines
Quoted 2 lines
We only need to check for specific io commands of the NVM/ZNS command set that can do extra DMA.
The spec defines a few commands that may use MPTR, but most of the possible opcodes that could use it are not defined in spec. You'd have to break forward compatibility through this interface for non-root users by limiting its use to only the known opcodes and reject everything else.
I see. Future commands. Looks like every solution involves non-root users to face some form of inconvenience.
Quoted 6 lines · Kanchan Joshi
User can specify a smaller meta buffer than what the device is wired to update/access. Kernel makes a copy of the meta buffer into which the device does DMA.
As a result, the device overwrites the unrelated kernel memory, causing random kernel crashes.
Quoted 7 lines
Same issue is possible for extended-lba case also. When user specifies a short unaligned buffer, the kernel makes a copy and uses that for DMA.
Detect these situations and prevent corruption for unprivileged user passthrough. No change to status-quo for privileged/root user.
Fixes 63263d60e0f9 ("nvme: Use metadata for passthrough commands")
Since change is only for unprivileged user, I should have changed this 'Fixes:' to point to this patch instead:
5b7717f44b1 (nvme: fine-granular CAP_SYS_ADMIN for nvme io commands)