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