igvm: handle optional directives
v2 cover igvm: handle optional directives
This series fixes the handling for optional directives, that are defined by the igvm spec. Currently, because the optional bit is not masked, any directive is rejected as unrecognized.
I opened a PR to export the optional bit in the igvm repository and it has been merged. (https://github.com/microsoft/igvm/pull/121)
I'm picking up igvm: replace raw uint32_t with igvm library types [1] as there is a conflict on the qigvm_handler signature between the two patches.
[1] https://lore.kernel.org/qemu-devel/[email protected]
---
Changes in v2:
- Introduced two helpers: one to mask and one to check if the optional
bit is set
- Protected IGVM_VHT_OPTIONAL_BIT using ifndef as it has been defined
in the IGVM repo https://github.com/microsoft/igvm/pull/121
- Rebased to latest upstream
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
---
Luigi Leonardi (2):
igvm: replace raw uint32_t with igvm library types
igvm: fix handling of optional variable header types
backends/igvm.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-)
---
base-commit: cc329c491768b2d91eb0b0984f3baa0bf805776d
change-id: 20260525-igvm_optional-ca1592b613be
Best regards,
Signature · 1 line
Luigi Leonardi <[email protected]>
PATCH v2 1/2 igvm: replace raw uint32_t with igvm library types
Use IgvmVariableHeaderType and IgvmHeaderSection in QIGVMHandler and qigvm_handler() instead of plain uint32_t, so that each field's purpose is clear from its type.
--- backends/igvm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
backends/igvm.c +3 -3
@@ -102,8 +102,8 @@ static int qigvm_initialization_guest_policy(QIgvm *ctx, Error **errp);struct QIGVMHandler { -uint32_t type; -uint32_t section; +IgvmVariableHeaderType type; +IgvmHeaderSection section;int (*handler)(QIgvm *ctx,const uint8_t *header_data,Error **errp); };
@@ -132,7 +132,7 @@ static struct QIGVMHandler handlers[] = { qigvm_directive_madt }, }; -static int qigvm_handler (QIgvm *ctx,uint32_t type,Error **errp) +static int qigvm_handler (QIgvm *ctx,IgvmVariableHeaderType type,Error **errp) {size_t handler;IgvmHandle header_handle;
Signature · 1 line
2.54.0
Re: [PATCH v2 1/2] igvm: replace raw uint32_t with igvm library types
Quoted 10 lines · Luigi Leonardi
Use IgvmVariableHeaderType and IgvmHeaderSection in QIGVMHandler and
qigvm_handler()instead of plainuint32_t, so that each field's purpose is clear from its type.Reviewed-by Stefano Garzarella <[email protected]>Reviewed-by Ani Sinha <[email protected]>Signed-off-by Luigi Leonardi <[email protected]>--- backends/igvm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
PATCH v2 2/2 igvm: fix handling of optional variable header types
In reply to Luigi LeonardiThe IGVM spec defines bit 31 of the variable header type as an optional flag: if set, a loader that does not recognize the header type may safely skip it. If clear, the loader must reject the file.
Currently, the optional bit is not stripped before comparing header types, so headers with the bit set fail to match any known type and are rejected.
Mask bit 31 before comparing header types throughout the IGVM loader, and skip with a warning any unrecognized header that has the optional bit set.
--- backends/igvm.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-)
backends/igvm.c +33 -4
@@ -26,6 +26,25 @@#include <igvm/igvm.h> #include <igvm/igvm_defs.h> +#ifndef IGVM_VHT_OPTIONAL_BIT +#define IGVM_VHT_OPTIONAL_BIT (1U << 31) +#endif + +/* +* Bit 31 of the variable header type indicates that the header is +* optional and can be safely ignored by a loader that does not +* support it. If the bit is clear, the file cannot be loaded. +* https://docs.rs/igvm_defs/0.4.0/igvm_defs/struct.IgvmVariableHeaderType.html +*/ +static IgvmVariableHeaderType igvm_vht_type (IgvmVariableHeaderType type) +{ +return type & ~IGVM_VHT_OPTIONAL_BIT ; +} + +static bool igvm_vht_optional (IgvmVariableHeaderType type) +{ +return !!(type &IGVM_VHT_OPTIONAL_BIT ); +}/* * Some directives are specific to particular confidential computing platforms.
@@ -132,12 +151,14 @@ static struct QIGVMHandler handlers[] = { qigvm_directive_madt }, }; -static int qigvm_handler (QIgvm *ctx,IgvmVariableHeaderType type,Error **errp) +static int qigvm_handler (QIgvm *ctx,IgvmVariableHeaderType raw_type, +Error **errp) {size_t handler;IgvmHandle header_handle;const uint8_t *header_data;int result; +IgvmVariableHeaderType type =igvm_vht_type (raw_type);for (handler =0 ; handler <G_N_ELEMENTS (handlers); handler++) {if (handlers[handler].type != type) {
@@ -166,6 +187,13 @@ static int qigvm_handler(QIgvm *ctx, IgvmVariableHeaderType type, Error **errp)igvm_free_buffer (ctx->file, header_handle);return result; } + +if (igvm_vht_optional (raw_type)) { +warn_report ("IGVM: Skipping unsupported optional header type 0x%" +PRIX32 , type); +return 0 ; + } + error_setg(errp, "IGVM: Unknown header type encountered when processing file: " "(type 0x%X)",
@@ -787,6 +815,7 @@ static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp) header_index++) {IgvmVariableHeaderType typ =igvm_get_header_type ( ctx->file,IGVM_HEADER_SECTION_PLATFORM , header_index); + typ =igvm_vht_type (typ);if (typ ==IGVM_VHT_SUPPORTED_PLATFORM ) { header_handle =igvm_get_header ( ctx->file,IGVM_HEADER_SECTION_PLATFORM , header_index);
@@ -945,10 +974,10 @@ int qigvm_process_file(IgvmCfg *cfg, MachineState *machine_state,for (ctx.current_header_index =0 ; ctx.current_header_index < (unsigned )header_count; ctx.current_header_index++) { -IgvmVariableHeaderType type =igvm_get_header_type ( +IgvmVariableHeaderType raw_type =igvm_get_header_type ( ctx.file,IGVM_HEADER_SECTION_DIRECTIVE , ctx.current_header_index); -if (!onlyVpContext || (type ==IGVM_VHT_VP_CONTEXT )) { -if (qigvm_handler (&ctx, type, errp) <0 ) { +if (!onlyVpContext ||igvm_vht_type (raw_type) ==IGVM_VHT_VP_CONTEXT ) { +if (qigvm_handler (&ctx, raw_type, errp) <0 ) { goto cleanup_parameters; } }
Signature · 1 line
2.54.0