From: Will Deacon <will@kernel.org>
To: "Pierre-Clément Tosi" <ptosi@google.com>
Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org,
kvm@vger.kernel.org, Marc Zyngier <maz@kernel.org>,
Oliver Upton <oliver.upton@linux.dev>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Vincent Donnefort <vdonnefort@google.com>
Subject: Re: [PATCH v3 09/12] KVM: arm64: VHE: Add test module for hyp kCFI
Date: Mon, 13 May 2024 18:21:21 +0100 [thread overview]
Message-ID: <20240513172120.GA29051@willie-the-truck> (raw)
In-Reply-To: <20240510112645.3625702-10-ptosi@google.com>
On Fri, May 10, 2024 at 12:26:38PM +0100, Pierre-Clément Tosi wrote:
> In order to easily periodically (and potentially automatically) validate
> that the hypervisor kCFI feature doesn't bitrot, introduce a way to
> trigger hypervisor kCFI faults from userspace on test builds of KVM.
>
> Add hooks in the hypervisor code to call registered callbacks (intended
> to trigger kCFI faults either for the callback call itself of from
> within the callback function) when running with guest or host VBAR_EL2.
> As the calls are issued from the KVM_RUN ioctl handling path, userspace
> gains control over when the actual triggering of the fault happens
> without needing to modify the KVM uAPI.
>
> Export kernel functions to register these callbacks from modules and
> introduce a kernel module intended to contain any testing logic. By
> limiting the changes to the core kernel to a strict minimum, this
> architectural split allows tests to be updated (within the module)
> without the need to redeploy (or recompile) the kernel (hyp) under test.
>
> Use the module parameters as the uAPI for configuring the fault
> condition being tested (i.e. either at insertion or post-insertion
> using /sys/module/.../parameters), which naturally makes it impossible
> for userspace to test kCFI without the module (and, inversely, makes
> the module only - not KVM - responsible for exposing said uAPI).
>
> As kCFI is implemented with a caller-side check of a callee-side value,
> make the module support 4 tests based on the location of the caller and
> callee (built-in or in-module), for each of the 2 hypervisor contexts
> (host & guest), selected by userspace using the 'guest' or 'host' module
> parameter. For this purpose, export symbols which the module can use to
> configure the callbacks for in-kernel and module-to-built-in kCFI
> faulting calls.
>
> Define the module-to-kernel API to allow the module to detect that it
> was loaded on a kernel built with support for it but which is running
> without a hypervisor (-ENXIO) or with one that doesn't use the VHE CPU
> feature (-EOPNOTSUPP), which is currently the only mode for which KVM
> supports hypervisor kCFI.
>
> Allow kernel build configs to set CONFIG_HYP_CFI_TEST to only support
> the in-kernel hooks (=y) or also build the test module (=m). Use
> intermediate internal Kconfig flags (CONFIG_HYP_SUPPORTS_CFI_TEST and
> CONFIG_HYP_CFI_TEST_MODULE) to simplify the Makefiles and #ifdefs. As
> the symbols for callback registration are only exported to modules when
> CONFIG_HYP_CFI_TEST != n, it is impossible for the test module to be
> non-forcefully inserted on a kernel that doesn't support it.
>
> Note that this feature must NOT result in any noticeable change
> (behavioral or binary size) when HYP_CFI_TEST_MODULE = n.
>
> CONFIG_HYP_CFI_TEST is intentionally independent of CONFIG_CFI_CLANG, to
> avoid arbitrarily limiting the number of flag combinations that can be
> tested with the module.
>
> Also note that, as VHE aliases VBAR_EL1 to VBAR_EL2 for the host,
> testing hypervisor kCFI in VHE and in host context is equivalent to
> testing kCFI support of the kernel itself i.e. EL1 in non-VHE and/or in
> non-virtualized environments. For this reason, CONFIG_CFI_PERMISSIVE
> **will** prevent the test module from triggering a hyp panic (although a
> warning still gets printed) in that context.
>
> Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
> ---
> arch/arm64/include/asm/kvm_cfi.h | 36 ++++++++
> arch/arm64/kvm/Kconfig | 22 +++++
> arch/arm64/kvm/Makefile | 3 +
> arch/arm64/kvm/hyp/include/hyp/cfi.h | 47 ++++++++++
> arch/arm64/kvm/hyp/vhe/Makefile | 1 +
> arch/arm64/kvm/hyp/vhe/cfi.c | 37 ++++++++
> arch/arm64/kvm/hyp/vhe/switch.c | 7 ++
> arch/arm64/kvm/hyp_cfi_test.c | 43 +++++++++
> arch/arm64/kvm/hyp_cfi_test_module.c | 133 +++++++++++++++++++++++++++
> 9 files changed, 329 insertions(+)
> create mode 100644 arch/arm64/include/asm/kvm_cfi.h
> create mode 100644 arch/arm64/kvm/hyp/include/hyp/cfi.h
> create mode 100644 arch/arm64/kvm/hyp/vhe/cfi.c
> create mode 100644 arch/arm64/kvm/hyp_cfi_test.c
> create mode 100644 arch/arm64/kvm/hyp_cfi_test_module.c
>
> diff --git a/arch/arm64/include/asm/kvm_cfi.h b/arch/arm64/include/asm/kvm_cfi.h
> new file mode 100644
> index 000000000000..13cc7b19d838
> --- /dev/null
> +++ b/arch/arm64/include/asm/kvm_cfi.h
> @@ -0,0 +1,36 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * Copyright (C) 2024 - Google Inc
> + * Author: Pierre-Clément Tosi <ptosi@google.com>
> + */
> +
> +#ifndef __ARM64_KVM_CFI_H__
> +#define __ARM64_KVM_CFI_H__
> +
> +#include <asm/kvm_asm.h>
> +#include <linux/errno.h>
> +
> +#ifdef CONFIG_HYP_SUPPORTS_CFI_TEST
> +
> +int kvm_cfi_test_register_host_ctxt_cb(void (*cb)(void));
> +int kvm_cfi_test_register_guest_ctxt_cb(void (*cb)(void));
Hmm, I tend to think this indirection is a little over the top for a test
module that only registers a small handful of handlers:
> +static int set_param_mode(const char *val, const struct kernel_param *kp,
> + int (*register_cb)(void (*)(void)))
> +{
> + unsigned int *mode = kp->arg;
> + int err;
> +
> + err = param_set_uint(val, kp);
> + if (err)
> + return err;
> +
> + switch (*mode) {
> + case 0:
> + return register_cb(NULL);
> + case 1:
> + return register_cb(hyp_trigger_builtin_cfi_fault);
> + case 2:
> + return register_cb((void *)hyp_cfi_builtin2module_test_target);
> + case 3:
> + return register_cb(trigger_module2builtin_cfi_fault);
> + case 4:
> + return register_cb(trigger_module2module_cfi_fault);
> + default:
> + return -EINVAL;
> + }
> +}
Why not just have a hyp selftest that runs through all of this behind a
static key? I think it would simplify the code quite a bit, and you could
move the registration and indirection logic.
Will
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2024-05-13 17:21 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-10 11:26 [PATCH v3 00/12] KVM: arm64: Add support for hypervisor kCFI Pierre-Clément Tosi
2024-05-10 11:26 ` [PATCH v3 01/12] KVM: arm64: Fix clobbered ELR in sync abort/SError Pierre-Clément Tosi
2024-05-13 13:55 ` Will Deacon
2024-05-10 11:26 ` [PATCH v3 02/12] KVM: arm64: Fix __pkvm_init_switch_pgd C signature Pierre-Clément Tosi
2024-05-13 14:03 ` Will Deacon
2024-05-10 11:26 ` [PATCH v3 03/12] KVM: arm64: Pass pointer to __pkvm_init_switch_pgd Pierre-Clément Tosi
2024-05-13 14:17 ` Will Deacon
2024-05-10 11:26 ` [PATCH v3 04/12] KVM: arm64: nVHE: Remove __guest_exit_panic path Pierre-Clément Tosi
2024-05-13 14:27 ` Will Deacon
2024-05-10 11:26 ` [PATCH v3 05/12] KVM: arm64: nVHE: Add EL2h sync exception handler Pierre-Clément Tosi
2024-05-10 11:26 ` [PATCH v3 06/12] KVM: arm64: nVHE: gen-hyprel: Skip R_AARCH64_ABS32 Pierre-Clément Tosi
2024-05-13 14:33 ` Will Deacon
2024-05-10 11:26 ` [PATCH v3 07/12] KVM: arm64: VHE: Mark __hyp_call_panic __noreturn Pierre-Clément Tosi
2024-05-13 14:52 ` Will Deacon
2024-05-10 11:26 ` [PATCH v3 08/12] arm64: Move esr_comment() to <asm/esr.h> Pierre-Clément Tosi
2024-05-13 14:57 ` Will Deacon
2024-05-10 11:26 ` [PATCH v3 09/12] KVM: arm64: VHE: Add test module for hyp kCFI Pierre-Clément Tosi
2024-05-13 17:21 ` Will Deacon [this message]
2024-05-29 12:26 ` Pierre-Clément Tosi
2024-06-03 13:10 ` Will Deacon
2024-05-10 11:26 ` [PATCH v3 10/12] KVM: arm64: nVHE: Support CONFIG_CFI_CLANG at EL2 Pierre-Clément Tosi
2024-05-13 17:30 ` Will Deacon
2024-05-29 12:30 ` Pierre-Clément Tosi
2024-06-03 13:09 ` Will Deacon
2024-05-10 11:26 ` [PATCH v3 11/12] KVM: arm64: nVHE: Support test module for hyp kCFI Pierre-Clément Tosi
2024-05-10 11:26 ` [PATCH v3 12/12] KVM: arm64: Improve CONFIG_CFI_CLANG error message Pierre-Clément Tosi
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=20240513172120.GA29051@willie-the-truck \
--to=will@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=ptosi@google.com \
--cc=suzuki.poulose@arm.com \
--cc=vdonnefort@google.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