-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathshim.c
More file actions
1309 lines (1139 loc) · 32.8 KB
/
shim.c
File metadata and controls
1309 lines (1139 loc) · 32.8 KB
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-2-Clause-Patent
/*
* shim - trivial UEFI first-stage bootloader
*
* Copyright Red Hat, Inc
* Author: Matthew Garrett
*
* Significant portions of this code are derived from Tianocore
* (http://tianocore.sf.net) and are Copyright 2009-2012 Intel
* Corporation.
*/
#include "shim.h"
#if defined(ENABLE_SHIM_CERT)
#include "shim_cert.h"
#endif /* defined(ENABLE_SHIM_CERT) */
#include <stdint.h>
static EFI_SYSTEM_TABLE *systab;
static EFI_HANDLE global_image_handle;
static EFI_LOADED_IMAGE *shim_li;
static EFI_LOADED_IMAGE shim_li_bak;
list_t sbat_var;
/*
* The vendor certificate used for validating the second stage loader
*/
extern struct {
UINT32 vendor_authorized_size;
UINT32 vendor_deauthorized_size;
UINT32 vendor_authorized_offset;
UINT32 vendor_deauthorized_offset;
} cert_table;
typedef struct {
UINT32 MokSize;
UINT8 *Mok;
} MokListNode;
/*
* Check whether we're in Secure Boot and user mode
*/
BOOLEAN secure_mode (void)
{
static int first = 1;
if (user_insecure_mode)
return FALSE;
if (variable_is_secureboot() != 1) {
if (verbose && !in_protocol && first) {
CHAR16 *title = L"Secure boot not enabled";
CHAR16 *message = L"Press any key to continue";
console_countdown(title, message, 5);
}
first = 0;
return FALSE;
}
/* If we /do/ have "SecureBoot", but /don't/ have "SetupMode",
* then the implementation is bad, but we assume that secure boot is
* enabled according to the status of "SecureBoot". If we have both
* of them, then "SetupMode" may tell us additional data, and we need
* to consider it.
*/
if (variable_is_setupmode(0) == 1) {
if (verbose && !in_protocol && first) {
CHAR16 *title = L"Platform is in setup mode";
CHAR16 *message = L"Press any key to continue";
console_countdown(title, message, 5);
}
first = 0;
return FALSE;
}
first = 0;
return TRUE;
}
static int
should_use_fallback(EFI_HANDLE image_handle)
{
EFI_LOADED_IMAGE *li;
EFI_FILE_IO_INTERFACE *fio = NULL;
EFI_FILE *vh = NULL;
EFI_FILE *fh = NULL;
EFI_STATUS efi_status;
int ret = 0;
efi_status = BS->HandleProtocol(image_handle, &EFI_LOADED_IMAGE_GUID,
(void **)&li);
if (EFI_ERROR(efi_status)) {
perror(L"Could not get image for boot" EFI_ARCH L".efi: %r\n",
efi_status);
return 0;
}
if (!is_removable_media_path(li))
goto error;
efi_status = BS->HandleProtocol(li->DeviceHandle, &FileSystemProtocol,
(void **) &fio);
if (EFI_ERROR(efi_status)) {
perror(L"Could not get fio for li->DeviceHandle: %r\n",
efi_status);
goto error;
}
efi_status = fio->OpenVolume(fio, &vh);
if (EFI_ERROR(efi_status)) {
perror(L"Could not open fio volume: %r\n", efi_status);
goto error;
}
efi_status = vh->Open(vh, &fh, L"\\EFI\\BOOT" FALLBACK,
EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(efi_status)) {
/* Do not print the error here - this is an acceptable case
* for removable media, where we genuinely don't want
* fallback.efi to exist.
* Print(L"Could not open \"\\EFI\\BOOT%s\": %r\n", FALLBACK,
* efi_status);
*/
goto error;
}
ret = 1;
error:
if (fh)
fh->Close(fh);
if (vh)
vh->Close(vh);
return ret;
}
/*
* Open the second stage bootloader and read it into a buffer
*/
static EFI_STATUS load_image (EFI_LOADED_IMAGE *li, void **data,
int *datasize, CHAR16 *PathName)
{
EFI_STATUS efi_status;
EFI_HANDLE device;
EFI_FILE_INFO *fileinfo = NULL;
EFI_FILE_IO_INTERFACE *drive;
EFI_FILE *root, *grub;
UINTN buffersize = sizeof(EFI_FILE_INFO);
device = li->DeviceHandle;
dprint(L"attempting to load %s\n", PathName);
/*
* Open the device
*/
efi_status = BS->HandleProtocol(device, &EFI_SIMPLE_FILE_SYSTEM_GUID,
(void **) &drive);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to find fs: %r\n", efi_status);
goto error;
}
efi_status = drive->OpenVolume(drive, &root);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to open fs: %r\n", efi_status);
goto error;
}
/*
* And then open the file
*/
efi_status = root->Open(root, &grub, PathName, EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to open %s - %r\n", PathName, efi_status);
goto error;
}
fileinfo = AllocatePool(buffersize);
if (!fileinfo) {
perror(L"Unable to allocate file info buffer\n");
efi_status = EFI_OUT_OF_RESOURCES;
goto error;
}
/*
* Find out how big the file is in order to allocate the storage
* buffer
*/
efi_status = grub->GetInfo(grub, &EFI_FILE_INFO_GUID, &buffersize,
fileinfo);
if (efi_status == EFI_BUFFER_TOO_SMALL) {
FreePool(fileinfo);
fileinfo = AllocatePool(buffersize);
if (!fileinfo) {
perror(L"Unable to allocate file info buffer\n");
efi_status = EFI_OUT_OF_RESOURCES;
goto error;
}
efi_status = grub->GetInfo(grub, &EFI_FILE_INFO_GUID,
&buffersize, fileinfo);
}
if (EFI_ERROR(efi_status)) {
perror(L"Unable to get file info: %r\n", efi_status);
goto error;
}
buffersize = fileinfo->FileSize;
*data = AllocatePool(buffersize);
if (!*data) {
perror(L"Unable to allocate file buffer\n");
efi_status = EFI_OUT_OF_RESOURCES;
goto error;
}
/*
* Perform the actual read
*/
efi_status = grub->Read(grub, &buffersize, *data);
if (efi_status == EFI_BUFFER_TOO_SMALL) {
FreePool(*data);
*data = AllocatePool(buffersize);
efi_status = grub->Read(grub, &buffersize, *data);
}
if (EFI_ERROR(efi_status)) {
perror(L"Unexpected return from initial read: %r, buffersize %x\n",
efi_status, buffersize);
goto error;
}
*datasize = buffersize;
FreePool(fileinfo);
return EFI_SUCCESS;
error:
if (*data) {
FreePool(*data);
*data = NULL;
}
if (fileinfo)
FreePool(fileinfo);
return efi_status;
}
VOID
restore_loaded_image(VOID)
{
if (shim_li->FilePath)
FreePool(shim_li->FilePath);
/*
* Restore our original loaded image values
*/
CopyMem(shim_li, &shim_li_bak, sizeof(shim_li_bak));
}
/* If gets used on static data it probably needs boundary checking */
void
str16_to_str8(CHAR16 *str16, CHAR8 **str8)
{
int i = 0;
while (str16[i++] != '\0');
*str8 = (CHAR8 *)AllocatePool((i + 1) * sizeof (CHAR8));
i = 0;
while (str16[i] != '\0') {
(*str8)[i] = (CHAR8)str16[i];
i++;
}
(*str8)[i] = '\0';
}
/*
* Load and run an EFI executable
*/
EFI_STATUS read_image(EFI_HANDLE image_handle, CHAR16 *ImagePath,
CHAR16 **PathName, void **data, int *datasize,
int flags)
{
EFI_STATUS efi_status;
void *sourcebuffer = NULL;
UINT64 sourcesize = 0;
CHAR8 *netbootname;
/*
* We need to refer to the loaded image protocol on the running
* binary in order to find our path
*/
efi_status = BS->HandleProtocol(image_handle, &EFI_LOADED_IMAGE_GUID,
(void **)&shim_li);
if (EFI_ERROR(efi_status)) {
perror(L"Unable to init protocol\n");
return efi_status;
}
/*
* Build a new path from the existing one plus the executable name
*/
efi_status = generate_path_from_image_path(shim_li, ImagePath, PathName);
if (EFI_ERROR(efi_status)) {
perror(L"Unable to generate path %s: %r\n", ImagePath,
efi_status);
return efi_status;
}
if (findNetboot(shim_li->DeviceHandle)) {
str16_to_str8(ImagePath, &netbootname);
efi_status = parseNetbootinfo(image_handle, netbootname);
if (EFI_ERROR(efi_status)) {
perror(L"Netboot parsing failed: %r\n", efi_status);
return EFI_PROTOCOL_ERROR;
}
FreePool(netbootname);
efi_status = FetchNetbootimage(image_handle, &sourcebuffer,
&sourcesize, flags);
if (EFI_ERROR(efi_status)) {
if (~flags & SUPPRESS_NETBOOT_OPEN_FAILURE_NOISE)
perror(L"Unable to fetch TFTP image: %r\n",
efi_status);
return efi_status;
}
*data = sourcebuffer;
*datasize = sourcesize;
} else if (find_httpboot(shim_li->DeviceHandle)) {
str16_to_str8(ImagePath, &netbootname);
efi_status = httpboot_fetch_buffer (image_handle,
&sourcebuffer,
&sourcesize,
netbootname);
if (EFI_ERROR(efi_status)) {
if (~flags & SUPPRESS_NETBOOT_OPEN_FAILURE_NOISE)
perror(L"Unable to fetch HTTP image %a: %r\n",
netbootname, efi_status);
return efi_status;
}
*data = sourcebuffer;
*datasize = sourcesize;
} else {
/*
* Read the new executable off disk
*/
efi_status = load_image(shim_li, data, datasize, *PathName);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to load image %s: %r\n",
*PathName, efi_status);
PrintErrors();
ClearErrors();
return efi_status;
}
}
if (*datasize < 0)
efi_status = EFI_INVALID_PARAMETER;
return efi_status;
}
/*
* Load and run an EFI executable
*/
EFI_STATUS start_image(EFI_HANDLE image_handle, CHAR16 *ImagePath)
{
EFI_STATUS efi_status;
EFI_IMAGE_ENTRY_POINT entry_point;
EFI_PHYSICAL_ADDRESS alloc_address;
UINTN alloc_pages;
CHAR16 *PathName = NULL;
void *data = NULL;
int datasize = 0;
unsigned int alloc_alignment;
efi_status = read_image(image_handle, ImagePath, &PathName, &data,
&datasize, 0);
if (EFI_ERROR(efi_status))
goto done;
/*
* We need to modify the loaded image protocol entry before running
* the new binary, so back it up
*/
CopyMem(&shim_li_bak, shim_li, sizeof(shim_li_bak));
/*
* Update the loaded image with the second stage loader file path
*/
shim_li->FilePath = FileDevicePath(NULL, PathName);
if (!shim_li->FilePath) {
perror(L"Unable to update loaded image file path\n");
efi_status = EFI_OUT_OF_RESOURCES;
goto restore;
}
/*
* Verify and, if appropriate, relocate and execute the executable
*/
efi_status = handle_image(data, datasize, shim_li, image_handle,
&entry_point, &alloc_address, &alloc_pages,
&alloc_alignment, false);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to load image: %r\n", efi_status);
PrintErrors();
ClearErrors();
goto restore;
}
#if 0
save_logs();
#endif
/*
* The binary is trusted and relocated. Run it
*/
efi_status = entry_point(image_handle, systab);
restore:
restore_loaded_image();
done:
if (PathName)
FreePool(PathName);
if (data)
FreePool(data);
return efi_status;
}
/*
* Load and run grub. If that fails because grub isn't trusted, load and
* run MokManager.
*/
EFI_STATUS init_grub(EFI_HANDLE image_handle)
{
EFI_STATUS efi_status;
int use_fb = should_use_fallback(image_handle);
efi_status = start_image(image_handle, use_fb ? FALLBACK :second_stage);
if (efi_status == EFI_SECURITY_VIOLATION ||
efi_status == EFI_ACCESS_DENIED) {
efi_status = start_image(image_handle, MOK_MANAGER);
if (EFI_ERROR(efi_status)) {
console_print(L"start_image() returned %r\n", efi_status);
usleep(2000000);
return efi_status;
}
efi_status = start_image(image_handle,
use_fb ? FALLBACK : second_stage);
}
/*
* If the filename is invalid, or the file does not exist, just fall
* back to the default loader. Also fall back to the default loader
* if we get a TFTP error or HTTP error.
*/
if (!use_fb && (efi_status == EFI_INVALID_PARAMETER ||
efi_status == EFI_NOT_FOUND ||
efi_status == EFI_HTTP_ERROR ||
efi_status == EFI_TFTP_ERROR)) {
console_print(
L"start_image() returned %r, falling back to default loader\n",
efi_status);
usleep(2000000);
load_options = NULL;
load_options_size = 0;
efi_status = start_image(image_handle, DEFAULT_LOADER);
}
if (EFI_ERROR(efi_status)) {
console_print(L"start_image() returned %r\n", efi_status);
usleep(2000000);
}
return efi_status;
}
/*
* Check the load options to specify the second stage loader
*/
EFI_STATUS set_second_stage (EFI_HANDLE image_handle)
{
EFI_STATUS efi_status;
EFI_LOADED_IMAGE *li = NULL;
second_stage = (optional_second_stage) ? optional_second_stage : DEFAULT_LOADER;
load_options = NULL;
load_options_size = 0;
efi_status = BS->HandleProtocol(image_handle, &LoadedImageProtocol,
(void **) &li);
if (EFI_ERROR(efi_status)) {
perror (L"Failed to get load options: %r\n", efi_status);
return efi_status;
}
#if defined(DISABLE_REMOVABLE_LOAD_OPTIONS)
/*
* boot services build very strange load options, and we might misparse them,
* causing boot failures on removable media.
*/
if (is_removable_media_path(li)) {
dprint("Invoked from removable media path, ignoring boot options");
return EFI_SUCCESS;
}
#endif
efi_status = parse_load_options(li);
if (EFI_ERROR(efi_status)) {
perror (L"Failed to get load options: %r\n", efi_status);
return efi_status;
}
return EFI_SUCCESS;
}
static SHIM_LOCK shim_lock_interface;
static EFI_HANDLE shim_lock_handle;
EFI_STATUS
install_shim_protocols(void)
{
SHIM_LOCK *shim_lock;
EFI_STATUS efi_status;
/*
* Did another instance of shim earlier already install the
* protocol? If so, get rid of it.
*
* We have to uninstall shim's protocol here, because if we're
* On the fallback.efi path, then our call pathway is:
*
* shim->fallback->shim->grub
* ^ ^ ^
* | | \- gets protocol #0
* | \- installs its protocol (#1)
* \- installs its protocol (#0)
* and if we haven't removed this, then grub will get the *first*
* shim's protocol, but it'll get the second shim's systab
* replacements. So even though it will participate and verify
* the kernel, the systab never finds out.
*/
efi_status = LibLocateProtocol(&SHIM_LOCK_GUID, (VOID **)&shim_lock);
if (!EFI_ERROR(efi_status))
uninstall_shim_protocols();
init_image_loader();
/*
* Install the protocol
*/
efi_status = BS->InstallMultipleProtocolInterfaces(&shim_lock_handle,
&SHIM_LOCK_GUID,
&shim_lock_interface,
&SHIM_IMAGE_LOADER_GUID,
&shim_image_loader_interface,
NULL);
if (EFI_ERROR(efi_status)) {
console_error(L"Could not install security protocol",
efi_status);
return efi_status;
}
if (!secure_mode())
return EFI_SUCCESS;
#if defined(OVERRIDE_SECURITY_POLICY)
/*
* Install the security protocol hook
*/
security_policy_install(shim_verify);
#endif
return EFI_SUCCESS;
}
void
uninstall_shim_protocols(void)
{
/*
* If we're back here then clean everything up before exiting
*/
BS->UninstallMultipleProtocolInterfaces(shim_lock_handle,
&SHIM_LOCK_GUID,
&shim_lock_interface,
&SHIM_IMAGE_LOADER_GUID,
&shim_image_loader_interface,
NULL);
if (!secure_mode())
return;
#if defined(OVERRIDE_SECURITY_POLICY)
/*
* Clean up the security protocol hook
*/
security_policy_uninstall();
#endif
}
static void
check_section_helper(char *section_name, int len, void **pointer,
EFI_IMAGE_SECTION_HEADER *Section, void *data,
int datasize, size_t minsize)
{
if (CompareMem(Section->Name, section_name, len) == 0) {
*pointer = ImageAddress(data, datasize, Section->PointerToRawData);
if (Section->SizeOfRawData < minsize) {
dprint(L"found and rejected %.*a bad size\n", len, section_name);
dprint(L"minsize: %d\n", minsize);
dprint(L"rawsize: %d\n", Section->SizeOfRawData);
return ;
}
if (!*pointer) {
return ;
}
dprint(L"found %.*a\n", len, section_name);
}
}
#define check_section(section_name, pointer, section, data, datasize, minsize) \
check_section_helper(section_name, sizeof(section_name) - 1, pointer, \
section, data, datasize, minsize)
EFI_STATUS
load_revocations_file(EFI_HANDLE image_handle, CHAR16 *FileName, CHAR16 *PathName)
{
EFI_STATUS efi_status = EFI_SUCCESS;
PE_COFF_LOADER_IMAGE_CONTEXT context;
EFI_IMAGE_SECTION_HEADER *Section;
int datasize = 0;
void *data = NULL;
unsigned int i;
char *sbat_var_automatic = NULL;
char *sbat_var_latest = NULL;
uint8_t *ssps_automatic = NULL;
uint8_t *sspv_automatic = NULL;
uint8_t *ssps_latest = NULL;
uint8_t *sspv_latest = NULL;
efi_status = read_image(image_handle, FileName, &PathName,
&data, &datasize,
SUPPRESS_NETBOOT_OPEN_FAILURE_NOISE);
if (!EFI_ERROR(efi_status))
efi_status = verify_image(data, datasize, shim_li, &context);
if (EFI_ERROR(efi_status)) {
dprint(L"revocations failed to verify\n");
return efi_status;
}
dprint(L"verified revocations\n");
Section = context.FirstSection;
for (i = 0; i < context.NumberOfSections; i++, Section++) {
dprint(L"checking section \"%c%c%c%c%c%c%c%c\"\n", (char *)Section->Name);
check_section(".sbata\0\0", (void **)&sbat_var_automatic, Section,
data, datasize, sizeof(SBAT_VAR_ORIGINAL));
check_section(".sbatl\0\0", (void **)&sbat_var_latest, Section,
data, datasize, sizeof(SBAT_VAR_ORIGINAL));
check_section(".sspva\0\0", (void **)&sspv_automatic, Section,
data, datasize, SSPVER_SIZE);
check_section(".sspsa\0\0", (void **)&ssps_automatic, Section,
data, datasize, SSPSIG_SIZE);
check_section(".sspvl\0\0", (void **)&sspv_latest, Section,
data, datasize, SSPVER_SIZE);
check_section(".sspsl\0\0", (void **)&ssps_latest, Section,
data, datasize, SSPSIG_SIZE);
}
if (sbat_var_latest && sbat_var_automatic) {
dprint(L"attempting to update SBAT_LEVEL\n");
efi_status = set_sbat_uefi_variable(sbat_var_automatic,
sbat_var_latest);
} else {
dprint(L"no data for SBAT_LEVEL\n");
}
if ((sspv_automatic && ssps_automatic) || (sspv_latest && ssps_latest)) {
dprint(L"attempting to update SkuSiPolicy\n");
efi_status = set_ssp_uefi_variable(sspv_automatic, ssps_automatic,
sspv_latest, ssps_latest);
} else {
dprint(L"no data for SkuSiPolicy\n");
}
FreePool(data);
return efi_status;
}
EFI_STATUS
load_cert_file(EFI_HANDLE image_handle, CHAR16 *filename, CHAR16 *PathName,
int flags)
{
EFI_STATUS efi_status;
PE_COFF_LOADER_IMAGE_CONTEXT context;
EFI_IMAGE_SECTION_HEADER *Section;
EFI_SIGNATURE_LIST *certlist;
void *pointer;
UINT32 original;
UINT32 offset;
int datasize = 0;
void *data = NULL;
int i;
efi_status = read_image(image_handle, filename, &PathName,
&data, &datasize, flags);
if (EFI_ERROR(efi_status))
return efi_status;
efi_status = verify_image(data, datasize, shim_li, &context);
if (EFI_ERROR(efi_status)) {
FreePool(data);
return efi_status;
}
Section = context.FirstSection;
for (i = 0; i < context.NumberOfSections; i++, Section++) {
UINT32 sec_size = MIN(Section->Misc.VirtualSize, Section->SizeOfRawData);
if (CompareMem(Section->Name, ".db\0\0\0\0\0", 8) == 0) {
offset = 0;
while ((sec_size - offset) >= sizeof(EFI_SIGNATURE_LIST)) {
UINT8 *tmp;
original = user_cert_size;
pointer = ImageAddress(data, datasize,
Section->PointerToRawData + offset);
if (!pointer) {
break;
}
certlist = pointer;
if (certlist->SignatureListSize < sizeof(EFI_SIGNATURE_LIST) ||
checked_add(offset, certlist->SignatureListSize, &offset) ||
offset > sec_size ||
checked_add(user_cert_size, certlist->SignatureListSize,
&user_cert_size)) {
break;
}
tmp = ReallocatePool(user_cert, original,
user_cert_size);
if (!tmp) {
FreePool(data);
return EFI_OUT_OF_RESOURCES;
}
user_cert = tmp;
CopyMem(user_cert + original, pointer,
certlist->SignatureListSize);
}
}
}
FreePool(data);
return EFI_SUCCESS;
}
/*
* Read additional certificates and SBAT Level requirements from files
* (after verifying signatures)
*/
EFI_STATUS
load_unbundled_trust(EFI_HANDLE image_handle)
{
EFI_STATUS efi_status;
EFI_LOADED_IMAGE *li = NULL;
CHAR16 *PathName = NULL;
static CHAR16 FileName[] = L"shim_certificate_0.efi";
EFI_FILE *root, *dir;
EFI_FILE_INFO *info;
EFI_HANDLE device;
EFI_FILE_IO_INTERFACE *drive;
UINTN buffersize = 0;
void *buffer = NULL;
BOOLEAN search_revocations = TRUE;
int i = 0;
efi_status = gBS->HandleProtocol(image_handle, &EFI_LOADED_IMAGE_GUID,
(void **)&li);
if (EFI_ERROR(efi_status)) {
perror(L"Unable to init protocol\n");
return efi_status;
}
efi_status = generate_path_from_image_path(li, L"", &PathName);
if (EFI_ERROR(efi_status))
goto done;
device = li->DeviceHandle;
efi_status = gBS->HandleProtocol(device, &EFI_SIMPLE_FILE_SYSTEM_GUID,
(void **)&drive);
if (EFI_ERROR(efi_status)) {
dprint(L"Failed to find fs on local drive (netboot?): %r \n",
efi_status);
/*
* Network boot cases do not support reading a directory. Try
* to read revocations to pull in any unbundled SBATLevel
* updates unconditionally in those cases. This may produce
* console noise when the file is not present.
*/
load_revocations_file(image_handle, SKUSIREVOCATIONFILE, PathName);
load_revocations_file(image_handle, SBATREVOCATIONFILE, PathName);
while (load_cert_file(image_handle, FileName, PathName,
SUPPRESS_NETBOOT_OPEN_FAILURE_NOISE) == EFI_SUCCESS
&& i++ < 10) {
FileName[17]++;
}
goto done;
}
efi_status = drive->OpenVolume(drive, &root);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to open fs: %r\n", efi_status);
goto done;
}
efi_status = root->Open(root, &dir, PathName, EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to open %s - %r\n", PathName, efi_status);
goto done;
}
if (!secure_mode())
goto done;
while (true) {
UINTN old = buffersize;
efi_status = dir->Read(dir, &buffersize, buffer);
if (efi_status == EFI_BUFFER_TOO_SMALL) {
if (buffersize == old) {
/*
* Some UEFI drivers or firmwares are not compliant with
* the EFI_FILE_PROTOCOL.Read() specs and do not return the
* required buffer size along with EFI_BUFFER_TOO_SMALL.
* Work around this by progressively increasing the buffer
* size, up to a certain point, until the call succeeds.
*/
perror(L"Error reading directory %s - non-compliant UEFI driver or firmware!\n",
PathName);
buffersize = (buffersize < 4) ? 4 : buffersize * 2;
if (buffersize > 1024)
goto done;
}
buffer = ReallocatePool(buffer, old, buffersize);
if (buffer == NULL) {
perror(L"Failed to read directory %s - %r\n",
PathName, EFI_OUT_OF_RESOURCES);
goto done;
}
continue;
} else if (EFI_ERROR(efi_status)) {
perror(L"Failed to read directory %s - %r\n", PathName,
efi_status);
goto done;
}
info = (EFI_FILE_INFO *)buffer;
if (buffersize == 0 || !info) {
if (search_revocations) {
search_revocations = FALSE;
efi_status = root->Open(root, &dir, PathName,
EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to open %s - %r\n",
PathName, efi_status);
goto done;
}
continue;
} else {
goto done;
}
}
/*
* In the event that there are unprocessed sbat revocation
* additions, they could be intended to ban any *new* trust
* anchors we find here. With that in mind, we always want to
* do a pass of loading revocations before we try to add
* anything new to our allowlist. This is done by making two
* passes over the directory, first to search for the
* revocations_sbat.efi file then to search for shim_certificate*.efi
*/
if (search_revocations &&
StrCaseCmp(info->FileName, SBATREVOCATIONFILE) == 0) {
load_revocations_file(image_handle, SBATREVOCATIONFILE, PathName);
search_revocations = FALSE;
efi_status = root->Open(root, &dir, PathName,
EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to open %s - %r\n",
PathName, efi_status);
goto done;
}
}
if (!search_revocations) {
if (StrnCaseCmp(info->FileName, L"shim_certificate", 16) == 0) {
load_cert_file(image_handle, info->FileName, PathName, 0);
}
if (StrCaseCmp(info->FileName, SKUSIREVOCATIONFILE) == 0) {
load_revocations_file(image_handle,
SKUSIREVOCATIONFILE, PathName);
}
}
}
done:
FreePool(buffer);
FreePool(PathName);
return efi_status;
}
/* Read optional options file */
EFI_STATUS
load_shim_options(EFI_HANDLE image_handle)
{
EFI_STATUS efi_status;
EFI_HANDLE device;
EFI_LOADED_IMAGE *li = NULL;
EFI_FILE_IO_INTERFACE *drive;
EFI_FILE *root;
EFI_FILE_HANDLE ofile;
CHAR16 *PathName = NULL;
CHAR16 *buffer;
UINTN comma0;
UINT64 bs;
efi_status = gBS->HandleProtocol(image_handle, &EFI_LOADED_IMAGE_GUID,
(void **)&li);
if (EFI_ERROR(efi_status)) {
perror(L"Unable to init protocol\n");
return efi_status;
}
efi_status = generate_path_from_image_path(li, L"options.csv", &PathName);
if (EFI_ERROR(efi_status))
goto done;
device = li->DeviceHandle;
efi_status = BS->HandleProtocol(device, &EFI_SIMPLE_FILE_SYSTEM_GUID,
(void **) &drive);
if (EFI_ERROR(efi_status))
goto done;
efi_status = drive->OpenVolume(drive, &root);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to open fs: %r\n", efi_status);
goto done;
}
efi_status = root->Open(root, &ofile, PathName, EFI_FILE_READ_ONLY, 0);
if (EFI_ERROR(efi_status)) {
if (efi_status != EFI_NOT_FOUND)
perror(L"Failed to open %s - %r\n", PathName, efi_status);
goto done;
}
dprint(L"Loading optional second stage info from options.csv\n");
efi_status = read_file(ofile, PathName, &buffer, &bs);
if (EFI_ERROR(efi_status)) {
perror(L"Failed to read file\n");
goto done;
}
/*
* This file may or may not start with the Unicode byte order marker.
* Since UEFI is defined as LE, this file must also be LE.
* If we find the LE byte order marker, just skip its.
*/
if (*buffer == 0xfeff)
buffer++;
comma0 = StrCSpn(buffer, L",");
if (comma0 == 0) {
perror(L"Invalid csv file\n");
goto done;
}
/*
* Currently the options.csv file allows one entry for the optional
* secondary boot stage, anything afterwards is skipped.
*/
buffer[comma0] = L'\0';
dprint(L"Optional 2nd stage:\"%s\"\n", buffer);
optional_second_stage=buffer;
done:
FreePool(PathName);
return EFI_SUCCESS;
}
EFI_STATUS
shim_init(void)
{