Skip to contentSkip to conversation

block: bio: Use struct_size() in kmalloc()

PATCH Applied Gustavo A. R. Silva 3 participants 3 messages Jun 10, 2019 – Jun 15, 2019 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: bio: Use struct_size() in kmalloc()

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 bio_map_data {
	...
        struct iovec iov[];
};

instance = kmalloc(sizeof(sizeof(struct bio_map_data) + sizeof(struct iovec) *

                          count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kmalloc(struct_size(instance, iov, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

---
 block/bio.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
block/bio.c +1 -2
index 683cbb40f051..4bcdcd3f63f4 100644
@@ -1120,8 +1120,7 @@ static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,
 	if (data->nr_segs > UIO_MAXIOV)
 		return NULL;
 
-	bmd = kmalloc(sizeof(struct bio_map_data) +
-		       sizeof(struct iovec) * data->nr_segs, gfp_mask);
+	bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
 	if (!bmd)
 		return NULL;
 	memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 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 bio_map_data {
	...
        struct iovec iov[];
};

instance = kmalloc(sizeof(sizeof(struct bio_map_data) + sizeof(struct iovec) *

                          count, GFP_KERNEL);
Quoted 8 lines

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kmalloc(struct_size(instance, iov, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

-Kees

Quoted 21 lines
---
 block/bio.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
block/bio.c +1 -2
index 683cbb40f051..4bcdcd3f63f4 100644
@@ -1120,8 +1120,7 @@ static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,
 	if (data->nr_segs > UIO_MAXIOV)
 		return NULL;
 
-	bmd = kmalloc(sizeof(struct bio_map_data) +
-		       sizeof(struct iovec) * data->nr_segs, gfp_mask);
+	bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
 	if (!bmd)
 		return NULL;
 	memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
Signature · 1 line
2.21.0
Signature · 1 line
Kees Cook
maintainer Applied
#2 lore raw
In reply to Gustavo A. R. Silva
Quoted 10 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 bio_map_data {
	...
         struct iovec iov[];
};

instance = kmalloc(sizeof(sizeof(struct bio_map_data) + sizeof(struct iovec) *

Quoted 8 lines
                           count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kmalloc(struct_size(instance, iov, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Applied, thanks.

Signature · 1 line
Jens Axboe