From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com [40.92.19.49]) by mx.groups.io with SMTP id smtpd.web11.3101.1586543923240798538 for ; Fri, 10 Apr 2020 11:38:43 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@outlook.com header.s=selector1 header.b=bxE2FCi+; spf=pass (domain: outlook.com, ip: 40.92.19.49, mailfrom: michael.kubacki@outlook.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QUCCZ9860y6WqWJRfhUUs1TpbLbc4aM8GYFO22fha79zPfzxC2BbWXgkignuOWYNWt2QoGAs2M9Xloyskuj+NJqk2x62yKKcbbe6yzdMFNkqbIQA/LzVbnu+F8SHlMTXZAfs+mrO1VDg1m1+P2jHCE0UvGSVyRVwhK6M9bBkvHexQP8rFyYl+KWixorFS+/TvxjL0ioqB4gxsbkp4Dd0nWCYmQqa2irej2TZO9/8tsNfDOZANq0Kd4yee9dmwkSX0G0mdv/WrdRzogCJ8WNmVJ5WD9dSTfzifk1EdjEvySQVcGKvA9tVAkaaaRfkt9wnUc1T5wJh+2Kp1yeqk3AENg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=RMZoZMY9LZvoJkb23U7+Ov0GYCWgmW23bXcnCuRPySA=; b=WkgHUluIF4YUsrUXEqMbSe7ipO+5IQWWeGF47KLYptLwfdrwkZM7ENrxfoFw1V824i4LouIbm2QAkGcW06x776l7ERP7GphZSBTLFR1rrJhy3L1NSqvui0NmrQCOVMeTipLje2xTFcH2pviEHVzzyg5Yu//8QMRAhG5cj2lXjerUdrb0LoUpv6WAjy0Im9PginZNF9T8ZuktiCqfb8eLXG041NI9aROpfbOVMkkz1R/y5OJLFB+CzCUoKjXvJQ5EIm9SswhPaLfSVNmbGXQlobFa3M1U8JeL0PYj26AEdQx2D67jPyylVsafV3jJtTIORGwfy7p26W41kC5+yZnDgg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=outlook.com; dmarc=pass action=none header.from=outlook.com; dkim=pass header.d=outlook.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=RMZoZMY9LZvoJkb23U7+Ov0GYCWgmW23bXcnCuRPySA=; b=bxE2FCi+dKUnOJzAtB6uksC8jmhbYgsNXOqc8lBgb2jISwPCKV5d759c1n+KD7QQHKOPjwO+nUDqsej0hfG+fFWcTF1oYWTGbTFuADd+xUOO4DFoSDVQaL05odUDp/QpOtKwBr/sk3grZ97TlBxf6wnSwhxkGwqhpDidcHMjFdU3h8l5sFyc4MBwprUfFR0I5IKkNwhxHLqFQFDkqRCLTOtucM3cAsXeAl4HNbdtycA5PJAx3Hog1b6KMUfOhGM/wgzA2mW/pGWdhyA2TeOixhL14/fWNy9pdbKh/oCkwrWSvM/AUJUyb4lDvD+N8aKixGIsxw53I0YPoz4q/3Gb8Q== Received: from BN8NAM11FT063.eop-nam11.prod.protection.outlook.com (2a01:111:e400:fc4b::47) by BN8NAM11HT106.eop-nam11.prod.protection.outlook.com (2a01:111:e400:fc4b::129) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.15; Fri, 10 Apr 2020 18:38:40 +0000 Received: from MWHPR07MB3440.namprd07.prod.outlook.com (2a01:111:e400:fc4b::44) by BN8NAM11FT063.mail.protection.outlook.com (2a01:111:e400:fc4b::366) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.15 via Frontend Transport; Fri, 10 Apr 2020 18:38:40 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:CFD07FF674DE67ACF36D550A538EA584E4B024B38A6DCBA51EAC9535A59271E2;UpperCasedChecksum:7F33C9F4B61B0D7343AB82DC564E81AB9F5C7CCF60092308E90E454D3944C460;SizeAsReceived:7788;Count:49 Received: from MWHPR07MB3440.namprd07.prod.outlook.com ([fe80::bcc9:271b:20db:52e3]) by MWHPR07MB3440.namprd07.prod.outlook.com ([fe80::bcc9:271b:20db:52e3%6]) with mapi id 15.20.2900.015; Fri, 10 Apr 2020 18:38:40 +0000 From: "Michael Kubacki" To: devel@edk2.groups.io Cc: Jian J Wang , Hao A Wu , Liming Gao Subject: [PATCH v1 5/9] MdeModulePkg: Connect VariablePolicy business logic to VariableServices Date: Fri, 10 Apr 2020 11:37:58 -0700 Message-ID: X-Mailer: git-send-email 2.16.3.windows.1 In-Reply-To: <20200410183802.21192-1-michael.kubacki@outlook.com> References: <20200410183802.21192-1-michael.kubacki@outlook.com> X-ClientProxiedBy: MWHPR20CA0007.namprd20.prod.outlook.com (2603:10b6:300:13d::17) To MWHPR07MB3440.namprd07.prod.outlook.com (2603:10b6:301:69::28) Return-Path: michael.kubacki@outlook.com X-Microsoft-Original-Message-ID: <20200410183802.21192-5-michael.kubacki@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (2001:4898:80e8:9:2d7c:9ade:505:3bf5) by MWHPR20CA0007.namprd20.prod.outlook.com (2603:10b6:300:13d::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.17 via Frontend Transport; Fri, 10 Apr 2020 18:38:38 +0000 X-Mailer: git-send-email 2.16.3.windows.1 X-Microsoft-Original-Message-ID: <20200410183802.21192-5-michael.kubacki@outlook.com> X-TMN: [oSy/hXNtkPEwfht/spI9kPmxE37gX1OcBBmEHYubUtzdYrNT78gIpIsQO5428y2A] X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 49 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 2a5a167b-c08d-4c00-7405-08d7dd7e627a X-MS-TrafficTypeDiagnostic: BN8NAM11HT106: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /LPeVm5/whwPE8DUGNb+fuR9BApgu3qECWy0ydntcQe/++j4saJFReVwCNzO9cH6MQF8yRIDGLi/oxtQ/A85+M6emjnqby6NENVmUuyT/mZtgmDQbRiBPWNaCbHluYJ8ikLcJ42dqPXsNH2EDcKMk0GPR7+/4sJ3R2zZlV42GLmlJJURZYh1Dr8Zh+RzHl6tpFpVUwGsE9jKGiE+oGaCnRs2zAV/A8Rw2oW5ipurTAw= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:0;SRV:;IPV:NLI;SFV:NSPM;H:MWHPR07MB3440.namprd07.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:;DIR:OUT;SFP:1901; X-MS-Exchange-AntiSpam-MessageData: 05uz6WhPVaeahCklZZqZkSY0n+afg60fVp/eCNBizCkjpbv8oYclKmgCozWe+I8QZamHe1Hj9Q6XFxQZB44husWeGmTYphUTYppvg/57DlB34f3SaUAen3p/EW/RPT4bhe6JCfJL7Q2VWvSpghN76fuhTAXhwSzhO3xbT+Hg0yMdFP2Jr30sNvh0iDvNDBe5nTB7tkoiZHuvrTrhw2SJqQ== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2a5a167b-c08d-4c00-7405-08d7dd7e627a X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Apr 2020 18:38:38.6213 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8NAM11HT106 Content-Type: text/plain From: Bret Barkelew https://bugzilla.tianocore.org/show_bug.cgi?id=2522 VariablePolicy is an updated interface to replace VarLock and VarCheckProtocol. Add connective code to publish the VariablePolicy protocol and wire it to either the SMM communication interface or directly into the VariablePolicyLib business logic. Cc: Jian J Wang Cc: Hao A Wu Cc: Liming Gao Signed-off-by: Bret Barkelew Signed-off-by: Michael Kubacki --- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c | 51 +++ MdeModulePkg/Universal/Variable/RuntimeDxe/VariablePolicySmmDxe.c | 445 ++++++++++++++++++++ MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 15 + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf | 3 + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf | 8 + 5 files changed, 522 insertions(+) diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c index 7d2b6c8e1fad..659382f9c88c 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableDxe.c @@ -5,18 +5,34 @@ Copyright (C) 2013, Red Hat, Inc. Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
(C) Copyright 2015 Hewlett Packard Enterprise Development LP
+Copyright (c) Microsoft Corporation. SPDX-License-Identifier: BSD-2-Clause-Patent **/ #include "Variable.h" +#include +#include + +EFI_STATUS +EFIAPI +ProtocolIsVariablePolicyEnabled ( + OUT BOOLEAN *State + ); + EFI_HANDLE mHandle = NULL; EFI_EVENT mVirtualAddressChangeEvent = NULL; VOID *mFtwRegistration = NULL; VOID ***mVarCheckAddressPointer = NULL; UINTN mVarCheckAddressPointerCount = 0; EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock = { VariableLockRequestToLock }; +VARIABLE_POLICY_PROTOCOL mVariablePolicyProtocol = { VARIABLE_POLICY_PROTOCOL_REVISION, + DisableVariablePolicy, + ProtocolIsVariablePolicyEnabled, + RegisterVariablePolicy, + DumpVariablePolicy, + LockVariablePolicy }; EDKII_VAR_CHECK_PROTOCOL mVarCheck = { VarCheckRegisterSetVariableCheckHandler, VarCheckVariablePropertySet, VarCheckVariablePropertyGet }; @@ -303,6 +319,8 @@ OnReadyToBoot ( } } + ASSERT_EFI_ERROR (LockVariablePolicy ()); + gBS->CloseEvent (Event); } @@ -466,6 +484,28 @@ FtwNotificationEvent ( } +/** + This API function returns whether or not the policy engine is + currently being enforced. + + @param[out] State Pointer to a return value for whether the policy enforcement + is currently enabled. + + @retval EFI_SUCCESS + @retval Others An error has prevented this command from completing. + +**/ +EFI_STATUS +EFIAPI +ProtocolIsVariablePolicyEnabled ( + OUT BOOLEAN *State + ) +{ + *State = IsVariablePolicyEnabled (); + return EFI_SUCCESS; +} + + /** Variable Driver main entry point. The Variable driver places the 4 EFI runtime services in the EFI System Table and installs arch protocols @@ -576,6 +616,17 @@ VariableServiceInitialize ( ); ASSERT_EFI_ERROR (Status); + // Register and initialize the VariablePolicy engine. + Status = gBS->InstallMultipleProtocolInterfaces ( + &mHandle, + &gVariablePolicyProtocolGuid, + &mVariablePolicyProtocol, + NULL + ); + ASSERT_EFI_ERROR (Status); + Status = InitVariablePolicyLib (VariableServiceGetVariable); + ASSERT_EFI_ERROR (Status); + return EFI_SUCCESS; } diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariablePolicySmmDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariablePolicySmmDxe.c new file mode 100644 index 000000000000..eecfc549c86f --- /dev/null +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariablePolicySmmDxe.c @@ -0,0 +1,445 @@ +/** @file -- VariablePolicySmmDxe.c +This protocol allows communication with Variable Policy Engine. + +Copyright (c) Microsoft Corporation. +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +VARIABLE_POLICY_PROTOCOL mVariablePolicyProtocol; +EFI_MM_COMMUNICATION_PROTOCOL *mMmCommunication; + +VOID *mMmCommunicationBuffer; +UINTN mMmCommunicationBufferSize; + + +/** + This API function disables the variable policy enforcement. If it's + already been called once, will return EFI_ALREADY_STARTED. + + @retval EFI_SUCCESS + @retval EFI_ALREADY_STARTED Has already been called once this boot. + @retval EFI_WRITE_PROTECTED Interface has been locked until reboot. + +**/ +STATIC +EFI_STATUS +EFIAPI +ProtocolDisableVariablePolicy ( + VOID + ) +{ + EFI_STATUS Status; + EFI_MM_COMMUNICATE_HEADER *CommHeader; + VAR_CHECK_POLICY_COMM_HEADER *PolicyHeader; + UINTN BufferSize; + + // Set up the MM communication. + BufferSize = mMmCommunicationBufferSize; + CommHeader = mMmCommunicationBuffer; + PolicyHeader = (VAR_CHECK_POLICY_COMM_HEADER*)&CommHeader->Data; + CopyGuid( &CommHeader->HeaderGuid, &gVarCheckPolicyLibMmiHandlerGuid ); + CommHeader->MessageLength = BufferSize; + PolicyHeader->Signature = VAR_CHECK_POLICY_COMM_SIG; + PolicyHeader->Revision = VAR_CHECK_POLICY_COMM_REVISION; + PolicyHeader->Command = VAR_CHECK_POLICY_COMMAND_DISABLE; + + Status = mMmCommunication->Communicate( mMmCommunication, CommHeader, &BufferSize ); + DEBUG(( DEBUG_VERBOSE, "%a - MmCommunication returned %r.\n", __FUNCTION__, Status )); + + return (EFI_ERROR( Status )) ? Status : PolicyHeader->Result; +} + + +/** + This API function returns whether or not the policy engine is + currently being enforced. + + @param[out] State Pointer to a return value for whether the policy enforcement + is currently enabled. + + @retval EFI_SUCCESS + @retval Others An error has prevented this command from completing. + +**/ +STATIC +EFI_STATUS +EFIAPI +ProtocolIsVariablePolicyEnabled ( + OUT BOOLEAN *State + ) +{ + EFI_STATUS Status; + EFI_MM_COMMUNICATE_HEADER *CommHeader; + VAR_CHECK_POLICY_COMM_HEADER *PolicyHeader; + VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS *CommandParams; + UINTN BufferSize; + + // Set up the MM communication. + BufferSize = mMmCommunicationBufferSize; + CommHeader = mMmCommunicationBuffer; + PolicyHeader = (VAR_CHECK_POLICY_COMM_HEADER*)&CommHeader->Data; + CommandParams = (VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS*)(PolicyHeader + 1); + CopyGuid( &CommHeader->HeaderGuid, &gVarCheckPolicyLibMmiHandlerGuid ); + CommHeader->MessageLength = BufferSize; + PolicyHeader->Signature = VAR_CHECK_POLICY_COMM_SIG; + PolicyHeader->Revision = VAR_CHECK_POLICY_COMM_REVISION; + PolicyHeader->Command = VAR_CHECK_POLICY_COMMAND_IS_ENABLED; + + Status = mMmCommunication->Communicate( mMmCommunication, CommHeader, &BufferSize ); + DEBUG(( DEBUG_VERBOSE, "%a - MmCommunication returned %r.\n", __FUNCTION__, Status )); + + if (!EFI_ERROR( Status )) { + Status = PolicyHeader->Result; + *State = CommandParams->State; + } + + return Status; +} + + +/** + This API function validates and registers a new policy with + the policy enforcement engine. + + @param[in] NewPolicy Pointer to the incoming policy structure. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER NewPolicy is NULL or is internally inconsistent. + @retval EFI_ALREADY_STARTED An identical matching policy already exists. + @retval EFI_WRITE_PROTECTED The interface has been locked until the next reboot. + @retval EFI_UNSUPPORTED Policy enforcement has been disabled. No reason to add more policies. + @retval EFI_ABORTED A calculation error has prevented this function from completing. + @retval EFI_OUT_OF_RESOURCES Cannot grow the table to hold any more policies. + +**/ +STATIC +EFI_STATUS +EFIAPI +ProtocolRegisterVariablePolicy ( + IN VARIABLE_POLICY_ENTRY *NewPolicy + ) +{ + EFI_STATUS Status; + EFI_MM_COMMUNICATE_HEADER *CommHeader; + VAR_CHECK_POLICY_COMM_HEADER *PolicyHeader; + VOID *PolicyBuffer; + UINTN BufferSize; + UINTN RequiredSize; + + // First, make sure that the required size does not exceed the capabilities + // of the MmCommunication buffer. + RequiredSize = OFFSET_OF(EFI_MM_COMMUNICATE_HEADER, Data) + sizeof(VAR_CHECK_POLICY_COMM_HEADER); + Status = SafeUintnAdd( RequiredSize, NewPolicy->Size, &RequiredSize ); + if (EFI_ERROR( Status ) || RequiredSize > mMmCommunicationBufferSize) { + DEBUG(( DEBUG_ERROR, "%a - Policy too large for buffer! %r, %d > %d \n", __FUNCTION__, + Status, RequiredSize, mMmCommunicationBufferSize )); + return EFI_OUT_OF_RESOURCES; + } + + // Set up the MM communication. + BufferSize = mMmCommunicationBufferSize; + CommHeader = mMmCommunicationBuffer; + PolicyHeader = (VAR_CHECK_POLICY_COMM_HEADER*)&CommHeader->Data; + PolicyBuffer = (VOID*)(PolicyHeader + 1); + CopyGuid( &CommHeader->HeaderGuid, &gVarCheckPolicyLibMmiHandlerGuid ); + CommHeader->MessageLength = BufferSize; + PolicyHeader->Signature = VAR_CHECK_POLICY_COMM_SIG; + PolicyHeader->Revision = VAR_CHECK_POLICY_COMM_REVISION; + PolicyHeader->Command = VAR_CHECK_POLICY_COMMAND_REGISTER; + + // Copy the policy into place. This copy is safe because we've already tested above. + CopyMem( PolicyBuffer, NewPolicy, NewPolicy->Size ); + + Status = mMmCommunication->Communicate( mMmCommunication, CommHeader, &BufferSize ); + DEBUG(( DEBUG_VERBOSE, "%a - MmCommunication returned %r.\n", __FUNCTION__, Status )); + + return (EFI_ERROR( Status )) ? Status : PolicyHeader->Result; +} + + +/** + This API function will dump the entire contents of the variable policy table. + + Similar to GetVariable, the first call can be made with a 0 size and it will return + the size of the buffer required to hold the entire table. + + @param[out] Policy Pointer to the policy buffer. Can be NULL if Size is 0. + @param[in,out] Size On input, the size of the output buffer. On output, the size + of the data returned. + + @retval EFI_SUCCESS Policy data is in the output buffer and Size has been updated. + @retval EFI_INVALID_PARAMETER Size is NULL, or Size is non-zero and Policy is NULL. + @retval EFI_BUFFER_TOO_SMALL Size is insufficient to hold policy. Size updated with required size. + +**/ +STATIC +EFI_STATUS +EFIAPI +ProtocolDumpVariablePolicy ( + IN OUT UINT8 *Policy, + IN OUT UINT32 *Size + ) +{ + // There's currently no use for this, but it shouldn't be hard to implement. + return EFI_UNSUPPORTED; +} + + +/** + This API function locks the interface so that no more policy updates + can be performed or changes made to the enforcement until the next boot. + + @retval EFI_SUCCESS + @retval Others An error has prevented this command from completing. + +**/ +STATIC +EFI_STATUS +EFIAPI +ProtocolLockVariablePolicy ( + VOID + ) +{ + EFI_STATUS Status; + EFI_MM_COMMUNICATE_HEADER *CommHeader; + VAR_CHECK_POLICY_COMM_HEADER *PolicyHeader; + UINTN BufferSize; + + // Set up the MM communication. + BufferSize = mMmCommunicationBufferSize; + CommHeader = mMmCommunicationBuffer; + PolicyHeader = (VAR_CHECK_POLICY_COMM_HEADER*)&CommHeader->Data; + CopyGuid( &CommHeader->HeaderGuid, &gVarCheckPolicyLibMmiHandlerGuid ); + CommHeader->MessageLength = BufferSize; + PolicyHeader->Signature = VAR_CHECK_POLICY_COMM_SIG; + PolicyHeader->Revision = VAR_CHECK_POLICY_COMM_REVISION; + PolicyHeader->Command = VAR_CHECK_POLICY_COMMAND_LOCK; + + Status = mMmCommunication->Communicate( mMmCommunication, CommHeader, &BufferSize ); + DEBUG(( DEBUG_VERBOSE, "%a - MmCommunication returned %r.\n", __FUNCTION__, Status )); + + return (EFI_ERROR( Status )) ? Status : PolicyHeader->Result; +} + + +/** + This helper function locates the shared comm buffer and assigns it to input pointers. + + @param[in,out] BufferSize On input, the minimum buffer size required INCLUDING the MM communicate header. + On output, the size of the matching buffer found. + @param[out] LocatedBuffer A pointer to the matching buffer. + + @retval EFI_SUCCESS + @retval Others An error has prevented this command from completing. + +**/ +STATIC +EFI_STATUS +LocateMmCommonCommBuffer ( + IN OUT UINTN *BufferSize, + OUT VOID **LocatedBuffer + ) +{ + EFI_STATUS Status = EFI_ABORTED; + UINTN Index; + UINTN CurrentRegionSize; + EFI_MEMORY_DESCRIPTOR *SmmCommMemRegion; + EDKII_PI_SMM_COMMUNICATION_REGION_TABLE *PiSmmCommunicationRegionTable; + + // Make sure that we're working with good pointers. + if (BufferSize == NULL || LocatedBuffer == NULL) { + return EFI_INVALID_PARAMETER; + } + + Status = EfiGetSystemConfigurationTable( &gEdkiiPiSmmCommunicationRegionTableGuid, (VOID**)&PiSmmCommunicationRegionTable ); + if (EFI_ERROR( Status )) { + DEBUG((DEBUG_ERROR, "%a - Failed to get system configuration table! %r\n", __FUNCTION__, Status)); + return Status; + } + + // Walk through each of the entries trying to find one that will work for the target size. + Status = EFI_OUT_OF_RESOURCES; + CurrentRegionSize = 0; + SmmCommMemRegion = (EFI_MEMORY_DESCRIPTOR*)(PiSmmCommunicationRegionTable + 1); + for (Index = 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries; Index++) { + if (SmmCommMemRegion->Type == EfiConventionalMemory) { + CurrentRegionSize = EFI_PAGES_TO_SIZE((UINTN)SmmCommMemRegion->NumberOfPages); + if (CurrentRegionSize >= *BufferSize) { + Status = EFI_SUCCESS; + break; + } + } + SmmCommMemRegion = (EFI_MEMORY_DESCRIPTOR*)((UINT8*)SmmCommMemRegion + PiSmmCommunicationRegionTable->DescriptorSize); + } + + if (!EFI_ERROR( Status )) { + *LocatedBuffer = (VOID*)(UINTN)SmmCommMemRegion->PhysicalStart; + *BufferSize = CurrentRegionSize; + } + else { + *LocatedBuffer = NULL; + *BufferSize = 0; + } + + return Status; +} + + +/** + This helper is responsible for telemetry and any other actions that + need to be taken if the VariablePolicy fails to lock. + + NOTE: It's possible that parts of this handling will need to become + part of a platform policy. + + @param[in] FailureStatus The failure that was reported by LockVariablePolicy + +**/ +STATIC +VOID +VariablePolicyHandleFailureToLock ( + IN EFI_STATUS FailureStatus + ) +{ + // For now, there's no agreed-upon policy for this. + return; +} + + +/** + ReadyToBoot Callback + Lock the VariablePolicy interface if it hasn't already been locked. + + @param[in] Event Event whose notification function is being invoked + @param[in] Context Pointer to the notification function's context + +**/ +STATIC +VOID +EFIAPI +LockPolicyInterfaceAtReadyToBoot ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + + Status = ProtocolLockVariablePolicy(); + + if (EFI_ERROR( Status )) { + VariablePolicyHandleFailureToLock( Status ); + } + else { + gBS->CloseEvent( Event ); + } + +} + + +/** + The driver's entry point. + + @param[in] ImageHandle The firmware allocated handle for the EFI image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point executed successfully. + @retval other Some error occured when executing this entry point. + +**/ +EFI_STATUS +EFIAPI +VariablePolicySmmDxeMain ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status = EFI_SUCCESS; + BOOLEAN ProtocolInstalled = FALSE; + BOOLEAN CallbackRegistered = FALSE; + EFI_EVENT ReadyToBootEvent; + + // Update the minimum buffer size. + mMmCommunicationBufferSize = VAR_CHECK_POLICY_MIN_MM_BUFFER_SIZE; + // Locate the shared comm buffer to use for sending MM commands. + Status = LocateMmCommonCommBuffer( &mMmCommunicationBufferSize, &mMmCommunicationBuffer ); + if (EFI_ERROR( Status )) { + DEBUG((DEBUG_ERROR, "%a - Failed to locate a viable MM comm buffer! %r\n", __FUNCTION__, Status)); + ASSERT_EFI_ERROR( Status ); + return Status; + } + + // Locate the MmCommunication protocol. + Status = gBS->LocateProtocol( &gEfiMmCommunicationProtocolGuid, NULL, (VOID**)&mMmCommunication ); + if (EFI_ERROR( Status )) { + DEBUG((DEBUG_ERROR, "%a - Failed to locate MmCommunication protocol! %r\n", __FUNCTION__, Status)); + ASSERT_EFI_ERROR( Status ); + return Status; + } + + // Configure the VariablePolicy protocol structure. + mVariablePolicyProtocol.Revision = VARIABLE_POLICY_PROTOCOL_REVISION; + mVariablePolicyProtocol.DisableVariablePolicy = ProtocolDisableVariablePolicy; + mVariablePolicyProtocol.IsVariablePolicyEnabled = ProtocolIsVariablePolicyEnabled; + mVariablePolicyProtocol.RegisterVariablePolicy = ProtocolRegisterVariablePolicy; + mVariablePolicyProtocol.DumpVariablePolicy = ProtocolDumpVariablePolicy; + mVariablePolicyProtocol.LockVariablePolicy = ProtocolLockVariablePolicy; + + // Register all the protocols and return the status. + Status = gBS->InstallMultipleProtocolInterfaces( &ImageHandle, + &gVariablePolicyProtocolGuid, &mVariablePolicyProtocol, + NULL ); + if (EFI_ERROR( Status )) { + DEBUG(( DEBUG_ERROR, "%a - Failed to install protocol! %r\n", __FUNCTION__, Status )); + goto Exit; + } + else { + ProtocolInstalled = TRUE; + } + + // + // Register a callback for ReadyToBoot so that the interface is at least locked before + // dispatching any bootloaders or UEFI apps. + Status = gBS->CreateEventEx( EVT_NOTIFY_SIGNAL, + TPL_CALLBACK, + LockPolicyInterfaceAtReadyToBoot, + NULL, + &gEfiEventReadyToBootGuid, + &ReadyToBootEvent ); + if (EFI_ERROR( Status )) { + DEBUG(( DEBUG_ERROR, "%a - Failed to create ReadyToBoot event! %r\n", __FUNCTION__, Status )); + goto Exit; + } + else { + CallbackRegistered = TRUE; + } + +Exit: + // + // If we're about to return a failed status (and unload this driver), we must first undo anything that + // has been successfully done. + if (EFI_ERROR( Status )) { + if (ProtocolInstalled) { + gBS->UninstallProtocolInterface( &ImageHandle, &gVariablePolicyProtocolGuid, &mVariablePolicyProtocol ); + } + if (CallbackRegistered) { + gBS->CloseEvent( ReadyToBootEvent ); + } + } + + return Status; +} diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c index 2cf0ed32ae55..6bc576ecc47e 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c @@ -14,6 +14,7 @@ InitCommunicateBuffer() is really function to check the variable data size. Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) Microsoft Corporation. SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -64,6 +65,17 @@ EFI_LOCK mVariableServicesLock; EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock; EDKII_VAR_CHECK_PROTOCOL mVarCheck; +/** + The logic to initialize the VariablePolicy engine is in its own file. + +**/ +EFI_STATUS +EFIAPI +VariablePolicySmmDxeMain ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + /** Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc). Record their initial State when variable write service is ready. @@ -1791,6 +1803,9 @@ VariableSmmRuntimeInitialize ( &mVirtualAddressChangeEvent ); + // Initialize the VariablePolicy protocol and engine. + VariablePolicySmmDxeMain (ImageHandle, SystemTable); + return EFI_SUCCESS; } diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf index bc3033588d40..bbc8d2080193 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf @@ -19,6 +19,7 @@ # the authentication service provided in this driver will be broken, and the behavior is undefined. # # Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
+# Copyright (c) Microsoft Corporation. # SPDX-License-Identifier: BSD-2-Clause-Patent # ## @@ -78,6 +79,8 @@ AuthVariableLib VarCheckLib UefiBootServicesTableLib + VariablePolicyLib + VariablePolicyHelperLib [Protocols] gEfiSmmFirmwareVolumeBlockProtocolGuid ## CONSUMES diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf index 592862773390..5d7384ece5b2 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf @@ -14,6 +14,7 @@ # the authentication service provided in this driver will be broken, and the behavior is undefined. # # Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
+# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: BSD-2-Clause-Patent # ## @@ -42,6 +43,7 @@ VariableParsing.c VariableParsing.h Variable.h + VariablePolicySmmDxe.c [Packages] MdePkg/MdePkg.dec @@ -56,17 +58,20 @@ DxeServicesTableLib UefiDriverEntryPoint TpmMeasurementLib + SafeIntLib [Protocols] gEfiVariableWriteArchProtocolGuid ## PRODUCES gEfiVariableArchProtocolGuid ## PRODUCES gEfiSmmCommunicationProtocolGuid ## CONSUMES + gEfiMmCommunicationProtocolGuid ## CONSUMES ## CONSUMES ## NOTIFY ## UNDEFINED # Used to do smm communication gEfiSmmVariableProtocolGuid gEdkiiVariableLockProtocolGuid ## PRODUCES gEdkiiVarCheckProtocolGuid ## PRODUCES + gVariablePolicyProtocolGuid ## PRODUCES [FeaturePcd] gEfiMdeModulePkgTokenSpaceGuid.PcdEnableVariableRuntimeCache ## CONSUMES @@ -99,6 +104,9 @@ ## SOMETIMES_CONSUMES ## Variable:L"dbt" gEfiImageSecurityDatabaseGuid + gVarCheckPolicyLibMmiHandlerGuid + gEdkiiPiSmmCommunicationRegionTableGuid + [Depex] gEfiSmmCommunicationProtocolGuid -- 2.16.3.windows.1