Re: [PATCH] riscv: cfi: parse riscv_nousercfi before riscv_fill_hwcap()

From: Guodong Xu

Date: Sat Dec 05 2026 - 07:07:51 EST


On Wed, 18 Nov 2026 21:09:53 +0100, Radim Krcmar wrote:
> 2026-11-08T04:42:01-05:00, Guodong Xu <guodong.xu@xxxxxxxxxxxxxxxx>:
> > ISA bitmaps don't honour riscv_nousercfi because the ISA bitmaps are
> > composed during setup_arch(), while riscv_nousercfi is a __setup()
> > parameter, and those are parsed from start_kernel() after setup_arch()
> > has returned.
> >
> > As a consequence, the riscv_nousercfi test in riscv_cfilp_validate() and
> > riscv_cfiss_validate() always sees zero, and Zicfilp and Zicfiss stay in
> > the ISA bitmaps even when user CFI is disabled on the kernel command
> > line.
>
> (I thought that was the intention.)
>
> riscv_nousercfi definintely should disable the hwprobe reporting of
> those features (my series did that), and prevent their enablement in
> user-mode.

Thanks for the review, Radim. Yes, that's what I expect too. And it
is also what kernel-parameters.txt says the option does ("Disable
user CFI ABI to userspace even if cpu extension are available"), I
mean the user-mode part.

> Why disable Zicfilp and Zicfiss in the ISA bitmaps by setting nousercfi,
> though? Aren't the validate functions just misusing riscv_nousercfi
> instead of defining a clear detection mechanism?

Then the kernel part, as you pointed out, the real issue seems to be the
misuse of a user-mode facing variable in the kernel facing ISA bitmaps.
So my next question is whether a new variable is needed (I tend to say
yes) to record what hardware and SBI firmware support.

In the current code riscv_nousercfi has "firmware refused shadow stack"
(in head.S) merged with "kernel cmdline asked for user CFI off". I think
this is a confusing design.

It also leads to a bug actually. When the FWFT SHADOW_STACK call fails on
a secondary hart, head.S stores the bcfi value to riscv_nousercfi (by
then, the kernel cmdline parsing is already finished) and overwrites
whatever the command line had set:

beqz a0, 1f
la a1, riscv_nousercfi
li a0, CMDLINE_DISABLE_RISCV_USERCFI_BCFI
REG_S a0, (a1) /* a plain store, not an OR */

With riscv_nousercfi=all or riscv_nousercfi=fcfi the fcfi bit is
lost, so landing pads stay available to user space although the
user asked to disable them. I see this under QEMU with 4 harts and
an OpenSBI that refuses FWFT SHADOW_STACK: the boot log says
"landing pad status : disabled", but prctl(PR_GET_CFI) and
prctl(PR_SET_CFI) with PR_CFI_ENABLE both succeed.

Two more things about the current code.

1) head.S, on the boot hart and on secondary harts, only asks for and
checks bcfi (SBI_FWFT_SHADOW_STACK). It does not do the same for
fcfi (SBI_FWFT_LANDING_PAD). I can't tell if that is by design or an
omission, so I only point it out and would like to hear opinions.

2) The riscv_nousercfi test in riscv_cfilp_validate():

if (!IS_ENABLED(CONFIG_RISCV_USER_CFI) ||
(riscv_nousercfi & CMDLINE_DISABLE_RISCV_USERCFI_FCFI))
return -EINVAL;

is dead code, based on the reason I mentioned in my v1 commit
message: the command line is parsed after the validation has run,
and nothing else sets the fcfi bit.

> I think the ISA bitmaps should consider whether Linux has the
> extensions, which with standard SBI means a successfuly executed FWFT
> set/get.
>
> Linux can then use the ISA bitmap to decide whether to use CFI for
> kernel code, allow it in KVM guests, report it as available hardware
> feature, or allow it in user mode.
>
> nousercfi seems like it should only restrict use of CFI in user mode.
>
> Thanks.

In v2 I plan to add a separate flag for the result of the FWFT call in
head.S (such as 'riscv_fwft_shstk_failed', the name is open), checked in
riscv_cfiss_validate(), and to keep riscv_nousercfi for user mode
only. It also fixes the secondary hart overwrite bug above.

With that, the ISA bitmap validation does not read riscv_nousercfi
any more, so KVM does not depend on riscv_nousercfi in any way.

Radim, Anup, Paul, Deepak, what do you think?

NOTE: today all the code discussed above is wrapped by
CONFIG_RISCV_USER_CFI. With that set to n, the ISA bitmap never has
Zicfilp or Zicfiss, so KVM guests cannot use them either.

Guodong