Skip to content

Commit

Permalink
tpm2: add support for a trusted SRK
Browse files Browse the repository at this point in the history
Prevent attackers from spoofing the tpmKey portion of the AuthSession by
adding a trusted key to the LUKS header metadata. Also, use a persistent
object rather than a transient object.

This provides the following benifits:
1. No way to MITM the tpmKey portion of the session, see [1] for
details.

2. Strengthens the encrypted sessions, note that the bindKey could be
   dropped now.

3. Speed, once it's created we just use it.

4. Owner Auth is needed to call create primary, so using the SRK
   creates a scratch space for normal users.

This is a "first to set" model, in where the first person to set the key
in the LUKS header wins. Thus, setup should be done in a known good
state. If an SRK, which is a primary key at a special persistent
address, is found, it will use whatever is there. If not, it creates an
SRK. The SRK follows the convetions used through the tpm2-software
organization code on GitHub [2], however, a split has occured between
Windows and Linux with respect to SRK templates. The Linux SRK is
generated with the unique field size set to 0, in Windows, it properly
sets the size to key size in bytes and the unique data to all 0's of that
size. Note the proper templates for SRKs is covered in spec [3].
However, the most important thing, is that both SRKs are passwordless,
and thus they should be interchangable. If Windows is the first to make
the SRK, systemd will gladly accept it and vice-versa.

1. Without the bindKey being utilized, an attacker was able to intercept
this and fake a key, thus being able to decrypt and encrypt traffic as
needed. Introduction of the bindKey strengthened this, but allows for
the attacker to brute force AES128CFB using pin guesses. Introduction of
the salt increases the difficulty of this attack as well as DA attacks
on the TPM objects itself.

2. https://github.com/tpm2-software

3. https://trustedcomputinggroup.org/wp-content/uploads/TCG-TPM-v2.0-Provisioning-Guidance-Published-v1r1.pdf

Fixes: #20668
Fixes: #22637

Signed-off-by: William Roberts <william.c.roberts@intel.com>
  • Loading branch information
William Roberts committed Feb 23, 2023
1 parent 06762aa commit 4fc6f51
Show file tree
Hide file tree
Showing 13 changed files with 489 additions and 69 deletions.
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ Features:

* pick up creds from EFI vars

* Add and pickup tpm2 metadata for creds structure.

* sd-boot: we probably should include all BootXY EFI variable defined boot
entries in our menu, and then suppress ourselves. Benefit: instant
compatibility with all other OSes which register things there, in particular
Expand Down
9 changes: 7 additions & 2 deletions src/cryptenroll/cryptenroll-tpm2.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ int enroll_tpm2(struct crypt_device *cd,
_cleanup_(erase_and_freep) void *secret = NULL;
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *signature_json = NULL;
_cleanup_(erase_and_freep) char *base64_encoded = NULL;
size_t secret_size, blob_size, hash_size, pubkey_size = 0;
_cleanup_(freep) void *srk_buf = NULL;
size_t secret_size, blob_size, hash_size, pubkey_size = 0, srk_buf_size = 0;
_cleanup_free_ void *blob = NULL, *hash = NULL, *pubkey = NULL;
uint16_t pcr_bank, primary_alg;
const char *node;
Expand Down Expand Up @@ -214,7 +215,9 @@ int enroll_tpm2(struct crypt_device *cd,
&blob, &blob_size,
&hash, &hash_size,
&pcr_bank,
&primary_alg);
&primary_alg,
&srk_buf,
&srk_buf_size);
if (r < 0)
return r;

