Skip to contentSkip to conversation

igvm: fix handling of optional variable header types

PATCH Superseded Luigi Leonardi 4 participants 6 messages May 25 – May 26

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 igvm: fix handling of optional variable header types

The 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, all the types with the optional bit set are not recognized as valid headers.

Implement optional header handling by masking bit 31 before matching against the handler table, and skip with a warning any unrecognized header that has the optional bit set.

Fixes c1d466d267cf ("backends/igvm: Add IGVM loader and configuration")
---
 backends/igvm.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
backends/igvm.c +21 -3
index c347d0c17e..408917f826 100644
@@ -26,6 +26,7 @@
 #include <igvm/igvm.h>
 #include <igvm/igvm_defs.h>
 
+#define IGVM_VHT_OPTIONAL_BIT (1U << 31)
 
 /*
  * Some directives are specific to particular confidential computing platforms.
@@ -139,8 +140,16 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
     const uint8_t *header_data;
     int result;
 
+    /*
+     * 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
+     */
+    IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;
+
     for (handler = 0; handler < G_N_ELEMENTS(handlers); handler++) {
-        if (handlers[handler].type != type) {
+        if (handlers[handler].type != base_type) {
             continue;
         }
         header_handle = igvm_get_header(ctx->file, handlers[handler].section,
@@ -166,6 +175,13 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
         igvm_free_buffer(ctx->file, header_handle);
         return result;
     }
+
+    if (type & IGVM_VHT_OPTIONAL_BIT) {
+        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,7 +803,8 @@ 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);
-        if (typ == IGVM_VHT_SUPPORTED_PLATFORM) {
+        IgvmVariableHeaderType base_type = typ & ~IGVM_VHT_OPTIONAL_BIT;
+        if (base_type == IGVM_VHT_SUPPORTED_PLATFORM) {
             header_handle = igvm_get_header(
                 ctx->file, IGVM_HEADER_SECTION_PLATFORM, header_index);
             if (header_handle < 0) {
@@ -947,7 +964,8 @@ int qigvm_process_file(IgvmCfg *cfg, MachineState *machine_state,
          ctx.current_header_index++) {
         IgvmVariableHeaderType type = igvm_get_header_type(
             ctx.file, IGVM_HEADER_SECTION_DIRECTIVE, ctx.current_header_index);
-        if (!onlyVpContext || (type == IGVM_VHT_VP_CONTEXT)) {
+        IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;
+        if (!onlyVpContext || base_type == IGVM_VHT_VP_CONTEXT) {
             if (qigvm_handler(&ctx, type, errp) < 0) {
                 goto cleanup_parameters;
             }

---
base-commit: cbf877d67a812be17a9ce404a589e1bdf722c1f6
change-id: 20260525-igvm_optional-ca1592b613be

Best regards,

Signature · 1 line
Luigi Leonardi <[email protected]>
#1 lore raw
Quoted 52 lines · Luigi Leonardi

The 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, all the types with the optional bit set are not recognized as valid headers.

Implement optional header handling by masking bit 31 before matching against the handler table, and skip with a warning any unrecognized header that has the optional bit set.

Fixes c1d466d267cf ("backends/igvm: Add IGVM loader and configuration")

---
backends/igvm.c | 24 +++++++++++++++++++++---

1 file changed, 21 insertions(+), 3 deletions(-)
backends/igvm.c +10 -1
index c347d0c17e..408917f826 100644
@@ -26,6 +26,7 @@
 #include <igvm/igvm.h>
 #include <igvm/igvm_defs.h>
 
+#define IGVM_VHT_OPTIONAL_BIT (1U << 31)
 
 /*
 * Some directives are specific to particular confidential computing platforms.
@@ -139,8 +140,16 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
    const uint8_t *header_data;
    int result;
 
+    /*
+     * 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
+     */
+    IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;
+
    for (handler = 0; handler < G_N_ELEMENTS(handlers); handler++) {
-        if (handlers[handler].type != type) {
+        if (handlers[handler].type != base_type) {
            continue;
        }
        header_handle = igvm_get_header(ctx->file, handlers[handler].section,

backends/igvm.c static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp) · line 184

Quoted 10 lines
backends/igvm.c +7 -0
@@ -166,6 +175,13 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
        igvm_free_buffer(ctx->file, header_handle);
        return result;
    }
+
+    if (type & IGVM_VHT_OPTIONAL_BIT) {
+        warn_report("IGVM: Skipping unsupported optional header type 0x%"
+                    PRIX32, type);
+        return 0;
+    }
+

I think this check should be at the top of the loop. If the bit is set, simply return with that warning message. No further processing is needed.

Quoted 32 lines
backends/igvm.c +4 -3
    error_setg(errp,
               "IGVM: Unknown header type encountered when processing file: "
               "(type 0x%X)",
@@ -787,7 +803,8 @@ 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);
-        if (typ == IGVM_VHT_SUPPORTED_PLATFORM) {
+        IgvmVariableHeaderType base_type = typ & ~IGVM_VHT_OPTIONAL_BIT;
+        if (base_type == IGVM_VHT_SUPPORTED_PLATFORM) {
            header_handle = igvm_get_header(
                ctx->file, IGVM_HEADER_SECTION_PLATFORM, header_index);
            if (header_handle < 0) {
@@ -947,7 +964,8 @@ int qigvm_process_file(IgvmCfg *cfg, MachineState *machine_state,
         ctx.current_header_index++) {
        IgvmVariableHeaderType type = igvm_get_header_type(
            ctx.file, IGVM_HEADER_SECTION_DIRECTIVE, ctx.current_header_index);
-        if (!onlyVpContext || (type == IGVM_VHT_VP_CONTEXT)) {
+        IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;
+        if (!onlyVpContext || base_type == IGVM_VHT_VP_CONTEXT) {
            if (qigvm_handler(&ctx, type, errp) < 0) {
                goto cleanup_parameters;
            }
 
---

base-commit: cbf877d67a812be17a9ce404a589e1bdf722c1f6
change-id: 20260525-igvm_optional-ca1592b613be

Best regards,

Signature · 1 line
Luigi Leonardi <[email protected]>
#2 lore raw
Quoted 56 lines · Ani Sinha
Quoted 56 lines · Luigi Leonardi

The 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, all the types with the optional bit set are not recognized as valid headers.

Implement optional header handling by masking bit 31 before matching against the handler table, and skip with a warning any unrecognized header that has the optional bit set.

Fixes c1d466d267cf ("backends/igvm: Add IGVM loader and configuration")

---
backends/igvm.c | 24 +++++++++++++++++++++---

1 file changed, 21 insertions(+), 3 deletions(-)
backends/igvm.c +11 -1
index c347d0c17e..408917f826 100644
@@ -26,6 +26,7 @@
 #include <igvm/igvm.h>
 #include <igvm/igvm_defs.h>
 
+#define IGVM_VHT_OPTIONAL_BIT (1U << 31)
 
 /*
 * Some directives are specific to particular confidential computing platforms.
@@ -139,8 +140,16 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
    const uint8_t *header_data;
    int result;
 
+    /*
+     * 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
+     */
+    IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;
+
    for (handler = 0; handler < G_N_ELEMENTS(handlers); handler++) {
-        if (handlers[handler].type != type) {
+        if (handlers[handler].type != base_type) {
            continue;
        }
        header_handle = igvm_get_header(ctx->file, handlers[handler].section,
@@ -166,6 +175,13 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
        igvm_free_buffer(ctx->file, header_handle);
        return result;
    }
+
Quoted 8 lines

backends/igvm.c static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp) · line 184

Quoted 6 lines
backends/igvm.c +6 -0
+    if (type & IGVM_VHT_OPTIONAL_BIT) {
+        warn_report("IGVM: Skipping unsupported optional header type 0x%"
+                    PRIX32, type);
+        return 0;
+    }
+

I think this check should be at the top of the loop. If the bit is set, simply return with that warning message. No further processing is needed.

IIUC, in this way we would always skip optional parameter, right? My idea is to always process optional parameters if we support it.

Quoted 35 lines
Quoted 34 lines
backends/igvm.c +4 -3
    error_setg(errp,
               "IGVM: Unknown header type encountered when processing file: "
               "(type 0x%X)",
@@ -787,7 +803,8 @@ 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);
-        if (typ == IGVM_VHT_SUPPORTED_PLATFORM) {
+        IgvmVariableHeaderType base_type = typ & ~IGVM_VHT_OPTIONAL_BIT;
+        if (base_type == IGVM_VHT_SUPPORTED_PLATFORM) {
            header_handle = igvm_get_header(
                ctx->file, IGVM_HEADER_SECTION_PLATFORM, header_index);
            if (header_handle < 0) {
@@ -947,7 +964,8 @@ int qigvm_process_file(IgvmCfg *cfg, MachineState *machine_state,
         ctx.current_header_index++) {
        IgvmVariableHeaderType type = igvm_get_header_type(
            ctx.file, IGVM_HEADER_SECTION_DIRECTIVE, ctx.current_header_index);
-        if (!onlyVpContext || (type == IGVM_VHT_VP_CONTEXT)) {
+        IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;
+        if (!onlyVpContext || base_type == IGVM_VHT_VP_CONTEXT) {
            if (qigvm_handler(&ctx, type, errp) < 0) {
                goto cleanup_parameters;
            }
 
---

base-commit: cbf877d67a812be17a9ce404a589e1bdf722c1f6
change-id: 20260525-igvm_optional-ca1592b613be

Best regards,
--
Luigi Leonardi <[email protected]>

#3 lore raw
Quoted 12 lines · Luigi Leonardi
Quoted 7 lines · Ani Sinha
Quoted 7 lines · Luigi Leonardi

The 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.

This description is very confusing. Where is this text written? I can’t find it.

Quoted 58 lines
Quoted 49 lines
Quoted 49 lines

Currently, all the types with the optional bit set are not recognized as valid headers.

Implement optional header handling by masking bit 31 before matching against the handler table, and skip with a warning any unrecognized header that has the optional bit set.

Fixes c1d466d267cf ("backends/igvm: Add IGVM loader and configuration")

---
backends/igvm.c | 24 +++++++++++++++++++++---

1 file changed, 21 insertions(+), 3 deletions(-)
backends/igvm.c +11 -1
index c347d0c17e..408917f826 100644
@@ -26,6 +26,7 @@
 #include <igvm/igvm.h>
 #include <igvm/igvm_defs.h>
 
+#define IGVM_VHT_OPTIONAL_BIT (1U << 31)
 
 /*
 * Some directives are specific to particular confidential computing platforms.
@@ -139,8 +140,16 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
   const uint8_t *header_data;
   int result;
 
+    /*
+     * 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
+     */
+    IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;
+
   for (handler = 0; handler < G_N_ELEMENTS(handlers); handler++) {
-        if (handlers[handler].type != type) {
+        if (handlers[handler].type != base_type) {
           continue;
       }
       header_handle = igvm_get_header(ctx->file, handlers[handler].section,
@@ -166,6 +175,13 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
       igvm_free_buffer(ctx->file, header_handle);
       return result;
   }
+
Quoted 8 lines

backends/igvm.c static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp) · line 184

Quoted 6 lines
backends/igvm.c +6 -0
+    if (type & IGVM_VHT_OPTIONAL_BIT) {
+        warn_report("IGVM: Skipping unsupported optional header type 0x%"
+                    PRIX32, type);
+        return 0;
+    }
+

I think this check should be at the top of the loop. If the bit is set, simply return with that warning message. No further processing is needed.

Quoted 2 lines

IIUC, in this way we would always skip optional parameter, right? My idea is to always process optional parameters if we support it.

The spec says:

"The top bit of this type may be set to indicate a loader may safely ignore that structure.”

So if the bit is set, the way I understand it, there is no need for further processing.

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
backends/igvm.c +4 -3
   error_setg(errp,
              "IGVM: Unknown header type encountered when processing file: "
              "(type 0x%X)",
@@ -787,7 +803,8 @@ 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);
-        if (typ == IGVM_VHT_SUPPORTED_PLATFORM) {
+        IgvmVariableHeaderType base_type = typ & ~IGVM_VHT_OPTIONAL_BIT;
+        if (base_type == IGVM_VHT_SUPPORTED_PLATFORM) {
           header_handle = igvm_get_header(
               ctx->file, IGVM_HEADER_SECTION_PLATFORM, header_index);
           if (header_handle < 0) {
@@ -947,7 +964,8 @@ int qigvm_process_file(IgvmCfg *cfg, MachineState *machine_state,
        ctx.current_header_index++) {
       IgvmVariableHeaderType type = igvm_get_header_type(
           ctx.file, IGVM_HEADER_SECTION_DIRECTIVE, ctx.current_header_index);
-        if (!onlyVpContext || (type == IGVM_VHT_VP_CONTEXT)) {
+        IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;
+        if (!onlyVpContext || base_type == IGVM_VHT_VP_CONTEXT) {
           if (qigvm_handler(&ctx, type, errp) < 0) {
               goto cleanup_parameters;
           }
 
---

base-commit: cbf877d67a812be17a9ce404a589e1bdf722c1f6
change-id: 20260525-igvm_optional-ca1592b613be

Best regards,
--
Luigi Leonardi <[email protected]>

#4 lore raw
Quoted 10 lines
Quoted 4 lines
Quoted 1 line

I think this check should be at the top of the loop. If the bit is set, simply return with that warning message. No further processing is needed.

IIUC, in this way we would always skip optional parameter, right? My idea is to always process optional parameters if we support it.

The spec says:

"The top bit of this type may be set to indicate a loader may safely ignore that structure.”

So if the bit is set, the way I understand it, there is no need for further processing.

I read that as "If that directive is unsupported (by the hypervisor) it safe to skip it (instead of throwing an fatal error)", so In think the patch is correct in looking at the flag in the error handling code path.

Never processing the directive also doesn't make much sense, why would one write such a directive into the igvm file in the first place?

take care,
Gerd

#5 lore raw
In reply to Luigi Leonardi
Quoted 22 lines · Luigi Leonardi

The 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, all the types with the optional bit set are not recognized as valid headers.

Implement optional header handling by masking bit 31 before matching against the handler table, and skip with a warning any unrecognized header that has the optional bit set.

Fixes c1d466d267cf ("backends/igvm: Add IGVM loader and configuration")

---
backends/igvm.c | 24 +++++++++++++++++++++---

1 file changed, 21 insertions(+), 3 deletions(-)
backends/igvm.c +0 -0
index c347d0c17e..408917f826 100644

backends/igvm.c line 29

Quoted 4 lines
backends/igvm.c +1 -0
@@ -26,6 +26,7 @@
 #include <igvm/igvm.h>
 #include <igvm/igvm_defs.h>
 
+#define IGVM_VHT_OPTIONAL_BIT (1U << 31)

The best would be to have this exposed by the library headers.
I see you opened https://github.com/microsoft/igvm/pull/121 exactly for that, so I'd wait that one merged before merging this and I'd like to add an #ifndef here to define it if not defined by the library, so we will be ready when we will update the dependency.

Quoted 4 lines
backends/igvm.c +0 -0
 /*
 * Some directives are specific to particular confidential computing platforms.

backends/igvm.c static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp) · line 149

Quoted 10 lines
backends/igvm.c +7 -0
@@ -139,8 +140,16 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
    const uint8_t *header_data;
    int result;
 
+    /*
+     * 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
+     */
+    IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;

What about adding a function to mask that bit?

So we will have a single point where to put this comment and use the function everywhere.

Ideally that function should be provided by the igvm library IMHO.

That said base_type & type is confusing IMO. What about raw_type (the one with flags, etc.) and type (the one after the mask) ?

Quoted 22 lines
backends/igvm.c +9 -1
+
    for (handler = 0; handler < G_N_ELEMENTS(handlers); handler++) {
-        if (handlers[handler].type != type) {
+        if (handlers[handler].type != base_type) {
            continue;
        }
        header_handle = igvm_get_header(ctx->file, handlers[handler].section,
@@ -166,6 +175,13 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
        igvm_free_buffer(ctx->file, header_handle);
        return result;
    }
+
+    if (type & IGVM_VHT_OPTIONAL_BIT) {
+        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)",

backends/igvm.c static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp) · line 806

Quoted 5 lines
backends/igvm.c +1 -1
@@ -787,7 +803,8 @@ 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);
-        if (typ == IGVM_VHT_SUPPORTED_PLATFORM) {
+        IgvmVariableHeaderType base_type = typ & ~IGVM_VHT_OPTIONAL_BIT;

Now typ is used just to define base_type, can we just mask the value returned by igvm_get_header_type() ? (Or typ &= ...). I mean I don't see any value on defining a new variable.

Thanks,
Stefano

Quoted 23 lines
backends/igvm.c +3 -2
+        if (base_type == IGVM_VHT_SUPPORTED_PLATFORM) {
            header_handle = igvm_get_header(
                ctx->file, IGVM_HEADER_SECTION_PLATFORM, header_index);
            if (header_handle < 0) {
@@ -947,7 +964,8 @@ int qigvm_process_file(IgvmCfg *cfg, MachineState *machine_state,
         ctx.current_header_index++) {
        IgvmVariableHeaderType type = igvm_get_header_type(
            ctx.file, IGVM_HEADER_SECTION_DIRECTIVE, ctx.current_header_index);
-        if (!onlyVpContext || (type == IGVM_VHT_VP_CONTEXT)) {
+        IgvmVariableHeaderType base_type = type & ~IGVM_VHT_OPTIONAL_BIT;
+        if (!onlyVpContext || base_type == IGVM_VHT_VP_CONTEXT) {
            if (qigvm_handler(&ctx, type, errp) < 0) {
                goto cleanup_parameters;
            }
 
---

base-commit: cbf877d67a812be17a9ce404a589e1bdf722c1f6
change-id: 20260525-igvm_optional-ca1592b613be

Best regards,

Signature · 1 line
Luigi Leonardi <[email protected]>