<p>Andrey Pronin has uploaded this change for <strong>review</strong>.</p><p><a href="https://review.coreboot.org/23456">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">security/vboot: overwrite existing spaces during factory init for tpm2<br><br>In TPM 2.0 case, if the factory initialization is interrupted after<br>defining, say, the kernel tpm nvram space but before writing to this<br>space, the following will happen upon reboot when the factory<br>initialization will be re-attempted. Writing to this space will be<br>skipped, and coreboot will finish the factory initialization with<br>this space remained unwritten. At a later stage, when the rollback<br>logic will attempt to check the version in the kernel space, it will<br>fail (TPM2.0 returns an error when reading from unwritten spaces),<br>and the system will go into recovery with no way out (since the<br>kernel space will never be written).<br><br>This change fixes that by always writing to the kernel, MRC hash and<br>firmware spaces during factory initialization, even if the space<br>already existed by that time.<br><br>BUG=b:71884828<br>TEST=delete firmware space, delete, define but not write to the kernel<br>     space, reboot. coreboot should fill the kernel space and boot the<br>     OS.<br><br>Change-Id: I48d8bb4f9fc0e5276e6ec81247b3b6768ec9fa3b<br>Signed-off-by: Andrey Pronin <apronin@google.com><br>---<br>M src/security/vboot/secdata_tpm.c<br>1 file changed, 33 insertions(+), 29 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://review.coreboot.org:29418/coreboot refs/changes/56/23456/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/security/vboot/secdata_tpm.c b/src/security/vboot/secdata_tpm.c</span><br><span>index 04162b0..f59361e 100644</span><br><span>--- a/src/security/vboot/secdata_tpm.c</span><br><span>+++ b/src/security/vboot/secdata_tpm.c</span><br><span>@@ -165,45 +165,49 @@</span><br><span>  return tlcl_write(index, data, length);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static uint32_t set_space(const char *name,</span><br><span style="color: hsl(120, 100%, 40%);">+                   uint32_t index,</span><br><span style="color: hsl(120, 100%, 40%);">+                       const void *data,</span><br><span style="color: hsl(120, 100%, 40%);">+                     uint32_t length)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ uint32_t rv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        rv = tlcl_define_space(index, length);</span><br><span style="color: hsl(120, 100%, 40%);">+        if (rv == TPM_E_NV_DEFINED) {</span><br><span style="color: hsl(120, 100%, 40%);">+         /*</span><br><span style="color: hsl(120, 100%, 40%);">+             * Continue with writing: it may be defined, but not written</span><br><span style="color: hsl(120, 100%, 40%);">+           * to. In that case a subsequent tlcl_read() would still return</span><br><span style="color: hsl(120, 100%, 40%);">+                * TPM_E_BADINDEX on TPM 2.0. The cases when some non-firmware</span><br><span style="color: hsl(120, 100%, 40%);">+                 * space is defined while the firmware space is not there</span><br><span style="color: hsl(120, 100%, 40%);">+              * should be rare (interrupted initialization), so no big harm</span><br><span style="color: hsl(120, 100%, 40%);">+                 * in writing once again even if it was written already.</span><br><span style="color: hsl(120, 100%, 40%);">+               */</span><br><span style="color: hsl(120, 100%, 40%);">+           VBDEBUG("%s: %s space already exists\n", __func__, name);</span><br><span style="color: hsl(120, 100%, 40%);">+           rv = TPM_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   if (rv != TPM_SUCCESS)</span><br><span style="color: hsl(120, 100%, 40%);">+                return rv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  return safe_write(index, data, length);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static uint32_t set_firmware_space(const void *firmware_blob)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-      RETURN_ON_FAILURE(tlcl_define_space(FIRMWARE_NV_INDEX,</span><br><span style="color: hsl(0, 100%, 40%);">-                                      VB2_SECDATA_SIZE));</span><br><span style="color: hsl(0, 100%, 40%);">- RETURN_ON_FAILURE(safe_write(FIRMWARE_NV_INDEX, firmware_blob,</span><br><span style="color: hsl(0, 100%, 40%);">-                               VB2_SECDATA_SIZE));</span><br><span style="color: hsl(0, 100%, 40%);">-        return TPM_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+   return set_space("firmware", FIRMWARE_NV_INDEX, firmware_blob,</span><br><span style="color: hsl(120, 100%, 40%);">+                       VB2_SECDATA_SIZE);</span><br><span> }</span><br><span> </span><br><span> static uint32_t set_kernel_space(const void *kernel_blob)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-  uint32_t rv;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-    rv = tlcl_define_space(KERNEL_NV_INDEX, sizeof(secdata_kernel));</span><br><span style="color: hsl(0, 100%, 40%);">-        if (rv == TPM_E_NV_DEFINED) {</span><br><span style="color: hsl(0, 100%, 40%);">-           VBDEBUG("%s: kernel space already exists\n", __func__);</span><br><span style="color: hsl(0, 100%, 40%);">-               return TPM_SUCCESS;</span><br><span style="color: hsl(0, 100%, 40%);">-     }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-       if (rv != TPM_SUCCESS)</span><br><span style="color: hsl(0, 100%, 40%);">-          return rv;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-      return safe_write(KERNEL_NV_INDEX, kernel_blob, sizeof(secdata_kernel));</span><br><span style="color: hsl(120, 100%, 40%);">+      return set_space("kernel", KERNEL_NV_INDEX, kernel_blob,</span><br><span style="color: hsl(120, 100%, 40%);">+                     sizeof(secdata_kernel));</span><br><span> }</span><br><span> </span><br><span> static uint32_t set_rec_hash_space(const uint8_t *data)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-      uint32_t rv;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-    rv = tlcl_define_space(REC_HASH_NV_INDEX, REC_HASH_NV_SIZE);</span><br><span style="color: hsl(0, 100%, 40%);">-    if (rv == TPM_E_NV_DEFINED) {</span><br><span style="color: hsl(0, 100%, 40%);">-           VBDEBUG("%s: MRC Hash space already exists\n", __func__);</span><br><span style="color: hsl(0, 100%, 40%);">-             return TPM_SUCCESS;</span><br><span style="color: hsl(0, 100%, 40%);">-     }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-       if (rv != TPM_SUCCESS)</span><br><span style="color: hsl(0, 100%, 40%);">-          return rv;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-      return safe_write(REC_HASH_NV_INDEX, data, REC_HASH_NV_SIZE);</span><br><span style="color: hsl(120, 100%, 40%);">+ return set_space("MRC Hash", REC_HASH_NV_INDEX, data,</span><br><span style="color: hsl(120, 100%, 40%);">+                        REC_HASH_NV_SIZE);</span><br><span> }</span><br><span> </span><br><span> static uint32_t _factory_initialize_tpm(struct vb2_context *ctx)</span><br><span></span><br></pre><p>To view, visit <a href="https://review.coreboot.org/23456">change 23456</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://review.coreboot.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://review.coreboot.org/23456"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: coreboot </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I48d8bb4f9fc0e5276e6ec81247b3b6768ec9fa3b </div>
<div style="display:none"> Gerrit-Change-Number: 23456 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Andrey Pronin <apronin@google.com> </div>