efi: stmm: Fix for incorrect buffer allocation and cleanups
v3 cover efi: stmm: Fix for incorrect buffer allocation and cleanups
Changes in v3:
- improve description of patch 3
Changes in v2:
- simplify page allocation approach
- switch to returning EFI_DEVICE_ERROR on ENOMEM
- drop efi return value argument from setup_mm_hdr
One critical fix for the EFI StMM driver, one error return code adjustment and two smaller cleanups.
Note that the suggestion to convert tee_shm_register_kernel_buf to take pages instead of buffer addresses is not forgotten, just in the backlog. I wanted to avoid that the critical fix has to wait for me finding time to refactor the API as well.
Jan
Jan Kiszka (4):
efi: stmm: Fix incorrect buffer allocation method
efi: stmm: Do not return EFI_OUT_OF_RESOURCES on internal errors
efi: stmm: Drop unused EFI error from setup_mm_hdr arguments
efi: stmm: Drop unneeded null pointer check
drivers/firmware/efi/stmm/tee_stmm_efi.c | 61 +++++++++++------------- 1 file changed, 27 insertions(+), 34 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 1/4 efi: stmm: Fix incorrect buffer allocation method
From: Jan Kiszka <[email protected]>
The communication buffer allocated by setup_mm_hdr is later on passed to tee_shm_register_kernel_buf. The latter expects those buffers to be contiguous pages, but setup_mm_hdr just uses kmalloc. That can cause various corruptions or BUGs, specifically since 9aec2fb0fd5e, though it was broken before as well.
Fix this by using alloc_pages_exact instead of kmalloc.
--- drivers/firmware/efi/stmm/tee_stmm_efi.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/firmware/efi/stmm/tee_stmm_efi.c +12 -9
@@ -143,6 +143,10 @@ static efi_status_t mm_communicate(u8 *comm_buf, size_t payload_size)return var_hdr->ret_status; } +#define COMM_BUF_SIZE (__payload_size) (MM_COMMUNICATE_HEADER_SIZE + \ + MM_VARIABLE_COMMUNICATE_SIZE + \ + (__payload_size)) +/** * setup_mm_hdr() - Allocate a buffer for StandAloneMM and initialize the * header data.
@@ -173,9 +177,8 @@ static void *setup_mm_hdr(u8 **dptr, size_t payload_size, size_t func,return NULL ; } - comm_buf =kzalloc (MM_COMMUNICATE_HEADER_SIZE + -MM_VARIABLE_COMMUNICATE_SIZE + payload_size, -GFP_KERNEL ); + comm_buf =alloc_pages_exact (COMM_BUF_SIZE (payload_size), +GFP_KERNEL | __GFP_ZERO);if (!comm_buf) { *ret =EFI_OUT_OF_RESOURCES ;return NULL ;
@@ -239,7 +242,7 @@ static efi_status_t get_max_payload(size_t *size)*/ *size -=2 ; out: -kfree (comm_buf); +free_pages_exact (comm_buf,COMM_BUF_SIZE (payload_size));return ret; }
@@ -282,7 +285,7 @@ static efi_status_t get_property_int(u16 *name, size_t name_size,memcpy (var_property, &smm_property->property,sizeof (*var_property)); out: -kfree (comm_buf); +free_pages_exact (comm_buf,COMM_BUF_SIZE (payload_size));return ret; }
@@ -347,7 +350,7 @@ static efi_status_t tee_get_variable(u16 *name, efi_guid_t *vendor,memcpy (data, (u8 *)var_acc->name + var_acc->name_size, var_acc->data_size); out: -kfree (comm_buf); +free_pages_exact (comm_buf,COMM_BUF_SIZE (payload_size));return ret; }
@@ -404,7 +407,7 @@ static efi_status_t tee_get_next_variable(unsigned long *name_size,memcpy (name, var_getnext->name, var_getnext->name_size); out: -kfree (comm_buf); +free_pages_exact (comm_buf,COMM_BUF_SIZE (payload_size));return ret; }
@@ -467,7 +470,7 @@ static efi_status_t tee_set_variable(efi_char16_t *name, efi_guid_t *vendor, ret =mm_communicate (comm_buf, payload_size);dev_dbg (pvt_data.dev,"Set Variable %s %d %lx\n" , __FILE__, __LINE__, ret); out: -kfree (comm_buf); +free_pages_exact (comm_buf,COMM_BUF_SIZE (payload_size));return ret; }
@@ -507,7 +510,7 @@ static efi_status_t tee_query_variable_info(u32 attributes, *max_variable_size = mm_query_info->max_variable_size; out: -kfree (comm_buf); +free_pages_exact (comm_buf,COMM_BUF_SIZE (payload_size));return ret; }
Signature · 1 line
2.43.0
PATCH v3 2/4 efi: stmm: Do not return EFI_OUT_OF_RESOURCES on internal errors
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
When we are low on memory or when the internal API is violated, we cannot return EFI_OUT_OF_RESOURCES. According to the UEFI standard, that error code is either related to persistent storage used for the variable or even not foreseen as possible error (GetVariable e.g.). Use the not fully accurate but compliant error code EFI_DEVICE_ERROR in those cases.
--- drivers/firmware/efi/stmm/tee_stmm_efi.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/firmware/efi/stmm/tee_stmm_efi.c +6 -6
@@ -218,7 +218,7 @@ static efi_status_t get_max_payload(size_t *size)SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE , &ret);if (!var_payload) -return EFI_OUT_OF_RESOURCES ; +return EFI_DEVICE_ERROR ; ret =mm_communicate (comm_buf, payload_size);if (ret !=EFI_SUCCESS )
@@ -264,7 +264,7 @@ static efi_status_t get_property_int(u16 *name, size_t name_size, &comm_buf, payload_size, SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET, &ret);if (!smm_property) -return EFI_OUT_OF_RESOURCES ; +return EFI_DEVICE_ERROR ;memcpy (&smm_property->guid, vendor,sizeof (smm_property->guid)); smm_property->name_size = name_size;
@@ -320,7 +320,7 @@ static efi_status_t tee_get_variable(u16 *name, efi_guid_t *vendor, var_acc =setup_mm_hdr (&comm_buf, payload_size,SMM_VARIABLE_FUNCTION_GET_VARIABLE , &ret);if (!var_acc) -return EFI_OUT_OF_RESOURCES ; +return EFI_DEVICE_ERROR ;/* Fill in contents */ memcpy (&var_acc->guid, vendor,sizeof (var_acc->guid));
@@ -386,7 +386,7 @@ static efi_status_t tee_get_next_variable(unsigned long *name_size,SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME , &ret);if (!var_getnext) -return EFI_OUT_OF_RESOURCES ; +return EFI_DEVICE_ERROR ;/* Fill in contents */ memcpy (&var_getnext->guid, guid,sizeof (var_getnext->guid));
@@ -442,7 +442,7 @@ static efi_status_t tee_set_variable(efi_char16_t *name, efi_guid_t *vendor, var_acc =setup_mm_hdr (&comm_buf, payload_size,SMM_VARIABLE_FUNCTION_SET_VARIABLE , &ret);if (!var_acc) -return EFI_OUT_OF_RESOURCES ; +return EFI_DEVICE_ERROR ;/* * The API has the ability to override RO flags. If no RO check was
@@ -498,7 +498,7 @@ static efi_status_t tee_query_variable_info(u32 attributes,SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO , &ret);if (!mm_query_info) -return EFI_OUT_OF_RESOURCES ; +return EFI_DEVICE_ERROR ; mm_query_info->attr = attributes; ret =mm_communicate (comm_buf, payload_size);
Signature · 1 line
2.43.0
PATCH v3 3/4 efi: stmm: Drop unused EFI error from setup_mm_hdr arguments
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
No caller ever evaluates what we return in 'ret'. They only use the return code of the function.
--- drivers/firmware/efi/stmm/tee_stmm_efi.c | 25 ++++++++---------------- 1 file changed, 8 insertions(+), 17 deletions(-)
drivers/firmware/efi/stmm/tee_stmm_efi.c +8 -17
@@ -154,11 +154,9 @@ static efi_status_t mm_communicate(u8 *comm_buf, size_t payload_size)* @dptr: pointer address to store allocated buffer * @payload_size: payload size * @func: standAloneMM function number -* @ret: EFI return code * Return: pointer to corresponding StandAloneMM function buffer or NULL */ -static void *setup_mm_hdr (u8 **dptr,size_t payload_size,size_t func, -efi_status_t *ret) +static void *setup_mm_hdr (u8 **dptr,size_t payload_size,size_t func) {const efi_guid_t mm_var_guid =EFI_MM_VARIABLE_GUID ;struct efi_mm_communicate_header *mm_hdr;
@@ -173,16 +171,13 @@ static void *setup_mm_hdr(u8 **dptr, size_t payload_size, size_t func,if (max_buffer_size && max_buffer_size < (MM_COMMUNICATE_HEADER_SIZE +MM_VARIABLE_COMMUNICATE_SIZE + payload_size)) { - *ret =EFI_INVALID_PARAMETER ;return NULL ; } comm_buf =alloc_pages_exact (COMM_BUF_SIZE (payload_size),GFP_KERNEL | __GFP_ZERO); -if (!comm_buf) { - *ret =EFI_OUT_OF_RESOURCES ; +if (!comm_buf)return NULL ; - } mm_hdr = (struct efi_mm_communicate_header *)comm_buf;memcpy (&mm_hdr->header_guid, &mm_var_guid,sizeof (mm_hdr->header_guid));
@@ -192,7 +187,6 @@ static void *setup_mm_hdr(u8 **dptr, size_t payload_size, size_t func, var_hdr->function = func;if (dptr) *dptr = comm_buf; - *ret =EFI_SUCCESS ;return var_hdr->data; }
@@ -215,8 +209,7 @@ static efi_status_t get_max_payload(size_t *size) payload_size =sizeof (*var_payload); var_payload =setup_mm_hdr (&comm_buf, payload_size, -SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE , - &ret); +SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE );if (!var_payload)return EFI_DEVICE_ERROR ;
@@ -262,7 +255,7 @@ static efi_status_t get_property_int(u16 *name, size_t name_size, smm_property =setup_mm_hdr ( &comm_buf, payload_size, -SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET , &ret); +SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET );if (!smm_property)return EFI_DEVICE_ERROR ;
@@ -318,7 +311,7 @@ static efi_status_t tee_get_variable(u16 *name, efi_guid_t *vendor, payload_size =MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + tmp_dsize; var_acc =setup_mm_hdr (&comm_buf, payload_size, -SMM_VARIABLE_FUNCTION_GET_VARIABLE , &ret); +SMM_VARIABLE_FUNCTION_GET_VARIABLE );if (!var_acc)return EFI_DEVICE_ERROR ;
@@ -383,8 +376,7 @@ static efi_status_t tee_get_next_variable(unsigned long *name_size, payload_size =MM_VARIABLE_GET_NEXT_HEADER_SIZE + out_name_size; var_getnext =setup_mm_hdr (&comm_buf, payload_size, -SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME , - &ret); +SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME );if (!var_getnext)return EFI_DEVICE_ERROR ;
@@ -440,7 +432,7 @@ static efi_status_t tee_set_variable(efi_char16_t *name, efi_guid_t *vendor,* the properties, if the allocation fails */ var_acc =setup_mm_hdr (&comm_buf, payload_size, -SMM_VARIABLE_FUNCTION_SET_VARIABLE , &ret); +SMM_VARIABLE_FUNCTION_SET_VARIABLE );if (!var_acc)return EFI_DEVICE_ERROR ;
@@ -495,8 +487,7 @@ static efi_status_t tee_query_variable_info(u32 attributes, payload_size =sizeof (*mm_query_info); mm_query_info =setup_mm_hdr (&comm_buf, payload_size, -SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO , - &ret); +SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO );if (!mm_query_info)return EFI_DEVICE_ERROR ;
Signature · 1 line
2.43.0
PATCH v3 4/4 efi: stmm: Drop unneeded null pointer check
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
The API documenation of setup_mm_hdr does not mention that dptr can be NULL, this is a local function, and no caller passes NULL. So drop the unneeded check.
--- drivers/firmware/efi/stmm/tee_stmm_efi.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/firmware/efi/stmm/tee_stmm_efi.c +1 -2
@@ -185,8 +185,7 @@ static void *setup_mm_hdr(u8 **dptr, size_t payload_size, size_t func) var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data; var_hdr->function = func; -if (dptr) - *dptr = comm_buf; + *dptr = comm_buf;return var_hdr->data; }
Signature · 1 line
2.43.0
Quoted 18 lines · Jan Kiszka
Changes in v3:
- improve description of patch 3Changes in v2:
- simplify page allocation approach
- switch to returningEFI_DEVICE_ERRORon ENOMEM
- drop efi return value argument fromsetup_mm_hdrOne critical fix for the EFI StMM driver, one error return code adjustment and two smaller cleanups.
Note that the suggestion to convert
tee_shm_register_kernel_bufto take pages instead of buffer addresses is not forgotten, just in the backlog. I wanted to avoid that the critical fix has to wait for me finding time to refactor the API as well.Jan
Quoted 8 lines
Jan Kiszka (4):
efi: stmm: Fix incorrect buffer allocation method
efi: stmm: Do not returnEFI_OUT_OF_RESOURCESon internal errors
efi: stmm: Drop unused EFI error fromsetup_mm_hdrarguments
efi: stmm: Drop unneeded null pointer checkdrivers/firmware/efi/stmm/tee_stmm_efi.c | 61 +++++++++++------------- 1 file changed, 27 insertions(+), 34 deletions(-)
LGTM, for the series:
-Sumit
Ard Biesheuvel
maintainerQuoted 26 lines · Sumit Garg
Quoted 24 lines · Jan Kiszka
Changes in v3:
- improve description of patch 3Changes in v2:
- simplify page allocation approach
- switch to returningEFI_DEVICE_ERRORon ENOMEM
- drop efi return value argument fromsetup_mm_hdrOne critical fix for the EFI StMM driver, one error return code adjustment and two smaller cleanups.
Note that the suggestion to convert
tee_shm_register_kernel_bufto take pages instead of buffer addresses is not forgotten, just in the backlog. I wanted to avoid that the critical fix has to wait for me finding time to refactor the API as well.Jan
Jan Kiszka (4):
efi: stmm: Fix incorrect buffer allocation method
efi: stmm: Do not returnEFI_OUT_OF_RESOURCESon internal errors
efi: stmm: Drop unused EFI error fromsetup_mm_hdrarguments
efi: stmm: Drop unneeded null pointer check
Quoted 7 lines
Quoted 2 lines
drivers/firmware/efi/stmm/tee_stmm_efi.c | 61 +++++++++++------------- 1 file changed, 27 insertions(+), 34 deletions(-)LGTM, for the series:
Acked-by Sumit Garg <[email protected]>
Thanks all - I've queued these up now.