Skip to contentSkip to conversation

csky: string.h: Provide basic sanity checks for fallback memcpy()

PATCH New Kees Cook 1 participant 1 message May 23, 2025 C-sky architecture

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 csky: string.h: Provide basic sanity checks for fallback memcpy()

Add basic sanity checking for pathological "size" arguments to memcpy(). Besides the run-time checking benefit, this avoids GCC trying to be very smart about value range tracking[1] when CONFIG_PROFILE_ALL_BRANCHES=y but FORTIFY_SOURCE=n.

Additionally avoid duplicate memcpy definitions in page.h.

---
v2: split this off to csky
v1: https://lore.kernel.org/lkml/[email protected]/
Cc: Guo Ren <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Geert Uytterhoeven <[email protected]>
Cc: Stafford Horne <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: "Mike Rapoport (IBM)" <[email protected]>
Cc: Yan Zhao <[email protected]>
Cc: Linus Walleij <[email protected]>
Cc: Vincenzo Frascino <[email protected]>
Cc: <[email protected]>

---
 arch/csky/abiv1/inc/abi/string.h | 11 +++++++++++
 arch/csky/abiv2/inc/abi/string.h | 11 +++++++++++
 arch/csky/include/asm/page.h     |  4 +---
 3 files changed, 23 insertions(+), 3 deletions(-)
arch/csky/abiv1/inc/abi/string.h +11 -0
index de50117b904d..9e780877d8ab 100644
@@ -6,6 +6,17 @@
 #define __HAVE_ARCH_MEMCPY
 extern void *memcpy(void *, const void *, __kernel_size_t);
 
+#ifndef CONFIG_FORTIFY_SOURCE
+#define memcpy(t, f, n)					\
+	({						\
+		typeof(n) __n = (n);			\
+		/* Skip impossible sizes. */		\
+		if (!(__n < 0 || __n == SIZE_MAX))	\
+			__builtin_memcpy(t, f, __n);	\
+		(t);					\
+	})
+#endif /* !CONFIG_FORTIFY_SOURCE */
+
 #define __HAVE_ARCH_MEMMOVE
 extern void *memmove(void *, const void *, __kernel_size_t);
 
arch/csky/abiv2/inc/abi/string.h +11 -0
index f01bad2ac4fb..e66d5d2f7e52 100644
@@ -9,6 +9,17 @@ extern int memcmp(const void *, const void *, __kernel_size_t);
 #define __HAVE_ARCH_MEMCPY
 extern void *memcpy(void *, const void *, __kernel_size_t);
 
+#ifndef CONFIG_FORTIFY_SOURCE
+#define memcpy(t, f, n)					\
+	({						\
+		typeof(n) __n = (n);			\
+		/* Skip impossible sizes. */		\
+		if (!(__n < 0 || __n == SIZE_MAX))	\
+			__builtin_memcpy(t, f, __n);	\
+		(t);					\
+	})
+#endif /* !CONFIG_FORTIFY_SOURCE */
+
 #define __HAVE_ARCH_MEMMOVE
 extern void *memmove(void *, const void *, __kernel_size_t);
 
arch/csky/include/asm/page.h +1 -3
index 4911d0892b71..069971389ce6 100644
@@ -5,6 +5,7 @@
 
 #include <asm/setup.h>
 #include <asm/cache.h>
+#include <asm/string.h>
 #include <linux/const.h>
 
 #include <vdso/page.h>
@@ -33,9 +34,6 @@
 #define virt_addr_valid(kaddr)  ((void *)(kaddr) >= (void *)PAGE_OFFSET && \
 			(void *)(kaddr) < high_memory)
 
-extern void *memset(void *dest, int c, size_t l);
-extern void *memcpy(void *to, const void *from, size_t l);
-
 #define clear_page(page)	memset((page), 0, PAGE_SIZE)
 #define copy_page(to, from)	memcpy((to), (from), PAGE_SIZE)
 
Signature · 1 line
2.34.1