From: Kees Cook <kees@kernel.org>
To: Guo Ren <guoren@kernel.org>
Cc: Kees Cook <kees@kernel.org>, kernel test robot <lkp@intel.com>,
Arnd Bergmann <arnd@arndb.de>,
Geert Uytterhoeven <geert@linux-m68k.org>,
Stafford Horne <shorne@gmail.com>,
Thomas Gleixner <tglx@linutronix.de>,
"Mike Rapoport (IBM)" <rppt@kernel.org>,
Yan Zhao <yan.y.zhao@intel.com>,
Linus Walleij <linus.walleij@linaro.org>,
Vincenzo Frascino <vincenzo.frascino@arm.com>,
linux-csky@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-hardening@vger.kernel.org
Subject: [PATCH] csky: string.h: Provide basic sanity checks for fallback memcpy()
Date: Thu, 22 May 2025 21:27:39 -0700 [thread overview]
Message-ID: <20250523042738.work.777-kees@kernel.org> (raw)
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.
Link: https://lore.kernel.org/all/202505191117.C094A90F88@keescook/ [1]
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/all/202501040747.S3LYfvYq-lkp@intel.com/
Signed-off-by: Kees Cook <kees@kernel.org>
---
v2: split this off to csky
v1: https://lore.kernel.org/lkml/20250520163320.work.924-kees@kernel.org/
Cc: Guo Ren <guoren@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Stafford Horne <shorne@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: "Mike Rapoport (IBM)" <rppt@kernel.org>
Cc: Yan Zhao <yan.y.zhao@intel.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: <linux-csky@vger.kernel.org>
---
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(-)
diff --git a/arch/csky/abiv1/inc/abi/string.h b/arch/csky/abiv1/inc/abi/string.h
index de50117b904d..9e780877d8ab 100644
--- a/arch/csky/abiv1/inc/abi/string.h
+++ b/arch/csky/abiv1/inc/abi/string.h
@@ -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);
diff --git a/arch/csky/abiv2/inc/abi/string.h b/arch/csky/abiv2/inc/abi/string.h
index f01bad2ac4fb..e66d5d2f7e52 100644
--- a/arch/csky/abiv2/inc/abi/string.h
+++ b/arch/csky/abiv2/inc/abi/string.h
@@ -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);
diff --git a/arch/csky/include/asm/page.h b/arch/csky/include/asm/page.h
index 4911d0892b71..069971389ce6 100644
--- a/arch/csky/include/asm/page.h
+++ b/arch/csky/include/asm/page.h
@@ -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)
--
2.34.1
reply other threads:[~2025-05-23 4:27 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20250523042738.work.777-kees@kernel.org \
--to=kees@kernel.org \
--cc=arnd@arndb.de \
--cc=geert@linux-m68k.org \
--cc=guoren@kernel.org \
--cc=linus.walleij@linaro.org \
--cc=linux-csky@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lkp@intel.com \
--cc=rppt@kernel.org \
--cc=shorne@gmail.com \
--cc=tglx@linutronix.de \
--cc=vincenzo.frascino@arm.com \
--cc=yan.y.zhao@intel.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 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.