Secure IPL Support for SCSI Scheme of virtio-blk/virtio-scsi Devices
Subsystems (2)
v7 cover Secure IPL Support for SCSI Scheme of virtio-blk/virtio-scsi Devices
Changelog v6->v7
Add functional test for secure IPL
Secure IPL documentation
- Merged command line into one line
- Limited line length to 80 columns
[PATCH v6 01/28] Add boot-certs to s390-ccw-virtio machine type option
- Renamed BootCertificate to BootCertificates
- Added Acked-by: Markus Armbruster <[email protected]>
[PATCH v6 02/28] crypto/x509-utils: Refactor with GNUTLS fallback
- Edited commit message
- Added Acked-by: Daniel P. Berrangé <[email protected]>
[PATCH v6 03/28] crypto/x509-utils: Add helper functions for certificate store
- Added Acked-by: Daniel P. Berrangé <[email protected]>
[PATCH v6 04/28] hw/s390x/ipl: Create certificate store
- Removed QEMU_PAKCED
- Inlined cert2buf wrapper function to reduce lines of code
- Reported error and handled exit in s390_ipl_create_cert_store()
[PATCH v6 05/28] s390x/diag: Introduce DIAG 320 for Certificate Store Facility
- Fixed typos
- Added Reviewed-by: Farhan Ali <[email protected]>
[PATCH v6 06/28] s390x/diag: Refactor address validation check from diag308_parm_check
- Moved diag_parm_addr_valid() to target/s390x/diag.c
- Added Reviewed-by: Farhan Ali <[email protected]>
[PATCH v6 07/28] s390x/diag: Implement DIAG 320 subcode 1
- Added Reviewed-by: Farhan Ali <[email protected]>
[PATCH v6 08/28] crypto/x509-utils: Add helper functions for DIAG 320 subcode 2
- Edited commit message
- Handled negative return value from gnutls_x509_crt_get_pk_algorithm() in qcrypto_x509_get_pk_algorithm
- Checked boundary before using hash_alg in qcrypto_x509_get_cert_key_id()
- Rename qcrypto_x509_is_ecc_curve_p521() to qcrypto_x509_check_ecc_curve_p521()
[PATCH v6 09/28] s390x/diag: Implement DIAG 320 subcode 2
- Removed redundant set vce->len
- Added check to make sure data doesn't exceed buffer bounds
[PATCH v6 10/28] s390x/diag: Introduce DIAG 508 for secure IPL operations
- Reworded document to avoid the term “KVM”
- Added Reviewed-by: Farhan Ali <[email protected]>
[PATCH v6 11/28] crypto/x509-utils: Add helper functions for DIAG 508 subcode 1
- Fixed indentation
- Replaced g_new0() and memcpy() with g_memdup2()
[PATCH v6 12/28] s390x/diag: Implement DIAG 508 subcode 1 for signature verification
- Edit commit message and documentation in handle_diag508_sig_verif()
- Changed diag_508_verify_sig function return type from int to bool
- Set upper limit for comp_len and sig_len to avoid malicious memory allocation
- Fixed coding style - eliminate unnecessary variables
[PATCH v6 13/28] pc-bios/s390-ccw: Introduce IPL Information Report Block (IIRB)
- Removed bios structs packing
[PATCH v6 14/28] pc-bios/s390-ccw: Define memory for IPLB and convert IPLB to pointers
- specify ipl_data attribute in the pc-bios/s390-ccw/main.c
[PATCH v6 15/28] hw/s390x/ipl: Add IPIB flags to IPL Parameter Block
- Moved the setting of iplb->len to when the DIAG308_IPIB_FLAGS_IPLIR is set
[PATCH v6 17/28] pc-bios/s390-ccw: Refactor zipl_run()
- Preserved return error value from zipl_run_normal()
[PATCH v6 18/28] pc-bios/s390-ccw: Rework zipl_load_segment function
- Fixed typoes
- Added Reviewed-by: Thomas Huth <[email protected]>
[PATCH v6 19/28] pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
- Edited documentation
- Removed bios structs packing
- Be more specific in the error message and mention Passthrough (vfio) CCW device does not support secure boot
- Allocated memory for vcb using max_single_vcb_len instead of MAX_SECTOR_SIZE in request_certificate()
- Rename variables in zipl_run_secure()cert_index → cert_entry_idxcomp_index → comp_entry_idxcert_idx → cert_table_idx
- Refactored handle_certificate(): only handles certificate related operations and moved the increment of cert_entry_idx and cert to zipl_run_secure()
[PATCH v6 21/28] pc-bios/s390-ccw: Add additional security checks for secure boot
- Fixed coding style
- Added out of range warning in comp_addr_range_add()
- Fixed end address issue in is_psw_valid() and is_comp_overlap()
- Inlined comparison functions
[PATCH v6 22/28] Add secure-boot to s390-ccw-virtio machine type option
- Moved bool secure_boot next to other bool variables to avoid additional padding
[PATCH v6 24/28] pc-bios/s390-ccw: Handle true secure IPL mode
- Edited documentation
- Used panic instead of IPL_assert() in zipl_secure_handle()
[PATCH v6 25/28] pc-bios/s390-ccw: Handle secure boot with multiple boot devices
- Used a stack variable for QemuIplParameters instead of a malloc in check_secure_boot_support()
-----------------------------------------------------------------------
# Description
This patch series is an external requirement by Linux distribution partners to verify secure IPL process. Additional secure IPL checks are also included in this series to address security holes in the original secure IPL design to prevent malicious actors to boot modified or unsigned code despite secure IPL being enforced.
Secure IPL is enabled when the QEMU options for secure IPL are specified in the command line.
During this process, additional security checks are performed to ensure system integrity.
As components are loaded from disk, DIAG 508 subcode 2 performs signature verification if a signature entry is identified. Upon successful verification, DIAG 320 subcode 2 will request the corresponding certificate from QEMU key store to the BIOS.
Secure IPL will continue until all the components are loaded if no error occurs during True secure IPL mode or in Audit mode (see explanation below).
After that, an IPL Information report block (IIRB) is initialized immediately following an IPL Parameter Information Block. The IIRB is populated with information about the components, verification results and certificate data.
Finally, the guest system proceeds to boot.
Only List-Directed-IPL contains the relevant zIPL data structures to perform secure IPL. This patch series only adds support for the SCSI scheme of virtio-blk/virtio-scsi devices. Secure IPL for other device types will be considered as follow-up work at a later date.
** Note: "secure IPL" and "secure boot" are used interchangeably throughout the design. **
# True Secure IPL Mode and Audit Mode
## True Secure IPL Mode
When secure IPL is enabled and certificates are provided, all the secure IPL checks will performed. The boot process will abort if any error occurs during the secure IPL checks.
## Audit Mode
When the secure IPL option is not selected and certificates are provided, all the secure IPL checks will still be performed. However, the boot process will continue if any errors occur, with messages logged to the console during the secure IPL checks.
The audit mode is also considered as simulated secure IPL because it is less pervasive, and allows the guest to boot regardless of the secure checking results.
# How to Enable Secure IPL
## QEMU Build Notes
When building QEMU, enable the cryptographic libraries.
Run configure script in QEMU repository with either parameter:
./configure … --enable-gnutls
## Create Certificates via certtool
Generate and use an RSA private key for signing.
certtool --generate-privkey > key.pem
A self-signed certificate requires the organization name. Use the cert.info template to pre-fill values and avoid interactive prompts from certtool.
cat > cert.info <<EOF cn = "My Name" expiration_days = 36500 cert_signing_key EOF certtool --generate-self-signed \ --load-privkey key.pem \ --template cert.info \ --hash=SHA256 \ --outfile cert.pem
It is recommended to store the certificate(s) in the /…/qemu/certs directory for easy identification.
## Sign Kernel and Prepare zipl
All actions must be performed on a guest.
Copy the sign-file script (located in Linux source repository), generated private key(s), and certificate(s) to guest's file system.
Sign guest image(s) and stage3 binary:
./sign-file sha256 key.pem cert.pem /boot/vmlinuz-… ./sign-file sha256 key.pem cert.pem /usr/lib/s390-tools/stage3.bin
Run zipl with secure boot enabled.
zipl --secure 1 -V
Guest image(s) are now signed, stored on disk, and can be verified.
## New QEMU Command Options for Secure IPL
New parameters have been added to the s390-ccw-virtio machine type to enable Secure IPL and provide certificates for signature verification.
This parameter enables or disables Secure IPL/boot. If not specified, it defaults to off.
qemu-system-s390x -machine s390-ccw-virtio,secure-boot=on|off
This parameter specifies one or more paths to boot certificates, used for signature verification. You can provide a single certificate file or a directory.
qemu-system-s390x -machine s390-ccw-virtio, \
boot-certs.0.path=/.../qemu/certs, \
boot-certs.1.path=/another/path/cert.pemExample:
qemu-system-s390x -machine s390-ccw-virtio,
secure-boot=on, \
boot-certs.0.path=/.../qemu/certs, \
boot-certs.1.path=/another/path/cert.pemSecure IPL command options overview:
If neither the -secure-boot nor the -boot-certs options are specified, the guest will boot in normal mode, and no security checks will be conducted.
If the -secure-boot option is not specified or is set to off, and the -boot-certs option is provided, the guest will boot in audit mode. In this mode, all security checks are performed; however, any errors encountered will not interrupt the boot process.
If the -secure-boot option is set to on and the -boot-certs option is provided, the guest will boot in true secure IPL mode. In this mode, all security checks are performed, and any errors encountered will terminate the boot process.
- If the -boot-certs option is not provided in true secure IPL mode, the boot process will fail for the corresponding device.
## Constraints
- z16 CPU model
- certificates must be in X.509 PEM format
- only support for SCSI scheme of virtio-blk/virtio-scsi devices
- The boot process will terminate if secure boot is enabled without
specifying a boot device.
- If enabling secure boot with multiple boot devices, any
unsupported devices or non-eligible devices will cause the entire boot process terminating early with an error logged to the console.
- attempting to perform secure IPL outside of these constraints will result in a failure.
# DIAGNOSE 508 - IPL Extensions
Signature verification is performed during IPL via DIAG 508. Component address, component length, signature address and signature length are obtained in the BIOS and pass to DIAG 508 subcode 2 to perform signature verification in QEMU. If verification succeeds, DIAG 508 subcode 2 (signature verification) will return the length and index of the certificate in the QemuCertificateStore that was used for verification.
## Data Structures
Diag508SignatureVerificationBlock (SVB) — stores addresses and lengths of the component and signature to be used for signature verification. Upon verification, an index and the length of the certificate used is stored.
------------------------------------------------------------------------
Changelog v5->v6
Add boot-certs to s390-ccw-virtio machine type option
- Renamed struct BootCertPath to BootCertificate
- Defined DummyBootCertificates struct for QMP to use BootCertificateList internally
crypto/x509-utils
- Used gnutls_x509_crt_export2() in qcrypto_x509_convert_cert_der()
- Allocated buffers with g_new for results in qcrypto_x509_convert_cert_der() and qcrypto_pkcs7_convert_sig_pem()
- Added check for qcrypto_to_gnutls_hash_alg_map bounds in qcrypto_x509_get_cert_key_id()
- Added qcrypto_x509_is_ecc_curve_p521() function to determine if an ECC public key algorithm uses P521 curve
- Fixed typo
hw/s390x/ipl: Create certificate store
- Made any configuration errors fatal when loading certificates into the cert store
- Removed key_id_size and hash_size from S390IPLCertificate and defined them as constants
- Do not reinitialize the cert store if it already exists
- Handled memory cleanup
s390x/diag: Implement DIAG 320 subcode 1
- Added QEMU_BUILD_BUG_MSG
s390x/diag: Implement DIAG 320 subcode 2
- Refactored build_vce_header() and created a new function for getting key type
- Added QEMU_BUILD_BUG_MSG
s390x/diag: Implement DIAG 508 subcode 1 for signature verification
- Updated Diag508SigVerifBlock
hw/s390x/ipl: Add IPIB flags to IPL Parameter Block
- Override IPLB length only when DIAG308_IPIB_FLAGS_IPLIR flag is set
- Dropped hw/s390x/ipl: Set iplb->len to maximum length of IPL Parameter Block patch
pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
- Added validation for VCB and VCE before retrieving information in request_certificate()
- Replaced uint64_t *cert with uint8_t *cert in zipl_run_secure()
Changelog v4->v5
- Segmented documentation by introducing them alongside the patches that add the relevant functionality
- Removed hash-type restriction from S390IPLCertificateStore
Add boot-certs to s390-ccw-virtio machine type option
- Added boot-certs property to the s390-ccw-virtio machine type
- Changed to use an array property for passing certificate instead
of colo-delimited strings.
e.g. boot-certs.0.path=/path/to/dir,
boot-certs.1.path=/to/other/dir,
boot-certs.2.path=/some/...crypto/x509-utils
- Refactored fallback handling into a separated commit
- Removed QCryptoKeyidFlags and qcrypto_x509_get_keyid_len() function
- Removed qcrypto_x509_get_cert_version() and qcrypto_x509_get_signature_algorithm()
- Passed QCryptoHashAlgo to qcrypto_x509_get_cert_key_id() instead of QCryptoKeyidFlags
- Improved memory cleanup
hw/s390x/ipl: Create certificate store
- Fixed error handling and memory management
- Added condition to only accept files with ".pem" extension
s390x: Guest support for Certificate Store Facility (CS)
- Renamed "S390_FEAT_DIAG_320" to "S390_FEAT_CERT_STORE"
- Merged this patch with s390x/diag: Introduce DIAG 320 for certificate store facility patch
s390x/diag: Introduce DIAG 320 for certificate store facility
- Added bit zero to indicate subcode 0 is supported
- Set response code to 0x0102 when the subcode is not supported
- Added check to ensure subcode fits within one byte
s390x/diag: Implement DIAG 320 subcode 1
- Refactored and moved implementation to a helper function
- Added check to ensure VCSSB length is set to a minimum of 128 bytes from the userspace
- Removed max_vce_len field from the VCSSB data structure
s390x/diag: Implement DIAG 320 subcode 2
- Refactored and split functions for extracting certificate information
- Set VCE length to 72 if certificate is invalid
- Returned VCE even if the certificate is invalid
pc-bios/s390-ccw: Refactor zipl_run()
- Refactor zipl_run_normal() to reduce duplicate code
pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
- Moved secure-boot related functions into secure-ipl.c/h files
- Rename zipl_handle_sig_entry() to zipl_load_signature()
- Refactored zipl_run_secure() to use switch cases for different entry types instead of a while loop
- Renamed ZiplBootMode enumerations with "ZIPL_BOOT_MODE" prefix
- Added meaningful file header to secure-ipl.c
- Renamed zipl_secure_print() to zipl_secure_handle()
Changelog v3->v4:
cyrpto/x590-utils
- Updated patch descriptions to summarize what each patch introduces.
- Restricted accepted certificate format on the QEMU command line to PEM only.
- Added internal conversion routines to handle PEM-to-DER and
DER-to-PEM as needed.
- Renamed crypto functions using the qcrypto_x509_ prefix.
- Replaced QAPI enums with plain C enums.
- Improved GNUTLS error propagation throughout the crypto API.
- Standardized error returns to -1 (instead of using errno values) for consistency.
- Removed unused helpers: qcrypto_check_x509_cert_fmt() and qcrypto_get_x509_hash_len().
- Added bounds checking before converting GNUTLS enums to internal crypto enums.
- Modified qcrypto_get_x509_cert_key_id() to dynamically allocate the result.
- Fixed a typo in the crypto API documentation.
hw/s390x/ipl: Create certificate store
- Removed max_size check when loading certificate.
- Removed check_path_type() and use the stat() & S_ISXXX checks inline where needed.
- Initialized all g_autofree variables.
- Refactored get_cert_paths()
- Used g_auto(GStrv) and g_autoptr(GPtrArray) for automatic cleanup.
- Renamed misleading variables and dropped unnecessary intermediates.
- Replaced check_path_type() with stat(), and treated failures as fatal.
- Treated empty path components as fatal configuration errors.
- Used GError with g_dir_open() and handled errors properly.
- Removed redundant casts and manual cleanup.
- Treated providing 65 or more certificates as a fatal error.
- Modified S390IPLCertificate
- Changed raw from char * to uint8_t * for API consistency.
- Added cert_der field to calculate certificate data buffer size for
DIAG 320 and format conversion.
s390x/diag
- Handled error propagation from crypto API correctly.
- Added g_autofree to variables to ensure proper memory cleanup.
- Initialized all g_autofree variables.
pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
- Allocated certificate buffer based on calculated size instead of static MAX_CERT_SIZE.
pc-bios/s390-ccw: Add additional security checks for secure boot
- Refactored to improve readability.
- Handled SCLAB Single Component/No Unsigned Component flags.
docs: Add secure IPL documentation
- Updated documentation.
- Illustrated with gnutls ‘certtool’ instead of ‘openssl’ for consistency with other certificate creation docs.
- Updated cover letter with corresponding changes.
Changelog v2->v3:
- Fixed typos in patches
- Edited and corrected patch description
- Enabled secure IPL feature in TCG
- Split crypto subsystem changes from s390x subsystem changes
- Added API documentation for each helper function
- Removed qcrypto_get_x509_cert_fmt()
- Reworked qcrypto_check_x509_cert_fmt() to return 0 on success and
negative error code on errors
- Rephrased error messages when GNUTLS is not enabled
- Changed dev cycle reference from 9.2 to 10.1
hw/s390x/ipl: Create certificate store
- Passed *cert_buf to g_file_get_contents() directly in cert2buf()
- Returned NULL for early returns
- Added check to only allow maximum of 64 certificates in the certificate store
s390x: Guest support for Certificate Store Facility (CS)
- Renamed “diag320” to “cstore”
s390x/diag: Implement DIAG 320 subcode 1
- Removed QEMU_PACKED from VCStorageSizeBlock
- Removed unnecessary error check from subcode 1 implementation
s390x/diag: Implement DIAG 320 subcode 2
- Removed QEMU_PACKED from VCBlock and VCEngtry
- Reworked diag_320_is_cert_valid() to return 0 on success and negative error code on errors
- Set *key_id_data and *hash_data to NULL after g_free()
- Moved DIAG_320_SUBC_STORE_VC case block implementation to a separate function
s390x/diag: Implement DIAG 508 subcode 1 for signature verification
- Added a reserved field to Diag508CertificateStoreInfo
- Removed QEMU_PACKED from Diag508CertificateStoreInfo and Diag508SignatureVerificationBlock
- Applied g_autofree to svb, svb_comp and svb_sig varaibles
- Moved DIAG_508_SUBC_SIG_VERIF case block implementation to a separate function
pc-bios/s390-ccw: Refactor zipl_load_segment function
- Removed casting when calculating comp_len
pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
- Used malloc() to allocate certificate buffer instead of statically allocating
- Reworked zipl_secure_print()
s390x: Guest support for Secure-IPL Code Loading Attributes Facility (SCLAF)
- Added check to make sure SIPL is enabled when SCLAF is enabled
docs: Add secure IPL documentation
- Split document into two parts:
- docs/specs/s390x-secure-ipl.rst (developer reference)
- docs/system/s390x/secure-ipl.rst (user guide)
Changelog v1->v2:
- Fixed typos in patches
- Edited cover letter
- Added secure IPL documentation
QEMU Command-Line Interface:
- Moved boot-certificates under the machine-type option for s390x-virtio-ccw
- Moved secure-boot under the machine-type option for s390x-virtio-ccw
hw/s390x/ipl: Create Certificate Store
- Defined internal GNUTLS-related APIs
- Added check to only accept certificates using SHA-256 hashing
- Recalculated data_buf_size to ensure word alignment
- Cleaned up memory allocation
- Refactored functions for clarity
s390x: Guest Support for Certificate Store Facility (CS)
- Updated patch description to clarify:
- Why Secure IPL is not available with Secure Execution
- Why this feature requires S390_FEAT_EXTENDED_LENGTH_SCCB
-Restricted features to z16 due to additional layers requiring z16
s390x/diag: Implement DIAG 320 Subcode 1
- Renamed VerificationCertificate prefix to VC
- Byte-swap written values for endianness correctness
s390x/diag: Implement DIAG 320 Subcode 2
- Edited commit message for clarity
- Defined internal GNUTLS-related APIs
- Renamed data structure variables
- Ensured length fields in VCE are word-aligned
- Handled the VC index 0 case
- General refactoring
s390x/diag: Implement DIAG 508 Subcode 2 for Signature Verification
- Defined subcode from 2 to 1
- Removed unused error codes
- Defined internal GNUTLS-related APIs
- Byte-swap read values
hw/s390x/ipl: Add IPIB Flags to IPL Parameter Block
- Moved DIAG308 flags to a new header file
s390x: Guest Support for Secure-IPL Facility
- Renamed SCLP variable from cbl to fac_ipl
pc-bios/s390-ccw: Add Signature Verification for Secure Boot (Audit Mode)
- Moved Secure IPL-related functions to pc-bios/s390-ccw/secure-ip.c|h
- Refactored code for clarity
Collin L. Walling (2):
s390x/diag: Introduce DIAG 508 for secure IPL operations s390x/diag: Implement DIAG 508 subcode 1 for signature verification
Zhuoying Cai (27):
Add boot-certs to s390-ccw-virtio machine type option crypto/x509-utils: Refactor with GNUTLS fallback crypto/x509-utils: Add helper functions for certificate store hw/s390x/ipl: Create certificate store
s390x/diag: Introduce DIAG 320 for Certificate Store Facility s390x/diag: Refactor address validation check from diag308_parm_check s390x/diag: Implement DIAG 320 subcode 1 crypto/x509-utils: Add helper functions for DIAG 320 subcode 2 s390x/diag: Implement DIAG 320 subcode 2 crypto/x509-utils: Add helper functions for DIAG 508 subcode 1 pc-bios/s390-ccw: Introduce IPL Information Report Block (IIRB) pc-bios/s390-ccw: Define memory for IPLB and convert IPLB to pointers hw/s390x/ipl: Add IPIB flags to IPL Parameter Block
s390x: Guest support for Secure-IPL Facility pc-bios/s390-ccw: Refactor zipl_run()
pc-bios/s390-ccw: Rework zipl_load_segment function pc-bios/s390-ccw: Add signature verification for secure IPL in audit
mode
s390x: Guest support for Secure-IPL Code Loading Attributes Facility
(SCLAF)
pc-bios/s390-ccw: Add additional security checks for secure boot Add secure-boot to s390-ccw-virtio machine type option hw/s390x/ipl: Set IPIB flags for secure IPL pc-bios/s390-ccw: Handle true secure IPL mode pc-bios/s390-ccw: Handle secure boot with multiple boot devices hw/s390x/ipl: Handle secure boot without specifying a boot device tests/functional/s390x: Add secure IPL functional test docs/specs: Add secure IPL documentation docs/system/s390x: Add secure IPL documentation
crypto/meson.build | 2 +- crypto/x509-utils.c | 421 ++++++++++++ docs/specs/s390x-secure-ipl.rst | 175 +++++ docs/system/s390x/secure-ipl.rst | 175 +++++ hw/s390x/cert-store.c | 211 ++++++ hw/s390x/cert-store.h | 41 ++ hw/s390x/ipl.c | 60 ++ hw/s390x/ipl.h | 21 +- hw/s390x/meson.build | 1 + hw/s390x/s390-virtio-ccw.c | 52 ++ hw/s390x/sclp.c | 2 + include/crypto/x509-utils.h | 135 ++++ include/hw/s390x/ipl/diag308.h | 34 + include/hw/s390x/ipl/diag320.h | 91 +++ include/hw/s390x/ipl/diag508.h | 41 ++ include/hw/s390x/ipl/qipl.h | 7 +- include/hw/s390x/s390-virtio-ccw.h | 3 + include/hw/s390x/sclp.h | 4 +- pc-bios/s390-ccw/Makefile | 3 +- pc-bios/s390-ccw/bootmap.c | 112 ++- pc-bios/s390-ccw/bootmap.h | 11 + pc-bios/s390-ccw/iplb.h | 97 ++- pc-bios/s390-ccw/jump2ipl.c | 6 +- pc-bios/s390-ccw/main.c | 109 ++- pc-bios/s390-ccw/netmain.c | 8 +- pc-bios/s390-ccw/s390-ccw.h | 19 + pc-bios/s390-ccw/sclp.c | 52 ++ pc-bios/s390-ccw/sclp.h | 7 + pc-bios/s390-ccw/secure-ipl.c | 803 ++++++++++++++++++++++ pc-bios/s390-ccw/secure-ipl.h | 152 ++++ qapi/machine-s390x.json | 23 + qapi/pragma.json | 1 + qemu-options.hx | 10 +- target/s390x/cpu_features.c | 7 + target/s390x/cpu_features.h | 1 + target/s390x/cpu_features_def.h.inc | 5 + target/s390x/cpu_models.c | 7 + target/s390x/diag.c | 571 ++++++++++++++- target/s390x/gen-features.c | 7 + target/s390x/kvm/kvm.c | 34 + target/s390x/s390x-internal.h | 4 + target/s390x/tcg/misc_helper.c | 14 + tests/functional/s390x/meson.build | 2 + tests/functional/s390x/test_secure_ipl.py | 140 ++++ 44 files changed, 3613 insertions(+), 68 deletions(-) create mode 100644 docs/specs/s390x-secure-ipl.rst create mode 100644 docs/system/s390x/secure-ipl.rst create mode 100644 hw/s390x/cert-store.c create mode 100644 hw/s390x/cert-store.h create mode 100644 include/hw/s390x/ipl/diag308.h create mode 100644 include/hw/s390x/ipl/diag320.h create mode 100644 include/hw/s390x/ipl/diag508.h create mode 100644 pc-bios/s390-ccw/secure-ipl.c create mode 100644 pc-bios/s390-ccw/secure-ipl.h create mode 100644 tests/functional/s390x/test_secure_ipl.py
Signature · 1 line
2.51.1
PATCH v7 1/29 Add boot-certs to s390-ccw-virtio machine type option
Introduce a new boot-certs machine type option for the s390-ccw-virtio machine. This allows users to specify one or more certificate file paths or directories to be used during secure boot.
Each entry is specified using the syntax:
boot-certs.<index>.path=/path/to/cert.pem
Multiple paths can be specify using array properties:
boot-certs.0.path=/path/to/cert.pem, boot-certs.1.path=/path/to/cert-dir, boot-certs.2.path=/path/to/another-dir...
--- docs/system/s390x/secure-ipl.rst | 20 ++++++++++++++++++++ hw/s390x/s390-virtio-ccw.c | 30 ++++++++++++++++++++++++++++++ include/hw/s390x/s390-virtio-ccw.h | 2 ++ qapi/machine-s390x.json | 23 +++++++++++++++++++++++ qapi/pragma.json | 1 + qemu-options.hx | 6 +++++- 6 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 docs/system/s390x/secure-ipl.rst
docs/system/s390x/secure-ipl.rst new file +20 -0
@@ -0,0 +1,20 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Secure IPL Command Line Options +=============================== + +The s390-ccw-virtio machine type supports secure IPL. These parameters allow +users to provide certificates and enable secure IPL directly via the command +line. + +Providing Certificates +---------------------- + +The certificate store can be populated by supplying a list of X.509 certificate +file paths or directories containing certificate files on the command-line: + +Note: certificate files must have a .pem extension. + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ...
hw/s390x/s390-virtio-ccw.c +30 -0
@@ -45,6 +45,7 @@#include "target/s390x/kvm/pv.h" #include "migration/blocker.h" #include "qapi/visitor.h" +#include "qapi/qapi-visit-machine-s390x.h" #include "hw/s390x/cpu-topology.h" #include "kvm/kvm_s390x.h" #include "hw/virtio/virtio-md-pci.h"
@@ -778,6 +779,30 @@ static void machine_set_loadparm(Object *obj, Visitor *v,g_free (val); } +static void machine_get_boot_certs (Object *obj,Visitor *v, +const char *name,void *opaque, +Error **errp) +{ +S390CcwMachineState *ms =S390_CCW_MACHINE (obj); +BootCertificatesList **certs = &ms->boot_certs; + +visit_type_BootCertificatesList (v, name, certs, errp); +} + +static void machine_set_boot_certs (Object *obj,Visitor *v,const char *name, +void *opaque,Error **errp) +{ +S390CcwMachineState *ms =S390_CCW_MACHINE (obj); +BootCertificatesList *cert_list =NULL ; + +visit_type_BootCertificatesList (v, name, &cert_list, errp); +if (!cert_list) { +return ; + } + + ms->boot_certs = cert_list; +} +static void ccw_machine_class_init (ObjectClass *oc,const void *data) {MachineClass *mc =MACHINE_CLASS (oc);
@@ -831,6 +856,11 @@ static void ccw_machine_class_init(ObjectClass *oc, const void *data)"Up to 8 chars in set of [A-Za-z0-9. ] (lower case chars converted" " to upper case) to pass to machine loader, boot manager," " and guest kernel"); + +object_class_property_add (oc,"boot-certs" ,"BootCertificatesList" , + machine_get_boot_certs, machine_set_boot_certs,NULL ,NULL ); +object_class_property_set_description (oc,"boot-certs" , +"provide paths to a directory and/or a certificate file for secure boot" ); }static inline void s390_machine_initfn (Object *obj)
include/hw/s390x/s390-virtio-ccw.h +2 -0
@@ -14,6 +14,7 @@#include "hw/boards.h" #include "qom/object.h" #include "hw/s390x/sclp.h" +#include "qapi/qapi-types-machine-s390x.h" #define TYPE_S390_CCW_MACHINE "s390-ccw-machine"
@@ -31,6 +32,7 @@ struct S390CcwMachineState {uint8_t loadparm[8 ];uint64_t memory_limit;uint64_t max_pagesize; +BootCertificatesList *boot_certs;SCLPDevice *sclp; };
qapi/machine-s390x.json +23 -0
@@ -140,3 +140,26 @@ { 'event': 'SCLP_CPI_INFO_AVAILABLE', 'features': [ 'unstable' ] } + +## +# @BootCertificates: +# +# Boot certificate for secure IPL. +# +# @path: path to an X.509 certificate file or a directory containing +# certificate files. +# +# Since: 10.2 +## +{ 'struct': 'BootCertificates', + 'data': {'path': 'str'} } + +## +# @DummyBootCertificates: +# +# Not used by QMP; hack to let us use BootCertificatesList internally. +# +# Since: 10.2 +## +{ 'struct': 'DummyBootCertificates', + 'data': {'unused-boot-certs': ['BootCertificates'] } }
qapi/pragma.json +1 -0
@@ -49,6 +49,7 @@ 'DisplayProtocol', 'DriveBackupWrapper', 'DummyBlockCoreForceArrays', + 'DummyBootCertificates', 'DummyForceArrays', 'DummyVirtioForceArrays', 'HotKeyMod',
qemu-options.hx +5 -1
@@ -44,7 +44,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \ #endif " memory-backend='backend-id' specifies explicitly provided backend for main RAM (default=none)\n" " cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n" - " smp-cache.0.cache=cachename,smp-cache.0.topology=topologylevel\n", + " smp-cache.0.cache=cachename,smp-cache.0.topology=topologylevel\n" + " boot-certs.0.path=/path/directory,boot-certs.1.path=/path/file provides paths to a directory and/or a certificate file\n", QEMU_ARCH_ALL) SRST ``-machine [type=]name[,prop=value[,...]]``
@@ -205,6 +206,9 @@ SRST :: -machine smp-cache.0.cache=l1d,smp-cache.0.topology=core,smp-cache.1.cache=l1i,smp-cache.1.topology=core + + ``boot-certs.0.path=/path/directory,boot-certs.1.path=/path/file`` + Provide paths to a directory and/or a certificate file on the host [s390x only]. ERST DEF("M", HAS_ARG, QEMU_OPTION_M,
Signature · 1 line
2.51.1
Re: [PATCH v7 01/29] Add boot-certs to s390-ccw-virtio machine type option
Quoted 40 lines · Zhuoying Cai
Introduce a new
boot-certsmachine type option for the s390-ccw-virtio machine. This allows users to specify one or more certificate file paths or directories to be used during secure boot.Each entry is specified using the syntax:
boot-certs.<index>.path=/path/to/cert.pemMultiple paths can be specify using array properties:
boot-certs.0.path=/path/to/cert.pem, boot-certs.1.path=/path/to/cert-dir, boot-certs.2.path=/path/to/another-dir...Signed-off-by Zhuoying Cai <[email protected]>--- docs/system/s390x/secure-ipl.rst | 20 ++++++++++++++++++++ hw/s390x/s390-virtio-ccw.c | 30 ++++++++++++++++++++++++++++++ include/hw/s390x/s390-virtio-ccw.h | 2 ++ qapi/machine-s390x.json | 23 +++++++++++++++++++++++ qapi/pragma.json | 1 + qemu-options.hx | 6 +++++- 6 files changed, 81 insertions(+), 1 deletion(-)create mode 100644
docs/system/s390x/secure-ipl.rstdocs/system/s390x/secure-ipl.rst new file +10
-0@@ -0,0 +1,20 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Secure IPL Command Line Options +=============================== + +The s390-ccw-virtio machine type supports secure IPL. These parameters allow +users to provide certificates and enable secure IPL directly via the command +line. + +Providing Certificates
docs/system/s390x/secure-ipl.rst line 20
Quoted 10 lines
docs/system/s390x/secure-ipl.rst new file +10
-0+---------------------- + +The certificate store can be populated by supplying a list of X.509 certificate +file paths or directories containing certificate files on the command-line: + +Note: certificate files must have a .pem extension. + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ...
Maybe use "-M" instead of "-machine" to shorten the line a little bit? ... just an idea, feel free to keep "-machine" if you prefer it.
Quoted 8 lines
qapi/machine-s390x.json +0
-0@@ -140,3 +140,26 @@ { 'event': 'SCLP_CPI_INFO_AVAILABLE', 'features': [ 'unstable' ] }
qapi/machine-s390x.json line 152
Quoted 10 lines
qapi/machine-s390x.json +10
-0+ +## +# @BootCertificates: +# +# Boot certificate for secure IPL. +# +# @path: path to an X.509 certificate file or a directory containing +# certificate files. +# +# Since: 10.2
This needs to be updated to 11.0 now.
qapi/machine-s390x.json line 162
Quoted 10 lines
qapi/machine-s390x.json +10
-0+## +{ 'struct': 'BootCertificates', + 'data': {'path': 'str'} } + +## +# @DummyBootCertificates: +# +# Not used by QMP; hack to let us use BootCertificatesList internally. +# +# Since: 10.2
dito.
Thomas
PATCH v7 2/29 crypto/x509-utils: Refactor with GNUTLS fallback
In reply to Zhuoying CaiAlways compile x509-utils.c and add a fallback when GNUTLS is unavailable.
These functions will be needed in the s390x code regardless of whether GNUTLS is available.
--- crypto/meson.build | 2 +- crypto/x509-utils.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-)
crypto/meson.build +1 -1
@@ -22,12 +22,12 @@ crypto_ss.add(files( 'tlscredsx509.c', 'tlssession.c', 'rsakey.c', + 'x509-utils.c', )) if gnutls.found() crypto_ss.add(files( 'tlscredsbox.c', - 'x509-utils.c', )) endif
crypto/x509-utils.c +16 -0
@@ -11,6 +11,8 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "crypto/x509-utils.h" + +#ifdef CONFIG_GNUTLS #include <gnutls/gnutls.h> #include <gnutls/crypto.h> #include <gnutls/x509.h>
@@ -78,3 +80,17 @@ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size,gnutls_x509_crt_deinit (crt); return ret; } + +#else /* ! CONFIG_GNUTLS */ + +int qcrypto_get_x509_cert_fingerprint (uint8_t *cert,size_t size, +QCryptoHashAlgo hash, +uint8_t *result, +size_t *resultlen, +Error **errp) +{ +error_setg (errp,"GNUTLS is required to get fingerprint" ); +return -1 ; +} + +#endif /* ! CONFIG_GNUTLS */
Signature · 1 line
2.51.1
Re: [PATCH v7 02/29] crypto/x509-utils: Refactor with GNUTLS fallback
Quoted 6 lines · Zhuoying Cai
Always compile
x509-utils.cand add a fallback when GNUTLS is unavailable.These functions will be needed in the s390x code regardless of whether GNUTLS is available.
Quoted 8 lines
Signed-off-by Zhuoying Cai <[email protected]>Reviewed-by Daniel P. Berrangé <[email protected]>Reviewed-by Farhan Ali <[email protected]>--- crypto/meson.build | 2 +- crypto/x509-utils.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-)
PATCH v7 3/29 crypto/x509-utils: Add helper functions for certificate store
In reply to Zhuoying CaiIntroduce new helper functions for x509 certificate, which will be used by the certificate store:
qcrypto_x509_convert_cert_der() - converts a certificate from PEM to DER format
These functions provide support for certificate format conversion.
--- crypto/x509-utils.c | 49 +++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 21 ++++++++++++++++ 2 files changed, 70 insertions(+)
crypto/x509-utils.c +49 -0
@@ -81,6 +81,46 @@ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size,return ret; } +int qcrypto_x509_convert_cert_der (uint8_t *cert,size_t size, +uint8_t **result,size_t *resultlen, +Error **errp) +{ +int ret =-1 ; +int rc; +gnutls_x509_crt_t crt; +gnutls_datum_t datum = {.data = cert, .size = size}; +gnutls_datum_t datum_der = {.data =NULL , .size =0 }; + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_x509_crt_export2 (crt,GNUTLS_X509_FMT_DER , &datum_der); +if (rc !=0 ) { +error_setg (errp,"Failed to convert certificate to DER format: %s" , +gnutls_strerror (rc)); + goto cleanup; + } + + *resultlen = datum_der.size; + *result =g_memdup2 (datum_der.data, datum_der.size); + + ret =0 ; + +cleanup: +gnutls_x509_crt_deinit (crt); +gnutls_free (datum_der.data); +return ret; +} +#else /* ! CONFIG_GNUTLS */ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size,
@@ -93,4 +133,13 @@ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size, return -1; } +int qcrypto_x509_convert_cert_der (uint8_t *cert,size_t size, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +error_setg (errp,"GNUTLS is required to export X.509 certificate" ); +return -1 ; +} +#endif /* ! CONFIG_GNUTLS */
include/crypto/x509-utils.h +21 -0
@@ -19,4 +19,25 @@ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size,size_t *resultlen, Error **errp); +/** +* qcrypto_x509_convert_cert_der +* @cert: pointer to the raw certificate data in PEM format +* @size: size of the certificate +* @result: output location for the allocated buffer for the certificate +* in DER format +* (the function allocates memory which must be freed by the caller) +* @resultlen: pointer to the size of the buffer (will be updated with the +* actual size of the DER-encoded certificate) +* @errp: error pointer +* +* Convert the given @cert from PEM to DER format. +* +* Returns: 0 on success, +* -1 on error. +*/ +int qcrypto_x509_convert_cert_der (uint8_t *cert,size_t size, +uint8_t **result, +size_t *resultlen, +Error **errp); +#endif
Signature · 1 line
2.51.1
Re: [PATCH v7 03/29] crypto/x509-utils: Add helper functions for certificate store
Quoted 7 lines · Zhuoying Cai
Introduce new helper functions for x509 certificate, which will be used by the certificate store:
qcrypto_x509_convert_cert_der()- converts a certificate from PEM to DER formatThese functions provide support for certificate format conversion.
Quoted 8 lines
Signed-off-by Zhuoying Cai <[email protected]>Reviewed-by Daniel P. Berrangé <[email protected]>Reviewed-by Farhan Ali <[email protected]>--- crypto/x509-utils.c | 49 +++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 21 ++++++++++++++++ 2 files changed, 70 insertions(+)
PATCH v7 4/29 hw/s390x/ipl: Create certificate store
In reply to Zhuoying CaiCreate a certificate store for boot certificates used for secure IPL.
Load certificates from the boot-certs parameter of s390-ccw-virtio machine type option into the cert store.
Currently, only X.509 certificates in PEM format are supported, as the QEMU command line accepts certificates in PEM format only.
--- docs/specs/s390x-secure-ipl.rst | 16 +++ hw/s390x/cert-store.c | 211 ++++++++++++++++++++++++++++++++ hw/s390x/cert-store.h | 41 +++++++ hw/s390x/ipl.c | 19 +++ hw/s390x/ipl.h | 3 + hw/s390x/meson.build | 1 + include/hw/s390x/ipl/qipl.h | 2 + 7 files changed, 293 insertions(+) create mode 100644 docs/specs/s390x-secure-ipl.rst create mode 100644 hw/s390x/cert-store.c create mode 100644 hw/s390x/cert-store.h
docs/specs/s390x-secure-ipl.rst new file +16 -0
@@ -0,0 +1,16 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +s390 Certificate Store and Functions +==================================== + +s390 Certificate Store +---------------------- + +A certificate store is implemented for s390-ccw guests to retain within +memory all certificates provided by the user via the command-line, which +are expected to be stored somewhere on the host's file system. The store +will keep track of the number of certificates, their respective size, +and a summation of the sizes. + +Note: A maximum of 64 certificates are allowed to be stored in the certificate +store.
hw/s390x/cert-store.c new file +211 -0
211 lines not on this page · raw message
hw/s390x/cert-store.h new file +41 -0
41 lines not on this page · raw message
hw/s390x/ipl.c +19 -0
@@ -35,6 +35,7 @@#include "qemu/option.h" #include "qemu/ctype.h" #include "standard-headers/linux/virtio_ids.h" +#include "cert-store.h" #define KERN_IMAGE_START 0x010000UL#define LINUX_MAGIC_ADDR 0x010008UL
@@ -424,6 +425,20 @@ void s390_ipl_convert_loadparm(char *ascii_lp, uint8_t *ebcdic_lp) } } +S390IPLCertificateStore *s390_ipl_get_certificate_store (void ) +{ +S390IPLState *ipl =get_ipl_device (); + +return &ipl->cert_store; +} + +static bool s390_has_certificate (void ) +{ +S390IPLState *ipl =get_ipl_device (); + +return ipl->cert_store.count >0 ; +} +static bool s390_build_iplb (DeviceState *dev_st,IplParameterBlock *iplb) {CcwDevice *ccw_dev =NULL ;
@@ -719,6 +734,10 @@ void s390_ipl_prepare_cpu(S390CPU *cpu)if (!ipl->kernel || ipl->iplb_valid) { cpu->env.psw.addr = ipl->bios_start_addr; +/* initialize cert store if it's empty */ +if (!s390_has_certificate ()) { +s390_ipl_create_cert_store (&ipl->cert_store); + }if (!ipl->iplb_valid) { ipl->iplb_valid =s390_init_all_iplbs (ipl); }else {
hw/s390x/ipl.h +3 -0
@@ -13,6 +13,7 @@#ifndef HW_S390_IPL_H #define HW_S390_IPL_H +#include "cert-store.h" #include "cpu.h" #include "exec/target_page.h" #include "system/address-spaces.h"
@@ -35,6 +36,7 @@ int s390_ipl_pv_unpack(struct S390PVResponse *pv_resp);void s390_ipl_prepare_cpu (S390CPU *cpu);IplParameterBlock *s390_ipl_get_iplb (void );IplParameterBlock *s390_ipl_get_iplb_pv (void ); +S390IPLCertificateStore *s390_ipl_get_certificate_store (void );enum s390_reset {/* default is a reset not triggered by a CPU e.g. issued by QMP */
@@ -64,6 +66,7 @@ struct S390IPLState {IplParameterBlock iplb;IplParameterBlock iplb_pv;QemuIplParameters qipl; +S390IPLCertificateStore cert_store;uint64_t start_addr;uint64_t compat_start_addr;uint64_t bios_start_addr;
hw/s390x/meson.build +1 -0
@@ -17,6 +17,7 @@ s390x_ss.add(files( 'sclpcpu.c', 'sclpquiesce.c', 'tod.c', + 'cert-store.c', )) s390x_ss.add(when: 'CONFIG_KVM', if_true: files( 'tod-kvm.c',
include/hw/s390x/ipl/qipl.h +2 -0
@@ -20,6 +20,8 @@#define LOADPARM_LEN 8#define NO_LOADPARM "\0\0\0\0\0\0\0\0" +#define MAX_CERTIFICATES 64 +/* * The QEMU IPL Parameters will be stored at absolute address * 204 (0xcc) which means it is 32-bit word aligned but not
Signature · 1 line
2.51.1
Re: [PATCH v7 04/29] hw/s390x/ipl: Create certificate store
Quoted 86 lines · Zhuoying Cai
Create a certificate store for boot certificates used for secure IPL.
Load certificates from the
boot-certsparameter of s390-ccw-virtio machine type option into the cert store.Currently, only X.509 certificates in PEM format are supported, as the QEMU command line accepts certificates in PEM format only.
Signed-off-by Zhuoying Cai <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 16 +++ hw/s390x/cert-store.c | 211 ++++++++++++++++++++++++++++++++ hw/s390x/cert-store.h | 41 +++++++ hw/s390x/ipl.c | 19 +++ hw/s390x/ipl.h | 3 + hw/s390x/meson.build | 1 + include/hw/s390x/ipl/qipl.h | 2 + 7 files changed, 293 insertions(+) create mode 100644 docs/specs/s390x-secure-ipl.rst create mode 100644 hw/s390x/cert-store.c create mode 100644 hw/s390x/cert-store.hdocs/specs/s390x-secure-ipl.rst new file +16
-0@@ -0,0 +1,16 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +s390 Certificate Store and Functions +==================================== + +s390 Certificate Store +---------------------- + +A certificate store is implemented for s390-ccw guests to retain within +memory all certificates provided by the user via the command-line, which +are expected to be stored somewhere on the host's file system. The store +will keep track of the number of certificates, their respective size, +and a summation of the sizes. + +Note: A maximum of 64 certificates are allowed to be stored in the certificate +store.hw/s390x/cert-store.c new file +36
-0@@ -0,0 +1,211 @@ +/* +* S390 certificate store implementation +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "cert-store.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/option.h" +#include "qemu/config-file.h" +#include "hw/s390x/ebcdic.h" +#include "hw/s390x/s390-virtio-ccw.h" +#include "qemu/cutils.h" +#include "crypto/x509-utils.h" +#include "qapi/qapi-types-machine-s390x.h" + +static BootCertificatesList *s390_get_boot_certs (void ) +{ +return S390_CCW_MACHINE (qdev_get_machine ())->boot_certs; +} + +static S390IPLCertificate *init_cert_x509 (size_t size,uint8_t *raw,Error **errp) +{ +S390IPLCertificate *cert =NULL ; + g_autofree uint8_t *cert_der = NULL; +size_t der_len = size; +int rc; + + rc =qcrypto_x509_convert_cert_der (raw, size, &cert_der, &der_len, errp); +if (rc !=0 ) { +return NULL ;
hw/s390x/cert-store.c line 46
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+ } + + cert =g_new0 (S390IPLCertificate,1 ); + cert->size = size; + cert->der_size = der_len; +/* store raw pointer - ownership transfers to cert */ + cert->raw = raw; + +return cert; +}
Looks good.
Quoted 8 lines
hw/s390x/cert-store.c new file +8
-0+ +static S390IPLCertificate *init_cert (char *path,Error **errp) +{ +char *buf; +size_t size; +char vc_name[VC_NAME_LEN_BYTES ]; + g_autofree gchar *filename = NULL; +S390IPLCertificate *cert =NULL ;
hw/s390x/cert-store.c line 64
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+Error *local_err =NULL ; + + filename =g_path_get_basename (path); + +if (!g_file_get_contents (path, &buf, &size,NULL )) { +error_setg (errp,"Failed to load certificate: %s" , path); +return NULL ; + } + + cert =init_cert_x509 (size, (uint8_t *)buf, &local_err);
init_cert_x509 is small and shares some variables with this one. It might look good to merge the code into one function.
Quoted 29 lines
hw/s390x/cert-store.c new file +29
-0+if (cert ==NULL ) { +error_propagate_prepend (errp, local_err, +"Failed to initialize certificate: %s: " , path); +g_free (buf); +return NULL ; + } + +/* +* Left justified certificate name with padding on the right with blanks. +* Convert certificate name to EBCDIC. +*/ +strpadcpy (vc_name,VC_NAME_LEN_BYTES , filename,' ' ); +ebcdic_put (cert->vc_name, vc_name,VC_NAME_LEN_BYTES ); + +return cert; +} + +static void update_cert_store (S390IPLCertificateStore *cert_store, +S390IPLCertificate *cert) +{ +size_t data_buf_size; +size_t keyid_buf_size; +size_t hash_buf_size; +size_t cert_buf_size; + +/* length field is word aligned for later DIAG use */ + keyid_buf_size =ROUND_UP (CERT_KEY_ID_LEN ,4 ); + hash_buf_size =ROUND_UP (CERT_HASH_LEN ,4 ); + cert_buf_size =ROUND_UP (cert->der_size,4 );
hw/s390x/cert-store.c line 103
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+ data_buf_size = keyid_buf_size + hash_buf_size + cert_buf_size; + +if (cert_store->max_cert_size < data_buf_size) { + cert_store->max_cert_size = data_buf_size; + } + + cert_store->certs[cert_store->count] = *cert; + cert_store->total_bytes += data_buf_size; + cert_store->count++; +}
Looks good.
Quoted 6 lines
hw/s390x/cert-store.c new file +6
-0+ +static GPtrArray *get_cert_paths (Error **errp) +{ +struct stat st; +BootCertificatesList *path_list =NULL ; +BootCertificatesList *list =NULL ;
hw/s390x/cert-store.c line 119
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+gchar *cert_path; +GDir *dir =NULL ; +const gchar *filename; +g_autoptr (GError) err =NULL ; +g_autoptr (GPtrArray) cert_path_builder =g_ptr_array_new_full (0 , g_free); + + path_list =s390_get_boot_certs (); +if (path_list ==NULL ) { +return g_steal_pointer (&cert_path_builder); + }
This function can be reduced by removing the check above. The loop will be skipped when list = NULL and the same return g_steal_pointer(&cert_path_builder) is invoked at the end of the function.
Additionally, the loop could be simplified to:
```
list = s390_get_boot_certs();
while (list) {
...
list = list->next;
}
```Quoted 30 lines
hw/s390x/cert-store.c new file +30
-0+ +for (list = path_list; list; list = list->next) { + cert_path = list->value->path; + +if (g_strcmp0 (cert_path,"" ) ==0 ) { +error_setg (errp,"Empty path in certificate path list is not allowed" ); + goto fail; + } + +if (stat (cert_path, &st) !=0 ) { +error_setg (errp,"Failed to stat path '%s': %s" , + cert_path,g_strerror (errno)); + goto fail; + } + +if (S_ISREG (st.st_mode)) { +if (!g_str_has_suffix (cert_path,".pem" )) { +error_setg (errp,"Certificate file '%s' must have a .pem extension" , + cert_path); + goto fail; + } + +g_ptr_array_add (cert_path_builder,g_strdup (cert_path)); + }else if (S_ISDIR (st.st_mode)) { + dir =g_dir_open (cert_path,0 , &err); +if (dir ==NULL ) { +error_setg (errp,"Failed to open directory '%s': %s" , + cert_path, err->message); + + goto fail;
hw/s390x/cert-store.c line 159
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+ } + +while ((filename =g_dir_read_name (dir))) { +if (g_str_has_suffix (filename,".pem" )) { +g_ptr_array_add (cert_path_builder, +g_build_filename (cert_path, filename,NULL )); + } + } + +g_dir_close (dir);
Does this mean that an empty directory or one that does not contain any .pem files is allowed? Should at least a warning message should be printed? Also, if a file found within the directory is not
Another approach is to first iterate through each directory and resolve the path of *every file* and append them to the end of list (ignoring sub directories). Throw a warn/err if empty dir. Then continue to iterate each file in list.
Quoted 42 lines
hw/s390x/cert-store.c new file +42
-0+ }else { +error_setg (errp,"Path '%s' is neither a file nor a directory" , cert_path); + goto fail; + } + } + +qapi_free_BootCertificatesList (path_list); +return g_steal_pointer (&cert_path_builder); + +fail: +qapi_free_BootCertificatesList (path_list); +return NULL ; +} + +void s390_ipl_create_cert_store (S390IPLCertificateStore *cert_store) +{ +GPtrArray *cert_path_builder; +Error *err =NULL ; + + cert_path_builder =get_cert_paths (&err); +if (cert_path_builder ==NULL ) { +error_report_err (err); +exit (1 ); + } + +if (cert_path_builder->len ==0 ) { +g_ptr_array_free (cert_path_builder, TRUE); +return ; + } + +if (cert_path_builder->len >MAX_CERTIFICATES -1 ) { +error_report ("Cert store exceeds maximum of %d certificates" ,MAX_CERTIFICATES ); +g_ptr_array_free (cert_path_builder, TRUE); +exit (1 ); + } + + cert_store->max_cert_size =0 ; + cert_store->total_bytes =0 ; + +for (int i =0 ; i < cert_path_builder->len; i++) { +S390IPLCertificate *cert =init_cert ((char *) cert_path_builder->pdata[i], &err); +if (!cert) {
hw/s390x/cert-store.c line 211
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+error_report_err (err); +g_ptr_array_free (cert_path_builder, TRUE); +exit (1 ); + } + +update_cert_store (cert_store, cert); + } + +g_ptr_array_free (cert_path_builder, TRUE); +}
Looks good.
Quoted 23 lines
hw/s390x/cert-store.h new file +17
-0@@ -0,0 +1,41 @@ +/* +* S390 certificate store +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_S390_CERT_STORE_H +#define HW_S390_CERT_STORE_H + +#include "hw/s390x/ipl/qipl.h" +#include "crypto/x509-utils.h" + +#define VC_NAME_LEN_BYTES 64 +
hw/s390x/cert-store.h line 27
Quoted 10 lines
hw/s390x/cert-store.h new file +10
-0+#define CERT_KEY_ID_LEN QCRYPTO_HASH_DIGEST_LEN_SHA256 +#define CERT_HASH_LEN QCRYPTO_HASH_DIGEST_LEN_SHA256 + +struct S390IPLCertificate { +uint8_t vc_name[VC_NAME_LEN_BYTES ]; +size_t size; +size_t der_size; +uint8_t *raw; +}; +typedef struct S390IPLCertificate S390IPLCertificate ;
Since the S390IPLCertificate data structures are abstract from the architected DIAG 320 data structures, it would make more sense to use "name" instead of using "vc" anywhere. e.g.
VC_NAME_LEN_BYTES -> CERT_NAME_LEN_MAX (or some variation of that)
vc_name -> name
hw/s390x/cert-store.h line 31
Quoted 4 lines
hw/s390x/cert-store.h new file +4
-0+ +struct S390IPLCertificateStore { +uint16_t count; +size_t max_cert_size;
"max" makes it seem like a hard limitation is defined for the cert size.
"largest_cert_size" is more clear.
Quoted 29 lines
hw/s390x/cert-store.h new file +10
-0+size_t total_bytes; +S390IPLCertificate certs[MAX_CERTIFICATES ]; +}; +typedef struct S390IPLCertificateStore S390IPLCertificateStore ; +QEMU_BUILD_BUG_MSG (sizeof (S390IPLCertificateStore) !=5656 , +"size of S390IPLCertificateStore is wrong" ); + +void s390_ipl_create_cert_store (S390IPLCertificateStore *cert_store); + +#endif hw/s390x/ipl.c +4
-0@@ -35,6 +35,7 @@#include "qemu/option.h" #include "qemu/ctype.h" #include "standard-headers/linux/virtio_ids.h" +#include "cert-store.h" #define KERN_IMAGE_START 0x010000UL#define LINUX_MAGIC_ADDR 0x010008UL@@ -424,6 +425,20 @@ void s390_ipl_convert_loadparm(char *ascii_lp, uint8_t *ebcdic_lp) } } +S390IPLCertificateStore *s390_ipl_get_certificate_store (void ) +{ +S390IPLState *ipl =get_ipl_device ();
hw/s390x/ipl.c void s390_ipl_convert_loadparm(char *ascii_lp, uint8_t *ebcdic_lp) · line 440
Quoted 10 lines
hw/s390x/ipl.c +10
-0+ +return &ipl->cert_store; +} + +static bool s390_has_certificate (void ) +{ +S390IPLState *ipl =get_ipl_device (); + +return ipl->cert_store.count >0 ; +}
With the suggestion below taken into account, neither of these functions are invoked in this patch. The typical approach is to introduce a function in the same patch as its first use.
hw/s390x/ipl.c void s390_ipl_prepare_cpu(S390CPU *cpu) · line 740
Quoted 12 lines
hw/s390x/ipl.c +5
-0+static bool s390_build_iplb (DeviceState *dev_st,IplParameterBlock *iplb) {CcwDevice *ccw_dev =NULL ;@@ -719,6 +734,10 @@ void s390_ipl_prepare_cpu(S390CPU *cpu)if (!ipl->kernel || ipl->iplb_valid) { cpu->env.psw.addr = ipl->bios_start_addr; +/* initialize cert store if it's empty */ +if (!s390_has_certificate ()) { +s390_ipl_create_cert_store (&ipl->cert_store); + }
The s390_has_certificate check does not appear to make much sense to be nested under the !ipl->kernel || ipl->iplb_valid check -- the cert store creation is not dependent on either variable.
Firstly, the cert count check could be moved as one of the first things in function s390_ipl_create_cert_store:
```
/* If cert store is already populated, then no work to do */
if (ipl->cert_store.count) {
return;
}
```Then invoke s390_ipl_create_cert_store(&ipl->cert_store) probably before the if (!ipl->kernel || ipl->iplb_valid) check.
Quoted 56 lines
hw/s390x/ipl.c +0
-0if (!ipl->iplb_valid) { ipl->iplb_valid =s390_init_all_iplbs (ipl); }else {hw/s390x/ipl.h +3
-0@@ -13,6 +13,7 @@#ifndef HW_S390_IPL_H #define HW_S390_IPL_H +#include "cert-store.h" #include "cpu.h" #include "exec/target_page.h" #include "system/address-spaces.h" @@ -35,6 +36,7 @@ int s390_ipl_pv_unpack(struct S390PVResponse *pv_resp);void s390_ipl_prepare_cpu (S390CPU *cpu);IplParameterBlock *s390_ipl_get_iplb (void );IplParameterBlock *s390_ipl_get_iplb_pv (void ); +S390IPLCertificateStore *s390_ipl_get_certificate_store (void );enum s390_reset {/* default is a reset not triggered by a CPU e.g. issued by QMP */ @@ -64,6 +66,7 @@ struct S390IPLState {IplParameterBlock iplb;IplParameterBlock iplb_pv;QemuIplParameters qipl; +S390IPLCertificateStore cert_store;uint64_t start_addr;uint64_t compat_start_addr;uint64_t bios_start_addr;hw/s390x/meson.build +1
-0@@ -17,6 +17,7 @@ s390x_ss.add(files( 'sclpcpu.c', 'sclpquiesce.c', 'tod.c', + 'cert-store.c', )) s390x_ss.add(when: 'CONFIG_KVM', if_true: files( 'tod-kvm.c',include/hw/s390x/ipl/qipl.h +2
-0@@ -20,6 +20,8 @@#define LOADPARM_LEN 8#define NO_LOADPARM "\0\0\0\0\0\0\0\0" +#define MAX_CERTIFICATES 64 +/* * The QEMU IPL Parameters will be stored at absolute address * 204 (0xcc) which means it is 32-bit word aligned but not
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 04/29] hw/s390x/ipl: Create certificate store
Err, correction to my cut-off sentence below. Sorry about that.
On 1/7/26 17:18, Collin Walling wrote:
[...]
Quoted 31 lines
Quoted 31 lines
hw/s390x/cert-store.c new file +31
-0+for (list = path_list; list; list = list->next) { + cert_path = list->value->path; + +if (g_strcmp0 (cert_path,"" ) ==0 ) { +error_setg (errp,"Empty path in certificate path list is not allowed" ); + goto fail; + } + +if (stat (cert_path, &st) !=0 ) { +error_setg (errp,"Failed to stat path '%s': %s" , + cert_path,g_strerror (errno)); + goto fail; + } + +if (S_ISREG (st.st_mode)) { +if (!g_str_has_suffix (cert_path,".pem" )) { +error_setg (errp,"Certificate file '%s' must have a .pem extension" , + cert_path); + goto fail; + } + +g_ptr_array_add (cert_path_builder,g_strdup (cert_path)); + }else if (S_ISDIR (st.st_mode)) { + dir =g_dir_open (cert_path,0 , &err); +if (dir ==NULL ) { +error_setg (errp,"Failed to open directory '%s': %s" , + cert_path, err->message); + + goto fail; + } +
Quoted 8 lines
hw/s390x/cert-store.c line 159
Quoted 8 lines
hw/s390x/cert-store.c new file +8
-0+while ((filename =g_dir_read_name (dir))) { +if (g_str_has_suffix (filename,".pem" )) { +g_ptr_array_add (cert_path_builder, +g_build_filename (cert_path, filename,NULL )); + } + } + +g_dir_close (dir);
Does this mean that an empty directory or one that does not contain any .pem files is allowed? Should at least a warning message should be printed? Also, if a file found within the directory is not a .pem, should that be reported as well?
Another approach is to first iterate through each directory and resolve the path of *every file* and append them to the end of list ignoring sub directories). Throw a warn/err if empty dir. Then continue to iterate each file in list.
Quoted 13 lines
Quoted 13 lines
hw/s390x/cert-store.c new file +13
-0+ }else { +error_setg (errp,"Path '%s' is neither a file nor a directory" , cert_path); + goto fail; + } + } + +qapi_free_BootCertificatesList (path_list); +return g_steal_pointer (&cert_path_builder); + +fail: +qapi_free_BootCertificatesList (path_list); +return NULL ; +}
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 04/29] hw/s390x/ipl: Create certificate store
In reply to Zhuoying CaiQuoted 10 lines · Zhuoying Cai
Create a certificate store for boot certificates used for secure IPL.
Load certificates from the
boot-certsparameter of s390-ccw-virtio machine type option into the cert store.Currently, only X.509 certificates in PEM format are supported, as the QEMU command line accepts certificates in PEM format only.
Signed-off-by Zhuoying Cai <[email protected]>---
...
Quoted 37 lines
hw/s390x/cert-store.c new file +31
-0@@ -0,0 +1,211 @@ +/* +* S390 certificate store implementation +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "cert-store.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/option.h" +#include "qemu/config-file.h" +#include "hw/s390x/ebcdic.h" +#include "hw/s390x/s390-virtio-ccw.h" +#include "qemu/cutils.h" +#include "crypto/x509-utils.h" +#include "qapi/qapi-types-machine-s390x.h" + +static BootCertificatesList *s390_get_boot_certs (void ) +{ +return S390_CCW_MACHINE (qdev_get_machine ())->boot_certs; +} + +static S390IPLCertificate *init_cert_x509 (size_t size,uint8_t *raw,Error **errp) +{ +S390IPLCertificate *cert =NULL ; + g_autofree uint8_t *cert_der = NULL; +size_t der_len = size;
hw/s390x/cert-store.c line 41
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+int rc; + + rc =qcrypto_x509_convert_cert_der (raw, size, &cert_der, &der_len, errp); +if (rc !=0 ) { +return NULL ; + } + + cert =g_new0 (S390IPLCertificate,1 ); + cert->size = size; + cert->der_size = der_len;
Why is only der_len stored here, but cert_der is silently discarded? Could you please add a comment explaining this?
Quoted 47 lines
hw/s390x/cert-store.c new file +47
-0+/* store raw pointer - ownership transfers to cert */ + cert->raw = raw; + +return cert; +} + +static S390IPLCertificate *init_cert (char *path,Error **errp) +{ +char *buf; +size_t size; +char vc_name[VC_NAME_LEN_BYTES ]; + g_autofree gchar *filename = NULL; +S390IPLCertificate *cert =NULL ; +Error *local_err =NULL ; + + filename =g_path_get_basename (path); + +if (!g_file_get_contents (path, &buf, &size,NULL )) { +error_setg (errp,"Failed to load certificate: %s" , path); +return NULL ; + } + + cert =init_cert_x509 (size, (uint8_t *)buf, &local_err); +if (cert ==NULL ) { +error_propagate_prepend (errp, local_err, +"Failed to initialize certificate: %s: " , path); +g_free (buf); +return NULL ; + } + +/* +* Left justified certificate name with padding on the right with blanks. +* Convert certificate name to EBCDIC. +*/ +strpadcpy (vc_name,VC_NAME_LEN_BYTES , filename,' ' ); +ebcdic_put (cert->vc_name, vc_name,VC_NAME_LEN_BYTES ); + +return cert; +} + +static void update_cert_store (S390IPLCertificateStore *cert_store, +S390IPLCertificate *cert) +{ +size_t data_buf_size; +size_t keyid_buf_size; +size_t hash_buf_size; +size_t cert_buf_size;
hw/s390x/cert-store.c line 98
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+ +/* length field is word aligned for later DIAG use */ + keyid_buf_size =ROUND_UP (CERT_KEY_ID_LEN ,4 ); + hash_buf_size =ROUND_UP (CERT_HASH_LEN ,4 ); + cert_buf_size =ROUND_UP (cert->der_size,4 ); + data_buf_size = keyid_buf_size + hash_buf_size + cert_buf_size; + +if (cert_store->max_cert_size < data_buf_size) { + cert_store->max_cert_size = data_buf_size; + }
The next line looks like we should do a
g_assert(cert_store->count < MAX_CERTIFICATES)
here first.
hw/s390x/cert-store.c line 103
Quoted 4 lines
hw/s390x/cert-store.c new file +4
-0+ cert_store->certs[cert_store->count] = *cert; + cert_store->total_bytes += data_buf_size; + cert_store->count++; +}
Thomas
Re: [PATCH v7 04/29] hw/s390x/ipl: Create certificate store
Quoted 54 lines · Thomas Huth
Quoted 10 lines · Zhuoying Cai
Create a certificate store for boot certificates used for secure IPL.
Load certificates from the
boot-certsparameter of s390-ccw-virtio machine type option into the cert store.Currently, only X.509 certificates in PEM format are supported, as the QEMU command line accepts certificates in PEM format only.
Signed-off-by Zhuoying Cai <[email protected]>---
...
Quoted 42 lines
hw/s390x/cert-store.c new file +36
-0@@ -0,0 +1,211 @@ +/* +* S390 certificate store implementation +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "cert-store.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/option.h" +#include "qemu/config-file.h" +#include "hw/s390x/ebcdic.h" +#include "hw/s390x/s390-virtio-ccw.h" +#include "qemu/cutils.h" +#include "crypto/x509-utils.h" +#include "qapi/qapi-types-machine-s390x.h" + +static BootCertificatesList *s390_get_boot_certs (void ) +{ +return S390_CCW_MACHINE (qdev_get_machine ())->boot_certs; +} + +static S390IPLCertificate *init_cert_x509 (size_t size,uint8_t *raw,Error **errp) +{ +S390IPLCertificate *cert =NULL ; + g_autofree uint8_t *cert_der = NULL; +size_t der_len = size; +int rc; + + rc =qcrypto_x509_convert_cert_der (raw, size, &cert_der, &der_len, errp); +if (rc !=0 ) { +return NULL ;
Quoted 9 lines
hw/s390x/cert-store.c line 41
Quoted 5 lines
hw/s390x/cert-store.c new file +5
-0+ } + + cert =g_new0 (S390IPLCertificate,1 ); + cert->size = size; + cert->der_size = der_len;Why is only
der_lenstored here, butcert_deris silently discarded? Could you please add a comment explaining this?
Sure, I will add comments to explain this.
To elaborate, cert_der is only used once in handle_cert() (target/s390x/diag.c) when populating the Verification Certificate Entry (VCE), and it can be easily regenerated by calling qcrypto_x509_convert_cert_der(). Most other operations on the certificate can be performed using the raw certificate in PEM format.
For this reason, storing cert_der in S390IPLCertificate seemed unnecessary, whereas der_len is required in several places during certificate store setup, including when calculating the total size of S390IPLCertificateStore and the VCE length.
Quoted 6 lines
hw/s390x/cert-store.c line 47
Quoted 6 lines
hw/s390x/cert-store.c new file +6
-0+/* store raw pointer - ownership transfers to cert */ + cert->raw = raw; + +return cert; +} +
[...]
Thanks for the feedback!
Re: [PATCH v7 04/29] hw/s390x/ipl: Create certificate store
In reply to Zhuoying CaiQuoted 10 lines · Zhuoying Cai
Create a certificate store for boot certificates used for secure IPL.
Load certificates from the
boot-certsparameter of s390-ccw-virtio machine type option into the cert store.Currently, only X.509 certificates in PEM format are supported, as the QEMU command line accepts certificates in PEM format only.
Signed-off-by Zhuoying Cai <[email protected]>---
Claude just spotted a possible memory leak:
...
Quoted 9 lines
hw/s390x/cert-store.c new file +9
-0+static void update_cert_store (S390IPLCertificateStore *cert_store, +S390IPLCertificate *cert) +{ +size_t data_buf_size; +size_t keyid_buf_size; +size_t hash_buf_size; +size_t cert_buf_size; + +/* length field is word aligned for later DIAG use */
hw/s390x/cert-store.c line 100
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+ keyid_buf_size =ROUND_UP (CERT_KEY_ID_LEN ,4 ); + hash_buf_size =ROUND_UP (CERT_HASH_LEN ,4 ); + cert_buf_size =ROUND_UP (cert->der_size,4 ); + data_buf_size = keyid_buf_size + hash_buf_size + cert_buf_size; + +if (cert_store->max_cert_size < data_buf_size) { + cert_store->max_cert_size = data_buf_size; + } + + cert_store->certs[cert_store->count] = *cert;
This copies the cert by value (instead of storing the pointer only), so the original buffer is not used anymore afterwards ... (see below)
Quoted 3 lines
hw/s390x/cert-store.c new file +3
-0+ cert_store->total_bytes += data_buf_size; + cert_store->count++; +}
...
Quoted 24 lines
hw/s390x/cert-store.c new file +24
-0+void s390_ipl_create_cert_store (S390IPLCertificateStore *cert_store) +{ +GPtrArray *cert_path_builder; +Error *err =NULL ; + + cert_path_builder =get_cert_paths (&err); +if (cert_path_builder ==NULL ) { +error_report_err (err); +exit (1 ); + } + +if (cert_path_builder->len ==0 ) { +g_ptr_array_free (cert_path_builder, TRUE); +return ; + } + +if (cert_path_builder->len >MAX_CERTIFICATES -1 ) { +error_report ("Cert store exceeds maximum of %d certificates" ,MAX_CERTIFICATES ); +g_ptr_array_free (cert_path_builder, TRUE); +exit (1 ); + } + + cert_store->max_cert_size =0 ; + cert_store->total_bytes =0 ;
hw/s390x/cert-store.c line 207
Quoted 10 lines
hw/s390x/cert-store.c new file +10
-0+ +for (int i =0 ; i < cert_path_builder->len; i++) { +S390IPLCertificate *cert =init_cert ((char *) cert_path_builder->pdata[i], &err); +if (!cert) { +error_report_err (err); +g_ptr_array_free (cert_path_builder, TRUE); +exit (1 ); + } + +update_cert_store (cert_store, cert);
... so you should free cert here to avoid leaking memory!
hw/s390x/cert-store.c line 188
Quoted 4 lines
hw/s390x/cert-store.c new file +4
-0+ } + +g_ptr_array_free (cert_path_builder, TRUE); +}
Thomas
PATCH v7 5/29 s390x/diag: Introduce DIAG 320 for Certificate Store Facility
In reply to Zhuoying CaiDIAGNOSE 320 is introduced to support Certificate Store (CS) Facility, which includes operations such as query certificate storage information and provide certificates in the certificate store.
Currently, only subcode 0 is supported with this patch, which is used to query the Installed Subcodes Mask (ISM).
This subcode is only supported when the CS facility is enabled.
Availability of CS facility is determined by byte 134 bit 5 of the SCLP Read Info block. Byte 134's facilities cannot be represented without the availability of the extended-length-SCCB, so add it as a check for consistency.
Note: secure IPL is not available for Secure Execution (SE) guests, as their images are already integrity protected, and an additional protection of the kernel by secure IPL is not necessary.
This feature is available starting with the gen16 CPU model.
--- docs/specs/s390x-secure-ipl.rst | 12 ++++++++ include/hw/s390x/ipl/diag320.h | 20 +++++++++++++ target/s390x/cpu_features.c | 1 + target/s390x/cpu_features_def.h.inc | 1 + target/s390x/cpu_models.c | 2 ++ target/s390x/diag.c | 44 +++++++++++++++++++++++++++++ target/s390x/gen-features.c | 3 ++ target/s390x/kvm/kvm.c | 16 +++++++++++ target/s390x/s390x-internal.h | 2 ++ target/s390x/tcg/misc_helper.c | 7 +++++ 10 files changed, 108 insertions(+) create mode 100644 include/hw/s390x/ipl/diag320.h
docs/specs/s390x-secure-ipl.rst +12 -0
@@ -14,3 +14,15 @@ and a summation of the sizes. Note: A maximum of 64 certificates are allowed to be stored in the certificate store. + +DIAGNOSE function code 'X'320' - Certificate Store Facility +----------------------------------------------------------- + +DIAGNOSE 'X'320' is used to provide support for userspace to directly +query the s390 certificate store. Userspace may be the s390-ccw BIOS or +the guest kernel. + +Subcode 0 - query installed subcodes + Returns a 256-bit installed subcodes mask (ISM) stored in the installed + subcodes block (ISB). This mask indicates which subcodes are currently + installed and available for use.
include/hw/s390x/ipl/diag320.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* S/390 DIAGNOSE 320 definitions and structures +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef S390X_DIAG320_H +#define S390X_DIAG320_H + +#define DIAG_320_SUBC_QUERY_ISM 0 + +#define DIAG_320_RC_OK 0x0001 +#define DIAG_320_RC_NOT_SUPPORTED 0x0102 + +#define DIAG_320_ISM_QUERY_SUBCODES 0x80000000 + +#endif
target/s390x/cpu_features.c +1 -0
@@ -147,6 +147,7 @@ void s390_fill_feat_block(const S390FeatBitmap features, S390FeatType type,break ;case S390_FEAT_TYPE_SCLP_FAC134 :clear_be_bit (s390_feat_def (S390_FEAT_DIAG_318 )->bit, data); +clear_be_bit (s390_feat_def (S390_FEAT_CERT_STORE )->bit, data);break ;default :return ;
target/s390x/cpu_features_def.h.inc +1 -0
@@ -138,6 +138,7 @@ DEF_FEAT(SIE_IBS, "ibs", SCLP_CONF_CHAR_EXT, 10, "SIE: Interlock-and-broadcast-s /* Features exposed via SCLP SCCB Facilities byte 134 (bit numbers relative to byte-134) */ DEF_FEAT(DIAG_318, "diag318", SCLP_FAC134, 0, "Control program name and version codes") +DEF_FEAT(CERT_STORE, "cstore", SCLP_FAC134, 5, "Provide Certificate Store functions") /* Features exposed via SCLP CPU info. */ DEF_FEAT(SIE_F2, "sief2", SCLP_CPU, 4, "SIE: interception format 2 (Virtual SIE)")
target/s390x/cpu_models.c +2 -0
@@ -248,6 +248,7 @@ bool s390_has_feat(S390Feat feat)if (s390_is_pv ()) {switch (feat) {case S390_FEAT_DIAG_318 : +case S390_FEAT_CERT_STORE :case S390_FEAT_HPMA2 :case S390_FEAT_SIE_F2 :case S390_FEAT_SIE_SKEY :
@@ -505,6 +506,7 @@ static void check_consistency(const S390CPUModel *model) { S390_FEAT_PTFF_STOUE, S390_FEAT_MULTIPLE_EPOCH }, { S390_FEAT_AP_QUEUE_INTERRUPT_CONTROL, S390_FEAT_AP }, { S390_FEAT_DIAG_318, S390_FEAT_EXTENDED_LENGTH_SCCB }, + { S390_FEAT_CERT_STORE, S390_FEAT_EXTENDED_LENGTH_SCCB }, { S390_FEAT_NNPA, S390_FEAT_VECTOR }, { S390_FEAT_RDP, S390_FEAT_LOCAL_TLB_CLEARING }, { S390_FEAT_UV_FEAT_AP, S390_FEAT_AP },
target/s390x/diag.c +44 -0
53 lines not on this page · raw message
target/s390x/gen-features.c +3 -0
@@ -720,6 +720,7 @@ static uint16_t full_GEN16_GA1[] = {S390_FEAT_PAIE ,S390_FEAT_UV_FEAT_AP ,S390_FEAT_UV_FEAT_AP_INTR , + S390_FEAT_CERT_STORE, }; static uint16_t full_GEN17_GA1[] = {
@@ -919,6 +920,8 @@ static uint16_t qemu_MAX[] = {S390_FEAT_KIMD_SHA_512 ,S390_FEAT_KLMD_SHA_512 ,S390_FEAT_PRNO_TRNG , +S390_FEAT_EXTENDED_LENGTH_SCCB , + S390_FEAT_CERT_STORE, };/****** END FEATURE DEFS ******/
target/s390x/kvm/kvm.c +16 -0
@@ -98,6 +98,7 @@#define DIAG_TIMEREVENT 0x288#define DIAG_IPL 0x308#define DIAG_SET_CONTROL_PROGRAM_CODES 0x318 +#define DIAG_CERT_STORE 0x320#define DIAG_KVM_HYPERCALL 0x500#define DIAG_KVM_BREAKPOINT 0x501
@@ -1560,6 +1561,16 @@ static void handle_diag_318(S390CPU *cpu, struct kvm_run *run) } } +static void kvm_handle_diag_320 (S390CPU *cpu,struct kvm_run *run) +{ +uint64_t r1, r3; + + r1 = (run->s390_sieic.ipa &0x00f0 ) >>4 ; + r3 = run->s390_sieic.ipa &0x000f ; + +handle_diag_320 (&cpu->env, r1, r3,RA_IGNORED ); +} +#define DIAG_KVM_CODE_MASK 0x000000000000ffffstatic int handle_diag (S390CPU *cpu,struct kvm_run *run,uint32_t ipb)
@@ -1590,6 +1601,9 @@ static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)case DIAG_KVM_BREAKPOINT : r =handle_sw_breakpoint (cpu, run);break ; +case DIAG_CERT_STORE : +kvm_handle_diag_320 (cpu, run); +break ;default :trace_kvm_insn_diag (func_code);kvm_s390_program_interrupt (cpu,PGM_SPECIFICATION );
@@ -2490,6 +2504,8 @@ bool kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)set_bit (S390_FEAT_DIAG_318 , model->features); } +set_bit (S390_FEAT_CERT_STORE , model->features); +/* Test for Ultravisor features that influence secure guest behavior */ query_uv_feat_guest (model->features);
target/s390x/s390x-internal.h +2 -0
@@ -388,6 +388,8 @@ int mmu_translate_real(CPUS390XState *env, target_ulong raddr, int rw,int handle_diag_288 (CPUS390XState *env,uint64_t r1,uint64_t r3);void handle_diag_308 (CPUS390XState *env,uint64_t r1,uint64_t r3,uintptr_t ra); +void handle_diag_320 (CPUS390XState *env,uint64_t r1,uint64_t r3, +uintptr_t ra);/* translate.c */
target/s390x/tcg/misc_helper.c +7 -0
@@ -142,6 +142,13 @@ void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num)/* time bomb (watchdog) */ r =handle_diag_288 (env, r1, r3);break ; +case 0x320 : +/* cert store */ +bql_lock (); +handle_diag_320 (env, r1, r3,GETPC ()); +bql_unlock (); + r =0 ; +break ;default : r =-1 ;break ;
Signature · 1 line
2.51.1
Re: [PATCH v7 05/29] s390x/diag: Introduce DIAG 320 for Certificate Store Facility
Quoted 25 lines · Zhuoying Cai
DIAGNOSE 320 is introduced to support Certificate Store (CS) Facility, which includes operations such as query certificate storage information and provide certificates in the certificate store.
Currently, only subcode 0 is supported with this patch, which is used to query the Installed Subcodes Mask (ISM).
This subcode is only supported when the CS facility is enabled.
Availability of CS facility is determined by byte 134 bit 5 of the SCLP Read Info block. Byte 134's facilities cannot be represented without the availability of the extended-length-SCCB, so add it as a check for consistency.
Note: secure IPL is not available for Secure Execution (SE) guests, as their images are already integrity protected, and an additional protection of the kernel by secure IPL is not necessary.
This feature is available starting with the gen16 CPU model.
Signed-off-by Zhuoying Cai <[email protected]>Reviewed-by Collin Walling <[email protected]>Reviewed-by Farhan Ali <[email protected]>---
[...]
Quoted 12 lines
target/s390x/diag.c +12
-0+ +void handle_diag_320 (CPUS390XState *env,uint64_t r1,uint64_t r3,uintptr_t ra) +{ +S390CPU *cpu =env_archcpu (env); +uint64_t subcode = env->regs[r3]; +uint64_t addr = env->regs[r1]; + +if (env->psw.mask &PSW_MASK_PSTATE ) { +s390_program_interrupt (env,PGM_PRIVILEGED , ra); +return ; + } +
target/s390x/diag.c out: · line 216
Quoted 10 lines
target/s390x/diag.c +10
-0+if (!s390_has_feat (S390_FEAT_CERT_STORE )) { +s390_program_interrupt (env,PGM_SPECIFICATION , ra); +return ; + } + +if ((subcode & ~0x000ffULL ) || (r1 &1 )) { +s390_program_interrupt (env,PGM_SPECIFICATION , ra); +return ; + } +
Nit: merge the above two ifs since they both have the same output.
This does not bar my r-b, but wanted to make note of it.
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 05/29] s390x/diag: Introduce DIAG 320 for Certificate Store Facility
In reply to Zhuoying CaiQuoted 25 lines · Zhuoying Cai
DIAGNOSE 320 is introduced to support Certificate Store (CS) Facility, which includes operations such as query certificate storage information and provide certificates in the certificate store.
Currently, only subcode 0 is supported with this patch, which is used to query the Installed Subcodes Mask (ISM).
This subcode is only supported when the CS facility is enabled.
Availability of CS facility is determined by byte 134 bit 5 of the SCLP Read Info block. Byte 134's facilities cannot be represented without the availability of the extended-length-SCCB, so add it as a check for consistency.
Note: secure IPL is not available for Secure Execution (SE) guests, as their images are already integrity protected, and an additional protection of the kernel by secure IPL is not necessary.
This feature is available starting with the gen16 CPU model.
Signed-off-by Zhuoying Cai <[email protected]>Reviewed-by Collin Walling <[email protected]>Reviewed-by Farhan Ali <[email protected]>---
...
target/s390x/cpu_features_def.h.inc DEF_FEAT(SIE_IBS, "ibs", SCLP_CONF_CHAR_EXT, 10, "SIE: Interlock-and-broadcast-s · line 141
Quoted 9 lines
target/s390x/cpu_features_def.h.inc +1
-0@@ -138,6 +138,7 @@ DEF_FEAT(SIE_IBS, "ibs", SCLP_CONF_CHAR_EXT, 10, "SIE: Interlock-and-broadcast-s /* Features exposed via SCLP SCCB Facilities byte 134 (bit numbers relative to byte-134) */ DEF_FEAT(DIAG_318, "diag318", SCLP_FAC134, 0, "Control program name and version codes") +DEF_FEAT(CERT_STORE, "cstore", SCLP_FAC134, 5, "Provide Certificate Store functions")
Please drop "Provide" from the description text. We don't use such a verb for any of the other features, so this would be inconsistent now.
Thanks,
Thomas
PATCH v7 6/29 s390x/diag: Refactor address validation check from diag308_parm_check
In reply to Zhuoying CaiCreate a function to validate the address parameter of DIAGNOSE.
Refactor the function for reuse in the next patch, which allows address validation in read or write operation of DIAGNOSE.
--- target/s390x/diag.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
target/s390x/diag.c +7 -3
@@ -26,6 +26,12 @@#include "qemu/error-report.h" +static inline bool diag_parm_addr_valid (uint64_t addr,size_t size,bool write) +{ +return address_space_access_valid (&address_space_memory, addr, + size, write,MEMTXATTRS_UNSPECIFIED ); +} +int handle_diag_288 (CPUS390XState *env,uint64_t r1,uint64_t r3) {uint64_t func = env->regs[r1];
@@ -65,9 +71,7 @@ static int diag308_parm_check(CPUS390XState *env, uint64_t r1, uint64_t addr,s390_program_interrupt (env,PGM_SPECIFICATION , ra);return -1 ; } -if (!address_space_access_valid (&address_space_memory, addr, -sizeof (IplParameterBlock), write, -MEMTXATTRS_UNSPECIFIED )) { +if (!diag_parm_addr_valid (addr,sizeof (IplParameterBlock), write)) {s390_program_interrupt (env,PGM_ADDRESSING , ra);return -1 ; }
Signature · 1 line
2.51.1
Re: [PATCH v7 06/29] s390x/diag: Refactor address validation check from diag308_parm_check
Quoted 29 lines · Zhuoying Cai
Create a function to validate the address parameter of DIAGNOSE.
Refactor the function for reuse in the next patch, which allows address validation in read or write operation of DIAGNOSE.
Signed-off-by Zhuoying Cai <[email protected]>Reviewed-by Farhan Ali <[email protected]>--- target/s390x/diag.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)target/s390x/diag.c +6
-0@@ -26,6 +26,12 @@#include "qemu/error-report.h" +static inline bool diag_parm_addr_valid (uint64_t addr,size_t size,bool write) +{ +return address_space_access_valid (&address_space_memory, addr, + size, write,MEMTXATTRS_UNSPECIFIED ); +} +int handle_diag_288 (CPUS390XState *env,uint64_t r1,uint64_t r3) {uint64_t func = env->regs[r1];
target/s390x/diag.c static int diag308_parm_check(CPUS390XState *env, uint64_t r1, uint64_t addr, · line 77
Quoted 10 lines
target/s390x/diag.c +1
-3@@ -65,9 +71,7 @@ static int diag308_parm_check(CPUS390XState *env, uint64_t r1, uint64_t addr,s390_program_interrupt (env,PGM_SPECIFICATION , ra);return -1 ; } -if (!address_space_access_valid (&address_space_memory, addr, -sizeof (IplParameterBlock), write, -MEMTXATTRS_UNSPECIFIED )) { +if (!diag_parm_addr_valid (addr,sizeof (IplParameterBlock), write)) {s390_program_interrupt (env,PGM_ADDRESSING , ra);return -1 ; }
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 06/29] s390x/diag: Refactor address validation check from diag308_parm_check
In reply to Zhuoying CaiQuoted 29 lines · Zhuoying Cai
Create a function to validate the address parameter of DIAGNOSE.
Refactor the function for reuse in the next patch, which allows address validation in read or write operation of DIAGNOSE.
Signed-off-by Zhuoying Cai <[email protected]>Reviewed-by Farhan Ali <[email protected]>--- target/s390x/diag.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)target/s390x/diag.c +6
-0@@ -26,6 +26,12 @@#include "qemu/error-report.h" +static inline bool diag_parm_addr_valid (uint64_t addr,size_t size,bool write) +{ +return address_space_access_valid (&address_space_memory, addr, + size, write,MEMTXATTRS_UNSPECIFIED ); +} +int handle_diag_288 (CPUS390XState *env,uint64_t r1,uint64_t r3) {uint64_t func = env->regs[r1];
target/s390x/diag.c static int diag308_parm_check(CPUS390XState *env, uint64_t r1, uint64_t addr, · line 77
Quoted 10 lines
target/s390x/diag.c +1
-3@@ -65,9 +71,7 @@ static int diag308_parm_check(CPUS390XState *env, uint64_t r1, uint64_t addr,s390_program_interrupt (env,PGM_SPECIFICATION , ra);return -1 ; } -if (!address_space_access_valid (&address_space_memory, addr, -sizeof (IplParameterBlock), write, -MEMTXATTRS_UNSPECIFIED )) { +if (!diag_parm_addr_valid (addr,sizeof (IplParameterBlock), write)) {s390_program_interrupt (env,PGM_ADDRESSING , ra);return -1 ; }
PATCH v7 7/29 s390x/diag: Implement DIAG 320 subcode 1
In reply to Zhuoying CaiDIAG 320 subcode 1 provides information needed to determine the amount of storage to store one or more certificates from the certificate store.
Upon successful completion, this subcode returns information of the current cert store, such as the number of certificates stored and allowed in the cert store, amount of space may need to be allocate to store a certificate, etc for verification-certificate blocks (VCBs).
The subcode value is denoted by setting the left-most bit of an 8-byte field.
The verification-certificate-storage-size block (VCSSB) contains the output data when the operation completes successfully. A VCSSB length of 4 indicates that no certificate are available in the cert store.
--- docs/specs/s390x-secure-ipl.rst | 12 +++++++ include/hw/s390x/ipl/diag320.h | 22 ++++++++++++ target/s390x/diag.c | 59 ++++++++++++++++++++++++++++++++- 3 files changed, 92 insertions(+), 1 deletion(-)
docs/specs/s390x-secure-ipl.rst +12 -0
@@ -26,3 +26,15 @@ Subcode 0 - query installed subcodes Returns a 256-bit installed subcodes mask (ISM) stored in the installed subcodes block (ISB). This mask indicates which subcodes are currently installed and available for use. + +Subcode 1 - query verification certificate storage information + Provides the information required to determine the amount of memory needed + to store one or more verification-certificates (VCs) from the certificate + store (CS). + + Upon successful completion, this subcode returns various storage size values + for verification-certificate blocks (VCBs). + + The output is returned in the verification-certificate-storage-size block + (VCSSB). A VCSSB length of 4 indicates that no certificates are available + in the CS.
include/hw/s390x/ipl/diag320.h +22 -0
@@ -11,10 +11,32 @@#define S390X_DIAG320_H #define DIAG_320_SUBC_QUERY_ISM 0 +#define DIAG_320_SUBC_QUERY_VCSI 1#define DIAG_320_RC_OK 0x0001#define DIAG_320_RC_NOT_SUPPORTED 0x0102 +#define DIAG_320_RC_INVAL_VCSSB_LEN 0x0202#define DIAG_320_ISM_QUERY_SUBCODES 0x80000000 +#define DIAG_320_ISM_QUERY_VCSI 0x40000000 + +#define VCSSB_NO_VC 4 +#define VCSSB_MIN_LEN 128 +#define VCE_HEADER_LEN 128 +#define VCB_HEADER_LEN 64 + +struct VCStorageSizeBlock { +uint32_t length; +uint8_t reserved0[3 ]; +uint8_t version; +uint32_t reserved1[6 ]; +uint16_t total_vc_ct; +uint16_t max_vc_ct; +uint32_t reserved3[11 ]; +uint32_t max_single_vcb_len; +uint32_t total_vcb_len; +uint32_t reserved4[10 ]; +}; +typedef struct VCStorageSizeBlock VCStorageSizeBlock ;#endif
target/s390x/diag.c +58 -1
82 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v7 07/29] s390x/diag: Implement DIAG 320 subcode 1
Quoted 12 lines · Zhuoying Cai
DIAG 320 subcode 1 provides information needed to determine the amount of storage to store one or more certificates from the certificate store.
Upon successful completion, this subcode returns information of the current cert store, such as the number of certificates stored and allowed in the cert store, amount of space may need to be allocate to store a certificate, etc for verification-certificate blocks (VCBs).
The subcode value is denoted by setting the left-most bit of an 8-byte field.
Quoted 8 lines
The verification-certificate-storage-size block (VCSSB) contains the output data when the operation completes successfully. A VCSSB length of 4 indicates that no certificate are available in the cert store.
Signed-off-by Zhuoying Cai <[email protected]>Reviewed-by Farhan Ali <[email protected]>---
LGTM
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 07/29] s390x/diag: Implement DIAG 320 subcode 1
In reply to Zhuoying CaiQuoted 45 lines · Zhuoying Cai
DIAG 320 subcode 1 provides information needed to determine the amount of storage to store one or more certificates from the certificate store.
Upon successful completion, this subcode returns information of the current cert store, such as the number of certificates stored and allowed in the cert store, amount of space may need to be allocate to store a certificate, etc for verification-certificate blocks (VCBs).
The subcode value is denoted by setting the left-most bit of an 8-byte field.
The verification-certificate-storage-size block (VCSSB) contains the output data when the operation completes successfully. A VCSSB length of 4 indicates that no certificate are available in the cert store.
Signed-off-by Zhuoying Cai <[email protected]>Reviewed-by Farhan Ali <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 12 +++++++ include/hw/s390x/ipl/diag320.h | 22 ++++++++++++ target/s390x/diag.c | 59 ++++++++++++++++++++++++++++++++- 3 files changed, 92 insertions(+), 1 deletion(-)docs/specs/s390x-secure-ipl.rst +12
-0@@ -26,3 +26,15 @@ Subcode 0 - query installed subcodes Returns a 256-bit installed subcodes mask (ISM) stored in the installed subcodes block (ISB). This mask indicates which subcodes are currently installed and available for use. + +Subcode 1 - query verification certificate storage information + Provides the information required to determine the amount of memory needed + to store one or more verification-certificates (VCs) from the certificate + store (CS). + + Upon successful completion, this subcode returns various storage size values + for verification-certificate blocks (VCBs). + + The output is returned in the verification-certificate-storage-size block + (VCSSB). A VCSSB length of 4 indicates that no certificates are available + in the CS.
...
Quoted 12 lines
target/s390x/diag.c +4
-0@@ -197,11 +197,50 @@ out: } } +static int handle_diag320_query_vcsi (S390CPU *cpu,uint64_t addr,uint64_t r1, +uintptr_t ra,S390IPLCertificateStore *qcs) +{ + g_autofree VCStorageSizeBlock *vcssb = NULL;
target/s390x/diag.c out: · line 213
Quoted 10 lines
target/s390x/diag.c +10
-0+ + vcssb =g_new0 (VCStorageSizeBlock,1 ); +if (s390_cpu_virt_mem_read (cpu, addr, r1, vcssb,sizeof (*vcssb))) { +s390_cpu_virt_mem_handle_exc (cpu, ra); +return -1 ; + } + +if (be32_to_cpu (vcssb->length) <VCSSB_MIN_LEN ) { +return DIAG_320_RC_INVAL_VCSSB_LEN ; + }
Do we maybe also need to check for an upper limit for vcssb->length ? (see below...)
target/s390x/diag.c out: · line 227
Quoted 13 lines
target/s390x/diag.c +13
-0+if (!qcs->count) { + vcssb->length =cpu_to_be32 (VCSSB_NO_VC ); + }else { + vcssb->version =0 ; + vcssb->total_vc_ct =cpu_to_be16 (qcs->count); + vcssb->max_vc_ct =cpu_to_be16 (MAX_CERTIFICATES ); + vcssb->max_single_vcb_len =cpu_to_be32 (VCB_HEADER_LEN +VCE_HEADER_LEN + + qcs->max_cert_size); + vcssb->total_vcb_len =cpu_to_be32 (VCB_HEADER_LEN + qcs->count *VCE_HEADER_LEN + + qcs->total_bytes); + } + +if (s390_cpu_virt_mem_write (cpu, addr, r1, vcssb,be32_to_cpu (vcssb->length))) {
So this uses vcssb as source, but the length is controlled by a value that is provided by the guest? ... this sounds dangerous, I think we should definitely limit vcssb->length to be <= sizeof(*vcssb) !
target/s390x/diag.c out: · line 232
Quoted 5 lines
target/s390x/diag.c +5
-0+s390_cpu_virt_mem_handle_exc (cpu, ra); +return -1 ; + } +return DIAG_320_RC_OK ; +}
Thomas
PATCH v7 8/29 crypto/x509-utils: Add helper functions for DIAG 320 subcode 2
In reply to Zhuoying CaiIntroduce new helper functions to extract certificate metadata:
qcrypto_x509_check_cert_times() - validates the certificate's validity period against the current time qcrypto_x509_get_pk_algorithm() - returns the public key algorithm used in the certificate qcrypto_x509_get_cert_key_id() - extracts the key ID from the certificate qcrypto_x509_is_ecc_curve_p521() - determines the ECC public key algorithm uses P-521 curve
These functions provide support for metadata extraction and validity checking for X.509 certificates.
--- crypto/x509-utils.c | 248 ++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 73 +++++++++++ 2 files changed, 321 insertions(+)
crypto/x509-utils.c +248 -0
@@ -27,6 +27,25 @@ static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { [QCRYPTO_HASH_ALGO_RIPEMD160] = GNUTLS_DIG_RMD160, }; +static const int gnutls_to_qcrypto_pk_alg_map[] = { + [GNUTLS_PK_RSA ] =QCRYPTO_PK_ALGO_RSA , + [GNUTLS_PK_DSA ] =QCRYPTO_PK_ALGO_DSA , + [GNUTLS_PK_ECDSA ] =QCRYPTO_PK_ALGO_ECDSA , + [GNUTLS_PK_RSA_OAEP ] =QCRYPTO_PK_ALGO_RSA_OAEP , + [GNUTLS_PK_EDDSA_ED25519 ] =QCRYPTO_PK_ALGO_ED25519 , + [GNUTLS_PK_EDDSA_ED448 ] =QCRYPTO_PK_ALGO_ED448 , +}; + +static const int qcrypto_to_gnutls_keyid_flags_map[] = { + [QCRYPTO_HASH_ALGO_MD5 ] =-1 , + [QCRYPTO_HASH_ALGO_SHA1 ] =GNUTLS_KEYID_USE_SHA1 , + [QCRYPTO_HASH_ALGO_SHA224 ] =-1 , + [QCRYPTO_HASH_ALGO_SHA256 ] =GNUTLS_KEYID_USE_SHA256 , + [QCRYPTO_HASH_ALGO_SHA384 ] =-1 , + [QCRYPTO_HASH_ALGO_SHA512 ] =GNUTLS_KEYID_USE_SHA512 , + [QCRYPTO_HASH_ALGO_RIPEMD160 ] =-1 , +}; + int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size, QCryptoHashAlgo alg, uint8_t *result,
@@ -121,6 +140,207 @@ cleanup:return ret; } +int qcrypto_x509_check_cert_times (uint8_t *cert,size_t size,Error **errp) +{ +int rc; +int ret =-1 ; +gnutls_x509_crt_t crt; +gnutls_datum_t datum = {.data = cert, .size = size}; +time_t now =time (0 ); +time_t exp_time; +time_t act_time; + +if (now == ((time_t)-1 )) { +error_setg_errno (errp, errno,"Cannot get current time" ); +return ret; + } + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + exp_time =gnutls_x509_crt_get_expiration_time (crt); +if (exp_time == ((time_t)-1 )) { +error_setg (errp,"Failed to get certificate expiration time" ); + goto cleanup; + } +if (exp_time < now) { +error_setg (errp,"The certificate has expired" ); + goto cleanup; + } + + act_time =gnutls_x509_crt_get_activation_time (crt); +if (act_time == ((time_t)-1 )) { +error_setg (errp,"Failed to get certificate activation time" ); + goto cleanup; + } +if (act_time > now) { +error_setg (errp,"The certificate is not yet active" ); + goto cleanup; + } + + ret =0 ; + +cleanup: +gnutls_x509_crt_deinit (crt); +return ret; +} + +int qcrypto_x509_get_pk_algorithm (uint8_t *cert,size_t size,Error **errp) +{ +int rc; +int ret =-1 ; +unsigned int bits; +gnutls_x509_crt_t crt; +gnutls_datum_t datum = {.data = cert, .size = size}; + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_x509_crt_get_pk_algorithm (crt, &bits); +if (rc >=G_N_ELEMENTS (gnutls_to_qcrypto_pk_alg_map) || rc <0 ) { +error_setg (errp,"Unknown public key algorithm %d" , rc); + goto cleanup; + } + + ret = gnutls_to_qcrypto_pk_alg_map[rc]; + +cleanup: +gnutls_x509_crt_deinit (crt); +return ret; +} + +int qcrypto_x509_get_cert_key_id (uint8_t *cert,size_t size, +QCryptoHashAlgo hash_alg, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +int rc; +int ret =-1 ; +gnutls_x509_crt_t crt; +gnutls_datum_t datum = {.data = cert, .size = size}; + +if (hash_alg >=G_N_ELEMENTS (qcrypto_to_gnutls_hash_alg_map)) { +error_setg (errp,"Unknown hash algorithm %d" , hash_alg); +return ret; + } + +if (hash_alg >=G_N_ELEMENTS (qcrypto_to_gnutls_keyid_flags_map) || + qcrypto_to_gnutls_keyid_flags_map[hash_alg] ==-1 ) { +error_setg (errp,"Unsupported key id flag %d" , hash_alg); +return ret; + } + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + *resultlen =gnutls_hash_get_len (qcrypto_to_gnutls_hash_alg_map[hash_alg]); +if (*resultlen ==0 ) { +error_setg (errp,"Failed to get hash algorithn length: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + *result =g_malloc0 (*resultlen); +if (gnutls_x509_crt_get_key_id (crt, + qcrypto_to_gnutls_keyid_flags_map[hash_alg], + *result, resultlen) !=0 ) { +error_setg (errp,"Failed to get key ID from certificate" ); +g_clear_pointer (result, g_free); + goto cleanup; + } + + ret =0 ; + +cleanup: +gnutls_x509_crt_deinit (crt); +return ret; +} + +static int qcrypto_x509_get_ecc_curve (uint8_t *cert,size_t size,Error **errp) +{ +int rc; +int ret =-1 ; +gnutls_x509_crt_t crt; +gnutls_datum_t datum = {.data = cert, .size = size}; +gnutls_ecc_curve_t curve_id; +gnutls_datum_t x = {.data =NULL , .size =0 }; +gnutls_datum_t y = {.data =NULL , .size =0 }; + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_x509_crt_get_pk_ecc_raw (crt, &curve_id, &x, &y); +if (rc !=0 ) { +error_setg (errp,"Failed to get ECC public key curve: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + ret = curve_id; + +cleanup: +gnutls_x509_crt_deinit (crt); +gnutls_free (x.data); +gnutls_free (y.data); +return ret; +} + +int qcrypto_x509_check_ecc_curve_p521 (uint8_t *cert,size_t size,Error **errp) +{ +int curve_id; + + curve_id =qcrypto_x509_get_ecc_curve (cert, size, errp); +if (curve_id ==-1 ) { +return -1 ; + } + +if (curve_id ==GNUTLS_ECC_CURVE_INVALID ) { +error_setg (errp,"Invalid ECC curve" ); +return -1 ; + } + +if (curve_id ==GNUTLS_ECC_CURVE_SECP521R1 ) { +return 1 ; + } + +return 0 ; +} +#else /* ! CONFIG_GNUTLS */ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size,
@@ -142,4 +362,32 @@ int qcrypto_x509_convert_cert_der(uint8_t *cert, size_t size,return -1 ; } +int qcrypto_x509_check_cert_times (uint8_t *cert,size_t size,Error **errp) +{ +error_setg (errp,"GNUTLS is required to get certificate times" ); +return -1 ; +} + +int qcrypto_x509_get_pk_algorithm (uint8_t *cert,size_t size,Error **errp) +{ +error_setg (errp,"GNUTLS is required to get public key algorithm" ); +return -1 ; +} + +int qcrypto_x509_get_cert_key_id (uint8_t *cert,size_t size, +QCryptoHashAlgo hash_alg, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +error_setg (errp,"GNUTLS is required to get key ID" ); +return -1 ; +} + +int qcrypto_x509_check_ecc_curve_p521 (uint8_t *cert,size_t size,Error **errp) +{ +error_setg (errp,"GNUTLS is required to determine ecc curve" ); +return -1 ; +} +#endif /* ! CONFIG_GNUTLS */
include/crypto/x509-utils.h +73 -0
83 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v7 08/29] crypto/x509-utils: Add helper functions for DIAG 320 subcode 2
Quoted 6 lines · Zhuoying Cai
Introduce new helper functions to extract certificate metadata:
qcrypto_x509_check_cert_times()- validates the certificate's validity period against the current timeqcrypto_x509_get_pk_algorithm()- returns the public key algorithm used in the certificateqcrypto_x509_get_cert_key_id()- extracts the key ID from the certificateqcrypto_x509_is_ecc_curve_p521()- determines the ECC public key algorithm uses P-521 curve
I think this function name has changed to qcrypto_x509_check_ecc_curve_p521?
Quoted 5 lines
These functions provide support for metadata extraction and validity checking for X.509 certificates.
Signed-off-by Zhuoying Cai<[email protected]>---
The patch LGTM
Re: [PATCH v7 08/29] crypto/x509-utils: Add helper functions for DIAG 320 subcode 2
Quoted 11 lines · Farhan Ali
Quoted 6 lines · Zhuoying Cai
Introduce new helper functions to extract certificate metadata:
qcrypto_x509_check_cert_times()- validates the certificate's validity period against the current timeqcrypto_x509_get_pk_algorithm()- returns the public key algorithm used in the certificateqcrypto_x509_get_cert_key_id()- extracts the key ID from the certificateqcrypto_x509_is_ecc_curve_p521()- determines the ECC public key algorithm uses P-521 curveI think this function name has changed to
qcrypto_x509_check_ecc_curve_p521?
Thanks for pointing it out! I'll fix it in the next version.
Quoted 11 lines
Quoted 5 lines
These functions provide support for metadata extraction and validity checking for X.509 certificates.
Signed-off-by Zhuoying Cai<[email protected]>---
The patch LGTM
Reviewed-by Farhan Ali<[email protected]>
Re: [PATCH v7 08/29] crypto/x509-utils: Add helper functions for DIAG 320 subcode 2
In reply to Zhuoying CaiQuoted 21 lines · Zhuoying Cai
Introduce new helper functions to extract certificate metadata:
qcrypto_x509_check_cert_times()- validates the certificate's validity period against the current timeqcrypto_x509_get_pk_algorithm()- returns the public key algorithm used in the certificateqcrypto_x509_get_cert_key_id()- extracts the key ID from the certificateqcrypto_x509_is_ecc_curve_p521()- determines the ECC public key algorithm uses P-521 curveThese functions provide support for metadata extraction and validity checking for X.509 certificates.
Signed-off-by Zhuoying Cai <[email protected]>--- crypto/x509-utils.c | 248 ++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 73 +++++++++++ 2 files changed, 321 insertions(+)crypto/x509-utils.c +0
-0
crypto/x509-utils.c static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { · line 30
Quoted 4 lines
crypto/x509-utils.c +1
-0@@ -27,6 +27,25 @@ static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { [QCRYPTO_HASH_ALGO_RIPEMD160] = GNUTLS_DIG_RMD160, }; +static const int gnutls_to_qcrypto_pk_alg_map[] = {
Could you do me a favour and add a line like this at the beginning of this array:
[GNUTLS_PK_UNKNOWN] = QCRYPTO_PK_ALGO_UNKNOWN,
and then also set QCRYPTO_PK_ALGO_UNKNOWN = 0 in the enum in the header? That way we can be sure that zero values are not accidentally mapped to a valid algorithm.
Quoted 22 lines
crypto/x509-utils.c +18
-0+ [GNUTLS_PK_RSA] = QCRYPTO_PK_ALGO_RSA, + [GNUTLS_PK_DSA] = QCRYPTO_PK_ALGO_DSA, + [GNUTLS_PK_ECDSA] = QCRYPTO_PK_ALGO_ECDSA, + [GNUTLS_PK_RSA_OAEP] = QCRYPTO_PK_ALGO_RSA_OAEP, + [GNUTLS_PK_EDDSA_ED25519] = QCRYPTO_PK_ALGO_ED25519, + [GNUTLS_PK_EDDSA_ED448] = QCRYPTO_PK_ALGO_ED448, +}; + +static const int qcrypto_to_gnutls_keyid_flags_map[] = { + [QCRYPTO_HASH_ALGO_MD5 ] =-1 , + [QCRYPTO_HASH_ALGO_SHA1 ] =GNUTLS_KEYID_USE_SHA1 , + [QCRYPTO_HASH_ALGO_SHA224 ] =-1 , + [QCRYPTO_HASH_ALGO_SHA256 ] =GNUTLS_KEYID_USE_SHA256 , + [QCRYPTO_HASH_ALGO_SHA384 ] =-1 , + [QCRYPTO_HASH_ALGO_SHA512 ] =GNUTLS_KEYID_USE_SHA512 , + [QCRYPTO_HASH_ALGO_RIPEMD160 ] =-1 , +}; + int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size, QCryptoHashAlgo alg, uint8_t *result,
crypto/x509-utils.c cleanup: · line 149
Quoted 10 lines
crypto/x509-utils.c +7
-0@@ -121,6 +140,207 @@ cleanup:return ret; } +int qcrypto_x509_check_cert_times (uint8_t *cert,size_t size,Error **errp) +{ +int rc; +int ret =-1 ; +gnutls_x509_crt_t crt; +gnutls_datum_t datum = {.data = cert, .size = size}; +time_t now =time (0 );
time() takes a pointer, so "time(NULL)" would be more appropriate here.
Quoted 235 lines
crypto/x509-utils.c +222
-0+time_t exp_time; +time_t act_time; + +if (now == ((time_t)-1 )) { +error_setg_errno (errp, errno,"Cannot get current time" ); +return ret; + } + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + exp_time =gnutls_x509_crt_get_expiration_time (crt); +if (exp_time == ((time_t)-1 )) { +error_setg (errp,"Failed to get certificate expiration time" ); + goto cleanup; + } +if (exp_time < now) { +error_setg (errp,"The certificate has expired" ); + goto cleanup; + } + + act_time =gnutls_x509_crt_get_activation_time (crt); +if (act_time == ((time_t)-1 )) { +error_setg (errp,"Failed to get certificate activation time" ); + goto cleanup; + } +if (act_time > now) { +error_setg (errp,"The certificate is not yet active" ); + goto cleanup; + } + + ret =0 ; + +cleanup: +gnutls_x509_crt_deinit (crt); +return ret; +} + +int qcrypto_x509_get_pk_algorithm (uint8_t *cert,size_t size,Error **errp) +{ +int rc; +int ret =-1 ; +unsigned int bits; +gnutls_x509_crt_t crt; +gnutls_datum_t datum = {.data = cert, .size = size}; + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_x509_crt_get_pk_algorithm (crt, &bits); +if (rc >=G_N_ELEMENTS (gnutls_to_qcrypto_pk_alg_map) || rc <0 ) { +error_setg (errp,"Unknown public key algorithm %d" , rc); + goto cleanup; + } + + ret = gnutls_to_qcrypto_pk_alg_map[rc]; + +cleanup: +gnutls_x509_crt_deinit (crt); +return ret; +} + +int qcrypto_x509_get_cert_key_id (uint8_t *cert,size_t size, +QCryptoHashAlgo hash_alg, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +int rc; +int ret =-1 ; +gnutls_x509_crt_t crt; +gnutls_datum_t datum = {.data = cert, .size = size}; + +if (hash_alg >=G_N_ELEMENTS (qcrypto_to_gnutls_hash_alg_map)) { +error_setg (errp,"Unknown hash algorithm %d" , hash_alg); +return ret; + } + +if (hash_alg >=G_N_ELEMENTS (qcrypto_to_gnutls_keyid_flags_map) || + qcrypto_to_gnutls_keyid_flags_map[hash_alg] ==-1 ) { +error_setg (errp,"Unsupported key id flag %d" , hash_alg); +return ret; + } + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + *resultlen =gnutls_hash_get_len (qcrypto_to_gnutls_hash_alg_map[hash_alg]); +if (*resultlen ==0 ) { +error_setg (errp,"Failed to get hash algorithn length: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + *result =g_malloc0 (*resultlen); +if (gnutls_x509_crt_get_key_id (crt, + qcrypto_to_gnutls_keyid_flags_map[hash_alg], + *result, resultlen) !=0 ) { +error_setg (errp,"Failed to get key ID from certificate" ); +g_clear_pointer (result, g_free); + goto cleanup; + } + + ret =0 ; + +cleanup: +gnutls_x509_crt_deinit (crt); +return ret; +} + +static int qcrypto_x509_get_ecc_curve (uint8_t *cert,size_t size,Error **errp) +{ +int rc; +int ret =-1 ; +gnutls_x509_crt_t crt; +gnutls_datum_t datum = {.data = cert, .size = size}; +gnutls_ecc_curve_t curve_id; +gnutls_datum_t x = {.data =NULL , .size =0 }; +gnutls_datum_t y = {.data =NULL , .size =0 }; + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_x509_crt_import (crt, &datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_x509_crt_get_pk_ecc_raw (crt, &curve_id, &x, &y); +if (rc !=0 ) { +error_setg (errp,"Failed to get ECC public key curve: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + ret = curve_id; + +cleanup: +gnutls_x509_crt_deinit (crt); +gnutls_free (x.data); +gnutls_free (y.data); +return ret; +} + +int qcrypto_x509_check_ecc_curve_p521 (uint8_t *cert,size_t size,Error **errp) +{ +int curve_id; + + curve_id =qcrypto_x509_get_ecc_curve (cert, size, errp); +if (curve_id ==-1 ) { +return -1 ; + } + +if (curve_id ==GNUTLS_ECC_CURVE_INVALID ) { +error_setg (errp,"Invalid ECC curve" ); +return -1 ; + } + +if (curve_id ==GNUTLS_ECC_CURVE_SECP521R1 ) { +return 1 ; + } + +return 0 ; +} +#else /* ! CONFIG_GNUTLS */ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size,@@ -142,4 +362,32 @@ int qcrypto_x509_convert_cert_der(uint8_t *cert, size_t size,return -1 ; } +int qcrypto_x509_check_cert_times (uint8_t *cert,size_t size,Error **errp) +{ +error_setg (errp,"GNUTLS is required to get certificate times" ); +return -1 ; +} + +int qcrypto_x509_get_pk_algorithm (uint8_t *cert,size_t size,Error **errp) +{ +error_setg (errp,"GNUTLS is required to get public key algorithm" ); +return -1 ; +} + +int qcrypto_x509_get_cert_key_id (uint8_t *cert,size_t size, +QCryptoHashAlgo hash_alg, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +error_setg (errp,"GNUTLS is required to get key ID" ); +return -1 ; +} + +int qcrypto_x509_check_ecc_curve_p521 (uint8_t *cert,size_t size,Error **errp) +{ +error_setg (errp,"GNUTLS is required to determine ecc curve" ); +return -1 ; +} +#endif /* ! CONFIG_GNUTLS */ include/crypto/x509-utils.h +0
-0
include/crypto/x509-utils.h line 16
Quoted 4 lines
include/crypto/x509-utils.h +1
-0@@ -13,6 +13,15 @@#include "crypto/hash.h" +typedef enum {
Please add QCRYPTO_PK_ALGO_UNKNOWN here (or maybe rather QCRYPTO_PK_ALGO_INVALID ?)
include/crypto/x509-utils.h line 23
Quoted 7 lines
include/crypto/x509-utils.h +7
-0+QCRYPTO_PK_ALGO_RSA , +QCRYPTO_PK_ALGO_DSA , +QCRYPTO_PK_ALGO_ECDSA , +QCRYPTO_PK_ALGO_RSA_OAEP , +QCRYPTO_PK_ALGO_ED25519 , +QCRYPTO_PK_ALGO_ED448 , +} QCryptoPkAlgo;
Thanks,
Thomas
Re: [PATCH v7 08/29] crypto/x509-utils: Add helper functions for DIAG 320 subcode 2
Quoted 29 lines · Thomas Huth
Quoted 21 lines · Zhuoying Cai
Introduce new helper functions to extract certificate metadata:
qcrypto_x509_check_cert_times()- validates the certificate's validity period against the current timeqcrypto_x509_get_pk_algorithm()- returns the public key algorithm used in the certificateqcrypto_x509_get_cert_key_id()- extracts the key ID from the certificateqcrypto_x509_is_ecc_curve_p521()- determines the ECC public key algorithm uses P-521 curveThese functions provide support for metadata extraction and validity checking for X.509 certificates.
Signed-off-by Zhuoying Cai <[email protected]>--- crypto/x509-utils.c | 248 ++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 73 +++++++++++ 2 files changed, 321 insertions(+)crypto/x509-utils.c +0
-0crypto/x509-utils.c static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { · line 29
Quoted 3 lines
crypto/x509-utils.c +1
-0@@ -27,6 +27,25 @@ static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { [QCRYPTO_HASH_ALGO_RIPEMD160] = GNUTLS_DIG_RMD160, }; +static const int gnutls_to_qcrypto_pk_alg_map[] = {Could you do me a favour and add a line like this at the beginning of this array:
Quoted 5 lines
[
GNUTLS_PK_UNKNOWN] =QCRYPTO_PK_ALGO_UNKNOWN,and then also set
QCRYPTO_PK_ALGO_UNKNOWN= 0 in the enum in the header? That way we can be sure that zero values are not accidentally mapped to a valid algorithm.
That would be special casing just one enum type in the crypto subsystem to have this special unknown value. We've got 1000's of enums across QEMU and don't generally add such a special zeroth constant, so I don't find this suggested change to be desirable.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v7 08/29] crypto/x509-utils: Add helper functions for DIAG 320 subcode 2
Quoted 32 lines · Daniel P. Berrangé
Quoted 31 lines · Thomas Huth
Quoted 21 lines · Zhuoying Cai
Introduce new helper functions to extract certificate metadata:
qcrypto_x509_check_cert_times()- validates the certificate's validity period against the current timeqcrypto_x509_get_pk_algorithm()- returns the public key algorithm used in the certificateqcrypto_x509_get_cert_key_id()- extracts the key ID from the certificateqcrypto_x509_is_ecc_curve_p521()- determines the ECC public key algorithm uses P-521 curveThese functions provide support for metadata extraction and validity checking for X.509 certificates.
Signed-off-by Zhuoying Cai <[email protected]>--- crypto/x509-utils.c | 248 ++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 73 +++++++++++ 2 files changed, 321 insertions(+)crypto/x509-utils.c +0
-0crypto/x509-utils.c static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { · line 29
Quoted 3 lines
crypto/x509-utils.c +1
-0@@ -27,6 +27,25 @@ static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { [QCRYPTO_HASH_ALGO_RIPEMD160] = GNUTLS_DIG_RMD160, }; +static const int gnutls_to_qcrypto_pk_alg_map[] = {Could you do me a favour and add a line like this at the beginning of this array:
[
GNUTLS_PK_UNKNOWN] =QCRYPTO_PK_ALGO_UNKNOWN,
Quoted 8 lines
Quoted 3 lines
and then also set
QCRYPTO_PK_ALGO_UNKNOWN= 0 in the enum in the header? That way we can be sure that zero values are not accidentally mapped to a valid algorithm.That would be special casing just one enum type in the crypto subsystem to have this special unknown value. We've got 1000's of enums across QEMU and don't generally add such a special zeroth constant, so I don't find this suggested change to be desirable.
I came up with this idea when reviewing the qcrypto_x509_get_pk_algorithm() function in this patch which is basically doing:
ret = gnutls_to_qcrypto_pk_alg_map[rc];
My concern is that if someone ever extends the QCryptoPkAlgo in the header, but forgets to fill in as much entries to gnutls_to_qcrypto_pk_alg_map as there are entries in the enum, we're in trouble, since the wholes in the array will be padded with zeros that then get mapped to QCRYPTO_PK_ALGO_RSA. Having a clearly invalid meaning of 0 would help in such cases. But if you consider this as too unlikely to happen, then never mind my suggestion.
Thomas
Re: [PATCH v7 08/29] crypto/x509-utils: Add helper functions for DIAG 320 subcode 2
Quoted 45 lines · Thomas Huth
Quoted 32 lines · Daniel P. Berrangé
Quoted 31 lines · Thomas Huth
Quoted 21 lines · Zhuoying Cai
Introduce new helper functions to extract certificate metadata:
qcrypto_x509_check_cert_times()- validates the certificate's validity period against the current timeqcrypto_x509_get_pk_algorithm()- returns the public key algorithm used in the certificateqcrypto_x509_get_cert_key_id()- extracts the key ID from the certificateqcrypto_x509_is_ecc_curve_p521()- determines the ECC public key algorithm uses P-521 curveThese functions provide support for metadata extraction and validity checking for X.509 certificates.
Signed-off-by Zhuoying Cai <[email protected]>--- crypto/x509-utils.c | 248 ++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 73 +++++++++++ 2 files changed, 321 insertions(+)crypto/x509-utils.c +0
-0crypto/x509-utils.c static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { · line 29
Quoted 3 lines
crypto/x509-utils.c +1
-0@@ -27,6 +27,25 @@ static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { [QCRYPTO_HASH_ALGO_RIPEMD160] = GNUTLS_DIG_RMD160, }; +static const int gnutls_to_qcrypto_pk_alg_map[] = {Could you do me a favour and add a line like this at the beginning of this array:
[
GNUTLS_PK_UNKNOWN] =QCRYPTO_PK_ALGO_UNKNOWN,Quoted 8 lines
Quoted 3 lines
and then also set
QCRYPTO_PK_ALGO_UNKNOWN= 0 in the enum in the header? That way we can be sure that zero values are not accidentally mapped to a valid algorithm.That would be special casing just one enum type in the crypto subsystem to have this special unknown value. We've got 1000's of enums across QEMU and don't generally add such a special zeroth constant, so I don't find this suggested change to be desirable.
I came up with this idea when reviewing the
qcrypto_x509_get_pk_algorithm()function in this patch which is basically doing:
Quoted 7 lines
ret = gnutls_to_qcrypto_pk_alg_map[rc];My concern is that if someone ever extends the QCryptoPkAlgo in the header, but forgets to fill in as much entries to
gnutls_to_qcrypto_pk_alg_mapas there are entries in the enum, we're in trouble, since the wholes in the array will be padded with zeros that then get mapped toQCRYPTO_PK_ALGO_RSA. Having a clearly invalid meaning of 0 would help in such cases.
Hmm, looking at the usage of qcrypto_x509_get_pk_algorithm(), I think the method and the QCryptoPkAlgo enums should be deleted entirely.
In the next patch, the caller does
/* public key algorithm */
algo = qcrypto_x509_get_pk_algorithm(cert.raw, cert.size, &err);
if (algo < 0) {
error_report_err(err);
return -1;
}
if (algo == QCRYPTO_PK_ALGO_ECDSA) {
rc = qcrypto_x509_check_ecc_curve_p521(cert.raw, cert.size, &err);
if (rc == -1) {
error_report_err(err);
return -1;
}
return (rc == 1) ? DIAG_320_VCE_KEYTYPE_ECDSA_P521 :
DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING;
}
return DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING;so the caller doesn't actually care about anything other than PK_ALGO_ECDSA, so IMHO going to the trouble of defining an enum and mapping gnutls enums is pointless.
This caller should unconditionally call
qcrypto_x509_check_ecc_curve_p521()
and then then qcrypto_x509_check_ecc_curve_p521() method should check whether the pk algorithm is GNUTLS_PK_ECDSA internally and just return 0 if not.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v7 08/29] crypto/x509-utils: Add helper functions for DIAG 320 subcode 2
Quoted 89 lines · Daniel P. Berrangé
Quoted 45 lines · Thomas Huth
Quoted 32 lines · Daniel P. Berrangé
Quoted 31 lines · Thomas Huth
Quoted 21 lines · Zhuoying Cai
Introduce new helper functions to extract certificate metadata:
qcrypto_x509_check_cert_times()- validates the certificate's validity period against the current timeqcrypto_x509_get_pk_algorithm()- returns the public key algorithm used in the certificateqcrypto_x509_get_cert_key_id()- extracts the key ID from the certificateqcrypto_x509_is_ecc_curve_p521()- determines the ECC public key algorithm uses P-521 curveThese functions provide support for metadata extraction and validity checking for X.509 certificates.
Signed-off-by Zhuoying Cai <[email protected]>--- crypto/x509-utils.c | 248 ++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 73 +++++++++++ 2 files changed, 321 insertions(+)crypto/x509-utils.c +0
-0crypto/x509-utils.c static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { · line 29
Quoted 3 lines
crypto/x509-utils.c +1
-0@@ -27,6 +27,25 @@ static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = { [QCRYPTO_HASH_ALGO_RIPEMD160] = GNUTLS_DIG_RMD160, }; +static const int gnutls_to_qcrypto_pk_alg_map[] = {Could you do me a favour and add a line like this at the beginning of this array:
[
GNUTLS_PK_UNKNOWN] =QCRYPTO_PK_ALGO_UNKNOWN,Quoted 8 lines
Quoted 3 lines
and then also set
QCRYPTO_PK_ALGO_UNKNOWN= 0 in the enum in the header? That way we can be sure that zero values are not accidentally mapped to a valid algorithm.That would be special casing just one enum type in the crypto subsystem to have this special unknown value. We've got 1000's of enums across QEMU and don't generally add such a special zeroth constant, so I don't find this suggested change to be desirable.
I came up with this idea when reviewing the
qcrypto_x509_get_pk_algorithm()function in this patch which is basically doing:Quoted 7 lines
ret = gnutls_to_qcrypto_pk_alg_map[rc];My concern is that if someone ever extends the QCryptoPkAlgo in the header, but forgets to fill in as much entries to
gnutls_to_qcrypto_pk_alg_mapas there are entries in the enum, we're in trouble, since the wholes in the array will be padded with zeros that then get mapped toQCRYPTO_PK_ALGO_RSA. Having a clearly invalid meaning of 0 would help in such cases.Hmm, looking at the usage of
qcrypto_x509_get_pk_algorithm(), I think the method and the QCryptoPkAlgo enums should be deleted entirely.In the next patch, the caller does
/* public key algorithm */ algo = qcrypto_x509_get_pk_algorithm(cert.raw, cert.size, &err); if (algo < 0) { error_report_err(err); return -1; } if (algo == QCRYPTO_PK_ALGO_ECDSA) { rc = qcrypto_x509_check_ecc_curve_p521(cert.raw, cert.size, &err); if (rc == -1) { error_report_err(err); return -1; } return (rc == 1) ? DIAG_320_VCE_KEYTYPE_ECDSA_P521 : DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING; } return DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING;so the caller doesn't actually care about anything other than
PK_ALGO_ECDSA, so IMHO going to the trouble of defining an enum and mapping gnutls enums is pointless.This caller should unconditionally call
qcrypto_x509_check_ecc_curve_p521()
Quoted 7 lines
and then then
qcrypto_x509_check_ecc_curve_p521()method should check whether the pk algorithm isGNUTLS_PK_ECDSAinternally and just return 0 if not.With regards,
Daniel
Thanks for the comments. I agree with your suggestion. Given the current usage, the caller only needs to know whether the key is ECDSA P-521 or not.
I had a similar concern to Thomas about the QCryptoPkAlgo enum and its mapping, since the gnutls_pk_algorithm_t enum includes many values. Moving the algorithm check into qcrypto_x509_check_ecc_curve_p521() addresses this. I'll rework the patch accordingly.
Thanks again for all the helpful feedback!
PATCH v7 9/29 s390x/diag: Implement DIAG 320 subcode 2
In reply to Zhuoying CaiDIAG 320 subcode 2 provides verification-certificates (VCs) that are in the certificate store. Only X509 certificates in DER format and SHA-256 hash type are recognized.
The subcode value is denoted by setting the second-left-most bit of an 8-byte field.
The Verification Certificate Block (VCB) contains the output data when the operation completes successfully. It includes a common header followed by zero or more Verification Certificate Entries (VCEs), depending on the VCB input length and the VC range (from the first VC index to the last VC index) in the certificate store.
Each VCE contains information about a certificate retrieved from the S390IPLCertificateStore, such as the certificate name, key type, key ID length, hash length, and the raw certificate data.
The key ID and hash are extracted from the raw certificate by the crypto API.
Note: SHA2-256 VC hash type is required for retrieving the hash (fingerprint) of the certificate.
--- docs/specs/s390x-secure-ipl.rst | 13 ++ include/hw/s390x/ipl/diag320.h | 49 +++++ target/s390x/diag.c | 334 +++++++++++++++++++++++++++++++- 3 files changed, 395 insertions(+), 1 deletion(-)
docs/specs/s390x-secure-ipl.rst +13 -0
@@ -38,3 +38,16 @@ Subcode 1 - query verification certificate storage information The output is returned in the verification-certificate-storage-size block (VCSSB). A VCSSB length of 4 indicates that no certificates are available in the CS. + +Subcode 2 - store verification certificates + Provides VCs that are in the certificate store. + + The output is provided in a VCB, which includes a common header followed by + zero or more verification-certificate entries (VCEs). + + The first-VC index and last-VC index fields of VCB specify the range of VCs + to be stored by subcode 2. Stored count and remained count fields specify + the number of VCs stored and could not be stored in the VCB due to + insufficient storage specified in the VCB input length field. + + VCE contains various information of a VC from the CS.
include/hw/s390x/ipl/diag320.h +49 -0
72 lines not on this page · raw message
target/s390x/diag.c +333 -1
366 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v7 09/29] s390x/diag: Implement DIAG 320 subcode 2
Quoted 38 lines · Zhuoying Cai
DIAG 320 subcode 2 provides verification-certificates (VCs) that are in the certificate store. Only X509 certificates in DER format and SHA-256 hash type are recognized.
The subcode value is denoted by setting the second-left-most bit of an 8-byte field.
The Verification Certificate Block (VCB) contains the output data when the operation completes successfully. It includes a common header followed by zero or more Verification Certificate Entries (VCEs), depending on the VCB input length and the VC range (from the first VC index to the last VC index) in the certificate store.
Each VCE contains information about a certificate retrieved from the S390IPLCertificateStore, such as the certificate name, key type, key ID length, hash length, and the raw certificate data.
The key ID and hash are extracted from the raw certificate by the crypto API.Note: SHA2-256 VC hash type is required for retrieving the hash (fingerprint) of the certificate.
Signed-off-by Zhuoying Cai <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 13 ++ include/hw/s390x/ipl/diag320.h | 49 +++++ target/s390x/diag.c | 334 +++++++++++++++++++++++++++++++- 3 files changed, 395 insertions(+), 1 deletion(-)docs/specs/s390x-secure-ipl.rst +2
-0@@ -38,3 +38,16 @@ Subcode 1 - query verification certificate storage information The output is returned in the verification-certificate-storage-size block (VCSSB). A VCSSB length of 4 indicates that no certificates are available in the CS. + +Subcode 2 - store verification certificates
docs/specs/s390x-secure-ipl.rst Subcode 1 - query verification certificate storage information · line 52
Quoted 10 lines
docs/specs/s390x-secure-ipl.rst +10
-0+ Provides VCs that are in the certificate store. + + The output is provided in a VCB, which includes a common header followed by + zero or more verification-certificate entries (VCEs). + + The first-VC index and last-VC index fields of VCB specify the range of VCs + to be stored by subcode 2. Stored count and remained count fields specify + the number of VCs stored and could not be stored in the VCB due to + insufficient storage specified in the VCB input length field. +
Add to the paragraph above: "The instruction expects the cert store to maintain an origin of 1 for the index (i.e. a retrieval of the first certificate in the store should be denoted by setting first-VC to 1)."
docs/specs/s390x-secure-ipl.rst Subcode 1 - query verification certificate storage information · line 53
Quoted 1 line
docs/specs/s390x-secure-ipl.rst +1
-0+ VCE contains various information of a VC from the CS.
Change the line above to:
"Each VCE contains a header followed by information extracted from a certificate within the certificate store. The information includes:
key-id, hash, and certificate data. This information is stored contiguously in a VCE (with zero-padding). Following the header, the key-id is immediately stored. The hash and certificate data follow and may be accessed via the respective offset fields stored in the VCE."
Quoted 38 lines
include/hw/s390x/ipl/diag320.h +11
-0@@ -12,19 +12,30 @@#define DIAG_320_SUBC_QUERY_ISM 0#define DIAG_320_SUBC_QUERY_VCSI 1 +#define DIAG_320_SUBC_STORE_VC 2#define DIAG_320_RC_OK 0x0001#define DIAG_320_RC_NOT_SUPPORTED 0x0102#define DIAG_320_RC_INVAL_VCSSB_LEN 0x0202 +#define DIAG_320_RC_INVAL_VCB_LEN 0x0204 +#define DIAG_320_RC_BAD_RANGE 0x0302#define DIAG_320_ISM_QUERY_SUBCODES 0x80000000#define DIAG_320_ISM_QUERY_VCSI 0x40000000 +#define DIAG_320_ISM_STORE_VC 0x20000000#define VCSSB_NO_VC 4#define VCSSB_MIN_LEN 128#define VCE_HEADER_LEN 128 +#define VCE_INVALID_LEN 72#define VCB_HEADER_LEN 64 +#define DIAG_320_VCE_FLAGS_VALID 0x80 +#define DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING 0 +#define DIAG_320_VCE_KEYTYPE_ECDSA_P521 1 +#define DIAG_320_VCE_FORMAT_X509_DER 1 +#define DIAG_320_VCE_HASHTYPE_SHA2_256 1 +struct VCStorageSizeBlock {uint32_t length;uint8_t reserved0[3 ];@@ -39,4 +50,42 @@ struct VCStorageSizeBlock { };typedef struct VCStorageSizeBlock VCStorageSizeBlock ;
include/hw/s390x/ipl/diag320.h struct VCStorageSizeBlock { · line 61
Quoted 10 lines
include/hw/s390x/ipl/diag320.h +9
-0+struct VCBlock { +uint32_t in_len; +uint32_t reserved0; +uint16_t first_vc_index; +uint16_t last_vc_index; +uint32_t reserved1[5 ]; +uint32_t out_len; +uint8_t reserved2[3 ]; +uint8_t version;
Sorry, it's been some time since I've looked at this code and I do not remember if this discussion was already had: do we need the version field at all right now? I looked around the future patches and don't see it set or checked anywhere. Though harmless, it may make sense to consume this field by reserved2.
include/hw/s390x/ipl/diag320.h struct VCStorageSizeBlock { · line 67
Quoted 6 lines
include/hw/s390x/ipl/diag320.h +6
-0+uint16_t stored_ct; +uint16_t remain_ct; +uint32_t reserved3[5 ]; +uint8_t vce_buf[]; +}; +typedef struct VCBlock VCBlock ;
Other than the nit above, the struct layout is correct.
Quoted 12 lines
include/hw/s390x/ipl/diag320.h +12
-0+ +struct VCEntry { +uint32_t len; +uint8_t flags; +uint8_t key_type; +uint16_t cert_idx; +uint32_t name[16 ]; +uint8_t format; +uint8_t reserved0; +uint16_t keyid_len; +uint8_t reserved1; +uint8_t hash_type;
include/hw/s390x/ipl/diag320.h struct VCStorageSizeBlock { · line 89
Quoted 10 lines
include/hw/s390x/ipl/diag320.h +10
-0+uint16_t hash_len; +uint32_t reserved2; +uint32_t cert_len; +uint32_t reserved3[2 ]; +uint16_t hash_offset; +uint16_t cert_offset; +uint32_t reserved4[7 ]; +uint8_t cert_buf[]; +}; +typedef struct VCEntry VCEntry ;
Looks good.
From here on, I'll assume the data structures haven't changed between iterations unless explicitly stated in the cover letter change log :)
Quoted 38 lines
include/hw/s390x/ipl/diag320.h +1
-0+#endif target/s390x/diag.c +14
-0@@ -17,6 +17,7 @@#include "s390x-internal.h" #include "hw/watchdog/wdt_diag288.h" #include "system/cpus.h" +#include "hw/s390x/cert-store.h" #include "hw/s390x/ipl.h" #include "hw/s390x/ipl/diag320.h" #include "hw/s390x/s390-virtio-ccw.h" @@ -24,6 +25,7 @@#include "kvm/kvm_s390x.h" #include "target/s390x/kvm/pv.h" #include "qemu/error-report.h" +#include "crypto/x509-utils.h" static inline bool diag_parm_addr_valid (uint64_t addr,size_t size,bool write)@@ -231,8 +233,330 @@ static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1,return DIAG_320_RC_OK ; } +static bool is_cert_valid (S390IPLCertificate cert) +{ +int rc; +Error *err =NULL ; + + rc =qcrypto_x509_check_cert_times (cert.raw, cert.size, &err); +if (rc !=0 ) { +error_report_err (err); +return false; + } + +return true;
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 257
Quoted 10 lines
target/s390x/diag.c +10
-0+} + +static void handle_key_id (VCEntry *vce,S390IPLCertificate cert) +{ +int rc; + g_autofree unsigned char *key_id_data = NULL; +size_t key_id_len; +Error *err =NULL ; + + key_id_len =CERT_KEY_ID_LEN ;
Looking at qcrypto_x509_get_cert_key_id(), it doesn't look like key_id_len gets used before it's overwritten by gnutls_hash_get_len(). Is the line above still needed?
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 258
Quoted 1 line
target/s390x/diag.c +1
-0+/* key id and key id len */
Remove comment.
Quoted 18 lines
target/s390x/diag.c +18
-0+ rc =qcrypto_x509_get_cert_key_id (cert.raw, cert.size, +QCRYPTO_HASH_ALGO_SHA256 , + &key_id_data, &key_id_len, &err); +if (rc <0 ) { +error_report_err (err); +return ; + } + +if (VCE_HEADER_LEN + key_id_len >be32_to_cpu (vce->len)) { +error_report ("Unable to write key ID: exceeds buffer bounds" ); +return ; + } + + vce->keyid_len =cpu_to_be16 (key_id_len); + +memcpy (vce->cert_buf, key_id_data, key_id_len); +} +
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 286
Quoted 10 lines
target/s390x/diag.c +10
-0+static int handle_hash (VCEntry *vce,S390IPLCertificate cert,uint16_t keyid_field_len) +{ +int rc; +uint16_t hash_offset; + g_autofree void *hash_data = NULL; +size_t hash_len; +Error *err =NULL ; + + hash_len =CERT_HASH_LEN ; +/* hash and hash len */
Remove comment.
Quoted 22 lines
target/s390x/diag.c +22
-0+ hash_data =g_malloc0 (hash_len); + rc =qcrypto_get_x509_cert_fingerprint (cert.raw, cert.size, +QCRYPTO_HASH_ALGO_SHA256 , + hash_data, &hash_len, &err); +if (rc <0 ) { +error_report_err (err); +return -1 ; + } + + hash_offset =VCE_HEADER_LEN + keyid_field_len; +if (hash_offset + hash_len >be32_to_cpu (vce->len)) { +error_report ("Unable to write hash: exceeds buffer bounds" ); +return -1 ; + } + + vce->hash_len =cpu_to_be16 (hash_len); + vce->hash_type =DIAG_320_VCE_HASHTYPE_SHA2_256 ; + vce->hash_offset =cpu_to_be16 (hash_offset); + +memcpy ((uint8_t *)vce + hash_offset, hash_data, hash_len); + +return 0 ;
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 318
Quoted 10 lines
target/s390x/diag.c +10
-0+} + +static int handle_cert (VCEntry *vce,S390IPLCertificate cert,uint16_t hash_field_len) +{ +int rc; +uint16_t cert_offset; + g_autofree uint8_t *cert_der = NULL; +Error *err =NULL ; + +/* certificate in DER format */
Remove comment.
Quoted 19 lines
target/s390x/diag.c +19
-0+ rc =qcrypto_x509_convert_cert_der (cert.raw, cert.size, + &cert_der, &cert.der_size, &err); +if (rc <0 ) { +error_report_err (err); +return -1 ; + } + + cert_offset =be16_to_cpu (vce->hash_offset) + hash_field_len; +if (cert_offset + cert.der_size >be32_to_cpu (vce->len)) { +error_report ("Unable to write certificate: exceeds buffer bounds" ); +return -1 ; + } + + vce->format =DIAG_320_VCE_FORMAT_X509_DER ; + vce->cert_len =cpu_to_be32 (cert.der_size); + vce->cert_offset =cpu_to_be16 (cert_offset); + +memcpy ((uint8_t *)vce + cert_offset, cert_der, cert.der_size); +
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 347
Quoted 10 lines
target/s390x/diag.c +10
-0+return 0 ; +} + +static int get_key_type (S390IPLCertificate cert) +{ +int algo; +int rc; +Error *err =NULL ; + +/* public key algorithm */
Remove comment.
Quoted 37 lines
target/s390x/diag.c +37
-0+ algo =qcrypto_x509_get_pk_algorithm (cert.raw, cert.size, &err); +if (algo <0 ) { +error_report_err (err); +return -1 ; + } + +if (algo ==QCRYPTO_PK_ALGO_ECDSA ) { + rc =qcrypto_x509_check_ecc_curve_p521 (cert.raw, cert.size, &err); +if (rc ==-1 ) { +error_report_err (err); +return -1 ; + } + +return (rc ==1 ) ?DIAG_320_VCE_KEYTYPE_ECDSA_P521 : +DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING ; + } + +return DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING ; +} + +static int build_vce_header (VCEntry *vce,S390IPLCertificate cert,int idx) +{ +int key_type; + + vce->len =cpu_to_be32 (VCE_HEADER_LEN ); + vce->cert_idx =cpu_to_be16 (idx +1 ); +strncpy ((char *)vce->name, (char *)cert.vc_name,VC_NAME_LEN_BYTES ); + + key_type =get_key_type (cert); +if (key_type ==-1 ) { +return -1 ; + } + vce->key_type = key_type; + +return 0 ; +} +
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 394
Quoted 10 lines
target/s390x/diag.c +10
-0+static int build_vce_data (VCEntry *vce,S390IPLCertificate cert) +{ +uint16_t keyid_field_len; +uint16_t hash_field_len; +uint32_t cert_field_len; +uint32_t vce_len; +int rc; + +handle_key_id (vce, cert); +/* vce key id field length - can be 0 if failed to retrieve */
What is the reason that the key-id may fail to be retrieved and the program is allowed to continue? Is this data insignificant?
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 407
Quoted 13 lines
target/s390x/diag.c +13
-0+ keyid_field_len =ROUND_UP (be16_to_cpu (vce->keyid_len),4 ); + + rc =handle_hash (vce, cert, keyid_field_len); +if (rc) { +return -1 ; + } + hash_field_len =ROUND_UP (be16_to_cpu (vce->hash_len),4 ); + + rc =handle_cert (vce, cert, hash_field_len); +if (rc || !is_cert_valid (cert)) { +return -1 ; + } +/* vce certificate field length */
Remove comment.
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 415
Quoted 8 lines
target/s390x/diag.c +8
-0+ cert_field_len =ROUND_UP (be32_to_cpu (vce->cert_len),4 ); + + vce_len =VCE_HEADER_LEN + keyid_field_len + hash_field_len + cert_field_len; +if (vce_len >be32_to_cpu (vce->len)) { +return -1 ; + } + +/* The certificate is valid and VCE contains the certificate */
Remove comment.
Quoted 9 lines
target/s390x/diag.c +9
-0+ vce->flags |=DIAG_320_VCE_FLAGS_VALID ; + +/* Update vce length to reflect the actual size used by vce */ + vce->len =cpu_to_be32 (vce_len); + +return 0 ; +} + +static VCEntry *diag_320_build_vce (S390IPLCertificate cert,uint32_t vce_len,int idx)
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 434
Quoted 10 lines
target/s390x/diag.c +10
-0+{ + g_autofree VCEntry *vce = NULL; +int rc; + +/* +* Construct VCE +* Allocate enough memory for all certificate data +* (key id, hash and certificate). +* Unused area following the VCE field contains zeros. +*/
Reword to:
``` /* * Allocate enough space for the largest possible size for this VCE. * As the certificate fields (key-id, hash, data) are parsed, the * VCE's length field will be updated accordingly. */ ```
Quoted 9 lines
target/s390x/diag.c +9
-0+ vce =g_malloc0 (vce_len); + rc =build_vce_header (vce, cert, idx); +if (rc) { + vce->len =cpu_to_be32 (VCE_INVALID_LEN ); + goto out; + } + + vce->len =cpu_to_be32 (vce_len); + rc =build_vce_data (vce, cert);
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 453
Quoted 10 lines
target/s390x/diag.c +10
-0+if (rc) { + vce->len =cpu_to_be32 (VCE_INVALID_LEN ); + } + +out: +return g_steal_pointer (&vce); +} + +static int handle_diag320_store_vc (S390CPU *cpu,uint64_t addr,uint64_t r1,uintptr_t ra, +S390IPLCertificateStore *qcs)
Thought: perhaps the variable should be something other than "qcs"? The "q" doesn't have meaning anymore.
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 456
Quoted 3 lines
target/s390x/diag.c +3
-0+{ + g_autofree VCBlock *vcb = NULL; +size_t vce_offset;
Would read better as either "entry_offset" or just "offset".
Quoted 27 lines
target/s390x/diag.c +27
-0+size_t remaining_space; +uint32_t vce_len; +uint16_t first_vc_index; +uint16_t last_vc_index; +uint32_t in_len; + + vcb =g_new0 (VCBlock,1 ); +if (s390_cpu_virt_mem_read (cpu, addr, r1, vcb,sizeof (*vcb))) { +s390_cpu_virt_mem_handle_exc (cpu, ra); +return -1 ; + } + + in_len =be32_to_cpu (vcb->in_len); + first_vc_index =be16_to_cpu (vcb->first_vc_index); + last_vc_index =be16_to_cpu (vcb->last_vc_index); + +if (in_len %TARGET_PAGE_SIZE !=0 ) { +return DIAG_320_RC_INVAL_VCB_LEN ; + } + +if (first_vc_index > last_vc_index) { +return DIAG_320_RC_BAD_RANGE ; + } + + vcb->out_len =VCB_HEADER_LEN ; + +if (first_vc_index ==0 ) {
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 493
Quoted 10 lines
target/s390x/diag.c +10
-0+/* +* Zero is a valid index for the first and last VC index. +* Zero index results in the VCB header and zero certificates returned. +*/ +if (last_vc_index ==0 ) { + goto out; + } + +/* DIAG320 certificate store remains a one origin for cert entries */ + vcb->first_vc_index =1 ;
The vcb's first index field should not be modified.
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 495
Quoted 2 lines
target/s390x/diag.c +2
-0+ first_vc_index =1 ; + }
The adjustments above are unfortunately confusing to those without the context. The main discrepancy between how DIAG 320 subcode 2's data structures are implemented and how the S390CerticiateStore is implemented is the index origin (former uses 1, latter uses 0). This should be described in detail to clarify why the code above is needed:
```
/*
* DIAG 320 subcode 2 expects to query a certificate store that
* maintains an index origin of 1. However, the S390IPLCertificateStore
* maintains an index origin of 0. Thus, the indices must be adjusted
* for correct access into the cert store. A couple of special cases
* must also be accounted for.
*/
/* Special case: both indices are 0; return header with no certs */
if (first_vc_index == 0 && last_vc_index == 0) {
goto out;
}
/* Special case: first > 0; adjust for index origin */
if (first_vc_index > 0) {
first_vc_index--;
}
last_vc_index--;
for (i = first_vc_index; i <= last_vc_index; i++) {
...
}
```It might make sense to rename the *_vc_index variables -- either something generic, or something specifically denoting they're indexing the S390IPLCertificateStore.
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 500
Quoted 5 lines
target/s390x/diag.c +5
-0+ + vce_offset =VCB_HEADER_LEN ; + remaining_space = in_len -VCB_HEADER_LEN ; + +for (int i = first_vc_index -1 ; i < last_vc_index && i < qcs->count; i++) {
There should be special handling in the case where certs are requested outside of the cert store's range. It's not explicitly documented to be handled in this way, but returning DIAG_320_RC_BAD_RANGE may be valid (maybe this check should get handled some point before the loop?).
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 506
Quoted 6 lines
target/s390x/diag.c +6
-0+VCEntry *vce; +S390IPLCertificate cert = qcs->certs[i]; +/* +* Each VCE is word aligned. +* Each variable length field within the VCE is also word aligned. +*/
Change this comment to:
``` /* * Each field of the VCE is word-aligned. Allocate enough space to * contain the largest possible size of this entry. The actual size is * calculated later. */ ```
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 510
Quoted 4 lines
target/s390x/diag.c +4
-0+ vce_len =VCE_HEADER_LEN + +ROUND_UP (CERT_KEY_ID_LEN ,4 ) + +ROUND_UP (CERT_HASH_LEN ,4 ) + +ROUND_UP (cert.der_size,4 );
The name "vce_len" is a bit misleading, as this isn't the actual length (which is calculated within build_vce_data()). Rather, this is a calculation that represents the maximum space potentially required to store an entry. Later, this value gets discarded once the actual size of the VCE is calculated.
Rename this variable to something more reflective of what this value represents, something like "vce_max_size".
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 520
Quoted 10 lines
target/s390x/diag.c +10
-0+ +/* +* If there is no more space to store the cert, +* set the remaining verification cert count and +* break early. +*/ +if (remaining_space < vce_len) { + vcb->remain_ct =cpu_to_be16 (last_vc_index - i); +break ; + }
Shouldn't the check for remaining space occur after the actual length of the VCE has been calculated?
If this holds true, then the current "vce_len" calculation above can be moved to diag_320_build_vce(), which would significantly improve readability.
Quoted 17 lines
target/s390x/diag.c +17
-0+ + vce =diag_320_build_vce (cert, vce_len, i); + +/* Write VCE */ +if (s390_cpu_virt_mem_write (cpu, addr + vce_offset, r1, + vce,be32_to_cpu (vce->len))) { +s390_cpu_virt_mem_handle_exc (cpu, ra); +g_free (vce); +return -1 ; + } + + vce_offset +=be32_to_cpu (vce->len); + vcb->out_len +=be32_to_cpu (vce->len); + remaining_space -=be32_to_cpu (vce->len); + vcb->stored_ct++; + +g_free (vce);
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 547
Quoted 10 lines
target/s390x/diag.c +10
-0+ } + vcb->stored_ct =cpu_to_be16 (vcb->stored_ct); + +out: + vcb->out_len =cpu_to_be32 (vcb->out_len); +/* +* Write VCB header +* All VCEs have been populated with the latest information +* and write VCB header last. +*/
This comment is confusing because there is a case where no certs may be populated (first and last indices are both 0). Either a one-liner like:
/* Finally, write the VCB header */
or it can be removed.
Quoted 29 lines
target/s390x/diag.c +12
-1+if (s390_cpu_virt_mem_write (cpu, addr, r1, vcb,VCB_HEADER_LEN )) { +s390_cpu_virt_mem_handle_exc (cpu, ra); +return -1 ; + } + +return DIAG_320_RC_OK ; +} +QEMU_BUILD_BUG_MSG (sizeof (VCStorageSizeBlock) !=VCSSB_MIN_LEN ,"size of VCStorageSizeBlock is wrong" ); +QEMU_BUILD_BUG_MSG (sizeof (VCBlock) !=VCB_HEADER_LEN ,"size of VCBlock is wrong" ); +QEMU_BUILD_BUG_MSG (sizeof (VCEntry) !=VCE_HEADER_LEN ,"size of VCEntry is wrong" );void handle_diag_320 (CPUS390XState *env,uint64_t r1,uint64_t r3,uintptr_t ra) {@@ -265,7 +589,8 @@ void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)* for now. */ uint32_t ism_word0 =cpu_to_be32 (DIAG_320_ISM_QUERY_SUBCODES | -DIAG_320_ISM_QUERY_VCSI ); +DIAG_320_ISM_QUERY_VCSI | +DIAG_320_ISM_STORE_VC );if (s390_cpu_virt_mem_write (cpu, addr, r1, &ism_word0,sizeof (ism_word0))) {s390_cpu_virt_mem_handle_exc (cpu, ra);@@ -291,6 +616,13 @@ void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra) } env->regs[r1 +1 ] = rc;break ;
target/s390x/diag.c void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra) · line 628
Quoted 10 lines
target/s390x/diag.c +7
-0+case DIAG_320_SUBC_STORE_VC : + rc =handle_diag320_store_vc (cpu, addr, r1, ra, qcs); +if (rc ==-1 ) { +return ; + } + env->regs[r1 +1 ] = rc; +break ;default : env->regs[r1 +1 ] =DIAG_320_RC_NOT_SUPPORTED ;break ;
The handler code here looks fine.
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 09/29] s390x/diag: Implement DIAG 320 subcode 2
Thank you for all the feedback! I'll fix them in the next version.
Quoted 201 lines · Collin Walling
Quoted 38 lines · Zhuoying Cai
DIAG 320 subcode 2 provides verification-certificates (VCs) that are in the certificate store. Only X509 certificates in DER format and SHA-256 hash type are recognized.
The subcode value is denoted by setting the second-left-most bit of an 8-byte field.
The Verification Certificate Block (VCB) contains the output data when the operation completes successfully. It includes a common header followed by zero or more Verification Certificate Entries (VCEs), depending on the VCB input length and the VC range (from the first VC index to the last VC index) in the certificate store.
Each VCE contains information about a certificate retrieved from the S390IPLCertificateStore, such as the certificate name, key type, key ID length, hash length, and the raw certificate data.
The key ID and hash are extracted from the raw certificate by the crypto API.Note: SHA2-256 VC hash type is required for retrieving the hash (fingerprint) of the certificate.
Signed-off-by Zhuoying Cai <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 13 ++ include/hw/s390x/ipl/diag320.h | 49 +++++ target/s390x/diag.c | 334 +++++++++++++++++++++++++++++++- 3 files changed, 395 insertions(+), 1 deletion(-)docs/specs/s390x-secure-ipl.rst +2
-0@@ -38,3 +38,16 @@ Subcode 1 - query verification certificate storage information The output is returned in the verification-certificate-storage-size block (VCSSB). A VCSSB length of 4 indicates that no certificates are available in the CS. + +Subcode 2 - store verification certificatesdocs/specs/s390x-secure-ipl.rst Subcode 1 - query verification certificate storage information · line 52
Quoted 10 lines
docs/specs/s390x-secure-ipl.rst +10
-0+ Provides VCs that are in the certificate store. + + The output is provided in a VCB, which includes a common header followed by + zero or more verification-certificate entries (VCEs). + + The first-VC index and last-VC index fields of VCB specify the range of VCs + to be stored by subcode 2. Stored count and remained count fields specify + the number of VCs stored and could not be stored in the VCB due to + insufficient storage specified in the VCB input length field. +Add to the paragraph above: "The instruction expects the cert store to maintain an origin of 1 for the index (i.e. a retrieval of the first certificate in the store should be denoted by setting first-VC to 1)."
docs/specs/s390x-secure-ipl.rst Subcode 1 - query verification certificate storage information · line 53
Quoted 1 line
docs/specs/s390x-secure-ipl.rst +1
-0+ VCE contains various information of a VC from the CS.Change the line above to:
"Each VCE contains a header followed by information extracted from a certificate within the certificate store. The information includes:
key-id, hash, and certificate data. This information is stored contiguously in a VCE (with zero-padding). Following the header, the key-id is immediately stored. The hash and certificate data follow and may be accessed via the respective offset fields stored in the VCE."Quoted 38 lines
include/hw/s390x/ipl/diag320.h +11
-0@@ -12,19 +12,30 @@#define DIAG_320_SUBC_QUERY_ISM 0#define DIAG_320_SUBC_QUERY_VCSI 1 +#define DIAG_320_SUBC_STORE_VC 2#define DIAG_320_RC_OK 0x0001#define DIAG_320_RC_NOT_SUPPORTED 0x0102#define DIAG_320_RC_INVAL_VCSSB_LEN 0x0202 +#define DIAG_320_RC_INVAL_VCB_LEN 0x0204 +#define DIAG_320_RC_BAD_RANGE 0x0302#define DIAG_320_ISM_QUERY_SUBCODES 0x80000000#define DIAG_320_ISM_QUERY_VCSI 0x40000000 +#define DIAG_320_ISM_STORE_VC 0x20000000#define VCSSB_NO_VC 4#define VCSSB_MIN_LEN 128#define VCE_HEADER_LEN 128 +#define VCE_INVALID_LEN 72#define VCB_HEADER_LEN 64 +#define DIAG_320_VCE_FLAGS_VALID 0x80 +#define DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING 0 +#define DIAG_320_VCE_KEYTYPE_ECDSA_P521 1 +#define DIAG_320_VCE_FORMAT_X509_DER 1 +#define DIAG_320_VCE_HASHTYPE_SHA2_256 1 +struct VCStorageSizeBlock {uint32_t length;uint8_t reserved0[3 ];@@ -39,4 +50,42 @@ struct VCStorageSizeBlock { };typedef struct VCStorageSizeBlock VCStorageSizeBlock ;include/hw/s390x/ipl/diag320.h struct VCStorageSizeBlock { · line 61
Quoted 10 lines
include/hw/s390x/ipl/diag320.h +9
-0+struct VCBlock { +uint32_t in_len; +uint32_t reserved0; +uint16_t first_vc_index; +uint16_t last_vc_index; +uint32_t reserved1[5 ]; +uint32_t out_len; +uint8_t reserved2[3 ]; +uint8_t version;Sorry, it's been some time since I've looked at this code and I do not remember if this discussion was already had: do we need the version field at all right now? I looked around the future patches and don't see it set or checked anywhere. Though harmless, it may make sense to consume this field by reserved2.
include/hw/s390x/ipl/diag320.h struct VCStorageSizeBlock { · line 67
Quoted 6 lines
include/hw/s390x/ipl/diag320.h +6
-0+uint16_t stored_ct; +uint16_t remain_ct; +uint32_t reserved3[5 ]; +uint8_t vce_buf[]; +}; +typedef struct VCBlock VCBlock ;Other than the nit above, the struct layout is correct.
Quoted 12 lines
include/hw/s390x/ipl/diag320.h +12
-0+ +struct VCEntry { +uint32_t len; +uint8_t flags; +uint8_t key_type; +uint16_t cert_idx; +uint32_t name[16 ]; +uint8_t format; +uint8_t reserved0; +uint16_t keyid_len; +uint8_t reserved1; +uint8_t hash_type;include/hw/s390x/ipl/diag320.h struct VCStorageSizeBlock { · line 89
Quoted 10 lines
include/hw/s390x/ipl/diag320.h +10
-0+uint16_t hash_len; +uint32_t reserved2; +uint32_t cert_len; +uint32_t reserved3[2 ]; +uint16_t hash_offset; +uint16_t cert_offset; +uint32_t reserved4[7 ]; +uint8_t cert_buf[]; +}; +typedef struct VCEntry VCEntry ;Looks good.
From here on, I'll assume the data structures haven't changed between iterations unless explicitly stated in the cover letter change log :)
Quoted 44 lines
include/hw/s390x/ipl/diag320.h +1
-0+#endif target/s390x/diag.c +20
-0@@ -17,6 +17,7 @@#include "s390x-internal.h" #include "hw/watchdog/wdt_diag288.h" #include "system/cpus.h" +#include "hw/s390x/cert-store.h" #include "hw/s390x/ipl.h" #include "hw/s390x/ipl/diag320.h" #include "hw/s390x/s390-virtio-ccw.h" @@ -24,6 +25,7 @@#include "kvm/kvm_s390x.h" #include "target/s390x/kvm/pv.h" #include "qemu/error-report.h" +#include "crypto/x509-utils.h" static inline bool diag_parm_addr_valid (uint64_t addr,size_t size,bool write)@@ -231,8 +233,330 @@ static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1,return DIAG_320_RC_OK ; } +static bool is_cert_valid (S390IPLCertificate cert) +{ +int rc; +Error *err =NULL ; + + rc =qcrypto_x509_check_cert_times (cert.raw, cert.size, &err); +if (rc !=0 ) { +error_report_err (err); +return false; + } + +return true; +} + +static void handle_key_id (VCEntry *vce,S390IPLCertificate cert) +{ +int rc; + g_autofree unsigned char *key_id_data = NULL;
Quoted 9 lines
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 257
Quoted 4 lines
target/s390x/diag.c +4
-0+size_t key_id_len; +Error *err =NULL ; + + key_id_len =CERT_KEY_ID_LEN ;Looking at
qcrypto_x509_get_cert_key_id(), it doesn't look likekey_id_lengets used before it's overwritten bygnutls_hash_get_len(). Is the line above still needed?
Yes, you are correct. key_id_len is not needed here, since it is overwritten by gnutls_hash_get_len(). Passing QCRYPTO_HASH_ALGO_SHA256 to qcrypto_x509_get_cert_key_id() is sufficient and guarantees the expected key ID length. I will remove the unnecessary assignment.
Quoted 144 lines
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 258
Quoted 1 line
target/s390x/diag.c +1
-0+/* key id and key id len */ Remove comment.
Quoted 18 lines
target/s390x/diag.c +18
-0+ rc =qcrypto_x509_get_cert_key_id (cert.raw, cert.size, +QCRYPTO_HASH_ALGO_SHA256 , + &key_id_data, &key_id_len, &err); +if (rc <0 ) { +error_report_err (err); +return ; + } + +if (VCE_HEADER_LEN + key_id_len >be32_to_cpu (vce->len)) { +error_report ("Unable to write key ID: exceeds buffer bounds" ); +return ; + } + + vce->keyid_len =cpu_to_be16 (key_id_len); + +memcpy (vce->cert_buf, key_id_data, key_id_len); +} +target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 286
Quoted 10 lines
target/s390x/diag.c +10
-0+static int handle_hash (VCEntry *vce,S390IPLCertificate cert,uint16_t keyid_field_len) +{ +int rc; +uint16_t hash_offset; + g_autofree void *hash_data = NULL; +size_t hash_len; +Error *err =NULL ; + + hash_len =CERT_HASH_LEN ; +/* hash and hash len */ Remove comment.
Quoted 22 lines
target/s390x/diag.c +22
-0+ hash_data =g_malloc0 (hash_len); + rc =qcrypto_get_x509_cert_fingerprint (cert.raw, cert.size, +QCRYPTO_HASH_ALGO_SHA256 , + hash_data, &hash_len, &err); +if (rc <0 ) { +error_report_err (err); +return -1 ; + } + + hash_offset =VCE_HEADER_LEN + keyid_field_len; +if (hash_offset + hash_len >be32_to_cpu (vce->len)) { +error_report ("Unable to write hash: exceeds buffer bounds" ); +return -1 ; + } + + vce->hash_len =cpu_to_be16 (hash_len); + vce->hash_type =DIAG_320_VCE_HASHTYPE_SHA2_256 ; + vce->hash_offset =cpu_to_be16 (hash_offset); + +memcpy ((uint8_t *)vce + hash_offset, hash_data, hash_len); + +return 0 ;target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 318
Quoted 10 lines
target/s390x/diag.c +10
-0+} + +static int handle_cert (VCEntry *vce,S390IPLCertificate cert,uint16_t hash_field_len) +{ +int rc; +uint16_t cert_offset; + g_autofree uint8_t *cert_der = NULL; +Error *err =NULL ; + +/* certificate in DER format */ Remove comment.
Quoted 19 lines
target/s390x/diag.c +19
-0+ rc =qcrypto_x509_convert_cert_der (cert.raw, cert.size, + &cert_der, &cert.der_size, &err); +if (rc <0 ) { +error_report_err (err); +return -1 ; + } + + cert_offset =be16_to_cpu (vce->hash_offset) + hash_field_len; +if (cert_offset + cert.der_size >be32_to_cpu (vce->len)) { +error_report ("Unable to write certificate: exceeds buffer bounds" ); +return -1 ; + } + + vce->format =DIAG_320_VCE_FORMAT_X509_DER ; + vce->cert_len =cpu_to_be32 (cert.der_size); + vce->cert_offset =cpu_to_be16 (cert_offset); + +memcpy ((uint8_t *)vce + cert_offset, cert_der, cert.der_size); +target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 347
Quoted 10 lines
target/s390x/diag.c +10
-0+return 0 ; +} + +static int get_key_type (S390IPLCertificate cert) +{ +int algo; +int rc; +Error *err =NULL ; + +/* public key algorithm */ Remove comment.
Quoted 42 lines
target/s390x/diag.c +42
-0+ algo =qcrypto_x509_get_pk_algorithm (cert.raw, cert.size, &err); +if (algo <0 ) { +error_report_err (err); +return -1 ; + } + +if (algo ==QCRYPTO_PK_ALGO_ECDSA ) { + rc =qcrypto_x509_check_ecc_curve_p521 (cert.raw, cert.size, &err); +if (rc ==-1 ) { +error_report_err (err); +return -1 ; + } + +return (rc ==1 ) ?DIAG_320_VCE_KEYTYPE_ECDSA_P521 : +DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING ; + } + +return DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING ; +} + +static int build_vce_header (VCEntry *vce,S390IPLCertificate cert,int idx) +{ +int key_type; + + vce->len =cpu_to_be32 (VCE_HEADER_LEN ); + vce->cert_idx =cpu_to_be16 (idx +1 ); +strncpy ((char *)vce->name, (char *)cert.vc_name,VC_NAME_LEN_BYTES ); + + key_type =get_key_type (cert); +if (key_type ==-1 ) { +return -1 ; + } + vce->key_type = key_type; + +return 0 ; +} + +static int build_vce_data (VCEntry *vce,S390IPLCertificate cert) +{ +uint16_t keyid_field_len; +uint16_t hash_field_len; +uint32_t cert_field_len;
Quoted 9 lines
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 394
Quoted 5 lines
target/s390x/diag.c +5
-0+uint32_t vce_len; +int rc; + +handle_key_id (vce, cert); +/* vce key id field length - can be 0 if failed to retrieve */ What is the reason that the key-id may fail to be retrieved and the program is allowed to continue? Is this data insignificant?
The key ID is not currently validated or used by either secure boot or the kernel. In contrast, the certificate and its hash are required and verified as part of the kernel keyring creation or secure boot flow.
Failure to retrieve the key ID does not impact current execution, so I thought it would be acceptable to allow the program to continue.
Please let me know if you see any concerns or have suggestions with this approach.
Quoted 301 lines
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 407
Quoted 13 lines
target/s390x/diag.c +13
-0+ keyid_field_len =ROUND_UP (be16_to_cpu (vce->keyid_len),4 ); + + rc =handle_hash (vce, cert, keyid_field_len); +if (rc) { +return -1 ; + } + hash_field_len =ROUND_UP (be16_to_cpu (vce->hash_len),4 ); + + rc =handle_cert (vce, cert, hash_field_len); +if (rc || !is_cert_valid (cert)) { +return -1 ; + } +/* vce certificate field length */ Remove comment.
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 415
Quoted 8 lines
target/s390x/diag.c +8
-0+ cert_field_len =ROUND_UP (be32_to_cpu (vce->cert_len),4 ); + + vce_len =VCE_HEADER_LEN + keyid_field_len + hash_field_len + cert_field_len; +if (vce_len >be32_to_cpu (vce->len)) { +return -1 ; + } + +/* The certificate is valid and VCE contains the certificate */ Remove comment.
Quoted 9 lines
target/s390x/diag.c +9
-0+ vce->flags |=DIAG_320_VCE_FLAGS_VALID ; + +/* Update vce length to reflect the actual size used by vce */ + vce->len =cpu_to_be32 (vce_len); + +return 0 ; +} + +static VCEntry *diag_320_build_vce (S390IPLCertificate cert,uint32_t vce_len,int idx)target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 434
Quoted 10 lines
target/s390x/diag.c +10
-0+{ + g_autofree VCEntry *vce = NULL; +int rc; + +/* +* Construct VCE +* Allocate enough memory for all certificate data +* (key id, hash and certificate). +* Unused area following the VCE field contains zeros. +*/ Reword to:
``` /* * Allocate enough space for the largest possible size for this VCE. * As the certificate fields (key-id, hash, data) are parsed, the * VCE's length field will be updated accordingly. */ ```Quoted 9 lines
target/s390x/diag.c +9
-0+ vce =g_malloc0 (vce_len); + rc =build_vce_header (vce, cert, idx); +if (rc) { + vce->len =cpu_to_be32 (VCE_INVALID_LEN ); + goto out; + } + + vce->len =cpu_to_be32 (vce_len); + rc =build_vce_data (vce, cert);target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 453
Quoted 10 lines
target/s390x/diag.c +10
-0+if (rc) { + vce->len =cpu_to_be32 (VCE_INVALID_LEN ); + } + +out: +return g_steal_pointer (&vce); +} + +static int handle_diag320_store_vc (S390CPU *cpu,uint64_t addr,uint64_t r1,uintptr_t ra, +S390IPLCertificateStore *qcs)Thought: perhaps the variable should be something other than "qcs"? The "q" doesn't have meaning anymore.
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 456
Quoted 3 lines
target/s390x/diag.c +3
-0+{ + g_autofree VCBlock *vcb = NULL; +size_t vce_offset;Would read better as either "
entry_offset" or just "offset".Quoted 27 lines
target/s390x/diag.c +27
-0+size_t remaining_space; +uint32_t vce_len; +uint16_t first_vc_index; +uint16_t last_vc_index; +uint32_t in_len; + + vcb =g_new0 (VCBlock,1 ); +if (s390_cpu_virt_mem_read (cpu, addr, r1, vcb,sizeof (*vcb))) { +s390_cpu_virt_mem_handle_exc (cpu, ra); +return -1 ; + } + + in_len =be32_to_cpu (vcb->in_len); + first_vc_index =be16_to_cpu (vcb->first_vc_index); + last_vc_index =be16_to_cpu (vcb->last_vc_index); + +if (in_len %TARGET_PAGE_SIZE !=0 ) { +return DIAG_320_RC_INVAL_VCB_LEN ; + } + +if (first_vc_index > last_vc_index) { +return DIAG_320_RC_BAD_RANGE ; + } + + vcb->out_len =VCB_HEADER_LEN ; + +if (first_vc_index ==0 ) {target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 493
Quoted 10 lines
target/s390x/diag.c +10
-0+/* +* Zero is a valid index for the first and last VC index. +* Zero index results in the VCB header and zero certificates returned. +*/ +if (last_vc_index ==0 ) { + goto out; + } + +/* DIAG320 certificate store remains a one origin for cert entries */ + vcb->first_vc_index =1 ;The vcb's first index field should not be modified.
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 495
Quoted 2 lines
target/s390x/diag.c +2
-0+ first_vc_index =1 ; + }The adjustments above are unfortunately confusing to those without the context. The main discrepancy between how DIAG 320 subcode 2's data structures are implemented and how the S390CerticiateStore is implemented is the index origin (former uses 1, latter uses 0). This should be described in detail to clarify why the code above is needed:
``` /* * DIAG 320 subcode 2 expects to query a certificate store that * maintains an index origin of 1. However, the S390IPLCertificateStore * maintains an index origin of 0. Thus, the indices must be adjusted * for correct access into the cert store. A couple of special cases * must also be accounted for. */ /* Special case: both indices are 0; return header with no certs */ if (first_vc_index == 0 && last_vc_index == 0) { goto out; } /* Special case: first > 0; adjust for index origin */ if (first_vc_index > 0) { first_vc_index--; } last_vc_index--; for (i = first_vc_index; i <= last_vc_index; i++) { ... } ```It might make sense to rename the
*_vc_indexvariables -- either something generic, or something specifically denoting they're indexing the S390IPLCertificateStore.target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 500
Quoted 5 lines
target/s390x/diag.c +5
-0+ + vce_offset =VCB_HEADER_LEN ; + remaining_space = in_len -VCB_HEADER_LEN ; + +for (int i = first_vc_index -1 ; i < last_vc_index && i < qcs->count; i++) {There should be special handling in the case where certs are requested outside of the cert store's range. It's not explicitly documented to be handled in this way, but returning
DIAG_320_RC_BAD_RANGEmay be valid (maybe this check should get handled some point before the loop?).target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 506
Quoted 6 lines
target/s390x/diag.c +6
-0+VCEntry *vce; +S390IPLCertificate cert = qcs->certs[i]; +/* +* Each VCE is word aligned. +* Each variable length field within the VCE is also word aligned. +*/ Change this comment to:
``` /* * Each field of the VCE is word-aligned. Allocate enough space to * contain the largest possible size of this entry. The actual size is * calculated later. */ ```target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 510
Quoted 4 lines
target/s390x/diag.c +4
-0+ vce_len =VCE_HEADER_LEN + +ROUND_UP (CERT_KEY_ID_LEN ,4 ) + +ROUND_UP (CERT_HASH_LEN ,4 ) + +ROUND_UP (cert.der_size,4 );The name "
vce_len" is a bit misleading, as this isn't the actual length (which is calculated withinbuild_vce_data()). Rather, this is a calculation that represents the maximum space potentially required to store an entry. Later, this value gets discarded once the actual size of the VCE is calculated.Rename this variable to something more reflective of what this value represents, something like "
vce_max_size".target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 520
Quoted 10 lines
target/s390x/diag.c +10
-0+ +/* +* If there is no more space to store the cert, +* set the remaining verification cert count and +* break early. +*/ +if (remaining_space < vce_len) { + vcb->remain_ct =cpu_to_be16 (last_vc_index - i); +break ; + }Shouldn't the check for remaining space occur after the actual length of the VCE has been calculated?
If this holds true, then the current "
vce_len" calculation above can be moved todiag_320_build_vce(), which would significantly improve readability.Quoted 17 lines
target/s390x/diag.c +17
-0+ + vce =diag_320_build_vce (cert, vce_len, i); + +/* Write VCE */ +if (s390_cpu_virt_mem_write (cpu, addr + vce_offset, r1, + vce,be32_to_cpu (vce->len))) { +s390_cpu_virt_mem_handle_exc (cpu, ra); +g_free (vce); +return -1 ; + } + + vce_offset +=be32_to_cpu (vce->len); + vcb->out_len +=be32_to_cpu (vce->len); + remaining_space -=be32_to_cpu (vce->len); + vcb->stored_ct++; + +g_free (vce);target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 547
Quoted 10 lines
target/s390x/diag.c +10
-0+ } + vcb->stored_ct =cpu_to_be16 (vcb->stored_ct); + +out: + vcb->out_len =cpu_to_be32 (vcb->out_len); +/* +* Write VCB header +* All VCEs have been populated with the latest information +* and write VCB header last. +*/ This comment is confusing because there is a case where no certs may be populated (first and last indices are both 0). Either a one-liner like:
/* Finally, write the VCB header */or it can be removed.
Quoted 29 lines
target/s390x/diag.c +12
-1+if (s390_cpu_virt_mem_write (cpu, addr, r1, vcb,VCB_HEADER_LEN )) { +s390_cpu_virt_mem_handle_exc (cpu, ra); +return -1 ; + } + +return DIAG_320_RC_OK ; +} +QEMU_BUILD_BUG_MSG (sizeof (VCStorageSizeBlock) !=VCSSB_MIN_LEN ,"size of VCStorageSizeBlock is wrong" ); +QEMU_BUILD_BUG_MSG (sizeof (VCBlock) !=VCB_HEADER_LEN ,"size of VCBlock is wrong" ); +QEMU_BUILD_BUG_MSG (sizeof (VCEntry) !=VCE_HEADER_LEN ,"size of VCEntry is wrong" );void handle_diag_320 (CPUS390XState *env,uint64_t r1,uint64_t r3,uintptr_t ra) {@@ -265,7 +589,8 @@ void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)* for now. */ uint32_t ism_word0 =cpu_to_be32 (DIAG_320_ISM_QUERY_SUBCODES | -DIAG_320_ISM_QUERY_VCSI ); +DIAG_320_ISM_QUERY_VCSI | +DIAG_320_ISM_STORE_VC );if (s390_cpu_virt_mem_write (cpu, addr, r1, &ism_word0,sizeof (ism_word0))) {s390_cpu_virt_mem_handle_exc (cpu, ra);@@ -291,6 +616,13 @@ void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra) } env->regs[r1 +1 ] = rc;break ;target/s390x/diag.c void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra) · line 628
Quoted 10 lines
target/s390x/diag.c +7
-0+case DIAG_320_SUBC_STORE_VC : + rc =handle_diag320_store_vc (cpu, addr, r1, ra, qcs); +if (rc ==-1 ) { +return ; + } + env->regs[r1 +1 ] = rc; +break ;default : env->regs[r1 +1 ] =DIAG_320_RC_NOT_SUPPORTED ;break ;The handler code here looks fine.
Re: [PATCH v7 09/29] s390x/diag: Implement DIAG 320 subcode 2
In reply to Zhuoying Cai<..snip...>
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 395
Quoted 11 lines · Zhuoying Cai
target/s390x/diag.c +11
-0+static int build_vce_data (VCEntry *vce,S390IPLCertificate cert) +{ +uint16_t keyid_field_len; +uint16_t hash_field_len; +uint32_t cert_field_len; +uint32_t vce_len; +int rc; + +handle_key_id (vce, cert); +/* vce key id field length - can be 0 if failed to retrieve */ + keyid_field_len =ROUND_UP (be16_to_cpu (vce->keyid_len),4 );
If we fail to retrieve the key, does it makes sense to build the VCE? I think we need the key to verify the signature of the certificate, so why not mark the certificate as invalid?
Quoted 28 lines
target/s390x/diag.c +28
-0+ + rc =handle_hash (vce, cert, keyid_field_len); +if (rc) { +return -1 ; + } + hash_field_len =ROUND_UP (be16_to_cpu (vce->hash_len),4 ); + + rc =handle_cert (vce, cert, hash_field_len); +if (rc || !is_cert_valid (cert)) { +return -1 ; + } +/* vce certificate field length */ + cert_field_len =ROUND_UP (be32_to_cpu (vce->cert_len),4 ); + + vce_len =VCE_HEADER_LEN + keyid_field_len + hash_field_len + cert_field_len; +if (vce_len >be32_to_cpu (vce->len)) { +return -1 ; + } + +/* The certificate is valid and VCE contains the certificate */ + vce->flags |=DIAG_320_VCE_FLAGS_VALID ; + +/* Update vce length to reflect the actual size used by vce */ + vce->len =cpu_to_be32 (vce_len); + +return 0 ; +} +
<...snip...>
Re: [PATCH v7 09/29] s390x/diag: Implement DIAG 320 subcode 2
Quoted 20 lines · Farhan Ali
<..snip...>
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 395
Quoted 11 lines · Zhuoying Cai
target/s390x/diag.c +11
-0+static int build_vce_data (VCEntry *vce,S390IPLCertificate cert) +{ +uint16_t keyid_field_len; +uint16_t hash_field_len; +uint32_t cert_field_len; +uint32_t vce_len; +int rc; + +handle_key_id (vce, cert); +/* vce key id field length - can be 0 if failed to retrieve */ + keyid_field_len =ROUND_UP (be16_to_cpu (vce->keyid_len),4 );If we fail to retrieve the key, does it makes sense to build the VCE? I think we need the key to verify the signature of the certificate, so why not mark the certificate as invalid?
The key ID is used to identify the public key in the certificate, but it is not utilized in the current patch series. Therefore, I thought it would be acceptable to continue building the VCE without this field, though I am open to other ideas or suggestions on how to handle it.
Quoted 31 lines
Quoted 28 lines
target/s390x/diag.c +28
-0+ + rc =handle_hash (vce, cert, keyid_field_len); +if (rc) { +return -1 ; + } + hash_field_len =ROUND_UP (be16_to_cpu (vce->hash_len),4 ); + + rc =handle_cert (vce, cert, hash_field_len); +if (rc || !is_cert_valid (cert)) { +return -1 ; + } +/* vce certificate field length */ + cert_field_len =ROUND_UP (be32_to_cpu (vce->cert_len),4 ); + + vce_len =VCE_HEADER_LEN + keyid_field_len + hash_field_len + cert_field_len; +if (vce_len >be32_to_cpu (vce->len)) { +return -1 ; + } + +/* The certificate is valid and VCE contains the certificate */ + vce->flags |=DIAG_320_VCE_FLAGS_VALID ; + +/* Update vce length to reflect the actual size used by vce */ + vce->len =cpu_to_be32 (vce_len); + +return 0 ; +} +<...snip...>
Re: [PATCH v7 09/29] s390x/diag: Implement DIAG 320 subcode 2
Quoted 20 lines · Zhuoying Cai
Quoted 19 lines · Farhan Ali
<..snip...>
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 395
Quoted 11 lines · Zhuoying Cai
target/s390x/diag.c +11
-0+static int build_vce_data (VCEntry *vce,S390IPLCertificate cert) +{ +uint16_t keyid_field_len; +uint16_t hash_field_len; +uint32_t cert_field_len; +uint32_t vce_len; +int rc; + +handle_key_id (vce, cert); +/* vce key id field length - can be 0 if failed to retrieve */ + keyid_field_len =ROUND_UP (be16_to_cpu (vce->keyid_len),4 );If we fail to retrieve the key, does it makes sense to build the VCE? I think we need the key to verify the signature of the certificate, so why not mark the certificate as invalid?
Quoted 4 lines
The key ID is used to identify the public key in the certificate, but it is not utilized in the current patch series. Therefore, I thought it would be acceptable to continue building the VCE without this field, though I am open to other ideas or suggestions on how to handle it.
I looked at the kernel code again and it doesn't look like the kernel uses the key information today(arch/s390/kernel/cert_store.c). So maybe its fine to continue building the vce today. But IMHO it's a little odd that we are marking a certificate as valid even though we have an invalid key field.
Quoted 30 lines
Quoted 30 lines
Quoted 28 lines
target/s390x/diag.c +28
-0+ + rc =handle_hash (vce, cert, keyid_field_len); +if (rc) { +return -1 ; + } + hash_field_len =ROUND_UP (be16_to_cpu (vce->hash_len),4 ); + + rc =handle_cert (vce, cert, hash_field_len); +if (rc || !is_cert_valid (cert)) { +return -1 ; + } +/* vce certificate field length */ + cert_field_len =ROUND_UP (be32_to_cpu (vce->cert_len),4 ); + + vce_len =VCE_HEADER_LEN + keyid_field_len + hash_field_len + cert_field_len; +if (vce_len >be32_to_cpu (vce->len)) { +return -1 ; + } + +/* The certificate is valid and VCE contains the certificate */ + vce->flags |=DIAG_320_VCE_FLAGS_VALID ; + +/* Update vce length to reflect the actual size used by vce */ + vce->len =cpu_to_be32 (vce_len); + +return 0 ; +} +<...snip...>
Re: [PATCH v7 09/29] s390x/diag: Implement DIAG 320 subcode 2
Quoted 21 lines · Farhan Ali
Quoted 20 lines · Zhuoying Cai
Quoted 19 lines · Farhan Ali
<..snip...>
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 395
Quoted 11 lines · Zhuoying Cai
target/s390x/diag.c +11
-0+static int build_vce_data (VCEntry *vce,S390IPLCertificate cert) +{ +uint16_t keyid_field_len; +uint16_t hash_field_len; +uint32_t cert_field_len; +uint32_t vce_len; +int rc; + +handle_key_id (vce, cert); +/* vce key id field length - can be 0 if failed to retrieve */ + keyid_field_len =ROUND_UP (be16_to_cpu (vce->keyid_len),4 );If we fail to retrieve the key, does it makes sense to build the VCE? I think we need the key to verify the signature of the certificate, so why not mark the certificate as invalid?
Quoted 4 lines
Quoted 4 lines
The key ID is used to identify the public key in the certificate, but it is not utilized in the current patch series. Therefore, I thought it would be acceptable to continue building the VCE without this field, though I am open to other ideas or suggestions on how to handle it.
With respect to the VCE, since the key_id is used in neither the secure IPL process nor the kernel, perhaps remove it from the data structure. I don't see a reason to waste precious BIOS memory holding onto it otherwise.
Quoted 5 lines
I looked at the kernel code again and it doesn't look like the kernel uses the key information
today(arch/s390/kernel/cert_store.c). So maybe its fine to continue building the vce today. But IMHO it's a little odd that we are marking a certificate as valid even though we have an invalid key field.
I agree with Farhan. It's still important to sanity check as much of the certificate as possible to ensure its validity before continuing. The better approach is to handle any errors that arise with the key_id parsing.
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 09/29] s390x/diag: Implement DIAG 320 subcode 2
In reply to Zhuoying CaiQuoted 23 lines · Zhuoying Cai
DIAG 320 subcode 2 provides verification-certificates (VCs) that are in the certificate store. Only X509 certificates in DER format and SHA-256 hash type are recognized.
The subcode value is denoted by setting the second-left-most bit of an 8-byte field.
The Verification Certificate Block (VCB) contains the output data when the operation completes successfully. It includes a common header followed by zero or more Verification Certificate Entries (VCEs), depending on the VCB input length and the VC range (from the first VC index to the last VC index) in the certificate store.
Each VCE contains information about a certificate retrieved from the S390IPLCertificateStore, such as the certificate name, key type, key ID length, hash length, and the raw certificate data.
The key ID and hash are extracted from the raw certificate by the crypto API.Note: SHA2-256 VC hash type is required for retrieving the hash (fingerprint) of the certificate.
Signed-off-by Zhuoying Cai <[email protected]>---
...
include/hw/s390x/ipl/diag320.h struct VCStorageSizeBlock { · line 74
Quoted 6 lines
include/hw/s390x/ipl/diag320.h +6
-0+struct VCEntry { +uint32_t len; +uint8_t flags; +uint8_t key_type; +uint16_t cert_idx; +uint32_t name[16 ];
Why is this defined as an array of uint32_t when it is rather a string instead?
Quoted 15 lines
include/hw/s390x/ipl/diag320.h +15
-0+uint8_t format; +uint8_t reserved0; +uint16_t keyid_len; +uint8_t reserved1; +uint8_t hash_type; +uint16_t hash_len; +uint32_t reserved2; +uint32_t cert_len; +uint32_t reserved3[2 ]; +uint16_t hash_offset; +uint16_t cert_offset; +uint32_t reserved4[7 ]; +uint8_t cert_buf[]; +}; +typedef struct VCEntry VCEntry ;
...
Quoted 21 lines
target/s390x/diag.c +2
-0@@ -17,6 +17,7 @@#include "s390x-internal.h" #include "hw/watchdog/wdt_diag288.h" #include "system/cpus.h" +#include "hw/s390x/cert-store.h" #include "hw/s390x/ipl.h" #include "hw/s390x/ipl/diag320.h" #include "hw/s390x/s390-virtio-ccw.h" @@ -24,6 +25,7 @@#include "kvm/kvm_s390x.h" #include "target/s390x/kvm/pv.h" #include "qemu/error-report.h" +#include "crypto/x509-utils.h" static inline bool diag_parm_addr_valid (uint64_t addr,size_t size,bool write)
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 236
Quoted 4 lines
target/s390x/diag.c +1
-0@@ -231,8 +233,330 @@ static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1,return DIAG_320_RC_OK ; } +static bool is_cert_valid (S390IPLCertificate cert)
You're using call-by-value for all S390IPLCertificate parameters in this patch ... that's quite cumbersome since the struct has a size of ~80 bytes.
Unless there is a very good reason for doing so, I'd suggest that you switch the code to use (const) pointers instead of passing around that struct on the stack.
Quoted 128 lines
target/s390x/diag.c +128
-0+{ +int rc; +Error *err =NULL ; + + rc =qcrypto_x509_check_cert_times (cert.raw, cert.size, &err); +if (rc !=0 ) { +error_report_err (err); +return false; + } + +return true; +} + +static void handle_key_id (VCEntry *vce,S390IPLCertificate cert) +{ +int rc; + g_autofree unsigned char *key_id_data = NULL; +size_t key_id_len; +Error *err =NULL ; + + key_id_len =CERT_KEY_ID_LEN ; +/* key id and key id len */ + rc =qcrypto_x509_get_cert_key_id (cert.raw, cert.size, +QCRYPTO_HASH_ALGO_SHA256 , + &key_id_data, &key_id_len, &err); +if (rc <0 ) { +error_report_err (err); +return ; + } + +if (VCE_HEADER_LEN + key_id_len >be32_to_cpu (vce->len)) { +error_report ("Unable to write key ID: exceeds buffer bounds" ); +return ; + } + + vce->keyid_len =cpu_to_be16 (key_id_len); + +memcpy (vce->cert_buf, key_id_data, key_id_len); +} + +static int handle_hash (VCEntry *vce,S390IPLCertificate cert,uint16_t keyid_field_len) +{ +int rc; +uint16_t hash_offset; + g_autofree void *hash_data = NULL; +size_t hash_len; +Error *err =NULL ; + + hash_len =CERT_HASH_LEN ; +/* hash and hash len */ + hash_data =g_malloc0 (hash_len); + rc =qcrypto_get_x509_cert_fingerprint (cert.raw, cert.size, +QCRYPTO_HASH_ALGO_SHA256 , + hash_data, &hash_len, &err); +if (rc <0 ) { +error_report_err (err); +return -1 ; + } + + hash_offset =VCE_HEADER_LEN + keyid_field_len; +if (hash_offset + hash_len >be32_to_cpu (vce->len)) { +error_report ("Unable to write hash: exceeds buffer bounds" ); +return -1 ; + } + + vce->hash_len =cpu_to_be16 (hash_len); + vce->hash_type =DIAG_320_VCE_HASHTYPE_SHA2_256 ; + vce->hash_offset =cpu_to_be16 (hash_offset); + +memcpy ((uint8_t *)vce + hash_offset, hash_data, hash_len); + +return 0 ; +} + +static int handle_cert (VCEntry *vce,S390IPLCertificate cert,uint16_t hash_field_len) +{ +int rc; +uint16_t cert_offset; + g_autofree uint8_t *cert_der = NULL; +Error *err =NULL ; + +/* certificate in DER format */ + rc =qcrypto_x509_convert_cert_der (cert.raw, cert.size, + &cert_der, &cert.der_size, &err); +if (rc <0 ) { +error_report_err (err); +return -1 ; + } + + cert_offset =be16_to_cpu (vce->hash_offset) + hash_field_len; +if (cert_offset + cert.der_size >be32_to_cpu (vce->len)) { +error_report ("Unable to write certificate: exceeds buffer bounds" ); +return -1 ; + } + + vce->format =DIAG_320_VCE_FORMAT_X509_DER ; + vce->cert_len =cpu_to_be32 (cert.der_size); + vce->cert_offset =cpu_to_be16 (cert_offset); + +memcpy ((uint8_t *)vce + cert_offset, cert_der, cert.der_size); + +return 0 ; +} + +static int get_key_type (S390IPLCertificate cert) +{ +int algo; +int rc; +Error *err =NULL ; + +/* public key algorithm */ + algo =qcrypto_x509_get_pk_algorithm (cert.raw, cert.size, &err); +if (algo <0 ) { +error_report_err (err); +return -1 ; + } + +if (algo ==QCRYPTO_PK_ALGO_ECDSA ) { + rc =qcrypto_x509_check_ecc_curve_p521 (cert.raw, cert.size, &err); +if (rc ==-1 ) { +error_report_err (err); +return -1 ; + } + +return (rc ==1 ) ?DIAG_320_VCE_KEYTYPE_ECDSA_P521 : +DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING ; + } +
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 374
Quoted 10 lines
target/s390x/diag.c +10
-0+return DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING ; +} + +static int build_vce_header (VCEntry *vce,S390IPLCertificate cert,int idx) +{ +int key_type; + + vce->len =cpu_to_be32 (VCE_HEADER_LEN ); + vce->cert_idx =cpu_to_be16 (idx +1 ); +strncpy ((char *)vce->name, (char *)cert.vc_name,VC_NAME_LEN_BYTES );
strncpy is often tripping up static analyzers when you use it like this.
Please consider using memcpy(), pstrcpy() or strpadcpy() instead.
(I guess memcpy is the right thing to use here since vc_name has already been initialized with strpadcpy ?)
target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 383
Quoted 8 lines
target/s390x/diag.c +8
-0+ key_type =get_key_type (cert); +if (key_type ==-1 ) { +return -1 ; + } + vce->key_type = key_type; + +return 0 ; +}
Thomas
Re: [PATCH v7 09/29] s390x/diag: Implement DIAG 320 subcode 2
Thanks for the feedback!
Quoted 25 lines · Thomas Huth
Quoted 23 lines · Zhuoying Cai
DIAG 320 subcode 2 provides verification-certificates (VCs) that are in the certificate store. Only X509 certificates in DER format and SHA-256 hash type are recognized.
The subcode value is denoted by setting the second-left-most bit of an 8-byte field.
The Verification Certificate Block (VCB) contains the output data when the operation completes successfully. It includes a common header followed by zero or more Verification Certificate Entries (VCEs), depending on the VCB input length and the VC range (from the first VC index to the last VC index) in the certificate store.
Each VCE contains information about a certificate retrieved from the S390IPLCertificateStore, such as the certificate name, key type, key ID length, hash length, and the raw certificate data.
The key ID and hash are extracted from the raw certificate by the crypto API.Note: SHA2-256 VC hash type is required for retrieving the hash (fingerprint) of the certificate.
Signed-off-by Zhuoying Cai <[email protected]>---
...
Quoted 9 lines
include/hw/s390x/ipl/diag320.h struct VCStorageSizeBlock { · line 74
Quoted 6 lines
include/hw/s390x/ipl/diag320.h +6
-0+struct VCEntry { +uint32_t len; +uint8_t flags; +uint8_t key_type; +uint16_t cert_idx; +uint32_t name[16 ];Why is this defined as an array of
uint32_twhen it is rather a string instead?
This field contains the 64-byte name of the certificate in EBCDIC format. I think it would be more appropriate to define it as uint8_t name[64].
Quoted 204 lines
Quoted 15 lines
include/hw/s390x/ipl/diag320.h +15
-0+uint8_t format; +uint8_t reserved0; +uint16_t keyid_len; +uint8_t reserved1; +uint8_t hash_type; +uint16_t hash_len; +uint32_t reserved2; +uint32_t cert_len; +uint32_t reserved3[2 ]; +uint16_t hash_offset; +uint16_t cert_offset; +uint32_t reserved4[7 ]; +uint8_t cert_buf[]; +}; +typedef struct VCEntry VCEntry ;...
Quoted 21 lines
target/s390x/diag.c +2
-0@@ -17,6 +17,7 @@#include "s390x-internal.h" #include "hw/watchdog/wdt_diag288.h" #include "system/cpus.h" +#include "hw/s390x/cert-store.h" #include "hw/s390x/ipl.h" #include "hw/s390x/ipl/diag320.h" #include "hw/s390x/s390-virtio-ccw.h" @@ -24,6 +25,7 @@#include "kvm/kvm_s390x.h" #include "target/s390x/kvm/pv.h" #include "qemu/error-report.h" +#include "crypto/x509-utils.h" static inline bool diag_parm_addr_valid (uint64_t addr,size_t size,bool write)target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 236
Quoted 4 lines
target/s390x/diag.c +1
-0@@ -231,8 +233,330 @@ static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1,return DIAG_320_RC_OK ; } +static bool is_cert_valid (S390IPLCertificate cert)You're using call-by-value for all S390IPLCertificate parameters in this patch ... that's quite cumbersome since the struct has a size of ~80 bytes.
Unless there is a very good reason for doing so, I'd suggest that you switch the code to use (const) pointers instead of passing around that struct on the stack.
Quoted 128 lines
target/s390x/diag.c +128
-0+{ +int rc; +Error *err =NULL ; + + rc =qcrypto_x509_check_cert_times (cert.raw, cert.size, &err); +if (rc !=0 ) { +error_report_err (err); +return false; + } + +return true; +} + +static void handle_key_id (VCEntry *vce,S390IPLCertificate cert) +{ +int rc; + g_autofree unsigned char *key_id_data = NULL; +size_t key_id_len; +Error *err =NULL ; + + key_id_len =CERT_KEY_ID_LEN ; +/* key id and key id len */ + rc =qcrypto_x509_get_cert_key_id (cert.raw, cert.size, +QCRYPTO_HASH_ALGO_SHA256 , + &key_id_data, &key_id_len, &err); +if (rc <0 ) { +error_report_err (err); +return ; + } + +if (VCE_HEADER_LEN + key_id_len >be32_to_cpu (vce->len)) { +error_report ("Unable to write key ID: exceeds buffer bounds" ); +return ; + } + + vce->keyid_len =cpu_to_be16 (key_id_len); + +memcpy (vce->cert_buf, key_id_data, key_id_len); +} + +static int handle_hash (VCEntry *vce,S390IPLCertificate cert,uint16_t keyid_field_len) +{ +int rc; +uint16_t hash_offset; + g_autofree void *hash_data = NULL; +size_t hash_len; +Error *err =NULL ; + + hash_len =CERT_HASH_LEN ; +/* hash and hash len */ + hash_data =g_malloc0 (hash_len); + rc =qcrypto_get_x509_cert_fingerprint (cert.raw, cert.size, +QCRYPTO_HASH_ALGO_SHA256 , + hash_data, &hash_len, &err); +if (rc <0 ) { +error_report_err (err); +return -1 ; + } + + hash_offset =VCE_HEADER_LEN + keyid_field_len; +if (hash_offset + hash_len >be32_to_cpu (vce->len)) { +error_report ("Unable to write hash: exceeds buffer bounds" ); +return -1 ; + } + + vce->hash_len =cpu_to_be16 (hash_len); + vce->hash_type =DIAG_320_VCE_HASHTYPE_SHA2_256 ; + vce->hash_offset =cpu_to_be16 (hash_offset); + +memcpy ((uint8_t *)vce + hash_offset, hash_data, hash_len); + +return 0 ; +} + +static int handle_cert (VCEntry *vce,S390IPLCertificate cert,uint16_t hash_field_len) +{ +int rc; +uint16_t cert_offset; + g_autofree uint8_t *cert_der = NULL; +Error *err =NULL ; + +/* certificate in DER format */ + rc =qcrypto_x509_convert_cert_der (cert.raw, cert.size, + &cert_der, &cert.der_size, &err); +if (rc <0 ) { +error_report_err (err); +return -1 ; + } + + cert_offset =be16_to_cpu (vce->hash_offset) + hash_field_len; +if (cert_offset + cert.der_size >be32_to_cpu (vce->len)) { +error_report ("Unable to write certificate: exceeds buffer bounds" ); +return -1 ; + } + + vce->format =DIAG_320_VCE_FORMAT_X509_DER ; + vce->cert_len =cpu_to_be32 (cert.der_size); + vce->cert_offset =cpu_to_be16 (cert_offset); + +memcpy ((uint8_t *)vce + cert_offset, cert_der, cert.der_size); + +return 0 ; +} + +static int get_key_type (S390IPLCertificate cert) +{ +int algo; +int rc; +Error *err =NULL ; + +/* public key algorithm */ + algo =qcrypto_x509_get_pk_algorithm (cert.raw, cert.size, &err); +if (algo <0 ) { +error_report_err (err); +return -1 ; + } + +if (algo ==QCRYPTO_PK_ALGO_ECDSA ) { + rc =qcrypto_x509_check_ecc_curve_p521 (cert.raw, cert.size, &err); +if (rc ==-1 ) { +error_report_err (err); +return -1 ; + } + +return (rc ==1 ) ?DIAG_320_VCE_KEYTYPE_ECDSA_P521 : +DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING ; + } +target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 374
Quoted 10 lines
target/s390x/diag.c +10
-0+return DIAG_320_VCE_KEYTYPE_SELF_DESCRIBING ; +} + +static int build_vce_header (VCEntry *vce,S390IPLCertificate cert,int idx) +{ +int key_type; + + vce->len =cpu_to_be32 (VCE_HEADER_LEN ); + vce->cert_idx =cpu_to_be16 (idx +1 ); +strncpy ((char *)vce->name, (char *)cert.vc_name,VC_NAME_LEN_BYTES );strncpy is often tripping up static analyzers when you use it like this. Please consider using
memcpy(),pstrcpy()orstrpadcpy()instead. (I guess memcpy is the right thing to use here sincevc_namehas already been initialized with strpadcpy ?)target/s390x/diag.c static int handle_diag320_query_vcsi(S390CPU *cpu, uint64_t addr, uint64_t r1, · line 383
Quoted 8 lines
target/s390x/diag.c +8
-0+ key_type =get_key_type (cert); +if (key_type ==-1 ) { +return -1 ; + } + vce->key_type = key_type; + +return 0 ; +}Thomas
PATCH v7 10/29 s390x/diag: Introduce DIAG 508 for secure IPL operations
In reply to Zhuoying CaiFrom: Collin Walling <[email protected]>
In order to support secure IPL (aka secure boot) for the s390-ccw BIOS, a new s390 DIAGNOSE instruction is introduced to leverage QEMU for handling operations such as signature verification and certificate retrieval.
Currently, only subcode 0 is supported with this patch, which is used to query a bitmap of which subcodes are supported.
--- docs/specs/s390x-secure-ipl.rst | 18 ++++++++++++++++++ include/hw/s390x/ipl/diag508.h | 15 +++++++++++++++ target/s390x/diag.c | 27 +++++++++++++++++++++++++++ target/s390x/kvm/kvm.c | 14 ++++++++++++++ target/s390x/s390x-internal.h | 2 ++ target/s390x/tcg/misc_helper.c | 7 +++++++ 6 files changed, 83 insertions(+) create mode 100644 include/hw/s390x/ipl/diag508.h
docs/specs/s390x-secure-ipl.rst +18 -0
@@ -51,3 +51,21 @@ Subcode 2 - store verification certificates insufficient storage specified in the VCB input length field. VCE contains various information of a VC from the CS. + + +Secure IPL Data Structures, Facilities, and Functions +===================================================== + +DIAGNOSE function code 'X'508' - IPL extensions +--------------------------------------------------- + +DIAGNOSE 'X'508' is reserved for guest use in order to facilitate communication +of additional IPL operations that cannot be handled by userspace, such as +signature verification for secure IPL. + +If the function code specifies 0x508, IPL extension functions are performed. +These functions are meant to provide extended functionality for s390 guest boot +that requires assistance from QEMU. + +Subcode 0 - query installed subcodes + Returns a 64-bit mask indicating which subcodes are supported.
include/hw/s390x/ipl/diag508.h new file +15 -0
@@ -0,0 +1,15 @@ +/* +* S/390 DIAGNOSE 508 definitions and structures +* +* Copyright 2025 IBM Corp. +* Author(s): Collin Walling <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef S390X_DIAG508_H +#define S390X_DIAG508_H + +#define DIAG_508_SUBC_QUERY_SUBC 0x0000 + +#endif
target/s390x/diag.c +27 -0
@@ -20,6 +20,7 @@#include "hw/s390x/cert-store.h" #include "hw/s390x/ipl.h" #include "hw/s390x/ipl/diag320.h" +#include "hw/s390x/ipl/diag508.h" #include "hw/s390x/s390-virtio-ccw.h" #include "system/kvm.h" #include "kvm/kvm_s390x.h"
@@ -628,3 +629,29 @@ void handle_diag_320(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)break ; } } + +void handle_diag_508 (CPUS390XState *env,uint64_t r1,uint64_t r3,uintptr_t ra) +{ +uint64_t subcode = env->regs[r3]; +int rc; + +if (env->psw.mask &PSW_MASK_PSTATE ) { +s390_program_interrupt (env,PGM_PRIVILEGED , ra); +return ; + } + +if ((subcode & ~0x0ffffULL ) || (r1 &1 )) { +s390_program_interrupt (env,PGM_SPECIFICATION , ra); +return ; + } + +switch (subcode) { +case DIAG_508_SUBC_QUERY_SUBC : + rc =0 ; +break ; +default : +s390_program_interrupt (env,PGM_SPECIFICATION , ra); +return ; + } + env->regs[r1 +1 ] = rc; +}
target/s390x/kvm/kvm.c +14 -0
@@ -101,6 +101,7 @@#define DIAG_CERT_STORE 0x320#define DIAG_KVM_HYPERCALL 0x500#define DIAG_KVM_BREAKPOINT 0x501 +#define DIAG_SECURE_IPL 0x508#define ICPT_INSTRUCTION 0x04#define ICPT_PROGRAM 0x08
@@ -1571,6 +1572,16 @@ static void kvm_handle_diag_320(S390CPU *cpu, struct kvm_run *run)handle_diag_320 (&cpu->env, r1, r3,RA_IGNORED ); } +static void kvm_handle_diag_508 (S390CPU *cpu,struct kvm_run *run) +{ +uint64_t r1, r3; + + r1 = (run->s390_sieic.ipa &0x00f0 ) >>4 ; + r3 = run->s390_sieic.ipa &0x000f ; + +handle_diag_508 (&cpu->env, r1, r3,RA_IGNORED ); +} +#define DIAG_KVM_CODE_MASK 0x000000000000ffffstatic int handle_diag (S390CPU *cpu,struct kvm_run *run,uint32_t ipb)
@@ -1604,6 +1615,9 @@ static int handle_diag(S390CPU *cpu, struct kvm_run *run, uint32_t ipb)case DIAG_CERT_STORE :kvm_handle_diag_320 (cpu, run);break ; +case DIAG_SECURE_IPL : +kvm_handle_diag_508 (cpu, run); +break ;default :trace_kvm_insn_diag (func_code);kvm_s390_program_interrupt (cpu,PGM_SPECIFICATION );
target/s390x/s390x-internal.h +2 -0
@@ -390,6 +390,8 @@ void handle_diag_308(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra);void handle_diag_320 (CPUS390XState *env,uint64_t r1,uint64_t r3,uintptr_t ra); +void handle_diag_508 (CPUS390XState *env,uint64_t r1,uint64_t r3, +uintptr_t ra);/* translate.c */
target/s390x/tcg/misc_helper.c +7 -0
@@ -149,6 +149,13 @@ void HELPER(diag)(CPUS390XState *env, uint32_t r1, uint32_t r3, uint32_t num)bql_unlock (); r =0 ;break ; +case 0x508 : +/* secure ipl operations */ +bql_lock (); +handle_diag_508 (env, r1, r3,GETPC ()); +bql_unlock (); + r =0 ; +break ;default : r =-1 ;break ;
Signature · 1 line
2.51.1
Re: [PATCH v7 10/29] s390x/diag: Introduce DIAG 508 for secure IPL operations
Quoted 20 lines · Zhuoying Cai
From: Collin Walling <[email protected]>
In order to support secure IPL (aka secure boot) for the s390-ccw BIOS, a new s390 DIAGNOSE instruction is introduced to leverage QEMU for handling operations such as signature verification and certificate retrieval.
Currently, only subcode 0 is supported with this patch, which is used to query a bitmap of which subcodes are supported.
Signed-off-by Collin Walling <[email protected]>Reviewed-by Farhan Ali <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 18 ++++++++++++++++++ include/hw/s390x/ipl/diag508.h | 15 +++++++++++++++ target/s390x/diag.c | 27 +++++++++++++++++++++++++++ target/s390x/kvm/kvm.c | 14 ++++++++++++++ target/s390x/s390x-internal.h | 2 ++ target/s390x/tcg/misc_helper.c | 7 +++++++ 6 files changed, 83 insertions(+)
Quoted 1 line
create mode 100644
include/hw/s390x/ipl/diag508.h
PATCH v7 11/29 crypto/x509-utils: Add helper functions for DIAG 508 subcode 1
In reply to Zhuoying CaiIntroduce helper functions to support signature verification required by DIAG 508 subcode 1:
qcrypto_pkcs7_convert_sig_pem() – converts a signature from DER to PEM format qcrypto_x509_verify_sig() – verifies the provided data against the given signature
These functions enable basic signature verification support.
--- crypto/x509-utils.c | 108 ++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 41 ++++++++++++++ 2 files changed, 149 insertions(+)
crypto/x509-utils.c +108 -0
@@ -16,6 +16,7 @@#include <gnutls/gnutls.h> #include <gnutls/crypto.h> #include <gnutls/x509.h> +#include <gnutls/pkcs7.h> static const int qcrypto_to_gnutls_hash_alg_map[QCRYPTO_HASH_ALGO__MAX ] = { [QCRYPTO_HASH_ALGO_MD5 ] =GNUTLS_DIG_MD5 ,
@@ -341,6 +342,96 @@ int qcrypto_x509_check_ecc_curve_p521(uint8_t *cert, size_t size, Error **errp)return 0 ; } +int qcrypto_pkcs7_convert_sig_pem (uint8_t *sig,size_t sig_size, +uint8_t **result,size_t *resultlen, +Error **errp) +{ +int ret =-1 ; +int rc; +gnutls_pkcs7_t signature; +gnutls_datum_t sig_datum_der = {.data = sig, .size = sig_size}; +gnutls_datum_t sig_datum_pem = {.data =NULL , .size =0 }; + + rc =gnutls_pkcs7_init (&signature); +if (rc <0 ) { +error_setg (errp,"Failed to initalize pkcs7 data: %s" ,gnutls_strerror (rc)); +return ret; + } + + rc =gnutls_pkcs7_import (signature, &sig_datum_der,GNUTLS_X509_FMT_DER ); +if (rc !=0 ) { +error_setg (errp,"Failed to import signature: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_pkcs7_export2 (signature,GNUTLS_X509_FMT_PEM , &sig_datum_pem); +if (rc !=0 ) { +error_setg (errp,"Failed to convert signature to PEM format: %s" , +gnutls_strerror (rc)); + goto cleanup; + } + + *resultlen = sig_datum_pem.size; + *result =g_memdup2 (sig_datum_pem.data, sig_datum_pem.size); + + ret =0 ; + +cleanup: +gnutls_pkcs7_deinit (signature); +gnutls_free (sig_datum_pem.data); +return ret; +} + +int qcrypto_x509_verify_sig (uint8_t *cert,size_t cert_size, +uint8_t *comp,size_t comp_size, +uint8_t *sig,size_t sig_size,Error **errp) +{ +int rc; +int ret =-1 ; +gnutls_x509_crt_t crt =NULL ; +gnutls_pkcs7_t signature =NULL ; +gnutls_datum_t cert_datum = {.data = cert, .size = cert_size}; +gnutls_datum_t data_datum = {.data = comp, .size = comp_size}; +gnutls_datum_t sig_datum = {.data = sig, .size = sig_size}; + + rc =gnutls_x509_crt_init (&crt); +if (rc <0 ) { +error_setg (errp,"Failed to initialize certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_x509_crt_import (crt, &cert_datum,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import certificate: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_pkcs7_init (&signature); +if (rc <0 ) { +error_setg (errp,"Failed to initalize pkcs7 data: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_pkcs7_import (signature, &sig_datum ,GNUTLS_X509_FMT_PEM ); +if (rc !=0 ) { +error_setg (errp,"Failed to import signature: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + rc =gnutls_pkcs7_verify_direct (signature, crt,0 , &data_datum,0 ); +if (rc !=0 ) { +error_setg (errp,"Failed to verify signature: %s" ,gnutls_strerror (rc)); + goto cleanup; + } + + ret =0 ; + +cleanup: +gnutls_x509_crt_deinit (crt); +gnutls_pkcs7_deinit (signature); +return ret; +} +#else /* ! CONFIG_GNUTLS */ int qcrypto_get_x509_cert_fingerprint(uint8_t *cert, size_t size,
@@ -390,4 +481,21 @@ int qcrypto_x509_check_ecc_curve_p521(uint8_t *cert, size_t size, Error **errp) return -1; } +int qcrypto_pkcs7_convert_sig_pem (uint8_t *sig,size_t sig_size, +uint8_t **result, +size_t *resultlen, +Error **errp) +{ +error_setg (errp,"GNUTLS is required to export pkcs7 signature" ); +return -1 ; +} + +int qcrypto_x509_verify_sig (uint8_t *cert,size_t cert_size, +uint8_t *comp,size_t comp_size, +uint8_t *sig,size_t sig_size,Error **errp) +{ +error_setg (errp,"GNUTLS is required for signature-verification support" ); +return -1 ; +} +#endif /* ! CONFIG_GNUTLS */
include/crypto/x509-utils.h +41 -0
45 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v7 11/29] crypto/x509-utils: Add helper functions for DIAG 508 subcode 1
Quoted 6 lines · Zhuoying Cai
Introduce helper functions to support signature verification required by DIAG 508 subcode 1:
qcrypto_pkcs7_convert_sig_pem()– converts a signature from DER to PEM formatqcrypto_x509_verify_sig()– verifies the provided data against the given signature
Quoted 7 lines
These functions enable basic signature verification support.
Signed-off-by Zhuoying Cai <[email protected]>--- crypto/x509-utils.c | 108 ++++++++++++++++++++++++++++++++++++ include/crypto/x509-utils.h | 41 ++++++++++++++ 2 files changed, 149 insertions(+)
Re: [PATCH v7 11/29] crypto/x509-utils: Add helper functions for DIAG 508 subcode 1
In reply to Zhuoying CaiQuoted 9 lines · Zhuoying Cai
Introduce helper functions to support signature verification required by DIAG 508 subcode 1:
qcrypto_pkcs7_convert_sig_pem()– converts a signature from DER to PEM formatqcrypto_x509_verify_sig()– verifies the provided data against the given signatureThese functions enable basic signature verification support.
Signed-off-by Zhuoying Cai<[email protected]>
PATCH v7 12/29 s390x/diag: Implement DIAG 508 subcode 1 for signature verification
In reply to Zhuoying CaiFrom: Collin Walling <[email protected]>
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
--- docs/specs/s390x-secure-ipl.rst | 17 ++++++ include/hw/s390x/ipl/diag508.h | 26 ++++++++ target/s390x/diag.c | 103 +++++++++++++++++++++++++++++++- 3 files changed, 145 insertions(+), 1 deletion(-)
docs/specs/s390x-secure-ipl.rst +17 -0
@@ -69,3 +69,20 @@ that requires assistance from QEMU. Subcode 0 - query installed subcodes Returns a 64-bit mask indicating which subcodes are supported. + +Subcode 1 - perform signature verification + Perform signature-verification on a signed component, using certificates + from the certificate store and leveraging qcrypto libraries to perform + this operation. + + Note: verification of initrd is not supported. + + 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
include/hw/s390x/ipl/diag508.h +26 -0
@@ -11,5 +11,31 @@#define S390X_DIAG508_H #define DIAG_508_SUBC_QUERY_SUBC 0x0000 +#define DIAG_508_SUBC_SIG_VERIF 0x8000 + +#define DIAG_508_RC_OK 0x0001 +#define DIAG_508_RC_NO_CERTS 0x0102 +#define DIAG_508_RC_INVAL_COMP_DATA 0x0202 +#define DIAG_508_RC_INVAL_PKCS7_SIG 0x0302 +#define DIAG_508_RC_FAIL_VERIF 0x0402 +#define DIAG_508_RC_INVAL_LEN 0x0502 + +#define DIAG_508_MAX_COMP_LEN 0x10000000 +#define DIAG_508_MAX_SIG_LEN 4096 + +struct Diag508SigVerifBlock { +uint32_t length; +uint8_t reserved0[3 ]; +uint8_t version; +uint32_t reserved[2 ]; +uint8_t cert_store_index; +uint8_t reserved1[7 ]; +uint64_t cert_len; +uint64_t comp_len; +uint64_t comp_addr; +uint64_t sig_len; +uint64_t sig_addr; +}; +typedef struct Diag508SigVerifBlock Diag508SigVerifBlock ;#endif
target/s390x/diag.c +102 -1
118 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v7 12/29] s390x/diag: Implement DIAG 508 subcode 1 for signature verification
Quoted 28 lines · Zhuoying Cai
From: Collin Walling <[email protected]>
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
Quoted 3 lines
Signed-off-by Collin Walling <[email protected]>Signed-off-by Zhuoying Cai <[email protected]>---
Re: [PATCH v7 12/29] s390x/diag: Implement DIAG 508 subcode 1 for signature verification
In reply to Zhuoying CaiQuoted 71 lines · Zhuoying Cai
From: Collin Walling <[email protected]>
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 invalidSigned-off-by Collin Walling <[email protected]>Signed-off-by Zhuoying Cai <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 17 ++++++ include/hw/s390x/ipl/diag508.h | 26 ++++++++ target/s390x/diag.c | 103 +++++++++++++++++++++++++++++++- 3 files changed, 145 insertions(+), 1 deletion(-)docs/specs/s390x-secure-ipl.rst +17
-0@@ -69,3 +69,20 @@ that requires assistance from QEMU. Subcode 0 - query installed subcodes Returns a 64-bit mask indicating which subcodes are supported. + +Subcode 1 - perform signature verification + Perform signature-verification on a signed component, using certificates + from the certificate store and leveraging qcrypto libraries to perform + this operation. + + Note: verification of initrd is not supported. + + 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 invalidinclude/hw/s390x/ipl/diag508.h +2
-0@@ -11,5 +11,31 @@#define S390X_DIAG508_H #define DIAG_508_SUBC_QUERY_SUBC 0x0000 +#define DIAG_508_SUBC_SIG_VERIF 0x8000 +
include/hw/s390x/ipl/diag508.h line 25
Quoted 10 lines
include/hw/s390x/ipl/diag508.h +10
-0+#define DIAG_508_RC_OK 0x0001 +#define DIAG_508_RC_NO_CERTS 0x0102 +#define DIAG_508_RC_INVAL_COMP_DATA 0x0202 +#define DIAG_508_RC_INVAL_PKCS7_SIG 0x0302 +#define DIAG_508_RC_FAIL_VERIF 0x0402 +#define DIAG_508_RC_INVAL_LEN 0x0502 + +#define DIAG_508_MAX_COMP_LEN 0x10000000 +#define DIAG_508_MAX_SIG_LEN 4096 +
Based on my understanding, these values for component and signature length are based on the max that is possible today (v6 discussion https://lore.kernel.org/all/[email protected]/). These values are not defined in the architecture and its possible we may have to update them in the future.Maybe we can add a comment for this?
<..snip..>
Quoted 26 lines
include/hw/s390x/ipl/diag508.h +26
-0+static int handle_diag508_sig_verif (uint64_t addr) +{ +int verified; +uint32_t svb_len; +uint64_t comp_len, comp_addr; +uint64_t sig_len, sig_addr; + g_autofree uint8_t *comp = NULL; + g_autofree uint8_t *sig = NULL; + g_autofree Diag508SigVerifBlock *svb = NULL; +size_t svb_size =sizeof (Diag508SigVerifBlock); +S390IPLCertificateStore *qcs =s390_ipl_get_certificate_store (); + +if (!qcs->count) { +return DIAG_508_RC_NO_CERTS ; + } + + svb =g_new0 (Diag508SigVerifBlock,1 ); +cpu_physical_memory_read (addr, svb, svb_size); + + svb_len =be32_to_cpu (svb->length); +if (svb_len != svb_size) { +return DIAG_508_RC_INVAL_LEN ; + } + + comp_len =be64_to_cpu (svb->comp_len); + comp_addr =be64_to_cpu (svb->comp_addr);
include/hw/s390x/ipl/diag508.h
Quoted 10 lines
include/hw/s390x/ipl/diag508.h +10
-0+ sig_len =be64_to_cpu (svb->sig_len); + sig_addr =be64_to_cpu (svb->sig_addr); + +if (!comp_len || comp_len >DIAG_508_MAX_COMP_LEN || !comp_addr) { +return DIAG_508_RC_INVAL_COMP_DATA ; + } + +if (!sig_len || sig_len >DIAG_508_MAX_SIG_LEN || !sig_addr) { +return DIAG_508_RC_INVAL_PKCS7_SIG ; + }
And maybe a warning to indicate the failure is due to signature/component length being larger than our currently defined ones?
Quoted 24 lines
include/hw/s390x/ipl/diag508.h +23
-0+ + comp =g_malloc0 (comp_len); +cpu_physical_memory_read (comp_addr, comp, comp_len); + + sig =g_malloc0 (sig_len); +cpu_physical_memory_read (sig_addr, sig, sig_len); + +for (int i =0 ; i < qcs->count; i++) { + verified =diag_508_verify_sig (qcs->certs[i].raw, + qcs->certs[i].size, + comp, comp_len, + sig, sig_len); +if (verified) { + svb->cert_store_index = i; + svb->cert_len =cpu_to_be64 (qcs->certs[i].der_size); +cpu_physical_memory_write (addr, svb, svb_size); +return DIAG_508_RC_OK ; + } + } + +return DIAG_508_RC_FAIL_VERIF ; +} +;
<..snip..>
PATCH v7 13/29 pc-bios/s390-ccw: Introduce IPL Information Report Block (IIRB)
In reply to Zhuoying CaiThe IPL information report block (IIRB) contains information used to locate IPL records and to report the results of signature verification of one or more secure components of the load device.
IIRB is stored immediately following the IPL Parameter Block. Results on component verification in any case (failure or success) are stored.
--- docs/specs/s390x-secure-ipl.rst | 13 +++++++ pc-bios/s390-ccw/iplb.h | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+)
docs/specs/s390x-secure-ipl.rst +13 -0
@@ -86,3 +86,16 @@ Subcode 1 - perform signature verification * ``0x0302``: PKCS#7 format signature is invalid * ``0x0402``: signature-verification failed * ``0x0502``: length of Diag508SigVerifBlock is invalid + +IPL Information Report Block +---------------------------- + +The IPL Parameter Block (IPLPB), utilized for IPL operation, is extended with an +IPL Information Report Block (IIRB), which contains the results from secure IPL +operations such as: + +* component data +* verification results +* certificate data + +The guest kernel will inspect the IIRB and build the keyring.
pc-bios/s390-ccw/iplb.h +62 -0
68 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v7 13/29] pc-bios/s390-ccw: Introduce IPL Information Report Block (IIRB)
Quoted 24 lines · Zhuoying Cai
The IPL information report block (IIRB) contains information used to locate IPL records and to report the results of signature verification of one or more secure components of the load device.
IIRB is stored immediately following the IPL Parameter Block. Results on component verification in any case (failure or success) are stored.
Signed-off-by Zhuoying Cai <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 13 +++++++ pc-bios/s390-ccw/iplb.h | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+)docs/specs/s390x-secure-ipl.rst +3
-0@@ -86,3 +86,16 @@ Subcode 1 - perform signature verification * ``0x0302``: PKCS#7 format signature is invalid * ``0x0402``: signature-verification failed * ``0x0502``: length of Diag508SigVerifBlock is invalid + +IPL Information Report Block +----------------------------
docs/specs/s390x-secure-ipl.rst Subcode 1 - perform signature verification · line 101
Quoted 10 lines
docs/specs/s390x-secure-ipl.rst +10
-0+ +The IPL Parameter Block (IPLPB), utilized for IPL operation, is extended with an +IPL Information Report Block (IIRB), which contains the results from secure IPL +operations such as: + +* component data +* verification results +* certificate data + +The guest kernel will inspect the IIRB and build the keyring.
This needs more elaboration. Is the data listed above used in the keyring? Maybe rewording to "The guest's kernel will use this data in the IIRB when building its keyring." ?
pc-bios/s390-ccw/iplb.h extern QemuIplParameters qipl; · line 28
Quoted 11 lines
pc-bios/s390-ccw/iplb.h +3
-0@@ -23,6 +23,68 @@ extern QemuIplParameters qipl; extern IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE)));extern bool have_iplb; +struct IplInfoReportBlockHeader { +uint32_t len; +uint8_t iirb_flags;
s/iirb_flags/flags
pc-bios/s390-ccw/iplb.h extern QemuIplParameters qipl; · line 30
Quoted 2 lines
pc-bios/s390-ccw/iplb.h +2
-0+uint8_t reserved1[2 ]; +uint8_t version;
Is version used? If not, merge into one reserved field.
pc-bios/s390-ccw/iplb.h extern QemuIplParameters qipl; · line 37
Quoted 7 lines
pc-bios/s390-ccw/iplb.h +7
-0+uint8_t reserved2[8 ]; +}; +typedef struct IplInfoReportBlockHeader IplInfoReportBlockHeader ; + +struct IplInfoBlockHeader { +uint32_t len; +uint8_t ibt;
s/ibt/type
pc-bios/s390-ccw/iplb.h extern QemuIplParameters qipl; · line 39
Quoted 2 lines
pc-bios/s390-ccw/iplb.h +2
-0+uint8_t reserved1[3 ]; +uint8_t reserved2[8 ];
Make this one reserved[11].
pc-bios/s390-ccw/iplb.h extern QemuIplParameters qipl; · line 46
Quoted 7 lines
pc-bios/s390-ccw/iplb.h +7
-0+}; +typedef struct IplInfoBlockHeader IplInfoBlockHeader ; + +enum IplIbt { +IPL_IBT_CERTIFICATES =1 , +IPL_IBT_COMPONENTS =2 , +};
s/IplIbt/IplInfoBlockType
s/IPL_IBT/IPL_INFO_BLOCK_TYPE
It becomes more verbose, but it's much easier to read.
Quoted 5 lines
pc-bios/s390-ccw/iplb.h +5
-0+ +struct IplSignatureCertificateEntry { +uint64_t addr; +uint64_t len; +};
pc-bios/s390-ccw/iplb.h extern QemuIplParameters qipl; · line 61
Quoted 10 lines
pc-bios/s390-ccw/iplb.h +10
-0+typedef struct IplSignatureCertificateEntry IplSignatureCertificateEntry ; + +struct IplSignatureCertificateList { +IplInfoBlockHeader ipl_info_header; +IplSignatureCertificateEntry cert_entries[MAX_CERTIFICATES ]; +}; +typedef struct IplSignatureCertificateList IplSignatureCertificateList ; + +#define S390_IPL_COMPONENT_FLAG_SC 0x80 +#define S390_IPL_COMPONENT_FLAG_CSV 0x40
Rename these to S390_IPL_DEV_COMP_FLAG_*
Quoted 19 lines
pc-bios/s390-ccw/iplb.h +19
-0+ +struct IplDeviceComponentEntry { +uint64_t addr; +uint64_t len; +uint8_t flags; +uint8_t reserved1[5 ]; +uint16_t cert_index; +uint8_t reserved2[8 ]; +}; +typedef struct IplDeviceComponentEntry IplDeviceComponentEntry ; + +struct IplDeviceComponentList { +IplInfoBlockHeader ipl_info_header; +IplDeviceComponentEntry device_entries[MAX_CERTIFICATES ]; +}; +typedef struct IplDeviceComponentList IplDeviceComponentList ; + +#define COMP_LIST_MAX sizeof(IplDeviceComponentList) +#define CERT_LIST_MAX sizeof(IplSignatureCertificateList)
pc-bios/s390-ccw/iplb.h extern QemuIplParameters qipl; · line 90
Quoted 10 lines
pc-bios/s390-ccw/iplb.h +7
-0+ +struct IplInfoReportBlock { +IplInfoReportBlockHeader hdr; +uint8_t info_blks[COMP_LIST_MAX +CERT_LIST_MAX ]; +}; +typedef struct IplInfoReportBlock IplInfoReportBlock ; +#define S390_IPL_TYPE_FCP 0x00#define S390_IPL_TYPE_CCW 0x02#define S390_IPL_TYPE_QEMU_SCSI 0xff
Other than the nits above, the data structure layouts are correct.
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 13/29] pc-bios/s390-ccw: Introduce IPL Information Report Block (IIRB)
Quoted 31 lines · Collin Walling
Quoted 30 lines · Zhuoying Cai
The IPL information report block (IIRB) contains information used to locate IPL records and to report the results of signature verification of one or more secure components of the load device.
IIRB is stored immediately following the IPL Parameter Block. Results on component verification in any case (failure or success) are stored.
Signed-off-by Zhuoying Cai <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 13 +++++++ pc-bios/s390-ccw/iplb.h | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+)docs/specs/s390x-secure-ipl.rst +9
-0@@ -86,3 +86,16 @@ Subcode 1 - perform signature verification * ``0x0302``: PKCS#7 format signature is invalid * ``0x0402``: signature-verification failed * ``0x0502``: length of Diag508SigVerifBlock is invalid + +IPL Information Report Block +---------------------------- + +The IPL Parameter Block (IPLPB), utilized for IPL operation, is extended with an +IPL Information Report Block (IIRB), which contains the results from secure IPL +operations such as: + +* component data
Quoted 9 lines
docs/specs/s390x-secure-ipl.rst Subcode 1 - perform signature verification · line 101
Quoted 4 lines
docs/specs/s390x-secure-ipl.rst +4
-0+* verification results +* certificate data + +The guest kernel will inspect the IIRB and build the keyring.This needs more elaboration. Is the data listed above used in the keyring? Maybe rewording to "The guest's kernel will use this data in the IIRB when building its keyring." ?
Thanks for pointing this out. The data in the IIRB is not used to build the keyring. I'll update the text as:
"When secure boot is enabled, the guest kernel will inspect the IIRB."
[...]
Re: [PATCH v7 13/29] pc-bios/s390-ccw: Introduce IPL Information Report Block (IIRB)
Quoted 44 lines · Zhuoying Cai
Quoted 31 lines · Collin Walling
Quoted 30 lines · Zhuoying Cai
The IPL information report block (IIRB) contains information used to locate IPL records and to report the results of signature verification of one or more secure components of the load device.
IIRB is stored immediately following the IPL Parameter Block. Results on component verification in any case (failure or success) are stored.
Signed-off-by Zhuoying Cai <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 13 +++++++ pc-bios/s390-ccw/iplb.h | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+)docs/specs/s390x-secure-ipl.rst +9
-0@@ -86,3 +86,16 @@ Subcode 1 - perform signature verification * ``0x0302``: PKCS#7 format signature is invalid * ``0x0402``: signature-verification failed * ``0x0502``: length of Diag508SigVerifBlock is invalid + +IPL Information Report Block +---------------------------- + +The IPL Parameter Block (IPLPB), utilized for IPL operation, is extended with an +IPL Information Report Block (IIRB), which contains the results from secure IPL +operations such as: + +* component dataQuoted 9 lines
docs/specs/s390x-secure-ipl.rst Subcode 1 - perform signature verification · line 101
Quoted 4 lines
docs/specs/s390x-secure-ipl.rst +4
-0+* verification results +* certificate data + +The guest kernel will inspect the IIRB and build the keyring.This needs more elaboration. Is the data listed above used in the keyring? Maybe rewording to "The guest's kernel will use this data in the IIRB when building its keyring." ?
Quoted 7 lines
Thanks for pointing this out. The data in the IIRB is not used to build the keyring. I'll update the text as:
"When secure boot is enabled, the guest kernel will inspect the IIRB."
[...]
Apologies for my previous reply. I reviewed my notes and realized that the guest kernel uses the certificate data in the IIRB to build the keyring. I'll update the documentation accordingly.
PATCH v7 14/29 pc-bios/s390-ccw: Define memory for IPLB and convert IPLB to pointers
In reply to Zhuoying CaiDefine a memory space for both IPL Parameter Block (IPLB) and IPL Information Report Block (IIRB) since IIRB is stored immediately following IPLB.
Convert IPLB to pointer and it points to the start of the defined memory space. IIRB points to the end of IPLB.
--- pc-bios/s390-ccw/iplb.h | 13 +++++++++++-- pc-bios/s390-ccw/jump2ipl.c | 6 +++--- pc-bios/s390-ccw/main.c | 34 +++++++++++++++++++--------------- pc-bios/s390-ccw/netmain.c | 8 ++++---- 4 files changed, 37 insertions(+), 24 deletions(-)
pc-bios/s390-ccw/iplb.h +11 -2
@@ -20,7 +20,7 @@#include <string.h> extern QemuIplParameters qipl; -extern IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE))); +extern IplParameterBlock *iplb;extern bool have_iplb;struct IplInfoReportBlockHeader {
@@ -85,6 +85,15 @@ struct IplInfoReportBlock { };typedef struct IplInfoReportBlock IplInfoReportBlock ; +struct IplBlocks { +IplParameterBlock iplb; +IplInfoReportBlock iirb; +}; +typedef struct IplBlocks IplBlocks ; + +/* extern only allowed in header file */ +extern IplBlocks ipl_data __attribute__((__aligned__(PAGE_SIZE))); +#define S390_IPL_TYPE_FCP 0x00#define S390_IPL_TYPE_CCW 0x02#define S390_IPL_TYPE_QEMU_SCSI 0xff
@@ -127,7 +136,7 @@ static inline bool load_next_iplb(void) qipl.index++; next_iplb = (IplParameterBlock *) qipl.next_iplb; -memcpy (&iplb, next_iplb,sizeof (IplParameterBlock)); +memcpy (iplb, next_iplb,sizeof (IplParameterBlock)); qipl.chain_len--; qipl.next_iplb = qipl.next_iplb +sizeof (IplParameterBlock);
pc-bios/s390-ccw/jump2ipl.c +3 -3
@@ -43,11 +43,11 @@ int jump_to_IPL_code(uint64_t address)* The IPLB for QEMU SCSI type devices must be rebuilt during re-ipl. The * iplb.devno is set to the boot position of the target SCSI device. */ -if (iplb.pbt ==S390_IPL_TYPE_QEMU_SCSI ) { - iplb.devno = qipl.index; +if (iplb->pbt ==S390_IPL_TYPE_QEMU_SCSI ) { + iplb->devno = qipl.index; } -if (have_iplb && !set_iplb (&iplb)) { +if (have_iplb && !set_iplb (iplb)) {panic ("Failed to set IPLB" ); }
pc-bios/s390-ccw/main.c +19 -15
@@ -22,7 +22,9 @@static SubChannelId blk_schid = { .one =1 };static char loadparm_str[LOADPARM_LEN +1 ];QemuIplParameters qipl; -IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE))); +/* Ensure that IPLB and IIRB are page aligned and sequential in memory */ +IplBlocks ipl_data; +IplParameterBlock *iplb;bool have_iplb;static uint16_t cutype;LowCore *lowcore;/* Yes, this *is* a pointer to address 0 */
@@ -51,7 +53,7 @@ void write_subsystem_identification(void)void write_iplb_location (void ) {if (cutype ==CU_TYPE_VIRTIO &&virtio_get_device_type () !=VIRTIO_ID_NET ) { - lowcore->ptr_iplb =ptr2u32 (&iplb); + lowcore->ptr_iplb =ptr2u32 (iplb); } }
@@ -162,7 +164,7 @@ static void menu_setup(void)return ; } -switch (iplb.pbt) { +switch (iplb->pbt) {case S390_IPL_TYPE_CCW :case S390_IPL_TYPE_QEMU_SCSI : menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK,
@@ -191,8 +193,8 @@ static void boot_setup(void) {char lpmsg[] ="LOADPARM=[________]\n" ; -if (have_iplb &&memcmp (iplb.loadparm,NO_LOADPARM ,LOADPARM_LEN ) !=0 ) { -ebcdic_to_ascii ((char *) iplb.loadparm, loadparm_str,LOADPARM_LEN ); +if (have_iplb &&memcmp (iplb->loadparm,NO_LOADPARM ,LOADPARM_LEN ) !=0 ) { +ebcdic_to_ascii ((char *) iplb->loadparm, loadparm_str,LOADPARM_LEN ); }else {sclp_get_loadparm_ascii (loadparm_str); }
@@ -216,21 +218,21 @@ static bool find_boot_device(void)VDev *vdev =virtio_get_device ();bool found = false; -switch (iplb.pbt) { +switch (iplb->pbt) {case S390_IPL_TYPE_CCW : vdev->scsi_device_selected = false; -debug_print_int ("device no. " , iplb.ccw.devno); - blk_schid.ssid = iplb.ccw.ssid &0x3 ; +debug_print_int ("device no. " , iplb->ccw.devno); + blk_schid.ssid = iplb->ccw.ssid &0x3 ;debug_print_int ("ssid " , blk_schid.ssid); - found =find_subch (iplb.ccw.devno); + found =find_subch (iplb->ccw.devno);break ;case S390_IPL_TYPE_QEMU_SCSI : vdev->scsi_device_selected = true; - vdev->selected_scsi_device.channel = iplb.scsi.channel; - vdev->selected_scsi_device.target = iplb.scsi.target; - vdev->selected_scsi_device.lun = iplb.scsi.lun; - blk_schid.ssid = iplb.scsi.ssid &0x3 ; - found =find_subch (iplb.scsi.devno); + vdev->selected_scsi_device.channel = iplb->scsi.channel; + vdev->selected_scsi_device.target = iplb->scsi.target; + vdev->selected_scsi_device.lun = iplb->scsi.lun; + blk_schid.ssid = iplb->scsi.ssid &0x3 ; + found =find_subch (iplb->scsi.devno);break ;default :puts ("Unsupported IPLB" );
@@ -311,10 +313,12 @@ static void probe_boot_device(void)void main (void ) { + iplb = &ipl_data.iplb; +copy_qipl ();sclp_setup ();css_setup (); - have_iplb =store_iplb (&iplb); + have_iplb =store_iplb (iplb);if (!have_iplb) {boot_setup ();probe_boot_device ();
pc-bios/s390-ccw/netmain.c +4 -4
@@ -528,11 +528,11 @@ static bool virtio_setup(void)*/ enable_mss_facility (); -if (have_iplb ||store_iplb (&iplb)) { -IPL_assert (iplb.pbt ==S390_IPL_TYPE_CCW ,"IPL_TYPE_CCW expected" ); - dev_no = iplb.ccw.devno; +if (have_iplb ||store_iplb (iplb)) { +IPL_assert (iplb->pbt ==S390_IPL_TYPE_CCW ,"IPL_TYPE_CCW expected" ); + dev_no = iplb->ccw.devno;debug_print_int ("device no. " , dev_no); - net_schid.ssid = iplb.ccw.ssid &0x3 ; + net_schid.ssid = iplb->ccw.ssid &0x3 ;debug_print_int ("ssid " , net_schid.ssid); found =find_net_dev (&schib, dev_no); }else {
Signature · 1 line
2.51.1
Re: [PATCH v7 14/29] pc-bios/s390-ccw: Define memory for IPLB and convert IPLB to pointers
Quoted 30 lines · Zhuoying Cai
Define a memory space for both IPL Parameter Block (IPLB) and IPL Information Report Block (IIRB) since IIRB is stored immediately following IPLB.
Convert IPLB to pointer and it points to the start of the defined memory space. IIRB points to the end of IPLB.
Signed-off-by Zhuoying Cai <[email protected]>--- pc-bios/s390-ccw/iplb.h | 13 +++++++++++-- pc-bios/s390-ccw/jump2ipl.c | 6 +++--- pc-bios/s390-ccw/main.c | 34 +++++++++++++++++++--------------- pc-bios/s390-ccw/netmain.c | 8 ++++---- 4 files changed, 37 insertions(+), 24 deletions(-)pc-bios/s390-ccw/iplb.h +1
-1@@ -20,7 +20,7 @@#include <string.h> extern QemuIplParameters qipl; -extern IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE))); +extern IplParameterBlock *iplb;extern bool have_iplb;struct IplInfoReportBlockHeader {@@ -85,6 +85,15 @@ struct IplInfoReportBlock { };
pc-bios/s390-ccw/iplb.h struct IplInfoReportBlock { · line 95
Quoted 10 lines
pc-bios/s390-ccw/iplb.h +8
-0typedef struct IplInfoReportBlock IplInfoReportBlock ; +struct IplBlocks { +IplParameterBlock iplb; +IplInfoReportBlock iirb; +}; +typedef struct IplBlocks IplBlocks ; + +/* extern only allowed in header file */ +extern IplBlocks ipl_data __attribute__((__aligned__(PAGE_SIZE)));
Not sure ... does that __attribute__ have any effects for "extern" declarations?
pc-bios/s390-ccw/main.c line 26
Quoted 11 lines
pc-bios/s390-ccw/main.c +2
-1@@ -22,7 +22,9 @@static SubChannelId blk_schid = { .one =1 };static char loadparm_str[LOADPARM_LEN +1 ];QemuIplParameters qipl; -IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE))); +/* Ensure that IPLB and IIRB are page aligned and sequential in memory */ +IplBlocks ipl_data;
I think you definitely want to have an __attribute__((__aligned__(PAGE_SIZE))) after the ipl_data declaration here!
Thomas
Re: [PATCH v7 14/29] pc-bios/s390-ccw: Define memory for IPLB and convert IPLB to pointers
Quoted 33 lines · Thomas Huth
Quoted 32 lines · Zhuoying Cai
Define a memory space for both IPL Parameter Block (IPLB) and IPL Information Report Block (IIRB) since IIRB is stored immediately following IPLB.
Convert IPLB to pointer and it points to the start of the defined memory space. IIRB points to the end of IPLB.
Signed-off-by Zhuoying Cai <[email protected]>--- pc-bios/s390-ccw/iplb.h | 13 +++++++++++-- pc-bios/s390-ccw/jump2ipl.c | 6 +++--- pc-bios/s390-ccw/main.c | 34 +++++++++++++++++++--------------- pc-bios/s390-ccw/netmain.c | 8 ++++---- 4 files changed, 37 insertions(+), 24 deletions(-)pc-bios/s390-ccw/iplb.h +1
-1@@ -20,7 +20,7 @@#include <string.h> extern QemuIplParameters qipl; -extern IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE))); +extern IplParameterBlock *iplb;extern bool have_iplb;struct IplInfoReportBlockHeader {@@ -85,6 +85,15 @@ struct IplInfoReportBlock { };typedef struct IplInfoReportBlock IplInfoReportBlock ;
Quoted 11 lines
pc-bios/s390-ccw/iplb.h struct IplInfoReportBlock { · line 95
Quoted 8 lines
pc-bios/s390-ccw/iplb.h +8
-0+struct IplBlocks { +IplParameterBlock iplb; +IplInfoReportBlock iirb; +}; +typedef struct IplBlocks IplBlocks ; + +/* extern only allowed in header file */ +extern IplBlocks ipl_data __attribute__((__aligned__(PAGE_SIZE)));Not sure ... does that
__attribute__have any effects for "extern" declarations?
Thanks for pointing this out. The __attribute__ on an extern declaration doesn't have any effect. I'll move the __attribute__ on the definition and drop it from the extern declaration.
Quoted 17 lines
pc-bios/s390-ccw/main.c line 26
Quoted 11 lines
pc-bios/s390-ccw/main.c +2
-1@@ -22,7 +22,9 @@static SubChannelId blk_schid = { .one =1 };static char loadparm_str[LOADPARM_LEN +1 ];QemuIplParameters qipl; -IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE))); +/* Ensure that IPLB and IIRB are page aligned and sequential in memory */ +IplBlocks ipl_data;I think you definitely want to have an __attribute__((__aligned__(PAGE_SIZE))) after the
ipl_datadeclaration here!Thomas
PATCH v7 15/29 hw/s390x/ipl: Add IPIB flags to IPL Parameter Block
In reply to Zhuoying CaiAdd IPIB flags to IPL Parameter Block to determine if IPL needs to perform securely and if IPL Information Report Block (IIRB) exists.
Move DIAG308 flags to a separated header file and add flags for secure IPL.
Secure boot in audit mode will perform if certificate(s) exist in the key store. IIRB will exist and results of verification will be stored in IIRB.
To ensure proper alignment of the IIRB and prevent overlap, set iplb->len to the maximum length of the IPLB, allowing alignment constraints to be determined based on its size.
--- hw/s390x/ipl.c | 14 ++++++++++++++ hw/s390x/ipl.h | 18 +----------------- include/hw/s390x/ipl/diag308.h | 34 ++++++++++++++++++++++++++++++++++ include/hw/s390x/ipl/qipl.h | 5 ++++- 4 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 include/hw/s390x/ipl/diag308.h
hw/s390x/ipl.c +14 -0
@@ -496,6 +496,20 @@ static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)s390_ipl_convert_loadparm ((char *)lp, iplb->loadparm); iplb->flags |=DIAG308_FLAGS_LP_VALID ; +/* +* Secure boot in audit mode will perform +* if certificate(s) exist in the key store. +* +* IPL Information Report Block (IIRB) will exist +* for secure boot in audit mode. +* +* Results of secure boot will be stored in IIRB. +*/ +if (s390_has_certificate ()) { + iplb->hdr_flags |=DIAG308_IPIB_FLAGS_IPLIR ; + iplb->len =cpu_to_be32 (S390_IPLB_MAX_LEN ); + } +return true; }
hw/s390x/ipl.h +1 -17
@@ -23,7 +23,6 @@#include "qom/object.h" #include "target/s390x/kvm/pv.h" -#define DIAG308_FLAGS_LP_VALID 0x80#define MAX_BOOT_DEVS 8/* Max number of devices that may have a bootindex */ void s390_ipl_convert_loadparm (char *ascii_lp,uint8_t *ebcdic_lp);
@@ -91,22 +90,6 @@ struct S390IPLState { };QEMU_BUILD_BUG_MSG (offsetof(S390IPLState , iplb) &3 ,"alignment of iplb wrong" ); -#define DIAG_308_RC_OK 0x0001 -#define DIAG_308_RC_NO_CONF 0x0102 -#define DIAG_308_RC_INVALID 0x0402 -#define DIAG_308_RC_NO_PV_CONF 0x0902 -#define DIAG_308_RC_INVAL_FOR_PV 0x0a02 - -#define DIAG308_RESET_MOD_CLR 0 -#define DIAG308_RESET_LOAD_NORM 1 -#define DIAG308_LOAD_CLEAR 3 -#define DIAG308_LOAD_NORMAL_DUMP 4 -#define DIAG308_SET 5 -#define DIAG308_STORE 6 -#define DIAG308_PV_SET 8 -#define DIAG308_PV_STORE 9 -#define DIAG308_PV_START 10 -#define S390_IPL_TYPE_FCP 0x00#define S390_IPL_TYPE_CCW 0x02#define S390_IPL_TYPE_PV 0x05
@@ -117,6 +100,7 @@ QEMU_BUILD_BUG_MSG(offsetof(S390IPLState, iplb) & 3, "alignment of iplb wrong");#define S390_IPLB_MIN_CCW_LEN 200#define S390_IPLB_MIN_FCP_LEN 384#define S390_IPLB_MIN_QEMU_SCSI_LEN 200 +#define S390_IPLB_MAX_LEN 4096static inline bool iplb_valid_len (IplParameterBlock *iplb) {
include/hw/s390x/ipl/diag308.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* S/390 DIAGNOSE 308 definitions and structures +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef S390X_DIAG308_H +#define S390X_DIAG308_H + +#define DIAG_308_RC_OK 0x0001 +#define DIAG_308_RC_NO_CONF 0x0102 +#define DIAG_308_RC_INVALID 0x0402 +#define DIAG_308_RC_NO_PV_CONF 0x0902 +#define DIAG_308_RC_INVAL_FOR_PV 0x0a02 + +#define DIAG308_RESET_MOD_CLR 0 +#define DIAG308_RESET_LOAD_NORM 1 +#define DIAG308_LOAD_CLEAR 3 +#define DIAG308_LOAD_NORMAL_DUMP 4 +#define DIAG308_SET 5 +#define DIAG308_STORE 6 +#define DIAG308_PV_SET 8 +#define DIAG308_PV_STORE 9 +#define DIAG308_PV_START 10 + +#define DIAG308_FLAGS_LP_VALID 0x80 + +#define DIAG308_IPIB_FLAGS_SIPL 0x40 +#define DIAG308_IPIB_FLAGS_IPLIR 0x20 + +#endif
include/hw/s390x/ipl/qipl.h +4 -1
@@ -12,6 +12,8 @@#ifndef S390X_QIPL_H #define S390X_QIPL_H +#include "diag308.h" +/* Boot Menu flags */ #define QIPL_FLAG_BM_OPTS_CMD 0x80#define QIPL_FLAG_BM_OPTS_ZIPL 0x40
@@ -103,7 +105,8 @@ typedef struct IplBlockQemuScsi IplBlockQemuScsi;union IplParameterBlock {struct {uint32_t len; -uint8_t reserved0[3 ]; +uint8_t hdr_flags; +uint8_t reserved0[2 ];uint8_t version;uint32_t blk0_len;uint8_t pbt;
Signature · 1 line
2.51.1
Re: [PATCH v7 15/29] hw/s390x/ipl: Add IPIB flags to IPL Parameter Block
Quoted 9 lines · Zhuoying Cai
Add IPIB flags to IPL Parameter Block to determine if IPL needs to perform securely and if IPL Information Report Block (IIRB) exists.
Move DIAG308 flags to a separated header file and add flags for secure IPL.
Secure boot in audit mode will perform if certificate(s) exist in the key store. IIRB will exist and results of verification will be stored in IIRB.
Quoted 6 lines
To ensure proper alignment of the IIRB and prevent overlap, set
iplb->lento the maximum length of the IPLB, allowing alignment constraints to be determined based on its size.Signed-off-by Zhuoying Cai <[email protected]>---
PATCH v7 16/29 s390x: Guest support for Secure-IPL Facility
In reply to Zhuoying CaiIntroduce Secure-IPL (SIPL) facility.
Use fac_ipl to represent bytes 136 and 137 for IPL device facilities of the SCLP Read Info block.
Availability of SIPL facility is determined by byte 136 bit 1 of the SCLP Read Info block. Byte 136's facilities cannot be represented without the availability of the extended-length-SCCB, so add it as a check for consistency.
Secure IPL is not available for guests under protected virtualization.
This feature is available starting with the gen16 CPU model.
--- hw/s390x/sclp.c | 2 ++ include/hw/s390x/sclp.h | 4 +++- target/s390x/cpu_features.c | 4 ++++ target/s390x/cpu_features.h | 1 + target/s390x/cpu_features_def.h.inc | 3 +++ target/s390x/cpu_models.c | 2 ++ target/s390x/gen-features.c | 2 ++ target/s390x/kvm/kvm.c | 3 +++ 8 files changed, 20 insertions(+), 1 deletion(-)
hw/s390x/sclp.c +2 -0
@@ -146,6 +146,8 @@ static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb)if (s390_has_feat (S390_FEAT_EXTENDED_LENGTH_SCCB )) {s390_get_feat_block (S390_FEAT_TYPE_SCLP_FAC134 , &read_info->fac134); +s390_get_feat_block (S390_FEAT_TYPE_SCLP_FAC_IPL , + read_info->fac_ipl); } read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO |
include/hw/s390x/sclp.h +3 -1
@@ -136,7 +136,9 @@ typedef struct ReadInfo {uint32_t hmfai;uint8_t _reserved7[134 -128 ];/* 128-133 */ uint8_t fac134; -uint8_t _reserved8[144 -135 ];/* 135-143 */ +uint8_t _reserved8; +uint8_t fac_ipl[2 ];/* 136-137 */ +uint8_t _reserved9[144 -137 ];/* 138-143 */ struct CPUEntry entries[];/* * When the Extended-Length SCCB (ELS) feature is enabled the
target/s390x/cpu_features.c +4 -0
@@ -119,6 +119,7 @@ void s390_fill_feat_block(const S390FeatBitmap features, S390FeatType type,* Some facilities are not available for CPUs in protected mode: * - All SIE facilities because SIE is not available * - DIAG318 +* - Secure IPL Facility * * As VMs can move in and out of protected mode the CPU model * doesn't protect us from that problem because it is only
@@ -149,6 +150,9 @@ void s390_fill_feat_block(const S390FeatBitmap features, S390FeatType type,clear_be_bit (s390_feat_def (S390_FEAT_DIAG_318 )->bit, data);clear_be_bit (s390_feat_def (S390_FEAT_CERT_STORE )->bit, data);break ; +case S390_FEAT_TYPE_SCLP_FAC_IPL : +clear_be_bit (s390_feat_def (S390_FEAT_SIPL )->bit, data); +break ;default :return ; }
target/s390x/cpu_features.h +1 -0
@@ -24,6 +24,7 @@ typedef enum {S390_FEAT_TYPE_SCLP_CONF_CHAR ,S390_FEAT_TYPE_SCLP_CONF_CHAR_EXT ,S390_FEAT_TYPE_SCLP_FAC134 , +S390_FEAT_TYPE_SCLP_FAC_IPL ,S390_FEAT_TYPE_SCLP_CPU ,S390_FEAT_TYPE_MISC , S390_FEAT_TYPE_PLO,
target/s390x/cpu_features_def.h.inc +3 -0
@@ -140,6 +140,9 @@ DEF_FEAT(SIE_IBS, "ibs", SCLP_CONF_CHAR_EXT, 10, "SIE: Interlock-and-broadcast-s DEF_FEAT(DIAG_318, "diag318", SCLP_FAC134, 0, "Control program name and version codes") DEF_FEAT(CERT_STORE, "cstore", SCLP_FAC134, 5, "Provide Certificate Store functions") +/* Features exposed via SCLP SCCB Facilities byte 136 - 137 (bit numbers relative to byte-136) */ +DEF_FEAT(SIPL, "sipl", SCLP_FAC_IPL, 1, "Secure-IPL facility") + /* Features exposed via SCLP CPU info. */ DEF_FEAT(SIE_F2, "sief2", SCLP_CPU, 4, "SIE: interception format 2 (Virtual SIE)") DEF_FEAT(SIE_SKEY, "skey", SCLP_CPU, 5, "SIE: Storage-key facility")
target/s390x/cpu_models.c +2 -0
@@ -263,6 +263,7 @@ bool s390_has_feat(S390Feat feat)case S390_FEAT_SIE_CMMA :case S390_FEAT_SIE_PFMFI :case S390_FEAT_SIE_IBS : +case S390_FEAT_SIPL :case S390_FEAT_CONFIGURATION_TOPOLOGY :return false;break ;
@@ -507,6 +508,7 @@ static void check_consistency(const S390CPUModel *model) { S390_FEAT_AP_QUEUE_INTERRUPT_CONTROL, S390_FEAT_AP }, { S390_FEAT_DIAG_318, S390_FEAT_EXTENDED_LENGTH_SCCB }, { S390_FEAT_CERT_STORE, S390_FEAT_EXTENDED_LENGTH_SCCB }, + { S390_FEAT_SIPL, S390_FEAT_EXTENDED_LENGTH_SCCB }, { S390_FEAT_NNPA, S390_FEAT_VECTOR }, { S390_FEAT_RDP, S390_FEAT_LOCAL_TLB_CLEARING }, { S390_FEAT_UV_FEAT_AP, S390_FEAT_AP },
target/s390x/gen-features.c +2 -0
@@ -721,6 +721,7 @@ static uint16_t full_GEN16_GA1[] = {S390_FEAT_UV_FEAT_AP ,S390_FEAT_UV_FEAT_AP_INTR ,S390_FEAT_CERT_STORE , + S390_FEAT_SIPL, }; static uint16_t full_GEN17_GA1[] = {
@@ -922,6 +923,7 @@ static uint16_t qemu_MAX[] = {S390_FEAT_PRNO_TRNG ,S390_FEAT_EXTENDED_LENGTH_SCCB ,S390_FEAT_CERT_STORE , + S390_FEAT_SIPL, };/****** END FEATURE DEFS ******/
target/s390x/kvm/kvm.c +3 -0
@@ -2520,6 +2520,9 @@ bool kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)set_bit (S390_FEAT_CERT_STORE , model->features); +/* Some Secure IPL facilities are emulated by QEMU */ +set_bit (S390_FEAT_SIPL , model->features); +/* Test for Ultravisor features that influence secure guest behavior */ query_uv_feat_guest (model->features);
Signature · 1 line
2.51.1
Re: [PATCH v7 16/29] s390x: Guest support for Secure-IPL Facility
Quoted 17 lines · Zhuoying Cai
Introduce Secure-IPL (SIPL) facility.
Use
fac_iplto represent bytes 136 and 137 for IPL device facilities of the SCLP Read Info block.Availability of SIPL facility is determined by byte 136 bit 1 of the SCLP Read Info block. Byte 136's facilities cannot be represented without the availability of the extended-length-SCCB, so add it as a check for consistency.
Secure IPL is not available for guests under protected virtualization.
This feature is available starting with the gen16 CPU model.
Signed-off-by Zhuoying Cai <[email protected]>Reviewed-by Collin Walling <[email protected]>---
...
include/hw/s390x/sclp.h typedef struct ReadInfo { · line 141
Quoted 12 lines
include/hw/s390x/sclp.h +3
-1@@ -136,7 +136,9 @@ typedef struct ReadInfo {uint32_t hmfai;uint8_t _reserved7[134 -128 ];/* 128-133 */ uint8_t fac134; -uint8_t _reserved8[144 -135 ];/* 135-143 */ +uint8_t _reserved8; +uint8_t fac_ipl[2 ];/* 136-137 */ +uint8_t _reserved9[144 -137 ];/* 138-143 */
Should that be "[144 - 138]" instead?
Thomas
PATCH v7 17/29 pc-bios/s390-ccw: Refactor zipl_run()
In reply to Zhuoying CaiRefactor to enhance readability before enabling secure IPL in later patches.
--- pc-bios/s390-ccw/bootmap.c | 51 ++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 18 deletions(-)
pc-bios/s390-ccw/bootmap.c +33 -18
@@ -674,12 +674,42 @@ static int zipl_load_segment(ComponentEntry *entry)return 0 ; } +static int zipl_run_normal (ComponentEntry **entry_ptr,uint8_t *tmp_sec) +{ +ComponentEntry *entry = *entry_ptr; + +while (entry->component_type ==ZIPL_COMP_ENTRY_LOAD || + entry->component_type ==ZIPL_COMP_ENTRY_SIGNATURE ) { + +/* Secure boot is off, so we skip signature entries */ +if (entry->component_type ==ZIPL_COMP_ENTRY_SIGNATURE ) { + entry++; +continue ; + } + +if (zipl_load_segment (entry)) { +return -1 ; + } + + entry++; + +if ((uint8_t *)&entry[1 ] > tmp_sec +MAX_SECTOR_SIZE ) { +puts ("Wrong entry value" ); +return -EINVAL ; + } + } + + *entry_ptr = entry; +return 0 ; +} +/* Run a zipl program */ static int zipl_run (ScsiBlockPtr *pte) {ComponentHeader *header;ComponentEntry *entry;uint8_t tmp_sec[MAX_SECTOR_SIZE ]; +int rc;if (virtio_read (pte->blockno, tmp_sec)) {puts ("Cannot read header" );
@@ -700,25 +730,10 @@ static int zipl_run(ScsiBlockPtr *pte)/* Load image(s) into RAM */ entry = (ComponentEntry *)(&header[1 ]); -while (entry->component_type ==ZIPL_COMP_ENTRY_LOAD || - entry->component_type ==ZIPL_COMP_ENTRY_SIGNATURE ) { - -/* We don't support secure boot yet, so we skip signature entries */ -if (entry->component_type ==ZIPL_COMP_ENTRY_SIGNATURE ) { - entry++; -continue ; - } - -if (zipl_load_segment (entry)) { -return -1 ; - } - entry++; - -if ((uint8_t *)(&entry[1 ]) > (tmp_sec +MAX_SECTOR_SIZE )) { -puts ("Wrong entry value" ); -return -EINVAL ; - } + rc =zipl_run_normal (&entry, tmp_sec); +if (rc) { +return rc; }if (entry->component_type !=ZIPL_COMP_ENTRY_EXEC ) {
Signature · 1 line
2.51.1
Re: [PATCH v7 17/29] pc-bios/s390-ccw: Refactor zipl_run()
Quoted 7 lines · Zhuoying Cai
Refactor to enhance readability before enabling secure IPL in later patches.
Signed-off-by Zhuoying Cai <[email protected]>--- pc-bios/s390-ccw/bootmap.c | 51 ++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 18 deletions(-)
PATCH v7 18/29 pc-bios/s390-ccw: Rework zipl_load_segment function
In reply to Zhuoying CaiMake the address variable a parameter of zipl_load_segment and return segment length.
Modify this function to allow the caller to specify a memory address where segment data should be loaded into.
seg_len variable is necessary to store the calculated segment length and is used during signature verification. Return the length on success, or a negative return code on failure.
--- pc-bios/s390-ccw/bootmap.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
pc-bios/s390-ccw/bootmap.c +10 -5
@@ -613,19 +613,22 @@ static int ipl_eckd(void)* IPL a SCSI disk */ -static int zipl_load_segment (ComponentEntry *entry) +/* +* Returns: length of the segment on success, +* negative value on error. +*/ +static int zipl_load_segment (ComponentEntry *entry,uint64_t address) {const int max_entries = (MAX_SECTOR_SIZE /sizeof (ScsiBlockPtr));ScsiBlockPtr *bprs = (void *)sec;const int bprs_size =sizeof (sec);block_number_t blockno; -uint64_t address;int i;char err_msg[] ="zIPL failed to read BPRS at 0xZZZZZZZZZZZZZZZZ" ;char *blk_no = &err_msg[30 ];/* where to print blockno in (those ZZs) */ +int seg_len =0 ; blockno = entry->data.blockno; - address = entry->compdat.load_addr;debug_print_int ("loading segment at block" , blockno);debug_print_int ("addr" , address);
@@ -668,10 +671,12 @@ static int zipl_load_segment(ComponentEntry *entry)puts ("zIPL load segment failed" );return -EIO ; } + + seg_len += bprs->size * (bprs[i].blockct +1 ); } }while (blockno); -return 0 ; +return seg_len; }static int zipl_run_normal (ComponentEntry **entry_ptr,uint8_t *tmp_sec)
@@ -687,7 +692,7 @@ static int zipl_run_normal(ComponentEntry **entry_ptr, uint8_t *tmp_sec)continue ; } -if (zipl_load_segment (entry)) { +if (zipl_load_segment (entry, entry->compdat.load_addr) <0 ) {return -1 ; }
Signature · 1 line
2.51.1
PATCH v7 19/29 pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
In reply to Zhuoying CaiEnable 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.
Add a comp_len variable to store the length of a segment in zipl_load_segment. comp_len variable is necessary to store the calculated segment length and is used during signature verification. Return the length on success, or a negative return code on failure.
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.
--- docs/system/s390x/secure-ipl.rst | 34 +++ pc-bios/s390-ccw/Makefile | 3 +- pc-bios/s390-ccw/bootmap.c | 44 +++- pc-bios/s390-ccw/bootmap.h | 11 + pc-bios/s390-ccw/main.c | 9 + pc-bios/s390-ccw/s390-ccw.h | 15 ++ pc-bios/s390-ccw/sclp.c | 44 ++++ pc-bios/s390-ccw/sclp.h | 6 + pc-bios/s390-ccw/secure-ipl.c | 383 +++++++++++++++++++++++++++++++ pc-bios/s390-ccw/secure-ipl.h | 94 ++++++++ 10 files changed, 638 insertions(+), 5 deletions(-) create mode 100644 pc-bios/s390-ccw/secure-ipl.c create mode 100644 pc-bios/s390-ccw/secure-ipl.h
docs/system/s390x/secure-ipl.rst +34 -0
@@ -18,3 +18,37 @@ Note: certificate files must have a .pem extension. .. code-block:: shell qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ... + + +IPL Modes +========= +Multiple IPL modes are available to differentiate between the various IPL +configurations. These modes are mutually exclusive and enabled based on the +``boot-certs`` option on the QEMU command line. + +Normal Mode +----------- + +The absence of certificates will attempt to IPL a guest without secure IPL +operations. No checks are performed, and no warnings/errors are reported. +This is the default mode. + +Configuration: + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio ... + +Audit Mode +---------- + +With *only* the presence of certificates in the store, it is assumed that secure +boot operations should be performed with errors reported as warnings. As such, +the secure IPL operations will be performed, and any errors that stem from these +operations will result in a warning. + +Configuration: + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ...
pc-bios/s390-ccw/Makefile +2 -1
@@ -34,7 +34,8 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d .PHONY : all clean build-all distclean OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \ - virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o + virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \ + secure-ipl.o SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
pc-bios/s390-ccw/bootmap.c +40 -4
85 lines not on this page · raw message
pc-bios/s390-ccw/bootmap.h +11 -0
@@ -88,9 +88,18 @@ typedef struct BootMapTable {BootMapPointer entry[]; } __attribute__ ((packed)) BootMapTable; +#define DER_SIGNATURE_FORMAT 1 + +typedef struct SignatureInformation { +uint8_t format; +uint8_t reserved[3 ]; +uint32_t sig_len; +}SignatureInformation ; +typedef union ComponentEntryData {uint64_t load_psw;uint64_t load_addr; +SignatureInformation sig_info; }ComponentEntryData ; typedef struct ComponentEntry {
@@ -113,6 +122,8 @@ typedef struct ScsiMbr {ScsiBlockPtr pt;/* block pointer to program table */ } __attribute__ ((packed)) ScsiMbr; +int zipl_load_segment (ComponentEntry *entry,uint64_t address); +#define ZIPL_MAGIC "zIPL"#define ZIPL_MAGIC_EBCDIC "\xa9\xc9\xd7\xd3"#define IPL1_MAGIC "\xc9\xd7\xd3\xf1"/* == "IPL1" in EBCDIC */
pc-bios/s390-ccw/main.c +9 -0
@@ -28,6 +28,7 @@ IplParameterBlock *iplb;bool have_iplb;static uint16_t cutype;LowCore *lowcore;/* Yes, this *is* a pointer to address 0 */ +ZiplBootMode boot_mode;#define LOADPARM_PROMPT "PROMPT "#define LOADPARM_EMPTY " "
@@ -272,9 +273,17 @@ static int virtio_setup(void)static void ipl_boot_device (void ) { +if (boot_mode ==ZIPL_BOOT_MODE_UNSPECIFIED ) { + boot_mode =zipl_mode (iplb->hdr_flags); + } +switch (cutype) {case CU_TYPE_DASD_3990 :case CU_TYPE_DASD_2107 : +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +panic ("Passthrough (vfio) CCW device does not support secure boot!" ); + } +dasd_ipl (blk_schid, cutype);break ;case CU_TYPE_VIRTIO :
pc-bios/s390-ccw/s390-ccw.h +15 -0
@@ -39,6 +39,9 @@ typedef unsigned long long u64;#define MIN_NON_ZERO (a, b) ((a) == 0 ? (b) : \ ((b) == 0 ? (a) : (MIN(a, b))))#endif +#ifndef ROUND_UP +#define ROUND_UP (n, d) (((n) + (d) - 1) & -(0 ? (n) : (d))) +#endif #define ARRAY_SIZE (a) (sizeof(a) / sizeof((a)[0]))
@@ -64,6 +67,8 @@ void sclp_print(const char *string);void sclp_set_write_mask (uint32_t receive_mask,uint32_t send_mask);void sclp_setup (void );void sclp_get_loadparm_ascii (char *loadparm); +bool sclp_is_diag320_on (void ); +bool sclp_is_sipl_on (void );int sclp_read (char *str,size_t count);/* virtio.c */
@@ -76,6 +81,16 @@ int virtio_read(unsigned long sector, void *load_addr);/* bootmap.c */ void zipl_load (void ); +typedef enum ZiplBootMode { +ZIPL_BOOT_MODE_UNSPECIFIED =0 , +ZIPL_BOOT_MODE_NORMAL =1 , +ZIPL_BOOT_MODE_SECURE_AUDIT =2 , +}ZiplBootMode ; + +extern ZiplBootMode boot_mode; + +ZiplBootMode zipl_mode (uint8_t hdr_flags); +/* jump2ipl.c */ void write_reset_psw (uint64_t psw);int jump_to_IPL_code (uint64_t address);
pc-bios/s390-ccw/sclp.c +44 -0
50 lines not on this page · raw message
pc-bios/s390-ccw/sclp.h +6 -0
@@ -50,6 +50,8 @@ typedef struct SCCBHeader { } __attribute__((packed)) SCCBHeader;#define SCCB_DATA_LEN (SCCB_SIZE - sizeof(SCCBHeader)) +#define SCCB_FAC134_DIAG320_BIT 0x4 +#define SCCB_FAC_IPL_SIPL_BIT 0x4000 typedef struct ReadInfo { SCCBHeader h;
@@ -57,6 +59,10 @@ typedef struct ReadInfo {uint8_t rnsize;uint8_t reserved[13 ];uint8_t loadparm[LOADPARM_LEN ]; +uint8_t reserved1[102 ]; +uint8_t fac134; +uint8_t reserved2; +uint16_t fac_ipl; } __attribute__((packed)) ReadInfo; typedef struct SCCB {
pc-bios/s390-ccw/secure-ipl.c new file +383 -0
383 lines not on this page · raw message
pc-bios/s390-ccw/secure-ipl.h new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v7 19/29] pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
Quoted 18 lines · Zhuoying Cai
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.
Add a
comp_lenvariable to store the length of a segment inzipl_load_segment.comp_lenvariable is necessary to store the calculated segment length and is used during signature verification. Return the length on success, or a negative return code on failure.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 <[email protected]>---
...
pc-bios/s390-ccw/secure-ipl.c line 122
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c new file +5
-0+static void cert_list_add (IplSignatureCertificateList *certs,int cert_index, +uint8_t *cert,uint64_t cert_len) +{ +if (cert_index >MAX_CERTIFICATES -1 ) { + printf("Warning: Ignoring cert entry [%d] because it's over %d entires\n",
Typo: entires
... but maybe rather change the sentence around it, too:
Ignoring cert entry #%d because only %d entries are supported
Or something similar?
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c new file +5
-0+ cert_index + 1, MAX_CERTIFICATES); +return ; + } + + certs->cert_entries[cert_index].addr = (uint64_t )cert;
pc-bios/s390-ccw/secure-ipl.c line 137
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+ certs->cert_entries[cert_index].len = cert_len; + certs->ipl_info_header.len +=sizeof (certs->cert_entries[cert_index]); +} + +static void comp_list_add (IplDeviceComponentList *comps,int comp_index, +int cert_index,uint64_t comp_addr, +uint64_t comp_len,uint8_t flags) +{ +if (comp_index >MAX_CERTIFICATES -1 ) { + printf("Warning: Ignoring comp entry [%d] because it's over %d entires\n",
dito
pc-bios/s390-ccw/secure-ipl.c line 140
Quoted 3 lines
pc-bios/s390-ccw/secure-ipl.c new file +3
-0+ comp_index + 1, MAX_CERTIFICATES); +return ; + }
Thomas
Re: [PATCH v7 19/29] pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
In reply to Zhuoying CaiQuoted 58 lines · Zhuoying Cai
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.
Add a
comp_lenvariable to store the length of a segment inzipl_load_segment.comp_lenvariable is necessary to store the calculated segment length and is used during signature verification. Return the length on success, or a negative return code on failure.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 <[email protected]>--- docs/system/s390x/secure-ipl.rst | 34 +++ pc-bios/s390-ccw/Makefile | 3 +- pc-bios/s390-ccw/bootmap.c | 44 +++- pc-bios/s390-ccw/bootmap.h | 11 + pc-bios/s390-ccw/main.c | 9 + pc-bios/s390-ccw/s390-ccw.h | 15 ++ pc-bios/s390-ccw/sclp.c | 44 ++++ pc-bios/s390-ccw/sclp.h | 6 + pc-bios/s390-ccw/secure-ipl.c | 383 +++++++++++++++++++++++++++++++ pc-bios/s390-ccw/secure-ipl.h | 94 ++++++++ 10 files changed, 638 insertions(+), 5 deletions(-) create mode 100644 pc-bios/s390-ccw/secure-ipl.c create mode 100644 pc-bios/s390-ccw/secure-ipl.hdocs/system/s390x/secure-ipl.rst +18
-0@@ -18,3 +18,37 @@ Note: certificate files must have a .pem extension. .. code-block:: shell qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ... + + +IPL Modes +========= +Multiple IPL modes are available to differentiate between the various IPL +configurations. These modes are mutually exclusive and enabled based on the +``boot-certs`` option on the QEMU command line. + +Normal Mode +----------- + +The absence of certificates will attempt to IPL a guest without secure IPL +operations. No checks are performed, and no warnings/errors are reported. +This is the default mode. + +Configuration: + +.. code-block:: shell
docs/system/s390x/secure-ipl.rst Note: certificate files must have a .pem extension. · line 48
Quoted 10 lines
docs/system/s390x/secure-ipl.rst +10
-0+ + qemu-system-s390x -machine s390-ccw-virtio ... + +Audit Mode +---------- + +With *only* the presence of certificates in the store, it is assumed that secure +boot operations should be performed with errors reported as warnings. As such, +the secure IPL operations will be performed, and any errors that stem from these +operations will result in a warning.
I think both sentences are saying the same thing. How about:
"When the certificate store is populated with at least one certificate and no additional secure IPL parameters are provided on the command line, then secure IPL will proceed in "audit mode". All secure IPL operations will be performed with signature verification errors reported as non-disruptive warnings."
Quoted 55 lines
docs/system/s390x/secure-ipl.rst +6
-0+ +Configuration: + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ...pc-bios/s390-ccw/Makefile +2
-1@@ -34,7 +34,8 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d .PHONY : all clean build-all distclean OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \ - virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o + virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \ + secure-ipl.o SLOF_DIR := $(SRC_PATH)/../../roms/SLOFpc-bios/s390-ccw/bootmap.c +9
-4@@ -15,6 +15,7 @@#include "bootmap.h" #include "virtio.h" #include "bswap.h" +#include "secure-ipl.h" #ifdef DEBUG /* #define DEBUG_FALLBACK */ @@ -617,7 +618,7 @@ static int ipl_eckd(void)* Returns: length of the segment on success, * negative value on error. */ -static int zipl_load_segment (ComponentEntry *entry,uint64_t address) +int zipl_load_segment (ComponentEntry *entry,uint64_t address) {const int max_entries = (MAX_SECTOR_SIZE /sizeof (ScsiBlockPtr));ScsiBlockPtr *bprs = (void *)sec;@@ -736,9 +737,22 @@ static int zipl_run(ScsiBlockPtr *pte)/* Load image(s) into RAM */ entry = (ComponentEntry *)(&header[1 ]); - rc =zipl_run_normal (&entry, tmp_sec); -if (rc) { -return rc; +switch (boot_mode) { +case ZIPL_BOOT_MODE_SECURE_AUDIT : + rc =zipl_run_secure (&entry, tmp_sec); +if (rc) { +return rc; + } +break ;
pc-bios/s390-ccw/bootmap.c static int zipl_run(ScsiBlockPtr *pte) · line 756
Quoted 10 lines
pc-bios/s390-ccw/bootmap.c +9
-0+case ZIPL_BOOT_MODE_NORMAL : + rc =zipl_run_normal (&entry, tmp_sec); +if (rc) { +return rc; + } +break ; +default : +puts ("Unknown boot mode" ); +return -1 ; }
Each case has a common pattern. Move the if (rc) check outside the switch block.
Quoted 7 lines
pc-bios/s390-ccw/bootmap.c +1
-0if (entry->component_type !=ZIPL_COMP_ENTRY_EXEC ) {@@ -1103,17 +1117,35 @@ static int zipl_load_vscsi(void)* IPL starts here */ +ZiplBootMode zipl_mode (uint8_t hdr_flags)
pc-bios/s390-ccw/bootmap.c static int zipl_load_vscsi(void) · line 1130
Quoted 10 lines
pc-bios/s390-ccw/bootmap.c +10
-0+{ +bool sipl_set = hdr_flags &DIAG308_IPIB_FLAGS_SIPL ; +bool iplir_set = hdr_flags &DIAG308_IPIB_FLAGS_IPLIR ; + +if (!sipl_set && iplir_set) { +return ZIPL_BOOT_MODE_SECURE_AUDIT ; + } + +return ZIPL_BOOT_MODE_NORMAL ; +}
What about making this a void function set_boot_mode() which gets invoked in main just after the iplb is stored. This would also remove the need for the "UNSPECIFIED" mode, since this variable would get resolved early on.
Perhaps it makes more sense to default to NORMAL unless the parameters specify otherwise?
Quoted 22 lines
pc-bios/s390-ccw/bootmap.c +7
-0+void zipl_load (void ) {VDev *vdev =virtio_get_device ();if (vdev->is_cdrom) { +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +panic ("Secure boot from ISO image is not supported!" ); + }ipl_iso_el_torito ();puts ("Failed to IPL this ISO image!" );return ; }if (virtio_get_device_type () ==VIRTIO_ID_NET ) { +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +panic ("Virtio net boot device does not support secure boot!" ); + }netmain ();puts ("Failed to IPL from this network!" );return ;
pc-bios/s390-ccw/bootmap.c void zipl_load(void) · line 1162
Quoted 7 lines
pc-bios/s390-ccw/bootmap.c +4
-0@@ -1124,6 +1156,10 @@ void zipl_load(void)return ; } +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +panic ("ECKD boot device does not support secure boot!" ); + } +
For all cases here in zipl_load, an IPL_assert is much cleaner. e.g.
IPL_assert(boot_mode == ZIPL_BOOT_MODE_NORMAL, "message");
Which will also cover the need to check for the other boot modes in a future patch.
Additionally, I'm not sure if "ECKD boot device" is correct here. Couldn't the IPL devices after this point actually be virtio-blk, virtio-scsi, etc? Perhaps a generic "Boot device does not support secure IPL" or something is sufficient.
Quoted 52 lines
pc-bios/s390-ccw/bootmap.c +0
-0switch (virtio_get_device_type ()) {case VIRTIO_ID_BLOCK :zipl_load_vblk ();pc-bios/s390-ccw/bootmap.h +11
-0@@ -88,9 +88,18 @@ typedef struct BootMapTable {BootMapPointer entry[]; } __attribute__ ((packed)) BootMapTable; +#define DER_SIGNATURE_FORMAT 1 + +typedef struct SignatureInformation { +uint8_t format; +uint8_t reserved[3 ]; +uint32_t sig_len; +}SignatureInformation ; +typedef union ComponentEntryData {uint64_t load_psw;uint64_t load_addr; +SignatureInformation sig_info; }ComponentEntryData ; typedef struct ComponentEntry {@@ -113,6 +122,8 @@ typedef struct ScsiMbr {ScsiBlockPtr pt;/* block pointer to program table */ } __attribute__ ((packed)) ScsiMbr; +int zipl_load_segment (ComponentEntry *entry,uint64_t address); +#define ZIPL_MAGIC "zIPL"#define ZIPL_MAGIC_EBCDIC "\xa9\xc9\xd7\xd3"#define IPL1_MAGIC "\xc9\xd7\xd3\xf1"/* == "IPL1" in EBCDIC */ pc-bios/s390-ccw/main.c +2
-0@@ -28,6 +28,7 @@ IplParameterBlock *iplb;bool have_iplb;static uint16_t cutype;LowCore *lowcore;/* Yes, this *is* a pointer to address 0 */ +ZiplBootMode boot_mode;#define LOADPARM_PROMPT "PROMPT "#define LOADPARM_EMPTY " "@@ -272,9 +273,17 @@ static int virtio_setup(void)static void ipl_boot_device (void ) { +if (boot_mode ==ZIPL_BOOT_MODE_UNSPECIFIED ) {
pc-bios/s390-ccw/main.c static int virtio_setup(void) · line 286
Quoted 10 lines
pc-bios/s390-ccw/main.c +7
-0+ boot_mode =zipl_mode (iplb->hdr_flags); + } +switch (cutype) {case CU_TYPE_DASD_3990 :case CU_TYPE_DASD_2107 : +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +panic ("Passthrough (vfio) CCW device does not support secure boot!" ); + } +
IPL_assert
Quoted 48 lines
pc-bios/s390-ccw/main.c +0
-0dasd_ipl (blk_schid, cutype);break ;case CU_TYPE_VIRTIO :pc-bios/s390-ccw/s390-ccw.h +15
-0@@ -39,6 +39,9 @@ typedef unsigned long long u64;#define MIN_NON_ZERO (a, b) ((a) == 0 ? (b) : \ ((b) == 0 ? (a) : (MIN(a, b))))#endif +#ifndef ROUND_UP +#define ROUND_UP (n, d) (((n) + (d) - 1) & -(0 ? (n) : (d))) +#endif #define ARRAY_SIZE (a) (sizeof(a) / sizeof((a)[0]))@@ -64,6 +67,8 @@ void sclp_print(const char *string);void sclp_set_write_mask (uint32_t receive_mask,uint32_t send_mask);void sclp_setup (void );void sclp_get_loadparm_ascii (char *loadparm); +bool sclp_is_diag320_on (void ); +bool sclp_is_sipl_on (void );int sclp_read (char *str,size_t count);/* virtio.c */ @@ -76,6 +81,16 @@ int virtio_read(unsigned long sector, void *load_addr);/* bootmap.c */ void zipl_load (void ); +typedef enum ZiplBootMode { +ZIPL_BOOT_MODE_UNSPECIFIED =0 , +ZIPL_BOOT_MODE_NORMAL =1 , +ZIPL_BOOT_MODE_SECURE_AUDIT =2 , +}ZiplBootMode ; + +extern ZiplBootMode boot_mode; + +ZiplBootMode zipl_mode (uint8_t hdr_flags); +/* jump2ipl.c */ void write_reset_psw (uint64_t psw);int jump_to_IPL_code (uint64_t address);pc-bios/s390-ccw/sclp.c +0
-0
pc-bios/s390-ccw/sclp.c void sclp_get_loadparm_ascii(char *loadparm) · line 118
Quoted 6 lines
pc-bios/s390-ccw/sclp.c +3
-0@@ -113,6 +113,50 @@ void sclp_get_loadparm_ascii(char *loadparm) } } +static void sclp_get_fac134 (uint8_t *fac134) +{ +
Remove extra newline.
Quoted 22 lines
pc-bios/s390-ccw/sclp.c +22
-0+ReadInfo *sccb = (void *)_sccb; + +memset ((char *)_sccb,0 ,sizeof (ReadInfo)); + sccb->h.length =SCCB_SIZE ; +if (!sclp_service_call (SCLP_CMDW_READ_SCP_INFO , sccb)) { + *fac134 = sccb->fac134; + } +} + +bool sclp_is_diag320_on (void ) +{ +uint8_t fac134 =0 ; + +sclp_get_fac134 (&fac134); +return fac134 &SCCB_FAC134_DIAG320_BIT ; +} + +/* +* Get fac_ipl (byte 136 and byte 137 of the SCLP Read Info block) +* for IPL device facilities. +*/ +static void sclp_get_fac_ipl (uint16_t *fac_ipl)
pc-bios/s390-ccw/sclp.c void sclp_get_loadparm_ascii(char *loadparm) · line 150
Quoted 10 lines
pc-bios/s390-ccw/sclp.c +10
-0+{ + +ReadInfo *sccb = (void *)_sccb; + +memset ((char *)_sccb,0 ,sizeof (ReadInfo)); + sccb->h.length =SCCB_SIZE ; +if (!sclp_service_call (SCLP_CMDW_READ_SCP_INFO , sccb)) { + *fac_ipl = sccb->fac_ipl; + } +}
Hmm. The number of SCLP service calls could be reduced by doing a singular read scp info call early and storing all the fields needed to be accessed later.
But maybe that is outside the scope of these patches.
Quoted 71 lines
pc-bios/s390-ccw/sclp.c +9
-0+ +bool sclp_is_sipl_on (void ) +{ +uint16_t fac_ipl =0 ; + +sclp_get_fac_ipl (&fac_ipl); +return fac_ipl &SCCB_FAC_IPL_SIPL_BIT ; +} +int sclp_read (char *str,size_t count) {ReadEventData *sccb = (void *)_sccb;pc-bios/s390-ccw/sclp.h +6
-0@@ -50,6 +50,8 @@ typedef struct SCCBHeader { } __attribute__((packed)) SCCBHeader;#define SCCB_DATA_LEN (SCCB_SIZE - sizeof(SCCBHeader)) +#define SCCB_FAC134_DIAG320_BIT 0x4 +#define SCCB_FAC_IPL_SIPL_BIT 0x4000 typedef struct ReadInfo { SCCBHeader h;@@ -57,6 +59,10 @@ typedef struct ReadInfo {uint8_t rnsize;uint8_t reserved[13 ];uint8_t loadparm[LOADPARM_LEN ]; +uint8_t reserved1[102 ]; +uint8_t fac134; +uint8_t reserved2; +uint16_t fac_ipl; } __attribute__((packed)) ReadInfo; typedef struct SCCB {pc-bios/s390-ccw/secure-ipl.c new file +29
-0@@ -0,0 +1,383 @@ +/* +* S/390 Secure IPL +* +* Functions to support IPL in secure boot mode (DIAG 320, DIAG 508, +* signature verification, and certificate handling). +* +* For secure IPL overview: docs/system/s390x/secure-ipl.rst +* For secure IPL technical: docs/specs/s390x-secure-ipl.rst +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include "bootmap.h" +#include "s390-ccw.h" +#include "secure-ipl.h" + +uint8_t vcssb_data[VCSSB_MIN_LEN] __attribute__((__aligned__(PAGE_SIZE))); + +VCStorageSizeBlock *zipl_secure_get_vcssb (void ) +{ +VCStorageSizeBlock *vcssb; +int rc; +
pc-bios/s390-ccw/secure-ipl.c line 39
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+if (!sclp_is_diag320_on () || !is_cert_store_facility_supported ()) { +puts ("Certificate Store Facility is not supported by the hypervisor!" ); +return NULL ; + } + + vcssb = (VCStorageSizeBlock *)vcssb_data; +/* avoid retrieving vcssb multiple times */ +if (vcssb->length >=VCSSB_MIN_LEN ) { +return vcssb; + }
Change the order of operations here to check if vcssb has been retrieved before checking diag320 stuff. Otherwise, every time zipl_secure_get_vcssb() is invoked, both an sclp service call and a diag320 call is always performed, which is unnecessary if the VCSSB has already been stored.
pc-bios/s390-ccw/secure-ipl.c line 45
Quoted 6 lines
pc-bios/s390-ccw/secure-ipl.c new file +6
-0+ + vcssb->length =VCSSB_MIN_LEN ; + rc =diag320 (vcssb,DIAG_320_SUBC_QUERY_VCSI ); +if (rc !=DIAG_320_RC_OK ) { +return NULL ; + }
Can do if (diag320(vcssb, DIAG_320_SUBC_QUERY_VCSI) != DIAG_320_RC_OK)
and then get rid of rc
pc-bios/s390-ccw/secure-ipl.c line 50
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c new file +5
-0+ +return vcssb; +} + +static uint32_t get_certs_length (void )
The function name is ambiguous. I had to digest the math below to understand the intention. How about something like...
get_total_certs_length()
...to help clarify that this function is returning a value to store all certs.
pc-bios/s390-ccw/secure-ipl.c line 61
Quoted 11 lines
pc-bios/s390-ccw/secure-ipl.c new file +11
-0+{ +VCStorageSizeBlock *vcssb; +uint32_t len; + + vcssb =zipl_secure_get_vcssb (); +if (vcssb ==NULL ) { +return 0 ; + } + + len = vcssb->total_vcb_len -VCB_HEADER_LEN - vcssb->total_vc_ct *VCE_HEADER_LEN ; +
Could return the line above and get rid of len.
Quoted 17 lines
pc-bios/s390-ccw/secure-ipl.c new file +17
-0+return len; +} + +static uint32_t request_certificate (uint8_t *cert,uint8_t index) +{ +VCStorageSizeBlock *vcssb; +VCBlock *vcb; +VCEntry *vce; +uint64_t rc =0 ; +uint32_t cert_len =0 ; +uint32_t max_single_vcb_len; + +/* Get Verification Certificate Storage Size block with DIAG320 subcode 1 */ + vcssb =zipl_secure_get_vcssb (); +if (vcssb ==NULL ) { +return 0 ; + }
pc-bios/s390-ccw/secure-ipl.c line 88
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+ +/* +* Request single entry +* Fill input fields of single-entry VCB +*/ + max_single_vcb_len =ROUND_UP (vcssb->max_single_vcb_len,PAGE_SIZE ); + vcb =malloc (max_single_vcb_len); + vcb->in_len = max_single_vcb_len; + vcb->first_vc_index = index +1 ; + vcb->last_vc_index = index +1 ;
There needs to be a better way to account for the index origin differences. See my comments below for verify_signature().
pc-bios/s390-ccw/secure-ipl.c line 93
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c new file +5
-0+ + rc =diag320 (vcb,DIAG_320_SUBC_STORE_VC ); +if (rc !=DIAG_320_RC_OK ) { + goto out; + }
if (!diag320(vcb, DIAG_320_SUBC_STORE_VC))
then get rid of rc
Quoted 16 lines
pc-bios/s390-ccw/secure-ipl.c new file +16
-0+ +if (vcb->out_len ==VCB_HEADER_LEN ) { +puts ("No certificate entry" ); + goto out; + } +if (vcb->remain_ct !=0 ) { +puts ("Not enough memory to store all requested certificates" ); + goto out; + } + + vce = (VCEntry *)vcb->vce_buf; +if (!(vce->flags &DIAG_320_VCE_FLAGS_VALID )) { +puts ("Invalid certificate" ); + goto out; + } +
pc-bios/s390-ccw/secure-ipl.c line 119
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+ cert_len = vce->cert_len; +memcpy (cert, (uint8_t *)vce + vce->cert_offset, vce->cert_len); + +out: +free (vcb); +return cert_len; +} + +static void cert_list_add (IplSignatureCertificateList *certs,int cert_index, +uint8_t *cert,uint64_t cert_len)
s/certs/cert_list
s/cert/cert_addr
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c new file +5
-0+{ +if (cert_index >MAX_CERTIFICATES -1 ) { +printf ("Warning: Ignoring cert entry [%d] because it's over %d entires\n" , + cert_index +1 ,MAX_CERTIFICATES ); +return ;
pc-bios/s390-ccw/secure-ipl.c line 134
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+ } + + certs->cert_entries[cert_index].addr = (uint64_t )cert; + certs->cert_entries[cert_index].len = cert_len; + certs->ipl_info_header.len +=sizeof (certs->cert_entries[cert_index]); +} + +static void comp_list_add (IplDeviceComponentList *comps,int comp_index, +int cert_index,uint64_t comp_addr, +uint64_t comp_len,uint8_t flags)
s/comps/comp_list
Quoted 24 lines
pc-bios/s390-ccw/secure-ipl.c new file +24
-0+{ +if (comp_index >MAX_CERTIFICATES -1 ) { +printf ("Warning: Ignoring comp entry [%d] because it's over %d entires\n" , + comp_index +1 ,MAX_CERTIFICATES ); +return ; + } + + comps->device_entries[comp_index].addr = comp_addr; + comps->device_entries[comp_index].len = comp_len; + comps->device_entries[comp_index].flags = flags; + comps->device_entries[comp_index].cert_index = cert_index; + comps->ipl_info_header.len +=sizeof (comps->device_entries[comp_index]); +} + +static int update_iirb (IplDeviceComponentList *comps,IplSignatureCertificateList *certs) +{ +IplInfoReportBlock *iirb; +IplDeviceComponentList *iirb_comps; +IplSignatureCertificateList *iirb_certs; +uint32_t iirb_hdr_len; +uint32_t comps_len; +uint32_t certs_len; + +if (iplb->len %8 !=0 ) {
pc-bios/s390-ccw/secure-ipl.c line 168
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+panic ("IPL parameter block length field value is not multiple of 8 bytes" ); + } + + iirb_hdr_len =sizeof (IplInfoReportBlockHeader); + comps_len = comps->ipl_info_header.len; + certs_len = certs->ipl_info_header.len; +if ((comps_len + certs_len + iirb_hdr_len) >sizeof (IplInfoReportBlock)) { +puts ("Not enough space to hold all components and certificates in IIRB" ); +return -1 ; + }
Shouldn't this also be a panic? If there's not enough space for the IIRB, then that should be more serious than just warning in audit mode.
Quoted 44 lines
pc-bios/s390-ccw/secure-ipl.c new file +44
-0+ +/* IIRB immediately follows IPLB */ + iirb = &ipl_data.iirb; + iirb->hdr.len = iirb_hdr_len; + +/* Copy IPL device component list after IIRB Header */ + iirb_comps = (IplDeviceComponentList *) iirb->info_blks; +memcpy (iirb_comps, comps, comps_len); + +/* Update IIRB length */ + iirb->hdr.len += comps_len; + +/* Copy IPL sig cert list after IPL device component list */ + iirb_certs = (IplSignatureCertificateList *) (iirb->info_blks + + iirb_comps->ipl_info_header.len); +memcpy (iirb_certs, certs, certs_len); + +/* Update IIRB length */ + iirb->hdr.len += certs_len; + +return 0 ; +} + +static bool secure_ipl_supported (void ) +{ +if (!sclp_is_sipl_on ()) { +puts ("Secure IPL Facility is not supported by the hypervisor!" ); +return false; + } + +if (!is_secure_ipl_extension_supported ()) { +puts ("Secure IPL extensions are not supported by the hypervisor!" ); +return false; + } + +if (!sclp_is_diag320_on () || !is_cert_store_facility_supported ()) { +puts ("Certificate Store Facility is not supported by the hypervisor!" ); +return false; + } + +return true; +} + +static void init_lists (IplDeviceComponentList *comps,IplSignatureCertificateList *certs)
pc-bios/s390-ccw/secure-ipl.c line 222
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+{ + comps->ipl_info_header.ibt =IPL_IBT_COMPONENTS ; + comps->ipl_info_header.len =sizeof (comps->ipl_info_header); + + certs->ipl_info_header.ibt =IPL_IBT_CERTIFICATES ; + certs->ipl_info_header.len =sizeof (certs->ipl_info_header); +} + +static uint32_t zipl_load_signature (ComponentEntry *entry,uint64_t sig_sec) +{
Needs to be type int, otherwise returning -1 below doesn't make sense.
pc-bios/s390-ccw/secure-ipl.c line 235
Quoted 13 lines
pc-bios/s390-ccw/secure-ipl.c new file +13
-0+uint32_t sig_len; + +if (zipl_load_segment (entry, sig_sec) <0 ) { +return -1 ; + } + +if (entry->compdat.sig_info.format !=DER_SIGNATURE_FORMAT ) { +puts ("Signature is not in DER format" ); +return -1 ; + } + sig_len = entry->compdat.sig_info.sig_len; + +return sig_len;
Could return entry->compdat.sig_info.sig_len, then sig_len goes away.
Quoted 19 lines
pc-bios/s390-ccw/secure-ipl.c new file +19
-0+} + +/* +* Returns: 1 - New certificate requested and added to cert_table and cert list +* 0 - Certificate already exists in the cert_table +* -1 - Error while requesting certificate +*/ +static int handle_certificate (int *cert_table,uint8_t **cert, +uint64_t cert_len,uint8_t cert_table_idx, +IplSignatureCertificateList *certs,int cert_entry_idx) +{ +if (cert_table[cert_table_idx] !=-1 ) { +return 0 ; + } + +if (!request_certificate (*cert, cert_table_idx)) { +puts ("Could not get certificate" ); +return -1 ; + }
pc-bios/s390-ccw/secure-ipl.c line 264
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+ +cert_list_add (certs, cert_entry_idx, *cert, cert_len); + cert_table[cert_table_idx] = cert_entry_idx; +return 1 ; +} + +int zipl_run_secure (ComponentEntry **entry_ptr,uint8_t *tmp_sec) +{ +IplDeviceComponentList comps; +IplSignatureCertificateList certs;
It would read a little easier if these were comp_list and cert_list
Quoted 11 lines
pc-bios/s390-ccw/secure-ipl.c new file +11
-0+ComponentEntry *entry = *entry_ptr; +uint8_t *cert =NULL ; +uint64_t *sig =NULL ; +int cert_entry_idx =0 ; +int comp_entry_idx =0 ; +uint64_t comp_addr; +int comp_len; +uint32_t sig_len =0 ; +uint64_t cert_len =-1 ; +uint8_t cert_table_idx =-1 ; +bool verified;
pc-bios/s390-ccw/secure-ipl.c line 285
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+uint32_t certs_len; +/* +* Store indices of cert entry that have already used for signature +* verification to prevent allocating the same certificate multiple times. +* cert_table index (cert_table_idx): +* index of certificate from qemu cert store used for verification +* cert_table value (cert_entry_idx): +* index of cert entry in cert list that contains the certificate +*/ +int cert_table[MAX_CERTIFICATES ] = { [0 ...MAX_CERTIFICATES -1 ] =-1 };
If you agree with the suggestion below for verify_signature to adjust the returned index, then the cert_table can be used a bit differently:
Make this an array of booleans. Use the index returned by verify_signature. Value true means that the cert has already been added to the IplSignatureCertificateList. False means it has yet to be added to the list.
Remove the mention of "qemu cert store", as the BIOS technically does not know anything about the underlying cert store or its implementation.
pc-bios/s390-ccw/secure-ipl.c line 295
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+int rc; +int signed_count =0 ; + +if (!secure_ipl_supported ()) { +return -1 ; + } + +init_lists (&comps, &certs); + certs_len =get_certs_length (); + cert =malloc (certs_len);
cert = malloc(get_total_certs_length());
Then get rid of the certs_len variable.
Quoted 21 lines
pc-bios/s390-ccw/secure-ipl.c new file +21
-0+ sig =malloc (MAX_SECTOR_SIZE ); + +while (entry->component_type !=ZIPL_COMP_ENTRY_EXEC ) { +switch (entry->component_type) { +case ZIPL_COMP_ENTRY_SIGNATURE : +if (sig_len) { + goto out; + } + + sig_len =zipl_load_signature (entry, (uint64_t )sig); +if (sig_len <0 ) { + goto out; + } +break ; +case ZIPL_COMP_ENTRY_LOAD : + comp_addr = entry->compdat.load_addr; + comp_len =zipl_load_segment (entry, comp_addr); +if (comp_len <0 ) { + goto out; + } +
pc-bios/s390-ccw/secure-ipl.c line 326
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+if (!sig_len) { +break ; + } + + verified =verify_signature (comp_len, comp_addr, sig_len, (uint64_t )sig, + &cert_len, &cert_table_idx); + +if (verified) { + rc =handle_certificate (cert_table, &cert, cert_len, + cert_table_idx, &certs, cert_entry_idx);
Check cert_table here before calling handle_certificate. It would contain all the cert_table logic to zipl_run_secure and make it a little bit easier to understand. Also less variables to pass around.
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+if (rc ==-1 ) { + goto out; + } + +/* increment for the next certificate */ +if (rc ==1 ) { + cert_entry_idx++; + cert += cert_len; + } +
pc-bios/s390-ccw/secure-ipl.c line 346
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+puts ("Verified component" ); +comp_list_add (&comps, comp_entry_idx, cert_table[cert_table_idx], + comp_addr, comp_len, +S390_IPL_COMPONENT_FLAG_SC |S390_IPL_COMPONENT_FLAG_CSV ); + }else { +comp_list_add (&comps, comp_entry_idx,-1 , + comp_addr, comp_len, +S390_IPL_COMPONENT_FLAG_SC ); +zipl_secure_handle ("Could not verify component" ); + }
This can be cleaned up a bit. Set cert_index and flags within the if/else block. Call comp_list_add() once after the block.
Quoted 52 lines
pc-bios/s390-ccw/secure-ipl.c new file +37
-0+ + comp_entry_idx++; + signed_count +=1 ; +/* After a signature is used another new one can be accepted */ + sig_len =0 ; +break ; +default : +puts ("Unknown component entry type" ); +return -1 ; + } + + entry++; + +if ((uint8_t *)(&entry[1 ]) > tmp_sec +MAX_SECTOR_SIZE ) { +puts ("Wrong entry value" ); +return -EINVAL ; + } + } + +if (signed_count ==0 ) { +zipl_secure_handle ("Secure boot is on, but components are not signed" ); + } + +if (update_iirb (&comps, &certs)) { +zipl_secure_handle ("Failed to write IPL Information Report Block" ); + } + + *entry_ptr = entry; +free (sig); + +return 0 ; +out: +free (cert); +free (sig); + +return -1 ; +}pc-bios/s390-ccw/secure-ipl.h new file +9
-0@@ -0,0 +1,94 @@ +/* +* S/390 Secure IPL +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +
pc-bios/s390-ccw/secure-ipl.h line 19
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.h new file +10
-0+#ifndef _PC_BIOS_S390_CCW_SECURE_IPL_H +#define _PC_BIOS_S390_CCW_SECURE_IPL_H + +#include <diag320.h> +#include <diag508.h> + +VCStorageSizeBlock *zipl_secure_get_vcssb (void ); +int zipl_run_secure (ComponentEntry **entry_ptr,uint8_t *tmp_sec); + +static inline void zipl_secure_handle (const char *message)
This could be implemented like an assert, where the first param is the condition.
e.g.
`zipl_secure_handle(signed_count > 0, "Secure boot is on, but components
are not signed");`
Then some if checks / extra indentation goes away.
Quoted 21 lines
pc-bios/s390-ccw/secure-ipl.h new file +21
-0+{ +switch (boot_mode) { +case ZIPL_BOOT_MODE_SECURE_AUDIT : +IPL_check (false, message); +break ; +default : +break ; + } +} + +static inline uint64_t diag320 (void *data,unsigned long subcode) +{ + register unsigned long addr asm("0") = (unsigned long)data; + register unsigned long rc asm("1") = 0; + + asmvolatile ("diag %0,%2,0x320\n" + :"+d" (addr),"+d" (rc) + :"d" (subcode) + :"memory" ,"cc" ); +return rc; +}
pc-bios/s390-ccw/secure-ipl.h line 50
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.h new file +10
-0+ +static inline bool is_cert_store_facility_supported (void ) +{ +uint32_t d320_ism; + +diag320 (&d320_ism,DIAG_320_SUBC_QUERY_ISM ); +return (d320_ism &DIAG_320_ISM_QUERY_SUBCODES ) && + (d320_ism &DIAG_320_ISM_QUERY_VCSI ) && + (d320_ism &DIAG_320_ISM_STORE_VC ); +}
Don't need to check if DIAG_320_ISM_QUERY_SUBCODES is supported (implied if query ISM returns OK).
Can clean this up by doing return d320_ism & (DIAG_320_ISM_QUERY_VCSI | DIAG_320_ISM_STORE_VC);
Merge this function with sclp_is_diag320_on(), checking the read scp facility bit first before calling diag320.
Quoted 4 lines
pc-bios/s390-ccw/secure-ipl.h new file +4
-0+ +static inline uint64_t _diag508 (void *data,unsigned long subcode) +{ + register unsigned long addr asm("0") = (unsigned long)data;
pc-bios/s390-ccw/secure-ipl.h line 64
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.h new file +10
-0+ register unsigned long rc asm("1") = 0; + + asmvolatile ("diag %0,%2,0x508\n" + :"+d" (addr),"+d" (rc) + :"d" (subcode) + :"memory" ,"cc" ); +return rc; +} + +static inline bool is_secure_ipl_extension_supported (void )
"is_signature_verif_supported()" or something like that reads better.
Quoted 13 lines
pc-bios/s390-ccw/secure-ipl.h new file +13
-0+{ +uint64_t d508_subcodes; + + d508_subcodes =_diag508 (NULL ,DIAG_508_SUBC_QUERY_SUBC ); +return d508_subcodes &DIAG_508_SUBC_SIG_VERIF ; +} + +static inline bool verify_signature (uint64_t comp_len,uint64_t comp_addr, +uint64_t sig_len,uint64_t sig_addr, +uint64_t *cert_len,uint8_t *cert_idx) +{ +Diag508SigVerifBlock svb; +
pc-bios/s390-ccw/secure-ipl.h line 87
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.h new file +10
-0+ svb.length =sizeof (Diag508SigVerifBlock); + svb.version =0 ; + svb.comp_len = comp_len; + svb.comp_addr = comp_addr; + svb.sig_len = sig_len; + svb.sig_addr = sig_addr; + +if (_diag508 (&svb,DIAG_508_SUBC_SIG_VERIF ) ==DIAG_508_RC_OK ) { + *cert_len = svb.cert_len; + *cert_idx = svb.cert_store_index;
Adjust for different indexing origins here, accounting for what DIAG320 will later need when retrieving certs:
``` /* * DIAG 508 utilizes an index origin of 0 when indexing the cert store. * The cert_idx will be used for DIAG 320 data structures, which expects * an index origin of 1. Account for the offset here so it's easier to * manage later. */ *cert_idx = svb.cert_store_index + 1; ```
Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.h new file +7
-0+ return true; + } + +return false; +} + +#endif /* _PC_BIOS_S390_CCW_SECURE_IPL_H */
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 19/29] pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
Thanks for reviewing!
Quoted 170 lines · Collin Walling
Quoted 58 lines · Zhuoying Cai
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.
Add a
comp_lenvariable to store the length of a segment inzipl_load_segment.comp_lenvariable is necessary to store the calculated segment length and is used during signature verification. Return the length on success, or a negative return code on failure.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 <[email protected]>--- docs/system/s390x/secure-ipl.rst | 34 +++ pc-bios/s390-ccw/Makefile | 3 +- pc-bios/s390-ccw/bootmap.c | 44 +++- pc-bios/s390-ccw/bootmap.h | 11 + pc-bios/s390-ccw/main.c | 9 + pc-bios/s390-ccw/s390-ccw.h | 15 ++ pc-bios/s390-ccw/sclp.c | 44 ++++ pc-bios/s390-ccw/sclp.h | 6 + pc-bios/s390-ccw/secure-ipl.c | 383 +++++++++++++++++++++++++++++++ pc-bios/s390-ccw/secure-ipl.h | 94 ++++++++ 10 files changed, 638 insertions(+), 5 deletions(-) create mode 100644 pc-bios/s390-ccw/secure-ipl.c create mode 100644 pc-bios/s390-ccw/secure-ipl.hdocs/system/s390x/secure-ipl.rst +18
-0@@ -18,3 +18,37 @@ Note: certificate files must have a .pem extension. .. code-block:: shell qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ... + + +IPL Modes +========= +Multiple IPL modes are available to differentiate between the various IPL +configurations. These modes are mutually exclusive and enabled based on the +``boot-certs`` option on the QEMU command line. + +Normal Mode +----------- + +The absence of certificates will attempt to IPL a guest without secure IPL +operations. No checks are performed, and no warnings/errors are reported. +This is the default mode. + +Configuration: + +.. code-block:: shelldocs/system/s390x/secure-ipl.rst Note: certificate files must have a .pem extension. · line 48
Quoted 10 lines
docs/system/s390x/secure-ipl.rst +10
-0+ + qemu-system-s390x -machine s390-ccw-virtio ... + +Audit Mode +---------- + +With *only* the presence of certificates in the store, it is assumed that secure +boot operations should be performed with errors reported as warnings. As such, +the secure IPL operations will be performed, and any errors that stem from these +operations will result in a warning.I think both sentences are saying the same thing. How about:
"When the certificate store is populated with at least one certificate and no additional secure IPL parameters are provided on the command line, then secure IPL will proceed in "audit mode". All secure IPL operations will be performed with signature verification errors reported as non-disruptive warnings."
Quoted 55 lines
docs/system/s390x/secure-ipl.rst +6
-0+ +Configuration: + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ...pc-bios/s390-ccw/Makefile +2
-1@@ -34,7 +34,8 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d .PHONY : all clean build-all distclean OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \ - virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o + virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \ + secure-ipl.o SLOF_DIR := $(SRC_PATH)/../../roms/SLOFpc-bios/s390-ccw/bootmap.c +9
-4@@ -15,6 +15,7 @@#include "bootmap.h" #include "virtio.h" #include "bswap.h" +#include "secure-ipl.h" #ifdef DEBUG /* #define DEBUG_FALLBACK */ @@ -617,7 +618,7 @@ static int ipl_eckd(void)* Returns: length of the segment on success, * negative value on error. */ -static int zipl_load_segment (ComponentEntry *entry,uint64_t address) +int zipl_load_segment (ComponentEntry *entry,uint64_t address) {const int max_entries = (MAX_SECTOR_SIZE /sizeof (ScsiBlockPtr));ScsiBlockPtr *bprs = (void *)sec;@@ -736,9 +737,22 @@ static int zipl_run(ScsiBlockPtr *pte)/* Load image(s) into RAM */ entry = (ComponentEntry *)(&header[1 ]); - rc =zipl_run_normal (&entry, tmp_sec); -if (rc) { -return rc; +switch (boot_mode) { +case ZIPL_BOOT_MODE_SECURE_AUDIT : + rc =zipl_run_secure (&entry, tmp_sec); +if (rc) { +return rc; + } +break ;pc-bios/s390-ccw/bootmap.c static int zipl_run(ScsiBlockPtr *pte) · line 756
Quoted 10 lines
pc-bios/s390-ccw/bootmap.c +9
-0+case ZIPL_BOOT_MODE_NORMAL : + rc =zipl_run_normal (&entry, tmp_sec); +if (rc) { +return rc; + } +break ; +default : +puts ("Unknown boot mode" ); +return -1 ; }Each case has a common pattern. Move the if (rc) check outside the switch block.
Quoted 7 lines
pc-bios/s390-ccw/bootmap.c +1
-0if (entry->component_type !=ZIPL_COMP_ENTRY_EXEC ) {@@ -1103,17 +1117,35 @@ static int zipl_load_vscsi(void)* IPL starts here */ +ZiplBootMode zipl_mode (uint8_t hdr_flags)pc-bios/s390-ccw/bootmap.c static int zipl_load_vscsi(void) · line 1130
Quoted 10 lines
pc-bios/s390-ccw/bootmap.c +10
-0+{ +bool sipl_set = hdr_flags &DIAG308_IPIB_FLAGS_SIPL ; +bool iplir_set = hdr_flags &DIAG308_IPIB_FLAGS_IPLIR ; + +if (!sipl_set && iplir_set) { +return ZIPL_BOOT_MODE_SECURE_AUDIT ; + } + +return ZIPL_BOOT_MODE_NORMAL ; +}What about making this a void function
set_boot_mode()which gets invoked in main just after the iplb is stored. This would also remove the need for the "UNSPECIFIED" mode, since this variable would get resolved early on.
Quoted 3 lines
Perhaps it makes more sense to default to NORMAL unless the parameters specify otherwise?
I agree that NORMAL can be the default, so the UNSPECIFIED mode can be removed. It may be clearer to keep this as a value-returning helper and rename it to get_boot_mode() (or similar). The function simply derives the boot mode from the IPLB flags, and main can assign it directly after the IPLB is stored:
boot_mode = get_boot_mode(iplb->hdr_flags);
Quoted 34 lines
Quoted 22 lines
pc-bios/s390-ccw/bootmap.c +7
-0+void zipl_load (void ) {VDev *vdev =virtio_get_device ();if (vdev->is_cdrom) { +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +panic ("Secure boot from ISO image is not supported!" ); + }ipl_iso_el_torito ();puts ("Failed to IPL this ISO image!" );return ; }if (virtio_get_device_type () ==VIRTIO_ID_NET ) { +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +panic ("Virtio net boot device does not support secure boot!" ); + }netmain ();puts ("Failed to IPL from this network!" );return ;pc-bios/s390-ccw/bootmap.c void zipl_load(void) · line 1162
Quoted 7 lines
pc-bios/s390-ccw/bootmap.c +4
-0@@ -1124,6 +1156,10 @@ void zipl_load(void)return ; } +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +panic ("ECKD boot device does not support secure boot!" ); + } +For all cases here in
zipl_load, anIPL_assertis much cleaner. e.g.
IPL_assert(boot_mode == ZIPL_BOOT_MODE_NORMAL, "message");
Quoted 8 lines
Which will also cover the need to check for the other boot modes in a future patch.
Additionally, I'm not sure if "ECKD boot device" is correct here. Couldn't the IPL devices after this point actually be virtio-blk, virtio-scsi, etc? Perhaps a generic "Boot device does not support secure IPL" or something is sufficient.
Yes, IPL devices after this point are virtio-blk/virtio-scsi using the ECKD scheme. Maybe we could rephrase the message to something like:
"Secure boot with the ECKD scheme is not supported!"
Quoted 542 lines
Quoted 52 lines
pc-bios/s390-ccw/bootmap.c +0
-0switch (virtio_get_device_type ()) {case VIRTIO_ID_BLOCK :zipl_load_vblk ();pc-bios/s390-ccw/bootmap.h +11
-0@@ -88,9 +88,18 @@ typedef struct BootMapTable {BootMapPointer entry[]; } __attribute__ ((packed)) BootMapTable; +#define DER_SIGNATURE_FORMAT 1 + +typedef struct SignatureInformation { +uint8_t format; +uint8_t reserved[3 ]; +uint32_t sig_len; +}SignatureInformation ; +typedef union ComponentEntryData {uint64_t load_psw;uint64_t load_addr; +SignatureInformation sig_info; }ComponentEntryData ; typedef struct ComponentEntry {@@ -113,6 +122,8 @@ typedef struct ScsiMbr {ScsiBlockPtr pt;/* block pointer to program table */ } __attribute__ ((packed)) ScsiMbr; +int zipl_load_segment (ComponentEntry *entry,uint64_t address); +#define ZIPL_MAGIC "zIPL"#define ZIPL_MAGIC_EBCDIC "\xa9\xc9\xd7\xd3"#define IPL1_MAGIC "\xc9\xd7\xd3\xf1"/* == "IPL1" in EBCDIC */ pc-bios/s390-ccw/main.c +2
-0@@ -28,6 +28,7 @@ IplParameterBlock *iplb;bool have_iplb;static uint16_t cutype;LowCore *lowcore;/* Yes, this *is* a pointer to address 0 */ +ZiplBootMode boot_mode;#define LOADPARM_PROMPT "PROMPT "#define LOADPARM_EMPTY " "@@ -272,9 +273,17 @@ static int virtio_setup(void)static void ipl_boot_device (void ) { +if (boot_mode ==ZIPL_BOOT_MODE_UNSPECIFIED ) {pc-bios/s390-ccw/main.c static int virtio_setup(void) · line 286
Quoted 10 lines
pc-bios/s390-ccw/main.c +7
-0+ boot_mode =zipl_mode (iplb->hdr_flags); + } +switch (cutype) {case CU_TYPE_DASD_3990 :case CU_TYPE_DASD_2107 : +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +panic ("Passthrough (vfio) CCW device does not support secure boot!" ); + } +
IPL_assertQuoted 48 lines
pc-bios/s390-ccw/main.c +0
-0dasd_ipl (blk_schid, cutype);break ;case CU_TYPE_VIRTIO :pc-bios/s390-ccw/s390-ccw.h +15
-0@@ -39,6 +39,9 @@ typedef unsigned long long u64;#define MIN_NON_ZERO (a, b) ((a) == 0 ? (b) : \ ((b) == 0 ? (a) : (MIN(a, b))))#endif +#ifndef ROUND_UP +#define ROUND_UP (n, d) (((n) + (d) - 1) & -(0 ? (n) : (d))) +#endif #define ARRAY_SIZE (a) (sizeof(a) / sizeof((a)[0]))@@ -64,6 +67,8 @@ void sclp_print(const char *string);void sclp_set_write_mask (uint32_t receive_mask,uint32_t send_mask);void sclp_setup (void );void sclp_get_loadparm_ascii (char *loadparm); +bool sclp_is_diag320_on (void ); +bool sclp_is_sipl_on (void );int sclp_read (char *str,size_t count);/* virtio.c */ @@ -76,6 +81,16 @@ int virtio_read(unsigned long sector, void *load_addr);/* bootmap.c */ void zipl_load (void ); +typedef enum ZiplBootMode { +ZIPL_BOOT_MODE_UNSPECIFIED =0 , +ZIPL_BOOT_MODE_NORMAL =1 , +ZIPL_BOOT_MODE_SECURE_AUDIT =2 , +}ZiplBootMode ; + +extern ZiplBootMode boot_mode; + +ZiplBootMode zipl_mode (uint8_t hdr_flags); +/* jump2ipl.c */ void write_reset_psw (uint64_t psw);int jump_to_IPL_code (uint64_t address);pc-bios/s390-ccw/sclp.c +0
-0pc-bios/s390-ccw/sclp.c void sclp_get_loadparm_ascii(char *loadparm) · line 118
Quoted 6 lines
pc-bios/s390-ccw/sclp.c +3
-0@@ -113,6 +113,50 @@ void sclp_get_loadparm_ascii(char *loadparm) } } +static void sclp_get_fac134 (uint8_t *fac134) +{ +Remove extra newline.
Quoted 22 lines
pc-bios/s390-ccw/sclp.c +22
-0+ReadInfo *sccb = (void *)_sccb; + +memset ((char *)_sccb,0 ,sizeof (ReadInfo)); + sccb->h.length =SCCB_SIZE ; +if (!sclp_service_call (SCLP_CMDW_READ_SCP_INFO , sccb)) { + *fac134 = sccb->fac134; + } +} + +bool sclp_is_diag320_on (void ) +{ +uint8_t fac134 =0 ; + +sclp_get_fac134 (&fac134); +return fac134 &SCCB_FAC134_DIAG320_BIT ; +} + +/* +* Get fac_ipl (byte 136 and byte 137 of the SCLP Read Info block) +* for IPL device facilities. +*/ +static void sclp_get_fac_ipl (uint16_t *fac_ipl)pc-bios/s390-ccw/sclp.c void sclp_get_loadparm_ascii(char *loadparm) · line 150
Quoted 10 lines
pc-bios/s390-ccw/sclp.c +10
-0+{ + +ReadInfo *sccb = (void *)_sccb; + +memset ((char *)_sccb,0 ,sizeof (ReadInfo)); + sccb->h.length =SCCB_SIZE ; +if (!sclp_service_call (SCLP_CMDW_READ_SCP_INFO , sccb)) { + *fac_ipl = sccb->fac_ipl; + } +}Hmm. The number of SCLP service calls could be reduced by doing a singular read scp info call early and storing all the fields needed to be accessed later.
But maybe that is outside the scope of these patches.
Quoted 71 lines
pc-bios/s390-ccw/sclp.c +9
-0+ +bool sclp_is_sipl_on (void ) +{ +uint16_t fac_ipl =0 ; + +sclp_get_fac_ipl (&fac_ipl); +return fac_ipl &SCCB_FAC_IPL_SIPL_BIT ; +} +int sclp_read (char *str,size_t count) {ReadEventData *sccb = (void *)_sccb;pc-bios/s390-ccw/sclp.h +6
-0@@ -50,6 +50,8 @@ typedef struct SCCBHeader { } __attribute__((packed)) SCCBHeader;#define SCCB_DATA_LEN (SCCB_SIZE - sizeof(SCCBHeader)) +#define SCCB_FAC134_DIAG320_BIT 0x4 +#define SCCB_FAC_IPL_SIPL_BIT 0x4000 typedef struct ReadInfo { SCCBHeader h;@@ -57,6 +59,10 @@ typedef struct ReadInfo {uint8_t rnsize;uint8_t reserved[13 ];uint8_t loadparm[LOADPARM_LEN ]; +uint8_t reserved1[102 ]; +uint8_t fac134; +uint8_t reserved2; +uint16_t fac_ipl; } __attribute__((packed)) ReadInfo; typedef struct SCCB {pc-bios/s390-ccw/secure-ipl.c new file +29
-0@@ -0,0 +1,383 @@ +/* +* S/390 Secure IPL +* +* Functions to support IPL in secure boot mode (DIAG 320, DIAG 508, +* signature verification, and certificate handling). +* +* For secure IPL overview: docs/system/s390x/secure-ipl.rst +* For secure IPL technical: docs/specs/s390x-secure-ipl.rst +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include "bootmap.h" +#include "s390-ccw.h" +#include "secure-ipl.h" + +uint8_t vcssb_data[VCSSB_MIN_LEN] __attribute__((__aligned__(PAGE_SIZE))); + +VCStorageSizeBlock *zipl_secure_get_vcssb (void ) +{ +VCStorageSizeBlock *vcssb; +int rc; +pc-bios/s390-ccw/secure-ipl.c line 39
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+if (!sclp_is_diag320_on () || !is_cert_store_facility_supported ()) { +puts ("Certificate Store Facility is not supported by the hypervisor!" ); +return NULL ; + } + + vcssb = (VCStorageSizeBlock *)vcssb_data; +/* avoid retrieving vcssb multiple times */ +if (vcssb->length >=VCSSB_MIN_LEN ) { +return vcssb; + }Change the order of operations here to check if vcssb has been retrieved before checking diag320 stuff. Otherwise, every time
zipl_secure_get_vcssb()is invoked, both an sclp service call and a diag320 call is always performed, which is unnecessary if the VCSSB has already been stored.pc-bios/s390-ccw/secure-ipl.c line 45
Quoted 6 lines
pc-bios/s390-ccw/secure-ipl.c new file +6
-0+ + vcssb->length =VCSSB_MIN_LEN ; + rc =diag320 (vcssb,DIAG_320_SUBC_QUERY_VCSI ); +if (rc !=DIAG_320_RC_OK ) { +return NULL ; + }Can do if (
diag320(vcssb, DIAG_320_SUBC_QUERY_VCSI)!=DIAG_320_RC_OK)and then get rid of rc
pc-bios/s390-ccw/secure-ipl.c line 50
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c new file +5
-0+ +return vcssb; +} + +static uint32_t get_certs_length (void )The function name is ambiguous. I had to digest the math below to understand the intention. How about something like...
get_total_certs_length()...to help clarify that this function is returning a value to store all certs.
pc-bios/s390-ccw/secure-ipl.c line 61
Quoted 11 lines
pc-bios/s390-ccw/secure-ipl.c new file +11
-0+{ +VCStorageSizeBlock *vcssb; +uint32_t len; + + vcssb =zipl_secure_get_vcssb (); +if (vcssb ==NULL ) { +return 0 ; + } + + len = vcssb->total_vcb_len -VCB_HEADER_LEN - vcssb->total_vc_ct *VCE_HEADER_LEN ; +Could return the line above and get rid of len.
Quoted 17 lines
pc-bios/s390-ccw/secure-ipl.c new file +17
-0+return len; +} + +static uint32_t request_certificate (uint8_t *cert,uint8_t index) +{ +VCStorageSizeBlock *vcssb; +VCBlock *vcb; +VCEntry *vce; +uint64_t rc =0 ; +uint32_t cert_len =0 ; +uint32_t max_single_vcb_len; + +/* Get Verification Certificate Storage Size block with DIAG320 subcode 1 */ + vcssb =zipl_secure_get_vcssb (); +if (vcssb ==NULL ) { +return 0 ; + }pc-bios/s390-ccw/secure-ipl.c line 88
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+ +/* +* Request single entry +* Fill input fields of single-entry VCB +*/ + max_single_vcb_len =ROUND_UP (vcssb->max_single_vcb_len,PAGE_SIZE ); + vcb =malloc (max_single_vcb_len); + vcb->in_len = max_single_vcb_len; + vcb->first_vc_index = index +1 ; + vcb->last_vc_index = index +1 ;There needs to be a better way to account for the index origin differences. See my comments below for
verify_signature().pc-bios/s390-ccw/secure-ipl.c line 93
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c new file +5
-0+ + rc =diag320 (vcb,DIAG_320_SUBC_STORE_VC ); +if (rc !=DIAG_320_RC_OK ) { + goto out; + }if (!diag320(vcb,
DIAG_320_SUBC_STORE_VC))then get rid of rc
Quoted 16 lines
pc-bios/s390-ccw/secure-ipl.c new file +16
-0+ +if (vcb->out_len ==VCB_HEADER_LEN ) { +puts ("No certificate entry" ); + goto out; + } +if (vcb->remain_ct !=0 ) { +puts ("Not enough memory to store all requested certificates" ); + goto out; + } + + vce = (VCEntry *)vcb->vce_buf; +if (!(vce->flags &DIAG_320_VCE_FLAGS_VALID )) { +puts ("Invalid certificate" ); + goto out; + } +pc-bios/s390-ccw/secure-ipl.c line 119
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+ cert_len = vce->cert_len; +memcpy (cert, (uint8_t *)vce + vce->cert_offset, vce->cert_len); + +out: +free (vcb); +return cert_len; +} + +static void cert_list_add (IplSignatureCertificateList *certs,int cert_index, +uint8_t *cert,uint64_t cert_len)s/certs/cert_list
s/cert/cert_addrQuoted 5 lines
pc-bios/s390-ccw/secure-ipl.c new file +5
-0+{ +if (cert_index >MAX_CERTIFICATES -1 ) { +printf ("Warning: Ignoring cert entry [%d] because it's over %d entires\n" , + cert_index +1 ,MAX_CERTIFICATES ); +return ;pc-bios/s390-ccw/secure-ipl.c line 134
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+ } + + certs->cert_entries[cert_index].addr = (uint64_t )cert; + certs->cert_entries[cert_index].len = cert_len; + certs->ipl_info_header.len +=sizeof (certs->cert_entries[cert_index]); +} + +static void comp_list_add (IplDeviceComponentList *comps,int comp_index, +int cert_index,uint64_t comp_addr, +uint64_t comp_len,uint8_t flags)s/comps/comp_list
Quoted 24 lines
pc-bios/s390-ccw/secure-ipl.c new file +24
-0+{ +if (comp_index >MAX_CERTIFICATES -1 ) { +printf ("Warning: Ignoring comp entry [%d] because it's over %d entires\n" , + comp_index +1 ,MAX_CERTIFICATES ); +return ; + } + + comps->device_entries[comp_index].addr = comp_addr; + comps->device_entries[comp_index].len = comp_len; + comps->device_entries[comp_index].flags = flags; + comps->device_entries[comp_index].cert_index = cert_index; + comps->ipl_info_header.len +=sizeof (comps->device_entries[comp_index]); +} + +static int update_iirb (IplDeviceComponentList *comps,IplSignatureCertificateList *certs) +{ +IplInfoReportBlock *iirb; +IplDeviceComponentList *iirb_comps; +IplSignatureCertificateList *iirb_certs; +uint32_t iirb_hdr_len; +uint32_t comps_len; +uint32_t certs_len; + +if (iplb->len %8 !=0 ) {pc-bios/s390-ccw/secure-ipl.c line 168
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+panic ("IPL parameter block length field value is not multiple of 8 bytes" ); + } + + iirb_hdr_len =sizeof (IplInfoReportBlockHeader); + comps_len = comps->ipl_info_header.len; + certs_len = certs->ipl_info_header.len; +if ((comps_len + certs_len + iirb_hdr_len) >sizeof (IplInfoReportBlock)) { +puts ("Not enough space to hold all components and certificates in IIRB" ); +return -1 ; + }Shouldn't this also be a panic? If there's not enough space for the IIRB, then that should be more serious than just warning in audit mode.
Quoted 44 lines
pc-bios/s390-ccw/secure-ipl.c new file +44
-0+ +/* IIRB immediately follows IPLB */ + iirb = &ipl_data.iirb; + iirb->hdr.len = iirb_hdr_len; + +/* Copy IPL device component list after IIRB Header */ + iirb_comps = (IplDeviceComponentList *) iirb->info_blks; +memcpy (iirb_comps, comps, comps_len); + +/* Update IIRB length */ + iirb->hdr.len += comps_len; + +/* Copy IPL sig cert list after IPL device component list */ + iirb_certs = (IplSignatureCertificateList *) (iirb->info_blks + + iirb_comps->ipl_info_header.len); +memcpy (iirb_certs, certs, certs_len); + +/* Update IIRB length */ + iirb->hdr.len += certs_len; + +return 0 ; +} + +static bool secure_ipl_supported (void ) +{ +if (!sclp_is_sipl_on ()) { +puts ("Secure IPL Facility is not supported by the hypervisor!" ); +return false; + } + +if (!is_secure_ipl_extension_supported ()) { +puts ("Secure IPL extensions are not supported by the hypervisor!" ); +return false; + } + +if (!sclp_is_diag320_on () || !is_cert_store_facility_supported ()) { +puts ("Certificate Store Facility is not supported by the hypervisor!" ); +return false; + } + +return true; +} + +static void init_lists (IplDeviceComponentList *comps,IplSignatureCertificateList *certs)pc-bios/s390-ccw/secure-ipl.c line 222
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+{ + comps->ipl_info_header.ibt =IPL_IBT_COMPONENTS ; + comps->ipl_info_header.len =sizeof (comps->ipl_info_header); + + certs->ipl_info_header.ibt =IPL_IBT_CERTIFICATES ; + certs->ipl_info_header.len =sizeof (certs->ipl_info_header); +} + +static uint32_t zipl_load_signature (ComponentEntry *entry,uint64_t sig_sec) +{Needs to be type int, otherwise returning -1 below doesn't make sense.
pc-bios/s390-ccw/secure-ipl.c line 235
Quoted 13 lines
pc-bios/s390-ccw/secure-ipl.c new file +13
-0+uint32_t sig_len; + +if (zipl_load_segment (entry, sig_sec) <0 ) { +return -1 ; + } + +if (entry->compdat.sig_info.format !=DER_SIGNATURE_FORMAT ) { +puts ("Signature is not in DER format" ); +return -1 ; + } + sig_len = entry->compdat.sig_info.sig_len; + +return sig_len;Could return
entry->compdat.sig_info.sig_len, thensig_lengoes away.Quoted 19 lines
pc-bios/s390-ccw/secure-ipl.c new file +19
-0+} + +/* +* Returns: 1 - New certificate requested and added to cert_table and cert list +* 0 - Certificate already exists in the cert_table +* -1 - Error while requesting certificate +*/ +static int handle_certificate (int *cert_table,uint8_t **cert, +uint64_t cert_len,uint8_t cert_table_idx, +IplSignatureCertificateList *certs,int cert_entry_idx) +{ +if (cert_table[cert_table_idx] !=-1 ) { +return 0 ; + } + +if (!request_certificate (*cert, cert_table_idx)) { +puts ("Could not get certificate" ); +return -1 ; + }pc-bios/s390-ccw/secure-ipl.c line 264
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+ +cert_list_add (certs, cert_entry_idx, *cert, cert_len); + cert_table[cert_table_idx] = cert_entry_idx; +return 1 ; +} + +int zipl_run_secure (ComponentEntry **entry_ptr,uint8_t *tmp_sec) +{ +IplDeviceComponentList comps; +IplSignatureCertificateList certs;It would read a little easier if these were
comp_listandcert_listQuoted 11 lines
pc-bios/s390-ccw/secure-ipl.c new file +11
-0+ComponentEntry *entry = *entry_ptr; +uint8_t *cert =NULL ; +uint64_t *sig =NULL ; +int cert_entry_idx =0 ; +int comp_entry_idx =0 ; +uint64_t comp_addr; +int comp_len; +uint32_t sig_len =0 ; +uint64_t cert_len =-1 ; +uint8_t cert_table_idx =-1 ; +bool verified;pc-bios/s390-ccw/secure-ipl.c line 285
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+uint32_t certs_len; +/* +* Store indices of cert entry that have already used for signature +* verification to prevent allocating the same certificate multiple times. +* cert_table index (cert_table_idx): +* index of certificate from qemu cert store used for verification +* cert_table value (cert_entry_idx): +* index of cert entry in cert list that contains the certificate +*/ +int cert_table[MAX_CERTIFICATES ] = { [0 ...MAX_CERTIFICATES -1 ] =-1 };
Quoted 8 lines
If you agree with the suggestion below for
verify_signatureto adjust the returned index, then thecert_tablecan be used a bit differently:Make this an array of booleans. Use the index returned by
verify_signature. Value true means that the cert has already been added to the IplSignatureCertificateList. False means it has yet to be added to the list.
If verify_signature adjusted the returned index, I don’t quite see why cert_table should become a boolean array. We still need to track the certificate index in IplSignatureCertificateList, since that index is used later as the IPL-Signature-Certificate Index (SCI) in IplDeviceComponentList. That’s what cert_table currently provides.
With a boolean array, that index information would be lost. Am I missing something?
Quoted 254 lines
Remove the mention of "qemu cert store", as the BIOS technically does not know anything about the underlying cert store or its implementation.
pc-bios/s390-ccw/secure-ipl.c line 295
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+int rc; +int signed_count =0 ; + +if (!secure_ipl_supported ()) { +return -1 ; + } + +init_lists (&comps, &certs); + certs_len =get_certs_length (); + cert =malloc (certs_len);cert = malloc(get_total_certs_length());
Then get rid of the
certs_lenvariable.Quoted 21 lines
pc-bios/s390-ccw/secure-ipl.c new file +21
-0+ sig =malloc (MAX_SECTOR_SIZE ); + +while (entry->component_type !=ZIPL_COMP_ENTRY_EXEC ) { +switch (entry->component_type) { +case ZIPL_COMP_ENTRY_SIGNATURE : +if (sig_len) { + goto out; + } + + sig_len =zipl_load_signature (entry, (uint64_t )sig); +if (sig_len <0 ) { + goto out; + } +break ; +case ZIPL_COMP_ENTRY_LOAD : + comp_addr = entry->compdat.load_addr; + comp_len =zipl_load_segment (entry, comp_addr); +if (comp_len <0 ) { + goto out; + } +pc-bios/s390-ccw/secure-ipl.c line 326
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+if (!sig_len) { +break ; + } + + verified =verify_signature (comp_len, comp_addr, sig_len, (uint64_t )sig, + &cert_len, &cert_table_idx); + +if (verified) { + rc =handle_certificate (cert_table, &cert, cert_len, + cert_table_idx, &certs, cert_entry_idx);Check
cert_tablehere before callinghandle_certificate. It would contain all thecert_tablelogic tozipl_run_secureand make it a little bit easier to understand. Also less variables to pass around.Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+if (rc ==-1 ) { + goto out; + } + +/* increment for the next certificate */ +if (rc ==1 ) { + cert_entry_idx++; + cert += cert_len; + } +pc-bios/s390-ccw/secure-ipl.c line 346
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c new file +10
-0+puts ("Verified component" ); +comp_list_add (&comps, comp_entry_idx, cert_table[cert_table_idx], + comp_addr, comp_len, +S390_IPL_COMPONENT_FLAG_SC |S390_IPL_COMPONENT_FLAG_CSV ); + }else { +comp_list_add (&comps, comp_entry_idx,-1 , + comp_addr, comp_len, +S390_IPL_COMPONENT_FLAG_SC ); +zipl_secure_handle ("Could not verify component" ); + }This can be cleaned up a bit. Set
cert_indexand flags within the if/else block. Callcomp_list_add()once after the block.Quoted 52 lines
pc-bios/s390-ccw/secure-ipl.c new file +37
-0+ + comp_entry_idx++; + signed_count +=1 ; +/* After a signature is used another new one can be accepted */ + sig_len =0 ; +break ; +default : +puts ("Unknown component entry type" ); +return -1 ; + } + + entry++; + +if ((uint8_t *)(&entry[1 ]) > tmp_sec +MAX_SECTOR_SIZE ) { +puts ("Wrong entry value" ); +return -EINVAL ; + } + } + +if (signed_count ==0 ) { +zipl_secure_handle ("Secure boot is on, but components are not signed" ); + } + +if (update_iirb (&comps, &certs)) { +zipl_secure_handle ("Failed to write IPL Information Report Block" ); + } + + *entry_ptr = entry; +free (sig); + +return 0 ; +out: +free (cert); +free (sig); + +return -1 ; +}pc-bios/s390-ccw/secure-ipl.h new file +9
-0@@ -0,0 +1,94 @@ +/* +* S/390 Secure IPL +* +* Copyright 2025 IBM Corp. +* Author(s): Zhuoying Cai <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +pc-bios/s390-ccw/secure-ipl.h line 19
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.h new file +10
-0+#ifndef _PC_BIOS_S390_CCW_SECURE_IPL_H +#define _PC_BIOS_S390_CCW_SECURE_IPL_H + +#include <diag320.h> +#include <diag508.h> + +VCStorageSizeBlock *zipl_secure_get_vcssb (void ); +int zipl_run_secure (ComponentEntry **entry_ptr,uint8_t *tmp_sec); + +static inline void zipl_secure_handle (const char *message)This could be implemented like an assert, where the first param is the condition.
e.g.
`zipl_secure_handle(signed_count > 0, "Secure boot is on, but components
are not signed");`Then some if checks / extra indentation goes away.
Quoted 21 lines
pc-bios/s390-ccw/secure-ipl.h new file +21
-0+{ +switch (boot_mode) { +case ZIPL_BOOT_MODE_SECURE_AUDIT : +IPL_check (false, message); +break ; +default : +break ; + } +} + +static inline uint64_t diag320 (void *data,unsigned long subcode) +{ + register unsigned long addr asm("0") = (unsigned long)data; + register unsigned long rc asm("1") = 0; + + asmvolatile ("diag %0,%2,0x320\n" + :"+d" (addr),"+d" (rc) + :"d" (subcode) + :"memory" ,"cc" ); +return rc; +}pc-bios/s390-ccw/secure-ipl.h line 50
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.h new file +10
-0+ +static inline bool is_cert_store_facility_supported (void ) +{ +uint32_t d320_ism; + +diag320 (&d320_ism,DIAG_320_SUBC_QUERY_ISM ); +return (d320_ism &DIAG_320_ISM_QUERY_SUBCODES ) && + (d320_ism &DIAG_320_ISM_QUERY_VCSI ) && + (d320_ism &DIAG_320_ISM_STORE_VC ); +}Don't need to check if
DIAG_320_ISM_QUERY_SUBCODESis supported (implied if query ISM returns OK).Can clean this up by doing
return d320_ism & (DIAG_320_ISM_QUERY_VCSI | DIAG_320_ISM_STORE_VC);Merge this function with
sclp_is_diag320_on(), checking the read scp facility bit first before calling diag320.Quoted 4 lines
pc-bios/s390-ccw/secure-ipl.h new file +4
-0+ +static inline uint64_t _diag508 (void *data,unsigned long subcode) +{ + register unsigned long addr asm("0") = (unsigned long)data;pc-bios/s390-ccw/secure-ipl.h line 64
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.h new file +10
-0+ register unsigned long rc asm("1") = 0; + + asmvolatile ("diag %0,%2,0x508\n" + :"+d" (addr),"+d" (rc) + :"d" (subcode) + :"memory" ,"cc" ); +return rc; +} + +static inline bool is_secure_ipl_extension_supported (void )"
is_signature_verif_supported()" or something like that reads better.Quoted 13 lines
pc-bios/s390-ccw/secure-ipl.h new file +13
-0+{ +uint64_t d508_subcodes; + + d508_subcodes =_diag508 (NULL ,DIAG_508_SUBC_QUERY_SUBC ); +return d508_subcodes &DIAG_508_SUBC_SIG_VERIF ; +} + +static inline bool verify_signature (uint64_t comp_len,uint64_t comp_addr, +uint64_t sig_len,uint64_t sig_addr, +uint64_t *cert_len,uint8_t *cert_idx) +{ +Diag508SigVerifBlock svb; +pc-bios/s390-ccw/secure-ipl.h line 87
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.h new file +10
-0+ svb.length =sizeof (Diag508SigVerifBlock); + svb.version =0 ; + svb.comp_len = comp_len; + svb.comp_addr = comp_addr; + svb.sig_len = sig_len; + svb.sig_addr = sig_addr; + +if (_diag508 (&svb,DIAG_508_SUBC_SIG_VERIF ) ==DIAG_508_RC_OK ) { + *cert_len = svb.cert_len; + *cert_idx = svb.cert_store_index;Adjust for different indexing origins here, accounting for what DIAG320 will later need when retrieving certs:
``` /* * DIAG 508 utilizes an index origin of 0 when indexing the cert store. * The cert_idx will be used for DIAG 320 data structures, which expects * an index origin of 1. Account for the offset here so it's easier to * manage later. */ *cert_idx = svb.cert_store_index + 1; ```Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.h new file +7
-0+ return true; + } + +return false; +} + +#endif /* _PC_BIOS_S390_CCW_SECURE_IPL_H */
Re: [PATCH v7 19/29] pc-bios/s390-ccw: Add signature verification for secure IPL in audit mode
Quoted 17 lines · Zhuoying Cai
Quoted 9 lines
pc-bios/s390-ccw/secure-ipl.c line 285
Quoted 9 lines
pc-bios/s390-ccw/secure-ipl.c new file +9
-0+/* +* Store indices of cert entry that have already used for signature +* verification to prevent allocating the same certificate multiple times. +* cert_table index (cert_table_idx): +* index of certificate from qemu cert store used for verification +* cert_table value (cert_entry_idx): +* index of cert entry in cert list that contains the certificate +*/ +int cert_table[MAX_CERTIFICATES ] = { [0 ...MAX_CERTIFICATES -1 ] =-1 };Quoted 8 lines
If you agree with the suggestion below for
verify_signatureto adjust the returned index, then thecert_tablecan be used a bit differently:Make this an array of booleans. Use the index returned by
verify_signature. Value true means that the cert has already been added to the IplSignatureCertificateList. False means it has yet to be added to the list.
Quoted 8 lines
If verify_signature adjusted the returned index, I don’t quite see why
cert_tableshould become a boolean array. We still need to track the certificate index in IplSignatureCertificateList, since that index is used later as the IPL-Signature-Certificate Index (SCI) in IplDeviceComponentList. That’s whatcert_tablecurrently provides.With a boolean array, that index information would be lost. Am I missing something?
Ah, I see. This is to keep track of which cert store index corresponds to the entry in the IplSignatureCertificateList. I thought it was to **only** keep track of which cert was already retrieved from the store, so my suggestion was off. Thanks for clearing that up.
I might've been confused by the comment. What about:
``` /* * Keep track of which certificate store indices correspond to the * certificate data entries within the IplSignatureCertificateList to * prevent allocating space for the same certificate multiple times. * * The array index corresponds to the certificate's cert-store index. * * The array value corresponds to the certificate's entry within the * IplSignatureCertificateList (with a value of -1 denoting no entry * exists for the certificate). */ ```
It's just a more verbose explanation of what you already have, but the main point is to correlate where the index value comes from and which data structure the array assists.
If you take the suggestion to rename the IplSignatureCertificateList variable from certs to cert_list, then I would also suggest renaming cert_table to something like cert_list_table so it will read a little more clearly when viewed next to cert_list.
Signature · 2 lines
Regards, Collin
PATCH v7 20/29 s390x: Guest support for Secure-IPL Code Loading Attributes Facility (SCLAF)
In reply to Zhuoying CaiThe secure-IPL-code-loading-attributes facility (SCLAF) provides additional security during secure IPL.
Availability of SCLAF is determined by byte 136 bit 3 of the SCLP Read Info block.
This feature is available starting with the gen16 CPU model.
--- docs/specs/s390x-secure-ipl.rst | 19 +++++++++++++++++++ target/s390x/cpu_features.c | 2 ++ target/s390x/cpu_features_def.h.inc | 1 + target/s390x/cpu_models.c | 3 +++ target/s390x/gen-features.c | 2 ++ target/s390x/kvm/kvm.c | 1 + 6 files changed, 28 insertions(+)
docs/specs/s390x-secure-ipl.rst +19 -0
@@ -99,3 +99,22 @@ operations such as: * certificate data The guest kernel will inspect the IIRB and build the keyring. + + +Secure Code Loading Attributes Facility +--------------------------------- + +The Secure Code Loading Attributes Facility (SCLAF) enhances system security +during the IPL by enforcing additional verification rules. + +When SCLAF is available, its behavior depends on the IPL mode. It introduces +verification of both signed and unsigned components to help ensure that only +authorized code is loaded during the IPL process. Any errors detected by SCLAF +are reported in the IIRB. + +Unsigned components are restricted to load addresses at or above absolute +storage address ``0x2000``. + +Signed components must include a Secure Code Loading Attribute Block (SCLAB), +which is appended at the very end of the component. The SCLAB defines security +attributes for handling the signed code.
target/s390x/cpu_features.c +2 -0
@@ -120,6 +120,7 @@ void s390_fill_feat_block(const S390FeatBitmap features, S390FeatType type,* - All SIE facilities because SIE is not available * - DIAG318 * - Secure IPL Facility +* - Secure IPL Code Loading Attributes Facility * * As VMs can move in and out of protected mode the CPU model * doesn't protect us from that problem because it is only
@@ -152,6 +153,7 @@ void s390_fill_feat_block(const S390FeatBitmap features, S390FeatType type,break ;case S390_FEAT_TYPE_SCLP_FAC_IPL :clear_be_bit (s390_feat_def (S390_FEAT_SIPL )->bit, data); +clear_be_bit (s390_feat_def (S390_FEAT_SCLAF )->bit, data);break ;default :return ;
target/s390x/cpu_features_def.h.inc +1 -0
@@ -142,6 +142,7 @@ DEF_FEAT(CERT_STORE, "cstore", SCLP_FAC134, 5, "Provide Certificate Store functi /* Features exposed via SCLP SCCB Facilities byte 136 - 137 (bit numbers relative to byte-136) */ DEF_FEAT(SIPL, "sipl", SCLP_FAC_IPL, 1, "Secure-IPL facility") +DEF_FEAT(SCLAF, "sclaf", SCLP_FAC_IPL, 3, "Secure-IPL-code-loading-attributes facility") /* Features exposed via SCLP CPU info. */ DEF_FEAT(SIE_F2, "sief2", SCLP_CPU, 4, "SIE: interception format 2 (Virtual SIE)")
target/s390x/cpu_models.c +3 -0
@@ -264,6 +264,7 @@ bool s390_has_feat(S390Feat feat)case S390_FEAT_SIE_PFMFI :case S390_FEAT_SIE_IBS :case S390_FEAT_SIPL : +case S390_FEAT_SCLAF :case S390_FEAT_CONFIGURATION_TOPOLOGY :return false;break ;
@@ -509,6 +510,8 @@ static void check_consistency(const S390CPUModel *model) { S390_FEAT_DIAG_318, S390_FEAT_EXTENDED_LENGTH_SCCB }, { S390_FEAT_CERT_STORE, S390_FEAT_EXTENDED_LENGTH_SCCB }, { S390_FEAT_SIPL, S390_FEAT_EXTENDED_LENGTH_SCCB }, + { S390_FEAT_SCLAF, S390_FEAT_EXTENDED_LENGTH_SCCB }, + { S390_FEAT_SCLAF, S390_FEAT_SIPL }, { S390_FEAT_NNPA, S390_FEAT_VECTOR }, { S390_FEAT_RDP, S390_FEAT_LOCAL_TLB_CLEARING }, { S390_FEAT_UV_FEAT_AP, S390_FEAT_AP },
target/s390x/gen-features.c +2 -0
@@ -722,6 +722,7 @@ static uint16_t full_GEN16_GA1[] = {S390_FEAT_UV_FEAT_AP_INTR ,S390_FEAT_CERT_STORE ,S390_FEAT_SIPL , + S390_FEAT_SCLAF, }; static uint16_t full_GEN17_GA1[] = {
@@ -924,6 +925,7 @@ static uint16_t qemu_MAX[] = {S390_FEAT_EXTENDED_LENGTH_SCCB ,S390_FEAT_CERT_STORE ,S390_FEAT_SIPL , + S390_FEAT_SCLAF, };/****** END FEATURE DEFS ******/
target/s390x/kvm/kvm.c +1 -0
@@ -2522,6 +2522,7 @@ bool kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp)/* Some Secure IPL facilities are emulated by QEMU */ set_bit (S390_FEAT_SIPL , model->features); +set_bit (S390_FEAT_SCLAF , model->features);/* Test for Ultravisor features that influence secure guest behavior */ query_uv_feat_guest (model->features);
Signature · 1 line
2.51.1
PATCH v7 21/29 pc-bios/s390-ccw: Add additional security checks for secure boot
In reply to Zhuoying CaiAdd additional checks to ensure that components do not overlap with signed components when loaded into memory.
Add additional checks to ensure the load addresses of unsigned components are greater than or equal to 0x2000.
When the secure IPL code loading attributes facility (SCLAF) is installed, all signed components must contain a secure code loading attributes block (SCLAB).
The SCLAB provides further validation of information on where to load the signed binary code from the load device, and where to start the execution of the loaded OS code.
When SCLAF is installed, its content must be evaluated during secure IPL. However, a missing SCLAB will not be reported in audit mode. The SCALB checking will be skipped in this case.
Add IPL Information Error Indicators (IIEI) and Component Error Indicators (CEI) for IPL Information Report Block (IIRB).
When SCLAF is installed, additional secure boot checks are performed during zipl and store results of verification into IIRB.
--- pc-bios/s390-ccw/iplb.h | 26 ++- pc-bios/s390-ccw/s390-ccw.h | 1 + pc-bios/s390-ccw/sclp.c | 8 + pc-bios/s390-ccw/sclp.h | 1 + pc-bios/s390-ccw/secure-ipl.c | 422 +++++++++++++++++++++++++++++++++- pc-bios/s390-ccw/secure-ipl.h | 55 +++++ 6 files changed, 508 insertions(+), 5 deletions(-)
pc-bios/s390-ccw/iplb.h +24 -2
@@ -32,11 +32,19 @@ struct IplInfoReportBlockHeader { };typedef struct IplInfoReportBlockHeader IplInfoReportBlockHeader ; +#define S390_IPL_INFO_IIEI_NO_SIGNED_COMP 0x8000/* bit 0 */ +#define S390_IPL_INFO_IIEI_NO_SCLAB 0x4000/* bit 1 */ +#define S390_IPL_INFO_IIEI_NO_GLOBAL_SCLAB 0x2000/* bit 2 */ +#define S390_IPL_INFO_IIEI_MORE_GLOBAL_SCLAB 0x1000/* bit 3 */ +#define S390_IPL_INFO_IIEI_FOUND_UNSIGNED_COMP 0x800/* bit 4 */ +#define S390_IPL_INFO_IIEI_MORE_SIGNED_COMP 0x400/* bit 5 */ +struct IplInfoBlockHeader {uint32_t len;uint8_t ibt;uint8_t reserved1[3 ]; -uint8_t reserved2[8 ]; +uint16_t iiei; +uint8_t reserved2[6 ]; };typedef struct IplInfoBlockHeader IplInfoBlockHeader ;
@@ -60,13 +68,27 @@ typedef struct IplSignatureCertificateList IplSignatureCertificateList;#define S390_IPL_COMPONENT_FLAG_SC 0x80#define S390_IPL_COMPONENT_FLAG_CSV 0x40 +#define S390_IPL_COMPONENT_CEI_INVALID_SCLAB 0x80000000/* bit 0 */ +#define S390_IPL_COMPONENT_CEI_INVALID_SCLAB_LEN 0x40000000/* bit 1 */ +#define S390_IPL_COMPONENT_CEI_INVALID_SCLAB_FORMAT 0x20000000/* bit 2 */ +#define S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_ADDR 0x10000000/* bit 3 */ +#define S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_PSW 0x8000000/* bit 4 */ +#define S390_IPL_COMPONENT_CEI_INVALID_LOAD_PSW 0x4000000/* bit 5 */ +#define S390_IPL_COMPONENT_CEI_NUC_NOT_IN_GLOBAL_SCLA 0x2000000/* bit 6 */ +#define S390_IPL_COMPONENT_CEI_SCLAB_OLA_NOT_ONE 0x1000000/* bit 7 */ +#define S390_IPL_COMPONENT_CEI_SC_NOT_IN_GLOBAL_SCLAB 0x800000/* bit 8 */ +#define S390_IPL_COMPONENT_CEI_SCLAB_LOAD_ADDR_NOT_ZERO 0x400000/* bit 9 */ +#define S390_IPL_COMPONENT_CEI_SCLAB_LOAD_PSW_NOT_ZERO 0x200000/* bit 10 */ +#define S390_IPL_COMPONENT_CEI_INVALID_UNSIGNED_ADDR 0x100000/* bit 11 */ +struct IplDeviceComponentEntry {uint64_t addr;uint64_t len;uint8_t flags;uint8_t reserved1[5 ];uint16_t cert_index; -uint8_t reserved2[8 ]; +uint32_t cei; +uint8_t reserved2[4 ]; };typedef struct IplDeviceComponentEntry IplDeviceComponentEntry ;
pc-bios/s390-ccw/s390-ccw.h +1 -0
@@ -69,6 +69,7 @@ void sclp_setup(void);void sclp_get_loadparm_ascii (char *loadparm);bool sclp_is_diag320_on (void );bool sclp_is_sipl_on (void ); +bool sclp_is_sclaf_on (void );int sclp_read (char *str,size_t count);/* virtio.c */
pc-bios/s390-ccw/sclp.c +8 -0
@@ -157,6 +157,14 @@ bool sclp_is_sipl_on(void)return fac_ipl &SCCB_FAC_IPL_SIPL_BIT ; } +bool sclp_is_sclaf_on (void ) +{ +uint16_t fac_ipl =0 ; + +sclp_get_fac_ipl (&fac_ipl); +return fac_ipl &SCCB_FAC_IPL_SCLAF_BIT ; +} +int sclp_read (char *str,size_t count) {ReadEventData *sccb = (void *)_sccb;
pc-bios/s390-ccw/sclp.h +1 -0
@@ -52,6 +52,7 @@ typedef struct SCCBHeader {#define SCCB_DATA_LEN (SCCB_SIZE - sizeof(SCCBHeader))#define SCCB_FAC134_DIAG320_BIT 0x4#define SCCB_FAC_IPL_SIPL_BIT 0x4000 +#define SCCB_FAC_IPL_SCLAF_BIT 0x1000 typedef struct ReadInfo { SCCBHeader h;
pc-bios/s390-ccw/secure-ipl.c +419 -3
457 lines not on this page · raw message
pc-bios/s390-ccw/secure-ipl.h +55 -0
67 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v7 21/29] pc-bios/s390-ccw: Add additional security checks for secure boot
Quoted 26 lines · Zhuoying Cai
Add additional checks to ensure that components do not overlap with signed components when loaded into memory.
Add additional checks to ensure the load addresses of unsigned components are greater than or equal to
0x2000.When the secure IPL code loading attributes facility (SCLAF) is installed, all signed components must contain a secure code loading attributes block (SCLAB).
The SCLAB provides further validation of information on where to load the signed binary code from the load device, and where to start the execution of the loaded OS code.
When SCLAF is installed, its content must be evaluated during secure IPL. However, a missing SCLAB will not be reported in audit mode. The SCALB checking will be skipped in this case.
Add IPL Information Error Indicators (IIEI) and Component Error Indicators (CEI) for IPL Information Report Block (IIRB).
When SCLAF is installed, additional secure boot checks are performed during zipl and store results of verification into IIRB.
Signed-off-by Zhuoying Cai <[email protected]>---
...
Quoted 19 lines
pc-bios/s390-ccw/secure-ipl.c +6
-0@@ -206,6 +206,12 @@ static bool secure_ipl_supported(void)return false; } +if (!sclp_is_sclaf_on ()) { +puts ("Secure IPL Code Loading Attributes Facility is not supported by" +" the hypervisor!" ); +return false; + } +return true; }@@ -218,6 +224,402 @@ static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis certs->ipl_info_header.len =sizeof (certs->ipl_info_header);
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 234
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +8
-0} +static bool is_comp_overlap (SecureIplCompAddrRange *comp_addr_range, +int addr_range_index, +uint64_t start_addr,uint64_t end_addr) +{ +/* neither a signed nor an unsigned component can overlap with a signed component */ +for (int i =0 ; i < addr_range_index; i++) { + if ((comp_addr_range[i].start_addr <= end_addr - 1 && + start_addr <= comp_addr_range[i].end_addr - 1) &&
Instead of comparing with "<= end_addr - 1", please simply compare with "< end_addr" instead. That's easier to read and avoids bugs like wrong comparisons in case of end_addr accidentally being 0 (so you would get a wrap-around when subtracting 1).
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 247
Quoted 13 lines
pc-bios/s390-ccw/secure-ipl.c +13
-0+ comp_addr_range[i].is_signed) { +return true; + } + } + +return false; +} + +static void comp_addr_range_add (SecureIplCompAddrRange *comp_addr_range, +int addr_range_index,bool is_signed, +uint64_t start_addr,uint64_t end_addr) +{ +if (addr_range_index >MAX_CERTIFICATES -1 ) {
">= MAX_CERTIFICATES" without the "- 1"
Thomas
Re: [PATCH v7 21/29] pc-bios/s390-ccw: Add additional security checks for secure boot
In reply to Zhuoying CaiQuoted 39 lines · Zhuoying Cai
Add additional checks to ensure that components do not overlap with signed components when loaded into memory.
Add additional checks to ensure the load addresses of unsigned components are greater than or equal to
0x2000.When the secure IPL code loading attributes facility (SCLAF) is installed, all signed components must contain a secure code loading attributes block (SCLAB).
The SCLAB provides further validation of information on where to load the signed binary code from the load device, and where to start the execution of the loaded OS code.
When SCLAF is installed, its content must be evaluated during secure IPL. However, a missing SCLAB will not be reported in audit mode. The SCALB checking will be skipped in this case.
Add IPL Information Error Indicators (IIEI) and Component Error Indicators (CEI) for IPL Information Report Block (IIRB).
When SCLAF is installed, additional secure boot checks are performed during zipl and store results of verification into IIRB.
Signed-off-by Zhuoying Cai <[email protected]>--- pc-bios/s390-ccw/iplb.h | 26 ++- pc-bios/s390-ccw/s390-ccw.h | 1 + pc-bios/s390-ccw/sclp.c | 8 + pc-bios/s390-ccw/sclp.h | 1 + pc-bios/s390-ccw/secure-ipl.c | 422 +++++++++++++++++++++++++++++++++- pc-bios/s390-ccw/secure-ipl.h | 55 +++++ 6 files changed, 508 insertions(+), 5 deletions(-)pc-bios/s390-ccw/iplb.h +0
-0
pc-bios/s390-ccw/iplb.h struct IplInfoReportBlockHeader { · line 40
Quoted 9 lines
pc-bios/s390-ccw/iplb.h +6
-0@@ -32,11 +32,19 @@ struct IplInfoReportBlockHeader { };typedef struct IplInfoReportBlockHeader IplInfoReportBlockHeader ; +#define S390_IPL_INFO_IIEI_NO_SIGNED_COMP 0x8000/* bit 0 */ +#define S390_IPL_INFO_IIEI_NO_SCLAB 0x4000/* bit 1 */ +#define S390_IPL_INFO_IIEI_NO_GLOBAL_SCLAB 0x2000/* bit 2 */ +#define S390_IPL_INFO_IIEI_MORE_GLOBAL_SCLAB 0x1000/* bit 3 */ +#define S390_IPL_INFO_IIEI_FOUND_UNSIGNED_COMP 0x800/* bit 4 */ +#define S390_IPL_INFO_IIEI_MORE_SIGNED_COMP 0x400/* bit 5 */
What about shorthanding these to just S390_IIEI_*
Then add a comment above giving the longhand /* IPL Info Error Indicators */
Quoted 17 lines
pc-bios/s390-ccw/iplb.h +5
-1+struct IplInfoBlockHeader {uint32_t len;uint8_t ibt;uint8_t reserved1[3 ]; -uint8_t reserved2[8 ]; +uint16_t iiei; +uint8_t reserved2[6 ]; };typedef struct IplInfoBlockHeader IplInfoBlockHeader ;@@ -60,13 +68,27 @@ typedef struct IplSignatureCertificateList IplSignatureCertificateList;#define S390_IPL_COMPONENT_FLAG_SC 0x80#define S390_IPL_COMPONENT_FLAG_CSV 0x40 +#define S390_IPL_COMPONENT_CEI_INVALID_SCLAB 0x80000000/* bit 0 */ +#define S390_IPL_COMPONENT_CEI_INVALID_SCLAB_LEN 0x40000000/* bit 1 */
pc-bios/s390-ccw/iplb.h typedef struct IplSignatureCertificateList IplSignatureCertificateList; · line 82
Quoted 10 lines
pc-bios/s390-ccw/iplb.h +10
-0+#define S390_IPL_COMPONENT_CEI_INVALID_SCLAB_FORMAT 0x20000000/* bit 2 */ +#define S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_ADDR 0x10000000/* bit 3 */ +#define S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_PSW 0x8000000/* bit 4 */ +#define S390_IPL_COMPONENT_CEI_INVALID_LOAD_PSW 0x4000000/* bit 5 */ +#define S390_IPL_COMPONENT_CEI_NUC_NOT_IN_GLOBAL_SCLA 0x2000000/* bit 6 */ +#define S390_IPL_COMPONENT_CEI_SCLAB_OLA_NOT_ONE 0x1000000/* bit 7 */ +#define S390_IPL_COMPONENT_CEI_SC_NOT_IN_GLOBAL_SCLAB 0x800000/* bit 8 */ +#define S390_IPL_COMPONENT_CEI_SCLAB_LOAD_ADDR_NOT_ZERO 0x400000/* bit 9 */ +#define S390_IPL_COMPONENT_CEI_SCLAB_LOAD_PSW_NOT_ZERO 0x200000/* bit 10 */ +#define S390_IPL_COMPONENT_CEI_INVALID_UNSIGNED_ADDR 0x100000/* bit 11 */
Similar to above, what about S390_CEI_* and a comment.
Quoted 102 lines
pc-bios/s390-ccw/iplb.h +3
-1+struct IplDeviceComponentEntry {uint64_t addr;uint64_t len;uint8_t flags;uint8_t reserved1[5 ];uint16_t cert_index; -uint8_t reserved2[8 ]; +uint32_t cei; +uint8_t reserved2[4 ]; };typedef struct IplDeviceComponentEntry IplDeviceComponentEntry ;pc-bios/s390-ccw/s390-ccw.h +1
-0@@ -69,6 +69,7 @@ void sclp_setup(void);void sclp_get_loadparm_ascii (char *loadparm);bool sclp_is_diag320_on (void );bool sclp_is_sipl_on (void ); +bool sclp_is_sclaf_on (void );int sclp_read (char *str,size_t count);/* virtio.c */ pc-bios/s390-ccw/sclp.c +8
-0@@ -157,6 +157,14 @@ bool sclp_is_sipl_on(void)return fac_ipl &SCCB_FAC_IPL_SIPL_BIT ; } +bool sclp_is_sclaf_on (void ) +{ +uint16_t fac_ipl =0 ; + +sclp_get_fac_ipl (&fac_ipl); +return fac_ipl &SCCB_FAC_IPL_SCLAF_BIT ; +} +int sclp_read (char *str,size_t count) {ReadEventData *sccb = (void *)_sccb;pc-bios/s390-ccw/sclp.h +1
-0@@ -52,6 +52,7 @@ typedef struct SCCBHeader {#define SCCB_DATA_LEN (SCCB_SIZE - sizeof(SCCBHeader))#define SCCB_FAC134_DIAG320_BIT 0x4#define SCCB_FAC_IPL_SIPL_BIT 0x4000 +#define SCCB_FAC_IPL_SCLAF_BIT 0x1000 typedef struct ReadInfo { SCCBHeader h;pc-bios/s390-ccw/secure-ipl.c +31
-0@@ -206,6 +206,12 @@ static bool secure_ipl_supported(void)return false; } +if (!sclp_is_sclaf_on ()) { +puts ("Secure IPL Code Loading Attributes Facility is not supported by" +" the hypervisor!" ); +return false; + } +return true; }@@ -218,6 +224,402 @@ static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis certs->ipl_info_header.len =sizeof (certs->ipl_info_header); } +static bool is_comp_overlap (SecureIplCompAddrRange *comp_addr_range, +int addr_range_index, +uint64_t start_addr,uint64_t end_addr) +{ +/* neither a signed nor an unsigned component can overlap with a signed component */ +for (int i =0 ; i < addr_range_index; i++) { +if ((comp_addr_range[i].start_addr <= end_addr -1 && + start_addr <= comp_addr_range[i].end_addr -1 ) && + comp_addr_range[i].is_signed) { +return true; + } + } + +return false; +} + +static void comp_addr_range_add (SecureIplCompAddrRange *comp_addr_range, +int addr_range_index,bool is_signed, +uint64_t start_addr,uint64_t end_addr) +{ +if (addr_range_index >MAX_CERTIFICATES -1 ) { +printf ("Warning: Ignoring component address range index [%d]" +" because it's over %d index\n" , + addr_range_index,MAX_CERTIFICATES ); +return ;
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 261
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +10
-0+ } + + comp_addr_range[addr_range_index].is_signed = is_signed; + comp_addr_range[addr_range_index].start_addr = start_addr; + comp_addr_range[addr_range_index].end_addr = end_addr; +} + +static void check_unsigned_addr (uint64_t load_addr,IplDeviceComponentList *comps, +int comp_index) +{
Is load_addr the same as comps->device_entries[comp_index].addr? Also, can this function just be passed comps->device_entries[comp_index] instead?
Quoted 18 lines
pc-bios/s390-ccw/secure-ipl.c +18
-0+uint32_t flag; +bool valid; + +/* unsigned load address must be greater than or equal to 0x2000 */ + valid = load_addr >=0x2000 ; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_INVALID_UNSIGNED_ADDR ; +set_cei_with_log (comps, comp_index, flag, +"Load address is less than 0x2000" ); + } +} + +static void addr_overlap_check (SecureIplCompAddrRange *comp_addr_range, +int *addr_range_index, +uint64_t start_addr,uint64_t end_addr,bool is_signed) +{ +bool overlap; +
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 289
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +10
-0+ overlap =is_comp_overlap (comp_addr_range, *addr_range_index, + start_addr, end_addr); +if (!overlap) { +comp_addr_range_add (comp_addr_range, *addr_range_index, is_signed, + start_addr, end_addr); + *addr_range_index +=1 ; + }else { +zipl_secure_handle ("Component addresses overlap" ); + } +}
Is the address-range / comp-overlap stuff part of the SCLAF? Is it possible some of these checks are moved over to a separate patch so that this code can be isolated?
Quoted 13 lines
pc-bios/s390-ccw/secure-ipl.c +13
-0+ +static bool check_sclab_presence (uint8_t *sclab_magic, +IplDeviceComponentList *comps,int comp_index) +{ +/* identifies the presence of SCLAB */ +if (!magic_match (sclab_magic,ZIPL_MAGIC )) { + comps->device_entries[comp_index].cei |=S390_IPL_COMPONENT_CEI_INVALID_SCLAB ; + +/* a missing SCLAB will not be reported in audit mode */ +return false; + } + +return true;
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 312
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +10
-0+} + +static void check_sclab_length (uint16_t sclab_len, +IplDeviceComponentList *comps,int comp_index) +{ +uint32_t flag; +bool valid; + +/* minimum SCLAB length is 32 bytes */ + valid = sclab_len >=32 ;
#define this length somewhere and then get rid of the comment.
Quoted 183 lines
pc-bios/s390-ccw/secure-ipl.c +183
-0+if (!valid) { + flag =S390_IPL_COMPONENT_CEI_INVALID_SCLAB_LEN | +S390_IPL_COMPONENT_CEI_INVALID_SCLAB ; +set_cei_with_log (comps, comp_index, flag,"Invalid SCLAB length" ); + } +} + +static void check_sclab_format (uint8_t sclab_format, +IplDeviceComponentList *comps,int comp_index) +{ +uint32_t flag; +bool valid; + +/* SCLAB format must set to zero, indicating a format-0 SCLAB being used */ + valid = sclab_format ==0 ; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_INVALID_SCLAB_FORMAT ; +set_cei_with_log (comps, comp_index, flag, +"Format-0 SCLAB is not being use" ); + } +} + +static void check_sclab_opsw (SecureCodeLoadingAttributesBlock *sclab, +SecureIplSclabInfo *sclab_info, +IplDeviceComponentList *comps,int comp_index) +{ +const char *msg; +uint32_t flag; +bool is_opsw_set; +bool valid; + + is_opsw_set =is_sclab_flag_set (sclab->flags,S390_SECURE_IPL_SCLAB_FLAG_OPSW ); +if (!is_opsw_set) { +/* OPSW = 0 - Load PSW field in SCLAB must contain zeros */ + valid = sclab->load_psw ==0 ; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_SCLAB_LOAD_PSW_NOT_ZERO ; + msg ="Load PSW is not zero when Override PSW bit is zero" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + }else { +/* OPSW = 1 indicating global SCLAB */ + sclab_info->global_count +=1 ; +if (sclab_info->global_count ==1 ) { + sclab_info->load_psw = sclab->load_psw; + sclab_info->flags = sclab->flags; + } + +/* OLA must set to one */ + valid =is_sclab_flag_set (sclab->flags,S390_SECURE_IPL_SCLAB_FLAG_OLA ); +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_SCLAB_OLA_NOT_ONE ; + msg ="Override Load Address bit is not set to one in the global SCLAB" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + } +} + +static void check_sclab_ola (SecureCodeLoadingAttributesBlock *sclab, +uint64_t load_addr,IplDeviceComponentList *comps, +int comp_index) +{ +const char *msg; +uint32_t flag; +bool is_ola_set; +bool valid; + + is_ola_set =is_sclab_flag_set (sclab->flags,S390_SECURE_IPL_SCLAB_FLAG_OLA ); +if (!is_ola_set) { +/* OLA = 0 - Load address field in SCLAB must contain zeros */ + valid = sclab->load_addr ==0 ; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_SCLAB_LOAD_ADDR_NOT_ZERO ; + msg ="Load Address is not zero when Override Load Address bit is zero" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + + }else { +/* OLA = 1 - Load address field must match storage address of the component */ + valid = sclab->load_addr == load_addr; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_ADDR ; + msg ="Load Address does not match with component load address" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + } +} + +static void check_sclab_nuc (uint16_t sclab_flags,IplDeviceComponentList *comps, +int comp_index) +{ +const char *msg; +uint32_t flag; +bool is_nuc_set; +bool is_global_sclab; + + is_nuc_set =is_sclab_flag_set (sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_NUC ); + is_global_sclab =is_sclab_flag_set (sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_OPSW ); +if (is_nuc_set && !is_global_sclab) { + flag =S390_IPL_COMPONENT_CEI_NUC_NOT_IN_GLOBAL_SCLA ; + msg ="No Unsigned Components bit is set, but not in the global SCLAB" ; +set_cei_with_log (comps, comp_index, flag, msg); + } +} + +static void check_sclab_sc (uint16_t sclab_flags,IplDeviceComponentList *comps, +int comp_index) +{ +const char *msg; +uint32_t flag; +bool is_sc_set; +bool is_global_sclab; + + is_sc_set =is_sclab_flag_set (sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_SC ); + is_global_sclab =is_sclab_flag_set (sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_OPSW ); +if (is_sc_set && !is_global_sclab) { + flag =S390_IPL_COMPONENT_CEI_SC_NOT_IN_GLOBAL_SCLAB ; + msg ="Single Component bit is set, but not in the global SCLAB" ; +set_cei_with_log (comps, comp_index, flag, msg); + } +} + +static bool is_psw_valid (uint64_t psw,SecureIplCompAddrRange *comp_addr_range, +int range_index) +{ +uint32_t addr = psw &0x7fffffff ; + +/* PSW points within a signed binary code component */ +for (int i =0 ; i < range_index; i++) { +if (comp_addr_range[i].is_signed && + addr >= comp_addr_range[i].start_addr && + addr <= comp_addr_range[i].end_addr -2 ) { +return true; + } + } + +return false; +} + +static void check_load_psw (SecureIplCompAddrRange *comp_addr_range, +int addr_range_index,uint64_t sclab_load_psw, +uint64_t load_psw,IplDeviceComponentList *comps, +int comp_index) +{ +uint32_t flag; +const char *msg; +bool valid; + + valid =is_psw_valid (sclab_load_psw, comp_addr_range, addr_range_index) && +is_psw_valid (load_psw, comp_addr_range, addr_range_index); +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_INVALID_LOAD_PSW ; + msg ="Invalid PSW" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + +/* compare load PSW with the PSW specified in component */ + valid = sclab_load_psw == load_psw; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_PSW ; + msg ="Load PSW does not match with PSW in component" ; +set_cei_with_log (comps, comp_index, flag, msg); + } +} + +static void check_nuc (uint16_t global_sclab_flags,int unsigned_count, +IplDeviceComponentList *comps) +{ +uint16_t flag; +const char *msg; +bool is_nuc_set; + + is_nuc_set =is_sclab_flag_set (global_sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_NUC ); +if (is_nuc_set && unsigned_count >0 ) { + flag =S390_IPL_INFO_IIEI_FOUND_UNSIGNED_COMP ; + msg ="Unsigned components are not allowed" ; +set_iiei_with_log (comps, flag, msg); + } +} + +static void check_sc (uint16_t global_sclab_flags,int signed_count, +IplDeviceComponentList *comps) +{
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 505
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +10
-0+uint16_t flag; +const char *msg; +bool is_sc_set; + + is_sc_set =is_sclab_flag_set (global_sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_SC ); +if (is_sc_set && signed_count !=1 ) { + flag =S390_IPL_INFO_IIEI_MORE_SIGNED_COMP ; + msg ="Only one signed component is allowed" ; +set_iiei_with_log (comps, flag, msg); + }
I believe this error is meant to indicate that only a single binary component which is also signed is allowed in the component table, not "only one component in the table should be signed".
Might need clarification since the internal doc is a little confusing on this one.
Quoted 57 lines
pc-bios/s390-ccw/secure-ipl.c +57
-0+} + +void check_global_sclab (SecureIplSclabInfo sclab_info, +SecureIplCompAddrRange *comp_addr_range, +int addr_range_index,uint64_t load_psw, +int unsigned_count,int signed_count, +IplDeviceComponentList *comps,int comp_index) +{ +uint16_t flag; +const char *msg; + +if (sclab_info.count ==0 ) { +return ; + } + +if (sclab_info.global_count ==0 ) { + flag =S390_IPL_INFO_IIEI_NO_GLOBAL_SCLAB ; + msg ="Global SCLAB does not exists" ; +set_iiei_with_log (comps, flag, msg); +return ; + } + +if (sclab_info.global_count >1 ) { + flag =S390_IPL_INFO_IIEI_MORE_GLOBAL_SCLAB ; + msg ="More than one global SCLAB" ; +set_iiei_with_log (comps, flag, msg); +return ; + } + +if (sclab_info.load_psw) { +/* Verify PSW from the final component entry with PSW from the global SCLAB */ +check_load_psw (comp_addr_range, addr_range_index, + sclab_info.load_psw, load_psw, + comps, comp_index); + } + +if (sclab_info.flags) { +/* Unsigned components are not allowed if NUC flag is set in the global SCLAB */ +check_nuc (sclab_info.flags, unsigned_count, comps); + +/* Only one signed component is allowed is SC flag is set in the global SCLAB */ +check_sc (sclab_info.flags, signed_count, comps); + } +} + +static void check_signed_comp (int signed_count,IplDeviceComponentList *comps) +{ +uint16_t flag; +const char *msg; + +if (signed_count >0 ) { +return ; + } + + flag =S390_IPL_INFO_IIEI_NO_SIGNED_COMP ; + msg ="Secure boot is on, but components are not signed" ; +set_iiei_with_log (comps, flag, msg);
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 572
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +10
-0+} + +static void check_sclab_count (int count,IplDeviceComponentList *comps) +{ +uint16_t flag; +const char *msg; + +if (count >0 ) { +return ; + }
I *think* the condition for setting this error indicator is suppose to be if _any_ component is missing a SCLAB? So perhaps this check could be done during check_sclab? Further, I think this would be the same check as check_sclab_presence?
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 577
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c +5
-0+ + flag =S390_IPL_INFO_IIEI_NO_SCLAB ; + msg ="No recognizable SCLAB" ; +set_iiei_with_log (comps, flag, msg); +}
Most functions above this point follow the same formula:
// check condition
// if false: set flag and log message
Which is the perfect setup for an assert-style function. Something like
sclab_handle(condition, comp, "message", FLAG);
...or maybe a different name, since all checks aren't directly related to SCLAF and some need to set cei or iiei.
This would reduce the code in this patch considerably and make things far easier to understand.
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 588
Quoted 11 lines
pc-bios/s390-ccw/secure-ipl.c +11
-0+ +static void check_unsigned_comp (uint64_t comp_addr,IplDeviceComponentList *comps, +int comp_index,int cert_index,uint64_t comp_len) +{ +check_unsigned_addr (comp_addr, comps, comp_index); + +comp_list_add (comps, comp_index, cert_index, comp_addr, comp_len,0x00 ); +} + +static void check_sclab (uint64_t comp_addr,IplDeviceComponentList *comps, +uint64_t comp_len,int comp_index,SecureIplSclabInfo *sclab_info)
An IplDeviceComponentList contains an IplDeviceComponentEntry which has addr and len fields. Could those fields be used instead of comp_addr and comp_len?
Additionally, it looks like only one component is used inside this function. Just pass to this function the component that needs to be
inspected: comps[index].
Instead of a SecureIplSclabInfo struct, what about just using a SecureCodeLoadingAttributesBlock to represent the global SCLAB? When the OPSW flag is detected, check if global_sclab is NULL and set it. Otherwise, throw an error right away as another global sclab was found.
The function call could look as simple as:
check_sclab(comps[index], &global_sclab);
The global_sclab can later be passed to check_global_sclab :)
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 595
Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.c +7
-0+{ +SclabOriginLocator *sclab_locator; +SecureCodeLoadingAttributesBlock *sclab; +bool exist; +bool valid; + + sclab_locator = (SclabOriginLocator *)(comp_addr + comp_len -8 );
Add a comment describing what this is doing.
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 601
Quoted 6 lines
pc-bios/s390-ccw/secure-ipl.c +6
-0+ +/* return early if sclab does not exist */ + exist =check_sclab_presence (sclab_locator->magic, comps, comp_index); +if (!exist) { +return ; + }
Shouldn't SCLAB missing in secure mode be an error?
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 610
Quoted 9 lines
pc-bios/s390-ccw/secure-ipl.c +9
-0+ +check_sclab_length (sclab_locator->len, comps, comp_index); + +/* return early if sclab is invalid */ + valid = (comps->device_entries[comp_index].cei & +S390_IPL_COMPONENT_CEI_INVALID_SCLAB ) ==0 ; +if (!valid) { +return ; + }
Same here?
Quoted 109 lines
pc-bios/s390-ccw/secure-ipl.c +29
-3+ + sclab_info->count +=1 ; + sclab = (SecureCodeLoadingAttributesBlock *)(comp_addr + comp_len - + sclab_locator->len); + +check_sclab_format (sclab->format, comps, comp_index); +check_sclab_opsw (sclab, sclab_info, comps, comp_index); +check_sclab_ola (sclab, comp_addr, comps, comp_index); +check_sclab_nuc (sclab->flags, comps, comp_index); +check_sclab_sc (sclab->flags, comps, comp_index); +} +static uint32_t zipl_load_signature (ComponentEntry *entry,uint64_t sig_sec) {uint32_t sig_len;@@ -284,7 +686,11 @@ int zipl_run_secure(ComponentEntry **entry_ptr, uint8_t *tmp_sec)*/ int cert_table[MAX_CERTIFICATES ] = { [0 ...MAX_CERTIFICATES -1 ] =-1 };int rc; +SecureIplCompAddrRange comp_addr_range[MAX_CERTIFICATES ]; +int addr_range_index =0 ;int signed_count =0 ; +int unsigned_count =0 ; +SecureIplSclabInfo sclab_info = {0 };if (!secure_ipl_supported ()) {return -1 ;@@ -314,10 +720,18 @@ int zipl_run_secure(ComponentEntry **entry_ptr, uint8_t *tmp_sec) goto out; } +addr_overlap_check (comp_addr_range, &addr_range_index, + comp_addr, comp_addr + comp_len, sig_len >0 ); +if (!sig_len) { +check_unsigned_comp (comp_addr, &comps, + comp_entry_idx, cert_entry_idx, comp_len); + unsigned_count +=1 ; + comp_entry_idx++;break ; } +check_sclab (comp_addr, &comps, comp_len, comp_entry_idx, &sclab_info); verified =verify_signature (comp_len, comp_addr, sig_len, (uint64_t )sig, &cert_len, &cert_table_idx);@@ -363,9 +777,11 @@ int zipl_run_secure(ComponentEntry **entry_ptr, uint8_t *tmp_sec) } } -if (signed_count ==0 ) { -zipl_secure_handle ("Secure boot is on, but components are not signed" ); - } +check_signed_comp (signed_count, &comps); +check_sclab_count (sclab_info.count, &comps); +check_global_sclab (sclab_info, comp_addr_range, addr_range_index, + entry->compdat.load_psw, unsigned_count, signed_count, + &comps, comp_entry_idx);if (update_iirb (&comps, &certs)) {zipl_secure_handle ("Failed to write IPL Information Report Block" );pc-bios/s390-ccw/secure-ipl.h +36
-0@@ -16,6 +16,42 @@VCStorageSizeBlock *zipl_secure_get_vcssb (void );int zipl_run_secure (ComponentEntry **entry_ptr,uint8_t *tmp_sec); +#define S390_SECURE_IPL_SCLAB_FLAG_OPSW 0x8000 +#define S390_SECURE_IPL_SCLAB_FLAG_OLA 0x4000 +#define S390_SECURE_IPL_SCLAB_FLAG_NUC 0x2000 +#define S390_SECURE_IPL_SCLAB_FLAG_SC 0x1000 + +struct SecureCodeLoadingAttributesBlock { +uint8_t format; +uint8_t reserved1; +uint16_t flags; +uint8_t reserved2[4 ]; +uint64_t load_psw; +uint64_t load_addr; +uint64_t reserved3[]; +} __attribute__ ((packed)); +typedef struct SecureCodeLoadingAttributesBlock SecureCodeLoadingAttributesBlock ; + +struct SclabOriginLocator { +uint8_t reserved[2 ]; +uint16_t len; +uint8_t magic[4 ]; +} __attribute__ ((packed)); +typedef struct SclabOriginLocator SclabOriginLocator ; + +typedef struct SecureIplCompAddrRange { +bool is_signed; +uint64_t start_addr; +uint64_t end_addr; +}SecureIplCompAddrRange ; + +typedef struct SecureIplSclabInfo { +int count; +int global_count; +uint64_t load_psw; +uint16_t flags; +}SecureIplSclabInfo ; +static inline void zipl_secure_handle (const char *message) {switch (boot_mode) {
pc-bios/s390-ccw/secure-ipl.h static inline void zipl_secure_handle(const char *message) · line 69
Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.h +4
-0@@ -27,6 +63,25 @@ static inline void zipl_secure_handle(const char *message) } } +static inline bool is_sclab_flag_set (uint16_t sclab_flags,uint16_t flag) +{ +return (sclab_flags & flag) !=0 ; +}
Since this is just comparing the two parameters in one line, it would look a lot cleaner if the check was just done inline instead of calling a function to do the check.
pc-bios/s390-ccw/secure-ipl.h static inline void zipl_secure_handle(const char *message) · line 76
Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.h +7
-0+ +static inline void set_cei_with_log (IplDeviceComponentList *comps,int comp_index, +uint32_t flag,const char *message) +{ + comps->device_entries[comp_index].cei |= flag; +zipl_secure_handle (message); +}
Same here.
pc-bios/s390-ccw/secure-ipl.h static inline void zipl_secure_handle(const char *message) · line 83
Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.h +7
-0+ +static inline void set_iiei_with_log (IplDeviceComponentList *comps,uint16_t flag, +const char *message) +{ + comps->ipl_info_header.iiei |= flag; +zipl_secure_handle (message); +}
And for here. Just keep these few lines of code in-line on the function invoking them.
pc-bios/s390-ccw/secure-ipl.h static inline void zipl_secure_handle(const char *message) · line 87
Quoted 4 lines
pc-bios/s390-ccw/secure-ipl.h +1
-0+static inline uint64_t diag320 (void *data,unsigned long subcode) { register unsigned long addr asm("0") = (unsigned long)data;
Overall for the larger patches in this series, the biggest request would be to find a way reduce the number of parameters passed around to each function. It makes following the logic very tricky.
Signature · 2 lines
Regards, Collin
Re: [PATCH v7 21/29] pc-bios/s390-ccw: Add additional security checks for secure boot
Quoted 191 lines · Collin Walling
Quoted 39 lines · Zhuoying Cai
Add additional checks to ensure that components do not overlap with signed components when loaded into memory.
Add additional checks to ensure the load addresses of unsigned components are greater than or equal to
0x2000.When the secure IPL code loading attributes facility (SCLAF) is installed, all signed components must contain a secure code loading attributes block (SCLAB).
The SCLAB provides further validation of information on where to load the signed binary code from the load device, and where to start the execution of the loaded OS code.
When SCLAF is installed, its content must be evaluated during secure IPL. However, a missing SCLAB will not be reported in audit mode. The SCALB checking will be skipped in this case.
Add IPL Information Error Indicators (IIEI) and Component Error Indicators (CEI) for IPL Information Report Block (IIRB).
When SCLAF is installed, additional secure boot checks are performed during zipl and store results of verification into IIRB.
Signed-off-by Zhuoying Cai <[email protected]>--- pc-bios/s390-ccw/iplb.h | 26 ++- pc-bios/s390-ccw/s390-ccw.h | 1 + pc-bios/s390-ccw/sclp.c | 8 + pc-bios/s390-ccw/sclp.h | 1 + pc-bios/s390-ccw/secure-ipl.c | 422 +++++++++++++++++++++++++++++++++- pc-bios/s390-ccw/secure-ipl.h | 55 +++++ 6 files changed, 508 insertions(+), 5 deletions(-)pc-bios/s390-ccw/iplb.h +0
-0pc-bios/s390-ccw/iplb.h struct IplInfoReportBlockHeader { · line 40
Quoted 9 lines
pc-bios/s390-ccw/iplb.h +6
-0@@ -32,11 +32,19 @@ struct IplInfoReportBlockHeader { };typedef struct IplInfoReportBlockHeader IplInfoReportBlockHeader ; +#define S390_IPL_INFO_IIEI_NO_SIGNED_COMP 0x8000/* bit 0 */ +#define S390_IPL_INFO_IIEI_NO_SCLAB 0x4000/* bit 1 */ +#define S390_IPL_INFO_IIEI_NO_GLOBAL_SCLAB 0x2000/* bit 2 */ +#define S390_IPL_INFO_IIEI_MORE_GLOBAL_SCLAB 0x1000/* bit 3 */ +#define S390_IPL_INFO_IIEI_FOUND_UNSIGNED_COMP 0x800/* bit 4 */ +#define S390_IPL_INFO_IIEI_MORE_SIGNED_COMP 0x400/* bit 5 */ What about shorthanding these to just S390_IIEI_*
Then add a comment above giving the longhand /* IPL Info Error Indicators */
Quoted 17 lines
pc-bios/s390-ccw/iplb.h +5
-1+struct IplInfoBlockHeader {uint32_t len;uint8_t ibt;uint8_t reserved1[3 ]; -uint8_t reserved2[8 ]; +uint16_t iiei; +uint8_t reserved2[6 ]; };typedef struct IplInfoBlockHeader IplInfoBlockHeader ;@@ -60,13 +68,27 @@ typedef struct IplSignatureCertificateList IplSignatureCertificateList;#define S390_IPL_COMPONENT_FLAG_SC 0x80#define S390_IPL_COMPONENT_FLAG_CSV 0x40 +#define S390_IPL_COMPONENT_CEI_INVALID_SCLAB 0x80000000/* bit 0 */ +#define S390_IPL_COMPONENT_CEI_INVALID_SCLAB_LEN 0x40000000/* bit 1 */ pc-bios/s390-ccw/iplb.h typedef struct IplSignatureCertificateList IplSignatureCertificateList; · line 82
Quoted 10 lines
pc-bios/s390-ccw/iplb.h +10
-0+#define S390_IPL_COMPONENT_CEI_INVALID_SCLAB_FORMAT 0x20000000/* bit 2 */ +#define S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_ADDR 0x10000000/* bit 3 */ +#define S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_PSW 0x8000000/* bit 4 */ +#define S390_IPL_COMPONENT_CEI_INVALID_LOAD_PSW 0x4000000/* bit 5 */ +#define S390_IPL_COMPONENT_CEI_NUC_NOT_IN_GLOBAL_SCLA 0x2000000/* bit 6 */ +#define S390_IPL_COMPONENT_CEI_SCLAB_OLA_NOT_ONE 0x1000000/* bit 7 */ +#define S390_IPL_COMPONENT_CEI_SC_NOT_IN_GLOBAL_SCLAB 0x800000/* bit 8 */ +#define S390_IPL_COMPONENT_CEI_SCLAB_LOAD_ADDR_NOT_ZERO 0x400000/* bit 9 */ +#define S390_IPL_COMPONENT_CEI_SCLAB_LOAD_PSW_NOT_ZERO 0x200000/* bit 10 */ +#define S390_IPL_COMPONENT_CEI_INVALID_UNSIGNED_ADDR 0x100000/* bit 11 */ Similar to above, what about S390_CEI_* and a comment.
Quoted 107 lines
pc-bios/s390-ccw/iplb.h +3
-1+struct IplDeviceComponentEntry {uint64_t addr;uint64_t len;uint8_t flags;uint8_t reserved1[5 ];uint16_t cert_index; -uint8_t reserved2[8 ]; +uint32_t cei; +uint8_t reserved2[4 ]; };typedef struct IplDeviceComponentEntry IplDeviceComponentEntry ;pc-bios/s390-ccw/s390-ccw.h +1
-0@@ -69,6 +69,7 @@ void sclp_setup(void);void sclp_get_loadparm_ascii (char *loadparm);bool sclp_is_diag320_on (void );bool sclp_is_sipl_on (void ); +bool sclp_is_sclaf_on (void );int sclp_read (char *str,size_t count);/* virtio.c */ pc-bios/s390-ccw/sclp.c +8
-0@@ -157,6 +157,14 @@ bool sclp_is_sipl_on(void)return fac_ipl &SCCB_FAC_IPL_SIPL_BIT ; } +bool sclp_is_sclaf_on (void ) +{ +uint16_t fac_ipl =0 ; + +sclp_get_fac_ipl (&fac_ipl); +return fac_ipl &SCCB_FAC_IPL_SCLAF_BIT ; +} +int sclp_read (char *str,size_t count) {ReadEventData *sccb = (void *)_sccb;pc-bios/s390-ccw/sclp.h +1
-0@@ -52,6 +52,7 @@ typedef struct SCCBHeader {#define SCCB_DATA_LEN (SCCB_SIZE - sizeof(SCCBHeader))#define SCCB_FAC134_DIAG320_BIT 0x4#define SCCB_FAC_IPL_SIPL_BIT 0x4000 +#define SCCB_FAC_IPL_SCLAF_BIT 0x1000 typedef struct ReadInfo { SCCBHeader h;pc-bios/s390-ccw/secure-ipl.c +36
-0@@ -206,6 +206,12 @@ static bool secure_ipl_supported(void)return false; } +if (!sclp_is_sclaf_on ()) { +puts ("Secure IPL Code Loading Attributes Facility is not supported by" +" the hypervisor!" ); +return false; + } +return true; }@@ -218,6 +224,402 @@ static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis certs->ipl_info_header.len =sizeof (certs->ipl_info_header); } +static bool is_comp_overlap (SecureIplCompAddrRange *comp_addr_range, +int addr_range_index, +uint64_t start_addr,uint64_t end_addr) +{ +/* neither a signed nor an unsigned component can overlap with a signed component */ +for (int i =0 ; i < addr_range_index; i++) { +if ((comp_addr_range[i].start_addr <= end_addr -1 && + start_addr <= comp_addr_range[i].end_addr -1 ) && + comp_addr_range[i].is_signed) { +return true; + } + } + +return false; +} + +static void comp_addr_range_add (SecureIplCompAddrRange *comp_addr_range, +int addr_range_index,bool is_signed, +uint64_t start_addr,uint64_t end_addr) +{ +if (addr_range_index >MAX_CERTIFICATES -1 ) { +printf ("Warning: Ignoring component address range index [%d]" +" because it's over %d index\n" , + addr_range_index,MAX_CERTIFICATES ); +return ; + } + + comp_addr_range[addr_range_index].is_signed = is_signed; + comp_addr_range[addr_range_index].start_addr = start_addr; + comp_addr_range[addr_range_index].end_addr = end_addr;
Quoted 9 lines
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 261
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c +5
-0+} + +static void check_unsigned_addr (uint64_t load_addr,IplDeviceComponentList *comps, +int comp_index) +{Is
load_addrthe same as comps->device_entries[comp_index].addr? Also, can this function just be passed comps->device_entries[comp_index] instead?
comps->device_entries[comp_index].addr is not initialized at this point, so load_addr is required here. Passing comps->device_entries[comp_index] should still work.
Quoted 257 lines
Quoted 18 lines
pc-bios/s390-ccw/secure-ipl.c +18
-0+uint32_t flag; +bool valid; + +/* unsigned load address must be greater than or equal to 0x2000 */ + valid = load_addr >=0x2000 ; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_INVALID_UNSIGNED_ADDR ; +set_cei_with_log (comps, comp_index, flag, +"Load address is less than 0x2000" ); + } +} + +static void addr_overlap_check (SecureIplCompAddrRange *comp_addr_range, +int *addr_range_index, +uint64_t start_addr,uint64_t end_addr,bool is_signed) +{ +bool overlap; +pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 289
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +10
-0+ overlap =is_comp_overlap (comp_addr_range, *addr_range_index, + start_addr, end_addr); +if (!overlap) { +comp_addr_range_add (comp_addr_range, *addr_range_index, is_signed, + start_addr, end_addr); + *addr_range_index +=1 ; + }else { +zipl_secure_handle ("Component addresses overlap" ); + } +}Is the address-range / comp-overlap stuff part of the SCLAF? Is it possible some of these checks are moved over to a separate patch so that this code can be isolated?
Quoted 13 lines
pc-bios/s390-ccw/secure-ipl.c +13
-0+ +static bool check_sclab_presence (uint8_t *sclab_magic, +IplDeviceComponentList *comps,int comp_index) +{ +/* identifies the presence of SCLAB */ +if (!magic_match (sclab_magic,ZIPL_MAGIC )) { + comps->device_entries[comp_index].cei |=S390_IPL_COMPONENT_CEI_INVALID_SCLAB ; + +/* a missing SCLAB will not be reported in audit mode */ +return false; + } + +return true;pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 312
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +10
-0+} + +static void check_sclab_length (uint16_t sclab_len, +IplDeviceComponentList *comps,int comp_index) +{ +uint32_t flag; +bool valid; + +/* minimum SCLAB length is 32 bytes */ + valid = sclab_len >=32 ;#define this length somewhere and then get rid of the comment.
Quoted 183 lines
pc-bios/s390-ccw/secure-ipl.c +183
-0+if (!valid) { + flag =S390_IPL_COMPONENT_CEI_INVALID_SCLAB_LEN | +S390_IPL_COMPONENT_CEI_INVALID_SCLAB ; +set_cei_with_log (comps, comp_index, flag,"Invalid SCLAB length" ); + } +} + +static void check_sclab_format (uint8_t sclab_format, +IplDeviceComponentList *comps,int comp_index) +{ +uint32_t flag; +bool valid; + +/* SCLAB format must set to zero, indicating a format-0 SCLAB being used */ + valid = sclab_format ==0 ; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_INVALID_SCLAB_FORMAT ; +set_cei_with_log (comps, comp_index, flag, +"Format-0 SCLAB is not being use" ); + } +} + +static void check_sclab_opsw (SecureCodeLoadingAttributesBlock *sclab, +SecureIplSclabInfo *sclab_info, +IplDeviceComponentList *comps,int comp_index) +{ +const char *msg; +uint32_t flag; +bool is_opsw_set; +bool valid; + + is_opsw_set =is_sclab_flag_set (sclab->flags,S390_SECURE_IPL_SCLAB_FLAG_OPSW ); +if (!is_opsw_set) { +/* OPSW = 0 - Load PSW field in SCLAB must contain zeros */ + valid = sclab->load_psw ==0 ; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_SCLAB_LOAD_PSW_NOT_ZERO ; + msg ="Load PSW is not zero when Override PSW bit is zero" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + }else { +/* OPSW = 1 indicating global SCLAB */ + sclab_info->global_count +=1 ; +if (sclab_info->global_count ==1 ) { + sclab_info->load_psw = sclab->load_psw; + sclab_info->flags = sclab->flags; + } + +/* OLA must set to one */ + valid =is_sclab_flag_set (sclab->flags,S390_SECURE_IPL_SCLAB_FLAG_OLA ); +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_SCLAB_OLA_NOT_ONE ; + msg ="Override Load Address bit is not set to one in the global SCLAB" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + } +} + +static void check_sclab_ola (SecureCodeLoadingAttributesBlock *sclab, +uint64_t load_addr,IplDeviceComponentList *comps, +int comp_index) +{ +const char *msg; +uint32_t flag; +bool is_ola_set; +bool valid; + + is_ola_set =is_sclab_flag_set (sclab->flags,S390_SECURE_IPL_SCLAB_FLAG_OLA ); +if (!is_ola_set) { +/* OLA = 0 - Load address field in SCLAB must contain zeros */ + valid = sclab->load_addr ==0 ; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_SCLAB_LOAD_ADDR_NOT_ZERO ; + msg ="Load Address is not zero when Override Load Address bit is zero" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + + }else { +/* OLA = 1 - Load address field must match storage address of the component */ + valid = sclab->load_addr == load_addr; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_ADDR ; + msg ="Load Address does not match with component load address" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + } +} + +static void check_sclab_nuc (uint16_t sclab_flags,IplDeviceComponentList *comps, +int comp_index) +{ +const char *msg; +uint32_t flag; +bool is_nuc_set; +bool is_global_sclab; + + is_nuc_set =is_sclab_flag_set (sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_NUC ); + is_global_sclab =is_sclab_flag_set (sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_OPSW ); +if (is_nuc_set && !is_global_sclab) { + flag =S390_IPL_COMPONENT_CEI_NUC_NOT_IN_GLOBAL_SCLA ; + msg ="No Unsigned Components bit is set, but not in the global SCLAB" ; +set_cei_with_log (comps, comp_index, flag, msg); + } +} + +static void check_sclab_sc (uint16_t sclab_flags,IplDeviceComponentList *comps, +int comp_index) +{ +const char *msg; +uint32_t flag; +bool is_sc_set; +bool is_global_sclab; + + is_sc_set =is_sclab_flag_set (sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_SC ); + is_global_sclab =is_sclab_flag_set (sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_OPSW ); +if (is_sc_set && !is_global_sclab) { + flag =S390_IPL_COMPONENT_CEI_SC_NOT_IN_GLOBAL_SCLAB ; + msg ="Single Component bit is set, but not in the global SCLAB" ; +set_cei_with_log (comps, comp_index, flag, msg); + } +} + +static bool is_psw_valid (uint64_t psw,SecureIplCompAddrRange *comp_addr_range, +int range_index) +{ +uint32_t addr = psw &0x7fffffff ; + +/* PSW points within a signed binary code component */ +for (int i =0 ; i < range_index; i++) { +if (comp_addr_range[i].is_signed && + addr >= comp_addr_range[i].start_addr && + addr <= comp_addr_range[i].end_addr -2 ) { +return true; + } + } + +return false; +} + +static void check_load_psw (SecureIplCompAddrRange *comp_addr_range, +int addr_range_index,uint64_t sclab_load_psw, +uint64_t load_psw,IplDeviceComponentList *comps, +int comp_index) +{ +uint32_t flag; +const char *msg; +bool valid; + + valid =is_psw_valid (sclab_load_psw, comp_addr_range, addr_range_index) && +is_psw_valid (load_psw, comp_addr_range, addr_range_index); +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_INVALID_LOAD_PSW ; + msg ="Invalid PSW" ; +set_cei_with_log (comps, comp_index, flag, msg); + } + +/* compare load PSW with the PSW specified in component */ + valid = sclab_load_psw == load_psw; +if (!valid) { + flag =S390_IPL_COMPONENT_CEI_UNMATCHED_SCLAB_LOAD_PSW ; + msg ="Load PSW does not match with PSW in component" ; +set_cei_with_log (comps, comp_index, flag, msg); + } +} + +static void check_nuc (uint16_t global_sclab_flags,int unsigned_count, +IplDeviceComponentList *comps) +{ +uint16_t flag; +const char *msg; +bool is_nuc_set; + + is_nuc_set =is_sclab_flag_set (global_sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_NUC ); +if (is_nuc_set && unsigned_count >0 ) { + flag =S390_IPL_INFO_IIEI_FOUND_UNSIGNED_COMP ; + msg ="Unsigned components are not allowed" ; +set_iiei_with_log (comps, flag, msg); + } +} + +static void check_sc (uint16_t global_sclab_flags,int signed_count, +IplDeviceComponentList *comps) +{pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 505
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +10
-0+uint16_t flag; +const char *msg; +bool is_sc_set; + + is_sc_set =is_sclab_flag_set (global_sclab_flags,S390_SECURE_IPL_SCLAB_FLAG_SC ); +if (is_sc_set && signed_count !=1 ) { + flag =S390_IPL_INFO_IIEI_MORE_SIGNED_COMP ; + msg ="Only one signed component is allowed" ; +set_iiei_with_log (comps, flag, msg); + }I believe this error is meant to indicate that only a single binary component which is also signed is allowed in the component table, not "only one component in the table should be signed".
Quoted 3 lines
Might need clarification since the internal doc is a little confusing on this one.
I think you are correct. After revisiting the internal doc, my understanding is that the component table must contain exactly one component, and that component must be signed.
If more than one component exist in the component table, signed or unsigned, the IPL will terminate.
Quoted 64 lines
Quoted 64 lines
pc-bios/s390-ccw/secure-ipl.c +64
-0+} + +void check_global_sclab (SecureIplSclabInfo sclab_info, +SecureIplCompAddrRange *comp_addr_range, +int addr_range_index,uint64_t load_psw, +int unsigned_count,int signed_count, +IplDeviceComponentList *comps,int comp_index) +{ +uint16_t flag; +const char *msg; + +if (sclab_info.count ==0 ) { +return ; + } + +if (sclab_info.global_count ==0 ) { + flag =S390_IPL_INFO_IIEI_NO_GLOBAL_SCLAB ; + msg ="Global SCLAB does not exists" ; +set_iiei_with_log (comps, flag, msg); +return ; + } + +if (sclab_info.global_count >1 ) { + flag =S390_IPL_INFO_IIEI_MORE_GLOBAL_SCLAB ; + msg ="More than one global SCLAB" ; +set_iiei_with_log (comps, flag, msg); +return ; + } + +if (sclab_info.load_psw) { +/* Verify PSW from the final component entry with PSW from the global SCLAB */ +check_load_psw (comp_addr_range, addr_range_index, + sclab_info.load_psw, load_psw, + comps, comp_index); + } + +if (sclab_info.flags) { +/* Unsigned components are not allowed if NUC flag is set in the global SCLAB */ +check_nuc (sclab_info.flags, unsigned_count, comps); + +/* Only one signed component is allowed is SC flag is set in the global SCLAB */ +check_sc (sclab_info.flags, signed_count, comps); + } +} + +static void check_signed_comp (int signed_count,IplDeviceComponentList *comps) +{ +uint16_t flag; +const char *msg; + +if (signed_count >0 ) { +return ; + } + + flag =S390_IPL_INFO_IIEI_NO_SIGNED_COMP ; + msg ="Secure boot is on, but components are not signed" ; +set_iiei_with_log (comps, flag, msg); +} + +static void check_sclab_count (int count,IplDeviceComponentList *comps) +{ +uint16_t flag; +const char *msg; +
Quoted 9 lines
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 572
Quoted 3 lines
pc-bios/s390-ccw/secure-ipl.c +3
-0+if (count >0 ) { +return ; + }I *think* the condition for setting this error indicator is suppose to be if _any_ component is missing a SCLAB? So perhaps this check could be done during
check_sclab? Further, I think this would be the same check ascheck_sclab_presence?
My understanding is that the IPL-Information Error Indicators (IIEI) are set only after all components have been loaded, since they are part of the IPL Device Component List header. Errors related to a specific component’s SCLAB should instead be reported through that component’s Component Error Indicators (CEI).
This bit indicates that no recognizable SCLAB was found across all components. For example, if an earlier component lacks a SCLAB but a later one contains one, setting this bit too early would not reflect the actual condition.
Quoted 62 lines
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 577
Quoted 5 lines
pc-bios/s390-ccw/secure-ipl.c +5
-0+ + flag =S390_IPL_INFO_IIEI_NO_SCLAB ; + msg ="No recognizable SCLAB" ; +set_iiei_with_log (comps, flag, msg); +}Most functions above this point follow the same formula:
// check condition
// if false: set flag and log messageWhich is the perfect setup for an assert-style function. Something like
sclab_handle(condition, comp, "message", FLAG);...or maybe a different name, since all checks aren't directly related to SCLAF and some need to set cei or iiei.
This would reduce the code in this patch considerably and make things far easier to understand.
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 588
Quoted 11 lines
pc-bios/s390-ccw/secure-ipl.c +11
-0+ +static void check_unsigned_comp (uint64_t comp_addr,IplDeviceComponentList *comps, +int comp_index,int cert_index,uint64_t comp_len) +{ +check_unsigned_addr (comp_addr, comps, comp_index); + +comp_list_add (comps, comp_index, cert_index, comp_addr, comp_len,0x00 ); +} + +static void check_sclab (uint64_t comp_addr,IplDeviceComponentList *comps, +uint64_t comp_len,int comp_index,SecureIplSclabInfo *sclab_info)An IplDeviceComponentList contains an IplDeviceComponentEntry which has addr and len fields. Could those fields be used instead of
comp_addrandcomp_len?Additionally, it looks like only one component is used inside this function. Just pass to this function the component that needs to be
inspected:comps[index].Instead of a SecureIplSclabInfo struct, what about just using a SecureCodeLoadingAttributesBlock to represent the global SCLAB? When the OPSW flag is detected, check if
global_sclabis NULL and set it. Otherwise, throw an error right away as another global sclab was found.The function call could look as simple as:
check_sclab(comps[index], &global_sclab);The
global_sclabcan later be passed tocheck_global_sclab:)pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 595
Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.c +7
-0+{ +SclabOriginLocator *sclab_locator; +SecureCodeLoadingAttributesBlock *sclab; +bool exist; +bool valid; + + sclab_locator = (SclabOriginLocator *)(comp_addr + comp_len -8 );Add a comment describing what this is doing.
Quoted 9 lines
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 601
Quoted 6 lines
pc-bios/s390-ccw/secure-ipl.c +6
-0+ +/* return early if sclab does not exist */ + exist =check_sclab_presence (sclab_locator->magic, comps, comp_index); +if (!exist) { +return ; + }Shouldn't SCLAB missing in secure mode be an error?
Yes, it is handled in check_sclab_presence().
Quoted 12 lines
pc-bios/s390-ccw/secure-ipl.c static void init_lists(IplDeviceComponentList *comps, IplSignatureCertificateLis · line 610
Quoted 9 lines
pc-bios/s390-ccw/secure-ipl.c +9
-0+ +check_sclab_length (sclab_locator->len, comps, comp_index); + +/* return early if sclab is invalid */ + valid = (comps->device_entries[comp_index].cei & +S390_IPL_COMPONENT_CEI_INVALID_SCLAB ) ==0 ; +if (!valid) { +return ; + }Same here?
In secure mode, the IPL will terminate and won't reach this point. In audit mode, the IPL could continue, and this function returns early to avoid further SCLAB checks once the SCLAB is determined to be invalid.
Quoted 151 lines
Quoted 109 lines
pc-bios/s390-ccw/secure-ipl.c +29
-3+ + sclab_info->count +=1 ; + sclab = (SecureCodeLoadingAttributesBlock *)(comp_addr + comp_len - + sclab_locator->len); + +check_sclab_format (sclab->format, comps, comp_index); +check_sclab_opsw (sclab, sclab_info, comps, comp_index); +check_sclab_ola (sclab, comp_addr, comps, comp_index); +check_sclab_nuc (sclab->flags, comps, comp_index); +check_sclab_sc (sclab->flags, comps, comp_index); +} +static uint32_t zipl_load_signature (ComponentEntry *entry,uint64_t sig_sec) {uint32_t sig_len;@@ -284,7 +686,11 @@ int zipl_run_secure(ComponentEntry **entry_ptr, uint8_t *tmp_sec)*/ int cert_table[MAX_CERTIFICATES ] = { [0 ...MAX_CERTIFICATES -1 ] =-1 };int rc; +SecureIplCompAddrRange comp_addr_range[MAX_CERTIFICATES ]; +int addr_range_index =0 ;int signed_count =0 ; +int unsigned_count =0 ; +SecureIplSclabInfo sclab_info = {0 };if (!secure_ipl_supported ()) {return -1 ;@@ -314,10 +720,18 @@ int zipl_run_secure(ComponentEntry **entry_ptr, uint8_t *tmp_sec) goto out; } +addr_overlap_check (comp_addr_range, &addr_range_index, + comp_addr, comp_addr + comp_len, sig_len >0 ); +if (!sig_len) { +check_unsigned_comp (comp_addr, &comps, + comp_entry_idx, cert_entry_idx, comp_len); + unsigned_count +=1 ; + comp_entry_idx++;break ; } +check_sclab (comp_addr, &comps, comp_len, comp_entry_idx, &sclab_info); verified =verify_signature (comp_len, comp_addr, sig_len, (uint64_t )sig, &cert_len, &cert_table_idx);@@ -363,9 +777,11 @@ int zipl_run_secure(ComponentEntry **entry_ptr, uint8_t *tmp_sec) } } -if (signed_count ==0 ) { -zipl_secure_handle ("Secure boot is on, but components are not signed" ); - } +check_signed_comp (signed_count, &comps); +check_sclab_count (sclab_info.count, &comps); +check_global_sclab (sclab_info, comp_addr_range, addr_range_index, + entry->compdat.load_psw, unsigned_count, signed_count, + &comps, comp_entry_idx);if (update_iirb (&comps, &certs)) {zipl_secure_handle ("Failed to write IPL Information Report Block" );pc-bios/s390-ccw/secure-ipl.h +36
-0@@ -16,6 +16,42 @@VCStorageSizeBlock *zipl_secure_get_vcssb (void );int zipl_run_secure (ComponentEntry **entry_ptr,uint8_t *tmp_sec); +#define S390_SECURE_IPL_SCLAB_FLAG_OPSW 0x8000 +#define S390_SECURE_IPL_SCLAB_FLAG_OLA 0x4000 +#define S390_SECURE_IPL_SCLAB_FLAG_NUC 0x2000 +#define S390_SECURE_IPL_SCLAB_FLAG_SC 0x1000 + +struct SecureCodeLoadingAttributesBlock { +uint8_t format; +uint8_t reserved1; +uint16_t flags; +uint8_t reserved2[4 ]; +uint64_t load_psw; +uint64_t load_addr; +uint64_t reserved3[]; +} __attribute__ ((packed)); +typedef struct SecureCodeLoadingAttributesBlock SecureCodeLoadingAttributesBlock ; + +struct SclabOriginLocator { +uint8_t reserved[2 ]; +uint16_t len; +uint8_t magic[4 ]; +} __attribute__ ((packed)); +typedef struct SclabOriginLocator SclabOriginLocator ; + +typedef struct SecureIplCompAddrRange { +bool is_signed; +uint64_t start_addr; +uint64_t end_addr; +}SecureIplCompAddrRange ; + +typedef struct SecureIplSclabInfo { +int count; +int global_count; +uint64_t load_psw; +uint16_t flags; +}SecureIplSclabInfo ; +static inline void zipl_secure_handle (const char *message) {switch (boot_mode) {pc-bios/s390-ccw/secure-ipl.h static inline void zipl_secure_handle(const char *message) · line 69
Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.h +4
-0@@ -27,6 +63,25 @@ static inline void zipl_secure_handle(const char *message) } } +static inline bool is_sclab_flag_set (uint16_t sclab_flags,uint16_t flag) +{ +return (sclab_flags & flag) !=0 ; +}Since this is just comparing the two parameters in one line, it would look a lot cleaner if the check was just done inline instead of calling a function to do the check.
pc-bios/s390-ccw/secure-ipl.h static inline void zipl_secure_handle(const char *message) · line 76
Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.h +7
-0+ +static inline void set_cei_with_log (IplDeviceComponentList *comps,int comp_index, +uint32_t flag,const char *message) +{ + comps->device_entries[comp_index].cei |= flag; +zipl_secure_handle (message); +}Same here.
pc-bios/s390-ccw/secure-ipl.h static inline void zipl_secure_handle(const char *message) · line 83
Quoted 7 lines
pc-bios/s390-ccw/secure-ipl.h +7
-0+ +static inline void set_iiei_with_log (IplDeviceComponentList *comps,uint16_t flag, +const char *message) +{ + comps->ipl_info_header.iiei |= flag; +zipl_secure_handle (message); +}And for here. Just keep these few lines of code in-line on the function invoking them.
pc-bios/s390-ccw/secure-ipl.h static inline void zipl_secure_handle(const char *message) · line 87
Quoted 4 lines
pc-bios/s390-ccw/secure-ipl.h +1
-0+static inline uint64_t diag320 (void *data,unsigned long subcode) { register unsigned long addr asm("0") = (unsigned long)data;Overall for the larger patches in this series, the biggest request would be to find a way reduce the number of parameters passed around to each function. It makes following the logic very tricky.
PATCH v7 22/29 Add secure-boot to s390-ccw-virtio machine type option
In reply to Zhuoying CaiAdd secure-boot as a parameter of s390-ccw-virtio machine type option.
The secure-boot=on|off parameter is implemented to enable secure IPL.
By default, secure-boot is set to false if not specified in the command line.
--- docs/system/s390x/secure-ipl.rst | 22 +++++++++++++++++----- hw/s390x/s390-virtio-ccw.c | 22 ++++++++++++++++++++++ include/hw/s390x/s390-virtio-ccw.h | 1 + qemu-options.hx | 6 +++++- 4 files changed, 45 insertions(+), 6 deletions(-)
docs/system/s390x/secure-ipl.rst +17 -5
@@ -19,19 +19,31 @@ Note: certificate files must have a .pem extension. qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ... +Enabling Secure IPL +------------------- + +Secure IPL is enabled by explicitly setting ``secure-boot=on``; if not +specified, secure boot is considered off. + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,secure-boot=on|off + IPL Modes ========= Multiple IPL modes are available to differentiate between the various IPL -configurations. These modes are mutually exclusive and enabled based on the -``boot-certs`` option on the QEMU command line. +configurations. These modes are mutually exclusive and enabled based on specific +combinations of the ``secure-boot`` and ``boot-certs`` options on the QEMU +command line. Normal Mode ----------- -The absence of certificates will attempt to IPL a guest without secure IPL -operations. No checks are performed, and no warnings/errors are reported. -This is the default mode. +The absence of both certificates and the ``secure-boot`` option will attempt to +IPL a guest without secure IPL operations. No checks are performed, and no +warnings/errors are reported. This is the default mode, and can be explicitly +enabled with ``secure-boot=off``. Configuration:
hw/s390x/s390-virtio-ccw.c +22 -0
@@ -803,6 +803,21 @@ static void machine_set_boot_certs(Object *obj, Visitor *v, const char *name, ms->boot_certs = cert_list; } +static inline bool machine_get_secure_boot (Object *obj,Error **errp) +{ +S390CcwMachineState *ms =S390_CCW_MACHINE (obj); + +return ms->secure_boot; +} + +static inline void machine_set_secure_boot (Object *obj,bool value, +Error **errp) +{ +S390CcwMachineState *ms =S390_CCW_MACHINE (obj); + + ms->secure_boot = value; +} +static void ccw_machine_class_init (ObjectClass *oc,const void *data) {MachineClass *mc =MACHINE_CLASS (oc);
@@ -861,6 +876,13 @@ static void ccw_machine_class_init(ObjectClass *oc, const void *data) machine_get_boot_certs, machine_set_boot_certs, NULL, NULL);object_class_property_set_description (oc,"boot-certs" ,"provide paths to a directory and/or a certificate file for secure boot" ); + +object_class_property_add_bool (oc,"secure-boot" , + machine_get_secure_boot, + machine_set_secure_boot); +object_class_property_set_description (oc,"secure-boot" , +"enable/disable secure boot" ); + }static inline void s390_machine_initfn (Object *obj)
include/hw/s390x/s390-virtio-ccw.h +1 -0
@@ -29,6 +29,7 @@ struct S390CcwMachineState {bool aes_key_wrap;bool dea_key_wrap;bool pv; +bool secure_boot;uint8_t loadparm[8 ];uint64_t memory_limit;uint64_t max_pagesize;
qemu-options.hx +5 -1
@@ -45,7 +45,8 @@ DEF("machine", HAS_ARG, QEMU_OPTION_machine, \ " memory-backend='backend-id' specifies explicitly provided backend for main RAM (default=none)\n" " cxl-fmw.0.targets.0=firsttarget,cxl-fmw.0.targets.1=secondtarget,cxl-fmw.0.size=size[,cxl-fmw.0.interleave-granularity=granularity]\n" " smp-cache.0.cache=cachename,smp-cache.0.topology=topologylevel\n" - " boot-certs.0.path=/path/directory,boot-certs.1.path=/path/file provides paths to a directory and/or a certificate file\n", + " boot-certs.0.path=/path/directory,boot-certs.1.path=/path/file provides paths to a directory and/or a certificate file\n" + " secure-boot=on|off enable/disable secure boot (default=off) \n", QEMU_ARCH_ALL) SRST ``-machine [type=]name[,prop=value[,...]]``
@@ -209,6 +210,9 @@ SRST ``boot-certs.0.path=/path/directory,boot-certs.1.path=/path/file`` Provide paths to a directory and/or a certificate file on the host [s390x only]. + + ``secure-boot=on|off`` + Enables or disables secure boot on s390-ccw guest. The default is off. ERST DEF("M", HAS_ARG, QEMU_OPTION_M,
Signature · 1 line
2.51.1
Re: [PATCH v7 22/29] Add secure-boot to s390-ccw-virtio machine type option
Quoted 7 lines · Zhuoying Cai
Add secure-boot as a parameter of s390-ccw-virtio machine type option.
The
secure-boot=on|offparameter is implemented to enable secure IPL.By default, secure-boot is set to false if not specified in the command line.
Quoted 7 lines
Signed-off-by Zhuoying Cai <[email protected]>--- docs/system/s390x/secure-ipl.rst | 22 +++++++++++++++++----- hw/s390x/s390-virtio-ccw.c | 22 ++++++++++++++++++++++ include/hw/s390x/s390-virtio-ccw.h | 1 + qemu-options.hx | 6 +++++- 4 files changed, 45 insertions(+), 6 deletions(-)
PATCH v7 23/29 hw/s390x/ipl: Set IPIB flags for secure IPL
In reply to Zhuoying CaiIf -secure-boot on is specified on the command line option, indicating true secure IPL enabled, set Secure-IPL bit and IPL-Information-Report bit on in IPIB Flags field, and trigger true secure IPL in the S390 BIOS.
Any error that occurs during true secure IPL will cause the IPL to terminate.
--- hw/s390x/ipl.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
hw/s390x/ipl.c +18 -1
@@ -439,6 +439,11 @@ static bool s390_has_certificate(void)return ipl->cert_store.count >0 ; } +static bool s390_secure_boot_enabled (void ) +{ +return S390_CCW_MACHINE (qdev_get_machine ())->secure_boot; +} +static bool s390_build_iplb (DeviceState *dev_st,IplParameterBlock *iplb) {CcwDevice *ccw_dev =NULL ;
@@ -496,6 +501,18 @@ static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)s390_ipl_convert_loadparm ((char *)lp, iplb->loadparm); iplb->flags |=DIAG308_FLAGS_LP_VALID ; +/* +* If secure-boot is enabled, then toggle the secure IPL flags to +* trigger secure boot in the s390 BIOS. +* +* Boot process will terminate if any error occurs during secure boot. +* +* If SIPL is on, IPLIR must also be on. +*/ +if (s390_secure_boot_enabled ()) { + iplb->hdr_flags |= (DIAG308_IPIB_FLAGS_SIPL |DIAG308_IPIB_FLAGS_IPLIR ); + iplb->len =cpu_to_be32 (S390_IPLB_MAX_LEN ); + }/* * Secure boot in audit mode will perform * if certificate(s) exist in the key store.
@@ -505,7 +522,7 @@ static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)* * Results of secure boot will be stored in IIRB. */ -if (s390_has_certificate ()) { + else if (s390_has_certificate()) { iplb->hdr_flags |=DIAG308_IPIB_FLAGS_IPLIR ; iplb->len =cpu_to_be32 (S390_IPLB_MAX_LEN ); }
Signature · 1 line
2.51.1
Re: [PATCH v7 23/29] hw/s390x/ipl: Set IPIB flags for secure IPL
Quoted 1 line · Zhuoying Cai
If
-secure-boot onis specified on the command line option, indicating
Change the "-secure-boot on" to "-M secure-boot=on" now?
Apart from that, the patch looks fine to me.
Quoted 10 lines
true secure IPL enabled, set Secure-IPL bit and IPL-Information-Report bit on in IPIB Flags field, and trigger true secure IPL in the S390 BIOS.
Any error that occurs during true secure IPL will cause the IPL to terminate.
Signed-off-by Zhuoying Cai <[email protected]>--- hw/s390x/ipl.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
Thomas
PATCH v7 24/29 pc-bios/s390-ccw: Handle true secure IPL mode
In reply to Zhuoying CaiWhen secure boot is enabled (-secure-boot on) and certificate(s) are provided, the boot operates in True Secure IPL mode.
Any verification error during True Secure IPL mode will cause the entire boot process to terminate.
Secure IPL in audit mode requires at least one certificate provided in the key store along with necessary facilities. If secure boot is enabled but no certificate is provided, the boot process will also terminate, as this is not a valid secure boot configuration.
Note: True Secure IPL mode is implemented for the SCSI scheme of virtio-blk/virtio-scsi devices.
--- docs/system/s390x/secure-ipl.rst | 13 +++++++++++++ pc-bios/s390-ccw/bootmap.c | 19 ++++++++++++++++--- pc-bios/s390-ccw/main.c | 7 ++++++- pc-bios/s390-ccw/s390-ccw.h | 2 ++ pc-bios/s390-ccw/secure-ipl.c | 4 ++++ pc-bios/s390-ccw/secure-ipl.h | 3 +++ 6 files changed, 44 insertions(+), 4 deletions(-)
docs/system/s390x/secure-ipl.rst +13 -0
@@ -64,3 +64,16 @@ Configuration: .. code-block:: shell qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ... + +Secure Mode +----------- + +When both certificates are provided and ``secure-boot=on`` option is set, +a secure boot is performed with error reporting enabled. The boot process aborts +if any error occurs. + +Configuration: + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,secure-boot=on,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ...
pc-bios/s390-ccw/bootmap.c +16 -3
@@ -738,6 +738,9 @@ static int zipl_run(ScsiBlockPtr *pte) entry = (ComponentEntry *)(&header[1 ]);switch (boot_mode) { +case ZIPL_BOOT_MODE_INVALID : +return -1 ; +case ZIPL_BOOT_MODE_SECURE :case ZIPL_BOOT_MODE_SECURE_AUDIT : rc =zipl_run_secure (&entry, tmp_sec);if (rc) {
@@ -1121,9 +1124,16 @@ ZiplBootMode zipl_mode(uint8_t hdr_flags) {bool sipl_set = hdr_flags &DIAG308_IPIB_FLAGS_SIPL ;bool iplir_set = hdr_flags &DIAG308_IPIB_FLAGS_IPLIR ; +VCStorageSizeBlock *vcssb;if (!sipl_set && iplir_set) {return ZIPL_BOOT_MODE_SECURE_AUDIT ; + }else if (sipl_set && iplir_set) { + vcssb =zipl_secure_get_vcssb (); +if (vcssb ==NULL || vcssb->length ==VCSSB_NO_VC ) { +return ZIPL_BOOT_MODE_INVALID ; + } +return ZIPL_BOOT_MODE_SECURE ; }return ZIPL_BOOT_MODE_NORMAL ;
@@ -1134,7 +1144,8 @@ void zipl_load(void)VDev *vdev =virtio_get_device ();if (vdev->is_cdrom) { -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || + boot_mode ==ZIPL_BOOT_MODE_SECURE ) {panic ("Secure boot from ISO image is not supported!" ); }ipl_iso_el_torito ();
@@ -1143,7 +1154,8 @@ void zipl_load(void) }if (virtio_get_device_type () ==VIRTIO_ID_NET ) { -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || + boot_mode ==ZIPL_BOOT_MODE_SECURE ) {panic ("Virtio net boot device does not support secure boot!" ); }netmain ();
@@ -1156,7 +1168,8 @@ void zipl_load(void)return ; } -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || + boot_mode ==ZIPL_BOOT_MODE_SECURE ) {panic ("ECKD boot device does not support secure boot!" ); }
pc-bios/s390-ccw/main.c +6 -1
@@ -277,10 +277,15 @@ static void ipl_boot_device(void) boot_mode =zipl_mode (iplb->hdr_flags); } +if (boot_mode ==ZIPL_BOOT_MODE_INVALID ) { +panic ("Need at least one certificate for secure boot!" ); + } +switch (cutype) {case CU_TYPE_DASD_3990 :case CU_TYPE_DASD_2107 : -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT ) { +if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || + boot_mode ==ZIPL_BOOT_MODE_SECURE ) {panic ("Passthrough (vfio) CCW device does not support secure boot!" ); }
pc-bios/s390-ccw/s390-ccw.h +2 -0
@@ -83,9 +83,11 @@ int virtio_read(unsigned long sector, void *load_addr);void zipl_load (void );typedef enum ZiplBootMode { +ZIPL_BOOT_MODE_INVALID =-1 ,ZIPL_BOOT_MODE_UNSPECIFIED =0 ,ZIPL_BOOT_MODE_NORMAL =1 ,ZIPL_BOOT_MODE_SECURE_AUDIT =2 , +ZIPL_BOOT_MODE_SECURE =3 , }ZiplBootMode ;extern ZiplBootMode boot_mode;
pc-bios/s390-ccw/secure-ipl.c +4 -0
@@ -296,6 +296,10 @@ static bool check_sclab_presence(uint8_t *sclab_magic, comps->device_entries[comp_index].cei |=S390_IPL_COMPONENT_CEI_INVALID_SCLAB ;/* a missing SCLAB will not be reported in audit mode */ +if (boot_mode ==ZIPL_BOOT_MODE_SECURE ) { +zipl_secure_handle ("Magic does not matched. SCLAB does not exist" ); + } +return false; }
pc-bios/s390-ccw/secure-ipl.h +3 -0
@@ -58,6 +58,9 @@ static inline void zipl_secure_handle(const char *message)case ZIPL_BOOT_MODE_SECURE_AUDIT :IPL_check (false, message);break ; +case ZIPL_BOOT_MODE_SECURE : +panic (message); +break ;default :break ; }
Signature · 1 line
2.51.1
Re: [PATCH v7 24/29] pc-bios/s390-ccw: Handle true secure IPL mode
Quoted 29 lines · Zhuoying Cai
When secure boot is enabled (-secure-boot on) and certificate(s) are provided, the boot operates in True Secure IPL mode.
Any verification error during True Secure IPL mode will cause the entire boot process to terminate.
Secure IPL in audit mode requires at least one certificate provided in the key store along with necessary facilities. If secure boot is enabled but no certificate is provided, the boot process will also terminate, as this is not a valid secure boot configuration.
Note: True Secure IPL mode is implemented for the SCSI scheme of virtio-blk/virtio-scsi devices.
Signed-off-by Zhuoying Cai <[email protected]>--- docs/system/s390x/secure-ipl.rst | 13 +++++++++++++ pc-bios/s390-ccw/bootmap.c | 19 ++++++++++++++++--- pc-bios/s390-ccw/main.c | 7 ++++++- pc-bios/s390-ccw/s390-ccw.h | 2 ++ pc-bios/s390-ccw/secure-ipl.c | 4 ++++ pc-bios/s390-ccw/secure-ipl.h | 3 +++ 6 files changed, 44 insertions(+), 4 deletions(-)docs/system/s390x/secure-ipl.rst +0
-0
docs/system/s390x/secure-ipl.rst Configuration: · line 71
Quoted 8 lines
docs/system/s390x/secure-ipl.rst +5
-0@@ -64,3 +64,16 @@ Configuration: .. code-block:: shell qemu-system-s390x -machine s390-ccw-virtio,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ... + +Secure Mode +----------- + +When both certificates are provided and ``secure-boot=on`` option is set,
When I started reading this sentence "When both certificates are provided" I wondered why two certificates are involved here ... so this sentence is somewhat hard to digest. Could you please rephrase it?
Quoted 8 lines
docs/system/s390x/secure-ipl.rst +8
-0+a secure boot is performed with error reporting enabled. The boot process aborts +if any error occurs. + +Configuration: + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,secure-boot=on,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ...
...
pc-bios/s390-ccw/secure-ipl.c static bool check_sclab_presence(uint8_t *sclab_magic, · line 300
Quoted 10 lines
pc-bios/s390-ccw/secure-ipl.c +2
-0@@ -296,6 +296,10 @@ static bool check_sclab_presence(uint8_t *sclab_magic, comps->device_entries[comp_index].cei |=S390_IPL_COMPONENT_CEI_INVALID_SCLAB ;/* a missing SCLAB will not be reported in audit mode */ +if (boot_mode ==ZIPL_BOOT_MODE_SECURE ) { +zipl_secure_handle ("Magic does not matched. SCLAB does not exist" );
s/matched/match/
pc-bios/s390-ccw/secure-ipl.c static bool check_sclab_presence(uint8_t *sclab_magic, · line 304
Quoted 4 lines
pc-bios/s390-ccw/secure-ipl.c +2
-0+ } +return false; }
Thomas
PATCH v7 25/29 pc-bios/s390-ccw: Handle secure boot with multiple boot devices
In reply to Zhuoying CaiThe current approach to enable secure boot relies on providing secure-boot and boot-certs parameters of s390-ccw-virtio machine type option, which apply to all boot devices.
With the possibility of multiple boot devices, secure boot expects all provided devices to be supported and eligible (e.g., virtio-blk/virtio-scsi using the SCSI scheme).
If multiple boot devices are provided and include an unsupported (e.g., ECKD, VFIO) or a non-eligible (e.g., Net) device, the boot process will terminate with an error logged to the console.
--- pc-bios/s390-ccw/bootmap.c | 31 +++++++++------- pc-bios/s390-ccw/main.c | 73 ++++++++++++++++++++++++++++++++++--- pc-bios/s390-ccw/s390-ccw.h | 1 + 3 files changed, 86 insertions(+), 19 deletions(-)
pc-bios/s390-ccw/bootmap.c +18 -13
@@ -1139,25 +1139,35 @@ ZiplBootMode zipl_mode(uint8_t hdr_flags)return ZIPL_BOOT_MODE_NORMAL ; } +int zipl_check_scsi_mbr_magic (void ) +{ +ScsiMbr *mbr = (void *)sec; + +/* Grab the MBR */ +memset (sec,FREE_SPACE_FILLER ,sizeof (sec)); +if (virtio_read (0 , mbr)) { +puts ("Cannot read block 0" ); +return -EIO ; + } + +if (!magic_match (mbr->magic,ZIPL_MAGIC )) { +return -1 ; + } + +return 0 ; +} +void zipl_load (void ) {VDev *vdev =virtio_get_device ();if (vdev->is_cdrom) { -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || - boot_mode ==ZIPL_BOOT_MODE_SECURE ) { -panic ("Secure boot from ISO image is not supported!" ); - }ipl_iso_el_torito ();puts ("Failed to IPL this ISO image!" );return ; }if (virtio_get_device_type () ==VIRTIO_ID_NET ) { -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || - boot_mode ==ZIPL_BOOT_MODE_SECURE ) { -panic ("Virtio net boot device does not support secure boot!" ); - }netmain ();puts ("Failed to IPL from this network!" );return ;
@@ -1168,11 +1178,6 @@ void zipl_load(void)return ; } -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || - boot_mode ==ZIPL_BOOT_MODE_SECURE ) { -panic ("ECKD boot device does not support secure boot!" ); - } -switch (virtio_get_device_type ()) {case VIRTIO_ID_BLOCK :zipl_load_vblk ();
pc-bios/s390-ccw/main.c +67 -6
95 lines not on this page · raw message
pc-bios/s390-ccw/s390-ccw.h +1 -0
@@ -93,6 +93,7 @@ typedef enum ZiplBootMode {extern ZiplBootMode boot_mode;ZiplBootMode zipl_mode (uint8_t hdr_flags); +int zipl_check_scsi_mbr_magic (void );/* jump2ipl.c */ void write_reset_psw (uint64_t psw);
Signature · 1 line
2.51.1
Re: [PATCH v7 25/29] pc-bios/s390-ccw: Handle secure boot with multiple boot devices
Quoted 7 lines · Zhuoying Cai
The current approach to enable secure boot relies on providing secure-boot and boot-certs parameters of s390-ccw-virtio machine type option, which apply to all boot devices.
With the possibility of multiple boot devices, secure boot expects all provided devices to be supported and eligible (e.g., virtio-blk/virtio-scsi using the SCSI scheme).
Just double-checking: Wouldn't it be easier to do this on the QEMU side already? Or is there something in a spec somewhere saying that this has to be done by the guest?
Quoted 16 lines
If multiple boot devices are provided and include an unsupported (e.g., ECKD, VFIO) or a non-eligible (e.g., Net) device, the boot process will terminate with an error logged to the console.
Signed-off-by Zhuoying Cai <[email protected]>--- pc-bios/s390-ccw/bootmap.c | 31 +++++++++------- pc-bios/s390-ccw/main.c | 73 ++++++++++++++++++++++++++++++++++--- pc-bios/s390-ccw/s390-ccw.h | 1 + 3 files changed, 86 insertions(+), 19 deletions(-)pc-bios/s390-ccw/bootmap.c +0
-0
pc-bios/s390-ccw/bootmap.c ZiplBootMode zipl_mode(uint8_t hdr_flags) · line 1147
Quoted 9 lines
pc-bios/s390-ccw/bootmap.c +6
-0@@ -1139,25 +1139,35 @@ ZiplBootMode zipl_mode(uint8_t hdr_flags)return ZIPL_BOOT_MODE_NORMAL ; } +int zipl_check_scsi_mbr_magic (void ) +{ +ScsiMbr *mbr = (void *)sec; + +/* Grab the MBR */ +memset (sec,FREE_SPACE_FILLER ,sizeof (sec));
Mixing different pointer types that point to the same buffer always make me nervous - with regards to GCC's strict aliasing optimizations. (see e.g. https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule if you don't know that yet).
Since you're only interested in the MBR magic here, I suggest to replace the above memset with:
memset(mbr->magic, FREE_SPACE_FILLER, sizeof(mbr->magic));
That's certainly less risky and also faster.
Quoted 99 lines
pc-bios/s390-ccw/bootmap.c +12
-13+if (virtio_read (0 , mbr)) { +puts ("Cannot read block 0" ); +return -EIO ; + } + +if (!magic_match (mbr->magic,ZIPL_MAGIC )) { +return -1 ; + } + +return 0 ; +} +void zipl_load (void ) {VDev *vdev =virtio_get_device ();if (vdev->is_cdrom) { -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || - boot_mode ==ZIPL_BOOT_MODE_SECURE ) { -panic ("Secure boot from ISO image is not supported!" ); - }ipl_iso_el_torito ();puts ("Failed to IPL this ISO image!" );return ; }if (virtio_get_device_type () ==VIRTIO_ID_NET ) { -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || - boot_mode ==ZIPL_BOOT_MODE_SECURE ) { -panic ("Virtio net boot device does not support secure boot!" ); - }netmain ();puts ("Failed to IPL from this network!" );return ;@@ -1168,11 +1178,6 @@ void zipl_load(void)return ; } -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || - boot_mode ==ZIPL_BOOT_MODE_SECURE ) { -panic ("ECKD boot device does not support secure boot!" ); - } -switch (virtio_get_device_type ()) {case VIRTIO_ID_BLOCK :zipl_load_vblk ();pc-bios/s390-ccw/main.c +36
-1@@ -271,8 +271,43 @@ static int virtio_setup(void)return ret; } -static void ipl_boot_device (void ) +static void validate_secure_boot_device (void ) +{ +switch (cutype) { +case CU_TYPE_DASD_3990 : +case CU_TYPE_DASD_2107 : +panic ("Passthrough (vfio) CCW device does not support secure boot!" ); +break ; +case CU_TYPE_VIRTIO : +if (virtio_setup () ==0 ) { +VDev *vdev =virtio_get_device (); + +if (vdev->is_cdrom) { +panic ("Secure boot from ISO image is not supported!" ); + } + +if (virtio_get_device_type () ==VIRTIO_ID_NET ) { +panic ("Virtio net boot device does not support secure boot!" ); + } + +if (zipl_check_scsi_mbr_magic ()) { +panic ("ECKD boot device does not support secure boot!" ); + } + } +break ; +default : +panic ("Secure boot from unexpected device type is not supported!" ); + } + +printf ("SCSI boot device supports secure boot.\n" ); +} + +static void check_secure_boot_support (void ) { +bool have_iplb_copy; +IplParameterBlock *iplb_copy; +QemuIplParameters qipl_copy; +if (boot_mode ==ZIPL_BOOT_MODE_UNSPECIFIED ) { boot_mode =zipl_mode (iplb->hdr_flags); }@@ -281,14 +316,38 @@ static void ipl_boot_device(void)panic ("Need at least one certificate for secure boot!" ); }
pc-bios/s390-ccw/main.c static void ipl_boot_device(void) · line 328
Quoted 10 lines
pc-bios/s390-ccw/main.c +10
-0+if (boot_mode ==ZIPL_BOOT_MODE_NORMAL ) { +return ; + } + +/* +* Store copies of have_iplb, iplb and qipl. +* They will be updated in load_next_iplb(). +*/ + have_iplb_copy = have_iplb; + iplb_copy =malloc (sizeof (IplParameterBlock));
Please add:
IPL_assert(iplb_copy != NULL, "out of memory");
or so.
Quoted 5 lines
pc-bios/s390-ccw/main.c +5
-0+memcpy (&qipl_copy, &qipl,sizeof (QemuIplParameters)); +memcpy (iplb_copy, iplb,sizeof (IplParameterBlock)); + +while (have_iplb_copy) { +if (have_iplb_copy &&find_boot_device ()) {
pc-bios/s390-ccw/main.c static void ipl_boot_device(void) · line 344
Quoted 10 lines
pc-bios/s390-ccw/main.c +10
-0+validate_secure_boot_device (); + } + have_iplb_copy =load_next_iplb (); + } + +memcpy (&qipl, &qipl_copy,sizeof (QemuIplParameters)); +memcpy (iplb, iplb_copy,sizeof (IplParameterBlock)); + +free (iplb_copy); +}
Do we really have to check all devices in advance here? I don't quite get the logic here yet, if we check the MBR of each and every disk in advance, that means that each of the devices has to be bootable in secure mode. But what sense does it make to specify multiple devices if all of them are bootable, i.e. we will always boot from the first given device?
Should we maybe rather simply disallow multiple boot devices from the QEMU side already and only allow one bootable device when running in secure mode?
Thomas
Quoted 34 lines
pc-bios/s390-ccw/main.c +4
-5+static void ipl_boot_device (void ) +{switch (cutype) {case CU_TYPE_DASD_3990 :case CU_TYPE_DASD_2107 : -if (boot_mode ==ZIPL_BOOT_MODE_SECURE_AUDIT || - boot_mode ==ZIPL_BOOT_MODE_SECURE ) { -panic ("Passthrough (vfio) CCW device does not support secure boot!" ); - } -dasd_ipl (blk_schid, cutype);break ;case CU_TYPE_VIRTIO :@@ -338,6 +397,8 @@ void main(void)probe_boot_device (); } +check_secure_boot_support (); +while (have_iplb) {boot_setup ();if (have_iplb &&find_boot_device ()) {pc-bios/s390-ccw/s390-ccw.h +1
-0@@ -93,6 +93,7 @@ typedef enum ZiplBootMode {extern ZiplBootMode boot_mode;ZiplBootMode zipl_mode (uint8_t hdr_flags); +int zipl_check_scsi_mbr_magic (void );/* jump2ipl.c */ void write_reset_psw (uint64_t psw);
PATCH v7 26/29 hw/s390x/ipl: Handle secure boot without specifying a boot device
In reply to Zhuoying CaiIf secure boot in audit mode or True Secure IPL mode is enabled without specifying a boot device, the boot process will terminate with an error.
--- hw/s390x/ipl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
hw/s390x/ipl.c +10 -0
@@ -771,6 +771,16 @@ void s390_ipl_prepare_cpu(S390CPU *cpu) }if (!ipl->iplb_valid) { ipl->iplb_valid =s390_init_all_iplbs (ipl); + +/* +* Secure IPL without specifying a boot device. +* IPLB is not generated if no boot device is defined. +*/ +if ((s390_has_certificate () ||s390_secure_boot_enabled ()) && + !ipl->iplb_valid) { +error_report ("No boot device defined for Secure IPL" ); +exit (1 ); + } }else { ipl->qipl.chain_len =0 ; }
Signature · 1 line
2.51.1
Re: [PATCH v7 26/29] hw/s390x/ipl: Handle secure boot without specifying a boot device
Quoted 19 lines · Zhuoying Cai
If secure boot in audit mode or True Secure IPL mode is enabled without specifying a boot device, the boot process will terminate with an error.
Signed-off-by Zhuoying Cai <[email protected]>--- hw/s390x/ipl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)hw/s390x/ipl.c +3
-0@@ -771,6 +771,16 @@ void s390_ipl_prepare_cpu(S390CPU *cpu) }if (!ipl->iplb_valid) { ipl->iplb_valid =s390_init_all_iplbs (ipl); + +/* +* Secure IPL without specifying a boot device.
hw/s390x/ipl.c void s390_ipl_prepare_cpu(S390CPU *cpu) · line 786
Quoted 10 lines
hw/s390x/ipl.c +7
-0+* IPLB is not generated if no boot device is defined. +*/ +if ((s390_has_certificate () ||s390_secure_boot_enabled ()) && + !ipl->iplb_valid) { +error_report ("No boot device defined for Secure IPL" ); +exit (1 ); + } }else { ipl->qipl.chain_len =0 ; }
PATCH v7 27/29 tests/functional/s390x: Add secure IPL functional test
In reply to Zhuoying CaiAdd functional test for secure IPL.
--- tests/functional/s390x/meson.build | 2 + tests/functional/s390x/test_secure_ipl.py | 140 ++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 tests/functional/s390x/test_secure_ipl.py
tests/functional/s390x/meson.build +2 -0
@@ -2,6 +2,7 @@ test_s390x_timeouts = { 'ccw_virtio' : 420, + 'secure_ipl' : 280, } tests_s390x_system_quick = [
@@ -12,6 +13,7 @@ tests_s390x_system_thorough = [ 'ccw_virtio', 'pxelinux', 'replay', + 'secure_ipl', 'topology', 'tuxrun', ]
tests/functional/s390x/test_secure_ipl.py new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
2.51.1
Re: [PATCH v7 27/29] tests/functional/s390x: Add secure IPL functional test
Quoted 1 line · Zhuoying Cai
Add functional test for secure IPL.
Thanks a lot for adding the test, I think this will be helpful!
Quoted 2 lines
Signed-off-by Zhuoying Cai <[email protected]>---
...
tests/functional/s390x/test_secure_ipl.py line 7
Quoted 13 lines
tests/functional/s390x/test_secure_ipl.py new file +7
-0@@ -0,0 +1,140 @@ +#!/usr/bin/env python3 +# +# s390x Secure IPL functional test: validates secure-boot verification results +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import os
As far as I can see, you never use any function from "os", so you can drop that import.
tests/functional/s390x/test_secure_ipl.py line 13
Quoted 6 lines
tests/functional/s390x/test_secure_ipl.py new file +6
-0+import time + +from qemu_test import QemuSystemTest, Asset +from qemu_test import exec_command_and_wait_for_pattern, exec_command +from qemu_test import wait_for_console_pattern, skipBigDataTest +from qemu.utils import kvm_available, tcg_available
You never use kvm_available or tcg_available, so please drop that import.
Quoted 7 lines
tests/functional/s390x/test_secure_ipl.py new file +7
-0+class S390xSecureIpl(QemuSystemTest): + ASSET_F40_QCOW2 = Asset( + ('https://archives.fedoraproject.org/pub/archive/' + 'fedora-secondary/releases/40/Server/s390x/images/' + 'Fedora-Server-KVM-40-1.14.s390x.qcow2'), + '091c232a7301be14e19c76ce9a0c1cbd2be2c4157884a731e1fc4f89e7455a5f') +
tests/functional/s390x/test_secure_ipl.py line 31
Quoted 10 lines
tests/functional/s390x/test_secure_ipl.py new file +10
-0+ # Boot a temporary VM to set up secure IPL image: + # - Create certificate + # - Sign stage3 binary and kernel + # - Run zipl + # - Extract certificate + # Small delay added to allow the guest prompt/filesystem updates to settle + def setup_s390x_secure_ipl(self): + temp_vm = self.get_vm(name='sipl_setup') + temp_vm.set_machine('s390-ccw-virtio') + self.require_accelerator('kvm')
I'd maybe move the self.require_accelerator('kvm') to the beginning of the test_s390x_secure_ipl() function already.
Quoted 47 lines
tests/functional/s390x/test_secure_ipl.py new file +47
-0+ self.qcow2_path = self.ASSET_F40_QCOW2.fetch() + + temp_vm.set_console() + temp_vm.add_args('-nographic', + '-accel', 'kvm', + '-m', '1024', + '-drive', + f'id=drive0,if=none,format=qcow2,file={self.qcow2_path}', + '-device', 'virtio-blk-ccw,drive=drive0,bootindex=1') + temp_vm.launch() + + # Initial root account setup (Fedora first boot screen) + self.root_password = 'fedora40password' + wait_for_console_pattern(self, 'Please make a selection from the above', + vm=temp_vm) + exec_command_and_wait_for_pattern(self, '4', 'Password:', vm=temp_vm) + exec_command_and_wait_for_pattern(self, self.root_password, + 'Password (confirm):', vm=temp_vm) + exec_command_and_wait_for_pattern(self, self.root_password, + 'Please make a selection from the above', + vm=temp_vm) + + # Login as root + exec_command_and_wait_for_pattern(self, 'c', 'localhost login:', vm=temp_vm) + exec_command_and_wait_for_pattern(self, 'root', 'Password:', vm=temp_vm) + exec_command_and_wait_for_pattern(self, self.root_password, + '[root@localhost ~]#', vm=temp_vm) + + # Certificate generation + time.sleep(1) + exec_command_and_wait_for_pattern(self, + 'openssl version', 'OpenSSL 3.2.1 30', + vm=temp_vm) + exec_command_and_wait_for_pattern(self, + 'openssl req -new -x509 -newkey rsa:2048 ' + '-keyout mykey.pem -outform PEM -out mycert.pem ' + '-days 36500 -subj "/CN=My Name/" -nodes -verbose', + 'Writing private key to \'mykey.pem\'', vm=temp_vm) + + # Install kernel-devel (needed for sign-file) + exec_command_and_wait_for_pattern(self, + 'sudo dnf install kernel-devel-$(uname -r) -y', + 'Complete!', vm=temp_vm) + time.sleep(1) + exec_command_and_wait_for_pattern(self, + 'ls /usr/src/kernels/$(uname -r)/scripts/', + 'sign-file', vm=temp_vm)
tests/functional/s390x/test_secure_ipl.py line 89
Quoted 10 lines
tests/functional/s390x/test_secure_ipl.py new file +10
-0+ + # Sign stage3 binary and kernel + exec_command(self, '/usr/src/kernels/$(uname -r)/scripts/sign-file ' + 'sha256 mykey.pem mycert.pem /lib/s390-tools/stage3.bin', + vm=temp_vm) + time.sleep(1) + exec_command(self, '/usr/src/kernels/$(uname -r)/scripts/sign-file ' + 'sha256 mykey.pem mycert.pem /boot/vmlinuz-$(uname -r)', + vm=temp_vm) + time.sleep(1)
Can you maybe work-around the above time.sleep() commands by waiting for the shell prompt inbetween? wait_for_console_pattern(self, '[root@localhost ~]#') or so ?
Quoted 5 lines
tests/functional/s390x/test_secure_ipl.py new file +5
-0+ # Run zipl to prepare for secure boot + exec_command_and_wait_for_pattern(self, 'zipl --secure 1 -VV', 'Done.', + vm=temp_vm) + + # Extract certificate to host
tests/functional/s390x/test_secure_ipl.py line 105
Quoted 10 lines
tests/functional/s390x/test_secure_ipl.py new file +10
-0+ out = exec_command_and_wait_for_pattern(self, 'cat mycert.pem', + '-----END CERTIFICATE-----', + vm=temp_vm) + # strip first line to avoid console echo artifacts + cert = "\n".join(out.decode("utf-8").splitlines()[1:]) + self.log.info("%s", cert) + + self.cert_path = self.scratch_file("mycert.pem") + + with open(self.cert_path, 'w') as file_object:
Please add an encoding='utf-8' parameter to open() to avoid a warning from pylint.
...
tests/functional/s390x/test_secure_ipl.py line 131
Quoted 2 lines
tests/functional/s390x/test_secure_ipl.py new file +2
-0+ wait_for_console_pattern(self, verified_output); + wait_for_console_pattern(self, verified_output);
Please drop the semicolons at the end here.
(Hint: pylint or flake8 can help to detect such coding-style issues)
Thanks,
Thomas
PATCH v7 28/29 docs/specs: Add secure IPL documentation
In reply to Zhuoying CaiAdd documentation for secure IPL
--- docs/specs/s390x-secure-ipl.rst | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
docs/specs/s390x-secure-ipl.rst +55 -0
@@ -1,5 +1,60 @@ .. SPDX-License-Identifier: GPL-2.0-or-later +s390 Secure IPL +=============== + +Secure IPL (a.k.a. secure boot) enables s390-ccw virtual machines to +leverage qcrypto libraries and z/Architecture emulations to verify the +integrity of signed kernels. The qcrypto libraries are used to perform +certificate validation and signature-verification, whereas the +z/Architecture emulations are used to ensure secure IPL data has not +been tampered with, convey data between QEMU and userspace, and set up +the relevant secure IPL data structures with verification results. + +To find out more about using this feature, see +``docs/system/s390x/secure-ipl.rst``. + +Note that "userspace" will refer to the s390-ccw BIOS unless stated +otherwise. + +Both QEMU and userspace work in tandem to perform secure IPL. The Secure +Loading Attributes Facility (SCLAF) is used to check the Secure Code +Loading Attribute Block (SCLAB) and ensure that secure IPL data has not +been tampered with. DIAGNOSE 'X'320' is invoked by userspace to query +the certificate store info and retrieve specific certificates from QEMU. +DIAGNOSE 'X'508' is used by userspace to leverage qcrypto libraries to +perform signature-verification in QEMU. Lastly, userspace generates and +appends an IPL Information Report Block (IIRB) at the end of the IPL +Parameter Block, which is used by the kernel to store signed and +verified entries. + +The logical steps are as follows: + +- Userspace reads data payload from disk (e.g. stage3 boot loader, kernel) +- Userspace checks the validity of the SCLAB +- Userspace invokes DIAG 508 subcode 1 and provides it the payload +- QEMU handles DIAG 508 request by reading the payload and retrieving the + certificate store +- QEMU DIAG 508 utilizes qcrypto libraries to perform signature-verification on + the payload, attempting with each cert in the store (until success or + exhausted) +- QEMU DIAG 508 returns: + + - success: index of cert used to verify payload + - failure: error code + +- Userspace responds to this operation: + + - success: retrieves cert from store via DIAG 320 using returned index + - failure: reports with warning (audit mode), aborts with error (secure mode) + +- Userspace appends IIRB at the end of the IPLB +- Userspace kicks off IPL + +More information regarding the respective DIAGNOSE commands and IPL data +structures are outlined within this document. + + s390 Certificate Store and Functions ====================================
Signature · 1 line
2.51.1
Re: [PATCH v7 28/29] docs/specs: Add secure IPL documentation
Quoted 22 lines · Zhuoying Cai
Add documentation for secure IPL
Signed-off-by Collin Walling <[email protected]>--- docs/specs/s390x-secure-ipl.rst | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)docs/specs/s390x-secure-ipl.rst +8
-0@@ -1,5 +1,60 @@ .. SPDX-License-Identifier: GPL-2.0-or-later +s390 Secure IPL +=============== + +Secure IPL (a.k.a. secure boot) enables s390-ccw virtual machines to +leverage qcrypto libraries and z/Architecture emulations to verify the +integrity of signed kernels. The qcrypto libraries are used to perform +certificate validation and signature-verification, whereas the +z/Architecture emulations are used to ensure secure IPL data has not
docs/specs/s390x-secure-ipl.rst line 20
Quoted 10 lines
docs/specs/s390x-secure-ipl.rst +10
-0+been tampered with, convey data between QEMU and userspace, and set up +the relevant secure IPL data structures with verification results. + +To find out more about using this feature, see +``docs/system/s390x/secure-ipl.rst``. + +Note that "userspace" will refer to the s390-ccw BIOS unless stated +otherwise. + +Both QEMU and userspace work in tandem to perform secure IPL. The Secure
I'm not a native speaker, but I'd maybe rather say "work in cooperation" instead of "work in tandem" ?
Thomas
PATCH v7 29/29 docs/system/s390x: Add secure IPL documentation
In reply to Zhuoying CaiAdd documentation for secure IPL
--- docs/system/s390x/secure-ipl.rst | 96 ++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+)
docs/system/s390x/secure-ipl.rst +96 -0
@@ -1,5 +1,21 @@ .. SPDX-License-Identifier: GPL-2.0-or-later +s390 Secure IPL +=============== + +Secure IPL, also known as secure boot, enables s390-ccw virtual machines to +verify the integrity of guest kernels. + +For technical details of this feature, see ``docs/specs/s390x-secure-ipl.rst``. + +This document explains how to use secure IPL with s390x in QEMU. It covers +new command line options for providing certificates and enabling secure IPL, +the different IPL modes (Normal, Audit, and Secure), and system requirements. + +A quickstart guide is provided to demonstrate how to generate certificates, +sign images, and start a guest in Secure Mode. + + Secure IPL Command Line Options ===============================
@@ -77,3 +93,83 @@ Configuration: .. code-block:: shell qemu-system-s390x -machine s390-ccw-virtio,secure-boot=on,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ... + + +Constraints +=========== + +The following constraints apply when attempting to secure IPL an s390 guest: + +- z16 CPU model +- certificates must be in X.509 PEM format +- only support for SCSI scheme of virtio-blk/virtio-scsi devices +- a boot device must be specified +- any unsupported devices (e.g., ECKD and VFIO) or non-eligible devices (e.g., + Net) will cause the entire boot process terminating early with an error + logged to the console. + + +Secure IPL Quickstart +===================== + +Build QEMU with gnutls enabled +------------------------------- + +.. code-block:: shell + + ./configure … --enable-gnutls + +Generate certificate (e.g. via certtool) +---------------------------------------- + +A private key is required before generating a certificate. This key must be kept +secure and confidential. + +Use an RSA private key for signing. + +.. code-block:: shell + + certtool --generate-privkey > key.pem + +A self-signed certificate requires the organization name. Use the ``cert.info`` +template to pre-fill values and avoid interactive prompts from certtool. + +.. code-block:: shell + + cat > cert.info <<EOF + cn = "My Name" + expiration_days = 36500 + cert_signing_key + EOF + + certtool --generate-self-signed \ + --load-privkey key.pem \ + --template cert.info \ + --hash=SHA256 \ + --outfile cert.pem + +Sign Images (e.g. via sign-file) +-------------------------------- + +- signing must be performed on a KVM guest filesystem +- sign-file script used in the example below is located within the kernel source + repo + +.. code-block:: shell + + ./sign-file sha256 key.pem cert.pem /boot/vmlinuz-… + ./sign-file sha256 key.pem cert.pem /usr/lib/s390-tools/stage3.bin + +Run zipl with secure boot enabled +--------------------------------- + +.. code-block:: shell + + zipl --secure 1 -V + +Start Guest with Cmd Options +---------------------------- + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,secure-boot=on,boot-certs.0.path=cert.pem ...
Signature · 1 line
2.51.1
Re: [PATCH v7 29/29] docs/system/s390x: Add secure IPL documentation
Quoted 5 lines · Zhuoying Cai
Add documentation for secure IPL
Signed-off-by Collin Walling <[email protected]>Signed-off-by Zhuoying Cai <[email protected]>---
...
docs/system/s390x/secure-ipl.rst Configuration: · line 103
Quoted 12 lines
docs/system/s390x/secure-ipl.rst +8
-0@@ -77,3 +93,83 @@ Configuration: .. code-block:: shell qemu-system-s390x -machine s390-ccw-virtio,secure-boot=on,boot-certs.0.path=/.../qemu/certs,boot-certs.1.path=/another/path/cert.pem ... + + +Constraints +=========== + +The following constraints apply when attempting to secure IPL an s390 guest: + +- z16 CPU model
Should this mention the "qemu" CPU model, too?
docs/system/s390x/secure-ipl.rst Configuration: · line 108
Quoted 5 lines
docs/system/s390x/secure-ipl.rst +5
-0+- certificates must be in X.509 PEM format +- only support for SCSI scheme of virtio-blk/virtio-scsi devices +- a boot device must be specified +- any unsupported devices (e.g., ECKD and VFIO) or non-eligible devices (e.g., + Net) will cause the entire boot process terminating early with an error
s/(e.g., Net)/(e.g. network)/
Quoted 52 lines
docs/system/s390x/secure-ipl.rst +52
-0+ logged to the console. + + +Secure IPL Quickstart +===================== + +Build QEMU with gnutls enabled +------------------------------- + +.. code-block:: shell + + ./configure … --enable-gnutls + +Generate certificate (e.g. via certtool) +---------------------------------------- + +A private key is required before generating a certificate. This key must be kept +secure and confidential. + +Use an RSA private key for signing. + +.. code-block:: shell + + certtool --generate-privkey > key.pem + +A self-signed certificate requires the organization name. Use the ``cert.info`` +template to pre-fill values and avoid interactive prompts from certtool. + +.. code-block:: shell + + cat > cert.info <<EOF + cn = "My Name" + expiration_days = 36500 + cert_signing_key + EOF + + certtool --generate-self-signed \ + --load-privkey key.pem \ + --template cert.info \ + --hash=SHA256 \ + --outfile cert.pem + +Sign Images (e.g. via sign-file) +-------------------------------- + +- signing must be performed on a KVM guest filesystem +- sign-file script used in the example below is located within the kernel source + repo + +.. code-block:: shell + + ./sign-file sha256 key.pem cert.pem /boot/vmlinuz-…
docs/system/s390x/secure-ipl.rst Configuration: · line 170
Quoted 10 lines
docs/system/s390x/secure-ipl.rst +10
-0+ ./sign-file sha256 key.pem cert.pem /usr/lib/s390-tools/stage3.bin + +Run zipl with secure boot enabled +--------------------------------- + +.. code-block:: shell + + zipl --secure 1 -V + +Start Guest with Cmd Options
"Command line options for starting the guest" ?
docs/system/s390x/secure-ipl.rst Configuration: · line 175
Quoted 5 lines
docs/system/s390x/secure-ipl.rst +5
-0+---------------------------- + +.. code-block:: shell + + qemu-system-s390x -machine s390-ccw-virtio,secure-boot=on,boot-certs.0.path=cert.pem ...
Thomas