Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Charlie Jenkins" <thecharlesjenkins@gmail.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v3 07/17] riscv: Maintain epc on misaligned emulation error
Date: Wed, 08 Jul 2026 03:55:13 +0000	[thread overview]
Message-ID: <20260708035514.57B7B1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260707-riscv_insn_table-v3-7-3f8e5f75ba97@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Removing `regs->epc = 0` breaks the emulation of misaligned stores that use the `zero` register (`x0`), causing them to leak and write the instruction pointer (EPC) to memory instead of zero.

Pre-existing issues:
- [High] Emulating kernel-mode misaligned memory accesses uses `memcpy()` without exception tables, leading to kernel panics when accessing unmapped memory (e.g., via `bpf_probe_read()` or `get_user()`).
--

--- Patch [7]: [PATCH v3 07/17] riscv: Maintain epc on misaligned emulation error ---
commit 4be6480bf402b9e8440e0f9f14c2dfd93692b4cd
Author: Charlie Jenkins <thecharlesjenkins@gmail.com>

riscv: Maintain epc on misaligned emulation error

Currently the epc is set to zero when there is a misaligned emulation
error. This will print a misleading error message with the current epc
value of zero instead of the epc value that triggered the emulation.
Avoid setting epc to zero to maintain the state of epc.

> diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c
> index 81b7682e6c6db..524215a186e03 100644
> --- a/arch/riscv/kernel/traps_misaligned.c
> +++ b/arch/riscv/kernel/traps_misaligned.c
> @@ -238,8 +238,6 @@ static int handle_scalar_misaligned_load(struct pt_regs *regs)
>  	if (get_insn(regs, epc, &insn))
>  		return -1;
>  
> -	regs->epc = 0;
> -
>  	if ((insn & INSN_MASK_LW) == INSN_MATCH_LW) {
>  		len = 4;
>  		shift = 8 * (sizeof(unsigned long) - len);

[Severity: High]
This is a pre-existing issue, but does emulating kernel-mode misaligned
loads and stores in these handlers risk a kernel panic?

Further down in handle_scalar_misaligned_load() and
handle_scalar_misaligned_store(), the code falls back to memcpy() when
user_mode(regs) is false:

	} else {
		memcpy(&val, (u8 *)addr, len);
	}

If a routine like get_user() or bpf_probe_read() is passed an unmapped,
misaligned pointer, the hardware traps here. Since memcpy() lacks exception
table entries, won't the resulting page fault be unhandled and bypass the
intended safety mechanisms?

[ ... ]

> @@ -349,8 +346,6 @@ static int handle_scalar_misaligned_store(struct pt_regs *regs)
>  	if (get_insn(regs, epc, &insn))
>  		return -1;
>  
> -	regs->epc = 0;
> -
>  	val.data_ulong = GET_RS2(insn, regs);

[Severity: High]
Does removing this assignment cause an information leak when emulating
stores from the zero register (x0)?

The GET_RS2() and GET_RS2C() macros calculate the register value by taking
the byte offset into struct pt_regs. For the zero register (index 0), this
maps to offset 0, which corresponds to regs->epc.

Previously, clearing regs->epc = 0 neutralized this aliasing, allowing
misaligned stores using x0 (e.g., sw x0, 1(a0)) to correctly write zero.
Without this, will the code now read the actual EPC value and write the
faulting instruction pointer to memory instead of zero?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260707-riscv_insn_table-v3-0-3f8e5f75ba97@gmail.com?part=7

  reply	other threads:[~2026-07-08  3:55 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-08  3:34 [PATCH v3 00/17] riscv: Generate riscv instruction functions Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 01/17] riscv: Introduce instruction table generation Charlie Jenkins
2026-07-08  3:50   ` sashiko-bot
2026-07-09  6:23     ` Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 02/17] riscv: alternatives: Use generated instruction headers for patching code Charlie Jenkins
2026-07-08  3:45   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 03/17] riscv: kgdb: Use generated instruction headers Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 04/17] riscv: Add kprobes instruction simulation KUnit Charlie Jenkins
2026-07-08  3:51   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 05/17] riscv: kprobes: Use generated instruction headers Charlie Jenkins
2026-07-08  3:52   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 06/17] riscv: cfi: " Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 07/17] riscv: Maintain epc on misaligned emulation error Charlie Jenkins
2026-07-08  3:55   ` sashiko-bot [this message]
2026-07-08  3:34 ` [PATCH v3 08/17] riscv: Use generated instruction headers for misaligned loads/stores Charlie Jenkins
2026-07-08  3:49   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 09/17] riscv: kvm: Use generated instruction headers for csr code Charlie Jenkins
2026-07-08  3:48   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 10/17] KVM: device: Add test device Charlie Jenkins
2026-07-08  3:48   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 11/17] KVM: riscv: selftests: Add mmio test Charlie Jenkins
2026-07-08  3:59   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 12/17] riscv: kvm: Use generated instruction headers for mmio emulation Charlie Jenkins
2026-07-08  4:01   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 13/17] riscv: kvm: Add emulated test csr Charlie Jenkins
2026-07-08  4:00   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 14/17] KVM: riscv: selftests: Add csr emulation test Charlie Jenkins
2026-07-08  3:58   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 15/17] riscv: kvm: Use generated instruction headers for csr emulation Charlie Jenkins
2026-07-08  4:04   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 16/17] riscv: kexec: Use generated instruction headers for kexec relocations Charlie Jenkins
2026-07-08  4:01   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 17/17] riscv: Remove unused instruction headers Charlie Jenkins
2026-07-08  4:09   ` sashiko-bot
2026-07-09  6:26 ` [syzbot ci] Re: riscv: Generate riscv instruction functions syzbot ci

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=20260708035514.57B7B1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=thecharlesjenkins@gmail.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