Re: [PATCH] usb: ch9: Replace 1-element array with flexible array

From: Kees Cook
Date: Wed Jun 14 2023 - 13:47:54 EST


On Tue, Jun 13, 2023 at 04:52:08PM -0600, Gustavo A. R. Silva wrote:
> On Tue, Jun 13, 2023 at 02:04:04PM -0700, Kees Cook wrote:
> > With "-fstrict-flex-arrays=3" enabled, UBSAN_BOUNDS no longer pretends
> > 1-element arrays are unbounded. Walking wData will trigger a warning,
> > so make it a proper flexible array. Add a union to keep the struct size
> > identical for userspace in case anything was depending on the old size.
> >
> > Reported-by: kernel test robot <oliver.sang@xxxxxxxxx>
> > Closes: https://lore.kernel.org/oe-lkp/202306102333.8f5a7443-oliver.sang@xxxxxxxxx
> > Fixes: df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
>
> I always have mixed feelings about a 'Fixes' tag applied to a commit
> like this (one that enables a compiler option that avoids the introduction
> of buggy code), when we are addressing the potentially buggy code that
> the option is inteded to prevent. (thinkingface)

Yeah, I've been on the fence about this too. Since it's fixing a
(modern) coding style issue, there's nothing wrong technically. i.e. I
can't say "Fixes: ...usb commit..." since this isn't a bug. But it's
fixing a warning introduced by the fstrict-flex-arrays=3, and tracking
those issues is useful. But, it's not really fixing _that_ commit, as
it's doing exactly what it should be doing. So, perhaps, in the future I
can just mention it more directly in the commit log without a Fixes tag.
For example, this should probably have been written as:

Since commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3"),
UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking
wData will trigger a warning, so make it a proper flexible array. Add a
union to keep the struct size identical for userspace in case anything
was depending on the old size.

Reported-by: kernel test robot <oliver.sang@xxxxxxxxx> Closes:
https://lore.kernel.org/oe-lkp/202306102333.8f5a7443-oliver.sang@xxxxxxxxx

-Kees

--
Kees Cook