]> git.itanic.dy.fi Git - linux-stable/commitdiff
hwrng: core - treat default_quality as a maximum and default to 1024
authorJason A. Donenfeld <Jason@zx2c4.com>
Mon, 7 Nov 2022 12:24:55 +0000 (13:24 +0100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 18 Nov 2022 08:59:34 +0000 (16:59 +0800)
Most hw_random devices return entropy which is assumed to be of full
quality, but driver authors don't bother setting the quality knob. Some
hw_random devices return less than full quality entropy, and then driver
authors set the quality knob. Therefore, the entropy crediting should be
opt-out rather than opt-in per-driver, to reflect the actual reality on
the ground.

For example, the two Raspberry Pi RNG drivers produce full entropy
randomness, and both EDK2 and U-Boot's drivers for these treat them as
such. The result is that EFI then uses these numbers and passes the to
Linux, and Linux credits them as boot, thereby initializing the RNG.
Yet, in Linux, the quality knob was never set to anything, and so on the
chance that Linux is booted without EFI, nothing is ever credited.
That's annoying.

The same pattern appears to repeat itself throughout various drivers. In
fact, very very few drivers have bothered setting quality=1024.

Looking at the git history of existing drivers and corresponding mailing
list discussion, this conclusion tracks. There's been a decent amount of
discussion about drivers that set quality < 1024 -- somebody read and
interepreted a datasheet, or made some back of the envelope calculation
somehow. But there's been very little, if any, discussion about most
drivers where the quality is just set to 1024 or unset (or set to 1000
when the authors misunderstood the API and assumed it was base-10 rather
than base-2); in both cases the intent was fairly clear of, "this is a
hardware random device; it's fine."

So let's invert this logic. A hw_random struct's quality knob now
controls the maximum quality a driver can produce, or 0 to specify 1024.
Then, the module-wide switch called "default_quality" is changed to
represent the maximum quality of any driver. By default it's 1024, and
the quality of any particular driver is then given by:

    min(default_quality, rng->quality ?: 1024);

This way, the user can still turn this off for weird reasons (and we can
replace whatever driver-specific disabling hacks existed in the past),
yet we get proper crediting for relevant RNGs.

Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
16 files changed:
arch/um/drivers/random.c
drivers/char/hw_random/cavium-rng-vf.c
drivers/char/hw_random/cn10k-rng.c
drivers/char/hw_random/core.c
drivers/char/hw_random/mpfs-rng.c
drivers/char/hw_random/npcm-rng.c
drivers/char/hw_random/s390-trng.c
drivers/char/hw_random/timeriomem-rng.c
drivers/char/hw_random/virtio-rng.c
drivers/crypto/allwinner/sun8i-ce/sun8i-ce-trng.c
drivers/crypto/atmel-sha204a.c
drivers/crypto/caam/caamrng.c
drivers/firmware/turris-mox-rwtm.c
drivers/s390/crypto/zcrypt_api.c
drivers/usb/misc/chaoskey.c
include/linux/hw_random.h

index 32b3341fe9707ada1069ff0962a594b76c0e11c3..da985e0dc69a5c3722180f9106c69f2696f6f5e6 100644 (file)
@@ -82,7 +82,6 @@ static int __init rng_init (void)
        sigio_broken(random_fd);
        hwrng.name = RNG_MODULE_NAME;
        hwrng.read = rng_dev_read;
-       hwrng.quality = 1024;
 
        err = hwrng_register(&hwrng);
        if (err) {
index 7c55f4cf4a8ba69b27185fc27432c363e6d8acc9..c99c54cd99c67675bd6b679dbd57986feaeb40a7 100644 (file)
@@ -225,7 +225,6 @@ static int cavium_rng_probe_vf(struct       pci_dev         *pdev,
                return -ENOMEM;
 
        rng->ops.read    = cavium_rng_read;
-       rng->ops.quality = 1000;
 
        pci_set_drvdata(pdev, rng);
 
index a01e9307737c5e11eea9ccd9dd5c10fe724ac04b..c1193f85982c36cae1cd1bf57db09f9ceb99b9a6 100644 (file)
@@ -145,7 +145,6 @@ static int cn10k_rng_probe(struct pci_dev *pdev, const struct pci_device_id *id)
                return -ENOMEM;
 
        rng->ops.read    = cn10k_rng_read;
-       rng->ops.quality = 1000;
        rng->ops.priv = (unsigned long)rng;
 
        reset_rng_health_state(rng);
index cc002b0c2f0c3817e71e3a9e04c89015ff447994..afde685f5e0a4f4057fdcbe230e38bfc60e139bb 100644 (file)
@@ -41,14 +41,14 @@ static DEFINE_MUTEX(reading_mutex);
 static int data_avail;
 static u8 *rng_buffer, *rng_fillbuf;
 static unsigned short current_quality;
-static unsigned short default_quality; /* = 0; default to "off" */
+static unsigned short default_quality = 1024; /* default to maximum */
 
 module_param(current_quality, ushort, 0644);
 MODULE_PARM_DESC(current_quality,
                 "current hwrng entropy estimation per 1024 bits of input -- obsolete, use rng_quality instead");
 module_param(default_quality, ushort, 0644);
 MODULE_PARM_DESC(default_quality,
-                "default entropy content of hwrng per 1024 bits of input");
+                "default maximum entropy content of hwrng per 1024 bits of input");
 
 static void drop_current_rng(void);
 static int hwrng_init(struct hwrng *rng);
@@ -170,10 +170,7 @@ static int hwrng_init(struct hwrng *rng)
        reinit_completion(&rng->cleanup_done);
 
 skip_init:
-       if (!rng->quality)
-               rng->quality = default_quality;
-       if (rng->quality > 1024)
-               rng->quality = 1024;
+       rng->quality = min_t(u16, min_t(u16, default_quality, 1024), rng->quality ?: 1024);
        current_quality = rng->quality; /* obsolete */
 
        return 0;
index 5813da617a485e9afb5b4840a691b34e23b4bd16..c6972734ae62e8e31b734b10beaa6ceed13af326 100644 (file)
@@ -78,7 +78,6 @@ static int mpfs_rng_probe(struct platform_device *pdev)
 
        rng_priv->rng.read = mpfs_rng_read;
        rng_priv->rng.name = pdev->name;
-       rng_priv->rng.quality = 1024;
 
        platform_set_drvdata(pdev, rng_priv);
 
index 5bf7f370f9859655881fec9c092c59ae709ccc45..9903d0357e06ea425e915b0b66a7e2820dd3a1f2 100644 (file)
@@ -111,7 +111,6 @@ static int npcm_rng_probe(struct platform_device *pdev)
        priv->rng.name = pdev->name;
        priv->rng.read = npcm_rng_read;
        priv->rng.priv = (unsigned long)&pdev->dev;
-       priv->rng.quality = 1000;
        priv->clkp = (u32)(uintptr_t)of_device_get_match_data(&pdev->dev);
 
        writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
index 795853dfc46b70a0f38b4a803780971b8900fb6a..cffa326ddc8d3b1738621c0bd8a24d08f1458016 100644 (file)
@@ -191,7 +191,6 @@ static struct hwrng trng_hwrng_dev = {
        .name           = "s390-trng",
        .data_read      = trng_hwrng_data_read,
        .read           = trng_hwrng_read,
-       .quality        = 1024,
 };
 
 
index 8ea1fc831eb7bac773848fc39ea9f63742217ba3..26f322d19a883f9672473c2c86aa733432ca8113 100644 (file)
@@ -145,8 +145,6 @@ static int timeriomem_rng_probe(struct platform_device *pdev)
                if (!of_property_read_u32(pdev->dev.of_node,
                                                "quality", &i))
                        priv->rng_ops.quality = i;
-               else
-                       priv->rng_ops.quality = 0;
        } else {
                period = pdata->period;
                priv->rng_ops.quality = pdata->quality;
index a6f3a8a2aca6d1c9091a21da56d3efea38ba74e9..f7690e0f92ede2f1ed29afc7de5943443c4b0988 100644 (file)
@@ -148,7 +148,6 @@ static int probe_common(struct virtio_device *vdev)
                .cleanup = virtio_cleanup,
                .priv = (unsigned long)vi,
                .name = vi->name,
-               .quality = 1000,
        };
        vdev->priv = vi;
 
index c4b0a8b588429b7f62fdfb37d2710c75ace7d9e6..e2b9b9104694172fa264e275eae21de8b69b0ee3 100644 (file)
@@ -108,7 +108,6 @@ int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce)
        }
        ce->trng.name = "sun8i Crypto Engine TRNG";
        ce->trng.read = sun8i_ce_trng_read;
-       ce->trng.quality = 1000;
 
        ret = hwrng_register(&ce->trng);
        if (ret)
index a84b657598c6e854053c83141634484af0e1220a..c0103e7fc2e753edd57329a0b2394ee5de864ef7 100644 (file)
@@ -107,7 +107,6 @@ static int atmel_sha204a_probe(struct i2c_client *client,
 
        i2c_priv->hwrng.name = dev_name(&client->dev);
        i2c_priv->hwrng.read = atmel_sha204a_rng_read;
-       i2c_priv->hwrng.quality = 1024;
 
        ret = devm_hwrng_register(&client->dev, &i2c_priv->hwrng);
        if (ret)
index 77d048dfe5d061923a1e1422c00a525aa10c5908..1f0e820509767358b4bda23ff3f6647848d8865c 100644 (file)
@@ -246,7 +246,6 @@ int caam_rng_init(struct device *ctrldev)
        ctx->rng.cleanup = caam_cleanup;
        ctx->rng.read    = caam_read;
        ctx->rng.priv    = (unsigned long)ctx;
-       ctx->rng.quality = 1024;
 
        dev_info(ctrldev, "registering rng-caam\n");
 
index c2d34dc8ba462828a428ee26899193c40b8c8889..6ea5789a89e2be9626928847456a3bc3b7cae47c 100644 (file)
@@ -528,7 +528,6 @@ static int turris_mox_rwtm_probe(struct platform_device *pdev)
        rwtm->hwrng.name = DRIVER_NAME "_hwrng";
        rwtm->hwrng.read = mox_hwrng_read;
        rwtm->hwrng.priv = (unsigned long) rwtm;
-       rwtm->hwrng.quality = 1024;
 
        ret = devm_hwrng_register(dev, &rwtm->hwrng);
        if (ret < 0) {
index f94b43ce9a65836690ffe3a57688e5d340d63a58..4bf36e53fe3e4af6cd41e690dd77dcf5613173ee 100644 (file)
@@ -53,10 +53,6 @@ MODULE_LICENSE("GPL");
 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
 
-static int zcrypt_hwrng_seed = 1;
-module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
-MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
-
 DEFINE_SPINLOCK(zcrypt_list_lock);
 LIST_HEAD(zcrypt_card_list);
 
@@ -2063,8 +2059,6 @@ int zcrypt_rng_device_add(void)
                        goto out;
                }
                zcrypt_rng_buffer_index = 0;
-               if (!zcrypt_hwrng_seed)
-                       zcrypt_rng_dev.quality = 0;
                rc = hwrng_register(&zcrypt_rng_dev);
                if (rc)
                        goto out_free;
index 87067c3d6109b966fb7a35be4e0ed6da9f15c9af..6fb5140e29b9dd641be37ddd42034e4f6466a12f 100644 (file)
@@ -200,7 +200,6 @@ static int chaoskey_probe(struct usb_interface *interface,
 
        dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
        dev->hwrng.read = chaoskey_rng_read;
-       dev->hwrng.quality = 1024;
 
        dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
        if (!dev->hwrng_registered)
index 77c2885c4c13027f5125ec5f2e3644b28a5adb8c..8a3115516a1ba9a6462f73f40c0109c998097904 100644 (file)
@@ -34,7 +34,7 @@
  * @priv:              Private data, for use by the RNG driver.
  * @quality:           Estimation of true entropy in RNG's bitstream
  *                     (in bits of entropy per 1024 bits of input;
- *                     valid values: 1 to 1024, or 0 for unknown).
+ *                     valid values: 1 to 1024, or 0 for maximum).
  */
 struct hwrng {
        const char *name;