From: Jisheng Zhang <jszhang@kernel.org>
To: Yunhui Cui <cuiyunhui@bytedance.com>
Cc: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
alex@ghiti.fr, zong.li@sifive.com, wangruikang@iscas.ac.cn,
debug@rivosinc.com, qirui.001@bytedance.com,
guoziyi114@gmail.com, samuel.holland@sifive.com,
ben.dooks@codethink.co.uk, cyrilbur@tenstorrent.com,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] riscv: Fix SR_SUM leakage across context switches
Date: Thu, 22 Oct 2026 19:08:39 +0800 [thread overview]
Message-ID: <atnut7gKRKzDoqBf@xhacker> (raw)
In-Reply-To: <20261022083212.84721-1-cuiyunhui@bytedance.com>
On Thu, Oct 22, 2026 at 04:32:12PM +0800, Yunhui Cui wrote:
> __switch_to() saves the sstatus value of prev and restores the SR_SUM bit
> of next with csrs. However, csrs only sets bits. If prev has SR_SUM set
> and next has SR_SUM clear in its saved state, the old SR_SUM bit remains
> set in sstatus.
>
> This lets next run with user memory access enabled even though its saved
> SR_SUM state is clear.
>
> Use csrrc to save the sstatus value of prev and clear SR_SUM from the
> live CSR before restoring the saved SR_SUM state of next. This keeps
> prev's SR_SUM state intact, while preventing it from leaking to next.
>
> Fixes: 788aa64c01f1 ("riscv: save the SR_SUM status over switches")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
Reviewed-by: Jisheng Zhang <jszhang@kernel.org>
> ---
> arch/riscv/kernel/entry.S | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
> index 61b7a77166971..628b85e3915a7 100644
> --- a/arch/riscv/kernel/entry.S
> +++ b/arch/riscv/kernel/entry.S
> @@ -443,15 +443,15 @@ SYM_FUNC_START(__switch_to)
> REG_S s10, TASK_THREAD_S10_RA(a3)
> REG_S s11, TASK_THREAD_S11_RA(a3)
>
> - /* save the user space access flag */
> - csrr s0, CSR_STATUS
> + /* Save and clear the user space access flag. */
> + li s1, SR_SUM
> + csrrc s0, CSR_STATUS, s1
> REG_S s0, TASK_THREAD_SUM_RA(a3)
>
> /* Save the kernel shadow call stack pointer */
> scs_save_current
> /* Restore context from next->thread */
> REG_L s0, TASK_THREAD_SUM_RA(a4)
> - li s1, SR_SUM
> and s0, s0, s1
> csrs CSR_STATUS, s0
> REG_L ra, TASK_THREAD_RA_RA(a4)
> --
> 2.39.5
>
>
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2026-10-22 11:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-22 8:32 Yunhui Cui
2026-10-22 11:08 ` Jisheng Zhang [this message]
2026-10-22 22:19 ` Andy Chiu
2026-10-23 7:59 ` Ben Dooks
2026-10-23 14:50 ` [External] " yunhui cui
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=atnut7gKRKzDoqBf@xhacker \
--to=jszhang@kernel.org \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=ben.dooks@codethink.co.uk \
--cc=cuiyunhui@bytedance.com \
--cc=cyrilbur@tenstorrent.com \
--cc=debug@rivosinc.com \
--cc=guoziyi114@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=qirui.001@bytedance.com \
--cc=samuel.holland@sifive.com \
--cc=stable@vger.kernel.org \
--cc=wangruikang@iscas.ac.cn \
--cc=zong.li@sifive.com \
/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®