From: David Laight <David.Laight@ACULAB.COM>
To: 'Alejandro Colomar' <alx@kernel.org>,
Kees Cook <keescook@chromium.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: RE: struct_size() using sizeof() vs offsetof()
Date: Mon, 21 Aug 2023 08:16:17 +0000 [thread overview]
Message-ID: <55bb7e4f633340db9a9c013b91afedd6@AcuMS.aculab.com> (raw)
In-Reply-To: <74e8cf91-d095-33e3-c548-34d80b691089@kernel.org>
From: Alejandro Colomar
> Sent: Thursday, August 17, 2023 1:23 AM
>
> Hi Kees, Gustavo,
>
> I've been discussing with a friend about the appropriateness of sizeof()
> vs offsetof() for calculating the size of a structure with a flexible
> array member (FAM).
>
> After reading Jens Gustedt's blog post about it[1], we tried some tests,
> and we got some interesting results that discouraged me from using sizeof().
> See below.
>
> But then, said friend pointed to me that the kernel uses sizeof() in
> struct_size(), and we wondered why you would have chosen it. It's safe
> as long as you _know_ that there's no padding, or that the alignment of
> the FAM is as large as the padding (which you probably know in the kernel),
> but it seems safer to use
>
> MAX(sizeof(s), offsetof(s, fam) + sizeof_member(s, fam) * count)
>
> The thing is, if there's any trailing padding in the struct, the FAM may
> overlap the padding, and the calculation with sizeof() will waste a few
> bytes, and if misused to get the location of the FAM, the problem will be
> bigger, as you'll get a wrong location.
>
> So, I just wanted to pry what and especially why the kernel chose to prefer
> a simple sizeof().
>
> Cheers,
> Alex
>
> ---
.....
> strcpy(s->fam, "Hello, sizeof!");
> p = (char *) s + sizeof(struct s);
> puts(p);
Why on earth would you expect the above to do anything sensible?
It is a shame you can just use offsetof(type, member[count + 1]).
That is fine for constants, but the C language requires offsetof()
to be a compile-time constant - I can't help feeling the standards
body didn't consider non-constant array offsets.
(The compiler for a well known OS won't compile that (or anything
that looks like it) even for a constant array subscript!)
The actual problem with using offsetof() is that you might end
up with something smaller than the structure size.
(When the variable sized array is smaller than the padding.)
While max() will generate a constant for constant input, it
will be a real compare for non-constant input.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2023-08-21 8:16 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-17 0:23 struct_size() using sizeof() vs offsetof() Alejandro Colomar
2023-08-17 3:05 ` 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
2023-08-21 13:51 ` Alejandro Colomar
2023-08-21 8:16 ` David Laight [this message]
2023-08-21 13:45 ` 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=55bb7e4f633340db9a9c013b91afedd6@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=alx@kernel.org \
--cc=gustavoars@kernel.org \
--cc=keescook@chromium.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