1/*- 2 * Copyright (c) 2000-2013 Mark R V Murray 3 * Copyright (c) 2013 Arthur Mesh 4 * Copyright (c) 2004 Robert N. M. Watson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD$"); 32 33#include "opt_random.h" 34 35#include <sys/param.h> 36#include <sys/systm.h> 37#include <sys/eventhandler.h> 38#include <sys/kernel.h> 39#include <sys/kthread.h> 40#include <sys/linker.h> 41#include <sys/lock.h> 42#include <sys/malloc.h> 43#include <sys/mutex.h> 44#include <sys/random.h> 45#include <sys/selinfo.h> 46#include <sys/sysctl.h> 47#include <sys/unistd.h> 48 49#include <machine/cpu.h> 50#include <machine/vmparam.h> 51 52#include <dev/random/randomdev.h> 53#include <dev/random/randomdev_soft.h> 54#include <dev/random/random_adaptors.h> 55#include <dev/random/random_harvestq.h> 56#include <dev/random/live_entropy_sources.h> 57#include <dev/random/rwfile.h> 58 59#define RANDOM_FIFO_MAX 1024 /* How many events to queue up */ 60 61/* 62 * The harvest mutex protects the consistency of the entropy fifos and 63 * empty fifo and other associated structures. 64 */ 65struct mtx harvest_mtx; 66 67/* Lockable FIFO queue holding entropy buffers */ 68struct entropyfifo { 69 int count; 70 STAILQ_HEAD(harvestlist, harvest) head; 71}; 72 73/* Empty entropy buffers */ 74static struct entropyfifo emptyfifo; 75 76/* Harvested entropy */ 77static struct entropyfifo harvestfifo; 78 79/* <0 to end the kthread, 0 to let it run, 1 to flush the harvest queues */ 80int random_kthread_control = 0; 81 82static struct proc *random_kthread_proc; 83 84static event_proc_f random_cb; 85 86#ifdef RANDOM_RWFILE 87static const char *entropy_files[] = { 88 "/entropy", 89 NULL 90}; 91#endif 92 93/* Deal with entropy cached externally if this is present. 94 * Lots of policy may eventually arrive in this function. 95 * Called after / is mounted. 96 */ 97static void 98random_harvestq_cache(void *arg __unused) 99{ 100 uint8_t *keyfile, *data; 101 size_t size, i; 102#ifdef RANDOM_RWFILE 103 const char **entropy_file; 104 uint8_t *zbuf; 105 int error; 106#endif 107 108 /* Get stuff that may have been preloaded by loader(8) */ 109 keyfile = preload_search_by_type("/boot/entropy"); 110 if (keyfile != NULL) { 111 data = preload_fetch_addr(keyfile); 112 size = preload_fetch_size(keyfile); 113 if (data != NULL && size != 0) { 114 for (i = 0; i < size; i += 16) 115 random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED); 116 printf("random: read %zu bytes from preloaded cache\n", size); 117 bzero(data, size); 118 } 119 else 120 printf("random: no preloaded entropy cache available\n"); 121 } 122 123#ifdef RANDOM_RWFILE 124 /* Read and attempt to overwrite the entropy cache files. 125 * If the file exists, can be read and then overwritten, 126 * then use it. Ignore it otherwise, but print out what is 127 * going on. 128 */ 129 data = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK); 130 zbuf = __DECONST(void *, zero_region); 131 for (entropy_file = entropy_files; *entropy_file; entropy_file++) { 132 error = randomdev_read_file(*entropy_file, data, PAGE_SIZE); 133 if (error == 0) { 134 printf("random: entropy cache '%s' provides %ld bytes\n", *entropy_file, (long)PAGE_SIZE); 135 error = randomdev_write_file(*entropy_file, zbuf, PAGE_SIZE); 136 if (error == 0) { 137 printf("random: entropy cache '%s' contents used and successfully overwritten\n", *entropy_file); 138 for (i = 0; i < PAGE_SIZE; i += 16) 139 random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED); 140 } 141 else 142 printf("random: entropy cache '%s' not overwritten and therefore not used; error = %d\n", *entropy_file, error); 143 } 144 else 145 printf("random: entropy cache '%s' not present or unreadable; error = %d\n", *entropy_file, error); 146 } 147 bzero(data, PAGE_SIZE); 148 free(data, M_ENTROPY); 149#endif 150} 151EVENTHANDLER_DEFINE(mountroot, random_harvestq_cache, NULL, 0); 152 153static void 154random_kthread(void *arg) 155{ 156 STAILQ_HEAD(, harvest) local_queue; 157 struct harvest *event = NULL; 158 int local_count; 159 event_proc_f entropy_processor = arg; 160 161 STAILQ_INIT(&local_queue); 162 local_count = 0; 163 164 /* Process until told to stop */ 165 mtx_lock_spin(&harvest_mtx); 166 for (; random_kthread_control >= 0;) { 167 168 /* 169 * Grab all the entropy events. 170 * Drain entropy source records into a thread-local 171 * queue for processing while not holding the mutex. 172 */ 173 STAILQ_CONCAT(&local_queue, &harvestfifo.head); 174 local_count += harvestfifo.count; 175 harvestfifo.count = 0; 176 177 /* 178 * Deal with events, if any. 179 * Then transfer the used events back into the empty fifo. 180 */ 181 if (!STAILQ_EMPTY(&local_queue)) { 182 mtx_unlock_spin(&harvest_mtx); 183 STAILQ_FOREACH(event, &local_queue, next) 184 entropy_processor(event); 185 mtx_lock_spin(&harvest_mtx); 186 STAILQ_CONCAT(&emptyfifo.head, &local_queue); 187 emptyfifo.count += local_count; 188 local_count = 0; 189 } 190 191 KASSERT(local_count == 0, ("random_kthread: local_count %d", 192 local_count)); 193 194 /* 195 * Do only one round of the hardware sources for now. 196 * Later we'll need to make it rate-adaptive. 197 */ 198 mtx_unlock_spin(&harvest_mtx); 199 live_entropy_sources_feed(1, entropy_processor); 200 mtx_lock_spin(&harvest_mtx); 201 202 /* 203 * If a queue flush was commanded, it has now happened, 204 * and we can mark this by resetting the command. 205 */ 206 207 if (random_kthread_control == 1) 208 random_kthread_control = 0; 209 210 /* Work done, so don't belabour the issue */ 211 msleep_spin_sbt(&random_kthread_control, &harvest_mtx, 212 "-", SBT_1S/10, 0, C_PREL(1)); 213 214 } 215 mtx_unlock_spin(&harvest_mtx); 216 217 random_set_wakeup_exit(&random_kthread_control); 218 /* NOTREACHED */ 219} 220 221void 222random_harvestq_init(event_proc_f cb) 223{ 224 int i; 225 struct harvest *np; 226 227 /* Initialise the harvest fifos */ 228 229 /* Contains the currently unused event structs. */ 230 STAILQ_INIT(&emptyfifo.head); 231 for (i = 0; i < RANDOM_FIFO_MAX; i++) { 232 np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK); 233 STAILQ_INSERT_TAIL(&emptyfifo.head, np, next); 234 } 235 emptyfifo.count = RANDOM_FIFO_MAX; 236 237 /* Will contain the queued-up events. */ 238 STAILQ_INIT(&harvestfifo.head); 239 harvestfifo.count = 0; 240 241 mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN); 242 243 random_cb = cb; 244} 245 246static void 247random_harvestq_start_kproc(void *arg __unused) 248{ 249 int error; 250 251 if (random_cb == NULL) 252 return; 253 254 /* Start the hash/reseed thread */ 255 error = kproc_create(random_kthread, random_cb, 256 &random_kthread_proc, RFHIGHPID, 0, "rand_harvestq"); /* RANDOM_CSPRNG_NAME */ 257 258 if (error != 0) 259 panic("Cannot create entropy maintenance thread."); 260} 261SYSINIT(random_kthread, SI_SUB_DRIVERS, SI_ORDER_ANY, 262 random_harvestq_start_kproc, NULL); 263 264void 265random_harvestq_deinit(void) 266{ 267 struct harvest *np; 268 269 /* Destroy the harvest fifos */ 270 while (!STAILQ_EMPTY(&emptyfifo.head)) { 271 np = STAILQ_FIRST(&emptyfifo.head); 272 STAILQ_REMOVE_HEAD(&emptyfifo.head, next); 273 free(np, M_ENTROPY); 274 } 275 emptyfifo.count = 0; 276 while (!STAILQ_EMPTY(&harvestfifo.head)) { 277 np = STAILQ_FIRST(&harvestfifo.head); 278 STAILQ_REMOVE_HEAD(&harvestfifo.head, next); 279 free(np, M_ENTROPY); 280 } 281 harvestfifo.count = 0; 282 283 /* 284 * Command the hash/reseed thread to end and wait for it to finish 285 */ 286 mtx_lock_spin(&harvest_mtx); 287 if (random_kthread_proc != NULL) { 288 random_kthread_control = -1; 289 msleep_spin((void *)&random_kthread_control, &harvest_mtx, 290 "term", 0); 291 } 292 mtx_unlock_spin(&harvest_mtx); 293 294 mtx_destroy(&harvest_mtx); 295} 296 297/* 298 * Entropy harvesting routine. 299 * This is supposed to be fast; do not do anything slow in here! 300 * 301 * It is also illegal (and morally reprehensible) to insert any 302 * high-rate data here. "High-rate" is define as a data source 303 * that will usually cause lots of failures of the "Lockless read" 304 * check a few lines below. This includes the "always-on" sources 305 * like the Intel "rdrand" or the VIA Nehamiah "xstore" sources. 306 */ 307void 308random_harvestq_internal(u_int64_t somecounter, const void *entropy, 309 u_int count, u_int bits, enum esource origin) 310{ 311 struct harvest *event; 312 313 KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, 314 ("random_harvest_internal: origin %d invalid\n", origin)); 315 316 /* Lockless read to avoid lock operations if fifo is full. */ 317 if (harvestfifo.count >= RANDOM_FIFO_MAX) 318 return; 319 320 mtx_lock_spin(&harvest_mtx); 321 322 /* 323 * On't overfill the harvest queue; this could steal all 324 * our memory. 325 */ 326 if (harvestfifo.count < RANDOM_FIFO_MAX) { 327 event = STAILQ_FIRST(&emptyfifo.head); 328 if (event != NULL) { 329 /* Add the harvested data to the fifo */ 330 STAILQ_REMOVE_HEAD(&emptyfifo.head, next); 331 emptyfifo.count--; 332 event->somecounter = somecounter; 333 event->size = count; 334 event->bits = bits; 335 event->source = origin; 336 337 /* XXXX Come back and make this dynamic! */ 338 count = MIN(count, HARVESTSIZE); 339 memcpy(event->entropy, entropy, count); 340 341 STAILQ_INSERT_TAIL(&harvestfifo.head, 342 event, next); 343 harvestfifo.count++; 344 } 345 } 346 347 mtx_unlock_spin(&harvest_mtx); 348} 349