Skip to contentSkip to conversation

Automatic Disk Unlock with TPM2

PATCH Superseded Gary Lin via Grub-devel 3 participants 32 messages Feb 5, 2024 – Jun 25, 2024 Kbuild

v9 of 23 Superseded Latest is v23 →

lore
Share & tools
lore
#0 lore raw

v9 cover Automatic Disk Unlock with TPM2

GIT repo for v9: https://github.com/lcp/grub2/tree/tpm2-unlock-v9

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 on 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 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 fix the potential security issues spotted by Fabian Vogt.

Patch 21~22 add the TPM key unsealing testcase.

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

v9:
- Introducing c-ctype.h to posix_wrap and implementing strncat
- Adding the descriptive comments to the disabled code in libtasn1
- Replacing strcat with the bound-checked _asn1_str_cat in libtasn1 and including c-ctype.h directly
- Integrating the asn1 testcases into "functional_test"
- Updating the libtasn1 patches mentioned in the documentation
- Moving the key protector to a module
- Amending configure.ac to enable/disable grub-protect
- Fixing an timeout issue in the tpm2_test script by feeding the config through stdin

v8:
- https://lists.gnu.org/archive/html/grub-devel/2024-01/msg00013.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v8
- Introducing TPM device support to grub-emu and adding the TPM key unsealing testcase

v7:
- https://lists.gnu.org/archive/html/grub-devel/2023-11/msg00127.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v7
- Stopping reading SRK from the well-known persistent handle (TPM2_SRK_HANDLE, i.e. 0x81000001) by default since the persistent handle may be created by other OS and causes unsealing failure due to SRK mismatching
- The user now has to specify the persistent handle with "--srk" explicitly.
- Utilizing grub_error() to print more error messages
- Unifying the format of the error messages from TPM2 commands

v6:
- https://lists.gnu.org/archive/html/grub-devel/2023-10/msg00026.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v6
- Supporting more SRK types than RSA2048 and ECC_NIST_P256
- Documenting SHA512 as the supported PCR bank type in the tpm2 protector
- Removing the redundant error message for grub_tpm2_protector_srk_get() since it may overwrite the real error message.
- Updating the supported SRK types and PCR bank types in grub-protect
- Removing the unused type: TPM2_ECC_CURVE

v5:
- https://lists.gnu.org/archive/html/grub-devel/2023-08/msg00113.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-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
asn1_test: test module for libtasn1

Gary Lin (9):
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
tpm2: Enable tpm2 module for grub-emu
tests: Add tpm2_test

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

Patrick Colp (2):
tpm2: Implement NV index
cryptodisk: Fallback to passphrase

 .gitignore                                    |    2 +
 Makefile.util.def                             |   35 +
 configure.ac                                  |   30 +
 docs/grub-dev.texi                            |   27 +
 grub-core/Makefile.am                         |    1 +
 grub-core/Makefile.core.def                   |   48 +
 grub-core/disk/cryptodisk.c                   |  183 +-
 grub-core/disk/diskfilter.c                   |   35 +-
 grub-core/disk/key_protector.c                |   78 +
 grub-core/kern/emu/main.c                     |   11 +-
 grub-core/kern/emu/misc.c                     |   51 +
 ...asn1-disable-code-not-needed-in-grub.patch |  319 +++
 ...tasn1-changes-for-grub-compatibility.patch |  142 +
 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         | 2503 +++++++++++++++++
 grub-core/lib/libtasn1/lib/element.c          | 1109 ++++++++
 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              |  220 ++
 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        | 1227 ++++++++
 grub-core/lib/libtasn1/lib/structure.h        |   46 +
 grub-core/lib/libtasn1_wrap/wrap.c            |   26 +
 grub-core/lib/posix_wrap/c-ctype.h            |  114 +
 grub-core/lib/posix_wrap/limits.h             |    1 +
 grub-core/lib/posix_wrap/stdlib.h             |    8 +
 grub-core/lib/posix_wrap/string.h             |   21 +
 grub-core/lib/posix_wrap/sys/types.h          |    1 +
 .../tests/asn1/CVE-2018-1000654-1_asn1_tab.h  |   32 +
 .../tests/asn1/CVE-2018-1000654-2_asn1_tab.h  |   36 +
 grub-core/tests/asn1/CVE-2018-1000654.c       |   58 +
 grub-core/tests/asn1/Test_overflow.c          |  134 +
 grub-core/tests/asn1/Test_simple.c            |  205 ++
 grub-core/tests/asn1/Test_strings.c           |  142 +
 grub-core/tests/asn1/asn1_test.c              |   49 +
 grub-core/tests/asn1/asn1_test.h              |   44 +
 grub-core/tests/asn1/object-id-decoding.c     |  109 +
 grub-core/tests/asn1/object-id-encoding.c     |  114 +
 grub-core/tests/asn1/octet-string.c           |  199 ++
 grub-core/tests/asn1/reproducers.c            |   80 +
 grub-core/tests/lib/functional_test.c         |    1 +
 grub-core/tpm2/args.c                         |  177 ++
 grub-core/tpm2/buffer.c                       |  145 +
 grub-core/tpm2/module.c                       | 1127 ++++++++
 grub-core/tpm2/mu.c                           | 1069 +++++++
 grub-core/tpm2/tcg2-emu.c                     |   52 +
 grub-core/tpm2/tcg2.c                         |  143 +
 grub-core/tpm2/tpm2.c                         | 1185 ++++++++
 grub-core/tpm2/tpm2key.asn                    |   31 +
 grub-core/tpm2/tpm2key.c                      |  447 +++
 grub-core/tpm2/tpm2key_asn1_tab.c             |   41 +
 include/grub/cryptodisk.h                     |   16 +
 include/grub/emu/misc.h                       |    5 +
 include/grub/key_protector.h                  |   46 +
 include/grub/libtasn1.h                       |  657 +++++
 include/grub/tpm2/buffer.h                    |   65 +
 include/grub/tpm2/internal/args.h             |   41 +
 include/grub/tpm2/internal/functions.h        |  174 ++
 include/grub/tpm2/internal/structs.h          |  761 +++++
 include/grub/tpm2/internal/types.h            |  384 +++
 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/asn1_test.in                            |   12 +
 tests/tpm2_test.in                            |  179 ++
 tests/util/grub-shell.in                      |    6 +-
 util/grub-protect.c                           | 1492 ++++++++++
 73 files changed, 19337 insertions(+), 43 deletions(-)
 create mode 100644 grub-core/disk/key_protector.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/wrap.c
 create mode 100644 grub-core/lib/posix_wrap/c-ctype.h
 create mode 100644 grub-core/tests/asn1/CVE-2018-1000654-1_asn1_tab.h
 create mode 100644 grub-core/tests/asn1/CVE-2018-1000654-2_asn1_tab.h
 create mode 100644 grub-core/tests/asn1/CVE-2018-1000654.c
 create mode 100644 grub-core/tests/asn1/Test_overflow.c
 create mode 100644 grub-core/tests/asn1/Test_simple.c
 create mode 100644 grub-core/tests/asn1/Test_strings.c
 create mode 100644 grub-core/tests/asn1/asn1_test.c
 create mode 100644 grub-core/tests/asn1/asn1_test.h
 create mode 100644 grub-core/tests/asn1/object-id-decoding.c
 create mode 100644 grub-core/tests/asn1/object-id-encoding.c
 create mode 100644 grub-core/tests/asn1/octet-string.c
 create mode 100644 grub-core/tests/asn1/reproducers.c
 create mode 100644 grub-core/tpm2/args.c
 create mode 100644 grub-core/tpm2/buffer.c
 create mode 100644 grub-core/tpm2/module.c
 create mode 100644 grub-core/tpm2/mu.c
 create mode 100644 grub-core/tpm2/tcg2-emu.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/key_protector.h
 create mode 100644 include/grub/libtasn1.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/asn1_test.in
 create mode 100644 tests/tpm2_test.in
 create mode 100644 util/grub-protect.c

Range-diff against v8:

 1:  dcda63b50 <  -:  --------- posix_wrap: tweaks in preparation for libtasn1
 -:  --------- >  1:  e1b4dca2d posix_wrap: tweaks in preparation for libtasn1
 2:  9f3ca3e25 !  2:  89dc7ef87 libtasn1: import libtasn1-4.19.0

@@ Commit message

+ Reviewed-by: Vladimir Serbinenko <[email protected]>

## grub-core/lib/libtasn1/COPYING (new) ##
@@
3: 543b5b4f7 ! 3: 07f7ff212 libtasn1: disable code not needed in grub

    @@ Commit message
         which is convenient because it means we don't have to
         import it from gnulib.
     
    +    Cc: Vladimir Serbinenko <[email protected]>
         Signed-off-by: Daniel Axtens <[email protected]>
         Signed-off-by: Gary Lin <[email protected]>
     
    @@ grub-core/lib/libtasn1/lib/coding.c
      
      #define MAX_TAG_LEN 16
      
    -+#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
      /******************************************************/
      /* Function : _asn1_error_description_value_not_found */
      /* Description: creates the ErrorDescription string   */
    @@ grub-core/lib/libtasn1/lib/coding.c: asn1_encode_simple_der (unsigned int etype,
        return ASN1_SUCCESS;
      }
      
    -+#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
      /******************************************************/
      /* Function : _asn1_time_der                          */
      /* Description: creates the DER coding for a TIME     */
    @@ grub-core/lib/libtasn1/lib/coding.c: asn1_bit_der (const unsigned char *str, int
      }
      
      
    -+#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
      /******************************************************/
      /* Function : _asn1_complete_explicit_tag             */
      /* Description: add the length coding to the EXPLICIT */
    @@ grub-core/lib/libtasn1/lib/coding.c: const tag_and_class_st _asn1_tags[] = {
      unsigned int _asn1_tags_size = sizeof (_asn1_tags) / sizeof (_asn1_tags[0]);
      
     +
    -+#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
      /******************************************************/
      /* Function : _asn1_insert_tag_der                    */
      /* Description: creates the DER coding of tags of one */
    @@ grub-core/lib/libtasn1/lib/coding.c: error:
      }
     +
     +#endif
    - \ No newline at end of file
     
      ## grub-core/lib/libtasn1/lib/decoding.c ##
     @@ grub-core/lib/libtasn1/lib/decoding.c: asn1_der_decoding (asn1_node * element, const void *ider, int ider_len,
        return asn1_der_decoding2 (element, ider, &ider_len, 0, errorDescription);
      }
      
    -+#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
      /**
       * asn1_der_decoding_element:
       * @structure: pointer to an ASN1 structure
    @@ grub-core/lib/libtasn1/lib/decoding.c: asn1_der_decoding_element (asn1_node * st
       * asn1_der_decoding_startEnd:
     
      ## grub-core/lib/libtasn1/lib/element.c ##
    +@@ grub-core/lib/libtasn1/lib/element.c: _asn1_convert_integer (const unsigned char *value, unsigned char *value_out,
    + 	value_out[k2 - k] = val[k2];
    +     }
    + 
    +-#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
    +   printf ("_asn1_convert_integer: valueIn=%s, lenOut=%d", value, *len);
    +   for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT; k++)
    +     printf (", vOut[%d]=%d", k, value_out[k]);
     @@ grub-core/lib/libtasn1/lib/element.c: _asn1_append_sequence_set (asn1_node node, struct node_tail_cache_st *pcache)
        return ASN1_SUCCESS;
      }
    @@ grub-core/lib/libtasn1/lib/errors.c: static const libtasn1_error_entry error_alg
      };
      
     +
    -+#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
      /**
       * asn1_perror:
       * @error: is an error returned by a libtasn1 function.
    @@ grub-core/lib/libtasn1/lib/structure.c: _asn1_find_left (asn1_node_const node)
      }
      
     -
    -+#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
      int
      _asn1_create_static_structure (asn1_node_const pointer,
      			       char *output_file_name, char *vector_name)
    @@ grub-core/lib/libtasn1/lib/structure.c: asn1_create_element (asn1_node_const def
      }
      
     -
    -+#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
      /**
       * asn1_print_structure:
       * @out: pointer to the output file (e.g. stdout).
    @@ grub-core/lib/libtasn1/lib/structure.c: asn1_find_structure_from_oid (asn1_node_
        return NULL;			/* ASN1_ELEMENT_NOT_FOUND; */
      }
      
    -+#if 0
    ++#if 0 /* GRUB SKIPPED IMPORTING */
      /**
       * asn1_copy_node:
       * @dst: Destination asn1 node.
    @@ include/grub/libtasn1.h: extern "C"
      /***********************************/
      
     +/* These functions are not used in grub and should not be referenced. */
    -+# if 0
    ++# if 0 /* GRUB SKIPPED IMPORTING */
        extern ASN1_API int
          asn1_parser2tree (const char *file,
      		      asn1_node * definitions, char *error_desc);
    @@ include/grub/libtasn1.h: extern "C"
          asn1_array2tree (const asn1_static_node * array,
      		     asn1_node * definitions, char *errorDescription);
      
    -+# if 0
    ++# if 0 /* GRUB SKIPPED IMPORTING */
        extern ASN1_API void
          asn1_print_structure (FILE * out, asn1_node_const structure,
      			  const char *name, int mode);
    @@ include/grub/libtasn1.h: extern "C"
        extern ASN1_API int
          asn1_delete_element (asn1_node structure, const char *element_name);
      
    -+# if 0
    ++# if 0 /* GRUB SKIPPED IMPORTING */
        extern ASN1_API int
          asn1_write_value (asn1_node node_root, const char *name,
      		      const void *ivalue, int len);
    @@ include/grub/libtasn1.h: extern "C"
          asn1_number_of_elements (asn1_node_const element, const char *name,
      			     int *num);
      
    -+# if 0
    ++# if 0 /* GRUB SKIPPED IMPORTING */
        extern ASN1_API int
          asn1_der_coding (asn1_node_const element, const char *name,
      		     void *ider, int *len, char *ErrorDescription);
    @@ include/grub/libtasn1.h: extern "C"
          asn1_der_decoding (asn1_node * element, const void *ider,
      		       int ider_len, char *errorDescription);
      
    -+# if 0
    ++# if 0 /* GRUB SKIPPED IMPORTING */
      /* Do not use. Use asn1_der_decoding() instead. */
        extern ASN1_API int
          asn1_der_decoding_element (asn1_node * structure,
    @@ include/grub/libtasn1.h: extern "C"
      							    const char
      							    *oidValue);
      
    -+# if 0
    ++# if 0 /* GRUB SKIPPED IMPORTING */
          __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
    ++# if 0 /* GRUB SKIPPED IMPORTING */
        extern ASN1_API void asn1_perror (int error);
     +# endif
 4:  01c6f8a5e <  -:  --------- libtasn1: changes for grub compatibility
 -:  --------- >  4:  0113d1c12 libtasn1: changes for grub compatibility
 5:  89cb4a6e8 !  5:  e5e4db9fc libtasn1: compile into asn1 module

@@ Commit message

+ Reviewed-by: Vladimir Serbinenko <[email protected]>

