All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: david.laight.linux@gmail.com
Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH next 1/3] fortify: replace __compiletime_lessthan() with statically_true()
Date: Mon, 30 Mar 2026 16:50:22 -0700	[thread overview]
Message-ID: <202603301649.A136F870E@keescook> (raw)
In-Reply-To: <20260330132003.3379-2-david.laight.linux@gmail.com>

On Mon, Mar 30, 2026 at 02:20:01PM +0100, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> __compiletime_lessthan(a, b) is exactly the same as statically_true(a < b)
> Replace the former by the latter.
> 
> Signed-off-by: David Laight <david.laight.linux@gmail.com>

This one is a clear win: I didn't know about statically_true(). This
results in identical code.

I'll pick this one up.

-Kees

> ---
>  include/linux/fortify-string.h | 47 +++++++++++++++-------------------
>  1 file changed, 21 insertions(+), 26 deletions(-)
> 
> diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h
> index 171982e53c9a..214d237214d5 100644
> --- a/include/linux/fortify-string.h
> +++ b/include/linux/fortify-string.h
> @@ -154,11 +154,6 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size)
>  #define POS0			__pass_object_size(0)
>  #endif
>  
> -#define __compiletime_lessthan(bounds, length)	(	\
> -	__builtin_constant_p((bounds) < (length)) &&	\
> -	(bounds) < (length)				\
> -)
> -
>  /**
>   * strncpy - Copy a string to memory with non-guaranteed NUL padding
>   *
> @@ -196,7 +191,7 @@ char *strncpy(char * const POS p, const char *q, __kernel_size_t size)
>  {
>  	const size_t p_size = __member_size(p);
>  
> -	if (__compiletime_lessthan(p_size, size))
> +	if (statically_true(p_size < size))
>  		__write_overflow();
>  	if (p_size < size)
>  		fortify_panic(FORTIFY_FUNC_strncpy, FORTIFY_WRITE, p_size, size, p);
> @@ -287,14 +282,14 @@ __FORTIFY_INLINE ssize_t sized_strscpy(char * const POS p, const char * const PO
>  	 * If size can be known at compile time and is greater than
>  	 * p_size, generate a compile time write overflow error.
>  	 */
> -	if (__compiletime_lessthan(p_size, size))
> +	if (statically_true(p_size < size))
>  		__write_overflow();
>  
>  	/* Short-circuit for compile-time known-safe lengths. */
> -	if (__compiletime_lessthan(p_size, SIZE_MAX)) {
> +	if (statically_true(p_size < SIZE_MAX)) {
>  		len = __compiletime_strlen(q);
>  
> -		if (len < SIZE_MAX && __compiletime_lessthan(len, size)) {
> +		if (len < SIZE_MAX && statically_true(len < size)) {
>  			__underlying_memcpy(p, q, len + 1);
>  			return len;
>  		}
> @@ -469,12 +464,12 @@ __FORTIFY_INLINE bool fortify_memset_chk(__kernel_size_t size,
>  		 */
>  
>  		/* Error when size is larger than enclosing struct. */
> -		if (__compiletime_lessthan(p_size_field, p_size) &&
> -		    __compiletime_lessthan(p_size, size))
> +		if (statically_true(p_size_field < p_size) &&
> +		    statically_true(p_size < size))
>  			__write_overflow();
>  
>  		/* Warn when write size is larger than dest field. */
> -		if (__compiletime_lessthan(p_size_field, size))
> +		if (statically_true(p_size_field < size))
>  			__write_overflow_field(p_size_field, size);
>  	}
>  	/*
> @@ -557,15 +552,15 @@ __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
>  		 */
>  
>  		/* Error when size is larger than enclosing struct. */
> -		if (__compiletime_lessthan(p_size_field, p_size) &&
> -		    __compiletime_lessthan(p_size, size))
> +		if (statically_true(p_size_field < p_size) &&
> +		    statically_true(p_size < size))
>  			__write_overflow();
> -		if (__compiletime_lessthan(q_size_field, q_size) &&
> -		    __compiletime_lessthan(q_size, size))
> +		if (statically_true(q_size_field < q_size) &&
> +		    statically_true(q_size < size))
>  			__read_overflow2();
>  
>  		/* Warn when write size argument larger than dest field. */
> -		if (__compiletime_lessthan(p_size_field, size))
> +		if (statically_true(p_size_field < size))
>  			__write_overflow_field(p_size_field, size);
>  		/*
>  		 * Warn for source field over-read when building with W=1
> @@ -573,8 +568,8 @@ __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
>  		 * the same time.
>  		 */
>  		if ((IS_ENABLED(KBUILD_EXTRA_WARN1) ||
> -		     __compiletime_lessthan(p_size_field, size)) &&
> -		    __compiletime_lessthan(q_size_field, size))
> +		     statically_true(p_size_field < size)) &&
> +		    statically_true(q_size_field < size))
>  			__read_overflow2_field(q_size_field, size);
>  	}
>  	/*
> @@ -699,7 +694,7 @@ __FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size)
>  {
>  	const size_t p_size = __struct_size(p);
>  
> -	if (__compiletime_lessthan(p_size, size))
> +	if (statically_true(p_size < size))
>  		__read_overflow();
>  	if (p_size < size)
>  		fortify_panic(FORTIFY_FUNC_memscan, FORTIFY_READ, p_size, size, NULL);
> @@ -713,9 +708,9 @@ int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t
>  	const size_t q_size = __struct_size(q);
>  
>  	if (__builtin_constant_p(size)) {
> -		if (__compiletime_lessthan(p_size, size))
> +		if (statically_true(p_size < size))
>  			__read_overflow();
> -		if (__compiletime_lessthan(q_size, size))
> +		if (statically_true(q_size < size))
>  			__read_overflow2();
>  	}
>  	if (p_size < size)
> @@ -730,7 +725,7 @@ void *memchr(const void * const POS0 p, int c, __kernel_size_t size)
>  {
>  	const size_t p_size = __struct_size(p);
>  
> -	if (__compiletime_lessthan(p_size, size))
> +	if (statically_true(p_size < size))
>  		__read_overflow();
>  	if (p_size < size)
>  		fortify_panic(FORTIFY_FUNC_memchr, FORTIFY_READ, p_size, size, NULL);
> @@ -742,7 +737,7 @@ __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
>  {
>  	const size_t p_size = __struct_size(p);
>  
> -	if (__compiletime_lessthan(p_size, size))
> +	if (statically_true(p_size < size))
>  		__read_overflow();
>  	if (p_size < size)
>  		fortify_panic(FORTIFY_FUNC_memchr_inv, FORTIFY_READ, p_size, size, NULL);
> @@ -755,7 +750,7 @@ __FORTIFY_INLINE void *kmemdup_noprof(const void * const POS0 p, size_t size, gf
>  {
>  	const size_t p_size = __struct_size(p);
>  
> -	if (__compiletime_lessthan(p_size, size))
> +	if (statically_true(p_size < size))
>  		__read_overflow();
>  	if (p_size < size)
>  		fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ, p_size, size,
> @@ -793,7 +788,7 @@ char *strcpy(char * const POS p, const char * const POS q)
>  		return __underlying_strcpy(p, q);
>  	size = strlen(q) + 1;
>  	/* Compile-time check for const size overflow. */
> -	if (__compiletime_lessthan(p_size, size))
> +	if (statically_true(p_size < size))
>  		__write_overflow();
>  	/* Run-time check for dynamic size overflow. */
>  	if (p_size < size)
> -- 
> 2.39.5
> 

-- 
Kees Cook

  reply	other threads:[~2026-03-30 23:50 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-30 13:20 [PATCH next 0/3] fortify: Minor changes to strlen() and strnlen() david.laight.linux
2026-03-30 13:20 ` [PATCH next 1/3] fortify: replace __compiletime_lessthan() with statically_true() david.laight.linux
2026-03-30 23:50   ` Kees Cook [this message]
2026-03-30 13:20 ` [PATCH next 2/3] fortify: Optimise strnlen() david.laight.linux
2026-03-30 23:54   ` Kees Cook
2026-03-31 22:09     ` David Laight
2026-03-31 23:51       ` Kees Cook
2026-04-01 13:48         ` David Laight
2026-04-03  8:50         ` David Laight
2026-04-16 14:22         ` David Laight
2026-03-31  6:36   ` Kees Cook
2026-03-31 10:14     ` David Laight
2026-03-31 14:55       ` David Laight
2026-03-31 15:56         ` Kees Cook
2026-04-01  0:15   ` kernel test robot
2026-04-03  8:23     ` David Laight
2026-03-30 13:20 ` [PATCH next 3/3] fortify: Simplify strlen() logic david.laight.linux
2026-03-31  6:07   ` Kees Cook
2026-03-31  8:58     ` David Laight
2026-03-31  6:18   ` 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=202603301649.A136F870E@keescook \
    --to=kees@kernel.org \
    --cc=david.laight.linux@gmail.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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.