[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20180501234425.GB3791@eros>
Date: Wed, 2 May 2018 09:44:25 +1000
From: "Tobin C. Harding" <me@...in.cc>
To: linux-kernel@...r.kernel.org
Cc: Linus Torvalds <torvalds@...ux-foundation.org>,
Randy Dunlap <rdunlap@...radead.org>,
Steven Rostedt <rostedt@...dmis.org>,
Kees Cook <keescook@...omium.org>,
Anna-Maria Gleixner <anna-maria@...utronix.de>,
Andrew Morton <akpm@...ux-foundation.org>,
Theodore Ts'o <tytso@....edu>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Arnd Bergmann <arnd@...db.de>
Subject: Re: [PATCH 2/3] random: Return nbytes filled from hw RNG
On Wed, May 02, 2018 at 09:33:39AM +1000, Tobin C. Harding wrote:
> Currently the function get_random_bytes_arch() has return value 'void'.
> If the hw RNG fails we currently fall back to using get_random_bytes().
> This defeats the purpose of requesting random material from the hw RNG
> in the first place.
>
> There are currently no intree users of get_random_bytes_arch().
>
> Only get random bytes from the hw RNG, make function return the number
> of bytes retrieved from the hw RNG.
>
> Suggested-by: Linus Torvalds <torvalds@...ux-foundation.org>
> Signed-off-by: Tobin C. Harding <me@...in.cc>
> ---
> drivers/char/random.c | 16 +++++++++-------
> include/linux/random.h | 2 +-
> 2 files changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/char/random.c b/drivers/char/random.c
> index 031d18b31e0f..3a66507ea60b 100644
> --- a/drivers/char/random.c
> +++ b/drivers/char/random.c
> @@ -1725,26 +1725,28 @@ EXPORT_SYMBOL(del_random_ready_callback);
> * key known by the NSA). So it's useful if we need the speed, but
> * only if we're willing to trust the hardware manufacturer not to
> * have put in a back door.
> + *
> + * Return number of bytes filled in.
> */
> -void get_random_bytes_arch(void *buf, int nbytes)
> +int __must_check get_random_bytes_arch(void *buf, int nbytes)
> {
> char *p = buf;
> + int left = nbytes;
>
> - trace_get_random_bytes_arch(nbytes, _RET_IP_);
> - while (nbytes) {
> + trace_get_random_bytes_arch(left, _RET_IP_);
> + while (left) {
> unsigned long v;
> - int chunk = min(nbytes, (int)sizeof(unsigned long));
> + int chunk = min(left, (int)sizeof(unsigned long));
>
> if (!arch_get_random_long(&v))
> break;
>
> memcpy(p, &v, chunk);
> p += chunk;
> - nbytes -= chunk;
> + left -= chunk;
> }
>
> - if (nbytes)
> - get_random_bytes(p, nbytes);
> + return nbytes - left;
> }
> EXPORT_SYMBOL(get_random_bytes_arch);
>
> diff --git a/include/linux/random.h b/include/linux/random.h
> index 2ddf13b4281e..308168c0a637 100644
> --- a/include/linux/random.h
> +++ b/include/linux/random.h
> @@ -38,7 +38,7 @@ extern void get_random_bytes(void *buf, int nbytes);
> extern int wait_for_random_bytes(void);
> extern int add_random_ready_callback(struct random_ready_callback *rdy);
> extern void del_random_ready_callback(struct random_ready_callback *rdy);
> -extern void get_random_bytes_arch(void *buf, int nbytes);
> +extern int get_random_bytes_arch(void *buf, int nbytes);
>
> #ifndef MODULE
> extern const struct file_operations random_fops, urandom_fops;
> --
> 2.7.4
>
Tested by loading a module with this init funciton
static int __init test_init(void)
{
const int size = 256;
char buf[size];
int got;
pr_info("test: module inserted\n");
got = get_random_bytes_arch(buf, size);
if (got != size)
pr_warn("get_random_bytes_arch() failed, got %d bytes\n", got);
return 0;
}
module_init(test_init);
Fails on machine without RDRAND and succeeds on machine with RDRAND
thanks,
Tobin.
Powered by blists - more mailing lists