Skip to contentSkip to conversation

hw/uefi: security fixes

PATCH In review Gerd Hoffmann 3 participants 9 messages Jul 20 – Jul 24

lore
Share & tools
lore
#0 lore raw

v4 cover hw/uefi: security fixes

v4:
- add fix for CVE-2026-16288
- fix format string (Luigi).
v3:
- just drop debug function instead of trying to fix it.
- add three more CVE fixes.

Gerd Hoffmann (6):
hw/uefi: add sanity check
hw/uefi: remove debug function
hw/uefi: check lower limit for signature list size hw/uefi: account variable policy entries against storage size hw/uefi: add post_load checks
hw/uefi: make SetupMode read-only

 hw/uefi/var-service-auth.c    |  7 ++++
 hw/uefi/var-service-core.c    | 14 +++++++-
 hw/uefi/var-service-json.c    |  5 +++
 hw/uefi/var-service-policy.c  | 68 ++++++++++++++++-------------------
 hw/uefi/var-service-siglist.c |  3 ++
 hw/uefi/var-service-vars.c    | 19 ++++++++++
 6 files changed, 78 insertions(+), 38 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v4 1/6 hw/uefi: add sanity check

Verify the passed buffer has the minimal required length before reading the size field + verifying the total length.

Fixes: CVE-2026-58581
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3614

---
 hw/uefi/var-service-policy.c | 3 +++
 1 file changed, 3 insertions(+)
hw/uefi/var-service-policy.c +3 -0
index 58da4adbebaf..989bf87ddb86 100644
@@ -276,6 +276,9 @@ static uint32_t uefi_vars_mm_check_policy_register(uefi_vars_state *uv,
     uefi_var_policy *pol;
     uint64_t length;
 
+    if (mhdr->length < sizeof(*mchk) + sizeof(*pe)) {
+        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_BAD_BUFFER_SIZE);
+    }
     if (uadd64_overflow(sizeof(*mchk), pe->size, &length)) {
         return uefi_vars_mm_policy_error(mhdr, mchk, EFI_BAD_BUFFER_SIZE);
     }
Signature · 1 line
2.55.0
#2 lore raw

PATCH v4 2/6 hw/uefi: remove debug function

In reply to Gerd Hoffmann

This was never meant to be present in production builds. It's a code path not hit on a normal boot (OVMF wouldn't try variable updates which are not allowed), so this went unnoticed.

Remove the function. If needed for debugging the git log is your friend.

Fixes: CVE-2026-58582
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3615

---
 hw/uefi/var-service-policy.c | 32 --------------------------------
 1 file changed, 32 deletions(-)
hw/uefi/var-service-policy.c +0 -32
index 989bf87ddb86..0880bfe074a6 100644
@@ -37,37 +37,6 @@ const VMStateDescription vmstate_uefi_var_policy = {
     },
 };
 
