Skip to contentSkip to conversation

test: dm: hash: check digest size before memset

PATCH In review James Hilliard 2 participants 2 messages Aug 11 – Aug 20

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test: dm: hash: check digest size before memset

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")
---
 test/dm/hash.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
test/dm/hash.c +15 -1
index fe949e33de5..6adf916dc77 100644
@@ -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"));
Signature · 1 line
2.53.0
#1 lore raw
Quoted 9 lines · James Hilliard

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.

[...]

Applied to u-boot/main, thanks!

[1/1] test: dm: hash: check digest size before memset

      commit: 6073c36b2c8d39afe3ecc789b281667a3ddebc70
Signature · 1 line
Tom