Index: head/lib/libc/sys/getrandom.2 =================================================================== --- head/lib/libc/sys/getrandom.2 (revision 356666) +++ head/lib/libc/sys/getrandom.2 (revision 356667) @@ -1,121 +1,133 @@ -.\" Copyright (c) 2018 Conrad Meyer -.\" All rights reserved. +.\" Copyright 2020, 2018 Conrad Meyer . 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 AUTHORS 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 AUTHORS 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. .\" .\" $FreeBSD$ .\" -.Dd February 24, 2018 +.Dd January 12, 2020 .Dt GETRANDOM 2 .Os .Sh NAME .Nm getrandom .Nd get random data .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In sys/random.h .Ft ssize_t .Fn getrandom "void *buf" "size_t buflen" "unsigned int flags" .Sh DESCRIPTION .Fn getrandom fills .Fa buf with up to .Fa buflen bytes of random data. .Pp The .Fa flags argument may include zero or more of the following: .Bl -tag -width _GRND_NONBLOCK_ .It Ql GRND_NONBLOCK Return .Er EAGAIN instead of blocking, if the .Xr random 4 device has not yet been seeded. By default, .Fn getrandom will block until the device is seeded. .It Ql GRND_RANDOM This flag does nothing on .Fx . .Pa /dev/random and .Pa /dev/urandom are identical. +.It Ql GRND_INSECURE +This flag is treated as an alternative name for +.Dv GRND_NONBLOCK . +It is provided solely for API compatibility with Linux. .El .Pp If the .Xr random 4 device has been seeded, reads of up to 256 bytes will always return as many bytes as requested and will not be interrupted by signals. -.Pp .Sh RETURN VALUES Upon successful completion, the number of bytes which were actually read is returned. For requests larger than 256 bytes, this can be fewer bytes than were requested. Otherwise, -1 is returned and the global variable .Va errno is set to indicate the error. .Sh ERRORS The .Fn getrandom operation returns the following errors: .Bl -tag -width Er .It Bq Er EAGAIN The .Ql GRND_NONBLOCK +(or +.Ql GRND_INSECURE ) flag was set and the .Xr random 4 device was not yet seeded. .It Bq Er EFAULT The .Fa buf parameter points to an invalid address. .It Bq Er EINTR The sleep was interrupted by a signal. .It Bq Er EINVAL An invalid .Fa flags was specified. .It Bq Er EINVAL The requested .Fa buflen was larger than .Dv IOSIZE_MAX . .El .Sh SEE ALSO .Xr arc4random 3 , .Xr getentropy 3 , .Xr random 4 .Sh STANDARDS -.Fn getentropy +.Fn getrandom is non-standard. It is present in Linux. .Sh HISTORY The .Fn getrandom system call first appeared in .Fx 12.0 . +.Sh CAVEATS +Unlike Linux, the +.Dv GRND_INSECURE +flag on +.Fx +does not produce any output before the +.Xr random 4 +device is seeded. Index: head/sys/kern/sys_getrandom.c =================================================================== --- head/sys/kern/sys_getrandom.c (revision 356666) +++ head/sys/kern/sys_getrandom.c (revision 356667) @@ -1,96 +1,130 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2018 Conrad Meyer * 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include -#define GRND_VALIDFLAGS (GRND_NONBLOCK | GRND_RANDOM) +#define GRND_VALIDFLAGS (GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE) /* * 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); + + /* + * Linux compatibility: We have two choices for handling Linux's + * GRND_INSECURE. + * + * 1. We could ignore it completely (like GRND_RANDOM). However, this + * might produce the surprising result of GRND_INSECURE requests + * blocking, when the Linux API does not block. + * + * 2. Alternatively, we could treat GRND_INSECURE requests as requests + * for GRND_NONBLOCk. Here, the surprising result for Linux programs + * is that invocations with unseeded random(4) will produce EAGAIN, + * rather than garbage. + * + * Honoring the flag in the way Linux does seems fraught. If we + * actually use the output of a random(4) implementation prior to + * seeding, we leak some entropy about the initial seed to attackers. + * This seems unacceptable -- it defeats the purpose of blocking on + * initial seeding. + * + * Secondary to that concern, before seeding we may have arbitrarily + * little entropy collected; producing output from zero or a handful of + * entropy bits does not seem particularly useful to userspace. + * + * If userspace can accept garbage, insecure non-random bytes, they can + * create their own insecure garbage with srandom(time(NULL)) or + * similar. Asking the kernel to produce it from the secure + * getrandom(2) API seems inane. + * + * We elect to emulate GRND_INSECURE as an alternative spelling of + * GRND_NONBLOCK (2). + */ + if ((flags & GRND_INSECURE) != 0) + flags |= GRND_NONBLOCK; 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)); } Index: head/sys/sys/random.h =================================================================== --- head/sys/sys/random.h (revision 356666) +++ head/sys/sys/random.h (revision 356667) @@ -1,167 +1,168 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2000-2015, 2017 Mark R. V. Murray * 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 * in this position and unchanged. * 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 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. * * $FreeBSD$ */ #ifndef _SYS_RANDOM_H_ #define _SYS_RANDOM_H_ #include #ifdef _KERNEL struct uio; /* * In the loadable random world, there are set of dangling pointers left in the * core kernel: * * read_random, read_random_uio, is_random_seeded are function pointers, * rather than functions. * * p_random_alg_context is a true pointer in loadable random kernels. * * These are initialized at SI_SUB_RANDOM:SI_ORDER_SECOND during boot. The * read-type pointers are initialized by random_alg_context_init() in * randomdev.c and p_random_alg_context in the algorithm, e.g., fortuna.c's * random_fortuna_init_alg(). The nice thing about function pointers is they * have a similar calling convention to ordinary functions. * * (In !loadable, the read_random, etc, routines are just plain functions; * p_random_alg_context is a macro for the public visibility * &random_alg_context.) */ #if defined(RANDOM_LOADABLE) extern void (*_read_random)(void *, u_int); extern int (*_read_random_uio)(struct uio *, bool); extern bool (*_is_random_seeded)(void); #define read_random(a, b) (*_read_random)(a, b) #define read_random_uio(a, b) (*_read_random_uio)(a, b) #define is_random_seeded() (*_is_random_seeded)() #else void read_random(void *, u_int); int read_random_uio(struct uio *, bool); bool is_random_seeded(void); #endif /* * Note: if you add or remove members of random_entropy_source, remember to * also update the strings in the static array random_source_descr[] in * random_harvestq.c. */ enum random_entropy_source { RANDOM_START = 0, RANDOM_CACHED = 0, /* Environmental sources */ RANDOM_ATTACH, RANDOM_KEYBOARD, RANDOM_MOUSE, RANDOM_NET_TUN, RANDOM_NET_ETHER, RANDOM_NET_NG, RANDOM_INTERRUPT, RANDOM_SWI, RANDOM_FS_ATIME, RANDOM_UMA, /* Special!! UMA/SLAB Allocator */ RANDOM_ENVIRONMENTAL_END = RANDOM_UMA, /* Fast hardware random-number sources from here on. */ RANDOM_PURE_START, RANDOM_PURE_OCTEON = RANDOM_PURE_START, RANDOM_PURE_SAFE, RANDOM_PURE_GLXSB, RANDOM_PURE_UBSEC, RANDOM_PURE_HIFN, RANDOM_PURE_RDRAND, RANDOM_PURE_NEHEMIAH, RANDOM_PURE_RNDTEST, RANDOM_PURE_VIRTIO, RANDOM_PURE_BROADCOM, RANDOM_PURE_CCP, RANDOM_PURE_DARN, RANDOM_PURE_TPM, RANDOM_PURE_VMGENID, ENTROPYSOURCE }; _Static_assert(ENTROPYSOURCE <= 32, "hardcoded assumption that values fit in a typical word-sized bitset"); #define RANDOM_CACHED_BOOT_ENTROPY_MODULE "boot_entropy_cache" extern u_int hc_source_mask; void random_harvest_queue_(const void *, u_int, enum random_entropy_source); void random_harvest_fast_(const void *, u_int); void random_harvest_direct_(const void *, u_int, enum random_entropy_source); static __inline void random_harvest_queue(const void *entropy, u_int size, enum random_entropy_source origin) { if (hc_source_mask & (1 << origin)) random_harvest_queue_(entropy, size, origin); } static __inline void random_harvest_fast(const void *entropy, u_int size, enum random_entropy_source origin) { if (hc_source_mask & (1 << origin)) random_harvest_fast_(entropy, size); } static __inline void random_harvest_direct(const void *entropy, u_int size, enum random_entropy_source origin) { if (hc_source_mask & (1 << origin)) random_harvest_direct_(entropy, size, origin); } void random_harvest_register_source(enum random_entropy_source); void random_harvest_deregister_source(enum random_entropy_source); #if defined(RANDOM_ENABLE_UMA) #define random_harvest_fast_uma(a, b, c) random_harvest_fast(a, b, c) #else /* !defined(RANDOM_ENABLE_UMA) */ #define random_harvest_fast_uma(a, b, c) do {} while (0) #endif /* defined(RANDOM_ENABLE_UMA) */ #if defined(RANDOM_ENABLE_ETHER) #define random_harvest_queue_ether(a, b) random_harvest_queue(a, b, RANDOM_NET_ETHER) #else /* !defined(RANDOM_ENABLE_ETHER) */ #define random_harvest_queue_ether(a, b) do {} while (0) #endif /* defined(RANDOM_ENABLE_ETHER) */ #endif /* _KERNEL */ #define GRND_NONBLOCK 0x1 #define GRND_RANDOM 0x2 +#define GRND_INSECURE 0x4 __BEGIN_DECLS ssize_t getrandom(void *buf, size_t buflen, unsigned int flags); __END_DECLS #endif /* _SYS_RANDOM_H_ */