Re: [PATCH][next] net: dev: Convert sa_data to flexible array in struct sockaddr

From: Kees Cook
Date: Fri Oct 21 2022 - 21:52:55 EST


On Thu, Oct 20, 2022 at 10:58:50AM +0200, Paolo Abeni wrote:
> On Tue, 2022-10-18 at 02:56 -0700, Kees Cook wrote:
> > [...]
> > struct sockaddr {
> > sa_family_t sa_family; /* address family, AF_xxx */
> > - char sa_data[14]; /* 14 bytes of protocol address */
> > + union {
> > + char sa_data_min[14]; /* Minimum 14 bytes of protocol address */
> > + DECLARE_FLEX_ARRAY(char, sa_data);
>
> Any special reason to avoid preserving the old name for the array and
> e.g. using sa_data_flex for the new field, so we don't have to touch
> the sockaddr users?

Yes -- the reason is exactly to not touch the sockaddr users (who
generally treat sa_data as a fake flexible array). By switching it to a
flex-array the behavior will stay the same (especially under the coming
-fstrict-flex-arrays option), except that it breaks sizeof(). But the
broken sizeof() allows us to immediately find all the places where the
code explicitly depends on sa_data being 14 bytes. And for those cases,
we switch to sizeof(sa_data_min).

If we went the reverse route (and added -fstrict-flex-arrays) we might
end up adding a bunch of false positives all at once, because the places
that treated it as a flex-array would suddenly all begin behaving as a
14-byte array.

--
Kees Cook