Source-Changes-HG archive

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

[src/trunk]: src/lib/librumpuser Clean up random implementation for librumpuser



details:   https://anonhg.NetBSD.org/src/rev/7d915310f665
branches:  trunk
changeset: 330819:7d915310f665
user:      justin <justin%NetBSD.org@localhost>
date:      Tue Jul 22 22:41:58 2014 +0000

description:
Clean up random implementation for librumpuser

Use /dev/urandom for platforms without arc4random, not srandom(),
deduplicate code, do not read excessive random bytes

Reviewed by pooka@

diffstat:

 lib/librumpuser/Makefile          |   4 +-
 lib/librumpuser/rumpfiber.c       |  37 +--------------
 lib/librumpuser/rumpuser.c        |  39 +---------------
 lib/librumpuser/rumpuser_int.h    |   4 +-
 lib/librumpuser/rumpuser_port.h   |  10 +--
 lib/librumpuser/rumpuser_random.c |  91 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 107 insertions(+), 78 deletions(-)

diffs (279 lines):

diff -r dd1726a80d51 -r 7d915310f665 lib/librumpuser/Makefile
--- a/lib/librumpuser/Makefile  Tue Jul 22 22:35:03 2014 +0000
+++ b/lib/librumpuser/Makefile  Tue Jul 22 22:41:58 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.20 2014/07/11 20:26:31 justin Exp $
+#      $NetBSD: Makefile,v 1.21 2014/07/22 22:41:58 justin Exp $
 #
 
 .include <bsd.own.mk>
@@ -41,7 +41,7 @@
 .error Unsupported rumpuser threading type: ${RUMPUSER_THREADS}
 .endif
 
-SRCS+=         rumpuser_component.c
+SRCS+=         rumpuser_component.c rumpuser_random.c
 SRCS+=         rumpuser_file.c rumpuser_mem.c
 
 SRCS+=         rumpuser_errtrans.c rumpuser_sigtrans.c
diff -r dd1726a80d51 -r 7d915310f665 lib/librumpuser/rumpfiber.c
--- a/lib/librumpuser/rumpfiber.c       Tue Jul 22 22:35:03 2014 +0000
+++ b/lib/librumpuser/rumpfiber.c       Tue Jul 22 22:41:58 2014 +0000
@@ -66,7 +66,7 @@
 #include "rumpuser_port.h"
 
 #if !defined(lint)
-__RCSID("$NetBSD: rumpfiber.c,v 1.1 2014/07/11 20:26:31 justin Exp $");
+__RCSID("$NetBSD: rumpfiber.c,v 1.2 2014/07/22 22:41:58 justin Exp $");
 #endif /* !lint */
 
 #include <sys/ioctl.h>
@@ -416,20 +416,9 @@
                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);
+       if (rumpuser__random_init() != 0) {
+               return 1;
        }
-#endif
 
         rumpuser__hyp = *hyp;
 
@@ -570,26 +559,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 -r dd1726a80d51 -r 7d915310f665 lib/librumpuser/rumpuser.c
--- a/lib/librumpuser/rumpuser.c        Tue Jul 22 22:35:03 2014 +0000
+++ b/lib/librumpuser/rumpuser.c        Tue Jul 22 22:41:58 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rumpuser.c,v 1.61 2014/07/10 08:17:43 justin Exp $     */
+/*     $NetBSD: rumpuser.c,v 1.62 2014/07/22 22:41:58 justin Exp $     */
 
 /*
  * Copyright (c) 2007-2010 Antti Kantee.  All Rights Reserved.
@@ -28,7 +28,7 @@
 #include "rumpuser_port.h"
 
 #if !defined(lint)
-__RCSID("$NetBSD: rumpuser.c,v 1.61 2014/07/10 08:17:43 justin Exp $");
+__RCSID("$NetBSD: rumpuser.c,v 1.62 2014/07/22 22:41:58 justin Exp $");
 #endif /* !lint */
 
 #include <sys/stat.h>
@@ -64,20 +64,9 @@
                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);
+       if (rumpuser__random_init() != 0) {
+               return 1;
        }
-#endif
 
        rumpuser__thrinit();
        rumpuser__hyp = *hyp;
@@ -276,23 +265,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 -r dd1726a80d51 -r 7d915310f665 lib/librumpuser/rumpuser_int.h
--- a/lib/librumpuser/rumpuser_int.h    Tue Jul 22 22:35:03 2014 +0000
+++ b/lib/librumpuser/rumpuser_int.h    Tue Jul 22 22:41:58 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rumpuser_int.h,v 1.9 2014/02/20 01:24:49 pooka Exp $   */
+/*     $NetBSD: rumpuser_int.h,v 1.10 2014/07/22 22:41:58 justin Exp $ */
 
 /*
  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
@@ -101,3 +101,5 @@
 #else
 #define ET(_v_) return (_v_) ? rumpuser__errtrans(_v_) : 0;
 #endif
+
+int rumpuser__random_init(void);
diff -r dd1726a80d51 -r 7d915310f665 lib/librumpuser/rumpuser_port.h
--- a/lib/librumpuser/rumpuser_port.h   Tue Jul 22 22:35:03 2014 +0000
+++ b/lib/librumpuser/rumpuser_port.h   Tue Jul 22 22:41:58 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rumpuser_port.h,v 1.33 2014/06/17 09:53:59 justin Exp $        */
+/*     $NetBSD: rumpuser_port.h,v 1.34 2014/07/22 22:41:58 justin Exp $        */
 
 /*
  * Portability header for non-NetBSD platforms.
@@ -217,11 +217,9 @@
 #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__) && __NetBSD_Version__ > 600000000) || \
+  defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+#define PLATFORM_HAS_ARC4RANDOM_BUF
 #endif
 
 #ifndef __NetBSD_Prereq__
diff -r dd1726a80d51 -r 7d915310f665 lib/librumpuser/rumpuser_random.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/librumpuser/rumpuser_random.c Tue Jul 22 22:41:58 2014 +0000
@@ -0,0 +1,91 @@
+/*
+ * 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.
+ */
+
+#include "rumpuser_port.h"
+
+#if !defined(lint)
+__RCSID("$NetBSD: rumpuser_random.c,v 1.1 2014/07/22 22:41:58 justin Exp $");
+#endif /* !lint */
+
+#include <sys/types.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <rump/rumpuser.h>
+
+#include "rumpuser_int.h"
+
+static const size_t random_maxread = 32;
+
+#ifdef PLATFORM_HAS_ARC4RANDOM_BUF
+int
+rumpuser__random_init(void)
+{
+
+       return 0;
+}
+#else
+static const char *random_device = "/dev/urandom";
+static int random_fd = -1;
+
+int
+rumpuser__random_init(void)
+{
+
+       random_fd = open(random_device, O_RDONLY);
+       if (random_fd < 0) {
+               fprintf(stderr, "random init open failed\n");
+               return 1;
+       }
+       return 0;
+}
+#endif
+
+int
+rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
+{
+#ifndef PLATFORM_HAS_ARC4RANDOM_BUF
+       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);
+}



Home | Main Index | Thread Index | Old Index