ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
2aacef
From 1b7dfe48d6d66cad5d0368b8e8b387a4d9586ccd Mon Sep 17 00:00:00 2001
2aacef
From: Daan De Meyer <daan.j.demeyer@gmail.com>
2aacef
Date: Thu, 27 Oct 2022 11:12:10 +0200
2aacef
Subject: [PATCH] condition: Check that subsystem is enabled in
2aacef
 ConditionSecurity=tpm2
2aacef
2aacef
Instead of succeeding when either the firmware reports a TPM device
2aacef
or we find a TPM device, let's check that the firmware reports a TPM
2aacef
device and the TPM subsystem is enabled in the kernel.
2aacef
2aacef
To check whether the subsystem enabled, we check if the relevant
2aacef
subdirectory in /sys exists at all.
2aacef
2aacef
(cherry picked from commit 300bba79c22e4be1effe2faad0e59ac725d396a1)
2aacef
2aacef
Related #2138081
2aacef
---
2aacef
 man/systemd-creds.xml  |  4 ++--
2aacef
 src/creds/creds.c      |  6 ++++--
2aacef
 src/shared/condition.c |  9 ++++-----
2aacef
 src/shared/tpm2-util.c |  6 +++++-
2aacef
 src/shared/tpm2-util.h | 11 ++++++-----
2aacef
 5 files changed, 21 insertions(+), 15 deletions(-)
2aacef
2aacef
diff --git a/man/systemd-creds.xml b/man/systemd-creds.xml
2aacef
index 1e5632e63d..003fbcd463 100644
2aacef
--- a/man/systemd-creds.xml
2aacef
+++ b/man/systemd-creds.xml
2aacef
@@ -175,8 +175,8 @@
2aacef
         by the OS kernel drivers and by userspace (i.e. systemd) this prints <literal>yes</literal> and exits
2aacef
         with exit status zero. If no such device is discovered/supported/used, prints
2aacef
         <literal>no</literal>. Otherwise prints <literal>partial</literal>. In either of these two cases
2aacef
-        exits with non-zero exit status. It also shows three lines indicating separately whether drivers,
2aacef
-        firmware and the system discovered/support/use TPM2.</para>
2aacef
+        exits with non-zero exit status. It also shows four lines indicating separately whether firmware,
2aacef
+        drivers, the system and the kernel discovered/support/use TPM2.</para>
2aacef
 
2aacef
         <para>Combine with <option>--quiet</option> to suppress the output.</para></listitem>
2aacef
       </varlistentry>
2aacef
diff --git a/src/creds/creds.c b/src/creds/creds.c
2aacef
index 5586fd776a..a755a52c34 100644
2aacef
--- a/src/creds/creds.c
2aacef
+++ b/src/creds/creds.c
2aacef
@@ -637,10 +637,12 @@ static int verb_has_tpm2(int argc, char **argv, void *userdata) {
2aacef
 
2aacef
                 printf("%sfirmware\n"
2aacef
                        "%sdriver\n"
2aacef
-                       "%ssystem\n",
2aacef
+                       "%ssystem\n"
2aacef
+                       "%ssubsystem\n",
2aacef
                        plus_minus(s & TPM2_SUPPORT_FIRMWARE),
2aacef
                        plus_minus(s & TPM2_SUPPORT_DRIVER),
2aacef
-                       plus_minus(s & TPM2_SUPPORT_SYSTEM));
2aacef
+                       plus_minus(s & TPM2_SUPPORT_SYSTEM),
2aacef
+                       plus_minus(s & TPM2_SUPPORT_SUBSYSTEM));
2aacef
         }
2aacef
 
2aacef
         /* Return inverted bit flags. So that TPM2_SUPPORT_FULL becomes EXIT_SUCCESS and the other values
2aacef
diff --git a/src/shared/condition.c b/src/shared/condition.c
2aacef
index 310ffcbdc6..a23d6a3e45 100644
2aacef
--- a/src/shared/condition.c
2aacef
+++ b/src/shared/condition.c
2aacef
@@ -664,14 +664,13 @@ static int condition_test_ac_power(Condition *c, char **env) {
2aacef
 }
2aacef
 
2aacef
 static int has_tpm2(void) {
2aacef
-        /* Checks whether the system has at least one TPM2 resource manager device, i.e. at least one "tpmrm"
2aacef
-         * class device. Alternatively, we are also happy if the firmware reports support (this is to cover
2aacef
-         * for cases where we simply haven't loaded the driver for it yet, i.e. during early boot where we
2aacef
-         * very likely want to use this condition check).
2aacef
+        /* Checks whether the kernel has the TPM subsystem enabled and the firmware reports support. Note
2aacef
+         * we don't check for actual TPM devices, since we might not have loaded the driver for it yet, i.e.
2aacef
+         * during early boot where we very likely want to use this condition check).
2aacef
          *
2aacef
          * Note that we don't check if we ourselves are built with TPM2 support here! */
2aacef
 
2aacef
-        return (tpm2_support() & (TPM2_SUPPORT_DRIVER|TPM2_SUPPORT_FIRMWARE)) != 0;
2aacef
+        return FLAGS_SET(tpm2_support(), TPM2_SUPPORT_SUBSYSTEM|TPM2_SUPPORT_FIRMWARE);
2aacef
 }
2aacef
 
2aacef
 static int condition_test_security(Condition *c, char **env) {
2aacef
diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c
2aacef
index 13e92c4144..65e8d48347 100644
2aacef
--- a/src/shared/tpm2-util.c
2aacef
+++ b/src/shared/tpm2-util.c
2aacef
@@ -2189,7 +2189,11 @@ Tpm2Support tpm2_support(void) {
2aacef
                         if (r != -ENOENT)
2aacef
                                 log_debug_errno(r, "Unable to test whether /sys/class/tpmrm/ exists and is populated, assuming it is not: %m");
2aacef
                 } else if (r == 0) /* populated! */
2aacef
-                        support |= TPM2_SUPPORT_DRIVER;
2aacef
+                        support |= TPM2_SUPPORT_SUBSYSTEM|TPM2_SUPPORT_DRIVER;
2aacef
+                else
2aacef
+                        /* If the directory exists but is empty, we know the subsystem is enabled but no
2aacef
+                         * driver has been loaded yet. */
2aacef
+                        support |= TPM2_SUPPORT_SUBSYSTEM;
2aacef
         }
2aacef
 
2aacef
         if (efi_has_tpm2())
2aacef
diff --git a/src/shared/tpm2-util.h b/src/shared/tpm2-util.h
2aacef
index 048c28d6ca..c240335ae6 100644
2aacef
--- a/src/shared/tpm2-util.h
2aacef
+++ b/src/shared/tpm2-util.h
2aacef
@@ -137,11 +137,12 @@ typedef struct {
2aacef
 typedef enum Tpm2Support {
2aacef
         /* NOTE! The systemd-creds tool returns these flags 1:1 as exit status. Hence these flags are pretty
2aacef
          * much ABI! Hence, be extra careful when changing/extending these definitions. */
2aacef
-        TPM2_SUPPORT_NONE     = 0,       /* no support */
2aacef
-        TPM2_SUPPORT_FIRMWARE = 1 << 0,  /* firmware reports TPM2 was used */
2aacef
-        TPM2_SUPPORT_DRIVER   = 1 << 1,  /* the kernel has a driver loaded for it */
2aacef
-        TPM2_SUPPORT_SYSTEM   = 1 << 2,  /* we support it ourselves */
2aacef
-        TPM2_SUPPORT_FULL     = TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER|TPM2_SUPPORT_SYSTEM,
2aacef
+        TPM2_SUPPORT_NONE      = 0,       /* no support */
2aacef
+        TPM2_SUPPORT_FIRMWARE  = 1 << 0,  /* firmware reports TPM2 was used */
2aacef
+        TPM2_SUPPORT_DRIVER    = 1 << 1,  /* the kernel has a driver loaded for it */
2aacef
+        TPM2_SUPPORT_SYSTEM    = 1 << 2,  /* we support it ourselves */
2aacef
+        TPM2_SUPPORT_SUBSYSTEM = 1 << 3,  /* the kernel has the tpm subsystem enabled */
2aacef
+        TPM2_SUPPORT_FULL      = TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER|TPM2_SUPPORT_SYSTEM|TPM2_SUPPORT_SUBSYSTEM,
2aacef
 } Tpm2Support;
2aacef
 
2aacef
 Tpm2Support tpm2_support(void);