Skip to contentSkip to conversation

nvmet: prefer struct_size over open coded arithmetic

PATCH Applied Len Baker 3 participants 5 messages Oct 17, 2021 – Oct 23, 2021

Subsystems (2)
lore
Share & tools
lore

Len Baker

author
#0 lore raw

PATCH 1/1 nvmet: prefer struct_size over open coded arithmetic

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

In this case this is not actually dynamic size: all the operands involved in the calculation are constant values. However it is better to refactor this anyway, just to keep the open-coded math idiom out of code.

So, use the struct_size() helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc() function.

This code was detected with the help of Coccinelle and audited and fixed manually.

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

---
Hi,

this patch is built against the linux-next tree (tag next-20211015).

Regards,
Len

 drivers/nvme/target/admin-cmd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/target/admin-cmd.c +2 -2
index aa6d84d8848e..4aa71625c86a 100644
@@ -278,8 +278,8 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
 	u16 status;
 
 	status = NVME_SC_INTERNAL;
-	desc = kmalloc(sizeof(struct nvme_ana_group_desc) +
-			NVMET_MAX_NAMESPACES * sizeof(__le32), GFP_KERNEL);
+	desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
+		       GFP_KERNEL);
 	if (!desc)
 		goto out;
 

--
2.25.1

#1 lore raw
Quoted 19 lines · Len Baker

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

In this case this is not actually dynamic size: all the operands involved in the calculation are constant values. However it is better to refactor this anyway, just to keep the open-coded math idiom out of code.

So, use the struct_size() helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc() function.

This code was detected with the help of Coccinelle and audited and fixed manually.

Quoted 7 lines

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

---
Hi,

this patch is built against the linux-next tree (tag next-20211015).

You don't need to include these lines in every patch. Just add [next] to the subject line, like this:

[PATCH][next] nvmet: prefer struct_size over open coded arithmetic

It should be clear enough for people that you are talking about linux-next. And in case someone asks, then you proceed to clarify. :)

Quoted 12 lines

Regards,
Len

 drivers/nvme/target/admin-cmd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/target/admin-cmd.c +0 -0
index aa6d84d8848e..4aa71625c86a 100644

drivers/nvme/target/admin-cmd.c static void nvmet_execute_get_log_page_ana(struct nvmet_req *req) · line 282

Quoted 7 lines
drivers/nvme/target/admin-cmd.c +2 -2
@@ -278,8 +278,8 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
 	u16 status;
 
 	status = NVME_SC_INTERNAL;
-	desc = kmalloc(sizeof(struct nvme_ana_group_desc) +
-			NVMET_MAX_NAMESPACES * sizeof(__le32), GFP_KERNEL);
+	desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
+		       GFP_KERNEL);

It might be worth exploring if the flexible array is actually needed, once the allocation is always determined by NVMET_MAX_NAMESPACES. Maybe it can be changed to the following and remove the dynamic allocation entirely?

	struct nvme_ana_group_desc {
		__le32  grpid;
		__le32  nnsids;
		__le64  chgcnt;
		__u8    state;
		__u8    rsvd17[15];
		__le32  nsids[NVMET_MAX_NAMESPACES];
	};

If the above is possible then (at least) these lines should be audited:

drivers/nvme/host/multipath.c-551- if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))

drivers/nvme/host/multipath.c-566- offset += sizeof(*desc);
drivers/nvme/host/multipath.c-567- if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))

If the flexible array remains, then this line could use flex_array_size():

drivers/nvme/host/multipath.c-555- nsid_buf_size = nr_nsids * sizeof(__le32);

struct_size() could be used here, as well:

drivers/nvme/host/multipath.c-847- ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
drivers/nvme/host/multipath.c:848: ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
drivers/nvme/host/multipath.c-849- ctrl->max_namespaces * sizeof(__le32);

drivers/nvme/target/admin-cmd.c:267: return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32);

Thanks
--
Gustavo

Quoted 6 lines
drivers/nvme/target/admin-cmd.c +0 -1
 	if (!desc)
 		goto out;
 
--

2.25.1

author
#2 lore raw

Hi Gustavo,

first of all, thanks for this review (and all others reviews as well) ;)

More below.

Quoted 28 lines · Gustavo A. R. Silva
Quoted 19 lines · Len Baker

As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors.

In this case this is not actually dynamic size: all the operands involved in the calculation are constant values. However it is better to refactor this anyway, just to keep the open-coded math idiom out of code.

