@samitouri / QOSamiQemu / commits / 1988a7f8ed

pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode

Enable secure IPL in audit mode, which performs signature verification, but any error does not terminate the boot process. Only warnings will be logged to the console instead. Secure IPL in audit mode requires at least one certificate provided in the key store along with necessary facilities (Secure IPL Facility, Certificate Store Facility and secure IPL extension support). Note: Secure IPL in audit mode is implemented for the SCSI scheme of virtio-blk/virtio-scsi devices. Signed-off-by: Zhuoying Cai <zycai@linux.ibm.com> Reviewed-by: Eric Farman <farman@linux.ibm.com> Reviewed-by: Jared Rossi <jrossi@linux.ibm.com> Reviewed-by: Collin Walling <walling@linux.ibm.com> Link: https://lore.kernel.org/qemu-devel/20260730214624.2328883-24-zycai@linux.ibm.com Signed-off-by: Eric Farman <farman@linux.ibm.com>

Zhuoying Cai committed Jul 30, 2026 at 17:46 UTC 1988a7f8ed191c54efeaf8add8cb83bcb8cfb872
12 files changed +628 -2
docs/system/s390x/secure-ipl.rst
+15
@@ -39,3 +39,18 @@ Configuration:
39 .. code-block:: shell
40
41 qemu-system-s390x -machine s390-ccw-virtio ...
42 +
43 +Audit Mode
44 +^^^^^^^^^^
45 +
46 +When the certificate store is populated with at least one certificate
47 +and no additional secure IPL parameters are provided on the command
48 +line, then secure IPL will proceed in "audit mode". All secure IPL
49 +operations will be performed with signature verification errors reported
50 +as non-disruptive warnings.
51 +
52 +Configuration:
53 +
54 +.. code-block:: shell
55 +
56 + qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ...
hw/s390x/ipl.c
+9
@@ -828,6 +828,15 @@ void s390_ipl_prepare_cpu(S390CPU *cpu)
828 cpu->env.psw.addr = ipl->bios_start_addr;
829 if (!ipl->iplb_valid) {
830 ipl->iplb_valid = s390_init_all_iplbs(ipl);
831 +
832 + /*
833 + * Secure IPL without specifying a boot device.
834 + * IPLB is not generated if no boot device is defined.
835 + */
836 + if (s390_has_certificate() && !ipl->iplb_valid) {
837 + error_report("No boot device defined for Secure IPL");
838 + exit(1);
839 + }
840 } else {
841 ipl->qipl.chain_len = 0;
842 }
pc-bios/s390-ccw/Makefile
+1 -1
@@ -35,7 +35,7 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
35
36 OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
37 virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
38 - virtio-ccw.o clp.o pci.o virtio-pci.o
38 + virtio-ccw.o clp.o pci.o virtio-pci.o secure-ipl.o
39
40 SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
41
pc-bios/s390-ccw/bootmap.c
+28
@@ -10,11 +10,13 @@
10
11 #include <string.h>
12 #include <stdio.h>
13 +#include <stdlib.h>
14 #include "s390-ccw.h"
15 #include "s390-arch.h"
16 #include "bootmap.h"
17 #include "virtio.h"
18 #include "bswap.h"
19 +#include "secure-ipl.h"
20
21 #ifdef DEBUG
22 /* #define DEBUG_FALLBACK */
@@ -712,6 +714,9 @@ static int zipl_run(ScsiBlockPtr *pte)
714 ComponentHeader *header;
715 ComponentEntry *entry;
716 uint8_t tmp_sec[MAX_SECTOR_SIZE];
717 + IplDeviceComponentList comp_list = { 0 };
718 + IplSignatureCertificateList cert_list = { 0 };
719 + uint8_t *tmp_cert_buf = NULL;
720 int rc;
721
722 if (virtio_read(pte->blockno, tmp_sec)) {
@@ -738,6 +743,9 @@ static int zipl_run(ScsiBlockPtr *pte)
743 case ZIPL_BOOT_MODE_NORMAL:
744 rc = zipl_run_normal(&entry, tmp_sec);
745 break;
746 + case ZIPL_BOOT_MODE_SECURE_AUDIT:
747 + rc = zipl_run_secure(&entry, tmp_sec, &comp_list, &cert_list, &tmp_cert_buf);
748 + break;
749 default:
750 panic("Unknown boot mode");
751 }
@@ -748,10 +756,18 @@ static int zipl_run(ScsiBlockPtr *pte)
756
757 if (entry->component_type != ZIPL_COMP_ENTRY_EXEC) {
758 puts("No EXEC entry");
759 + free(tmp_cert_buf);
760 return -EINVAL;
761 }
762
763 write_reset_psw(entry->compdat.load_psw);
764 +
765 + if (boot_mode == ZIPL_BOOT_MODE_SECURE_AUDIT) {
766 + update_cert_list(&cert_list);
767 + update_iirb(&comp_list, &cert_list);
768 + free(tmp_cert_buf);
769 + }
770 +
771 jump_to_IPL_code(0);
772 return -1; /* should not return */
773 }
@@ -1107,6 +1123,18 @@ static int zipl_load_vscsi(void)
1123 * IPL starts here
1124 */
1125
1126 +ZiplBootMode get_boot_mode(uint8_t hdr_flags)
1127 +{
1128 + bool sipl_set = hdr_flags & DIAG308_IPIB_FLAGS_SIPL;
1129 + bool iplir_set = hdr_flags & DIAG308_IPIB_FLAGS_IPLIR;
1130 +
1131 + if (!sipl_set && iplir_set) {
1132 + return ZIPL_BOOT_MODE_SECURE_AUDIT;
1133 + }
1134 +
1135 + return ZIPL_BOOT_MODE_NORMAL;
1136 +}
1137 +
1138 void zipl_load(void)
1139 {
1140 VDev *vdev = virtio_get_device();
pc-bios/s390-ccw/bootmap.h
+9
@@ -88,9 +88,18 @@ typedef struct BootMapTable {
88 BootMapPointer entry[];
89 } __attribute__ ((packed)) BootMapTable;
90
91 +#define DER_SIGNATURE_FORMAT 1
92 +
93 +typedef struct SignatureInformation {
94 + uint8_t format;
95 + uint8_t reserved[3];
96 + uint32_t sig_len;
97 +} SignatureInformation;
98 +
99 typedef union ComponentEntryData {
100 uint64_t load_psw;
101 uint64_t load_addr;
102 + SignatureInformation sig_info;
103 } ComponentEntryData;
104
105 typedef struct ComponentEntry {
pc-bios/s390-ccw/jump2ipl.c
+7
@@ -75,6 +75,13 @@ int jump_to_IPL_code(uint64_t address)
75 "diag %%r1,%%r1,0x308\n\t"
76 : : : "1", "memory");
77 puts("IPL code jump failed");
78 +
79 + /*
80 + * A failed jump only occurs in extreme conditions, so abort the IPL entirely.
81 + * This also prevents attempts to boot from the chain area if it has been
82 + * overwritten with component data.
83 + */
84 + qipl.chain_len = 0;
85 return -1;
86 }
87
pc-bios/s390-ccw/main.c
+18 -1
@@ -20,6 +20,7 @@
20 #include "dasd-ipl.h"
21 #include "clp.h"
22 #include "virtio-pci.h"
23 +#include "secure-ipl.h"
24
25 static SubChannelId blk_schid = { .one = 1 };
26 static char loadparm_str[LOADPARM_LEN + 1];
@@ -386,6 +387,8 @@ static void probe_boot_device(void)
387
388 void main(void)
389 {
390 + int vcssb_len;
391 +
392 iplb = &ipl_blocks.iplb;
393
394 copy_qipl();
@@ -397,7 +400,21 @@ void main(void)
400 probe_boot_device();
401 }
402
400 - boot_mode = ZIPL_BOOT_MODE_NORMAL;
403 + boot_mode = get_boot_mode(iplb->hdr_flags);
404 + switch (boot_mode) {
405 + case ZIPL_BOOT_MODE_SECURE_AUDIT:
406 + if (!secure_ipl_supported()) {
407 + panic("Unable to boot in audit mode");
408 + }
409 +
410 + vcssb_len = zipl_secure_get_vcssb();
411 + if (vcssb_len == 0) {
412 + panic("Failed to query certificate storage information!");
413 + }
414 + break;
415 + default:
416 + break;
417 + }
418
419 while (have_iplb) {
420 boot_setup();
pc-bios/s390-ccw/s390-ccw.h
+20
@@ -40,6 +40,22 @@ typedef unsigned long long u64;
40 ((b) == 0 ? (a) : (MIN(a, b))))
41 #endif
42
43 +/*
44 + * Round number down to multiple. Requires that d be a power of 2.
45 + * Works even if d is a smaller type than n.
46 + */
47 +#ifndef ROUND_DOWN
48 +#define ROUND_DOWN(n, d) ((n) & -(0 ? (n) : (d)))
49 +#endif
50 +
51 +/*
52 + * Round number up to multiple. Requires that d be a power of 2.
53 + * Works even if d is a smaller type than n.
54 + */
55 +#ifndef ROUND_UP
56 +#define ROUND_UP(n, d) ROUND_DOWN((n) + (d) - 1, (d))
57 +#endif
58 +
59 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
60
61 #include "cio.h"
@@ -64,6 +80,8 @@ void sclp_print(const char *string);
80 void sclp_set_write_mask(uint32_t receive_mask, uint32_t send_mask);
81 void sclp_setup(void);
82 void sclp_get_loadparm_ascii(char *loadparm);
83 +bool sclp_is_diag320_on(void);
84 +bool sclp_is_fac_ipl_flag_on(uint16_t fac_ipl_flag);
85 int sclp_read(char *str, size_t count);
86
87 /* bootmap.c */
@@ -71,9 +89,11 @@ void zipl_load(void);
89
90 typedef enum ZiplBootMode {
91 ZIPL_BOOT_MODE_NORMAL = 0,
92 + ZIPL_BOOT_MODE_SECURE_AUDIT = 1,
93 } ZiplBootMode;
94
95 extern ZiplBootMode boot_mode;
96 +ZiplBootMode get_boot_mode(uint8_t hdr_flags);
97
98 /* jump2ipl.c */
99 void write_reset_psw(uint64_t psw);
pc-bios/s390-ccw/sclp.c
+27
@@ -113,6 +113,33 @@ void sclp_get_loadparm_ascii(char *loadparm)
113 }
114 }
115
116 +bool sclp_is_diag320_on(void)
117 +{
118 + ReadInfo *sccb = (void *)_sccb;
119 +
120 + memset((char *)_sccb, 0, sizeof(ReadInfo));
121 + sccb->h.length = SCCB_SIZE;
122 + if (!sclp_service_call(SCLP_CMDW_READ_SCP_INFO, sccb)) {
123 + return sccb->fac134 & SCCB_FAC134_DIAG320_BIT;
124 + }
125 +
126 + return 0;
127 +}
128 +
129 +/* check if specified IPL facility flag is enabled */
130 +bool sclp_is_fac_ipl_flag_on(uint16_t fac_ipl_flag)
131 +{
132 + ReadInfo *sccb = (void *)_sccb;
133 +
134 + memset((char *)_sccb, 0, sizeof(ReadInfo));
135 + sccb->h.length = SCCB_SIZE;
136 + if (!sclp_service_call(SCLP_CMDW_READ_SCP_INFO, sccb)) {
137 + return sccb->fac_ipl & fac_ipl_flag;
138 + }
139 +
140 + return 0;
141 +}
142 +
143 int sclp_read(char *str, size_t count)
144 {
145 ReadEventData *sccb = (void *)_sccb;
pc-bios/s390-ccw/sclp.h
+6
@@ -50,6 +50,8 @@ typedef struct SCCBHeader {
50 } __attribute__((packed)) SCCBHeader;
51
52 #define SCCB_DATA_LEN (SCCB_SIZE - sizeof(SCCBHeader))
53 +#define SCCB_FAC134_DIAG320_BIT 0x4
54 +#define SCCB_FAC_IPL_SIPL_BIT 0x4000
55
56 typedef struct ReadInfo {
57 SCCBHeader h;
@@ -57,6 +59,10 @@ typedef struct ReadInfo {
59 uint8_t rnsize;
60 uint8_t reserved[13];
61 uint8_t loadparm[LOADPARM_LEN];
62 + uint8_t reserved1[102];
63 + uint8_t fac134;
64 + uint8_t reserved2;
65 + uint16_t fac_ipl;
66 } __attribute__((packed)) ReadInfo;
67
68 typedef struct SCCB {
pc-bios/s390-ccw/secure-ipl.c new
+369
@@ -0,0 +1,369 @@
1 +/*
2 + * S/390 Secure IPL
3 + *
4 + * Functions to support IPL in secure boot mode (DIAG 320, DIAG 508,
5 + * signature verification, and certificate handling).
6 + *
7 + * For secure IPL overview: docs/system/s390x/secure-ipl.rst
8 + * For secure IPL technical: docs/specs/s390x-secure-ipl.rst
9 + *
10 + * Copyright 2025 IBM Corp.
11 + * Author(s): Zhuoying Cai <zycai@linux.ibm.com>
12 + *
13 + * SPDX-License-Identifier: GPL-2.0-or-later
14 + */
15 +
16 +#include <stdlib.h>
17 +#include <string.h>
18 +#include <stdio.h>
19 +#include "s390-ccw.h"
20 +#include "sclp.h"
21 +#include "secure-ipl.h"
22 +
23 +static VCStorageSizeBlock vcssb __attribute__((__aligned__(8)));
24 +
25 +#define for_each_rb_entry(entry, list) \
26 + for (entry = (void *)(list) + sizeof((list)->ipl_info_header); \
27 + (void *)(entry) + sizeof(*(entry)) <= \
28 + (void *)(list) + (list)->ipl_info_header.len; \
29 + entry++)
30 +
31 +int zipl_secure_get_vcssb(void)
32 +{
33 + /* avoid retrieving vcssb multiple times */
34 + if (vcssb.length == VCSSB_LEN_VALID) {
35 + goto out;
36 + }
37 +
38 + vcssb.length = VCSSB_LEN_VALID;
39 + if (_diag320(&vcssb, DIAG_320_SUBC_QUERY_VCSI) != DIAG_320_RC_OK) {
40 + vcssb.length = 0;
41 + }
42 +
43 +out:
44 + return vcssb.length;
45 +}
46 +
47 +static uint32_t request_certificate(uint8_t *cert_buf, uint8_t index)
48 +{
49 + VCEntryHeader *vce_hdr;
50 + struct vcb {
51 + VCBlockHeader vcb_hdr;
52 + struct vce {
53 + VCEntryHeader vce_hdr;
54 + uint8_t cert_buf[CERT_BUF_MAX_LEN];
55 + } vce;
56 + } __attribute__((__aligned__(PAGE_SIZE))) vcb = { 0 };
57 +
58 + /*
59 + * Request single entry
60 + * Fill input fields of single-entry VCB
61 + *
62 + * First and last index must be equal because only one
63 + * VCE per VCB is currently supported
64 + */
65 + vcb.vcb_hdr.in_len = ROUND_UP(vcssb.max_single_vcb_len, PAGE_SIZE);
66 + vcb.vcb_hdr.first_vc_index = index;
67 + vcb.vcb_hdr.last_vc_index = index;
68 +
69 + if (_diag320(&vcb, DIAG_320_SUBC_STORE_VC) != DIAG_320_RC_OK) {
70 + puts("Could not get certificate");
71 + return 0;
72 + }
73 +
74 + if (vcb.vcb_hdr.out_len == sizeof(VCBlockHeader)) {
75 + puts("No certificate entry");
76 + return 0;
77 + }
78 +
79 + if (vcb.vcb_hdr.remain_ct != 0) {
80 + panic("Not enough memory to store requested certificate");
81 + }
82 +
83 + vce_hdr = &vcb.vce.vce_hdr;
84 + if (!(vce_hdr->flags & DIAG_320_VCE_FLAGS_VALID)) {
85 + puts("Invalid certificate");
86 + return 0;
87 + }
88 +
89 + memcpy(cert_buf, (uint8_t *)&vcb.vce + vce_hdr->cert_offset, vce_hdr->cert_len);
90 +
91 + return vce_hdr->cert_len;
92 +}
93 +
94 +static int cert_list_add(IplSignatureCertificateList *cert_list,
95 + IplSignatureCertificateEntry cert_entry)
96 +{
97 + int cert_entry_idx;
98 +
99 + cert_entry_idx = (cert_list->ipl_info_header.len - sizeof(IplInfoBlockHeader)) /
100 + sizeof(IplSignatureCertificateEntry);
101 +
102 + cert_list->cert_entries[cert_entry_idx] = cert_entry;
103 + cert_list->ipl_info_header.len += sizeof(IplSignatureCertificateEntry);
104 +
105 + return cert_entry_idx;
106 +}
107 +
108 +static void comp_list_add(IplDeviceComponentList *comp_list,
109 + IplDeviceComponentEntry comp_entry)
110 +{
111 + int comp_entry_idx;
112 +
113 + comp_entry_idx = (comp_list->ipl_info_header.len - sizeof(IplInfoBlockHeader)) /
114 + sizeof(IplDeviceComponentEntry);
115 + if (comp_entry_idx > MAX_COMP_ENTRIES - 1) {
116 + printf("Warning: only %d component entries are supported\n",
117 + MAX_COMP_ENTRIES);
118 + panic("The device component list has reached its maximum capacity");
119 + }
120 +
121 + comp_list->device_entries[comp_entry_idx] = comp_entry;
122 + comp_list->ipl_info_header.len += sizeof(IplDeviceComponentEntry);
123 +}
124 +
125 +void update_iirb(IplDeviceComponentList *comp_list,
126 + IplSignatureCertificateList *cert_list)
127 +{
128 + IplInfoReportBlock *iirb;
129 + IplDeviceComponentList *iirb_comps;
130 + IplSignatureCertificateList *iirb_certs;
131 + uint32_t iirb_hdr_len;
132 + uint32_t comps_len;
133 + uint32_t certs_len;
134 +
135 + if (iplb->len % 8 != 0) {
136 + panic("IPL parameter block length field value is not multiple of 8 bytes");
137 + }
138 +
139 + iirb_hdr_len = sizeof(IplInfoReportBlockHeader);
140 + comps_len = comp_list->ipl_info_header.len;
141 + certs_len = cert_list->ipl_info_header.len;
142 + if ((comps_len + certs_len + iirb_hdr_len) > sizeof(IplInfoReportBlock)) {
143 + panic("Not enough space to hold all components and certificates in IIRB");
144 + }
145 +
146 + /* IIRB immediately follows IPLB */
147 + iirb = &ipl_blocks.iirb;
148 + iirb->hdr.len = iirb_hdr_len;
149 +
150 + /* Copy IPL device component list after IIRB Header */
151 + iirb_comps = (IplDeviceComponentList *) iirb->info_blks;
152 + memcpy(iirb_comps, comp_list, comps_len);
153 +
154 + /* Update IIRB length */
155 + iirb->hdr.len += comps_len;
156 +
157 + /* Copy IPL sig cert list after IPL device component list */
158 + iirb_certs = (IplSignatureCertificateList *) (iirb->info_blks +
159 + iirb_comps->ipl_info_header.len);
160 + memcpy(iirb_certs, cert_list, certs_len);
161 +
162 + /* Update IIRB length */
163 + iirb->hdr.len += certs_len;
164 +}
165 +
166 +bool secure_ipl_supported(void)
167 +{
168 + if (!sclp_is_fac_ipl_flag_on(SCCB_FAC_IPL_SIPL_BIT)) {
169 + puts("Secure IPL Facility is not supported by the hypervisor!");
170 + return false;
171 + }
172 +
173 + if (!is_signature_verif_supported()) {
174 + puts("Secure IPL extensions are not supported by the hypervisor!");
175 + return false;
176 + }
177 +
178 + if (!is_cert_store_facility_supported()) {
179 + puts("Certificate Store Facility is not supported by the hypervisor!");
180 + return false;
181 + }
182 +
183 + return true;
184 +}
185 +
186 +static void init_lists(IplDeviceComponentList *comp_list,
187 + IplSignatureCertificateList *cert_list)
188 +{
189 + comp_list->ipl_info_header.type = IPL_INFO_BLOCK_TYPE_COMPONENTS;
190 + comp_list->ipl_info_header.len = sizeof(IplInfoBlockHeader);
191 +
192 + cert_list->ipl_info_header.type = IPL_INFO_BLOCK_TYPE_CERTIFICATES;
193 + cert_list->ipl_info_header.len = sizeof(IplInfoBlockHeader);
194 +}
195 +
196 +static int zipl_load_signature(ComponentEntry *entry, uint64_t sig)
197 +{
198 + if (entry->compdat.sig_info.format != DER_SIGNATURE_FORMAT) {
199 + puts("Signature is not in DER format");
200 + return -1;
201 + }
202 +
203 + if (zipl_load_segment(entry->data.blockno, sig) < 0) {
204 + return -1;
205 + }
206 +
207 + return entry->compdat.sig_info.sig_len;
208 +}
209 +
210 +void update_cert_list(IplSignatureCertificateList *cert_list)
211 +{
212 + IplSignatureCertificateEntry *cert_entry;
213 + uint8_t *cert_buf;
214 +
215 + /*
216 + * Recover the original base address of ipl_data for cert storage.
217 + *
218 + * The IplParameterBlocks stored in ipl_data will no longer be needed
219 + * after this point. Reuse this region to store certificates from the
220 + * BIOS heap into stable memory.
221 + */
222 + cert_buf = (uint8_t *)qipl.ipl_data - qipl.index * sizeof(IplParameterBlock);
223 +
224 + for_each_rb_entry(cert_entry, cert_list) {
225 + memcpy(cert_buf, (uint8_t *)cert_entry->addr, cert_entry->len);
226 + cert_entry->addr = (uint64_t)cert_buf;
227 + cert_buf += cert_entry->len;
228 + }
229 +}
230 +
231 +int zipl_run_secure(ComponentEntry **entry_ptr, const uint8_t *tmp_sec,
232 + IplDeviceComponentList *comp_list,
233 + IplSignatureCertificateList *cert_list,
234 + uint8_t **tmp_cert_buf)
235 +{
236 + /*
237 + * Keep track of which certificate store indices correspond to the
238 + * certificate data entries within the IplSignatureCertificateList to
239 + * prevent allocating space for the same certificate multiple times.
240 + *
241 + * The array index corresponds to the certificate's cert-store index.
242 + *
243 + * The array value corresponds to the certificate's entry within the
244 + * IplSignatureCertificateList (with a value of -1 denoting no entry
245 + * exists for the certificate).
246 + */
247 + int cert_list_table[vcssb.total_vc_ct + 1];
248 + IplSignatureCertificateEntry sig_entry = { 0 };
249 + IplSignatureCertificateEntry cert_entry;
250 + IplDeviceComponentEntry comp_entry;
251 + ComponentEntry *entry = *entry_ptr;
252 + int rc = -1;
253 + int sig_len = 0;
254 + int comp_len;
255 + int cert_entry_idx;
256 + uint64_t comp_addr;
257 + uint8_t cert_table_idx;
258 + uint8_t *tmp_buf;
259 + bool verified;
260 + bool signed_found = false;
261 +
262 + if ((MAX_SIGNED_COMP * CERT_BUF_MAX_LEN) > CERT_BUF_SIZE) {
263 + panic("Not enough memory to store certificates");
264 + }
265 + *tmp_cert_buf = malloc(CERT_BUF_SIZE);
266 + tmp_buf = *tmp_cert_buf;
267 +
268 + init_lists(comp_list, cert_list);
269 + sig_entry.addr = (uint64_t)malloc(MAX_SECTOR_SIZE);
270 + memset(cert_list_table, -1, sizeof(cert_list_table));
271 +
272 + while (entry->component_type != ZIPL_COMP_ENTRY_EXEC) {
273 + switch (entry->component_type) {
274 + case ZIPL_COMP_ENTRY_SIGNATURE:
275 + if (sig_entry.len) {
276 + goto error;
277 + }
278 +
279 + sig_len = zipl_load_signature(entry, sig_entry.addr);
280 + if (sig_len < 0) {
281 + goto error;
282 + }
283 +
284 + sig_entry.len = sig_len;
285 + break;
286 + case ZIPL_COMP_ENTRY_LOAD:
287 + comp_addr = entry->compdat.load_addr;
288 + comp_len = zipl_load_segment(entry->data.blockno, comp_addr);
289 + if (comp_len < 0) {
290 + goto error;
291 + }
292 +
293 + comp_entry = (IplDeviceComponentEntry){ 0 };
294 + comp_entry.addr = comp_addr;
295 + comp_entry.len = (uint64_t)comp_len;
296 +
297 + /* no signature present (unsigned component) */
298 + if (!sig_entry.len) {
299 + comp_list_add(comp_list, comp_entry);
300 + break;
301 + }
302 +
303 + /*
304 + * Initialize with SC flag (signed component)
305 + * CSV flag set upon successful verification
306 + */
307 + comp_entry.flags = S390_IPL_DEV_COMP_FLAG_SC;
308 + signed_found = true;
309 +
310 + cert_entry = (IplSignatureCertificateEntry) { 0 };
311 + verified = verify_signature(comp_entry, sig_entry,
312 + &cert_entry.len, &cert_table_idx);
313 +
314 + if (verified) {
315 + if (cert_list_table[cert_table_idx] == -1) {
316 + if (!request_certificate(tmp_buf, cert_table_idx)) {
317 + puts("Could not get certificate");
318 + goto error;
319 + }
320 +
321 + cert_entry.addr = (uint64_t)tmp_buf;
322 + cert_entry_idx = cert_list_add(cert_list, cert_entry);
323 + /* map cert-store index to cert-list entry index */
324 + cert_list_table[cert_table_idx] = cert_entry_idx;
325 + /* increment for the next certificate */
326 + tmp_buf += cert_entry.len;
327 + }
328 +
329 + comp_entry.cert_index = cert_list_table[cert_table_idx];
330 + comp_entry.flags |= S390_IPL_DEV_COMP_FLAG_CSV;
331 + puts("Verified component");
332 + } else {
333 + zipl_secure_error("Could not verify component");
334 + }
335 +
336 + comp_list_add(comp_list, comp_entry);
337 +
338 + /* After a signature is used another new one can be accepted */
339 + sig_entry.len = 0;
340 + break;
341 + default:
342 + puts("Unknown component entry type");
343 + goto error;
344 + }
345 +
346 + entry++;
347 +
348 + if ((uint8_t *)(&entry[1]) > tmp_sec + MAX_SECTOR_SIZE) {
349 + puts("Wrong entry value");
350 + rc = -EINVAL;
351 + goto error;
352 + }
353 + }
354 +
355 + if (!signed_found) {
356 + zipl_secure_error("Secure boot is on, but components are not signed");
357 + }
358 +
359 + *entry_ptr = entry;
360 + free((void *)sig_entry.addr);
361 +
362 + return 0;
363 +error:
364 + free(*tmp_cert_buf);
365 + *tmp_cert_buf = NULL;
366 + free((void *)sig_entry.addr);
367 +
368 + return rc;
369 +}
pc-bios/s390-ccw/secure-ipl.h new
+119
@@ -0,0 +1,119 @@
1 +/*
2 + * S/390 Secure IPL
3 + *
4 + * Copyright 2025 IBM Corp.
5 + * Author(s): Zhuoying Cai <zycai@linux.ibm.com>
6 + *
7 + * SPDX-License-Identifier: GPL-2.0-or-later
8 + */
9 +
10 +#ifndef _PC_BIOS_S390_CCW_SECURE_IPL_H
11 +#define _PC_BIOS_S390_CCW_SECURE_IPL_H
12 +
13 +#include "bootmap.h"
14 +#include <diag320.h>
15 +#include <diag508.h>
16 +
17 +#define MAX_SIGNED_COMP 3
18 +
19 +int zipl_secure_get_vcssb(void);
20 +bool secure_ipl_supported(void);
21 +void update_iirb(IplDeviceComponentList *comp_list,
22 + IplSignatureCertificateList *cert_list);
23 +void update_cert_list(IplSignatureCertificateList *cert_list);
24 +int zipl_run_secure(ComponentEntry **entry_ptr, const uint8_t *tmp_sec,
25 + IplDeviceComponentList *comp_list,
26 + IplSignatureCertificateList *cert_list,
27 + uint8_t **tmp_cert_buf);
28 +
29 +static inline void zipl_secure_error(const char *message)
30 +{
31 + switch (boot_mode) {
32 + case ZIPL_BOOT_MODE_SECURE_AUDIT:
33 + printf("AUDIT MODE WARNING: %s\n", message);
34 + break;
35 + default:
36 + /*
37 + * Errors are intentionally ignored in non-secure boot modes.
38 + * This function should only be reached in SECURE modes.
39 + */
40 + break;
41 + }
42 +}
43 +
44 +static inline uint64_t _diag320(void *data, unsigned long subcode)
45 +{
46 + register unsigned long addr asm("0") = (unsigned long)data;
47 + register unsigned long rc asm("1") = 0;
48 +
49 + asm volatile ("diag %0,%2,0x320\n"
50 + : "+d" (addr), "+d" (rc)
51 + : "d" (subcode)
52 + : "memory", "cc");
53 + return rc;
54 +}
55 +
56 +static inline bool is_cert_store_facility_supported(void)
57 +{
58 + uint32_t d320_ism;
59 +
60 + if (!sclp_is_diag320_on()) {
61 + return false;
62 + }
63 +
64 + if (_diag320(&d320_ism, DIAG_320_SUBC_QUERY_ISM) != DIAG_320_RC_OK) {
65 + return false;
66 + }
67 +
68 + return d320_ism & (DIAG_320_ISM_QUERY_VCSI | DIAG_320_ISM_STORE_VC);
69 +}
70 +
71 +static inline uint64_t _diag508(void *data, unsigned long subcode)
72 +{
73 + register unsigned long addr asm("0") = (unsigned long)data;
74 + register unsigned long rc asm("1") = 0;
75 +
76 + asm volatile ("diag %0,%2,0x508\n"
77 + : "+d" (addr), "+d" (rc)
78 + : "d" (subcode)
79 + : "memory", "cc");
80 + return rc;
81 +}
82 +
83 +static inline bool is_signature_verif_supported(void)
84 +{
85 + uint64_t d508_subcodes;
86 +
87 + d508_subcodes = _diag508(NULL, DIAG_508_SUBC_QUERY_SUBC);
88 + return d508_subcodes & DIAG_508_SUBC_SIG_VERIF;
89 +}
90 +
91 +static inline bool verify_signature(IplDeviceComponentEntry comp_entry,
92 + IplSignatureCertificateEntry sig_entry,
93 + uint64_t *cert_len, uint8_t *cert_idx)
94 +{
95 + Diag508SigVerifBlock svb;
96 +
97 + svb.length = sizeof(Diag508SigVerifBlock);
98 + svb.version = 0;
99 + svb.comp_len = comp_entry.len;
100 + svb.comp_addr = comp_entry.addr;
101 + svb.sig_len = sig_entry.len;
102 + svb.sig_addr = sig_entry.addr;
103 +
104 + if (_diag508(&svb, DIAG_508_SUBC_SIG_VERIF) == DIAG_508_RC_OK) {
105 + *cert_len = svb.cert_len;
106 + /*
107 + * DIAG 508 utilizes an index origin of 0 when indexing the cert store.
108 + * The cert_idx will be used for DIAG 320 data structures, which expects
109 + * an index origin of 1. Account for the offset here so it's easier to
110 + * manage later.
111 + */
112 + *cert_idx = svb.cert_store_index + 1;
113 + return true;
114 + }
115 +
116 + return false;
117 +}
118 +
119 +#endif /* _PC_BIOS_S390_CCW_SECURE_IPL_H */