public inbox for linux-hardening@vger.kernel.org
 help / color / mirror / Atom feed
From: Fuad Tabba <tabba@google.com>
To: Kees Cook <kees@kernel.org>, Andy Shevchenko <andy@kernel.org>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	 will@kernel.org, tabba@google.com
Subject: [PATCH] lib/string: Fix UBSAN misaligned access in sized_strscpy
Date: Tue, 24 Feb 2026 17:04:27 +0000	[thread overview]
Message-ID: <20260224170427.2296592-1-tabba@google.com> (raw)

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 <tabba@google.com>
---
 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	other threads:[~2026-02-24 17:04 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-24 17:04 Fuad Tabba [this message]
2026-02-24 17:21 ` [PATCH] lib/string: Fix UBSAN misaligned access in sized_strscpy Andy Shevchenko
2026-02-24 17:54   ` Fuad Tabba
2026-02-24 23:06     ` David Laight
2026-02-25  8:33       ` Fuad Tabba
2026-02-25 10:11         ` David Laight
2026-02-25 11:09           ` Fuad Tabba
2026-02-24 21:07 ` Kees Cook
2026-02-25  8:08   ` Fuad Tabba
2026-02-25  9:32     ` Andy Shevchenko
2026-02-25 14:40     ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260224170427.2296592-1-tabba@google.com \
    --to=tabba@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=andy@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox