linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
Cc: Jens Axboe <axboe@kernel.dk>,
	linux-block@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] block: bio: Use struct_size() in kmalloc()
Date: Mon, 10 Jun 2019 10:53:08 -0700	[thread overview]
Message-ID: <201906101053.6B73D8906@keescook> (raw)
In-Reply-To: <20190610150412.GA8430@embeddedor>

On Mon, Jun 10, 2019 at 10:04:12AM -0500, Gustavo A. R. Silva wrote:
> 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.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>

Reviewed-by: Kees Cook <keescook@chromium.org>

-Kees

> ---
>  block/bio.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/block/bio.c b/block/bio.c
> index 683cbb40f051..4bcdcd3f63f4 100644
> --- a/block/bio.c
> +++ b/block/bio.c
> @@ -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);
> -- 
> 2.21.0
> 

-- 
Kees Cook

  reply	other threads:[~2019-06-10 17:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-10 15:04 [PATCH] block: bio: Use struct_size() in kmalloc() Gustavo A. R. Silva
2019-06-10 17:53 ` Kees Cook [this message]
2019-06-15  7:45 ` Jens Axboe
  -- strict thread matches above, loose matches on Subject: below --
2019-05-17  9:12 [PATCH] block: bio: use " xiaolinkui
2019-05-17 17:18 ` Chaitanya Kulkarni
2019-05-17 21:17 ` Jens Axboe
2019-05-17 22:59   ` Jens Axboe
2019-05-18  0:43     ` Chaitanya Kulkarni
2019-05-18  4:16       ` Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201906101053.6B73D8906@keescook \
    --to=keescook@chromium.org \
    --cc=axboe@kernel.dk \
    --cc=gustavo@embeddedor.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).