All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hash: fix a memory leak
@ 2023-06-12 19:59 Sergei Antonov
  2023-06-12 21:17 ` Simon Glass
  2023-07-15 15:03 ` Tom Rini
  0 siblings, 2 replies; 3+ messages in thread
From: Sergei Antonov @ 2023-06-12 19:59 UTC (permalink / raw)
  To: u-boot; +Cc: Sergei Antonov, Breno Lima

memalign() returns a pointer which is to be freed by free(). To call
unmap_sysmem() is incorrect, furthermore it was called in a wrong scope.

Also add a check for allocation error.

Fixes: d7af2baa49c6 ("crypto/fsl: Fix HW accelerated hash commands")
Cc: Breno Lima <breno.lima@nxp.com>
Signed-off-by: Sergei Antonov <saproj@gmail.com>
---
 common/hash.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/common/hash.c b/common/hash.c
index 9a52d6073ceb..cbffdfd6db36 100644
--- a/common/hash.c
+++ b/common/hash.c
@@ -586,6 +586,8 @@ int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
 
 		output = memalign(ARCH_DMA_MINALIGN,
 				  sizeof(uint32_t) * HASH_MAX_DIGEST_SIZE);
+		if (!output)
+			return CMD_RET_FAILURE;
 
 		buf = map_sysmem(addr, len);
 		algo->hash_func_ws(buf, len, output, algo->chunk_size);
@@ -602,6 +604,7 @@ int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
 					flags & HASH_FLAG_ENV)) {
 				printf("ERROR: %s does not contain a valid "
 					"%s sum\n", *argv, algo->name);
+				free(output);
 				return 1;
 			}
 			if (memcmp(output, vsum, algo->digest_size) != 0) {
@@ -612,6 +615,7 @@ int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
 				for (i = 0; i < algo->digest_size; i++)
 					printf("%02x", vsum[i]);
 				puts(" ** ERROR **\n");
+				free(output);
 				return 1;
 			}
 		} else {
@@ -622,10 +626,10 @@ int hash_command(const char *algo_name, int flags, struct cmd_tbl *cmdtp,
 				store_result(algo, output, *argv,
 					flags & HASH_FLAG_ENV);
 			}
-		unmap_sysmem(output);
-
 		}
 
+		free(output);
+
 	/* Horrible code size hack for boards that just want crc32 */
 	} else {
 		ulong crc;
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] hash: fix a memory leak
  2023-06-12 19:59 [PATCH] hash: fix a memory leak Sergei Antonov
@ 2023-06-12 21:17 ` Simon Glass
  2023-07-15 15:03 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Simon Glass @ 2023-06-12 21:17 UTC (permalink / raw)
  To: Sergei Antonov; +Cc: u-boot, Breno Lima

On Mon, 12 Jun 2023 at 21:01, Sergei Antonov <saproj@gmail.com> wrote:
>
> memalign() returns a pointer which is to be freed by free(). To call
> unmap_sysmem() is incorrect, furthermore it was called in a wrong scope.
>
> Also add a check for allocation error.
>
> Fixes: d7af2baa49c6 ("crypto/fsl: Fix HW accelerated hash commands")
> Cc: Breno Lima <breno.lima@nxp.com>
> Signed-off-by: Sergei Antonov <saproj@gmail.com>
> ---
>  common/hash.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>

Reviewed-by: Simon Glass <sjg@chromium.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] hash: fix a memory leak
  2023-06-12 19:59 [PATCH] hash: fix a memory leak Sergei Antonov
  2023-06-12 21:17 ` Simon Glass
@ 2023-07-15 15:03 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Rini @ 2023-07-15 15:03 UTC (permalink / raw)
  To: Sergei Antonov; +Cc: u-boot, Breno Lima

[-- Attachment #1: Type: text/plain, Size: 524 bytes --]

On Mon, Jun 12, 2023 at 10:59:10PM +0300, Sergei Antonov wrote:

> memalign() returns a pointer which is to be freed by free(). To call
> unmap_sysmem() is incorrect, furthermore it was called in a wrong scope.
> 
> Also add a check for allocation error.
> 
> Fixes: d7af2baa49c6 ("crypto/fsl: Fix HW accelerated hash commands")
> Cc: Breno Lima <breno.lima@nxp.com>
> Signed-off-by: Sergei Antonov <saproj@gmail.com>
> Reviewed-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-07-15 15:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-12 19:59 [PATCH] hash: fix a memory leak Sergei Antonov
2023-06-12 21:17 ` Simon Glass
2023-07-15 15:03 ` Tom Rini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.