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