xref: /freebsd/sys/dev/random/random_infra.c (revision 8b238f4126d32df3e70056bc32536b7248ebffa0)
1 /*-
2  * Copyright (c) 2015 Mark R V Murray
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/random.h>
36 #include <sys/sysctl.h>
37 
38 #if defined(RANDOM_LOADABLE)
39 #include <sys/lock.h>
40 #include <sys/sx.h>
41 #endif
42 
43 #include <dev/random/randomdev.h>
44 
45 /* Set up the sysctl root node for the entropy device */
46 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0,
47     "Cryptographically Secure Random Number Generator");
48 SYSCTL_NODE(_kern_random, OID_AUTO, initial_seeding, CTLFLAG_RW, 0,
49     "Initial seeding control and information");
50 
51 /*
52  * N.B., this is a dangerous default, but it matches the behavior prior to
53  * r346250 (and, say, OpenBSD -- although they get some guaranteed saved
54  * entropy from the prior boot because of their KARL system, on RW media).
55  */
56 bool random_bypass_before_seeding = true;
57 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
58     bypass_before_seeding, CTLFLAG_RDTUN, &random_bypass_before_seeding,
59     0, "If set non-zero, bypass the random device in requests for random "
60     "data when the random device is not yet seeded.  This is considered "
61     "dangerous.  Ordinarily, the random device will block requests until "
62     "it is seeded by sufficient entropy.");
63 
64 /*
65  * This is a read-only diagnostic that reports the combination of the former
66  * tunable and actual bypass.  It is intended for programmatic inspection by
67  * userspace administrative utilities after boot.
68  */
69 bool read_random_bypassed_before_seeding = false;
70 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
71     read_random_bypassed_before_seeding, CTLFLAG_RD,
72     &read_random_bypassed_before_seeding, 0, "If non-zero, the random device "
73     "was bypassed because the 'bypass_before_seeding' knob was enabled and a "
74     "request was submitted prior to initial seeding.");
75 
76 /*
77  * This is a read-only diagnostic that reports the combination of the former
78  * tunable and actual bypass for arc4random initial seeding.  It is intended
79  * for programmatic inspection by userspace administrative utilities after
80  * boot.
81  */
82 bool arc4random_bypassed_before_seeding = false;
83 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
84     arc4random_bypassed_before_seeding, CTLFLAG_RD,
85     &arc4random_bypassed_before_seeding, 0, "If non-zero, the random device "
86     "was bypassed when initially seeding the kernel arc4random(9), because "
87     "the 'bypass_before_seeding' knob was enabled and a request was submitted "
88     "prior to initial seeding.");
89 
90 /*
91  * This knob is for users who do not want additional warnings in their logs
92  * because they intend to handle bypass by inspecting the status of the
93  * diagnostic sysctls.
94  */
95 bool random_bypass_disable_warnings = false;
96 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO,
97     disable_bypass_warnings, CTLFLAG_RDTUN,
98     &random_bypass_disable_warnings, 0, "If non-zero, do not log a warning "
99     "if the 'bypass_before_seeding' knob is enabled and a request is "
100     "submitted prior to initial seeding.");
101 
102 MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures");
103 
104 #if defined(RANDOM_LOADABLE)
105 struct random_algorithm *p_random_alg_context = NULL;
106 #else /* !defined(RANDOM_LOADABLE) */
107 struct random_algorithm *p_random_alg_context = &random_alg_context;
108 #endif /* defined(RANDOM_LOADABLE) */
109 
110 #if defined(RANDOM_LOADABLE)
111 
112 static void
113 null_read_random(void *dummy __unused, u_int dummy2 __unused)
114 {
115 	panic("%s: no random module is loaded", __func__);
116 }
117 
118 static bool
119 null_is_random_seeded(void)
120 {
121 	return (false);
122 }
123 
124 struct random_readers {
125 	int	(*read_random_uio)(struct uio *, bool);
126 	void	(*read_random)(void *, u_int);
127 	bool	(*is_random_seeded)(void);
128 } random_reader_context = {
129 	(int (*)(struct uio *, bool))nullop,
130 	null_read_random,
131 	null_is_random_seeded,
132 };
133 
134 struct sx randomdev_config_lock;
135 
136 static void
137 random_infra_sysinit(void *dummy __unused)
138 {
139 
140 	RANDOM_CONFIG_INIT_LOCK();
141 }
142 SYSINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_FIRST, random_infra_sysinit, NULL);
143 
144 void
145 random_infra_init(int (*p_random_read_uio)(struct uio *, bool),
146     void (*p_random_read)(void *, u_int),
147     bool (*p_is_random_seeded)(void))
148 {
149 
150 	RANDOM_CONFIG_X_LOCK();
151 	random_reader_context.read_random_uio = p_random_read_uio;
152 	random_reader_context.read_random = p_random_read;
153 	random_reader_context.is_random_seeded = p_is_random_seeded;
154 	RANDOM_CONFIG_X_UNLOCK();
155 }
156 
157 void
158 random_infra_uninit(void)
159 {
160 
161 	RANDOM_CONFIG_X_LOCK();
162 	random_reader_context.read_random_uio = (int (*)(struct uio *, bool))nullop;
163 	random_reader_context.read_random = null_read_random;
164 	random_reader_context.is_random_seeded = null_is_random_seeded;
165 	RANDOM_CONFIG_X_UNLOCK();
166 }
167 
168 static void
169 random_infra_sysuninit(void *dummy __unused)
170 {
171 
172 	RANDOM_CONFIG_DEINIT_LOCK();
173 }
174 SYSUNINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_FIRST, random_infra_sysuninit, NULL);
175 
176 int
177 read_random_uio(struct uio *uio, bool nonblock)
178 {
179 	int retval;
180 
181 	RANDOM_CONFIG_S_LOCK();
182 	retval = random_reader_context.read_random_uio(uio, nonblock);
183 	RANDOM_CONFIG_S_UNLOCK();
184 	return (retval);
185 }
186 
187 void
188 read_random(void *buf, u_int len)
189 {
190 
191 	RANDOM_CONFIG_S_LOCK();
192 	random_reader_context.read_random(buf, len);
193 	RANDOM_CONFIG_S_UNLOCK();
194 }
195 
196 bool
197 is_random_seeded(void)
198 {
199 	bool result;
200 
201 	RANDOM_CONFIG_S_LOCK();
202 	result = random_reader_context.is_random_seeded();
203 	RANDOM_CONFIG_S_UNLOCK();
204 	return (result);
205 }
206 
207 
208 #endif /* defined(RANDOM_LOADABLE) */
209