From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
To: Bence Csokas <bence.csokas@arm.com>,
linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Mark Rutland <mark.rutland@arm.com>, Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>
Subject: Re: [PATCH] overflow: Support sub-union members in struct_size()
Date: Tue, 28 Jul 2026 12:57:54 -0600 [thread overview]
Message-ID: <3aa0853e-278f-4825-b51e-42eb437111bf@embeddedor.com> (raw)
In-Reply-To: <20260728114912.122417-3-bence.csokas@arm.com>
On 7/28/26 05:49, Bence Csokas wrote:
> Original definition of struct_size() assumed that the total size of an
> object with a flex-array member can be calculated simply by adding the
> flex-array size to what sizeof() returns. This is however not the case
> if the struct has a union inside it, and `member` refers to a field
> inside that sub-union:
>
> struct my_inner {
> int a;
> char b[];
> };
>
> struct my_outer {
> char c;
> union {
> struct my_inner inner;
> long l;
> };
> };
>
> /* This will give the wrong result */
> struct_size_t(struct my_outer, inner.b, n_elems);
>
> Replacing sizeof() by the offsetof() of the member fixes this.
>
> Existing users specifying non-union members should not be affected.
This actually affects users of flexible structs that have tail padding[1].
-Gustavo
[1] https://godbolt.org/z/KPq59EYej
>
> Cc: Mark Rutland <mark.rutland@arm.com>
>
> Signed-off-by: Bence Csokas <bence.csokas@arm.com>
> ---
> include/linux/overflow.h | 4 ++--
> include/linux/stddef.h | 8 ++++++++
> 2 files changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> index a8cb6319b4fb..a11c1135f617 100644
> --- a/include/linux/overflow.h
> +++ b/include/linux/overflow.h
> @@ -433,8 +433,8 @@ static __always_inline size_t __must_check size_sub(size_t minuend, size_t subtr
> */
> #define struct_size(p, member, count) \
> __builtin_choose_expr(__is_constexpr(count), \
> - sizeof(*(p)) + flex_array_size(p, member, count), \
> - size_add(sizeof(*(p)), flex_array_size(p, member, count)))
> + offsetofp((p), member) + flex_array_size(p, member, count), \
> + size_add(offsetofp((p), member), flex_array_size(p, member, count)))
>
> /**
> * struct_size_t() - Calculate size of structure with trailing flexible array
> diff --git a/include/linux/stddef.h b/include/linux/stddef.h
> index e1851c50c89b..b8f0dbd2de55 100644
> --- a/include/linux/stddef.h
> +++ b/include/linux/stddef.h
> @@ -32,6 +32,14 @@ enum {
> #define offsetofend(TYPE, MEMBER) \
> (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
>
> +/**
> + * offsetofp() - Get the offset of a struct field from a pointer to the struct
> + *
> + * @P: Pointer to the structure
> + * @MEMBER: The member within the structure to get the end offset of
> + */
> +#define offsetofp(P, MEMBER) offsetof(typeof(*(P)), MEMBER)
> +
> /**
> * struct_group() - Wrap a set of declarations in a mirrored struct
> *
>
> base-commit: 62cc90241548d5570ee68e01aaba6506964e9811
next prev parent reply other threads:[~2026-07-28 18:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-28 11:49 Bence Csokas
2026-07-28 18:57 ` Gustavo A. R. Silva [this message]
2026-08-06 17:24 ` Mark Rutland
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=3aa0853e-278f-4825-b51e-42eb437111bf@embeddedor.com \
--to=gustavo@embeddedor.com \
--cc=bence.csokas@arm.com \
--cc=gustavoars@kernel.org \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
/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
all inboxes | Powered by JetHome®