Skip to contentSkip to conversation

Automatic Disk Unlock with TPM2

PATCH Superseded Gary Lin 3 participants 24 messages Feb 22, 2023 – Mar 7, 2023 Kbuild

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Gary Lin

author
#0 lore raw

cover Automatic Disk Unlock with TPM2

The patch series "Automatic TPM Disk Unlock" posted by Hernan Gatta introduces the key protector framework and TPM2 stack to GRUB2, and it's a useful feature for the systems to implement full disk encryption. However, it seems the development was stalled for a while, and I'd like to push it forward.

Patch 1~5 are Hernan Gatta's patch series(*) with a few modifications:
- Converting 8 spaces into 1 tab
- Merging the minor build fix from Michael Chang
- Replacing "lu" with "PRIuGRUB_SIZE" for grub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master

To minimize the changes to Patch 1~5, the follow-up fixes (Patch 6~14) from my colleagues and me are committed separately. Those patches fix the problems we found while testing the original patchset.

Quote from Hernan Gatta's cover letter:

"
Updates since v1:

1. One key can unlock multiple disks:
It is now possible to use key protectors with cryptomount's -a and -b options.

2. No passphrase prompt on error if key protector(s) specified:
cryptomount no longer prompts for a passphrase if key protectors are specified but fail to provide a working unlock key seeing as the user explicitly requested unlocking via key protectors.

3. Key protector parameterization is separate:
Previously, one would parameterize a key protector via a colon-separated argument list nested within a cryptomount argument. Now, key protectors are expected to provide an initialization function, if necessary.

As such, instead of:

cryptomount -k tpm2:mode=srk:keyfile=KEYFILE:pcrs=7,11...

one now writes:

tpm2_key_protector_init --mode=srk --keyfile=KEYFILE --pcrs=7,11 ... cryptomount -k tpm2

Additionally, one may write:

cryptomount -k protector_1 -k protector_2 ...

where cryptomount will try each in order on failure.

4. Standard argument parsing:
The TPM2 key protector now uses 'struct grub_arg_option' and the grub-protect tool uses 'struct argp_option'. Additionally, common argument parsing functionality is now shared between the module and the tool.

5. More useful messages:
Both the TPM2 module and the grub-protect tool now provide more useful messages to help the user learn how to use their functionality (--help and --usage) as well as to determine what is wrong, if anything. Furthermore, the module now prints additional debug output to help diagnose problems.

I forgot to mention last time that this patch series intends to address:
https://bugzilla.redhat.com/show_bug.cgi?id=1854177

Previous series:
https://lists.gnu.org/archive/html/grub-devel/2022-01/msg00125.html
"

(*) https://lists.gnu.org/archive/html/grub-devel/2022-02/msg00006.html

Gary Lin (8):
tpm2: Don't measure the sealed key
tpm2: adjust the input parameters of TPM2_EvictControl
tpm2: declare the input arguments of TPM2 functions as const
tpm2: resend the command on TPM_RC_RETRY
tpm2: check the command parameters of TPM2 commands
tpm2: pack the missing authorization command for TPM2_PCR_Read
tpm2: allow some command parameters to be NULL
tpm2: remove the unnecessary variables

Hernan Gatta (5):
protectors: Add key protectors framework
tpm2: Add TPM Software Stack (TSS)
protectors: Add TPM2 Key Protector
cryptodisk: Support key protectors
util/grub-protect: Add new tool

Michael Chang (1):
crytodisk: fix cryptodisk module looking up

 .gitignore                             |    1 +
 Makefile.util.def                      |   19 +
 configure.ac                           |    1 +
 grub-core/Makefile.am                  |    1 +
 grub-core/Makefile.core.def            |   12 +
 grub-core/disk/cryptodisk.c            |  176 +++-
 grub-core/kern/protectors.c            |   75 ++
 grub-core/tpm2/args.c                  |  129 +++
 grub-core/tpm2/buffer.c                |  145 +++
 grub-core/tpm2/module.c                |  710 +++++++++++++
 grub-core/tpm2/mu.c                    |  807 +++++++++++++++
 grub-core/tpm2/tcg2.c                  |  143 +++
 grub-core/tpm2/tpm2.c                  |  761 ++++++++++++++
 include/grub/cryptodisk.h              |   14 +
 include/grub/protector.h               |   48 +
 include/grub/tpm2/buffer.h             |   65 ++
 include/grub/tpm2/internal/args.h      |   39 +
 include/grub/tpm2/internal/functions.h |  117 +++
 include/grub/tpm2/internal/structs.h   |  675 ++++++++++++
 include/grub/tpm2/internal/types.h     |  372 +++++++
 include/grub/tpm2/mu.h                 |  292 ++++++
 include/grub/tpm2/tcg2.h               |   34 +
 include/grub/tpm2/tpm2.h               |   38 +
 util/grub-protect.c                    | 1314 ++++++++++++++++++++++++
 24 files changed, 5955 insertions(+), 33 deletions(-)
 create mode 100644 grub-core/kern/protectors.c
 create mode 100644 grub-core/tpm2/args.c
 create mode 100644 grub-core/tpm2/buffer.c
 create mode 100644 grub-core/tpm2/module.c
 create mode 100644 grub-core/tpm2/mu.c
 create mode 100644 grub-core/tpm2/tcg2.c
 create mode 100644 grub-core/tpm2/tpm2.c
 create mode 100644 include/grub/protector.h
 create mode 100644 include/grub/tpm2/buffer.h
 create mode 100644 include/grub/tpm2/internal/args.h
 create mode 100644 include/grub/tpm2/internal/functions.h
 create mode 100644 include/grub/tpm2/internal/structs.h
 create mode 100644 include/grub/tpm2/internal/types.h
 create mode 100644 include/grub/tpm2/mu.h
 create mode 100644 include/grub/tpm2/tcg2.h
 create mode 100644 include/grub/tpm2/tpm2.h
 create mode 100644 util/grub-protect.c
Signature · 1 line
2.35.3
author
#1 lore raw

PATCH 1/14 protectors: Add key protectors framework

From: Hernan Gatta <[email protected]>

A key protector encapsulates functionality to retrieve an unlocking key for a fully-encrypted disk from a specific source. A key protector module registers itself with the key protectors framework when it is loaded and unregisters when unloaded. Additionally, a key protector may accept parameters that describe how it should operate.

The key protectors framework, besides offering registration and unregistration functions, also offers a one-stop routine for finding and invoking a key protector by name. If a key protector with the specified name exists and if an unlocking key is successfully retrieved by it, the function returns to the caller the retrieved key and its length.

---
 grub-core/Makefile.am       |  1 +
 grub-core/Makefile.core.def |  1 +
 grub-core/kern/protectors.c | 75 +++++++++++++++++++++++++++++++++++++
 include/grub/protector.h    | 48 ++++++++++++++++++++++++
 4 files changed, 125 insertions(+)
 create mode 100644 grub-core/kern/protectors.c
 create mode 100644 include/grub/protector.h
