-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1117-i386-slaunch-Add-secure-launch-framework-and-command.patch
1098 lines (1042 loc) · 35.4 KB
/
1117-i386-slaunch-Add-secure-launch-framework-and-command.patch
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
From cf61e4e0ca2f76b6b306bf3e88faac028e427b12 Mon Sep 17 00:00:00 2001
From: Ross Philipson <[email protected]>
Date: Wed, 7 Aug 2019 15:01:00 -0400
Subject: [PATCH] i386/slaunch: Add secure launch framework and commands
Signed-off-by: Ross Philipson <[email protected]>
Signed-off-by: Daniel Kiper <[email protected]>
Signed-off-by: Krystian Hebel <[email protected]>
---
grub-core/Makefile.am | 6 +
grub-core/Makefile.core.def | 15 +
grub-core/lib/i386/relocator32.S | 8 +
grub-core/loader/i386/bsd.c | 4 +
grub-core/loader/i386/coreboot/chainloader.c | 2 +
grub-core/loader/i386/linux.c | 320 ++++++++++++++++++-
grub-core/loader/i386/pc/plan9.c | 3 +-
grub-core/loader/i386/slaunch.c | 304 ++++++++++++++++++
grub-core/loader/i386/xnu.c | 3 +
grub-core/loader/multiboot.c | 5 +
include/grub/file.h | 3 +
include/grub/i386/linux.h | 14 +-
12 files changed, 670 insertions(+), 17 deletions(-)
create mode 100644 grub-core/loader/i386/slaunch.c
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index ee88e44e97a0..44ba9ac40538 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -103,6 +103,8 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/pxe.h
KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/int.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/slaunch.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/slr_table.h
endif
if COND_i386_xen_pvh
@@ -122,6 +124,8 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/slaunch.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/slr_table.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pmtimer.h
@@ -183,6 +187,8 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/slaunch.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/slr_table.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pmtimer.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index f482cc01e36e..7ebacaa3cefe 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1816,6 +1816,15 @@ module = {
enable = noemu;
};
+module = {
+ name = slaunch;
+ x86 = loader/i386/slaunch.c;
+ x86 = loader/i386/txt/txt.c;
+ x86 = loader/i386/txt/acmod.c;
+ x86 = loader/i386/txt/verify.c;
+ enable = x86;
+};
+
module = {
name = fdt;
efi = loader/efi/fdt.c;
@@ -2489,6 +2498,12 @@ module = {
common = commands/testspeed.c;
};
+module = {
+ name = tpm;
+ x86 = commands/i386/tpm.c;
+ enable = x86;
+};
+
module = {
name = tpm_verifier;
common = commands/tpm_verifier.c;
diff --git a/grub-core/lib/i386/relocator32.S b/grub-core/lib/i386/relocator32.S
index 09ce56ad0ae6..a2b377197b16 100644
--- a/grub-core/lib/i386/relocator32.S
+++ b/grub-core/lib/i386/relocator32.S
@@ -24,6 +24,8 @@
#include "relocator_common.S"
+#include <grub/i386/slaunch.h>
+
.p2align 4 /* force 16-byte alignment */
VARIABLE(grub_relocator32_start)
@@ -110,11 +112,17 @@ VARIABLE(grub_relocator32_edx)
payload and makes this implementation easier. */
cld
+ cmpl $SLP_INTEL_TXT, %edi
+ je LOCAL(intel_txt)
+
.byte 0xea
VARIABLE(grub_relocator32_eip)
.long 0
.word CODE_SEGMENT
+LOCAL(intel_txt):
+ getsec
+
/* GDT. Copied from loader/i386/linux.c. */
.p2align 4
LOCAL(gdt):
diff --git a/grub-core/loader/i386/bsd.c b/grub-core/loader/i386/bsd.c
index 5f3290ce17bc..d9832328d307 100644
--- a/grub-core/loader/i386/bsd.c
+++ b/grub-core/loader/i386/bsd.c
@@ -21,6 +21,7 @@
#include <grub/i386/cpuid.h>
#include <grub/memory.h>
#include <grub/i386/memory.h>
+#include <grub/i386/slaunch.h>
#include <grub/file.h>
#include <grub/err.h>
#include <grub/dl.h>
@@ -792,6 +793,7 @@ grub_freebsd_boot (void)
#endif
grub_memcpy (&stack[9], &bi, sizeof (bi));
+ state.edi = SLP_NONE;
state.eip = entry;
state.esp = stack_target;
state.ebp = stack_target;
@@ -907,6 +909,7 @@ grub_openbsd_boot (void)
return err;
#endif
+ state.edi = SLP_NONE;
state.eip = entry;
state.ebp = state.esp
= ((grub_uint8_t *) stack - (grub_uint8_t *) buf0) + buf_target;
@@ -1229,6 +1232,7 @@ grub_netbsd_boot (void)
return err;
#endif
+ state.edi = SLP_NONE;
state.eip = entry;
state.esp = stack_target;
state.ebp = stack_target;
diff --git a/grub-core/loader/i386/coreboot/chainloader.c b/grub-core/loader/i386/coreboot/chainloader.c
index 0a19ebb9c3e5..dccd86b07bde 100644
--- a/grub-core/loader/i386/coreboot/chainloader.c
+++ b/grub-core/loader/i386/coreboot/chainloader.c
@@ -33,6 +33,7 @@
#include <grub/lib/LzmaDec.h>
#include <grub/efi/pe32.h>
#include <grub/i386/cpuid.h>
+#include <grub/i386/slaunch.h>
GRUB_MOD_LICENSE ("GPLv3+");
@@ -47,6 +48,7 @@ grub_chain_boot (void)
grub_video_set_mode ("text", 0, 0);
state.eip = entry;
+ state.edi = SLP_NONE;
return grub_relocator32_boot (relocator, state, 0);
}
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
index 9f74a96b19ae..bf3878ab91e2 100644
--- a/grub-core/loader/i386/linux.c
+++ b/grub-core/loader/i386/linux.c
@@ -34,9 +34,12 @@
#include <grub/i386/relocator.h>
#include <grub/i18n.h>
#include <grub/lib/cmdline.h>
+#include <grub/i386/slaunch.h>
+#include <grub/i386/txt.h>
#include <grub/linux.h>
#include <grub/machine/kernel.h>
#include <grub/safemath.h>
+#include <grub/slr_table.h>
GRUB_MOD_LICENSE ("GPLv3+");
@@ -63,18 +66,36 @@ GRUB_MOD_LICENSE ("GPLv3+");
#define ACCEPTS_PURE_TEXT 1
#endif
+/* See kernel_info in Documentation/arch/x86/boot.rst in the kernel tree */
+#define KERNEL_INFO_HEADER "LToP"
+#define KERNEL_INFO_MIN_SIZE_TOTAL 12
+
+struct linux_params_efi_info
+{
+ grub_uint32_t efi_signature;
+ grub_uint32_t efi_system_table;
+ grub_uint32_t efi_mem_desc_size;
+ grub_uint32_t efi_mem_desc_version;
+ grub_uint32_t efi_mmap;
+ grub_uint32_t efi_mmap_size;
+ grub_uint32_t efi_system_table_hi;
+ grub_uint32_t efi_mmap_hi;
+};
+
static grub_dl_t my_mod;
static grub_size_t linux_mem_size;
static int loaded;
static void *prot_mode_mem;
static grub_addr_t prot_mode_target;
+static grub_size_t prot_file_size;
static void *initrd_mem;
static grub_addr_t initrd_mem_target;
static grub_size_t prot_init_space;
static struct grub_relocator *relocator = NULL;
static void *efi_mmap_buf;
static grub_size_t maximal_cmdline_size;
+static struct linux_kernel_info *linux_info;
static struct linux_kernel_params linux_params;
static char *linux_cmdline;
#ifdef GRUB_MACHINE_EFI
@@ -98,6 +119,8 @@ static struct idt_descriptor idt_desc =
};
#endif
+#define OFFSET_OF(x, y) ((grub_size_t)((grub_uint8_t *)(&(y)->x) - (grub_uint8_t *)(y)))
+
static inline grub_size_t
page_align (grub_size_t size)
{
@@ -150,11 +173,37 @@ allocate_pages (grub_size_t prot_size, grub_size_t *align,
grub_uint64_t preferred_address)
{
grub_err_t err;
+ grub_size_t total_size;
+ struct grub_slaunch_params *slparams = grub_slaunch_params();
if (prot_size == 0)
prot_size = 1;
- prot_size = page_align (prot_size);
+ if (grub_slaunch_platform_type () == SLP_INTEL_TXT)
+ {
+ if (prot_size > GRUB_TXT_MLE_MAX_SIZE)
+ {
+ err = GRUB_ERR_OUT_OF_RANGE;
+ goto fail;
+ }
+
+ /* Check performed above makes sure that this doesn't overflow. */
+ prot_size = ALIGN_UP (prot_size, GRUB_TXT_PMR_ALIGN);
+
+ slparams->mle_ptab_size = grub_txt_get_mle_ptab_size (prot_size);
+ slparams->mle_ptab_size = ALIGN_UP (slparams->mle_ptab_size, GRUB_TXT_PMR_ALIGN);
+ /* Do not go below GRUB_TXT_PMR_ALIGN. */
+ preferred_address = (preferred_address > slparams->mle_ptab_size) ?
+ (preferred_address - slparams->mle_ptab_size) : GRUB_TXT_PMR_ALIGN;
+ preferred_address = ALIGN_UP (preferred_address, GRUB_TXT_PMR_ALIGN);
+ }
+ else
+ {
+ prot_size = page_align (prot_size);
+ slparams->mle_ptab_size = 0;
+ }
+
+ total_size = prot_size + slparams->mle_ptab_size;
/* Initialize the memory pointers with NULL for convenience. */
free_pages ();
@@ -176,7 +225,7 @@ allocate_pages (grub_size_t prot_size, grub_size_t *align,
err = grub_relocator_alloc_chunk_align (relocator, &ch,
preferred_address,
preferred_address,
- prot_size, 1,
+ total_size, 1,
GRUB_RELOCATOR_PREFERENCE_LOW,
1);
for (; err && *align + 1 > min_align; (*align)--)
@@ -194,11 +243,65 @@ allocate_pages (grub_size_t prot_size, grub_size_t *align,
else
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
preferred_address,
- prot_size);
+ total_size);
if (err)
goto fail;
prot_mode_mem = get_virtual_current_address (ch);
prot_mode_target = get_physical_target_address (ch);
+
+ if (grub_slaunch_platform_type () == SLP_INTEL_TXT)
+ {
+ /* Zero out memory to get stable MLE measurements. */
+ grub_memset (prot_mode_mem, 0, total_size);
+
+ slparams->mle_ptab_mem = prot_mode_mem;
+ slparams->mle_ptab_target = prot_mode_target;
+
+ prot_mode_mem = (char *)prot_mode_mem + slparams->mle_ptab_size;
+ prot_mode_target += slparams->mle_ptab_size;
+
+ slparams->mle_start = prot_mode_target;
+ slparams->mle_size = prot_size;
+ slparams->mle_mem = prot_mode_mem;
+
+ grub_dprintf ("linux", "mle_ptab_mem = %p, mle_ptab_target = %lx, mle_ptab_size = %x\n",
+ slparams->mle_ptab_mem, (unsigned long) slparams->mle_ptab_target,
+ (unsigned) slparams->mle_ptab_size);
+
+ err = grub_relocator_alloc_chunk_align (relocator, &ch, 0x1000000,
+ 0xffffffff - GRUB_PAGE_SIZE,
+ GRUB_PAGE_SIZE, GRUB_PAGE_SIZE,
+ GRUB_RELOCATOR_PREFERENCE_NONE, 1);
+ if (err)
+ goto fail;
+
+ slparams->slr_table_base = get_physical_target_address (ch);
+ slparams->slr_table_size = GRUB_PAGE_SIZE;
+ slparams->slr_table_mem = get_virtual_current_address (ch);
+
+ grub_memset (slparams->slr_table_mem, 0, slparams->slr_table_size);
+
+ grub_dprintf ("linux", "slr_table_base = %lx, slr_table_size = %x\n",
+ (unsigned long) slparams->slr_table_base,
+ (unsigned) slparams->slr_table_size);
+
+ err = grub_relocator_alloc_chunk_align (relocator, &ch, 0x1000000,
+ 0xffffffff - GRUB_SLAUNCH_TPM_EVT_LOG_SIZE,
+ GRUB_SLAUNCH_TPM_EVT_LOG_SIZE, GRUB_PAGE_SIZE,
+ GRUB_RELOCATOR_PREFERENCE_NONE, 1);
+ if (err)
+ goto fail;
+
+ slparams->tpm_evt_log_base = get_physical_target_address (ch);
+ slparams->tpm_evt_log_size = GRUB_SLAUNCH_TPM_EVT_LOG_SIZE;
+
+ grub_txt_init_tpm_event_log (get_virtual_current_address (ch),
+ slparams->tpm_evt_log_size);
+
+ grub_dprintf ("linux", "tpm_evt_log_base = %lx, tpm_evt_log_size = %x\n",
+ (unsigned long) slparams->tpm_evt_log_base,
+ (unsigned) slparams->tpm_evt_log_size);
+ }
}
grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
@@ -286,7 +389,7 @@ grub_linux_setup_video (struct linux_kernel_params *params)
params->lfb_size >>= 16;
params->have_vga = GRUB_VIDEO_LINUX_TYPE_VESA;
break;
-
+
case GRUB_VIDEO_DRIVER_EFI_UGA:
case GRUB_VIDEO_DRIVER_EFI_GOP:
params->have_vga = GRUB_VIDEO_LINUX_TYPE_EFIFB;
@@ -398,6 +501,63 @@ grub_linux_boot_mmap_fill (grub_uint64_t addr, grub_uint64_t size,
return 0;
}
+static void
+grub_linux_setup_slr_table (struct grub_slaunch_params *slparams)
+{
+ struct linux_kernel_params *boot_params = (void *) (grub_addr_t) slparams->boot_params_addr;
+ struct linux_params_efi_info *efi_info;
+
+ /* A bit of work to extract the v2.08 EFI info from the linux params */
+ efi_info = (void *)((grub_uint8_t *)&boot_params->v0208 + 2*sizeof(grub_uint32_t));
+
+ grub_slaunch_add_slrt_policy_entry (GRUB_SLAUNCH_DATA_PCR,
+ GRUB_SLR_ET_BOOT_PARAMS,
+ /*flags=*/0,
+ (grub_addr_t) boot_params,
+ GRUB_PAGE_SIZE,
+ "Measured boot parameters");
+
+ if (boot_params->setup_data)
+ grub_slaunch_add_slrt_policy_entry (GRUB_SLAUNCH_DATA_PCR,
+ GRUB_SLR_ET_SETUP_DATA,
+ GRUB_SLR_POLICY_IMPLICIT_SIZE,
+ boot_params->setup_data,
+ /*size=*/0,
+ "Measured Kernel setup_data");
+
+ /* The cmdline ptr can have hi bits but GRUB puts it always < 4G */
+ grub_slaunch_add_slrt_policy_entry (GRUB_SLAUNCH_DATA_PCR,
+ GRUB_SLR_ET_CMDLINE,
+ /*flags=*/0,
+ boot_params->cmd_line_ptr,
+ boot_params->cmdline_size,
+ "Measured Kernel command line");
+
+ if (!grub_memcmp(&efi_info->efi_signature, "EL64", sizeof(grub_uint32_t)))
+ {
+ grub_uint64_t mmap_addr =
+ ((grub_uint64_t) efi_info->efi_mmap_hi << 32) | efi_info->efi_mmap;
+ grub_slaunch_add_slrt_policy_entry (GRUB_SLAUNCH_DATA_PCR,
+ GRUB_SLR_ET_UEFI_MEMMAP,
+ /*flags=*/0,
+ mmap_addr,
+ efi_info->efi_mmap_size,
+ "Measured EFI memory map");
+ }
+
+ if (boot_params->ramdisk_image)
+ /*
+ * The initrd image and size can have hi bits but in GRUB it is always
+ * < 4G, see GRUB_LINUX_INITRD_MAX_ADDRESS in grub_cmd_initrd().
+ */
+ grub_slaunch_add_slrt_policy_entry (GRUB_SLAUNCH_CODE_PCR,
+ GRUB_SLR_ET_RAMDISK,
+ /*flags=*/0,
+ boot_params->ramdisk_image,
+ boot_params->ramdisk_size,
+ "Measured Kernel initrd");
+}
+
static grub_err_t
grub_linux_boot (void)
{
@@ -411,6 +571,8 @@ grub_linux_boot (void)
};
grub_size_t mmap_size;
grub_size_t cl_offset;
+ grub_size_t ap_wake_block_size = 0;
+ struct grub_slaunch_params *slparams = grub_slaunch_params();
#ifdef GRUB_MACHINE_IEEE1275
{
@@ -543,6 +705,9 @@ grub_linux_boot (void)
(unsigned) ctx.real_size,
(unsigned) efi_mmap_size);
+ if (grub_slaunch_platform_type () == SLP_INTEL_TXT)
+ ap_wake_block_size = GRUB_MLE_AP_WAKE_BLOCK_SIZE;
+
if (! ctx.real_mode_target)
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages");
@@ -550,7 +715,8 @@ grub_linux_boot (void)
grub_relocator_chunk_t ch;
grub_size_t sz;
- if (grub_add (ctx.real_size, efi_mmap_size, &sz))
+ if (grub_add (efi_mmap_size, ap_wake_block_size, &sz) ||
+ grub_add (ctx.real_size, sz, &sz))
return GRUB_ERR_OUT_OF_RANGE;
err = grub_relocator_alloc_chunk_addr (relocator, &ch,
@@ -561,6 +727,20 @@ grub_linux_boot (void)
}
efi_mmap_buf = (grub_uint8_t *) real_mode_mem + ctx.real_size;
+ if (grub_slaunch_platform_type () == SLP_INTEL_TXT)
+ {
+ slparams->ap_wake_block = ctx.real_mode_target + ctx.real_size + efi_mmap_size;
+ slparams->ap_wake_block_size = ap_wake_block_size;
+ grub_memset ((void *) ((grub_addr_t) real_mode_mem + ctx.real_size +
+ efi_mmap_size), 0, ap_wake_block_size);
+ grub_dprintf ("linux", "ap_wake_block = %lx, ap_wake_block_size = %lx\n",
+ (unsigned long) slparams->ap_wake_block,
+ (unsigned long) ap_wake_block_size);
+
+ /* Grab the real mode target address, this is the boot params page. */
+ slparams->boot_params_addr = ctx.real_mode_target;
+ }
+
grub_dprintf ("linux", "real_mode_mem = %p\n",
real_mode_mem);
@@ -587,13 +767,15 @@ grub_linux_boot (void)
ctx.params->secure_boot = grub_efi_get_secureboot ();
+ grub_dprintf ("linux", "EFI exit boot services\n");
+
err = grub_efi_finish_boot_services (&efi_mmap_size, efi_mmap_buf, NULL,
&efi_desc_size, &efi_desc_version);
if (err)
return err;
-
+
/* Note that no boot services are available from here. */
- efi_mmap_target = ctx.real_mode_target
+ efi_mmap_target = ctx.real_mode_target
+ ((grub_uint8_t *) efi_mmap_buf - (grub_uint8_t *) real_mode_mem);
/* Pass EFI parameters. */
if (grub_le_to_cpu16 (ctx.params->version) >= 0x0208)
@@ -624,12 +806,36 @@ grub_linux_boot (void)
}
#endif
- /* FIXME. */
- /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
- state.ebp = state.edi = state.ebx = 0;
- state.esi = ctx.real_mode_target;
- state.esp = ctx.real_mode_target;
- state.eip = ctx.params->code32_start;
+ state.edi = grub_slaunch_platform_type ();
+
+ if (state.edi == SLP_INTEL_TXT)
+ {
+ err = grub_txt_boot_prepare (slparams);
+
+ if (err != GRUB_ERR_NONE)
+ return err;
+
+ grub_slaunch_add_slrt_policy_entries ();
+ grub_txt_add_slrt_policy_entries ();
+ grub_linux_setup_slr_table (slparams);
+ grub_slaunch_finish_slr_table ();
+
+ /* Configure relocator GETSEC[SENTER] call. */
+ state.eax = GRUB_SMX_LEAF_SENTER;
+ state.ebx = slparams->dce_base;
+ state.ecx = slparams->dce_size;
+ state.edx = 0;
+ }
+ else
+ {
+ /* FIXME. */
+ /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
+ state.ebp = state.edi = state.ebx = 0;
+ state.esi = ctx.real_mode_target;
+ state.esp = ctx.real_mode_target;
+ state.eip = ctx.params->code32_start;
+ }
+
return grub_relocator32_boot (relocator, state, 0);
}
@@ -650,12 +856,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
grub_file_t file = 0;
struct linux_i386_kernel_header lh;
grub_uint8_t setup_sects;
- grub_size_t real_size, prot_size, prot_file_size;
+ grub_size_t real_size, prot_size;
grub_ssize_t len;
int i;
grub_size_t align, min_align;
int relocatable;
grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
+ struct grub_slaunch_params *slparams = grub_slaunch_params();
grub_dl_ref (my_mod);
@@ -743,7 +950,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
align = 0;
relocatable = 0;
}
-
+
if (grub_le_to_cpu16 (lh.version) >= 0x020a)
{
min_align = lh.min_alignment;
@@ -760,6 +967,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
prot_init_space = page_align (prot_size) * 3;
}
+ if (grub_slaunch_platform_type () == SLP_INTEL_TXT)
+ {
+ /* PMRs require GRUB_TXT_PMR_ALIGN_SHIFT aligments. */
+ min_align = grub_max (min_align, GRUB_TXT_PMR_ALIGN_SHIFT);
+ align = grub_max (align, GRUB_TXT_PMR_ALIGN_SHIFT);
+ }
+
if (allocate_pages (prot_size, &align,
min_align, relocatable,
preferred_address))
@@ -767,6 +981,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
grub_memset (&linux_params, 0, sizeof (linux_params));
+ if (grub_slaunch_platform_type () == SLP_INTEL_TXT)
+ grub_txt_setup_mle_ptab (grub_slaunch_params ());
+
/*
* The Linux 32-bit boot protocol defines the setup header end
* to be at 0x202 + the byte value at 0x201.
@@ -793,6 +1010,79 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
goto fail;
}
+ /* Read the kernel_info struct. */
+ if (grub_le_to_cpu16 (lh.version) >= 0x020f)
+ {
+ if (grub_file_seek (file, (grub_off_t) grub_le_to_cpu32 (lh.kernel_info_offset) +
+ real_size + GRUB_DISK_SECTOR_SIZE) == ((grub_off_t) -1))
+ goto fail;
+
+ linux_info = grub_malloc (KERNEL_INFO_MIN_SIZE_TOTAL);
+
+ if (!linux_info)
+ {
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate memory for kernel_info"));
+ goto fail;
+ }
+
+ /* Load minimal kernel_info struct. */
+ if (grub_file_read (file, linux_info,
+ KERNEL_INFO_MIN_SIZE_TOTAL) != KERNEL_INFO_MIN_SIZE_TOTAL)
+ {
+ if (!grub_errno)
+ grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
+ goto fail;
+ }
+
+ if (grub_memcmp (&linux_info->header, KERNEL_INFO_HEADER, sizeof (linux_info->header)))
+ {
+ grub_error (GRUB_ERR_BAD_OS, N_("incorrect kernel_info header"));
+ goto fail;
+ }
+
+ if (linux_info->size_total < KERNEL_INFO_MIN_SIZE_TOTAL)
+ {
+ grub_error (GRUB_ERR_BAD_OS, N_("incorrect kernel_info size"));
+ goto fail;
+ }
+
+ linux_info = grub_realloc (linux_info, linux_info->size_total);
+
+ if (!linux_info)
+ {
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot reallocate memory for kernel_info"));
+ goto fail;
+ }
+
+ /* Load the rest of kernel_info struct. */
+ if (grub_file_read (file, &linux_info->setup_type_max,
+ linux_info->size_total - KERNEL_INFO_MIN_SIZE_TOTAL) !=
+ (grub_ssize_t)(linux_info->size_total - KERNEL_INFO_MIN_SIZE_TOTAL))
+ {
+ if (!grub_errno)
+ grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
+ goto fail;
+ }
+
+ if (grub_slaunch_platform_type () != SLP_NONE)
+ {
+ if (OFFSET_OF (mle_header_offset, linux_info) >=
+ grub_le_to_cpu32 (linux_info->size))
+ {
+ if (!grub_errno)
+ grub_error (GRUB_ERR_BAD_OS, N_("not slaunch kernel: lack of mle_header_offset"));
+ goto fail;
+ }
+
+ slparams->mle_header_offset = grub_le_to_cpu32 (linux_info->mle_header_offset);
+ }
+ }
+ else if (grub_slaunch_platform_type () != SLP_NONE)
+ {
+ grub_error (GRUB_ERR_BAD_OS, N_("not slaunch kernel: boot protocol too old"));
+ goto fail;
+ }
+
linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
linux_params.kernel_alignment = (1 << align);
linux_params.ps_mouse = linux_params.padding11 = 0;
diff --git a/grub-core/loader/i386/pc/plan9.c b/grub-core/loader/i386/pc/plan9.c
index 37550155df78..cd8213a05d31 100644
--- a/grub-core/loader/i386/pc/plan9.c
+++ b/grub-core/loader/i386/pc/plan9.c
@@ -34,6 +34,7 @@
#include <grub/cpu/relocator.h>
#include <grub/extcmd.h>
#include <grub/verify.h>
+#include <grub/i386/slaunch.h>
GRUB_MOD_LICENSE ("GPLv3+");
@@ -84,7 +85,7 @@ grub_plan9_boot (void)
.ebx = 0,
.ecx = 0,
.edx = 0,
- .edi = 0,
+ .edi = SLP_NONE,
.esp = 0,
.ebp = 0,
.esi = 0
diff --git a/grub-core/loader/i386/slaunch.c b/grub-core/loader/i386/slaunch.c
new file mode 100644
index 000000000000..b15194c78b61
--- /dev/null
+++ b/grub-core/loader/i386/slaunch.c
@@ -0,0 +1,304 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2020 Oracle and/or its affiliates.
+ *
+ * GRUB is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GRUB is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/loader.h>
+#include <grub/memory.h>
+#include <grub/normal.h>
+#include <grub/err.h>
+#include <grub/misc.h>
+#include <grub/types.h>
+#include <grub/dl.h>
+#include <grub/slr_table.h>
+#include <grub/cpu/relocator.h>
+#include <grub/i386/cpuid.h>
+#include <grub/i386/msr.h>
+#include <grub/i386/mmio.h>
+#include <grub/i386/slaunch.h>
+#include <grub/i386/tpm.h>
+#include <grub/i386/txt.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_uint32_t slp = SLP_NONE;
+
+static void *slaunch_module = NULL;
+
+static struct grub_slaunch_params slparams;
+
+/* Area to collect and build SLR Table information. */
+static struct grub_slr_entry_dl_info slr_dl_info_staging;
+static struct grub_slr_entry_log_info slr_log_info_staging;
+static grub_uint8_t slr_policy_buf[GRUB_PAGE_SIZE];
+static struct grub_slr_entry_policy *slr_policy_staging =
+ (struct grub_slr_entry_policy *)slr_policy_buf;
+
+grub_uint32_t
+grub_slaunch_platform_type (void)
+{
+ return slp;
+}
+
+void *
+grub_slaunch_module (void)
+{
+ return slaunch_module;
+}
+
+struct grub_slaunch_params *
+grub_slaunch_params (void)
+{
+ return &slparams;
+}
+
+void
+grub_slaunch_init_slrt_storage (int arch)
+{
+ struct grub_txt_mle_header *mle_header =
+ (void *) ((grub_uint8_t *) slparams.mle_mem + slparams.mle_header_offset);
+
+ /* Setup the generic bits of the SLRT. */
+ grub_slr_init_table(slparams.slr_table_mem, arch, slparams.slr_table_size);
+
+ /* Setup DCE and DLME information. */
+ slr_dl_info_staging.hdr.tag = GRUB_SLR_ENTRY_DL_INFO;
+ slr_dl_info_staging.hdr.size = sizeof(struct grub_slr_entry_dl_info);
+ slr_dl_info_staging.dce_base = slparams.dce_base;
+ slr_dl_info_staging.dce_size = slparams.dce_size;
+ slr_dl_info_staging.dlme_entry = mle_header->entry_point;
+
+ slr_log_info_staging.hdr.tag = GRUB_SLR_ENTRY_LOG_INFO;
+ slr_log_info_staging.hdr.size = sizeof(struct grub_slr_entry_log_info);
+ slr_log_info_staging.addr = slparams.tpm_evt_log_base;
+ slr_log_info_staging.size = slparams.tpm_evt_log_size;
+ slr_log_info_staging.format =
+ (grub_get_tpm_ver () == GRUB_TPM_20) ?
+ GRUB_SLR_DRTM_TPM20_LOG : GRUB_SLR_DRTM_TPM12_LOG;
+
+ slr_policy_staging->hdr.tag = GRUB_SLR_ENTRY_DRTM_POLICY;
+ slr_policy_staging->hdr.size = sizeof(struct grub_slr_entry_policy);
+ slr_policy_staging->revision = GRUB_SLR_TABLE_REVISION;
+ slr_policy_staging->nr_entries = 0;
+}
+
+void grub_slaunch_add_slrt_policy_entries (void)
+{
+ /* The SLR table should be measured too, at least parts of it. */
+ grub_slaunch_add_slrt_policy_entry (GRUB_SLAUNCH_DATA_PCR,
+ GRUB_SLR_ET_SLRT,
+ GRUB_SLR_POLICY_IMPLICIT_SIZE,
+ slparams.slr_table_base,
+ /*size=*/0,
+ "Measured SLR Table");
+}
+
+void
+grub_slaunch_add_slrt_policy_entry (grub_uint16_t pcr,
+ grub_uint16_t entity_type,
+ grub_uint16_t flags,
+ grub_uint64_t entity,
+ grub_uint64_t size,
+ const char *evt_info)
+{
+ struct grub_slr_policy_entry *entry =
+ (void *)((grub_uint8_t *)slr_policy_staging +
+ sizeof(struct grub_slr_entry_policy) +
+ slr_policy_staging->nr_entries*sizeof(*entry));
+
+ if (slr_policy_staging->hdr.size > sizeof(slr_policy_buf) - sizeof(*entry))
+ grub_fatal("Not enough space for adding policy entry: %s! The buffer is full.",
+ evt_info);
+
+ entry->pcr = pcr;
+ entry->entity_type = entity_type;
+ entry->flags = flags;
+ entry->entity = entity;
+ entry->size = size;
+
+ grub_strncpy(entry->evt_info, evt_info, sizeof(entry->evt_info) - 1);
+ entry->evt_info[sizeof(entry->evt_info) - 1] = '\0';
+
+ slr_policy_staging->hdr.size += sizeof(*entry);
+ ++slr_policy_staging->nr_entries;
+}
+
+void
+grub_slaunch_finish_slr_table (void)
+{
+ struct grub_slr_table *slr_table = slparams.slr_table_mem;
+
+ grub_slr_add_entry (slr_table, &slr_dl_info_staging.hdr);
+ grub_slr_add_entry (slr_table, &slr_log_info_staging.hdr);
+ grub_slr_add_entry (slr_table, &slr_policy_staging->hdr);
+}
+
+static grub_err_t
+grub_cmd_slaunch (grub_command_t cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char *argv[] __attribute__ ((unused)))
+{
+ grub_uint32_t manufacturer[3];
+ grub_uint32_t eax;
+ grub_err_t err;
+
+ if (!grub_cpu_is_cpuid_supported ())
+ return grub_error (GRUB_ERR_BAD_DEVICE, N_("CPUID is unsupported"));
+
+ err = grub_cpu_is_msr_supported ();
+
+ if (err != GRUB_ERR_NONE)
+ return grub_error (err, N_("MSRs are unsupported"));
+
+ grub_cpuid (0, eax, manufacturer[0], manufacturer[2], manufacturer[1]);
+
+ if (!grub_memcmp (manufacturer, "GenuineIntel", 12))
+ {
+ err = grub_txt_init ();
+
+ if (err != GRUB_ERR_NONE)
+ return err;
+
+ slp = SLP_INTEL_TXT;
+ }
+ else
+ return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("CPU is unsupported"));
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_slaunch_module (grub_command_t cmd __attribute__ ((unused)),
+ int argc, char *argv[])
+{
+ grub_file_t file;
+ grub_ssize_t size;
+
+ if (argc != 1)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected: filename"));
+
+ if (slp == SLP_NONE)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("secure launch not enabled"));
+
+ if (slp != SLP_INTEL_TXT)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
+ N_("unknown secure launch platform type: %d"), slp);
+
+ grub_errno = GRUB_ERR_NONE;
+
+ file = grub_file_open (argv[0], GRUB_FILE_TYPE_SLAUNCH_MODULE);
+
+ if (file == NULL)
+ return grub_errno;
+
+ size = grub_file_size (file);
+
+ if (!size)
+ {
+ grub_error (GRUB_ERR_BAD_ARGUMENT, N_("file size is zero"));
+ goto fail;
+ }
+
+ slaunch_module = grub_malloc (size);
+
+ if (slaunch_module == NULL)
+ goto fail;
+
+ if (grub_file_read (file, slaunch_module, size) != size)
+ {
+ if (grub_errno == GRUB_ERR_NONE)
+ grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file: %s"),
+ argv[0]);
+ goto fail;
+ }
+
+ if (slp == SLP_INTEL_TXT)
+ {
+ if (!grub_txt_is_sinit_acmod (slaunch_module, size))
+ {
+ grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("it does not look like SINIT ACM"));
+ goto fail;
+ }
+
+ if (!grub_txt_acmod_match_platform (slaunch_module))
+ {
+ grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("SINIT ACM does not match platform"));
+ goto fail;
+ }
+ }
+
+ grub_file_close (file);
+
+ return GRUB_ERR_NONE;
+
+fail:
+ grub_error_push ();
+
+ grub_free (slaunch_module);
+ grub_file_close (file);
+
+ slaunch_module = NULL;
+
+ grub_error_pop ();
+
+ return grub_errno;
+}
+
+static grub_err_t
+grub_cmd_slaunch_state (grub_command_t cmd __attribute__ ((unused)),
+ int argc __attribute__ ((unused)),
+ char *argv[] __attribute__ ((unused)))
+{
+ if (slp == SLP_NONE)
+ grub_printf ("Secure launcher: Disabled\n");
+ else if (slp == SLP_INTEL_TXT)
+ {
+ grub_printf ("Secure launcher: Intel TXT\n");
+ grub_txt_state_show ();
+ }
+ else
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
+ N_("Unknown secure launcher platform type: %d\n"), slp);
+
+ return GRUB_ERR_NONE;
+}
+
+static grub_command_t cmd_slaunch, cmd_slaunch_module, cmd_slaunch_state;
+
+GRUB_MOD_INIT (slaunch)
+{
+ cmd_slaunch = grub_register_command ("slaunch", grub_cmd_slaunch,
+ NULL, N_("Enable secure launcher"));
+ cmd_slaunch_module = grub_register_command ("slaunch_module", grub_cmd_slaunch_module,
+ NULL, N_("Load secure launcher module from file"));
+ cmd_slaunch_state = grub_register_command ("slaunch_state", grub_cmd_slaunch_state,
+ NULL, N_("Display secure launcher state"));
+}
+
+GRUB_MOD_FINI (slaunch)
+{
+ if (cmd_slaunch_state)
+ grub_unregister_command (cmd_slaunch_state);
+
+ if (cmd_slaunch_module)
+ grub_unregister_command (cmd_slaunch_module);
+
+ if (cmd_slaunch)
+ grub_unregister_command (cmd_slaunch);
+
+ if (slp == SLP_INTEL_TXT)
+ grub_txt_shutdown ();
+}
diff --git a/grub-core/loader/i386/xnu.c b/grub-core/loader/i386/xnu.c
index a7009360732a..4989227bdbf5 100644
--- a/grub-core/loader/i386/xnu.c
+++ b/grub-core/loader/i386/xnu.c