Re: [PATCH] lib/string: Fix UBSAN misaligned access in sized_strscpy

From: David Laight

Date: Tue Feb 24 2026 - 18:07:46 EST


On Tue, 24 Feb 2026 17:54:07 +0000
Fuad Tabba <tabba@xxxxxxxxxx> wrote:

> Hi Andy,
>
> On Tue, 24 Feb 2026 at 17:21, Andy Shevchenko
> <andriy.shevchenko@xxxxxxxxx> wrote:
> >
> > On Tue, Feb 24, 2026 at 05:04:27PM +0000, Fuad Tabba wrote:
> > > 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.
> >
> > Have you measured the performance impact?
>
> Not directly. I verified the disassembly for both x86_64 and aarch64.
> On x86_64, both the raw pointer cast and put_unaligned() compile down
> to mov %rdi,(%rsi). On aarch64, both compile to str x0, [x1].

What happens on cpu that trap misaligned accesses (eg sparc64)?
put_unaligned() exists because it can be horrid.

David

>
> > Have you read the comment near to
> >
> > if (IS_ENABLED(CONFIG_KMSAN))
>
> Not until now to be honest. However, are you asking whether
> put_unaligned() breaks KMSAN? I don't think it does, max is set to 0
> when KMSAN is enabled, this entire while loop is bypassed.
>
> Thanks,
> /fuad
>
> > ?
> >
> > --
> > With Best Regards,
> > Andy Shevchenko
> >
> >
>