Skip to contentSkip to conversation

nvme: Fix u32 overflow in the number of namespace list calculation

PATCH Superseded Jaesoo Lee 3 participants 4 messages Jun 3, 2019 1 message not in the archive NVMe

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Jaesoo Lee

author
#1 lore raw

PATCH 1/1 nvme: Fix u32 overflow in the number of namespace list calculation

The Number of Namespaces (nn) field in the identify controller data structure is defined as u32 and the maximum allowed value in NVMe specification is 0xFFFFFFFEUL. This change fixes the possible overflow of the DIV_ROUND_UP() operation used in nvme_scan_ns_list() by casting the nn to u64.

Signed-off-by Jaesoo Lee <jalee at purestorage.com>
---
 drivers/nvme/host/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/core.c +0 -0
index 2c43e12..5bcf926 100644
@@ -3373,7 +3373,7 @@ static int nvme_scan_ns_list(struct nvme_ctrl

*ctrl, unsigned nn)

 {
        struct nvme_ns *ns;
        __le32 *ns_list;
-       unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
+       unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP((u64)nn, 1024);
        int ret = 0;

        ns_list = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);

--
2.7.4

#2 lore raw

PATCH 1/1 nvme: Fix u32 overflow in the number of namespace list calculation

Review

Quoted 2 lines
(quoted diff) +1 -1
-       unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
+       unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP((u64)nn, 1024);

This needs to be DIV64_U64_ROUND_UP(nn, 1024) to avoid issues on 32-bit kernels.

maintainer
#3 lore raw

PATCH 1/1 nvme: Fix u32 overflow in the number of namespace list calculation

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +1 -1
-       unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
+       unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP((u64)nn, 1024);

This needs to be DIV64_U64_ROUND_UP(nn, 1024) to avoid issues on 32-bit kernels.

Agreed.

author
#4 lore raw

PATCH 1/1 nvme: Fix u32 overflow in the number of namespace list calculation

Thanks for pointing that out; I was totally unaware of that.

I just pushed a revision, but using DIV_ROUND_UP_ULL() instead of DIV64_U64_ROUND_UP(), because I found DIV_ROUND_UP_ULL() is more popular.

Please let me know we prefer DIV64_U64_ROUND_UP().

Quoted 8 lines · Sagi Grimberg
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +1 -1
-       unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
+       unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP((u64)nn, 1024);

This needs to be DIV64_U64_ROUND_UP(nn, 1024) to avoid issues on 32-bit kernels.

Agreed.