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 #include <dev/random/randomdev.h> 39 40 /* Set up the sysctl root node for the entropy device */ 41 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, 42 "Cryptographically Secure Random Number Generator"); 43 SYSCTL_NODE(_kern_random, OID_AUTO, initial_seeding, CTLFLAG_RW, 0, 44 "Initial seeding control and information"); 45 46 /* 47 * N.B., this is a dangerous default, but it matches the behavior prior to 48 * r346250 (and, say, OpenBSD -- although they get some guaranteed saved 49 * entropy from the prior boot because of their KARL system, on RW media). 50 */ 51 bool random_bypass_before_seeding = true; 52 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO, 53 bypass_before_seeding, CTLFLAG_RDTUN, &random_bypass_before_seeding, 54 0, "If set non-zero, bypass the random device in requests for random " 55 "data when the random device is not yet seeded. This is considered " 56 "dangerous. Ordinarily, the random device will block requests until " 57 "it is seeded by sufficient entropy."); 58 59 /* 60 * This is a read-only diagnostic that reports the combination of the former 61 * tunable and actual bypass. It is intended for programmatic inspection by 62 * userspace administrative utilities after boot. 63 */ 64 bool read_random_bypassed_before_seeding = false; 65 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO, 66 read_random_bypassed_before_seeding, CTLFLAG_RD, 67 &read_random_bypassed_before_seeding, 0, "If non-zero, the random device " 68 "was bypassed because the 'bypass_before_seeding' knob was enabled and a " 69 "request was submitted prior to initial seeding."); 70 71 /* 72 * This is a read-only diagnostic that reports the combination of the former 73 * tunable and actual bypass for arc4random initial seeding. It is intended 74 * for programmatic inspection by userspace administrative utilities after 75 * boot. 76 */ 77 bool arc4random_bypassed_before_seeding = false; 78 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO, 79 arc4random_bypassed_before_seeding, CTLFLAG_RD, 80 &arc4random_bypassed_before_seeding, 0, "If non-zero, the random device " 81 "was bypassed when initially seeding the kernel arc4random(9), because " 82 "the 'bypass_before_seeding' knob was enabled and a request was submitted " 83 "prior to initial seeding."); 84 85 /* 86 * This knob is for users who do not want additional warnings in their logs 87 * because they intend to handle bypass by inspecting the status of the 88 * diagnostic sysctls. 89 */ 90 bool random_bypass_disable_warnings = false; 91 SYSCTL_BOOL(_kern_random_initial_seeding, OID_AUTO, 92 disable_bypass_warnings, CTLFLAG_RDTUN, 93 &random_bypass_disable_warnings, 0, "If non-zero, do not log a warning " 94 "if the 'bypass_before_seeding' knob is enabled and a request is " 95 "submitted prior to initial seeding."); 96 97 MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures"); 98 99 #if defined(RANDOM_LOADABLE) 100 const struct random_algorithm *p_random_alg_context; 101 void (*_read_random)(void *, u_int); 102 int (*_read_random_uio)(struct uio *, bool); 103 bool (*_is_random_seeded)(void); 104 #endif /* defined(RANDOM_LOADABLE) */ 105