2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-19 17:02:15 +00:00

fix(kvm): a Secure Boot domain can start on a firmware with no enrolled keys

guest_os stated <loader secure='yes'/> alone for a node with
vm.othersettings firmware:efi-secureboot. libvirt reads that element as
the secure-boot feature, and a firmware with secure-boot and an empty key
store boots an unsigned binary. The two x86_64 descriptors of an EL 10
host name the same OVMF_CODE.secboot.fd and differ in the nvram template,
OVMF_VARS.secboot.fd against OVMF_VARS.fd, so only descriptor priority
picked the one that holds keys. Commit 772b2c656 of this branch says that
element selects the descriptor with enrolled keys. It does not.

firmware_features names secure-boot and enrolled-keys for a Secure Boot
guest, and add_firmware_features writes them into the <os> of the domain.
guest_os cannot carry the element: the firmware attribute of <os> and the
<firmware> child of <os> are one key of one hash for XMLout, and the
attribute is the one that starts the selection. firmware:efi asks for no
feature and is unchanged.

Measured with libvirt 11.10.0 on an x86_64 host, with the enrolled
descriptor masked for one connection: the old XML starts on OVMF_VARS.fd
with enrolled-keys resolved to no, and the new XML is refused with
"Unable to find 'efi' firmware that is compatible with the current
configuration". virt-xml-validate accepts the new XML.

