From: Benjamin Marzinski <bmarzins@redhat.com>
To: luoqing <l1138897701@163.com>
Cc: agk@redhat.com, snitzer@kernel.org, dm-devel@lists.linux.dev,
mpatocka@redhat.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] dm-bufio: Use struct_size() helper in kmalloc()
Date: Fri, 6 Mar 2026 18:47:53 -0500 [thread overview]
Message-ID: <aatnqYei263w5RHG@redhat.com> (raw)
In-Reply-To: <20260306080257.1268206-1-l1138897701@163.com>
On Fri, Mar 06, 2026 at 04:02:57PM +0800, luoqing wrote:
> From: luoqing <luoqing@kylinos.cn>
>
> Make use of the struct_size() helper instead of an open-coded version,
> in order to avoid any potential type mistakes or integer overflows that,
> in the worst scenario, could lead to heap overflows.
>
> Signed-off-by: luoqing <luoqing@kylinos.cn>
> ---
> drivers/md/dm-bufio.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/md/dm-bufio.c b/drivers/md/dm-bufio.c
> index 60f7badec91f..f8a6b423b4ea 100644
> --- a/drivers/md/dm-bufio.c
> +++ b/drivers/md/dm-bufio.c
> @@ -2511,7 +2511,7 @@ struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsign
> }
>
> num_locks = dm_num_hash_locks();
> - c = kzalloc(sizeof(*c) + (num_locks * sizeof(struct buffer_tree)), GFP_KERNEL);
> + c = kzalloc(struct_size(c, buffer_tree, num_locks), GFP_KERNEL);
buffer_tree isn't a member of struct dm_bufio_client (which is the type
of *c). Its a member of struct dm_buffer_cache (which is the type of
c->cache). Since struct_size() uses the same variable for referring to the
base structure and the structure holding the flex array, it won't work
here.
-Ben
> if (!c) {
> r = -ENOMEM;
> goto bad_client;
> --
> 2.25.1
prev parent reply other threads:[~2026-03-06 23:47 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-06 8:02 [PATCH 2/2] dm-bufio: Use struct_size() helper in kmalloc() luoqing
2026-03-06 17:14 ` kernel test robot
2026-03-06 19:07 ` kernel test robot
2026-03-06 23:47 ` Benjamin Marzinski [this message]
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=aatnqYei263w5RHG@redhat.com \
--to=bmarzins@redhat.com \
--cc=agk@redhat.com \
--cc=dm-devel@lists.linux.dev \
--cc=l1138897701@163.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mpatocka@redhat.com \
--cc=snitzer@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