]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/AuthVariableLib/AuthVariableLib.c
SecurityPkg: enhance secure boot Config Dxe & Time Based AuthVariable.
[mirror_edk2.git] / SecurityPkg / Library / AuthVariableLib / AuthVariableLib.c
index bf60bf54e5cb97936f0573829ecd4aa4c608f7cf..792a1232aed968a9dde8a8a9b5fb64d87fd3238b 100644 (file)
@@ -11,7 +11,7 @@
   may not be modified without authorization. If platform fails to protect these resources,\r
   the authentication service provided in this driver will be broken, and the behavior is undefined.\r
 \r
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -33,6 +33,7 @@ UINT32   mMaxKeyNumber;
 UINT32   mMaxKeyDbSize;\r
 UINT8    *mCertDbStore;\r
 UINT32   mMaxCertDbSize;\r
+UINT32   mPlatformMode;\r
 UINT8    mVendorKeyState;\r
 \r
 EFI_GUID mSignatureSupport[] = {EFI_CERT_SHA1_GUID, EFI_CERT_SHA256_GUID, EFI_CERT_RSA2048_GUID, EFI_CERT_X509_GUID};\r
@@ -99,16 +100,16 @@ VARIABLE_ENTRY_PROPERTY mAuthVarEntry[] = {
     }\r
   },\r
   {\r
-    &gEdkiiSecureBootModeGuid,\r
-    L"SecureBootMode",\r
+    &gEfiCertDbGuid,\r
+    EFI_CERT_DB_VOLATILE_NAME,\r
     {\r
       VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r
       VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r
-      VARIABLE_ATTRIBUTE_NV_BS_RT,\r
-      sizeof (UINT8),\r
-      sizeof (UINT8)\r
+      VARIABLE_ATTRIBUTE_BS_RT_AT,\r
+      sizeof (UINT32),\r
+      MAX_UINTN\r
     }\r
-  }\r
+  },\r
 };\r
 \r
 VOID **mAuthVarAddressPointer[10];\r
@@ -142,6 +143,8 @@ AuthVariableLibInitialize (
   UINT8                 *Data;\r
   UINTN                 DataSize;\r
   UINTN                 CtxSize;\r
+  UINT8                 SecureBootMode;\r
+  UINT8                 SecureBootEnable;\r
   UINT8                 CustomMode;\r
   UINT32                ListSize;\r
 \r
@@ -172,8 +175,9 @@ AuthVariableLibInitialize (
 \r
   //\r
   // Reserve runtime buffer for certificate database. The size excludes variable header and name size.\r
+  // Use EFI_CERT_DB_VOLATILE_NAME size since it is longer.\r
   //\r
-  mMaxCertDbSize = (UINT32) (mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (EFI_CERT_DB_NAME));\r
+  mMaxCertDbSize = (UINT32) (mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (EFI_CERT_DB_VOLATILE_NAME));\r
   mCertDbStore   = AllocateRuntimePool (mMaxCertDbSize);\r
   if (mCertDbStore == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -216,11 +220,31 @@ AuthVariableLibInitialize (
     mPubKeyNumber = (UINT32) (DataSize / sizeof (AUTHVAR_KEY_DB_DATA));\r
   }\r
 \r
+  Status = AuthServiceInternalFindVariable (EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid, (VOID **) &Data, &DataSize);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_INFO, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME));\r
+  } else {\r
+    DEBUG ((EFI_D_INFO, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME));\r
+  }\r
+\r
   //\r
-  // Init Secure Boot variables\r
+  // Create "SetupMode" variable with BS+RT attribute set.\r
   //\r