kvm_secureboot_firmware.t covers the features and the element. Two
mutations turn it red: dropping enrolled-keys from firmware_features, and
making add_firmware_features return its argument.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
Daniel Hilst
2026-10-22 10:11:49 -03:00
parent 789913b001
commit 94d2ce2cd5
3 changed files with 156 additions and 10 deletions
+79 -5
View File
@@ -528,11 +528,12 @@ sub firmware_machine_error {
A ppc64le hypervisor gets no <bios useserial>. A profile without bios drops it too.
A machine type from vm.othersettings replaces the machine type of the profile.
A profile with secureboot states <loader secure='yes'/>. libvirt reads it while it
selects the firmware and keeps only the descriptors that carry secure-boot, so a
hypervisor with no such firmware refuses the domain. Without the element the guest
takes whichever firmware the hypervisor prefers, and the domain says nothing about
whether that firmware verifies what it boots.
A profile with secureboot states <loader secure='yes'/>. libvirt keeps only the
descriptors that carry the secure-boot feature while it selects the firmware, so a
hypervisor with no such firmware refuses the domain. The element says nothing about
the UEFI keys, so firmware_features asks for enrolled-keys beside it. Without the
element the guest takes whichever firmware the hypervisor prefers, and the domain
says nothing about whether that firmware verifies what it boots.
Arguments:
$profile - the hash reference from guest_arch_profile
%args - hyp_cpumodel: the cpumodel of the hypervisor; bootorder: vm.bootorder of the
@@ -568,6 +569,79 @@ sub guest_os {
#-------------------------------------------------------
=head3 firmware_features
Descriptions:
The firmware autoselection features of one guest, in the form <firmware> states them.
A guest with secureboot asks for secure-boot and for enrolled-keys. secure-boot names
a firmware that can verify what it boots. It does not name the UEFI keys that decide
what passes, and a firmware with secure-boot and no enrolled keys boots an unsigned
binary. The two x86_64 descriptors of an EL 10 host show the difference:
30-edk2-ovmf-x64-sb-enrolled.json and 40-edk2-ovmf-x64-sb.json name the same
OVMF_CODE.secboot.fd and differ in the nvram template, OVMF_VARS.secboot.fd against
OVMF_VARS.fd. Only the first template holds keys.
libvirt reads <loader secure='yes'/> as the secure-boot feature alone, and picks the
descriptor of highest priority that carries it. A host that ships the 40- descriptor
and not the 30- one satisfies that domain with an empty key store.
Arguments:
$profile - the hash reference from guest_arch_profile, or undef
Returns:
an array reference of { name, enabled } pairs, empty when the guest asks for none
=cut
#-------------------------------------------------------
sub firmware_features {
my ($profile) = @_;
return [] unless ref($profile) eq 'HASH' and $profile->{secureboot};
return [
{ name => 'secure-boot', enabled => 'yes' },
{ name => 'enrolled-keys', enabled => 'yes' },
];
}
#-------------------------------------------------------
=head3 add_firmware_features
Descriptions:
Add <firmware> with the features firmware_features names to the <os> of one domain.
guest_os cannot carry this element. The firmware attribute of <os> and the <firmware>
child of <os> are one key of one hash for XMLout, which writes a scalar as an
attribute and a reference as an element, so the hash holds one of the two. The
attribute is the one that starts the firmware selection.
Arguments:
$xml - the domain XML
$features - the array reference from firmware_features
Returns:
the new domain XML, or $xml when the guest asks for no feature
=cut
#-------------------------------------------------------
sub add_firmware_features {
my ($xml, $features) = @_;
return $xml unless ref($features) eq 'ARRAY' and scalar(@$features);
require XML::LibXML;
my $domdesc = XML::LibXML->new()->parse_string($xml);
my ($os) = $domdesc->findnodes('/domain/os');
return $xml unless $os;
my $element = $domdesc->createElement('firmware');
foreach my $feature (@$features) {
my $child = $domdesc->createElement('feature');
$child->setAttribute('enabled', $feature->{enabled});
$child->setAttribute('name', $feature->{name});
$element->appendChild($child);
}
$os->appendChild($element);
return $domdesc->toString();
}
#-------------------------------------------------------
=head3 clock_offset
Descriptions:
+2 -1
View File
@@ -889,7 +889,8 @@ sub build_xmldesc {
$xtree{devices}->{console}->{type} = 'pty';
$xtree{devices}->{console}->{target}->{port} = '1';
return XMLout(\%xtree, RootName => "domain");
return xCAT::KVMUtils::add_firmware_features(XMLout(\%xtree, RootName => "domain"),
xCAT::KVMUtils::firmware_features($profile));
}
sub refresh_vm {
+75 -4
View File
@@ -8,16 +8,26 @@
# neither. A node that must boot signed binaries has to say so, so that a hypervisor without a
# Secure Boot firmware refuses the domain instead of starting it unverified.
#
# <loader secure='yes'/> is the requirement libvirt reads during firmware selection. Measured
# with libvirt 11.10.0 on rome01: <os firmware='efi'> with that element resolves to
# OVMF_CODE.secboot.fd and <smm state='on'/>, and the same domain with
# <firmware><feature enabled='no' name='secure-boot'/></firmware> resolves to OVMF_CODE.fd.
# <loader secure='yes'/> is not that requirement on its own. libvirt reads it as the secure-boot
# feature, and a firmware with secure-boot and an empty key store boots an unsigned binary. The
# two x86_64 descriptors of an EL 10 host name the same OVMF_CODE.secboot.fd and differ in the
# nvram template: 30-edk2-ovmf-x64-sb-enrolled.json takes OVMF_VARS.secboot.fd, which holds the
# keys, and 40-edk2-ovmf-x64-sb.json takes OVMF_VARS.fd, which does not. Only descriptor
# priority picks the first one, and priority is host policy.
#
# So the domain asks for enrolled-keys as well. Measured with libvirt 11.10.0 on rome01, with
# the 30- descriptor masked for one connection: the domain that states <loader secure='yes'/>
# alone starts on OVMF_VARS.fd with enrolled-keys resolved to no, and the domain that asks for
# enrolled-keys is refused with "Unable to find 'efi' firmware that is compatible with the
# current configuration".
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../../perl-xCAT";
use Test::More;
use XML::LibXML;
use XML::Simple qw(XMLout);
require xCAT::KVMUtils;
@@ -105,4 +115,65 @@ is( xCAT::KVMUtils::firmware_machine_error( undef, 'pc', 'x86_64', 'cn1' ),
is( xCAT::KVMUtils::firmware_machine_error( 'efi', 'virt', 'riscv64', 'cn1' ),
undef, 'the machine types of another architecture are not judged here' );
# <firmware> carries the features libvirt filters the descriptors by. It is not in the hash
# guest_os returns: the firmware attribute of <os> and the <firmware> child of <os> are one key
# of one hash for XMLout, and the attribute is the one that starts the selection. So
# add_firmware_features adds the element to the domain XMLout wrote, which is what kvm.pm does.
my $features = xCAT::KVMUtils::firmware_features($sb);
is_deeply(
$features,
[ { name => 'secure-boot', enabled => 'yes' }, { name => 'enrolled-keys', enabled => 'yes' } ],
'a Secure Boot node asks for secure-boot and for enrolled-keys'
);
is_deeply( xCAT::KVMUtils::firmware_features( node_profile('firmware:efi') ),
[], 'firmware:efi asks for no firmware feature' );
is_deeply( xCAT::KVMUtils::firmware_features( node_profile(undef) ),
[], 'and neither does a node that asks for no firmware' );
is_deeply( xCAT::KVMUtils::firmware_features(undef), [], 'and neither does an undefined profile' );
# The domain as kvm.pm writes it: the <os> of guest_os, then the features.
sub domain_of {
my ($profile) = @_;
my $os = XMLout( os_of($profile), RootName => 'os' );
$os =~ s/^<\?xml[^>]*\?>\s*//;
return xCAT::KVMUtils::add_firmware_features( "<domain type='kvm'>\n$os\n</domain>\n",
xCAT::KVMUtils::firmware_features($profile) );
}
sub feature_enabled {
my ( $xml, $name ) = @_;
my ($node) =
XML::LibXML->load_xml( string => $xml )
->findnodes("/domain/os/firmware/feature[\@name='$name']");
return defined($node) ? $node->getAttribute('enabled') : undef;
}
my $sb_domain = domain_of($sb);
is( feature_enabled( $sb_domain, 'enrolled-keys' ),
'yes', 'the domain of a Secure Boot node asks for a firmware whose keys are enrolled' );
is( feature_enabled( $sb_domain, 'secure-boot' ),
'yes', 'and for one that verifies what it boots' );
is( XML::LibXML->load_xml( string => $sb_domain )->findnodes('/domain/os')->[0]
->getAttribute('firmware'),
'efi', 'and it keeps firmware="efi" on <os>, which is what starts the firmware selection' );
is( XML::LibXML->load_xml( string => $sb_domain )->findnodes('/domain/os/loader')->[0]
->getAttribute('secure'),
'yes', 'and keeps <loader secure="yes"/> beside the features' );
# A node that asked only for UEFI gets no <firmware>, so it keeps taking the descriptor its
# hypervisor prefers.
foreach my $othersettings ( 'firmware:efi', undef ) {
my $name = defined($othersettings) ? $othersettings : 'no firmware';
my $domain = domain_of( node_profile($othersettings) );
is_deeply(
[ XML::LibXML->load_xml( string => $domain )->findnodes('/domain/os/firmware') ],
[], "$name states no firmware feature" );
}
is( xCAT::KVMUtils::add_firmware_features( "<domain type='kvm'><os/></domain>", [] ),
"<domain type='kvm'><os/></domain>",
'a domain that asks for no feature is returned unchanged, not re-serialised' );
is( xCAT::KVMUtils::add_firmware_features( "<domain type='kvm'/>", $features ),
"<domain type='kvm'/>", 'a domain with no <os> is returned unchanged' );
done_testing();