So, use the struct_size() helper to do the arithmetic instead of the argument "size + count * size" in the kmalloc() function.

This code was detected with the help of Coccinelle and audited and fixed manually.

Quoted 7 lines

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

---
Hi,

this patch is built against the linux-next tree (tag next-20211015).

Quoted 7 lines

You don't need to include these lines in every patch. Just add [next] to the subject line, like this:

[PATCH][next] nvmet: prefer struct_size over open coded arithmetic

It should be clear enough for people that you are talking about linux-next. And in case someone asks, then you proceed to clarify. :)

Ok, understood. Thanks for the advise.

Quoted 24 lines
Quoted 11 lines

Regards,
Len

 drivers/nvme/target/admin-cmd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/target/admin-cmd.c +0 -0
index aa6d84d8848e..4aa71625c86a 100644

drivers/nvme/target/admin-cmd.c static void nvmet_execute_get_log_page_ana(struct nvmet_req *req) · line 282

Quoted 7 lines
drivers/nvme/target/admin-cmd.c +2 -2
@@ -278,8 +278,8 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
 	u16 status;
 
 	status = NVME_SC_INTERNAL;
-	desc = kmalloc(sizeof(struct nvme_ana_group_desc) +
-			NVMET_MAX_NAMESPACES * sizeof(__le32), GFP_KERNEL);
+	desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
+		       GFP_KERNEL);

It might be worth exploring if the flexible array is actually needed, once the allocation is always determined by NVMET_MAX_NAMESPACES. Maybe it can be changed to the following and remove the dynamic allocation entirely?

Quoted 8 lines
	struct nvme_ana_group_desc {
		__le32  grpid;
		__le32  nnsids;
		__le64  chgcnt;
		__u8    state;
		__u8    rsvd17[15];
		__le32  nsids[NVMET_MAX_NAMESPACES];
	};

What's the size limit for dynamic allocation vs stack allocation? I think that NVMET_MAX_NAMESPACES * sizeof(__le32) = 1024 * 4 = 4096 bytes is big enough (but I don't know if it is the correct way to think).

However, due to the following comment in the NVMET_MAX_NAMESPACES macro definition:

/*
* Nice round number that makes a list of nsids fit into a page.
* Should become tunable at some point in the future. */
#define NVMET_MAX_NAMESPACES 1024

I think that it is better to use the dynamic allocation since in the future the struct size could be dynamic.

Quoted 5 lines

If the above is possible then (at least) these lines should be audited:

drivers/nvme/host/multipath.c-551- if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))

Quoted 7 lines

drivers/nvme/host/multipath.c-566- offset += sizeof(*desc);
drivers/nvme/host/multipath.c-567- if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))

If the flexible array remains, then this line could use flex_array_size():

drivers/nvme/host/multipath.c-555- nsid_buf_size = nr_nsids * sizeof(__le32);

Ok. I didn't see it.

Quoted 6 lines

struct_size() could be used here, as well:

drivers/nvme/host/multipath.c-847- ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
drivers/nvme/host/multipath.c:848: ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
drivers/nvme/host/multipath.c-849- ctrl->max_namespaces * sizeof(__le32);

Sorry, but here it's not possible to use struct_size() due to

sizeof(struct nvme_ana_group_desc) + ctrl->max_namespaces * sizeof(__le32)

it's not one single element. The "sizeof(struct nvme_ana_group_desc)" is multiplied by "ctrl->nanagrpid" and then added "ctrl->max_namespaces * sizeof(__le32)".

Quoted 1 line

drivers/nvme/target/admin-cmd.c:267: return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32);

Ok. I forgot it. Apologies.

Again, thanks for your time and advises,
Len

#3 lore raw
Quoted 4 lines · Len Baker

Hi Gustavo,

first of all, thanks for this review (and all others reviews as well) ;)

No problem. :)

Quoted 2 lines

I think that it is better to use the dynamic allocation since in the future the struct size could be dynamic.

Yep; that seems sensible.

Quoted 2 lines

it's not one single element. The "sizeof(struct nvme_ana_group_desc)" is multiplied by "ctrl->nanagrpid" and then added "ctrl->max_namespaces * sizeof(__le32)".

You're right. The whole expression got me a bit confused.

Quoted 3 lines
Quoted 1 line

drivers/nvme/target/admin-cmd.c:267: return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32);

Ok. I forgot it. Apologies.

No apologies. Thanks for your patches.

Quoted 1 line

Again, thanks for your time and advises,

Anytime.

Thanks
--
Gustavo