Skip to content

Commit b006c43

Browse files
Dominik Brodowskiherbertx
authored andcommitted
hwrng: core - start hwrng kthread also for untrusted sources
Start the hwrng kthread even if the hwrng source has a quality setting of zero. Then, every crng reseed interval, one batch of data from this zero-quality hwrng source will be mixed into the CRNG pool. This patch is based on the assumption that data from a hwrng source will not actively harm the CRNG state. Instead, many hwrng sources (such as TPM devices), even though they are assigend a quality level of zero, actually provide some entropy, which is good enough to mix into the CRNG pool every once in a while. Cc: Herbert Xu <[email protected]> Cc: Jason A. Donenfeld <[email protected]> Signed-off-by: Dominik Brodowski <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 4edff84 commit b006c43

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

drivers/char/hw_random/core.c

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ MODULE_PARM_DESC(default_quality,
5252

5353
static void drop_current_rng(void);
5454
static int hwrng_init(struct hwrng *rng);
55-
static void hwrng_manage_rngd(struct hwrng *rng);
55+
static int hwrng_fillfn(void *unused);
5656

5757
static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
5858
int wait);
@@ -96,6 +96,15 @@ static int set_current_rng(struct hwrng *rng)
9696
drop_current_rng();
9797
current_rng = rng;
9898

99+
/* if necessary, start hwrng thread */
100+
if (!hwrng_fill) {
101+
hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
102+
if (IS_ERR(hwrng_fill)) {
103+
pr_err("hwrng_fill thread creation failed\n");
104+
hwrng_fill = NULL;
105+
}
106+
}
107+
99108
return 0;
100109
}
101110

@@ -167,8 +176,6 @@ static int hwrng_init(struct hwrng *rng)
167176
rng->quality = 1024;
168177
current_quality = rng->quality; /* obsolete */
169178

170-
hwrng_manage_rngd(rng);
171-
172179
return 0;
173180
}
174181

@@ -454,10 +461,6 @@ static ssize_t rng_quality_store(struct device *dev,
454461
/* the best available RNG may have changed */
455462
ret = enable_best_rng();
456463

457-
/* start/stop rngd if necessary */
458-
if (current_rng)
459-
hwrng_manage_rngd(current_rng);
460-
461464
out:
462465
mutex_unlock(&rng_mutex);
463466
return ret ? ret : len;
@@ -513,9 +516,6 @@ static int hwrng_fillfn(void *unused)
513516

514517
put_rng(rng);
515518

516-
if (!quality)
517-
break;
518-
519519
if (rc <= 0)
520520
continue;
521521

@@ -534,22 +534,6 @@ static int hwrng_fillfn(void *unused)
534534
return 0;
535535
}
536536

537-
static void hwrng_manage_rngd(struct hwrng *rng)
538-
{
539-
if (WARN_ON(!mutex_is_locked(&rng_mutex)))
540-
return;
541-
542-
if (rng->quality == 0 && hwrng_fill)
543-
kthread_stop(hwrng_fill);
544-
if (rng->quality > 0 && !hwrng_fill) {
545-
hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
546-
if (IS_ERR(hwrng_fill)) {
547-
pr_err("hwrng_fill thread creation failed\n");
548-
hwrng_fill = NULL;
549-
}
550-
}
551-
}
552-
553537
int hwrng_register(struct hwrng *rng)
554538
{
555539
int err = -EINVAL;

0 commit comments

Comments
 (0)