## grub-core/Makefile.core.def ##
@@ grub-core/Makefile.core.def: module = {
6: 95296156f ! 6: a43b4a45e test_asn1: test module for libtasn1

    @@ Metadata
     Author: Daniel Axtens <[email protected]>
     
      ## Commit message ##
    -    test_asn1: test module for libtasn1
    +    asn1_test: test module for libtasn1
     
         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.
    +    import. This test module is integrated into functional_test so that the
    +    user can run the test in grub shell.
     
         This doesn't test the full decoder but that will be exercised in
         test suites for coming patch sets.
    @@ Commit message
         Add testcase target in accordance with
         5e10be48e5 tests: Add check-native and check-nonnative make targets
     
    +    Cc: Vladimir Serbinenko <[email protected]>
         Signed-off-by: Daniel Axtens <[email protected]>
         Signed-off-by: Gary Lin <[email protected]>
     
    @@ Makefile.util.def: script = {
      
     +script = {
     +  testcase = native;
    -+  name = test_asn1;
    -+  common = tests/test_asn1.in;
    ++  name = asn1_test;
    ++  common = tests/asn1_test.in;
     +};
     +
      program = {
    @@ grub-core/Makefile.core.def: module = {
      };
     +
     +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;
    ++  name = asn1_test;
    ++  common = tests/asn1/CVE-2018-1000654.c;
    ++  common = tests/asn1/object-id-decoding.c;
    ++  common = tests/asn1/object-id-encoding.c;
    ++  common = tests/asn1/octet-string.c;
    ++  common = tests/asn1/reproducers.c;
    ++  common = tests/asn1/Test_overflow.c;
    ++  common = tests/asn1/Test_simple.c;
    ++  common = tests/asn1/Test_strings.c;
    ++  common = tests/asn1/asn1_test.c;
     +};
     
    - ## grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-1_asn1_tab.h (new) ##
    + ## grub-core/tests/asn1/CVE-2018-1000654-1_asn1_tab.h (new) ##
     @@
     +#if HAVE_CONFIG_H
     +# include "config.h"
    @@ grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-1_asn1_tab.h (new)
     +  { NULL, 0, NULL }
     +};
     
    - ## grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-2_asn1_tab.h (new) ##
    + ## grub-core/tests/asn1/CVE-2018-1000654-2_asn1_tab.h (new) ##
     @@
     +#if HAVE_CONFIG_H
     +# include "config.h"
    @@ grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-2_asn1_tab.h (new)
     +  { NULL, 0, NULL }
     +};
     
    - ## grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.c (new) ##
    + ## grub-core/tests/asn1/CVE-2018-1000654.c (new) ##
     @@
     +/*
     + * Copyright (C) 2002-2018 Free Software Foundation, Inc.
    @@ grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.c (new)
     +/* Description: reproducer for CVE-2018-1000654			*/
     +/****************************************************************/
     +
    -+#include <grub/libtasn1.h>
    -+#include <grub/err.h>
    -+#include <grub/mm.h>
    -+#include <grub/misc.h>
    -+#include <grub/types.h>
    -+#include "../wrap_tests.h"
    ++#include "asn1_test.h"
     +
     +#include "CVE-2018-1000654-1_asn1_tab.h"
     +#include "CVE-2018-1000654-2_asn1_tab.h"
     +
    -+void
    ++int
     +test_CVE_2018_1000654 (void)
     +{
     +  int result;
    @@ grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.c (new)
     +  result = asn1_array2tree (CVE_2018_1000654_1_asn1_tab, &definitions, errorDescription);
     +  if (result != ASN1_RECURSION)
     +    {
    -+      grub_fatal ("Error: %s\nErrorDescription = %s\n\n",
    -+		  asn1_strerror (result), errorDescription);
    -+      return;
    ++      grub_printf ("Error: %s\nErrorDescription = %s\n\n",
    ++		   asn1_strerror (result), errorDescription);
    ++      return 1;
     +    }
     +
     +  asn1_delete_structure (&definitions);
    @@ grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.c (new)
     +  result = asn1_array2tree (CVE_2018_1000654_2_asn1_tab, &definitions, errorDescription);
     +  if (result != ASN1_RECURSION)
     +    {
    -+      grub_fatal ("Error: %s\nErrorDescription = %s\n\n",
    -+		  asn1_strerror (result), errorDescription);
    -+      return;
    ++      grub_printf ("Error: %s\nErrorDescription = %s\n\n",
    ++		   asn1_strerror (result), errorDescription);
    ++      return 1;
     +    }
     +
     +  asn1_delete_structure (&definitions);
    ++
    ++  return 0;
     +}
     
    - ## grub-core/lib/libtasn1_wrap/tests/Test_overflow.c (new) ##
    + ## grub-core/tests/asn1/Test_overflow.c (new) ##
     @@
     +/*
     + * Copyright (C) 2012-2014 Free Software Foundation, Inc.
    @@ grub-core/lib/libtasn1_wrap/tests/Test_overflow.c (new)
     +
     +/* Written by Simon Josefsson */
     +
    -+#include <grub/libtasn1.h>
    -+#include <grub/err.h>
    -+#include <grub/mm.h>
    -+#include <grub/misc.h>
    -+#include <grub/types.h>
    -+#include "../wrap_tests.h"
    ++#include "asn1_test.h"
     +
    -+void
    ++int
     +test_overflow(void)
     +{
     +  /* Test that values larger than long are rejected.  This has worked
    @@ grub-core/lib/libtasn1_wrap/tests/Test_overflow.c (new)
     +
     +    if (l != -2L)
     +      {
    -+	grub_fatal ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n", l, len);
    -+	return;
    ++	grub_printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n", l, len);
    ++	return 1;
     +      }
     +  }
     +
    @@ grub-core/lib/libtasn1_wrap/tests/Test_overflow.c (new)
     +
     +      if (l != -2L)
     +	{
    -+	  grub_fatal ("ERROR: asn1_get_length_der intnum (l %ld len %d)\n", l,
    -+		      len);
    -+	  return;
    ++	  grub_printf ("ERROR: asn1_get_length_der intnum (l %ld len %d)\n", l,
    ++		       len);
    ++	  return 1;
     +	}
     +    }
     +#endif
    @@ grub-core/lib/libtasn1_wrap/tests/Test_overflow.c (new)
     +
     +    if (l != -4L)
     +      {
    -+	grub_fatal ("ERROR: asn1_get_length_der overflow-small (l %ld len %d)\n",
    -+		    l, len);
    -+	return;
    ++	grub_printf ("ERROR: asn1_get_length_der overflow-small (l %ld len %d)\n",
    ++		     l, len);
    ++	return 1;
     +      }
     +  }
     +
    @@ grub-core/lib/libtasn1_wrap/tests/Test_overflow.c (new)
     +
     +    if (l != -4L)
     +      {
    -+	grub_fatal ("ERROR: asn1_get_length_der overflow-large1 (l %ld len %d)\n",
    -+		    l, len);
    -+	return;
    ++	grub_printf ("ERROR: asn1_get_length_der overflow-large1 (l %ld len %d)\n",
    ++		     l, len);
    ++	return 1;
     +      }
     +  }
     +
    @@ grub-core/lib/libtasn1_wrap/tests/Test_overflow.c (new)
     +
     +    if (l != -2L)
     +      {
    -+	grub_fatal ("ERROR: asn1_get_length_der overflow-large2 (l %ld len %d)\n",
    -+		    l, len);
    -+	return;
    ++	grub_printf ("ERROR: asn1_get_length_der overflow-large2 (l %ld len %d)\n",
    ++		     l, len);
    ++	return 1;
     +      }
     +  }
    ++  return 0;
     +}
     
    - ## grub-core/lib/libtasn1_wrap/tests/Test_simple.c (new) ##
    + ## grub-core/tests/asn1/Test_simple.c (new) ##
     @@
     +/*
     + * Copyright (C) 2011-2014 Free Software Foundation, Inc.
    @@ grub-core/lib/libtasn1_wrap/tests/Test_simple.c (new)
     + *
     + */
     +
    -+#include <grub/libtasn1.h>
    -+#include <grub/mm.h>
    -+#include <grub/misc.h>
    -+#include <grub/err.h>
    -+#include "../wrap_tests.h"
    ++#include "asn1_test.h"
     +
     +struct tv
     +{
    @@ grub-core/lib/libtasn1_wrap/tests/Test_simple.c (new)
     +  {19, "\xFF\xFF\xFF", 5, "\x04\x05\xff\xff\xe0"},
     +};
     +
    -+void
    ++int
     +test_simple (void)
     +{
     +  int result;
    @@ grub-core/lib/libtasn1_wrap/tests/Test_simple.c (new)
     +  result = asn1_get_bit_der (der, 0, &ret_len, str, str_size, &bit_len);
     +  if (result != ASN1_GENERIC_ERROR)
     +    {
    -+      grub_fatal ("asn1_get_bit_der zero\n");
    -+      return;
    ++      grub_printf ("asn1_get_bit_der zero\n");
    ++      return 1;
     +    }
     +
     +  /* Encode short strings with increasing bit lengths */
    @@ grub-core/lib/libtasn1_wrap/tests/Test_simple.c (new)
     +
     +      if (der_len != tv[i].derlen || grub_memcmp (der, tv[i].der, der_len) != 0)
     +	{
    -+	  grub_fatal ("asn1_bit_der iter %lu\n", (unsigned long) i);
    -+	  return;
    ++	  grub_printf ("asn1_bit_der iter %lu\n", (unsigned long) i);
    ++	  return 1;
     +	}
     +
     +      /* Decode it */
    @@ grub-core/lib/libtasn1_wrap/tests/Test_simple.c (new)
     +      if (result != ASN1_SUCCESS || ret_len != tv[i].derlen
     +	  || bit_len != tv[i].bitlen)
     +	{
    -+	  grub_fatal ("asn1_get_bit_der iter %lu, err: %d\n", (unsigned long) i, result);
    -+	  return;
    ++	  grub_printf ("asn1_get_bit_der iter %lu, err: %d\n", (unsigned long) i, result);
    ++	  return 1;
     +	}
     +    }
     +
    @@ grub-core/lib/libtasn1_wrap/tests/Test_simple.c (new)
     +  if (result != ASN1_SUCCESS || ret_len != 5
     +      || bit_len != 18 || grub_memcmp (str, "\x6e\x5d\xc0", 3) != 0)
     +    {
    -+      grub_fatal ("asn1_get_bit_der example\n");
    -+      return;
    ++      grub_printf ("asn1_get_bit_der example\n");
    ++      return 1;
     +    }
     +
     +  der_len = sizeof (der);
     +  asn1_bit_der (str, bit_len, der, &der_len);
     +  if (der_len != 5 || grub_memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
     +    {
    -+      grub_fatal ("asn1_bit_der example roundtrip\n");
    -+      return;
    ++      grub_printf ("asn1_bit_der example roundtrip\n");
    ++      return 1;
     +    }
     +
     +  /* 03 04 06 6e 5d e0 padded with "100000" */
    @@ grub-core/lib/libtasn1_wrap/tests/Test_simple.c (new)
     +  if (result != ASN1_SUCCESS || ret_len != 5
     +      || bit_len != 18 || grub_memcmp (str, "\x6e\x5d\xe0", 3) != 0)
     +    {
    -+      grub_fatal ("asn1_get_bit_der example padded\n");
    -+      return;
    ++      grub_printf ("asn1_get_bit_der example padded\n");
    ++      return 1;
     +    }
     +
     +  der_len = sizeof (der);
     +  asn1_bit_der (str, bit_len, der, &der_len);
     +  if (der_len != 5 || grub_memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
     +    {
    -+      grub_fatal ("asn1_bit_der example roundtrip\n");
    -+      return;
    ++      grub_printf ("asn1_bit_der example roundtrip\n");
    ++      return 1;
     +    }
     +
     +  /* 03 81 04 06 6e 5d c0 long form of length octets */
    @@ grub-core/lib/libtasn1_wrap/tests/Test_simple.c (new)
     +  if (result != ASN1_SUCCESS || ret_len != 6
     +      || bit_len != 18 || grub_memcmp (str, "\x6e\x5d\xc0", 3) != 0)
     +    {
    -+      grub_fatal ("asn1_get_bit_der example long form\n");
    -+      return;
    ++      grub_printf ("asn1_get_bit_der example long form\n");
    ++      return 1;
     +    }
     +
     +  der_len = sizeof (der);
     +  asn1_bit_der (str, bit_len, der, &der_len);
     +  if (der_len != 5 || grub_memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0)
     +    {
    -+      grub_fatal ("asn1_bit_der example roundtrip\n");
    -+      return;
    ++      grub_printf ("asn1_bit_der example roundtrip\n");
    ++      return 1;
     +    }
    ++
    ++  return 0;
     +}
     
    - ## grub-core/lib/libtasn1_wrap/tests/Test_strings.c (new) ##
    + ## grub-core/tests/asn1/Test_strings.c (new) ##
     @@
     +/*
     + * Copyright (C) 2012-2014 Free Software Foundation, Inc.
    @@ grub-core/lib/libtasn1_wrap/tests/Test_strings.c (new)
     + *
     + */
     +
    -+#include <grub/mm.h>
    -+#include <grub/err.h>
    -+#include <grub/misc.h>
    -+#include <grub/libtasn1.h>
    -+#include "../wrap_tests.h"
    ++#include "asn1_test.h"
     +
     +struct tv
     +{
    @@ grub-core/lib/libtasn1_wrap/tests/Test_strings.c (new)
     +   SSTR("\x24\x80\x04\x82\x00\x02\xa0\xa0\x04\x82\x00\x03\xb0\xb0\xb0\x24\x80\x04\x82\x00\x02\xa1\xa1\x04\x82\x00\x01\xc1\x00\x00\x00\x00")},
     +};
     +
    -+void
    ++int
     +test_strings ()
     +{
     +  int ret;
    @@ grub-core/lib/libtasn1_wrap/tests/Test_strings.c (new)
     +				    tl, &tl_len);
     +      if (ret != ASN1_SUCCESS)
     +	{
    -+	  grub_fatal ("Encoding error in %u: %s\n", i,
    -+		   asn1_strerror (ret));
    -+	  return;
    ++	  grub_printf ("Encoding error in %u: %s\n", i, asn1_strerror (ret));
    ++	  return 1;
     +	}
     +      der_len = tl_len + tv[i].str_len;
     +
     +      if (der_len != tv[i].der_len || grub_memcmp (tl, tv[i].der, tl_len) != 0)
     +	{
    -+	  grub_fatal (
    -+		   "DER encoding differs in %u! (size: %u, expected: %u)\n",
    -+		   i, der_len, tv[i].der_len);
    -+	  return;
    ++	  grub_printf ("DER encoding differs in %u! (size: %u, expected: %u)\n",
    ++		       i, der_len, tv[i].der_len);
    ++	  return 1;
     +	}
     +
     +      /* decoding */
    @@ grub-core/lib/libtasn1_wrap/tests/Test_strings.c (new)
     +				&str_len);
     +      if (ret != ASN1_SUCCESS)
     +	{
    -+	  grub_fatal ("Decoding error in %u: %s\n", i,
    -+		   asn1_strerror (ret));
    -+	  return;
    ++	  grub_printf ("Decoding error in %u: %s\n", i, asn1_strerror (ret));
    ++	  return 1;
     +	}
     +
     +      if (str_len != tv[i].str_len || grub_memcmp (str, tv[i].str, str_len) != 0)
     +	{
    -+	  grub_fatal (
    -+		   "DER decoded data differ in %u! (size: %u, expected: %u)\n",
    -+		   i, der_len, tv[i].str_len);
    -+	  return;
    ++	  grub_printf ("DER decoded data differ in %u! (size: %u, expected: %u)\n",
    ++		       i, der_len, tv[i].str_len);
    ++	  return 1;
     +	}
     +    }
     +
    @@ grub-core/lib/libtasn1_wrap/tests/Test_strings.c (new)
     +				&str_len, NULL);
     +      if (ret != ASN1_SUCCESS)
     +	{
    -+	  grub_fatal ("BER decoding error in %u: %s\n", i,
    -+		   asn1_strerror (ret));
    -+	  return;
    ++	  grub_printf ("BER decoding error in %u: %s\n", i, asn1_strerror (ret));
    ++	  return 1;
     +	}
     +
     +      if (str_len != ber[i].str_len || grub_memcmp (b, ber[i].str, str_len) != 0)
     +	{
    -+	  grub_fatal (
    -+		   "BER decoded data differ in %u! (size: %u, expected: %u)\n",
    -+		   i, str_len, ber[i].str_len);
    -+	  return;
    ++	  grub_printf ("BER decoded data differ in %u! (size: %u, expected: %u)\n",
    ++		       i, str_len, ber[i].str_len);
    ++	  return 1;
     +	}
     +      grub_free(b);
     +    }
    ++
    ++  return 0;
    ++}
    +
    + ## grub-core/tests/asn1/asn1_test.c (new) ##
    +@@
    ++/*
    ++ *  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/test.h>
    ++#include <grub/dl.h>
    ++#include "asn1_test.h"
    ++
    ++/*
    ++ * libtasn1 tests - from which this is derived - are provided under GPL3+.
    ++ */
    ++GRUB_MOD_LICENSE ("GPLv3+");
    ++
    ++static void
    ++asn1_test (void)
    ++{
    ++  grub_test_assert (test_CVE_2018_1000654 () == 0, "CVE-2018-1000654 test failed");
    ++
    ++  grub_test_assert (test_object_id_encoding () == 0, "ASN.1 object ID encoding test failed");
    ++
    ++  grub_test_assert (test_object_id_decoding () == 0, "ASN.1 object ID decoding test failed");
    ++
    ++  grub_test_assert (test_octet_string () == 0, "ASN.1 octet string test failed");
    ++
    ++  grub_test_assert (test_overflow () == 0, "ASN.1 overflow test failed");
    ++
    ++  grub_test_assert (test_reproducers () == 0, "ASN.1 reproducers test failed");
    ++
    ++  grub_test_assert (test_simple () == 0, "ASN.1 simple test failed");
    ++
    ++  grub_test_assert (test_strings () == 0, "ASN.1 strings test fail" );
     +}
    ++
    ++/* Register asn1_test method as a functional test.  */
    ++GRUB_FUNCTIONAL_TEST (asn1_test, asn1_test);
    +
    + ## grub-core/tests/asn1/asn1_test.h (new) ##
    +@@
    ++/*
    ++ *  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
    ++
    ++#include <grub/libtasn1.h>
    ++#include <grub/err.h>
    ++#include <grub/mm.h>
    ++#include <grub/misc.h>
    ++#include <grub/types.h>
    ++
    ++int test_CVE_2018_1000654 (void);
    ++
    ++int test_object_id_encoding (void);
    ++
    ++int test_object_id_decoding (void);
    ++
    ++int test_octet_string (void);
    ++
    ++int test_overflow (void);
    ++
    ++int test_reproducers (void);
    ++
    ++int test_simple (void);
    ++
    ++int test_strings (void);
    ++
    ++#endif
     
    - ## grub-core/lib/libtasn1_wrap/tests/object-id-decoding.c (new) ##
    + ## grub-core/tests/asn1/object-id-decoding.c (new) ##
     @@
     +/*
     + * Copyright (C) 2016 Red Hat, Inc.
    @@ grub-core/lib/libtasn1_wrap/tests/object-id-decoding.c (new)
     + *
     + */
     +
    -+#include <grub/libtasn1.h>
    -+#include <grub/types.h>
    -+#include <grub/misc.h>
    -+#include <grub/err.h>
    -+#include "../wrap_tests.h"
    ++#include "asn1_test.h"
     +
     +struct tv
     +{
    @@ grub-core/lib/libtasn1_wrap/tests/object-id-decoding.c (new)
     +   .expected_error = ASN1_SUCCESS},
     +};
     +
    -+void
    ++int
     +test_object_id_decoding (void)
     +{
     +  char str[128];
    @@ grub-core/lib/libtasn1_wrap/tests/object-id-decoding.c (new)
     +				sizeof (str));
     +      if (ret != tv[i].expected_error)
     +	{
    -+	  grub_fatal (
    -+		   "%d: asn1_get_object_id_der iter %lu: got '%s' expected %d\n",
    -+		   __LINE__, (unsigned long) i, asn1_strerror(ret), tv[i].expected_error);
    -+	  return;
    ++	  grub_printf ("%d: asn1_get_object_id_der iter %lu: got '%s' expected %d\n",
    ++		       __LINE__, (unsigned long) i, asn1_strerror(ret), tv[i].expected_error);
    ++	  return 1;
     +	}
     +
     +      if (tv[i].expected_error != ASN1_SUCCESS)
    @@ grub-core/lib/libtasn1_wrap/tests/object-id-decoding.c (new)
     +
     +      if (ret_len != tv[i].der_len-1)
     +	{
    -+	  grub_fatal (
    -+		   "%d: iter %lu: error in DER, length returned is %d, had %d\n",
    -+		   __LINE__, (unsigned long)i, ret_len, tv[i].der_len-1);
    -+	  return;
    ++	  grub_printf ("%d: iter %lu: error in DER, length returned is %d, had %d\n",
    ++		       __LINE__, (unsigned long)i, ret_len, tv[i].der_len-1);
    ++	  return 1;
     +	}
     +
     +      if (grub_strcmp (tv[i].oid, str) != 0)
     +	{
    -+	  grub_fatal (
    -+		   "%d: strcmp iter %lu: got invalid OID: %s, expected: %s\n",
    -+		   __LINE__, (unsigned long) i, str, tv[i].oid);
    -+	  return;
    ++	  grub_printf ("%d: strcmp iter %lu: got invalid OID: %s, expected: %s\n",
    ++		       __LINE__, (unsigned long) i, str, tv[i].oid);
    ++	  return 1;
     +	}
    -+
     +    }
    ++  return 0;
     +}
     
    - ## grub-core/lib/libtasn1_wrap/tests/object-id-encoding.c (new) ##
    + ## grub-core/tests/asn1/object-id-encoding.c (new) ##
     @@
     +/*
     + * Copyright (C) 2019 Nikos Mavrogiannopoulos
    @@ grub-core/lib/libtasn1_wrap/tests/object-id-encoding.c (new)
     + *
     + */
     +
    -+#include <grub/libtasn1.h>
    -+#include <grub/types.h>
    -+#include <grub/misc.h>
    -+#include <grub/err.h>
    -+#include "../wrap_tests.h"
    ++#include "asn1_test.h"
     +
     +struct tv
     +{
    @@ grub-core/lib/libtasn1_wrap/tests/object-id-encoding.c (new)
     +   .expected_error = ASN1_SUCCESS},
     +};
     +
    -+void
    ++int
     +test_object_id_encoding(void)
     +{
     +  unsigned char der[128];
    @@ grub-core/lib/libtasn1_wrap/tests/object-id-encoding.c (new)
     +	{
     +	  if (ret == tv[i].expected_error)
     +	    continue;
    -+	  grub_fatal (
    -+		   "%d: iter %lu, encoding of OID failed: %s\n",
    -+		   __LINE__, (unsigned long) i, asn1_strerror(ret));
    -+	  return;
    ++	  grub_printf ("%d: iter %lu, encoding of OID failed: %s\n",
    ++		       __LINE__, (unsigned long) i, asn1_strerror(ret));
    ++	  return 1;
     +	}
     +      else if (ret != tv[i].expected_error)
     +        {
    -+	  grub_fatal (
    -+		   "%d: iter %lu, encoding of OID %s succeeded when expecting failure\n",
    -+		   __LINE__, (unsigned long) i, tv[i].oid);
    -+          return;
    ++	  grub_printf ("%d: iter %lu, encoding of OID %s succeeded when expecting failure\n",
    ++		       __LINE__, (unsigned long) i, tv[i].oid);
    ++          return 1;
     +        }
     +
     +      if (der_len != tv[i].der_len || grub_memcmp(der, tv[i].der, der_len) != 0)
     +	{
    -+	  grub_fatal (
    -+		   "%d: iter %lu, re-encoding of OID %s resulted to different string (%d vs %d bytes)\n",
    -+		   __LINE__, (unsigned long) i, tv[i].oid, der_len, tv[i].der_len);
    ++	  grub_printf ("%d: iter %lu, re-encoding of OID %s resulted to different string (%d vs %d bytes)\n",
    ++		       __LINE__, (unsigned long) i, tv[i].oid, der_len, tv[i].der_len);
     +
    -+	  return;
    ++	  return 1;
     +	}
     +    }
    ++  return 0;
     +}
     
    - ## grub-core/lib/libtasn1_wrap/tests/octet-string.c (new) ##
    + ## grub-core/tests/asn1/octet-string.c (new) ##
     @@
     +/*
     + * Copyright (C) 2011-2020 Free Software Foundation, Inc.
    @@ grub-core/lib/libtasn1_wrap/tests/octet-string.c (new)
     + *
     + */
     +
    -+#include <grub/libtasn1.h>
    -+#include <grub/err.h>
    -+#include <grub/mm.h>
    -+#include <grub/misc.h>
    -+#include "../wrap_tests.h"
    ++#include "asn1_test.h"
     +
     +
     +struct tv
    @@ grub-core/lib/libtasn1_wrap/tests/octet-string.c (new)
     +   }
     +};
     +
    -+void
    ++int
     +test_octet_string (void)
     +{
     +  unsigned char str[100];
    @@ grub-core/lib/libtasn1_wrap/tests/octet-string.c (new)
     +				sizeof (str), &str_size);
     +	  if (ret != tv[i].expected_error)
     +	    {
    -+	      grub_fatal (
    -+		       "%d: asn1_get_octet_der: %s: got %d expected %d\n",
    -+		       __LINE__, tv[i].name, ret,
    -+		       tv[i].expected_error);
    -+	      return;
    ++	      grub_printf ("%d: asn1_get_octet_der: %s: got %d expected %d\n",
    ++			   __LINE__, tv[i].name, ret, tv[i].expected_error);
    ++	      return 1;
     +	    }
     +	  if (tv[i].expected_error)
     +	    continue;
     +
     +	  if (ret_len != tv[i].der_len - 1)
     +	    {
    -+	      grub_fatal (
    -+		       "%d: error in DER, length returned is %d, had %d\n",
    -+		       __LINE__, ret_len, tv[i].der_len - 1);
    -+	      return;
    ++	      grub_printf ("%d: error in DER, length returned is %d, had %d\n",
    ++			   __LINE__, ret_len, tv[i].der_len - 1);
    ++	      return 1;
     +	    }
     +
     +	  if (str_size != tv[i].len
     +	      || grub_memcmp (tv[i].string, str, tv[i].len) != 0)
     +	    {
    -+	      grub_fatal (
    -+		       "%d: memcmp: %s: got invalid decoding\n",
    -+		       __LINE__, tv[i].name);
    ++	      grub_printf ("%d: memcmp: %s: got invalid decoding\n",
    ++			   __LINE__, tv[i].name);
     +
    -+              return;
    ++              return 1;
     +	    }
     +
     +	  /* Encode */
    @@ grub-core/lib/libtasn1_wrap/tests/octet-string.c (new)
     +	  if (der_len != tv[i].der_len - 1
     +	      || grub_memcmp (tv[i].der_str + 1, der, tv[i].der_len - 1) != 0)
     +	    {
    -+	      grub_fatal (
    -+		       "encoding: %s: got invalid encoding\n",
    -+		       tv[i].name);
    -+	      return;
    ++	      grub_printf ("encoding: %s: got invalid encoding\n", tv[i].name);
    ++	      return 1;
     +	    }
     +	}
     +
    @@ grub-core/lib/libtasn1_wrap/tests/octet-string.c (new)
     +				&tmp, (unsigned int*)&str_size, (unsigned int*)&der_len);
     +      if (ret != tv[i].expected_error)
     +	{
    -+	  grub_fatal (
    -+		   "%d: asn1_decode_simple_ber: %s: got %s expected %s\n",
    -+		   __LINE__, tv[i].name, asn1_strerror(ret), asn1_strerror(tv[i].expected_error));
    -+	  return;
    ++	  grub_printf ("%d: asn1_decode_simple_ber: %s: got %s expected %s\n",
    ++		       __LINE__, tv[i].name, asn1_strerror(ret),
    ++		       asn1_strerror(tv[i].expected_error));
    ++	  return 1;
     +	}
     +      if (tv[i].expected_error)
     +        continue;
     +
     +      if (der_len != tv[i].der_len)
     +	{
    -+	  grub_fatal (
    -+		   "%d: error: %s: DER, length returned is %d, had %d\n",
    -+		   __LINE__, tv[i].name, der_len, tv[i].der_len);
    -+	  return;
    ++	  grub_printf ("%d: error: %s: DER, length returned is %d, had %d\n",
    ++		       __LINE__, tv[i].name, der_len, tv[i].der_len);
    ++	  return 1;
     +	}
     +
     +      if (str_size != tv[i].len || grub_memcmp (tv[i].string, tmp, tv[i].len) != 0)
     +	{
    -+	  grub_fatal (
    -+		   "%d: memcmp: %s: got invalid decoding\n",
    -+		   __LINE__, tv[i].name);
    -+          return;
    ++	  grub_printf ("%d: memcmp: %s: got invalid decoding\n", __LINE__, tv[i].name);
    ++          return 1;
     +	}
     +      grub_free (tmp);
     +      tmp = NULL;
     +
     +    }
    ++  return 0;
     +}
     
    - ## grub-core/lib/libtasn1_wrap/tests/reproducers.c (new) ##
    + ## grub-core/tests/asn1/reproducers.c (new) ##
     @@
     +/*
     + * Copyright (C) 2019 Free Software Foundation, Inc.
    @@ grub-core/lib/libtasn1_wrap/tests/reproducers.c (new)
     +/* Description: run reproducers for several fixed issues        */
     +/****************************************************************/
     +
    -+#include <grub/libtasn1.h>
    -+#include <grub/err.h>
    -+#include <grub/mm.h>
    -+#include "../wrap_tests.h"
    ++#include "asn1_test.h"
     +
     +#define CONST_DOWN        (1U<<29)
     +
    @@ grub-core/lib/libtasn1_wrap/tests/reproducers.c (new)
     +{ NULL, 0, NULL }
     +};
     +
    -+void
    ++int
     +test_reproducers (void)
     +{
     +  int result;
    @@ grub-core/lib/libtasn1_wrap/tests/reproducers.c (new)
     +  result = asn1_array2tree (endless_asn1_tab, &definitions, errorDescription);
     +  if (result != ASN1_SUCCESS)
     +    {
    -+      grub_fatal ("Error: %s\nErrorDescription = %s\n\n",
    -+		  asn1_strerror (result), errorDescription);
    -+      return;
    ++      grub_printf ("Error: %s\nErrorDescription = %s\n\n",
    ++		   asn1_strerror (result), errorDescription);
    ++      return 1;
     +    }
     +
     +  asn1_delete_structure (&definitions);
    @@ grub-core/lib/libtasn1_wrap/tests/reproducers.c (new)
     +  result = asn1_array2tree (tab, &definitions, errorDescription);
     +  if (result != ASN1_SUCCESS)
     +    {
    -+      grub_fatal ("Error: %s\nErrorDescription = %s\n\n",
    -+		  asn1_strerror (result), errorDescription);
    -+      return;
    ++      grub_printf ("Error: %s\nErrorDescription = %s\n\n",
    ++		   asn1_strerror (result), errorDescription);
    ++      return 1;
     +    }
     +
     +  asn1_delete_structure (&definitions);
    -+}
    -
    - ## grub-core/lib/libtasn1_wrap/wrap_tests.c (new) ##
    -@@
    -+/*
    -+ *  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>
    -+#include <grub/command.h>
    -+#include <grub/mm.h>
    -+#include "wrap_tests.h"
    -+
    -+/*
    -+ * libtasn1 tests - from which this is derived - are provided under GPL3+.
    -+ */
    -+GRUB_MOD_LICENSE ("GPLv3+");
    -+
    -+static grub_command_t cmd;
    -+
    -+static grub_err_t
    -+grub_cmd_asn1test (grub_command_t cmdd __attribute__((unused)),
    -+		   int argc __attribute__((unused)),
    -+		   char **args __attribute__((unused)))
    -+{
    -+  grub_printf ("test_CVE_2018_1000654\n");
    -+  test_CVE_2018_1000654 ();
    -+
    -+  grub_printf ("test_object_id_decoding\n");
    -+  test_object_id_decoding ();
    -+
    -+  grub_printf ("test_object_id_encoding\n");
    -+  test_object_id_encoding ();
    -+
    -+  grub_printf ("test_octet_string\n");
    -+  test_octet_string ();
    -+
    -+  grub_printf ("test_overflow\n");
    -+  test_overflow ();
    -+
    -+  grub_printf ("test_reproducers\n");
    -+  test_overflow ();
    -+
    -+  grub_printf ("test_simple\n");
    -+  test_simple ();
    -+
    -+  grub_printf ("test_strings\n");
    -+  test_strings ();
    -+
    -+  grub_printf ("ASN.1 self-tests passed\n");
    -+
    -+  return GRUB_ERR_NONE;
    -+}
    -+
     +
    -+GRUB_MOD_INIT(test_asn1)
    -+{
    -+  cmd = grub_register_command ("test_asn1", grub_cmd_asn1test, NULL,
    -+			       "Run self-tests for the ASN.1 parser.");
    -+}
    -+
    -+GRUB_MOD_FINI(test_asn1)
    -+{
    -+  grub_unregister_command (cmd);
    ++  return 0;
     +}
     
    - ## grub-core/lib/libtasn1_wrap/wrap_tests.h (new) ##
    -@@
    -+/*
    -+ *  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
    + ## grub-core/tests/lib/functional_test.c ##
    +@@ grub-core/tests/lib/functional_test.c: grub_functional_all_tests (grub_extcmd_context_t ctxt __attribute__ ((unused)),
    +   grub_dl_load ("cmp_test");
    +   grub_dl_load ("mul_test");
    +   grub_dl_load ("shift_test");
    ++  grub_dl_load ("asn1_test");
    + 
    +   FOR_LIST_ELEMENTS (test, grub_test_list)
    +     ok = !grub_test_run (test) && ok;
     
    - ## tests/test_asn1.in (new) ##
    + ## tests/asn1_test.in (new) ##
     @@
     +#! @BUILD_SHEBANG@
     +set -e
     +
     +. "@builddir@/grub-core/modinfo.sh"
     +
    -+out=`echo test_asn1 | @builddir@/grub-shell`
    ++out=`echo functional_test asn1_test | @builddir@/grub-shell`
     +
    -+if [ "$(echo "$out" | tail -n 1)" != "ASN.1 self-tests passed" ]; then
    ++if [ "$(echo "$out" | tail -n 1)" != "ALL TESTS PASSED" ]; then
     +  echo "ASN.1 test failure: $out"
     +  exit 1
     +fi
 7:  1de592e66 !  7:  3f1368c63 libtasn1: Add the documentation
    @@ Commit message
         Also add the patches to make libtasn1 compatible with grub code.
     
         Signed-off-by: Gary Lin <[email protected]>
    +    Reviewed-by: Vladimir Serbinenko <[email protected]>
     
      ## docs/grub-dev.texi ##
     @@ docs/grub-dev.texi: cp minilzo-2.10/*.[hc] grub-core/lib/minilzo
    @@ docs/grub-dev.texi: cp minilzo-2.10/*.[hc] grub-core/lib/minilzo
     
      ## grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch (new) ##
     @@
    -+From c1c3459159d5d84e0d6da6eec6b86df5ccee1417 Mon Sep 17 00:00:00 2001
    ++From 210923227f4b366afa1104f62e82836587a8a753 Mon Sep 17 00:00:00 2001
     +From: Daniel Axtens <[email protected]>
     +Date: Fri, 1 May 2020 17:12:23 +1000
     +Subject: [PATCH 1/2] libtasn1: disable code not needed in grub
    @@ grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.pat
     +---
     + 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/element.c   |  6 +++---
     + 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(-)
    ++ 6 files changed, 39 insertions(+), 9 deletions(-)
     +
     +diff --git a/grub-core/lib/libtasn1/lib/coding.c b/grub-core/lib/libtasn1/lib/coding.c
    -+index ea5bc370e..841fe47a9 100644
    ++index ea5bc370e..5d03bca9d 100644
     +--- a/grub-core/lib/libtasn1/lib/coding.c
     ++++ b/grub-core/lib/libtasn1/lib/coding.c
     +@@ -30,11 +30,11 @@
    @@ -30,11 +30,11 @@
     + 
     + #define MAX_TAG_LEN 16
     + 
    -++#if 0
    +++#if 0 /* GRUB SKIPPED IMPORTING */
     + /******************************************************/
     + /* Function : _asn1_error_description_value_not_found */
     + /* Description: creates the ErrorDescription string   */
    @@ -244,6 +245,7 @@ asn1_encode_simple_der (unsigned int etype, const unsigned char
     +   return ASN1_SUCCESS;
     + }
     + 
    -++#if 0
    +++#if 0 /* GRUB SKIPPED IMPORTING */
     + /******************************************************/
     + /* Function : _asn1_time_der                          */
     + /* Description: creates the DER coding for a TIME     */
    @@ -519,6 +521,7 @@ asn1_bit_der (const unsigned char *str, int bit_len,
     + }
     + 
     + 
    -++#if 0
    +++#if 0 /* GRUB SKIPPED IMPORTING */
     + /******************************************************/
     + /* Function : _asn1_complete_explicit_tag             */
     + /* Description: add the length coding to the EXPLICIT */
    @@ -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
    +++#if 0 /* GRUB SKIPPED IMPORTING */
     + /******************************************************/
     + /* Function : _asn1_insert_tag_der                    */
     + /* Description: creates the DER coding of tags of one */
    @@ -1423,3 +1429,5 @@ error:
     + }
     ++
     ++#endif
    -+\ No newline at end of file
     +diff --git a/grub-core/lib/libtasn1/lib/decoding.c b/grub-core/lib/libtasn1/lib/decoding.c
    -+index b9245c486..92fc87c23 100644
    ++index b9245c486..bf9cb13ac 100644
     +--- a/grub-core/lib/libtasn1/lib/decoding.c
     ++++ b/grub-core/lib/libtasn1/lib/decoding.c
     +@@ -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
    +++#if 0 /* GRUB SKIPPED IMPORTING */
     + /**
     +  * asn1_der_decoding_element:
     +  * @structure: pointer to an ASN1 structure
    @@ -1650,6 +1651,7 @@ asn1_der_decoding_element (asn1_node * structure, const char
     + /**
     +  * asn1_der_decoding_startEnd:
     +diff --git a/grub-core/lib/libtasn1/lib/element.c b/grub-core/lib/libtasn1/lib/element.c
    -+index d4c558e10..5c7941e53 100644
    ++index d4c558e10..bc4c3c8d7 100644
     +--- a/grub-core/lib/libtasn1/lib/element.c
     ++++ b/grub-core/lib/libtasn1/lib/element.c
    ++@@ -118,7 +118,7 @@ _asn1_convert_integer (const unsigned char *value, unsigned char *value_out,
    ++ 	value_out[k2 - k] = val[k2];
    ++     }
    ++ 
    ++-#if 0
    +++#if 0 /* GRUB SKIPPED IMPORTING */
    ++   printf ("_asn1_convert_integer: valueIn=%s, lenOut=%d", value, *len);
    ++   for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT; k++)
    ++     printf (", vOut[%d]=%d", k, value_out[k]);
     +@@ -191,7 +191,7 @@ _asn1_append_sequence_set (asn1_node node, struct node_tail_cache_st *pcache)
     +   return ASN1_SUCCESS;
     + }
    @@ -646,7 +646,7 @@ asn1_write_value (asn1_node node_root, const char *name,
     + #define PUT_VALUE( ptr, ptr_size, data, data_size) \
     + 	*len = data_size; \
     +diff --git a/grub-core/lib/libtasn1/lib/errors.c b/grub-core/lib/libtasn1/lib/errors.c
    -+index aef5dfe6f..0175ddedb 100644
    ++index aef5dfe6f..2b2322152 100644
     +--- a/grub-core/lib/libtasn1/lib/errors.c
     ++++ b/grub-core/lib/libtasn1/lib/errors.c
     +@@ -57,6 +57,8 @@ static const libtasn1_error_entry error_algorithms[] = {
    @@ -57,6 +57,8 @@ static const libtasn1_error_entry error_algorithms[] = {
     + };
     + 
     ++
    -++#if 0
    +++#if 0 /* GRUB SKIPPED IMPORTING */
     + /**
     +  * asn1_perror:
     +  * @error: is an error returned by a libtasn1 function.
    @@ -73,6 +75,7 @@ asn1_perror (int error)
     + /**
     +  * asn1_strerror:
     +diff --git a/grub-core/lib/libtasn1/lib/structure.c b/grub-core/lib/libtasn1/lib/structure.c
    -+index 512dd601f..3e1e35ba5 100644
    ++index 512dd601f..f5a947d57 100644
     +--- a/grub-core/lib/libtasn1/lib/structure.c
     ++++ b/grub-core/lib/libtasn1/lib/structure.c
     +@@ -76,7 +76,7 @@ _asn1_find_left (asn1_node_const node)
    @@ -76,7 +76,7 @@ _asn1_find_left (asn1_node_const node)
     + }
     + 
     +-
    -++#if 0
    +++#if 0 /* GRUB SKIPPED IMPORTING */
     + int
     + _asn1_create_static_structure (asn1_node_const pointer,
     + 			       char *output_file_name, char *vector_name)
    @@ -721,7 +721,7 @@ asn1_create_element (asn1_node_const definitions, const char *s
     + }
     + 
     +-
    -++#if 0
    +++#if 0 /* GRUB SKIPPED IMPORTING */
     + /**
     +  * asn1_print_structure:
     +  * @out: pointer to the output file (e.g. stdout).
    @@ -1158,6 +1158,7 @@ asn1_find_structure_from_oid (asn1_node_const definitions,
     +   return NULL;			/* ASN1_ELEMENT_NOT_FOUND; */
     + }
     + 
    -++#if 0
    +++#if 0 /* GRUB SKIPPED IMPORTING */
     + /**
     +  * asn1_copy_node:
     +  * @dst: Destination asn1 node.
    @@ -1207,6 +1208,7 @@ asn1_copy_node (asn1_node dst, const char *dst_name,
     + /**
     +  * asn1_dup_node:
     +diff --git a/include/grub/libtasn1.h b/include/grub/libtasn1.h
    -+index 51cc7879f..507e0679e 100644
    ++index 51cc7879f..058ab27b0 100644
     +--- a/include/grub/libtasn1.h
     ++++ b/include/grub/libtasn1.h
     +@@ -318,6 +318,8 @@ extern "C"
    @@ -318,6 +318,8 @@ extern "C"
     + /***********************************/
     + 
     ++/* These functions are not used in grub and should not be referenced. */
    -++# if 0
    +++# if 0 /* GRUB SKIPPED IMPORTING */
     +   extern ASN1_API int
     +     asn1_parser2tree (const char *file,
     + 		      asn1_node * definitions, char *error_desc);
    @@ -326,14 +328,17 @@ extern "C"
     +     asn1_array2tree (const asn1_static_node * array,
     + 		     asn1_node * definitions, char *errorDescription);
     + 
    -++# if 0
    +++# if 0 /* GRUB SKIPPED IMPORTING */
     +   extern ASN1_API void
     +     asn1_print_structure (FILE * out, asn1_node_const structure,
     + 			  const char *name, int mode);
    @@ -347,9 +352,11 @@ extern "C"
     +   extern ASN1_API int
     +     asn1_delete_element (asn1_node structure, const char *element_name);
     + 
    -++# if 0
    +++# if 0 /* GRUB SKIPPED IMPORTING */
     +   extern ASN1_API int
     +     asn1_write_value (asn1_node node_root, const char *name,
     + 		      const void *ivalue, int len);
    @@ -366,9 +373,11 @@ extern "C"
     +     asn1_number_of_elements (asn1_node_const element, const char *name,
     + 			     int *num);
     + 
    -++# if 0
    +++# if 0 /* GRUB SKIPPED IMPORTING */
     +   extern ASN1_API int
     +     asn1_der_coding (asn1_node_const element, const char *name,
     + 		     void *ider, int *len, char *ErrorDescription);
    @@ -379,6 +388,7 @@ extern "C"
     +     asn1_der_decoding (asn1_node * element, const void *ider,
     + 		       int ider_len, char *errorDescription);
     + 
    -++# if 0
    +++# if 0 /* GRUB SKIPPED IMPORTING */
     + /* Do not use. Use asn1_der_decoding() instead. */
     +   extern ASN1_API int
     +     asn1_der_decoding_element (asn1_node * structure,
    @@ -411,12 +422,16 @@ extern "C"
     + 							    const char
     + 							    *oidValue);
     + 
    -++# if 0
    +++# if 0 /* GRUB SKIPPED IMPORTING */
     +     __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
    +++# if 0 /* GRUB SKIPPED IMPORTING */
     +   extern ASN1_API void asn1_perror (int error);
     ++# endif
     + 
    @@ -411,12 +422,16 @@ extern "C"
     
      ## grub-core/lib/libtasn1-patches/0002-libtasn1-changes-for-grub-compatibility.patch (new) ##
     @@
    -+From 5d84feb27c073056739514bc0cb578fdf7cc5a90 Mon Sep 17 00:00:00 2001
    ++From 6ace81543a3c716a15ac92e825e90a5f949b9faf Mon Sep 17 00:00:00 2001
     +From: Daniel Axtens <[email protected]>
     +Date: Fri, 1 May 2020 20:44:29 +1000
     +Subject: [PATCH 2/2] libtasn1: changes for grub compatibility
     +
     +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.
    ++ - remove _asn1_strcat and replace strcat with the bound-checked
    ++   _asn1_str_cat except the one inside _asn1_str_cat. That strcat is
    ++   replaced with strcpy.
     +
    -+ - 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 in libtasn1.h
     +
     + - adjust header paths to "grub/libtasn1.h".
     +
    @@ grub-core/lib/libtasn1-patches/0002-libtasn1-changes-for-grub-compatibility.patc
     +
     +Signed-off-by: Daniel Axtens <[email protected]>
     +Signed-off-by: Gary Lin <[email protected]>
    ++
    ++libtasn1: get rid of strcat
    ++
    ++- Replace strcat and memcpy in _asn1_str_cat with strcpy and strncat
    ++- Replace strcat with the bound-checking _asn1_str_cat
    ++- Remove _asn1_strcat
    ++
    ++Signed-off-by: Gary Lin <[email protected]>
     +---
    -+ 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   | 8 ++++----
    ++ grub-core/lib/libtasn1/lib/element.c    | 2 +-
    ++ grub-core/lib/libtasn1/lib/gstr.c       | 2 +-
    ++ grub-core/lib/libtasn1/lib/int.h        | 3 +--
    ++ grub-core/lib/libtasn1/lib/parser_aux.c | 2 +-
    ++ include/grub/libtasn1.h                 | 5 ++---
    ++ 6 files changed, 10 insertions(+), 12 deletions(-)
     +
     +diff --git a/grub-core/lib/libtasn1/lib/decoding.c b/grub-core/lib/libtasn1/lib/decoding.c
    -+index 92fc87c23..4fd5f0ce6 100644
    ++index bf9cb13ac..51859fe36 100644
     +--- a/grub-core/lib/libtasn1/lib/decoding.c
     ++++ b/grub-core/lib/libtasn1/lib/decoding.c
    -+@@ -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,
    ++@@ -2016,8 +2016,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);
    +++	  _asn1_str_cat (name, sizeof (name), ".");
    +++	  _asn1_str_cat (name, sizeof (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,
    ++@@ -2034,8 +2034,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);
    +++		  _asn1_str_cat (name, sizeof (name), ".");
    +++		  _asn1_str_cat (name, sizeof (name), p2->name);
     + 
     + 		  result = asn1_create_element (definitions, name, &aux);
     + 		  if (result == ASN1_SUCCESS)
     +diff --git a/grub-core/lib/libtasn1/lib/element.c b/grub-core/lib/libtasn1/lib/element.c
    -+index 5c7941e53..822a1b6ab 100644
    ++index bc4c3c8d7..8694fecb9 100644
     +--- a/grub-core/lib/libtasn1/lib/element.c
     ++++ b/grub-core/lib/libtasn1/lib/element.c
    -+@@ -30,9 +30,10 @@
    -+ #include "parser_aux.h"
    -+ #include <gstr.h>
    -+ #include "structure.h"
    -+-#include "c-ctype.h"
    -+ #include "element.h"
    ++@@ -688,7 +688,7 @@ asn1_write_value (asn1_node node_root, const char *name,
    ++                 return ASN1_MEM_ERROR; \
    ++         } else { \
    ++                 /* this strcat is checked */ \
    ++-                if (ptr) _asn1_strcat (ptr, data); \
    +++                if (ptr) _asn1_str_cat ((char *)ptr, ptr_size, (const char *)data); \
    ++         }
     + 
    -++#define c_isdigit grub_isdigit
    -++
    -+ void
    -+ _asn1_hierarchical_name (asn1_node_const node, char *name, int name_size)
    -+ {
    ++ /**
     +diff --git a/grub-core/lib/libtasn1/lib/gstr.c b/grub-core/lib/libtasn1/lib/gstr.c
    -+index eef419554..bc507d3d8 100644
    ++index eef419554..a9c16f5d3 100644
     +--- a/grub-core/lib/libtasn1/lib/gstr.c
     ++++ b/grub-core/lib/libtasn1/lib/gstr.c
    -+@@ -36,13 +36,13 @@ _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
    ++@@ -36,7 +36,7 @@ _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);
    +++      strcpy (dest + dest_size, 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;
    -+ 	}
    -+     }
     +diff --git a/grub-core/lib/libtasn1/lib/int.h b/grub-core/lib/libtasn1/lib/int.h
    -+index d94d51c8c..07dd669ac 100644
    ++index d94d51c8c..7409c7655 100644
     +--- a/grub-core/lib/libtasn1/lib/int.h
     ++++ b/grub-core/lib/libtasn1/lib/int.h
     +@@ -35,7 +35,7 @@
    @@ -35,7 +35,7 @@
     + 
     + # define ASN1_SMALL_VALUE_SIZE 16
     + 
    -+@@ -115,7 +115,7 @@ extern const tag_and_class_st _asn1_tags[];
    ++@@ -115,7 +115,6 @@ 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)
     +diff --git a/grub-core/lib/libtasn1/lib/parser_aux.c b/grub-core/lib/libtasn1/lib/parser_aux.c
    -+index c05bd2339..8bdc42ae3 100644
    ++index c05bd2339..e4e4c0556 100644
     +--- a/grub-core/lib/libtasn1/lib/parser_aux.c
     ++++ b/grub-core/lib/libtasn1/lib/parser_aux.c
    -+@@ -26,7 +26,8 @@
    -+ #include "gstr.h"
    -+ #include "structure.h"
    -+ #include "element.h"
    -+-#include "c-ctype.h"
    -++
    -++#define c_isdigit grub_isdigit
    -+ 
    -+ char _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])
    ++@@ -632,7 +632,7 @@ _asn1_ltostr (int64_t v, char str[LTOSTR_MAX_SIZE])
     +   count = 0;
     +   do
     +     {
    @@ -632,7 +633,7 @@ _asn1_ltostr (int64_t v, char str[LTOSTR_MAX_SIZE])
     +       temp[start + count] = '0' + (char) r;
     +       count++;
     +diff --git a/include/grub/libtasn1.h b/include/grub/libtasn1.h
    -+index 507e0679e..201089c11 100644
    ++index 058ab27b0..7d64b6ab7 100644
     +--- a/include/grub/libtasn1.h
     ++++ b/include/grub/libtasn1.h
    -+@@ -34,29 +34,16 @@
    -+ #ifndef LIBTASN1_H
    -+ # define LIBTASN1_H
    -+ 
    -+-# ifndef ASN1_API
    -+-#  if defined ASN1_BUILDING && defined HAVE_VISIBILITY && HAVE_VISIBILITY
    -+-#   define ASN1_API __attribute__((__visibility__("default")))
    -+-#  elif defined ASN1_BUILDING && defined _MSC_VER && ! defined ASN1_STATIC
    -+-#   define ASN1_API __declspec(dllexport)
    -+-#  elif defined _MSC_VER && ! defined ASN1_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
    ++@@ -54,9 +54,8 @@
    ++ #  define __LIBTASN1_PURE__
    ++ # endif
     + 
     +-# include <sys/types.h>
     +-# include <time.h>
 8:  f9e763584 !  8:  b9282e8db protectors: Add key protectors framework
    @@ Metadata
     Author: Hernan Gatta <[email protected]>
     
      ## Commit message ##
    -    protectors: Add key protectors framework
    +    key_protector: Add key protectors framework
     
         A key protector encapsulates functionality to retrieve an unlocking key
         for a fully-encrypted disk from a specific source. A key protector
    @@ Commit message
         name exists and if an unlocking key is successfully retrieved by it, the
         function returns to the caller the retrieved key and its length.
     
    +    Cc: Vladimir Serbinenko <[email protected]>
         Signed-off-by: Hernan Gatta <[email protected]>
         Signed-off-by: Gary Lin <[email protected]>
     
    @@ grub-core/Makefile.am: 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/key_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 ##
    -@@ grub-core/Makefile.core.def: 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/Makefile.core.def: module = {
    +   common = disk/raid6_recover.c;
    + };
    + 
    ++module = {
    ++  name = key_protector;
    ++  common = disk/key_protector.c;
    ++};
    ++
    + module = {
    +   name = scsi;
    +   common = disk/scsi.c;
     
    - ## grub-core/kern/protectors.c (new) ##
    + ## grub-core/disk/key_protector.c (new) ##
     @@
     +/*
     + *  GRUB  --  GRand Unified Bootloader
    @@ grub-core/kern/protectors.c (new)
     + *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
     + */
     +
    ++#include <grub/dl.h>
     +#include <grub/list.h>
     +#include <grub/misc.h>
     +#include <grub/mm.h>
    -+#include <grub/protector.h>
    ++#include <grub/key_protector.h>
    ++
    ++GRUB_MOD_LICENSE ("GPLv3+");
     +
     +struct grub_key_protector *grub_key_protectors = NULL;
     +
    @@ grub-core/kern/protectors.c (new)
     +  return kp->recover_key (key, key_size);
     +}
     
    - ## include/grub/protector.h (new) ##
    + ## include/grub/key_protector.h (new) ##
     @@
     +/*
     + *  GRUB  --  GRand Unified Bootloader
    @@ include/grub/protector.h (new)
     +  grub_err_t (*recover_key) (grub_uint8_t **key, grub_size_t *key_size);
     +};
     +
    -+extern struct grub_key_protector *EXPORT_VAR (grub_key_protectors);
    -+
     +grub_err_t
    -+EXPORT_FUNC (grub_key_protector_register) (struct grub_key_protector *protector);
    ++grub_key_protector_register (struct grub_key_protector *protector);
     +
     +grub_err_t
    -+EXPORT_FUNC (grub_key_protector_unregister) (struct grub_key_protector *protector);
    ++grub_key_protector_unregister (struct grub_key_protector *protector);
     +
     +grub_err_t
    -+EXPORT_FUNC (grub_key_protector_recover_key) (const char *protector,
    -+					      grub_uint8_t **key,
    -+					      grub_size_t *key_size);
    ++grub_key_protector_recover_key (const char *protector,
    ++				grub_uint8_t **key,
    ++				grub_size_t *key_size);
     +
     +#endif /* ! GRUB_PROTECTOR_HEADER */
 9:  675137af0 =  9:  ad404454c tpm2: Add TPM Software Stack (TSS)
10:  62866697f ! 10:  5602679b9 protectors: Add TPM2 Key Protector

@@ Metadata
Author: Hernan Gatta <[email protected]>

## Commit message ##
- protectors: Add TPM2 Key Protector
+ key_protector: Add TPM2 Key Protector

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.
@@ grub-core/tpm2/module.c (new)
+#include <grub/list.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
-+#include <grub/protector.h>
++#include <grub/key_protector.h>
+#include <grub/tpm2/buffer.h>
+#include <grub/tpm2/internal/args.h>
+#include <grub/tpm2/internal/types.h>
11: 526e31f28 ! 11: b280029ad cryptodisk: Support key protectors
@@ Commit message

## Makefile.util.def ##
@@ Makefile.util.def: 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;
+ common = grub-core/disk/luks2.c;
+ common = grub-core/disk/geli.c;
++ common = grub-core/disk/key_protector.c;
+ common = grub-core/disk/cryptodisk.c;
+ common = grub-core/disk/AFSplitter.c;
+ common = grub-core/lib/pbkdf2.c;

## grub-core/disk/cryptodisk.c ##
@@
#include <grub/file.h>
#include <grub/procfs.h>
#include <grub/partition.h>
-+#include <grub/protector.h>
++#include <grub/key_protector.h>

#ifdef GRUB_UTIL
#include <grub/emu/hostdisk.h>
12: b5a5f3648 ! 12: 1cceb47a6 util/grub-protect: Add new tool

    @@ Makefile.util.def: program = {
     +  ldadd = '$(LIBTASN1)';
     +  ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
     +
    -+  enable = efi;
    ++  condition = COND_GRUB_PROTECT;
     +};
     +
      program = {
    @@ configure.ac: fi
      AC_SUBST([LIBZFS])
      AC_SUBST([LIBNVPAIR])
      
    ++AC_ARG_ENABLE([grub-protect],
    ++	      [AS_HELP_STRING([--enable-grub-protect],
    ++                             [build and install the `grub-protect' utility (default=guessed)])])
    ++if test x"$enable_grub_protect" = xno ; then
    ++  grub_protect_excuse="explicitly disabled"
    ++fi
    ++
     +LIBTASN1=
    -+if test x"$platform" = xefi; then
    -+  AC_CHECK_LIB([tasn1], [asn1_write_value], [],
    -+               [AC_MSG_ERROR([Your platform requires libtasn1])])
    -+  LIBTASN1="-ltasn1"
    ++if test x"$grub_protect_excuse" = x ; then
    ++  AC_CHECK_LIB([tasn1], [asn1_write_value], [LIBTASN1="-ltasn1"], [grub_protect_excuse="need libtasn1 library"])
     +fi
     +AC_SUBST([LIBTASN1])
    ++
    ++if test x"$enable_grub_protect" = xyes && test x"$grub_protect_excuse" != x ; then
    ++  AC_MSG_ERROR([grub-protect was explicitly requested but can't be compiled ($grub_protect_excuse)])
    ++fi
    ++if test x"$grub_protect_excuse" = x ; then
    ++enable_grub_protect=yes
    ++else
    ++enable_grub_protect=no
    ++fi
    ++AC_SUBST([enable_grub_protect])
     +
      LIBS=""
      
      AC_SUBST([FONT_SOURCE])
    +@@ configure.ac: AM_CONDITIONAL([COND_GRUB_EMU_SDL], [test x$enable_grub_emu_sdl = xyes])
    + AM_CONDITIONAL([COND_GRUB_EMU_PCI], [test x$enable_grub_emu_pci = xyes])
    + AM_CONDITIONAL([COND_GRUB_MKFONT], [test x$enable_grub_mkfont = xyes])
    + AM_CONDITIONAL([COND_GRUB_MOUNT], [test x$enable_grub_mount = xyes])
    ++AM_CONDITIONAL([COND_GRUB_PROTECT], [test x$enable_grub_protect = xyes])
    + AM_CONDITIONAL([COND_HAVE_FONT_SOURCE], [test x$FONT_SOURCE != x])
    + if test x$FONT_SOURCE != x ; then
    +    HAVE_FONT_SOURCE=1
    +@@ configure.ac: echo grub-mount: Yes
    + else
    + echo grub-mount: No "($grub_mount_excuse)"
    + fi
    ++if [ x"$grub_protect_excuse" = x ]; then
    ++echo grub-protect: Yes
    ++else
    ++echo grub-protect: No "($grub_protect_excuse)"
    ++fi
    + if [ x"$starfield_excuse" = x ]; then
    + echo starfield theme: Yes
    + echo With DejaVuSans font from $DJVU_FONT_SOURCE
     
      ## util/grub-protect.c (new) ##
     @@
13:  3258119bb = 13:  7aa196b17 tpm2: Add TPM2 types, structures, and command constants
14:  369fba234 = 14:  843a0cb76 tpm2: Add more marshal/unmarshal functions
15:  dfa4668f2 = 15:  043c77591 tpm2: Implement more TPM2 commands
16:  0c47dd034 = 16:  9e1565125 tpm2: Support authorized policy
17:  3f07bcb61 ! 17:  cb501a8a6 protectors: Implement NV index

@@ Metadata
Author: Patrick Colp <[email protected]>

## Commit message ##
- protectors: Implement NV index
+ tpm2: Implement NV index

Currently with the TPM2 protector, only SRK mode is supported and NV index support is just a stub. Implement the NV index option.

18:  af122d6af = 18:  f360a6676 cryptodisk: Fallback to passphrase
19:  8ef3bab8c = 19:  60058fd98 cryptodisk: wipe out the cached keys from protectors
20:  4e1a6ead5 = 20:  25709b60c diskfilter: look up cryptodisk devices first
21:  59df654a0 = 21:  35854d3cd tpm2: Enable tpm2 module for grub-emu
22:  8b8c69b95 ! 22:  626435733 tests: Add tpm2_test
    @@ Commit message
     
      ## Makefile.util.def ##
     @@ Makefile.util.def: script = {
    -   common = tests/test_asn1.in;
    +   common = tests/asn1_test.in;
      };
      
     +script = {
    @@ tests/tpm2_test.in (new)
     +EOF
     +
     +# Test TPM unsealing with the same PCR
    -+${grubshell} --timeout=$timeout --grub-emu-opts="-t ${tpm2dev}" ${tpm2testdir}/testcase.cfg > ${testoutput} || ret=$?
    ++${grubshell} --timeout=$timeout --grub-emu-opts="-t ${tpm2dev}" < ${tpm2testdir}/testcase.cfg > ${testoutput} || ret=$?
     +
     +if [ "$ret" -eq 0 ]; then
     +    if ! grep -q "^${vtext}$" "$testoutput"; then
Signature · 7 lines
2.35.3


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

PATCH v9 1/22 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.

- Implement c-ctype.h and the functions defined in the header.

- Implement strncat in string.h.

---
 grub-core/lib/posix_wrap/c-ctype.h   | 114 +++++++++++++++++++++++++++
 grub-core/lib/posix_wrap/limits.h    |   1 +
 grub-core/lib/posix_wrap/stdlib.h    |   8 ++
 grub-core/lib/posix_wrap/string.h    |  21 +++++
 grub-core/lib/posix_wrap/sys/types.h |   1 +
 5 files changed, 145 insertions(+)
 create mode 100644 grub-core/lib/posix_wrap/c-ctype.h
grub-core/lib/posix_wrap/c-ctype.h new file +114 -0
new file mode 100644
index 000000000..5f8fc8ce3
@@ -0,0 +1,114 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2024  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_POSIX_C_CTYPE_H
+#define GRUB_POSIX_C_CTYPE_H	1
+
+#include <grub/misc.h>
+
+static inline bool
+c_isspace (int c)
+{
+  return !!grub_isspace (c);
+}
+
+static inline bool
+c_isdigit (int c)
+{
+  return !!grub_isdigit (c);
+}
+
+static inline bool
+c_islower (int c)
+{
+  return !!grub_islower (c);
+}
+
+static inline bool
+c_isascii (int c)
+{
+  return !(c & ~0x7f);
+}
+
+static inline bool
+c_isupper (int c)
+{
+  return !!grub_isupper (c);
+}
+
+static inline bool
+c_isxdigit (int c)
+{
+  return !!grub_isxdigit (c);
+}
+
+static inline bool
+c_isprint (int c)
+{
+  return !!grub_isprint (c);
+}
+
+static inline bool
+c_iscntrl (int c)
+{
+  return !grub_isprint (c);
+}
+
+static inline bool
+c_isgraph (int c)
+{
+  return grub_isprint (c) && !grub_isspace (c);
+}
+
+static inline bool
+c_isalnum (int c)
+{
+  return grub_isalpha (c) || grub_isdigit (c);
+}
+
+static inline bool
+c_ispunct (int c)
+{
+  return grub_isprint (c) && !grub_isspace (c) && !c_isalnum (c);
+}
+
+static inline bool
+c_isalpha (int c)
+{
+  return !!grub_isalpha (c);
+}
+
+static inline bool
+c_isblank (int c)
+{
+  return c == ' ' || c == '\t';
+}
+
+static inline int
+c_tolower (int c)
+{
+  return grub_tolower (c);
+}
+
+static inline int
+c_toupper (int c)
+{
+  return grub_toupper (c);
+}
+
+#endif
grub-core/lib/posix_wrap/limits.h +1 -0
index 26918c8a0..4be7b4080 100644
@@ -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
index f5279756a..14e4efdd0 100644
@@ -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/string.h +21 -0
index 1adb450b5..b0c5928d2 100644
@@ -84,6 +84,27 @@ memchr (const void *s, int c, grub_size_t n)
   return grub_memchr (s, c, n);
 }
 
+static inline char *
+strncat(char *dest, const char *src, grub_size_t n)
+{
+  const char *end;
+  char *str = dest;
+  grub_size_t src_len;
+
+  dest += grub_strlen (dest);
+
+  end = grub_memchr (src, '\0', n);
+  if (end != NULL)
+    src_len = (grub_size_t) (end - src);
+  else
+    src_len = n;
+
+  dest[src_len] = '\0';
+  grub_memcpy (dest, src, src_len);
+
+  return str;
+}
+
 #define memcmp grub_memcmp
 #define memcpy grub_memcpy
 #define memmove grub_memmove
grub-core/lib/posix_wrap/sys/types.h +1 -0
index eeda543c4..2f3e86549 100644
@@ -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 · 7 lines
2.35.3


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

PATCH v9 2/22 libtasn1: import libtasn1-4.19.0

In reply to Gary Lin via Grub-devel

From: 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
new file mode 100644
index 000000000..e8b3628db
@@ -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 · 7 lines
2.35.3


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

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

#3 lore raw

PATCH v9 3/22 libtasn1: disable code not needed in grub

In reply to Gary Lin via Grub-devel

From: 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   |  6 +++---
 grub-core/lib/libtasn1/lib/errors.c    |  3 +++
 grub-core/lib/libtasn1/lib/structure.c | 10 ++++++----
 include/grub/libtasn1.h                | 15 +++++++++++++++
 6 files changed, 39 insertions(+), 9 deletions(-)
grub-core/lib/libtasn1/lib/coding.c +10 -2
index ea5bc370e..5d03bca9d 100644
@@ -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 /* GRUB SKIPPED IMPORTING */
 /******************************************************/
 /* 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 /* GRUB SKIPPED IMPORTING */
 /******************************************************/
 /* 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 /* GRUB SKIPPED IMPORTING */
 /******************************************************/
 /* 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 /* GRUB SKIPPED IMPORTING */
 /******************************************************/
 /* 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
index b9245c486..bf9cb13ac 100644
@@ -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 /* GRUB SKIPPED IMPORTING */
 /**
  * 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 +3 -3
index d4c558e10..bc4c3c8d7 100644
@@ -118,7 +118,7 @@ _asn1_convert_integer (const unsigned char *value, unsigned char *value_out,
 	value_out[k2 - k] = val[k2];
     }
 
-#if 0
+#if 0 /* GRUB SKIPPED IMPORTING */
   printf ("_asn1_convert_integer: valueIn=%s, lenOut=%d", value, *len);
   for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT; k++)
     printf (", vOut[%d]=%d", k, value_out[k]);
@@ -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
index aef5dfe6f..2b2322152 100644
@@ -57,6 +57,8 @@ static const libtasn1_error_entry error_algorithms[] = {
   {0, 0}
 };
 
+
+#if 0 /* GRUB SKIPPED IMPORTING */
 /**
  * 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
index 512dd601f..f5a947d57 100644
@@ -76,7 +76,7 @@ _asn1_find_left (asn1_node_const node)
   return node->left;
 }
 
-
+#if 0 /* GRUB SKIPPED IMPORTING */
 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 /* GRUB SKIPPED IMPORTING */
 /**
  * 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 /* GRUB SKIPPED IMPORTING */
 /**
  * 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
index 51cc7879f..058ab27b0 100644
@@ -318,6 +318,8 @@ extern "C"
 /*  Functions definitions          */
 /***********************************/
 
+/* These functions are not used in grub and should not be referenced. */
+# if 0 /* GRUB SKIPPED IMPORTING */
   extern ASN1_API int
     asn1_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 int
     asn1_array2tree (const asn1_static_node * array,
 		     asn1_node * definitions, char *errorDescription);
 
+# if 0 /* GRUB SKIPPED IMPORTING */
   extern ASN1_API void
     asn1_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 int
     asn1_delete_element (asn1_node structure, const char *element_name);
 
+# if 0 /* GRUB SKIPPED IMPORTING */
   extern ASN1_API int
     asn1_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 /* GRUB SKIPPED IMPORTING */
   extern ASN1_API int
     asn1_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 /* GRUB SKIPPED IMPORTING */
 /* 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 /* GRUB SKIPPED IMPORTING */
     __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 /* GRUB SKIPPED IMPORTING */
   extern ASN1_API void asn1_perror (int error);
+# endif
 
 # define ASN1_MAX_TAG_SIZE 4
 # define ASN1_MAX_LENGTH_SIZE 9
Signature · 7 lines
2.35.3


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

PATCH v9 4/22 libtasn1: changes for grub compatibility

In reply to Gary Lin via Grub-devel

From: Daniel Axtens <[email protected]>

Do a few things to make libtasn1 compile as part of grub:

- remove _asn1_strcat and replace strcat with the bound-checked _asn1_str_cat except the one inside _asn1_str_cat. That strcat is replaced with strcpy.

- adjust header paths in libtasn1.h

- 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   | 8 ++++----
 grub-core/lib/libtasn1/lib/element.c    | 2 +-
 grub-core/lib/libtasn1/lib/gstr.c       | 2 +-
 grub-core/lib/libtasn1/lib/int.h        | 3 +--
 grub-core/lib/libtasn1/lib/parser_aux.c | 2 +-
 include/grub/libtasn1.h                 | 5 ++---
 6 files changed, 10 insertions(+), 12 deletions(-)
grub-core/lib/libtasn1/lib/decoding.c +4 -4
index bf9cb13ac..51859fe36 100644
@@ -2016,8 +2016,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_str_cat (name, sizeof (name), ".");
+	  _asn1_str_cat (name, sizeof (name), p2->name);
 
 	  len = sizeof (value);
 	  result = asn1_read_value (definitions, name, value, &len);
@@ -2034,8 +2034,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_str_cat (name, sizeof (name), ".");
+		  _asn1_str_cat (name, sizeof (name), p2->name);
 
 		  result = asn1_create_element (definitions, name, &aux);
 		  if (result == ASN1_SUCCESS)
grub-core/lib/libtasn1/lib/element.c +1 -1
index bc4c3c8d7..8694fecb9 100644
@@ -688,7 +688,7 @@ asn1_write_value (asn1_node node_root, const char *name,
                 return ASN1_MEM_ERROR; \
         } else { \
                 /* this strcat is checked */ \
-                if (ptr) _asn1_strcat (ptr, data); \
+                if (ptr) _asn1_str_cat ((char *)ptr, ptr_size, (const char *)data); \
         }
 
 /**
grub-core/lib/libtasn1/lib/gstr.c +1 -1
index eef419554..a9c16f5d3 100644
@@ -36,7 +36,7 @@ _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
 
   if (dest_tot_size - dest_size > str_size)
     {
-      strcat (dest, src);
+      strcpy (dest + dest_size, src);
     }
   else
     {
grub-core/lib/libtasn1/lib/int.h +1 -2
index d94d51c8c..7409c7655 100644
@@ -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,6 @@ 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)
 
 # 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 +1 -1
index c05bd2339..e4e4c0556 100644
@@ -632,7 +632,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 +2 -3
index 058ab27b0..7d64b6ab7 100644
@@ -54,9 +54,8 @@
 #  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 · 7 lines
2.35.3


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

PATCH v9 5/22 libtasn1: compile into asn1 module

In reply to Gary Lin via Grub-devel

From: 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
index 1571421d7..b1294e0f7 100644
@@ -2595,3 +2595,18 @@ module = {
   efi = commands/bli.c;
   enable = efi;
 };
+
+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
new file mode 100644
index 000000000..622ba942e
@@ -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 · 7 lines
2.35.3


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

PATCH v9 6/22 asn1_test: test module for libtasn1

In reply to Gary Lin via Grub-devel

From: Daniel Axtens <[email protected]>

Import tests from libtasn1 that don't use functionality we don't import. This test module is integrated into functional_test so that the user can run the test in grub shell.

This doesn't test the full decoder but that will be exercised in test suites for coming patch sets.

Add testcase target in accordance with
5e10be48e5 tests: Add check-native and check-nonnative make targets

---
 Makefile.util.def                             |   6 +
 grub-core/Makefile.core.def                   |  13 ++
 .../tests/asn1/CVE-2018-1000654-1_asn1_tab.h  |  32 +++
 .../tests/asn1/CVE-2018-1000654-2_asn1_tab.h  |  36 +++
 grub-core/tests/asn1/CVE-2018-1000654.c       |  58 +++++
 grub-core/tests/asn1/Test_overflow.c          | 134 ++++++++++++
 grub-core/tests/asn1/Test_simple.c            | 205 ++++++++++++++++++
 grub-core/tests/asn1/Test_strings.c           | 142 ++++++++++++
 grub-core/tests/asn1/asn1_test.c              |  49 +++++
 grub-core/tests/asn1/asn1_test.h              |  44 ++++
 grub-core/tests/asn1/object-id-decoding.c     | 109 ++++++++++
 grub-core/tests/asn1/object-id-encoding.c     | 114 ++++++++++
 grub-core/tests/asn1/octet-string.c           | 199 +++++++++++++++++
 grub-core/tests/asn1/reproducers.c            |  80 +++++++
 grub-core/tests/lib/functional_test.c         |   1 +
 tests/asn1_test.in                            |  12 +
 16 files changed, 1234 insertions(+)
 create mode 100644 grub-core/tests/asn1/CVE-2018-1000654-1_asn1_tab.h
 create mode 100644 grub-core/tests/asn1/CVE-2018-1000654-2_asn1_tab.h
 create mode 100644 grub-core/tests/asn1/CVE-2018-1000654.c
 create mode 100644 grub-core/tests/asn1/Test_overflow.c
 create mode 100644 grub-core/tests/asn1/Test_simple.c
 create mode 100644 grub-core/tests/asn1/Test_strings.c
 create mode 100644 grub-core/tests/asn1/asn1_test.c
 create mode 100644 grub-core/tests/asn1/asn1_test.h
 create mode 100644 grub-core/tests/asn1/object-id-decoding.c
 create mode 100644 grub-core/tests/asn1/object-id-encoding.c
 create mode 100644 grub-core/tests/asn1/octet-string.c
 create mode 100644 grub-core/tests/asn1/reproducers.c
 create mode 100644 tests/asn1_test.in
Makefile.util.def +6 -0
index 9432365a9..b53afb1d3 100644
@@ -1250,6 +1250,12 @@ script = {
   common = tests/luks2_test.in;
 };
 
+script = {
+  testcase = native;
+  name = asn1_test;
+  common = tests/asn1_test.in;
+};
+
 program = {
   testcase = native;
   name = example_unit_test;
grub-core/Makefile.core.def +13 -0
index b1294e0f7..fe2a2da90 100644
@@ -2610,3 +2610,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 = asn1_test;
+  common = tests/asn1/CVE-2018-1000654.c;
+  common = tests/asn1/object-id-decoding.c;
+  common = tests/asn1/object-id-encoding.c;
+  common = tests/asn1/octet-string.c;
+  common = tests/asn1/reproducers.c;
+  common = tests/asn1/Test_overflow.c;
+  common = tests/asn1/Test_simple.c;
+  common = tests/asn1/Test_strings.c;
+  common = tests/asn1/asn1_test.c;
+};
grub-core/tests/asn1/CVE-2018-1000654-1_asn1_tab.h new file +32 -0
new file mode 100644
index 000000000..1e7d3d64f
@@ -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/tests/asn1/CVE-2018-1000654-2_asn1_tab.h new file +36 -0
new file mode 100644
index 000000000..e2561e5ec
@@ -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/tests/asn1/CVE-2018-1000654.c new file +58 -0

58 lines not on this page · raw message

grub-core/tests/asn1/Test_overflow.c new file +134 -0

134 lines not on this page · raw message

grub-core/tests/asn1/Test_simple.c new file +205 -0

205 lines not on this page · raw message

grub-core/tests/asn1/Test_strings.c new file +142 -0

142 lines not on this page · raw message

grub-core/tests/asn1/asn1_test.c new file +49 -0

49 lines not on this page · raw message

grub-core/tests/asn1/asn1_test.h new file +44 -0

44 lines not on this page · raw message

grub-core/tests/asn1/object-id-decoding.c new file +109 -0

109 lines not on this page · raw message

grub-core/tests/asn1/object-id-encoding.c new file +114 -0

114 lines not on this page · raw message

grub-core/tests/asn1/octet-string.c new file +199 -0

199 lines not on this page · raw message

grub-core/tests/asn1/reproducers.c new file +80 -0

80 lines not on this page · raw message

grub-core/tests/lib/functional_test.c +1 -0
index 96781fb39..38e981f2c 100644
@@ -79,6 +79,7 @@ grub_functional_all_tests (grub_extcmd_context_t ctxt __attribute__ ((unused)),
   grub_dl_load ("cmp_test");
   grub_dl_load ("mul_test");
   grub_dl_load ("shift_test");
+  grub_dl_load ("asn1_test");
 
   FOR_LIST_ELEMENTS (test, grub_test_list)
     ok = !grub_test_run (test) && ok;
tests/asn1_test.in new file +12 -0
new file mode 100644
index 000000000..4fbb768a1
@@ -0,0 +1,12 @@
+#! @BUILD_SHEBANG@
+set -e
+
+. "@builddir@/grub-core/modinfo.sh"
+
+out=`echo functional_test asn1_test | @builddir@/grub-shell`
+
+if [ "$(echo "$out" | tail -n 1)" != "ALL TESTS PASSED" ]; then
+  echo "ASN.1 test failure: $out"
+  exit 1
+fi
+
Signature · 7 lines
2.35.3


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

PATCH v9 7/22 libtasn1: Add the documentation

In reply to Gary Lin via Grub-devel

Document libtasn1 in docs/grub-dev.texi and add the upgrade steps. Also add the patches to make libtasn1 compatible with grub code.

---
 docs/grub-dev.texi                            |  27 ++
 ...asn1-disable-code-not-needed-in-grub.patch | 319 ++++++++++++++++++
 ...tasn1-changes-for-grub-compatibility.patch | 142 ++++++++
 3 files changed, 488 insertions(+)
 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
docs/grub-dev.texi +27 -0
index 1276c5930..80c6f6738 100644
@@ -596,6 +596,33 @@ cp minilzo-2.10/*.[hc] grub-core/lib/minilzo
 rm -r minilzo-2.10*
 @end example
 
+@node libtasn1
+@section libtasn1
+
+libtasn1 is a library providing Abstract Syntax Notation One (ASN.1, as
+specified by the X.680 ITU-T recommendation) parsing and structures management,
+and Distinguished Encoding Rules (DER, as per X.690) encoding and decoding
+functions.
+
+To upgrade to a new version of the libtasn1 library, download the release
+tarball and copy the files into the target directory:
+
+@example
+curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
+tar -zxf libtasn1-4.19.0.tar.gz
+rm -r grub-core/lib/libtasn1/
+mkdir libtasn1/lib
+mkdir -p grub-core/lib/libtasn1/lib/
+cp libtasn1-4.19.0/{README.md,COPYING} grub-core/lib/libtasn1/
+cp 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} grub-core/lib/libtasn1/lib/
+cp libtasn1-4.19.0/lib/includes/libtasn1.h include/grub/
+rm -rf libtasn1-4.19.0
+@end example
+
+After upgrading the library, it is necessary to apply the patches in
+@file{grub-core/lib/libtasn1-patches/} to adjust the code to be compatible with
+grub.
+
 @node Debugging
 @chapter Debugging
 
grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch new file +319 -0

319 lines not on this page · raw message

grub-core/lib/libtasn1-patches/0002-libtasn1-changes-for-grub-compatibility.patch new file +142 -0

142 lines not on this page · raw message

Signature · 7 lines
2.35.3


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

PATCH v9 8/22 key_protector: Add key protectors framework

In reply to Gary Lin via Grub-devel

From: Hernan Gatta <[email protected]>

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

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

---
 grub-core/Makefile.am          |  1 +
 grub-core/Makefile.core.def    |  5 +++
 grub-core/disk/key_protector.c | 78 ++++++++++++++++++++++++++++++++++
 include/grub/key_protector.h   | 46 ++++++++++++++++++++
 4 files changed, 130 insertions(+)
 create mode 100644 grub-core/disk/key_protector.c
 create mode 100644 include/grub/key_protector.h
grub-core/Makefile.am +1 -0
index f18550c1c..9d3d5f519 100644
@@ -90,6 +90,7 @@ endif
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/parser.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/key_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 +5 -0
index fe2a2da90..2d6871e7c 100644
@@ -1278,6 +1278,11 @@ module = {
   common = disk/raid6_recover.c;
 };
 
+module = {
+  name = key_protector;
+  common = disk/key_protector.c;
+};
+
 module = {
   name = scsi;
   common = disk/scsi.c;
grub-core/disk/key_protector.c new file +78 -0

78 lines not on this page · raw message

include/grub/key_protector.h new file +46 -0

46 lines not on this page · raw message

Signature · 7 lines
2.35.3


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

PATCH v9 9/22 tpm2: Add TPM Software Stack (TSS)

In reply to Gary Lin via Grub-devel

From: Hernan Gatta <[email protected]>

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

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

The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, 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     | 370 ++++++++++++
 include/grub/tpm2/mu.h                 | 292 +++++++++
 include/grub/tpm2/tcg2.h               |  34 ++
 include/grub/tpm2/tpm2.h               |  34 ++
 11 files changed, 3443 insertions(+)
 create mode 100644 grub-core/tpm2/buffer.c
 create mode 100644 grub-core/tpm2/mu.c
 create mode 100644 grub-core/tpm2/tcg2.c
 create mode 100644 grub-core/tpm2/tpm2.c
 create mode 100644 include/grub/tpm2/buffer.h
 create mode 100644 include/grub/tpm2/internal/functions.h
 create mode 100644 include/grub/tpm2/internal/structs.h
 create mode 100644 include/grub/tpm2/internal/types.h
 create mode 100644 include/grub/tpm2/mu.h
 create mode 100644 include/grub/tpm2/tcg2.h
 create mode 100644 include/grub/tpm2/tpm2.h
grub-core/tpm2/buffer.c new file +145 -0
new file mode 100644
index 000000000..cb9f29497
@@ -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 +370 -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

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

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

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

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

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

Signature · 7 lines
2.35.3


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

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

#10 lore raw

PATCH v9 10/22 key_protector: Add TPM2 Key Protector

In reply to Gary Lin via Grub-devel

From: Hernan Gatta <[email protected]>

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

The theory of operation is such that the module accepts various arguments, most of which are optional and therefore possess reasonable defaults. One of these arguments is the keyfile/tpm2key parameter, which is mandatory. There are two supported key formats:

1. Raw Sealed Key (--keyfile)
When sealing a key with TPM2_Create, the public portion of the sealed key is stored in TPM2B_PUBLIC, and the private portion is in TPM2B_PRIVATE. The raw sealed key glues the fully marshalled TPM2B_PUBLIC and TPM2B_PRIVATE into one file.

2. TPM 2.0 Key (--tpm2key)
The following is the ASN.1 definition of TPM 2.0 Key File:

   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

}

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. 'policy' and 'authPolicy' are the possible policy command sequences to construst the policy digest to unseal the key. Similar to the raw sealed key, the public portion (TPM2B_PUBLIC) of the sealed key is stored in 'pubkey', and the private portion (TPM2B_PRIVATE) is in 'privkey'.

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/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 -u <PART1_UUID> -P tpm2

tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub2/sealed-2.key --pcrs=7,11 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 -u <PART1_UUID> -P tpm2

tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub2/sealed-2.tpm --pcrs=7,11 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.

Before unsealing the key, the TPM2 key protector follows the "TPMPolicy" sequences to enforce the TPM policy commands to construct a valid policy digest to unseal the key.

For the TPM 2.0 Key files, 'authPolicy' may contain multiple "TPMPolicy" sequences, the TPM2 key protector iterates 'authPolicy' to find a valid sequence to unseal key. If 'authPolicy' is empty or all sequences in 'authPolicy' fail, the protector tries the one from 'policy'. In case 'policy' is also empty, the protector creates a "TPMPolicy" sequence based on the given PCR selection.

For the raw sealed key, the TPM2 key protector treats the key file as a TPM 2.0 Key file without 'authPolicy' and 'policy', so the "TPMPolicy" sequence is always based on the PCR selection from the command parameters.

Currently, there is only one supported policy command: TPM2_PolicyPCR. The command set can be extended to support advanced features, such as authorized policy, in the future.

---
 grub-core/Makefile.core.def       |   13 +
 grub-core/tpm2/args.c             |  177 +++++
 grub-core/tpm2/module.c           | 1028 +++++++++++++++++++++++++++++
 grub-core/tpm2/tpm2key.asn        |   31 +
 grub-core/tpm2/tpm2key.c          |  447 +++++++++++++
 grub-core/tpm2/tpm2key_asn1_tab.c |   41 ++
 include/grub/tpm2/internal/args.h |   41 ++
 include/grub/tpm2/tpm2key.h       |   83 +++
 8 files changed, 1861 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
index 2d6871e7c..e937f6538 100644
@@ -2557,6 +2557,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 +177 -0

177 lines not on this page · raw message

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

1,028 lines not on this page · raw message

grub-core/tpm2/tpm2key.asn new file +31 -0
new file mode 100644
index 000000000..e3b6a03e0
@@ -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 +447 -0

447 lines not on this page · raw message

grub-core/tpm2/tpm2key_asn1_tab.c new file +41 -0

41 lines not on this page · raw message

include/grub/tpm2/internal/args.h new file +41 -0

41 lines not on this page · raw message

include/grub/tpm2/tpm2key.h new file +83 -0

83 lines not on this page · raw message

Signature · 7 lines
2.35.3


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

PATCH v9 11/22 cryptodisk: Support key protectors

In reply to Gary Lin via Grub-devel

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.

---
 Makefile.util.def           |   1 +
 grub-core/disk/cryptodisk.c | 172 +++++++++++++++++++++++++++++-------
 include/grub/cryptodisk.h   |  16 ++++
 3 files changed, 158 insertions(+), 31 deletions(-)
Makefile.util.def +1 -0
index b53afb1d3..19ad5a96f 100644
@@ -40,6 +40,7 @@ library = {
   common = grub-core/disk/luks.c;
   common = grub-core/disk/luks2.c;
   common = grub-core/disk/geli.c;
+  common = grub-core/disk/key_protector.c;
   common = grub-core/disk/cryptodisk.c;
   common = grub-core/disk/AFSplitter.c;
   common = grub-core/lib/pbkdf2.c;
grub-core/disk/cryptodisk.c +141 -31

267 lines not on this page · raw message

include/grub/cryptodisk.h +16 -0
index d94df68b6..0b41e249e 100644
@@ -70,6 +70,18 @@ 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 +93,10 @@ struct grub_cryptomount_args
   /* recover_key: Length of key_data */
   grub_size_t key_len;
   grub_file_t hdr_file;
+  /* recover_key: Names of the key protectors to use (NULL-terminated) */
+  char **protectors;
+  /* recover_key: Key cache to avoid invoking the same key protector twice */
+  struct grub_cryptomount_cached_key *key_cache;
 };
 typedef struct grub_cryptomount_args *grub_cryptomount_args_t;
 
Signature · 7 lines
2.35.3


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

PATCH v9 12/22 util/grub-protect: Add new tool

In reply to Gary Lin via Grub-devel

From: Hernan Gatta <[email protected]>

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

For the TPM2 key protector, the intended flow is for a user to have a LUKS 1 or LUKS 2-protected fully-encrypted disk. The user then creates a new 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 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:

$ 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.tpm

Or, 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.key

Then, in the boot script, for TPM 2.0 Key File:

tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm 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 <SDB1_UUID> -P tpm2

---
 .gitignore          |    2 +
 Makefile.util.def   |   22 +
 configure.ac        |   30 +
 util/grub-protect.c | 1492 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1546 insertions(+)
 create mode 100644 util/grub-protect.c
.gitignore +2 -0
index 4d0dfb700..d7b7c22d6 100644
@@ -169,6 +169,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
index 19ad5a96f..a0a3e2cd5 100644
@@ -207,6 +207,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)';
+
+  condition = COND_GRUB_PROTECT;
+};
+
 program = {
   name = grub-mkrelpath;
   mansection = 1;
configure.ac +30 -0
index 84a202c6e..3a07ab570 100644
@@ -76,6 +76,7 @@ grub_TRANSFORM([grub-mkpasswd-pbkdf2])
 grub_TRANSFORM([grub-mkrelpath])
 grub_TRANSFORM([grub-mkrescue])
 grub_TRANSFORM([grub-probe])
+grub_TRANSFORM([grub-protect])
 grub_TRANSFORM([grub-reboot])
 grub_TRANSFORM([grub-script-check])
 grub_TRANSFORM([grub-set-default])
@@ -2057,6 +2058,29 @@ fi
 AC_SUBST([LIBZFS])
 AC_SUBST([LIBNVPAIR])
 
+AC_ARG_ENABLE([grub-protect],
+	      [AS_HELP_STRING([--enable-grub-protect],
+                             [build and install the `grub-protect' utility (default=guessed)])])
+if test x"$enable_grub_protect" = xno ; then
+  grub_protect_excuse="explicitly disabled"
+fi
+
+LIBTASN1=
+if test x"$grub_protect_excuse" = x ; then
+  AC_CHECK_LIB([tasn1], [asn1_write_value], [LIBTASN1="-ltasn1"], [grub_protect_excuse="need libtasn1 library"])
+fi
+AC_SUBST([LIBTASN1])
+
+if test x"$enable_grub_protect" = xyes && test x"$grub_protect_excuse" != x ; then
+  AC_MSG_ERROR([grub-protect was explicitly requested but can't be compiled ($grub_protect_excuse)])
+fi
+if test x"$grub_protect_excuse" = x ; then
+enable_grub_protect=yes
+else
+enable_grub_protect=no
+fi
+AC_SUBST([enable_grub_protect])
+
 LIBS=""
 
 AC_SUBST([FONT_SOURCE])
@@ -2173,6 +2197,7 @@ AM_CONDITIONAL([COND_GRUB_EMU_SDL], [test x$enable_grub_emu_sdl = xyes])
 AM_CONDITIONAL([COND_GRUB_EMU_PCI], [test x$enable_grub_emu_pci = xyes])
 AM_CONDITIONAL([COND_GRUB_MKFONT], [test x$enable_grub_mkfont = xyes])
 AM_CONDITIONAL([COND_GRUB_MOUNT], [test x$enable_grub_mount = xyes])
+AM_CONDITIONAL([COND_GRUB_PROTECT], [test x$enable_grub_protect = xyes])
 AM_CONDITIONAL([COND_HAVE_FONT_SOURCE], [test x$FONT_SOURCE != x])
 if test x$FONT_SOURCE != x ; then
    HAVE_FONT_SOURCE=1
@@ -2300,6 +2325,11 @@ echo grub-mount: Yes
 else
 echo grub-mount: No "($grub_mount_excuse)"
 fi
+if [ x"$grub_protect_excuse" = x ]; then
+echo grub-protect: Yes
+else
+echo grub-protect: No "($grub_protect_excuse)"
+fi
 if [ x"$starfield_excuse" = x ]; then
 echo starfield theme: Yes
 echo With DejaVuSans font from $DJVU_FONT_SOURCE
util/grub-protect.c new file +1492 -0

1,492 lines not on this page · raw message

Signature · 7 lines
2.35.3


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

PATCH v9 13/22 tpm2: Add TPM2 types, structures, and command constants

In reply to Gary Lin via Grub-devel

Add new TPM2 types and structures as the preparation to support authorized policy.

* New types:
TPM_ALG_ECDAA, TPM_ALG_ECDSA, TPM_ALG_ECSCHNORR, TPM_ALG_RSASSA, TPM_ALG_RSAPSS, TPM_ALG_SM2, and TPMI_ALG_SIG_SCHEME

* New structures:
TPMS_EMPTY, TPMS_SIGNATURE_RSA, TPMS_SIGNATURE_ECC, TPMS_SIGNATURE_ECDSA, TPMS_SIGNATURE_ECDAA, TPMS_SIGNATURE_SM2, TPMS_SIGNATURE_ECSCHNORR, TPMU_SIGNATURE, and TPMT_TK_VERIFIED

* New command constants:
TPM_CC_LoadExternal, TPM_CC_HashSequenceStart, TPM_CC_SequenceUpdate, TPM_CC_SequenceComplete, TPM_CC_Hash, TPM_CC_VerifySignature, TPM_CC_PolicyAuthorize

---
 include/grub/tpm2/internal/structs.h | 86 ++++++++++++++++++++++++++++
 include/grub/tpm2/internal/types.h   | 42 +++++++++-----
 2 files changed, 114 insertions(+), 14 deletions(-)
include/grub/tpm2/internal/structs.h +86 -0
index 72d71eb70..db9eb6cf6 100644
@@ -672,4 +672,90 @@ struct TPMT_TK_CREATION
 };
 typedef struct TPMT_TK_CREATION TPMT_TK_CREATION;
 
+/* TPMS_EMPTY Structure */
+struct TPMS_EMPTY {
+  grub_uint8_t empty[1]; /* a structure with no member */
+};
+typedef struct TPMS_EMPTY TPMS_EMPTY;
+
+/* TPMS_SIGNATURE_RSA Structure */
+struct TPMS_SIGNATURE_RSA {
+  TPMI_ALG_HASH hash;
+  TPM2B_PUBLIC_KEY_RSA sig;
+};
+typedef struct TPMS_SIGNATURE_RSA TPMS_SIGNATURE_RSA;
+
+/* Definition of Types for RSA Signature */
+typedef TPMS_SIGNATURE_RSA TPMS_SIGNATURE_RSASSA;
+typedef TPMS_SIGNATURE_RSA TPMS_SIGNATURE_RSAPSS;
+
+/* TPMS_SIGNATURE_ECC Structure */
+struct TPMS_SIGNATURE_ECC {
+  TPMI_ALG_HASH hash;
+  TPM2B_ECC_PARAMETER signatureR;
+  TPM2B_ECC_PARAMETER signatureS;
+};
+typedef struct TPMS_SIGNATURE_ECC TPMS_SIGNATURE_ECC;
+
+/* Definition of Types for ECC TPMS_SIGNATURE_ECC */
+typedef TPMS_SIGNATURE_ECC TPMS_SIGNATURE_ECDSA;
+typedef TPMS_SIGNATURE_ECC TPMS_SIGNATURE_ECDAA;
+typedef TPMS_SIGNATURE_ECC TPMS_SIGNATURE_SM2;
+typedef TPMS_SIGNATURE_ECC TPMS_SIGNATURE_ECSCHNORR;
+
+/* TPMU_SIGNATURE Structure */
+union TPMU_SIGNATURE {
+  TPMS_SIGNATURE_RSASSA rsassa;
+  TPMS_SIGNATURE_RSAPSS rsapss;
+  TPMS_SIGNATURE_ECDSA ecdsa;
+  TPMS_SIGNATURE_ECDAA ecdaa;
+  TPMS_SIGNATURE_SM2 sm2;
+  TPMS_SIGNATURE_ECSCHNORR ecschnorr;
+  TPMT_HA hmac;
+  TPMS_SCHEME_HASH any;
+  TPMS_EMPTY null;
+};
+typedef union TPMU_SIGNATURE TPMU_SIGNATURE;
+
+/* TPMT_SIGNATURE Structure */
+struct TPMT_SIGNATURE {
+  TPMI_ALG_SIG_SCHEME sigAlg;
+  TPMU_SIGNATURE signature;
+};
+typedef struct TPMT_SIGNATURE TPMT_SIGNATURE;
+
+static inline TPMI_ALG_HASH
+TPMT_SIGNATURE_get_hash_alg (TPMT_SIGNATURE *sig)
+{
+  switch (sig->sigAlg)
+    {
+    case TPM_ALG_RSASSA:
+      return sig->signature.rsassa.hash;
+    case TPM_ALG_RSAPSS:
+      return sig->signature.rsapss.hash;
+    case TPM_ALG_ECDSA:
+      return sig->signature.ecdsa.hash;
+    case TPM_ALG_ECDAA:
+      return sig->signature.ecdaa.hash;
+    case TPM_ALG_SM2:
+      return sig->signature.sm2.hash;
+    case TPM_ALG_ECSCHNORR:
+      return sig->signature.ecschnorr.hash;
+    case TPM_ALG_HMAC:
+      return sig->signature.hmac.hashAlg;
+    default:
+      break;
+    }
+
+  return TPM_ALG_NULL;
+}
+
+/* TPMT_TK_VERIFIED Structure */
+struct TPMT_TK_VERIFIED {
+  TPM_ST tag;
+  TPMI_RH_HIERARCHY hierarchy;
+  TPM2B_DIGEST digest;
+};
+typedef struct TPMT_TK_VERIFIED TPMT_TK_VERIFIED;
+
 #endif /* ! GRUB_TPM2_INTERNAL_STRUCTS_HEADER */
include/grub/tpm2/internal/types.h +28 -14

70 lines not on this page · raw message

Signature · 7 lines
2.35.3


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

PATCH v9 14/22 tpm2: Add more marshal/unmarshal functions

In reply to Gary Lin via Grub-devel

Add a few more marshal/unmarshal functions to support authorized policy.

* Marshal:
grub_tpm2_mu_TPMU_SENSITIVE_COMPOSITE_Marshal()
grub_tpm2_mu_TPMT_SENSITIVE_Marshal()
grub_tpm2_mu_TPM2B_SENSITIVE_Marshal()
grub_tpm2_mu_TPMS_SIGNATURE_RSA_Marshal()
grub_tpm2_mu_TPMS_SIGNATURE_ECC_Marshal()
grub_tpm2_mu_TPMU_HA_Marshal()
grub_tpm2_mu_TPMT_HA_Marshal()
grub_tpm2_mu_TPMU_SIGNATURE_Marshal()
grub_tpm2_mu_TPMT_SIGNATURE_Marshal()
grub_tpm2_mu_TPMT_TK_VERIFIED_Marshal()

* Unmarshal:
grub_tpm2_mu_TPMT_TK_HASHCHECK_Unmarshal()
grub_tpm2_mu_TPMT_TK_VERIFIED_Unmarshal()
grub_tpm2_mu_TPMS_SIGNATURE_RSA_Unmarshal()
grub_tpm2_mu_TPMS_SIGNATURE_ECC_Unmarshal()
grub_tpm2_mu_TPMU_HA_Unmarshal()
grub_tpm2_mu_TPMT_HA_Unmarshal()
grub_tpm2_mu_TPMU_SIGNATURE_Unmarshal()
grub_tpm2_mu_TPMT_SIGNATURE_Unmarshal()

---
 grub-core/tpm2/mu.c    | 262 +++++++++++++++++++++++++++++++++++++++++
 include/grub/tpm2/mu.h |  75 ++++++++++++
 2 files changed, 337 insertions(+)
grub-core/tpm2/mu.c +262 -0
index 1617f37cd..3a9a3c1be 100644
@@ -383,6 +383,49 @@ grub_tpm2_mu_TPMS_SENSITIVE_CREATE_Marshal (grub_tpm2_buffer_t buffer,
   grub_tpm2_mu_TPM2B_Marshal (buffer, p->data.size, p->data.buffer);
 }
 
+void
+grub_tpm2_mu_TPMU_SENSITIVE_COMPOSITE_Marshal (grub_tpm2_buffer_t buffer,
+                                               const TPMI_ALG_PUBLIC type,
+                                               const TPMU_SENSITIVE_COMPOSITE *p)
+{
+  switch(type)
+    {
+    case TPM_ALG_RSA:
+      grub_tpm2_mu_TPM2B_Marshal (buffer, p->rsa.size, p->rsa.buffer);
+      break;
+    case TPM_ALG_ECC:
+      grub_tpm2_mu_TPM2B_Marshal (buffer, p->ecc.size, p->ecc.buffer);
+      break;
+    case TPM_ALG_KEYEDHASH:
+      grub_tpm2_mu_TPM2B_Marshal (buffer, p->bits.size, p->bits.buffer);
+      break;
+    case TPM_ALG_SYMCIPHER:
+      grub_tpm2_mu_TPM2B_Marshal (buffer, p->sym.size, p->sym.buffer);
+      break;
+    default:
+      buffer->error = 1;
+    }
+}
+
+void
+grub_tpm2_mu_TPMT_SENSITIVE_Marshal (grub_tpm2_buffer_t buffer,
+                                     const TPMT_SENSITIVE *p)
+{
+  grub_tpm2_buffer_pack_u16 (buffer, p->sensitiveType);
+  grub_tpm2_mu_TPM2B_Marshal (buffer, p->authValue.size, p->authValue.buffer);
+  grub_tpm2_mu_TPM2B_Marshal (buffer, p->seedValue.size, p->seedValue.buffer);
+  grub_tpm2_mu_TPMU_SENSITIVE_COMPOSITE_Marshal (buffer, p->sensitiveType,
+                                                 &p->sensitive);
+}
+
+void
+grub_tpm2_mu_TPM2B_SENSITIVE_Marshal (grub_tpm2_buffer_t buffer,
+                                      const TPM2B_SENSITIVE *p)
+{
+  grub_tpm2_buffer_pack_u16 (buffer, p->size);
+  grub_tpm2_mu_TPMT_SENSITIVE_Marshal (buffer, &p->sensitiveArea);
+}
+
 void
 grub_tpm2_mu_TPM2B_SENSITIVE_CREATE_Marshal (grub_tpm2_buffer_t buffer,
 					     const TPM2B_SENSITIVE_CREATE *sensitiveCreate)
@@ -405,6 +448,113 @@ grub_tpm2_mu_TPM2B_SENSITIVE_CREATE_Marshal (grub_tpm2_buffer_t buffer,
     grub_tpm2_buffer_pack_u16 (buffer, 0);
 }
 
+void
+grub_tpm2_mu_TPMS_SIGNATURE_RSA_Marshal (grub_tpm2_buffer_t buffer,
+                                         const TPMS_SIGNATURE_RSA *p)
+{
+  grub_tpm2_buffer_pack_u16 (buffer, p->hash);
+  grub_tpm2_mu_TPM2B_Marshal (buffer, p->sig.size, p->sig.buffer);
+}
+
+void
+grub_tpm2_mu_TPMS_SIGNATURE_ECC_Marshal (grub_tpm2_buffer_t buffer,
+                                         const TPMS_SIGNATURE_ECC *p)
+{
+  grub_tpm2_buffer_pack_u16 (buffer, p->hash);
+  grub_tpm2_mu_TPM2B_Marshal (buffer, p->signatureR.size, p->signatureR.buffer);
+  grub_tpm2_mu_TPM2B_Marshal (buffer, p->signatureS.size, p->signatureS.buffer);
+}
+
+void
+grub_tpm2_mu_TPMU_HA_Marshal (grub_tpm2_buffer_t buffer,
+                              const TPMI_ALG_HASH hashAlg,
+                              const TPMU_HA *p)
+{
+  switch (hashAlg)
+    {
+    case TPM_ALG_SHA1:
+      for (grub_uint16_t i = 0; i < TPM_SHA1_DIGEST_SIZE; i++)
+        grub_tpm2_buffer_pack_u8 (buffer, p->sha1[i]);
+      break;
+    case TPM_ALG_SHA256:
+      for (grub_uint16_t i = 0; i < TPM_SHA256_DIGEST_SIZE; i++)
+        grub_tpm2_buffer_pack_u8 (buffer, p->sha256[i]);
+      break;
+    case TPM_ALG_SHA384:
+      for (grub_uint16_t i = 0; i < TPM_SHA384_DIGEST_SIZE; i++)
+        grub_tpm2_buffer_pack_u8 (buffer, p->sha384[i]);
+      break;
+    case TPM_ALG_SHA512:
+      for (grub_uint16_t i = 0; i < TPM_SHA512_DIGEST_SIZE; i++)
+        grub_tpm2_buffer_pack_u8 (buffer, p->sha512[i]);
+      break;
+    default:
+      buffer->error = 1;
+      break;
+    }
+}
+
+void
+grub_tpm2_mu_TPMT_HA_Marshal (grub_tpm2_buffer_t buffer,
+                              const TPMT_HA *p)
+{
+  grub_tpm2_buffer_pack_u16 (buffer, p->hashAlg);
+  grub_tpm2_mu_TPMU_HA_Marshal (buffer, p->hashAlg, &p->digest);
+}
+
+void
+grub_tpm2_mu_TPMU_SIGNATURE_Marshal (grub_tpm2_buffer_t buffer,
+                                     const TPMI_ALG_SIG_SCHEME sigAlg,
+                                     const TPMU_SIGNATURE *p)
+{
+  switch (sigAlg)
+    {
+    case TPM_ALG_RSASSA:
+      grub_tpm2_mu_TPMS_SIGNATURE_RSA_Marshal (buffer, (TPMS_SIGNATURE_RSA *)&p->rsassa);
+      break;
+    case TPM_ALG_RSAPSS:
+      grub_tpm2_mu_TPMS_SIGNATURE_RSA_Marshal (buffer, (TPMS_SIGNATURE_RSA *)&p->rsapss);
+      break;
+    case TPM_ALG_ECDSA:
+      grub_tpm2_mu_TPMS_SIGNATURE_ECC_Marshal (buffer, (TPMS_SIGNATURE_ECC *)&p->ecdsa);
+      break;
+    case TPM_ALG_ECDAA:
+      grub_tpm2_mu_TPMS_SIGNATURE_ECC_Marshal (buffer, (TPMS_SIGNATURE_ECC *)&p->ecdaa);
+      break;
+    case TPM_ALG_SM2:
+      grub_tpm2_mu_TPMS_SIGNATURE_ECC_Marshal (buffer, (TPMS_SIGNATURE_ECC *)&p->sm2);
+      break;
+    case TPM_ALG_ECSCHNORR:
+      grub_tpm2_mu_TPMS_SIGNATURE_ECC_Marshal (buffer, (TPMS_SIGNATURE_ECC *)&p->ecschnorr);
+      break;
+    case TPM_ALG_HMAC:
+      grub_tpm2_mu_TPMT_HA_Marshal (buffer, &p->hmac);
+      break;
+    case TPM_ALG_NULL:
+      break;
+    default:
+      buffer->error = 1;
+      break;
+    }
+}
+
+void
+grub_tpm2_mu_TPMT_SIGNATURE_Marshal (grub_tpm2_buffer_t buffer,
+                                     const TPMT_SIGNATURE *p)
+{
+  grub_tpm2_buffer_pack_u16 (buffer, p->sigAlg);
+  grub_tpm2_mu_TPMU_SIGNATURE_Marshal (buffer, p->sigAlg, &p->signature);
+}
+
+void
+grub_tpm2_mu_TPMT_TK_VERIFIED_Marshal (grub_tpm2_buffer_t buffer,
+                                       const TPMT_TK_VERIFIED *p)
+{
+  grub_tpm2_buffer_pack_u16 (buffer, p->tag);
+  grub_tpm2_buffer_pack_u32 (buffer, p->hierarchy);
+  grub_tpm2_mu_TPM2B_Marshal (buffer, p->digest.size, p->digest.buffer);
+}
+
 void
 grub_tpm2_mu_TPM2B_Unmarshal (grub_tpm2_buffer_t buffer,
 			      TPM2B* p)
@@ -775,6 +925,24 @@ grub_tpm2_mu_TPMT_TK_CREATION_Unmarshal (grub_tpm2_buffer_t buffer,
   grub_tpm2_mu_TPM2B_Unmarshal (buffer, (TPM2B*) &p->digest);
 }
 
+void
+grub_tpm2_mu_TPMT_TK_HASHCHECK_Unmarshal (grub_tpm2_buffer_t buffer,
+                                          TPMT_TK_HASHCHECK *p)
+{
+  grub_tpm2_buffer_unpack_u16 (buffer, &p->tag);
+  grub_tpm2_buffer_unpack_u32 (buffer, &p->hierarchy);
+  grub_tpm2_mu_TPM2B_Unmarshal (buffer, (TPM2B*) &p->digest);
+}
+
+void
+grub_tpm2_mu_TPMT_TK_VERIFIED_Unmarshal (grub_tpm2_buffer_t buffer,
+                                         TPMT_TK_VERIFIED *p)
+{
+  grub_tpm2_buffer_unpack_u16 (buffer, &p->tag);
+  grub_tpm2_buffer_unpack_u32 (buffer, &p->hierarchy);
+  grub_tpm2_mu_TPM2B_Unmarshal (buffer, (TPM2B*) &p->digest);
+}
+
 void
 grub_tpm2_mu_TPMS_PCR_SELECTION_Unmarshal (grub_tpm2_buffer_t buf,
 					   TPMS_PCR_SELECTION* pcrSelection)
@@ -805,3 +973,97 @@ grub_tpm2_mu_TPML_DIGEST_Unmarshal (grub_tpm2_buffer_t buf,
   for (grub_uint32_t i = 0; i < digest->count; i++)
     grub_tpm2_mu_TPM2B_DIGEST_Unmarshal (buf, &digest->digests[i]);
 }
+
+void
+grub_tpm2_mu_TPMS_SIGNATURE_RSA_Unmarshal (grub_tpm2_buffer_t buffer,
+                                           TPMS_SIGNATURE_RSA *rsa)
+{
+  grub_tpm2_buffer_unpack_u16 (buffer, &rsa->hash);
+  grub_tpm2_mu_TPM2B_Unmarshal (buffer, (TPM2B*)&rsa->sig);
+}
+
+void
+grub_tpm2_mu_TPMS_SIGNATURE_ECC_Unmarshal (grub_tpm2_buffer_t buffer,
+                                           TPMS_SIGNATURE_ECC *ecc)
+{
+  grub_tpm2_buffer_unpack_u16 (buffer, &ecc->hash);
+  grub_tpm2_mu_TPM2B_Unmarshal (buffer, (TPM2B*)&ecc->signatureR);
+  grub_tpm2_mu_TPM2B_Unmarshal (buffer, (TPM2B*)&ecc->signatureS);
+}
+
+void
+grub_tpm2_mu_TPMU_HA_Unmarshal (grub_tpm2_buffer_t buffer,
+                                TPMI_ALG_HASH hashAlg,
+                                TPMU_HA *p)
+{
+  switch (hashAlg)
+    {
+    case TPM_ALG_SHA1:
+      grub_tpm2_buffer_unpack (buffer, &p->sha1, TPM_SHA1_DIGEST_SIZE);
+      break;
+    case TPM_ALG_SHA256:
+      grub_tpm2_buffer_unpack (buffer, &p->sha256, TPM_SHA256_DIGEST_SIZE);
+      break;
+    case TPM_ALG_SHA384:
+      grub_tpm2_buffer_unpack (buffer, &p->sha384, TPM_SHA384_DIGEST_SIZE);
+      break;
+    case TPM_ALG_SHA512:
+      grub_tpm2_buffer_unpack (buffer, &p->sha512, TPM_SHA512_DIGEST_SIZE);
+      break;
+    default:
+      buffer->error = 1;
+      break;
+    }
+}
+
+void
+grub_tpm2_mu_TPMT_HA_Unmarshal (grub_tpm2_buffer_t buffer,
+                                TPMT_HA *p)
+{
+  grub_tpm2_buffer_unpack_u16 (buffer, &p->hashAlg);
+  grub_tpm2_mu_TPMU_HA_Unmarshal (buffer, p->hashAlg, &p->digest);
+}
+
+void
+grub_tpm2_mu_TPMU_SIGNATURE_Unmarshal (grub_tpm2_buffer_t buffer,
+                                       TPMI_ALG_SIG_SCHEME sigAlg,
+                                       TPMU_SIGNATURE *p)
+{
+  switch (sigAlg)
+    {
+    case TPM_ALG_RSASSA:
+      grub_tpm2_mu_TPMS_SIGNATURE_RSA_Unmarshal (buffer, (TPMS_SIGNATURE_RSA *)&p->rsassa);
+      break;
+    case TPM_ALG_RSAPSS:
+      grub_tpm2_mu_TPMS_SIGNATURE_RSA_Unmarshal (buffer, (TPMS_SIGNATURE_RSA *)&p->rsapss);
+      break;
+    case TPM_ALG_ECDSA:
+      grub_tpm2_mu_TPMS_SIGNATURE_ECC_Unmarshal (buffer, (TPMS_SIGNATURE_ECC *)&p->ecdsa);
+      break;
+    case TPM_ALG_ECDAA:
+      grub_tpm2_mu_TPMS_SIGNATURE_ECC_Unmarshal (buffer, (TPMS_SIGNATURE_ECC *)&p->ecdaa);
+      break;
+    case TPM_ALG_SM2:
+      grub_tpm2_mu_TPMS_SIGNATURE_ECC_Unmarshal (buffer, (TPMS_SIGNATURE_ECC *)&p->sm2);
+      break;
+    case TPM_ALG_ECSCHNORR:
+      grub_tpm2_mu_TPMS_SIGNATURE_ECC_Unmarshal (buffer, (TPMS_SIGNATURE_ECC *)&p->ecschnorr);
+      break;
+    case TPM_ALG_HMAC:
+      grub_tpm2_mu_TPMT_HA_Unmarshal (buffer, &p->hmac);
+      break;
+    case TPM_ALG_NULL:
+      break;
+    default:
+      buffer->error = 1;
+      break;
+    }
+}
+
+void
+grub_tpm2_mu_TPMT_SIGNATURE_Unmarshal (grub_tpm2_buffer_t buffer,
+                                       TPMT_SIGNATURE *p)
+{
+  grub_tpm2_buffer_unpack_u16 (buffer, &p->sigAlg);
+  grub_tpm2_mu_TPMU_SIGNATURE_Unmarshal (buffer, p->sigAlg, &p->signature);
+}
include/grub/tpm2/mu.h +75 -0

91 lines not on this page · raw message

Signature · 7 lines
2.35.3


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

PATCH v9 15/22 tpm2: Implement more TPM2 commands

In reply to Gary Lin via Grub-devel

This commit implements a few more TPM2 commands as the preparation for the authorized policy support.

* TPM2_LoadExternal
This command is added to load the external public key to verify the signed policy digest
* TPM2_HashSequenceStart, TPM2_SequenceUpdate, TPM2_SequenceComplete, and TPM2_Hash
With those commands, we can use the TPM as a coprocessor to calculate the hash of a given binary blob.
* TPM2_VerifySignature
This command verifies the given signature with the given public key and returns the validation ticket to authorize the policy.
* TPM2_PolicyAuthorize
This command approves the given policy digest so that we can unseal the key with the newly authorized policy.

---
 grub-core/tpm2/tpm2.c                  | 424 +++++++++++++++++++++++++
 include/grub/tpm2/internal/functions.h |  57 ++++
 2 files changed, 481 insertions(+)
grub-core/tpm2/tpm2.c +424 -0
index d67699a24..159353b08 100644
@@ -427,6 +427,73 @@ TPM2_Load (const TPMI_DH_OBJECT parent_handle,
   return TPM_RC_SUCCESS;
 }
 
+TPM_RC
+TPM2_LoadExternal (const TPMS_AUTH_COMMAND *authCommand,
+                   const TPM2B_SENSITIVE *inPrivate,
+                   const TPM2B_PUBLIC *inPublic,
+                   const TPMI_RH_HIERARCHY hierarchy,
+                   TPM_HANDLE *objectHandle,
+                   TPM2B_NAME *name,
+                   TPMS_AUTH_RESPONSE *authResponse)
+{
+  TPM_RC rc;
+  struct grub_tpm2_buffer in;
+  struct grub_tpm2_buffer out;
+  TPM_HANDLE objectHandleTmp;
+  TPM2B_NAME nameTmp;
+  TPMS_AUTH_RESPONSE authResponseTmp;
+  TPMI_ST_COMMAND_TAG tag = authCommand ? TPM_ST_SESSIONS : TPM_ST_NO_SESSIONS;
+  TPM_RC responseCode;
+  grub_uint32_t param_size;
+
+  if (!inPublic)
+    return TPM_RC_VALUE;
+
+  if (!objectHandle)
+    objectHandle = &objectHandleTmp;
+  if (!name)
+    name = &nameTmp;
+  if (!authResponse)
+    authResponse = &authResponseTmp;
+
+  grub_memset (objectHandle, 0, sizeof (*objectHandle));
+  grub_memset (name, 0, sizeof (*name));
+  grub_memset (authResponse, 0, sizeof (*authResponse));
+
+  /* Marshal */
+  grub_tpm2_buffer_init (&in);
+  if (authCommand)
+    grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
+  if (inPrivate)
+    grub_tpm2_mu_TPM2B_SENSITIVE_Marshal (&in, inPrivate);
+  else
+    grub_tpm2_buffer_pack_u16 (&in, 0);
+  grub_tpm2_mu_TPM2B_PUBLIC_Marshal (&in, inPublic);
+  grub_tpm2_buffer_pack_u32 (&in, hierarchy);
+  if (in.error)
+    return TPM_RC_FAILURE;
+
+  /* Submit */
+  grub_tpm2_buffer_init (&out);
+  rc = grub_tpm2_submit_command (tag, TPM_CC_LoadExternal, &responseCode, &in, &out);
+  if (rc != TPM_RC_SUCCESS)
+    return rc;
+  if (responseCode != TPM_RC_SUCCESS)
+    return responseCode;
+
+  /* Unmarshal*/
+  grub_tpm2_buffer_unpack_u32 (&out, objectHandle);
+  if (tag == TPM_ST_SESSIONS)
+    grub_tpm2_buffer_unpack_u32 (&out, &param_size);
+  grub_tpm2_mu_TPM2B_Unmarshal (&out, (TPM2B*)name);
+  if (tag == TPM_ST_SESSIONS)
+    grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal (&out, authResponse);
+  if (out.error)
+    return TPM_RC_FAILURE;
+
+  return TPM_RC_SUCCESS;
+}
+
 TPM_RC
 TPM2_Unseal (const TPMI_DH_OBJECT itemHandle,
 	     const TPMS_AUTH_COMMAND *authCommand,
@@ -759,3 +826,360 @@ TPM2_EvictControl (const TPMI_RH_PROVISION auth,
 
   return TPM_RC_SUCCESS;
 }
+
+TPM_RC
+TPM2_HashSequenceStart (const TPMS_AUTH_COMMAND *authCommand,
+                        const TPM2B_AUTH *auth,
+                        const TPMI_ALG_HASH hashAlg,
+                        TPMI_DH_OBJECT *sequenceHandle,
+                        TPMS_AUTH_RESPONSE *authResponse)
+{
+  struct grub_tpm2_buffer in;
+  struct grub_tpm2_buffer out;
+  TPMI_DH_OBJECT sequenceHandleTmp;
+  TPMS_AUTH_RESPONSE authResponseTmp;
+  TPMI_ST_COMMAND_TAG tag = authCommand ? TPM_ST_SESSIONS : TPM_ST_NO_SESSIONS;
+  TPM_RC responseCode;
+  TPM_RC rc;
+  grub_uint32_t parameterSize;
+
+  if (!auth)
+    return TPM_RC_VALUE;
+
+  if (!sequenceHandle)
+    sequenceHandle = &sequenceHandleTmp;
+  if (!authResponse)
+    authResponse = &authResponseTmp;
+
+  grub_memset (sequenceHandle, 0, sizeof (*sequenceHandle));
+  grub_memset (authResponse, 0, sizeof (*authResponse));
+
+  /* Marshal */
+  grub_tpm2_buffer_init (&in);
+  if (authCommand)
+    grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
+  grub_tpm2_mu_TPM2B_Marshal (&in, auth->size, auth->buffer);
+  grub_tpm2_buffer_pack_u16 (&in, hashAlg);
+  if (in.error)
+    return TPM_RC_FAILURE;
+
+  /* Submit */
+  grub_tpm2_buffer_init (&out);
+  rc = grub_tpm2_submit_command (tag, TPM_CC_HashSequenceStart, &responseCode, &in,
+                                 &out);
+  if (rc != TPM_RC_SUCCESS)
+    return rc;
+  if (responseCode != TPM_RC_SUCCESS)
+    return responseCode;
+
+  /* Unmarshal */
+  grub_tpm2_buffer_unpack_u32 (&out, sequenceHandle);
+  if (tag == TPM_ST_SESSIONS)
+    {
+      grub_tpm2_buffer_unpack_u32 (&out, &parameterSize);
+      grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal(&out, authResponse);
+    }
+  if (out.error)
+    return TPM_RC_FAILURE;
+
+  return TPM_RC_SUCCESS;
+}
+
+TPM_RC
+TPM2_SequenceUpdate (const TPMI_DH_OBJECT sequenceHandle,
+                     const TPMS_AUTH_COMMAND *authCommand,
+                     const TPM2B_MAX_BUFFER *buffer,
+                     TPMS_AUTH_RESPONSE *authResponse)
+{
+  struct grub_tpm2_buffer in;
+  struct grub_tpm2_buffer out;
+  TPMS_AUTH_RESPONSE authResponseTmp;
+  TPM_RC responseCode;
+  TPM_RC rc;
+  grub_uint32_t parameterSize;
+
+  if (!authCommand)
+    return TPM_RC_VALUE;
+
+  if (!authResponse)
+    authResponse = &authResponseTmp;
+
+  grub_memset (authResponse, 0, sizeof (*authResponse));
+
+  /* Marshal */
+  grub_tpm2_buffer_init (&in);
+  grub_tpm2_buffer_pack_u32 (&in, sequenceHandle);
+  grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
+  if (buffer)
+    grub_tpm2_mu_TPM2B_Marshal (&in, buffer->size, buffer->buffer);
+  else
+    grub_tpm2_buffer_pack_u16 (&in, 0);
+  if (in.error)
+    return TPM_RC_FAILURE;
+
+  /* Submit */
+  grub_tpm2_buffer_init (&out);
+  rc = grub_tpm2_submit_command (TPM_ST_SESSIONS, TPM_CC_SequenceUpdate,
+                                 &responseCode, &in, &out);
+  if (rc != TPM_RC_SUCCESS)
+    return rc;
+  if (responseCode != TPM_RC_SUCCESS)
+    return responseCode;
+
+  /* Unmarshal */
+  grub_tpm2_buffer_unpack_u32 (&out, &parameterSize);
+  grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal(&out, authResponse);
+  if (out.error)
+    return TPM_RC_FAILURE;
+
+  return TPM_RC_SUCCESS;
+}
+
+TPM_RC
+TPM2_SequenceComplete (const TPMI_DH_OBJECT sequenceHandle,
+                       const TPMS_AUTH_COMMAND *authCommand,
+                       const TPM2B_MAX_BUFFER *buffer,
+                       const TPMI_RH_HIERARCHY hierarchy,
+                       TPM2B_DIGEST *result,
+                       TPMT_TK_HASHCHECK *validation,
+                       TPMS_AUTH_RESPONSE *authResponse)
+{
+  struct grub_tpm2_buffer in;
+  struct grub_tpm2_buffer out;
+  TPM2B_DIGEST resultTmp;
+  TPMT_TK_HASHCHECK validationTmp;
+  TPMS_AUTH_RESPONSE authResponseTmp;
+  TPM_RC responseCode;
+  TPM_RC rc;
+  grub_uint32_t parameterSize;
+
+  if (!authCommand)
+    return TPM_RC_VALUE;
+
+  if (!result)
+    result = &resultTmp;
+  if (!validation)
+    validation = &validationTmp;
+  if (!authResponse)
+    authResponse = &authResponseTmp;
+
+  grub_memset (result, 0, sizeof (*result));
+  grub_memset (validation, 0, sizeof (*validation));
+  grub_memset (authResponse, 0, sizeof (*authResponse));
+
+  /* Marshal */
+  grub_tpm2_buffer_init (&in);
+  grub_tpm2_buffer_pack_u32 (&in, sequenceHandle);
+  grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
+  if (buffer)
+    grub_tpm2_mu_TPM2B_Marshal (&in, buffer->size, buffer->buffer);
+  else
+    grub_tpm2_buffer_pack_u16 (&in, 0);
+  grub_tpm2_buffer_pack_u32 (&in, hierarchy);
+
+  if (in.error)
+    return TPM_RC_FAILURE;
+
+  /* Submit */
+  grub_tpm2_buffer_init (&out);
+  rc = grub_tpm2_submit_command (TPM_ST_SESSIONS, TPM_CC_SequenceComplete,
+                                 &responseCode, &in, &out);
+  if (rc != TPM_RC_SUCCESS)
+    return rc;
+  if (responseCode != TPM_RC_SUCCESS)
+    return responseCode;
+
+  /* Unmarshal */
+  grub_tpm2_buffer_unpack_u32 (&out, &parameterSize);
+  grub_tpm2_mu_TPM2B_DIGEST_Unmarshal (&out, result);
+  grub_tpm2_mu_TPMT_TK_HASHCHECK_Unmarshal (&out, validation);
+  grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal(&out, authResponse);
+  if (out.error)
+    return TPM_RC_FAILURE;
+
+  return TPM_RC_SUCCESS;
+}
+
+TPM_RC
+TPM2_Hash (const TPMS_AUTH_COMMAND *authCommand,
+           const TPM2B_MAX_BUFFER *data,
+           const TPMI_ALG_HASH hashAlg,
+           const TPMI_RH_HIERARCHY hierarchy,
+           TPM2B_DIGEST *outHash,
+           TPMT_TK_HASHCHECK *validation,
+           TPMS_AUTH_RESPONSE *authResponse)
+{
+  TPM_RC rc;
+  struct grub_tpm2_buffer in;
+  struct grub_tpm2_buffer out;
+  TPMS_AUTH_RESPONSE authResponseTmp;
+  TPM2B_DIGEST outHashTmp;
+  TPMT_TK_HASHCHECK validationTmp;
+  TPMI_ST_COMMAND_TAG tag = authCommand ? TPM_ST_SESSIONS : TPM_ST_NO_SESSIONS;
+  TPM_RC responseCode;
+  grub_uint32_t param_size;
+
+  if (hashAlg == TPM_ALG_NULL)
+    return TPM_RC_VALUE;
+
+  if (!outHash)
+    outHash = &outHashTmp;
+  if (!validation)
+    validation = &validationTmp;
+  if (!authResponse)
+    authResponse = &authResponseTmp;
+
+  grub_memset (outHash, 0, sizeof (*outHash));
+  grub_memset (validation, 0, sizeof (*validation));
+  grub_memset (authResponse, 0, sizeof (*authResponse));
+
+  /* Marshal */
+  grub_tpm2_buffer_init (&in);
+  if (authCommand)
+    grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
+  if (data)
+    grub_tpm2_mu_TPM2B_Marshal (&in, data->size, data->buffer);
+  else
+    grub_tpm2_buffer_pack_u16 (&in, 0);
+  grub_tpm2_buffer_pack_u16 (&in, hashAlg);
+  grub_tpm2_buffer_pack_u32 (&in, hierarchy);
+  if (in.error)
+    return TPM_RC_FAILURE;
+
+  /* Submit */
+  grub_tpm2_buffer_init (&out);
+  rc = grub_tpm2_submit_command (tag, TPM_CC_Hash, &responseCode, &in, &out);
+  if (rc != TPM_RC_SUCCESS)
+    return rc;
+  if (responseCode != TPM_RC_SUCCESS)
+    return responseCode;
+
+  /* Unmarshal*/
+  if (tag == TPM_ST_SESSIONS)
+    grub_tpm2_buffer_unpack_u32 (&out, &param_size);
+  grub_tpm2_mu_TPM2B_DIGEST_Unmarshal (&out, outHash);
+  grub_tpm2_mu_TPMT_TK_HASHCHECK_Unmarshal (&out, validation);
+  if (tag == TPM_ST_SESSIONS)
+    grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal (&out, authResponse);
+  if (out.error)
+    return TPM_RC_FAILURE;
+
+  return TPM_RC_SUCCESS;
+}
+
+TPM_RC
+TPM2_VerifySignature (const TPMI_DH_OBJECT keyHandle,
+                      const TPMS_AUTH_COMMAND *authCommand,
+                      const TPM2B_DIGEST *digest,
+                      const TPMT_SIGNATURE *signature,
+                      TPMT_TK_VERIFIED *validation,
+                      TPMS_AUTH_RESPONSE *authResponse)
+{
+  TPM_RC rc;
+  struct grub_tpm2_buffer in;
+  struct grub_tpm2_buffer out;
+  TPMS_AUTH_RESPONSE authResponseTmp;
+  TPMI_ST_COMMAND_TAG tag = authCommand ? TPM_ST_SESSIONS : TPM_ST_NO_SESSIONS;
+  TPMT_TK_VERIFIED validationTmp;
+  TPM_RC responseCode;
+  grub_uint32_t param_size;
+
+  if (!digest || !signature)
+    return TPM_RC_VALUE;
+
+  if (!validation)
+    validation = &validationTmp;
+  if (!authResponse)
+    authResponse = &authResponseTmp;
+
+  grub_memset (validation, 0, sizeof (*validation));
+  grub_memset (authResponse, 0, sizeof (*authResponse));
+
+  /* Marshal */
+  grub_tpm2_buffer_init (&in);
+  if (authCommand)
+    grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
+  grub_tpm2_buffer_pack_u32 (&in, keyHandle);
+  grub_tpm2_mu_TPM2B_Marshal (&in, digest->size, digest->buffer);
+  grub_tpm2_mu_TPMT_SIGNATURE_Marshal (&in, signature);
+  if (in.error)
+    return TPM_RC_FAILURE;
+
+  /* Submit */
+  grub_tpm2_buffer_init (&out);
+  rc = grub_tpm2_submit_command (tag, TPM_CC_VerifySignature, &responseCode, &in, &out);
+  if (rc != TPM_RC_SUCCESS)
+    return rc;
+  if (responseCode != TPM_RC_SUCCESS)
+    return responseCode;
+
+  /* Unmarshal*/
+  if (tag == TPM_ST_SESSIONS)
+    grub_tpm2_buffer_unpack_u32 (&out, &param_size);
+  grub_tpm2_mu_TPMT_TK_VERIFIED_Unmarshal (&out, validation);
+  if (tag == TPM_ST_SESSIONS)
+    grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal (&out, authResponse);
+  if (out.error)
+    return TPM_RC_FAILURE;
+
+  return TPM_RC_SUCCESS;
+}
+
+TPM_RC
+TPM2_PolicyAuthorize (const TPMI_SH_POLICY policySession,
+                      const TPMS_AUTH_COMMAND *authCommand,
+                      const TPM2B_DIGEST *approvedPolicy,
+                      const TPM2B_NONCE *policyRef,
+                      const TPM2B_NAME *keySign,
+                      const TPMT_TK_VERIFIED *checkTicket,
+                      TPMS_AUTH_RESPONSE *authResponse)
+{
+  TPM_RC rc;
+  struct grub_tpm2_buffer in;
+  struct grub_tpm2_buffer out;
+  TPMS_AUTH_RESPONSE authResponseTmp;
+  TPMI_ST_COMMAND_TAG tag = authCommand ? TPM_ST_SESSIONS : TPM_ST_NO_SESSIONS;
+  TPM_RC responseCode;
+  grub_uint32_t param_size;
+
+  if (!approvedPolicy || !keySign || !checkTicket)
+    return TPM_RC_VALUE;
+
+  if (!authResponse)
+    authResponse = &authResponseTmp;
+
+  grub_memset (authResponse, 0, sizeof (*authResponse));
+
+  /* Marshal */
+  grub_tpm2_buffer_init (&in);
+  grub_tpm2_buffer_pack_u32 (&in, policySession);
+  if (authCommand)
+    grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (&in, authCommand);
+  grub_tpm2_mu_TPM2B_Marshal (&in, approvedPolicy->size, approvedPolicy->buffer);
+  if (policyRef)
+    grub_tpm2_mu_TPM2B_Marshal (&in, policyRef->size, policyRef->buffer);
+  else
+    grub_tpm2_buffer_pack_u16 (&in, 0);
+  grub_tpm2_mu_TPM2B_Marshal (&in, keySign->size, keySign->name);
+  grub_tpm2_mu_TPMT_TK_VERIFIED_Marshal (&in, checkTicket);
+  if (in.error)
+    return TPM_RC_FAILURE;
+
+  /* Submit */
+  grub_tpm2_buffer_init (&out);
+  rc = grub_tpm2_submit_command (tag, TPM_CC_PolicyAuthorize, &responseCode, &in, &out);
+  if (rc != TPM_RC_SUCCESS)
+    return rc;
+  if (responseCode != TPM_RC_SUCCESS)
+    return responseCode;
+
+  /* Unmarshal*/
+  if (tag == TPM_ST_SESSIONS)
+    grub_tpm2_buffer_unpack_u32 (&out, &param_size);
+  if (tag == TPM_ST_SESSIONS)
+    grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal (&out, authResponse);
+  if (out.error)
+    return TPM_RC_FAILURE;
+
+  return TPM_RC_SUCCESS;
+}
include/grub/tpm2/internal/functions.h +57 -0

67 lines not on this page · raw message

Signature · 7 lines
2.35.3


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

PATCH v9 16/22 tpm2: Support authorized policy

In reply to Gary Lin via Grub-devel

This commit handles the TPM2_PolicyAuthorize command from the key file in TPM 2.0 Key File format.

TPM2_PolicyAuthorize is the essential command to support authorized policy which allows the users to sign TPM policies with their own keys. Per TPM 2.0 Key File(*1), CommandPolicy for TPM2_PolicyAuthorize comprises 'TPM2B_PUBLIC pubkey', 'TPM2B_DIGEST policy_ref', and 'TPMT_SIGNATURE signature'. To verify the signature, the current policy digest is hashed with the hash algorithm written in 'signature', and then 'signature' is verified with the hashed policy digest and 'pubkey'. Once TPM accepts 'signature', TPM2_PolicyAuthorize is invoked to authorize the signed policy.

To create the key file with authorized policy, here are the pcr-oracle(*2) commands:

# Generate the RSA key and create the authorized policy file

  $ pcr-oracle \
	--rsa-generate-key \
	--private-key policy-key.pem \
	--auth authorized.policy \
	create-authorized-policy 0,2,4,7,9

# Seal the secret with the authorized policy

  $ pcr-oracle \
	--key-format tpm2.0 \
	--auth authorized.policy \
	--input disk-secret.txt \
	--output sealed.key \
	seal-secret

# Sign the predicted PCR policy

  $ pcr-oracle \
	--key-format tpm2.0 \
	--private-key policy-key.pem \
	--from eventlog \
	--stop-event "grub-file=grub.cfg" \
	--after \
	--input sealed.key \
	--output sealed.tpm \
	sign 0,2,4,7.9

Then specify the key file and the key protector to grub.cfg in the EFI system partition:

tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -u <PART_UUID> -P tpm2

For any change in the boot components, just run the 'sign' command again to update the signature in sealed.tpm, and TPM can unseal the key file with the updated PCR policy.

(*1) https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
(*2) https://github.com/okirch/pcr-oracle

---
 grub-core/tpm2/module.c | 84 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
grub-core/tpm2/module.c +84 -0
index 2f3a9375f..1a2d44531 100644
@@ -453,6 +453,87 @@ grub_tpm2_protector_policypcr (TPMI_SH_AUTH_SESSION session,
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+grub_tpm2_protector_policyauthorize (TPMI_SH_AUTH_SESSION session,
+				     struct grub_tpm2_buffer *cmd_buf)
+{
+  TPM2B_PUBLIC pubkey;
+  TPM2B_DIGEST policy_ref;
+  TPMT_SIGNATURE signature;
+  TPM2B_DIGEST pcr_policy;
+  TPM2B_DIGEST pcr_policy_hash;
+  TPMI_ALG_HASH sig_hash;
+  TPMT_TK_VERIFIED verification_ticket;
+  TPM_HANDLE pubkey_handle = 0;
+  TPM2B_NAME pubname;
+  TPM_RC rc;
+  grub_err_t err;
+
+  grub_tpm2_mu_TPM2B_PUBLIC_Unmarshal (cmd_buf, &pubkey);
+  grub_tpm2_mu_TPM2B_DIGEST_Unmarshal (cmd_buf, &policy_ref);
+  grub_tpm2_mu_TPMT_SIGNATURE_Unmarshal (cmd_buf, &signature);
+  if (cmd_buf->error != 0)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+		       N_("Failed to unmarshal the buffer for TPM2_PolicyAuthorize"));
+
+  /* Retrieve Policy Digest */
+  rc = TPM2_PolicyGetDigest (session, NULL, &pcr_policy, NULL);
+  if (rc != TPM_RC_SUCCESS)
+    return grub_error (GRUB_ERR_BAD_DEVICE,
+		       N_("Failed to get policy digest (TPM2_PolicyGetDigest: 0x%x)."),
+		       rc);
+
+  /* Calculate the digest of the polcy for VerifySignature */
+  sig_hash = TPMT_SIGNATURE_get_hash_alg (&signature);
+  if (sig_hash == TPM_ALG_NULL)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+		       N_("Failed to get the hash algorithm of the signature"));
+
+  rc = TPM2_Hash (NULL, (TPM2B_MAX_BUFFER *)&pcr_policy, sig_hash,
+		  TPM_RH_NULL, &pcr_policy_hash, NULL, NULL);
+  if (rc != TPM_RC_SUCCESS)
+    return grub_error (GRUB_ERR_BAD_DEVICE,
+		       N_("Failed to create PCR policy hash (TPM2_Hash: 0x%x)"),
+		       rc);
+
+  /* Load the public key */
+  rc = TPM2_LoadExternal (NULL, NULL, &pubkey, TPM_RH_OWNER,
+			  &pubkey_handle, &pubname, NULL);
+  if (rc != TPM_RC_SUCCESS)
+    return grub_error (GRUB_ERR_BAD_DEVICE,
+		       N_("Failed to load public key (TPM2_LoadExternal: 0x%x)"),
+		       rc);
+
+  /* Verify the signature against the public key and the policy digest */
+  rc = TPM2_VerifySignature (pubkey_handle, NULL, &pcr_policy_hash, &signature,
+			     &verification_ticket, NULL);
+  if (rc != TPM_RC_SUCCESS)
+    {
+      err = grub_error (GRUB_ERR_BAD_DEVICE,
+			N_("Failed to verify signature (TPM2_VerifySignature: 0x%x)"),
+			rc);
+      goto error;
+    }
+
+  /* Authorize the signed policy with the public key and the verification ticket */
+  rc = TPM2_PolicyAuthorize (session, NULL, &pcr_policy, &policy_ref, &pubname,
+			     &verification_ticket, NULL);
+  if (rc != TPM_RC_SUCCESS)
+    {
+      err = grub_error (GRUB_ERR_BAD_DEVICE,
+			N_("Failed to authorize PCR policy (TPM2_PolicyAuthorize: 0x%x)"),
+			rc);
+      goto error;
+    }
+
+  err = GRUB_ERR_NONE;
+
+error:
+  TPM2_FlushContext (pubkey_handle);
+
+  return err;
+}
+
 static grub_err_t
 grub_tpm2_protector_enforce_policy (tpm2key_policy_t policy, TPMI_SH_AUTH_SESSION session)
 {
@@ -472,6 +553,9 @@ grub_tpm2_protector_enforce_policy (tpm2key_policy_t policy, TPMI_SH_AUTH_SESSIO
     case TPM_CC_PolicyPCR:
       err = grub_tpm2_protector_policypcr (session, &buf);
       break;
+    case TPM_CC_PolicyAuthorize:
+      err = grub_tpm2_protector_policyauthorize (session, &buf);
+      break;
     default:
       return grub_error (GRUB_ERR_BAD_ARGUMENT,
 			 N_("Unknown TPM Command: 0x%x"), policy->cmd_code);
Signature · 7 lines
2.35.3


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

PATCH v9 17/22 tpm2: Implement NV index

In reply to Gary Lin via Grub-devel

From: Patrick Colp <[email protected]>

Currently with the TPM2 protector, only SRK mode is supported and NV index support is just a stub. Implement the NV index option.

Note: This only extends support on the unseal path. grub2_protect has not been updated. tpm2-tools can be used to insert a key into the NV index.

An example of inserting a key using tpm2-tools:

# Get random key.
tpm2_getrandom 32 > key.dat

# Create primary object.
tpm2_createprimary -C o -g sha256 -G rsa -c primary.ctx

# Create policy object. pcrs.dat contains the PCR values to seal against. tpm2_startauthsession -S session.dat
tpm2_policypcr -S session.dat -l sha256:7,11 -f pcrs.dat -L policy.dat tpm2_flushcontext session.dat

# Seal key into TPM.
cat key.dat | tpm2_create -C primary.ctx -u key.pub -r key.priv -L policy.dat -i- tpm2_load -C primary.ctx -u key.pub -r key.priv -n sealing.name -c sealing.ctx tpm2_evictcontrol -C o -c sealing.ctx 0x81000000

Then to unseal the key in grub, add this to grub.cfg:

  tpm2_key_protector_init --mode=nv --nvindex=0x81000000 --pcrs=7,11
  cryptomount -u <UUID> --protector tpm2
---
 grub-core/tpm2/module.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
grub-core/tpm2/module.c +20 -5
index 1a2d44531..866275044 100644
@@ -834,12 +834,27 @@ static grub_err_t
 grub_tpm2_protector_nv_recover (const struct grub_tpm2_protector_context *ctx,
 				grub_uint8_t **key, grub_size_t *key_size)
 {
-  (void)ctx;
-  (void)key;
-  (void)key_size;
+  TPM_HANDLE sealed_handle = ctx->nv;
+  tpm2key_policy_t policy_seq = NULL;
+  grub_err_t err;
+
+  /* Create a basic policy sequence based on the given PCR selection */
+  err = grub_tpm2_protector_simple_policy_seq (ctx, &policy_seq);
+  if (err != GRUB_ERR_NONE)
+    goto exit;
+
+  err = grub_tpm2_protector_unseal (policy_seq, sealed_handle, key, key_size);
+
+  /* Pop error messages on success */
+  if (err == GRUB_ERR_NONE)
+    while (grub_error_pop ());
+
+exit:
+  TPM2_FlushContext (sealed_handle);
 
-  return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
-		     N_("NV Index mode is not implemented yet"));
+  grub_tpm2key_free_policy_seq (policy_seq);
+
+  return err;
 }
 
 static grub_err_t
Signature · 7 lines
2.35.3


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

PATCH v9 18/22 cryptodisk: Fallback to passphrase

In reply to Gary Lin via Grub-devel

From: Patrick Colp <[email protected]>

If a protector is specified, but it fails to unlock the disk, fall back to asking for the passphrase. However, an error was set indicating that the protector(s) failed. Later code (e.g., LUKS code) fails as grub_errno is now set. Print the existing errors out first, before proceeding with the passphrase.

---
 grub-core/disk/cryptodisk.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +6 -1
index 0ca1a5c4d..0dbf601c4 100644
@@ -1191,11 +1191,16 @@ grub_cryptodisk_scan_device_real (const char *name,
 		  source->name, source->partition != NULL ? "," : "",
 		  part != NULL ? part : N_("UNKNOWN"), dev->uuid);
       grub_free (part);
-      goto error;
     }
 
   if (!cargs->key_len)
     {
+      if (grub_errno)
+	{
+	  grub_print_error ();
+	  grub_errno = GRUB_ERR_NONE;
+	}
+
       /* Get the passphrase from the user, if no key data. */
       askpass = 1;
       part = grub_partition_get_name (source->partition);
Signature · 7 lines
2.35.3


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

PATCH v9 19/22 cryptodisk: wipe out the cached keys from protectors

In reply to Gary Lin via Grub-devel

An attacker may insert a malicious disk with the same crypto UUID and trick grub2 to mount the fake root. Even though the key from the key protector fails to unlock the fake root, it's not wiped out cleanly so the attacker could dump the memory to retrieve the secret key. To defend such attack, wipe out the cached key when we don't need it.

---
 grub-core/disk/cryptodisk.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +5 -1
index 0dbf601c4..94507ec65 100644
@@ -1349,7 +1349,11 @@ grub_cryptodisk_clear_key_cache (struct grub_cryptomount_args *cargs)
     return;
 
   for (i = 0; cargs->protectors[i]; i++)
-    grub_free (cargs->key_cache[i].key);
+    {
+      if (cargs->key_cache[i].key)
+	grub_memset (cargs->key_cache[i].key, 0, cargs->key_cache[i].key_len);
+      grub_free (cargs->key_cache[i].key);
+    }
 
   grub_free (cargs->key_cache);
 }
Signature · 7 lines
2.35.3


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

PATCH v9 20/22 diskfilter: look up cryptodisk devices first

In reply to Gary Lin via Grub-devel

When using disk auto-unlocking with TPM 2.0, the typical grub.cfg may look like this:

  tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm
  cryptomount -u <PART-UUID> -P tpm2
  search --fs-uuid --set=root <FS-UUID>

Since the disk search order is based on the order of module loading, the attacker could insert a malicious disk with the same FS-UUID root to trick grub2 to boot into th malicious root and further dump memory to steal the unsealed key.

To defend such attack, we can specify the hint provided by 'grub-probe' to search the encrypted partition first:

search --fs-uuid --set=root --hint='cryptouuid/<PART-UUID>' <FS-UUID>

However, for LVM on a encrypted partition, the search hint provided by 'grub-probe' is:

--hint='lvmid/<VG-UUID>/<LV-UUID>'

It doesn't guarantee to look up the logical volume from the encrypted partition, so the attacker may have the chance to fool grub2 to boot into the malicious disk.

To mininize the attack surface, this commit tweaks the disk device search in diskfilter to look up cryptodisk devices first and then others, so that the auto-unlocked disk will be found first, not the attacker's disk.

---
 grub-core/disk/diskfilter.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
grub-core/disk/diskfilter.c +26 -9
index 21e239511..df1992305 100644
@@ -226,15 +226,32 @@ scan_devices (const char *arname)
   int need_rescan;
 
   for (pull = 0; pull < GRUB_DISK_PULL_MAX; pull++)
-    for (p = grub_disk_dev_list; p; p = p->next)
-      if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID
-	  && p->disk_iterate)
-	{
-	  if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
-	    return;
-	  if (arname && is_lv_readable (find_lv (arname), 1))
-	    return;
-	}
+    {
+      /* look up the crytodisk devices first */
+      for (p = grub_disk_dev_list; p; p = p->next)
+	if (p->id == GRUB_DISK_DEVICE_CRYPTODISK_ID
+	    && p->disk_iterate)
+	  {
+	    if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
+	      return;
+	    if (arname && is_lv_readable (find_lv (arname), 1))
+	      return;
+	    break;
+	  }
+
+      /* check the devices other than crytodisk */
+      for (p = grub_disk_dev_list; p; p = p->next)
+	if (p->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
+	  continue;
+	else if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID
+	    && p->disk_iterate)
+	  {
+	    if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
+	      return;
+	    if (arname && is_lv_readable (find_lv (arname), 1))
+	      return;
+	  }
+    }
 
   scan_depth = 0;
   need_rescan = 1;
Signature · 7 lines
2.35.3


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

PATCH v9 21/22 tpm2: Enable tpm2 module for grub-emu

In reply to Gary Lin via Grub-devel

As a preparation to test TPM 2.0 TSS stack with grub-emu, the new option, --tpm-device, is introduced to specify the TPM device for grub-emu so that grub-emu can share the emulated TPM device with the host.

Since grub-emu can directly access the device node on host, it's easy to implement the essential TCG2 command with the standard read/write functions and enable the commands from tpm2 module for grub-emu, so that we can further test TPM key unsealing with grub-emu.

---
 grub-core/Makefile.core.def |  2 ++
 grub-core/kern/emu/main.c   | 11 +++++++-
 grub-core/kern/emu/misc.c   | 51 ++++++++++++++++++++++++++++++++++++
 grub-core/tpm2/tcg2-emu.c   | 52 +++++++++++++++++++++++++++++++++++++
 include/grub/emu/misc.h     |  5 ++++
 5 files changed, 120 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/tpm2/tcg2-emu.c
grub-core/Makefile.core.def +2 -0
index e937f6538..962090d2c 100644
@@ -2567,7 +2567,9 @@ module = {
   common = tpm2/tpm2key.c;
   common = tpm2/tpm2key_asn1_tab.c;
   efi = tpm2/tcg2.c;
+  emu = tpm2/tcg2-emu.c;
   enable = efi;
+  enable = emu;
 };
 
 module = {
grub-core/kern/emu/main.c +10 -1
index 855b11c3d..c10838613 100644
@@ -55,7 +55,7 @@
 static jmp_buf main_env;
 
 /* Store the prefix specified by an argument.  */
-static char *root_dev = NULL, *dir = NULL;
+static char *root_dev = NULL, *dir = NULL, *tpm_dev = NULL;
 
 grub_addr_t grub_modbase = 0;
 
@@ -108,6 +108,7 @@ static struct argp_option options[] = {
   {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
   {"hold",     'H', N_("SECS"),      OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0},
   {"kexec",       'X', 0,      0, N_("use kexec to boot Linux kernels via systemctl (pass twice to enable dangerous fallback to non-systemctl)."), 0},
+  {"tpm-device",  't', N_("DEV"), 0, N_("Set TPM device."), 0},
   { 0, 0, 0, 0, 0, 0 }
 };
 
@@ -168,6 +169,10 @@ argp_parser (int key, char *arg, struct argp_state *state)
     case 'X':
       grub_util_set_kexecute ();
       break;
+    case 't':
+      free (tpm_dev);
+      tpm_dev = xstrdup (arg);
+      break;
 
     case ARGP_KEY_ARG:
       {
@@ -276,6 +281,9 @@ main (int argc, char *argv[])
 
   dir = xstrdup (dir);
 
+  if (tpm_dev)
+    grub_util_tpm_open (tpm_dev);
+
   /* Start GRUB!  */
   if (setjmp (main_env) == 0)
     grub_main ();
@@ -283,6 +291,7 @@ main (int argc, char *argv[])
   grub_fini_all ();
   grub_hostfs_fini ();
   grub_host_fini ();
+  grub_util_tpm_close ();
 
   grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
 
grub-core/kern/emu/misc.c +51 -0

66 lines not on this page · raw message

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

52 lines not on this page · raw message

include/grub/emu/misc.h +5 -0
index 01056954b..1ab8152be 100644
@@ -75,4 +75,9 @@ grub_util_fopen (const char *path, const char *mode);
 
 int grub_util_file_sync (FILE *f);
 
+grub_err_t grub_util_tpm_open (const char *tpm_dev);
+grub_err_t grub_util_tpm_close (void);
+grub_size_t EXPORT_FUNC(grub_util_tpm_read) (void *output, grub_size_t size);
+grub_size_t EXPORT_FUNC(grub_util_tpm_write) (const void *input, grub_size_t size);
+
 #endif /* GRUB_EMU_MISC_H */
Signature · 7 lines
2.35.3


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

PATCH v9 22/22 tests: Add tpm2_test

In reply to Gary Lin via Grub-devel

For TPM 2.0 TSS stack, the TCG2 command sending function is the only difference between the a QEMU instance and grub-emu. To test TPM key unsealing with a QEMU instance, it requires an extra OS image to invoke grub-protect to seal the LUKS key, not only a simple grub-shell rescue CD image. On the other hand, grub-emu can share the emulated TPM device with the host, so that we can seal the LUKS key on host and test key unsealing with grub-emu.

This test script firstly creates a simple LUKS image to be loaded as a loopback device in grub-emu. Then an emulated TPM device is created by swtpm_cuse and PCR 0 and 1 are extended. The LUKS password is sealed by grub-protect against PCR 0 and 1. The last step is to launch grub-emu to load the LUKS image, try to mount the image with tpm2_key_protector_init and cryptomount, and verify the result.

Based on the idea from Michael Chang.

Cc Michael Chang <[email protected]>
---
 Makefile.util.def        |   6 ++
 tests/tpm2_test.in       | 179 +++++++++++++++++++++++++++++++++++++++
 tests/util/grub-shell.in |   6 +-
 3 files changed, 190 insertions(+), 1 deletion(-)
 create mode 100644 tests/tpm2_test.in
Makefile.util.def +6 -0
index a0a3e2cd5..77bbdd453 100644
@@ -1279,6 +1279,12 @@ script = {
   common = tests/asn1_test.in;
 };
 
+script = {
+  testcase = native;
+  name = tpm2_test;
+  common = tests/tpm2_test.in;
+};
+
 program = {
   testcase = native;
   name = example_unit_test;
tests/tpm2_test.in new file +179 -0

179 lines not on this page · raw message

tests/util/grub-shell.in +5 -1
index 496e1bab3..f8642543d 100644
@@ -75,6 +75,7 @@ work_directory=${WORKDIR:-`mktemp -d "${TMPDIR:-/tmp}/grub-shell.XXXXXXXXXX"`} |
 
 . "${builddir}/grub-core/modinfo.sh"
 qemuopts=
+grubemuopts=
 serial_port=com0
 serial_null=
 halt_cmd=halt
@@ -281,6 +282,9 @@ for option in "$@"; do
     --qemu-opts=*)
 	qs=`echo "$option" | sed -e 's/--qemu-opts=//'`
 	qemuopts="$qemuopts $qs" ;;
+    --grub-emu-opts=*)
+	qs=`echo "$option" | sed -e 's/--grub-emu-opts=//'`
+	grubemuopts="$grubemuopts $qs" ;;
     --disk=*)
 	dsk=`echo "$option" | sed -e 's/--disk=//'`
 	if [ ${grub_modinfo_platform} = emu ]; then
@@ -576,7 +580,7 @@ elif [ x$boot = xemu ]; then
     cat >"$work_directory/run.sh" <<EOF
 #! @BUILD_SHEBANG@
 SDIR=\$(realpath -e \${0%/*})
-exec "$(realpath -e "${builddir}")/grub-core/grub-emu" -m "\$SDIR/${device_map##*/}" --memdisk "\$SDIR/${roottar##*/}" -r memdisk -d "/boot/grub"
+exec "$(realpath -e "${builddir}")/grub-core/grub-emu" -m "\$SDIR/${device_map##*/}" --memdisk "\$SDIR/${roottar##*/}" -r memdisk -d "/boot/grub" ${grubemuopts}
 EOF
 else
     cat >"$work_directory/run.sh" <<EOF
Signature · 7 lines
2.35.3


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#23 lore raw
In reply to Gary Lin via Grub-devel

Hey,

Adding Patrick...

Quoted 6 lines · Gary Lin

GIT repo for v9: https://github.com/lcp/grub2/tree/tpm2-unlock-v9

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.

Sadly this patch set have many issues...

The git complains in the following way...

Applying: asn1_test: test module for libtasn1 .git/rebase-apply/patch:1374: new blank line at EOF.
warning: 1 line adds whitespace errors.
Applying: libtasn1: Add the documentation .git/rebase-apply/patch:90: trailing whitespace. .git/rebase-apply/patch:92: trailing whitespace. .git/rebase-apply/patch:99: trailing whitespace. .git/rebase-apply/patch:102: trailing whitespace. .git/rebase-apply/patch:108: trailing whitespace.
warning: squelched 80 whitespace errors
warning: 85 lines add whitespace errors.

The developers manual does not build due to following errors...

grub-dev.texi:616: misplaced {
grub-dev.texi:616: misplaced }
grub-dev.texi:617: misplaced {
grub-dev.texi:617: misplaced }
grub-dev.texi:580: warning: node libtasn1' is next for minilzo' in sectioning but not in menu grub-dev.texi:599: warning: unreferenced node `libtasn1'
grub-dev.texi:599: warning: node minilzo' is prev for libtasn1' in sectioning but not in menu grub-dev.texi:599: warning: node Updating External Code' is up for libtasn1' in sectioning but not in menu grub-dev.texi:499: node Updating External Code' lacks menu item for libtasn1' despite being its Up target

And I have attached the Coverity report. All issues reported there have to be fixed. If you cannot fix an issue you have to explain why you cannot do that and what is potential impact on the code stability, security, etc.

Please do not forget to check code which you add adhere to GRUB coding style [1]. Good example is in grub-core/kern/efi/sb.c too. Of course this requirement does not apply to the libs which you import.

Additionally, please CC [email protected] next time. He is interested in this patch set development.

Daniel

[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Coding-style

Hi,

Please find the latest report on new defect(s) introduced to GRUB found with Coverity Scan.

15 new defect(s) introduced to GRUB found with Coverity Scan.

New defect(s) Reported-by: Coverity Scan
Showing 15 of 15 defect(s)

** CID 435775: Resource leaks (RESOURCE_LEAK) /util/grub-protect.c: 982 in grub_protect_tpm2_add()

________________________________________________________________________________________________________
*** CID 435775: Resource leaks (RESOURCE_LEAK) /util/grub-protect.c: 982 in grub_protect_tpm2_add() 976
977 if (key_size > TPM_MAX_SYM_DATA)
978 {
979 fprintf (stderr,

980     	     _("Input key is too long, maximum allowed size is %u bytes.\n"),
981     	     TPM_MAX_SYM_DATA);
>>>     CID 435775:  Resource leaks  (RESOURCE_LEAK)
>>>     Variable "key" going out of scope leaks the storage it points to.

982 return GRUB_ERR_OUT_OF_RANGE;
983 }
984
985 err = grub_protect_tpm2_get_srk (args, &srk);
986 if (err != GRUB_ERR_NONE)
987 goto exit2;

** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()

________________________________________________________________________________________________________
*** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()
475 */
476 if (leading != 0 && der[len_len + k] == 0x80)
477 return ASN1_DER_ERROR;
478 leading = 0;
479
480 /* check for wrap around */

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT)
"val < ((((1 ? 0 : val) - 1 < 0) ? ~((((1 ? 0 : val) + 1 << 62UL /* sizeof (+val) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : val) + 0)) >> 7)" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

481 if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482 return ASN1_DER_ERROR;
483
484 val = val << 7;
485 val |= der[len_len + k] & 0x7F;
486

** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()

________________________________________________________________________________________________________
*** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()
433 return ASN1_DER_ERROR;
434
435 val0 = 0;
436
437 for (k = 0; k < len; k++)
438 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435773: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0UL : val0) - 1UL < 0UL".

439 if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440 return ASN1_DER_ERROR;
441

442           val0 <<= 7;
443           val0 |= der[len_len + k] & 0x7F;
444           if (!(der[len_len + k] & 0x80))

** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

________________________________________________________________________________________________________
*** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435772: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0U : ((1 ? 0U : ris) + 128U)) - 1U < 0U".

204     	  if (INT_MULTIPLY_OVERFLOW (ris, 128))
205     	    return ASN1_DER_ERROR;
206     	  ris *= 128;

207
208 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209 return ASN1_DER_ERROR;

** CID 435771: (RESOURCE_LEAK)
/util/grub-protect.c: 982 in grub_protect_tpm2_add() /util/grub-protect.c: 971 in grub_protect_tpm2_add() /util/grub-protect.c: 1027 in grub_protect_tpm2_add()

________________________________________________________________________________________________________
*** CID 435771: (RESOURCE_LEAK)
/util/grub-protect.c: 982 in grub_protect_tpm2_add() 976
977 if (key_size > TPM_MAX_SYM_DATA)
978 {
979 fprintf (stderr,

980     	     _("Input key is too long, maximum allowed size is %u bytes.\n"),
981     	     TPM_MAX_SYM_DATA);
>>>     CID 435771:    (RESOURCE_LEAK)
>>>     Variable "grub_drive" going out of scope leaks the storage it points to.

982 return GRUB_ERR_OUT_OF_RANGE;
983 }
984
985 err = grub_protect_tpm2_get_srk (args, &srk);
986 if (err != GRUB_ERR_NONE)
987 goto exit2;
/util/grub-protect.c: 971 in grub_protect_tpm2_add()
965 char *grub_drive = NULL;
966
967 grub_protect_get_grub_drive_for_file (args->tpm2_outfile, &grub_drive);
968
969 err = grub_protect_tpm2_open_device (args->tpm2_device);
970 if (err != GRUB_ERR_NONE)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435771: (RESOURCE_LEAK)
Variable "grub_drive" going out of scope leaks the storage it points to.

971 return err;
972
973 err = grub_protect_read_file (args->tpm2_keyfile, (void **)&key, &key_size);
974 if (err != GRUB_ERR_NONE)
975 goto exit1;
976
/util/grub-protect.c: 1027 in grub_protect_tpm2_add()
1021 exit2:
1022 grub_free (key);
1023
1024 exit1:
1025 grub_protect_tpm2_close_device ();
1026

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435771: (RESOURCE_LEAK)
Variable "grub_drive" going out of scope leaks the storage it points to.

1027 return err;
1028 }
1029

1030     static grub_err_t
1031     grub_protect_tpm2_remove (struct grub_protect_args *args)
1032     {

** CID 435770: Insecure data handling (TAINTED_SCALAR) /grub-core/tpm2/mu.c: 564 in grub_tpm2_mu_TPM2B_Unmarshal()

________________________________________________________________________________________________________
*** CID 435770: Insecure data handling (TAINTED_SCALAR) /grub-core/tpm2/mu.c: 564 in grub_tpm2_mu_TPM2B_Unmarshal()

558     void
559     grub_tpm2_mu_TPM2B_Unmarshal (grub_tpm2_buffer_t buffer,
560     			      TPM2B* p)
561     {

562 grub_tpm2_buffer_unpack_u16 (buffer, &p->size);
563

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435770: Insecure data handling (TAINTED_SCALAR) Using tainted variable "p->size" as a loop boundary.

564 for (grub_uint16_t i = 0; i < p->size; i++)
565 grub_tpm2_buffer_unpack_u8 (buffer, &p->buffer[i]);
566 }
567
568 void
569 grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal (grub_tpm2_buffer_t buffer,

** CID 435769: Null pointer dereferences (FORWARD_NULL)

________________________________________________________________________________________________________
*** CID 435769: Null pointer dereferences (FORWARD_NULL) /grub-core/tpm2/module.c: 222 in grub_tpm2_protector_srk_read_file()
216 *buffer = read_buffer;
217 *buffer_size = file_size;
218
219 err = GRUB_ERR_NONE;
220

221     error:
>>>     CID 435769:  Null pointer dereferences  (FORWARD_NULL)
>>>     Passing null pointer "file" to "grub_file_close", which dereferences it.

222 grub_file_close (file);
223
224 return err;
225 }
226
227 static grub_err_t

** CID 435768: (CONSTANT_EXPRESSION_RESULT)
/grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der() /grub-core/lib/libtasn1/lib/decoding.c: 217 in asn1_get_tag_der()

________________________________________________________________________________________________________
*** CID 435768: (CONSTANT_EXPRESSION_RESULT)
/grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
    CID 435768:    (CONSTANT_EXPRESSION_RESULT)
    "ris < (((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0) ? ~((((1 ? 0 : ((1 ? 0 : ris) + 128)) + 1 << 30UL /* sizeof (+((1 ? 0 : ris) + 128)) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : ((1 ? 0 : ris) + 128)) + 0)) / 128" is always false regardless of the values of its operands. This occurs as the second operand of "?:".
204     	  if (INT_MULTIPLY_OVERFLOW (ris, 128))
205     	    return ASN1_DER_ERROR;
206     	  ris *= 128;

207
208 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209 return ASN1_DER_ERROR;
/grub-core/lib/libtasn1/lib/decoding.c: 217 in asn1_get_tag_der()
211 punt++;
212 }
213
214 if (punt >= der_len)
215 return ASN1_DER_ERROR;
216

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
    CID 435768:    (CONSTANT_EXPRESSION_RESULT)
    "ris < (((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0) ? ~((((1 ? 0 : ((1 ? 0 : ris) + 128)) + 1 << 30UL /* sizeof (+((1 ? 0 : ris) + 128)) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : ((1 ? 0 : ris) + 128)) + 0)) / 128" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

217 if (INT_MULTIPLY_OVERFLOW (ris, 128))
218 return ASN1_DER_ERROR;
219 ris *= 128;
220
221 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
222 return ASN1_DER_ERROR;

** CID 435767: Insecure data handling (TAINTED_SCALAR)

________________________________________________________________________________________________________
*** CID 435767: Insecure data handling (TAINTED_SCALAR) /grub-core/tpm2/mu.c: 974 in grub_tpm2_mu_TPML_DIGEST_Unmarshal()

968     grub_tpm2_mu_TPML_DIGEST_Unmarshal (grub_tpm2_buffer_t buf,
969     				    TPML_DIGEST* digest)
970     {

971 grub_tpm2_buffer_unpack_u32 (buf, &digest->count);
972
973 for (grub_uint32_t i = 0; i < digest->count; i++)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435767: Insecure data handling (TAINTED_SCALAR)
Passing tainted expression "digest->digests[i]->size" to "grub_tpm2_mu_TPM2B_DIGEST_Unmarshal", which uses it as a loop boundary.

974 grub_tpm2_mu_TPM2B_DIGEST_Unmarshal (buf, &digest->digests[i]);
975 }
976
977 void
978 grub_tpm2_mu_TPMS_SIGNATURE_RSA_Unmarshal (grub_tpm2_buffer_t buffer,
979 TPMS_SIGNATURE_RSA *rsa)

** CID 435766: Memory - corruptions (OVERRUN)

________________________________________________________________________________________________________
*** CID 435766: Memory - corruptions (OVERRUN) /grub-core/lib/libtasn1/lib/decoding.c: 1204 in asn1_der_decoding2()
1198 }
1199
1200 DECR_LEN (ider_len, len2);
1201

1202     	      tlen = strlen (temp);
1203     	      if (tlen > 0)
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435766: Memory - corruptions (OVERRUN)
Allocating insufficient memory for the terminating null of the string.

1204 _asn1_set_value (p, temp, tlen);
1205

1206     	      counter += len2;
1207     	      move = RIGHT;
1208     	      break;
1209     	    case ASN1_ETYPE_OCTET_STRING:

** CID 435765: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()

________________________________________________________________________________________________________
*** CID 435765: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()
433 return ASN1_DER_ERROR;
434
435 val0 = 0;
436
437 for (k = 0; k < len; k++)
438 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435765: Integer handling issues (CONSTANT_EXPRESSION_RESULT)
"val0 < ((((1 ? 0 : val0) - 1 < 0) ? ~((((1 ? 0 : val0) + 1 << 62UL /* sizeof (+val0) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : val0) + 0)) >> 7)" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

439 if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440 return ASN1_DER_ERROR;
441

442           val0 <<= 7;
443           val0 |= der[len_len + k] & 0x7F;
444           if (!(der[len_len + k] & 0x80))

** CID 435764: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 137 in asn1_get_length_der()

________________________________________________________________________________________________________
*** CID 435764: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 137 in asn1_get_length_der()
131 punt = 1;
132 if (k)

133     	{			/* definite length method */
134     	  ans = 0;
135     	  while (punt <= k && punt < der_len)
136     	    {
>>>     CID 435764:  Integer handling issues  (NO_EFFECT)
>>>     This less-than-zero comparison of an unsigned value is never true. "(1 ? 0U : ((1 ? 0U : ans) + 256U)) - 1U < 0U".
137     	      if (INT_MULTIPLY_OVERFLOW (ans, 256))
138     		return -2;
139     	      ans *= 256;

140
141 if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
142 return -2;

** CID 435763: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 137 in asn1_get_length_der()

________________________________________________________________________________________________________
*** CID 435763: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 137 in asn1_get_length_der()
131 punt = 1;
132 if (k)

133     	{			/* definite length method */
134     	  ans = 0;
135     	  while (punt <= k && punt < der_len)
136     	    {
>>>     CID 435763:  Integer handling issues  (CONSTANT_EXPRESSION_RESULT)
>>>     "ans < (((1 ? 0 : ((1 ? 0 : ans) + 256)) - 1 < 0) ? ~((((1 ? 0 : ((1 ? 0 : ans) + 256)) + 1 << 30UL /* sizeof (+((1 ? 0 : ans) + 256)) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : ((1 ? 0 : ans) + 256)) + 0)) / 256" is always false regardless of the values of its operands. This occurs as the second operand of "?:".
137     	      if (INT_MULTIPLY_OVERFLOW (ans, 256))
138     		return -2;
139     	      ans *= 256;

140
141 if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
142 return -2;

** CID 435762: Memory - corruptions (OVERRUN) /grub-core/lib/libtasn1/lib/coding.c: 152 in _asn1_tag_der()

________________________________________________________________________________________________________
*** CID 435762: Memory - corruptions (OVERRUN) /grub-core/lib/libtasn1/lib/coding.c: 152 in _asn1_tag_der()

146     	  if (k > ASN1_MAX_TAG_SIZE - 1)
147     	    break;		/* will not encode larger tags */
148     	}
149           *ans_len = k + 1;
150           while (k--)
151     	ans[*ans_len - 1 - k] = temp[k] + 128;
>>>     CID 435762:  Memory - corruptions  (OVERRUN)
>>>     Overrunning array of 4 bytes at byte offset 4 by dereferencing pointer "ans + (*ans_len - 1)".

152 ans[*ans_len - 1] -= 128;
153 }
154 }
155
156 /**
157 * asn1_octet_der:

** CID 435761: Integer handling issues (NEGATIVE_RETURNS)

________________________________________________________________________________________________________
*** CID 435761: Integer handling issues (NEGATIVE_RETURNS) /util/grub-protect.c: 260 in grub_protect_read_file()
254 err = GRUB_ERR_FILE_READ_ERROR;
255 goto exit1;
256 }
257
258 rewind (f);
259

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435761: Integer handling issues (NEGATIVE_RETURNS) "len" is passed to a parameter that cannot be negative.

260 buf = grub_malloc (len);
261 if (buf == NULL)
262 {
263 err = GRUB_ERR_OUT_OF_MEMORY;
264 goto exit1;
265 }

________________________________________________________________________________________________________
To view the defects in Coverity Scan visit, https://u15810271.ct.sendgrid.net/ls/click?upn=HRESupC-2F2Czv4BOaCWWCy7my0P0qcxCbhZ31OYv50yrkVfgDS2kXmgKKqI9QA-2BVNoKK9q7SFpWGd3m11cukzsw-3D-3DHheQ_pqn7nzZAiyf0hBS-2Bvrr6KCOYHbMcAEkU4V1pyJ3pOgs3pZi4ADKvKcb1upZ6ha0yK6rBruvdhfB2bMQUK0oTfB-2BmmO7FWTlT8Q5-2BK-2FhLtvLedURDxKrS-2F-2Fu4NC-2FvDExIZEGC5875AQV99KngzPjcntrJ9pI7881eNQK6iUKSF8c8EVFpPotSz8Gdch4PkS4ifsHhJXRzb9tKRTpiDXPV3A-3D-3D

To manage Coverity Scan email notifications for "[email protected]", click https://u15810271.ct.sendgrid.net/ls/click?upn=HRESupC-2F2Czv4BOaCWWCy7my0P0qcxCbhZ31OYv50yped04pjJnmXOsUBtKYNIXxaL5v6wpPWFsnxwMNLnBswWpckve7onnQrbVzEOkeh9Xj7WV2EPa18sBXMRAENqUVq6r9iyHPR1kC40r-2F2raBCBdK3ZqYVQCFAD4uZPmTSzA-3D-za2_pqn7nzZAiyf0hBS-2Bvrr6KCOYHbMcAEkU4V1pyJ3pOgs3pZi4ADKvKcb1upZ6ha0yemkjYC26UmaxrZoePKKFgIJtj0eah3FtFeSTXcGscF-2FK-2BIKRBDpumaA31JWJVTNFjO4VU9bQuKiYkGvXD3gO7fIzDQbTR7-2BPp8EcxE7go0X9ZqODMbiGnyEH-2B-2Bl58uCmUGTa0JXtHEUpUXG-2FlgqaUg-3D-3D

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

#24 lore raw
Quoted 12 lines · Daniel Kiper

Hey,

Adding Patrick...

Quoted 6 lines · Gary Lin

GIT repo for v9: https://github.com/lcp/grub2/tree/tpm2-unlock-v9

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 7 lines

Sadly this patch set have many issues...

The git complains in the following way...

Applying: asn1_test: test module for libtasn1 .git/rebase-apply/patch:1374: new blank line at EOF.
warning: 1 line adds whitespace errors.

Will be fixed in v10.

Quoted 9 lines

Applying: libtasn1: Add the documentation .git/rebase-apply/patch:90: trailing whitespace. .git/rebase-apply/patch:92: trailing whitespace. .git/rebase-apply/patch:99: trailing whitespace. .git/rebase-apply/patch:102: trailing whitespace. .git/rebase-apply/patch:108: trailing whitespace.
warning: squelched 80 whitespace errors
warning: 85 lines add whitespace errors.

Those whitespaces naturally exist in the two 'patch' files added in this commit, so it's hard to avoid the warning.

Quoted 12 lines

The developers manual does not build due to following errors...

grub-dev.texi:616: misplaced {
grub-dev.texi:616: misplaced }
grub-dev.texi:617: misplaced {
grub-dev.texi:617: misplaced }
grub-dev.texi:580: warning: node libtasn1' is next for minilzo' in sectioning but not in menu grub-dev.texi:599: warning: unreferenced node `libtasn1'
grub-dev.texi:599: warning: node minilzo' is prev for libtasn1' in sectioning but not in menu grub-dev.texi:599: warning: node Updating External Code' is up for libtasn1' in sectioning but not in menu grub-dev.texi:499: node Updating External Code' lacks menu item for libtasn1' despite being its Up target

Will be fixed in v10.

Quoted 5 lines

And I have attached the Coverity report. All issues reported there have to be fixed. If you cannot fix an issue you have to explain why you cannot do that and what is potential impact on the code stability, security, etc.

I'm working on that and will fix them in v10.

Quoted 7 lines

Please do not forget to check code which you add adhere to GRUB coding style [1]. Good example is in grub-core/kern/efi/sb.c too. Of course this requirement does not apply to the libs which you import.

Additionally, please CC [email protected] next time. He is interested in this patch set development.

Added Patrick to my patch sending script.

Thanks,

Gary Lin

Quoted 3 lines

Daniel

[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Coding-style

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

#25 lore raw
Quoted 31 lines · Gary Lin via Grub-devel
Quoted 12 lines · Daniel Kiper

Hey,

Adding Patrick...

Quoted 6 lines · Gary Lin

GIT repo for v9: https://github.com/lcp/grub2/tree/tpm2-unlock-v9

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 7 lines

Sadly this patch set have many issues...

The git complains in the following way...

Applying: asn1_test: test module for libtasn1 .git/rebase-apply/patch:1374: new blank line at EOF.
warning: 1 line adds whitespace errors.

Will be fixed in v10.

Quoted 9 lines

Applying: libtasn1: Add the documentation .git/rebase-apply/patch:90: trailing whitespace. .git/rebase-apply/patch:92: trailing whitespace. .git/rebase-apply/patch:99: trailing whitespace. .git/rebase-apply/patch:102: trailing whitespace. .git/rebase-apply/patch:108: trailing whitespace.
warning: squelched 80 whitespace errors
warning: 85 lines add whitespace errors.

Quoted 2 lines

Those whitespaces naturally exist in the two 'patch' files added in this commit, so it's hard to avoid the warning.

OK, please ignore my comment then...

Daniel

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

#26 lore raw
In reply to Daniel Kiper
Quoted 2 lines · Daniel Kiper

Hey,

--8<--

Quoted 6 lines

And I have attached the Coverity report. All issues reported there have to be fixed. If you cannot fix an issue you have to explain why you cannot do that and what is potential impact on the code stability, security, etc.

I have went through all the coverity issues. There are 6 issues in the TPM2 stack and the utility:

CID 435775, CID 435771, CID 435770, CID 435769, CID 435767, CID 435761

Those will be fixed in the next version.

The 9 issues are from libtasn1 and gnulib.

There are two memory corruption issue: CID 435762 and CID 435766, and I've filed the issues in libtasn1 upstream.

- CID 435762
https://gitlab.com/gnutls/libtasn1/-/issues/49
This is a valid case. However, the only exploitable function, _asn1_insert_tag_der(), is disabled in grub2 patch, so the severity is low. I have a quick fix but upstream would like to fix it in another way.
- CID 435766
https://gitlab.com/gnutls/libtasn1/-/issues/50
IMO, this is false positive, but I need libtasn1 upstream to confirm that.

Then, the remaining 7 Integer handling issues are involved with the macros from gnulib, and I believe those are false positive.

The following are my analyses:

________________________________________________________________________________________________________
*** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()
475 */
476 if (leading != 0 && der[len_len + k] == 0x80)
477 return ASN1_DER_ERROR;
478 leading = 0;
479
480 /* check for wrap around */

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT)
"val < ((((1 ? 0 : val) - 1 < 0) ? ~((((1 ? 0 : val) + 1 << 62UL /* sizeof (+val) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : val) + 0)) >> 7)" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

481 if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482 return ASN1_DER_ERROR;
483
484 val = val << 7;
485 val |= der[len_len + k] & 0x7F;
486

Here are the related macros:

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define INT_LEFT_SHIFT_OVERFLOW(a, b) \

  INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
                                 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
  ((a) < 0                                              \
   ? (a) < (min) >> (b)                                 \

: (max) >> (b) < (a))

The statement in question is expanded "(a) < (min) >> (b)" of INT_LEFT_SHIFT_RANGE_OVERFLOW. Since 'val' is 'uint64_t', '(a) < 0' is always false, so the result of that statement doen't matter.

________________________________________________________________________________________________________
*** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()
433 return ASN1_DER_ERROR;
434
435 val0 = 0;
436
437 for (k = 0; k < len; k++)
438 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435773: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0UL : val0) - 1UL < 0UL".

439 if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440 return ASN1_DER_ERROR;
441

442           val0 <<= 7;
443           val0 |= der[len_len + k] & 0x7F;
444           if (!(der[len_len + k] & 0x80))

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

The statement in question is the expanded 'EXPR_SIGNED'. Since that macro is to test whether the variable is signed or not. For 'uint64_t val0', it's expected to be always false.

________________________________________________________________________________________________________
*** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435772: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0U : ((1 ? 0U : ris) + 128U)) - 1U < 0U".

204     	  if (INT_MULTIPLY_OVERFLOW (ris, 128))
205     	    return ASN1_DER_ERROR;
206     	  ris *= 128;

207
208 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209 return ASN1_DER_ERROR;

Here are the related macros:

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

The statement in question is the expanded 'EXPR_SIGNED(_GL_INT_CONVERT (ris, 128))'. Since 'ris' is 'unsigned int', it's expected that 'EXPR_SIGNED' always returns false.

________________________________________________________________________________________________________
*** CID 435768: (CONSTANT_EXPRESSION_RESULT)
/grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
    CID 435768:    (CONSTANT_EXPRESSION_RESULT)
    "ris < (((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0) ? ~((((1 ? 0 : ((1 ? 0 : ris) + 128)) + 1 << 30UL /* sizeof (+((1 ? 0 : ris) + 128)) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : ((1 ? 0 : ris) + 128)) + 0)) / 128" is always false regardless of the values of its operands. This occurs as the second operand of "?:".
204     	  if (INT_MULTIPLY_OVERFLOW (ris, 128))
205     	    return ASN1_DER_ERROR;
206     	  ris *= 128;

207
208 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209 return ASN1_DER_ERROR;
/grub-core/lib/libtasn1/lib/decoding.c: 217 in asn1_get_tag_der()
211 punt++;
212 }
213
214 if (punt >= der_len)
215 return ASN1_DER_ERROR;
216

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
    CID 435768:    (CONSTANT_EXPRESSION_RESULT)
    "ris < (((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0) ? ~((((1 ? 0 : ((1 ? 0 : ris) + 128)) + 1 << 30UL /* sizeof (+((1 ? 0 : ris) + 128)) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : ((1 ? 0 : ris) + 128)) + 0)) / 128" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

217 if (INT_MULTIPLY_OVERFLOW (ris, 128))
218 return ASN1_DER_ERROR;
219 ris *= 128;
220
221 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
222 return ASN1_DER_ERROR;

Here are the related macros:

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max)     \
  ((b) < 0                                              \
   ? ((a) < 0                                           \
      ? (a) < (max) / (b)                               \
      : (b) == -1                                       \
      ? 0                                               \
      : (min) / (b) < (a))                              \
   : (b) == 0                                           \
   ? 0                                                  \
   : ((a) < 0                                           \
      ? (a) < (min) / (b)                               \
      : (max) / (b) < (a)))
# define _GL_MULTIPLY_OVERFLOW(a, b, min, max)                           \
   (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a))))       \
    || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))

