xref: /f-stack/freebsd/kern/sys_getrandom.c (revision 22ce4aff)
1*22ce4affSfengbojiang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
4*22ce4affSfengbojiang  * Copyright (c) 2018 Conrad Meyer <[email protected]>
5*22ce4affSfengbojiang  * All rights reserved.
6*22ce4affSfengbojiang  *
7*22ce4affSfengbojiang  * Redistribution and use in source and binary forms, with or without
8*22ce4affSfengbojiang  * modification, are permitted provided that the following conditions
9*22ce4affSfengbojiang  * are met:
10*22ce4affSfengbojiang  * 1. Redistributions of source code must retain the above copyright
11*22ce4affSfengbojiang  *    notice, this list of conditions and the following disclaimer.
12*22ce4affSfengbojiang  * 2. Redistributions in binary form must reproduce the above copyright
13*22ce4affSfengbojiang  *    notice, this list of conditions and the following disclaimer in the
14*22ce4affSfengbojiang  *    documentation and/or other materials provided with the distribution.
15*22ce4affSfengbojiang  *
16*22ce4affSfengbojiang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*22ce4affSfengbojiang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*22ce4affSfengbojiang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*22ce4affSfengbojiang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*22ce4affSfengbojiang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*22ce4affSfengbojiang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*22ce4affSfengbojiang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*22ce4affSfengbojiang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*22ce4affSfengbojiang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*22ce4affSfengbojiang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*22ce4affSfengbojiang  * SUCH DAMAGE.
27*22ce4affSfengbojiang  */
28*22ce4affSfengbojiang 
29*22ce4affSfengbojiang #include <sys/cdefs.h>
30*22ce4affSfengbojiang __FBSDID("$FreeBSD$");
31*22ce4affSfengbojiang 
32*22ce4affSfengbojiang #include <sys/param.h>
33*22ce4affSfengbojiang #include <sys/errno.h>
34*22ce4affSfengbojiang #include <sys/limits.h>
35*22ce4affSfengbojiang #include <sys/proc.h>
36*22ce4affSfengbojiang #include <sys/random.h>
37*22ce4affSfengbojiang #include <sys/sysproto.h>
38*22ce4affSfengbojiang #include <sys/systm.h>
39*22ce4affSfengbojiang #include <sys/uio.h>
40*22ce4affSfengbojiang 
41*22ce4affSfengbojiang #define GRND_VALIDFLAGS	(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE)
42*22ce4affSfengbojiang 
43*22ce4affSfengbojiang /*
44*22ce4affSfengbojiang  * random_read_uio(9) returns EWOULDBLOCK if a nonblocking request would block,
45*22ce4affSfengbojiang  * but the Linux API name is EAGAIN.  On FreeBSD, they have the same numeric
46*22ce4affSfengbojiang  * value for now.
47*22ce4affSfengbojiang  */
48*22ce4affSfengbojiang CTASSERT(EWOULDBLOCK == EAGAIN);
49*22ce4affSfengbojiang 
50*22ce4affSfengbojiang static int
kern_getrandom(struct thread * td,void * user_buf,size_t buflen,unsigned int flags)51*22ce4affSfengbojiang kern_getrandom(struct thread *td, void *user_buf, size_t buflen,
52*22ce4affSfengbojiang     unsigned int flags)
53*22ce4affSfengbojiang {
54*22ce4affSfengbojiang 	struct uio auio;
55*22ce4affSfengbojiang 	struct iovec aiov;
56*22ce4affSfengbojiang 	int error;
57*22ce4affSfengbojiang 
58*22ce4affSfengbojiang 	if ((flags & ~GRND_VALIDFLAGS) != 0)
59*22ce4affSfengbojiang 		return (EINVAL);
60*22ce4affSfengbojiang 	if (buflen > IOSIZE_MAX)
61*22ce4affSfengbojiang 		return (EINVAL);
62*22ce4affSfengbojiang 
63*22ce4affSfengbojiang 	/*
64*22ce4affSfengbojiang 	 * Linux compatibility: We have two choices for handling Linux's
65*22ce4affSfengbojiang 	 * GRND_INSECURE.
66*22ce4affSfengbojiang 	 *
67*22ce4affSfengbojiang 	 * 1. We could ignore it completely (like GRND_RANDOM).  However, this
68*22ce4affSfengbojiang 	 * might produce the surprising result of GRND_INSECURE requests
69*22ce4affSfengbojiang 	 * blocking, when the Linux API does not block.
70*22ce4affSfengbojiang 	 *
71*22ce4affSfengbojiang 	 * 2. Alternatively, we could treat GRND_INSECURE requests as requests
72*22ce4affSfengbojiang 	 * for GRND_NONBLOCK.  Here, the surprising result for Linux programs
73*22ce4affSfengbojiang 	 * is that invocations with unseeded random(4) will produce EAGAIN,
74*22ce4affSfengbojiang 	 * rather than garbage.
75*22ce4affSfengbojiang 	 *
76*22ce4affSfengbojiang 	 * Honoring the flag in the way Linux does seems fraught.  If we
77*22ce4affSfengbojiang 	 * actually use the output of a random(4) implementation prior to
78*22ce4affSfengbojiang 	 * seeding, we leak some entropy about the initial seed to attackers.
79*22ce4affSfengbojiang 	 * This seems unacceptable -- it defeats the purpose of blocking on
80*22ce4affSfengbojiang 	 * initial seeding.
81*22ce4affSfengbojiang 	 *
82*22ce4affSfengbojiang 	 * Secondary to that concern, before seeding we may have arbitrarily
83*22ce4affSfengbojiang 	 * little entropy collected; producing output from zero or a handful of
84*22ce4affSfengbojiang 	 * entropy bits does not seem particularly useful to userspace.
85*22ce4affSfengbojiang 	 *
86*22ce4affSfengbojiang 	 * If userspace can accept garbage, insecure non-random bytes, they can
87*22ce4affSfengbojiang 	 * create their own insecure garbage with srandom(time(NULL)) or
88*22ce4affSfengbojiang 	 * similar.  Asking the kernel to produce it from the secure
89*22ce4affSfengbojiang 	 * getrandom(2) API seems inane.
90*22ce4affSfengbojiang 	 *
91*22ce4affSfengbojiang 	 * We elect to emulate GRND_INSECURE as an alternative spelling of
92*22ce4affSfengbojiang 	 * GRND_NONBLOCK (2).
93*22ce4affSfengbojiang 	 */
94*22ce4affSfengbojiang 	if ((flags & GRND_INSECURE) != 0)
95*22ce4affSfengbojiang 		flags |= GRND_NONBLOCK;
96*22ce4affSfengbojiang 
97*22ce4affSfengbojiang 	if (buflen == 0) {
98*22ce4affSfengbojiang 		td->td_retval[0] = 0;
99*22ce4affSfengbojiang 		return (0);
100*22ce4affSfengbojiang 	}
101*22ce4affSfengbojiang 
102*22ce4affSfengbojiang 	aiov.iov_base = user_buf;
103*22ce4affSfengbojiang 	aiov.iov_len = buflen;
104*22ce4affSfengbojiang 	auio.uio_iov = &aiov;
105*22ce4affSfengbojiang 	auio.uio_iovcnt = 1;
106*22ce4affSfengbojiang 	auio.uio_offset = 0;
107*22ce4affSfengbojiang 	auio.uio_resid = buflen;
108*22ce4affSfengbojiang 	auio.uio_segflg = UIO_USERSPACE;
109*22ce4affSfengbojiang 	auio.uio_rw = UIO_READ;
110*22ce4affSfengbojiang 	auio.uio_td = td;
111*22ce4affSfengbojiang 
112*22ce4affSfengbojiang 	error = read_random_uio(&auio, (flags & GRND_NONBLOCK) != 0);
113*22ce4affSfengbojiang 	if (error == 0)
114*22ce4affSfengbojiang 		td->td_retval[0] = buflen - auio.uio_resid;
115*22ce4affSfengbojiang 	return (error);
116*22ce4affSfengbojiang }
117*22ce4affSfengbojiang 
118*22ce4affSfengbojiang #ifndef _SYS_SYSPROTO_H_
119*22ce4affSfengbojiang struct getrandom_args {
120*22ce4affSfengbojiang 	void		*buf;
121*22ce4affSfengbojiang 	size_t		buflen;
122*22ce4affSfengbojiang 	unsigned int	flags;
123*22ce4affSfengbojiang };
124*22ce4affSfengbojiang #endif
125*22ce4affSfengbojiang 
126*22ce4affSfengbojiang int
sys_getrandom(struct thread * td,struct getrandom_args * uap)127*22ce4affSfengbojiang sys_getrandom(struct thread *td, struct getrandom_args *uap)
128*22ce4affSfengbojiang {
129*22ce4affSfengbojiang 	return (kern_getrandom(td, uap->buf, uap->buflen, uap->flags));
130*22ce4affSfengbojiang }
131