mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vincent Mailhol <mailhol@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, Vincent Mailhol <mailhol@kernel.org>
Subject: [PATCH 2/2] lib/ucs2_string.c: fix out-of-bounds read in ucs2_strnlen()
Date: Wed, 22 Jul 2026 23:04:53 +0200	[thread overview]
Message-ID: <20260722-fix-ucs2_strnlen-v1-2-ba5bade2b026@kernel.org> (raw)
In-Reply-To: <20260722-fix-ucs2_strnlen-v1-0-ba5bade2b026@kernel.org>

ucs2_strnlen() checks the current character before checking whether the
caller-provided maximum length has been reached. If the input is not
NUL-terminated within that bound, the loop can read one ucs2_char_t past
the limit.

Test the length before dereferencing to prevent an off-by-one
out-of-bounds read.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
Concerning the Fixes: tag, the ucs2_strnlen() function can be traced as
follow:

  - commit 0635eb8a54cf ("Move utf16 functions to kernel core and
    rename") renames it from utf16_strnlen() to ucs2_strnlen() as we
    know it today.

  - commit a2940908391f ("efivars: String functions") renames
    utf8_strlen() to utf16_strnlen().

  - utf8_strlen() is present since the begining of the git history in
    commit 1da177e4c3f4 ("Linux-2.6.12-rc2")

And that of-by-one issue was present since the beginning and survived
all the renamings. This is why I picked 1da177e4c3f4 in the Fixes tag.
---
 lib/ucs2_string.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/ucs2_string.c b/lib/ucs2_string.c
index f07fcdad764b..1f7dd4eb640a 100644
--- a/lib/ucs2_string.c
+++ b/lib/ucs2_string.c
@@ -8,7 +8,7 @@ ucs2_strnlen(const ucs2_char_t *s, size_t maxlength)
 {
 	unsigned long length = 0;
 
-	while (*s++ != 0 && length < maxlength)
+	while (length < maxlength && *s++ != 0)
 		length++;
 	return length;
 }

-- 
2.54.0


  parent reply	other threads:[~2026-07-22 21:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22 21:04 [PATCH 0/2] " Vincent Mailhol
2026-07-22 21:04 ` [PATCH 1/2] lib/ucs2_string.c: fix indentation Vincent Mailhol
2026-07-22 21:04 ` Vincent Mailhol [this message]
2026-07-22 23:01   ` [PATCH 2/2] lib/ucs2_string.c: fix out-of-bounds read in ucs2_strnlen() Andrew Morton
2026-07-23 19:38     ` Vincent Mailhol

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=20260722-fix-ucs2_strnlen-v1-2-ba5bade2b026@kernel.org \
    --to=mailhol@kernel.org \
    --cc=akpm@linux-foundation.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

all inboxes | Powered by JetHome®