summaryrefslogtreecommitdiff
path: root/sys/kern
diff options
context:
space:
mode:
authorriastradh <riastradh@NetBSD.org>2020-08-14 00:53:15 +0000
committerriastradh <riastradh@NetBSD.org>2020-08-14 00:53:15 +0000
commite2fa4d60fd222537c72bee82b7b9d8a56f9727ff (patch)
tree41a0209dde4f6cd8b4dd8bbcdd74c6d610a641c5 /sys/kern
parentf1e4c716a0c7708391ccc7132fc073a9cfa7aca4 (diff)
New system call getrandom() compatible with Linux and others.
Three ways to call: getrandom(p, n, 0) Blocks at boot until full entropy. Returns up to n bytes at p; guarantees up to 256 bytes even if interrupted after blocking. getrandom(0,0,0) serves as an entropy barrier: return only after system has full entropy. getrandom(p, n, GRND_INSECURE) Never blocks. Guarantees up to 256 bytes even if interrupted. Equivalent to /dev/urandom. Safe only after successful getrandom(...,0), getrandom(...,GRND_RANDOM), or read from /dev/random. getrandom(p, n, GRND_RANDOM) May block at any time. Returns up to n bytes at p, but no guarantees about how many -- may return as short as 1 byte. Equivalent to /dev/random. Legacy. Provided only for source compatibility with Linux. Can also use flags|GRND_NONBLOCK to fail with EWOULDBLOCK/EAGAIN without producing any output instead of blocking. - The combination GRND_INSECURE|GRND_NONBLOCK is the same as GRND_INSECURE, since GRND_INSECURE never blocks anyway. - The combinations GRND_INSECURE|GRND_RANDOM and GRND_INSECURE|GRND_RANDOM|GRND_NONBLOCK are nonsensical and fail with EINVAL. As proposed on tech-userlevel, tech-crypto, tech-security, and tech-kern, and subsequently adopted by core (minus the getentropy part of the proposal, because other operating systems and participants in the discussion couldn't come to an agreement about getentropy and blocking semantics): https://mail-index.netbsd.org/tech-userlevel/2020/05/02/msg012333.html
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/files.kern3
-rw-r--r--sys/kern/kern_entropy.c30
-rw-r--r--sys/kern/sys_getrandom.c246
-rw-r--r--sys/kern/syscalls.master5
4 files changed, 276 insertions, 8 deletions
diff --git a/sys/kern/files.kern b/sys/kern/files.kern
index 9d3acfd4229..d40d9d6b0df 100644
--- a/sys/kern/files.kern
+++ b/sys/kern/files.kern
@@ -1,4 +1,4 @@
-# $NetBSD: files.kern,v 1.50 2020/07/28 20:15:07 riastradh Exp $
+# $NetBSD: files.kern,v 1.51 2020/08/14 00:53:16 riastradh Exp $
#
# kernel sources
@@ -157,6 +157,7 @@ file kern/sys_aio.c aio
file kern/sys_descrip.c kern
file kern/sys_futex.c kern
file kern/sys_generic.c kern
+file kern/sys_getrandom.c kern
file kern/sys_module.c kern
file kern/sys_mqueue.c mqueue
file kern/sys_lwp.c kern
diff --git a/sys/kern/kern_entropy.c b/sys/kern/kern_entropy.c
index f9519810312..1bb1350c68e 100644
--- a/sys/kern/kern_entropy.c
+++ b/sys/kern/kern_entropy.c
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_entropy.c,v 1.22 2020/05/12 20:50:17 riastradh Exp $ */
+/* $NetBSD: kern_entropy.c,v 1.23 2020/08/14 00:53:16 riastradh Exp $ */
/*-
* Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.22 2020/05/12 20:50:17 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.23 2020/08/14 00:53:16 riastradh Exp $");
#include <sys/param.h>
#include <sys/types.h>
@@ -612,6 +612,18 @@ entropy_epoch(void)
}
/*
+ * entropy_ready()
+ *
+ * True if the entropy pool has full entropy.
+ */
+bool
+entropy_ready(void)
+{
+
+ return atomic_load_relaxed(&E->needed) == 0;
+}
+
+/*
* entropy_account_cpu(ec)
*
* Consider whether to consolidate entropy into the global pool
@@ -1231,6 +1243,8 @@ sysctl_entropy_gather(SYSCTLFN_ARGS)
*
* ENTROPY_WAIT Wait for entropy if not available yet.
* ENTROPY_SIG Allow interruption by a signal during wait.
+ * ENTROPY_HARDFAIL Either fill the buffer with full entropy,
+ * or fail without filling it at all.
*
* Return zero on success, or error on failure:
*
@@ -1292,9 +1306,15 @@ entropy_extract(void *buf, size_t len, int flags)
}
}
- /* Count failure -- but fill the buffer nevertheless. */
- if (error)
+ /*
+ * Count failure -- but fill the buffer nevertheless, unless
+ * the caller specified ENTROPY_HARDFAIL.
+ */
+ if (error) {
+ if (ISSET(flags, ENTROPY_HARDFAIL))
+ goto out;
entropy_extract_fail_evcnt.ev_count++;
+ }
/*
* Report a warning if we have never yet reached full entropy.
@@ -1324,7 +1344,7 @@ entropy_extract(void *buf, size_t len, int flags)
entropy_deplete_evcnt.ev_count++;
}
- /* Release the global lock and return the error. */
+out: /* Release the global lock and return the error. */
if (E->stage >= ENTROPY_WARM)
mutex_exit(&E->lock);
return error;
diff --git a/sys/kern/sys_getrandom.c b/sys/kern/sys_getrandom.c
new file mode 100644
index 00000000000..947b5d79717
--- /dev/null
+++ b/sys/kern/sys_getrandom.c
@@ -0,0 +1,246 @@
+/* $NetBSD: sys_getrandom.c,v 1.1 2020/08/14 00:53:16 riastradh Exp $ */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Taylor R. Campbell.
+ *
+ * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``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 FOUNDATION 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.
+ */
+
+/*
+ * getrandom() system call
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: sys_getrandom.c,v 1.1 2020/08/14 00:53:16 riastradh Exp $");
+
+#include <sys/types.h>
+#include <sys/param.h>
+
+#include <sys/atomic.h>
+#include <sys/cprng.h>
+#include <sys/entropy.h>
+#include <sys/kmem.h>
+#include <sys/lwp.h>
+#include <sys/proc.h>
+#include <sys/random.h>
+#include <sys/sched.h>
+#include <sys/signalvar.h>
+#include <sys/syscallargs.h>
+#include <sys/uio.h>
+
+#include <crypto/nist_hash_drbg/nist_hash_drbg.h>
+
+#define RANDOM_BUFSIZE 512
+
+int
+dogetrandom(struct uio *uio, unsigned int flags)
+{
+ uint8_t seed[NIST_HASH_DRBG_SEEDLEN_BYTES] = {0};
+ struct nist_hash_drbg drbg;
+ uint8_t *buf;
+ int extractflags = 0;
+ int error;
+
+ KASSERT((flags & ~(GRND_RANDOM|GRND_INSECURE|GRND_NONBLOCK)) == 0);
+ KASSERT((flags & (GRND_RANDOM|GRND_INSECURE)) !=
+ (GRND_RANDOM|GRND_INSECURE));
+
+ /* Get a buffer for transfers. */
+ buf = kmem_alloc(RANDOM_BUFSIZE, KM_SLEEP);
+
+ /*
+ * Fast path: for short reads other than from /dev/random, if
+ * seeded or if INSECURE, just draw from per-CPU cprng_strong.
+ */
+ if (uio->uio_resid <= RANDOM_BUFSIZE &&
+ !ISSET(flags, GRND_RANDOM) &&
+ (entropy_ready() || ISSET(flags, GRND_INSECURE))) {
+ /* Generate data and transfer it out. */
+ cprng_strong(user_cprng, buf, uio->uio_resid, 0);
+ error = uiomove(buf, uio->uio_resid, uio);
+ goto out;
+ }
+
+ /*
+ * Try to get a seed from the entropy pool. Fail if we would
+ * block. If GRND_INSECURE, always return something even if it
+ * is partial entropy; if !GRND_INSECURE, set ENTROPY_HARDFAIL
+ * in order to tell entropy_extract not to bother drawing
+ * anything from a partial pool if we can't get full entropy.
+ */
+ if (!ISSET(flags, GRND_NONBLOCK) && !ISSET(flags, GRND_INSECURE))
+ extractflags |= ENTROPY_WAIT|ENTROPY_SIG;
+ if (!ISSET(flags, GRND_INSECURE))
+ extractflags |= ENTROPY_HARDFAIL;
+ error = entropy_extract(seed, sizeof seed, extractflags);
+ if (error && !ISSET(flags, GRND_INSECURE))
+ goto out;
+
+ /* Instantiate the DRBG. */
+ if (nist_hash_drbg_instantiate(&drbg, seed, sizeof seed, NULL, 0,
+ NULL, 0))
+ panic("nist_hash_drbg_instantiate");
+
+ /* Promptly zero the seed. */
+ explicit_memset(seed, 0, sizeof seed);
+
+ /* Generate data. */
+ error = 0;
+ while (uio->uio_resid) {
+ size_t n = MIN(uio->uio_resid, RANDOM_BUFSIZE);
+
+ /*
+ * Clamp /dev/random output to the entropy capacity and
+ * seed size. Programs can't rely on long reads.
+ */
+ if (ISSET(flags, GRND_RANDOM)) {
+ n = MIN(n, ENTROPY_CAPACITY);
+ n = MIN(n, sizeof seed);
+ /*
+ * Guarantee never to return more than one
+ * buffer in this case to minimize bookkeeping.
+ */
+ CTASSERT(ENTROPY_CAPACITY <= RANDOM_BUFSIZE);
+ CTASSERT(sizeof seed <= RANDOM_BUFSIZE);
+ }
+
+ /*
+ * Try to generate a block of data, but if we've hit
+ * the DRBG reseed interval, reseed.
+ */
+ if (nist_hash_drbg_generate(&drbg, buf, n, NULL, 0)) {
+ /*
+ * Get a fresh seed without blocking -- we have
+ * already generated some output so it is not
+ * useful to block. This can fail only if the
+ * request is obscenely large, so it is OK for
+ * either /dev/random or /dev/urandom to fail:
+ * we make no promises about gigabyte-sized
+ * reads happening all at once.
+ */
+ error = entropy_extract(seed, sizeof seed,
+ ENTROPY_HARDFAIL);
+ if (error)
+ break;
+
+ /* Reseed and try again. */
+ if (nist_hash_drbg_reseed(&drbg, seed, sizeof seed,
+ NULL, 0))
+ panic("nist_hash_drbg_reseed");
+
+ /* Promptly zero the seed. */
+ explicit_memset(seed, 0, sizeof seed);
+
+ /* If it fails now, that's a bug. */
+ if (nist_hash_drbg_generate(&drbg, buf, n, NULL, 0))
+ panic("nist_hash_drbg_generate");
+ }
+
+ /* Transfer n bytes out. */
+ error = uiomove(buf, n, uio);
+ if (error)
+ break;
+
+ /*
+ * If this is /dev/random, stop here, return what we
+ * have, and force the next read to reseed. Programs
+ * can't rely on /dev/random for long reads.
+ */
+ if (ISSET(flags, GRND_RANDOM)) {
+ error = 0;
+ break;
+ }
+
+ /* Yield if requested. */
+ if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
+ preempt();
+
+ /* Check for interruption after at least 256 bytes. */
+ CTASSERT(RANDOM_BUFSIZE >= 256);
+ if (__predict_false(curlwp->l_flag & LW_PENDSIG) &&
+ sigispending(curlwp, 0)) {
+ error = EINTR;
+ break;
+ }
+ }
+
+out: /* Zero the buffer and free it. */
+ explicit_memset(buf, 0, RANDOM_BUFSIZE);
+ kmem_free(buf, RANDOM_BUFSIZE);
+
+ return error;
+}
+
+int
+sys_getrandom(struct lwp *l, const struct sys_getrandom_args *uap,
+ register_t *retval)
+{
+ /* {
+ syscallarg(void *) buf;
+ syscallarg(size_t) buflen;
+ syscallarg(unsigned) flags;
+ } */
+ void *buf = SCARG(uap, buf);
+ size_t buflen = SCARG(uap, buflen);
+ int flags = SCARG(uap, flags);
+ int error;
+
+ /* Set up an iov and uio to read into the user's buffer. */
+ struct iovec iov = { .iov_base = buf, .iov_len = buflen };
+ struct uio uio = {
+ .uio_iov = &iov,
+ .uio_iovcnt = 1,
+ .uio_offset = 0,
+ .uio_resid = buflen,
+ .uio_rw = UIO_READ,
+ .uio_vmspace = curproc->p_vmspace,
+ };
+
+ /* Validate the flags. */
+ if (flags & ~(GRND_RANDOM|GRND_INSECURE|GRND_NONBLOCK)) {
+ /* Unknown flags. */
+ error = EINVAL;
+ goto out;
+ }
+ if ((flags & (GRND_RANDOM|GRND_INSECURE)) ==
+ (GRND_RANDOM|GRND_INSECURE)) {
+ /* Nonsensical combination. */
+ error = EINVAL;
+ goto out;
+ }
+
+ /* Do it. */
+ error = dogetrandom(&uio, flags);
+
+out: /*
+ * If we transferred anything, return the number of bytes
+ * transferred and suppress error; otherwise return the error.
+ */
+ *retval = buflen - uio.uio_resid;
+ if (*retval)
+ error = 0;
+ return error;
+}
diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master
index a53b8a25b4b..f814c189ee6 100644
--- a/sys/kern/syscalls.master
+++ b/sys/kern/syscalls.master
@@ -1,4 +1,4 @@
- $NetBSD: syscalls.master,v 1.305 2020/05/16 18:31:50 christos Exp $
+ $NetBSD: syscalls.master,v 1.306 2020/08/14 00:53:16 riastradh Exp $
; @(#)syscalls.master 8.2 (Berkeley) 1/13/94
@@ -216,7 +216,8 @@
89 COMPAT_43 MODULAR compat_43 \
{ int|sys||getdtablesize(void); } ogetdtablesize
90 STD RUMP { int|sys||dup2(int from, int to); }
-91 UNIMPL getdopt
+91 STD RUMP { ssize_t|sys||getrandom(void *buf, size_t buflen, \
+ unsigned int flags); }
92 STD RUMP { int|sys||fcntl(int fd, int cmd, ... void *arg); }
93 COMPAT_50 MODULAR compat_50 RUMP \
{ int|sys||select(int nd, fd_set *in, fd_set *ou, \