naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
594167
From 2418e9c1409ba04ddff516a83d83b2daa3417832 Mon Sep 17 00:00:00 2001
594167
From: Grigori Goronzy <greg@chown.ath.cx>
594167
Date: Fri, 18 Feb 2022 11:56:02 +0100
594167
Subject: [PATCH] cryptsetup: add support for TPM2 pin
594167
594167
Extend cryptsetup for TPM2 pin entry, similar to FIDO2.
594167
594167
(cherry picked from commit bea344a1a426e615ba87b66b6d3ff4b265c57a95)
594167
594167
Related: #2087652
594167
---
594167
 src/cryptsetup/cryptsetup-tpm2.c | 108 ++++++++++++++++++++++++++++++-
594167
 src/cryptsetup/cryptsetup-tpm2.h |  16 ++++-
594167
 src/cryptsetup/cryptsetup.c      |  16 ++++-
594167
 3 files changed, 135 insertions(+), 5 deletions(-)
594167
594167
diff --git a/src/cryptsetup/cryptsetup-tpm2.c b/src/cryptsetup/cryptsetup-tpm2.c
594167
index 05d76a684d..b84d64def8 100644
594167
--- a/src/cryptsetup/cryptsetup-tpm2.c
594167
+++ b/src/cryptsetup/cryptsetup-tpm2.c
594167
@@ -1,7 +1,9 @@
594167
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
594167
 
594167
 #include "alloc-util.h"
594167
+#include "ask-password-api.h"
594167
 #include "cryptsetup-tpm2.h"
594167
+#include "env-util.h"
594167
 #include "fileio.h"
594167
 #include "hexdecoct.h"
594167
 #include "json.h"
594167
@@ -9,6 +11,47 @@
594167
 #include "random-util.h"
594167
 #include "tpm2-util.h"
594167
 
