diff mbox series

[4/5,next] uapi: net: arp: Avoid -Wflex-array-member-not-at-end warnings

Message ID f04e61e1c69991559f5589080462320bf772499d.1729037131.git.gustavoars@kernel.org (mailing list archive)
State Superseded
Headers show
Series net: Avoid thousands of -Wflex-array-member-not-at-end warnings | expand

Commit Message

Gustavo A. R. Silva Oct. 16, 2024, 12:32 a.m. UTC
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are
getting ready to enable it, globally.

Address the following warnings by changing the type of the middle struct
members in a couple of composite structs, which are currently causing
trouble, from `struct sockaddr` to `struct sockaddr_legacy`. Note that
the latter struct doesn't contain a flexible-array member.

include/uapi/linux/if_arp.h:118:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
include/uapi/linux/if_arp.h:119:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
include/uapi/linux/if_arp.h:121:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
include/uapi/linux/if_arp.h:126:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
include/uapi/linux/if_arp.h:127:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

Also, update some related code, accordingly.

No binary differences are present after these changes.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 include/uapi/linux/if_arp.h | 18 +++++++++---------
 net/ipv4/arp.c              |  2 +-
 2 files changed, 10 insertions(+), 10 deletions(-)

Comments

Kuniyuki Iwashima Oct. 16, 2024, 3:44 a.m. UTC | #1
From: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Date: Tue, 15 Oct 2024 18:32:43 -0600
> -Wflex-array-member-not-at-end was introduced in GCC-14, and we are
> getting ready to enable it, globally.
> 
> Address the following warnings by changing the type of the middle struct
> members in a couple of composite structs, which are currently causing
> trouble, from `struct sockaddr` to `struct sockaddr_legacy`. Note that
> the latter struct doesn't contain a flexible-array member.
> 
> include/uapi/linux/if_arp.h:118:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> include/uapi/linux/if_arp.h:119:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> include/uapi/linux/if_arp.h:121:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> include/uapi/linux/if_arp.h:126:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> include/uapi/linux/if_arp.h:127:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> 
> Also, update some related code, accordingly.
> 
> No binary differences are present after these changes.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  include/uapi/linux/if_arp.h | 18 +++++++++---------
>  net/ipv4/arp.c              |  2 +-
>  2 files changed, 10 insertions(+), 10 deletions(-)
> 
> diff --git a/include/uapi/linux/if_arp.h b/include/uapi/linux/if_arp.h
> index 4783af9fe520..cb6813f7783a 100644
> --- a/include/uapi/linux/if_arp.h
> +++ b/include/uapi/linux/if_arp.h
> @@ -115,18 +115,18 @@
>  
>  /* ARP ioctl request. */
>  struct arpreq {
> -	struct sockaddr	arp_pa;		/* protocol address		 */
> -	struct sockaddr	arp_ha;		/* hardware address		 */
> -	int		arp_flags;	/* flags			 */
> -	struct sockaddr arp_netmask;    /* netmask (only for proxy arps) */
> -	char		arp_dev[IFNAMSIZ];
> +	struct sockaddr_legacy	arp_pa;		/* protocol address		 */
> +	struct sockaddr_legacy	arp_ha;		/* hardware address		 */
> +	int			arp_flags;	/* flags			 */
> +	struct sockaddr_legacy	arp_netmask;    /* netmask (only for proxy arps) */
> +	char			arp_dev[IFNAMSIZ];
>  };
>  
>  struct arpreq_old {
> -	struct sockaddr	arp_pa;		/* protocol address		 */
> -	struct sockaddr	arp_ha;		/* hardware address		 */
> -	int		arp_flags;	/* flags			 */
> -	struct sockaddr	arp_netmask;    /* netmask (only for proxy arps) */
> +	struct sockaddr_legacy	arp_pa;		/* protocol address		 */
> +	struct sockaddr_legacy	arp_ha;		/* hardware address		 */
> +	int			arp_flags;	/* flags			 */
> +	struct sockaddr		arp_netmask;    /* netmask (only for proxy arps) */

I think we can use _legacy here too, 14 bytes are enough for ARP.

But whichever is fine to me because arpreq_old is not used in
kernel, so

Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>


>  };
>  
>  /* ARP Flag values. */
> diff --git a/net/ipv4/arp.c b/net/ipv4/arp.c
> index 11c1519b3699..3a97efe1587b 100644
> --- a/net/ipv4/arp.c
> +++ b/net/ipv4/arp.c
> @@ -1185,7 +1185,7 @@ static int arp_req_get(struct net *net, struct arpreq *r)
>  
>  	read_lock_bh(&neigh->lock);
>  	memcpy(r->arp_ha.sa_data, neigh->ha,
> -	       min(dev->addr_len, sizeof(r->arp_ha.sa_data_min)));
> +	       min(dev->addr_len, sizeof(r->arp_ha.sa_data)));
>  	r->arp_flags = arp_state_to_flags(neigh);
>  	read_unlock_bh(&neigh->lock);
>  
> -- 
> 2.34.1
Andrew Lunn Oct. 16, 2024, 12:30 p.m. UTC | #2
On Tue, Oct 15, 2024 at 06:32:43PM -0600, Gustavo A. R. Silva wrote:
> -Wflex-array-member-not-at-end was introduced in GCC-14, and we are
> getting ready to enable it, globally.
> 
> Address the following warnings by changing the type of the middle struct
> members in a couple of composite structs, which are currently causing
> trouble, from `struct sockaddr` to `struct sockaddr_legacy`. Note that
> the latter struct doesn't contain a flexible-array member.
> 
> include/uapi/linux/if_arp.h:118:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> include/uapi/linux/if_arp.h:119:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> include/uapi/linux/if_arp.h:121:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> include/uapi/linux/if_arp.h:126:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> include/uapi/linux/if_arp.h:127:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> 
> Also, update some related code, accordingly.
> 
> No binary differences are present after these changes.

