History

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().

Peter Collingbourne committed 1 year ago

Commit d94c12b1 parent 8ffd0151 file · +10 −3Browse files

Showing the changes to lib/string.c (1 of 1 file). Show all

Unified · Split

lib/string.c+10−3
@@ -119,6 +119,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count) · 118 unmodified lines
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,12 +134,14 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count) · 8 unmodified lines
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;
@@ -146,7 +149,11 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count) · 1 unmodified line
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);