mirror of
https://kernel.googlesource.com/pub/scm/linux/kernel/git/stable/linux-stable.git
synced 2025-09-13 11:07:46 +10:00
string: Add load_unaligned_zeropad() code path to sized_strscpy()
commitd94c12bd97
upstream. The call to read_word_at_a_time() in sized_strscpy() is problematic with MTE because it may trigger a tag check fault when reading across a tag granule (16 bytes) boundary. To make this code MTE compatible, let's start using load_unaligned_zeropad() on architectures where it is available (i.e. architectures that define CONFIG_DCACHE_WORD_ACCESS). Because load_unaligned_zeropad() takes care of page boundaries as well as tag granule boundaries, also disable the code preventing crossing page boundaries when using load_unaligned_zeropad(). Signed-off-by: Peter Collingbourne <pcc@google.com> Link: https://linux-review.googlesource.com/id/If4b22e43b5a4ca49726b4bf98ada827fdf755548 Fixes:94ab5b61ee
("kasan, arm64: enable CONFIG_KASAN_HW_TAGS") Cc: stable@vger.kernel.org Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Link: https://lore.kernel.org/r/20250403000703.2584581-2-pcc@google.com Signed-off-by: Kees Cook <kees@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
9042efa7f1
commit
5f878db827
13
lib/string.c
13
lib/string.c
@ -113,6 +113,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
|
||||
if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
|
||||
return -E2BIG;
|
||||
|
||||
#ifndef CONFIG_DCACHE_WORD_ACCESS
|
||||
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
|
||||
/*
|
||||
* If src is unaligned, don't cross a page boundary,
|
||||
@ -127,12 +128,14 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
|
||||
/* If src or dest is unaligned, don't do word-at-a-time. */
|
||||
if (((long) dest | (long) src) & (sizeof(long) - 1))
|
||||
max = 0;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* read_word_at_a_time() below may read uninitialized bytes after the
|
||||
* trailing zero and use them in comparisons. Disable this optimization
|
||||
* under KMSAN to prevent false positive reports.
|
||||
* load_unaligned_zeropad() or read_word_at_a_time() below may read
|
||||
* uninitialized bytes after the trailing zero and use them in
|
||||
* comparisons. Disable this optimization under KMSAN to prevent
|
||||
* false positive reports.
|
||||
*/
|
||||
if (IS_ENABLED(CONFIG_KMSAN))
|
||||
max = 0;
|
||||
@ -140,7 +143,11 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
|
||||
while (max >= sizeof(unsigned long)) {
|
||||
unsigned long c, data;
|
||||
|
||||
#ifdef CONFIG_DCACHE_WORD_ACCESS
|
||||
c = load_unaligned_zeropad(src+res);
|
||||
#else
|
||||
c = read_word_at_a_time(src+res);
|
||||
#endif
|
||||
if (has_zero(c, &data, &constants)) {
|
||||
data = prep_zero_mask(c, data, &constants);
|
||||
data = create_zero_mask(data);
|
||||
|
Loading…
Reference in New Issue
Block a user