From bebf710f8a7d4e3e85d2d46d60cfc144f7c283a4 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 17 Dec 2025 09:46:23 +0100 Subject: [PATCH] F #7331: Add support for memory encryption This commit adds support for memory encryption. It includes: * A new VM template attribute to set memory encryption * A host probe to gather the memory extensions available (SEV, SEV-ES...) * When selected AUTOMATIC_REQUIREMENTS include memory encryption as a requirement * Domain XML generation has been updated to include memory encryption attributes * A kvm.conf attribute MEMORY_TUNE_OFFSET to add an additional memory offset for encryption. Author: MarioRobres Author: Daniel Clavijo Coca --- include/LibVirtDriver.h | 6 ++ include/VirtualMachine.h | 25 ++++++++ install.sh | 1 + share/doc/xsd/vm.xsd | 7 ++ .../dummy-probes.d/host/system/system.rb | 5 ++ .../host/system/memory_encryption.rb | 53 +++++++++++++++ src/vm/VirtualMachine.cc | 43 +++++++++++++ src/vm/VirtualMachineParser.cc | 64 ++++++++++++++++++- src/vmm/LibVirtDriverKVM.cc | 48 ++++++++++++++ src/vmm_mad/exec/x86_64/vmm_exec_kvm.conf | 3 + src/vmm_mad/remotes/kvm/deploy | 1 - 11 files changed, 253 insertions(+), 3 deletions(-) create mode 100755 src/im_mad/remotes/kvm-probes.d/host/system/memory_encryption.rb diff --git a/include/LibVirtDriver.h b/include/LibVirtDriver.h index b562af841a..a57cc675da 100644 --- a/include/LibVirtDriver.h +++ b/include/LibVirtDriver.h @@ -56,6 +56,12 @@ private: static constexpr auto XML_DOMAIN_RNG_PATH = "/schemas/libvirt/domain.rng"; + static constexpr uint32_t POLICY_DEBUG_DISALLOWED = (1u << 0); + + static constexpr uint32_t POLICY_KEY_SHARING_DISALLOWED = (1u << 1); + + static constexpr uint32_t POLICY_SEV_ES_REQUIRED = (1u << 2); + int deployment_description(const VirtualMachine * vm, const std::string& fn) const override { diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 886438d727..6000142590 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -2201,6 +2201,31 @@ private: */ void parse_well_known_attributes(); + /** + * Parse a vector attribute with one or more required keys + * @param name Name of the attribute to parse + * @param required_keys List of required keys that must be present and non-empty + * @param error Returns the error reason, if any + * @return 0 on success + */ + int parse_vector_attribute(const std::string& name, + const std::vector& required_keys, + std::string& error); + + /** + * Parse the "TPM" attribute of the template + * @param error_str Returns the error reason, if any + * @return 0 on success + */ + int parse_tpm(std::string& error_str); + + /** + * Parse the "MEMORY_ENCRYPTION" attribute of the template + * @param error_str Returns the error reason, if any + * @return 0 on success + */ + int parse_memory_encryption(std::string& error_str); + // ------------------------------------------------------------------------- // Context related functions // ------------------------------------------------------------------------- diff --git a/install.sh b/install.sh index 4bd3311408..cd02e775f6 100755 --- a/install.sh +++ b/install.sh @@ -1081,6 +1081,7 @@ IM_PROBES_KVM_HOST_SYSTEM_FILES="\ src/im_mad/remotes/kvm-probes.d/host/system/clean_db.rb \ src/im_mad/remotes/kvm-probes.d/host/system/wild_vm.rb \ src/im_mad/remotes/kvm-probes.d/host/system/pci.rb \ + src/im_mad/remotes/kvm-probes.d/host/system/memory_encryption.rb \ src/im_mad/remotes/kvm-probes.d/host/system/version.sh" IM_PROBES_KVM_VM_MONITOR_FILES="\ diff --git a/share/doc/xsd/vm.xsd b/share/doc/xsd/vm.xsd index c749cea523..b6470ad169 100644 --- a/share/doc/xsd/vm.xsd +++ b/share/doc/xsd/vm.xsd @@ -182,6 +182,13 @@ + + + + + + + diff --git a/src/im_mad/remotes/dummy-probes.d/host/system/system.rb b/src/im_mad/remotes/dummy-probes.d/host/system/system.rb index a9d00c6de5..0d6b7176dc 100755 --- a/src/im_mad/remotes/dummy-probes.d/host/system/system.rb +++ b/src/im_mad/remotes/dummy-probes.d/host/system/system.rb @@ -134,9 +134,14 @@ def hostname "HOSTNAME=#{ARGV[2]}\n" end +def memory_encryption + "MEMORY_ENCRYPTION=\"SEV\"\n" +end + result = '' result << system +result << memory_encryption result << hostname result << topology(2, 8, [2048, 1048576], 4, 16777216) result << pci diff --git a/src/im_mad/remotes/kvm-probes.d/host/system/memory_encryption.rb b/src/im_mad/remotes/kvm-probes.d/host/system/memory_encryption.rb new file mode 100755 index 0000000000..b5ecf0c6fc --- /dev/null +++ b/src/im_mad/remotes/kvm-probes.d/host/system/memory_encryption.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'open3' +require 'rexml/document' + +cmd = 'virsh -c qemu:///system domcapabilities' +domcapabilities, _e, s = Open3.capture3(cmd) + +exit 0 unless s.success? + +begin + + domcap_xml = REXML::Document.new(domcapabilities) + domcap_xml = domcap_xml.root + + features = REXML::XPath.first(domcap_xml, '/domainCapabilities/features') + + memory_encryption = 'NONE' + + if (sev = features.elements['sev']) + if sev['supported'] == 'yes' + if (snp = sev.elements['sevsnp']) && snp['supported'] == 'yes' + memory_encryption = 'SEV-SNP' + else + es_guests = sev.elements['maxESGuests'].text + memory_encryption = es_guests == '0' ? 'SEV' : 'SEV-ES' + end + end + elsif (tdx = features.elements['tdx']) + memory_encryption = 'TDX' if tdx['supported'] == 'yes' + end +rescue StandardError + puts "MEMORY_ENCRYPTION=NONE" + exit 0 +end + +puts "MEMORY_ENCRYPTION=#{memory_encryption}" diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 331baf5ed4..1b91e2de48 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -1063,6 +1063,22 @@ int VirtualMachine::insert(SqlDB * db, string& error_str) goto error_os; } + // ------------------------------------------------------------------------ + // Parse TPM attribute + // ------------------------------------------------------------------------ + if (parse_tpm(error_str) != 0) + { + goto error_common; + } + + // ------------------------------------------------------------------------ + // Parse MEMORY_ENCRYPTION attribute + // ------------------------------------------------------------------------ + if (parse_memory_encryption(error_str) != 0) + { + goto error_common; + } + // ------------------------------------------------------------------------ // Check the CPU Model attribute // ------------------------------------------------------------------------ @@ -1765,6 +1781,33 @@ int VirtualMachine::automatic_requirements(set& cluster_ids, } } + const VectorAttribute * memory_encryption = obj_template->get("MEMORY_ENCRYPTION"); + + if (memory_encryption != nullptr) + { + std::string type = memory_encryption->vector_value("TYPE"); + + if (!type.empty()) + { + std::string requirement; + + if (type == "SEV") + { + requirement = "SEV*"; + } + else if (type == "SEV-ES") + { + requirement = "SEV-*"; + } + else // SEV-SNP, TDX + { + requirement = type; + } + + oss << " & (MEMORY_ENCRYPTION = \"" << requirement << "\")"; + } + } + obj_template->add("AUTOMATIC_REQUIREMENTS", oss.str()); oss.str(""); diff --git a/src/vm/VirtualMachineParser.cc b/src/vm/VirtualMachineParser.cc index 7c39cb7cc1..fe246d3b06 100644 --- a/src/vm/VirtualMachineParser.cc +++ b/src/vm/VirtualMachineParser.cc @@ -657,8 +657,7 @@ void VirtualMachine::parse_well_known_attributes() "TOPOLOGY", "NUMA_NODE", "HYPERV_OPTIONS", - "SPICE_OPTIONS", - "TPM" + "SPICE_OPTIONS" }; for (auto it = names.begin(); it != names.end() ; ++it) @@ -816,6 +815,67 @@ int VirtualMachine::parse_cpu_model(Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int VirtualMachine::parse_vector_attribute(const std::string& name, + const std::vector& required_keys, + std::string& error) +{ + std::vector v_attr; + bool fail = false; + + user_obj_template->remove(name, v_attr); + + for (auto& attr_ptr : v_attr) + { + for (const auto& key : required_keys) + { + if (attr_ptr->vector_value(key).empty()) + { + error = name + " attribute is missing required key: " + key; + fail = true; + break; + } + } + + if ( fail ) break; + } + + if (fail) + { + for (VectorAttribute* p : v_attr) + { + delete p; + } + + return -1; + } + + for (VectorAttribute* attr_ptr : v_attr) + { + obj_template->set(attr_ptr); + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualMachine::parse_tpm(std::string& error) +{ + return parse_vector_attribute("TPM", {"MODEL"}, error); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualMachine::parse_memory_encryption(std::string& error) +{ + return parse_vector_attribute("MEMORY_ENCRYPTION", {"TYPE"}, error); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int VirtualMachine::parse_topology(Template * tmpl, std::string &error) { /** diff --git a/src/vmm/LibVirtDriverKVM.cc b/src/vmm/LibVirtDriverKVM.cc index a58c502163..aa26e9271a 100644 --- a/src/vmm/LibVirtDriverKVM.cc +++ b/src/vmm/LibVirtDriverKVM.cc @@ -655,6 +655,8 @@ int LibVirtDriver::deployment_description_kvm( string virtio_scsi_queues; string hyperv_options; + vector memory_encryption; + vector raw; string default_raw; string data; @@ -2442,6 +2444,52 @@ int LibVirtDriver::deployment_description_kvm( << "\t" << endl; } + // ------------------------------------------------------------------------ + // Memory encryption + // ------------------------------------------------------------------------ + + num = vm->get_template_attribute("MEMORY_ENCRYPTION", memory_encryption); + + for (int i = 0; i < num; i++) + { + type = memory_encryption[i]->vector_value("TYPE"); + + // AMD SEV/SEV-ES + if (type.find("SEV") != string::npos && !one_util::icasecmp(type, "SEV-SNP")) + { + // https://libvirt.org/formatdomain.html#launch-security. + // Setup policy bitmask, debug disallowed, key_sharing_disallowed + uint32_t policy = POLICY_DEBUG_DISALLOWED | POLICY_KEY_SHARING_DISALLOWED; + + if (one_util::icasecmp(type, "SEV-ES")) + { + policy |= POLICY_SEV_ES_REQUIRED; + } + + file << "\t" << endl + << "\t\t0x" + << hex << setw(8) << setfill('0') << policy + << "" << endl + << "\t" << endl; + + // Prevent qemu failing to allocate memory + int mem_tune_offset = 256; + + get_attribute(vm, nullptr, nullptr, "MEMORY_TUNE_OFFSET", mem_tune_offset); + + file << "\t\n" + << "\t\t" + << (memory + mem_tune_offset) + << "\n" + << "\t\n"; + } + else // SEV-SNP, Intel TDX... + { + vm->log("VMM", Log::WARNING, + "Memory encryption " + type + " is not supported or undefined, ignored."); + } + } + // ------------------------------------------------------------------------ // Raw KVM attributes // ------------------------------------------------------------------------ diff --git a/src/vmm_mad/exec/x86_64/vmm_exec_kvm.conf b/src/vmm_mad/exec/x86_64/vmm_exec_kvm.conf index d725152e46..80a6039bd8 100644 --- a/src/vmm_mad/exec/x86_64/vmm_exec_kvm.conf +++ b/src/vmm_mad/exec/x86_64/vmm_exec_kvm.conf @@ -76,6 +76,7 @@ # * HYPERV_OPTIONS: options used for FEATURES = [ HYPERV = yes ] # * HYPERV_TIMERS: timers added when HYPERV is set to yes in FEATURES # * SPICE_OPTIONS +# * MEMORY_TUNE_OFFSET: Offset in MiB to add to VM memory for memory encryption # # The following attributes can be overridden at Cluster and Host level, # but not within individual VM configuration: @@ -174,6 +175,8 @@ SPICE_OPTIONS = " " +MEMORY_TUNE_OFFSET = 256 + Q35_ROOT_PORTS = 16 Q35_NUMA_PCIE = "YES" diff --git a/src/vmm_mad/remotes/kvm/deploy b/src/vmm_mad/remotes/kvm/deploy index 6bedf8b783..9a38f60f8c 100755 --- a/src/vmm_mad/remotes/kvm/deploy +++ b/src/vmm_mad/remotes/kvm/deploy @@ -107,7 +107,6 @@ if [ -d "$DEP_FILE_LOCATION/tpm" ]; then (sudo -l | grep -q vtpm_setup) && sudo /var/tmp/one/vtpm_setup recover "$DOM_UUID" "$DEP_FILE_LOCATION" fi - DATA=`virsh --connect $LIBVIRT_URI create $DEP_FILE` if [ "x$?" != "x0" ]; then