Re: [PATCH v5 03/11] tpm: Allow PCR 23 to be restricted to kernel-only use

From
Eric Biggers <ebiggers@kernel.org>
Date
2022-11-13 20:46:48
Also in
linux-integrity, lkml
Reply to
Evan Green, 2022-11-11
On Fri, Nov 11, 2022 at 03:16:28PM -0800, Evan Green wrote:
Introduce a new Kconfig, TCG_TPM_RESTRICT_PCR, which if enabled
TCG_TPM_RESTRICT_PCR => TCG_TPM2_RESTRICT_PCR
For systems with TPM1 devices, having this Kconfig enabled completely
restricts usermode's access to the TPM.
This doesn't appear to actually be the case.
quoted hunk ↗ jump to hunk
+config TCG_TPM2_RESTRICT_PCR
+	bool "Restrict userland access to PCR 23 on TPM2 devices"
+	depends on TCG_TPM
I assume you also considered making this a once-settable sysctl, or similar?
I guess this kconfig is fine for now, but IMO it does violate the concept of
"kernel provides mechanism, not policy".
quoted hunk ↗ jump to hunk
diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index 303ce2ea02a4b0..3bc5546fddc792 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -778,3 +778,25 @@ int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
 
 	return -1;
 }
+
+#ifdef CONFIG_TCG_TPM2_RESTRICT_PCR
+int tpm2_cmd_restricted(struct tpm_chip *chip, u8 *buffer, size_t size)
+{
+	int cc = tpm2_find_and_validate_cc(chip, NULL, buffer, size);
+	__be32 *handle;
+
+	switch (cc) {
+	case TPM2_CC_PCR_EXTEND:
+	case TPM2_CC_PCR_RESET:
+		if (size < (TPM_HEADER_SIZE + sizeof(u32)))
+			return -EINVAL;
+
+		handle = (__be32 *)&buffer[TPM_HEADER_SIZE];
+		if (be32_to_cpu(*handle) == TPM_RESTRICTED_PCR)
+			return -EPERM;
get_unaligned_be32((__be32 *)&buffer[TPM_HEADER_SIZE]),
to avoid an unaligned memory access.
quoted hunk ↗ jump to hunk
+		break;
+	}
+
+	return 0;
So, if tpm2_find_and_validate_cc() returns an error code, the command is *not*
restricted, even if it uses one of the forbidden command codes.  Are you sure
there are no loopholes here?

- Eric
Thread (61 messages) 61 messages, 8 authors, last reply 2023-02-07
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help