Skip to contentSkip to conversation

Add TPM support

PATCH In review Matthew Garrett 3 participants 8 messages Jun 15, 2017

lore
Share & tools
lore
#0 lore raw

This patchset reworks my earlier TPM support to use the verifiers framework. It only includes UEFI support right now due to the unclear copyright situation on the BIOS code from trusted-grub.

#1 lore raw

PATCH 1/3 Move verifiers to the kernel

We want to be able to measure stuff right from the very beginning of grub execution, so it makes sense for the core verifiers code to be present in-kernel rather than having it as an external module.

---
 grub-core/Makefile.am                              |  1 +
 grub-core/Makefile.core.def                        |  6 +---
 grub-core/kern/main.c                              |  3 ++
 .../{commands/verify_helper.c => kern/verifiers.c} | 35 +++++++++++++++++-----
 include/grub/verify.h                              | 16 +++-------
 5 files changed, 36 insertions(+), 25 deletions(-)
 rename grub-core/{commands/verify_helper.c => kern/verifiers.c} (78%)
grub-core/Makefile.am +1 -0
index 104513847..ef2b66e0f 100644
@@ -93,6 +93,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/net.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/memory.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/verify.h
 
 if COND_i386_pc
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
grub-core/Makefile.core.def +1 -5
index 16c4d0ea5..ab0f29960 100644
@@ -129,6 +129,7 @@ kernel = {
   common = kern/rescue_parser.c;
   common = kern/rescue_reader.c;
   common = kern/term.c;
+  common = kern/verifiers.c;
 
   noemu = kern/compiler-rt.c;
   noemu = kern/mm.c;
@@ -899,11 +900,6 @@ module = {
   cppflags = '-I$(srcdir)/lib/posix_wrap';
 };
 
-module = {
-  name = verify_helper;
-  common = commands/verify_helper.c;
-};
-
 module = {
   name = hdparm;
   common = commands/hdparm.c;
grub-core/kern/main.c +3 -0
index 9cad0c448..b5a43c155 100644
@@ -29,6 +29,7 @@
 #include <grub/command.h>
 #include <grub/reader.h>
 #include <grub/parser.h>
+#include <grub/verify.h>
 
 #ifdef GRUB_MACHINE_PCBIOS
 #include <grub/machine/memory.h>
@@ -274,6 +275,8 @@ grub_main (void)
   grub_printf ("Welcome to GRUB!\n\n");
   grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
 
+  grub_file_filter_register (GRUB_FILE_FILTER_VERIFY, grub_verify_helper_open);
+
   grub_load_config ();
 
   grub_boot_time ("Before loading embedded modules.");
grub-core/kern/verifiers.c renamed from grub-core/commands/verify_helper.c +27 -8
similarity index 78%
rename from grub-core/commands/verify_helper.c
rename to grub-core/kern/verifiers.c
index 5a55927e3..f8e47b009 100644
@@ -1,8 +1,25 @@
+/* verifiers.c - core verifiers support code */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
+ *
+ *  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/file.h>
 #include <grub/verify.h>
-#include <grub/dl.h>
-
-GRUB_MOD_LICENSE ("GPLv3+");
+#include <grub/misc.h>
 
 struct grub_file_verifier *grub_file_verifiers;
 
@@ -55,7 +72,7 @@ struct grub_fs verified_fs =
   .close = verified_close
 };
 
-static grub_file_t
+grub_file_t
 grub_verify_helper_open (grub_file_t io, enum grub_file_type type)
 {
   grub_verified_t verified = 0;
@@ -175,12 +192,14 @@ grub_verify_string (char *str, enum grub_verify_string_type type)
   return GRUB_ERR_NONE;
 }
 
-GRUB_MOD_INIT(verify_helper)
+void
+grub_verifier_register (struct grub_file_verifier *ver)
 {
-  grub_file_filter_register (GRUB_FILE_FILTER_VERIFY, grub_verify_helper_open);
+  grub_list_push (GRUB_AS_LIST_P (&grub_file_verifiers), GRUB_AS_LIST (ver));
 }
 
-GRUB_MOD_FINI(verify_helper)
+void
+grub_verifier_unregister (struct grub_file_verifier *ver)
 {
-  grub_file_filter_unregister (GRUB_FILE_FILTER_VERIFY);
+  grub_list_remove (GRUB_AS_LIST (ver));
 }
include/grub/verify.h +4 -12
index acab4f437..891036eb4 100644
@@ -37,17 +37,9 @@ struct grub_file_verifier
 
 extern struct grub_file_verifier *grub_file_verifiers;
 
-static inline void
-grub_verifier_register (struct grub_file_verifier *ver)
-{
-  grub_list_push (GRUB_AS_LIST_P (&grub_file_verifiers), GRUB_AS_LIST (ver));
-}
-
-static inline void
-grub_verifier_unregister (struct grub_file_verifier *ver)
-{
-  grub_list_remove (GRUB_AS_LIST (ver));
-}
+grub_file_t grub_verify_helper_open(grub_file_t io, enum grub_file_type type);
 
+void EXPORT_FUNC(grub_verifier_register) (struct grub_file_verifier *ver);
+void EXPORT_FUNC(grub_verifier_unregister) (struct grub_file_verifier *ver);
 grub_err_t
-grub_verify_string (char *str, enum grub_verify_string_type type);
+EXPORT_FUNC(grub_verify_string) (char *str, enum grub_verify_string_type type);
Signature · 1 line
2.13.1.518.g3df882009-goog
#6 lore raw

PATCH 2/3 Verify commands executed by grub

In reply to Matthew Garrett

Pass commands to the verification code. We want to be able to log these in the TPM verification case.

---
 grub-core/script/execute.c | 27 ++++++++++++++++++++++++---
 include/grub/verify.h      |  1 +
 2 files changed, 25 insertions(+), 3 deletions(-)
grub-core/script/execute.c +24 -3
index a8502d907..ee299fd0e 100644
@@ -27,6 +27,7 @@
 #include <grub/normal.h>
 #include <grub/extcmd.h>
 #include <grub/i18n.h>
+#include <grub/verify.h>
 
 /* Max digits for a char is 3 (0xFF is 255), similarly for an int it
    is sizeof (int) * 3, and one extra for a possible -ve sign.  */
@@ -929,8 +930,9 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
   grub_err_t ret = 0;
   grub_script_function_t func = 0;
   char errnobuf[18];
-  char *cmdname;
-  int argc;
+  char *cmdname, *cmdstring;
+  int argc, offset = 0, cmdlen = 0;
+  unsigned int i;
   char **args;
   int invert;
   struct grub_script_argv argv = { 0, 0, 0 };
@@ -939,6 +941,26 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
   if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0])
     return grub_errno;
 
