Skip to contentSkip to conversation

efi_loader: variable: attributes may not be changed if a variable exists

PATCH New AKASHI Takahiro 2 participants 7 messages May 24, 2019

lore
Share & tools
lore
#0 lore raw

v2 cover efi_loader: variable: attributes may not be changed if a variable exists

Changes in v2
* add patch#1 and patch#2 to avoid sefltest failure
* doesn't free a buffer returned by env_get() (patch#3)

AKASHI Takahiro (3):
efi: selftest: APPEND_WRITE is not supported
efi_loader: variable: return error for APPEND_WRITE
efi_loader: variable: attributes may not be changed if a variable
exists

 lib/efi_loader/efi_variable.c             | 16 ++++++++++---
 lib/efi_selftest/efi_selftest_variables.c | 28 +++++++++++------------
 2 files changed, 27 insertions(+), 17 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 1/3 efi: selftest: APPEND_WRITE is not supported

The error here should be marked *todo*.

---
 lib/efi_selftest/efi_selftest_variables.c | 28 +++++++++++------------
 1 file changed, 14 insertions(+), 14 deletions(-)
lib/efi_selftest/efi_selftest_variables.c +14 -14
index b028c64bbc85..06c1a032dd04 100644
@@ -116,21 +116,21 @@ static int execute(void)
 				    EFI_VARIABLE_APPEND_WRITE,
 				    7, v + 8);
 	if (ret != EFI_SUCCESS) {
-		efi_st_error("SetVariable failed\n");
-		return EFI_ST_FAILURE;
-	}
-	len = EFI_ST_MAX_DATA_SIZE;
-	ret = runtime->get_variable(L"efi_st_var1", &guid_vendor1,
-				    &attr, &len, data);
-	if (ret != EFI_SUCCESS) {
-		efi_st_error("GetVariable failed\n");
-		return EFI_ST_FAILURE;
+		efi_st_todo("SetVariable(APPEND_WRITE) failed\n");
+	} else {
+		len = EFI_ST_MAX_DATA_SIZE;
+		ret = runtime->get_variable(L"efi_st_var1", &guid_vendor1,
+					    &attr, &len, data);
+		if (ret != EFI_SUCCESS) {
+			efi_st_error("GetVariable failed\n");
+			return EFI_ST_FAILURE;
+		}
+		if (len != 15)
+			efi_st_todo("GetVariable returned wrong length %u\n",
+				    (unsigned int)len);
+		if (memcmp(data, v, len))
+			efi_st_todo("GetVariable returned wrong value\n");
 	}
-	if (len != 15)
-		efi_st_todo("GetVariable returned wrong length %u\n",
-			    (unsigned int)len);
-	if (memcmp(data, v, len))
-		efi_st_todo("GetVariable returned wrong value\n");
 	/* Enumerate variables */
 	boottime->set_mem(&guid, 16, 0);
 	*varname = 0;
Signature · 1 line
2.21.0
#3 lore raw

PATCH v2 2/3 efi_loader: variable: return error for APPEND_WRITE

In reply to AKASHI Takahiro

The current efi_et_variable() doesn't support EFI_VARIABLE_APPEND_WRITE attiribut for now, and so should return an error.

---
 lib/efi_loader/efi_variable.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/efi_loader/efi_variable.c +2 -1
index 28b1aa7505ae..e3ec502ffb45 100644
@@ -427,7 +427,8 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
 	EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
 		  data_size, data);
 
-	if (!variable_name || !vendor) {
+	if (!variable_name || !vendor ||
+	    (attributes & EFI_VARIABLE_APPEND_WRITE)) {
 		ret = EFI_INVALID_PARAMETER;
 		goto out;
 	}
Signature · 1 line
2.21.0
#4 lore raw

PATCH v2 2/3 efi_loader: variable: return error for APPEND_WRITE

Quoted 4 lines · AKASHI Takahiro

The current efi_et_variable() doesn't support EFI_VARIABLE_APPEND_WRITE attiribut for now, and so should return an error.

I will correct the typos in the commit message and add a TODO comment in the code.

Quoted 19 lines
---
 lib/efi_loader/efi_variable.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/efi_loader/efi_variable.c +2 -1
index 28b1aa7505ae..e3ec502ffb45 100644
@@ -427,7 +427,8 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
 	EFI_ENTRY("\"%ls\" %pUl %x %zu %p", variable_name, vendor, attributes,
 		  data_size, data);
 
-	if (!variable_name || !vendor) {
+	if (!variable_name || !vendor ||
+	    (attributes & EFI_VARIABLE_APPEND_WRITE)) {
 		ret = EFI_INVALID_PARAMETER;
 		goto out;
 	}
#5 lore raw

PATCH v2 3/3 efi_loader: variable: attributes may not be changed if a variable exists

In reply to AKASHI Takahiro

If a variable already exists, efi_set_variable() should not change the variable's attributes. This patch enforces it.

---
 lib/efi_loader/efi_variable.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
lib/efi_loader/efi_variable.c +11 -2
index e3ec502ffb45..1bb3bbf3393e 100644
@@ -450,12 +450,21 @@ efi_status_t EFIAPI efi_set_variable(u16 *variable_name,
 	if (val) {
 		parse_attr(val, &attr);
 
+		/* We should not free val */
+		val = NULL;
 		if (attr & READ_ONLY) {
-			/* We should not free val */
-			val = NULL;
 			ret = EFI_WRITE_PROTECTED;
 			goto out;
 		}
+
+		/*
+		 * attributes won't be changed
+		 * TODO: take care of APPEND_WRITE once supported
+		 */
+		if (attr != attributes) {
+			ret = EFI_INVALID_PARAMETER;
+			goto out;
+		}
 	}
 
 	val = malloc(2 * data_size + strlen("{ro,run,boot}(blob)") + 1);
Signature · 1 line
2.21.0
#6 lore raw

PATCH v2 3/3 efi_loader: variable: attributes may not be changed if a variable exists

Quoted 4 lines · AKASHI Takahiro

If a variable already exists, efi_set_variable() should not change the variable's attributes. This patch enforces it.

Quoted 3 lines
---
 lib/efi_loader/efi_variable.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)