Skip to contentSkip to conversation

efi_loader: update SetVariable attribute check

PATCH Superseded Masahisa Kojima 2 participants 3 messages Feb 15, 2023

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 efi_loader: update SetVariable attribute check

UEFI specification v2.10 says that EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated and EFI_UNSUPPORTED should be returned in SetVariable variable service. Current implementation returns EFI_INVALID_PARAMETER, let's fix the return value.

Together with above change, this commit also updates the SetVariable attribute check to be aligned with the EDK2 reference implementation.

---
Changes in v2:
- fix coding style
- HR must be set with NV

 lib/efi_loader/efi_variable.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
lib/efi_loader/efi_variable.c +26 -7
index 4c85cfa607..b12e79d658 100644
@@ -230,9 +230,31 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
 	u64 time = 0;
 	enum efi_auth_var_type var_type;
 
-	if (!variable_name || !*variable_name || !vendor ||
-	    ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&
-	     !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)))
+	if (!variable_name || !*variable_name || !vendor)
+		return EFI_INVALID_PARAMETER;
+
+	if (data_size && !data)
+		return EFI_INVALID_PARAMETER;
+
+	/* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */
+	if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS ||
+	    (!(attributes & EFI_VARIABLE_MASK)))
+		return EFI_UNSUPPORTED;
+
+	/* Make sure if runtime bit is set, boot service bit is set also */
+	if ((attributes &
+	     (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
+	    EFI_VARIABLE_RUNTIME_ACCESS)
+		return EFI_INVALID_PARAMETER;
+
+	/* only EFI_VARIABLE_NON_VOLATILE attribute is invalid */
+	if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE)
+		return EFI_INVALID_PARAMETER;
+
+	/* Make sure HR is set with NV */
+	if ((attributes &
+	     (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) ==
+	    EFI_VARIABLE_HARDWARE_ERROR_RECORD)
 		return EFI_INVALID_PARAMETER;
 
 	/* check if a variable exists */
@@ -281,8 +303,6 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
 
 	/* authenticate a variable */
 	if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
-		if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
-			return EFI_INVALID_PARAMETER;
 		if (attributes &
 		    EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
 			u32 env_attr;
@@ -300,8 +320,7 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
 		}
 	} else {
 		if (attributes &
-		    (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
-		     EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
+		    EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
 			EFI_PRINT("Secure boot is not configured\n");
 			return EFI_INVALID_PARAMETER;
 		}
Signature · 1 line
2.17.1
#1 lore raw
Quoted 28 lines · Masahisa Kojima

UEFI specification v2.10 says that EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated and EFI_UNSUPPORTED should be returned in SetVariable variable service. Current implementation returns EFI_INVALID_PARAMETER, let's fix the return value.

Together with above change, this commit also updates the SetVariable attribute check to be aligned with the EDK2 reference implementation.

---
Changes in v2:
- fix coding style
- HR must be set with NV

  lib/efi_loader/efi_variable.c | 33 ++++++++++++++++++++++++++-------
  1 file changed, 26 insertions(+), 7 deletions(-)
lib/efi_loader/efi_variable.c +0 -2
index 4c85cfa607..b12e79d658 100644
@@ -230,9 +230,31 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
  	u64 time = 0;
  	enum efi_auth_var_type var_type;
 
-	if (!variable_name || !*variable_name || !vendor ||
-	    ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&

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

Quoted 10 lines
lib/efi_loader/efi_variable.c +9 -1
-	     !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)))
+	if (!variable_name || !*variable_name || !vendor)
+		return EFI_INVALID_PARAMETER;
+
+	if (data_size && !data)
+		return EFI_INVALID_PARAMETER;
+
+	/* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */
+	if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS ||
+	    (!(attributes & EFI_VARIABLE_MASK)))

UEFI Spec 2.10, p. 215: "Setting a data variable with no access attributes causes it to be deleted."

We don't want to make deletion unsupported.

Best regards

Heinrich

