sized_strscpy() performs word-at-a-time writes to the destination
buffer. If the destination buffer is not aligned to unsigned long,
direct assignment causes UBSAN misaligned-access errors.

Use put_unaligned() to safely write the words to the destination.

Fixes: 30035e45753b7 ("string: provide strscpy()")
Signed-off-by: Fuad Tabba <[email protected]>
---
 lib/string.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index b632c71df1a5..a1697bf72078 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -157,16 +157,16 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t 
count)
                if (has_zero(c, &data, &constants)) {
                        data = prep_zero_mask(c, data, &constants);
                        data = create_zero_mask(data);
-                       *(unsigned long *)(dest+res) = c & zero_bytemask(data);
+                       put_unaligned(c & zero_bytemask(data), (unsigned long 
*)(dest+res));
                        return res + find_zero(data);
                }
                count -= sizeof(unsigned long);
                if (unlikely(!count)) {
                        c &= ALLBUTLAST_BYTE_MASK;
-                       *(unsigned long *)(dest+res) = c;
+                       put_unaligned(c, (unsigned long *)(dest+res));
                        return -E2BIG;
                }
-               *(unsigned long *)(dest+res) = c;
+               put_unaligned(c, (unsigned long *)(dest+res));
                res += sizeof(unsigned long);
                max -= sizeof(unsigned long);
        }
-- 
2.53.0.371.g1d285c8824-goog


Reply via email to