The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] sprint_symbol: Replace strcpy with memmove to handle potential overlap
@ 2024-06-04  4:42 liujinlong
  2024-06-28 16:56 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: liujinlong @ 2024-06-04  4:42 UTC (permalink / raw)
  To: kees
  Cc: thunder.leizhen, yonghong.song, ndesaulniers, song, ardb,
	maninder1.s, azeemshaikh38, linux-kernel, liujinlong, k2ci

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;
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] sprint_symbol: Replace strcpy with memmove to handle potential overlap
  2024-06-04  4:42 [PATCH] sprint_symbol: Replace strcpy with memmove to handle potential overlap liujinlong
@ 2024-06-28 16:56 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2024-06-28 16:56 UTC (permalink / raw)
  To: liujinlong
  Cc: thunder.leizhen, yonghong.song, ndesaulniers, song, ardb,
	maninder1.s, azeemshaikh38, linux-kernel, liujinlong, k2ci,
	Mirsad Todorovac

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-06-28 16:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox