@samitouri / QOSamiQemu / commits / f5faede1ea

s390x/diag: Implement DIAG 508 subcode 1 for signature verification

DIAG 508 subcode 1 performs signature-verification on signed components. A signed component may be a Linux kernel image, or any other signed binary. **Verification of initrd is not supported.** The instruction call expects two item-pairs: an address of a device component, an address of the analogous signature file (in PKCS#7 DER format), and their respective lengths. All of this data should be encapsulated within a Diag508SigVerifBlock. The DIAG handler will read from the provided addresses to retrieve the necessary data, parse the signature file, then perform the signature-verification. Because there is no way to correlate a specific certificate to a component, each certificate in the store is tried until either verification succeeds, or all certs have been exhausted. A return code of 1 indicates success, and the index and length of the corresponding certificate will be set in the Diag508SigVerifBlock. The following values indicate failure: 0x0102: no certificates are available in the store 0x0202: component data is invalid 0x0302: PKCS#7 format signature is invalid 0x0402: signature-verification failed 0x0502: length of Diag508SigVerifBlock is invalid Signed-off-by: Collin Walling <walling@linux.ibm.com> Signed-off-by: Zhuoying Cai <zycai@linux.ibm.com> Reviewed-by: Thomas Huth <thuth@redhat.com> Reviewed-by: Farhan Ali <alifm@linux.ibm.com> Link: https://lore.kernel.org/qemu-devel/20260730214624.2328883-15-zycai@linux.ibm.com Signed-off-by: Eric Farman <farman@linux.ibm.com>

Collin Walling committed Jul 30, 2026 at 17:46 UTC f5faede1ea8f99bd61e4c30c3c473cf104fd2fc4
3 files changed +160 -1
docs/specs/s390x-secure-ipl.rst
+17
@@ -84,3 +84,20 @@ that requires assistance from QEMU.
84
85 Subcode 0 - query installed subcodes
86 Returns a 64-bit mask indicating which subcodes are supported.
87 +
88 +Subcode 1 - perform signature verification
89 + Perform signature-verification on a signed component, using certificates
90 + from the certificate store and leveraging qcrypto libraries to perform
91 + this operation.
92 +
93 + Note: verification of initrd is not supported.
94 +
95 + A return code of 1 indicates success, and the index and length of the
96 + corresponding certificate will be set in the Diag508SigVerifBlock.
97 + The following values indicate failure:
98 +
99 + * ``0x0102``: no certificates are available in the store
100 + * ``0x0202``: component data is invalid
101 + * ``0x0302``: PKCS#7 format signature is invalid
102 + * ``0x0402``: signature-verification failed
103 + * ``0x0502``: length of Diag508SigVerifBlock is invalid
include/hw/s390x/ipl/diag508.h
+30
@@ -11,5 +11,35 @@
11 #define S390X_DIAG508_H
12
13 #define DIAG_508_SUBC_QUERY_SUBC 0x0000
14 +#define DIAG_508_SUBC_SIG_VERIF 0x8000
15 +
16 +#define DIAG_508_RC_OK 0x0001
17 +#define DIAG_508_RC_NO_CERTS 0x0102
18 +#define DIAG_508_RC_INVAL_COMP_DATA 0x0202
19 +#define DIAG_508_RC_INVAL_PKCS7_SIG 0x0302
20 +#define DIAG_508_RC_FAIL_VERIF 0x0402
21 +#define DIAG_508_RC_INVAL_LEN 0x0502
22 +
23 +/*
24 + * Maximum component and signature sizes for current secure boot implementation
25 + * Not architecturally defined and may need to revisit if increased
26 + */
27 +#define DIAG_508_MAX_COMP_LEN 0x10000000
28 +#define DIAG_508_MAX_SIG_LEN 4096
29 +
30 +struct Diag508SigVerifBlock {
31 + uint32_t length;
32 + uint8_t reserved0[3];
33 + uint8_t version;
34 + uint32_t reserved[2];
35 + uint8_t cert_store_index;
36 + uint8_t reserved1[7];
37 + uint64_t cert_len;
38 + uint64_t comp_len;
39 + uint64_t comp_addr;
40 + uint64_t sig_len;
41 + uint64_t sig_addr;
42 +};
43 +typedef struct Diag508SigVerifBlock Diag508SigVerifBlock;
44
45 #endif
target/s390x/diag.c
+113 -1
@@ -12,6 +12,7 @@
12 * GNU General Public License for more details.
13 */
14
15 +#include <inttypes.h>
16 #include "qemu/osdep.h"
17 #include "cpu.h"
18 #include "s390x-internal.h"
@@ -620,9 +621,112 @@ void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)
621 }
622 }
623
624 +static bool diag_508_verify_sig(uint8_t *cert, size_t cert_size,
625 + uint8_t *comp, size_t comp_size,
626 + uint8_t *sig, size_t sig_size)
627 +{
628 + g_autofree uint8_t *sig_pem = NULL;
629 + size_t sig_size_pem;
630 + int rc;
631 +
632 + /*
633 + * PKCS#7 signature with DER format
634 + * Convert to PEM format for signature verification
635 + *
636 + * Ignore errors during qcrypto signature format conversion and verification
637 + * Return false on any error, treating it as a verification failure
638 + */
639 + rc = qcrypto_pkcs7_convert_sig_pem(sig, sig_size, &sig_pem, &sig_size_pem, NULL);
640 + if (rc < 0) {
641 + return false;
642 + }
643 +
644 + rc = qcrypto_x509_verify_sig(cert, cert_size,
645 + comp, comp_size,
646 + sig_pem, sig_size_pem, NULL);
647 + if (rc < 0) {
648 + return false;
649 + }
650 +
651 + return true;
652 +}
653 +
654 +static int handle_diag508_sig_verif(CPUS390XState *env, uint64_t addr)
655 +{
656 + int verified;
657 + uint32_t svb_len;
658 + uint64_t comp_len, comp_addr;
659 + uint64_t sig_len, sig_addr;
660 + g_autofree uint8_t *comp = NULL;
661 + g_autofree uint8_t *sig = NULL;
662 + g_autofree Diag508SigVerifBlock *svb = NULL;
663 + size_t svb_size = sizeof(Diag508SigVerifBlock);
664 + S390IPLCertificateStore *cs = s390_ipl_get_certificate_store();
665 +
666 + if (!cs->count) {
667 + return DIAG_508_RC_NO_CERTS;
668 + }
669 +
670 + svb = g_new0(Diag508SigVerifBlock, 1);
671 + s390_ipl_read(env, addr, svb, svb_size);
672 +
673 + svb_len = be32_to_cpu(svb->length);
674 + if (svb_len != svb_size) {
675 + return DIAG_508_RC_INVAL_LEN;
676 + }
677 +
678 + comp_len = be64_to_cpu(svb->comp_len);
679 + comp_addr = be64_to_cpu(svb->comp_addr);
680 + sig_len = be64_to_cpu(svb->sig_len);
681 + sig_addr = be64_to_cpu(svb->sig_addr);
682 +
683 + if (!comp_len || !comp_addr || comp_len > DIAG_508_MAX_COMP_LEN) {
684 + if (comp_len > DIAG_508_MAX_COMP_LEN) {
685 + warn_report("DIAG 0x508: component length %" PRIu64
686 + " exceeds current maximum %u",
687 + comp_len, DIAG_508_MAX_COMP_LEN);
688 + }
689 + return DIAG_508_RC_INVAL_COMP_DATA;
690 + }
691 +
692 + if (!sig_len || !sig_addr || sig_len > DIAG_508_MAX_SIG_LEN) {
693 + if (sig_len > DIAG_508_MAX_SIG_LEN) {
694 + warn_report("DIAG 0x508: signature length %" PRIu64
695 + " exceeds current maximum %u",
696 + sig_len, DIAG_508_MAX_SIG_LEN);
697 + }
698 + return DIAG_508_RC_INVAL_PKCS7_SIG;
699 + }
700 +
701 + comp = g_malloc0(comp_len);
702 + s390_ipl_read(env, comp_addr, comp, comp_len);
703 +
704 + sig = g_malloc0(sig_len);
705 + s390_ipl_read(env, sig_addr, sig, sig_len);
706 +
707 + for (int i = 0; i < cs->count; i++) {
708 + verified = diag_508_verify_sig(cs->certs[i].raw,
709 + cs->certs[i].size,
710 + comp, comp_len,
711 + sig, sig_len);
712 + if (verified) {
713 + svb->cert_store_index = i;
714 + svb->cert_len = cpu_to_be64(cs->certs[i].der_size);
715 + s390_ipl_write(env, addr, svb, svb_size);
716 + return DIAG_508_RC_OK;
717 + }
718 + }
719 +
720 + return DIAG_508_RC_FAIL_VERIF;
721 +}
722 +
723 +QEMU_BUILD_BUG_MSG(sizeof(Diag508SigVerifBlock) != 64,
724 + "size of Diag508SigVerifBlock is wrong");
725 +
726 void handle_diag_508(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)
727 {
728 uint64_t subcode = env->regs[r3];
729 + uint64_t addr = env->regs[r1];
730 int rc;
731
732 if (env->psw.mask & PSW_MASK_PSTATE) {
@@ -637,7 +741,15 @@ void handle_diag_508(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)
741
742 switch (subcode) {
743 case DIAG_508_SUBC_QUERY_SUBC:
640 - rc = 0;
744 + rc = DIAG_508_SUBC_SIG_VERIF;
745 + break;
746 + case DIAG_508_SUBC_SIG_VERIF:
747 + if (!diag_parm_addr_valid(addr, sizeof(Diag508SigVerifBlock), true)) {
748 + s390_program_interrupt(env, PGM_ADDRESSING, ra);
749 + return;
750 + }
751 +
752 + rc = handle_diag508_sig_verif(env, addr);
753 break;
754 default:
755 s390_program_interrupt(env, PGM_SPECIFICATION, ra);