594167
+static int get_pin(usec_t until, AskPasswordFlags ask_password_flags, bool headless, char **ret_pin_str) {
594167
+        _cleanup_free_ char *pin_str = NULL;
594167
+        _cleanup_strv_free_erase_ char **pin = NULL;
594167
+        int r;
594167
+
594167
+        assert(ret_pin_str);
594167
+
594167
+        r = getenv_steal_erase("PIN", &pin_str);
594167
+        if (r < 0)
594167
+                return log_error_errno(r, "Failed to acquire PIN from environment: %m");
594167
+        if (!r) {
594167
+                if (headless)
594167
+                        return log_error_errno(
594167
+                                        SYNTHETIC_ERRNO(ENOPKG),
594167
+                                        "PIN querying disabled via 'headless' option. "
594167
+                                        "Use the '$PIN' environment variable.");
594167
+
594167
+                pin = strv_free_erase(pin);
594167
+                r = ask_password_auto(
594167
+                                "Please enter TPM2 PIN:",
594167
+                                "drive-harddisk",
594167
+                                NULL,
594167
+                                "tpm2-pin",
594167
+                                "cryptsetup.tpm2-pin",
594167
+                                until,
594167
+                                ask_password_flags,
594167
+                                &pin;;
594167
+                if (r < 0)
594167
+                        return log_error_errno(r, "Failed to ask for user pin: %m");
594167
+                assert(strv_length(pin) == 1);
594167
+
594167
+                pin_str = strdup(pin[0]);
594167
+                if (!pin_str)
594167
+                        return log_oom();
594167
+        }
594167
+
594167
+        *ret_pin_str = TAKE_PTR(pin_str);
594167
+
594167
+        return r;
594167
+}
594167
+
594167
 int acquire_tpm2_key(
594167
                 const char *volume_name,
594167
                 const char *device,
594167
@@ -22,6 +65,10 @@ int acquire_tpm2_key(
594167
                 size_t key_data_size,
594167
                 const void *policy_hash,
594167
                 size_t policy_hash_size,
594167
+                TPM2Flags flags,
594167
+                usec_t until,
594167
+                bool headless,
594167
+                AskPasswordFlags ask_password_flags,
594167
                 void **ret_decrypted_key,
594167
                 size_t *ret_decrypted_key_size) {
594167
 
594167
@@ -64,7 +111,51 @@ int acquire_tpm2_key(
594167
                 blob = loaded_blob;
594167
         }
594167
 
594167
-        return tpm2_unseal(device, pcr_mask, pcr_bank, primary_alg, blob, blob_size, policy_hash, policy_hash_size, NULL, ret_decrypted_key, ret_decrypted_key_size);
594167
+        if (!(flags & TPM2_FLAGS_USE_PIN))
594167
+                return tpm2_unseal(
594167
+                                device,
594167
+                                pcr_mask,
594167
+                                pcr_bank,
594167
+                                primary_alg,
594167
+                                blob,
594167
+                                blob_size,
594167
+                                policy_hash,
594167
+                                policy_hash_size,
594167
+                                NULL,
594167
+                                ret_decrypted_key,
594167
+                                ret_decrypted_key_size);
594167
+
594167
+        for (int i = 5;; i--) {
594167
+                _cleanup_(erase_and_freep) char *pin_str = NULL;
594167
+
594167
+                if (i <= 0)
594167
+                        return -EACCES;
594167
+
594167
+                r = get_pin(until, ask_password_flags, headless, &pin_str);
594167
+                if (r < 0)
594167
+                        return r;
594167
+
594167
+                r = tpm2_unseal(
594167
+                                device,
594167
+                                pcr_mask,
594167
+                                pcr_bank,
594167
+                                primary_alg,
594167
+                                blob,
594167
+                                blob_size,
594167
+                                policy_hash,
594167
+                                policy_hash_size,
594167
+                                pin_str,
594167
+                                ret_decrypted_key,
594167
+                                ret_decrypted_key_size);
594167
+                /* We get this error in case there is an authentication policy mismatch. This should
594167
+                 * not happen, but this avoids confusing behavior, just in case. */
594167
+                if (IN_SET(r, -EPERM, -ENOLCK))
594167
+                        return r;
594167
+                if (r < 0)
594167
+                        continue;
594167
+
594167
+                return r;
594167
+        }
594167
 }
594167
 
594167
 int find_tpm2_auto_data(
594167
@@ -79,11 +170,13 @@ int find_tpm2_auto_data(
594167
                 void **ret_policy_hash,
594167
                 size_t *ret_policy_hash_size,
594167
                 int *ret_keyslot,
594167
-                int *ret_token) {
594167
+                int *ret_token,
594167
+                TPM2Flags *ret_flags) {
594167
 
594167
         _cleanup_free_ void *blob = NULL, *policy_hash = NULL;
594167
         size_t blob_size = 0, policy_hash_size = 0;
594167
         int r, keyslot = -1, token = -1;
594167
+        TPM2Flags flags = 0;
594167
         uint32_t pcr_mask = 0;
594167
         uint16_t pcr_bank = UINT16_MAX; /* default: pick automatically */
594167
         uint16_t primary_alg = TPM2_ALG_ECC; /* ECC was the only supported algorithm in systemd < 250, use that as implied default, for compatibility */
594167
@@ -196,6 +289,16 @@ int find_tpm2_auto_data(
594167
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
594167
                                                "Invalid base64 data in 'tpm2-policy-hash' field.");
594167
 
594167
+                w = json_variant_by_key(v, "tpm2-pin");
594167
+                if (w) {
594167
+                        if (!json_variant_is_boolean(w))
594167
+                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
594167
+                                                       "TPM2 PIN policy is not a boolean.");
594167
+
594167
+                        if (json_variant_boolean(w))
594167
+                                flags |= TPM2_FLAGS_USE_PIN;
594167
+                }
594167
+
594167
                 break;
594167
         }
594167
 
594167
@@ -215,6 +318,7 @@ int find_tpm2_auto_data(
594167
         *ret_token = token;
594167
         *ret_pcr_bank = pcr_bank;
594167
         *ret_primary_alg = primary_alg;
594167
+        *ret_flags = flags;
594167
 
594167
         return 0;
594167
 }
594167
diff --git a/src/cryptsetup/cryptsetup-tpm2.h b/src/cryptsetup/cryptsetup-tpm2.h
594167
index bd04620462..ab16d0a18f 100644
594167
--- a/src/cryptsetup/cryptsetup-tpm2.h
594167
+++ b/src/cryptsetup/cryptsetup-tpm2.h
594167
@@ -3,9 +3,11 @@
594167
 
594167
 #include <sys/types.h>
594167
 
594167
+#include "ask-password-api.h"
594167
 #include "cryptsetup-util.h"
594167
 #include "log.h"
594167
 #include "time-util.h"
594167
+#include "tpm2-util.h"
594167
 
594167
 #if HAVE_TPM2
594167
 
594167
@@ -22,6 +24,10 @@ int acquire_tpm2_key(
594167
                 size_t key_data_size,
594167
                 const void *policy_hash,
594167
                 size_t policy_hash_size,
594167
+                TPM2Flags flags,
594167
+                usec_t until,
594167
+                bool headless,
594167
+                AskPasswordFlags ask_password_flags,
594167
                 void **ret_decrypted_key,
594167
                 size_t *ret_decrypted_key_size);
594167
 
594167
@@ -37,7 +43,8 @@ int find_tpm2_auto_data(
594167
                 void **ret_policy_hash,
594167
                 size_t *ret_policy_hash_size,
594167
                 int *ret_keyslot,
594167
-                int *ret_token);
594167
+                int *ret_token,
594167
+                TPM2Flags *ret_flags);
594167
 
594167
 #else
594167
 
594167
@@ -54,6 +61,10 @@ static inline int acquire_tpm2_key(
594167
                 size_t key_data_size,
594167
                 const void *policy_hash,
594167
                 size_t policy_hash_size,
594167
+                TPM2Flags flags,
594167
+                usec_t until,
594167
+                bool headless,
594167
+                AskPasswordFlags ask_password_flags,
594167
                 void **ret_decrypted_key,
594167
                 size_t *ret_decrypted_key_size) {
594167
 
594167
@@ -73,7 +84,8 @@ static inline int find_tpm2_auto_data(
594167
                 void **ret_policy_hash,
594167
                 size_t *ret_policy_hash_size,
594167
                 int *ret_keyslot,
594167
-                int *ret_token) {
594167
+                int *ret_token,
594167
+                TPM2Flags *ret_flags) {
594167
 
594167
         return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
594167
                                "TPM2 support not available.");
594167
diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c
594167
index dddd976dc8..ede0f7ed0b 100644
594167
--- a/src/cryptsetup/cryptsetup.c
594167
+++ b/src/cryptsetup/cryptsetup.c
594167
@@ -1301,9 +1301,15 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
594167
                                         key_file, arg_keyfile_size, arg_keyfile_offset,
594167
                                         key_data, key_data_size,
594167
                                         NULL, 0, /* we don't know the policy hash */
594167
+                                        0, /* PIN is currently unhandled in this case */
594167
+                                        until,
594167
+                                        arg_headless,
594167
+                                        arg_ask_password_flags,
594167
                                         &decrypted_key, &decrypted_key_size);
594167
                         if (r >= 0)
594167
                                 break;
594167
+                        if (IN_SET(r, -EACCES, -ENOLCK))
594167
+                                return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
594167
                         if (ERRNO_IS_NOT_SUPPORTED(r)) /* TPM2 support not compiled in? */
594167
                                 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 support not available, falling back to traditional unlocking.");
594167
                         if (r != -EAGAIN) /* EAGAIN means: no tpm2 chip found */
594167
@@ -1335,6 +1341,7 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
594167
                         for (;;) {
594167
                                 uint32_t pcr_mask;
594167
                                 uint16_t pcr_bank, primary_alg;
594167
+                                TPM2Flags tpm2_flags;
594167
 
594167
                                 r = find_tpm2_auto_data(
594167
                                                 cd,
594167
@@ -1346,7 +1353,8 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
594167
                                                 &blob, &blob_size,
594167
                                                 &policy_hash, &policy_hash_size,
594167
                                                 &keyslot,
594167
-                                                &token);
594167
+                                                &token,
594167
+                                                &tpm2_flags);
594167
                                 if (r == -ENXIO)
594167
                                         /* No further TPM2 tokens found in the LUKS2 header. */
594167
                                         return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
594167
@@ -1369,7 +1377,13 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
594167
                                                 NULL, 0, 0, /* no key file */
594167
                                                 blob, blob_size,
594167
                                                 policy_hash, policy_hash_size,
594167
+                                                tpm2_flags,
594167
+                                                until,
594167
+                                                arg_headless,
594167
+                                                arg_ask_password_flags,
594167
                                                 &decrypted_key, &decrypted_key_size);
594167
+                                if (IN_SET(r, -EACCES, -ENOLCK))
594167
+                                        return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
594167
                                 if (r != -EPERM)
594167
                                         break;
594167