[PATCH -next] firmware_loader: Fix possible resource leak in fw_log_firmware_info()

Gaosheng Cui posted 1 patch 1 month, 1 week ago
drivers/base/firmware_loader/main.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
[PATCH -next] firmware_loader: Fix possible resource leak in fw_log_firmware_info()
Posted by Gaosheng Cui 1 month, 1 week ago
The alg instance should be released under the exception path, otherwise
there may be resource leak here.

To mitigate this, free the alg instance with crypto_free_shash when kmalloc
fails.

Fixes: 02fe26f25325 ("firmware_loader: Add debug message with checksum for FW file")
Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
---
 drivers/base/firmware_loader/main.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
index 324a9a3c087a..c6664a787969 100644
--- a/drivers/base/firmware_loader/main.c
+++ b/drivers/base/firmware_loader/main.c
@@ -829,19 +829,18 @@ static void fw_log_firmware_info(const struct firmware *fw, const char *name, st
 	shash->tfm = alg;
 
 	if (crypto_shash_digest(shash, fw->data, fw->size, sha256buf) < 0)
-		goto out_shash;
+		goto out_free;
 
 	for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
 		sprintf(&outbuf[i * 2], "%02x", sha256buf[i]);
 	outbuf[SHA256_BLOCK_SIZE] = 0;
 	dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
 
-out_shash:
-	crypto_free_shash(alg);
 out_free:
 	kfree(shash);
 	kfree(outbuf);
 	kfree(sha256buf);
+	crypto_free_shash(alg);
 }
 #else
 static void fw_log_firmware_info(const struct firmware *fw, const char *name,
-- 
2.25.1
Re: [PATCH -next] firmware_loader: Fix possible resource leak in fw_log_firmware_info()
Posted by Amadeusz Sławiński 1 month ago
On 10/16/2024 1:03 PM, Gaosheng Cui wrote:
> The alg instance should be released under the exception path, otherwise
> there may be resource leak here.
> 
> To mitigate this, free the alg instance with crypto_free_shash when kmalloc
> fails.
> 
> Fixes: 02fe26f25325 ("firmware_loader: Add debug message with checksum for FW file")
> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
> ---

Makes sense, thanks!

Reviewed-by: Amadeusz Sławiński <amadeuszx.slawinski@linux.intel.com>
Re: [PATCH -next] firmware_loader: Fix possible resource leak in fw_log_firmware_info()
Posted by Russ Weight 1 month ago
On Wed, Oct 16, 2024 at 07:03:35PM +0800, Gaosheng Cui wrote:
> The alg instance should be released under the exception path, otherwise
> there may be resource leak here.
> 
> To mitigate this, free the alg instance with crypto_free_shash when kmalloc
> fails.
> 
> Fixes: 02fe26f25325 ("firmware_loader: Add debug message with checksum for FW file")
> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>

Reviewed-by: Russ Weight <russ.weight@linux.dev>

> ---
>  drivers/base/firmware_loader/main.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
> index 324a9a3c087a..c6664a787969 100644
> --- a/drivers/base/firmware_loader/main.c
> +++ b/drivers/base/firmware_loader/main.c
> @@ -829,19 +829,18 @@ static void fw_log_firmware_info(const struct firmware *fw, const char *name, st
>  	shash->tfm = alg;
>  
>  	if (crypto_shash_digest(shash, fw->data, fw->size, sha256buf) < 0)
> -		goto out_shash;
> +		goto out_free;
>  
>  	for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
>  		sprintf(&outbuf[i * 2], "%02x", sha256buf[i]);
>  	outbuf[SHA256_BLOCK_SIZE] = 0;
>  	dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
>  
> -out_shash:
> -	crypto_free_shash(alg);
>  out_free:
>  	kfree(shash);
>  	kfree(outbuf);
>  	kfree(sha256buf);
> +	crypto_free_shash(alg);
>  }
>  #else
>  static void fw_log_firmware_info(const struct firmware *fw, const char *name,
> -- 
> 2.25.1
>
Re: [PATCH -next] firmware_loader: Fix possible resource leak in fw_log_firmware_info()
Posted by Greg KH 1 month, 1 week ago
On Wed, Oct 16, 2024 at 07:03:35PM +0800, Gaosheng Cui wrote:
> The alg instance should be released under the exception path, otherwise
> there may be resource leak here.
> 
> To mitigate this, free the alg instance with crypto_free_shash when kmalloc
> fails.
> 
> Fixes: 02fe26f25325 ("firmware_loader: Add debug message with checksum for FW file")
> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
> ---
>  drivers/base/firmware_loader/main.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
> index 324a9a3c087a..c6664a787969 100644
> --- a/drivers/base/firmware_loader/main.c
> +++ b/drivers/base/firmware_loader/main.c
> @@ -829,19 +829,18 @@ static void fw_log_firmware_info(const struct firmware *fw, const char *name, st
>  	shash->tfm = alg;
>  
>  	if (crypto_shash_digest(shash, fw->data, fw->size, sha256buf) < 0)
> -		goto out_shash;
> +		goto out_free;
>  
>  	for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
>  		sprintf(&outbuf[i * 2], "%02x", sha256buf[i]);
>  	outbuf[SHA256_BLOCK_SIZE] = 0;
>  	dev_dbg(device, "Loaded FW: %s, sha256: %s\n", name, outbuf);
>  
> -out_shash:
> -	crypto_free_shash(alg);
>  out_free:
>  	kfree(shash);
>  	kfree(outbuf);
>  	kfree(sha256buf);
> +	crypto_free_shash(alg);
>  }
>  #else
>  static void fw_log_firmware_info(const struct firmware *fw, const char *name,
> -- 
> 2.25.1
> 

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman.  You have sent him
a patch that has triggered this response.  He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created.  Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- You have marked a patch with a "Fixes:" tag for a commit that is in an
  older released kernel, yet you do not have a cc: stable line in the
  signed-off-by area at all, which means that the patch will not be
  applied to any older kernel releases.  To properly fix this, please
  follow the documented rules in the
  Documentation/process/stable-kernel-rules.rst file for how to resolve
  this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot