randomdev.c (255379) | randomdev.c (256377) |
---|---|
1/*- | 1/*- |
2 * Copyright (c) 2000-2013 Mark R V Murray |
|
2 * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com> | 3 * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com> |
3 * Copyright (c) 2000-2004 Mark R V Murray | |
4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. --- 10 unchanged lines hidden (view full) --- 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29#include <sys/cdefs.h> | 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. --- 10 unchanged lines hidden (view full) --- 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29#include <sys/cdefs.h> |
30__FBSDID("$FreeBSD: head/sys/dev/random/randomdev.c 255379 2013-09-07 22:07:36Z markm $"); | 30__FBSDID("$FreeBSD: head/sys/dev/random/randomdev.c 256377 2013-10-12 12:57:57Z markm $"); |
31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/bus.h> 35#include <sys/conf.h> 36#include <sys/fcntl.h> 37#include <sys/filio.h> 38#include <sys/kernel.h> 39#include <sys/kthread.h> 40#include <sys/lock.h> 41#include <sys/malloc.h> 42#include <sys/module.h> 43#include <sys/mutex.h> 44#include <sys/poll.h> 45#include <sys/priv.h> 46#include <sys/proc.h> | 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/bus.h> 35#include <sys/conf.h> 36#include <sys/fcntl.h> 37#include <sys/filio.h> 38#include <sys/kernel.h> 39#include <sys/kthread.h> 40#include <sys/lock.h> 41#include <sys/malloc.h> 42#include <sys/module.h> 43#include <sys/mutex.h> 44#include <sys/poll.h> 45#include <sys/priv.h> 46#include <sys/proc.h> |
47#include <sys/random.h> |
|
47#include <sys/selinfo.h> 48#include <sys/uio.h> 49#include <sys/unistd.h> 50 51#include <machine/bus.h> 52#include <machine/cpu.h> 53 | 48#include <sys/selinfo.h> 49#include <sys/uio.h> 50#include <sys/unistd.h> 51 52#include <machine/bus.h> 53#include <machine/cpu.h> 54 |
54#include <dev/random/random_adaptors.h> | |
55#include <dev/random/randomdev.h> | 55#include <dev/random/randomdev.h> |
56#include <dev/random/randomdev_soft.h> 57#include <dev/random/random_adaptors.h> 58#include <dev/random/random_harvestq.h> 59#include <dev/random/live_entropy_sources.h> |
|
56 57#define RANDOM_MINOR 0 58 | 60 61#define RANDOM_MINOR 0 62 |
59static d_close_t random_close; | |
60static d_read_t random_read; 61static d_write_t random_write; 62static d_ioctl_t random_ioctl; 63static d_poll_t random_poll; 64 65static struct cdevsw random_cdevsw = { 66 .d_version = D_VERSION, | 63static d_read_t random_read; 64static d_write_t random_write; 65static d_ioctl_t random_ioctl; 66static d_poll_t random_poll; 67 68static struct cdevsw random_cdevsw = { 69 .d_version = D_VERSION, |
67 .d_close = random_close, | |
68 .d_read = random_read, 69 .d_write = random_write, 70 .d_ioctl = random_ioctl, 71 .d_poll = random_poll, 72 .d_name = "random", 73}; 74 | 70 .d_read = random_read, 71 .d_write = random_write, 72 .d_ioctl = random_ioctl, 73 .d_poll = random_poll, 74 .d_name = "random", 75}; 76 |
75static eventhandler_tag attach_tag; 76static int random_inited; 77 | |
78/* For use with make_dev(9)/destroy_dev(9). */ 79static struct cdev *random_dev; 80 81/* ARGSUSED */ 82static int | 77/* For use with make_dev(9)/destroy_dev(9). */ 78static struct cdev *random_dev; 79 80/* ARGSUSED */ 81static int |
83random_close(struct cdev *dev __unused, int flags, int fmt __unused, 84 struct thread *td) 85{ 86 if ((flags & FWRITE) && (priv_check(td, PRIV_RANDOM_RESEED) == 0) 87 && (securelevel_gt(td->td_ucred, 0) == 0)) { 88 (*random_adaptor->reseed)(); 89 random_adaptor->seeded = 1; 90 arc4rand(NULL, 0, 1); /* Reseed arc4random as well. */ 91 } 92 93 return (0); 94} 95 96/* ARGSUSED */ 97static int | |
98random_read(struct cdev *dev __unused, struct uio *uio, int flag) 99{ 100 int c, error = 0; 101 void *random_buf; 102 103 /* Blocking logic */ 104 if (!random_adaptor->seeded) 105 error = (*random_adaptor->block)(flag); 106 107 /* The actual read */ 108 if (!error) { 109 | 82random_read(struct cdev *dev __unused, struct uio *uio, int flag) 83{ 84 int c, error = 0; 85 void *random_buf; 86 87 /* Blocking logic */ 88 if (!random_adaptor->seeded) 89 error = (*random_adaptor->block)(flag); 90 91 /* The actual read */ 92 if (!error) { 93 |
110 random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); | 94 random_buf = (void *)malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK); |
111 112 while (uio->uio_resid > 0 && !error) { 113 c = MIN(uio->uio_resid, PAGE_SIZE); 114 c = (*random_adaptor->read)(random_buf, c); 115 error = uiomove(random_buf, c, uio); 116 } | 95 96 while (uio->uio_resid > 0 && !error) { 97 c = MIN(uio->uio_resid, PAGE_SIZE); 98 c = (*random_adaptor->read)(random_buf, c); 99 error = uiomove(random_buf, c, uio); 100 } |
101 /* Finished reading; let the source know so it can do some 102 * optional housekeeping */ 103 (*random_adaptor->read)(NULL, 0); |
|
117 | 104 |
118 free(random_buf, M_TEMP); | 105 free(random_buf, M_ENTROPY); |
119 120 } 121 122 return (error); 123} 124 125/* ARGSUSED */ 126static int 127random_write(struct cdev *dev __unused, struct uio *uio, int flag __unused) 128{ | 106 107 } 108 109 return (error); 110} 111 112/* ARGSUSED */ 113static int 114random_write(struct cdev *dev __unused, struct uio *uio, int flag __unused) 115{ |
129 int c, error = 0; 130 void *random_buf; | |
131 | 116 |
132 random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); | 117 /* We used to allow this to insert userland entropy. 118 * We don't any more because (1) this so-called entropy 119 * is usually lousy and (b) its vaguely possible to 120 * mess with entropy harvesting by overdoing a write. 121 * Now we just ignore input like /dev/null does. 122 */ 123 uio->uio_resid = 0; |
133 | 124 |
134 while (uio->uio_resid > 0) { 135 c = MIN((int)uio->uio_resid, PAGE_SIZE); 136 error = uiomove(random_buf, c, uio); 137 if (error) 138 break; 139 (*random_adaptor->write)(random_buf, c); 140 } 141 142 free(random_buf, M_TEMP); 143 144 return (error); | 125 return (0); |
145} 146 147/* ARGSUSED */ 148static int 149random_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr __unused, 150 int flags __unused, struct thread *td __unused) 151{ 152 int error = 0; --- 14 unchanged lines hidden (view full) --- 167random_poll(struct cdev *dev __unused, int events, struct thread *td) 168{ 169 int revents = 0; 170 171 if (events & (POLLIN | POLLRDNORM)) { 172 if (random_adaptor->seeded) 173 revents = events & (POLLIN | POLLRDNORM); 174 else | 126} 127 128/* ARGSUSED */ 129static int 130random_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr __unused, 131 int flags __unused, struct thread *td __unused) 132{ 133 int error = 0; --- 14 unchanged lines hidden (view full) --- 148random_poll(struct cdev *dev __unused, int events, struct thread *td) 149{ 150 int revents = 0; 151 152 if (events & (POLLIN | POLLRDNORM)) { 153 if (random_adaptor->seeded) 154 revents = events & (POLLIN | POLLRDNORM); 155 else |
175 revents = (*random_adaptor->poll) (events,td); | 156 revents = (*random_adaptor->poll)(events, td); |
176 } 177 return (revents); 178} 179 180static void 181random_initialize(void *p, struct random_adaptor *s) 182{ | 157 } 158 return (revents); 159} 160 161static void 162random_initialize(void *p, struct random_adaptor *s) 163{ |
164 static int random_inited = 0; 165 |
|
183 if (random_inited) { 184 printf("random: <%s> already initialized\n", 185 random_adaptor->ident); 186 return; 187 } 188 189 random_adaptor = s; 190 191 (s->init)(); 192 193 printf("random: <%s> initialized\n", s->ident); 194 | 166 if (random_inited) { 167 printf("random: <%s> already initialized\n", 168 random_adaptor->ident); 169 return; 170 } 171 172 random_adaptor = s; 173 174 (s->init)(); 175 176 printf("random: <%s> initialized\n", s->ident); 177 |
178 /* Use an appropriately evil mode for those who are concerned 179 * with daemons */ |
|
195 random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw, 196 RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0666, "random"); | 180 random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw, 181 RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0666, "random"); |
197 make_dev_alias(random_dev, "urandom"); /* XXX Deprecated */ | 182 make_dev_alias(random_dev, "urandom"); /* compatibility */ |
198 199 /* mark random(4) as initialized, to avoid being called again */ 200 random_inited = 1; 201} 202 203/* ARGSUSED */ 204static int 205random_modevent(module_t mod __unused, int type, void *data __unused) 206{ | 183 184 /* mark random(4) as initialized, to avoid being called again */ 185 random_inited = 1; 186} 187 188/* ARGSUSED */ 189static int 190random_modevent(module_t mod __unused, int type, void *data __unused) 191{ |
192 static eventhandler_tag attach_tag = NULL; |
|
207 int error = 0; 208 209 switch (type) { 210 case MOD_LOAD: 211 random_adaptor_choose(&random_adaptor); 212 213 if (random_adaptor == NULL) { | 193 int error = 0; 194 195 switch (type) { 196 case MOD_LOAD: 197 random_adaptor_choose(&random_adaptor); 198 199 if (random_adaptor == NULL) { |
214 printf( 215 "random: No random adaptor attached, postponing initialization\n"); | 200 printf("random: No random adaptor attached, " 201 "postponing initialization\n"); |
216 attach_tag = EVENTHANDLER_REGISTER(random_adaptor_attach, 217 random_initialize, NULL, EVENTHANDLER_PRI_ANY); | 202 attach_tag = EVENTHANDLER_REGISTER(random_adaptor_attach, 203 random_initialize, NULL, EVENTHANDLER_PRI_ANY); |
218 } else { | 204 } else |
219 random_initialize(NULL, random_adaptor); | 205 random_initialize(NULL, random_adaptor); |
220 } | |
221 222 break; 223 224 case MOD_UNLOAD: 225 if (random_adaptor != NULL) { 226 (*random_adaptor->deinit)(); 227 destroy_dev(random_dev); 228 } 229 /* Unregister the event handler */ | 206 207 break; 208 209 case MOD_UNLOAD: 210 if (random_adaptor != NULL) { 211 (*random_adaptor->deinit)(); 212 destroy_dev(random_dev); 213 } 214 /* Unregister the event handler */ |
230 if (attach_tag != NULL) { | 215 if (attach_tag != NULL) |
231 EVENTHANDLER_DEREGISTER(random_adaptor_attach, 232 attach_tag); | 216 EVENTHANDLER_DEREGISTER(random_adaptor_attach, 217 attach_tag); |
233 } | |
234 235 break; 236 237 case MOD_SHUTDOWN: 238 break; 239 240 default: 241 error = EOPNOTSUPP; 242 break; 243 244 } 245 return (error); 246} 247 248DEV_MODULE(random, random_modevent, NULL); 249MODULE_VERSION(random, 1); | 218 219 break; 220 221 case MOD_SHUTDOWN: 222 break; 223 224 default: 225 error = EOPNOTSUPP; 226 break; 227 228 } 229 return (error); 230} 231 232DEV_MODULE(random, random_modevent, NULL); 233MODULE_VERSION(random, 1); |