All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: liujinlong <mingliu6104@gmail.com>
Cc: thunder.leizhen@huawei.com, yonghong.song@linux.dev,
	ndesaulniers@google.com, song@kernel.org, ardb@kernel.org,
	maninder1.s@samsung.com, azeemshaikh38@gmail.com,
	linux-kernel@vger.kernel.org, liujinlong <liujinlong@kylinos.cn>,
	k2ci <kernel-bot@kylinos.cn>,
	Mirsad Todorovac <mtodorovac69@gmail.com>
Subject: Re: [PATCH] sprint_symbol: Replace strcpy with memmove to handle potential overlap
Date: Fri, 28 Jun 2024 09:56:17 -0700	[thread overview]
Message-ID: <202406280952.28AFD754@keescook> (raw)
In-Reply-To: <20240604044228.2910712-1-liujinlong@kylinos.cn>

On Tue, Jun 04, 2024 at 12:42:28PM +0800, liujinlong wrote:
> In the function __sprint_symbol, replace strcpy with memmove to ensure
> correct behavior even if the source and destination buffers overlap.
> This change prevents potential undefined behavior flagged by recent
> compilers as [-Werror=restrict].
> 
> Reported-by: k2ci <kernel-bot@kylinos.cn>
> Signed-off-by: liujinlong <liujinlong@kylinos.cn>
> ---
>  kernel/kallsyms.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
> index 22ea19a36e6e..3c3a77fcd020 100644
> --- a/kernel/kallsyms.c
> +++ b/kernel/kallsyms.c
> @@ -489,7 +489,7 @@ static int __sprint_symbol(char *buffer, unsigned long address,
>  		return sprintf(buffer, "0x%lx", address - symbol_offset);
>  
>  	if (name != buffer)
> -		strcpy(buffer, name);
> +		memmove(buffer, name, strlen(name) + 1);
>  	len = strlen(buffer);
>  	offset -= symbol_offset;

The warning is[1]:

>   CC      kernel/kallsyms.o
> In file included from ./include/linux/string.h:374,
>                  from ./arch/x86/include/asm/page_32.h:18,
>                  from ./arch/x86/include/asm/page.h:14,
>                  from ./arch/x86/include/asm/thread_info.h:12,
>                  from ./include/linux/thread_info.h:60,
>                  from ./include/linux/spinlock.h:60,
>                  from ./include/linux/mmzone.h:8,
>                  from ./include/linux/gfp.h:7,
>                  from ./include/linux/mm.h:7,
>                  from ./include/linux/kallsyms.h:13,
>                  from kernel/kallsyms.c:15:
> kernel/kallsyms.c: In function ‘__sprint_symbol’:
> ./include/linux/fortify-string.h:122:33: error: ‘__builtin_strcpy’ source argument is the same as destination [-Werror=restrict]
>   122 | #define __underlying_strcpy     __builtin_strcpy
>       |                                 ^
> ./include/linux/fortify-string.h:787:24: note: in expansion of macro ‘__underlying_strcpy’
>   787 |                 return __underlying_strcpy(p, q);
>       |                        ^~~~~~~~~~~~~~~~~~~

But the code is already checking "name != buffer". Does anyone know
what is actually happening here where we have an _overlap_, but not
identical strings?

-Kees

[1] https://lore.kernel.org/lkml/202406271127.CEAE5F4E@keescook/

-- 
Kees Cook

      reply	other threads:[~2024-06-28 16:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-04  4:42 [PATCH] sprint_symbol: Replace strcpy with memmove to handle potential overlap liujinlong
2024-06-28 16:56 ` 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=202406280952.28AFD754@keescook \
    --to=kees@kernel.org \
    --cc=ardb@kernel.org \
    --cc=azeemshaikh38@gmail.com \
    --cc=kernel-bot@kylinos.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liujinlong@kylinos.cn \
    --cc=maninder1.s@samsung.com \
    --cc=mingliu6104@gmail.com \
    --cc=mtodorovac69@gmail.com \
    --cc=ndesaulniers@google.com \
    --cc=song@kernel.org \
    --cc=thunder.leizhen@huawei.com \
    --cc=yonghong.song@linux.dev \
    /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.