#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \

  op_result_overflow (a, b,                                     \
                      _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
                      _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))

#define INT_MULTIPLY_OVERFLOW(a, b) \
_GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)

The statement in question is expanded from '(a) < (min) / (b)' in INT_MULTIPLY_RANGE_OVERFLOW.

'(a) < (min) / (b)'
=> '(a) < (_GL_INT_MINIMUM (_GL_INT_CONVERT (a, b))) / (b)' => '(ris) < (_GL_INT_MINIMUM (_GL_INT_CONVERT (ris, 128))) / (128)'

Since 'ris' is 'unsigned int', '(a) < 0' is always false, so the result of '(a) < (min) / (b)' doesn't matter.

________________________________________________________________________________________________________
*** CID 435765: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()
433 return ASN1_DER_ERROR;
434
435 val0 = 0;
436
437 for (k = 0; k < len; k++)
438 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435765: Integer handling issues (CONSTANT_EXPRESSION_RESULT)
"val0 < ((((1 ? 0 : val0) - 1 < 0) ? ~((((1 ? 0 : val0) + 1 << 62UL /* sizeof (+val0) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : val0) + 0)) >> 7)" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

439 if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440 return ASN1_DER_ERROR;
441

442           val0 <<= 7;
443           val0 |= der[len_len + k] & 0x7F;
444           if (!(der[len_len + k] & 0x80))

