| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "efi-firmware.h" |
| 4 |
#include "efi-string.h" |
| 5 |
|
| 6 |
static bool efifw_validate_header( |
| 7 |
const void *blob, |
| 8 |
size_t blob_len, |
| 9 |
const char **ret_fwid, |
| 10 |
const char **ret_payload) { |
| 11 |
|
| 12 |
if ((uintptr_t) blob % alignof(EfiFwHeader) != 0) |
| 13 |
return false; |
| 14 |
|
| 15 |
size_t base_sz = offsetof(EfiFwHeader, payload); |
| 16 |
|
| 17 |
/* at least the base size of the header must be in memory */ |
| 18 |
if (blob_len < base_sz) |
| 19 |
return false; |
| 20 |
|
| 21 |
const EfiFwHeader *fw_header = ASSERT_PTR(blob); |
| 22 |
|
| 23 |
if (fw_header->magic != FWHEADERMAGIC) |
| 24 |
return false; |
| 25 |
|
| 26 |
uint32_t header_len = fw_header->header_len; |
| 27 |
|
| 28 |
/* header_len must not be malformed */ |
| 29 |
if (header_len < base_sz) |
| 30 |
return false; |
| 31 |
|
| 32 |
uint32_t fwid_len = fw_header->fwid_len; |
| 33 |
uint32_t payload_len = fw_header->payload_len; |
| 34 |
size_t total_computed_size; |
| 35 |
|
| 36 |
/* check for unusually large values of payload_len, header_len or fwid_len */ |
| 37 |
if (!ADD_SAFE(&total_computed_size, header_len, fwid_len) || |
| 38 |
!ADD_SAFE(&total_computed_size, total_computed_size, payload_len)) |
| 39 |
return false; |
| 40 |
|
| 41 |
/* see if entire size of the base header is present in memory */ |
| 42 |
if (blob_len < total_computed_size) |
| 43 |
return false; |
| 44 |
|
| 45 |
const char *fwid = (const char*) blob + header_len; |
| 46 |
const char *payload = fwid + fwid_len; |
| 47 |
|
| 48 |
/* check that fwid points to a NUL terminated string */ |
| 49 |
if (memchr(fwid, 0, fwid_len) != fwid + fwid_len - 1) |
| 50 |
return false; |
| 51 |
|
| 52 |
if (ret_fwid) |
| 53 |
*ret_fwid = fwid; |
| 54 |
|
| 55 |
if (ret_payload) |
| 56 |
*ret_payload = payload; |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
EFI_STATUS efi_firmware_match_by_fwid( |
| 61 |
const void *blob, |
| 62 |
size_t blob_len, |
| 63 |
const char *fwid) { |
| 64 |
|
| 65 |
assert(blob); |
| 66 |
assert(fwid); |
| 67 |
|
| 68 |
const char *blob_fwid; |
| 69 |
if (!efifw_validate_header(blob, blob_len, &blob_fwid, NULL)) |
| 70 |
return EFI_INVALID_PARAMETER; |
| 71 |
|
| 72 |
return streq8(blob_fwid, fwid) ? EFI_SUCCESS : EFI_NOT_FOUND; |
| 73 |
} |