nvme: core: reject invalid LBA data size from Identify Namespace
PATCH v3 1/1 nvme: core: reject invalid LBA data size from Identify Namespace
nvme_update_ns_info_block() trusts id->lbaf[lbaf].ds from the controller and assigns it directly to ns->head->lba_shift without bounds checking. nvme_lba_to_sect() then does:
return lba << (head->lba_shift - SECTOR_SHIFT);
When called with lba = le64_to_cpu(id->nsze) to compute the device capacity, an attacker-controlled controller can choose ds < 9 or a combination of (ds, nsze) that makes the left shift overflow sector_t. The former is a C undefined behaviour that UBSAN reports as a BUG; the latter silently yields a bogus capacity that the block layer then trusts for bounds checking.
Validate ds against SECTOR_SHIFT and use check_shl_overflow() to compute capacity so that any (ds, nsze) combination that would overflow sector_t is rejected. The namespace is skipped with -ENODEV instead of crashing the kernel. This is reachable by a malicious NVMe device, a buggy firmware, or an attacker-controlled NVMe-oF target.
The check is performed before queue_limits_start_update() and blk_mq_freeze_queue(), so the error path is a plain goto out with no cleanup needed.
Stack trace (UBSAN, ds < 9 variant):
RIP: nvme_lba_to_sect drivers/nvme/host/nvme.h:699 [inline] RIP: nvme_update_ns_info_block.cold+0x5/0x7 Call Trace: nvme_update_ns_info+0x175/0xd90 drivers/nvme/host/core.c:2467 nvme_validate_ns drivers/nvme/host/core.c:4299 [inline] nvme_scan_ns drivers/nvme/host/core.c:4350 nvme_scan_ns_async+0xa5/0xe0 drivers/nvme/host/core.c:4383 async_run_entry_fn process_one_work worker_thread kthread
Found by Syzkaller.
---
Changes since v2:
- Hoist the validation above queue_limits_start_update() and blk_mq_freeze_queue(); error path is now a plain goto out.
- Merge the ds == 0 case into the general invalid check.
v1: https://lore.kernel.org/linux-nvme/[email protected]/
v2: https://lore.kernel.org/linux-nvme/[email protected]/
drivers/nvme/host/core.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +11 -1
@@ -2404,12 +2404,22 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns, goto out; } +if (id->lbaf[lbaf].ds <SECTOR_SHIFT || +check_shl_overflow (le64_to_cpu (id->nsze), + id->lbaf[lbaf].ds -SECTOR_SHIFT , + &capacity)) { +dev_warn_once (ns->ctrl->device, +"invalid LBA data size %u, skipping namespace\n" , + id->lbaf[lbaf].ds); + ret = -ENODEV ; + goto out; + } + lim =queue_limits_start_update (ns->disk->queue); memflags =blk_mq_freeze_queue (ns->disk->queue); ns->head->lba_shift = id->lbaf[lbaf].ds; ns->head->nuse =le64_to_cpu (id->nuse); - capacity =nvme_lba_to_sect (ns->head,le64_to_cpu (id->nsze));nvme_set_ctrl_limits (ns->ctrl, &lim, false);nvme_configure_metadata (ns->ctrl, ns->head, id, nvm, info);nvme_set_chunk_sectors (ns, id, &lim);
Signature · 1 line
2.43.0
Quoted 13 lines · Chao Shi
nvme_update_ns_info_block()trusts id->lbaf[lbaf].ds from the controller and assigns it directly tons->head->lba_shiftwithout bounds checking.nvme_lba_to_sect()then does:return lba << (head->lba_shift - SECTOR_SHIFT);When called with lba =
le64_to_cpu(id->nsze)to compute the device capacity, an attacker-controlled controller can choose ds < 9 or a combination of (ds, nsze) that makes the left shift overflowsector_t. The former is a C undefined behaviour that UBSAN reports as a BUG; the latter silently yields a bogus capacity that the block layer then trusts for bounds checking.
Quoted 6 lines
Validate ds against
SECTOR_SHIFTand usecheck_shl_overflow()to compute capacity so that any (ds, nsze) combination that would overflowsector_tis rejected. The namespace is skipped with-ENODEVinstead of crashing the kernel. This is reachable by a malicious NVMe device, a buggy firmware, or an attacker-controlled NVMe-oF target.
Thanks, applied to nvme-7.2.
drivers/nvme/host/core.c static int nvme_update_ns_info_block(struct nvme_ns *ns, · line 2416
Quoted 10 lines · Chao Shi
drivers/nvme/host/core.c +9
-0+if (id->lbaf[lbaf].ds <SECTOR_SHIFT || + check_shl_overflow(le64_to_cpu(id->nsze),> + id->lbaf[lbaf].ds - SECTOR_SHIFT, + &capacity)) { +dev_warn_once (ns->ctrl->device, +"invalid LBA data size %u, skipping namespace\n" , + id->lbaf[lbaf].ds); + ret = -ENODEV ; + goto out; + }
JFYI, this is giving a C=1 warning:
drivers/nvme/host/core.c:2411:13: warning: unsigned value that used to be signed checked against zero? drivers/nvme/host/core.c:2411:13: signed value source
I can't seem to quieten it myself, though.
BTW, I would have thought that check_shl_overflow would catch id->lbaf[lbaf].ds < SECTOR_SHIFT (so that we don't need the extra check).
Keith Busch
maintainerQuoted 14 lines · John Garry
Review
Quoted 2 lines · Chao Shi
(quoted diff) +2
-0+ if (id->lbaf[lbaf].ds < SECTOR_SHIFT || + check_shl_overflow(le64_to_cpu(id->nsze),> + id->lbaf[lbaf].ds -
SECTOR_SHIFT,drivers/nvme/host/core.c static int nvme_update_ns_info_block(struct nvme_ns *ns, · line 2416
Quoted 7 lines
drivers/nvme/host/core.c +7
-0+ &capacity)) { +dev_warn_once (ns->ctrl->device, +"invalid LBA data size %u, skipping namespace\n" , + id->lbaf[lbaf].ds); + ret = -ENODEV ; + goto out; + }JFYI, this is giving a C=1 warning:
Quoted 7 lines
drivers/nvme/host/core.c:2411:13: warning: unsigned value that used to be signed checked against zero?drivers/nvme/host/core.c:2411:13: signed value sourceI can't seem to quieten it myself, though.
BTW, I would have thought that
check_shl_overflowwould catch id->lbaf[lbaf].ds <SECTOR_SHIFT(so that we don't need the extra check).
I see it too. check_shl_overflow has checks that suggest it was expecting a signed type, as all the < 0 checks don't make sense for unsigned. The warning seems harmless, but I'd too like to see it suppressed.
I think it's odd that I'm not seeing a similar error for the similar usage in generic_check_addressable() from fs/libfs.c. They look the same to me with respect to the types passed in.
Keith Busch
maintainerQuoted 23 lines · Keith Busch
Quoted 14 lines · John Garry
Review
Quoted 2 lines · Chao Shi
(quoted diff) +2
-0+ if (id->lbaf[lbaf].ds < SECTOR_SHIFT || + check_shl_overflow(le64_to_cpu(id->nsze),> + id->lbaf[lbaf].ds -
SECTOR_SHIFT,Review
Quoted 7 lines
(quoted diff) +7
-0+ &capacity)) { + dev_warn_once(ns->ctrl->device, + "invalid LBA data size %u, skipping namespace\n", + id->lbaf[lbaf].ds); + ret = -ENODEV; + goto out; + }JFYI, this is giving a C=1 warning:
Quoted 7 lines
drivers/nvme/host/core.c:2411:13: warning: unsigned value that used to be signed checked against zero?drivers/nvme/host/core.c:2411:13: signed value sourceI can't seem to quieten it myself, though.
BTW, I would have thought that
check_shl_overflowwould catch id->lbaf[lbaf].ds <SECTOR_SHIFT(so that we don't need the extra check).
Quoted 8 lines
I see it too.
check_shl_overflowhas checks that suggest it was expecting a signed type, as all the < 0 checks don't make sense for unsigned. The warning seems harmless, but I'd too like to see it suppressed.I think it's odd that I'm not seeing a similar error for the similar usage in
generic_check_addressable()fromfs/libfs.c. They look the same to me with respect to the types passed in.
It appears that sparse is having trouble with the type provenance of a __bitwise __le64 type. No idea why. As a test, I replaced the le64_to_cpu() to a u64 type on stack initialized to a random ULL value and the warning goes away. I say we can ignore the sparse warning, or we can rewrite this to avoid the check_shl_overflow entirely.
---
drivers/nvme/host/core.c +7 -6
@@ -2372,8 +2372,8 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,struct nvme_zone_info zi = {};struct nvme_id_ns *id;unsigned int memflags; -sector_t capacity; -unsigned lbaf; +unsigned lbaf, shift =0 ; +u64 capacity, nsze;int ret; ret =nvme_identify_ns (ns->ctrl, info->nsid, &id);
@@ -2407,10 +2407,13 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns, goto out; } -if (id->lbaf[lbaf].ds <SECTOR_SHIFT || -check_shl_overflow (le64_to_cpu (id->nsze), - id->lbaf[lbaf].ds -SECTOR_SHIFT , - &capacity)) { + nsze =le64_to_cpu (id->nsze); +if (id->lbaf[lbaf].ds >=SECTOR_SHIFT ) + shift = id->lbaf[lbaf].ds -SECTOR_SHIFT ; + +if (shift <SECTOR_SHIFT || shift >=64 || nsze >U64_MAX >> shift) {dev_warn_once (ns->ctrl->device,"invalid LBA data size %u, skipping namespace\n" , id->lbaf[lbaf].ds);
--
Quoted 29 lines · Keith Busch
Quoted 28 lines · Keith Busch
Quoted 14 lines · John Garry
Review
Quoted 2 lines · Chao Shi
(quoted diff) +2
-0+ if (id->lbaf[lbaf].ds < SECTOR_SHIFT || + check_shl_overflow(le64_to_cpu(id->nsze),> + id->lbaf[lbaf].ds -
SECTOR_SHIFT,Review
Quoted 7 lines
(quoted diff) +7
-0+ &capacity)) { + dev_warn_once(ns->ctrl->device, + "invalid LBA data size %u, skipping namespace\n", + id->lbaf[lbaf].ds); + ret = -ENODEV; + goto out; + }JFYI, this is giving a C=1 warning:
Quoted 7 lines
drivers/nvme/host/core.c:2411:13: warning: unsigned value that used to be signed checked against zero?drivers/nvme/host/core.c:2411:13: signed value sourceI can't seem to quieten it myself, though.
BTW, I would have thought that
check_shl_overflowwould catch id->lbaf[lbaf].ds <SECTOR_SHIFT(so that we don't need the extra check).I see it too.
check_shl_overflowhas checks that suggest it was expecting a signed type, as all the < 0 checks don't make sense for unsigned. The warning seems harmless, but I'd too like to see it suppressed.
Quoted 8 lines
Quoted 3 lines
I think it's odd that I'm not seeing a similar error for the similar usage in
generic_check_addressable()fromfs/libfs.c. They look the same to me with respect to the types passed in.It appears that sparse is having trouble with the type provenance of a
__bitwise__le64type. No idea why. As a test, I replaced thele64_to_cpu()to a u64 type on stack initialized to a random ULL value and the warning goes away.
Yeah, ditto.
Quoted 2 lines
I say we can ignore the sparse warning, or we
can rewrite this to avoid thecheck_shl_overflowentirely.
Since sparse is having problems with le64_to_cpu(), I suppose using your own version is ok. It would be nice to know the root cause of this issue, though...
cheers
Quoted 37 lines
---
drivers/nvme/host/core.c +7
-7@@ -2372,8 +2372,8 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,struct nvme_zone_info zi = {};struct nvme_id_ns *id;unsigned int memflags; -sector_t capacity; -unsigned lbaf; +unsigned lbaf, shift =0 ; +u64 capacity, nsze;int ret; ret =nvme_identify_ns (ns->ctrl, info->nsid, &id);@@ -2407,10 +2407,13 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns, goto out; } -if (id->lbaf[lbaf].ds <SECTOR_SHIFT || -check_shl_overflow (le64_to_cpu (id->nsze), - id->lbaf[lbaf].ds -SECTOR_SHIFT , - &capacity)) { + nsze =le64_to_cpu (id->nsze); +if (id->lbaf[lbaf].ds >=SECTOR_SHIFT ) + shift = id->lbaf[lbaf].ds -SECTOR_SHIFT ; + +if (shift <SECTOR_SHIFT || shift >=64 || nsze >U64_MAX >> shift) {dev_warn_once (ns->ctrl->device,"invalid LBA data size %u, skipping namespace\n" , id->lbaf[lbaf].ds); --
Quoted 4 lines · John Garry
Quoted 3 lines
I say we can ignore the sparse warning, or we
can rewrite this to avoid thecheck_shl_overflowentirely.
FWIW, adding a separate function keeps sparse happy for me:
drivers/nvme/host/core.c +7 -4
@@ -2364,6 +2364,11 @@ static int nvme_query_fdp_info(struct nvme_ns *ns, struct nvme_ns_info *info)return ret; } +static bool nvme_valid_ds (u64 nsze,signed int shift,u64 *capacity) +{ +return check_shl_overflow (nsze, shift, capacity); +} +static int nvme_update_ns_info_block (struct nvme_ns *ns,struct nvme_ns_info *info) {
@@ -2407,10 +2412,8 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns, goto out; } -if (id->lbaf[lbaf].ds <SECTOR_SHIFT || -check_shl_overflow (le64_to_cpu (id->nsze), - id->lbaf[lbaf].ds -SECTOR_SHIFT , - &capacity)) { +if (nvme_valid_ds (le64_to_cpu (id->nsze), + id->lbaf[lbaf].ds -SECTOR_SHIFT , &capacity)) {dev_warn_once (ns->ctrl->device,"invalid LBA data size %u, skipping namespace\n" , id->lbaf[lbaf].ds);
Hi John, Keith,
Thanks for catching the sparse warning, and sorry for the delayed response.
Quoted 2 lines
BTW, I have thought that
check_shl_overflowwould catch id->lbaf[lbaf].ds <SECTOR_SHIFT(so that we don't need the extra check).
Confirmed -- check_shl_overflow() returns true for the negative shift that ds < SECTOR_SHIFT produces (_to_shift collapses to 0 and the _to_shift != _s test fires). I checked ds=0 and ds=8: both are still rejected with the explicit lower-bound test removed, so it is redundant.
For the C=1 warning, the minimal fix is to drop that redundant check and feed nsze through a plain u64 local -- as Keith found, laundering the le64_to_cpu() result through a non-__le64 type makes the warning go away:
u64 nsze;
...
nsze = le64_to_cpu(id->nsze);
if (check_shl_overflow(nsze, id->lbaf[lbaf].ds - SECTOR_SHIFT,
&capacity)) {dev_warn_once(...);
ret = -ENODEV;
goto out;
}
This keeps check_shl_overflow() in one tested helper and avoids a wrapper. John's nvme_valid_ds() works too; if we prefer that, I'd name it for its actual sense (it returns true on overflow, i.e. invalid), e.g. nvme_ds_overflows().
One note: I'd lean toward keeping check_shl_overflow() rather than open-coding the bound. It folds the lower-bound (negative shift) and the overflow case into one tested helper, so we don't have to re-derive the boundary by hand -- e.g. the lower bound is on ds itself, not on the post-subtract (ds - SECTOR_SHIFT) shift, which I found easy to get subtly wrong.
Keith, since v3 is already in your tree: do you want an incremental fixup on top, or a v4 to replace the applied commit? I have both ready.
Thanks,
Chao
Quoted 41 lines · John Garry
Quoted 4 lines · John Garry
Quoted 3 lines
I say we can ignore the sparse warning, or we
can rewrite this to avoid thecheck_shl_overflowentirely.FWIW, adding a separate function keeps sparse happy for me:
drivers/nvme/host/core.c +5
-0@@ -2364,6 +2364,11 @@ static int nvme_query_fdp_info(struct nvme_ns *ns, struct nvme_ns_info *info)return ret; } +static bool nvme_valid_ds (u64 nsze,signed int shift,u64 *capacity) +{ +return check_shl_overflow (nsze, shift, capacity); +} +static int nvme_update_ns_info_block (struct nvme_ns *ns,struct nvme_ns_info *info){drivers/nvme/host/core.c +2
-4@@ -2407,10 +2412,8 @@ static int nvme_update_ns_info_block(structnvme_ns *ns, goto out; } -if (id->lbaf[lbaf].ds <SECTOR_SHIFT || -check_shl_overflow (le64_to_cpu (id->nsze), - id->lbaf[lbaf].ds -SECTOR_SHIFT , - &capacity)) { +if (nvme_valid_ds (le64_to_cpu (id->nsze), + id->lbaf[lbaf].ds -SECTOR_SHIFT , &capacity)) { dev_warn_once(ns->ctrl->device, "invalid LBA data size %u, skipping namespace\n",id->lbaf[lbaf].ds);
Quoted 26 lines · Chao S
Quoted 2 lines
BTW, I have thought that
check_shl_overflowwould catch id->lbaf[lbaf].ds <SECTOR_SHIFT(so that we don't need the extra check).Confirmed --
check_shl_overflow()returns true for the negative shift that ds <SECTOR_SHIFTproduces (_to_shiftcollapses to 0 and the_to_shift!= _s test fires). I checked ds=0 and ds=8: both are still rejected with the explicit lower-bound test removed, so it is redundant.For the C=1 warning, the minimal fix is to drop that redundant check and feed nsze through a plain u64 local -- as Keith found, laundering the
le64_to_cpu()result through a non-__le64 type makes the warning go away:u64 nsze;
...
nsze =le64_to_cpu(id->nsze);
if (check_shl_overflow(nsze, id->lbaf[lbaf].ds -SECTOR_SHIFT,&capacity)) {
dev_warn_once(...);
ret =-ENODEV;
goto out;
}This keeps
check_shl_overflow()in one tested helper and avoids a wrapper. John'snvme_valid_ds()works too; if we prefer that, I'd name it for its actual sense (it returns true on overflow, i.e. invalid), e.g.nvme_ds_overflows().
Quoted 6 lines
One note: I'd lean toward keeping
check_shl_overflow()rather than open-coding the bound. It folds the lower-bound (negative shift) and the overflow case into one tested helper, so we don't have to re-derive the boundary by hand -- e.g. the lower bound is on ds itself, not on the post-subtract (ds -SECTOR_SHIFT) shift, which I found easy to get subtly wrong.
What exactly is your proposed change (to what is in the tree)?
Quoted 4 lines
Keith, since v3 is already in your tree: do you want an incremental fixup on top, or a v4 to replace the applied commit? I have both ready.
Hi John,
Here is the concrete change (drivers/nvme/host/core.c, nvme_update_ns_info_block()):
unsigned int memflags;
sector_t capacity;
unsigned lbaf;
+ u64 nsze;
int ret;
...
- if (id->lbaf[lbaf].ds < SECTOR_SHIFT ||
- check_shl_overflow(le64_to_cpu(id->nsze),
- id->lbaf[lbaf].ds - SECTOR_SHIFT,
- &capacity)) {
+ /*
+ * check_shl_overflow() also rejects a data size below SECTOR_SHIFT,
+ * as that makes the shift count negative, so no separate lower-bound
+ * test is needed. Feed nsze through a plain u64 so sparse does not
+ * trip over the __le64 provenance of le64_to_cpu().
+ */
+ nsze = le64_to_cpu(id->nsze);
+ if (check_shl_overflow(nsze, id->lbaf[lbaf].ds - SECTOR_SHIFT,
+ &capacity)) {
dev_warn_once(ns->ctrl->device,
"invalid LBA data size %u, skipping namespace\n", id->lbaf[lbaf].ds);
ret = -ENODEV;
goto out;
}
Two things:
1. Drops the explicit ds < SECTOR_SHIFT test as redundant -- check_shl_overflow() already returns true for the resulting negative shift. I confirmed ds=0 and ds=8 are still rejected without it.
2. Routes le64_to_cpu(id->nsze) through a plain u64 local, which is what silences the C=1 warning (sparse loses the __le64 provenance when the value is passed straight into check_shl_overflow()).
Behavior is otherwise unchanged. Since the original is already in v7.2-rc1, I'll send this as a standalone patch on top of mainline (Reported-by: you) unless you'd prefer a different form.
Thanks,
Chao
Quoted 44 lines · John Garry
Quoted 26 lines · Chao S
Quoted 2 lines
BTW, I have thought that
check_shl_overflowwould catch id->lbaf[lbaf].ds <SECTOR_SHIFT(so that we don't need the extra check).Confirmed --
check_shl_overflow()returns true for the negative shift that ds <SECTOR_SHIFTproduces (_to_shiftcollapses to 0 and the_to_shift!= _s test fires). I checked ds=0 and ds=8: both are still rejected with the explicit lower-bound test removed, so it is redundant.For the C=1 warning, the minimal fix is to drop that redundant check and feed nsze through a plain u64 local -- as Keith found, laundering the
le64_to_cpu()result through a non-__le64 type makes the warning go away:u64 nsze;
...
nsze =le64_to_cpu(id->nsze);
if (check_shl_overflow(nsze, id->lbaf[lbaf].ds -SECTOR_SHIFT,&capacity)) {
dev_warn_once(...);
ret =-ENODEV;
goto out;
}This keeps
check_shl_overflow()in one tested helper and avoids a wrapper. John'snvme_valid_ds()works too; if we prefer that, I'd name it for its actual sense (it returns true on overflow, i.e. invalid), e.g.nvme_ds_overflows().Quoted 6 lines
One note: I'd lean toward keeping
check_shl_overflow()rather than open-coding the bound. It folds the lower-bound (negative shift) and the overflow case into one tested helper, so we don't have to re-derive the boundary by hand -- e.g. the lower bound is on ds itself, not on the post-subtract (ds -SECTOR_SHIFT) shift, which I found easy to get subtly wrong.What exactly is your proposed change (to what is in the tree)?
Quoted 4 lines
Keith, since v3 is already in your tree: do you want an incremental fixup on top, or a v4 to replace the applied commit? I have both ready.
This response took almost 3 weeks. The previous response took again almost 3 weeks. kernel development may be relatively slow moving, but it's not that slow. You need to respond much more promptly to keep up with current development.
Quoted 36 lines
Here is the concrete change (
drivers/nvme/host/core.c,nvme_update_ns_info_block()):unsigned int memflags; sector_t capacity; unsigned lbaf; + u64 nsze; int ret;...
- if (id->lbaf[lbaf].ds <SECTOR_SHIFT||(quoted diff) +9
-3- check_shl_overflow(le64_to_cpu(id->nsze), - id->lbaf[lbaf].ds - SECTOR_SHIFT, - &capacity)) { + /* + * check_shl_overflow() also rejects a data size below SECTOR_SHIFT, + * as that makes the shift count negative, so no separate lower-bound + * test is needed. Feed nsze through a plain u64 so sparse does not + * trip over the __le64 provenance of le64_to_cpu(). + */ + nsze = le64_to_cpu(id->nsze); + if (check_shl_overflow(nsze, id->lbaf[lbaf].ds - SECTOR_SHIFT, + &capacity)) { dev_warn_once(ns->ctrl->device, "invalid LBA data size %u, skipping namespace\n", id->lbaf[lbaf].ds); ret = -ENODEV; goto out; }Two things:
1. Drops the explicit ds <
SECTOR_SHIFTtest as redundant --check_shl_overflow()already returns true for the resulting negative shift. I confirmed ds=0 and ds=8 are still rejected without it.
Quoted 7 lines
2. Routes
le64_to_cpu(id->nsze)through a plain u64 local, which is what silences the C=1 warning (sparse loses the__le64provenance when the value is passed straight intocheck_shl_overflow()).Behavior is otherwise unchanged. Since the original is already in v7.2-rc1, I'll send this as a standalone patch on top of mainline (Reported-by: you) unless you'd prefer a different form.
My fix is now in mainline https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v7.2-rc3&id=92f58587a04c94985fd4a9e3575720b054c432bf, so any change which you want to make must be on top of that.
Quoted 49 lines
Thanks,
ChaoQuoted 44 lines · John Garry
Quoted 26 lines · Chao S
Quoted 2 lines
BTW, I have thought that
check_shl_overflowwould catch id->lbaf[lbaf].ds <SECTOR_SHIFT(so that we don't need the extra check).Confirmed --
check_shl_overflow()returns true for the negative shift that ds <SECTOR_SHIFTproduces (_to_shiftcollapses to 0 and the_to_shift!= _s test fires). I checked ds=0 and ds=8: both are still rejected with the explicit lower-bound test removed, so it is redundant.For the C=1 warning, the minimal fix is to drop that redundant check and feed nsze through a plain u64 local -- as Keith found, laundering the
le64_to_cpu()result through a non-__le64 type makes the warning go away:u64 nsze;
...
nsze =le64_to_cpu(id->nsze);
if (check_shl_overflow(nsze, id->lbaf[lbaf].ds -SECTOR_SHIFT,&capacity)) {
dev_warn_once(...);
ret =-ENODEV;
goto out;
}This keeps
check_shl_overflow()in one tested helper and avoids a wrapper. John'snvme_valid_ds()works too; if we prefer that, I'd name it for its actual sense (it returns true on overflow, i.e. invalid), e.g.nvme_ds_overflows().Quoted 6 lines
One note: I'd lean toward keeping
check_shl_overflow()rather than open-coding the bound. It folds the lower-bound (negative shift) and the overflow case into one tested helper, so we don't have to re-derive the boundary by hand -- e.g. the lower bound is on ds itself, not on the post-subtract (ds -SECTOR_SHIFT) shift, which I found easy to get subtly wrong.What exactly is your proposed change (to what is in the tree)?
Quoted 4 lines
Keith, since v3 is already in your tree: do you want an incremental fixup on top, or a v4 to replace the applied commit? I have both ready.
Hi John,
I apologize for the delayed response. I may have missed the notifications, since at the same time, we have some crashes and bug patches working in parallel. So the response process is a little slower. Thanks for letting me know
the newest fixup. Apologize again!
Best,
Chao
Quoted 104 lines · John Garry
This response took almost 3 weeks. The previous response took again almost 3 weeks. kernel development may be relatively slow moving, but it's not that slow. You need to respond much more promptly to keep up with current development.
Quoted 36 lines
Here is the concrete change (
drivers/nvme/host/core.c,nvme_update_ns_info_block()):unsigned int memflags; sector_t capacity; unsigned lbaf; + u64 nsze; int ret;...
- if (id->lbaf[lbaf].ds <SECTOR_SHIFT||(quoted diff) +9
-3- check_shl_overflow(le64_to_cpu(id->nsze), - id->lbaf[lbaf].ds - SECTOR_SHIFT, - &capacity)) { + /* + * check_shl_overflow() also rejects a data size below SECTOR_SHIFT, + * as that makes the shift count negative, so no separate lower-bound + * test is needed. Feed nsze through a plain u64 so sparse does not + * trip over the __le64 provenance of le64_to_cpu(). + */ + nsze = le64_to_cpu(id->nsze); + if (check_shl_overflow(nsze, id->lbaf[lbaf].ds - SECTOR_SHIFT, + &capacity)) { dev_warn_once(ns->ctrl->device, "invalid LBA data size %u, skipping namespace\n", id->lbaf[lbaf].ds); ret = -ENODEV; goto out; }Two things:
1. Drops the explicit ds <
SECTOR_SHIFTtest as redundant --check_shl_overflow()already returns true for the resulting negative shift. I confirmed ds=0 and ds=8 are still rejected without it.Quoted 7 lines
2. Routes
le64_to_cpu(id->nsze)through a plain u64 local, which is what silences the C=1 warning (sparse loses the__le64provenance when the value is passed straight intocheck_shl_overflow()).Behavior is otherwise unchanged. Since the original is already in v7.2-rc1, I'll send this as a standalone patch on top of mainline (Reported-by: you) unless you'd prefer a different form.
My fix is now in mainline
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v7.2-rc3&id=92f58587a04c94985fd4a9e3575720b054c432bf,
so any change which you want to make must be on top of that.Quoted 49 lines
Thanks,
ChaoQuoted 44 lines · John Garry
Quoted 26 lines · Chao S
Quoted 2 lines
BTW, I have thought that
check_shl_overflowwould catch id->lbaf[lbaf].ds <SECTOR_SHIFT(so that we don't need the extra check).Confirmed --
check_shl_overflow()returns true for the negative shift that ds <SECTOR_SHIFTproduces (_to_shiftcollapses to 0 and the_to_shift!= _s test fires). I checked ds=0 and ds=8: both are still rejected with the explicit lower-bound test removed, so it is redundant.For the C=1 warning, the minimal fix is to drop that redundant check and feed nsze through a plain u64 local -- as Keith found, laundering the
le64_to_cpu()result through a non-__le64 type makes the warning go away:u64 nsze;
...
nsze =le64_to_cpu(id->nsze);
if (check_shl_overflow(nsze, id->lbaf[lbaf].ds -SECTOR_SHIFT,&capacity)) {
dev_warn_once(...);
ret =-ENODEV;
goto out;
}This keeps
check_shl_overflow()in one tested helper and avoids a wrapper. John'snvme_valid_ds()works too; if we prefer that, I'd name it for its actual sense (it returns true on overflow, i.e. invalid), e.g.nvme_ds_overflows().Quoted 6 lines
One note: I'd lean toward keeping
check_shl_overflow()rather than open-coding the bound. It folds the lower-bound (negative shift) and the overflow case into one tested helper, so we don't have to re-derive the boundary by hand -- e.g. the lower bound is on ds itself, not on the post-subtract (ds -SECTOR_SHIFT) shift, which I found easy to get subtly wrong.What exactly is your proposed change (to what is in the tree)?
Quoted 4 lines
Keith, since v3 is already in your tree: do you want an incremental fixup on top, or a v4 to replace the applied commit? I have both ready.