Skip to contentSkip to conversation

efi: fix and verify variables append_write

PATCH In review Vincent Stehlé 2 participants 4 messages Nov 13 – Nov 22

lore
Share & tools
lore
#0 lore raw

cover efi: fix and verify variables append_write

This couple of patches does the following:
- Fix a corner case of the non-TEE SetVariable(), which could record the APPEND_WRITE attribute when appending to non-existing variables.
- Enhance the existing selftest to verify that the APPEND_WRITE attribute is never returned by GetVariable().

See the second patch for the details to run on the sandbox.

I have also verified that the selftest changes do not harm with OP-TEE + StMM. (This hits other issues with QueryVariableInfo() but they are unrelated.)

We could reasonably reduce the test to verify the attributes only in the cases where GetVariable() follows a SetVariable() with the append attribute set, as those are arguably more likely to be buggy.
If this is preferred just let me know and I will send a v2.

---
Vincent Stehlé (2):
efi_loader: prevent set_variable() to record append_write
efi_selftest: verify get_variable() never returns append_write

 lib/efi_loader/efi_variable.c             |  3 ++-
 lib/efi_selftest/efi_selftest_variables.c | 17 ++++++++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)

---
base-commit: ff8a9bc41e4e5bdd3406932af5abb4c18064e539
change-id: 20261113-append-1cfc64c0e0d8

Best regards,

Signature · 1 line
Vincent.
#1 lore raw

PATCH 1/2 efi_loader: prevent set_variable() to record append_write

If SetVariable() records the Attributes bitmask with the EFI_VARIABLE_APPEND_WRITE attribute set, this attribute can be returned later on by GetVariable(), while this is forbidden by the UEFI specification. [1]

While the non-TEE SetVariable() already makes sure not to record the attribute when appending to an existing variable, it forgets to do so when appending to a non-existing variable; fix it.

---
 lib/efi_loader/efi_variable.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/efi_loader/efi_variable.c +2 -1
index 9923936c1b5..8bf2c91f3f9 100644
@@ -384,7 +384,8 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
 				      var->length, old_data, data_size, data,
 				      time, &changed);
 	} else {
-		ret = efi_var_mem_ins(variable_name, vendor, attributes,
+		ret = efi_var_mem_ins(variable_name, vendor,
+				      attributes & ~EFI_VARIABLE_APPEND_WRITE,
 				      data_size, data, 0, NULL, time,
 				      &changed);
 	}
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/2] efi_loader: prevent set_variable() to record append_write

Quoted 23 lines · Vincent Stehlé

If SetVariable() records the Attributes bitmask with the EFI_VARIABLE_APPEND_WRITE attribute set, this attribute can be returned later on by GetVariable(), while this is forbidden by the UEFI specification. [1]

While the non-TEE SetVariable() already makes sure not to record the attribute when appending to an existing variable, it forgets to do so when appending to a non-existing variable; fix it.

---
  lib/efi_loader/efi_variable.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
lib/efi_loader/efi_variable.c +0 -0
index 9923936c1b5..8bf2c91f3f9 100644

lib/efi_loader/efi_variable.c efi_status_t efi_set_variable_int(const u16 *variable_name, · line 388

Quoted 6 lines
lib/efi_loader/efi_variable.c +2 -1
@@ -384,7 +384,8 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
  				      var->length, old_data, data_size, data,
  				      time, &changed);
  	} else {
-		ret = efi_var_mem_ins(variable_name, vendor, attributes,
+		ret = efi_var_mem_ins(variable_name, vendor,
+				      attributes & ~EFI_VARIABLE_APPEND_WRITE,

Hello Vincent,

Thank you for detecting this bug.

If the variable does not exists yet (var == NULL) and data is provided (data_size > 0), you still end up passing around EFI_VARIABLE_APPEND_WRITE in unsolicited ways.

Should we remove the flag from attributes directly after setting variable append?

Best regards

Heinrich

Quoted 4 lines
lib/efi_loader/efi_variable.c +0 -0
  				      data_size, data, 0, NULL, time,
  				      &changed);
  	}
#3 lore raw

PATCH 2/2 efi_selftest: verify get_variable() never returns append_write

In reply to Vincent Stehlé

Verify that EFI_VARIABLE_APPEND_WRITE is never set in the Attributes bitmask returned by GetVariable() when it succeeds.

This can be run on the sandbox with the following command:

./u-boot -T -c 'setenv efi_selftest variables; bootefi selftest'

Cc Heinrich Schuchardt <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Tom Rini <[email protected]>
---
 lib/efi_selftest/efi_selftest_variables.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
lib/efi_selftest/efi_selftest_variables.c +16 -1
index 976eee8dcef..35d6387009d 100644
@@ -65,6 +65,10 @@ static int execute(void)
 		efi_st_error("GetVariable wrote past the end of the buffer\n");
 		return EFI_ST_FAILURE;
 	}
+	if (attr & EFI_VARIABLE_APPEND_WRITE) {
+		efi_st_error("APPEND_WRITE in returned attributes\n");
+		return EFI_ST_FAILURE;
+	}
 	/* Set variable 1 */
 	ret = st_runtime->set_variable(u"efi_st_var1", &guid_vendor1,
 				       EFI_VARIABLE_BOOTSERVICE_ACCESS, 8, v);
@@ -88,6 +92,10 @@ static int execute(void)
 		efi_st_error("GetVariable returned wrong value\n");
 		return EFI_ST_FAILURE;
 	}
+	if (attr & EFI_VARIABLE_APPEND_WRITE) {
+		efi_st_error("APPEND_WRITE in returned attributes\n");
+		return EFI_ST_FAILURE;
+	}
 	/* Append variable 1 */
 	ret = st_runtime->set_variable(u"efi_st_var1", &guid_vendor1,
 				       EFI_VARIABLE_BOOTSERVICE_ACCESS |
@@ -109,7 +117,10 @@ static int execute(void)
 			    (unsigned int)len);
 	if (memcmp(data, v, len))
 		efi_st_todo("GetVariable returned wrong value\n");
-
+	if (attr & EFI_VARIABLE_APPEND_WRITE) {
+		efi_st_error("APPEND_WRITE in returned attributes\n");
+		return EFI_ST_FAILURE;
+	}
 	/* Append variable 2, write to non-existent variable with datasize=0 */
 	ret = st_runtime->set_variable(u"efi_none", &guid_vendor1,
 				       EFI_VARIABLE_BOOTSERVICE_ACCESS |
@@ -149,6 +160,10 @@ static int execute(void)
 			    (unsigned int)len);
 	if (memcmp(data, v, len))
 		efi_st_todo("GetVariable returned wrong value\n");
+	if (attr & EFI_VARIABLE_APPEND_WRITE) {
+		efi_st_error("APPEND_WRITE in returned attributes\n");
+		return EFI_ST_FAILURE;
+	}
 	/* Delete variable efi_none */
 	ret = st_runtime->set_variable(u"efi_none", &guid_vendor1, 0, 0, NULL);
 	if (ret != EFI_SUCCESS) {
Signature · 1 line
2.53.0