From: Jianglei Nie <hidden> Date: 2021-12-12 13:56:17
Line 36 (#1) allocates a memory chunk for scratch by kmalloc(), but
it is never freed through the function, which will lead to a memory
leak.
We should kfree() scratch before the function returns (#2, #3 and #4).
31 static int tpm2_key_encode(struct trusted_key_payload *payload,
32 struct trusted_key_options *options,
33 u8 *src, u32 len)
34 {
36 u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);
// #1: kmalloc space
37 u8 *work = scratch, *work1;
50 if (!scratch)
51 return -ENOMEM;
56 if (options->blobauth_len == 0) {
60 if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
61 return PTR_ERR(w); // #2: missing kfree
63 }
71 if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
72 "BUG: scratch buffer is too small"))
73 return -EINVAL; // #3: missing kfree
// #4: missing kfree: scratch is never used afterwards.
82 if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
83 return PTR_ERR(work1);
85 return work1 - payload->blob;
86 }
Signed-off-by: Jianglei Nie <redacted>
---
security/keys/trusted-keys/trusted_tpm2.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
@@ -57,8 +57,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,unsignedcharbool[3],*w=bool;/* tag 0 is emptyAuth */w=asn1_encode_boolean(w,w+sizeof(bool),true);-if(WARN(IS_ERR(w),"BUG: Boolean failed to encode"))+if(WARN(IS_ERR(w),"BUG: Boolean failed to encode")){+kfree(scratch);returnPTR_ERR(w);+}work=asn1_encode_tag(work,end_work,0,bool,w-bool);}
@@ -69,9 +71,12 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,*trigger,soifitdoesthere'ssomethingnefariousgoingon*/if(WARN(work-scratch+pub_len+priv_len+14>SCRATCH_SIZE,-"BUG: scratch buffer is too small"))+"BUG: scratch buffer is too small")){+kfree(scratch);return-EINVAL;+}+kfree(scratch);work=asn1_encode_integer(work,end_work,options->keyhandle);work=asn1_encode_octet_string(work,end_work,pub,pub_len);work=asn1_encode_octet_string(work,end_work,priv,priv_len);
From: "Serge E. Hallyn" <serge@hallyn.com> Date: 2021-12-12 21:21:35
On Sun, Dec 12, 2021 at 09:54:03PM +0800, Jianglei Nie wrote:
Line 36 (#1) allocates a memory chunk for scratch by kmalloc(), but
it is never freed through the function, which will lead to a memory
leak.
We should kfree() scratch before the function returns (#2, #3 and #4).
31 static int tpm2_key_encode(struct trusted_key_payload *payload,
32 struct trusted_key_options *options,
33 u8 *src, u32 len)
34 {
36 u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);
// #1: kmalloc space
37 u8 *work = scratch, *work1;
50 if (!scratch)
51 return -ENOMEM;
56 if (options->blobauth_len == 0) {
60 if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
61 return PTR_ERR(w); // #2: missing kfree
63 }
71 if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
72 "BUG: scratch buffer is too small"))
73 return -EINVAL; // #3: missing kfree
// #4: missing kfree: scratch is never used afterwards.
82 if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
83 return PTR_ERR(work1);
85 return work1 - payload->blob;
86 }
Signed-off-by: Jianglei Nie <redacted>
I don't know that we need to keep the line by line recap in
the full git log, but it def looks correct:
Reviewed-by: Serge Hallyn <serge@hallyn.com>
thanks,
-serge
@@ -57,8 +57,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,unsignedcharbool[3],*w=bool;/* tag 0 is emptyAuth */w=asn1_encode_boolean(w,w+sizeof(bool),true);-if(WARN(IS_ERR(w),"BUG: Boolean failed to encode"))+if(WARN(IS_ERR(w),"BUG: Boolean failed to encode")){+kfree(scratch);returnPTR_ERR(w);+}work=asn1_encode_tag(work,end_work,0,bool,w-bool);}
@@ -69,9 +71,12 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,*trigger,soifitdoesthere'ssomethingnefariousgoingon*/if(WARN(work-scratch+pub_len+priv_len+14>SCRATCH_SIZE,-"BUG: scratch buffer is too small"))+"BUG: scratch buffer is too small")){+kfree(scratch);return-EINVAL;+}+kfree(scratch);work=asn1_encode_integer(work,end_work,options->keyhandle);work=asn1_encode_octet_string(work,end_work,pub,pub_len);work=asn1_encode_octet_string(work,end_work,priv,priv_len);