[PATCH] test: dm: hash: check digest size before memset

James Hilliard <[email protected]>
Newsgroups org.u-boot-project.lists.u-boot
Message-ID <[email protected]>
hash_algo_digest_size() returns -EINVAL for an invalid algorithm. The
test success provider passes that result directly to memset(), where it
is converted to a large size_t.

Return the error before touching the output buffer, and exercise the
invalid-algorithm path in the provider-selection test. This addresses
Coverity CIDs 652907 and 652908.

Fixes: 94b349bd902d ("crypto: hash: use DM providers from hash command")
Signed-off-by: James Hilliard <[email protected]>
---
 test/dm/hash.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/test/dm/hash.c b/test/dm/hash.c
index fe949e33de5..6adf916dc77 100644
--- a/test/dm/hash.c
+++ b/test/dm/hash.c
@@ -31,8 +31,14 @@ static int hash_test_success(struct udevice *dev, enum HASH_ALGO algo,
 			     const void *ibuf, const uint32_t ilen,
 			     void *obuf, uint32_t chunk_sz)
 {
+	ssize_t digest_size;
+
 	success_calls++;
-	memset(obuf, 0x5a, hash_algo_digest_size(algo));
+	digest_size = hash_algo_digest_size(algo);
+	if (digest_size < 0)
+		return digest_size;
+
+	memset(obuf, 0x5a, digest_size);
 
 	return 0;
 }
@@ -124,6 +130,14 @@ static int dm_test_hash_provider_selection(struct unit_test_state *uts)
 	for (int i = 0; i < sizeof(digest); i++)
 		ut_asserteq(0x5a, digest[i]);
 
+	memset(digest, 0, sizeof(digest));
+	ret = hash_digest_wd_lookup(HASH_ALGO_INVALID, "test", 4, digest, 4);
+	ut_asserteq(-EINVAL, ret);
+	ut_asserteq(2, unsupported_calls);
+	ut_asserteq(2, success_calls);
+	for (int i = 0; i < sizeof(digest); i++)
+		ut_asserteq(0, digest[i]);
+
 	ut_assertok(hash_test_unbind_all());
 	ut_assertok(hash_test_bind(DM_DRIVER_GET(hash_test_hard_error_drv),
 				   "hash-hard-error"));
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.