Skip to content

Commit

Permalink
hash: fix a memory leak
Browse files Browse the repository at this point in the history
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: d7af2ba ("crypto/fsl: Fix HW accelerated hash commands")
Cc: Breno Lima <[email protected]>
Signed-off-by: Sergei Antonov <[email protected]>
Reviewed-by: Simon Glass <[email protected]>
  • Loading branch information
saproj authored and trini committed Jul 14, 2023
1 parent 45a0052 commit 497e3a8
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions common/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 497e3a8

Please sign in to comment.