On Mon, Jul 21, 2014 at 6:02 PM, Antti Kantee <[email protected]> wrote:
>> RDRAND support in NetBSD would help for amd64 for hardware that supports
>> it.
>
>
> What support does it need?  Isn't it just one instruction?  Yea, it might
> make things a bit better, although as I understand it, it's untrustworthy
> randomness.

Yes, its just one (non priv) instruction.

Well, if you have something to mix it with it helps. The main issue
was when freebsd fed it straight to /dev/random. No one is forcing its
use.

Attached is new version of patch.

I decided it made more sense to limit the amount of randomness
supplied anyway, its just a waste of CPU and involves more rekeying
even if it is pseudorandomness.
Binary files src/.git/index and rsrc/.git/index differ
diff -urN src/.git/logs/HEAD rsrc/.git/logs/HEAD
--- src/.git/logs/HEAD  2014-07-20 12:39:23.455438095 +0100
+++ rsrc/.git/logs/HEAD 2014-07-19 14:48:13.213397073 +0100
@@ -1,2 +1,2 @@
-0000000000000000000000000000000000000000 
0fc9af5c18d8e686e6b44a1c15bd2686c9a1926d Justin Cormack 
<[email protected]> 1405856360 +0100       clone: from 
https://github.com/rumpkernel/src-netbsd
-0fc9af5c18d8e686e6b44a1c15bd2686c9a1926d 
364a119a49af2478a40bca96cf45bbfc4b8e0979 Justin Cormack 
<[email protected]> 1405856363 +0100       checkout: moving from 
master to 364a119a49af2478a40bca96cf45bbfc4b8e0979
+0000000000000000000000000000000000000000 
0fc9af5c18d8e686e6b44a1c15bd2686c9a1926d Justin Cormack 
<[email protected]> 1405777690 +0100       clone: from 
https://github.com/rumpkernel/src-netbsd
+0fc9af5c18d8e686e6b44a1c15bd2686c9a1926d 
364a119a49af2478a40bca96cf45bbfc4b8e0979 Justin Cormack 
<[email protected]> 1405777693 +0100       checkout: moving from 
master to 364a119a49af2478a40bca96cf45bbfc4b8e0979
diff -urN src/.git/logs/refs/heads/master rsrc/.git/logs/refs/heads/master
--- src/.git/logs/refs/heads/master     2014-07-20 12:39:20.791438217 +0100
+++ rsrc/.git/logs/refs/heads/master    2014-07-19 14:48:10.071826953 +0100
@@ -1 +1 @@
-0000000000000000000000000000000000000000 
0fc9af5c18d8e686e6b44a1c15bd2686c9a1926d Justin Cormack 
<[email protected]> 1405856360 +0100       clone: from 
https://github.com/rumpkernel/src-netbsd
+0000000000000000000000000000000000000000 
0fc9af5c18d8e686e6b44a1c15bd2686c9a1926d Justin Cormack 
<[email protected]> 1405777690 +0100       clone: from 
https://github.com/rumpkernel/src-netbsd
diff -urN src/.git/logs/refs/remotes/origin/HEAD 
rsrc/.git/logs/refs/remotes/origin/HEAD
--- src/.git/logs/refs/remotes/origin/HEAD      2014-07-20 12:39:20.791438217 
+0100
+++ rsrc/.git/logs/refs/remotes/origin/HEAD     2014-07-19 14:48:10.071826953 
+0100
@@ -1 +1 @@
-0000000000000000000000000000000000000000 
0fc9af5c18d8e686e6b44a1c15bd2686c9a1926d Justin Cormack 
<[email protected]> 1405856360 +0100       clone: from 
https://github.com/rumpkernel/src-netbsd
+0000000000000000000000000000000000000000 
0fc9af5c18d8e686e6b44a1c15bd2686c9a1926d Justin Cormack 
<[email protected]> 1405777690 +0100       clone: from 
https://github.com/rumpkernel/src-netbsd
diff -urN src/lib/librumpuser/rumpfiber.c rsrc/lib/librumpuser/rumpfiber.c
--- src/lib/librumpuser/rumpfiber.c     2014-07-20 12:39:21.515438184 +0100
+++ rsrc/lib/librumpuser/rumpfiber.c    2014-07-22 16:39:09.618890878 +0100
@@ -89,6 +89,7 @@
 
 #include "rumpuser_int.h"
 #include "rumpfiber.h"
+#include "rumpuser_random.h"
 
 static void init_sched(void);
 static void join_thread(struct thread *);
@@ -416,20 +417,7 @@
                return 1;
        }
 
-#ifdef RUMPUSER_USE_DEVRANDOM
-       uint32_t rv;
-       int fd;
-
-       if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
-               srandom(time(NULL));
-       } else {
-               if (read(fd, &rv, sizeof(rv)) != sizeof(rv))
-                       srandom(time(NULL));
-               else
-                       srandom(rv);
-               close(fd);
-       }
-#endif
+       RUMPUSER_RANDOM_INIT;
 
         rumpuser__hyp = *hyp;
 
@@ -570,26 +558,6 @@
        return 0;
 }
 
-int
-rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
-{
-       size_t origlen = buflen;
-       uint32_t *p = buf;
-       uint32_t tmp;
-       int chunk;
-
-       do {
-               chunk = buflen < 4 ? buflen : 4; /* portable MIN ... */
-               tmp = RUMPUSER_RANDOM();
-               memcpy(p, &tmp, chunk);
-               p++;
-               buflen -= chunk;
-       } while (chunk);
-
-       *retp = origlen;
-       ET(0);
-}
-
 /* thread functions */
 
 TAILQ_HEAD(waithead, waiter);
diff -urN src/lib/librumpuser/rumpuser.c rsrc/lib/librumpuser/rumpuser.c
--- src/lib/librumpuser/rumpuser.c      2014-07-20 12:39:21.515438184 +0100
+++ rsrc/lib/librumpuser/rumpuser.c     2014-07-22 16:38:57.006891454 +0100
@@ -50,6 +50,7 @@
 #include <rump/rumpuser.h>
 
 #include "rumpuser_int.h"
+#include "rumpuser_random.h"
 
 struct rumpuser_hyperup rumpuser__hyp;
 
@@ -63,20 +64,7 @@
                return 1;
        }
 
-#ifdef RUMPUSER_USE_DEVRANDOM
-       uint32_t rv;
-       int fd;
-
-       if ((fd = open("/dev/urandom", O_RDONLY)) == -1) {
-               srandom(time(NULL));
-       } else {
-               if (read(fd, &rv, sizeof(rv)) != sizeof(rv))
-                       srandom(time(NULL));
-               else
-                       srandom(rv);
-               close(fd);
-       }
-#endif
+       RUMPUSER_RANDOM_INIT;
 
        rumpuser__thrinit();
        rumpuser__hyp = *hyp;
@@ -275,23 +263,3 @@
                raise(sig);
        return 0;
 }
-
-int
-rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
-{
-       size_t origlen = buflen;
-       uint32_t *p = buf;
-       uint32_t tmp;
-       int chunk;
-
-       do {
-               chunk = buflen < 4 ? buflen : 4; /* portable MIN ... */
-               tmp = RUMPUSER_RANDOM();
-               memcpy(p, &tmp, chunk);
-               p++;
-               buflen -= chunk;
-       } while (chunk);
-
-       *retp = origlen;
-       ET(0);
-}
diff -urN src/lib/librumpuser/rumpuser_port.h 
rsrc/lib/librumpuser/rumpuser_port.h
--- src/lib/librumpuser/rumpuser_port.h 2014-07-20 12:39:21.515438184 +0100
+++ rsrc/lib/librumpuser/rumpuser_port.h        2014-07-20 14:50:55.955077711 
+0100
@@ -218,11 +218,8 @@
 #define __STRING(x)    #x
 #endif
 
-#if defined(__linux__) || defined(__sun__) || defined (__CYGWIN__)
-#define RUMPUSER_RANDOM() random()
-#define RUMPUSER_USE_DEVRANDOM
-#else
-#define RUMPUSER_RANDOM() arc4random()
+#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || 
defined(__DragonFly__)
+#define PLATFORM_HAS_ARC4RANDOM
 #endif
 
 #ifndef __NetBSD_Prereq__
diff -urN src/lib/librumpuser/rumpuser_random.h 
rsrc/lib/librumpuser/rumpuser_random.h
--- src/lib/librumpuser/rumpuser_random.h       1970-01-01 01:00:00.000000000 
+0100
+++ rsrc/lib/librumpuser/rumpuser_random.h      2014-07-22 16:53:33.558851429 
+0100
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2014 Justin Cormack.  All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+static const size_t random_maxread = 32;
+
+#ifdef PLATFORM_HAS_ARC4RANDOM
+#define RUMPUSER_RANDOM_INIT
+#else
+static const char *random_device = "/dev/urandom";
+static int random_fd = -1;
+#define RUMPUSER_RANDOM_INIT \
+do { \
+       random_fd = open(random_device, O_RDONLY); \
+       if (random_fd < 0) { \
+               fprintf(stderr, "random init failed\n"); \
+       } \
+} while (/*CONSTCOND*/0)
+#endif
+
+int
+rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
+{
+#ifndef PLATFORM_HAS_ARC4RANDOM
+       ssize_t rv;
+
+       rv = read(random_fd, buf, buflen > random_maxread ? random_maxread : 
buflen);
+       if (rv < 0) {
+               ET(errno);
+       }
+       *retp = rv;
+#else
+       buflen = buflen > random_maxread ? random_maxread : buflen
+       arc4random_buf(buf, buflen);
+       *retp = buflen;
+#endif
+
+       ET(0);
+}
------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
rumpkernel-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rumpkernel-users

Reply via email to