public inbox for linux-hardening@vger.kernel.org
 help / color / mirror / Atom feed
From: Josh Law <objecting@objecting.org>
To: kees@kernel.org, akpm@linux-foundation.org
Cc: andy@kernel.org, linux-hardening@vger.kernel.org,
	linux-kernel@vger.kernel.org, Josh Law <objecting@objecting.org>
Subject: [PATCH] lib/string: rewrite strlcat() to use sized_strscpy()
Date: Mon, 16 Mar 2026 16:49:39 +0000	[thread overview]
Message-ID: <20260316164939.89349-1-objecting@objecting.org> (raw)

Replace the hand-rolled copy logic and BUG_ON() in strlcat() with a
call to sized_strscpy(). This removes the kernel panic on caller
misuse (dsize >= count) and instead returns a truncation indicator
consistent with BSD strlcat semantics.

strlcat() is deprecated and callers should be converted to strscpy()
or similar bounded interfaces.

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/string.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index b632c71df1a5..414016f8a6a6 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -253,18 +253,13 @@ size_t strlcat(char *dest, const char *src, size_t count)
 {
 	size_t dsize = strlen(dest);
 	size_t len = strlen(src);
-	size_t res = dsize + len;
 
-	/* This would be a bug */
-	BUG_ON(dsize >= count);
+	if (dsize >= count)
+		return count + len;
 
-	dest += dsize;
-	count -= dsize;
-	if (len >= count)
-		len = count-1;
-	__builtin_memcpy(dest, src, len);
-	dest[len] = 0;
-	return res;
+	sized_strscpy(dest + dsize, src, count - dsize);
+
+	return dsize + len;
 }
 EXPORT_SYMBOL(strlcat);
 #endif
-- 
2.34.1


             reply	other threads:[~2026-03-16 16:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-16 16:49 Josh Law [this message]
2026-03-16 16:53 ` [PATCH] lib/string: rewrite strlcat() to use sized_strscpy() Andy Shevchenko

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=20260316164939.89349-1-objecting@objecting.org \
    --to=objecting@objecting.org \
    --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 \
    /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