Here are the related macros:

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define INT_LEFT_SHIFT_OVERFLOW(a, b) \

  INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
                                 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
  ((a) < 0                                              \
   ? (a) < (min) >> (b)                                 \

: (max) >> (b) < (a))

The statement in question is expanded "(a) < (min) >> (b)" of INT_LEFT_SHIFT_RANGE_OVERFLOW. Since 'val0' is 'uint64_t', '(a) < 0' is always false, so the result of that statement doesn't matter.

________________________________________________________________________________________________________
*** CID 435764: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 137 in asn1_get_length_der()
131 punt = 1;
132 if (k)

133     	{			/* definite length method */
134     	  ans = 0;
135     	  while (punt <= k && punt < der_len)
136     	    {
>>>     CID 435764:  Integer handling issues  (NO_EFFECT)
>>>     This less-than-zero comparison of an unsigned value is never true. "(1 ? 0U : ((1 ? 0U : ans) + 256U)) - 1U < 0U".
137     	      if (INT_MULTIPLY_OVERFLOW (ans, 256))
138     		return -2;
139     	      ans *= 256;

140
141 if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
142 return -2;

Here are the related macros:

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \
   : _GL_INT_CONVERT (e, 0))
#define _GL_INT_MAXIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? _GL_SIGNED_INT_MAXIMUM (e)                                         \
   : _GL_INT_NEGATE_CONVERT (e, 1))
#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow)        \
  op_result_overflow (a, b,                                     \
                      _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
                      _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))

The statement in question is EXPR_SIGNED from either _GL_INT_MINIMUM or _GL_INT_MAXIMUM.

EXPR_SIGNED (_GL_INT_CONVERT (ans, 256))
=> _GL_INT_NEGATE_CONVERT (_GL_INT_CONVERT (ans, 256), 1) < 0

The macro is used to test whether ans and 256 are signed or not, so it's expected to be always false.

________________________________________________________________________________________________________
*** CID 435763: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 137 in asn1_get_length_der()
131 punt = 1;
132 if (k)

133     	{			/* definite length method */
134     	  ans = 0;
135     	  while (punt <= k && punt < der_len)
136     	    {
>>>     CID 435763:  Integer handling issues  (CONSTANT_EXPRESSION_RESULT)
>>>     "ans < (((1 ? 0 : ((1 ? 0 : ans) + 256)) - 1 < 0) ? ~((((1 ? 0 : ((1 ? 0 : ans) + 256)) + 1 << 30UL /* sizeof (+((1 ? 0 : ans) + 256)) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : ((1 ? 0 : ans) + 256)) + 0)) / 256" is always false regardless of the values of its operands. This occurs as the second operand of "?:".
137     	      if (INT_MULTIPLY_OVERFLOW (ans, 256))
138     		return -2;
139     	      ans *= 256;

140
141 if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
142 return -2;

Here are the related macros:

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max)     \
  ((b) < 0                                              \
   ? ((a) < 0                                           \
      ? (a) < (max) / (b)                               \
      : (b) == -1                                       \
      ? 0                                               \
      : (min) / (b) < (a))                              \
   : (b) == 0                                           \
   ? 0                                                  \
   : ((a) < 0                                           \
      ? (a) < (min) / (b)                               \
      : (max) / (b) < (a)))
# define _GL_MULTIPLY_OVERFLOW(a, b, min, max)                           \
   (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a))))       \
    || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))