-  Status = InitSecureBootVariables ();\r
-\r
+  if (EFI_ERROR (Status)) {\r
+    mPlatformMode = SETUP_MODE;\r
+  } else {\r
+    mPlatformMode = USER_MODE;\r
+  }\r
+  Status = AuthServiceInternalUpdateVariable (\r
+             EFI_SETUP_MODE_NAME,\r
+             &gEfiGlobalVariableGuid,\r
+             &mPlatformMode,\r
+             sizeof(UINT8),\r
+             EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
   //\r
   // Create "SignatureSupport" variable with BS+RT attribute set.\r
@@ -236,6 +260,57 @@ AuthVariableLibInitialize (
     return Status;\r
   }\r
 \r
+  //\r
+  // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.\r
+  // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.\r
+  // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.\r
+  //\r
+  SecureBootEnable = SECURE_BOOT_DISABLE;\r
+  Status = AuthServiceInternalFindVariable (EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, (VOID **) &Data, &DataSize);\r
+  if (!EFI_ERROR (Status)) {\r
+    if (mPlatformMode == USER_MODE){\r
+      SecureBootEnable = *(UINT8 *) Data;\r
+    }\r
+  } else if (mPlatformMode == USER_MODE) {\r
+    //\r
+    // "SecureBootEnable" not exist, initialize it in USER_MODE.\r
+    //\r
+    SecureBootEnable = SECURE_BOOT_ENABLE;\r
+    Status = AuthServiceInternalUpdateVariable (\r
+               EFI_SECURE_BOOT_ENABLE_NAME,\r
+               &gEfiSecureBootEnableDisableGuid,\r
+               &SecureBootEnable,\r
+               sizeof (UINT8),\r
+               EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Create "SecureBoot" variable with BS+RT attribute set.\r
+  //\r
+  if (SecureBootEnable == SECURE_BOOT_ENABLE && mPlatformMode == USER_MODE) {\r
+    SecureBootMode = SECURE_BOOT_MODE_ENABLE;\r
+  } else {\r
+    SecureBootMode = SECURE_BOOT_MODE_DISABLE;\r
+  }\r
+  Status = AuthServiceInternalUpdateVariable (\r
+             EFI_SECURE_BOOT_MODE_NAME,\r
+             &gEfiGlobalVariableGuid,\r
+             &SecureBootMode,\r
+             sizeof (UINT8),\r
+             EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SETUP_MODE_NAME, mPlatformMode));\r
+  DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME, SecureBootMode));\r
+  DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME, SecureBootEnable));\r
+\r
   //\r
   // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.\r
   //\r
@@ -288,6 +363,22 @@ AuthVariableLibInitialize (
     }\r
   }\r
 \r
+  //\r
+  // Create "certdbv" variable with RT+BS+AT set.\r
+  //\r
+  VarAttr  = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;\r
+  ListSize = sizeof (UINT32);\r
+  Status   = AuthServiceInternalUpdateVariable (\r
+               EFI_CERT_DB_VOLATILE_NAME,\r
+               &gEfiCertDbGuid,\r
+               &ListSize,\r
+               sizeof (UINT32),\r
+               VarAttr\r
+               );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
   //\r
   // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.\r
   //\r
@@ -330,7 +421,7 @@ AuthVariableLibInitialize (
   AuthVarLibContextOut->StructVersion = AUTH_VAR_LIB_CONTEXT_OUT_STRUCT_VERSION;\r
   AuthVarLibContextOut->StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_OUT);\r
   AuthVarLibContextOut->AuthVarEntry = mAuthVarEntry;\r
-  AuthVarLibContextOut->AuthVarEntryCount = sizeof (mAuthVarEntry) / sizeof (mAuthVarEntry[0]);\r
+  AuthVarLibContextOut->AuthVarEntryCount = ARRAY_SIZE (mAuthVarEntry);\r
   mAuthVarAddressPointer[0] = (VOID **) &mPubKeyStore;\r
   mAuthVarAddressPointer[1] = (VOID **) &mCertDbStore;\r
   mAuthVarAddressPointer[2] = (VOID **) &mHashCtx;\r
@@ -342,7 +433,7 @@ AuthVariableLibInitialize (
   mAuthVarAddressPointer[8] = (VOID **) &(mAuthVarLibContextIn->CheckRemainingSpaceForConsistency),\r
   mAuthVarAddressPointer[9] = (VOID **) &(mAuthVarLibContextIn->AtRuntime),\r
   AuthVarLibContextOut->AddressPointer = mAuthVarAddressPointer;\r
-  AuthVarLibContextOut->AddressPointerCount = sizeof (mAuthVarAddressPointer) / sizeof (mAuthVarAddressPointer[0]);\r
+  AuthVarLibContextOut->AddressPointerCount = ARRAY_SIZE (mAuthVarAddressPointer);\r
 \r
   return Status;\r
 }\r
@@ -380,16 +471,10 @@ AuthVariableLibProcessVariable (
 {\r
   EFI_STATUS        Status;\r
 \r
-  //\r
-  // Process PK, KEK, Sigdb, AuditMode, DeployedMode separately.\r
-  //\r
   if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){\r
     Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, TRUE);\r
   } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {\r
     Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, FALSE);\r
-  } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) \r
-          && (StrCmp (VariableName, EFI_AUDIT_MODE_NAME) == 0 || StrCmp (VariableName, EFI_DEPLOYED_MODE_NAME) == 0)) {\r
-    Status = ProcessSecureBootModeVar(VariableName, VendorGuid, Data, DataSize, Attributes);\r
   } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&\r
              ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE)  == 0) ||\r
               (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||\r