mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: <shao.mingyin@zte.com.cn>
Cc: <pjw@kernel.org>, <mikey@neuling.org>, <jiangfeng@kylinos.cn>,
	<palmer@dabbelt.com>, <aou@eecs.berkeley.edu>, <alex@ghiti.fr>,
	<linux-riscv@lists.infradead.org>, <linux-kernel@vger.kernel.org>,
	<zhang.yue5@zte.com.cn>
Subject: Re: [PATCH] riscv: lib: Fix ZBB strnlen wrap-around regression on huge counts
Date: Mon, 14 Sep 2026 08:49:30 +0100	[thread overview]
Message-ID: <20260914084930.739e3832@pumpkin> (raw)
In-Reply-To: <20260828145152578tXQPUG9lxxgbJjmfpuaQz@zte.com.cn>

On Fri, 28 Aug 2026 14:51:52 +0800 (CST)
<shao.mingyin@zte.com.cn> wrote:

> From: Shao Mingyin <shao.mingyin@zte.com.cn>
> 
> commit 5d588c684833 ("riscv: lib: Fix ZBB strnlen reading past count
> boundary") computes the aligned scan boundary from the last valid byte
> (s + count - 1).  When count is huge (e.g. SIZE_MAX, which FORTIFY
> strcat/strlcat pass when the destination size is not known at compile
> time), s + count wraps around and the aligned boundary lands before s.
> The pre-loop guard "bgeu t0, t4, 2f" then always exits for aligned
> strings of 8 or more characters, and strnlen() returns 8 instead of
> the real length.
> 
> This silently truncates strings built by fortified strcat: the dm
> sysfs name attribute shows "live-bas" instead of "live-base", the
> truncated name pollutes the udev database, and blivet/anaconda (as
> well as LVM/dm-crypt/multipath userspace) break on RISC-V systems.
> 
> Detect the wrap-around and saturate the boundary to the top of the
> address space, making the scan equivalent to strlen().  Normal counts
> are unaffected.
> 
> Fixes: 5d588c684833 ("riscv: lib: Fix ZBB strnlen reading past count boundary")
> Signed-off-by: Shao Mingyin <shao.mingyin@zte.com.cn>
> ---
>  arch/riscv/lib/strnlen.S | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/arch/riscv/lib/strnlen.S b/arch/riscv/lib/strnlen.S
> index a8911605c248..2451289ed0f9 100644
> --- a/arch/riscv/lib/strnlen.S
> +++ b/arch/riscv/lib/strnlen.S
> @@ -87,9 +87,19 @@ strnlen_zbb:
>  	 * Aligned boundary.  Use the address of the last valid byte
>  	 * (s + count - 1) to avoid loading a word past the count
>  	 * boundary in the loop below.  count == 0 is handled above.
> +	 *
> +	 * Saturate the boundary when s + count wraps around (very large
> +	 * counts, e.g. SIZE_MAX passed by FORTIFY strcat/strlcat with a
> +	 * destination whose size is unknown at compile time).  Without
> +	 * this, the wrapped boundary lands before s and the pre-loop
> +	 * guard below always exits, returning a truncated length.
> +	 * Saturating makes the scan equivalent to strlen().
>  	 */
>  	add	t4, a0, a1
>  	addi	t4, t4, -1
> +	bgeu	t4, a0, 1f
> +	li	t4, -1
> +1:

Ugg....
That adds a taken branch to the normal path that is likely to get
mispredicted.
Might be better replaced with a cmp, dec, or sequence.

David 


>  	andi	t4, t4, -SZREG
> 
>  	/* Get the first word.  */


      parent reply	other threads:[~2026-09-14  7:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-28  6:51 shao.mingyin
2026-09-11  2:47 ` shao.mingyin
2026-09-14  3:36 ` Michael Neuling
2026-09-14  7:49 ` David Laight [this message]

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=20260914084930.739e3832@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=jiangfeng@kylinos.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=mikey@neuling.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=shao.mingyin@zte.com.cn \
    --cc=zhang.yue5@zte.com.cn \
    /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®