Quoted 38 lines
lib/efi_loader/efi_variable.c +17 -4
+		return EFI_UNSUPPORTED;
+
+	/* Make sure if runtime bit is set, boot service bit is set also */
+	if ((attributes &
+	     (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
+	    EFI_VARIABLE_RUNTIME_ACCESS)
+		return EFI_INVALID_PARAMETER;
+
+	/* only EFI_VARIABLE_NON_VOLATILE attribute is invalid */
+	if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE)
+		return EFI_INVALID_PARAMETER;
+
+	/* Make sure HR is set with NV */
+	if ((attributes &
+	     (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) ==
+	    EFI_VARIABLE_HARDWARE_ERROR_RECORD)
  		return EFI_INVALID_PARAMETER;
 
  	/* check if a variable exists */
@@ -281,8 +303,6 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
 
  	/* authenticate a variable */
  	if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
-		if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
-			return EFI_INVALID_PARAMETER;
  		if (attributes &
  		    EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
  			u32 env_attr;
@@ -300,8 +320,7 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
  		}
  	} else {
  		if (attributes &
-		    (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
-		     EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
+		    EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
  			EFI_PRINT("Secure boot is not configured\n");
  			return EFI_INVALID_PARAMETER;
  		}
#2 lore raw

Hi Heinrich,

Quoted 44 lines · Heinrich Schuchardt
Quoted 28 lines · Masahisa Kojima

UEFI specification v2.10 says that EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated and EFI_UNSUPPORTED should be returned in SetVariable variable service. Current implementation returns EFI_INVALID_PARAMETER, let's fix the return value.

Together with above change, this commit also updates the SetVariable attribute check to be aligned with the EDK2 reference implementation.

---
Changes in v2:
- fix coding style
- HR must be set with NV

  lib/efi_loader/efi_variable.c | 33 ++++++++++++++++++++++++++-------
  1 file changed, 26 insertions(+), 7 deletions(-)
lib/efi_loader/efi_variable.c +0 -2
index 4c85cfa607..b12e79d658 100644
@@ -230,9 +230,31 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
      u64 time = 0;
      enum efi_auth_var_type var_type;
 
-     if (!variable_name || !*variable_name || !vendor ||
-         ((attributes & EFI_VARIABLE_RUNTIME_ACCESS) &&

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

Quoted 10 lines
lib/efi_loader/efi_variable.c +9 -1
-          !(attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)))
+     if (!variable_name || !*variable_name || !vendor)
+             return EFI_INVALID_PARAMETER;
+
+     if (data_size && !data)
+             return EFI_INVALID_PARAMETER;
+
+     /* EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated */
+     if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS ||
+         (!(attributes & EFI_VARIABLE_MASK)))

UEFI Spec 2.10, p. 215: "Setting a data variable with no access attributes causes it to be deleted."

Quoted 1 line

We don't want to make deletion unsupported.

Sorry, you are correct. I will remove the second condition.

Thanks,
Masahisa Kojima

Quoted 44 lines

Best regards

Heinrich

Quoted 38 lines
lib/efi_loader/efi_variable.c +17 -4
+             return EFI_UNSUPPORTED;
+
+     /* Make sure if runtime bit is set, boot service bit is set also */
+     if ((attributes &
+          (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) ==
+         EFI_VARIABLE_RUNTIME_ACCESS)
+             return EFI_INVALID_PARAMETER;
+
+     /* only EFI_VARIABLE_NON_VOLATILE attribute is invalid */
+     if ((attributes & EFI_VARIABLE_MASK) == EFI_VARIABLE_NON_VOLATILE)
+             return EFI_INVALID_PARAMETER;
+
+     /* Make sure HR is set with NV */
+     if ((attributes &
+          (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) ==
+         EFI_VARIABLE_HARDWARE_ERROR_RECORD)
              return EFI_INVALID_PARAMETER;
 
      /* check if a variable exists */
@@ -281,8 +303,6 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
 
      /* authenticate a variable */
      if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
-             if (attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
-                     return EFI_INVALID_PARAMETER;
              if (attributes &
                  EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
                      u32 env_attr;
@@ -300,8 +320,7 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
              }
      } else {
              if (attributes &
-                 (EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS |
-                  EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {
+                 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {
                      EFI_PRINT("Secure boot is not configured\n");
                      return EFI_INVALID_PARAMETER;
              }