| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Functions for querying, manipulating and locking rollback indices | 5 * Functions for querying, manipulating and locking rollback indices |
| 6 * stored in the TPM NVRAM. | 6 * stored in the TPM NVRAM. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "rollback_index.h" | 9 #include "rollback_index.h" |
| 10 | 10 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 static uint32_t SafeWrite(uint32_t index, uint8_t* data, uint32_t length) { | 40 static uint32_t SafeWrite(uint32_t index, uint8_t* data, uint32_t length) { |
| 41 uint32_t result = TlclWrite(index, data, length); | 41 uint32_t result = TlclWrite(index, data, length); |
| 42 if (result == TPM_E_MAXNVWRITES) { | 42 if (result == TPM_E_MAXNVWRITES) { |
| 43 RETURN_ON_FAILURE(TPMClearAndReenable()); | 43 RETURN_ON_FAILURE(TPMClearAndReenable()); |
| 44 return TlclWrite(index, data, length); | 44 return TlclWrite(index, data, length); |
| 45 } else { | 45 } else { |
| 46 return result; | 46 return result; |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 /* Similarly to SafeWrite(), this ensures we don't fail a DefineSpace because |
| 51 * we hit the TPM write limit. This is even less likely to happen than with |
| 52 * writes because we only define spaces once at initialization, but we'd rather |
| 53 * be paranoid about this. |
| 54 */ |
| 55 static uint32_t SafeDefineSpace(uint32_t index, uint32_t perm, uint32_t size) { |
| 56 uint32_t result = TlclDefineSpace(index, perm, size); |
| 57 if (result == TPM_E_MAXNVWRITES) { |
| 58 RETURN_ON_FAILURE(TPMClearAndReenable()); |
| 59 return TlclDefineSpace(index, perm, size); |
| 60 } else { |
| 61 return result; |
| 62 } |
| 63 } |
| 64 |
| 50 static uint32_t InitializeKernelVersionsSpaces(void) { | 65 static uint32_t InitializeKernelVersionsSpaces(void) { |
| 51 RETURN_ON_FAILURE(TlclDefineSpace(KERNEL_VERSIONS_NV_INDEX, | 66 RETURN_ON_FAILURE(SafeDefineSpace(KERNEL_VERSIONS_NV_INDEX, |
| 52 TPM_NV_PER_PPWRITE, KERNEL_SPACE_SIZE)); | 67 TPM_NV_PER_PPWRITE, KERNEL_SPACE_SIZE)); |
| 53 RETURN_ON_FAILURE(SafeWrite(KERNEL_VERSIONS_NV_INDEX, KERNEL_SPACE_INIT_DATA, | 68 RETURN_ON_FAILURE(SafeWrite(KERNEL_VERSIONS_NV_INDEX, KERNEL_SPACE_INIT_DATA, |
| 54 KERNEL_SPACE_SIZE)); | 69 KERNEL_SPACE_SIZE)); |
| 55 return TPM_SUCCESS; | 70 return TPM_SUCCESS; |
| 56 } | 71 } |
| 57 | 72 |
| 58 /* When the return value is TPM_SUCCESS, this function sets *|initialized| to 1 | 73 /* When the return value is TPM_SUCCESS, this function sets *|initialized| to 1 |
| 59 * if the spaces have been fully initialized, to 0 if not. Otherwise | 74 * if the spaces have been fully initialized, to 0 if not. Otherwise |
| 60 * *|initialized| is not changed. | 75 * *|initialized| is not changed. |
| 61 */ | 76 */ |
| (...skipping 17 matching lines...) Expand all Loading... |
| 79 /* Creates the NVRAM spaces, and sets their initial values as needed. | 94 /* Creates the NVRAM spaces, and sets their initial values as needed. |
| 80 */ | 95 */ |
| 81 static uint32_t InitializeSpaces(void) { | 96 static uint32_t InitializeSpaces(void) { |
| 82 uint32_t zero = 0; | 97 uint32_t zero = 0; |
| 83 uint32_t firmware_perm = TPM_NV_PER_GLOBALLOCK | TPM_NV_PER_PPWRITE; | 98 uint32_t firmware_perm = TPM_NV_PER_GLOBALLOCK | TPM_NV_PER_PPWRITE; |
| 84 | 99 |
| 85 VBDEBUG(("Initializing spaces\n")); | 100 VBDEBUG(("Initializing spaces\n")); |
| 86 | 101 |
| 87 RETURN_ON_FAILURE(TlclSetNvLocked()); | 102 RETURN_ON_FAILURE(TlclSetNvLocked()); |
| 88 | 103 |
| 89 RETURN_ON_FAILURE(TlclDefineSpace(FIRMWARE_VERSIONS_NV_INDEX, | 104 RETURN_ON_FAILURE(SafeDefineSpace(FIRMWARE_VERSIONS_NV_INDEX, |
| 90 firmware_perm, sizeof(uint32_t))); | 105 firmware_perm, sizeof(uint32_t))); |
| 91 RETURN_ON_FAILURE(SafeWrite(FIRMWARE_VERSIONS_NV_INDEX, | 106 RETURN_ON_FAILURE(SafeWrite(FIRMWARE_VERSIONS_NV_INDEX, |
| 92 (uint8_t*) &zero, sizeof(uint32_t))); | 107 (uint8_t*) &zero, sizeof(uint32_t))); |
| 93 | 108 |
| 94 RETURN_ON_FAILURE(InitializeKernelVersionsSpaces()); | 109 RETURN_ON_FAILURE(InitializeKernelVersionsSpaces()); |
| 95 | 110 |
| 96 /* The space KERNEL_VERSIONS_BACKUP_NV_INDEX is used to protect the kernel | 111 /* The space KERNEL_VERSIONS_BACKUP_NV_INDEX is used to protect the kernel |
| 97 * versions. The content of space KERNEL_MUST_USE_BACKUP determines whether | 112 * versions. The content of space KERNEL_MUST_USE_BACKUP determines whether |
| 98 * only the backup value should be trusted. | 113 * only the backup value should be trusted. |
| 99 */ | 114 */ |
| 100 RETURN_ON_FAILURE(TlclDefineSpace(KERNEL_VERSIONS_BACKUP_NV_INDEX, | 115 RETURN_ON_FAILURE(SafeDefineSpace(KERNEL_VERSIONS_BACKUP_NV_INDEX, |
| 101 firmware_perm, sizeof(uint32_t))); | 116 firmware_perm, sizeof(uint32_t))); |
| 102 RETURN_ON_FAILURE(SafeWrite(KERNEL_VERSIONS_BACKUP_NV_INDEX, | 117 RETURN_ON_FAILURE(SafeWrite(KERNEL_VERSIONS_BACKUP_NV_INDEX, |
| 103 (uint8_t*) &zero, sizeof(uint32_t))); | 118 (uint8_t*) &zero, sizeof(uint32_t))); |
| 104 RETURN_ON_FAILURE(TlclDefineSpace(KERNEL_MUST_USE_BACKUP_NV_INDEX, | 119 RETURN_ON_FAILURE(SafeDefineSpace(KERNEL_MUST_USE_BACKUP_NV_INDEX, |
| 105 firmware_perm, sizeof(uint32_t))); | 120 firmware_perm, sizeof(uint32_t))); |
| 106 RETURN_ON_FAILURE(SafeWrite(KERNEL_MUST_USE_BACKUP_NV_INDEX, | 121 RETURN_ON_FAILURE(SafeWrite(KERNEL_MUST_USE_BACKUP_NV_INDEX, |
| 107 (uint8_t*) &zero, sizeof(uint32_t))); | 122 (uint8_t*) &zero, sizeof(uint32_t))); |
| 108 RETURN_ON_FAILURE(TlclDefineSpace(DEVELOPER_MODE_NV_INDEX, | 123 RETURN_ON_FAILURE(SafeDefineSpace(DEVELOPER_MODE_NV_INDEX, |
| 109 firmware_perm, sizeof(uint32_t))); | 124 firmware_perm, sizeof(uint32_t))); |
| 110 RETURN_ON_FAILURE(SafeWrite(DEVELOPER_MODE_NV_INDEX, | 125 RETURN_ON_FAILURE(SafeWrite(DEVELOPER_MODE_NV_INDEX, |
| 111 (uint8_t*) &zero, sizeof(uint32_t))); | 126 (uint8_t*) &zero, sizeof(uint32_t))); |
| 112 | 127 |
| 113 /* The space TPM_IS_INITIALIZED_NV_INDEX is used to indicate that the TPM | 128 /* The space TPM_IS_INITIALIZED_NV_INDEX is used to indicate that the TPM |
| 114 * initialization has completed. Without it we cannot be sure that the last | 129 * initialization has completed. Without it we cannot be sure that the last |
| 115 * space to be created was also initialized (power could have been lost right | 130 * space to be created was also initialized (power could have been lost right |
| 116 * after its creation). | 131 * after its creation). |
| 117 */ | 132 */ |
| 118 RETURN_ON_FAILURE(TlclDefineSpace(TPM_IS_INITIALIZED_NV_INDEX, | 133 RETURN_ON_FAILURE(SafeDefineSpace(TPM_IS_INITIALIZED_NV_INDEX, |
| 119 firmware_perm, sizeof(uint32_t))); | 134 firmware_perm, sizeof(uint32_t))); |
| 120 return TPM_SUCCESS; | 135 return TPM_SUCCESS; |
| 121 } | 136 } |
| 122 | 137 |
| 123 static uint32_t SetDistrustKernelSpaceAtNextBoot(uint32_t distrust) { | 138 static uint32_t SetDistrustKernelSpaceAtNextBoot(uint32_t distrust) { |
| 124 uint32_t must_use_backup; | 139 uint32_t must_use_backup; |
| 125 RETURN_ON_FAILURE(TlclRead(KERNEL_MUST_USE_BACKUP_NV_INDEX, | 140 RETURN_ON_FAILURE(TlclRead(KERNEL_MUST_USE_BACKUP_NV_INDEX, |
| 126 (uint8_t*) &must_use_backup, sizeof(uint32_t))); | 141 (uint8_t*) &must_use_backup, sizeof(uint32_t))); |
| 127 if (must_use_backup != distrust) { | 142 if (must_use_backup != distrust) { |
| 128 RETURN_ON_FAILURE(SafeWrite(KERNEL_MUST_USE_BACKUP_NV_INDEX, | 143 RETURN_ON_FAILURE(SafeWrite(KERNEL_MUST_USE_BACKUP_NV_INDEX, |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 return TPM_SUCCESS; | 358 return TPM_SUCCESS; |
| 344 } | 359 } |
| 345 | 360 |
| 346 uint32_t RollbackKernelLock(void) { | 361 uint32_t RollbackKernelLock(void) { |
| 347 if (!g_rollback_recovery_mode) { | 362 if (!g_rollback_recovery_mode) { |
| 348 return TlclLockPhysicalPresence(); | 363 return TlclLockPhysicalPresence(); |
| 349 } else { | 364 } else { |
| 350 return TPM_SUCCESS; | 365 return TPM_SUCCESS; |
| 351 } | 366 } |
| 352 } | 367 } |
| OLD | NEW |