+  for (i = 0; i < argv.argc; i++)
+    {
+      cmdlen += grub_strlen (argv.args[i]) + 1;
+    }
+
+  cmdstring = grub_malloc (cmdlen);
+  if (!cmdstring)
+    {
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+			 N_("cannot allocate command buffer"));
+    }
+
+  for (i = 0; i < argv.argc; i++)
+    {
+      offset += grub_snprintf (cmdstring + offset, cmdlen - offset, "%s ",
+			       argv.args[i]);
+    }
+  cmdstring[cmdlen - 1] = '\0';
+  grub_verify_string (cmdstring, GRUB_VERIFY_COMMAND);
+  grub_free (cmdstring);
   invert = 0;
   argc = argv.argc - 1;
   args = argv.args + 1;
@@ -1163,4 +1185,3 @@ grub_script_execute (struct grub_script *script)
 
   return grub_script_execute_cmd (script->cmd);
 }
-
include/grub/verify.h +1 -0
index 891036eb4..317b6efaf 100644
@@ -11,6 +11,7 @@ enum grub_verify_string_type
   {
     GRUB_VERIFY_KERNEL_CMDLINE,
     GRUB_VERIFY_MODULE_CMDLINE,
+    GRUB_VERIFY_COMMAND,
   };
 
 struct grub_file_verifier
Signature · 1 line
2.13.1.518.g3df882009-goog
#7 lore raw

PATCH 3/3 Core TPM support

In reply to Matthew Garrett

Add support for performing basic TPM measurements. Right now this only supports extending PCRs statically and only on UEFI.

---
 grub-core/Makefile.am          |   1 +
 grub-core/Makefile.core.def    |   2 +
 grub-core/kern/efi/tpm.c       | 282 +++++++++++++++++++++++++++++++++++++++++
 grub-core/kern/i386/efi/init.c |   2 +
 grub-core/kern/tpm.c           |  81 ++++++++++++
 include/grub/efi/tpm.h         | 153 ++++++++++++++++++++++
 include/grub/tpm.h             |  94 ++++++++++++++
 7 files changed, 615 insertions(+)
 create mode 100644 grub-core/kern/efi/tpm.c
 create mode 100644 grub-core/kern/tpm.c
 create mode 100644 include/grub/efi/tpm.h
 create mode 100644 include/grub/tpm.h
