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

From: Kees Cook <[email-masked]>
Date: Wed, 16 Oct 2024 09:45:09 -0700

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

-- 
Kees Cook