Skip to content

Commit 8bc7280

Browse files
pccjfvogel
authored andcommitted
string: Add load_unaligned_zeropad() code path to sized_strscpy()
commit d94c12bd97d567de342fd32599e7cd9e50bfa140 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 <[email protected]> Link: https://linux-review.googlesource.com/id/If4b22e43b5a4ca49726b4bf98ada827fdf755548 Fixes: 94ab5b6 ("kasan, arm64: enable CONFIG_KASAN_HW_TAGS") Cc: [email protected] Reviewed-by: Catalin Marinas <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 5f878db827c08b8bea0590252d6f754eb1ad3a07) Signed-off-by: Jack Vogel <[email protected]>
1 parent c9bc068 commit 8bc7280

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/string.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
113113
if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
114114
return -E2BIG;
115115

116+
#ifndef CONFIG_DCACHE_WORD_ACCESS
116117
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
117118
/*
118119
* If src is unaligned, don't cross a page boundary,
@@ -127,20 +128,26 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
127128
/* If src or dest is unaligned, don't do word-at-a-time. */
128129
if (((long) dest | (long) src) & (sizeof(long) - 1))
129130
max = 0;
131+
#endif
130132
#endif
131133

132134
/*
133-
* read_word_at_a_time() below may read uninitialized bytes after the
134-
* trailing zero and use them in comparisons. Disable this optimization
135-
* under KMSAN to prevent false positive reports.
135+
* load_unaligned_zeropad() or read_word_at_a_time() below may read
136+
* uninitialized bytes after the trailing zero and use them in
137+
* comparisons. Disable this optimization under KMSAN to prevent
138+
* false positive reports.
136139
*/
137140
if (IS_ENABLED(CONFIG_KMSAN))
138141
max = 0;
139142

140143
while (max >= sizeof(unsigned long)) {
141144
unsigned long c, data;
142145

146+
#ifdef CONFIG_DCACHE_WORD_ACCESS
147+
c = load_unaligned_zeropad(src+res);
148+
#else
143149
c = read_word_at_a_time(src+res);
150+
#endif
144151
if (has_zero(c, &data, &constants)) {
145152
data = prep_zero_mask(c, data, &constants);
146153
data = create_zero_mask(data);

0 commit comments

Comments
 (0)