From: Marc Zyngier <maz@kernel.org>
To: Fuad Tabba <tabba@google.com>
Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
oliver.upton@linux.dev, will@kernel.org, joey.gouly@arm.com,
suzuki.poulose@arm.com, yuzenghui@huawei.com,
catalin.marinas@arm.com
Subject: Re: [PATCH v1 2/4] KVM: arm64: Add compile-time type check for register in __vcpu_assign_sys_reg()
Date: Tue, 28 Oct 2025 10:51:39 +0000 [thread overview]
Message-ID: <86y0ovuwn8.wl-maz@kernel.org> (raw)
In-Reply-To: <20251027113943.1282568-3-tabba@google.com>
On Mon, 27 Oct 2025 11:39:41 +0000,
Fuad Tabba <tabba@google.com> wrote:
>
> The 'r' (register) and 'val' (value) parameters in
> __vcpu_assign_sys_reg() can be easily transposed. The register ID is an
> enum, whereas the value is a u64. This has led to bugs in the past, and
> the risk was increased by the historically inconsistent parameter
> ordering of the vcpu_write_sys_reg() function.
>
> To prevent this class of bugs, add a compile-time type compatibility check
> to prevent the 'r' parameter from having a 'u64' type.
>
> This directly catches the erroneous transposition (passing the 'u64'
> value as 'r') while remaining flexible, as it does not force 'r' to
> be a specific enum type.
I don't think that's enough. It is too easy to pass a u32 and
accidentally bypass this check. I really think we should redefine the
register identifiers to be a fixed value structure, something like
this:
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 7501a2ee4dd44..18f65dbff2379 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -624,6 +624,12 @@ enum vcpu_sysreg {
NR_SYS_REGS /* Nothing after this line! */
};
+struct vcpu_sys_reg {
+ enum vcpu_sysreg reg;
+};
+
+static const struct vcpu_sys_reg vcpu_sys_reg_SCTLR_EL1 = { SCTLR_EL1 };
+
struct kvm_sysreg_masks {
struct {
u64 res0;
and then enforce that whatever is interpreted as a system register is
actually one of these structures (with some creative cpp wrapping to
make it less awkward).
In a way, this is similar to what we are doing for page table
manipulation, where pte_t is a structure wrapping a u64.
If we move to such construct, it becomes impossible to swap value and
register, and we can stop worrying about that.
Thoughts?
M.
--
Without deviation from the norm, progress is not possible.
next prev parent reply other threads:[~2025-10-28 10:51 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-27 11:39 [PATCH v1 0/4] KVM: arm64: Prevent sysreg helper parameter transposition Fuad Tabba
2025-10-27 11:39 ` [PATCH v1 1/4] KVM: arm64: Switch reg and val parameter ordering in vcpu_write_sys_reg() Fuad Tabba
2025-10-28 10:38 ` Marc Zyngier
2025-10-28 10:40 ` Fuad Tabba
2025-10-27 11:39 ` [PATCH v1 2/4] KVM: arm64: Add compile-time type check for register in __vcpu_assign_sys_reg() Fuad Tabba
2025-10-28 10:51 ` Marc Zyngier [this message]
2025-10-28 10:58 ` Fuad Tabba
2025-10-28 17:05 ` Fuad Tabba
2025-10-30 9:53 ` Fuad Tabba
2025-10-27 11:39 ` [PATCH v1 3/4] KVM: arm64: Add compile-time type check to vcpu_write_sys_reg() Fuad Tabba
2025-10-27 11:39 ` [PATCH v1 4/4] KVM: arm64: Add compile-time type check for register in __vcpu_rmw_sys_reg() Fuad Tabba
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=86y0ovuwn8.wl-maz@kernel.org \
--to=maz@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=joey.gouly@arm.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=oliver.upton@linux.dev \
--cc=suzuki.poulose@arm.com \
--cc=tabba@google.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.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;
as well as URLs for NNTP newsgroup(s).