aboutsummaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorConrad Meyer <cem@FreeBSD.org>2018-03-21 01:15:45 +0000
committerConrad Meyer <cem@FreeBSD.org>2018-03-21 01:15:45 +0000
commite9ac27430c0c639c1f89b1f048a44f6cea0ec265 (patch)
tree4db48083e97206fa5c935d41e2e7c09a9c7ed033 /sys
parent672756aa9f1033719fff013226f46b3e46fe3ea5 (diff)
downloadsrc-e9ac27430c0c639c1f89b1f048a44f6cea0ec265.tar.gz
src-e9ac27430c0c639c1f89b1f048a44f6cea0ec265.zip
Implement getrandom(2) and getentropy(3)
The general idea here is to provide userspace programs with well-defined sources of entropy, in a fashion that doesn't require opening a new file descriptor (ulimits) or accessing paths (/dev/urandom may be restricted by chroot or capsicum). getrandom(2) is the more general API, and comes from the Linux world. Since our urandom and random devices are identical, the GRND_RANDOM flag is ignored. getentropy(3) is added as a compatibility shim for the OpenBSD API. truss(1) support is included. Tests for both system calls are provided. Coverage is believed to be at least as comprehensive as LTP getrandom(2) test coverage. Additionally, instructions for running the LTP tests directly against FreeBSD are provided in the "Test Plan" section of the Differential revision linked below. (They pass, of course.) PR: 194204 Reported by: David CARLIER <david.carlier AT hardenedbsd.org> Discussed with: cperciva, delphij, jhb, markj Relnotes: maybe Differential Revision: https://reviews.freebsd.org/D14500
Notes
Notes: svn path=/head/; revision=331279
Diffstat (limited to 'sys')
-rw-r--r--sys/compat/freebsd32/syscalls.master2
-rw-r--r--sys/conf/files1
-rw-r--r--sys/kern/sys_getrandom.c97
-rw-r--r--sys/kern/syscalls.master2
-rw-r--r--sys/sys/random.h8
5 files changed, 108 insertions, 2 deletions
diff --git a/sys/compat/freebsd32/syscalls.master b/sys/compat/freebsd32/syscalls.master
index 50613d037ca8..6e9a3a63d61f 100644
--- a/sys/compat/freebsd32/syscalls.master
+++ b/sys/compat/freebsd32/syscalls.master
@@ -1116,5 +1116,7 @@
cpuwhich_t which, uint32_t id1, uint32_t id2, \
size_t domainsetsize, domainset_t *mask, \
int policy); }
+563 AUE_NULL NOPROTO { int getrandom(void *buf, size_t buflen, \
+ unsigned int flags); }
; vim: syntax=off
diff --git a/sys/conf/files b/sys/conf/files
index 99b9c87734bb..92f39e6b1443 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -3895,6 +3895,7 @@ kern/subr_vmem.c standard
kern/subr_witness.c optional witness
kern/sys_capability.c standard
kern/sys_generic.c standard
+kern/sys_getrandom.c standard
kern/sys_pipe.c standard
kern/sys_procdesc.c standard
kern/sys_process.c standard
diff --git a/sys/kern/sys_getrandom.c b/sys/kern/sys_getrandom.c
new file mode 100644
index 000000000000..34916df79be7
--- /dev/null
+++ b/sys/kern/sys_getrandom.c
@@ -0,0 +1,97 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org>
+ * 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 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 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 <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "opt_compat.h"
+
+#include <sys/param.h>
+#include <sys/errno.h>
+#include <sys/proc.h>
+#include <sys/random.h>
+#include <sys/sysproto.h>
+#include <sys/systm.h>
+#include <sys/uio.h>
+
+#define GRND_VALIDFLAGS (GRND_NONBLOCK | GRND_RANDOM)
+
+/*
+ * random_read_uio(9) returns EWOULDBLOCK if a nonblocking request would block,
+ * but the Linux API name is EAGAIN. On FreeBSD, they have the same numeric
+ * value for now.
+ */
+CTASSERT(EWOULDBLOCK == EAGAIN);
+
+static int
+kern_getrandom(struct thread *td, void *user_buf, size_t buflen,
+ unsigned int flags)
+{
+ struct uio auio;
+ struct iovec aiov;
+ int error;
+
+ if ((flags & ~GRND_VALIDFLAGS) != 0)
+ return (EINVAL);
+ if (buflen > IOSIZE_MAX)
+ return (EINVAL);
+
+ if (buflen == 0) {
+ td->td_retval[0] = 0;
+ return (0);
+ }
+
+ aiov.iov_base = user_buf;
+ aiov.iov_len = buflen;
+ auio.uio_iov = &aiov;
+ auio.uio_iovcnt = 1;
+ auio.uio_offset = 0;
+ auio.uio_resid = buflen;
+ auio.uio_segflg = UIO_USERSPACE;
+ auio.uio_rw = UIO_READ;
+ auio.uio_td = td;
+
+ error = read_random_uio(&auio, (flags & GRND_NONBLOCK) != 0);
+ if (error == 0)
+ td->td_retval[0] = buflen - auio.uio_resid;
+ return (error);
+}
+
+#ifndef _SYS_SYSPROTO_H_
+struct getrandom_args {
+ void *buf;
+ size_t buflen;
+ unsigned int flags;
+};
+#endif
+
+int
+sys_getrandom(struct thread *td, struct getrandom_args *uap)
+{
+ return (kern_getrandom(td, uap->buf, uap->buflen, uap->flags));
+}
diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master
index 00d47533c4e5..871d379034c6 100644
--- a/sys/kern/syscalls.master
+++ b/sys/kern/syscalls.master
@@ -1021,6 +1021,8 @@
cpuwhich_t which, id_t id, \
size_t domainsetsize, domainset_t *mask, \
int policy); }
+563 AUE_NULL STD { int getrandom(void *buf, size_t buflen, \
+ unsigned int flags); }
; Please copy any additions and changes to the following compatability tables:
; sys/compat/freebsd32/syscalls.master
diff --git a/sys/sys/random.h b/sys/sys/random.h
index cf8e663d8f9e..ed71f34b3386 100644
--- a/sys/sys/random.h
+++ b/sys/sys/random.h
@@ -31,10 +31,10 @@
#ifndef _SYS_RANDOM_H_
#define _SYS_RANDOM_H_
-#ifdef _KERNEL
-
#include <sys/types.h>
+#ifdef _KERNEL
+
#if !defined(KLD_MODULE)
#if defined(RANDOM_LOADABLE) && defined(RANDOM_YARROW)
#error "Cannot define both RANDOM_LOADABLE and RANDOM_YARROW"
@@ -127,4 +127,8 @@ void random_harvest_deregister_source(enum random_entropy_source);
#endif /* _KERNEL */
+#define GRND_NONBLOCK 0x1
+#define GRND_RANDOM 0x2
+ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
+
#endif /* _SYS_RANDOM_H_ */