]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/boot/efi/stub.c
stub: handle random seed like sd-boot does
[thirdparty/systemd.git] / src / boot / efi / stub.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
0fa2cac4
KS
2
3#include <efi.h>
4#include <efilib.h>
5
845707aa 6#include "cpio.h"
33bc9b75 7#include "devicetree.h"
8110e144 8#include "disk.h"
37fa3690 9#include "graphics.h"
0fa2cac4 10#include "linux.h"
d4cbada2 11#include "measure.h"
0a1d8ac7 12#include "part-discovery.h"
d4cbada2 13#include "pe.h"
0a1d8ac7 14#include "random-seed.h"
ce0f078f 15#include "secure-boot.h"
cf0fbc49 16#include "splash.h"
aa323c09 17#include "tpm-pcr.h"
cf0fbc49 18#include "util.h"
0fa2cac4
KS
19
20/* magic string to find in the binary image */
2ccd5986 21_used_ _section_(".sdmagic") static const char magic[] = "#### LoaderInfo: systemd-stub " GIT_VERSION " ####";
0fa2cac4 22
845707aa
LP
23static EFI_STATUS combine_initrd(
24 EFI_PHYSICAL_ADDRESS initrd_base, UINTN initrd_size,
23002b45 25 const void * const extra_initrds[], const size_t extra_initrd_sizes[], size_t n_extra_initrds,
09173c91 26 Pages *ret_initr_pages, UINTN *ret_initrd_size) {
845707aa 27
845707aa
LP
28 UINTN n;
29
09173c91 30 assert(ret_initr_pages);
845707aa
LP
31 assert(ret_initrd_size);
32
f3b6f333 33 /* Combines four initrds into one, by simple concatenation in memory */
845707aa 34
b4e7df4a 35 n = ALIGN4(initrd_size); /* main initrd might not be padded yet */
845707aa 36
23002b45
LP
37 for (size_t i = 0; i < n_extra_initrds; i++) {
38 if (!extra_initrds[i])
39 continue;
f3b6f333 40
23002b45 41 if (n > UINTN_MAX - extra_initrd_sizes[i])
845707aa
LP
42 return EFI_OUT_OF_RESOURCES;
43
23002b45 44 n += extra_initrd_sizes[i];
845707aa
LP
45 }
46
09173c91 47 _cleanup_pages_ Pages pages = xmalloc_pages(
845707aa
LP
48 AllocateMaxAddress,
49 EfiLoaderData,
50 EFI_SIZE_TO_PAGES(n),
09173c91
JJ
51 UINT32_MAX /* Below 4G boundary. */);
52 uint8_t *p = PHYSICAL_ADDRESS_TO_POINTER(pages.addr);
845707aa
LP
53 if (initrd_base != 0) {
54 UINTN pad;
55
56 /* Order matters, the real initrd must come first, since it might include microcode updates
57 * which the kernel only looks for in the first cpio archive */
515581d6 58 p = mempcpy(p, PHYSICAL_ADDRESS_TO_POINTER(initrd_base), initrd_size);
845707aa 59
b4e7df4a 60 pad = ALIGN4(initrd_size) - initrd_size;
845707aa 61 if (pad > 0) {
bbc1f2ea 62 memset(p, 0, pad);
845707aa
LP
63 p += pad;
64 }
65 }
66
23002b45
LP
67 for (size_t i = 0; i < n_extra_initrds; i++) {
68 if (!extra_initrds[i])
69 continue;
70
71 p = mempcpy(p, extra_initrds[i], extra_initrd_sizes[i]);
72 }
845707aa 73
09173c91 74 assert(PHYSICAL_ADDRESS_TO_POINTER(pages.addr + n) == p);
845707aa 75
09173c91 76 *ret_initr_pages = pages;
845707aa 77 *ret_initrd_size = n;
09173c91 78 pages.n_pages = 0;
845707aa
LP
79
80 return EFI_SUCCESS;
81}
82
b30a43df 83static void export_variables(EFI_LOADED_IMAGE_PROTOCOL *loaded_image) {
46d33672
LP
84 static const uint64_t stub_features =
85 EFI_STUB_FEATURE_REPORT_BOOT_PARTITION | /* We set LoaderDevicePartUUID */
86 EFI_STUB_FEATURE_PICK_UP_CREDENTIALS | /* We pick up credentials from the boot partition */
87 EFI_STUB_FEATURE_PICK_UP_SYSEXTS | /* We pick up system extensions from the boot partition */
88 EFI_STUB_FEATURE_THREE_PCRS | /* We can measure kernel image, parameters and sysext */
0a1d8ac7 89 EFI_STUB_FEATURE_RANDOM_SEED | /* We pass a random seed to the kernel */
46d33672
LP
90 0;
91
3639d1b0 92 char16_t uuid[37];
5a186322
LP
93
94 assert(loaded_image);
95
96 /* Export the device path this image is started from, if it's not set yet */
97 if (efivar_get_raw(LOADER_GUID, L"LoaderDevicePartUUID", NULL, NULL) != EFI_SUCCESS)
98 if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS)
99 efivar_set(LOADER_GUID, L"LoaderDevicePartUUID", uuid, 0);
100
101 /* If LoaderImageIdentifier is not set, assume the image with this stub was loaded directly from the
102 * UEFI firmware without any boot loader, and hence set the LoaderImageIdentifier ourselves. Note
103 * that some boot chain loaders neither set LoaderImageIdentifier nor make FilePath available to us,
104 * in which case there's simple nothing to set for us. (The UEFI spec doesn't really say who's wrong
105 * here, i.e. whether FilePath may be NULL or not, hence handle this gracefully and check if FilePath
106 * is non-NULL explicitly.) */
107 if (efivar_get_raw(LOADER_GUID, L"LoaderImageIdentifier", NULL, NULL) != EFI_SUCCESS &&
108 loaded_image->FilePath) {
3639d1b0 109 _cleanup_free_ char16_t *s = NULL;
616a80fe 110 if (device_path_to_str(loaded_image->FilePath, &s) == EFI_SUCCESS)
9f048123 111 efivar_set(LOADER_GUID, L"LoaderImageIdentifier", s, 0);
5a186322
LP
112 }
113
114 /* if LoaderFirmwareInfo is not set, let's set it */
115 if (efivar_get_raw(LOADER_GUID, L"LoaderFirmwareInfo", NULL, NULL) != EFI_SUCCESS) {
3639d1b0 116 _cleanup_free_ char16_t *s = NULL;
ec4106af 117 s = xpool_print(L"%s %u.%02u", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
0a15a824 118 efivar_set(LOADER_GUID, L"LoaderFirmwareInfo", s, 0);
5a186322
LP
119 }
120
121 /* ditto for LoaderFirmwareType */
122 if (efivar_get_raw(LOADER_GUID, L"LoaderFirmwareType", NULL, NULL) != EFI_SUCCESS) {
3639d1b0 123 _cleanup_free_ char16_t *s = NULL;
ec4106af 124 s = xpool_print(L"UEFI %u.%02u", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
0a15a824 125 efivar_set(LOADER_GUID, L"LoaderFirmwareType", s, 0);
5a186322
LP
126 }
127
46d33672 128
24120e40
LP
129 /* add StubInfo (this is one is owned by the stub, hence we unconditionally override this with our
130 * own data) */
131 (void) efivar_set(LOADER_GUID, L"StubInfo", L"systemd-stub " GIT_VERSION, 0);
46d33672
LP
132
133 (void) efivar_set_uint64_le(LOADER_GUID, L"StubFeatures", stub_features, 0);
5a186322
LP
134}
135
0fa2cac4 136EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
23002b45
LP
137 _cleanup_free_ void *credential_initrd = NULL, *global_credential_initrd = NULL, *sysext_initrd = NULL, *pcrsig_initrd = NULL, *pcrpkey_initrd = NULL;
138 UINTN credential_initrd_size = 0, global_credential_initrd_size = 0, sysext_initrd_size = 0, pcrsig_initrd_size = 0, pcrpkey_initrd_size = 0;
33bc9b75 139 UINTN cmdline_len = 0, linux_size, initrd_size, dt_size;
33bc9b75
MR
140 EFI_PHYSICAL_ADDRESS linux_base, initrd_base, dt_base;
141 _cleanup_(devicetree_cleanup) struct devicetree_state dt_state = {};
b30a43df 142 EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
6017eee9 143 UINTN addrs[_UNIFIED_SECTION_MAX] = {}, szs[_UNIFIED_SECTION_MAX] = {};
07d0fde4
JJ
144 char *cmdline = NULL;
145 _cleanup_free_ char *cmdline_owned = NULL;
72c97c19 146 int sections_measured = -1, parameters_measured = -1;
de7ad6d4 147 bool sysext_measured = false, m;
0a1d8ac7 148 uint64_t loader_features = 0;
0fa2cac4
KS
149 EFI_STATUS err;
150
151 InitializeLib(image, sys_table);
948d085e
JJ
152 debug_hook(L"systemd-stub");
153 /* Uncomment the next line if you need to wait for debugger. */
154 // debug_break();
0fa2cac4 155
12f32748
JJ
156 err = BS->OpenProtocol(
157 image,
158 &LoadedImageProtocol,
159 (void **)&loaded_image,
160 image,
161 NULL,
162 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2a5e4fe4 163 if (err != EFI_SUCCESS)
8aba0eec 164 return log_error_status_stall(err, L"Error getting a LoadedImageProtocol handle: %r", err);
0fa2cac4 165
0a1d8ac7
JD
166 if (efivar_get_uint64_le(LOADER_GUID, L"LoaderFeatures", &loader_features) != EFI_SUCCESS ||
167 !FLAGS_SET(loader_features, EFI_LOADER_FEATURE_RANDOM_SEED)) {
168 _cleanup_(file_closep) EFI_FILE *esp_dir = NULL;
169
170 err = partition_open(ESP_GUID, loaded_image->DeviceHandle, NULL, &esp_dir);
171 if (err == EFI_SUCCESS) /* Non-fatal on failure, so that we still boot without it. */
172 (void) process_random_seed(esp_dir);
173 }
174
6017eee9
LP
175 err = pe_memory_locate_sections(loaded_image->ImageBase, unified_sections, addrs, szs);
176 if (err != EFI_SUCCESS || szs[UNIFIED_SECTION_LINUX] == 0) {
2a5e4fe4 177 if (err == EFI_SUCCESS)
65ff3d26 178 err = EFI_NOT_FOUND;
8aba0eec 179 return log_error_status_stall(err, L"Unable to locate embedded .linux section: %r", err);
65ff3d26 180 }
0fa2cac4 181
72c97c19
LP
182 /* Measure all "payload" of this PE image into a separate PCR (i.e. where nothing else is written
183 * into so far), so that we have one PCR that we can nicely write policies against because it
184 * contains all static data of this image, and thus can be easily be pre-calculated. */
6017eee9 185 for (UnifiedSection section = 0; section < _UNIFIED_SECTION_MAX; section++) {
df7ee6f8
LP
186
187 if (!unified_section_measure(section)) /* shall not measure? */
188 continue;
72c97c19
LP
189
190 if (szs[section] == 0) /* not found */
191 continue;
192
df7ee6f8
LP
193 m = false;
194
72c97c19
LP
195 /* First measure the name of the section */
196 (void) tpm_log_event_ascii(
197 TPM_PCR_INDEX_KERNEL_IMAGE,
6017eee9
LP
198 POINTER_TO_PHYSICAL_ADDRESS(unified_sections[section]),
199 strsize8(unified_sections[section]), /* including NUL byte */
200 unified_sections[section],
72c97c19
LP
201 &m);
202
203 sections_measured = sections_measured < 0 ? m : (sections_measured && m);
204
205 /* Then measure the data of the section */
206 (void) tpm_log_event_ascii(
207 TPM_PCR_INDEX_KERNEL_IMAGE,
208 POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[section],
209 szs[section],
6017eee9 210 unified_sections[section],
72c97c19
LP
211 &m);
212
213 sections_measured = sections_measured < 0 ? m : (sections_measured && m);
214 }
215
216 /* After we are done, set an EFI variable that tells userspace this was done successfully, and encode
217 * in it which PCR was used. */
218 if (sections_measured > 0)
219 (void) efivar_set_uint_string(LOADER_GUID, L"StubPcrKernelImage", TPM_PCR_INDEX_KERNEL_IMAGE, 0);
220
94b81afb 221 /* Show splash screen as early as possible */
957dfcc9 222 graphics_splash((const uint8_t*) loaded_image->ImageBase + addrs[UNIFIED_SECTION_SPLASH], szs[UNIFIED_SECTION_SPLASH]);
94b81afb 223
6017eee9
LP
224 if (szs[UNIFIED_SECTION_CMDLINE] > 0) {
225 cmdline = (char *) loaded_image->ImageBase + addrs[UNIFIED_SECTION_CMDLINE];
226 cmdline_len = szs[UNIFIED_SECTION_CMDLINE];
e41d3d89 227 }
0fa2cac4 228
03d5449f 229 /* if we are not in secure boot mode, or none was provided, accept a custom command line and replace
5c19169f 230 * the built-in one. We also do a superficial check whether first character of passed command line
03d5449f
LP
231 * is printable character (for compat with some Dell systems which fill in garbage?). */
232 if ((!secure_boot_enabled() || cmdline_len == 0) &&
233 loaded_image->LoadOptionsSize > 0 &&
234 ((char16_t *) loaded_image->LoadOptions)[0] > 0x1F) {
07d0fde4 235 cmdline_len = (loaded_image->LoadOptionsSize / sizeof(char16_t)) * sizeof(char);
03d5449f 236 cmdline = cmdline_owned = xnew(char, cmdline_len);
9f048123 237
03d5449f
LP
238 for (UINTN i = 0; i < cmdline_len; i++) {
239 char16_t c = ((char16_t *) loaded_image->LoadOptions)[i];
240 cmdline[i] = c > 0x1F && c < 0x7F ? c : ' '; /* convert non-printable and non_ASCII characters to spaces. */
241 }
92ed3bb4 242
e6e24af5
LP
243 /* Let's measure the passed kernel command line into the TPM. Note that this possibly
244 * duplicates what we already did in the boot menu, if that was already used. However, since
245 * we want the boot menu to support an EFI binary, and want to this stub to be usable from
246 * any boot menu, let's measure things anyway. */
599fe002
LP
247 m = false;
248 (void) tpm_log_load_options(loaded_image->LoadOptions, &m);
249 parameters_measured = m;
0fa2cac4
KS
250 }
251
5a186322 252 export_variables(loaded_image);
1aa15def 253
599fe002
LP
254 if (pack_cpio(loaded_image,
255 NULL,
256 L".cred",
257 ".extra/credentials",
258 /* dir_mode= */ 0500,
259 /* access_mode= */ 0400,
260 /* tpm_pcr= */ (uint32_t[]) { TPM_PCR_INDEX_KERNEL_PARAMETERS, TPM_PCR_INDEX_KERNEL_PARAMETERS_COMPAT },
261 /* n_tpm_pcr= */ 2,
262 L"Credentials initrd",
263 &credential_initrd,
264 &credential_initrd_size,
265 &m) == EFI_SUCCESS)
266 parameters_measured = parameters_measured < 0 ? m : (parameters_measured && m);
267
268 if (pack_cpio(loaded_image,
269 L"\\loader\\credentials",
270 L".cred",
271 ".extra/global_credentials",
272 /* dir_mode= */ 0500,
273 /* access_mode= */ 0400,
274 /* tpm_pcr= */ (uint32_t[]) { TPM_PCR_INDEX_KERNEL_PARAMETERS, TPM_PCR_INDEX_KERNEL_PARAMETERS_COMPAT },
275 /* n_tpm_pcr= */ 2,
276 L"Global credentials initrd",
277 &global_credential_initrd,
278 &global_credential_initrd_size,
279 &m) == EFI_SUCCESS)
280 parameters_measured = parameters_measured < 0 ? m : (parameters_measured && m);
f3b6f333 281
de7ad6d4
LP
282 if (pack_cpio(loaded_image,
283 NULL,
284 L".raw",
285 ".extra/sysext",
286 /* dir_mode= */ 0555,
287 /* access_mode= */ 0444,
288 /* tpm_pcr= */ (uint32_t[]) { TPM_PCR_INDEX_INITRD_SYSEXTS },
289 /* n_tpm_pcr= */ 1,
290 L"System extension initrd",
291 &sysext_initrd,
292 &sysext_initrd_size,
293 &m) == EFI_SUCCESS)
294 sysext_measured = m;
599fe002
LP
295
296 if (parameters_measured > 0)
297 (void) efivar_set_uint_string(LOADER_GUID, L"StubPcrKernelParameters", TPM_PCR_INDEX_KERNEL_PARAMETERS, 0);
de7ad6d4
LP
298 if (sysext_measured)
299 (void) efivar_set_uint_string(LOADER_GUID, L"StubPcrInitRDSysExts", TPM_PCR_INDEX_INITRD_SYSEXTS, 0);
845707aa 300
23002b45
LP
301 /* If the PCR signature was embedded in the PE image, then let's wrap it in a cpio and also pass it
302 * to the kernel, so that it can be read from /.extra/tpm2-pcr-signature.json. Note that this section
303 * is not measured, neither as raw section (see above), nor as cpio (here), because it is the
5dcb9c3c 304 * signature of expected PCR values, i.e. its input are PCR measurements, and hence it shouldn't
23002b45
LP
305 * itself be input for PCR measurements. */
306 if (szs[UNIFIED_SECTION_PCRSIG] > 0)
307 (void) pack_cpio_literal(
308 (uint8_t*) loaded_image->ImageBase + addrs[UNIFIED_SECTION_PCRSIG],
309 szs[UNIFIED_SECTION_PCRSIG],
310 ".extra",
311 L"tpm2-pcr-signature.json",
312 /* dir_mode= */ 0555,
313 /* access_mode= */ 0444,
314 /* tpm_pcr= */ NULL,
315 /* n_tpm_pcr= */ 0,
316 /* tpm_description= */ NULL,
317 &pcrsig_initrd,
318 &pcrsig_initrd_size,
319 /* ret_measured= */ NULL);
320
321 /* If the public key used for the PCR signatures was embedded in the PE image, then let's wrap it in
322 * a cpio and also pass it to the kernel, so that it can be read from
323 * /.extra/tpm2-pcr-public-key.pem. This section is already measure above, hence we won't measure the
324 * cpio. */
325 if (szs[UNIFIED_SECTION_PCRPKEY] > 0)
326 (void) pack_cpio_literal(
327 (uint8_t*) loaded_image->ImageBase + addrs[UNIFIED_SECTION_PCRPKEY],
328 szs[UNIFIED_SECTION_PCRPKEY],
329 ".extra",
330 L"tpm2-pcr-public-key.pem",
331 /* dir_mode= */ 0555,
332 /* access_mode= */ 0444,
333 /* tpm_pcr= */ NULL,
334 /* n_tpm_pcr= */ 0,
335 /* tpm_description= */ NULL,
336 &pcrpkey_initrd,
337 &pcrpkey_initrd_size,
338 /* ret_measured= */ NULL);
339
6017eee9
LP
340 linux_size = szs[UNIFIED_SECTION_LINUX];
341 linux_base = POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[UNIFIED_SECTION_LINUX];
5b5d365d 342
6017eee9
LP
343 initrd_size = szs[UNIFIED_SECTION_INITRD];
344 initrd_base = initrd_size != 0 ? POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[UNIFIED_SECTION_INITRD] : 0;
37fa3690 345
6017eee9
LP
346 dt_size = szs[UNIFIED_SECTION_DTB];
347 dt_base = dt_size != 0 ? POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[UNIFIED_SECTION_DTB] : 0;
33bc9b75 348
09173c91 349 _cleanup_pages_ Pages initrd_pages = {};
64650de7 350 if (credential_initrd || global_credential_initrd || sysext_initrd || pcrsig_initrd || pcrpkey_initrd) {
845707aa
LP
351 /* If we have generated initrds dynamically, let's combine them with the built-in initrd. */
352 err = combine_initrd(
353 initrd_base, initrd_size,
23002b45
LP
354 (const void*const[]) {
355 credential_initrd,
356 global_credential_initrd,
357 sysext_initrd,
358 pcrsig_initrd,
359 pcrpkey_initrd,
360 },
361 (const size_t[]) {
362 credential_initrd_size,
363 global_credential_initrd_size,
364 sysext_initrd_size,
365 pcrsig_initrd_size,
366 pcrpkey_initrd_size,
367 },
368 5,
09173c91 369 &initrd_pages, &initrd_size);
2a5e4fe4 370 if (err != EFI_SUCCESS)
845707aa
LP
371 return err;
372
09173c91
JJ
373 initrd_base = initrd_pages.addr;
374
845707aa 375 /* Given these might be large let's free them explicitly, quickly. */
f3b6f333
AV
376 credential_initrd = mfree(credential_initrd);
377 global_credential_initrd = mfree(global_credential_initrd);
378 sysext_initrd = mfree(sysext_initrd);
23002b45
LP
379 pcrsig_initrd = mfree(pcrsig_initrd);
380 pcrpkey_initrd = mfree(pcrpkey_initrd);
845707aa 381 }
0fa2cac4 382
33bc9b75
MR
383 if (dt_size > 0) {
384 err = devicetree_install_from_memory(
385 &dt_state, PHYSICAL_ADDRESS_TO_POINTER(dt_base), dt_size);
2a5e4fe4 386 if (err != EFI_SUCCESS)
33bc9b75
MR
387 log_error_stall(L"Error loading embedded devicetree: %r", err);
388 }
389
a6089431 390 err = linux_exec(image, cmdline, cmdline_len,
dc467928 391 PHYSICAL_ADDRESS_TO_POINTER(linux_base), linux_size,
a6089431 392 PHYSICAL_ADDRESS_TO_POINTER(initrd_base), initrd_size);
e5a1b8f9 393 graphics_mode(false);
a529d818 394 return err;
0fa2cac4 395}