master
py 172 lines 7.28 KB
Raw
1 #!/usr/bin/env python3
2 #
3 # SPDX-License-Identifier: GPL-2.0-or-later
4 """
5 s390x Secure IPL functional test.
6
7 Validates s390x secure boot by preparing a signed guest image, booting with
8 secure-boot enabled, and verifying cryptographic validation results.
9 """
10
11 from subprocess import check_call, DEVNULL
12
13 from qemu_test import QemuSystemTest, Asset, get_qemu_img
14 from qemu_test import exec_command_and_wait_for_pattern, exec_command
15 from qemu_test import wait_for_console_pattern, skipBigDataTest
16
17 class S390xSecureIpl(QemuSystemTest):
18 """Test s390x Secure IPL (secure boot) functionality."""
19 ASSET_F40_QCOW2 = Asset(
20 ('https://archives.fedoraproject.org/pub/archive/'
21 'fedora-secondary/releases/40/Server/s390x/images/'
22 'Fedora-Server-KVM-40-1.14.s390x.qcow2'),
23 '091c232a7301be14e19c76ce9a0c1cbd2be2c4157884a731e1fc4f89e7455a5f')
24
25 def __init__(self, *args, **kwargs):
26 super().__init__(*args, **kwargs)
27 self.root_password = None
28 self.qcow2_path = None
29 self.cert_path = None
30 self.prompt = None
31
32 def _create_certificate(self, vm):
33 """Generate x509 certificate"""
34 exec_command_and_wait_for_pattern(self,
35 'openssl version', 'OpenSSL 3.2.1 30',
36 vm=vm)
37 exec_command_and_wait_for_pattern(self,
38 'openssl req -new -x509 -newkey rsa:2048 '
39 '-keyout mykey.pem -outform PEM -out mycert.pem '
40 '-days 36500 -subj "/CN=My Name/" -nodes -verbose',
41 'Writing private key to \'mykey.pem\'', vm=vm)
42
43 def _sign_binaries(self, vm):
44 """Sign stage3 binary and kernel"""
45 # Install kernel-devel (needed for sign-file)
46 exec_command_and_wait_for_pattern(self,
47 'sudo dnf install kernel-devel-$(uname -r) -y',
48 'Complete!', vm=vm)
49 wait_for_console_pattern(self, self.prompt, vm=vm)
50 exec_command_and_wait_for_pattern(self,
51 'ls /usr/src/kernels/$(uname -r)/scripts/',
52 'sign-file', vm=vm)
53
54 # Sign stage3 binary and kernel
55 exec_command(self, '/usr/src/kernels/$(uname -r)/scripts/sign-file '
56 'sha256 mykey.pem mycert.pem /lib/s390-tools/stage3.bin',
57 vm=vm)
58 wait_for_console_pattern(self, self.prompt, vm=vm)
59 exec_command(self, '/usr/src/kernels/$(uname -r)/scripts/sign-file '
60 'sha256 mykey.pem mycert.pem /boot/vmlinuz-$(uname -r)',
61 vm=vm)
62 wait_for_console_pattern(self, self.prompt, vm=vm)
63
64 def _run_zipl_secure(self, vm):
65 """Run zipl to prepare for secure boot"""
66 exec_command_and_wait_for_pattern(self, 'zipl --secure 1 -VV', 'Done.',
67 vm=vm)
68
69 def _extract_certificate(self, vm):
70 """Extract certificate from VM to host filesystem"""
71 out = exec_command_and_wait_for_pattern(self, 'cat mycert.pem',
72 '-----END CERTIFICATE-----',
73 vm=vm)
74 # strip first line to avoid console echo artifacts
75 cert = "\n".join(out.decode("utf-8").splitlines()[1:])
76 self.log.info("%s", cert)
77
78 self.cert_path = self.scratch_file("mycert.pem")
79
80 with open(self.cert_path, 'w', encoding="utf-8") as file_object:
81 file_object.write(cert)
82
83 def setup_s390x_secure_ipl(self):
84 """
85 Prepare a secure boot-enabled guest image.
86
87 Boots a temporary VM to generate a certificate, sign boot components
88 (stage3 and kernel), run zipl, and extract the certificate to host.
89 """
90 self.require_netdev('user')
91
92 temp_vm = self.get_vm(name='sipl_setup')
93 temp_vm.set_machine('s390-ccw-virtio')
94
95 asset_path = self.ASSET_F40_QCOW2.fetch()
96 self.qcow2_path = self.scratch_file('f40.qcow2')
97 qemu_img = get_qemu_img(self)
98 check_call([qemu_img, 'create', '-f', 'qcow2', '-b', asset_path,
99 '-F', 'qcow2', self.qcow2_path], stdout=DEVNULL, stderr=DEVNULL)
100
101 temp_vm.set_console()
102 temp_vm.add_args('-nographic',
103 '-accel', 'kvm',
104 '-m', '1024',
105 '-drive',
106 f'id=drive0,if=none,format=qcow2,file={self.qcow2_path}',
107 '-device', 'virtio-blk-ccw,drive=drive0,bootindex=1')
108 temp_vm.launch()
109
110 # Initial root account setup (Fedora first boot screen)
111 self.root_password = 'fedora40password'
112 wait_for_console_pattern(self, 'Please make a selection from the above',
113 vm=temp_vm)
114 exec_command_and_wait_for_pattern(self, '4', 'Password:', vm=temp_vm)
115 exec_command_and_wait_for_pattern(self, self.root_password,
116 'Password (confirm):', vm=temp_vm)
117 exec_command_and_wait_for_pattern(self, self.root_password,
118 'Please make a selection from the above',
119 vm=temp_vm)
120
121 # Login as root
122 self.prompt = '[root@localhost ~]#'
123 exec_command_and_wait_for_pattern(self, 'c', 'localhost login:', vm=temp_vm)
124 exec_command_and_wait_for_pattern(self, 'root', 'Password:', vm=temp_vm)
125 exec_command_and_wait_for_pattern(self, self.root_password, self.prompt,
126 vm=temp_vm)
127
128 self._create_certificate(temp_vm)
129 self._sign_binaries(temp_vm)
130 self._run_zipl_secure(temp_vm)
131 self._extract_certificate(temp_vm)
132
133 # Shutdown temp vm
134 temp_vm.shutdown()
135
136 @skipBigDataTest()
137 def test_s390x_secure_ipl(self):
138 """
139 Verify secure boot validation during s390x guest boot.
140
141 Expects two "Verified component" messages and confirms
142 /sys/firmware/ipl/secure reports secure boot is active.
143 """
144 self.require_accelerator('kvm')
145 self.setup_s390x_secure_ipl()
146
147 self.set_machine('s390-ccw-virtio')
148
149 self.vm.set_console()
150 self.vm.add_args('-nographic',
151 '-machine', 's390-ccw-virtio,secure-boot=on,'
152 f'boot-certs.0.path={self.cert_path}',
153 '-accel', 'kvm',
154 '-m', '1024',
155 '-drive',
156 f'id=drive1,if=none,format=qcow2,file={self.qcow2_path}',
157 '-device', 'virtio-blk-ccw,drive=drive1,bootindex=1')
158 self.vm.launch()
159
160 # Expect two verified components
161 verified_output = "Verified component"
162 wait_for_console_pattern(self, verified_output)
163 wait_for_console_pattern(self, verified_output)
164
165 # Login and verify the vm is booted using secure boot
166 wait_for_console_pattern(self, 'localhost login:')
167 exec_command_and_wait_for_pattern(self, 'root', 'Password:')
168 exec_command_and_wait_for_pattern(self, self.root_password, self.prompt)
169 exec_command_and_wait_for_pattern(self, 'cat /sys/firmware/ipl/secure', '1')
170
171 if __name__ == '__main__':
172 QemuSystemTest.main()