kernel/kallsyms.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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
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
© 2016 - 2026 Red Hat, Inc.