Angel Pons | 986d50e | 2020-04-02 23:48:53 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 2 | |
Arthur Heymans | 6f8e944 | 2021-03-29 14:23:53 +0200 | [diff] [blame^] | 3 | #include <rules.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 4 | #include <console/cbmem_console.h> |
| 5 | #include <console/console.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 6 | #include <security/tpm/tspi/crtm.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 7 | #include <security/tpm/tspi.h> |
| 8 | #include <security/tpm/tss.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <security/vboot/misc.h> |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 11 | #include <vb2_api.h> |
Joel Kitching | 2eb89c8 | 2019-04-25 17:45:12 +0800 | [diff] [blame] | 12 | #include <vb2_sha.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 13 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 14 | #if CONFIG(TPM1) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 15 | static uint32_t tpm1_invoke_state_machine(void) |
| 16 | { |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 17 | uint8_t disabled; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 18 | uint8_t deactivated; |
| 19 | uint32_t result = TPM_SUCCESS; |
| 20 | |
| 21 | /* Check that the TPM is enabled and activated. */ |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 22 | result = tlcl_get_flags(&disabled, &deactivated, NULL); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 23 | if (result != TPM_SUCCESS) { |
| 24 | printk(BIOS_ERR, "TPM: Can't read capabilities.\n"); |
| 25 | return result; |
| 26 | } |
| 27 | |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 28 | if (disabled) { |
| 29 | printk(BIOS_INFO, "TPM: is disabled. Enabling...\n"); |
| 30 | |
| 31 | result = tlcl_set_enable(); |
| 32 | if (result != TPM_SUCCESS) { |
| 33 | printk(BIOS_ERR, "TPM: Can't set enabled state.\n"); |
| 34 | return result; |
| 35 | } |
| 36 | } |
| 37 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 38 | if (!!deactivated != CONFIG(TPM_DEACTIVATE)) { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 39 | printk(BIOS_INFO, |
| 40 | "TPM: Unexpected TPM deactivated state. Toggling...\n"); |
| 41 | result = tlcl_set_deactivated(!deactivated); |
| 42 | if (result != TPM_SUCCESS) { |
| 43 | printk(BIOS_ERR, |
| 44 | "TPM: Can't toggle deactivated state.\n"); |
| 45 | return result; |
| 46 | } |
| 47 | |
| 48 | deactivated = !deactivated; |
| 49 | result = TPM_E_MUST_REBOOT; |
| 50 | } |
| 51 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 52 | return result; |
| 53 | } |
| 54 | #endif |
| 55 | |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 56 | static uint32_t tpm_setup_s3_helper(void) |
| 57 | { |
| 58 | uint32_t result; |
| 59 | |
| 60 | result = tlcl_resume(); |
| 61 | switch (result) { |
| 62 | case TPM_SUCCESS: |
| 63 | break; |
| 64 | |
| 65 | case TPM_E_INVALID_POSTINIT: |
| 66 | /* |
| 67 | * We're on a platform where the TPM maintains power |
| 68 | * in S3, so it's already initialized. |
| 69 | */ |
| 70 | printk(BIOS_INFO, "TPM: Already initialized.\n"); |
| 71 | result = TPM_SUCCESS; |
| 72 | break; |
| 73 | |
| 74 | default: |
| 75 | printk(BIOS_ERR, "TPM: Resume failed (%#x).\n", result); |
| 76 | break; |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | static uint32_t tpm_setup_epilogue(uint32_t result) |
| 83 | { |
| 84 | if (result != TPM_SUCCESS) |
| 85 | post_code(POST_TPM_FAILURE); |
| 86 | else |
| 87 | printk(BIOS_INFO, "TPM: setup succeeded\n"); |
| 88 | |
| 89 | return result; |
| 90 | } |
| 91 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 92 | static int tpm_is_setup; |
| 93 | static inline int tspi_tpm_is_setup(void) |
| 94 | { |
Julius Werner | 23a82e8 | 2020-03-31 13:32:10 -0700 | [diff] [blame] | 95 | /* |
| 96 | * vboot_logic_executed() only starts returning true at the end of |
| 97 | * verstage, but the vboot logic itself already wants to extend PCRs |
| 98 | * before that. So in the stage where verification actually runs, we |
| 99 | * need to check tpm_is_setup. Skip that check in all other stages so |
| 100 | * this whole function can be evaluated at compile time. |
| 101 | */ |
| 102 | if (CONFIG(VBOOT)) { |
| 103 | if (verification_should_run()) |
| 104 | return tpm_is_setup; |
| 105 | return vboot_logic_executed(); |
| 106 | } |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 107 | |
Arthur Heymans | 6f8e944 | 2021-03-29 14:23:53 +0200 | [diff] [blame^] | 108 | if (CONFIG(TPM_MEASURED_BOOT_INIT_BOOTBLOCK)) |
| 109 | return ENV_BOOTBLOCK ? tpm_is_setup : 1; |
| 110 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 111 | if (ENV_RAMSTAGE) |
| 112 | return tpm_is_setup; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 117 | /* |
| 118 | * tpm_setup starts the TPM and establishes the root of trust for the |
Jonathan Neuschäfer | 61322d7 | 2018-10-29 22:52:42 +0100 | [diff] [blame] | 119 | * anti-rollback mechanism. tpm_setup can fail for three reasons. 1 A bug. |
| 120 | * 2 a TPM hardware failure. 3 An unexpected TPM state due to some attack. In |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 121 | * general we cannot easily distinguish the kind of failure, so our strategy is |
Jonathan Neuschäfer | 61322d7 | 2018-10-29 22:52:42 +0100 | [diff] [blame] | 122 | * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 123 | * again, which executes (almost) the same sequence of operations. There is a |
| 124 | * good chance that, if recovery mode was entered because of a TPM failure, the |
| 125 | * failure will repeat itself. (In general this is impossible to guarantee |
| 126 | * because we have no way of creating the exact TPM initial state at the |
| 127 | * previous boot.) In recovery mode, we ignore the failure and continue, thus |
| 128 | * giving the recovery kernel a chance to fix things (that's why we don't set |
| 129 | * bGlobalLock). The choice is between a knowingly insecure device and a |
| 130 | * bricked device. |
| 131 | * |
| 132 | * As a side note, observe that we go through considerable hoops to avoid using |
| 133 | * the STCLEAR permissions for the index spaces. We do this to avoid writing |
| 134 | * to the TPM flashram at every reboot or wake-up, because of concerns about |
| 135 | * the durability of the NVRAM. |
| 136 | */ |
| 137 | uint32_t tpm_setup(int s3flag) |
| 138 | { |
| 139 | uint32_t result; |
| 140 | |
| 141 | result = tlcl_lib_init(); |
| 142 | if (result != TPM_SUCCESS) { |
| 143 | printk(BIOS_ERR, "TPM: Can't initialize.\n"); |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 144 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* Handle special init for S3 resume path */ |
| 148 | if (s3flag) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 149 | printk(BIOS_INFO, "TPM: Handle S3 resume.\n"); |
| 150 | return tpm_setup_epilogue(tpm_setup_s3_helper()); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | result = tlcl_startup(); |
Arthur Heymans | 6d5fcf4 | 2019-10-14 17:06:27 +0200 | [diff] [blame] | 154 | if (CONFIG(TPM_STARTUP_IGNORE_POSTINIT) |
| 155 | && result == TPM_E_INVALID_POSTINIT) { |
| 156 | printk(BIOS_DEBUG, "TPM: ignoring invalid POSTINIT\n"); |
| 157 | result = TPM_SUCCESS; |
| 158 | } |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 159 | if (result != TPM_SUCCESS) { |
| 160 | printk(BIOS_ERR, "TPM: Can't run startup command.\n"); |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 161 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | result = tlcl_assert_physical_presence(); |
| 165 | if (result != TPM_SUCCESS) { |
| 166 | /* |
| 167 | * It is possible that the TPM was delivered with the physical |
| 168 | * presence command disabled. This tries enabling it, then |
| 169 | * tries asserting PP again. |
| 170 | */ |
| 171 | result = tlcl_physical_presence_cmd_enable(); |
| 172 | if (result != TPM_SUCCESS) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 173 | printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n"); |
| 174 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | result = tlcl_assert_physical_presence(); |
| 178 | if (result != TPM_SUCCESS) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 179 | printk(BIOS_ERR, "TPM: Can't assert physical presence.\n"); |
| 180 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 184 | #if CONFIG(TPM1) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 185 | result = tpm1_invoke_state_machine(); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 186 | #endif |
Arthur Heymans | 6f8e944 | 2021-03-29 14:23:53 +0200 | [diff] [blame^] | 187 | if (CONFIG(TPM_MEASURED_BOOT) && !CONFIG(TPM_MEASURED_BOOT_INIT_BOOTBLOCK)) |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 188 | result = tspi_measure_cache_to_pcr(); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 189 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 190 | tpm_is_setup = 1; |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 191 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | uint32_t tpm_clear_and_reenable(void) |
| 195 | { |
| 196 | uint32_t result; |
| 197 | |
| 198 | printk(BIOS_INFO, "TPM: Clear and re-enable\n"); |
| 199 | result = tlcl_force_clear(); |
| 200 | if (result != TPM_SUCCESS) { |
| 201 | printk(BIOS_ERR, "TPM: Can't initiate a force clear.\n"); |
| 202 | return result; |
| 203 | } |
| 204 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 205 | #if CONFIG(TPM1) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 206 | result = tlcl_set_enable(); |
| 207 | if (result != TPM_SUCCESS) { |
| 208 | printk(BIOS_ERR, "TPM: Can't set enabled state.\n"); |
| 209 | return result; |
| 210 | } |
| 211 | |
| 212 | result = tlcl_set_deactivated(0); |
| 213 | if (result != TPM_SUCCESS) { |
| 214 | printk(BIOS_ERR, "TPM: Can't set deactivated state.\n"); |
| 215 | return result; |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | return TPM_SUCCESS; |
| 220 | } |
| 221 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 222 | uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo, |
| 223 | uint8_t *digest, size_t digest_len, const char *name) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 224 | { |
Philipp Deppenwiese | f849972 | 2018-07-30 01:27:47 +0200 | [diff] [blame] | 225 | uint32_t result; |
| 226 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 227 | if (!digest) |
| 228 | return TPM_E_IOERROR; |
| 229 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 230 | if (tspi_tpm_is_setup()) { |
| 231 | result = tlcl_lib_init(); |
| 232 | if (result != TPM_SUCCESS) { |
| 233 | printk(BIOS_ERR, "TPM: Can't initialize library.\n"); |
| 234 | return result; |
| 235 | } |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 236 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 237 | printk(BIOS_DEBUG, "TPM: Extending digest for %s into PCR %d\n", name, pcr); |
| 238 | result = tlcl_extend(pcr, digest, NULL); |
| 239 | if (result != TPM_SUCCESS) |
| 240 | return result; |
| 241 | } |
| 242 | |
| 243 | if (CONFIG(TPM_MEASURED_BOOT)) |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 244 | tcpa_log_add_table_entry(name, pcr, digest_algo, |
| 245 | digest, digest_len); |
| 246 | |
Furquan Shaikh | 38f3ffa | 2018-07-31 14:26:39 -0700 | [diff] [blame] | 247 | return TPM_SUCCESS; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 248 | } |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 249 | |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 250 | #if CONFIG(VBOOT_LIB) |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 251 | uint32_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr, |
| 252 | const char *rname) |
| 253 | { |
| 254 | uint8_t digest[TPM_PCR_MAX_LEN], digest_len; |
| 255 | uint8_t buf[HASH_DATA_CHUNK_SIZE]; |
| 256 | uint32_t result, offset; |
| 257 | size_t len; |
| 258 | struct vb2_digest_context ctx; |
| 259 | enum vb2_hash_algorithm hash_alg; |
| 260 | |
| 261 | if (!rdev || !rname) |
| 262 | return TPM_E_INVALID_ARG; |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 263 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 264 | if (CONFIG(TPM1)) { |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 265 | hash_alg = VB2_HASH_SHA1; |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 266 | } else { /* CONFIG_TPM2 */ |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 267 | hash_alg = VB2_HASH_SHA256; |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 268 | } |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 269 | |
| 270 | digest_len = vb2_digest_size(hash_alg); |
| 271 | assert(digest_len <= sizeof(digest)); |
| 272 | if (vb2_digest_init(&ctx, hash_alg)) { |
| 273 | printk(BIOS_ERR, "TPM: Error initializing hash.\n"); |
| 274 | return TPM_E_HASH_ERROR; |
| 275 | } |
| 276 | /* |
| 277 | * Though one can mmap the full needed region on x86 this is not the |
| 278 | * case for e.g. ARM. In order to make this code as universal as |
| 279 | * possible across different platforms read the data to hash in chunks. |
| 280 | */ |
| 281 | for (offset = 0; offset < region_device_sz(rdev); offset += len) { |
| 282 | len = MIN(sizeof(buf), region_device_sz(rdev) - offset); |
| 283 | if (rdev_readat(rdev, buf, offset, len) < 0) { |
| 284 | printk(BIOS_ERR, "TPM: Not able to read region %s.\n", |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 285 | rname); |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 286 | return TPM_E_READ_FAILURE; |
| 287 | } |
| 288 | if (vb2_digest_extend(&ctx, buf, len)) { |
| 289 | printk(BIOS_ERR, "TPM: Error extending hash.\n"); |
| 290 | return TPM_E_HASH_ERROR; |
| 291 | } |
| 292 | } |
| 293 | if (vb2_digest_finalize(&ctx, digest, digest_len)) { |
| 294 | printk(BIOS_ERR, "TPM: Error finalizing hash.\n"); |
| 295 | return TPM_E_HASH_ERROR; |
| 296 | } |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 297 | result = tpm_extend_pcr(pcr, hash_alg, digest, digest_len, rname); |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 298 | if (result != TPM_SUCCESS) { |
| 299 | printk(BIOS_ERR, "TPM: Extending hash into PCR failed.\n"); |
| 300 | return result; |
| 301 | } |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 302 | printk(BIOS_DEBUG, "TPM: Digest of %s to PCR %d %s\n", |
| 303 | rname, pcr, tspi_tpm_is_setup() ? "measured" : "logged"); |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 304 | return TPM_SUCCESS; |
| 305 | } |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 306 | #endif /* VBOOT_LIB */ |