GIT repo for v5: https://github.com/lcp/grub2/tree/tpm2-unlock-v5
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 adds the document for libtasn1 and the steps to upgrade the library. Patch 8~12 are Hernan Gatta's patches with the follow-up fixes and improvements: - 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 <o...@suse.com> - 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 so that we don't have to declare empty variables - 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 of grub_swap_bytes*() which may cause problems in big-indian machines - Changing the short name of "--protector" of "cryptomount" from "-k" to "-P" to avoid the conflict with "--key-file" - Supporting TPM 2.0 Key File Format besides the raw sealed key - Adding the external libtasn1 dependency to grub-protect to write the TPM 2.0 Key files Patch 13~16 implement the authorized policy support. Patch 17 implements the missing NV index mode. (Thanks to Patrick Colp) Patch 18 improves the 'cryptomount' command to fall back to the passphrase mode when the key protector fails to unlock the encrypted partition. (Another patch from Patrick Colp) Patch 19~20 fixes the potential security issues spotted by Fabian Vogt. 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.tpm 3. 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 v5: - Rebasing to the latest git HEAD and improving the commit messages - Implementing authorized poilcy support - Implementing NV index mode - Improving the 'cryptomount' command to fall back to the passphrase mode when the key protector fails to unlock the encrypted partition - Fixing the potential security issues v4: - https://lists.gnu.org/archive/html/grub-devel/2023-04/msg00104.html - GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v4 - Improving the error condition checks in cryptodisk.c - Moving the code to unseal with the standalone policy sequence below the code for authpolicy sequence - The standalone policy sequence was mistakenly prepended to to the authpolicy sequence with grub_list_push() while it should be appended. - Pushing the error messages from the authpolicy sequence into the grub_error stack so that we can list all errors from the sequence - Improving the error messages in the TPM2 protector - Amending the calculation of the max string lengths of 'Policy', 'CommandCode' and 'CommandPolicy' - Skipping the error path in grub_tpm2key_get_authpolicy_seq() on success to avoid freeing the authpolicy sequence v3: - https://lists.gnu.org/archive/html/grub-devel/2023-04/msg00055.html - GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v3 - Adding the document for libtasn1 - Improving the error condition checks ex: "if (!ptr)" ==> "if (ptr == NULL)" "if (err)" ==> "if (err != GRUB_ERR_NONE)" "if (rc)" ==> "if (rc != TPM_RC_SUCCESS)" - Supporting the "TPMPolicy" and "TPMAuthPolicy" sequence in the TPM 2.0 key File - Refactoring the key recover function to support "TPMPolicy" and "TPMAuthPolicy" sequence - Using TPMS_PCR_SELECTION_SelectPCR() to set the PCR bit mask - Also dropping TPM2_PCR_TO_SELECT() and TPM2_PCR_TO_BIT() which are not necessary anymore - Removing the redundant variable, 'crd', from grub_cryptodisk_scan_device_real() - Fixing the spaces/tabs in cryptodisk.c - Fixing the comment format in cryptodisk.h - Adding the defensive check for "cargs->protectors" in grub_cryptodisk_scan_device() - Improving 'grub-protect' for the better support of TPM 2.0 Key File - Adding more comments v2: - https://lists.gnu.org/archive/html/grub-devel/2023-03/msg00094.html - GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v2 v1: - https://lists.gnu.org/archive/html/grub-devel/2023-02/msg00130.html - GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock 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 Gary Lin (7): libtasn1: Add the documentation tpm2: Add TPM2 types, structures, and command constants tpm2: Add more marshal/unmarshal functions tpm2: Implement more TPM2 commands tpm2: Support authorized policy cryptodisk: wipe out the cached keys from protectors diskfilter: look up cryptodisk devices first 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 Patrick Colp (2): protectors: Implement NV index cryptodisk: Fallback to passphrase .gitignore | 2 + Makefile.util.def | 29 + configure.ac | 9 + docs/grub-dev.texi | 27 + grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 42 + grub-core/disk/cryptodisk.c | 183 +- grub-core/disk/diskfilter.c | 35 +- grub-core/kern/protectors.c | 75 + ...asn1-disable-code-not-needed-in-grub.patch | 311 ++ ...tasn1-changes-for-grub-compatibility.patch | 209 ++ 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 | 131 + grub-core/tpm2/buffer.c | 145 + grub-core/tpm2/module.c | 1146 ++++++++ grub-core/tpm2/mu.c | 1069 +++++++ grub-core/tpm2/tcg2.c | 143 + grub-core/tpm2/tpm2.c | 1185 ++++++++ grub-core/tpm2/tpm2key.asn | 31 + grub-core/tpm2/tpm2key.c | 440 +++ grub-core/tpm2/tpm2key_asn1_tab.c | 41 + include/grub/cryptodisk.h | 16 + 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 | 174 ++ include/grub/tpm2/internal/structs.h | 761 +++++ include/grub/tpm2/internal/types.h | 386 +++ include/grub/tpm2/mu.h | 367 +++ include/grub/tpm2/tcg2.h | 34 + include/grub/tpm2/tpm2.h | 34 + include/grub/tpm2/tpm2key.h | 83 + tests/test_asn1.in | 12 + util/grub-protect.c | 1508 ++++++++++ 64 files changed, 18961 insertions(+), 41 deletions(-) create mode 100644 grub-core/kern/protectors.c create mode 100644 grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch create mode 100644 grub-core/lib/libtasn1-patches/0002-libtasn1-changes-for-grub-compatibility.patch 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 Range-diff against v4: 1: 9167c663e = 1: 4abbfb9b9 posix_wrap: tweaks in preparation for libtasn1 2: 1b4a9c12b = 2: 032237404 libtasn1: import libtasn1-4.19.0 3: 2821434ad = 3: c8ef29db6 libtasn1: disable code not needed in grub 4: 98ec778a1 = 4: 8ec6f5492 libtasn1: changes for grub compatibility 5: e3aa6f3f9 ! 5: 8d1a2c5cf libtasn1: compile into asn1 module @@ Commit message ## grub-core/Makefile.core.def ## @@ grub-core/Makefile.core.def: module = { - common = commands/memtools.c; - condition = COND_MM_DEBUG; + efi = commands/bli.c; + enable = efi; }; + +module = { 6: de837eca0 = 6: ab12479d6 test_asn1: test module for libtasn1 7: 4b1507c28 ! 7: e99338e18 libtasn1: Add the documentation @@ docs/grub-dev.texi: cp minilzo-2.10/*.[hc] grub-core/lib/minilzo +@file{grub-core/lib/libtasn1-patches/} to adjust the code to be compatible with +grub. + - @node Porting - @chapter Porting + @node Debugging + @chapter Debugging ## grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch (new) ## 8: 5affde982 = 8: 0e699ac18 protectors: Add key protectors framework 9: a4f5c4aa6 ! 9: c806992eb tpm2: Add TPM Software Stack (TSS) @@ grub-core/tpm2/tcg2.c (new) + if (has_caps) + goto exit; + -+ status = efi_call_2 (protocol->get_capability, protocol, &caps); ++ status = protocol->get_capability (protocol, &caps); + if (status != GRUB_EFI_SUCCESS || !caps.TPMPresentFlag) + return GRUB_ERR_FILE_NOT_FOUND; + @@ grub-core/tpm2/tcg2.c (new) +static grub_err_t +grub_tcg2_get_protocol (grub_efi_tpm2_protocol_t **protocol) +{ -+ static grub_efi_guid_t tpm2_guid = EFI_TPM2_GUID; ++ static grub_guid_t tpm2_guid = EFI_TPM2_GUID; + static grub_efi_tpm2_protocol_t *tpm2_protocol = NULL; + + int tpm2; @@ grub-core/tpm2/tcg2.c (new) + if (err) + return err; + -+ status = efi_call_5 (protocol->submit_command, protocol, input_size, input, -+ output_size, output); ++ status = protocol->submit_command (protocol, input_size, input, ++ output_size, output); + if (status != GRUB_EFI_SUCCESS) + return GRUB_ERR_INVALID_COMMAND; + 10: ab730e5bc ! 10: 7c08cc056 protectors: Add TPM2 Key Protector @@ Commit message 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 + cryptomount -u <PART1_UUID> -P tpm2 tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub2/sealed-2.key --pcrs=7,11 - cryptomount DISK2 -P tpm2 + cryptomount -u <PART2_UUID> -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 + cryptomount -u <PART1_UUID> -P tpm2 tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub2/sealed-2.tpm --pcrs=7,11 - cryptomount DISK2 -P tpm2 + cryptomount -u <PART2_UUID> -P tpm2 If a user does not initialize the key protector and attempts to use it anyway, the protector returns an error. 11: d7c38672a = 11: aab155530 cryptodisk: Support key protectors 12: 38ebade13 ! 12: 3e175a1e8 util/grub-protect: Add new tool @@ Commit message 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. + has to ensure the cryptomount command is included in GRUB's boot script + and that it carries the requisite key protector (-P) parameter. Sample usage: @@ Commit message Then, 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 + cryptomount -u <SDB1_UUID> -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. + cryptomount -u <SDB1_UUID> -P tpm2 Signed-off-by: Hernan Gatta <hega...@linux.microsoft.com> Signed-off-by: Gary Lin <g...@suse.com> -: --------- > 13: 110e46ce2 tpm2: Add TPM2 types, structures, and command constants -: --------- > 14: 6a510e6bb tpm2: Add more marshal/unmarshal functions -: --------- > 15: 500e6f081 tpm2: Implement more TPM2 commands -: --------- > 16: 350089cdd tpm2: Support authorized policy -: --------- > 17: 8047e574a protectors: Implement NV index -: --------- > 18: 26510a9ba cryptodisk: Fallback to passphrase -: --------- > 19: fb1a22e16 cryptodisk: wipe out the cached keys from protectors -: --------- > 20: 11a0350ab diskfilter: look up cryptodisk devices first -- 2.35.3 _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel