From: Alexander Grund <theflamefire89@gmail.com>
To: cip-dev@lists.cip-project.org
Cc: uli+cip@fpond.eu
Subject: [PATCH 4.4 3/4] string: uninline memcpy_and_pad
Date: Thu, 11 Jan 2024 20:06:57 +0100 [thread overview]
Message-ID: <20240111190658.153488-4-theflamefire89@gmail.com> (raw)
In-Reply-To: <20240111190658.153488-1-theflamefire89@gmail.com>
From: Guenter Roeck <linux@roeck-us.net>
commit 5c4e0a21fae877a7ef89be6dcc6263ec672372b8 upstream.
When building m68k:allmodconfig, recent versions of gcc generate the
following error if the length of UTS_RELEASE is less than 8 bytes.
In function 'memcpy_and_pad',
inlined from 'nvmet_execute_disc_identify' at
drivers/nvme/target/discovery.c:268:2: arch/m68k/include/asm/string.h:72:25: error:
'__builtin_memcpy' reading 8 bytes from a region of size 7
Discussions around the problem suggest that this only happens if an
architecture does not provide strlen(), if -ffreestanding is provided as
compiler option, and if CONFIG_FORTIFY_SOURCE=n. All of this is the case
for m68k. The exact reasons are unknown, but seem to be related to the
ability of the compiler to evaluate the return value of strlen() and
the resulting execution flow in memcpy_and_pad(). It would be possible
to work around the problem by using sizeof(UTS_RELEASE) instead of
strlen(UTS_RELEASE), but that would only postpone the problem until the
function is called in a similar way. Uninline memcpy_and_pad() instead
to solve the problem for good.
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Reviewed-by: Geert Uytterhoeven <geert@linux-m68k.org>
Acked-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Alexander Grund <theflamefire89@gmail.com>
---
include/linux/string.h | 19 ++-----------------
lib/string_helpers.c | 20 ++++++++++++++++++++
2 files changed, 22 insertions(+), 17 deletions(-)
diff --git a/include/linux/string.h b/include/linux/string.h
index c874f5ea9bb3e..9f745d7e9f3f7 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -204,22 +204,7 @@ static inline const char *kbasename(const char *path)
return tail ? tail + 1 : path;
}
-/**
- * memcpy_and_pad - Copy one buffer to another with padding
- * @dest: Where to copy to
- * @dest_len: The destination buffer size
- * @src: Where to copy from
- * @count: The number of bytes to copy
- * @pad: Character to use for padding if space is left in destination.
- */
-static inline void memcpy_and_pad(void *dest, size_t dest_len,
- const void *src, size_t count, int pad)
-{
- if (dest_len > count) {
- memcpy(dest, src, count);
- memset(dest + count, pad, dest_len - count);
- } else
- memcpy(dest, src, dest_len);
-}
+void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
+ int pad);
#endif /* _LINUX_STRING_H_ */
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 5c88204b6f1f1..f46075b3d9e41 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -534,3 +534,23 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
return p - dst;
}
EXPORT_SYMBOL(string_escape_mem);
+
+/**
+ * memcpy_and_pad - Copy one buffer to another with padding
+ * @dest: Where to copy to
+ * @dest_len: The destination buffer size
+ * @src: Where to copy from
+ * @count: The number of bytes to copy
+ * @pad: Character to use for padding if space is left in destination.
+ */
+void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
+ int pad)
+{
+ if (dest_len > count) {
+ memcpy(dest, src, count);
+ memset(dest + count, pad, dest_len - count);
+ } else {
+ memcpy(dest, src, dest_len);
+ }
+}
+EXPORT_SYMBOL(memcpy_and_pad);
--
2.40.1
next prev parent reply other threads:[~2024-01-11 19:08 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-11 19:06 [PATCH 4.4 0/4] Bluetooth: hci_sock: Fix possible OOB write in create_monitor_event Alexander Grund
2024-01-11 19:06 ` [PATCH 4.4 1/4] string.h: add memcpy_and_pad() Alexander Grund
2024-01-11 19:06 ` [PATCH 4.4 2/4] string.h: un-fortify memcpy_and_pad Alexander Grund
2024-01-11 19:06 ` Alexander Grund [this message]
2024-01-11 19:06 ` [PATCH 4.4 4/4] Bluetooth: hci_sock: Correctly bounds check and pad HCI_MON_NEW_INDEX name Alexander Grund
2024-01-15 19:50 ` [cip-dev] " Pavel Machek
2024-01-15 20:05 ` [cip-dev] [PATCH 4.4 0/4] Bluetooth: hci_sock: Fix possible OOB write in create_monitor_event Pavel Machek
2024-01-29 2:06 ` Ulrich Hecht
2024-01-29 2:04 ` Ulrich Hecht
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=20240111190658.153488-4-theflamefire89@gmail.com \
--to=theflamefire89@gmail.com \
--cc=cip-dev@lists.cip-project.org \
--cc=uli+cip@fpond.eu \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox