diff mbox series

[-next] KEYS: trusted: Switch to kmemdup_nul()

Message ID 20210402092346.2444932-1-yangyingliang@huawei.com (mailing list archive)
State New
Headers show
Series [-next] KEYS: trusted: Switch to kmemdup_nul() | expand

Commit Message

Yang Yingliang April 2, 2021, 9:23 a.m. UTC
Use kmemdup_nul() helper instead of open-coding to
simplify the code.

Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 security/keys/trusted-keys/trusted_tpm1.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Comments

Jarkko Sakkinen April 4, 2021, 4:01 p.m. UTC | #1
On Fri, Apr 02, 2021 at 05:23:46PM +0800, Yang Yingliang wrote:
> Use kmemdup_nul() helper instead of open-coding to
> simplify the code.
> 
> Reported-by: Hulk Robot <hulkci@huawei.com>
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> ---
>  security/keys/trusted-keys/trusted_tpm1.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)


Acked-by: Jarkko Sakkinen <jarkko@kernel.org>

/Jarkko

> 
> diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
> index 493eb91ed017..90ded4757e79 100644
> --- a/security/keys/trusted-keys/trusted_tpm1.c
> +++ b/security/keys/trusted-keys/trusted_tpm1.c
> @@ -978,11 +978,9 @@ static int trusted_instantiate(struct key *key,
>  	if (datalen <= 0 || datalen > 32767 || !prep->data)
>  		return -EINVAL;
>  
> -	datablob = kmalloc(datalen + 1, GFP_KERNEL);
> +	datablob = kmemdup_nul(prep->data, datalen, GFP_KERNEL);
>  	if (!datablob)
>  		return -ENOMEM;
> -	memcpy(datablob, prep->data, datalen);
> -	datablob[datalen] = '\0';
>  
>  	options = trusted_options_alloc();
>  	if (!options) {
> -- 
> 2.25.1
> 
>
diff mbox series

Patch

diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
index 493eb91ed017..90ded4757e79 100644
--- a/security/keys/trusted-keys/trusted_tpm1.c
+++ b/security/keys/trusted-keys/trusted_tpm1.c
@@ -978,11 +978,9 @@  static int trusted_instantiate(struct key *key,
 	if (datalen <= 0 || datalen > 32767 || !prep->data)
 		return -EINVAL;
 
-	datablob = kmalloc(datalen + 1, GFP_KERNEL);
+	datablob = kmemdup_nul(prep->data, datalen, GFP_KERNEL);
 	if (!datablob)
 		return -ENOMEM;
-	memcpy(datablob, prep->data, datalen);
-	datablob[datalen] = '\0';
 
 	options = trusted_options_alloc();
 	if (!options) {