Skip to content

Commit ffcaa21

Browse files
committed
KEYS: trusted: Fix memory leak in tpm2_key_encode()
'scratch' is never freed. Fix this by calling kfree() in the success, and in the error case. Cc: stable@vger.kernel.org # +v5.13 Fixes: f221974 ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs") Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
1 parent 8f6a15f commit ffcaa21

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

security/keys/trusted-keys/trusted_tpm2.c

+18-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
3838
u8 *end_work = scratch + SCRATCH_SIZE;
3939
u8 *priv, *pub;
4040
u16 priv_len, pub_len;
41+
int ret;
4142

4243
priv_len = get_unaligned_be16(src) + 2;
4344
priv = src;
@@ -57,8 +58,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
5758
unsigned char bool[3], *w = bool;
5859
/* tag 0 is emptyAuth */
5960
w = asn1_encode_boolean(w, w + sizeof(bool), true);
60-
if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
61-
return PTR_ERR(w);
61+
if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) {
62+
ret = PTR_ERR(w);
63+
goto err;
64+
}
6265
work = asn1_encode_tag(work, end_work, 0, bool, w - bool);
6366
}
6467

@@ -69,8 +72,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
6972
* trigger, so if it does there's something nefarious going on
7073
*/
7174
if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
72-
"BUG: scratch buffer is too small"))
73-
return -EINVAL;
75+
"BUG: scratch buffer is too small")) {
76+
ret = -EINVAL;
77+
goto err;
78+
}
7479

7580
work = asn1_encode_integer(work, end_work, options->keyhandle);
7681
work = asn1_encode_octet_string(work, end_work, pub, pub_len);
@@ -79,10 +84,17 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
7984
work1 = payload->blob;
8085
work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),
8186
scratch, work - scratch);
82-
if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
83-
return PTR_ERR(work1);
87+
if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) {
88+
ret = PTR_ERR(work1);
89+
goto err;
90+
}
8491

92+
kfree(scratch);
8593
return work1 - payload->blob;
94+
95+
err:
96+
kfree(scratch);
97+
return ret;
8698
}
8799

88100
struct tpm2_key_context {

0 commit comments

Comments
 (0)