#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \

  op_result_overflow (a, b,                                     \
                      _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
                      _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))

#define INT_MULTIPLY_OVERFLOW(a, b) \
_GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)

The statement in question is expanded from '(a) < (min) / (b)' in INT_MULTIPLY_RANGE_OVERFLOW.

'(a) < (min) / (b)'
=> '(a) < (_GL_INT_MINIMUM (_GL_INT_CONVERT (a, b))) / (b)' => '(ans) < (_GL_INT_MINIMUM (_GL_INT_CONVERT (ans, 256))) / (256)'

Since 'ans' is 'unsigned int', '(a) < 0' is always false, so the result of '(a) < (min) / (b)' doesn't matter.

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

#27 lore raw
Quoted 5 lines · Gary Lin via Grub-devel
Quoted 2 lines · Daniel Kiper

Hey,

--8<--

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

And I have attached the Coverity report. All issues reported there have to be fixed. If you cannot fix an issue you have to explain why you cannot do that and what is potential impact on the code stability, security, etc.

I have went through all the coverity issues. There are 6 issues in the TPM2 stack and the utility:

[...]

Any progress on this? You are blocking another patch set which depends on some code which you introduce. If there is no progress here I will ask an author of the other patch set to resume the work and queue your patch set as a second one to merge.

Daniel

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

