nvme: a few error-path and validation fixes
Subsystems (2)
cover nvme: a few error-path and validation fixes
This series collects four independent fixes found while auditing error and command-processing paths in the NVMe host and target drivers. Each is standalone; they only share the same subsystem.
1. nvmet: an Identify CNS 07h (active NS list for a command set)
dereferences req->ns, which is always NULL on that path -> NULL
pointer oops. Also fixes the filter to test the iterated ns->csi. 2. nvmet: nvmet_ns_enable() ignores the percpu_ref_init() return value and reports success to userspace when it fails, leaving the ns dead.
3. nvme-pci: the per-NUMA-node descriptor DMA pools, created lazily on admin tag set allocation, are only freed in nvme_remove(); a probe failure after that point leaks them.
4. nvme: nvme_query_fdp_info() trusts the device-supplied nruhsd count and can read past the fixed-size RUH status buffer (heap OOB read).
Found by code audit. #1 and #2 are functional/crash fixes; #3 is a resource leak; #4 is device-triggered hardening. Compile-tested.
Guixin Liu (4):
nvmet: fix NULL pointer dereference in nvmet_execute_identify_nslist()
nvmet: propagate percpu_ref_init() failure in nvmet_ns_enable()
nvme-pci: release descriptor pools on probe failure
nvme: clamp FDP placement handle count to the buffer size
drivers/nvme/host/core.c | 1 + drivers/nvme/host/pci.c | 1 + drivers/nvme/target/admin-cmd.c | 2 +- drivers/nvme/target/core.c | 3 ++- 4 files changed, 5 insertions(+), 2 deletions(-)
base-commit: 11028ab62899e4191e074ee364c712b77823a9c4
Signature · 1 line
2.43.7
PATCH 1/4 nvmet: fix NULL pointer dereference in nvmet_execute_identify_nslist()
When a host issues an Identify command with CNS 07h (Active Namespace ID List for a specific I/O Command Set), nvmet_execute_identify_nslist() is called with match_css set. The command-set filter dereferences req->ns, but this handler never calls nvmet_req_find_ns(), so req->ns is always NULL (nvmet_req_init() resets it to NULL). As soon as an enabled namespace with an NSID greater than the requested value exists, req->ns->csi dereferences a NULL pointer and oopses.
Besides the crash, the comparison is logically wrong: to filter the list by command set it must test the command set of the namespace being iterated, not a single fixed value. Use the loop variable ns->csi.
--- drivers/nvme/target/admin-cmd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/target/admin-cmd.c +1 -1
@@ -958,7 +958,7 @@ static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css)nvmet_for_each_enabled_ns (&ctrl->subsys->namespaces, idx, ns) {if (ns->nsid <= min_nsid)continue ; -if (match_css && req->ns->csi != req->cmd->identify.csi) +if (match_css && ns->csi != req->cmd->identify.csi)continue ; list[i++] =cpu_to_le32 (ns->nsid);if (i == buf_size /sizeof (__le32))
Signature · 1 line
2.43.7
Re: [PATCH 1/4] nvmet: fix NULL pointer dereference in nvmet_execute_identify_nslist()
Quoted 23 lines · Guixin Liu
When a host issues an Identify command with CNS 07h (Active Namespace ID List for a specific I/O Command Set),
nvmet_execute_identify_nslist()is called withmatch_cssset. The command-set filter dereferencesreq->ns, but this handler never callsnvmet_req_find_ns(), soreq->nsis always NULL (nvmet_req_init()resets it to NULL). As soon as an enabled namespace with an NSID greater than the requested value exists,req->ns->csidereferences a NULL pointer and oopses.Besides the crash, the comparison is logically wrong: to filter the list by command set it must test the command set of the namespace being iterated, not a single fixed value. Use the loop variable
ns->csi.Fixes 61c9967cd634 ("nvmet: implement active command set ns list")Signed-off-by Guixin Liu <[email protected]>--- drivers/nvme/target/admin-cmd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/nvme/target/admin-cmd.c +0
-0
drivers/nvme/target/admin-cmd.c static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css) · line 964
Quoted 8 lines
drivers/nvme/target/admin-cmd.c +1
-1@@ -958,7 +958,7 @@ static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css)nvmet_for_each_enabled_ns (&ctrl->subsys->namespaces, idx, ns) {if (ns->nsid <= min_nsid)continue ; -if (match_css && req->ns->csi != req->cmd->identify.csi) +if (match_css && ns->csi != req->cmd->identify.csi)continue ; list[i++] =cpu_to_le32 (ns->nsid);if (i == buf_size /sizeof (__le32))
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
Re: [PATCH 1/4] nvmet: fix NULL pointer dereference in nvmet_execute_identify_nslist()
In reply to Guixin LiuLooks good:
Quoted 7 lines · Guixin Liu
When a host issues an Identify command with CNS 07h (Active Namespace ID List for a specific I/O Command Set),
nvmet_execute_identify_nslist()is called withmatch_cssset. The command-set filter dereferencesreq->ns, but this handler never callsnvmet_req_find_ns(), soreq->nsis always NULL (nvmet_req_init()resets it to NULL). As soon as an enabled namespace with an NSID greater than the requested value exists,req->ns->csidereferences a NULL pointer and oopses.
Can you submit a testcase for that to blktests?
Re: [PATCH 1/4] nvmet: fix NULL pointer dereference in nvmet_execute_identify_nslist()
Quoted 5 lines · Christoph Hellwig
Looks good:
Reviewed-by Christoph Hellwig <[email protected]>On Thu, Jul 30, 2026 at 12:31:02PM +0800, Guixin Liu wrote:
Quoted 8 lines
Quoted 7 lines
When a host issues an Identify command with CNS 07h (Active Namespace ID List for a specific I/O Command Set),
nvmet_execute_identify_nslist()is called withmatch_cssset. The command-set filter dereferencesreq->ns, but this handler never callsnvmet_req_find_ns(), soreq->nsis always NULL (nvmet_req_init()resets it to NULL). As soon as an enabled namespace with an NSID greater than the requested value exists,req->ns->csidereferences a NULL pointer and oopses.Can you submit a testcase for that to blktests?
Sure, I will send a patch for that.
Best Regards,
Guixin Liu
Re: [PATCH 1/4] nvmet: fix NULL pointer dereference in nvmet_execute_identify_nslist()
In reply to Guixin LiuQuoted 8 lines · Guixin Liu
When a host issues an Identify command with CNS 07h (Active Namespace ID List for a specific I/O Command Set),
nvmet_execute_identify_nslist()is called withmatch_cssset. The command-set filter dereferencesreq->ns, but this handler never callsnvmet_req_find_ns(), soreq->nsis always NULL (nvmet_req_init()resets it to NULL). As soon as an enabled namespace with an NSID greater than the requested value exists,req->ns->csidereferences a NULL pointer and oopses.
Quoted 6 lines
Besides the crash, the comparison is logically wrong: to filter the list by command set it must test the command set of the namespace being iterated, not a single fixed value. Use the loop variable
ns->csi.Fixes 61c9967cd634 ("nvmet: implement active command set ns list")Signed-off-by Guixin Liu <[email protected]>
Looks good to me.
PATCH 2/4 nvmet: propagate percpu_ref_init() failure in nvmet_ns_enable()
In reply to Guixin LiuThe return value of percpu_ref_init() is discarded. At this point ret is 0 from the preceding successful steps, so when the allocation inside percpu_ref_init() fails the code jumps to the out_pr_exit cleanup chain which ends with "return ret", i.e. reports success. The configfs enable store then tells userspace the namespace was enabled even though it was not and its backing device has already been torn down.
Capture the return value so the failure is propagated.
--- drivers/nvme/target/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/target/core.c +2 -1
@@ -610,7 +610,8 @@ int nvmet_ns_enable(struct nvmet_ns *ns) goto out_dev_put; } -if (percpu_ref_init (&ns->ref, nvmet_destroy_namespace,0 ,GFP_KERNEL )) + ret =percpu_ref_init (&ns->ref, nvmet_destroy_namespace,0 ,GFP_KERNEL ); +if (ret) goto out_pr_exit;nvmet_ns_changed (subsys, ns->nsid);
Signature · 1 line
2.43.7
Re: [PATCH 2/4] nvmet: propagate percpu_ref_init() failure in nvmet_ns_enable()
Quoted 20 lines · Guixin Liu
The return value of
percpu_ref_init()is discarded. At this point ret is 0 from the preceding successful steps, so when the allocation insidepercpu_ref_init()fails the code jumps to theout_pr_exitcleanup chain which ends with "return ret", i.e. reports success. The configfs enable store then tells userspace the namespace was enabled even though it was not and its backing device has already been torn down.Capture the return value so the failure is propagated.
Fixes 408232680707 ("nvmet: Fix crash when a namespace is disabled")Signed-off-by Guixin Liu <[email protected]>--- drivers/nvme/target/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/nvme/target/core.c +0
-0
drivers/nvme/target/core.c int nvmet_ns_enable(struct nvmet_ns *ns) · line 617
Quoted 9 lines
drivers/nvme/target/core.c +2
-1@@ -610,7 +610,8 @@ int nvmet_ns_enable(struct nvmet_ns *ns) goto out_dev_put; } -if (percpu_ref_init (&ns->ref, nvmet_destroy_namespace,0 ,GFP_KERNEL )) + ret =percpu_ref_init (&ns->ref, nvmet_destroy_namespace,0 ,GFP_KERNEL ); +if (ret) goto out_pr_exit;nvmet_ns_changed (subsys, ns->nsid);
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
Re: [PATCH 2/4] nvmet: propagate percpu_ref_init() failure in nvmet_ns_enable()
In reply to Guixin LiuQuoted 6 lines · Guixin Liu
The return value of
percpu_ref_init()is discarded. At this point ret is 0 from the preceding successful steps, so when the allocation insidepercpu_ref_init()fails the code jumps to theout_pr_exitcleanup chain which ends with "return ret", i.e. reports success. The configfs enable store then tells userspace the namespace was enabled even though it was not and its backing device has already been torn down.
Looks good:
Re: [PATCH 2/4] nvmet: propagate percpu_ref_init() failure in nvmet_ns_enable()
In reply to Guixin LiuQuoted 11 lines · Guixin Liu
The return value of
percpu_ref_init()is discarded. At this point ret is 0 from the preceding successful steps, so when the allocation insidepercpu_ref_init()fails the code jumps to theout_pr_exitcleanup chain which ends with "return ret", i.e. reports success. The configfs enable store then tells userspace the namespace was enabled even though it was not and its backing device has already been torn down.Capture the return value so the failure is propagated.
Fixes 408232680707 ("nvmet: Fix crash when a namespace is disabled")Signed-off-by Guixin Liu <[email protected]>
Looks good to me.
PATCH 3/4 nvme-pci: release descriptor pools on probe failure
In reply to Guixin LiuThe per-NUMA-node descriptor DMA pools are created lazily from nvme_init_hctx_common() once the admin tag set is allocated, but they are only destroyed in nvme_remove() via nvme_release_descriptor_pools(). Any probe failure after the admin tag set has been allocated unwinds through the out_disable label and nvme_pci_free_ctrl(), neither of which releases the pools, leaking the dma_pool objects.
Release the descriptor pools in the out_disable error path. It must not be added to nvme_pci_free_ctrl(), as that would double-free against nvme_remove() on the normal teardown path.
--- drivers/nvme/host/pci.c | 1 + 1 file changed, 1 insertion(+)
drivers/nvme/host/pci.c +1 -0
@@ -3838,6 +3838,7 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)nvme_dev_remove_admin (dev);nvme_dbbuf_dma_free (dev);nvme_free_queues (dev,0 ); +nvme_release_descriptor_pools (dev); out_release_iod_mempool:mempool_destroy (dev->dmavec_mempool); out_dev_unmap:
Signature · 1 line
2.43.7
Re: [PATCH 3/4] nvme-pci: release descriptor pools on probe failure
Quoted 22 lines · Guixin Liu
The per-NUMA-node descriptor DMA pools are created lazily from
nvme_init_hctx_common()once the admin tag set is allocated, but they are only destroyed innvme_remove()vianvme_release_descriptor_pools(). Any probe failure after the admin tag set has been allocated unwinds through theout_disablelabel andnvme_pci_free_ctrl(), neither of which releases the pools, leaking thedma_poolobjects.Release the descriptor pools in the
out_disableerror path. It must not be added tonvme_pci_free_ctrl(), as that would double-free againstnvme_remove()on the normal teardown path.Fixes d977506f8863 ("nvme-pci: make PRP list DMA pools per-NUMA-node")Signed-off-by Guixin Liu <[email protected]>--- drivers/nvme/host/pci.c | 1 + 1 file changed, 1 insertion(+)drivers/nvme/host/pci.c +0
-0
drivers/nvme/host/pci.c static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 3844
Quoted 7 lines
drivers/nvme/host/pci.c +1
-0@@ -3838,6 +3838,7 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)nvme_dev_remove_admin (dev);nvme_dbbuf_dma_free (dev);nvme_free_queues (dev,0 ); +nvme_release_descriptor_pools (dev); out_release_iod_mempool:mempool_destroy (dev->dmavec_mempool); out_dev_unmap:
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
Re: [PATCH 3/4] nvme-pci: release descriptor pools on probe failure
In reply to Guixin LiuLooks good:
Re: [PATCH 3/4] nvme-pci: release descriptor pools on probe failure
In reply to Guixin LiuRe: [PATCH 3/4] nvme-pci: release descriptor pools on probe failure
In reply to Guixin LiuQuoted 7 lines · Guixin Liu
The per-NUMA-node descriptor DMA pools are created lazily from
nvme_init_hctx_common()once the admin tag set is allocated, but they are only destroyed innvme_remove()vianvme_release_descriptor_pools(). Any probe failure after the admin tag set has been allocated unwinds through theout_disablelabel andnvme_pci_free_ctrl(), neither of which releases the pools, leaking thedma_poolobjects.
Quoted 6 lines
Release the descriptor pools in the
out_disableerror path. It must not be added tonvme_pci_free_ctrl(), as that would double-free againstnvme_remove()on the normal teardown path.Fixes d977506f8863 ("nvme-pci: make PRP list DMA pools per-NUMA-node")Signed-off-by Guixin Liu <[email protected]>
Looks good to me.
PATCH 4/4 nvme: clamp FDP placement handle count to the buffer size
In reply to Guixin Liunvme_query_fdp_info() allocates the RUH status buffer for at most S8_MAX - 1 descriptors and caps the io-mgmt-receive transfer to that size. head->nr_plids, however, is taken verbatim from the device-supplied nruhsd field, which can be up to 65535. If a non-conformant or malicious device reports more descriptors than the buffer holds, the copy loop reads past the end of the ruhs buffer (heap out-of-bounds read).
Clamp nr_plids to the number of descriptors the buffer can actually hold.
--- drivers/nvme/host/core.c | 1 + 1 file changed, 1 insertion(+)
drivers/nvme/host/core.c +1 -0
@@ -2358,6 +2358,7 @@ static int nvme_query_fdp_info(struct nvme_ns *ns, struct nvme_ns_info *info) } head->nr_plids =le16_to_cpu (ruhs->nruhsd); + head->nr_plids =min_t (u16, head->nr_plids,S8_MAX -1 );if (!head->nr_plids) goto free;
Signature · 1 line
2.43.7
Re: [PATCH 4/4] nvme: clamp FDP placement handle count to the buffer size
Quoted 20 lines · Guixin Liu
nvme_query_fdp_info()allocates the RUH status buffer for at mostS8_MAX- 1 descriptors and caps the io-mgmt-receive transfer to that size.head->nr_plids, however, is taken verbatim from the device-supplied nruhsd field, which can be up to 65535. If a non-conformant or malicious device reports more descriptors than the buffer holds, the copy loop reads past the end of the ruhs buffer (heap out-of-bounds read).Clamp
nr_plidsto the number of descriptors the buffer can actually hold.Fixes 30b5f20bb2dd ("nvme: register fdp parameters with the block layer")Signed-off-by Guixin Liu <[email protected]>--- drivers/nvme/host/core.c | 1 + 1 file changed, 1 insertion(+)drivers/nvme/host/core.c +0
-0
drivers/nvme/host/core.c static int nvme_query_fdp_info(struct nvme_ns *ns, struct nvme_ns_info *info) · line 2364
Quoted 7 lines
drivers/nvme/host/core.c +1
-0@@ -2358,6 +2358,7 @@ static int nvme_query_fdp_info(struct nvme_ns *ns, struct nvme_ns_info *info) } head->nr_plids =le16_to_cpu (ruhs->nruhsd); + head->nr_plids =min_t (u16, head->nr_plids,S8_MAX -1 );if (!head->nr_plids) goto free;
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
Re: [PATCH 4/4] nvme: clamp FDP placement handle count to the buffer size
In reply to Guixin LiuQuoted 20 lines · Guixin Liu
nvme_query_fdp_info()allocates the RUH status buffer for at mostS8_MAX- 1 descriptors and caps the io-mgmt-receive transfer to that size.head->nr_plids, however, is taken verbatim from the device-supplied nruhsd field, which can be up to 65535. If a non-conformant or malicious device reports more descriptors than the buffer holds, the copy loop reads past the end of the ruhs buffer (heap out-of-bounds read).Clamp
nr_plidsto the number of descriptors the buffer can actually hold.Fixes 30b5f20bb2dd ("nvme: register fdp parameters with the block layer")Signed-off-by Guixin Liu<[email protected]>--- drivers/nvme/host/core.c | 1 + 1 file changed, 1 insertion(+)drivers/nvme/host/core.c +0
-0
drivers/nvme/host/core.c static int nvme_query_fdp_info(struct nvme_ns *ns, struct nvme_ns_info *info) · line 2361
Quoted 4 lines
drivers/nvme/host/core.c +1
-0@@ -2358,6 +2358,7 @@ static int nvme_query_fdp_info(struct nvme_ns *ns, struct nvme_ns_info *info) } head->nr_plids =le16_to_cpu (ruhs->nruhsd); + head->nr_plids =min_t (u16, head->nr_plids,S8_MAX -1 );
Given what we have at this point, the fix is fine.
But I feel the need to improve (what we have) with a follow-up patch:
1. Revise the cap, from S8_MAX - 1 to U8_MAX; since bio->bi_write_hint is u8.
2. And a dev_warn() to know if head->nr_plids go above the kernel-defined cap. It's possible/fine for device to have more placement ids, and if that happens, this can tell.
Keith, Christoph - thoughts?
Christoph Hellwig
maintainerRe: [PATCH 4/4] nvme: clamp FDP placement handle count to the buffer size
Quoted 3 lines · Kanchan Joshi
But I feel the need to improve (what we have) with a follow-up patch:
1. Revise the cap, fromS8_MAX- 1 toU8_MAX; sincebio->bi_write_hintis u8.
Signed 8-bit -1 is indeed a really odd value, and from looking at the original commit I'm not really sure how we arrived there.
Quoted 3 lines
2. And a
dev_warn()to know ifhead->nr_plidsgo above the kernel-defined cap. It's possible/fine for device to have more placement ids, and if that happens, this can tell.
Yeah.
Keith Busch
maintainerRe: [PATCH 4/4] nvme: clamp FDP placement handle count to the buffer size
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Kanchan Joshi
But I feel the need to improve (what we have) with a follow-up patch:
1. Revise the cap, fromS8_MAX- 1 toU8_MAX; sincebio->bi_write_hintis u8.Signed 8-bit -1 is indeed a really odd value, and from looking at the original commit I'm not really sure how we arrived there.
"S8_MAX - 1" is a weird way to represent it, but the reason that value was used (and it's admittedly not a good reason) was that the result fits in a page. Not that there's any restriction on admin commands using PRP lists; it's just a coincidental limit for all the driver initiated admin commands.
Re: [PATCH 4/4] nvme: clamp FDP placement handle count to the buffer size
Quoted 5 lines · Keith Busch
Quoted 4 lines · Christoph Hellwig
Quoted 3 lines · Kanchan Joshi
But I feel the need to improve (what we have) with a follow-up patch:
1. Revise the cap, fromS8_MAX- 1 toU8_MAX; sincebio->bi_write_hintis u8.
Quoted 7 lines
Quoted 2 lines
Signed 8-bit -1 is indeed a really odd value, and from looking at the original commit I'm not really sure how we arrived there.
"
S8_MAX- 1" is a weird way to represent it, but the reason that value was used (and it's admittedly not a good reason) was that the result fits in a page. Not that there's any restriction on admin commands using PRP lists; it's just a coincidental limit for all the driver initiated admin commands.
I will send another patch build on top of the patch4 to:
1. Change "S8_MAX -1" to U8_MAX
2. Add a warning log
Best Regards,
Guixin Liu
Re: [PATCH 4/4] nvme: clamp FDP placement handle count to the buffer size
In reply to Guixin LiuQuoted 8 lines · Guixin Liu
nvme_query_fdp_info()allocates the RUH status buffer for at mostS8_MAX- 1 descriptors and caps the io-mgmt-receive transfer to that size.head->nr_plids, however, is taken verbatim from the device-supplied nruhsd field, which can be up to 65535. If a non-conformant or malicious device reports more descriptors than the buffer holds, the copy loop reads past the end of the ruhs buffer (heap out-of-bounds read).Clamp
nr_plidsto the number of descriptors the buffer can actually hold.
Looks good:
Re: [PATCH 4/4] nvme: clamp FDP placement handle count to the buffer size
In reply to Guixin LiuQuoted 11 lines · Guixin Liu
nvme_query_fdp_info()allocates the RUH status buffer for at mostS8_MAX- 1 descriptors and caps the io-mgmt-receive transfer to that size.head->nr_plids, however, is taken verbatim from the device-supplied nruhsd field, which can be up to 65535. If a non-conformant or malicious device reports more descriptors than the buffer holds, the copy loop reads past the end of the ruhs buffer (heap out-of-bounds read).Clamp
nr_plidsto the number of descriptors the buffer can actually hold.Fixes 30b5f20bb2dd ("nvme: register fdp parameters with the block layer")Signed-off-by Guixin Liu<[email protected]>
Looks good to me.