[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <202305301738.92C6DE1D@keescook>
Date: Tue, 30 May 2023 17:40:59 -0700
From: Kees Cook <keescook@...omium.org>
To: Joe Perches <joe@...ches.com>
Cc: Andy Whitcroft <apw@...onical.com>,
Dwaipayan Ray <dwaipayanray1@...il.com>,
Lukas Bulwahn <lukas.bulwahn@...il.com>,
"Gustavo A . R . Silva" <gustavoars@...nel.org>,
linux-kernel@...r.kernel.org, linux-hardening@...r.kernel.org
Subject: Re: [PATCH v3] checkpatch: Check for 0-length and 1-element arrays
On Fri, May 26, 2023 at 09:31:39PM -0700, Joe Perches wrote:
> On Fri, 2023-05-26 at 19:09 -0700, Kees Cook wrote:
> > Fake flexible arrays have been deprecated since last millennium. Proper
> > C99 flexible arrays must be used throughout the kernel so
> > CONFIG_FORTIFY_SOURCE and CONFIG_UBSAN_BOUNDS can provide proper array
> > bounds checking.
>
> Hi again.
>
>
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
> > @@ -7430,6 +7430,21 @@ sub process {
> > }
> > }
> >
> > +# check for array definition/declarations that should use flexible arrays instead
> > + if ($sline =~ /^[\+ ]\s*}\s*;\s*$/ &&
> > + $prevline =~ /^\+\s*(?:\}|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
>
> Ahh, now I see why you want to use (?:\}|$Type) to find
> struct arrays defined as the last object in another struct.
Yeah -- this has been a place where past "[1]"s have snuck in before. :)
> grepping for some existing variants, perhaps the $sline test should
> be extended to something like:
>
> if ($sline =~ /^[\+ ]\s*}\s*(?:__packed|__attribute__\s*$balanced_parens)?\s*;\s*$/ &&
Ugh, I forgot attributes. Hmmm. I don't need this check to be _perfect_
either. :P Really only "__packed" has come up in other conversions.
> > + if ($1 == '0') {
> > + if (ERROR("ZERO_LENGTH_ARRAY",
> > + "Use C99 flexible arrays instead of zero-length arrays - see https://github.com/KSPP/linux/issues/78\n" . $hereprev) &&
>
> Instead of github links, how about using:
> https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays
Yeah, though the issues have a bit more detail. Probably better to be
self-referential. :)
>
>
> Why not combine both of these into a single ERROR?
> What's the value in separate types?
Yup, with the same URL (instead of separate URLs), it gets cleaner too.
Thanks!
-Kees
--
Kees Cook
Powered by blists - more mailing lists