* [PATCH v3 1/2] hwrng: fetch randomness only after device init
2014-07-10 10:12 [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes Amit Shah
@ 2014-07-10 10:12 ` Amit Shah
2014-07-10 11:13 ` Jason Cooper
2014-07-10 10:12 ` [PATCH v3 2/2] virtio: rng: ensure reads happen after successful probe Amit Shah
2014-07-14 12:50 ` [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes Herbert Xu
2 siblings, 1 reply; 8+ messages in thread
From: Amit Shah @ 2014-07-10 10:12 UTC (permalink / raw)
To: linux-kernel
Cc: Virtualization List, Rusty Russell, herbert, keescook, jason,
Amit Shah, stable
Commit d9e7972619334 "hwrng: add randomness to system from rng sources"
added a call to rng_get_data() from the hwrng_register() function.
However, some rng devices need initialization before data can be read
from them.
This commit makes the call to rng_get_data() depend on no init fn
pointer being registered by the device. If an init function is
registered, this call is made after device init.
CC: Kees Cook <keescook@chromium.org>
CC: Jason Cooper <jason@lakedaemon.net>
CC: Herbert Xu <herbert@gondor.apana.org.au>
CC: <stable@vger.kernel.org> # For v3.15+
Signed-off-by: Amit Shah <amit.shah@redhat.com>
---
drivers/char/hw_random/core.c | 41 +++++++++++++++++++++++++++++++++--------
1 file changed, 33 insertions(+), 8 deletions(-)
diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
index 334601c..2a451b1 100644
--- a/drivers/char/hw_random/core.c
+++ b/drivers/char/hw_random/core.c
@@ -55,16 +55,35 @@ static DEFINE_MUTEX(rng_mutex);
static int data_avail;
static u8 *rng_buffer;
+static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
+ int wait);
+
static size_t rng_buffer_size(void)
{
return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
}
+static void add_early_randomness(struct hwrng *rng)
+{
+ unsigned char bytes[16];
+ int bytes_read;
+
+ bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
+ if (bytes_read > 0)
+ add_device_randomness(bytes, bytes_read);
+}
+
static inline int hwrng_init(struct hwrng *rng)
{
- if (!rng->init)
- return 0;
- return rng->init(rng);
+ if (rng->init) {
+ int ret;
+
+ ret = rng->init(rng);
+ if (ret)
+ return ret;
+ }
+ add_early_randomness(rng);
+ return 0;
}
static inline void hwrng_cleanup(struct hwrng *rng)
@@ -304,8 +323,6 @@ int hwrng_register(struct hwrng *rng)
{
int err = -EINVAL;
struct hwrng *old_rng, *tmp;
- unsigned char bytes[16];
- int bytes_read;
if (rng->name == NULL ||
(rng->data_read == NULL && rng->read == NULL))
@@ -347,9 +364,17 @@ int hwrng_register(struct hwrng *rng)
INIT_LIST_HEAD(&rng->list);
list_add_tail(&rng->list, &rng_list);
- bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
- if (bytes_read > 0)
- add_device_randomness(bytes, bytes_read);
+ if (old_rng && !rng->init) {
+ /*
+ * Use a new device's input to add some randomness to
+ * the system. If this rng device isn't going to be
+ * used right away, its init function hasn't been
+ * called yet; so only use the randomness from devices
+ * that don't need an init callback.
+ */
+ add_early_randomness(rng);
+ }
+
out_unlock:
mutex_unlock(&rng_mutex);
out:
--
1.9.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH v3 1/2] hwrng: fetch randomness only after device init
2014-07-10 10:12 ` [PATCH v3 1/2] hwrng: fetch randomness only after device init Amit Shah
@ 2014-07-10 11:13 ` Jason Cooper
0 siblings, 0 replies; 8+ messages in thread
From: Jason Cooper @ 2014-07-10 11:13 UTC (permalink / raw)
To: Amit Shah
Cc: linux-kernel, Virtualization List, Rusty Russell, herbert,
keescook, stable
On Thu, Jul 10, 2014 at 03:42:34PM +0530, Amit Shah wrote:
> Commit d9e7972619334 "hwrng: add randomness to system from rng sources"
> added a call to rng_get_data() from the hwrng_register() function.
> However, some rng devices need initialization before data can be read
> from them.
>
> This commit makes the call to rng_get_data() depend on no init fn
> pointer being registered by the device. If an init function is
> registered, this call is made after device init.
>
> CC: Kees Cook <keescook@chromium.org>
> CC: Jason Cooper <jason@lakedaemon.net>
> CC: Herbert Xu <herbert@gondor.apana.org.au>
> CC: <stable@vger.kernel.org> # For v3.15+
> Signed-off-by: Amit Shah <amit.shah@redhat.com>
> ---
> drivers/char/hw_random/core.c | 41 +++++++++++++++++++++++++++++++++--------
> 1 file changed, 33 insertions(+), 8 deletions(-)
Reviewed-by: Jason Cooper <jason@lakedaemon.net>
thx,
Jason.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 2/2] virtio: rng: ensure reads happen after successful probe
2014-07-10 10:12 [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes Amit Shah
2014-07-10 10:12 ` [PATCH v3 1/2] hwrng: fetch randomness only after device init Amit Shah
@ 2014-07-10 10:12 ` Amit Shah
2014-07-14 12:50 ` [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes Herbert Xu
2 siblings, 0 replies; 8+ messages in thread
From: Amit Shah @ 2014-07-10 10:12 UTC (permalink / raw)
To: linux-kernel
Cc: Virtualization List, Rusty Russell, herbert, keescook, jason,
Amit Shah, stable
The hwrng core asks for random data in the hwrng_register() call itself
from commit d9e7972619. This doesn't play well with virtio -- the
DRIVER_OK bit is only set by virtio core on a successful probe, and
we're not yet out of our probe routine when this call is made. This
causes the host to not acknowledge any requests we put in the virtqueue,
and the insmod or kernel boot process just waits for data to arrive from
the host, which never happens.
CC: Kees Cook <keescook@chromium.org>
CC: Jason Cooper <jason@lakedaemon.net>
CC: Herbert Xu <herbert@gondor.apana.org.au>
CC: <stable@vger.kernel.org> # For v3.15+
Reviewed-by: Jason Cooper <jason@lakedaemon.net>
Signed-off-by: Amit Shah <amit.shah@redhat.com>
---
drivers/char/hw_random/core.c | 6 ++++++
drivers/char/hw_random/virtio-rng.c | 10 ++++++++++
2 files changed, 16 insertions(+)
diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
index 2a451b1..c4419ea 100644
--- a/drivers/char/hw_random/core.c
+++ b/drivers/char/hw_random/core.c
@@ -68,6 +68,12 @@ static void add_early_randomness(struct hwrng *rng)
unsigned char bytes[16];
int bytes_read;
+ /*
+ * Currently only virtio-rng cannot return data during device
+ * probe, and that's handled in virtio-rng.c itself. If there
+ * are more such devices, this call to rng_get_data can be
+ * made conditional here instead of doing it per-device.
+ */
bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
if (bytes_read > 0)
add_device_randomness(bytes, bytes_read);
diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
index f3e7150..e9b15bc 100644
--- a/drivers/char/hw_random/virtio-rng.c
+++ b/drivers/char/hw_random/virtio-rng.c
@@ -38,6 +38,8 @@ struct virtrng_info {
int index;
};
+static bool probe_done;
+
static void random_recv_done(struct virtqueue *vq)
{
struct virtrng_info *vi = vq->vdev->priv;
@@ -67,6 +69,13 @@ static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
int ret;
struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
+ /*
+ * Don't ask host for data till we're setup. This call can
+ * happen during hwrng_register(), after commit d9e7972619.
+ */
+ if (unlikely(!probe_done))
+ return 0;
+
if (!vi->busy) {
vi->busy = true;
init_completion(&vi->have_data);
@@ -137,6 +146,7 @@ static int probe_common(struct virtio_device *vdev)
return err;
}
+ probe_done = true;
return 0;
}
--
1.9.3
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes
2014-07-10 10:12 [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes Amit Shah
2014-07-10 10:12 ` [PATCH v3 1/2] hwrng: fetch randomness only after device init Amit Shah
2014-07-10 10:12 ` [PATCH v3 2/2] virtio: rng: ensure reads happen after successful probe Amit Shah
@ 2014-07-14 12:50 ` Herbert Xu
2014-07-15 4:40 ` Amit Shah
2 siblings, 1 reply; 8+ messages in thread
From: Herbert Xu @ 2014-07-14 12:50 UTC (permalink / raw)
To: Amit Shah
Cc: linux-kernel, Virtualization List, Rusty Russell, keescook, jason
On Thu, Jul 10, 2014 at 03:42:33PM +0530, Amit Shah wrote:
> v3:
> - Kees Cook pointed out a weird side-effect: devices which have
> ->init() registered get their randomness added to the system each
> time they're switched in, but devices that don't have the init
> callback don't contribute to system randomness more than once. The
> weirdness is resolved here by using the randomness each time
> hwrng_init() is attempted, irrespective of the existence of the
> device's ->init() callback.
All applied to crypto. Thanks!
--
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes
2014-07-14 12:50 ` [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes Herbert Xu
@ 2014-07-15 4:40 ` Amit Shah
2014-07-15 4:45 ` Herbert Xu
0 siblings, 1 reply; 8+ messages in thread
From: Amit Shah @ 2014-07-15 4:40 UTC (permalink / raw)
To: Herbert Xu
Cc: linux-kernel, Virtualization List, Rusty Russell, keescook, jason
On (Mon) 14 Jul 2014 [20:50:06], Herbert Xu wrote:
> On Thu, Jul 10, 2014 at 03:42:33PM +0530, Amit Shah wrote:
> > v3:
> > - Kees Cook pointed out a weird side-effect: devices which have
> > ->init() registered get their randomness added to the system each
> > time they're switched in, but devices that don't have the init
> > callback don't contribute to system randomness more than once. The
> > weirdness is resolved here by using the randomness each time
> > hwrng_init() is attempted, irrespective of the existence of the
> > device's ->init() callback.
>
> All applied to crypto. Thanks!
Thanks, Herbert. I didn't mention it, but pls queue it up for 3.16.
Amit
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes
2014-07-15 4:40 ` Amit Shah
@ 2014-07-15 4:45 ` Herbert Xu
2014-07-15 4:49 ` Amit Shah
0 siblings, 1 reply; 8+ messages in thread
From: Herbert Xu @ 2014-07-15 4:45 UTC (permalink / raw)
To: Amit Shah
Cc: linux-kernel, Virtualization List, Rusty Russell, keescook, jason
On Tue, Jul 15, 2014 at 10:10:28AM +0530, Amit Shah wrote:
> On (Mon) 14 Jul 2014 [20:50:06], Herbert Xu wrote:
> > On Thu, Jul 10, 2014 at 03:42:33PM +0530, Amit Shah wrote:
> > > v3:
> > > - Kees Cook pointed out a weird side-effect: devices which have
> > > ->init() registered get their randomness added to the system each
> > > time they're switched in, but devices that don't have the init
> > > callback don't contribute to system randomness more than once. The
> > > weirdness is resolved here by using the randomness each time
> > > hwrng_init() is attempted, irrespective of the existence of the
> > > device's ->init() callback.
> >
> > All applied to crypto. Thanks!
>
> Thanks, Herbert. I didn't mention it, but pls queue it up for 3.16.
Yes that's why it's in crypto as opposed to cryptodev.
Cheers,
--
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 0/2] hwrng, virtio-rng: init-time fixes
2014-07-15 4:45 ` Herbert Xu
@ 2014-07-15 4:49 ` Amit Shah
0 siblings, 0 replies; 8+ messages in thread
From: Amit Shah @ 2014-07-15 4:49 UTC (permalink / raw)
To: Herbert Xu
Cc: linux-kernel, Virtualization List, Rusty Russell, keescook, jason
On (Tue) 15 Jul 2014 [12:45:56], Herbert Xu wrote:
> On Tue, Jul 15, 2014 at 10:10:28AM +0530, Amit Shah wrote:
> > On (Mon) 14 Jul 2014 [20:50:06], Herbert Xu wrote:
> > > On Thu, Jul 10, 2014 at 03:42:33PM +0530, Amit Shah wrote:
> > > > v3:
> > > > - Kees Cook pointed out a weird side-effect: devices which have
> > > > ->init() registered get their randomness added to the system each
> > > > time they're switched in, but devices that don't have the init
> > > > callback don't contribute to system randomness more than once. The
> > > > weirdness is resolved here by using the randomness each time
> > > > hwrng_init() is attempted, irrespective of the existence of the
> > > > device's ->init() callback.
> > >
> > > All applied to crypto. Thanks!
> >
> > Thanks, Herbert. I didn't mention it, but pls queue it up for 3.16.
>
> Yes that's why it's in crypto as opposed to cryptodev.
Ah; thanks!
Amit
^ permalink raw reply [flat|nested] 8+ messages in thread