grub-core/Makefile.am +1 -0
index ef2b66e0f..cc641bc36 100644
@@ -92,6 +92,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/net.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/tpm.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/memory.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/verify.h
 
grub-core/Makefile.core.def +2 -0
index ab0f29960..2bdb6cafd 100644
@@ -129,6 +129,7 @@ kernel = {
   common = kern/rescue_parser.c;
   common = kern/rescue_reader.c;
   common = kern/term.c;
+  common = kern/tpm.c;
   common = kern/verifiers.c;
 
   noemu = kern/compiler-rt.c;
@@ -192,6 +193,7 @@ kernel = {
   efi = term/efi/console.c;
   efi = kern/acpi.c;
   efi = kern/efi/acpi.c;
+  efi = kern/efi/tpm.c;
   i386_coreboot = kern/i386/pc/acpi.c;
   i386_multiboot = kern/i386/pc/acpi.c;
   i386_coreboot = kern/acpi.c;
grub-core/kern/efi/tpm.c new file +282 -0

282 lines not on this page · raw message

grub-core/kern/i386/efi/init.c +2 -0
index a28316cc6..7306df91e 100644
@@ -27,12 +27,14 @@
 #include <grub/efi/efi.h>
 #include <grub/i386/tsc.h>
 #include <grub/loader.h>
+#include <grub/tpm.h>
 
 void
 grub_machine_init (void)
 {
   grub_efi_init ();
   grub_tsc_init ();
+  grub_tpm_init ();
 }
 
 void
grub-core/kern/tpm.c new file +81 -0

81 lines not on this page · raw message

include/grub/efi/tpm.h new file +153 -0

153 lines not on this page · raw message

include/grub/tpm.h new file +94 -0

94 lines not on this page · raw message

Signature · 1 line
2.13.1.518.g3df882009-goog
#2 lore raw

Re: [PATCH 1/3] Move verifiers to the kernel

In reply to Matthew Garrett

On Jun 15, 2017 2:43 AM, "Matthew Garrett" <[email protected]> wrote:

We want to be able to measure stuff right from the very beginning of grub execution, so it makes sense for the core verifiers code to be present in-kernel rather than having it as an external module.

This bid at odds with the need to keep kernel small. Why not just put verifiers as the first module to load? Presumably you need to verify the whole core in either case.

---
 grub-core/Makefile.am                              |  1 +
 grub-core/Makefile.core.def                        |  6 +---
 grub-core/kern/main.c                              |  3 ++
 .../{commands/verify_helper.c => kern/verifiers.c} | 35
+++++++++++++++++-----
 include/grub/verify.h                              | 16 +++-------
 5 files changed, 36 insertions(+), 25 deletions(-)
 rename grub-core/{commands/verify_helper.c => kern/verifiers.c} (78%)
grub-core/Makefile.am +1 -0
index 104513847..ef2b66e0f 100644
@@ -93,6 +93,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/net.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/memory.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/verify.h
 
 if COND_i386_pc
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
grub-core/Makefile.core.def +1 -5
index 16c4d0ea5..ab0f29960 100644
@@ -129,6 +129,7 @@ kernel = {
   common = kern/rescue_parser.c;
   common = kern/rescue_reader.c;
   common = kern/term.c;
+  common = kern/verifiers.c;
 
   noemu = kern/compiler-rt.c;
   noemu = kern/mm.c;
@@ -899,11 +900,6 @@ module = {
   cppflags = '-I$(srcdir)/lib/posix_wrap';
 };
 
-module = {
-  name = verify_helper;
-  common = commands/verify_helper.c;
-};
-
 module = {
   name = hdparm;
   common = commands/hdparm.c;
grub-core/kern/main.c +2 -0
index 9cad0c448..b5a43c155 100644
@@ -29,6 +29,7 @@
 #include <grub/command.h>
 #include <grub/reader.h>
 #include <grub/parser.h>
+#include <grub/verify.h>
 
 #ifdef GRUB_MACHINE_PCBIOS
 #include <grub/machine/memory.h>
@@ -274,6 +275,8 @@ grub_main (void)
   grub_printf ("Welcome to GRUB!\n\n");
   grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
 
+  grub_file_filter_register (GRUB_FILE_FILTER_VERIFY,

grub_verify_helper_open);
+
grub_load_config ();

grub_boot_time ("Before loading embedded modules.");

grub-core/kern/verifiers.c renamed from grub-core/commands/verify_helper.c +21 -4
similarity index 78%
rename from grub-core/commands/verify_helper.c
rename to grub-core/kern/verifiers.c
index 5a55927e3..f8e47b009 100644
@@ -1,8 +1,25 @@
+/* verifiers.c - core verifiers support code */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009  Free Software Foundation, Inc.
+ *
+ *  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/file.h>
 #include <grub/verify.h>
-#include <grub/dl.h>
-
-GRUB_MOD_LICENSE ("GPLv3+");
+#include <grub/misc.h>
 
 struct grub_file_verifier *grub_file_verifiers;
 
@@ -55,7 +72,7 @@ struct grub_fs verified_fs =
   .close = verified_close
 };
 
-static grub_file_t
+grub_file_t
 grub_verify_helper_open (grub_file_t io, enum grub_file_type type)
 {
   grub_verified_t verified = 0;
@@ -175,12 +192,14 @@ grub_verify_string (char *str, enum

grub_verify_string_type type)
return GRUB_ERR_NONE;
}

-GRUB_MOD_INIT(verify_helper)
+void
+grub_verifier_register (struct grub_file_verifier *ver)
{
- grub_file_filter_register (GRUB_FILE_FILTER_VERIFY, grub_verify_helper_open);
+ grub_list_push (GRUB_AS_LIST_P (&grub_file_verifiers), GRUB_AS_LIST (ver));
}

-GRUB_MOD_FINI(verify_helper)
+void
+grub_verifier_unregister (struct grub_file_verifier *ver)
{
- grub_file_filter_unregister (GRUB_FILE_FILTER_VERIFY);
+ grub_list_remove (GRUB_AS_LIST (ver));
}

include/grub/verify.h +0 -4
index acab4f437..891036eb4 100644
@@ -37,17 +37,9 @@ struct grub_file_verifier
 
 extern struct grub_file_verifier *grub_file_verifiers;
 
-static inline void
-grub_verifier_register (struct grub_file_verifier *ver)
-{
-  grub_list_push (GRUB_AS_LIST_P (&grub_file_verifiers), GRUB_AS_LIST

(ver));
-}
-
-static inline void
-grub_verifier_unregister (struct grub_file_verifier *ver) -{
- grub_list_remove (GRUB_AS_LIST (ver));
-}
+grub_file_t grub_verify_helper_open(grub_file_t io, enum grub_file_type type);

+void EXPORT_FUNC(grub_verifier_register) (struct grub_file_verifier *ver); +void EXPORT_FUNC(grub_verifier_unregister) (struct grub_file_verifier *ver);
grub_err_t
-grub_verify_string (char *str, enum grub_verify_string_type type); +EXPORT_FUNC(grub_verify_string) (char *str, enum grub_verify_string_type type);
--
2.13.1.518.g3df882009-goog

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw

Re: [PATCH 1/3] Move verifiers to the kernel

Quoted 3 lines · Vladimir 'phcoder' Serbinenko

This bid at odds with the need to keep kernel small. Why not just put verifiers as the first module to load? Presumably you need to verify the whole core in either case.

They're not useful as an external module, so they need to be built into the core image in any case (otherwise an attacker just replaces the verifier module…). And if you're making the ordering significant, it's far too easy for someone to mess up and end up with an insecure system as a result.

Signature · 1 line
Matthew Garrett | [email protected]
#4 lore raw

Re: [PATCH 1/3] Move verifiers to the kernel

Quoted 10 lines · On Thu, Jun 15, 2017, 03:49 Matthew Garrett <[email protected]> wrote:
Quoted 3 lines · Vladimir 'phcoder' Serbinenko

This bid at odds with the need to keep kernel small. Why not just put verifiers as the first module to load? Presumably you need to verify the whole core in either case.

They're not useful as an external module, so they need to be built into the core image in any case (otherwise an attacker just replaces the verifier module…).

Yes, part of core image, that's what I meant

Quoted 4 lines

if you're making the ordering significant,
it's far too easy for someone to mess up and end up with an insecure system as a result.

Adding them would be part of grub-install, not manual by user.

Quoted 9 lines

--
Matthew Garrett | [email protected]

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw

Re: [PATCH 1/3] Move verifiers to the kernel

Quoted 6 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · On Thu, Jun 15, 2017, 03:49 Matthew Garrett <[email protected]> wrote:

if you're making the ordering significant,
it's far too easy for someone to mess up and end up with an insecure system as a result.

Adding them would be part of grub-install, not manual by user.

Hm. Is there any way for a module to detect that it's being loaded rather than being part of the core, and throw an error in that case?

Signature · 1 line
Matthew Garrett | [email protected]