hw/uefi: a batch of security fixes
cover hw/uefi: a batch of security fixes
Gerd Hoffmann (6):
hw/uefi: fix buffer overruns
hw/uefi: verify pio_xfer_offset before calculating buffer checksum hw/uefi: fix ucs2 string helper functions
hw/uefi: add name_size check to uefi_vars_mm_lock_variable() hw/uefi: verify data size before accessing it in wrap_pkcs7 hw/uefi: avoid possibly unaligned variable_auth_2 struct field access
hw/uefi/var-service-auth.c | 16 +++++++------- hw/uefi/var-service-core.c | 4 ++++ hw/uefi/var-service-pkcs7.c | 3 ++- hw/uefi/var-service-utils.c | 42 +++++++++++++++++++++---------------- hw/uefi/var-service-vars.c | 22 +++++++++++++++---- 5 files changed, 57 insertions(+), 30 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/6 hw/uefi: fix buffer overruns
The buffer size checks do not consider the mm_header size, simliar to CVE-2026-5744. Factor out the repeated size check to a small helper function, fix the check, update all places to use the new helper.
--- hw/uefi/var-service-vars.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
hw/uefi/var-service-vars.c +15 -4
@@ -297,6 +297,17 @@ static size_t uefi_vars_mm_error(mm_header *mhdr, mm_variable *mvar,return sizeof (*mvar); } +static bool check_buffer_size (uefi_vars_state *uv,uint64_t length) +{ +/* uefi_vars_cmd_mm() checks that */ +g_assert (uv->buf_size >=sizeof (mm_header)); + +if (uv->buf_size -sizeof (mm_header) < length) { +return false; + } +return true; +} +static size_t uefi_vars_mm_get_variable (uefi_vars_state *uv,mm_header *mhdr,mm_variable *mvar,void *func) {
@@ -344,7 +355,7 @@ static size_t uefi_vars_mm_get_variable(uefi_vars_state *uv, mm_header *mhdr,if (uadd64_overflow (length, va->data_size, &length)) {return uefi_vars_mm_error (mhdr, mvar,EFI_BAD_BUFFER_SIZE ); } -if (uv->buf_size < length) { +if (!check_buffer_size (uv, length)) {return uefi_vars_mm_error (mhdr, mvar,EFI_BAD_BUFFER_SIZE ); }
@@ -414,7 +425,7 @@ uefi_vars_mm_get_next_variable(uefi_vars_state *uv, mm_header *mhdr, } length =sizeof (*mvar) +sizeof (*nv) + var->name_size; -if (uv->buf_size < length) { +if (!check_buffer_size (uv, length)) {return uefi_vars_mm_error (mhdr, mvar,EFI_BAD_BUFFER_SIZE ); }
@@ -605,7 +616,7 @@ static size_t uefi_vars_mm_variable_info(uefi_vars_state *uv, mm_header *mhdr,uint64_t length; length =sizeof (*mvar) +sizeof (*vi); -if (uv->buf_size < length) { +if (!check_buffer_size (uv, length)) {return uefi_vars_mm_error (mhdr, mvar,EFI_BAD_BUFFER_SIZE ); }
@@ -626,7 +637,7 @@ uefi_vars_mm_get_payload_size(uefi_vars_state *uv, mm_header *mhdr,uint64_t length; length =sizeof (*mvar) +sizeof (*ps); -if (uv->buf_size < length) { +if (!check_buffer_size (uv, length)) {return uefi_vars_mm_error (mhdr, mvar,EFI_BAD_BUFFER_SIZE ); }
Signature · 1 line
2.53.0
PATCH 2/6 hw/uefi: verify pio_xfer_offset before calculating buffer checksum
In reply to Gerd HoffmannWithout that it is possible to do trigger OOB reads by first advancing offset, then making the buffer smaller, finally asking for a checksum.
--- hw/uefi/var-service-core.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/uefi/var-service-core.c +4 -0
@@ -235,6 +235,10 @@ static uint64_t uefi_vars_read(void *opaque, hwaddr addr, unsigned size) uv->pio_xfer_offset += size;break ;case UEFI_VARS_REG_PIO_BUFFER_CRC32C : +if (uv->pio_xfer_offset > uv->buf_size) { + retval =0 ; +break ; + } retval =crc32c (0xffffffff , uv->pio_xfer_buffer, uv->pio_xfer_offset);break ;case UEFI_VARS_REG_FLAGS :
Signature · 1 line
2.53.0
PATCH 3/6 hw/uefi: fix ucs2 string helper functions
In reply to Gerd HoffmannThe length passed in is in bytes not characters. Rename the parameters to make that clear. Calculate the number of chars if needed. Fix length checks to use the number of chars not bytes to avoid OOB reads.
--- hw/uefi/var-service-utils.c | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-)
hw/uefi/var-service-utils.c +24 -18
@@ -19,13 +19,18 @@* sometimes when they are not (for example in variable policies). */ -gboolean uefi_str_is_valid (const uint16_t *str,size_t len, +gboolean uefi_str_is_valid (const uint16_t *str,size_t bytes,gboolean must_be_null_terminated) { +size_t chars = bytes /2 ;size_t pos =0 ; +if ((bytes %2 ) !=0 ) { +return false; + } +for (;;) { -if (pos == len) { +if (pos == chars) {if (must_be_null_terminated) {return false; }else {
@@ -47,12 +52,13 @@ gboolean uefi_str_is_valid(const uint16_t *str, size_t len, } } -size_t uefi_strlen (const uint16_t *str,size_t len) +size_t uefi_strlen (const uint16_t *str,size_t bytes) { +size_t chars = bytes /2 ;size_t pos =0 ;for (;;) { -if (pos == len) { +if (pos == chars) {return pos; }if (str[pos] ==0 ) {
@@ -62,25 +68,25 @@ size_t uefi_strlen(const uint16_t *str, size_t len) } } -gboolean uefi_str_equal_ex (const uint16_t *a,size_t alen, -const uint16_t *b,size_t blen, +gboolean uefi_str_equal_ex (const uint16_t *a,size_t a_bytes, +const uint16_t *b,size_t b_bytes,gboolean wildcards_in_a) { +size_t a_chars = a_bytes /2 ; +size_t b_chars = b_bytes /2 ;size_t pos =0 ; - alen = alen /2 ; - blen = blen /2 ;for (;;) { -if (pos == alen && pos == blen) { +if (pos == a_chars && pos == b_chars) {return true; } -if (pos == alen && b[pos] ==0 ) { +if (pos == a_chars && b[pos] ==0 ) {return true; } -if (pos == blen && a[pos] ==0 ) { +if (pos == b_chars && a[pos] ==0 ) {return true; } -if (pos == alen || pos == blen) { +if (pos == a_chars || pos == b_chars) {return false; }if (a[pos] ==0 && b[pos] ==0 ) {
@@ -100,18 +106,18 @@ gboolean uefi_str_equal_ex(const uint16_t *a, size_t alen, } } -gboolean uefi_str_equal (const uint16_t *a,size_t alen, -const uint16_t *b,size_t blen) +gboolean uefi_str_equal (const uint16_t *a,size_t a_bytes, +const uint16_t *b,size_t b_bytes) { -return uefi_str_equal_ex (a, alen, b, blen, false); +return uefi_str_equal_ex (a, a_bytes, b, b_bytes, false); } -char *uefi_ucs2_to_ascii (const uint16_t *ucs2,uint64_t ucs2_size) +char *uefi_ucs2_to_ascii (const uint16_t *ucs2,uint64_t ucs2_bytes) { -char *str =g_malloc0 (ucs2_size /2 +1 ); +char *str =g_malloc0 (ucs2_bytes /2 +1 );int i; -for (i =0 ; i *2 < ucs2_size; i++) { +for (i =0 ; i *2 < ucs2_bytes; i++) {if (ucs2[i] ==0 ) {break ; }
Signature · 1 line
2.53.0
PATCH 4/6 hw/uefi: add name_size check to uefi_vars_mm_lock_variable()
In reply to Gerd HoffmannMake sure the total variable_policy_entry size stays below 64k so the (16-bit) size field can not wrap.
--- hw/uefi/var-service-vars.c | 3 +++ 1 file changed, 3 insertions(+)
hw/uefi/var-service-vars.c +3 -0
@@ -667,6 +667,9 @@ uefi_vars_mm_lock_variable(uefi_vars_state *uv, mm_header *mhdr,if (mhdr->length < length) {return uefi_vars_mm_error (mhdr, mvar,EFI_BAD_BUFFER_SIZE ); } +if (sizeof (*pe) + lv->name_size >UINT16_MAX ) { +return uefi_vars_mm_error (mhdr, mvar,EFI_BAD_BUFFER_SIZE ); + }uefi_trace_variable (__func__, lv->guid, name, lv->name_size);
Signature · 1 line
2.53.0
PATCH 5/6 hw/uefi: verify data size before accessing it in wrap_pkcs7
In reply to Gerd Hoffmann--- hw/uefi/var-service-pkcs7.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/uefi/var-service-pkcs7.c +2 -1
@@ -73,7 +73,8 @@ static void wrap_pkcs7(gnutls_datum_t *pkcs7) };gnutls_datum_t wrap; - if (pkcs7->data[4] == 0x06 && + if (pkcs7->size > 16 && + pkcs7->data[4] == 0x06 && pkcs7->data[5] == 0x09 && memcmp(pkcs7->data + 6, signed_data_oid, sizeof(signed_data_oid)) == 0 && pkcs7->data[15] == 0x0a &&
Signature · 1 line
2.53.0
PATCH 6/6 hw/uefi: avoid possibly unaligned variable_auth_2 struct field access
In reply to Gerd HoffmannCopy data to stack-allocated struct before accessing it to make sure it is properly aligned.
--- hw/uefi/var-service-auth.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
hw/uefi/var-service-auth.c +9 -7
@@ -218,23 +218,25 @@ static efi_status uefi_vars_check_auth_2_sb(uefi_vars_state *uv,efi_status uefi_vars_check_auth_2 (uefi_vars_state *uv,uefi_variable *var,mm_variable_access *va,void *data) { -variable_auth_2 *auth = data; +variable_auth_2 auth;uint64_t data_offset;efi_status status; -if (va->data_size <sizeof (*auth)) { +if (va->data_size <sizeof (auth)) {return EFI_SECURITY_VIOLATION ; } -if (uadd64_overflow (sizeof (efi_time), auth->hdr_length, &data_offset)) { +memcpy (&auth, data,sizeof (auth)); + +if (uadd64_overflow (sizeof (efi_time), auth.hdr_length, &data_offset)) {return EFI_SECURITY_VIOLATION ; }if (va->data_size < data_offset) {return EFI_SECURITY_VIOLATION ; } -if (auth->hdr_revision !=0x0200 || - auth->hdr_cert_type !=WIN_CERT_TYPE_EFI_GUID || - !qemu_uuid_is_equal (&auth->guid_cert_type, &EfiCertTypePkcs7Guid)) { +if (auth.hdr_revision !=0x0200 || + auth.hdr_cert_type !=WIN_CERT_TYPE_EFI_GUID || + !qemu_uuid_is_equal (&auth.guid_cert_type, &EfiCertTypePkcs7Guid)) {return EFI_UNSUPPORTED ; }
@@ -255,7 +257,7 @@ efi_status uefi_vars_check_auth_2(uefi_vars_state *uv, uefi_variable *var, }/* checks passed, set variable data */ - var->time = auth->timestamp; + var->time = auth.timestamp;if (va->data_size - data_offset >0 ) { var->data =g_malloc (va->data_size - data_offset);memcpy (var->data, data + data_offset, va->data_size - data_offset);
Signature · 1 line
2.53.0
Re: [PATCH 6/6] hw/uefi: avoid possibly unaligned variable_auth_2 struct field access
Hi Gerd,
Quoted 60 lines · Gerd Hoffmann
Copy data to stack-allocated struct before accessing it to make sure it is properly aligned.
Fixes CVE-2026-41440Fixes f1488fac0584 ("hw/uefi: add var-service-auth.c")Reported-by Katherine Leaver <[email protected]>Signed-off-by Gerd Hoffmann <[email protected]>---
hw/uefi/var-service-auth.c| 16 +++++++++-------1 file changed, 9 insertions(+), 7 deletions(-)hw/uefi/var-service-auth.c +9
-7@@ -218,23 +218,25 @@ static efi_status uefi_vars_check_auth_2_sb(uefi_vars_state *uv,efi_status uefi_vars_check_auth_2 (uefi_vars_state *uv,uefi_variable *var,mm_variable_access *va,void *data) { -variable_auth_2 *auth = data; +variable_auth_2 auth;uint64_t data_offset;efi_status status; -if (va->data_size <sizeof (*auth)) { +if (va->data_size <sizeof (auth)) {return EFI_SECURITY_VIOLATION ; } -if (uadd64_overflow (sizeof (efi_time), auth->hdr_length, &data_offset)) { +memcpy (&auth, data,sizeof (auth)); + +if (uadd64_overflow (sizeof (efi_time), auth.hdr_length, &data_offset)) {return EFI_SECURITY_VIOLATION ; }if (va->data_size < data_offset) {return EFI_SECURITY_VIOLATION ; } -if (auth->hdr_revision !=0x0200 || - auth->hdr_cert_type !=WIN_CERT_TYPE_EFI_GUID || - !qemu_uuid_is_equal (&auth->guid_cert_type, &EfiCertTypePkcs7Guid)) { +if (auth.hdr_revision !=0x0200 || + auth.hdr_cert_type !=WIN_CERT_TYPE_EFI_GUID || + !qemu_uuid_is_equal (&auth.guid_cert_type, &EfiCertTypePkcs7Guid)) {return EFI_UNSUPPORTED ; }@@ -255,7 +257,7 @@ efi_status uefi_vars_check_auth_2(uefi_vars_state *uv, uefi_variable *var, }/* checks passed, set variable data */ - var->time = auth->timestamp; + var->time = auth.timestamp;if (va->data_size - data_offset >0 ) { var->data =g_malloc (va->data_size - data_offset);memcpy (var->data, data + data_offset, va->data_size - data_offset);Signature · 1 line
2.53.0
From a quick git grep I can see the same pattern in uefi_vars_check_auth_2_sb and in file hw/uefi/var-service-pkcs7.c in functions build_signed_data and build_pkcs7.
Should these fixed as well?
Thanks,
Luigi
Re: [PATCH 6/6] hw/uefi: avoid possibly unaligned variable_auth_2 struct field access
Quoted 18 lines · Luigi Leonardi
Hi Gerd,
Quoted 5 lines
(quoted diff) +1
-1- variable_auth_2 *auth = data; + variable_auth_2 auth; uint64_t data_offset; efi_status status;Review
Quoted 10 lines
(quoted diff) +4
-2- if (va->data_size < sizeof(*auth)) { + if (va->data_size < sizeof(auth)) { return EFI_SECURITY_VIOLATION; } - if (uadd64_overflow(sizeof(efi_time), auth->hdr_length, &data_offset)) { + memcpy(&auth, data, sizeof(auth)); + + if (uadd64_overflow(sizeof(efi_time), auth.hdr_length, &data_offset)) { return EFI_SECURITY_VIOLATION; }
Quoted 5 lines
From a quick
git grepI can see the same pattern inuefi_vars_check_auth_2_sband in filehw/uefi/var-service-pkcs7.cin functionsbuild_signed_dataandbuild_pkcs7.Should these fixed as well?
Yes. Incremental patch below.
take care,
Gerd
From 6f8e7c7f46729edce44b6b55631fa485cb2a8452 Mon Sep 17 00:00:00 2001
From: Gerd Hoffmann <[email protected]>
Date: Thu, 30 Apr 2026 09:50:57 +0200
Subject: [PATCH] fixup! hw/uefi: avoid possibly unaligned variable_auth_2
struct field access
--- hw/uefi/var-service-auth.c | 5 +++-- hw/uefi/var-service-pkcs7.c | 18 +++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-)
hw/uefi/var-service-auth.c +3 -2
@@ -180,9 +180,10 @@ static efi_status uefi_vars_check_auth_2_sb(uefi_vars_state *uv, void *data, uint64_t data_offset) { -variable_auth_2 *auth = data; +variable_auth_2 auth;uefi_variable *siglist; +memcpy (&auth, data,sizeof (auth));if (custom_mode_is_active (uv)) {/* no authentication in custom mode */ return EFI_SUCCESS ;
@@ -193,7 +194,7 @@ static efi_status uefi_vars_check_auth_2_sb(uefi_vars_state *uv,return EFI_SUCCESS ; } -if (auth->hdr_length ==24 ) { +if (auth.hdr_length ==24 ) {/* no signature (auth->cert_data is empty) */ return EFI_SECURITY_VIOLATION ; }
hw/uefi/var-service-pkcs7.c +11 -7
@@ -21,17 +21,20 @@*/ static gnutls_datum_t *build_signed_data (mm_variable_access *va,void *data) { -variable_auth_2 *auth = data; -uint64_t data_offset =sizeof (efi_time) + auth->hdr_length; +variable_auth_2 auth; +uint64_t data_offset;uint16_t *name = (void *)va +sizeof (mm_variable_access);gnutls_datum_t *sdata;uint64_t pos =0 ; +memcpy (&auth, data,sizeof (auth)); + data_offset =sizeof (efi_time) + auth.hdr_length; + sdata =g_new (gnutls_datum_t,1 ); sdata->size = (va->name_size -2 +sizeof (QemuUUID) +sizeof (va->attributes) - +sizeof (auth->timestamp) + +sizeof (auth.timestamp) + va->data_size - data_offset); sdata->data =g_malloc (sdata->size);
@@ -48,8 +51,8 @@ static gnutls_datum_t *build_signed_data(mm_variable_access *va, void *data) pos +=sizeof (va->attributes);/* TimeStamp */ -memcpy (sdata->data + pos, &auth->timestamp,sizeof (auth->timestamp)); - pos +=sizeof (auth->timestamp); +memcpy (sdata->data + pos, &auth.timestamp,sizeof (auth.timestamp)); + pos +=sizeof (auth.timestamp);/* Variable Content */ memcpy (sdata->data + pos, data + data_offset, va->data_size - data_offset);
@@ -105,11 +108,12 @@ static void wrap_pkcs7(gnutls_datum_t *pkcs7)static gnutls_datum_t *build_pkcs7 (void *data) { -variable_auth_2 *auth = data; +variable_auth_2 auth;gnutls_datum_t *pkcs7; +memcpy (&auth, data,sizeof (auth)); pkcs7 =g_new (gnutls_datum_t,1 ); - pkcs7->size = auth->hdr_length -24 ; + pkcs7->size = auth.hdr_length -24 ; pkcs7->data =g_malloc (pkcs7->size);memcpy (pkcs7->data, data +16 +24 , pkcs7->size);
Signature · 1 line
2.53.0