grub-core/Makefile.am +1 -0
index 80e7a83ed..79d17a3d2 100644
@@ -90,6 +90,7 @@ endif
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/parser.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/protector.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/stack_protector.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
grub-core/Makefile.core.def +1 -0
index 71093a100..f301b4617 100644
@@ -144,6 +144,7 @@ kernel = {
   common = kern/misc.c;
   common = kern/parser.c;
   common = kern/partition.c;
+  common = kern/protectors.c;
   common = kern/rescue_parser.c;
   common = kern/rescue_reader.c;
   common = kern/term.c;
grub-core/kern/protectors.c new file +75 -0

75 lines not on this page · raw message

include/grub/protector.h new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.35.3
author
#2 lore raw

PATCH 2/14 tpm2: Add TPM Software Stack (TSS)

In reply to Gary Lin

From: Hernan Gatta <[email protected]>

A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose, submit, and parse TPM commands and responses.

A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.

The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, two, of the TPM wire protocol itself.

As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.

Functions: TPM2_Create, TPM2_CreatePrimary, TPM2_EvictControl, TPM2_FlushContext, TPM2_Load, TPM2_PCR_Read, TPM2_PolicyGetDigest, TPM2_PolicyPCR, TPM2_ReadPublic, TPM2_StartAuthSession, TPM2_Unseal.

---
 grub-core/tpm2/buffer.c                | 145 +++++
 grub-core/tpm2/mu.c                    | 807 +++++++++++++++++++++++++
 grub-core/tpm2/tcg2.c                  | 143 +++++
 grub-core/tpm2/tpm2.c                  | 711 ++++++++++++++++++++++
 include/grub/tpm2/buffer.h             |  65 ++
 include/grub/tpm2/internal/functions.h | 117 ++++
 include/grub/tpm2/internal/structs.h   | 675 +++++++++++++++++++++
 include/grub/tpm2/internal/types.h     | 372 ++++++++++++
 include/grub/tpm2/mu.h                 | 292 +++++++++
 include/grub/tpm2/tcg2.h               |  34 ++
 include/grub/tpm2/tpm2.h               |  38 ++
 11 files changed, 3399 insertions(+)
 create mode 100644 grub-core/tpm2/buffer.c
 create mode 100644 grub-core/tpm2/mu.c
 create mode 100644 grub-core/tpm2/tcg2.c
 create mode 100644 grub-core/tpm2/tpm2.c
 create mode 100644 include/grub/tpm2/buffer.h
 create mode 100644 include/grub/tpm2/internal/functions.h
 create mode 100644 include/grub/tpm2/internal/structs.h
 create mode 100644 include/grub/tpm2/internal/types.h
 create mode 100644 include/grub/tpm2/mu.h
 create mode 100644 include/grub/tpm2/tcg2.h
 create mode 100644 include/grub/tpm2/tpm2.h
grub-core/tpm2/buffer.c new file +145 -0
new file mode 100644
index 000000000..3b340b965
@@ -0,0 +1,145 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Microsoft Corporation
+ *
+ *  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/misc.h>
+#include <grub/tpm2/buffer.h>
+
+void grub_tpm2_buffer_init (grub_tpm2_buffer_t buffer)
+{
+  grub_memset (buffer->data, 0xDD, sizeof (buffer->data));
+  buffer->size = 0;
+  buffer->offset = 0;
+  buffer->cap = sizeof (buffer->data);
+  buffer->error = 0;
+}
+
+void
+grub_tpm2_buffer_pack (grub_tpm2_buffer_t buffer, const void* data,
+		       grub_size_t size)
+{
+  grub_uint32_t r = buffer->cap - buffer->size;
+
+  if (buffer->error)
+    return;
+
+  if (size > r)
+    {
+      buffer->error = 1;
+      return;
+    }
+
+  grub_memcpy (&buffer->data[buffer->size], (void*) data, size);
+  buffer->size += size;
+}
+
+void
+grub_tpm2_buffer_pack_u8 (grub_tpm2_buffer_t buffer, grub_uint8_t value)
+{
+  grub_tpm2_buffer_pack (buffer, (const char*) &value, sizeof (value));
+}
+
+void
+grub_tpm2_buffer_pack_u16 (grub_tpm2_buffer_t buffer, grub_uint16_t value)
+{
+  grub_uint16_t tmp = grub_swap_bytes16 (value);
+  grub_tpm2_buffer_pack (buffer, (const char*) &tmp, sizeof (tmp));
+}
+
+void
+grub_tpm2_buffer_pack_u32 (grub_tpm2_buffer_t buffer, grub_uint32_t value)
+{
+  grub_uint32_t tmp = grub_swap_bytes32 (value);
+  grub_tpm2_buffer_pack (buffer, (const char*) &tmp, sizeof (tmp));
+}
+
+void
+grub_tpm2_buffer_unpack (grub_tpm2_buffer_t buffer, void* data,
+			 grub_size_t size)
+{
+  grub_uint32_t r = buffer->size - buffer->offset;
+
+  if (buffer->error)
+    return;
+
+  if (size > r)
+    {
+      buffer->error = 1;
+      return;
+    }
+
+  grub_memcpy (data, &buffer->data[buffer->offset], size);
+  buffer->offset += size;
+}
+
+void
+grub_tpm2_buffer_unpack_u8 (grub_tpm2_buffer_t buffer, grub_uint8_t* value)
+{
+  grub_uint32_t r = buffer->size - buffer->offset;
+
+  if (buffer->error)
+    return;
+
+  if (sizeof (*value) > r)
+    {
+      buffer->error = 1;
+      return;
+    }
+
+  grub_memcpy (value, &buffer->data[buffer->offset], sizeof (*value));
+  buffer->offset += sizeof (*value);
+}
+
+void
+grub_tpm2_buffer_unpack_u16 (grub_tpm2_buffer_t buffer, grub_uint16_t* value)
+{
+  grub_uint16_t tmp;
+  grub_uint32_t r = buffer->size - buffer->offset;
+
+  if (buffer->error)
+    return;
+
+  if (sizeof (tmp) > r)
+    {
+      buffer->error = 1;
+      return;
+    }
+
+  grub_memcpy (&tmp, &buffer->data[buffer->offset], sizeof (tmp));
+  buffer->offset += sizeof (tmp);
+  *value = grub_swap_bytes16 (tmp);
+}
+
+void
+grub_tpm2_buffer_unpack_u32 (grub_tpm2_buffer_t buffer, grub_uint32_t* value)
+{
+  grub_uint32_t tmp;
+  grub_uint32_t r = buffer->size - buffer->offset;
+
+  if (buffer->error)
+    return;
+
+  if (sizeof (tmp) > r)
+    {
+      buffer->error = 1;
+      return;
+    }
+
+  grub_memcpy (&tmp, &buffer->data[buffer->offset], sizeof (tmp));
+  buffer->offset += sizeof (tmp);
+  *value = grub_swap_bytes32 (tmp);
+}
grub-core/tpm2/mu.c new file +807 -0

807 lines not on this page · raw message

grub-core/tpm2/tcg2.c new file +143 -0

143 lines not on this page · raw message

grub-core/tpm2/tpm2.c new file +711 -0

711 lines not on this page · raw message

include/grub/tpm2/buffer.h new file +65 -0

65 lines not on this page · raw message

include/grub/tpm2/internal/functions.h new file +117 -0

117 lines not on this page · raw message

include/grub/tpm2/internal/structs.h new file +675 -0

675 lines not on this page · raw message

include/grub/tpm2/internal/types.h new file +372 -0

337 lines not on this page · raw message

include/grub/tpm2/mu.h new file +292 -0

292 lines not on this page · raw message

include/grub/tpm2/tcg2.h new file +34 -0
new file mode 100644
index 000000000..553b3fd93

Lines of this file are not shown. View the raw message.

include/grub/tpm2/tpm2.h new file +38 -0
new file mode 100644
index 000000000..4b7a9fbb5

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.35.3

Part of the diff is not shown here. View the raw message.

author
#3 lore raw

PATCH 3/14 protectors: Add TPM2 Key Protector

In reply to Gary Lin

From: Hernan Gatta <[email protected]>

The TPM2 key protector is a module that enables the automatic retrieval of a fully-encrypted disk's unlocking key from a TPM 2.0.

The theory of operation is such that the module accepts various arguments, most of which are optional and therefore possess reasonable defaults. One of these arguments is the keyfile parameter, which is mandatory.

The value of this parameter must be a path to a sealed key file (e.g., (hd0,gpt1)/boot/grub2/sealed_key). This sealed key file is created via the grub-protect tool. The tool utilizes the TPM's sealing functionality to seal (i.e., encrypt) an unlocking key using a Storage Root Key (SRK) to the values of various Platform Configuration Registers (PCRs). These PCRs reflect the state of the system as it boots. If the values are as expected, the system may be considered trustworthy, at which point the TPM allows for a caller to utilize the private component of the SRK to unseal (i.e., decrypt) the sealed key file. The caller, in this case, is this key protector.

The TPM2 key protector registers two commands:

- tpm2_key_protector_init: Initializes the state of the TPM2 key protector for later usage, clearing any previous state, too, if any.

- tpm2_key_protector_clear: Clears any state set by tpm2_key_protector_init.

The way this is expected to be used requires the user to, either interactively or, normally, via a boot script, initialize (i.e., configure) the key protector and then specify that it be used by the cryptomount command (modifications to this command are in a different patch).

For instance:

tpm2_key_protector_init --keyfile=KEYFILE1
cryptomount DISK1 -k tpm2

tpm2_key_protector_init --keyfile=KEYFILE2 --pcrs=7,11 cryptomount DISK2 -k tpm2

If a user does not initialize the key protector and attempts to use it anyway, the protector returns an error.

---
 grub-core/Makefile.core.def       |  11 +
 grub-core/tpm2/args.c             | 129 ++++++
 grub-core/tpm2/module.c           | 710 ++++++++++++++++++++++++++++++
 include/grub/tpm2/internal/args.h |  39 ++
 4 files changed, 889 insertions(+)
 create mode 100644 grub-core/tpm2/args.c
 create mode 100644 grub-core/tpm2/module.c
 create mode 100644 include/grub/tpm2/internal/args.h
grub-core/Makefile.core.def +11 -0
index f301b4617..3cfc3b93c 100644
@@ -2518,6 +2518,17 @@ module = {
   enable = efi;
 };
 
+module = {
+  name = tpm2;
+  common = tpm2/args.c;
+  common = tpm2/buffer.c;
+  common = tpm2/module.c;
+  common = tpm2/mu.c;
+  common = tpm2/tpm2.c;
+  efi = tpm2/tcg2.c;
+  enable = efi;
+};
+
 module = {
   name = tr;
   common = commands/tr.c;
grub-core/tpm2/args.c new file +129 -0

129 lines not on this page · raw message

grub-core/tpm2/module.c new file +710 -0

710 lines not on this page · raw message

include/grub/tpm2/internal/args.h new file +39 -0
new file mode 100644
index 000000000..df3341913
@@ -0,0 +1,39 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022 Microsoft Corporation
+ *
+ *  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/>.
+ */
+
+#ifndef GRUB_TPM2_INTERNAL_ARGS_HEADER
+#define GRUB_TPM2_INTERNAL_ARGS_HEADER 1
+
+#include <grub/err.h>
+#include <grub/tpm2/tpm2.h>
+
+grub_err_t
+grub_tpm2_protector_parse_pcrs (char *value, grub_uint8_t *pcrs,
+				grub_uint8_t *pcr_count);
+
+grub_err_t
+grub_tpm2_protector_parse_asymmetric (const char *value,
+				      TPM_ALG_ID *asymmetric);
+
+grub_err_t
+grub_tpm2_protector_parse_bank (const char *value, TPM_ALG_ID *bank);
+
+grub_err_t
+grub_tpm2_protector_parse_tpm_handle (const char *value, TPM_HANDLE *handle);
+
+#endif /* ! GRUB_TPM2_INTERNAL_ARGS_HEADER */
Signature · 1 line
2.35.3
#4 lore raw

Re: [PATCH 03/14] protectors: Add TPM2 Key Protector

Quoted 17 lines · Gary Lin via Grub-devel
grub-core/tpm2/module.c new file +17 -0
+GRUB_MOD_INIT (tpm2)
+{
+  grub_tpm2_protector_init_cmd =
+    grub_register_extcmd ("tpm2_key_protector_init",
+                         grub_tpm2_protector_init_cmd_handler, 0,
+                         N_("[-m mode] "
+                            "[-p pcr_list] "
+                            "[-b pcr_bank] "
+                            "[-k sealed_key_file_path] "
+                            "[-s srk_handle] "
+                            "[-a asymmetric_key_type] "
+                            "[-n nv_index]"),
+                         N_("Initialize the TPM2 key protector."),
+                         grub_tpm2_protector_init_cmd_options);
+  grub_tpm2_protector_clear_cmd =
+    grub_register_extcmd ("tpm2_key_protector_clear",
+                         grub_tpm2_protector_clear_cmd_handler, 0,
Quoted 5 lines

NULL,
+                         N_("Clear the TPM2 key protector if previously initialized."),
+                         NULL);
+  grub_key_protector_register (&grub_tpm2_key_protector);

Hang on, we've spend ages standardising the format of TPM key files:

https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html

And every major TPM based key system uses this format, so a key file created by any TPM2 cryptosystem can be read by any other (meaning you can use any crypotosystem tools to manage the key). What you're doing here just reinvents a non-standard format and creates a load of parameters that the user needs to know here, but are part of the key format standard, so you not only need to remember a load of information not in your key file, but you have to have a non-standard tool to create the key in the first place.

Even if you want to keep your own tool to create keys, what about interoperability with the kernel? The kernel's TPM key subsystem (trusted keys) also speaks the above format

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/security/keys/trusted-keys/tpm2key.asn1

so you wouldn't be able to send this key down to day dm-crypt as a trusted key, you'd have to create two separate key files to do that.

James

author
#5 lore raw

Re: [PATCH 03/14] protectors: Add TPM2 Key Protector

Quoted 28 lines · James Bottomley
Quoted 17 lines · Gary Lin via Grub-devel
grub-core/tpm2/module.c new file +17 -0
+GRUB_MOD_INIT (tpm2)
+{
+  grub_tpm2_protector_init_cmd =
+    grub_register_extcmd ("tpm2_key_protector_init",
+                         grub_tpm2_protector_init_cmd_handler, 0,
+                         N_("[-m mode] "
+                            "[-p pcr_list] "
+                            "[-b pcr_bank] "
+                            "[-k sealed_key_file_path] "
+                            "[-s srk_handle] "
+                            "[-a asymmetric_key_type] "
+                            "[-n nv_index]"),
+                         N_("Initialize the TPM2 key protector."),
+                         grub_tpm2_protector_init_cmd_options);
+  grub_tpm2_protector_clear_cmd =
+    grub_register_extcmd ("tpm2_key_protector_clear",
+                         grub_tpm2_protector_clear_cmd_handler, 0,
Quoted 5 lines

NULL,
+                         N_("Clear the TPM2 key protector if previously initialized."),
+                         NULL);
+  grub_key_protector_register (&grub_tpm2_key_protector);

Hang on, we've spend ages standardising the format of TPM key files:

https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html

Quoted 9 lines

And every major TPM based key system uses this format, so a key file created by any TPM2 cryptosystem can be read by any other (meaning you can use any crypotosystem tools to manage the key). What you're doing here just reinvents a non-standard format and creates a load of parameters that the user needs to know here, but are part of the key format standard, so you not only need to remember a load of information not in your key file, but you have to have a non-standard tool to create the key in the first place.

Thanks for the spec. I'm not aware of that such format already existed. Hernan's patch intuitively combines the public the private parts into one file and provides the tool, util/grub-protect.c, to seal the password. This certainly can be improved.

Quoted 9 lines

Even if you want to keep your own tool to create keys, what about interoperability with the kernel? The kernel's TPM key subsystem (trusted keys) also speaks the above format

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/security/keys/trusted-keys/tpm2key.asn1

so you wouldn't be able to send this key down to day dm-crypt as a trusted key, you'd have to create two separate key files to do that.

The sealed key file would be in be in ESP which is accessible to the normal users. For safety, the key will be sealed against the PCR state before loading the kernel, and the key file cannot be unsealed in OS. Instead, we plan to synthesize an initrd with the unsealed key in "/etc/cryptsetup-keys.d/" and prepend it, so that the crypttab daemon can decrypt the disk right the way.

Gary Lin

author
#6 lore raw

Re: [PATCH 03/14] protectors: Add TPM2 Key Protector

In reply to James Bottomley
Quoted 15 lines · James Bottomley
Quoted 14 lines · Gary Lin via Grub-devel
grub-core/tpm2/module.c new file +14 -0
+GRUB_MOD_INIT (tpm2)
+{
+  grub_tpm2_protector_init_cmd =
+    grub_register_extcmd ("tpm2_key_protector_init",
+                         grub_tpm2_protector_init_cmd_handler, 0,
+                         N_("[-m mode] "
+                            "[-p pcr_list] "
+                            "[-b pcr_bank] "
+                            "[-k sealed_key_file_path] "
+                            "[-s srk_handle] "
+                            "[-a asymmetric_key_type] "
+                            "[-n nv_index]"),
+                         N_("Initialize the TPM2 key protector."),
+                         grub_tpm2_protector_init_cmd_options);
Quoted 9 lines
Quoted 8 lines

+  grub_tpm2_protector_clear_cmd =
+    grub_register_extcmd ("tpm2_key_protector_clear",
+                         grub_tpm2_protector_clear_cmd_handler, 0, NULL,
+                         N_("Clear the TPM2 key protector if previously initialized."),
+                         NULL);
+  grub_key_protector_register (&grub_tpm2_key_protector);

Hi James,

Quoted 4 lines

Hang on, we've spend ages standardising the format of TPM key files:

https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html

Per the spec, the type OIDs are defined as the following:

id-tpmkey OBJECT IDENTIFIER ::=

    {joint-iso-itu-t(2) international-organizations(23) 133 10}

id-loadablekey OBJECT IDENTIFIER ::=

    {id-tpmkey 3}

id-importablekey OBJECT IDENTIFIER ::=

    {id-tpmkey 4}

id-sealedkey OBJECT IDENTIFIER ::=

    {id-tpmkey 5}

Then I assume that the sealed key OID is 2.23.133.10.5. However, I found that the sealed key OID in openssl-tpm2-engine and linux kernel is 2.23.133.10.1.5, and it doesn't match the tpm 2.0 key spec.

Did I miss anything in the spec?

Cheers,

Gary Lin

Quoted 20 lines

And every major TPM based key system uses this format, so a key file created by any TPM2 cryptosystem can be read by any other (meaning you can use any crypotosystem tools to manage the key). What you're doing here just reinvents a non-standard format and creates a load of parameters that the user needs to know here, but are part of the key format standard, so you not only need to remember a load of information not in your key file, but you have to have a non-standard tool to create the key in the first place.

Even if you want to keep your own tool to create keys, what about interoperability with the kernel? The kernel's TPM key subsystem (trusted keys) also speaks the above format

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/security/keys/trusted-keys/tpm2key.asn1

so you wouldn't be able to send this key down to day dm-crypt as a trusted key, you'd have to create two separate key files to do that.

James

#7 lore raw

Re: [PATCH 03/14] protectors: Add TPM2 Key Protector

Quoted 50 lines · Gary Lin
Quoted 18 lines · James Bottomley
Quoted 17 lines · Gary Lin via Grub-devel
grub-core/tpm2/module.c new file +5 -0
+GRUB_MOD_INIT (tpm2)
+{
+  grub_tpm2_protector_init_cmd =
+    grub_register_extcmd ("tpm2_key_protector_init",
+                         grub_tpm2_protector_init_cmd_handler,

0,
+                         N_("[-m mode] "
+                            "[-p pcr_list] "
+                            "[-b pcr_bank] "
+                            "[-k sealed_key_file_path] " +                            "[-s srk_handle] "
+                            "[-a asymmetric_key_type] " +                            "[-n nv_index]"),
+                         N_("Initialize the TPM2 key protector."),
+                         grub_tpm2_protector_init_cmd_options); +  grub_tpm2_protector_clear_cmd =

Quoted 9 lines
Quoted 8 lines

+    grub_register_extcmd ("tpm2_key_protector_clear", +                         grub_tpm2_protector_clear_cmd_handler, 0,
NULL,
+                         N_("Clear the TPM2 key protector if previously initialized."),
+                         NULL);
+  grub_key_protector_register (&grub_tpm2_key_protector);

Hi James,

Quoted 5 lines

Hang on, we've spend ages standardising the format of TPM key files:

https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html

Per the spec, the type OIDs are defined as the following:

  id-tpmkey OBJECT IDENTIFIER ::=
    {joint-iso-itu-t(2) international-organizations(23) 133 10}

  id-loadablekey OBJECT IDENTIFIER ::=
    {id-tpmkey 3}

  id-importablekey OBJECT IDENTIFIER ::=
    {id-tpmkey 4}

  id-sealedkey OBJECT IDENTIFIER ::=
    {id-tpmkey 5}

Quoted 6 lines

Then I assume that the sealed key OID is 2.23.133.10.5. However, I found
that the sealed key OID in openssl-tpm2-engine and linux kernel is 2.23.133.10.1.5, and it doesn't match the tpm 2.0 key spec.

Did I miss anything in the spec?

Possibly not. We went around the houses for ages with the TCG guardian of OID allocations trying to agree on the numbers. It's probable the spec missed one of the iterations of the prefix. If you'd like to send a patch to fix it, the list is:

[email protected]

The raw spec is in git here:

https://git.kernel.org/pub/scm/linux/kernel/git/jejb/openssl_tpm2_engine.git/tree/doc/draft-bottomley-tpm2-keys.xml

James

author
#8 lore raw

Re: [PATCH 03/14] protectors: Add TPM2 Key Protector

Quoted 56 lines · James Bottomley
Quoted 55 lines · Gary Lin
Quoted 18 lines · James Bottomley
Quoted 17 lines · Gary Lin via Grub-devel
grub-core/tpm2/module.c new file +5 -0
+GRUB_MOD_INIT (tpm2)
+{
+  grub_tpm2_protector_init_cmd =
+    grub_register_extcmd ("tpm2_key_protector_init",
+                         grub_tpm2_protector_init_cmd_handler,

0,
+                         N_("[-m mode] "
+                            "[-p pcr_list] "
+                            "[-b pcr_bank] "
+                            "[-k sealed_key_file_path] " +                            "[-s srk_handle] "
+                            "[-a asymmetric_key_type] " +                            "[-n nv_index]"),
+                         N_("Initialize the TPM2 key protector."),
+                         grub_tpm2_protector_init_cmd_options); +  grub_tpm2_protector_clear_cmd =

Quoted 9 lines
Quoted 8 lines

+    grub_register_extcmd ("tpm2_key_protector_clear", +                         grub_tpm2_protector_clear_cmd_handler, 0,
NULL,
+                         N_("Clear the TPM2 key protector if previously initialized."),
+                         NULL);
+  grub_key_protector_register (&grub_tpm2_key_protector);

Hi James,

Quoted 5 lines

Hang on, we've spend ages standardising the format of TPM key files:

https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html

Per the spec, the type OIDs are defined as the following:

  id-tpmkey OBJECT IDENTIFIER ::=
    {joint-iso-itu-t(2) international-organizations(23) 133 10}

  id-loadablekey OBJECT IDENTIFIER ::=
    {id-tpmkey 3}

  id-importablekey OBJECT IDENTIFIER ::=
    {id-tpmkey 4}

  id-sealedkey OBJECT IDENTIFIER ::=
    {id-tpmkey 5}

Then I assume that the sealed key OID is 2.23.133.10.5. However, I found
that the sealed key OID in openssl-tpm2-engine and linux kernel is 2.23.133.10.1.5, and it doesn't match the tpm 2.0 key spec.

Quoted 9 lines
Quoted 1 line

Did I miss anything in the spec?

Possibly not. We went around the houses for ages with the TCG guardian of OID allocations trying to agree on the numbers. It's probable the spec missed one of the iterations of the prefix. If you'd like to send a patch to fix it, the list is:

[email protected]

Well, I only spotted the missing number and have no idea of what it is. It needs someone who knows the spec well to fix the OID properly.

Gary Lin

Quoted 6 lines

The raw spec is in git here:

https://git.kernel.org/pub/scm/linux/kernel/git/jejb/openssl_tpm2_engine.git/tree/doc/draft-bottomley-tpm2-keys.xml

James

author
#9 lore raw

PATCH 4/14 cryptodisk: Support key protectors

In reply to Gary Lin

From: Hernan Gatta <[email protected]>

Add a new parameter to cryptomount to support the key protectors framework: -k. The parameter is used to automatically retrieve a key from specified key protectors. The parameter may be repeated to specify any number of key protectors. These are tried in order until one provides a usable key for any given disk.

Signed-off-by <Hernan Gatta [email protected]>
---
 Makefile.util.def           |   1 +
 grub-core/disk/cryptodisk.c | 175 +++++++++++++++++++++++++++++-------
 include/grub/cryptodisk.h   |  14 +++
 3 files changed, 157 insertions(+), 33 deletions(-)
Makefile.util.def +1 -0
index beaef1168..1da5a826c 100644
@@ -35,6 +35,7 @@ library = {
   common = grub-core/kern/list.c;
   common = grub-core/kern/misc.c;
   common = grub-core/kern/partition.c;
+  common = grub-core/kern/protectors.c;
   common = grub-core/lib/crypto.c;
   common = grub-core/lib/json/json.c;
   common = grub-core/disk/luks.c;
grub-core/disk/cryptodisk.c +142 -33

270 lines not on this page · raw message

include/grub/cryptodisk.h +14 -0
index d94df68b6..ce18df883 100644
@@ -70,6 +70,16 @@ typedef gcry_err_code_t
 (*grub_cryptodisk_rekey_func_t) (struct grub_cryptodisk *dev,
 				 grub_uint64_t zoneno);
 
+struct grub_cryptomount_cached_key
+{
+  grub_uint8_t *key;
+  grub_size_t key_len;
+
+  /* The key protector associated with this cache entry failed, so avoid it
+   * even if the cached entry (an instance of this structure) is empty. */
+  int invalid;
+};
+
 struct grub_cryptomount_args
 {
   /* scan: Flag to indicate that only bootable volumes should be decrypted */
@@ -81,6 +91,10 @@ struct grub_cryptomount_args
   /* recover_key: Length of key_data */
   grub_size_t key_len;
   grub_file_t hdr_file;
+  /* recover_key: Names of the key protectors to use (NULL-terminated) */
+  char **protectors;
+  /* recover_key: Key cache to avoid invoking the same key protector twice */
+  struct grub_cryptomount_cached_key *key_cache;
 };
 typedef struct grub_cryptomount_args *grub_cryptomount_args_t;
 
Signature · 1 line
2.35.3
author
#10 lore raw

Re: [PATCH 04/14] cryptodisk: Support key protectors

Quoted 9 lines · Gary Lin via Grub-devel

From: Hernan Gatta <[email protected]>

Add a new parameter to cryptomount to support the key protectors framework: -k. The parameter is used to automatically retrieve a key from specified key protectors. The parameter may be repeated to specify any number of key protectors. These are tried in order until one provides a usable key for any given disk.

Signed-off-by <Hernan Gatta [email protected]>

The email format is broken here and I didn't notice it... Will fix the format in V2.

Quoted 347 lines
---
 Makefile.util.def           |   1 +
 grub-core/disk/cryptodisk.c | 175 +++++++++++++++++++++++++++++-------
 include/grub/cryptodisk.h   |  14 +++
 3 files changed, 157 insertions(+), 33 deletions(-)
Makefile.util.def +1 -0
index beaef1168..1da5a826c 100644
@@ -35,6 +35,7 @@ library = {
   common = grub-core/kern/list.c;
   common = grub-core/kern/misc.c;
   common = grub-core/kern/partition.c;
+  common = grub-core/kern/protectors.c;
   common = grub-core/lib/crypto.c;
   common = grub-core/lib/json/json.c;
   common = grub-core/disk/luks.c;
grub-core/disk/cryptodisk.c +142 -33
index 34b67a705..c990d5985 100644
@@ -26,6 +26,7 @@
 #include <grub/file.h>
 #include <grub/procfs.h>
 #include <grub/partition.h>
+#include <grub/protector.h>
 
 #ifdef GRUB_UTIL
 #include <grub/emu/hostdisk.h>
@@ -44,7 +45,8 @@ enum
     OPTION_KEYFILE,
     OPTION_KEYFILE_OFFSET,
     OPTION_KEYFILE_SIZE,
-    OPTION_HEADER
+    OPTION_HEADER,
+    OPTION_PROTECTOR
   };
 
 static const struct grub_arg_option options[] =
@@ -58,6 +60,8 @@ static const struct grub_arg_option options[] =
     {"keyfile-offset", 'O', 0, N_("Key file offset (bytes)"), 0, ARG_TYPE_INT},
     {"keyfile-size", 'S', 0, N_("Key file data size (bytes)"), 0, ARG_TYPE_INT},
     {"header", 'H', 0, N_("Read header from file"), 0, ARG_TYPE_STRING},
+    {"protector", 'k', GRUB_ARG_OPTION_REPEATABLE,
+     N_("Unlock volume(s) using key protector(s)."), 0, ARG_TYPE_STRING},
     {0, 0, 0, 0, 0, 0}
   };
 
@@ -1060,7 +1064,8 @@ grub_cryptodisk_scan_device_real (const char *name,
 {
   grub_err_t ret = GRUB_ERR_NONE;
   grub_cryptodisk_t dev;
-  grub_cryptodisk_dev_t cr;
+  grub_cryptodisk_dev_t cr, crd = NULL;
+  int i;
   struct cryptodisk_read_hook_ctx read_hook_data = {0};
   int askpass = 0;
   char *part = NULL;
@@ -1113,41 +1118,112 @@ grub_cryptodisk_scan_device_real (const char *name,
       goto error_no_close;
     if (!dev)
       continue;
+    crd = cr;
+  }
 
-    if (!cargs->key_len)
-      {
-	/* Get the passphrase from the user, if no key data. */
-	askpass = 1;
-	part = grub_partition_get_name (source->partition);
-	grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
-		     source->partition != NULL ? "," : "",
-		     part != NULL ? part : N_("UNKNOWN"),
-		     dev->uuid);
-	grub_free (part);
-
-	cargs->key_data = grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE);
-	if (cargs->key_data == NULL)
-	  goto error_no_close;
-
-	if (!grub_password_get ((char *) cargs->key_data, GRUB_CRYPTODISK_MAX_PASSPHRASE))
-	  {
-	    grub_error (GRUB_ERR_BAD_ARGUMENT, "passphrase not supplied");
-	    goto error;
-	  }
-	cargs->key_len = grub_strlen ((char *) cargs->key_data);
-      }
+  if (!dev)
+    {
+      grub_error (GRUB_ERR_BAD_MODULE,
+                  "no cryptodisk module can handle this device");
+      goto error_no_close;
+    }
 
-    ret = cr->recover_key (source, dev, cargs);
-    if (ret != GRUB_ERR_NONE)
-      goto error;
+  if (cargs->protectors)
+    {
+      for (i = 0; cargs->protectors[i]; i++)
+        {
+          if (cargs->key_cache[i].invalid)
+            continue;
+
+          if (!cargs->key_cache[i].key)
+            {
+              ret = grub_key_protector_recover_key (cargs->protectors[i],
+                                                    &cargs->key_cache[i].key,
+                                                    &cargs->key_cache[i].key_len);
+              if (ret)
+                {
+                  if (grub_errno)
+                    {
+                      grub_print_error ();
+                      grub_errno = GRUB_ERR_NONE;
+                    }
+
+                  grub_dprintf ("cryptodisk",
+                                "failed to recover a key from key protector "
+                                "%s, will not try it again for any other "
+                                "disks, if any, during this invocation of "
+                                "cryptomount\n",
+                                cargs->protectors[i]);
+
+                  cargs->key_cache[i].invalid = 1;
+                  continue;
+                }
+            }
+
+          cargs->key_data = cargs->key_cache[i].key;
+          cargs->key_len = cargs->key_cache[i].key_len;
+
+          ret = crd->recover_key (source, dev, cargs);
+          if (ret)
+            {
+              part = grub_partition_get_name (source->partition);
+              grub_dprintf ("cryptodisk",
+                            "recovered a key from key protector %s but it "
+                            "failed to unlock %s%s%s (%s)\n",
+                             cargs->protectors[i], source->name,
+                             source->partition != NULL ? "," : "",
+                             part != NULL ? part : N_("UNKNOWN"), dev->uuid);
+               grub_free (part);
+               continue;
+            }
+         else
+           {
+             ret = grub_cryptodisk_insert (dev, name, source);
+             if (ret != GRUB_ERR_NONE)
+               goto error;
+             goto cleanup;
+           }
+        }
 
-    ret = grub_cryptodisk_insert (dev, name, source);
-    if (ret != GRUB_ERR_NONE)
+      part = grub_partition_get_name (source->partition);
+      grub_error (GRUB_ERR_ACCESS_DENIED,
+                  N_("no key protector provided a usable key for %s%s%s (%s)"),
+                  source->name, source->partition != NULL ? "," : "",
+                  part != NULL ? part : N_("UNKNOWN"), dev->uuid);
+      grub_free (part);
       goto error;
+    }
+
+  if (!cargs->key_len)
+    {
+      /* Get the passphrase from the user, if no key data. */
+      askpass = 1;
+      part = grub_partition_get_name (source->partition);
+      grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+                    source->partition != NULL ? "," : "",
+                    part != NULL ? part : N_("UNKNOWN"), dev->uuid);
+      grub_free (part);
+
+      cargs->key_data = grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE);
+      if (cargs->key_data == NULL)
+        goto error;
+
+      if (!grub_password_get ((char *) cargs->key_data, GRUB_CRYPTODISK_MAX_PASSPHRASE))
+        {
+          grub_error (GRUB_ERR_BAD_ARGUMENT, "passphrase not supplied");
+          goto error;
+        }
+      cargs->key_len = grub_strlen ((char *) cargs->key_data);
+    }
+
+  ret = crd->recover_key (source, dev, cargs);
+  if (ret != GRUB_ERR_NONE)
+    goto error;
+
+  ret = grub_cryptodisk_insert (dev, name, source);
+  if (ret != GRUB_ERR_NONE)
+    goto error;
 
-    goto cleanup;
-  }
-  grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk module can handle this device");
   goto cleanup;
 
  error:
@@ -1258,6 +1334,20 @@ grub_cryptodisk_scan_device (const char *name,
   return ret;
 }
 
+static void
+grub_cryptodisk_clear_key_cache (struct grub_cryptomount_args *cargs)
+{
+  int i;
+
+  if (!cargs->key_cache)
+    return;
+
+  for (i = 0; cargs->protectors[i]; i++)
+    grub_free (cargs->key_cache[i].key);
+
+  grub_free (cargs->key_cache);
+}
+
 static grub_err_t
 grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
 {
@@ -1270,6 +1360,10 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
   if (grub_cryptodisk_list == NULL)
     return grub_error (GRUB_ERR_BAD_MODULE, "no cryptodisk modules loaded");
 
+  if (state[OPTION_PASSWORD].set && state[OPTION_PROTECTOR].set) /* password and key protector */
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                       "a password and a key protector cannot both be set");
+
   if (state[OPTION_PASSWORD].set) /* password */
     {
       cargs.key_data = (grub_uint8_t *) state[OPTION_PASSWORD].arg;
@@ -1362,6 +1456,15 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
 	return grub_errno;
     }
 
+  if (state[OPTION_PROTECTOR].set) /* key protector(s) */
+    {
+      cargs.key_cache = grub_zalloc (state[OPTION_PROTECTOR].set * sizeof (*cargs.key_cache));
+      if (!cargs.key_cache)
+        return grub_error (GRUB_ERR_OUT_OF_MEMORY,
+                           "no memory for key protector key cache");
+      cargs.protectors = state[OPTION_PROTECTOR].args;
+    }
+
   if (state[OPTION_UUID].set) /* uuid */
     {
       int found_uuid;
@@ -1370,6 +1473,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
       dev = grub_cryptodisk_get_by_uuid (args[0]);
       if (dev)
 	{
+          grub_cryptodisk_clear_key_cache (&cargs);
 	  grub_dprintf ("cryptodisk",
 			"already mounted as crypto%lu\n", dev->id);
 	  return GRUB_ERR_NONE;
@@ -1378,6 +1482,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
       cargs.check_boot = state[OPTION_BOOT].set;
       cargs.search_uuid = args[0];
       found_uuid = grub_device_iterate (&grub_cryptodisk_scan_device, &cargs);
+      grub_cryptodisk_clear_key_cache (&cargs);
 
       if (found_uuid)
 	return GRUB_ERR_NONE;
@@ -1397,6 +1502,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
     {
       cargs.check_boot = state[OPTION_BOOT].set;
       grub_device_iterate (&grub_cryptodisk_scan_device, &cargs);
+      grub_cryptodisk_clear_key_cache (&cargs);
       return GRUB_ERR_NONE;
     }
   else
@@ -1420,6 +1526,7 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
       disk = grub_disk_open (diskname);
       if (!disk)
 	{
+          grub_cryptodisk_clear_key_cache (&cargs);
 	  if (disklast)
 	    *disklast = ')';
 	  return grub_errno;
@@ -1430,12 +1537,14 @@ grub_cmd_cryptomount (grub_extcmd_context_t ctxt, int argc, char **args)
 	{
 	  grub_dprintf ("cryptodisk", "already mounted as crypto%lu\n", dev->id);
 	  grub_disk_close (disk);
+          grub_cryptodisk_clear_key_cache (&cargs);
 	  if (disklast)
 	    *disklast = ')';
 	  return GRUB_ERR_NONE;
 	}
 
       dev = grub_cryptodisk_scan_device_real (diskname, disk, &cargs);
+      grub_cryptodisk_clear_key_cache (&cargs);
 
       grub_disk_close (disk);
       if (disklast)
@@ -1576,7 +1685,7 @@ GRUB_MOD_INIT (cryptodisk)
   cmd = grub_register_extcmd ("cryptomount", grub_cmd_cryptomount, 0,
 			      N_("[ [-p password] | [-k keyfile"
 				 " [-O keyoffset] [-S keysize] ] ] [-H file]"
-				 " <SOURCE|-u UUID|-a|-b>"),
+				 " [-k protector [-k protector ...]] <SOURCE|-u UUID|-a|-b>"),
 			      N_("Mount a crypto device."), options);
   grub_procfs_register ("luks_script", &luks_script);
 }
include/grub/cryptodisk.h +14 -0
index d94df68b6..ce18df883 100644
@@ -70,6 +70,16 @@ typedef gcry_err_code_t
 (*grub_cryptodisk_rekey_func_t) (struct grub_cryptodisk *dev,
 				 grub_uint64_t zoneno);
 
+struct grub_cryptomount_cached_key
+{
+  grub_uint8_t *key;
+  grub_size_t key_len;
+
+  /* The key protector associated with this cache entry failed, so avoid it
+   * even if the cached entry (an instance of this structure) is empty. */
+  int invalid;
+};
+
 struct grub_cryptomount_args
 {
   /* scan: Flag to indicate that only bootable volumes should be decrypted */
@@ -81,6 +91,10 @@ struct grub_cryptomount_args
   /* recover_key: Length of key_data */
   grub_size_t key_len;
   grub_file_t hdr_file;
+  /* recover_key: Names of the key protectors to use (NULL-terminated) */
+  char **protectors;
+  /* recover_key: Key cache to avoid invoking the same key protector twice */
+  struct grub_cryptomount_cached_key *key_cache;
 };
 typedef struct grub_cryptomount_args *grub_cryptomount_args_t;
 
Signature · 7 lines
2.35.3


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

PATCH 5/14 util/grub-protect: Add new tool

In reply to Gary Lin

From: Hernan Gatta <[email protected]>

To utilize the key protectors framework, there must be a way to protect full-disk encryption keys in the first place. The grub-protect tool includes support for the TPM2 key protector but other protectors that require setup ahead of time can be supported in the future.

For the TPM2 key protector, the intended flow is for a user to have a LUKS 1 or LUKS 2-protected fully-encrypted disk. The user then creates a new key file, say by reading /dev/urandom into a file, and creates a new LUKS key slot for this key. Then, the user invokes the grub-protect tool to seal this key file to a set of PCRs using the system's TPM 2.0. The resulting sealed key file is stored in an unencrypted partition such as the EFI System Partition (ESP) so that GRUB may read it. The user also ensures the cryptomount command is included in GRUB's boot script and that it carries the requisite key protector (-k) parameter.

Sample usage:

$ dd if=/dev/urandom of=key bs=1 count=32
$ sudo cryptsetup luksAddKey /dev/sdb1 key --pbkdf=pbkdf2 --hash=sha512
$ sudo grub-protect --action=add
                    --protector=tpm2
                    --tpm2-keyfile=key
                    --tpm2-outfile=/boot/efi/boot/grub2/sealed_key

Then, in the boot script:

tpm2_key_protector_init -k (hd0,gpt1)/boot/grub2/sealed_key cryptomount -u b20f95d0834842bc9197bd78b36732f8 -k tpm2

where the UUID corresponds to /dev/sdb1.

---
 .gitignore          |    1 +
 Makefile.util.def   |   18 +
 configure.ac        |    1 +
 util/grub-protect.c | 1314 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1334 insertions(+)
 create mode 100644 util/grub-protect.c
.gitignore +1 -0
index 4064d3d1e..f25f94aad 100644
@@ -167,6 +167,7 @@ widthspec.bin
 /grub-ofpathname.exe
 /grub-probe
 /grub-probe.exe
+/grub-protect
 /grub-reboot
 /grub-render-label
 /grub-render-label.exe
Makefile.util.def +18 -0
index 1da5a826c..2e8e4891e 100644
@@ -206,6 +206,24 @@ program = {
   ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
 };
 
+program = {
+  name = grub-protect;
+
+  common = grub-core/osdep/init.c;
+  common = grub-core/tpm2/args.c;
+  common = grub-core/tpm2/buffer.c;
+  common = grub-core/tpm2/mu.c;
+  common = grub-core/tpm2/tpm2.c;
+  common = util/grub-protect.c;
+  common = util/probe.c;
+
+  ldadd = libgrubmods.a;
+  ldadd = libgrubgcry.a;
+  ldadd = libgrubkern.a;
+  ldadd = grub-core/lib/gnulib/libgnu.a;
+  ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
+};
+
 program = {
   name = grub-mkrelpath;
   mansection = 1;
configure.ac +1 -0
index 93626b798..e2c66df0d 100644
@@ -76,6 +76,7 @@ grub_TRANSFORM([grub-mkpasswd-pbkdf2])
 grub_TRANSFORM([grub-mkrelpath])
 grub_TRANSFORM([grub-mkrescue])
 grub_TRANSFORM([grub-probe])
+grub_TRANSFORM([grub-protect])
 grub_TRANSFORM([grub-reboot])
 grub_TRANSFORM([grub-script-check])
 grub_TRANSFORM([grub-set-default])
util/grub-protect.c new file +1314 -0

1,314 lines not on this page · raw message

Signature · 1 line
2.35.3
author
#12 lore raw

PATCH 6/14 crytodisk: fix cryptodisk module looking up

In reply to Gary Lin

From: Michael Chang <[email protected]>

The error "no cryptodisk module can handle this device" may happen even encrypted disk were correctly formatted and required modules were loaded.

It is casued by missing break to the loop in which cryptodisk modules are iterated to find the one matching target's disk format. With the break statement, the loop will be always ended with testing last cryptodisk module on the list that may not be able to handle the format of encrypted disk's.

---
 grub-core/disk/cryptodisk.c | 1 +
 1 file changed, 1 insertion(+)
grub-core/disk/cryptodisk.c +1 -0
index c990d5985..e199f550f 100644
@@ -1119,6 +1119,7 @@ grub_cryptodisk_scan_device_real (const char *name,
     if (!dev)
       continue;
     crd = cr;
+    break;
   }
 
   if (!dev)
Signature · 1 line
2.35.3
author
#13 lore raw

PATCH 7/14 tpm2: Don't measure the sealed key

In reply to Gary Lin

Based on the patch from Olaf Kirch <[email protected]>

The sealed key is the subject to change and measuring the file into PCR9 makes the prediction of PCR9 value impossible. This commit opens the file with GRUB_FILE_TYPE_SIGNATURE to avoid the measurement.

---
 grub-core/tpm2/module.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
grub-core/tpm2/module.c +3 -1
index c35bfabdb..bebdecd1c 100644
@@ -139,7 +139,9 @@ grub_tpm2_protector_srk_read_keyfile (const char *filepath, void **buffer,
   void *sealed_key_buffer;
   grub_off_t sealed_key_read;
 
-  sealed_key_file = grub_file_open (filepath, GRUB_FILE_TYPE_NONE);
+  /* Using GRUB_FILE_TYPE_SIGNATURE ensures we do not hash the keyfile into PCR9
+   * otherwise we'll never be able to predict the value of PCR9 at unseal time */
+  sealed_key_file = grub_file_open (filepath, GRUB_FILE_TYPE_SIGNATURE);
   if (!sealed_key_file)
     {
       grub_dprintf ("tpm2", "Could not open sealed key file.\n");
Signature · 1 line
2.35.3
author
#14 lore raw

PATCH 8/14 tpm2: adjust the input parameters of TPM2_EvictControl

In reply to Gary Lin

Per "TCG TPM2 Part3 Commands", 'persistentHandle' of TPM2_EvictControl is in the parameter area, i.e. after the authorization command. Adjust the order of the arguments to match the spec definition.

---
 grub-core/tpm2/tpm2.c                  | 2 +-
 include/grub/tpm2/internal/functions.h | 2 +-
 util/grub-protect.c                    | 8 ++++----
 3 files changed, 6 insertions(+), 6 deletions(-)
grub-core/tpm2/tpm2.c +1 -1
index 7baa4c6e5..85bd4ef23 100644
@@ -662,8 +662,8 @@ TPM2_Create (TPMI_DH_OBJECT parentHandle,
 TPM_RC
 TPM2_EvictControl (TPMI_RH_PROVISION auth,
 		   TPMI_DH_OBJECT objectHandle,
-		   TPMI_DH_PERSISTENT persistentHandle,
 		   const TPMS_AUTH_COMMAND *authCommand,
+		   TPMI_DH_PERSISTENT persistentHandle,
 		   TPMS_AUTH_RESPONSE *authResponse)
 {
   struct grub_tpm2_buffer in;
include/grub/tpm2/internal/functions.h +1 -1
index 9d5823975..f88ea2dfe 100644
@@ -110,8 +110,8 @@ TPM2_Create (TPMI_DH_OBJECT parentHandle,
 TPM_RC
 TPM2_EvictControl (TPMI_RH_PROVISION auth,
 		   TPMI_DH_OBJECT objectHandle,
-		   TPMI_DH_PERSISTENT persistentHandle,
 		   const TPMS_AUTH_COMMAND *authCommand,
+		   TPMI_DH_PERSISTENT persistentHandle,
 		   TPMS_AUTH_RESPONSE *authResponse);
 
 #endif /* ! GRUB_TPM2_INTERNAL_FUNCTIONS_HEADER */
util/grub-protect.c +4 -4
index acd30642f..f0f27af0a 100644
@@ -695,8 +695,8 @@ grub_protect_tpm2_get_srk (struct grub_protect_args *args, TPM_HANDLE *srk)
   /* Persist SRK */
   if (args->tpm2_persist)
     {
-      rc = TPM2_EvictControl (TPM_RH_OWNER, srkHandle, args->tpm2_srk,
-			      &authCommand, NULL);
+      rc = TPM2_EvictControl (TPM_RH_OWNER, srkHandle, &authCommand,
+			      args->tpm2_srk, NULL);
       if (rc == TPM_RC_SUCCESS)
 	{
 	  TPM2_FlushContext (srkHandle);
@@ -877,8 +877,8 @@ grub_protect_tpm2_remove (struct grub_protect_args *args)
   /* Evict SRK */
   authCommand.sessionHandle = TPM_RS_PW;
 
-  rc = TPM2_EvictControl (TPM_RH_OWNER, args->tpm2_srk, args->tpm2_srk,
-			  &authCommand, NULL);
+  rc = TPM2_EvictControl (TPM_RH_OWNER, args->tpm2_srk, &authCommand,
+			  args->tpm2_srk, NULL);
   if (rc != TPM_RC_SUCCESS)
     {
       fprintf (stderr,
Signature · 1 line
2.35.3
author
#15 lore raw

PATCH 9/14 tpm2: declare the input arguments of TPM2 functions as const

In reply to Gary Lin

The arguments, except the buffer, of the marshal functions are the pure inputs. Also, the TPM2 command parameters are supposed not changed by the command. Declare those arguments as 'const' so that the compiler can help to detect the undesired change on those arguments. Besides, when looking up the TPM2 command functions, it's easier to tell which arguments are the command parameters (the constant arguments) and which are the response parameters.

---
 grub-core/tpm2/mu.c                    | 64 ++++++++++++------------
 grub-core/tpm2/tpm2.c                  | 68 +++++++++++++-------------
 include/grub/tpm2/internal/functions.h | 64 ++++++++++++------------
 include/grub/tpm2/mu.h                 | 64 ++++++++++++------------
 4 files changed, 130 insertions(+), 130 deletions(-)
grub-core/tpm2/mu.c +32 -32
index 2492f1305..60907abfa 100644
@@ -49,7 +49,7 @@ grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPM2B_Marshal (grub_tpm2_buffer_t buffer,
-			    grub_uint16_t size,
+			    const grub_uint16_t size,
 			    const grub_uint8_t* b)
 {
   grub_tpm2_buffer_pack_u16 (buffer, size);
@@ -60,8 +60,8 @@ grub_tpm2_mu_TPM2B_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMU_SYM_KEY_BITS_Marshal (grub_tpm2_buffer_t buffer,
-					TPMI_ALG_SYM_OBJECT algorithm,
-					TPMU_SYM_KEY_BITS *p)
+					const TPMI_ALG_SYM_OBJECT algorithm,
+					const TPMU_SYM_KEY_BITS *p)
 {
   switch (algorithm)
     {
@@ -78,8 +78,8 @@ grub_tpm2_mu_TPMU_SYM_KEY_BITS_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMU_SYM_MODE_Marshal (grub_tpm2_buffer_t buffer,
-				    TPMI_ALG_SYM_OBJECT algorithm,
-				    TPMU_SYM_MODE *p)
+				    const TPMI_ALG_SYM_OBJECT algorithm,
+				    const TPMU_SYM_MODE *p)
 {
   switch (algorithm)
     {
@@ -96,7 +96,7 @@ grub_tpm2_mu_TPMU_SYM_MODE_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMT_SYM_DEF_Marshal (grub_tpm2_buffer_t buffer,
-				   TPMT_SYM_DEF *p)
+				   const TPMT_SYM_DEF *p)
 {
   grub_tpm2_buffer_pack_u16 (buffer, p->algorithm);
   grub_tpm2_mu_TPMU_SYM_KEY_BITS_Marshal (buffer, p->algorithm, &p->keyBits);
@@ -134,7 +134,7 @@ grub_tpm2_mu_TPMA_OBJECT_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMS_SCHEME_XOR_Marshal (grub_tpm2_buffer_t buffer,
-				      TPMS_SCHEME_XOR *p)
+				      const TPMS_SCHEME_XOR *p)
 {
   grub_tpm2_buffer_pack_u16 (buffer, p->hashAlg);
   grub_tpm2_buffer_pack_u16 (buffer, p->kdf);
@@ -142,15 +142,15 @@ grub_tpm2_mu_TPMS_SCHEME_XOR_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMS_SCHEME_HMAC_Marshal (grub_tpm2_buffer_t buffer,
-				       TPMS_SCHEME_HMAC *p)
+				       const TPMS_SCHEME_HMAC *p)
 {
   grub_tpm2_buffer_pack_u16 (buffer, p->hashAlg);
 }
 
 void
 grub_tpm2_mu_TPMU_SCHEME_KEYEDHASH_Marshal (grub_tpm2_buffer_t buffer,
-					    TPMI_ALG_KEYEDHASH_SCHEME scheme,
-					    TPMU_SCHEME_KEYEDHASH *p)
+					    const TPMI_ALG_KEYEDHASH_SCHEME scheme,
+					    const TPMU_SCHEME_KEYEDHASH *p)
 {
   switch (scheme)
     {
@@ -170,7 +170,7 @@ grub_tpm2_mu_TPMU_SCHEME_KEYEDHASH_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMT_KEYEDHASH_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
-					    TPMT_KEYEDHASH_SCHEME *p)
+					    const TPMT_KEYEDHASH_SCHEME *p)
 {
   grub_tpm2_buffer_pack_u16 (buffer, p->scheme);
   grub_tpm2_mu_TPMU_SCHEME_KEYEDHASH_Marshal (buffer, p->scheme, &p->details);
@@ -178,14 +178,14 @@ grub_tpm2_mu_TPMT_KEYEDHASH_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMS_KEYEDHASH_PARMS_Marshal (grub_tpm2_buffer_t buffer,
-					   TPMS_KEYEDHASH_PARMS *p)
+					   const TPMS_KEYEDHASH_PARMS *p)
 {
   grub_tpm2_mu_TPMT_KEYEDHASH_SCHEME_Marshal (buffer, &p->scheme);
 }
 
 void
 grub_tpm2_mu_TPMT_SYM_DEF_OBJECT_Marshal (grub_tpm2_buffer_t buffer,
-					  TPMT_SYM_DEF_OBJECT *p)
+					  const TPMT_SYM_DEF_OBJECT *p)
 {
   grub_tpm2_buffer_pack_u16 (buffer, p->algorithm);
   grub_tpm2_mu_TPMU_SYM_KEY_BITS_Marshal (buffer, p->algorithm, &p->keyBits);
@@ -194,8 +194,8 @@ grub_tpm2_mu_TPMT_SYM_DEF_OBJECT_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMU_ASYM_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
-				       TPMI_ALG_RSA_DECRYPT scheme,
-				       TPMU_ASYM_SCHEME *p __attribute__ ((unused)))
+				       const TPMI_ALG_RSA_DECRYPT scheme,
+				       const TPMU_ASYM_SCHEME *p __attribute__ ((unused)))
 {
   switch (scheme)
     {
@@ -210,7 +210,7 @@ grub_tpm2_mu_TPMU_ASYM_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMT_RSA_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
-				      TPMT_RSA_SCHEME *p)
+				      const TPMT_RSA_SCHEME *p)
 {
   grub_tpm2_buffer_pack_u16 (buffer, p->scheme);
   grub_tpm2_mu_TPMU_ASYM_SCHEME_Marshal (buffer, p->scheme, &p->details);
@@ -218,7 +218,7 @@ grub_tpm2_mu_TPMT_RSA_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMS_RSA_PARMS_Marshal (grub_tpm2_buffer_t buffer,
-				     TPMS_RSA_PARMS *p)
+				     const TPMS_RSA_PARMS *p)
 {
   grub_tpm2_mu_TPMT_SYM_DEF_OBJECT_Marshal (buffer, &p->symmetric);
   grub_tpm2_mu_TPMT_RSA_SCHEME_Marshal (buffer, &p->scheme);
@@ -228,14 +228,14 @@ grub_tpm2_mu_TPMS_RSA_PARMS_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMS_SYMCIPHER_PARMS_Marshal (grub_tpm2_buffer_t buffer,
-					   TPMS_SYMCIPHER_PARMS *p)
+					   const TPMS_SYMCIPHER_PARMS *p)
 {
   grub_tpm2_mu_TPMT_SYM_DEF_OBJECT_Marshal (buffer, &p->sym);
 }
 
 void
 grub_tpm2_mu_TPMT_ECC_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
-				      TPMT_ECC_SCHEME *p)
+				      const TPMT_ECC_SCHEME *p)
 {
   grub_tpm2_buffer_pack_u16 (buffer, p->scheme);
   grub_tpm2_mu_TPMU_ASYM_SCHEME_Marshal (buffer, p->scheme, &p->details);
@@ -243,8 +243,8 @@ grub_tpm2_mu_TPMT_ECC_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMU_KDF_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
-				      TPMI_ALG_KDF scheme,
-				      TPMU_KDF_SCHEME *p)
+				      const TPMI_ALG_KDF scheme,
+				      const TPMU_KDF_SCHEME *p)
 {
   switch (scheme)
     {
@@ -270,7 +270,7 @@ grub_tpm2_mu_TPMU_KDF_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMT_KDF_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
-				      TPMT_KDF_SCHEME *p)
+				      const TPMT_KDF_SCHEME *p)
 {
   grub_tpm2_buffer_pack_u16 (buffer, p->scheme);
   grub_tpm2_mu_TPMU_KDF_SCHEME_Marshal (buffer, p->scheme, &p->details);
@@ -278,7 +278,7 @@ grub_tpm2_mu_TPMT_KDF_SCHEME_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMS_ECC_PARMS_Marshal (grub_tpm2_buffer_t buffer,
-				     TPMS_ECC_PARMS *p)
+				     const TPMS_ECC_PARMS *p)
 {
   grub_tpm2_mu_TPMT_SYM_DEF_OBJECT_Marshal (buffer, &p->symmetric);
   grub_tpm2_mu_TPMT_ECC_SCHEME_Marshal (buffer, &p->scheme);
@@ -288,8 +288,8 @@ grub_tpm2_mu_TPMS_ECC_PARMS_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMU_PUBLIC_PARMS_Marshal (grub_tpm2_buffer_t buffer,
-					grub_uint32_t type,
-					TPMU_PUBLIC_PARMS *p)
+					const grub_uint32_t type,
+					const TPMU_PUBLIC_PARMS *p)
 {
   switch (type)
     {
@@ -313,7 +313,7 @@ grub_tpm2_mu_TPMU_PUBLIC_PARMS_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMS_ECC_POINT_Marshal (grub_tpm2_buffer_t buffer,
-				     TPMS_ECC_POINT *p)
+				     const TPMS_ECC_POINT *p)
 {
   grub_tpm2_mu_TPM2B_Marshal (buffer, p->x.size, p->x.buffer);
   grub_tpm2_mu_TPM2B_Marshal (buffer, p->y.size, p->y.buffer);
@@ -321,8 +321,8 @@ grub_tpm2_mu_TPMS_ECC_POINT_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMU_PUBLIC_ID_Marshal (grub_tpm2_buffer_t buffer,
-				     TPMI_ALG_PUBLIC type,
-				     TPMU_PUBLIC_ID *p)
+				     const TPMI_ALG_PUBLIC type,
+				     const TPMU_PUBLIC_ID *p)
 {
   switch(type)
     {
@@ -344,7 +344,7 @@ grub_tpm2_mu_TPMU_PUBLIC_ID_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMT_PUBLIC_Marshal (grub_tpm2_buffer_t buffer,
-				  TPMT_PUBLIC *p)
+				  const TPMT_PUBLIC *p)
 {
   grub_tpm2_buffer_pack_u16 (buffer, p->type);
   grub_tpm2_buffer_pack_u16 (buffer, p->nameAlg);
@@ -356,7 +356,7 @@ grub_tpm2_mu_TPMT_PUBLIC_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPM2B_PUBLIC_Marshal (grub_tpm2_buffer_t buffer,
-				   TPM2B_PUBLIC *p)
+				   const TPM2B_PUBLIC *p)
 {
   grub_uint32_t start;
   grub_uint16_t size;
@@ -377,7 +377,7 @@ grub_tpm2_mu_TPM2B_PUBLIC_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPMS_SENSITIVE_CREATE_Marshal (grub_tpm2_buffer_t buffer,
-					    TPMS_SENSITIVE_CREATE *p)
+					    const TPMS_SENSITIVE_CREATE *p)
 {
   grub_tpm2_mu_TPM2B_Marshal (buffer, p->userAuth.size, p->userAuth.buffer);
   grub_tpm2_mu_TPM2B_Marshal (buffer, p->data.size, p->data.buffer);
@@ -385,7 +385,7 @@ grub_tpm2_mu_TPMS_SENSITIVE_CREATE_Marshal (grub_tpm2_buffer_t buffer,
 
 void
 grub_tpm2_mu_TPM2B_SENSITIVE_CREATE_Marshal (grub_tpm2_buffer_t buffer,
-					     TPM2B_SENSITIVE_CREATE *sensitiveCreate)
+					     const TPM2B_SENSITIVE_CREATE *sensitiveCreate)
 {
   grub_uint32_t start;
   grub_uint16_t size;
grub-core/tpm2/tpm2.c +34 -34

145 lines not on this page · raw message

include/grub/tpm2/internal/functions.h +32 -32

123 lines not on this page · raw message

include/grub/tpm2/mu.h +32 -32

146 lines not on this page · raw message

Signature · 1 line
2.35.3
author
#16 lore raw

PATCH 10/14 tpm2: resend the command on TPM_RC_RETRY

In reply to Gary Lin

Sometimes TPM may return TPM_RC_RETRY for some reason, and the only thing we can do is to send the command again. To avoid pending in the while loop indefinitely, just try to send the command 3 times.

---
 grub-core/tpm2/tpm2.c | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)
grub-core/tpm2/tpm2.c +28 -5
index 6c476ff31..bc1d797d4 100644
@@ -25,11 +25,11 @@
 #include <grub/types.h>
 
 static TPM_RC
-grub_tpm2_submit_command (const TPMI_ST_COMMAND_TAG tag,
-			  const TPM_CC commandCode,
-			  TPM_RC *responseCode,
-			  const struct grub_tpm2_buffer *in,
-			  struct grub_tpm2_buffer *out)
+grub_tpm2_submit_command_real (const TPMI_ST_COMMAND_TAG tag,
+			       const TPM_CC commandCode,
+			       TPM_RC *responseCode,
+			       const struct grub_tpm2_buffer *in,
+			       struct grub_tpm2_buffer *out)
 {
   grub_err_t err;
   struct grub_tpm2_buffer buf;
@@ -75,6 +75,29 @@ grub_tpm2_submit_command (const TPMI_ST_COMMAND_TAG tag,
   return TPM_RC_SUCCESS;
 }
 
+static TPM_RC
+grub_tpm2_submit_command (const TPMI_ST_COMMAND_TAG tag,
+			  const TPM_CC commandCode,
+			  TPM_RC *responseCode,
+			  const struct grub_tpm2_buffer *in,
+			  struct grub_tpm2_buffer *out)
+{
+  TPM_RC err;
+  int retry_cnt = 0;
+
+  /* Catch TPM_RC_RETRY and send the command again */
+  do {
+    err = grub_tpm2_submit_command_real (tag, commandCode, responseCode,
+					 in, out);
+    if (*responseCode != TPM_RC_RETRY)
+      break;
+
+    retry_cnt++;
+  } while (retry_cnt < 3);
+
+  return err;
+}
+
 TPM_RC
 TPM2_CreatePrimary (const TPMI_RH_HIERARCHY primaryHandle,
 		    const TPMS_AUTH_COMMAND *authCommand,
Signature · 1 line
2.35.3
author
#17 lore raw

PATCH 11/14 tpm2: check the command parameters of TPM2 commands

In reply to Gary Lin

Some command parameters should not be NULL. Add the conditional check to avoid the potential NULL pointer reference.

Besides, for TPM2_StartAuthSession, when 'tpmKey' is 'TPM_RH_NULL', the size of 'encryptedSalt' must be 0 per "TCG TPM2 Part3 Commands".

---
 grub-core/tpm2/tpm2.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
grub-core/tpm2/tpm2.c +20 -1
index bc1d797d4..ad9d52cd4 100644
@@ -127,6 +127,9 @@ TPM2_CreatePrimary (const TPMI_RH_HIERARCHY primaryHandle,
   TPM_RC responseCode;
   grub_uint32_t parameterSize;
 
+  if (!inSensitive || !inPublic || !outsideInfo || !creationPCR)
+    return TPM_RC_VALUE;
+
   if (!objectHandle)
     objectHandle = &objectHandleTmp;
   if (!outPublic)
@@ -210,6 +213,13 @@ TPM2_StartAuthSession (const TPMI_DH_OBJECT tpmKey,
   TPM_RC responseCode;
   grub_uint32_t param_size;
 
+  if (!nonceCaller || !symmetric)
+    return TPM_RC_VALUE;
+
+  if (tpmKey == TPM_RH_NULL &&
+      (encryptedSalt && encryptedSalt->size != 0))
+    return TPM_RC_VALUE;
+
   if (!sessionHandle)
     sessionHandle = &sessionHandleTmp;
   if (!nonceTpm)
@@ -272,6 +282,9 @@ TPM2_PolicyPCR (const TPMI_SH_POLICY policySessions,
   TPM_RC responseCode;
   grub_uint32_t param_size;
 
+  if (!pcrs)
+    return TPM_RC_VALUE;
+
   if (!authResponse)
     authResponse = &authResponseTmp;
 
@@ -363,6 +376,9 @@ TPM2_Load (const TPMI_DH_OBJECT parent_handle,
   TPM_RC responseCode;
   grub_uint32_t param_size;
 
+  if (!inPrivate || !inPublic)
+    return TPM_RC_VALUE;
+
   if (!objectHandle)
     objectHandle = &objectHandleTmp;
   if (!name)
@@ -506,7 +522,7 @@ TPM2_PCR_Read (const TPMS_AUTH_COMMAND *authCommand,
   grub_uint32_t parameterSize;
 
   if (!pcrSelectionIn)
-    return TPM_RC_FAILURE;
+    return TPM_RC_VALUE;
 
   if (!pcrUpdateCounter)
     pcrUpdateCounter = &pcrUpdateCounterTmp;
@@ -625,6 +641,9 @@ TPM2_Create (const TPMI_DH_OBJECT parentHandle,
   TPM_RC rc;
   grub_uint32_t parameterSize;
 
+  if (!inSensitive || !inPublic || !outsideInfo || !creationPCR)
+    return TPM_RC_VALUE;
+
   if (!outPrivate)
     outPrivate = &outPrivateTmp;
   if (!outPublic)
Signature · 1 line
2.35.3
author
#18 lore raw

PATCH 12/14 tpm2: pack the missing authorization command for TPM2_PCR_Read

In reply to Gary Lin

When the caller of TPM2_PCR_Read() passes a valid authorization command, we should pack it into the 'in' buffer before sending the command.

---
 grub-core/tpm2/tpm2.c | 2 ++
 1 file changed, 2 insertions(+)
grub-core/tpm2/tpm2.c +2 -0
index ad9d52cd4..470fe7fc1 100644
@@ -535,6 +535,8 @@ TPM2_PCR_Read (const TPMS_AUTH_COMMAND *authCommand,
 
   /* Marshal */
   grub_tpm2_buffer_init (&in);
+  if (authCommand)
+    grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
   grub_tpm2_mu_TPML_PCR_SELECTION_Marshal (&in, pcrSelectionIn);
   if (in.error)
     return TPM_RC_FAILURE;
Signature · 1 line
2.35.3
author
#19 lore raw

PATCH 13/14 tpm2: allow some command parameters to be NULL

In reply to Gary Lin

There are some parameters of TPM2 commmands allowing to be empty such as 'encryptedSalt' of 'TPM2_StartAuthSession' and 'pcrDigest' of 'TPM2_PolicyPCR'. Instead of forcing the user of those functions to declare an empty variable, we can just pack a u16 zero to fabricate an empty variable when the user passes NULL for them.

This also fixes the potential crash caused by grub_tpm2_protector_srk_recover() that invokes TPM2_PolicyPCR() with a NULL pcrDigest.

---
 grub-core/tpm2/tpm2.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
grub-core/tpm2/tpm2.c +8 -2
index 470fe7fc1..d67699a24 100644
@@ -238,7 +238,10 @@ TPM2_StartAuthSession (const TPMI_DH_OBJECT tpmKey,
   if (tag == TPM_ST_SESSIONS)
     grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
   grub_tpm2_mu_TPM2B_Marshal (&in, nonceCaller->size, nonceCaller->buffer);
-  grub_tpm2_mu_TPM2B_Marshal (&in, encryptedSalt->size, encryptedSalt->secret);
+  if (encryptedSalt)
+    grub_tpm2_mu_TPM2B_Marshal (&in, encryptedSalt->size, encryptedSalt->secret);
+  else
+    grub_tpm2_buffer_pack_u16 (&in, 0);
   grub_tpm2_buffer_pack_u8 (&in, sessionType);
   grub_tpm2_mu_TPMT_SYM_DEF_Marshal (&in, symmetric);
   grub_tpm2_buffer_pack_u16 (&in, authHash);
@@ -295,7 +298,10 @@ TPM2_PolicyPCR (const TPMI_SH_POLICY policySessions,
   grub_tpm2_buffer_pack_u32 (&in, policySessions);
   if (tag == TPM_ST_SESSIONS)
     grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
-  grub_tpm2_mu_TPM2B_Marshal (&in, pcrDigest->size, pcrDigest->buffer);
+  if (pcrDigest)
+    grub_tpm2_mu_TPM2B_Marshal (&in, pcrDigest->size, pcrDigest->buffer);
+  else
+    grub_tpm2_buffer_pack_u16 (&in, 0);
   grub_tpm2_mu_TPML_PCR_SELECTION_Marshal (&in, pcrs);
   if (in.error)
     return TPM_RC_FAILURE;
Signature · 1 line
2.35.3
author
#20 lore raw

PATCH 14/14 tpm2: remove the unnecessary variables

In reply to Gary Lin

Since the NULL 'encryptedSalt' of 'TPM2_StartAuthSession' is handled as an empty TPM2B structure, there is no need to declare an empty salt. As for 'nonceTPM', we don't use in the following TPM2 commands, so we can safely ignore it.

---
 grub-core/tpm2/module.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
grub-core/tpm2/module.c +2 -4
index bebdecd1c..580c1f972 100644
@@ -311,9 +311,7 @@ grub_tpm2_protector_srk_recover (const struct grub_tpm2_protector_context *ctx,
   grub_size_t sealed_key_size;
   TPM_HANDLE srk_handle;
   TPM2B_NONCE nonceCaller = { 0 };
-  TPM2B_ENCRYPTED_SECRET salt = { 0 };
   TPMT_SYM_DEF symmetric = { 0 };
-  TPM2B_NONCE nonceTPM = { 0 };
   TPMI_SH_AUTH_SESSION session;
   TPML_PCR_SELECTION pcrSel = {
     .count = 1,
@@ -364,9 +362,9 @@ grub_tpm2_protector_srk_recover (const struct grub_tpm2_protector_context *ctx,
   nonceCaller.size = TPM_SHA256_DIGEST_SIZE;
   symmetric.algorithm = TPM_ALG_NULL;
 
-  rc = TPM2_StartAuthSession (TPM_RH_NULL, TPM_RH_NULL, 0, &nonceCaller, &salt,
+  rc = TPM2_StartAuthSession (TPM_RH_NULL, TPM_RH_NULL, NULL, &nonceCaller, NULL,
 			      TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256,
-			      &session, &nonceTPM, 0);
+			      &session, NULL, NULL);
   if (rc)
     {
       grub_error (err, N_("Failed to start auth session (TPM2_StartAuthSession "
Signature · 1 line
2.35.3
#21 lore raw
In reply to Gary Lin
Quoted 5 lines · Gary Lin via Grub-devel

The patch series "Automatic TPM Disk Unlock" posted by Hernan Gatta introduces the key protector framework and TPM2 stack to GRUB2, and it's a useful feature for the systems to implement full disk encryption. However, it seems the development was stalled for a while, and I'd like to push it forward.

Thank you for updating this patch set. Certainly I want to have it in the GRUB upstream. Sadly I cannot consider this as a 2.12 release material. So, I will not review these patches immediately due to their low priority. Though I have a few comments below...

Quoted 6 lines

Patch 1~5 are Hernan Gatta's patch series(*) with a few modifications:
- Converting 8 spaces into 1 tab
- Merging the minor build fix from Michael Chang
- Replacing "lu" with "PRIuGRUB_SIZE" for grub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master

Thank you for doing all of this!

Quoted 3 lines

To minimize the changes to Patch 1~5, the follow-up fixes (Patch 6~14) from my colleagues and me are committed separately. Those patches fix the problems we found while testing the original patchset.

Please merge all patches after #5 into original patch series and add your and your colleagues SOBs to relevant patches.

And of course take into account James comments...

Daniel

author
#22 lore raw
Quoted 7 lines · Daniel Kiper
Quoted 5 lines · Gary Lin via Grub-devel

The patch series "Automatic TPM Disk Unlock" posted by Hernan Gatta introduces the key protector framework and TPM2 stack to GRUB2, and it's a useful feature for the systems to implement full disk encryption. However, it seems the development was stalled for a while, and I'd like to push it forward.

Quoted 5 lines

Thank you for updating this patch set. Certainly I want to have it in the GRUB upstream. Sadly I cannot consider this as a 2.12 release material. So, I will not review these patches immediately due to their low priority. Though I have a few comments below...

I see.

Quoted 7 lines
Quoted 6 lines

Patch 1~5 are Hernan Gatta's patch series(*) with a few modifications:
- Converting 8 spaces into 1 tab
- Merging the minor build fix from Michael Chang
- Replacing "lu" with "PRIuGRUB_SIZE" for grub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master

Quoted 9 lines

Thank you for doing all of this!

Quoted 3 lines

To minimize the changes to Patch 1~5, the follow-up fixes (Patch 6~14) from my colleagues and me are committed separately. Those patches fix the problems we found while testing the original patchset.

Please merge all patches after #5 into original patch series and add your and your colleagues SOBs to relevant patches.

Will merge those patchs in V2.

Quoted 2 lines

And of course take into account James comments...

I'm evaluating the effort to adopt the TPM 2.0 key file format. However, the libtasn1 patches (*) are still not merged, and it's probably not practical to implement my own asn1 parser. It'd be nice if the libtasn1 patches could be merged soon.

Thanks,

Gary Lin

(*) https://lists.gnu.org/archive/html/grub-devel/2021-06/msg00055.html

#23 lore raw

Adding folks involved in appended signature secure boot support development...

Quoted 8 lines · Gary Lin via Grub-devel
Quoted 7 lines · Daniel Kiper
Quoted 5 lines · Gary Lin via Grub-devel

The patch series "Automatic TPM Disk Unlock" posted by Hernan Gatta introduces the key protector framework and TPM2 stack to GRUB2, and it's a useful feature for the systems to implement full disk encryption. However, it seems the development was stalled for a while, and I'd like to push it forward.

Quoted 6 lines
Quoted 5 lines

Thank you for updating this patch set. Certainly I want to have it in the GRUB upstream. Sadly I cannot consider this as a 2.12 release material. So, I will not review these patches immediately due to their low priority. Though I have a few comments below...

I see.

Thank you!

Quoted 9 lines
Quoted 9 lines
Quoted 6 lines

Patch 1~5 are Hernan Gatta's patch series(*) with a few modifications:
- Converting 8 spaces into 1 tab
- Merging the minor build fix from Michael Chang
- Replacing "lu" with "PRIuGRUB_SIZE" for grub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master

Thank you for doing all of this!

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

To minimize the changes to Patch 1~5, the follow-up fixes (Patch 6~14) from my colleagues and me are committed separately. Those patches fix the problems we found while testing the original patchset.

Please merge all patches after #5 into original patch series and add your and your colleagues SOBs to relevant patches.

Will merge those patchs in V2.

Cool! Thank you!

Quoted 6 lines
Quoted 2 lines

And of course take into account James comments...

I'm evaluating the effort to adopt the TPM 2.0 key file format. However, the libtasn1 patches (*) are still not merged, and it's probably not practical to implement my own asn1 parser. It'd be nice if the libtasn1 patches could be merged soon.

I cannot see any reason to not make libtasn1 patches part of this series. And potentially this series could be merged earlier because it is smaller one. Anyway, please coordinate this with folks working on appended signature...

Daniel