hw/uefi: security fixes
PATCH v3 2/5 hw/uefi: remove debug function
In reply to Gerd HoffmannThis 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
@@ -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
v3 cover hw/uefi: security fixes
v3:
- just drop debug function instead of trying to fix it.
- add three more CVE fixes.
Gerd Hoffmann (5):
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/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 ++++++++++ 5 files changed, 71 insertions(+), 38 deletions(-)
Signature · 1 line
2.55.0
PATCH v3 1/5 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
@@ -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
PATCH v3 5/5 hw/uefi: add post_load checks
In reply to Gerd HoffmannAdd 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
@@ -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 (%ld > %ld)" , + 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
@@ -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
@@ -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
@@ -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
PATCH v3 4/5 hw/uefi: account variable policy entries against storage size
In reply to Gerd Hoffmannuefi-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
@@ -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
@@ -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
PATCH v3 3/5 hw/uefi: check lower limit for signature list size
In reply to Gerd HoffmannSpecifically 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
@@ -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
Re: [PATCH v3 4/5] hw/uefi: account variable policy entries against storage size
In reply to Gerd HoffmannHi Gerd,
Quoted 8 lines · 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/3890Signed-off-by Gerd Hoffmann <[email protected]>---
Should we decrement this counter when policies are cleared?
Luigi
Re: [PATCH v3 5/5] hw/uefi: add post_load checks
In reply to Gerd HoffmannQuoted 31 lines · 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/3885Signed-off-by Gerd Hoffmann <[email protected]>---
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 +1
-0@@ -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;
hw/uefi/var-service-core.c static int uefi_vars_post_load(void *opaque, int version_id) · line 40
Quoted 10 lines
hw/uefi/var-service-core.c +8
-0+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 (%ld > %ld)",
nit: what about replacing %ld with PRIu64?
Quoted 120 lines
hw/uefi/var-service-core.c +4
-1+ 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@@ -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@@ -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 +0
-0@@ -47,6 +47,20 @@ static int uefi_vars_pre_load(void *opaque)return 0 ;
hw/uefi/var-service-vars.c static int uefi_vars_pre_load(void *opaque) · line 57
Quoted 10 lines
hw/uefi/var-service-vars.c +8
-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 */ )) {
is it worth moving that define to a common header and using it here?
Quoted 21 lines
hw/uefi/var-service-vars.c +7
-0+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
Re: [PATCH v3 5/5] hw/uefi: add post_load checks
Hi,
Quoted 4 lines
hw/uefi/var-service-core.c static int uefi_vars_post_load(void *opaque, int version_id) · line 40
Quoted 2 lines
hw/uefi/var-service-core.c +2
-0+if (uv->used_storage > uv->max_storage) { + error_report("out of variable memory (%ld > %ld)",nit: what about replacing %ld with PRIu64?
Done.
Quoted 10 lines
hw/uefi/var-service-vars.c static int uefi_vars_pre_load(void *opaque) · line 57
Quoted 8 lines
hw/uefi/var-service-vars.c +8
-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 */ )) {is it worth moving that define to a common header and using it here?
Was thinking about that too, but I'd prefer to keep the AUTHVAR_DIGEST_* #defines together and the other has a dependency on gnutls so I can't move it around easily.
take care,
Gerd