Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1ppObS-0006q1-Ir for mharc-grub-devel@gnu.org; Thu, 20 Apr 2023 03:21:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ppObQ-0006pq-Ft for grub-devel@gnu.org; Thu, 20 Apr 2023 03:21:12 -0400 Received: from mail-he1eur04on0623.outbound.protection.outlook.com ([2a01:111:f400:fe0d::623] helo=EUR04-HE1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ppObN-0004NJ-1Y for grub-devel@gnu.org; Thu, 20 Apr 2023 03:21:12 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jDM5sUPPLY+vdsb5+DatGm/u8Xj9gDA2b0K8OUEve3Alz0JsjGNeSp5L12eIW1XAxjzfMjF87VGScye1CiNBsIsvW65yCRmTMwTCGTE4wANSe2wcLYS8HAd3aF0MdArRvrqgV2ubNl4FemVBYaPepUmbqR31EYEe4nnXcIv3f9HHDrmt+ikH+fxbzjT4zg5fXBZmS7IYrBD0VxQvlPw9DPFAXS1RCK9htEkfL2aYu6XO6/yYqlWpcnrdUz60LxM7qWgDX9T2QVmuc9NWSeRYQGl0fGuluBiNUFOf7aT++hAawpSNPpOLKyXccYD/UnEcIDvAxZaTvAgGEDEl9bWbyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=gYrizhXAIEVLngIctMklyxm6LnTgtUpZUYCdrL12LHs=; b=eS7Io8krTWXd6TISArg3jOt7aZPf5o+2Cdocx6LQreXKGwMKAeEXEq6S05hMsUen9knISn5ZWNc0NK2Tc7aPVhA8KrY2wTWbsTIP72itk5ZZ3TNYPIjn/2+4LoDfzBwSi5QRUjATWL19p6ExWUiOqe6VzurguMv+pzXz1MLxeRFfytg3rVlWtzRwjmD/dnXJYn+Ib0s5wBKZCy6TYZ2PgRxADtTpks0pJsHeOMLCCBM+3ww/lbet3h6dAtfEYJNNF2GiMeTQ0lonfgIuDJ4H79bG178A2sYvSeYTLXkQLXLWCLs5nMkDNEWjHW3/2B68aUIA3FqnMEB4rgr96zRS9A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gYrizhXAIEVLngIctMklyxm6LnTgtUpZUYCdrL12LHs=; b=kljIiwnQiALvvTxyAU2OkT1CgEKb0MNcxB/U4HFgqTzk8D0vsAqv8gLDy1ns2p5muHVpeDiKIoGY57H22WXBonpL/c3Wi195i2pP2QUuCXV6mQCJUD2WvQdYAUC7wbzLvJeLqwkxPcM/xcxO48yYWRmlpa4G+/yvjLzZe+lDYkJt/fpV3dqXxpPZ3W6c/ghaup94EqVr15SZtmqksgeWmV92bXc0r5UmQAGhwH+vFjGn+/zcb7pnq+HYb4offk2QodpiNDpRsEsvMfFGaaVTlkbTeKuAslTj/WQlbAet8kAU2y50ujOwsuL+DeLuHX7J9WYIz7Y9I8J0sjePunJRiw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Received: from DBBPR04MB6140.eurprd04.prod.outlook.com (2603:10a6:10:c7::18) by PAXPR04MB8094.eurprd04.prod.outlook.com (2603:10a6:102:1c1::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 07:16:02 +0000 Received: from DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::dc0f:b8e1:84c9:2906]) by DBBPR04MB6140.eurprd04.prod.outlook.com ([fe80::dc0f:b8e1:84c9:2906%6]) with mapi id 15.20.6298.045; Thu, 20 Apr 2023 07:16:02 +0000 From: Gary Lin To: The development of GNU GRUB Cc: Hernan Gatta , Daniel Axtens , 'Daniel Kiper ' , shkhisti@microsoft.com, jaskaran.khurana@microsoft.com, christopher.co@microsoft.com, daniel.mihai@microsoft.com, rharwood@redhat.com, jaredz@redhat.com, development@efficientek.com, jejb@linux.ibm.com, mchang@suse.com Subject: [PATCH v4 00/12] Automatic Disk Unlock with TPM2 Date: Thu, 20 Apr 2023 15:15:36 +0800 Message-Id: <20230420071548.28286-1-glin@suse.com> X-Mailer: git-send-email 2.35.3 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: TYCP286CA0063.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:31a::12) To DBBPR04MB6140.eurprd04.prod.outlook.com (2603:10a6:10:c7::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DBBPR04MB6140:EE_|PAXPR04MB8094:EE_ X-MS-Office365-Filtering-Correlation-Id: 08ca6ef7-791a-4798-30e0-08db416f190b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kPnWmvmnHG3ypeuoiazuLKqy0n6s4KaqWia8LMngN/1/hAnIvXzDpQwyX/pnrbk1fE0JPSWyqsWL1eCANHKdZy8XXW+BituW65z+Sb9HFOYWRG+x/xZmbpEIvtKFXO9oLdmpe1RLLHumQZGCGkOLZP9s77AeKSlRFfsqGQJhZ6z47v47QwUX89wKsz7MZIj8jRXV1FHXd3ZPjNoqlvb+cJPpoczmm50k32tp5PvHSEtN1C+GeTVD+sS3JuGxP0tLT5Z8Xgzc3IkYHwSGRTklZql9swWWF08p9hdrlUzuJgMpw/SeGuJ6B7AGAquRBPxPNqJMYKGPOonYHZcXa7E1qy9RujO1Z4d8TmeIMQuRZ74IkFaG9GL8Jrg8yVsyFC+1Qa//Blw/FrRQfkF6IbLwfXGUO6AnO7vU2zBRdbCn6Ep+/jlf7oIwKeOQcsG6OVgUlqf76r8f7Rlzpjj5QTKYX+u2kjBSo5y6ZUnGp+pkh8IjHgDIfzvyKVMeA0FKqh/jw0MpQYhmvpITh1g/bgWtHom2tLi6qL/dLqG1VqCC3yh9E7QlgvpMOi6hn2627fS9sYuZuZyeqxkD1hMCeZ8fpg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DBBPR04MB6140.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(136003)(39860400002)(366004)(396003)(346002)(376002)(451199021)(6916009)(26005)(6512007)(186003)(4326008)(316002)(6506007)(1076003)(8676002)(8936002)(30864003)(2906002)(36756003)(86362001)(5660300002)(7416002)(478600001)(6666004)(54906003)(83380400001)(41300700001)(2616005)(38100700002)(6486002)(66556008)(107886003)(66476007)(66946007)(966005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?YvelqBqZH0OAoH7qBSuPDTmGJpwynrAi4ghWZDARUI0/aZm0JH44deoeD+Nj?= =?us-ascii?Q?y9G2Xhe1JXfMnqRrUpI7k7TUGnNsdVH8kPq+sG3ui3t7QZQl0VDXJBjuZHLa?= =?us-ascii?Q?G8NiGyMRVu+sWd9YuapTsxafXZVKUJtXF4lqj4pUTVVSfxsgcCkSehzDtyd+?= =?us-ascii?Q?nBTs/TZXKbD6Me6VfWLMsOxttdekh8/9qX0B1pqvhyMtnDS9G3XPwqM2Fle9?= =?us-ascii?Q?72CqezzLt+6MNXnHsUdAgmNV0qlAdZByksU/1Mgd5+dgZEk99pA7nuD4NUBF?= =?us-ascii?Q?NgYEFLxpg8Qfi9wsBWV2FGKAyIOc5nd8JDI4MsjE3QQM47oSbJwa4sAMGpvF?= =?us-ascii?Q?bCrgQQ1bA8C4suEIDFXGGMpUq8gO1rJiMd38UNzIRAGBcvEg/T0i9jefpf2H?= =?us-ascii?Q?NgN9xNrlYVyW23kAfl8rf4qCqa7ynLWBN+91Ep3y2EMsXpG8aEXrc5ztrgJg?= =?us-ascii?Q?mNvHZ5nvdU2kBOpGXPYzsx9ew3+PVqvbn92Vae+7jz/MdeYOmbKrAxqxJ+el?= =?us-ascii?Q?5kf2aBY1oUWVIuRc5k1gmvU84T0X4EM2RewfXNBVUX0sJnT/Mxj13OKbNRxi?= =?us-ascii?Q?uDeWMjLiJl+qBACaD/kGO+mUtMU0jA2Uf5RdP/rjZNrNy1HEr+Sh8CUJtmLM?= =?us-ascii?Q?lEFdKSYQUUY24yIky6SF0egHNoZcuBfS+5kLg7OGGvUh/qdcJr6caVjd1QCe?= =?us-ascii?Q?hlcuF8f8KMfhLlgTadYb090FeZoEXJKdkNkW2bWonC5juqrcprm+OIvUlpTf?= =?us-ascii?Q?kgbnL33QUYsF/uydfK9ZgZdvLkKXfz386M6U1DoXH3XaD5f1+uIcj8T/WHRg?= =?us-ascii?Q?a0O0ZVcPsSVhW/X11InEUJg3w7+cMPdRRyyLLgE64AFkrCHacd0nA33w3y2D?= =?us-ascii?Q?WkVXLcNMzeLxf5jaU/mDKHev6PdXQ+S61huQVnT03FohRKtH89LudsqZi9eR?= =?us-ascii?Q?gkhV/4wdOkgVlKnB1wbpGVfTfnKUFbLLqxuD0vRRJRz7L+AAv4hwYZLwJKOT?= =?us-ascii?Q?YKGUcjlL1/dp/vscC4JhOctaudiIGwUcXFLAZ/BLt1MiJSGnxIox/oF/w4fT?= =?us-ascii?Q?w+dOJuSrNHGdNqRSgPJqvJgYP7AOJsNPQSh+u6vNnynnZ45YAhpOxKjin6xA?= =?us-ascii?Q?RcGoJNYeymlkGb13BMPwj/ro3QCDm4ycNqjzzGug6lqAw133LPiErVHT7ehp?= =?us-ascii?Q?LIeKUG1I9dnBlpWav4OyHcXuEWep2NPpXobt28jHkN3y6m5It8twlGY4g44s?= =?us-ascii?Q?Tm0ZVAlP4Zxuq28Z3WSUzBiI3sgWReLTtNRjxFXV4gr1td0mcQ3K06JFWtMm?= =?us-ascii?Q?l0TPCLO7g3xtCkUuQz64BGDFheQ0Ss5k1UHdRGeDYNnekaVOwCF//VSz3xCj?= =?us-ascii?Q?crFOH5jG0zOpe06Cvo9M39QaxGsgVIUG6VBJ/p/w7lLVX031mZAuzHwUjSsZ?= =?us-ascii?Q?cpLFwxPnIWDzb9Q1NkeYyz459eu/7Qq7BZ/yA5fucREpS5zG2mHqWHH37U6u?= =?us-ascii?Q?BlwsoAfPCenC0TcptDWsqV8hNgibQlejESbeCyirCkyfCYbqQmUOvCQG2hju?= =?us-ascii?Q?SpbVkxZL+r0LU+BjdA4iv17QgZgFhYWjgpcOWW3U?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 08ca6ef7-791a-4798-30e0-08db416f190b X-MS-Exchange-CrossTenant-AuthSource: DBBPR04MB6140.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 07:16:02.4970 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: aSnF5mJ+4LDFHaUVY6949/9SQDJMRvKg8goSUbFGvf7W57HldHYY9kAbXHqV56hm X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR04MB8094 Received-SPF: pass client-ip=2a01:111:f400:fe0d::623; envelope-from=GLin@suse.com; helo=EUR04-HE1-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 Apr 2023 07:21:12 -0000 GIT repo for v4: https://github.com/lcp/grub2/tree/tpm2-unlock-v4 This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption. To support TPM 2.0 Key File format(*2), patch 1~6 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch. Patch 7 adds the document for libtasn1 and the steps to upgrade the library. Patch 8~12 are Hernan Gatta's patches with the follow-up fixes and improvements: - Converting 8 spaces into 1 tab - Merging the minor build fix from Michael Chang - Replacing "lu" with "PRIuGRUB_SIZE" for grub_dprintf - Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def - Rebasing "cryptodisk: Support key protectors" to the git master - Removing the measurement on the sealed key - Based ont the patch from Olaf Kirch - Adjusting the input parameters of TPM2_EvictControl to match the order in "TCG TPM2 Part3 Commands" - Declaring the input arguments of TPM2 functions as const - Resending TPM2 commands on TPM_RC_RETRY - Adding checks for the parameters of TPM2 commands - Packing the missing authorization command for TPM2_PCR_Read - Tweaking the TPM2 command functions to allow some parameters to be NULL so that we don't have to declare empty variables - Only enabling grub-protect for "efi" since the TPM2 stack currently relies on the EFI TCG2 protocol to send TPM2 commands - Using grub_cpu_to_be*() in the TPM2 stack instead of grub_swap_bytes*() which may cause problems in big-indian machines - Changing the short name of "--protector" of "cryptomount" from "-k" to "-P" to avoid the conflict with "--key-file" - Supporting TPM 2.0 Key File Format besides the raw sealed key - Adding the external libtasn1 dependency to grub-protect to write the TPM 2.0 Key files To utilize the TPM2 key protector to unlock the encrypted partition (sdb1), here are the sample steps: 1. Add an extra random key for LUKS (luks-key) $ dd if=/dev/urandom of=luks-key bs=1 count=32 $ sudo cryptsetup luksAddKey /dev/sdb1 luks-key --pbkdf=pbkdf2 2. Seal the key $ sudo grub-protect --action=add \ --protector=tpm2 \ --tpm2key \ --tpm2-keyfile=luks-key \ --tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm 3. Unseal the key with the proper commands in grub.cfg: tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -u SDB1_UUID -P tpm2 (*1) https://lists.gnu.org/archive/html/grub-devel/2022-02/msg00006.html (*2) https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html (*3) https://lists.gnu.org/archive/html/grub-devel/2021-06/msg00044.html v4: - Improving the error condition checks in cryptodisk.c - Moving the code to unseal with the standalone policy sequence below the code for authpolicy sequence - The standalone policy sequence was mistakenly prepended to to the authpolicy sequence with grub_list_push() while it should be appended. - Pushing the error messages from the authpolicy sequence into the grub_error stack so that we can list all errors from the sequence - Improving the error messages in the TPM2 protector - Amending the calculation of the max string lengths of 'Policy', 'CommandCode' and 'CommandPolicy' - Skipping the error path in grub_tpm2key_get_authpolicy_seq() on success to avoid freeing the authpolicy sequence v3: - https://lists.gnu.org/archive/html/grub-devel/2023-04/msg00055.html - GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v3 - Adding the document for libtasn1 - Improving the error condition checks ex: "if (!ptr)" ==> "if (ptr == NULL)" "if (err)" ==> "if (err != GRUB_ERR_NONE)" "if (rc)" ==> "if (rc != TPM_RC_SUCCESS)" - Supporting the "TPMPolicy" and "TPMAuthPolicy" sequence in the TPM 2.0 key File - Refactoring the key recover function to support "TPMPolicy" and "TPMAuthPolicy" sequence - Using TPMS_PCR_SELECTION_SelectPCR() to set the PCR bit mask - Also dropping TPM2_PCR_TO_SELECT() and TPM2_PCR_TO_BIT() which are not necessary anymore - Removing the redundant variable, 'crd', from grub_cryptodisk_scan_device_real() - Fixing the spaces/tabs in cryptodisk.c - Fixing the comment format in cryptodisk.h - Adding the defensive check for "cargs->protectors" in grub_cryptodisk_scan_device() - Improving 'grub-protect' for the better support of TPM 2.0 Key File - Adding more comments v2: - https://lists.gnu.org/archive/html/grub-devel/2023-03/msg00094.html - GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v2 v1: - https://lists.gnu.org/archive/html/grub-devel/2023-02/msg00130.html - GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock Daniel Axtens (6): posix_wrap: tweaks in preparation for libtasn1 libtasn1: import libtasn1-4.19.0 libtasn1: disable code not needed in grub libtasn1: changes for grub compatibility libtasn1: compile into asn1 module test_asn1: test module for libtasn1 Gary Lin (1): libtasn1: Add the documentation Hernan Gatta (5): protectors: Add key protectors framework tpm2: Add TPM Software Stack (TSS) protectors: Add TPM2 Key Protector cryptodisk: Support key protectors util/grub-protect: Add new tool .gitignore | 2 + Makefile.util.def | 29 + configure.ac | 9 + docs/grub-dev.texi | 27 + grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 42 + grub-core/disk/cryptodisk.c | 172 +- grub-core/kern/protectors.c | 75 + ...asn1-disable-code-not-needed-in-grub.patch | 311 ++ ...tasn1-changes-for-grub-compatibility.patch | 209 ++ grub-core/lib/libtasn1/COPYING | 16 + grub-core/lib/libtasn1/README.md | 98 + grub-core/lib/libtasn1/lib/coding.c | 1433 ++++++++++ grub-core/lib/libtasn1/lib/decoding.c | 2504 +++++++++++++++++ grub-core/lib/libtasn1/lib/element.c | 1110 ++++++++ grub-core/lib/libtasn1/lib/element.h | 42 + grub-core/lib/libtasn1/lib/errors.c | 103 + grub-core/lib/libtasn1/lib/gstr.c | 74 + grub-core/lib/libtasn1/lib/gstr.h | 50 + grub-core/lib/libtasn1/lib/int.h | 221 ++ grub-core/lib/libtasn1/lib/parser_aux.c | 1179 ++++++++ grub-core/lib/libtasn1/lib/parser_aux.h | 172 ++ grub-core/lib/libtasn1/lib/structure.c | 1227 ++++++++ grub-core/lib/libtasn1/lib/structure.h | 46 + .../tests/CVE-2018-1000654-1_asn1_tab.h | 32 + .../tests/CVE-2018-1000654-2_asn1_tab.h | 36 + .../libtasn1_wrap/tests/CVE-2018-1000654.c | 61 + .../lib/libtasn1_wrap/tests/Test_overflow.c | 138 + .../lib/libtasn1_wrap/tests/Test_simple.c | 207 ++ .../lib/libtasn1_wrap/tests/Test_strings.c | 150 + .../libtasn1_wrap/tests/object-id-decoding.c | 116 + .../libtasn1_wrap/tests/object-id-encoding.c | 120 + .../lib/libtasn1_wrap/tests/octet-string.c | 211 ++ .../lib/libtasn1_wrap/tests/reproducers.c | 81 + grub-core/lib/libtasn1_wrap/wrap.c | 26 + grub-core/lib/libtasn1_wrap/wrap_tests.c | 75 + grub-core/lib/libtasn1_wrap/wrap_tests.h | 38 + grub-core/lib/posix_wrap/limits.h | 1 + grub-core/lib/posix_wrap/stdlib.h | 8 + grub-core/lib/posix_wrap/sys/types.h | 1 + grub-core/tpm2/args.c | 131 + grub-core/tpm2/buffer.c | 145 + grub-core/tpm2/module.c | 1033 +++++++ grub-core/tpm2/mu.c | 807 ++++++ grub-core/tpm2/tcg2.c | 143 + grub-core/tpm2/tpm2.c | 761 +++++ grub-core/tpm2/tpm2key.asn | 31 + grub-core/tpm2/tpm2key.c | 440 +++ grub-core/tpm2/tpm2key_asn1_tab.c | 41 + include/grub/cryptodisk.h | 16 + include/grub/libtasn1.h | 645 +++++ include/grub/protector.h | 48 + include/grub/tpm2/buffer.h | 65 + include/grub/tpm2/internal/args.h | 39 + include/grub/tpm2/internal/functions.h | 117 + include/grub/tpm2/internal/structs.h | 675 +++++ include/grub/tpm2/internal/types.h | 372 +++ include/grub/tpm2/mu.h | 292 ++ include/grub/tpm2/tcg2.h | 34 + include/grub/tpm2/tpm2.h | 34 + include/grub/tpm2/tpm2key.h | 83 + tests/test_asn1.in | 12 + util/grub-protect.c | 1508 ++++++++++ 63 files changed, 17894 insertions(+), 31 deletions(-) create mode 100644 grub-core/kern/protectors.c create mode 100644 grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch create mode 100644 grub-core/lib/libtasn1-patches/0002-libtasn1-changes-for-grub-compatibility.patch create mode 100644 grub-core/lib/libtasn1/COPYING create mode 100644 grub-core/lib/libtasn1/README.md create mode 100644 grub-core/lib/libtasn1/lib/coding.c create mode 100644 grub-core/lib/libtasn1/lib/decoding.c create mode 100644 grub-core/lib/libtasn1/lib/element.c create mode 100644 grub-core/lib/libtasn1/lib/element.h create mode 100644 grub-core/lib/libtasn1/lib/errors.c create mode 100644 grub-core/lib/libtasn1/lib/gstr.c create mode 100644 grub-core/lib/libtasn1/lib/gstr.h create mode 100644 grub-core/lib/libtasn1/lib/int.h create mode 100644 grub-core/lib/libtasn1/lib/parser_aux.c create mode 100644 grub-core/lib/libtasn1/lib/parser_aux.h create mode 100644 grub-core/lib/libtasn1/lib/structure.c create mode 100644 grub-core/lib/libtasn1/lib/structure.h create mode 100644 grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-1_asn1_tab.h create mode 100644 grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654-2_asn1_tab.h create mode 100644 grub-core/lib/libtasn1_wrap/tests/CVE-2018-1000654.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/Test_overflow.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/Test_simple.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/Test_strings.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/object-id-decoding.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/object-id-encoding.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/octet-string.c create mode 100644 grub-core/lib/libtasn1_wrap/tests/reproducers.c create mode 100644 grub-core/lib/libtasn1_wrap/wrap.c create mode 100644 grub-core/lib/libtasn1_wrap/wrap_tests.c create mode 100644 grub-core/lib/libtasn1_wrap/wrap_tests.h create mode 100644 grub-core/tpm2/args.c create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/module.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.c create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 grub-core/tpm2/tpm2key.asn create mode 100644 grub-core/tpm2/tpm2key.c create mode 100644 grub-core/tpm2/tpm2key_asn1_tab.c create mode 100644 include/grub/libtasn1.h create mode 100644 include/grub/protector.h create mode 100644 include/grub/tpm2/buffer.h create mode 100644 include/grub/tpm2/internal/args.h create mode 100644 include/grub/tpm2/internal/functions.h create mode 100644 include/grub/tpm2/internal/structs.h create mode 100644 include/grub/tpm2/internal/types.h create mode 100644 include/grub/tpm2/mu.h create mode 100644 include/grub/tpm2/tcg2.h create mode 100644 include/grub/tpm2/tpm2.h create mode 100644 include/grub/tpm2/tpm2key.h create mode 100644 tests/test_asn1.in create mode 100644 util/grub-protect.c Range-diff against v3: 1: f2a39b639 = 1: 9167c663e posix_wrap: tweaks in preparation for libtasn1 2: 493f2dc86 = 2: 1b4a9c12b libtasn1: import libtasn1-4.19.0 3: c1c345915 = 3: 2821434ad libtasn1: disable code not needed in grub 4: 5d84feb27 = 4: 98ec778a1 libtasn1: changes for grub compatibility 5: 21441d802 = 5: e3aa6f3f9 libtasn1: compile into asn1 module 6: 7064cf0bc = 6: de837eca0 test_asn1: test module for libtasn1 7: 5485acfb8 = 7: 4b1507c28 libtasn1: Add the documentation 8: 327bb228f = 8: 5affde982 protectors: Add key protectors framework 9: ced33130b = 9: a4f5c4aa6 tpm2: Add TPM Software Stack (TSS) 10: 2249db295 ! 10: ab730e5bc protectors: Add TPM2 Key Protector @@ grub-core/tpm2/module.c (new) + if (sealed_key_size > buf.cap) + { + grub_dprintf ("tpm2", "Sealed key file is larger than decode buffer " -+ "(%" PRIuGRUB_SIZE " vs %" PRIuGRUB_SIZE " bytes).\n", sealed_key_size, buf.cap); ++ "(%" PRIuGRUB_SIZE " vs %" PRIuGRUB_SIZE " bytes).\n", ++ sealed_key_size, buf.cap); + return GRUB_ERR_BAD_ARGUMENT; + } + @@ grub-core/tpm2/module.c (new) + if (sealed_pub_size + sealed_priv_size > buf.cap) + { + grub_dprintf ("tpm2", "Sealed key is larger than decode buffer " -+ "(%" PRIuGRUB_SIZE " vs %" PRIuGRUB_SIZE " bytes).\n", sealed_pub_size, buf.cap); ++ "(%" PRIuGRUB_SIZE " vs %" PRIuGRUB_SIZE " bytes).\n", ++ sealed_pub_size, buf.cap); + err = GRUB_ERR_BAD_ARGUMENT; + goto error; + } @@ grub-core/tpm2/module.c (new) + &creationData, &creationHash, &creationTicket, + &srkName, NULL); + if (rc != TPM_RC_SUCCESS) -+ { -+ grub_dprintf ("tpm2", "Could not create SRK (TPM2_CreatePrimary failed " -+ "with TSS/TPM error %u).\n", rc); -+ return GRUB_ERR_BAD_DEVICE; -+ } ++ return grub_error (GRUB_ERR_BAD_DEVICE, ++ N_("Could not create SRK (TPM2_CreatePrimary: 0x%x)"), ++ rc); + + *srk = srkHandle; + @@ grub-core/tpm2/module.c (new) + TPM2B_DIGEST pcr_digest; + TPML_PCR_SELECTION pcr_sel; + TPM_RC rc; -+ grub_err_t err; + + grub_tpm2_mu_TPM2B_DIGEST_Unmarshal (cmd_buf, &pcr_digest); + grub_tpm2_mu_TPML_PCR_SELECTION_Unmarshal (cmd_buf, &pcr_sel); + if (cmd_buf->error) -+ { -+ err = GRUB_ERR_BAD_ARGUMENT; -+ return grub_error (err, N_("Failed to unmarshal the buffer for " -+ "TPM2_PolicyPCR")); -+ } ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, ++ N_("Failed to unmarshal CommandPolicy for TPM2_PolicyPCR")); + + rc = TPM2_PolicyPCR (session, NULL, &pcr_digest, &pcr_sel, NULL); + if (rc != TPM_RC_SUCCESS) -+ { -+ err = GRUB_ERR_BAD_DEVICE; -+ grub_error (err, N_("Failed to submit PCR policy (TPM2_PolicyPCR " -+ "failed with TSS/TPM error %u)"), rc); -+ return err; -+ } ++ return grub_error (GRUB_ERR_BAD_DEVICE, ++ N_("Failed to submit PCR policy (TPM2_PolicyPCR: 0x%x)"), ++ rc); + + return GRUB_ERR_NONE; +} @@ grub-core/tpm2/module.c (new) + + grub_tpm2_buffer_init (&buf); + if (policy->cmd_policy_len > buf.cap) -+ { -+ return GRUB_ERR_BAD_ARGUMENT; -+ } ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, ++ N_("CommandPolicy larger than TPM buffer")); + + grub_memcpy (buf.data, policy->cmd_policy, policy->cmd_policy_len); + buf.size = policy->cmd_policy_len; @@ grub-core/tpm2/module.c (new) + err = grub_tpm2_protector_policypcr (session, &buf); + break; + default: -+ err = GRUB_ERR_BAD_ARGUMENT; -+ grub_error (err, N_("Unknown TPM Command: %x"), policy->cmd_code); ++ return grub_error (GRUB_ERR_BAD_ARGUMENT, ++ N_("Unknown TPM Command: 0x%x"), policy->cmd_code); + } + + return err; @@ grub-core/tpm2/module.c (new) + TPM_RC rc; + grub_err_t err; + -+ err = GRUB_ERR_BAD_DEVICE; -+ + /* Start Auth Session */ + nonceCaller.size = TPM_SHA256_DIGEST_SIZE; + symmetric.algorithm = TPM_ALG_NULL; @@ grub-core/tpm2/module.c (new) + TPM_SE_POLICY, &symmetric, TPM_ALG_SHA256, + &session, NULL, NULL); + if (rc != TPM_RC_SUCCESS) -+ { -+ grub_error (err, N_("Failed to start auth session (TPM2_StartAuthSession " -+ "failed with TSS/TPM error %u)"), rc); -+ goto error; -+ } ++ return grub_error (GRUB_ERR_BAD_DEVICE, ++ N_("Failed to start auth session (TPM2_StartAuthSession: " ++ "0x%x)"), rc); + + /* Enforce the policy command sequence */ + err = grub_tpm2_protector_enforce_policy_seq (policy_seq, session); @@ grub-core/tpm2/module.c (new) + rc = TPM2_Unseal (sealed_handle, &authCmd, &data, NULL); + if (rc != TPM_RC_SUCCESS) + { -+ grub_error (err, N_("Failed to unseal sealed key (TPM2_Unseal failed " -+ "with TSS/TPM error %u)"), rc); ++ err = GRUB_ERR_BAD_DEVICE; ++ grub_error (err, N_("Failed to unseal sealed key (TPM2_Unseal: 0x%x)"), ++ rc); + goto error; + } + @@ grub-core/tpm2/module.c (new) + grub_uint8_t **key, grub_size_t *key_size) +{ + TPMS_AUTH_COMMAND authCmd = { 0 }; -+ TPM2_SEALED_KEY sealed_key; -+ TPM2B_NAME name; -+ void *file_bytes; -+ grub_size_t file_size; ++ TPM2_SEALED_KEY sealed_key = { 0 }; ++ TPM2B_NAME name = { 0 }; ++ void *file_bytes = NULL; ++ grub_size_t file_size = 0; + TPM_HANDLE parent_handle = 0; -+ TPM_HANDLE srk_handle; -+ TPM_HANDLE sealed_handle; ++ TPM_HANDLE srk_handle = 0; ++ TPM_HANDLE sealed_handle = 0; + tpm2key_policy_t policy_seq = NULL; + tpm2key_authpolicy_t authpol = NULL; + tpm2key_authpolicy_t authpol_seq = NULL; @@ grub-core/tpm2/module.c (new) + } + } + -+ /* -+ * Create a basic policy sequence based on the given PCR selection if the -+ * key file doesn't provide any policy or authpolicy sequence -+ */ -+ if (policy_seq == NULL) -+ { -+ err = grub_tpm2_protector_simple_policy_seq (ctx, &policy_seq); -+ if (err != GRUB_ERR_NONE) -+ goto exit1; -+ } -+ -+ /* -+ * Append the standalone policy sequence into the authpolicy sequence as -+ * the fallback -+ */ -+ authpol = grub_malloc (sizeof (struct tpm2key_authpolicy)); -+ if (authpol == NULL) -+ { -+ /* -+ * Free policy_seq here since it's not included in authpol_seq -+ * yet, and grub_tpm2key_free_policy_seq() won't be able to -+ * free it. -+ */ -+ grub_tpm2key_free_policy_seq (policy_seq); -+ err = GRUB_ERR_OUT_OF_MEMORY; -+ goto exit1; -+ } -+ authpol->policy_seq = policy_seq; -+ grub_list_push (GRUB_AS_LIST_P (&authpol_seq), GRUB_AS_LIST (authpol)); -+ + /* Get the SRK to unseal the sealed key */ + err = grub_tpm2_protector_srk_get (ctx, parent_handle, &srk_handle); + if (err != GRUB_ERR_NONE) @@ grub-core/tpm2/module.c (new) + &sealed_handle, &name, NULL); + if (rc != TPM_RC_SUCCESS) + { -+ grub_error (err, N_("Failed to load sealed key (TPM2_Load failed " -+ "with TSS/TPM error %u)"), rc); ++ err = GRUB_ERR_BAD_DEVICE; ++ grub_error (err, N_("Failed to load sealed key (TPM2_Load: 0x%x)"), rc); + goto exit2; + } + ++ err = GRUB_ERR_READ_ERROR; ++ + /* Iterate the authpolicy sequence to find one that unseals the key */ + FOR_LIST_ELEMENTS (authpol, authpol_seq) + { @@ grub-core/tpm2/module.c (new) + key, key_size); + if (err == GRUB_ERR_NONE) + break; ++ ++ /* ++ * Push the error message into the grub_error stack ++ * Note: The grub_error stack may overflow if there are too many policy ++ * sequences. Anyway, we still can keep the error messages from ++ * the first few policy sequences which are usually most likely to ++ * unseal the key. ++ */ ++ grub_error_push(); + } + ++ /* Give the standalone policy sequence a try */ ++ if (err != GRUB_ERR_NONE) ++ { ++ /* ++ * Create a basic policy sequence based on the given PCR selection if the ++ * key file doesn't provide one ++ */ ++ if (policy_seq == NULL) ++ { ++ err = grub_tpm2_protector_simple_policy_seq (ctx, &policy_seq); ++ if (err != GRUB_ERR_NONE) ++ goto exit3; ++ } ++ ++ err = grub_tpm2_protector_unseal (policy_seq, sealed_handle, key, key_size); ++ } ++ ++ /* Pop error messages on success */ ++ if (err == GRUB_ERR_NONE) ++ while (grub_error_pop ()); ++ ++exit3: + TPM2_FlushContext (sealed_handle); + +exit2: + TPM2_FlushContext (srk_handle); + +exit1: ++ grub_tpm2key_free_policy_seq (policy_seq); + grub_tpm2key_free_authpolicy_seq (authpol_seq); + grub_free (file_bytes); + return err; @@ grub-core/tpm2/tpm2key.c (new) +} + +/* ++ * The string to fetch 'Policy' from 'authPolicy': ++ * authPolicy.?XX.Policy ++ */ ++#define AUTHPOLICY_POL_MAX_STR "authPolicy.?XX.Policy" ++#define AUTHPOLICY_POL_MAX (sizeof (AUTHPOLICY_POL_MAX_STR)) ++ ++/* + * Expected strings for CommandCode and CommandPolicy: + * policy.?XX.CommandCode + * policy.?XX.CommandPolicy + * authPolicy.?XX.Policy.?YY.CommandCode + * authPolicy.?XX.Policy.?YY.CommandPolicy + */ -+#define CMD_CODE_MAX (sizeof ("authPolicy.?XX.Policy.?YY.CommandCode")) -+#define CMD_POL_MAX (sizeof ("authPolicy.?XX.Policy.?YY.CommandPolicy")) ++#define CMD_CODE_MAX_STR AUTHPOLICY_POL_MAX_STR".?YY.CommandCode" ++#define CMD_POL_MAX_STR AUTHPOLICY_POL_MAX_STR".?YY.CommandPolicy" ++#define CMD_CODE_MAX (sizeof (CMD_CODE_MAX_STR)) ++#define CMD_POL_MAX (sizeof (CMD_POL_MAX_STR)) + +static int +tpm2key_get_policy_seq (asn1_node tpm2key, const char *prefix, @@ grub-core/tpm2/tpm2key.c (new) + } +} + -+#define AUTHPOLICY_POL_MAX (sizeof ("authPolicy.?XX.Policy")) -+ +grub_err_t +grub_tpm2key_get_authpolicy_seq (asn1_node tpm2key, tpm2key_authpolicy_t *authpol_seq) +{ @@ grub-core/tpm2/tpm2key.c (new) + + *authpol_seq = tmp_seq; + ++ return GRUB_ERR_NONE; ++ +error: + if (authpol) + { 11: 9ad473fd8 ! 11: d7c38672a cryptodisk: Support key protectors @@ Commit message Signed-off-by: Hernan Gatta Signed-off-by: Michael Chang Signed-off-by: Gary Lin + Reviewed-by: Glenn Washburn ## Makefile.util.def ## @@ Makefile.util.def: library = { @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_scan_device_real (const char *name, - } - cargs->key_len = grub_strlen ((char *) cargs->key_data); - } -+ if (!dev) ++ if (dev == NULL) + { + grub_error (GRUB_ERR_BAD_MODULE, + "no cryptodisk module can handle this device"); @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_scan_device_real (const char *name, + if (cargs->key_cache[i].invalid) + continue; + -+ if (!cargs->key_cache[i].key) ++ if (cargs->key_cache[i].key == NULL) + { + ret = grub_key_protector_recover_key (cargs->protectors[i], + &cargs->key_cache[i].key, @@ grub-core/disk/cryptodisk.c: grub_cmd_cryptomount (grub_extcmd_context_t ctxt, i + if (state[OPTION_PROTECTOR].set) /* key protector(s) */ + { + cargs.key_cache = grub_zalloc (state[OPTION_PROTECTOR].set * sizeof (*cargs.key_cache)); -+ if (!cargs.key_cache) ++ if (cargs.key_cache == NULL) + return grub_error (GRUB_ERR_OUT_OF_MEMORY, + "no memory for key protector key cache"); + cargs.protectors = state[OPTION_PROTECTOR].args; 12: aa6210c44 = 12: 38ebade13 util/grub-protect: Add new tool -- 2.35.3