Skip to content

Commit d94c12b

Browse files
pcckees
authored andcommitted
string: Add load_unaligned_zeropad() code path to sized_strscpy()
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]>
1 parent 8ffd015 commit d94c12b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/string.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
119119
if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
120120
return -E2BIG;
121121

122+
#ifndef CONFIG_DCACHE_WORD_ACCESS
122123
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
123124
/*
124125
* If src is unaligned, don't cross a page boundary,
@@ -133,20 +134,26 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
133134
/* If src or dest is unaligned, don't do word-at-a-time. */
134135
if (((long) dest | (long) src) & (sizeof(long) - 1))
135136
max = 0;
137+
#endif
136138
#endif
137139

138140
/*
139-
* read_word_at_a_time() below may read uninitialized bytes after the
140-
* trailing zero and use them in comparisons. Disable this optimization
141-
* under KMSAN to prevent false positive reports.
141+
* load_unaligned_zeropad() or read_word_at_a_time() below may read
142+
* uninitialized bytes after the trailing zero and use them in
143+
* comparisons. Disable this optimization under KMSAN to prevent
144+
* false positive reports.
142145
*/
143146
if (IS_ENABLED(CONFIG_KMSAN))
144147
max = 0;
145148

146149
while (max >= sizeof(unsigned long)) {
147150
unsigned long c, data;
148151

152+
#ifdef CONFIG_DCACHE_WORD_ACCESS
153+
c = load_unaligned_zeropad(src+res);
154+
#else
149155
c = read_word_at_a_time(src+res);
156+
#endif
150157
if (has_zero(c, &data, &constants)) {
151158
data = prep_zero_mask(c, data, &constants);
152159
data = create_zero_mask(data);

0 commit comments

Comments
 (0)