linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings
@ 2025-10-21 11:43 Gustavo A. R. Silva
  2025-10-21 15:58 ` Simon Horman
  2025-10-24  0:25 ` Jakub Kicinski
  0 siblings, 2 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2025-10-21 11:43 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman
  Cc: netdev, linux-kernel, Gustavo A. R. Silva, linux-hardening

Use the new TRAILING_OVERLAP() helper to fix 2600 of the following
warnings:

2600 ./include/net/inet_sock.h:65:33: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

This helper creates a union between a flexible-array member (FAM)
and a set of members that would otherwise follow it (in this case
`char data[40];) This overlays the trailing members (data) onto the FAM
(__data) while keeping the FAM and the start of MEMBERS aligned.

The static_assert() ensures this alignment remains, and it's
intentionally placed inmediately after `struct ip_options_data`
(no blank line in between).

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---

I think it's worth mentioning that the introduction of the new
TRAILING_OVERLAP() helper saves us from making changes like the
following, for this particular case:

	https://lore.kernel.org/linux-hardening/ZzK-n_C2yl8mW2Tz@kspp/

Thanks

 include/net/inet_sock.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h
index 1086256549fa..a974588803af 100644
--- a/include/net/inet_sock.h
+++ b/include/net/inet_sock.h
@@ -62,9 +62,12 @@ struct ip_options_rcu {
 };
 
 struct ip_options_data {
-	struct ip_options_rcu	opt;
-	char			data[40];
+	TRAILING_OVERLAP(struct ip_options_rcu, opt, opt.__data,
+			 char			data[40];
+	);
 };
+static_assert(offsetof(struct ip_options_data, opt.opt.__data) ==
+	      offsetof(struct ip_options_data, data));
 
 struct inet_request_sock {
 	struct request_sock	req;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH][next] net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings
  2025-10-21 11:43 [PATCH][next] net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings Gustavo A. R. Silva
@ 2025-10-21 15:58 ` Simon Horman
  2025-10-24  0:25 ` Jakub Kicinski
  1 sibling, 0 replies; 5+ messages in thread
From: Simon Horman @ 2025-10-21 15:58 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	netdev, linux-kernel, linux-hardening

On Tue, Oct 21, 2025 at 12:43:30PM +0100, Gustavo A. R. Silva wrote:
> Use the new TRAILING_OVERLAP() helper to fix 2600 of the following
> warnings:
> 
> 2600 ./include/net/inet_sock.h:65:33: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> 
> This helper creates a union between a flexible-array member (FAM)
> and a set of members that would otherwise follow it (in this case
> `char data[40];) This overlays the trailing members (data) onto the FAM
> (__data) while keeping the FAM and the start of MEMBERS aligned.
> 
> The static_assert() ensures this alignment remains, and it's
> intentionally placed inmediately after `struct ip_options_data`
> (no blank line in between).
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
> 
> I think it's worth mentioning that the introduction of the new
> TRAILING_OVERLAP() helper saves us from making changes like the
> following, for this particular case:
> 
> 	https://lore.kernel.org/linux-hardening/ZzK-n_C2yl8mW2Tz@kspp/

Thanks,

I was able to reproduce a (small) subset of those warnings
and agree that this is a very nice way to address them.

Reviewed-by: Simon Horman <horms@kernel.org>

...

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][next] net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings
  2025-10-21 11:43 [PATCH][next] net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings Gustavo A. R. Silva
  2025-10-21 15:58 ` Simon Horman
@ 2025-10-24  0:25 ` Jakub Kicinski
  2025-10-24 11:24   ` Gustavo A. R. Silva
  1 sibling, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2025-10-24  0:25 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Simon Horman, netdev,
	linux-kernel, linux-hardening

On Tue, 21 Oct 2025 12:43:30 +0100 Gustavo A. R. Silva wrote:
>  struct ip_options_data {
> -	struct ip_options_rcu	opt;
> -	char			data[40];
> +	TRAILING_OVERLAP(struct ip_options_rcu, opt, opt.__data,
> +			 char			data[40];
> +	);
>  };

Is there a way to reserve space for flexible length array on the stack
without resorting to any magic macros? This struct has total of 5 users.
-- 
pw-bot: cr

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][next] net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings
  2025-10-24  0:25 ` Jakub Kicinski
@ 2025-10-24 11:24   ` Gustavo A. R. Silva
  2025-10-24 23:23     ` Jakub Kicinski
  0 siblings, 1 reply; 5+ messages in thread
From: Gustavo A. R. Silva @ 2025-10-24 11:24 UTC (permalink / raw)
  To: Jakub Kicinski, Gustavo A. R. Silva
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Simon Horman, netdev,
	linux-kernel, linux-hardening



On 10/24/25 01:25, Jakub Kicinski wrote:
> On Tue, 21 Oct 2025 12:43:30 +0100 Gustavo A. R. Silva wrote:
>>   struct ip_options_data {
>> -	struct ip_options_rcu	opt;
>> -	char			data[40];
>> +	TRAILING_OVERLAP(struct ip_options_rcu, opt, opt.__data,
>> +			 char			data[40];
>> +	);
>>   };
> 
> Is there a way to reserve space for flexible length array on the stack
> without resorting to any magic macros? This struct has total of 5 users.

Not that I know of. That's the reason why we had to implement macros like
TRAILING_OVERLAP(), DEFINE_FLEX(), DEFINE_RAW_FLEX().

Regarding these three macros, the simplest and least intrusive one to use is
actually TRAILING_OVERLAP(), when the flex-array member is not annotated with
the counted_by attribute (otherwise, DEFINE_FLEX() would be preferred).

Of course, the most straightforward alternative is to use fixed-size arrays
if flex arrays are not actually needed.

Thanks
-Gustavo


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][next] net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings
  2025-10-24 11:24   ` Gustavo A. R. Silva
@ 2025-10-24 23:23     ` Jakub Kicinski
  0 siblings, 0 replies; 5+ messages in thread
From: Jakub Kicinski @ 2025-10-24 23:23 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Gustavo A. R. Silva, David S. Miller, Eric Dumazet, Paolo Abeni,
	Simon Horman, netdev, linux-kernel, linux-hardening

On Fri, 24 Oct 2025 12:24:09 +0100 Gustavo A. R. Silva wrote:
> On 10/24/25 01:25, Jakub Kicinski wrote:
> > On Tue, 21 Oct 2025 12:43:30 +0100 Gustavo A. R. Silva wrote:  
> >>   struct ip_options_data {
> >> -	struct ip_options_rcu	opt;
> >> -	char			data[40];
> >> +	TRAILING_OVERLAP(struct ip_options_rcu, opt, opt.__data,
> >> +			 char			data[40];
> >> +	);
> >>   };  
> > 
> > Is there a way to reserve space for flexible length array on the stack
> > without resorting to any magic macros? This struct has total of 5 users.  
> 
> Not that I know of. That's the reason why we had to implement macros like
> TRAILING_OVERLAP(), DEFINE_FLEX(), DEFINE_RAW_FLEX().
> 
> Regarding these three macros, the simplest and least intrusive one to use is
> actually TRAILING_OVERLAP(), when the flex-array member is not annotated with
> the counted_by attribute (otherwise, DEFINE_FLEX() would be preferred).
> 
> Of course, the most straightforward alternative is to use fixed-size arrays
> if flex arrays are not actually needed.

Honestly, I'm tired of the endless, nasty macros for no clear benefit.
This patch is not happening.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-10-24 23:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-21 11:43 [PATCH][next] net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings Gustavo A. R. Silva
2025-10-21 15:58 ` Simon Horman
2025-10-24  0:25 ` Jakub Kicinski
2025-10-24 11:24   ` Gustavo A. R. Silva
2025-10-24 23:23     ` Jakub Kicinski

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).