Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: stable@vger.kernel.org
Cc: Yunhui Cui <cuiyunhui@bytedance.com>,
	Jisheng Zhang <jszhang@kernel.org>,
	Andy Chiu <tchiu@tenstorrent.com>,
	Yicong Yang <yang.yicong@picoheart.com>,
	Paul Walmsley <pjw@kernel.org>, Sasha Levin <sashal@kernel.org>
Subject: [PATCH 6.6.y] riscv: Fix SR_SUM leakage across context switches
Date: Thu,  3 Dec 2026 23:26:14 -0500	[thread overview]
Message-ID: <20261204042602.2598716-1-sashal@kernel.org> (raw)
In-Reply-To: <2026120213-mortuary-lying-96d8@gregkh>

From: Yunhui Cui <cuiyunhui@bytedance.com>

[ Upstream commit 807b09be9c8f234ce758bcf43101f34e0a036b83 ]

__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>
Reviewed-by: Andy Chiu <tchiu@tenstorrent.com>
Reviewed-by: Yicong Yang <yang.yicong@picoheart.com>
Link: https://patch.msgid.link/20261022083212.84721-1-cuiyunhui@bytedance.com
Signed-off-by: Paul Walmsley <pjw@kernel.org>
[ sashal: Adapted to the older layout by keeping the existing SR_SUM mask and swapping the save-side csrr for csrrc, leaving the restore side unchanged and using the older TASK_THREAD_STATUS_RA name. ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 arch/riscv/kernel/entry.S | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index b9d321fb1f754..f25ab9aed2209 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -257,9 +257,9 @@ 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 */
+	/* Save and clear the user space access flag. */
 	li    s0, SR_SUM
-	csrr  s1, CSR_STATUS
+	csrrc s1, CSR_STATUS, s0
 	and   s1, s1, s0
 	REG_S s1, TASK_THREAD_STATUS_RA(a3)
 
-- 
2.53.0


      reply	other threads:[~2026-12-04  4:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 13:41 FAILED: patch "[PATCH] riscv: Fix SR_SUM leakage across context switches" failed to apply to 6.6-stable tree gregkh
2026-12-04  4:26 ` Sasha Levin [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=20261204042602.2598716-1-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=cuiyunhui@bytedance.com \
    --cc=jszhang@kernel.org \
    --cc=pjw@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tchiu@tenstorrent.com \
    --cc=yang.yicong@picoheart.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