| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdalign.h> |
| 4 |
|
| 5 |
#include "bcd.h" |
| 6 |
#include "efi-string.h" |
| 7 |
|
| 8 |
enum { |
| 9 |
SIG_BASE_BLOCK = 1718052210, /* regf */ |
| 10 |
SIG_KEY = 27502, /* nk */ |
| 11 |
SIG_SUBKEY_FAST = 26220, /* lf */ |
| 12 |
SIG_KEY_VALUE = 27510, /* vk */ |
| 13 |
}; |
| 14 |
|
| 15 |
enum { |
| 16 |
REG_SZ = 1, |
| 17 |
REG_MULTI_SZ = 7, |
| 18 |
}; |
| 19 |
|
| 20 |
/* These structs contain a lot more members than we care for. They have all |
| 21 |
* been squashed into _padN for our convenience. */ |
| 22 |
|
| 23 |
typedef struct { |
| 24 |
uint32_t sig; |
| 25 |
uint32_t primary_seqnum; |
| 26 |
uint32_t secondary_seqnum; |
| 27 |
uint64_t _pad1; |
| 28 |
uint32_t version_major; |
| 29 |
uint32_t version_minor; |
| 30 |
uint32_t type; |
| 31 |
uint32_t _pad2; |
| 32 |
uint32_t root_cell_offset; |
| 33 |
uint64_t _pad3[507]; |
| 34 |
} _packed_ BaseBlock; |
| 35 |
assert_cc(sizeof(BaseBlock) == 4096); |
| 36 |
assert_cc(offsetof(BaseBlock, sig) == 0); |
| 37 |
assert_cc(offsetof(BaseBlock, primary_seqnum) == 4); |
| 38 |
assert_cc(offsetof(BaseBlock, secondary_seqnum) == 8); |
| 39 |
assert_cc(offsetof(BaseBlock, version_major) == 20); |
| 40 |
assert_cc(offsetof(BaseBlock, version_minor) == 24); |
| 41 |
assert_cc(offsetof(BaseBlock, type) == 28); |
| 42 |
assert_cc(offsetof(BaseBlock, root_cell_offset) == 36); |
| 43 |
|
| 44 |
/* All offsets are relative to the base block and technically point to a hive |
| 45 |
* cell struct. But for our use case we don't need to bother about that one, |
| 46 |
* so skip over the cell_size uint32_t. */ |
| 47 |
#define HIVE_CELL_OFFSET (sizeof(BaseBlock) + 4) |
| 48 |
|
| 49 |
typedef struct { |
| 50 |
uint16_t sig; |
| 51 |
uint16_t _pad1[13]; |
| 52 |
uint32_t subkeys_offset; |
| 53 |
uint32_t _pad2; |
| 54 |
uint32_t n_key_values; |
| 55 |
uint32_t key_values_offset; |
| 56 |
uint32_t _pad3[7]; |
| 57 |
uint16_t key_name_len; |
| 58 |
uint16_t _pad4; |
| 59 |
char key_name[]; |
| 60 |
} _packed_ Key; |
| 61 |
assert_cc(offsetof(Key, sig) == 0); |
| 62 |
assert_cc(offsetof(Key, subkeys_offset) == 28); |
| 63 |
assert_cc(offsetof(Key, n_key_values) == 36); |
| 64 |
assert_cc(offsetof(Key, key_values_offset) == 40); |
| 65 |
assert_cc(offsetof(Key, key_name_len) == 72); |
| 66 |
assert_cc(offsetof(Key, key_name) == 76); |
| 67 |
|
| 68 |
typedef struct { |
| 69 |
uint16_t sig; |
| 70 |
uint16_t n_entries; |
| 71 |
struct SubkeyFastEntry { |
| 72 |
uint32_t key_offset; |
| 73 |
char name_hint[4]; |
| 74 |
} _packed_ entries[]; |
| 75 |
} _packed_ SubkeyFast; |
| 76 |
assert_cc(offsetof(SubkeyFast, sig) == 0); |
| 77 |
assert_cc(offsetof(SubkeyFast, n_entries) == 2); |
| 78 |
assert_cc(offsetof(SubkeyFast, entries) == 4); |
| 79 |
|
| 80 |
typedef struct { |
| 81 |
uint16_t sig; |
| 82 |
uint16_t name_len; |
| 83 |
uint32_t data_size; |
| 84 |
uint32_t data_offset; |
| 85 |
uint32_t data_type; |
| 86 |
uint32_t _pad; |
| 87 |
char name[]; |
| 88 |
} _packed_ KeyValue; |
| 89 |
assert_cc(offsetof(KeyValue, sig) == 0); |
| 90 |
assert_cc(offsetof(KeyValue, name_len) == 2); |
| 91 |
assert_cc(offsetof(KeyValue, data_size) == 4); |
| 92 |
assert_cc(offsetof(KeyValue, data_offset) == 8); |
| 93 |
assert_cc(offsetof(KeyValue, data_type) == 12); |
| 94 |
assert_cc(offsetof(KeyValue, name) == 20); |
| 95 |
|
| 96 |
#define BAD_OFFSET(offset, len, max) \ |
| 97 |
((uint64_t) (offset) + (len) >= (max)) |
| 98 |
|
| 99 |
#define BAD_STRUCT(type, offset, max) \ |
| 100 |
((uint64_t) (offset) + sizeof(type) >= (max)) |
| 101 |
|
| 102 |
#define BAD_ARRAY(type, array, offset, array_len, max) \ |
| 103 |
((uint64_t) (offset) + offsetof(type, array) + \ |
| 104 |
sizeof((type){}.array[0]) * (uint64_t) (array_len) >= (max)) |
| 105 |
|
| 106 |
static const Key *get_key(const uint8_t *bcd, uint32_t bcd_len, uint32_t offset, const char *name); |
| 107 |
|
| 108 |
static const Key *get_subkey(const uint8_t *bcd, uint32_t bcd_len, uint32_t offset, const char *name) { |
| 109 |
assert(bcd); |
| 110 |
assert(name); |
| 111 |
|
| 112 |
if (BAD_STRUCT(SubkeyFast, offset, bcd_len)) |
| 113 |
return NULL; |
| 114 |
|
| 115 |
const SubkeyFast *subkey = (const SubkeyFast *) (bcd + offset); |
| 116 |
if (subkey->sig != SIG_SUBKEY_FAST) |
| 117 |
return NULL; |
| 118 |
|
| 119 |
if (BAD_ARRAY(SubkeyFast, entries, offset, subkey->n_entries, bcd_len)) |
| 120 |
return NULL; |
| 121 |
|
| 122 |
for (uint16_t i = 0; i < subkey->n_entries; i++) { |
| 123 |
if (!strncaseeq8(name, subkey->entries[i].name_hint, sizeof(subkey->entries[i].name_hint))) |
| 124 |
continue; |
| 125 |
|
| 126 |
const Key *key = get_key(bcd, bcd_len, subkey->entries[i].key_offset, name); |
| 127 |
if (key) |
| 128 |
return key; |
| 129 |
} |
| 130 |
|
| 131 |
return NULL; |
| 132 |
} |
| 133 |
|
| 134 |
/* We use NUL as registry path separators for convenience. To start from the root, begin |
| 135 |
* name with a NUL. Name must end with two NUL. The lookup depth is not restricted, so |
| 136 |
* name must be properly validated before calling get_key(). */ |
| 137 |
static const Key *get_key(const uint8_t *bcd, uint32_t bcd_len, uint32_t offset, const char *name) { |
| 138 |
assert(bcd); |
| 139 |
assert(name); |
| 140 |
|
| 141 |
if (BAD_STRUCT(Key, offset, bcd_len)) |
| 142 |
return NULL; |
| 143 |
|
| 144 |
const Key *key = (const Key *) (bcd + offset); |
| 145 |
if (key->sig != SIG_KEY) |
| 146 |
return NULL; |
| 147 |
|
| 148 |
if (BAD_ARRAY(Key, key_name, offset, key->key_name_len, bcd_len)) |
| 149 |
return NULL; |
| 150 |
|
| 151 |
if (*name) { |
| 152 |
if (strncaseeq8(name, key->key_name, key->key_name_len) && strlen8(name) == key->key_name_len) |
| 153 |
name += key->key_name_len; |
| 154 |
else |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
name++; |
| 159 |
return *name ? get_subkey(bcd, bcd_len, key->subkeys_offset, name) : key; |
| 160 |
} |
| 161 |
|
| 162 |
static const KeyValue *get_key_value(const uint8_t *bcd, uint32_t bcd_len, const Key *key, const char *name) { |
| 163 |
assert(bcd); |
| 164 |
assert(key); |
| 165 |
assert(name); |
| 166 |
|
| 167 |
if (key->n_key_values == 0) |
| 168 |
return NULL; |
| 169 |
|
| 170 |
if (BAD_OFFSET(key->key_values_offset, sizeof(uint32_t) * (uint64_t) key->n_key_values, bcd_len) || |
| 171 |
(uintptr_t) (bcd + key->key_values_offset) % alignof(uint32_t) != 0) |
| 172 |
return NULL; |
| 173 |
|
| 174 |
const uint32_t *key_value_list = (const uint32_t *) (bcd + key->key_values_offset); |
| 175 |
for (uint32_t i = 0; i < key->n_key_values; i++) { |
| 176 |
uint32_t offset = *(key_value_list + i); |
| 177 |
|
| 178 |
if (BAD_STRUCT(KeyValue, offset, bcd_len)) |
| 179 |
continue; |
| 180 |
|
| 181 |
const KeyValue *kv = (const KeyValue *) (bcd + offset); |
| 182 |
if (kv->sig != SIG_KEY_VALUE) |
| 183 |
continue; |
| 184 |
|
| 185 |
if (BAD_ARRAY(KeyValue, name, offset, kv->name_len, bcd_len)) |
| 186 |
continue; |
| 187 |
|
| 188 |
/* If most significant bit is set, data is stored in data_offset itself, but |
| 189 |
* we are only interested in UTF16 strings. The only strings that could fit |
| 190 |
* would have just one char in it, so let's not bother with this. */ |
| 191 |
if (FLAGS_SET(kv->data_size, UINT32_C(1) << 31)) |
| 192 |
continue; |
| 193 |
|
| 194 |
if (BAD_OFFSET(kv->data_offset, kv->data_size, bcd_len)) |
| 195 |
continue; |
| 196 |
|
| 197 |
if (strncaseeq8(name, kv->name, kv->name_len) && strlen8(name) == kv->name_len) |
| 198 |
return kv; |
| 199 |
} |
| 200 |
|
| 201 |
return NULL; |
| 202 |
} |
| 203 |
|
| 204 |
/* The BCD store is really just a regular windows registry hive with a rather cryptic internal |
| 205 |
* key structure. On a running system it gets mounted to HKEY_LOCAL_MACHINE\BCD00000000. |
| 206 |
* |
| 207 |
* Of interest to us are these two keys: |
| 208 |
* - \Objects\{bootmgr}\Elements\24000001 |
| 209 |
* This key is the "displayorder" property and contains a value of type REG_MULTI_SZ |
| 210 |
* with the name "Element" that holds a {GUID} list (UTF16, NUL-separated). |
| 211 |
* - \Objects\{GUID}\Elements\12000004 |
| 212 |
* This key is the "description" property and contains a value of type REG_SZ with the |
| 213 |
* name "Element" that holds a NUL-terminated UTF16 string. |
| 214 |
* |
| 215 |
* The GUIDs and properties are as reported by "bcdedit.exe /v". |
| 216 |
* |
| 217 |
* To get a title for the BCD store we first look at the displayorder property of {bootmgr} |
| 218 |
* (it always has the GUID 9dea862c-5cdd-4e70-acc1-f32b344d4795). If it contains more than |
| 219 |
* one GUID, the BCD is multi-boot and we stop looking. Otherwise we take that GUID, look it |
| 220 |
* up, and return its description property. */ |
| 221 |
char16_t *get_bcd_title(uint8_t *bcd, size_t bcd_len) { |
| 222 |
assert(bcd); |
| 223 |
|
| 224 |
if (HIVE_CELL_OFFSET >= bcd_len) |
| 225 |
return NULL; |
| 226 |
|
| 227 |
BaseBlock *base_block = (BaseBlock *) bcd; |
| 228 |
if (base_block->sig != SIG_BASE_BLOCK || |
| 229 |
base_block->version_major != 1 || |
| 230 |
base_block->version_minor != 3 || |
| 231 |
base_block->type != 0 || |
| 232 |
base_block->primary_seqnum != base_block->secondary_seqnum) |
| 233 |
return NULL; |
| 234 |
|
| 235 |
bcd += HIVE_CELL_OFFSET; |
| 236 |
bcd_len -= HIVE_CELL_OFFSET; |
| 237 |
|
| 238 |
const Key *objects_key = get_key(bcd, bcd_len, base_block->root_cell_offset, "\0Objects\0"); |
| 239 |
if (!objects_key) |
| 240 |
return NULL; |
| 241 |
|
| 242 |
const Key *displayorder_key = get_subkey( |
| 243 |
bcd, |
| 244 |
bcd_len, |
| 245 |
objects_key->subkeys_offset, |
| 246 |
"{9dea862c-5cdd-4e70-acc1-f32b344d4795}\0Elements\00024000001\0"); |
| 247 |
if (!displayorder_key) |
| 248 |
return NULL; |
| 249 |
|
| 250 |
const KeyValue *displayorder_value = get_key_value(bcd, bcd_len, displayorder_key, "Element"); |
| 251 |
if (!displayorder_value) |
| 252 |
return NULL; |
| 253 |
|
| 254 |
char order_guid[sizeof("{00000000-0000-0000-0000-000000000000}\0")]; |
| 255 |
if (displayorder_value->data_type != REG_MULTI_SZ || |
| 256 |
displayorder_value->data_size != sizeof(char16_t[sizeof(order_guid)]) || |
| 257 |
(uintptr_t) (bcd + displayorder_value->data_offset) % alignof(char16_t) != 0) |
| 258 |
/* BCD is multi-boot. */ |
| 259 |
return NULL; |
| 260 |
|
| 261 |
/* Keys are stored as ASCII in registry hives if the data fits (and GUIDS always should). */ |
| 262 |
char16_t *order_guid_utf16 = (char16_t *) (bcd + displayorder_value->data_offset); |
| 263 |
for (size_t i = 0; i < sizeof(order_guid) - 2; i++) { |
| 264 |
char16_t c = order_guid_utf16[i]; |
| 265 |
switch (c) { |
| 266 |
case '-': |
| 267 |
case '{': |
| 268 |
case '}': |
| 269 |
case '0' ... '9': |
| 270 |
case 'a' ... 'f': |
| 271 |
case 'A' ... 'F': |
| 272 |
order_guid[i] = c; |
| 273 |
break; |
| 274 |
default: |
| 275 |
/* Not a valid GUID. */ |
| 276 |
return NULL; |
| 277 |
} |
| 278 |
} |
| 279 |
/* Our functions expect the lookup key to be double-derminated. */ |
| 280 |
order_guid[sizeof(order_guid) - 2] = '\0'; |
| 281 |
order_guid[sizeof(order_guid) - 1] = '\0'; |
| 282 |
|
| 283 |
const Key *default_key = get_subkey(bcd, bcd_len, objects_key->subkeys_offset, order_guid); |
| 284 |
if (!default_key) |
| 285 |
return NULL; |
| 286 |
|
| 287 |
const Key *description_key = get_subkey( |
| 288 |
bcd, bcd_len, default_key->subkeys_offset, "Elements\00012000004\0"); |
| 289 |
if (!description_key) |
| 290 |
return NULL; |
| 291 |
|
| 292 |
const KeyValue *description_value = get_key_value(bcd, bcd_len, description_key, "Element"); |
| 293 |
if (!description_value) |
| 294 |
return NULL; |
| 295 |
|
| 296 |
if (description_value->data_type != REG_SZ || |
| 297 |
description_value->data_size < sizeof(char16_t) || |
| 298 |
description_value->data_size % sizeof(char16_t) != 0 || |
| 299 |
(uintptr_t) (bcd + description_value->data_offset) % alignof(char16_t)) |
| 300 |
return NULL; |
| 301 |
|
| 302 |
/* The data should already be NUL-terminated. */ |
| 303 |
char16_t *title = (char16_t *) (bcd + description_value->data_offset); |
| 304 |
title[description_value->data_size / sizeof(char16_t) - 1] = '\0'; |
| 305 |
return title; |
| 306 |
} |