nvme: fix FDP configuration log parsing
PATCH 1/1 nvme: fix FDP configuration log parsing
NUMFDPC in the FDP Configurations log (NVMe Base Specification, Figure 279) is a 0-based count of configuration descriptors. Valid fdpcidx values are 0 through the NUMFDPC field value inclusive.
Fix the off-by-one check which incorrectly accepts fdpcidx == NUMFDPC+1. Also validate descriptor sizes while walking the list so dsze == 0 or a descriptor past the log end cannot cause unbounded iteration or reads past the buffer.
--- drivers/nvme/host/core.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/nvme/host/core.c +14 -5
@@ -2231,7 +2231,8 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl,struct nvme_fdp_config_desc *desc;size_t size =sizeof (hdr);void *log, *end; -int i, n, ret; +int i, ret; +u16 numfdpc; ret =nvme_get_log_lsi (ctrl,0 ,NVME_LOG_FDP_CONFIGS ,0 ,NVME_CSI_NVM , &hdr, size,0 , info->endgid);
@@ -2262,10 +2263,10 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, goto out; } - n =le16_to_cpu (h->numfdpc) +1 ; -if (fdp_idx > n) { + numfdpc =le16_to_cpu (h->numfdpc); +if (fdp_idx > numfdpc) {dev_warn (ctrl->device,"FDP index:%d out of range:%d\n" , - fdp_idx, n); + fdp_idx, numfdpc);/* Proceed without registering FDP streams */ ret =0 ; goto out;
@@ -2275,7 +2276,15 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, desc = log; end = log + size -sizeof (*h);for (i =0 ; i < fdp_idx; i++) { - log +=le16_to_cpu (desc->dsze); +u16 dsze =le16_to_cpu (desc->dsze); + +if (!dsze || log + dsze > end) { +dev_warn (ctrl->device, +"FDP invalid config descriptor at index %d\n" , i); + ret =0 ; + goto out; + } + log += dsze; desc = log;if (log >= end) { dev_warn(ctrl->device,
Signature · 1 line
2.43.0
Keith Busch
maintainerdrivers/nvme/host/core.c static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, · line 2267
Quoted 8 lines · liuxixin
drivers/nvme/host/core.c +2
-2@@ -2262,10 +2263,10 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, goto out; } - n =le16_to_cpu (h->numfdpc) +1 ; -if (fdp_idx > n) { + numfdpc =le16_to_cpu (h->numfdpc); +if (fdp_idx > numfdpc) {
Can't this be fixed with just a simpler one-line change:
+ if (fdp_idx >= n) {
v2 cover nvme: fix FDP configuration log parsing
Hi Keith,
Thanks for the review. v2 uses the minimal bounds fix you suggested:
keep n = NUMFDPC + 1 and compare with >= instead of >.
The descriptor walk validation (dsze == 0 / past end of log) is unchanged from v1.
## Test plan
- Build: make M=drivers/nvme -j12 (linux-next, verified)
- Repro/validation (lab):
Tested on linux-next 7.1.0-rc4-next-20260521 with QEMU 8.2 nvme-subsys,fdp=on. With a single FDP configuration (NUMFDPC field 0), forcing fdpcidx=1 triggers FDP invalid config descriptor list on the unfixed driver, while the fix rejects it early with FDP index:1 out of range:1 and skips stream registration.
Thanks,
liuxixin
PATCH v2 1/1 nvme: fix FDP configuration log parsing
<[email protected]>
From: liuxixin <[email protected]>
Date: Wed, 27 May 2026 10:22:32 +0800
Subject: [PATCH v2 1/1] nvme: fix FDP configuration log parsing
The fdpcidx bounds check sets n = NUMFDPC + 1 but used > instead of >=, incorrectly accepting fdp_idx when it equals n (i.e. NUMFDPC + 1).
Also validate descriptor sizes while walking the list so dsze == 0 or a descriptor past the log end cannot cause unbounded iteration or reads past the buffer.
--- drivers/nvme/host/core.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/nvme/host/core.c +10 -2
@@ -2263,7 +2263,7 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, } n =le16_to_cpu (h->numfdpc) +1 ; -if (fdp_idx > n) { +if (fdp_idx >= n) {dev_warn (ctrl->device,"FDP index:%d out of range:%d\n" , fdp_idx, n);/* Proceed without registering FDP streams */
@@ -2275,7 +2275,15 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, desc = log; end = log + size -sizeof (*h);for (i =0 ; i < fdp_idx; i++) { - log +=le16_to_cpu (desc->dsze); +u16 dsze =le16_to_cpu (desc->dsze); + +if (!dsze || log + dsze > end) { +dev_warn (ctrl->device, +"FDP invalid config descriptor at index %d\n" , i); + ret =0 ; + goto out; + } + log += dsze; desc = log;if (log >= end) { dev_warn(ctrl->device,
Signature · 1 line
2.43.0
Quoted 8 lines · liuxixin
<[email protected]>From: liuxixin <[email protected]>
Date: Wed, 27 May 2026 10:22:32 +0800
Subject: [PATCH v2 1/1] nvme: fix FDP configuration log parsingThe fdpcidx bounds check sets n = NUMFDPC + 1 but used > instead of >=, incorrectly accepting
fdp_idxwhen it equals n (i.e. NUMFDPC + 1).
Quoted 7 lines
Also validate descriptor sizes while walking the list so dsze == 0 or a descriptor past the log end cannot cause unbounded iteration or reads past the buffer.
Fixes 30b5f20bb2ddab013035399e5c7e6577da49320a ("nvme: register fdp parameters with the block layer")Signed-off-by liuxixin <[email protected]>
Christoph Hellwig
maintainerQuoted 7 lines · liuxixin
<[email protected]>From: liuxixin <[email protected]>
Date: Wed, 27 May 2026 10:22:32 +0800
Subject: [PATCH v2 1/1] nvme: fix FDP configuration log parsingThe fdpcidx bounds check sets n = NUMFDPC + 1 but used > instead of >=, incorrectly accepting
fdp_idxwhen it equals n (i.e. NUMFDPC + 1).
Looks good.
Quoted 4 lines
Also validate descriptor sizes while walking the list so dsze == 0 or a descriptor past the log end cannot cause unbounded iteration or reads past the buffer.
Also looks good, although I would have split that into a separate patch.
Quoted 1 line
Fixes 30b5f20bb2ddab013035399e5c7e6577da49320a ("nvme: register fdp parameters with the block layer")
Please shorten the sha id to 12 characters, i.e:
v3 cover nvme: fix FDP configuration log parsing
Hi Christoph, Keith, Nitesh,
Thanks for the reviews on v2.
v3 splits the series as Christoph suggested:
1/2 - fdpcidx bounds check (>= instead of >), with Fixes tag 2/2 - validate descriptor sizes while walking the log
1/2 also applies Keith's suggestion to use >= while keeping n = NUMFDPC + 1. Uses a 12-character Fixes commit id per Christoph's feedback, and includes Nitesh's Reviewed-by tags on both patches.
## Test plan
- Build: make M=drivers/nvme -j12 (linux-next, verified)
- QEMU (fdp-lab, tested as one patch before this split): linux-next 7.1-rc4, QEMU 8.2 nvme-subsys,fdp=on; fdpcidx=1 / NUMFDPC 0 - unfixed: invalid descriptor list; fixed: FDP index:1 out of range:1. v3 splits commits only; no functional change, no re-test.
Thanks,
liuxixin
PATCH v3 1/2 nvme: fix FDP fdpcidx bounds check
<[email protected]>
From: liuxixin <[email protected]>
Date: Thu, 28 May 2026 09:00:36 +0800
Subject: [PATCH v3 1/2] nvme: fix FDP fdpcidx bounds check
The fdpcidx bounds check sets n = NUMFDPC + 1 but used > instead of >=, incorrectly accepting fdp_idx when it equals n (i.e. NUMFDPC + 1).
--- drivers/nvme/host/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/core.c +1 -1
@@ -2263,7 +2263,7 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, } n =le16_to_cpu (h->numfdpc) +1 ; -if (fdp_idx > n) { +if (fdp_idx >= n) {dev_warn (ctrl->device,"FDP index:%d out of range:%d\n" , fdp_idx, n);/* Proceed without registering FDP streams */
Signature · 1 line
2.43.0
Re: [PATCH v3 1/2] nvme: fix FDP fdpcidx bounds check
Quoted 4 lines · liuxixin
<[email protected]>From: liuxixin <[email protected]>
Date: Thu, 28 May 2026 09:00:36 +0800
Subject: [PATCH v3 1/2] nvme: fix FDP fdpcidx bounds check
Somehow the mail format got messed up here.
Either way, the patch itself looks good:
PATCH v3 2/2 nvme: validate FDP configuration descriptor sizes
In reply to liuxixin<[email protected]>
From: liuxixin <[email protected]>
Date: Thu, 28 May 2026 09:00:37 +0800
Subject: [PATCH v3 2/2] nvme: validate FDP configuration descriptor sizes
Validate descriptor sizes while walking the FDP configurations log so dsze == 0 or a descriptor past the log end cannot cause unbounded iteration or reads past the buffer.
--- drivers/nvme/host/core.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +9 -1
@@ -2275,7 +2275,15 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, desc = log; end = log + size -sizeof (*h);for (i =0 ; i < fdp_idx; i++) { - log +=le16_to_cpu (desc->dsze); +u16 dsze =le16_to_cpu (desc->dsze); + +if (!dsze || log + dsze > end) { +dev_warn (ctrl->device, +"FDP invalid config descriptor at index %d\n" , i); + ret =0 ; + goto out; + } + log += dsze; desc = log;if (log >= end) { dev_warn(ctrl->device,
Signature · 1 line
2.43.0
Re: [PATCH v3 2/2] nvme: validate FDP configuration descriptor sizes
Quoted 4 lines · liuxixin
<[email protected]>From: liuxixin <[email protected]>
Date: Thu, 28 May 2026 09:00:37 +0800
Subject: [PATCH v3 2/2] nvme: validate FDP configuration descriptor sizes
Same formatting issue as the last one.
The patch itself looks good:
v4 cover nvme: fix FDP configuration log parsing
In reply to liuxixinHi Christoph, Keith, Nitesh,
v4 is a re-spin of v3 with no functional changes. Fixes the garbled mail format on v3 and adds Christoph's Reviewed-by tags on both patches (v3 already had Nitesh's Reviewed-by).
## Test plan
- Build: make M=drivers/nvme -j12 (linux-next, verified)
- QEMU (fdp-lab, tested as one patch before this split): linux-next 7.1-rc4, QEMU 8.2 nvme-subsys,fdp=on; fdpcidx=1 / NUMFDPC 0 - unfixed: invalid descriptor list; fixed: FDP index:1 out of range:1. v4 is tag/format only; no re-test.
Thanks,
liuxixin
PATCH v4 1/2 nvme: fix FDP fdpcidx bounds check
The fdpcidx bounds check sets n = NUMFDPC + 1 but used > instead of >=, incorrectly accepting fdp_idx when it equals n (i.e. NUMFDPC + 1).
--- drivers/nvme/host/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/core.c +1 -1
@@ -2263,7 +2263,7 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, } n =le16_to_cpu (h->numfdpc) +1 ; -if (fdp_idx > n) { +if (fdp_idx >= n) {dev_warn (ctrl->device,"FDP index:%d out of range:%d\n" , fdp_idx, n);/* Proceed without registering FDP streams */
Signature · 1 line
2.43.0
Re: [PATCH v4 1/2] nvme: fix FDP fdpcidx bounds check
Quoted 2 lines · liuxixin
The fdpcidx bounds check sets n = NUMFDPC + 1 but used > instead of >=, incorrectly accepting
fdp_idxwhen it equals n (i.e. NUMFDPC + 1).
Thanks, applied patch 1 to nvme-7.2.
Take a look at my omment on patch 2 and let me know if we should wait for a new version for that one.
PATCH v4 2/2 nvme: validate FDP configuration descriptor sizes
In reply to liuxixinValidate descriptor sizes while walking the FDP configurations log so dsze == 0 or a descriptor past the log end cannot cause unbounded iteration or reads past the buffer.
--- drivers/nvme/host/core.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +9 -1
@@ -2275,7 +2275,15 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, desc = log; end = log + size -sizeof (*h);for (i =0 ; i < fdp_idx; i++) { - log +=le16_to_cpu (desc->dsze); +u16 dsze =le16_to_cpu (desc->dsze); + +if (!dsze || log + dsze > end) { +dev_warn (ctrl->device, +"FDP invalid config descriptor at index %d\n" , i); + ret =0 ; + goto out; + } + log += dsze; desc = log;if (log >= end) { dev_warn(ctrl->device,
Signature · 1 line
2.43.0
Keith Busch
maintainerRe: [PATCH v4 2/2] nvme: validate FDP configuration descriptor sizes
Quoted 7 lines · liuxixin
drivers/nvme/host/core.c +2
-1@@ -2275,7 +2275,15 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, desc = log; end = log + size -sizeof (*h);for (i =0 ; i < fdp_idx; i++) { - log +=le16_to_cpu (desc->dsze); +u16 dsze =le16_to_cpu (desc->dsze); +
drivers/nvme/host/core.c static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, · line 2289
Quoted 10 lines
drivers/nvme/host/core.c +7
-0+if (!dsze || log + dsze > end) { +dev_warn (ctrl->device, +"FDP invalid config descriptor at index %d\n" , i); + ret =0 ; + goto out; + } + log += dsze; desc = log;if (log >= end) { dev_warn(ctrl->device,
I think you can delete this "log >= end" check now that you added the same check right above.
v5 cover nvme: validate FDP configuration descriptor sizes
Hi Keith,
Thanks for applying v4 1/2 to nvme-7.2.
v5 is only the descriptor-size validation patch, with your feedback on v4 2/2: remove the redundant "log >= end" check inside the walk loop now that dsze is validated before advancing.
## Test plan
- Build: make M=drivers/nvme -j$(nproc) (linux-next)
- fdp-lab: dsze==0 / walk past end -> "FDP invalid config descriptor at index %d" (see fdp-lab/TEST-FDP-BOUNDS.md)
Thanks,
liuxixin
PATCH v5 1/1 nvme: validate FDP configuration descriptor sizes
Validate descriptor sizes while walking the FDP configurations log so dsze == 0 or a descriptor past the log end cannot cause unbounded iteration or reads past the buffer.
--- drivers/nvme/host/core.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/nvme/host/core.c +6 -4
@@ -2275,14 +2275,16 @@ static int nvme_query_fdp_granularity(struct nvme_ctrl *ctrl, desc = log; end = log + size -sizeof (*h);for (i =0 ; i < fdp_idx; i++) { - log +=le16_to_cpu (desc->dsze); - desc = log; -if (log >= end) { +u16 dsze =le16_to_cpu (desc->dsze); + +if (!dsze || log + dsze > end) {dev_warn (ctrl->device, -"FDP invalid config descriptor list\n" ); +"FDP invalid config descriptor at index %d\n" , i); ret =0 ; goto out; } + log += dsze; + desc = log; }if (le32_to_cpu (desc->nrg) >1 ) {
Signature · 1 line
2.43.0
Re: [PATCH v5 1/1] nvme: validate FDP configuration descriptor sizes
Quoted 3 lines · liuxixin
Validate descriptor sizes while walking the FDP configurations log so dsze == 0 or a descriptor past the log end cannot cause unbounded iteration or reads past the buffer.
Thanks, applied to nvme-7.2.