]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
MdeModulePkg/BdsDxe: Check deferred images before booting to OS
[mirror_edk2.git] / MdeModulePkg / Universal / SecurityStubDxe / Defer3rdPartyImageLoad.c
CommitLineData
8be37a5c
RN
1/** @file\r
2 Implement defer image load services for user identification in UEFI2.2.\r
3\r
4Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14#include "Defer3rdPartyImageLoad.h"\r
15\r
16//\r
17// The structure to save the deferred 3rd party image information.\r
18//\r
19typedef struct {\r
20 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r
21 BOOLEAN BootOption;\r
22 BOOLEAN Loaded;\r
23} DEFERRED_3RD_PARTY_IMAGE_INFO;\r
24\r
25//\r
26// The table to save the deferred 3rd party image item.\r
27//\r
28typedef struct {\r
29 UINTN Count; ///< deferred 3rd party image count\r
30 DEFERRED_3RD_PARTY_IMAGE_INFO *ImageInfo; ///< deferred 3rd party image item\r
31} DEFERRED_3RD_PARTY_IMAGE_TABLE;\r
32\r
33BOOLEAN mEndOfDxe = FALSE;\r
34DEFERRED_3RD_PARTY_IMAGE_TABLE mDeferred3rdPartyImage = {\r
35 0, // Deferred image count\r
36 NULL // The deferred image info\r
37};\r
38\r
39EFI_DEFERRED_IMAGE_LOAD_PROTOCOL mDeferredImageLoad = {\r
40 GetDefferedImageInfo\r
41};\r
42\r
43/**\r
44 Return whether the file comes from FV.\r
45\r
46 @param[in] File This is a pointer to the device path of the file\r
47 that is being dispatched.\r
48\r
49 @retval TRUE File comes from FV.\r
50 @retval FALSE File doesn't come from FV.\r
51**/\r
52BOOLEAN\r
53FileFromFv (\r
54 IN CONST EFI_DEVICE_PATH_PROTOCOL *File\r
55 )\r
56{\r
57 EFI_STATUS Status;\r
58 EFI_HANDLE DeviceHandle;\r
59 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
60\r
61 //\r
62 // First check to see if File is from a Firmware Volume\r
63 //\r
64 DeviceHandle = NULL;\r
65 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;\r
66 Status = gBS->LocateDevicePath (\r
67 &gEfiFirmwareVolume2ProtocolGuid,\r
68 &TempDevicePath,\r
69 &DeviceHandle\r
70 );\r
71 if (!EFI_ERROR (Status)) {\r
72 Status = gBS->OpenProtocol (\r
73 DeviceHandle,\r
74 &gEfiFirmwareVolume2ProtocolGuid,\r
75 NULL,\r
76 NULL,\r
77 NULL,\r
78 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
79 );\r
80 if (!EFI_ERROR (Status)) {\r
81 return TRUE;\r
82 }\r
83 }\r
84\r
85 return FALSE;\r
86}\r
87\r
88/**\r
89 Find the deferred image which matches the device path.\r
90\r
91 @param[in] ImageDevicePath A pointer to the device path of a image.\r
92 @param[in] BootOption Whether the image is a boot option.\r
93\r
94 @return Pointer to the found deferred image or NULL if not found.\r
95**/\r
96DEFERRED_3RD_PARTY_IMAGE_INFO *\r
97LookupImage (\r
98 IN CONST EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath,\r
99 IN BOOLEAN BootOption\r
100 )\r
101{\r
102 UINTN Index;\r
103 UINTN DevicePathSize;\r
104\r
105 DevicePathSize = GetDevicePathSize (ImageDevicePath);\r
106\r
107 for (Index = 0; Index < mDeferred3rdPartyImage.Count; Index++) {\r
108 if (CompareMem (ImageDevicePath, mDeferred3rdPartyImage.ImageInfo[Index].ImageDevicePath, DevicePathSize) == 0) {\r
109 ASSERT (mDeferred3rdPartyImage.ImageInfo[Index].BootOption == BootOption);\r
110 return &mDeferred3rdPartyImage.ImageInfo[Index];\r
111 }\r
112 }\r
113\r
114 return NULL;\r
115}\r
116\r
117/**\r
118 Add the image info to a deferred image list.\r
119\r
120 @param[in] ImageDevicePath A pointer to the device path of a image.\r
121 @param[in] BootOption Whether the image is a boot option.\r
122\r
123**/\r
124VOID\r
125QueueImage (\r
126 IN CONST EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath,\r
127 IN BOOLEAN BootOption\r
128 )\r
129{\r
130 DEFERRED_3RD_PARTY_IMAGE_INFO *ImageInfo;\r
131\r
132 //\r
133 // Expand memory for the new deferred image.\r
134 //\r
135 ImageInfo = ReallocatePool (\r
136 mDeferred3rdPartyImage.Count * sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO),\r
137 (mDeferred3rdPartyImage.Count + 1) * sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO),\r
138 mDeferred3rdPartyImage.ImageInfo\r
139 );\r
140 if (ImageInfo == NULL) {\r
141 return;\r
142 }\r
143 mDeferred3rdPartyImage.ImageInfo = ImageInfo;\r
144\r
145 //\r
146 // Save the deferred image information.\r
147 //\r
148 ImageInfo = &mDeferred3rdPartyImage.ImageInfo[mDeferred3rdPartyImage.Count];\r
149 ImageInfo->ImageDevicePath = DuplicateDevicePath (ImageDevicePath);\r
150 if (ImageInfo->ImageDevicePath == NULL) {\r
151 return;\r
152 }\r
153 ImageInfo->BootOption = BootOption;\r
154 ImageInfo->Loaded = FALSE;\r
155 mDeferred3rdPartyImage.Count++;\r
156}\r
157\r
158\r
159/**\r
160 Returns information about a deferred image.\r
161\r
162 This function returns information about a single deferred image. The deferred images are\r
163 numbered consecutively, starting with 0. If there is no image which corresponds to\r
164 ImageIndex, then EFI_NOT_FOUND is returned. All deferred images may be returned by\r
165 iteratively calling this function until EFI_NOT_FOUND is returned.\r
166 Image may be NULL and ImageSize set to 0 if the decision to defer execution was made\r
167 because of the location of the executable image, rather than its actual contents.\r
168\r
169 @param[in] This Points to this instance of the EFI_DEFERRED_IMAGE_LOAD_PROTOCOL.\r
170 @param[in] ImageIndex Zero-based index of the deferred index.\r
171 @param[out] ImageDevicePath On return, points to a pointer to the device path of the image.\r
172 The device path should not be freed by the caller.\r
173 @param[out] Image On return, points to the first byte of the image or NULL if the\r
174 image is not available. The image should not be freed by the caller\r
175 unless LoadImage() has been successfully called.\r
176 @param[out] ImageSize On return, the size of the image, or 0 if the image is not available.\r
177 @param[out] BootOption On return, points to TRUE if the image was intended as a boot option\r
178 or FALSE if it was not intended as a boot option.\r
179\r
180 @retval EFI_SUCCESS Image information returned successfully.\r
181 @retval EFI_NOT_FOUND ImageIndex does not refer to a valid image.\r
182 @retval EFI_INVALID_PARAMETER ImageDevicePath is NULL or Image is NULL or ImageSize is NULL or\r
183 BootOption is NULL.\r
184\r
185**/\r
186EFI_STATUS\r
187EFIAPI\r
188GetDefferedImageInfo (\r
189 IN EFI_DEFERRED_IMAGE_LOAD_PROTOCOL *This,\r
190 IN UINTN ImageIndex,\r
191 OUT EFI_DEVICE_PATH_PROTOCOL **ImageDevicePath,\r
192 OUT VOID **Image,\r
193 OUT UINTN *ImageSize,\r
194 OUT BOOLEAN *BootOption\r
195 )\r
196{\r
197 UINTN Index;\r
198 UINTN NewCount;\r
199\r
200 if ((This == NULL) || (ImageSize == NULL) || (Image == NULL)) {\r
201 return EFI_INVALID_PARAMETER;\r
202 }\r
203\r
204 if ((ImageDevicePath == NULL) || (BootOption == NULL)) {\r
205 return EFI_INVALID_PARAMETER;\r
206 }\r
207\r
208 //\r
209 // Remove the loaded images from the defer list in the first call.\r
210 //\r
211 if (ImageIndex == 0) {\r
212 NewCount = 0;\r
213 for (Index = 0; Index < mDeferred3rdPartyImage.Count; Index++) {\r
214 if (!mDeferred3rdPartyImage.ImageInfo[Index].Loaded) {\r
215 CopyMem (\r
216 &mDeferred3rdPartyImage.ImageInfo[NewCount],\r
217 &mDeferred3rdPartyImage.ImageInfo[Index],\r
218 sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO)\r
219 );\r
220 NewCount++;\r
221 }\r
222 }\r
223\r
224 mDeferred3rdPartyImage.Count = NewCount;\r
225 }\r
226\r
227 if (ImageIndex >= mDeferred3rdPartyImage.Count) {\r
228 return EFI_NOT_FOUND;\r
229 }\r
230\r
231 //\r
232 // Get the request deferred image.\r
233 //\r
234 *ImageDevicePath = mDeferred3rdPartyImage.ImageInfo[ImageIndex].ImageDevicePath;\r
235 *BootOption = mDeferred3rdPartyImage.ImageInfo[ImageIndex].BootOption;\r
236 *Image = NULL;\r
237 *ImageSize = 0;\r
238\r
239 return EFI_SUCCESS;\r
240}\r
241\r
242/**\r
243 Callback function executed when the EndOfDxe event group is signaled.\r
244\r
245 @param[in] Event Event whose notification function is being invoked.\r
246 @param[in] Context The pointer to the notification function's context, which\r
247 is implementation-dependent.\r
248**/\r
249VOID\r
250EFIAPI\r
251EndOfDxe (\r
252 IN EFI_EVENT Event,\r
253 IN VOID *Context\r
254 )\r
255{\r
256 mEndOfDxe = TRUE;\r
257}\r
258\r
259/**\r
260 Defer the 3rd party image load and installs Deferred Image Load Protocol.\r
261\r
262 @param[in] File This is a pointer to the device path of the file that\r
263 is being dispatched. This will optionally be used for\r
264 logging.\r
265 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
266\r
267 @retval EFI_SUCCESS The file is not 3rd party image and can be loaded immediately.\r
268 @retval EFI_ACCESS_DENIED The file is 3rd party image and needs deferred.\r
269**/\r
270EFI_STATUS\r
271Defer3rdPartyImageLoad (\r
272 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,\r
273 IN BOOLEAN BootPolicy\r
274 )\r
275{\r
276 DEFERRED_3RD_PARTY_IMAGE_INFO *ImageInfo;\r
277\r
278 //\r
279 // Ignore if File is NULL.\r
280 //\r
281 if (File == NULL) {\r
282 return EFI_SUCCESS;\r
283 }\r
284\r
285 if (FileFromFv (File)) {\r
286 return EFI_SUCCESS;\r
287 }\r
288\r
289 ImageInfo = LookupImage (File, BootPolicy);\r
290\r
291 DEBUG_CODE (\r
292 CHAR16 *DevicePathStr;\r
293 DevicePathStr = ConvertDevicePathToText (File, FALSE, FALSE);\r
294 DEBUG ((\r
295 DEBUG_INFO,\r
296 "[Security] 3rd party image[%p] %s EndOfDxe: %s.\n", ImageInfo,\r
297 mEndOfDxe ? L"can be loaded after": L"is deferred to load before",\r
298 DevicePathStr\r
299 ));\r
300 if (DevicePathStr != NULL) {\r
301 FreePool (DevicePathStr);\r
302 }\r
303 );\r
304\r
305 if (mEndOfDxe) {\r
306 //\r
307 // The image might be first time loaded after EndOfDxe,\r
308 // So ImageInfo can be NULL.\r
309 //\r
310 if (ImageInfo != NULL) {\r
311 ImageInfo->Loaded = TRUE;\r
312 }\r
313 return EFI_SUCCESS;\r
314 } else {\r
315 //\r
316 // The image might be second time loaded before EndOfDxe,\r
317 // So ImageInfo can be non-NULL.\r
318 //\r
319 if (ImageInfo == NULL) {\r
320 QueueImage (File, BootPolicy);\r
321 }\r
322 return EFI_ACCESS_DENIED;\r
323 }\r
324}\r
325\r
326/**\r
327 Installs DeferredImageLoad Protocol and listens EndOfDxe event.\r
328**/\r
329VOID\r
330Defer3rdPartyImageLoadInitialize (\r
331 VOID\r
332 )\r
333{\r
334 EFI_STATUS Status;\r
335 EFI_HANDLE Handle;\r
336 EFI_EVENT Event;\r
337\r
338 Handle = NULL;\r
339 Status = gBS->InstallMultipleProtocolInterfaces (\r
340 &Handle,\r
341 &gEfiDeferredImageLoadProtocolGuid,\r
342 &mDeferredImageLoad,\r
343 NULL\r
344 );\r
345 ASSERT_EFI_ERROR (Status);\r
346\r
347 Status = gBS->CreateEventEx (\r
348 EVT_NOTIFY_SIGNAL,\r
349 TPL_CALLBACK,\r
350 EndOfDxe,\r
351 NULL,\r
352 &gEfiEndOfDxeEventGroupGuid,\r
353 &Event\r
354 );\r
355 ASSERT_EFI_ERROR (Status);\r
356}\r