Automatic Disk Unlock with TPM2
v20 cover Automatic Disk Unlock with TPM2
GIT repo for v20: https://github.com/lcp/grub2/tree/tpm2-unlock-v20
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 GRUB, 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~7,9-17 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into GRUB. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.
Patch 8 fixes a potential buffer overrun in libtasn1.
(https://gitlab.com/gnutls/libtasn1/-/issues/49)
Patch 17 adds the document for libtasn1 and the steps to upgrade the library.
Patch 19~25 are based on 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
- 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
- Extending the TPM2 TSS stack to support authorized policy
Patch 26 implements the authorized policy support.
Patch 27 implements the missing NV index mode. (Thanks to Patrick Colp)
Patch 28 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 29 and 30 fix the potential security issues spotted by Fabian Vogt.
Patch 31 and 32 implement the TPM2 key unsealing testcases.
Patch 33 documents TPM2 key protector including the new GRUB commands and the user-space utility.
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/efi/grub/sealed.tpm3. Unseal the key with the proper commands in grub.cfg:
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub/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
v20:
- Rearranging the order of the libtasn1 patches
- libtasn1 patches are created by the following repo against the libtasn1-4.19.0-base-v4 tag:
https://github.com/lcp/grub2/tree/import-libtasn1-4.19.0-v4
- Excluding a libtasn1 testcase to avoid a potential gcc warning when 'long' is the same as 'int'
- Replacing grub_swap_bytes32() with grub_cpu_to_be32() to make the TPM command size always big endian
- Correcting the GRUB path in the commit messages and the document
- "/efi/grub2" => "/efi/grub"
- "/boot/efi/boot/grub2" => "/boot/efi/efi/grub/"
- "(hd0,gpt1)/boot/grub2/" => "(hd0,gpt1)/efi/grub"
- Removing the unnecessary "grub_errno = GRUB_ERR_NONE;"
- Replacing 'lu' with 'PRIuGRUB_UINT64_T' when setting the error message for 'uint64_t' to avoid the portability issue
- Removing the 'grub_' and 'GRUB_' prefixes from the local types, functions, and variables
- Documenting the option '-P' for 'cryptomount'
- Fixing coding style issues
- Removing the unused headers from grub-core/lib/tss2/tcg2_emu.c
- Updating the TPM2 key protector in user manual
v19:
- https://lists.gnu.org/archive/html/grub-devel/2024-09/msg00055.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v19
- Updating the coverity report for libtasn1
- Splitting the strcat() patch for libtasn1
- Addressing why the header paths are adjusted in libtasn1.h
- Fixing the order of the copyright headers
- Breaking down the patch for asn1 tests into the smaller patches
- libtasn1 patches are created by the following repo against the libtasn1-4.19.0-base-v3 tag:
https://github.com/lcp/grub2/tree/import-libtasn1-4.19.0-v3
- Marking the 'extern' functions
- Amending the string length checks
- Unmarking the technical strings for translation
- Fixing more line foldings and coding style issues
- Renaming the TPM2 types to add the '_t' postfix for typedefs
- Adding the 'grub_' prefix to the global functions from tss2 and removing the 'grub_' prefix from the location functions
- Adding more comments about the passphrase fallback
- Simplying the diskfilter check
- Documenting TPM2 key protector in the user manual
v18:
- https://lists.gnu.org/archive/html/grub-devel/2024-06/msg00228.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v18
- Updating the steps to import libtasn1
- Importing test cases from libtasn1 and then applying the follow-up patch by autogen.sh
- The patch files in patch 3~8 are created with the following repo
against the libtasn1-4.19.0-base-v2 tag:
https://github.com/lcp/grub2/tree/import-libtasn1-4.19.0-v2- Splitting the grub compatibility patch for libtasn1
- Moving the TSS2 code to grub-core/lib/tss2 and renaming grub_tpm2_mu_*() functions to grub_Tss2_MU_*()
- Splitting the TSS2 patch into 3 patches
- Fixing the types of TPMA_SESSION, TPMA_OBJECT, and TPMA_LOCALITY
- Renaming the tpm2 module to tpm2_key_protector
- Adding more comments to asn1_read_uint32() and defining the lower and upper bounds for tpm2key elements
- Also fixing a bug that allows 100 elements in the key file
- Fixing the copyright headers
- Fixing the coding style
v17:
- https://lists.gnu.org/archive/html/grub-devel/2024-06/msg00108.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v17
- Fixing the missing space in strncat()
- Updating the steps to import libtasn1
- Moving libtasn1.h to grub-core/lib/libtasn1/ and fixing the affected patches
- libtasn1.h is included in tpm2key.h, so there is no need to include the header again in module.c and tpm2key.c.
- Applying the libtasn1 patches in a different way
- Instead of applying the patches directly in the grub2 source code, libtasn1 is copied to grub-core/lib/libtasn1-grub by autogen.sh and then the script applies the libtasn1 patches to libtasn1-grub.
- The patch files in patch 3, 4, and 5 are created by the following repo against the libtasn1-4.19.0-base tag.
https://github.com/lcp/grub2/tree/import-libtasn1-4.19.0
- Correcting the description of the tpm2_test commit to replace swtpm_cuse with "swtpm chardev"
v16:
- https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00093.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v16
- Rebasing patch 6, 12, and 16 to fix the conflicts with the latest master branch
- Changes in cryptodisk:
- Disallowing that both OPTION_KEYFILE and OPTION_PROTECTOR are set since the key data for "--key-file" would be overwritten by the key protectors
- Resetting the cargs key data when the key from a key protector doesn't work for the disk to ensure the passphrase prompt will be triggered later
- Adding the comment to address why grub_errno is only cleared for
cargs->key_len == 0
v15:
- https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00059.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v15
- Changes in tpm2_test
- Quoting the variables which contain file paths
- Correcting the exit code for several commands
- Writing the verification text directly into the LUKS device
- Amending the waiting loop for swtpm
- Replacing exit with return in tpm2_seal_unseal() and
tpm2_seal_unseal_nv()
- Collecting the parameters for the SRK mode testcases in an array and invoking tpm2_seal_unseal() with a for loop
- Moving the tpm2-tools commands for the NV index mode to a separate function
- Using tpm2_evictcontrol to remove the object from the NV index to match the key sealing commands
- Printing the test results
- Printing error messages to stderr
v14:
- https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00011.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v14
- Addressing the libtasn1 patches more in the document
- Various improvements in tpm2_test
- Verifying the test inside the LUKS device
- Improving the return status checks and the waiting loop for swtpm
- Fixing the portability issues
- Making all variables braced
- Renaming grub-emu-opts to --emu-opts (grub-shell)
v13:
- https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00155.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v13
- Fixing typos and a few multi-line comments
- Improving the conditional checks for the arguments of tpm2_key_protector_init
- Updating to the latest TPM 2.0 Key File format
- Adding the new optional fields: description and rsaParent
- Handling "rsaParent == TRUE" when unsealing the tpm2key
- Setting "rsaParent" to "TRUE" when sealing the key with RSA SRK
- Removing non-standard SRKs: RSA3072, RSA4096, ECC_NIST_P384, ECC_NIST_P521, and ECC_SM2_P256
- Adding more error messages to grub-protect
- Improving the error checking for the swtpm chardev instance
- Exiting the tpm2_test script if grub-protect failed to seal the key
v12:
- https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00108.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v12
- Fixing typos and indentation
- Removing the unused TPM commands: TPM2_HashSequenceStart, TPM2_SequenceUpdate, and TPM2_SequenceComplete,
- Following the TCG EK templates to set the parameters of SRK
- Removing ECC_BN_P256 and ECC_BN_P638 from the SRK algorithm list since those two algorithms are not mentioned in the TCG EK templates
- Updating the help messages of the tpm2 module and grub-protect
- Removing the unnecessary NULL checks
- Adding the manpage for grub-protect
- Replacing grub_crypto_hash() with TPM2_Hash() in grub-protect to support SHA384 PCR banks
- Using 'swtpm chardev' to start swtpm instead of 'swtpm_cuse' since some distros may not build swtpm with cuse
- Adding a new testcase without specifying the SRK type to test the default SRK settings
- Amending tpm2_test to remove the duplicate error checking code
- Silencing the tpm2-tools commands in tpm2_test
- Fixing the exit trap of tpm2_test to removing the working directory on success
v11:
- https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00052.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v11
- Adding the missing default: handlers in grub-core/tpm2/mu.c
- Updating the help messages and commit messages to reflect the change of the default SRK algorithm (RSA2048 -> ECC_NIST_P256)
- Adding the testcase for the NV index mode
v10:
- https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00019.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v10
- Fixing the coverity issues: CID 435775, CID 435771, CID 435770, CID 435769, CID 435767, CID 435761
https://lists.gnu.org/archive/html/grub-devel/2024-02/txtKIuUb5lf3O.txt
- Fixing the potential memory leak (CID 435775)
- Removing the unnecessary grub_protect_get_grub_drive_for_file() from
util/grub-protect.c (CID 435771)
- Using the grub_tpm2_mu_TPM2B_*_Unmarshal functions to unmarshal the
TPM2B structs instead of a generic grub_tpm2_mu_TPM2B_Unmarshal
(CID 435770)
- Fixing Null pointer dereference (CID 435769)
- Adding bound checks to grub_tpm2_mu_TPML_DIGEST_Unmarshal()
(CID 435767) - Improving the check for the return value of ftell() (CID 435761)
- Adding a quick fix for CID 435762
- Removing the empty ending line in tests/asn1_test.in
- Fixing docs/grub-dev.texi and updating the libtasn1 patches in grub-core/lib/libtasn1-patches/
- Merging all the TPM2 TSS stack patches into one to reduce the total patch number
- Switching the default asymmetric algorithm from RSA2048 to TPM_ECC_NIST_P256 for the faster key generation
- Adding the fallback SRK templates to try a few more SRK types in case grub2 failed to associate the sealed key with the SRK in the persistent handle or the default SRK
- Improving the test script to add tests for the persistent handle and the fallback SRKs
v9:
- https://lists.gnu.org/archive/html/grub-devel/2024-02/msg00007.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-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
*********************************************
* Anaylses for Coverity issuses on libtasn1 *
*********************************************
2 Memory corruptions: CID 435762, CID 435766
________________________________________________________________________________________________________
*** 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:
Reported to upstream: https://gitlab.com/gnutls/libtasn1/-/issues/49
________________________________________________________________________________________________________
*** 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:
False positive?
https://gitlab.com/gnutls/libtasn1/-/issues/50
==
7 Integer handling issues:
CID 435774, CID 435773, CID 435772, CID 435768, CID 435765, CID 435764, CID 435763
________________________________________________________________________________________________________
*** 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
/grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()
Here are the related macros from gnulib:
#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_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \
: _GL_INT_NEGATE_CONVERT (e, 1))
#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ ((a) < 0 \ ? (a) < (min) >> (b) \
: (max) >> (b) < (a))
#define INT_LEFT_SHIFT_OVERFLOW(a, b) \
INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
_GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))The statement in question is expanded "(a) < (min) >> (b)" from 'INT_LEFT_SHIFT_RANGE_OVERFLOW'.
'(a) < (min) >> (b)'
=> '(val) < _GL_INT_MINIMUM (val) >> (7)'
=> '(val) < \
(EXPR_SIGNED (val) \
? ~ _GL_SIGNED_INT_MAXIMUM (val) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((_GL_INT_NEGATE_CONVERT (val, 1) < 0) \
? ~ _GL_SIGNED_INT_MAXIMUM (val) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ _GL_SIGNED_INT_MAXIMUM (val) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ (((_GL_INT_CONVERT (val, 1) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ (((((1 ? 0 : (val)) + (1)) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: ((1 ? 0 : (val)) + (0))) \
>> (7)''_GL_INT_MINIMUM' returns the minimum value of the given type. Since 'val' is 'uint64_t', '_GL_INT_MINIMUM (val)' is 0
'(val) < _GL_INT_MINIMUM (val) >> (7)' => '(val) < 0 >> (7)' => '(val) < 0'
For 'uint64_t val', the result is always false.
However, in 'INT_LEFT_SHIFT_RANGE_OVERFLOW':
((a) < 0 \ ? (a) < (min) >> (b) \ : (max) >> (b) < (a))
'(a) < 0' is false for 'uint64_t val', so the second operand, '(a) < (min) >> (b)', is always skipped. Thus, the result of the second operand doesn'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 from gnulib:
#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_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \
: _GL_INT_NEGATE_CONVERT (e, 1))
#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ ((a) < 0 \ ? (a) < (min) >> (b) \
: (max) >> (b) < (a))
#define INT_LEFT_SHIFT_OVERFLOW(a, b) \
INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
_GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))The statement in question is the expanded 'EXPR_SIGNED (val0)' from either '_GL_INT_MAXIMUM' or '_GL_INT_MINIMUM'
'EXPR_SIGNED (val0)'
=> '(_GL_INT_NEGATE_CONVERT (val0, 1) < 0)'
=> '(((1 ? 0 : (val0)) - (1)) < 0)'
'EXPR_SIGNED' is designed to test if the given expression is signed, and 'EXPR_SIGNED (val0)' is expected to be false for 'uint64_t val0'. The macro dutifully reflects the fact.
________________________________________________________________________________________________________
*** 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 gnulib:
#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \
: _GL_INT_NEGATE_CONVERT (e, 1))
#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 the expanded 'EXPR_SIGNED (_GL_INT_CONVERT (ris, 128))' from either '_GL_INT_MINIMUM' or '_GL_INT_MAXIMUM'.
'EXPR_SIGNED (_GL_INT_CONVERT (ris, 128))'
=> 'EXPR_SIGNED ((1 ? 0 : (ris)) + (128))'
=> '(_GL_INT_NEGATE_CONVERT (((1 ? 0 : (ris)) + (128), 1) < 0))' => '((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0)'
'_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ris' and '128' is 'unsigned int', '_GL_INT_CONVERT (ris, 128)' is '128U'.
'EXPR_SIGNED' is designed to check if the given expression is signed. Thus, 'EXPR_SIGNED (128U)' is expected to be false.
The combination of 'EXPR_SIGNED(e)' and '_GL_INT_CONVERT(e, v)' is used to test if the common type of the given two variables is signed, and those macros dutifully reflect the fact: the common type of 'ris' and '128' is unsigned.
________________________________________________________________________________________________________
*** 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 gnulib:
#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \
: _GL_INT_NEGATE_CONVERT (e, 1))
#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 the expanded '(a) < (min) / (b)' from '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)'
=> '(ris) < \
(EXPR_SIGNED (_GL_INT_CONVERT (ris, 128)) \
? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((_GL_INT_NEGATE_CONVERT (_GL_INT_CONVERT (ris, 128), 1) < 0) \
? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : (_GL_INT_CONVERT (ris, 128))) - (1)) < 0) \
? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \
? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \
? ~ (((_GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 1) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ris, 128))) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \
? ~ (((((1 ? 0 : (_GL_INT_CONVERT (ris, 128))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ris, 128))) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \
? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ris, 128))) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \
? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ (_GL_INT_CONVERT (ris, 128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \
? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \
? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \
? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: ((1 ? 0 : (_GL_INT_CONVERT (ris, 128)) + (0)) \
/ (128)'
=> '(ris) < \
((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \
? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: ((1 ? 0 : ((1 ? 0 : (ris)) + (128)) + (0)) \
/ (128)''_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ris' and '128' is 'unsigned int', '_GL_INT_CONVERT (ris, 128)' is '128U'.
'_GL_INT_MINIMUM (128U)' returns the minimum value of 'unsigned int', i.e. 0.
'(ris) < (_GL_INT_MINIMUM (_GL_INT_CONVERT (ris, 128))) / (128)' => '(ris) < 0 / (128)' => '(ris) < 0'
For 'unsigned int ris', the result is always false.
However, in 'INT_MULTIPLY_RANGE_OVERFLOW', 'a' is 'unsigned int ris' and 'b' is '128'. We can skip the statements for 'b < 0' and 'b == 0' and reduce the macro to
(a) < 0 \ ? (a) < (min) / (b) \ : (max) / (b) < (a))) \
Since '(a) < 0' is false for 'unsigned int ris', the statement in question, '(a) < (min) / (b)', is always skipped. Thus, the result of the statement 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 from gnulib:
#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_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \
: _GL_INT_NEGATE_CONVERT (e, 1))
#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ ((a) < 0 \ ? (a) < (min) >> (b) \
: (max) >> (b) < (a))
#define INT_LEFT_SHIFT_OVERFLOW(a, b) \
INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
_GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))The statement in question is expanded '(a) < (min) >> (b)' from INT_LEFT_SHIFT_RANGE_OVERFLOW.
'(a) < (min) >> (b)'
=> '(val) < (_GL_INT_MINIMUM (val)) >> (7)'
=> '(val) < \
(EXPR_SIGNED (val) \
? ~ _GL_SIGNED_INT_MAXIMUM (val) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((_GL_INT_NEGATE_CONVERT (val, 1) < 0) \
? ~ _GL_SIGNED_INT_MAXIMUM (val) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ _GL_SIGNED_INT_MAXIMUM (val) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ (((_GL_INT_CONVERT (val, 1) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ (((((1 ? 0 : (val)) + (1)) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (+ (val)) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (val, 0)) \
>> (7)'
=> '(val) < \
((((1 ? 0 : (val)) - (1)) < 0) \
? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (+ (val)) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: ((1 ? 0 : (val)) + (0))) \
>> (7)''_GL_INT_MINIMUM' returns the minimum value of the given type. For 'uint64_t val', '_GL_INT_MINIMUM (val)' is 0.
'(val) < (_GL_INT_MINIMUM (val)) >> (7)' => '(val) < 0 >> (7)' => '(val) < 0'
For 'uint64_t val', the result is always false.
However, in 'INT_LEFT_SHIFT_RANGE_OVERFLOW':
((a) < 0 \ ? (a) < (min) >> (b) \ : (max) >> (b) < (a))
'(a) < 0' is false for 'uint64_t val', so the statement in question, '(a) < (min) >> (b)', is always skipped. Thus, the result of the 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 from gnulib:
#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \
: _GL_INT_NEGATE_CONVERT (e, 1))
#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 'EXPR_SIGNED (_GL_INT_CONVERT (ans, 256))' from either '_GL_INT_MINIMUM' or '_GL_INT_MAXIMUM'.
'EXPR_SIGNED (_GL_INT_CONVERT (ans, 256))'
=> 'EXPR_SIGNED ((1 ? 0 : (ans)) + (256))'
=> '(_GL_INT_NEGATE_CONVERT (((1 ? 0 : (ans)) + (256)), 1) < 0)' => '((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1))'
'_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ans' and '256' is 'unsigned int', '_GL_INT_CONVERT (ans, 256)' is '256U'.
'EXPR_SIGNED' is designed to check if the given expression is signed. Thus, 'EXPR_SIGNED (256U)' is expected to be false.
The combination of 'EXPR_SIGNED(e)' and '_GL_INT_CONVERT(e, v)' is used to test if the common type of the given two variables is signed, and those macros dutifully reflect the fact: the common type of 'ans' and '256' is unsigned.
________________________________________________________________________________________________________
*** 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 from gnulib:
#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \
: _GL_INT_NEGATE_CONVERT (e, 1))
#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 the expanded '(a) < (min) / (b)' from 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)'
=> '(ans) < \
(EXPR_SIGNED (_GL_INT_CONVERT (ans, 256)) \
? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ans, 256)) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \
/ (256)'
=> '(ans) < \
(_GL_INT_NEGATE_CONVERT (_GL_INT_CONVERT (ans, 256), 1) < 0) \
? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ans, 256)) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \
/ (256)'
=> '(ans) < \
(((1 ? 0 : (_GL_INT_CONVERT (ans, 256))) - (1)) < 0) \
? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ans, 256)) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \
/ (256)'
=> '(ans) < \
(((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \
? ~ (((_GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 1) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ans, 256))) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \
/ (256)'
=> '(ans) < \
(((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \
? ~ ((((1 ? 0 : (_GL_INT_CONVERT (ans, 256))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ans, 256))) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \
/ (256)'
=> '(ans) < \
(((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \
? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ans, 256))) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \
/ (256)'
=> '(ans) < \
(((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \
? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ (_GL_INT_CONVERT (ans, 256))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \
/ (256)'
=> '(ans) < \
(((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \
? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ ((1 ? 0 : (ans)) + (1))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \
/ (256)'
=> '(ans) < \
(((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \
? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ ((1 ? 0 : (ans)) + (1))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: ((1 ? 0 : (_GL_INT_CONVERT (ans, 256))) + (0)) ) \
/ (256)'
=> '(ans) < \
(((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \
? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ ((1 ? 0 : (ans)) + (1))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
: ((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (0)) ) \
/ (256)''_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ans' and '256' is 'unsigned int', '_GL_INT_CONVERT (ans, 256)' is '256U'.
'_GL_INT_MINIMUM (256U)' returns the minimum value of 'unsigned int', i.e. 0.
'(ans) < (_GL_INT_MINIMUM (_GL_INT_CONVERT (ans, 256))) / (256)' => '(ans) < 0 / (256)' => '(ans) < 0'
For 'unsigned int ans', the result is always false.
However, in 'INT_MULTIPLY_RANGE_OVERFLOW', 'a' is 'unsigned int ans' and 'b' is '256'. We can skip the statements for 'b < 0' and 'b == 0' and reduce the macro to
(a) < 0 \ ? (a) < (min) / (b) \ : (max) / (b) < (a))) \
Since '(ans) < 0' is false for 'unsigned int ans', the statement in question, '(a) < (min) / (b)', is always skipped. Thus, the result of the statement doesn't matter.
Daniel Axtens (4):
posix_wrap: tweaks in preparation for libtasn1
libtasn1: import libtasn1-4.19.0
libtasn1: compile into asn1 module
asn1_test: test module for libtasn1
Gary Lin (23):
libtasn1: disable code not needed in grub
libtasn1: replace strcat() with strcpy() in _asn1_str_cat()
libtasn1: replace strcat() with _asn1_str_cat()
libtasn1: adjust the header paths in libtasn1.h
libtasn1: Use grub_divmod64() for division
libtasn1: fix the potential buffer overrun
asn1_test: include asn1_test.h only
asn1_test: rename the main functions to the test names
asn1_test: return either 0 or 1 to reflect the results
asn1_test: remove 'verbose' and the unnecessary printf()
asn1_test: print the error messages with grub_printf()
asn1_test: use the grub-specific functions and types
asn1_test: exclude a testcase when long and int are the same
libtasn1: Add the documentation
tss2: Add TPM2 buffer handling functions
tss2: Add TPM2 types and Marshal/Unmarshal functions
tss2: Add TPM2 Software Stack (TSS2) support
tpm2_key_protector: Support authorized policy
cryptodisk: wipe out the cached keys from protectors
diskfilter: look up cryptodisk devices first
tpm2_key_protector: Add grub-emu support
tests: Add tpm2_key_protector_test
docs: Document TPM2 key protector
Hernan Gatta (4):
key_protector: Add key protectors framework
key_protector: Add TPM2 Key Protector
cryptodisk: Support key protectors
util/grub-protect: Add new tool
Patrick Colp (2):
tpm2_key_protector: Implement NV index
cryptodisk: Fallback to passphrase
.gitignore | 2 + Makefile.util.def | 39 + autogen.sh | 33 + configure.ac | 30 + docs/grub-dev.texi | 35 + docs/grub.texi | 526 +++- docs/man/grub-protect.h2m | 4 + grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 60 + grub-core/commands/tpm2_key_protector/args.c | 127 + .../commands/tpm2_key_protector/module.c | 1238 ++++++++ grub-core/commands/tpm2_key_protector/tpm2.h | 36 + .../commands/tpm2_key_protector/tpm2_args.h | 49 + .../commands/tpm2_key_protector/tpm2key.asn | 49 + .../commands/tpm2_key_protector/tpm2key.c | 499 ++++ .../commands/tpm2_key_protector/tpm2key.h | 87 + .../tpm2_key_protector/tpm2key_asn1_tab.c | 63 + grub-core/disk/cryptodisk.c | 268 +- grub-core/disk/diskfilter.c | 31 +- grub-core/disk/key_protector.c | 73 + grub-core/kern/emu/main.c | 11 +- grub-core/kern/emu/misc.c | 51 + grub-core/lib/efi/tcg2.c | 143 + ...asn1-disable-code-not-needed-in-grub.patch | 320 +++ ...-strcat-with-strcpy-in-_asn1_str_cat.patch | 33 + ...n1-replace-strcat-with-_asn1_str_cat.patch | 71 + ...djust-the-header-paths-in-libtasn1.h.patch | 39 + ...tasn1-Use-grub_divmod64-for-division.patch | 31 + ...sn1-fix-the-potential-buffer-overrun.patch | 36 + ...7-asn1_test-include-asn1_test.h-only.patch | 164 ++ ...-the-main-functions-to-the-test-name.patch | 129 + ...-either-0-or-1-to-reflect-the-result.patch | 78 + ...e-verbose-and-the-unnecessary-printf.patch | 173 ++ ...-the-error-messages-with-grub_printf.patch | 485 ++++ ...he-grub-specific-functions-and-types.patch | 263 ++ ...e-a-testcase-when-long-and-int-are-t.patch | 47 + 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 + grub-core/lib/libtasn1/libtasn1.h | 643 +++++ .../tests/CVE-2018-1000654-1_asn1_tab.h | 32 + .../tests/CVE-2018-1000654-2_asn1_tab.h | 36 + .../lib/libtasn1/tests/CVE-2018-1000654.c | 72 + grub-core/lib/libtasn1/tests/Test_overflow.c | 168 ++ grub-core/lib/libtasn1/tests/Test_simple.c | 226 ++ grub-core/lib/libtasn1/tests/Test_strings.c | 156 + .../lib/libtasn1/tests/object-id-decoding.c | 121 + .../lib/libtasn1/tests/object-id-encoding.c | 133 + grub-core/lib/libtasn1/tests/octet-string.c | 230 ++ grub-core/lib/libtasn1/tests/reproducers.c | 90 + grub-core/lib/libtasn1_wrap/wrap.c | 27 + 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 + grub-core/lib/tss2/buffer.c | 147 + grub-core/lib/tss2/tcg2.h | 35 + grub-core/lib/tss2/tcg2_emu.c | 51 + grub-core/lib/tss2/tpm2_cmd.c | 1043 +++++++ grub-core/lib/tss2/tpm2_cmd.h | 157 ++ grub-core/lib/tss2/tss2.c | 21 + grub-core/lib/tss2/tss2_buffer.h | 64 + grub-core/lib/tss2/tss2_mu.c | 1174 ++++++++ grub-core/lib/tss2/tss2_mu.h | 397 +++ grub-core/lib/tss2/tss2_structs.h | 773 +++++ grub-core/lib/tss2/tss2_types.h | 404 +++ grub-core/tests/asn1/asn1_test.c | 50 + grub-core/tests/asn1/asn1_test.h | 45 + grub-core/tests/lib/functional_test.c | 1 + include/grub/cryptodisk.h | 16 + include/grub/emu/misc.h | 5 + include/grub/key_protector.h | 47 + tests/asn1_test.in | 11 + tests/tpm2_key_protector_test.in | 389 +++ tests/util/grub-shell.in | 6 +- util/grub-protect.c | 1394 +++++++++ 88 files changed, 21810 insertions(+), 80 deletions(-) create mode 100644 docs/man/grub-protect.h2m create mode 100644 grub-core/commands/tpm2_key_protector/args.c create mode 100644 grub-core/commands/tpm2_key_protector/module.c create mode 100644 grub-core/commands/tpm2_key_protector/tpm2.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2_args.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.asn create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.c create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key_asn1_tab.c create mode 100644 grub-core/disk/key_protector.c create mode 100644 grub-core/lib/efi/tcg2.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-replace-strcat-with-strcpy-in-_asn1_str_cat.patch create mode 100644 grub-core/lib/libtasn1-patches/0003-libtasn1-replace-strcat-with-_asn1_str_cat.patch create mode 100644 grub-core/lib/libtasn1-patches/0004-libtasn1-adjust-the-header-paths-in-libtasn1.h.patch create mode 100644 grub-core/lib/libtasn1-patches/0005-libtasn1-Use-grub_divmod64-for-division.patch create mode 100644 grub-core/lib/libtasn1-patches/0006-libtasn1-fix-the-potential-buffer-overrun.patch create mode 100644 grub-core/lib/libtasn1-patches/0007-asn1_test-include-asn1_test.h-only.patch create mode 100644 grub-core/lib/libtasn1-patches/0008-asn1_test-rename-the-main-functions-to-the-test-name.patch create mode 100644 grub-core/lib/libtasn1-patches/0009-asn1_test-return-either-0-or-1-to-reflect-the-result.patch create mode 100644 grub-core/lib/libtasn1-patches/0010-asn1_test-remove-verbose-and-the-unnecessary-printf.patch create mode 100644 grub-core/lib/libtasn1-patches/0011-asn1_test-print-the-error-messages-with-grub_printf.patch create mode 100644 grub-core/lib/libtasn1-patches/0012-asn1_test-use-the-grub-specific-functions-and-types.patch create mode 100644 grub-core/lib/libtasn1-patches/0013-asn1_test-exclude-a-testcase-when-long-and-int-are-t.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/libtasn1.h create mode 100644 grub-core/lib/libtasn1/tests/CVE-2018-1000654-1_asn1_tab.h create mode 100644 grub-core/lib/libtasn1/tests/CVE-2018-1000654-2_asn1_tab.h create mode 100644 grub-core/lib/libtasn1/tests/CVE-2018-1000654.c create mode 100644 grub-core/lib/libtasn1/tests/Test_overflow.c create mode 100644 grub-core/lib/libtasn1/tests/Test_simple.c create mode 100644 grub-core/lib/libtasn1/tests/Test_strings.c create mode 100644 grub-core/lib/libtasn1/tests/object-id-decoding.c create mode 100644 grub-core/lib/libtasn1/tests/object-id-encoding.c create mode 100644 grub-core/lib/libtasn1/tests/octet-string.c create mode 100644 grub-core/lib/libtasn1/tests/reproducers.c 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/lib/tss2/buffer.c create mode 100644 grub-core/lib/tss2/tcg2.h create mode 100644 grub-core/lib/tss2/tcg2_emu.c create mode 100644 grub-core/lib/tss2/tpm2_cmd.c create mode 100644 grub-core/lib/tss2/tpm2_cmd.h create mode 100644 grub-core/lib/tss2/tss2.c create mode 100644 grub-core/lib/tss2/tss2_buffer.h create mode 100644 grub-core/lib/tss2/tss2_mu.c create mode 100644 grub-core/lib/tss2/tss2_mu.h create mode 100644 grub-core/lib/tss2/tss2_structs.h create mode 100644 grub-core/lib/tss2/tss2_types.h create mode 100644 grub-core/tests/asn1/asn1_test.c create mode 100644 grub-core/tests/asn1/asn1_test.h create mode 100644 include/grub/key_protector.h create mode 100644 tests/asn1_test.in create mode 100644 tests/tpm2_key_protector_test.in create mode 100644 util/grub-protect.c
Range-diff against v19:
1: 7ddef18d2 = 1: 2cdb45a08 posix_wrap: tweaks in preparation for libtasn1
2: f27ad35a7 = 2: f069b4e35 libtasn1: import libtasn1-4.19.0
3: 86f8e472e ! 3: 56bdb8c9c libtasn1: disable code not needed in grub
@@ Commit message
## grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch (new) ##
@@
-+From b927f1b24fe10c57edc9711ff4baa12b013ce351 Mon Sep 17 00:00:00 2001
++From f534b3c0902adf207b7e51b8364284fe6a06731b Mon Sep 17 00:00:00 2001
+From: Daniel Axtens <[email protected]>
+Date: Fri, 1 May 2020 17:12:23 +1000
-+Subject: [PATCH 01/12] libtasn1: disable code not needed in grub
++Subject: [PATCH 01/13] libtasn1: disable code not needed in grub
+
+We don't expect to be able to write ASN.1, only read it,
+so we can disable some code.
@@ -411,12 +422,16 @@ extern "C"
+ # define ASN1_MAX_TAG_SIZE 4
+ # define ASN1_MAX_LENGTH_SIZE 9
+--
-+2.35.3
++2.43.0
+ 4: 455cee6e0 ! 4: fd95d1a0c libtasn1: replace strcat() with strcpy() in _asn1_str_cat()
@@ Commit message
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/lib/libtasn1-patches/0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch (new) ##
@@
-+From 4e59e270b879693a0247011e265ccf397f19cacc Mon Sep 17 00:00:00 2001
++From 93453558fbe34634096770933b3dc40c9199dfb4 Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Tue, 20 Aug 2024 16:14:51 +0800
-+Subject: [PATCH 02/12] libtasn1: replace strcat() with strcpy() in
++Subject: [PATCH 02/13] libtasn1: replace strcat() with strcpy() in
+ _asn1_str_cat()
+
+strcat() is not available in GRUB. This commit replaces strcat() with
@@ grub-core/lib/libtasn1-patches/0002-libtasn1-replace-strcat-with-strcpy-in-_asn1
+
+Signed-off-by: Daniel Axtens <[email protected]>
+Signed-off-by: Gary Lin <[email protected]>
++Reviewed-by: Daniel Kiper <[email protected]>
+---
+ grub-core/lib/libtasn1-grub/lib/gstr.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
@@ -36,7 +36,7 @@ _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
+ else
+ {
+--
-+2.35.3
++2.43.0
+
5: 6c2df83bd ! 5: b8cf72c0b libtasn1: replace strcat() with _asn1_str_cat()
@@ Commit message
Signed-off-by: Daniel Axtens <[email protected]>
Signed-off-by: Gary Lin <[email protected]>
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/lib/libtasn1-patches/0003-libtasn1-replace-strcat-with-_asn1_str_cat.patch (new) ##
@@
-+From 8aa07d427966fbb560871a0a87e0af876920002c Mon Sep 17 00:00:00 2001
++From b5efa707619fde1921a3cbd83f4ee95a7d995fa0 Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Tue, 20 Aug 2024 16:26:45 +0800
-+Subject: [PATCH 03/12] libtasn1: replace strcat() with _asn1_str_cat()
++Subject: [PATCH 03/13] libtasn1: replace strcat() with _asn1_str_cat()
+
+strcat() is not available in GRUB. This commit replaces strcat() and
+_asn1_strcat() with the bounds-checking _asn1_str_cat().
+
+Signed-off-by: Daniel Axtens <[email protected]>
+Signed-off-by: Gary Lin <[email protected]>
++Reviewed-by: Daniel Kiper <[email protected]>
+---
+ grub-core/lib/libtasn1-grub/lib/decoding.c | 8 ++++----
+ grub-core/lib/libtasn1-grub/lib/element.c | 2 +-
@@ -115,7 +115,6 @@ extern const tag_and_class_st _asn1_tags[];
+ # if SIZEOF_UNSIGNED_LONG_INT == 8
+ # define _asn1_strtou64(n,e,b) strtoul((const char *) n, e, b)
+--
-+2.35.3
++2.43.0
+ 6: 626894626 ! 6: b274a1e0f libtasn1: adjust the header paths in libtasn1.h
@@ Commit message
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/lib/libtasn1-patches/0004-libtasn1-adjust-the-header-paths-in-libtasn1.h.patch (new) ##
@@
-+From 3ea2376db98e99a4461411fc476850de33822999 Mon Sep 17 00:00:00 2001
++From 24036474fab426917507fcadc85a83fa0b1cef3b Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Tue, 25 Jun 2024 16:30:40 +0800
-+Subject: [PATCH 04/12] libtasn1: adjust the header paths in libtasn1.h
++Subject: [PATCH 04/13] libtasn1: adjust the header paths in libtasn1.h
+
+Since libtasn1.h is the header to be included by users, including the
+standard POSIX headers in libtasn1.h would force the user to add the
@@ grub-core/lib/libtasn1-patches/0004-libtasn1-adjust-the-header-paths-in-libtasn1
+
+Signed-off-by: Daniel Axtens <[email protected]>
+Signed-off-by: Gary Lin <[email protected]>
++Reviewed-by: Daniel Kiper <[email protected]>
+---
+ grub-core/lib/libtasn1-grub/libtasn1.h | 5 ++---
+ 1 file changed, 2 insertions(+), 3 deletions(-)
@@ -54,9 +54,8 @@
+ # ifdef __cplusplus
+ extern "C"
+--
-+2.35.3
++2.43.0
+
7: 9805bc603 ! 7: e4581e36e libtasn1: Use grub_divmod64() for division
@@ Commit message
## grub-core/lib/libtasn1-patches/0005-libtasn1-Use-grub_divmod64-for-division.patch (new) ##
@@
-+From f4d086cc829544a33fd6fd705538cd8d820d6c40 Mon Sep 17 00:00:00 2001
++From 5ee39f0bb8b0b713f766c0fc83439d83ac2c0bf2 Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Tue, 25 Jun 2024 16:32:50 +0800
-+Subject: [PATCH 05/12] libtasn1: Use grub_divmod64() for division
++Subject: [PATCH 05/13] libtasn1: Use grub_divmod64() for division
+
+Replace a 64-bit division with a call to grub_divmod64(), preventing
+creation of __udivdi3() calls on 32-bit platforms.
@@ -632,7 +632,7 @@ _asn1_ltostr (int64_t v, char str[LTOSTR_MAX_SIZE])
+ temp[start + count] = '0' + (char) r;
+ count++;
+--
-+2.35.3
++2.43.0
+
8: 15b0caaaf ! 8: fdb2a66e7 libtasn1: fix the potential buffer overrun
@@ Commit message
## grub-core/lib/libtasn1-patches/0006-libtasn1-fix-the-potential-buffer-overrun.patch (new) ##
@@
-+From 66f5485a9b4ea02f7d2796c5f245fcbf7c88b390 Mon Sep 17 00:00:00 2001
++From f629f58b01b3e88052be5e50f51a667181203e05 Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Mon, 8 Apr 2024 14:57:21 +0800
-+Subject: [PATCH 06/12] libtasn1: fix the potential buffer overrun
++Subject: [PATCH 06/13] libtasn1: fix the potential buffer overrun
+
+In _asn1_tag_der(), the first while loop for the long form may end up
+with a 'k' value with 'ASN1_MAX_TAG_SIZE' and cause the buffer overrun
@@ -143,7 +143,7 @@ _asn1_tag_der (unsigned char class, unsigned int tag_value,
+ }
+ *ans_len = k + 1;
+--
-+2.35.3
++2.43.0
+
9: 96da6bf0d ! 9: 3739920d3 asn1_test: include asn1_test.h only
@@ Commit message
Signed-off-by: Daniel Axtens <[email protected]>
Signed-off-by: Gary Lin <[email protected]>
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/lib/libtasn1-patches/0007-asn1_test-include-asn1_test.h-only.patch (new) ##
@@
-+From f1fef1a65ff5ba39f78230f8b4f663bb8f5884b7 Mon Sep 17 00:00:00 2001
++From 9cc63a71d6772947127d4d77e135d5fda68ccb8d Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Fri, 16 Aug 2024 14:10:21 +0800
-+Subject: [PATCH 07/12] asn1_test: include asn1_test.h only
++Subject: [PATCH 07/13] asn1_test: include asn1_test.h only
+
+This commit removes all the headers and only uses asn1_test.h.
+To avoid including int.h from grub-core/lib/libtasn1-grub/lib/,
@@ grub-core/lib/libtasn1-patches/0007-asn1_test-include-asn1_test.h-only.patch (ne
+
+Signed-off-by: Daniel Axtens <[email protected]>
+Signed-off-by: Gary Lin <[email protected]>
++Reviewed-by: Daniel Kiper <[email protected]>
+---
+ grub-core/tests/asn1/tests/CVE-2018-1000654.c | 4 +---
+ grub-core/tests/asn1/tests/Test_overflow.c | 7 +------
@@ -22,13 +22,9 @@
+ /* produces endless loop (fixed by d4b624b2):
+ * The following translates into a single node with all pointers
+--
-+2.35.3
++2.43.0
+10: e00a23d87 ! 10: 1225831f7 asn1_test: rename the main functions to the test names
@@ Commit message
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/lib/libtasn1-patches/0008-asn1_test-rename-the-main-functions-to-the-test-name.patch (new) ##
@@
-+From 5d1f9b5fd4df3f62ad20cbbecd3865c033aad7a2 Mon Sep 17 00:00:00 2001
++From c0f39b9eae745a36c44fc0dfa106675405ea154f Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Fri, 16 Aug 2024 14:18:44 +0800
-+Subject: [PATCH 08/12] asn1_test: rename the main functions to the test names
++Subject: [PATCH 08/13] asn1_test: rename the main functions to the test names
+
+This commit changes the main functions in the testcases to the test
+names so that the real 'main' test function can invokes them.
+
+Signed-off-by: Daniel Axtens <[email protected]>
+Signed-off-by: Gary Lin <[email protected]>
++Reviewed-by: Daniel Kiper <[email protected]>
+---
+ grub-core/tests/asn1/tests/CVE-2018-1000654.c | 2 +-
+ grub-core/tests/asn1/tests/Test_overflow.c | 2 +-
@@ -49,7 +49,7 @@ const asn1_static_node tab[] = {
+ int result, verbose = 0;
+ asn1_node definitions = NULL;
+--
-+2.35.3
++2.43.0
+
-: --------- > 11: aa4f68b56 asn1_test: return either 0 or 1 to reflect the results
11: 6e91722f6 ! 12: 83342d997 asn1_test: remove 'verbose' and the unnecessary printf()
@@ Commit message
Signed-off-by: Daniel Axtens <[email protected]>
Signed-off-by: Gary Lin <[email protected]>
+ Reviewed-by: Daniel Kiper <[email protected]>
- ## grub-core/lib/libtasn1-patches/0009-asn1_test-remove-verbose-and-the-unnecessary-printf.patch (new) ##
+ ## grub-core/lib/libtasn1-patches/0010-asn1_test-remove-verbose-and-the-unnecessary-printf.patch (new) ##
@@
-+From 408ccf080a5d0993aac217639ac0b8cfe34e0cf6 Mon Sep 17 00:00:00 2001
++From 759bc707a924f89b0d32ba7be2f54e100f1d57b7 Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Fri, 16 Aug 2024 14:26:38 +0800
-+Subject: [PATCH 09/12] asn1_test: remove 'verbose' and the unnecessary
++Subject: [PATCH 10/13] asn1_test: remove 'verbose' and the unnecessary
+ printf()
+
+This commit removes the 'verbose' variables and the unnecessary printf()
@@ grub-core/lib/libtasn1-patches/0009-asn1_test-remove-verbose-and-the-unnecessary
+
+Signed-off-by: Daniel Axtens <[email protected]>
+Signed-off-by: Gary Lin <[email protected]>
++Reviewed-by: Daniel Kiper <[email protected]>
+---
+ grub-core/tests/asn1/tests/CVE-2018-1000654.c | 11 +----
+ grub-core/tests/asn1/tests/Test_overflow.c | 40 +++----------------
@@ grub-core/lib/libtasn1-patches/0009-asn1_test-remove-verbose-and-the-unnecessary
+ 3 files changed, 7 insertions(+), 52 deletions(-)
+
+diff --git a/grub-core/tests/asn1/tests/CVE-2018-1000654.c b/grub-core/tests/asn1/tests/CVE-2018-1000654.c
-+index a935ab541..5710b61a7 100644
++index 2085b3cd3..81448cde8 100644
+--- a/grub-core/tests/asn1/tests/CVE-2018-1000654.c
++++ b/grub-core/tests/asn1/tests/CVE-2018-1000654.c
+@@ -29,15 +29,10 @@
@@ -64,7 +57,5 @@ test_CVE_2018_1000654 (void)
+
+- if (verbose)
+- printf ("Success\n");
-+ exit (0);
++ return 0;
+ }
+diff --git a/grub-core/tests/asn1/tests/Test_overflow.c b/grub-core/tests/asn1/tests/Test_overflow.c
+index bc28d0826..9f9578a1f 100644
@@ -146,12 +121,7 @@ test_overflow (void)
+ printf ("ERROR: asn1_get_length_der overflow-large2 (l %ld len %d)\n",
+ l, len);
+diff --git a/grub-core/tests/asn1/tests/reproducers.c b/grub-core/tests/asn1/tests/reproducers.c
-+index e843b74b9..d68fc0fbd 100644
++index 54a243d51..fa3cea762 100644
+--- a/grub-core/tests/asn1/tests/reproducers.c
++++ b/grub-core/tests/asn1/tests/reproducers.c
+@@ -51,13 +51,10 @@ const asn1_static_node tab[] = {
@@ -79,8 +76,5 @@ test_reproducers (void)
+- if (verbose)
+- printf ("Success\n");
+-
-+ exit (EXIT_SUCCESS);
++ return 0;
+ }
+--
-+2.35.3
++2.43.0
+
12: c717d8585 ! 13: 319d34b9e asn1_test: print the error messages with grub_printf()
@@ Commit message
Signed-off-by: Daniel Axtens <[email protected]>
Signed-off-by: Gary Lin <[email protected]>
+ Reviewed-by: Daniel Kiper <[email protected]>
- ## grub-core/lib/libtasn1-patches/0010-asn1_test-print-the-error-messages-with-grub_printf.patch (new) ##
+ ## grub-core/lib/libtasn1-patches/0011-asn1_test-print-the-error-messages-with-grub_printf.patch (new) ##
@@
-+From 2bc1bdd99f43da96305add2ce591ca637dce5648 Mon Sep 17 00:00:00 2001
++From af5f0a651de09e44e78177b988857acd3208c4b6 Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Fri, 16 Aug 2024 15:00:42 +0800
-+Subject: [PATCH 10/12] asn1_test: print the error messages with grub_printf()
++Subject: [PATCH 11/13] asn1_test: print the error messages with grub_printf()
+
+This commit replaces printf() and fprintf() with grub_printf() to print
+the error messages for the testcases. Besides, asn1_strerror() is used
@@ grub-core/lib/libtasn1-patches/0010-asn1_test-print-the-error-messages-with-grub
+
+Signed-off-by: Daniel Axtens <[email protected]>
+Signed-off-by: Gary Lin <[email protected]>
++Reviewed-by: Daniel Kiper <[email protected]>
+---
+ grub-core/tests/asn1/tests/CVE-2018-1000654.c | 8 +--
+ grub-core/tests/asn1/tests/Test_overflow.c | 14 ++---
@@ grub-core/lib/libtasn1-patches/0010-asn1_test-print-the-error-messages-with-grub
+ 8 files changed, 67 insertions(+), 99 deletions(-)
+
+diff --git a/grub-core/tests/asn1/tests/CVE-2018-1000654.c b/grub-core/tests/asn1/tests/CVE-2018-1000654.c
-+index 5710b61a7..9eb4d7979 100644
++index 81448cde8..b78667900 100644
+--- a/grub-core/tests/asn1/tests/CVE-2018-1000654.c
++++ b/grub-core/tests/asn1/tests/CVE-2018-1000654.c
+@@ -38,8 +38,8 @@ test_CVE_2018_1000654 (void)
@@ -38,8 +38,8 @@ test_CVE_2018_1000654 (void)
+- printf ("ErrorDescription = %s\n\n", errorDescription);
++ grub_printf ("Error: %s\nErrorDescription = %s\n\n",
++ asn1_strerror (result), errorDescription);
-+ exit (1);
++ return 1;
+ }
+
+@@ -50,8 +50,8 @@ test_CVE_2018_1000654 (void)
@@ -50,8 +50,8 @@ test_CVE_2018_1000654 (void)
+- printf ("ErrorDescription = %s\n\n", errorDescription);
++ grub_printf ("Error: %s\nErrorDescription = %s\n\n",
++ asn1_strerror (result), errorDescription);
-+ exit (1);
++ return 1;
+ }
+
+diff --git a/grub-core/tests/asn1/tests/Test_overflow.c b/grub-core/tests/asn1/tests/Test_overflow.c
@@ -196,25 +191,22 @@ test_octet_string (void)
+ }
+ free (tmp);
+diff --git a/grub-core/tests/asn1/tests/reproducers.c b/grub-core/tests/asn1/tests/reproducers.c
-+index d68fc0fbd..278cfed6c 100644
++index fa3cea762..0e3c9fd65 100644
+--- a/grub-core/tests/asn1/tests/reproducers.c
++++ b/grub-core/tests/asn1/tests/reproducers.c
+@@ -58,8 +58,7 @@ test_reproducers (void)
@@ -58,8 +58,7 @@ test_reproducers (void)
+- asn1_perror (result);
+- printf ("ErrorDescription = %s\n\n", errorDescription);
++ grub_printf ("Error: %s\nErrorDescription = %s\n\n", asn1_strerror (result), errorDescription);
-+ exit (EXIT_FAILURE);
++ return 1;
+ }
+
+@@ -69,8 +68,7 @@ test_reproducers (void)
@@ -69,8 +68,7 @@ test_reproducers (void)
+- asn1_perror (result);
+- printf ("ErrorDescription = %s\n\n", errorDescription);
++ grub_printf ("Error: %s\nErrorDescription = %s\n\n", asn1_strerror (result), errorDescription);
-+ exit (EXIT_FAILURE);
++ return 1;
+ }
+
+--
-+2.35.3
++2.43.0
+
13: a1a1f32a6 < -: --------- asn1_test: return either 0 or 1 to reflect the results
14: 8e33e1600 ! 14: 859e00234 asn1_test: use the grub-specific functions and types
@@ Commit message
Signed-off-by: Daniel Axtens <[email protected]>
Signed-off-by: Gary Lin <[email protected]>
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/lib/libtasn1-patches/0012-asn1_test-use-the-grub-specific-functions-and-types.patch (new) ##
@@
-+From 625c23cdb7599ee4fa5e4991864fb69ccb89cae3 Mon Sep 17 00:00:00 2001
++From 392da24a8a4b2ab94a93e9e0f74db027a3a9374d Mon Sep 17 00:00:00 2001
+From: Gary Lin <[email protected]>
+Date: Fri, 16 Aug 2024 15:46:59 +0800
-+Subject: [PATCH 12/12] asn1_test: use the grub-specific functions and types
++Subject: [PATCH 12/13] asn1_test: use the grub-specific functions and types
+
+This commit converts functions and types to the grub-specific ones:
+
@@ grub-core/lib/libtasn1-patches/0012-asn1_test-use-the-grub-specific-functions-an
+
+Signed-off-by: Daniel Axtens <[email protected]>
+Signed-off-by: Gary Lin <[email protected]>
++Reviewed-by: Daniel Kiper <[email protected]>
+---
+ grub-core/tests/asn1/tests/Test_overflow.c | 4 ++--
+ grub-core/tests/asn1/tests/Test_simple.c | 22 +++++++++----------
@@ -209,7 +209,7 @@ test_octet_string (void)
+
+ }
+--
-+2.35.3
++2.43.0
+
-: --------- > 15: 1572cd8af asn1_test: exclude a testcase when long and int are the same
15: 9731f42c9 = 16: 01bc77f99 libtasn1: compile into asn1 module
16: 6fd2a949c ! 17: 9cd195194 asn1_test: test module for libtasn1
@@ autogen.sh: mkdir -p grub-core/lib/libtasn1-grub/lib
+ 0006-libtasn1-fix-the-potential-buffer-overrun.patch \
+ 0007-asn1_test-include-asn1_test.h-only.patch \
+ 0008-asn1_test-rename-the-main-functions-to-the-test-name.patch \
-+ 0009-asn1_test-remove-verbose-and-the-unnecessary-printf.patch \
-+ 0010-asn1_test-print-the-error-messages-with-grub_printf.patch \
-+ 0011-asn1_test-return-either-0-or-1-to-reflect-the-result.patch \
-+ 0012-asn1_test-use-the-grub-specific-functions-and-types.patch ; do
++ 0009-asn1_test-return-either-0-or-1-to-reflect-the-result.patch \
++ 0010-asn1_test-remove-verbose-and-the-unnecessary-printf.patch \
++ 0011-asn1_test-print-the-error-messages-with-grub_printf.patch \
++ 0012-asn1_test-use-the-grub-specific-functions-and-types.patch \
++ 0013-asn1_test-exclude-a-testcase-when-long-and-int-are-t.patch ; do
patch -p1 -i grub-core/lib/libtasn1-patches/$patch
done17: a7ae94e27 = 18: d3b6748e4 libtasn1: Add the documentation
18: 53342acde = 19: 9fed5b9b0 key_protector: Add key protectors framework
19: db99647d5 = 20: e2535cf2f tss2: Add TPM2 buffer handling functions
20: a5e494bb2 = 21: c7f0aa98c tss2: Add TPM2 types and Marshal/Unmarshal functions
21: 47cc29d2d ! 22: 55ad182d5 tss2: Add TPM2 Software Stack (TSS2) support
@@ grub-core/lib/tss2/tpm2_cmd.c (new)
+ if (buf.error != 0)
+ return TPM_RC_FAILURE;
+
-+ command_size = grub_swap_bytes32 (buf.size);
-+ grub_memcpy (&buf.data[sizeof (grub_uint16_t)], &command_size,
-+ sizeof (command_size));
++ /* Convert the command size to big endian to fill the u32 buffer below 'tag' */
++ command_size = grub_cpu_to_be32 (buf.size);
++ grub_memcpy (&buf.data[sizeof (grub_uint16_t)], &command_size, sizeof (command_size));
+
+ /* Stay within output block limits */
+ err = grub_tcg2_get_max_output_size (&max_output_size);
22: 12d09a2c8 ! 23: b74c06c22 key_protector: Add TPM2 Key Protector
@@ Commit message
For instance, to unseal the raw sealed key file:
- tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub2/sealed-1.key
+ tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed-1.key
cryptomount -u <PART1_UUID> -P tpm2
- tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub2/sealed-2.key --pcrs=7,11
+ tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/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
+ tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub/sealed-1.tpm
cryptomount -u <PART1_UUID> -P tpm2
- tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub2/sealed-2.tpm --pcrs=7,11
+ tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub/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
@@ grub-core/commands/tpm2_key_protector/args.c (new)
+ if (next_pcr != NULL)
+ *next_pcr = '\0';
+
-+ grub_errno = GRUB_ERR_NONE;
+ pcr = grub_strtoul (current_pcr, &pcr_end, 10);
+ if (*current_pcr == '\0' || *pcr_end != '\0')
+ return grub_error (GRUB_ERR_BAD_NUMBER, "Entry '%s' in PCR list is not a number", current_pcr);
@@ grub-core/commands/tpm2_key_protector/args.c (new)
+ grub_uint64_t num;
+ const char *str_end;
+
-+ grub_errno = GRUB_ERR_NONE;
+ num = grub_strtoul (value, &str_end, 0);
+ if (*value == '\0' || *str_end != '\0')
+ return grub_error (GRUB_ERR_BAD_NUMBER, "TPM handle value '%s' is not a number", value);
+
+ if (num > GRUB_UINT_MAX)
-+ return grub_error (GRUB_ERR_OUT_OF_RANGE, "Value %lu is too large to be a TPM handle, TPM handles are unsigned 32-bit integers", num);
++ return grub_error (GRUB_ERR_OUT_OF_RANGE, "Value %" PRIuGRUB_UINT64_T "is too large to be a TPM handle, TPM handles are unsigned 32-bit integers", num);
+
+ *handle = (TPM_HANDLE_t) num;
+
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
-+typedef enum grub_tpm2_protector_mode
++typedef enum tpm2_protector_mode
+{
-+ GRUB_TPM2_PROTECTOR_MODE_UNSET,
-+ GRUB_TPM2_PROTECTOR_MODE_SRK,
-+ GRUB_TPM2_PROTECTOR_MODE_NV
-+} grub_tpm2_protector_mode_t;
++ TPM2_PROTECTOR_MODE_UNSET,
++ TPM2_PROTECTOR_MODE_SRK,
++ TPM2_PROTECTOR_MODE_NV
++} tpm2_protector_mode_t;
+
-+enum grub_tpm2_protector_options
++typedef enum tpm2_protector_options
+{
+ OPTION_MODE,
+ OPTION_PCRS,
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ OPTION_SRK,
+ OPTION_ASYMMETRIC,
+ OPTION_NVINDEX
-+};
++} tpm2_protector_options_t;
+
-+struct grub_tpm2_protector_context
++typedef struct tpm2_protector_context
+{
-+ grub_tpm2_protector_mode_t mode;
++ tpm2_protector_mode_t mode;
+ grub_uint8_t pcrs[TPM_MAX_PCRS];
+ grub_uint8_t pcr_count;
+ grub_srk_type_t srk_type;
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ const char *keyfile;
+ TPM_HANDLE_t srk;
+ TPM_HANDLE_t nv;
-+};
++} tpm2_protector_context_t;
+
-+static const struct grub_arg_option grub_tpm2_protector_init_cmd_options[] =
++static const struct grub_arg_option tpm2_protector_init_cmd_options[] =
+ {
+ /* Options for all modes */
+ {
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ {0, 0, 0, 0, 0, 0}
+ };
+
-+static grub_extcmd_t grub_tpm2_protector_init_cmd;
-+static grub_extcmd_t grub_tpm2_protector_clear_cmd;
-+static struct grub_tpm2_protector_context grub_tpm2_protector_ctx = {0};
++static grub_extcmd_t tpm2_protector_init_cmd;
++static grub_extcmd_t tpm2_protector_clear_cmd;
++static tpm2_protector_context_t tpm2_protector_ctx = {0};
+
+static grub_err_t
+tpm2_protector_srk_read_file (const char *filepath, void **buffer, grub_size_t *buffer_size)
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+}
+
+static grub_err_t
-+tpm2_protector_load_key (const struct grub_tpm2_protector_context *ctx,
++tpm2_protector_load_key (const tpm2_protector_context_t *ctx,
+ const tpm2_sealed_key_t *sealed_key,
+ const TPM_HANDLE_t parent_handle,
+ TPM_HANDLE_t *sealed_handle,
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+}
+
+static grub_err_t
-+tpm2_protector_simple_policy_seq (const struct grub_tpm2_protector_context *ctx,
++tpm2_protector_simple_policy_seq (const tpm2_protector_context_t *ctx,
+ tpm2key_policy_t *policy_seq)
+{
+ tpm2key_policy_t policy = NULL;
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+}
+
+static grub_err_t
-+tpm2_protector_srk_recover (const struct grub_tpm2_protector_context *ctx,
++tpm2_protector_srk_recover (const tpm2_protector_context_t *ctx,
+ grub_uint8_t **key, grub_size_t *key_size)
+{
+ tpm2_sealed_key_t sealed_key = {0};
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+
+ if (rsaparent == 1)
+ {
-+ struct grub_tpm2_protector_context *ctx_w;
++ tpm2_protector_context_t *ctx_w;
+
+ /* Overwrite the SRK type as noted in the key */
-+ ctx_w = (struct grub_tpm2_protector_context *)ctx;
++ ctx_w = (tpm2_protector_context_t *)ctx;
+ ctx_w->srk_type.type = TPM_ALG_RSA;
+ ctx_w->srk_type.detail.rsa_bits = 2048;
+ }
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+}
+
+static grub_err_t
-+tpm2_protector_nv_recover (const struct grub_tpm2_protector_context *ctx __attribute__ ((unused)),
++tpm2_protector_nv_recover (const tpm2_protector_context_t *ctx __attribute__ ((unused)),
+ grub_uint8_t **key __attribute__ ((unused)),
+ grub_size_t *key_size __attribute__ ((unused)))
+{
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+}
+
+static grub_err_t
-+tpm2_protector_recover (const struct grub_tpm2_protector_context *ctx,
++tpm2_protector_recover (const tpm2_protector_context_t *ctx,
+ grub_uint8_t **key, grub_size_t *key_size)
+{
+ switch (ctx->mode)
+ {
-+ case GRUB_TPM2_PROTECTOR_MODE_SRK:
++ case TPM2_PROTECTOR_MODE_SRK:
+ return tpm2_protector_srk_recover (ctx, key, key_size);
-+ case GRUB_TPM2_PROTECTOR_MODE_NV:
++ case TPM2_PROTECTOR_MODE_NV:
+ return tpm2_protector_nv_recover (ctx, key, key_size);
+ default:
+ return GRUB_ERR_BAD_ARGUMENT;
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+tpm2_protector_recover_key (grub_uint8_t **key, grub_size_t *key_size)
+{
+ /* Expect a call to tpm2_protector_init before anybody tries to use us */
-+ if (grub_tpm2_protector_ctx.mode == GRUB_TPM2_PROTECTOR_MODE_UNSET)
++ if (tpm2_protector_ctx.mode == TPM2_PROTECTOR_MODE_UNSET)
+ return grub_error (GRUB_ERR_INVALID_COMMAND, "Cannot use TPM2 key protector without initializing it, call tpm2_protector_init first");
+
+ if (key == NULL || key_size == NULL)
+ return GRUB_ERR_BAD_ARGUMENT;
+
-+ return tpm2_protector_recover (&grub_tpm2_protector_ctx, key, key_size);
++ return tpm2_protector_recover (&tpm2_protector_ctx, key, key_size);
+}
+
+static grub_err_t
-+tpm2_protector_check_args (struct grub_tpm2_protector_context *ctx)
++tpm2_protector_check_args (tpm2_protector_context_t *ctx)
+{
-+ if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_UNSET)
-+ ctx->mode = GRUB_TPM2_PROTECTOR_MODE_SRK;
++ if (ctx->mode == TPM2_PROTECTOR_MODE_UNSET)
++ ctx->mode = TPM2_PROTECTOR_MODE_SRK;
+
+ /* Checks for SRK mode */
-+ if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK &&
++ if (ctx->mode == TPM2_PROTECTOR_MODE_SRK &&
+ (ctx->keyfile == NULL && ctx->tpm2key == NULL))
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "In SRK mode, a key file must be specified: --tpm2key/-T or --keyfile/-k");
+
-+ if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK &&
++ if (ctx->mode == TPM2_PROTECTOR_MODE_SRK &&
+ (ctx->keyfile != NULL && ctx->tpm2key != NULL))
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "In SRK mode, please specify a key file with only --tpm2key/-T or --keyfile/-k");
+
-+ if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK && ctx->nv != 0)
++ if (ctx->mode == TPM2_PROTECTOR_MODE_SRK && ctx->nv != 0)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "In SRK mode, an NV Index cannot be specified");
+
+ /* Checks for NV mode */
-+ if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_NV && ctx->nv == 0)
++ if (ctx->mode == TPM2_PROTECTOR_MODE_NV && ctx->nv == 0)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "In NV Index mode, an NV Index must be specified: --nvindex or -n");
+
-+ if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_NV &&
++ if (ctx->mode == TPM2_PROTECTOR_MODE_NV &&
+ (ctx->tpm2key != NULL || ctx->keyfile != NULL))
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "In NV Index mode, a keyfile cannot be specified");
+
-+ if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_NV && ctx->srk != 0)
++ if (ctx->mode == TPM2_PROTECTOR_MODE_NV && ctx->srk != 0)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "In NV Index mode, an SRK cannot be specified");
+
-+ if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_NV &&
++ if (ctx->mode == TPM2_PROTECTOR_MODE_NV &&
+ ctx->srk_type.type != TPM_ALG_ERROR)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "In NV Index mode, an asymmetric key type cannot be specified");
+
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ ctx->pcr_count = 1;
+ }
+
-+ if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK &&
++ if (ctx->mode == TPM2_PROTECTOR_MODE_SRK &&
+ ctx->srk_type.type == TPM_ALG_ERROR)
+ {
+ ctx->srk_type.type = TPM_ALG_ECC;
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+}
+
+static grub_err_t
-+tpm2_protector_parse_mode (const char *value, grub_tpm2_protector_mode_t *mode)
++tpm2_protector_parse_mode (const char *value, tpm2_protector_mode_t *mode)
+{
+ if (grub_strcmp (value, "srk") == 0)
-+ *mode = GRUB_TPM2_PROTECTOR_MODE_SRK;
++ *mode = TPM2_PROTECTOR_MODE_SRK;
+ else if (grub_strcmp (value, "nv") == 0)
-+ *mode = GRUB_TPM2_PROTECTOR_MODE_NV;
++ *mode = TPM2_PROTECTOR_MODE_NV;
+ else
+ return grub_error (GRUB_ERR_OUT_OF_RANGE, "Value '%s' is not a valid TPM2 key protector mode", value);
+
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ if (argc)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "The TPM2 key protector does not accept any non-option arguments (i.e., like -o and/or --option only)");
+
-+ grub_free ((void *) grub_tpm2_protector_ctx.keyfile);
-+ grub_memset (&grub_tpm2_protector_ctx, 0, sizeof (grub_tpm2_protector_ctx));
++ grub_free ((void *) tpm2_protector_ctx.keyfile);
++ grub_memset (&tpm2_protector_ctx, 0, sizeof (tpm2_protector_ctx));
+
+ if (state[OPTION_MODE].set) /* mode */
+ {
-+ err = tpm2_protector_parse_mode (state[OPTION_MODE].arg, &grub_tpm2_protector_ctx.mode);
++ err = tpm2_protector_parse_mode (state[OPTION_MODE].arg, &tpm2_protector_ctx.mode);
+ if (err != GRUB_ERR_NONE)
+ return err;
+ }
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ if (state[OPTION_PCRS].set) /* pcrs */
+ {
+ err = grub_tpm2_protector_parse_pcrs (state[OPTION_PCRS].arg,
-+ grub_tpm2_protector_ctx.pcrs,
-+ &grub_tpm2_protector_ctx.pcr_count);
++ tpm2_protector_ctx.pcrs,
++ &tpm2_protector_ctx.pcr_count);
+ if (err != GRUB_ERR_NONE)
+ return err;
+ }
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ if (state[OPTION_BANK].set) /* bank */
+ {
+ err = grub_tpm2_protector_parse_bank (state[OPTION_BANK].arg,
-+ &grub_tpm2_protector_ctx.bank);
++ &tpm2_protector_ctx.bank);
+ if (err != GRUB_ERR_NONE)
+ return err;
+ }
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ if (state[OPTION_TPM2KEY].set) /* tpm2key */
+ {
+ err = tpm2_protector_parse_file (state[OPTION_TPM2KEY].arg,
-+ &grub_tpm2_protector_ctx.tpm2key);
++ &tpm2_protector_ctx.tpm2key);
+ if (err != GRUB_ERR_NONE)
+ return err;
+ }
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ if (state[OPTION_KEYFILE].set) /* keyfile */
+ {
+ err = tpm2_protector_parse_file (state[OPTION_KEYFILE].arg,
-+ &grub_tpm2_protector_ctx.keyfile);
++ &tpm2_protector_ctx.keyfile);
+ if (err != GRUB_ERR_NONE)
+ return err;
+ }
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ if (state[OPTION_SRK].set) /* srk */
+ {
+ err = grub_tpm2_protector_parse_tpm_handle (state[OPTION_SRK].arg,
-+ &grub_tpm2_protector_ctx.srk);
++ &tpm2_protector_ctx.srk);
+ if (err != GRUB_ERR_NONE)
+ return err;
+ }
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ if (state[OPTION_ASYMMETRIC].set) /* asymmetric */
+ {
+ err = grub_tpm2_protector_parse_asymmetric (state[OPTION_ASYMMETRIC].arg,
-+ &grub_tpm2_protector_ctx.srk_type);
++ &tpm2_protector_ctx.srk_type);
+ if (err != GRUB_ERR_NONE)
+ return err;
+ }
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ if (state[OPTION_NVINDEX].set) /* nvindex */
+ {
+ err = grub_tpm2_protector_parse_tpm_handle (state[OPTION_NVINDEX].arg,
-+ &grub_tpm2_protector_ctx.nv);
++ &tpm2_protector_ctx.nv);
+ if (err != GRUB_ERR_NONE)
+ return err;
+ }
+
-+ err = tpm2_protector_check_args (&grub_tpm2_protector_ctx);
++ err = tpm2_protector_check_args (&tpm2_protector_ctx);
+
+ /* This command only initializes the protector, so nothing else to do. */
+
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ if (argc)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "tpm2_key_protector_clear accepts no arguments");
+
-+ grub_free ((void *) grub_tpm2_protector_ctx.keyfile);
-+ grub_memset (&grub_tpm2_protector_ctx, 0, sizeof (grub_tpm2_protector_ctx));
++ grub_free ((void *) tpm2_protector_ctx.keyfile);
++ grub_memset (&tpm2_protector_ctx, 0, sizeof (tpm2_protector_ctx));
+
+ return GRUB_ERR_NONE;
+}
+
-+static struct grub_key_protector grub_tpm2_key_protector =
++static struct grub_key_protector tpm2_key_protector =
+ {
+ .name = "tpm2",
+ .recover_key = tpm2_protector_recover_key
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+
+GRUB_MOD_INIT (tpm2_key_protector)
+{
-+ grub_tpm2_protector_init_cmd =
++ tpm2_protector_init_cmd =
+ grub_register_extcmd ("tpm2_key_protector_init",
+ tpm2_protector_init_cmd_handler, 0,
+ N_("[-m mode] "
@@ grub-core/commands/tpm2_key_protector/module.c (new)
+ "[-a asymmetric_key_type] "
+ "[-n nv_index]"),
+ N_("Initialize the TPM2 key protector."),
-+ grub_tpm2_protector_init_cmd_options);
-+ grub_tpm2_protector_clear_cmd =
++ tpm2_protector_init_cmd_options);
++ tpm2_protector_clear_cmd =
+ grub_register_extcmd ("tpm2_key_protector_clear",
+ tpm2_protector_clear_cmd_handler, 0, NULL,
+ N_("Clear the TPM2 key protector if previously initialized."),
+ NULL);
-+ grub_key_protector_register (&grub_tpm2_key_protector);
++ grub_key_protector_register (&tpm2_key_protector);
+}
+
+GRUB_MOD_FINI (tpm2_key_protector)
+{
-+ grub_free ((void *) grub_tpm2_protector_ctx.keyfile);
++ grub_free ((void *) tpm2_protector_ctx.keyfile);
+
-+ grub_key_protector_unregister (&grub_tpm2_key_protector);
-+ grub_unregister_extcmd (grub_tpm2_protector_clear_cmd);
-+ grub_unregister_extcmd (grub_tpm2_protector_init_cmd);
++ grub_key_protector_unregister (&tpm2_key_protector);
++ grub_unregister_extcmd (tpm2_protector_clear_cmd);
++ grub_unregister_extcmd (tpm2_protector_init_cmd);
+}
## grub-core/commands/tpm2_key_protector/tpm2.h (new) ##23: 9f346b58e ! 24: 903b1249b cryptodisk: Support key protectors
@@ Makefile.util.def: library = {
common = grub-core/disk/AFSplitter.c;
common = grub-core/lib/pbkdf2.c;
+ ## docs/grub.texi ##
+@@ docs/grub.texi: Alias for @code{hashsum --hash crc32 arg @dots{}}. See command @command{hashsum}
+ @node cryptomount
+ @subsection cryptomount
+
+-@deffn Command cryptomount [ [@option{-p} password] | [@option{-k} keyfile [@option{-O} keyoffset] [@option{-S} keysize] ] ] [@option{-H} file] device|@option{-u} uuid|@option{-a}|@option{-b} ++@deffn Command cryptomount [ [@option{-p} password] | [@option{-k} keyfile [@option{-O} keyoffset] [@option{-S} keysize] ] | [@option{-P} protector] ] [@option{-H} file] device|@option{-u} uuid|@option{-a}|@option{-b}
+ Setup access to encrypted device. A passphrase will be requested interactively,
+ if neither the @option{-p} nor @option{-k} options are given. The option
+ @option{-p} can be used to supply a passphrase (useful for scripts).
+ Alternatively the @option{-k} option can be used to supply a keyfile with
+ options @option{-O} and @option{-S} optionally supplying the offset and size,
+-respectively, of the key data in the given key file. The @option{-H} options can
+-be used to supply cryptomount backends with an alternative header file (aka
+-detached header). Not all backends have headers nor support alternative header
+-files (currently only LUKS1 and LUKS2 support them).
++respectively, of the key data in the given key file. Besides the keyfile,
++the key can be stored in a key protector, and option @option{-P} configures
++specific key protector, e.g. tpm2, to retrieve the key from.
++The @option{-H} options can be used to supply cryptomount backends with an
++alternative header file (aka detached header). Not all backends have headers
++nor support alternative header files (currently only LUKS1 and LUKS2 support them).
+ Argument @var{device} configures specific grub device
+ (@pxref{Naming convention}); option @option{-u} @var{uuid} configures device
+ with specified @var{uuid}; option @option{-a} configures all detected encrypted
+
## grub-core/disk/cryptodisk.c ##
@@
#include <grub/file.h>
24: 08eab00ac ! 25: 5fdb8b86f util/grub-protect: Add new tool
@@ Commit message
--tpm2-pcrs=0,2,4,7,9 \
--tpm2key \
--tpm2-keyfile=luks-key \
- --tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm
+ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm
Or, to seal the key with the raw sealed key:
@@ Commit message
--protector=tpm2 \
--tpm2-pcrs=0,2,4,7,9 \
--tpm2-keyfile=luks-key \
- --tpm2-outfile=/boot/efi/boot/grub2/sealed.key
+ --tpm2-outfile=/boot/efi/efi/grub/sealed.key
Then, in the boot script, for TPM 2.0 Key File:
- tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm
+ tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub/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 --pcrs=0,2,4,7,9
+ tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.key --pcrs=0,2,4,7,9
cryptomount -u <SDB1_UUID> -P tpm2
The benefit of using TPM 2.0 Key File is that the PCR set is already
@@ util/grub-protect.c (new)
+#include "progname.h"
+
+/* Unprintable option keys for argp */
-+typedef enum grub_protect_opt
++typedef enum protect_opt
+{
+ /* General */
-+ GRUB_PROTECT_OPT_ACTION = 'a',
-+ GRUB_PROTECT_OPT_PROTECTOR = 'p',
++ PROTECT_OPT_ACTION = 'a',
++ PROTECT_OPT_PROTECTOR = 'p',
+ /* TPM2 */
-+ GRUB_PROTECT_OPT_TPM2_DEVICE = 0x100,
-+ GRUB_PROTECT_OPT_TPM2_PCRS,
-+ GRUB_PROTECT_OPT_TPM2_ASYMMETRIC,
-+ GRUB_PROTECT_OPT_TPM2_BANK,
-+ GRUB_PROTECT_OPT_TPM2_SRK,
-+ GRUB_PROTECT_OPT_TPM2_KEYFILE,
-+ GRUB_PROTECT_OPT_TPM2_OUTFILE,
-+ GRUB_PROTECT_OPT_TPM2_EVICT,
-+ GRUB_PROTECT_OPT_TPM2_TPM2KEY
-+} grub_protect_opt;
++ PROTECT_OPT_TPM2_DEVICE = 0x100,
++ PROTECT_OPT_TPM2_PCRS,
++ PROTECT_OPT_TPM2_ASYMMETRIC,
++ PROTECT_OPT_TPM2_BANK,
++ PROTECT_OPT_TPM2_SRK,
++ PROTECT_OPT_TPM2_KEYFILE,
++ PROTECT_OPT_TPM2_OUTFILE,
++ PROTECT_OPT_TPM2_EVICT,
++ PROTECT_OPT_TPM2_TPM2KEY
++} protect_opt_t;
+
+/* Option flags to keep track of specified arguments */
-+typedef enum grub_protect_arg
++typedef enum protect_arg
+{
+ /* General */
-+ GRUB_PROTECT_ARG_ACTION = 1 << 0,
-+ GRUB_PROTECT_ARG_PROTECTOR = 1 << 1,
++ PROTECT_ARG_ACTION = 1 << 0,
++ PROTECT_ARG_PROTECTOR = 1 << 1,
+ /* TPM2 */
-+ GRUB_PROTECT_ARG_TPM2_DEVICE = 1 << 2,
-+ GRUB_PROTECT_ARG_TPM2_PCRS = 1 << 3,
-+ GRUB_PROTECT_ARG_TPM2_ASYMMETRIC = 1 << 4,
-+ GRUB_PROTECT_ARG_TPM2_BANK = 1 << 5,
-+ GRUB_PROTECT_ARG_TPM2_SRK = 1 << 6,
-+ GRUB_PROTECT_ARG_TPM2_KEYFILE = 1 << 7,
-+ GRUB_PROTECT_ARG_TPM2_OUTFILE = 1 << 8,
-+ GRUB_PROTECT_ARG_TPM2_EVICT = 1 << 9,
-+ GRUB_PROTECT_ARG_TPM2_TPM2KEY = 1 << 10
-+} grub_protect_arg_t;
-+
-+typedef enum grub_protect_protector
++ PROTECT_ARG_TPM2_DEVICE = 1 << 2,
++ PROTECT_ARG_TPM2_PCRS = 1 << 3,
++ PROTECT_ARG_TPM2_ASYMMETRIC = 1 << 4,
++ PROTECT_ARG_TPM2_BANK = 1 << 5,
++ PROTECT_ARG_TPM2_SRK = 1 << 6,
++ PROTECT_ARG_TPM2_KEYFILE = 1 << 7,
++ PROTECT_ARG_TPM2_OUTFILE = 1 << 8,
++ PROTECT_ARG_TPM2_EVICT = 1 << 9,
++ PROTECT_ARG_TPM2_TPM2KEY = 1 << 10
++} protect_arg_t;
++
++typedef enum protect_protector
+{
-+ GRUB_PROTECT_TYPE_ERROR,
-+ GRUB_PROTECT_TYPE_TPM2
-+} grub_protect_protector_t;
++ PROTECT_TYPE_ERROR,
++ PROTECT_TYPE_TPM2
++} protect_protector_t;
+
-+typedef enum grub_protect_action
++typedef enum protect_action
+{
-+ GRUB_PROTECT_ACTION_ERROR,
-+ GRUB_PROTECT_ACTION_ADD,
-+ GRUB_PROTECT_ACTION_REMOVE
-+} grub_protect_action_t;
++ PROTECT_ACTION_ERROR,
++ PROTECT_ACTION_ADD,
++ PROTECT_ACTION_REMOVE
++} protect_action_t;
+
-+struct grub_protect_args
++typedef struct protect_args
+{
-+ grub_protect_arg_t args;
-+ grub_protect_action_t action;
-+ grub_protect_protector_t protector;
++ protect_arg_t args;
++ protect_action_t action;
++ protect_protector_t protector;
+
+ const char *tpm2_device;
+ grub_uint8_t tpm2_pcrs[TPM_MAX_PCRS];
@@ util/grub-protect.c (new)
+ const char *tpm2_outfile;
+ int tpm2_evict;
+ int tpm2_tpm2key;
-+};
++} protect_args_t;
+
-+static struct argp_option grub_protect_options[] =
++static struct argp_option protect_options[] =
+ {
+ /* Top-level options */
+ {
@@ util/grub-protect.c (new)
+ /* TPM2 key protector options */
+ {
+ .name = "tpm2-device",
-+ .key = GRUB_PROTECT_OPT_TPM2_DEVICE,
++ .key = PROTECT_OPT_TPM2_DEVICE,
+ .arg = "FILE",
+ .flags = 0,
+ .doc =
@@ util/grub-protect.c (new)
+ },
+ {
+ .name = "tpm2-pcrs",
-+ .key = GRUB_PROTECT_OPT_TPM2_PCRS,
++ .key = PROTECT_OPT_TPM2_PCRS,
+ .arg = "0[,1]...",
+ .flags = 0,
+ .doc =
@@ util/grub-protect.c (new)
+ },
+ {
+ .name = "tpm2-bank",
-+ .key = GRUB_PROTECT_OPT_TPM2_BANK,
++ .key = PROTECT_OPT_TPM2_BANK,
+ .arg = "ALG",
+ .flags = 0,
+ .doc =
@@ util/grub-protect.c (new)
+ },
+ {
+ .name = "tpm2-keyfile",
-+ .key = GRUB_PROTECT_OPT_TPM2_KEYFILE,
++ .key = PROTECT_OPT_TPM2_KEYFILE,
+ .arg = "FILE",
+ .flags = 0,
+ .doc =
@@ util/grub-protect.c (new)
+ },
+ {
+ .name = "tpm2-outfile",
-+ .key = GRUB_PROTECT_OPT_TPM2_OUTFILE,
++ .key = PROTECT_OPT_TPM2_OUTFILE,
+ .arg = "FILE",
+ .flags = 0,
+ .doc =
@@ util/grub-protect.c (new)
+ },
+ {
+ .name = "tpm2-srk",
-+ .key = GRUB_PROTECT_OPT_TPM2_SRK,
++ .key = PROTECT_OPT_TPM2_SRK,
+ .arg = "NUM",
+ .flags = 0,
+ .doc =
@@ util/grub-protect.c (new)
+ },
+ {
+ .name = "tpm2-asymmetric",
-+ .key = GRUB_PROTECT_OPT_TPM2_ASYMMETRIC,
++ .key = PROTECT_OPT_TPM2_ASYMMETRIC,
+ .arg = "TYPE",
+ .flags = 0,
+ .doc =
@@ util/grub-protect.c (new)
+ },
+ {
+ .name = "tpm2-evict",
-+ .key = GRUB_PROTECT_OPT_TPM2_EVICT,
++ .key = PROTECT_OPT_TPM2_EVICT,
+ .arg = NULL,
+ .flags = 0,
+ .doc =
@@ util/grub-protect.c (new)
+ },
+ {
+ .name = "tpm2key",
-+ .key = GRUB_PROTECT_OPT_TPM2_TPM2KEY,
++ .key = PROTECT_OPT_TPM2_TPM2KEY,
+ .arg = NULL,
+ .flags = 0,
+ .doc =
-+ N_("Use TPM 2.0 Key File format instead of the raw format."),
++ N_("Use TPM 2.0 Key File instead of the raw format."),
+ .group = 0
+ },
+ /* End of list */
@@ util/grub-protect.c (new)
+ if (fwrite (buffer, buffer_size, 1, f) != 1)
+ {
+ err = GRUB_ERR_WRITE_ERROR;
-+ goto exit1;
++ goto exit;
+ }
+
+ err = GRUB_ERR_NONE;
+
-+ exit1:
++ exit:
+ fclose (f);
+
+ return err;
@@ util/grub-protect.c (new)
+}
+
+static grub_err_t
-+protect_tpm2_get_policy_digest (struct grub_protect_args *args, TPM2B_DIGEST_t *digest)
++protect_tpm2_get_policy_digest (protect_args_t *args, TPM2B_DIGEST_t *digest)
+{
+ TPM_RC_t rc;
+ TPML_PCR_SELECTION_t pcr_sel = {
@@ util/grub-protect.c (new)
+ {
+ .hash = args->tpm2_bank,
+ .sizeOfSelect = 3,
-+ .pcrSelect = { 0 }
++ .pcrSelect = {0}
+ },
+ }
+ };
-+ TPML_PCR_SELECTION_t pcr_sel_out = { 0 };
-+ TPML_DIGEST_t pcr_values = { 0 };
-+ TPM2B_DIGEST_t pcr_digest = { 0 };
++ TPML_PCR_SELECTION_t pcr_sel_out = {0};
++ TPML_DIGEST_t pcr_values = {0};
++ TPM2B_DIGEST_t pcr_digest = {0};
+ grub_size_t pcr_digest_len;
-+ TPM2B_MAX_BUFFER_t pcr_concat = { 0 };
++ TPM2B_MAX_BUFFER_t pcr_concat = {0};
+ grub_size_t pcr_concat_len;
+ grub_uint8_t *pcr_cursor;
-+ TPM2B_NONCE_t nonce = { 0 };
-+ TPM2B_ENCRYPTED_SECRET_t salt = { 0 };
-+ TPMT_SYM_DEF_t symmetric = { 0 };
++ TPM2B_NONCE_t nonce = {0};
++ TPM2B_ENCRYPTED_SECRET_t salt = {0};
++ TPMT_SYM_DEF_t symmetric = {0};
+ TPMI_SH_AUTH_SESSION_t session = 0;
-+ TPM2B_DIGEST_t policy_digest = { 0 };
++ TPM2B_DIGEST_t policy_digest = {0};
+ grub_uint8_t i;
+ grub_err_t err;
+
@@ util/grub-protect.c (new)
+}
+
+static grub_err_t
-+protect_tpm2_get_srk (struct grub_protect_args *args, TPM_HANDLE_t *srk)
++protect_tpm2_get_srk (protect_args_t *args, TPM_HANDLE_t *srk)
+{
+ TPM_RC_t rc;
+ TPM2B_PUBLIC_t public;
-+ TPMS_AUTH_COMMAND_t authCommand = { 0 };
-+ TPM2B_SENSITIVE_CREATE_t inSensitive = { 0 };
-+ TPM2B_PUBLIC_t inPublic = { 0 };
-+ TPM2B_DATA_t outsideInfo = { 0 };
-+ TPML_PCR_SELECTION_t creationPcr = { 0 };
-+ TPM2B_PUBLIC_t outPublic = { 0 };
-+ TPM2B_CREATION_DATA_t creationData = { 0 };
-+ TPM2B_DIGEST_t creationHash = { 0 };
-+ TPMT_TK_CREATION_t creationTicket = { 0 };
-+ TPM2B_NAME_t srkName = { 0 };
++ TPMS_AUTH_COMMAND_t authCommand = {0};
++ TPM2B_SENSITIVE_CREATE_t inSensitive = {0};
++ TPM2B_PUBLIC_t inPublic = {0};
++ TPM2B_DATA_t outsideInfo = {0};
++ TPML_PCR_SELECTION_t creationPcr = {0};
++ TPM2B_PUBLIC_t outPublic = {0};
++ TPM2B_CREATION_DATA_t creationData = {0};
++ TPM2B_DIGEST_t creationHash = {0};
++ TPMT_TK_CREATION_t creationTicket = {0};
++ TPM2B_NAME_t srkName = {0};
+ TPM_HANDLE_t srkHandle;
+
+ if (args->tpm2_srk != 0)
@@ util/grub-protect.c (new)
+ tpm2_sealed_key_t *sealed_key)
+{
+ TPM_RC_t rc;
-+ TPMS_AUTH_COMMAND_t authCommand = { 0 };
-+ TPM2B_SENSITIVE_CREATE_t inSensitive = { 0 };
-+ TPM2B_PUBLIC_t inPublic = { 0 };
-+ TPM2B_DATA_t outsideInfo = { 0 };
-+ TPML_PCR_SELECTION_t pcr_sel = { 0 };
-+ TPM2B_PRIVATE_t outPrivate = { 0 };
-+ TPM2B_PUBLIC_t outPublic = { 0 };
++ TPMS_AUTH_COMMAND_t authCommand = {0};
++ TPM2B_SENSITIVE_CREATE_t inSensitive = {0};
++ TPM2B_PUBLIC_t inPublic = {0};
++ TPM2B_DATA_t outsideInfo = {0};
++ TPML_PCR_SELECTION_t pcr_sel = {0};
++ TPM2B_PRIVATE_t outPrivate = {0};
++ TPM2B_PUBLIC_t outPublic = {0};
+
+ /* Seal Data */
+ authCommand.sessionHandle = TPM_RS_PW;
@@ util/grub-protect.c (new)
+extern asn1_static_node tpm2key_asn1_tab[];
+
+static grub_err_t
-+protect_tpm2_export_tpm2key (const struct grub_protect_args *args,
++protect_tpm2_export_tpm2key (const protect_args_t *args,
+ tpm2_sealed_key_t *sealed_key)
+{
+ const char *sealed_key_oid = "2.23.133.10.1.5";
@@ util/grub-protect.c (new)
+ {
+ .hash = args->tpm2_bank,
+ .sizeOfSelect = 3,
-+ .pcrSelect = { 0 }
++ .pcrSelect = {0}
+ },
+ }
+ };
@@ util/grub-protect.c (new)
+}
+
+static grub_err_t
-+protect_tpm2_add (struct grub_protect_args *args)
++protect_tpm2_add (protect_args_t *args)
+{
+ grub_err_t err;
+ grub_uint8_t *key = NULL;
@@ util/grub-protect.c (new)
+}
+
+static grub_err_t
-+protect_tpm2_remove (struct grub_protect_args *args)
++protect_tpm2_remove (protect_args_t *args)
+{
+ TPM_RC_t rc;
+ TPM2B_PUBLIC_t public;
-+ TPMS_AUTH_COMMAND_t authCommand = { 0 };
++ TPMS_AUTH_COMMAND_t authCommand = {0};
+ grub_err_t err;
+
+ if (args->tpm2_evict == 0)
@@ util/grub-protect.c (new)
+}
+
+static grub_err_t
-+protect_tpm2_run (struct grub_protect_args *args)
++protect_tpm2_run (protect_args_t *args)
+{
+ switch (args->action)
+ {
-+ case GRUB_PROTECT_ACTION_ADD:
++ case PROTECT_ACTION_ADD:
+ return protect_tpm2_add (args);
+
-+ case GRUB_PROTECT_ACTION_REMOVE:
++ case PROTECT_ACTION_REMOVE:
+ return protect_tpm2_remove (args);
+
+ default:
@@ util/grub-protect.c (new)
+}
+
+static grub_err_t
-+protect_tpm2_args_verify (struct grub_protect_args *args)
++protect_tpm2_args_verify (protect_args_t *args)
+{
+ switch (args->action)
+ {
-+ case GRUB_PROTECT_ACTION_ADD:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_EVICT)
++ case PROTECT_ACTION_ADD:
++ if (args->args & PROTECT_ARG_TPM2_EVICT)
+ {
+ fprintf (stderr, N_("--tpm2-evict is invalid when --action is 'add'.\n"));
+ return GRUB_ERR_BAD_ARGUMENT;
@@ util/grub-protect.c (new)
+
+ break;
+
-+ case GRUB_PROTECT_ACTION_REMOVE:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_ASYMMETRIC)
++ case PROTECT_ACTION_REMOVE:
++ if (args->args & PROTECT_ARG_TPM2_ASYMMETRIC)
+ {
+ fprintf (stderr, N_("--tpm2-asymmetric is invalid when --action is 'remove'.\n"));
+ return GRUB_ERR_BAD_ARGUMENT;
+ }
+
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_BANK)
++ if (args->args & PROTECT_ARG_TPM2_BANK)
+ {
+ fprintf (stderr, N_("--tpm2-bank is invalid when --action is 'remove'.\n"));
+ return GRUB_ERR_BAD_ARGUMENT;
+ }
+
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_KEYFILE)
++ if (args->args & PROTECT_ARG_TPM2_KEYFILE)
+ {
+ fprintf (stderr, N_("--tpm2-keyfile is invalid when --action is 'remove'.\n"));
+ return GRUB_ERR_BAD_ARGUMENT;
+ }
+
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_OUTFILE)
++ if (args->args & PROTECT_ARG_TPM2_OUTFILE)
+ {
+ fprintf (stderr, N_("--tpm2-outfile is invalid when --action is 'remove'.\n"));
+ return GRUB_ERR_BAD_ARGUMENT;
+ }
+
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_PCRS)
++ if (args->args & PROTECT_ARG_TPM2_PCRS)
+ {
+ fprintf (stderr, N_("--tpm2-pcrs is invalid when --action is 'remove'.\n"));
+ return GRUB_ERR_BAD_ARGUMENT;
@@ util/grub-protect.c (new)
+protect_argp_parser (int key, char *arg, struct argp_state *state)
+{
+ grub_err_t err;
-+ struct grub_protect_args *args = state->input;
++ protect_args_t *args = state->input;
+
+ switch (key)
+ {
-+ case GRUB_PROTECT_OPT_ACTION:
-+ if (args->args & GRUB_PROTECT_ARG_ACTION)
++ case PROTECT_OPT_ACTION:
++ if (args->args & PROTECT_ARG_ACTION)
+ {
+ fprintf (stderr, N_("--action|-a can only be specified once.\n"));
+ return EINVAL;
+ }
+
+ if (grub_strcmp (arg, "add") == 0)
-+ args->action = GRUB_PROTECT_ACTION_ADD;
++ args->action = PROTECT_ACTION_ADD;
+ else if (grub_strcmp (arg, "remove") == 0)
-+ args->action = GRUB_PROTECT_ACTION_REMOVE;
++ args->action = PROTECT_ACTION_REMOVE;
+ else
+ {
+ fprintf (stderr, N_("'%s' is not a valid action.\n"), arg);
+ return EINVAL;
+ }
+
-+ args->args |= GRUB_PROTECT_ARG_ACTION;
++ args->args |= PROTECT_ARG_ACTION;
+ break;
+
-+ case GRUB_PROTECT_OPT_PROTECTOR:
-+ if (args->args & GRUB_PROTECT_ARG_PROTECTOR)
++ case PROTECT_OPT_PROTECTOR:
++ if (args->args & PROTECT_ARG_PROTECTOR)
+ {
+ fprintf (stderr, N_("--protector|-p can only be specified once.\n"));
+ return EINVAL;
+ }
+
+ if (grub_strcmp (arg, "tpm2") == 0)
-+ args->protector = GRUB_PROTECT_TYPE_TPM2;
++ args->protector = PROTECT_TYPE_TPM2;
+ else
+ {
+ fprintf (stderr, N_("'%s' is not a valid protector.\n"), arg);
+ return EINVAL;
+ }
+
-+ args->args |= GRUB_PROTECT_ARG_PROTECTOR;
++ args->args |= PROTECT_ARG_PROTECTOR;
+ break;
+
-+ case GRUB_PROTECT_OPT_TPM2_DEVICE:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_DEVICE)
++ case PROTECT_OPT_TPM2_DEVICE:
++ if (args->args & PROTECT_ARG_TPM2_DEVICE)
+ {
+ fprintf (stderr, N_("--tpm2-device can only be specified once.\n"));
+ return EINVAL;
+ }
+
+ args->tpm2_device = xstrdup(arg);
-+ args->args |= GRUB_PROTECT_ARG_TPM2_DEVICE;
++ args->args |= PROTECT_ARG_TPM2_DEVICE;
+ break;
+
-+ case GRUB_PROTECT_OPT_TPM2_PCRS:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_PCRS)
++ case PROTECT_OPT_TPM2_PCRS:
++ if (args->args & PROTECT_ARG_TPM2_PCRS)
+ {
+ fprintf (stderr, N_("--tpm2-pcrs can only be specified once.\n"));
+ return EINVAL;
@@ util/grub-protect.c (new)
+ return EINVAL;
+ }
+
-+ args->args |= GRUB_PROTECT_ARG_TPM2_PCRS;
++ args->args |= PROTECT_ARG_TPM2_PCRS;
+ break;
+
-+ case GRUB_PROTECT_OPT_TPM2_SRK:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_SRK)
++ case PROTECT_OPT_TPM2_SRK:
++ if (args->args & PROTECT_ARG_TPM2_SRK)
+ {
+ fprintf (stderr, N_("--tpm2-srk can only be specified once.\n"));
+ return EINVAL;
@@ util/grub-protect.c (new)
+ return EINVAL;
+ }
+
-+ args->args |= GRUB_PROTECT_ARG_TPM2_SRK;
++ args->args |= PROTECT_ARG_TPM2_SRK;
+ break;
+
-+ case GRUB_PROTECT_OPT_TPM2_ASYMMETRIC:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_ASYMMETRIC)
++ case PROTECT_OPT_TPM2_ASYMMETRIC:
++ if (args->args & PROTECT_ARG_TPM2_ASYMMETRIC)
+ {
+ fprintf (stderr, N_("--tpm2-asymmetric can only be specified once.\n"));
+ return EINVAL;
@@ util/grub-protect.c (new)
+ return EINVAL;
+ }
+
-+ args->args |= GRUB_PROTECT_ARG_TPM2_ASYMMETRIC;
++ args->args |= PROTECT_ARG_TPM2_ASYMMETRIC;
+ break;
+
-+ case GRUB_PROTECT_OPT_TPM2_BANK:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_BANK)
++ case PROTECT_OPT_TPM2_BANK:
++ if (args->args & PROTECT_ARG_TPM2_BANK)
+ {
+ fprintf (stderr, N_("--tpm2-bank can only be specified once.\n"));
+ return EINVAL;
@@ util/grub-protect.c (new)
+ return EINVAL;
+ }
+
-+ args->args |= GRUB_PROTECT_ARG_TPM2_BANK;
++ args->args |= PROTECT_ARG_TPM2_BANK;
+ break;
+
-+ case GRUB_PROTECT_OPT_TPM2_KEYFILE:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_KEYFILE)
++ case PROTECT_OPT_TPM2_KEYFILE:
++ if (args->args & PROTECT_ARG_TPM2_KEYFILE)
+ {
+ fprintf (stderr, N_("--tpm2-keyfile can only be specified once.\n"));
+ return EINVAL;
+ }
+
+ args->tpm2_keyfile = xstrdup(arg);
-+ args->args |= GRUB_PROTECT_ARG_TPM2_KEYFILE;
++ args->args |= PROTECT_ARG_TPM2_KEYFILE;
+ break;
+
-+ case GRUB_PROTECT_OPT_TPM2_OUTFILE:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_OUTFILE)
++ case PROTECT_OPT_TPM2_OUTFILE:
++ if (args->args & PROTECT_ARG_TPM2_OUTFILE)
+ {
+ fprintf (stderr, N_("--tpm2-outfile can only be specified once.\n"));
+ return EINVAL;
+ }
+
+ args->tpm2_outfile = xstrdup(arg);
-+ args->args |= GRUB_PROTECT_ARG_TPM2_OUTFILE;
++ args->args |= PROTECT_ARG_TPM2_OUTFILE;
+ break;
+
-+ case GRUB_PROTECT_OPT_TPM2_EVICT:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_EVICT)
++ case PROTECT_OPT_TPM2_EVICT:
++ if (args->args & PROTECT_ARG_TPM2_EVICT)
+ {
+ fprintf (stderr, N_("--tpm2-evict can only be specified once.\n"));
+ return EINVAL;
+ }
+
+ args->tpm2_evict = 1;
-+ args->args |= GRUB_PROTECT_ARG_TPM2_EVICT;
++ args->args |= PROTECT_ARG_TPM2_EVICT;
+ break;
+
-+ case GRUB_PROTECT_OPT_TPM2_TPM2KEY:
-+ if (args->args & GRUB_PROTECT_ARG_TPM2_TPM2KEY)
++ case PROTECT_OPT_TPM2_TPM2KEY:
++ if (args->args & PROTECT_ARG_TPM2_TPM2KEY)
+ {
+ fprintf (stderr, N_("--tpm2-tpm2key can only be specified once.\n"));
+ return EINVAL;
+ }
+
+ args->tpm2_tpm2key = 1;
-+ args->args |= GRUB_PROTECT_ARG_TPM2_TPM2KEY;
++ args->args |= PROTECT_ARG_TPM2_TPM2KEY;
+ break;
+
+ default:
@@ util/grub-protect.c (new)
+}
+
+static grub_err_t
-+protect_args_verify (struct grub_protect_args *args)
++protect_args_verify (protect_args_t *args)
+{
-+ if (args->action == GRUB_PROTECT_ACTION_ERROR)
++ if (args->action == PROTECT_ACTION_ERROR)
+ {
+ fprintf (stderr, N_("--action is mandatory.\n"));
+ return GRUB_ERR_BAD_ARGUMENT;
@@ util/grub-protect.c (new)
+
+ /* At the moment, the only configurable key protector is the TPM2 one, so it
+ * is the only key protector supported by this tool. */
-+ if (args->protector != GRUB_PROTECT_TYPE_TPM2)
++ if (args->protector != PROTECT_TYPE_TPM2)
+ {
+ fprintf (stderr, N_("--protector is mandatory and only 'tpm2' is currently supported.\n"));
+ return GRUB_ERR_BAD_ARGUMENT;
@@ util/grub-protect.c (new)
+
+ switch (args->protector)
+ {
-+ case GRUB_PROTECT_TYPE_TPM2:
++ case PROTECT_TYPE_TPM2:
+ return protect_tpm2_args_verify (args);
+ default:
+ return GRUB_ERR_BAD_ARGUMENT;
@@ util/grub-protect.c (new)
+}
+
+static grub_err_t
-+protect_dispatch (struct grub_protect_args *args)
++protect_dispatch (protect_args_t *args)
+{
+ switch (args->protector)
+ {
-+ case GRUB_PROTECT_TYPE_TPM2:
++ case PROTECT_TYPE_TPM2:
+ return protect_tpm2_run (args);
+ default:
+ return GRUB_ERR_BAD_ARGUMENT;
@@ util/grub-protect.c (new)
+ grub_util_biosdisk_fini ();
+}
+
-+static struct argp grub_protect_argp =
++static struct argp protect_argp =
+{
-+ .options = grub_protect_options,
++ .options = protect_options,
+ .parser = protect_argp_parser,
+ .args_doc = NULL,
+ .doc =
@@ util/grub-protect.c (new)
+main (int argc, char *argv[])
+{
+ grub_err_t err;
-+ struct grub_protect_args args = { 0 };
++ protect_args_t args = {0};
+
-+ if (argp_parse (&grub_protect_argp, argc, argv, 0, 0, &args) != 0)
++ if (argp_parse (&protect_argp, argc, argv, 0, 0, &args) != 0)
+ {
+ fprintf (stderr, N_("Could not parse arguments.\n"));
+ return GRUB_ERR_BAD_ARGUMENT;25: b5e60d579 ! 26: b9194893a tpm2_key_protector: Support authorized policy
@@ Commit message
--stop-event "grub-file=grub.cfg" \
--after \
--input sealed.key \
- --output sealed.tpm \
+ --output /boot/efi/efi/grub/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 -a RSA --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm
+ tpm2_key_protector_init -a RSA --tpm2key=(hd0,gpt1)/efi/grub/sealed.tpm
cryptomount -u <PART_UUID> -P tpm2
For any change in the boot components, just run the 'sign' command again
@@ Commit message
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/commands/tpm2_key_protector/module.c ##
@@ grub-core/commands/tpm2_key_protector/module.c: tpm2_protector_policypcr (TPMI_SH_AUTH_SESSION_t session, struct grub_tpm2_buffe
26: 784a1954c ! 27: ce493c766 tpm2_key_protector: Implement NV index
@@ Commit message
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
+ Note: This only extends support on the unseal path. grub-protect
has not been updated. tpm2-tools can be used to insert a key into the NV index.
@@ Commit message
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/commands/tpm2_key_protector/module.c ##
-@@ grub-core/commands/tpm2_key_protector/module.c: tpm2_protector_srk_recover (const struct grub_tpm2_protector_context *ctx, +@@ grub-core/commands/tpm2_key_protector/module.c: tpm2_protector_srk_recover (const tpm2_protector_context_t *ctx,
}
static grub_err_t
--tpm2_protector_nv_recover (const struct grub_tpm2_protector_context *ctx __attribute__ ((unused)), +-tpm2_protector_nv_recover (const tpm2_protector_context_t *ctx __attribute__ ((unused)),
- grub_uint8_t **key __attribute__ ((unused)),
- grub_size_t *key_size __attribute__ ((unused)))
-+tpm2_protector_nv_recover (const struct grub_tpm2_protector_context *ctx, ++tpm2_protector_nv_recover (const tpm2_protector_context_t *ctx,
+ grub_uint8_t **key, grub_size_t *key_size)
{
- return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "NV Index mode is not implemented yet");
27: bf64c4c21 ! 28: 56b8f52f3 cryptodisk: Fallback to passphrase
@@ Commit message
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/disk/cryptodisk.c ##
@@ grub-core/disk/cryptodisk.c: grub_cryptodisk_scan_device_real (const char *name,
28: e1d67e568 ! 29: 56df542cc cryptodisk: wipe out the cached keys from protectors
@@ Commit message
cryptodisk: wipe out the cached keys from protectors
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
+ trick GRUB 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.
29: 89452b2c0 ! 30: 69d89e3ac diskfilter: look up cryptodisk devices first
@@ Commit message
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
+ tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub/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 the malicious root and further dump memory to
+ trick GRUB to boot into the malicious root and further dump memory to
steal the unsealed key.
Do defend against such an attack, we can specify the hint provided by
@@ Commit message
--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
+ partition, so the attacker may have the chance to fool GRUB to boot
into the malicious disk.
To minimize the attack surface, this commit tweaks the disk device search
@@ Commit message
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/disk/diskfilter.c ##
@@ grub-core/disk/diskfilter.c: scan_devices (const char *arname)
30: 37dfe60a8 ! 31: 686de5074 tpm2_key_protector: Add grub-emu support
@@ Commit message
As a preparation to test tpm2_key_protector 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
+ grub-emu so that grub-emu can access an emulated TPM device from
the host.
- Since grub-emu can directly access the device node on host, it's easy to
+ Since grub-emu can directly access the device on host, it's easy to
implement the essential TCG2 command submission function with the read/write functions and enable tpm2_key_protector module for grub-emu, so that we can further test TPM2 key unsealing with grub-emu.
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/Makefile.core.def ##
@@ grub-core/Makefile.core.def: module = {
@@ grub-core/kern/emu/main.c: 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}, ++ {"tpm-device", 't', N_("DEV"), 0, N_("set TPM device."), 0},
{ 0, 0, 0, 0, 0, 0 }
};
@@ grub-core/lib/tss2/tcg2_emu.c (new)
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
-+#include <grub/efi/api.h>
-+#include <grub/efi/efi.h>
-+#include <grub/efi/tpm.h>
+#include <grub/mm.h>
+#include <grub/emu/misc.h>
+
@@ grub-core/lib/tss2/tcg2_emu.c (new)
+ grub_size_t output_size, grub_uint8_t *output)
+{
+ static const grub_size_t header_size = sizeof (grub_uint16_t) +
-+ (2 * sizeof(grub_uint32_t));
++ (2 * sizeof (grub_uint32_t));
+
+ if (grub_util_tpm_write (input, input_size) != input_size)
+ return GRUB_ERR_BAD_DEVICE;
31: 66969ccc9 = 32: cf6366169 tests: Add tpm2_key_protector_test 32: 48c991a37 < -: --------- cryptodisk: Document the '-P' option 33: b9d90b33e ! 33: 45fdafd47 docs: Document TPM2 key protector
@@ docs/grub.texi: either @var{expression1} or @var{expression2} is true
+
+@deffn Command tpm2_key_protector_init [@option{-m} mode] | [@option{-p} pcrlist] | [@option{-b} pcrbank] | [ [@option{-T} tpm2key_file] | [@option{-k} keyfile] ] | [@option{-s} handle] | [@option{-a} srk_type] | [@option{-n} nv_index] +Initialize the TPM2 key protector to unseal the key for the @command{cryptomount}
-+(@pxref{cryptomount}) command. There are two supported mode,
++(@pxref{cryptomount}) command. There are two supported modes,
+SRK(@kbd{srk}) and NV index(@kbd{nv}), to be specified by the option
-+@option{-m}. The default mode is srk. The main difference between SRK mode
++@option{-m}. The default mode is SRK. The main difference between SRK mode
+and NV index mode is the storage of the sealed key. For SRK mode, the sealed
+key is stored in a file while NV index mode stores the sealed key in the
+non-volatile memory inside TPM with a given NV index.
+
+The @option{-p} and @option{-b} options are used to supply the PCR list and
-+bank that the key sealed with. The PCR list is a comma-separated list, e.g.,
++bank that the key is sealed with. The PCR list is a comma-separated list, e.g.,
+'0,2,4,7,9', to represent the involved PCRs, and the default is '7'. The PCR
-+bank is marked by the hash algorithms. The current supported PCR banks are
-+SHA1, SHA256, SHA384, and SHA512, and the default is SHA256.
++bank is chosen by selecting a hash algorithm. The current supported PCR banks
++are SHA1, SHA256, SHA384, and SHA512, and the default is SHA256.
+
+There are some options only available for the specific mode. The SRK-specific
+options are @option{-T}, @option{-k}, @option{-a}, and @option{-s}. On the
@@ docs/grub.texi: either @var{expression1} or @var{expression2} is true
+
+The key file for SRK mode can be supplied with either @option{-T} or
+@option{-k}. The @option{-T} option is for the path to the key file in
-+the TPM 2.0 Key File format. Since the parameters for the TPM commands are
-+written in the policy sequences, there is no need to set the PCR
-+list(@option{-p}) and bank(@option{-b}) when using @option{-T} option. The
-+@option{-k} option is for the key file in the raw format, and the @option{-p}
-+and @option{-b} options are necessary for the non-default PCR list or bank.
++TPM 2.0 Key File. Since the parameters for the TPM commands are written in
++the file, there is no need to set the PCR list(@option{-p}) and
++bank(@option{-b}) when using the @option{-T} option. The @option{-k} option
++is for the key file in the raw format, and the @option{-p} and @option{-b}
++options are necessary for the non-default PCR list or bank.
+
+Besides the key file, there are two options, @option{-a} and @option{-s}, to
+tweak the TPM Storage Root Key (SRK). The SRK can be either created at
@@ docs/grub.texi: be restricted and some operations/commands cannot be executed.
+partition, @command{cryptomount} falls back to the passphrase prompt.)
+
+There are two supported modes to store the sealed key, SRK and NV index,
-+and the details will be addressed in the latter sections.
++and the details will be addressed in later sections.
+
+TPM2 key protector is currently only supported on EFI and EMU platforms.
+
@@ docs/grub.texi: be restricted and some operations/commands cannot be executed.
+to calculate the correct PCR values.
+
+Please note that PCRs are sensitive to any change, so an update of a component
-+could invalidate the sealed key, and it is so-called PCR brittleness. For the
++could invalidate the sealed key, due to the so-called PCR brittleness. For the
+bootloader update, PCR 4 may be affected. This can be mitigated by extracting
+the events from the TPM event log and predict the value with the updated
+bootloader binary. On the other hand, it is difficult to predict PCR 0~7 after
@@ docs/grub.texi: be restricted and some operations/commands cannot be executed.
+@subsection SRK mode
+
+To unlock the partition with SRK mode, assume that the sealed key is in
-+@file{(hd0,gpt1)/boot/grub2/sealed.tpm}, the following GRUB commands
++@file{(hd0,gpt1)/efi/grub/sealed.tpm}, the following GRUB commands
+unseal the disk key with SRK mode and supply it to @command{cryptomount}.
+
+@example
-+@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/boot/grub2/sealed.tpm}
++@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm}
+@kbd{cryptomount -u <UUID> -P tpm2}
+@end example
+
@@ docs/grub.texi: be restricted and some operations/commands cannot be executed.
+and @command{pcr-oracle} (@url{https://github.com/okirch/pcr-oracle}).
+
+The following sample command uses @command{grub-protect} to seal the random
-+key, @file{luks.key}, with PCR 0, 2, 4 and 7 in TPM 2.0 Key File format.
++key, @file{luks.key}, with PCR 0, 2, 4 and 7 in TPM 2.0 Key File.
+
+@example
+@group
@@ docs/grub.texi: be restricted and some operations/commands cannot be executed.
+ --tpm2-pcrs=0,2,4,7 \
+ --tpm2key \
+ --tpm2-keyfile=luks.key \
-+ --tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm}
++ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm}
+@end group
+@end example
+
-+Since @command{grub-protect} only seals the key with the current PCR values,
-+when a boot component updates such as shim or GRUB, it requires to reboot the
-+system to update the measurement results and seal the key again. That means
-+the random disk key has to be stored in cleartext for the next key sealing.
-+Besides, the measurement result of some PCRs may differ between boot time and
-+OS runtime. For example, PCR 9 measures the files loaded by GRUB including the
-+linux kernel and initrd. To unlock the disk containing the kernel and initrd,
-+the key has to be sealed with PCR 9 value before loading the kernel and
-+initrd. However, PCR 9 changes after GRUB loading the kernel and initrd, so
-+PCR 9 at OS runtime cannot be used directly for key sealing.
-+
-+To solve the problems, @command{pcr-oracle} takes a different approach. It
++@command{grub-protect} only seals the key with the current PCR values.
++Therefore, when a boot component, such as shim or GRUB, is updated, it is
++neccessary to reboot the system to update the measurement results and seal
++the key again. That means the random disk key has to be stored in cleartext
++for the next key sealing. Besides this, the measurement result of some PCRs
++may differ between boot time and OS runtime. For example, PCR 9 measures the
++files loaded by GRUB including the Linux kernel and initrd. To unlock the disk
++containing the kernel and initrd, the key has to be sealed with PCR 9 value
++before loading the kernel and initrd. However, PCR 9 changes after GRUB
++loading the kernel and initrd, so PCR 9 at OS runtime cannot be used directly
++for key sealing.
++
++To solve these problems, @command{pcr-oracle} takes a different approach. It
+reads the TPM eventlog and predicts the PCR values. Besides,
+@command{pcr-oracle} also supports ``authorized policy'' which allows the
-+PCR policy to be updated with a valid signature, so that the user only
-+needs to seal the random disk key once and then updates the signature
-+of PCR policy for the later changes.
++PCR policy to be updated with a valid signature, so that the user only seals
++the random disk key once. If at some later time the PCR values change due to
++an update of the system firmware, bootloader, or config file, the user just
++needs to update the signature of PCR policy.
+
-+To seal the key with authorized policy, the first thing is to generate
++To seal the key with the authorized policy, the first thing is to generate
+the RSA policy key, @file{policy-key.pem}, and the authorized policy file,
+@file{authorized.policy}. In this example, PCR 0, 2, 4, 7 and 9 are chosen
+for key sealing.
@@ docs/grub.texi: be restricted and some operations/commands cannot be executed.
+@end group
+@end example
+
-+The random disk key file, @file{luks.key}, now can be removed for good.
++Since we now have the sealed key, we can remove the random disk key file
++@file{lukes.key}.
++
+The last step is to sign the predicted PCR policy and save the final key
+file, @file{sealed.tpm}.
+
@@ docs/grub.texi: be restricted and some operations/commands cannot be executed.
+ --stop-event "grub-file=grub.cfg" \
+ --after \
+ --input sealed.key \
-+ --output /boot/efi/boot/grub2/sealed.tpm \
++ --output /boot/efi/efi/grub/sealed.tpm \
+ sign 0,2,4,7,9}
+@end group
+@end example
@@ docs/grub.texi: be restricted and some operations/commands cannot be executed.
+
+@subsection NV index mode
+
-+Instead of storing the sealed key in the file, NV index mode uses the TPM
-+non-volatile memory for the sealed key.
++Instead of storing the sealed key in a file, NV index mode uses the TPM
++non-volatile memory to store the sealed key.
+
+The following sample commands use tpm2-tools (@url{https://github.com/tpm2-software/tpm2-tools})
+commands to seal @file{luks.key} into the specific NV index: @kbd{0x81000000}.
@@ docs/grub.texi: Print verbose messages.
+Set the SRK handle, e.g. @samp{0x81000000}, if the SRK is to be made persistent.
+
+@item --tpm2key
-+Use TPM 2.0 Key File format instead of the raw format.
++Use TPM 2.0 Key File instead of the raw format.
+
+@end table
+
@@ docs/grub.texi: Print verbose messages.
+(@pxref{TPM2 key protector, TPM PCR usage}) to choose a proper set of PCRs.
+
+Assume that there is a key file, @file{luks.key}, to be sealed with PCR 0, 2,
-+4, and 7, here is the @command{grub-protect} command to create the sealed
++4, and 7, and here is the @command{grub-protect} command to create the sealed
+key file:
+
+@example
@@ docs/grub.texi: Print verbose messages.
+ --tpm2-pcrs=0,2,4,7 \
+ --tpm2key \
+ --tpm2-keyfile=luks.key \
-+ --tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm}
++ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm}
+@end group
+@end example
+
+Then, GRUB can unlock the target partition with the following commands:
+
+@example
-+@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/boot/grub2/sealed.tpm}
++@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm}
+@kbd{cryptomount -u <UUID> -P tpm2}
+@end example
+
@@ docs/grub.texi: Print verbose messages.
+ --tpm2-srk=0x81000000 \
+ --tpm2key \
+ --tpm2-keyfile=luks.key \
-+ --tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm}
++ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm}
+@end group
+@end example
+
@@ docs/grub.texi: Print verbose messages.
+informs the TPM2 key protector to fetch the SRK from @samp{0x81000000}.
+
+@example
-+@kbd{tpm2_key_protector_init -s 0x81000000 --keyfile=(hd0,gpt1)/boot/grub2/sealed.tpm}
++@kbd{tpm2_key_protector_init -s 0x81000000 --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm}
+@kbd{cryptomount -u <UUID> -P tpm2}
+@end example
+
@@ docs/grub.texi: Print verbose messages.
+
+In case the user doesn't want to use the TPM2 key protector anymore, besides
+removing the sealed key file, here is the command to remove the persistent
-+SRK handle (@samp{0x81000000}.) with @kbd{--tpm2-srk} and @kbd{--tpm2-evict}.
++
++The sealed key can be remove once the user does not want to use the TPM2 key
++protector anymore. Here is the command to remove the persistent SRK handle
++(@samp{0x81000000}) with @kbd{--tpm2-srk} and @kbd{--tpm2-evict}.
+
+@example
+@group
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 1/33 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
@@ -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
@@ -41,5 +41,6 @@#define LONG_MAX GRUB_LONG_MAX#define CHAR_BIT 8 +#define WORD_BIT 32#endif
grub-core/lib/posix_wrap/stdlib.h +8 -0
@@ -64,4 +64,12 @@ abort (void) grub_abort (); } +#define strtol grub_strtol + +/* for libgcrypt */ +#define HAVE_STRTOUL +#define strtoul grub_strtoul + +#define strtoull grub_strtoull +#endif
grub-core/lib/posix_wrap/string.h +21 -0
@@ -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
@@ -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.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 2/33 libtasn1: import libtasn1-4.19.0
In reply to Gary Lin via Grub-develFrom: Daniel Axtens <[email protected]>
Import a very trimmed-down set of libtasn1 files:
curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
tar xf libtasn1-4.19.0.tar.gz
rm -rf grub-core/lib/libtasn1/
mkdir -p grub-core/lib/libtasn1/lib/
mkdir -p grub-core/lib/libtasn1/tests/
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/libtasn1/lib/
cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1/
cp libtasn1-4.19.0/tests/{CVE-2018-1000654-1_asn1_tab.h,CVE-2018-1000654-2_asn1_tab.h,CVE-2018-1000654.c,object-id-decoding.c,object-id-encoding.c,octet-string.c,reproducers.c,Test_overflow.c,Test_simple.c,Test_strings.c} grub-core/lib/libtasn1/tests rm -rf libtasn1-4.19.0*
--- 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 + grub-core/lib/libtasn1/libtasn1.h | 643 +++++ .../tests/CVE-2018-1000654-1_asn1_tab.h | 32 + .../tests/CVE-2018-1000654-2_asn1_tab.h | 36 + .../lib/libtasn1/tests/CVE-2018-1000654.c | 72 + grub-core/lib/libtasn1/tests/Test_overflow.c | 168 ++ grub-core/lib/libtasn1/tests/Test_simple.c | 226 ++ grub-core/lib/libtasn1/tests/Test_strings.c | 156 + .../lib/libtasn1/tests/object-id-decoding.c | 121 + .../lib/libtasn1/tests/object-id-encoding.c | 133 + grub-core/lib/libtasn1/tests/octet-string.c | 230 ++ grub-core/lib/libtasn1/tests/reproducers.c | 90 + 25 files changed, 10164 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 grub-core/lib/libtasn1/libtasn1.h create mode 100644 grub-core/lib/libtasn1/tests/CVE-2018-1000654-1_asn1_tab.h create mode 100644 grub-core/lib/libtasn1/tests/CVE-2018-1000654-2_asn1_tab.h create mode 100644 grub-core/lib/libtasn1/tests/CVE-2018-1000654.c create mode 100644 grub-core/lib/libtasn1/tests/Test_overflow.c create mode 100644 grub-core/lib/libtasn1/tests/Test_simple.c create mode 100644 grub-core/lib/libtasn1/tests/Test_strings.c create mode 100644 grub-core/lib/libtasn1/tests/object-id-decoding.c create mode 100644 grub-core/lib/libtasn1/tests/object-id-encoding.c create mode 100644 grub-core/lib/libtasn1/tests/octet-string.c create mode 100644 grub-core/lib/libtasn1/tests/reproducers.c
grub-core/lib/libtasn1/COPYING new file +16 -0
@@ -0,0 +1,16 @@ +LICENSING +========= + +The libtasn1 library is released under the GNU Lesser General Public +License (LGPL) version 2.1 or later; see [COPYING.LESSER](doc/COPYING.LESSER) +for the license terms. + +The GNU LGPL applies to the main libtasn1 library, while the +included applications library are under the GNU GPL version 3. +The libtasn1 library is located in the lib directory, while the applications +in src/. + +The documentation in doc/ is under the GNU FDL license 1.3. + +For any copyright year range specified as YYYY-ZZZZ in this package +note that the range specifies every single year in that closed interval.
grub-core/lib/libtasn1/README.md new file +98 -0
98 lines not on this page · raw message
grub-core/lib/libtasn1/lib/coding.c new file +1425 -0
1,425 lines not on this page · raw message
grub-core/lib/libtasn1/lib/decoding.c new file +2501 -0
1,461 lines not on this page · raw message
grub-core/lib/libtasn1/lib/element.c new file +1109 -0
1,109 lines not on this page · raw message
grub-core/lib/libtasn1/lib/element.h new file +42 -0
42 lines not on this page · raw message
grub-core/lib/libtasn1/lib/errors.c new file +100 -0
100 lines not on this page · raw message
grub-core/lib/libtasn1/lib/gstr.c new file +74 -0
74 lines not on this page · raw message
grub-core/lib/libtasn1/lib/gstr.h new file +50 -0
50 lines not on this page · raw message
grub-core/lib/libtasn1/lib/int.h new file +221 -0
221 lines not on this page · raw message
grub-core/lib/libtasn1/lib/parser_aux.c new file +1178 -0
1,178 lines not on this page · raw message
grub-core/lib/libtasn1/lib/parser_aux.h new file +172 -0
172 lines not on this page · raw message
grub-core/lib/libtasn1/lib/structure.c new file +1225 -0
1,225 lines not on this page · raw message
grub-core/lib/libtasn1/lib/structure.h new file +46 -0
46 lines not on this page · raw message
grub-core/lib/libtasn1/libtasn1.h new file +643 -0
643 lines not on this page · raw message
grub-core/lib/libtasn1/tests/CVE-2018-1000654-1_asn1_tab.h new file +32 -0
Lines of this file are not shown. View the raw message.
grub-core/lib/libtasn1/tests/CVE-2018-1000654-2_asn1_tab.h new file +36 -0
Lines of this file are not shown. View the raw message.
grub-core/lib/libtasn1/tests/CVE-2018-1000654.c new file +72 -0
72 lines not on this page · raw message
grub-core/lib/libtasn1/tests/Test_overflow.c new file +168 -0
168 lines not on this page · raw message
grub-core/lib/libtasn1/tests/Test_simple.c new file +226 -0
226 lines not on this page · raw message
grub-core/lib/libtasn1/tests/Test_strings.c new file +156 -0
156 lines not on this page · raw message
grub-core/lib/libtasn1/tests/object-id-decoding.c new file +121 -0
121 lines not on this page · raw message
grub-core/lib/libtasn1/tests/object-id-encoding.c new file +133 -0
133 lines not on this page · raw message
grub-core/lib/libtasn1/tests/octet-string.c new file +230 -0
230 lines not on this page · raw message
grub-core/lib/libtasn1/tests/reproducers.c new file +90 -0
90 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 7 lines
2.43.0 _______________________________________________ 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.
PATCH v20 3/33 libtasn1: disable code not needed in grub
In reply to Gary Lin via Grub-develWe 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.
--- ...asn1-disable-code-not-needed-in-grub.patch | 320 ++++++++++++++++++ 1 file changed, 320 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch
grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch new file +320 -0
@@ -0,0 +1,320 @@ +From f534b3c0902adf207b7e51b8364284fe6a06731b Mon Sep 17 00:00:00 2001 +From: Daniel Axtens <[email protected]> +Date: Fri, 1 May 2020 17:12:23 +1000 +Subject: [PATCH 01/13] libtasn1: disable code not needed in grub + +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. + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/lib/libtasn1-grub/lib/coding.c | 12 ++++++++++-- + grub-core/lib/libtasn1-grub/lib/decoding.c | 2 ++ + grub-core/lib/libtasn1-grub/lib/element.c | 6 +++--- + grub-core/lib/libtasn1-grub/lib/errors.c | 3 +++ + grub-core/lib/libtasn1-grub/lib/structure.c | 10 ++++++---- + grub-core/lib/libtasn1-grub/libtasn1.h | 15 +++++++++++++++ + 6 files changed, 39 insertions(+), 9 deletions(-) + +diff --git a/grub-core/lib/libtasn1-grub/lib/coding.c b/grub-core/lib/libtasn1-grub/lib/coding.c +index ea5bc370e..5d03bca9d 100644 +--- a/grub-core/lib/libtasn1-grub/lib/coding.c ++++ b/grub-core/lib/libtasn1-grub/lib/coding.c +@@ -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 +diff --git a/grub-core/lib/libtasn1-grub/lib/decoding.c b/grub-core/lib/libtasn1-grub/lib/decoding.c +index b9245c486..bf9cb13ac 100644 +--- a/grub-core/lib/libtasn1-grub/lib/decoding.c ++++ b/grub-core/lib/libtasn1-grub/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 /* 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: +diff --git a/grub-core/lib/libtasn1-grub/lib/element.c b/grub-core/lib/libtasn1-grub/lib/element.c +index d4c558e10..bc4c3c8d7 100644 +--- a/grub-core/lib/libtasn1-grub/lib/element.c ++++ b/grub-core/lib/libtasn1-grub/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; + } + +- ++#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; \ +diff --git a/grub-core/lib/libtasn1-grub/lib/errors.c b/grub-core/lib/libtasn1-grub/lib/errors.c +index aef5dfe6f..2b2322152 100644 +--- a/grub-core/lib/libtasn1-grub/lib/errors.c ++++ b/grub-core/lib/libtasn1-grub/lib/errors.c +@@ -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: +diff --git a/grub-core/lib/libtasn1-grub/lib/structure.c b/grub-core/lib/libtasn1-grub/lib/structure.c +index 512dd601f..f5a947d57 100644 +--- a/grub-core/lib/libtasn1-grub/lib/structure.c ++++ b/grub-core/lib/libtasn1-grub/lib/structure.c +@@ -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: +diff --git a/grub-core/lib/libtasn1-grub/libtasn1.h b/grub-core/lib/libtasn1-grub/libtasn1.h +index 51cc7879f..058ab27b0 100644 +--- a/grub-core/lib/libtasn1-grub/libtasn1.h ++++ b/grub-core/lib/libtasn1-grub/libtasn1.h +@@ -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 +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 4/33 libtasn1: replace strcat() with strcpy() in _asn1_str_cat()
In reply to Gary Lin via Grub-develstrcat() is not available in GRUB. This commit replaces strcat() with strcpy() in _asn1_str_cat() as the preparation to replace other strcat() with the bounds-checking _asn1_str_cat().
--- ...-strcat-with-strcpy-in-_asn1_str_cat.patch | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch
grub-core/lib/libtasn1-patches/0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch new file +33 -0
@@ -0,0 +1,33 @@ +From 93453558fbe34634096770933b3dc40c9199dfb4 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Tue, 20 Aug 2024 16:14:51 +0800 +Subject: [PATCH 02/13] libtasn1: replace strcat() with strcpy() in + _asn1_str_cat() + +strcat() is not available in GRUB. This commit replaces strcat() with +strcpy() in _asn1_str_cat() as the preparation to replace other strcat() +with the bounds-checking _asn1_str_cat(). + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/lib/libtasn1-grub/lib/gstr.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/lib/libtasn1-grub/lib/gstr.c b/grub-core/lib/libtasn1-grub/lib/gstr.c +index eef419554..a9c16f5d3 100644 +--- a/grub-core/lib/libtasn1-grub/lib/gstr.c ++++ b/grub-core/lib/libtasn1-grub/lib/gstr.c +@@ -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 + { +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 5/33 libtasn1: replace strcat() with _asn1_str_cat()
In reply to Gary Lin via Grub-develstrcat() is not available in GRUB. This commit replaces strcat() and _asn1_strcat() with the bounds-checking _asn1_str_cat().
--- ...n1-replace-strcat-with-_asn1_str_cat.patch | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0003-libtasn1-replace-strcat-with-_asn1_str_cat.patch
grub-core/lib/libtasn1-patches/0003-libtasn1-replace-strcat-with-_asn1_str_cat.patch new file +71 -0
@@ -0,0 +1,71 @@ +From b5efa707619fde1921a3cbd83f4ee95a7d995fa0 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Tue, 20 Aug 2024 16:26:45 +0800 +Subject: [PATCH 03/13] libtasn1: replace strcat() with _asn1_str_cat() + +strcat() is not available in GRUB. This commit replaces strcat() and +_asn1_strcat() with the bounds-checking _asn1_str_cat(). + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/lib/libtasn1-grub/lib/decoding.c | 8 ++++---- + grub-core/lib/libtasn1-grub/lib/element.c | 2 +- + grub-core/lib/libtasn1-grub/lib/int.h | 1 - + 3 files changed, 5 insertions(+), 6 deletions(-) + +diff --git a/grub-core/lib/libtasn1-grub/lib/decoding.c b/grub-core/lib/libtasn1-grub/lib/decoding.c +index bf9cb13ac..51859fe36 100644 +--- a/grub-core/lib/libtasn1-grub/lib/decoding.c ++++ b/grub-core/lib/libtasn1-grub/lib/decoding.c +@@ -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) +diff --git a/grub-core/lib/libtasn1-grub/lib/element.c b/grub-core/lib/libtasn1-grub/lib/element.c +index bc4c3c8d7..8694fecb9 100644 +--- a/grub-core/lib/libtasn1-grub/lib/element.c ++++ b/grub-core/lib/libtasn1-grub/lib/element.c +@@ -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); \ + } + + /** +diff --git a/grub-core/lib/libtasn1-grub/lib/int.h b/grub-core/lib/libtasn1-grub/lib/int.h +index d94d51c8c..cadd80df6 100644 +--- a/grub-core/lib/libtasn1-grub/lib/int.h ++++ b/grub-core/lib/libtasn1-grub/lib/int.h +@@ -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) +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 6/33 libtasn1: adjust the header paths in libtasn1.h
In reply to Gary Lin via Grub-develSince libtasn1.h is the header to be included by users, including the standard POSIX headers in libtasn1.h would force the user to add the CFLAGS/CPPFLAGS for the POSIX headers.
This commit adjusts the header paths to use the grub headers instead of the standard POSIX headers, so that users only need to include libtasn1.h to use libtasn1 functions.
--- ...djust-the-header-paths-in-libtasn1.h.patch | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0004-libtasn1-adjust-the-header-paths-in-libtasn1.h.patch
grub-core/lib/libtasn1-patches/0004-libtasn1-adjust-the-header-paths-in-libtasn1.h.patch new file +39 -0
@@ -0,0 +1,39 @@ +From 24036474fab426917507fcadc85a83fa0b1cef3b Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Tue, 25 Jun 2024 16:30:40 +0800 +Subject: [PATCH 04/13] libtasn1: adjust the header paths in libtasn1.h + +Since libtasn1.h is the header to be included by users, including the +standard POSIX headers in libtasn1.h would force the user to add the +CFLAGS/CPPFLAGS for the POSIX headers. + +This commit adjusts the header paths to use the grub headers instead of +the standard POSIX headers, so that users only need to include +libtasn1.h to use libtasn1 functions. + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/lib/libtasn1-grub/libtasn1.h | 5 ++--- + 1 file changed, 2 insertions(+), 3 deletions(-) + +diff --git a/grub-core/lib/libtasn1-grub/libtasn1.h b/grub-core/lib/libtasn1-grub/libtasn1.h +index 058ab27b0..7d64b6ab7 100644 +--- a/grub-core/lib/libtasn1-grub/libtasn1.h ++++ b/grub-core/lib/libtasn1-grub/libtasn1.h +@@ -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" +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 7/33 libtasn1: Use grub_divmod64() for division
In reply to Gary Lin via Grub-develReplace a 64-bit division with a call to grub_divmod64(), preventing creation of __udivdi3() calls on 32-bit platforms.
--- ...tasn1-Use-grub_divmod64-for-division.patch | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0005-libtasn1-Use-grub_divmod64-for-division.patch
grub-core/lib/libtasn1-patches/0005-libtasn1-Use-grub_divmod64-for-division.patch new file +31 -0
@@ -0,0 +1,31 @@ +From 5ee39f0bb8b0b713f766c0fc83439d83ac2c0bf2 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Tue, 25 Jun 2024 16:32:50 +0800 +Subject: [PATCH 05/13] libtasn1: Use grub_divmod64() for division + +Replace a 64-bit division with a call to grub_divmod64(), preventing +creation of __udivdi3() calls on 32-bit platforms. + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/lib/libtasn1-grub/lib/parser_aux.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/lib/libtasn1-grub/lib/parser_aux.c b/grub-core/lib/libtasn1-grub/lib/parser_aux.c +index c05bd2339..e4e4c0556 100644 +--- a/grub-core/lib/libtasn1-grub/lib/parser_aux.c ++++ b/grub-core/lib/libtasn1-grub/lib/parser_aux.c +@@ -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++; +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 8/33 libtasn1: fix the potential buffer overrun
In reply to Gary Lin via Grub-develIn _asn1_tag_der(), the first while loop for the long form may end up with a 'k' value with 'ASN1_MAX_TAG_SIZE' and cause the buffer overrun in the second while loop. This commit tweaks the conditional check to avoid producing a too large 'k'.
This is a quick fix and may differ from the official upstream fix.
libtasn1 issue: https://gitlab.com/gnutls/libtasn1/-/issues/49
--- ...sn1-fix-the-potential-buffer-overrun.patch | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0006-libtasn1-fix-the-potential-buffer-overrun.patch
grub-core/lib/libtasn1-patches/0006-libtasn1-fix-the-potential-buffer-overrun.patch new file +36 -0
@@ -0,0 +1,36 @@ +From f629f58b01b3e88052be5e50f51a667181203e05 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Mon, 8 Apr 2024 14:57:21 +0800 +Subject: [PATCH 06/13] libtasn1: fix the potential buffer overrun + +In _asn1_tag_der(), the first while loop for the long form may end up +with a 'k' value with 'ASN1_MAX_TAG_SIZE' and cause the buffer overrun +in the second while loop. This commit tweaks the conditional check to +avoid producing a too large 'k'. + +This is a quick fix and may differ from the official upstream fix. + +libtasn1 issue: https://gitlab.com/gnutls/libtasn1/-/issues/49 + +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/lib/libtasn1-grub/lib/coding.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/lib/libtasn1-grub/lib/coding.c b/grub-core/lib/libtasn1-grub/lib/coding.c +index 5d03bca9d..0458829a5 100644 +--- a/grub-core/lib/libtasn1-grub/lib/coding.c ++++ b/grub-core/lib/libtasn1-grub/lib/coding.c +@@ -143,7 +143,7 @@ _asn1_tag_der (unsigned char class, unsigned int tag_value, + temp[k++] = tag_value & 0x7F; + tag_value >>= 7; + +- if (k > ASN1_MAX_TAG_SIZE - 1) ++ if (k >= ASN1_MAX_TAG_SIZE - 1) + break; /* will not encode larger tags */ + } + *ans_len = k + 1; +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 9/33 asn1_test: include asn1_test.h only
In reply to Gary Lin via Grub-develThis commit removes all the headers and only uses asn1_test.h. To avoid including int.h from grub-core/lib/libtasn1-grub/lib/, CONST_DOWN is defined in reproducers.c.
--- ...7-asn1_test-include-asn1_test.h-only.patch | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0007-asn1_test-include-asn1_test.h-only.patch
grub-core/lib/libtasn1-patches/0007-asn1_test-include-asn1_test.h-only.patch new file +164 -0
@@ -0,0 +1,164 @@ +From 9cc63a71d6772947127d4d77e135d5fda68ccb8d Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 16 Aug 2024 14:10:21 +0800 +Subject: [PATCH 07/13] asn1_test: include asn1_test.h only + +This commit removes all the headers and only uses asn1_test.h. +To avoid including int.h from grub-core/lib/libtasn1-grub/lib/, +CONST_DOWN is defined in reproducers.c. + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/tests/asn1/tests/CVE-2018-1000654.c | 4 +--- + grub-core/tests/asn1/tests/Test_overflow.c | 7 +------ + grub-core/tests/asn1/tests/Test_simple.c | 6 +----- + grub-core/tests/asn1/tests/Test_strings.c | 6 +----- + grub-core/tests/asn1/tests/object-id-decoding.c | 6 +----- + grub-core/tests/asn1/tests/object-id-encoding.c | 6 +----- + grub-core/tests/asn1/tests/octet-string.c | 6 +----- + grub-core/tests/asn1/tests/reproducers.c | 8 ++------ + 8 files changed, 9 insertions(+), 40 deletions(-) + +diff --git a/grub-core/tests/asn1/tests/CVE-2018-1000654.c b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +index 0c22b7012..98c2a8b8c 100644 +--- a/grub-core/tests/asn1/tests/CVE-2018-1000654.c ++++ b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +@@ -22,9 +22,7 @@ + /* Description: reproducer for CVE-2018-1000654 */ + /****************************************************************/ + +-#include <stdio.h> +-#include <string.h> +-#include <stdlib.h> ++#include "asn1_test.h" + #include "CVE-2018-1000654-1_asn1_tab.h" + #include "CVE-2018-1000654-2_asn1_tab.h" + +diff --git a/grub-core/tests/asn1/tests/Test_overflow.c b/grub-core/tests/asn1/tests/Test_overflow.c +index c61dea4bb..73e9d8c68 100644 +--- a/grub-core/tests/asn1/tests/Test_overflow.c ++++ b/grub-core/tests/asn1/tests/Test_overflow.c +@@ -20,12 +20,7 @@ + + /* Written by Simon Josefsson */ + +-#include <stdio.h> +-#include <string.h> +-#include <stdlib.h> +-#include <limits.h> +- +-#include "libtasn1.h" ++#include "asn1_test.h" + + int + main (int argc, char **argv) +diff --git a/grub-core/tests/asn1/tests/Test_simple.c b/grub-core/tests/asn1/tests/Test_simple.c +index 6cd07e069..3aa8ce21b 100644 +--- a/grub-core/tests/asn1/tests/Test_simple.c ++++ b/grub-core/tests/asn1/tests/Test_simple.c +@@ -20,11 +20,7 @@ + * + */ + +-#include <stdio.h> +-#include <string.h> +-#include <stdlib.h> +- +-#include "libtasn1.h" ++#include "asn1_test.h" + + struct tv + { +diff --git a/grub-core/tests/asn1/tests/Test_strings.c b/grub-core/tests/asn1/tests/Test_strings.c +index 27f7215e1..c49229af9 100644 +--- a/grub-core/tests/asn1/tests/Test_strings.c ++++ b/grub-core/tests/asn1/tests/Test_strings.c +@@ -20,11 +20,7 @@ + * + */ + +-#include <stdio.h> +-#include <string.h> +-#include <stdlib.h> +- +-#include "libtasn1.h" ++#include "asn1_test.h" + + struct tv + { +diff --git a/grub-core/tests/asn1/tests/object-id-decoding.c b/grub-core/tests/asn1/tests/object-id-decoding.c +index 06a6c52a2..0a77db752 100644 +--- a/grub-core/tests/asn1/tests/object-id-decoding.c ++++ b/grub-core/tests/asn1/tests/object-id-decoding.c +@@ -18,11 +18,7 @@ + * + */ + +-#include <stdio.h> +-#include <string.h> +-#include <stdlib.h> +- +-#include "libtasn1.h" ++#include "asn1_test.h" + + struct tv + { +diff --git a/grub-core/tests/asn1/tests/object-id-encoding.c b/grub-core/tests/asn1/tests/object-id-encoding.c +index 1a3396986..e32835830 100644 +--- a/grub-core/tests/asn1/tests/object-id-encoding.c ++++ b/grub-core/tests/asn1/tests/object-id-encoding.c +@@ -18,11 +18,7 @@ + * + */ + +-#include <stdio.h> +-#include <string.h> +-#include <stdlib.h> +- +-#include "libtasn1.h" ++#include "asn1_test.h" + + struct tv + { +diff --git a/grub-core/tests/asn1/tests/octet-string.c b/grub-core/tests/asn1/tests/octet-string.c +index 69eb18a62..8e803af41 100644 +--- a/grub-core/tests/asn1/tests/octet-string.c ++++ b/grub-core/tests/asn1/tests/octet-string.c +@@ -20,11 +20,7 @@ + * + */ + +-#include <stdio.h> +-#include <string.h> +-#include <stdlib.h> +- +-#include "libtasn1.h" ++#include "asn1_test.h" + + struct tv + { +diff --git a/grub-core/tests/asn1/tests/reproducers.c b/grub-core/tests/asn1/tests/reproducers.c +index a09d8b021..ce24e0991 100644 +--- a/grub-core/tests/asn1/tests/reproducers.c ++++ b/grub-core/tests/asn1/tests/reproducers.c +@@ -22,13 +22,9 @@ + /* Description: run reproducers for several fixed issues */ + /****************************************************************/ + +-#include <stdio.h> +-#include <string.h> +-#include <stdlib.h> ++#include "asn1_test.h" + +-#include <libtasn1.h> +- +-#include <int.h> ++#define CONST_DOWN (1U<<29) + + /* produces endless loop (fixed by d4b624b2): + * The following translates into a single node with all pointers +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 10/33 asn1_test: rename the main functions to the test names
In reply to Gary Lin via Grub-develThis commit changes the main functions in the testcases to the test names so that the real 'main' test function can invokes them.
--- ...-the-main-functions-to-the-test-name.patch | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0008-asn1_test-rename-the-main-functions-to-the-test-name.patch
grub-core/lib/libtasn1-patches/0008-asn1_test-rename-the-main-functions-to-the-test-name.patch new file +129 -0
@@ -0,0 +1,129 @@ +From c0f39b9eae745a36c44fc0dfa106675405ea154f Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 16 Aug 2024 14:18:44 +0800 +Subject: [PATCH 08/13] asn1_test: rename the main functions to the test names + +This commit changes the main functions in the testcases to the test +names so that the real 'main' test function can invokes them. + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/tests/asn1/tests/CVE-2018-1000654.c | 2 +- + grub-core/tests/asn1/tests/Test_overflow.c | 2 +- + grub-core/tests/asn1/tests/Test_simple.c | 2 +- + grub-core/tests/asn1/tests/Test_strings.c | 2 +- + grub-core/tests/asn1/tests/object-id-decoding.c | 2 +- + grub-core/tests/asn1/tests/object-id-encoding.c | 2 +- + grub-core/tests/asn1/tests/octet-string.c | 2 +- + grub-core/tests/asn1/tests/reproducers.c | 2 +- + 8 files changed, 8 insertions(+), 8 deletions(-) + +diff --git a/grub-core/tests/asn1/tests/CVE-2018-1000654.c b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +index 98c2a8b8c..a935ab541 100644 +--- a/grub-core/tests/asn1/tests/CVE-2018-1000654.c ++++ b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +@@ -27,7 +27,7 @@ + #include "CVE-2018-1000654-2_asn1_tab.h" + + int +-main (int argc, char *argv[]) ++test_CVE_2018_1000654 (void) + { + int result, verbose = 0; + asn1_node definitions = NULL; +diff --git a/grub-core/tests/asn1/tests/Test_overflow.c b/grub-core/tests/asn1/tests/Test_overflow.c +index 73e9d8c68..bc28d0826 100644 +--- a/grub-core/tests/asn1/tests/Test_overflow.c ++++ b/grub-core/tests/asn1/tests/Test_overflow.c +@@ -23,7 +23,7 @@ + #include "asn1_test.h" + + int +-main (int argc, char **argv) ++test_overflow (void) + { + /* Test that values larger than long are rejected. This has worked + fine with all versions of libtasn1. */ +diff --git a/grub-core/tests/asn1/tests/Test_simple.c b/grub-core/tests/asn1/tests/Test_simple.c +index 3aa8ce21b..12993bfba 100644 +--- a/grub-core/tests/asn1/tests/Test_simple.c ++++ b/grub-core/tests/asn1/tests/Test_simple.c +@@ -73,7 +73,7 @@ static const struct tv tv[] = { + }; + + int +-main (int argc, char *argv[]) ++test_simple (void) + { + int result; + unsigned char der[100]; +diff --git a/grub-core/tests/asn1/tests/Test_strings.c b/grub-core/tests/asn1/tests/Test_strings.c +index c49229af9..2538f2558 100644 +--- a/grub-core/tests/asn1/tests/Test_strings.c ++++ b/grub-core/tests/asn1/tests/Test_strings.c +@@ -70,7 +70,7 @@ static const struct tv ber[] = { + }; + + int +-main (int argc, char *argv[]) ++test_strings (void) + { + int ret; + unsigned char tl[ASN1_MAX_TL_SIZE]; +diff --git a/grub-core/tests/asn1/tests/object-id-decoding.c b/grub-core/tests/asn1/tests/object-id-decoding.c +index 0a77db752..fdbb8ea21 100644 +--- a/grub-core/tests/asn1/tests/object-id-decoding.c ++++ b/grub-core/tests/asn1/tests/object-id-decoding.c +@@ -70,7 +70,7 @@ static const struct tv tv[] = { + }; + + int +-main (int argc, char *argv[]) ++test_object_id_decoding (void) + { + char str[128]; + int ret, ret_len; +diff --git a/grub-core/tests/asn1/tests/object-id-encoding.c b/grub-core/tests/asn1/tests/object-id-encoding.c +index e32835830..a497015e3 100644 +--- a/grub-core/tests/asn1/tests/object-id-encoding.c ++++ b/grub-core/tests/asn1/tests/object-id-encoding.c +@@ -80,7 +80,7 @@ static const struct tv tv[] = { + }; + + int +-main (int argc, char *argv[]) ++test_object_id_encoding (void) + { + unsigned char der[128]; + int ret, der_len, i, j; +diff --git a/grub-core/tests/asn1/tests/octet-string.c b/grub-core/tests/asn1/tests/octet-string.c +index 8e803af41..8c49c6e0c 100644 +--- a/grub-core/tests/asn1/tests/octet-string.c ++++ b/grub-core/tests/asn1/tests/octet-string.c +@@ -108,7 +108,7 @@ static const struct tv tv[] = { + }; + + int +-main (int argc, char *argv[]) ++test_octet_string (void) + { + unsigned char str[100]; + unsigned char der[100]; +diff --git a/grub-core/tests/asn1/tests/reproducers.c b/grub-core/tests/asn1/tests/reproducers.c +index ce24e0991..e843b74b9 100644 +--- a/grub-core/tests/asn1/tests/reproducers.c ++++ b/grub-core/tests/asn1/tests/reproducers.c +@@ -49,7 +49,7 @@ const asn1_static_node tab[] = { + }; + + int +-main (int argc, char *argv[]) ++test_reproducers (void) + { + int result, verbose = 0; + asn1_node definitions = NULL; +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 11/33 asn1_test: return either 0 or 1 to reflect the results
In reply to Gary Lin via Grub-develSome testcases use exit() to end the test. Since all the asn1 testcases are invoked as functions, this commit replaces exit() with return to reflect the test results, so that the main test function can check the results.
--- ...-either-0-or-1-to-reflect-the-result.patch | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0009-asn1_test-return-either-0-or-1-to-reflect-the-result.patch
grub-core/lib/libtasn1-patches/0009-asn1_test-return-either-0-or-1-to-reflect-the-result.patch new file +78 -0
@@ -0,0 +1,78 @@ +From 103ec39de5277e2683099017959415e107b885a5 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 16 Aug 2024 15:32:39 +0800 +Subject: [PATCH 09/13] asn1_test: return either 0 or 1 to reflect the results + +Some testcases use exit() to end the test. Since all the asn1 testcases +are invoked as functions, this commit replaces exit() with return to +reflect the test results, so that the main test function can check the +results. + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/tests/asn1/tests/CVE-2018-1000654.c | 6 +++--- + grub-core/tests/asn1/tests/reproducers.c | 6 +++--- + 2 files changed, 6 insertions(+), 6 deletions(-) + +diff --git a/grub-core/tests/asn1/tests/CVE-2018-1000654.c b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +index a935ab541..2085b3cd3 100644 +--- a/grub-core/tests/asn1/tests/CVE-2018-1000654.c ++++ b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +@@ -45,7 +45,7 @@ test_CVE_2018_1000654 (void) + { + asn1_perror (result); + printf ("ErrorDescription = %s\n\n", errorDescription); +- exit (1); ++ return 1; + } + + asn1_delete_structure (&definitions); +@@ -59,12 +59,12 @@ test_CVE_2018_1000654 (void) + { + asn1_perror (result); + printf ("ErrorDescription = %s\n\n", errorDescription); +- exit (1); ++ return 1; + } + + asn1_delete_structure (&definitions); + + if (verbose) + printf ("Success\n"); +- exit (0); ++ return 0; + } +diff --git a/grub-core/tests/asn1/tests/reproducers.c b/grub-core/tests/asn1/tests/reproducers.c +index e843b74b9..54a243d51 100644 +--- a/grub-core/tests/asn1/tests/reproducers.c ++++ b/grub-core/tests/asn1/tests/reproducers.c +@@ -63,7 +63,7 @@ test_reproducers (void) + { + asn1_perror (result); + printf ("ErrorDescription = %s\n\n", errorDescription); +- exit (EXIT_FAILURE); ++ return 1; + } + + asn1_delete_structure (&definitions); +@@ -74,7 +74,7 @@ test_reproducers (void) + { + asn1_perror (result); + printf ("ErrorDescription = %s\n\n", errorDescription); +- exit (EXIT_FAILURE); ++ return 1; + } + + asn1_delete_structure (&definitions); +@@ -82,5 +82,5 @@ test_reproducers (void) + if (verbose) + printf ("Success\n"); + +- exit (EXIT_SUCCESS); ++ return 0; + } +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 12/33 asn1_test: remove 'verbose' and the unnecessary printf()
In reply to Gary Lin via Grub-develThis commit removes the 'verbose' variables and the unnecessary printf() to simplify the output.
--- ...e-verbose-and-the-unnecessary-printf.patch | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0010-asn1_test-remove-verbose-and-the-unnecessary-printf.patch
grub-core/lib/libtasn1-patches/0010-asn1_test-remove-verbose-and-the-unnecessary-printf.patch new file +173 -0
@@ -0,0 +1,173 @@ +From 759bc707a924f89b0d32ba7be2f54e100f1d57b7 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 16 Aug 2024 14:26:38 +0800 +Subject: [PATCH 10/13] asn1_test: remove 'verbose' and the unnecessary + printf() + +This commit removes the 'verbose' variables and the unnecessary printf() +to simplify the output. + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/tests/asn1/tests/CVE-2018-1000654.c | 11 +---- + grub-core/tests/asn1/tests/Test_overflow.c | 40 +++---------------- + grub-core/tests/asn1/tests/reproducers.c | 8 +--- + 3 files changed, 7 insertions(+), 52 deletions(-) + +diff --git a/grub-core/tests/asn1/tests/CVE-2018-1000654.c b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +index 2085b3cd3..81448cde8 100644 +--- a/grub-core/tests/asn1/tests/CVE-2018-1000654.c ++++ b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +@@ -29,15 +29,10 @@ + int + test_CVE_2018_1000654 (void) + { +- int result, verbose = 0; ++ int result; + asn1_node definitions = NULL; + char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; + +- if (argc > 1) +- verbose = 1; +- +- printf ("Test 1\n"); +- + result = + asn1_array2tree (CVE_2018_1000654_1_asn1_tab, &definitions, + errorDescription); +@@ -50,8 +45,6 @@ test_CVE_2018_1000654 (void) + + asn1_delete_structure (&definitions); + +- printf ("Test 2\n"); +- + result = + asn1_array2tree (CVE_2018_1000654_2_asn1_tab, &definitions, + errorDescription); +@@ -64,7 +57,5 @@ test_CVE_2018_1000654 (void) + + asn1_delete_structure (&definitions); + +- if (verbose) +- printf ("Success\n"); + return 0; + } +diff --git a/grub-core/tests/asn1/tests/Test_overflow.c b/grub-core/tests/asn1/tests/Test_overflow.c +index bc28d0826..9f9578a1f 100644 +--- a/grub-core/tests/asn1/tests/Test_overflow.c ++++ b/grub-core/tests/asn1/tests/Test_overflow.c +@@ -27,11 +27,6 @@ test_overflow (void) + { + /* Test that values larger than long are rejected. This has worked + fine with all versions of libtasn1. */ +- int verbose = 0; +- +- if (argc > 1) +- verbose = 1; +- + { + unsigned char der[] = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"; + long l; +@@ -39,12 +34,7 @@ test_overflow (void) + + l = asn1_get_length_der (der, sizeof der, &len); + +- if (l == -2L) +- { +- if (verbose) +- puts ("OK: asn1_get_length_der bignum"); +- } +- else ++ if (l != -2L) + { + printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n", l, len); + return 1; +@@ -65,12 +55,7 @@ test_overflow (void) + + l = asn1_get_length_der (der, der_len, &len); + +- if (l == -2L) +- { +- if (verbose) +- puts ("OK: asn1_get_length_der intnum"); +- } +- else ++ if (l != -2L) + { + printf ("ERROR: asn1_get_length_der intnum (l %ld len %d)\n", l, + len); +@@ -92,12 +77,7 @@ test_overflow (void) + der_len = sizeof (der); + l = asn1_get_length_der (der, der_len, &len); + +- if (l == -4L) +- { +- if (verbose) +- puts ("OK: asn1_get_length_der overflow-small"); +- } +- else ++ if (l != -4L) + { + printf ("ERROR: asn1_get_length_der overflow-small (l %ld len %d)\n", + l, len); +@@ -119,12 +99,7 @@ test_overflow (void) + der_len = sizeof (der); + l = asn1_get_length_der (der, der_len, &len); + +- if (l == -4L) +- { +- if (verbose) +- puts ("OK: asn1_get_length_der overflow-large1"); +- } +- else ++ if (l != -4L) + { + printf ("ERROR: asn1_get_length_der overflow-large1 (l %ld len %d)\n", + l, len); +@@ -146,12 +121,7 @@ test_overflow (void) + der_len = sizeof (der); + l = asn1_get_length_der (der, der_len, &len); + +- if (l == -2L) +- { +- if (verbose) +- puts ("OK: asn1_get_length_der overflow-large2"); +- } +- else ++ if (l != -2L) + { + printf ("ERROR: asn1_get_length_der overflow-large2 (l %ld len %d)\n", + l, len); +diff --git a/grub-core/tests/asn1/tests/reproducers.c b/grub-core/tests/asn1/tests/reproducers.c +index 54a243d51..fa3cea762 100644 +--- a/grub-core/tests/asn1/tests/reproducers.c ++++ b/grub-core/tests/asn1/tests/reproducers.c +@@ -51,13 +51,10 @@ const asn1_static_node tab[] = { + int + test_reproducers (void) + { +- int result, verbose = 0; ++ int result; + asn1_node definitions = NULL; + char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; + +- if (argc > 1) +- verbose = 1; +- + result = asn1_array2tree (endless_asn1_tab, &definitions, errorDescription); + if (result != ASN1_SUCCESS) + { +@@ -79,8 +76,5 @@ test_reproducers (void) + + asn1_delete_structure (&definitions); + +- if (verbose) +- printf ("Success\n"); +- + return 0; + } +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 13/33 asn1_test: print the error messages with grub_printf()
In reply to Gary Lin via Grub-develThis commit replaces printf() and fprintf() with grub_printf() to print the error messages for the testcases. Besides, asn1_strerror() is used to convert the result code to strings instead of asn1_perror().
--- ...-the-error-messages-with-grub_printf.patch | 485 ++++++++++++++++++ 1 file changed, 485 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0011-asn1_test-print-the-error-messages-with-grub_printf.patch
grub-core/lib/libtasn1-patches/0011-asn1_test-print-the-error-messages-with-grub_printf.patch new file +485 -0
@@ -0,0 +1,485 @@ +From af5f0a651de09e44e78177b988857acd3208c4b6 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 16 Aug 2024 15:00:42 +0800 +Subject: [PATCH 11/13] asn1_test: print the error messages with grub_printf() + +This commit replaces printf() and fprintf() with grub_printf() to print +the error messages for the testcases. Besides, asn1_strerror() is used +to convert the result code to strings instead of asn1_perror(). + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/tests/asn1/tests/CVE-2018-1000654.c | 8 +-- + grub-core/tests/asn1/tests/Test_overflow.c | 14 ++--- + grub-core/tests/asn1/tests/Test_simple.c | 21 ++++---- + grub-core/tests/asn1/tests/Test_strings.c | 21 +++----- + .../tests/asn1/tests/object-id-decoding.c | 16 +++--- + .../tests/asn1/tests/object-id-encoding.c | 26 ++++----- + grub-core/tests/asn1/tests/octet-string.c | 54 ++++++++----------- + grub-core/tests/asn1/tests/reproducers.c | 6 +-- + 8 files changed, 67 insertions(+), 99 deletions(-) + +diff --git a/grub-core/tests/asn1/tests/CVE-2018-1000654.c b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +index 81448cde8..b78667900 100644 +--- a/grub-core/tests/asn1/tests/CVE-2018-1000654.c ++++ b/grub-core/tests/asn1/tests/CVE-2018-1000654.c +@@ -38,8 +38,8 @@ test_CVE_2018_1000654 (void) + errorDescription); + if (result != ASN1_RECURSION) + { +- asn1_perror (result); +- printf ("ErrorDescription = %s\n\n", errorDescription); ++ grub_printf ("Error: %s\nErrorDescription = %s\n\n", ++ asn1_strerror (result), errorDescription); + return 1; + } + +@@ -50,8 +50,8 @@ test_CVE_2018_1000654 (void) + errorDescription); + if (result != ASN1_RECURSION) + { +- asn1_perror (result); +- printf ("ErrorDescription = %s\n\n", errorDescription); ++ grub_printf ("Error: %s\nErrorDescription = %s\n\n", ++ asn1_strerror (result), errorDescription); + return 1; + } + +diff --git a/grub-core/tests/asn1/tests/Test_overflow.c b/grub-core/tests/asn1/tests/Test_overflow.c +index 9f9578a1f..ffac8507a 100644 +--- a/grub-core/tests/asn1/tests/Test_overflow.c ++++ b/grub-core/tests/asn1/tests/Test_overflow.c +@@ -36,7 +36,7 @@ test_overflow (void) + + if (l != -2L) + { +- printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n", l, len); ++ grub_printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n", l, len); + return 1; + } + } +@@ -57,8 +57,7 @@ test_overflow (void) + + if (l != -2L) + { +- printf ("ERROR: asn1_get_length_der intnum (l %ld len %d)\n", l, +- len); ++ grub_printf ("ERROR: asn1_get_length_der intnum (l %ld len %d)\n", l, len); + return 1; + } + } +@@ -79,8 +78,7 @@ test_overflow (void) + + if (l != -4L) + { +- printf ("ERROR: asn1_get_length_der overflow-small (l %ld len %d)\n", +- l, len); ++ grub_printf ("ERROR: asn1_get_length_der overflow-small (l %ld len %d)\n", l, len); + return 1; + } + } +@@ -101,8 +99,7 @@ test_overflow (void) + + if (l != -4L) + { +- printf ("ERROR: asn1_get_length_der overflow-large1 (l %ld len %d)\n", +- l, len); ++ grub_printf ("ERROR: asn1_get_length_der overflow-large1 (l %ld len %d)\n", l, len); + return 1; + } + } +@@ -123,8 +120,7 @@ test_overflow (void) + + if (l != -2L) + { +- printf ("ERROR: asn1_get_length_der overflow-large2 (l %ld len %d)\n", +- l, len); ++ grub_printf ("ERROR: asn1_get_length_der overflow-large2 (l %ld len %d)\n", l, len); + return 1; + } + } +diff --git a/grub-core/tests/asn1/tests/Test_simple.c b/grub-core/tests/asn1/tests/Test_simple.c +index 12993bfba..dc70db191 100644 +--- a/grub-core/tests/asn1/tests/Test_simple.c ++++ b/grub-core/tests/asn1/tests/Test_simple.c +@@ -94,7 +94,7 @@ test_simple (void) + result = asn1_encode_simple_der (etype, my_str, my_str_len, tl, &tl_len); + if (result != ASN1_VALUE_NOT_VALID) + { +- fprintf (stderr, "asn1_encode_simple_der out of range etype\n"); ++ grub_printf ("asn1_encode_simple_der out of range etype\n"); + return 1; + } + } +@@ -105,7 +105,7 @@ test_simple (void) + result = asn1_get_bit_der (der, 0, &ret_len, str, str_size, &bit_len); + if (result != ASN1_GENERIC_ERROR) + { +- fprintf (stderr, "asn1_get_bit_der zero\n"); ++ grub_printf ("asn1_get_bit_der zero\n"); + return 1; + } + +@@ -129,7 +129,7 @@ test_simple (void) + + if (der_len != tv[i].derlen || memcmp (der, tv[i].der, der_len) != 0) + { +- fprintf (stderr, "asn1_bit_der iter %lu\n", (unsigned long) i); ++ grub_printf ("asn1_bit_der iter %lu\n", (unsigned long) i); + return 1; + } + +@@ -140,8 +140,7 @@ test_simple (void) + if (result != ASN1_SUCCESS || ret_len != tv[i].derlen + || bit_len != tv[i].bitlen) + { +- fprintf (stderr, "asn1_get_bit_der iter %lu, err: %d\n", +- (unsigned long) i, result); ++ grub_printf ("asn1_get_bit_der iter %lu, err: %d\n", (unsigned long) i, result); + return 1; + } + } +@@ -163,7 +162,7 @@ test_simple (void) + if (result != ASN1_SUCCESS || ret_len != 5 + || bit_len != 18 || memcmp (str, "\x6e\x5d\xc0", 3) != 0) + { +- fprintf (stderr, "asn1_get_bit_der example\n"); ++ grub_printf ("asn1_get_bit_der example\n"); + return 1; + } + +@@ -171,7 +170,7 @@ test_simple (void) + asn1_bit_der (str, bit_len, der, &der_len); + if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0) + { +- fprintf (stderr, "asn1_bit_der example roundtrip\n"); ++ grub_printf ("asn1_bit_der example roundtrip\n"); + return 1; + } + +@@ -184,7 +183,7 @@ test_simple (void) + if (result != ASN1_SUCCESS || ret_len != 5 + || bit_len != 18 || memcmp (str, "\x6e\x5d\xe0", 3) != 0) + { +- fprintf (stderr, "asn1_get_bit_der example padded\n"); ++ grub_printf ("asn1_get_bit_der example padded\n"); + return 1; + } + +@@ -192,7 +191,7 @@ test_simple (void) + asn1_bit_der (str, bit_len, der, &der_len); + if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0) + { +- fprintf (stderr, "asn1_bit_der example roundtrip\n"); ++ grub_printf ("asn1_bit_der example roundtrip\n"); + return 1; + } + +@@ -206,7 +205,7 @@ test_simple (void) + if (result != ASN1_SUCCESS || ret_len != 6 + || bit_len != 18 || memcmp (str, "\x6e\x5d\xc0", 3) != 0) + { +- fprintf (stderr, "asn1_get_bit_der example long form\n"); ++ grub_printf ("asn1_get_bit_der example long form\n"); + return 1; + } + +@@ -214,7 +213,7 @@ test_simple (void) + asn1_bit_der (str, bit_len, der, &der_len); + if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0) + { +- fprintf (stderr, "asn1_bit_der example roundtrip\n"); ++ grub_printf ("asn1_bit_der example roundtrip\n"); + return 1; + } + +diff --git a/grub-core/tests/asn1/tests/Test_strings.c b/grub-core/tests/asn1/tests/Test_strings.c +index 2538f2558..65c30937f 100644 +--- a/grub-core/tests/asn1/tests/Test_strings.c ++++ b/grub-core/tests/asn1/tests/Test_strings.c +@@ -89,17 +89,14 @@ test_strings (void) + tl, &tl_len); + if (ret != ASN1_SUCCESS) + { +- fprintf (stderr, "Encoding error in %u: %s\n", i, +- asn1_strerror (ret)); ++ 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 || memcmp (tl, tv[i].der, tl_len) != 0) + { +- fprintf (stderr, +- "DER encoding differs in %u! (size: %u, expected: %u)\n", +- i, der_len, tv[i].der_len); ++ grub_printf ("DER encoding differs in %u! (size: %u, expected: %u)\n", i, der_len, tv[i].der_len); + return 1; + } + +@@ -109,16 +106,13 @@ test_strings (void) + &str_len); + if (ret != ASN1_SUCCESS) + { +- fprintf (stderr, "Decoding error in %u: %s\n", i, +- asn1_strerror (ret)); ++ grub_printf ("Decoding error in %u: %s\n", i, asn1_strerror (ret)); + return 1; + } + + if (str_len != tv[i].str_len || memcmp (str, tv[i].str, str_len) != 0) + { +- fprintf (stderr, +- "DER decoded data differ in %u! (size: %u, expected: %u)\n", +- i, der_len, tv[i].str_len); ++ grub_printf ("DER decoded data differ in %u! (size: %u, expected: %u)\n", i, der_len, tv[i].str_len); + return 1; + } + } +@@ -132,16 +126,13 @@ test_strings (void) + &str_len, NULL); + if (ret != ASN1_SUCCESS) + { +- fprintf (stderr, "BER decoding error in %u: %s\n", i, +- asn1_strerror (ret)); ++ grub_printf ("BER decoding error in %u: %s\n", i, asn1_strerror (ret)); + return 1; + } + + if (str_len != ber[i].str_len || memcmp (b, ber[i].str, str_len) != 0) + { +- fprintf (stderr, +- "BER decoded data differ in %u! (size: %u, expected: %u)\n", +- i, str_len, ber[i].str_len); ++ grub_printf ("BER decoded data differ in %u! (size: %u, expected: %u)\n", i, str_len, ber[i].str_len); + return 1; + } + free (b); +diff --git a/grub-core/tests/asn1/tests/object-id-decoding.c b/grub-core/tests/asn1/tests/object-id-decoding.c +index fdbb8ea21..c087b46e3 100644 +--- a/grub-core/tests/asn1/tests/object-id-decoding.c ++++ b/grub-core/tests/asn1/tests/object-id-decoding.c +@@ -85,10 +85,8 @@ test_object_id_decoding (void) + sizeof (str)); + if (ret != tv[i].expected_error) + { +- fprintf (stderr, +- "%d: asn1_get_object_id_der iter %lu: got '%s' expected %d\n", +- __LINE__, (unsigned long) i, asn1_strerror (ret), +- tv[i].expected_error); ++ 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; + } + +@@ -97,17 +95,15 @@ test_object_id_decoding (void) + + if (ret_len != tv[i].der_len - 1) + { +- fprintf (stderr, +- "%d: iter %lu: error in DER, length returned is %d, had %d\n", +- __LINE__, (unsigned long) i, ret_len, tv[i].der_len - 1); ++ 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 (strcmp (tv[i].oid, str) != 0) + { +- fprintf (stderr, +- "%d: strcmp iter %lu: got invalid OID: %s, expected: %s\n", +- __LINE__, (unsigned long) i, str, tv[i].oid); ++ grub_printf ("%d: strcmp iter %lu: got invalid OID: %s, expected: %s\n", ++ __LINE__, (unsigned long) i, str, tv[i].oid); + return 1; + } + +diff --git a/grub-core/tests/asn1/tests/object-id-encoding.c b/grub-core/tests/asn1/tests/object-id-encoding.c +index a497015e3..e3da092cc 100644 +--- a/grub-core/tests/asn1/tests/object-id-encoding.c ++++ b/grub-core/tests/asn1/tests/object-id-encoding.c +@@ -93,33 +93,29 @@ test_object_id_encoding (void) + { + if (ret == tv[i].expected_error) + continue; +- fprintf (stderr, +- "%d: iter %lu, encoding of OID failed: %s\n", +- __LINE__, (unsigned long) i, asn1_strerror (ret)); ++ 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) + { +- fprintf (stderr, +- "%d: iter %lu, encoding of OID %s succeeded when expecting failure\n", +- __LINE__, (unsigned long) i, tv[i].oid); ++ 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 || memcmp (der, tv[i].der, der_len) != 0) + { +- fprintf (stderr, +- "%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); +- fprintf (stderr, "\nGot:\t\t"); ++ 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); ++ grub_printf ("\nGot:\t\t"); + for (j = 0; j < der_len; j++) +- fprintf (stderr, "%.2x", der[j]); ++ grub_printf ("%.2x", der[j]); + +- fprintf (stderr, "\nExpected:\t"); ++ grub_printf ("\nExpected:\t"); + for (j = 0; j < tv[i].der_len; j++) +- fprintf (stderr, "%.2x", tv[i].der[j]); +- fprintf (stderr, "\n"); ++ grub_printf ("%.2x", tv[i].der[j]); ++ grub_printf ("\n"); + + return 1; + } +diff --git a/grub-core/tests/asn1/tests/octet-string.c b/grub-core/tests/asn1/tests/octet-string.c +index 8c49c6e0c..d3a35dff8 100644 +--- a/grub-core/tests/asn1/tests/octet-string.c ++++ b/grub-core/tests/asn1/tests/octet-string.c +@@ -131,9 +131,8 @@ test_octet_string (void) + sizeof (str), &str_size); + if (ret != tv[i].expected_error) + { +- fprintf (stderr, +- "%d: asn1_get_octet_der: %s: got %d expected %d\n", +- __LINE__, tv[i].name, ret, tv[i].expected_error); ++ 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) +@@ -141,27 +140,25 @@ test_octet_string (void) + + if (ret_len != tv[i].der_len - 1) + { +- fprintf (stderr, +- "%d: error in DER, length returned is %d, had %d\n", +- __LINE__, ret_len, tv[i].der_len - 1); ++ 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 + || memcmp (tv[i].string, str, tv[i].len) != 0) + { +- fprintf (stderr, +- "%d: memcmp: %s: got invalid decoding\n", +- __LINE__, tv[i].name); ++ grub_printf ("%d: memcmp: %s: got invalid decoding\n", ++ __LINE__, tv[i].name); + +- fprintf (stderr, "\nGot:\t\t"); ++ grub_printf ("\nGot:\t\t"); + for (j = 0; j < str_size; j++) +- fprintf (stderr, "%.2x", str[j]); ++ grub_printf ("%.2x", str[j]); + +- fprintf (stderr, "\nExpected:\t"); ++ grub_printf ("\nExpected:\t"); + for (j = 0; j < tv[i].len; j++) +- fprintf (stderr, "%.2x", tv[i].string[j]); +- fprintf (stderr, "\n"); ++ grub_printf ("%.2x", tv[i].string[j]); ++ grub_printf ("\n"); + return 1; + } + +@@ -172,8 +169,7 @@ test_octet_string (void) + if (der_len != tv[i].der_len - 1 + || memcmp (tv[i].der_str + 1, der, tv[i].der_len - 1) != 0) + { +- fprintf (stderr, +- "encoding: %s: got invalid encoding\n", tv[i].name); ++ grub_printf ("encoding: %s: got invalid encoding\n", tv[i].name); + return 1; + } + } +@@ -185,10 +181,9 @@ test_octet_string (void) + (unsigned int *) &der_len); + if (ret != tv[i].expected_error) + { +- fprintf (stderr, +- "%d: asn1_decode_simple_ber: %s: got %s expected %s\n", +- __LINE__, tv[i].name, asn1_strerror (ret), +- asn1_strerror (tv[i].expected_error)); ++ 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) +@@ -196,25 +191,22 @@ test_octet_string (void) + + if (der_len != tv[i].der_len) + { +- fprintf (stderr, +- "%d: error: %s: DER, length returned is %d, had %d\n", +- __LINE__, tv[i].name, der_len, tv[i].der_len); ++ 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 || memcmp (tv[i].string, tmp, tv[i].len) != 0) + { +- fprintf (stderr, +- "%d: memcmp: %s: got invalid decoding\n", +- __LINE__, tv[i].name); +- fprintf (stderr, "\nGot:\t\t"); ++ grub_printf ("%d: memcmp: %s: got invalid decoding\n", __LINE__, tv[i].name); ++ grub_printf ("\nGot:\t\t"); + for (j = 0; j < str_size; j++) +- fprintf (stderr, "%.2x", tmp[j]); ++ grub_printf ("%.2x", tmp[j]); + +- fprintf (stderr, "\nExpected:\t"); ++ grub_printf ("\nExpected:\t"); + for (j = 0; j < tv[i].len; j++) +- fprintf (stderr, "%.2x", tv[i].string[j]); +- fprintf (stderr, "\n"); ++ grub_printf ("%.2x", tv[i].string[j]); ++ grub_printf ("\n"); + return 1; + } + free (tmp); +diff --git a/grub-core/tests/asn1/tests/reproducers.c b/grub-core/tests/asn1/tests/reproducers.c +index fa3cea762..0e3c9fd65 100644 +--- a/grub-core/tests/asn1/tests/reproducers.c ++++ b/grub-core/tests/asn1/tests/reproducers.c +@@ -58,8 +58,7 @@ test_reproducers (void) + result = asn1_array2tree (endless_asn1_tab, &definitions, errorDescription); + if (result != ASN1_SUCCESS) + { +- asn1_perror (result); +- printf ("ErrorDescription = %s\n\n", errorDescription); ++ grub_printf ("Error: %s\nErrorDescription = %s\n\n", asn1_strerror (result), errorDescription); + return 1; + } + +@@ -69,8 +68,7 @@ test_reproducers (void) + result = asn1_array2tree (tab, &definitions, errorDescription); + if (result != ASN1_SUCCESS) + { +- asn1_perror (result); +- printf ("ErrorDescription = %s\n\n", errorDescription); ++ grub_printf ("Error: %s\nErrorDescription = %s\n\n", asn1_strerror (result), errorDescription); + return 1; + } + +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 14/33 asn1_test: use the grub-specific functions and types
In reply to Gary Lin via Grub-develThis commit converts functions and types to the grub-specific ones:
LONG_MAX -> GRUB_LONG_MAXINT_MAX -> GRUB_INT_MAXUINT_MAX -> GRUB_UINT_MAXsize_t -> grub_size_tmemcmp() -> grub_memcmp()memcpy() -> grub_memcpy()free() -> grub_free()strcmp() -> grub_strcmp()
--- ...he-grub-specific-functions-and-types.patch | 263 ++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0012-asn1_test-use-the-grub-specific-functions-and-types.patch
grub-core/lib/libtasn1-patches/0012-asn1_test-use-the-grub-specific-functions-and-types.patch new file +263 -0
@@ -0,0 +1,263 @@ +From 392da24a8a4b2ab94a93e9e0f74db027a3a9374d Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Fri, 16 Aug 2024 15:46:59 +0800 +Subject: [PATCH 12/13] asn1_test: use the grub-specific functions and types + +This commit converts functions and types to the grub-specific ones: + +LONG_MAX -> GRUB_LONG_MAX +INT_MAX -> GRUB_INT_MAX +UINT_MAX -> GRUB_UINT_MAX +size_t -> grub_size_t +memcmp() -> grub_memcmp() +memcpy() -> grub_memcpy() +free() -> grub_free() +strcmp() -> grub_strcmp() + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Daniel Kiper <[email protected]> +--- + grub-core/tests/asn1/tests/Test_overflow.c | 4 ++-- + grub-core/tests/asn1/tests/Test_simple.c | 22 +++++++++---------- + grub-core/tests/asn1/tests/Test_strings.c | 8 +++---- + .../tests/asn1/tests/object-id-decoding.c | 4 ++-- + .../tests/asn1/tests/object-id-encoding.c | 2 +- + grub-core/tests/asn1/tests/octet-string.c | 10 ++++----- + 6 files changed, 25 insertions(+), 25 deletions(-) + +diff --git a/grub-core/tests/asn1/tests/Test_overflow.c b/grub-core/tests/asn1/tests/Test_overflow.c +index ffac8507a..65843abf6 100644 +--- a/grub-core/tests/asn1/tests/Test_overflow.c ++++ b/grub-core/tests/asn1/tests/Test_overflow.c +@@ -43,9 +43,9 @@ test_overflow (void) + + /* Test that values larger than int but smaller than long are + rejected. This limitation was introduced with libtasn1 2.12. */ +- if (LONG_MAX > INT_MAX) ++ if (GRUB_LONG_MAX > GRUB_INT_MAX) + { +- unsigned long num = ((long) UINT_MAX) << 2; ++ unsigned long num = ((long) GRUB_UINT_MAX) << 2; + unsigned char der[20]; + int der_len; + long l; +diff --git a/grub-core/tests/asn1/tests/Test_simple.c b/grub-core/tests/asn1/tests/Test_simple.c +index dc70db191..19613cae8 100644 +--- a/grub-core/tests/asn1/tests/Test_simple.c ++++ b/grub-core/tests/asn1/tests/Test_simple.c +@@ -81,7 +81,7 @@ test_simple (void) + int der_len = sizeof (der); + int str_size = sizeof (str); + int ret_len, bit_len; +- size_t i; ++ grub_size_t i; + + { + unsigned int etype = 38; +@@ -127,7 +127,7 @@ test_simple (void) + } + #endif + +- if (der_len != tv[i].derlen || memcmp (der, tv[i].der, der_len) != 0) ++ if (der_len != tv[i].derlen || grub_memcmp (der, tv[i].der, der_len) != 0) + { + grub_printf ("asn1_bit_der iter %lu\n", (unsigned long) i); + return 1; +@@ -155,12 +155,12 @@ test_simple (void) + + /* 03 04 06 6e 5d c0 DER encoding */ + +- memcpy (der, "\x04\x06\x6e\x5d\xc0", 5); ++ grub_memcpy (der, "\x04\x06\x6e\x5d\xc0", 5); + der_len = 5; + + result = asn1_get_bit_der (der, der_len, &ret_len, str, str_size, &bit_len); + if (result != ASN1_SUCCESS || ret_len != 5 +- || bit_len != 18 || memcmp (str, "\x6e\x5d\xc0", 3) != 0) ++ || bit_len != 18 || grub_memcmp (str, "\x6e\x5d\xc0", 3) != 0) + { + grub_printf ("asn1_get_bit_der example\n"); + return 1; +@@ -168,7 +168,7 @@ test_simple (void) + + der_len = sizeof (der); + asn1_bit_der (str, bit_len, der, &der_len); +- if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0) ++ if (der_len != 5 || grub_memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0) + { + grub_printf ("asn1_bit_der example roundtrip\n"); + return 1; +@@ -176,12 +176,12 @@ test_simple (void) + + /* 03 04 06 6e 5d e0 padded with "100000" */ + +- memcpy (der, "\x04\x06\x6e\x5d\xe0", 5); ++ grub_memcpy (der, "\x04\x06\x6e\x5d\xe0", 5); + der_len = 5; + + result = asn1_get_bit_der (der, der_len, &ret_len, str, str_size, &bit_len); + if (result != ASN1_SUCCESS || ret_len != 5 +- || bit_len != 18 || memcmp (str, "\x6e\x5d\xe0", 3) != 0) ++ || bit_len != 18 || grub_memcmp (str, "\x6e\x5d\xe0", 3) != 0) + { + grub_printf ("asn1_get_bit_der example padded\n"); + return 1; +@@ -189,7 +189,7 @@ test_simple (void) + + der_len = sizeof (der); + asn1_bit_der (str, bit_len, der, &der_len); +- if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0) ++ if (der_len != 5 || grub_memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0) + { + grub_printf ("asn1_bit_der example roundtrip\n"); + return 1; +@@ -197,13 +197,13 @@ test_simple (void) + + /* 03 81 04 06 6e 5d c0 long form of length octets */ + +- memcpy (der, "\x81\x04\x06\x6e\x5d\xc0", 6); ++ grub_memcpy (der, "\x81\x04\x06\x6e\x5d\xc0", 6); + der_len = 6; + + result = asn1_get_bit_der (der, der_len, &ret_len, str, str_size, &bit_len); + + if (result != ASN1_SUCCESS || ret_len != 6 +- || bit_len != 18 || memcmp (str, "\x6e\x5d\xc0", 3) != 0) ++ || bit_len != 18 || grub_memcmp (str, "\x6e\x5d\xc0", 3) != 0) + { + grub_printf ("asn1_get_bit_der example long form\n"); + return 1; +@@ -211,7 +211,7 @@ test_simple (void) + + der_len = sizeof (der); + asn1_bit_der (str, bit_len, der, &der_len); +- if (der_len != 5 || memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0) ++ if (der_len != 5 || grub_memcmp (der, "\x04\x06\x6e\x5d\xc0", 5) != 0) + { + grub_printf ("asn1_bit_der example roundtrip\n"); + return 1; +diff --git a/grub-core/tests/asn1/tests/Test_strings.c b/grub-core/tests/asn1/tests/Test_strings.c +index 65c30937f..c7c1afa1b 100644 +--- a/grub-core/tests/asn1/tests/Test_strings.c ++++ b/grub-core/tests/asn1/tests/Test_strings.c +@@ -94,7 +94,7 @@ test_strings (void) + } + der_len = tl_len + tv[i].str_len; + +- if (der_len != tv[i].der_len || memcmp (tl, tv[i].der, tl_len) != 0) ++ if (der_len != tv[i].der_len || grub_memcmp (tl, tv[i].der, tl_len) != 0) + { + grub_printf ("DER encoding differs in %u! (size: %u, expected: %u)\n", i, der_len, tv[i].der_len); + return 1; +@@ -110,7 +110,7 @@ test_strings (void) + return 1; + } + +- if (str_len != tv[i].str_len || memcmp (str, tv[i].str, str_len) != 0) ++ if (str_len != tv[i].str_len || grub_memcmp (str, tv[i].str, str_len) != 0) + { + grub_printf ("DER decoded data differ in %u! (size: %u, expected: %u)\n", i, der_len, tv[i].str_len); + return 1; +@@ -130,12 +130,12 @@ test_strings (void) + return 1; + } + +- if (str_len != ber[i].str_len || memcmp (b, ber[i].str, str_len) != 0) ++ if (str_len != ber[i].str_len || grub_memcmp (b, ber[i].str, str_len) != 0) + { + grub_printf ("BER decoded data differ in %u! (size: %u, expected: %u)\n", i, str_len, ber[i].str_len); + return 1; + } +- free (b); ++ grub_free (b); + } + + +diff --git a/grub-core/tests/asn1/tests/object-id-decoding.c b/grub-core/tests/asn1/tests/object-id-decoding.c +index c087b46e3..91af8cde5 100644 +--- a/grub-core/tests/asn1/tests/object-id-decoding.c ++++ b/grub-core/tests/asn1/tests/object-id-decoding.c +@@ -74,7 +74,7 @@ test_object_id_decoding (void) + { + char str[128]; + int ret, ret_len; +- size_t i; ++ grub_size_t i; + + for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++) + { +@@ -100,7 +100,7 @@ test_object_id_decoding (void) + return 1; + } + +- if (strcmp (tv[i].oid, str) != 0) ++ if (grub_strcmp (tv[i].oid, str) != 0) + { + grub_printf ("%d: strcmp iter %lu: got invalid OID: %s, expected: %s\n", + __LINE__, (unsigned long) i, str, tv[i].oid); +diff --git a/grub-core/tests/asn1/tests/object-id-encoding.c b/grub-core/tests/asn1/tests/object-id-encoding.c +index e3da092cc..f8f98ff17 100644 +--- a/grub-core/tests/asn1/tests/object-id-encoding.c ++++ b/grub-core/tests/asn1/tests/object-id-encoding.c +@@ -104,7 +104,7 @@ test_object_id_encoding (void) + return 1; + } + +- if (der_len != tv[i].der_len || memcmp (der, tv[i].der, der_len) != 0) ++ if (der_len != tv[i].der_len || grub_memcmp (der, tv[i].der, der_len) != 0) + { + 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); +diff --git a/grub-core/tests/asn1/tests/octet-string.c b/grub-core/tests/asn1/tests/octet-string.c +index d3a35dff8..dcf0fb808 100644 +--- a/grub-core/tests/asn1/tests/octet-string.c ++++ b/grub-core/tests/asn1/tests/octet-string.c +@@ -116,7 +116,7 @@ test_octet_string (void) + int str_size = sizeof (str); + unsigned char *tmp = NULL; + int ret, ret_len, j; +- size_t i; ++ grub_size_t i; + + for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++) + { +@@ -146,7 +146,7 @@ test_octet_string (void) + } + + if (str_size != tv[i].len +- || memcmp (tv[i].string, str, tv[i].len) != 0) ++ || grub_memcmp (tv[i].string, str, tv[i].len) != 0) + { + grub_printf ("%d: memcmp: %s: got invalid decoding\n", + __LINE__, tv[i].name); +@@ -167,7 +167,7 @@ test_octet_string (void) + asn1_octet_der (str, str_size, der, &der_len); + + if (der_len != tv[i].der_len - 1 +- || memcmp (tv[i].der_str + 1, der, tv[i].der_len - 1) != 0) ++ || grub_memcmp (tv[i].der_str + 1, der, tv[i].der_len - 1) != 0) + { + grub_printf ("encoding: %s: got invalid encoding\n", tv[i].name); + return 1; +@@ -196,7 +196,7 @@ test_octet_string (void) + return 1; + } + +- if (str_size != tv[i].len || memcmp (tv[i].string, tmp, tv[i].len) != 0) ++ if (str_size != tv[i].len || grub_memcmp (tv[i].string, tmp, tv[i].len) != 0) + { + grub_printf ("%d: memcmp: %s: got invalid decoding\n", __LINE__, tv[i].name); + grub_printf ("\nGot:\t\t"); +@@ -209,7 +209,7 @@ test_octet_string (void) + grub_printf ("\n"); + return 1; + } +- free (tmp); ++ grub_free (tmp); + tmp = NULL; + + } +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 15/33 asn1_test: exclude a testcase when long and int are the same
In reply to Gary Lin via Grub-develThere is a testcase to test the values larger than 'int' but smaller than 'long'. However, for some architectures, 'long' and 'int' are the same, and the compiler may issue a warning like this:
grub-core/tests/asn1/tests/Test_overflow.c:48:50: error: left shift of negative value [-Werror=shift-negative-value]
unsigned long num = ((long) GRUB_UINT_MAX) << 2;
^~To avoid the unnecessary error, the testcase is opted out when GRUB_LONG_MAX is not larger than GRUB_INT_MAX.
--- ...e-a-testcase-when-long-and-int-are-t.patch | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0013-asn1_test-exclude-a-testcase-when-long-and-int-are-t.patch
grub-core/lib/libtasn1-patches/0013-asn1_test-exclude-a-testcase-when-long-and-int-are-t.patch new file +47 -0
@@ -0,0 +1,47 @@ +From 737bef13796f6dbeb4178d42bdb1f3e496971d52 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Mon, 7 Oct 2024 11:33:19 +0800 +Subject: [PATCH 13/13] asn1_test: exclude a testcase when long and int are the + same + +There is a testcase to test the values larger than 'int' but smaller +than 'long'. However, for some architectures, 'long' and 'int' are the +same, and the compiler may issue a warning like this: + +grub-core/tests/asn1/tests/Test_overflow.c:48:50: error: left shift of negative value [-Werror=shift-negative-value] + unsigned long num = ((long) GRUB_UINT_MAX) << 2; + ^~ + +To avoid the unnecessary error, the testcase is opted out when +GRUB_LONG_MAX is not larger than GRUB_INT_MAX. + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +--- + grub-core/tests/asn1/tests/Test_overflow.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/grub-core/tests/asn1/tests/Test_overflow.c b/grub-core/tests/asn1/tests/Test_overflow.c +index 65843abf6..fdeff5634 100644 +--- a/grub-core/tests/asn1/tests/Test_overflow.c ++++ b/grub-core/tests/asn1/tests/Test_overflow.c +@@ -43,7 +43,7 @@ test_overflow (void) + + /* Test that values larger than int but smaller than long are + rejected. This limitation was introduced with libtasn1 2.12. */ +- if (GRUB_LONG_MAX > GRUB_INT_MAX) ++#if (GRUB_LONG_MAX > GRUB_INT_MAX) + { + unsigned long num = ((long) GRUB_UINT_MAX) << 2; + unsigned char der[20]; +@@ -61,6 +61,7 @@ test_overflow (void) + return 1; + } + } ++#endif + + /* Test that values larger than would fit in the input string are + rejected. This problem was fixed in libtasn1 2.12. */ +-- +2.43.0 +
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 15/33] asn1_test: exclude a testcase when long and int are the same
Quoted 10 lines · Gary Lin
There is a testcase to test the values larger than 'int' but smaller than 'long'. However, for some architectures, 'long' and 'int' are the same, and the compiler may issue a warning like this:
grub-core/tests/asn1/tests/Test_overflow.c:48:50: error: left shift of negative value [-Werror=shift-negative-value]unsigned long num = ((long) GRUB_UINT_MAX) << 2; ^~To avoid the unnecessary error, the testcase is opted out when
GRUB_LONG_MAXis not larger thanGRUB_INT_MAX.
I would say instead: To avoid unnecessary error the testcase is enabled only when GRUB_LONG_MAX is larger than GRUB_INT_MAX.
Please fix similar sentence below.
Quoted 2 lines
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Gary Lin <[email protected]>
Otherwise Reviewed-by: Daniel Kiper <[email protected]>...
Quoted 45 lines
--- ...e-a-testcase-when-long-and-int-are-t.patch | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0013-asn1_test-exclude-a-testcase-when-long-and-int-are-t.patchgrub-core/lib/libtasn1-patches/0013-asn1_test-exclude-a-testcase-when-long-and-int-are-t.patch new file +34
-0@@ -0,0 +1,47 @@ +From 737bef13796f6dbeb4178d42bdb1f3e496971d52 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Mon, 7 Oct 2024 11:33:19 +0800 +Subject: [PATCH 13/13] asn1_test: exclude a testcase when long and int are the + same + +There is a testcase to test the values larger than 'int' but smaller +than 'long'. However, for some architectures, 'long' and 'int' are the +same, and the compiler may issue a warning like this: + +grub-core/tests/asn1/tests/Test_overflow.c:48:50: error: left shift of negative value [-Werror=shift-negative-value] + unsigned long num = ((long) GRUB_UINT_MAX) << 2; + ^~ + +To avoid the unnecessary error, the testcase is opted out when +GRUB_LONG_MAX is not larger than GRUB_INT_MAX. + +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +--- + grub-core/tests/asn1/tests/Test_overflow.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/grub-core/tests/asn1/tests/Test_overflow.c b/grub-core/tests/asn1/tests/Test_overflow.c +index 65843abf6..fdeff5634 100644 +--- a/grub-core/tests/asn1/tests/Test_overflow.c ++++ b/grub-core/tests/asn1/tests/Test_overflow.c +@@ -43,7 +43,7 @@ test_overflow (void) + + /* Test that values larger than int but smaller than long are + rejected. This limitation was introduced with libtasn1 2.12. */ +- if (GRUB_LONG_MAX > GRUB_INT_MAX) ++#if (GRUB_LONG_MAX > GRUB_INT_MAX) + {
grub-core/lib/libtasn1-patches/0013-asn1_test-exclude-a-testcase-when-long-and-int-are-t.patch line 44
Quoted 10 lines
grub-core/lib/libtasn1-patches/0013-asn1_test-exclude-a-testcase-when-long-and-int-are-t.patch new file +10
-0+ unsigned long num = ((long) GRUB_UINT_MAX) << 2; + unsigned char der[20]; +@@ -61,6 +61,7 @@ test_overflow (void) + return 1; + } + } ++#endif + + /* Test that values larger than would fit in the input string are + rejected. This problem was fixed in libtasn1 2.12. */
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 16/33 libtasn1: compile into asn1 module
In reply to Gary Lin via Grub-develFrom: Daniel Axtens <[email protected]>
Create a wrapper file that specifies the module license. Set up the makefile so it is built.
--- autogen.sh | 19 +++++++++++++++++++ grub-core/Makefile.core.def | 15 +++++++++++++++ grub-core/lib/libtasn1_wrap/wrap.c | 27 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 grub-core/lib/libtasn1_wrap/wrap.c
autogen.sh +19 -0
@@ -51,6 +51,25 @@ for x in mpi-asm-defs.h mpih-add1.c mpih-sub1.c mpih-mul1.c mpih-mul2.c mpih-mul cp grub-core/lib/libgcrypt-grub/mpi/generic/"$x" grub-core/lib/libgcrypt-grub/mpi/"$x" done +echo "Importing libtasn1..." +if [ -d grub-core/lib/libtasn1-grub ]; then + rm -rf grub-core/lib/libtasn1-grub +fi + +mkdir -p grub-core/lib/libtasn1-grub/lib +cp grub-core/lib/libtasn1/lib/*.[ch] grub-core/lib/libtasn1-grub/lib +cp grub-core/lib/libtasn1/libtasn1.h grub-core/lib/libtasn1-grub/ + +for patch in \ + 0001-libtasn1-disable-code-not-needed-in-grub.patch \ + 0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch \ + 0003-libtasn1-replace-strcat-with-_asn1_str_cat.patch \ + 0004-libtasn1-adjust-the-header-paths-in-libtasn1.h.patch \ + 0005-libtasn1-Use-grub_divmod64-for-division.patch \ + 0006-libtasn1-fix-the-potential-buffer-overrun.patch ; do + patch -p1 -i grub-core/lib/libtasn1-patches/$patch +done + echo "Generating Automake input..." # Automake doesn't like including files from a path outside the project.
grub-core/Makefile.core.def +15 -0
@@ -2606,3 +2606,18 @@ module = { enable = efi; depends = part_gpt; }; + +module = { + name = asn1; + common = lib/libtasn1-grub/lib/decoding.c; + common = lib/libtasn1-grub/lib/coding.c; + common = lib/libtasn1-grub/lib/element.c; + common = lib/libtasn1-grub/lib/structure.c; + common = lib/libtasn1-grub/lib/parser_aux.c; + common = lib/libtasn1-grub/lib/gstr.c; + common = lib/libtasn1-grub/lib/errors.c; + common = lib/libtasn1_wrap/wrap.c; + cflags = '$(CFLAGS_POSIX) $(CFLAGS_GNULIB)'; + /* -Wno-type-limits comes from configure.ac of libtasn1 */ + cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/lib/libtasn1-grub/lib -Wno-type-limits'; +};
grub-core/lib/libtasn1_wrap/wrap.c new file +27 -0
@@ -0,0 +1,27 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2020 IBM Corporation +* 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/>. +*/ + +#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.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 17/33 asn1_test: test module for libtasn1
In reply to Gary Lin via Grub-develFrom: Daniel Axtens <[email protected]>
Import tests from libtasn1 that use functionality we 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 with5e10be48e5 tests: Add check-native and check-nonnative make targets
--- Makefile.util.def | 6 ++++ autogen.sh | 16 ++++++++- grub-core/Makefile.core.def | 15 ++++++++ grub-core/tests/asn1/asn1_test.c | 50 +++++++++++++++++++++++++++ grub-core/tests/asn1/asn1_test.h | 45 ++++++++++++++++++++++++ grub-core/tests/lib/functional_test.c | 1 + tests/asn1_test.in | 11 ++++++ 7 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 grub-core/tests/asn1/asn1_test.c create mode 100644 grub-core/tests/asn1/asn1_test.h create mode 100644 tests/asn1_test.in
Makefile.util.def +6 -0
@@ -1257,6 +1257,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;
autogen.sh +15 -1
@@ -60,13 +60,27 @@ mkdir -p grub-core/lib/libtasn1-grub/lib cp grub-core/lib/libtasn1/lib/*.[ch] grub-core/lib/libtasn1-grub/lib cp grub-core/lib/libtasn1/libtasn1.h grub-core/lib/libtasn1-grub/ +if [ -d grub-core/tests/asn1/tests ]; then + rm -rf grub-core/tests/asn1/tests +fi + +mkdir grub-core/tests/asn1/tests +cp grub-core/lib/libtasn1/tests/*.[ch] grub-core/tests/asn1/tests + for patch in \ 0001-libtasn1-disable-code-not-needed-in-grub.patch \ 0002-libtasn1-replace-strcat-with-strcpy-in-_asn1_str_cat.patch \ 0003-libtasn1-replace-strcat-with-_asn1_str_cat.patch \ 0004-libtasn1-adjust-the-header-paths-in-libtasn1.h.patch \ 0005-libtasn1-Use-grub_divmod64-for-division.patch \ - 0006-libtasn1-fix-the-potential-buffer-overrun.patch ; do + 0006-libtasn1-fix-the-potential-buffer-overrun.patch \ + 0007-asn1_test-include-asn1_test.h-only.patch \ + 0008-asn1_test-rename-the-main-functions-to-the-test-name.patch \ + 0009-asn1_test-return-either-0-or-1-to-reflect-the-result.patch \ + 0010-asn1_test-remove-verbose-and-the-unnecessary-printf.patch \ + 0011-asn1_test-print-the-error-messages-with-grub_printf.patch \ + 0012-asn1_test-use-the-grub-specific-functions-and-types.patch \ + 0013-asn1_test-exclude-a-testcase-when-long-and-int-are-t.patch ; do patch -p1 -i grub-core/lib/libtasn1-patches/$patch done
grub-core/Makefile.core.def +15 -0
@@ -2621,3 +2621,18 @@ module = { /* -Wno-type-limits comes from configure.ac of libtasn1 */ cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/lib/libtasn1-grub/lib -Wno-type-limits'; }; + +module = { + name = asn1_test; + common = tests/asn1/tests/CVE-2018-1000654.c; + common = tests/asn1/tests/object-id-decoding.c; + common = tests/asn1/tests/object-id-encoding.c; + common = tests/asn1/tests/octet-string.c; + common = tests/asn1/tests/reproducers.c; + common = tests/asn1/tests/Test_overflow.c; + common = tests/asn1/tests/Test_simple.c; + common = tests/asn1/tests/Test_strings.c; + common = tests/asn1/asn1_test.c; + cflags = '-Wno-uninitialized'; + cppflags = '-I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/tests/asn1/'; +};
grub-core/tests/asn1/asn1_test.c new file +50 -0
50 lines not on this page · raw message
grub-core/tests/asn1/asn1_test.h new file +45 -0
45 lines not on this page · raw message
grub-core/tests/lib/functional_test.c +1 -0
@@ -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 +11 -0
@@ -0,0 +1,11 @@ +#! @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.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 18/33 libtasn1: Add the documentation
In reply to Gary Lin via Grub-develDocument 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 | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
docs/grub-dev.texi +35 -0
@@ -506,6 +506,7 @@ to update it. * Gnulib:: * jsmn:: * minilzo:: +* libtasn1:: @end menu @node Gnulib
@@ -596,6 +597,40 @@ 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 xf libtasn1-4.19.0.tar.gz +rm -rf grub-core/lib/libtasn1/ +mkdir -p grub-core/lib/libtasn1/lib/ +mkdir -p grub-core/lib/libtasn1/tests/ +cp libtasn1-4.19.0/@lbracechar{}README.md,COPYING@rbracechar{} grub-core/lib/libtasn1/ +cp libtasn1-4.19.0/lib/@lbracechar{}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@rbracechar{} grub-core/lib/libtasn1/lib/ +cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1/ +cp libtasn1-4.19.0/tests/@lbracechar{}CVE-2018-1000654-1_asn1_tab.h,CVE-2018-1000654-2_asn1_tab.h,CVE-2018-1000654.c,object-id-decoding.c,object-id-encoding.c,octet-string.c,reproducers.c,Test_overflow.c,Test_simple.c,Test_strings.c@rbracechar{} grub-core/lib/libtasn1/tests +rm -rf libtasn1-4.19.0* +@end example + +After upgrading the library, it may be necessary to apply the patches in +@file{grub-core/lib/libtasn1-patches/} to adjust the code to be compatible with +GRUB. These patches were needed to use the current version of libtasn1. The +existing patches may not apply cleanly, apply at all, or even be needed for a +newer version of the library, and other patches may be needed due to changes in +the newer version. If existing patches need to be refreshed to apply cleanly, +please include updated patches as part of the a patch set sent to the list. +If new patches are needed or existing patches are not needed, also please send +additions or removals as part of any patch set upgrading libtasn1. + @node Debugging @chapter Debugging
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 19/33 key_protector: Add key protectors framework
In reply to Gary Lin via Grub-develFrom: 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 | 73 ++++++++++++++++++++++++++++++++++ include/grub/key_protector.h | 47 ++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 grub-core/disk/key_protector.c create mode 100644 include/grub/key_protector.h
grub-core/Makefile.am +1 -0
@@ -90,6 +90,7 @@ endif KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/parser.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/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
@@ -1282,6 +1282,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 +73 -0
73 lines not on this page · raw message
include/grub/key_protector.h new file +47 -0
47 lines not on this page · raw message
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 20/33 tss2: Add TPM2 buffer handling functions
In reply to Gary Lin via Grub-develAs the prepartion to support TPM2 Software Stack (TSS2), this commit implements the TPM2 buffer handling functions to pack data for the TPM2 commands and unpack the data from the response.
--- grub-core/lib/tss2/buffer.c | 147 +++++++++++++++++++++++++++++++ grub-core/lib/tss2/tss2_buffer.h | 64 ++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 grub-core/lib/tss2/buffer.c create mode 100644 grub-core/lib/tss2/tss2_buffer.h
grub-core/lib/tss2/buffer.c new file +147 -0
@@ -0,0 +1,147 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* 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/>. +*/ + +#include <grub/misc.h> + +#include <tss2_buffer.h> + +void grub_tpm2_buffer_init (grub_tpm2_buffer_t buffer) +{ +grub_memset (buffer->data,0 ,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 void *) &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 void *) &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 void *) &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/lib/tss2/tss2_buffer.h new file +64 -0
64 lines not on this page · raw message
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 21/33 tss2: Add TPM2 types and Marshal/Unmarshal functions
In reply to Gary Lin via Grub-develThis commit adds the necessary TPM2 types and structs as the preparation for the TPM2 Software Stack (TSS2) support. The Marshal/Unmarshal functions are also added to handle the data structure to be submitted to TPM2 commands and to be received from the response.
--- grub-core/lib/tss2/tss2_mu.c | 1174 +++++++++++++++++++++++++++++ grub-core/lib/tss2/tss2_mu.h | 397 ++++++++++ grub-core/lib/tss2/tss2_structs.h | 773 +++++++++++++++++++ grub-core/lib/tss2/tss2_types.h | 404 ++++++++++ 4 files changed, 2748 insertions(+) create mode 100644 grub-core/lib/tss2/tss2_mu.c create mode 100644 grub-core/lib/tss2/tss2_mu.h create mode 100644 grub-core/lib/tss2/tss2_structs.h create mode 100644 grub-core/lib/tss2/tss2_types.h
grub-core/lib/tss2/tss2_mu.c new file +1174 -0
@@ -0,0 +1,1174 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* 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/>. +*/ + +#include <grub/misc.h> + +#include <tss2_mu.h> + +void +grub_Tss2_MU_TPMS_AUTH_COMMAND_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_AUTH_COMMAND_t *authCommand) +{ +grub_uint32_t start; +grub_uint32_t tmp; + +grub_tpm2_buffer_pack_u32 (buffer,0 ); + start = buffer->size; + +grub_tpm2_buffer_pack_u32 (buffer, authCommand->sessionHandle); + +grub_tpm2_buffer_pack_u16 (buffer, authCommand->nonce.size); +grub_tpm2_buffer_pack (buffer, authCommand->nonce.buffer, authCommand->nonce.size); + +grub_tpm2_buffer_pack_u8 (buffer, *((const grub_uint8_t *) &authCommand->sessionAttributes)); + +grub_tpm2_buffer_pack_u16 (buffer, authCommand->hmac.size); +grub_tpm2_buffer_pack (buffer, authCommand->hmac.buffer, authCommand->hmac.size); + + tmp =grub_cpu_to_be32 (buffer->size - start); +grub_memcpy (&buffer->data[start -sizeof (grub_uint32_t)], &tmp,sizeof (tmp)); +} + +void +grub_Tss2_MU_TPM2B_Marshal (grub_tpm2_buffer_t buffer, +const grub_uint16_t size, +const grub_uint8_t *b) +{ +grub_uint16_t i; + +grub_tpm2_buffer_pack_u16 (buffer, size); + +for (i =0 ; i < size; i++) +grub_tpm2_buffer_pack_u8 (buffer, b[i]); +} + +void +grub_Tss2_MU_TPMU_SYM_KEY_BITS_Marshal (grub_tpm2_buffer_t buffer, +const TPMI_ALG_SYM_OBJECT_t algorithm, +const TPMU_SYM_KEY_BITS_t *p) +{ +switch (algorithm) + { +case TPM_ALG_AES : +case TPM_ALG_SM4 : +case TPM_ALG_CAMELLIA : +case TPM_ALG_XOR : +grub_tpm2_buffer_pack_u16 (buffer, *((const grub_uint16_t *) p)); +break ; +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMU_SYM_MODE_Marshal (grub_tpm2_buffer_t buffer, +const TPMI_ALG_SYM_OBJECT_t algorithm, +const TPMU_SYM_MODE_t *p) +{ +switch (algorithm) + { +case TPM_ALG_AES : +case TPM_ALG_SM4 : +case TPM_ALG_CAMELLIA : +grub_tpm2_buffer_pack_u16 (buffer, *((const grub_uint16_t *) p)); +break ; +case TPM_ALG_XOR : +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_SYM_DEF_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_SYM_DEF_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->algorithm); +grub_Tss2_MU_TPMU_SYM_KEY_BITS_Marshal (buffer, p->algorithm, &p->keyBits); +grub_Tss2_MU_TPMU_SYM_MODE_Marshal (buffer, p->algorithm, &p->mode); +} + +void +grub_Tss2_MU_TPMS_PCR_SELECTION_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_PCR_SELECTION_t *pcrSelection) +{ +grub_uint32_t i; + +grub_tpm2_buffer_pack_u16 (buffer, pcrSelection->hash); +grub_tpm2_buffer_pack_u8 (buffer, pcrSelection->sizeOfSelect); + +for (i =0 ; i < pcrSelection->sizeOfSelect; i++) +grub_tpm2_buffer_pack_u8 (buffer, pcrSelection->pcrSelect[i]); +} + +void +grub_Tss2_MU_TPML_PCR_SELECTION_Marshal (grub_tpm2_buffer_t buffer, +const TPML_PCR_SELECTION_t *pcrSelection) +{ +grub_uint32_t i; + +grub_tpm2_buffer_pack_u32 (buffer, pcrSelection->count); + +for (i =0 ; i < pcrSelection->count; i++) +grub_Tss2_MU_TPMS_PCR_SELECTION_Marshal (buffer, &pcrSelection->pcrSelections[i]); +} + +void +grub_Tss2_MU_TPMA_OBJECT_Marshal (grub_tpm2_buffer_t buffer, +const TPMA_OBJECT_t *p) +{ +grub_tpm2_buffer_pack_u32 (buffer, *((const grub_uint32_t *) p)); +} + +void +grub_Tss2_MU_TPMS_SCHEME_XOR_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_SCHEME_XOR_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->hashAlg); +grub_tpm2_buffer_pack_u16 (buffer, p->kdf); +} + +void +grub_Tss2_MU_TPMS_SCHEME_HMAC_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_SCHEME_HMAC_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->hashAlg); +} + +void +grub_Tss2_MU_TPMU_SCHEME_KEYEDHASH_Marshal (grub_tpm2_buffer_t buffer, +const TPMI_ALG_KEYEDHASH_SCHEME_t scheme, +const TPMU_SCHEME_KEYEDHASH_t *p) +{ +switch (scheme) + { +case TPM_ALG_HMAC : +grub_Tss2_MU_TPMS_SCHEME_HMAC_Marshal (buffer, &p->hmac); +break ; +case TPM_ALG_XOR : +grub_Tss2_MU_TPMS_SCHEME_XOR_Marshal (buffer, &p->exclusiveOr); +break ; +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_KEYEDHASH_SCHEME_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_KEYEDHASH_SCHEME_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->scheme); +grub_Tss2_MU_TPMU_SCHEME_KEYEDHASH_Marshal (buffer, p->scheme, &p->details); +} + +void +grub_Tss2_MU_TPMS_KEYEDHASH_PARMS_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_KEYEDHASH_PARMS_t *p) +{ +grub_Tss2_MU_TPMT_KEYEDHASH_SCHEME_Marshal (buffer, &p->scheme); +} + +void +grub_Tss2_MU_TPMT_SYM_DEF_OBJECT_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_SYM_DEF_OBJECT_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->algorithm); +grub_Tss2_MU_TPMU_SYM_KEY_BITS_Marshal (buffer, p->algorithm, &p->keyBits); +grub_Tss2_MU_TPMU_SYM_MODE_Marshal (buffer, p->algorithm, &p->mode); +} + +void +grub_Tss2_MU_TPMU_ASYM_SCHEME_Marshal (grub_tpm2_buffer_t buffer, +const TPMI_ALG_RSA_DECRYPT_t scheme, +const TPMU_ASYM_SCHEME_t *p __attribute__ ((unused))) +{ +switch (scheme) + { +case TPM_ALG_NULL : +break ; +default : +/* Unsupported */ + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_RSA_SCHEME_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_RSA_SCHEME_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->scheme); +grub_Tss2_MU_TPMU_ASYM_SCHEME_Marshal (buffer, p->scheme, &p->details); +} + +void +grub_Tss2_MU_TPMS_RSA_PARMS_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_RSA_PARMS_t *p) +{ +grub_Tss2_MU_TPMT_SYM_DEF_OBJECT_Marshal (buffer, &p->symmetric); +grub_Tss2_MU_TPMT_RSA_SCHEME_Marshal (buffer, &p->scheme); +grub_tpm2_buffer_pack_u16 (buffer, p->keyBits); +grub_tpm2_buffer_pack_u32 (buffer, p->exponent); +} + +void +grub_Tss2_MU_TPMS_SYMCIPHER_PARMS_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_SYMCIPHER_PARMS_t *p) +{ +grub_Tss2_MU_TPMT_SYM_DEF_OBJECT_Marshal (buffer, &p->sym); +} + +void +grub_Tss2_MU_TPMT_ECC_SCHEME_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_ECC_SCHEME_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->scheme); +grub_Tss2_MU_TPMU_ASYM_SCHEME_Marshal (buffer, p->scheme, &p->details); +} + +void +grub_Tss2_MU_TPMU_KDF_SCHEME_Marshal (grub_tpm2_buffer_t buffer, +const TPMI_ALG_KDF_t scheme, +const TPMU_KDF_SCHEME_t *p) +{ +switch (scheme) + { +case TPM_ALG_MGF1 : +grub_tpm2_buffer_pack_u16 (buffer, p->mgf1.hashAlg); +break ; +case TPM_ALG_KDF1_SP800_56A : +grub_tpm2_buffer_pack_u16 (buffer, p->kdf1_sp800_56a.hashAlg); +break ; +case TPM_ALG_KDF2 : +grub_tpm2_buffer_pack_u16 (buffer, p->kdf2.hashAlg); +break ; +case TPM_ALG_KDF1_SP800_108 : +grub_tpm2_buffer_pack_u16 (buffer, p->kdf1_sp800_108.hashAlg); +break ; +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_KDF_SCHEME_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_KDF_SCHEME_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->scheme); +grub_Tss2_MU_TPMU_KDF_SCHEME_Marshal (buffer, p->scheme, &p->details); +} + +void +grub_Tss2_MU_TPMS_ECC_PARMS_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_ECC_PARMS_t *p) +{ +grub_Tss2_MU_TPMT_SYM_DEF_OBJECT_Marshal (buffer, &p->symmetric); +grub_Tss2_MU_TPMT_ECC_SCHEME_Marshal (buffer, &p->scheme); +grub_tpm2_buffer_pack_u16 (buffer, p->curveID); +grub_Tss2_MU_TPMT_KDF_SCHEME_Marshal (buffer, &p->kdf); +} + +void +grub_Tss2_MU_TPMU_PUBLIC_PARMS_Marshal (grub_tpm2_buffer_t buffer, +const grub_uint32_t type, +const TPMU_PUBLIC_PARMS_t *p) +{ +switch (type) + { +case TPM_ALG_KEYEDHASH : +grub_Tss2_MU_TPMS_KEYEDHASH_PARMS_Marshal (buffer, &p->keyedHashDetail); +break ; +case TPM_ALG_SYMCIPHER : +grub_Tss2_MU_TPMS_SYMCIPHER_PARMS_Marshal (buffer, &p->symDetail); +break ; +case TPM_ALG_RSA : +grub_Tss2_MU_TPMS_RSA_PARMS_Marshal (buffer, &p->rsaDetail); +break ; +case TPM_ALG_ECC : +grub_Tss2_MU_TPMS_ECC_PARMS_Marshal (buffer, &p->eccDetail); +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMS_ECC_POINT_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_ECC_POINT_t *p) +{ +grub_Tss2_MU_TPM2B_Marshal (buffer, p->x.size, p->x.buffer); +grub_Tss2_MU_TPM2B_Marshal (buffer, p->y.size, p->y.buffer); +} + +void +grub_Tss2_MU_TPMU_PUBLIC_ID_Marshal (grub_tpm2_buffer_t buffer, +const TPMI_ALG_PUBLIC_t type, +const TPMU_PUBLIC_ID_t *p) +{ +switch (type) + { +case TPM_ALG_KEYEDHASH : +grub_Tss2_MU_TPM2B_Marshal (buffer, p->keyedHash.size, p->keyedHash.buffer); +break ; +case TPM_ALG_SYMCIPHER : +grub_Tss2_MU_TPM2B_Marshal (buffer, p->sym.size, p->sym.buffer); +break ; +case TPM_ALG_RSA : +grub_Tss2_MU_TPM2B_Marshal (buffer, p->rsa.size, p->rsa.buffer); +break ; +case TPM_ALG_ECC : +grub_Tss2_MU_TPMS_ECC_POINT_Marshal (buffer, &p->ecc); +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_PUBLIC_PARMS_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_PUBLIC_PARMS_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->type); +grub_Tss2_MU_TPMU_PUBLIC_PARMS_Marshal (buffer, p->type, &p->parameters); +} + +void +grub_Tss2_MU_TPMT_PUBLIC_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_PUBLIC_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->type); +grub_tpm2_buffer_pack_u16 (buffer, p->nameAlg); +grub_Tss2_MU_TPMA_OBJECT_Marshal (buffer, &p->objectAttributes); +grub_Tss2_MU_TPM2B_Marshal (buffer, p->authPolicy.size, p->authPolicy.buffer); +grub_Tss2_MU_TPMU_PUBLIC_PARMS_Marshal (buffer, p->type, &p->parameters); +grub_Tss2_MU_TPMU_PUBLIC_ID_Marshal (buffer, p->type, &p->unique); +} + +void +grub_Tss2_MU_TPM2B_PUBLIC_Marshal (grub_tpm2_buffer_t buffer, +const TPM2B_PUBLIC_t *p) +{ +grub_uint32_t start; +grub_uint16_t size; + +if (p) + { +grub_tpm2_buffer_pack_u16 (buffer, p->size); + + start = buffer->size; +grub_Tss2_MU_TPMT_PUBLIC_Marshal (buffer, &p->publicArea); + size =grub_cpu_to_be16 (buffer->size - start); +grub_memcpy (&buffer->data[start -sizeof (grub_uint16_t)], &size,sizeof (size)); + } +else +grub_tpm2_buffer_pack_u16 (buffer,0 ); +} + +void +grub_Tss2_MU_TPMS_SENSITIVE_CREATE_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_SENSITIVE_CREATE_t *p) +{ +grub_Tss2_MU_TPM2B_Marshal (buffer, p->userAuth.size, p->userAuth.buffer); +grub_Tss2_MU_TPM2B_Marshal (buffer, p->data.size, p->data.buffer); +} + +void +grub_Tss2_MU_TPMU_SENSITIVE_COMPOSITE_Marshal (grub_tpm2_buffer_t buffer, +const TPMI_ALG_PUBLIC_t type, +const TPMU_SENSITIVE_COMPOSITE_t *p) +{ +switch (type) + { +case TPM_ALG_RSA : +grub_Tss2_MU_TPM2B_Marshal (buffer, p->rsa.size, p->rsa.buffer); +break ; +case TPM_ALG_ECC : +grub_Tss2_MU_TPM2B_Marshal (buffer, p->ecc.size, p->ecc.buffer); +break ; +case TPM_ALG_KEYEDHASH : +grub_Tss2_MU_TPM2B_Marshal (buffer, p->bits.size, p->bits.buffer); +break ; +case TPM_ALG_SYMCIPHER : +grub_Tss2_MU_TPM2B_Marshal (buffer, p->sym.size, p->sym.buffer); +break ; +default : + buffer->error =1 ; + } +} + +void +grub_Tss2_MU_TPMT_SENSITIVE_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_SENSITIVE_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->sensitiveType); +grub_Tss2_MU_TPM2B_Marshal (buffer, p->authValue.size, p->authValue.buffer); +grub_Tss2_MU_TPM2B_Marshal (buffer, p->seedValue.size, p->seedValue.buffer); +grub_Tss2_MU_TPMU_SENSITIVE_COMPOSITE_Marshal (buffer, p->sensitiveType, &p->sensitive); +} + +void +grub_Tss2_MU_TPM2B_SENSITIVE_Marshal (grub_tpm2_buffer_t buffer, +const TPM2B_SENSITIVE_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->size); +grub_Tss2_MU_TPMT_SENSITIVE_Marshal (buffer, &p->sensitiveArea); +} + +void +grub_Tss2_MU_TPM2B_SENSITIVE_CREATE_Marshal (grub_tpm2_buffer_t buffer, +const TPM2B_SENSITIVE_CREATE_t *sensitiveCreate) +{ +grub_uint32_t start; +grub_uint16_t size; + +if (sensitiveCreate) + { +grub_tpm2_buffer_pack_u16 (buffer, sensitiveCreate->size); + start = buffer->size; +grub_Tss2_MU_TPMS_SENSITIVE_CREATE_Marshal (buffer, &sensitiveCreate->sensitive); + size =grub_cpu_to_be16 (buffer->size - start); + +grub_memcpy (&buffer->data[start -sizeof (grub_uint16_t)], &size,sizeof (size)); + } +else +grub_tpm2_buffer_pack_u16 (buffer,0 ); +} + +void +grub_Tss2_MU_TPMS_SIGNATURE_RSA_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_SIGNATURE_RSA_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->hash); +grub_Tss2_MU_TPM2B_Marshal (buffer, p->sig.size, p->sig.buffer); +} + +void +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_SIGNATURE_ECC_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->hash); +grub_Tss2_MU_TPM2B_Marshal (buffer, p->signatureR.size, p->signatureR.buffer); +grub_Tss2_MU_TPM2B_Marshal (buffer, p->signatureS.size, p->signatureS.buffer); +} + +void +grub_Tss2_MU_TPMU_HA_Marshal (grub_tpm2_buffer_t buffer, +const TPMI_ALG_HASH_t hashAlg, +const TPMU_HA_t *p) +{ +grub_uint16_t i; + +switch (hashAlg) + { +case TPM_ALG_SHA1 : +for (i =0 ; i <TPM_SHA1_DIGEST_SIZE ; i++) +grub_tpm2_buffer_pack_u8 (buffer, p->sha1[i]); +break ; +case TPM_ALG_SHA256 : +for (i =0 ; i <TPM_SHA256_DIGEST_SIZE ; i++) +grub_tpm2_buffer_pack_u8 (buffer, p->sha256[i]); +break ; +case TPM_ALG_SHA384 : +for (i =0 ; i <TPM_SHA384_DIGEST_SIZE ; i++) +grub_tpm2_buffer_pack_u8 (buffer, p->sha384[i]); +break ; +case TPM_ALG_SHA512 : +for (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_Tss2_MU_TPMT_HA_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_HA_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->hashAlg); +grub_Tss2_MU_TPMU_HA_Marshal (buffer, p->hashAlg, &p->digest); +} + +void +grub_Tss2_MU_TPMU_SIGNATURE_Marshal (grub_tpm2_buffer_t buffer, +const TPMI_ALG_SIG_SCHEME_t sigAlg, +const TPMU_SIGNATURE_t *p) +{ +switch (sigAlg) + { +case TPM_ALG_RSASSA : +grub_Tss2_MU_TPMS_SIGNATURE_RSA_Marshal (buffer, (TPMS_SIGNATURE_RSA_t *) &p->rsassa); +break ; +case TPM_ALG_RSAPSS : +grub_Tss2_MU_TPMS_SIGNATURE_RSA_Marshal (buffer, (TPMS_SIGNATURE_RSA_t *) &p->rsapss); +break ; +case TPM_ALG_ECDSA : +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Marshal (buffer, (TPMS_SIGNATURE_ECC_t *) &p->ecdsa); +break ; +case TPM_ALG_ECDAA : +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Marshal (buffer, (TPMS_SIGNATURE_ECC_t *) &p->ecdaa); +break ; +case TPM_ALG_SM2 : +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Marshal (buffer, (TPMS_SIGNATURE_ECC_t *) &p->sm2); +break ; +case TPM_ALG_ECSCHNORR : +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Marshal (buffer, (TPMS_SIGNATURE_ECC_t *) &p->ecschnorr); +break ; +case TPM_ALG_HMAC : +grub_Tss2_MU_TPMT_HA_Marshal (buffer, &p->hmac); +break ; +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_SIGNATURE_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_SIGNATURE_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->sigAlg); +grub_Tss2_MU_TPMU_SIGNATURE_Marshal (buffer, p->sigAlg, &p->signature); +} + +void +grub_Tss2_MU_TPMT_TK_VERIFIED_Marshal (grub_tpm2_buffer_t buffer, +const TPMT_TK_VERIFIED_t *p) +{ +grub_tpm2_buffer_pack_u16 (buffer, p->tag); +grub_tpm2_buffer_pack_u32 (buffer, p->hierarchy); +grub_Tss2_MU_TPM2B_Marshal (buffer, p->digest.size, p->digest.buffer); +} + +static void +__Tss2_MU_TPM2B_BUFFER_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_t *p,grub_uint16_t bound) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->size); + +if (p->size > bound) + { + buffer->error =1 ; +return ; + } + +grub_tpm2_buffer_unpack (buffer, &p->buffer, p->size); +} + +#define TPM2B_BUFFER_UNMARSHAL (buffer, type, data) \ + __Tss2_MU_TPM2B_BUFFER_Unmarshal(buffer, (TPM2B_t *)data, sizeof(type) - sizeof(grub_uint16_t)) + +void +grub_Tss2_MU_TPMS_AUTH_RESPONSE_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_AUTH_RESPONSE_t *p) +{ +grub_uint8_t tmp; +grub_uint32_t tmp32; + +grub_tpm2_buffer_unpack_u16 (buffer, &p->nonce.size); + +if (p->nonce.size) +grub_tpm2_buffer_unpack (buffer, &p->nonce.buffer, p->nonce.size); + +grub_tpm2_buffer_unpack_u8 (buffer, &tmp); + tmp32 = tmp; +grub_memcpy (&p->sessionAttributes, &tmp32,sizeof (grub_uint32_t)); + +grub_tpm2_buffer_unpack_u16 (buffer, &p->hmac.size); + +if (p->hmac.size) +grub_tpm2_buffer_unpack (buffer, &p->hmac.buffer, p->hmac.size); +} + +void +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_DIGEST_t *digest) +{ +TPM2B_BUFFER_UNMARSHAL (buffer, TPM2B_DIGEST_t, digest); +} + +void +grub_Tss2_MU_TPM2B_NONCE_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_NONCE_t *nonce) +{ +TPM2B_BUFFER_UNMARSHAL (buffer, TPM2B_NONCE_t, nonce); +} + +void +grub_Tss2_MU_TPM2B_DATA_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_DATA_t *data) +{ +TPM2B_BUFFER_UNMARSHAL (buffer, TPM2B_DATA_t, data); +} + +void +grub_Tss2_MU_TPMS_CREATION_DATA_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_CREATION_DATA_t *data) +{ +grub_Tss2_MU_TPML_PCR_SELECTION_Unmarshal (buffer, &data->pcrSelect); +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (buffer, &data->pcrDigest); +grub_tpm2_buffer_unpack_u8 (buffer, (grub_uint8_t *)&data->locality); +grub_tpm2_buffer_unpack_u16 (buffer, &data->parentNameAlg); +grub_Tss2_MU_TPM2B_NAME_Unmarshal (buffer, &data->parentName); +grub_Tss2_MU_TPM2B_NAME_Unmarshal (buffer, &data->parentQualifiedName); +grub_Tss2_MU_TPM2B_DATA_Unmarshal (buffer, &data->outsideInfo); +} + +void +grub_Tss2_MU_TPM2B_CREATION_DATA_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_CREATION_DATA_t *data) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &data->size); +grub_Tss2_MU_TPMS_CREATION_DATA_Unmarshal (buffer, &data->creationData); +} + +void +grub_Tss2_MU_TPM2B_PRIVATE_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_PRIVATE_t *private) +{ +TPM2B_BUFFER_UNMARSHAL (buffer, TPM2B_PRIVATE_t, private); +} + +void +grub_Tss2_MU_TPM2B_SENSITIVE_DATA_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_SENSITIVE_DATA_t *data) +{ +TPM2B_BUFFER_UNMARSHAL (buffer, TPM2B_SENSITIVE_DATA_t, data); +} + +void +grub_Tss2_MU_TPM2B_PUBLIC_KEY_RSA_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_PUBLIC_KEY_RSA_t *rsa) +{ +TPM2B_BUFFER_UNMARSHAL (buffer, TPM2B_PUBLIC_KEY_RSA_t, rsa); +} + +void +grub_Tss2_MU_TPM2B_ECC_PARAMETER_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_ECC_PARAMETER_t *param) +{ +TPM2B_BUFFER_UNMARSHAL (buffer, TPM2B_ECC_PARAMETER_t, param); +} + +void +grub_Tss2_MU_TPMA_OBJECT_Unmarshal (grub_tpm2_buffer_t buffer, +TPMA_OBJECT_t *p) +{ +grub_tpm2_buffer_unpack_u32 (buffer, (grub_uint32_t *) p); +} + +void +grub_Tss2_MU_TPMS_SCHEME_HMAC_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_SCHEME_HMAC_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->hashAlg); +} + +void +grub_Tss2_MU_TPMS_SCHEME_XOR_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_SCHEME_XOR_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->hashAlg); +grub_tpm2_buffer_unpack_u16 (buffer, &p->kdf); +} + +void +grub_Tss2_MU_TPMU_SCHEME_KEYEDHASH_Unmarshal (grub_tpm2_buffer_t buffer, +TPMI_ALG_KEYEDHASH_SCHEME_t scheme, +TPMU_SCHEME_KEYEDHASH_t *p) +{ +switch (scheme) + { +case TPM_ALG_HMAC : +grub_Tss2_MU_TPMS_SCHEME_HMAC_Unmarshal (buffer, &p->hmac); +break ; +case TPM_ALG_XOR : +grub_Tss2_MU_TPMS_SCHEME_XOR_Unmarshal (buffer, &p->exclusiveOr); +break ; +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_KEYEDHASH_SCHEME_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_KEYEDHASH_SCHEME_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->scheme); +grub_Tss2_MU_TPMU_SCHEME_KEYEDHASH_Unmarshal (buffer, p->scheme, &p->details); +} + +void +grub_Tss2_MU_TPMS_KEYEDHASH_PARMS_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_KEYEDHASH_PARMS_t *p) +{ +grub_Tss2_MU_TPMT_KEYEDHASH_SCHEME_Unmarshal (buffer, &p->scheme); +} + +void +grub_Tss2_MU_TPMU_SYM_KEY_BITS_Unmarshal (grub_tpm2_buffer_t buffer, +TPMI_ALG_SYM_OBJECT_t algorithm, +TPMU_SYM_KEY_BITS_t *p) +{ +switch (algorithm) + { +case TPM_ALG_AES : +case TPM_ALG_SM4 : +case TPM_ALG_CAMELLIA : +case TPM_ALG_XOR : +grub_tpm2_buffer_unpack_u16 (buffer, (grub_uint16_t *) p); +break ; +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMU_SYM_MODE_Unmarshal (grub_tpm2_buffer_t buffer, +TPMI_ALG_SYM_OBJECT_t algorithm, +TPMU_SYM_MODE_t *p) +{ +switch (algorithm) + { +case TPM_ALG_AES : +case TPM_ALG_SM4 : +case TPM_ALG_CAMELLIA : +grub_tpm2_buffer_unpack_u16 (buffer, (grub_uint16_t *) p); +break ; +case TPM_ALG_XOR : +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_SYM_DEF_OBJECT_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_SYM_DEF_OBJECT_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->algorithm); +grub_Tss2_MU_TPMU_SYM_KEY_BITS_Unmarshal (buffer, p->algorithm, &p->keyBits); +grub_Tss2_MU_TPMU_SYM_MODE_Unmarshal (buffer, p->algorithm, &p->mode); +} + +void +grub_Tss2_MU_TPMS_SYMCIPHER_PARMS_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_SYMCIPHER_PARMS_t *p) +{ +grub_Tss2_MU_TPMT_SYM_DEF_OBJECT_Unmarshal (buffer, &p->sym); +} + +void +grub_Tss2_MU_TPMU_ASYM_SCHEME_Unmarshal (grub_tpm2_buffer_t buffer, +TPMI_ALG_RSA_DECRYPT_t scheme, +TPMU_ASYM_SCHEME_t *p __attribute__((unused))) +{ +switch (scheme) + { +case TPM_ALG_NULL : +break ; +default : +/* Unsupported */ + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_RSA_SCHEME_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_RSA_SCHEME_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->scheme); +grub_Tss2_MU_TPMU_ASYM_SCHEME_Unmarshal (buffer, p->scheme, &p->details); +} + +void +grub_Tss2_MU_TPMS_RSA_PARMS_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_RSA_PARMS_t *p) +{ +grub_Tss2_MU_TPMT_SYM_DEF_OBJECT_Unmarshal (buffer, &p->symmetric); +grub_Tss2_MU_TPMT_RSA_SCHEME_Unmarshal (buffer, &p->scheme); +grub_tpm2_buffer_unpack_u16 (buffer, &p->keyBits); +grub_tpm2_buffer_unpack_u32 (buffer, &p->exponent); +} + +void +grub_Tss2_MU_TPMT_ECC_SCHEME_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_ECC_SCHEME_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->scheme); +grub_Tss2_MU_TPMU_ASYM_SCHEME_Unmarshal (buffer, p->scheme, &p->details); +} + +void +grub_Tss2_MU_TPMU_KDF_SCHEME_Unmarshal (grub_tpm2_buffer_t buffer, +TPMI_ALG_KDF_t scheme, +TPMU_KDF_SCHEME_t *p) +{ +switch (scheme) + { +case TPM_ALG_MGF1 : +grub_tpm2_buffer_unpack_u16 (buffer, &p->mgf1.hashAlg); +break ; +case TPM_ALG_KDF1_SP800_56A : +grub_tpm2_buffer_unpack_u16 (buffer, &p->kdf1_sp800_56a.hashAlg); +break ; +case TPM_ALG_KDF2 : +grub_tpm2_buffer_unpack_u16 (buffer, &p->kdf2.hashAlg); +break ; +case TPM_ALG_KDF1_SP800_108 : +grub_tpm2_buffer_unpack_u16 (buffer, &p->kdf1_sp800_108.hashAlg); +break ; +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_KDF_SCHEME_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_KDF_SCHEME_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->scheme); +grub_Tss2_MU_TPMU_KDF_SCHEME_Unmarshal (buffer, p->scheme, &p->details); +} + +void +grub_Tss2_MU_TPMS_ECC_PARMS_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_ECC_PARMS_t *p) +{ +grub_Tss2_MU_TPMT_SYM_DEF_OBJECT_Unmarshal (buffer, &p->symmetric); +grub_Tss2_MU_TPMT_ECC_SCHEME_Unmarshal (buffer, &p->scheme ); +grub_tpm2_buffer_unpack_u16 (buffer, &p->curveID); +grub_Tss2_MU_TPMT_KDF_SCHEME_Unmarshal (buffer, &p->kdf); +} + +void +grub_Tss2_MU_TPMU_PUBLIC_PARMS_Unmarshal (grub_tpm2_buffer_t buffer, +grub_uint32_t type, +TPMU_PUBLIC_PARMS_t *p) +{ +switch (type) + { +case TPM_ALG_KEYEDHASH : +grub_Tss2_MU_TPMS_KEYEDHASH_PARMS_Unmarshal (buffer, &p->keyedHashDetail); +break ; +case TPM_ALG_SYMCIPHER : +grub_Tss2_MU_TPMS_SYMCIPHER_PARMS_Unmarshal (buffer, &p->symDetail); +break ; +case TPM_ALG_RSA : +grub_Tss2_MU_TPMS_RSA_PARMS_Unmarshal (buffer, &p->rsaDetail); +break ; +case TPM_ALG_ECC : +grub_Tss2_MU_TPMS_ECC_PARMS_Unmarshal (buffer, &p->eccDetail); +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMS_ECC_POINT_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_ECC_POINT_t *p) +{ +grub_Tss2_MU_TPM2B_ECC_PARAMETER_Unmarshal (buffer, &p->x); +grub_Tss2_MU_TPM2B_ECC_PARAMETER_Unmarshal (buffer, &p->y); +} + +void +grub_Tss2_MU_TPMU_PUBLIC_ID_Unmarshal (grub_tpm2_buffer_t buffer, +TPMI_ALG_PUBLIC_t type, +TPMU_PUBLIC_ID_t *p) +{ +switch (type) + { +case TPM_ALG_KEYEDHASH : +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (buffer, &p->keyedHash); +break ; +case TPM_ALG_SYMCIPHER : +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (buffer, &p->sym); +break ; +case TPM_ALG_RSA : +grub_Tss2_MU_TPM2B_PUBLIC_KEY_RSA_Unmarshal (buffer, &p->rsa); +break ; +case TPM_ALG_ECC : +grub_Tss2_MU_TPMS_ECC_POINT_Unmarshal (buffer, &p->ecc); +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_PUBLIC_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_PUBLIC_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->type); +grub_tpm2_buffer_unpack_u16 (buffer, &p->nameAlg); +grub_Tss2_MU_TPMA_OBJECT_Unmarshal (buffer, &p->objectAttributes); +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (buffer, &p->authPolicy); +grub_Tss2_MU_TPMU_PUBLIC_PARMS_Unmarshal (buffer, p->type, &p->parameters); +grub_Tss2_MU_TPMU_PUBLIC_ID_Unmarshal (buffer, p->type, &p->unique); +} + +void +grub_Tss2_MU_TPM2B_PUBLIC_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_PUBLIC_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->size); +grub_Tss2_MU_TPMT_PUBLIC_Unmarshal (buffer, &p->publicArea); +} + +void +grub_Tss2_MU_TPMS_NV_PUBLIC_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_NV_PUBLIC_t *p) +{ +grub_tpm2_buffer_unpack_u32 (buffer, &p->nvIndex); +grub_tpm2_buffer_unpack_u16 (buffer, &p->nameAlg); +grub_tpm2_buffer_unpack_u32 (buffer, &p->attributes); +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (buffer, &p->authPolicy); +grub_tpm2_buffer_unpack_u16 (buffer, &p->dataSize); +} + +void +grub_Tss2_MU_TPM2B_NV_PUBLIC_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_NV_PUBLIC_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->size); +grub_Tss2_MU_TPMS_NV_PUBLIC_Unmarshal (buffer, &p->nvPublic); +} + +void +grub_Tss2_MU_TPM2B_NAME_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_NAME_t *n) +{ +TPM2B_BUFFER_UNMARSHAL (buffer, TPM2B_NAME_t, n); +} + +void +grub_Tss2_MU_TPMS_TAGGED_PROPERTY_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_TAGGED_PROPERTY_t *property) +{ +grub_tpm2_buffer_unpack_u32 (buffer, &property->property); +grub_tpm2_buffer_unpack_u32 (buffer, &property->value); +} + +void +grub_Tss2_MU_TPMT_TK_CREATION_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_TK_CREATION_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->tag); +grub_tpm2_buffer_unpack_u32 (buffer, &p->hierarchy); +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (buffer, &p->digest); +} + +void +grub_Tss2_MU_TPMT_TK_HASHCHECK_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_TK_HASHCHECK_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->tag); +grub_tpm2_buffer_unpack_u32 (buffer, &p->hierarchy); +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (buffer, &p->digest); +} + +void +grub_Tss2_MU_TPMT_TK_VERIFIED_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_TK_VERIFIED_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->tag); +grub_tpm2_buffer_unpack_u32 (buffer, &p->hierarchy); +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (buffer, &p->digest); +} + +void +grub_Tss2_MU_TPMS_PCR_SELECTION_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_PCR_SELECTION_t *pcrSelection) +{ +grub_uint32_t i; + +grub_tpm2_buffer_unpack_u16 (buffer, &pcrSelection->hash); +grub_tpm2_buffer_unpack_u8 (buffer, &pcrSelection->sizeOfSelect); + +if (pcrSelection->sizeOfSelect >TPM_PCR_SELECT_MAX ) + { + buffer->error =1 ; +return ; + } + +for (i =0 ; i < pcrSelection->sizeOfSelect; i++) +grub_tpm2_buffer_unpack_u8 (buffer, &pcrSelection->pcrSelect[i]); +} + +void +grub_Tss2_MU_TPML_PCR_SELECTION_Unmarshal (grub_tpm2_buffer_t buffer, +TPML_PCR_SELECTION_t *pcrSelection) +{ +grub_uint32_t i; + +grub_tpm2_buffer_unpack_u32 (buffer, &pcrSelection->count); + +if (pcrSelection->count >TPM_NUM_PCR_BANKS ) + { + buffer->error =1 ; +return ; + } + +for (i =0 ; i < pcrSelection->count; i++) +grub_Tss2_MU_TPMS_PCR_SELECTION_Unmarshal (buffer, &pcrSelection->pcrSelections[i]); +} + +void +grub_Tss2_MU_TPML_DIGEST_Unmarshal (grub_tpm2_buffer_t buffer, +TPML_DIGEST_t *digest) +{ +grub_uint32_t i; + +grub_tpm2_buffer_unpack_u32 (buffer, &digest->count); + +if (digest->count >8 ) + { + buffer->error =1 ; +return ; + } + +for (i =0 ; i < digest->count; i++) +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (buffer, &digest->digests[i]); +} + +void +grub_Tss2_MU_TPMS_SIGNATURE_RSA_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_SIGNATURE_RSA_t *rsa) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &rsa->hash); +grub_Tss2_MU_TPM2B_PUBLIC_KEY_RSA_Unmarshal (buffer, &rsa->sig); +} + +void +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_SIGNATURE_ECC_t *ecc) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &ecc->hash); +grub_Tss2_MU_TPM2B_ECC_PARAMETER_Unmarshal (buffer, &ecc->signatureR); +grub_Tss2_MU_TPM2B_ECC_PARAMETER_Unmarshal (buffer, &ecc->signatureS); +} + +void +grub_Tss2_MU_TPMU_HA_Unmarshal (grub_tpm2_buffer_t buffer, +TPMI_ALG_HASH_t hashAlg, +TPMU_HA_t *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_Tss2_MU_TPMT_HA_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_HA_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->hashAlg); +grub_Tss2_MU_TPMU_HA_Unmarshal (buffer, p->hashAlg, &p->digest); +} + +void +grub_Tss2_MU_TPMU_SIGNATURE_Unmarshal (grub_tpm2_buffer_t buffer, +TPMI_ALG_SIG_SCHEME_t sigAlg, +TPMU_SIGNATURE_t *p) +{ +switch (sigAlg) + { +case TPM_ALG_RSASSA : +grub_Tss2_MU_TPMS_SIGNATURE_RSA_Unmarshal (buffer, (TPMS_SIGNATURE_RSA_t *)&p->rsassa); +break ; +case TPM_ALG_RSAPSS : +grub_Tss2_MU_TPMS_SIGNATURE_RSA_Unmarshal (buffer, (TPMS_SIGNATURE_RSA_t *)&p->rsapss); +break ; +case TPM_ALG_ECDSA : +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Unmarshal (buffer, (TPMS_SIGNATURE_ECC_t *)&p->ecdsa); +break ; +case TPM_ALG_ECDAA : +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Unmarshal (buffer, (TPMS_SIGNATURE_ECC_t *)&p->ecdaa); +break ; +case TPM_ALG_SM2 : +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Unmarshal (buffer, (TPMS_SIGNATURE_ECC_t *)&p->sm2); +break ; +case TPM_ALG_ECSCHNORR : +grub_Tss2_MU_TPMS_SIGNATURE_ECC_Unmarshal (buffer, (TPMS_SIGNATURE_ECC_t *)&p->ecschnorr); +break ; +case TPM_ALG_HMAC : +grub_Tss2_MU_TPMT_HA_Unmarshal (buffer, &p->hmac); +break ; +case TPM_ALG_NULL : +break ; +default : + buffer->error =1 ; +break ; + } +} + +void +grub_Tss2_MU_TPMT_SIGNATURE_Unmarshal (grub_tpm2_buffer_t buffer, +TPMT_SIGNATURE_t *p) +{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->sigAlg); +grub_Tss2_MU_TPMU_SIGNATURE_Unmarshal (buffer, p->sigAlg, &p->signature); +}
grub-core/lib/tss2/tss2_mu.h new file +397 -0
397 lines not on this page · raw message
grub-core/lib/tss2/tss2_structs.h new file +773 -0
773 lines not on this page · raw message
grub-core/lib/tss2/tss2_types.h new file +404 -0
404 lines not on this page · raw message
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 22/33 tss2: Add TPM2 Software Stack (TSS2) support
In reply to Gary Lin via Grub-develA Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
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 implementations of various grub_tpm2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions:
* grub_tpm2_create()
* grub_tpm2_createprimary()
* grub_tpm2_evictcontrol()
* grub_tpm2_flushcontext()
* grub_tpm2_load()
* grub_tpm2_pcr_read()
* grub_tpm2_policygetdigest()
* grub_tpm2_policypcr()
* grub_tpm2_readpublic()
* grub_tpm2_startauthsession()
* grub_tpm2_unseal()
* grub_tpm2_loadexternal()
* grub_tpm2_hash()
* grub_tpm2_verifysignature()
* grub_tpm2_policyauthorize()
* grub_tpm2_testparms()
--- grub-core/Makefile.core.def | 11 + grub-core/lib/efi/tcg2.c | 143 +++++ grub-core/lib/tss2/tcg2.h | 35 ++ grub-core/lib/tss2/tpm2_cmd.c | 1043 +++++++++++++++++++++++++++++++++ grub-core/lib/tss2/tpm2_cmd.h | 157 +++++ grub-core/lib/tss2/tss2.c | 21 + 6 files changed, 1410 insertions(+) create mode 100644 grub-core/lib/efi/tcg2.c create mode 100644 grub-core/lib/tss2/tcg2.h create mode 100644 grub-core/lib/tss2/tpm2_cmd.c create mode 100644 grub-core/lib/tss2/tpm2_cmd.h create mode 100644 grub-core/lib/tss2/tss2.c
grub-core/Makefile.core.def +11 -0
@@ -2567,6 +2567,17 @@ module = { enable = efi; }; +module = { + name = tss2; + common = lib/tss2/buffer.c; + common = lib/tss2/tss2_mu.c; + common = lib/tss2/tpm2_cmd.c; + common = lib/tss2/tss2.c; + efi = lib/efi/tcg2.c; + enable = efi; + cppflags = '-I$(srcdir)/lib/tss2'; +}; + module = { name = tr; common = commands/tr.c;
grub-core/lib/efi/tcg2.c new file +143 -0
143 lines not on this page · raw message
grub-core/lib/tss2/tcg2.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* 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_TPM2_TCG2_HEADER +#define GRUB_TPM2_TCG2_HEADER 1 + +#include <grub/err.h> +#include <grub/types.h> + +extern grub_err_t +grub_tcg2_get_max_output_size (grub_size_t *size); + +extern grub_err_t +grub_tcg2_submit_command (grub_size_t input_size, +grub_uint8_t *input, +grub_size_t output_size, +grub_uint8_t *output); + +#endif /* ! GRUB_TPM2_TCG2_HEADER */
grub-core/lib/tss2/tpm2_cmd.c new file +1043 -0
1,043 lines not on this page · raw message
grub-core/lib/tss2/tpm2_cmd.h new file +157 -0
157 lines not on this page · raw message
grub-core/lib/tss2/tss2.c new file +21 -0
@@ -0,0 +1,21 @@ +/* +* 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/>. +*/ + +#include <grub/dl.h> + +GRUB_MOD_LICENSE ("GPLv3+" );
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 23/33 key_protector: Add TPM2 Key Protector
In reply to Gary Lin via Grub-develFrom: 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 description [4] EXPLICIT UTF8String OPTIONAL,
rsaParent [5] EXPLICIT BOOLEAN 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/grub/sealed-1.key cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/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/grub/sealed-1.tpm cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub/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.
This commit only supports one policy command: TPM2_PolicyPCR. The command set will be extended to support advanced features, such as authorized policy, in the later commits.
--- grub-core/Makefile.core.def | 11 + grub-core/commands/tpm2_key_protector/args.c | 127 ++ .../commands/tpm2_key_protector/module.c | 1153 +++++++++++++++++ grub-core/commands/tpm2_key_protector/tpm2.h | 36 + .../commands/tpm2_key_protector/tpm2_args.h | 49 + .../commands/tpm2_key_protector/tpm2key.asn | 49 + .../commands/tpm2_key_protector/tpm2key.c | 499 +++++++ .../commands/tpm2_key_protector/tpm2key.h | 87 ++ .../tpm2_key_protector/tpm2key_asn1_tab.c | 63 + 9 files changed, 2074 insertions(+) create mode 100644 grub-core/commands/tpm2_key_protector/args.c create mode 100644 grub-core/commands/tpm2_key_protector/module.c create mode 100644 grub-core/commands/tpm2_key_protector/tpm2.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2_args.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.asn create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.c create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key_asn1_tab.c
grub-core/Makefile.core.def +11 -0
@@ -2578,6 +2578,17 @@ module = { cppflags = '-I$(srcdir)/lib/tss2'; }; +module = { + name = tpm2_key_protector; + common = commands/tpm2_key_protector/args.c; + common = commands/tpm2_key_protector/module.c; + common = commands/tpm2_key_protector/tpm2key.c; + common = commands/tpm2_key_protector/tpm2key_asn1_tab.c; + /* The plaform support of tpm2_key_protector depends on the tcg2 implementation in tss2. */ + enable = efi; + cppflags = '-I$(srcdir)/lib/tss2 -I$(srcdir)/lib/libtasn1-grub'; +}; + module = { name = tr; common = commands/tr.c;
grub-core/commands/tpm2_key_protector/args.c new file +127 -0
127 lines not on this page · raw message
grub-core/commands/tpm2_key_protector/module.c new file +1153 -0
1,153 lines not on this page · raw message
grub-core/commands/tpm2_key_protector/tpm2.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* 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_TPM2_TPM2_HEADER +#define GRUB_TPM2_TPM2_HEADER 1 + +#include <tss2_types.h> +#include <tss2_structs.h> +#include <tpm2_cmd.h> + +/* Well-Known Windows SRK handle */ +#define TPM2_SRK_HANDLE 0x81000001 + +struct tpm2_sealed_key { +TPM2B_PUBLIC_t public; +TPM2B_PRIVATE_t private; +}; +typedef struct tpm2_sealed_key tpm2_sealed_key_t ; + +#endif /* ! GRUB_TPM2_TPM2_HEADER */
grub-core/commands/tpm2_key_protector/tpm2_args.h new file +49 -0
49 lines not on this page · raw message
grub-core/commands/tpm2_key_protector/tpm2key.asn new file +49 -0
49 lines not on this page · raw message
grub-core/commands/tpm2_key_protector/tpm2key.c new file +499 -0
499 lines not on this page · raw message
grub-core/commands/tpm2_key_protector/tpm2key.h new file +87 -0
87 lines not on this page · raw message
grub-core/commands/tpm2_key_protector/tpm2key_asn1_tab.c new file +63 -0
63 lines not on this page · raw message
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 23/33] key_protector: Add TPM2 Key Protector
Quoted 200 lines · Gary Lin
From: Hernan Gatta <[email protected]>
The TPM2 key protector is a module that enables the automatic retrieval of a fully-encrypted disk's unlocking key from a TPM 2.0.
The theory of operation is such that the module accepts various arguments, most of which are optional and therefore possess reasonable defaults. One of these arguments is the keyfile/tpm2key parameter, which is mandatory. There are two supported key formats:
1. Raw Sealed Key (--keyfile)
When sealing a key withTPM2_Create, the public portion of the sealed key is stored inTPM2B_PUBLIC, and the private portion is inTPM2B_PRIVATE. The raw sealed key glues the fully marshalledTPM2B_PUBLICandTPM2B_PRIVATEinto 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 OPTIONALdescription [4] EXPLICIT UTF8String OPTIONAL,
rsaParent [5] EXPLICIT BOOLEAN 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 bytpm2_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/grub/sealed-1.key cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub/sealed-2.key --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2Or, to unseal the TPM 2.0 Key file:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub/sealed-1.tpm cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub/sealed-2.tpm --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2If 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.
This commit only supports one policy command:
TPM2_PolicyPCR. The command set will be extended to support advanced features, such as authorized policy, in the later commits.Cc Stefan Berger <[email protected]>Cc James Bottomley <[email protected]>Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>--- grub-core/Makefile.core.def | 11 + grub-core/commands/tpm2_key_protector/args.c | 127 ++ .../commands/tpm2_key_protector/module.c | 1153 +++++++++++++++++ grub-core/commands/tpm2_key_protector/tpm2.h | 36 + .../commands/tpm2_key_protector/tpm2_args.h | 49 + .../commands/tpm2_key_protector/tpm2key.asn | 49 + .../commands/tpm2_key_protector/tpm2key.c | 499 +++++++ .../commands/tpm2_key_protector/tpm2key.h | 87 ++ .../tpm2_key_protector/tpm2key_asn1_tab.c | 63 + 9 files changed, 2074 insertions(+) create mode 100644 grub-core/commands/tpm2_key_protector/args.c create mode 100644 grub-core/commands/tpm2_key_protector/module.c create mode 100644 grub-core/commands/tpm2_key_protector/tpm2.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2_args.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.asn create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.c create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key_asn1_tab.cgrub-core/Makefile.core.def +11
-0@@ -2578,6 +2578,17 @@ module = { cppflags = '-I$(srcdir)/lib/tss2'; }; +module = { + name = tpm2_key_protector; + common = commands/tpm2_key_protector/args.c; + common = commands/tpm2_key_protector/module.c; + common = commands/tpm2_key_protector/tpm2key.c; + common = commands/tpm2_key_protector/tpm2key_asn1_tab.c; + /* The plaform support of tpm2_key_protector depends on the tcg2 implementation in tss2. */ + enable = efi; + cppflags = '-I$(srcdir)/lib/tss2 -I$(srcdir)/lib/libtasn1-grub'; +}; + module = { name = tr; common = commands/tr.c;grub-core/commands/tpm2_key_protector/args.c new file +34
-0@@ -0,0 +1,127 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* 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/>. +*/ + +#include <grub/err.h> +#include <grub/mm.h> +#include <grub/misc.h> + +#include "tpm2_args.h" + +grub_err_t +grub_tpm2_protector_parse_pcrs (char *value,grub_uint8_t *pcrs, +grub_uint8_t *pcr_count) +{ +char *current_pcr = value; +char *next_pcr; +const char *pcr_end; +grub_uint64_t pcr; +grub_uint8_t i;
grub-core/commands/tpm2_key_protector/args.c line 44
Quoted 10 lines
grub-core/commands/tpm2_key_protector/args.c new file +10
-0+ +if (grub_strlen (value) ==0 ) +return GRUB_ERR_BAD_ARGUMENT ; + + *pcr_count =0 ; +for (i =0 ; i <TPM_MAX_PCRS ; i++) + { + next_pcr =grub_strchr (current_pcr,',' ); +if (next_pcr == current_pcr) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Empty entry in PCR list" );
All grub_error() messages should start with lowercase. Of course there are exceptions, e.g. abbreviations. Additionally, you can mark simple error messages like this one for translation using N_() macro. Please fix this kind of issues in all patches.
grub-core/commands/tpm2_key_protector/args.c line 50
Quoted 6 lines
grub-core/commands/tpm2_key_protector/args.c new file +6
-0+if (next_pcr !=NULL ) + *next_pcr ='\0' ; + + pcr =grub_strtoul (current_pcr, &pcr_end,10 ); +if (*current_pcr =='\0' || *pcr_end !='\0' ) +return grub_error (GRUB_ERR_BAD_NUMBER ,"Entry '%s' in PCR list is not a number" , current_pcr);
Ditto...
grub-core/commands/tpm2_key_protector/args.c line 53
Quoted 3 lines
grub-core/commands/tpm2_key_protector/args.c new file +3
-0+ +if (pcr >TPM_MAX_PCRS ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"Entry %" PRIuGRUB_UINT64_T" in PCR list is too large to be a PCR number, PCR numbers range from 0 to %u" , pcr,TPM_MAX_PCRS );
Ditto...
FYI, PRIuGRUB_UINT64_T et consortes do not work with N_() macro. Sadly you have to cast variables to something long enough.
grub-core/commands/tpm2_key_protector/args.c line 63
Quoted 10 lines
grub-core/commands/tpm2_key_protector/args.c new file +10
-0+ + pcrs[i] = (grub_uint8_t ) pcr; + ++(*pcr_count); + +if (next_pcr ==NULL ) +break ; + + current_pcr = next_pcr +1 ; +if (*current_pcr =='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Trailing comma at the end of PCR list" );
Ditto...
grub-core/commands/tpm2_key_protector/args.c line 67
Quoted 4 lines
grub-core/commands/tpm2_key_protector/args.c new file +4
-0+ } + +if (i ==TPM_MAX_PCRS ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"Too many PCRs in PCR list, the maximum number of PCRs is %u" ,TPM_MAX_PCRS );
Ditto...
Quoted 3 lines
grub-core/commands/tpm2_key_protector/args.c new file +3
-0+ +return GRUB_ERR_NONE ; +}
[...]
Quoted 43 lines
grub-core/commands/tpm2_key_protector/tpm2_args.h new file +37
-0@@ -0,0 +1,49 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* 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_TPM2_INTERNAL_ARGS_HEADER +#define GRUB_TPM2_INTERNAL_ARGS_HEADER 1 + +#include <grub/err.h> + +#include "tpm2.h" + +struct grub_srk_type +{ +TPMI_ALG_PUBLIC_t type; +union { +TPM_KEY_BITS_t rsa_bits; +TPM_ECC_CURVE_t ecc_curve; + } detail; +}; +typedef struct grub_srk_type grub_srk_type_t ; + +grub_err_t
grub-core/commands/tpm2_key_protector/tpm2_args.h line 47
Quoted 10 lines
grub-core/commands/tpm2_key_protector/tpm2_args.h new file +10
-0+grub_tpm2_protector_parse_pcrs (char *value, grub_uint8_t *pcrs, grub_uint8_t *pcr_count); + +grub_err_t +grub_tpm2_protector_parse_asymmetric (const char *value,grub_srk_type_t *srk_type); + +grub_err_t +grub_tpm2_protector_parse_bank (const char *value,TPM_ALG_ID_t *bank); + +grub_err_t +grub_tpm2_protector_parse_tpm_handle (const char *value,TPM_HANDLE_t *handle);
Please prefix all variables and functions declarations with extern everywhere including other patches in this series.
If you fix all these minor issues feel free to add to this patch
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 23/33] key_protector: Add TPM2 Key Protector
Quoted 206 lines · Daniel Kiper
Quoted 205 lines · Gary Lin
From: Hernan Gatta <[email protected]>
The TPM2 key protector is a module that enables the automatic retrieval of a fully-encrypted disk's unlocking key from a TPM 2.0.
The theory of operation is such that the module accepts various arguments, most of which are optional and therefore possess reasonable defaults. One of these arguments is the keyfile/tpm2key parameter, which is mandatory. There are two supported key formats:
1. Raw Sealed Key (--keyfile)
When sealing a key withTPM2_Create, the public portion of the sealed key is stored inTPM2B_PUBLIC, and the private portion is inTPM2B_PRIVATE. The raw sealed key glues the fully marshalledTPM2B_PUBLICandTPM2B_PRIVATEinto 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 OPTIONALdescription [4] EXPLICIT UTF8String OPTIONAL,
rsaParent [5] EXPLICIT BOOLEAN 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 bytpm2_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/grub/sealed-1.key cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub/sealed-2.key --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2Or, to unseal the TPM 2.0 Key file:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub/sealed-1.tpm cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub/sealed-2.tpm --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2If 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.
This commit only supports one policy command:
TPM2_PolicyPCR. The command set will be extended to support advanced features, such as authorized policy, in the later commits.Cc Stefan Berger <[email protected]>Cc James Bottomley <[email protected]>Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>--- grub-core/Makefile.core.def | 11 + grub-core/commands/tpm2_key_protector/args.c | 127 ++ .../commands/tpm2_key_protector/module.c | 1153 +++++++++++++++++ grub-core/commands/tpm2_key_protector/tpm2.h | 36 + .../commands/tpm2_key_protector/tpm2_args.h | 49 + .../commands/tpm2_key_protector/tpm2key.asn | 49 + .../commands/tpm2_key_protector/tpm2key.c | 499 +++++++ .../commands/tpm2_key_protector/tpm2key.h | 87 ++ .../tpm2_key_protector/tpm2key_asn1_tab.c | 63 + 9 files changed, 2074 insertions(+) create mode 100644 grub-core/commands/tpm2_key_protector/args.c create mode 100644 grub-core/commands/tpm2_key_protector/module.c create mode 100644 grub-core/commands/tpm2_key_protector/tpm2.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2_args.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.asn create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.c create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key.h create mode 100644 grub-core/commands/tpm2_key_protector/tpm2key_asn1_tab.cgrub-core/Makefile.core.def +11
-0@@ -2578,6 +2578,17 @@ module = { cppflags = '-I$(srcdir)/lib/tss2'; }; +module = { + name = tpm2_key_protector; + common = commands/tpm2_key_protector/args.c; + common = commands/tpm2_key_protector/module.c; + common = commands/tpm2_key_protector/tpm2key.c; + common = commands/tpm2_key_protector/tpm2key_asn1_tab.c; + /* The plaform support of tpm2_key_protector depends on the tcg2 implementation in tss2. */ + enable = efi; + cppflags = '-I$(srcdir)/lib/tss2 -I$(srcdir)/lib/libtasn1-grub'; +}; + module = { name = tr; common = commands/tr.c;grub-core/commands/tpm2_key_protector/args.c new file +39
-0@@ -0,0 +1,127 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* 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/>. +*/ + +#include <grub/err.h> +#include <grub/mm.h> +#include <grub/misc.h> + +#include "tpm2_args.h" + +grub_err_t +grub_tpm2_protector_parse_pcrs (char *value,grub_uint8_t *pcrs, +grub_uint8_t *pcr_count) +{ +char *current_pcr = value; +char *next_pcr; +const char *pcr_end; +grub_uint64_t pcr; +grub_uint8_t i; + +if (grub_strlen (value) ==0 ) +return GRUB_ERR_BAD_ARGUMENT ; + + *pcr_count =0 ;
Quoted 8 lines
grub-core/commands/tpm2_key_protector/args.c line 44
Quoted 5 lines
grub-core/commands/tpm2_key_protector/args.c new file +5
-0+for (i =0 ; i <TPM_MAX_PCRS ; i++) + { + next_pcr =grub_strchr (current_pcr,',' ); +if (next_pcr == current_pcr) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Empty entry in PCR list" );All
grub_error()messages should start with lowercase. Of course there are exceptions, e.g. abbreviations.
Will fix them in the next version.
Quoted 4 lines
Quoted 1 line
Additionally, you can mark simple
error messages like this one for translation using
N_()macro. Please fix this kind of issues in all patches.
Okay, I'll check the grub_error messages and add N_() to the non-technical ones.
Quoted 9 lines
grub-core/commands/tpm2_key_protector/args.c line 50
Quoted 6 lines
grub-core/commands/tpm2_key_protector/args.c new file +6
-0+if (next_pcr !=NULL ) + *next_pcr ='\0' ; + + pcr =grub_strtoul (current_pcr, &pcr_end,10 ); +if (*current_pcr =='\0' || *pcr_end !='\0' ) +return grub_error (GRUB_ERR_BAD_NUMBER ,"Entry '%s' in PCR list is not a number" , current_pcr);Ditto...
Quoted 9 lines
grub-core/commands/tpm2_key_protector/args.c line 53
Quoted 3 lines
grub-core/commands/tpm2_key_protector/args.c new file +3
-0+ +if (pcr >TPM_MAX_PCRS ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"Entry %" PRIuGRUB_UINT64_T" in PCR list is too large to be a PCR number, PCR numbers range from 0 to %u" , pcr,TPM_MAX_PCRS );Ditto...
FYI,
PRIuGRUB_UINT64_Tet consortes do not work withN_()macro. Sadly you have to cast variables to something long enough.
Then I will cast them to "unsigned long long" and replace PRIuGRUB_UINT64_T with "llu".
Quoted 74 lines
grub-core/commands/tpm2_key_protector/args.c line 63
Quoted 10 lines
grub-core/commands/tpm2_key_protector/args.c new file +10
-0+ + pcrs[i] = (grub_uint8_t ) pcr; + ++(*pcr_count); + +if (next_pcr ==NULL ) +break ; + + current_pcr = next_pcr +1 ; +if (*current_pcr =='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Trailing comma at the end of PCR list" );Ditto...
grub-core/commands/tpm2_key_protector/args.c line 67
Quoted 4 lines
grub-core/commands/tpm2_key_protector/args.c new file +4
-0+ } + +if (i ==TPM_MAX_PCRS ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"Too many PCRs in PCR list, the maximum number of PCRs is %u" ,TPM_MAX_PCRS );Ditto...
Quoted 3 lines
grub-core/commands/tpm2_key_protector/args.c new file +3
-0+ +return GRUB_ERR_NONE ; +}[...]
Quoted 48 lines
grub-core/commands/tpm2_key_protector/tpm2_args.h new file +42
-0@@ -0,0 +1,49 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* 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_TPM2_INTERNAL_ARGS_HEADER +#define GRUB_TPM2_INTERNAL_ARGS_HEADER 1 + +#include <grub/err.h> + +#include "tpm2.h" + +struct grub_srk_type +{ +TPMI_ALG_PUBLIC_t type; +union { +TPM_KEY_BITS_t rsa_bits; +TPM_ECC_CURVE_t ecc_curve; + } detail; +}; +typedef struct grub_srk_type grub_srk_type_t ; + +grub_err_t +grub_tpm2_protector_parse_pcrs (char *value,grub_uint8_t *pcrs,grub_uint8_t *pcr_count); + +grub_err_t +grub_tpm2_protector_parse_asymmetric (const char *value,grub_srk_type_t *srk_type); +
Quoted 9 lines
grub-core/commands/tpm2_key_protector/tpm2_args.h line 47
Quoted 5 lines
grub-core/commands/tpm2_key_protector/tpm2_args.h new file +5
-0+grub_err_t +grub_tpm2_protector_parse_bank (const char *value,TPM_ALG_ID_t *bank); + +grub_err_t +grub_tpm2_protector_parse_tpm_handle (const char *value,TPM_HANDLE_t *handle);Please prefix all variables and functions declarations with extern everywhere including other patches in this series.
No problem. I'll check the functions and variables declared in the headers.
Quoted 3 lines
If you fix all these minor issues feel free to add to this patch
Reviewed-by Daniel Kiper <[email protected]>.
Thanks,
Gary Lin
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 24/33 cryptodisk: Support key protectors
In reply to Gary Lin via Grub-develFrom: 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 + docs/grub.texi | 12 +- grub-core/disk/cryptodisk.c | 243 ++++++++++++++++++++++++++---------- include/grub/cryptodisk.h | 16 +++ 4 files changed, 203 insertions(+), 69 deletions(-)
Makefile.util.def +1 -0
@@ -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;
docs/grub.texi +7 -5
@@ -6695,16 +6695,18 @@ Alias for @code{hashsum --hash crc32 arg @dots{}}. See command @command{hashsum} @node cryptomount @subsection cryptomount -@deffn Command cryptomount [ [@option{-p} password] | [@option{-k} keyfile [@option{-O} keyoffset] [@option{-S} keysize] ] ] [@option{-H} file] device|@option{-u} uuid|@option{-a}|@option{-b} +@deffn Command cryptomount [ [@option{-p} password] | [@option{-k} keyfile [@option{-O} keyoffset] [@option{-S} keysize] ] | [@option{-P} protector] ] [@option{-H} file] device|@option{-u} uuid|@option{-a}|@option{-b} Setup access to encrypted device. A passphrase will be requested interactively, if neither the @option{-p} nor @option{-k} options are given. The option @option{-p} can be used to supply a passphrase (useful for scripts). Alternatively the @option{-k} option can be used to supply a keyfile with options @option{-O} and @option{-S} optionally supplying the offset and size, -respectively, of the key data in the given key file. The @option{-H} options can -be used to supply cryptomount backends with an alternative header file (aka -detached header). Not all backends have headers nor support alternative header -files (currently only LUKS1 and LUKS2 support them). +respectively, of the key data in the given key file. Besides the keyfile, +the key can be stored in a key protector, and option @option{-P} configures +specific key protector, e.g. tpm2, to retrieve the key from. +The @option{-H} options can be used to supply cryptomount backends with an +alternative header file (aka detached header). Not all backends have headers +nor support alternative header files (currently only LUKS1 and LUKS2 support them). Argument @var{device} configures specific grub device (@pxref{Naming convention}); option @option{-u} @var{uuid} configures device with specified @var{uuid}; option @option{-a} configures all detected encrypted
grub-core/disk/cryptodisk.c +179 -64
343 lines not on this page · raw message
include/grub/cryptodisk.h +16 -0
@@ -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. +*/ +bool 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.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 25/33 util/grub-protect: Add new tool
In reply to Gary Lin via Grub-develFrom: 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 \
--tpm2-pcrs=0,2,4,7,9 \
--tpm2key \
--tpm2-keyfile=luks-key \
--tpm2-outfile=/boot/efi/efi/grub/sealed.tpmOr, to seal the key with the raw sealed key:
$ sudo grub-protect --action=add \
--protector=tpm2 \
--tpm2-pcrs=0,2,4,7,9 \
--tpm2-keyfile=luks-key \
--tpm2-outfile=/boot/efi/efi/grub/sealed.keyThen, in the boot script, for TPM 2.0 Key File:
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub/sealed.tpm cryptomount -u <SDB1_UUID> -P tpm2
Or, for the raw sealed key:
tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.key --pcrs=0,2,4,7,9 cryptomount -u <SDB1_UUID> -P tpm2
The benefit of using TPM 2.0 Key File is that the PCR set is already written in the key file, so there is no need to specify PCRs when invoking tpm2_key_protector_init.
--- .gitignore | 2 + Makefile.util.def | 26 + configure.ac | 30 + docs/man/grub-protect.h2m | 4 + util/grub-protect.c | 1394 +++++++++++++++++++++++++++++++++++++ 5 files changed, 1456 insertions(+) create mode 100644 docs/man/grub-protect.h2m create mode 100644 util/grub-protect.c
.gitignore +2 -0
@@ -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 +26 -0
@@ -208,6 +208,32 @@ program = { ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; }; +program = { + name = grub-protect; + mansection = 1; + + common = grub-core/kern/emu/argp_common.c; + common = grub-core/osdep/init.c; + common = grub-core/lib/tss2/buffer.c; + common = grub-core/lib/tss2/tss2_mu.c; + common = grub-core/lib/tss2/tpm2_cmd.c; + common = grub-core/commands/tpm2_key_protector/args.c; + common = grub-core/commands/tpm2_key_protector/tpm2key_asn1_tab.c; + common = util/grub-protect.c; + common = util/probe.c; + + cflags = '-I$(srcdir)/grub-core/lib/tss2 -I$(srcdir)/grub-core/commands/tpm2_key_protector'; + + 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
@@ -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])
@@ -2068,6 +2069,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])
@@ -2184,6 +2208,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
@@ -2311,6 +2336,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
docs/man/grub-protect.h2m new file +4 -0
@@ -0,0 +1,4 @@ +[NAME] +grub-protect \- protect a disk key with a key protector +[DESCRIPTION] +grub-protect helps to protect a disk encryption key with a specified key protector.
util/grub-protect.c new file +1394 -0
1,394 lines not on this page · raw message
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 25/33] util/grub-protect: Add new tool
Quoted 40 lines · Gary Lin
From: Hernan Gatta <[email protected]>
To utilize the key protectors framework, there must be a way to protect full-disk encryption keys in the first place. The grub-protect tool includes support for the TPM2 key protector but other protectors that require setup ahead of time can be supported in the future.
For the TPM2 key protector, the intended flow is for a user to have a LUKS 1 or LUKS 2-protected fully-encrypted disk. The user then creates a new LUKS key file, say by reading
/dev/urandominto 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=sha512To seal the key with TPM 2.0 Key File (recommended):
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpmOr, to seal the key with the raw sealed key:
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.key
util/grub-protect.c line 242
Quoted 10 lines
util/grub-protect.c new file +10
-0+static grub_err_t +protect_read_file (const char *filepath,void **buffer,size_t *buffer_size) +{ +grub_err_t err; +FILE *f; +long len; +void *buf; + + f =fopen (filepath,"rb" ); +if (f ==NULL )
An error message should be either printed here or the caller should print out an error. Otherwise no error message appears at all.
Quoted 30 lines
util/grub-protect.c new file +30
-0+return GRUB_ERR_FILE_NOT_FOUND ; + +if (fseek (f,0 ,SEEK_END )) + { + err =GRUB_ERR_FILE_READ_ERROR ; + goto exit1; + } + + len =ftell (f); +if (len <=0 ) + { + err =GRUB_ERR_FILE_READ_ERROR ; + goto exit1; + } + +rewind (f); + + buf =grub_malloc (len); +if (buf ==NULL ) + { + err =GRUB_ERR_OUT_OF_MEMORY ; + goto exit1; + } + +if (fread (buf, len,1 , f) !=1 ) + { + err =GRUB_ERR_FILE_READ_ERROR ; + goto exit2; + } +
util/grub-protect.c line 282
Quoted 10 lines
util/grub-protect.c new file +10
-0+ *buffer = buf; + *buffer_size = len; + + buf =NULL ; + err =GRUB_ERR_NONE ; + + exit2: +grub_free (buf); + + exit1:
probably also print an error message here that something with the file went wrong.
util/grub-protect.c line 295
Quoted 13 lines
util/grub-protect.c new file +13
-0+fclose (f); + +return err; +} + +static grub_err_t +protect_write_file (const char *filepath,void *buffer,size_t buffer_size) +{ +grub_err_t err; +FILE *f; + + f =fopen (filepath,"wb" ); +if (f ==NULL )
Here the caller prints out err. Good.
Quoted 25 lines
util/grub-protect.c new file +25
-0+return GRUB_ERR_FILE_NOT_FOUND ; + +if (fwrite (buffer, buffer_size,1 , f) !=1 ) + { + err =GRUB_ERR_WRITE_ERROR ; + goto exit; + } + + err =GRUB_ERR_NONE ; + + exit: +fclose (f); + +return err; +} + +grub_err_t +grub_tcg2_get_max_output_size (grub_size_t *size) +{ +if (size ==NULL ) +return GRUB_ERR_BAD_ARGUMENT ; + + *size =GRUB_TPM2_BUFFER_CAPACITY ; + +return GRUB_ERR_NONE ;
util/grub-protect.c line 330
Quoted 10 lines
util/grub-protect.c new file +10
-0+} + +grub_err_t +grub_tcg2_submit_command (grub_size_t input_size,grub_uint8_t *input, +grub_size_t output_size,grub_uint8_t *output) +{ +static const grub_size_t header_size =sizeof (grub_uint16_t) + + (2 *sizeof (grub_uint32_t)); + +if (write (protector_tpm2_fd, input, input_size) != input_size)
Also here ...
util/grub-protect.c line 333
Quoted 3 lines
util/grub-protect.c new file +3
-0+return GRUB_ERR_BAD_DEVICE ; + +if (read (protector_tpm2_fd, output, output_size) < header_size)
... and here since the (single) caller does not print an error.
util/grub-protect.c line 338
Quoted 5 lines
util/grub-protect.c new file +5
-0+return GRUB_ERR_BAD_DEVICE ; + +return GRUB_ERR_NONE ; +} +
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 25/33] util/grub-protect: Add new tool
Quoted 43 lines · Stefan Berger
Quoted 39 lines · Gary Lin
From: Hernan Gatta <[email protected]>
To utilize the key protectors framework, there must be a way to protect full-disk encryption keys in the first place. The grub-protect tool includes support for the TPM2 key protector but other protectors that require setup ahead of time can be supported in the future.
For the TPM2 key protector, the intended flow is for a user to have a LUKS 1 or LUKS 2-protected fully-encrypted disk. The user then creates a new LUKS key file, say by reading
/dev/urandominto 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=sha512To seal the key with TPM 2.0 Key File (recommended):
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpmOr, to seal the key with the raw sealed key:
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.key
Quoted 14 lines
util/grub-protect.c line 242
Quoted 10 lines
util/grub-protect.c new file +10
-0+static grub_err_t +protect_read_file (const char *filepath,void **buffer,size_t *buffer_size) +{ +grub_err_t err; +FILE *f; +long len; +void *buf; + + f =fopen (filepath,"rb" ); +if (f ==NULL )An error message should be either printed here or the caller should print out an error. Otherwise no error message appears at all.
Right, the user needs to know what goes wrong with file reading. I'll add messages for every error in this function.
Quoted 98 lines
Quoted 30 lines
util/grub-protect.c new file +30
-0+return GRUB_ERR_FILE_NOT_FOUND ; + +if (fseek (f,0 ,SEEK_END )) + { + err =GRUB_ERR_FILE_READ_ERROR ; + goto exit1; + } + + len =ftell (f); +if (len <=0 ) + { + err =GRUB_ERR_FILE_READ_ERROR ; + goto exit1; + } + +rewind (f); + + buf =grub_malloc (len); +if (buf ==NULL ) + { + err =GRUB_ERR_OUT_OF_MEMORY ; + goto exit1; + } + +if (fread (buf, len,1 , f) !=1 ) + { + err =GRUB_ERR_FILE_READ_ERROR ; + goto exit2; + } +util/grub-protect.c line 282
Quoted 10 lines
util/grub-protect.c new file +10
-0+ *buffer = buf; + *buffer_size = len; + + buf =NULL ; + err =GRUB_ERR_NONE ; + + exit2: +grub_free (buf); + + exit1:probably also print an error message here that something with the file went wrong.
util/grub-protect.c line 295
Quoted 13 lines
util/grub-protect.c new file +13
-0+fclose (f); + +return err; +} + +static grub_err_t +protect_write_file (const char *filepath,void *buffer,size_t buffer_size) +{ +grub_err_t err; +FILE *f; + + f =fopen (filepath,"wb" ); +if (f ==NULL )Here the caller prints out err. Good.
Quoted 25 lines
util/grub-protect.c new file +25
-0+return GRUB_ERR_FILE_NOT_FOUND ; + +if (fwrite (buffer, buffer_size,1 , f) !=1 ) + { + err =GRUB_ERR_WRITE_ERROR ; + goto exit; + } + + err =GRUB_ERR_NONE ; + + exit: +fclose (f); + +return err; +} + +grub_err_t +grub_tcg2_get_max_output_size (grub_size_t *size) +{ +if (size ==NULL ) +return GRUB_ERR_BAD_ARGUMENT ; + + *size =GRUB_TPM2_BUFFER_CAPACITY ; + +return GRUB_ERR_NONE ;util/grub-protect.c line 330
Quoted 10 lines
util/grub-protect.c new file +10
-0+} + +grub_err_t +grub_tcg2_submit_command (grub_size_t input_size,grub_uint8_t *input, +grub_size_t output_size,grub_uint8_t *output) +{ +static const grub_size_t header_size =sizeof (grub_uint16_t) + + (2 *sizeof (grub_uint32_t)); + +if (write (protector_tpm2_fd, input, input_size) != input_size)Also here ...
Quoted 6 lines
util/grub-protect.c line 333
Quoted 3 lines
util/grub-protect.c new file +3
-0+return GRUB_ERR_BAD_DEVICE ; + +if (read (protector_tpm2_fd, output, output_size) < header_size)... and here since the (single) caller does not print an error.
I'll add error messages for write()/read() here.
Thanks,
Gary Lin
Quoted 5 lines
util/grub-protect.c line 338
Quoted 5 lines
util/grub-protect.c new file +5
-0+return GRUB_ERR_BAD_DEVICE ; + +return GRUB_ERR_NONE ; +} +
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 25/33] util/grub-protect: Add new tool
In reply to Gary Lin via Grub-develQuoted 65 lines · Gary Lin
From: Hernan Gatta <[email protected]>
To utilize the key protectors framework, there must be a way to protect full-disk encryption keys in the first place. The grub-protect tool includes support for the TPM2 key protector but other protectors that require setup ahead of time can be supported in the future.
For the TPM2 key protector, the intended flow is for a user to have a LUKS 1 or LUKS 2-protected fully-encrypted disk. The user then creates a new LUKS key file, say by reading
/dev/urandominto 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=sha512To seal the key with TPM 2.0 Key File (recommended):
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpmOr, to seal the key with the raw sealed key:
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.keyThen, in the boot script, for TPM 2.0 Key File:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub/sealed.tpm cryptomount -u <SDB1_UUID> -P tpm2Or, for the raw sealed key:
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub/sealed.key --pcrs=0,2,4,7,9 cryptomount -u <SDB1_UUID> -P tpm2The benefit of using TPM 2.0 Key File is that the PCR set is already written in the key file, so there is no need to specify PCRs when invoking
tpm2_key_protector_init.Cc Stefan Berger <[email protected]>Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>--- .gitignore | 2 + Makefile.util.def | 26 + configure.ac | 30 + docs/man/grub-protect.h2m | 4 + util/grub-protect.c | 1394 +++++++++++++++++++++++++++++++++++++ 5 files changed, 1456 insertions(+) create mode 100644 docs/man/grub-protect.h2m create mode 100644 util/grub-protect.c
[...]
Quoted 31 lines
util/grub-protect.c new file +31
-0+static grub_err_t +protect_write_file (const char *filepath,void *buffer,size_t buffer_size) +{ +grub_err_t err; +FILE *f; + + f =fopen (filepath,"wb" ); +if (f ==NULL ) +return GRUB_ERR_FILE_NOT_FOUND ; + +if (fwrite (buffer, buffer_size,1 , f) !=1 ) + { + err =GRUB_ERR_WRITE_ERROR ; + goto exit; + } + + err =GRUB_ERR_NONE ; + + exit: +fclose (f); + +return err; +} + +grub_err_t +grub_tcg2_get_max_output_size (grub_size_t *size) +{ +if (size ==NULL ) +return GRUB_ERR_BAD_ARGUMENT ; + + *size =GRUB_TPM2_BUFFER_CAPACITY ;
util/grub-protect.c line 328
Quoted 10 lines
util/grub-protect.c new file +10
-0+ +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_tcg2_submit_command (grub_size_t input_size,grub_uint8_t *input, +grub_size_t output_size,grub_uint8_t *output) +{ +static const grub_size_t header_size =sizeof (grub_uint16_t) + + (2 *sizeof (grub_uint32_t));
AIUI this is somehow related to the TPM structs defined elsewhere. So, I think these structs should have defined header as a separate struct (probably preferred). Then you could use, e.g., sizeof (tpm_header) and be done. Or you can define a constant next to TPM structs as you do here. Of course it would be nice if the constant has relevant name and/or a comment...
Quoted 8 lines
util/grub-protect.c new file +8
-0+if (write (protector_tpm2_fd, input, input_size) != input_size) +return GRUB_ERR_BAD_DEVICE ; + +if (read (protector_tpm2_fd, output, output_size) < header_size) +return GRUB_ERR_BAD_DEVICE ; + +return GRUB_ERR_NONE ; +}
[...]
util/grub-protect.c line 661
Quoted 5 lines
util/grub-protect.c new file +5
-0+static grub_err_t +protect_tpm2_export_tpm2key (const protect_args_t *args, +tpm2_sealed_key_t *sealed_key) +{ +const char *sealed_key_oid ="2.23.133.10.1.5" ;
Please name this OID in a comment or define properly named constant.
Quoted 209 lines
util/grub-protect.c new file +209
-0+asn1_node asn1_def =NULL ; +asn1_node tpm2key =NULL ; +grub_uint32_t parent; +grub_uint32_t cmd_code; +struct grub_tpm2_buffer pol_buf; +TPML_PCR_SELECTION_t pcr_sel = { + .count =1 , + .pcrSelections = { + { + .hash = args->tpm2_bank, + .sizeOfSelect =3 , + .pcrSelect = {0 } + }, + } + }; +struct grub_tpm2_buffer pub_buf; +struct grub_tpm2_buffer priv_buf; +void *der_buf =NULL ; +int der_buf_size =0 ; +int i; +int ret; +grub_err_t err; + +for (i =0 ; i < args->tpm2_pcr_count; i++) +TPMS_PCR_SELECTION_SelectPCR (&pcr_sel.pcrSelections[0 ], args->tpm2_pcrs[i]); + +/* +* Prepare the parameters for TPM_CC_PolicyPCR: +* empty pcrDigest and the user selected PCRs +*/ +grub_tpm2_buffer_init (&pol_buf); +grub_tpm2_buffer_pack_u16 (&pol_buf,0 ); +grub_Tss2_MU_TPML_PCR_SELECTION_Marshal (&pol_buf, &pcr_sel); + +grub_tpm2_buffer_init (&pub_buf); +grub_Tss2_MU_TPM2B_PUBLIC_Marshal (&pub_buf, &sealed_key->public); +grub_tpm2_buffer_init (&priv_buf); +grub_Tss2_MU_TPM2B_Marshal (&priv_buf, sealed_key->private.size, + sealed_key->private.buffer); +if (pub_buf.error !=0 || priv_buf.error !=0 ) +return GRUB_ERR_BAD_ARGUMENT ; + + ret =asn1_array2tree (tpm2key_asn1_tab, &asn1_def,NULL ); +if (ret !=ASN1_SUCCESS ) +return GRUB_ERR_BAD_ARGUMENT ; + + ret =asn1_create_element (asn1_def,"TPM2KEY.TPMKey" , &tpm2key); +if (ret !=ASN1_SUCCESS ) +return GRUB_ERR_BAD_ARGUMENT ; + +/* Set 'type' to "sealed key" */ + ret =asn1_write_value (tpm2key,"type" , sealed_key_oid,1 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'type': 0x%u\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set 'emptyAuth' to TRUE */ + ret =asn1_write_value (tpm2key,"emptyAuth" ,"TRUE" ,1 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'emptyAuth': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set 'policy' */ + ret =asn1_write_value (tpm2key,"policy" ,"NEW" ,1 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'policy': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + cmd_code =grub_cpu_to_be32 (TPM_CC_PolicyPCR); + ret =asn1_write_value (tpm2key,"policy.?LAST.CommandCode" , &cmd_code, +sizeof (cmd_code)); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'policy CommandCode': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + ret =asn1_write_value (tpm2key,"policy.?LAST.CommandPolicy" , &pol_buf.data, + pol_buf.size); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'policy CommandPolicy': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Remove 'secret' */ + ret =asn1_write_value (tpm2key,"secret" ,NULL ,0 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to remove 'secret': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Remove 'authPolicy' */ + ret =asn1_write_value (tpm2key,"authPolicy" ,NULL ,0 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to remove 'authPolicy': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Remove 'description' */ + ret =asn1_write_value (tpm2key,"description" ,NULL ,0 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to remove 'description': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* +* Use the SRK handle as the parent handle if specified +* Otherwise, Use TPM_RH_OWNER as the default parent handle +*/ +if (args->tpm2_srk !=0 ) + parent =grub_cpu_to_be32 (args->tpm2_srk); +else + parent =grub_cpu_to_be32 (TPM_RH_OWNER ); + ret =asn1_write_value (tpm2key,"parent" , &parent,sizeof (parent)); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'parent': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* +* Set 'rsaParent' to TRUE if the RSA SRK is specified and the SRK +* handle is not persistent. Otherwise, remove 'rsaParent'. +*/ +if (args->tpm2_srk ==0 && args->srk_type.type ==TPM_ALG_RSA ) + ret =asn1_write_value (tpm2key,"rsaParent" ,"TRUE" ,1 ); +else + ret =asn1_write_value (tpm2key,"rsaParent" ,NULL ,0 ); + +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'rsaParent': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set the pubkey */ + ret =asn1_write_value (tpm2key,"pubkey" , pub_buf.data, pub_buf.size); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'pubkey': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set the privkey */ + ret =asn1_write_value (tpm2key,"privkey" , priv_buf.data, priv_buf.size); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'privkey': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Create the DER binary */ + der_buf_size =0 ; + ret =asn1_der_coding (tpm2key,"" ,NULL , &der_buf_size,NULL ); +if (ret !=ASN1_MEM_ERROR ) + { +fprintf (stderr,"Failed to get DER size: 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + + der_buf =grub_malloc (der_buf_size); +if (der_buf ==NULL ) + { +fprintf (stderr,"Failed to allocate memory for DER encoding\n" ); + err =GRUB_ERR_OUT_OF_MEMORY ; + goto error; + } + + ret =asn1_der_coding (tpm2key,"" , der_buf, &der_buf_size,NULL ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"DER coding error: 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + + err =protect_write_file (args->tpm2_outfile, der_buf, der_buf_size); +if (err !=GRUB_ERR_NONE ) +fprintf (stderr,"Could not write tpm2key file (Error: %u).\n" , errno); + + error: +grub_free (der_buf); + +if (tpm2key) +asn1_delete_structure (&tpm2key); + +return err; +}
[...]
Quoted 16 lines
util/grub-protect.c new file +16
-0+static grub_err_t +protect_tpm2_args_verify (protect_args_t *args) +{ +switch (args->action) + { +case PROTECT_ACTION_ADD : +if (args->args &PROTECT_ARG_TPM2_EVICT ) + { +fprintf (stderr,N_ ("--tpm2-evict is invalid when --action is 'add'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_keyfile ==NULL ) + { +fprintf (stderr,N_ ("--tpm2-keyfile must be specified.\n" )); +return GRUB_ERR_BAD_ARGUMENT ;
util/grub-protect.c line 1039
Quoted 10 lines
util/grub-protect.c new file +10
-0+ } + +if (args->tpm2_outfile ==NULL ) + { +fprintf (stderr,N_ ("--tpm2-outfile must be specified.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_device ==NULL ) + args->tpm2_device ="/dev/tpm0" ;
When you move this "if" to the beginning of function or its end then you can do this check once and drop one below...
Quoted 46 lines
util/grub-protect.c new file +46
-0+if (args->tpm2_pcr_count ==0 ) + { + args->tpm2_pcrs[0 ] =7 ; + args->tpm2_pcr_count =1 ; + } + +if (args->srk_type.type ==TPM_ALG_ERROR ) + { + args->srk_type.type =TPM_ALG_ECC ; + args->srk_type.detail.ecc_curve =TPM_ECC_NIST_P256 ; + } + +if (args->tpm2_bank ==TPM_ALG_ERROR ) + args->tpm2_bank =TPM_ALG_SHA256 ; + +break ; + +case PROTECT_ACTION_REMOVE : +if (args->args &PROTECT_ARG_TPM2_ASYMMETRIC ) + { +fprintf (stderr,N_ ("--tpm2-asymmetric is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_BANK ) + { +fprintf (stderr,N_ ("--tpm2-bank is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_KEYFILE ) + { +fprintf (stderr,N_ ("--tpm2-keyfile is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_OUTFILE ) + { +fprintf (stderr,N_ ("--tpm2-outfile is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_PCRS ) + { +fprintf (stderr,N_ ("--tpm2-pcrs is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ;
util/grub-protect.c line 1096
Quoted 10 lines
util/grub-protect.c new file +10
-0+ } + +if (args->tpm2_srk ==0 ) + { +fprintf (stderr,N_ ("--tpm2-srk is not specified when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_device ==NULL ) + args->tpm2_device ="/dev/tpm0" ;
... I mean from here...
Quoted 9 lines
util/grub-protect.c new file +9
-0+break ; + +default : +fprintf (stderr,N_ ("The TPM2 key protector only supports the following actions: add, remove.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +return GRUB_ERR_NONE ; +}
[...]
util/grub-protect.c line 1299
Quoted 11 lines
util/grub-protect.c new file +11
-0+static grub_err_t +protect_args_verify (protect_args_t *args) +{ +if (args->action ==PROTECT_ACTION_ERROR ) + { +fprintf (stderr,N_ ("--action is mandatory.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +/* At the moment, the only configurable key protector is the TPM2 one, so it +* is the only key protector supported by this tool. */
Wrong coding style for the comment...
Quoted 16 lines
util/grub-protect.c new file +16
-0+if (args->protector !=PROTECT_TYPE_TPM2 ) + { +fprintf (stderr,N_ ("--protector is mandatory and only 'tpm2' is currently supported.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +switch (args->protector) + { +case PROTECT_TYPE_TPM2 : +return protect_tpm2_args_verify (args); +default : +return GRUB_ERR_BAD_ARGUMENT ; + } + +return GRUB_ERR_NONE ; +}
[...]
util/grub-protect.c line 1371
Quoted 4 lines
util/grub-protect.c new file +4
-0+int +main (int argc,char *argv[]) +{ +grub_err_t err;
s/grub_err_t/int/
util/grub-protect.c line 1377
Quoted 6 lines
util/grub-protect.c new file +6
-0+protect_args_t args = {0 }; + +if (argp_parse (&protect_argp, argc, argv,0 ,0 , &args) !=0 ) + { +fprintf (stderr,N_ ("Could not parse arguments.\n" )); +return GRUB_ERR_BAD_ARGUMENT ;
You expose GRUB internals to the user space and mix types. There is no guarantee GRUB_ERR_BAD_ARGUMENT value will not change in the future. So, I think you should return EXIT_FAILURE here.
util/grub-protect.c line 1382
Quoted 5 lines
util/grub-protect.c new file +5
-0+ } + +protect_init (&argc, &argv); + + err =protect_args_verify (&args);
Ditto... The EXIT_SUCCESS and EXIT_FAILURE are your friends...
util/grub-protect.c line 1386
Quoted 4 lines
util/grub-protect.c new file +4
-0+if (err !=GRUB_ERR_NONE ) + goto exit; + + err =protect_dispatch (&args);
Ditto...
I did not check other patches but if you do the same thing elsewhere please fix it.
util/grub-protect.c line 1394
Quoted 8 lines
util/grub-protect.c new file +8
-0+if (err !=GRUB_ERR_NONE ) + goto exit; + + exit: +protect_fini (); + +return err; +}
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 25/33] util/grub-protect: Add new tool
Quoted 109 lines · Daniel Kiper
Quoted 65 lines · Gary Lin
From: Hernan Gatta <[email protected]>
To utilize the key protectors framework, there must be a way to protect full-disk encryption keys in the first place. The grub-protect tool includes support for the TPM2 key protector but other protectors that require setup ahead of time can be supported in the future.
For the TPM2 key protector, the intended flow is for a user to have a LUKS 1 or LUKS 2-protected fully-encrypted disk. The user then creates a new LUKS key file, say by reading
/dev/urandominto 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=sha512To seal the key with TPM 2.0 Key File (recommended):
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpmOr, to seal the key with the raw sealed key:
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.keyThen, in the boot script, for TPM 2.0 Key File:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub/sealed.tpm cryptomount -u <SDB1_UUID> -P tpm2Or, for the raw sealed key:
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub/sealed.key --pcrs=0,2,4,7,9 cryptomount -u <SDB1_UUID> -P tpm2The benefit of using TPM 2.0 Key File is that the PCR set is already written in the key file, so there is no need to specify PCRs when invoking
tpm2_key_protector_init.Cc Stefan Berger <[email protected]>Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>--- .gitignore | 2 + Makefile.util.def | 26 + configure.ac | 30 + docs/man/grub-protect.h2m | 4 + util/grub-protect.c | 1394 +++++++++++++++++++++++++++++++++++++ 5 files changed, 1456 insertions(+) create mode 100644 docs/man/grub-protect.h2m create mode 100644 util/grub-protect.c[...]
Quoted 40 lines
util/grub-protect.c new file +40
-0+static grub_err_t +protect_write_file (const char *filepath,void *buffer,size_t buffer_size) +{ +grub_err_t err; +FILE *f; + + f =fopen (filepath,"wb" ); +if (f ==NULL ) +return GRUB_ERR_FILE_NOT_FOUND ; + +if (fwrite (buffer, buffer_size,1 , f) !=1 ) + { + err =GRUB_ERR_WRITE_ERROR ; + goto exit; + } + + err =GRUB_ERR_NONE ; + + exit: +fclose (f); + +return err; +} + +grub_err_t +grub_tcg2_get_max_output_size (grub_size_t *size) +{ +if (size ==NULL ) +return GRUB_ERR_BAD_ARGUMENT ; + + *size =GRUB_TPM2_BUFFER_CAPACITY ; + +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_tcg2_submit_command (grub_size_t input_size,grub_uint8_t *input, +grub_size_t output_size,grub_uint8_t *output) +{ + static const grub_size_t header_size = sizeof (grub_uint16_t) +
Quoted 9 lines
util/grub-protect.c line 328
Quoted 1 line
util/grub-protect.c new file +1
-0+ (2 *sizeof (grub_uint32_t));AIUI this is somehow related to the TPM structs defined elsewhere. So, I think these structs should have defined header as a separate struct (probably preferred). Then you could use, e.g., sizeof (
tpm_header) and be done. Or you can define a constant next to TPM structs as you do here. Of course it would be nice if the constant has relevant name and/or a comment...
I'll define a TPM command header in tss2_structs.h like this:
struct TPM_COMMAND_HEADER
{
grub_uint16_t tag;
grub_uint32_t command_size;
TPM_CC_t command_code;
};
typedef struct TPM_COMMAND_HEADER TPM_COMMAND_HEADER_t;
This would make the code more comprehensive.
Quoted 11 lines
Quoted 8 lines
util/grub-protect.c new file +8
-0+if (write (protector_tpm2_fd, input, input_size) != input_size) +return GRUB_ERR_BAD_DEVICE ; + +if (read (protector_tpm2_fd, output, output_size) < header_size) +return GRUB_ERR_BAD_DEVICE ; + +return GRUB_ERR_NONE ; +}[...]
Quoted 8 lines
util/grub-protect.c line 661
Quoted 5 lines
util/grub-protect.c new file +5
-0+static grub_err_t +protect_tpm2_export_tpm2key (const protect_args_t *args, +tpm2_sealed_key_t *sealed_key) +{ +const char *sealed_key_oid ="2.23.133.10.1.5" ;Please name this OID in a comment or define properly named constant.
The OID is from the TPM 2.0 Key File spec. I'll add a '#define' for it and a proper comment.
Quoted 233 lines
Quoted 209 lines
util/grub-protect.c new file +209
-0+asn1_node asn1_def =NULL ; +asn1_node tpm2key =NULL ; +grub_uint32_t parent; +grub_uint32_t cmd_code; +struct grub_tpm2_buffer pol_buf; +TPML_PCR_SELECTION_t pcr_sel = { + .count =1 , + .pcrSelections = { + { + .hash = args->tpm2_bank, + .sizeOfSelect =3 , + .pcrSelect = {0 } + }, + } + }; +struct grub_tpm2_buffer pub_buf; +struct grub_tpm2_buffer priv_buf; +void *der_buf =NULL ; +int der_buf_size =0 ; +int i; +int ret; +grub_err_t err; + +for (i =0 ; i < args->tpm2_pcr_count; i++) +TPMS_PCR_SELECTION_SelectPCR (&pcr_sel.pcrSelections[0 ], args->tpm2_pcrs[i]); + +/* +* Prepare the parameters for TPM_CC_PolicyPCR: +* empty pcrDigest and the user selected PCRs +*/ +grub_tpm2_buffer_init (&pol_buf); +grub_tpm2_buffer_pack_u16 (&pol_buf,0 ); +grub_Tss2_MU_TPML_PCR_SELECTION_Marshal (&pol_buf, &pcr_sel); + +grub_tpm2_buffer_init (&pub_buf); +grub_Tss2_MU_TPM2B_PUBLIC_Marshal (&pub_buf, &sealed_key->public); +grub_tpm2_buffer_init (&priv_buf); +grub_Tss2_MU_TPM2B_Marshal (&priv_buf, sealed_key->private.size, + sealed_key->private.buffer); +if (pub_buf.error !=0 || priv_buf.error !=0 ) +return GRUB_ERR_BAD_ARGUMENT ; + + ret =asn1_array2tree (tpm2key_asn1_tab, &asn1_def,NULL ); +if (ret !=ASN1_SUCCESS ) +return GRUB_ERR_BAD_ARGUMENT ; + + ret =asn1_create_element (asn1_def,"TPM2KEY.TPMKey" , &tpm2key); +if (ret !=ASN1_SUCCESS ) +return GRUB_ERR_BAD_ARGUMENT ; + +/* Set 'type' to "sealed key" */ + ret =asn1_write_value (tpm2key,"type" , sealed_key_oid,1 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'type': 0x%u\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set 'emptyAuth' to TRUE */ + ret =asn1_write_value (tpm2key,"emptyAuth" ,"TRUE" ,1 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'emptyAuth': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set 'policy' */ + ret =asn1_write_value (tpm2key,"policy" ,"NEW" ,1 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'policy': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + cmd_code =grub_cpu_to_be32 (TPM_CC_PolicyPCR); + ret =asn1_write_value (tpm2key,"policy.?LAST.CommandCode" , &cmd_code, +sizeof (cmd_code)); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'policy CommandCode': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + ret =asn1_write_value (tpm2key,"policy.?LAST.CommandPolicy" , &pol_buf.data, + pol_buf.size); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'policy CommandPolicy': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Remove 'secret' */ + ret =asn1_write_value (tpm2key,"secret" ,NULL ,0 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to remove 'secret': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Remove 'authPolicy' */ + ret =asn1_write_value (tpm2key,"authPolicy" ,NULL ,0 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to remove 'authPolicy': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Remove 'description' */ + ret =asn1_write_value (tpm2key,"description" ,NULL ,0 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to remove 'description': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* +* Use the SRK handle as the parent handle if specified +* Otherwise, Use TPM_RH_OWNER as the default parent handle +*/ +if (args->tpm2_srk !=0 ) + parent =grub_cpu_to_be32 (args->tpm2_srk); +else + parent =grub_cpu_to_be32 (TPM_RH_OWNER ); + ret =asn1_write_value (tpm2key,"parent" , &parent,sizeof (parent)); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'parent': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* +* Set 'rsaParent' to TRUE if the RSA SRK is specified and the SRK +* handle is not persistent. Otherwise, remove 'rsaParent'. +*/ +if (args->tpm2_srk ==0 && args->srk_type.type ==TPM_ALG_RSA ) + ret =asn1_write_value (tpm2key,"rsaParent" ,"TRUE" ,1 ); +else + ret =asn1_write_value (tpm2key,"rsaParent" ,NULL ,0 ); + +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'rsaParent': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set the pubkey */ + ret =asn1_write_value (tpm2key,"pubkey" , pub_buf.data, pub_buf.size); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'pubkey': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set the privkey */ + ret =asn1_write_value (tpm2key,"privkey" , priv_buf.data, priv_buf.size); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'privkey': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Create the DER binary */ + der_buf_size =0 ; + ret =asn1_der_coding (tpm2key,"" ,NULL , &der_buf_size,NULL ); +if (ret !=ASN1_MEM_ERROR ) + { +fprintf (stderr,"Failed to get DER size: 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + + der_buf =grub_malloc (der_buf_size); +if (der_buf ==NULL ) + { +fprintf (stderr,"Failed to allocate memory for DER encoding\n" ); + err =GRUB_ERR_OUT_OF_MEMORY ; + goto error; + } + + ret =asn1_der_coding (tpm2key,"" , der_buf, &der_buf_size,NULL ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"DER coding error: 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + + err =protect_write_file (args->tpm2_outfile, der_buf, der_buf_size); +if (err !=GRUB_ERR_NONE ) +fprintf (stderr,"Could not write tpm2key file (Error: %u).\n" , errno); + + error: +grub_free (der_buf); + +if (tpm2key) +asn1_delete_structure (&tpm2key); + +return err; +}[...]
Quoted 21 lines
util/grub-protect.c new file +21
-0+static grub_err_t +protect_tpm2_args_verify (protect_args_t *args) +{ +switch (args->action) + { +case PROTECT_ACTION_ADD : +if (args->args &PROTECT_ARG_TPM2_EVICT ) + { +fprintf (stderr,N_ ("--tpm2-evict is invalid when --action is 'add'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_keyfile ==NULL ) + { +fprintf (stderr,N_ ("--tpm2-keyfile must be specified.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_outfile ==NULL ) + { +fprintf (stderr,N_ ("--tpm2-outfile must be specified.\n" ));
Quoted 9 lines
util/grub-protect.c line 1039
Quoted 5 lines
util/grub-protect.c new file +5
-0+return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_device ==NULL ) + args->tpm2_device ="/dev/tpm0" ;When you move this "if" to the beginning of function or its end then you can do this check once and drop one below...
Got it.
Quoted 71 lines
Quoted 46 lines
util/grub-protect.c new file +46
-0+if (args->tpm2_pcr_count ==0 ) + { + args->tpm2_pcrs[0 ] =7 ; + args->tpm2_pcr_count =1 ; + } + +if (args->srk_type.type ==TPM_ALG_ERROR ) + { + args->srk_type.type =TPM_ALG_ECC ; + args->srk_type.detail.ecc_curve =TPM_ECC_NIST_P256 ; + } + +if (args->tpm2_bank ==TPM_ALG_ERROR ) + args->tpm2_bank =TPM_ALG_SHA256 ; + +break ; + +case PROTECT_ACTION_REMOVE : +if (args->args &PROTECT_ARG_TPM2_ASYMMETRIC ) + { +fprintf (stderr,N_ ("--tpm2-asymmetric is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_BANK ) + { +fprintf (stderr,N_ ("--tpm2-bank is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_KEYFILE ) + { +fprintf (stderr,N_ ("--tpm2-keyfile is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_OUTFILE ) + { +fprintf (stderr,N_ ("--tpm2-outfile is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_PCRS ) + { +fprintf (stderr,N_ ("--tpm2-pcrs is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ;util/grub-protect.c line 1096
Quoted 10 lines
util/grub-protect.c new file +10
-0+ } + +if (args->tpm2_srk ==0 ) + { +fprintf (stderr,N_ ("--tpm2-srk is not specified when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_device ==NULL ) + args->tpm2_device ="/dev/tpm0" ;... I mean from here...
Quoted 9 lines
util/grub-protect.c new file +9
-0+break ; + +default : +fprintf (stderr,N_ ("The TPM2 key protector only supports the following actions: add, remove.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +return GRUB_ERR_NONE ; +}[...]
Quoted 14 lines
util/grub-protect.c line 1299
Quoted 11 lines
util/grub-protect.c new file +11
-0+static grub_err_t +protect_args_verify (protect_args_t *args) +{ +if (args->action ==PROTECT_ACTION_ERROR ) + { +fprintf (stderr,N_ ("--action is mandatory.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +/* At the moment, the only configurable key protector is the TPM2 one, so it +* is the only key protector supported by this tool. */ Wrong coding style for the comment...
Will fix it in the next version.
Quoted 33 lines
Quoted 16 lines
util/grub-protect.c new file +16
-0+if (args->protector !=PROTECT_TYPE_TPM2 ) + { +fprintf (stderr,N_ ("--protector is mandatory and only 'tpm2' is currently supported.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +switch (args->protector) + { +case PROTECT_TYPE_TPM2 : +return protect_tpm2_args_verify (args); +default : +return GRUB_ERR_BAD_ARGUMENT ; + } + +return GRUB_ERR_NONE ; +}[...]
util/grub-protect.c line 1371
Quoted 4 lines
util/grub-protect.c new file +4
-0+int +main (int argc,char *argv[]) +{ +grub_err_t err;s/grub_err_t/int/
util/grub-protect.c line 1377
Quoted 6 lines
util/grub-protect.c new file +6
-0+protect_args_t args = {0 }; + +if (argp_parse (&protect_argp, argc, argv,0 ,0 , &args) !=0 ) + { +fprintf (stderr,N_ ("Could not parse arguments.\n" )); +return GRUB_ERR_BAD_ARGUMENT ;
Quoted 4 lines
You expose GRUB internals to the user space and mix types. There is no guarantee
GRUB_ERR_BAD_ARGUMENTvalue will not change in the future. So, I think you should returnEXIT_FAILUREhere.
Will fix it in the next version.
Quoted 13 lines
util/grub-protect.c line 1382
Quoted 5 lines
util/grub-protect.c new file +5
-0+ } + +protect_init (&argc, &argv); + + err =protect_args_verify (&args);Ditto... The
EXIT_SUCCESSandEXIT_FAILUREare your friends...util/grub-protect.c line 1386
Quoted 4 lines
util/grub-protect.c new file +4
-0+if (err !=GRUB_ERR_NONE ) + goto exit; + + err =protect_dispatch (&args);
Quoted 5 lines
Ditto...
I did not check other patches but if you do the same thing elsewhere please fix it.
Will check other patches.
Quoted 8 lines
util/grub-protect.c line 1393
Quoted 8 lines
util/grub-protect.c new file +7
-0+if (err !=GRUB_ERR_NONE ) + goto exit; + + exit: +protect_fini (); + +return err;
I would like to handle 'err' like this:
if (err != GRUB_ERR_NONE)
return EXIT_FAILURE;
return EXIT_SUCCESS;
Gary Lin
Quoted 3 lines
Quoted 1 line
+}
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 25/33] util/grub-protect: Add new tool
Quoted 122 lines · Gary Lin
Quoted 109 lines · Daniel Kiper
Quoted 65 lines · Gary Lin
From: Hernan Gatta <[email protected]>
To utilize the key protectors framework, there must be a way to protect full-disk encryption keys in the first place. The grub-protect tool includes support for the TPM2 key protector but other protectors that require setup ahead of time can be supported in the future.
For the TPM2 key protector, the intended flow is for a user to have a LUKS 1 or LUKS 2-protected fully-encrypted disk. The user then creates a new LUKS key file, say by reading
/dev/urandominto 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=sha512To seal the key with TPM 2.0 Key File (recommended):
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpmOr, to seal the key with the raw sealed key:
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2-pcrs=0,2,4,7,9 \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.keyThen, in the boot script, for TPM 2.0 Key File:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub/sealed.tpm cryptomount -u <SDB1_UUID> -P tpm2Or, for the raw sealed key:
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub/sealed.key --pcrs=0,2,4,7,9 cryptomount -u <SDB1_UUID> -P tpm2The benefit of using TPM 2.0 Key File is that the PCR set is already written in the key file, so there is no need to specify PCRs when invoking
tpm2_key_protector_init.Cc Stefan Berger <[email protected]>Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>--- .gitignore | 2 + Makefile.util.def | 26 + configure.ac | 30 + docs/man/grub-protect.h2m | 4 + util/grub-protect.c | 1394 +++++++++++++++++++++++++++++++++++++ 5 files changed, 1456 insertions(+) create mode 100644 docs/man/grub-protect.h2m create mode 100644 util/grub-protect.c[...]
Quoted 40 lines
util/grub-protect.c new file +40
-0+static grub_err_t +protect_write_file (const char *filepath,void *buffer,size_t buffer_size) +{ +grub_err_t err; +FILE *f; + + f =fopen (filepath,"wb" ); +if (f ==NULL ) +return GRUB_ERR_FILE_NOT_FOUND ; + +if (fwrite (buffer, buffer_size,1 , f) !=1 ) + { + err =GRUB_ERR_WRITE_ERROR ; + goto exit; + } + + err =GRUB_ERR_NONE ; + + exit: +fclose (f); + +return err; +} + +grub_err_t +grub_tcg2_get_max_output_size (grub_size_t *size) +{ +if (size ==NULL ) +return GRUB_ERR_BAD_ARGUMENT ; + + *size =GRUB_TPM2_BUFFER_CAPACITY ; + +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_tcg2_submit_command (grub_size_t input_size,grub_uint8_t *input, +grub_size_t output_size,grub_uint8_t *output) +{ + static const grub_size_t header_size = sizeof (grub_uint16_t) +Quoted 9 lines
util/grub-protect.c line 328
Quoted 1 line
util/grub-protect.c new file +1
-0+ (2 *sizeof (grub_uint32_t));AIUI this is somehow related to the TPM structs defined elsewhere. So, I think these structs should have defined header as a separate struct (probably preferred). Then you could use, e.g., sizeof (
tpm_header) and be done. Or you can define a constant next to TPM structs as you do here. Of course it would be nice if the constant has relevant name and/or a comment...I'll define a TPM command header in
tss2_structs.hlike this:
Quoted 8 lines
struct
TPM_COMMAND_HEADER
{
grub_uint16_ttag;
grub_uint32_tcommand_size;
TPM_CC_tcommand_code;
};
typedef structTPM_COMMAND_HEADERTPM_COMMAND_HEADER_t;
Correction:
The header size check is for the response, so the struct is actually TPM_RESPONSE_HEADER:
struct TPM_RESPONSE_HEADER
{
grub_uint16_t tag;
grub_uint32_t response_size;
TPM_RC_t response_code;
};
typedef struct TPM_RESPONSE_HEADER TPM_RESPONSE_HEADER_t;
Gary Lin
Quoted 425 lines
This would make the code more comprehensive.
Quoted 11 lines
Quoted 8 lines
util/grub-protect.c new file +8
-0+if (write (protector_tpm2_fd, input, input_size) != input_size) +return GRUB_ERR_BAD_DEVICE ; + +if (read (protector_tpm2_fd, output, output_size) < header_size) +return GRUB_ERR_BAD_DEVICE ; + +return GRUB_ERR_NONE ; +}[...]
Quoted 8 lines
util/grub-protect.c line 661
Quoted 5 lines
util/grub-protect.c new file +5
-0+static grub_err_t +protect_tpm2_export_tpm2key (const protect_args_t *args, +tpm2_sealed_key_t *sealed_key) +{ +const char *sealed_key_oid ="2.23.133.10.1.5" ;Please name this OID in a comment or define properly named constant.
The OID is from the TPM 2.0 Key File spec. I'll add a '#define' for it and a proper comment.
Quoted 233 lines
Quoted 209 lines
util/grub-protect.c new file +209
-0+asn1_node asn1_def =NULL ; +asn1_node tpm2key =NULL ; +grub_uint32_t parent; +grub_uint32_t cmd_code; +struct grub_tpm2_buffer pol_buf; +TPML_PCR_SELECTION_t pcr_sel = { + .count =1 , + .pcrSelections = { + { + .hash = args->tpm2_bank, + .sizeOfSelect =3 , + .pcrSelect = {0 } + }, + } + }; +struct grub_tpm2_buffer pub_buf; +struct grub_tpm2_buffer priv_buf; +void *der_buf =NULL ; +int der_buf_size =0 ; +int i; +int ret; +grub_err_t err; + +for (i =0 ; i < args->tpm2_pcr_count; i++) +TPMS_PCR_SELECTION_SelectPCR (&pcr_sel.pcrSelections[0 ], args->tpm2_pcrs[i]); + +/* +* Prepare the parameters for TPM_CC_PolicyPCR: +* empty pcrDigest and the user selected PCRs +*/ +grub_tpm2_buffer_init (&pol_buf); +grub_tpm2_buffer_pack_u16 (&pol_buf,0 ); +grub_Tss2_MU_TPML_PCR_SELECTION_Marshal (&pol_buf, &pcr_sel); + +grub_tpm2_buffer_init (&pub_buf); +grub_Tss2_MU_TPM2B_PUBLIC_Marshal (&pub_buf, &sealed_key->public); +grub_tpm2_buffer_init (&priv_buf); +grub_Tss2_MU_TPM2B_Marshal (&priv_buf, sealed_key->private.size, + sealed_key->private.buffer); +if (pub_buf.error !=0 || priv_buf.error !=0 ) +return GRUB_ERR_BAD_ARGUMENT ; + + ret =asn1_array2tree (tpm2key_asn1_tab, &asn1_def,NULL ); +if (ret !=ASN1_SUCCESS ) +return GRUB_ERR_BAD_ARGUMENT ; + + ret =asn1_create_element (asn1_def,"TPM2KEY.TPMKey" , &tpm2key); +if (ret !=ASN1_SUCCESS ) +return GRUB_ERR_BAD_ARGUMENT ; + +/* Set 'type' to "sealed key" */ + ret =asn1_write_value (tpm2key,"type" , sealed_key_oid,1 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'type': 0x%u\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set 'emptyAuth' to TRUE */ + ret =asn1_write_value (tpm2key,"emptyAuth" ,"TRUE" ,1 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'emptyAuth': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set 'policy' */ + ret =asn1_write_value (tpm2key,"policy" ,"NEW" ,1 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'policy': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + cmd_code =grub_cpu_to_be32 (TPM_CC_PolicyPCR); + ret =asn1_write_value (tpm2key,"policy.?LAST.CommandCode" , &cmd_code, +sizeof (cmd_code)); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'policy CommandCode': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + ret =asn1_write_value (tpm2key,"policy.?LAST.CommandPolicy" , &pol_buf.data, + pol_buf.size); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'policy CommandPolicy': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Remove 'secret' */ + ret =asn1_write_value (tpm2key,"secret" ,NULL ,0 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to remove 'secret': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Remove 'authPolicy' */ + ret =asn1_write_value (tpm2key,"authPolicy" ,NULL ,0 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to remove 'authPolicy': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Remove 'description' */ + ret =asn1_write_value (tpm2key,"description" ,NULL ,0 ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to remove 'description': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* +* Use the SRK handle as the parent handle if specified +* Otherwise, Use TPM_RH_OWNER as the default parent handle +*/ +if (args->tpm2_srk !=0 ) + parent =grub_cpu_to_be32 (args->tpm2_srk); +else + parent =grub_cpu_to_be32 (TPM_RH_OWNER ); + ret =asn1_write_value (tpm2key,"parent" , &parent,sizeof (parent)); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'parent': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* +* Set 'rsaParent' to TRUE if the RSA SRK is specified and the SRK +* handle is not persistent. Otherwise, remove 'rsaParent'. +*/ +if (args->tpm2_srk ==0 && args->srk_type.type ==TPM_ALG_RSA ) + ret =asn1_write_value (tpm2key,"rsaParent" ,"TRUE" ,1 ); +else + ret =asn1_write_value (tpm2key,"rsaParent" ,NULL ,0 ); + +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'rsaParent': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set the pubkey */ + ret =asn1_write_value (tpm2key,"pubkey" , pub_buf.data, pub_buf.size); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'pubkey': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Set the privkey */ + ret =asn1_write_value (tpm2key,"privkey" , priv_buf.data, priv_buf.size); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"Failed to set 'privkey': 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + +/* Create the DER binary */ + der_buf_size =0 ; + ret =asn1_der_coding (tpm2key,"" ,NULL , &der_buf_size,NULL ); +if (ret !=ASN1_MEM_ERROR ) + { +fprintf (stderr,"Failed to get DER size: 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + + der_buf =grub_malloc (der_buf_size); +if (der_buf ==NULL ) + { +fprintf (stderr,"Failed to allocate memory for DER encoding\n" ); + err =GRUB_ERR_OUT_OF_MEMORY ; + goto error; + } + + ret =asn1_der_coding (tpm2key,"" , der_buf, &der_buf_size,NULL ); +if (ret !=ASN1_SUCCESS ) + { +fprintf (stderr,"DER coding error: 0x%x\n" , ret); + err =GRUB_ERR_BAD_ARGUMENT ; + goto error; + } + + err =protect_write_file (args->tpm2_outfile, der_buf, der_buf_size); +if (err !=GRUB_ERR_NONE ) +fprintf (stderr,"Could not write tpm2key file (Error: %u).\n" , errno); + + error: +grub_free (der_buf); + +if (tpm2key) +asn1_delete_structure (&tpm2key); + +return err; +}[...]
Quoted 21 lines
util/grub-protect.c new file +21
-0+static grub_err_t +protect_tpm2_args_verify (protect_args_t *args) +{ +switch (args->action) + { +case PROTECT_ACTION_ADD : +if (args->args &PROTECT_ARG_TPM2_EVICT ) + { +fprintf (stderr,N_ ("--tpm2-evict is invalid when --action is 'add'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_keyfile ==NULL ) + { +fprintf (stderr,N_ ("--tpm2-keyfile must be specified.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_outfile ==NULL ) + { +fprintf (stderr,N_ ("--tpm2-outfile must be specified.\n" ));Quoted 9 lines
util/grub-protect.c line 1039
Quoted 5 lines
util/grub-protect.c new file +5
-0+return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_device ==NULL ) + args->tpm2_device ="/dev/tpm0" ;When you move this "if" to the beginning of function or its end then you can do this check once and drop one below...
Got it.
Quoted 71 lines
Quoted 46 lines
util/grub-protect.c new file +46
-0+if (args->tpm2_pcr_count ==0 ) + { + args->tpm2_pcrs[0 ] =7 ; + args->tpm2_pcr_count =1 ; + } + +if (args->srk_type.type ==TPM_ALG_ERROR ) + { + args->srk_type.type =TPM_ALG_ECC ; + args->srk_type.detail.ecc_curve =TPM_ECC_NIST_P256 ; + } + +if (args->tpm2_bank ==TPM_ALG_ERROR ) + args->tpm2_bank =TPM_ALG_SHA256 ; + +break ; + +case PROTECT_ACTION_REMOVE : +if (args->args &PROTECT_ARG_TPM2_ASYMMETRIC ) + { +fprintf (stderr,N_ ("--tpm2-asymmetric is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_BANK ) + { +fprintf (stderr,N_ ("--tpm2-bank is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_KEYFILE ) + { +fprintf (stderr,N_ ("--tpm2-keyfile is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_OUTFILE ) + { +fprintf (stderr,N_ ("--tpm2-outfile is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->args &PROTECT_ARG_TPM2_PCRS ) + { +fprintf (stderr,N_ ("--tpm2-pcrs is invalid when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ;util/grub-protect.c line 1096
Quoted 10 lines
util/grub-protect.c new file +10
-0+ } + +if (args->tpm2_srk ==0 ) + { +fprintf (stderr,N_ ("--tpm2-srk is not specified when --action is 'remove'.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (args->tpm2_device ==NULL ) + args->tpm2_device ="/dev/tpm0" ;... I mean from here...
Quoted 9 lines
util/grub-protect.c new file +9
-0+break ; + +default : +fprintf (stderr,N_ ("The TPM2 key protector only supports the following actions: add, remove.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +return GRUB_ERR_NONE ; +}[...]
Quoted 14 lines
util/grub-protect.c line 1299
Quoted 11 lines
util/grub-protect.c new file +11
-0+static grub_err_t +protect_args_verify (protect_args_t *args) +{ +if (args->action ==PROTECT_ACTION_ERROR ) + { +fprintf (stderr,N_ ("--action is mandatory.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +/* At the moment, the only configurable key protector is the TPM2 one, so it +* is the only key protector supported by this tool. */ Wrong coding style for the comment...
Will fix it in the next version.
Quoted 33 lines
Quoted 16 lines
util/grub-protect.c new file +16
-0+if (args->protector !=PROTECT_TYPE_TPM2 ) + { +fprintf (stderr,N_ ("--protector is mandatory and only 'tpm2' is currently supported.\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +switch (args->protector) + { +case PROTECT_TYPE_TPM2 : +return protect_tpm2_args_verify (args); +default : +return GRUB_ERR_BAD_ARGUMENT ; + } + +return GRUB_ERR_NONE ; +}[...]
util/grub-protect.c line 1371
Quoted 4 lines
util/grub-protect.c new file +4
-0+int +main (int argc,char *argv[]) +{ +grub_err_t err;s/grub_err_t/int/
util/grub-protect.c line 1377
Quoted 6 lines
util/grub-protect.c new file +6
-0+protect_args_t args = {0 }; + +if (argp_parse (&protect_argp, argc, argv,0 ,0 , &args) !=0 ) + { +fprintf (stderr,N_ ("Could not parse arguments.\n" )); +return GRUB_ERR_BAD_ARGUMENT ;Quoted 4 lines
You expose GRUB internals to the user space and mix types. There is no guarantee
GRUB_ERR_BAD_ARGUMENTvalue will not change in the future. So, I think you should returnEXIT_FAILUREhere.Will fix it in the next version.
Quoted 13 lines
util/grub-protect.c line 1382
Quoted 5 lines
util/grub-protect.c new file +5
-0+ } + +protect_init (&argc, &argv); + + err =protect_args_verify (&args);Ditto... The
EXIT_SUCCESSandEXIT_FAILUREare your friends...util/grub-protect.c line 1386
Quoted 4 lines
util/grub-protect.c new file +4
-0+if (err !=GRUB_ERR_NONE ) + goto exit; + + err =protect_dispatch (&args);Quoted 5 lines
Ditto...
I did not check other patches but if you do the same thing elsewhere please fix it.
Will check other patches.
Quoted 8 lines
util/grub-protect.c line 1393
Quoted 8 lines
util/grub-protect.c new file +7
-0+if (err !=GRUB_ERR_NONE ) + goto exit; + + exit: +protect_fini (); + +return err;I would like to handle 'err' like this:
Quoted 11 lines
if (err !=
GRUB_ERR_NONE)return EXIT_FAILURE;return
EXIT_SUCCESS;Gary Lin
Quoted 3 lines
Quoted 1 line
+}
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 26/33 tpm2_key_protector: Support authorized policy
In reply to Gary Lin via Grub-develThis 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 /boot/efi/efi/grub/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 -a RSA --tpm2key=(hd0,gpt1)/efi/grub/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
--- .../commands/tpm2_key_protector/module.c | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+)
grub-core/commands/tpm2_key_protector/module.c +70 -0
@@ -618,6 +618,73 @@ tpm2_protector_policypcr (TPMI_SH_AUTH_SESSION_t session, struct grub_tpm2_buffereturn GRUB_ERR_NONE ; } +static grub_err_t +tpm2_protector_policyauthorize (TPMI_SH_AUTH_SESSION_t session,struct grub_tpm2_buffer *cmd_buf) +{ +TPM2B_PUBLIC_t pubkey; +TPM2B_DIGEST_t policy_ref; +TPMT_SIGNATURE_t signature; +TPM2B_DIGEST_t pcr_policy; +TPM2B_DIGEST_t pcr_policy_hash; +TPMI_ALG_HASH_t sig_hash; +TPMT_TK_VERIFIED_t verification_ticket; +TPM_HANDLE_t pubkey_handle =0 ; +TPM2B_NAME_t pubname; +TPM_RC_t rc; +grub_err_t err; + +grub_Tss2_MU_TPM2B_PUBLIC_Unmarshal (cmd_buf, &pubkey); +grub_Tss2_MU_TPM2B_DIGEST_Unmarshal (cmd_buf, &policy_ref); +grub_Tss2_MU_TPMT_SIGNATURE_Unmarshal (cmd_buf, &signature); +if (cmd_buf->error !=0 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Failed to unmarshal the buffer for TPM2_PolicyAuthorize" ); + +/* Retrieve Policy Digest */ + rc =grub_tpm2_policygetdigest (session,NULL , &pcr_policy,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_DEVICE ,"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 ,"Failed to get the hash algorithm of the signature" ); + + rc =grub_tpm2_hash (NULL , (TPM2B_MAX_BUFFER_t *) &pcr_policy, sig_hash, +TPM_RH_NULL , &pcr_policy_hash,NULL ,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_DEVICE ,"Failed to create PCR policy hash (TPM2_Hash: 0x%x)" , rc); + +/* Load the public key */ + rc =grub_tpm2_loadexternal (NULL ,NULL , &pubkey,TPM_RH_OWNER , &pubkey_handle, &pubname,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_DEVICE ,"Failed to load public key (TPM2_LoadExternal: 0x%x)" , rc); + +/* Verify the signature against the public key and the policy digest */ + rc =grub_tpm2_verifysignature (pubkey_handle,NULL , &pcr_policy_hash, &signature, + &verification_ticket,NULL ); +if (rc !=TPM_RC_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_DEVICE ,"Failed to verify signature (TPM2_VerifySignature: 0x%x)" , rc); + goto error; + } + +/* Authorize the signed policy with the public key and the verification ticket */ + rc =grub_tpm2_policyauthorize (session,NULL , &pcr_policy, &policy_ref, &pubname, + &verification_ticket,NULL ); +if (rc !=TPM_RC_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_DEVICE ,"Failed to authorize PCR policy (TPM2_PolicyAuthorize: 0x%x)" , rc); + goto error; + } + + err =GRUB_ERR_NONE ; + + error: +grub_tpm2_flushcontext (pubkey_handle); + +return err; +} +static grub_err_t tpm2_protector_enforce_policy (tpm2key_policy_t policy,TPMI_SH_AUTH_SESSION_t session) {
@@ -636,6 +703,9 @@ tpm2_protector_enforce_policy (tpm2key_policy_t policy, TPMI_SH_AUTH_SESSION_t scase TPM_CC_PolicyPCR: err =tpm2_protector_policypcr (session, &buf);break ; +case TPM_CC_PolicyAuthorize: + err =tpm2_protector_policyauthorize (session, &buf); +break ;default :return grub_error (GRUB_ERR_BAD_ARGUMENT ,"Unknown TPM Command: 0x%x" , policy->cmd_code); }
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 27/33 tpm2_key_protector: Implement NV index
In reply to Gary Lin via Grub-develFrom: 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. grub-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 ecc -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
--- .../commands/tpm2_key_protector/module.c | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
grub-core/commands/tpm2_key_protector/module.c +19 -4
@@ -973,11 +973,26 @@ tpm2_protector_srk_recover (const tpm2_protector_context_t *ctx, }static grub_err_t -tpm2_protector_nv_recover (const tpm2_protector_context_t *ctx __attribute__ ((unused)), -grub_uint8_t **key __attribute__ ((unused)), -grub_size_t *key_size __attribute__ ((unused))) +tpm2_protector_nv_recover (const tpm2_protector_context_t *ctx, +grub_uint8_t **key,grub_size_t *key_size) { -return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET ,"NV Index mode is not implemented yet" ); +TPM_HANDLE_t 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 =tpm2_protector_simple_policy_seq (ctx, &policy_seq); +if (err !=GRUB_ERR_NONE ) + goto exit; + + err =tpm2_protector_unseal (policy_seq, sealed_handle, key, key_size); + + exit: +grub_tpm2_flushcontext (sealed_handle); + +grub_tpm2key_free_policy_seq (policy_seq); + +return err; } static grub_err_t
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 28/33 cryptodisk: Fallback to passphrase
In reply to Gary Lin via Grub-develFrom: Patrick Colp <[email protected]>
If a protector is specified, but it fails to unlock the disk, fall back to asking for the passphrase.
Before requesting the passphrase, the error from the key protector(s) has to be cleared, or the later code (e.g., LUKS code) may stop as 'grub_errno' is set. This commit prints error from the key protector(s) and sets 'grub_errno' to 'GRUB_ERR_NONE' to have a fresh start.
--- grub-core/disk/cryptodisk.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +22 -1
@@ -1167,6 +1167,10 @@ grub_cryptodisk_scan_device_real (const char *name, ret = cr->recover_key (source, dev, cargs);if (ret !=GRUB_ERR_NONE ) { +/* Reset key data to trigger the passphrase prompt later */ + cargs->key_data =NULL ; + cargs->key_len =0 ; + part =grub_partition_get_name (source->partition);grub_dprintf ("cryptodisk" , "recovered a key from key protector %s but it "
@@ -1192,7 +1196,6 @@ 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)
@@ -1207,6 +1210,24 @@ grub_cryptodisk_scan_device_real (const char *name,unsigned long tries =3 ;const char *tries_env; +/* +* Print the error from key protectors and clear grub_errno. +* +* Since '--protector' cannot coexist with '--password' and +* '--key-file', in case key protectors fail, only +* "cargs->key_len == 0" is expected, so cryptomount falls back +* here to request the passphrase. +* +* To avoid the error from key protectors stops the further code, +* print the error to notify the user why key protectors fail and +* clear grub_errno to have a fresh start. +*/ +if (grub_errno !=GRUB_ERR_NONE ) + { +grub_print_error (); + grub_errno =GRUB_ERR_NONE ; + } + askpass =1 ; cargs->key_data =grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE );if (cargs->key_data ==NULL )
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 29/33 cryptodisk: wipe out the cached keys from protectors
In reply to Gary Lin via Grub-develAn attacker may insert a malicious disk with the same crypto UUID and trick GRUB 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
@@ -1405,7 +1405,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.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 30/33 diskfilter: look up cryptodisk devices first
In reply to Gary Lin via Grub-develWhen using disk auto-unlocking with TPM 2.0, the typical grub.cfg may look like this:
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub/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 GRUB to boot into the malicious root and further dump memory to steal the unsealed key.
Do defend against such an 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 an 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 GRUB to boot into the malicious disk.
To minimize 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 | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-)
grub-core/disk/diskfilter.c +22 -9
@@ -226,15 +226,28 @@ 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_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.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 31/33 tpm2_key_protector: Add grub-emu support
In reply to Gary Lin via Grub-develAs a preparation to test tpm2_key_protector with grub-emu, the new option, --tpm-device, is introduced to specify the TPM device for grub-emu so that grub-emu can access an emulated TPM device from the host.
Since grub-emu can directly access the device on host, it's easy to implement the essential TCG2 command submission function with the read/write functions and enable tpm2_key_protector module for grub-emu, so that we can further test TPM2 key unsealing with grub-emu.
--- grub-core/Makefile.core.def | 3 +++ grub-core/kern/emu/main.c | 11 +++++++- grub-core/kern/emu/misc.c | 51 +++++++++++++++++++++++++++++++++++ grub-core/lib/tss2/tcg2_emu.c | 51 +++++++++++++++++++++++++++++++++++ include/grub/emu/misc.h | 5 ++++ 5 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 grub-core/lib/tss2/tcg2_emu.c
grub-core/Makefile.core.def +3 -0
@@ -2574,7 +2574,9 @@ module = { common = lib/tss2/tpm2_cmd.c; common = lib/tss2/tss2.c; efi = lib/efi/tcg2.c; + emu = lib/tss2/tcg2_emu.c; enable = efi; + enable = emu; cppflags = '-I$(srcdir)/lib/tss2'; };
@@ -2586,6 +2588,7 @@ module = { common = commands/tpm2_key_protector/tpm2key_asn1_tab.c; /* The plaform support of tpm2_key_protector depends on the tcg2 implementation in tss2. */ enable = efi; + enable = emu; cppflags = '-I$(srcdir)/lib/tss2 -I$(srcdir)/lib/libtasn1-grub'; };
grub-core/kern/emu/main.c +10 -1
@@ -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/lib/tss2/tcg2_emu.c new file +51 -0
51 lines not on this page · raw message
include/grub/emu/misc.h +5 -0
@@ -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.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 32/33 tests: Add tpm2_key_protector_test
In reply to Gary Lin via Grub-develFor the tpm2_key_protector module, the TCG2 command submission function is the only difference between a QEMU instance and grub-emu. To test TPM2 key unsealing with a QEMU instance, it requires an extra OS image to invoke grub-protect to seal the LUKS key, rather than a simple grub-shell rescue CD image. On the other hand, grub-emu can share the emulated TPM2 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 TPM2 device is created by "swtpm chardev" and PCR 0 and 1 are extended.
There are several test cases in the script to test various settings. Each test case uses grub-protect or tpm2-tools to seal the LUKS password with PCR 0 and PCR 1. Then grub-emu is launched 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.
--- Makefile.util.def | 6 + tests/tpm2_key_protector_test.in | 389 +++++++++++++++++++++++++++++++ tests/util/grub-shell.in | 6 +- 3 files changed, 400 insertions(+), 1 deletion(-) create mode 100644 tests/tpm2_key_protector_test.in
Makefile.util.def +6 -0
@@ -1290,6 +1290,12 @@ script = { common = tests/asn1_test.in; }; +script = { + testcase = native; + name = tpm2_key_protector_test; + common = tests/tpm2_key_protector_test.in; +}; + program = { testcase = native; name = example_unit_test;
tests/tpm2_key_protector_test.in new file +389 -0
389 lines not on this page · raw message
tests/util/grub-shell.in +5 -1
@@ -75,6 +75,7 @@ work_directory=${WORKDIR:-`mktemp -d "${TMPDIR:-/tmp}/grub-shell.XXXXXXXXXX"`} | . "${builddir}/grub-core/modinfo.sh" qemuopts= +emuopts= serial_port=com0 serial_null= halt_cmd=halt
@@ -376,6 +377,9 @@ for option in "$@"; do --qemu-opts=*) qs=`echo "$option" | sed -e 's/--qemu-opts=//'` qemuopts="$qemuopts $qs" ;; + --emu-opts=*) + qs=`echo "$option" | sed -e 's/--emu-opts=//'` + emuopts="$emuopts $qs" ;; --disk=*) dsk=`echo "$option" | sed -e 's/--disk=//'` if [ ${grub_modinfo_platform} = emu ]; then
@@ -674,7 +678,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" ${emuopts} EOF else cat >"$work_directory/run.sh" <<EOF
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v20 33/33 docs: Document TPM2 key protector
In reply to Gary Lin via Grub-develUpdate the user manual to address TPM2 key protector including the two related commands, tpm2_key_protector_init and tpm2_key_protector_clear, and the user-space utility: grub-protect.
--- docs/grub.texi | 514 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 514 insertions(+)
docs/grub.texi +514 -0
@@ -6443,6 +6443,8 @@ you forget a command, you can run the command @command{help} * smbios:: Retrieve SMBIOS information * source:: Read a configuration file in same context * test:: Check file types and compare values +* tpm2_key_protector_init:: Initialize the TPM2 key protector +* tpm2_key_protector_clear:: Clear the TPM2 key protector * true:: Do nothing, successfully * trust:: Add public key to list of trusted keys * unset:: Unset an environment variable
@@ -8001,6 +8003,56 @@ either @var{expression1} or @var{expression2} is true @end table @end deffn +@node tpm2_key_protector_init +@subsection tpm2_key_protector_init + +@deffn Command tpm2_key_protector_init [@option{-m} mode] | [@option{-p} pcrlist] | [@option{-b} pcrbank] | [ [@option{-T} tpm2key_file] | [@option{-k} keyfile] ] | [@option{-s} handle] | [@option{-a} srk_type] | [@option{-n} nv_index] +Initialize the TPM2 key protector to unseal the key for the @command{cryptomount} +(@pxref{cryptomount}) command. There are two supported modes, +SRK(@kbd{srk}) and NV index(@kbd{nv}), to be specified by the option +@option{-m}. The default mode is SRK. The main difference between SRK mode +and NV index mode is the storage of the sealed key. For SRK mode, the sealed +key is stored in a file while NV index mode stores the sealed key in the +non-volatile memory inside TPM with a given NV index. + +The @option{-p} and @option{-b} options are used to supply the PCR list and +bank that the key is sealed with. The PCR list is a comma-separated list, e.g., +'0,2,4,7,9', to represent the involved PCRs, and the default is '7'. The PCR +bank is chosen by selecting a hash algorithm. The current supported PCR banks +are SHA1, SHA256, SHA384, and SHA512, and the default is SHA256. + +There are some options only available for the specific mode. The SRK-specific +options are @option{-T}, @option{-k}, @option{-a}, and @option{-s}. On the +other hand, the NV index-specific option is @option{-n}. + +The key file for SRK mode can be supplied with either @option{-T} or +@option{-k}. The @option{-T} option is for the path to the key file in +TPM 2.0 Key File. Since the parameters for the TPM commands are written in +the file, there is no need to set the PCR list(@option{-p}) and +bank(@option{-b}) when using the @option{-T} option. The @option{-k} option +is for the key file in the raw format, and the @option{-p} and @option{-b} +options are necessary for the non-default PCR list or bank. + +Besides the key file, there are two options, @option{-a} and @option{-s}, to +tweak the TPM Storage Root Key (SRK). The SRK can be either created at +runtime or stored in the non-volatile memory. When creating SRK at runtime, +GRUB provides the SRK template to TPM to create the key. There are two SRK +templates, ECC and RSA, for the @option{-a} option and the default is ECC. +If the SRK is stored in a specific handle, e.g. @code{0x81000001}, the +@option{-s} option can be used to set the handle to notify GRUB to load +the SRK from the given handle. + +The only nvindex-specific option is the option @option{-n} which is used to +set the NV index containing the sealed key, so that GRUB can load the sealed +key and unseal it with the given PCR list and bank. +@end deffn + +@node tpm2_key_protector_clear +@subsection tpm2_key_protector_clear + +@deffn Command tpm2_key_protector_clear +Clear the TPM2 key protector if previously initialized. +@end deffn @node true @subsection true
@@ -8529,6 +8581,7 @@ environment variables and commands are listed in the same order. * Secure Boot Advanced Targeting:: Embedded information for generation number based revocation * Measured Boot:: Measuring boot components * Lockdown:: Lockdown when booting on a secure setup +* TPM2 key protector:: Managing disk key with TPM2 key protector @end menu @node Authentication and authorisation
@@ -8772,6 +8825,299 @@ be restricted and some operations/commands cannot be executed. The @samp{lockdown} variable is set to @samp{y} when the GRUB is locked down. Otherwise it does not exit. +@node TPM2 key protector +@section TPM2 key protector in GRUB + +TPM2 key protector extends measured boot to unlock the encrypted partition +without user intervention. It uses the TPM Storage Root Key (SRK) to seal +the disk key with a given set of PCR values. If the system state matches, +i.e. PCR values match the sealed PCR set, TPM2 key protector unseals the +disk key for @command{cryptomount} (@pxref{cryptomount}) to unlock the +encrypted partition. (In case the unsealed key fails to unlock the +partition, @command{cryptomount} falls back to the passphrase prompt.) + +There are two supported modes to store the sealed key, SRK and NV index, +and the details will be addressed in later sections. + +TPM2 key protector is currently only supported on EFI and EMU platforms. + +@subsection TPM PCR usage + +Since TPM2 key protector relies on PCRs to check the system state, it is +important to decide which PCRs to seal the key with. The following table +shows the users of PCRs and the measured objects on EFI platforms. + +@multitable @columnfractions 0.1 0.2 0.7 +@headitem PCR @tab Used by @tab Measured Objects +@item 0 +@tab Firmware +@tab Core system firmware executable code +@item 1 +@tab Firmware +@tab Core system firmware data/host platform configuration; typically +contains serial and model numbers +@item 2 +@tab Firmware +@tab Extended or pluggable executable code; includes option ROMs on +pluggable hardware +@item 3 +@tab Firmware +@tab Extended or pluggable firmware data; includes information about +pluggable hardware +@item 4 +@tab Firmware +@tab Boot loader and additional drivers; binaries and extensions loaded +by the boot loader +@item 5 +@tab Firmware +@tab GPT/Partition table +@item 7 +@tab Firmware +@tab SecureBoot state +@item 8 +@tab GRUB +@tab Commands and kernel command line +@item 9 +@tab GRUB +@tab All files read (including kernel image) +@item 9 +@tab Linux Kernel +@tab All passed initrds (when the new LOAD_FILE2 initrd protocol is used) +@item 10 +@tab Linux Kernel +@tab Protection of the IMA measurement log +@item 14 +@tab shim +@tab “MOK” certificates and hashes +@end multitable + +PCR 0, 2, 4, and 7 can be used to check the integrity of the firmware code +and bootloaders. PCR 8 and 9 are useful to check the file and data processed +by GRUB. PCRs 10, 11, 12, 13, and 15 are controlled by the operating system, +so those PCRs are usually still in the initial state when GRUB is running. + +In general, it is nice to include PCR 0, 2, 4, and 7 to ensure the integrity +of the firmware and bootloaders. For PCR 8 and 9, a sophisticated tool is +required to examine the GRUB configuration files and the files to be loaded +to calculate the correct PCR values. + +Please note that PCRs are sensitive to any change, so an update of a component +could invalidate the sealed key, due to the so-called PCR brittleness. For the +bootloader update, PCR 4 may be affected. This can be mitigated by extracting +the events from the TPM event log and predict the value with the updated +bootloader binary. On the other hand, it is difficult to predict PCR 0~7 after +a firmware update since the content of the code and the order of drivers may +not follow the TPM event log from the previous firmware version, so it is +necessary to reboot the system to update the measurement results of PCR 0~7 +and seal or sign the sealed key again. + +Reference: @url{https://uapi-group.org/specifications/specs/linux_tpm_pcr_registry/, Linux TPM PCR Registry} + +@subsection Setting up the extra disk key + +Instead of using the existing password, it is recommended to seal a new +random disk key and use the existing password for recovery. + +Here are the sample commands to create a 128 random bytes key file and +enroll the key into the target partition (sda2). + +@example +# @kbd{dd if=/dev/urandom of=luks.key bs=1 count=128} +# @kbd{cryptsetup luksAddKey /dev/sda2 luks.key --pbkdf=pbkdf2 --hash=sha512} +@end example + +@subsection SRK mode + +To unlock the partition with SRK mode, assume that the sealed key is in +@file{(hd0,gpt1)/efi/grub/sealed.tpm}, the following GRUB commands +unseal the disk key with SRK mode and supply it to @command{cryptomount}. + +@example +@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm} +@kbd{cryptomount -u <UUID> -P tpm2} +@end example + +There are two programs to create the sealed key for SRK mode: @command{grub-protect} +and @command{pcr-oracle} (@url{https://github.com/okirch/pcr-oracle}). + +The following sample command uses @command{grub-protect} to seal the random +key, @file{luks.key}, with PCR 0, 2, 4 and 7 in TPM 2.0 Key File. + +@example +@group +# @kbd{grub-protect --action=add \ + --protector=tpm2 \ + --tpm2-pcrs=0,2,4,7 \ + --tpm2key \ + --tpm2-keyfile=luks.key \ + --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm} +@end group +@end example + +@command{grub-protect} only seals the key with the current PCR values. +Therefore, when a boot component, such as shim or GRUB, is updated, it is +neccessary to reboot the system to update the measurement results and seal +the key again. That means the random disk key has to be stored in cleartext +for the next key sealing. Besides this, the measurement result of some PCRs +may differ between boot time and OS runtime. For example, PCR 9 measures the +files loaded by GRUB including the Linux kernel and initrd. To unlock the disk +containing the kernel and initrd, the key has to be sealed with PCR 9 value +before loading the kernel and initrd. However, PCR 9 changes after GRUB +loading the kernel and initrd, so PCR 9 at OS runtime cannot be used directly +for key sealing. + +To solve these problems, @command{pcr-oracle} takes a different approach. It +reads the TPM eventlog and predicts the PCR values. Besides, +@command{pcr-oracle} also supports ``authorized policy'' which allows the +PCR policy to be updated with a valid signature, so that the user only seals +the random disk key once. If at some later time the PCR values change due to +an update of the system firmware, bootloader, or config file, the user just +needs to update the signature of PCR policy. + +To seal the key with the authorized policy, the first thing is to generate +the RSA policy key, @file{policy-key.pem}, and the authorized policy file, +@file{authorized.policy}. In this example, PCR 0, 2, 4, 7 and 9 are chosen +for key sealing. + +@example +@group +# @kbd{pcr-oracle --rsa-generate-key \ + --private-key policy-key.pem \ + --auth authorized.policy \ + create-authorized-policy 0,2,4,7,9} +@end group +@end example + +Then, we seal the random disk key, @file{luks.key}, with the authorized +policy file and save the sealed key in @file{sealed.key}. + +@example +@group +# @kbd{pcr-oracle --key-format tpm2.0 \ + --auth authorized.policy \ + --input luks.key \ + --output sealed.key \ + seal-secret} +@end group +@end example + +Since we now have the sealed key, we can remove the random disk key file +@file{lukes.key}. + +The last step is to sign the predicted PCR policy and save the final key +file, @file{sealed.tpm}. + +@example +@group +# @kbd{pcr-oracle --key-format tpm2.0 \ + --private-key policy-key.pem \ + --from eventlog \ + --stop-event "grub-file=grub.cfg" \ + --after \ + --input sealed.key \ + --output /boot/efi/efi/grub/sealed.tpm \ + sign 0,2,4,7,9} +@end group +@end example + +Here we also set a stop event for the prediction. With +@kbd{--stop-event grub-file=grub.cfg --after}, @command{pcr-oracle} stops +the calculation of PCR values right after GRUB loads @file{grub.cfg}. + +When shim or GRUB updates, it only requires to run the last @command{pcr-oracle} +command to update the predicted PCR policy. + +@subsection NV index mode + +Instead of storing the sealed key in a file, NV index mode uses the TPM +non-volatile memory to store the sealed key. + +The following sample commands use tpm2-tools (@url{https://github.com/tpm2-software/tpm2-tools}) +commands to seal @file{luks.key} into the specific NV index: @kbd{0x81000000}. + +First, we need to create the object file for the primary key, i.e. storage +root key (SRK) with the default key settings in GRUB: SHA256 hash algorithm +and ECC key algorithm. + +@example +# @kbd{tpm2_createprimary -C o -g sha256 -G ecc -c primary.ctx} +@end example + +The next commands collect the current values of PCR 0, 2, 4, and 7 and saves +them in @file{pcr.dat}. + +@example +# @kbd{tpm2_startauthsession -S session.dat} +# @kbd{tpm2_policypcr -S session.dat -l sha256:0,2,4,7 -f pcrs.dat -L policy.dat} +# @kbd{tpm2_flushcontext session.dat} +@end example + +The last commands seal @file{luks.key} with the primary key and stores the +result in @kbd{0x81000000}. + +@example +# @kbd{cat luks.key | tpm2_create -C primary.ctx -u key.pub -r key.priv -L policy.dat -i-} +# @kbd{tpm2_load -C primary.ctx -u key.pub -r key.priv -n sealing.name -c sealing.ctx} +# @kbd{tpm2_evictcontrol -C o -c sealing.ctx 0x81000000} +@end example + +To unseal the key, we have to specify the mode, @kbd{nv}, the NV index, +@kbd{0x81000000}, and the PCRs, @kbd{0,2,4,7} for @command{tpm2_key_protector_init} +command. + +@example +@kbd{tpm2_key_protector_init --mode=nv --nvindex=0x81000000 --pcrs=0,2,4,7} +@kbd{cryptomount -u <UUID> --protector tpm2} +@end example + +@subsection Setting up software TPM for EMU platform + +In order to test TPM2 key protector and TPM2 Software Stack (TSS2), it is +handy to set up a software TPM (swtpm) instance and run the commands on the +EMU platform. + +Here are the commands to start a swtpm instance which provides a character +device interface. To store the TPM states, the directory, @file{swtpm-state}, +is created before the @command{swtpm} command. All the messages are stored +in @file{swtpm.log} including the name of the character device. + +@example +# @kbd{mkdir swtpm-state} +@group +# @kbd{swtpm chardev --vtpm-proxy --tpmstate dir=swtpm-state \ + --tpm2 --ctrl type=unixio,path="swtpm-state/ctrl" \ + --flags startup-clear --daemon > swtpm.log} +@end group +@end example + +Then, we extract the name of the character device from @file{swtpm.log} and +save it to the variable, @samp{tpm2dev}. + +@example +# @kbd{tpm2dev=$(grep "New TPM device" swtpm.log | cut -d' ' -f 4)} +@end example + +Now we can start @kbd{grub-emu} with @kbd{--tpm-device $tpm2dev} to interact +with the swtpm instance. + +@example +# @kbd{grub-emu --tpm-device $tpm2dev} +@end example + +On the host, the tpm2-tools commands can interact with the swtpm instance by +setting @samp{TPM2TOOLS_TCTI}. + +@example +# @kbd{export TPM2TOOLS_TCTI="device:$tpm2dev"} +@end example + +When the test is done, use @kbd{swtpm_ioctl} to send the shutdown +command through the swtpm control channel. + +@example +# @kbd{swtpm_ioctl -s --unix swtpm-state/ctrl} +@end example + @node Platform limitations @chapter Platform limitations
@@ -9212,6 +9558,7 @@ bootability on other machines. * Invoking grub-mkrescue:: Make a GRUB rescue image * Invoking grub-mount:: Mount a file system using GRUB * Invoking grub-probe:: Probe device information for GRUB +* Invoking grub-protect:: Protect a disk key with a key protector * Invoking grub-script-check:: Check GRUB script file for syntax errors @end menu
@@ -9594,6 +9941,173 @@ Print verbose messages. @end table +@node Invoking grub-protect +@section Invoking grub-protect + +The program @command{grub-protect} protects a disk encryption key with +a specified key protector. + +@table @option +@item --help +Print a summary of the command-line options and exit. + +@item --version +Print the version number of GRUB and exit. + +@item -a +@itemx --action=add|remove +Add or remove a key protector to or from a key. + +@item -p +@itemx --protector=@var{protector} +Set the key protector. Currently, @samp{tpm2} is the only supported key +protector. + +@item --tpm2-asymmetric=@var{type} +Choose the the type of SRK. The valid options are @samp{RSA} (@samp{RSA2048}) +and @samp{ECC} (@samp{ECC_NIST_P256}).(default: @samp{ECC}) + +@item --tpm2-bank=@var{alg} +Choose bank of PCRs used to authorize key release: @samp{SHA1}, @samp{SHA256}, +@samp{SHA384}, or @samp{SHA512}. (default: @samp{SHA256}) + +@item --tpm2-device=@var{device} +Set the path to the TPM2 device. (default: @samp{/dev/tpm0}) + +@item --tpm2-evict +Evict a previously persisted SRK from the TPM, if any. + +@item --tpm2-keyfile=@var{file} +Set the path to a file that contains the cleartext key to protect. + +@item --tpm2-outfile=@var{file} +Set the path to the file that will contain the key after sealing +(must be accessible to GRUB during boot). + +@item --tpm2-pcrs=@var{pcrs} +Set a comma-separated list of PCRs used to authorize key release e.g., @samp{7,11}. +Please be aware that PCR 0~7 are used by the firmware and the measurement result +may change after a firmware update (for baremetal systems) or a package +(OVMF/SeaBIOS/SLOF) update in the VM host. This may lead to the failure of key +unsealing. (default: @samp{7}) + +@item --tpm2-srk=@var{handle} +Set the SRK handle, e.g. @samp{0x81000000}, if the SRK is to be made persistent. + +@item --tpm2key +Use TPM 2.0 Key File instead of the raw format. + +@end table + +Before sealing the key, please check the TPM PCR usage +(@pxref{TPM2 key protector, TPM PCR usage}) to choose a proper set of PCRs. + +Assume that there is a key file, @file{luks.key}, to be sealed with PCR 0, 2, +4, and 7, and here is the @command{grub-protect} command to create the sealed +key file: + +@example +@group +# @kbd{grub-protect --action=add \ + --protector=tpm2 \ + --tpm2-pcrs=0,2,4,7 \ + --tpm2key \ + --tpm2-keyfile=luks.key \ + --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm} +@end group +@end example + +Then, GRUB can unlock the target partition with the following commands: + +@example +@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm} +@kbd{cryptomount -u <UUID> -P tpm2} +@end example + +In most of cases, the user only needs to create the key with the `add' action. +If auto-unlocking is unwanted, just remove the file and restore the GRUB +commands. + +The only case for the `remove' action is when the SRK is made persistent. + +There are two supported SRKs in @command{grub-protect}: @samp{RSA} and @samp{ECC}. +Due to slower key generation, some users of the @samp{RSA} SRK would prefer +making it persistent so that the TPM can skip the SRK generation when GRUB tries +to unseal the key. + +The available persistent handles can be checked with @command{tpm2_getcap}. + +@example +@group +# @kbd{tpm2_getcap properties-variable} +... +TPM2_PT_HR_PERSISTENT: 0x0 +TPM2_PT_HR_PERSISTENT_AVAIL: 0x41 +... +@end group +@end example + +In this system, there is no persistent handle. A TPM handle is an unsigned +32-bit integer, and the persistent handles starts with @samp{0x81}. Here +we choose the well-known persistent handle: @samp{0x81000000}. + +@example +@group +# @kbd{grub-protect --action=add \ + --protector=tpm2 \ + --tpm2-pcrs=0,2,4,7 \ + --tpm2-asymmetric=RSA \ + --tpm2-srk=0x81000000 \ + --tpm2key \ + --tpm2-keyfile=luks.key \ + --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm} +@end group +@end example + +The additional @kbd{--tpm2-asymmetric=RSA} and @kbd{--tpm2-srk=0x81000000} +options are used to make the key sealed with the RSA SRK and store the SRK +in @samp{0x81000000}. + +For the @command{tpm2_key_protector_init} command, the additional @kbd{-s 0x81000000} +informs the TPM2 key protector to fetch the SRK from @samp{0x81000000}. + +@example +@kbd{tpm2_key_protector_init -s 0x81000000 --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm} +@kbd{cryptomount -u <UUID> -P tpm2} +@end example + +After making the SRK handle persistent, we can check the status of the +persistent handles with @command{tpm2_getcap}. + +@example +@group +# @kbd{tpm2_getcap properties-variable} +... +TPM2_PT_HR_PERSISTENT: 0x1 +TPM2_PT_HR_PERSISTENT_AVAIL: 0x40 +... +# @kbd{tpm2_getcap handles-persistent} +- 0x81000000 +@end group +@end example + +In case the user doesn't want to use the TPM2 key protector anymore, besides +removing the sealed key file, here is the command to remove the persistent + +The sealed key can be remove once the user does not want to use the TPM2 key +protector anymore. Here is the command to remove the persistent SRK handle +(@samp{0x81000000}) with @kbd{--tpm2-srk} and @kbd{--tpm2-evict}. + +@example +@group +# @kbd{grub-protect --action=remove \ + --protector=tpm2 \ + --tpm2-srk 0x81000000 \ + --tpm2-evict} +@end group +@end example + + @node Invoking grub-script-check @section Invoking grub-script-check
Signature · 7 lines
2.43.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 33/33] docs: Document TPM2 key protector
Quoted 41 lines · Gary Lin
Update the user manual to address TPM2 key protector including the two related commands,
tpm2_key_protector_initandtpm2_key_protector_clear, and the user-space utility: grub-protect.Signed-off-by Gary Lin <[email protected]>--- docs/grub.texi | 514 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 514 insertions(+)docs/grub.texi +17
-0@@ -6443,6 +6443,8 @@ you forget a command, you can run the command @command{help} * smbios:: Retrieve SMBIOS information * source:: Read a configuration file in same context * test:: Check file types and compare values +* tpm2_key_protector_init:: Initialize the TPM2 key protector +* tpm2_key_protector_clear:: Clear the TPM2 key protector * true:: Do nothing, successfully * trust:: Add public key to list of trusted keys * unset:: Unset an environment variable@@ -8001,6 +8003,56 @@ either @var{expression1} or @var{expression2} is true @end table @end deffn +@node tpm2_key_protector_init +@subsection tpm2_key_protector_init + +@deffn Command tpm2_key_protector_init [@option{-m} mode] | [@option{-p} pcrlist] | [@option{-b} pcrbank] | [ [@option{-T} tpm2key_file] | [@option{-k} keyfile] ] | [@option{-s} handle] | [@option{-a} srk_type] | [@option{-n} nv_index] +Initialize the TPM2 key protector to unseal the key for the @command{cryptomount} +(@pxref{cryptomount}) command. There are two supported modes, +SRK(@kbd{srk}) and NV index(@kbd{nv}), to be specified by the option +@option{-m}. The default mode is SRK. The main difference between SRK mode +and NV index mode is the storage of the sealed key. For SRK mode, the sealed +key is stored in a file while NV index mode stores the sealed key in the +non-volatile memory inside TPM with a given NV index. + +The @option{-p} and @option{-b} options are used to supply the PCR list and +bank that the key is sealed with. The PCR list is a comma-separated list, e.g., +'0,2,4,7,9', to represent the involved PCRs, and the default is '7'. The PCR
docs/grub.texi either @var{expression1} or @var{expression2} is true · line 8030
Quoted 10 lines
docs/grub.texi +10
-0+bank is chosen by selecting a hash algorithm. The current supported PCR banks +are SHA1, SHA256, SHA384, and SHA512, and the default is SHA256. + +There are some options only available for the specific mode. The SRK-specific +options are @option{-T}, @option{-k}, @option{-a}, and @option{-s}. On the +other hand, the NV index-specific option is @option{-n}. + +The key file for SRK mode can be supplied with either @option{-T} or +@option{-k}. The @option{-T} option is for the path to the key file in +TPM 2.0 Key File. Since the parameters for the TPM commands are written in
s/TPM 2.0 Key File/TPM 2.0 Key File format/?
docs/grub.texi either @var{expression1} or @var{expression2} is true · line 8040
Quoted 10 lines
docs/grub.texi +10
-0+the file, there is no need to set the PCR list(@option{-p}) and +bank(@option{-b}) when using the @option{-T} option. The @option{-k} option +is for the key file in the raw format, and the @option{-p} and @option{-b} +options are necessary for the non-default PCR list or bank. + +Besides the key file, there are two options, @option{-a} and @option{-s}, to +tweak the TPM Storage Root Key (SRK). The SRK can be either created at +runtime or stored in the non-volatile memory. When creating SRK at runtime, +GRUB provides the SRK template to TPM to create the key. There are two SRK +templates, ECC and RSA, for the @option{-a} option and the default is ECC.
s/templates, ECC and RSA, for the @option{-a} option/templates for the @option{-a} option, ECC and RSA,/?
docs/grub.texi either @var{expression1} or @var{expression2} is true · line 8045
Quoted 5 lines
docs/grub.texi +5
-0+If the SRK is stored in a specific handle, e.g. @code{0x81000001}, the +@option{-s} option can be used to set the handle to notify GRUB to load +the SRK from the given handle. + +The only nvindex-specific option is the option @option{-n} which is used to
s/option @option{-n}/@option{-n} option/
docs/grub.texi either @var{expression1} or @var{expression2} is true · line 8046
Quoted 1 line
docs/grub.texi +1
-0+set the NV index containing the sealed key, so that GRUB can load the sealed
s/, so that/. Then/
Quoted 24 lines
docs/grub.texi +10
-0+key and unseal it with the given PCR list and bank. +@end deffn + +@node tpm2_key_protector_clear +@subsection tpm2_key_protector_clear + +@deffn Command tpm2_key_protector_clear +Clear the TPM2 key protector if previously initialized. +@end deffn @node true @subsection true@@ -8529,6 +8581,7 @@ environment variables and commands are listed in the same order. * Secure Boot Advanced Targeting:: Embedded information for generation number based revocation * Measured Boot:: Measuring boot components * Lockdown:: Lockdown when booting on a secure setup +* TPM2 key protector:: Managing disk key with TPM2 key protector @end menu @node Authentication and authorisation@@ -8772,6 +8825,299 @@ be restricted and some operations/commands cannot be executed. The @samp{lockdown} variable is set to @samp{y} when the GRUB is locked down. Otherwise it does not exit.
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 8837
Quoted 10 lines
docs/grub.texi +10
-0+@node TPM2 key protector +@section TPM2 key protector in GRUB + +TPM2 key protector extends measured boot to unlock the encrypted partition +without user intervention. It uses the TPM Storage Root Key (SRK) to seal +the disk key with a given set of PCR values. If the system state matches, +i.e. PCR values match the sealed PCR set, TPM2 key protector unseals the +disk key for @command{cryptomount} (@pxref{cryptomount}) to unlock the +encrypted partition. (In case the unsealed key fails to unlock the +partition, @command{cryptomount} falls back to the passphrase prompt.)
Please drop parenthesis. They are not needed here.
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 8839
Quoted 1 line
docs/grub.texi +1
-0+There are two supported modes to store the sealed key, SRK and NV index,
s/,$/./
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 8840
Quoted 1 line
docs/grub.texi +1
-0+and the details will be addressed in later sections.
s/and the/The/
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 8848
Quoted 7 lines
docs/grub.texi +7
-0+TPM2 key protector is currently only supported on EFI and EMU platforms. + +@subsection TPM PCR usage + +Since TPM2 key protector relies on PCRs to check the system state, it is +important to decide which PCRs to seal the key with. The following table +shows the users of PCRs and the measured objects on EFI platforms.
s/shows the users/list uses/
Quoted 212 lines
docs/grub.texi +212
-0+@multitable @columnfractions 0.1 0.2 0.7 +@headitem PCR @tab Used by @tab Measured Objects +@item 0 +@tab Firmware +@tab Core system firmware executable code +@item 1 +@tab Firmware +@tab Core system firmware data/host platform configuration; typically +contains serial and model numbers +@item 2 +@tab Firmware +@tab Extended or pluggable executable code; includes option ROMs on +pluggable hardware +@item 3 +@tab Firmware +@tab Extended or pluggable firmware data; includes information about +pluggable hardware +@item 4 +@tab Firmware +@tab Boot loader and additional drivers; binaries and extensions loaded +by the boot loader +@item 5 +@tab Firmware +@tab GPT/Partition table +@item 7 +@tab Firmware +@tab SecureBoot state +@item 8 +@tab GRUB +@tab Commands and kernel command line +@item 9 +@tab GRUB +@tab All files read (including kernel image) +@item 9 +@tab Linux Kernel +@tab All passed initrds (when the new LOAD_FILE2 initrd protocol is used) +@item 10 +@tab Linux Kernel +@tab Protection of the IMA measurement log +@item 14 +@tab shim +@tab “MOK” certificates and hashes +@end multitable + +PCR 0, 2, 4, and 7 can be used to check the integrity of the firmware code +and bootloaders. PCR 8 and 9 are useful to check the file and data processed +by GRUB. PCRs 10, 11, 12, 13, and 15 are controlled by the operating system, +so those PCRs are usually still in the initial state when GRUB is running. + +In general, it is nice to include PCR 0, 2, 4, and 7 to ensure the integrity +of the firmware and bootloaders. For PCR 8 and 9, a sophisticated tool is +required to examine the GRUB configuration files and the files to be loaded +to calculate the correct PCR values. + +Please note that PCRs are sensitive to any change, so an update of a component +could invalidate the sealed key, due to the so-called PCR brittleness. For the +bootloader update, PCR 4 may be affected. This can be mitigated by extracting +the events from the TPM event log and predict the value with the updated +bootloader binary. On the other hand, it is difficult to predict PCR 0~7 after +a firmware update since the content of the code and the order of drivers may +not follow the TPM event log from the previous firmware version, so it is +necessary to reboot the system to update the measurement results of PCR 0~7 +and seal or sign the sealed key again. + +Reference: @url{https://uapi-group.org/specifications/specs/linux_tpm_pcr_registry/, Linux TPM PCR Registry} + +@subsection Setting up the extra disk key + +Instead of using the existing password, it is recommended to seal a new +random disk key and use the existing password for recovery. + +Here are the sample commands to create a 128 random bytes key file and +enroll the key into the target partition (sda2). + +@example +# @kbd{dd if=/dev/urandom of=luks.key bs=1 count=128} +# @kbd{cryptsetup luksAddKey /dev/sda2 luks.key --pbkdf=pbkdf2 --hash=sha512} +@end example + +@subsection SRK mode + +To unlock the partition with SRK mode, assume that the sealed key is in +@file{(hd0,gpt1)/efi/grub/sealed.tpm}, the following GRUB commands +unseal the disk key with SRK mode and supply it to @command{cryptomount}. + +@example +@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm} +@kbd{cryptomount -u <UUID> -P tpm2} +@end example + +There are two programs to create the sealed key for SRK mode: @command{grub-protect} +and @command{pcr-oracle} (@url{https://github.com/okirch/pcr-oracle}). + +The following sample command uses @command{grub-protect} to seal the random +key, @file{luks.key}, with PCR 0, 2, 4 and 7 in TPM 2.0 Key File. + +@example +@group +# @kbd{grub-protect --action=add \ + --protector=tpm2 \ + --tpm2-pcrs=0,2,4,7 \ + --tpm2key \ + --tpm2-keyfile=luks.key \ + --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm} +@end group +@end example + +@command{grub-protect} only seals the key with the current PCR values. +Therefore, when a boot component, such as shim or GRUB, is updated, it is +neccessary to reboot the system to update the measurement results and seal +the key again. That means the random disk key has to be stored in cleartext +for the next key sealing. Besides this, the measurement result of some PCRs +may differ between boot time and OS runtime. For example, PCR 9 measures the +files loaded by GRUB including the Linux kernel and initrd. To unlock the disk +containing the kernel and initrd, the key has to be sealed with PCR 9 value +before loading the kernel and initrd. However, PCR 9 changes after GRUB +loading the kernel and initrd, so PCR 9 at OS runtime cannot be used directly +for key sealing. + +To solve these problems, @command{pcr-oracle} takes a different approach. It +reads the TPM eventlog and predicts the PCR values. Besides, +@command{pcr-oracle} also supports ``authorized policy'' which allows the +PCR policy to be updated with a valid signature, so that the user only seals +the random disk key once. If at some later time the PCR values change due to +an update of the system firmware, bootloader, or config file, the user just +needs to update the signature of PCR policy. + +To seal the key with the authorized policy, the first thing is to generate +the RSA policy key, @file{policy-key.pem}, and the authorized policy file, +@file{authorized.policy}. In this example, PCR 0, 2, 4, 7 and 9 are chosen +for key sealing. + +@example +@group +# @kbd{pcr-oracle --rsa-generate-key \ + --private-key policy-key.pem \ + --auth authorized.policy \ + create-authorized-policy 0,2,4,7,9} +@end group +@end example + +Then, we seal the random disk key, @file{luks.key}, with the authorized +policy file and save the sealed key in @file{sealed.key}. + +@example +@group +# @kbd{pcr-oracle --key-format tpm2.0 \ + --auth authorized.policy \ + --input luks.key \ + --output sealed.key \ + seal-secret} +@end group +@end example + +Since we now have the sealed key, we can remove the random disk key file +@file{lukes.key}. + +The last step is to sign the predicted PCR policy and save the final key +file, @file{sealed.tpm}. + +@example +@group +# @kbd{pcr-oracle --key-format tpm2.0 \ + --private-key policy-key.pem \ + --from eventlog \ + --stop-event "grub-file=grub.cfg" \ + --after \ + --input sealed.key \ + --output /boot/efi/efi/grub/sealed.tpm \ + sign 0,2,4,7,9} +@end group +@end example + +Here we also set a stop event for the prediction. With +@kbd{--stop-event grub-file=grub.cfg --after}, @command{pcr-oracle} stops +the calculation of PCR values right after GRUB loads @file{grub.cfg}. + +When shim or GRUB updates, it only requires to run the last @command{pcr-oracle} +command to update the predicted PCR policy. + +@subsection NV index mode + +Instead of storing the sealed key in a file, NV index mode uses the TPM +non-volatile memory to store the sealed key. + +The following sample commands use tpm2-tools (@url{https://github.com/tpm2-software/tpm2-tools}) +commands to seal @file{luks.key} into the specific NV index: @kbd{0x81000000}. + +First, we need to create the object file for the primary key, i.e. storage +root key (SRK) with the default key settings in GRUB: SHA256 hash algorithm +and ECC key algorithm. + +@example +# @kbd{tpm2_createprimary -C o -g sha256 -G ecc -c primary.ctx} +@end example + +The next commands collect the current values of PCR 0, 2, 4, and 7 and saves +them in @file{pcr.dat}. + +@example +# @kbd{tpm2_startauthsession -S session.dat} +# @kbd{tpm2_policypcr -S session.dat -l sha256:0,2,4,7 -f pcrs.dat -L policy.dat} +# @kbd{tpm2_flushcontext session.dat} +@end example + +The last commands seal @file{luks.key} with the primary key and stores the +result in @kbd{0x81000000}. + +@example +# @kbd{cat luks.key | tpm2_create -C primary.ctx -u key.pub -r key.priv -L policy.dat -i-} +# @kbd{tpm2_load -C primary.ctx -u key.pub -r key.priv -n sealing.name -c sealing.ctx} +# @kbd{tpm2_evictcontrol -C o -c sealing.ctx 0x81000000}
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 9071
Quoted 10 lines
docs/grub.texi +10
-0+@end example + +To unseal the key, we have to specify the mode, @kbd{nv}, the NV index, +@kbd{0x81000000}, and the PCRs, @kbd{0,2,4,7} for @command{tpm2_key_protector_init} +command. + +@example +@kbd{tpm2_key_protector_init --mode=nv --nvindex=0x81000000 --pcrs=0,2,4,7} +@kbd{cryptomount -u <UUID> --protector tpm2} +@end example
I think you should precisely describe which commands are executed from running OS and which ones from the GRUB.
Quoted 67 lines
docs/grub.texi +53
-0+@subsection Setting up software TPM for EMU platform + +In order to test TPM2 key protector and TPM2 Software Stack (TSS2), it is +handy to set up a software TPM (swtpm) instance and run the commands on the +EMU platform. + +Here are the commands to start a swtpm instance which provides a character +device interface. To store the TPM states, the directory, @file{swtpm-state}, +is created before the @command{swtpm} command. All the messages are stored +in @file{swtpm.log} including the name of the character device. + +@example +# @kbd{mkdir swtpm-state} +@group +# @kbd{swtpm chardev --vtpm-proxy --tpmstate dir=swtpm-state \ + --tpm2 --ctrl type=unixio,path="swtpm-state/ctrl" \ + --flags startup-clear --daemon > swtpm.log} +@end group +@end example + +Then, we extract the name of the character device from @file{swtpm.log} and +save it to the variable, @samp{tpm2dev}. + +@example +# @kbd{tpm2dev=$(grep "New TPM device" swtpm.log | cut -d' ' -f 4)} +@end example + +Now we can start @kbd{grub-emu} with @kbd{--tpm-device $tpm2dev} to interact +with the swtpm instance. + +@example +# @kbd{grub-emu --tpm-device $tpm2dev} +@end example + +On the host, the tpm2-tools commands can interact with the swtpm instance by +setting @samp{TPM2TOOLS_TCTI}. + +@example +# @kbd{export TPM2TOOLS_TCTI="device:$tpm2dev"} +@end example + +When the test is done, use @kbd{swtpm_ioctl} to send the shutdown +command through the swtpm control channel. + +@example +# @kbd{swtpm_ioctl -s --unix swtpm-state/ctrl} +@end example + @node Platform limitations @chapter Platform limitations@@ -9212,6 +9558,7 @@ bootability on other machines. * Invoking grub-mkrescue:: Make a GRUB rescue image * Invoking grub-mount:: Mount a file system using GRUB * Invoking grub-probe:: Probe device information for GRUB +* Invoking grub-protect:: Protect a disk key with a key protector * Invoking grub-script-check:: Check GRUB script file for syntax errors @end menu@@ -9594,6 +9941,173 @@ Print verbose messages. @end table +@node Invoking grub-protect +@section Invoking grub-protect + +The program @command{grub-protect} protects a disk encryption key with
docs/grub.texi Print verbose messages. · line 9957
Quoted 10 lines
docs/grub.texi +10
-0+a specified key protector. + +@table @option +@item --help +Print a summary of the command-line options and exit. + +@item --version +Print the version number of GRUB and exit. + +@item -a
s/-a/-a add|remove/
Quoted 58 lines
docs/grub.texi +58
-0+@itemx --action=add|remove +Add or remove a key protector to or from a key. + +@item -p +@itemx --protector=@var{protector} +Set the key protector. Currently, @samp{tpm2} is the only supported key +protector. + +@item --tpm2-asymmetric=@var{type} +Choose the the type of SRK. The valid options are @samp{RSA} (@samp{RSA2048}) +and @samp{ECC} (@samp{ECC_NIST_P256}).(default: @samp{ECC}) + +@item --tpm2-bank=@var{alg} +Choose bank of PCRs used to authorize key release: @samp{SHA1}, @samp{SHA256}, +@samp{SHA384}, or @samp{SHA512}. (default: @samp{SHA256}) + +@item --tpm2-device=@var{device} +Set the path to the TPM2 device. (default: @samp{/dev/tpm0}) + +@item --tpm2-evict +Evict a previously persisted SRK from the TPM, if any. + +@item --tpm2-keyfile=@var{file} +Set the path to a file that contains the cleartext key to protect. + +@item --tpm2-outfile=@var{file} +Set the path to the file that will contain the key after sealing +(must be accessible to GRUB during boot). + +@item --tpm2-pcrs=@var{pcrs} +Set a comma-separated list of PCRs used to authorize key release e.g., @samp{7,11}. +Please be aware that PCR 0~7 are used by the firmware and the measurement result +may change after a firmware update (for baremetal systems) or a package +(OVMF/SeaBIOS/SLOF) update in the VM host. This may lead to the failure of key +unsealing. (default: @samp{7}) + +@item --tpm2-srk=@var{handle} +Set the SRK handle, e.g. @samp{0x81000000}, if the SRK is to be made persistent. + +@item --tpm2key +Use TPM 2.0 Key File instead of the raw format. + +@end table + +Before sealing the key, please check the TPM PCR usage +(@pxref{TPM2 key protector, TPM PCR usage}) to choose a proper set of PCRs. + +Assume that there is a key file, @file{luks.key}, to be sealed with PCR 0, 2, +4, and 7, and here is the @command{grub-protect} command to create the sealed +key file: + +@example +@group +# @kbd{grub-protect --action=add \ + --protector=tpm2 \ + --tpm2-pcrs=0,2,4,7 \ + --tpm2key \ + --tpm2-keyfile=luks.key \
docs/grub.texi Print verbose messages. · line 10025
Quoted 10 lines
docs/grub.texi +10
-0+ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm} +@end group +@end example + +Then, GRUB can unlock the target partition with the following commands: + +@example +@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm} +@kbd{cryptomount -u <UUID> -P tpm2} +@end example
Which commands are executed where?
docs/grub.texi Print verbose messages. · line 10029
Quoted 3 lines
docs/grub.texi +3
-0+In most of cases, the user only needs to create the key with the `add' action. +If auto-unlocking is unwanted, just remove the file and restore the GRUB +commands.
What do you mean by "restore the GRUB commands"?
Quoted 55 lines
docs/grub.texi +55
-0+The only case for the `remove' action is when the SRK is made persistent. + +There are two supported SRKs in @command{grub-protect}: @samp{RSA} and @samp{ECC}. +Due to slower key generation, some users of the @samp{RSA} SRK would prefer +making it persistent so that the TPM can skip the SRK generation when GRUB tries +to unseal the key. + +The available persistent handles can be checked with @command{tpm2_getcap}. + +@example +@group +# @kbd{tpm2_getcap properties-variable} +... +TPM2_PT_HR_PERSISTENT: 0x0 +TPM2_PT_HR_PERSISTENT_AVAIL: 0x41 +... +@end group +@end example + +In this system, there is no persistent handle. A TPM handle is an unsigned +32-bit integer, and the persistent handles starts with @samp{0x81}. Here +we choose the well-known persistent handle: @samp{0x81000000}. + +@example +@group +# @kbd{grub-protect --action=add \ + --protector=tpm2 \ + --tpm2-pcrs=0,2,4,7 \ + --tpm2-asymmetric=RSA \ + --tpm2-srk=0x81000000 \ + --tpm2key \ + --tpm2-keyfile=luks.key \ + --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm} +@end group +@end example + +The additional @kbd{--tpm2-asymmetric=RSA} and @kbd{--tpm2-srk=0x81000000} +options are used to make the key sealed with the RSA SRK and store the SRK +in @samp{0x81000000}. + +For the @command{tpm2_key_protector_init} command, the additional @kbd{-s 0x81000000} +informs the TPM2 key protector to fetch the SRK from @samp{0x81000000}. + +@example +@kbd{tpm2_key_protector_init -s 0x81000000 --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm} +@kbd{cryptomount -u <UUID> -P tpm2} +@end example + +After making the SRK handle persistent, we can check the status of the +persistent handles with @command{tpm2_getcap}. + +@example +@group +# @kbd{tpm2_getcap properties-variable} +...
docs/grub.texi Print verbose messages. · line 10095
Quoted 10 lines
docs/grub.texi +10
-0+TPM2_PT_HR_PERSISTENT: 0x1 +TPM2_PT_HR_PERSISTENT_AVAIL: 0x40 +... +# @kbd{tpm2_getcap handles-persistent} +- 0x81000000 +@end group +@end example + +In case the user doesn't want to use the TPM2 key protector anymore, besides +removing the sealed key file, here is the command to remove the persistent
Hmmm... The "persistent" what?
docs/grub.texi Print verbose messages. · line 10097
Quoted 1 line
docs/grub.texi +1
-0+The sealed key can be remove once the user does not want to use the TPM2 key
s/remove/removed/
docs/grub.texi Print verbose messages. · line 10108
Quoted 11 lines
docs/grub.texi +11
-0+protector anymore. Here is the command to remove the persistent SRK handle +(@samp{0x81000000}) with @kbd{--tpm2-srk} and @kbd{--tpm2-evict}. + +@example +@group +# @kbd{grub-protect --action=remove \ + --protector=tpm2 \ + --tpm2-srk 0x81000000 \ + --tpm2-evict} +@end group +@end example
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v20 33/33] docs: Document TPM2 key protector
Quoted 46 lines · Daniel Kiper
Quoted 45 lines · Gary Lin
Update the user manual to address TPM2 key protector including the two related commands,
tpm2_key_protector_initandtpm2_key_protector_clear, and the user-space utility: grub-protect.Signed-off-by Gary Lin <[email protected]>--- docs/grub.texi | 514 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 514 insertions(+)docs/grub.texi +21
-0@@ -6443,6 +6443,8 @@ you forget a command, you can run the command @command{help} * smbios:: Retrieve SMBIOS information * source:: Read a configuration file in same context * test:: Check file types and compare values +* tpm2_key_protector_init:: Initialize the TPM2 key protector +* tpm2_key_protector_clear:: Clear the TPM2 key protector * true:: Do nothing, successfully * trust:: Add public key to list of trusted keys * unset:: Unset an environment variable@@ -8001,6 +8003,56 @@ either @var{expression1} or @var{expression2} is true @end table @end deffn +@node tpm2_key_protector_init +@subsection tpm2_key_protector_init + +@deffn Command tpm2_key_protector_init [@option{-m} mode] | [@option{-p} pcrlist] | [@option{-b} pcrbank] | [ [@option{-T} tpm2key_file] | [@option{-k} keyfile] ] | [@option{-s} handle] | [@option{-a} srk_type] | [@option{-n} nv_index] +Initialize the TPM2 key protector to unseal the key for the @command{cryptomount} +(@pxref{cryptomount}) command. There are two supported modes, +SRK(@kbd{srk}) and NV index(@kbd{nv}), to be specified by the option +@option{-m}. The default mode is SRK. The main difference between SRK mode +and NV index mode is the storage of the sealed key. For SRK mode, the sealed +key is stored in a file while NV index mode stores the sealed key in the +non-volatile memory inside TPM with a given NV index. + +The @option{-p} and @option{-b} options are used to supply the PCR list and +bank that the key is sealed with. The PCR list is a comma-separated list, e.g., +'0,2,4,7,9', to represent the involved PCRs, and the default is '7'. The PCR +bank is chosen by selecting a hash algorithm. The current supported PCR banks +are SHA1, SHA256, SHA384, and SHA512, and the default is SHA256. + +There are some options only available for the specific mode. The SRK-specific
Quoted 9 lines
docs/grub.texi either @var{expression1} or @var{expression2} is true · line 8030
Quoted 6 lines
docs/grub.texi +6
-0+options are @option{-T}, @option{-k}, @option{-a}, and @option{-s}. On the +other hand, the NV index-specific option is @option{-n}. + +The key file for SRK mode can be supplied with either @option{-T} or +@option{-k}. The @option{-T} option is for the path to the key file in +TPM 2.0 Key File. Since the parameters for the TPM commands are written ins/TPM 2.0 Key File/TPM 2.0 Key File format/?
Will fix it in the next version.
Quoted 4 lines
Quoted 4 lines
docs/grub.texi +4
-0+the file, there is no need to set the PCR list(@option{-p}) and +bank(@option{-b}) when using the @option{-T} option. The @option{-k} option +is for the key file in the raw format, and the @option{-p} and @option{-b} +options are necessary for the non-default PCR list or bank.
Quoted 9 lines
docs/grub.texi either @var{expression1} or @var{expression2} is true · line 8040
Quoted 6 lines
docs/grub.texi +6
-0+ +Besides the key file, there are two options, @option{-a} and @option{-s}, to +tweak the TPM Storage Root Key (SRK). The SRK can be either created at +runtime or stored in the non-volatile memory. When creating SRK at runtime, +GRUB provides the SRK template to TPM to create the key. There are two SRK +templates, ECC and RSA, for the @option{-a} option and the default is ECC.s/templates, ECC and RSA, for the @option{-a} option/templates for the @option{-a} option, ECC and RSA,/?
Ditto
Quoted 8 lines
docs/grub.texi either @var{expression1} or @var{expression2} is true · line 8045
Quoted 5 lines
docs/grub.texi +5
-0+If the SRK is stored in a specific handle, e.g. @code{0x81000001}, the +@option{-s} option can be used to set the handle to notify GRUB to load +the SRK from the given handle. + +The only nvindex-specific option is the option @option{-n} which is used tos/option @option{-n}/@option{-n} option/
Ditto
Quoted 4 lines
docs/grub.texi either @var{expression1} or @var{expression2} is true · line 8046
Quoted 1 line
docs/grub.texi +1
-0+set the NV index containing the sealed key, so that GRUB can load the sealeds/, so that/. Then/
Ditto
Quoted 24 lines
Quoted 24 lines
docs/grub.texi +10
-0+key and unseal it with the given PCR list and bank. +@end deffn + +@node tpm2_key_protector_clear +@subsection tpm2_key_protector_clear + +@deffn Command tpm2_key_protector_clear +Clear the TPM2 key protector if previously initialized. +@end deffn @node true @subsection true@@ -8529,6 +8581,7 @@ environment variables and commands are listed in the same order. * Secure Boot Advanced Targeting:: Embedded information for generation number based revocation * Measured Boot:: Measuring boot components * Lockdown:: Lockdown when booting on a secure setup +* TPM2 key protector:: Managing disk key with TPM2 key protector @end menu @node Authentication and authorisation@@ -8772,6 +8825,299 @@ be restricted and some operations/commands cannot be executed. The @samp{lockdown} variable is set to @samp{y} when the GRUB is locked down. Otherwise it does not exit.
Quoted 13 lines
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 8837
Quoted 10 lines
docs/grub.texi +10
-0+@node TPM2 key protector +@section TPM2 key protector in GRUB + +TPM2 key protector extends measured boot to unlock the encrypted partition +without user intervention. It uses the TPM Storage Root Key (SRK) to seal +the disk key with a given set of PCR values. If the system state matches, +i.e. PCR values match the sealed PCR set, TPM2 key protector unseals the +disk key for @command{cryptomount} (@pxref{cryptomount}) to unlock the +encrypted partition. (In case the unsealed key fails to unlock the +partition, @command{cryptomount} falls back to the passphrase prompt.)Please drop parenthesis. They are not needed here.
No problem. Will drop parenthesis.
Quoted 4 lines
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 8839
Quoted 1 line
docs/grub.texi +1
-0+There are two supported modes to store the sealed key, SRK and NV index,s/,$/./
Will fix it in the next version.
Quoted 4 lines
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 8840
Quoted 1 line
docs/grub.texi +1
-0+and the details will be addressed in later sections.s/and the/The/
Ditto
Quoted 10 lines
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 8848
Quoted 7 lines
docs/grub.texi +7
-0+TPM2 key protector is currently only supported on EFI and EMU platforms. + +@subsection TPM PCR usage + +Since TPM2 key protector relies on PCRs to check the system state, it is +important to decide which PCRs to seal the key with. The following table +shows the users of PCRs and the measured objects on EFI platforms.s/shows the users/list uses/
Ditto
Quoted 217 lines
Quoted 217 lines
docs/grub.texi +217
-0+@multitable @columnfractions 0.1 0.2 0.7 +@headitem PCR @tab Used by @tab Measured Objects +@item 0 +@tab Firmware +@tab Core system firmware executable code +@item 1 +@tab Firmware +@tab Core system firmware data/host platform configuration; typically +contains serial and model numbers +@item 2 +@tab Firmware +@tab Extended or pluggable executable code; includes option ROMs on +pluggable hardware +@item 3 +@tab Firmware +@tab Extended or pluggable firmware data; includes information about +pluggable hardware +@item 4 +@tab Firmware +@tab Boot loader and additional drivers; binaries and extensions loaded +by the boot loader +@item 5 +@tab Firmware +@tab GPT/Partition table +@item 7 +@tab Firmware +@tab SecureBoot state +@item 8 +@tab GRUB +@tab Commands and kernel command line +@item 9 +@tab GRUB +@tab All files read (including kernel image) +@item 9 +@tab Linux Kernel +@tab All passed initrds (when the new LOAD_FILE2 initrd protocol is used) +@item 10 +@tab Linux Kernel +@tab Protection of the IMA measurement log +@item 14 +@tab shim +@tab “MOK” certificates and hashes +@end multitable + +PCR 0, 2, 4, and 7 can be used to check the integrity of the firmware code +and bootloaders. PCR 8 and 9 are useful to check the file and data processed +by GRUB. PCRs 10, 11, 12, 13, and 15 are controlled by the operating system, +so those PCRs are usually still in the initial state when GRUB is running. + +In general, it is nice to include PCR 0, 2, 4, and 7 to ensure the integrity +of the firmware and bootloaders. For PCR 8 and 9, a sophisticated tool is +required to examine the GRUB configuration files and the files to be loaded +to calculate the correct PCR values. + +Please note that PCRs are sensitive to any change, so an update of a component +could invalidate the sealed key, due to the so-called PCR brittleness. For the +bootloader update, PCR 4 may be affected. This can be mitigated by extracting +the events from the TPM event log and predict the value with the updated +bootloader binary. On the other hand, it is difficult to predict PCR 0~7 after +a firmware update since the content of the code and the order of drivers may +not follow the TPM event log from the previous firmware version, so it is +necessary to reboot the system to update the measurement results of PCR 0~7 +and seal or sign the sealed key again. + +Reference: @url{https://uapi-group.org/specifications/specs/linux_tpm_pcr_registry/, Linux TPM PCR Registry} + +@subsection Setting up the extra disk key + +Instead of using the existing password, it is recommended to seal a new +random disk key and use the existing password for recovery. + +Here are the sample commands to create a 128 random bytes key file and +enroll the key into the target partition (sda2). + +@example +# @kbd{dd if=/dev/urandom of=luks.key bs=1 count=128} +# @kbd{cryptsetup luksAddKey /dev/sda2 luks.key --pbkdf=pbkdf2 --hash=sha512} +@end example + +@subsection SRK mode + +To unlock the partition with SRK mode, assume that the sealed key is in +@file{(hd0,gpt1)/efi/grub/sealed.tpm}, the following GRUB commands +unseal the disk key with SRK mode and supply it to @command{cryptomount}. + +@example +@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm} +@kbd{cryptomount -u <UUID> -P tpm2} +@end example + +There are two programs to create the sealed key for SRK mode: @command{grub-protect} +and @command{pcr-oracle} (@url{https://github.com/okirch/pcr-oracle}). + +The following sample command uses @command{grub-protect} to seal the random +key, @file{luks.key}, with PCR 0, 2, 4 and 7 in TPM 2.0 Key File. + +@example +@group +# @kbd{grub-protect --action=add \ + --protector=tpm2 \ + --tpm2-pcrs=0,2,4,7 \ + --tpm2key \ + --tpm2-keyfile=luks.key \ + --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm} +@end group +@end example + +@command{grub-protect} only seals the key with the current PCR values. +Therefore, when a boot component, such as shim or GRUB, is updated, it is +neccessary to reboot the system to update the measurement results and seal +the key again. That means the random disk key has to be stored in cleartext +for the next key sealing. Besides this, the measurement result of some PCRs +may differ between boot time and OS runtime. For example, PCR 9 measures the +files loaded by GRUB including the Linux kernel and initrd. To unlock the disk +containing the kernel and initrd, the key has to be sealed with PCR 9 value +before loading the kernel and initrd. However, PCR 9 changes after GRUB +loading the kernel and initrd, so PCR 9 at OS runtime cannot be used directly +for key sealing. + +To solve these problems, @command{pcr-oracle} takes a different approach. It +reads the TPM eventlog and predicts the PCR values. Besides, +@command{pcr-oracle} also supports ``authorized policy'' which allows the +PCR policy to be updated with a valid signature, so that the user only seals +the random disk key once. If at some later time the PCR values change due to +an update of the system firmware, bootloader, or config file, the user just +needs to update the signature of PCR policy. + +To seal the key with the authorized policy, the first thing is to generate +the RSA policy key, @file{policy-key.pem}, and the authorized policy file, +@file{authorized.policy}. In this example, PCR 0, 2, 4, 7 and 9 are chosen +for key sealing. + +@example +@group +# @kbd{pcr-oracle --rsa-generate-key \ + --private-key policy-key.pem \ + --auth authorized.policy \ + create-authorized-policy 0,2,4,7,9} +@end group +@end example + +Then, we seal the random disk key, @file{luks.key}, with the authorized +policy file and save the sealed key in @file{sealed.key}. + +@example +@group +# @kbd{pcr-oracle --key-format tpm2.0 \ + --auth authorized.policy \ + --input luks.key \ + --output sealed.key \ + seal-secret} +@end group +@end example + +Since we now have the sealed key, we can remove the random disk key file +@file{lukes.key}. + +The last step is to sign the predicted PCR policy and save the final key +file, @file{sealed.tpm}. + +@example +@group +# @kbd{pcr-oracle --key-format tpm2.0 \ + --private-key policy-key.pem \ + --from eventlog \ + --stop-event "grub-file=grub.cfg" \ + --after \ + --input sealed.key \ + --output /boot/efi/efi/grub/sealed.tpm \ + sign 0,2,4,7,9} +@end group +@end example + +Here we also set a stop event for the prediction. With +@kbd{--stop-event grub-file=grub.cfg --after}, @command{pcr-oracle} stops +the calculation of PCR values right after GRUB loads @file{grub.cfg}. + +When shim or GRUB updates, it only requires to run the last @command{pcr-oracle} +command to update the predicted PCR policy. + +@subsection NV index mode + +Instead of storing the sealed key in a file, NV index mode uses the TPM +non-volatile memory to store the sealed key. + +The following sample commands use tpm2-tools (@url{https://github.com/tpm2-software/tpm2-tools}) +commands to seal @file{luks.key} into the specific NV index: @kbd{0x81000000}. + +First, we need to create the object file for the primary key, i.e. storage +root key (SRK) with the default key settings in GRUB: SHA256 hash algorithm +and ECC key algorithm. + +@example +# @kbd{tpm2_createprimary -C o -g sha256 -G ecc -c primary.ctx} +@end example + +The next commands collect the current values of PCR 0, 2, 4, and 7 and saves +them in @file{pcr.dat}. + +@example +# @kbd{tpm2_startauthsession -S session.dat} +# @kbd{tpm2_policypcr -S session.dat -l sha256:0,2,4,7 -f pcrs.dat -L policy.dat} +# @kbd{tpm2_flushcontext session.dat} +@end example + +The last commands seal @file{luks.key} with the primary key and stores the +result in @kbd{0x81000000}. + +@example +# @kbd{cat luks.key | tpm2_create -C primary.ctx -u key.pub -r key.priv -L policy.dat -i-} +# @kbd{tpm2_load -C primary.ctx -u key.pub -r key.priv -n sealing.name -c sealing.ctx} +# @kbd{tpm2_evictcontrol -C o -c sealing.ctx 0x81000000} +@end example + +To unseal the key, we have to specify the mode, @kbd{nv}, the NV index, +@kbd{0x81000000}, and the PCRs, @kbd{0,2,4,7} for @command{tpm2_key_protector_init} +command.
Quoted 9 lines
docs/grub.texi be restricted and some operations/commands cannot be executed. · line 9071
Quoted 5 lines
docs/grub.texi +5
-0+ +@example +@kbd{tpm2_key_protector_init --mode=nv --nvindex=0x81000000 --pcrs=0,2,4,7} +@kbd{cryptomount -u <UUID> --protector tpm2} +@end exampleI think you should precisely describe which commands are executed from running OS and which ones from the GRUB.
I'm planning to prepend 'grub> ' to the GRUB commands to notify the readers that those commands are for GRUB.
Quoted 63 lines
Quoted 63 lines
docs/grub.texi +49
-0+@subsection Setting up software TPM for EMU platform + +In order to test TPM2 key protector and TPM2 Software Stack (TSS2), it is +handy to set up a software TPM (swtpm) instance and run the commands on the +EMU platform. + +Here are the commands to start a swtpm instance which provides a character +device interface. To store the TPM states, the directory, @file{swtpm-state}, +is created before the @command{swtpm} command. All the messages are stored +in @file{swtpm.log} including the name of the character device. + +@example +# @kbd{mkdir swtpm-state} +@group +# @kbd{swtpm chardev --vtpm-proxy --tpmstate dir=swtpm-state \ + --tpm2 --ctrl type=unixio,path="swtpm-state/ctrl" \ + --flags startup-clear --daemon > swtpm.log} +@end group +@end example + +Then, we extract the name of the character device from @file{swtpm.log} and +save it to the variable, @samp{tpm2dev}. + +@example +# @kbd{tpm2dev=$(grep "New TPM device" swtpm.log | cut -d' ' -f 4)} +@end example + +Now we can start @kbd{grub-emu} with @kbd{--tpm-device $tpm2dev} to interact +with the swtpm instance. + +@example +# @kbd{grub-emu --tpm-device $tpm2dev} +@end example + +On the host, the tpm2-tools commands can interact with the swtpm instance by +setting @samp{TPM2TOOLS_TCTI}. + +@example +# @kbd{export TPM2TOOLS_TCTI="device:$tpm2dev"} +@end example + +When the test is done, use @kbd{swtpm_ioctl} to send the shutdown +command through the swtpm control channel. + +@example +# @kbd{swtpm_ioctl -s --unix swtpm-state/ctrl} +@end example + @node Platform limitations @chapter Platform limitations@@ -9212,6 +9558,7 @@ bootability on other machines. * Invoking grub-mkrescue:: Make a GRUB rescue image * Invoking grub-mount:: Mount a file system using GRUB * Invoking grub-probe:: Probe device information for GRUB +* Invoking grub-protect:: Protect a disk key with a key protector * Invoking grub-script-check:: Check GRUB script file for syntax errors @end menu@@ -9594,6 +9941,173 @@ Print verbose messages. @end table
Quoted 17 lines
Quoted 4 lines
docs/grub.texi +4
-0+@node Invoking grub-protect +@section Invoking grub-protect + +The program @command{grub-protect} protects a disk encryption key withdocs/grub.texi Print verbose messages. · line 9957
Quoted 10 lines
docs/grub.texi +10
-0+a specified key protector. + +@table @option +@item --help +Print a summary of the command-line options and exit. + +@item --version +Print the version number of GRUB and exit. + +@item -as/-a/-a add|remove/
Will fix it in the next version.
Quoted 62 lines
Quoted 62 lines
docs/grub.texi +62
-0+@itemx --action=add|remove +Add or remove a key protector to or from a key. + +@item -p +@itemx --protector=@var{protector} +Set the key protector. Currently, @samp{tpm2} is the only supported key +protector. + +@item --tpm2-asymmetric=@var{type} +Choose the the type of SRK. The valid options are @samp{RSA} (@samp{RSA2048}) +and @samp{ECC} (@samp{ECC_NIST_P256}).(default: @samp{ECC}) + +@item --tpm2-bank=@var{alg} +Choose bank of PCRs used to authorize key release: @samp{SHA1}, @samp{SHA256}, +@samp{SHA384}, or @samp{SHA512}. (default: @samp{SHA256}) + +@item --tpm2-device=@var{device} +Set the path to the TPM2 device. (default: @samp{/dev/tpm0}) + +@item --tpm2-evict +Evict a previously persisted SRK from the TPM, if any. + +@item --tpm2-keyfile=@var{file} +Set the path to a file that contains the cleartext key to protect. + +@item --tpm2-outfile=@var{file} +Set the path to the file that will contain the key after sealing +(must be accessible to GRUB during boot). + +@item --tpm2-pcrs=@var{pcrs} +Set a comma-separated list of PCRs used to authorize key release e.g., @samp{7,11}. +Please be aware that PCR 0~7 are used by the firmware and the measurement result +may change after a firmware update (for baremetal systems) or a package +(OVMF/SeaBIOS/SLOF) update in the VM host. This may lead to the failure of key +unsealing. (default: @samp{7}) + +@item --tpm2-srk=@var{handle} +Set the SRK handle, e.g. @samp{0x81000000}, if the SRK is to be made persistent. + +@item --tpm2key +Use TPM 2.0 Key File instead of the raw format. + +@end table + +Before sealing the key, please check the TPM PCR usage +(@pxref{TPM2 key protector, TPM PCR usage}) to choose a proper set of PCRs. + +Assume that there is a key file, @file{luks.key}, to be sealed with PCR 0, 2, +4, and 7, and here is the @command{grub-protect} command to create the sealed +key file: + +@example +@group +# @kbd{grub-protect --action=add \ + --protector=tpm2 \ + --tpm2-pcrs=0,2,4,7 \ + --tpm2key \ + --tpm2-keyfile=luks.key \ + --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm} +@end group +@end example +
Quoted 9 lines
docs/grub.texi Print verbose messages. · line 10025
Quoted 6 lines
docs/grub.texi +6
-0+Then, GRUB can unlock the target partition with the following commands: + +@example +@kbd{tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm} +@kbd{cryptomount -u <UUID> -P tpm2} +@end exampleWhich commands are executed where?
How about this?
Then, GRUB can unseal the disk key with @command{tpm2_key_protector_init} and use @command{cryptomount} to unlock the target partition with the key from the TPM2 key protector.
Quoted 6 lines
docs/grub.texi Print verbose messages. · line 10029
Quoted 3 lines
docs/grub.texi +3
-0+In most of cases, the user only needs to create the key with the `add' action. +If auto-unlocking is unwanted, just remove the file and restore the GRUB +commands.What do you mean by "restore the GRUB commands"?
The user can restore the above GRUB commands to "cryptomount -u <UUID>" to stop using auto-unlocking.
Quoted 64 lines
Quoted 63 lines
docs/grub.texi +63
-0+The only case for the `remove' action is when the SRK is made persistent. + +There are two supported SRKs in @command{grub-protect}: @samp{RSA} and @samp{ECC}. +Due to slower key generation, some users of the @samp{RSA} SRK would prefer +making it persistent so that the TPM can skip the SRK generation when GRUB tries +to unseal the key. + +The available persistent handles can be checked with @command{tpm2_getcap}. + +@example +@group +# @kbd{tpm2_getcap properties-variable} +... +TPM2_PT_HR_PERSISTENT: 0x0 +TPM2_PT_HR_PERSISTENT_AVAIL: 0x41 +... +@end group +@end example + +In this system, there is no persistent handle. A TPM handle is an unsigned +32-bit integer, and the persistent handles starts with @samp{0x81}. Here +we choose the well-known persistent handle: @samp{0x81000000}. + +@example +@group +# @kbd{grub-protect --action=add \ + --protector=tpm2 \ + --tpm2-pcrs=0,2,4,7 \ + --tpm2-asymmetric=RSA \ + --tpm2-srk=0x81000000 \ + --tpm2key \ + --tpm2-keyfile=luks.key \ + --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm} +@end group +@end example + +The additional @kbd{--tpm2-asymmetric=RSA} and @kbd{--tpm2-srk=0x81000000} +options are used to make the key sealed with the RSA SRK and store the SRK +in @samp{0x81000000}. + +For the @command{tpm2_key_protector_init} command, the additional @kbd{-s 0x81000000} +informs the TPM2 key protector to fetch the SRK from @samp{0x81000000}. + +@example +@kbd{tpm2_key_protector_init -s 0x81000000 --keyfile=(hd0,gpt1)/efi/grub/sealed.tpm} +@kbd{cryptomount -u <UUID> -P tpm2} +@end example + +After making the SRK handle persistent, we can check the status of the +persistent handles with @command{tpm2_getcap}. + +@example +@group +# @kbd{tpm2_getcap properties-variable} +... +TPM2_PT_HR_PERSISTENT: 0x1 +TPM2_PT_HR_PERSISTENT_AVAIL: 0x40 +... +# @kbd{tpm2_getcap handles-persistent} +- 0x81000000 +@end group +@end example +
Quoted 5 lines
docs/grub.texi Print verbose messages. · line 10095
Quoted 2 lines
docs/grub.texi +2
-0+In case the user doesn't want to use the TPM2 key protector anymore, besides +removing the sealed key file, here is the command to remove the persistentHmmm... The "persistent" what?
Oops, this part is supposed to be removed.
Quoted 4 lines
docs/grub.texi Print verbose messages. · line 10097
Quoted 1 line
docs/grub.texi +1
-0+The sealed key can be remove once the user does not want to use the TPM2 keys/remove/removed/
Will fix it in the next version.
Gary Lin
Quoted 5 lines
Quoted 5 lines
docs/grub.texi +5
-0+protector anymore. Here is the command to remove the persistent SRK handle +(@samp{0x81000000}) with @kbd{--tpm2-srk} and @kbd{--tpm2-evict}. + +@example +@group
Quoted 8 lines
docs/grub.texi Print verbose messages. · line 10108
Quoted 6 lines
docs/grub.texi +6
-0+# @kbd{grub-protect --action=remove \ + --protector=tpm2 \ + --tpm2-srk 0x81000000 \ + --tpm2-evict} +@end group +@end exampleDaniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 103 lines · Gary Lin
GIT repo for v20: https://github.com/lcp/grub2/tree/tpm2-unlock-v20
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 GRUB, 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~7,9-17 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into GRUB. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.Patch 8 fixes a potential buffer overrun in libtasn1.
(https://gitlab.com/gnutls/libtasn1/-/issues/49)Patch 17 adds the document for libtasn1 and the steps to upgrade the library.
Patch 19~25 are based on 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" forgrub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master
- Removing the measurement on the sealed key
- Based on the patch from Olaf Kirch <[email protected]>
- Adjusting the input parameters ofTPM2_EvictControlto match the order in "TCG TPM2 Part3 Commands"
- Declaring the input arguments of TPM2 functions as const
- Resending TPM2 commands onTPM_RC_RETRY
- Adding checks for the parameters of TPM2 commands
- Packing the missing authorization command forTPM2_PCR_Read
- Tweaking the TPM2 command functions to allow some parameters to be NULL so that we don't have to declare empty variables
- 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
- Extending the TPM2 TSS stack to support authorized policyPatch 26 implements the authorized policy support.
Patch 27 implements the missing NV index mode. (Thanks to Patrick Colp)
Patch 28 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 29 and 30 fix the potential security issues spotted by Fabian Vogt.
Patch 31 and 32 implement the TPM2 key unsealing testcases.
Patch 33 documents TPM2 key protector including the new GRUB commands and the user-space utility.
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=pbkdf22. Seal the key
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm3. Unseal the key with the proper commands in grub.cfg:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub/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.htmlv20:
- Rearranging the order of the libtasn1 patches
- libtasn1 patches are created by the following repo against the libtasn1-4.19.0-base-v4 tag:
https://github.com/lcp/grub2/tree/import-libtasn1-4.19.0-v4
- Excluding a libtasn1 testcase to avoid a potential gcc warning when 'long' is the same as 'int'
- Replacinggrub_swap_bytes32()withgrub_cpu_to_be32()to make the TPM command size always big endian
- Correcting the GRUB path in the commit messages and the document
- "/efi/grub2" => "/efi/grub"
- "/boot/efi/boot/grub2" => "/boot/efi/efi/grub/"
- "(hd0,gpt1)/boot/grub2/" => "(hd0,gpt1)/efi/grub"
- Removing the unnecessary "grub_errno=GRUB_ERR_NONE;"
- Replacing 'lu' with 'PRIuGRUB_UINT64_T' when setting the error message for 'uint64_t' to avoid the portability issue
- Removing the 'grub_' and 'GRUB_' prefixes from the local types, functions, and variables
- Documenting the option '-P' for 'cryptomount'
- Fixing coding style issues
- Removing the unused headers fromgrub-core/lib/tss2/tcg2_emu.c
- Updating the TPM2 key protector in user manual
[...]
Quoted 2 lines
*********************************************
* Anaylses for Coverity issuses on libtasn1 *
s/Anaylses for Coverity issuses on libtasn1/Analysis of libtasn1 Coverity issuses/
Quoted 15 lines
*********************************************
2 Memory corruptions: CID 435762, CID 435766
________________________________________________________________________________________________________
*** 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)".
Quoted 8 lines
152 ans[*ans_len - 1] -= 128;
153 }
154 }
155
156 /**
157 *asn1_octet_der:Reported to upstream: https://gitlab.com/gnutls/libtasn1/-/issues/49
OK but do we need a fix right now or not? If yes do we have one now? If not can we ignore this Coverity issue? Please comment that in next cover letter.
Quoted 13 lines
________________________________________________________________________________________________________
*** CID 435766: Memory - corruptions (OVERRUN) /grub-core/lib/libtasn1/lib/decoding.c: 1204 inasn1_der_decoding2()
1198 }
1199
1200DECR_LEN(ider_len, len2);
12011202 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
Quoted 7 lines
1206 counter += len2; 1207 move = RIGHT; 1208 break; 1209 case ASN1_ETYPE_OCTET_STRING:False positive?
https://gitlab.com/gnutls/libtasn1/-/issues/50
Ditto.
Quoted 104 lines
==
7 Integer handling issues:
CID 435774, CID 435773, CID 435772, CID 435768, CID 435765, CID 435764, CID 435763________________________________________________________________________________________________________
*** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 inasn1_get_object_id_der()
475 */
476 if (leading != 0 && der[len_len + k] ==0x80)
477 returnASN1_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 returnASN1_DER_ERROR;
483
484 val = val << 7;
485 val |= der[len_len + k] &0x7F;
486/grub-core/lib/libtasn1/lib/decoding.c: 481 in
asn1_get_object_id_der()Here are the related macros from gnulib:
#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_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ ((a) < 0 \ ? (a) < (min) >> (b) \: (max) >> (b) < (a))
#define
INT_LEFT_SHIFT_OVERFLOW(a, b)\INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))The statement in question is expanded "(a) < (min) >> (b)" from '
INT_LEFT_SHIFT_RANGE_OVERFLOW'.'(a) < (min) >> (b)'
=> '(val) <_GL_INT_MINIMUM(val) >> (7)'=> '(val) < \ (EXPR_SIGNED (val) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((_GL_INT_NEGATE_CONVERT (val, 1) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((_GL_INT_CONVERT (val, 1) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : (val)) + (0))) \ >> (7)''
_GL_INT_MINIMUM' returns the minimum value of the given type. Since 'val' is 'uint64_t', '_GL_INT_MINIMUM(val)' is 0'(val) <
_GL_INT_MINIMUM(val) >> (7)' => '(val) < 0 >> (7)' => '(val) < 0'For '
uint64_tval', the result is always false.
Quoted 8 lines
However, in '
INT_LEFT_SHIFT_RANGE_OVERFLOW':((a) < 0 \ ? (a) < (min) >> (b) \ : (max) >> (b) < (a))'(a) < 0' is false for '
uint64_tval', so the second operand, '(a) < (min) >> (b)', is always skipped. Thus, the result of the second operand doesn't matter.
What about the third operand? Does it return correct values? Can we ignore this Coverity issue? If yes why?
Quoted 53 lines
________________________________________________________________________________________________________
*** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 inasn1_get_object_id_der()
433 returnASN1_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 returnASN1_DER_ERROR;
441442 val0 <<= 7; 443 val0 |= der[len_len + k] & 0x7F; 444 if (!(der[len_len + k] & 0x80))Here are the related macros from gnulib:
#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_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ ((a) < 0 \ ? (a) < (min) >> (b) \: (max) >> (b) < (a))
#define
INT_LEFT_SHIFT_OVERFLOW(a, b)\INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))The statement in question is the expanded '
EXPR_SIGNED(val0)' from either '_GL_INT_MAXIMUM' or '_GL_INT_MINIMUM'
Quoted 7 lines
'
EXPR_SIGNED(val0)'
=> '(_GL_INT_NEGATE_CONVERT(val0, 1) < 0)'
=> '(((1 ? 0 : (val0)) - (1)) < 0)''
EXPR_SIGNED' is designed to test if the given expression is signed, and 'EXPR_SIGNED(val0)' is expected to be false for 'uint64_tval0'. The macro dutifully reflects the fact.
Again, can we ignore this Coverity issue? Why?
Quoted 78 lines
________________________________________________________________________________________________________
*** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 inasn1_get_tag_der()198 /* Long form */ 199 punt = 1; 200 ris = 0; 201 while (punt < der_len && der[punt] & 128)202 {
203Quoted 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 returnASN1_DER_ERROR;Here are the related macros gnulib:
#define
_GL_INT_NEGATE_CONVERT(e, v)((1 ? 0 : (e)) - (v))#define
_GL_INT_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
EXPR_SIGNED(e)(_GL_INT_NEGATE_CONVERT(e, 1) < 0)#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#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 the expanded '
EXPR_SIGNED(_GL_INT_CONVERT(ris, 128))' from either '_GL_INT_MINIMUM' or '_GL_INT_MAXIMUM'.'
EXPR_SIGNED(_GL_INT_CONVERT(ris, 128))'
=> 'EXPR_SIGNED((1 ? 0 : (ris)) + (128))'
=> '(_GL_INT_NEGATE_CONVERT(((1 ? 0 : (ris)) + (128), 1) < 0))' => '((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0)''
_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ris' and '128' is 'unsigned int', '_GL_INT_CONVERT(ris, 128)' is '128U'.
Quoted 6 lines
'
EXPR_SIGNED' is designed to check if the given expression is signed. Thus, 'EXPR_SIGNED(128U)' is expected to be false.The combination of '
EXPR_SIGNED(e)' and '_GL_INT_CONVERT(e, v)' is used to test if the common type of the given two variables is signed, and those macros dutifully reflect the fact: the common type of 'ris' and '128' is unsigned.
Ditto.
Quoted 161 lines
________________________________________________________________________________________________________
*** CID 435768: (CONSTANT_EXPRESSION_RESULT)
/grub-core/lib/libtasn1/lib/decoding.c: 204 inasn1_get_tag_der()198 /* Long form */ 199 punt = 1; 200 ris = 0; 201 while (punt < der_len && der[punt] & 128)202 {
203Quoted 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 returnASN1_DER_ERROR;
/grub-core/lib/libtasn1/lib/decoding.c: 217 inasn1_get_tag_der()
211 punt++;
212 }
213
214 if (punt >=der_len)
215 returnASN1_DER_ERROR;
216Quoted 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 returnASN1_DER_ERROR;
219 ris *= 128;
220
221 if (INT_ADD_OVERFLOW(ris, ((unsigned) (der[punt] &0x7F))))
222 returnASN1_DER_ERROR;Here are the related macros gnulib:
#define
_GL_INT_NEGATE_CONVERT(e, v)((1 ? 0 : (e)) - (v))#define
_GL_INT_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
EXPR_SIGNED(e)(_GL_INT_NEGATE_CONVERT(e, 1) < 0)#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#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 the expanded '(a) < (min) / (b)' from '
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)'=> '(ris) < \ (EXPR_SIGNED (_GL_INT_CONVERT (ris, 128)) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((_GL_INT_NEGATE_CONVERT (_GL_INT_CONVERT (ris, 128), 1) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : (_GL_INT_CONVERT (ris, 128))) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((_GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 1) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ris, 128))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : (_GL_INT_CONVERT (ris, 128))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ris, 128))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ris, 128))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ (_GL_INT_CONVERT (ris, 128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : (_GL_INT_CONVERT (ris, 128)) + (0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : ((1 ? 0 : (ris)) + (128)) + (0)) \ / (128)''
_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ris' and '128' is 'unsigned int', '_GL_INT_CONVERT(ris, 128)' is '128U'.'
_GL_INT_MINIMUM(128U)' returns the minimum value of 'unsigned int', i.e. 0.'(ris) < (
_GL_INT_MINIMUM(_GL_INT_CONVERT(ris, 128))) / (128)' => '(ris) < 0 / (128)' => '(ris) < 0'For 'unsigned int ris', the result is always false.
However, in '
INT_MULTIPLY_RANGE_OVERFLOW', 'a' is 'unsigned int ris' and 'b' is '128'. We can skip the statements for 'b < 0' and 'b == 0' and reduce the macro to
Quoted 7 lines
(a) < 0 \ ? (a) < (min) / (b) \ : (max) / (b) < (a))) \Since '(a) < 0' is false for 'unsigned int ris', the statement in question, '(a) < (min) / (b)', is always skipped. Thus, the result of the statement doesn't matter.
Same questions as for CID 435774...
Quoted 100 lines
________________________________________________________________________________________________________
*** CID 435765: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 439 inasn1_get_object_id_der()
433 returnASN1_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 returnASN1_DER_ERROR;
441442 val0 <<= 7; 443 val0 |= der[len_len + k] & 0x7F; 444 if (!(der[len_len + k] & 0x80))Here are the related macros from gnulib:
#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_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ ((a) < 0 \ ? (a) < (min) >> (b) \: (max) >> (b) < (a))
#define
INT_LEFT_SHIFT_OVERFLOW(a, b)\INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))The statement in question is expanded '(a) < (min) >> (b)' from
INT_LEFT_SHIFT_RANGE_OVERFLOW.'(a) < (min) >> (b)'
=> '(val) < (_GL_INT_MINIMUM(val)) >> (7)'=> '(val) < \ (EXPR_SIGNED (val) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((_GL_INT_NEGATE_CONVERT (val, 1) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((_GL_INT_CONVERT (val, 1) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (+ (val)) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (+ (val)) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : (val)) + (0))) \ >> (7)''
_GL_INT_MINIMUM' returns the minimum value of the given type. For 'uint64_tval', '_GL_INT_MINIMUM(val)' is 0.'(val) < (
_GL_INT_MINIMUM(val)) >> (7)' => '(val) < 0 >> (7)' => '(val) < 0'For '
uint64_tval', the result is always false.However, in '
INT_LEFT_SHIFT_RANGE_OVERFLOW':
Quoted 7 lines
((a) < 0 \ ? (a) < (min) >> (b) \ : (max) >> (b) < (a))'(a) < 0' is false for '
uint64_tval', so the statement in question, '(a) < (min) >> (b)', is always skipped. Thus, the result of the statement doesn't matter.
Ditto.
Quoted 78 lines
________________________________________________________________________________________________________
*** CID 435764: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 137 inasn1_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 from gnulib:
#define
_GL_INT_NEGATE_CONVERT(e, v)((1 ? 0 : (e)) - (v))#define
_GL_INT_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
EXPR_SIGNED(e)(_GL_INT_NEGATE_CONVERT(e, 1) < 0)#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#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 '
EXPR_SIGNED(_GL_INT_CONVERT(ans, 256))' from either '_GL_INT_MINIMUM' or '_GL_INT_MAXIMUM'.'
EXPR_SIGNED(_GL_INT_CONVERT(ans, 256))'
=> 'EXPR_SIGNED((1 ? 0 : (ans)) + (256))'
=> '(_GL_INT_NEGATE_CONVERT(((1 ? 0 : (ans)) + (256)), 1) < 0)' => '((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1))''
_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ans' and '256' is 'unsigned int', '_GL_INT_CONVERT(ans, 256)' is '256U'.
Quoted 6 lines
'
EXPR_SIGNED' is designed to check if the given expression is signed. Thus, 'EXPR_SIGNED(256U)' is expected to be false.The combination of '
EXPR_SIGNED(e)' and '_GL_INT_CONVERT(e, v)' is used to test if the common type of the given two variables is signed, and those macros dutifully reflect the fact: the common type of 'ans' and '256' is unsigned.
Can we ignore this Coverity issue? Why?
Quoted 137 lines
________________________________________________________________________________________________________
*** CID 435763: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 137 inasn1_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 from gnulib:
#define
_GL_INT_NEGATE_CONVERT(e, v)((1 ? 0 : (e)) - (v))#define
_GL_INT_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
EXPR_SIGNED(e)(_GL_INT_NEGATE_CONVERT(e, 1) < 0)#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#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 the expanded '(a) < (min) / (b)' from
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)'=> '(ans) < \ (EXPR_SIGNED (_GL_INT_CONVERT (ans, 256)) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ans, 256)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (_GL_INT_NEGATE_CONVERT (_GL_INT_CONVERT (ans, 256), 1) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ans, 256)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : (_GL_INT_CONVERT (ans, 256))) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ans, 256)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ (((_GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 1) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ans, 256))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : (_GL_INT_CONVERT (ans, 256))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ans, 256))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ans, 256))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ (_GL_INT_CONVERT (ans, 256))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ ((1 ? 0 : (ans)) + (1))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ ((1 ? 0 : (ans)) + (1))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : (_GL_INT_CONVERT (ans, 256))) + (0)) ) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ ((1 ? 0 : (ans)) + (1))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (0)) ) \ / (256)''
_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ans' and '256' is 'unsigned int', '_GL_INT_CONVERT(ans, 256)' is '256U'.'
_GL_INT_MINIMUM(256U)' returns the minimum value of 'unsigned int', i.e. 0.'(ans) < (
_GL_INT_MINIMUM(_GL_INT_CONVERT(ans, 256))) / (256)' => '(ans) < 0 / (256)' => '(ans) < 0'For 'unsigned int ans', the result is always false.
However, in '
INT_MULTIPLY_RANGE_OVERFLOW', 'a' is 'unsigned int ans' and 'b' is '256'. We can skip the statements for 'b < 0' and 'b == 0' and reduce the macro to
Quoted 7 lines
(a) < 0 \ ? (a) < (min) / (b) \ : (max) / (b) < (a))) \Since '(ans) < 0' is false for 'unsigned int ans', the statement in question, '(a) < (min) / (b)', is always skipped. Thus, the result of the statement doesn't matter.
Same questions as for CID 435774...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 134 lines · Daniel Kiper
Quoted 103 lines · Gary Lin
GIT repo for v20: https://github.com/lcp/grub2/tree/tpm2-unlock-v20
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 GRUB, 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~7,9-17 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into GRUB. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.Patch 8 fixes a potential buffer overrun in libtasn1.
(https://gitlab.com/gnutls/libtasn1/-/issues/49)Patch 17 adds the document for libtasn1 and the steps to upgrade the library.
Patch 19~25 are based on 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" forgrub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master
- Removing the measurement on the sealed key
- Based on the patch from Olaf Kirch <[email protected]>
- Adjusting the input parameters ofTPM2_EvictControlto match the order in "TCG TPM2 Part3 Commands"
- Declaring the input arguments of TPM2 functions as const
- Resending TPM2 commands onTPM_RC_RETRY
- Adding checks for the parameters of TPM2 commands
- Packing the missing authorization command forTPM2_PCR_Read
- Tweaking the TPM2 command functions to allow some parameters to be NULL so that we don't have to declare empty variables
- 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
- Extending the TPM2 TSS stack to support authorized policyPatch 26 implements the authorized policy support.
Patch 27 implements the missing NV index mode. (Thanks to Patrick Colp)
Patch 28 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 29 and 30 fix the potential security issues spotted by Fabian Vogt.
Patch 31 and 32 implement the TPM2 key unsealing testcases.
Patch 33 documents TPM2 key protector including the new GRUB commands and the user-space utility.
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=pbkdf22. Seal the key
$ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/efi/grub/sealed.tpm3. Unseal the key with the proper commands in grub.cfg:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub/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.htmlv20:
- Rearranging the order of the libtasn1 patches
- libtasn1 patches are created by the following repo against the libtasn1-4.19.0-base-v4 tag:
https://github.com/lcp/grub2/tree/import-libtasn1-4.19.0-v4
- Excluding a libtasn1 testcase to avoid a potential gcc warning when 'long' is the same as 'int'
- Replacinggrub_swap_bytes32()withgrub_cpu_to_be32()to make the TPM command size always big endian
- Correcting the GRUB path in the commit messages and the document
- "/efi/grub2" => "/efi/grub"
- "/boot/efi/boot/grub2" => "/boot/efi/efi/grub/"
- "(hd0,gpt1)/boot/grub2/" => "(hd0,gpt1)/efi/grub"
- Removing the unnecessary "grub_errno=GRUB_ERR_NONE;"
- Replacing 'lu' with 'PRIuGRUB_UINT64_T' when setting the error message for 'uint64_t' to avoid the portability issue
- Removing the 'grub_' and 'GRUB_' prefixes from the local types, functions, and variables
- Documenting the option '-P' for 'cryptomount'
- Fixing coding style issues
- Removing the unused headers fromgrub-core/lib/tss2/tcg2_emu.c
- Updating the TPM2 key protector in user manual[...]
Quoted 2 lines
*********************************************
* Anaylses for Coverity issuses on libtasn1 *s/Anaylses for Coverity issuses on libtasn1/Analysis of libtasn1 Coverity issuses/
Quoted 22 lines
*********************************************
2 Memory corruptions: CID 435762, CID 435766
________________________________________________________________________________________________________
*** 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:
Quoted 6 lines
Quoted 1 line
Reported to upstream: https://gitlab.com/gnutls/libtasn1/-/issues/49
OK but do we need a fix right now or not? If yes do we have one now? If not can we ignore this Coverity issue? Please comment that in next cover letter.
Oops, I forgot to copy the summary to the cover letter.
CID 435762 is fixed by Patch 08, and CID 435766 is likely to be false positive.
Quoted 18 lines
Quoted 18 lines
________________________________________________________________________________________________________
*** CID 435766: Memory - corruptions (OVERRUN) /grub-core/lib/libtasn1/lib/decoding.c: 1204 inasn1_der_decoding2()
1198 }
1199
1200DECR_LEN(ider_len, len2);
12011202 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);
12051206 counter += len2; 1207 move = RIGHT; 1208 break; 1209 case ASN1_ETYPE_OCTET_STRING:
Quoted 9 lines
Quoted 2 lines
False positive?
https://gitlab.com/gnutls/libtasn1/-/issues/50Ditto.
Quoted 4 lines
==
7 Integer handling issues:
CID 435774, CID 435773, CID 435772, CID 435768, CID 435765, CID 435764, CID 435763
All those integer handling issues are false positive. The results are expected, and those issues can be ignored.
Gary Lin
Quoted 771 lines
Quoted 100 lines
________________________________________________________________________________________________________
*** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 inasn1_get_object_id_der()
475 */
476 if (leading != 0 && der[len_len + k] ==0x80)
477 returnASN1_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 returnASN1_DER_ERROR;
483
484 val = val << 7;
485 val |= der[len_len + k] &0x7F;
486/grub-core/lib/libtasn1/lib/decoding.c: 481 in
asn1_get_object_id_der()Here are the related macros from gnulib:
#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_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ ((a) < 0 \ ? (a) < (min) >> (b) \: (max) >> (b) < (a))
#define
INT_LEFT_SHIFT_OVERFLOW(a, b)\INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))The statement in question is expanded "(a) < (min) >> (b)" from '
INT_LEFT_SHIFT_RANGE_OVERFLOW'.'(a) < (min) >> (b)'
=> '(val) <_GL_INT_MINIMUM(val) >> (7)'=> '(val) < \ (EXPR_SIGNED (val) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((_GL_INT_NEGATE_CONVERT (val, 1) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((_GL_INT_CONVERT (val, 1) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : (val)) + (0))) \ >> (7)''
_GL_INT_MINIMUM' returns the minimum value of the given type. Since 'val' is 'uint64_t', '_GL_INT_MINIMUM(val)' is 0'(val) <
_GL_INT_MINIMUM(val) >> (7)' => '(val) < 0 >> (7)' => '(val) < 0'For '
uint64_tval', the result is always false.Quoted 8 lines
However, in '
INT_LEFT_SHIFT_RANGE_OVERFLOW':((a) < 0 \ ? (a) < (min) >> (b) \ : (max) >> (b) < (a))'(a) < 0' is false for '
uint64_tval', so the second operand, '(a) < (min) >> (b)', is always skipped. Thus, the result of the second operand doesn't matter.What about the third operand? Does it return correct values? Can we ignore this Coverity issue? If yes why?
Quoted 53 lines
________________________________________________________________________________________________________
*** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 inasn1_get_object_id_der()
433 returnASN1_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 returnASN1_DER_ERROR;
441442 val0 <<= 7; 443 val0 |= der[len_len + k] & 0x7F; 444 if (!(der[len_len + k] & 0x80))Here are the related macros from gnulib:
#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_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ ((a) < 0 \ ? (a) < (min) >> (b) \: (max) >> (b) < (a))
#define
INT_LEFT_SHIFT_OVERFLOW(a, b)\INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))The statement in question is the expanded '
EXPR_SIGNED(val0)' from either '_GL_INT_MAXIMUM' or '_GL_INT_MINIMUM'Quoted 7 lines
'
EXPR_SIGNED(val0)'
=> '(_GL_INT_NEGATE_CONVERT(val0, 1) < 0)'
=> '(((1 ? 0 : (val0)) - (1)) < 0)''
EXPR_SIGNED' is designed to test if the given expression is signed, and 'EXPR_SIGNED(val0)' is expected to be false for 'uint64_tval0'. The macro dutifully reflects the fact.Again, can we ignore this Coverity issue? Why?
Quoted 78 lines
________________________________________________________________________________________________________
*** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 inasn1_get_tag_der()198 /* Long form */ 199 punt = 1; 200 ris = 0; 201 while (punt < der_len && der[punt] & 128)202 {
203Quoted 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 returnASN1_DER_ERROR;Here are the related macros gnulib:
#define
_GL_INT_NEGATE_CONVERT(e, v)((1 ? 0 : (e)) - (v))#define
_GL_INT_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
EXPR_SIGNED(e)(_GL_INT_NEGATE_CONVERT(e, 1) < 0)#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#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 the expanded '
EXPR_SIGNED(_GL_INT_CONVERT(ris, 128))' from either '_GL_INT_MINIMUM' or '_GL_INT_MAXIMUM'.'
EXPR_SIGNED(_GL_INT_CONVERT(ris, 128))'
=> 'EXPR_SIGNED((1 ? 0 : (ris)) + (128))'
=> '(_GL_INT_NEGATE_CONVERT(((1 ? 0 : (ris)) + (128), 1) < 0))' => '((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0)''
_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ris' and '128' is 'unsigned int', '_GL_INT_CONVERT(ris, 128)' is '128U'.Quoted 6 lines
'
EXPR_SIGNED' is designed to check if the given expression is signed. Thus, 'EXPR_SIGNED(128U)' is expected to be false.The combination of '
EXPR_SIGNED(e)' and '_GL_INT_CONVERT(e, v)' is used to test if the common type of the given two variables is signed, and those macros dutifully reflect the fact: the common type of 'ris' and '128' is unsigned.Ditto.
Quoted 161 lines
________________________________________________________________________________________________________
*** CID 435768: (CONSTANT_EXPRESSION_RESULT)
/grub-core/lib/libtasn1/lib/decoding.c: 204 inasn1_get_tag_der()198 /* Long form */ 199 punt = 1; 200 ris = 0; 201 while (punt < der_len && der[punt] & 128)202 {
203Quoted 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 returnASN1_DER_ERROR;
/grub-core/lib/libtasn1/lib/decoding.c: 217 inasn1_get_tag_der()
211 punt++;
212 }
213
214 if (punt >=der_len)
215 returnASN1_DER_ERROR;
216Quoted 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 returnASN1_DER_ERROR;
219 ris *= 128;
220
221 if (INT_ADD_OVERFLOW(ris, ((unsigned) (der[punt] &0x7F))))
222 returnASN1_DER_ERROR;Here are the related macros gnulib:
#define
_GL_INT_NEGATE_CONVERT(e, v)((1 ? 0 : (e)) - (v))#define
_GL_INT_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
EXPR_SIGNED(e)(_GL_INT_NEGATE_CONVERT(e, 1) < 0)#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#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 the expanded '(a) < (min) / (b)' from '
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)'=> '(ris) < \ (EXPR_SIGNED (_GL_INT_CONVERT (ris, 128)) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((_GL_INT_NEGATE_CONVERT (_GL_INT_CONVERT (ris, 128), 1) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : (_GL_INT_CONVERT (ris, 128))) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ris, 128)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((_GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 1) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ris, 128))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : (_GL_INT_CONVERT (ris, 128))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ris, 128))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ris, 128))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ (_GL_INT_CONVERT (ris, 128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ris, 128), 0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : (_GL_INT_CONVERT (ris, 128)) + (0)) \ / (128)' => '(ris) < \ ((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) - (1)) < 0) \ ? ~ (((((1 ? 0 : ((1 ? 0 : (ris)) + (128))) + (1)) << ((sizeof (+ ((1 ? 0 : (ris)) + (128))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : ((1 ? 0 : (ris)) + (128)) + (0)) \ / (128)''
_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ris' and '128' is 'unsigned int', '_GL_INT_CONVERT(ris, 128)' is '128U'.'
_GL_INT_MINIMUM(128U)' returns the minimum value of 'unsigned int', i.e. 0.'(ris) < (
_GL_INT_MINIMUM(_GL_INT_CONVERT(ris, 128))) / (128)' => '(ris) < 0 / (128)' => '(ris) < 0'For 'unsigned int ris', the result is always false.
However, in '
INT_MULTIPLY_RANGE_OVERFLOW', 'a' is 'unsigned int ris' and 'b' is '128'. We can skip the statements for 'b < 0' and 'b == 0' and reduce the macro toQuoted 7 lines
(a) < 0 \ ? (a) < (min) / (b) \ : (max) / (b) < (a))) \Since '(a) < 0' is false for 'unsigned int ris', the statement in question, '(a) < (min) / (b)', is always skipped. Thus, the result of the statement doesn't matter.
Same questions as for CID 435774...
Quoted 100 lines
________________________________________________________________________________________________________
*** CID 435765: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 439 inasn1_get_object_id_der()
433 returnASN1_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 returnASN1_DER_ERROR;
441442 val0 <<= 7; 443 val0 |= der[len_len + k] & 0x7F; 444 if (!(der[len_len + k] & 0x80))Here are the related macros from gnulib:
#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_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ ((a) < 0 \ ? (a) < (min) >> (b) \: (max) >> (b) < (a))
#define
INT_LEFT_SHIFT_OVERFLOW(a, b)\INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))The statement in question is expanded '(a) < (min) >> (b)' from
INT_LEFT_SHIFT_RANGE_OVERFLOW.'(a) < (min) >> (b)'
=> '(val) < (_GL_INT_MINIMUM(val)) >> (7)'=> '(val) < \ (EXPR_SIGNED (val) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((_GL_INT_NEGATE_CONVERT (val, 1) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (val) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((_GL_INT_CONVERT (val, 1) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (+ (val)) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (val, 0)) \ >> (7)' => '(val) < \ ((((1 ? 0 : (val)) - (1)) < 0) \ ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (+ (val)) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : (val)) + (0))) \ >> (7)''
_GL_INT_MINIMUM' returns the minimum value of the given type. For 'uint64_tval', '_GL_INT_MINIMUM(val)' is 0.'(val) < (
_GL_INT_MINIMUM(val)) >> (7)' => '(val) < 0 >> (7)' => '(val) < 0'For '
uint64_tval', the result is always false.However, in '
INT_LEFT_SHIFT_RANGE_OVERFLOW':Quoted 7 lines
((a) < 0 \ ? (a) < (min) >> (b) \ : (max) >> (b) < (a))'(a) < 0' is false for '
uint64_tval', so the statement in question, '(a) < (min) >> (b)', is always skipped. Thus, the result of the statement doesn't matter.Ditto.
Quoted 78 lines
________________________________________________________________________________________________________
*** CID 435764: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 137 inasn1_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 from gnulib:
#define
_GL_INT_NEGATE_CONVERT(e, v)((1 ? 0 : (e)) - (v))#define
_GL_INT_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
EXPR_SIGNED(e)(_GL_INT_NEGATE_CONVERT(e, 1) < 0)#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#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 '
EXPR_SIGNED(_GL_INT_CONVERT(ans, 256))' from either '_GL_INT_MINIMUM' or '_GL_INT_MAXIMUM'.'
EXPR_SIGNED(_GL_INT_CONVERT(ans, 256))'
=> 'EXPR_SIGNED((1 ? 0 : (ans)) + (256))'
=> '(_GL_INT_NEGATE_CONVERT(((1 ? 0 : (ans)) + (256)), 1) < 0)' => '((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1))''
_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ans' and '256' is 'unsigned int', '_GL_INT_CONVERT(ans, 256)' is '256U'.Quoted 6 lines
'
EXPR_SIGNED' is designed to check if the given expression is signed. Thus, 'EXPR_SIGNED(256U)' is expected to be false.The combination of '
EXPR_SIGNED(e)' and '_GL_INT_CONVERT(e, v)' is used to test if the common type of the given two variables is signed, and those macros dutifully reflect the fact: the common type of 'ans' and '256' is unsigned.Can we ignore this Coverity issue? Why?
Quoted 141 lines
________________________________________________________________________________________________________
*** CID 435763: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 137 inasn1_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 from gnulib:
#define
_GL_INT_NEGATE_CONVERT(e, v)((1 ? 0 : (e)) - (v))#define
_GL_INT_CONVERT(e, v)((1 ? 0 : (e)) + (v))#define
EXPR_SIGNED(e)(_GL_INT_NEGATE_CONVERT(e, 1) < 0)#define
TYPE_WIDTH(t)(sizeof (t) *CHAR_BIT)#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 _GL_INT_MAXIMUM(e) \ (EXPR_SIGNED (e) \ ? _GL_SIGNED_INT_MAXIMUM (e) \:
_GL_INT_NEGATE_CONVERT(e, 1))#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 the expanded '(a) < (min) / (b)' from
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)'=> '(ans) < \ (EXPR_SIGNED (_GL_INT_CONVERT (ans, 256)) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ans, 256)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (_GL_INT_NEGATE_CONVERT (_GL_INT_CONVERT (ans, 256), 1) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ans, 256)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : (_GL_INT_CONVERT (ans, 256))) - (1)) < 0) \ ? ~ _GL_SIGNED_INT_MAXIMUM (_GL_INT_CONVERT (ans, 256)) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ (((_GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 1) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ans, 256))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : (_GL_INT_CONVERT (ans, 256))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ans, 256))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << (TYPE_WIDTH (+ (_GL_INT_CONVERT (ans, 256))) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ (_GL_INT_CONVERT (ans, 256))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ ((1 ? 0 : (ans)) + (1))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : _GL_INT_CONVERT (_GL_INT_CONVERT (ans, 256), 0)) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ ((1 ? 0 : (ans)) + (1))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : (_GL_INT_CONVERT (ans, 256))) + (0)) ) \ / (256)' => '(ans) < \ (((1 ? 0 : ((1 ? 0 : (ans)) + (256))) - (1)) < 0) \ ? ~ ((((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (1)) << ((sizeof (+ ((1 ? 0 : (ans)) + (1))) * CHAR_BIT) - 2)) - 1) * 2 + 1) \ : ((1 ? 0 : ((1 ? 0 : (ans)) + (256))) + (0)) ) \ / (256)''
_GL_INT_CONVERT(e, v)' returns a value with the common real type of 'e' and 'v' and the value of 'v'. Since the common type of 'unsigned int ans' and '256' is 'unsigned int', '_GL_INT_CONVERT(ans, 256)' is '256U'.'
_GL_INT_MINIMUM(256U)' returns the minimum value of 'unsigned int', i.e. 0.'(ans) < (
_GL_INT_MINIMUM(_GL_INT_CONVERT(ans, 256))) / (256)' => '(ans) < 0 / (256)' => '(ans) < 0'For 'unsigned int ans', the result is always false.
However, in '
INT_MULTIPLY_RANGE_OVERFLOW', 'a' is 'unsigned int ans' and 'b' is '256'. We can skip the statements for 'b < 0' and 'b == 0' and reduce the macro to(a) < 0 \ ? (a) < (min) / (b) \ : (max) / (b) < (a))) \
Quoted 7 lines
Quoted 3 lines
Since '(ans) < 0' is false for 'unsigned int ans', the statement in question, '(a) < (min) / (b)', is always skipped. Thus, the result of the statement doesn't matter.
Same questions as for CID 435774...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel