Linux Hardening
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Alejandro Colomar' <alx@kernel.org>,
	"Gustavo A. R. Silva" <gustavo@embeddedor.com>,
	Kees Cook <keescook@chromium.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	"linux-hardening@vger.kernel.org"
	<linux-hardening@vger.kernel.org>
Subject: RE: struct_size() using sizeof() vs offsetof()
Date: Mon, 21 Aug 2023 08:38:43 +0000	[thread overview]
Message-ID: <d3c4c953c9a742ae98ae9b9036561b38@AcuMS.aculab.com> (raw)
In-Reply-To: <df8fdd4a-490f-6b2a-03b6-0333e3302dce@kernel.org>

From: Alejandro Colomar <alx@kernel.org>
> Sent: Thursday, August 17, 2023 7:38 PM
> 
> Hi Gustavo,
> 
> On 2023-08-17 18:05, Gustavo A. R. Silva wrote:
> >
> >> -               tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
> >> +               tp_c = kzalloc(struct_size(tp_c, hlist->ht, 1), GFP_KERNEL);
> >
> > I just sent a fix[1].
> >
> > Thanks for reporting this! :)

Perhaps struct_size() should include an assertion that:
	(offsetof(type, field[8]) > sizeof (type))
That will ensure that field is an array member and reasonably
near the end of the structure.

A more complex calculation (using _Alignof(type) and the offset/size
of field) could be used.
But I don't think you can actually detect it is field[] (or even the
last member).

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

  reply	other threads:[~2023-08-21  8:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <74e8cf91-d095-33e3-c548-34d80b691089@kernel.org>
2023-08-17  3:05 ` struct_size() using sizeof() vs offsetof() Kees Cook
2023-08-17 12:39   ` Alejandro Colomar
2023-08-17 16:05     ` Gustavo A. R. Silva
2023-08-17 18:37       ` Alejandro Colomar
2023-08-21  8:38         ` David Laight [this message]
2023-08-21 13:51           ` Alejandro Colomar

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=d3c4c953c9a742ae98ae9b9036561b38@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=alx@kernel.org \
    --cc=gustavo@embeddedor.com \
    --cc=gustavoars@kernel.org \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@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