Automatic Disk Unlock with TPM2
v2 cover Automatic Disk Unlock with TPM2
This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption.
To support TPM 2.0 Key File format(*2), patch 1~6 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.
Patch 7~11 are Hernan Gatta's patches with the follow-up fixes:
- 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
- Removing the measurement on the sealed key
- Based ont the patch from Olaf Kirch <[email protected]>
- Adjusting the input parameters of TPM2_EvictControl to match the order in "TCG TPM2 Part3 Commands"
- Declaring the input arguments of TPM2 functions as const
- Resending TPM2 commands on TPM_RC_RETRY
- Adding checks for the parameters of TPM2 commands
- Packing the missing authorization command for TPM2_PCR_Read
- Tweaking the TPM2 command functions to allow some parameters to be NULL
- Only enabling grub-protect for "efi" since the TPM2 stack currently relies on the EFI TCG2 protocol to send TPM2 commands
- Using grub_cpu_to_be*() in the TPM2 stack instead grub_swap_bytes*() for the marshal functions
- Changing the short name of "--protector" of "cryptomount" from "-k" to "-P" to avoid the conflict with "--key-file"
- Adding the primitive support for TPM 2.0 Key File Format besides the raw sealed key
- Adding the external libtasn1 dependency for grub-protect since "asn1_write_value()" is disabled in the embedded libtasn1
To utilize the TPM2 key protector to unlock the encrypted partition (sdb1), here are the sample steps:
1. Add an extra random key for LUKS (luks-key)
$ dd if=/dev/urandom of=luks-key bs=1 count=32 $ sudo cryptsetup luksAddKey /dev/sdb1 luks-key --pbkdf=pbkdf2
2. Seal the key
$ sudo grub-protect --action=add \
--protector=tpm2 \
--tpm2key \
--tpm2-keyfile=luks-key \
--tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm3. Unseal the key with the proper commands in grub.cfg:
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -u SDB1_UUID -P tpm2
(*1) https://lists.gnu.org/archive/html/grub-devel/2022-02/msg00006.html
(*2) https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
(*3) https://lists.gnu.org/archive/html/grub-devel/2021-06/msg00044.html
Daniel Axtens (6):
posix_wrap: tweaks in preparation for libtasn1
libtasn1: import libtasn1-4.19.0
libtasn1: disable code not needed in grub
libtasn1: changes for grub compatibility
libtasn1: compile into asn1 module
test_asn1: test module for libtasn1
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
.gitignore | 2 + Makefile.util.def | 29 + configure.ac | 9 + grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 42 + grub-core/disk/cryptodisk.c | 175 +- grub-core/kern/protectors.c | 75 + grub-core/lib/libtasn1/COPYING | 16 + grub-core/lib/libtasn1/README.md | 98 + grub-core/lib/libtasn1/lib/coding.c | 1433 ++++++++++ grub-core/lib/libtasn1/lib/decoding.c | 2504 +++++++++++++++++ grub-core/lib/libtasn1/lib/element.c | 1110 ++++++++ grub-core/lib/libtasn1/lib/element.h | 42 + grub-core/lib/libtasn1/lib/errors.c | 103 + grub-core/lib/libtasn1/lib/gstr.c | 74 + grub-core/lib/libtasn1/lib/gstr.h | 50 + grub-core/lib/libtasn1/lib/int.h | 221 ++ grub-core/lib/libtasn1/lib/parser_aux.c | 1179 ++++++++ grub-core/lib/libtasn1/lib/parser_aux.h | 172 ++ grub-core/lib/libtasn1/lib/structure.c | 1227 ++++++++ grub-core/lib/libtasn1/lib/structure.h | 46 + .../tests/CVE-2018-1000654-1_asn1_tab.h | 32 + .../tests/CVE-2018-1000654-2_asn1_tab.h | 36 + .../libtasn1_wrap/tests/CVE-2018-1000654.c | 61 + .../lib/libtasn1_wrap/tests/Test_overflow.c | 138 + .../lib/libtasn1_wrap/tests/Test_simple.c | 207 ++ .../lib/libtasn1_wrap/tests/Test_strings.c | 150 + .../libtasn1_wrap/tests/object-id-decoding.c | 116 + .../libtasn1_wrap/tests/object-id-encoding.c | 120 + .../lib/libtasn1_wrap/tests/octet-string.c | 211 ++ .../lib/libtasn1_wrap/tests/reproducers.c | 81 + grub-core/lib/libtasn1_wrap/wrap.c | 26 + grub-core/lib/libtasn1_wrap/wrap_tests.c | 75 + grub-core/lib/libtasn1_wrap/wrap_tests.h | 38 + grub-core/lib/posix_wrap/limits.h | 1 + grub-core/lib/posix_wrap/stdlib.h | 8 + grub-core/lib/posix_wrap/sys/types.h | 1 + grub-core/tpm2/args.c | 129 + grub-core/tpm2/buffer.c | 145 + grub-core/tpm2/module.c | 833 ++++++ grub-core/tpm2/mu.c | 807 ++++++ grub-core/tpm2/tcg2.c | 143 + grub-core/tpm2/tpm2.c | 761 +++++ grub-core/tpm2/tpm2key.asn | 31 + grub-core/tpm2/tpm2key.c | 218 ++ grub-core/tpm2/tpm2key_asn1_tab.c | 34 + include/grub/cryptodisk.h | 14 + include/grub/libtasn1.h | 645 +++++ 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 + include/grub/tpm2/tpm2key.h | 40 + tests/test_asn1.in | 12 + util/grub-protect.c | 1472 ++++++++++ 60 files changed, 16841 insertions(+), 32 deletions(-) create mode 100644 grub-core/kern/protectors.c create mode 100644 grub-core/lib/libtasn1/COPYING create mode 100644 grub-core/lib/libtasn1/README.md create mode 100644 grub-core/lib/libtasn1/lib/coding.c create mode 100644 grub-core/lib/libtasn1/lib/decoding.c create mode 100644 grub-core/lib/libtasn1/lib/element.c create mode 100644 grub-core/lib/libtasn1/lib/element.h create mode 100644 grub-core/lib/libtasn1/lib/errors.c create mode 100644 grub-core/lib/libtasn1/lib/gstr.c create mode 100644 grub-core/lib/libtasn1/lib/gstr.h create mode 100644 grub-core/lib/libtasn1/lib/int.h create mode 100644 grub-core/lib/libtasn1/lib/parser_aux.c create mode 100644 grub-core/lib/libtasn1/lib/parser_aux.h create mode 100644 grub-core/lib/libtasn1/lib/structure.c create mode 100644 grub-core/lib/libtasn1/lib/structure.h create mode 100644 grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-1_asn1_tab.h create mode 100644 grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-2_asn1_tab.h create mode 100644 grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/Test_overflow.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/Test_simple.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/Test_strings.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/object-id-decoding.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/object-id-encoding.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/octet-string.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/reproducers.c create mode 100644 grub-core/lib/libtasn1_wrap/wrap.c create mode 100644 grub-core/lib/libtasn1_wrap/wrap_tests.c create mode 100644 grub-core/lib/libtasn1_wrap/wrap_tests.h 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 grub-core/tpm2/tpm2key.asn create mode 100644 grub-core/tpm2/tpm2key.c create mode 100644 grub-core/tpm2/tpm2key_asn1_tab.c create mode 100644 include/grub/libtasn1.h 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 include/grub/tpm2/tpm2key.h create mode 100644 tests/test_asn1.in create mode 100644 util/grub-protect.c
Signature · 1 line
2.35.3
PATCH v2 1/11 posix_wrap: tweaks in preparation for libtasn1
From: Daniel Axtens <[email protected]>
- Define SIZEOF_UNSIGNED_LONG_INT, it's the same as SIZEOF_UNSIGNED_LONG.
- Define WORD_BIT, the size in bits of an int. This is a defined in the Single Unix Specification and in gnulib's limits.h. gnulib assumes it's 32 bits on all our platforms, including 64 bit platforms, so we also use that value.
- Provide strto[u]l[l] preprocessor macros that resolve to grub_strto[u]l[l]. To avoid gcrypt redefining strtoul, we also define HAVE_STRTOUL here.
--- grub-core/lib/posix_wrap/limits.h | 1 + grub-core/lib/posix_wrap/stdlib.h | 8 ++++++++ grub-core/lib/posix_wrap/sys/types.h | 1 + 3 files changed, 10 insertions(+)
grub-core/lib/posix_wrap/limits.h +1 -0
@@ -41,5 +41,6 @@#define LONG_MAX GRUB_LONG_MAX#define CHAR_BIT 8 +#define WORD_BIT 32#endif
grub-core/lib/posix_wrap/stdlib.h +8 -0
@@ -64,4 +64,12 @@ abort (void) grub_abort (); } +#define strtol grub_strtol + +/* for libgcrypt */ +#define HAVE_STRTOUL +#define strtoul grub_strtoul + +#define strtoull grub_strtoull +#endif
grub-core/lib/posix_wrap/sys/types.h +1 -0
@@ -50,6 +50,7 @@ typedef grub_uint8_t byte;typedef grub_addr_t uintptr_t ;#define SIZEOF_UNSIGNED_LONG GRUB_CPU_SIZEOF_LONG +#define SIZEOF_UNSIGNED_LONG_INT GRUB_CPU_SIZEOF_LONG#define SIZEOF_UNSIGNED_INT 4#define SIZEOF_UNSIGNED_LONG_LONG 8#define SIZEOF_UNSIGNED_SHORT 2
Signature · 1 line
2.35.3
PATCH v2 2/11 libtasn1: import libtasn1-4.19.0
In reply to Gary LinFrom: Daniel Axtens <[email protected]>
Import a very trimmed-down set of libtasn1 files:
pushd /tmp
wget https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
tar -xf libtasn1-4.19.0.tar.gz
popd
pushd grub-core/lib
rm -rf libtasn1
mkdir libtasn1
cp /tmp/libtasn1-4.19.0/{README.md,COPYING} libtasn1/
mkdir libtasn1/lib
cp /tmp/libtasn1-4.19.0/lib/{coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h} libtasn1/lib cp /tmp/libtasn1-4.19.0/lib/includes/libtasn1.h ../../include/grub/
git add libtasn1/ ../../include/grub/libtasn1.h
popd
--- grub-core/lib/libtasn1/COPYING | 16 + grub-core/lib/libtasn1/README.md | 98 + grub-core/lib/libtasn1/lib/coding.c | 1425 +++++++++++++ grub-core/lib/libtasn1/lib/decoding.c | 2501 +++++++++++++++++++++++ grub-core/lib/libtasn1/lib/element.c | 1109 ++++++++++ grub-core/lib/libtasn1/lib/element.h | 42 + grub-core/lib/libtasn1/lib/errors.c | 100 + grub-core/lib/libtasn1/lib/gstr.c | 74 + grub-core/lib/libtasn1/lib/gstr.h | 50 + grub-core/lib/libtasn1/lib/int.h | 221 ++ grub-core/lib/libtasn1/lib/parser_aux.c | 1178 +++++++++++ grub-core/lib/libtasn1/lib/parser_aux.h | 172 ++ grub-core/lib/libtasn1/lib/structure.c | 1225 +++++++++++ grub-core/lib/libtasn1/lib/structure.h | 46 + include/grub/libtasn1.h | 643 ++++++ 15 files changed, 8900 insertions(+) create mode 100644 grub-core/lib/libtasn1/COPYING create mode 100644 grub-core/lib/libtasn1/README.md create mode 100644 grub-core/lib/libtasn1/lib/coding.c create mode 100644 grub-core/lib/libtasn1/lib/decoding.c create mode 100644 grub-core/lib/libtasn1/lib/element.c create mode 100644 grub-core/lib/libtasn1/lib/element.h create mode 100644 grub-core/lib/libtasn1/lib/errors.c create mode 100644 grub-core/lib/libtasn1/lib/gstr.c create mode 100644 grub-core/lib/libtasn1/lib/gstr.h create mode 100644 grub-core/lib/libtasn1/lib/int.h create mode 100644 grub-core/lib/libtasn1/lib/parser_aux.c create mode 100644 grub-core/lib/libtasn1/lib/parser_aux.h create mode 100644 grub-core/lib/libtasn1/lib/structure.c create mode 100644 grub-core/lib/libtasn1/lib/structure.h create mode 100644 include/grub/libtasn1.h
grub-core/lib/libtasn1/COPYING new file +16 -0
@@ -0,0 +1,16 @@ +LICENSING +========= + +The libtasn1 library is released under the GNU Lesser General Public +License (LGPL) version 2.1 or later; see [COPYING.LESSER](doc/COPYING.LESSER) +for the license terms. + +The GNU LGPL applies to the main libtasn1 library, while the +included applications library are under the GNU GPL version 3. +The libtasn1 library is located in the lib directory, while the applications +in src/. + +The documentation in doc/ is under the GNU FDL license 1.3. + +For any copyright year range specified as YYYY-ZZZZ in this package +note that the range specifies every single year in that closed interval.
grub-core/lib/libtasn1/README.md new file +98 -0
98 lines not on this page · raw message
grub-core/lib/libtasn1/lib/coding.c new file +1425 -0
1,425 lines not on this page · raw message
grub-core/lib/libtasn1/lib/decoding.c new file +2501 -0
1,461 lines not on this page · raw message
grub-core/lib/libtasn1/lib/element.c new file +1109 -0
1,109 lines not on this page · raw message
grub-core/lib/libtasn1/lib/element.h new file +42 -0
42 lines not on this page · raw message
grub-core/lib/libtasn1/lib/errors.c new file +100 -0
100 lines not on this page · raw message
grub-core/lib/libtasn1/lib/gstr.c new file +74 -0
74 lines not on this page · raw message
grub-core/lib/libtasn1/lib/gstr.h new file +50 -0
50 lines not on this page · raw message
grub-core/lib/libtasn1/lib/int.h new file +221 -0
221 lines not on this page · raw message
grub-core/lib/libtasn1/lib/parser_aux.c new file +1178 -0
1,178 lines not on this page · raw message
grub-core/lib/libtasn1/lib/parser_aux.h new file +172 -0
172 lines not on this page · raw message
grub-core/lib/libtasn1/lib/structure.c new file +1225 -0
1,225 lines not on this page · raw message
grub-core/lib/libtasn1/lib/structure.h new file +46 -0
46 lines not on this page · raw message
include/grub/libtasn1.h new file +643 -0
643 lines not on this page · 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.
PATCH v2 3/11 libtasn1: disable code not needed in grub
In reply to Gary LinFrom: Daniel Axtens <[email protected]>
We don't expect to be able to write ASN.1, only read it, so we can disable some code.
Do that with #if 0/#endif, rather than deletion. This means that the difference between upstream and grub is smaller, which should make updating libtasn1 easier in the future.
With these exclusions we also avoid the need for minmax.h, which is convenient because it means we don't have to import it from gnulib.
--- grub-core/lib/libtasn1/lib/coding.c | 12 ++++++++++-- grub-core/lib/libtasn1/lib/decoding.c | 2 ++ grub-core/lib/libtasn1/lib/element.c | 4 ++-- grub-core/lib/libtasn1/lib/errors.c | 3 +++ grub-core/lib/libtasn1/lib/structure.c | 10 ++++++---- include/grub/libtasn1.h | 15 +++++++++++++++ 6 files changed, 38 insertions(+), 8 deletions(-)
grub-core/lib/libtasn1/lib/coding.c +10 -2
@@ -30,11 +30,11 @@#include "parser_aux.h" #include <gstr.h> #include "element.h" -#include "minmax.h" #include <structure.h> #define MAX_TAG_LEN 16 +#if 0 /******************************************************/ /* Function : _asn1_error_description_value_not_found */ /* Description: creates the ErrorDescription string */
@@ -58,6 +58,7 @@ _asn1_error_description_value_not_found (asn1_node node, Estrcat (ErrorDescription, "' not found"); } +#endif /** * asn1_length_der:
@@ -244,6 +245,7 @@ asn1_encode_simple_der (unsigned int etype, const unsigned char *str,return ASN1_SUCCESS ; } +#if 0 /******************************************************/ /* Function : _asn1_time_der */ /* Description: creates the DER coding for a TIME */
@@ -278,7 +280,7 @@ _asn1_time_der (unsigned char *str, int str_len, unsigned char *der, return ASN1_SUCCESS; } - +#endif /* void
@@ -519,6 +521,7 @@ asn1_bit_der (const unsigned char *str, int bit_len, } +#if 0 /******************************************************/ /* Function : _asn1_complete_explicit_tag */ /* Description: add the length coding to the EXPLICIT */
@@ -595,6 +598,7 @@ _asn1_complete_explicit_tag (asn1_node node, unsigned char *der,return ASN1_SUCCESS ; } +#endif const tag_and_class_st _asn1_tags[] = { [ASN1_ETYPE_GENERALSTRING] =
@@ -647,6 +651,8 @@ const tag_and_class_st _asn1_tags[] = {unsigned int _asn1_tags_size =sizeof (_asn1_tags) /sizeof (_asn1_tags[0 ]); + +#if 0 /******************************************************/ /* Function : _asn1_insert_tag_der */ /* Description: creates the DER coding of tags of one */
@@ -1423,3 +1429,5 @@ error:asn1_delete_structure (&node); return err; } + +#endif
grub-core/lib/libtasn1/lib/decoding.c +2 -0
@@ -1620,6 +1620,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, int ider_len,return asn1_der_decoding2 (element, ider, &ider_len,0 , errorDescription); } +#if 0 /** * asn1_der_decoding_element: * @structure: pointer to an ASN1 structure
@@ -1650,6 +1651,7 @@ asn1_der_decoding_element (asn1_node * structure, const char *elementName, {return asn1_der_decoding (structure, ider, len, errorDescription); } +#endif /** * asn1_der_decoding_startEnd:
grub-core/lib/libtasn1/lib/element.c +2 -2
@@ -191,7 +191,7 @@ _asn1_append_sequence_set (asn1_node node, struct node_tail_cache_st *pcache)return ASN1_SUCCESS ; } - +#if 0 /** * asn1_write_value: * @node_root: pointer to a structure
@@ -646,7 +646,7 @@ asn1_write_value (asn1_node node_root, const char *name, return ASN1_SUCCESS; } - +#endif #define PUT_VALUE ( ptr, ptr_size, data, data_size) \ *len = data_size; \
grub-core/lib/libtasn1/lib/errors.c +3 -0
@@ -57,6 +57,8 @@ static const libtasn1_error_entry error_algorithms[] = { {0, 0} }; + +#if 0 /** * asn1_perror: * @error: is an error returned by a libtasn1 function.
@@ -73,6 +75,7 @@ asn1_perror (int error)const char *str =asn1_strerror (error); fprintf (stderr, "LIBTASN1 ERROR: %s\n", str ? str : "(null)"); } +#endif /** * asn1_strerror:
grub-core/lib/libtasn1/lib/structure.c +6 -4
@@ -76,7 +76,7 @@ _asn1_find_left (asn1_node_const node)return node->left; } - +#if 0 int _asn1_create_static_structure (asn1_node_const pointer,char *output_file_name,char *vector_name)
@@ -155,7 +155,7 @@ _asn1_create_static_structure (asn1_node_const pointer, return ASN1_SUCCESS; } - +#endif /** * asn1_array2tree:
@@ -721,7 +721,7 @@ asn1_create_element (asn1_node_const definitions, const char *source_name,return res; } - +#if 0 /** * asn1_print_structure: * @out: pointer to the output file (e.g. stdout).
@@ -1062,7 +1062,7 @@ asn1_print_structure (FILE * out, asn1_node_const structure, const char *name, } } } - +#endif /**
@@ -1158,6 +1158,7 @@ asn1_find_structure_from_oid (asn1_node_const definitions,return NULL ;/* ASN1_ELEMENT_NOT_FOUND; */ } +#if 0 /** * asn1_copy_node: * @dst: Destination asn1 node.
@@ -1207,6 +1208,7 @@ asn1_copy_node (asn1_node dst, const char *dst_name, return result; } +#endif /** * asn1_dup_node:
include/grub/libtasn1.h +15 -0
@@ -318,6 +318,8 @@ extern "C"/* Functions definitions */ /***********************************/ +/* These functions are not used in grub and should not be referenced. */ +# if 0 extern ASN1_API intasn1_parser2tree (const char *file,asn1_node * definitions,char *error_desc);
@@ -326,14 +328,17 @@ extern "C" asn1_parser2array (const char *inputFileName, const char *outputFileName, const char *vectorName, char *error_desc); +# endif extern ASN1_API intasn1_array2tree (const asn1_static_node * array,asn1_node * definitions,char *errorDescription); +# if 0 extern ASN1_API voidasn1_print_structure (FILE * out,asn1_node_const structure,const char *name,int mode); +# endif extern ASN1_API int asn1_create_element (asn1_node_const definitions,
@@ -347,9 +352,11 @@ extern "C" extern ASN1_API intasn1_delete_element (asn1_node structure,const char *element_name); +# if 0 extern ASN1_API intasn1_write_value (asn1_node node_root,const char *name,const void *ivalue,int len); +# endif extern ASN1_API int asn1_read_value (asn1_node_const root, const char *name,
@@ -366,9 +373,11 @@ extern "C" asn1_number_of_elements (asn1_node_const element, const char *name, int *num); +# if 0 extern ASN1_API intasn1_der_coding (asn1_node_const element,const char *name,void *ider,int *len,char *ErrorDescription); +# endif extern ASN1_API int asn1_der_decoding2 (asn1_node * element, const void *ider,
@@ -379,6 +388,7 @@ extern "C" asn1_der_decoding (asn1_node * element, const void *ider, int ider_len, char *errorDescription); +# if 0 /* Do not use. Use asn1_der_decoding() instead. */ extern ASN1_API int asn1_der_decoding_element (asn1_node * structure,
@@ -386,6 +396,7 @@ extern "C" const void *ider, int len, char *errorDescription) _ASN1_GCC_ATTR_DEPRECATED; +# endif extern ASN1_API int asn1_der_decoding_startEnd (asn1_node element,
@@ -411,12 +422,16 @@ extern "C"const char *oidValue); +# if 0 __LIBTASN1_PURE__ extern ASN1_API const char *asn1_check_version (const char *req_version); +# endif __LIBTASN1_PURE__ extern ASN1_API const char *asn1_strerror (int error); +# if 0 extern ASN1_API void asn1_perror (int error); +# endif # define ASN1_MAX_TAG_SIZE 4# define ASN1_MAX_LENGTH_SIZE 9
Signature · 1 line
2.35.3
PATCH v2 4/11 libtasn1: changes for grub compatibility
In reply to Gary LinFrom: Daniel Axtens <[email protected]>
Do a few things to make libtasn1 compile as part of grub:
- redefine _asn1_strcat. grub removed strcat so replace it with the appropriate calls to memcpy and strlen. Use this internally where strcat was used.
- replace c_isdigit with grub_isdigit (and don't import c-ctype from gnulib) grub_isdigit provides the same functionality as c_isdigit: it determines if the input is an ASCII digit without regard for locale.
- replace GL_ATTRIBUTE_PURE with __attribute__((pure)) which been supported since gcc-2.96. This avoids messing around with gnulib.
- adjust libtasn1.h: drop the ASN1_API logic, it's not needed for our modules. Unconditionally support const and pure attributes and adjust header paths.
- adjust header paths to "grub/libtasn1.h".
- replace a 64 bit division with a call to grub_divmod64, preventing creation of __udivdi3 calls on 32 bit platforms.
--- grub-core/lib/libtasn1/lib/decoding.c | 11 +++++----- grub-core/lib/libtasn1/lib/element.c | 3 ++- grub-core/lib/libtasn1/lib/gstr.c | 4 ++-- grub-core/lib/libtasn1/lib/int.h | 4 ++-- grub-core/lib/libtasn1/lib/parser_aux.c | 7 +++--- include/grub/libtasn1.h | 29 +++++++------------------ 6 files changed, 24 insertions(+), 34 deletions(-)
grub-core/lib/libtasn1/lib/decoding.c +6 -5
@@ -32,7 +32,8 @@#include <element.h> #include <limits.h> #include <intprops.h> -#include "c-ctype.h" + +#define c_isdigit grub_isdigit#ifdef DEBUG # define warn () fprintf(stderr, "%s: %d\n", __func__, __LINE__)
@@ -2016,8 +2017,8 @@ asn1_expand_octet_string (asn1_node_const definitions, asn1_node * element, (p2->type & CONST_ASSIGN)) {strcpy (name, definitions->name); -strcat (name,"." ); -strcat (name, p2->name); +_asn1_strcat (name,"." ); +_asn1_strcat (name, p2->name); len =sizeof (value); result =asn1_read_value (definitions, name, value, &len);
@@ -2034,8 +2035,8 @@ asn1_expand_octet_string (asn1_node_const definitions, asn1_node * element,if (p2) {strcpy (name, definitions->name); -strcat (name,"." ); -strcat (name, p2->name); +_asn1_strcat (name,"." ); +_asn1_strcat (name, p2->name); result =asn1_create_element (definitions, name, &aux);if (result ==ASN1_SUCCESS )
grub-core/lib/libtasn1/lib/element.c +2 -1
@@ -30,9 +30,10 @@#include "parser_aux.h" #include <gstr.h> #include "structure.h" -#include "c-ctype.h" #include "element.h" +#define c_isdigit grub_isdigit +void _asn1_hierarchical_name (asn1_node_const node,char *name,int name_size) {
grub-core/lib/libtasn1/lib/gstr.c +2 -2
@@ -36,13 +36,13 @@ _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)if (dest_tot_size - dest_size > str_size) { -strcat (dest, src); +_asn1_strcat (dest, src); }else {if (dest_tot_size > dest_size) { -strncat (dest, src, (dest_tot_size - dest_size) -1 ); +memcpy (dest + dest_size, src, (dest_tot_size - dest_size) -1 ); dest[dest_tot_size -1 ] =0 ; } }
grub-core/lib/libtasn1/lib/int.h +2 -2
@@ -35,7 +35,7 @@# include <sys/types.h> # endif -# include <libtasn1.h> +# include "grub/libtasn1.h" # define ASN1_SMALL_VALUE_SIZE 16
@@ -115,7 +115,7 @@ extern const tag_and_class_st _asn1_tags[];# define _asn1_strtoul (n,e,b) strtoul((const char *) n, e, b)# define _asn1_strcmp (a,b) strcmp((const char *)a, (const char *)b)# define _asn1_strcpy (a,b) strcpy((char *)a, (const char *)b) -# define _asn1_strcat (a,b) strcat((char *)a, (const char *)b) +# define _asn1_strcat (a,b) memcpy((char *)a + strlen((const char *)a), (const char *)b, strlen((const char *)b) + 1)# if SIZEOF_UNSIGNED_LONG_INT ==8 # define _asn1_strtou64 (n,e,b) strtoul((const char *) n, e, b)
grub-core/lib/libtasn1/lib/parser_aux.c +4 -3
@@ -26,7 +26,8 @@#include "gstr.h" #include "structure.h" #include "element.h" -#include "c-ctype.h" + +#define c_isdigit grub_isdigitchar _asn1_identifierMissing[ASN1_MAX_NAME_SIZE +1 ];/* identifier name not found */
@@ -40,7 +41,7 @@ char _asn1_identifierMissing[ASN1_MAX_NAME_SIZE + 1]; /* identifier name not fou#ifdef __clang__ __attribute__((no_sanitize ("integer" ))) #endif - _GL_ATTRIBUTE_PURE static unsigned int _asn1_hash_name (const char *x) + __attribute__((__pure__))static unsigned int _asn1_hash_name (const char *x) {const unsigned char *s = (unsigned char *) x;unsigned h =0 ;
@@ -632,7 +633,7 @@ _asn1_ltostr (int64_t v, char str[LTOSTR_MAX_SIZE]) count =0 ; do { - d = val / 10; + d = grub_divmod64(val, 10, NULL); r = val - d * 10; temp[start + count] = '0' + (char) r; count++;
include/grub/libtasn1.h +8 -21
@@ -34,29 +34,16 @@#ifndef LIBTASN1_H # define LIBTASN1_H -# ifndef ASN1_API -# if definedASN1_BUILDING && definedHAVE_VISIBILITY &&HAVE_VISIBILITY -# define ASN1_API __attribute__((__visibility__("default"))) -# elif definedASN1_BUILDING && defined _MSC_VER && ! definedASN1_STATIC -# define ASN1_API __declspec(dllexport) -# elif defined _MSC_VER && ! definedASN1_STATIC -# define ASN1_API __declspec(dllimport) -# else -# define ASN1_API -# endif -# endif +/* grub: ASN1_API is not used */ +# define ASN1_API + +/* grub: all our supported compilers support these attributes */ +# define __LIBTASN1_CONST__ __attribute__((const)) +# define __LIBTASN1_PURE__ __attribute__((pure)) -# ifdef __GNUC__ -# define __LIBTASN1_CONST__ __attribute__((const)) -# define __LIBTASN1_PURE__ __attribute__((pure)) -# else -# define __LIBTASN1_CONST__ -# define __LIBTASN1_PURE__ -# endif -# include <sys/types.h> -# include <time.h> -# include <stdio.h> /* for FILE* */ +# include <grub/types.h> +# include <grub/time.h> # ifdef __cplusplus extern "C"
Signature · 1 line
2.35.3
PATCH v2 5/11 libtasn1: compile into asn1 module
In reply to Gary LinFrom: Daniel Axtens <[email protected]>
Create a wrapper file that specifies the module license. Set up the makefile so it is built.
--- grub-core/Makefile.core.def | 15 +++++++++++++++ grub-core/lib/libtasn1_wrap/wrap.c | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 grub-core/lib/libtasn1_wrap/wrap.c
grub-core/Makefile.core.def +15 -0
@@ -2561,3 +2561,18 @@ module = { common = commands/memtools.c; condition = COND_MM_DEBUG; }; + +module = { + name = asn1; + common = lib/libtasn1/lib/decoding.c; + common = lib/libtasn1/lib/coding.c; + common = lib/libtasn1/lib/element.c; + common = lib/libtasn1/lib/structure.c; + common = lib/libtasn1/lib/parser_aux.c; + common = lib/libtasn1/lib/gstr.c; + common = lib/libtasn1/lib/errors.c; + common = lib/libtasn1_wrap/wrap.c; + cflags = '$(CFLAGS_POSIX) $(CFLAGS_GNULIB)'; + // -Wno-type-limits comes from libtasn1's configure.ac + cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1/lib -Wno-type-limits'; +};
grub-core/lib/libtasn1_wrap/wrap.c new file +26 -0
@@ -0,0 +1,26 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2020 IBM 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/dl.h> + +/* +* libtasn1 is provided under LGPL2.1+, which is compatible +* with GPL3+. As Grub as a whole is under GPL3+, this module +* is therefore under GPL3+ also. +*/ +GRUB_MOD_LICENSE ("GPLv3+" );
Signature · 1 line
2.35.3
PATCH v2 6/11 test_asn1: test module for libtasn1
In reply to Gary LinFrom: Daniel Axtens <[email protected]>
Import tests from libtasn1 that don't use functionality we don't import. I have put them here rather than in the libtasn1 directory because:
- They need much more significant changes to run in the grub context.
- I don't expect they will need to be changed when updating
libtasn1: I expect the old tests will usually continue to pass on new versions.
This doesn't test the full decoder but that will be exercised in test suites for coming patch sets.
Add testcase target in accordance with5e10be48e5 tests: Add check-native and check-nonnative make targets
--- Makefile.util.def | 6 + grub-core/Makefile.core.def | 13 ++ .../tests/CVE-2018-1000654-1_asn1_tab.h | 32 +++ .../tests/CVE-2018-1000654-2_asn1_tab.h | 36 +++ .../libtasn1_wrap/tests/CVE-2018-1000654.c | 61 +++++ .../lib/libtasn1_wrap/tests/Test_overflow.c | 138 ++++++++++++ .../lib/libtasn1_wrap/tests/Test_simple.c | 207 +++++++++++++++++ .../lib/libtasn1_wrap/tests/Test_strings.c | 150 +++++++++++++ .../libtasn1_wrap/tests/object-id-decoding.c | 116 ++++++++++ .../libtasn1_wrap/tests/object-id-encoding.c | 120 ++++++++++ .../lib/libtasn1_wrap/tests/octet-string.c | 211 ++++++++++++++++++ .../lib/libtasn1_wrap/tests/reproducers.c | 81 +++++++ grub-core/lib/libtasn1_wrap/wrap_tests.c | 75 +++++++ grub-core/lib/libtasn1_wrap/wrap_tests.h | 38 ++++ tests/test_asn1.in | 12 + 15 files changed, 1296 insertions(+) create mode 100644 grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-1_asn1_tab.h create mode 100644 grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-2_asn1_tab.h create mode 100644 grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/Test_overflow.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/Test_simple.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/Test_strings.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/object-id-decoding.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/object-id-encoding.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/octet-string.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/reproducers.c create mode 100644 grub-core/lib/libtasn1_wrap/wrap_tests.c create mode 100644 grub-core/lib/libtasn1_wrap/wrap_tests.h create mode 100644 tests/test_asn1.in
Makefile.util.def +6 -0
@@ -1237,6 +1237,12 @@ script = { common = tests/luks2_test.in; }; +script = { + testcase = native; + name = test_asn1; + common = tests/test_asn1.in; +}; + program = { testcase = native; name = example_unit_test;
grub-core/Makefile.core.def +13 -0
@@ -2576,3 +2576,16 @@ module = { // -Wno-type-limits comes from libtasn1's configure.ac cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1/lib -Wno-type-limits'; }; + +module = { + name = test_asn1; + common = lib/libtasn1_wrap/tests/CVE-2018-1000654.c; + common = lib/libtasn1_wrap/tests/object-id-decoding.c; + common = lib/libtasn1_wrap/tests/object-id-encoding.c; + common = lib/libtasn1_wrap/tests/octet-string.c; + common = lib/libtasn1_wrap/tests/reproducers.c; + common = lib/libtasn1_wrap/tests/Test_overflow.c; + common = lib/libtasn1_wrap/tests/Test_simple.c; + common = lib/libtasn1_wrap/tests/Test_strings.c; + common = lib/libtasn1_wrap/wrap_tests.c; +};
grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-1_asn1_tab.h new file +32 -0
@@ -0,0 +1,32 @@ +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include <grub/libtasn1.h> + +const asn1_static_node CVE_2018_1000654_1_asn1_tab[] = { + {"TEST_TREE" ,536875024 ,NULL }, + {NULL ,1610612748 ,NULL }, + {"iso" ,1073741825 ,"1" }, + {"identified-organization" ,1073741825 ,"3" }, + {"dod" ,1073741825 ,"6" }, + {"internet" ,1073741825 ,"1" }, + {"security" ,1073741825 ,"5" }, + {"mechanisms" ,1073741825 ,"5" }, + {"pkix" ,1073741825 ,"7" }, + {"id-mod" ,1073741825 ,"0" }, + {"id-pkix1-implicit-88" ,1 ,"2" }, + {"id-xnyTest" ,1879048204 ,NULL }, + {NULL ,1073741825 ,"id-ix" }, + {NULL ,1073741825 ,"29" }, + {NULL ,1 ,"1" }, + {"id-ix" ,1880096780 ,"OBJECR" }, + {NULL ,1073741825 ,"id-ix" }, + {NULL ,1073741825 ,"29" }, + {NULL ,1 ,"2" }, + {"id-xnyTest" ,805306380 ,NULL }, + {NULL ,1073741825 ,"id-ix" }, + {NULL ,1073741825 ,"29" }, + {NULL ,1 ,"1" }, + {NULL ,0 ,NULL } +};
grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-2_asn1_tab.h new file +36 -0
@@ -0,0 +1,36 @@ +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include <grub/libtasn1.h> + +const asn1_static_node CVE_2018_1000654_2_asn1_tab[] = { + {"TEST_TREE" ,536875024 ,NULL }, + {NULL ,1610612748 ,NULL }, + {"iso" ,1073741825 ,"1" }, + {"identified-organization" ,1073741825 ,"3" }, + {"dod" ,1073741825 ,"6" }, + {"internet" ,1073741825 ,"1" }, + {"security" ,1073741825 ,"5" }, + {"mechanisms" ,1073741825 ,"5" }, + {"pkix" ,1073741825 ,"7" }, + {"id-mod" ,1073741825 ,"0" }, + {"id-pkix1-implicit-88" ,1 ,"2" }, + {"id-oneTest" ,1879048204 ,NULL }, + {NULL ,1073741825 ,"id-two" }, + {NULL ,1073741825 ,"9" }, + {NULL ,1 ,"1" }, + {"id-two" ,1879048204 ,NULL }, + {NULL ,1073741825 ,"id-three" }, + {NULL ,1073741825 ,"2" }, + {NULL ,1 ,"2" }, + {"id-three" ,1879048204 ,NULL }, + {NULL ,1073741825 ,"id-four" }, + {NULL ,1073741825 ,"3" }, + {NULL ,1 ,"3" }, + {"id-four" ,805306380 ,NULL }, + {NULL ,1073741825 ,"id-two" }, + {NULL ,1073741825 ,"3" }, + {NULL ,1 ,"3" }, + {NULL ,0 ,NULL } +};
grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.c new file +61 -0
61 lines not on this page · raw message
grub-core/lib/libtasn1_wrap/tests/Test_overflow.c new file +138 -0
138 lines not on this page · raw message
grub-core/lib/libtasn1_wrap/tests/Test_simple.c new file +207 -0
207 lines not on this page · raw message
grub-core/lib/libtasn1_wrap/tests/Test_strings.c new file +150 -0
150 lines not on this page · raw message
grub-core/lib/libtasn1_wrap/tests/object-id-decoding.c new file +116 -0
116 lines not on this page · raw message
grub-core/lib/libtasn1_wrap/tests/object-id-encoding.c new file +120 -0
120 lines not on this page · raw message
grub-core/lib/libtasn1_wrap/tests/octet-string.c new file +211 -0
211 lines not on this page · raw message
grub-core/lib/libtasn1_wrap/tests/reproducers.c new file +81 -0
81 lines not on this page · raw message
grub-core/lib/libtasn1_wrap/wrap_tests.c new file +75 -0
75 lines not on this page · raw message
grub-core/lib/libtasn1_wrap/wrap_tests.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2020 IBM 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 LIBTASN1_WRAP_TESTS_H +#define LIBTASN1_WRAP_TESTS_H + +void test_CVE_2018_1000654 (void ); + +void test_object_id_encoding (void ); + +void test_object_id_decoding (void ); + +void test_octet_string (void ); + +void test_overflow (void ); + +void test_reproducers (void ); + +void test_simple (void ); + +void test_strings (void ); + +#endif
tests/test_asn1.in new file +12 -0
@@ -0,0 +1,12 @@ +#! @BUILD_SHEBANG@ +set -e + +. "@builddir@/grub-core/modinfo.sh" + +out=`echo test_asn1 | @builddir@/grub-shell` + +if [ "$(echo "$out" | tail -n 1)" != "ASN.1 self-tests passed" ]; then + echo "ASN.1 test failure: $out" + exit 1 +fi +
Signature · 1 line
2.35.3
PATCH v2 7/11 protectors: Add key protectors framework
In reply to Gary LinFrom: 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
@@ -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
@@ -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
PATCH v2 8/11 tpm2: Add TPM Software Stack (TSS)
In reply to Gary LinFrom: 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, second, 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 | 761 +++++++++++++++++++++++ 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, 3449 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
@@ -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_cpu_to_be16 (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_cpu_to_be32 (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_be_to_cpu16 (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_be_to_cpu32 (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 +761 -0
761 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
287 lines not on this page · raw message
include/grub/tpm2/mu.h new file +292 -0
292 lines not on this page · 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.
PATCH v2 9/11 protectors: Add TPM2 Key Protector
In reply to Gary LinFrom: 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. There are two supported key formats:
1. Raw Sealed Key (--keyfile)
The raw sealed key glues TPM2B_PUBLIC and TPM2B_PRIVATE, which are returned from TPM2_Create, into one file, and is defined as a C struct:
typedef struct TPM2_SEALED_KEY {
TPM2B_PUBLIC public;
TPM2B_PRIVATE private;
} TPM2_SEALED_KEY;2. TPM 2.0 Key (--tpm2key)
The following is the ASN.1 definition of TPM 2.0 Key File:
TPMKey ::= SEQUENCE {
type OBJECT IDENTIFIER
emptyAuth [0] EXPLICIT BOOLEAN OPTIONAL
policy [1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL
secret [2] EXPLICIT OCTET STRING OPTIONAL
authPolicy [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL
parent INTEGER
pubkey OCTET STRING
privkey OCTET STRING}
The TPM2 key protector only expects a "sealed" key in DER encoding, so 'type' is always 2.23.133.10.1.5, 'emptyAuth' is 'TRUE', and 'secret' is empty. TPM2B_PUBLIC is stored in 'pubkey' and TPM2B_PRIVATE is stored in 'privkey'. 'policy' and 'authPolicy' are ignored for the time being and will be used for the advanced features in the future.
For more details: https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
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, to unseal the raw sealed key file:
tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub2/sealed-1.key cryptomount DISK1 -P tpm2
tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub2/sealed-2.key --pcrs=7,11 cryptomount DISK2 -P tpm2
Or, to unseal the TPM 2.0 Key file:
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub2/sealed-1.tpm cryptomount DISK1 -P tpm2
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub2/sealed-2.tpm --pcrs=7,11 cryptomount DISK2 -P 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 | 13 + grub-core/tpm2/args.c | 129 +++++ grub-core/tpm2/module.c | 833 ++++++++++++++++++++++++++++++ grub-core/tpm2/tpm2key.asn | 31 ++ grub-core/tpm2/tpm2key.c | 218 ++++++++ grub-core/tpm2/tpm2key_asn1_tab.c | 34 ++ include/grub/tpm2/internal/args.h | 39 ++ include/grub/tpm2/tpm2key.h | 40 ++ 8 files changed, 1337 insertions(+) create mode 100644 grub-core/tpm2/args.c create mode 100644 grub-core/tpm2/module.c create mode 100644 grub-core/tpm2/tpm2key.asn create mode 100644 grub-core/tpm2/tpm2key.c create mode 100644 grub-core/tpm2/tpm2key_asn1_tab.c create mode 100644 include/grub/tpm2/internal/args.h create mode 100644 include/grub/tpm2/tpm2key.h
grub-core/Makefile.core.def +13 -0
@@ -2525,6 +2525,19 @@ 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; + common = tpm2/tpm2key.c; + common = tpm2/tpm2key_asn1_tab.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 +833 -0
833 lines not on this page · raw message
grub-core/tpm2/tpm2key.asn new file +31 -0
@@ -0,0 +1,31 @@ +-- +-- TPM 2.0 key file format +-- To generate tpm2key_asn1_tab.c: asn1Parser tpm2key.asn +-- +TPM2KEY {} +DEFINITIONS IMPLICIT TAGS ::= + +BEGIN + +TPMPolicy ::= SEQUENCE { + CommandCode [0] EXPLICIT INTEGER, + CommandPolicy [1] EXPLICIT OCTET STRING +} + +TPMAuthPolicy ::= SEQUENCE { + Name [0] EXPLICIT UTF8String OPTIONAL, + Policy [1] EXPLICIT SEQUENCE OF TPMPolicy +} + +TPMKey ::= SEQUENCE { + type OBJECT IDENTIFIER, + emptyAuth [0] EXPLICIT BOOLEAN OPTIONAL, + policy [1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL, + secret [2] EXPLICIT OCTET STRING OPTIONAL, + authPolicy [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL, + parent INTEGER, + pubkey OCTET STRING, + privkey OCTET STRING +} + +END
grub-core/tpm2/tpm2key.c new file +218 -0
218 lines not on this page · raw message
grub-core/tpm2/tpm2key_asn1_tab.c new file +34 -0
@@ -0,0 +1,34 @@ +#include <grub/mm.h> +#include <grub/libtasn1.h> + +const asn1_static_node tpm2key_asn1_tab[] = { + {"TPM2KEY" ,536875024 ,NULL }, + {NULL ,1073741836 ,NULL }, + {"TPMPolicy" ,1610612741 ,NULL }, + {"CommandCode" ,1610620931 ,NULL }, + {NULL ,2056 ,"0" }, + {"CommandPolicy" ,536879111 ,NULL }, + {NULL ,2056 ,"1" }, + {"TPMAuthPolicy" ,1610612741 ,NULL }, + {"Name" ,1610637346 ,NULL }, + {NULL ,2056 ,"0" }, + {"Policy" ,536879115 ,NULL }, + {NULL ,1073743880 ,"1" }, + {NULL ,2 ,"TPMPolicy" }, + {"TPMKey" ,536870917 ,NULL }, + {"type" ,1073741836 ,NULL }, + {"emptyAuth" ,1610637316 ,NULL }, + {NULL ,2056 ,"0" }, + {"policy" ,1610637323 ,NULL }, + {NULL ,1073743880 ,"1" }, + {NULL ,2 ,"TPMPolicy" }, + {"secret" ,1610637319 ,NULL }, + {NULL ,2056 ,"2" }, + {"authPolicy" ,1610637323 ,NULL }, + {NULL ,1073743880 ,"3" }, + {NULL ,2 ,"TPMAuthPolicy" }, + {"parent" ,1073741827 ,NULL }, + {"pubkey" ,1073741831 ,NULL }, + {"privkey" ,7 ,NULL }, + {NULL ,0 ,NULL } +};
include/grub/tpm2/internal/args.h new file +39 -0
@@ -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 */
include/grub/tpm2/tpm2key.h new file +40 -0
@@ -0,0 +1,40 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2023 SUSE LLC +* +* 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_TPM2KEY_HEADER +#define GRUB_TPM2_TPM2KEY_HEADER 1 + +#include <grub/types.h> +#include <grub/libtasn1.h> + +grub_err_t +grub_tpm2key_start_parsing (asn1_node *parsed_tpm2key,void *data,grub_size_t size); + +void +grub_tpm2key_end_parsing (asn1_node tpm2key); + +grub_err_t +grub_tpm2key_get_parent (asn1_node tpm2key,grub_uint32_t *parent); + +grub_err_t +grub_tpm2key_get_pubkey (asn1_node tpm2key,void **data,grub_size_t *size); + +grub_err_t +grub_tpm2key_get_privkey (asn1_node tpm2key,void **data,grub_size_t *size); + +#endif /* GRUB_TPM2_TPM2KEY_HEADER */
Signature · 1 line
2.35.3
PATCH v2 10/11 cryptodisk: Support key protectors
In reply to Gary LinFrom: Hernan Gatta <[email protected]>
Add a new parameter to cryptomount to support the key protectors framework: -P. 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.
--- Makefile.util.def | 1 + grub-core/disk/cryptodisk.c | 175 +++++++++++++++++++++++++++++------- include/grub/cryptodisk.h | 14 +++ 3 files changed, 158 insertions(+), 32 deletions(-)
Makefile.util.def +1 -0
@@ -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 +143 -32
270 lines not on this page · raw message
include/grub/cryptodisk.h +14 -0
@@ -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
Re: [PATCH v2 10/11] cryptodisk: Support key protectors
Quoted 10 lines · Gary Lin
From: Hernan Gatta <[email protected]>
Add a new parameter to cryptomount to support the key protectors framework: -P. 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]>Signed-off-by Michael Chang <[email protected]>
I'm wondering, why Michael's SOB is needed here? Did I miss somewhere on the list where he contributed to this patch?
Quoted 61 lines
Signed-off-by Gary Lin <[email protected]>--- Makefile.util.def | 1 + grub-core/disk/cryptodisk.c | 175 +++++++++++++++++++++++++++++------- include/grub/cryptodisk.h | 14 +++ 3 files changed, 158 insertions(+), 32 deletions(-)Makefile.util.def +1
-0@@ -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 +7
-2@@ -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 @@ enumOPTION_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", 'P', 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 ;
grub-core/disk/cryptodisk.c grub_cryptodisk_scan_device_real (const char *name, · line 1121
Quoted 4 lines
grub-core/disk/cryptodisk.c +1
-0@@ -1113,41 +1118,113 @@ grub_cryptodisk_scan_device_real (const char *name, goto error_no_close;if (!dev)continue ; + crd = cr;
Why can't we just use cr below and not introduce crd?
Quoted 19 lines
grub-core/disk/cryptodisk.c +2
-16+break ; + } -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-core/disk/cryptodisk.c grub_cryptodisk_scan_device_real (const char *name, · line 1128
Quoted 10 lines
grub-core/disk/cryptodisk.c +4
-6- { -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" );
Every 8 spaces should be converted to a tab character. Same for all instances below too (and probably in other patches, though not in the ones importing foreign libs, like libtasn).
Quoted 9 lines
grub-core/disk/cryptodisk.c +5
-3+ 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++)
grub-core/disk/cryptodisk.c grub_cryptodisk_scan_device_real (const char *name, · line 1144
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+ { +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)
Daniel will say this should be "ret != GRUB_ERR_NONE", and same for similar uses elsewhere.
Quoted 91 lines
grub-core/disk/cryptodisk.c +79
-5+ { +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:
grub-core/disk/cryptodisk.c grub_cryptodisk_scan_device (const char *name, · line 1343
Quoted 9 lines
grub-core/disk/cryptodisk.c +6
-0@@ -1258,6 +1335,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)
Daniel also prefers "cargs->key_cache == NULL" for NULL checks on pointers.
grub-core/disk/cryptodisk.c grub_cryptodisk_scan_device (const char *name, · line 1346
Quoted 3 lines
grub-core/disk/cryptodisk.c +3
-0+return ; + +for (i =0 ; cargs->protectors[i]; i++)
Maybe good to be defensive and check that cargs->protectors is not NULL.
Quoted 98 lines
grub-core/disk/cryptodisk.c +19
-0+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 +1361,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 +1457,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 +1474,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);
returnGRUB_ERR_NONE;grub-core/disk/cryptodisk.c +6
-0@@ -1378,6 +1483,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 +1503,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 +1527,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 +1538,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,6 +1686,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]" +" [-P protector [-P protector ...]]" " <SOURCE|-u UUID|-a|-b>" ),N_ ("Mount a crypto device." ), options);grub_procfs_register ("luks_script" , &luks_script);include/grub/cryptodisk.h +0
-0
include/grub/cryptodisk.h typedef gcry_err_code_t · line 79
Quoted 10 lines
include/grub/cryptodisk.h +7
-0@@ -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. */
Not a properly formatted multiline comment[1].
Overall this looks okay, even if I'm still not in love with the key protectors user interface (but like the idea).
Glenn
[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Multi_002dLine-Comments
Quoted 17 lines
include/grub/cryptodisk.h +7
-0+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 ;
Re: [PATCH v2 10/11] cryptodisk: Support key protectors
Quoted 9 lines · Glenn Washburn
Quoted 8 lines · Gary Lin
From: Hernan Gatta <[email protected]>
Add a new parameter to cryptomount to support the key protectors framework: -P. 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.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Michael Chang <[email protected]>I'm wondering, why Michael's SOB is needed here? Did I miss somewhere on the list where he contributed to this patch?
Michael did the most hard work of rebasing the patch, so I'd like to give him the credit.
Quoted 56 lines
Quoted 56 lines
Signed-off-by Gary Lin <[email protected]>--- Makefile.util.def | 1 + grub-core/disk/cryptodisk.c | 175 +++++++++++++++++++++++++++++------- include/grub/cryptodisk.h | 14 +++ 3 files changed, 158 insertions(+), 32 deletions(-)Makefile.util.def +1
-0@@ -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 +7
-2@@ -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 @@ enumOPTION_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", 'P', 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 ;
Quoted 9 lines
grub-core/disk/cryptodisk.c grub_cryptodisk_scan_device_real (const char *name, · line 1121
Quoted 6 lines
grub-core/disk/cryptodisk.c +1
-0char *part =NULL ;@@ -1113,41 +1118,113 @@ grub_cryptodisk_scan_device_real (const char *name, goto error_no_close;if (!dev)continue ; + crd = cr;Why can't we just use cr below and not introduce crd?
Good question. 'crd' seems not necessary. Will check the code again.
Quoted 24 lines
Quoted 24 lines
grub-core/disk/cryptodisk.c +2
-22+break ; + } -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); - }
Quoted 9 lines
grub-core/disk/cryptodisk.c grub_cryptodisk_scan_device_real (const char *name, · line 1128
Quoted 4 lines
grub-core/disk/cryptodisk.c +4
-0+if (!dev) + { +grub_error (GRUB_ERR_BAD_MODULE , +"no cryptodisk module can handle this device" );Every 8 spaces should be converted to a tab character. Same for all instances below too (and probably in other patches, though not in the ones importing foreign libs, like libtasn).
Will fix in v3.
Quoted 19 lines
Quoted 8 lines
grub-core/disk/cryptodisk.c +5
-3+ 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++)grub-core/disk/cryptodisk.c grub_cryptodisk_scan_device_real (const char *name, · line 1144
Quoted 10 lines
grub-core/disk/cryptodisk.c +10
-0+ { +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)
Quoted 3 lines
Daniel will say this should be "ret !=
GRUB_ERR_NONE", and same for similar uses elsewhere.
Ditto.
Quoted 90 lines
Quoted 90 lines
grub-core/disk/cryptodisk.c +79
-5+ { +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 +1335,20 @@ grub_cryptodisk_scan_device (const char *name,return ret; }
Quoted 9 lines
grub-core/disk/cryptodisk.c grub_cryptodisk_scan_device (const char *name, · line 1343
Quoted 6 lines
grub-core/disk/cryptodisk.c +6
-0+static void +grub_cryptodisk_clear_key_cache (struct grub_cryptomount_args *cargs) +{ +int i; + +if (!cargs->key_cache)Daniel also prefers "
cargs->key_cache== NULL" for NULL checks on pointers.
Ditto.
Quoted 6 lines
grub-core/disk/cryptodisk.c grub_cryptodisk_scan_device (const char *name, · line 1346
Quoted 3 lines
grub-core/disk/cryptodisk.c +3
-0+return ; + +for (i =0 ; cargs->protectors[i]; i++)Maybe good to be defensive and check that
cargs->protectorsis not NULL.
Agree. Will fix in v3.
Quoted 96 lines
Quoted 96 lines
grub-core/disk/cryptodisk.c +19
-0+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 +1361,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 +1457,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 +1474,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);
returnGRUB_ERR_NONE;grub-core/disk/cryptodisk.c +6
-0@@ -1378,6 +1483,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 +1503,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 +1527,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 +1538,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,6 +1686,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]" +" [-P protector [-P protector ...]]" " <SOURCE|-u UUID|-a|-b>" ),N_ ("Mount a crypto device." ), options);grub_procfs_register ("luks_script" , &luks_script);include/grub/cryptodisk.h +1
-0@@ -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
Quoted 9 lines
include/grub/cryptodisk.h typedef gcry_err_code_t · line 79
Quoted 6 lines
include/grub/cryptodisk.h +6
-0+{ +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. */ Not a properly formatted multiline comment[1].
Will fix in v3.
Quoted 3 lines
Overall this looks okay, even if I'm still not in love with the key protectors user interface (but like the idea).
Thanks for reviewing the patch!
Gary Lin
Quoted 21 lines
Glenn
[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Multi_002dLine-Comments
Quoted 16 lines
include/grub/cryptodisk.h +7
-0+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 ;
PATCH v2 11/11 util/grub-protect: Add new tool
In reply to Gary LinFrom: 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 LUKS 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 (-P) parameter.
Sample usage:
$ dd if=/dev/urandom of=luks-key bs=1 count=32 $ sudo cryptsetup luksAddKey /dev/sdb1 luks-key --pbkdf=pbkdf2 --hash=sha512
To seal the key with TPM 2.0 Key File (recommended):
$ sudo grub-protect --action=add \
--protector=tpm2 \
--tpm2key \
--tpm2-keyfile=luks-key \
--tpm2-outfile=/boot/efi/boot/grub2/sealed.tpmOr, to seal the key with the raw sealed key:
$ sudo grub-protect --action=add \
--protector=tpm2 \
--tpm2-keyfile=luks-key \
--tpm2-outfile=/boot/efi/boot/grub2/sealed.keyThen, in the boot script, for TPM 2.0 Key File:
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -u b20f95d0834842bc9197bd78b36732f8 -P tpm2
Or, for the raw sealed key:
tpm2_key_protector_init --keyfile=(hd0,gpt1)/boot/grub2/sealed.key cryptomount -u b20f95d0834842bc9197bd78b36732f8 -P tpm2
where the UUID corresponds to /dev/sdb1.
--- .gitignore | 2 + Makefile.util.def | 22 + configure.ac | 9 + util/grub-protect.c | 1472 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1505 insertions(+) create mode 100644 util/grub-protect.c
.gitignore +2 -0
@@ -167,6 +167,8 @@ widthspec.bin /grub-ofpathname.exe /grub-probe /grub-probe.exe +/grub-protect +/grub-protect.exe /grub-reboot /grub-render-label /grub-render-label.exe
Makefile.util.def +22 -0
@@ -206,6 +206,28 @@ 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 = grub-core/tpm2/tpm2key_asn1_tab.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 = '$(LIBTASN1)'; + ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; + + enable = efi; +}; + program = { name = grub-mkrelpath; mansection = 1;
configure.ac +9 -0
@@ -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])
@@ -1922,6 +1923,14 @@ fi AC_SUBST([LIBZFS]) AC_SUBST([LIBNVPAIR]) +LIBTASN1= +if test x"$platform" = xefi; then + AC_CHECK_LIB([tasn1], [asn1_write_value], [], + [AC_MSG_ERROR([Your platform requires libtasn1])]) + LIBTASN1="-ltasn1" +fi +AC_SUBST([LIBTASN1]) + LIBS="" AC_SUBST([FONT_SOURCE])
util/grub-protect.c new file +1472 -0
1,472 lines not on this page · raw message
Signature · 1 line
2.35.3
Hi Gary,
Usually a v2 series will also include what changed from v1 -> v2 and subsequent versions will keep the changelog. Also, for such a large series, using --range-diff with git format-patch can be helpful for reviewers (it will include here the exact changes to each patch from the last version of the series).
Quoted 9 lines · Gary Lin
This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption.
To support TPM 2.0 Key File
format(*2), patch 1~6 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.
How was the upgrade process? Were there any gotchas? Regardless when including new external libraries the update process should be included in docs/grub-dev.texi under the section "Updating External Code".
Similarly, I am not in favor of including this new user functionality without properly documenting it in docs/grub.texi.
Also, and I know this is a longshot, but it would be great to add tests for the protector and TPM code. Probably the easiest option would be to create non-native (ie QEMU) make check tests. QEMU can use an emulated software TPM[1].
Quoted 3 lines
Patch 7~11 are Hernan Gatta's patches with the follow-up fixes:
- Converting 8 spaces into 1 tab
This is good. However, I noticed that there are places in the patch "cryptodisk: Support key protectors" where this did not happen. I've not checked other patches. I'll note a specific instance in a reply to that patch.
Quoted 39 lines
- Merging the minor build fix from Michael Chang
- Replacing "lu" with "PRIuGRUB_SIZE" forgrub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master
- Removing the measurement on the sealed key
- Based ont the patch from Olaf Kirch <[email protected]>
- Adjusting the input parameters ofTPM2_EvictControlto match the order in "TCG TPM2 Part3 Commands"
- Declaring the input arguments of TPM2 functions as const
- Resending TPM2 commands onTPM_RC_RETRY
- Adding checks for the parameters of TPM2 commands
- Packing the missing authorization command forTPM2_PCR_Read
- Tweaking the TPM2 command functions to allow some parameters to be NULL
- Only enabling grub-protect for "efi" since the TPM2 stack currently relies on the EFI TCG2 protocol to send TPM2 commands
- Using grub_cpu_to_be*() in the TPM2 stack instead grub_swap_bytes*() for the marshal functions
- Changing the short name of "--protector" of "cryptomount" from "-k" to "-P" to avoid the conflict with "--key-file"
- Adding the primitive support for TPM 2.0 Key File Format besides the raw sealed key
- Adding the external libtasn1 dependency for grub-protect since "asn1_write_value()" is disabled in the embedded libtasn1To utilize the TPM2 key protector to unlock the encrypted partition (sdb1), here are the sample steps:
1. Add an extra random key for LUKS (luks-key)
$ dd if=/dev/urandom of=luks-key bs=1 count=32 $ sudo cryptsetup luksAddKey /dev/sdb1 luks-key --pbkdf=pbkdf22. Seal the key
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm
Quoted 3 lines
3. Unseal the key with the proper commands in grub.cfg:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -uSDB1_UUID-P tpm2
This is great, but as I'm sure you know this won't appear in the commit log and as far as I can tell is not documented anywhere else. This would be nice to add to the user documentation.
Glenn
[1] https://qemu.readthedocs.io/en/latest/specs/tpm.html#the-qemu-tpm-emulator-device
Quoted 132 lines
(*1) https://lists.gnu.org/archive/html/grub-devel/2022-02/msg00006.html
(*2) https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
(*3) https://lists.gnu.org/archive/html/grub-devel/2021-06/msg00044.htmlDaniel Axtens (6):
posix_wrap: tweaks in preparation for libtasn1
libtasn1: import libtasn1-4.19.0
libtasn1: disable code not needed in grub
libtasn1: changes for grub compatibility
libtasn1: compile into asn1 module
test_asn1: test module for libtasn1Hernan 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.gitignore | 2 + Makefile.util.def | 29 + configure.ac | 9 + grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 42 + grub-core/disk/cryptodisk.c | 175 +- grub-core/kern/protectors.c | 75 + grub-core/lib/libtasn1/COPYING | 16 + grub-core/lib/libtasn1/README.md | 98 + grub-core/lib/libtasn1/lib/coding.c | 1433 ++++++++++ grub-core/lib/libtasn1/lib/decoding.c | 2504 +++++++++++++++++ grub-core/lib/libtasn1/lib/element.c | 1110 ++++++++ grub-core/lib/libtasn1/lib/element.h | 42 + grub-core/lib/libtasn1/lib/errors.c | 103 + grub-core/lib/libtasn1/lib/gstr.c | 74 + grub-core/lib/libtasn1/lib/gstr.h | 50 + grub-core/lib/libtasn1/lib/int.h | 221 ++ grub-core/lib/libtasn1/lib/parser_aux.c | 1179 ++++++++ grub-core/lib/libtasn1/lib/parser_aux.h | 172 ++ grub-core/lib/libtasn1/lib/structure.c | 1227 ++++++++ grub-core/lib/libtasn1/lib/structure.h | 46 + .../tests/CVE-2018-1000654-1_asn1_tab.h | 32 + .../tests/CVE-2018-1000654-2_asn1_tab.h | 36 + .../libtasn1_wrap/tests/CVE-2018-1000654.c | 61 + .../lib/libtasn1_wrap/tests/Test_overflow.c | 138 + .../lib/libtasn1_wrap/tests/Test_simple.c | 207 ++ .../lib/libtasn1_wrap/tests/Test_strings.c | 150 + .../libtasn1_wrap/tests/object-id-decoding.c | 116 + .../libtasn1_wrap/tests/object-id-encoding.c | 120 + .../lib/libtasn1_wrap/tests/octet-string.c | 211 ++ .../lib/libtasn1_wrap/tests/reproducers.c | 81 + grub-core/lib/libtasn1_wrap/wrap.c | 26 + grub-core/lib/libtasn1_wrap/wrap_tests.c | 75 + grub-core/lib/libtasn1_wrap/wrap_tests.h | 38 + grub-core/lib/posix_wrap/limits.h | 1 + grub-core/lib/posix_wrap/stdlib.h | 8 + grub-core/lib/posix_wrap/sys/types.h | 1 + grub-core/tpm2/args.c | 129 + grub-core/tpm2/buffer.c | 145 + grub-core/tpm2/module.c | 833 ++++++ grub-core/tpm2/mu.c | 807 ++++++ grub-core/tpm2/tcg2.c | 143 + grub-core/tpm2/tpm2.c | 761 +++++ grub-core/tpm2/tpm2key.asn | 31 + grub-core/tpm2/tpm2key.c | 218 ++ grub-core/tpm2/tpm2key_asn1_tab.c | 34 + include/grub/cryptodisk.h | 14 + include/grub/libtasn1.h | 645 +++++ 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 + include/grub/tpm2/tpm2key.h | 40 + tests/test_asn1.in | 12 + util/grub-protect.c | 1472 ++++++++++ 60 files changed, 16841 insertions(+), 32 deletions(-)create mode 100644
grub-core/kern/protectors.c
create mode 100644 grub-core/lib/libtasn1/COPYING
create mode 100644grub-core/lib/libtasn1/README.md
create mode 100644grub-core/lib/libtasn1/lib/coding.c
create mode 100644grub-core/lib/libtasn1/lib/decoding.c
create mode 100644grub-core/lib/libtasn1/lib/element.c
create mode 100644grub-core/lib/libtasn1/lib/element.h
create mode 100644grub-core/lib/libtasn1/lib/errors.c
create mode 100644grub-core/lib/libtasn1/lib/gstr.c
create mode 100644grub-core/lib/libtasn1/lib/gstr.h
create mode 100644grub-core/lib/libtasn1/lib/int.h
create mode 100644grub-core/lib/libtasn1/lib/parser_aux.c
create mode 100644grub-core/lib/libtasn1/lib/parser_aux.h
create mode 100644grub-core/lib/libtasn1/lib/structure.c
create mode 100644grub-core/lib/libtasn1/lib/structure.h
create mode 100644grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-1_asn1_tab.hcreate mode 100644grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-2_asn1_tab.hcreate mode 100644grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/Test_overflow.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/Test_simple.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/Test_strings.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/object-id-decoding.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/object-id-encoding.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/octet-string.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/reproducers.ccreate mode 100644grub-core/lib/libtasn1_wrap/wrap.c
create mode 100644grub-core/lib/libtasn1_wrap/wrap_tests.c
create mode 100644grub-core/lib/libtasn1_wrap/wrap_tests.h
create mode 100644grub-core/tpm2/args.c
create mode 100644grub-core/tpm2/buffer.c
create mode 100644grub-core/tpm2/module.c
create mode 100644grub-core/tpm2/mu.c
create mode 100644grub-core/tpm2/tcg2.c
create mode 100644grub-core/tpm2/tpm2.c
create mode 100644 grub-core/tpm2/tpm2key.asn
create mode 100644grub-core/tpm2/tpm2key.c
create mode 100644grub-core/tpm2/tpm2key_asn1_tab.c
create mode 100644include/grub/libtasn1.h
create mode 100644include/grub/protector.h
create mode 100644include/grub/tpm2/buffer.h
create mode 100644include/grub/tpm2/internal/args.h
create mode 100644include/grub/tpm2/internal/functions.h
create mode 100644include/grub/tpm2/internal/structs.h
create mode 100644include/grub/tpm2/internal/types.h
create mode 100644include/grub/tpm2/mu.h
create mode 100644include/grub/tpm2/tcg2.h
create mode 100644include/grub/tpm2/tpm2.h
create mode 100644include/grub/tpm2/tpm2key.h
create mode 100644 tests/test_asn1.in
create mode 100644util/grub-protect.c
Quoted 2 lines · Glenn Washburn
Hi Gary,
Hi Glenn,
Quoted 6 lines
Usually a v2 series will also include what changed from v1 -> v2 and subsequent versions will keep the changelog. Also, for such a large series, using --range-diff with git format-patch can be helpful for reviewers (it will include here the exact changes to each patch from the last version of the series).
The difference between v1 and v2 is a bit huge. libtasn1 was added, several follow-up patches in v1 are merged into Hernan's patches. Not to mention the newly implemented TPM 2.0 Key. It made me feel that v2 is more like a fresh start.
Quoted 6 lines · Gary Lin
Quoted 5 lines
This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption.
Quoted 9 lines
Quoted 4 lines
To support TPM 2.0 Key File
format(*2), patch 1~6 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.How was the upgrade process? Were there any gotchas? Regardless when including new external libraries the update process should be included in docs/grub-dev.texi under the section "Updating External Code".
I followed the steps in the comment of Patch 2 to upgrade the lib. Will check how to write the steps into the document.
Quoted 8 lines
Similarly, I am not in favor of including this new user functionality without properly documenting it in docs/grub.texi.
Also, and I know this is a longshot, but it would be great to add tests for the protector and TPM code. Probably the easiest option would be to create non-native (ie QEMU) make check tests. QEMU can use an emulated software TPM[1].
Sure, I mainly tested the code with QEMU + swtpm.
Quoted 9 lines
Quoted 3 lines
Patch 7~11 are Hernan Gatta's patches with the follow-up fixes:
- Converting 8 spaces into 1 tabThis is good. However, I noticed that there are places in the patch "cryptodisk: Support key protectors" where this did not happen. I've not checked other patches. I'll note a specific instance in a reply to that patch.
I probably overlooked cryptodisk.c. Will check all affect files again.
Quoted 39 lines
Quoted 39 lines
- Merging the minor build fix from Michael Chang
- Replacing "lu" with "PRIuGRUB_SIZE" forgrub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master
- Removing the measurement on the sealed key
- Based ont the patch from Olaf Kirch <[email protected]>
- Adjusting the input parameters ofTPM2_EvictControlto match the order in "TCG TPM2 Part3 Commands"
- Declaring the input arguments of TPM2 functions as const
- Resending TPM2 commands onTPM_RC_RETRY
- Adding checks for the parameters of TPM2 commands
- Packing the missing authorization command forTPM2_PCR_Read
- Tweaking the TPM2 command functions to allow some parameters to be NULL
- Only enabling grub-protect for "efi" since the TPM2 stack currently relies on the EFI TCG2 protocol to send TPM2 commands
- Using grub_cpu_to_be*() in the TPM2 stack instead grub_swap_bytes*() for the marshal functions
- Changing the short name of "--protector" of "cryptomount" from "-k" to "-P" to avoid the conflict with "--key-file"
- Adding the primitive support for TPM 2.0 Key File Format besides the raw sealed key
- Adding the external libtasn1 dependency for grub-protect since "asn1_write_value()" is disabled in the embedded libtasn1To utilize the TPM2 key protector to unlock the encrypted partition (sdb1), here are the sample steps:
1. Add an extra random key for LUKS (luks-key)
$ dd if=/dev/urandom of=luks-key bs=1 count=32 $ sudo cryptsetup luksAddKey /dev/sdb1 luks-key --pbkdf=pbkdf22. Seal the key
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm
Quoted 8 lines
Quoted 3 lines
3. Unseal the key with the proper commands in grub.cfg:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -uSDB1_UUID-P tpm2This is great, but as I'm sure you know this won't appear in the commit log and as far as I can tell is not documented anywhere else. This would be nice to add to the user documentation.
The similar steps are also mentioned in the comment of Patch 11. Those steps give the overview of what these patches do, so I want them in the cover letter.
Since this patchset introduces several new things, before we reach the consensus on the implementation, arguments, etc., I'd like to defer the user documentation work.
Thanks,
Gary Lin
Quoted 137 lines
Glenn
[1] https://qemu.readthedocs.io/en/latest/specs/tpm.html#the-qemu-tpm-emulator-device
Quoted 132 lines
(*1) https://lists.gnu.org/archive/html/grub-devel/2022-02/msg00006.html
(*2) https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
(*3) https://lists.gnu.org/archive/html/grub-devel/2021-06/msg00044.htmlDaniel Axtens (6):
posix_wrap: tweaks in preparation for libtasn1
libtasn1: import libtasn1-4.19.0
libtasn1: disable code not needed in grub
libtasn1: changes for grub compatibility
libtasn1: compile into asn1 module
test_asn1: test module for libtasn1Hernan 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.gitignore | 2 + Makefile.util.def | 29 + configure.ac | 9 + grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 42 + grub-core/disk/cryptodisk.c | 175 +- grub-core/kern/protectors.c | 75 + grub-core/lib/libtasn1/COPYING | 16 + grub-core/lib/libtasn1/README.md | 98 + grub-core/lib/libtasn1/lib/coding.c | 1433 ++++++++++ grub-core/lib/libtasn1/lib/decoding.c | 2504 +++++++++++++++++ grub-core/lib/libtasn1/lib/element.c | 1110 ++++++++ grub-core/lib/libtasn1/lib/element.h | 42 + grub-core/lib/libtasn1/lib/errors.c | 103 + grub-core/lib/libtasn1/lib/gstr.c | 74 + grub-core/lib/libtasn1/lib/gstr.h | 50 + grub-core/lib/libtasn1/lib/int.h | 221 ++ grub-core/lib/libtasn1/lib/parser_aux.c | 1179 ++++++++ grub-core/lib/libtasn1/lib/parser_aux.h | 172 ++ grub-core/lib/libtasn1/lib/structure.c | 1227 ++++++++ grub-core/lib/libtasn1/lib/structure.h | 46 + .../tests/CVE-2018-1000654-1_asn1_tab.h | 32 + .../tests/CVE-2018-1000654-2_asn1_tab.h | 36 + .../libtasn1_wrap/tests/CVE-2018-1000654.c | 61 + .../lib/libtasn1_wrap/tests/Test_overflow.c | 138 + .../lib/libtasn1_wrap/tests/Test_simple.c | 207 ++ .../lib/libtasn1_wrap/tests/Test_strings.c | 150 + .../libtasn1_wrap/tests/object-id-decoding.c | 116 + .../libtasn1_wrap/tests/object-id-encoding.c | 120 + .../lib/libtasn1_wrap/tests/octet-string.c | 211 ++ .../lib/libtasn1_wrap/tests/reproducers.c | 81 + grub-core/lib/libtasn1_wrap/wrap.c | 26 + grub-core/lib/libtasn1_wrap/wrap_tests.c | 75 + grub-core/lib/libtasn1_wrap/wrap_tests.h | 38 + grub-core/lib/posix_wrap/limits.h | 1 + grub-core/lib/posix_wrap/stdlib.h | 8 + grub-core/lib/posix_wrap/sys/types.h | 1 + grub-core/tpm2/args.c | 129 + grub-core/tpm2/buffer.c | 145 + grub-core/tpm2/module.c | 833 ++++++ grub-core/tpm2/mu.c | 807 ++++++ grub-core/tpm2/tcg2.c | 143 + grub-core/tpm2/tpm2.c | 761 +++++ grub-core/tpm2/tpm2key.asn | 31 + grub-core/tpm2/tpm2key.c | 218 ++ grub-core/tpm2/tpm2key_asn1_tab.c | 34 + include/grub/cryptodisk.h | 14 + include/grub/libtasn1.h | 645 +++++ 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 + include/grub/tpm2/tpm2key.h | 40 + tests/test_asn1.in | 12 + util/grub-protect.c | 1472 ++++++++++ 60 files changed, 16841 insertions(+), 32 deletions(-)create mode 100644
grub-core/kern/protectors.c
create mode 100644 grub-core/lib/libtasn1/COPYING
create mode 100644grub-core/lib/libtasn1/README.md
create mode 100644grub-core/lib/libtasn1/lib/coding.c
create mode 100644grub-core/lib/libtasn1/lib/decoding.c
create mode 100644grub-core/lib/libtasn1/lib/element.c
create mode 100644grub-core/lib/libtasn1/lib/element.h
create mode 100644grub-core/lib/libtasn1/lib/errors.c
create mode 100644grub-core/lib/libtasn1/lib/gstr.c
create mode 100644grub-core/lib/libtasn1/lib/gstr.h
create mode 100644grub-core/lib/libtasn1/lib/int.h
create mode 100644grub-core/lib/libtasn1/lib/parser_aux.c
create mode 100644grub-core/lib/libtasn1/lib/parser_aux.h
create mode 100644grub-core/lib/libtasn1/lib/structure.c
create mode 100644grub-core/lib/libtasn1/lib/structure.h
create mode 100644grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-1_asn1_tab.hcreate mode 100644grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-2_asn1_tab.hcreate mode 100644grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/Test_overflow.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/Test_simple.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/Test_strings.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/object-id-decoding.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/object-id-encoding.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/octet-string.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/reproducers.ccreate mode 100644grub-core/lib/libtasn1_wrap/wrap.c
create mode 100644grub-core/lib/libtasn1_wrap/wrap_tests.c
create mode 100644grub-core/lib/libtasn1_wrap/wrap_tests.h
create mode 100644grub-core/tpm2/args.c
create mode 100644grub-core/tpm2/buffer.c
create mode 100644grub-core/tpm2/module.c
create mode 100644grub-core/tpm2/mu.c
create mode 100644grub-core/tpm2/tcg2.c
create mode 100644grub-core/tpm2/tpm2.c
create mode 100644 grub-core/tpm2/tpm2key.asn
create mode 100644grub-core/tpm2/tpm2key.c
create mode 100644grub-core/tpm2/tpm2key_asn1_tab.c
create mode 100644include/grub/libtasn1.h
create mode 100644include/grub/protector.h
create mode 100644include/grub/tpm2/buffer.h
create mode 100644include/grub/tpm2/internal/args.h
create mode 100644include/grub/tpm2/internal/functions.h
create mode 100644include/grub/tpm2/internal/structs.h
create mode 100644include/grub/tpm2/internal/types.h
create mode 100644include/grub/tpm2/mu.h
create mode 100644include/grub/tpm2/tcg2.h
create mode 100644include/grub/tpm2/tpm2.h
create mode 100644include/grub/tpm2/tpm2key.h
create mode 100644 tests/test_asn1.in
create mode 100644util/grub-protect.c
Quoted 37 lines · Gary Lin
Quoted 2 lines · Glenn Washburn
Hi Gary,
Hi Glenn,
Quoted 6 lines
Usually a v2 series will also include what changed from v1 -> v2 and subsequent versions will keep the changelog. Also, for such a large series, using --range-diff with git format-patch can be helpful for reviewers (it will include here the exact changes to each patch from the last version of the series).
The difference between v1 and v2 is a bit huge. libtasn1 was added, several follow-up patches in v1 are merged into Hernan's patches. Not to mention the newly implemented TPM 2.0 Key. It made me feel that v2 is more like a fresh start.
Quoted 6 lines · Gary Lin
Quoted 5 lines
This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption.
Quoted 9 lines
Quoted 4 lines
To support TPM 2.0 Key File
format(*2), patch 1~6 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.How was the upgrade process? Were there any gotchas? Regardless when including new external libraries the update process should be included in docs/grub-dev.texi under the section "Updating External Code".
I followed the steps in the comment of Patch 2 to upgrade the lib. Will check how to write the steps into the document.
Quoted 3 lines
Similarly, I am not in favor of including this new user functionality without properly documenting it in docs/grub.texi.
Quoted 6 lines
Quoted 5 lines
Also, and I know this is a longshot, but it would be great to add tests for the protector and TPM code. Probably the easiest option would be to create non-native (ie QEMU) make check tests. QEMU can use an emulated software TPM[1].
Sure, I mainly tested the code with QEMU + swtpm.
Great, so sounds like you have a good idea how things need to be setup, that's a large part of making the test.
Quoted 55 lines
Quoted 10 lines
Quoted 4 lines
Patch 7~11 are Hernan Gatta's patches with the follow-up fixes:
- Converting 8 spaces into 1 tabThis is good. However, I noticed that there are places in the patch "cryptodisk: Support key protectors" where this did not happen. I've not checked other patches. I'll note a specific instance in a reply to that patch.
I probably overlooked
cryptodisk.c. Will check all affect files again.Quoted 43 lines
Quoted 42 lines
- Merging the minor build fix from Michael Chang
- Replacing "lu" with "PRIuGRUB_SIZE" forgrub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master
- Removing the measurement on the sealed key
- Based ont the patch from Olaf Kirch <[email protected]>
- Adjusting the input parameters ofTPM2_EvictControlto match the order in "TCG TPM2 Part3 Commands"
- Declaring the input arguments of TPM2 functions as const
- Resending TPM2 commands onTPM_RC_RETRY
- Adding checks for the parameters of TPM2 commands
- Packing the missing authorization command forTPM2_PCR_Read
- Tweaking the TPM2 command functions to allow some parameters to be NULL
- Only enabling grub-protect for "efi" since the TPM2 stack currently relies on the EFI TCG2 protocol to send TPM2 commands
- Using grub_cpu_to_be*() in the TPM2 stack instead grub_swap_bytes*() for the marshal functions
- Changing the short name of "--protector" of "cryptomount" from "-k" to "-P" to avoid the conflict with "--key-file"
- Adding the primitive support for TPM 2.0 Key File Format besides the raw sealed key
- Adding the external libtasn1 dependency for grub-protect since "asn1_write_value()" is disabled in the embedded libtasn1To utilize the TPM2 key protector to unlock the encrypted partition (sdb1), here are the sample steps:
1. Add an extra random key for LUKS (luks-key)
$ dd if=/dev/urandom of=luks-key bs=1 count=32 $ sudo cryptsetup luksAddKey /dev/sdb1 luks-key --pbkdf=pbkdf22. Seal the key
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm3. Unseal the key with the proper commands in grub.cfg:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -uSDB1_UUID-P tpm2
Quoted 7 lines
Quoted 4 lines
This is great, but as I'm sure you know this won't appear in the commit log and as far as I can tell is not documented anywhere else. This would be nice to add to the user documentation.
The similar steps are also mentioned in the comment of Patch 11. Those steps give the overview of what these patches do, so I want them in the cover letter.
Great, I hadn't checked patch 11. And its more detailed there, so even better.
Quoted 3 lines
Since this patchset introduces several new things, before we reach the consensus on the implementation, arguments, etc., I'd like to defer the user documentation work.
Yep, completely agree, my comment was meant to put/keep the documentation on the radar (lest it be forgot).
Glenn
Quoted 142 lines
Thanks,
Gary Lin
Quoted 137 lines
Glenn
[1] https://qemu.readthedocs.io/en/latest/specs/tpm.html#the-qemu-tpm-emulator-device
Quoted 132 lines
(*1) https://lists.gnu.org/archive/html/grub-devel/2022-02/msg00006.html
(*2) https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
(*3) https://lists.gnu.org/archive/html/grub-devel/2021-06/msg00044.htmlDaniel Axtens (6):
posix_wrap: tweaks in preparation for libtasn1
libtasn1: import libtasn1-4.19.0
libtasn1: disable code not needed in grub
libtasn1: changes for grub compatibility
libtasn1: compile into asn1 module
test_asn1: test module for libtasn1Hernan 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.gitignore | 2 + Makefile.util.def | 29 + configure.ac | 9 + grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 42 + grub-core/disk/cryptodisk.c | 175 +- grub-core/kern/protectors.c | 75 + grub-core/lib/libtasn1/COPYING | 16 + grub-core/lib/libtasn1/README.md | 98 + grub-core/lib/libtasn1/lib/coding.c | 1433 ++++++++++ grub-core/lib/libtasn1/lib/decoding.c | 2504 +++++++++++++++++ grub-core/lib/libtasn1/lib/element.c | 1110 ++++++++ grub-core/lib/libtasn1/lib/element.h | 42 + grub-core/lib/libtasn1/lib/errors.c | 103 + grub-core/lib/libtasn1/lib/gstr.c | 74 + grub-core/lib/libtasn1/lib/gstr.h | 50 + grub-core/lib/libtasn1/lib/int.h | 221 ++ grub-core/lib/libtasn1/lib/parser_aux.c | 1179 ++++++++ grub-core/lib/libtasn1/lib/parser_aux.h | 172 ++ grub-core/lib/libtasn1/lib/structure.c | 1227 ++++++++ grub-core/lib/libtasn1/lib/structure.h | 46 + .../tests/CVE-2018-1000654-1_asn1_tab.h | 32 + .../tests/CVE-2018-1000654-2_asn1_tab.h | 36 + .../libtasn1_wrap/tests/CVE-2018-1000654.c | 61 + .../lib/libtasn1_wrap/tests/Test_overflow.c | 138 + .../lib/libtasn1_wrap/tests/Test_simple.c | 207 ++ .../lib/libtasn1_wrap/tests/Test_strings.c | 150 + .../libtasn1_wrap/tests/object-id-decoding.c | 116 + .../libtasn1_wrap/tests/object-id-encoding.c | 120 + .../lib/libtasn1_wrap/tests/octet-string.c | 211 ++ .../lib/libtasn1_wrap/tests/reproducers.c | 81 + grub-core/lib/libtasn1_wrap/wrap.c | 26 + grub-core/lib/libtasn1_wrap/wrap_tests.c | 75 + grub-core/lib/libtasn1_wrap/wrap_tests.h | 38 + grub-core/lib/posix_wrap/limits.h | 1 + grub-core/lib/posix_wrap/stdlib.h | 8 + grub-core/lib/posix_wrap/sys/types.h | 1 + grub-core/tpm2/args.c | 129 + grub-core/tpm2/buffer.c | 145 + grub-core/tpm2/module.c | 833 ++++++ grub-core/tpm2/mu.c | 807 ++++++ grub-core/tpm2/tcg2.c | 143 + grub-core/tpm2/tpm2.c | 761 +++++ grub-core/tpm2/tpm2key.asn | 31 + grub-core/tpm2/tpm2key.c | 218 ++ grub-core/tpm2/tpm2key_asn1_tab.c | 34 + include/grub/cryptodisk.h | 14 + include/grub/libtasn1.h | 645 +++++ 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 + include/grub/tpm2/tpm2key.h | 40 + tests/test_asn1.in | 12 + util/grub-protect.c | 1472 ++++++++++ 60 files changed, 16841 insertions(+), 32 deletions(-)create mode 100644
grub-core/kern/protectors.c
create mode 100644 grub-core/lib/libtasn1/COPYING
create mode 100644grub-core/lib/libtasn1/README.md
create mode 100644grub-core/lib/libtasn1/lib/coding.c
create mode 100644grub-core/lib/libtasn1/lib/decoding.c
create mode 100644grub-core/lib/libtasn1/lib/element.c
create mode 100644grub-core/lib/libtasn1/lib/element.h
create mode 100644grub-core/lib/libtasn1/lib/errors.c
create mode 100644grub-core/lib/libtasn1/lib/gstr.c
create mode 100644grub-core/lib/libtasn1/lib/gstr.h
create mode 100644grub-core/lib/libtasn1/lib/int.h
create mode 100644grub-core/lib/libtasn1/lib/parser_aux.c
create mode 100644grub-core/lib/libtasn1/lib/parser_aux.h
create mode 100644grub-core/lib/libtasn1/lib/structure.c
create mode 100644grub-core/lib/libtasn1/lib/structure.h
create mode 100644grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-1_asn1_tab.hcreate mode 100644grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-2_asn1_tab.hcreate mode 100644grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/Test_overflow.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/Test_simple.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/Test_strings.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/object-id-decoding.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/object-id-encoding.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/octet-string.ccreate mode 100644grub-core/lib/libtasn1_wrap/tests/reproducers.ccreate mode 100644grub-core/lib/libtasn1_wrap/wrap.c
create mode 100644grub-core/lib/libtasn1_wrap/wrap_tests.c
create mode 100644grub-core/lib/libtasn1_wrap/wrap_tests.h
create mode 100644grub-core/tpm2/args.c
create mode 100644grub-core/tpm2/buffer.c
create mode 100644grub-core/tpm2/module.c
create mode 100644grub-core/tpm2/mu.c
create mode 100644grub-core/tpm2/tcg2.c
create mode 100644grub-core/tpm2/tpm2.c
create mode 100644 grub-core/tpm2/tpm2key.asn
create mode 100644grub-core/tpm2/tpm2key.c
create mode 100644grub-core/tpm2/tpm2key_asn1_tab.c
create mode 100644include/grub/libtasn1.h
create mode 100644include/grub/protector.h
create mode 100644include/grub/tpm2/buffer.h
create mode 100644include/grub/tpm2/internal/args.h
create mode 100644include/grub/tpm2/internal/functions.h
create mode 100644include/grub/tpm2/internal/structs.h
create mode 100644include/grub/tpm2/internal/types.h
create mode 100644include/grub/tpm2/mu.h
create mode 100644include/grub/tpm2/tcg2.h
create mode 100644include/grub/tpm2/tpm2.h
create mode 100644include/grub/tpm2/tpm2key.h
create mode 100644 tests/test_asn1.in
create mode 100644util/grub-protect.c