From b6d1f2c3a9cf43299e162caaffdd019bc7c4b1ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20L=C3=B3pez?= Date: Mon, 16 Oct 2023 12:53:01 +0200 Subject: [PATCH] fw_meta: check that firmware memory regions do not overlap with kernel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Check that the memory regions provided by the SEV firmware metadata do not overlap with kernel memory before validating them. Not doing this could lead to the SVSM doubly validating its own memory, which allows for remapping attacks. Fixes: #114 Signed-off-by: Carlos López --- src/fw_meta.rs | 13 ++++++++++++- src/svsm.rs | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/fw_meta.rs b/src/fw_meta.rs index acb69b3ef..39482fe1f 100644 --- a/src/fw_meta.rs +++ b/src/fw_meta.rs @@ -9,6 +9,7 @@ extern crate alloc; use crate::address::PhysAddr; use crate::cpu::percpu::this_cpu_mut; use crate::error::SvsmError; +use crate::kernel_launch::KernelLaunchInfo; use crate::mm::PerCPUPageMappingGuard; use crate::mm::SIZE_1G; use crate::sev::ghcb::PageStateChangeOp; @@ -414,7 +415,10 @@ fn validate_fw_memory_vec(regions: Vec>) -> Result<(), Sv validate_fw_memory_vec(next_vec) } -pub fn validate_fw_memory(fw_meta: &SevFWMetaData) -> Result<(), SvsmError> { +pub fn validate_fw_memory( + fw_meta: &SevFWMetaData, + launch_info: &KernelLaunchInfo, +) -> Result<(), SvsmError> { // Initalize vector with regions from the FW let mut regions = fw_meta.valid_mem.clone(); @@ -436,6 +440,13 @@ pub fn validate_fw_memory(fw_meta: &SevFWMetaData) -> Result<(), SvsmError> { // Sort regions by base address regions.sort_unstable_by_key(|a| a.start()); + let kernel_region = launch_info.kernel_region(); + for region in regions.iter() { + if region.overlap(&kernel_region) { + panic!("FwMeta region ovelaps with kernel"); + } + } + validate_fw_memory_vec(regions) } diff --git a/src/svsm.rs b/src/svsm.rs index 3d7b163e9..138658f13 100644 --- a/src/svsm.rs +++ b/src/svsm.rs @@ -442,7 +442,7 @@ pub extern "C" fn svsm_main() { print_fw_meta(&fw_meta); - if let Err(e) = validate_fw_memory(&fw_meta) { + if let Err(e) = validate_fw_memory(&fw_meta, &LAUNCH_INFO) { panic!("Failed to validate firmware memory: {:#?}", e); }