From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (NAM12-BN8-obe.outbound.protection.outlook.com [40.92.21.75]) by mx.groups.io with SMTP id smtpd.web12.3042.1590101063941529045 for ; Thu, 21 May 2020 15:44:24 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@outlook.com header.s=selector1 header.b=Iv3g7WES; spf=pass (domain: outlook.com, ip: 40.92.21.75, mailfrom: michael.kubacki@outlook.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=j01MKBtAuLlhMAesubnkBbOkPROGjJSW3rBBv/1aEt46SP/idWL7Ov9VHauXGjUFx1X3qrBSYfNSinJuakBBdaoo+eB6qQt3n3hAfCuuUeryAtszjL9iSSG7/IAcN1WCILfFCTunenmt7nCrMsZ5z35Nozgmid6GPPbN4eo5JwUmJ48k/22niz7WZr8U6IPhvKt71m3yfdaXeByV3bEYuYTxpbpLHi5Jvt1gyS5JqcNFWYYhv9xWHY0Tsa4lLg4m1G+EnK5pvdIfb9G1CM5OerY/CAAobQMdJdmheKC7lcJlmmQoDmxJk1M942uaNxX32hqRSLDcFOCuDHyTjyvppg== 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=Xfc1AZjE0kmLnsAKaYFqJ5/CPmz3TCsBV0G4q1qJFfM=; b=QQ3UAAFgIDh6+6HVz/q3VJb1E4/+PJ6kaU/Y6ApJ/MvOVb60mAGpNdyswcUVdTsYjRb3MVpsO5dSBMX7OdoRkKaS+kM8dOApDwrIdJzqPfnBiyr4BK6chz7CgGf7P3qemPcRmH6JEfy+fyyEn6ki7MU9SQC9lsk/ARtQv3X6x1XM3fLHLINHEca3U2r+y1u343J2ay6sveNSqXfzeea7xOWGg0dhVNC9pyjmd4iF6Dr7itrJiIV+MTrKZpDXOokySs5etlt9XiCA4qbmrF/6WQjP1y/aD94jqs6Sph/n/KLC+UYgfiFOqkjTPPpVLjRZIpXXbOF1y4/Kd93l5eP3IA== 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=Xfc1AZjE0kmLnsAKaYFqJ5/CPmz3TCsBV0G4q1qJFfM=; b=Iv3g7WESHTsSfP6BisuF12uypJmaZg9ONTrY47+rLKaPIVukt9I3SHCYL9jLea067mJBFwIaNnmk9yDT6Xk9cglciW83JMiT/F1M2TvQcrXYJl6bFr/4cMKOYJik/RlQ78N9aALIa9xrIya1OJ6odPeQtirX3GwdNZ8Wi1Gw13ccyG7SVtkzle24pnk/fFOQj/25n6AxDehZZIxL4hJ6ZAlLZbdU4lN1zArpZX3bEr5bdrEhXdfFoT/SobDVE0AmErkGe2IUpflj2luW0sUUTX7/v9Iflq75LQQE0TJCityuzNDitJPwGlT6KD4LHo/gtBoP0dXlbhI3DXyhoCWsWg== Received: from MW2NAM12FT062.eop-nam12.prod.protection.outlook.com (2a01:111:e400:fc65::4c) by MW2NAM12HT042.eop-nam12.prod.protection.outlook.com (2a01:111:e400:fc65::120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3021.11; Thu, 21 May 2020 22:44:22 +0000 Received: from MWHPR07MB3440.namprd07.prod.outlook.com (2a01:111:e400:fc65::50) by MW2NAM12FT062.mail.protection.outlook.com (2a01:111:e400:fc65::370) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3021.11 via Frontend Transport; Thu, 21 May 2020 22:44:22 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:3C70C5044CFBB76D49C51149DF5BDAAF0160A9312AC4E945B45BFA5C40DDCFB8;UpperCasedChecksum:816E3C828F20E5348ECDB9B4B3CEACE1930F113F416B7AB593901758862AF36D;SizeAsReceived:7876;Count:50 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.3021.020; Thu, 21 May 2020 22:44:22 +0000 From: "Michael Kubacki" To: devel@edk2.groups.io CC: Jian J Wang , Hao A Wu , Liming Gao , Bret Barkelew Subject: [PATCH v3 04/14] MdeModulePkg: Define the VarCheckPolicyLib and SMM interface Date: Thu, 21 May 2020 15:43:21 -0700 Message-ID: X-Mailer: git-send-email 2.16.3.windows.1 In-Reply-To: <20200521224331.15616-1-michael.kubacki@outlook.com> References: <20200521224331.15616-1-michael.kubacki@outlook.com> X-ClientProxiedBy: MWHPR12CA0071.namprd12.prod.outlook.com (2603:10b6:300:103::33) To MWHPR07MB3440.namprd07.prod.outlook.com (2603:10b6:301:69::28) Return-Path: michael.kubacki@outlook.com X-Microsoft-Original-Message-ID: <20200521224331.15616-5-michael.kubacki@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (2001:4898:80e8:1:2c94:8481:fffa:8ac5) by MWHPR12CA0071.namprd12.prod.outlook.com (2603:10b6:300:103::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3021.23 via Frontend Transport; Thu, 21 May 2020 22:44:21 +0000 X-Mailer: git-send-email 2.16.3.windows.1 X-Microsoft-Original-Message-ID: <20200521224331.15616-5-michael.kubacki@outlook.com> X-TMN: [WesBG7ZznLl0F3rbaBi2y2zDLjJmlT67cUNf5d8hW14LHmPts84v9NxJp9h9Fo/W] X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 50 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 274acd5d-630b-4adc-ed0c-08d7fdd8811f X-MS-TrafficTypeDiagnostic: MW2NAM12HT042: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +bn7dRZKvUP0VYBfS+6mxdXuxGRfsY3uqLp9tNHygWLF/1GgJL2Nu9Jbfu3TH4d7SB6oOnJuCzdgeElw4d6NqDKm+kcHL4eA5bbPWL+ZVPPT0LtJZUWBKIyDNN46T36d8y6HUKbqDCcY75qmTguC2abmolL8hGEkWDkG3y1aZQUUTDaBtiuJ9a6f2hMUwKfGVanvv+I3TSGLPCWmVYoMn7kyeVgGg/3aNGoEuH574mpldd+65HMSakr0bdRlnOZQ 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: G6Gu40aQyC+1FstQBf7DG0N5/yfh6ylHPWpo/kp0TvLTFwr7+c5FwqH0Lq/KuBBbpkr/d25eLNFiQ4FIgZvZbQl5Mp8jizJXnvoDG/TtzLndMiynKitJT8fp479KrqLdAfm/pCkCy8sVEYQAZ2KM3/gYod8ZFR2M86z2fBjZDj51B4omr1acXc0zCuTZwYwneou+Wiap7DJ+T36jPGiIhA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 274acd5d-630b-4adc-ed0c-08d7fdd8811f X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 May 2020 22:44:22.4007 (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: MW2NAM12HT042 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain From: Bret Barkelew https://bugzilla.tianocore.org/show_bug.cgi?id=3D2522 VariablePolicy is an updated interface to replace VarLock and VarCheckProtocol. This is an instance of a VarCheckLib that is backed by the VariablePolicyLib business logic. It also publishes the SMM calling interface for messages from the DXE protocol. Cc: Jian J Wang Cc: Hao A Wu Cc: Liming Gao Cc: Bret Barkelew Signed-off-by: Michael Kubacki --- MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.c | 324 +++++++= +++++++++++++ MdeModulePkg/Include/Guid/VarCheckPolicyMmi.h | 54 ++++ MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.inf | 42 +++ MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.uni | 12 + MdeModulePkg/MdeModulePkg.dec | 4 + MdeModulePkg/MdeModulePkg.dsc | 2 + 6 files changed, 438 insertions(+) diff --git a/MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.c b/M= deModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.c new file mode 100644 index 000000000000..bfe9bb5f22e6 --- /dev/null +++ b/MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.c @@ -0,0 +1,324 @@ +/** @file -- VarCheckPolicyLib.c +This is an instance of a VarCheck lib that leverages the business logic be= hind +the VariablePolicy code to make its decisions. + +Copyright (c) Microsoft Corporation. +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include + +//=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +// As a VarCheck library, we're linked into the VariableServices +// and may not be able to call them indirectly. To get around this, +// use the internal GetVariable function to query the variable store. +//=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +EFI_STATUS +EFIAPI +VariableServiceGetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT UINT32 *Attributes OPTIONAL, + IN OUT UINTN *DataSize, + OUT VOID *Data + ); + + +/** + MM Communication Handler to recieve commands from the DXE protocol for + Variable Policies. This communication channel is used to register new po= licies + and poll and toggle the enforcement of variable policies. + + @param[in] DispatchHandle All parameters standard to MM commun= ications convention. + @param[in] RegisterContext All parameters standard to MM commun= ications convention. + @param[in,out] CommBuffer All parameters standard to MM commun= ications convention. + @param[in,out] CommBufferSize All parameters standard to MM commun= ications convention. + + @retval EFI_SUCCESS + @retval EFI_INVALID_PARAMETER CommBuffer or CommBufferSize is null= pointer. + @retval EFI_INVALID_PARAMETER CommBuffer size is wrong. + @retval EFI_INVALID_PARAMETER Revision or signature don't match. + +**/ +STATIC +EFI_STATUS +EFIAPI +VarCheckPolicyLibMmiHandler ( + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *RegisterContext, + IN OUT VOID *CommBuffer, + IN OUT UINTN *CommBufferSize + ) +{ + EFI_STATUS Status; + EFI_STATUS SubCommandStatus; + VAR_CHECK_POLICY_COMM_HEADER *PolicyCommmHeader; + VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS *IsEnabledParams; + VAR_CHECK_POLICY_COMM_DUMP_PARAMS *DumpParams; + UINT8 *DumpInputBuffer; + UINT8 *DumpOutputBuffer; + UINTN DumpTotalPages; + VARIABLE_POLICY_ENTRY *PolicyEntry; + UINTN ExpectedSize; + // Pagination Cache Variables + static UINT8 *PaginationCache =3D NULL; + static UINTN PaginationCacheSize =3D 0; + static UINT32 CurrentPaginationCommand =3D 0= ; + + Status =3D EFI_SUCCESS; + + // + // Validate some input parameters. + // + // If either of the pointers are NULL, we can't proceed. + if (CommBuffer =3D=3D NULL || CommBufferSize =3D=3D NULL) { + DEBUG ((DEBUG_INFO, "%a - Invalid comm buffer pointers!\n", __FUNCTION= __)); + return EFI_INVALID_PARAMETER; + } + + // If the size does not meet a minimum threshold, we cannot proceed. + ExpectedSize =3D sizeof (VAR_CHECK_POLICY_COMM_HEADER); + if (*CommBufferSize < ExpectedSize) { + DEBUG ((DEBUG_INFO, "%a - Bad comm buffer size! %d < %d\n", __FUNCTION= __, *CommBufferSize, ExpectedSize)); + return EFI_INVALID_PARAMETER; + } + + // Check the revision and the signature of the comm header. + PolicyCommmHeader =3D CommBuffer; + if (PolicyCommmHeader->Signature !=3D VAR_CHECK_POLICY_COMM_SIG || + PolicyCommmHeader->Revision !=3D VAR_CHECK_POLICY_COMM_REVISION) { + DEBUG ((DEBUG_INFO, "%a - Signature or revision are incorrect!\n", __F= UNCTION__)); + // We have verified the buffer is not null and have enough size to hol= d Result field. + PolicyCommmHeader->Result =3D EFI_INVALID_PARAMETER; + return EFI_SUCCESS; + } + + // If we're in the middle of a paginated dump and any other command is s= ent, + // pagination cache must be cleared. + if (PaginationCache !=3D NULL && PolicyCommmHeader->Command !=3D Current= PaginationCommand) { + FreePool (PaginationCache); + PaginationCache =3D NULL; + PaginationCacheSize =3D 0; + CurrentPaginationCommand =3D 0; + } + + // + // Now we can process the command as it was sent. + // + PolicyCommmHeader->Result =3D EFI_ABORTED; // Set a default return fo= r incomplete commands. + switch (PolicyCommmHeader->Command) { + case VAR_CHECK_POLICY_COMMAND_DISABLE: + PolicyCommmHeader->Result =3D DisableVariablePolicy (); + break; + + case VAR_CHECK_POLICY_COMMAND_IS_ENABLED: + // Make sure that we're dealing with a reasonable size. + // This add should be safe because these are fixed sizes so far. + ExpectedSize +=3D sizeof (VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS); + if (*CommBufferSize < ExpectedSize) { + DEBUG ((DEBUG_INFO, "%a - Bad comm buffer size! %d < %d\n", __FUNC= TION__, *CommBufferSize, ExpectedSize)); + PolicyCommmHeader->Result =3D EFI_INVALID_PARAMETER; + break; + } + + // Now that we know we've got a valid size, we can fill in the rest = of the data. + IsEnabledParams =3D (VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS *) ((UI= NT8 *) CommBuffer + sizeof (VAR_CHECK_POLICY_COMM_HEADER)); + IsEnabledParams->State =3D IsVariablePolicyEnabled (); + PolicyCommmHeader->Result =3D EFI_SUCCESS; + break; + + case VAR_CHECK_POLICY_COMMAND_REGISTER: + // Make sure that we're dealing with a reasonable size. + // This add should be safe because these are fixed sizes so far. + ExpectedSize +=3D sizeof (VARIABLE_POLICY_ENTRY); + if (*CommBufferSize < ExpectedSize) { + DEBUG ((DEBUG_INFO, "%a - Bad comm buffer size! %d < %d\n", __FUNC= TION__, *CommBufferSize, ExpectedSize)); + PolicyCommmHeader->Result =3D EFI_INVALID_PARAMETER; + break; + } + + // At the very least, we can assume that we're working with a valid = policy entry. + // Time to compare its internal size. + PolicyEntry =3D (VARIABLE_POLICY_ENTRY *) ((UINT8 *) CommBuffer + si= zeof (VAR_CHECK_POLICY_COMM_HEADER)); + if (PolicyEntry->Version !=3D VARIABLE_POLICY_ENTRY_REVISION || + PolicyEntry->Size < sizeof (VARIABLE_POLICY_ENTRY) || + EFI_ERROR (SafeUintnAdd (sizeof (VAR_CHECK_POLICY_COMM_HEADER), = PolicyEntry->Size, &ExpectedSize)) || + *CommBufferSize < ExpectedSize) { + DEBUG ((DEBUG_INFO, "%a - Bad policy entry contents!\n", __FUNCTIO= N__)); + PolicyCommmHeader->Result =3D EFI_INVALID_PARAMETER; + break; + } + + PolicyCommmHeader->Result =3D RegisterVariablePolicy (PolicyEntry); + break; + + case VAR_CHECK_POLICY_COMMAND_DUMP: + // Make sure that we're dealing with a reasonable size. + // This add should be safe because these are fixed sizes so far. + ExpectedSize +=3D sizeof (VAR_CHECK_POLICY_COMM_DUMP_PARAMS) + VAR_C= HECK_POLICY_MM_DUMP_BUFFER_SIZE; + if (*CommBufferSize < ExpectedSize) { + DEBUG ((DEBUG_INFO, "%a - Bad comm buffer size! %d < %d\n", __FUNC= TION__, *CommBufferSize, ExpectedSize)); + PolicyCommmHeader->Result =3D EFI_INVALID_PARAMETER; + break; + } + + // Now that we know we've got a valid size, we can fill in the rest = of the data. + DumpParams =3D (VAR_CHECK_POLICY_COMM_DUMP_PARAMS *) (PolicyCommmHea= der + 1); + + // If we're requesting the first page, initialize the cache and get = the sizes. + if (DumpParams->PageRequested =3D=3D 0) { + if (PaginationCache !=3D NULL) { + FreePool (PaginationCache); + PaginationCache =3D NULL; + } + + // Determine what the required size is going to be. + DumpParams->TotalSize =3D 0; + DumpParams->PageSize =3D 0; + DumpParams->HasMore =3D FALSE; + SubCommandStatus =3D DumpVariablePolicy (NULL, &DumpParams->TotalS= ize); + if (SubCommandStatus =3D=3D EFI_BUFFER_TOO_SMALL && DumpParams->To= talSize > 0) { + CurrentPaginationCommand =3D VAR_CHECK_POLICY_COMMAND_DUMP; + PaginationCacheSize =3D DumpParams->TotalSize; + PaginationCache =3D AllocatePool (PaginationCacheSize); + if (PaginationCache =3D=3D NULL) { + SubCommandStatus =3D EFI_OUT_OF_RESOURCES; + } + } + + // If we've allocated our pagination cache, we're good to cache. + if (PaginationCache !=3D NULL) { + SubCommandStatus =3D DumpVariablePolicy (PaginationCache, &DumpP= arams->TotalSize); + } + + // Populate the remaining fields and we can boogie. + if (!EFI_ERROR (SubCommandStatus) && PaginationCache !=3D NULL) { + DumpParams->HasMore =3D TRUE; + } + } else if (PaginationCache !=3D NULL) { + DumpParams->TotalSize =3D (UINT32) PaginationCacheSize; + DumpParams->PageSize =3D VAR_CHECK_POLICY_MM_DUMP_BUFFER_SIZE; + DumpOutputBuffer =3D (UINT8 *) (DumpParams + 1); + + // Make sure that we don't over-index the cache. + DumpTotalPages =3D PaginationCacheSize / DumpParams->PageSize; + if (PaginationCacheSize % DumpParams->PageSize) DumpTotalPages++; + if (DumpParams->PageRequested > DumpTotalPages) { + SubCommandStatus =3D EFI_INVALID_PARAMETER; + } else { + // Figure out how far into the page cache we need to go for our = next page. + // We know the blind subtraction won't be bad because we already= checked for page 0. + DumpInputBuffer =3D &PaginationCache[DumpParams->PageSize * (Dum= pParams->PageRequested - 1)]; + // If we're getting the last page, adjust the PageSize. + if (DumpParams->PageRequested =3D=3D DumpTotalPages) { + DumpParams->PageSize =3D PaginationCacheSize % DumpParams->Pag= eSize; + } + CopyMem (DumpOutputBuffer, DumpInputBuffer, DumpParams->PageSize= ); + // If we just got the last page, settle up the cache. + if (DumpParams->PageRequested =3D=3D DumpTotalPages) { + DumpParams->HasMore =3D FALSE; + FreePool (PaginationCache); + PaginationCache =3D NULL; + PaginationCacheSize =3D 0; + CurrentPaginationCommand =3D 0; + } else { + // Otherwise, we could do more here. + DumpParams->HasMore =3D TRUE; + } + + // If we made it this far, we're basically good. + SubCommandStatus =3D EFI_SUCCESS; + } + } else { + // If we've requested any other page than 0 and the cache is empty= , we must have timed out. + DumpParams->TotalSize =3D 0; + DumpParams->PageSize =3D 0; + DumpParams->HasMore =3D FALSE; + SubCommandStatus =3D EFI_TIMEOUT; + } + + // There's currently no use for this, but it shouldn't be hard to im= plement. + PolicyCommmHeader->Result =3D SubCommandStatus; + break; + + case VAR_CHECK_POLICY_COMMAND_LOCK: + PolicyCommmHeader->Result =3D LockVariablePolicy (); + break; + + default: + // Mark unknown requested command as EFI_UNSUPPORTED. + DEBUG ((DEBUG_INFO, "%a - Invalid command requested! %d\n", __FUNCTI= ON__, PolicyCommmHeader->Command)); + PolicyCommmHeader->Result =3D EFI_UNSUPPORTED; + break; + } + + DEBUG (( + DEBUG_VERBOSE, + "%a - Command %d returning %r.\n", + __FUNCTION__, + PolicyCommmHeader->Command, + PolicyCommmHeader->Result + )); + + return Status; +} + + +/** + Constructor function of VarCheckPolicyLib to register VarCheck handler a= nd + SW MMI handlers. + + @param[in] ImageHandle The firmware allocated handle for the EFI imag= e. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The constructor executed correctly. + +**/ +EFI_STATUS +EFIAPI +VarCheckPolicyLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + EFI_HANDLE DiscardedHandle; + + // Initialize the business logic with the internal GetVariable handler. + Status =3D InitVariablePolicyLib (VariableServiceGetVariable); + + // Only proceed with init if the business logic could be initialized. + if (!EFI_ERROR (Status)) { + // Register the VarCheck handler for SetVariable filtering. + // Forward the check to the business logic of the library. + VarCheckLibRegisterSetVariableCheckHandler (ValidateSetVariable); + + // Register the MMI handlers for receiving policy commands. + DiscardedHandle =3D NULL; + Status =3D gMmst->MmiHandlerRegister ( + VarCheckPolicyLibMmiHandler, + &gVarCheckPolicyLibMmiHandlerGuid, + &DiscardedHandle + ); + } else { + // Otherwise, there's not much we can do. + DEBUG ((DEBUG_ERROR, "%a - Cannot Initialize VariablePolicyLib! %r\n",= __FUNCTION__, Status)); + ASSERT_EFI_ERROR (Status); + } + + return Status; +} diff --git a/MdeModulePkg/Include/Guid/VarCheckPolicyMmi.h b/MdeModulePkg/I= nclude/Guid/VarCheckPolicyMmi.h new file mode 100644 index 000000000000..09aba59ca9f7 --- /dev/null +++ b/MdeModulePkg/Include/Guid/VarCheckPolicyMmi.h @@ -0,0 +1,54 @@ +/** @file -- VarCheckPolicyMmiCommon.h +This header contains communication definitions that are shared between DXE +and the MM component of VarCheckPolicy. + +Copyright (c) Microsoft Corporation. +SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef _VAR_CHECK_POLICY_MMI_COMMON_H_ +#define _VAR_CHECK_POLICY_MMI_COMMON_H_ + +#define VAR_CHECK_POLICY_COMM_SIG SIGNATURE_32('V', 'C', 'P', 'C') +#define VAR_CHECK_POLICY_COMM_REVISION 1 + +#pragma pack(push, 1) + +typedef struct _VAR_CHECK_POLICY_COMM_HEADER { + UINT32 Signature; + UINT32 Revision; + UINT32 Command; + EFI_STATUS Result; +} VAR_CHECK_POLICY_COMM_HEADER; + +typedef struct _VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS { + BOOLEAN State; +} VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS; + +typedef struct _VAR_CHECK_POLICY_COMM_DUMP_PARAMS { + UINT32 PageRequested; + UINT32 TotalSize; + UINT32 PageSize; + BOOLEAN HasMore; +} VAR_CHECK_POLICY_COMM_DUMP_PARAMS; + +#pragma pack(pop) + +// Make sure that we will hold at least the headers. +#define VAR_CHECK_POLICY_MM_COMM_BUFFER_SIZE MAX((OFFSET_OF (EFI_MM_COM= MUNICATE_HEADER, Data) + sizeof (VAR_CHECK_POLICY_COMM_HEADER) + EFI_PAGES_= TO_SIZE (1)), EFI_PAGES_TO_SIZE (4)) +#define VAR_CHECK_POLICY_MM_DUMP_BUFFER_SIZE (VAR_CHECK_POLICY_MM_COMM_= BUFFER_SIZE - \ + (OFFSET_OF (EFI_MM_COM= MUNICATE_HEADER, Data) + \ + sizeof (VAR_CHECK_PO= LICY_COMM_HEADER) + \ + sizeof (VAR_CHECK_PO= LICY_COMM_DUMP_PARAMS))) +STATIC_ASSERT ( + VAR_CHECK_POLICY_MM_DUMP_BUFFER_SIZE < VAR_CHECK_POLICY_MM_COMM_BUFFER_S= IZE, + "an integer underflow may have occurred calculating VAR_CHECK_POLICY_MM_= DUMP_BUFFER_SIZE" + ); + +#define VAR_CHECK_POLICY_COMMAND_DISABLE 0x0001 +#define VAR_CHECK_POLICY_COMMAND_IS_ENABLED 0x0002 +#define VAR_CHECK_POLICY_COMMAND_REGISTER 0x0003 +#define VAR_CHECK_POLICY_COMMAND_DUMP 0x0004 +#define VAR_CHECK_POLICY_COMMAND_LOCK 0x0005 + +#endif // _VAR_CHECK_POLICY_MMI_COMMON_H_ diff --git a/MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.inf b= /MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.inf new file mode 100644 index 000000000000..077bcc8990ca --- /dev/null +++ b/MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.inf @@ -0,0 +1,42 @@ +## @file VarCheckPolicyLib.inf +# This is an instance of a VarCheck lib that leverages the business logic = behind +# the VariablePolicy code to make its decisions. +# +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D VarCheckPolicyLib + FILE_GUID =3D 9C28A48F-C884-4B1F-8B95-DEF125448023 + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D NULL|DXE_RUNTIME_DRIVER DXE_SMM_DRIVE= R + CONSTRUCTOR =3D VarCheckPolicyLibConstructor + + +[Sources] + VarCheckPolicyLib.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + + +[LibraryClasses] + BaseLib + DebugLib + BaseMemoryLib + DxeServicesLib + MemoryAllocationLib + VarCheckLib + VariablePolicyLib + VariablePolicyHelperLib + SafeIntLib + MmServicesTableLib + + +[Guids] + gVarCheckPolicyLibMmiHandlerGuid ## CONSUME ## Used to register f= or MM Communication events. diff --git a/MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.uni b= /MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.uni new file mode 100644 index 000000000000..eedeeed15d31 --- /dev/null +++ b/MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.uni @@ -0,0 +1,12 @@ +// /** @file +// VarCheckPolicyLib.uni +// +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "NULL library impl= ementation that conforms to the VarCheck interface to allow VariablePolicy = engine to enforce policies" + +#string STR_MODULE_DESCRIPTION #language en-US "NULL library impl= ementation that conforms to the VarCheck interface to allow VariablePolicy = engine to enforce policies" diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 2db37bd8ea9e..04abc49311a1 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -385,6 +385,10 @@ [Guids] ## Include/Guid/EndofS3Resume.h gEdkiiEndOfS3ResumeGuid =3D { 0x96f5296d, 0x05f7, 0x4f3c, {0x84, 0x67, 0= xe4, 0x56, 0x89, 0x0e, 0x0c, 0xb5 } } =20 + ## Used (similar to Variable Services) to communicate policies to the en= forcement engine. + # {DA1B0D11-D1A7-46C4-9DC9-F3714875C6EB} + gVarCheckPolicyLibMmiHandlerGuid =3D { 0xda1b0d11, 0xd1a7, 0x46c4, { 0x9= d, 0xc9, 0xf3, 0x71, 0x48, 0x75, 0xc6, 0xeb }} + ## Include/Guid/S3SmmInitDone.h gEdkiiS3SmmInitDoneGuid =3D { 0x8f9d4825, 0x797d, 0x48fc, { 0x84, 0x71, = 0x84, 0x50, 0x25, 0x79, 0x2e, 0xf6 } } =20 diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc index 37795b9e4f58..f0a75a3b337b 100644 --- a/MdeModulePkg/MdeModulePkg.dsc +++ b/MdeModulePkg/MdeModulePkg.dsc @@ -313,6 +313,7 @@ [Components] MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLibNull.inf MdeModulePkg/Library/VariablePolicyLib/VariablePolicyLib.inf MdeModulePkg/Library/VariablePolicyLib/VariablePolicyLibRuntimeDxe.inf + MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.inf MdeModulePkg/Library/VarCheckLib/VarCheckLib.inf MdeModulePkg/Library/VarCheckHiiLib/VarCheckHiiLib.inf MdeModulePkg/Library/VarCheckPcdLib/VarCheckPcdLib.inf @@ -458,6 +459,7 @@ [Components.IA32, Components.X64] MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf { + NULL|MdeModulePkg/Library/VarCheckPolicyLib/VarCheckPolicyLib.inf NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf NULL|MdeModulePkg/Library/VarCheckHiiLib/VarCheckHiiLib.inf NULL|MdeModulePkg/Library/VarCheckPcdLib/VarCheckPcdLib.inf --=20 2.16.3.windows.1