Skip to contentSkip to conversation

crypto: drbg: check blocklen is non zero

PATCH In review [email protected] 3 participants 5 messages Aug 2, 2020 – Aug 20, 2020 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: drbg: check blocklen is non zero

From: Tom Rix <[email protected]>

Clang static analysis reports this error

crypto/drbg.c:441:40: warning: Division by zero

        padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

When drbg_bocklen fails it returns 0.

	if (drbg && drbg->core)
		return drbg->core->blocklen_bytes;
	return 0;

In many places in drbg_ctr_df drbg_bocklen is assumed to be non zero. So turn the assumption into a check.

Fixes 541af946fe13 ("crypto: drbg - SP800-90A Deterministic Random Bit Generator")
---
 crypto/drbg.c | 3 +++
 1 file changed, 3 insertions(+)
crypto/drbg.c +3 -0
index e99fe34cfa00..bd9a137e5473 100644
@@ -420,6 +420,9 @@ static int drbg_ctr_df(struct drbg_state *drbg,
 	size_t inputlen = 0;
 	struct drbg_string *seed = NULL;
 
+	if (!drbg_blocklen(drbg))
+		return -EINVAL;
+
 	memset(pad, 0, drbg_blocklen(drbg));
 	memset(iv, 0, drbg_blocklen(drbg));
 
Signature · 1 line
2.18.1
Reviewed-by on v1 1/1
#1 lore raw

Am Sonntag, 2. August 2020, 19:12:47 CEST schrieb [email protected]:

Hi Tom,

Quoted 14 lines

From: Tom Rix <[email protected]>

Clang static analysis reports this error

crypto/drbg.c:441:40: warning: Division by zero

        padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

When drbg_bocklen fails it returns 0.

	if (drbg && drbg->core)
		return drbg->core->blocklen_bytes;
	return 0;
Quoted 7 lines

In many places in drbg_ctr_df drbg_bocklen is assumed to be non zero. So turn the assumption into a check.

Fixes 541af946fe13 ("crypto: drbg - SP800-90A Deterministic Random Bit

Generator")

Thank you.

Ciao
Stephan

maintainer
#2 lore raw
In reply to [email protected]
Quoted 5 lines · On Sun, Aug 02, 2020 at 10:12:47AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this error

crypto/drbg.c:441:40: warning: Division by zero

Quoted 8 lines
        padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

When drbg_bocklen fails it returns 0.

	if (drbg && drbg->core)
		return drbg->core->blocklen_bytes;
	return 0;

Yes but it can only fail if the drbg is not instantiated. If you're hitting the generate path with an uninstantiated drbg you've got bigger problems than a divide by zero.

So how is this even possible?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author
#3 lore raw

There are many divide by 0 reports.  This one got attention because it is in crypto, where i believe problems, even false positives, should be fixed.

Tom

Quoted 21 lines · Herbert Xu
Quoted 5 lines · On Sun, Aug 02, 2020 at 10:12:47AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

Clang static analysis reports this error

crypto/drbg.c:441:40: warning: Division by zero

Quoted 8 lines
        padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

When drbg_bocklen fails it returns 0.

	if (drbg && drbg->core)
		return drbg->core->blocklen_bytes;
	return 0;

Yes but it can only fail if the drbg is not instantiated. If you're hitting the generate path with an uninstantiated drbg you've got bigger problems than a divide by zero.

So how is this even possible?

Cheers,

maintainer
#4 lore raw
Quoted 1 line · Tom Rix

There are many divide by 0 reports.  This one got attention because it is in crypto, where i believe problems, even false positives, should be fixed.

Please don't top post.

AS your bug report is a false positive I'm rejecting your patch.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt