net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings
Subsystems (4)
PATCH 1/1 net: inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings
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).
---
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(-)
include/net/inet_sock.h +5 -2
@@ -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;
Signature · 1 line
2.43.0
Quoted 14 lines · Gustavo A. R. Silva
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 afterstruct ip_options_data(no blank line in between).
Quoted 8 lines
Signed-off-by Gustavo A. R. Silva <[email protected]>---
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.
...
Jakub Kicinski
maintainerinclude/net/inet_sock.h struct ip_options_rcu { · line 68
Quoted 7 lines · On Tue, 21 Oct 2025 12:43:30 +0100 Gustavo A. R. Silva wrote:
struct
ip_options_data{include/net/inet_sock.h +3
-2-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.
Signature · 1 line
pw-bot: cr
Quoted 11 lines · Jakub Kicinski
include/net/inet_sock.h struct ip_options_rcu { · line 68
Quoted 7 lines · On Tue, 21 Oct 2025 12:43:30 +0100 Gustavo A. R. Silva wrote:
struct ip_options_data {include/net/inet_sock.h +3
-2-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
Jakub Kicinski
maintainerQuoted 16 lines · On Fri, 24 Oct 2025 12:24:09 +0100 Gustavo A. R. Silva wrote:
Quoted 11 lines · Jakub Kicinski
include/net/inet_sock.h struct ip_options_rcu { · line 68
Quoted 7 lines · On Tue, 21 Oct 2025 12:43:30 +0100 Gustavo A. R. Silva wrote:
struct ip_options_data {include/net/inet_sock.h +3
-2-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().
Quoted 6 lines
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 thecounted_byattribute (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.
Quoted 21 lines · Jakub Kicinski
Quoted 20 lines · On Fri, 24 Oct 2025 12:24:09 +0100 Gustavo A. R. Silva wrote:
Quoted 11 lines · Jakub Kicinski
Review
Quoted 7 lines · On Tue, 21 Oct 2025 12:43:30 +0100 Gustavo A. R. Silva wrote:
struct ip_options_data {(quoted diff) +3
-2- 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 thecounted_byattribute (otherwise,DEFINE_FLEX()would be preferred).
Quoted 5 lines
Quoted 2 lines
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.
Not sure what you mean by "nasty" and "no clear benefit", but here is an alternative. See it as RFC:
[PATCH RFC][next] ipv4/inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warnings
Use DEFINE_RAW_FLEX() to avoid thousands of -Wflex-array-member-not-at-end warnings.
Remove fixed-size array char data[40]; from struct ip_options_data, so that flexible-array member struct ip_options_rcu::opt.__data[] ends last in this (and other) structure(s).
Compensate for this by using the DEFINE_RAW_FLEX() helper to declare each on-stack struct instance that contains struct ip_options_data as a member.
--- include/net/inet_sock.h | 1 - net/ipv4/icmp.c | 88 +++++++++++++++++++++-------------------- net/ipv4/ip_output.c | 12 +++--- net/ipv4/ping.c | 6 +-- net/ipv4/raw.c | 6 +-- net/ipv4/udp.c | 6 +-- 6 files changed, 60 insertions(+), 59 deletions(-)
include/net/inet_sock.h +0 -1
@@ -63,7 +63,6 @@ struct ip_options_rcu {struct ip_options_data {struct ip_options_rcu opt; -char data[40 ]; };struct inet_request_sock {
net/ipv4/icmp.c +45 -43
171 lines not on this page · raw message
net/ipv4/ip_output.c +6 -6
@@ -1606,7 +1606,7 @@ void ip_send_unicast_reply(struct sock *sk, const struct sock *orig_sk,const struct ip_reply_arg *arg, unsigned int len, u64 transmit_time, u32 txhash) { - struct ip_options_data replyopts; + DEFINE_RAW_FLEX(struct ip_options_data, replyopts, opt.opt.__data, 40);struct ipcm_cookie ipc;struct flowi4 fl4;struct rtable *rt =skb_rtable (skb);
@@ -1615,18 +1615,18 @@ void ip_send_unicast_reply(struct sock *sk, const struct sock *orig_sk,int err;int oif; -if (__ip_options_echo (net, &replyopts.opt.opt, skb, sopt)) +if (__ip_options_echo (net, &replyopts->opt.opt, skb, sopt))return ;ipcm_init (&ipc); ipc.addr = daddr; ipc.sockc.transmit_time = transmit_time; -if (replyopts.opt.opt.optlen) { - ipc.opt = &replyopts.opt; +if (replyopts->opt.opt.optlen) { + ipc.opt = &replyopts->opt; -if (replyopts.opt.opt.srr) - daddr = replyopts.opt.opt.faddr; +if (replyopts->opt.opt.srr) + daddr = replyopts->opt.opt.faddr; } oif = arg->bound_dev_if;
net/ipv4/ping.c +3 -3
@@ -690,6 +690,7 @@ EXPORT_IPV6_MOD_GPL(ping_common_sendmsg);static int ping_v4_sendmsg (struct sock *sk,struct msghdr *msg,size_t len) { + DEFINE_RAW_FLEX(struct ip_options_data, opt_copy, opt.opt.__data, 40);struct net *net =sock_net (sk);struct flowi4 fl4;struct inet_sock *inet =inet_sk (sk);
@@ -697,7 +698,6 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)struct icmphdr user_icmph;struct pingfakehdr pfh;struct rtable *rt =NULL ; -struct ip_options_data opt_copy;int free =0 ;__be32 saddr, daddr, faddr;u8 scope;
@@ -746,9 +746,9 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)rcu_read_lock (); inet_opt =rcu_dereference (inet->inet_opt);if (inet_opt) { -memcpy (&opt_copy, inet_opt, +memcpy (opt_copy, inet_opt,sizeof (*inet_opt) + inet_opt->opt.optlen); - ipc.opt = &opt_copy.opt; + ipc.opt = &opt_copy->opt; }rcu_read_unlock (); }
net/ipv4/raw.c +3 -3
@@ -481,6 +481,7 @@ static int raw_getfrag(void *from, char *to, int offset, int len, int odd,static int raw_sendmsg (struct sock *sk,struct msghdr *msg,size_t len) { + DEFINE_RAW_FLEX(struct ip_options_data, opt_copy, opt.opt.__data, 40);struct inet_sock *inet =inet_sk (sk);struct net *net =sock_net (sk);struct ipcm_cookie ipc;
@@ -491,7 +492,6 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)__be32 daddr;__be32 saddr;int uc_index, err; -struct ip_options_data opt_copy;struct raw_frag_vec rfv;int hdrincl;
@@ -561,9 +561,9 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)rcu_read_lock (); inet_opt =rcu_dereference (inet->inet_opt);if (inet_opt) { -memcpy (&opt_copy, inet_opt, +memcpy (opt_copy, inet_opt,sizeof (*inet_opt) + inet_opt->opt.optlen); - ipc.opt = &opt_copy.opt; + ipc.opt = &opt_copy->opt; }rcu_read_unlock (); }
net/ipv4/udp.c +3 -3
@@ -1269,6 +1269,7 @@ EXPORT_IPV6_MOD_GPL(udp_cmsg_send);int udp_sendmsg (struct sock *sk,struct msghdr *msg,size_t len) { + DEFINE_RAW_FLEX(struct ip_options_data, opt_copy, opt.opt.__data, 40);struct inet_sock *inet =inet_sk (sk);struct udp_sock *up =udp_sk (sk); DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
@@ -1286,7 +1287,6 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)int corkreq =udp_test_bit (CORK , sk) || msg->msg_flags &MSG_MORE ;int (*getfrag)(void *,char *,int ,int ,int ,struct sk_buff *);struct sk_buff *skb; -struct ip_options_data opt_copy;int uc_index;if (len >0xFFFF )
@@ -1368,9 +1368,9 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)rcu_read_lock (); inet_opt =rcu_dereference (inet->inet_opt);if (inet_opt) { -memcpy (&opt_copy, inet_opt, +memcpy (opt_copy, inet_opt,sizeof (*inet_opt) + inet_opt->opt.optlen); - ipc.opt = &opt_copy.opt; + ipc.opt = &opt_copy->opt; }rcu_read_unlock (); }
Signature · 5 lines
2.43.0 Thanks -Gustavo
Jakub Kicinski
maintainerQuoted 2 lines · On Tue, 18 Nov 2025 13:36:41 +0900 Gustavo A. R. Silva wrote:
Not sure what you mean by "nasty" and "no clear benefit", but here is an alternative. See it as RFC:
Meaning of the words per dictionary definition.
Quoted 7 lines
[PATCH RFC][next]
ipv4/inet_sock.h: Avoid thousands of -Wflex-array-member-not-at-end warningsUse
DEFINE_RAW_FLEX()to avoid thousands of -Wflex-array-member-not-at-end warnings.
Quoted 6 lines
Remove fixed-size array char data[40]; from struct
ip_options_data, so that flexible-array member struct ip_options_rcu::opt.__data[] ends last in this (and other) structure(s).Compensate for this by using the
DEFINE_RAW_FLEX()helper to declare each on-stack struct instance that contains structip_options_dataas a member.
This is much better, thanks!
include/net/inet_sock.h struct ip_options_rcu { · line 66
Quoted 4 lines · Gustavo A. R. Silva
struct
ip_options_data{include/net/inet_sock.h +0
-1struct ip_options_rcu opt; -char data[40 ]; };
If this just contains struct ip_options_rcu now, can ip_options_data just be removed entirely?
-Kees
Signature · 1 line
Kees Cook