Introduce LVBS support for Hyper-V guests
Subsystems (4)
cover Introduce LVBS support for Hyper-V guests
Introduction
=============
Microsoft Hyper-V implements Virtualization-Based Security (VBS) through Virtual Secure Mode (VSM), which uses Virtual Trust Levels (VTLs) to establish isolated execution environments enforced by the hypervisor. The primary operating system executes in VTL0, while security-sensitive components can execute in higher trust levels such as VTL1.
Linux Virtualization-Based Security (LVBS) extends this model to Linux Hyper-V guests by introducing a secure kernel running in VTL1 alongside the primary Linux kernel running in VTL0. The hypervisor enforces isolation between the two trust levels, allowing security-sensitive functionality to execute independently of the primary operating system.
This RFC series introduces the foundational infrastructure required to support LVBS and boot a secure kernel in VTL1 from VTL0. Before any LVBS service can exist, VTL0 has to stand VTL1 up, load the secure kernel and boot all CPUs in VTL1.
This series intentionally focuses on establishing the VTL1 execution environment and does not yet introduce higher-level LVBS services. Future RFCs will build on this foundation by adding support for VTL0 to request secure services from VTL1, attestation support, memory protection services, and other security capabilities.
The goal of this RFC is to gather feedback on the overall LVBS architecture, VTL1 boot model, processor bring-up sequence, and Linux kernel integration before additional functionality is introduced.
Threat Model
=============
The security of the whole construction rests on the early boot path being trusted. On a VSM-capable Hyper-V guest, the VTL0 kernel is launched via Secure Boot, so the kernel image that enables VTL1 and loads the secure kernel is itself measured and signature-checked before it runs. Enabling VTL1 and populating it from VTL0 during early boot is therefore no weaker than Secure Boot itself: an attacker who can subvert this stage can already subvert the kernel before VTL1 exists.
For the same reason, a Unified Kernel Image (UKI) is the preferred delivery vehicle for LVBS. A UKI bundles the kernel, initrd, cmdline, and (optionally) devicetree into a single PE binary that is signed and verified as one unit by Secure Boot. This extends the signature-verified boundary to cover the initrd, which is where the secure kernel image is staged before VTL0 hands it to VTL1. Without a UKI (or an equivalent measured-initrd scheme), an unsigned initrd would be an obvious weak spot in the chain.
The series is based on hyperv-next (tag hyperv-next-signed-20260826) and boot-tested on an x86_64 Hyper-V VTL0 guest with litebox [1] as the VTL1 secure kernel.
[1] https://github.com/microsoft/litebox
Comments and feedback are greatly appreciated.
Thara Gopinath (12):
drivers: hv: Add HYPERV_VSM kconfig option
drivers: hv: hv_common: Allocate Hyper-V output arg page when VSM is
enabled
drivers: hv: Reserve memory for VSM secure kernel during early boot
firmware: efi: libstub: x86-stub: Enable VSM awareness in efi os
indications variable
include: hyperv: hvgdk_mini.h: Add VTL-specific structures and bits
drivers: hv: Add VSM boot driver and enable VTL1 at the partition
level
drivers: hv: hv_vsm_boot: load secure kernel image from firmware
arch: x86: hyperv: Build initial vCPU context for VTL1 secure kernel
drivers: hv: hv_vsm_boot: Enable VTL1 on the boot processor
arch: x86: hyperv: hv_vtl_vsm: Introduce vtlcall
drivers: hv: hv_vsm_boot: Boot primary processor in VTL1
drivers: hv: hv_vsm_boot: Boot secondary processors in VTL1
arch/x86/hyperv/Makefile | 1 + arch/x86/hyperv/hv_vtl_vsm.c | 287 +++++++++++ arch/x86/hyperv/mshv-asm-offsets.c | 8 + arch/x86/hyperv/mshv_vtl_asm.S | 75 +++ arch/x86/include/asm/mshyperv.h | 9 + drivers/firmware/efi/libstub/x86-stub.c | 57 +++ drivers/hv/Kconfig | 9 + drivers/hv/Makefile | 3 +- drivers/hv/hv_common.c | 3 +- drivers/hv/hv_vsm.h | 19 + drivers/hv/hv_vsm_boot.c | 630 ++++++++++++++++++++++++ drivers/hv/hv_vsm_securekernel.c | 188 +++++++ include/hyperv/hvgdk_mini.h | 46 ++ include/hyperv/vsm.h | 43 ++ 14 files changed, 1376 insertions(+), 2 deletions(-) create mode 100644 arch/x86/hyperv/hv_vtl_vsm.c create mode 100644 drivers/hv/hv_vsm.h create mode 100644 drivers/hv/hv_vsm_boot.c create mode 100644 drivers/hv/hv_vsm_securekernel.c create mode 100644 include/hyperv/vsm.h
base-commit: be0cfab740e58b70047ef6e7e3d578f00ed5d258
Signature · 1 line
2.34.1
RFC PATCH 1/12 drivers: hv: Add HYPERV_VSM kconfig option
Introduce CONFIG_HYPERV_VSM, the top-level switch that gates VTL0-side support for Microsoft Hyper-V's Virtual Secure Mode (VSM). Enabling it lets the VTL0 kernel load a small secure kernel into VTL1 and stand up the VTL0<->VTL1 interfaces needed to request VSM services.
Depends on HYPERV and on X86_64 which is the only architecture currently implemented.
--- drivers/hv/Kconfig | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/hv/Kconfig +9 -0
@@ -40,6 +40,15 @@ config HYPERV_VTL_MODE If unsure, say N +config HYPERV_VSM + bool "Microsoft Hyper-V VSM driver" + depends on HYPERV && X86_64 + help + Select this option to enable Hyper-V Virtual Secure Mode. + Enabling this option will load a secure kernel in VTL1 and + establish an interface between VTL0 and VTL1 to request for + VSM services. + config HYPERV_TIMER def_bool HYPERV && X86
Signature · 1 line
2.34.1
RFC PATCH 2/12 drivers: hv: hv_common: Allocate Hyper-V output arg page when VSM is enabled
In reply to Thara Gopinathhv_output_page_exists() controls whether the per-CPU Hyper-V output argument page is allocated at boot. This page is required for hypercalls that return results via an output GPA, like HvCallGetVpRegisters.
The VSM drivers read VSM-specific VP registers (e.g. HvRegisterVsmVpStatus, HvRegisterVsmCapabilities) via get_vp_registers(), which issues HvCallGetVpRegisters and therefore depends on the output argument page being present. Without it, those hypercalls fail and VSM initialization cannot proceed.
Extend hv_output_page_exists() to return true when CONFIG_HYPERV_VSM is enabled.
--- drivers/hv/hv_common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hv/hv_common.c +2 -1
@@ -259,7 +259,8 @@ static void hv_kmsg_dump_register(void)static inline bool hv_output_page_exists (void ) { -return hv_parent_partition () ||IS_ENABLED (CONFIG_HYPERV_VTL_MODE ); +return hv_parent_partition () ||IS_ENABLED (CONFIG_HYPERV_VTL_MODE ) || +IS_ENABLED (CONFIG_HYPERV_VSM ); }void __init hv_get_partition_id (void )
Signature · 1 line
2.34.1
RFC PATCH 3/12 drivers: hv: Reserve memory for VSM secure kernel during early boot
In reply to Thara GopinathThe VSM secure kernel runs in VTL1 and cannot use the normal Linux kernel memory allocators — its memory must be reserved before the general-purpose allocator takes ownership of the physical address space.
Add hv_vsm_securekernel.c to handle this early boot reservation. The driver parses a new "securekernel=<size>[K|M|G][@<addr>]" kernel command line argument to allow explicit control over the reserved region's size and base address. If the argument is absent or specifies a region smaller than the computed minimum, the driver falls back to an automatic allocation.
The minimum reservation is calculated as:
16 MB (base) + (num_possible_cpus * 4 MB per-CPU)
Memory is reserved via memblock_phys_alloc_range() with 2 MB alignment (SECKERNEL_ALIGN) and registered as an IORESOURCE_SYSTEM_RAM entry named "vsm" in the iomem resource tree so it is visible in /proc/iomem and protected from reuse.
The reservation is skipped if sk_res.start is already set, preventing duplicate reservations if the setup hook is called more than once.
--- drivers/hv/Makefile | 1 + drivers/hv/hv_vsm_securekernel.c | 188 +++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 drivers/hv/hv_vsm_securekernel.c
drivers/hv/Makefile +1 -0
@@ -25,3 +25,4 @@ obj-$(subst m,y,$(CONFIG_MSHV_ROOT)) += hv_proc.o ifneq ($(CONFIG_MSHV_ROOT)$(CONFIG_MSHV_VTL),) obj-y += mshv_common.o endif +obj-$(subst m,y,$(CONFIG_HYPERV_VSM)) += hv_vsm_securekernel.o
drivers/hv/hv_vsm_securekernel.c new file +188 -0
188 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 4/12 firmware: efi: libstub: x86-stub: Enable VSM awareness in efi os indications variable
In reply to Thara GopinathSet bit 0 of the Hyper-V private OsLoaderIndications EFI variable during exit_boot() so the bootloader/firmware knows the OS intends to enable VTL1. Without this, VTL1 cannot be brought up from the Linux kernel.
The support bit is first checked in OsLoaderIndicationsSupported, and the variable is only written when the VSM bit is not already set.
--- drivers/firmware/efi/libstub/x86-stub.c | 57 +++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
drivers/firmware/efi/libstub/x86-stub.c +57 -0
@@ -21,6 +21,17 @@#include "efistub.h" #include "x86-stub.h" +#ifdef CONFIG_HYPERV_VSM +#define HYPERV_PRIVATE_EFI_NAMESPACE_GUID \ + EFI_GUID(0x610b9e98, 0xc6f6, 0x47f8, 0x8b, 0x47, 0x2d, 0x2d, 0xa0, 0xd5, 0x2a, 0x91) + +static const efi_char16_t efi_HvPrivOsloaderIndications_name[] =L"OsLoaderIndications" ; +static const efi_char16_t efi_HvPrivOsloaderIndicationsSupported_name[] = +L"OsLoaderIndicationsSupported" ; +#define HV_OSLOADER_INDICATION_VSM BIT(0) + +#endif +extern char _bss[], _ebss[];const efi_system_table_t *efi_system_table;
@@ -754,6 +765,47 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map,return EFI_SUCCESS ; } +#ifdef CONFIG_HYPERV_VSM +static void efi_set_hv_os_indications (void ) +{ +efi_guid_t guid =HYPERV_PRIVATE_EFI_NAMESPACE_GUID ; +efi_status_t status; +unsigned long size; +u32 attr, val; + + size =sizeof (val); + status =get_efi_var (efi_HvPrivOsloaderIndicationsSupported_name, + &guid, &attr, &size, &val); +if (status !=EFI_SUCCESS ) { +efi_err ("Could not read Hyper-V OsloaderIndicationsSupported\n" ); +return ; + } + +if (!(val &HV_OSLOADER_INDICATION_VSM )) { +efi_info ("Hyper-V does not support VSM in OsloaderIndicationsSupported\n" ); +return ; + } + + size =sizeof (val); + status =get_efi_var (efi_HvPrivOsloaderIndications_name, &guid, &attr, &size, &val); +if (status !=EFI_SUCCESS ) { +efi_err ("Could not read Hyper-V OsLoaderIndications\n" ); +return ; + } + +if (val &HV_OSLOADER_INDICATION_VSM ) { +efi_info ("VSM is already supported in OsLoaderIndications." ); +return ; + } + + val |=HV_OSLOADER_INDICATION_VSM ; + size =sizeof (val); + status =set_efi_var (efi_HvPrivOsloaderIndications_name, &guid, attr, size, &val); +if (status !=EFI_SUCCESS ) +efi_err ("Could not set Hyper-V OsLoaderIndications to indicate VSM support\n" ); +} +#endif +static efi_status_t exit_boot (struct boot_params *boot_params,void *handle) {struct setup_data *e820ext =NULL ;
@@ -768,6 +820,11 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)if (status !=EFI_SUCCESS )return status; +#ifdef CONFIG_HYPERV_VSM +/* Indicate to bootloader that we will be enabling VTL1 before exiting boot services */ +efi_set_hv_os_indications (); +#endif +/* Might as well exit boot services now */ status =efi_exit_boot_services (handle, &priv, exit_boot_func);if (status !=EFI_SUCCESS )
Signature · 1 line
2.34.1
RFC PATCH 5/12 include: hyperv: hvgdk_mini.h: Add VTL-specific structures and bits
In reply to Thara GopinathAdd the definitions needed by the upcoming VSM / VTL1 bring-up code:
- HV_ACCESS_VSM and HV_ACCESS_VP_REGS partition privilege bits
- HVCALL_ENABLE_PARTITION_VTL and HVCALL_MODIFY_VTL_PROTECTION_MASK hypercall IDs
- hv_input_enable_partition_vtl input structure and its flags union
- hv_register_vsm_{partition_status,vp_status} register layout unions
- HV_REGISTER_VSM_PARTITION_STATUS and HV_REGISTER_VSM_VP_SECURE_CONFIG_VTL0 register names
Definitions taken from the Hyper-V TLFS.
--- include/hyperv/hvgdk_mini.h | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
include/hyperv/hvgdk_mini.h +46 -0
@@ -303,6 +303,8 @@ union hv_hypervisor_version_info {#define HV_ACCESS_STATS BIT(8)#define HV_DEBUGGING BIT(11)#define HV_CPU_MANAGEMENT BIT(12) +#define HV_ACCESS_VSM BIT(16) +#define HV_ACCESS_VP_REGS BIT(17)#define HV_ENABLE_EXTENDED_HYPERCALLS BIT(20)#define HV_ISOLATION BIT(22)
@@ -438,6 +440,8 @@ union hv_vp_assist_msr_contents { /* HV_REGISTER_VP_ASSIST_PAGE */#define HVCALL_GET_LOGICAL_PROCESSOR_RUN_TIME 0x0004#define HVCALL_NOTIFY_LONG_SPIN_WAIT 0x0008#define HVCALL_SEND_IPI 0x000b +#define HVCALL_MODIFY_VTL_PROTECTION_MASK 0x000c +#define HVCALL_ENABLE_PARTITION_VTL 0x000d#define HVCALL_ENABLE_VP_VTL 0x000f#define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX 0x0013#define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX 0x0014
@@ -878,6 +882,46 @@ struct hv_init_vp_context {u64 msr_cr_pat; } __packed; +union hv_enable_partition_vtl_flags { +u8 as_uint8; +struct { +u8 enable_mbec:1 ; +u8 enable_supervisor_shadow_stack:1 ; +u8 enable_hardware_hvpt:1 ; +u8 reserved:5 ; + }; +} __packed; + +struct hv_input_enable_partition_vtl { +u64 partition_id; +union hv_input_vtl target_vtl; +union hv_enable_partition_vtl_flags flags; +u16 rsvd_z16; +u32 rsvd_z32; +} __packed; + +union hv_register_vsm_partition_status { +u64 as_uint64; +struct { +u64 enabled_vtl_set :16 ; +u64 max_vtl :4 ; +u64 mbec_enabled_vtl_set:16 ; +u64 supervisor_shadow_stack_enabled_vtl_set :4 ; +u64 reserved :24 ; + }; +} __packed; + +union hv_register_vsm_vp_status { +u64 as_uint64; +struct { +u64 active_vtl :4 ; +u64 active_mbec_enabled :1 ; +u64 reserved_z0 :11 ; +u64 enabled_vtl_set :16 ; +u64 reserved_z1 :32 ; + }; +} __packed; +struct hv_enable_vp_vtl {u64 partition_id;u32 vp_index;
@@ -1061,8 +1105,10 @@ enum hv_register_name {/* Synthetic VSM registers */ HV_REGISTER_VSM_CODE_PAGE_OFFSETS =0x000D0002 , +HV_REGISTER_VSM_PARTITION_STATUS =0x000D0004 ,HV_REGISTER_VSM_CAPABILITIES =0x000D0006 ,HV_REGISTER_VSM_PARTITION_CONFIG =0x000D0007 , +HV_REGISTER_VSM_VP_SECURE_CONFIG_VTL0 =0x000D0010 , #if defined(CONFIG_X86)/* X64 Debug Registers */
Signature · 1 line
2.34.1
RFC PATCH 6/12 drivers: hv: Add VSM boot driver and enable VTL1 at the partition level
In reply to Thara GopinathLinux VBS (LVBS) uses Hyper-V's Virtual Secure Mode to run a small trusted kernel in VTL1 alongside the regular VTL0 kernel, so that security-sensitive state (e.g. hypervisor-enforced code integrity, credential isolation) can live behind a higher-privilege boundary that VTL0 compromise cannot cross. Bringing that up from Linux requires the VTL0 kernel to drive the VSM setup itself.
Add drivers/hv/hv_vsm_boot.c as the entry point for that sequence. This first step handles partition-level VTL1 enable only:
- Probe VSM / VP-register privileges and SynIC availability before doing anything.
- Pin init to the VTL0 boot CPU so VTL1 comes up on the same CPU (later patches rely on this).
- Read HV_REGISTER_VSM_PARTITION_STATUS, and if VTL1 is not already enabled, issue HVCALL_ENABLE_PARTITION_VTL with MBEC and confirm by re-reading the register.
--- drivers/hv/Makefile | 4 +- drivers/hv/hv_vsm_boot.c | 178 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 drivers/hv/hv_vsm_boot.c
drivers/hv/Makefile +2 -2
@@ -22,7 +22,7 @@ mshv_vtl-y := mshv_vtl_main.o # Code that must be built-in obj-$(CONFIG_HYPERV) += hv_common.o obj-$(subst m,y,$(CONFIG_MSHV_ROOT)) += hv_proc.o -ifneq ($(CONFIG_MSHV_ROOT)$(CONFIG_MSHV_VTL),) +ifneq ($(CONFIG_MSHV_ROOT)$(CONFIG_MSHV_VTL)$(CONFIG_HYPERV_VSM),) obj-y += mshv_common.o endif -obj-$(subst m,y,$(CONFIG_HYPERV_VSM)) += hv_vsm_securekernel.o +obj-$(subst m,y,$(CONFIG_HYPERV_VSM)) += hv_vsm_securekernel.o hv_vsm_boot.o
drivers/hv/hv_vsm_boot.c new file +178 -0
178 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 7/12 drivers: hv: hv_vsm_boot: load secure kernel image from firmware
In reply to Thara GopinathLVBS bring-up requires loading a secure kernel image into VTL1 before starting it. Add the VTL0-side loader that stages the image in the memory region reserved by hv_vsm_securekernel, in preparation for the VTL1 bring-up.
The image is a 64-bit ELF fetched via request_firmware("vsm_sk"). It is expected to ship inside the signed UKI/initramfs so it is authenticated end-to-end via Secure Boot before the loader consumes it; sourcing it from an unauthenticated location would break the LVBS trust model.
The loader validates the ELF header, stages the PT_LOAD segments into the reserved region and records the entry point as a physical address for use at VTL1 start time.
If VSM support has been advertised to the hypervisor but no secure kernel region was reserved on the command line, panic: LVBS bring-up is committed at this point and there is no safe way to continue.
--- drivers/hv/hv_vsm.h | 17 ++++ drivers/hv/hv_vsm_boot.c | 201 ++++++++++++++++++++++++++++++++++++++- include/hyperv/vsm.h | 21 ++++ 3 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 drivers/hv/hv_vsm.h create mode 100644 include/hyperv/vsm.h
drivers/hv/hv_vsm.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2023-2026, Microsoft Corporation. +* +* Author: Thara Gopinath <[email protected]> +* +*/ + +#ifndef _HV_VSM_H +#define _HV_VSM_H + +#include <linux/ioport.h> +#include <linux/types.h> + +extern struct resource sk_res; + +#endif /* _HV_VSM_H */
drivers/hv/hv_vsm_boot.c +200 -1
241 lines not on this page · raw message
include/hyperv/vsm.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Common definitions shared by generic and arch code for enabling VTL1 +* and the Virtual Secure Mode (VSM) framework on Microsoft Hyper-V. +* +* Copyright (c) 2025-2026, Microsoft Corporation. +* +* Author: Thara Gopinath <[email protected]> +*/ + +#ifndef _HYPERV_VSM_H +#define _HYPERV_VSM_H + +/* +* Size of memory that is initially mapped for the secure kernel by the +* VTL0-side loader. The secure kernel image itself may be larger than +* this and map additional memory on its own. +*/ +#define VSM_SK_INITIAL_MAP_SIZE (16 * 1024 * 1024) + +#endif /* _HYPERV_VSM_H */
Signature · 1 line
2.34.1
RFC PATCH 8/12 arch: x86: hyperv: Build initial vCPU context for VTL1 secure kernel
In reply to Thara GopinathEnabling VTL1 on the boot processor requires handing Hyper-V a fully populated hv_init_vp_context describing the state VTL1 should start in:
initial page tables, GDT/TSS, control registers and entry point. Add the arch-specific builder that assembles this context using the memory region reserved for the secure kernel.
Actual enablement of VTL1 using this context is done in a subsequent patch.
--- arch/x86/hyperv/Makefile | 1 + arch/x86/hyperv/hv_vtl_vsm.c | 258 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 9 ++ 3 files changed, 268 insertions(+) create mode 100644 arch/x86/hyperv/hv_vtl_vsm.c
arch/x86/hyperv/Makefile +1 -0
@@ -2,6 +2,7 @@ obj-y := hv_init.o mmu.o nested.o irqdomain.o ivm.o obj-$(CONFIG_X86_64) += hv_apic.o obj-$(CONFIG_HYPERV_VTL_MODE) += hv_vtl.o mshv_vtl_asm.o +obj-$(CONFIG_HYPERV_VSM) += hv_vtl_vsm.o $(obj)/mshv_vtl_asm.o: $(obj)/mshv-asm-offsets.h
arch/x86/hyperv/hv_vtl_vsm.c new file +258 -0
258 lines not on this page · raw message
arch/x86/include/asm/mshyperv.h +9 -0
@@ -7,6 +7,7 @@#include <linux/msi.h> #include <linux/io.h> #include <linux/static_call.h> +#include <linux/elf.h> #include <asm/nospec-branch.h> #include <asm/msr.h> #include <hyperv/hvhdk.h>
@@ -248,6 +249,14 @@ void hv_crash_asm_end(void);static inline void hv_root_crash_init (void ) {}#endif /* CONFIG_MSHV_ROOT && CONFIG_CRASH_DUMP */ +#ifdef CONFIG_HYPERV_VSM +void __init hv_vsm_arch_init_vp (struct hv_init_vp_context *vp_ctx,Elf64_Addr sk_entry_pa, +phys_addr_t sk_pa); +#else /* CONFIG_HYPERV_VSM */ +static inline void __init hv_vsm_arch_init_vp (struct hv_init_vp_context *vp_ctx, +Elf64_Addr sk_entry_pa,phys_addr_t sk_pa) {} +#endif +#else /* CONFIG_HYPERV */ static inline void hyperv_init (void ) {}static inline void hyperv_setup_mmu_ops (void ) {}
Signature · 1 line
2.34.1
RFC PATCH 9/12 drivers: hv: hv_vsm_boot: Enable VTL1 on the boot processor
In reply to Thara GopinathWith the partition-level VTL1 enabled and the secure kernel image loaded into the reserved region, the next step is to enable VTL1 on the boot virtual processor so that the hypervisor can dispatch it into the secure kernel entry point.
Introduce two helpers in hv_vsm_boot.c:
- hv_vsm_get_vp_status() reads HV_REGISTER_VSM_VP_STATUS.
- hv_vsm_enable_vp_vtl() issues HVCALL_ENABLE_VP_VTL with an initial vCPU context built via hv_vsm_arch_init_vp().
Extend hv_vsm_bootstrap_vtl() to enable VTL1 on the boot VP if it is not already enabled, and re-query the status to confirm the transition.
--- drivers/hv/hv_vsm_boot.c | 73 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-)
drivers/hv/hv_vsm_boot.c +71 -2
@@ -216,6 +216,47 @@ static int __init hv_vsm_load_secure_kernel(Elf64_Addr *sk_entry_pa)return ret; } +static int __init hv_vsm_enable_vp_vtl (Elf64_Addr sk_entry_pa) +{ +u64 status =0 ; +unsigned long flags; +struct hv_enable_vp_vtl *hvin; + +local_irq_save (flags); + + hvin = *this_cpu_ptr (hyperv_pcpu_input_arg); +memset (hvin,0 ,sizeof (*hvin)); + + hvin->partition_id =HV_PARTITION_ID_SELF ; + hvin->vp_index =HV_VP_INDEX_SELF ; + hvin->target_vtl.target_vtl =HV_VTL_SECURE ; + +hv_vsm_arch_init_vp (&hvin->vp_context, sk_entry_pa, sk_res.start); + + status =hv_do_hypercall (HVCALL_ENABLE_VP_VTL , hvin,NULL ); + +local_irq_restore (flags); + +return hv_result (status); +} + +static int __init hv_vsm_get_vp_status (u16 *enabled_vtl_set,u8 *active_mbec_enabled) +{ +u64 result; +int ret; +union hv_register_vsm_vp_status vsm_vp_status = {0 }; + + ret =hv_vsm_get_register (HV_REGISTER_VSM_VP_STATUS , &result); +if (ret) +return ret; + + vsm_vp_status = (union hv_register_vsm_vp_status )result; + *enabled_vtl_set = vsm_vp_status.enabled_vtl_set; + *active_mbec_enabled = vsm_vp_status.active_mbec_enabled; + +return 0 ; +} +static int __init hv_vsm_enable_partition_vtl (void ) {u64 status =0 ;
@@ -262,7 +303,8 @@ static int __init hv_vsm_get_partition_status(u16 *enabled_vtl_set, u8 *max_vtl,static int __init hv_vsm_bootstrap_vtl (void ) {u16 partition_enabled_vtl_set =0 , partition_mbec_enabled_vtl_set =0 ; -u8 partition_max_vtl; +u16 vp_enabled_vtl_set =0 ; +u8 partition_max_vtl, active_mbec_enabled =0 ;Elf64_Addr sk_entry_pa;int ret;
@@ -300,7 +342,34 @@ static int __init hv_vsm_bootstrap_vtl(void) } } -return hv_vsm_load_secure_kernel (&sk_entry_pa); + ret =hv_vsm_load_secure_kernel (&sk_entry_pa); +if (ret) +return ret; + +/* Check and enable VTL1 for the primary virtual processor */ + ret =hv_vsm_get_vp_status (&vp_enabled_vtl_set, &active_mbec_enabled); +if (ret) +return ret; + +if (vp_enabled_vtl_set &HV_VTL1_ENABLE_BIT ) { +pr_info ("VP VTL1 is already enabled\n" ); + }else { + ret =hv_vsm_enable_vp_vtl (sk_entry_pa); +if (ret) { +pr_err ("Enabling VP VTL1 failed with status 0x%x\n" , ret); +/* TODO: Should we disable VTL1 at partition level in this case? */ +return -EINVAL ; + } + ret =hv_vsm_get_vp_status (&vp_enabled_vtl_set, &active_mbec_enabled); +if (ret) +return ret; + +if (!(vp_enabled_vtl_set &HV_VTL1_ENABLE_BIT )) { +pr_err ("Tried Enabling VP VTL1 and still failed\n" ); +return -EINVAL ; + } + } +return 0 ; }static void __init hv_vsm_get_sk_mem (void )
Signature · 1 line
2.34.1
RFC PATCH 10/12 arch: x86: hyperv: hv_vtl_vsm: Introduce vtlcall
In reply to Thara GopinathA vtlcall is the mechanism by which a lower VTL (VTL0) transitions into a higher VTL (VTL1) on Microsoft Hyper-V. It is issued as a call to a fixed offset within the hypercall page; the hypervisor saves the lower-VTL context, restores the upper-VTL context, and resumes execution at the upper VTL. Control returns to VTL0 when the upper VTL executes a vtlreturn.
Introduce the x86 VTL0-side vtlcall infrastructure:
- hv_vsm_vtlcall(): runtime entry point. Preserves state VTL1 can clobber (IRQs, FPU, CR2) and invokes the assembly trampoline. Returns the signed 64-bit status the secure kernel places in a3.
- hv_vsm_init_vtlcall(u64 vtl_call_offset): one-shot initializer that installs the vtlcall target into a static_call. The offset is passed in by the caller so the register read stays outside arch code, mirroring mshv_vtl_return_call_init().
- struct hv_vtlcall_param and CONFIG_HYPERV_VSM API in include/hyperv/vsm.h, with no-op stubs when VSM is disabled.
- __hv_vsm_vtlcall assembly trampoline in mshv_vtl_asm.S, plus asm-offsets for the argument block.
--- arch/x86/hyperv/Makefile | 2 +- arch/x86/hyperv/hv_vtl_vsm.c | 29 ++++++++++++ arch/x86/hyperv/mshv-asm-offsets.c | 8 ++++ arch/x86/hyperv/mshv_vtl_asm.S | 75 ++++++++++++++++++++++++++++++ include/hyperv/vsm.h | 22 +++++++++ 5 files changed, 135 insertions(+), 1 deletion(-)
arch/x86/hyperv/Makefile +1 -1
@@ -2,7 +2,7 @@ obj-y := hv_init.o mmu.o nested.o irqdomain.o ivm.o obj-$(CONFIG_X86_64) += hv_apic.o obj-$(CONFIG_HYPERV_VTL_MODE) += hv_vtl.o mshv_vtl_asm.o -obj-$(CONFIG_HYPERV_VSM) += hv_vtl_vsm.o +obj-$(CONFIG_HYPERV_VSM) += hv_vtl_vsm.o mshv_vtl_asm.o $(obj)/mshv_vtl_asm.o: $(obj)/mshv-asm-offsets.h
arch/x86/hyperv/hv_vtl_vsm.c +29 -0
@@ -15,6 +15,7 @@#include <hyperv/vsm.h> #include <asm/msr-index.h> #include <asm/processor-flags.h> +#include <hyperv/hvgdk_mini.h> #include <asm/mshyperv.h> /* Define PAGE size and related variables for initial secure kernel pages */
@@ -256,3 +257,31 @@ void __init hv_vsm_arch_init_vp(struct hv_init_vp_context *vp_ctx, Elf64_Addr skhv_vsm_init_gdt (vp_ctx, sk_pa);hv_vsm_init_page_tables (vp_ctx, sk_pa); } + +/* Implemented in mshv_vtl_asm.S */ +void __hv_vsm_vtlcall (struct hv_vtlcall_param *args); + +DEFINE_STATIC_CALL_NULL(__hv_vsm_vtlcall_hypercall, void (*)(void)); + +void __init hv_vsm_init_vtlcall (u64 vtl_call_offset) +{ +static_call_update (__hv_vsm_vtlcall_hypercall, + (void *)((u8 *)hv_hypercall_pg + vtl_call_offset)); +} + +s64 hv_vsm_vtlcall (struct hv_vtlcall_param *args) +{ +unsigned long flags; +u64 cr2; + +local_irq_save (flags); +kernel_fpu_begin_mask (0 ); + cr2 =native_read_cr2 (); +__hv_vsm_vtlcall (args); +native_write_cr2 (cr2); +kernel_fpu_end (); +local_irq_restore (flags); + +/* The secure kernel returns a signed 64-bit status in a3. */ +return (s64 )args->a3; +}
arch/x86/hyperv/mshv-asm-offsets.c +8 -0
@@ -12,6 +12,7 @@#define COMPILE_OFFSETS #include <linux/kbuild.h> +#include <hyperv/vsm.h> #include <asm/mshyperv.h> static void __used common (void )
@@ -34,4 +35,11 @@ static void __used common(void)OFFSET (MSHV_VTL_CPU_CONTEXT_r15, mshv_vtl_cpu_context, r15);OFFSET (MSHV_VTL_CPU_CONTEXT_cr2, mshv_vtl_cpu_context, cr2); } + +if (IS_ENABLED (CONFIG_HYPERV_VSM )) { +OFFSET (HV_VTLCALL_PARAM_a0, hv_vtlcall_param, a0); +OFFSET (HV_VTLCALL_PARAM_a1, hv_vtlcall_param, a1); +OFFSET (HV_VTLCALL_PARAM_a2, hv_vtlcall_param, a2); +OFFSET (HV_VTLCALL_PARAM_a3, hv_vtlcall_param, a3); + } }
arch/x86/hyperv/mshv_vtl_asm.S +75 -0
84 lines not on this page · raw message
include/hyperv/vsm.h +22 -0
@@ -11,6 +11,8 @@#ifndef _HYPERV_VSM_H#define _HYPERV_VSM_H +#include <linux/types.h> +/* * Size of memory that is initially mapped for the secure kernel by the * VTL0-side loader. The secure kernel image itself may be larger than
@@ -18,4 +20,24 @@*/ #define VSM_SK_INITIAL_MAP_SIZE (16 * 1024 * 1024) +/* +* Argument block passed from VTL0 to VTL1 across a vtlcall. Layout is +* shared with the arch-specific assembly trampoline that marshals these +* into registers. +*/ +struct hv_vtlcall_param { +u64 a0; +u64 a1; +u64 a2; +u64 a3; +} __packed; + +#ifdef CONFIG_HYPERV_VSM +s64 hv_vsm_vtlcall (struct hv_vtlcall_param *args); +void hv_vsm_init_vtlcall (u64 vtl_call_offset); +#else +static inline s64 hv_vsm_vtlcall (struct hv_vtlcall_param *args) {return 0 ; } +static inline void hv_vsm_init_vtlcall (u64 vtl_call_offset) {} +#endif +#endif /* _HYPERV_VSM_H */
Signature · 1 line
2.34.1
RFC PATCH 11/12 drivers: hv: hv_vsm_boot: Boot primary processor in VTL1
In reply to Thara GopinathWith partition-level VTL1 enabled, the secure kernel image loaded, its initial vCPU context built, and VTL1 enabled on the boot CPU, the final step of the primary-CPU bring-up is to actually transition into VTL1 and start executing the secure kernel. Hyper-V restores the CPU state from the vp_context supplied during VTL1 enablement when the first vtlcall is issued.
Issue that first vtlcall on the primary CPU, passing the number of possible CPUs and the location of the secure kernel reserved region as the VTL1 boot handshake.
--- drivers/hv/hv_vsm_boot.c | 63 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-)
drivers/hv/hv_vsm_boot.c +62 -1
@@ -35,6 +35,15 @@static void *vsm_skm_va; +/* +* By default, when a processor boots in VTL1, we assume that MBEC (Mode-Based Execution Control) +* support is also enabled. MBEC distinguishes between user and kernel memory execution permissions. +* After the processor boots in VTL1, we verify whether MBEC is actually enabled. If it is not, +* we set a global flag to false. This flag is shared across all processors—if any processor fails +* to enable MBEC, the system treats MBEC as disabled. +*/ +static bool hv_vsm_mbec_enabled = true; +static int hv_vsm_get_register (u32 reg_name,u64 *result) {struct hv_register_assoc reg = {
@@ -300,6 +309,51 @@ static int __init hv_vsm_get_partition_status(u16 *enabled_vtl_set, u8 *max_vtl,return 0 ; } +static int __init hv_vsm_init_code_page_offsets (void ) +{ +union hv_register_vsm_page_offsets offsets; +u64 result; +int ret; + + ret =hv_vsm_get_register (HV_REGISTER_VSM_CODE_PAGE_OFFSETS , &result); +if (ret) { +pr_err ("Failed to read VSM code page offsets: %d\n" , ret); +return ret; + } + + offsets.as_uint64 = result; +hv_vsm_init_vtlcall (offsets.vtl_call_offset); +return 0 ; +} + +static int __init hv_vsm_boot_vtl1 (void ) +{ +struct hv_vtlcall_param args = {0 }; +u16 vp_enabled_vtl_set =0 ; +u8 active_mbec_enabled =0 ; +int ret; +s64 sk_status; + + args.a0 =num_possible_cpus (); + args.a1 = sk_res.start; + args.a2 =resource_size (&sk_res); + +/* Kick start vtl1 boot on the primary cpu. */ + sk_status =hv_vsm_vtlcall (&args); +if (sk_status) +pr_warn ("VTL1 boot returned status %lld\n" , sk_status); + + ret =hv_vsm_get_vp_status (&vp_enabled_vtl_set, &active_mbec_enabled); +if (ret) +return ret; + +if (!active_mbec_enabled) { +pr_err ("Failed to enable MBEC for VP0\n" ); + hv_vsm_mbec_enabled = false; + } +return 0 ; +} +static int __init hv_vsm_bootstrap_vtl (void ) {u16 partition_enabled_vtl_set =0 , partition_mbec_enabled_vtl_set =0 ;
@@ -369,7 +423,14 @@ static int __init hv_vsm_bootstrap_vtl(void)return -EINVAL ; } } -return 0 ; + +/* Point the vtlcall trampoline at the correct hypercall page offset */ + ret =hv_vsm_init_code_page_offsets (); +if (ret) +return ret; + +/* Boot primary virtual processor in VTL1 */ +return hv_vsm_boot_vtl1 (); }static void __init hv_vsm_get_sk_mem (void )
Signature · 1 line
2.34.1
RFC PATCH 12/12 drivers: hv: hv_vsm_boot: Boot secondary processors in VTL1
In reply to Thara GopinathThe next step after the primary CPU is running in VTL1 is to boot the remaining CPUs in VTL1, so the secure kernel executes on every VP rather than only VP0. Hyper-V does not drive this: VTL1 needs to know which APs to expect, and each AP must issue its own vtlcall from VTL0 to actually transition.
As on the boot CPU, VTL1 must be enabled at each VP and its initial VP context (RIP, GDT, page tables, etc.) built before that VP can enter VTL1. This can only be done from VTL1 for non boot CPus. Introduce VSM_VTL_CALL_FUNC_ID_BOOT_APS, a vtlcall the boot cpu issues from VTL0, which asks the secure kernel to enable VTL1 and set up the initial VP context for each AP in the supplied online-CPU mask. The mask is handed to VTL1 via a shared page whose PFN is passed in the vtlcall arguments; VTL1 copies it synchronously so the page can be freed as soon as the call returns.
Once VTL1 has prepared the APs, bring them into VTL1 one at a time using a CPU-bound FIFO kthread and a completion. Serialising this way keeps VTL1 entries ordered and lets a per-AP failure be surfaced synchronously to the caller. Uniprocessor systems skip AP bring-up entirely.
--- drivers/hv/hv_vsm.h | 2 + drivers/hv/hv_vsm_boot.c | 125 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 126 insertions(+), 1 deletion(-)
drivers/hv/hv_vsm.h +2 -0
@@ -12,6 +12,8 @@#include <linux/ioport.h> #include <linux/types.h> +#define VSM_VTL_CALL_FUNC_ID_BOOT_APS 0x1FFE1 +extern struct resource sk_res;#endif /* _HV_VSM_H */
drivers/hv/hv_vsm_boot.c +124 -1
149 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [RFC PATCH 04/12] firmware: efi: libstub: x86-stub: Enable VSM awareness in efi os indications variable
In reply to Thara GopinathThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Uninitialized stack memory leak to NVRAM and unintended EFI variable size modification. - [Medium]…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Uninitialized stack memory leak to NVRAM and unintended EFI variable size modification.
- [Medium] Unconditional execution of efi_set_hv_os_indications() causes an EFI error message on all non-Hyper-V systems.
- [Medium] efi_set_hv_os_indications() fails to enable VSM if OsLoaderIndications does not already exist.
- [Low] Missing newline character in efi_info() output.
--
commit 3d285ae76c7e0c1fdff6a68f110b5b5a021e7b38
Author: Thara Gopinath <[email protected]>
firmware: efi: libstub: x86-stub: Enable VSM awareness in efi os indications variable
This commit sets bit 0 of the Hyper-V private OsLoaderIndications EFI variable during exit_boot() to let the bootloader or firmware know that the OS intends to enable VTL1. The variable is written only when supported and when the bit is not already set.
Quoted 4 lines
drivers/firmware/efi/libstub/x86-stub.c +0
-0
[ ... ]
drivers/firmware/efi/libstub/x86-stub.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, · line 774
Quoted 11 lines
drivers/firmware/efi/libstub/x86-stub.c +7
-0@@ -754,6 +765,47 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map,return EFI_SUCCESS ; } +#ifdef CONFIG_HYPERV_VSM +static void efi_set_hv_os_indications (void ) +{ +efi_guid_t guid =HYPERV_PRIVATE_EFI_NAMESPACE_GUID ; +efi_status_t status; +unsigned long size; +u32 attr, val;
[Severity: High]
Since val is declared without zero-initialization, is it possible for get_efi_var() to copy fewer than 4 bytes if the actual size of the variable in NVRAM is smaller?
If so, the upper bytes of val might remain as uninitialized stack memory. When set_efi_var() is subsequently called with size = sizeof(val), could this leak up to 3 bytes of uninitialized kernel stack memory into persistent NVRAM?
Also, could this force the EFI variable to be exactly 4 bytes, potentially corrupting it if the firmware expects a different size?
Quoted 8 lines
drivers/firmware/efi/libstub/x86-stub.c +8
-0+ + size =sizeof (val); + status =get_efi_var (efi_HvPrivOsloaderIndicationsSupported_name, + &guid, &attr, &size, &val); +if (status !=EFI_SUCCESS ) { +efi_err ("Could not read Hyper-V OsloaderIndicationsSupported\n" ); +return ; + }
drivers/firmware/efi/libstub/x86-stub.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, · line 792
Quoted 10 lines
drivers/firmware/efi/libstub/x86-stub.c +10
-0+ +if (!(val &HV_OSLOADER_INDICATION_VSM )) { +efi_info ("Hyper-V does not support VSM in OsloaderIndicationsSupported\n" ); +return ; + } + + size =sizeof (val); + status =get_efi_var (efi_HvPrivOsloaderIndications_name, &guid, &attr, &size, &val); +if (status !=EFI_SUCCESS ) { +efi_err ("Could not read Hyper-V OsLoaderIndications\n" );
[Severity: Medium]
If the OsLoaderIndications variable does not exist (for example, on a fresh OS installation or if not pre-populated by firmware), get_efi_var() will return EFI_NOT_FOUND.
Does treating this as a fatal error prevent VTL1 from being brought up?
Would it be better to handle EFI_NOT_FOUND by assuming val is 0 and proceeding to create the variable?
drivers/firmware/efi/libstub/x86-stub.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, · line 797
Quoted 5 lines
drivers/firmware/efi/libstub/x86-stub.c +5
-0+return ; + } + +if (val &HV_OSLOADER_INDICATION_VSM ) { +efi_info ("VSM is already supported in OsLoaderIndications." );
[Severity: Low]
Does this efi_info() string need a trailing newline character?
Because efi_info() maps to efi_printk() which does not append newlines automatically, this might cause subsequent EFI console output to be concatenated onto the same line.
Quoted 15 lines
drivers/firmware/efi/libstub/x86-stub.c +11
-0+ return; + } + + val |=HV_OSLOADER_INDICATION_VSM ; + size =sizeof (val); + status =set_efi_var (efi_HvPrivOsloaderIndications_name, &guid, attr, size, &val); +if (status !=EFI_SUCCESS ) +efi_err ("Could not set Hyper-V OsLoaderIndications to indicate VSM support\n" ); +} +#endif +static efi_status_t exit_boot (struct boot_params *boot_params,void *handle) {struct setup_data *e820ext =NULL ;
drivers/firmware/efi/libstub/x86-stub.c static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) · line 825
Quoted 6 lines
drivers/firmware/efi/libstub/x86-stub.c +3
-0@@ -768,6 +820,11 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)if (status !=EFI_SUCCESS )return status; +#ifdef CONFIG_HYPERV_VSM +/* Indicate to bootloader that we will be enabling VTL1 before exiting boot services */ +efi_set_hv_os_indications ();
[Severity: Medium]
Since this function is called unconditionally on all x86 EFI boots when CONFIG_HYPERV_VSM is enabled, will this cause get_efi_var() to return EFI_NOT_FOUND on systems without Hyper-V?
It seems this could result in spurious error messages in the boot log for all non-Hyper-V users because efi_err() is called unconditionally inside efi_set_hv_os_indications().
Should there be a check for Hyper-V presence before making this call?
Quoted 5 lines
drivers/firmware/efi/libstub/x86-stub.c +2
-0+#endif +/* Might as well exit boot services now */ status =efi_exit_boot_services (handle, &priv, exit_boot_func);if (status !=EFI_SUCCESS )
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [RFC PATCH 03/12] drivers: hv: Reserve memory for VSM secure kernel during early boot
In reply to Thara GopinathThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] The automatic memory reservation fallback is never executed if the `securekernel` parameter is…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] The automatic memory reservation fallback is never executed if the securekernel parameter is omitted from the kernel command line.
- [High] Manual string parsing of boot_command_line uses a fragile substring match, misparsing unrelated parameters and ignoring the pre-parsed setup argument.
- [Medium] The __setup handler returns 0 on success and -EINVAL on failure, inverting the API contract and leaking the parameter to the init process.
- [Low] parse_securekernel_simple fails to validate trailing characters after parsing the base address.
--
commit c057f006b3a9db9a754349283e3c7dd23550b133
Author: Thara Gopinath <[email protected]> Date: Tue Sep 1 16:55:17 2026 +0000
drivers: hv: Reserve memory for VSM secure kernel during early boot
The VSM secure kernel runs in VTL1 and requires its memory to be reserved early. This commit introduces a driver to handle early boot reservation of this memory, either parsed from the securekernel= command line parameter or calculated automatically as a fallback.
Quoted 5 lines
drivers/hv/hv_vsm_securekernel.c new file +0
-0
[ ... ]
Quoted 5 lines
drivers/hv/hv_vsm_securekernel.c new file +5
-0+static int __init parse_securekernel_simple (char *cmdline, +unsigned long long *securekernel_size, +unsigned long long *securekernel_base) +{ +char *cur = cmdline;
drivers/hv/hv_vsm_securekernel.c line 55
Quoted 10 lines
drivers/hv/hv_vsm_securekernel.c new file +10
-0+ + *securekernel_size =memparse (cmdline, &cur); +if (cmdline == cur) { +pr_warn ("securekernel: memory value expected\n" ); +return -EINVAL ; + } + +if (*cur =='@' ) { + *securekernel_base =memparse (cur +1 , &cur); + }else if (*cur !=' ' && *cur !='\0' ) {
[Severity: Low]
Does this code skip trailing character validation if @ was found?
Because the validation is inside an else if, if a user specifies a base address with trailing non-space characters (e.g., securekernel=16M@2G_foo), memparse() consumes the valid number, but the remaining garbage characters are silently ignored without raising an error.
Quoted 6 lines
drivers/hv/hv_vsm_securekernel.c new file +6
-0+pr_warn ("securekernel: unrecognized char: %c\n" , *cur); +return -EINVAL ; + } + +return 0 ; +}
drivers/hv/hv_vsm_securekernel.c line 71
Quoted 10 lines
drivers/hv/hv_vsm_securekernel.c new file +10
-0+ +static __init char *get_last_securekernel (char *cmdline,const char *name) +{ +char *p = cmdline, *sk_cmdline =NULL ; + +/* find securekernel and use the last one if there are more */ + p =strstr (p, name); +while (p) { + sk_cmdline = p; + p =strstr (p +1 , name);
[Severity: High]
Does this unbounded substring search misparse unrelated parameters?
Because it lacks word-boundary or prefix validation, strstr() will match the substring anywhere in the command line. The driver could misparse independent kernel parameters (e.g., nosecurekernel=100M).
Furthermore, since it explicitly searches for the last occurrence, an unrelated parameter at the end of the command line containing the substring securekernel= could overwrite a valid reservation parameter.
Quoted 7 lines
drivers/hv/hv_vsm_securekernel.c new file +7
-0+ } + +if (!sk_cmdline) +return NULL ; + +return sk_cmdline; +}
[ ... ]
Quoted 1 line
+static int
__inithv_vsm_seckernel_mem_init(char *__unused)
[Severity: High]
Is it intentional to discard the correctly pre-parsed parameter value provided by the __setup infrastructure?
The __setup() macro already does exact matching and provides the specific parameter value to the callback via its argument. By explicitly ignoring this argument (naming it __unused) and implementing a custom parser over the global boot_command_line, the driver uses a fragile manual parsing routine instead of the established API.
Quoted 3 lines
drivers/hv/hv_vsm_securekernel.c new file +3
-0+{ +unsigned long long securekernel_size =0 , securekernel_base =0 ; +int ret;
[ ... ]
drivers/hv/hv_vsm_securekernel.c line 172
Quoted 8 lines
drivers/hv/hv_vsm_securekernel.c new file +8
-0+/* Default: Find the base address automatically */ +if (!securekernel_base) { + securekernel_base =memblock_phys_alloc_range (securekernel_size,SECKERNEL_ALIGN , +0 ,SECKERNEL_ADDR_MAX ); +if (!securekernel_base) { +pr_err ("%s: Securekernel reservation failed-VSM will not be enabled.\n" , + __func__); +return -EINVAL ;
[Severity: Medium]
Will returning -EINVAL (non-zero) on the error path incorrectly tell the kernel's parser that the parameter was successfully handled?
The kernel API documentation requires __setup handlers to return 1 (or non-zero) if handled and 0 if not. Returning a non-zero error code treats the failed parameter as successfully consumed.
Quoted 4 lines
drivers/hv/hv_vsm_securekernel.c new file +4
-0+ } + } + + pr_info("Reserving %ldMB of memory at 0x%llx(%ld MB) for securekernel(System RAM:%ldMB)\n",
drivers/hv/hv_vsm_securekernel.c line 186
Quoted 10 lines
drivers/hv/hv_vsm_securekernel.c new file +10
-0+ (unsigned long )(securekernel_size >>20 ), + securekernel_base, + (unsigned long )(securekernel_base >>20 ), + (unsigned long)(memblock_phys_mem_size() >> 20)); + + sk_res.start = securekernel_base; + sk_res.end = securekernel_base + securekernel_size -1 ; +insert_resource (&iomem_resource, &sk_res); + +return 0 ;
[Severity: Medium]
Will returning 0 on the success path inform the kernel's setup infrastructure that the parameter was not handled?
This causes the kernel to erroneously pass the securekernel=... parameter as an environment variable to the init process. The handler should return 1 when the parameter is successfully handled.
drivers/hv/hv_vsm_securekernel.c
Quoted 2 lines
drivers/hv/hv_vsm_securekernel.c new file +2
-0+} +__setup ("securekernel" , hv_vsm_seckernel_mem_init);
[Severity: High]
Can the automatic memory reservation fallback ever execute if the parameter is omitted from the kernel command line?
Functions registered with __setup() are only invoked by init/main.c if the target string matches a command-line parameter. If absent, the initialization function is completely bypassed, meaning the core feature (automatic memory reservation) fails entirely by default, and the VSM secure kernel will not boot due to lack of reserved memory.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [RFC PATCH 02/12] drivers: hv: hv_common: Allocate Hyper-V output arg page when VSM is enabled
In reply to Thara GopinathThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] hv_apicid_to_vp_index() unconditionally dereferences the unallocated hyperv_pcpu_output_arg pointer in standard/SNP guests. --
commit 0ee3313e113c9ca7969923f2af5a1ed18ddb25d2
Author: Thara Gopinath <[email protected]>
drivers: hv: hv_common: Allocate Hyper-V output arg page when VSM is enabled
This commit extends hv_output_page_exists() to return true when CONFIG_HYPERV_VSM is enabled. This ensures the per-CPU output argument page is allocated at boot for VSM hypercalls.
drivers/hv/hv_common.c static void hv_kmsg_dump_register(void) · line 263
Quoted 11 lines
drivers/hv/hv_common.c +2
-1@@ -259,7 +259,8 @@ static void hv_kmsg_dump_register(void)static inline bool hv_output_page_exists (void ) { -return hv_parent_partition () ||IS_ENABLED (CONFIG_HYPERV_VTL_MODE ); +return hv_parent_partition () ||IS_ENABLED (CONFIG_HYPERV_VTL_MODE ) || +IS_ENABLED (CONFIG_HYPERV_VSM );
[Severity: High]
This is a pre-existing issue, but modifying this function highlights a vulnerability for SEV-SNP guests where VSM and VTL modes are disabled.
When an SEV-SNP guest boots secondary CPUs, hv_smp_prepare_cpus() overrides the wakeup callback with hv_snp_boot_ap(), which then calls hv_apicid_to_vp_index() in arch/x86/hyperv/ivm.c.
If CONFIG_HYPERV_VSM and CONFIG_HYPERV_VTL_MODE are both disabled, hv_output_page_exists() evaluates to false. This means hyperv_pcpu_output_arg is never allocated during hv_common_init() and remains NULL.
Consequently, arch/x86/hyperv/hv_init.c:hv_apicid_to_vp_index() dereferences this unallocated pointer:
arch/x86/hyperv/hv_init.c:hv_apicid_to_vp_index() {
...
output = *this_cpu_ptr(hyperv_pcpu_output_arg);
...
status = hv_do_hypercall(HVCALL_GET_VP_ID_FROM_APIC_ID, input, output);
...
}Since this_cpu_ptr(NULL) resolves to the base of the per-CPU area, it dereferences a garbage pointer. This is then passed to hv_do_hypercall(), causing the hypervisor to overwrite random physical memory with the hypercall result, and eventually oopsing the kernel when output[0] is read.
Should hv_output_page_exists() also be extended to account for SEV-SNP guests that rely on hv_apicid_to_vp_index() during boot?
Quoted 3 lines
drivers/hv/hv_common.c +0
-0}void __init hv_get_partition_id (void )
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [RFC PATCH 07/12] drivers: hv: hv_vsm_boot: load secure kernel image from firmware
In reply to Thara GopinathThank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider: - [High] The ELF loader lacks bounds checking for program headers and segment data against the loaded…
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] The ELF loader lacks bounds checking for program headers and segment data against the loaded firmware size, allowing out-of-bounds memory reads.
- [High] Missing validation that p_memsz >= p_filesz leads to integer underflow and massive out-of-bounds memset.
- [High] Signed-to-unsigned integer promotion bypasses the spatial boundary check, allowing out-of-bounds memory writes.
- [High] The ELF load alignment is passed directly to PTR_ALIGN without verifying it is a power of two, breaking pointer arithmetic.
- [High] The bounds check uses a hardcoded maximum size instead of the actual reserved memory resource size, allowing out-of-bounds writes.
- [High] Integer overflow in the calculation of the ELF binary memory footprint bypasses spatial bounds checks.
- [Medium] Incorrect use of the PTR_ALIGN macro miscalculates alignment by unnecessarily pre-adding the alignment value, violating generic alignment semantics. --
commit a2d9d89a62300f04ed251e2db601b303f521e71c
Author: Thara Gopinath <[email protected]>
drivers: hv: hv_vsm_boot: load secure kernel image from firmware
This commit adds a VTL0-side loader for LVBS bring-up that stages a secure kernel image into the reserved memory region. It fetches a 64-bit ELF using request_firmware(), validates the ELF header, and stages the PT_LOAD segments into memory.
Quoted 3 lines
drivers/hv/hv_vsm_boot.c +0
-0
[ ... ]
Quoted 1 line
drivers/hv/hv_vsm_boot.c +0
-0@@ -38,6 +55,167 @@ static int hv_vsm_get_register(u32 reg_name, u64 *result)
[ ... ]
Quoted 2 lines
drivers/hv/hv_vsm_boot.c +2
-0+static size_t __init hv_vsm_elf_binary_size (void *image) +{
[ ... ]
drivers/hv/hv_vsm_boot.c
Quoted 1 line
drivers/hv/hv_vsm_boot.c +1
-0+for (i =0 ; i < ehdr->e_phnum; i++, phdr++) {
[Severity: High]
Is it safe to blindly iterate over and read memory from phdr here? If the program header table offset or count extends beyond the firmware limits, this could result in out-of-bounds memory reads.
drivers/hv/hv_vsm_boot.c static int hv_vsm_get_register(u32 reg_name, u64 *result) · line 91
Quoted 4 lines
drivers/hv/hv_vsm_boot.c +4
-0+if (phdr->p_type !=PT_LOAD ) +continue ; + + max_paddr =max (max_paddr, phdr->p_paddr + phdr->p_memsz);
[Severity: High]
Can this calculation overflow? If a malformed ELF segment defines p_paddr and p_memsz such that their sum exceeds the 64-bit integer limit, it could wrap around. This would result in an undersized binary size, potentially bypassing later spatial bounds checks and allowing out-of-bounds writes.
Quoted 7 lines
drivers/hv/hv_vsm_boot.c +7
-0+ } + +return max_paddr - min_paddr; +} + +static int __init hv_vsm_load_elf (void *image,Elf64_Addr *sk_entry_pa) +{
[ ... ]
drivers/hv/hv_vsm_boot.c static int hv_vsm_get_register(u32 reg_name, u64 *result) · line 120
Quoted 2 lines
drivers/hv/hv_vsm_boot.c +2
-0+/* Align the base load address up to the first PT_LOAD segment alignment */ + base_addr =PTR_ALIGN (vsm_skm_va + first_load_align, first_load_align);
[Severity: Medium]
Does this code miscalculate the alignment? By explicitly adding first_load_align before invoking PTR_ALIGN, it forces the pointer to skip an entire alignment block even if it is already aligned, which wastes memory within the strictly reserved region.
[Severity: High]
Additionally, PTR_ALIGN relies on the alignment being a power of two. Since first_load_align is only validated below using modulo (modulo SZ_2M), could a non-power-of-two alignment corrupt the pointer calculation entirely?
drivers/hv/hv_vsm_boot.c static int hv_vsm_get_register(u32 reg_name, u64 *result) · line 123
Quoted 3 lines
drivers/hv/hv_vsm_boot.c +3
-0+ + size =hv_vsm_elf_binary_size (image); +if (vsm_skm_va +VSM_SK_INITIAL_MAP_SIZE - base_addr < size) {
[Severity: High]
Can a malicious base_addr bypass this validation entirely? If base_addr is located beyond the vsm_skm_va + VSM_SK_INITIAL_MAP_SIZE limit, the pointer arithmetic yields a negative ptrdiff_t. When compared against the unsigned size_t size, it undergoes standard C integer promotion to a massive positive value, completely bypassing the boundary condition.
[Severity: High]
Also, does this check artificially limit the copy size against a hardcoded constant rather than the actual bounds of the allocated sk_res region? If a user reserves a secure kernel region smaller than 16MB via the kernel command line, this could allow writes outside the dynamically allocated physical region.
Quoted 4 lines
drivers/hv/hv_vsm_boot.c +4
-0+pr_err ("secure kernel does not fit: %zu > %td\n" , size, + vsm_skm_va +VSM_SK_INITIAL_MAP_SIZE - base_addr); +return -EFBIG ; + }
[ ... ]
Quoted 6 lines
drivers/hv/hv_vsm_boot.c +6
-0+for (i =0 ; i < ehdr->e_phnum; i++, phdr++) { +void *load_addr; + +if (phdr->p_type !=PT_LOAD ) +continue ; +
drivers/hv/hv_vsm_boot.c static int hv_vsm_get_register(u32 reg_name, u64 *result) · line 161
Quoted 10 lines
drivers/hv/hv_vsm_boot.c +10
-0+/* +* Adjust the load address by min_paddr to compensate the +* offset. +*/ + load_addr = base_addr + (phdr->p_paddr - min_paddr); + +pr_debug (" p_offset: %#016llx, p_filesz: %#016llx, p_memsz: %#016llx to pa %#016llx\n" , + phdr->p_offset, phdr->p_filesz, phdr->p_memsz, +virt_to_phys (load_addr)); +memcpy (load_addr, image + phdr->p_offset, phdr->p_filesz);
[Severity: High]
Are the ELF segment properties validated before this memcpy? The source memory is derived from phdr->p_offset and phdr->p_filesz, which don't appear to be checked against the total firmware size. This risks out-of-bounds memory reads.
drivers/hv/hv_vsm_boot.c static int hv_vsm_get_register(u32 reg_name, u64 *result) · line 170
Quoted 9 lines
drivers/hv/hv_vsm_boot.c +9
-0+ +if (phdr->p_memsz == phdr->p_filesz) +continue ; + +pr_debug (" zeroing %#016llx bytes at pa %#016llx\n" , + phdr->p_memsz - phdr->p_filesz, +virt_to_phys (load_addr + phdr->p_filesz)); +memset (load_addr + phdr->p_filesz,0 , + phdr->p_memsz - phdr->p_filesz);
[Severity: High]
Can this subtraction underflow? If a malformed ELF provides a PT_LOAD segment where phdr->p_filesz is larger than phdr->p_memsz, this calculation would result in a massive out-of-bounds memset that could destroy memory.
Quoted 12 lines
drivers/hv/hv_vsm_boot.c +12
-0+ } + + *sk_entry_pa =virt_to_phys (base_addr + (ehdr->e_entry - min_paddr)); +pr_debug ("secure kernel entry pa: %#llx\n" , *sk_entry_pa); + +return 0 ; +} + +static int __init hv_vsm_load_secure_kernel (Elf64_Addr *sk_entry_pa) +{ +const struct firmware *fw; +Elf64_Ehdr *ehdr;
drivers/hv/hv_vsm_boot.c static int hv_vsm_get_register(u32 reg_name, u64 *result) · line 192
Quoted 10 lines
drivers/hv/hv_vsm_boot.c +10
-0+int ret; + + ret =request_firmware (&fw,SK_FW_NAME ,NULL ); +if (ret) { +pr_err ("Failed to load %s firmware: %d\n" ,SK_FW_NAME , ret); +return ret; + } + + ehdr = (Elf64_Ehdr *)fw->data; + if (fw->size < sizeof(*ehdr) ||
[Severity: High]
Does this bounds check sufficiently validate the firmware size? It appears to omit validation for the program headers' offset and count, which could allow a malformed firmware file to trigger out-of-bounds reads later in the loader.
Quoted 2 lines
drivers/hv/hv_vsm_boot.c +2
-0+ memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || + (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewRe: [RFC PATCH 06/12] drivers: hv: Add VSM boot driver and enable VTL1 at the partition level
In reply to Thara GopinathThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Incorrect validation of the MBEC enablement bitmask allows silent failure of VTL1 security…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Incorrect validation of the MBEC enablement bitmask allows silent failure of VTL1 security configuration.
- [High] Missing MBEC validation when VTL1 is found to be already enabled, allowing acceptance of insecure pre-configured VTL1 states.
- [Medium] hv_vsm_boot_init pins initialization to cpumask_first(cpu_online_mask), which is incorrect for kexec/kdump where the boot CPU may not be CPU 0. --
commit cce0c5216b1bbddd78ce31614c3d1587f8bcd4ea
Author: Thara Gopinath <[email protected]>
drivers: hv: Add VSM boot driver and enable VTL1 at the partition level
This commit introduces a driver to bring up Linux VBS (LVBS) using Hyper-V's Virtual Secure Mode. It provides the initial sequence to probe privileges, pin execution to the VTL0 boot CPU, and enable VTL1 at the partition level with MBEC.
Quoted 5 lines
drivers/hv/hv_vsm_boot.c new file +0
-0
[ ... ]
Quoted 10 lines
drivers/hv/hv_vsm_boot.c new file +10
-0+static int __init hv_vsm_bootstrap_vtl (void ) +{ +u16 partition_enabled_vtl_set =0 , partition_mbec_enabled_vtl_set =0 ; +u8 partition_max_vtl; +int ret; + +/* Check and enable VTL1 at the partition level */ + ret =hv_vsm_get_partition_status (&partition_enabled_vtl_set, &partition_max_vtl, + &partition_mbec_enabled_vtl_set); +if (ret)
drivers/hv/hv_vsm_boot.c line 103
Quoted 10 lines
drivers/hv/hv_vsm_boot.c new file +10
-0+return ret; + +if (partition_max_vtl <HV_VTL_SECURE ) { +pr_err ("VTL1 is not supported by the partition\n" ); +return -EINVAL ; + } + +if (partition_enabled_vtl_set &HV_VTL1_ENABLE_BIT ) { +pr_info ("Partition VTL1 is already enabled\n" ); + }else {
[Severity: High]
Does this code skip verifying MBEC when VTL1 is already enabled?
If an attacker or previous boot stage races to enable VTL1 but omits enabling MBEC, checking partition_enabled_vtl_set here finds VTL1 enabled and skips further configuration without validating partition_mbec_enabled_vtl_set. Could this allow acceptance of insecure pre-configured VTL1 states?
Quoted 5 lines
drivers/hv/hv_vsm_boot.c new file +5
-0+ ret =hv_vsm_enable_partition_vtl (); +if (ret) { +pr_err ("Enabling Partition VTL1 failed with status 0x%x\n" , + ret); +return -EINVAL ;
drivers/hv/hv_vsm_boot.c line 118
Quoted 10 lines
drivers/hv/hv_vsm_boot.c new file +10
-0+ } + ret =hv_vsm_get_partition_status (&partition_enabled_vtl_set, &partition_max_vtl, + &partition_mbec_enabled_vtl_set); +if (ret) +return ret; +if (!(partition_enabled_vtl_set &HV_VTL1_ENABLE_BIT )) { +pr_err ("Tried Enabling Partition VTL 1 and still failed\n" ); +return -EINVAL ; + } +if (!partition_mbec_enabled_vtl_set) {
[Severity: High]
Since partition_mbec_enabled_vtl_set is a bitmask of all VTLs with MBEC, does this validation only check if the mask is completely empty?
If VTL0 already has MBEC enabled (bit 0 is 1), the mask is non-zero, causing this check to falsely pass even if the hypervisor failed to enable MBEC for VTL1. Should this instead isolate the VTL1 bit, for example:
!(partition_mbec_enabled_vtl_set & HV_VTL1_ENABLE_BIT) ?
Quoted 6 lines
drivers/hv/hv_vsm_boot.c new file +6
-0+pr_err ("Tried Enabling Partition MBEC and failed\n" ); +return -EINVAL ; + } + } +return 0 ; +}
[ ... ]
Quoted 12 lines
drivers/hv/hv_vsm_boot.c new file +12
-0+static int __init hv_vsm_boot_init (void ) +{ +cpumask_var_t mask; +unsigned int boot_cpu; +int ret; + +if (!vsm_arch_has_vsm_access ()) +return 0 ; + +/* +* Copy the current cpu mask and pin rest of the running code to boot cpu. +* Important since we want boot cpu of VTL0 to be the boot cpu for VTL1.
drivers/hv/hv_vsm_boot.c line 158
Quoted 10 lines
drivers/hv/hv_vsm_boot.c new file +10
-0+* ToDo: Check if copying and restoring current->cpus_mask is enough +* ToDo: Verify the assumption that cpumask_first(cpu_online_mask) is +* the boot cpu +*/ +if (!alloc_cpumask_var (&mask,GFP_KERNEL )) +panic ("Could not allocate cpumask" ); + +cpumask_copy (mask, ¤t->cpus_mask); + boot_cpu =cpumask_first (cpu_online_mask); +set_cpus_allowed_ptr (current,cpumask_of (boot_cpu));
[Severity: Medium]
Will this incorrectly pin the task to the lowest-numbered online CPU rather than the actual boot CPU during a kexec or kdump?
If the kernel is booted via kexec or kdump on a non-zero CPU, lower-numbered CPUs might be brought online during SMP initialization. In this scenario, cpumask_first(cpu_online_mask) returns the lowest numbered online CPU, which is not necessarily the Bootstrap Processor (BSP). Is get_boot_cpu_id() the proper API to use here?
Quoted 2 lines
drivers/hv/hv_vsm_boot.c new file +2
-0+ + ret =hv_vsm_bootstrap_vtl ();
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [RFC PATCH 08/12] arch: x86: hyperv: Build initial vCPU context for VTL1 secure kernel
In reply to Thara GopinathThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Page table initialization out-of-bounds write to the PDE page if the mapping crosses a 1GB…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Page table initialization out-of-bounds write to the PDE page if the mapping crosses a 1GB boundary.
- [High] Hardware-consumed GDT, TSS, and Kernel Stack pages are mapped but never zero-initialized, exposing host memory fragments.
- [Low] The TSS GDT descriptor limit is set to tss_size instead of tss_size - 1.
--
commit 6f8a1a825749fc111adb451004cf1aa1f0e24ee1
Author: Thara Gopinath <[email protected]>
arch: x86: hyperv: Build initial vCPU context for VTL1 secure kernel
This commit adds the architecture-specific initialization function to construct the initial page tables, GDT/TSS, and CPU register state for transitioning the boot processor to the VTL1 secure kernel. It allocates and populates the data structures required by Hyper-V's hv_init_vp_context.
Quoted 5 lines
arch/x86/hyperv/hv_vtl_vsm.c new file +0
-0
[ ... ]
arch/x86/hyperv/hv_vtl_vsm.c line 73
Quoted 12 lines
arch/x86/hyperv/hv_vtl_vsm.c new file +12
-0+static void __init hv_vsm_fill_pte_tables (phys_addr_t sk_pa,u64 *pde, +int pd_index,int num_pte_tables) +{ +u16 i, j; +phys_addr_t pte_pa; +u64 *pte; + +/* Fill page tables with entries */ +for (i =0 ; i < num_pte_tables; i++) { + pte_pa =PAGE_AT (sk_pa,VSM_PTE_PAGES + i); + pte =phys_to_virt (pte_pa); + *(pde + pd_index + i) = pte_pa |VSM_PTE_FLAGS ;
[Severity: High]
Does this loop overflow the 4KB PDE page if sk_pa crosses a 1GB boundary?
The allocator only guarantees a 2MB alignment for sk_pa, so pd_index can be near the end of the page directory (e.g., 511). Since num_pte_tables can be up to 8 (for a 16MB initial mapping), pd_index + i will exceed the 511 maximum index of the page directory, causing an out-of-bounds write into the adjacent VSM_PTE_PAGES memory.
[ ... ]
Quoted 2 lines
arch/x86/hyperv/hv_vtl_vsm.c new file +2
-0+static void __init hv_vsm_init_page_tables (struct hv_init_vp_context *vp_ctx,phys_addr_t sk_pa) +{
[ ... ]
arch/x86/hyperv/hv_vtl_vsm.c line 113
Quoted 10 lines
arch/x86/hyperv/hv_vtl_vsm.c new file +10
-0+ pml4e =phys_to_virt (pml4e_pa); + pdpe =phys_to_virt (pdpe_pa); + pde =phys_to_virt (pde_pa); + +/* +* Zero the PML4, PDP, PD and PTE pages before populating them so that +* any entry not explicitly written below has its present bit clear. +*/ +memset (pml4e,0 , + (VSM_KERNEL_STACK_PAGE -VSM_PML4E_PAGE ) *VSM_PAGE_SIZE );
[Severity: High]
By starting the memset at VSM_PML4E_PAGE and stopping before VSM_KERNEL_STACK_PAGE, does this leave VSM_GDT_PAGE, VSM_TSS_PAGE, and the kernel stack page completely uninitialized?
The underlying physical memory is not guaranteed to be zeroed by the allocator, which would expose host memory fragments directly to the secure kernel.
[ ... ]
Quoted 4 lines
arch/x86/hyperv/hv_vtl_vsm.c new file +4
-0+static void __init hv_vsm_init_gdt (struct hv_init_vp_context *vp_ctx,phys_addr_t sk_pa) +{ +phys_addr_t gdt_pa, tss_pa, kstack_pa; +void *gdt_va;
arch/x86/hyperv/hv_vtl_vsm.c line 141
Quoted 10 lines
arch/x86/hyperv/hv_vtl_vsm.c new file +10
-0+u64 tss_sk_va, gdt; +struct x86_hw_tss *tss; +size_t gdt_size =sizeof (gdt), tss_size =sizeof (*tss), gdt_offset =0 ; + +/* Get a page for the GDT */ + gdt_pa =PAGE_AT (sk_pa,VSM_GDT_PAGE ); + gdt_va =phys_to_virt (gdt_pa); +/* Get a page for the TSS */ + tss_pa =PAGE_AT (sk_pa,VSM_TSS_PAGE ); + tss =phys_to_virt (tss_pa);
[Severity: High]
Since the memory backing tss was skipped by the memset in hv_vsm_init_page_tables(), will the hardware task state segment be populated with random garbage?
This would leave hardware fields like the interrupt stack tables (ist) and the IO bitmap base uninitialized, potentially causing a fatal exception when an interrupt is taken in VTL1.
[ ... ]
arch/x86/hyperv/hv_vtl_vsm.c line 175
Quoted 11 lines
arch/x86/hyperv/hv_vtl_vsm.c new file +11
-0+/* +* Make and add a system segment descriptor for the TSS in the GDT. +* +* In 64-bit mode a system-segment descriptor (TSS/LDT) is 16 bytes +* wide: the lower 8 bytes have the same layout as the legacy 32-bit +* descriptor (produced by GDT_ENTRY), and the upper 8 bytes hold +* base[63:32] in the low 32 bits with the high 32 bits reserved 0. +* GDT_ENTRY masks base to 32 bits, so the upper half must be written +* explicitly. +*/ + gdt =GDT_ENTRY (DESC_TSS32 , tss_sk_va, tss_size);
[Severity: Low]
Is this descriptor's limit off by one?
x86 segment limits are inclusive, so shouldn't this pass tss_size - 1 rather than tss_size to avoid allowing access one byte past the end of the TSS?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[email protected]
AI reviewRe: [RFC PATCH 11/12] drivers: hv: hv_vsm_boot: Boot primary processor in VTL1
In reply to Thara GopinathThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Ignored error status from VTL1 boot handshake allows the system to continue in an insecure state…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Ignored error status from VTL1 boot handshake allows the system to continue in an insecure state, bypassing a security-critical kernel panic. --
commit 91cb8ed727ae4b19c3d3be7dbd03c34def48f245
Author: Thara Gopinath <[email protected]>
drivers: hv: hv_vsm_boot: Boot primary processor in VTL1
This commit introduces the final step of the primary-CPU bring-up to transition into VTL1 and start executing the secure kernel. It issues the first vtlcall on the primary CPU to pass initialization arguments. It also adds a check to see if Mode-Based Execution Control (MBEC) is enabled after booting into VTL1.
Quoted 4 lines
drivers/hv/hv_vsm_boot.c +0
-0
[ ... ]
Quoted 17 lines
drivers/hv/hv_vsm_boot.c +17
-0+static int __init hv_vsm_boot_vtl1 (void ) +{ +struct hv_vtlcall_param args = {0 }; +u16 vp_enabled_vtl_set =0 ; +u8 active_mbec_enabled =0 ; +int ret; +s64 sk_status; + + args.a0 =num_possible_cpus (); + args.a1 = sk_res.start; + args.a2 =resource_size (&sk_res); + +/* Kick start vtl1 boot on the primary cpu. */ + sk_status =hv_vsm_vtlcall (&args); +if (sk_status) +pr_warn ("VTL1 boot returned status %lld\n" , sk_status); +
drivers/hv/hv_vsm_boot.c static int __init hv_vsm_get_partition_status(u16 *enabled_vtl_set, u8 *max_vtl, · line 355
Quoted 10 lines
drivers/hv/hv_vsm_boot.c +10
-0+ ret =hv_vsm_get_vp_status (&vp_enabled_vtl_set, &active_mbec_enabled); +if (ret) +return ret; + +if (!active_mbec_enabled) { +pr_err ("Failed to enable MBEC for VP0\n" ); + hv_vsm_mbec_enabled = false; + } +return 0 ; +}
^^^^
[Severity: High]
Does this code silently ignore a VTL1 boot failure?
If hv_vsm_vtlcall() returns a non-zero error in sk_status, this function merely logs a warning and proceeds to call hv_vsm_get_vp_status().
If hv_vsm_get_vp_status() succeeds despite the VTL1 boot failure, hv_vsm_boot_vtl1() will return 0, because vp_enabled_vtl_set is never validated to confirm VTL1 actually booted.
This could allow the system to continue by bypassing failure handling in the caller. If VTL1 setup fails, could the partition be left in an exposed state where an attacker might race to configure VTL1 themselves and gain a higher-privilege foothold than VTL0?
Should this return an error code when hv_vsm_vtlcall() fails?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
[email protected]
AI reviewRe: [RFC PATCH 09/12] drivers: hv: hv_vsm_boot: Enable VTL1 on the boot processor
In reply to Thara GopinathThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Unconditional loading of the secure kernel overwrites VTL1 memory even if VTL1 is already active, leading to memory corruption or fatal faults.
Pre-existing issues:
- [High] Missing bounds check when parsing ELF program headers from firmware, leading to potential out-of-bounds read. --
commit 58f06b8de69e2f750e55a8ff7220ab38d88c0f8f
Author: Thara Gopinath <[email protected]>
drivers: hv: hv_vsm_boot: Enable VTL1 on the boot processor
This commit introduces helpers to get VP status and enable VTL1 on the boot virtual processor. It extends hv_vsm_bootstrap_vtl() to enable VTL1 on the boot VP if it is not already enabled, and re-queries the status to confirm the transition.
drivers/hv/hv_vsm_boot.c static int __init hv_vsm_load_secure_kernel(Elf64_Addr *sk_entry_pa) · line 217
Quoted 7 lines
drivers/hv/hv_vsm_boot.c +0
-0@@ -216,6 +216,47 @@ static int __init hv_vsm_load_secure_kernel(Elf64_Addr *sk_entry_pa)return ret; }
[Severity: High]
This isn't a bug introduced by this patch, but there appears to be a missing bounds check when parsing ELF program headers from the firmware in hv_vsm_load_elf() which is called by hv_vsm_load_secure_kernel().
The firmware size (fw->size) is discarded instead of being passed to hv_vsm_load_elf() for validation. Inside hv_vsm_load_elf(), the program header table offset (e_phoff) is never bounds-checked against the firmware size before being iterated over:
drivers/hv/hv_vsm_boot.c:hv_vsm_load_elf() {
...
Elf64_Phdr *phdr = image + ehdr->e_phoff;
...
}Later, the memcpy() copies uncontrolled lengths of kernel memory based on untrusted firmware headers:
drivers/hv/hv_vsm_boot.c:hv_vsm_load_elf() {
...
memcpy(load_addr, image + phdr->p_offset, phdr->p_filesz);
...
}Could a malformed or maliciously crafted firmware file cause an out-of-bounds read past the end of the firmware buffer, potentially leaking sensitive kernel data into the secure kernel's memory or causing a kernel panic?
[ ... ]
Quoted 6 lines
drivers/hv/hv_vsm_boot.c +1
-1@@ -300,7 +342,34 @@ static int __init hv_vsm_bootstrap_vtl(void) } } -return hv_vsm_load_secure_kernel (&sk_entry_pa); + ret =hv_vsm_load_secure_kernel (&sk_entry_pa);
drivers/hv/hv_vsm_boot.c static int __init hv_vsm_bootstrap_vtl(void) · line 355
Quoted 10 lines
drivers/hv/hv_vsm_boot.c +10
-0+if (ret) +return ret; + +/* Check and enable VTL1 for the primary virtual processor */ + ret =hv_vsm_get_vp_status (&vp_enabled_vtl_set, &active_mbec_enabled); +if (ret) +return ret; + +if (vp_enabled_vtl_set &HV_VTL1_ENABLE_BIT ) { +pr_info ("VP VTL1 is already enabled\n" );
[Severity: High]
If VTL1 is already enabled, does calling hv_vsm_load_secure_kernel() earlier in this function overwrite the memory of the currently running VTL1 secure kernel?
Since hv_vsm_load_secure_kernel() is called unconditionally before checking if VP VTL1 is active, could unconditionally loading the secure kernel ELF image cause a regression by triggering a fatal hypervisor intercept/fault in VTL0, or silently corrupting the active VTL1 (such as during a kexec reboot)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
[email protected]
AI reviewRe: [RFC PATCH 12/12] drivers: hv: hv_vsm_boot: Boot secondary processors in VTL1
In reply to Thara GopinathThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] The boot CPU is incorrectly included in the `cpu_online_mask` passed to VTL1. - [High] AP VTL1…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] The boot CPU is incorrectly included in the cpu_online_mask passed to VTL1.
- [High] AP VTL1 boot failures are silently ignored, leaving the system in a partially-booted secure state.
- [High] Unsafe usage of smp_processor_id() in preemptible context and task migration race.
- [Medium] Unprotected snapshot and iteration of cpu_online_mask.
--
commit 99445d1f4eda186e7be403a5b296f1c80a9cdae6
Author: Thara Gopinath <[email protected]>
drivers: hv: hv_vsm_boot: Boot secondary processors in VTL1
This commit adds support to boot secondary processors into VTL1 after the primary CPU has transitioned. It passes an online CPU mask to VTL1 via a shared page and uses a CPU-bound kthread to sequentially issue a vtlcall for each AP.
Quoted 4 lines
drivers/hv/hv_vsm_boot.c +0
-0
[ ... ]
Quoted 15 lines
drivers/hv/hv_vsm_boot.c +15
-0+static int __init hv_vsm_boot_sec_vp_thread_fn (void *arg) +{ +struct hv_vsm_ap_boot_ctx *ctx = arg; +struct hv_vtlcall_param args = {0 }; +int cpu =smp_processor_id (); +u16 vp_enabled_vtl_set =0 ; +u8 active_mbec_enabled =0 ; +s64 sk_status; +int ret =0 ; + +pr_info ("cpu%d entering vtl1 boot thread\n" , cpu); + sk_status =hv_vsm_vtlcall (&args); +if (sk_status) +pr_warn ("VP%d VTL1 boot returned status %lld\n" , cpu, sk_status); +
drivers/hv/hv_vsm_boot.c static int __init hv_vsm_init_code_page_offsets(void) · line 373
Quoted 10 lines
drivers/hv/hv_vsm_boot.c +10
-0+ ret =hv_vsm_get_vp_status (&vp_enabled_vtl_set, &active_mbec_enabled); +if (ret) + goto out; + +if (!active_mbec_enabled) { +pr_err ("Failed to enable MBEC for VP%d\n" , cpu); + hv_vsm_mbec_enabled = false; + } +out: + ctx->ret = ret;
[Severity: High]
If hv_vsm_vtlcall() fails and returns an error in sk_status, will this error be silently ignored? The warning is printed, but ret remains 0 before calling hv_vsm_get_vp_status(). If hv_vsm_get_vp_status() succeeds, ret is unconditionally set to 0. Does this leave the caller believing the AP booted successfully instead of surfacing the error?
Quoted 3 lines
drivers/hv/hv_vsm_boot.c +3
-0+complete (&ctx->done); +return 0 ; +}
[ ... ]
drivers/hv/hv_vsm_boot.c static int __init hv_vsm_init_code_page_offsets(void) · line 403
Quoted 5 lines
drivers/hv/hv_vsm_boot.c +5
-0+static int __init hv_vsm_boot_ap_vtl (void ) +{ +struct hv_vtlcall_param args = {0 }; +struct page *cpu_online_page; +unsigned int cpu, cur_cpu =smp_processor_id ();
[Severity: High]
Is it safe to use smp_processor_id() here in a preemptible context? Fetching it here triggers a kernel BUG warning. Also, because the thread can migrate between CPU evaluation and the sleepable operations like alloc_page() and wait_for_completion() below, could cur_cpu become stale? This might cause the AP loop to skip the new CPU and incorrectly attempt to boot the original primary CPU as an AP.
drivers/hv/hv_vsm_boot.c static int __init hv_vsm_init_code_page_offsets(void) · line 411
Quoted 8 lines
drivers/hv/hv_vsm_boot.c +8
-0+s64 sk_status; +int ret; + + cpu_online_page =hv_vsm_alloc_shared_page (); +if (IS_ERR (cpu_online_page)) +return PTR_ERR (cpu_online_page); + +cpumask_copy (page_address (cpu_online_page), cpu_online_mask);
[Severity: Medium]
Should the snapshot of cpu_online_mask and subsequent iteration be protected by cpus_read_lock()? Accessing this without hotplug protection could allow concurrent CPU hotplug events to alter the mask, potentially passing an inconsistent snapshot to VTL1 or binding an AP boot kthread to a newly offlined CPU.
[Severity: High]
Does this verbatim copy of cpu_online_mask incorrectly include the already-booted primary CPU in the mask passed to VTL1? If cur_cpu isn't cleared from the mask first, could VTL1 mistakenly expect the primary CPU to issue an AP boot vtlcall and hang or corrupt the primary CPU's secure context?
Quoted 9 lines
drivers/hv/hv_vsm_boot.c +9
-0+ +/* +* Hand VTL1 the set of APs to expect. VTL1 copies the mask +* synchronously inside this vtlcall and does not reference the +* page after it returns, so freeing it here is safe. +*/ + args.a0 =VSM_VTL_CALL_FUNC_ID_BOOT_APS ; + args.a1 =page_to_pfn (cpu_online_page); + sk_status =hv_vsm_vtlcall (&args);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Wei Liu
maintainerRe: [RFC PATCH 02/12] drivers: hv: hv_common: Allocate Hyper-V output arg page when VSM is enabled
In reply to [email protected]Quoted 50 lines · On Tue, Sep 01, 2026 at 05:12:38PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High]hv_apicid_to_vp_index()unconditionally dereferences the unallocatedhyperv_pcpu_output_argpointer in standard/SNP guests. --commit
0ee3313e113c9ca7969923f2af5a1ed18ddb25d2
Author: Thara Gopinath <[email protected]>drivers: hv:
hv_common: Allocate Hyper-V output arg page when VSM is enabledThis commit extends
hv_output_page_exists()to return true whenCONFIG_HYPERV_VSMis enabled. This ensures the per-CPU output argument page is allocated at boot for VSM hypercalls.drivers/hv/hv_common.c static void hv_kmsg_dump_register(void) · line 263
Quoted 11 lines
drivers/hv/hv_common.c +2
-1@@ -259,7 +259,8 @@ static void hv_kmsg_dump_register(void)static inline bool hv_output_page_exists (void ) { -return hv_parent_partition () ||IS_ENABLED (CONFIG_HYPERV_VTL_MODE ); +return hv_parent_partition () ||IS_ENABLED (CONFIG_HYPERV_VTL_MODE ) || +IS_ENABLED (CONFIG_HYPERV_VSM );[Severity: High]
This is a pre-existing issue, but modifying this function highlights a vulnerability for SEV-SNP guests where VSM and VTL modes are disabled.When an SEV-SNP guest boots secondary CPUs,
hv_smp_prepare_cpus()overrides the wakeup callback withhv_snp_boot_ap(), which then callshv_apicid_to_vp_index()inarch/x86/hyperv/ivm.c.If
CONFIG_HYPERV_VSMandCONFIG_HYPERV_VTL_MODEare both disabled,hv_output_page_exists()evaluates to false. This meanshyperv_pcpu_output_argis never allocated duringhv_common_init()and remains NULL.Consequently, arch/x86/hyperv/hv_init.c:hv_apicid_to_vp_index() dereferences this unallocated pointer:
arch/x86/hyperv/hv_init.c:hv_apicid_to_vp_index() { ... output = *this_cpu_ptr(hyperv_pcpu_output_arg); ... status = hv_do_hypercall(HVCALL_GET_VP_ID_FROM_APIC_ID, input, output); ... }
Quoted 5 lines
Since
this_cpu_ptr(NULL)resolves to the base of the per-CPU area, it dereferences a garbage pointer. This is then passed tohv_do_hypercall(), causing the hypervisor to overwrite random physical memory with the hypercall result, and eventually oopsing the kernel when output[0] is read.
This looks like a real bug. It should / will be fixed separately.
Wei
Wei Liu
maintainerRe: [RFC PATCH 03/12] drivers: hv: Reserve memory for VSM secure kernel during early boot
In reply to Thara GopinathQuoted 6 lines · Thara Gopinath
The VSM secure kernel runs in VTL1 and cannot use the normal Linux kernel memory allocators — its memory must be reserved before the general-purpose allocator takes ownership of the physical address space.
Add
hv_vsm_securekernel.cto handle this early boot reservation. The driver parses a new "securekernel=<size>[K|M|G][@<addr>]" kernel command
We should add a prefix to it, unless you tell me there is a plan to extend this to other virtualization solutions.
Wei
Wei Liu
maintainerRe: [RFC PATCH 04/12] firmware: efi: libstub: x86-stub: Enable VSM awareness in efi os indications variable
In reply to Thara GopinathQuoted 14 lines · Thara Gopinath
Set bit 0 of the Hyper-V private OsLoaderIndications EFI variable during
exit_boot()so the bootloader/firmware knows the OS intends to enable VTL1. Without this, VTL1 cannot be brought up from the Linux kernel.The support bit is first checked in OsLoaderIndicationsSupported, and the variable is only written when the VSM bit is not already set.
Signed-off-by Thara Gopinath <[email protected]>--- drivers/firmware/efi/libstub/x86-stub.c | 57 +++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
[...]
Quoted 26 lines
drivers/firmware/efi/libstub/x86-stub.c +26
-0+#ifdef CONFIG_HYPERV_VSM +static void efi_set_hv_os_indications (void ) +{ +efi_guid_t guid =HYPERV_PRIVATE_EFI_NAMESPACE_GUID ; +efi_status_t status; +unsigned long size; +u32 attr, val; + + size =sizeof (val); + status =get_efi_var (efi_HvPrivOsloaderIndicationsSupported_name, + &guid, &attr, &size, &val); +if (status !=EFI_SUCCESS ) { +efi_err ("Could not read Hyper-V OsloaderIndicationsSupported\n" ); +return ; + } + +if (!(val &HV_OSLOADER_INDICATION_VSM )) { +efi_info ("Hyper-V does not support VSM in OsloaderIndicationsSupported\n" ); +return ; + } + + size =sizeof (val); + status =get_efi_var (efi_HvPrivOsloaderIndications_name, &guid, &attr, &size, &val); +if (status !=EFI_SUCCESS ) { +efi_err ("Could not read Hyper-V OsLoaderIndications\n" ); +return ;
drivers/firmware/efi/libstub/x86-stub.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, · line 803
Quoted 10 lines
drivers/firmware/efi/libstub/x86-stub.c +10
-0+ } + +if (val &HV_OSLOADER_INDICATION_VSM ) { +efi_info ("VSM is already supported in OsLoaderIndications." ); +return ; + } + + val |=HV_OSLOADER_INDICATION_VSM ; + size =sizeof (val); + status =set_efi_var (efi_HvPrivOsloaderIndications_name, &guid, attr, size, &val);
I'm not familiar with the security model, so bear with me.
What happens if the VTL0 kernel doesn't use VTL1 at all? Does that become a security issue, that malware can use the VTL1 to hide itself?
Asking this because I think you will want to enable this in the generic kernel(s). Not all users have or want to package a secure kernel.
Wei
Quoted 23 lines
drivers/firmware/efi/libstub/x86-stub.c +10
-0+if (status !=EFI_SUCCESS ) + efi_err("Could not set Hyper-V OsLoaderIndications to indicate VSM support\n"); +} +#endif +static efi_status_t exit_boot (struct boot_params *boot_params,void *handle) {struct setup_data *e820ext =NULL ;@@ -768,6 +820,11 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)if (status !=EFI_SUCCESS )return status; +#ifdef CONFIG_HYPERV_VSM +/* Indicate to bootloader that we will be enabling VTL1 before exiting boot services */ +efi_set_hv_os_indications (); +#endif +/* Might as well exit boot services now */ status =efi_exit_boot_services (handle, &priv, exit_boot_func);if (status !=EFI_SUCCESS )Signature · 1 line
2.34.1
Wei Liu
maintainerRe: [RFC PATCH 06/12] drivers: hv: Add VSM boot driver and enable VTL1 at the partition level
In reply to Thara GopinathQuoted 20 lines · Thara Gopinath
Linux VBS (LVBS) uses Hyper-V's Virtual Secure Mode to run a small trusted kernel in VTL1 alongside the regular VTL0 kernel, so that security-sensitive state (e.g. hypervisor-enforced code integrity, credential isolation) can live behind a higher-privilege boundary that VTL0 compromise cannot cross. Bringing that up from Linux requires the VTL0 kernel to drive the VSM setup itself.
Add
drivers/hv/hv_vsm_boot.cas the entry point for that sequence. This first step handles partition-level VTL1 enable only:- Probe VSM / VP-register privileges and SynIC availability before doing anything.
- Pin init to the VTL0 boot CPU so VTL1 comes up on the same CPU (later patches rely on this).
- ReadHV_REGISTER_VSM_PARTITION_STATUS, and if VTL1 is not already enabled, issueHVCALL_ENABLE_PARTITION_VTLwith MBEC and confirm by re-reading the register.Signed-off-by Thara Gopinath <[email protected]>---
[...]
Quoted 5 lines
drivers/hv/hv_vsm_boot.c new file +5
-0+static int __init hv_vsm_boot_init (void ) +{ +cpumask_var_t mask; +unsigned int boot_cpu; +int ret;
drivers/hv/hv_vsm_boot.c line 151
Quoted 10 lines
drivers/hv/hv_vsm_boot.c new file +10
-0+ +if (!vsm_arch_has_vsm_access ()) +return 0 ; + +/* +* Copy the current cpu mask and pin rest of the running code to boot cpu. +* Important since we want boot cpu of VTL0 to be the boot cpu for VTL1. +* ToDo: Check if copying and restoring current->cpus_mask is enough +* ToDo: Verify the assumption that cpumask_first(cpu_online_mask) is +* the boot cpu
Can we get closures on these todos?
I see a bunch of todos in other patches, too. It would be good to close them as well.
Wei
Quoted 31 lines
drivers/hv/hv_vsm_boot.c new file +27
-0+*/ +if (!alloc_cpumask_var (&mask,GFP_KERNEL )) +panic ("Could not allocate cpumask" ); + +cpumask_copy (mask, ¤t->cpus_mask); + boot_cpu =cpumask_first (cpu_online_mask); +set_cpus_allowed_ptr (current,cpumask_of (boot_cpu)); + + ret =hv_vsm_bootstrap_vtl (); +/* +* At this point VTL0 has already advertised VSM support to the +* bootloader/firmware via the Hyper-V OsLoaderIndications EFI +* variable (see the x86-stub change). That signals the platform +* that a trusted VTL1 will be brought up. If we fail to actually +* set VTL1 up here, the partition is left in a state where an +* attacker could race to configure VTL1 themselves and gain a +* higher-privilege foothold than VTL0. Panic rather than continue +* running with that exposure. +*/ +if (ret) +panic ("VTL1 boot failure caused kernel panic; consult log for more details.\n" ); + +set_cpus_allowed_ptr (current, mask); +free_cpumask_var (mask); +return ret; +} +device_initcall (hv_vsm_boot_init);Signature · 1 line
2.34.1
Wei Liu
maintainerRe: [RFC PATCH 07/12] drivers: hv: hv_vsm_boot: load secure kernel image from firmware
In reply to Thara GopinathQuoted 11 lines · Thara Gopinath
LVBS bring-up requires loading a secure kernel image into VTL1 before starting it. Add the VTL0-side loader that stages the image in the memory region reserved by
hv_vsm_securekernel, in preparation for the VTL1 bring-up.The image is a 64-bit ELF fetched via
request_firmware("vsm_sk"). It is expected to ship inside the signed UKI/initramfs so it is authenticated end-to-end via Secure Boot before the loader consumes it; sourcing it from an unauthenticated location would break the LVBS trust model.
Quoted 7 lines
The loader validates the ELF header, stages the
PT_LOADsegments into the reserved region and records the entry point as a physical address for use at VTL1 start time.If VSM support has been advertised to the hypervisor but no secure kernel region was reserved on the command line, panic: LVBS bring-up is committed at this point and there is no safe way to continue.
This conflicts with the memory reservation patch, in which there is an automatic allocation when no kernel command line is specified.
Quoted 10 lines
Signed-off-by Stanislav Kinsburskii <[email protected]>Signed-off-by Thara Gopinath <[email protected]>--- drivers/hv/hv_vsm.h | 17 ++++ drivers/hv/hv_vsm_boot.c | 201 ++++++++++++++++++++++++++++++++++++++- include/hyperv/vsm.h | 21 ++++ 3 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 drivers/hv/hv_vsm.h create mode 100644 include/hyperv/vsm.h
[...]
drivers/hv/hv_vsm_boot.c static int __init hv_vsm_bootstrap_vtl(void) · line 316
Quoted 12 lines
drivers/hv/hv_vsm_boot.c +12
-0+ +static void __init hv_vsm_get_sk_mem (void ) +{ +/* +* The reserved secure kernel region is mandatory once VSM support has +* been advertised. Without it we cannot load the secure kernel and +* bringing up VTL1 is impossible, so fail hard rather than continuing +* in an unusable state. +*/ +if (!sk_res.start) +panic ("No memory reserved in cmdline for secure kernel" ); +
This log line is wrong.
Wei
Wei Liu
maintainerRe: [RFC PATCH 06/12] drivers: hv: Add VSM boot driver and enable VTL1 at the partition level
In reply to Thara GopinathQuoted 20 lines · Thara Gopinath
Linux VBS (LVBS) uses Hyper-V's Virtual Secure Mode to run a small trusted kernel in VTL1 alongside the regular VTL0 kernel, so that security-sensitive state (e.g. hypervisor-enforced code integrity, credential isolation) can live behind a higher-privilege boundary that VTL0 compromise cannot cross. Bringing that up from Linux requires the VTL0 kernel to drive the VSM setup itself.
Add
drivers/hv/hv_vsm_boot.cas the entry point for that sequence. This first step handles partition-level VTL1 enable only:- Probe VSM / VP-register privileges and SynIC availability before doing anything.
- Pin init to the VTL0 boot CPU so VTL1 comes up on the same CPU (later patches rely on this).
- ReadHV_REGISTER_VSM_PARTITION_STATUS, and if VTL1 is not already enabled, issueHVCALL_ENABLE_PARTITION_VTLwith MBEC and confirm by re-reading the register.Signed-off-by Thara Gopinath <[email protected]>---
[...]
Quoted 5 lines
drivers/hv/hv_vsm_boot.c new file +5
-0+cpumask_copy (mask, ¤t->cpus_mask); + boot_cpu =cpumask_first (cpu_online_mask); +set_cpus_allowed_ptr (current,cpumask_of (boot_cpu)); + + ret =hv_vsm_bootstrap_vtl ();
drivers/hv/hv_vsm_boot.c line 170
Quoted 10 lines
drivers/hv/hv_vsm_boot.c new file +10
-0+/* +* At this point VTL0 has already advertised VSM support to the +* bootloader/firmware via the Hyper-V OsLoaderIndications EFI +* variable (see the x86-stub change). That signals the platform +* that a trusted VTL1 will be brought up. If we fail to actually +* set VTL1 up here, the partition is left in a state where an +* attacker could race to configure VTL1 themselves and gain a +* higher-privilege foothold than VTL0. Panic rather than continue +* running with that exposure. +*/
Okay, I think this answers my question in the previous patch -- if VTL1 is not used by us, that's a security problem.
It is unclear to me, if this code is enabled, how a generic kernel can work without a secure kernel. There should be a way to configure the system such that not enabling VTL1 is okay. Is there any pre-EFI command line parsing we can do?
Wei
Quoted 12 lines
drivers/hv/hv_vsm_boot.c new file +8
-0+if (ret) +panic ("VTL1 boot failure caused kernel panic; consult log for more details.\n" ); + +set_cpus_allowed_ptr (current, mask); +free_cpumask_var (mask); +return ret; +} +device_initcall (hv_vsm_boot_init);Signature · 1 line
2.34.1
Re: [RFC PATCH 03/12] drivers: hv: Reserve memory for VSM secure kernel during early boot
In reply to Wei LiuQuoted 10 lines · Wei Liu
Quoted 6 lines · Thara Gopinath
The VSM secure kernel runs in VTL1 and cannot use the normal Linux kernel memory allocators — its memory must be reserved before the general-purpose allocator takes ownership of the physical address space.
Add
hv_vsm_securekernel.cto handle this early boot reservation. The driver parses a new "securekernel=<size>[K|M|G][@<addr>]" kernel commandWe should add a prefix to it, unless you tell me there is a plan to extend this to other virtualization solutions.
Thanks for the reviews Wei.. Yes I will add a prefix hv_ and we can remove it if/when this gets extended to other virtualization solutions.
Warm Regards
Thara
Quoted 2 lines
Wei
Re: [RFC PATCH 04/12] firmware: efi: libstub: x86-stub: Enable VSM awareness in efi os indications variable
In reply to Wei LiuQuoted 55 lines · Wei Liu
Quoted 14 lines · Thara Gopinath
Set bit 0 of the Hyper-V private OsLoaderIndications EFI variable during
exit_boot()so the bootloader/firmware knows the OS intends to enable VTL1. Without this, VTL1 cannot be brought up from the Linux kernel.The support bit is first checked in OsLoaderIndicationsSupported, and the variable is only written when the VSM bit is not already set.
Signed-off-by Thara Gopinath <[email protected]>--- drivers/firmware/efi/libstub/x86-stub.c | 57 +++++++++++++++++++++++++ 1 file changed, 57 insertions(+)[...]
Quoted 26 lines
drivers/firmware/efi/libstub/x86-stub.c +26
-0+#ifdef CONFIG_HYPERV_VSM +static void efi_set_hv_os_indications (void ) +{ +efi_guid_t guid =HYPERV_PRIVATE_EFI_NAMESPACE_GUID ; +efi_status_t status; +unsigned long size; +u32 attr, val; + + size =sizeof (val); + status =get_efi_var (efi_HvPrivOsloaderIndicationsSupported_name, + &guid, &attr, &size, &val); +if (status !=EFI_SUCCESS ) { +efi_err ("Could not read Hyper-V OsloaderIndicationsSupported\n" ); +return ; + } + +if (!(val &HV_OSLOADER_INDICATION_VSM )) { +efi_info ("Hyper-V does not support VSM in OsloaderIndicationsSupported\n" ); +return ; + } + + size =sizeof (val); + status =get_efi_var (efi_HvPrivOsloaderIndications_name, &guid, &attr, &size, &val); +if (status !=EFI_SUCCESS ) { +efi_err ("Could not read Hyper-V OsLoaderIndications\n" ); +return ;drivers/firmware/efi/libstub/x86-stub.c static efi_status_t exit_boot_func(struct efi_boot_memmap *map, · line 803
Quoted 10 lines
drivers/firmware/efi/libstub/x86-stub.c +10
-0+ } + +if (val &HV_OSLOADER_INDICATION_VSM ) { +efi_info ("VSM is already supported in OsLoaderIndications." ); +return ; + } + + val |=HV_OSLOADER_INDICATION_VSM ; + size =sizeof (val); + status =set_efi_var (efi_HvPrivOsloaderIndications_name, &guid, attr, size, &val);I'm not familiar with the security model, so bear with me.
Quoted 5 lines
What happens if the VTL0 kernel doesn't use VTL1 at all? Does that become a security issue, that malware can use the VTL1 to hide itself?
Asking this because I think you will want to enable this in the generic kernel(s). Not all users have or want to package a secure kernel.
Yes you are right. If we do this and a secure kernel is not loaded, it is a security hole. Which is why this is bound by the same config option CONFIG_HYPERV_VSM that does the secure kernel boot and in that path any error / inability to load and establish VTL1 is treated as a serious error and we panic. Generic kernels should not enable this option at all. The CONFIG_HYPERV_VSM should be enabled only if it is known that VTL1 environment can be established. Otherwise the system will not boot and will panic.
Warm Regards
Thara
Quoted 26 lines
Wei
Quoted 23 lines
drivers/firmware/efi/libstub/x86-stub.c +10
-0+if (status !=EFI_SUCCESS ) + efi_err("Could not set Hyper-V OsLoaderIndications to indicate VSM support\n"); +} +#endif +static efi_status_t exit_boot (struct boot_params *boot_params,void *handle) {struct setup_data *e820ext =NULL ;@@ -768,6 +820,11 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)if (status !=EFI_SUCCESS )return status; +#ifdef CONFIG_HYPERV_VSM +/* Indicate to bootloader that we will be enabling VTL1 before exiting boot services */ +efi_set_hv_os_indications (); +#endif +/* Might as well exit boot services now */ status =efi_exit_boot_services (handle, &priv, exit_boot_func);if (status !=EFI_SUCCESS )Signature · 1 line
2.34.1
Re: [RFC PATCH 06/12] drivers: hv: Add VSM boot driver and enable VTL1 at the partition level
In reply to Wei LiuQuoted 40 lines · Wei Liu
Quoted 20 lines · Thara Gopinath
Linux VBS (LVBS) uses Hyper-V's Virtual Secure Mode to run a small trusted kernel in VTL1 alongside the regular VTL0 kernel, so that security-sensitive state (e.g. hypervisor-enforced code integrity, credential isolation) can live behind a higher-privilege boundary that VTL0 compromise cannot cross. Bringing that up from Linux requires the VTL0 kernel to drive the VSM setup itself.
Add
drivers/hv/hv_vsm_boot.cas the entry point for that sequence. This first step handles partition-level VTL1 enable only:- Probe VSM / VP-register privileges and SynIC availability before doing anything.
- Pin init to the VTL0 boot CPU so VTL1 comes up on the same CPU (later patches rely on this).
- ReadHV_REGISTER_VSM_PARTITION_STATUS, and if VTL1 is not already enabled, issueHVCALL_ENABLE_PARTITION_VTLwith MBEC and confirm by re-reading the register.Signed-off-by Thara Gopinath <[email protected]>---
[...]
Quoted 5 lines
drivers/hv/hv_vsm_boot.c new file +5
-0+static int __init hv_vsm_boot_init (void ) +{ +cpumask_var_t mask; +unsigned int boot_cpu; +int ret;drivers/hv/hv_vsm_boot.c line 151
Quoted 10 lines
drivers/hv/hv_vsm_boot.c new file +10
-0+ +if (!vsm_arch_has_vsm_access ()) +return 0 ; + +/* +* Copy the current cpu mask and pin rest of the running code to boot cpu. +* Important since we want boot cpu of VTL0 to be the boot cpu for VTL1. +* ToDo: Check if copying and restoring current->cpus_mask is enough +* ToDo: Verify the assumption that cpumask_first(cpu_online_mask) is +* the boot cpu Can we get closures on these todos?
Quoted 2 lines
I see a bunch of todos in other patches, too. It would be good to close them as well.
Sorry about those. Most of them are remnants from the when the dev work was started and are closed or taken care of. I will ensure that these are fixed and the comments are edited properly in the next revision.
Warm Regards
Thara
Quoted 2 lines
Wei
Re: [RFC PATCH 07/12] drivers: hv: hv_vsm_boot: load secure kernel image from firmware
In reply to Wei LiuQuoted 16 lines · Wei Liu
Quoted 15 lines · Thara Gopinath
LVBS bring-up requires loading a secure kernel image into VTL1 before starting it. Add the VTL0-side loader that stages the image in the memory region reserved by
hv_vsm_securekernel, in preparation for the VTL1 bring-up.The image is a 64-bit ELF fetched via
request_firmware("vsm_sk"). It is expected to ship inside the signed UKI/initramfs so it is authenticated end-to-end via Secure Boot before the loader consumes it; sourcing it from an unauthenticated location would break the LVBS trust model.The loader validates the ELF header, stages the
PT_LOADsegments into the reserved region and records the entry point as a physical address for use at VTL1 start time.
Quoted 6 lines
Quoted 3 lines
If VSM support has been advertised to the hypervisor but no secure kernel region was reserved on the command line, panic: LVBS bring-up is committed at this point and there is no safe way to continue.
This conflicts with the memory reservation patch, in which there is an automatic allocation when no kernel command line is specified.
Ah no.. So securekernel= has to be specified in the command line for __setup() to be invoked . It can be left blank without parameters like securekernel= or with parameters like securekernel=256M@0x80000000. The reservation logic will take care of reserving the correct memory if there are no parameters but if there is no command line specified __setup will not be called. The other way of solving this and invoking the reservations unconditionally will be to call it from setup_arch like how reserve_crashkernel is invoked. I am not sure if we want to do that now ?? What do you think ?
But I will reword this and state that the panic happens if there is no securekernel memory allocated (either because securekernel= was not specified or because memory reservation itself failed)
Quoted 12 lines
Quoted 11 lines
Signed-off-by Stanislav Kinsburskii <[email protected]>Signed-off-by Thara Gopinath <[email protected]>--- drivers/hv/hv_vsm.h | 17 ++++ drivers/hv/hv_vsm_boot.c | 201 ++++++++++++++++++++++++++++++++++++++- include/hyperv/vsm.h | 21 ++++ 3 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 drivers/hv/hv_vsm.h create mode 100644 include/hyperv/vsm.h[...]
Quoted 14 lines
drivers/hv/hv_vsm_boot.c static int __init hv_vsm_bootstrap_vtl(void) · line 316
Quoted 12 lines
drivers/hv/hv_vsm_boot.c +12
-0+ +static void __init hv_vsm_get_sk_mem (void ) +{ +/* +* The reserved secure kernel region is mandatory once VSM support has +* been advertised. Without it we cannot load the secure kernel and +* bringing up VTL1 is impossible, so fail hard rather than continuing +* in an unusable state. +*/ +if (!sk_res.start) +panic ("No memory reserved in cmdline for secure kernel" ); +This log line is wrong.
I will fix this
Warm Regards
Thara
Quoted 2 lines
Wei
Wei Liu
maintainerRe: [RFC PATCH 07/12] drivers: hv: hv_vsm_boot: load secure kernel image from firmware
Quoted 26 lines · Thara Gopinath
Quoted 16 lines · Wei Liu
Quoted 15 lines · Thara Gopinath
LVBS bring-up requires loading a secure kernel image into VTL1 before starting it. Add the VTL0-side loader that stages the image in the memory region reserved by
hv_vsm_securekernel, in preparation for the VTL1 bring-up.The image is a 64-bit ELF fetched via
request_firmware("vsm_sk"). It is expected to ship inside the signed UKI/initramfs so it is authenticated end-to-end via Secure Boot before the loader consumes it; sourcing it from an unauthenticated location would break the LVBS trust model.The loader validates the ELF header, stages the
PT_LOADsegments into the reserved region and records the entry point as a physical address for use at VTL1 start time.Quoted 6 lines
Quoted 3 lines
If VSM support has been advertised to the hypervisor but no secure kernel region was reserved on the command line, panic: LVBS bring-up is committed at this point and there is no safe way to continue.
This conflicts with the memory reservation patch, in which there is an automatic allocation when no kernel command line is specified.
Quoted 9 lines
Ah no.. So securekernel= has to be specified in the command line for
__setup()to be invoked . It can be left blank without parameters like securekernel= or with parameters like securekernel=256M@0x80000000. The reservation logic will take care of reserving the correct memory if there are no parameters but if there is no command line specified__setupwill not be called. The other way of solving this and invoking the reservations unconditionally will be to call it fromsetup_archlike howreserve_crashkernelis invoked. I am not sure if we want to do that now ?? What do you think ?
I see. No need to do that now. Let's see if others have opinions.
Wei
Quoted 38 lines
But I will reword this and state that the panic happens if there is no securekernel memory allocated (either because securekernel= was not specified or because memory reservation itself failed)
Quoted 12 lines
Quoted 11 lines
Signed-off-by Stanislav Kinsburskii <[email protected]>Signed-off-by Thara Gopinath <[email protected]>--- drivers/hv/hv_vsm.h | 17 ++++ drivers/hv/hv_vsm_boot.c | 201 ++++++++++++++++++++++++++++++++++++++- include/hyperv/vsm.h | 21 ++++ 3 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 drivers/hv/hv_vsm.h create mode 100644 include/hyperv/vsm.h[...]
Quoted 14 lines
drivers/hv/hv_vsm_boot.c static int __init hv_vsm_bootstrap_vtl(void) · line 316
Quoted 12 lines
drivers/hv/hv_vsm_boot.c +12
-0+ +static void __init hv_vsm_get_sk_mem (void ) +{ +/* +* The reserved secure kernel region is mandatory once VSM support has +* been advertised. Without it we cannot load the secure kernel and +* bringing up VTL1 is impossible, so fail hard rather than continuing +* in an unusable state. +*/ +if (!sk_res.start) +panic ("No memory reserved in cmdline for secure kernel" ); +This log line is wrong.
I will fix this
Warm Regards
TharaQuoted 2 lines
Wei
Re: [RFC PATCH 06/12] drivers: hv: Add VSM boot driver and enable VTL1 at the partition level
In reply to Wei LiuQuoted 41 lines · Wei Liu
Quoted 20 lines · Thara Gopinath
Linux VBS (LVBS) uses Hyper-V's Virtual Secure Mode to run a small trusted kernel in VTL1 alongside the regular VTL0 kernel, so that security-sensitive state (e.g. hypervisor-enforced code integrity, credential isolation) can live behind a higher-privilege boundary that VTL0 compromise cannot cross. Bringing that up from Linux requires the VTL0 kernel to drive the VSM setup itself.
Add
drivers/hv/hv_vsm_boot.cas the entry point for that sequence. This first step handles partition-level VTL1 enable only:- Probe VSM / VP-register privileges and SynIC availability before doing anything.
- Pin init to the VTL0 boot CPU so VTL1 comes up on the same CPU (later patches rely on this).
- ReadHV_REGISTER_VSM_PARTITION_STATUS, and if VTL1 is not already enabled, issueHVCALL_ENABLE_PARTITION_VTLwith MBEC and confirm by re-reading the register.Signed-off-by Thara Gopinath <[email protected]>---
[...]
Quoted 5 lines
drivers/hv/hv_vsm_boot.c new file +5
-0+cpumask_copy (mask, ¤t->cpus_mask); + boot_cpu =cpumask_first (cpu_online_mask); +set_cpus_allowed_ptr (current,cpumask_of (boot_cpu)); + + ret =hv_vsm_bootstrap_vtl ();drivers/hv/hv_vsm_boot.c line 170
Quoted 10 lines
drivers/hv/hv_vsm_boot.c new file +10
-0+/* +* At this point VTL0 has already advertised VSM support to the +* bootloader/firmware via the Hyper-V OsLoaderIndications EFI +* variable (see the x86-stub change). That signals the platform +* that a trusted VTL1 will be brought up. If we fail to actually +* set VTL1 up here, the partition is left in a state where an +* attacker could race to configure VTL1 themselves and gain a +* higher-privilege foothold than VTL0. Panic rather than continue +* running with that exposure. +*/ Okay, I think this answers my question in the previous patch -- if VTL1 is not used by us, that's a security problem.
Quoted 4 lines
It is unclear to me, if this code is enabled, how a generic kernel can work without a secure kernel. There should be a way to configure the system such that not enabling VTL1 is okay. Is there any pre-EFI command line parsing we can do?
Yes, we can do a pre-efi-exit-boot-service command line parsing and not set the OsLoaderIndications EFI variable asking HYPER-V to allow VTL1 setup. We can rename the securekernel command line option to mshv_securekernel and repurpose it for this as well. Basically if CONFIG_HYPER_VSM is enabled but if mshv_securekernel command line is not set VTL1 support will not be enabled and kernel will boot. I did quickly prototype this and it is doable.
Having said that generic kernel should not enable CONFIG_HYPERV_VSM at all. I can update the KConfig to state the same as well.
Warm Regards
Thara
Quoted 16 lines
Wei
Quoted 12 lines
drivers/hv/hv_vsm_boot.c new file +8
-0+if (ret) +panic ("VTL1 boot failure caused kernel panic; consult log for more details.\n" ); + +set_cpus_allowed_ptr (current, mask); +free_cpumask_var (mask); +return ret; +} +device_initcall (hv_vsm_boot_init);Signature · 1 line
2.34.1