| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: noise.c,v 1.7 2004/04/02 01:03:49 mdw Exp $ |
| 4 | * |
| 5 | * Acquisition of environmental noise (Unix-specific) |
| 6 | * |
| 7 | * (c) 1998 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: noise.c,v $ |
| 33 | * Revision 1.7 2004/04/02 01:03:49 mdw |
| 34 | * Miscellaneous constification. |
| 35 | * |
| 36 | * Revision 1.6 2000/06/17 12:57:47 mdw |
| 37 | * New free counter noise generator, for use if /dev/random is |
| 38 | * unavailable. |
| 39 | * |
| 40 | * Revision 1.5 1999/12/22 15:57:55 mdw |
| 41 | * Label system-specific parts more clearly. |
| 42 | * |
| 43 | * Revision 1.4 1999/12/10 23:25:15 mdw |
| 44 | * Bug fix: remove old spurious fflush. |
| 45 | * |
| 46 | * Revision 1.3 1999/12/10 23:24:11 mdw |
| 47 | * Bug fix: flush buffers before forking. |
| 48 | * |
| 49 | * Revision 1.2 1999/11/11 00:59:08 mdw |
| 50 | * A bit of reformatting. Initialize the uid and gid correctly. |
| 51 | * |
| 52 | * Revision 1.1 1999/09/03 08:41:12 mdw |
| 53 | * Initial import. |
| 54 | * |
| 55 | */ |
| 56 | |
| 57 | /*----- Header files ------------------------------------------------------*/ |
| 58 | |
| 59 | #include "config.h" |
| 60 | |
| 61 | #include <setjmp.h> |
| 62 | #include <stdio.h> |
| 63 | #include <string.h> |
| 64 | #include <signal.h> |
| 65 | |
| 66 | #include <sys/types.h> |
| 67 | #include <sys/time.h> |
| 68 | #include <sys/wait.h> |
| 69 | |
| 70 | #include <fcntl.h> |
| 71 | #include <unistd.h> |
| 72 | |
| 73 | #ifdef HAVE_SETGROUPS |
| 74 | # include <grp.h> |
| 75 | #endif |
| 76 | |
| 77 | #include <mLib/bits.h> |
| 78 | #include <mLib/tv.h> |
| 79 | |
| 80 | #include "noise.h" |
| 81 | #include "paranoia.h" |
| 82 | #include "rand.h" |
| 83 | |
| 84 | /*----- Magical numbers ---------------------------------------------------*/ |
| 85 | |
| 86 | #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */ |
| 87 | #define MILLION 1000000 /* One million */ |
| 88 | |
| 89 | /*----- Noise source definition -------------------------------------------*/ |
| 90 | |
| 91 | const rand_source noise_source = { noise_acquire, noise_timer }; |
| 92 | |
| 93 | /*----- Static variables --------------------------------------------------*/ |
| 94 | |
| 95 | /* --- Timer differences --- */ |
| 96 | |
| 97 | static unsigned long noise_last; /* Last time recorded */ |
| 98 | static unsigned long noise_diff; /* Last first order difference */ |
| 99 | |
| 100 | /* --- Setuid program handling --- */ |
| 101 | |
| 102 | static uid_t noise_uid = NOISE_NOSETUID; /* Uid to set to spawn processes */ |
| 103 | static gid_t noise_gid = NOISE_NOSETGID; /* Gid to set to spawn processes */ |
| 104 | |
| 105 | /*----- Main code ---------------------------------------------------------*/ |
| 106 | |
| 107 | /* --- @bitcount@ --- * |
| 108 | * |
| 109 | * Arguments: @unsigned long x@ = a word containing bits |
| 110 | * |
| 111 | * Returns: The number of bits set in the word. |
| 112 | */ |
| 113 | |
| 114 | static int bitcount(unsigned long x) |
| 115 | { |
| 116 | char ctab[] = { 0, 1, 1, 2, 1, 2, 2, 3, |
| 117 | 1, 2, 2, 3, 2, 3, 3, 4 }; |
| 118 | int count = 0; |
| 119 | while (x) { |
| 120 | count += ctab[x & 0xfu]; |
| 121 | x >>= 4; |
| 122 | } |
| 123 | return (count); |
| 124 | } |
| 125 | |
| 126 | /* --- @timer@ --- * |
| 127 | * |
| 128 | * Arguments: @rand_pool *r@ = pointer to randomness pool |
| 129 | * @struct timeval *tv@ = pointer to time block |
| 130 | * |
| 131 | * Returns: Nonzero if some randomness was contributed. |
| 132 | * |
| 133 | * Use: Low-level timer contributor. |
| 134 | */ |
| 135 | |
| 136 | static int timer(rand_pool *r, struct timeval *tv) |
| 137 | { |
| 138 | unsigned long x, d, dd; |
| 139 | int de, dde; |
| 140 | int ret; |
| 141 | |
| 142 | x = tv->tv_usec + MILLION * tv->tv_sec; |
| 143 | d = x ^ noise_last; |
| 144 | dd = d ^ noise_diff; |
| 145 | noise_diff = d; |
| 146 | de = bitcount(d); |
| 147 | dde = bitcount(dd); |
| 148 | rand_add(r, tv, sizeof(*tv), de <= dde ? de : dde); |
| 149 | ret = (de || dde); |
| 150 | BURN(tv); x = d = dd = de = dde = 0; |
| 151 | return (ret); |
| 152 | } |
| 153 | |
| 154 | /* --- @noise_timer@ --- * |
| 155 | * |
| 156 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
| 157 | * |
| 158 | * Returns: Nonzero if some randomness was contributed. |
| 159 | * |
| 160 | * Use: Contributes the current time to the randomness pool. |
| 161 | * A guess at the number of useful bits contributed is made, |
| 162 | * based on first and second order bit differences. This isn't |
| 163 | * ever-so reliable, but it's better than nothing. |
| 164 | */ |
| 165 | |
| 166 | int noise_timer(rand_pool *r) |
| 167 | { |
| 168 | struct timeval tv; |
| 169 | gettimeofday(&tv, 0); |
| 170 | return (timer(r, &tv)); |
| 171 | } |
| 172 | |
| 173 | /* --- @noise_devrandom@ --- * |
| 174 | * |
| 175 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
| 176 | * |
| 177 | * Returns: Nonzero if some randomness was contributed. |
| 178 | * |
| 179 | * Use: Attempts to obtain some randomness from the system entropy |
| 180 | * pool. All bits from the device are assumed to be good. |
| 181 | */ |
| 182 | |
| 183 | int noise_devrandom(rand_pool *r) |
| 184 | { |
| 185 | int fd; |
| 186 | octet buf[RAND_POOLSZ]; |
| 187 | ssize_t len; |
| 188 | int ret = 0; |
| 189 | |
| 190 | /* --- Be nice to other clients of the random device --- * |
| 191 | * |
| 192 | * Attempt to open the device nonblockingly. If that works, take up to |
| 193 | * one bufferful and then close again. If there's no data to be read, |
| 194 | * then that's tough and we go away again, on the grounds that the device |
| 195 | * needs to get some more entropy from somewhere. |
| 196 | */ |
| 197 | |
| 198 | if ((fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK)) >= 0 || |
| 199 | (fd = open("/dev/arandom", O_RDONLY | O_NONBLOCK)) >= 0 || |
| 200 | (fd = open("/dev/random", O_RDONLY | O_NONBLOCK)) >= 0) { |
| 201 | if ((len = read(fd, buf, sizeof(buf))) > 0) { |
| 202 | rand_add(r, buf, len, len * 8); |
| 203 | BURN(buf); |
| 204 | ret = 1; |
| 205 | } |
| 206 | close(fd); |
| 207 | } |
| 208 | noise_timer(r); |
| 209 | return (ret); |
| 210 | } |
| 211 | |
| 212 | /* --- @noise_setid@ --- * |
| 213 | * |
| 214 | * Arguments: @uid_t uid@ = uid to set |
| 215 | * @gid_t gid@ = gid to set |
| 216 | * |
| 217 | * Returns: --- |
| 218 | * |
| 219 | * Use: Sets the user and group ids to be used by @noise_filter@ |
| 220 | * when running child processes. This is useful to avoid |
| 221 | * giving shell commands (even carefully written ones) undue |
| 222 | * privileges. This interface is Unix-specific |
| 223 | */ |
| 224 | |
| 225 | void noise_setid(uid_t uid, gid_t gid) |
| 226 | { |
| 227 | noise_uid = uid; |
| 228 | noise_gid = gid; |
| 229 | } |
| 230 | |
| 231 | /* --- @noise_filter@ --- * |
| 232 | * |
| 233 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
| 234 | * @int good@ = number of good bits per 1024 bits |
| 235 | * @const char *c@ = shell command to run |
| 236 | * |
| 237 | * Returns: Nonzero if some randomness was contributed. |
| 238 | * |
| 239 | * Use: Attempts to execute a shell command, and dump it into the |
| 240 | * randomness pool. A very rough estimate of the number of |
| 241 | * good bits is made, based on the size of the command's output. |
| 242 | * This function calls @waitpid@, so be careful. Before execing |
| 243 | * the command, the process uid and gid are set to the values |
| 244 | * given to @noise_setid@, and an attempt is made to reset the |
| 245 | * list of supplementary groups. The environment passed to |
| 246 | * the command has been severly lobotimized. If the command |
| 247 | * fails to complete within a short time period, it is killed. |
| 248 | * Paranoid use of close-on-exec flags for file descriptors is |
| 249 | * recommended. |
| 250 | * |
| 251 | * This interface is Unix-specific. |
| 252 | */ |
| 253 | |
| 254 | int noise_filter(rand_pool *r, int good, const char *c) |
| 255 | { |
| 256 | char buf[4096]; |
| 257 | pid_t kid; |
| 258 | int fd[2]; |
| 259 | struct timeval dead; |
| 260 | int ret = 0; |
| 261 | const char *env[] = { |
| 262 | "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc", |
| 263 | 0 |
| 264 | }; |
| 265 | |
| 266 | /* --- Remember when this business started --- */ |
| 267 | |
| 268 | gettimeofday(&dead, 0); |
| 269 | timer(r, &dead); |
| 270 | |
| 271 | /* --- Create a pipe --- */ |
| 272 | |
| 273 | if (pipe(fd)) |
| 274 | return (ret); |
| 275 | |
| 276 | /* --- Fork a child off --- */ |
| 277 | |
| 278 | fflush(0); |
| 279 | kid = fork(); |
| 280 | if (kid < 0) { |
| 281 | close(fd[0]); |
| 282 | close(fd[1]); |
| 283 | return (ret); |
| 284 | } |
| 285 | |
| 286 | /* --- Handle the child end of the deal --- */ |
| 287 | |
| 288 | if (kid == 0) { |
| 289 | int f; |
| 290 | |
| 291 | /* --- Set the pipe as standard output, close standard input --- */ |
| 292 | |
| 293 | close(0); close(1); close(2); |
| 294 | |
| 295 | if (fd[1] != 1) { |
| 296 | if (dup2(fd[1], 1) < 0) _exit(127); |
| 297 | close(fd[1]); |
| 298 | } |
| 299 | |
| 300 | if ((f = open("/dev/null", O_RDONLY)) != 0 || |
| 301 | (f = open("/dev/null", O_WRONLY)) != 2) |
| 302 | _exit(127); |
| 303 | |
| 304 | /* --- Play games with uids --- */ |
| 305 | |
| 306 | if (noise_gid != NOISE_NOSETGID) { |
| 307 | setgid(noise_gid); |
| 308 | setegid(noise_gid); |
| 309 | #ifdef HAVE_SETGROUPS |
| 310 | setgroups(1, &noise_gid); |
| 311 | #endif |
| 312 | } |
| 313 | |
| 314 | if (noise_uid != NOISE_NOSETUID) { |
| 315 | setuid(noise_uid); |
| 316 | seteuid(noise_uid); |
| 317 | } |
| 318 | |
| 319 | /* --- Start the process up --- */ |
| 320 | |
| 321 | execle("/bin/sh", "-c", c, (char *)0, env); |
| 322 | _exit(127); |
| 323 | } |
| 324 | |
| 325 | /* --- Sort out my end of the deal --- */ |
| 326 | |
| 327 | close(fd[1]); |
| 328 | |
| 329 | /* --- Decide on the deadline --- */ |
| 330 | |
| 331 | TV_ADDL(&dead, &dead, 0, NOISE_KIDLIFE); |
| 332 | |
| 333 | /* --- Now read, and think --- */ |
| 334 | |
| 335 | for (;;) { |
| 336 | struct timeval now, diff; |
| 337 | fd_set rd; |
| 338 | |
| 339 | gettimeofday(&now, 0); |
| 340 | timer(r, &now); |
| 341 | if (TV_CMP(&now, >, &dead)) |
| 342 | break; |
| 343 | TV_SUB(&diff, &dead, &now); |
| 344 | |
| 345 | FD_ZERO(&rd); |
| 346 | FD_SET(fd[0], &rd); |
| 347 | |
| 348 | if (select(fd[0] + 1, &rd, 0, 0, &diff) < 0) |
| 349 | break; |
| 350 | if (FD_ISSET(fd[0], &rd)) { |
| 351 | ssize_t sz; |
| 352 | int goodbits; |
| 353 | |
| 354 | if ((sz = read(fd[0], buf, sizeof(buf))) <= 0) |
| 355 | break; |
| 356 | goodbits = (sz * good) / 128; |
| 357 | rand_add(r, buf, sz, goodbits); |
| 358 | ret = 1; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /* --- We've finished with it: kill the child process --- * |
| 363 | * |
| 364 | * This is morally questionable. On the other hand, I don't want to be |
| 365 | * held up in the @waitpid@ call if I can possibly help it. Maybe a |
| 366 | * double-fork is worth doing here. |
| 367 | */ |
| 368 | |
| 369 | close(fd[0]); |
| 370 | BURN(buf); |
| 371 | noise_timer(r); |
| 372 | kill(kid, SIGKILL); |
| 373 | waitpid(kid, 0, 0); |
| 374 | return (ret); |
| 375 | } |
| 376 | |
| 377 | /* --- @noise_freewheel@ --- * |
| 378 | * |
| 379 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
| 380 | * |
| 381 | * Returns: Nonzero if some randomness was contributed. |
| 382 | * |
| 383 | * Use: Runs a free counter for a short while as a desparate attempt |
| 384 | * to get randomness from somewhere. This is actually quite |
| 385 | * effective. |
| 386 | */ |
| 387 | |
| 388 | #ifdef USE_FREEWHEEL |
| 389 | |
| 390 | static jmp_buf fwjmp; |
| 391 | |
| 392 | static void fwalarm(int sig) |
| 393 | { |
| 394 | siglongjmp(fwjmp, 1); |
| 395 | } |
| 396 | |
| 397 | int noise_freewheel(rand_pool *r) |
| 398 | { |
| 399 | void (*sigal)(int) = 0; |
| 400 | struct itimerval oitv, itv = { { 0, 0 }, { 0, 5000 } }; |
| 401 | int rc = 0; |
| 402 | volatile uint32 fwcount = 0; |
| 403 | |
| 404 | if (!sigsetjmp(fwjmp, 1)) { |
| 405 | if ((sigal = signal(SIGALRM, fwalarm)) == SIG_ERR) |
| 406 | return (0); |
| 407 | if (setitimer(ITIMER_REAL, &itv, &oitv)) |
| 408 | goto done; |
| 409 | for (;;) |
| 410 | fwcount++; |
| 411 | } else { |
| 412 | octet buf[4]; |
| 413 | STORE32(buf, fwcount); |
| 414 | rand_add(r, buf, sizeof(buf), 16); |
| 415 | rc = 1; |
| 416 | } |
| 417 | |
| 418 | done: |
| 419 | signal(SIGALRM, sigal); |
| 420 | TV_SUB(&oitv.it_value, &oitv.it_value, &itv.it_value); |
| 421 | setitimer(ITIMER_REAL, &oitv, 0); |
| 422 | return (rc); |
| 423 | } |
| 424 | |
| 425 | #else |
| 426 | |
| 427 | int noise_freewheel(rand_pool *r) |
| 428 | { |
| 429 | return (0); |
| 430 | } |
| 431 | |
| 432 | #endif |
| 433 | |
| 434 | /* --- @noise_enquire@ --- * |
| 435 | * |
| 436 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
| 437 | * |
| 438 | * Returns: Nonzero if some randomness was contributed. |
| 439 | * |
| 440 | * Use: Runs some shell commands to enquire about the prevailing |
| 441 | * environment. This can gather quite a lot of low-quality |
| 442 | * entropy. |
| 443 | */ |
| 444 | |
| 445 | int noise_enquire(rand_pool *r) |
| 446 | { |
| 447 | struct tab { |
| 448 | const char *cmd; |
| 449 | unsigned rate; |
| 450 | } tab[] = { |
| 451 | { "ps alxww || ps -elf", 16 }, |
| 452 | { "netstat -n", 6 }, |
| 453 | { "df", 20 }, |
| 454 | { "w", 6 }, |
| 455 | { "ls -align /tmp/.", 10 }, |
| 456 | { 0, 0 } |
| 457 | }; |
| 458 | int i; |
| 459 | |
| 460 | for (i = 0; tab[i].cmd; i++) |
| 461 | noise_filter(r, tab[i].rate, tab[i].cmd); |
| 462 | return (1); |
| 463 | } |
| 464 | |
| 465 | /* --- @noise_acquire@ --- * |
| 466 | * |
| 467 | * Arguments: @rand_pool *r@ = pointer to a randomness pool |
| 468 | * |
| 469 | * Returns: --- |
| 470 | * |
| 471 | * Use: Acquires some randomness from somewhere. |
| 472 | */ |
| 473 | |
| 474 | void noise_acquire(rand_pool *r) |
| 475 | { |
| 476 | unsigned i; |
| 477 | for (i = 0; i < 8; i++) |
| 478 | noise_freewheel(r); |
| 479 | if (!noise_devrandom(r)) { |
| 480 | noise_enquire(r); |
| 481 | for (i = 0; i < 8; i++) |
| 482 | noise_freewheel(r); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /*----- That's all, folks -------------------------------------------------*/ |