-static void print_policy_entry(variable_policy_entry *pe)
-{
-    uint16_t *name = (void *)pe + pe->offset_to_name;
-
-    fprintf(stderr, "%s:\n", __func__);
-
-    fprintf(stderr, "    name ´");
-    while (*name) {
-        fprintf(stderr, "%c", *name);
-        name++;
-    }
-    fprintf(stderr, "', version=%d.%d, size=%d\n",
-            pe->version >> 16, pe->version & 0xffff, pe->size);
-
-    if (pe->min_size) {
-        fprintf(stderr, "    size min=%d\n", pe->min_size);
-    }
-    if (pe->max_size != UINT32_MAX) {
-        fprintf(stderr, "    size max=%u\n", pe->max_size);
-    }
-    if (pe->attributes_must_have) {
-        fprintf(stderr, "    attr must=0x%x\n", pe->attributes_must_have);
-    }
-    if (pe->attributes_cant_have) {
-        fprintf(stderr, "    attr cant=0x%x\n", pe->attributes_cant_have);
-    }
-    if (pe->lock_policy_type) {
-        fprintf(stderr, "    lock policy type %d\n", pe->lock_policy_type);
-    }
-}
-
 static gboolean wildcard_str_equal(uefi_var_policy *pol,
                                    uefi_variable *var)
 {
@@ -173,7 +142,6 @@ efi_status uefi_vars_policy_check(uefi_vars_state *uv,
     pe = pol->entry;
 
     uefi_trace_variable(__func__, var->guid, var->name, var->name_size);
-    print_policy_entry(pe);
 
     if ((var->attributes & pe->attributes_must_have) != pe->attributes_must_have) {
         trace_uefi_vars_policy_deny("must-have-attr");
Signature · 1 line
2.55.0
#3 lore raw

PATCH v4 3/6 hw/uefi: check lower limit for signature list size

In reply to Gerd Hoffmann

Specifically disallow zero which can lead to an endless loop.

Fixes: CVE-2026-61406
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3899

---
 hw/uefi/var-service-siglist.c | 3 +++
 1 file changed, 3 insertions(+)
hw/uefi/var-service-siglist.c +3 -0
index 8948f1b78471..9e03a4dc0c28 100644
@@ -94,6 +94,9 @@ void uefi_vars_siglist_parse(uefi_vars_siglist *siglist,
             break;
         }
         efilist = data;
+        if (efilist->siglist_size < sizeof(*efilist)) {
+            break;
+        }
         if (size < efilist->siglist_size) {
             break;
         }
Signature · 1 line
2.55.0
#4 lore raw

PATCH v4 4/6 hw/uefi: account variable policy entries against storage size

In reply to Gerd Hoffmann

uefi-vars already tracks (and limits) the memory footprint of UEFI variables. Do that for variable policies too.

Fixes: CVE-2026-61405
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3890

---
 hw/uefi/var-service-policy.c | 5 +++++
 hw/uefi/var-service-vars.c   | 4 ++++
 2 files changed, 9 insertions(+)
hw/uefi/var-service-policy.c +5 -0
index 0880bfe074a6..46c4ff2dacae 100644
@@ -283,7 +283,12 @@ static uint32_t uefi_vars_mm_check_policy_register(uefi_vars_state *uv,
         return uefi_vars_mm_policy_error(mhdr, mchk, EFI_ALREADY_STARTED);
     }
 
+    if (uv->used_storage + pe->size > uv->max_storage) {
+        return uefi_vars_mm_policy_error(mhdr, mchk, EFI_OUT_OF_RESOURCES);
+    }
+
     uefi_vars_add_policy(uv, pe);
+    uv->used_storage += pe->size;
 
     mchk->result = EFI_SUCCESS;
     return sizeof(*mchk);
hw/uefi/var-service-vars.c +4 -0
index 2c83130ebf63..fd07782f1e0d 100644
@@ -201,11 +201,15 @@ void uefi_vars_clear_all(uefi_vars_state *uv)
 void uefi_vars_update_storage(uefi_vars_state *uv)
 {
     uefi_variable *var;
+    uefi_var_policy *pol;
 
     uv->used_storage = 0;
     QTAILQ_FOREACH(var, &uv->variables, next) {
         uv->used_storage += variable_size(var);
     }
+    QTAILQ_FOREACH(pol, &uv->var_policies, next) {
+        uv->used_storage += pol->entry->size;
+    }
 }
 
 static gboolean check_access(uefi_vars_state *uv, uefi_variable *var)
Signature · 1 line
2.55.0
#5 lore raw

PATCH v4 5/6 hw/uefi: add post_load checks

In reply to Gerd Hoffmann

Add sanity checks to uefi-vars state loaded from live migration data stream. Fail migration if invalid data or inconsistencies are found.

Fixes: CVE-2026-61404
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3837
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3838
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3839
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3885

---
 hw/uefi/var-service-core.c   | 14 +++++++++++++-
 hw/uefi/var-service-json.c   |  5 +++++
 hw/uefi/var-service-policy.c | 28 +++++++++++++++++++++++-----
 hw/uefi/var-service-vars.c   | 15 +++++++++++++++
 4 files changed, 56 insertions(+), 6 deletions(-)
hw/uefi/var-service-core.c +13 -1
index 828d76007318..65da71029a60 100644
@@ -5,6 +5,7 @@
  */
 #include "qemu/osdep.h"
 #include "qemu/crc32c.h"
+#include "qemu/error-report.h"
 #include "system/dma.h"
 #include "migration/vmstate.h"
 
@@ -28,9 +29,20 @@ static int uefi_vars_post_load(void *opaque, int version_id)
 {
     uefi_vars_state *uv = opaque;
 
+    if (uv->buf_size > MAX_BUFFER_SIZE) {
+        error_report("invalid buffer size");
+        return -1;
+    }
+    uv->buffer = g_malloc(uv->buf_size);
+
     uefi_vars_update_storage(uv);
+    if (uv->used_storage > uv->max_storage) {
+        error_report("out of variable memory (%" PRId64 " > %" PRId64 ")",
+                     uv->used_storage, uv->max_storage);
+        return -1;
+    }
+
     uefi_vars_json_save(uv);
-    uv->buffer = g_malloc(uv->buf_size);
     return 0;
 }
 
hw/uefi/var-service-json.c +5 -0
index 8621b86c5c5f..f90deef79b76 100644
@@ -18,6 +18,7 @@
 #include "qobject/qobject.h"
 #include "qobject/qjson.h"
 
+#include "qapi/error.h"
 #include "qapi/dealloc-visitor.h"
 #include "qapi/qobject-input-visitor.h"
 #include "qapi/qobject-output-visitor.h"
@@ -249,6 +250,10 @@ void uefi_vars_json_load(uefi_vars_state *uv, Error **errp)
     if (!(*errp)) {
         uefi_vars_from_qapi(uv, vs);
         uefi_vars_update_storage(uv);
+        if (uv->used_storage > uv->max_storage) {
+            error_setg(errp, "out of variable memory (%ld > %ld)",
+                       uv->used_storage, uv->max_storage);
+        }
     }
 
     qapi_free_UefiVarStore(vs);
hw/uefi/var-service-policy.c +23 -5
index 46c4ff2dacae..d43cc9b2500b 100644
@@ -7,6 +7,7 @@
  * https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Library/VariablePolicyLib/ReadMe.md
  */
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "system/dma.h"
 #include "migration/vmstate.h"
 
@@ -16,14 +17,18 @@
 
 #include "trace.h"
 
-static void calc_policy(uefi_var_policy *pol);
+static int check_calc_policy(uefi_var_policy *pol);
 
 static int uefi_var_policy_post_load(void *opaque, int version_id)
 {
     uefi_var_policy *pol = opaque;
+    int rc;
 
-    calc_policy(pol);
-    return 0;
+    rc = check_calc_policy(pol);
+    if (rc != 0) {
+        error_report("invalid uefi variable policy");
+    }
+    return rc;
 }
 
 const VMStateDescription vmstate_uefi_var_policy = {
@@ -80,32 +85,45 @@ static uefi_var_policy *wildcard_find_policy(uefi_vars_state *uv,
     return NULL;
 }
 
-static void calc_policy(uefi_var_policy *pol)
+static int check_calc_policy(uefi_var_policy *pol)
 {
     variable_policy_entry *pe = pol->entry;
     unsigned int i;
 
+    if (pol->entry_size != pe->size ||
+        pe->offset_to_name >= pe->size) {
+        return -1;
+    }
+
     pol->name = (void *)pol->entry + pe->offset_to_name;
     pol->name_size = pe->size - pe->offset_to_name;
 
+    if (!uefi_str_is_valid(pol->name, pol->name_size, false)) {
+        return -1;
+    }
+
     for (i = 0; i < pol->name_size / 2; i++) {
         if (pol->name[i] == '#') {
             pol->hashmarks++;
         }
     }
+
+    return 0;
 }
 
 uefi_var_policy *uefi_vars_add_policy(uefi_vars_state *uv,
                                       variable_policy_entry *pe)
 {
     uefi_var_policy *pol, *p;
+    int rc;
 
     pol = g_new0(uefi_var_policy, 1);
     pol->entry = g_malloc(pe->size);
     memcpy(pol->entry, pe, pe->size);
     pol->entry_size = pe->size;
 
-    calc_policy(pol);
+    rc = check_calc_policy(pol);
+    g_assert(rc == 0);
 
     /* keep list sorted by priority, add to tail of priority group */
     QTAILQ_FOREACH(p, &uv->var_policies, next) {
hw/uefi/var-service-vars.c +15 -0
index fd07782f1e0d..e0ea3d532eb6 100644
@@ -47,6 +47,20 @@ static int uefi_vars_pre_load(void *opaque)
     return 0;
 }
 
+static int uefi_vars_post_load(void *opaque, int version_id)
+{
+    uefi_variable *var = opaque;
+
+    if (!uefi_str_is_valid(var->name, var->name_size, true) ||
+        var->attributes & ~EFI_VARIABLE_ATTRIBUTE_SUPPORTED ||
+        (var->digest_size != 0 &&
+         var->digest_size != 32 /* AUTHVAR_DIGEST_SIZE */)) {
+        error_report("invalid uefi variable");
+        return -1;
+    }
+    return 0;
+}
+
 static bool uefi_vars_digest_is_needed(void *opaque)
 {
     uefi_variable *var = opaque;
@@ -72,6 +86,7 @@ const VMStateDescription vmstate_uefi_variable_digest = {
 const VMStateDescription vmstate_uefi_variable = {
     .name = "uefi-variable",
     .pre_load = uefi_vars_pre_load,
+    .post_load = uefi_vars_post_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT8_ARRAY_V(guid.data, uefi_variable, sizeof(QemuUUID), 0),
         VMSTATE_UINT32(name_size, uefi_variable),
Signature · 1 line
2.55.0
#6 lore raw

PATCH v4 6/6 hw/uefi: make SetupMode read-only

In reply to Gerd Hoffmann

This is read-only variable which informs the OS about the secure boot state. Reject any attempts to write to it.

Fixes: CVE-2026-16288
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4039

---
 hw/uefi/var-service-auth.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/uefi/var-service-auth.c +7 -0
index f3dc9c6ca608..899444af12df 100644
@@ -298,6 +298,13 @@ efi_status uefi_vars_check_secure_boot(uefi_vars_state *uv, uefi_variable *var)
         return EFI_WRITE_PROTECTED;
     }
 
+    /* reject SetupMode updates */
+    if (qemu_uuid_is_equal(&var->guid, &EfiGlobalVariable) &&
+        uefi_str_equal(var->name, var->name_size,
+                       name_setup_mode, sizeof(name_setup_mode))) {
+        return EFI_WRITE_PROTECTED;
+    }
+
     return EFI_SUCCESS;
 }
 
Signature · 1 line
2.55.0
Acked-by on the v4 series
#7 lore raw
In reply to Gerd Hoffmann
Quoted 15 lines · Gerd Hoffmann

v4:
- add fix for CVE-2026-16288
- fix format string (Luigi).
v3:
- just drop debug function instead of trying to fix it.
- add three more CVE fixes.

Gerd Hoffmann (6):
hw/uefi: add sanity check
hw/uefi: remove debug function
hw/uefi: check lower limit for signature list size hw/uefi: account variable policy entries against storage size hw/uefi: add post_load checks
hw/uefi: make SetupMode read-only

Quoted 12 lines
hw/uefi/var-service-auth.c    |  7 ++++
hw/uefi/var-service-core.c    | 14 +++++++-
hw/uefi/var-service-json.c    |  5 +++
hw/uefi/var-service-policy.c  | 68 ++++++++++++++++-------------------
hw/uefi/var-service-siglist.c |  3 ++
hw/uefi/var-service-vars.c    | 19 ++++++++++
6 files changed, 78 insertions(+), 38 deletions(-)
Signature · 1 line
2.55.0
#8 lore raw
In reply to Gerd Hoffmann
Quoted 15 lines · Gerd Hoffmann

v4:
- add fix for CVE-2026-16288
- fix format string (Luigi).
v3:
- just drop debug function instead of trying to fix it.
- add three more CVE fixes.

Gerd Hoffmann (6):
hw/uefi: add sanity check
hw/uefi: remove debug function
hw/uefi: check lower limit for signature list size hw/uefi: account variable policy entries against storage size hw/uefi: add post_load checks
hw/uefi: make SetupMode read-only

Quoted 7 lines
  hw/uefi/var-service-auth.c    |  7 ++++
  hw/uefi/var-service-core.c    | 14 +++++++-
  hw/uefi/var-service-json.c    |  5 +++
  hw/uefi/var-service-policy.c  | 68 ++++++++++++++++-------------------
  hw/uefi/var-service-siglist.c |  3 ++
  hw/uefi/var-service-vars.c    | 19 ++++++++++
  6 files changed, 78 insertions(+), 38 deletions(-)

I'm picking this patch set for the stable qemu series. Please let me know if I shouldn't.

Thanks,

/mjt