Skip to contentSkip to conversation

dm ioctl: Use struct_size() helper

PATCH Gustavo A. R. Silva 2 participants 2 messages Jun 16, 2020 – Jun 17, 2020 1 message not in the archive Device mapper

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

Hi all,

Friendly ping: who can take this?

It's been almost a year... and I just noticed there was a problem with the email addresses back then...

I just fixed the issue and this patch should now appear on [email protected] and LKML.

Thanks
--
Gustavo

Quoted 41 lines · Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct dm_target_deps {
	...
        __u64 dev[0];   /* out */
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following form:

sizeof(*deps) + (sizeof(*deps->dev) * count)

with:

struct_size(deps, dev, count)

This code was detected with the help of Coccinelle.

---
 drivers/md/dm-ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-ioctl.c +1 -1
index fb6f8fb1f13d..b2d52cec70d4 100644
@@ -1446,7 +1446,7 @@ static void retrieve_deps(struct dm_table *table,
 	/*
 	 * Check we have enough space.
 	 */
-	needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
+	needed = struct_size(deps, dev, count);
 	if (len < needed) {
 		param->flags |= DM_BUFFER_FULL_FLAG;
 		return;
#2 lore raw
Quoted 9 lines · Gustavo A. R. Silva

Hi all,

Friendly ping: who can take this?

It's been almost a year... and I just noticed there was a problem with the email addresses back then...

I just fixed the issue and this patch should now appear on [email protected] and LKML.

I don't see any resubmit from you on either list. But I've applied the fix by hand and attributed it to you.

Thanks,
Mike

Quoted 43 lines · Gustavo A. R. Silva
Quoted 41 lines

One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example:

struct dm_target_deps {
	...
        __u64 dev[0];   /* out */
};

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

So, replace the following form:

sizeof(*deps) + (sizeof(*deps->dev) * count)

with:

struct_size(deps, dev, count)

This code was detected with the help of Coccinelle.

---
 drivers/md/dm-ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-ioctl.c +1 -1
index fb6f8fb1f13d..b2d52cec70d4 100644
@@ -1446,7 +1446,7 @@ static void retrieve_deps(struct dm_table *table,
 	/*
 	 * Check we have enough space.
 	 */
-	needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
+	needed = struct_size(deps, dev, count);
 	if (len < needed) {
 		param->flags |= DM_BUFFER_FULL_FLAG;
 		return;