]>
Commit | Line | Data |
---|---|---|
bfc9a610 | 1 | /* |
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. | |
bfc9a610 | 12 | * |
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 | ||
35484284 | 27 | #include <openssl/rand.h> |
28 | #include <openssl/sha.h> | |
c7c72446 | 29 | #include <openssl/crypto.h> |
bfc9a610 | 30 | |
819b676f | 31 | /* SunOS 4.4.4 needs this */ |
32 | #ifdef HAVE_FLOATINGPOINT_H | |
33 | # include <floatingpoint.h> | |
34 | #endif /* HAVE_FLOATINGPOINT_H */ | |
35 | ||
42f11eb2 | 36 | #include "ssh.h" |
e1a023df | 37 | #include "misc.h" |
42f11eb2 | 38 | #include "xmalloc.h" |
39 | #include "atomicio.h" | |
effa6591 | 40 | #include "pathnames.h" |
42f11eb2 | 41 | #include "log.h" |
42 | ||
bfc9a610 | 43 | RCSID("$Id$"); |
44 | ||
bfc9a610 | 45 | #ifndef offsetof |
46 | # define offsetof(type, member) ((size_t) &((type *)0)->member) | |
47 | #endif | |
48c99b2c | 48 | |
48c99b2c | 49 | /* Number of times to pass through command list gathering entropy */ |
50 | #define NUM_ENTROPY_RUNS 1 | |
51 | ||
52 | /* Scale entropy estimates back by this amount on subsequent runs */ | |
53 | #define SCALE_PER_RUN 10.0 | |
54 | ||
55 | /* Minimum number of commands to be considered valid */ | |
56 | #define MIN_ENTROPY_SOURCES 16 | |
57 | ||
58 | #define WHITESPACE " \t\n" | |
59 | ||
cbd7492e | 60 | #ifndef RUSAGE_SELF |
61 | # define RUSAGE_SELF 0 | |
62 | #endif | |
63 | #ifndef RUSAGE_CHILDREN | |
64 | # define RUSAGE_CHILDREN 0 | |
65 | #endif | |
66 | ||
86b416a7 | 67 | #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS) |
68 | # define SAVED_IDS_WORK_WITH_SETEUID | |
69 | #endif | |
70 | ||
c7c72446 | 71 | void check_openssl_version(void) |
72 | { | |
73 | if (SSLeay() != OPENSSL_VERSION_NUMBER) | |
32e0cb42 | 74 | fatal("OpenSSL version mismatch. Built against %lx, you " |
75 | "have %lx", OPENSSL_VERSION_NUMBER, SSLeay()); | |
c7c72446 | 76 | } |
77 | ||
9bdd5929 | 78 | #if defined(PRNGD_SOCKET) || defined(PRNGD_PORT) |
79 | # define USE_PRNGD | |
80 | #endif | |
c7c72446 | 81 | |
9bdd5929 | 82 | #if defined(USE_PRNGD) || defined(RANDOM_POOL) |
48c99b2c | 83 | |
9bdd5929 | 84 | #ifdef USE_PRNGD |
85 | /* Collect entropy from PRNGD/EGD */ | |
be0b9bb7 | 86 | int get_random_bytes(unsigned char *buf, int len) |
bfc9a610 | 87 | { |
be0b9bb7 | 88 | int fd; |
89 | char msg[2]; | |
9bdd5929 | 90 | #ifdef PRNGD_PORT |
91 | struct sockaddr_in addr; | |
92 | #else | |
bfc9a610 | 93 | struct sockaddr_un addr; |
9bdd5929 | 94 | #endif |
6a951840 | 95 | int addr_len, rval, errors; |
e1a023df | 96 | mysig_t old_sigpipe; |
bfc9a610 | 97 | |
9bdd5929 | 98 | memset(&addr, '\0', sizeof(addr)); |
99 | ||
100 | #ifdef PRNGD_PORT | |
101 | addr.sin_family = AF_INET; | |
102 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
103 | addr.sin_port = htons(PRNGD_PORT); | |
104 | addr_len = sizeof(struct sockaddr_in); | |
105 | #else /* use IP socket PRNGD_SOCKET instead */ | |
be0b9bb7 | 106 | /* Sanity checks */ |
9bdd5929 | 107 | if (sizeof(PRNGD_SOCKET) > sizeof(addr.sun_path)) |
bfc9a610 | 108 | fatal("Random pool path is too long"); |
be0b9bb7 | 109 | if (len > 255) |
9bdd5929 | 110 | fatal("Too many bytes to read from PRNGD"); |
be0b9bb7 | 111 | |
be0b9bb7 | 112 | addr.sun_family = AF_UNIX; |
9bdd5929 | 113 | strlcpy(addr.sun_path, PRNGD_SOCKET, sizeof(addr.sun_path)); |
114 | addr_len = offsetof(struct sockaddr_un, sun_path) + | |
115 | sizeof(PRNGD_SOCKET); | |
116 | #endif | |
2b87da3b | 117 | |
e1a023df | 118 | old_sigpipe = mysignal(SIGPIPE, SIG_IGN); |
6a951840 | 119 | |
120 | errors = rval = 0; | |
121 | reopen: | |
9bdd5929 | 122 | #ifdef PRNGD_PORT |
123 | fd = socket(addr.sin_family, SOCK_STREAM, 0); | |
124 | if (fd == -1) { | |
125 | error("Couldn't create AF_INET socket: %s", strerror(errno)); | |
126 | goto done; | |
127 | } | |
128 | #else | |
129 | fd = socket(addr.sun_family, SOCK_STREAM, 0); | |
be0b9bb7 | 130 | if (fd == -1) { |
131 | error("Couldn't create AF_UNIX socket: %s", strerror(errno)); | |
6a951840 | 132 | goto done; |
be0b9bb7 | 133 | } |
9bdd5929 | 134 | #endif |
bfc9a610 | 135 | |
be0b9bb7 | 136 | if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) { |
9bdd5929 | 137 | #ifdef PRNGD_PORT |
138 | error("Couldn't connect to PRNGD port %d: %s", | |
139 | PRNGD_PORT, strerror(errno)); | |
140 | #else | |
141 | error("Couldn't connect to PRNGD socket \"%s\": %s", | |
142 | addr.sun_path, strerror(errno)); | |
143 | #endif | |
6a951840 | 144 | goto done; |
be0b9bb7 | 145 | } |
bfc9a610 | 146 | |
9bdd5929 | 147 | /* Send blocking read request to PRNGD */ |
be0b9bb7 | 148 | msg[0] = 0x02; |
149 | msg[1] = len; | |
150 | ||
151 | if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) { | |
6a951840 | 152 | if (errno == EPIPE && errors < 10) { |
153 | close(fd); | |
154 | errors++; | |
155 | goto reopen; | |
156 | } | |
9bdd5929 | 157 | error("Couldn't write to PRNGD socket: %s", |
158 | strerror(errno)); | |
6a951840 | 159 | goto done; |
be0b9bb7 | 160 | } |
bfc9a610 | 161 | |
be0b9bb7 | 162 | if (atomicio(read, fd, buf, len) != len) { |
6a951840 | 163 | if (errno == EPIPE && errors < 10) { |
164 | close(fd); | |
165 | errors++; | |
166 | goto reopen; | |
167 | } | |
9bdd5929 | 168 | error("Couldn't read from PRNGD socket: %s", |
169 | strerror(errno)); | |
6a951840 | 170 | goto done; |
be0b9bb7 | 171 | } |
2b87da3b | 172 | |
6a951840 | 173 | rval = 1; |
174 | done: | |
2adddc78 | 175 | mysignal(SIGPIPE, old_sigpipe); |
6a951840 | 176 | if (fd != -1) |
177 | close(fd); | |
178 | return(rval); | |
bfc9a610 | 179 | } |
9bdd5929 | 180 | #else /* !USE_PRNGD */ |
bfc9a610 | 181 | #ifdef RANDOM_POOL |
182 | /* Collect entropy from /dev/urandom or pipe */ | |
be0b9bb7 | 183 | int get_random_bytes(unsigned char *buf, int len) |
bfc9a610 | 184 | { |
be0b9bb7 | 185 | int random_pool; |
bfc9a610 | 186 | |
be0b9bb7 | 187 | random_pool = open(RANDOM_POOL, O_RDONLY); |
bfc9a610 | 188 | if (random_pool == -1) { |
2b87da3b | 189 | error("Couldn't open random pool \"%s\": %s", |
be0b9bb7 | 190 | RANDOM_POOL, strerror(errno)); |
191 | return(0); | |
bfc9a610 | 192 | } |
2b87da3b | 193 | |
be0b9bb7 | 194 | if (atomicio(read, random_pool, buf, len) != len) { |
2b87da3b | 195 | error("Couldn't read from random pool \"%s\": %s", |
be0b9bb7 | 196 | RANDOM_POOL, strerror(errno)); |
197 | close(random_pool); | |
198 | return(0); | |
199 | } | |
2b87da3b | 200 | |
be0b9bb7 | 201 | close(random_pool); |
2b87da3b | 202 | |
be0b9bb7 | 203 | return(1); |
bfc9a610 | 204 | } |
205 | #endif /* RANDOM_POOL */ | |
9bdd5929 | 206 | #endif /* USE_PRNGD */ |
bfc9a610 | 207 | |
48c99b2c | 208 | /* |
209 | * Seed OpenSSL's random number pool from Kernel random number generator | |
9bdd5929 | 210 | * or PRNGD/EGD |
48c99b2c | 211 | */ |
212 | void | |
213 | seed_rng(void) | |
214 | { | |
9bdd5929 | 215 | unsigned char buf[32]; |
2b87da3b | 216 | |
48c99b2c | 217 | debug("Seeding random number generator"); |
be0b9bb7 | 218 | |
b5b3f75d | 219 | if (!get_random_bytes(buf, sizeof(buf))) { |
220 | if (!RAND_status()) | |
221 | fatal("Entropy collection failed and entropy exhausted"); | |
222 | } else { | |
223 | RAND_add(buf, sizeof(buf), sizeof(buf)); | |
224 | } | |
2b87da3b | 225 | |
48c99b2c | 226 | memset(buf, '\0', sizeof(buf)); |
227 | } | |
228 | ||
c7c72446 | 229 | void init_rng(void) |
230 | { | |
231 | check_openssl_version(); | |
232 | } | |
264dce47 | 233 | |
9bdd5929 | 234 | #else /* defined(USE_PRNGD) || defined(RANDOM_POOL) */ |
48c99b2c | 235 | |
2b87da3b | 236 | /* |
bfc9a610 | 237 | * FIXME: proper entropy estimations. All current values are guesses |
b7a87eea | 238 | * FIXME: (ATL) do estimates at compile time? |
bfc9a610 | 239 | * FIXME: More entropy sources |
240 | */ | |
241 | ||
b7a87eea | 242 | /* slow command timeouts (all in milliseconds) */ |
243 | /* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */ | |
244 | static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC; | |
245 | ||
d3083fbd | 246 | static int prng_seed_saved = 0; |
264dce47 | 247 | static int prng_initialised = 0; |
248 | uid_t original_uid; | |
bfc9a610 | 249 | |
250 | typedef struct | |
251 | { | |
252 | /* Proportion of data that is entropy */ | |
253 | double rate; | |
b7a87eea | 254 | /* Counter goes positive if this command times out */ |
255 | unsigned int badness; | |
256 | /* Increases by factor of two each timeout */ | |
257 | unsigned int sticky_badness; | |
bfc9a610 | 258 | /* Path to executable */ |
d3083fbd | 259 | char *path; |
bfc9a610 | 260 | /* argv to pass to executable */ |
d3083fbd | 261 | char *args[5]; |
ad85db64 | 262 | /* full command string (debug) */ |
263 | char *cmdstring; | |
bfc9a610 | 264 | } entropy_source_t; |
265 | ||
b7a87eea | 266 | double stir_from_system(void); |
267 | double stir_from_programs(void); | |
268 | double stir_gettimeofday(double entropy_estimate); | |
269 | double stir_clock(double entropy_estimate); | |
270 | double stir_rusage(int who, double entropy_estimate); | |
271 | double hash_output_from_command(entropy_source_t *src, char *hash); | |
272 | ||
d3083fbd | 273 | /* this is initialised from a file, by prng_read_commands() */ |
274 | entropy_source_t *entropy_sources = NULL; | |
bfc9a610 | 275 | |
2b87da3b | 276 | double |
bfc9a610 | 277 | stir_from_system(void) |
278 | { | |
279 | double total_entropy_estimate; | |
280 | long int i; | |
2b87da3b | 281 | |
bfc9a610 | 282 | total_entropy_estimate = 0; |
2b87da3b | 283 | |
bfc9a610 | 284 | i = getpid(); |
cbd7492e | 285 | RAND_add(&i, sizeof(i), 0.5); |
bfc9a610 | 286 | total_entropy_estimate += 0.1; |
2b87da3b | 287 | |
bfc9a610 | 288 | i = getppid(); |
cbd7492e | 289 | RAND_add(&i, sizeof(i), 0.5); |
bfc9a610 | 290 | total_entropy_estimate += 0.1; |
291 | ||
292 | i = getuid(); | |
293 | RAND_add(&i, sizeof(i), 0.0); | |
294 | i = getgid(); | |
295 | RAND_add(&i, sizeof(i), 0.0); | |
296 | ||
297 | total_entropy_estimate += stir_gettimeofday(1.0); | |
cbd7492e | 298 | total_entropy_estimate += stir_clock(0.5); |
bfc9a610 | 299 | total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0); |
300 | ||
301 | return(total_entropy_estimate); | |
302 | } | |
303 | ||
2b87da3b | 304 | double |
bfc9a610 | 305 | stir_from_programs(void) |
306 | { | |
307 | int i; | |
308 | int c; | |
309 | double entropy_estimate; | |
310 | double total_entropy_estimate; | |
311 | char hash[SHA_DIGEST_LENGTH]; | |
312 | ||
bfc9a610 | 313 | total_entropy_estimate = 0; |
48c99b2c | 314 | for(i = 0; i < NUM_ENTROPY_RUNS; i++) { |
bfc9a610 | 315 | c = 0; |
316 | while (entropy_sources[c].path != NULL) { | |
bfc9a610 | 317 | |
b7a87eea | 318 | if (!entropy_sources[c].badness) { |
319 | /* Hash output from command */ | |
320 | entropy_estimate = hash_output_from_command(&entropy_sources[c], hash); | |
321 | ||
322 | /* Scale back entropy estimate according to command's rate */ | |
323 | entropy_estimate *= entropy_sources[c].rate; | |
2b87da3b | 324 | |
b7a87eea | 325 | /* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */ |
326 | if (entropy_estimate > SHA_DIGEST_LENGTH) | |
327 | entropy_estimate = SHA_DIGEST_LENGTH; | |
bfc9a610 | 328 | |
2b87da3b | 329 | /* Scale back estimates for subsequent passes through list */ |
48c99b2c | 330 | entropy_estimate /= SCALE_PER_RUN * (i + 1.0); |
2b87da3b | 331 | |
b7a87eea | 332 | /* Stir it in */ |
333 | RAND_add(hash, sizeof(hash), entropy_estimate); | |
bfc9a610 | 334 | |
2b87da3b | 335 | debug3("Got %0.2f bytes of entropy from '%s'", entropy_estimate, |
ad85db64 | 336 | entropy_sources[c].cmdstring); |
bfc9a610 | 337 | |
b7a87eea | 338 | total_entropy_estimate += entropy_estimate; |
bfc9a610 | 339 | |
340 | /* Execution times should be a little unpredictable */ | |
b7a87eea | 341 | total_entropy_estimate += stir_gettimeofday(0.05); |
342 | total_entropy_estimate += stir_clock(0.05); | |
343 | total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1); | |
344 | total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1); | |
345 | } else { | |
22d89d24 | 346 | debug2("Command '%s' disabled (badness %d)", |
ad85db64 | 347 | entropy_sources[c].cmdstring, entropy_sources[c].badness); |
b7a87eea | 348 | |
349 | if (entropy_sources[c].badness > 0) | |
350 | entropy_sources[c].badness--; | |
351 | } | |
352 | ||
bfc9a610 | 353 | c++; |
354 | } | |
355 | } | |
2b87da3b | 356 | |
bfc9a610 | 357 | return(total_entropy_estimate); |
358 | } | |
359 | ||
360 | double | |
361 | stir_gettimeofday(double entropy_estimate) | |
362 | { | |
363 | struct timeval tv; | |
2b87da3b | 364 | |
bfc9a610 | 365 | if (gettimeofday(&tv, NULL) == -1) |
366 | fatal("Couldn't gettimeofday: %s", strerror(errno)); | |
367 | ||
368 | RAND_add(&tv, sizeof(tv), entropy_estimate); | |
2b87da3b | 369 | |
bfc9a610 | 370 | return(entropy_estimate); |
371 | } | |
372 | ||
373 | double | |
374 | stir_clock(double entropy_estimate) | |
375 | { | |
376 | #ifdef HAVE_CLOCK | |
377 | clock_t c; | |
2b87da3b | 378 | |
bfc9a610 | 379 | c = clock(); |
380 | RAND_add(&c, sizeof(c), entropy_estimate); | |
2b87da3b | 381 | |
bfc9a610 | 382 | return(entropy_estimate); |
383 | #else /* _HAVE_CLOCK */ | |
384 | return(0); | |
385 | #endif /* _HAVE_CLOCK */ | |
386 | } | |
387 | ||
388 | double | |
389 | stir_rusage(int who, double entropy_estimate) | |
390 | { | |
391 | #ifdef HAVE_GETRUSAGE | |
392 | struct rusage ru; | |
2b87da3b | 393 | |
b7a87eea | 394 | if (getrusage(who, &ru) == -1) |
cbd7492e | 395 | return(0); |
bfc9a610 | 396 | |
cbd7492e | 397 | RAND_add(&ru, sizeof(ru), entropy_estimate); |
bfc9a610 | 398 | |
399 | return(entropy_estimate); | |
400 | #else /* _HAVE_GETRUSAGE */ | |
401 | return(0); | |
402 | #endif /* _HAVE_GETRUSAGE */ | |
403 | } | |
404 | ||
ad85db64 | 405 | |
406 | static | |
407 | int | |
408 | _get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) { | |
409 | int secdiff, usecdiff; | |
410 | ||
411 | secdiff = t2->tv_sec - t1->tv_sec; | |
412 | usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec); | |
413 | return (int)(usecdiff / 1000); | |
414 | } | |
415 | ||
bfc9a610 | 416 | double |
b7a87eea | 417 | hash_output_from_command(entropy_source_t *src, char *hash) |
bfc9a610 | 418 | { |
419 | static int devnull = -1; | |
420 | int p[2]; | |
b7a87eea | 421 | fd_set rdset; |
422 | int cmd_eof = 0, error_abort = 0; | |
ad85db64 | 423 | struct timeval tv_start, tv_current; |
424 | int msec_elapsed = 0; | |
bfc9a610 | 425 | pid_t pid; |
426 | int status; | |
ad85db64 | 427 | char buf[16384]; |
bfc9a610 | 428 | int bytes_read; |
429 | int total_bytes_read; | |
430 | SHA_CTX sha; | |
2b87da3b | 431 | |
22d89d24 | 432 | debug3("Reading output from \'%s\'", src->cmdstring); |
433 | ||
bfc9a610 | 434 | if (devnull == -1) { |
435 | devnull = open("/dev/null", O_RDWR); | |
436 | if (devnull == -1) | |
437 | fatal("Couldn't open /dev/null: %s", strerror(errno)); | |
438 | } | |
2b87da3b | 439 | |
bfc9a610 | 440 | if (pipe(p) == -1) |
441 | fatal("Couldn't open pipe: %s", strerror(errno)); | |
442 | ||
ad85db64 | 443 | (void)gettimeofday(&tv_start, NULL); /* record start time */ |
444 | ||
bfc9a610 | 445 | switch (pid = fork()) { |
446 | case -1: /* Error */ | |
447 | close(p[0]); | |
448 | close(p[1]); | |
449 | fatal("Couldn't fork: %s", strerror(errno)); | |
450 | /* NOTREACHED */ | |
451 | case 0: /* Child */ | |
b7a87eea | 452 | dup2(devnull, STDIN_FILENO); |
453 | dup2(p[1], STDOUT_FILENO); | |
454 | dup2(p[1], STDERR_FILENO); | |
bfc9a610 | 455 | close(p[0]); |
456 | close(p[1]); | |
457 | close(devnull); | |
458 | ||
264dce47 | 459 | setuid(original_uid); |
b7a87eea | 460 | execv(src->path, (char**)(src->args)); |
ad85db64 | 461 | debug("(child) Couldn't exec '%s': %s", src->cmdstring, |
462 | strerror(errno)); | |
bfc9a610 | 463 | _exit(-1); |
464 | default: /* Parent */ | |
465 | break; | |
466 | } | |
467 | ||
468 | RAND_add(&pid, sizeof(&pid), 0.0); | |
469 | ||
470 | close(p[1]); | |
471 | ||
472 | /* Hash output from child */ | |
473 | SHA1_Init(&sha); | |
474 | total_bytes_read = 0; | |
b7a87eea | 475 | |
476 | while (!error_abort && !cmd_eof) { | |
477 | int ret; | |
478 | struct timeval tv; | |
ad85db64 | 479 | int msec_remaining; |
480 | ||
481 | (void) gettimeofday(&tv_current, 0); | |
48c99b2c | 482 | msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current); |
ad85db64 | 483 | if (msec_elapsed >= entropy_timeout_current) { |
484 | error_abort=1; | |
485 | continue; | |
486 | } | |
487 | msec_remaining = entropy_timeout_current - msec_elapsed; | |
b7a87eea | 488 | |
489 | FD_ZERO(&rdset); | |
490 | FD_SET(p[0], &rdset); | |
ad85db64 | 491 | tv.tv_sec = msec_remaining / 1000; |
492 | tv.tv_usec = (msec_remaining % 1000) * 1000; | |
b7a87eea | 493 | |
494 | ret = select(p[0]+1, &rdset, NULL, NULL, &tv); | |
ad85db64 | 495 | |
264dce47 | 496 | RAND_add(&tv, sizeof(tv), 0.0); |
497 | ||
b7a87eea | 498 | switch (ret) { |
499 | case 0: | |
500 | /* timer expired */ | |
501 | error_abort = 1; | |
502 | break; | |
b7a87eea | 503 | case 1: |
504 | /* command input */ | |
505 | bytes_read = read(p[0], buf, sizeof(buf)); | |
264dce47 | 506 | RAND_add(&bytes_read, sizeof(&bytes_read), 0.0); |
b7a87eea | 507 | if (bytes_read == -1) { |
508 | error_abort = 1; | |
509 | break; | |
264dce47 | 510 | } else if (bytes_read) { |
ad85db64 | 511 | SHA1_Update(&sha, buf, bytes_read); |
512 | total_bytes_read += bytes_read; | |
264dce47 | 513 | } else { |
ad85db64 | 514 | cmd_eof = 1; |
264dce47 | 515 | } |
b7a87eea | 516 | break; |
b7a87eea | 517 | case -1: |
518 | default: | |
264dce47 | 519 | /* error */ |
ad85db64 | 520 | debug("Command '%s': select() failed: %s", src->cmdstring, |
521 | strerror(errno)); | |
b7a87eea | 522 | error_abort = 1; |
523 | break; | |
264dce47 | 524 | } |
525 | } | |
b7a87eea | 526 | |
bfc9a610 | 527 | SHA1_Final(hash, &sha); |
528 | ||
529 | close(p[0]); | |
ad85db64 | 530 | |
22d89d24 | 531 | debug3("Time elapsed: %d msec", msec_elapsed); |
2b87da3b | 532 | |
bfc9a610 | 533 | if (waitpid(pid, &status, 0) == -1) { |
22d89d24 | 534 | error("Couldn't wait for child '%s' completion: %s", src->cmdstring, |
ad85db64 | 535 | strerror(errno)); |
b7a87eea | 536 | return(0.0); |
bfc9a610 | 537 | } |
538 | ||
539 | RAND_add(&status, sizeof(&status), 0.0); | |
540 | ||
b7a87eea | 541 | if (error_abort) { |
542 | /* closing p[0] on timeout causes the entropy command to | |
543 | * SIGPIPE. Take whatever output we got, and mark this command | |
544 | * as slow */ | |
22d89d24 | 545 | debug2("Command '%s' timed out", src->cmdstring); |
b7a87eea | 546 | src->sticky_badness *= 2; |
547 | src->badness = src->sticky_badness; | |
bfc9a610 | 548 | return(total_bytes_read); |
b7a87eea | 549 | } |
550 | ||
551 | if (WIFEXITED(status)) { | |
552 | if (WEXITSTATUS(status)==0) { | |
553 | return(total_bytes_read); | |
554 | } else { | |
2b87da3b | 555 | debug2("Command '%s' exit status was %d", src->cmdstring, |
48c99b2c | 556 | WEXITSTATUS(status)); |
b7a87eea | 557 | src->badness = src->sticky_badness = 128; |
558 | return (0.0); | |
559 | } | |
560 | } else if (WIFSIGNALED(status)) { | |
2b87da3b | 561 | debug2("Command '%s' returned on uncaught signal %d !", src->cmdstring, |
48c99b2c | 562 | status); |
b7a87eea | 563 | src->badness = src->sticky_badness = 128; |
564 | return(0.0); | |
565 | } else | |
566 | return(0.0); | |
567 | } | |
568 | ||
569 | /* | |
570 | * prng seedfile functions | |
571 | */ | |
572 | int | |
573 | prng_check_seedfile(char *filename) { | |
574 | ||
575 | struct stat st; | |
576 | ||
577 | /* FIXME raceable: eg replace seed between this stat and subsequent open */ | |
578 | /* Not such a problem because we don't trust the seed file anyway */ | |
579 | if (lstat(filename, &st) == -1) { | |
5b2d4b75 | 580 | /* Give up on hard errors */ |
b7a87eea | 581 | if (errno != ENOENT) |
2b87da3b | 582 | debug("WARNING: Couldn't stat random seed file \"%s\": %s", |
5b2d4b75 | 583 | filename, strerror(errno)); |
b7a87eea | 584 | |
585 | return(0); | |
586 | } | |
587 | ||
588 | /* regular file? */ | |
589 | if (!S_ISREG(st.st_mode)) | |
590 | fatal("PRNG seedfile %.100s is not a regular file", filename); | |
591 | ||
592 | /* mode 0600, owned by root or the current user? */ | |
5b2d4b75 | 593 | if (((st.st_mode & 0177) != 0) || !(st.st_uid == original_uid)) { |
594 | debug("WARNING: PRNG seedfile %.100s must be mode 0600, owned by uid %d", | |
b7a87eea | 595 | filename, getuid()); |
5b2d4b75 | 596 | return(0); |
597 | } | |
2b87da3b | 598 | |
b7a87eea | 599 | return(1); |
600 | } | |
601 | ||
602 | void | |
603 | prng_write_seedfile(void) { | |
604 | int fd; | |
605 | char seed[1024]; | |
606 | char filename[1024]; | |
607 | struct passwd *pw; | |
608 | ||
609 | /* Don't bother if we have already saved a seed */ | |
610 | if (prng_seed_saved) | |
611 | return; | |
2b87da3b | 612 | |
264dce47 | 613 | setuid(original_uid); |
2b87da3b | 614 | |
52bcc044 | 615 | prng_seed_saved = 1; |
2b87da3b | 616 | |
264dce47 | 617 | pw = getpwuid(original_uid); |
b7a87eea | 618 | if (pw == NULL) |
2b87da3b | 619 | fatal("Couldn't get password entry for current user (%i): %s", |
264dce47 | 620 | original_uid, strerror(errno)); |
2b87da3b | 621 | |
b7a87eea | 622 | /* Try to ensure that the parent directory is there */ |
2b87da3b | 623 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, |
effa6591 | 624 | _PATH_SSH_USER_DIR); |
b7a87eea | 625 | mkdir(filename, 0700); |
626 | ||
2b87da3b | 627 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, |
b7a87eea | 628 | SSH_PRNG_SEED_FILE); |
629 | ||
630 | debug("writing PRNG seed to file %.100s", filename); | |
631 | ||
632 | RAND_bytes(seed, sizeof(seed)); | |
633 | ||
634 | /* Don't care if the seed doesn't exist */ | |
635 | prng_check_seedfile(filename); | |
2b87da3b | 636 | |
5b2d4b75 | 637 | if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) { |
2b87da3b | 638 | debug("WARNING: couldn't access PRNG seedfile %.100s (%.100s)", |
5b2d4b75 | 639 | filename, strerror(errno)); |
2b87da3b | 640 | } else { |
5b2d4b75 | 641 | if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed)) |
2b87da3b | 642 | fatal("problem writing PRNG seedfile %.100s (%.100s)", filename, |
5b2d4b75 | 643 | strerror(errno)); |
b7a87eea | 644 | |
5b2d4b75 | 645 | close(fd); |
646 | } | |
b7a87eea | 647 | } |
648 | ||
649 | void | |
650 | prng_read_seedfile(void) { | |
651 | int fd; | |
652 | char seed[1024]; | |
653 | char filename[1024]; | |
654 | struct passwd *pw; | |
2b87da3b | 655 | |
264dce47 | 656 | pw = getpwuid(original_uid); |
b7a87eea | 657 | if (pw == NULL) |
2b87da3b | 658 | fatal("Couldn't get password entry for current user (%i): %s", |
264dce47 | 659 | original_uid, strerror(errno)); |
2b87da3b | 660 | |
661 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, | |
b7a87eea | 662 | SSH_PRNG_SEED_FILE); |
663 | ||
664 | debug("loading PRNG seed from file %.100s", filename); | |
665 | ||
666 | if (!prng_check_seedfile(filename)) { | |
52ce34a2 | 667 | verbose("Random seed file not found or not valid, ignoring."); |
b7a87eea | 668 | return; |
669 | } | |
670 | ||
671 | /* open the file and read in the seed */ | |
672 | fd = open(filename, O_RDONLY); | |
673 | if (fd == -1) | |
2b87da3b | 674 | fatal("could not open PRNG seedfile %.100s (%.100s)", filename, |
b7a87eea | 675 | strerror(errno)); |
676 | ||
677 | if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) { | |
678 | verbose("invalid or short read from PRNG seedfile %.100s - ignoring", | |
679 | filename); | |
680 | memset(seed, '\0', sizeof(seed)); | |
681 | } | |
682 | close(fd); | |
683 | ||
684 | /* stir in the seed, with estimated entropy zero */ | |
685 | RAND_add(&seed, sizeof(seed), 0.0); | |
bfc9a610 | 686 | } |
b7a87eea | 687 | |
d3083fbd | 688 | |
689 | /* | |
690 | * entropy command initialisation functions | |
691 | */ | |
d3083fbd | 692 | int |
693 | prng_read_commands(char *cmdfilename) | |
694 | { | |
695 | FILE *f; | |
d3083fbd | 696 | char *cp; |
48c99b2c | 697 | char line[1024]; |
698 | char cmd[1024]; | |
699 | char path[256]; | |
d3083fbd | 700 | int linenum; |
d3083fbd | 701 | int num_cmds = 64; |
702 | int cur_cmd = 0; | |
48c99b2c | 703 | double est; |
704 | entropy_source_t *entcmd; | |
d3083fbd | 705 | |
706 | f = fopen(cmdfilename, "r"); | |
707 | if (!f) { | |
708 | fatal("couldn't read entropy commands file %.100s: %.100s", | |
709 | cmdfilename, strerror(errno)); | |
710 | } | |
711 | ||
d3083fbd | 712 | entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t)); |
713 | memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t)); | |
714 | ||
48c99b2c | 715 | /* Read in file */ |
716 | linenum = 0; | |
d3083fbd | 717 | while (fgets(line, sizeof(line), f)) { |
48c99b2c | 718 | int arg; |
719 | char *argv; | |
720 | ||
d3083fbd | 721 | linenum++; |
722 | ||
723 | /* skip leading whitespace, test for blank line or comment */ | |
724 | cp = line + strspn(line, WHITESPACE); | |
725 | if ((*cp == 0) || (*cp == '#')) | |
726 | continue; /* done with this line */ | |
727 | ||
48c99b2c | 728 | /* First non-whitespace char should be double quote delimiting */ |
729 | /* commandline */ | |
730 | if (*cp != '"') { | |
731 | error("bad entropy command, %.100s line %d", cmdfilename, | |
732 | linenum); | |
733 | continue; | |
2b87da3b | 734 | } |
d3083fbd | 735 | |
48c99b2c | 736 | /* first token, command args (incl. argv[0]) in double quotes */ |
737 | cp = strtok(cp, "\""); | |
738 | if (cp == NULL) { | |
739 | error("missing or bad command string, %.100s line %d -- ignored", | |
740 | cmdfilename, linenum); | |
741 | continue; | |
742 | } | |
743 | strlcpy(cmd, cp, sizeof(cmd)); | |
2b87da3b | 744 | |
48c99b2c | 745 | /* second token, full command path */ |
746 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { | |
747 | error("missing command path, %.100s line %d -- ignored", | |
748 | cmdfilename, linenum); | |
749 | continue; | |
750 | } | |
d3083fbd | 751 | |
48c99b2c | 752 | /* did configure mark this as dead? */ |
753 | if (strncmp("undef", cp, 5) == 0) | |
754 | continue; | |
d3083fbd | 755 | |
2b87da3b | 756 | strlcpy(path, cp, sizeof(path)); |
48c99b2c | 757 | |
758 | /* third token, entropy rate estimate for this command */ | |
759 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { | |
760 | error("missing entropy estimate, %.100s line %d -- ignored", | |
761 | cmdfilename, linenum); | |
762 | continue; | |
763 | } | |
764 | est = strtod(cp, &argv); | |
765 | ||
766 | /* end of line */ | |
767 | if ((cp = strtok(NULL, WHITESPACE)) != NULL) { | |
2b87da3b | 768 | error("garbage at end of line %d in %.100s -- ignored", linenum, |
48c99b2c | 769 | cmdfilename); |
d3083fbd | 770 | continue; |
771 | } | |
48c99b2c | 772 | |
773 | /* save the command for debug messages */ | |
774 | entcmd[cur_cmd].cmdstring = xstrdup(cmd); | |
2b87da3b | 775 | |
48c99b2c | 776 | /* split the command args */ |
777 | cp = strtok(cmd, WHITESPACE); | |
778 | arg = 0; | |
779 | argv = NULL; | |
780 | do { | |
781 | char *s = (char*)xmalloc(strlen(cp) + 1); | |
782 | strncpy(s, cp, strlen(cp) + 1); | |
783 | entcmd[cur_cmd].args[arg] = s; | |
784 | arg++; | |
785 | } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE))); | |
2b87da3b | 786 | |
48c99b2c | 787 | if (strtok(NULL, WHITESPACE)) |
788 | error("ignored extra command elements (max 5), %.100s line %d", | |
789 | cmdfilename, linenum); | |
790 | ||
791 | /* Copy the command path and rate estimate */ | |
792 | entcmd[cur_cmd].path = xstrdup(path); | |
793 | entcmd[cur_cmd].rate = est; | |
794 | ||
795 | /* Initialise other values */ | |
796 | entcmd[cur_cmd].sticky_badness = 1; | |
797 | ||
798 | cur_cmd++; | |
799 | ||
800 | /* If we've filled the array, reallocate it twice the size */ | |
801 | /* Do this now because even if this we're on the last command, | |
802 | we need another slot to mark the last entry */ | |
803 | if (cur_cmd == num_cmds) { | |
804 | num_cmds *= 2; | |
805 | entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t)); | |
806 | } | |
d3083fbd | 807 | } |
808 | ||
809 | /* zero the last entry */ | |
810 | memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t)); | |
48c99b2c | 811 | |
d3083fbd | 812 | /* trim to size */ |
813 | entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t)); | |
814 | ||
264dce47 | 815 | debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename); |
d3083fbd | 816 | |
817 | return (cur_cmd >= MIN_ENTROPY_SOURCES); | |
818 | } | |
819 | ||
b7a87eea | 820 | /* |
821 | * Write a keyfile at exit | |
2b87da3b | 822 | */ |
b7a87eea | 823 | void |
824 | prng_seed_cleanup(void *junk) | |
825 | { | |
826 | prng_write_seedfile(); | |
827 | } | |
828 | ||
bfc9a610 | 829 | /* |
b7a87eea | 830 | * Conditionally Seed OpenSSL's random number pool from |
831 | * syscalls and program output | |
bfc9a610 | 832 | */ |
833 | void | |
834 | seed_rng(void) | |
835 | { | |
e1a023df | 836 | mysig_t old_sigchld_handler; |
d3083fbd | 837 | |
264dce47 | 838 | if (!prng_initialised) |
839 | fatal("RNG not initialised"); | |
2b87da3b | 840 | |
a64009ad | 841 | /* Make sure some other sigchld handler doesn't reap our entropy */ |
842 | /* commands */ | |
e1a023df | 843 | old_sigchld_handler = mysignal(SIGCHLD, SIG_DFL); |
a64009ad | 844 | |
264dce47 | 845 | debug("Seeded RNG with %i bytes from programs", (int)stir_from_programs()); |
846 | debug("Seeded RNG with %i bytes from system calls", (int)stir_from_system()); | |
847 | ||
848 | if (!RAND_status()) | |
849 | fatal("Not enough entropy in RNG"); | |
b7a87eea | 850 | |
e1a023df | 851 | mysignal(SIGCHLD, old_sigchld_handler); |
a64009ad | 852 | |
ad85db64 | 853 | if (!RAND_status()) |
854 | fatal("Couldn't initialise builtin random number generator -- exiting."); | |
264dce47 | 855 | } |
ad85db64 | 856 | |
2b87da3b | 857 | void init_rng(void) |
264dce47 | 858 | { |
e879a080 | 859 | int original_euid; |
2b87da3b | 860 | |
c7c72446 | 861 | check_openssl_version(); |
862 | ||
264dce47 | 863 | original_uid = getuid(); |
e879a080 | 864 | original_euid = geteuid(); |
264dce47 | 865 | |
866 | /* Read in collection commands */ | |
867 | if (!prng_read_commands(SSH_PRNG_COMMAND_FILE)) | |
868 | fatal("PRNG initialisation failed -- exiting."); | |
869 | ||
870 | /* Set ourselves up to save a seed upon exit */ | |
2b87da3b | 871 | prng_seed_saved = 0; |
e879a080 | 872 | |
873 | /* Give up privs while reading seed file */ | |
e9a13ac1 | 874 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
e879a080 | 875 | if ((original_uid != original_euid) && (seteuid(original_uid) == -1)) |
876 | fatal("Couldn't give up privileges"); | |
e9a13ac1 | 877 | #else /* SAVED_IDS_WORK_WITH_SETEUID */ |
878 | /* | |
879 | * Propagate the privileged uid to all of our uids. | |
880 | * Set the effective uid to the given (unprivileged) uid. | |
881 | */ | |
d5c4c52e | 882 | if (original_uid != original_euid && (setuid(original_euid) == -1 || |
883 | seteuid(original_uid) == -1)) | |
e9a13ac1 | 884 | fatal("Couldn't give up privileges"); |
885 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ | |
2b87da3b | 886 | |
264dce47 | 887 | prng_read_seedfile(); |
e879a080 | 888 | |
e9a13ac1 | 889 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
e879a080 | 890 | if ((original_uid != original_euid) && (seteuid(original_euid) == -1)) |
891 | fatal("Couldn't restore privileges"); | |
e9a13ac1 | 892 | #else /* SAVED_IDS_WORK_WITH_SETEUID */ |
893 | /* | |
894 | * We are unable to restore the real uid to its unprivileged value. | |
895 | * Propagate the real uid (usually more privileged) to effective uid | |
896 | * as well. | |
897 | */ | |
d5c4c52e | 898 | if (original_uid != original_euid && (seteuid(original_euid) == -1 || |
899 | setuid(original_uid) == -1)) | |
e9a13ac1 | 900 | fatal("Couldn't restore privileges"); |
901 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ | |
e879a080 | 902 | |
264dce47 | 903 | fatal_add_cleanup(prng_seed_cleanup, NULL); |
904 | atexit(prng_write_seedfile); | |
905 | ||
906 | prng_initialised = 1; | |
bfc9a610 | 907 | } |
264dce47 | 908 | |
9bdd5929 | 909 | #endif /* defined(USE_PRNGD) || defined(RANDOM_POOL) */ |