TPM: Verify PCR0 matches the bootmode if it is already initialized

Refuse to boot in case of a mismatch

Google-Bug-Id: 24814315

Change-Id: I6bd5abba7583eae95968fba4a63dd56f85497727
diff --git a/arch/arm/boards/optimus/tpm.c b/arch/arm/boards/optimus/tpm.c
index 1e5081f..e61eba1 100644
--- a/arch/arm/boards/optimus/tpm.c
+++ b/arch/arm/boards/optimus/tpm.c
@@ -35,44 +35,82 @@
 
 #define PCR_DIGEST_LENGTH	20
 
+struct pcr_value_pair {
+	uint8_t digest[PCR_DIGEST_LENGTH];
+	uint8_t extended[PCR_DIGEST_LENGTH];
+};
+
 struct bootmode_pcr_values {
-	uint8_t normal_boot[PCR_DIGEST_LENGTH];
-	uint8_t recovery_mode[PCR_DIGEST_LENGTH];
+	struct pcr_value_pair normal_boot;
+	struct pcr_value_pair recovery_mode;
 };
 
 static struct bootmode_pcr_values pcr_values_secure_boot = {
 #ifdef CONFIG_DEVELOPER_BAREBOX
 	/* secure boot, normal boot, dev firmware [0, 0, 2] */
-	.normal_boot = {0x1e, 0xf6, 0x24, 0x48, 0x2d, 0x62, 0x0e, 0x43, 0xe6, 0xd3,
-			0x4d, 0xa1, 0xaf, 0xe4, 0x62, 0x67, 0xfc, 0x69, 0x5d, 0x9b},
+	.normal_boot = {
+		.digest = {0x1e, 0xf6, 0x24, 0x48, 0x2d, 0x62, 0x0e, 0x43, 0xe6, 0xd3,
+			   0x4d, 0xa1, 0xaf, 0xe4, 0x62, 0x67, 0xfc, 0x69, 0x5d, 0x9b},
+		.extended = {0xee, 0x56, 0xc3, 0x2d, 0x5f, 0x7b, 0xfa, 0x56, 0xba, 0x92,
+			     0x6f, 0x66, 0x27, 0x0e, 0xc2, 0xe9, 0xd0, 0x1c, 0x64, 0xb3},
+	},
+
 	/* secure boot, recovery mode, dev firmware [0, 1, 2] */
-	.recovery_mode = {0x0c, 0x7a, 0x62, 0x3f, 0xd2, 0xbb, 0xc0, 0x5b, 0x06, 0x42,
-			  0x3b, 0xe3, 0x59, 0xe4, 0x02, 0x1d, 0x36, 0xe7, 0x21, 0xad},
+	.recovery_mode = {
+		.digest = {0x0c, 0x7a, 0x62, 0x3f, 0xd2, 0xbb, 0xc0, 0x5b, 0x06, 0x42,
+			   0x3b, 0xe3, 0x59, 0xe4, 0x02, 0x1d, 0x36, 0xe7, 0x21, 0xad},
+		.extended = {0x08, 0x20, 0xc4, 0xb6, 0x7b, 0x33, 0xb2, 0x81, 0x25, 0x6b,
+			     0x03, 0x53, 0xdc, 0xc9, 0xfa, 0x43, 0x66, 0xe4, 0x27, 0xaf},
+	},
 #else
 	/* secure boot, normal boot, prod firmware [0, 0, 1] */
-	.normal_boot = {0x25, 0x47, 0xcc, 0x73, 0x6e, 0x95, 0x1f, 0xa4, 0x91, 0x98,
-			0x53, 0xc4, 0x3a, 0xe8, 0x90, 0x86, 0x1a, 0x3b, 0x32, 0x64},
+	.normal_boot = {
+		.digest = {0x25, 0x47, 0xcc, 0x73, 0x6e, 0x95, 0x1f, 0xa4, 0x91, 0x98,
+			   0x53, 0xc4, 0x3a, 0xe8, 0x90, 0x86, 0x1a, 0x3b, 0x32, 0x64},
+		.extended = {0x06, 0x4a, 0xec, 0x9b, 0xbd, 0x94, 0xde, 0xa1, 0x23, 0x1a,
+			     0xe7, 0x57, 0x67, 0x64, 0x7f, 0x09, 0x8c, 0x39, 0x8e, 0x79},
+	},
 	/* secure boot, recovery mode, prod firmware [0, 1, 1] */
-	.recovery_mode = {0xee, 0xe4, 0x47, 0xed, 0xc7, 0x9f, 0xea, 0x1c, 0xa7, 0xc7,
-			  0xd3, 0x4e, 0x46, 0x32, 0x61, 0xcd, 0xa4, 0xba, 0x33, 0x9e},
+	.recovery_mode = {
+		.digest = {0xee, 0xe4, 0x47, 0xed, 0xc7, 0x9f, 0xea, 0x1c, 0xa7, 0xc7,
+			   0xd3, 0x4e, 0x46, 0x32, 0x61, 0xcd, 0xa4, 0xba, 0x33, 0x9e},
+		.extended = {0x17, 0x18, 0xb0, 0x87, 0xa1, 0x6a, 0x4e, 0x80, 0xe7, 0x50,
+			     0x74, 0xc8, 0x11, 0x09, 0xde, 0x23, 0x1a, 0x04, 0xc0, 0x0b},
+	},
 #endif
 };
 
 static struct bootmode_pcr_values pcr_values_nonsecure_boot = {
 #ifdef CONFIG_DEVELOPER_BAREBOX
 	/* non-secure boot, normal boot, dev firmware [1, 0, 2] */
-	.normal_boot = {0xfa, 0x01, 0x0d, 0x26, 0x64, 0xcc, 0x5b, 0x3b, 0x82, 0xee,
-			0x48, 0x8f, 0xe2, 0xb9, 0xf5, 0x0f, 0x49, 0x32, 0xeb, 0x8f},
+	.normal_boot = {
+		.digest = {0xfa, 0x01, 0x0d, 0x26, 0x64, 0xcc, 0x5b, 0x3b, 0x82, 0xee,
+			   0x48, 0x8f, 0xe2, 0xb9, 0xf5, 0x0f, 0x49, 0x32, 0xeb, 0x8f},
+		.extended = {0x29, 0x56, 0x57, 0x4c, 0x9d, 0x6a, 0x95, 0xd3, 0x15, 0x8a,
+			     0x81, 0x45, 0x71, 0x6a, 0x25, 0x7c, 0x3f, 0x6d, 0x62, 0xc1},
+	},
 	/* non-secure boot, recovery mode, dev firmware [1, 1, 2] */
-	.recovery_mode = {0x12, 0xa3, 0x40, 0xd7, 0x89, 0x7f, 0xe7, 0x13, 0xfc, 0x8f,
-			  0x02, 0xac, 0x53, 0x65, 0xb8, 0x6e, 0xbf, 0x35, 0x31, 0x78},
+	.recovery_mode = {
+		.digest = {0x12, 0xa3, 0x40, 0xd7, 0x89, 0x7f, 0xe7, 0x13, 0xfc, 0x8f,
+			   0x02, 0xac, 0x53, 0x65, 0xb8, 0x6e, 0xbf, 0x35, 0x31, 0x78},
+		.extended = {0x36, 0x56, 0x6d, 0xe7, 0x9c, 0x23, 0x94, 0x2f, 0x1b, 0x79,
+			     0xcf, 0xd7, 0xde, 0x56, 0xe6, 0x9b, 0xe4, 0xfe, 0xcd, 0x72},
+	},
 #else
 	/* non-secure boot, normal boot, dev firmware [1, 0, 2] */
-	.normal_boot = {0xfa, 0x01, 0x0d, 0x26, 0x64, 0xcc, 0x5b, 0x3b, 0x82, 0xee,
-			0x48, 0x8f, 0xe2, 0xb9, 0xf5, 0x0f, 0x49, 0x32, 0xeb, 0x8f},
+	.normal_boot = {
+		.digest = {0xfa, 0x01, 0x0d, 0x26, 0x64, 0xcc, 0x5b, 0x3b, 0x82, 0xee,
+			   0x48, 0x8f, 0xe2, 0xb9, 0xf5, 0x0f, 0x49, 0x32, 0xeb, 0x8f},
+		.extended = {0x29, 0x56, 0x57, 0x4c, 0x9d, 0x6a, 0x95, 0xd3, 0x15, 0x8a,
+			     0x81, 0x45, 0x71, 0x6a, 0x25, 0x7c, 0x3f, 0x6d, 0x62, 0xc1},
+	},
 	/* non-secure boot, recovery mode, dev firmware [1, 1, 2] */
-	.recovery_mode = {0x12, 0xa3, 0x40, 0xd7, 0x89, 0x7f, 0xe7, 0x13, 0xfc, 0x8f,
-			  0x02, 0xac, 0x53, 0x65, 0xb8, 0x6e, 0xbf, 0x35, 0x31, 0x78},
+	.recovery_mode = {
+		.digest = {0x12, 0xa3, 0x40, 0xd7, 0x89, 0x7f, 0xe7, 0x13, 0xfc, 0x8f,
+			   0x02, 0xac, 0x53, 0x65, 0xb8, 0x6e, 0xbf, 0x35, 0x31, 0x78},
+		.extended = {0x36, 0x56, 0x6d, 0xe7, 0x9c, 0x23, 0x94, 0x2f, 0x1b, 0x79,
+			     0xcf, 0xd7, 0xde, 0x56, 0xe6, 0x9b, 0xe4, 0xfe, 0xcd, 0x72},
+	},
 #endif
 };
 
@@ -87,6 +125,13 @@
 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 };
 
+static void pcr_value_to_string(const uint8_t *value, char *str) {
+	int i;
+	for (i = 0; i < PCR_DIGEST_LENGTH; i++) {
+		sprintf(&str[i * 2], "%02x", value[i]);
+	}
+}
+
 uint32_t tpm_init(void) {
 	uint32_t result;
 	uint8_t disable;
@@ -111,6 +156,8 @@
 	RETURN_ON_FAILURE(tlcl_continue_self_test());
 
 	if (!is_recovery_mode()) {
+		struct pcr_value_pair *bootmode_pcr_values;
+
 		RETURN_ON_FAILURE(tlcl_get_flags(&disable, &deactivated, NULL));
 		if (disable || deactivated) {
 			printk(KERN_DEBUG "TPM: disabled (%d) or deactivated (%d). "
@@ -126,18 +173,27 @@
 			return TPM_E_MUST_REBOOT;
 		}
 
+		if (get_secure_boot_mode() == SECURE) {
+			bootmode_pcr_values = &pcr_values_secure_boot.normal_boot;
+		} else {
+			bootmode_pcr_values = &pcr_values_nonsecure_boot.normal_boot;
+		}
+
 		RETURN_ON_FAILURE(tlcl_read_pcr(0, pcr_value));
 		if (!memcmp(pcr_value, PCR_uninitialized, PCR_DIGEST_LENGTH)) {
 			/* PCR0 not initialized, extend it with the boot mode */
-			const uint8_t *digest;
-
-			if (get_secure_boot_mode() == SECURE) {
-				digest = pcr_values_secure_boot.normal_boot;
-			} else {
-				digest = pcr_values_nonsecure_boot.normal_boot;
+			RETURN_ON_FAILURE(tlcl_extend(0, bootmode_pcr_values->digest, NULL));
+		} else {
+			/* PCR0 is initialized from previous boot, verify the value matches */
+			if (memcmp(pcr_value, bootmode_pcr_values->extended, PCR_DIGEST_LENGTH)) {
+				char str[PCR_DIGEST_LENGTH * 2 + 1];
+				printk("TPM: PCR0 mismatch\n");
+				pcr_value_to_string(bootmode_pcr_values->extended, str);
+				printk("  expected: %s\n", str);
+				pcr_value_to_string(pcr_value, str);
+				printk("  is: %s\n", str);
+				return TPM_E_MUST_REBOOT;
 			}
-
-			RETURN_ON_FAILURE(tlcl_extend(0, digest, NULL));
 		}
 	} else {
 		const uint8_t *digest;
@@ -150,9 +206,9 @@
 		RETURN_ON_FAILURE(tlcl_set_deactivated(0));
 
 		if (get_secure_boot_mode() == SECURE) {
-			digest = pcr_values_secure_boot.recovery_mode;
+			digest = pcr_values_secure_boot.recovery_mode.digest;
 		} else {
-			digest = pcr_values_nonsecure_boot.recovery_mode;
+			digest = pcr_values_nonsecure_boot.recovery_mode.digest;
 		}
 
 		RETURN_ON_FAILURE(tlcl_extend(0, digest, NULL));