[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <b3quyvbfszsimhotsooftcssesjc7zi5hci3o4oasz4bnktfzn@l5qpvyt4rj6n>
Date: Fri, 7 Feb 2025 08:58:59 -0500
From: Kent Overstreet <kent.overstreet@...ux.dev>
To: Kees Cook <kees@...nel.org>
Cc: Suren Baghdasaryan <surenb@...gle.com>,
Andy Shevchenko <andy@...nel.org>, Luc Van Oostenryck <luc.vanoostenryck@...il.com>,
Nathan Chancellor <nathan@...nel.org>, Nick Desaulniers <ndesaulniers@...gle.com>,
Bill Wendling <morbo@...gle.com>, Justin Stitt <justinstitt@...gle.com>,
Philipp Reisner <philipp.reisner@...bit.com>, Miguel Ojeda <ojeda@...nel.org>, linux-kernel@...r.kernel.org,
linux-hardening@...r.kernel.org, llvm@...ts.linux.dev
Subject: Re: [PATCH 2/3] compiler.h: Introduce __must_be_char_array()
On Thu, Feb 06, 2025 at 10:11:29AM -0800, Kees Cook wrote:
> In preparation for adding stricter type checking to the str/mem*()
> helpers, provide a way to check that a variable is a character array
> via __must_be_char_array().
>
> Signed-off-by: Kees Cook <kees@...nel.org>
> ---
> include/linux/compiler.h | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/compiler.h b/include/linux/compiler.h
> index 7af999a131cb..a577fe0b1f8a 100644
> --- a/include/linux/compiler.h
> +++ b/include/linux/compiler.h
> @@ -221,7 +221,13 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
> #endif /* __CHECKER__ */
>
> /* &a[0] degrades to a pointer: a different type from an array */
> -#define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(__same_type((a), &(a)[0]), "must be array")
> +#define __is_array(a) (!__same_type((a), &(a)[0]))
> +#define __must_be_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_array(a), \
> + "must be array")
> +
> +#define __is_char_array(a) (__is_array(a) && sizeof((a)[0]) == 1)
> +#define __must_be_char_array(a) __BUILD_BUG_ON_ZERO_MSG(!__is_char_array(a), \
> + "must be byte array")
>
> /* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */
> #define __must_be_cstr(p) \
> --
> 2.34.1
>
Why not just use a combination of ARRAY_SIZE() and a function call
(to verify that it's a character array) for the typechecking?
Powered by blists - more mailing lists