]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: add ConditionSecurity=tpm2 support
authorLennart Poettering <lennart@poettering.net>
Sat, 28 Nov 2020 19:33:53 +0000 (20:33 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 3 Dec 2020 11:03:58 +0000 (12:03 +0100)
man/systemd.unit.xml
src/shared/condition.c

index b7dbbe309ee100d191d3a8392e94e8b0742633bf..2fdc0d583230311ecbd27ea845565e480b14faab 100644 (file)
           <listitem><para><varname>ConditionSecurity=</varname> may be used to check whether the given
           security technology is enabled on the system. Currently, the recognized values are
           <literal>selinux</literal>, <literal>apparmor</literal>, <literal>tomoyo</literal>,
-          <literal>ima</literal>, <literal>smack</literal>, <literal>audit</literal> and
-          <literal>uefi-secureboot</literal>. The test may be negated by prepending an exclamation
-          mark.</para>
+          <literal>ima</literal>, <literal>smack</literal>, <literal>audit</literal>,
+          <literal>uefi-secureboot</literal> and <literal>tpm2</literal>. The test may be negated by prepending
+          an exclamation mark.</para>
           </listitem>
         </varlistentry>
 
index b2ec690bc3beead7088de60ff922c40bb508a2c5..41d3a16391f67ba2361f79acc1a48a62f3cc32bc 100644 (file)
@@ -480,6 +480,21 @@ static int condition_test_ac_power(Condition *c, char **env) {
         return (on_ac_power() != 0) == !!r;
 }
 
+static int has_tpm2(void) {
+        int r;
+
+        /* Checks whether the system has at least one TPM2 resource manager device, i.e. at least one "tpmrm"
+         * class device */
+
+        r = dir_is_empty("/sys/class/tpmrm");
+        if (r == -ENOENT)
+                return false;
+        if (r < 0)
+                return log_debug_errno(r, "Failed to determine whether system has TPM2 support: %m");
+
+        return !r;
+}
+
 static int condition_test_security(Condition *c, char **env) {
         assert(c);
         assert(c->parameter);
@@ -499,6 +514,8 @@ static int condition_test_security(Condition *c, char **env) {
                 return mac_tomoyo_use();
         if (streq(c->parameter, "uefi-secureboot"))
                 return is_efi_secure_boot();
+        if (streq(c->parameter, "tpm2"))
+                return has_tpm2();
 
         return false;
 }