Expand Down Expand Up @@ -245,6 +248,7 @@ int enroll_tpm2(struct crypt_device *cd,
primary_alg,
blob, blob_size,
hash, hash_size,
srk_buf, srk_buf_size,
&secret2, &secret2_size);
if (r < 0)
return r;
Expand Down Expand Up @@ -283,6 +287,7 @@ int enroll_tpm2(struct crypt_device *cd,
hash, hash_size,
use_pin ? binary_salt : NULL,
use_pin ? sizeof(binary_salt) : 0,
srk_buf, srk_buf_size,
flags,
&v);
if (r < 0)
Expand Down
15 changes: 11 additions & 4 deletions src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-tpm2.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ _public_ int cryptsetup_token_open_pin(
void *usrptr /* plugin defined parameter passed to crypt_activate_by_token*() API */) {

_cleanup_(erase_and_freep) char *base64_encoded = NULL, *pin_string = NULL;
_cleanup_free_ void *blob = NULL, *pubkey = NULL, *policy_hash = NULL, *salt = NULL;
size_t blob_size, policy_hash_size, decrypted_key_size, pubkey_size, salt_size = 0;
_cleanup_free_ void *blob = NULL, *pubkey = NULL, *policy_hash = NULL, *salt = NULL, *srk_buf = NULL;
size_t blob_size, policy_hash_size, decrypted_key_size, pubkey_size, salt_size = 0, srk_buf_size = 0;
_cleanup_(erase_and_freep) void *decrypted_key = NULL;
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
uint32_t hash_pcr_mask, pubkey_pcr_mask;
Expand Down Expand Up @@ -92,6 +92,8 @@ _public_ int cryptsetup_token_open_pin(
&policy_hash_size,
&salt,
&salt_size,
&srk_buf,
&srk_buf_size,
&flags);
if (r < 0)
return log_debug_open_error(cd, r);
Expand All @@ -114,6 +116,8 @@ _public_ int cryptsetup_token_open_pin(
policy_hash_size,
salt,
salt_size,
srk_buf,
srk_buf_size,
flags,
&decrypted_key,
&decrypted_key_size);
Expand Down Expand Up @@ -172,9 +176,9 @@ _public_ void cryptsetup_token_dump(
const char *json /* validated 'systemd-tpm2' token if cryptsetup_token_validate is defined */) {

_cleanup_free_ char *hash_pcrs_str = NULL, *pubkey_pcrs_str = NULL, *blob_str = NULL, *policy_hash_str = NULL, *pubkey_str = NULL;
_cleanup_free_ void *blob = NULL, *pubkey = NULL, *policy_hash = NULL, *salt = NULL;
_cleanup_free_ void *blob = NULL, *pubkey = NULL, *policy_hash = NULL, *salt = NULL, *srk_buf = NULL;
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
size_t blob_size, policy_hash_size, pubkey_size, salt_size = 0;
size_t blob_size, policy_hash_size, pubkey_size, salt_size = 0, srk_buf_size = 0;
uint32_t hash_pcr_mask, pubkey_pcr_mask;
uint16_t pcr_bank, primary_alg;
TPM2Flags flags = 0;
Expand All @@ -201,6 +205,8 @@ _public_ void cryptsetup_token_dump(
&policy_hash_size,
&salt,
&salt_size,
&srk_buf,
&srk_buf_size,
&flags);
if (r < 0)
return (void) crypt_log_debug_errno(cd, r, "Failed to parse " TOKEN_NAME " JSON fields: %m");
Expand Down Expand Up @@ -234,6 +240,7 @@ _public_ void cryptsetup_token_dump(
crypt_log(cd, "\ttpm2-policy-hash:" CRYPT_DUMP_LINE_SEP "%s\n", policy_hash_str);
crypt_log(cd, "\ttpm2-pin: %s\n", true_false(flags & TPM2_FLAGS_USE_PIN));
crypt_log(cd, "\ttpm2-salt: %s\n", true_false(salt));
crypt_log(cd, "\ttpm2-srk: %s\n", true_false(srk_buf));
}

/*
Expand Down
3 changes: 3 additions & 0 deletions src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ int acquire_luks2_key(
size_t policy_hash_size,
const void *salt,
size_t salt_size,
const void *srk_buf,
size_t srk_buf_size,
TPM2Flags flags,
void **ret_decrypted_key,
size_t *ret_decrypted_key_size) {
Expand Down Expand Up @@ -89,5 +91,6 @@ int acquire_luks2_key(
primary_alg,
key_data, key_data_size,
policy_hash, policy_hash_size,
srk_buf, srk_buf_size,
ret_decrypted_key, ret_decrypted_key_size);
}
2 changes: 2 additions & 0 deletions src/cryptsetup/cryptsetup-tokens/luks2-tpm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ int acquire_luks2_key(
size_t policy_hash_size,
const void *salt,
size_t salt_size,
const void *srk_buf,
size_t srk_buf_size,
TPM2Flags flags,
void **ret_decrypted_key,
size_t *ret_decrypted_key_size);
15 changes: 13 additions & 2 deletions src/cryptsetup/cryptsetup-tpm2.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ int acquire_tpm2_key(
size_t policy_hash_size,
const void *salt,
size_t salt_size,
const void *srk_buf,
size_t srk_buf_size,
TPM2Flags flags,
usec_t until,
bool headless,
Expand Down Expand Up @@ -141,6 +143,8 @@ int acquire_tpm2_key(
blob_size,
policy_hash,
policy_hash_size,
srk_buf,
srk_buf_size,
ret_decrypted_key,
ret_decrypted_key_size);

Expand Down Expand Up @@ -181,6 +185,8 @@ int acquire_tpm2_key(
blob_size,
policy_hash,
policy_hash_size,
srk_buf,
srk_buf_size,
ret_decrypted_key,
ret_decrypted_key_size);
/* We get this error in case there is an authentication policy mismatch. This should
Expand Down Expand Up @@ -210,6 +216,8 @@ int find_tpm2_auto_data(
size_t *ret_policy_hash_size,
void **ret_salt,
size_t *ret_salt_size,
void **ret_srk_buf,
size_t *ret_srk_buf_size,
TPM2Flags *ret_flags,
int *ret_keyslot,
int *ret_token) {
Expand All @@ -219,9 +227,9 @@ int find_tpm2_auto_data(
assert(cd);

for (token = start_token; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
_cleanup_free_ void *blob = NULL, *policy_hash = NULL, *pubkey = NULL, *salt = NULL;
_cleanup_free_ void *blob = NULL, *policy_hash = NULL, *pubkey = NULL, *salt = NULL, *srk_buf = NULL;
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
size_t blob_size, policy_hash_size, pubkey_size, salt_size = 0;
size_t blob_size, policy_hash_size, pubkey_size, salt_size = 0, srk_buf_size = 0;
uint32_t hash_pcr_mask, pubkey_pcr_mask;
uint16_t pcr_bank, primary_alg;
TPM2Flags flags;
Expand All @@ -244,6 +252,7 @@ int find_tpm2_auto_data(
&blob, &blob_size,
&policy_hash, &policy_hash_size,
&salt, &salt_size,
&srk_buf, &srk_buf_size,
&flags);
if (r == -EUCLEAN) /* Gracefully handle issues in JSON fields not owned by us */
continue;
Expand All @@ -270,6 +279,8 @@ int find_tpm2_auto_data(
*ret_salt_size = salt_size;
*ret_keyslot = keyslot;
*ret_token = token;
*ret_srk_buf = TAKE_PTR(srk_buf);
*ret_srk_buf_size = srk_buf_size;
*ret_flags = flags;
return 0;
}
Expand Down
4 changes: 4 additions & 0 deletions src/cryptsetup/cryptsetup-tpm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ int acquire_tpm2_key(
size_t policy_hash_size,
const void *salt,
size_t salt_size,
const void *srk_buf,
size_t salt_srk_buf_size,
TPM2Flags flags,
usec_t until,
bool headless,
Expand All @@ -53,6 +55,8 @@ int find_tpm2_auto_data(
size_t *ret_policy_hash_size,
void **ret_salt,
size_t *ret_salt_size,
void **ret_srk_buf,
size_t *ret_srk_size,
TPM2Flags *ret_flags,
int *ret_keyslot,
int *ret_token);
Expand Down
7 changes: 5 additions & 2 deletions src/cryptsetup/cryptsetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,7 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
key_data, key_data_size,
/* policy_hash= */ NULL, /* policy_hash_size= */ 0, /* we don't know the policy hash */
/* salt= */ NULL, /* salt_size= */ 0,
/* srk_buf= */ NULL, /* srk_buf_size= */ 0,
arg_tpm2_pin ? TPM2_FLAGS_USE_PIN : 0,
until,
arg_headless,
Expand Down Expand Up @@ -1704,8 +1705,8 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
* works. */

for (;;) {
_cleanup_free_ void *pubkey = NULL, *salt = NULL;
size_t pubkey_size = 0, salt_size = 0;
_cleanup_free_ void *pubkey = NULL, *salt = NULL, *srk_buf = NULL;
size_t pubkey_size = 0, salt_size = 0, srk_buf_size = 0;
uint32_t hash_pcr_mask, pubkey_pcr_mask;
uint16_t pcr_bank, primary_alg;
TPM2Flags tpm2_flags;
Expand All @@ -1722,6 +1723,7 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
&blob, &blob_size,
&policy_hash, &policy_hash_size,
&salt, &salt_size,
&srk_buf, &srk_buf_size,
&tpm2_flags,
&keyslot,
&token);
Expand Down Expand Up @@ -1752,6 +1754,7 @@ static int attach_luks_or_plain_or_bitlk_by_tpm2(
blob, blob_size,
policy_hash, policy_hash_size,
salt, salt_size,
srk_buf, srk_buf_size,
tpm2_flags,
until,
arg_headless,
Expand Down
9 changes: 6 additions & 3 deletions src/partition/repart.c
Original file line number Diff line number Diff line change
Expand Up @@ -3376,8 +3376,8 @@ static int partition_encrypt(Context *context, Partition *p, const char *node) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
_cleanup_(erase_and_freep) void *secret = NULL;
_cleanup_free_ void *pubkey = NULL;
_cleanup_free_ void *blob = NULL, *hash = NULL;
size_t secret_size, blob_size, hash_size, pubkey_size = 0;
_cleanup_free_ void *blob = NULL, *hash = NULL, *srk_buf = NULL;
size_t secret_size, blob_size, hash_size, pubkey_size = 0, srk_buf_size = 0;
ssize_t base64_encoded_size;
uint16_t pcr_bank, primary_alg;
int keyslot;
Expand All @@ -3402,7 +3402,9 @@ static int partition_encrypt(Context *context, Partition *p, const char *node) {
&blob, &blob_size,
&hash, &hash_size,
&pcr_bank,
&primary_alg);
&primary_alg,
&srk_buf,
&srk_buf_size);
if (r < 0)
return log_error_errno(r, "Failed to seal to TPM2: %m");

Expand Down Expand Up @@ -3434,6 +3436,7 @@ static int partition_encrypt(Context *context, Partition *p, const char *node) {
blob, blob_size,
hash, hash_size,
NULL, 0, /* no salt because tpm2_seal has no pin */
srk_buf, srk_buf_size,
0,
&v);
if (r < 0)
Expand Down
10 changes: 9 additions & 1 deletion src/shared/creds-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,9 @@ int encrypt_credential_and_warn(
&tpm2_blob, &tpm2_blob_size,
&tpm2_policy_hash, &tpm2_policy_hash_size,
&tpm2_pcr_bank,
&tpm2_primary_alg);
&tpm2_primary_alg,
/* ret_srk_buf= */ NULL,
/* ret_srk_buf_size= */ 0);
if (r < 0) {
if (sd_id128_equal(with_key, _CRED_AUTO_INITRD))
log_warning("TPM2 present and used, but we didn't manage to talk to it. Credential will be refused if SecureBoot is enabled.");
Expand Down Expand Up @@ -1033,6 +1035,10 @@ int decrypt_credential_and_warn(
le32toh(z->size));
}

/*
* TODO: Add the SRK data to the credential structure so it can be plumbed
* through and used to verify the TPM session.
*/
r = tpm2_unseal(tpm2_device,
le64toh(t->pcr_mask),
le16toh(t->pcr_bank),
Expand All @@ -1046,6 +1052,8 @@ int decrypt_credential_and_warn(
le32toh(t->blob_size),
t->policy_hash_and_blob + le32toh(t->blob_size),
le32toh(t->policy_hash_size),
/* srk_buf= */ NULL,
/* srk_buf_size= */ 0,
&tpm2_key,
&tpm2_key_size);
if (r < 0)
Expand Down

0 comments on commit 4fc6f51

Please sign in to comment.