Add more strict check for MOR variable, besides MOR lock variable.
[mirror_edk2.git] / SecurityPkg / Tcg / MemoryOverwriteRequestControlLock / TcgMorLock.c
CommitLineData
70c7664c
JY
1/** @file\r
2 TCG MOR (Memory Overwrite Request) Lock Control Driver.\r
3\r
4 This driver initilize MemoryOverwriteRequestControlLock variable.\r
5 This module will add Variable Hook and allow MemoryOverwriteRequestControlLock variable set only once.\r
6\r
7Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
8This program and the accompanying materials \r
9are licensed and made available under the terms and conditions of the BSD License \r
10which accompanies this distribution. The full text of the license may be found at \r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <PiDxe.h>\r
19#include <Guid/MemoryOverwriteControl.h>\r
20#include <IndustryStandard/MemoryOverwriteRequestControlLock.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/BaseLib.h>\r
23#include <Library/BaseMemoryLib.h>\r
24#include "TcgMorLock.h"\r
25\r
26typedef struct {\r
27 CHAR16 *VariableName;\r
28 EFI_GUID *VendorGuid;\r
29} VARIABLE_TYPE;\r
30\r
31VARIABLE_TYPE mMorVariableType[] = {\r
32 {MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME, &gEfiMemoryOverwriteControlDataGuid},\r
33 {MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME, &gEfiMemoryOverwriteRequestControlLockGuid},\r
34};\r
35\r
36/**\r
37 Returns if this is MOR related variable.\r
38\r
39 @param VariableName the name of the vendor's variable, it's a Null-Terminated Unicode String\r
40 @param VendorGuid Unify identifier for vendor.\r
41\r
42 @retval TRUE The variable is MOR related.\r
43 @retval FALSE The variable is NOT MOR related.\r
44**/\r
45BOOLEAN\r
46IsAnyMorVariable (\r
47 IN CHAR16 *VariableName,\r
48 IN EFI_GUID *VendorGuid\r
49 )\r
50{\r
51 UINTN Index;\r
52\r
53 for (Index = 0; Index < sizeof(mMorVariableType)/sizeof(mMorVariableType[0]); Index++) {\r
54 if ((StrCmp (VariableName, mMorVariableType[Index].VariableName) == 0) && \r
55 (CompareGuid (VendorGuid, mMorVariableType[Index].VendorGuid))) {\r
56 return TRUE;\r
57 }\r
58 }\r
59 return FALSE;\r
60}\r
61\r
62/**\r
63 Returns if this is MOR lock variable.\r
64\r
65 @param VariableName the name of the vendor's variable, it's a Null-Terminated Unicode String\r
66 @param VendorGuid Unify identifier for vendor.\r
67\r
68 @retval TRUE The variable is MOR lock variable.\r
69 @retval FALSE The variable is NOT MOR lock variable.\r
70**/\r
71BOOLEAN\r
72IsMorLockVariable (\r
73 IN CHAR16 *VariableName,\r
74 IN EFI_GUID *VendorGuid\r
75 )\r
76{\r
77 if ((StrCmp (VariableName, MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME) == 0) && \r
78 (CompareGuid (VendorGuid, &gEfiMemoryOverwriteRequestControlLockGuid))) {\r
79 return TRUE;\r
80 }\r
81 return FALSE;\r
82}\r
83\r
84/**\r
85 This service is a checker handler for the UEFI Runtime Service SetVariable()\r
86\r
87 @param VariableName the name of the vendor's variable, as a\r
88 Null-Terminated Unicode String\r
89 @param VendorGuid Unify identifier for vendor.\r
90 @param Attributes Point to memory location to return the attributes of variable. If the point\r
91 is NULL, the parameter would be ignored.\r
92 @param DataSize The size in bytes of Data-Buffer.\r
93 @param Data Point to the content of the variable.\r
94\r
95 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
96 defined by the Attributes.\r
97 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the\r
98 DataSize exceeds the maximum allowed.\r
99 @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string.\r
100 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
101 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r
102 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
103 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
104 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\r
105 set but the AuthInfo does NOT pass the validation check carried\r
106 out by the firmware.\r
107 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
108\r
109**/\r
110EFI_STATUS\r
111EFIAPI\r
112SetVariableCheckHandlerMor (\r
113 IN CHAR16 *VariableName,\r
114 IN EFI_GUID *VendorGuid,\r
115 IN UINT32 Attributes,\r
116 IN UINTN DataSize,\r
117 IN VOID *Data\r
118 )\r
119{\r
120 UINTN MorLockDataSize;\r
121 BOOLEAN MorLock;\r
122 EFI_STATUS Status;\r
123\r
124 //\r
125 // do not handle non-MOR variable\r
126 //\r
127 if (!IsAnyMorVariable (VariableName, VendorGuid)) {\r
128 return EFI_SUCCESS;\r
129 }\r
130\r
131 MorLockDataSize = sizeof(MorLock);\r
132 Status = InternalGetVariable (\r
133 MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME,\r
134 &gEfiMemoryOverwriteRequestControlLockGuid,\r
135 NULL,\r
136 &MorLockDataSize,\r
137 &MorLock\r
138 );\r
139 if (!EFI_ERROR (Status) && MorLock) {\r
140 //\r
141 // If lock, deny access\r
142 //\r
143 return EFI_INVALID_PARAMETER;\r
144 }\r
145 \r
77656653
JY
146 //\r
147 // Delete not OK\r
148 //\r
149 if ((DataSize != sizeof(UINT8)) || (Data == NULL) || (Attributes == 0)) {\r
150 return EFI_INVALID_PARAMETER;\r
151 }\r
152\r
70c7664c
JY
153 //\r
154 // check format\r
155 //\r
156 if (IsMorLockVariable(VariableName, VendorGuid)) {\r
70c7664c
JY
157 //\r
158 // set to any other value not OK\r
159 //\r
77656653 160 if ((*(UINT8 *)Data != 1) && (*(UINT8 *)Data != 0)) {\r
70c7664c
JY
161 return EFI_INVALID_PARAMETER;\r
162 }\r
163 }\r
164 //\r
165 // Or grant access\r
166 //\r
167 return EFI_SUCCESS;\r
168}\r
169\r
170/**\r
171 Entry Point for MOR Lock Control driver.\r
172\r
173 @param[in] ImageHandle Image handle of this driver.\r
174 @param[in] SystemTable A Pointer to the EFI System Table.\r
175\r
176 @retval EFI_SUCEESS \r
177 @return Others Some error occurs.\r
178**/\r
179EFI_STATUS\r
180EFIAPI\r
181MorLockDriverInit (\r
182 VOID\r
183 )\r
184{\r
185 EFI_STATUS Status;\r
186 UINT8 Data;\r
187\r
188 Data = 0;\r
189 Status = InternalSetVariable (\r
190 MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME,\r
191 &gEfiMemoryOverwriteRequestControlLockGuid,\r
192 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
193 1,\r
194 &Data\r
195 );\r
196 return Status;\r
197}\r