| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/* |
| 3 |
* Port to systemd-boot |
| 4 |
* Copyright © 2017 Max Resch <[email protected]> |
| 5 |
* |
| 6 |
* Security Policy Handling |
| 7 |
* Copyright © 2012 <[email protected]> |
| 8 |
* https://github.com/mjg59/efitools |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "efi-efivars.h" |
| 12 |
#include "secure-boot.h" |
| 13 |
#include "shim.h" |
| 14 |
#include "util.h" |
| 15 |
|
| 16 |
#if defined(__x86_64__) || defined(__i386__) |
| 17 |
#define __sysv_abi__ __attribute__((sysv_abi)) |
| 18 |
#else |
| 19 |
#define __sysv_abi__ |
| 20 |
#endif |
| 21 |
|
| 22 |
struct ShimLock { |
| 23 |
EFI_STATUS __sysv_abi__ (*shim_verify) (const void *buffer, uint32_t size); |
| 24 |
|
| 25 |
/* context is actually a struct for the PE header, but it isn't needed so void is sufficient just do define the interface |
| 26 |
* see shim.c/shim.h and PeHeader.h in the github shim repo */ |
| 27 |
EFI_STATUS __sysv_abi__ (*generate_hash) (void *data, uint32_t datasize, void *context, uint8_t *sha256hash, uint8_t *sha1hash); |
| 28 |
|
| 29 |
EFI_STATUS __sysv_abi__ (*read_header) (void *data, uint32_t datasize, void *context); |
| 30 |
}; |
| 31 |
|
| 32 |
#define SHIM_IMAGE_LOADER_GUID \ |
| 33 |
{ 0x1f492041, 0xfadb, 0x4e59, { 0x9e, 0x57, 0x7c, 0xaf, 0xe7, 0x3a, 0x55, 0xab } } |
| 34 |
|
| 35 |
bool shim_loaded(void) { |
| 36 |
struct ShimLock *shim_lock; |
| 37 |
|
| 38 |
return BS->LocateProtocol(MAKE_GUID_PTR(SHIM_LOCK), NULL, (void **) &shim_lock) == EFI_SUCCESS; |
| 39 |
} |
| 40 |
|
| 41 |
/* Check if SHIM_IMAGE_LOADER is available, shim 16 or newer. */ |
| 42 |
bool shim_loader_available(void) { |
| 43 |
void *shim_image_loader; |
| 44 |
|
| 45 |
return BS->LocateProtocol(MAKE_GUID_PTR(SHIM_IMAGE_LOADER), NULL, &shim_image_loader) == EFI_SUCCESS; |
| 46 |
} |
| 47 |
|
| 48 |
static bool shim_validate( |
| 49 |
const void *ctx, const EFI_DEVICE_PATH *device_path, const void *file_buffer, size_t file_size) { |
| 50 |
|
| 51 |
EFI_STATUS err; |
| 52 |
_cleanup_free_ char *file_buffer_owned = NULL; |
| 53 |
|
| 54 |
if (!file_buffer) { |
| 55 |
if (!device_path) |
| 56 |
return false; |
| 57 |
|
| 58 |
err = load_file_from_simple_filesystem(device_path, &file_buffer_owned, &file_size); |
| 59 |
if (err != EFI_SUCCESS) |
| 60 |
return false; |
| 61 |
|
| 62 |
file_buffer = file_buffer_owned; |
| 63 |
} |
| 64 |
|
| 65 |
struct ShimLock *shim_lock; |
| 66 |
err = BS->LocateProtocol(MAKE_GUID_PTR(SHIM_LOCK), NULL, (void **) &shim_lock); |
| 67 |
if (err != EFI_SUCCESS) |
| 68 |
return false; |
| 69 |
|
| 70 |
return shim_lock->shim_verify(file_buffer, file_size) == EFI_SUCCESS; |
| 71 |
} |
| 72 |
|
| 73 |
EFI_STATUS shim_load_image( |
| 74 |
EFI_HANDLE parent, |
| 75 |
const EFI_DEVICE_PATH *device_path, |
| 76 |
bool boot_policy, |
| 77 |
EFI_HANDLE *ret_image) { |
| 78 |
|
| 79 |
assert(device_path); |
| 80 |
assert(ret_image); |
| 81 |
|
| 82 |
/* The shim lock protocol is for pre-v16 shim, where it was not hooked up to the BS->LoadImage() |
| 83 |
* system table and friends, and it has to be checked manually via the shim_validate() helper. If the |
| 84 |
* shim image loader protocol is available (shim v16 and newer), then it will have overridden |
| 85 |
* BS->LoadImage() and friends in the system table, so no specific helper is needed, and the standard |
| 86 |
* BS->LoadImage() and friends can be called instead. |
| 87 |
*/ |
| 88 |
|
| 89 |
// TODO: drop lock protocol and just use plain BS->LoadImage once Shim < 16 is no longer supported |
| 90 |
|
| 91 |
bool have_shim = shim_loaded() && !shim_loader_available(); |
| 92 |
|
| 93 |
if (have_shim) |
| 94 |
install_security_override(shim_validate, NULL); |
| 95 |
|
| 96 |
_cleanup_free_ char *source_buffer = NULL; |
| 97 |
size_t source_size = 0; |
| 98 |
|
| 99 |
/* For some AMI firmware, BS->LoadImage() does not read correctly when the file comes the ESP on an |
| 100 |
* optical drive. But the simple filesystem protocol does work. So we try to load it. If that does |
| 101 |
* not work, we let BS->LoadImage() try instead. |
| 102 |
*/ |
| 103 |
(void) load_file_from_simple_filesystem(device_path, &source_buffer, &source_size); |
| 104 |
|
| 105 |
EFI_STATUS ret = BS->LoadImage( |
| 106 |
/* BootPolicy= */ boot_policy, |
| 107 |
parent, |
| 108 |
(EFI_DEVICE_PATH *) device_path, |
| 109 |
source_buffer, |
| 110 |
source_size, |
| 111 |
ret_image); |
| 112 |
if (have_shim) |
| 113 |
uninstall_security_override(); |
| 114 |
|
| 115 |
return ret; |
| 116 |
} |
| 117 |
|
| 118 |
void shim_retain_protocol(void) { |
| 119 |
uint8_t value = 1; |
| 120 |
|
| 121 |
// TODO: drop setting this var once Shim < 16 is no longer supported, as the lock protocol is no longer needed |
| 122 |
if (shim_loader_available() || !shim_loaded()) |
| 123 |
return; |
| 124 |
|
| 125 |
/* Ask Shim to avoid uninstalling its security protocol, so that we can use it from sd-stub to |
| 126 |
* validate PE addons. By default, Shim uninstalls its protocol when calling StartImage(). |
| 127 |
* Requires Shim 15.8. */ |
| 128 |
(void) efivar_set_raw(MAKE_GUID_PTR(SHIM_LOCK), u"ShimRetainProtocol", &value, sizeof(value), 0); |
| 129 |
} |