linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Qian Cai <quic_qiancai@quicinc.com>
Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] fortify: Avoid shadowing previous locals
Date: Mon, 25 Oct 2021 12:34:03 -0700	[thread overview]
Message-ID: <202110251232.652ACB6@keescook> (raw)
In-Reply-To: <20211025183728.181399-1-quic_qiancai@quicinc.com>

On Mon, Oct 25, 2021 at 02:37:28PM -0400, Qian Cai wrote:
> __compiletime_strlen macro expansion will shadow p_size and p_len local
> variables. Just rename those in __compiletime_strlen.

They don't escape their local context, though, right? i.e. I don't see a
problem with the existing macro. Did you encounter a specific issue that
this patch fixes?

-Kees

> 
> Signed-off-by: Qian Cai <quic_qiancai@quicinc.com>
> ---
>  include/linux/fortify-string.h | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h
> index fdb0a74c9ca2..155c622e4f24 100644
> --- a/include/linux/fortify-string.h
> +++ b/include/linux/fortify-string.h
> @@ -10,18 +10,18 @@ void __read_overflow(void) __compiletime_error("detected read beyond size of obj
>  void __read_overflow2(void) __compiletime_error("detected read beyond size of object (2nd parameter)");
>  void __write_overflow(void) __compiletime_error("detected write beyond size of object (1st parameter)");
>  
> -#define __compiletime_strlen(p)				\
> -({							\
> -	unsigned char *__p = (unsigned char *)(p);      \
> -	size_t ret = (size_t)-1;			\
> -	size_t p_size = __builtin_object_size(p, 1);	\
> -	if (p_size != (size_t)-1) {			\
> -		size_t p_len = p_size - 1;		\
> -		if (__builtin_constant_p(__p[p_len]) &&	\
> -		    __p[p_len] == '\0')			\
> -			ret = __builtin_strlen(__p);	\
> -	}						\
> -	ret;						\
> +#define __compiletime_strlen(ptr)				\
> +({								\
> +	unsigned char *__ptr = (unsigned char *)(ptr);		\
> +	size_t ret = (size_t)-1;				\
> +	size_t ptr_size = __builtin_object_size(ptr, 1);	\
> +	if (ptr_size != (size_t)-1) {				\
> +		size_t ptr_len = ptr_size - 1;			\
> +		if (__builtin_constant_p(__ptr[ptr_len]) &&	\
> +		    __ptr[ptr_len] == '\0')			\
> +			ret = __builtin_strlen(__ptr);		\
> +	}							\
> +	ret;							\
>  })
>  
>  #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
> -- 
> 2.30.2
> 

-- 
Kees Cook

  reply	other threads:[~2021-10-25 20:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-25 18:37 [PATCH] fortify: Avoid shadowing previous locals Qian Cai
2021-10-25 19:34 ` Kees Cook [this message]
2021-10-25 20:15   ` Qian Cai
2021-10-25 20:49     ` 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=202110251232.652ACB6@keescook \
    --to=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=quic_qiancai@quicinc.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;
as well as URLs for NNTP newsgroup(s).