1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "cpio.h"
#include "device-path-util.h"
#include "devicetree.h"
#include "efivars.h"
#include "export-vars.h"
#include "graphics.h"
#include "iovec-util-fundamental.h"
#include "linux.h"
#include "measure.h"
#include "memory-util-fundamental.h"
#include "part-discovery.h"
#include "pe.h"
#include "proto/shell-parameters.h"
#include "random-seed.h"
#include "sbat.h"
#include "secure-boot.h"
#include "shim.h"
#include "splash.h"
#include "tpm2-pcr.h"
#include "uki.h"
#include "util.h"
#include "version.h"
#include "vmm.h"
/* The list of initrds we combine into one, in the order we want to merge them */
enum {
/* The first two are part of the PE binary */
INITRD_UCODE,
INITRD_BASE,
/* The rest are dynamically generated, and hence in dynamic memory */
_INITRD_DYNAMIC_FIRST,
INITRD_CREDENTIAL = _INITRD_DYNAMIC_FIRST,
INITRD_GLOBAL_CREDENTIAL,
INITRD_SYSEXT,
INITRD_CONFEXT,
INITRD_PCRSIG,
INITRD_PCRPKEY,
_INITRD_MAX,
};
/* magic string to find in the binary image */
DECLARE_NOALLOC_SECTION(".sdmagic", "#### LoaderInfo: systemd-stub " GIT_VERSION " ####");
DECLARE_SBAT(SBAT_STUB_SECTION_TEXT);
static char16_t* pe_section_to_str16(
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const PeSectionVector *section) {
assert(loaded_image);
assert(section);
if (!PE_SECTION_VECTOR_IS_SET(section))
return NULL;
return xstrn8_to_16((const char *) loaded_image->ImageBase + section->memory_offset, section->size);
}
static char *pe_section_to_str8(
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const PeSectionVector *section) {
assert(loaded_image);
assert(section);
if (!PE_SECTION_VECTOR_IS_SET(section))
return NULL;
return xstrndup8((const char *)loaded_image->ImageBase + section->memory_offset, section->size);
}
static void combine_measured_flag(int *value, int measured) {
assert(value);
/* Combine the "measured" flag in a sensible way: if we haven't measured anything yet, the first
* write is taken as is. Later writes can only turn off the flag, never on again. Or in other words,
* we eventually want to return true iff we really measured *everything* there was to measure.
*
* Reminder how the "measured" flag actually works:
* > 0 → something was measured
* == 0 → there was something to measure but we didn't (because no TPM or so)
* < 0 → nothing has been submitted for measurement so far
*/
if (measured < 0)
return;
*value = *value < 0 ? measured : *value && measured;
}
/* Combine initrds by concatenation in memory */
static EFI_STATUS combine_initrds(
struct iovec initrds[], size_t n_initrds,
Pages *ret_initrd_pages, size_t *ret_initrd_size) {
size_t n = 0;
assert(initrds || n_initrds == 0);
assert(ret_initrd_pages);
assert(ret_initrd_size);
FOREACH_ARRAY(i, initrds, n_initrds) {
/* some initrds (the ones from UKI sections) need padding, pad all to be safe */
size_t initrd_size = ALIGN4(i->iov_len);
if (n > SIZE_MAX - initrd_size)
return EFI_OUT_OF_RESOURCES;
n += initrd_size;
}
_cleanup_pages_ Pages pages = xmalloc_pages(
AllocateMaxAddress,
EfiLoaderData,
EFI_SIZE_TO_PAGES(n),
UINT32_MAX /* Below 4G boundary. */);
uint8_t *p = PHYSICAL_ADDRESS_TO_POINTER(pages.addr);
FOREACH_ARRAY(i, initrds, n_initrds) {
size_t pad;
p = mempcpy(p, i->iov_base, i->iov_len);
pad = ALIGN4(i->iov_len) - i->iov_len;
if (pad == 0)
continue;
memzero(p, pad);
p += pad;
}
assert(PHYSICAL_ADDRESS_TO_POINTER(pages.addr + n) == p);
*ret_initrd_pages = pages;
*ret_initrd_size = n;
pages.n_pages = 0;
return EFI_SUCCESS;
}
static void export_stub_variables(EFI_LOADED_IMAGE_PROTOCOL *loaded_image) {
static const uint64_t stub_features =
EFI_STUB_FEATURE_REPORT_BOOT_PARTITION | /* We set LoaderDevicePartUUID */
EFI_STUB_FEATURE_PICK_UP_CREDENTIALS | /* We pick up credentials from the boot partition */
EFI_STUB_FEATURE_PICK_UP_SYSEXTS | /* We pick up system extensions from the boot partition */
EFI_STUB_FEATURE_PICK_UP_CONFEXTS | /* We pick up configuration extensions from the boot partition */
EFI_STUB_FEATURE_THREE_PCRS | /* We can measure kernel image, parameters and sysext */
EFI_STUB_FEATURE_RANDOM_SEED | /* We pass a random seed to the kernel */
EFI_STUB_FEATURE_CMDLINE_ADDONS | /* We pick up .cmdline addons */
EFI_STUB_FEATURE_CMDLINE_SMBIOS | /* We support extending kernel cmdline from SMBIOS Type #11 */
EFI_STUB_FEATURE_DEVICETREE_ADDONS | /* We pick up .dtb addons */
0;
assert(loaded_image);
/* add StubInfo (this is one is owned by the stub, hence we unconditionally override this with our
* own data) */
(void) efivar_set_str16(MAKE_GUID_PTR(LOADER), u"StubInfo", u"systemd-stub " GIT_VERSION, 0);
(void) efivar_set_uint64_le(MAKE_GUID_PTR(LOADER), u"StubFeatures", stub_features, 0);
}
static bool use_load_options(
EFI_HANDLE stub_image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
bool have_cmdline,
char16_t **ret) {
assert(stub_image);
assert(loaded_image);
assert(ret);
/* We only allow custom command lines if we aren't in secure boot or if no cmdline was baked into
* the stub image.
* We also don't allow it if we are in confidential vms and secureboot is on. */
if (secure_boot_enabled() && (have_cmdline || is_confidential_vm()))
return false;
/* The UEFI shell registers EFI_SHELL_PARAMETERS_PROTOCOL onto images it runs. This lets us know that
* LoadOptions starts with the stub binary path which we want to strip off. */
EFI_SHELL_PARAMETERS_PROTOCOL *shell;
if (BS->HandleProtocol(stub_image, MAKE_GUID_PTR(EFI_SHELL_PARAMETERS_PROTOCOL), (void **) &shell) != EFI_SUCCESS) {
/* We also do a superficial check whether first character of passed command line
* is printable character (for compat with some Dell systems which fill in garbage?). */
if (loaded_image->LoadOptionsSize < sizeof(char16_t) || ((const char16_t *) loaded_image->LoadOptions)[0] <= 0x1F)
return false;
/* Not running from EFI shell, use entire LoadOptions. Note that LoadOptions is a void*, so
* it could be anything! */
*ret = mangle_stub_cmdline(xstrndup16(loaded_image->LoadOptions, loaded_image->LoadOptionsSize / sizeof(char16_t)));
return true;
}
if (shell->Argc < 2)
/* No arguments were provided? Then we fall back to built-in cmdline. */
return false;
/* Assemble the command line ourselves without our stub path. */
*ret = xstrdup16(shell->Argv[1]);
for (size_t i = 2; i < shell->Argc; i++) {
_cleanup_free_ char16_t *old = *ret;
*ret = xasprintf("%ls %ls", old, shell->Argv[i]);
}
return true;
}
static EFI_STATUS load_addons_from_dir(
EFI_FILE *root,
const char16_t *prefix,
char16_t ***items,
size_t *n_items,
size_t *n_allocated) {
_cleanup_(file_closep) EFI_FILE *extra_dir = NULL;
_cleanup_free_ EFI_FILE_INFO *dirent = NULL;
size_t dirent_size = 0;
EFI_STATUS err;
assert(root);
assert(prefix);
assert(items);
assert(n_items);
assert(n_allocated);
err = open_directory(root, prefix, &extra_dir);
if (err == EFI_NOT_FOUND)
/* No extra subdir, that's totally OK */
return EFI_SUCCESS;
if (err != EFI_SUCCESS)
return log_error_status(err, "Failed to open addons directory '%ls': %m", prefix);
for (;;) {
_cleanup_free_ char16_t *d = NULL;
err = readdir(extra_dir, &dirent, &dirent_size);
if (err != EFI_SUCCESS)
return log_error_status(err, "Failed to read addons directory of loaded image: %m");
if (!dirent) /* End of directory */
break;
if (dirent->FileName[0] == '.')
continue;
if (FLAGS_SET(dirent->Attribute, EFI_FILE_DIRECTORY))
continue;
if (!is_ascii(dirent->FileName))
continue;
if (strlen16(dirent->FileName) > 255) /* Max filename size on Linux */
continue;
if (!endswith_no_case(dirent->FileName, u".addon.efi"))
continue;
d = xstrdup16(dirent->FileName);
if (*n_items + 2 > *n_allocated) {
/* We allocate 16 entries at a time, as a matter of optimization */
if (*n_items > (SIZE_MAX / sizeof(uint16_t)) - 16) /* Overflow check, just in case */
return log_oom();
size_t m = *n_items + 16;
*items = xrealloc(*items, *n_allocated * sizeof(uint16_t *), m * sizeof(uint16_t *));
*n_allocated = m;
}
(*items)[(*n_items)++] = TAKE_PTR(d);
(*items)[*n_items] = NULL; /* Let's always NUL terminate, to make freeing via strv_free() easy */
}
return EFI_SUCCESS;
}
static void cmdline_append_and_measure_addons(
char16_t *cmdline_addon,
char16_t **cmdline_append,
int *parameters_measured) {
assert(cmdline_append);
assert(parameters_measured);
if (isempty(cmdline_addon))
return;
_cleanup_free_ char16_t *copy = mangle_stub_cmdline(xstrdup16(cmdline_addon));
if (isempty(copy))
return;
bool m = false;
(void) tpm_log_load_options(copy, &m);
combine_measured_flag(parameters_measured, m);
_cleanup_free_ char16_t *tmp = TAKE_PTR(*cmdline_append);
if (isempty(tmp))
*cmdline_append = TAKE_PTR(copy);
else
*cmdline_append = xasprintf("%ls %ls", tmp, copy);
}
typedef struct DevicetreeAddon {
char16_t *filename;
struct iovec blob;
} DevicetreeAddon;
static void devicetree_addon_done(DevicetreeAddon *a) {
assert(a);
a->filename = mfree(a->filename);
iovec_done(&a->blob);
}
static void devicetree_addon_free_many(DevicetreeAddon *a, size_t n) {
assert(a || n == 0);
FOREACH_ARRAY(i, a, n)
devicetree_addon_done(i);
free(a);
}
static void install_addon_devicetrees(
struct devicetree_state *dt_state,
DevicetreeAddon *addons,
size_t n_addons,
int *parameters_measured) {
EFI_STATUS err;
assert(dt_state);
assert(addons || n_addons == 0);
assert(parameters_measured);
FOREACH_ARRAY(a, addons, n_addons) {
err = devicetree_install_from_memory(dt_state, a->blob.iov_base, a->blob.iov_len);
if (err != EFI_SUCCESS) {
log_error_status(err, "Error loading addon devicetree, ignoring: %m");
continue;
}
bool m = false;
err = tpm_log_tagged_event(
TPM2_PCR_KERNEL_CONFIG,
POINTER_TO_PHYSICAL_ADDRESS(a->blob.iov_base),
a->blob.iov_len,
DEVICETREE_ADDON_EVENT_TAG_ID,
a->filename,
&m);
if (err != EFI_SUCCESS)
return (void) log_error_status(
err,
"Unable to extend PCR %i with DTB addon '%ls': %m",
TPM2_PCR_KERNEL_CONFIG,
a->filename);
combine_measured_flag(parameters_measured, m);
}
}
static EFI_STATUS load_addons(
EFI_HANDLE stub_image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const char16_t *prefix,
const char *uname,
char16_t **cmdline, /* Both input+output, extended with new addons we find */
DevicetreeAddon **devicetree_addons, /* Ditto */
size_t *n_devicetree_addons) {
_cleanup_(strv_freep) char16_t **items = NULL;
_cleanup_(file_closep) EFI_FILE *root = NULL;
size_t n_items = 0, n_allocated = 0;
EFI_STATUS err;
assert(stub_image);
assert(loaded_image);
assert(prefix);
if (!loaded_image->DeviceHandle)
return EFI_SUCCESS;
err = open_volume(loaded_image->DeviceHandle, &root);
if (err == EFI_UNSUPPORTED)
/* Error will be unsupported if the bootloader doesn't implement the file system protocol on
* its file handles. */
return EFI_SUCCESS;
if (err != EFI_SUCCESS)
return log_error_status(err, "Unable to open root directory: %m");
err = load_addons_from_dir(root, prefix, &items, &n_items, &n_allocated);
if (err != EFI_SUCCESS)
return err;
if (n_items == 0)
return EFI_SUCCESS; /* Empty directory */
/* Now, sort the files we found, to make this uniform and stable (and to ensure the TPM measurements
* are not dependent on read order) */
sort_pointer_array((void**) items, n_items, (compare_pointer_func_t) strcmp16);
for (size_t i = 0; i < n_items; i++) {
PeSectionVector sections[ELEMENTSOF(unified_sections)] = {};
_cleanup_free_ EFI_DEVICE_PATH *addon_path = NULL;
_cleanup_(unload_imagep) EFI_HANDLE addon = NULL;
EFI_LOADED_IMAGE_PROTOCOL *loaded_addon = NULL;
_cleanup_free_ char16_t *addon_spath = NULL;
addon_spath = xasprintf("%ls\\%ls", prefix, items[i]);
err = make_file_device_path(loaded_image->DeviceHandle, addon_spath, &addon_path);
if (err != EFI_SUCCESS)
return log_error_status(err, "Error making device path for %ls: %m", addon_spath);
/* By using shim_load_image, we cover both the case where the PE files are signed with MoK
* and with DB, and running with or without shim. */
err = shim_load_image(stub_image, addon_path, &addon);
if (err != EFI_SUCCESS) {
log_error_status(err,
"Failed to read '%ls' from '%ls', ignoring: %m",
items[i],
addon_spath);
continue;
}
err = BS->HandleProtocol(addon,
MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL),
(void **) &loaded_addon);
if (err != EFI_SUCCESS)
return log_error_status(err, "Failed to find protocol in %ls: %m", items[i]);
err = pe_memory_locate_sections(loaded_addon->ImageBase, unified_sections, sections);
if (err != EFI_SUCCESS ||
(!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_CMDLINE) &&
!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_DTB))) {
if (err == EFI_SUCCESS)
err = EFI_NOT_FOUND;
log_error_status(err,
"Unable to locate embedded .cmdline/.dtb sections in %ls, ignoring: %m",
items[i]);
continue;
}
/* We want to enforce that addons are not UKIs, i.e.: they must not embed a kernel. */
if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_LINUX)) {
log_error("%ls is a UKI, not an addon, ignoring.", items[i]);
continue;
}
/* Also enforce that, in case it is specified, .uname matches as a quick way to allow
* enforcing compatibility with a specific UKI only */
if (uname && PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_UNAME) &&
!strneq8(uname,
(const char *)loaded_addon->ImageBase + sections[UNIFIED_SECTION_UNAME].memory_offset,
sections[UNIFIED_SECTION_UNAME].size)) {
log_error(".uname mismatch between %ls and UKI, ignoring", items[i]);
continue;
}
if (cmdline && PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_CMDLINE)) {
_cleanup_free_ char16_t *tmp = TAKE_PTR(*cmdline),
*extra16 = mangle_stub_cmdline(pe_section_to_str16(loaded_addon, sections + UNIFIED_SECTION_CMDLINE));
*cmdline = xasprintf("%ls%ls%ls", strempty(tmp), isempty(tmp) ? u"" : u" ", extra16);
}
if (devicetree_addons && PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_DTB)) {
*devicetree_addons = xrealloc(*devicetree_addons,
*n_devicetree_addons * sizeof(size_t),
(*n_devicetree_addons + 1) * sizeof(size_t));
*devicetree_addons[(*n_devicetree_addons)++] = (DevicetreeAddon) {
.blob = {
.iov_base = xmemdup((const uint8_t*) loaded_addon->ImageBase + sections[UNIFIED_SECTION_DTB].memory_offset, sections[UNIFIED_SECTION_DTB].size),
.iov_len = sections[UNIFIED_SECTION_DTB].size,
},
.filename = xstrdup16(items[i]),
};
}
}
return EFI_SUCCESS;
}
static void refresh_random_seed(EFI_LOADED_IMAGE_PROTOCOL *loaded_image) {
EFI_STATUS err;
assert(loaded_image);
/* Handle case, where bootloader doesn't support DeviceHandle. */
if (!loaded_image->DeviceHandle)
return;
uint64_t loader_features = 0;
err = efivar_get_uint64_le(MAKE_GUID_PTR(LOADER), u"LoaderFeatures", &loader_features);
if (err != EFI_SUCCESS)
return;
/* Don't measure again, if sd-boot already initialized the random seed */
if (!FLAGS_SET(loader_features, EFI_LOADER_FEATURE_RANDOM_SEED))
return;
_cleanup_(file_closep) EFI_FILE *esp_dir = NULL;
err = partition_open(MAKE_GUID_PTR(ESP), loaded_image->DeviceHandle, NULL, &esp_dir);
if (err != EFI_SUCCESS) /* Non-fatal on failure, so that we still boot without it. */
return;
(void) process_random_seed(esp_dir);
}
static void measure_sections(
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const PeSectionVector sections[static _UNIFIED_SECTION_MAX],
int *sections_measured) {
assert(loaded_image);
assert(sections);
assert(sections_measured);
/* Measure all "payload" of this PE image into a separate PCR (i.e. where nothing else is written
* into so far), so that we have one PCR that we can nicely write policies against because it
* contains all static data of this image, and thus can be easily be pre-calculated. */
for (UnifiedSection section = 0; section < _UNIFIED_SECTION_MAX; section++) {
if (!unified_section_measure(section)) /* shall not measure? */
continue;
if (!PE_SECTION_VECTOR_IS_SET(sections + section)) /* not found */
continue;
/* First measure the name of the section */
bool m = false;
(void) tpm_log_ipl_event_ascii(
TPM2_PCR_KERNEL_BOOT,
POINTER_TO_PHYSICAL_ADDRESS(unified_sections[section]),
strsize8(unified_sections[section]), /* including NUL byte */
unified_sections[section],
&m);
combine_measured_flag(sections_measured, m);
/* Then measure the data of the section */
m = false;
(void) tpm_log_ipl_event_ascii(
TPM2_PCR_KERNEL_BOOT,
POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + sections[section].memory_offset,
sections[section].size,
unified_sections[section],
&m);
combine_measured_flag(sections_measured, m);
}
}
static void cmdline_append_and_measure_smbios(char16_t **cmdline, int *parameters_measured) {
assert(cmdline);
assert(parameters_measured);
/* SMBIOS OEM Strings data is controlled by the host admin and not covered by the VM attestation, so
* MUST NOT be trusted when in a confidential VM */
if (is_confidential_vm())
return;
const char *extra = smbios_find_oem_string("io.systemd.stub.kernel-cmdline-extra");
if (!extra)
return;
_cleanup_free_ char16_t *extra16 = mangle_stub_cmdline(xstr8_to_16(extra));
if (isempty(extra16))
return;
/* SMBIOS strings are measured in PCR1, but we also want to measure them in our specific PCR12, as
* firmware-owned PCRs are very difficult to use as they'll contain unpredictable measurements that
* are not under control of the machine owner. */
bool m = false;
(void) tpm_log_load_options(extra16, &m);
combine_measured_flag(parameters_measured, m);
_cleanup_free_ char16_t *tmp = TAKE_PTR(*cmdline);
if (isempty(tmp))
*cmdline = TAKE_PTR(extra16);
else
*cmdline = xasprintf("%ls %ls", tmp, extra16);
}
static void initrds_free(struct iovec (*initrds)[_INITRD_MAX]) {
assert(initrds);
/* Free the dynamic initrds, but leave the non-dynamic ones around */
for (size_t i = _INITRD_DYNAMIC_FIRST; i < _INITRD_MAX; i++)
iovec_done((*initrds) + i);
}
static bool initrds_need_combine(struct iovec initrds[static _INITRD_MAX]) {
assert(initrds);
/* Returns true if we have any initrds set that aren't the base initrd. In that case we need to
* merge, otherwise we can pass the embedded initrd as is */
for (size_t i = 0; i < _INITRD_MAX; i++) {
if (i == INITRD_BASE)
continue;
if (iovec_is_set(initrds + i))
return true;
}
return false;
}
static void generate_sidecar_initrds(
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
struct iovec initrds[static _INITRD_MAX],
int *parameters_measured,
int *sysext_measured,
int *confext_measured) {
bool m;
assert(loaded_image);
assert(initrds);
assert(parameters_measured);
assert(sysext_measured);
assert(confext_measured);
if (pack_cpio(loaded_image,
/* dropin_dir= */ NULL,
u".cred",
/* exclude_suffix= */ NULL,
".extra/credentials",
/* dir_mode= */ 0500,
/* access_mode= */ 0400,
/* tpm_pcr= */ TPM2_PCR_KERNEL_CONFIG,
u"Credentials initrd",
initrds + INITRD_CREDENTIAL,
&m) == EFI_SUCCESS)
combine_measured_flag(parameters_measured, m);
if (pack_cpio(loaded_image,
u"\\loader\\credentials",
u".cred",
/* exclude_suffix= */ NULL,
".extra/global_credentials",
/* dir_mode= */ 0500,
/* access_mode= */ 0400,
/* tpm_pcr= */ TPM2_PCR_KERNEL_CONFIG,
u"Global credentials initrd",
initrds + INITRD_GLOBAL_CREDENTIAL,
&m) == EFI_SUCCESS)
combine_measured_flag(parameters_measured, m);
if (pack_cpio(loaded_image,
/* dropin_dir= */ NULL,
u".raw", /* ideally we'd pick up only *.sysext.raw here, but for compat we pick up *.raw instead … */
u".confext.raw", /* … but then exclude *.confext.raw again */
".extra/sysext",
/* dir_mode= */ 0555,
/* access_mode= */ 0444,
/* tpm_pcr= */ TPM2_PCR_SYSEXTS,
u"System extension initrd",
initrds + INITRD_CONFEXT,
&m) == EFI_SUCCESS)
combine_measured_flag(sysext_measured, m);
if (pack_cpio(loaded_image,
/* dropin_dir= */ NULL,
u".confext.raw",
/* exclude_suffix= */ NULL,
".extra/confext",
/* dir_mode= */ 0555,
/* access_mode= */ 0444,
/* tpm_pcr= */ TPM2_PCR_KERNEL_CONFIG,
u"Configuration extension initrd",
initrds + INITRD_SYSEXT,
&m) == EFI_SUCCESS)
combine_measured_flag(confext_measured, m);
}
static void generate_embedded_initrds(
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
PeSectionVector sections[_UNIFIED_SECTION_MAX],
struct iovec initrds[static _INITRD_MAX]) {
assert(loaded_image);
assert(initrds);
/* If the PCR signature was embedded in the PE image, then let's wrap it in a cpio and also pass it
* to the kernel, so that it can be read from /.extra/tpm2-pcr-signature.json. Note that this section
* is not measured, neither as raw section (see above), nor as cpio (here), because it is the
* signature of expected PCR values, i.e. its input are PCR measurements, and hence it shouldn't
* itself be input for PCR measurements. */
if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_PCRSIG))
(void) pack_cpio_literal(
(const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_PCRSIG].memory_offset,
sections[UNIFIED_SECTION_PCRSIG].size,
".extra",
u"tpm2-pcr-signature.json",
/* dir_mode= */ 0555,
/* access_mode= */ 0444,
/* tpm_pcr= */ UINT32_MAX,
/* tpm_description= */ NULL,
initrds + INITRD_PCRSIG,
/* ret_measured= */ NULL);
/* If the public key used for the PCR signatures was embedded in the PE image, then let's wrap it in
* a cpio and also pass it to the kernel, so that it can be read from
* /.extra/tpm2-pcr-public-key.pem. This section is already measure above, hence we won't measure the
* cpio. */
if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_PCRPKEY))
(void) pack_cpio_literal(
(const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_PCRPKEY].memory_offset,
sections[UNIFIED_SECTION_PCRPKEY].size,
".extra",
u"tpm2-pcr-public-key.pem",
/* dir_mode= */ 0555,
/* access_mode= */ 0444,
/* tpm_pcr= */ UINT32_MAX,
/* tpm_description= */ NULL,
initrds + INITRD_PCRPKEY,
/* ret_measured= */ NULL);
}
static void lookup_embedded_initrds(
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
PeSectionVector sections[_UNIFIED_SECTION_MAX],
struct iovec initrds[static _INITRD_MAX]) {
assert(loaded_image);
assert(sections);
assert(initrds);
if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_INITRD))
initrds[INITRD_BASE] = IOVEC_MAKE(
(const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_INITRD].memory_offset,
sections[UNIFIED_SECTION_INITRD].size);
if (PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_UCODE))
initrds[INITRD_UCODE] = IOVEC_MAKE(
(const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_UCODE].memory_offset,
sections[UNIFIED_SECTION_UCODE].size);
}
static void export_pcr_variables(
int sections_measured,
int parameters_measured,
int sysext_measured,
int confext_measured) {
/* After we are done with measuring, set an EFI variable that tells userspace this was done
* successfully, and encode in it which PCR was used. */
if (sections_measured > 0)
(void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"StubPcrKernelImage", TPM2_PCR_KERNEL_BOOT, 0);
if (parameters_measured > 0)
(void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"StubPcrKernelParameters", TPM2_PCR_KERNEL_CONFIG, 0);
if (sysext_measured > 0)
(void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"StubPcrInitRDSysExts", TPM2_PCR_SYSEXTS, 0);
if (confext_measured > 0)
(void) efivar_set_uint64_str16(MAKE_GUID_PTR(LOADER), u"StubPcrInitRDConfExts", TPM2_PCR_KERNEL_CONFIG, 0);
}
static void install_embedded_devicetree(
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const PeSectionVector sections[static _UNIFIED_SECTION_MAX],
struct devicetree_state *dt_state) {
EFI_STATUS err;
assert(loaded_image);
assert(sections);
assert(dt_state);
if (!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_DTB))
return;
err = devicetree_install_from_memory(
dt_state,
(const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_DTB].memory_offset,
sections[UNIFIED_SECTION_DTB].size);
if (err != EFI_SUCCESS)
log_error_status(err, "Error loading embedded devicetree, igoring: %m");
}
static void load_all_addons(
EFI_HANDLE image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const char *uname,
char16_t **cmdline_addons,
DevicetreeAddon **dt_addons,
size_t *n_dt_addons) {
EFI_STATUS err;
assert(loaded_image);
assert(cmdline_addons);
assert(dt_addons);
assert(n_dt_addons);
err = load_addons(
image,
loaded_image,
u"\\loader\\addons",
uname,
cmdline_addons,
dt_addons,
n_dt_addons);
if (err != EFI_SUCCESS)
log_error_status(err, "Error loading global addons, ignoring: %m");
/* Some bootloaders always pass NULL in FilePath, so we need to check for it here. */
_cleanup_free_ char16_t *dropin_dir = get_extra_dir(loaded_image->FilePath);
if (!dropin_dir)
return;
err = load_addons(
image,
loaded_image,
dropin_dir,
uname,
cmdline_addons,
dt_addons,
n_dt_addons);
if (err != EFI_SUCCESS)
log_error_status(err, "Error loading UKI-specific addons, ignoring: %m");
}
static void display_splash(
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const PeSectionVector sections[static _UNIFIED_SECTION_MAX]) {
assert(loaded_image);
assert(sections);
if (!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_SPLASH))
return;
graphics_splash((const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_SPLASH].memory_offset, sections[UNIFIED_SECTION_SPLASH].size);
}
static void determine_cmdline(
EFI_HANDLE image,
EFI_LOADED_IMAGE_PROTOCOL *loaded_image,
const PeSectionVector sections[static _UNIFIED_SECTION_MAX],
char16_t **ret_cmdline,
int *parameters_measured) {
assert(loaded_image);
assert(sections);
if (use_load_options(image, loaded_image, /* have_cmdline= */ PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_CMDLINE), ret_cmdline)) {
/* Let's measure the passed kernel command line into the TPM. Note that this possibly
* duplicates what we already did in the boot menu, if that was already used. However, since
* we want the boot menu to support an EFI binary, and want to this stub to be usable from
* any boot menu, let's measure things anyway. */
bool m = false;
(void) tpm_log_load_options(*ret_cmdline, &m);
combine_measured_flag(parameters_measured, m);
} else
*ret_cmdline = mangle_stub_cmdline(pe_section_to_str16(loaded_image, sections + UNIFIED_SECTION_CMDLINE));
}
static EFI_STATUS run(EFI_HANDLE image) {
int sections_measured = -1, parameters_measured = -1, sysext_measured = -1, confext_measured = -1;
_cleanup_(devicetree_cleanup) struct devicetree_state dt_state = {};
_cleanup_free_ char16_t *cmdline = NULL, *cmdline_addons = NULL;
_cleanup_(initrds_free) struct iovec initrds[_INITRD_MAX] = {};
PeSectionVector sections[ELEMENTSOF(unified_sections)] = {};
EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
_cleanup_free_ char *uname = NULL;
DevicetreeAddon *dt_addons = NULL;
size_t n_dt_addons = 0;
EFI_STATUS err;
err = BS->HandleProtocol(image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), (void **) &loaded_image);
if (err != EFI_SUCCESS)
return log_error_status(err, "Error getting a LoadedImageProtocol handle: %m");
err = pe_memory_locate_sections(loaded_image->ImageBase, unified_sections, sections);
if (err != EFI_SUCCESS)
return log_error_status(err, "Unable to locate embedded PE sections: %m");
if (!PE_SECTION_VECTOR_IS_SET(sections + UNIFIED_SECTION_LINUX))
return log_error_status(EFI_NOT_FOUND, "Image lacks .linux section.");
measure_sections(loaded_image, sections, §ions_measured);
/* Show splash screen as early as possible, but after measuring it */
display_splash(loaded_image, sections);
refresh_random_seed(loaded_image);
uname = pe_section_to_str8(loaded_image, sections + UNIFIED_SECTION_UNAME);
determine_cmdline(image, loaded_image, sections, &cmdline, ¶meters_measured);
/* Now that we have the UKI sections loaded, also load global first and then local (per-UKI)
* addons. The data is loaded at once, and then used later. */
CLEANUP_ARRAY(dt_addons, n_dt_addons, devicetree_addon_free_many);
load_all_addons(image, loaded_image, uname, &cmdline_addons, &dt_addons, &n_dt_addons);
/* If we have any extra command line to add via PE addons, load them now and append, and measure the
* additions together, after the embedded options, but before the smbios ones, so that the order is
* reversed from "most hardcoded" to "most dynamic". The global addons are loaded first, and the
* image-specific ones later, for the same reason. */
cmdline_append_and_measure_addons(cmdline_addons, &cmdline, ¶meters_measured);
cmdline_append_and_measure_smbios(&cmdline, ¶meters_measured);
export_common_variables(loaded_image);
export_stub_variables(loaded_image);
/* First load the base device tree, then fix it up using addons - global first, then per-UKI. */
install_embedded_devicetree(loaded_image, sections, &dt_state);
install_addon_devicetrees(&dt_state, dt_addons, n_dt_addons, ¶meters_measured);
/* Generate & find all initrds */
generate_sidecar_initrds(loaded_image, initrds, ¶meters_measured, &sysext_measured, &confext_measured);
generate_embedded_initrds(loaded_image, sections, initrds);
lookup_embedded_initrds(loaded_image, sections, initrds);
/* Export variables indicating what we measured */
export_pcr_variables(sections_measured, parameters_measured, sysext_measured, confext_measured);
/* Combine the initrds into one */
_cleanup_pages_ Pages initrd_pages = {};
struct iovec final_initrd;
if (initrds_need_combine(initrds)) {
/* If we have generated initrds dynamically or there is a microcode initrd, combine them with
* the built-in initrd. */
err = combine_initrds(initrds, _INITRD_MAX, &initrd_pages, &final_initrd.iov_len);
if (err != EFI_SUCCESS)
return err;
final_initrd.iov_base = PHYSICAL_ADDRESS_TO_POINTER(initrd_pages.addr);
/* Given these might be large let's free them explicitly before we pass control to Linux */
initrds_free(&initrds);
} else
final_initrd = initrds[INITRD_BASE];
struct iovec kernel = IOVEC_MAKE(
(const uint8_t*) loaded_image->ImageBase + sections[UNIFIED_SECTION_LINUX].memory_offset,
sections[UNIFIED_SECTION_LINUX].size);
err = linux_exec(image, cmdline, &kernel, &final_initrd);
graphics_mode(false);
return err;
}
DEFINE_EFI_MAIN_FUNCTION(run, "systemd-stub", /* wait_for_debugger= */ false);
|