All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Joe Perches <joe@perches.com>
Cc: Andy Whitcroft <apw@canonical.com>,
	Dwaipayan Ray <dwaipayanray1@gmail.com>,
	Lukas Bulwahn <lukas.bulwahn@gmail.com>,
	"Gustavo A . R . Silva" <gustavoars@kernel.org>,
	linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH v3] checkpatch: Check for 0-length and 1-element arrays
Date: Tue, 30 May 2023 17:40:59 -0700	[thread overview]
Message-ID: <202305301738.92C6DE1D@keescook> (raw)
In-Reply-To: <75851e1185821084c0a1b7e5aaf60e9f38e0f792.camel@perches.com>

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

      reply	other threads:[~2023-05-31  0:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-27  2:09 [PATCH v3] checkpatch: Check for 0-length and 1-element arrays Kees Cook
2023-05-27  2:23 ` Gustavo A. R. Silva
2023-05-27  4:31 ` Joe Perches
2023-05-31  0:40   ` Kees Cook [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202305301738.92C6DE1D@keescook \
    --to=keescook@chromium.org \
    --cc=apw@canonical.com \
    --cc=dwaipayanray1@gmail.com \
    --cc=gustavoars@kernel.org \
    --cc=joe@perches.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas.bulwahn@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.