These are clearly UAPI files. It would be good to state in the commit
message why this is a safe change, at the source level.

Could user space code expect the type struct sockaddr and we are going
to get warnings when struct sockaddr_legacy is found? Have you tried
compiling an old arp binary, one that uses the IOCTL? The netfilter
code also references it:

http://charette.no-ip.com:81/programming/doxygen/netfilter/structarpreq.html

You also need to submit a patch to the man page:

https://man7.org/linux/man-pages/man7/arp.7.html

You might also want to build some Rust code:

https://docs.diesel.rs/master/libc/struct.arpreq.html

I've no idea what Rust does when a C structure has a type change.

	Andrew
Kees Cook Oct. 16, 2024, 4:45 p.m. UTC | #3
On Wed, Oct 16, 2024 at 02:30:02PM +0200, Andrew Lunn wrote:
> On Tue, Oct 15, 2024 at 06:32:43PM -0600, Gustavo A. R. Silva wrote:
> > -Wflex-array-member-not-at-end was introduced in GCC-14, and we are
> > getting ready to enable it, globally.
> > 
> > Address the following warnings by changing the type of the middle struct
> > members in a couple of composite structs, which are currently causing
> > trouble, from `struct sockaddr` to `struct sockaddr_legacy`. Note that
> > the latter struct doesn't contain a flexible-array member.
> > 
> > include/uapi/linux/if_arp.h:118:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> > include/uapi/linux/if_arp.h:119:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> > include/uapi/linux/if_arp.h:121:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> > include/uapi/linux/if_arp.h:126:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> > include/uapi/linux/if_arp.h:127:25: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> > 
> > Also, update some related code, accordingly.
> > 
> > No binary differences are present after these changes.
> 
> These are clearly UAPI files. It would be good to state in the commit
> message why this is a safe change, at the source level.

I think we can avoid complicating UAPI by doing something like this in
include/uapi/linux/socket.h:

#ifdef __KERNEL__
#define __kernel_sockaddr_legacy        sockaddr_legacy
#else
#define __kernel_sockaddr_legacy        sockaddr
#endif

And then the UAPI changes can use __kernel_sockaddr_legacy and userspace
will resolve to sockaddr (unchanged), and the kernel internals will
resolve to sockaddr_legacy (fixing the warnings).

-Kees
Gustavo A. R. Silva Oct. 18, 2024, 6:55 p.m. UTC | #4
>> These are clearly UAPI files. It would be good to state in the commit
>> message why this is a safe change, at the source level.

Yes, I'll update it!

> 
> I think we can avoid complicating UAPI by doing something like this in
> include/uapi/linux/socket.h:
> 
> #ifdef __KERNEL__
> #define __kernel_sockaddr_legacy        sockaddr_legacy
> #else
> #define __kernel_sockaddr_legacy        sockaddr
> #endif
> 
> And then the UAPI changes can use __kernel_sockaddr_legacy and userspace
> will resolve to sockaddr (unchanged), and the kernel internals will
> resolve to sockaddr_legacy (fixing the warnings).

Here are a couple of test patches (Don't mind the changelog text):

https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/commit/?h=testing/wfamnae-next20241015-2&id=c3b631a5036cbf45b3308d563bf74a518490f3e6
https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/commit/?h=testing/wfamnae-next20241015-2&id=66db096a530b95ce0ac33f9fdec66401ec5f2204

__kernel_sockaddr_legacy seems a bit too long, but at the same time
it makes it quite clear what's going on.

Thanks
--
Gustavo
diff mbox series

Patch

diff --git a/include/uapi/linux/if_arp.h b/include/uapi/linux/if_arp.h
index 4783af9fe520..cb6813f7783a 100644
--- a/include/uapi/linux/if_arp.h
+++ b/include/uapi/linux/if_arp.h
@@ -115,18 +115,18 @@ 
 
 /* ARP ioctl request. */
 struct arpreq {
-	struct sockaddr	arp_pa;		/* protocol address		 */
-	struct sockaddr	arp_ha;		/* hardware address		 */
-	int		arp_flags;	/* flags			 */
-	struct sockaddr arp_netmask;    /* netmask (only for proxy arps) */
-	char		arp_dev[IFNAMSIZ];
+	struct sockaddr_legacy	arp_pa;		/* protocol address		 */
+	struct sockaddr_legacy	arp_ha;		/* hardware address		 */
+	int			arp_flags;	/* flags			 */
+	struct sockaddr_legacy	arp_netmask;    /* netmask (only for proxy arps) */
+	char			arp_dev[IFNAMSIZ];
 };
 
 struct arpreq_old {
-	struct sockaddr	arp_pa;		/* protocol address		 */
-	struct sockaddr	arp_ha;		/* hardware address		 */
-	int		arp_flags;	/* flags			 */
-	struct sockaddr	arp_netmask;    /* netmask (only for proxy arps) */
+	struct sockaddr_legacy	arp_pa;		/* protocol address		 */
+	struct sockaddr_legacy	arp_ha;		/* hardware address		 */
+	int			arp_flags;	/* flags			 */
+	struct sockaddr		arp_netmask;    /* netmask (only for proxy arps) */
 };
 
 /* ARP Flag values. */
diff --git a/net/ipv4/arp.c b/net/ipv4/arp.c
index 11c1519b3699..3a97efe1587b 100644
--- a/net/ipv4/arp.c
+++ b/net/ipv4/arp.c
@@ -1185,7 +1185,7 @@  static int arp_req_get(struct net *net, struct arpreq *r)
 
 	read_lock_bh(&neigh->lock);
 	memcpy(r->arp_ha.sa_data, neigh->ha,
-	       min(dev->addr_len, sizeof(r->arp_ha.sa_data_min)));
+	       min(dev->addr_len, sizeof(r->arp_ha.sa_data)));
 	r->arp_flags = arp_state_to_flags(neigh);
 	read_unlock_bh(&neigh->lock);