#28 lore raw
Quoted 14 lines · Daniel Kiper
Quoted 5 lines · Gary Lin via Grub-devel
Quoted 2 lines · Daniel Kiper

Hey,

--8<--

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

And I have attached the Coverity report. All issues reported there have to be fixed. If you cannot fix an issue you have to explain why you cannot do that and what is potential impact on the code stability, security, etc.

I have went through all the coverity issues. There are 6 issues in the TPM2 stack and the utility:

Quoted 7 lines

[...]

Any progress on this? You are blocking another patch set which depends on some code which you introduce. If there is no progress here I will ask an author of the other patch set to resume the work and queue your patch set as a second one to merge.

I was waiting for upstream fix for CID 435762(*). It can be fixed by tweaking an if statement slightly but upstream prefers code refactoring to remove the loops. Anyway, the only potentially vulnerable call path is disabled in my patch set, so we can choose to leave the issue for the later update or just apply a quick fix.

Cheers,

Gary Lin

(*) https://gitlab.com/gnutls/libtasn1/-/issues/49

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

#29 lore raw
In reply to Gary Lin via Grub-devel
Quoted 74 lines · Gary Lin via Grub-devel
Quoted 2 lines · Daniel Kiper

Hey,

--8<--

Quoted 6 lines

And I have attached the Coverity report. All issues reported there have to be fixed. If you cannot fix an issue you have to explain why you cannot do that and what is potential impact on the code stability, security, etc.

I have went through all the coverity issues. There are 6 issues in the TPM2 stack and the utility:

CID 435775, CID 435771, CID 435770, CID 435769, CID 435767, CID 435761

Those will be fixed in the next version.

The 9 issues are from libtasn1 and gnulib.

There are two memory corruption issue: CID 435762 and CID 435766, and I've filed the issues in libtasn1 upstream.

- CID 435762
https://gitlab.com/gnutls/libtasn1/-/issues/49
This is a valid case. However, the only exploitable function, _asn1_insert_tag_der(), is disabled in grub2 patch, so the severity is low. I have a quick fix but upstream would like to fix it in another way.
- CID 435766
https://gitlab.com/gnutls/libtasn1/-/issues/50
IMO, this is false positive, but I need libtasn1 upstream to confirm that.

Then, the remaining 7 Integer handling issues are involved with the macros from gnulib, and I believe those are false positive.

The following are my analyses:

________________________________________________________________________________________________________
*** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()
475 */
476 if (leading != 0 && der[len_len + k] == 0x80)
477 return ASN1_DER_ERROR;
478 leading = 0;
479
480 /* check for wrap around */

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT)
"val < ((((1 ? 0 : val) - 1 < 0) ? ~((((1 ? 0 : val) + 1 << 62UL /* sizeof (+val) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : val) + 0)) >> 7)" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

481 if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482 return ASN1_DER_ERROR;
483
484 val = val << 7;
485 val |= der[len_len + k] & 0x7F;
486

Here are the related macros:

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define INT_LEFT_SHIFT_OVERFLOW(a, b) \

  INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
                                 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
Quoted 8 lines
#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
  ((a) < 0                                              \
   ? (a) < (min) >> (b)                                 \

: (max) >> (b) < (a))

The statement in question is expanded "(a) < (min) >> (b)" of INT_LEFT_SHIFT_RANGE_OVERFLOW. Since 'val' is 'uint64_t', '(a) < 0' is always false, so the result of that statement doen't matter.

Something is missing and/or requires clarification/expansion here. AFAICT at least your description completely ignores "(max) >> (b) < (a)" expression result.

Quoted 47 lines

________________________________________________________________________________________________________
*** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()
433 return ASN1_DER_ERROR;
434
435 val0 = 0;
436
437 for (k = 0; k < len; k++)
438 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435773: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0UL : val0) - 1UL < 0UL".

439 if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440 return ASN1_DER_ERROR;
441

442           val0 <<= 7;
443           val0 |= der[len_len + k] & 0x7F;
444           if (!(der[len_len + k] & 0x80))

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

The statement in question is the expanded 'EXPR_SIGNED'. Since that macro is to test whether the variable is signed or not. For 'uint64_t val0', it's expected to be always false.

________________________________________________________________________________________________________
*** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435772: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0U : ((1 ? 0U : ris) + 128U)) - 1U < 0U".

204     	  if (INT_MULTIPLY_OVERFLOW (ris, 128))
205     	    return ASN1_DER_ERROR;
206     	  ris *= 128;

207
208 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209 return ASN1_DER_ERROR;

Here are the related macros:

Quoted 7 lines

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

The statement in question is the expanded 'EXPR_SIGNED(_GL_INT_CONVERT (ris, 128))'. Since 'ris' is 'unsigned int', it's expected that 'EXPR_SIGNED' always returns false.

At least INT_MULTIPLY_OVERFLOW definitions is missing.

Please double check other analysis too. If you provide correct analysis for all non-fixable CIDs I will mark them as ignored. It would be nice if updated analysis is a part of latest version of the patch set.

Daniel

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

#30 lore raw
Quoted 80 lines · Daniel Kiper
Quoted 79 lines · Gary Lin via Grub-devel
Quoted 2 lines · Daniel Kiper

Hey,

--8<--

Quoted 6 lines

And I have attached the Coverity report. All issues reported there have to be fixed. If you cannot fix an issue you have to explain why you cannot do that and what is potential impact on the code stability, security, etc.

I have went through all the coverity issues. There are 6 issues in the TPM2 stack and the utility:

CID 435775, CID 435771, CID 435770, CID 435769, CID 435767, CID 435761

Those will be fixed in the next version.

The 9 issues are from libtasn1 and gnulib.

There are two memory corruption issue: CID 435762 and CID 435766, and I've filed the issues in libtasn1 upstream.

- CID 435762
https://gitlab.com/gnutls/libtasn1/-/issues/49
This is a valid case. However, the only exploitable function, _asn1_insert_tag_der(), is disabled in grub2 patch, so the severity is low. I have a quick fix but upstream would like to fix it in another way.
- CID 435766
https://gitlab.com/gnutls/libtasn1/-/issues/50
IMO, this is false positive, but I need libtasn1 upstream to confirm that.

Then, the remaining 7 Integer handling issues are involved with the macros from gnulib, and I believe those are false positive.

The following are my analyses:

________________________________________________________________________________________________________
*** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()
475 */
476 if (leading != 0 && der[len_len + k] == 0x80)
477 return ASN1_DER_ERROR;
478 leading = 0;
479
480 /* check for wrap around */

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT)
"val < ((((1 ? 0 : val) - 1 < 0) ? ~((((1 ? 0 : val) + 1 << 62UL /* sizeof (+val) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : val) + 0)) >> 7)" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

481 if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482 return ASN1_DER_ERROR;
483
484 val = val << 7;
485 val |= der[len_len + k] & 0x7F;
486

Here are the related macros:

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define INT_LEFT_SHIFT_OVERFLOW(a, b) \

  INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
                                 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
  ((a) < 0                                              \
   ? (a) < (min) >> (b)                                 \

: (max) >> (b) < (a))

Quoted 8 lines
Quoted 3 lines

The statement in question is expanded "(a) < (min) >> (b)" of INT_LEFT_SHIFT_RANGE_OVERFLOW. Since 'val' is 'uint64_t', '(a) < 0' is always false, so the result of that statement doen't matter.

Something is missing and/or requires clarification/expansion here. AFAICT at least your description completely ignores "(max) >> (b) < (a)" expression result.

Because Coverity only complained "(a) < (min) >> (b)". "(max) >> (b) < (a)" does the right thing to check whether 'a' is overflowed after the left shift.

Quoted 51 lines
Quoted 51 lines

________________________________________________________________________________________________________
*** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()
433 return ASN1_DER_ERROR;
434
435 val0 = 0;
436
437 for (k = 0; k < len; k++)
438 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435773: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0UL : val0) - 1UL < 0UL".

439 if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440 return ASN1_DER_ERROR;
441

442           val0 <<= 7;
443           val0 |= der[len_len + k] & 0x7F;
444           if (!(der[len_len + k] & 0x80))

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

The statement in question is the expanded 'EXPR_SIGNED'. Since that macro is to test whether the variable is signed or not. For 'uint64_t val0', it's expected to be always false.

________________________________________________________________________________________________________
*** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435772: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0U : ((1 ? 0U : ris) + 128U)) - 1U < 0U".

204     	  if (INT_MULTIPLY_OVERFLOW (ris, 128))
205     	    return ASN1_DER_ERROR;
206     	  ris *= 128;

207
208 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209 return ASN1_DER_ERROR;

Here are the related macros:

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

Quoted 6 lines
Quoted 3 lines

The statement in question is the expanded 'EXPR_SIGNED(_GL_INT_CONVERT (ris, 128))'. Since 'ris' is 'unsigned int', it's expected that 'EXPR_SIGNED' always returns false.

At least INT_MULTIPLY_OVERFLOW definitions is missing.

I got to admit that lots of details were skipped since the expansion of INT_MULTIPLY_OVERFLOW is really lengthy.

I'll add the details back in my v18 patchset cover letter.

Gary Lin

Quoted 3 lines

Please double check other analysis too. If you provide correct analysis for all non-fixable CIDs I will mark them as ignored. It would be nice if updated analysis is a part of latest version of the patch set.

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

#31 lore raw
Quoted 85 lines · Gary Lin
Quoted 84 lines · Daniel Kiper
Quoted 82 lines · Gary Lin via Grub-devel
Quoted 2 lines · Daniel Kiper

Hey,

--8<--

Quoted 6 lines

And I have attached the Coverity report. All issues reported there have to be fixed. If you cannot fix an issue you have to explain why you cannot do that and what is potential impact on the code stability, security, etc.

I have went through all the coverity issues. There are 6 issues in the TPM2 stack and the utility:

CID 435775, CID 435771, CID 435770, CID 435769, CID 435767, CID 435761

Those will be fixed in the next version.

The 9 issues are from libtasn1 and gnulib.

There are two memory corruption issue: CID 435762 and CID 435766, and I've filed the issues in libtasn1 upstream.

- CID 435762
https://gitlab.com/gnutls/libtasn1/-/issues/49
This is a valid case. However, the only exploitable function, _asn1_insert_tag_der(), is disabled in grub2 patch, so the severity is low. I have a quick fix but upstream would like to fix it in another way.
- CID 435766
https://gitlab.com/gnutls/libtasn1/-/issues/50
IMO, this is false positive, but I need libtasn1 upstream to confirm that.

Then, the remaining 7 Integer handling issues are involved with the macros from gnulib, and I believe those are false positive.

The following are my analyses:

________________________________________________________________________________________________________
*** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()
475 */
476 if (leading != 0 && der[len_len + k] == 0x80)
477 return ASN1_DER_ERROR;
478 leading = 0;
479
480 /* check for wrap around */

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT)
"val < ((((1 ? 0 : val) - 1 < 0) ? ~((((1 ? 0 : val) + 1 << 62UL /* sizeof (+val) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : val) + 0)) >> 7)" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

481 if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482 return ASN1_DER_ERROR;
483
484 val = val << 7;
485 val |= der[len_len + k] & 0x7F;
486

Here are the related macros:

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define INT_LEFT_SHIFT_OVERFLOW(a, b) \

  INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
                                 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
  ((a) < 0                                              \
   ? (a) < (min) >> (b)                                 \

: (max) >> (b) < (a))

The statement in question is expanded "(a) < (min) >> (b)" of INT_LEFT_SHIFT_RANGE_OVERFLOW. Since 'val' is 'uint64_t', '(a) < 0' is always false, so the result of that statement doen't matter.

Quoted 7 lines
Quoted 4 lines

Something is missing and/or requires clarification/expansion here. AFAICT at least your description completely ignores "(max) >> (b) < (a)" expression result.

Because Coverity only complained "(a) < (min) >> (b)". "(max) >> (b) < (a)" does the right thing to check whether 'a' is overflowed after the left shift.

OK, then something like above should be added to the CID description then.

Quoted 55 lines
Quoted 55 lines
Quoted 54 lines

________________________________________________________________________________________________________
*** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()
433 return ASN1_DER_ERROR;
434
435 val0 = 0;
436
437 for (k = 0; k < len; k++)
438 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435773: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0UL : val0) - 1UL < 0UL".

439 if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440 return ASN1_DER_ERROR;
441

442           val0 <<= 7;
443           val0 |= der[len_len + k] & 0x7F;
444           if (!(der[len_len + k] & 0x80))

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

The statement in question is the expanded 'EXPR_SIGNED'. Since that macro is to test whether the variable is signed or not. For 'uint64_t val0', it's expected to be always false.

________________________________________________________________________________________________________
*** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435772: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0U : ((1 ? 0U : ris) + 128U)) - 1U < 0U".

204     	  if (INT_MULTIPLY_OVERFLOW (ris, 128))
205     	    return ASN1_DER_ERROR;
206     	  ris *= 128;

207
208 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209 return ASN1_DER_ERROR;

Here are the related macros:

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v)) #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v)) #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

The statement in question is the expanded 'EXPR_SIGNED(_GL_INT_CONVERT (ris, 128))'. Since 'ris' is 'unsigned int', it's expected that 'EXPR_SIGNED' always returns false.

Quoted 6 lines
Quoted 2 lines

At least INT_MULTIPLY_OVERFLOW definitions is missing.

I got to admit that lots of details were skipped since the expansion of INT_MULTIPLY_OVERFLOW is really lengthy.

I'll add the details back in my v18 patchset cover letter.

Cool! Thanks!

Daniel

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