Re: [PATCH] lib/randutils: Do not block on getrandom()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Mar 19, 2018 at 10:31:07AM +0000, Carlo Caione wrote:
> We fix this passing GRND_NONBLOCK to getrandom(). 'sfdisk' will use the
> best entropy it has available and fallback only as necessary.
> 
> Signed-off-by: Carlo Caione <carlo@xxxxxxxxxxxx>
> ---
>  lib/randutils.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/randutils.c b/lib/randutils.c
> index e1c4059e1..02c3d9eb0 100644
> --- a/lib/randutils.c
> +++ b/lib/randutils.c
> @@ -36,6 +36,8 @@
>  
>  #if !defined(HAVE_GETRANDOM) && defined(SYS_getrandom)
>  /* libc without function, but we have syscal */
> +#define GRND_NONBLOCK 0x01
> +#define GRND_RANDOM 0x02
>  static int getrandom(void *buf, size_t buflen, unsigned int flags)
>  {
>  	return (syscall(SYS_getrandom, buf, buflen, flags));
> @@ -104,13 +106,15 @@ void random_get_bytes(void *buf, size_t nbytes)
>  		int x;
>  
>  		errno = 0;
> -		x = getrandom(cp, n, 0);
> +		x = getrandom(cp, n, GRND_NONBLOCK);
>  		if (x > 0) {			/* success */
>  		       n -= x;
>  		       cp += x;
>  		       lose_counter = 0;
>  		} else if (errno == ENOSYS)	/* kernel without getrandom() */
>  			break;
> +		else if (errno == EAGAIN)
> +			break;

Applied, but not sure if I like the if-EAGAIN-break there. It will
break in all situations and completely bypass the lose_counter idea.
Maybe it would be better to accept that we will call getrandom() 16x
on systems with uninitialized entropy. (And maybe add xusleep() to the
loop...)

    Karel

>  		else if (lose_counter++ > 16)	/* entropy problem? */
>  			break;
>  	}




> -- 
> 2.14.1
> 
> --
> To unsubscribe from this list: send the line "unsubscribe util-linux" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

-- 
 Karel Zak  <kzak@xxxxxxxxxx>
 http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux