Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Harald Freudenberger <freude@linux.ibm.com>
To: linux-crypto@vger.kernel.org
Cc: herbert@gondor.apana.org.au, dengler@linux.ibm.com, Jason@zx2c4.com
Subject: [PATCH] hwrng: core - Fix wrong quality calculation at hw rng registration
Date: Fri, 21 Jun 2024 11:54:59 +0200	[thread overview]
Message-ID: <20240621095459.43622-1-freude@linux.ibm.com> (raw)

When there are rng sources registering at the hwrng core via
hwrng_register() a struct hwrng is delivered. There is a quality
field in there which is used to decide which of the registered
hw rng sources will be used by the hwrng core.

With commit 16bdbae39428 ("hwrng: core - treat default_quality as
a maximum and default to 1024") there came in a new default of
1024 in case this field is empty and all the known hw rng sources
at that time had been reworked to not fill this field and thus
use the default of 1024.

The code choosing the 'better' hw rng source during registration
of a new hw rng source has never been adapted to this and thus
used 0 if the hw rng implementation does not fill the quality field.
So when two rng sources register, one with 0 (meaning 1024) and
the other one with 999, the 999 hw rng will be chosen.

This patch simple takes into account that a quality field value
of 0 is to be treated as 1024 and then the decision about which
hw rng to use works as expected.

Tested on s390 with two hardware rng sources: crypto cards and
trng true random generator device driver.

Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>

Reported-by: Christian Rund <Christian.Rund@de.ibm.com>
Fixes: 16bdbae39428 ("hwrng: core - treat default_quality as a maximum and default to 1024")
---
 drivers/char/hw_random/core.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
index 4084df65c9fa..993b8a1f1d19 100644
--- a/drivers/char/hw_random/core.c
+++ b/drivers/char/hw_random/core.c
@@ -525,6 +525,7 @@ static int hwrng_fillfn(void *unused)
 
 int hwrng_register(struct hwrng *rng)
 {
+	unsigned short rng_quality, cur_quality;
 	int err = -EINVAL;
 	struct hwrng *tmp;
 
@@ -545,8 +546,14 @@ int hwrng_register(struct hwrng *rng)
 	complete(&rng->cleanup_done);
 	init_completion(&rng->dying);
 
+	/* Quality field not set in struct hwrng means 1024 */
+	rng_quality = rng->quality ? rng->quality : 1024;
+	cur_quality = current_rng ?
+		(current_rng->quality ? current_rng->quality : 1024) :
+		0;
+
 	if (!current_rng ||
-	    (!cur_rng_set_by_user && rng->quality > current_rng->quality)) {
+	    (!cur_rng_set_by_user && rng_quality > cur_quality)) {
 		/*
 		 * Set new rng as current as the new rng source
 		 * provides better entropy quality and was not
-- 
2.34.1


             reply	other threads:[~2024-06-21  9:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-21  9:54 Harald Freudenberger [this message]
2024-06-21 11:17 ` [PATCH] hwrng: core - Fix wrong quality calculation at hw rng registration Herbert Xu
2024-06-21 11:31 ` Holger Dengler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240621095459.43622-1-freude@linux.ibm.com \
    --to=freude@linux.ibm.com \
    --cc=Jason@zx2c4.com \
    --cc=dengler@linux.ibm.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox