1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following disclaimer
11 * in the documentation and/or other materials provided with the
13 * * Neither the name of Google Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Functions for querying, manipulating and locking rollback indices
32 * stored in the TPM NVRAM.
35 #include <security/vboot/antirollback.h>
36 #include <security/vboot/tpm_common.h>
39 #include <security/tpm/tspi.h>
41 #include <console/console.h>
45 #define VBDEBUG(format, args...) printf(format, ## args)
47 #define VBDEBUG(format, args...) \
48 printk(BIOS_INFO, "%s():%d: " format, __func__, __LINE__, ## args)
51 #define RETURN_ON_FAILURE(tpm_cmd) do { \
53 if ((result_ = (tpm_cmd)) != TPM_SUCCESS) { \
54 VBDEBUG("Antirollback: %08x returned by " #tpm_cmd \
55 "\n", (int)result_); \
60 #define TPM_PCR_GBB_FLAGS_NAME "GBB flags"
61 #define TPM_PCR_GBB_HWID_NAME "GBB HWID"
63 static uint32_t safe_write(uint32_t index
, const void *data
, uint32_t length
);
65 static uint32_t read_space_firmware(struct vb2_context
*ctx
)
70 RETURN_ON_FAILURE(tlcl_read(FIRMWARE_NV_INDEX
, ctx
->secdata
,
73 if (vb2api_secdata_check(ctx
) == VB2_SUCCESS
)
76 VBDEBUG("TPM: %s() - bad CRC\n", __func__
);
79 VBDEBUG("TPM: %s() - too many bad CRCs, giving up\n", __func__
);
80 return TPM_E_CORRUPTED_STATE
;
83 static uint32_t read_space_rec_hash(uint8_t *data
)
85 RETURN_ON_FAILURE(tlcl_read(REC_HASH_NV_INDEX
, data
,
90 static uint32_t write_secdata(uint32_t index
,
91 const uint8_t *secdata
,
98 if (len
> sizeof(sd
)) {
99 VBDEBUG("TPM: %s() - data is too large\n", __func__
);
100 return TPM_E_WRITE_FAILURE
;
104 rv
= safe_write(index
, secdata
, len
);
105 /* Can't write, not gonna try again */
106 if (rv
!= TPM_SUCCESS
)
109 /* Read it back to be sure it got the right values. */
110 rv
= tlcl_read(index
, sd
, len
);
111 if (rv
== TPM_SUCCESS
&& memcmp(secdata
, sd
, len
) == 0)
114 VBDEBUG("TPM: %s() failed. trying again\n", __func__
);
115 /* Try writing it again. Maybe it was garbled on the way out. */
118 VBDEBUG("TPM: %s() - too many failures, giving up\n", __func__
);
120 return TPM_E_CORRUPTED_STATE
;
124 * This is used to initialize the TPM space for recovery hash after defining
125 * it. Since there is no data available to calculate hash at the point where TPM
126 * space is defined, initialize it to all 0s.
128 static const uint8_t rec_hash_data
[REC_HASH_NV_SIZE
] = { };
132 * Different sets of NVRAM space attributes apply to the "ro" spaces,
133 * i.e. those which should not be possible to delete or modify once
134 * the RO exits, and the rest of the NVRAM spaces.
136 const static TPMA_NV ro_space_attributes
= {
137 .TPMA_NV_PPWRITE
= 1,
138 .TPMA_NV_AUTHREAD
= 1,
140 .TPMA_NV_PLATFORMCREATE
= 1,
141 .TPMA_NV_WRITE_STCLEAR
= 1,
142 .TPMA_NV_POLICY_DELETE
= 1,
145 const static TPMA_NV rw_space_attributes
= {
146 .TPMA_NV_PPWRITE
= 1,
147 .TPMA_NV_AUTHREAD
= 1,
149 .TPMA_NV_PLATFORMCREATE
= 1,
153 * This policy digest was obtained using TPM2_PolicyPCR
154 * selecting only PCR_0 with a value of all zeros.
156 const static uint8_t pcr0_unchanged_policy
[] = {
157 0x09, 0x93, 0x3C, 0xCE, 0xEB, 0xB4, 0x41, 0x11, 0x18, 0x81, 0x1D,
158 0xD4, 0x47, 0x78, 0x80, 0x08, 0x88, 0x86, 0x62, 0x2D, 0xD7, 0x79,
159 0x94, 0x46, 0x62, 0x26, 0x68, 0x8E, 0xEE, 0xE6, 0x6A, 0xA1};
161 /* Nothing special in the TPM2 path yet. */
162 static uint32_t safe_write(uint32_t index
, const void *data
, uint32_t length
)
164 return tlcl_write(index
, data
, length
);
167 static uint32_t set_space(const char *name
, uint32_t index
, const void *data
,
168 uint32_t length
, const TPMA_NV nv_attributes
,
169 const uint8_t *nv_policy
, size_t nv_policy_size
)
173 rv
= tlcl_define_space(index
, length
, nv_attributes
, nv_policy
,
175 if (rv
== TPM_E_NV_DEFINED
) {
177 * Continue with writing: it may be defined, but not written
178 * to. In that case a subsequent tlcl_read() would still return
179 * TPM_E_BADINDEX on TPM 2.0. The cases when some non-firmware
180 * space is defined while the firmware space is not there
181 * should be rare (interrupted initialization), so no big harm
182 * in writing once again even if it was written already.
184 VBDEBUG("%s: %s space already exists\n", __func__
, name
);
188 if (rv
!= TPM_SUCCESS
)
191 return safe_write(index
, data
, length
);
194 static uint32_t set_firmware_space(const void *firmware_blob
)
196 return set_space("firmware", FIRMWARE_NV_INDEX
, firmware_blob
,
197 VB2_SECDATA_SIZE
, ro_space_attributes
,
198 pcr0_unchanged_policy
, sizeof(pcr0_unchanged_policy
));
201 static uint32_t set_kernel_space(const void *kernel_blob
)
203 return set_space("kernel", KERNEL_NV_INDEX
, kernel_blob
,
204 VB2_SECDATAK_SIZE
, rw_space_attributes
, NULL
, 0);
207 static uint32_t set_rec_hash_space(const uint8_t *data
)
209 return set_space("MRC Hash", REC_HASH_NV_INDEX
, data
,
211 ro_space_attributes
, pcr0_unchanged_policy
,
212 sizeof(pcr0_unchanged_policy
));
215 static uint32_t _factory_initialize_tpm(struct vb2_context
*ctx
)
217 RETURN_ON_FAILURE(tlcl_force_clear());
220 * Of all NVRAM spaces defined by this function the firmware space
221 * must be defined last, because its existence is considered an
222 * indication that TPM factory initialization was successfully
225 RETURN_ON_FAILURE(set_kernel_space(ctx
->secdatak
));
227 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE
))
228 RETURN_ON_FAILURE(set_rec_hash_space(rec_hash_data
));
230 RETURN_ON_FAILURE(set_firmware_space(ctx
->secdata
));
235 uint32_t antirollback_lock_space_firmware(void)
237 return tlcl_lock_nv_write(FIRMWARE_NV_INDEX
);
240 uint32_t antirollback_lock_space_rec_hash(void)
242 return tlcl_lock_nv_write(REC_HASH_NV_INDEX
);
248 * Like tlcl_write(), but checks for write errors due to hitting the 64-write
249 * limit and clears the TPM when that happens. This can only happen when the
250 * TPM is unowned, so it is OK to clear it (and we really have no choice).
251 * This is not expected to happen frequently, but it could happen.
254 static uint32_t safe_write(uint32_t index
, const void *data
, uint32_t length
)
256 uint32_t result
= tlcl_write(index
, data
, length
);
257 if (result
== TPM_E_MAXNVWRITES
) {
258 RETURN_ON_FAILURE(tpm_clear_and_reenable());
259 return tlcl_write(index
, data
, length
);
266 * Similarly to safe_write(), this ensures we don't fail a DefineSpace because
267 * we hit the TPM write limit. This is even less likely to happen than with
268 * writes because we only define spaces once at initialization, but we'd
269 * rather be paranoid about this.
271 static uint32_t safe_define_space(uint32_t index
, uint32_t perm
, uint32_t size
)
273 uint32_t result
= tlcl_define_space(index
, perm
, size
);
274 if (result
== TPM_E_MAXNVWRITES
) {
275 RETURN_ON_FAILURE(tpm_clear_and_reenable());
276 return tlcl_define_space(index
, perm
, size
);
282 static uint32_t set_rec_hash_space(const uint8_t *data
)
284 RETURN_ON_FAILURE(safe_define_space(REC_HASH_NV_INDEX
,
285 TPM_NV_PER_GLOBALLOCK
|
288 RETURN_ON_FAILURE(write_secdata(REC_HASH_NV_INDEX
, data
,
294 static uint32_t _factory_initialize_tpm(struct vb2_context
*ctx
)
296 TPM_PERMANENT_FLAGS pflags
;
299 result
= tlcl_get_permanent_flags(&pflags
);
300 if (result
!= TPM_SUCCESS
)
304 * TPM may come from the factory without physical presence finalized.
307 VBDEBUG("TPM: physicalPresenceLifetimeLock=%d\n",
308 pflags
.physicalPresenceLifetimeLock
);
309 if (!pflags
.physicalPresenceLifetimeLock
) {
310 VBDEBUG("TPM: Finalizing physical presence\n");
311 RETURN_ON_FAILURE(tlcl_finalize_physical_presence());
315 * The TPM will not enforce the NV authorization restrictions until the
316 * execution of a TPM_NV_DefineSpace with the handle of
317 * TPM_NV_INDEX_LOCK. Here we create that space if it doesn't already
319 VBDEBUG("TPM: nvLocked=%d\n", pflags
.nvLocked
);
320 if (!pflags
.nvLocked
) {
321 VBDEBUG("TPM: Enabling NV locking\n");
322 RETURN_ON_FAILURE(tlcl_set_nv_locked());
325 /* Clear TPM owner, in case the TPM is already owned for some reason. */
326 VBDEBUG("TPM: Clearing owner\n");
327 RETURN_ON_FAILURE(tpm_clear_and_reenable());
329 /* Define and write secdatak kernel space. */
330 RETURN_ON_FAILURE(safe_define_space(KERNEL_NV_INDEX
,
333 RETURN_ON_FAILURE(write_secdata(KERNEL_NV_INDEX
,
337 /* Define and write secdata firmware space. */
338 RETURN_ON_FAILURE(safe_define_space(FIRMWARE_NV_INDEX
,
339 TPM_NV_PER_GLOBALLOCK
|
342 RETURN_ON_FAILURE(write_secdata(FIRMWARE_NV_INDEX
,
346 /* Define and set rec hash space, if available. */
347 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE
))
348 RETURN_ON_FAILURE(set_rec_hash_space(rec_hash_data
));
353 uint32_t antirollback_lock_space_firmware(void)
355 return tlcl_set_global_lock();
358 uint32_t antirollback_lock_space_rec_hash(void)
361 * Nothing needs to be done here, since global lock is already set while
362 * locking firmware space.
369 * Perform one-time initializations.
371 * Create the NVRAM spaces, and set their initial values as needed. Sets the
372 * nvLocked bit and ensures the physical presence command is enabled and
375 static uint32_t factory_initialize_tpm(struct vb2_context
*ctx
)
379 /* Set initial values of secdata and secdatak spaces. */
380 vb2api_secdata_create(ctx
);
381 vb2api_secdatak_create(ctx
);
383 VBDEBUG("TPM: factory initialization\n");
386 * Do a full test. This only happens the first time the device is
387 * turned on in the factory, so performance is not an issue. This is
388 * almost certainly not necessary, but it gives us more confidence
389 * about some code paths below that are difficult to
390 * test---specifically the ones that set lifetime flags, and are only
391 * executed once per physical TPM.
393 result
= tlcl_self_test_full();
394 if (result
!= TPM_SUCCESS
)
397 result
= _factory_initialize_tpm(ctx
);
398 if (result
!= TPM_SUCCESS
)
401 VBDEBUG("TPM: factory initialization successful\n");
406 uint32_t antirollback_read_space_firmware(struct vb2_context
*ctx
)
410 /* Read the firmware space. */
411 rv
= read_space_firmware(ctx
);
412 if (rv
== TPM_E_BADINDEX
) {
414 * This seems the first time we've run. Initialize the TPM.
416 VBDEBUG("TPM: Not initialized yet.\n");
417 RETURN_ON_FAILURE(factory_initialize_tpm(ctx
));
418 } else if (rv
!= TPM_SUCCESS
) {
419 VBDEBUG("TPM: Firmware space in a bad state; giving up.\n");
420 //RETURN_ON_FAILURE(factory_initialize_tpm(ctx));
421 return TPM_E_CORRUPTED_STATE
;
427 uint32_t antirollback_write_space_firmware(struct vb2_context
*ctx
)
429 if (CONFIG(CR50_IMMEDIATELY_COMMIT_FW_SECDATA
))
430 tlcl_cr50_enable_nvcommits();
431 return write_secdata(FIRMWARE_NV_INDEX
, ctx
->secdata
, VB2_SECDATA_SIZE
);
434 uint32_t antirollback_read_space_rec_hash(uint8_t *data
, uint32_t size
)
436 if (size
!= REC_HASH_NV_SIZE
) {
437 VBDEBUG("TPM: Incorrect buffer size for rec hash. "
438 "(Expected=0x%x Actual=0x%x).\n", REC_HASH_NV_SIZE
,
440 return TPM_E_READ_FAILURE
;
442 return read_space_rec_hash(data
);
445 uint32_t antirollback_write_space_rec_hash(const uint8_t *data
, uint32_t size
)
447 uint8_t spc_data
[REC_HASH_NV_SIZE
];
450 if (size
!= REC_HASH_NV_SIZE
) {
451 VBDEBUG("TPM: Incorrect buffer size for rec hash. "
452 "(Expected=0x%x Actual=0x%x).\n", REC_HASH_NV_SIZE
,
454 return TPM_E_WRITE_FAILURE
;
457 rv
= read_space_rec_hash(spc_data
);
458 if (rv
== TPM_E_BADINDEX
) {
460 * If space is not defined already for recovery hash, define
463 VBDEBUG("TPM: Initializing recovery hash space.\n");
464 return set_rec_hash_space(data
);
467 if (rv
!= TPM_SUCCESS
)
470 return write_secdata(REC_HASH_NV_INDEX
, data
, size
);
473 vb2_error_t
vb2ex_tpm_clear_owner(struct vb2_context
*ctx
)
476 printk(BIOS_INFO
, "Clearing TPM owner\n");
477 rv
= tpm_clear_and_reenable();
479 return VB2_ERROR_EX_TPM_CLEAR_OWNER
;