From: Kees Cook <kees@kernel.org>
To: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Cc: Justin Stitt <justinstitt@google.com>,
Andy Shevchenko <andy@kernel.org>,
Luc Van Oostenryck <luc.vanoostenryck@gmail.com>,
Nick Desaulniers <ndesaulniers@google.com>,
Miguel Ojeda <ojeda@kernel.org>, Marco Elver <elver@google.com>,
Nathan Chancellor <nathan@kernel.org>,
Hao Luo <haoluo@google.com>,
linux-sparse@vger.kernel.org, linux-hardening@vger.kernel.org,
Bill Wendling <morbo@google.com>,
Mark Rutland <mark.rutland@arm.com>,
Jakub Kicinski <kuba@kernel.org>,
Alexander Lobakin <aleksander.lobakin@intel.com>,
Tony Ambardar <tony.ambardar@gmail.com>,
Petr Pavlu <petr.pavlu@suse.com>,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: [PATCH] string: Check for "nonstring" attribute on strscpy() arguments
Date: Thu, 22 Aug 2024 17:04:08 -0700 [thread overview]
Message-ID: <202408221701.62773B4E1@keescook> (raw)
In-Reply-To: <1724c53c-5a02-4816-a688-cae18db443d2@intel.com>
On Tue, Aug 06, 2024 at 12:29:30PM +0200, Przemek Kitszel wrote:
> On 8/5/24 23:43, Kees Cook wrote:
> > GCC already checks for arguments that are marked with the "nonstring"[1]
> > attribute when used on standard C String API functions (e.g. strcpy). Gain
> > this compile-time checking also for the kernel's primary string copying
> > function, strscpy().
> >
> > Note that Clang has neither "nonstring" nor __builtin_has_attribute().
> >
> > Link: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute [1]
> > Signed-off-by: Kees Cook <kees@kernel.org>
>
>
>
> > diff --git a/include/linux/string.h b/include/linux/string.h
> > index 9edace076ddb..95b3fc308f4f 100644
> > --- a/include/linux/string.h
> > +++ b/include/linux/string.h
> > @@ -76,12 +76,16 @@ ssize_t sized_strscpy(char *, const char *, size_t);
> > * known size.
> > */
> > #define __strscpy0(dst, src, ...) \
> > - sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst))
> > -#define __strscpy1(dst, src, size) sized_strscpy(dst, src, size)
> > + sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst) + \
> > + __must_be_cstr(dst) + __must_be_cstr(src))
> > +#define __strscpy1(dst, src, size) \
> > + sized_strscpy(dst, src, size + __must_be_cstr(dst) + __must_be_cstr(src))
> > #define __strscpy_pad0(dst, src, ...) \
> > - sized_strscpy_pad(dst, src, sizeof(dst) + __must_be_array(dst))
> > -#define __strscpy_pad1(dst, src, size) sized_strscpy_pad(dst, src, size)
> > + sized_strscpy_pad(dst, src, sizeof(dst) + __must_be_array(dst) + \
> > + __must_be_cstr(dst) + __must_be_cstr(src))
> > +#define __strscpy_pad1(dst, src, size) \
> > + sized_strscpy_pad(dst, src, size + __must_be_cstr(dst) + __must_be_cstr(src))
>
> any way to avoid the usual caveat of repeating macro argument?
>
> a variant of BUILD_BUG that is checking argument and otherwise pasting
> it would nail it, but I didn't pondered how to implement such
The use of __must_be_cstr() shouldn't cause side-effects, so from what I
can tell this is all okay. And since BUILD_BUG_ON_ZERO() resolves to a
constant expression, it shouldn't change the processing of the "size"
argument in the strscpy internals...
--
Kees Cook
next prev parent reply other threads:[~2024-08-23 0:04 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-05 21:43 [PATCH] string: Check for "nonstring" attribute on strscpy() arguments Kees Cook
2024-08-06 10:29 ` Przemek Kitszel
2024-08-23 0:04 ` Kees Cook [this message]
2024-08-06 10:46 ` Miguel Ojeda
2024-08-23 0:05 ` Kees Cook
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=202408221701.62773B4E1@keescook \
--to=kees@kernel.org \
--cc=aleksander.lobakin@intel.com \
--cc=andy@kernel.org \
--cc=elver@google.com \
--cc=haoluo@google.com \
--cc=justinstitt@google.com \
--cc=kuba@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sparse@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=luc.vanoostenryck@gmail.com \
--cc=mark.rutland@arm.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=ojeda@kernel.org \
--cc=petr.pavlu@suse.com \
--cc=przemyslaw.kitszel@intel.com \
--cc=tony.ambardar@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox