From owner-svn-src-all@freebsd.org Tue Jul 14 18:45:18 2015 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 603C59A1EA7; Tue, 14 Jul 2015 18:45:18 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 2D9683BB; Tue, 14 Jul 2015 18:45:18 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.70]) by repo.freebsd.org (8.14.9/8.14.9) with ESMTP id t6EIjHsR067547; Tue, 14 Jul 2015 18:45:17 GMT (envelope-from ed@FreeBSD.org) Received: (from ed@localhost) by repo.freebsd.org (8.14.9/8.14.9/Submit) id t6EIjFGh067539; Tue, 14 Jul 2015 18:45:15 GMT (envelope-from ed@FreeBSD.org) Message-Id: <201507141845.t6EIjFGh067539@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ed set sender to ed@FreeBSD.org using -f From: Ed Schouten Date: Tue, 14 Jul 2015 18:45:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r285549 - in head: share/man/man9 sys/compat/cloudabi sys/dev/random sys/sys X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Jul 2015 18:45:18 -0000 Author: ed Date: Tue Jul 14 18:45:15 2015 New Revision: 285549 URL: https://svnweb.freebsd.org/changeset/base/285549 Log: Implement the CloudABI random_get() system call. The random_get() system call works similar to getentropy()/getrandom() on OpenBSD/Linux. It fills a buffer with random data. This change introduces a new function, read_random_uio(), that is used to implement read() on the random devices. We can call into this function from within the CloudABI compatibility layer. Approved by: secteam Reviewed by: jmg, markm, wblock Obtained from: https://github.com/NuxiNL/freebsd Differential Revision: https://reviews.freebsd.org/D3053 Modified: head/share/man/man9/Makefile head/share/man/man9/random.9 head/sys/compat/cloudabi/cloudabi_random.c head/sys/dev/random/randomdev.c head/sys/sys/random.h Modified: head/share/man/man9/Makefile ============================================================================== --- head/share/man/man9/Makefile Tue Jul 14 18:24:05 2015 (r285548) +++ head/share/man/man9/Makefile Tue Jul 14 18:45:15 2015 (r285549) @@ -1281,6 +1281,7 @@ MLINKS+=psignal.9 gsignal.9 \ MLINKS+=random.9 arc4rand.9 \ random.9 arc4random.9 \ random.9 read_random.9 \ + random.9 read_random_uio.9 \ random.9 srandom.9 MLINKS+=refcount.9 refcount_acquire.9 \ refcount.9 refcount_init.9 \ Modified: head/share/man/man9/random.9 ============================================================================== --- head/share/man/man9/random.9 Tue Jul 14 18:24:05 2015 (r285548) +++ head/share/man/man9/random.9 Tue Jul 14 18:45:15 2015 (r285549) @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" " -.Dd June 30, 2015 +.Dd July 14, 2015 .Dt RANDOM 9 .Os .Sh NAME @@ -34,6 +34,7 @@ .Nm arc4random , .Nm random , .Nm read_random , +.Nm read_random_uio , .Nm srandom .Nd supply pseudo-random numbers .Sh SYNOPSIS @@ -50,6 +51,8 @@ .In sys/random.h .Ft int .Fn read_random "void *buffer" "int count" +.Ft int +.Fn read_random_uio "struct uio *uio" "bool nonblock" .Sh DESCRIPTION The .Fn random @@ -117,11 +120,27 @@ necessary to know that no entropy has been returned. .Pp +The +.Fn read_random_uio +function behaves identically to +.Xr read 2 +on +.Pa /dev/random . +The +.Fa uio +argument points to a buffer where random data should be stored. +This function only returns data if the the random device is seeded. +It blocks if unseeded, +except when the +.Fa nonblock +argument is true. +.Pp All the bits returned by .Fn random , -.Fn arc4rand +.Fn arc4rand , +.Fn read_random , and -.Fn read_random +.Fn read_random_uio are usable. For example, .Sq Li random()&01 @@ -168,6 +187,22 @@ The function returns the number of bytes placed in .Fa buffer . +.Pp +.Fn read_random_uio +returns zero when successful, +otherwise an error code is returned. +.Sh ERRORS +.Fn read_random_uio +may fail if: +.Bl -tag -width Er +.It Bq Er EFAULT +.Fa uio +points to an invalid memory region. +.It Bq Er EWOULDBLOCK +The random device is unseeded and +.Fa nonblock +is true. +.El .Sh AUTHORS .An Dan Moschuk wrote Modified: head/sys/compat/cloudabi/cloudabi_random.c ============================================================================== --- head/sys/compat/cloudabi/cloudabi_random.c Tue Jul 14 18:24:05 2015 (r285548) +++ head/sys/compat/cloudabi/cloudabi_random.c Tue Jul 14 18:45:15 2015 (r285549) @@ -26,13 +26,28 @@ #include __FBSDID("$FreeBSD$"); +#include +#include +#include + #include int cloudabi_sys_random_get(struct thread *td, struct cloudabi_sys_random_get_args *uap) { + struct iovec iov = { + .iov_base = uap->buf, + .iov_len = uap->nbyte + }; + struct uio uio = { + .uio_iov = &iov, + .uio_iovcnt = 1, + .uio_resid = iov.iov_len, + .uio_segflg = UIO_USERSPACE, + .uio_rw = UIO_READ, + .uio_td = td + }; - /* Not implemented. */ - return (ENOSYS); + return (read_random_uio(&uio, false)); } Modified: head/sys/dev/random/randomdev.c ============================================================================== --- head/sys/dev/random/randomdev.c Tue Jul 14 18:24:05 2015 (r285548) +++ head/sys/dev/random/randomdev.c Tue Jul 14 18:45:15 2015 (r285549) @@ -152,6 +152,13 @@ static struct selinfo rsel; static int randomdev_read(struct cdev *dev __unused, struct uio *uio, int flags) { + + return (read_random_uio(uio, (flags & O_NONBLOCK) != 0)); +} + +int +read_random_uio(struct uio *uio, bool nonblock) +{ uint8_t *random_buf; int error; ssize_t read_len, total_read, c; Modified: head/sys/sys/random.h ============================================================================== --- head/sys/sys/random.h Tue Jul 14 18:24:05 2015 (r285548) +++ head/sys/sys/random.h Tue Jul 14 18:45:15 2015 (r285549) @@ -31,7 +31,12 @@ #ifdef _KERNEL +#include + +struct uio; + u_int read_random(void *, u_int); +int read_random_uio(struct uio *, bool); /* * Note: if you add or remove members of random_entropy_source, remember to also update the