]>
Commit | Line | Data |
---|---|---|
483449c9 BB |
1 | /** @file -- VarCheckPolicyLib.c\r |
2 | This is a NULL library instance that leverages the VarCheck interface\r | |
3 | and the business logic behind the VariablePolicy code to make its decisions.\r | |
4 | \r | |
5 | Copyright (c) Microsoft Corporation.\r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | \r | |
8 | **/\r | |
9 | \r | |
10 | #include <Library/VarCheckLib.h>\r | |
11 | #include <Library/BaseLib.h>\r | |
12 | #include <Library/DebugLib.h>\r | |
13 | #include <Library/SafeIntLib.h>\r | |
14 | #include <Library/MmServicesTableLib.h>\r | |
483449c9 BB |
15 | #include <Library/BaseMemoryLib.h>\r |
16 | #include <Library/MemoryAllocationLib.h>\r | |
17 | \r | |
18 | #include <Protocol/MmCommunication.h>\r | |
19 | \r | |
20 | #include <Protocol/VariablePolicy.h>\r | |
21 | #include <Library/VariablePolicyLib.h>\r | |
22 | \r | |
23 | #include <Guid/VarCheckPolicyMmi.h>\r | |
24 | \r | |
e2747dbb MK |
25 | #include "VarCheckPolicyLib.h"\r |
26 | \r | |
483449c9 BB |
27 | //================================================\r |
28 | // As a VarCheck library, we're linked into the VariableServices\r | |
29 | // and may not be able to call them indirectly. To get around this,\r | |
30 | // use the internal GetVariable function to query the variable store.\r | |
31 | //================================================\r | |
32 | EFI_STATUS\r | |
33 | EFIAPI\r | |
34 | VariableServiceGetVariable (\r | |
35 | IN CHAR16 *VariableName,\r | |
36 | IN EFI_GUID *VendorGuid,\r | |
37 | OUT UINT32 *Attributes OPTIONAL,\r | |
38 | IN OUT UINTN *DataSize,\r | |
39 | OUT VOID *Data\r | |
40 | );\r | |
41 | \r | |
42 | \r | |
43 | UINT8 mSecurityEvalBuffer[VAR_CHECK_POLICY_MM_COMM_BUFFER_SIZE];\r | |
44 | \r | |
45 | // Pagination Cache Variables\r | |
46 | UINT8 *mPaginationCache = NULL;\r | |
47 | UINTN mPaginationCacheSize = 0;\r | |
48 | UINT32 mCurrentPaginationCommand = 0;\r | |
49 | \r | |
50 | \r | |
51 | /**\r | |
52 | MM Communication Handler to recieve commands from the DXE protocol for\r | |
53 | Variable Policies. This communication channel is used to register new policies\r | |
54 | and poll and toggle the enforcement of variable policies.\r | |
55 | \r | |
56 | @param[in] DispatchHandle All parameters standard to MM communications convention.\r | |
57 | @param[in] RegisterContext All parameters standard to MM communications convention.\r | |
58 | @param[in,out] CommBuffer All parameters standard to MM communications convention.\r | |
59 | @param[in,out] CommBufferSize All parameters standard to MM communications convention.\r | |
60 | \r | |
61 | @retval EFI_SUCCESS\r | |
62 | @retval EFI_INVALID_PARAMETER CommBuffer or CommBufferSize is null pointer.\r | |
63 | @retval EFI_INVALID_PARAMETER CommBuffer size is wrong.\r | |
64 | @retval EFI_INVALID_PARAMETER Revision or signature don't match.\r | |
65 | \r | |
66 | **/\r | |
67 | STATIC\r | |
68 | EFI_STATUS\r | |
69 | EFIAPI\r | |
70 | VarCheckPolicyLibMmiHandler (\r | |
71 | IN EFI_HANDLE DispatchHandle,\r | |
72 | IN CONST VOID *RegisterContext,\r | |
73 | IN OUT VOID *CommBuffer,\r | |
74 | IN OUT UINTN *CommBufferSize\r | |
75 | )\r | |
76 | {\r | |
77 | UINTN InternalCommBufferSize;\r | |
78 | VOID *InternalCommBuffer;\r | |
79 | EFI_STATUS Status;\r | |
80 | EFI_STATUS SubCommandStatus;\r | |
81 | VAR_CHECK_POLICY_COMM_HEADER *PolicyCommmHeader;\r | |
82 | VAR_CHECK_POLICY_COMM_HEADER *InternalPolicyCommmHeader;\r | |
83 | VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS *IsEnabledParams;\r | |
84 | VAR_CHECK_POLICY_COMM_DUMP_PARAMS *DumpParamsIn;\r | |
85 | VAR_CHECK_POLICY_COMM_DUMP_PARAMS *DumpParamsOut;\r | |
86 | UINT8 *DumpInputBuffer;\r | |
87 | UINT8 *DumpOutputBuffer;\r | |
88 | UINTN DumpTotalPages;\r | |
89 | VARIABLE_POLICY_ENTRY *PolicyEntry;\r | |
90 | UINTN ExpectedSize;\r | |
91 | UINT32 TempSize;\r | |
92 | \r | |
93 | Status = EFI_SUCCESS;\r | |
94 | \r | |
95 | //\r | |
96 | // Validate some input parameters.\r | |
97 | //\r | |
98 | // If either of the pointers are NULL, we can't proceed.\r | |
99 | if (CommBuffer == NULL || CommBufferSize == NULL) {\r | |
100 | DEBUG(( DEBUG_INFO, "%a - Invalid comm buffer pointers!\n", __FUNCTION__ ));\r | |
101 | return EFI_INVALID_PARAMETER;\r | |
102 | }\r | |
103 | // Make sure that the buffer does not overlap SMM.\r | |
104 | // This should be covered by the SmiManage infrastructure, but just to be safe...\r | |
105 | InternalCommBufferSize = *CommBufferSize;\r | |
e2747dbb MK |
106 | if (InternalCommBufferSize > VAR_CHECK_POLICY_MM_COMM_BUFFER_SIZE ||\r |
107 | !VarCheckPolicyIsBufferOutsideValid((UINTN)CommBuffer, (UINT64)InternalCommBufferSize)) {\r | |
483449c9 BB |
108 | DEBUG ((DEBUG_ERROR, "%a - Invalid CommBuffer supplied! 0x%016lX[0x%016lX]\n", __FUNCTION__, CommBuffer, InternalCommBufferSize));\r |
109 | return EFI_INVALID_PARAMETER;\r | |
110 | }\r | |
111 | // If the size does not meet a minimum threshold, we cannot proceed.\r | |
112 | ExpectedSize = sizeof(VAR_CHECK_POLICY_COMM_HEADER);\r | |
113 | if (InternalCommBufferSize < ExpectedSize) {\r | |
114 | DEBUG(( DEBUG_INFO, "%a - Bad comm buffer size! %d < %d\n", __FUNCTION__, InternalCommBufferSize, ExpectedSize ));\r | |
115 | return EFI_INVALID_PARAMETER;\r | |
116 | }\r | |
117 | \r | |
118 | //\r | |
119 | // Before proceeding any further, copy the buffer internally so that we can compare\r | |
120 | // without worrying about TOCTOU.\r | |
121 | //\r | |
122 | InternalCommBuffer = &mSecurityEvalBuffer[0];\r | |
123 | CopyMem(InternalCommBuffer, CommBuffer, InternalCommBufferSize);\r | |
124 | PolicyCommmHeader = CommBuffer;\r | |
125 | InternalPolicyCommmHeader = InternalCommBuffer;\r | |
126 | // Check the revision and the signature of the comm header.\r | |
127 | if (InternalPolicyCommmHeader->Signature != VAR_CHECK_POLICY_COMM_SIG ||\r | |
128 | InternalPolicyCommmHeader->Revision != VAR_CHECK_POLICY_COMM_REVISION) {\r | |
129 | DEBUG(( DEBUG_INFO, "%a - Signature or revision are incorrect!\n", __FUNCTION__ ));\r | |
130 | // We have verified the buffer is not null and have enough size to hold Result field.\r | |
131 | PolicyCommmHeader->Result = EFI_INVALID_PARAMETER;\r | |
132 | return EFI_SUCCESS;\r | |
133 | }\r | |
134 | \r | |
135 | // If we're in the middle of a paginated dump and any other command is sent,\r | |
136 | // pagination cache must be cleared.\r | |
137 | if (mPaginationCache != NULL && InternalPolicyCommmHeader->Command != mCurrentPaginationCommand) {\r | |
138 | FreePool (mPaginationCache);\r | |
139 | mPaginationCache = NULL;\r | |
140 | mPaginationCacheSize = 0;\r | |
141 | mCurrentPaginationCommand = 0;\r | |
142 | }\r | |
143 | \r | |
144 | //\r | |
145 | // Now we can process the command as it was sent.\r | |
146 | //\r | |
147 | PolicyCommmHeader->Result = EFI_ABORTED; // Set a default return for incomplete commands.\r | |
148 | switch(InternalPolicyCommmHeader->Command) {\r | |
149 | case VAR_CHECK_POLICY_COMMAND_DISABLE:\r | |
150 | PolicyCommmHeader->Result = DisableVariablePolicy();\r | |
151 | break;\r | |
152 | \r | |
153 | case VAR_CHECK_POLICY_COMMAND_IS_ENABLED:\r | |
154 | // Make sure that we're dealing with a reasonable size.\r | |
155 | // This add should be safe because these are fixed sizes so far.\r | |
156 | ExpectedSize += sizeof(VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS);\r | |
157 | if (InternalCommBufferSize < ExpectedSize) {\r | |
158 | DEBUG(( DEBUG_INFO, "%a - Bad comm buffer size! %d < %d\n", __FUNCTION__, InternalCommBufferSize, ExpectedSize ));\r | |
159 | PolicyCommmHeader->Result = EFI_INVALID_PARAMETER;\r | |
160 | break;\r | |
161 | }\r | |
162 | \r | |
163 | // Now that we know we've got a valid size, we can fill in the rest of the data.\r | |
164 | IsEnabledParams = (VAR_CHECK_POLICY_COMM_IS_ENABLED_PARAMS*)((UINT8*)CommBuffer + sizeof(VAR_CHECK_POLICY_COMM_HEADER));\r | |
165 | IsEnabledParams->State = IsVariablePolicyEnabled();\r | |
166 | PolicyCommmHeader->Result = EFI_SUCCESS;\r | |
167 | break;\r | |
168 | \r | |
169 | case VAR_CHECK_POLICY_COMMAND_REGISTER:\r | |
170 | // Make sure that we're dealing with a reasonable size.\r | |
171 | // This add should be safe because these are fixed sizes so far.\r | |
172 | ExpectedSize += sizeof(VARIABLE_POLICY_ENTRY);\r | |
173 | if (InternalCommBufferSize < ExpectedSize) {\r | |
174 | DEBUG(( DEBUG_INFO, "%a - Bad comm buffer size! %d < %d\n", __FUNCTION__, InternalCommBufferSize, ExpectedSize ));\r | |
175 | PolicyCommmHeader->Result = EFI_INVALID_PARAMETER;\r | |
176 | break;\r | |
177 | }\r | |
178 | \r | |
179 | // At the very least, we can assume that we're working with a valid policy entry.\r | |
180 | // Time to compare its internal size.\r | |
181 | PolicyEntry = (VARIABLE_POLICY_ENTRY*)((UINT8*)InternalCommBuffer + sizeof(VAR_CHECK_POLICY_COMM_HEADER));\r | |
182 | if (PolicyEntry->Version != VARIABLE_POLICY_ENTRY_REVISION ||\r | |
183 | PolicyEntry->Size < sizeof(VARIABLE_POLICY_ENTRY) ||\r | |
184 | EFI_ERROR(SafeUintnAdd(sizeof(VAR_CHECK_POLICY_COMM_HEADER), PolicyEntry->Size, &ExpectedSize)) ||\r | |
185 | InternalCommBufferSize < ExpectedSize) {\r | |
186 | DEBUG(( DEBUG_INFO, "%a - Bad policy entry contents!\n", __FUNCTION__ ));\r | |
187 | PolicyCommmHeader->Result = EFI_INVALID_PARAMETER;\r | |
188 | break;\r | |
189 | }\r | |
190 | \r | |
191 | PolicyCommmHeader->Result = RegisterVariablePolicy( PolicyEntry );\r | |
192 | break;\r | |
193 | \r | |
194 | case VAR_CHECK_POLICY_COMMAND_DUMP:\r | |
195 | // Make sure that we're dealing with a reasonable size.\r | |
196 | // This add should be safe because these are fixed sizes so far.\r | |
197 | ExpectedSize += sizeof(VAR_CHECK_POLICY_COMM_DUMP_PARAMS) + VAR_CHECK_POLICY_MM_DUMP_BUFFER_SIZE;\r | |
198 | if (InternalCommBufferSize < ExpectedSize) {\r | |
199 | DEBUG(( DEBUG_INFO, "%a - Bad comm buffer size! %d < %d\n", __FUNCTION__, InternalCommBufferSize, ExpectedSize ));\r | |
200 | PolicyCommmHeader->Result = EFI_INVALID_PARAMETER;\r | |
201 | break;\r | |
202 | }\r | |
203 | \r | |
204 | // Now that we know we've got a valid size, we can fill in the rest of the data.\r | |
205 | DumpParamsIn = (VAR_CHECK_POLICY_COMM_DUMP_PARAMS*)(InternalPolicyCommmHeader + 1);\r | |
206 | DumpParamsOut = (VAR_CHECK_POLICY_COMM_DUMP_PARAMS*)(PolicyCommmHeader + 1);\r | |
207 | \r | |
208 | // If we're requesting the first page, initialize the cache and get the sizes.\r | |
209 | if (DumpParamsIn->PageRequested == 0) {\r | |
210 | if (mPaginationCache != NULL) {\r | |
211 | FreePool (mPaginationCache);\r | |
212 | mPaginationCache = NULL;\r | |
213 | }\r | |
214 | \r | |
215 | // Determine what the required size is going to be.\r | |
216 | DumpParamsOut->TotalSize = 0;\r | |
217 | DumpParamsOut->PageSize = 0;\r | |
218 | DumpParamsOut->HasMore = FALSE;\r | |
219 | SubCommandStatus = DumpVariablePolicy (NULL, &TempSize);\r | |
220 | if (SubCommandStatus == EFI_BUFFER_TOO_SMALL && TempSize > 0) {\r | |
221 | mCurrentPaginationCommand = VAR_CHECK_POLICY_COMMAND_DUMP;\r | |
222 | mPaginationCacheSize = TempSize;\r | |
223 | DumpParamsOut->TotalSize = TempSize;\r | |
224 | mPaginationCache = AllocatePool (mPaginationCacheSize);\r | |
225 | if (mPaginationCache == NULL) {\r | |
226 | SubCommandStatus = EFI_OUT_OF_RESOURCES;\r | |
227 | }\r | |
228 | }\r | |
229 | \r | |
230 | // If we've allocated our pagination cache, we're good to cache.\r | |
231 | if (mPaginationCache != NULL) {\r | |
232 | SubCommandStatus = DumpVariablePolicy (mPaginationCache, &TempSize);\r | |
233 | }\r | |
234 | \r | |
235 | // Populate the remaining fields and we can boogie.\r | |
236 | if (!EFI_ERROR (SubCommandStatus) && mPaginationCache != NULL) {\r | |
237 | DumpParamsOut->HasMore = TRUE;\r | |
238 | }\r | |
239 | } else if (mPaginationCache != NULL) {\r | |
240 | DumpParamsOut->TotalSize = (UINT32)mPaginationCacheSize;\r | |
241 | DumpOutputBuffer = (UINT8*)(DumpParamsOut + 1);\r | |
242 | \r | |
243 | // Make sure that we don't over-index the cache.\r | |
244 | DumpTotalPages = mPaginationCacheSize / VAR_CHECK_POLICY_MM_DUMP_BUFFER_SIZE;\r | |
245 | if (mPaginationCacheSize % VAR_CHECK_POLICY_MM_DUMP_BUFFER_SIZE != 0) {\r | |
246 | DumpTotalPages++;\r | |
247 | }\r | |
248 | if (DumpParamsIn->PageRequested > DumpTotalPages) {\r | |
249 | SubCommandStatus = EFI_INVALID_PARAMETER;\r | |
250 | } else {\r | |
251 | // Figure out how far into the page cache we need to go for our next page.\r | |
252 | // We know the blind subtraction won't be bad because we already checked for page 0.\r | |
253 | DumpInputBuffer = &mPaginationCache[VAR_CHECK_POLICY_MM_DUMP_BUFFER_SIZE * (DumpParamsIn->PageRequested - 1)];\r | |
254 | TempSize = VAR_CHECK_POLICY_MM_DUMP_BUFFER_SIZE;\r | |
255 | // If we're getting the last page, adjust the PageSize.\r | |
256 | if (DumpParamsIn->PageRequested == DumpTotalPages) {\r | |
257 | TempSize = mPaginationCacheSize % VAR_CHECK_POLICY_MM_DUMP_BUFFER_SIZE;\r | |
258 | }\r | |
259 | CopyMem (DumpOutputBuffer, DumpInputBuffer, TempSize);\r | |
260 | DumpParamsOut->PageSize = TempSize;\r | |
261 | // If we just got the last page, settle up the cache.\r | |
262 | if (DumpParamsIn->PageRequested == DumpTotalPages) {\r | |
263 | DumpParamsOut->HasMore = FALSE;\r | |
264 | FreePool (mPaginationCache);\r | |
265 | mPaginationCache = NULL;\r | |
266 | mPaginationCacheSize = 0;\r | |
267 | mCurrentPaginationCommand = 0;\r | |
268 | // Otherwise, we could do more here.\r | |
269 | } else {\r | |
270 | DumpParamsOut->HasMore = TRUE;\r | |
271 | }\r | |
272 | \r | |
273 | // If we made it this far, we're basically good.\r | |
274 | SubCommandStatus = EFI_SUCCESS;\r | |
275 | }\r | |
276 | // If we've requested any other page than 0 and the cache is empty, we must have timed out.\r | |
277 | } else {\r | |
278 | DumpParamsOut->TotalSize = 0;\r | |
279 | DumpParamsOut->PageSize = 0;\r | |
280 | DumpParamsOut->HasMore = FALSE;\r | |
281 | SubCommandStatus = EFI_TIMEOUT;\r | |
282 | }\r | |
283 | \r | |
284 | // There's currently no use for this, but it shouldn't be hard to implement.\r | |
285 | PolicyCommmHeader->Result = SubCommandStatus;\r | |
286 | break;\r | |
287 | \r | |
288 | case VAR_CHECK_POLICY_COMMAND_LOCK:\r | |
289 | PolicyCommmHeader->Result = LockVariablePolicy();\r | |
290 | break;\r | |
291 | \r | |
292 | default:\r | |
293 | // Mark unknown requested command as EFI_UNSUPPORTED.\r | |
294 | DEBUG(( DEBUG_INFO, "%a - Invalid command requested! %d\n", __FUNCTION__, PolicyCommmHeader->Command ));\r | |
295 | PolicyCommmHeader->Result = EFI_UNSUPPORTED;\r | |
296 | break;\r | |
297 | }\r | |
298 | \r | |
299 | DEBUG(( DEBUG_VERBOSE, "%a - Command %d returning %r.\n", __FUNCTION__,\r | |
300 | PolicyCommmHeader->Command, PolicyCommmHeader->Result ));\r | |
301 | \r | |
302 | return Status;\r | |
303 | }\r | |
304 | \r | |
305 | \r | |
306 | /**\r | |
307 | Constructor function of VarCheckPolicyLib to register VarCheck handler and\r | |
308 | SW MMI handlers.\r | |
309 | \r | |
483449c9 BB |
310 | @retval EFI_SUCCESS The constructor executed correctly.\r |
311 | \r | |
312 | **/\r | |
313 | EFI_STATUS\r | |
314 | EFIAPI\r | |
e2747dbb MK |
315 | VarCheckPolicyLibCommonConstructor (\r |
316 | VOID\r | |
483449c9 BB |
317 | )\r |
318 | {\r | |
319 | EFI_STATUS Status;\r | |
320 | EFI_HANDLE DiscardedHandle;\r | |
321 | \r | |
322 | // Initialize the business logic with the internal GetVariable handler.\r | |
323 | Status = InitVariablePolicyLib( VariableServiceGetVariable );\r | |
324 | \r | |
325 | // Only proceed with init if the business logic could be initialized.\r | |
326 | if (!EFI_ERROR( Status )) {\r | |
327 | // Register the VarCheck handler for SetVariable filtering.\r | |
328 | // Forward the check to the business logic of the library.\r | |
329 | VarCheckLibRegisterSetVariableCheckHandler( ValidateSetVariable );\r | |
330 | \r | |
331 | // Register the MMI handlers for receiving policy commands.\r | |
332 | DiscardedHandle = NULL;\r | |
333 | Status = gMmst->MmiHandlerRegister( VarCheckPolicyLibMmiHandler,\r | |
334 | &gVarCheckPolicyLibMmiHandlerGuid,\r | |
335 | &DiscardedHandle );\r | |
336 | }\r | |
337 | // Otherwise, there's not much we can do.\r | |
338 | else {\r | |
339 | DEBUG(( DEBUG_ERROR, "%a - Cannot Initialize VariablePolicyLib! %r\n", __FUNCTION__, Status ));\r | |
340 | ASSERT_EFI_ERROR( Status );\r | |
341 | }\r | |
342 | \r | |
343 | return Status;\r | |
344 | }\r |