blob: 5a85009c689f9735cf6d226c408f9b664200e709 [file] [log] [blame]
Damien Miller040f3832000-04-03 14:50:43 +10001/*
2 * Copyright (c) 2000 Damien Miller. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Miller040f3832000-04-03 14:50:43 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
26
Damien Miller5f056372000-04-16 12:31:48 +100027#include <openssl/rand.h>
28#include <openssl/sha.h>
Damien Miller040f3832000-04-03 14:50:43 +100029
Damien Millerecbb26d2000-07-15 14:59:14 +100030/* SunOS 4.4.4 needs this */
31#ifdef HAVE_FLOATINGPOINT_H
32# include <floatingpoint.h>
33#endif /* HAVE_FLOATINGPOINT_H */
34
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "ssh.h"
Damien Millera1072a82001-02-18 15:28:11 +110036#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "xmalloc.h"
38#include "atomicio.h"
Ben Lindstromcb577332001-01-22 21:06:19 +000039#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "log.h"
41
Kevin Steves4679f5b2001-02-18 11:34:32 +000042RCSID("$Id: entropy.c,v 1.29 2001/02/18 11:34:32 stevesk Exp $");
Damien Miller040f3832000-04-03 14:50:43 +100043
Damien Miller040f3832000-04-03 14:50:43 +100044#ifndef offsetof
45# define offsetof(type, member) ((size_t) &((type *)0)->member)
46#endif
Damien Miller14c12cb2000-06-07 22:20:23 +100047
Damien Miller14c12cb2000-06-07 22:20:23 +100048/* Number of times to pass through command list gathering entropy */
49#define NUM_ENTROPY_RUNS 1
50
51/* Scale entropy estimates back by this amount on subsequent runs */
52#define SCALE_PER_RUN 10.0
53
54/* Minimum number of commands to be considered valid */
55#define MIN_ENTROPY_SOURCES 16
56
57#define WHITESPACE " \t\n"
58
Damien Miller7b22d652000-06-18 14:07:04 +100059#ifndef RUSAGE_SELF
60# define RUSAGE_SELF 0
61#endif
62#ifndef RUSAGE_CHILDREN
63# define RUSAGE_CHILDREN 0
64#endif
65
Damien Miller14c12cb2000-06-07 22:20:23 +100066#if defined(EGD_SOCKET) || defined(RANDOM_POOL)
67
68#ifdef EGD_SOCKET
Damien Miller040f3832000-04-03 14:50:43 +100069/* Collect entropy from EGD */
Damien Miller64681252000-06-26 13:01:33 +100070int get_random_bytes(unsigned char *buf, int len)
Damien Miller040f3832000-04-03 14:50:43 +100071{
Damien Miller64681252000-06-26 13:01:33 +100072 int fd;
73 char msg[2];
Damien Miller040f3832000-04-03 14:50:43 +100074 struct sockaddr_un addr;
Damien Millerb3ffc5f2001-02-18 12:44:29 +110075 int addr_len, rval, errors;
Damien Millera1072a82001-02-18 15:28:11 +110076 mysig_t old_sigpipe;
Damien Miller040f3832000-04-03 14:50:43 +100077
Damien Miller64681252000-06-26 13:01:33 +100078 /* Sanity checks */
Damien Miller040f3832000-04-03 14:50:43 +100079 if (sizeof(EGD_SOCKET) > sizeof(addr.sun_path))
80 fatal("Random pool path is too long");
Damien Miller040f3832000-04-03 14:50:43 +100081 if (len > 255)
82 fatal("Too many bytes to read from EGD");
Damien Miller64681252000-06-26 13:01:33 +100083
84 memset(&addr, '\0', sizeof(addr));
85 addr.sun_family = AF_UNIX;
86 strlcpy(addr.sun_path, EGD_SOCKET, sizeof(addr.sun_path));
87 addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(EGD_SOCKET);
Kevin Stevesef4eea92001-02-05 12:42:17 +000088
Damien Millera1072a82001-02-18 15:28:11 +110089 old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
Damien Millerb3ffc5f2001-02-18 12:44:29 +110090
91 errors = rval = 0;
92reopen:
Damien Miller64681252000-06-26 13:01:33 +100093 fd = socket(AF_UNIX, SOCK_STREAM, 0);
94 if (fd == -1) {
95 error("Couldn't create AF_UNIX socket: %s", strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +110096 goto done;
Damien Miller64681252000-06-26 13:01:33 +100097 }
98
99 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000100 error("Couldn't connect to EGD socket \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000101 addr.sun_path, strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100102 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000103 }
104
Damien Miller040f3832000-04-03 14:50:43 +1000105 /* Send blocking read request to EGD */
Damien Miller64681252000-06-26 13:01:33 +1000106 msg[0] = 0x02;
107 msg[1] = len;
Damien Miller040f3832000-04-03 14:50:43 +1000108
Damien Miller64681252000-06-26 13:01:33 +1000109 if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100110 if (errno == EPIPE && errors < 10) {
111 close(fd);
112 errors++;
113 goto reopen;
114 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000115 error("Couldn't write to EGD socket \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000116 EGD_SOCKET, strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100117 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000118 }
Damien Miller040f3832000-04-03 14:50:43 +1000119
Damien Miller64681252000-06-26 13:01:33 +1000120 if (atomicio(read, fd, buf, len) != len) {
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100121 if (errno == EPIPE && errors < 10) {
122 close(fd);
123 errors++;
124 goto reopen;
125 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000126 error("Couldn't read from EGD socket \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000127 EGD_SOCKET, strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100128 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000129 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000130
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100131 rval = 1;
132done:
Kevin Steves4679f5b2001-02-18 11:34:32 +0000133 mysignal(SIGPIPE, old_sigpipe);
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100134 if (fd != -1)
135 close(fd);
136 return(rval);
Damien Miller040f3832000-04-03 14:50:43 +1000137}
138#else /* !EGD_SOCKET */
139#ifdef RANDOM_POOL
140/* Collect entropy from /dev/urandom or pipe */
Damien Miller64681252000-06-26 13:01:33 +1000141int get_random_bytes(unsigned char *buf, int len)
Damien Miller040f3832000-04-03 14:50:43 +1000142{
Damien Miller64681252000-06-26 13:01:33 +1000143 int random_pool;
Damien Miller040f3832000-04-03 14:50:43 +1000144
Damien Miller64681252000-06-26 13:01:33 +1000145 random_pool = open(RANDOM_POOL, O_RDONLY);
Damien Miller040f3832000-04-03 14:50:43 +1000146 if (random_pool == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000147 error("Couldn't open random pool \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000148 RANDOM_POOL, strerror(errno));
149 return(0);
Damien Miller040f3832000-04-03 14:50:43 +1000150 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000151
Damien Miller64681252000-06-26 13:01:33 +1000152 if (atomicio(read, random_pool, buf, len) != len) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000153 error("Couldn't read from random pool \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000154 RANDOM_POOL, strerror(errno));
155 close(random_pool);
156 return(0);
157 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000158
Damien Miller64681252000-06-26 13:01:33 +1000159 close(random_pool);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000160
Damien Miller64681252000-06-26 13:01:33 +1000161 return(1);
Damien Miller040f3832000-04-03 14:50:43 +1000162}
163#endif /* RANDOM_POOL */
164#endif /* EGD_SOCKET */
165
Damien Miller14c12cb2000-06-07 22:20:23 +1000166/*
167 * Seed OpenSSL's random number pool from Kernel random number generator
168 * or EGD
169 */
170void
171seed_rng(void)
172{
173 char buf[32];
Kevin Stevesef4eea92001-02-05 12:42:17 +0000174
Damien Miller14c12cb2000-06-07 22:20:23 +1000175 debug("Seeding random number generator");
Damien Miller64681252000-06-26 13:01:33 +1000176
Damien Miller08006472000-06-26 13:55:31 +1000177 if (!get_random_bytes(buf, sizeof(buf))) {
178 if (!RAND_status())
179 fatal("Entropy collection failed and entropy exhausted");
180 } else {
181 RAND_add(buf, sizeof(buf), sizeof(buf));
182 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000183
Damien Miller14c12cb2000-06-07 22:20:23 +1000184 memset(buf, '\0', sizeof(buf));
185}
186
Damien Millerf9b625c2000-07-09 22:42:32 +1000187/* No-op */
188void init_rng(void) {}
189
Damien Miller14c12cb2000-06-07 22:20:23 +1000190#else /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
191
Kevin Stevesef4eea92001-02-05 12:42:17 +0000192/*
Damien Miller040f3832000-04-03 14:50:43 +1000193 * FIXME: proper entropy estimations. All current values are guesses
Damien Miller4018c192000-04-30 09:30:44 +1000194 * FIXME: (ATL) do estimates at compile time?
Damien Miller040f3832000-04-03 14:50:43 +1000195 * FIXME: More entropy sources
196 */
197
Damien Miller4018c192000-04-30 09:30:44 +1000198/* slow command timeouts (all in milliseconds) */
199/* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */
200static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC;
201
Damien Miller0437b332000-05-02 09:56:41 +1000202static int prng_seed_saved = 0;
Damien Millerf9b625c2000-07-09 22:42:32 +1000203static int prng_initialised = 0;
204uid_t original_uid;
Damien Miller040f3832000-04-03 14:50:43 +1000205
206typedef struct
207{
208 /* Proportion of data that is entropy */
209 double rate;
Damien Miller4018c192000-04-30 09:30:44 +1000210 /* Counter goes positive if this command times out */
211 unsigned int badness;
212 /* Increases by factor of two each timeout */
213 unsigned int sticky_badness;
Damien Miller040f3832000-04-03 14:50:43 +1000214 /* Path to executable */
Damien Miller0437b332000-05-02 09:56:41 +1000215 char *path;
Damien Miller040f3832000-04-03 14:50:43 +1000216 /* argv to pass to executable */
Damien Miller0437b332000-05-02 09:56:41 +1000217 char *args[5];
Damien Miller8d1fd572000-05-17 21:34:07 +1000218 /* full command string (debug) */
219 char *cmdstring;
Damien Miller040f3832000-04-03 14:50:43 +1000220} entropy_source_t;
221
Damien Miller4018c192000-04-30 09:30:44 +1000222double stir_from_system(void);
223double stir_from_programs(void);
224double stir_gettimeofday(double entropy_estimate);
225double stir_clock(double entropy_estimate);
226double stir_rusage(int who, double entropy_estimate);
227double hash_output_from_command(entropy_source_t *src, char *hash);
228
Damien Miller0437b332000-05-02 09:56:41 +1000229/* this is initialised from a file, by prng_read_commands() */
230entropy_source_t *entropy_sources = NULL;
Damien Miller040f3832000-04-03 14:50:43 +1000231
Kevin Stevesef4eea92001-02-05 12:42:17 +0000232double
Damien Miller040f3832000-04-03 14:50:43 +1000233stir_from_system(void)
234{
235 double total_entropy_estimate;
236 long int i;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000237
Damien Miller040f3832000-04-03 14:50:43 +1000238 total_entropy_estimate = 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000239
Damien Miller040f3832000-04-03 14:50:43 +1000240 i = getpid();
Damien Miller7b22d652000-06-18 14:07:04 +1000241 RAND_add(&i, sizeof(i), 0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000242 total_entropy_estimate += 0.1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000243
Damien Miller040f3832000-04-03 14:50:43 +1000244 i = getppid();
Damien Miller7b22d652000-06-18 14:07:04 +1000245 RAND_add(&i, sizeof(i), 0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000246 total_entropy_estimate += 0.1;
247
248 i = getuid();
249 RAND_add(&i, sizeof(i), 0.0);
250 i = getgid();
251 RAND_add(&i, sizeof(i), 0.0);
252
253 total_entropy_estimate += stir_gettimeofday(1.0);
Damien Miller7b22d652000-06-18 14:07:04 +1000254 total_entropy_estimate += stir_clock(0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000255 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0);
256
257 return(total_entropy_estimate);
258}
259
Kevin Stevesef4eea92001-02-05 12:42:17 +0000260double
Damien Miller040f3832000-04-03 14:50:43 +1000261stir_from_programs(void)
262{
263 int i;
264 int c;
265 double entropy_estimate;
266 double total_entropy_estimate;
267 char hash[SHA_DIGEST_LENGTH];
268
Damien Miller040f3832000-04-03 14:50:43 +1000269 total_entropy_estimate = 0;
Damien Miller14c12cb2000-06-07 22:20:23 +1000270 for(i = 0; i < NUM_ENTROPY_RUNS; i++) {
Damien Miller040f3832000-04-03 14:50:43 +1000271 c = 0;
272 while (entropy_sources[c].path != NULL) {
Damien Miller040f3832000-04-03 14:50:43 +1000273
Damien Miller4018c192000-04-30 09:30:44 +1000274 if (!entropy_sources[c].badness) {
275 /* Hash output from command */
276 entropy_estimate = hash_output_from_command(&entropy_sources[c], hash);
277
278 /* Scale back entropy estimate according to command's rate */
279 entropy_estimate *= entropy_sources[c].rate;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000280
Damien Miller4018c192000-04-30 09:30:44 +1000281 /* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */
282 if (entropy_estimate > SHA_DIGEST_LENGTH)
283 entropy_estimate = SHA_DIGEST_LENGTH;
Damien Miller040f3832000-04-03 14:50:43 +1000284
Kevin Stevesef4eea92001-02-05 12:42:17 +0000285 /* Scale back estimates for subsequent passes through list */
Damien Miller14c12cb2000-06-07 22:20:23 +1000286 entropy_estimate /= SCALE_PER_RUN * (i + 1.0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000287
Damien Miller4018c192000-04-30 09:30:44 +1000288 /* Stir it in */
289 RAND_add(hash, sizeof(hash), entropy_estimate);
Damien Miller040f3832000-04-03 14:50:43 +1000290
Kevin Stevesef4eea92001-02-05 12:42:17 +0000291 debug3("Got %0.2f bytes of entropy from '%s'", entropy_estimate,
Damien Miller8d1fd572000-05-17 21:34:07 +1000292 entropy_sources[c].cmdstring);
Damien Miller040f3832000-04-03 14:50:43 +1000293
Damien Miller4018c192000-04-30 09:30:44 +1000294 total_entropy_estimate += entropy_estimate;
Damien Miller040f3832000-04-03 14:50:43 +1000295
296 /* Execution times should be a little unpredictable */
Damien Miller4018c192000-04-30 09:30:44 +1000297 total_entropy_estimate += stir_gettimeofday(0.05);
298 total_entropy_estimate += stir_clock(0.05);
299 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1);
300 total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1);
301 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +1100302 debug2("Command '%s' disabled (badness %d)",
Damien Miller8d1fd572000-05-17 21:34:07 +1000303 entropy_sources[c].cmdstring, entropy_sources[c].badness);
Damien Miller4018c192000-04-30 09:30:44 +1000304
305 if (entropy_sources[c].badness > 0)
306 entropy_sources[c].badness--;
307 }
308
Damien Miller040f3832000-04-03 14:50:43 +1000309 c++;
310 }
311 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000312
Damien Miller040f3832000-04-03 14:50:43 +1000313 return(total_entropy_estimate);
314}
315
316double
317stir_gettimeofday(double entropy_estimate)
318{
319 struct timeval tv;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000320
Damien Miller040f3832000-04-03 14:50:43 +1000321 if (gettimeofday(&tv, NULL) == -1)
322 fatal("Couldn't gettimeofday: %s", strerror(errno));
323
324 RAND_add(&tv, sizeof(tv), entropy_estimate);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000325
Damien Miller040f3832000-04-03 14:50:43 +1000326 return(entropy_estimate);
327}
328
329double
330stir_clock(double entropy_estimate)
331{
332#ifdef HAVE_CLOCK
333 clock_t c;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000334
Damien Miller040f3832000-04-03 14:50:43 +1000335 c = clock();
336 RAND_add(&c, sizeof(c), entropy_estimate);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000337
Damien Miller040f3832000-04-03 14:50:43 +1000338 return(entropy_estimate);
339#else /* _HAVE_CLOCK */
340 return(0);
341#endif /* _HAVE_CLOCK */
342}
343
344double
345stir_rusage(int who, double entropy_estimate)
346{
347#ifdef HAVE_GETRUSAGE
348 struct rusage ru;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000349
Damien Miller4018c192000-04-30 09:30:44 +1000350 if (getrusage(who, &ru) == -1)
Damien Miller7b22d652000-06-18 14:07:04 +1000351 return(0);
Damien Miller040f3832000-04-03 14:50:43 +1000352
Damien Miller7b22d652000-06-18 14:07:04 +1000353 RAND_add(&ru, sizeof(ru), entropy_estimate);
Damien Miller040f3832000-04-03 14:50:43 +1000354
355 return(entropy_estimate);
356#else /* _HAVE_GETRUSAGE */
357 return(0);
358#endif /* _HAVE_GETRUSAGE */
359}
360
Damien Miller8d1fd572000-05-17 21:34:07 +1000361
362static
363int
364_get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) {
365 int secdiff, usecdiff;
366
367 secdiff = t2->tv_sec - t1->tv_sec;
368 usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec);
369 return (int)(usecdiff / 1000);
370}
371
Damien Miller040f3832000-04-03 14:50:43 +1000372double
Damien Miller4018c192000-04-30 09:30:44 +1000373hash_output_from_command(entropy_source_t *src, char *hash)
Damien Miller040f3832000-04-03 14:50:43 +1000374{
375 static int devnull = -1;
376 int p[2];
Damien Miller4018c192000-04-30 09:30:44 +1000377 fd_set rdset;
378 int cmd_eof = 0, error_abort = 0;
Damien Miller8d1fd572000-05-17 21:34:07 +1000379 struct timeval tv_start, tv_current;
380 int msec_elapsed = 0;
Damien Miller040f3832000-04-03 14:50:43 +1000381 pid_t pid;
382 int status;
Damien Miller8d1fd572000-05-17 21:34:07 +1000383 char buf[16384];
Damien Miller040f3832000-04-03 14:50:43 +1000384 int bytes_read;
385 int total_bytes_read;
386 SHA_CTX sha;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000387
Damien Millercb5e44a2000-09-29 12:12:36 +1100388 debug3("Reading output from \'%s\'", src->cmdstring);
389
Damien Miller040f3832000-04-03 14:50:43 +1000390 if (devnull == -1) {
391 devnull = open("/dev/null", O_RDWR);
392 if (devnull == -1)
393 fatal("Couldn't open /dev/null: %s", strerror(errno));
394 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000395
Damien Miller040f3832000-04-03 14:50:43 +1000396 if (pipe(p) == -1)
397 fatal("Couldn't open pipe: %s", strerror(errno));
398
Damien Miller8d1fd572000-05-17 21:34:07 +1000399 (void)gettimeofday(&tv_start, NULL); /* record start time */
400
Damien Miller040f3832000-04-03 14:50:43 +1000401 switch (pid = fork()) {
402 case -1: /* Error */
403 close(p[0]);
404 close(p[1]);
405 fatal("Couldn't fork: %s", strerror(errno));
406 /* NOTREACHED */
407 case 0: /* Child */
Damien Miller4018c192000-04-30 09:30:44 +1000408 dup2(devnull, STDIN_FILENO);
409 dup2(p[1], STDOUT_FILENO);
410 dup2(p[1], STDERR_FILENO);
Damien Miller040f3832000-04-03 14:50:43 +1000411 close(p[0]);
412 close(p[1]);
413 close(devnull);
414
Damien Millerf9b625c2000-07-09 22:42:32 +1000415 setuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000416 execv(src->path, (char**)(src->args));
Damien Miller8d1fd572000-05-17 21:34:07 +1000417 debug("(child) Couldn't exec '%s': %s", src->cmdstring,
418 strerror(errno));
Damien Miller040f3832000-04-03 14:50:43 +1000419 _exit(-1);
420 default: /* Parent */
421 break;
422 }
423
424 RAND_add(&pid, sizeof(&pid), 0.0);
425
426 close(p[1]);
427
428 /* Hash output from child */
429 SHA1_Init(&sha);
430 total_bytes_read = 0;
Damien Miller4018c192000-04-30 09:30:44 +1000431
432 while (!error_abort && !cmd_eof) {
433 int ret;
434 struct timeval tv;
Damien Miller8d1fd572000-05-17 21:34:07 +1000435 int msec_remaining;
436
437 (void) gettimeofday(&tv_current, 0);
Damien Miller14c12cb2000-06-07 22:20:23 +1000438 msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current);
Damien Miller8d1fd572000-05-17 21:34:07 +1000439 if (msec_elapsed >= entropy_timeout_current) {
440 error_abort=1;
441 continue;
442 }
443 msec_remaining = entropy_timeout_current - msec_elapsed;
Damien Miller4018c192000-04-30 09:30:44 +1000444
445 FD_ZERO(&rdset);
446 FD_SET(p[0], &rdset);
Damien Miller8d1fd572000-05-17 21:34:07 +1000447 tv.tv_sec = msec_remaining / 1000;
448 tv.tv_usec = (msec_remaining % 1000) * 1000;
Damien Miller4018c192000-04-30 09:30:44 +1000449
450 ret = select(p[0]+1, &rdset, NULL, NULL, &tv);
Damien Miller8d1fd572000-05-17 21:34:07 +1000451
Damien Millerf9b625c2000-07-09 22:42:32 +1000452 RAND_add(&tv, sizeof(tv), 0.0);
453
Damien Miller4018c192000-04-30 09:30:44 +1000454 switch (ret) {
455 case 0:
456 /* timer expired */
457 error_abort = 1;
458 break;
Damien Miller4018c192000-04-30 09:30:44 +1000459 case 1:
460 /* command input */
461 bytes_read = read(p[0], buf, sizeof(buf));
Damien Millerf9b625c2000-07-09 22:42:32 +1000462 RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
Damien Miller4018c192000-04-30 09:30:44 +1000463 if (bytes_read == -1) {
464 error_abort = 1;
465 break;
Damien Millerf9b625c2000-07-09 22:42:32 +1000466 } else if (bytes_read) {
Damien Miller8d1fd572000-05-17 21:34:07 +1000467 SHA1_Update(&sha, buf, bytes_read);
468 total_bytes_read += bytes_read;
Damien Millerf9b625c2000-07-09 22:42:32 +1000469 } else {
Damien Miller8d1fd572000-05-17 21:34:07 +1000470 cmd_eof = 1;
Damien Millerf9b625c2000-07-09 22:42:32 +1000471 }
Damien Miller4018c192000-04-30 09:30:44 +1000472 break;
Damien Miller4018c192000-04-30 09:30:44 +1000473 case -1:
474 default:
Damien Millerf9b625c2000-07-09 22:42:32 +1000475 /* error */
Damien Miller8d1fd572000-05-17 21:34:07 +1000476 debug("Command '%s': select() failed: %s", src->cmdstring,
477 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000478 error_abort = 1;
479 break;
Damien Millerf9b625c2000-07-09 22:42:32 +1000480 }
481 }
Damien Miller4018c192000-04-30 09:30:44 +1000482
Damien Miller040f3832000-04-03 14:50:43 +1000483 SHA1_Final(hash, &sha);
484
485 close(p[0]);
Damien Miller8d1fd572000-05-17 21:34:07 +1000486
Damien Millercb5e44a2000-09-29 12:12:36 +1100487 debug3("Time elapsed: %d msec", msec_elapsed);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000488
Damien Miller040f3832000-04-03 14:50:43 +1000489 if (waitpid(pid, &status, 0) == -1) {
Damien Millercb5e44a2000-09-29 12:12:36 +1100490 error("Couldn't wait for child '%s' completion: %s", src->cmdstring,
Damien Miller8d1fd572000-05-17 21:34:07 +1000491 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000492 return(0.0);
Damien Miller040f3832000-04-03 14:50:43 +1000493 }
494
495 RAND_add(&status, sizeof(&status), 0.0);
496
Damien Miller4018c192000-04-30 09:30:44 +1000497 if (error_abort) {
498 /* closing p[0] on timeout causes the entropy command to
499 * SIGPIPE. Take whatever output we got, and mark this command
500 * as slow */
Damien Millercb5e44a2000-09-29 12:12:36 +1100501 debug2("Command '%s' timed out", src->cmdstring);
Damien Miller4018c192000-04-30 09:30:44 +1000502 src->sticky_badness *= 2;
503 src->badness = src->sticky_badness;
Damien Miller040f3832000-04-03 14:50:43 +1000504 return(total_bytes_read);
Damien Miller4018c192000-04-30 09:30:44 +1000505 }
506
507 if (WIFEXITED(status)) {
508 if (WEXITSTATUS(status)==0) {
509 return(total_bytes_read);
510 } else {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000511 debug2("Command '%s' exit status was %d", src->cmdstring,
Damien Miller14c12cb2000-06-07 22:20:23 +1000512 WEXITSTATUS(status));
Damien Miller4018c192000-04-30 09:30:44 +1000513 src->badness = src->sticky_badness = 128;
514 return (0.0);
515 }
516 } else if (WIFSIGNALED(status)) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000517 debug2("Command '%s' returned on uncaught signal %d !", src->cmdstring,
Damien Miller14c12cb2000-06-07 22:20:23 +1000518 status);
Damien Miller4018c192000-04-30 09:30:44 +1000519 src->badness = src->sticky_badness = 128;
520 return(0.0);
521 } else
522 return(0.0);
Damien Miller040f3832000-04-03 14:50:43 +1000523}
Damien Miller4018c192000-04-30 09:30:44 +1000524
525/*
526 * prng seedfile functions
527 */
528int
529prng_check_seedfile(char *filename) {
530
531 struct stat st;
532
533 /* FIXME raceable: eg replace seed between this stat and subsequent open */
534 /* Not such a problem because we don't trust the seed file anyway */
535 if (lstat(filename, &st) == -1) {
Damien Miller52dc96b2000-10-16 20:13:43 +1100536 /* Give up on hard errors */
Damien Miller4018c192000-04-30 09:30:44 +1000537 if (errno != ENOENT)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000538 debug("WARNING: Couldn't stat random seed file \"%s\": %s",
Damien Miller52dc96b2000-10-16 20:13:43 +1100539 filename, strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000540
541 return(0);
542 }
543
544 /* regular file? */
545 if (!S_ISREG(st.st_mode))
546 fatal("PRNG seedfile %.100s is not a regular file", filename);
547
548 /* mode 0600, owned by root or the current user? */
Damien Miller52dc96b2000-10-16 20:13:43 +1100549 if (((st.st_mode & 0177) != 0) || !(st.st_uid == original_uid)) {
550 debug("WARNING: PRNG seedfile %.100s must be mode 0600, owned by uid %d",
Damien Miller4018c192000-04-30 09:30:44 +1000551 filename, getuid());
Damien Miller52dc96b2000-10-16 20:13:43 +1100552 return(0);
553 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000554
Damien Miller4018c192000-04-30 09:30:44 +1000555 return(1);
556}
557
558void
559prng_write_seedfile(void) {
560 int fd;
561 char seed[1024];
562 char filename[1024];
563 struct passwd *pw;
564
565 /* Don't bother if we have already saved a seed */
566 if (prng_seed_saved)
567 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000568
Damien Millerf9b625c2000-07-09 22:42:32 +1000569 setuid(original_uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000570
Damien Millerfc0b11b2000-05-02 00:03:55 +1000571 prng_seed_saved = 1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000572
Damien Millerf9b625c2000-07-09 22:42:32 +1000573 pw = getpwuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000574 if (pw == NULL)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000575 fatal("Couldn't get password entry for current user (%i): %s",
Damien Millerf9b625c2000-07-09 22:42:32 +1000576 original_uid, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000577
Damien Miller4018c192000-04-30 09:30:44 +1000578 /* Try to ensure that the parent directory is there */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000579 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Ben Lindstromcb577332001-01-22 21:06:19 +0000580 _PATH_SSH_USER_DIR);
Damien Miller4018c192000-04-30 09:30:44 +1000581 mkdir(filename, 0700);
582
Kevin Stevesef4eea92001-02-05 12:42:17 +0000583 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Damien Miller4018c192000-04-30 09:30:44 +1000584 SSH_PRNG_SEED_FILE);
585
586 debug("writing PRNG seed to file %.100s", filename);
587
588 RAND_bytes(seed, sizeof(seed));
589
590 /* Don't care if the seed doesn't exist */
591 prng_check_seedfile(filename);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000592
Damien Miller52dc96b2000-10-16 20:13:43 +1100593 if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000594 debug("WARNING: couldn't access PRNG seedfile %.100s (%.100s)",
Damien Miller52dc96b2000-10-16 20:13:43 +1100595 filename, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000596 } else {
Damien Miller52dc96b2000-10-16 20:13:43 +1100597 if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000598 fatal("problem writing PRNG seedfile %.100s (%.100s)", filename,
Damien Miller52dc96b2000-10-16 20:13:43 +1100599 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000600
Damien Miller52dc96b2000-10-16 20:13:43 +1100601 close(fd);
602 }
Damien Miller4018c192000-04-30 09:30:44 +1000603}
604
605void
606prng_read_seedfile(void) {
607 int fd;
608 char seed[1024];
609 char filename[1024];
610 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000611
Damien Millerf9b625c2000-07-09 22:42:32 +1000612 pw = getpwuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000613 if (pw == NULL)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000614 fatal("Couldn't get password entry for current user (%i): %s",
Damien Millerf9b625c2000-07-09 22:42:32 +1000615 original_uid, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000616
617 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Damien Miller4018c192000-04-30 09:30:44 +1000618 SSH_PRNG_SEED_FILE);
619
620 debug("loading PRNG seed from file %.100s", filename);
621
622 if (!prng_check_seedfile(filename)) {
Damien Miller21de4502001-01-17 09:37:15 +1100623 verbose("Random seed file not found or not valid, ignoring.");
Damien Miller4018c192000-04-30 09:30:44 +1000624 return;
625 }
626
627 /* open the file and read in the seed */
628 fd = open(filename, O_RDONLY);
629 if (fd == -1)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000630 fatal("could not open PRNG seedfile %.100s (%.100s)", filename,
Damien Miller4018c192000-04-30 09:30:44 +1000631 strerror(errno));
632
633 if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) {
634 verbose("invalid or short read from PRNG seedfile %.100s - ignoring",
635 filename);
636 memset(seed, '\0', sizeof(seed));
637 }
638 close(fd);
639
640 /* stir in the seed, with estimated entropy zero */
641 RAND_add(&seed, sizeof(seed), 0.0);
642}
643
Damien Miller0437b332000-05-02 09:56:41 +1000644
645/*
646 * entropy command initialisation functions
647 */
Damien Miller0437b332000-05-02 09:56:41 +1000648int
649prng_read_commands(char *cmdfilename)
650{
651 FILE *f;
Damien Miller0437b332000-05-02 09:56:41 +1000652 char *cp;
Damien Miller14c12cb2000-06-07 22:20:23 +1000653 char line[1024];
654 char cmd[1024];
655 char path[256];
Damien Miller0437b332000-05-02 09:56:41 +1000656 int linenum;
Damien Miller0437b332000-05-02 09:56:41 +1000657 int num_cmds = 64;
658 int cur_cmd = 0;
Damien Miller14c12cb2000-06-07 22:20:23 +1000659 double est;
660 entropy_source_t *entcmd;
Damien Miller0437b332000-05-02 09:56:41 +1000661
662 f = fopen(cmdfilename, "r");
663 if (!f) {
664 fatal("couldn't read entropy commands file %.100s: %.100s",
665 cmdfilename, strerror(errno));
666 }
667
Damien Miller0437b332000-05-02 09:56:41 +1000668 entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t));
669 memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t));
670
Damien Miller14c12cb2000-06-07 22:20:23 +1000671 /* Read in file */
672 linenum = 0;
Damien Miller0437b332000-05-02 09:56:41 +1000673 while (fgets(line, sizeof(line), f)) {
Damien Miller14c12cb2000-06-07 22:20:23 +1000674 int arg;
675 char *argv;
676
Damien Miller0437b332000-05-02 09:56:41 +1000677 linenum++;
678
679 /* skip leading whitespace, test for blank line or comment */
680 cp = line + strspn(line, WHITESPACE);
681 if ((*cp == 0) || (*cp == '#'))
682 continue; /* done with this line */
683
Damien Miller14c12cb2000-06-07 22:20:23 +1000684 /* First non-whitespace char should be double quote delimiting */
685 /* commandline */
686 if (*cp != '"') {
Damien Miller0437b332000-05-02 09:56:41 +1000687 error("bad entropy command, %.100s line %d", cmdfilename,
688 linenum);
689 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000690 }
Damien Miller14c12cb2000-06-07 22:20:23 +1000691
692 /* first token, command args (incl. argv[0]) in double quotes */
693 cp = strtok(cp, "\"");
694 if (cp == NULL) {
695 error("missing or bad command string, %.100s line %d -- ignored",
696 cmdfilename, linenum);
697 continue;
698 }
699 strlcpy(cmd, cp, sizeof(cmd));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000700
Damien Miller14c12cb2000-06-07 22:20:23 +1000701 /* second token, full command path */
702 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
703 error("missing command path, %.100s line %d -- ignored",
704 cmdfilename, linenum);
705 continue;
706 }
707
708 /* did configure mark this as dead? */
709 if (strncmp("undef", cp, 5) == 0)
710 continue;
711
Kevin Stevesef4eea92001-02-05 12:42:17 +0000712 strlcpy(path, cp, sizeof(path));
Damien Miller14c12cb2000-06-07 22:20:23 +1000713
714 /* third token, entropy rate estimate for this command */
715 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
716 error("missing entropy estimate, %.100s line %d -- ignored",
717 cmdfilename, linenum);
718 continue;
719 }
720 est = strtod(cp, &argv);
721
722 /* end of line */
723 if ((cp = strtok(NULL, WHITESPACE)) != NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000724 error("garbage at end of line %d in %.100s -- ignored", linenum,
Damien Miller14c12cb2000-06-07 22:20:23 +1000725 cmdfilename);
726 continue;
727 }
728
729 /* save the command for debug messages */
730 entcmd[cur_cmd].cmdstring = xstrdup(cmd);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000731
Damien Miller14c12cb2000-06-07 22:20:23 +1000732 /* split the command args */
733 cp = strtok(cmd, WHITESPACE);
734 arg = 0;
735 argv = NULL;
736 do {
737 char *s = (char*)xmalloc(strlen(cp) + 1);
738 strncpy(s, cp, strlen(cp) + 1);
739 entcmd[cur_cmd].args[arg] = s;
740 arg++;
741 } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE)));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000742
Damien Miller14c12cb2000-06-07 22:20:23 +1000743 if (strtok(NULL, WHITESPACE))
744 error("ignored extra command elements (max 5), %.100s line %d",
745 cmdfilename, linenum);
746
747 /* Copy the command path and rate estimate */
748 entcmd[cur_cmd].path = xstrdup(path);
749 entcmd[cur_cmd].rate = est;
750
751 /* Initialise other values */
752 entcmd[cur_cmd].sticky_badness = 1;
753
754 cur_cmd++;
755
756 /* If we've filled the array, reallocate it twice the size */
757 /* Do this now because even if this we're on the last command,
758 we need another slot to mark the last entry */
759 if (cur_cmd == num_cmds) {
760 num_cmds *= 2;
761 entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t));
Damien Miller0437b332000-05-02 09:56:41 +1000762 }
763 }
764
765 /* zero the last entry */
766 memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t));
Damien Miller14c12cb2000-06-07 22:20:23 +1000767
Damien Miller0437b332000-05-02 09:56:41 +1000768 /* trim to size */
769 entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t));
770
Damien Millerf9b625c2000-07-09 22:42:32 +1000771 debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename);
Damien Miller0437b332000-05-02 09:56:41 +1000772
773 return (cur_cmd >= MIN_ENTROPY_SOURCES);
774}
775
Damien Miller040f3832000-04-03 14:50:43 +1000776/*
Damien Miller4018c192000-04-30 09:30:44 +1000777 * Write a keyfile at exit
Kevin Stevesef4eea92001-02-05 12:42:17 +0000778 */
Damien Miller4018c192000-04-30 09:30:44 +1000779void
780prng_seed_cleanup(void *junk)
781{
782 prng_write_seedfile();
783}
784
785/*
786 * Conditionally Seed OpenSSL's random number pool from
787 * syscalls and program output
Damien Miller040f3832000-04-03 14:50:43 +1000788 */
789void
790seed_rng(void)
791{
Damien Millera1072a82001-02-18 15:28:11 +1100792 mysig_t old_sigchld_handler;
Damien Miller0437b332000-05-02 09:56:41 +1000793
Damien Millerf9b625c2000-07-09 22:42:32 +1000794 if (!prng_initialised)
795 fatal("RNG not initialised");
Kevin Stevesef4eea92001-02-05 12:42:17 +0000796
Damien Millerf3c6cf12000-05-17 22:08:29 +1000797 /* Make sure some other sigchld handler doesn't reap our entropy */
798 /* commands */
Damien Millera1072a82001-02-18 15:28:11 +1100799 old_sigchld_handler = mysignal(SIGCHLD, SIG_DFL);
Damien Millerf3c6cf12000-05-17 22:08:29 +1000800
Damien Millerf9b625c2000-07-09 22:42:32 +1000801 debug("Seeded RNG with %i bytes from programs", (int)stir_from_programs());
802 debug("Seeded RNG with %i bytes from system calls", (int)stir_from_system());
803
804 if (!RAND_status())
805 fatal("Not enough entropy in RNG");
Damien Miller4018c192000-04-30 09:30:44 +1000806
Damien Millera1072a82001-02-18 15:28:11 +1100807 mysignal(SIGCHLD, old_sigchld_handler);
Damien Millerf3c6cf12000-05-17 22:08:29 +1000808
Damien Miller8d1fd572000-05-17 21:34:07 +1000809 if (!RAND_status())
810 fatal("Couldn't initialise builtin random number generator -- exiting.");
Damien Miller040f3832000-04-03 14:50:43 +1000811}
Damien Millerf9b625c2000-07-09 22:42:32 +1000812
Kevin Stevesef4eea92001-02-05 12:42:17 +0000813void init_rng(void)
Damien Millerf9b625c2000-07-09 22:42:32 +1000814{
Damien Millerd592b632000-11-25 10:09:32 +1100815 int original_euid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000816
Damien Millerf9b625c2000-07-09 22:42:32 +1000817 original_uid = getuid();
Damien Millerd592b632000-11-25 10:09:32 +1100818 original_euid = geteuid();
Damien Millerf9b625c2000-07-09 22:42:32 +1000819
820 /* Read in collection commands */
821 if (!prng_read_commands(SSH_PRNG_COMMAND_FILE))
822 fatal("PRNG initialisation failed -- exiting.");
823
824 /* Set ourselves up to save a seed upon exit */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000825 prng_seed_saved = 0;
Damien Millerd592b632000-11-25 10:09:32 +1100826
827 /* Give up privs while reading seed file */
828 if ((original_uid != original_euid) && (seteuid(original_uid) == -1))
829 fatal("Couldn't give up privileges");
Kevin Stevesef4eea92001-02-05 12:42:17 +0000830
Damien Millerf9b625c2000-07-09 22:42:32 +1000831 prng_read_seedfile();
Damien Millerd592b632000-11-25 10:09:32 +1100832
833 if ((original_uid != original_euid) && (seteuid(original_euid) == -1))
834 fatal("Couldn't restore privileges");
835
Damien Millerf9b625c2000-07-09 22:42:32 +1000836 fatal_add_cleanup(prng_seed_cleanup, NULL);
837 atexit(prng_write_seedfile);
838
839 prng_initialised = 1;
840}
841
Damien Miller040f3832000-04-03 14:50:43 +1000842#endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */