Skip to content

Commit 939cafa

Browse files
ebiggersgregkh
authored andcommitted
KEYS: trusted: sanitize all key material
commit ee618b4619b72527aaed765f0f0b74072b281159 upstream. As the previous patch did for encrypted-keys, zero sensitive any potentially sensitive data related to the "trusted" key type before it is freed. Notably, we were not zeroing the tpm_buf structures in which the actual key is stored for TPM seal and unseal, nor were we zeroing the trusted_key_payload in certain error paths. Cc: Mimi Zohar <zohar@linux.vnet.ibm.com> Cc: David Safford <safford@us.ibm.com> Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: James Morris <james.l.morris@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 75f82a7 commit 939cafa

1 file changed

Lines changed: 22 additions & 28 deletions

File tree

security/keys/trusted.c

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static int TSS_sha1(const unsigned char *data, unsigned int datalen,
6969
}
7070

7171
ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
72-
kfree(sdesc);
72+
kzfree(sdesc);
7373
return ret;
7474
}
7575

@@ -113,7 +113,7 @@ static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
113113
if (!ret)
114114
ret = crypto_shash_final(&sdesc->shash, digest);
115115
out:
116-
kfree(sdesc);
116+
kzfree(sdesc);
117117
return ret;
118118
}
119119

@@ -164,7 +164,7 @@ static int TSS_authhmac(unsigned char *digest, const unsigned char *key,
164164
paramdigest, TPM_NONCE_SIZE, h1,
165165
TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
166166
out:
167-
kfree(sdesc);
167+
kzfree(sdesc);
168168
return ret;
169169
}
170170

@@ -245,7 +245,7 @@ static int TSS_checkhmac1(unsigned char *buffer,
245245
if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
246246
ret = -EINVAL;
247247
out:
248-
kfree(sdesc);
248+
kzfree(sdesc);
249249
return ret;
250250
}
251251

@@ -346,7 +346,7 @@ static int TSS_checkhmac2(unsigned char *buffer,
346346
if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
347347
ret = -EINVAL;
348348
out:
349-
kfree(sdesc);
349+
kzfree(sdesc);
350350
return ret;
351351
}
352352

@@ -563,7 +563,7 @@ static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
563563
*bloblen = storedsize;
564564
}
565565
out:
566-
kfree(td);
566+
kzfree(td);
567567
return ret;
568568
}
569569

@@ -677,7 +677,7 @@ static int key_seal(struct trusted_key_payload *p,
677677
if (ret < 0)
678678
pr_info("trusted_key: srkseal failed (%d)\n", ret);
679679

680-
kfree(tb);
680+
kzfree(tb);
681681
return ret;
682682
}
683683

@@ -702,7 +702,7 @@ static int key_unseal(struct trusted_key_payload *p,
702702
/* pull migratable flag out of sealed key */
703703
p->migratable = p->key[--p->key_len];
704704

705-
kfree(tb);
705+
kzfree(tb);
706706
return ret;
707707
}
708708

@@ -984,12 +984,12 @@ static int trusted_instantiate(struct key *key,
984984
if (!ret && options->pcrlock)
985985
ret = pcrlock(options->pcrlock);
986986
out:
987-
kfree(datablob);
988-
kfree(options);
987+
kzfree(datablob);
988+
kzfree(options);
989989
if (!ret)
990990
rcu_assign_keypointer(key, payload);
991991
else
992-
kfree(payload);
992+
kzfree(payload);
993993
return ret;
994994
}
995995

@@ -998,8 +998,7 @@ static void trusted_rcu_free(struct rcu_head *rcu)
998998
struct trusted_key_payload *p;
999999

10001000
p = container_of(rcu, struct trusted_key_payload, rcu);
1001-
memset(p->key, 0, p->key_len);
1002-
kfree(p);
1001+
kzfree(p);
10031002
}
10041003

10051004
/*
@@ -1041,13 +1040,13 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
10411040
ret = datablob_parse(datablob, new_p, new_o);
10421041
if (ret != Opt_update) {
10431042
ret = -EINVAL;
1044-
kfree(new_p);
1043+
kzfree(new_p);
10451044
goto out;
10461045
}
10471046

10481047
if (!new_o->keyhandle) {
10491048
ret = -EINVAL;
1050-
kfree(new_p);
1049+
kzfree(new_p);
10511050
goto out;
10521051
}
10531052

@@ -1061,22 +1060,22 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
10611060
ret = key_seal(new_p, new_o);
10621061
if (ret < 0) {
10631062
pr_info("trusted_key: key_seal failed (%d)\n", ret);
1064-
kfree(new_p);
1063+
kzfree(new_p);
10651064
goto out;
10661065
}
10671066
if (new_o->pcrlock) {
10681067
ret = pcrlock(new_o->pcrlock);
10691068
if (ret < 0) {
10701069
pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1071-
kfree(new_p);
1070+
kzfree(new_p);
10721071
goto out;
10731072
}
10741073
}
10751074
rcu_assign_keypointer(key, new_p);
10761075
call_rcu(&p->rcu, trusted_rcu_free);
10771076
out:
1078-
kfree(datablob);
1079-
kfree(new_o);
1077+
kzfree(datablob);
1078+
kzfree(new_o);
10801079
return ret;
10811080
}
10821081

@@ -1105,24 +1104,19 @@ static long trusted_read(const struct key *key, char __user *buffer,
11051104
for (i = 0; i < p->blob_len; i++)
11061105
bufp = hex_byte_pack(bufp, p->blob[i]);
11071106
if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) {
1108-
kfree(ascii_buf);
1107+
kzfree(ascii_buf);
11091108
return -EFAULT;
11101109
}
1111-
kfree(ascii_buf);
1110+
kzfree(ascii_buf);
11121111
return 2 * p->blob_len;
11131112
}
11141113

11151114
/*
1116-
* trusted_destroy - before freeing the key, clear the decrypted data
1115+
* trusted_destroy - clear and free the key's payload
11171116
*/
11181117
static void trusted_destroy(struct key *key)
11191118
{
1120-
struct trusted_key_payload *p = key->payload.data[0];
1121-
1122-
if (!p)
1123-
return;
1124-
memset(p->key, 0, p->key_len);
1125-
kfree(key->payload.data[0]);
1119+
kzfree(key->payload.data[0]);
11261120
}
11271121

11281122
struct key_type key_type_trusted = {

0 commit comments

Comments
 (0)